1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2006, 2021
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 poll_high_timeout = 250000UL;
128 * Some state machine states only require a low frequency polling.
129 * We use 25 Hz frequency for these.
131 static unsigned long poll_low_timeout = 40000000UL;
133 /* Maximum domain id, if not given via qci */
134 static int ap_max_domain_id = 15;
135 /* Maximum adapter id, if not given via qci */
136 static int ap_max_adapter_id = 63;
138 static struct bus_type ap_bus_type;
140 /* Adapter interrupt definitions */
141 static void ap_interrupt_handler(struct airq_struct *airq,
142 struct tpi_info *tpi_info);
144 static bool ap_irq_flag;
146 static struct airq_struct ap_airq = {
147 .handler = ap_interrupt_handler,
152 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
154 * Returns the address of the local-summary-indicator of the adapter
155 * interrupt handler for AP, or NULL if adapter interrupts are not
158 void *ap_airq_ptr(void)
161 return ap_airq.lsi_ptr;
166 * ap_interrupts_available(): Test if AP interrupts are available.
168 * Returns 1 if AP interrupts are available.
170 static int ap_interrupts_available(void)
172 return test_facility(65);
176 * ap_qci_available(): Test if AP configuration
177 * information can be queried via QCI subfunction.
179 * Returns 1 if subfunction PQAP(QCI) is available.
181 static int ap_qci_available(void)
183 return test_facility(12);
187 * ap_apft_available(): Test if AP facilities test (APFT)
188 * facility is available.
190 * Returns 1 if APFT is available.
192 static int ap_apft_available(void)
194 return test_facility(15);
198 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
200 * Returns 1 if the QACT subfunction is available.
202 static inline int ap_qact_available(void)
205 return ap_qci_info->qact;
210 * ap_sb_available(): Test if the AP secure binding facility is available.
212 * Returns 1 if secure binding facility is available.
214 int ap_sb_available(void)
217 return ap_qci_info->apsb;
222 * ap_fetch_qci_info(): Fetch cryptographic config info
224 * Returns the ap configuration info fetched via PQAP(QCI).
225 * On success 0 is returned, on failure a negative errno
226 * is returned, e.g. if the PQAP(QCI) instruction is not
227 * available, the return value will be -EOPNOTSUPP.
229 static inline int ap_fetch_qci_info(struct ap_config_info *info)
231 if (!ap_qci_available())
239 * ap_init_qci_info(): Allocate and query qci config info.
240 * Does also update the static variables ap_max_domain_id
241 * and ap_max_adapter_id if this info is available.
243 static void __init ap_init_qci_info(void)
245 if (!ap_qci_available()) {
246 AP_DBF_INFO("%s QCI not supported\n", __func__);
250 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
253 ap_qci_info_old = kzalloc(sizeof(*ap_qci_info_old), GFP_KERNEL);
254 if (!ap_qci_info_old) {
259 if (ap_fetch_qci_info(ap_qci_info) != 0) {
261 kfree(ap_qci_info_old);
263 ap_qci_info_old = NULL;
266 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
268 if (ap_qci_info->apxa) {
269 if (ap_qci_info->na) {
270 ap_max_adapter_id = ap_qci_info->na;
271 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
272 __func__, ap_max_adapter_id);
274 if (ap_qci_info->nd) {
275 ap_max_domain_id = ap_qci_info->nd;
276 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
277 __func__, ap_max_domain_id);
281 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
285 * ap_test_config(): helper function to extract the nrth bit
286 * within the unsigned int array field.
288 static inline int ap_test_config(unsigned int *field, unsigned int nr)
290 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
294 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
296 * Returns 0 if the card is not configured
297 * 1 if the card is configured or
298 * if the configuration information is not available
300 static inline int ap_test_config_card_id(unsigned int id)
302 if (id > ap_max_adapter_id)
305 return ap_test_config(ap_qci_info->apm, id);
310 * ap_test_config_usage_domain(): Test, whether an AP usage domain
313 * Returns 0 if the usage domain is not configured
314 * 1 if the usage domain is configured or
315 * if the configuration information is not available
317 int ap_test_config_usage_domain(unsigned int domain)
319 if (domain > ap_max_domain_id)
322 return ap_test_config(ap_qci_info->aqm, domain);
325 EXPORT_SYMBOL(ap_test_config_usage_domain);
328 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
330 * @domain AP control domain ID
332 * Returns 1 if the control domain is configured
333 * 0 in all other cases
335 int ap_test_config_ctrl_domain(unsigned int domain)
337 if (!ap_qci_info || domain > ap_max_domain_id)
339 return ap_test_config(ap_qci_info->adm, domain);
341 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
344 * ap_queue_info(): Check and get AP queue info.
345 * Returns: 1 if APQN exists and info is filled,
346 * 0 if APQN seems to exit but there is no info
347 * available (eg. caused by an asynch pending error)
348 * -1 invalid APQN, TAPQ error or AP queue status which
349 * indicates there is no APQN.
351 static int ap_queue_info(ap_qid_t qid, int *q_type, unsigned int *q_fac,
352 int *q_depth, int *q_ml, bool *q_decfg, bool *q_cstop)
354 struct ap_queue_status status;
355 struct ap_tapq_gr2 tapq_info;
359 /* make sure we don't run into a specifiation exception */
360 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
361 AP_QID_QUEUE(qid) > ap_max_domain_id)
364 /* call TAPQ on this APQN */
365 status = ap_test_queue(qid, ap_apft_available(), &tapq_info);
367 /* handle pending async error with return 'no info available' */
371 switch (status.response_code) {
372 case AP_RESPONSE_NORMAL:
373 case AP_RESPONSE_RESET_IN_PROGRESS:
374 case AP_RESPONSE_DECONFIGURED:
375 case AP_RESPONSE_CHECKSTOPPED:
376 case AP_RESPONSE_BUSY:
378 * According to the architecture in all these cases the
379 * info should be filled. All bits 0 is not possible as
380 * there is at least one of the mode bits set.
382 if (WARN_ON_ONCE(!tapq_info.value))
384 *q_type = tapq_info.at;
385 *q_fac = tapq_info.fac;
386 *q_depth = tapq_info.qd;
387 *q_ml = tapq_info.ml;
388 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
389 *q_cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
391 /* For CEX2 and CEX3 the available functions
392 * are not reflected by the facilities bits.
393 * Instead it is coded into the type. So here
394 * modify the function bits based on the type.
396 case AP_DEVICE_TYPE_CEX2A:
397 case AP_DEVICE_TYPE_CEX3A:
398 *q_fac |= 0x08000000;
400 case AP_DEVICE_TYPE_CEX2C:
401 case AP_DEVICE_TYPE_CEX3C:
402 *q_fac |= 0x10000000;
410 * A response code which indicates, there is no info available.
416 void ap_wait(enum ap_sm_wait wait)
421 case AP_SM_WAIT_AGAIN:
422 case AP_SM_WAIT_INTERRUPT:
425 if (ap_poll_kthread) {
426 wake_up(&ap_poll_wait);
430 case AP_SM_WAIT_LOW_TIMEOUT:
431 case AP_SM_WAIT_HIGH_TIMEOUT:
432 spin_lock_bh(&ap_poll_timer_lock);
433 if (!hrtimer_is_queued(&ap_poll_timer)) {
435 wait == AP_SM_WAIT_LOW_TIMEOUT ?
436 poll_low_timeout : poll_high_timeout;
437 hrtimer_forward_now(&ap_poll_timer, hr_time);
438 hrtimer_restart(&ap_poll_timer);
440 spin_unlock_bh(&ap_poll_timer_lock);
442 case AP_SM_WAIT_NONE:
449 * ap_request_timeout(): Handling of request timeouts
450 * @t: timer making this callback
452 * Handles request timeouts.
454 void ap_request_timeout(struct timer_list *t)
456 struct ap_queue *aq = from_timer(aq, t, timeout);
458 spin_lock_bh(&aq->lock);
459 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
460 spin_unlock_bh(&aq->lock);
464 * ap_poll_timeout(): AP receive polling for finished AP requests.
465 * @unused: Unused pointer.
467 * Schedules the AP tasklet using a high resolution timer.
469 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
471 tasklet_schedule(&ap_tasklet);
472 return HRTIMER_NORESTART;
476 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
477 * @airq: pointer to adapter interrupt descriptor
480 static void ap_interrupt_handler(struct airq_struct *airq,
481 struct tpi_info *tpi_info)
483 inc_irq_stat(IRQIO_APB);
484 tasklet_schedule(&ap_tasklet);
488 * ap_tasklet_fn(): Tasklet to poll all AP devices.
489 * @dummy: Unused variable
491 * Poll all AP devices on the bus.
493 static void ap_tasklet_fn(unsigned long dummy)
497 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
499 /* Reset the indicator if interrupts are used. Thus new interrupts can
500 * be received. Doing it in the beginning of the tasklet is therefor
501 * important that no requests on any AP get lost.
504 xchg(ap_airq.lsi_ptr, 0);
506 spin_lock_bh(&ap_queues_lock);
507 hash_for_each(ap_queues, bkt, aq, hnode) {
508 spin_lock_bh(&aq->lock);
509 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
510 spin_unlock_bh(&aq->lock);
512 spin_unlock_bh(&ap_queues_lock);
517 static int ap_pending_requests(void)
522 spin_lock_bh(&ap_queues_lock);
523 hash_for_each(ap_queues, bkt, aq, hnode) {
524 if (aq->queue_count == 0)
526 spin_unlock_bh(&ap_queues_lock);
529 spin_unlock_bh(&ap_queues_lock);
534 * ap_poll_thread(): Thread that polls for finished requests.
535 * @data: Unused pointer
537 * AP bus poll thread. The purpose of this thread is to poll for
538 * finished requests in a loop if there is a "free" cpu - that is
539 * a cpu that doesn't have anything better to do. The polling stops
540 * as soon as there is another task or if all messages have been
543 static int ap_poll_thread(void *data)
545 DECLARE_WAITQUEUE(wait, current);
547 set_user_nice(current, MAX_NICE);
549 while (!kthread_should_stop()) {
550 add_wait_queue(&ap_poll_wait, &wait);
551 set_current_state(TASK_INTERRUPTIBLE);
552 if (!ap_pending_requests()) {
556 set_current_state(TASK_RUNNING);
557 remove_wait_queue(&ap_poll_wait, &wait);
558 if (need_resched()) {
569 static int ap_poll_thread_start(void)
573 if (ap_irq_flag || ap_poll_kthread)
575 mutex_lock(&ap_poll_thread_mutex);
576 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
577 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
579 ap_poll_kthread = NULL;
580 mutex_unlock(&ap_poll_thread_mutex);
584 static void ap_poll_thread_stop(void)
586 if (!ap_poll_kthread)
588 mutex_lock(&ap_poll_thread_mutex);
589 kthread_stop(ap_poll_kthread);
590 ap_poll_kthread = NULL;
591 mutex_unlock(&ap_poll_thread_mutex);
594 #define is_card_dev(x) ((x)->parent == ap_root_device)
595 #define is_queue_dev(x) ((x)->parent != ap_root_device)
599 * @dev: Pointer to device
600 * @drv: Pointer to device_driver
602 * AP bus driver registration/unregistration.
604 static int ap_bus_match(struct device *dev, struct device_driver *drv)
606 struct ap_driver *ap_drv = to_ap_drv(drv);
607 struct ap_device_id *id;
610 * Compare device type of the device with the list of
611 * supported types of the device_driver.
613 for (id = ap_drv->ids; id->match_flags; id++) {
614 if (is_card_dev(dev) &&
615 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
616 id->dev_type == to_ap_dev(dev)->device_type)
618 if (is_queue_dev(dev) &&
619 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
620 id->dev_type == to_ap_dev(dev)->device_type)
627 * ap_uevent(): Uevent function for AP devices.
628 * @dev: Pointer to device
629 * @env: Pointer to kobj_uevent_env
631 * It sets up a single environment variable DEV_TYPE which contains the
632 * hardware device type.
634 static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env)
637 const struct ap_device *ap_dev = to_ap_dev(dev);
639 /* Uevents from ap bus core don't need extensions to the env */
640 if (dev == ap_root_device)
643 if (is_card_dev(dev)) {
644 struct ap_card *ac = to_ap_card(&ap_dev->device);
646 /* Set up DEV_TYPE environment variable. */
647 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
651 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
655 /* Add MODE=<accel|cca|ep11> */
656 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL))
657 rc = add_uevent_var(env, "MODE=accel");
658 else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
659 rc = add_uevent_var(env, "MODE=cca");
660 else if (ap_test_bit(&ac->functions, AP_FUNC_EP11))
661 rc = add_uevent_var(env, "MODE=ep11");
665 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
667 /* Add MODE=<accel|cca|ep11> */
668 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL))
669 rc = add_uevent_var(env, "MODE=accel");
670 else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
671 rc = add_uevent_var(env, "MODE=cca");
672 else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11))
673 rc = add_uevent_var(env, "MODE=ep11");
681 static void ap_send_init_scan_done_uevent(void)
683 char *envp[] = { "INITSCAN=done", NULL };
685 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
688 static void ap_send_bindings_complete_uevent(void)
691 char *envp[] = { "BINDINGS=complete", buf, NULL };
693 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
694 atomic64_inc_return(&ap_bindings_complete_count));
695 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
698 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
701 char *envp[] = { buf, NULL };
703 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
705 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
707 EXPORT_SYMBOL(ap_send_config_uevent);
709 void ap_send_online_uevent(struct ap_device *ap_dev, int online)
712 char *envp[] = { buf, NULL };
714 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
716 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
718 EXPORT_SYMBOL(ap_send_online_uevent);
720 static void ap_send_mask_changed_uevent(unsigned long *newapm,
721 unsigned long *newaqm)
724 char *envp[] = { buf, NULL };
727 snprintf(buf, sizeof(buf),
728 "APMASK=0x%016lx%016lx%016lx%016lx\n",
729 newapm[0], newapm[1], newapm[2], newapm[3]);
731 snprintf(buf, sizeof(buf),
732 "AQMASK=0x%016lx%016lx%016lx%016lx\n",
733 newaqm[0], newaqm[1], newaqm[2], newaqm[3]);
735 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
739 * calc # of bound APQNs
742 struct __ap_calc_ctrs {
747 static int __ap_calc_helper(struct device *dev, void *arg)
749 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg;
751 if (is_queue_dev(dev)) {
760 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
762 struct __ap_calc_ctrs ctrs;
764 memset(&ctrs, 0, sizeof(ctrs));
765 bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper);
772 * After initial ap bus scan do check if all existing APQNs are
773 * bound to device drivers.
775 static void ap_check_bindings_complete(void)
777 unsigned int apqns, bound;
779 if (atomic64_read(&ap_scan_bus_count) >= 1) {
780 ap_calc_bound_apqns(&apqns, &bound);
781 if (bound == apqns) {
782 if (!completion_done(&ap_init_apqn_bindings_complete)) {
783 complete_all(&ap_init_apqn_bindings_complete);
784 AP_DBF_INFO("%s complete\n", __func__);
786 ap_send_bindings_complete_uevent();
792 * Interface to wait for the AP bus to have done one initial ap bus
793 * scan and all detected APQNs have been bound to device drivers.
794 * If these both conditions are not fulfilled, this function blocks
795 * on a condition with wait_for_completion_interruptible_timeout().
796 * If these both conditions are fulfilled (before the timeout hits)
797 * the return value is 0. If the timeout (in jiffies) hits instead
798 * -ETIME is returned. On failures negative return values are
799 * returned to the caller.
801 int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
805 if (completion_done(&ap_init_apqn_bindings_complete))
809 l = wait_for_completion_interruptible_timeout(
810 &ap_init_apqn_bindings_complete, timeout);
812 l = wait_for_completion_interruptible(
813 &ap_init_apqn_bindings_complete);
815 return l == -ERESTARTSYS ? -EINTR : l;
816 else if (l == 0 && timeout)
821 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
823 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
825 if (is_queue_dev(dev) &&
826 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data)
827 device_unregister(dev);
831 static int __ap_revise_reserved(struct device *dev, void *dummy)
833 int rc, card, queue, devres, drvres;
835 if (is_queue_dev(dev)) {
836 card = AP_QID_CARD(to_ap_queue(dev)->qid);
837 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
838 mutex_lock(&ap_perms_mutex);
839 devres = test_bit_inv(card, ap_perms.apm) &&
840 test_bit_inv(queue, ap_perms.aqm);
841 mutex_unlock(&ap_perms_mutex);
842 drvres = to_ap_drv(dev->driver)->flags
843 & AP_DRIVER_FLAG_DEFAULT;
844 if (!!devres != !!drvres) {
845 AP_DBF_DBG("%s reprobing queue=%02x.%04x\n",
846 __func__, card, queue);
847 rc = device_reprobe(dev);
849 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
850 __func__, card, queue);
857 static void ap_bus_revise_bindings(void)
859 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
863 * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
864 * default host driver or not.
865 * @card: the APID of the adapter card to check
866 * @queue: the APQI of the queue to check
868 * Note: the ap_perms_mutex must be locked by the caller of this function.
870 * Return: an int specifying whether the AP adapter is reserved for the host (1)
873 int ap_owned_by_def_drv(int card, int queue)
877 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
880 if (test_bit_inv(card, ap_perms.apm) &&
881 test_bit_inv(queue, ap_perms.aqm))
886 EXPORT_SYMBOL(ap_owned_by_def_drv);
889 * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
890 * a set is reserved for the host drivers
892 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
893 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
895 * Note: the ap_perms_mutex must be locked by the caller of this function.
897 * Return: an int specifying whether each APQN is reserved for the host (1) or
900 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
903 int card, queue, rc = 0;
905 for (card = 0; !rc && card < AP_DEVICES; card++)
906 if (test_bit_inv(card, apm) &&
907 test_bit_inv(card, ap_perms.apm))
908 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
909 if (test_bit_inv(queue, aqm) &&
910 test_bit_inv(queue, ap_perms.aqm))
915 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
917 static int ap_device_probe(struct device *dev)
919 struct ap_device *ap_dev = to_ap_dev(dev);
920 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
921 int card, queue, devres, drvres, rc = -ENODEV;
923 if (!get_device(dev))
926 if (is_queue_dev(dev)) {
928 * If the apqn is marked as reserved/used by ap bus and
929 * default drivers, only probe with drivers with the default
930 * flag set. If it is not marked, only probe with drivers
931 * with the default flag not set.
933 card = AP_QID_CARD(to_ap_queue(dev)->qid);
934 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
935 mutex_lock(&ap_perms_mutex);
936 devres = test_bit_inv(card, ap_perms.apm) &&
937 test_bit_inv(queue, ap_perms.aqm);
938 mutex_unlock(&ap_perms_mutex);
939 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
940 if (!!devres != !!drvres)
944 /* Add queue/card to list of active queues/cards */
945 spin_lock_bh(&ap_queues_lock);
946 if (is_queue_dev(dev))
947 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
948 to_ap_queue(dev)->qid);
949 spin_unlock_bh(&ap_queues_lock);
951 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
954 spin_lock_bh(&ap_queues_lock);
955 if (is_queue_dev(dev))
956 hash_del(&to_ap_queue(dev)->hnode);
957 spin_unlock_bh(&ap_queues_lock);
959 ap_check_bindings_complete();
968 static void ap_device_remove(struct device *dev)
970 struct ap_device *ap_dev = to_ap_dev(dev);
971 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
973 /* prepare ap queue device removal */
974 if (is_queue_dev(dev))
975 ap_queue_prepare_remove(to_ap_queue(dev));
977 /* driver's chance to clean up gracefully */
979 ap_drv->remove(ap_dev);
981 /* now do the ap queue device remove */
982 if (is_queue_dev(dev))
983 ap_queue_remove(to_ap_queue(dev));
985 /* Remove queue/card from list of active queues/cards */
986 spin_lock_bh(&ap_queues_lock);
987 if (is_queue_dev(dev))
988 hash_del(&to_ap_queue(dev)->hnode);
989 spin_unlock_bh(&ap_queues_lock);
994 struct ap_queue *ap_get_qdev(ap_qid_t qid)
999 spin_lock_bh(&ap_queues_lock);
1000 hash_for_each(ap_queues, bkt, aq, hnode) {
1001 if (aq->qid == qid) {
1002 get_device(&aq->ap_dev.device);
1003 spin_unlock_bh(&ap_queues_lock);
1007 spin_unlock_bh(&ap_queues_lock);
1011 EXPORT_SYMBOL(ap_get_qdev);
1013 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1016 struct device_driver *drv = &ap_drv->driver;
1018 drv->bus = &ap_bus_type;
1021 return driver_register(drv);
1023 EXPORT_SYMBOL(ap_driver_register);
1025 void ap_driver_unregister(struct ap_driver *ap_drv)
1027 driver_unregister(&ap_drv->driver);
1029 EXPORT_SYMBOL(ap_driver_unregister);
1031 void ap_bus_force_rescan(void)
1033 /* processing a asynchronous bus rescan */
1034 del_timer(&ap_config_timer);
1035 queue_work(system_long_wq, &ap_scan_work);
1036 flush_work(&ap_scan_work);
1038 EXPORT_SYMBOL(ap_bus_force_rescan);
1041 * A config change has happened, force an ap bus rescan.
1043 void ap_bus_cfg_chg(void)
1045 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
1047 ap_bus_force_rescan();
1051 * hex2bitmap() - parse hex mask string and set bitmap.
1052 * Valid strings are "0x012345678" with at least one valid hex number.
1053 * Rest of the bitmap to the right is padded with 0. No spaces allowed
1054 * within the string, the leading 0x may be omitted.
1055 * Returns the bitmask with exactly the bits set as given by the hex
1056 * string (both in big endian order).
1058 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1062 /* bits needs to be a multiple of 8 */
1066 if (str[0] == '0' && str[1] == 'x')
1071 for (i = 0; isxdigit(*str) && i < bits; str++) {
1072 b = hex_to_bin(*str);
1073 for (n = 0; n < 4; n++)
1074 if (b & (0x08 >> n))
1075 set_bit_inv(i + n, bitmap);
1087 * modify_bitmap() - parse bitmask argument and modify an existing
1088 * bit mask accordingly. A concatenation (done with ',') of these
1089 * terms is recognized:
1090 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1091 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1092 * 0...bits-1; the leading + or - is required. Here are some examples:
1093 * +0-15,+32,-128,-0xFF
1094 * -0-255,+1-16,+0x128
1095 * +1,+2,+3,+4,-5,-7-10
1096 * Returns the new bitmap after all changes have been applied. Every
1097 * positive value in the string will set a bit and every negative value
1098 * in the string will clear a bit. As a bit may be touched more than once,
1099 * the last 'operation' wins:
1100 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1101 * cleared again. All other bits are unmodified.
1103 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1108 /* bits needs to be a multiple of 8 */
1114 if (sign != '+' && sign != '-')
1116 a = z = simple_strtoul(str, &np, 0);
1117 if (str == np || a >= bits)
1121 z = simple_strtoul(++str, &np, 0);
1122 if (str == np || a > z || z >= bits)
1126 for (i = a; i <= z; i++)
1128 set_bit_inv(i, bitmap);
1130 clear_bit_inv(i, bitmap);
1131 while (*str == ',' || *str == '\n')
1138 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1139 unsigned long *newmap)
1144 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1145 if (*str == '+' || *str == '-') {
1146 memcpy(newmap, bitmap, size);
1147 rc = modify_bitmap(str, newmap, bits);
1149 memset(newmap, 0, size);
1150 rc = hex2bitmap(str, newmap, bits);
1155 int ap_parse_mask_str(const char *str,
1156 unsigned long *bitmap, int bits,
1159 unsigned long *newmap, size;
1162 /* bits needs to be a multiple of 8 */
1166 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1167 newmap = kmalloc(size, GFP_KERNEL);
1170 if (mutex_lock_interruptible(lock)) {
1172 return -ERESTARTSYS;
1174 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
1176 memcpy(bitmap, newmap, size);
1181 EXPORT_SYMBOL(ap_parse_mask_str);
1184 * AP bus attributes.
1187 static ssize_t ap_domain_show(const struct bus_type *bus, char *buf)
1189 return sysfs_emit(buf, "%d\n", ap_domain_index);
1192 static ssize_t ap_domain_store(const struct bus_type *bus,
1193 const char *buf, size_t count)
1197 if (sscanf(buf, "%i\n", &domain) != 1 ||
1198 domain < 0 || domain > ap_max_domain_id ||
1199 !test_bit_inv(domain, ap_perms.aqm))
1202 spin_lock_bh(&ap_domain_lock);
1203 ap_domain_index = domain;
1204 spin_unlock_bh(&ap_domain_lock);
1206 AP_DBF_INFO("%s stored new default domain=%d\n",
1212 static BUS_ATTR_RW(ap_domain);
1214 static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf)
1216 if (!ap_qci_info) /* QCI not supported */
1217 return sysfs_emit(buf, "not supported\n");
1219 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1220 ap_qci_info->adm[0], ap_qci_info->adm[1],
1221 ap_qci_info->adm[2], ap_qci_info->adm[3],
1222 ap_qci_info->adm[4], ap_qci_info->adm[5],
1223 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1226 static BUS_ATTR_RO(ap_control_domain_mask);
1228 static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf)
1230 if (!ap_qci_info) /* QCI not supported */
1231 return sysfs_emit(buf, "not supported\n");
1233 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1234 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1235 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1236 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1237 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1240 static BUS_ATTR_RO(ap_usage_domain_mask);
1242 static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf)
1244 if (!ap_qci_info) /* QCI not supported */
1245 return sysfs_emit(buf, "not supported\n");
1247 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1248 ap_qci_info->apm[0], ap_qci_info->apm[1],
1249 ap_qci_info->apm[2], ap_qci_info->apm[3],
1250 ap_qci_info->apm[4], ap_qci_info->apm[5],
1251 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1254 static BUS_ATTR_RO(ap_adapter_mask);
1256 static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf)
1258 return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0);
1261 static BUS_ATTR_RO(ap_interrupts);
1263 static ssize_t config_time_show(const struct bus_type *bus, char *buf)
1265 return sysfs_emit(buf, "%d\n", ap_config_time);
1268 static ssize_t config_time_store(const struct bus_type *bus,
1269 const char *buf, size_t count)
1273 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1275 ap_config_time = time;
1276 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1280 static BUS_ATTR_RW(config_time);
1282 static ssize_t poll_thread_show(const struct bus_type *bus, char *buf)
1284 return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0);
1287 static ssize_t poll_thread_store(const struct bus_type *bus,
1288 const char *buf, size_t count)
1293 rc = kstrtobool(buf, &value);
1298 rc = ap_poll_thread_start();
1302 ap_poll_thread_stop();
1307 static BUS_ATTR_RW(poll_thread);
1309 static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf)
1311 return sysfs_emit(buf, "%lu\n", poll_high_timeout);
1314 static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf,
1317 unsigned long value;
1321 rc = kstrtoul(buf, 0, &value);
1325 /* 120 seconds = maximum poll interval */
1326 if (value > 120000000000UL)
1328 poll_high_timeout = value;
1329 hr_time = poll_high_timeout;
1331 spin_lock_bh(&ap_poll_timer_lock);
1332 hrtimer_cancel(&ap_poll_timer);
1333 hrtimer_set_expires(&ap_poll_timer, hr_time);
1334 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1335 spin_unlock_bh(&ap_poll_timer_lock);
1340 static BUS_ATTR_RW(poll_timeout);
1342 static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf)
1344 return sysfs_emit(buf, "%d\n", ap_max_domain_id);
1347 static BUS_ATTR_RO(ap_max_domain_id);
1349 static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf)
1351 return sysfs_emit(buf, "%d\n", ap_max_adapter_id);
1354 static BUS_ATTR_RO(ap_max_adapter_id);
1356 static ssize_t apmask_show(const struct bus_type *bus, char *buf)
1360 if (mutex_lock_interruptible(&ap_perms_mutex))
1361 return -ERESTARTSYS;
1362 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1363 ap_perms.apm[0], ap_perms.apm[1],
1364 ap_perms.apm[2], ap_perms.apm[3]);
1365 mutex_unlock(&ap_perms_mutex);
1370 static int __verify_card_reservations(struct device_driver *drv, void *data)
1373 struct ap_driver *ap_drv = to_ap_drv(drv);
1374 unsigned long *newapm = (unsigned long *)data;
1377 * increase the driver's module refcounter to be sure it is not
1378 * going away when we invoke the callback function.
1380 if (!try_module_get(drv->owner))
1383 if (ap_drv->in_use) {
1384 rc = ap_drv->in_use(newapm, ap_perms.aqm);
1389 /* release the driver's module */
1390 module_put(drv->owner);
1395 static int apmask_commit(unsigned long *newapm)
1398 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1401 * Check if any bits in the apmask have been set which will
1402 * result in queues being removed from non-default drivers
1404 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1405 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1406 __verify_card_reservations);
1411 memcpy(ap_perms.apm, newapm, APMASKSIZE);
1416 static ssize_t apmask_store(const struct bus_type *bus, const char *buf,
1419 int rc, changes = 0;
1420 DECLARE_BITMAP(newapm, AP_DEVICES);
1422 if (mutex_lock_interruptible(&ap_perms_mutex))
1423 return -ERESTARTSYS;
1425 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1429 changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
1431 rc = apmask_commit(newapm);
1434 mutex_unlock(&ap_perms_mutex);
1439 ap_bus_revise_bindings();
1440 ap_send_mask_changed_uevent(newapm, NULL);
1446 static BUS_ATTR_RW(apmask);
1448 static ssize_t aqmask_show(const struct bus_type *bus, char *buf)
1452 if (mutex_lock_interruptible(&ap_perms_mutex))
1453 return -ERESTARTSYS;
1454 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1455 ap_perms.aqm[0], ap_perms.aqm[1],
1456 ap_perms.aqm[2], ap_perms.aqm[3]);
1457 mutex_unlock(&ap_perms_mutex);
1462 static int __verify_queue_reservations(struct device_driver *drv, void *data)
1465 struct ap_driver *ap_drv = to_ap_drv(drv);
1466 unsigned long *newaqm = (unsigned long *)data;
1469 * increase the driver's module refcounter to be sure it is not
1470 * going away when we invoke the callback function.
1472 if (!try_module_get(drv->owner))
1475 if (ap_drv->in_use) {
1476 rc = ap_drv->in_use(ap_perms.apm, newaqm);
1481 /* release the driver's module */
1482 module_put(drv->owner);
1487 static int aqmask_commit(unsigned long *newaqm)
1490 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1493 * Check if any bits in the aqmask have been set which will
1494 * result in queues being removed from non-default drivers
1496 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1497 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1498 __verify_queue_reservations);
1503 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1508 static ssize_t aqmask_store(const struct bus_type *bus, const char *buf,
1511 int rc, changes = 0;
1512 DECLARE_BITMAP(newaqm, AP_DOMAINS);
1514 if (mutex_lock_interruptible(&ap_perms_mutex))
1515 return -ERESTARTSYS;
1517 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1521 changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
1523 rc = aqmask_commit(newaqm);
1526 mutex_unlock(&ap_perms_mutex);
1531 ap_bus_revise_bindings();
1532 ap_send_mask_changed_uevent(NULL, newaqm);
1538 static BUS_ATTR_RW(aqmask);
1540 static ssize_t scans_show(const struct bus_type *bus, char *buf)
1542 return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count));
1545 static ssize_t scans_store(const struct bus_type *bus, const char *buf,
1548 AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1550 ap_bus_force_rescan();
1555 static BUS_ATTR_RW(scans);
1557 static ssize_t bindings_show(const struct bus_type *bus, char *buf)
1560 unsigned int apqns, n;
1562 ap_calc_bound_apqns(&apqns, &n);
1563 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1564 rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns);
1566 rc = sysfs_emit(buf, "%u/%u\n", n, apqns);
1571 static BUS_ATTR_RO(bindings);
1573 static ssize_t features_show(const struct bus_type *bus, char *buf)
1577 if (!ap_qci_info) /* QCI not supported */
1578 return sysfs_emit(buf, "-\n");
1580 if (ap_qci_info->apsc)
1581 n += sysfs_emit_at(buf, n, "APSC ");
1582 if (ap_qci_info->apxa)
1583 n += sysfs_emit_at(buf, n, "APXA ");
1584 if (ap_qci_info->qact)
1585 n += sysfs_emit_at(buf, n, "QACT ");
1586 if (ap_qci_info->rc8a)
1587 n += sysfs_emit_at(buf, n, "RC8A ");
1588 if (ap_qci_info->apsb)
1589 n += sysfs_emit_at(buf, n, "APSB ");
1591 sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n");
1596 static BUS_ATTR_RO(features);
1598 static struct attribute *ap_bus_attrs[] = {
1599 &bus_attr_ap_domain.attr,
1600 &bus_attr_ap_control_domain_mask.attr,
1601 &bus_attr_ap_usage_domain_mask.attr,
1602 &bus_attr_ap_adapter_mask.attr,
1603 &bus_attr_config_time.attr,
1604 &bus_attr_poll_thread.attr,
1605 &bus_attr_ap_interrupts.attr,
1606 &bus_attr_poll_timeout.attr,
1607 &bus_attr_ap_max_domain_id.attr,
1608 &bus_attr_ap_max_adapter_id.attr,
1609 &bus_attr_apmask.attr,
1610 &bus_attr_aqmask.attr,
1611 &bus_attr_scans.attr,
1612 &bus_attr_bindings.attr,
1613 &bus_attr_features.attr,
1616 ATTRIBUTE_GROUPS(ap_bus);
1618 static struct bus_type ap_bus_type = {
1620 .bus_groups = ap_bus_groups,
1621 .match = &ap_bus_match,
1622 .uevent = &ap_uevent,
1623 .probe = ap_device_probe,
1624 .remove = ap_device_remove,
1628 * ap_select_domain(): Select an AP domain if possible and we haven't
1629 * already done so before.
1631 static void ap_select_domain(void)
1633 struct ap_queue_status status;
1637 * Choose the default domain. Either the one specified with
1638 * the "domain=" parameter or the first domain with at least
1641 spin_lock_bh(&ap_domain_lock);
1642 if (ap_domain_index >= 0) {
1643 /* Domain has already been selected. */
1646 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1647 if (!ap_test_config_usage_domain(dom) ||
1648 !test_bit_inv(dom, ap_perms.aqm))
1650 for (card = 0; card <= ap_max_adapter_id; card++) {
1651 if (!ap_test_config_card_id(card) ||
1652 !test_bit_inv(card, ap_perms.apm))
1654 status = ap_test_queue(AP_MKQID(card, dom),
1655 ap_apft_available(),
1657 if (status.response_code == AP_RESPONSE_NORMAL)
1660 if (card <= ap_max_adapter_id)
1663 if (dom <= ap_max_domain_id) {
1664 ap_domain_index = dom;
1665 AP_DBF_INFO("%s new default domain is %d\n",
1666 __func__, ap_domain_index);
1669 spin_unlock_bh(&ap_domain_lock);
1673 * This function checks the type and returns either 0 for not
1674 * supported or the highest compatible type value (which may
1675 * include the input type value).
1677 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1681 /* < CEX2A is not supported */
1682 if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1683 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1684 __func__, AP_QID_CARD(qid),
1685 AP_QID_QUEUE(qid), rawtype);
1688 /* up to CEX8 known and fully supported */
1689 if (rawtype <= AP_DEVICE_TYPE_CEX8)
1692 * unknown new type > CEX8, check for compatibility
1693 * to the highest known and supported type which is
1694 * currently CEX8 with the help of the QACT function.
1696 if (ap_qact_available()) {
1697 struct ap_queue_status status;
1698 union ap_qact_ap_info apinfo = {0};
1700 apinfo.mode = (func >> 26) & 0x07;
1701 apinfo.cat = AP_DEVICE_TYPE_CEX8;
1702 status = ap_qact(qid, 0, &apinfo);
1703 if (status.response_code == AP_RESPONSE_NORMAL &&
1704 apinfo.cat >= AP_DEVICE_TYPE_CEX2A &&
1705 apinfo.cat <= AP_DEVICE_TYPE_CEX8)
1706 comp_type = apinfo.cat;
1709 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1710 __func__, AP_QID_CARD(qid),
1711 AP_QID_QUEUE(qid), rawtype);
1712 else if (comp_type != rawtype)
1713 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1714 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1715 rawtype, comp_type);
1720 * Helper function to be used with bus_find_dev
1721 * matches for the card device with the given id
1723 static int __match_card_device_with_id(struct device *dev, const void *data)
1725 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
1729 * Helper function to be used with bus_find_dev
1730 * matches for the queue device with a given qid
1732 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1734 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
1738 * Helper function to be used with bus_find_dev
1739 * matches any queue device with given queue id
1741 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1743 return is_queue_dev(dev) &&
1744 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
1747 /* Helper function for notify_config_changed */
1748 static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1750 struct ap_driver *ap_drv = to_ap_drv(drv);
1752 if (try_module_get(drv->owner)) {
1753 if (ap_drv->on_config_changed)
1754 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1755 module_put(drv->owner);
1761 /* Notify all drivers about an qci config change */
1762 static inline void notify_config_changed(void)
1764 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1765 __drv_notify_config_changed);
1768 /* Helper function for notify_scan_complete */
1769 static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1771 struct ap_driver *ap_drv = to_ap_drv(drv);
1773 if (try_module_get(drv->owner)) {
1774 if (ap_drv->on_scan_complete)
1775 ap_drv->on_scan_complete(ap_qci_info,
1777 module_put(drv->owner);
1783 /* Notify all drivers about bus scan complete */
1784 static inline void notify_scan_complete(void)
1786 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1787 __drv_notify_scan_complete);
1791 * Helper function for ap_scan_bus().
1792 * Remove card device and associated queue devices.
1794 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1796 bus_for_each_dev(&ap_bus_type, NULL,
1797 (void *)(long)ac->id,
1798 __ap_queue_devices_with_id_unregister);
1799 device_unregister(&ac->ap_dev.device);
1803 * Helper function for ap_scan_bus().
1804 * Does the scan bus job for all the domains within
1805 * a valid adapter given by an ap_card ptr.
1807 static inline void ap_scan_domains(struct ap_card *ac)
1809 int rc, dom, depth, type, ml;
1810 bool decfg, chkstop;
1811 struct ap_queue *aq;
1817 * Go through the configuration for the domains and compare them
1818 * to the existing queue devices. Also take care of the config
1819 * and error state for the queue devices.
1822 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1823 qid = AP_MKQID(ac->id, dom);
1824 dev = bus_find_device(&ap_bus_type, NULL,
1826 __match_queue_device_with_qid);
1827 aq = dev ? to_ap_queue(dev) : NULL;
1828 if (!ap_test_config_usage_domain(dom)) {
1830 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
1831 __func__, ac->id, dom);
1832 device_unregister(dev);
1834 goto put_dev_and_continue;
1836 /* domain is valid, get info from this APQN */
1837 rc = ap_queue_info(qid, &type, &func, &depth,
1838 &ml, &decfg, &chkstop);
1842 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1843 __func__, ac->id, dom);
1844 device_unregister(dev);
1848 goto put_dev_and_continue;
1852 /* if no queue device exists, create a new one */
1854 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1856 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1857 __func__, ac->id, dom);
1861 aq->config = !decfg;
1862 aq->chkstop = chkstop;
1863 dev = &aq->ap_dev.device;
1864 dev->bus = &ap_bus_type;
1865 dev->parent = &ac->ap_dev.device;
1866 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1867 /* register queue device */
1868 rc = device_register(dev);
1870 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1871 __func__, ac->id, dom);
1872 goto put_dev_and_continue;
1874 /* get it and thus adjust reference counter */
1877 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
1878 __func__, ac->id, dom);
1880 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1881 __func__, ac->id, dom);
1883 AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
1884 __func__, ac->id, dom);
1885 goto put_dev_and_continue;
1887 /* handle state changes on already existing queue device */
1888 spin_lock_bh(&aq->lock);
1889 /* checkstop state */
1890 if (chkstop && !aq->chkstop) {
1893 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1894 aq->dev_state = AP_DEV_STATE_ERROR;
1895 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1897 spin_unlock_bh(&aq->lock);
1898 AP_DBF_DBG("%s(%d,%d) queue dev checkstop on\n",
1899 __func__, ac->id, dom);
1900 /* 'receive' pending messages with -EAGAIN */
1902 goto put_dev_and_continue;
1903 } else if (!chkstop && aq->chkstop) {
1905 aq->chkstop = false;
1906 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1907 aq->dev_state = AP_DEV_STATE_OPERATING;
1908 aq->sm_state = AP_SM_STATE_RESET_START;
1910 spin_unlock_bh(&aq->lock);
1911 AP_DBF_DBG("%s(%d,%d) queue dev checkstop off\n",
1912 __func__, ac->id, dom);
1913 goto put_dev_and_continue;
1915 /* config state change */
1916 if (decfg && aq->config) {
1917 /* config off this queue device */
1919 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1920 aq->dev_state = AP_DEV_STATE_ERROR;
1921 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1923 spin_unlock_bh(&aq->lock);
1924 AP_DBF_DBG("%s(%d,%d) queue dev config off\n",
1925 __func__, ac->id, dom);
1926 ap_send_config_uevent(&aq->ap_dev, aq->config);
1927 /* 'receive' pending messages with -EAGAIN */
1929 goto put_dev_and_continue;
1930 } else if (!decfg && !aq->config) {
1931 /* config on this queue device */
1933 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1934 aq->dev_state = AP_DEV_STATE_OPERATING;
1935 aq->sm_state = AP_SM_STATE_RESET_START;
1937 spin_unlock_bh(&aq->lock);
1938 AP_DBF_DBG("%s(%d,%d) queue dev config on\n",
1939 __func__, ac->id, dom);
1940 ap_send_config_uevent(&aq->ap_dev, aq->config);
1941 goto put_dev_and_continue;
1943 /* handle other error states */
1944 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1945 spin_unlock_bh(&aq->lock);
1946 /* 'receive' pending messages with -EAGAIN */
1948 /* re-init (with reset) the queue device */
1949 ap_queue_init_state(aq);
1950 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
1951 __func__, ac->id, dom);
1952 goto put_dev_and_continue;
1954 spin_unlock_bh(&aq->lock);
1955 put_dev_and_continue:
1961 * Helper function for ap_scan_bus().
1962 * Does the scan bus job for the given adapter id.
1964 static inline void ap_scan_adapter(int ap)
1966 int rc, dom, depth, type, comp_type, ml;
1967 bool decfg, chkstop;
1973 /* Is there currently a card device for this adapter ? */
1974 dev = bus_find_device(&ap_bus_type, NULL,
1976 __match_card_device_with_id);
1977 ac = dev ? to_ap_card(dev) : NULL;
1979 /* Adapter not in configuration ? */
1980 if (!ap_test_config_card_id(ap)) {
1982 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
1984 ap_scan_rm_card_dev_and_queue_devs(ac);
1991 * Adapter ap is valid in the current configuration. So do some checks:
1992 * If no card device exists, build one. If a card device exists, check
1993 * for type and functions changed. For all this we need to find a valid
1997 for (dom = 0; dom <= ap_max_domain_id; dom++)
1998 if (ap_test_config_usage_domain(dom)) {
1999 qid = AP_MKQID(ap, dom);
2000 if (ap_queue_info(qid, &type, &func, &depth,
2001 &ml, &decfg, &chkstop) > 0)
2004 if (dom > ap_max_domain_id) {
2005 /* Could not find one valid APQN for this adapter */
2007 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
2009 ap_scan_rm_card_dev_and_queue_devs(ac);
2012 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
2018 /* No apdater type info available, an unusable adapter */
2020 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
2022 ap_scan_rm_card_dev_and_queue_devs(ac);
2025 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
2031 /* Check APQN against existing card device for changes */
2032 if (ac->raw_hwtype != type) {
2033 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
2034 __func__, ap, type);
2035 ap_scan_rm_card_dev_and_queue_devs(ac);
2038 } else if ((ac->functions & TAPQ_CARD_FUNC_CMP_MASK) !=
2039 (func & TAPQ_CARD_FUNC_CMP_MASK)) {
2040 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
2041 __func__, ap, func);
2042 ap_scan_rm_card_dev_and_queue_devs(ac);
2046 /* handle checkstop state change */
2047 if (chkstop && !ac->chkstop) {
2050 AP_DBF_INFO("%s(%d) card dev checkstop on\n",
2052 } else if (!chkstop && ac->chkstop) {
2054 ac->chkstop = false;
2055 AP_DBF_INFO("%s(%d) card dev checkstop off\n",
2058 /* handle config state change */
2059 if (decfg && ac->config) {
2061 AP_DBF_INFO("%s(%d) card dev config off\n",
2063 ap_send_config_uevent(&ac->ap_dev, ac->config);
2064 } else if (!decfg && !ac->config) {
2066 AP_DBF_INFO("%s(%d) card dev config on\n",
2068 ap_send_config_uevent(&ac->ap_dev, ac->config);
2074 /* Build a new card device */
2075 comp_type = ap_get_compatible_type(qid, type, func);
2077 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
2078 __func__, ap, type);
2081 ac = ap_card_create(ap, depth, type, comp_type, func, ml);
2083 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
2087 ac->config = !decfg;
2088 ac->chkstop = chkstop;
2089 dev = &ac->ap_dev.device;
2090 dev->bus = &ap_bus_type;
2091 dev->parent = ap_root_device;
2092 dev_set_name(dev, "card%02x", ap);
2093 /* maybe enlarge ap_max_msg_size to support this card */
2094 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
2095 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
2096 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
2098 atomic_read(&ap_max_msg_size));
2100 /* Register the new card device with AP bus */
2101 rc = device_register(dev);
2103 AP_DBF_WARN("%s(%d) device_register() failed\n",
2108 /* get it and thus adjust reference counter */
2111 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
2112 __func__, ap, type, func);
2114 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
2115 __func__, ap, type, func);
2117 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
2118 __func__, ap, type, func);
2121 /* Verify the domains and the queue devices for this card */
2122 ap_scan_domains(ac);
2124 /* release the card device */
2125 put_device(&ac->ap_dev.device);
2129 * ap_get_configuration - get the host AP configuration
2131 * Stores the host AP configuration information returned from the previous call
2132 * to Query Configuration Information (QCI), then retrieves and stores the
2133 * current AP configuration returned from QCI.
2135 * Return: true if the host AP configuration changed between calls to QCI;
2136 * otherwise, return false.
2138 static bool ap_get_configuration(void)
2140 if (!ap_qci_info) /* QCI not supported */
2143 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2144 ap_fetch_qci_info(ap_qci_info);
2146 return memcmp(ap_qci_info, ap_qci_info_old,
2147 sizeof(struct ap_config_info)) != 0;
2151 * ap_scan_bus(): Scan the AP bus for new devices
2152 * Runs periodically, workqueue timer (ap_config_time)
2153 * @unused: Unused pointer.
2155 static void ap_scan_bus(struct work_struct *unused)
2157 int ap, config_changed = 0;
2159 /* config change notify */
2160 config_changed = ap_get_configuration();
2162 notify_config_changed();
2165 AP_DBF_DBG("%s running\n", __func__);
2167 /* loop over all possible adapters */
2168 for (ap = 0; ap <= ap_max_adapter_id; ap++)
2169 ap_scan_adapter(ap);
2171 /* scan complete notify */
2173 notify_scan_complete();
2175 /* check if there is at least one queue available with default domain */
2176 if (ap_domain_index >= 0) {
2177 struct device *dev =
2178 bus_find_device(&ap_bus_type, NULL,
2179 (void *)(long)ap_domain_index,
2180 __match_queue_device_with_queue_id);
2184 AP_DBF_INFO("%s no queue device with default domain %d available\n",
2185 __func__, ap_domain_index);
2188 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
2189 AP_DBF_DBG("%s init scan complete\n", __func__);
2190 ap_send_init_scan_done_uevent();
2191 ap_check_bindings_complete();
2194 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
2197 static void ap_config_timeout(struct timer_list *unused)
2199 queue_work(system_long_wq, &ap_scan_work);
2202 static int __init ap_debug_init(void)
2204 ap_dbf_info = debug_register("ap", 2, 1,
2205 DBF_MAX_SPRINTF_ARGS * sizeof(long));
2206 debug_register_view(ap_dbf_info, &debug_sprintf_view);
2207 debug_set_level(ap_dbf_info, DBF_ERR);
2212 static void __init ap_perms_init(void)
2214 /* all resources usable if no kernel parameter string given */
2215 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
2216 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2217 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2219 /* apm kernel parameter string */
2221 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
2222 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2226 /* aqm kernel parameter string */
2228 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
2229 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2235 * ap_module_init(): The module initialization code.
2237 * Initializes the module.
2239 static int __init ap_module_init(void)
2243 rc = ap_debug_init();
2247 if (!ap_instructions_available()) {
2248 pr_warn("The hardware system does not support AP instructions\n");
2252 /* init ap_queue hashtable */
2253 hash_init(ap_queues);
2255 /* set up the AP permissions (ioctls, ap and aq masks) */
2258 /* Get AP configuration data if available */
2261 /* check default domain setting */
2262 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
2263 (ap_domain_index >= 0 &&
2264 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
2265 pr_warn("%d is not a valid cryptographic domain\n",
2267 ap_domain_index = -1;
2270 /* enable interrupts if available */
2271 if (ap_interrupts_available() && ap_useirq) {
2272 rc = register_adapter_interrupt(&ap_airq);
2273 ap_irq_flag = (rc == 0);
2276 /* Create /sys/bus/ap. */
2277 rc = bus_register(&ap_bus_type);
2281 /* Create /sys/devices/ap. */
2282 ap_root_device = root_device_register("ap");
2283 rc = PTR_ERR_OR_ZERO(ap_root_device);
2286 ap_root_device->bus = &ap_bus_type;
2288 /* Setup the AP bus rescan timer. */
2289 timer_setup(&ap_config_timer, ap_config_timeout, 0);
2292 * Setup the high resultion poll timer.
2293 * If we are running under z/VM adjust polling to z/VM polling rate.
2296 poll_high_timeout = 1500000;
2297 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2298 ap_poll_timer.function = ap_poll_timeout;
2300 /* Start the low priority AP bus poll thread. */
2301 if (ap_thread_flag) {
2302 rc = ap_poll_thread_start();
2307 queue_work(system_long_wq, &ap_scan_work);
2312 hrtimer_cancel(&ap_poll_timer);
2313 root_device_unregister(ap_root_device);
2315 bus_unregister(&ap_bus_type);
2318 unregister_adapter_interrupt(&ap_airq);
2322 device_initcall(ap_module_init);