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