xref: /openbmc/linux/drivers/s390/crypto/ap_bus.c (revision 40662333)
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 /**
842  * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
843  *			default host driver or not.
844  * @card: the APID of the adapter card to check
845  * @queue: the APQI of the queue to check
846  *
847  * Note: the ap_perms_mutex must be locked by the caller of this function.
848  *
849  * Return: an int specifying whether the AP adapter is reserved for the host (1)
850  *	   or not (0).
851  */
852 int ap_owned_by_def_drv(int card, int queue)
853 {
854 	int rc = 0;
855 
856 	if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
857 		return -EINVAL;
858 
859 	if (test_bit_inv(card, ap_perms.apm) &&
860 	    test_bit_inv(queue, ap_perms.aqm))
861 		rc = 1;
862 
863 	return rc;
864 }
865 EXPORT_SYMBOL(ap_owned_by_def_drv);
866 
867 /**
868  * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
869  *				       a set is reserved for the host drivers
870  *				       or not.
871  * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
872  * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
873  *
874  * Note: the ap_perms_mutex must be locked by the caller of this function.
875  *
876  * Return: an int specifying whether each APQN is reserved for the host (1) or
877  *	   not (0)
878  */
879 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
880 				       unsigned long *aqm)
881 {
882 	int card, queue, rc = 0;
883 
884 	for (card = 0; !rc && card < AP_DEVICES; card++)
885 		if (test_bit_inv(card, apm) &&
886 		    test_bit_inv(card, ap_perms.apm))
887 			for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
888 				if (test_bit_inv(queue, aqm) &&
889 				    test_bit_inv(queue, ap_perms.aqm))
890 					rc = 1;
891 
892 	return rc;
893 }
894 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
895 
896 static int ap_device_probe(struct device *dev)
897 {
898 	struct ap_device *ap_dev = to_ap_dev(dev);
899 	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
900 	int card, queue, devres, drvres, rc = -ENODEV;
901 
902 	if (!get_device(dev))
903 		return rc;
904 
905 	if (is_queue_dev(dev)) {
906 		/*
907 		 * If the apqn is marked as reserved/used by ap bus and
908 		 * default drivers, only probe with drivers with the default
909 		 * flag set. If it is not marked, only probe with drivers
910 		 * with the default flag not set.
911 		 */
912 		card = AP_QID_CARD(to_ap_queue(dev)->qid);
913 		queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
914 		mutex_lock(&ap_perms_mutex);
915 		devres = test_bit_inv(card, ap_perms.apm) &&
916 			test_bit_inv(queue, ap_perms.aqm);
917 		mutex_unlock(&ap_perms_mutex);
918 		drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
919 		if (!!devres != !!drvres)
920 			goto out;
921 	}
922 
923 	/* Add queue/card to list of active queues/cards */
924 	spin_lock_bh(&ap_queues_lock);
925 	if (is_queue_dev(dev))
926 		hash_add(ap_queues, &to_ap_queue(dev)->hnode,
927 			 to_ap_queue(dev)->qid);
928 	spin_unlock_bh(&ap_queues_lock);
929 
930 	rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
931 
932 	if (rc) {
933 		spin_lock_bh(&ap_queues_lock);
934 		if (is_queue_dev(dev))
935 			hash_del(&to_ap_queue(dev)->hnode);
936 		spin_unlock_bh(&ap_queues_lock);
937 	} else {
938 		ap_check_bindings_complete();
939 	}
940 
941 out:
942 	if (rc)
943 		put_device(dev);
944 	return rc;
945 }
946 
947 static void ap_device_remove(struct device *dev)
948 {
949 	struct ap_device *ap_dev = to_ap_dev(dev);
950 	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
951 
952 	/* prepare ap queue device removal */
953 	if (is_queue_dev(dev))
954 		ap_queue_prepare_remove(to_ap_queue(dev));
955 
956 	/* driver's chance to clean up gracefully */
957 	if (ap_drv->remove)
958 		ap_drv->remove(ap_dev);
959 
960 	/* now do the ap queue device remove */
961 	if (is_queue_dev(dev))
962 		ap_queue_remove(to_ap_queue(dev));
963 
964 	/* Remove queue/card from list of active queues/cards */
965 	spin_lock_bh(&ap_queues_lock);
966 	if (is_queue_dev(dev))
967 		hash_del(&to_ap_queue(dev)->hnode);
968 	spin_unlock_bh(&ap_queues_lock);
969 
970 	put_device(dev);
971 }
972 
973 struct ap_queue *ap_get_qdev(ap_qid_t qid)
974 {
975 	int bkt;
976 	struct ap_queue *aq;
977 
978 	spin_lock_bh(&ap_queues_lock);
979 	hash_for_each(ap_queues, bkt, aq, hnode) {
980 		if (aq->qid == qid) {
981 			get_device(&aq->ap_dev.device);
982 			spin_unlock_bh(&ap_queues_lock);
983 			return aq;
984 		}
985 	}
986 	spin_unlock_bh(&ap_queues_lock);
987 
988 	return NULL;
989 }
990 EXPORT_SYMBOL(ap_get_qdev);
991 
992 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
993 		       char *name)
994 {
995 	struct device_driver *drv = &ap_drv->driver;
996 
997 	drv->bus = &ap_bus_type;
998 	drv->owner = owner;
999 	drv->name = name;
1000 	return driver_register(drv);
1001 }
1002 EXPORT_SYMBOL(ap_driver_register);
1003 
1004 void ap_driver_unregister(struct ap_driver *ap_drv)
1005 {
1006 	driver_unregister(&ap_drv->driver);
1007 }
1008 EXPORT_SYMBOL(ap_driver_unregister);
1009 
1010 void ap_bus_force_rescan(void)
1011 {
1012 	/* processing a asynchronous bus rescan */
1013 	del_timer(&ap_config_timer);
1014 	queue_work(system_long_wq, &ap_scan_work);
1015 	flush_work(&ap_scan_work);
1016 }
1017 EXPORT_SYMBOL(ap_bus_force_rescan);
1018 
1019 /*
1020  * A config change has happened, force an ap bus rescan.
1021  */
1022 void ap_bus_cfg_chg(void)
1023 {
1024 	AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
1025 
1026 	ap_bus_force_rescan();
1027 }
1028 
1029 /*
1030  * hex2bitmap() - parse hex mask string and set bitmap.
1031  * Valid strings are "0x012345678" with at least one valid hex number.
1032  * Rest of the bitmap to the right is padded with 0. No spaces allowed
1033  * within the string, the leading 0x may be omitted.
1034  * Returns the bitmask with exactly the bits set as given by the hex
1035  * string (both in big endian order).
1036  */
1037 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1038 {
1039 	int i, n, b;
1040 
1041 	/* bits needs to be a multiple of 8 */
1042 	if (bits & 0x07)
1043 		return -EINVAL;
1044 
1045 	if (str[0] == '0' && str[1] == 'x')
1046 		str++;
1047 	if (*str == 'x')
1048 		str++;
1049 
1050 	for (i = 0; isxdigit(*str) && i < bits; str++) {
1051 		b = hex_to_bin(*str);
1052 		for (n = 0; n < 4; n++)
1053 			if (b & (0x08 >> n))
1054 				set_bit_inv(i + n, bitmap);
1055 		i += 4;
1056 	}
1057 
1058 	if (*str == '\n')
1059 		str++;
1060 	if (*str)
1061 		return -EINVAL;
1062 	return 0;
1063 }
1064 
1065 /*
1066  * modify_bitmap() - parse bitmask argument and modify an existing
1067  * bit mask accordingly. A concatenation (done with ',') of these
1068  * terms is recognized:
1069  *   +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1070  * <bitnr> may be any valid number (hex, decimal or octal) in the range
1071  * 0...bits-1; the leading + or - is required. Here are some examples:
1072  *   +0-15,+32,-128,-0xFF
1073  *   -0-255,+1-16,+0x128
1074  *   +1,+2,+3,+4,-5,-7-10
1075  * Returns the new bitmap after all changes have been applied. Every
1076  * positive value in the string will set a bit and every negative value
1077  * in the string will clear a bit. As a bit may be touched more than once,
1078  * the last 'operation' wins:
1079  * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1080  * cleared again. All other bits are unmodified.
1081  */
1082 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1083 {
1084 	int a, i, z;
1085 	char *np, sign;
1086 
1087 	/* bits needs to be a multiple of 8 */
1088 	if (bits & 0x07)
1089 		return -EINVAL;
1090 
1091 	while (*str) {
1092 		sign = *str++;
1093 		if (sign != '+' && sign != '-')
1094 			return -EINVAL;
1095 		a = z = simple_strtoul(str, &np, 0);
1096 		if (str == np || a >= bits)
1097 			return -EINVAL;
1098 		str = np;
1099 		if (*str == '-') {
1100 			z = simple_strtoul(++str, &np, 0);
1101 			if (str == np || a > z || z >= bits)
1102 				return -EINVAL;
1103 			str = np;
1104 		}
1105 		for (i = a; i <= z; i++)
1106 			if (sign == '+')
1107 				set_bit_inv(i, bitmap);
1108 			else
1109 				clear_bit_inv(i, bitmap);
1110 		while (*str == ',' || *str == '\n')
1111 			str++;
1112 	}
1113 
1114 	return 0;
1115 }
1116 
1117 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1118 			       unsigned long *newmap)
1119 {
1120 	unsigned long size;
1121 	int rc;
1122 
1123 	size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1124 	if (*str == '+' || *str == '-') {
1125 		memcpy(newmap, bitmap, size);
1126 		rc = modify_bitmap(str, newmap, bits);
1127 	} else {
1128 		memset(newmap, 0, size);
1129 		rc = hex2bitmap(str, newmap, bits);
1130 	}
1131 	return rc;
1132 }
1133 
1134 int ap_parse_mask_str(const char *str,
1135 		      unsigned long *bitmap, int bits,
1136 		      struct mutex *lock)
1137 {
1138 	unsigned long *newmap, size;
1139 	int rc;
1140 
1141 	/* bits needs to be a multiple of 8 */
1142 	if (bits & 0x07)
1143 		return -EINVAL;
1144 
1145 	size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1146 	newmap = kmalloc(size, GFP_KERNEL);
1147 	if (!newmap)
1148 		return -ENOMEM;
1149 	if (mutex_lock_interruptible(lock)) {
1150 		kfree(newmap);
1151 		return -ERESTARTSYS;
1152 	}
1153 	rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
1154 	if (rc == 0)
1155 		memcpy(bitmap, newmap, size);
1156 	mutex_unlock(lock);
1157 	kfree(newmap);
1158 	return rc;
1159 }
1160 EXPORT_SYMBOL(ap_parse_mask_str);
1161 
1162 /*
1163  * AP bus attributes.
1164  */
1165 
1166 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1167 {
1168 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1169 }
1170 
1171 static ssize_t ap_domain_store(struct bus_type *bus,
1172 			       const char *buf, size_t count)
1173 {
1174 	int domain;
1175 
1176 	if (sscanf(buf, "%i\n", &domain) != 1 ||
1177 	    domain < 0 || domain > ap_max_domain_id ||
1178 	    !test_bit_inv(domain, ap_perms.aqm))
1179 		return -EINVAL;
1180 
1181 	spin_lock_bh(&ap_domain_lock);
1182 	ap_domain_index = domain;
1183 	spin_unlock_bh(&ap_domain_lock);
1184 
1185 	AP_DBF_INFO("%s stored new default domain=%d\n",
1186 		    __func__, domain);
1187 
1188 	return count;
1189 }
1190 
1191 static BUS_ATTR_RW(ap_domain);
1192 
1193 static ssize_t ap_control_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->adm[0], ap_qci_info->adm[1],
1201 			 ap_qci_info->adm[2], ap_qci_info->adm[3],
1202 			 ap_qci_info->adm[4], ap_qci_info->adm[5],
1203 			 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1204 }
1205 
1206 static BUS_ATTR_RO(ap_control_domain_mask);
1207 
1208 static ssize_t ap_usage_domain_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->aqm[0], ap_qci_info->aqm[1],
1216 			 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1217 			 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1218 			 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1219 }
1220 
1221 static BUS_ATTR_RO(ap_usage_domain_mask);
1222 
1223 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1224 {
1225 	if (!ap_qci_info)	/* QCI not supported */
1226 		return scnprintf(buf, PAGE_SIZE, "not supported\n");
1227 
1228 	return scnprintf(buf, PAGE_SIZE,
1229 			 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1230 			 ap_qci_info->apm[0], ap_qci_info->apm[1],
1231 			 ap_qci_info->apm[2], ap_qci_info->apm[3],
1232 			 ap_qci_info->apm[4], ap_qci_info->apm[5],
1233 			 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1234 }
1235 
1236 static BUS_ATTR_RO(ap_adapter_mask);
1237 
1238 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1239 {
1240 	return scnprintf(buf, PAGE_SIZE, "%d\n",
1241 			 ap_irq_flag ? 1 : 0);
1242 }
1243 
1244 static BUS_ATTR_RO(ap_interrupts);
1245 
1246 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1247 {
1248 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1249 }
1250 
1251 static ssize_t config_time_store(struct bus_type *bus,
1252 				 const char *buf, size_t count)
1253 {
1254 	int time;
1255 
1256 	if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1257 		return -EINVAL;
1258 	ap_config_time = time;
1259 	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1260 	return count;
1261 }
1262 
1263 static BUS_ATTR_RW(config_time);
1264 
1265 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1266 {
1267 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1268 }
1269 
1270 static ssize_t poll_thread_store(struct bus_type *bus,
1271 				 const char *buf, size_t count)
1272 {
1273 	int flag, rc;
1274 
1275 	if (sscanf(buf, "%d\n", &flag) != 1)
1276 		return -EINVAL;
1277 	if (flag) {
1278 		rc = ap_poll_thread_start();
1279 		if (rc)
1280 			count = rc;
1281 	} else {
1282 		ap_poll_thread_stop();
1283 	}
1284 	return count;
1285 }
1286 
1287 static BUS_ATTR_RW(poll_thread);
1288 
1289 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1290 {
1291 	return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1292 }
1293 
1294 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1295 				  size_t count)
1296 {
1297 	unsigned long long time;
1298 	ktime_t hr_time;
1299 
1300 	/* 120 seconds = maximum poll interval */
1301 	if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1302 	    time > 120000000000ULL)
1303 		return -EINVAL;
1304 	poll_timeout = time;
1305 	hr_time = poll_timeout;
1306 
1307 	spin_lock_bh(&ap_poll_timer_lock);
1308 	hrtimer_cancel(&ap_poll_timer);
1309 	hrtimer_set_expires(&ap_poll_timer, hr_time);
1310 	hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1311 	spin_unlock_bh(&ap_poll_timer_lock);
1312 
1313 	return count;
1314 }
1315 
1316 static BUS_ATTR_RW(poll_timeout);
1317 
1318 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1319 {
1320 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1321 }
1322 
1323 static BUS_ATTR_RO(ap_max_domain_id);
1324 
1325 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1326 {
1327 	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1328 }
1329 
1330 static BUS_ATTR_RO(ap_max_adapter_id);
1331 
1332 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1333 {
1334 	int rc;
1335 
1336 	if (mutex_lock_interruptible(&ap_perms_mutex))
1337 		return -ERESTARTSYS;
1338 	rc = scnprintf(buf, PAGE_SIZE,
1339 		       "0x%016lx%016lx%016lx%016lx\n",
1340 		       ap_perms.apm[0], ap_perms.apm[1],
1341 		       ap_perms.apm[2], ap_perms.apm[3]);
1342 	mutex_unlock(&ap_perms_mutex);
1343 
1344 	return rc;
1345 }
1346 
1347 static int __verify_card_reservations(struct device_driver *drv, void *data)
1348 {
1349 	int rc = 0;
1350 	struct ap_driver *ap_drv = to_ap_drv(drv);
1351 	unsigned long *newapm = (unsigned long *)data;
1352 
1353 	/*
1354 	 * increase the driver's module refcounter to be sure it is not
1355 	 * going away when we invoke the callback function.
1356 	 */
1357 	if (!try_module_get(drv->owner))
1358 		return 0;
1359 
1360 	if (ap_drv->in_use) {
1361 		rc = ap_drv->in_use(newapm, ap_perms.aqm);
1362 		if (rc)
1363 			rc = -EBUSY;
1364 	}
1365 
1366 	/* release the driver's module */
1367 	module_put(drv->owner);
1368 
1369 	return rc;
1370 }
1371 
1372 static int apmask_commit(unsigned long *newapm)
1373 {
1374 	int rc;
1375 	unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1376 
1377 	/*
1378 	 * Check if any bits in the apmask have been set which will
1379 	 * result in queues being removed from non-default drivers
1380 	 */
1381 	if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1382 		rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1383 				      __verify_card_reservations);
1384 		if (rc)
1385 			return rc;
1386 	}
1387 
1388 	memcpy(ap_perms.apm, newapm, APMASKSIZE);
1389 
1390 	return 0;
1391 }
1392 
1393 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1394 			    size_t count)
1395 {
1396 	int rc, changes = 0;
1397 	DECLARE_BITMAP(newapm, AP_DEVICES);
1398 
1399 	if (mutex_lock_interruptible(&ap_perms_mutex))
1400 		return -ERESTARTSYS;
1401 
1402 	rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1403 	if (rc)
1404 		goto done;
1405 
1406 	changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
1407 	if (changes)
1408 		rc = apmask_commit(newapm);
1409 
1410 done:
1411 	mutex_unlock(&ap_perms_mutex);
1412 	if (rc)
1413 		return rc;
1414 
1415 	if (changes) {
1416 		ap_bus_revise_bindings();
1417 		ap_send_mask_changed_uevent(newapm, NULL);
1418 	}
1419 
1420 	return count;
1421 }
1422 
1423 static BUS_ATTR_RW(apmask);
1424 
1425 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1426 {
1427 	int rc;
1428 
1429 	if (mutex_lock_interruptible(&ap_perms_mutex))
1430 		return -ERESTARTSYS;
1431 	rc = scnprintf(buf, PAGE_SIZE,
1432 		       "0x%016lx%016lx%016lx%016lx\n",
1433 		       ap_perms.aqm[0], ap_perms.aqm[1],
1434 		       ap_perms.aqm[2], ap_perms.aqm[3]);
1435 	mutex_unlock(&ap_perms_mutex);
1436 
1437 	return rc;
1438 }
1439 
1440 static int __verify_queue_reservations(struct device_driver *drv, void *data)
1441 {
1442 	int rc = 0;
1443 	struct ap_driver *ap_drv = to_ap_drv(drv);
1444 	unsigned long *newaqm = (unsigned long *)data;
1445 
1446 	/*
1447 	 * increase the driver's module refcounter to be sure it is not
1448 	 * going away when we invoke the callback function.
1449 	 */
1450 	if (!try_module_get(drv->owner))
1451 		return 0;
1452 
1453 	if (ap_drv->in_use) {
1454 		rc = ap_drv->in_use(ap_perms.apm, newaqm);
1455 		if (rc)
1456 			rc = -EBUSY;
1457 	}
1458 
1459 	/* release the driver's module */
1460 	module_put(drv->owner);
1461 
1462 	return rc;
1463 }
1464 
1465 static int aqmask_commit(unsigned long *newaqm)
1466 {
1467 	int rc;
1468 	unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1469 
1470 	/*
1471 	 * Check if any bits in the aqmask have been set which will
1472 	 * result in queues being removed from non-default drivers
1473 	 */
1474 	if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1475 		rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1476 				      __verify_queue_reservations);
1477 		if (rc)
1478 			return rc;
1479 	}
1480 
1481 	memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1482 
1483 	return 0;
1484 }
1485 
1486 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1487 			    size_t count)
1488 {
1489 	int rc, changes = 0;
1490 	DECLARE_BITMAP(newaqm, AP_DOMAINS);
1491 
1492 	if (mutex_lock_interruptible(&ap_perms_mutex))
1493 		return -ERESTARTSYS;
1494 
1495 	rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1496 	if (rc)
1497 		goto done;
1498 
1499 	changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
1500 	if (changes)
1501 		rc = aqmask_commit(newaqm);
1502 
1503 done:
1504 	mutex_unlock(&ap_perms_mutex);
1505 	if (rc)
1506 		return rc;
1507 
1508 	if (changes) {
1509 		ap_bus_revise_bindings();
1510 		ap_send_mask_changed_uevent(NULL, newaqm);
1511 	}
1512 
1513 	return count;
1514 }
1515 
1516 static BUS_ATTR_RW(aqmask);
1517 
1518 static ssize_t scans_show(struct bus_type *bus, char *buf)
1519 {
1520 	return scnprintf(buf, PAGE_SIZE, "%llu\n",
1521 			 atomic64_read(&ap_scan_bus_count));
1522 }
1523 
1524 static ssize_t scans_store(struct bus_type *bus, const char *buf,
1525 			   size_t count)
1526 {
1527 	AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1528 
1529 	ap_bus_force_rescan();
1530 
1531 	return count;
1532 }
1533 
1534 static BUS_ATTR_RW(scans);
1535 
1536 static ssize_t bindings_show(struct bus_type *bus, char *buf)
1537 {
1538 	int rc;
1539 	unsigned int apqns, n;
1540 
1541 	ap_calc_bound_apqns(&apqns, &n);
1542 	if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1543 		rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns);
1544 	else
1545 		rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns);
1546 
1547 	return rc;
1548 }
1549 
1550 static BUS_ATTR_RO(bindings);
1551 
1552 static struct attribute *ap_bus_attrs[] = {
1553 	&bus_attr_ap_domain.attr,
1554 	&bus_attr_ap_control_domain_mask.attr,
1555 	&bus_attr_ap_usage_domain_mask.attr,
1556 	&bus_attr_ap_adapter_mask.attr,
1557 	&bus_attr_config_time.attr,
1558 	&bus_attr_poll_thread.attr,
1559 	&bus_attr_ap_interrupts.attr,
1560 	&bus_attr_poll_timeout.attr,
1561 	&bus_attr_ap_max_domain_id.attr,
1562 	&bus_attr_ap_max_adapter_id.attr,
1563 	&bus_attr_apmask.attr,
1564 	&bus_attr_aqmask.attr,
1565 	&bus_attr_scans.attr,
1566 	&bus_attr_bindings.attr,
1567 	NULL,
1568 };
1569 ATTRIBUTE_GROUPS(ap_bus);
1570 
1571 static struct bus_type ap_bus_type = {
1572 	.name = "ap",
1573 	.bus_groups = ap_bus_groups,
1574 	.match = &ap_bus_match,
1575 	.uevent = &ap_uevent,
1576 	.probe = ap_device_probe,
1577 	.remove = ap_device_remove,
1578 };
1579 
1580 /**
1581  * ap_select_domain(): Select an AP domain if possible and we haven't
1582  * already done so before.
1583  */
1584 static void ap_select_domain(void)
1585 {
1586 	struct ap_queue_status status;
1587 	int card, dom;
1588 
1589 	/*
1590 	 * Choose the default domain. Either the one specified with
1591 	 * the "domain=" parameter or the first domain with at least
1592 	 * one valid APQN.
1593 	 */
1594 	spin_lock_bh(&ap_domain_lock);
1595 	if (ap_domain_index >= 0) {
1596 		/* Domain has already been selected. */
1597 		goto out;
1598 	}
1599 	for (dom = 0; dom <= ap_max_domain_id; dom++) {
1600 		if (!ap_test_config_usage_domain(dom) ||
1601 		    !test_bit_inv(dom, ap_perms.aqm))
1602 			continue;
1603 		for (card = 0; card <= ap_max_adapter_id; card++) {
1604 			if (!ap_test_config_card_id(card) ||
1605 			    !test_bit_inv(card, ap_perms.apm))
1606 				continue;
1607 			status = ap_test_queue(AP_MKQID(card, dom),
1608 					       ap_apft_available(),
1609 					       NULL);
1610 			if (status.response_code == AP_RESPONSE_NORMAL)
1611 				break;
1612 		}
1613 		if (card <= ap_max_adapter_id)
1614 			break;
1615 	}
1616 	if (dom <= ap_max_domain_id) {
1617 		ap_domain_index = dom;
1618 		AP_DBF_INFO("%s new default domain is %d\n",
1619 			    __func__, ap_domain_index);
1620 	}
1621 out:
1622 	spin_unlock_bh(&ap_domain_lock);
1623 }
1624 
1625 /*
1626  * This function checks the type and returns either 0 for not
1627  * supported or the highest compatible type value (which may
1628  * include the input type value).
1629  */
1630 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1631 {
1632 	int comp_type = 0;
1633 
1634 	/* < CEX2A is not supported */
1635 	if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1636 		AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1637 			    __func__, AP_QID_CARD(qid),
1638 			    AP_QID_QUEUE(qid), rawtype);
1639 		return 0;
1640 	}
1641 	/* up to CEX8 known and fully supported */
1642 	if (rawtype <= AP_DEVICE_TYPE_CEX8)
1643 		return rawtype;
1644 	/*
1645 	 * unknown new type > CEX8, check for compatibility
1646 	 * to the highest known and supported type which is
1647 	 * currently CEX8 with the help of the QACT function.
1648 	 */
1649 	if (ap_qact_available()) {
1650 		struct ap_queue_status status;
1651 		union ap_qact_ap_info apinfo = {0};
1652 
1653 		apinfo.mode = (func >> 26) & 0x07;
1654 		apinfo.cat = AP_DEVICE_TYPE_CEX8;
1655 		status = ap_qact(qid, 0, &apinfo);
1656 		if (status.response_code == AP_RESPONSE_NORMAL &&
1657 		    apinfo.cat >= AP_DEVICE_TYPE_CEX2A &&
1658 		    apinfo.cat <= AP_DEVICE_TYPE_CEX8)
1659 			comp_type = apinfo.cat;
1660 	}
1661 	if (!comp_type)
1662 		AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1663 			    __func__, AP_QID_CARD(qid),
1664 			    AP_QID_QUEUE(qid), rawtype);
1665 	else if (comp_type != rawtype)
1666 		AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1667 			    __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1668 			    rawtype, comp_type);
1669 	return comp_type;
1670 }
1671 
1672 /*
1673  * Helper function to be used with bus_find_dev
1674  * matches for the card device with the given id
1675  */
1676 static int __match_card_device_with_id(struct device *dev, const void *data)
1677 {
1678 	return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
1679 }
1680 
1681 /*
1682  * Helper function to be used with bus_find_dev
1683  * matches for the queue device with a given qid
1684  */
1685 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1686 {
1687 	return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
1688 }
1689 
1690 /*
1691  * Helper function to be used with bus_find_dev
1692  * matches any queue device with given queue id
1693  */
1694 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1695 {
1696 	return is_queue_dev(dev) &&
1697 		AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
1698 }
1699 
1700 /* Helper function for notify_config_changed */
1701 static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1702 {
1703 	struct ap_driver *ap_drv = to_ap_drv(drv);
1704 
1705 	if (try_module_get(drv->owner)) {
1706 		if (ap_drv->on_config_changed)
1707 			ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1708 		module_put(drv->owner);
1709 	}
1710 
1711 	return 0;
1712 }
1713 
1714 /* Notify all drivers about an qci config change */
1715 static inline void notify_config_changed(void)
1716 {
1717 	bus_for_each_drv(&ap_bus_type, NULL, NULL,
1718 			 __drv_notify_config_changed);
1719 }
1720 
1721 /* Helper function for notify_scan_complete */
1722 static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1723 {
1724 	struct ap_driver *ap_drv = to_ap_drv(drv);
1725 
1726 	if (try_module_get(drv->owner)) {
1727 		if (ap_drv->on_scan_complete)
1728 			ap_drv->on_scan_complete(ap_qci_info,
1729 						 ap_qci_info_old);
1730 		module_put(drv->owner);
1731 	}
1732 
1733 	return 0;
1734 }
1735 
1736 /* Notify all drivers about bus scan complete */
1737 static inline void notify_scan_complete(void)
1738 {
1739 	bus_for_each_drv(&ap_bus_type, NULL, NULL,
1740 			 __drv_notify_scan_complete);
1741 }
1742 
1743 /*
1744  * Helper function for ap_scan_bus().
1745  * Remove card device and associated queue devices.
1746  */
1747 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1748 {
1749 	bus_for_each_dev(&ap_bus_type, NULL,
1750 			 (void *)(long)ac->id,
1751 			 __ap_queue_devices_with_id_unregister);
1752 	device_unregister(&ac->ap_dev.device);
1753 }
1754 
1755 /*
1756  * Helper function for ap_scan_bus().
1757  * Does the scan bus job for all the domains within
1758  * a valid adapter given by an ap_card ptr.
1759  */
1760 static inline void ap_scan_domains(struct ap_card *ac)
1761 {
1762 	bool decfg, chkstop;
1763 	ap_qid_t qid;
1764 	unsigned int func;
1765 	struct device *dev;
1766 	struct ap_queue *aq;
1767 	int rc, dom, depth, type, ml;
1768 
1769 	/*
1770 	 * Go through the configuration for the domains and compare them
1771 	 * to the existing queue devices. Also take care of the config
1772 	 * and error state for the queue devices.
1773 	 */
1774 
1775 	for (dom = 0; dom <= ap_max_domain_id; dom++) {
1776 		qid = AP_MKQID(ac->id, dom);
1777 		dev = bus_find_device(&ap_bus_type, NULL,
1778 				      (void *)(long)qid,
1779 				      __match_queue_device_with_qid);
1780 		aq = dev ? to_ap_queue(dev) : NULL;
1781 		if (!ap_test_config_usage_domain(dom)) {
1782 			if (dev) {
1783 				AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
1784 					    __func__, ac->id, dom);
1785 				device_unregister(dev);
1786 				put_device(dev);
1787 			}
1788 			continue;
1789 		}
1790 		/* domain is valid, get info from this APQN */
1791 		if (!ap_queue_info(qid, &type, &func, &depth,
1792 				   &ml, &decfg, &chkstop)) {
1793 			if (aq) {
1794 				AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1795 					    __func__, ac->id, dom);
1796 				device_unregister(dev);
1797 				put_device(dev);
1798 			}
1799 			continue;
1800 		}
1801 		/* if no queue device exists, create a new one */
1802 		if (!aq) {
1803 			aq = ap_queue_create(qid, ac->ap_dev.device_type);
1804 			if (!aq) {
1805 				AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1806 					    __func__, ac->id, dom);
1807 				continue;
1808 			}
1809 			aq->card = ac;
1810 			aq->config = !decfg;
1811 			aq->chkstop = chkstop;
1812 			dev = &aq->ap_dev.device;
1813 			dev->bus = &ap_bus_type;
1814 			dev->parent = &ac->ap_dev.device;
1815 			dev_set_name(dev, "%02x.%04x", ac->id, dom);
1816 			/* register queue device */
1817 			rc = device_register(dev);
1818 			if (rc) {
1819 				AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1820 					    __func__, ac->id, dom);
1821 				goto put_dev_and_continue;
1822 			}
1823 			/* get it and thus adjust reference counter */
1824 			get_device(dev);
1825 			if (decfg)
1826 				AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
1827 					    __func__, ac->id, dom);
1828 			else if (chkstop)
1829 				AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1830 					    __func__, ac->id, dom);
1831 			else
1832 				AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
1833 					    __func__, ac->id, dom);
1834 			goto put_dev_and_continue;
1835 		}
1836 		/* handle state changes on already existing queue device */
1837 		spin_lock_bh(&aq->lock);
1838 		/* checkstop state */
1839 		if (chkstop && !aq->chkstop) {
1840 			/* checkstop on */
1841 			aq->chkstop = true;
1842 			if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1843 				aq->dev_state = AP_DEV_STATE_ERROR;
1844 				aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1845 			}
1846 			spin_unlock_bh(&aq->lock);
1847 			AP_DBF_DBG("%s(%d,%d) queue dev checkstop on\n",
1848 				   __func__, ac->id, dom);
1849 			/* 'receive' pending messages with -EAGAIN */
1850 			ap_flush_queue(aq);
1851 			goto put_dev_and_continue;
1852 		} else if (!chkstop && aq->chkstop) {
1853 			/* checkstop off */
1854 			aq->chkstop = false;
1855 			if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1856 				aq->dev_state = AP_DEV_STATE_OPERATING;
1857 				aq->sm_state = AP_SM_STATE_RESET_START;
1858 			}
1859 			spin_unlock_bh(&aq->lock);
1860 			AP_DBF_DBG("%s(%d,%d) queue dev checkstop off\n",
1861 				   __func__, ac->id, dom);
1862 			goto put_dev_and_continue;
1863 		}
1864 		/* config state change */
1865 		if (decfg && aq->config) {
1866 			/* config off this queue device */
1867 			aq->config = false;
1868 			if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1869 				aq->dev_state = AP_DEV_STATE_ERROR;
1870 				aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1871 			}
1872 			spin_unlock_bh(&aq->lock);
1873 			AP_DBF_DBG("%s(%d,%d) queue dev config off\n",
1874 				   __func__, ac->id, dom);
1875 			ap_send_config_uevent(&aq->ap_dev, aq->config);
1876 			/* 'receive' pending messages with -EAGAIN */
1877 			ap_flush_queue(aq);
1878 			goto put_dev_and_continue;
1879 		} else if (!decfg && !aq->config) {
1880 			/* config on this queue device */
1881 			aq->config = true;
1882 			if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1883 				aq->dev_state = AP_DEV_STATE_OPERATING;
1884 				aq->sm_state = AP_SM_STATE_RESET_START;
1885 			}
1886 			spin_unlock_bh(&aq->lock);
1887 			AP_DBF_DBG("%s(%d,%d) queue dev config on\n",
1888 				   __func__, ac->id, dom);
1889 			ap_send_config_uevent(&aq->ap_dev, aq->config);
1890 			goto put_dev_and_continue;
1891 		}
1892 		/* handle other error states */
1893 		if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1894 			spin_unlock_bh(&aq->lock);
1895 			/* 'receive' pending messages with -EAGAIN */
1896 			ap_flush_queue(aq);
1897 			/* re-init (with reset) the queue device */
1898 			ap_queue_init_state(aq);
1899 			AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
1900 				    __func__, ac->id, dom);
1901 			goto put_dev_and_continue;
1902 		}
1903 		spin_unlock_bh(&aq->lock);
1904 put_dev_and_continue:
1905 		put_device(dev);
1906 	}
1907 }
1908 
1909 /*
1910  * Helper function for ap_scan_bus().
1911  * Does the scan bus job for the given adapter id.
1912  */
1913 static inline void ap_scan_adapter(int ap)
1914 {
1915 	bool decfg, chkstop;
1916 	ap_qid_t qid;
1917 	unsigned int func;
1918 	struct device *dev;
1919 	struct ap_card *ac;
1920 	int rc, dom, depth, type, comp_type, ml;
1921 
1922 	/* Is there currently a card device for this adapter ? */
1923 	dev = bus_find_device(&ap_bus_type, NULL,
1924 			      (void *)(long)ap,
1925 			      __match_card_device_with_id);
1926 	ac = dev ? to_ap_card(dev) : NULL;
1927 
1928 	/* Adapter not in configuration ? */
1929 	if (!ap_test_config_card_id(ap)) {
1930 		if (ac) {
1931 			AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
1932 				    __func__, ap);
1933 			ap_scan_rm_card_dev_and_queue_devs(ac);
1934 			put_device(dev);
1935 		}
1936 		return;
1937 	}
1938 
1939 	/*
1940 	 * Adapter ap is valid in the current configuration. So do some checks:
1941 	 * If no card device exists, build one. If a card device exists, check
1942 	 * for type and functions changed. For all this we need to find a valid
1943 	 * APQN first.
1944 	 */
1945 
1946 	for (dom = 0; dom <= ap_max_domain_id; dom++)
1947 		if (ap_test_config_usage_domain(dom)) {
1948 			qid = AP_MKQID(ap, dom);
1949 			if (ap_queue_info(qid, &type, &func, &depth,
1950 					  &ml, &decfg, &chkstop))
1951 				break;
1952 		}
1953 	if (dom > ap_max_domain_id) {
1954 		/* Could not find a valid APQN for this adapter */
1955 		if (ac) {
1956 			AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
1957 				    __func__, ap);
1958 			ap_scan_rm_card_dev_and_queue_devs(ac);
1959 			put_device(dev);
1960 		} else {
1961 			AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1962 				   __func__, ap);
1963 		}
1964 		return;
1965 	}
1966 	if (!type) {
1967 		/* No apdater type info available, an unusable adapter */
1968 		if (ac) {
1969 			AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
1970 				    __func__, ap);
1971 			ap_scan_rm_card_dev_and_queue_devs(ac);
1972 			put_device(dev);
1973 		} else {
1974 			AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1975 				   __func__, ap);
1976 		}
1977 		return;
1978 	}
1979 
1980 	if (ac) {
1981 		/* Check APQN against existing card device for changes */
1982 		if (ac->raw_hwtype != type) {
1983 			AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
1984 				    __func__, ap, type);
1985 			ap_scan_rm_card_dev_and_queue_devs(ac);
1986 			put_device(dev);
1987 			ac = NULL;
1988 		} else if (ac->functions != func) {
1989 			AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
1990 				    __func__, ap, type);
1991 			ap_scan_rm_card_dev_and_queue_devs(ac);
1992 			put_device(dev);
1993 			ac = NULL;
1994 		} else {
1995 			/* handle checkstop state change */
1996 			if (chkstop && !ac->chkstop) {
1997 				/* checkstop on */
1998 				ac->chkstop = true;
1999 				AP_DBF_INFO("%s(%d) card dev checkstop on\n",
2000 					    __func__, ap);
2001 			} else if (!chkstop && ac->chkstop) {
2002 				/* checkstop off */
2003 				ac->chkstop = false;
2004 				AP_DBF_INFO("%s(%d) card dev checkstop off\n",
2005 					    __func__, ap);
2006 			}
2007 			/* handle config state change */
2008 			if (decfg && ac->config) {
2009 				ac->config = false;
2010 				AP_DBF_INFO("%s(%d) card dev config off\n",
2011 					    __func__, ap);
2012 				ap_send_config_uevent(&ac->ap_dev, ac->config);
2013 			} else if (!decfg && !ac->config) {
2014 				ac->config = true;
2015 				AP_DBF_INFO("%s(%d) card dev config on\n",
2016 					    __func__, ap);
2017 				ap_send_config_uevent(&ac->ap_dev, ac->config);
2018 			}
2019 		}
2020 	}
2021 
2022 	if (!ac) {
2023 		/* Build a new card device */
2024 		comp_type = ap_get_compatible_type(qid, type, func);
2025 		if (!comp_type) {
2026 			AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
2027 				    __func__, ap, type);
2028 			return;
2029 		}
2030 		ac = ap_card_create(ap, depth, type, comp_type, func, ml);
2031 		if (!ac) {
2032 			AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
2033 				    __func__, ap);
2034 			return;
2035 		}
2036 		ac->config = !decfg;
2037 		ac->chkstop = chkstop;
2038 		dev = &ac->ap_dev.device;
2039 		dev->bus = &ap_bus_type;
2040 		dev->parent = ap_root_device;
2041 		dev_set_name(dev, "card%02x", ap);
2042 		/* maybe enlarge ap_max_msg_size to support this card */
2043 		if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
2044 			atomic_set(&ap_max_msg_size, ac->maxmsgsize);
2045 			AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
2046 				    __func__, ap,
2047 				    atomic_read(&ap_max_msg_size));
2048 		}
2049 		/* Register the new card device with AP bus */
2050 		rc = device_register(dev);
2051 		if (rc) {
2052 			AP_DBF_WARN("%s(%d) device_register() failed\n",
2053 				    __func__, ap);
2054 			put_device(dev);
2055 			return;
2056 		}
2057 		/* get it and thus adjust reference counter */
2058 		get_device(dev);
2059 		if (decfg)
2060 			AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
2061 				    __func__, ap, type, func);
2062 		else if (chkstop)
2063 			AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
2064 				    __func__, ap, type, func);
2065 		else
2066 			AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
2067 				    __func__, ap, type, func);
2068 	}
2069 
2070 	/* Verify the domains and the queue devices for this card */
2071 	ap_scan_domains(ac);
2072 
2073 	/* release the card device */
2074 	put_device(&ac->ap_dev.device);
2075 }
2076 
2077 /**
2078  * ap_get_configuration - get the host AP configuration
2079  *
2080  * Stores the host AP configuration information returned from the previous call
2081  * to Query Configuration Information (QCI), then retrieves and stores the
2082  * current AP configuration returned from QCI.
2083  *
2084  * Return: true if the host AP configuration changed between calls to QCI;
2085  * otherwise, return false.
2086  */
2087 static bool ap_get_configuration(void)
2088 {
2089 	if (!ap_qci_info)	/* QCI not supported */
2090 		return false;
2091 
2092 	memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2093 	ap_fetch_qci_info(ap_qci_info);
2094 
2095 	return memcmp(ap_qci_info, ap_qci_info_old,
2096 		      sizeof(struct ap_config_info)) != 0;
2097 }
2098 
2099 /**
2100  * ap_scan_bus(): Scan the AP bus for new devices
2101  * Runs periodically, workqueue timer (ap_config_time)
2102  * @unused: Unused pointer.
2103  */
2104 static void ap_scan_bus(struct work_struct *unused)
2105 {
2106 	int ap, config_changed = 0;
2107 
2108 	/* config change notify */
2109 	config_changed = ap_get_configuration();
2110 	if (config_changed)
2111 		notify_config_changed();
2112 	ap_select_domain();
2113 
2114 	AP_DBF_DBG("%s running\n", __func__);
2115 
2116 	/* loop over all possible adapters */
2117 	for (ap = 0; ap <= ap_max_adapter_id; ap++)
2118 		ap_scan_adapter(ap);
2119 
2120 	/* scan complete notify */
2121 	if (config_changed)
2122 		notify_scan_complete();
2123 
2124 	/* check if there is at least one queue available with default domain */
2125 	if (ap_domain_index >= 0) {
2126 		struct device *dev =
2127 			bus_find_device(&ap_bus_type, NULL,
2128 					(void *)(long)ap_domain_index,
2129 					__match_queue_device_with_queue_id);
2130 		if (dev)
2131 			put_device(dev);
2132 		else
2133 			AP_DBF_INFO("%s no queue device with default domain %d available\n",
2134 				    __func__, ap_domain_index);
2135 	}
2136 
2137 	if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
2138 		AP_DBF_DBG("%s init scan complete\n", __func__);
2139 		ap_send_init_scan_done_uevent();
2140 		ap_check_bindings_complete();
2141 	}
2142 
2143 	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
2144 }
2145 
2146 static void ap_config_timeout(struct timer_list *unused)
2147 {
2148 	queue_work(system_long_wq, &ap_scan_work);
2149 }
2150 
2151 static int __init ap_debug_init(void)
2152 {
2153 	ap_dbf_info = debug_register("ap", 2, 1,
2154 				     DBF_MAX_SPRINTF_ARGS * sizeof(long));
2155 	debug_register_view(ap_dbf_info, &debug_sprintf_view);
2156 	debug_set_level(ap_dbf_info, DBF_ERR);
2157 
2158 	return 0;
2159 }
2160 
2161 static void __init ap_perms_init(void)
2162 {
2163 	/* all resources usable if no kernel parameter string given */
2164 	memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
2165 	memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2166 	memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2167 
2168 	/* apm kernel parameter string */
2169 	if (apm_str) {
2170 		memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
2171 		ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2172 				  &ap_perms_mutex);
2173 	}
2174 
2175 	/* aqm kernel parameter string */
2176 	if (aqm_str) {
2177 		memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
2178 		ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2179 				  &ap_perms_mutex);
2180 	}
2181 }
2182 
2183 /**
2184  * ap_module_init(): The module initialization code.
2185  *
2186  * Initializes the module.
2187  */
2188 static int __init ap_module_init(void)
2189 {
2190 	int rc;
2191 
2192 	rc = ap_debug_init();
2193 	if (rc)
2194 		return rc;
2195 
2196 	if (!ap_instructions_available()) {
2197 		pr_warn("The hardware system does not support AP instructions\n");
2198 		return -ENODEV;
2199 	}
2200 
2201 	/* init ap_queue hashtable */
2202 	hash_init(ap_queues);
2203 
2204 	/* set up the AP permissions (ioctls, ap and aq masks) */
2205 	ap_perms_init();
2206 
2207 	/* Get AP configuration data if available */
2208 	ap_init_qci_info();
2209 
2210 	/* check default domain setting */
2211 	if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
2212 	    (ap_domain_index >= 0 &&
2213 	     !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
2214 		pr_warn("%d is not a valid cryptographic domain\n",
2215 			ap_domain_index);
2216 		ap_domain_index = -1;
2217 	}
2218 
2219 	/* enable interrupts if available */
2220 	if (ap_interrupts_available() && ap_useirq) {
2221 		rc = register_adapter_interrupt(&ap_airq);
2222 		ap_irq_flag = (rc == 0);
2223 	}
2224 
2225 	/* Create /sys/bus/ap. */
2226 	rc = bus_register(&ap_bus_type);
2227 	if (rc)
2228 		goto out;
2229 
2230 	/* Create /sys/devices/ap. */
2231 	ap_root_device = root_device_register("ap");
2232 	rc = PTR_ERR_OR_ZERO(ap_root_device);
2233 	if (rc)
2234 		goto out_bus;
2235 	ap_root_device->bus = &ap_bus_type;
2236 
2237 	/* Setup the AP bus rescan timer. */
2238 	timer_setup(&ap_config_timer, ap_config_timeout, 0);
2239 
2240 	/*
2241 	 * Setup the high resultion poll timer.
2242 	 * If we are running under z/VM adjust polling to z/VM polling rate.
2243 	 */
2244 	if (MACHINE_IS_VM)
2245 		poll_timeout = 1500000;
2246 	hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2247 	ap_poll_timer.function = ap_poll_timeout;
2248 
2249 	/* Start the low priority AP bus poll thread. */
2250 	if (ap_thread_flag) {
2251 		rc = ap_poll_thread_start();
2252 		if (rc)
2253 			goto out_work;
2254 	}
2255 
2256 	queue_work(system_long_wq, &ap_scan_work);
2257 
2258 	return 0;
2259 
2260 out_work:
2261 	hrtimer_cancel(&ap_poll_timer);
2262 	root_device_unregister(ap_root_device);
2263 out_bus:
2264 	bus_unregister(&ap_bus_type);
2265 out:
2266 	if (ap_irq_flag)
2267 		unregister_adapter_interrupt(&ap_airq);
2268 	kfree(ap_qci_info);
2269 	return rc;
2270 }
2271 device_initcall(ap_module_init);
2272