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