1 // SPDX-License-Identifier: GPL-2.0+
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>
11 * Adjunct processor bus.
14 #define KMSG_COMPONENT "ap"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
30 #include <linux/atomic.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>
44 * Module parameters; note though this file itself isn't modular.
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);
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).");
57 module_param_named(apmask, apm_str, charp, 0440);
58 MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
61 module_param_named(aqmask, aqm_str, charp, 0440);
62 MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
64 atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
65 EXPORT_SYMBOL(ap_max_msg_size);
67 static struct device *ap_root_device;
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);
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);
80 /* # of bus scans since init */
81 static atomic64_t ap_scan_bus_count;
83 /* # of bindings complete since init */
84 static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
86 /* completion for initial APQN bindings complete */
87 static DECLARE_COMPLETION(ap_init_apqn_bindings_complete);
89 static struct ap_config_info *ap_qci_info;
92 * AP bus related debug feature things.
94 debug_info_t *ap_dbf_info;
97 * Workqueue timer for bus rescan.
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);
105 * Tasklet & timer for AP request polling and interrupts
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;
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.
118 static unsigned long long poll_timeout = 250000;
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;
125 static struct bus_type ap_bus_type;
127 /* Adapter interrupt definitions */
128 static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
130 static int ap_airq_flag;
132 static struct airq_struct ap_airq = {
133 .handler = ap_interrupt_handler,
138 * ap_using_interrupts() - Returns non-zero if interrupt support is
141 static inline int ap_using_interrupts(void)
147 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
149 * Returns the address of the local-summary-indicator of the adapter
150 * interrupt handler for AP, or NULL if adapter interrupts are not
153 void *ap_airq_ptr(void)
155 if (ap_using_interrupts())
156 return ap_airq.lsi_ptr;
161 * ap_interrupts_available(): Test if AP interrupts are available.
163 * Returns 1 if AP interrupts are available.
165 static int ap_interrupts_available(void)
167 return test_facility(65);
171 * ap_qci_available(): Test if AP configuration
172 * information can be queried via QCI subfunction.
174 * Returns 1 if subfunction PQAP(QCI) is available.
176 static int ap_qci_available(void)
178 return test_facility(12);
182 * ap_apft_available(): Test if AP facilities test (APFT)
183 * facility is available.
185 * Returns 1 if APFT is is available.
187 static int ap_apft_available(void)
189 return test_facility(15);
193 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
195 * Returns 1 if the QACT subfunction is available.
197 static inline int ap_qact_available(void)
200 return ap_qci_info->qact;
205 * ap_fetch_qci_info(): Fetch cryptographic config info
207 * Returns the ap configuration info fetched via PQAP(QCI).
208 * On success 0 is returned, on failure a negative errno
209 * is returned, e.g. if the PQAP(QCI) instruction is not
210 * available, the return value will be -EOPNOTSUPP.
212 static inline int ap_fetch_qci_info(struct ap_config_info *info)
214 if (!ap_qci_available())
222 * ap_init_qci_info(): Allocate and query qci config info.
223 * Does also update the static variables ap_max_domain_id
224 * and ap_max_adapter_id if this info is available.
227 static void __init ap_init_qci_info(void)
229 if (!ap_qci_available()) {
230 AP_DBF_INFO("%s QCI not supported\n", __func__);
234 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
237 if (ap_fetch_qci_info(ap_qci_info) != 0) {
242 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
244 if (ap_qci_info->apxa) {
245 if (ap_qci_info->Na) {
246 ap_max_adapter_id = ap_qci_info->Na;
247 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
248 __func__, ap_max_adapter_id);
250 if (ap_qci_info->Nd) {
251 ap_max_domain_id = ap_qci_info->Nd;
252 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
253 __func__, ap_max_domain_id);
259 * ap_test_config(): helper function to extract the nrth bit
260 * within the unsigned int array field.
262 static inline int ap_test_config(unsigned int *field, unsigned int nr)
264 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
268 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
270 * Returns 0 if the card is not configured
271 * 1 if the card is configured or
272 * if the configuration information is not available
274 static inline int ap_test_config_card_id(unsigned int id)
276 if (id > ap_max_adapter_id)
279 return ap_test_config(ap_qci_info->apm, id);
284 * ap_test_config_usage_domain(): Test, whether an AP usage domain
287 * Returns 0 if the usage domain is not configured
288 * 1 if the usage domain is configured or
289 * if the configuration information is not available
291 int ap_test_config_usage_domain(unsigned int domain)
293 if (domain > ap_max_domain_id)
296 return ap_test_config(ap_qci_info->aqm, domain);
299 EXPORT_SYMBOL(ap_test_config_usage_domain);
302 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
304 * @domain AP control domain ID
306 * Returns 1 if the control domain is configured
307 * 0 in all other cases
309 int ap_test_config_ctrl_domain(unsigned int domain)
311 if (!ap_qci_info || domain > ap_max_domain_id)
313 return ap_test_config(ap_qci_info->adm, domain);
315 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
318 * ap_queue_info(): Check and get AP queue info.
319 * Returns true if TAPQ succeeded and the info is filled or
322 static bool ap_queue_info(ap_qid_t qid, int *q_type, unsigned int *q_fac,
323 int *q_depth, int *q_ml, bool *q_decfg)
325 struct ap_queue_status status;
329 unsigned int fac : 32; /* facility bits */
330 unsigned int at : 8; /* ap type */
331 unsigned int _res1 : 8;
332 unsigned int _res2 : 4;
333 unsigned int ml : 4; /* apxl ml */
334 unsigned int _res3 : 4;
335 unsigned int qd : 4; /* queue depth */
341 /* make sure we don't run into a specifiation exception */
342 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
343 AP_QID_QUEUE(qid) > ap_max_domain_id)
346 /* call TAPQ on this APQN */
347 status = ap_test_queue(qid, ap_apft_available(), &tapq_info.value);
348 switch (status.response_code) {
349 case AP_RESPONSE_NORMAL:
350 case AP_RESPONSE_RESET_IN_PROGRESS:
351 case AP_RESPONSE_DECONFIGURED:
352 case AP_RESPONSE_CHECKSTOPPED:
353 case AP_RESPONSE_BUSY:
355 * According to the architecture in all these cases the
356 * info should be filled. All bits 0 is not possible as
357 * there is at least one of the mode bits set.
359 if (WARN_ON_ONCE(!tapq_info.value))
361 *q_type = tapq_info.tapq_gr2.at;
362 *q_fac = tapq_info.tapq_gr2.fac;
363 *q_depth = tapq_info.tapq_gr2.qd;
364 *q_ml = tapq_info.tapq_gr2.ml;
365 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
367 /* For CEX2 and CEX3 the available functions
368 * are not reflected by the facilities bits.
369 * Instead it is coded into the type. So here
370 * modify the function bits based on the type.
372 case AP_DEVICE_TYPE_CEX2A:
373 case AP_DEVICE_TYPE_CEX3A:
374 *q_fac |= 0x08000000;
376 case AP_DEVICE_TYPE_CEX2C:
377 case AP_DEVICE_TYPE_CEX3C:
378 *q_fac |= 0x10000000;
386 * A response code which indicates, there is no info available.
392 void ap_wait(enum ap_sm_wait wait)
397 case AP_SM_WAIT_AGAIN:
398 case AP_SM_WAIT_INTERRUPT:
399 if (ap_using_interrupts())
401 if (ap_poll_kthread) {
402 wake_up(&ap_poll_wait);
406 case AP_SM_WAIT_TIMEOUT:
407 spin_lock_bh(&ap_poll_timer_lock);
408 if (!hrtimer_is_queued(&ap_poll_timer)) {
409 hr_time = poll_timeout;
410 hrtimer_forward_now(&ap_poll_timer, hr_time);
411 hrtimer_restart(&ap_poll_timer);
413 spin_unlock_bh(&ap_poll_timer_lock);
415 case AP_SM_WAIT_NONE:
422 * ap_request_timeout(): Handling of request timeouts
423 * @t: timer making this callback
425 * Handles request timeouts.
427 void ap_request_timeout(struct timer_list *t)
429 struct ap_queue *aq = from_timer(aq, t, timeout);
431 spin_lock_bh(&aq->lock);
432 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
433 spin_unlock_bh(&aq->lock);
437 * ap_poll_timeout(): AP receive polling for finished AP requests.
438 * @unused: Unused pointer.
440 * Schedules the AP tasklet using a high resolution timer.
442 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
444 tasklet_schedule(&ap_tasklet);
445 return HRTIMER_NORESTART;
449 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
450 * @airq: pointer to adapter interrupt descriptor
452 static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
454 inc_irq_stat(IRQIO_APB);
455 tasklet_schedule(&ap_tasklet);
459 * ap_tasklet_fn(): Tasklet to poll all AP devices.
460 * @dummy: Unused variable
462 * Poll all AP devices on the bus.
464 static void ap_tasklet_fn(unsigned long dummy)
468 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
470 /* Reset the indicator if interrupts are used. Thus new interrupts can
471 * be received. Doing it in the beginning of the tasklet is therefor
472 * important that no requests on any AP get lost.
474 if (ap_using_interrupts())
475 xchg(ap_airq.lsi_ptr, 0);
477 spin_lock_bh(&ap_queues_lock);
478 hash_for_each(ap_queues, bkt, aq, hnode) {
479 spin_lock_bh(&aq->lock);
480 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
481 spin_unlock_bh(&aq->lock);
483 spin_unlock_bh(&ap_queues_lock);
488 static int ap_pending_requests(void)
493 spin_lock_bh(&ap_queues_lock);
494 hash_for_each(ap_queues, bkt, aq, hnode) {
495 if (aq->queue_count == 0)
497 spin_unlock_bh(&ap_queues_lock);
500 spin_unlock_bh(&ap_queues_lock);
505 * ap_poll_thread(): Thread that polls for finished requests.
506 * @data: Unused pointer
508 * AP bus poll thread. The purpose of this thread is to poll for
509 * finished requests in a loop if there is a "free" cpu - that is
510 * a cpu that doesn't have anything better to do. The polling stops
511 * as soon as there is another task or if all messages have been
514 static int ap_poll_thread(void *data)
516 DECLARE_WAITQUEUE(wait, current);
518 set_user_nice(current, MAX_NICE);
520 while (!kthread_should_stop()) {
521 add_wait_queue(&ap_poll_wait, &wait);
522 set_current_state(TASK_INTERRUPTIBLE);
523 if (!ap_pending_requests()) {
527 set_current_state(TASK_RUNNING);
528 remove_wait_queue(&ap_poll_wait, &wait);
529 if (need_resched()) {
540 static int ap_poll_thread_start(void)
544 if (ap_using_interrupts() || ap_poll_kthread)
546 mutex_lock(&ap_poll_thread_mutex);
547 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
548 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
550 ap_poll_kthread = NULL;
551 mutex_unlock(&ap_poll_thread_mutex);
555 static void ap_poll_thread_stop(void)
557 if (!ap_poll_kthread)
559 mutex_lock(&ap_poll_thread_mutex);
560 kthread_stop(ap_poll_kthread);
561 ap_poll_kthread = NULL;
562 mutex_unlock(&ap_poll_thread_mutex);
565 #define is_card_dev(x) ((x)->parent == ap_root_device)
566 #define is_queue_dev(x) ((x)->parent != ap_root_device)
570 * @dev: Pointer to device
571 * @drv: Pointer to device_driver
573 * AP bus driver registration/unregistration.
575 static int ap_bus_match(struct device *dev, struct device_driver *drv)
577 struct ap_driver *ap_drv = to_ap_drv(drv);
578 struct ap_device_id *id;
581 * Compare device type of the device with the list of
582 * supported types of the device_driver.
584 for (id = ap_drv->ids; id->match_flags; id++) {
585 if (is_card_dev(dev) &&
586 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
587 id->dev_type == to_ap_dev(dev)->device_type)
589 if (is_queue_dev(dev) &&
590 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
591 id->dev_type == to_ap_dev(dev)->device_type)
598 * ap_uevent(): Uevent function for AP devices.
599 * @dev: Pointer to device
600 * @env: Pointer to kobj_uevent_env
602 * It sets up a single environment variable DEV_TYPE which contains the
603 * hardware device type.
605 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
608 struct ap_device *ap_dev = to_ap_dev(dev);
610 /* Uevents from ap bus core don't need extensions to the env */
611 if (dev == ap_root_device)
614 if (is_card_dev(dev)) {
615 struct ap_card *ac = to_ap_card(&ap_dev->device);
617 /* Set up DEV_TYPE environment variable. */
618 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
622 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
626 /* Add MODE=<accel|cca|ep11> */
627 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL))
628 rc = add_uevent_var(env, "MODE=accel");
629 else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
630 rc = add_uevent_var(env, "MODE=cca");
631 else if (ap_test_bit(&ac->functions, AP_FUNC_EP11))
632 rc = add_uevent_var(env, "MODE=ep11");
636 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
638 /* Add MODE=<accel|cca|ep11> */
639 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL))
640 rc = add_uevent_var(env, "MODE=accel");
641 else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
642 rc = add_uevent_var(env, "MODE=cca");
643 else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11))
644 rc = add_uevent_var(env, "MODE=ep11");
652 static void ap_send_init_scan_done_uevent(void)
654 char *envp[] = { "INITSCAN=done", NULL };
656 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
659 static void ap_send_bindings_complete_uevent(void)
662 char *envp[] = { "BINDINGS=complete", buf, NULL };
664 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
665 atomic64_inc_return(&ap_bindings_complete_count));
666 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
669 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
672 char *envp[] = { buf, NULL };
674 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
676 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
678 EXPORT_SYMBOL(ap_send_config_uevent);
680 void ap_send_online_uevent(struct ap_device *ap_dev, int online)
683 char *envp[] = { buf, NULL };
685 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
687 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
689 EXPORT_SYMBOL(ap_send_online_uevent);
692 * calc # of bound APQNs
695 struct __ap_calc_ctrs {
700 static int __ap_calc_helper(struct device *dev, void *arg)
702 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *) arg;
704 if (is_queue_dev(dev)) {
706 if ((to_ap_dev(dev))->drv)
713 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
715 struct __ap_calc_ctrs ctrs;
717 memset(&ctrs, 0, sizeof(ctrs));
718 bus_for_each_dev(&ap_bus_type, NULL, (void *) &ctrs, __ap_calc_helper);
725 * After initial ap bus scan do check if all existing APQNs are
726 * bound to device drivers.
728 static void ap_check_bindings_complete(void)
730 unsigned int apqns, bound;
732 if (atomic64_read(&ap_scan_bus_count) >= 1) {
733 ap_calc_bound_apqns(&apqns, &bound);
734 if (bound == apqns) {
735 if (!completion_done(&ap_init_apqn_bindings_complete)) {
736 complete_all(&ap_init_apqn_bindings_complete);
737 AP_DBF(DBF_INFO, "%s complete\n", __func__);
739 ap_send_bindings_complete_uevent();
745 * Interface to wait for the AP bus to have done one initial ap bus
746 * scan and all detected APQNs have been bound to device drivers.
747 * If these both conditions are not fulfilled, this function blocks
748 * on a condition with wait_for_completion_interruptible_timeout().
749 * If these both conditions are fulfilled (before the timeout hits)
750 * the return value is 0. If the timeout (in jiffies) hits instead
751 * -ETIME is returned. On failures negative return values are
752 * returned to the caller.
754 int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
758 if (completion_done(&ap_init_apqn_bindings_complete))
762 l = wait_for_completion_interruptible_timeout(
763 &ap_init_apqn_bindings_complete, timeout);
765 l = wait_for_completion_interruptible(
766 &ap_init_apqn_bindings_complete);
768 return l == -ERESTARTSYS ? -EINTR : l;
769 else if (l == 0 && timeout)
774 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
776 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
778 if (is_queue_dev(dev) &&
779 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
780 device_unregister(dev);
784 static int __ap_revise_reserved(struct device *dev, void *dummy)
786 int rc, card, queue, devres, drvres;
788 if (is_queue_dev(dev)) {
789 card = AP_QID_CARD(to_ap_queue(dev)->qid);
790 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
791 mutex_lock(&ap_perms_mutex);
792 devres = test_bit_inv(card, ap_perms.apm)
793 && test_bit_inv(queue, ap_perms.aqm);
794 mutex_unlock(&ap_perms_mutex);
795 drvres = to_ap_drv(dev->driver)->flags
796 & AP_DRIVER_FLAG_DEFAULT;
797 if (!!devres != !!drvres) {
798 AP_DBF_DBG("reprobing queue=%02x.%04x\n",
800 rc = device_reprobe(dev);
807 static void ap_bus_revise_bindings(void)
809 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
812 int ap_owned_by_def_drv(int card, int queue)
816 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
819 mutex_lock(&ap_perms_mutex);
821 if (test_bit_inv(card, ap_perms.apm)
822 && test_bit_inv(queue, ap_perms.aqm))
825 mutex_unlock(&ap_perms_mutex);
829 EXPORT_SYMBOL(ap_owned_by_def_drv);
831 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
834 int card, queue, rc = 0;
836 mutex_lock(&ap_perms_mutex);
838 for (card = 0; !rc && card < AP_DEVICES; card++)
839 if (test_bit_inv(card, apm) &&
840 test_bit_inv(card, ap_perms.apm))
841 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
842 if (test_bit_inv(queue, aqm) &&
843 test_bit_inv(queue, ap_perms.aqm))
846 mutex_unlock(&ap_perms_mutex);
850 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
852 static int ap_device_probe(struct device *dev)
854 struct ap_device *ap_dev = to_ap_dev(dev);
855 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
856 int card, queue, devres, drvres, rc = -ENODEV;
858 if (!get_device(dev))
861 if (is_queue_dev(dev)) {
863 * If the apqn is marked as reserved/used by ap bus and
864 * default drivers, only probe with drivers with the default
865 * flag set. If it is not marked, only probe with drivers
866 * with the default flag not set.
868 card = AP_QID_CARD(to_ap_queue(dev)->qid);
869 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
870 mutex_lock(&ap_perms_mutex);
871 devres = test_bit_inv(card, ap_perms.apm)
872 && test_bit_inv(queue, ap_perms.aqm);
873 mutex_unlock(&ap_perms_mutex);
874 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
875 if (!!devres != !!drvres)
879 /* Add queue/card to list of active queues/cards */
880 spin_lock_bh(&ap_queues_lock);
881 if (is_queue_dev(dev))
882 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
883 to_ap_queue(dev)->qid);
884 spin_unlock_bh(&ap_queues_lock);
886 ap_dev->drv = ap_drv;
887 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
890 spin_lock_bh(&ap_queues_lock);
891 if (is_queue_dev(dev))
892 hash_del(&to_ap_queue(dev)->hnode);
893 spin_unlock_bh(&ap_queues_lock);
896 ap_check_bindings_complete();
904 static int ap_device_remove(struct device *dev)
906 struct ap_device *ap_dev = to_ap_dev(dev);
907 struct ap_driver *ap_drv = ap_dev->drv;
909 /* prepare ap queue device removal */
910 if (is_queue_dev(dev))
911 ap_queue_prepare_remove(to_ap_queue(dev));
913 /* driver's chance to clean up gracefully */
915 ap_drv->remove(ap_dev);
917 /* now do the ap queue device remove */
918 if (is_queue_dev(dev))
919 ap_queue_remove(to_ap_queue(dev));
921 /* Remove queue/card from list of active queues/cards */
922 spin_lock_bh(&ap_queues_lock);
923 if (is_queue_dev(dev))
924 hash_del(&to_ap_queue(dev)->hnode);
925 spin_unlock_bh(&ap_queues_lock);
933 struct ap_queue *ap_get_qdev(ap_qid_t qid)
938 spin_lock_bh(&ap_queues_lock);
939 hash_for_each(ap_queues, bkt, aq, hnode) {
940 if (aq->qid == qid) {
941 get_device(&aq->ap_dev.device);
942 spin_unlock_bh(&ap_queues_lock);
946 spin_unlock_bh(&ap_queues_lock);
950 EXPORT_SYMBOL(ap_get_qdev);
952 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
955 struct device_driver *drv = &ap_drv->driver;
957 drv->bus = &ap_bus_type;
960 return driver_register(drv);
962 EXPORT_SYMBOL(ap_driver_register);
964 void ap_driver_unregister(struct ap_driver *ap_drv)
966 driver_unregister(&ap_drv->driver);
968 EXPORT_SYMBOL(ap_driver_unregister);
970 void ap_bus_force_rescan(void)
972 /* processing a asynchronous bus rescan */
973 del_timer(&ap_config_timer);
974 queue_work(system_long_wq, &ap_scan_work);
975 flush_work(&ap_scan_work);
977 EXPORT_SYMBOL(ap_bus_force_rescan);
980 * A config change has happened, force an ap bus rescan.
982 void ap_bus_cfg_chg(void)
984 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
986 ap_bus_force_rescan();
990 * hex2bitmap() - parse hex mask string and set bitmap.
991 * Valid strings are "0x012345678" with at least one valid hex number.
992 * Rest of the bitmap to the right is padded with 0. No spaces allowed
993 * within the string, the leading 0x may be omitted.
994 * Returns the bitmask with exactly the bits set as given by the hex
995 * string (both in big endian order).
997 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1001 /* bits needs to be a multiple of 8 */
1005 if (str[0] == '0' && str[1] == 'x')
1010 for (i = 0; isxdigit(*str) && i < bits; str++) {
1011 b = hex_to_bin(*str);
1012 for (n = 0; n < 4; n++)
1013 if (b & (0x08 >> n))
1014 set_bit_inv(i + n, bitmap);
1026 * modify_bitmap() - parse bitmask argument and modify an existing
1027 * bit mask accordingly. A concatenation (done with ',') of these
1028 * terms is recognized:
1029 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1030 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1031 * 0...bits-1; the leading + or - is required. Here are some examples:
1032 * +0-15,+32,-128,-0xFF
1033 * -0-255,+1-16,+0x128
1034 * +1,+2,+3,+4,-5,-7-10
1035 * Returns the new bitmap after all changes have been applied. Every
1036 * positive value in the string will set a bit and every negative value
1037 * in the string will clear a bit. As a bit may be touched more than once,
1038 * the last 'operation' wins:
1039 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1040 * cleared again. All other bits are unmodified.
1042 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1047 /* bits needs to be a multiple of 8 */
1053 if (sign != '+' && sign != '-')
1055 a = z = simple_strtoul(str, &np, 0);
1056 if (str == np || a >= bits)
1060 z = simple_strtoul(++str, &np, 0);
1061 if (str == np || a > z || z >= bits)
1065 for (i = a; i <= z; i++)
1067 set_bit_inv(i, bitmap);
1069 clear_bit_inv(i, bitmap);
1070 while (*str == ',' || *str == '\n')
1077 int ap_parse_mask_str(const char *str,
1078 unsigned long *bitmap, int bits,
1081 unsigned long *newmap, size;
1084 /* bits needs to be a multiple of 8 */
1088 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
1089 newmap = kmalloc(size, GFP_KERNEL);
1092 if (mutex_lock_interruptible(lock)) {
1094 return -ERESTARTSYS;
1097 if (*str == '+' || *str == '-') {
1098 memcpy(newmap, bitmap, size);
1099 rc = modify_bitmap(str, newmap, bits);
1101 memset(newmap, 0, size);
1102 rc = hex2bitmap(str, newmap, bits);
1105 memcpy(bitmap, newmap, size);
1110 EXPORT_SYMBOL(ap_parse_mask_str);
1113 * AP bus attributes.
1116 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1118 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1121 static ssize_t ap_domain_store(struct bus_type *bus,
1122 const char *buf, size_t count)
1126 if (sscanf(buf, "%i\n", &domain) != 1 ||
1127 domain < 0 || domain > ap_max_domain_id ||
1128 !test_bit_inv(domain, ap_perms.aqm))
1131 spin_lock_bh(&ap_domain_lock);
1132 ap_domain_index = domain;
1133 spin_unlock_bh(&ap_domain_lock);
1135 AP_DBF_INFO("stored new default domain=%d\n", domain);
1140 static BUS_ATTR_RW(ap_domain);
1142 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1144 if (!ap_qci_info) /* QCI not supported */
1145 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1147 return scnprintf(buf, PAGE_SIZE,
1148 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1149 ap_qci_info->adm[0], ap_qci_info->adm[1],
1150 ap_qci_info->adm[2], ap_qci_info->adm[3],
1151 ap_qci_info->adm[4], ap_qci_info->adm[5],
1152 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1155 static BUS_ATTR_RO(ap_control_domain_mask);
1157 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1159 if (!ap_qci_info) /* QCI not supported */
1160 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1162 return scnprintf(buf, PAGE_SIZE,
1163 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1164 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1165 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1166 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1167 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1170 static BUS_ATTR_RO(ap_usage_domain_mask);
1172 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1174 if (!ap_qci_info) /* QCI not supported */
1175 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1177 return scnprintf(buf, PAGE_SIZE,
1178 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1179 ap_qci_info->apm[0], ap_qci_info->apm[1],
1180 ap_qci_info->apm[2], ap_qci_info->apm[3],
1181 ap_qci_info->apm[4], ap_qci_info->apm[5],
1182 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1185 static BUS_ATTR_RO(ap_adapter_mask);
1187 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1189 return scnprintf(buf, PAGE_SIZE, "%d\n",
1190 ap_using_interrupts() ? 1 : 0);
1193 static BUS_ATTR_RO(ap_interrupts);
1195 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1197 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1200 static ssize_t config_time_store(struct bus_type *bus,
1201 const char *buf, size_t count)
1205 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1207 ap_config_time = time;
1208 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1212 static BUS_ATTR_RW(config_time);
1214 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1216 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1219 static ssize_t poll_thread_store(struct bus_type *bus,
1220 const char *buf, size_t count)
1224 if (sscanf(buf, "%d\n", &flag) != 1)
1227 rc = ap_poll_thread_start();
1231 ap_poll_thread_stop();
1235 static BUS_ATTR_RW(poll_thread);
1237 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1239 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1242 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1245 unsigned long long time;
1248 /* 120 seconds = maximum poll interval */
1249 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1250 time > 120000000000ULL)
1252 poll_timeout = time;
1253 hr_time = poll_timeout;
1255 spin_lock_bh(&ap_poll_timer_lock);
1256 hrtimer_cancel(&ap_poll_timer);
1257 hrtimer_set_expires(&ap_poll_timer, hr_time);
1258 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1259 spin_unlock_bh(&ap_poll_timer_lock);
1264 static BUS_ATTR_RW(poll_timeout);
1266 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1268 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1271 static BUS_ATTR_RO(ap_max_domain_id);
1273 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1275 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1278 static BUS_ATTR_RO(ap_max_adapter_id);
1280 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1284 if (mutex_lock_interruptible(&ap_perms_mutex))
1285 return -ERESTARTSYS;
1286 rc = scnprintf(buf, PAGE_SIZE,
1287 "0x%016lx%016lx%016lx%016lx\n",
1288 ap_perms.apm[0], ap_perms.apm[1],
1289 ap_perms.apm[2], ap_perms.apm[3]);
1290 mutex_unlock(&ap_perms_mutex);
1295 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1300 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1304 ap_bus_revise_bindings();
1309 static BUS_ATTR_RW(apmask);
1311 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1315 if (mutex_lock_interruptible(&ap_perms_mutex))
1316 return -ERESTARTSYS;
1317 rc = scnprintf(buf, PAGE_SIZE,
1318 "0x%016lx%016lx%016lx%016lx\n",
1319 ap_perms.aqm[0], ap_perms.aqm[1],
1320 ap_perms.aqm[2], ap_perms.aqm[3]);
1321 mutex_unlock(&ap_perms_mutex);
1326 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1331 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1335 ap_bus_revise_bindings();
1340 static BUS_ATTR_RW(aqmask);
1342 static ssize_t scans_show(struct bus_type *bus, char *buf)
1344 return scnprintf(buf, PAGE_SIZE, "%llu\n",
1345 atomic64_read(&ap_scan_bus_count));
1348 static BUS_ATTR_RO(scans);
1350 static ssize_t bindings_show(struct bus_type *bus, char *buf)
1353 unsigned int apqns, n;
1355 ap_calc_bound_apqns(&apqns, &n);
1356 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1357 rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns);
1359 rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns);
1364 static BUS_ATTR_RO(bindings);
1366 static struct attribute *ap_bus_attrs[] = {
1367 &bus_attr_ap_domain.attr,
1368 &bus_attr_ap_control_domain_mask.attr,
1369 &bus_attr_ap_usage_domain_mask.attr,
1370 &bus_attr_ap_adapter_mask.attr,
1371 &bus_attr_config_time.attr,
1372 &bus_attr_poll_thread.attr,
1373 &bus_attr_ap_interrupts.attr,
1374 &bus_attr_poll_timeout.attr,
1375 &bus_attr_ap_max_domain_id.attr,
1376 &bus_attr_ap_max_adapter_id.attr,
1377 &bus_attr_apmask.attr,
1378 &bus_attr_aqmask.attr,
1379 &bus_attr_scans.attr,
1380 &bus_attr_bindings.attr,
1383 ATTRIBUTE_GROUPS(ap_bus);
1385 static struct bus_type ap_bus_type = {
1387 .bus_groups = ap_bus_groups,
1388 .match = &ap_bus_match,
1389 .uevent = &ap_uevent,
1390 .probe = ap_device_probe,
1391 .remove = ap_device_remove,
1395 * ap_select_domain(): Select an AP domain if possible and we haven't
1396 * already done so before.
1398 static void ap_select_domain(void)
1400 struct ap_queue_status status;
1404 * Choose the default domain. Either the one specified with
1405 * the "domain=" parameter or the first domain with at least
1408 spin_lock_bh(&ap_domain_lock);
1409 if (ap_domain_index >= 0) {
1410 /* Domain has already been selected. */
1413 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1414 if (!ap_test_config_usage_domain(dom) ||
1415 !test_bit_inv(dom, ap_perms.aqm))
1417 for (card = 0; card <= ap_max_adapter_id; card++) {
1418 if (!ap_test_config_card_id(card) ||
1419 !test_bit_inv(card, ap_perms.apm))
1421 status = ap_test_queue(AP_MKQID(card, dom),
1422 ap_apft_available(),
1424 if (status.response_code == AP_RESPONSE_NORMAL)
1427 if (card <= ap_max_adapter_id)
1430 if (dom <= ap_max_domain_id) {
1431 ap_domain_index = dom;
1432 AP_DBF_INFO("%s new default domain is %d\n",
1433 __func__, ap_domain_index);
1436 spin_unlock_bh(&ap_domain_lock);
1440 * This function checks the type and returns either 0 for not
1441 * supported or the highest compatible type value (which may
1442 * include the input type value).
1444 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1448 /* < CEX2A is not supported */
1449 if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1450 AP_DBF_WARN("get_comp_type queue=%02x.%04x unsupported type %d\n",
1451 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1454 /* up to CEX7 known and fully supported */
1455 if (rawtype <= AP_DEVICE_TYPE_CEX7)
1458 * unknown new type > CEX7, check for compatibility
1459 * to the highest known and supported type which is
1460 * currently CEX7 with the help of the QACT function.
1462 if (ap_qact_available()) {
1463 struct ap_queue_status status;
1464 union ap_qact_ap_info apinfo = {0};
1466 apinfo.mode = (func >> 26) & 0x07;
1467 apinfo.cat = AP_DEVICE_TYPE_CEX7;
1468 status = ap_qact(qid, 0, &apinfo);
1469 if (status.response_code == AP_RESPONSE_NORMAL
1470 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1471 && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
1472 comp_type = apinfo.cat;
1475 AP_DBF_WARN("get_comp_type queue=%02x.%04x unable to map type %d\n",
1476 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1477 else if (comp_type != rawtype)
1478 AP_DBF_INFO("get_comp_type queue=%02x.%04x map type %d to %d\n",
1479 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1480 rawtype, comp_type);
1485 * Helper function to be used with bus_find_dev
1486 * matches for the card device with the given id
1488 static int __match_card_device_with_id(struct device *dev, const void *data)
1490 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1494 * Helper function to be used with bus_find_dev
1495 * matches for the queue device with a given qid
1497 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1499 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1503 * Helper function to be used with bus_find_dev
1504 * matches any queue device with given queue id
1506 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1508 return is_queue_dev(dev)
1509 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1513 * Helper function for ap_scan_bus().
1514 * Remove card device and associated queue devices.
1516 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1518 bus_for_each_dev(&ap_bus_type, NULL,
1519 (void *)(long) ac->id,
1520 __ap_queue_devices_with_id_unregister);
1521 device_unregister(&ac->ap_dev.device);
1525 * Helper function for ap_scan_bus().
1526 * Does the scan bus job for all the domains within
1527 * a valid adapter given by an ap_card ptr.
1529 static inline void ap_scan_domains(struct ap_card *ac)
1535 struct ap_queue *aq;
1536 int rc, dom, depth, type, ml;
1539 * Go through the configuration for the domains and compare them
1540 * to the existing queue devices. Also take care of the config
1541 * and error state for the queue devices.
1544 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1545 qid = AP_MKQID(ac->id, dom);
1546 dev = bus_find_device(&ap_bus_type, NULL,
1548 __match_queue_device_with_qid);
1549 aq = dev ? to_ap_queue(dev) : NULL;
1550 if (!ap_test_config_usage_domain(dom)) {
1552 AP_DBF_INFO("%s(%d,%d) not in config any more, rm queue device\n",
1553 __func__, ac->id, dom);
1554 device_unregister(dev);
1559 /* domain is valid, get info from this APQN */
1560 if (!ap_queue_info(qid, &type, &func, &depth, &ml, &decfg)) {
1563 "%s(%d,%d) ap_queue_info() not successful, rm queue device\n",
1564 __func__, ac->id, dom);
1565 device_unregister(dev);
1570 /* if no queue device exists, create a new one */
1572 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1574 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1575 __func__, ac->id, dom);
1579 aq->config = !decfg;
1580 dev = &aq->ap_dev.device;
1581 dev->bus = &ap_bus_type;
1582 dev->parent = &ac->ap_dev.device;
1583 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1584 /* register queue device */
1585 rc = device_register(dev);
1587 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1588 __func__, ac->id, dom);
1589 goto put_dev_and_continue;
1591 /* get it and thus adjust reference counter */
1594 AP_DBF_INFO("%s(%d,%d) new (decfg) queue device created\n",
1595 __func__, ac->id, dom);
1597 AP_DBF_INFO("%s(%d,%d) new queue device created\n",
1598 __func__, ac->id, dom);
1599 goto put_dev_and_continue;
1601 /* Check config state on the already existing queue device */
1602 spin_lock_bh(&aq->lock);
1603 if (decfg && aq->config) {
1604 /* config off this queue device */
1606 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1607 aq->dev_state = AP_DEV_STATE_ERROR;
1608 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1610 spin_unlock_bh(&aq->lock);
1611 AP_DBF_INFO("%s(%d,%d) queue device config off\n",
1612 __func__, ac->id, dom);
1613 ap_send_config_uevent(&aq->ap_dev, aq->config);
1614 /* 'receive' pending messages with -EAGAIN */
1616 goto put_dev_and_continue;
1618 if (!decfg && !aq->config) {
1619 /* config on this queue device */
1621 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1622 aq->dev_state = AP_DEV_STATE_OPERATING;
1623 aq->sm_state = AP_SM_STATE_RESET_START;
1625 spin_unlock_bh(&aq->lock);
1626 AP_DBF_INFO("%s(%d,%d) queue device config on\n",
1627 __func__, ac->id, dom);
1628 ap_send_config_uevent(&aq->ap_dev, aq->config);
1629 goto put_dev_and_continue;
1631 /* handle other error states */
1632 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1633 spin_unlock_bh(&aq->lock);
1634 /* 'receive' pending messages with -EAGAIN */
1636 /* re-init (with reset) the queue device */
1637 ap_queue_init_state(aq);
1638 AP_DBF_INFO("%s(%d,%d) queue device reinit enforced\n",
1639 __func__, ac->id, dom);
1640 goto put_dev_and_continue;
1642 spin_unlock_bh(&aq->lock);
1643 put_dev_and_continue:
1649 * Helper function for ap_scan_bus().
1650 * Does the scan bus job for the given adapter id.
1652 static inline void ap_scan_adapter(int ap)
1659 int rc, dom, depth, type, comp_type, ml;
1661 /* Is there currently a card device for this adapter ? */
1662 dev = bus_find_device(&ap_bus_type, NULL,
1664 __match_card_device_with_id);
1665 ac = dev ? to_ap_card(dev) : NULL;
1667 /* Adapter not in configuration ? */
1668 if (!ap_test_config_card_id(ap)) {
1670 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devices\n",
1672 ap_scan_rm_card_dev_and_queue_devs(ac);
1679 * Adapter ap is valid in the current configuration. So do some checks:
1680 * If no card device exists, build one. If a card device exists, check
1681 * for type and functions changed. For all this we need to find a valid
1685 for (dom = 0; dom <= ap_max_domain_id; dom++)
1686 if (ap_test_config_usage_domain(dom)) {
1687 qid = AP_MKQID(ap, dom);
1688 if (ap_queue_info(qid, &type, &func,
1689 &depth, &ml, &decfg))
1692 if (dom > ap_max_domain_id) {
1693 /* Could not find a valid APQN for this adapter */
1696 "%s(%d) no type info (no APQN found), rm card and queue devices\n",
1698 ap_scan_rm_card_dev_and_queue_devs(ac);
1701 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1707 /* No apdater type info available, an unusable adapter */
1709 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devices\n",
1711 ap_scan_rm_card_dev_and_queue_devs(ac);
1714 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1721 /* Check APQN against existing card device for changes */
1722 if (ac->raw_hwtype != type) {
1723 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devices\n",
1724 __func__, ap, type);
1725 ap_scan_rm_card_dev_and_queue_devs(ac);
1728 } else if (ac->functions != func) {
1729 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devices\n",
1730 __func__, ap, type);
1731 ap_scan_rm_card_dev_and_queue_devs(ac);
1735 if (decfg && ac->config) {
1737 AP_DBF_INFO("%s(%d) card device config off\n",
1739 ap_send_config_uevent(&ac->ap_dev, ac->config);
1741 if (!decfg && !ac->config) {
1743 AP_DBF_INFO("%s(%d) card device config on\n",
1745 ap_send_config_uevent(&ac->ap_dev, ac->config);
1751 /* Build a new card device */
1752 comp_type = ap_get_compatible_type(qid, type, func);
1754 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
1755 __func__, ap, type);
1758 ac = ap_card_create(ap, depth, type, comp_type, func, ml);
1760 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
1764 ac->config = !decfg;
1765 dev = &ac->ap_dev.device;
1766 dev->bus = &ap_bus_type;
1767 dev->parent = ap_root_device;
1768 dev_set_name(dev, "card%02x", ap);
1769 /* maybe enlarge ap_max_msg_size to support this card */
1770 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
1771 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
1772 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
1773 __func__, ap, atomic_read(&ap_max_msg_size));
1775 /* Register the new card device with AP bus */
1776 rc = device_register(dev);
1778 AP_DBF_WARN("%s(%d) device_register() failed\n",
1783 /* get it and thus adjust reference counter */
1786 AP_DBF_INFO("%s(%d) new (decfg) card device type=%d func=0x%08x created\n",
1787 __func__, ap, type, func);
1789 AP_DBF_INFO("%s(%d) new card device type=%d func=0x%08x created\n",
1790 __func__, ap, type, func);
1793 /* Verify the domains and the queue devices for this card */
1794 ap_scan_domains(ac);
1796 /* release the card device */
1797 put_device(&ac->ap_dev.device);
1801 * ap_scan_bus(): Scan the AP bus for new devices
1802 * Runs periodically, workqueue timer (ap_config_time)
1804 static void ap_scan_bus(struct work_struct *unused)
1808 ap_fetch_qci_info(ap_qci_info);
1811 AP_DBF_DBG("%s running\n", __func__);
1813 /* loop over all possible adapters */
1814 for (ap = 0; ap <= ap_max_adapter_id; ap++)
1815 ap_scan_adapter(ap);
1817 /* check if there is at least one queue available with default domain */
1818 if (ap_domain_index >= 0) {
1819 struct device *dev =
1820 bus_find_device(&ap_bus_type, NULL,
1821 (void *)(long) ap_domain_index,
1822 __match_queue_device_with_queue_id);
1826 AP_DBF_INFO("no queue device with default domain %d available\n",
1830 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
1831 AP_DBF(DBF_DEBUG, "%s init scan complete\n", __func__);
1832 ap_send_init_scan_done_uevent();
1833 ap_check_bindings_complete();
1836 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1839 static void ap_config_timeout(struct timer_list *unused)
1841 queue_work(system_long_wq, &ap_scan_work);
1844 static int __init ap_debug_init(void)
1846 ap_dbf_info = debug_register("ap", 1, 1,
1847 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1848 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1849 debug_set_level(ap_dbf_info, DBF_ERR);
1854 static void __init ap_perms_init(void)
1856 /* all resources useable if no kernel parameter string given */
1857 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1858 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1859 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1861 /* apm kernel parameter string */
1863 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1864 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1868 /* aqm kernel parameter string */
1870 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1871 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1877 * ap_module_init(): The module initialization code.
1879 * Initializes the module.
1881 static int __init ap_module_init(void)
1885 rc = ap_debug_init();
1889 if (!ap_instructions_available()) {
1890 pr_warn("The hardware system does not support AP instructions\n");
1894 /* init ap_queue hashtable */
1895 hash_init(ap_queues);
1897 /* set up the AP permissions (ioctls, ap and aq masks) */
1900 /* Get AP configuration data if available */
1903 /* check default domain setting */
1904 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
1905 (ap_domain_index >= 0 &&
1906 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1907 pr_warn("%d is not a valid cryptographic domain\n",
1909 ap_domain_index = -1;
1912 /* enable interrupts if available */
1913 if (ap_interrupts_available()) {
1914 rc = register_adapter_interrupt(&ap_airq);
1915 ap_airq_flag = (rc == 0);
1918 /* Create /sys/bus/ap. */
1919 rc = bus_register(&ap_bus_type);
1923 /* Create /sys/devices/ap. */
1924 ap_root_device = root_device_register("ap");
1925 rc = PTR_ERR_OR_ZERO(ap_root_device);
1928 ap_root_device->bus = &ap_bus_type;
1930 /* Setup the AP bus rescan timer. */
1931 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1934 * Setup the high resultion poll timer.
1935 * If we are running under z/VM adjust polling to z/VM polling rate.
1938 poll_timeout = 1500000;
1939 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1940 ap_poll_timer.function = ap_poll_timeout;
1942 /* Start the low priority AP bus poll thread. */
1943 if (ap_thread_flag) {
1944 rc = ap_poll_thread_start();
1949 queue_work(system_long_wq, &ap_scan_work);
1954 hrtimer_cancel(&ap_poll_timer);
1955 root_device_unregister(ap_root_device);
1957 bus_unregister(&ap_bus_type);
1959 if (ap_using_interrupts())
1960 unregister_adapter_interrupt(&ap_airq);
1964 device_initcall(ap_module_init);