1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2006, 2020
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 static struct device *ap_root_device;
66 /* Hashtable of all queue devices on the AP bus */
67 DEFINE_HASHTABLE(ap_queues, 8);
68 /* lock used for the ap_queues hashtable */
69 DEFINE_SPINLOCK(ap_queues_lock);
71 /* Default permissions (ioctl, card and domain masking) */
72 struct ap_perms ap_perms;
73 EXPORT_SYMBOL(ap_perms);
74 DEFINE_MUTEX(ap_perms_mutex);
75 EXPORT_SYMBOL(ap_perms_mutex);
77 /* # of bus scans since init */
78 static atomic64_t ap_scan_bus_count;
80 /* completion for initial APQN bindings complete */
81 static DECLARE_COMPLETION(ap_init_apqn_bindings_complete);
83 static struct ap_config_info *ap_qci_info;
86 * AP bus related debug feature things.
88 debug_info_t *ap_dbf_info;
91 * Workqueue timer for bus rescan.
93 static struct timer_list ap_config_timer;
94 static int ap_config_time = AP_CONFIG_TIME;
95 static void ap_scan_bus(struct work_struct *);
96 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
99 * Tasklet & timer for AP request polling and interrupts
101 static void ap_tasklet_fn(unsigned long);
102 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
103 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
104 static struct task_struct *ap_poll_kthread;
105 static DEFINE_MUTEX(ap_poll_thread_mutex);
106 static DEFINE_SPINLOCK(ap_poll_timer_lock);
107 static struct hrtimer ap_poll_timer;
109 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
110 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
112 static unsigned long long poll_timeout = 250000;
114 /* Maximum domain id, if not given via qci */
115 static int ap_max_domain_id = 15;
116 /* Maximum adapter id, if not given via qci */
117 static int ap_max_adapter_id = 63;
119 static struct bus_type ap_bus_type;
121 /* Adapter interrupt definitions */
122 static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
124 static int ap_airq_flag;
126 static struct airq_struct ap_airq = {
127 .handler = ap_interrupt_handler,
132 * ap_using_interrupts() - Returns non-zero if interrupt support is
135 static inline int ap_using_interrupts(void)
141 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
143 * Returns the address of the local-summary-indicator of the adapter
144 * interrupt handler for AP, or NULL if adapter interrupts are not
147 void *ap_airq_ptr(void)
149 if (ap_using_interrupts())
150 return ap_airq.lsi_ptr;
155 * ap_interrupts_available(): Test if AP interrupts are available.
157 * Returns 1 if AP interrupts are available.
159 static int ap_interrupts_available(void)
161 return test_facility(65);
165 * ap_qci_available(): Test if AP configuration
166 * information can be queried via QCI subfunction.
168 * Returns 1 if subfunction PQAP(QCI) is available.
170 static int ap_qci_available(void)
172 return test_facility(12);
176 * ap_apft_available(): Test if AP facilities test (APFT)
177 * facility is available.
179 * Returns 1 if APFT is is available.
181 static int ap_apft_available(void)
183 return test_facility(15);
187 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
189 * Returns 1 if the QACT subfunction is available.
191 static inline int ap_qact_available(void)
194 return ap_qci_info->qact;
199 * ap_fetch_qci_info(): Fetch cryptographic config info
201 * Returns the ap configuration info fetched via PQAP(QCI).
202 * On success 0 is returned, on failure a negative errno
203 * is returned, e.g. if the PQAP(QCI) instruction is not
204 * available, the return value will be -EOPNOTSUPP.
206 static inline int ap_fetch_qci_info(struct ap_config_info *info)
208 if (!ap_qci_available())
216 * ap_init_qci_info(): Allocate and query qci config info.
217 * Does also update the static variables ap_max_domain_id
218 * and ap_max_adapter_id if this info is available.
221 static void __init ap_init_qci_info(void)
223 if (!ap_qci_available()) {
224 AP_DBF_INFO("%s QCI not supported\n", __func__);
228 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
231 if (ap_fetch_qci_info(ap_qci_info) != 0) {
236 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
238 if (ap_qci_info->apxa) {
239 if (ap_qci_info->Na) {
240 ap_max_adapter_id = ap_qci_info->Na;
241 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
242 __func__, ap_max_adapter_id);
244 if (ap_qci_info->Nd) {
245 ap_max_domain_id = ap_qci_info->Nd;
246 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
247 __func__, ap_max_domain_id);
253 * ap_test_config(): helper function to extract the nrth bit
254 * within the unsigned int array field.
256 static inline int ap_test_config(unsigned int *field, unsigned int nr)
258 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
262 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
264 * Returns 0 if the card is not configured
265 * 1 if the card is configured or
266 * if the configuration information is not available
268 static inline int ap_test_config_card_id(unsigned int id)
270 if (id > ap_max_adapter_id)
273 return ap_test_config(ap_qci_info->apm, id);
278 * ap_test_config_usage_domain(): Test, whether an AP usage domain
281 * Returns 0 if the usage domain is not configured
282 * 1 if the usage domain is configured or
283 * if the configuration information is not available
285 int ap_test_config_usage_domain(unsigned int domain)
287 if (domain > ap_max_domain_id)
290 return ap_test_config(ap_qci_info->aqm, domain);
293 EXPORT_SYMBOL(ap_test_config_usage_domain);
296 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
298 * @domain AP control domain ID
300 * Returns 1 if the control domain is configured
301 * 0 in all other cases
303 int ap_test_config_ctrl_domain(unsigned int domain)
305 if (!ap_qci_info || domain > ap_max_domain_id)
307 return ap_test_config(ap_qci_info->adm, domain);
309 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
312 * ap_queue_info(): Check and get AP queue info.
313 * Returns true if TAPQ succeeded and the info is filled or
316 static bool ap_queue_info(ap_qid_t qid, int *q_type,
317 unsigned int *q_fac, int *q_depth, bool *q_decfg)
319 struct ap_queue_status status;
320 unsigned long info = 0;
322 /* make sure we don't run into a specifiation exception */
323 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
324 AP_QID_QUEUE(qid) > ap_max_domain_id)
327 /* call TAPQ on this APQN */
328 status = ap_test_queue(qid, ap_apft_available(), &info);
329 switch (status.response_code) {
330 case AP_RESPONSE_NORMAL:
331 case AP_RESPONSE_RESET_IN_PROGRESS:
332 case AP_RESPONSE_DECONFIGURED:
333 case AP_RESPONSE_CHECKSTOPPED:
334 case AP_RESPONSE_BUSY:
336 * According to the architecture in all these cases the
337 * info should be filled. All bits 0 is not possible as
338 * there is at least one of the mode bits set.
340 if (WARN_ON_ONCE(!info))
342 *q_type = (int)((info >> 24) & 0xff);
343 *q_fac = (unsigned int)(info >> 32);
344 *q_depth = (int)(info & 0xff);
345 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
347 /* For CEX2 and CEX3 the available functions
348 * are not reflected by the facilities bits.
349 * Instead it is coded into the type. So here
350 * modify the function bits based on the type.
352 case AP_DEVICE_TYPE_CEX2A:
353 case AP_DEVICE_TYPE_CEX3A:
354 *q_fac |= 0x08000000;
356 case AP_DEVICE_TYPE_CEX2C:
357 case AP_DEVICE_TYPE_CEX3C:
358 *q_fac |= 0x10000000;
366 * A response code which indicates, there is no info available.
372 void ap_wait(enum ap_sm_wait wait)
377 case AP_SM_WAIT_AGAIN:
378 case AP_SM_WAIT_INTERRUPT:
379 if (ap_using_interrupts())
381 if (ap_poll_kthread) {
382 wake_up(&ap_poll_wait);
386 case AP_SM_WAIT_TIMEOUT:
387 spin_lock_bh(&ap_poll_timer_lock);
388 if (!hrtimer_is_queued(&ap_poll_timer)) {
389 hr_time = poll_timeout;
390 hrtimer_forward_now(&ap_poll_timer, hr_time);
391 hrtimer_restart(&ap_poll_timer);
393 spin_unlock_bh(&ap_poll_timer_lock);
395 case AP_SM_WAIT_NONE:
402 * ap_request_timeout(): Handling of request timeouts
403 * @t: timer making this callback
405 * Handles request timeouts.
407 void ap_request_timeout(struct timer_list *t)
409 struct ap_queue *aq = from_timer(aq, t, timeout);
411 spin_lock_bh(&aq->lock);
412 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
413 spin_unlock_bh(&aq->lock);
417 * ap_poll_timeout(): AP receive polling for finished AP requests.
418 * @unused: Unused pointer.
420 * Schedules the AP tasklet using a high resolution timer.
422 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
424 tasklet_schedule(&ap_tasklet);
425 return HRTIMER_NORESTART;
429 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
430 * @airq: pointer to adapter interrupt descriptor
432 static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
434 inc_irq_stat(IRQIO_APB);
435 tasklet_schedule(&ap_tasklet);
439 * ap_tasklet_fn(): Tasklet to poll all AP devices.
440 * @dummy: Unused variable
442 * Poll all AP devices on the bus.
444 static void ap_tasklet_fn(unsigned long dummy)
448 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
450 /* Reset the indicator if interrupts are used. Thus new interrupts can
451 * be received. Doing it in the beginning of the tasklet is therefor
452 * important that no requests on any AP get lost.
454 if (ap_using_interrupts())
455 xchg(ap_airq.lsi_ptr, 0);
457 spin_lock_bh(&ap_queues_lock);
458 hash_for_each(ap_queues, bkt, aq, hnode) {
459 spin_lock_bh(&aq->lock);
460 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
461 spin_unlock_bh(&aq->lock);
463 spin_unlock_bh(&ap_queues_lock);
468 static int ap_pending_requests(void)
473 spin_lock_bh(&ap_queues_lock);
474 hash_for_each(ap_queues, bkt, aq, hnode) {
475 if (aq->queue_count == 0)
477 spin_unlock_bh(&ap_queues_lock);
480 spin_unlock_bh(&ap_queues_lock);
485 * ap_poll_thread(): Thread that polls for finished requests.
486 * @data: Unused pointer
488 * AP bus poll thread. The purpose of this thread is to poll for
489 * finished requests in a loop if there is a "free" cpu - that is
490 * a cpu that doesn't have anything better to do. The polling stops
491 * as soon as there is another task or if all messages have been
494 static int ap_poll_thread(void *data)
496 DECLARE_WAITQUEUE(wait, current);
498 set_user_nice(current, MAX_NICE);
500 while (!kthread_should_stop()) {
501 add_wait_queue(&ap_poll_wait, &wait);
502 set_current_state(TASK_INTERRUPTIBLE);
503 if (!ap_pending_requests()) {
507 set_current_state(TASK_RUNNING);
508 remove_wait_queue(&ap_poll_wait, &wait);
509 if (need_resched()) {
520 static int ap_poll_thread_start(void)
524 if (ap_using_interrupts() || ap_poll_kthread)
526 mutex_lock(&ap_poll_thread_mutex);
527 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
528 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
530 ap_poll_kthread = NULL;
531 mutex_unlock(&ap_poll_thread_mutex);
535 static void ap_poll_thread_stop(void)
537 if (!ap_poll_kthread)
539 mutex_lock(&ap_poll_thread_mutex);
540 kthread_stop(ap_poll_kthread);
541 ap_poll_kthread = NULL;
542 mutex_unlock(&ap_poll_thread_mutex);
545 #define is_card_dev(x) ((x)->parent == ap_root_device)
546 #define is_queue_dev(x) ((x)->parent != ap_root_device)
550 * @dev: Pointer to device
551 * @drv: Pointer to device_driver
553 * AP bus driver registration/unregistration.
555 static int ap_bus_match(struct device *dev, struct device_driver *drv)
557 struct ap_driver *ap_drv = to_ap_drv(drv);
558 struct ap_device_id *id;
561 * Compare device type of the device with the list of
562 * supported types of the device_driver.
564 for (id = ap_drv->ids; id->match_flags; id++) {
565 if (is_card_dev(dev) &&
566 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
567 id->dev_type == to_ap_dev(dev)->device_type)
569 if (is_queue_dev(dev) &&
570 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
571 id->dev_type == to_ap_dev(dev)->device_type)
578 * ap_uevent(): Uevent function for AP devices.
579 * @dev: Pointer to device
580 * @env: Pointer to kobj_uevent_env
582 * It sets up a single environment variable DEV_TYPE which contains the
583 * hardware device type.
585 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
588 struct ap_device *ap_dev = to_ap_dev(dev);
590 /* Uevents from ap bus core don't need extensions to the env */
591 if (dev == ap_root_device)
594 /* Set up DEV_TYPE environment variable. */
595 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
600 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
607 static void ap_send_init_scan_done_uevent(void)
609 char *envp[] = { "INITSCAN=done", NULL };
611 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
614 static void ap_send_bindings_complete_uevent(void)
616 char *envp[] = { "BINDINGS=complete", NULL };
618 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
622 * calc # of bound APQNs
625 struct __ap_calc_ctrs {
630 static int __ap_calc_helper(struct device *dev, void *arg)
632 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *) arg;
634 if (is_queue_dev(dev)) {
636 if ((to_ap_dev(dev))->drv)
643 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
645 struct __ap_calc_ctrs ctrs;
647 memset(&ctrs, 0, sizeof(ctrs));
648 bus_for_each_dev(&ap_bus_type, NULL, (void *) &ctrs, __ap_calc_helper);
655 * After initial ap bus scan do check if all existing APQNs are
656 * bound to device drivers.
658 static void ap_check_bindings_complete(void)
660 unsigned int apqns, bound;
662 if (atomic64_read(&ap_scan_bus_count) >= 1) {
663 ap_calc_bound_apqns(&apqns, &bound);
664 if (bound == apqns) {
665 if (!completion_done(&ap_init_apqn_bindings_complete)) {
666 complete_all(&ap_init_apqn_bindings_complete);
667 AP_DBF(DBF_INFO, "%s complete\n", __func__);
669 ap_send_bindings_complete_uevent();
675 * Interface to wait for the AP bus to have done one initial ap bus
676 * scan and all detected APQNs have been bound to device drivers.
677 * If these both conditions are not fulfilled, this function blocks
678 * on a condition with wait_for_completion_interruptible_timeout().
679 * If these both conditions are fulfilled (before the timeout hits)
680 * the return value is 0. If the timeout (in jiffies) hits instead
681 * -ETIME is returned. On failures negative return values are
682 * returned to the caller.
684 int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
688 if (completion_done(&ap_init_apqn_bindings_complete))
692 l = wait_for_completion_interruptible_timeout(
693 &ap_init_apqn_bindings_complete, timeout);
695 l = wait_for_completion_interruptible(
696 &ap_init_apqn_bindings_complete);
698 return l == -ERESTARTSYS ? -EINTR : l;
699 else if (l == 0 && timeout)
704 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
706 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
708 if (is_queue_dev(dev) &&
709 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
710 device_unregister(dev);
714 static int __ap_revise_reserved(struct device *dev, void *dummy)
716 int rc, card, queue, devres, drvres;
718 if (is_queue_dev(dev)) {
719 card = AP_QID_CARD(to_ap_queue(dev)->qid);
720 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
721 mutex_lock(&ap_perms_mutex);
722 devres = test_bit_inv(card, ap_perms.apm)
723 && test_bit_inv(queue, ap_perms.aqm);
724 mutex_unlock(&ap_perms_mutex);
725 drvres = to_ap_drv(dev->driver)->flags
726 & AP_DRIVER_FLAG_DEFAULT;
727 if (!!devres != !!drvres) {
728 AP_DBF_DBG("reprobing queue=%02x.%04x\n",
730 rc = device_reprobe(dev);
737 static void ap_bus_revise_bindings(void)
739 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
742 int ap_owned_by_def_drv(int card, int queue)
746 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
749 mutex_lock(&ap_perms_mutex);
751 if (test_bit_inv(card, ap_perms.apm)
752 && test_bit_inv(queue, ap_perms.aqm))
755 mutex_unlock(&ap_perms_mutex);
759 EXPORT_SYMBOL(ap_owned_by_def_drv);
761 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
764 int card, queue, rc = 0;
766 mutex_lock(&ap_perms_mutex);
768 for (card = 0; !rc && card < AP_DEVICES; card++)
769 if (test_bit_inv(card, apm) &&
770 test_bit_inv(card, ap_perms.apm))
771 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
772 if (test_bit_inv(queue, aqm) &&
773 test_bit_inv(queue, ap_perms.aqm))
776 mutex_unlock(&ap_perms_mutex);
780 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
782 static int ap_device_probe(struct device *dev)
784 struct ap_device *ap_dev = to_ap_dev(dev);
785 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
786 int card, queue, devres, drvres, rc = -ENODEV;
788 if (!get_device(dev))
791 if (is_queue_dev(dev)) {
793 * If the apqn is marked as reserved/used by ap bus and
794 * default drivers, only probe with drivers with the default
795 * flag set. If it is not marked, only probe with drivers
796 * with the default flag not set.
798 card = AP_QID_CARD(to_ap_queue(dev)->qid);
799 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
800 mutex_lock(&ap_perms_mutex);
801 devres = test_bit_inv(card, ap_perms.apm)
802 && test_bit_inv(queue, ap_perms.aqm);
803 mutex_unlock(&ap_perms_mutex);
804 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
805 if (!!devres != !!drvres)
809 /* Add queue/card to list of active queues/cards */
810 spin_lock_bh(&ap_queues_lock);
811 if (is_queue_dev(dev))
812 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
813 to_ap_queue(dev)->qid);
814 spin_unlock_bh(&ap_queues_lock);
816 ap_dev->drv = ap_drv;
817 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
820 spin_lock_bh(&ap_queues_lock);
821 if (is_queue_dev(dev))
822 hash_del(&to_ap_queue(dev)->hnode);
823 spin_unlock_bh(&ap_queues_lock);
826 ap_check_bindings_complete();
834 static int ap_device_remove(struct device *dev)
836 struct ap_device *ap_dev = to_ap_dev(dev);
837 struct ap_driver *ap_drv = ap_dev->drv;
839 /* prepare ap queue device removal */
840 if (is_queue_dev(dev))
841 ap_queue_prepare_remove(to_ap_queue(dev));
843 /* driver's chance to clean up gracefully */
845 ap_drv->remove(ap_dev);
847 /* now do the ap queue device remove */
848 if (is_queue_dev(dev))
849 ap_queue_remove(to_ap_queue(dev));
851 /* Remove queue/card from list of active queues/cards */
852 spin_lock_bh(&ap_queues_lock);
853 if (is_queue_dev(dev))
854 hash_del(&to_ap_queue(dev)->hnode);
855 spin_unlock_bh(&ap_queues_lock);
863 struct ap_queue *ap_get_qdev(ap_qid_t qid)
868 spin_lock_bh(&ap_queues_lock);
869 hash_for_each(ap_queues, bkt, aq, hnode) {
870 if (aq->qid == qid) {
871 get_device(&aq->ap_dev.device);
872 spin_unlock_bh(&ap_queues_lock);
876 spin_unlock_bh(&ap_queues_lock);
880 EXPORT_SYMBOL(ap_get_qdev);
882 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
885 struct device_driver *drv = &ap_drv->driver;
887 drv->bus = &ap_bus_type;
888 drv->probe = ap_device_probe;
889 drv->remove = ap_device_remove;
892 return driver_register(drv);
894 EXPORT_SYMBOL(ap_driver_register);
896 void ap_driver_unregister(struct ap_driver *ap_drv)
898 driver_unregister(&ap_drv->driver);
900 EXPORT_SYMBOL(ap_driver_unregister);
902 void ap_bus_force_rescan(void)
904 /* processing a asynchronous bus rescan */
905 del_timer(&ap_config_timer);
906 queue_work(system_long_wq, &ap_scan_work);
907 flush_work(&ap_scan_work);
909 EXPORT_SYMBOL(ap_bus_force_rescan);
912 * A config change has happened, force an ap bus rescan.
914 void ap_bus_cfg_chg(void)
916 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
918 ap_bus_force_rescan();
922 * hex2bitmap() - parse hex mask string and set bitmap.
923 * Valid strings are "0x012345678" with at least one valid hex number.
924 * Rest of the bitmap to the right is padded with 0. No spaces allowed
925 * within the string, the leading 0x may be omitted.
926 * Returns the bitmask with exactly the bits set as given by the hex
927 * string (both in big endian order).
929 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
933 /* bits needs to be a multiple of 8 */
937 if (str[0] == '0' && str[1] == 'x')
942 for (i = 0; isxdigit(*str) && i < bits; str++) {
943 b = hex_to_bin(*str);
944 for (n = 0; n < 4; n++)
946 set_bit_inv(i + n, bitmap);
958 * modify_bitmap() - parse bitmask argument and modify an existing
959 * bit mask accordingly. A concatenation (done with ',') of these
960 * terms is recognized:
961 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
962 * <bitnr> may be any valid number (hex, decimal or octal) in the range
963 * 0...bits-1; the leading + or - is required. Here are some examples:
964 * +0-15,+32,-128,-0xFF
965 * -0-255,+1-16,+0x128
966 * +1,+2,+3,+4,-5,-7-10
967 * Returns the new bitmap after all changes have been applied. Every
968 * positive value in the string will set a bit and every negative value
969 * in the string will clear a bit. As a bit may be touched more than once,
970 * the last 'operation' wins:
971 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
972 * cleared again. All other bits are unmodified.
974 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
979 /* bits needs to be a multiple of 8 */
985 if (sign != '+' && sign != '-')
987 a = z = simple_strtoul(str, &np, 0);
988 if (str == np || a >= bits)
992 z = simple_strtoul(++str, &np, 0);
993 if (str == np || a > z || z >= bits)
997 for (i = a; i <= z; i++)
999 set_bit_inv(i, bitmap);
1001 clear_bit_inv(i, bitmap);
1002 while (*str == ',' || *str == '\n')
1009 int ap_parse_mask_str(const char *str,
1010 unsigned long *bitmap, int bits,
1013 unsigned long *newmap, size;
1016 /* bits needs to be a multiple of 8 */
1020 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
1021 newmap = kmalloc(size, GFP_KERNEL);
1024 if (mutex_lock_interruptible(lock)) {
1026 return -ERESTARTSYS;
1029 if (*str == '+' || *str == '-') {
1030 memcpy(newmap, bitmap, size);
1031 rc = modify_bitmap(str, newmap, bits);
1033 memset(newmap, 0, size);
1034 rc = hex2bitmap(str, newmap, bits);
1037 memcpy(bitmap, newmap, size);
1042 EXPORT_SYMBOL(ap_parse_mask_str);
1045 * AP bus attributes.
1048 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1050 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1053 static ssize_t ap_domain_store(struct bus_type *bus,
1054 const char *buf, size_t count)
1058 if (sscanf(buf, "%i\n", &domain) != 1 ||
1059 domain < 0 || domain > ap_max_domain_id ||
1060 !test_bit_inv(domain, ap_perms.aqm))
1063 spin_lock_bh(&ap_domain_lock);
1064 ap_domain_index = domain;
1065 spin_unlock_bh(&ap_domain_lock);
1067 AP_DBF_INFO("stored new default domain=%d\n", domain);
1072 static BUS_ATTR_RW(ap_domain);
1074 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1076 if (!ap_qci_info) /* QCI not supported */
1077 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1079 return scnprintf(buf, PAGE_SIZE,
1080 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1081 ap_qci_info->adm[0], ap_qci_info->adm[1],
1082 ap_qci_info->adm[2], ap_qci_info->adm[3],
1083 ap_qci_info->adm[4], ap_qci_info->adm[5],
1084 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1087 static BUS_ATTR_RO(ap_control_domain_mask);
1089 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1091 if (!ap_qci_info) /* QCI not supported */
1092 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1094 return scnprintf(buf, PAGE_SIZE,
1095 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1096 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1097 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1098 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1099 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1102 static BUS_ATTR_RO(ap_usage_domain_mask);
1104 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1106 if (!ap_qci_info) /* QCI not supported */
1107 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1109 return scnprintf(buf, PAGE_SIZE,
1110 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1111 ap_qci_info->apm[0], ap_qci_info->apm[1],
1112 ap_qci_info->apm[2], ap_qci_info->apm[3],
1113 ap_qci_info->apm[4], ap_qci_info->apm[5],
1114 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1117 static BUS_ATTR_RO(ap_adapter_mask);
1119 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1121 return scnprintf(buf, PAGE_SIZE, "%d\n",
1122 ap_using_interrupts() ? 1 : 0);
1125 static BUS_ATTR_RO(ap_interrupts);
1127 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1129 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1132 static ssize_t config_time_store(struct bus_type *bus,
1133 const char *buf, size_t count)
1137 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1139 ap_config_time = time;
1140 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1144 static BUS_ATTR_RW(config_time);
1146 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1148 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1151 static ssize_t poll_thread_store(struct bus_type *bus,
1152 const char *buf, size_t count)
1156 if (sscanf(buf, "%d\n", &flag) != 1)
1159 rc = ap_poll_thread_start();
1163 ap_poll_thread_stop();
1167 static BUS_ATTR_RW(poll_thread);
1169 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1171 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1174 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1177 unsigned long long time;
1180 /* 120 seconds = maximum poll interval */
1181 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1182 time > 120000000000ULL)
1184 poll_timeout = time;
1185 hr_time = poll_timeout;
1187 spin_lock_bh(&ap_poll_timer_lock);
1188 hrtimer_cancel(&ap_poll_timer);
1189 hrtimer_set_expires(&ap_poll_timer, hr_time);
1190 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1191 spin_unlock_bh(&ap_poll_timer_lock);
1196 static BUS_ATTR_RW(poll_timeout);
1198 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1200 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1203 static BUS_ATTR_RO(ap_max_domain_id);
1205 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1207 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1210 static BUS_ATTR_RO(ap_max_adapter_id);
1212 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1216 if (mutex_lock_interruptible(&ap_perms_mutex))
1217 return -ERESTARTSYS;
1218 rc = scnprintf(buf, PAGE_SIZE,
1219 "0x%016lx%016lx%016lx%016lx\n",
1220 ap_perms.apm[0], ap_perms.apm[1],
1221 ap_perms.apm[2], ap_perms.apm[3]);
1222 mutex_unlock(&ap_perms_mutex);
1227 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1232 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1236 ap_bus_revise_bindings();
1241 static BUS_ATTR_RW(apmask);
1243 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1247 if (mutex_lock_interruptible(&ap_perms_mutex))
1248 return -ERESTARTSYS;
1249 rc = scnprintf(buf, PAGE_SIZE,
1250 "0x%016lx%016lx%016lx%016lx\n",
1251 ap_perms.aqm[0], ap_perms.aqm[1],
1252 ap_perms.aqm[2], ap_perms.aqm[3]);
1253 mutex_unlock(&ap_perms_mutex);
1258 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1263 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1267 ap_bus_revise_bindings();
1272 static BUS_ATTR_RW(aqmask);
1274 static ssize_t scans_show(struct bus_type *bus, char *buf)
1276 return scnprintf(buf, PAGE_SIZE, "%llu\n",
1277 atomic64_read(&ap_scan_bus_count));
1280 static BUS_ATTR_RO(scans);
1282 static ssize_t bindings_show(struct bus_type *bus, char *buf)
1285 unsigned int apqns, n;
1287 ap_calc_bound_apqns(&apqns, &n);
1288 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1289 rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns);
1291 rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns);
1296 static BUS_ATTR_RO(bindings);
1298 static struct attribute *ap_bus_attrs[] = {
1299 &bus_attr_ap_domain.attr,
1300 &bus_attr_ap_control_domain_mask.attr,
1301 &bus_attr_ap_usage_domain_mask.attr,
1302 &bus_attr_ap_adapter_mask.attr,
1303 &bus_attr_config_time.attr,
1304 &bus_attr_poll_thread.attr,
1305 &bus_attr_ap_interrupts.attr,
1306 &bus_attr_poll_timeout.attr,
1307 &bus_attr_ap_max_domain_id.attr,
1308 &bus_attr_ap_max_adapter_id.attr,
1309 &bus_attr_apmask.attr,
1310 &bus_attr_aqmask.attr,
1311 &bus_attr_scans.attr,
1312 &bus_attr_bindings.attr,
1315 ATTRIBUTE_GROUPS(ap_bus);
1317 static struct bus_type ap_bus_type = {
1319 .bus_groups = ap_bus_groups,
1320 .match = &ap_bus_match,
1321 .uevent = &ap_uevent,
1325 * ap_select_domain(): Select an AP domain if possible and we haven't
1326 * already done so before.
1328 static void ap_select_domain(void)
1330 struct ap_queue_status status;
1334 * Choose the default domain. Either the one specified with
1335 * the "domain=" parameter or the first domain with at least
1338 spin_lock_bh(&ap_domain_lock);
1339 if (ap_domain_index >= 0) {
1340 /* Domain has already been selected. */
1343 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1344 if (!ap_test_config_usage_domain(dom) ||
1345 !test_bit_inv(dom, ap_perms.aqm))
1347 for (card = 0; card <= ap_max_adapter_id; card++) {
1348 if (!ap_test_config_card_id(card) ||
1349 !test_bit_inv(card, ap_perms.apm))
1351 status = ap_test_queue(AP_MKQID(card, dom),
1352 ap_apft_available(),
1354 if (status.response_code == AP_RESPONSE_NORMAL)
1357 if (card <= ap_max_adapter_id)
1360 if (dom <= ap_max_domain_id) {
1361 ap_domain_index = dom;
1362 AP_DBF_INFO("%s new default domain is %d\n",
1363 __func__, ap_domain_index);
1366 spin_unlock_bh(&ap_domain_lock);
1370 * This function checks the type and returns either 0 for not
1371 * supported or the highest compatible type value (which may
1372 * include the input type value).
1374 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1378 /* < CEX2A is not supported */
1379 if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1380 AP_DBF_WARN("get_comp_type queue=%02x.%04x unsupported type %d\n",
1381 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1384 /* up to CEX7 known and fully supported */
1385 if (rawtype <= AP_DEVICE_TYPE_CEX7)
1388 * unknown new type > CEX7, check for compatibility
1389 * to the highest known and supported type which is
1390 * currently CEX7 with the help of the QACT function.
1392 if (ap_qact_available()) {
1393 struct ap_queue_status status;
1394 union ap_qact_ap_info apinfo = {0};
1396 apinfo.mode = (func >> 26) & 0x07;
1397 apinfo.cat = AP_DEVICE_TYPE_CEX7;
1398 status = ap_qact(qid, 0, &apinfo);
1399 if (status.response_code == AP_RESPONSE_NORMAL
1400 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1401 && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
1402 comp_type = apinfo.cat;
1405 AP_DBF_WARN("get_comp_type queue=%02x.%04x unable to map type %d\n",
1406 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1407 else if (comp_type != rawtype)
1408 AP_DBF_INFO("get_comp_type queue=%02x.%04x map type %d to %d\n",
1409 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1410 rawtype, comp_type);
1415 * Helper function to be used with bus_find_dev
1416 * matches for the card device with the given id
1418 static int __match_card_device_with_id(struct device *dev, const void *data)
1420 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1424 * Helper function to be used with bus_find_dev
1425 * matches for the queue device with a given qid
1427 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1429 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1433 * Helper function to be used with bus_find_dev
1434 * matches any queue device with given queue id
1436 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1438 return is_queue_dev(dev)
1439 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1443 * Helper function for ap_scan_bus().
1444 * Remove card device and associated queue devices.
1446 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1448 bus_for_each_dev(&ap_bus_type, NULL,
1449 (void *)(long) ac->id,
1450 __ap_queue_devices_with_id_unregister);
1451 device_unregister(&ac->ap_dev.device);
1455 * Helper function for ap_scan_bus().
1456 * Does the scan bus job for all the domains within
1457 * a valid adapter given by an ap_card ptr.
1459 static inline void ap_scan_domains(struct ap_card *ac)
1465 struct ap_queue *aq;
1466 int rc, dom, depth, type;
1469 * Go through the configuration for the domains and compare them
1470 * to the existing queue devices. Also take care of the config
1471 * and error state for the queue devices.
1474 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1475 qid = AP_MKQID(ac->id, dom);
1476 dev = bus_find_device(&ap_bus_type, NULL,
1478 __match_queue_device_with_qid);
1479 aq = dev ? to_ap_queue(dev) : NULL;
1480 if (!ap_test_config_usage_domain(dom)) {
1482 AP_DBF_INFO("%s(%d,%d) not in config any more, rm queue device\n",
1483 __func__, ac->id, dom);
1484 device_unregister(dev);
1489 /* domain is valid, get info from this APQN */
1490 if (!ap_queue_info(qid, &type, &func, &depth, &decfg)) {
1493 "%s(%d,%d) ap_queue_info() not successful, rm queue device\n",
1494 __func__, ac->id, dom);
1495 device_unregister(dev);
1500 /* if no queue device exists, create a new one */
1502 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1504 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1505 __func__, ac->id, dom);
1509 aq->config = !decfg;
1510 dev = &aq->ap_dev.device;
1511 dev->bus = &ap_bus_type;
1512 dev->parent = &ac->ap_dev.device;
1513 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1514 /* register queue device */
1515 rc = device_register(dev);
1517 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1518 __func__, ac->id, dom);
1519 goto put_dev_and_continue;
1521 /* get it and thus adjust reference counter */
1524 AP_DBF_INFO("%s(%d,%d) new (decfg) queue device created\n",
1525 __func__, ac->id, dom);
1527 AP_DBF_INFO("%s(%d,%d) new queue device created\n",
1528 __func__, ac->id, dom);
1529 goto put_dev_and_continue;
1531 /* Check config state on the already existing queue device */
1532 spin_lock_bh(&aq->lock);
1533 if (decfg && aq->config) {
1534 /* config off this queue device */
1536 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1537 aq->dev_state = AP_DEV_STATE_ERROR;
1538 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1540 spin_unlock_bh(&aq->lock);
1541 AP_DBF_INFO("%s(%d,%d) queue device config off\n",
1542 __func__, ac->id, dom);
1543 /* 'receive' pending messages with -EAGAIN */
1545 goto put_dev_and_continue;
1547 if (!decfg && !aq->config) {
1548 /* config on this queue device */
1550 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1551 aq->dev_state = AP_DEV_STATE_OPERATING;
1552 aq->sm_state = AP_SM_STATE_RESET_START;
1554 spin_unlock_bh(&aq->lock);
1555 AP_DBF_INFO("%s(%d,%d) queue device config on\n",
1556 __func__, ac->id, dom);
1557 goto put_dev_and_continue;
1559 /* handle other error states */
1560 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1561 spin_unlock_bh(&aq->lock);
1562 /* 'receive' pending messages with -EAGAIN */
1564 /* re-init (with reset) the queue device */
1565 ap_queue_init_state(aq);
1566 AP_DBF_INFO("%s(%d,%d) queue device reinit enforced\n",
1567 __func__, ac->id, dom);
1568 goto put_dev_and_continue;
1570 spin_unlock_bh(&aq->lock);
1571 put_dev_and_continue:
1577 * Helper function for ap_scan_bus().
1578 * Does the scan bus job for the given adapter id.
1580 static inline void ap_scan_adapter(int ap)
1587 int rc, dom, depth, type, comp_type;
1589 /* Is there currently a card device for this adapter ? */
1590 dev = bus_find_device(&ap_bus_type, NULL,
1592 __match_card_device_with_id);
1593 ac = dev ? to_ap_card(dev) : NULL;
1595 /* Adapter not in configuration ? */
1596 if (!ap_test_config_card_id(ap)) {
1598 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devices\n",
1600 ap_scan_rm_card_dev_and_queue_devs(ac);
1607 * Adapter ap is valid in the current configuration. So do some checks:
1608 * If no card device exists, build one. If a card device exists, check
1609 * for type and functions changed. For all this we need to find a valid
1613 for (dom = 0; dom <= ap_max_domain_id; dom++)
1614 if (ap_test_config_usage_domain(dom)) {
1615 qid = AP_MKQID(ap, dom);
1616 if (ap_queue_info(qid, &type, &func, &depth, &decfg))
1619 if (dom > ap_max_domain_id) {
1620 /* Could not find a valid APQN for this adapter */
1623 "%s(%d) no type info (no APQN found), rm card and queue devices\n",
1625 ap_scan_rm_card_dev_and_queue_devs(ac);
1628 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1634 /* No apdater type info available, an unusable adapter */
1636 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devices\n",
1638 ap_scan_rm_card_dev_and_queue_devs(ac);
1641 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1648 /* Check APQN against existing card device for changes */
1649 if (ac->raw_hwtype != type) {
1650 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devices\n",
1651 __func__, ap, type);
1652 ap_scan_rm_card_dev_and_queue_devs(ac);
1655 } else if (ac->functions != func) {
1656 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devices\n",
1657 __func__, ap, type);
1658 ap_scan_rm_card_dev_and_queue_devs(ac);
1662 if (decfg && ac->config) {
1664 AP_DBF_INFO("%s(%d) card device config off\n",
1668 if (!decfg && !ac->config) {
1670 AP_DBF_INFO("%s(%d) card device config on\n",
1677 /* Build a new card device */
1678 comp_type = ap_get_compatible_type(qid, type, func);
1680 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
1681 __func__, ap, type);
1684 ac = ap_card_create(ap, depth, type, comp_type, func);
1686 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
1690 ac->config = !decfg;
1691 dev = &ac->ap_dev.device;
1692 dev->bus = &ap_bus_type;
1693 dev->parent = ap_root_device;
1694 dev_set_name(dev, "card%02x", ap);
1695 /* Register the new card device with AP bus */
1696 rc = device_register(dev);
1698 AP_DBF_WARN("%s(%d) device_register() failed\n",
1703 /* get it and thus adjust reference counter */
1706 AP_DBF_INFO("%s(%d) new (decfg) card device type=%d func=0x%08x created\n",
1707 __func__, ap, type, func);
1709 AP_DBF_INFO("%s(%d) new card device type=%d func=0x%08x created\n",
1710 __func__, ap, type, func);
1713 /* Verify the domains and the queue devices for this card */
1714 ap_scan_domains(ac);
1716 /* release the card device */
1717 put_device(&ac->ap_dev.device);
1721 * ap_scan_bus(): Scan the AP bus for new devices
1722 * Runs periodically, workqueue timer (ap_config_time)
1724 static void ap_scan_bus(struct work_struct *unused)
1728 ap_fetch_qci_info(ap_qci_info);
1731 AP_DBF_DBG("%s running\n", __func__);
1733 /* loop over all possible adapters */
1734 for (ap = 0; ap <= ap_max_adapter_id; ap++)
1735 ap_scan_adapter(ap);
1737 /* check if there is at least one queue available with default domain */
1738 if (ap_domain_index >= 0) {
1739 struct device *dev =
1740 bus_find_device(&ap_bus_type, NULL,
1741 (void *)(long) ap_domain_index,
1742 __match_queue_device_with_queue_id);
1746 AP_DBF_INFO("no queue device with default domain %d available\n",
1750 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
1751 AP_DBF(DBF_DEBUG, "%s init scan complete\n", __func__);
1752 ap_send_init_scan_done_uevent();
1753 ap_check_bindings_complete();
1756 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1759 static void ap_config_timeout(struct timer_list *unused)
1761 queue_work(system_long_wq, &ap_scan_work);
1764 static int __init ap_debug_init(void)
1766 ap_dbf_info = debug_register("ap", 1, 1,
1767 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1768 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1769 debug_set_level(ap_dbf_info, DBF_ERR);
1774 static void __init ap_perms_init(void)
1776 /* all resources useable if no kernel parameter string given */
1777 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1778 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1779 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1781 /* apm kernel parameter string */
1783 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1784 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1788 /* aqm kernel parameter string */
1790 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1791 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1797 * ap_module_init(): The module initialization code.
1799 * Initializes the module.
1801 static int __init ap_module_init(void)
1805 rc = ap_debug_init();
1809 if (!ap_instructions_available()) {
1810 pr_warn("The hardware system does not support AP instructions\n");
1814 /* init ap_queue hashtable */
1815 hash_init(ap_queues);
1817 /* set up the AP permissions (ioctls, ap and aq masks) */
1820 /* Get AP configuration data if available */
1823 /* check default domain setting */
1824 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
1825 (ap_domain_index >= 0 &&
1826 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1827 pr_warn("%d is not a valid cryptographic domain\n",
1829 ap_domain_index = -1;
1832 /* enable interrupts if available */
1833 if (ap_interrupts_available()) {
1834 rc = register_adapter_interrupt(&ap_airq);
1835 ap_airq_flag = (rc == 0);
1838 /* Create /sys/bus/ap. */
1839 rc = bus_register(&ap_bus_type);
1843 /* Create /sys/devices/ap. */
1844 ap_root_device = root_device_register("ap");
1845 rc = PTR_ERR_OR_ZERO(ap_root_device);
1848 ap_root_device->bus = &ap_bus_type;
1850 /* Setup the AP bus rescan timer. */
1851 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1854 * Setup the high resultion poll timer.
1855 * If we are running under z/VM adjust polling to z/VM polling rate.
1858 poll_timeout = 1500000;
1859 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1860 ap_poll_timer.function = ap_poll_timeout;
1862 /* Start the low priority AP bus poll thread. */
1863 if (ap_thread_flag) {
1864 rc = ap_poll_thread_start();
1869 queue_work(system_long_wq, &ap_scan_work);
1874 hrtimer_cancel(&ap_poll_timer);
1875 root_device_unregister(ap_root_device);
1877 bus_unregister(&ap_bus_type);
1879 if (ap_using_interrupts())
1880 unregister_adapter_interrupt(&ap_airq);
1884 device_initcall(ap_module_init);