1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2006, 2023
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_is_se_guest(): Check for SE guest with AP pass-through support.
224 bool ap_is_se_guest(void)
226 return is_prot_virt_guest() && ap_sb_available();
228 EXPORT_SYMBOL(ap_is_se_guest);
231 * ap_fetch_qci_info(): Fetch cryptographic config info
233 * Returns the ap configuration info fetched via PQAP(QCI).
234 * On success 0 is returned, on failure a negative errno
235 * is returned, e.g. if the PQAP(QCI) instruction is not
236 * available, the return value will be -EOPNOTSUPP.
238 static inline int ap_fetch_qci_info(struct ap_config_info *info)
240 if (!ap_qci_available())
248 * ap_init_qci_info(): Allocate and query qci config info.
249 * Does also update the static variables ap_max_domain_id
250 * and ap_max_adapter_id if this info is available.
252 static void __init ap_init_qci_info(void)
254 if (!ap_qci_available()) {
255 AP_DBF_INFO("%s QCI not supported\n", __func__);
259 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
262 ap_qci_info_old = kzalloc(sizeof(*ap_qci_info_old), GFP_KERNEL);
263 if (!ap_qci_info_old) {
268 if (ap_fetch_qci_info(ap_qci_info) != 0) {
270 kfree(ap_qci_info_old);
272 ap_qci_info_old = NULL;
275 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
277 if (ap_qci_info->apxa) {
278 if (ap_qci_info->na) {
279 ap_max_adapter_id = ap_qci_info->na;
280 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
281 __func__, ap_max_adapter_id);
283 if (ap_qci_info->nd) {
284 ap_max_domain_id = ap_qci_info->nd;
285 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
286 __func__, ap_max_domain_id);
290 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
294 * ap_test_config(): helper function to extract the nrth bit
295 * within the unsigned int array field.
297 static inline int ap_test_config(unsigned int *field, unsigned int nr)
299 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
303 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
305 * Returns 0 if the card is not configured
306 * 1 if the card is configured or
307 * if the configuration information is not available
309 static inline int ap_test_config_card_id(unsigned int id)
311 if (id > ap_max_adapter_id)
314 return ap_test_config(ap_qci_info->apm, id);
319 * ap_test_config_usage_domain(): Test, whether an AP usage domain
322 * Returns 0 if the usage domain is not configured
323 * 1 if the usage domain is configured or
324 * if the configuration information is not available
326 int ap_test_config_usage_domain(unsigned int domain)
328 if (domain > ap_max_domain_id)
331 return ap_test_config(ap_qci_info->aqm, domain);
334 EXPORT_SYMBOL(ap_test_config_usage_domain);
337 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
339 * @domain AP control domain ID
341 * Returns 1 if the control domain is configured
342 * 0 in all other cases
344 int ap_test_config_ctrl_domain(unsigned int domain)
346 if (!ap_qci_info || domain > ap_max_domain_id)
348 return ap_test_config(ap_qci_info->adm, domain);
350 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
353 * ap_queue_info(): Check and get AP queue info.
354 * Returns: 1 if APQN exists and info is filled,
355 * 0 if APQN seems to exist but there is no info
356 * available (eg. caused by an asynch pending error)
357 * -1 invalid APQN, TAPQ error or AP queue status which
358 * indicates there is no APQN.
360 static int ap_queue_info(ap_qid_t qid, int *q_type, unsigned int *q_fac,
361 int *q_depth, int *q_ml, bool *q_decfg, bool *q_cstop)
363 struct ap_queue_status status;
364 struct ap_tapq_gr2 tapq_info;
368 /* make sure we don't run into a specifiation exception */
369 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
370 AP_QID_QUEUE(qid) > ap_max_domain_id)
373 /* call TAPQ on this APQN */
374 status = ap_test_queue(qid, ap_apft_available(), &tapq_info);
376 switch (status.response_code) {
377 case AP_RESPONSE_NORMAL:
378 case AP_RESPONSE_RESET_IN_PROGRESS:
379 case AP_RESPONSE_DECONFIGURED:
380 case AP_RESPONSE_CHECKSTOPPED:
381 case AP_RESPONSE_BUSY:
382 /* For all these RCs the tapq info should be available */
385 /* On a pending async error the info should be available */
391 /* There should be at least one of the mode bits set */
392 if (WARN_ON_ONCE(!tapq_info.value))
395 *q_type = tapq_info.at;
396 *q_fac = tapq_info.fac;
397 *q_depth = tapq_info.qd;
398 *q_ml = tapq_info.ml;
399 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
400 *q_cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
405 void ap_wait(enum ap_sm_wait wait)
410 case AP_SM_WAIT_AGAIN:
411 case AP_SM_WAIT_INTERRUPT:
414 if (ap_poll_kthread) {
415 wake_up(&ap_poll_wait);
419 case AP_SM_WAIT_LOW_TIMEOUT:
420 case AP_SM_WAIT_HIGH_TIMEOUT:
421 spin_lock_bh(&ap_poll_timer_lock);
422 if (!hrtimer_is_queued(&ap_poll_timer)) {
424 wait == AP_SM_WAIT_LOW_TIMEOUT ?
425 poll_low_timeout : poll_high_timeout;
426 hrtimer_forward_now(&ap_poll_timer, hr_time);
427 hrtimer_restart(&ap_poll_timer);
429 spin_unlock_bh(&ap_poll_timer_lock);
431 case AP_SM_WAIT_NONE:
438 * ap_request_timeout(): Handling of request timeouts
439 * @t: timer making this callback
441 * Handles request timeouts.
443 void ap_request_timeout(struct timer_list *t)
445 struct ap_queue *aq = from_timer(aq, t, timeout);
447 spin_lock_bh(&aq->lock);
448 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
449 spin_unlock_bh(&aq->lock);
453 * ap_poll_timeout(): AP receive polling for finished AP requests.
454 * @unused: Unused pointer.
456 * Schedules the AP tasklet using a high resolution timer.
458 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
460 tasklet_schedule(&ap_tasklet);
461 return HRTIMER_NORESTART;
465 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
466 * @airq: pointer to adapter interrupt descriptor
469 static void ap_interrupt_handler(struct airq_struct *airq,
470 struct tpi_info *tpi_info)
472 inc_irq_stat(IRQIO_APB);
473 tasklet_schedule(&ap_tasklet);
477 * ap_tasklet_fn(): Tasklet to poll all AP devices.
478 * @dummy: Unused variable
480 * Poll all AP devices on the bus.
482 static void ap_tasklet_fn(unsigned long dummy)
486 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
488 /* Reset the indicator if interrupts are used. Thus new interrupts can
489 * be received. Doing it in the beginning of the tasklet is therefore
490 * important that no requests on any AP get lost.
493 xchg(ap_airq.lsi_ptr, 0);
495 spin_lock_bh(&ap_queues_lock);
496 hash_for_each(ap_queues, bkt, aq, hnode) {
497 spin_lock_bh(&aq->lock);
498 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
499 spin_unlock_bh(&aq->lock);
501 spin_unlock_bh(&ap_queues_lock);
506 static int ap_pending_requests(void)
511 spin_lock_bh(&ap_queues_lock);
512 hash_for_each(ap_queues, bkt, aq, hnode) {
513 if (aq->queue_count == 0)
515 spin_unlock_bh(&ap_queues_lock);
518 spin_unlock_bh(&ap_queues_lock);
523 * ap_poll_thread(): Thread that polls for finished requests.
524 * @data: Unused pointer
526 * AP bus poll thread. The purpose of this thread is to poll for
527 * finished requests in a loop if there is a "free" cpu - that is
528 * a cpu that doesn't have anything better to do. The polling stops
529 * as soon as there is another task or if all messages have been
532 static int ap_poll_thread(void *data)
534 DECLARE_WAITQUEUE(wait, current);
536 set_user_nice(current, MAX_NICE);
538 while (!kthread_should_stop()) {
539 add_wait_queue(&ap_poll_wait, &wait);
540 set_current_state(TASK_INTERRUPTIBLE);
541 if (!ap_pending_requests()) {
545 set_current_state(TASK_RUNNING);
546 remove_wait_queue(&ap_poll_wait, &wait);
547 if (need_resched()) {
558 static int ap_poll_thread_start(void)
562 if (ap_irq_flag || ap_poll_kthread)
564 mutex_lock(&ap_poll_thread_mutex);
565 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
566 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
568 ap_poll_kthread = NULL;
569 mutex_unlock(&ap_poll_thread_mutex);
573 static void ap_poll_thread_stop(void)
575 if (!ap_poll_kthread)
577 mutex_lock(&ap_poll_thread_mutex);
578 kthread_stop(ap_poll_kthread);
579 ap_poll_kthread = NULL;
580 mutex_unlock(&ap_poll_thread_mutex);
583 #define is_card_dev(x) ((x)->parent == ap_root_device)
584 #define is_queue_dev(x) ((x)->parent != ap_root_device)
588 * @dev: Pointer to device
589 * @drv: Pointer to device_driver
591 * AP bus driver registration/unregistration.
593 static int ap_bus_match(struct device *dev, struct device_driver *drv)
595 struct ap_driver *ap_drv = to_ap_drv(drv);
596 struct ap_device_id *id;
599 * Compare device type of the device with the list of
600 * supported types of the device_driver.
602 for (id = ap_drv->ids; id->match_flags; id++) {
603 if (is_card_dev(dev) &&
604 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
605 id->dev_type == to_ap_dev(dev)->device_type)
607 if (is_queue_dev(dev) &&
608 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
609 id->dev_type == to_ap_dev(dev)->device_type)
616 * ap_uevent(): Uevent function for AP devices.
617 * @dev: Pointer to device
618 * @env: Pointer to kobj_uevent_env
620 * It sets up a single environment variable DEV_TYPE which contains the
621 * hardware device type.
623 static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env)
626 const struct ap_device *ap_dev = to_ap_dev(dev);
628 /* Uevents from ap bus core don't need extensions to the env */
629 if (dev == ap_root_device)
632 if (is_card_dev(dev)) {
633 struct ap_card *ac = to_ap_card(&ap_dev->device);
635 /* Set up DEV_TYPE environment variable. */
636 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
640 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
644 /* Add MODE=<accel|cca|ep11> */
645 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL))
646 rc = add_uevent_var(env, "MODE=accel");
647 else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
648 rc = add_uevent_var(env, "MODE=cca");
649 else if (ap_test_bit(&ac->functions, AP_FUNC_EP11))
650 rc = add_uevent_var(env, "MODE=ep11");
654 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
656 /* Add MODE=<accel|cca|ep11> */
657 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL))
658 rc = add_uevent_var(env, "MODE=accel");
659 else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
660 rc = add_uevent_var(env, "MODE=cca");
661 else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11))
662 rc = add_uevent_var(env, "MODE=ep11");
670 static void ap_send_init_scan_done_uevent(void)
672 char *envp[] = { "INITSCAN=done", NULL };
674 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
677 static void ap_send_bindings_complete_uevent(void)
680 char *envp[] = { "BINDINGS=complete", buf, NULL };
682 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
683 atomic64_inc_return(&ap_bindings_complete_count));
684 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
687 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
690 char *envp[] = { buf, NULL };
692 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
694 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
696 EXPORT_SYMBOL(ap_send_config_uevent);
698 void ap_send_online_uevent(struct ap_device *ap_dev, int online)
701 char *envp[] = { buf, NULL };
703 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
705 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
707 EXPORT_SYMBOL(ap_send_online_uevent);
709 static void ap_send_mask_changed_uevent(unsigned long *newapm,
710 unsigned long *newaqm)
713 char *envp[] = { buf, NULL };
716 snprintf(buf, sizeof(buf),
717 "APMASK=0x%016lx%016lx%016lx%016lx\n",
718 newapm[0], newapm[1], newapm[2], newapm[3]);
720 snprintf(buf, sizeof(buf),
721 "AQMASK=0x%016lx%016lx%016lx%016lx\n",
722 newaqm[0], newaqm[1], newaqm[2], newaqm[3]);
724 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
728 * calc # of bound APQNs
731 struct __ap_calc_ctrs {
736 static int __ap_calc_helper(struct device *dev, void *arg)
738 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg;
740 if (is_queue_dev(dev)) {
749 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
751 struct __ap_calc_ctrs ctrs;
753 memset(&ctrs, 0, sizeof(ctrs));
754 bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper);
761 * After initial ap bus scan do check if all existing APQNs are
762 * bound to device drivers.
764 static void ap_check_bindings_complete(void)
766 unsigned int apqns, bound;
768 if (atomic64_read(&ap_scan_bus_count) >= 1) {
769 ap_calc_bound_apqns(&apqns, &bound);
770 if (bound == apqns) {
771 if (!completion_done(&ap_init_apqn_bindings_complete)) {
772 complete_all(&ap_init_apqn_bindings_complete);
773 AP_DBF_INFO("%s complete\n", __func__);
775 ap_send_bindings_complete_uevent();
781 * Interface to wait for the AP bus to have done one initial ap bus
782 * scan and all detected APQNs have been bound to device drivers.
783 * If these both conditions are not fulfilled, this function blocks
784 * on a condition with wait_for_completion_interruptible_timeout().
785 * If these both conditions are fulfilled (before the timeout hits)
786 * the return value is 0. If the timeout (in jiffies) hits instead
787 * -ETIME is returned. On failures negative return values are
788 * returned to the caller.
790 int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
794 if (completion_done(&ap_init_apqn_bindings_complete))
798 l = wait_for_completion_interruptible_timeout(
799 &ap_init_apqn_bindings_complete, timeout);
801 l = wait_for_completion_interruptible(
802 &ap_init_apqn_bindings_complete);
804 return l == -ERESTARTSYS ? -EINTR : l;
805 else if (l == 0 && timeout)
810 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
812 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
814 if (is_queue_dev(dev) &&
815 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data)
816 device_unregister(dev);
820 static int __ap_revise_reserved(struct device *dev, void *dummy)
822 int rc, card, queue, devres, drvres;
824 if (is_queue_dev(dev)) {
825 card = AP_QID_CARD(to_ap_queue(dev)->qid);
826 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
827 mutex_lock(&ap_perms_mutex);
828 devres = test_bit_inv(card, ap_perms.apm) &&
829 test_bit_inv(queue, ap_perms.aqm);
830 mutex_unlock(&ap_perms_mutex);
831 drvres = to_ap_drv(dev->driver)->flags
832 & AP_DRIVER_FLAG_DEFAULT;
833 if (!!devres != !!drvres) {
834 AP_DBF_DBG("%s reprobing queue=%02x.%04x\n",
835 __func__, card, queue);
836 rc = device_reprobe(dev);
838 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
839 __func__, card, queue);
846 static void ap_bus_revise_bindings(void)
848 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
852 * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
853 * default host driver or not.
854 * @card: the APID of the adapter card to check
855 * @queue: the APQI of the queue to check
857 * Note: the ap_perms_mutex must be locked by the caller of this function.
859 * Return: an int specifying whether the AP adapter is reserved for the host (1)
862 int ap_owned_by_def_drv(int card, int queue)
866 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
869 if (test_bit_inv(card, ap_perms.apm) &&
870 test_bit_inv(queue, ap_perms.aqm))
875 EXPORT_SYMBOL(ap_owned_by_def_drv);
878 * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
879 * a set is reserved for the host drivers
881 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
882 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
884 * Note: the ap_perms_mutex must be locked by the caller of this function.
886 * Return: an int specifying whether each APQN is reserved for the host (1) or
889 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
892 int card, queue, rc = 0;
894 for (card = 0; !rc && card < AP_DEVICES; card++)
895 if (test_bit_inv(card, apm) &&
896 test_bit_inv(card, ap_perms.apm))
897 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
898 if (test_bit_inv(queue, aqm) &&
899 test_bit_inv(queue, ap_perms.aqm))
904 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
906 static int ap_device_probe(struct device *dev)
908 struct ap_device *ap_dev = to_ap_dev(dev);
909 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
910 int card, queue, devres, drvres, rc = -ENODEV;
912 if (!get_device(dev))
915 if (is_queue_dev(dev)) {
917 * If the apqn is marked as reserved/used by ap bus and
918 * default drivers, only probe with drivers with the default
919 * flag set. If it is not marked, only probe with drivers
920 * with the default flag not set.
922 card = AP_QID_CARD(to_ap_queue(dev)->qid);
923 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
924 mutex_lock(&ap_perms_mutex);
925 devres = test_bit_inv(card, ap_perms.apm) &&
926 test_bit_inv(queue, ap_perms.aqm);
927 mutex_unlock(&ap_perms_mutex);
928 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
929 if (!!devres != !!drvres)
933 /* Add queue/card to list of active queues/cards */
934 spin_lock_bh(&ap_queues_lock);
935 if (is_queue_dev(dev))
936 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
937 to_ap_queue(dev)->qid);
938 spin_unlock_bh(&ap_queues_lock);
940 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
943 spin_lock_bh(&ap_queues_lock);
944 if (is_queue_dev(dev))
945 hash_del(&to_ap_queue(dev)->hnode);
946 spin_unlock_bh(&ap_queues_lock);
948 ap_check_bindings_complete();
957 static void ap_device_remove(struct device *dev)
959 struct ap_device *ap_dev = to_ap_dev(dev);
960 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
962 /* prepare ap queue device removal */
963 if (is_queue_dev(dev))
964 ap_queue_prepare_remove(to_ap_queue(dev));
966 /* driver's chance to clean up gracefully */
968 ap_drv->remove(ap_dev);
970 /* now do the ap queue device remove */
971 if (is_queue_dev(dev))
972 ap_queue_remove(to_ap_queue(dev));
974 /* Remove queue/card from list of active queues/cards */
975 spin_lock_bh(&ap_queues_lock);
976 if (is_queue_dev(dev))
977 hash_del(&to_ap_queue(dev)->hnode);
978 spin_unlock_bh(&ap_queues_lock);
983 struct ap_queue *ap_get_qdev(ap_qid_t qid)
988 spin_lock_bh(&ap_queues_lock);
989 hash_for_each(ap_queues, bkt, aq, hnode) {
990 if (aq->qid == qid) {
991 get_device(&aq->ap_dev.device);
992 spin_unlock_bh(&ap_queues_lock);
996 spin_unlock_bh(&ap_queues_lock);
1000 EXPORT_SYMBOL(ap_get_qdev);
1002 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1005 struct device_driver *drv = &ap_drv->driver;
1007 drv->bus = &ap_bus_type;
1010 return driver_register(drv);
1012 EXPORT_SYMBOL(ap_driver_register);
1014 void ap_driver_unregister(struct ap_driver *ap_drv)
1016 driver_unregister(&ap_drv->driver);
1018 EXPORT_SYMBOL(ap_driver_unregister);
1020 void ap_bus_force_rescan(void)
1022 /* Only trigger AP bus scans after the initial scan is done */
1023 if (atomic64_read(&ap_scan_bus_count) <= 0)
1026 /* processing a asynchronous bus rescan */
1027 del_timer(&ap_config_timer);
1028 queue_work(system_long_wq, &ap_scan_work);
1029 flush_work(&ap_scan_work);
1031 EXPORT_SYMBOL(ap_bus_force_rescan);
1034 * A config change has happened, force an ap bus rescan.
1036 void ap_bus_cfg_chg(void)
1038 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
1040 ap_bus_force_rescan();
1044 * hex2bitmap() - parse hex mask string and set bitmap.
1045 * Valid strings are "0x012345678" with at least one valid hex number.
1046 * Rest of the bitmap to the right is padded with 0. No spaces allowed
1047 * within the string, the leading 0x may be omitted.
1048 * Returns the bitmask with exactly the bits set as given by the hex
1049 * string (both in big endian order).
1051 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1055 /* bits needs to be a multiple of 8 */
1059 if (str[0] == '0' && str[1] == 'x')
1064 for (i = 0; isxdigit(*str) && i < bits; str++) {
1065 b = hex_to_bin(*str);
1066 for (n = 0; n < 4; n++)
1067 if (b & (0x08 >> n))
1068 set_bit_inv(i + n, bitmap);
1080 * modify_bitmap() - parse bitmask argument and modify an existing
1081 * bit mask accordingly. A concatenation (done with ',') of these
1082 * terms is recognized:
1083 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1084 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1085 * 0...bits-1; the leading + or - is required. Here are some examples:
1086 * +0-15,+32,-128,-0xFF
1087 * -0-255,+1-16,+0x128
1088 * +1,+2,+3,+4,-5,-7-10
1089 * Returns the new bitmap after all changes have been applied. Every
1090 * positive value in the string will set a bit and every negative value
1091 * in the string will clear a bit. As a bit may be touched more than once,
1092 * the last 'operation' wins:
1093 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1094 * cleared again. All other bits are unmodified.
1096 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1101 /* bits needs to be a multiple of 8 */
1107 if (sign != '+' && sign != '-')
1109 a = z = simple_strtoul(str, &np, 0);
1110 if (str == np || a >= bits)
1114 z = simple_strtoul(++str, &np, 0);
1115 if (str == np || a > z || z >= bits)
1119 for (i = a; i <= z; i++)
1121 set_bit_inv(i, bitmap);
1123 clear_bit_inv(i, bitmap);
1124 while (*str == ',' || *str == '\n')
1131 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1132 unsigned long *newmap)
1137 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1138 if (*str == '+' || *str == '-') {
1139 memcpy(newmap, bitmap, size);
1140 rc = modify_bitmap(str, newmap, bits);
1142 memset(newmap, 0, size);
1143 rc = hex2bitmap(str, newmap, bits);
1148 int ap_parse_mask_str(const char *str,
1149 unsigned long *bitmap, int bits,
1152 unsigned long *newmap, size;
1155 /* bits needs to be a multiple of 8 */
1159 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1160 newmap = kmalloc(size, GFP_KERNEL);
1163 if (mutex_lock_interruptible(lock)) {
1165 return -ERESTARTSYS;
1167 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
1169 memcpy(bitmap, newmap, size);
1174 EXPORT_SYMBOL(ap_parse_mask_str);
1177 * AP bus attributes.
1180 static ssize_t ap_domain_show(const struct bus_type *bus, char *buf)
1182 return sysfs_emit(buf, "%d\n", ap_domain_index);
1185 static ssize_t ap_domain_store(const struct bus_type *bus,
1186 const char *buf, size_t count)
1190 if (sscanf(buf, "%i\n", &domain) != 1 ||
1191 domain < 0 || domain > ap_max_domain_id ||
1192 !test_bit_inv(domain, ap_perms.aqm))
1195 spin_lock_bh(&ap_domain_lock);
1196 ap_domain_index = domain;
1197 spin_unlock_bh(&ap_domain_lock);
1199 AP_DBF_INFO("%s stored new default domain=%d\n",
1205 static BUS_ATTR_RW(ap_domain);
1207 static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf)
1209 if (!ap_qci_info) /* QCI not supported */
1210 return sysfs_emit(buf, "not supported\n");
1212 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1213 ap_qci_info->adm[0], ap_qci_info->adm[1],
1214 ap_qci_info->adm[2], ap_qci_info->adm[3],
1215 ap_qci_info->adm[4], ap_qci_info->adm[5],
1216 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1219 static BUS_ATTR_RO(ap_control_domain_mask);
1221 static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf)
1223 if (!ap_qci_info) /* QCI not supported */
1224 return sysfs_emit(buf, "not supported\n");
1226 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1227 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1228 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1229 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1230 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1233 static BUS_ATTR_RO(ap_usage_domain_mask);
1235 static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf)
1237 if (!ap_qci_info) /* QCI not supported */
1238 return sysfs_emit(buf, "not supported\n");
1240 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1241 ap_qci_info->apm[0], ap_qci_info->apm[1],
1242 ap_qci_info->apm[2], ap_qci_info->apm[3],
1243 ap_qci_info->apm[4], ap_qci_info->apm[5],
1244 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1247 static BUS_ATTR_RO(ap_adapter_mask);
1249 static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf)
1251 return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0);
1254 static BUS_ATTR_RO(ap_interrupts);
1256 static ssize_t config_time_show(const struct bus_type *bus, char *buf)
1258 return sysfs_emit(buf, "%d\n", ap_config_time);
1261 static ssize_t config_time_store(const struct bus_type *bus,
1262 const char *buf, size_t count)
1266 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1268 ap_config_time = time;
1269 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1273 static BUS_ATTR_RW(config_time);
1275 static ssize_t poll_thread_show(const struct bus_type *bus, char *buf)
1277 return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0);
1280 static ssize_t poll_thread_store(const struct bus_type *bus,
1281 const char *buf, size_t count)
1286 rc = kstrtobool(buf, &value);
1291 rc = ap_poll_thread_start();
1295 ap_poll_thread_stop();
1300 static BUS_ATTR_RW(poll_thread);
1302 static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf)
1304 return sysfs_emit(buf, "%lu\n", poll_high_timeout);
1307 static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf,
1310 unsigned long value;
1314 rc = kstrtoul(buf, 0, &value);
1318 /* 120 seconds = maximum poll interval */
1319 if (value > 120000000000UL)
1321 poll_high_timeout = value;
1322 hr_time = poll_high_timeout;
1324 spin_lock_bh(&ap_poll_timer_lock);
1325 hrtimer_cancel(&ap_poll_timer);
1326 hrtimer_set_expires(&ap_poll_timer, hr_time);
1327 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1328 spin_unlock_bh(&ap_poll_timer_lock);
1333 static BUS_ATTR_RW(poll_timeout);
1335 static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf)
1337 return sysfs_emit(buf, "%d\n", ap_max_domain_id);
1340 static BUS_ATTR_RO(ap_max_domain_id);
1342 static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf)
1344 return sysfs_emit(buf, "%d\n", ap_max_adapter_id);
1347 static BUS_ATTR_RO(ap_max_adapter_id);
1349 static ssize_t apmask_show(const struct bus_type *bus, char *buf)
1353 if (mutex_lock_interruptible(&ap_perms_mutex))
1354 return -ERESTARTSYS;
1355 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1356 ap_perms.apm[0], ap_perms.apm[1],
1357 ap_perms.apm[2], ap_perms.apm[3]);
1358 mutex_unlock(&ap_perms_mutex);
1363 static int __verify_card_reservations(struct device_driver *drv, void *data)
1366 struct ap_driver *ap_drv = to_ap_drv(drv);
1367 unsigned long *newapm = (unsigned long *)data;
1370 * increase the driver's module refcounter to be sure it is not
1371 * going away when we invoke the callback function.
1373 if (!try_module_get(drv->owner))
1376 if (ap_drv->in_use) {
1377 rc = ap_drv->in_use(newapm, ap_perms.aqm);
1382 /* release the driver's module */
1383 module_put(drv->owner);
1388 static int apmask_commit(unsigned long *newapm)
1391 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1394 * Check if any bits in the apmask have been set which will
1395 * result in queues being removed from non-default drivers
1397 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1398 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1399 __verify_card_reservations);
1404 memcpy(ap_perms.apm, newapm, APMASKSIZE);
1409 static ssize_t apmask_store(const struct bus_type *bus, const char *buf,
1412 int rc, changes = 0;
1413 DECLARE_BITMAP(newapm, AP_DEVICES);
1415 if (mutex_lock_interruptible(&ap_perms_mutex))
1416 return -ERESTARTSYS;
1418 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1422 changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
1424 rc = apmask_commit(newapm);
1427 mutex_unlock(&ap_perms_mutex);
1432 ap_bus_revise_bindings();
1433 ap_send_mask_changed_uevent(newapm, NULL);
1439 static BUS_ATTR_RW(apmask);
1441 static ssize_t aqmask_show(const struct bus_type *bus, char *buf)
1445 if (mutex_lock_interruptible(&ap_perms_mutex))
1446 return -ERESTARTSYS;
1447 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1448 ap_perms.aqm[0], ap_perms.aqm[1],
1449 ap_perms.aqm[2], ap_perms.aqm[3]);
1450 mutex_unlock(&ap_perms_mutex);
1455 static int __verify_queue_reservations(struct device_driver *drv, void *data)
1458 struct ap_driver *ap_drv = to_ap_drv(drv);
1459 unsigned long *newaqm = (unsigned long *)data;
1462 * increase the driver's module refcounter to be sure it is not
1463 * going away when we invoke the callback function.
1465 if (!try_module_get(drv->owner))
1468 if (ap_drv->in_use) {
1469 rc = ap_drv->in_use(ap_perms.apm, newaqm);
1474 /* release the driver's module */
1475 module_put(drv->owner);
1480 static int aqmask_commit(unsigned long *newaqm)
1483 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1486 * Check if any bits in the aqmask have been set which will
1487 * result in queues being removed from non-default drivers
1489 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1490 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1491 __verify_queue_reservations);
1496 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1501 static ssize_t aqmask_store(const struct bus_type *bus, const char *buf,
1504 int rc, changes = 0;
1505 DECLARE_BITMAP(newaqm, AP_DOMAINS);
1507 if (mutex_lock_interruptible(&ap_perms_mutex))
1508 return -ERESTARTSYS;
1510 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1514 changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
1516 rc = aqmask_commit(newaqm);
1519 mutex_unlock(&ap_perms_mutex);
1524 ap_bus_revise_bindings();
1525 ap_send_mask_changed_uevent(NULL, newaqm);
1531 static BUS_ATTR_RW(aqmask);
1533 static ssize_t scans_show(const struct bus_type *bus, char *buf)
1535 return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count));
1538 static ssize_t scans_store(const struct bus_type *bus, const char *buf,
1541 AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1543 ap_bus_force_rescan();
1548 static BUS_ATTR_RW(scans);
1550 static ssize_t bindings_show(const struct bus_type *bus, char *buf)
1553 unsigned int apqns, n;
1555 ap_calc_bound_apqns(&apqns, &n);
1556 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1557 rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns);
1559 rc = sysfs_emit(buf, "%u/%u\n", n, apqns);
1564 static BUS_ATTR_RO(bindings);
1566 static ssize_t features_show(const struct bus_type *bus, char *buf)
1570 if (!ap_qci_info) /* QCI not supported */
1571 return sysfs_emit(buf, "-\n");
1573 if (ap_qci_info->apsc)
1574 n += sysfs_emit_at(buf, n, "APSC ");
1575 if (ap_qci_info->apxa)
1576 n += sysfs_emit_at(buf, n, "APXA ");
1577 if (ap_qci_info->qact)
1578 n += sysfs_emit_at(buf, n, "QACT ");
1579 if (ap_qci_info->rc8a)
1580 n += sysfs_emit_at(buf, n, "RC8A ");
1581 if (ap_qci_info->apsb)
1582 n += sysfs_emit_at(buf, n, "APSB ");
1584 sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n");
1589 static BUS_ATTR_RO(features);
1591 static struct attribute *ap_bus_attrs[] = {
1592 &bus_attr_ap_domain.attr,
1593 &bus_attr_ap_control_domain_mask.attr,
1594 &bus_attr_ap_usage_domain_mask.attr,
1595 &bus_attr_ap_adapter_mask.attr,
1596 &bus_attr_config_time.attr,
1597 &bus_attr_poll_thread.attr,
1598 &bus_attr_ap_interrupts.attr,
1599 &bus_attr_poll_timeout.attr,
1600 &bus_attr_ap_max_domain_id.attr,
1601 &bus_attr_ap_max_adapter_id.attr,
1602 &bus_attr_apmask.attr,
1603 &bus_attr_aqmask.attr,
1604 &bus_attr_scans.attr,
1605 &bus_attr_bindings.attr,
1606 &bus_attr_features.attr,
1609 ATTRIBUTE_GROUPS(ap_bus);
1611 static struct bus_type ap_bus_type = {
1613 .bus_groups = ap_bus_groups,
1614 .match = &ap_bus_match,
1615 .uevent = &ap_uevent,
1616 .probe = ap_device_probe,
1617 .remove = ap_device_remove,
1621 * ap_select_domain(): Select an AP domain if possible and we haven't
1622 * already done so before.
1624 static void ap_select_domain(void)
1626 struct ap_queue_status status;
1630 * Choose the default domain. Either the one specified with
1631 * the "domain=" parameter or the first domain with at least
1634 spin_lock_bh(&ap_domain_lock);
1635 if (ap_domain_index >= 0) {
1636 /* Domain has already been selected. */
1639 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1640 if (!ap_test_config_usage_domain(dom) ||
1641 !test_bit_inv(dom, ap_perms.aqm))
1643 for (card = 0; card <= ap_max_adapter_id; card++) {
1644 if (!ap_test_config_card_id(card) ||
1645 !test_bit_inv(card, ap_perms.apm))
1647 status = ap_test_queue(AP_MKQID(card, dom),
1648 ap_apft_available(),
1650 if (status.response_code == AP_RESPONSE_NORMAL)
1653 if (card <= ap_max_adapter_id)
1656 if (dom <= ap_max_domain_id) {
1657 ap_domain_index = dom;
1658 AP_DBF_INFO("%s new default domain is %d\n",
1659 __func__, ap_domain_index);
1662 spin_unlock_bh(&ap_domain_lock);
1666 * This function checks the type and returns either 0 for not
1667 * supported or the highest compatible type value (which may
1668 * include the input type value).
1670 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1674 /* < CEX4 is not supported */
1675 if (rawtype < AP_DEVICE_TYPE_CEX4) {
1676 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1677 __func__, AP_QID_CARD(qid),
1678 AP_QID_QUEUE(qid), rawtype);
1681 /* up to CEX8 known and fully supported */
1682 if (rawtype <= AP_DEVICE_TYPE_CEX8)
1685 * unknown new type > CEX8, check for compatibility
1686 * to the highest known and supported type which is
1687 * currently CEX8 with the help of the QACT function.
1689 if (ap_qact_available()) {
1690 struct ap_queue_status status;
1691 union ap_qact_ap_info apinfo = {0};
1693 apinfo.mode = (func >> 26) & 0x07;
1694 apinfo.cat = AP_DEVICE_TYPE_CEX8;
1695 status = ap_qact(qid, 0, &apinfo);
1696 if (status.response_code == AP_RESPONSE_NORMAL &&
1697 apinfo.cat >= AP_DEVICE_TYPE_CEX4 &&
1698 apinfo.cat <= AP_DEVICE_TYPE_CEX8)
1699 comp_type = apinfo.cat;
1702 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1703 __func__, AP_QID_CARD(qid),
1704 AP_QID_QUEUE(qid), rawtype);
1705 else if (comp_type != rawtype)
1706 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1707 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1708 rawtype, comp_type);
1713 * Helper function to be used with bus_find_dev
1714 * matches for the card device with the given id
1716 static int __match_card_device_with_id(struct device *dev, const void *data)
1718 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
1722 * Helper function to be used with bus_find_dev
1723 * matches for the queue device with a given qid
1725 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1727 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
1731 * Helper function to be used with bus_find_dev
1732 * matches any queue device with given queue id
1734 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1736 return is_queue_dev(dev) &&
1737 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
1740 /* Helper function for notify_config_changed */
1741 static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1743 struct ap_driver *ap_drv = to_ap_drv(drv);
1745 if (try_module_get(drv->owner)) {
1746 if (ap_drv->on_config_changed)
1747 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1748 module_put(drv->owner);
1754 /* Notify all drivers about an qci config change */
1755 static inline void notify_config_changed(void)
1757 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1758 __drv_notify_config_changed);
1761 /* Helper function for notify_scan_complete */
1762 static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1764 struct ap_driver *ap_drv = to_ap_drv(drv);
1766 if (try_module_get(drv->owner)) {
1767 if (ap_drv->on_scan_complete)
1768 ap_drv->on_scan_complete(ap_qci_info,
1770 module_put(drv->owner);
1776 /* Notify all drivers about bus scan complete */
1777 static inline void notify_scan_complete(void)
1779 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1780 __drv_notify_scan_complete);
1784 * Helper function for ap_scan_bus().
1785 * Remove card device and associated queue devices.
1787 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1789 bus_for_each_dev(&ap_bus_type, NULL,
1790 (void *)(long)ac->id,
1791 __ap_queue_devices_with_id_unregister);
1792 device_unregister(&ac->ap_dev.device);
1796 * Helper function for ap_scan_bus().
1797 * Does the scan bus job for all the domains within
1798 * a valid adapter given by an ap_card ptr.
1800 static inline void ap_scan_domains(struct ap_card *ac)
1802 int rc, dom, depth, type, ml;
1803 bool decfg, chkstop;
1804 struct ap_queue *aq;
1810 * Go through the configuration for the domains and compare them
1811 * to the existing queue devices. Also take care of the config
1812 * and error state for the queue devices.
1815 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1816 qid = AP_MKQID(ac->id, dom);
1817 dev = bus_find_device(&ap_bus_type, NULL,
1819 __match_queue_device_with_qid);
1820 aq = dev ? to_ap_queue(dev) : NULL;
1821 if (!ap_test_config_usage_domain(dom)) {
1823 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
1824 __func__, ac->id, dom);
1825 device_unregister(dev);
1827 goto put_dev_and_continue;
1829 /* domain is valid, get info from this APQN */
1830 rc = ap_queue_info(qid, &type, &func, &depth,
1831 &ml, &decfg, &chkstop);
1835 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1836 __func__, ac->id, dom);
1837 device_unregister(dev);
1841 goto put_dev_and_continue;
1845 /* if no queue device exists, create a new one */
1847 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1849 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1850 __func__, ac->id, dom);
1854 aq->config = !decfg;
1855 aq->chkstop = chkstop;
1856 dev = &aq->ap_dev.device;
1857 dev->bus = &ap_bus_type;
1858 dev->parent = &ac->ap_dev.device;
1859 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1860 /* register queue device */
1861 rc = device_register(dev);
1863 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1864 __func__, ac->id, dom);
1865 goto put_dev_and_continue;
1867 /* get it and thus adjust reference counter */
1870 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
1871 __func__, ac->id, dom);
1872 } else if (chkstop) {
1873 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1874 __func__, ac->id, dom);
1876 /* nudge the queue's state machine */
1877 ap_queue_init_state(aq);
1878 AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
1879 __func__, ac->id, dom);
1881 goto put_dev_and_continue;
1883 /* handle state changes on already existing queue device */
1884 spin_lock_bh(&aq->lock);
1885 /* checkstop state */
1886 if (chkstop && !aq->chkstop) {
1889 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1890 aq->dev_state = AP_DEV_STATE_ERROR;
1891 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1893 spin_unlock_bh(&aq->lock);
1894 AP_DBF_DBG("%s(%d,%d) queue dev checkstop on\n",
1895 __func__, ac->id, dom);
1896 /* 'receive' pending messages with -EAGAIN */
1898 goto put_dev_and_continue;
1899 } else if (!chkstop && aq->chkstop) {
1901 aq->chkstop = false;
1902 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
1903 _ap_queue_init_state(aq);
1904 spin_unlock_bh(&aq->lock);
1905 AP_DBF_DBG("%s(%d,%d) queue dev checkstop off\n",
1906 __func__, ac->id, dom);
1907 goto put_dev_and_continue;
1909 /* config state change */
1910 if (decfg && aq->config) {
1911 /* config off this queue device */
1913 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1914 aq->dev_state = AP_DEV_STATE_ERROR;
1915 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1917 spin_unlock_bh(&aq->lock);
1918 AP_DBF_DBG("%s(%d,%d) queue dev config off\n",
1919 __func__, ac->id, dom);
1920 ap_send_config_uevent(&aq->ap_dev, aq->config);
1921 /* 'receive' pending messages with -EAGAIN */
1923 goto put_dev_and_continue;
1924 } else if (!decfg && !aq->config) {
1925 /* config on this queue device */
1927 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
1928 _ap_queue_init_state(aq);
1929 spin_unlock_bh(&aq->lock);
1930 AP_DBF_DBG("%s(%d,%d) queue dev config on\n",
1931 __func__, ac->id, dom);
1932 ap_send_config_uevent(&aq->ap_dev, aq->config);
1933 goto put_dev_and_continue;
1935 /* handle other error states */
1936 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1937 spin_unlock_bh(&aq->lock);
1938 /* 'receive' pending messages with -EAGAIN */
1940 /* re-init (with reset) the queue device */
1941 ap_queue_init_state(aq);
1942 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
1943 __func__, ac->id, dom);
1944 goto put_dev_and_continue;
1946 spin_unlock_bh(&aq->lock);
1947 put_dev_and_continue:
1953 * Helper function for ap_scan_bus().
1954 * Does the scan bus job for the given adapter id.
1956 static inline void ap_scan_adapter(int ap)
1958 int rc, dom, depth, type, comp_type, ml;
1959 bool decfg, chkstop;
1965 /* Is there currently a card device for this adapter ? */
1966 dev = bus_find_device(&ap_bus_type, NULL,
1968 __match_card_device_with_id);
1969 ac = dev ? to_ap_card(dev) : NULL;
1971 /* Adapter not in configuration ? */
1972 if (!ap_test_config_card_id(ap)) {
1974 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
1976 ap_scan_rm_card_dev_and_queue_devs(ac);
1983 * Adapter ap is valid in the current configuration. So do some checks:
1984 * If no card device exists, build one. If a card device exists, check
1985 * for type and functions changed. For all this we need to find a valid
1989 for (dom = 0; dom <= ap_max_domain_id; dom++)
1990 if (ap_test_config_usage_domain(dom)) {
1991 qid = AP_MKQID(ap, dom);
1992 if (ap_queue_info(qid, &type, &func, &depth,
1993 &ml, &decfg, &chkstop) > 0)
1996 if (dom > ap_max_domain_id) {
1997 /* Could not find one valid APQN for this adapter */
1999 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
2001 ap_scan_rm_card_dev_and_queue_devs(ac);
2004 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
2010 /* No apdater type info available, an unusable adapter */
2012 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
2014 ap_scan_rm_card_dev_and_queue_devs(ac);
2017 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
2023 /* Check APQN against existing card device for changes */
2024 if (ac->raw_hwtype != type) {
2025 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
2026 __func__, ap, type);
2027 ap_scan_rm_card_dev_and_queue_devs(ac);
2030 } else if ((ac->functions & TAPQ_CARD_FUNC_CMP_MASK) !=
2031 (func & TAPQ_CARD_FUNC_CMP_MASK)) {
2032 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
2033 __func__, ap, func);
2034 ap_scan_rm_card_dev_and_queue_devs(ac);
2038 /* handle checkstop state change */
2039 if (chkstop && !ac->chkstop) {
2042 AP_DBF_INFO("%s(%d) card dev checkstop on\n",
2044 } else if (!chkstop && ac->chkstop) {
2046 ac->chkstop = false;
2047 AP_DBF_INFO("%s(%d) card dev checkstop off\n",
2050 /* handle config state change */
2051 if (decfg && ac->config) {
2053 AP_DBF_INFO("%s(%d) card dev config off\n",
2055 ap_send_config_uevent(&ac->ap_dev, ac->config);
2056 } else if (!decfg && !ac->config) {
2058 AP_DBF_INFO("%s(%d) card dev config on\n",
2060 ap_send_config_uevent(&ac->ap_dev, ac->config);
2066 /* Build a new card device */
2067 comp_type = ap_get_compatible_type(qid, type, func);
2069 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
2070 __func__, ap, type);
2073 ac = ap_card_create(ap, depth, type, comp_type, func, ml);
2075 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
2079 ac->config = !decfg;
2080 ac->chkstop = chkstop;
2081 dev = &ac->ap_dev.device;
2082 dev->bus = &ap_bus_type;
2083 dev->parent = ap_root_device;
2084 dev_set_name(dev, "card%02x", ap);
2085 /* maybe enlarge ap_max_msg_size to support this card */
2086 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
2087 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
2088 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
2090 atomic_read(&ap_max_msg_size));
2092 /* Register the new card device with AP bus */
2093 rc = device_register(dev);
2095 AP_DBF_WARN("%s(%d) device_register() failed\n",
2100 /* get it and thus adjust reference counter */
2103 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
2104 __func__, ap, type, func);
2106 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
2107 __func__, ap, type, func);
2109 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
2110 __func__, ap, type, func);
2113 /* Verify the domains and the queue devices for this card */
2114 ap_scan_domains(ac);
2116 /* release the card device */
2117 put_device(&ac->ap_dev.device);
2121 * ap_get_configuration - get the host AP configuration
2123 * Stores the host AP configuration information returned from the previous call
2124 * to Query Configuration Information (QCI), then retrieves and stores the
2125 * current AP configuration returned from QCI.
2127 * Return: true if the host AP configuration changed between calls to QCI;
2128 * otherwise, return false.
2130 static bool ap_get_configuration(void)
2132 if (!ap_qci_info) /* QCI not supported */
2135 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2136 ap_fetch_qci_info(ap_qci_info);
2138 return memcmp(ap_qci_info, ap_qci_info_old,
2139 sizeof(struct ap_config_info)) != 0;
2143 * ap_scan_bus(): Scan the AP bus for new devices
2144 * Runs periodically, workqueue timer (ap_config_time)
2145 * @unused: Unused pointer.
2147 static void ap_scan_bus(struct work_struct *unused)
2149 int ap, config_changed = 0;
2151 /* config change notify */
2152 config_changed = ap_get_configuration();
2154 notify_config_changed();
2157 AP_DBF_DBG("%s running\n", __func__);
2159 /* loop over all possible adapters */
2160 for (ap = 0; ap <= ap_max_adapter_id; ap++)
2161 ap_scan_adapter(ap);
2163 /* scan complete notify */
2165 notify_scan_complete();
2167 /* check if there is at least one queue available with default domain */
2168 if (ap_domain_index >= 0) {
2169 struct device *dev =
2170 bus_find_device(&ap_bus_type, NULL,
2171 (void *)(long)ap_domain_index,
2172 __match_queue_device_with_queue_id);
2176 AP_DBF_INFO("%s no queue device with default domain %d available\n",
2177 __func__, ap_domain_index);
2180 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
2181 AP_DBF_DBG("%s init scan complete\n", __func__);
2182 ap_send_init_scan_done_uevent();
2183 ap_check_bindings_complete();
2186 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
2189 static void ap_config_timeout(struct timer_list *unused)
2191 queue_work(system_long_wq, &ap_scan_work);
2194 static int __init ap_debug_init(void)
2196 ap_dbf_info = debug_register("ap", 2, 1,
2197 DBF_MAX_SPRINTF_ARGS * sizeof(long));
2198 debug_register_view(ap_dbf_info, &debug_sprintf_view);
2199 debug_set_level(ap_dbf_info, DBF_ERR);
2204 static void __init ap_perms_init(void)
2206 /* all resources usable if no kernel parameter string given */
2207 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
2208 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2209 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2211 /* apm kernel parameter string */
2213 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
2214 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2218 /* aqm kernel parameter string */
2220 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
2221 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2227 * ap_module_init(): The module initialization code.
2229 * Initializes the module.
2231 static int __init ap_module_init(void)
2235 rc = ap_debug_init();
2239 if (!ap_instructions_available()) {
2240 pr_warn("The hardware system does not support AP instructions\n");
2244 /* init ap_queue hashtable */
2245 hash_init(ap_queues);
2247 /* set up the AP permissions (ioctls, ap and aq masks) */
2250 /* Get AP configuration data if available */
2253 /* check default domain setting */
2254 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
2255 (ap_domain_index >= 0 &&
2256 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
2257 pr_warn("%d is not a valid cryptographic domain\n",
2259 ap_domain_index = -1;
2262 /* enable interrupts if available */
2263 if (ap_interrupts_available() && ap_useirq) {
2264 rc = register_adapter_interrupt(&ap_airq);
2265 ap_irq_flag = (rc == 0);
2268 /* Create /sys/bus/ap. */
2269 rc = bus_register(&ap_bus_type);
2273 /* Create /sys/devices/ap. */
2274 ap_root_device = root_device_register("ap");
2275 rc = PTR_ERR_OR_ZERO(ap_root_device);
2278 ap_root_device->bus = &ap_bus_type;
2280 /* Setup the AP bus rescan timer. */
2281 timer_setup(&ap_config_timer, ap_config_timeout, 0);
2284 * Setup the high resolution poll timer.
2285 * If we are running under z/VM adjust polling to z/VM polling rate.
2288 poll_high_timeout = 1500000;
2289 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2290 ap_poll_timer.function = ap_poll_timeout;
2292 /* Start the low priority AP bus poll thread. */
2293 if (ap_thread_flag) {
2294 rc = ap_poll_thread_start();
2299 queue_work(system_long_wq, &ap_scan_work);
2304 hrtimer_cancel(&ap_poll_timer);
2305 root_device_unregister(ap_root_device);
2307 bus_unregister(&ap_bus_type);
2310 unregister_adapter_interrupt(&ap_airq);
2314 device_initcall(ap_module_init);