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>
31 #include <linux/atomic.h>
33 #include <linux/hrtimer.h>
34 #include <linux/ktime.h>
35 #include <asm/facility.h>
36 #include <linux/crypto.h>
37 #include <linux/mod_devicetable.h>
38 #include <linux/debugfs.h>
39 #include <linux/ctype.h>
40 #include <linux/module.h>
46 * Module parameters; note though this file itself isn't modular.
48 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
49 static DEFINE_SPINLOCK(ap_domain_lock);
50 module_param_named(domain, ap_domain_index, int, 0440);
51 MODULE_PARM_DESC(domain, "domain index for ap devices");
52 EXPORT_SYMBOL(ap_domain_index);
54 static int ap_thread_flag;
55 module_param_named(poll_thread, ap_thread_flag, int, 0440);
56 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
59 module_param_named(apmask, apm_str, charp, 0440);
60 MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
63 module_param_named(aqmask, aqm_str, charp, 0440);
64 MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
66 static int ap_useirq = 1;
67 module_param_named(useirq, ap_useirq, int, 0440);
68 MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on).");
70 atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
71 EXPORT_SYMBOL(ap_max_msg_size);
73 static struct device *ap_root_device;
75 /* Hashtable of all queue devices on the AP bus */
76 DEFINE_HASHTABLE(ap_queues, 8);
77 /* lock used for the ap_queues hashtable */
78 DEFINE_SPINLOCK(ap_queues_lock);
80 /* Default permissions (ioctl, card and domain masking) */
81 struct ap_perms ap_perms;
82 EXPORT_SYMBOL(ap_perms);
83 DEFINE_MUTEX(ap_perms_mutex);
84 EXPORT_SYMBOL(ap_perms_mutex);
86 /* # of bus scans since init */
87 static atomic64_t ap_scan_bus_count;
89 /* # of bindings complete since init */
90 static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
92 /* completion for initial APQN bindings complete */
93 static DECLARE_COMPLETION(ap_init_apqn_bindings_complete);
95 static struct ap_config_info *ap_qci_info;
96 static struct ap_config_info *ap_qci_info_old;
99 * AP bus related debug feature things.
101 debug_info_t *ap_dbf_info;
104 * Workqueue timer for bus rescan.
106 static struct timer_list ap_config_timer;
107 static int ap_config_time = AP_CONFIG_TIME;
108 static void ap_scan_bus(struct work_struct *);
109 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
112 * Tasklet & timer for AP request polling and interrupts
114 static void ap_tasklet_fn(unsigned long);
115 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
116 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
117 static struct task_struct *ap_poll_kthread;
118 static DEFINE_MUTEX(ap_poll_thread_mutex);
119 static DEFINE_SPINLOCK(ap_poll_timer_lock);
120 static struct hrtimer ap_poll_timer;
122 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
123 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
125 static unsigned long long poll_timeout = 250000;
127 /* Maximum domain id, if not given via qci */
128 static int ap_max_domain_id = 15;
129 /* Maximum adapter id, if not given via qci */
130 static int ap_max_adapter_id = 63;
132 static struct bus_type ap_bus_type;
134 /* Adapter interrupt definitions */
135 static void ap_interrupt_handler(struct airq_struct *airq,
136 struct tpi_info *tpi_info);
138 static bool ap_irq_flag;
140 static struct airq_struct ap_airq = {
141 .handler = ap_interrupt_handler,
146 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
148 * Returns the address of the local-summary-indicator of the adapter
149 * interrupt handler for AP, or NULL if adapter interrupts are not
152 void *ap_airq_ptr(void)
155 return ap_airq.lsi_ptr;
160 * ap_interrupts_available(): Test if AP interrupts are available.
162 * Returns 1 if AP interrupts are available.
164 static int ap_interrupts_available(void)
166 return test_facility(65);
170 * ap_qci_available(): Test if AP configuration
171 * information can be queried via QCI subfunction.
173 * Returns 1 if subfunction PQAP(QCI) is available.
175 static int ap_qci_available(void)
177 return test_facility(12);
181 * ap_apft_available(): Test if AP facilities test (APFT)
182 * facility is available.
184 * Returns 1 if APFT is available.
186 static int ap_apft_available(void)
188 return test_facility(15);
192 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
194 * Returns 1 if the QACT subfunction is available.
196 static inline int ap_qact_available(void)
199 return ap_qci_info->qact;
204 * ap_fetch_qci_info(): Fetch cryptographic config info
206 * Returns the ap configuration info fetched via PQAP(QCI).
207 * On success 0 is returned, on failure a negative errno
208 * is returned, e.g. if the PQAP(QCI) instruction is not
209 * available, the return value will be -EOPNOTSUPP.
211 static inline int ap_fetch_qci_info(struct ap_config_info *info)
213 if (!ap_qci_available())
221 * ap_init_qci_info(): Allocate and query qci config info.
222 * Does also update the static variables ap_max_domain_id
223 * and ap_max_adapter_id if this info is available.
225 static void __init ap_init_qci_info(void)
227 if (!ap_qci_available()) {
228 AP_DBF_INFO("%s QCI not supported\n", __func__);
232 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
235 ap_qci_info_old = kzalloc(sizeof(*ap_qci_info_old), GFP_KERNEL);
236 if (!ap_qci_info_old)
238 if (ap_fetch_qci_info(ap_qci_info) != 0) {
240 kfree(ap_qci_info_old);
242 ap_qci_info_old = NULL;
245 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
247 if (ap_qci_info->apxa) {
248 if (ap_qci_info->Na) {
249 ap_max_adapter_id = ap_qci_info->Na;
250 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
251 __func__, ap_max_adapter_id);
253 if (ap_qci_info->Nd) {
254 ap_max_domain_id = ap_qci_info->Nd;
255 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
256 __func__, ap_max_domain_id);
260 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
264 * ap_test_config(): helper function to extract the nrth bit
265 * within the unsigned int array field.
267 static inline int ap_test_config(unsigned int *field, unsigned int nr)
269 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
273 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
275 * Returns 0 if the card is not configured
276 * 1 if the card is configured or
277 * if the configuration information is not available
279 static inline int ap_test_config_card_id(unsigned int id)
281 if (id > ap_max_adapter_id)
284 return ap_test_config(ap_qci_info->apm, id);
289 * ap_test_config_usage_domain(): Test, whether an AP usage domain
292 * Returns 0 if the usage domain is not configured
293 * 1 if the usage domain is configured or
294 * if the configuration information is not available
296 int ap_test_config_usage_domain(unsigned int domain)
298 if (domain > ap_max_domain_id)
301 return ap_test_config(ap_qci_info->aqm, domain);
304 EXPORT_SYMBOL(ap_test_config_usage_domain);
307 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
309 * @domain AP control domain ID
311 * Returns 1 if the control domain is configured
312 * 0 in all other cases
314 int ap_test_config_ctrl_domain(unsigned int domain)
316 if (!ap_qci_info || domain > ap_max_domain_id)
318 return ap_test_config(ap_qci_info->adm, domain);
320 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
323 * ap_queue_info(): Check and get AP queue info.
324 * Returns true if TAPQ succeeded and the info is filled or
327 static bool ap_queue_info(ap_qid_t qid, int *q_type, unsigned int *q_fac,
328 int *q_depth, int *q_ml, bool *q_decfg, bool *q_cstop)
330 struct ap_queue_status status;
334 unsigned int fac : 32; /* facility bits */
335 unsigned int at : 8; /* ap type */
336 unsigned int _res1 : 8;
337 unsigned int _res2 : 4;
338 unsigned int ml : 4; /* apxl ml */
339 unsigned int _res3 : 4;
340 unsigned int qd : 4; /* queue depth */
346 /* make sure we don't run into a specifiation exception */
347 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
348 AP_QID_QUEUE(qid) > ap_max_domain_id)
351 /* call TAPQ on this APQN */
352 status = ap_test_queue(qid, ap_apft_available(), &tapq_info.value);
353 switch (status.response_code) {
354 case AP_RESPONSE_NORMAL:
355 case AP_RESPONSE_RESET_IN_PROGRESS:
356 case AP_RESPONSE_DECONFIGURED:
357 case AP_RESPONSE_CHECKSTOPPED:
358 case AP_RESPONSE_BUSY:
360 * According to the architecture in all these cases the
361 * info should be filled. All bits 0 is not possible as
362 * there is at least one of the mode bits set.
364 if (WARN_ON_ONCE(!tapq_info.value))
366 *q_type = tapq_info.tapq_gr2.at;
367 *q_fac = tapq_info.tapq_gr2.fac;
368 *q_depth = tapq_info.tapq_gr2.qd;
369 *q_ml = tapq_info.tapq_gr2.ml;
370 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
371 *q_cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
373 /* For CEX2 and CEX3 the available functions
374 * are not reflected by the facilities bits.
375 * Instead it is coded into the type. So here
376 * modify the function bits based on the type.
378 case AP_DEVICE_TYPE_CEX2A:
379 case AP_DEVICE_TYPE_CEX3A:
380 *q_fac |= 0x08000000;
382 case AP_DEVICE_TYPE_CEX2C:
383 case AP_DEVICE_TYPE_CEX3C:
384 *q_fac |= 0x10000000;
392 * A response code which indicates, there is no info available.
398 void ap_wait(enum ap_sm_wait wait)
403 case AP_SM_WAIT_AGAIN:
404 case AP_SM_WAIT_INTERRUPT:
407 if (ap_poll_kthread) {
408 wake_up(&ap_poll_wait);
412 case AP_SM_WAIT_TIMEOUT:
413 spin_lock_bh(&ap_poll_timer_lock);
414 if (!hrtimer_is_queued(&ap_poll_timer)) {
415 hr_time = poll_timeout;
416 hrtimer_forward_now(&ap_poll_timer, hr_time);
417 hrtimer_restart(&ap_poll_timer);
419 spin_unlock_bh(&ap_poll_timer_lock);
421 case AP_SM_WAIT_NONE:
428 * ap_request_timeout(): Handling of request timeouts
429 * @t: timer making this callback
431 * Handles request timeouts.
433 void ap_request_timeout(struct timer_list *t)
435 struct ap_queue *aq = from_timer(aq, t, timeout);
437 spin_lock_bh(&aq->lock);
438 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
439 spin_unlock_bh(&aq->lock);
443 * ap_poll_timeout(): AP receive polling for finished AP requests.
444 * @unused: Unused pointer.
446 * Schedules the AP tasklet using a high resolution timer.
448 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
450 tasklet_schedule(&ap_tasklet);
451 return HRTIMER_NORESTART;
455 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
456 * @airq: pointer to adapter interrupt descriptor
459 static void ap_interrupt_handler(struct airq_struct *airq,
460 struct tpi_info *tpi_info)
462 inc_irq_stat(IRQIO_APB);
463 tasklet_schedule(&ap_tasklet);
467 * ap_tasklet_fn(): Tasklet to poll all AP devices.
468 * @dummy: Unused variable
470 * Poll all AP devices on the bus.
472 static void ap_tasklet_fn(unsigned long dummy)
476 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
478 /* Reset the indicator if interrupts are used. Thus new interrupts can
479 * be received. Doing it in the beginning of the tasklet is therefor
480 * important that no requests on any AP get lost.
483 xchg(ap_airq.lsi_ptr, 0);
485 spin_lock_bh(&ap_queues_lock);
486 hash_for_each(ap_queues, bkt, aq, hnode) {
487 spin_lock_bh(&aq->lock);
488 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
489 spin_unlock_bh(&aq->lock);
491 spin_unlock_bh(&ap_queues_lock);
496 static int ap_pending_requests(void)
501 spin_lock_bh(&ap_queues_lock);
502 hash_for_each(ap_queues, bkt, aq, hnode) {
503 if (aq->queue_count == 0)
505 spin_unlock_bh(&ap_queues_lock);
508 spin_unlock_bh(&ap_queues_lock);
513 * ap_poll_thread(): Thread that polls for finished requests.
514 * @data: Unused pointer
516 * AP bus poll thread. The purpose of this thread is to poll for
517 * finished requests in a loop if there is a "free" cpu - that is
518 * a cpu that doesn't have anything better to do. The polling stops
519 * as soon as there is another task or if all messages have been
522 static int ap_poll_thread(void *data)
524 DECLARE_WAITQUEUE(wait, current);
526 set_user_nice(current, MAX_NICE);
528 while (!kthread_should_stop()) {
529 add_wait_queue(&ap_poll_wait, &wait);
530 set_current_state(TASK_INTERRUPTIBLE);
531 if (!ap_pending_requests()) {
535 set_current_state(TASK_RUNNING);
536 remove_wait_queue(&ap_poll_wait, &wait);
537 if (need_resched()) {
548 static int ap_poll_thread_start(void)
552 if (ap_irq_flag || ap_poll_kthread)
554 mutex_lock(&ap_poll_thread_mutex);
555 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
556 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
558 ap_poll_kthread = NULL;
559 mutex_unlock(&ap_poll_thread_mutex);
563 static void ap_poll_thread_stop(void)
565 if (!ap_poll_kthread)
567 mutex_lock(&ap_poll_thread_mutex);
568 kthread_stop(ap_poll_kthread);
569 ap_poll_kthread = NULL;
570 mutex_unlock(&ap_poll_thread_mutex);
573 #define is_card_dev(x) ((x)->parent == ap_root_device)
574 #define is_queue_dev(x) ((x)->parent != ap_root_device)
578 * @dev: Pointer to device
579 * @drv: Pointer to device_driver
581 * AP bus driver registration/unregistration.
583 static int ap_bus_match(struct device *dev, struct device_driver *drv)
585 struct ap_driver *ap_drv = to_ap_drv(drv);
586 struct ap_device_id *id;
589 * Compare device type of the device with the list of
590 * supported types of the device_driver.
592 for (id = ap_drv->ids; id->match_flags; id++) {
593 if (is_card_dev(dev) &&
594 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
595 id->dev_type == to_ap_dev(dev)->device_type)
597 if (is_queue_dev(dev) &&
598 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
599 id->dev_type == to_ap_dev(dev)->device_type)
606 * ap_uevent(): Uevent function for AP devices.
607 * @dev: Pointer to device
608 * @env: Pointer to kobj_uevent_env
610 * It sets up a single environment variable DEV_TYPE which contains the
611 * hardware device type.
613 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
616 struct ap_device *ap_dev = to_ap_dev(dev);
618 /* Uevents from ap bus core don't need extensions to the env */
619 if (dev == ap_root_device)
622 if (is_card_dev(dev)) {
623 struct ap_card *ac = to_ap_card(&ap_dev->device);
625 /* Set up DEV_TYPE environment variable. */
626 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
630 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
634 /* Add MODE=<accel|cca|ep11> */
635 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL))
636 rc = add_uevent_var(env, "MODE=accel");
637 else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
638 rc = add_uevent_var(env, "MODE=cca");
639 else if (ap_test_bit(&ac->functions, AP_FUNC_EP11))
640 rc = add_uevent_var(env, "MODE=ep11");
644 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
646 /* Add MODE=<accel|cca|ep11> */
647 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL))
648 rc = add_uevent_var(env, "MODE=accel");
649 else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
650 rc = add_uevent_var(env, "MODE=cca");
651 else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11))
652 rc = add_uevent_var(env, "MODE=ep11");
660 static void ap_send_init_scan_done_uevent(void)
662 char *envp[] = { "INITSCAN=done", NULL };
664 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
667 static void ap_send_bindings_complete_uevent(void)
670 char *envp[] = { "BINDINGS=complete", buf, NULL };
672 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
673 atomic64_inc_return(&ap_bindings_complete_count));
674 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
677 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
680 char *envp[] = { buf, NULL };
682 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
684 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
686 EXPORT_SYMBOL(ap_send_config_uevent);
688 void ap_send_online_uevent(struct ap_device *ap_dev, int online)
691 char *envp[] = { buf, NULL };
693 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
695 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
697 EXPORT_SYMBOL(ap_send_online_uevent);
699 static void ap_send_mask_changed_uevent(unsigned long *newapm,
700 unsigned long *newaqm)
703 char *envp[] = { buf, NULL };
706 snprintf(buf, sizeof(buf),
707 "APMASK=0x%016lx%016lx%016lx%016lx\n",
708 newapm[0], newapm[1], newapm[2], newapm[3]);
710 snprintf(buf, sizeof(buf),
711 "AQMASK=0x%016lx%016lx%016lx%016lx\n",
712 newaqm[0], newaqm[1], newaqm[2], newaqm[3]);
714 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
718 * calc # of bound APQNs
721 struct __ap_calc_ctrs {
726 static int __ap_calc_helper(struct device *dev, void *arg)
728 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg;
730 if (is_queue_dev(dev)) {
739 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
741 struct __ap_calc_ctrs ctrs;
743 memset(&ctrs, 0, sizeof(ctrs));
744 bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper);
751 * After initial ap bus scan do check if all existing APQNs are
752 * bound to device drivers.
754 static void ap_check_bindings_complete(void)
756 unsigned int apqns, bound;
758 if (atomic64_read(&ap_scan_bus_count) >= 1) {
759 ap_calc_bound_apqns(&apqns, &bound);
760 if (bound == apqns) {
761 if (!completion_done(&ap_init_apqn_bindings_complete)) {
762 complete_all(&ap_init_apqn_bindings_complete);
763 AP_DBF_INFO("%s complete\n", __func__);
765 ap_send_bindings_complete_uevent();
771 * Interface to wait for the AP bus to have done one initial ap bus
772 * scan and all detected APQNs have been bound to device drivers.
773 * If these both conditions are not fulfilled, this function blocks
774 * on a condition with wait_for_completion_interruptible_timeout().
775 * If these both conditions are fulfilled (before the timeout hits)
776 * the return value is 0. If the timeout (in jiffies) hits instead
777 * -ETIME is returned. On failures negative return values are
778 * returned to the caller.
780 int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
784 if (completion_done(&ap_init_apqn_bindings_complete))
788 l = wait_for_completion_interruptible_timeout(
789 &ap_init_apqn_bindings_complete, timeout);
791 l = wait_for_completion_interruptible(
792 &ap_init_apqn_bindings_complete);
794 return l == -ERESTARTSYS ? -EINTR : l;
795 else if (l == 0 && timeout)
800 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
802 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
804 if (is_queue_dev(dev) &&
805 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data)
806 device_unregister(dev);
810 static int __ap_revise_reserved(struct device *dev, void *dummy)
812 int rc, card, queue, devres, drvres;
814 if (is_queue_dev(dev)) {
815 card = AP_QID_CARD(to_ap_queue(dev)->qid);
816 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
817 mutex_lock(&ap_perms_mutex);
818 devres = test_bit_inv(card, ap_perms.apm) &&
819 test_bit_inv(queue, ap_perms.aqm);
820 mutex_unlock(&ap_perms_mutex);
821 drvres = to_ap_drv(dev->driver)->flags
822 & AP_DRIVER_FLAG_DEFAULT;
823 if (!!devres != !!drvres) {
824 AP_DBF_DBG("%s reprobing queue=%02x.%04x\n",
825 __func__, card, queue);
826 rc = device_reprobe(dev);
828 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
829 __func__, card, queue);
836 static void ap_bus_revise_bindings(void)
838 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
842 * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
843 * default host driver or not.
844 * @card: the APID of the adapter card to check
845 * @queue: the APQI of the queue to check
847 * Note: the ap_perms_mutex must be locked by the caller of this function.
849 * Return: an int specifying whether the AP adapter is reserved for the host (1)
852 int ap_owned_by_def_drv(int card, int queue)
856 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
859 if (test_bit_inv(card, ap_perms.apm) &&
860 test_bit_inv(queue, ap_perms.aqm))
865 EXPORT_SYMBOL(ap_owned_by_def_drv);
868 * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
869 * a set is reserved for the host drivers
871 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
872 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
874 * Note: the ap_perms_mutex must be locked by the caller of this function.
876 * Return: an int specifying whether each APQN is reserved for the host (1) or
879 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
882 int card, queue, rc = 0;
884 for (card = 0; !rc && card < AP_DEVICES; card++)
885 if (test_bit_inv(card, apm) &&
886 test_bit_inv(card, ap_perms.apm))
887 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
888 if (test_bit_inv(queue, aqm) &&
889 test_bit_inv(queue, ap_perms.aqm))
894 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
896 static int ap_device_probe(struct device *dev)
898 struct ap_device *ap_dev = to_ap_dev(dev);
899 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
900 int card, queue, devres, drvres, rc = -ENODEV;
902 if (!get_device(dev))
905 if (is_queue_dev(dev)) {
907 * If the apqn is marked as reserved/used by ap bus and
908 * default drivers, only probe with drivers with the default
909 * flag set. If it is not marked, only probe with drivers
910 * with the default flag not set.
912 card = AP_QID_CARD(to_ap_queue(dev)->qid);
913 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
914 mutex_lock(&ap_perms_mutex);
915 devres = test_bit_inv(card, ap_perms.apm) &&
916 test_bit_inv(queue, ap_perms.aqm);
917 mutex_unlock(&ap_perms_mutex);
918 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
919 if (!!devres != !!drvres)
923 /* Add queue/card to list of active queues/cards */
924 spin_lock_bh(&ap_queues_lock);
925 if (is_queue_dev(dev))
926 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
927 to_ap_queue(dev)->qid);
928 spin_unlock_bh(&ap_queues_lock);
930 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
933 spin_lock_bh(&ap_queues_lock);
934 if (is_queue_dev(dev))
935 hash_del(&to_ap_queue(dev)->hnode);
936 spin_unlock_bh(&ap_queues_lock);
938 ap_check_bindings_complete();
947 static void ap_device_remove(struct device *dev)
949 struct ap_device *ap_dev = to_ap_dev(dev);
950 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
952 /* prepare ap queue device removal */
953 if (is_queue_dev(dev))
954 ap_queue_prepare_remove(to_ap_queue(dev));
956 /* driver's chance to clean up gracefully */
958 ap_drv->remove(ap_dev);
960 /* now do the ap queue device remove */
961 if (is_queue_dev(dev))
962 ap_queue_remove(to_ap_queue(dev));
964 /* Remove queue/card from list of active queues/cards */
965 spin_lock_bh(&ap_queues_lock);
966 if (is_queue_dev(dev))
967 hash_del(&to_ap_queue(dev)->hnode);
968 spin_unlock_bh(&ap_queues_lock);
973 struct ap_queue *ap_get_qdev(ap_qid_t qid)
978 spin_lock_bh(&ap_queues_lock);
979 hash_for_each(ap_queues, bkt, aq, hnode) {
980 if (aq->qid == qid) {
981 get_device(&aq->ap_dev.device);
982 spin_unlock_bh(&ap_queues_lock);
986 spin_unlock_bh(&ap_queues_lock);
990 EXPORT_SYMBOL(ap_get_qdev);
992 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
995 struct device_driver *drv = &ap_drv->driver;
997 drv->bus = &ap_bus_type;
1000 return driver_register(drv);
1002 EXPORT_SYMBOL(ap_driver_register);
1004 void ap_driver_unregister(struct ap_driver *ap_drv)
1006 driver_unregister(&ap_drv->driver);
1008 EXPORT_SYMBOL(ap_driver_unregister);
1010 void ap_bus_force_rescan(void)
1012 /* processing a asynchronous bus rescan */
1013 del_timer(&ap_config_timer);
1014 queue_work(system_long_wq, &ap_scan_work);
1015 flush_work(&ap_scan_work);
1017 EXPORT_SYMBOL(ap_bus_force_rescan);
1020 * A config change has happened, force an ap bus rescan.
1022 void ap_bus_cfg_chg(void)
1024 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
1026 ap_bus_force_rescan();
1030 * hex2bitmap() - parse hex mask string and set bitmap.
1031 * Valid strings are "0x012345678" with at least one valid hex number.
1032 * Rest of the bitmap to the right is padded with 0. No spaces allowed
1033 * within the string, the leading 0x may be omitted.
1034 * Returns the bitmask with exactly the bits set as given by the hex
1035 * string (both in big endian order).
1037 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1041 /* bits needs to be a multiple of 8 */
1045 if (str[0] == '0' && str[1] == 'x')
1050 for (i = 0; isxdigit(*str) && i < bits; str++) {
1051 b = hex_to_bin(*str);
1052 for (n = 0; n < 4; n++)
1053 if (b & (0x08 >> n))
1054 set_bit_inv(i + n, bitmap);
1066 * modify_bitmap() - parse bitmask argument and modify an existing
1067 * bit mask accordingly. A concatenation (done with ',') of these
1068 * terms is recognized:
1069 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1070 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1071 * 0...bits-1; the leading + or - is required. Here are some examples:
1072 * +0-15,+32,-128,-0xFF
1073 * -0-255,+1-16,+0x128
1074 * +1,+2,+3,+4,-5,-7-10
1075 * Returns the new bitmap after all changes have been applied. Every
1076 * positive value in the string will set a bit and every negative value
1077 * in the string will clear a bit. As a bit may be touched more than once,
1078 * the last 'operation' wins:
1079 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1080 * cleared again. All other bits are unmodified.
1082 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1087 /* bits needs to be a multiple of 8 */
1093 if (sign != '+' && sign != '-')
1095 a = z = simple_strtoul(str, &np, 0);
1096 if (str == np || a >= bits)
1100 z = simple_strtoul(++str, &np, 0);
1101 if (str == np || a > z || z >= bits)
1105 for (i = a; i <= z; i++)
1107 set_bit_inv(i, bitmap);
1109 clear_bit_inv(i, bitmap);
1110 while (*str == ',' || *str == '\n')
1117 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1118 unsigned long *newmap)
1123 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1124 if (*str == '+' || *str == '-') {
1125 memcpy(newmap, bitmap, size);
1126 rc = modify_bitmap(str, newmap, bits);
1128 memset(newmap, 0, size);
1129 rc = hex2bitmap(str, newmap, bits);
1134 int ap_parse_mask_str(const char *str,
1135 unsigned long *bitmap, int bits,
1138 unsigned long *newmap, size;
1141 /* bits needs to be a multiple of 8 */
1145 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1146 newmap = kmalloc(size, GFP_KERNEL);
1149 if (mutex_lock_interruptible(lock)) {
1151 return -ERESTARTSYS;
1153 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
1155 memcpy(bitmap, newmap, size);
1160 EXPORT_SYMBOL(ap_parse_mask_str);
1163 * AP bus attributes.
1166 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1168 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1171 static ssize_t ap_domain_store(struct bus_type *bus,
1172 const char *buf, size_t count)
1176 if (sscanf(buf, "%i\n", &domain) != 1 ||
1177 domain < 0 || domain > ap_max_domain_id ||
1178 !test_bit_inv(domain, ap_perms.aqm))
1181 spin_lock_bh(&ap_domain_lock);
1182 ap_domain_index = domain;
1183 spin_unlock_bh(&ap_domain_lock);
1185 AP_DBF_INFO("%s stored new default domain=%d\n",
1191 static BUS_ATTR_RW(ap_domain);
1193 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1195 if (!ap_qci_info) /* QCI not supported */
1196 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1198 return scnprintf(buf, PAGE_SIZE,
1199 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1200 ap_qci_info->adm[0], ap_qci_info->adm[1],
1201 ap_qci_info->adm[2], ap_qci_info->adm[3],
1202 ap_qci_info->adm[4], ap_qci_info->adm[5],
1203 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1206 static BUS_ATTR_RO(ap_control_domain_mask);
1208 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1210 if (!ap_qci_info) /* QCI not supported */
1211 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1213 return scnprintf(buf, PAGE_SIZE,
1214 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1215 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1216 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1217 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1218 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1221 static BUS_ATTR_RO(ap_usage_domain_mask);
1223 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1225 if (!ap_qci_info) /* QCI not supported */
1226 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1228 return scnprintf(buf, PAGE_SIZE,
1229 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1230 ap_qci_info->apm[0], ap_qci_info->apm[1],
1231 ap_qci_info->apm[2], ap_qci_info->apm[3],
1232 ap_qci_info->apm[4], ap_qci_info->apm[5],
1233 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1236 static BUS_ATTR_RO(ap_adapter_mask);
1238 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1240 return scnprintf(buf, PAGE_SIZE, "%d\n",
1241 ap_irq_flag ? 1 : 0);
1244 static BUS_ATTR_RO(ap_interrupts);
1246 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1248 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1251 static ssize_t config_time_store(struct bus_type *bus,
1252 const char *buf, size_t count)
1256 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1258 ap_config_time = time;
1259 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1263 static BUS_ATTR_RW(config_time);
1265 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1267 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1270 static ssize_t poll_thread_store(struct bus_type *bus,
1271 const char *buf, size_t count)
1275 if (sscanf(buf, "%d\n", &flag) != 1)
1278 rc = ap_poll_thread_start();
1282 ap_poll_thread_stop();
1287 static BUS_ATTR_RW(poll_thread);
1289 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1291 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1294 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1297 unsigned long long time;
1300 /* 120 seconds = maximum poll interval */
1301 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1302 time > 120000000000ULL)
1304 poll_timeout = time;
1305 hr_time = poll_timeout;
1307 spin_lock_bh(&ap_poll_timer_lock);
1308 hrtimer_cancel(&ap_poll_timer);
1309 hrtimer_set_expires(&ap_poll_timer, hr_time);
1310 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1311 spin_unlock_bh(&ap_poll_timer_lock);
1316 static BUS_ATTR_RW(poll_timeout);
1318 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1320 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1323 static BUS_ATTR_RO(ap_max_domain_id);
1325 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1327 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1330 static BUS_ATTR_RO(ap_max_adapter_id);
1332 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1336 if (mutex_lock_interruptible(&ap_perms_mutex))
1337 return -ERESTARTSYS;
1338 rc = scnprintf(buf, PAGE_SIZE,
1339 "0x%016lx%016lx%016lx%016lx\n",
1340 ap_perms.apm[0], ap_perms.apm[1],
1341 ap_perms.apm[2], ap_perms.apm[3]);
1342 mutex_unlock(&ap_perms_mutex);
1347 static int __verify_card_reservations(struct device_driver *drv, void *data)
1350 struct ap_driver *ap_drv = to_ap_drv(drv);
1351 unsigned long *newapm = (unsigned long *)data;
1354 * increase the driver's module refcounter to be sure it is not
1355 * going away when we invoke the callback function.
1357 if (!try_module_get(drv->owner))
1360 if (ap_drv->in_use) {
1361 rc = ap_drv->in_use(newapm, ap_perms.aqm);
1366 /* release the driver's module */
1367 module_put(drv->owner);
1372 static int apmask_commit(unsigned long *newapm)
1375 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1378 * Check if any bits in the apmask have been set which will
1379 * result in queues being removed from non-default drivers
1381 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1382 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1383 __verify_card_reservations);
1388 memcpy(ap_perms.apm, newapm, APMASKSIZE);
1393 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1396 int rc, changes = 0;
1397 DECLARE_BITMAP(newapm, AP_DEVICES);
1399 if (mutex_lock_interruptible(&ap_perms_mutex))
1400 return -ERESTARTSYS;
1402 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1406 changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
1408 rc = apmask_commit(newapm);
1411 mutex_unlock(&ap_perms_mutex);
1416 ap_bus_revise_bindings();
1417 ap_send_mask_changed_uevent(newapm, NULL);
1423 static BUS_ATTR_RW(apmask);
1425 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1429 if (mutex_lock_interruptible(&ap_perms_mutex))
1430 return -ERESTARTSYS;
1431 rc = scnprintf(buf, PAGE_SIZE,
1432 "0x%016lx%016lx%016lx%016lx\n",
1433 ap_perms.aqm[0], ap_perms.aqm[1],
1434 ap_perms.aqm[2], ap_perms.aqm[3]);
1435 mutex_unlock(&ap_perms_mutex);
1440 static int __verify_queue_reservations(struct device_driver *drv, void *data)
1443 struct ap_driver *ap_drv = to_ap_drv(drv);
1444 unsigned long *newaqm = (unsigned long *)data;
1447 * increase the driver's module refcounter to be sure it is not
1448 * going away when we invoke the callback function.
1450 if (!try_module_get(drv->owner))
1453 if (ap_drv->in_use) {
1454 rc = ap_drv->in_use(ap_perms.apm, newaqm);
1459 /* release the driver's module */
1460 module_put(drv->owner);
1465 static int aqmask_commit(unsigned long *newaqm)
1468 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1471 * Check if any bits in the aqmask have been set which will
1472 * result in queues being removed from non-default drivers
1474 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1475 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1476 __verify_queue_reservations);
1481 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1486 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1489 int rc, changes = 0;
1490 DECLARE_BITMAP(newaqm, AP_DOMAINS);
1492 if (mutex_lock_interruptible(&ap_perms_mutex))
1493 return -ERESTARTSYS;
1495 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1499 changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
1501 rc = aqmask_commit(newaqm);
1504 mutex_unlock(&ap_perms_mutex);
1509 ap_bus_revise_bindings();
1510 ap_send_mask_changed_uevent(NULL, newaqm);
1516 static BUS_ATTR_RW(aqmask);
1518 static ssize_t scans_show(struct bus_type *bus, char *buf)
1520 return scnprintf(buf, PAGE_SIZE, "%llu\n",
1521 atomic64_read(&ap_scan_bus_count));
1524 static ssize_t scans_store(struct bus_type *bus, const char *buf,
1527 AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1529 ap_bus_force_rescan();
1534 static BUS_ATTR_RW(scans);
1536 static ssize_t bindings_show(struct bus_type *bus, char *buf)
1539 unsigned int apqns, n;
1541 ap_calc_bound_apqns(&apqns, &n);
1542 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1543 rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns);
1545 rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns);
1550 static BUS_ATTR_RO(bindings);
1552 static struct attribute *ap_bus_attrs[] = {
1553 &bus_attr_ap_domain.attr,
1554 &bus_attr_ap_control_domain_mask.attr,
1555 &bus_attr_ap_usage_domain_mask.attr,
1556 &bus_attr_ap_adapter_mask.attr,
1557 &bus_attr_config_time.attr,
1558 &bus_attr_poll_thread.attr,
1559 &bus_attr_ap_interrupts.attr,
1560 &bus_attr_poll_timeout.attr,
1561 &bus_attr_ap_max_domain_id.attr,
1562 &bus_attr_ap_max_adapter_id.attr,
1563 &bus_attr_apmask.attr,
1564 &bus_attr_aqmask.attr,
1565 &bus_attr_scans.attr,
1566 &bus_attr_bindings.attr,
1569 ATTRIBUTE_GROUPS(ap_bus);
1571 static struct bus_type ap_bus_type = {
1573 .bus_groups = ap_bus_groups,
1574 .match = &ap_bus_match,
1575 .uevent = &ap_uevent,
1576 .probe = ap_device_probe,
1577 .remove = ap_device_remove,
1581 * ap_select_domain(): Select an AP domain if possible and we haven't
1582 * already done so before.
1584 static void ap_select_domain(void)
1586 struct ap_queue_status status;
1590 * Choose the default domain. Either the one specified with
1591 * the "domain=" parameter or the first domain with at least
1594 spin_lock_bh(&ap_domain_lock);
1595 if (ap_domain_index >= 0) {
1596 /* Domain has already been selected. */
1599 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1600 if (!ap_test_config_usage_domain(dom) ||
1601 !test_bit_inv(dom, ap_perms.aqm))
1603 for (card = 0; card <= ap_max_adapter_id; card++) {
1604 if (!ap_test_config_card_id(card) ||
1605 !test_bit_inv(card, ap_perms.apm))
1607 status = ap_test_queue(AP_MKQID(card, dom),
1608 ap_apft_available(),
1610 if (status.response_code == AP_RESPONSE_NORMAL)
1613 if (card <= ap_max_adapter_id)
1616 if (dom <= ap_max_domain_id) {
1617 ap_domain_index = dom;
1618 AP_DBF_INFO("%s new default domain is %d\n",
1619 __func__, ap_domain_index);
1622 spin_unlock_bh(&ap_domain_lock);
1626 * This function checks the type and returns either 0 for not
1627 * supported or the highest compatible type value (which may
1628 * include the input type value).
1630 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1634 /* < CEX2A is not supported */
1635 if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1636 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1637 __func__, AP_QID_CARD(qid),
1638 AP_QID_QUEUE(qid), rawtype);
1641 /* up to CEX8 known and fully supported */
1642 if (rawtype <= AP_DEVICE_TYPE_CEX8)
1645 * unknown new type > CEX8, check for compatibility
1646 * to the highest known and supported type which is
1647 * currently CEX8 with the help of the QACT function.
1649 if (ap_qact_available()) {
1650 struct ap_queue_status status;
1651 union ap_qact_ap_info apinfo = {0};
1653 apinfo.mode = (func >> 26) & 0x07;
1654 apinfo.cat = AP_DEVICE_TYPE_CEX8;
1655 status = ap_qact(qid, 0, &apinfo);
1656 if (status.response_code == AP_RESPONSE_NORMAL &&
1657 apinfo.cat >= AP_DEVICE_TYPE_CEX2A &&
1658 apinfo.cat <= AP_DEVICE_TYPE_CEX8)
1659 comp_type = apinfo.cat;
1662 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1663 __func__, AP_QID_CARD(qid),
1664 AP_QID_QUEUE(qid), rawtype);
1665 else if (comp_type != rawtype)
1666 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1667 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1668 rawtype, comp_type);
1673 * Helper function to be used with bus_find_dev
1674 * matches for the card device with the given id
1676 static int __match_card_device_with_id(struct device *dev, const void *data)
1678 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
1682 * Helper function to be used with bus_find_dev
1683 * matches for the queue device with a given qid
1685 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1687 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
1691 * Helper function to be used with bus_find_dev
1692 * matches any queue device with given queue id
1694 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1696 return is_queue_dev(dev) &&
1697 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
1700 /* Helper function for notify_config_changed */
1701 static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1703 struct ap_driver *ap_drv = to_ap_drv(drv);
1705 if (try_module_get(drv->owner)) {
1706 if (ap_drv->on_config_changed)
1707 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1708 module_put(drv->owner);
1714 /* Notify all drivers about an qci config change */
1715 static inline void notify_config_changed(void)
1717 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1718 __drv_notify_config_changed);
1721 /* Helper function for notify_scan_complete */
1722 static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1724 struct ap_driver *ap_drv = to_ap_drv(drv);
1726 if (try_module_get(drv->owner)) {
1727 if (ap_drv->on_scan_complete)
1728 ap_drv->on_scan_complete(ap_qci_info,
1730 module_put(drv->owner);
1736 /* Notify all drivers about bus scan complete */
1737 static inline void notify_scan_complete(void)
1739 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1740 __drv_notify_scan_complete);
1744 * Helper function for ap_scan_bus().
1745 * Remove card device and associated queue devices.
1747 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1749 bus_for_each_dev(&ap_bus_type, NULL,
1750 (void *)(long)ac->id,
1751 __ap_queue_devices_with_id_unregister);
1752 device_unregister(&ac->ap_dev.device);
1756 * Helper function for ap_scan_bus().
1757 * Does the scan bus job for all the domains within
1758 * a valid adapter given by an ap_card ptr.
1760 static inline void ap_scan_domains(struct ap_card *ac)
1762 bool decfg, chkstop;
1766 struct ap_queue *aq;
1767 int rc, dom, depth, type, ml;
1770 * Go through the configuration for the domains and compare them
1771 * to the existing queue devices. Also take care of the config
1772 * and error state for the queue devices.
1775 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1776 qid = AP_MKQID(ac->id, dom);
1777 dev = bus_find_device(&ap_bus_type, NULL,
1779 __match_queue_device_with_qid);
1780 aq = dev ? to_ap_queue(dev) : NULL;
1781 if (!ap_test_config_usage_domain(dom)) {
1783 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
1784 __func__, ac->id, dom);
1785 device_unregister(dev);
1790 /* domain is valid, get info from this APQN */
1791 if (!ap_queue_info(qid, &type, &func, &depth,
1792 &ml, &decfg, &chkstop)) {
1794 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1795 __func__, ac->id, dom);
1796 device_unregister(dev);
1801 /* if no queue device exists, create a new one */
1803 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1805 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1806 __func__, ac->id, dom);
1810 aq->config = !decfg;
1811 aq->chkstop = chkstop;
1812 dev = &aq->ap_dev.device;
1813 dev->bus = &ap_bus_type;
1814 dev->parent = &ac->ap_dev.device;
1815 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1816 /* register queue device */
1817 rc = device_register(dev);
1819 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1820 __func__, ac->id, dom);
1821 goto put_dev_and_continue;
1823 /* get it and thus adjust reference counter */
1826 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
1827 __func__, ac->id, dom);
1829 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1830 __func__, ac->id, dom);
1832 AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
1833 __func__, ac->id, dom);
1834 goto put_dev_and_continue;
1836 /* handle state changes on already existing queue device */
1837 spin_lock_bh(&aq->lock);
1838 /* checkstop state */
1839 if (chkstop && !aq->chkstop) {
1842 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1843 aq->dev_state = AP_DEV_STATE_ERROR;
1844 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1846 spin_unlock_bh(&aq->lock);
1847 AP_DBF_DBG("%s(%d,%d) queue dev checkstop on\n",
1848 __func__, ac->id, dom);
1849 /* 'receive' pending messages with -EAGAIN */
1851 goto put_dev_and_continue;
1852 } else if (!chkstop && aq->chkstop) {
1854 aq->chkstop = false;
1855 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1856 aq->dev_state = AP_DEV_STATE_OPERATING;
1857 aq->sm_state = AP_SM_STATE_RESET_START;
1859 spin_unlock_bh(&aq->lock);
1860 AP_DBF_DBG("%s(%d,%d) queue dev checkstop off\n",
1861 __func__, ac->id, dom);
1862 goto put_dev_and_continue;
1864 /* config state change */
1865 if (decfg && aq->config) {
1866 /* config off this queue device */
1868 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1869 aq->dev_state = AP_DEV_STATE_ERROR;
1870 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1872 spin_unlock_bh(&aq->lock);
1873 AP_DBF_DBG("%s(%d,%d) queue dev config off\n",
1874 __func__, ac->id, dom);
1875 ap_send_config_uevent(&aq->ap_dev, aq->config);
1876 /* 'receive' pending messages with -EAGAIN */
1878 goto put_dev_and_continue;
1879 } else if (!decfg && !aq->config) {
1880 /* config on this queue device */
1882 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1883 aq->dev_state = AP_DEV_STATE_OPERATING;
1884 aq->sm_state = AP_SM_STATE_RESET_START;
1886 spin_unlock_bh(&aq->lock);
1887 AP_DBF_DBG("%s(%d,%d) queue dev config on\n",
1888 __func__, ac->id, dom);
1889 ap_send_config_uevent(&aq->ap_dev, aq->config);
1890 goto put_dev_and_continue;
1892 /* handle other error states */
1893 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1894 spin_unlock_bh(&aq->lock);
1895 /* 'receive' pending messages with -EAGAIN */
1897 /* re-init (with reset) the queue device */
1898 ap_queue_init_state(aq);
1899 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
1900 __func__, ac->id, dom);
1901 goto put_dev_and_continue;
1903 spin_unlock_bh(&aq->lock);
1904 put_dev_and_continue:
1910 * Helper function for ap_scan_bus().
1911 * Does the scan bus job for the given adapter id.
1913 static inline void ap_scan_adapter(int ap)
1915 bool decfg, chkstop;
1920 int rc, dom, depth, type, comp_type, ml;
1922 /* Is there currently a card device for this adapter ? */
1923 dev = bus_find_device(&ap_bus_type, NULL,
1925 __match_card_device_with_id);
1926 ac = dev ? to_ap_card(dev) : NULL;
1928 /* Adapter not in configuration ? */
1929 if (!ap_test_config_card_id(ap)) {
1931 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
1933 ap_scan_rm_card_dev_and_queue_devs(ac);
1940 * Adapter ap is valid in the current configuration. So do some checks:
1941 * If no card device exists, build one. If a card device exists, check
1942 * for type and functions changed. For all this we need to find a valid
1946 for (dom = 0; dom <= ap_max_domain_id; dom++)
1947 if (ap_test_config_usage_domain(dom)) {
1948 qid = AP_MKQID(ap, dom);
1949 if (ap_queue_info(qid, &type, &func, &depth,
1950 &ml, &decfg, &chkstop))
1953 if (dom > ap_max_domain_id) {
1954 /* Could not find a valid APQN for this adapter */
1956 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
1958 ap_scan_rm_card_dev_and_queue_devs(ac);
1961 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1967 /* No apdater type info available, an unusable adapter */
1969 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
1971 ap_scan_rm_card_dev_and_queue_devs(ac);
1974 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1981 /* Check APQN against existing card device for changes */
1982 if (ac->raw_hwtype != type) {
1983 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
1984 __func__, ap, type);
1985 ap_scan_rm_card_dev_and_queue_devs(ac);
1988 } else if (ac->functions != func) {
1989 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
1990 __func__, ap, type);
1991 ap_scan_rm_card_dev_and_queue_devs(ac);
1995 /* handle checkstop state change */
1996 if (chkstop && !ac->chkstop) {
1999 AP_DBF_INFO("%s(%d) card dev checkstop on\n",
2001 } else if (!chkstop && ac->chkstop) {
2003 ac->chkstop = false;
2004 AP_DBF_INFO("%s(%d) card dev checkstop off\n",
2007 /* handle config state change */
2008 if (decfg && ac->config) {
2010 AP_DBF_INFO("%s(%d) card dev config off\n",
2012 ap_send_config_uevent(&ac->ap_dev, ac->config);
2013 } else if (!decfg && !ac->config) {
2015 AP_DBF_INFO("%s(%d) card dev config on\n",
2017 ap_send_config_uevent(&ac->ap_dev, ac->config);
2023 /* Build a new card device */
2024 comp_type = ap_get_compatible_type(qid, type, func);
2026 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
2027 __func__, ap, type);
2030 ac = ap_card_create(ap, depth, type, comp_type, func, ml);
2032 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
2036 ac->config = !decfg;
2037 ac->chkstop = chkstop;
2038 dev = &ac->ap_dev.device;
2039 dev->bus = &ap_bus_type;
2040 dev->parent = ap_root_device;
2041 dev_set_name(dev, "card%02x", ap);
2042 /* maybe enlarge ap_max_msg_size to support this card */
2043 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
2044 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
2045 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
2047 atomic_read(&ap_max_msg_size));
2049 /* Register the new card device with AP bus */
2050 rc = device_register(dev);
2052 AP_DBF_WARN("%s(%d) device_register() failed\n",
2057 /* get it and thus adjust reference counter */
2060 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
2061 __func__, ap, type, func);
2063 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
2064 __func__, ap, type, func);
2066 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
2067 __func__, ap, type, func);
2070 /* Verify the domains and the queue devices for this card */
2071 ap_scan_domains(ac);
2073 /* release the card device */
2074 put_device(&ac->ap_dev.device);
2078 * ap_get_configuration - get the host AP configuration
2080 * Stores the host AP configuration information returned from the previous call
2081 * to Query Configuration Information (QCI), then retrieves and stores the
2082 * current AP configuration returned from QCI.
2084 * Return: true if the host AP configuration changed between calls to QCI;
2085 * otherwise, return false.
2087 static bool ap_get_configuration(void)
2089 if (!ap_qci_info) /* QCI not supported */
2092 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2093 ap_fetch_qci_info(ap_qci_info);
2095 return memcmp(ap_qci_info, ap_qci_info_old,
2096 sizeof(struct ap_config_info)) != 0;
2100 * ap_scan_bus(): Scan the AP bus for new devices
2101 * Runs periodically, workqueue timer (ap_config_time)
2102 * @unused: Unused pointer.
2104 static void ap_scan_bus(struct work_struct *unused)
2106 int ap, config_changed = 0;
2108 /* config change notify */
2109 config_changed = ap_get_configuration();
2111 notify_config_changed();
2114 AP_DBF_DBG("%s running\n", __func__);
2116 /* loop over all possible adapters */
2117 for (ap = 0; ap <= ap_max_adapter_id; ap++)
2118 ap_scan_adapter(ap);
2120 /* scan complete notify */
2122 notify_scan_complete();
2124 /* check if there is at least one queue available with default domain */
2125 if (ap_domain_index >= 0) {
2126 struct device *dev =
2127 bus_find_device(&ap_bus_type, NULL,
2128 (void *)(long)ap_domain_index,
2129 __match_queue_device_with_queue_id);
2133 AP_DBF_INFO("%s no queue device with default domain %d available\n",
2134 __func__, ap_domain_index);
2137 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
2138 AP_DBF_DBG("%s init scan complete\n", __func__);
2139 ap_send_init_scan_done_uevent();
2140 ap_check_bindings_complete();
2143 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
2146 static void ap_config_timeout(struct timer_list *unused)
2148 queue_work(system_long_wq, &ap_scan_work);
2151 static int __init ap_debug_init(void)
2153 ap_dbf_info = debug_register("ap", 2, 1,
2154 DBF_MAX_SPRINTF_ARGS * sizeof(long));
2155 debug_register_view(ap_dbf_info, &debug_sprintf_view);
2156 debug_set_level(ap_dbf_info, DBF_ERR);
2161 static void __init ap_perms_init(void)
2163 /* all resources usable if no kernel parameter string given */
2164 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
2165 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2166 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2168 /* apm kernel parameter string */
2170 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
2171 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2175 /* aqm kernel parameter string */
2177 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
2178 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2184 * ap_module_init(): The module initialization code.
2186 * Initializes the module.
2188 static int __init ap_module_init(void)
2192 rc = ap_debug_init();
2196 if (!ap_instructions_available()) {
2197 pr_warn("The hardware system does not support AP instructions\n");
2201 /* init ap_queue hashtable */
2202 hash_init(ap_queues);
2204 /* set up the AP permissions (ioctls, ap and aq masks) */
2207 /* Get AP configuration data if available */
2210 /* check default domain setting */
2211 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
2212 (ap_domain_index >= 0 &&
2213 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
2214 pr_warn("%d is not a valid cryptographic domain\n",
2216 ap_domain_index = -1;
2219 /* enable interrupts if available */
2220 if (ap_interrupts_available() && ap_useirq) {
2221 rc = register_adapter_interrupt(&ap_airq);
2222 ap_irq_flag = (rc == 0);
2225 /* Create /sys/bus/ap. */
2226 rc = bus_register(&ap_bus_type);
2230 /* Create /sys/devices/ap. */
2231 ap_root_device = root_device_register("ap");
2232 rc = PTR_ERR_OR_ZERO(ap_root_device);
2235 ap_root_device->bus = &ap_bus_type;
2237 /* Setup the AP bus rescan timer. */
2238 timer_setup(&ap_config_timer, ap_config_timeout, 0);
2241 * Setup the high resultion poll timer.
2242 * If we are running under z/VM adjust polling to z/VM polling rate.
2245 poll_timeout = 1500000;
2246 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2247 ap_poll_timer.function = ap_poll_timeout;
2249 /* Start the low priority AP bus poll thread. */
2250 if (ap_thread_flag) {
2251 rc = ap_poll_thread_start();
2256 queue_work(system_long_wq, &ap_scan_work);
2261 hrtimer_cancel(&ap_poll_timer);
2262 root_device_unregister(ap_root_device);
2264 bus_unregister(&ap_bus_type);
2267 unregister_adapter_interrupt(&ap_airq);
2271 device_initcall(ap_module_init);