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