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