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