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 bool ap_irq_flag;
132 static struct airq_struct ap_airq = {
133 .handler = ap_interrupt_handler,
138 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
140 * Returns the address of the local-summary-indicator of the adapter
141 * interrupt handler for AP, or NULL if adapter interrupts are not
144 void *ap_airq_ptr(void)
147 return ap_airq.lsi_ptr;
152 * ap_interrupts_available(): Test if AP interrupts are available.
154 * Returns 1 if AP interrupts are available.
156 static int ap_interrupts_available(void)
158 return test_facility(65);
162 * ap_qci_available(): Test if AP configuration
163 * information can be queried via QCI subfunction.
165 * Returns 1 if subfunction PQAP(QCI) is available.
167 static int ap_qci_available(void)
169 return test_facility(12);
173 * ap_apft_available(): Test if AP facilities test (APFT)
174 * facility is available.
176 * Returns 1 if APFT is is available.
178 static int ap_apft_available(void)
180 return test_facility(15);
184 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
186 * Returns 1 if the QACT subfunction is available.
188 static inline int ap_qact_available(void)
191 return ap_qci_info->qact;
196 * ap_fetch_qci_info(): Fetch cryptographic config info
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.
203 static inline int ap_fetch_qci_info(struct ap_config_info *info)
205 if (!ap_qci_available())
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.
218 static void __init ap_init_qci_info(void)
220 if (!ap_qci_available()) {
221 AP_DBF_INFO("%s QCI not supported\n", __func__);
225 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
228 if (ap_fetch_qci_info(ap_qci_info) != 0) {
233 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
235 if (ap_qci_info->apxa) {
236 if (ap_qci_info->Na) {
237 ap_max_adapter_id = ap_qci_info->Na;
238 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
239 __func__, ap_max_adapter_id);
241 if (ap_qci_info->Nd) {
242 ap_max_domain_id = ap_qci_info->Nd;
243 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
244 __func__, ap_max_domain_id);
250 * ap_test_config(): helper function to extract the nrth bit
251 * within the unsigned int array field.
253 static inline int ap_test_config(unsigned int *field, unsigned int nr)
255 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
259 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
261 * Returns 0 if the card is not configured
262 * 1 if the card is configured or
263 * if the configuration information is not available
265 static inline int ap_test_config_card_id(unsigned int id)
267 if (id > ap_max_adapter_id)
270 return ap_test_config(ap_qci_info->apm, id);
275 * ap_test_config_usage_domain(): Test, whether an AP usage domain
278 * Returns 0 if the usage domain is not configured
279 * 1 if the usage domain is configured or
280 * if the configuration information is not available
282 int ap_test_config_usage_domain(unsigned int domain)
284 if (domain > ap_max_domain_id)
287 return ap_test_config(ap_qci_info->aqm, domain);
290 EXPORT_SYMBOL(ap_test_config_usage_domain);
293 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
295 * @domain AP control domain ID
297 * Returns 1 if the control domain is configured
298 * 0 in all other cases
300 int ap_test_config_ctrl_domain(unsigned int domain)
302 if (!ap_qci_info || domain > ap_max_domain_id)
304 return ap_test_config(ap_qci_info->adm, domain);
306 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
309 * ap_queue_info(): Check and get AP queue info.
310 * Returns true if TAPQ succeeded and the info is filled or
313 static bool ap_queue_info(ap_qid_t qid, int *q_type, unsigned int *q_fac,
314 int *q_depth, int *q_ml, bool *q_decfg)
316 struct ap_queue_status status;
320 unsigned int fac : 32; /* facility bits */
321 unsigned int at : 8; /* ap type */
322 unsigned int _res1 : 8;
323 unsigned int _res2 : 4;
324 unsigned int ml : 4; /* apxl ml */
325 unsigned int _res3 : 4;
326 unsigned int qd : 4; /* queue depth */
332 /* make sure we don't run into a specifiation exception */
333 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
334 AP_QID_QUEUE(qid) > ap_max_domain_id)
337 /* call TAPQ on this APQN */
338 status = ap_test_queue(qid, ap_apft_available(), &tapq_info.value);
339 switch (status.response_code) {
340 case AP_RESPONSE_NORMAL:
341 case AP_RESPONSE_RESET_IN_PROGRESS:
342 case AP_RESPONSE_DECONFIGURED:
343 case AP_RESPONSE_CHECKSTOPPED:
344 case AP_RESPONSE_BUSY:
346 * According to the architecture in all these cases the
347 * info should be filled. All bits 0 is not possible as
348 * there is at least one of the mode bits set.
350 if (WARN_ON_ONCE(!tapq_info.value))
352 *q_type = tapq_info.tapq_gr2.at;
353 *q_fac = tapq_info.tapq_gr2.fac;
354 *q_depth = tapq_info.tapq_gr2.qd;
355 *q_ml = tapq_info.tapq_gr2.ml;
356 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
358 /* For CEX2 and CEX3 the available functions
359 * are not reflected by the facilities bits.
360 * Instead it is coded into the type. So here
361 * modify the function bits based on the type.
363 case AP_DEVICE_TYPE_CEX2A:
364 case AP_DEVICE_TYPE_CEX3A:
365 *q_fac |= 0x08000000;
367 case AP_DEVICE_TYPE_CEX2C:
368 case AP_DEVICE_TYPE_CEX3C:
369 *q_fac |= 0x10000000;
377 * A response code which indicates, there is no info available.
383 void ap_wait(enum ap_sm_wait wait)
388 case AP_SM_WAIT_AGAIN:
389 case AP_SM_WAIT_INTERRUPT:
392 if (ap_poll_kthread) {
393 wake_up(&ap_poll_wait);
397 case AP_SM_WAIT_TIMEOUT:
398 spin_lock_bh(&ap_poll_timer_lock);
399 if (!hrtimer_is_queued(&ap_poll_timer)) {
400 hr_time = poll_timeout;
401 hrtimer_forward_now(&ap_poll_timer, hr_time);
402 hrtimer_restart(&ap_poll_timer);
404 spin_unlock_bh(&ap_poll_timer_lock);
406 case AP_SM_WAIT_NONE:
413 * ap_request_timeout(): Handling of request timeouts
414 * @t: timer making this callback
416 * Handles request timeouts.
418 void ap_request_timeout(struct timer_list *t)
420 struct ap_queue *aq = from_timer(aq, t, timeout);
422 spin_lock_bh(&aq->lock);
423 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
424 spin_unlock_bh(&aq->lock);
428 * ap_poll_timeout(): AP receive polling for finished AP requests.
429 * @unused: Unused pointer.
431 * Schedules the AP tasklet using a high resolution timer.
433 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
435 tasklet_schedule(&ap_tasklet);
436 return HRTIMER_NORESTART;
440 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
441 * @airq: pointer to adapter interrupt descriptor
443 static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
445 inc_irq_stat(IRQIO_APB);
446 tasklet_schedule(&ap_tasklet);
450 * ap_tasklet_fn(): Tasklet to poll all AP devices.
451 * @dummy: Unused variable
453 * Poll all AP devices on the bus.
455 static void ap_tasklet_fn(unsigned long dummy)
459 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
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.
466 xchg(ap_airq.lsi_ptr, 0);
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);
474 spin_unlock_bh(&ap_queues_lock);
479 static int ap_pending_requests(void)
484 spin_lock_bh(&ap_queues_lock);
485 hash_for_each(ap_queues, bkt, aq, hnode) {
486 if (aq->queue_count == 0)
488 spin_unlock_bh(&ap_queues_lock);
491 spin_unlock_bh(&ap_queues_lock);
496 * ap_poll_thread(): Thread that polls for finished requests.
497 * @data: Unused pointer
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
505 static int ap_poll_thread(void *data)
507 DECLARE_WAITQUEUE(wait, current);
509 set_user_nice(current, MAX_NICE);
511 while (!kthread_should_stop()) {
512 add_wait_queue(&ap_poll_wait, &wait);
513 set_current_state(TASK_INTERRUPTIBLE);
514 if (!ap_pending_requests()) {
518 set_current_state(TASK_RUNNING);
519 remove_wait_queue(&ap_poll_wait, &wait);
520 if (need_resched()) {
531 static int ap_poll_thread_start(void)
535 if (ap_irq_flag || ap_poll_kthread)
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);
541 ap_poll_kthread = NULL;
542 mutex_unlock(&ap_poll_thread_mutex);
546 static void ap_poll_thread_stop(void)
548 if (!ap_poll_kthread)
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);
556 #define is_card_dev(x) ((x)->parent == ap_root_device)
557 #define is_queue_dev(x) ((x)->parent != ap_root_device)
561 * @dev: Pointer to device
562 * @drv: Pointer to device_driver
564 * AP bus driver registration/unregistration.
566 static int ap_bus_match(struct device *dev, struct device_driver *drv)
568 struct ap_driver *ap_drv = to_ap_drv(drv);
569 struct ap_device_id *id;
572 * Compare device type of the device with the list of
573 * supported types of the device_driver.
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)
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)
589 * ap_uevent(): Uevent function for AP devices.
590 * @dev: Pointer to device
591 * @env: Pointer to kobj_uevent_env
593 * It sets up a single environment variable DEV_TYPE which contains the
594 * hardware device type.
596 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
599 struct ap_device *ap_dev = to_ap_dev(dev);
601 /* Uevents from ap bus core don't need extensions to the env */
602 if (dev == ap_root_device)
605 if (is_card_dev(dev)) {
606 struct ap_card *ac = to_ap_card(&ap_dev->device);
608 /* Set up DEV_TYPE environment variable. */
609 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
613 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
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");
627 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
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");
643 static void ap_send_init_scan_done_uevent(void)
645 char *envp[] = { "INITSCAN=done", NULL };
647 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
650 static void ap_send_bindings_complete_uevent(void)
653 char *envp[] = { "BINDINGS=complete", buf, NULL };
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);
660 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
663 char *envp[] = { buf, NULL };
665 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
667 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
669 EXPORT_SYMBOL(ap_send_config_uevent);
671 void ap_send_online_uevent(struct ap_device *ap_dev, int online)
674 char *envp[] = { buf, NULL };
676 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
678 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
680 EXPORT_SYMBOL(ap_send_online_uevent);
683 * calc # of bound APQNs
686 struct __ap_calc_ctrs {
691 static int __ap_calc_helper(struct device *dev, void *arg)
693 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *) arg;
695 if (is_queue_dev(dev)) {
704 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
706 struct __ap_calc_ctrs ctrs;
708 memset(&ctrs, 0, sizeof(ctrs));
709 bus_for_each_dev(&ap_bus_type, NULL, (void *) &ctrs, __ap_calc_helper);
716 * After initial ap bus scan do check if all existing APQNs are
717 * bound to device drivers.
719 static void ap_check_bindings_complete(void)
721 unsigned int apqns, bound;
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__);
730 ap_send_bindings_complete_uevent();
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.
745 int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
749 if (completion_done(&ap_init_apqn_bindings_complete))
753 l = wait_for_completion_interruptible_timeout(
754 &ap_init_apqn_bindings_complete, timeout);
756 l = wait_for_completion_interruptible(
757 &ap_init_apqn_bindings_complete);
759 return l == -ERESTARTSYS ? -EINTR : l;
760 else if (l == 0 && timeout)
765 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
767 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
769 if (is_queue_dev(dev) &&
770 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
771 device_unregister(dev);
775 static int __ap_revise_reserved(struct device *dev, void *dummy)
777 int rc, card, queue, devres, drvres;
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",
791 rc = device_reprobe(dev);
798 static void ap_bus_revise_bindings(void)
800 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
803 int ap_owned_by_def_drv(int card, int queue)
807 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
810 mutex_lock(&ap_perms_mutex);
812 if (test_bit_inv(card, ap_perms.apm)
813 && test_bit_inv(queue, ap_perms.aqm))
816 mutex_unlock(&ap_perms_mutex);
820 EXPORT_SYMBOL(ap_owned_by_def_drv);
822 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
825 int card, queue, rc = 0;
827 mutex_lock(&ap_perms_mutex);
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))
837 mutex_unlock(&ap_perms_mutex);
841 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
843 static int ap_device_probe(struct device *dev)
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;
849 if (!get_device(dev))
852 if (is_queue_dev(dev)) {
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.
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)
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);
877 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
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);
885 ap_check_bindings_complete();
893 static void ap_device_remove(struct device *dev)
895 struct ap_device *ap_dev = to_ap_dev(dev);
896 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
898 /* prepare ap queue device removal */
899 if (is_queue_dev(dev))
900 ap_queue_prepare_remove(to_ap_queue(dev));
902 /* driver's chance to clean up gracefully */
904 ap_drv->remove(ap_dev);
906 /* now do the ap queue device remove */
907 if (is_queue_dev(dev))
908 ap_queue_remove(to_ap_queue(dev));
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);
919 struct ap_queue *ap_get_qdev(ap_qid_t qid)
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);
932 spin_unlock_bh(&ap_queues_lock);
936 EXPORT_SYMBOL(ap_get_qdev);
938 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
941 struct device_driver *drv = &ap_drv->driver;
943 drv->bus = &ap_bus_type;
946 return driver_register(drv);
948 EXPORT_SYMBOL(ap_driver_register);
950 void ap_driver_unregister(struct ap_driver *ap_drv)
952 driver_unregister(&ap_drv->driver);
954 EXPORT_SYMBOL(ap_driver_unregister);
956 void ap_bus_force_rescan(void)
958 /* processing a asynchronous bus rescan */
959 del_timer(&ap_config_timer);
960 queue_work(system_long_wq, &ap_scan_work);
961 flush_work(&ap_scan_work);
963 EXPORT_SYMBOL(ap_bus_force_rescan);
966 * A config change has happened, force an ap bus rescan.
968 void ap_bus_cfg_chg(void)
970 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
972 ap_bus_force_rescan();
976 * hex2bitmap() - parse hex mask string and set bitmap.
977 * Valid strings are "0x012345678" with at least one valid hex number.
978 * Rest of the bitmap to the right is padded with 0. No spaces allowed
979 * within the string, the leading 0x may be omitted.
980 * Returns the bitmask with exactly the bits set as given by the hex
981 * string (both in big endian order).
983 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
987 /* bits needs to be a multiple of 8 */
991 if (str[0] == '0' && str[1] == 'x')
996 for (i = 0; isxdigit(*str) && i < bits; str++) {
997 b = hex_to_bin(*str);
998 for (n = 0; n < 4; n++)
1000 set_bit_inv(i + n, bitmap);
1012 * modify_bitmap() - parse bitmask argument and modify an existing
1013 * bit mask accordingly. A concatenation (done with ',') of these
1014 * terms is recognized:
1015 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1016 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1017 * 0...bits-1; the leading + or - is required. Here are some examples:
1018 * +0-15,+32,-128,-0xFF
1019 * -0-255,+1-16,+0x128
1020 * +1,+2,+3,+4,-5,-7-10
1021 * Returns the new bitmap after all changes have been applied. Every
1022 * positive value in the string will set a bit and every negative value
1023 * in the string will clear a bit. As a bit may be touched more than once,
1024 * the last 'operation' wins:
1025 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1026 * cleared again. All other bits are unmodified.
1028 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1033 /* bits needs to be a multiple of 8 */
1039 if (sign != '+' && sign != '-')
1041 a = z = simple_strtoul(str, &np, 0);
1042 if (str == np || a >= bits)
1046 z = simple_strtoul(++str, &np, 0);
1047 if (str == np || a > z || z >= bits)
1051 for (i = a; i <= z; i++)
1053 set_bit_inv(i, bitmap);
1055 clear_bit_inv(i, bitmap);
1056 while (*str == ',' || *str == '\n')
1063 int ap_parse_mask_str(const char *str,
1064 unsigned long *bitmap, int bits,
1067 unsigned long *newmap, size;
1070 /* bits needs to be a multiple of 8 */
1074 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
1075 newmap = kmalloc(size, GFP_KERNEL);
1078 if (mutex_lock_interruptible(lock)) {
1080 return -ERESTARTSYS;
1083 if (*str == '+' || *str == '-') {
1084 memcpy(newmap, bitmap, size);
1085 rc = modify_bitmap(str, newmap, bits);
1087 memset(newmap, 0, size);
1088 rc = hex2bitmap(str, newmap, bits);
1091 memcpy(bitmap, newmap, size);
1096 EXPORT_SYMBOL(ap_parse_mask_str);
1099 * AP bus attributes.
1102 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1104 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1107 static ssize_t ap_domain_store(struct bus_type *bus,
1108 const char *buf, size_t count)
1112 if (sscanf(buf, "%i\n", &domain) != 1 ||
1113 domain < 0 || domain > ap_max_domain_id ||
1114 !test_bit_inv(domain, ap_perms.aqm))
1117 spin_lock_bh(&ap_domain_lock);
1118 ap_domain_index = domain;
1119 spin_unlock_bh(&ap_domain_lock);
1121 AP_DBF_INFO("stored new default domain=%d\n", domain);
1126 static BUS_ATTR_RW(ap_domain);
1128 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1130 if (!ap_qci_info) /* QCI not supported */
1131 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1133 return scnprintf(buf, PAGE_SIZE,
1134 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1135 ap_qci_info->adm[0], ap_qci_info->adm[1],
1136 ap_qci_info->adm[2], ap_qci_info->adm[3],
1137 ap_qci_info->adm[4], ap_qci_info->adm[5],
1138 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1141 static BUS_ATTR_RO(ap_control_domain_mask);
1143 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1145 if (!ap_qci_info) /* QCI not supported */
1146 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1148 return scnprintf(buf, PAGE_SIZE,
1149 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1150 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1151 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1152 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1153 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1156 static BUS_ATTR_RO(ap_usage_domain_mask);
1158 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1160 if (!ap_qci_info) /* QCI not supported */
1161 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1163 return scnprintf(buf, PAGE_SIZE,
1164 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1165 ap_qci_info->apm[0], ap_qci_info->apm[1],
1166 ap_qci_info->apm[2], ap_qci_info->apm[3],
1167 ap_qci_info->apm[4], ap_qci_info->apm[5],
1168 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1171 static BUS_ATTR_RO(ap_adapter_mask);
1173 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1175 return scnprintf(buf, PAGE_SIZE, "%d\n",
1176 ap_irq_flag ? 1 : 0);
1179 static BUS_ATTR_RO(ap_interrupts);
1181 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1183 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1186 static ssize_t config_time_store(struct bus_type *bus,
1187 const char *buf, size_t count)
1191 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1193 ap_config_time = time;
1194 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1198 static BUS_ATTR_RW(config_time);
1200 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1202 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1205 static ssize_t poll_thread_store(struct bus_type *bus,
1206 const char *buf, size_t count)
1210 if (sscanf(buf, "%d\n", &flag) != 1)
1213 rc = ap_poll_thread_start();
1217 ap_poll_thread_stop();
1221 static BUS_ATTR_RW(poll_thread);
1223 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1225 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1228 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1231 unsigned long long time;
1234 /* 120 seconds = maximum poll interval */
1235 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1236 time > 120000000000ULL)
1238 poll_timeout = time;
1239 hr_time = poll_timeout;
1241 spin_lock_bh(&ap_poll_timer_lock);
1242 hrtimer_cancel(&ap_poll_timer);
1243 hrtimer_set_expires(&ap_poll_timer, hr_time);
1244 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1245 spin_unlock_bh(&ap_poll_timer_lock);
1250 static BUS_ATTR_RW(poll_timeout);
1252 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1254 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1257 static BUS_ATTR_RO(ap_max_domain_id);
1259 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1261 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1264 static BUS_ATTR_RO(ap_max_adapter_id);
1266 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1270 if (mutex_lock_interruptible(&ap_perms_mutex))
1271 return -ERESTARTSYS;
1272 rc = scnprintf(buf, PAGE_SIZE,
1273 "0x%016lx%016lx%016lx%016lx\n",
1274 ap_perms.apm[0], ap_perms.apm[1],
1275 ap_perms.apm[2], ap_perms.apm[3]);
1276 mutex_unlock(&ap_perms_mutex);
1281 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1286 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1290 ap_bus_revise_bindings();
1295 static BUS_ATTR_RW(apmask);
1297 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1301 if (mutex_lock_interruptible(&ap_perms_mutex))
1302 return -ERESTARTSYS;
1303 rc = scnprintf(buf, PAGE_SIZE,
1304 "0x%016lx%016lx%016lx%016lx\n",
1305 ap_perms.aqm[0], ap_perms.aqm[1],
1306 ap_perms.aqm[2], ap_perms.aqm[3]);
1307 mutex_unlock(&ap_perms_mutex);
1312 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1317 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1321 ap_bus_revise_bindings();
1326 static BUS_ATTR_RW(aqmask);
1328 static ssize_t scans_show(struct bus_type *bus, char *buf)
1330 return scnprintf(buf, PAGE_SIZE, "%llu\n",
1331 atomic64_read(&ap_scan_bus_count));
1334 static BUS_ATTR_RO(scans);
1336 static ssize_t bindings_show(struct bus_type *bus, char *buf)
1339 unsigned int apqns, n;
1341 ap_calc_bound_apqns(&apqns, &n);
1342 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1343 rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns);
1345 rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns);
1350 static BUS_ATTR_RO(bindings);
1352 static struct attribute *ap_bus_attrs[] = {
1353 &bus_attr_ap_domain.attr,
1354 &bus_attr_ap_control_domain_mask.attr,
1355 &bus_attr_ap_usage_domain_mask.attr,
1356 &bus_attr_ap_adapter_mask.attr,
1357 &bus_attr_config_time.attr,
1358 &bus_attr_poll_thread.attr,
1359 &bus_attr_ap_interrupts.attr,
1360 &bus_attr_poll_timeout.attr,
1361 &bus_attr_ap_max_domain_id.attr,
1362 &bus_attr_ap_max_adapter_id.attr,
1363 &bus_attr_apmask.attr,
1364 &bus_attr_aqmask.attr,
1365 &bus_attr_scans.attr,
1366 &bus_attr_bindings.attr,
1369 ATTRIBUTE_GROUPS(ap_bus);
1371 static struct bus_type ap_bus_type = {
1373 .bus_groups = ap_bus_groups,
1374 .match = &ap_bus_match,
1375 .uevent = &ap_uevent,
1376 .probe = ap_device_probe,
1377 .remove = ap_device_remove,
1381 * ap_select_domain(): Select an AP domain if possible and we haven't
1382 * already done so before.
1384 static void ap_select_domain(void)
1386 struct ap_queue_status status;
1390 * Choose the default domain. Either the one specified with
1391 * the "domain=" parameter or the first domain with at least
1394 spin_lock_bh(&ap_domain_lock);
1395 if (ap_domain_index >= 0) {
1396 /* Domain has already been selected. */
1399 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1400 if (!ap_test_config_usage_domain(dom) ||
1401 !test_bit_inv(dom, ap_perms.aqm))
1403 for (card = 0; card <= ap_max_adapter_id; card++) {
1404 if (!ap_test_config_card_id(card) ||
1405 !test_bit_inv(card, ap_perms.apm))
1407 status = ap_test_queue(AP_MKQID(card, dom),
1408 ap_apft_available(),
1410 if (status.response_code == AP_RESPONSE_NORMAL)
1413 if (card <= ap_max_adapter_id)
1416 if (dom <= ap_max_domain_id) {
1417 ap_domain_index = dom;
1418 AP_DBF_INFO("%s new default domain is %d\n",
1419 __func__, ap_domain_index);
1422 spin_unlock_bh(&ap_domain_lock);
1426 * This function checks the type and returns either 0 for not
1427 * supported or the highest compatible type value (which may
1428 * include the input type value).
1430 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1434 /* < CEX2A is not supported */
1435 if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1436 AP_DBF_WARN("get_comp_type queue=%02x.%04x unsupported type %d\n",
1437 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1440 /* up to CEX7 known and fully supported */
1441 if (rawtype <= AP_DEVICE_TYPE_CEX7)
1444 * unknown new type > CEX7, check for compatibility
1445 * to the highest known and supported type which is
1446 * currently CEX7 with the help of the QACT function.
1448 if (ap_qact_available()) {
1449 struct ap_queue_status status;
1450 union ap_qact_ap_info apinfo = {0};
1452 apinfo.mode = (func >> 26) & 0x07;
1453 apinfo.cat = AP_DEVICE_TYPE_CEX7;
1454 status = ap_qact(qid, 0, &apinfo);
1455 if (status.response_code == AP_RESPONSE_NORMAL
1456 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1457 && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
1458 comp_type = apinfo.cat;
1461 AP_DBF_WARN("get_comp_type queue=%02x.%04x unable to map type %d\n",
1462 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1463 else if (comp_type != rawtype)
1464 AP_DBF_INFO("get_comp_type queue=%02x.%04x map type %d to %d\n",
1465 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1466 rawtype, comp_type);
1471 * Helper function to be used with bus_find_dev
1472 * matches for the card device with the given id
1474 static int __match_card_device_with_id(struct device *dev, const void *data)
1476 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1480 * Helper function to be used with bus_find_dev
1481 * matches for the queue device with a given qid
1483 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1485 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1489 * Helper function to be used with bus_find_dev
1490 * matches any queue device with given queue id
1492 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1494 return is_queue_dev(dev)
1495 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1499 * Helper function for ap_scan_bus().
1500 * Remove card device and associated queue devices.
1502 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1504 bus_for_each_dev(&ap_bus_type, NULL,
1505 (void *)(long) ac->id,
1506 __ap_queue_devices_with_id_unregister);
1507 device_unregister(&ac->ap_dev.device);
1511 * Helper function for ap_scan_bus().
1512 * Does the scan bus job for all the domains within
1513 * a valid adapter given by an ap_card ptr.
1515 static inline void ap_scan_domains(struct ap_card *ac)
1521 struct ap_queue *aq;
1522 int rc, dom, depth, type, ml;
1525 * Go through the configuration for the domains and compare them
1526 * to the existing queue devices. Also take care of the config
1527 * and error state for the queue devices.
1530 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1531 qid = AP_MKQID(ac->id, dom);
1532 dev = bus_find_device(&ap_bus_type, NULL,
1534 __match_queue_device_with_qid);
1535 aq = dev ? to_ap_queue(dev) : NULL;
1536 if (!ap_test_config_usage_domain(dom)) {
1538 AP_DBF_INFO("%s(%d,%d) not in config any more, rm queue device\n",
1539 __func__, ac->id, dom);
1540 device_unregister(dev);
1545 /* domain is valid, get info from this APQN */
1546 if (!ap_queue_info(qid, &type, &func, &depth, &ml, &decfg)) {
1549 "%s(%d,%d) ap_queue_info() not successful, rm queue device\n",
1550 __func__, ac->id, dom);
1551 device_unregister(dev);
1556 /* if no queue device exists, create a new one */
1558 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1560 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1561 __func__, ac->id, dom);
1565 aq->config = !decfg;
1566 dev = &aq->ap_dev.device;
1567 dev->bus = &ap_bus_type;
1568 dev->parent = &ac->ap_dev.device;
1569 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1570 /* register queue device */
1571 rc = device_register(dev);
1573 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1574 __func__, ac->id, dom);
1575 goto put_dev_and_continue;
1577 /* get it and thus adjust reference counter */
1580 AP_DBF_INFO("%s(%d,%d) new (decfg) queue device created\n",
1581 __func__, ac->id, dom);
1583 AP_DBF_INFO("%s(%d,%d) new queue device created\n",
1584 __func__, ac->id, dom);
1585 goto put_dev_and_continue;
1587 /* Check config state on the already existing queue device */
1588 spin_lock_bh(&aq->lock);
1589 if (decfg && aq->config) {
1590 /* config off this queue device */
1592 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1593 aq->dev_state = AP_DEV_STATE_ERROR;
1594 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1596 spin_unlock_bh(&aq->lock);
1597 AP_DBF_INFO("%s(%d,%d) queue device config off\n",
1598 __func__, ac->id, dom);
1599 ap_send_config_uevent(&aq->ap_dev, aq->config);
1600 /* 'receive' pending messages with -EAGAIN */
1602 goto put_dev_and_continue;
1604 if (!decfg && !aq->config) {
1605 /* config on this queue device */
1607 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1608 aq->dev_state = AP_DEV_STATE_OPERATING;
1609 aq->sm_state = AP_SM_STATE_RESET_START;
1611 spin_unlock_bh(&aq->lock);
1612 AP_DBF_INFO("%s(%d,%d) queue device config on\n",
1613 __func__, ac->id, dom);
1614 ap_send_config_uevent(&aq->ap_dev, aq->config);
1615 goto put_dev_and_continue;
1617 /* handle other error states */
1618 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1619 spin_unlock_bh(&aq->lock);
1620 /* 'receive' pending messages with -EAGAIN */
1622 /* re-init (with reset) the queue device */
1623 ap_queue_init_state(aq);
1624 AP_DBF_INFO("%s(%d,%d) queue device reinit enforced\n",
1625 __func__, ac->id, dom);
1626 goto put_dev_and_continue;
1628 spin_unlock_bh(&aq->lock);
1629 put_dev_and_continue:
1635 * Helper function for ap_scan_bus().
1636 * Does the scan bus job for the given adapter id.
1638 static inline void ap_scan_adapter(int ap)
1645 int rc, dom, depth, type, comp_type, ml;
1647 /* Is there currently a card device for this adapter ? */
1648 dev = bus_find_device(&ap_bus_type, NULL,
1650 __match_card_device_with_id);
1651 ac = dev ? to_ap_card(dev) : NULL;
1653 /* Adapter not in configuration ? */
1654 if (!ap_test_config_card_id(ap)) {
1656 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devices\n",
1658 ap_scan_rm_card_dev_and_queue_devs(ac);
1665 * Adapter ap is valid in the current configuration. So do some checks:
1666 * If no card device exists, build one. If a card device exists, check
1667 * for type and functions changed. For all this we need to find a valid
1671 for (dom = 0; dom <= ap_max_domain_id; dom++)
1672 if (ap_test_config_usage_domain(dom)) {
1673 qid = AP_MKQID(ap, dom);
1674 if (ap_queue_info(qid, &type, &func,
1675 &depth, &ml, &decfg))
1678 if (dom > ap_max_domain_id) {
1679 /* Could not find a valid APQN for this adapter */
1682 "%s(%d) no type info (no APQN found), rm card and queue devices\n",
1684 ap_scan_rm_card_dev_and_queue_devs(ac);
1687 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1693 /* No apdater type info available, an unusable adapter */
1695 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devices\n",
1697 ap_scan_rm_card_dev_and_queue_devs(ac);
1700 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1707 /* Check APQN against existing card device for changes */
1708 if (ac->raw_hwtype != type) {
1709 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devices\n",
1710 __func__, ap, type);
1711 ap_scan_rm_card_dev_and_queue_devs(ac);
1714 } else if (ac->functions != func) {
1715 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devices\n",
1716 __func__, ap, type);
1717 ap_scan_rm_card_dev_and_queue_devs(ac);
1721 if (decfg && ac->config) {
1723 AP_DBF_INFO("%s(%d) card device config off\n",
1725 ap_send_config_uevent(&ac->ap_dev, ac->config);
1727 if (!decfg && !ac->config) {
1729 AP_DBF_INFO("%s(%d) card device config on\n",
1731 ap_send_config_uevent(&ac->ap_dev, ac->config);
1737 /* Build a new card device */
1738 comp_type = ap_get_compatible_type(qid, type, func);
1740 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
1741 __func__, ap, type);
1744 ac = ap_card_create(ap, depth, type, comp_type, func, ml);
1746 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
1750 ac->config = !decfg;
1751 dev = &ac->ap_dev.device;
1752 dev->bus = &ap_bus_type;
1753 dev->parent = ap_root_device;
1754 dev_set_name(dev, "card%02x", ap);
1755 /* maybe enlarge ap_max_msg_size to support this card */
1756 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
1757 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
1758 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
1759 __func__, ap, atomic_read(&ap_max_msg_size));
1761 /* Register the new card device with AP bus */
1762 rc = device_register(dev);
1764 AP_DBF_WARN("%s(%d) device_register() failed\n",
1769 /* get it and thus adjust reference counter */
1772 AP_DBF_INFO("%s(%d) new (decfg) card device type=%d func=0x%08x created\n",
1773 __func__, ap, type, func);
1775 AP_DBF_INFO("%s(%d) new card device type=%d func=0x%08x created\n",
1776 __func__, ap, type, func);
1779 /* Verify the domains and the queue devices for this card */
1780 ap_scan_domains(ac);
1782 /* release the card device */
1783 put_device(&ac->ap_dev.device);
1787 * ap_scan_bus(): Scan the AP bus for new devices
1788 * Runs periodically, workqueue timer (ap_config_time)
1790 static void ap_scan_bus(struct work_struct *unused)
1794 ap_fetch_qci_info(ap_qci_info);
1797 AP_DBF_DBG("%s running\n", __func__);
1799 /* loop over all possible adapters */
1800 for (ap = 0; ap <= ap_max_adapter_id; ap++)
1801 ap_scan_adapter(ap);
1803 /* check if there is at least one queue available with default domain */
1804 if (ap_domain_index >= 0) {
1805 struct device *dev =
1806 bus_find_device(&ap_bus_type, NULL,
1807 (void *)(long) ap_domain_index,
1808 __match_queue_device_with_queue_id);
1812 AP_DBF_INFO("no queue device with default domain %d available\n",
1816 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
1817 AP_DBF(DBF_DEBUG, "%s init scan complete\n", __func__);
1818 ap_send_init_scan_done_uevent();
1819 ap_check_bindings_complete();
1822 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1825 static void ap_config_timeout(struct timer_list *unused)
1827 queue_work(system_long_wq, &ap_scan_work);
1830 static int __init ap_debug_init(void)
1832 ap_dbf_info = debug_register("ap", 1, 1,
1833 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1834 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1835 debug_set_level(ap_dbf_info, DBF_ERR);
1840 static void __init ap_perms_init(void)
1842 /* all resources useable if no kernel parameter string given */
1843 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1844 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1845 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1847 /* apm kernel parameter string */
1849 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1850 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1854 /* aqm kernel parameter string */
1856 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1857 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1863 * ap_module_init(): The module initialization code.
1865 * Initializes the module.
1867 static int __init ap_module_init(void)
1871 rc = ap_debug_init();
1875 if (!ap_instructions_available()) {
1876 pr_warn("The hardware system does not support AP instructions\n");
1880 /* init ap_queue hashtable */
1881 hash_init(ap_queues);
1883 /* set up the AP permissions (ioctls, ap and aq masks) */
1886 /* Get AP configuration data if available */
1889 /* check default domain setting */
1890 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
1891 (ap_domain_index >= 0 &&
1892 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1893 pr_warn("%d is not a valid cryptographic domain\n",
1895 ap_domain_index = -1;
1898 /* enable interrupts if available */
1899 if (ap_interrupts_available()) {
1900 rc = register_adapter_interrupt(&ap_airq);
1901 ap_irq_flag = (rc == 0);
1904 /* Create /sys/bus/ap. */
1905 rc = bus_register(&ap_bus_type);
1909 /* Create /sys/devices/ap. */
1910 ap_root_device = root_device_register("ap");
1911 rc = PTR_ERR_OR_ZERO(ap_root_device);
1914 ap_root_device->bus = &ap_bus_type;
1916 /* Setup the AP bus rescan timer. */
1917 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1920 * Setup the high resultion poll timer.
1921 * If we are running under z/VM adjust polling to z/VM polling rate.
1924 poll_timeout = 1500000;
1925 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1926 ap_poll_timer.function = ap_poll_timeout;
1928 /* Start the low priority AP bus poll thread. */
1929 if (ap_thread_flag) {
1930 rc = ap_poll_thread_start();
1935 queue_work(system_long_wq, &ap_scan_work);
1940 hrtimer_cancel(&ap_poll_timer);
1941 root_device_unregister(ap_root_device);
1943 bus_unregister(&ap_bus_type);
1946 unregister_adapter_interrupt(&ap_airq);
1950 device_initcall(ap_module_init);