xref: /openbmc/linux/drivers/s390/crypto/ap_bus.c (revision f5ad1c74)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright IBM Corp. 2006, 2020
4  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5  *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
6  *	      Ralph Wuerthner <rwuerthn@de.ibm.com>
7  *	      Felix Beck <felix.beck@de.ibm.com>
8  *	      Holger Dengler <hd@linux.vnet.ibm.com>
9  *	      Harald Freudenberger <freude@linux.ibm.com>
10  *
11  * Adjunct processor bus.
12  */
13 
14 #define KMSG_COMPONENT "ap"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 
17 #include <linux/kernel_stat.h>
18 #include <linux/moduleparam.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/freezer.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/slab.h>
26 #include <linux/notifier.h>
27 #include <linux/kthread.h>
28 #include <linux/mutex.h>
29 #include <asm/airq.h>
30 #include <linux/atomic.h>
31 #include <asm/isc.h>
32 #include <linux/hrtimer.h>
33 #include <linux/ktime.h>
34 #include <asm/facility.h>
35 #include <linux/crypto.h>
36 #include <linux/mod_devicetable.h>
37 #include <linux/debugfs.h>
38 #include <linux/ctype.h>
39 
40 #include "ap_bus.h"
41 #include "ap_debug.h"
42 
43 /*
44  * Module parameters; note though this file itself isn't modular.
45  */
46 int ap_domain_index = -1;	/* Adjunct Processor Domain Index */
47 static DEFINE_SPINLOCK(ap_domain_lock);
48 module_param_named(domain, ap_domain_index, int, 0440);
49 MODULE_PARM_DESC(domain, "domain index for ap devices");
50 EXPORT_SYMBOL(ap_domain_index);
51 
52 static int ap_thread_flag;
53 module_param_named(poll_thread, ap_thread_flag, int, 0440);
54 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
55 
56 static char *apm_str;
57 module_param_named(apmask, apm_str, charp, 0440);
58 MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
59 
60 static char *aqm_str;
61 module_param_named(aqmask, aqm_str, charp, 0440);
62 MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
63 
64 static struct device *ap_root_device;
65 
66 /* Hashtable of all queue devices on the AP bus */
67 DEFINE_HASHTABLE(ap_queues, 8);
68 /* lock used for the ap_queues hashtable */
69 DEFINE_SPINLOCK(ap_queues_lock);
70 
71 /* Default permissions (ioctl, card and domain masking) */
72 struct ap_perms ap_perms;
73 EXPORT_SYMBOL(ap_perms);
74 DEFINE_MUTEX(ap_perms_mutex);
75 EXPORT_SYMBOL(ap_perms_mutex);
76 
77 /* # of bus scans since init */
78 static atomic64_t ap_scan_bus_count;
79 
80 /* completion for initial APQN bindings complete */
81 static DECLARE_COMPLETION(ap_init_apqn_bindings_complete);
82 
83 static struct ap_config_info *ap_qci_info;
84 
85 /*
86  * AP bus related debug feature things.
87  */
88 debug_info_t *ap_dbf_info;
89 
90 /*
91  * Workqueue timer for bus rescan.
92  */
93 static struct timer_list ap_config_timer;
94 static int ap_config_time = AP_CONFIG_TIME;
95 static void ap_scan_bus(struct work_struct *);
96 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
97 
98 /*
99  * Tasklet & timer for AP request polling and interrupts
100  */
101 static void ap_tasklet_fn(unsigned long);
102 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
103 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
104 static struct task_struct *ap_poll_kthread;
105 static DEFINE_MUTEX(ap_poll_thread_mutex);
106 static DEFINE_SPINLOCK(ap_poll_timer_lock);
107 static struct hrtimer ap_poll_timer;
108 /*
109  * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
110  * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
111  */
112 static unsigned long long poll_timeout = 250000;
113 
114 /* Maximum domain id, if not given via qci */
115 static int ap_max_domain_id = 15;
116 /* Maximum adapter id, if not given via qci */
117 static int ap_max_adapter_id = 63;
118 
119 static struct bus_type ap_bus_type;
120 
121 /* Adapter interrupt definitions */
122 static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
123 
124 static int ap_airq_flag;
125 
126 static struct airq_struct ap_airq = {
127 	.handler = ap_interrupt_handler,
128 	.isc = AP_ISC,
129 };
130 
131 /**
132  * ap_using_interrupts() - Returns non-zero if interrupt support is
133  * available.
134  */
135 static inline int ap_using_interrupts(void)
136 {
137 	return ap_airq_flag;
138 }
139 
140 /**
141  * ap_airq_ptr() - Get the address of the adapter interrupt indicator
142  *
143  * Returns the address of the local-summary-indicator of the adapter
144  * interrupt handler for AP, or NULL if adapter interrupts are not
145  * available.
146  */
147 void *ap_airq_ptr(void)
148 {
149 	if (ap_using_interrupts())
150 		return ap_airq.lsi_ptr;
151 	return NULL;
152 }
153 
154 /**
155  * ap_interrupts_available(): Test if AP interrupts are available.
156  *
157  * Returns 1 if AP interrupts are available.
158  */
159 static int ap_interrupts_available(void)
160 {
161 	return test_facility(65);
162 }
163 
164 /**
165  * ap_qci_available(): Test if AP configuration
166  * information can be queried via QCI subfunction.
167  *
168  * Returns 1 if subfunction PQAP(QCI) is available.
169  */
170 static int ap_qci_available(void)
171 {
172 	return test_facility(12);
173 }
174 
175 /**
176  * ap_apft_available(): Test if AP facilities test (APFT)
177  * facility is available.
178  *
179  * Returns 1 if APFT is is available.
180  */
181 static int ap_apft_available(void)
182 {
183 	return test_facility(15);
184 }
185 
186 /*
187  * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
188  *
189  * Returns 1 if the QACT subfunction is available.
190  */
191 static inline int ap_qact_available(void)
192 {
193 	if (ap_qci_info)
194 		return ap_qci_info->qact;
195 	return 0;
196 }
197 
198 /*
199  * ap_fetch_qci_info(): Fetch cryptographic config info
200  *
201  * Returns the ap configuration info fetched via PQAP(QCI).
202  * On success 0 is returned, on failure a negative errno
203  * is returned, e.g. if the PQAP(QCI) instruction is not
204  * available, the return value will be -EOPNOTSUPP.
205  */
206 static inline int ap_fetch_qci_info(struct ap_config_info *info)
207 {
208 	if (!ap_qci_available())
209 		return -EOPNOTSUPP;
210 	if (!info)
211 		return -EINVAL;
212 	return ap_qci(info);
213 }
214 
215 /**
216  * ap_init_qci_info(): Allocate and query qci config info.
217  * Does also update the static variables ap_max_domain_id
218  * and ap_max_adapter_id if this info is available.
219 
220  */
221 static void __init ap_init_qci_info(void)
222 {
223 	if (!ap_qci_available()) {
224 		AP_DBF_INFO("%s QCI not supported\n", __func__);
225 		return;
226 	}
227 
228 	ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
229 	if (!ap_qci_info)
230 		return;
231 	if (ap_fetch_qci_info(ap_qci_info) != 0) {
232 		kfree(ap_qci_info);
233 		ap_qci_info = NULL;
234 		return;
235 	}
236 	AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
237 
238 	if (ap_qci_info->apxa) {
239 		if (ap_qci_info->Na) {
240 			ap_max_adapter_id = ap_qci_info->Na;
241 			AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
242 				    __func__, ap_max_adapter_id);
243 		}
244 		if (ap_qci_info->Nd) {
245 			ap_max_domain_id = ap_qci_info->Nd;
246 			AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
247 				    __func__, ap_max_domain_id);
248 		}
249 	}
250 }
251 
252 /*
253  * ap_test_config(): helper function to extract the nrth bit
254  *		     within the unsigned int array field.
255  */
256 static inline int ap_test_config(unsigned int *field, unsigned int nr)
257 {
258 	return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
259 }
260 
261 /*
262  * ap_test_config_card_id(): Test, whether an AP card ID is configured.
263  *
264  * Returns 0 if the card is not configured
265  *	   1 if the card is configured or
266  *	     if the configuration information is not available
267  */
268 static inline int ap_test_config_card_id(unsigned int id)
269 {
270 	if (id > ap_max_adapter_id)
271 		return 0;
272 	if (ap_qci_info)
273 		return ap_test_config(ap_qci_info->apm, id);
274 	return 1;
275 }
276 
277 /*
278  * ap_test_config_usage_domain(): Test, whether an AP usage domain
279  * is configured.
280  *
281  * Returns 0 if the usage domain is not configured
282  *	   1 if the usage domain is configured or
283  *	     if the configuration information is not available
284  */
285 int ap_test_config_usage_domain(unsigned int domain)
286 {
287 	if (domain > ap_max_domain_id)
288 		return 0;
289 	if (ap_qci_info)
290 		return ap_test_config(ap_qci_info->aqm, domain);
291 	return 1;
292 }
293 EXPORT_SYMBOL(ap_test_config_usage_domain);
294 
295 /*
296  * ap_test_config_ctrl_domain(): Test, whether an AP control domain
297  * is configured.
298  * @domain AP control domain ID
299  *
300  * Returns 1 if the control domain is configured
301  *	   0 in all other cases
302  */
303 int ap_test_config_ctrl_domain(unsigned int domain)
304 {
305 	if (!ap_qci_info || domain > ap_max_domain_id)
306 		return 0;
307 	return ap_test_config(ap_qci_info->adm, domain);
308 }
309 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
310 
311 /*
312  * ap_queue_info(): Check and get AP queue info.
313  * Returns true if TAPQ succeeded and the info is filled or
314  * false otherwise.
315  */
316 static bool ap_queue_info(ap_qid_t qid, int *q_type,
317 			  unsigned int *q_fac, int *q_depth, bool *q_decfg)
318 {
319 	struct ap_queue_status status;
320 	unsigned long info = 0;
321 
322 	/* make sure we don't run into a specifiation exception */
323 	if (AP_QID_CARD(qid) > ap_max_adapter_id ||
324 	    AP_QID_QUEUE(qid) > ap_max_domain_id)
325 		return false;
326 
327 	/* call TAPQ on this APQN */
328 	status = ap_test_queue(qid, ap_apft_available(), &info);
329 	switch (status.response_code) {
330 	case AP_RESPONSE_NORMAL:
331 	case AP_RESPONSE_RESET_IN_PROGRESS:
332 	case AP_RESPONSE_DECONFIGURED:
333 	case AP_RESPONSE_CHECKSTOPPED:
334 	case AP_RESPONSE_BUSY:
335 		/*
336 		 * According to the architecture in all these cases the
337 		 * info should be filled. All bits 0 is not possible as
338 		 * there is at least one of the mode bits set.
339 		 */
340 		if (WARN_ON_ONCE(!info))
341 			return false;
342 		*q_type = (int)((info >> 24) & 0xff);
343 		*q_fac = (unsigned int)(info >> 32);
344 		*q_depth = (int)(info & 0xff);
345 		*q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
346 		switch (*q_type) {
347 			/* For CEX2 and CEX3 the available functions
348 			 * are not reflected by the facilities bits.
349 			 * Instead it is coded into the type. So here
350 			 * modify the function bits based on the type.
351 			 */
352 		case AP_DEVICE_TYPE_CEX2A:
353 		case AP_DEVICE_TYPE_CEX3A:
354 			*q_fac |= 0x08000000;
355 			break;
356 		case AP_DEVICE_TYPE_CEX2C:
357 		case AP_DEVICE_TYPE_CEX3C:
358 			*q_fac |= 0x10000000;
359 			break;
360 		default:
361 			break;
362 		}
363 		return true;
364 	default:
365 		/*
366 		 * A response code which indicates, there is no info available.
367 		 */
368 		return false;
369 	}
370 }
371 
372 void ap_wait(enum ap_sm_wait wait)
373 {
374 	ktime_t hr_time;
375 
376 	switch (wait) {
377 	case AP_SM_WAIT_AGAIN:
378 	case AP_SM_WAIT_INTERRUPT:
379 		if (ap_using_interrupts())
380 			break;
381 		if (ap_poll_kthread) {
382 			wake_up(&ap_poll_wait);
383 			break;
384 		}
385 		fallthrough;
386 	case AP_SM_WAIT_TIMEOUT:
387 		spin_lock_bh(&ap_poll_timer_lock);
388 		if (!hrtimer_is_queued(&ap_poll_timer)) {
389 			hr_time = poll_timeout;
390 			hrtimer_forward_now(&ap_poll_timer, hr_time);
391 			hrtimer_restart(&ap_poll_timer);
392 		}
393 		spin_unlock_bh(&ap_poll_timer_lock);
394 		break;
395 	case AP_SM_WAIT_NONE:
396 	default:
397 		break;
398 	}
399 }
400 
401 /**
402  * ap_request_timeout(): Handling of request timeouts
403  * @t: timer making this callback
404  *
405  * Handles request timeouts.
406  */
407 void ap_request_timeout(struct timer_list *t)
408 {
409 	struct ap_queue *aq = from_timer(aq, t, timeout);
410 
411 	spin_lock_bh(&aq->lock);
412 	ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
413 	spin_unlock_bh(&aq->lock);
414 }
415 
416 /**
417  * ap_poll_timeout(): AP receive polling for finished AP requests.
418  * @unused: Unused pointer.
419  *
420  * Schedules the AP tasklet using a high resolution timer.
421  */
422 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
423 {
424 	tasklet_schedule(&ap_tasklet);
425 	return HRTIMER_NORESTART;
426 }
427 
428 /**
429  * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
430  * @airq: pointer to adapter interrupt descriptor
431  */
432 static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
433 {
434 	inc_irq_stat(IRQIO_APB);
435 	tasklet_schedule(&ap_tasklet);
436 }
437 
438 /**
439  * ap_tasklet_fn(): Tasklet to poll all AP devices.
440  * @dummy: Unused variable
441  *
442  * Poll all AP devices on the bus.
443  */
444 static void ap_tasklet_fn(unsigned long dummy)
445 {
446 	int bkt;
447 	struct ap_queue *aq;
448 	enum ap_sm_wait wait = AP_SM_WAIT_NONE;
449 
450 	/* Reset the indicator if interrupts are used. Thus new interrupts can
451 	 * be received. Doing it in the beginning of the tasklet is therefor
452 	 * important that no requests on any AP get lost.
453 	 */
454 	if (ap_using_interrupts())
455 		xchg(ap_airq.lsi_ptr, 0);
456 
457 	spin_lock_bh(&ap_queues_lock);
458 	hash_for_each(ap_queues, bkt, aq, hnode) {
459 		spin_lock_bh(&aq->lock);
460 		wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
461 		spin_unlock_bh(&aq->lock);
462 	}
463 	spin_unlock_bh(&ap_queues_lock);
464 
465 	ap_wait(wait);
466 }
467 
468 static int ap_pending_requests(void)
469 {
470 	int bkt;
471 	struct ap_queue *aq;
472 
473 	spin_lock_bh(&ap_queues_lock);
474 	hash_for_each(ap_queues, bkt, aq, hnode) {
475 		if (aq->queue_count == 0)
476 			continue;
477 		spin_unlock_bh(&ap_queues_lock);
478 		return 1;
479 	}
480 	spin_unlock_bh(&ap_queues_lock);
481 	return 0;
482 }
483 
484 /**
485  * ap_poll_thread(): Thread that polls for finished requests.
486  * @data: Unused pointer
487  *
488  * AP bus poll thread. The purpose of this thread is to poll for
489  * finished requests in a loop if there is a "free" cpu - that is
490  * a cpu that doesn't have anything better to do. The polling stops
491  * as soon as there is another task or if all messages have been
492  * delivered.
493  */
494 static int ap_poll_thread(void *data)
495 {
496 	DECLARE_WAITQUEUE(wait, current);
497 
498 	set_user_nice(current, MAX_NICE);
499 	set_freezable();
500 	while (!kthread_should_stop()) {
501 		add_wait_queue(&ap_poll_wait, &wait);
502 		set_current_state(TASK_INTERRUPTIBLE);
503 		if (!ap_pending_requests()) {
504 			schedule();
505 			try_to_freeze();
506 		}
507 		set_current_state(TASK_RUNNING);
508 		remove_wait_queue(&ap_poll_wait, &wait);
509 		if (need_resched()) {
510 			schedule();
511 			try_to_freeze();
512 			continue;
513 		}
514 		ap_tasklet_fn(0);
515 	}
516 
517 	return 0;
518 }
519 
520 static int ap_poll_thread_start(void)
521 {
522 	int rc;
523 
524 	if (ap_using_interrupts() || ap_poll_kthread)
525 		return 0;
526 	mutex_lock(&ap_poll_thread_mutex);
527 	ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
528 	rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
529 	if (rc)
530 		ap_poll_kthread = NULL;
531 	mutex_unlock(&ap_poll_thread_mutex);
532 	return rc;
533 }
534 
535 static void ap_poll_thread_stop(void)
536 {
537 	if (!ap_poll_kthread)
538 		return;
539 	mutex_lock(&ap_poll_thread_mutex);
540 	kthread_stop(ap_poll_kthread);
541 	ap_poll_kthread = NULL;
542 	mutex_unlock(&ap_poll_thread_mutex);
543 }
544 
545 #define is_card_dev(x) ((x)->parent == ap_root_device)
546 #define is_queue_dev(x) ((x)->parent != ap_root_device)
547 
548 /**
549  * ap_bus_match()
550  * @dev: Pointer to device
551  * @drv: Pointer to device_driver
552  *
553  * AP bus driver registration/unregistration.
554  */
555 static int ap_bus_match(struct device *dev, struct device_driver *drv)
556 {
557 	struct ap_driver *ap_drv = to_ap_drv(drv);
558 	struct ap_device_id *id;
559 
560 	/*
561 	 * Compare device type of the device with the list of
562 	 * supported types of the device_driver.
563 	 */
564 	for (id = ap_drv->ids; id->match_flags; id++) {
565 		if (is_card_dev(dev) &&
566 		    id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
567 		    id->dev_type == to_ap_dev(dev)->device_type)
568 			return 1;
569 		if (is_queue_dev(dev) &&
570 		    id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
571 		    id->dev_type == to_ap_dev(dev)->device_type)
572 			return 1;
573 	}
574 	return 0;
575 }
576 
577 /**
578  * ap_uevent(): Uevent function for AP devices.
579  * @dev: Pointer to device
580  * @env: Pointer to kobj_uevent_env
581  *
582  * It sets up a single environment variable DEV_TYPE which contains the
583  * hardware device type.
584  */
585 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
586 {
587 	int rc;
588 	struct ap_device *ap_dev = to_ap_dev(dev);
589 
590 	/* Uevents from ap bus core don't need extensions to the env */
591 	if (dev == ap_root_device)
592 		return 0;
593 
594 	/* Set up DEV_TYPE environment variable. */
595 	rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
596 	if (rc)
597 		return rc;
598 
599 	/* Add MODALIAS= */
600 	rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
601 	if (rc)
602 		return rc;
603 
604 	return 0;
605 }
606 
607 static void ap_send_init_scan_done_uevent(void)
608 {
609 	char *envp[] = { "INITSCAN=done", NULL };
610 
611 	kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
612 }
613 
614 static void ap_send_bindings_complete_uevent(void)
615 {
616 	char *envp[] = { "BINDINGS=complete", NULL };
617 
618 	kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
619 }
620 
621 /*
622  * calc # of bound APQNs
623  */
624 
625 struct __ap_calc_ctrs {
626 	unsigned int apqns;
627 	unsigned int bound;
628 };
629 
630 static int __ap_calc_helper(struct device *dev, void *arg)
631 {
632 	struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *) arg;
633 
634 	if (is_queue_dev(dev)) {
635 		pctrs->apqns++;
636 		if ((to_ap_dev(dev))->drv)
637 			pctrs->bound++;
638 	}
639 
640 	return 0;
641 }
642 
643 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
644 {
645 	struct __ap_calc_ctrs ctrs;
646 
647 	memset(&ctrs, 0, sizeof(ctrs));
648 	bus_for_each_dev(&ap_bus_type, NULL, (void *) &ctrs, __ap_calc_helper);
649 
650 	*apqns = ctrs.apqns;
651 	*bound = ctrs.bound;
652 }
653 
654 /*
655  * After initial ap bus scan do check if all existing APQNs are
656  * bound to device drivers.
657  */
658 static void ap_check_bindings_complete(void)
659 {
660 	unsigned int apqns, bound;
661 
662 	if (atomic64_read(&ap_scan_bus_count) >= 1) {
663 		ap_calc_bound_apqns(&apqns, &bound);
664 		if (bound == apqns) {
665 			if (!completion_done(&ap_init_apqn_bindings_complete)) {
666 				complete_all(&ap_init_apqn_bindings_complete);
667 				AP_DBF(DBF_INFO, "%s complete\n", __func__);
668 			}
669 			ap_send_bindings_complete_uevent();
670 		}
671 	}
672 }
673 
674 /*
675  * Interface to wait for the AP bus to have done one initial ap bus
676  * scan and all detected APQNs have been bound to device drivers.
677  * If these both conditions are not fulfilled, this function blocks
678  * on a condition with wait_for_completion_interruptible_timeout().
679  * If these both conditions are fulfilled (before the timeout hits)
680  * the return value is 0. If the timeout (in jiffies) hits instead
681  * -ETIME is returned. On failures negative return values are
682  * returned to the caller.
683  */
684 int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
685 {
686 	long l;
687 
688 	if (completion_done(&ap_init_apqn_bindings_complete))
689 		return 0;
690 
691 	if (timeout)
692 		l = wait_for_completion_interruptible_timeout(
693 			&ap_init_apqn_bindings_complete, timeout);
694 	else
695 		l = wait_for_completion_interruptible(
696 			&ap_init_apqn_bindings_complete);
697 	if (l < 0)
698 		return l == -ERESTARTSYS ? -EINTR : l;
699 	else if (l == 0 && timeout)
700 		return -ETIME;
701 
702 	return 0;
703 }
704 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
705 
706 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
707 {
708 	if (is_queue_dev(dev) &&
709 	    AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
710 		device_unregister(dev);
711 	return 0;
712 }
713 
714 static int __ap_revise_reserved(struct device *dev, void *dummy)
715 {
716 	int rc, card, queue, devres, drvres;
717 
718 	if (is_queue_dev(dev)) {
719 		card = AP_QID_CARD(to_ap_queue(dev)->qid);
720 		queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
721 		mutex_lock(&ap_perms_mutex);
722 		devres = test_bit_inv(card, ap_perms.apm)
723 			&& test_bit_inv(queue, ap_perms.aqm);
724 		mutex_unlock(&ap_perms_mutex);
725 		drvres = to_ap_drv(dev->driver)->flags
726 			& AP_DRIVER_FLAG_DEFAULT;
727 		if (!!devres != !!drvres) {
728 			AP_DBF_DBG("reprobing queue=%02x.%04x\n",
729 				   card, queue);
730 			rc = device_reprobe(dev);
731 		}
732 	}
733 
734 	return 0;
735 }
736 
737 static void ap_bus_revise_bindings(void)
738 {
739 	bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
740 }
741 
742 int ap_owned_by_def_drv(int card, int queue)
743 {
744 	int rc = 0;
745 
746 	if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
747 		return -EINVAL;
748 
749 	mutex_lock(&ap_perms_mutex);
750 
751 	if (test_bit_inv(card, ap_perms.apm)
752 	    && test_bit_inv(queue, ap_perms.aqm))
753 		rc = 1;
754 
755 	mutex_unlock(&ap_perms_mutex);
756 
757 	return rc;
758 }
759 EXPORT_SYMBOL(ap_owned_by_def_drv);
760 
761 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
762 				       unsigned long *aqm)
763 {
764 	int card, queue, rc = 0;
765 
766 	mutex_lock(&ap_perms_mutex);
767 
768 	for (card = 0; !rc && card < AP_DEVICES; card++)
769 		if (test_bit_inv(card, apm) &&
770 		    test_bit_inv(card, ap_perms.apm))
771 			for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
772 				if (test_bit_inv(queue, aqm) &&
773 				    test_bit_inv(queue, ap_perms.aqm))
774 					rc = 1;
775 
776 	mutex_unlock(&ap_perms_mutex);
777 
778 	return rc;
779 }
780 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
781 
782 static int ap_device_probe(struct device *dev)
783 {
784 	struct ap_device *ap_dev = to_ap_dev(dev);
785 	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
786 	int card, queue, devres, drvres, rc = -ENODEV;
787 
788 	if (!get_device(dev))
789 		return rc;
790 
791 	if (is_queue_dev(dev)) {
792 		/*
793 		 * If the apqn is marked as reserved/used by ap bus and
794 		 * default drivers, only probe with drivers with the default
795 		 * flag set. If it is not marked, only probe with drivers
796 		 * with the default flag not set.
797 		 */
798 		card = AP_QID_CARD(to_ap_queue(dev)->qid);
799 		queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
800 		mutex_lock(&ap_perms_mutex);
801 		devres = test_bit_inv(card, ap_perms.apm)
802 			&& test_bit_inv(queue, ap_perms.aqm);
803 		mutex_unlock(&ap_perms_mutex);
804 		drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
805 		if (!!devres != !!drvres)
806 			goto out;
807 	}
808 
809 	/* Add queue/card to list of active queues/cards */
810 	spin_lock_bh(&ap_queues_lock);
811 	if (is_queue_dev(dev))
812 		hash_add(ap_queues, &to_ap_queue(dev)->hnode,
813 			 to_ap_queue(dev)->qid);
814 	spin_unlock_bh(&ap_queues_lock);
815 
816 	ap_dev->drv = ap_drv;
817 	rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
818 
819 	if (rc) {
820 		spin_lock_bh(&ap_queues_lock);
821 		if (is_queue_dev(dev))
822 			hash_del(&to_ap_queue(dev)->hnode);
823 		spin_unlock_bh(&ap_queues_lock);
824 		ap_dev->drv = NULL;
825 	} else
826 		ap_check_bindings_complete();
827 
828 out:
829 	if (rc)
830 		put_device(dev);
831 	return rc;
832 }
833 
834 static int ap_device_remove(struct device *dev)
835 {
836 	struct ap_device *ap_dev = to_ap_dev(dev);
837 	struct ap_driver *ap_drv = ap_dev->drv;
838 
839 	/* prepare ap queue device removal */
840 	if (is_queue_dev(dev))
841 		ap_queue_prepare_remove(to_ap_queue(dev));
842 
843 	/* driver's chance to clean up gracefully */
844 	if (ap_drv->remove)
845 		ap_drv->remove(ap_dev);
846 
847 	/* now do the ap queue device remove */
848 	if (is_queue_dev(dev))
849 		ap_queue_remove(to_ap_queue(dev));
850 
851 	/* Remove queue/card from list of active queues/cards */
852 	spin_lock_bh(&ap_queues_lock);
853 	if (is_queue_dev(dev))
854 		hash_del(&to_ap_queue(dev)->hnode);
855 	spin_unlock_bh(&ap_queues_lock);
856 	ap_dev->drv = NULL;
857 
858 	put_device(dev);
859 
860 	return 0;
861 }
862 
863 struct ap_queue *ap_get_qdev(ap_qid_t qid)
864 {
865 	int bkt;
866 	struct ap_queue *aq;
867 
868 	spin_lock_bh(&ap_queues_lock);
869 	hash_for_each(ap_queues, bkt, aq, hnode) {
870 		if (aq->qid == qid) {
871 			get_device(&aq->ap_dev.device);
872 			spin_unlock_bh(&ap_queues_lock);
873 			return aq;
874 		}
875 	}
876 	spin_unlock_bh(&ap_queues_lock);
877 
878 	return NULL;
879 }
880 EXPORT_SYMBOL(ap_get_qdev);
881 
882 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
883 		       char *name)
884 {
885 	struct device_driver *drv = &ap_drv->driver;
886 
887 	drv->bus = &ap_bus_type;
888 	drv->probe = ap_device_probe;
889 	drv->remove = ap_device_remove;
890 	drv->owner = owner;
891 	drv->name = name;
892 	return driver_register(drv);
893 }
894 EXPORT_SYMBOL(ap_driver_register);
895 
896 void ap_driver_unregister(struct ap_driver *ap_drv)
897 {
898 	driver_unregister(&ap_drv->driver);
899 }
900 EXPORT_SYMBOL(ap_driver_unregister);
901 
902 void ap_bus_force_rescan(void)
903 {
904 	/* processing a asynchronous bus rescan */
905 	del_timer(&ap_config_timer);
906 	queue_work(system_long_wq, &ap_scan_work);
907 	flush_work(&ap_scan_work);
908 }
909 EXPORT_SYMBOL(ap_bus_force_rescan);
910 
911 /*
912 * A config change has happened, force an ap bus rescan.
913 */
914 void ap_bus_cfg_chg(void)
915 {
916 	AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
917 
918 	ap_bus_force_rescan();
919 }
920 
921 /*
922  * hex2bitmap() - parse hex mask string and set bitmap.
923  * Valid strings are "0x012345678" with at least one valid hex number.
924  * Rest of the bitmap to the right is padded with 0. No spaces allowed
925  * within the string, the leading 0x may be omitted.
926  * Returns the bitmask with exactly the bits set as given by the hex
927  * string (both in big endian order).
928  */
929 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
930 {
931 	int i, n, b;
932 
933 	/* bits needs to be a multiple of 8 */
934 	if (bits & 0x07)
935 		return -EINVAL;
936 
937 	if (str[0] == '0' && str[1] == 'x')
938 		str++;
939 	if (*str == 'x')
940 		str++;
941 
942 	for (i = 0; isxdigit(*str) && i < bits; str++) {
943 		b = hex_to_bin(*str);
944 		for (n = 0; n < 4; n++)
945 			if (b & (0x08 >> n))
946 				set_bit_inv(i + n, bitmap);
947 		i += 4;
948 	}
949 
950 	if (*str == '\n')
951 		str++;
952 	if (*str)
953 		return -EINVAL;
954 	return 0;
955 }
956 
957 /*
958  * modify_bitmap() - parse bitmask argument and modify an existing
959  * bit mask accordingly. A concatenation (done with ',') of these
960  * terms is recognized:
961  *   +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
962  * <bitnr> may be any valid number (hex, decimal or octal) in the range
963  * 0...bits-1; the leading + or - is required. Here are some examples:
964  *   +0-15,+32,-128,-0xFF
965  *   -0-255,+1-16,+0x128
966  *   +1,+2,+3,+4,-5,-7-10
967  * Returns the new bitmap after all changes have been applied. Every
968  * positive value in the string will set a bit and every negative value
969  * in the string will clear a bit. As a bit may be touched more than once,
970  * the last 'operation' wins:
971  * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
972  * cleared again. All other bits are unmodified.
973  */
974 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
975 {
976 	int a, i, z;
977 	char *np, sign;
978 
979 	/* bits needs to be a multiple of 8 */
980 	if (bits & 0x07)
981 		return -EINVAL;
982 
983 	while (*str) {
984 		sign = *str++;
985 		if (sign != '+' && sign != '-')
986 			return -EINVAL;
987 		a = z = simple_strtoul(str, &np, 0);
988 		if (str == np || a >= bits)
989 			return -EINVAL;
990 		str = np;
991 		if (*str == '-') {
992 			z = simple_strtoul(++str, &np, 0);
993 			if (str == np || a > z || z >= bits)
994 				return -EINVAL;
995 			str = np;
996 		}
997 		for (i = a; i <= z; i++)
998 			if (sign == '+')
999 				set_bit_inv(i, bitmap);
1000 			else
1001 				clear_bit_inv(i, bitmap);
1002 		while (*str == ',' || *str == '\n')
1003 			str++;
1004 	}
1005 
1006 	return 0;
1007 }
1008 
1009 int ap_parse_mask_str(const char *str,
1010 		      unsigned long *bitmap, int bits,
1011 		      struct mutex *lock)
1012 {
1013 	unsigned long *newmap, size;
1014 	int rc;
1015 
1016 	/* bits needs to be a multiple of 8 */
1017 	if (bits & 0x07)
1018 		return -EINVAL;
1019 
1020 	size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
1021 	newmap = kmalloc(size, GFP_KERNEL);
1022 	if (!newmap)
1023 		return -ENOMEM;
1024 	if (mutex_lock_interruptible(lock)) {
1025 		kfree(newmap);
1026 		return -ERESTARTSYS;
1027 	}
1028 
1029 	if (*str == '+' || *str == '-') {
1030 		memcpy(newmap, bitmap, size);
1031 		rc = modify_bitmap(str, newmap, bits);
1032 	} else {
1033 		memset(newmap, 0, size);
1034 		rc = hex2bitmap(str, newmap, bits);
1035 	}
1036 	if (rc == 0)
1037 		memcpy(bitmap, newmap, size);
1038 	mutex_unlock(lock);
1039 	kfree(newmap);
1040 	return rc;
1041 }
1042 EXPORT_SYMBOL(ap_parse_mask_str);
1043 
1044 /*
1045  * AP bus attributes.
1046  */
1047 
1048 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1049 {
1050 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1051 }
1052 
1053 static ssize_t ap_domain_store(struct bus_type *bus,
1054 			       const char *buf, size_t count)
1055 {
1056 	int domain;
1057 
1058 	if (sscanf(buf, "%i\n", &domain) != 1 ||
1059 	    domain < 0 || domain > ap_max_domain_id ||
1060 	    !test_bit_inv(domain, ap_perms.aqm))
1061 		return -EINVAL;
1062 
1063 	spin_lock_bh(&ap_domain_lock);
1064 	ap_domain_index = domain;
1065 	spin_unlock_bh(&ap_domain_lock);
1066 
1067 	AP_DBF_INFO("stored new default domain=%d\n", domain);
1068 
1069 	return count;
1070 }
1071 
1072 static BUS_ATTR_RW(ap_domain);
1073 
1074 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1075 {
1076 	if (!ap_qci_info)	/* QCI not supported */
1077 		return scnprintf(buf, PAGE_SIZE, "not supported\n");
1078 
1079 	return scnprintf(buf, PAGE_SIZE,
1080 			 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1081 			 ap_qci_info->adm[0], ap_qci_info->adm[1],
1082 			 ap_qci_info->adm[2], ap_qci_info->adm[3],
1083 			 ap_qci_info->adm[4], ap_qci_info->adm[5],
1084 			 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1085 }
1086 
1087 static BUS_ATTR_RO(ap_control_domain_mask);
1088 
1089 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1090 {
1091 	if (!ap_qci_info)	/* QCI not supported */
1092 		return scnprintf(buf, PAGE_SIZE, "not supported\n");
1093 
1094 	return scnprintf(buf, PAGE_SIZE,
1095 			 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1096 			 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1097 			 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1098 			 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1099 			 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1100 }
1101 
1102 static BUS_ATTR_RO(ap_usage_domain_mask);
1103 
1104 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1105 {
1106 	if (!ap_qci_info)	/* QCI not supported */
1107 		return scnprintf(buf, PAGE_SIZE, "not supported\n");
1108 
1109 	return scnprintf(buf, PAGE_SIZE,
1110 			 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1111 			 ap_qci_info->apm[0], ap_qci_info->apm[1],
1112 			 ap_qci_info->apm[2], ap_qci_info->apm[3],
1113 			 ap_qci_info->apm[4], ap_qci_info->apm[5],
1114 			 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1115 }
1116 
1117 static BUS_ATTR_RO(ap_adapter_mask);
1118 
1119 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1120 {
1121 	return scnprintf(buf, PAGE_SIZE, "%d\n",
1122 			 ap_using_interrupts() ? 1 : 0);
1123 }
1124 
1125 static BUS_ATTR_RO(ap_interrupts);
1126 
1127 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1128 {
1129 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1130 }
1131 
1132 static ssize_t config_time_store(struct bus_type *bus,
1133 				 const char *buf, size_t count)
1134 {
1135 	int time;
1136 
1137 	if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1138 		return -EINVAL;
1139 	ap_config_time = time;
1140 	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1141 	return count;
1142 }
1143 
1144 static BUS_ATTR_RW(config_time);
1145 
1146 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1147 {
1148 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1149 }
1150 
1151 static ssize_t poll_thread_store(struct bus_type *bus,
1152 				 const char *buf, size_t count)
1153 {
1154 	int flag, rc;
1155 
1156 	if (sscanf(buf, "%d\n", &flag) != 1)
1157 		return -EINVAL;
1158 	if (flag) {
1159 		rc = ap_poll_thread_start();
1160 		if (rc)
1161 			count = rc;
1162 	} else
1163 		ap_poll_thread_stop();
1164 	return count;
1165 }
1166 
1167 static BUS_ATTR_RW(poll_thread);
1168 
1169 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1170 {
1171 	return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1172 }
1173 
1174 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1175 				  size_t count)
1176 {
1177 	unsigned long long time;
1178 	ktime_t hr_time;
1179 
1180 	/* 120 seconds = maximum poll interval */
1181 	if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1182 	    time > 120000000000ULL)
1183 		return -EINVAL;
1184 	poll_timeout = time;
1185 	hr_time = poll_timeout;
1186 
1187 	spin_lock_bh(&ap_poll_timer_lock);
1188 	hrtimer_cancel(&ap_poll_timer);
1189 	hrtimer_set_expires(&ap_poll_timer, hr_time);
1190 	hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1191 	spin_unlock_bh(&ap_poll_timer_lock);
1192 
1193 	return count;
1194 }
1195 
1196 static BUS_ATTR_RW(poll_timeout);
1197 
1198 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1199 {
1200 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1201 }
1202 
1203 static BUS_ATTR_RO(ap_max_domain_id);
1204 
1205 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1206 {
1207 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1208 }
1209 
1210 static BUS_ATTR_RO(ap_max_adapter_id);
1211 
1212 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1213 {
1214 	int rc;
1215 
1216 	if (mutex_lock_interruptible(&ap_perms_mutex))
1217 		return -ERESTARTSYS;
1218 	rc = scnprintf(buf, PAGE_SIZE,
1219 		       "0x%016lx%016lx%016lx%016lx\n",
1220 		       ap_perms.apm[0], ap_perms.apm[1],
1221 		       ap_perms.apm[2], ap_perms.apm[3]);
1222 	mutex_unlock(&ap_perms_mutex);
1223 
1224 	return rc;
1225 }
1226 
1227 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1228 			    size_t count)
1229 {
1230 	int rc;
1231 
1232 	rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1233 	if (rc)
1234 		return rc;
1235 
1236 	ap_bus_revise_bindings();
1237 
1238 	return count;
1239 }
1240 
1241 static BUS_ATTR_RW(apmask);
1242 
1243 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1244 {
1245 	int rc;
1246 
1247 	if (mutex_lock_interruptible(&ap_perms_mutex))
1248 		return -ERESTARTSYS;
1249 	rc = scnprintf(buf, PAGE_SIZE,
1250 		       "0x%016lx%016lx%016lx%016lx\n",
1251 		       ap_perms.aqm[0], ap_perms.aqm[1],
1252 		       ap_perms.aqm[2], ap_perms.aqm[3]);
1253 	mutex_unlock(&ap_perms_mutex);
1254 
1255 	return rc;
1256 }
1257 
1258 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1259 			    size_t count)
1260 {
1261 	int rc;
1262 
1263 	rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1264 	if (rc)
1265 		return rc;
1266 
1267 	ap_bus_revise_bindings();
1268 
1269 	return count;
1270 }
1271 
1272 static BUS_ATTR_RW(aqmask);
1273 
1274 static ssize_t scans_show(struct bus_type *bus, char *buf)
1275 {
1276 	return scnprintf(buf, PAGE_SIZE, "%llu\n",
1277 			 atomic64_read(&ap_scan_bus_count));
1278 }
1279 
1280 static BUS_ATTR_RO(scans);
1281 
1282 static ssize_t bindings_show(struct bus_type *bus, char *buf)
1283 {
1284 	int rc;
1285 	unsigned int apqns, n;
1286 
1287 	ap_calc_bound_apqns(&apqns, &n);
1288 	if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1289 		rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns);
1290 	else
1291 		rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns);
1292 
1293 	return rc;
1294 }
1295 
1296 static BUS_ATTR_RO(bindings);
1297 
1298 static struct attribute *ap_bus_attrs[] = {
1299 	&bus_attr_ap_domain.attr,
1300 	&bus_attr_ap_control_domain_mask.attr,
1301 	&bus_attr_ap_usage_domain_mask.attr,
1302 	&bus_attr_ap_adapter_mask.attr,
1303 	&bus_attr_config_time.attr,
1304 	&bus_attr_poll_thread.attr,
1305 	&bus_attr_ap_interrupts.attr,
1306 	&bus_attr_poll_timeout.attr,
1307 	&bus_attr_ap_max_domain_id.attr,
1308 	&bus_attr_ap_max_adapter_id.attr,
1309 	&bus_attr_apmask.attr,
1310 	&bus_attr_aqmask.attr,
1311 	&bus_attr_scans.attr,
1312 	&bus_attr_bindings.attr,
1313 	NULL,
1314 };
1315 ATTRIBUTE_GROUPS(ap_bus);
1316 
1317 static struct bus_type ap_bus_type = {
1318 	.name = "ap",
1319 	.bus_groups = ap_bus_groups,
1320 	.match = &ap_bus_match,
1321 	.uevent = &ap_uevent,
1322 };
1323 
1324 /**
1325  * ap_select_domain(): Select an AP domain if possible and we haven't
1326  * already done so before.
1327  */
1328 static void ap_select_domain(void)
1329 {
1330 	struct ap_queue_status status;
1331 	int card, dom;
1332 
1333 	/*
1334 	 * Choose the default domain. Either the one specified with
1335 	 * the "domain=" parameter or the first domain with at least
1336 	 * one valid APQN.
1337 	 */
1338 	spin_lock_bh(&ap_domain_lock);
1339 	if (ap_domain_index >= 0) {
1340 		/* Domain has already been selected. */
1341 		goto out;
1342 	}
1343 	for (dom = 0; dom <= ap_max_domain_id; dom++) {
1344 		if (!ap_test_config_usage_domain(dom) ||
1345 		    !test_bit_inv(dom, ap_perms.aqm))
1346 			continue;
1347 		for (card = 0; card <= ap_max_adapter_id; card++) {
1348 			if (!ap_test_config_card_id(card) ||
1349 			    !test_bit_inv(card, ap_perms.apm))
1350 				continue;
1351 			status = ap_test_queue(AP_MKQID(card, dom),
1352 					       ap_apft_available(),
1353 					       NULL);
1354 			if (status.response_code == AP_RESPONSE_NORMAL)
1355 				break;
1356 		}
1357 		if (card <= ap_max_adapter_id)
1358 			break;
1359 	}
1360 	if (dom <= ap_max_domain_id) {
1361 		ap_domain_index = dom;
1362 		AP_DBF_INFO("%s new default domain is %d\n",
1363 			    __func__, ap_domain_index);
1364 	}
1365 out:
1366 	spin_unlock_bh(&ap_domain_lock);
1367 }
1368 
1369 /*
1370  * This function checks the type and returns either 0 for not
1371  * supported or the highest compatible type value (which may
1372  * include the input type value).
1373  */
1374 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1375 {
1376 	int comp_type = 0;
1377 
1378 	/* < CEX2A is not supported */
1379 	if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1380 		AP_DBF_WARN("get_comp_type queue=%02x.%04x unsupported type %d\n",
1381 			    AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1382 		return 0;
1383 	}
1384 	/* up to CEX7 known and fully supported */
1385 	if (rawtype <= AP_DEVICE_TYPE_CEX7)
1386 		return rawtype;
1387 	/*
1388 	 * unknown new type > CEX7, check for compatibility
1389 	 * to the highest known and supported type which is
1390 	 * currently CEX7 with the help of the QACT function.
1391 	 */
1392 	if (ap_qact_available()) {
1393 		struct ap_queue_status status;
1394 		union ap_qact_ap_info apinfo = {0};
1395 
1396 		apinfo.mode = (func >> 26) & 0x07;
1397 		apinfo.cat = AP_DEVICE_TYPE_CEX7;
1398 		status = ap_qact(qid, 0, &apinfo);
1399 		if (status.response_code == AP_RESPONSE_NORMAL
1400 		    && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1401 		    && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
1402 			comp_type = apinfo.cat;
1403 	}
1404 	if (!comp_type)
1405 		AP_DBF_WARN("get_comp_type queue=%02x.%04x unable to map type %d\n",
1406 			    AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1407 	else if (comp_type != rawtype)
1408 		AP_DBF_INFO("get_comp_type queue=%02x.%04x map type %d to %d\n",
1409 			    AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1410 			    rawtype, comp_type);
1411 	return comp_type;
1412 }
1413 
1414 /*
1415  * Helper function to be used with bus_find_dev
1416  * matches for the card device with the given id
1417  */
1418 static int __match_card_device_with_id(struct device *dev, const void *data)
1419 {
1420 	return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1421 }
1422 
1423 /*
1424  * Helper function to be used with bus_find_dev
1425  * matches for the queue device with a given qid
1426  */
1427 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1428 {
1429 	return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1430 }
1431 
1432 /*
1433  * Helper function to be used with bus_find_dev
1434  * matches any queue device with given queue id
1435  */
1436 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1437 {
1438 	return is_queue_dev(dev)
1439 		&& AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1440 }
1441 
1442 /*
1443  * Helper function for ap_scan_bus().
1444  * Remove card device and associated queue devices.
1445  */
1446 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1447 {
1448 	bus_for_each_dev(&ap_bus_type, NULL,
1449 			 (void *)(long) ac->id,
1450 			 __ap_queue_devices_with_id_unregister);
1451 	device_unregister(&ac->ap_dev.device);
1452 }
1453 
1454 /*
1455  * Helper function for ap_scan_bus().
1456  * Does the scan bus job for all the domains within
1457  * a valid adapter given by an ap_card ptr.
1458  */
1459 static inline void ap_scan_domains(struct ap_card *ac)
1460 {
1461 	bool decfg;
1462 	ap_qid_t qid;
1463 	unsigned int func;
1464 	struct device *dev;
1465 	struct ap_queue *aq;
1466 	int rc, dom, depth, type;
1467 
1468 	/*
1469 	 * Go through the configuration for the domains and compare them
1470 	 * to the existing queue devices. Also take care of the config
1471 	 * and error state for the queue devices.
1472 	 */
1473 
1474 	for (dom = 0; dom <= ap_max_domain_id; dom++) {
1475 		qid = AP_MKQID(ac->id, dom);
1476 		dev = bus_find_device(&ap_bus_type, NULL,
1477 				      (void *)(long) qid,
1478 				      __match_queue_device_with_qid);
1479 		aq = dev ? to_ap_queue(dev) : NULL;
1480 		if (!ap_test_config_usage_domain(dom)) {
1481 			if (dev) {
1482 				AP_DBF_INFO("%s(%d,%d) not in config any more, rm queue device\n",
1483 					    __func__, ac->id, dom);
1484 				device_unregister(dev);
1485 				put_device(dev);
1486 			}
1487 			continue;
1488 		}
1489 		/* domain is valid, get info from this APQN */
1490 		if (!ap_queue_info(qid, &type, &func, &depth, &decfg)) {
1491 			if (aq) {
1492 				AP_DBF_INFO(
1493 					"%s(%d,%d) ap_queue_info() not successful, rm queue device\n",
1494 					__func__, ac->id, dom);
1495 				device_unregister(dev);
1496 				put_device(dev);
1497 			}
1498 			continue;
1499 		}
1500 		/* if no queue device exists, create a new one */
1501 		if (!aq) {
1502 			aq = ap_queue_create(qid, ac->ap_dev.device_type);
1503 			if (!aq) {
1504 				AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1505 					    __func__, ac->id, dom);
1506 				continue;
1507 			}
1508 			aq->card = ac;
1509 			aq->config = !decfg;
1510 			dev = &aq->ap_dev.device;
1511 			dev->bus = &ap_bus_type;
1512 			dev->parent = &ac->ap_dev.device;
1513 			dev_set_name(dev, "%02x.%04x", ac->id, dom);
1514 			/* register queue device */
1515 			rc = device_register(dev);
1516 			if (rc) {
1517 				AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1518 					    __func__, ac->id, dom);
1519 				goto put_dev_and_continue;
1520 			}
1521 			/* get it and thus adjust reference counter */
1522 			get_device(dev);
1523 			if (decfg)
1524 				AP_DBF_INFO("%s(%d,%d) new (decfg) queue device created\n",
1525 					    __func__, ac->id, dom);
1526 			else
1527 				AP_DBF_INFO("%s(%d,%d) new queue device created\n",
1528 					    __func__, ac->id, dom);
1529 			goto put_dev_and_continue;
1530 		}
1531 		/* Check config state on the already existing queue device */
1532 		spin_lock_bh(&aq->lock);
1533 		if (decfg && aq->config) {
1534 			/* config off this queue device */
1535 			aq->config = false;
1536 			if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1537 				aq->dev_state = AP_DEV_STATE_ERROR;
1538 				aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1539 			}
1540 			spin_unlock_bh(&aq->lock);
1541 			AP_DBF_INFO("%s(%d,%d) queue device config off\n",
1542 				    __func__, ac->id, dom);
1543 			/* 'receive' pending messages with -EAGAIN */
1544 			ap_flush_queue(aq);
1545 			goto put_dev_and_continue;
1546 		}
1547 		if (!decfg && !aq->config) {
1548 			/* config on this queue device */
1549 			aq->config = true;
1550 			if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1551 				aq->dev_state = AP_DEV_STATE_OPERATING;
1552 				aq->sm_state = AP_SM_STATE_RESET_START;
1553 			}
1554 			spin_unlock_bh(&aq->lock);
1555 			AP_DBF_INFO("%s(%d,%d) queue device config on\n",
1556 				    __func__, ac->id, dom);
1557 			goto put_dev_and_continue;
1558 		}
1559 		/* handle other error states */
1560 		if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1561 			spin_unlock_bh(&aq->lock);
1562 			/* 'receive' pending messages with -EAGAIN */
1563 			ap_flush_queue(aq);
1564 			/* re-init (with reset) the queue device */
1565 			ap_queue_init_state(aq);
1566 			AP_DBF_INFO("%s(%d,%d) queue device reinit enforced\n",
1567 				    __func__, ac->id, dom);
1568 			goto put_dev_and_continue;
1569 		}
1570 		spin_unlock_bh(&aq->lock);
1571 put_dev_and_continue:
1572 		put_device(dev);
1573 	}
1574 }
1575 
1576 /*
1577  * Helper function for ap_scan_bus().
1578  * Does the scan bus job for the given adapter id.
1579  */
1580 static inline void ap_scan_adapter(int ap)
1581 {
1582 	bool decfg;
1583 	ap_qid_t qid;
1584 	unsigned int func;
1585 	struct device *dev;
1586 	struct ap_card *ac;
1587 	int rc, dom, depth, type, comp_type;
1588 
1589 	/* Is there currently a card device for this adapter ? */
1590 	dev = bus_find_device(&ap_bus_type, NULL,
1591 			      (void *)(long) ap,
1592 			      __match_card_device_with_id);
1593 	ac = dev ? to_ap_card(dev) : NULL;
1594 
1595 	/* Adapter not in configuration ? */
1596 	if (!ap_test_config_card_id(ap)) {
1597 		if (ac) {
1598 			AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devices\n",
1599 				    __func__, ap);
1600 			ap_scan_rm_card_dev_and_queue_devs(ac);
1601 			put_device(dev);
1602 		}
1603 		return;
1604 	}
1605 
1606 	/*
1607 	 * Adapter ap is valid in the current configuration. So do some checks:
1608 	 * If no card device exists, build one. If a card device exists, check
1609 	 * for type and functions changed. For all this we need to find a valid
1610 	 * APQN first.
1611 	 */
1612 
1613 	for (dom = 0; dom <= ap_max_domain_id; dom++)
1614 		if (ap_test_config_usage_domain(dom)) {
1615 			qid = AP_MKQID(ap, dom);
1616 			if (ap_queue_info(qid, &type, &func, &depth, &decfg))
1617 				break;
1618 		}
1619 	if (dom > ap_max_domain_id) {
1620 		/* Could not find a valid APQN for this adapter */
1621 		if (ac) {
1622 			AP_DBF_INFO(
1623 				"%s(%d) no type info (no APQN found), rm card and queue devices\n",
1624 				__func__, ap);
1625 			ap_scan_rm_card_dev_and_queue_devs(ac);
1626 			put_device(dev);
1627 		} else {
1628 			AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1629 				   __func__, ap);
1630 		}
1631 		return;
1632 	}
1633 	if (!type) {
1634 		/* No apdater type info available, an unusable adapter */
1635 		if (ac) {
1636 			AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devices\n",
1637 				    __func__, ap);
1638 			ap_scan_rm_card_dev_and_queue_devs(ac);
1639 			put_device(dev);
1640 		} else {
1641 			AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1642 				   __func__, ap);
1643 		}
1644 		return;
1645 	}
1646 
1647 	if (ac) {
1648 		/* Check APQN against existing card device for changes */
1649 		if (ac->raw_hwtype != type) {
1650 			AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devices\n",
1651 				    __func__, ap, type);
1652 			ap_scan_rm_card_dev_and_queue_devs(ac);
1653 			put_device(dev);
1654 			ac = NULL;
1655 		} else if (ac->functions != func) {
1656 			AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devices\n",
1657 				    __func__, ap, type);
1658 			ap_scan_rm_card_dev_and_queue_devs(ac);
1659 			put_device(dev);
1660 			ac = NULL;
1661 		} else {
1662 			if (decfg && ac->config) {
1663 				ac->config = false;
1664 				AP_DBF_INFO("%s(%d) card device config off\n",
1665 					    __func__, ap);
1666 
1667 			}
1668 			if (!decfg && !ac->config) {
1669 				ac->config = true;
1670 				AP_DBF_INFO("%s(%d) card device config on\n",
1671 					    __func__, ap);
1672 			}
1673 		}
1674 	}
1675 
1676 	if (!ac) {
1677 		/* Build a new card device */
1678 		comp_type = ap_get_compatible_type(qid, type, func);
1679 		if (!comp_type) {
1680 			AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
1681 				    __func__, ap, type);
1682 			return;
1683 		}
1684 		ac = ap_card_create(ap, depth, type, comp_type, func);
1685 		if (!ac) {
1686 			AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
1687 				    __func__, ap);
1688 			return;
1689 		}
1690 		ac->config = !decfg;
1691 		dev = &ac->ap_dev.device;
1692 		dev->bus = &ap_bus_type;
1693 		dev->parent = ap_root_device;
1694 		dev_set_name(dev, "card%02x", ap);
1695 		/* Register the new card device with AP bus */
1696 		rc = device_register(dev);
1697 		if (rc) {
1698 			AP_DBF_WARN("%s(%d) device_register() failed\n",
1699 				    __func__, ap);
1700 			put_device(dev);
1701 			return;
1702 		}
1703 		/* get it and thus adjust reference counter */
1704 		get_device(dev);
1705 		if (decfg)
1706 			AP_DBF_INFO("%s(%d) new (decfg) card device type=%d func=0x%08x created\n",
1707 				    __func__, ap, type, func);
1708 		else
1709 			AP_DBF_INFO("%s(%d) new card device type=%d func=0x%08x created\n",
1710 				    __func__, ap, type, func);
1711 	}
1712 
1713 	/* Verify the domains and the queue devices for this card */
1714 	ap_scan_domains(ac);
1715 
1716 	/* release the card device */
1717 	put_device(&ac->ap_dev.device);
1718 }
1719 
1720 /**
1721  * ap_scan_bus(): Scan the AP bus for new devices
1722  * Runs periodically, workqueue timer (ap_config_time)
1723  */
1724 static void ap_scan_bus(struct work_struct *unused)
1725 {
1726 	int ap;
1727 
1728 	ap_fetch_qci_info(ap_qci_info);
1729 	ap_select_domain();
1730 
1731 	AP_DBF_DBG("%s running\n", __func__);
1732 
1733 	/* loop over all possible adapters */
1734 	for (ap = 0; ap <= ap_max_adapter_id; ap++)
1735 		ap_scan_adapter(ap);
1736 
1737 	/* check if there is at least one queue available with default domain */
1738 	if (ap_domain_index >= 0) {
1739 		struct device *dev =
1740 			bus_find_device(&ap_bus_type, NULL,
1741 					(void *)(long) ap_domain_index,
1742 					__match_queue_device_with_queue_id);
1743 		if (dev)
1744 			put_device(dev);
1745 		else
1746 			AP_DBF_INFO("no queue device with default domain %d available\n",
1747 				    ap_domain_index);
1748 	}
1749 
1750 	if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
1751 		AP_DBF(DBF_DEBUG, "%s init scan complete\n", __func__);
1752 		ap_send_init_scan_done_uevent();
1753 		ap_check_bindings_complete();
1754 	}
1755 
1756 	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1757 }
1758 
1759 static void ap_config_timeout(struct timer_list *unused)
1760 {
1761 	queue_work(system_long_wq, &ap_scan_work);
1762 }
1763 
1764 static int __init ap_debug_init(void)
1765 {
1766 	ap_dbf_info = debug_register("ap", 1, 1,
1767 				     DBF_MAX_SPRINTF_ARGS * sizeof(long));
1768 	debug_register_view(ap_dbf_info, &debug_sprintf_view);
1769 	debug_set_level(ap_dbf_info, DBF_ERR);
1770 
1771 	return 0;
1772 }
1773 
1774 static void __init ap_perms_init(void)
1775 {
1776 	/* all resources useable if no kernel parameter string given */
1777 	memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1778 	memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1779 	memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1780 
1781 	/* apm kernel parameter string */
1782 	if (apm_str) {
1783 		memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1784 		ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1785 				  &ap_perms_mutex);
1786 	}
1787 
1788 	/* aqm kernel parameter string */
1789 	if (aqm_str) {
1790 		memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1791 		ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1792 				  &ap_perms_mutex);
1793 	}
1794 }
1795 
1796 /**
1797  * ap_module_init(): The module initialization code.
1798  *
1799  * Initializes the module.
1800  */
1801 static int __init ap_module_init(void)
1802 {
1803 	int rc;
1804 
1805 	rc = ap_debug_init();
1806 	if (rc)
1807 		return rc;
1808 
1809 	if (!ap_instructions_available()) {
1810 		pr_warn("The hardware system does not support AP instructions\n");
1811 		return -ENODEV;
1812 	}
1813 
1814 	/* init ap_queue hashtable */
1815 	hash_init(ap_queues);
1816 
1817 	/* set up the AP permissions (ioctls, ap and aq masks) */
1818 	ap_perms_init();
1819 
1820 	/* Get AP configuration data if available */
1821 	ap_init_qci_info();
1822 
1823 	/* check default domain setting */
1824 	if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
1825 	    (ap_domain_index >= 0 &&
1826 	     !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1827 		pr_warn("%d is not a valid cryptographic domain\n",
1828 			ap_domain_index);
1829 		ap_domain_index = -1;
1830 	}
1831 
1832 	/* enable interrupts if available */
1833 	if (ap_interrupts_available()) {
1834 		rc = register_adapter_interrupt(&ap_airq);
1835 		ap_airq_flag = (rc == 0);
1836 	}
1837 
1838 	/* Create /sys/bus/ap. */
1839 	rc = bus_register(&ap_bus_type);
1840 	if (rc)
1841 		goto out;
1842 
1843 	/* Create /sys/devices/ap. */
1844 	ap_root_device = root_device_register("ap");
1845 	rc = PTR_ERR_OR_ZERO(ap_root_device);
1846 	if (rc)
1847 		goto out_bus;
1848 	ap_root_device->bus = &ap_bus_type;
1849 
1850 	/* Setup the AP bus rescan timer. */
1851 	timer_setup(&ap_config_timer, ap_config_timeout, 0);
1852 
1853 	/*
1854 	 * Setup the high resultion poll timer.
1855 	 * If we are running under z/VM adjust polling to z/VM polling rate.
1856 	 */
1857 	if (MACHINE_IS_VM)
1858 		poll_timeout = 1500000;
1859 	hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1860 	ap_poll_timer.function = ap_poll_timeout;
1861 
1862 	/* Start the low priority AP bus poll thread. */
1863 	if (ap_thread_flag) {
1864 		rc = ap_poll_thread_start();
1865 		if (rc)
1866 			goto out_work;
1867 	}
1868 
1869 	queue_work(system_long_wq, &ap_scan_work);
1870 
1871 	return 0;
1872 
1873 out_work:
1874 	hrtimer_cancel(&ap_poll_timer);
1875 	root_device_unregister(ap_root_device);
1876 out_bus:
1877 	bus_unregister(&ap_bus_type);
1878 out:
1879 	if (ap_using_interrupts())
1880 		unregister_adapter_interrupt(&ap_airq);
1881 	kfree(ap_qci_info);
1882 	return rc;
1883 }
1884 device_initcall(ap_module_init);
1885