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