1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2006, 2023
4 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Ralph Wuerthner <rwuerthn@de.ibm.com>
7 * Felix Beck <felix.beck@de.ibm.com>
8 * Holger Dengler <hd@linux.vnet.ibm.com>
9 * Harald Freudenberger <freude@linux.ibm.com>
11 * Adjunct processor bus.
14 #define KMSG_COMPONENT "ap"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 #include <linux/kernel_stat.h>
18 #include <linux/moduleparam.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/freezer.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/slab.h>
26 #include <linux/notifier.h>
27 #include <linux/kthread.h>
28 #include <linux/mutex.h>
31 #include <linux/atomic.h>
33 #include <linux/hrtimer.h>
34 #include <linux/ktime.h>
35 #include <asm/facility.h>
36 #include <linux/crypto.h>
37 #include <linux/mod_devicetable.h>
38 #include <linux/debugfs.h>
39 #include <linux/ctype.h>
40 #include <linux/module.h>
46 * Module parameters; note though this file itself isn't modular.
48 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
49 static DEFINE_SPINLOCK(ap_domain_lock);
50 module_param_named(domain, ap_domain_index, int, 0440);
51 MODULE_PARM_DESC(domain, "domain index for ap devices");
52 EXPORT_SYMBOL(ap_domain_index);
54 static int ap_thread_flag;
55 module_param_named(poll_thread, ap_thread_flag, int, 0440);
56 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
59 module_param_named(apmask, apm_str, charp, 0440);
60 MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
63 module_param_named(aqmask, aqm_str, charp, 0440);
64 MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
66 static int ap_useirq = 1;
67 module_param_named(useirq, ap_useirq, int, 0440);
68 MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on).");
70 atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
71 EXPORT_SYMBOL(ap_max_msg_size);
73 static struct device *ap_root_device;
75 /* Hashtable of all queue devices on the AP bus */
76 DEFINE_HASHTABLE(ap_queues, 8);
77 /* lock used for the ap_queues hashtable */
78 DEFINE_SPINLOCK(ap_queues_lock);
80 /* Default permissions (ioctl, card and domain masking) */
81 struct ap_perms ap_perms;
82 EXPORT_SYMBOL(ap_perms);
83 DEFINE_MUTEX(ap_perms_mutex);
84 EXPORT_SYMBOL(ap_perms_mutex);
86 /* # of bus scans since init */
87 static atomic64_t ap_scan_bus_count;
89 /* # of bindings complete since init */
90 static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
92 /* completion for initial APQN bindings complete */
93 static DECLARE_COMPLETION(ap_init_apqn_bindings_complete);
95 static struct ap_config_info *ap_qci_info;
96 static struct ap_config_info *ap_qci_info_old;
99 * AP bus related debug feature things.
101 debug_info_t *ap_dbf_info;
104 * Workqueue timer for bus rescan.
106 static struct timer_list ap_config_timer;
107 static int ap_config_time = AP_CONFIG_TIME;
108 static void ap_scan_bus(struct work_struct *);
109 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
112 * Tasklet & timer for AP request polling and interrupts
114 static void ap_tasklet_fn(unsigned long);
115 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
116 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
117 static struct task_struct *ap_poll_kthread;
118 static DEFINE_MUTEX(ap_poll_thread_mutex);
119 static DEFINE_SPINLOCK(ap_poll_timer_lock);
120 static struct hrtimer ap_poll_timer;
122 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
123 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
125 static unsigned long 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, struct ap_tapq_hwinfo *hwinfo,
361 bool *decfg, bool *cstop)
363 struct ap_queue_status status;
367 /* make sure we don't run into a specifiation exception */
368 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
369 AP_QID_QUEUE(qid) > ap_max_domain_id)
372 /* call TAPQ on this APQN */
373 status = ap_test_queue(qid, ap_apft_available(), hwinfo);
375 switch (status.response_code) {
376 case AP_RESPONSE_NORMAL:
377 case AP_RESPONSE_RESET_IN_PROGRESS:
378 case AP_RESPONSE_DECONFIGURED:
379 case AP_RESPONSE_CHECKSTOPPED:
380 case AP_RESPONSE_BUSY:
381 /* For all these RCs the tapq info should be available */
384 /* On a pending async error the info should be available */
390 /* There should be at least one of the mode bits set */
391 if (WARN_ON_ONCE(!hwinfo->value))
394 *decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
395 *cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
400 void ap_wait(enum ap_sm_wait wait)
405 case AP_SM_WAIT_AGAIN:
406 case AP_SM_WAIT_INTERRUPT:
409 if (ap_poll_kthread) {
410 wake_up(&ap_poll_wait);
414 case AP_SM_WAIT_LOW_TIMEOUT:
415 case AP_SM_WAIT_HIGH_TIMEOUT:
416 spin_lock_bh(&ap_poll_timer_lock);
417 if (!hrtimer_is_queued(&ap_poll_timer)) {
419 wait == AP_SM_WAIT_LOW_TIMEOUT ?
420 poll_low_timeout : poll_high_timeout;
421 hrtimer_forward_now(&ap_poll_timer, hr_time);
422 hrtimer_restart(&ap_poll_timer);
424 spin_unlock_bh(&ap_poll_timer_lock);
426 case AP_SM_WAIT_NONE:
433 * ap_request_timeout(): Handling of request timeouts
434 * @t: timer making this callback
436 * Handles request timeouts.
438 void ap_request_timeout(struct timer_list *t)
440 struct ap_queue *aq = from_timer(aq, t, timeout);
442 spin_lock_bh(&aq->lock);
443 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
444 spin_unlock_bh(&aq->lock);
448 * ap_poll_timeout(): AP receive polling for finished AP requests.
449 * @unused: Unused pointer.
451 * Schedules the AP tasklet using a high resolution timer.
453 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
455 tasklet_schedule(&ap_tasklet);
456 return HRTIMER_NORESTART;
460 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
461 * @airq: pointer to adapter interrupt descriptor
464 static void ap_interrupt_handler(struct airq_struct *airq,
465 struct tpi_info *tpi_info)
467 inc_irq_stat(IRQIO_APB);
468 tasklet_schedule(&ap_tasklet);
472 * ap_tasklet_fn(): Tasklet to poll all AP devices.
473 * @dummy: Unused variable
475 * Poll all AP devices on the bus.
477 static void ap_tasklet_fn(unsigned long dummy)
481 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
483 /* Reset the indicator if interrupts are used. Thus new interrupts can
484 * be received. Doing it in the beginning of the tasklet is therefore
485 * important that no requests on any AP get lost.
488 xchg(ap_airq.lsi_ptr, 0);
490 spin_lock_bh(&ap_queues_lock);
491 hash_for_each(ap_queues, bkt, aq, hnode) {
492 spin_lock_bh(&aq->lock);
493 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
494 spin_unlock_bh(&aq->lock);
496 spin_unlock_bh(&ap_queues_lock);
501 static int ap_pending_requests(void)
506 spin_lock_bh(&ap_queues_lock);
507 hash_for_each(ap_queues, bkt, aq, hnode) {
508 if (aq->queue_count == 0)
510 spin_unlock_bh(&ap_queues_lock);
513 spin_unlock_bh(&ap_queues_lock);
518 * ap_poll_thread(): Thread that polls for finished requests.
519 * @data: Unused pointer
521 * AP bus poll thread. The purpose of this thread is to poll for
522 * finished requests in a loop if there is a "free" cpu - that is
523 * a cpu that doesn't have anything better to do. The polling stops
524 * as soon as there is another task or if all messages have been
527 static int ap_poll_thread(void *data)
529 DECLARE_WAITQUEUE(wait, current);
531 set_user_nice(current, MAX_NICE);
533 while (!kthread_should_stop()) {
534 add_wait_queue(&ap_poll_wait, &wait);
535 set_current_state(TASK_INTERRUPTIBLE);
536 if (!ap_pending_requests()) {
540 set_current_state(TASK_RUNNING);
541 remove_wait_queue(&ap_poll_wait, &wait);
542 if (need_resched()) {
553 static int ap_poll_thread_start(void)
557 if (ap_irq_flag || ap_poll_kthread)
559 mutex_lock(&ap_poll_thread_mutex);
560 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
561 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
563 ap_poll_kthread = NULL;
564 mutex_unlock(&ap_poll_thread_mutex);
568 static void ap_poll_thread_stop(void)
570 if (!ap_poll_kthread)
572 mutex_lock(&ap_poll_thread_mutex);
573 kthread_stop(ap_poll_kthread);
574 ap_poll_kthread = NULL;
575 mutex_unlock(&ap_poll_thread_mutex);
578 #define is_card_dev(x) ((x)->parent == ap_root_device)
579 #define is_queue_dev(x) ((x)->parent != ap_root_device)
583 * @dev: Pointer to device
584 * @drv: Pointer to device_driver
586 * AP bus driver registration/unregistration.
588 static int ap_bus_match(struct device *dev, struct device_driver *drv)
590 struct ap_driver *ap_drv = to_ap_drv(drv);
591 struct ap_device_id *id;
594 * Compare device type of the device with the list of
595 * supported types of the device_driver.
597 for (id = ap_drv->ids; id->match_flags; id++) {
598 if (is_card_dev(dev) &&
599 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
600 id->dev_type == to_ap_dev(dev)->device_type)
602 if (is_queue_dev(dev) &&
603 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
604 id->dev_type == to_ap_dev(dev)->device_type)
611 * ap_uevent(): Uevent function for AP devices.
612 * @dev: Pointer to device
613 * @env: Pointer to kobj_uevent_env
615 * It sets up a single environment variable DEV_TYPE which contains the
616 * hardware device type.
618 static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env)
621 const struct ap_device *ap_dev = to_ap_dev(dev);
623 /* Uevents from ap bus core don't need extensions to the env */
624 if (dev == ap_root_device)
627 if (is_card_dev(dev)) {
628 struct ap_card *ac = to_ap_card(&ap_dev->device);
630 /* Set up DEV_TYPE environment variable. */
631 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
635 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
639 /* Add MODE=<accel|cca|ep11> */
640 if (ac->hwinfo.accel)
641 rc = add_uevent_var(env, "MODE=accel");
642 else if (ac->hwinfo.cca)
643 rc = add_uevent_var(env, "MODE=cca");
644 else if (ac->hwinfo.ep11)
645 rc = add_uevent_var(env, "MODE=ep11");
649 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
651 /* Add MODE=<accel|cca|ep11> */
652 if (aq->card->hwinfo.accel)
653 rc = add_uevent_var(env, "MODE=accel");
654 else if (aq->card->hwinfo.cca)
655 rc = add_uevent_var(env, "MODE=cca");
656 else if (aq->card->hwinfo.ep11)
657 rc = add_uevent_var(env, "MODE=ep11");
665 static void ap_send_init_scan_done_uevent(void)
667 char *envp[] = { "INITSCAN=done", NULL };
669 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
672 static void ap_send_bindings_complete_uevent(void)
675 char *envp[] = { "BINDINGS=complete", buf, NULL };
677 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
678 atomic64_inc_return(&ap_bindings_complete_count));
679 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
682 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
685 char *envp[] = { buf, NULL };
687 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
689 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
691 EXPORT_SYMBOL(ap_send_config_uevent);
693 void ap_send_online_uevent(struct ap_device *ap_dev, int online)
696 char *envp[] = { buf, NULL };
698 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
700 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
702 EXPORT_SYMBOL(ap_send_online_uevent);
704 static void ap_send_mask_changed_uevent(unsigned long *newapm,
705 unsigned long *newaqm)
708 char *envp[] = { buf, NULL };
711 snprintf(buf, sizeof(buf),
712 "APMASK=0x%016lx%016lx%016lx%016lx\n",
713 newapm[0], newapm[1], newapm[2], newapm[3]);
715 snprintf(buf, sizeof(buf),
716 "AQMASK=0x%016lx%016lx%016lx%016lx\n",
717 newaqm[0], newaqm[1], newaqm[2], newaqm[3]);
719 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
723 * calc # of bound APQNs
726 struct __ap_calc_ctrs {
731 static int __ap_calc_helper(struct device *dev, void *arg)
733 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg;
735 if (is_queue_dev(dev)) {
744 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
746 struct __ap_calc_ctrs ctrs;
748 memset(&ctrs, 0, sizeof(ctrs));
749 bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper);
756 * After initial ap bus scan do check if all existing APQNs are
757 * bound to device drivers.
759 static void ap_check_bindings_complete(void)
761 unsigned int apqns, bound;
763 if (atomic64_read(&ap_scan_bus_count) >= 1) {
764 ap_calc_bound_apqns(&apqns, &bound);
765 if (bound == apqns) {
766 if (!completion_done(&ap_init_apqn_bindings_complete)) {
767 complete_all(&ap_init_apqn_bindings_complete);
768 AP_DBF_INFO("%s complete\n", __func__);
770 ap_send_bindings_complete_uevent();
776 * Interface to wait for the AP bus to have done one initial ap bus
777 * scan and all detected APQNs have been bound to device drivers.
778 * If these both conditions are not fulfilled, this function blocks
779 * on a condition with wait_for_completion_interruptible_timeout().
780 * If these both conditions are fulfilled (before the timeout hits)
781 * the return value is 0. If the timeout (in jiffies) hits instead
782 * -ETIME is returned. On failures negative return values are
783 * returned to the caller.
785 int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
789 if (completion_done(&ap_init_apqn_bindings_complete))
793 l = wait_for_completion_interruptible_timeout(
794 &ap_init_apqn_bindings_complete, timeout);
796 l = wait_for_completion_interruptible(
797 &ap_init_apqn_bindings_complete);
799 return l == -ERESTARTSYS ? -EINTR : l;
800 else if (l == 0 && timeout)
805 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
807 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
809 if (is_queue_dev(dev) &&
810 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data)
811 device_unregister(dev);
815 static int __ap_revise_reserved(struct device *dev, void *dummy)
817 int rc, card, queue, devres, drvres;
819 if (is_queue_dev(dev)) {
820 card = AP_QID_CARD(to_ap_queue(dev)->qid);
821 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
822 mutex_lock(&ap_perms_mutex);
823 devres = test_bit_inv(card, ap_perms.apm) &&
824 test_bit_inv(queue, ap_perms.aqm);
825 mutex_unlock(&ap_perms_mutex);
826 drvres = to_ap_drv(dev->driver)->flags
827 & AP_DRIVER_FLAG_DEFAULT;
828 if (!!devres != !!drvres) {
829 AP_DBF_DBG("%s reprobing queue=%02x.%04x\n",
830 __func__, card, queue);
831 rc = device_reprobe(dev);
833 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
834 __func__, card, queue);
841 static void ap_bus_revise_bindings(void)
843 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
847 * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
848 * default host driver or not.
849 * @card: the APID of the adapter card to check
850 * @queue: the APQI of the queue to check
852 * Note: the ap_perms_mutex must be locked by the caller of this function.
854 * Return: an int specifying whether the AP adapter is reserved for the host (1)
857 int ap_owned_by_def_drv(int card, int queue)
861 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
864 if (test_bit_inv(card, ap_perms.apm) &&
865 test_bit_inv(queue, ap_perms.aqm))
870 EXPORT_SYMBOL(ap_owned_by_def_drv);
873 * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
874 * a set is reserved for the host drivers
876 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
877 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
879 * Note: the ap_perms_mutex must be locked by the caller of this function.
881 * Return: an int specifying whether each APQN is reserved for the host (1) or
884 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
887 int card, queue, rc = 0;
889 for (card = 0; !rc && card < AP_DEVICES; card++)
890 if (test_bit_inv(card, apm) &&
891 test_bit_inv(card, ap_perms.apm))
892 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
893 if (test_bit_inv(queue, aqm) &&
894 test_bit_inv(queue, ap_perms.aqm))
899 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
901 static int ap_device_probe(struct device *dev)
903 struct ap_device *ap_dev = to_ap_dev(dev);
904 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
905 int card, queue, devres, drvres, rc = -ENODEV;
907 if (!get_device(dev))
910 if (is_queue_dev(dev)) {
912 * If the apqn is marked as reserved/used by ap bus and
913 * default drivers, only probe with drivers with the default
914 * flag set. If it is not marked, only probe with drivers
915 * with the default flag not set.
917 card = AP_QID_CARD(to_ap_queue(dev)->qid);
918 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
919 mutex_lock(&ap_perms_mutex);
920 devres = test_bit_inv(card, ap_perms.apm) &&
921 test_bit_inv(queue, ap_perms.aqm);
922 mutex_unlock(&ap_perms_mutex);
923 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
924 if (!!devres != !!drvres)
928 /* Add queue/card to list of active queues/cards */
929 spin_lock_bh(&ap_queues_lock);
930 if (is_queue_dev(dev))
931 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
932 to_ap_queue(dev)->qid);
933 spin_unlock_bh(&ap_queues_lock);
935 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
938 spin_lock_bh(&ap_queues_lock);
939 if (is_queue_dev(dev))
940 hash_del(&to_ap_queue(dev)->hnode);
941 spin_unlock_bh(&ap_queues_lock);
943 ap_check_bindings_complete();
952 static void ap_device_remove(struct device *dev)
954 struct ap_device *ap_dev = to_ap_dev(dev);
955 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
957 /* prepare ap queue device removal */
958 if (is_queue_dev(dev))
959 ap_queue_prepare_remove(to_ap_queue(dev));
961 /* driver's chance to clean up gracefully */
963 ap_drv->remove(ap_dev);
965 /* now do the ap queue device remove */
966 if (is_queue_dev(dev))
967 ap_queue_remove(to_ap_queue(dev));
969 /* Remove queue/card from list of active queues/cards */
970 spin_lock_bh(&ap_queues_lock);
971 if (is_queue_dev(dev))
972 hash_del(&to_ap_queue(dev)->hnode);
973 spin_unlock_bh(&ap_queues_lock);
978 struct ap_queue *ap_get_qdev(ap_qid_t qid)
983 spin_lock_bh(&ap_queues_lock);
984 hash_for_each(ap_queues, bkt, aq, hnode) {
985 if (aq->qid == qid) {
986 get_device(&aq->ap_dev.device);
987 spin_unlock_bh(&ap_queues_lock);
991 spin_unlock_bh(&ap_queues_lock);
995 EXPORT_SYMBOL(ap_get_qdev);
997 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1000 struct device_driver *drv = &ap_drv->driver;
1002 drv->bus = &ap_bus_type;
1005 return driver_register(drv);
1007 EXPORT_SYMBOL(ap_driver_register);
1009 void ap_driver_unregister(struct ap_driver *ap_drv)
1011 driver_unregister(&ap_drv->driver);
1013 EXPORT_SYMBOL(ap_driver_unregister);
1015 void ap_bus_force_rescan(void)
1017 /* Only trigger AP bus scans after the initial scan is done */
1018 if (atomic64_read(&ap_scan_bus_count) <= 0)
1021 /* processing a asynchronous bus rescan */
1022 del_timer(&ap_config_timer);
1023 queue_work(system_long_wq, &ap_scan_work);
1024 flush_work(&ap_scan_work);
1026 EXPORT_SYMBOL(ap_bus_force_rescan);
1029 * A config change has happened, force an ap bus rescan.
1031 void ap_bus_cfg_chg(void)
1033 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
1035 ap_bus_force_rescan();
1039 * hex2bitmap() - parse hex mask string and set bitmap.
1040 * Valid strings are "0x012345678" with at least one valid hex number.
1041 * Rest of the bitmap to the right is padded with 0. No spaces allowed
1042 * within the string, the leading 0x may be omitted.
1043 * Returns the bitmask with exactly the bits set as given by the hex
1044 * string (both in big endian order).
1046 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1050 /* bits needs to be a multiple of 8 */
1054 if (str[0] == '0' && str[1] == 'x')
1059 for (i = 0; isxdigit(*str) && i < bits; str++) {
1060 b = hex_to_bin(*str);
1061 for (n = 0; n < 4; n++)
1062 if (b & (0x08 >> n))
1063 set_bit_inv(i + n, bitmap);
1075 * modify_bitmap() - parse bitmask argument and modify an existing
1076 * bit mask accordingly. A concatenation (done with ',') of these
1077 * terms is recognized:
1078 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1079 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1080 * 0...bits-1; the leading + or - is required. Here are some examples:
1081 * +0-15,+32,-128,-0xFF
1082 * -0-255,+1-16,+0x128
1083 * +1,+2,+3,+4,-5,-7-10
1084 * Returns the new bitmap after all changes have been applied. Every
1085 * positive value in the string will set a bit and every negative value
1086 * in the string will clear a bit. As a bit may be touched more than once,
1087 * the last 'operation' wins:
1088 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1089 * cleared again. All other bits are unmodified.
1091 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1096 /* bits needs to be a multiple of 8 */
1102 if (sign != '+' && sign != '-')
1104 a = z = simple_strtoul(str, &np, 0);
1105 if (str == np || a >= bits)
1109 z = simple_strtoul(++str, &np, 0);
1110 if (str == np || a > z || z >= bits)
1114 for (i = a; i <= z; i++)
1116 set_bit_inv(i, bitmap);
1118 clear_bit_inv(i, bitmap);
1119 while (*str == ',' || *str == '\n')
1126 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1127 unsigned long *newmap)
1132 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1133 if (*str == '+' || *str == '-') {
1134 memcpy(newmap, bitmap, size);
1135 rc = modify_bitmap(str, newmap, bits);
1137 memset(newmap, 0, size);
1138 rc = hex2bitmap(str, newmap, bits);
1143 int ap_parse_mask_str(const char *str,
1144 unsigned long *bitmap, int bits,
1147 unsigned long *newmap, size;
1150 /* bits needs to be a multiple of 8 */
1154 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1155 newmap = kmalloc(size, GFP_KERNEL);
1158 if (mutex_lock_interruptible(lock)) {
1160 return -ERESTARTSYS;
1162 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
1164 memcpy(bitmap, newmap, size);
1169 EXPORT_SYMBOL(ap_parse_mask_str);
1172 * AP bus attributes.
1175 static ssize_t ap_domain_show(const struct bus_type *bus, char *buf)
1177 return sysfs_emit(buf, "%d\n", ap_domain_index);
1180 static ssize_t ap_domain_store(const struct bus_type *bus,
1181 const char *buf, size_t count)
1185 if (sscanf(buf, "%i\n", &domain) != 1 ||
1186 domain < 0 || domain > ap_max_domain_id ||
1187 !test_bit_inv(domain, ap_perms.aqm))
1190 spin_lock_bh(&ap_domain_lock);
1191 ap_domain_index = domain;
1192 spin_unlock_bh(&ap_domain_lock);
1194 AP_DBF_INFO("%s stored new default domain=%d\n",
1200 static BUS_ATTR_RW(ap_domain);
1202 static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf)
1204 if (!ap_qci_info) /* QCI not supported */
1205 return sysfs_emit(buf, "not supported\n");
1207 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1208 ap_qci_info->adm[0], ap_qci_info->adm[1],
1209 ap_qci_info->adm[2], ap_qci_info->adm[3],
1210 ap_qci_info->adm[4], ap_qci_info->adm[5],
1211 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1214 static BUS_ATTR_RO(ap_control_domain_mask);
1216 static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf)
1218 if (!ap_qci_info) /* QCI not supported */
1219 return sysfs_emit(buf, "not supported\n");
1221 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1222 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1223 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1224 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1225 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1228 static BUS_ATTR_RO(ap_usage_domain_mask);
1230 static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf)
1232 if (!ap_qci_info) /* QCI not supported */
1233 return sysfs_emit(buf, "not supported\n");
1235 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1236 ap_qci_info->apm[0], ap_qci_info->apm[1],
1237 ap_qci_info->apm[2], ap_qci_info->apm[3],
1238 ap_qci_info->apm[4], ap_qci_info->apm[5],
1239 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1242 static BUS_ATTR_RO(ap_adapter_mask);
1244 static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf)
1246 return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0);
1249 static BUS_ATTR_RO(ap_interrupts);
1251 static ssize_t config_time_show(const struct bus_type *bus, char *buf)
1253 return sysfs_emit(buf, "%d\n", ap_config_time);
1256 static ssize_t config_time_store(const struct bus_type *bus,
1257 const char *buf, size_t count)
1261 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1263 ap_config_time = time;
1264 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1268 static BUS_ATTR_RW(config_time);
1270 static ssize_t poll_thread_show(const struct bus_type *bus, char *buf)
1272 return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0);
1275 static ssize_t poll_thread_store(const struct bus_type *bus,
1276 const char *buf, size_t count)
1281 rc = kstrtobool(buf, &value);
1286 rc = ap_poll_thread_start();
1290 ap_poll_thread_stop();
1295 static BUS_ATTR_RW(poll_thread);
1297 static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf)
1299 return sysfs_emit(buf, "%lu\n", poll_high_timeout);
1302 static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf,
1305 unsigned long value;
1309 rc = kstrtoul(buf, 0, &value);
1313 /* 120 seconds = maximum poll interval */
1314 if (value > 120000000000UL)
1316 poll_high_timeout = value;
1317 hr_time = poll_high_timeout;
1319 spin_lock_bh(&ap_poll_timer_lock);
1320 hrtimer_cancel(&ap_poll_timer);
1321 hrtimer_set_expires(&ap_poll_timer, hr_time);
1322 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1323 spin_unlock_bh(&ap_poll_timer_lock);
1328 static BUS_ATTR_RW(poll_timeout);
1330 static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf)
1332 return sysfs_emit(buf, "%d\n", ap_max_domain_id);
1335 static BUS_ATTR_RO(ap_max_domain_id);
1337 static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf)
1339 return sysfs_emit(buf, "%d\n", ap_max_adapter_id);
1342 static BUS_ATTR_RO(ap_max_adapter_id);
1344 static ssize_t apmask_show(const struct bus_type *bus, char *buf)
1348 if (mutex_lock_interruptible(&ap_perms_mutex))
1349 return -ERESTARTSYS;
1350 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1351 ap_perms.apm[0], ap_perms.apm[1],
1352 ap_perms.apm[2], ap_perms.apm[3]);
1353 mutex_unlock(&ap_perms_mutex);
1358 static int __verify_card_reservations(struct device_driver *drv, void *data)
1361 struct ap_driver *ap_drv = to_ap_drv(drv);
1362 unsigned long *newapm = (unsigned long *)data;
1365 * increase the driver's module refcounter to be sure it is not
1366 * going away when we invoke the callback function.
1368 if (!try_module_get(drv->owner))
1371 if (ap_drv->in_use) {
1372 rc = ap_drv->in_use(newapm, ap_perms.aqm);
1377 /* release the driver's module */
1378 module_put(drv->owner);
1383 static int apmask_commit(unsigned long *newapm)
1386 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1389 * Check if any bits in the apmask have been set which will
1390 * result in queues being removed from non-default drivers
1392 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1393 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1394 __verify_card_reservations);
1399 memcpy(ap_perms.apm, newapm, APMASKSIZE);
1404 static ssize_t apmask_store(const struct bus_type *bus, const char *buf,
1407 int rc, changes = 0;
1408 DECLARE_BITMAP(newapm, AP_DEVICES);
1410 if (mutex_lock_interruptible(&ap_perms_mutex))
1411 return -ERESTARTSYS;
1413 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1417 changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
1419 rc = apmask_commit(newapm);
1422 mutex_unlock(&ap_perms_mutex);
1427 ap_bus_revise_bindings();
1428 ap_send_mask_changed_uevent(newapm, NULL);
1434 static BUS_ATTR_RW(apmask);
1436 static ssize_t aqmask_show(const struct bus_type *bus, char *buf)
1440 if (mutex_lock_interruptible(&ap_perms_mutex))
1441 return -ERESTARTSYS;
1442 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1443 ap_perms.aqm[0], ap_perms.aqm[1],
1444 ap_perms.aqm[2], ap_perms.aqm[3]);
1445 mutex_unlock(&ap_perms_mutex);
1450 static int __verify_queue_reservations(struct device_driver *drv, void *data)
1453 struct ap_driver *ap_drv = to_ap_drv(drv);
1454 unsigned long *newaqm = (unsigned long *)data;
1457 * increase the driver's module refcounter to be sure it is not
1458 * going away when we invoke the callback function.
1460 if (!try_module_get(drv->owner))
1463 if (ap_drv->in_use) {
1464 rc = ap_drv->in_use(ap_perms.apm, newaqm);
1469 /* release the driver's module */
1470 module_put(drv->owner);
1475 static int aqmask_commit(unsigned long *newaqm)
1478 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1481 * Check if any bits in the aqmask have been set which will
1482 * result in queues being removed from non-default drivers
1484 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1485 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1486 __verify_queue_reservations);
1491 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1496 static ssize_t aqmask_store(const struct bus_type *bus, const char *buf,
1499 int rc, changes = 0;
1500 DECLARE_BITMAP(newaqm, AP_DOMAINS);
1502 if (mutex_lock_interruptible(&ap_perms_mutex))
1503 return -ERESTARTSYS;
1505 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1509 changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
1511 rc = aqmask_commit(newaqm);
1514 mutex_unlock(&ap_perms_mutex);
1519 ap_bus_revise_bindings();
1520 ap_send_mask_changed_uevent(NULL, newaqm);
1526 static BUS_ATTR_RW(aqmask);
1528 static ssize_t scans_show(const struct bus_type *bus, char *buf)
1530 return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count));
1533 static ssize_t scans_store(const struct bus_type *bus, const char *buf,
1536 AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1538 ap_bus_force_rescan();
1543 static BUS_ATTR_RW(scans);
1545 static ssize_t bindings_show(const struct bus_type *bus, char *buf)
1548 unsigned int apqns, n;
1550 ap_calc_bound_apqns(&apqns, &n);
1551 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1552 rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns);
1554 rc = sysfs_emit(buf, "%u/%u\n", n, apqns);
1559 static BUS_ATTR_RO(bindings);
1561 static ssize_t features_show(const struct bus_type *bus, char *buf)
1565 if (!ap_qci_info) /* QCI not supported */
1566 return sysfs_emit(buf, "-\n");
1568 if (ap_qci_info->apsc)
1569 n += sysfs_emit_at(buf, n, "APSC ");
1570 if (ap_qci_info->apxa)
1571 n += sysfs_emit_at(buf, n, "APXA ");
1572 if (ap_qci_info->qact)
1573 n += sysfs_emit_at(buf, n, "QACT ");
1574 if (ap_qci_info->rc8a)
1575 n += sysfs_emit_at(buf, n, "RC8A ");
1576 if (ap_qci_info->apsb)
1577 n += sysfs_emit_at(buf, n, "APSB ");
1579 sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n");
1584 static BUS_ATTR_RO(features);
1586 static struct attribute *ap_bus_attrs[] = {
1587 &bus_attr_ap_domain.attr,
1588 &bus_attr_ap_control_domain_mask.attr,
1589 &bus_attr_ap_usage_domain_mask.attr,
1590 &bus_attr_ap_adapter_mask.attr,
1591 &bus_attr_config_time.attr,
1592 &bus_attr_poll_thread.attr,
1593 &bus_attr_ap_interrupts.attr,
1594 &bus_attr_poll_timeout.attr,
1595 &bus_attr_ap_max_domain_id.attr,
1596 &bus_attr_ap_max_adapter_id.attr,
1597 &bus_attr_apmask.attr,
1598 &bus_attr_aqmask.attr,
1599 &bus_attr_scans.attr,
1600 &bus_attr_bindings.attr,
1601 &bus_attr_features.attr,
1604 ATTRIBUTE_GROUPS(ap_bus);
1606 static struct bus_type ap_bus_type = {
1608 .bus_groups = ap_bus_groups,
1609 .match = &ap_bus_match,
1610 .uevent = &ap_uevent,
1611 .probe = ap_device_probe,
1612 .remove = ap_device_remove,
1616 * ap_select_domain(): Select an AP domain if possible and we haven't
1617 * already done so before.
1619 static void ap_select_domain(void)
1621 struct ap_queue_status status;
1625 * Choose the default domain. Either the one specified with
1626 * the "domain=" parameter or the first domain with at least
1629 spin_lock_bh(&ap_domain_lock);
1630 if (ap_domain_index >= 0) {
1631 /* Domain has already been selected. */
1634 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1635 if (!ap_test_config_usage_domain(dom) ||
1636 !test_bit_inv(dom, ap_perms.aqm))
1638 for (card = 0; card <= ap_max_adapter_id; card++) {
1639 if (!ap_test_config_card_id(card) ||
1640 !test_bit_inv(card, ap_perms.apm))
1642 status = ap_test_queue(AP_MKQID(card, dom),
1643 ap_apft_available(),
1645 if (status.response_code == AP_RESPONSE_NORMAL)
1648 if (card <= ap_max_adapter_id)
1651 if (dom <= ap_max_domain_id) {
1652 ap_domain_index = dom;
1653 AP_DBF_INFO("%s new default domain is %d\n",
1654 __func__, ap_domain_index);
1657 spin_unlock_bh(&ap_domain_lock);
1661 * This function checks the type and returns either 0 for not
1662 * supported or the highest compatible type value (which may
1663 * include the input type value).
1665 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1669 /* < CEX4 is not supported */
1670 if (rawtype < AP_DEVICE_TYPE_CEX4) {
1671 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1672 __func__, AP_QID_CARD(qid),
1673 AP_QID_QUEUE(qid), rawtype);
1676 /* up to CEX8 known and fully supported */
1677 if (rawtype <= AP_DEVICE_TYPE_CEX8)
1680 * unknown new type > CEX8, check for compatibility
1681 * to the highest known and supported type which is
1682 * currently CEX8 with the help of the QACT function.
1684 if (ap_qact_available()) {
1685 struct ap_queue_status status;
1686 union ap_qact_ap_info apinfo = {0};
1688 apinfo.mode = (func >> 26) & 0x07;
1689 apinfo.cat = AP_DEVICE_TYPE_CEX8;
1690 status = ap_qact(qid, 0, &apinfo);
1691 if (status.response_code == AP_RESPONSE_NORMAL &&
1692 apinfo.cat >= AP_DEVICE_TYPE_CEX4 &&
1693 apinfo.cat <= AP_DEVICE_TYPE_CEX8)
1694 comp_type = apinfo.cat;
1697 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1698 __func__, AP_QID_CARD(qid),
1699 AP_QID_QUEUE(qid), rawtype);
1700 else if (comp_type != rawtype)
1701 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1702 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1703 rawtype, comp_type);
1708 * Helper function to be used with bus_find_dev
1709 * matches for the card device with the given id
1711 static int __match_card_device_with_id(struct device *dev, const void *data)
1713 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
1717 * Helper function to be used with bus_find_dev
1718 * matches for the queue device with a given qid
1720 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1722 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
1726 * Helper function to be used with bus_find_dev
1727 * matches any queue device with given queue id
1729 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1731 return is_queue_dev(dev) &&
1732 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
1735 /* Helper function for notify_config_changed */
1736 static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1738 struct ap_driver *ap_drv = to_ap_drv(drv);
1740 if (try_module_get(drv->owner)) {
1741 if (ap_drv->on_config_changed)
1742 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1743 module_put(drv->owner);
1749 /* Notify all drivers about an qci config change */
1750 static inline void notify_config_changed(void)
1752 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1753 __drv_notify_config_changed);
1756 /* Helper function for notify_scan_complete */
1757 static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1759 struct ap_driver *ap_drv = to_ap_drv(drv);
1761 if (try_module_get(drv->owner)) {
1762 if (ap_drv->on_scan_complete)
1763 ap_drv->on_scan_complete(ap_qci_info,
1765 module_put(drv->owner);
1771 /* Notify all drivers about bus scan complete */
1772 static inline void notify_scan_complete(void)
1774 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1775 __drv_notify_scan_complete);
1779 * Helper function for ap_scan_bus().
1780 * Remove card device and associated queue devices.
1782 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1784 bus_for_each_dev(&ap_bus_type, NULL,
1785 (void *)(long)ac->id,
1786 __ap_queue_devices_with_id_unregister);
1787 device_unregister(&ac->ap_dev.device);
1791 * Helper function for ap_scan_bus().
1792 * Does the scan bus job for all the domains within
1793 * a valid adapter given by an ap_card ptr.
1795 static inline void ap_scan_domains(struct ap_card *ac)
1797 struct ap_tapq_hwinfo hwinfo;
1798 bool decfg, chkstop;
1799 struct ap_queue *aq;
1805 * Go through the configuration for the domains and compare them
1806 * to the existing queue devices. Also take care of the config
1807 * and error state for the queue devices.
1810 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1811 qid = AP_MKQID(ac->id, dom);
1812 dev = bus_find_device(&ap_bus_type, NULL,
1814 __match_queue_device_with_qid);
1815 aq = dev ? to_ap_queue(dev) : NULL;
1816 if (!ap_test_config_usage_domain(dom)) {
1818 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
1819 __func__, ac->id, dom);
1820 device_unregister(dev);
1822 goto put_dev_and_continue;
1824 /* domain is valid, get info from this APQN */
1825 rc = ap_queue_info(qid, &hwinfo, &decfg, &chkstop);
1829 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1830 __func__, ac->id, dom);
1831 device_unregister(dev);
1835 goto put_dev_and_continue;
1839 /* if no queue device exists, create a new one */
1841 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1843 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1844 __func__, ac->id, dom);
1848 aq->config = !decfg;
1849 aq->chkstop = chkstop;
1850 aq->se_bstate = hwinfo.bs;
1851 dev = &aq->ap_dev.device;
1852 dev->bus = &ap_bus_type;
1853 dev->parent = &ac->ap_dev.device;
1854 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1855 /* register queue device */
1856 rc = device_register(dev);
1858 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1859 __func__, ac->id, dom);
1860 goto put_dev_and_continue;
1862 /* get it and thus adjust reference counter */
1865 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
1866 __func__, ac->id, dom);
1867 } else if (chkstop) {
1868 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1869 __func__, ac->id, dom);
1871 /* nudge the queue's state machine */
1872 ap_queue_init_state(aq);
1873 AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
1874 __func__, ac->id, dom);
1876 goto put_dev_and_continue;
1878 /* handle state changes on already existing queue device */
1879 spin_lock_bh(&aq->lock);
1881 aq->se_bstate = hwinfo.bs;
1882 /* checkstop state */
1883 if (chkstop && !aq->chkstop) {
1886 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1887 aq->dev_state = AP_DEV_STATE_ERROR;
1888 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1890 spin_unlock_bh(&aq->lock);
1891 AP_DBF_DBG("%s(%d,%d) queue dev checkstop on\n",
1892 __func__, ac->id, dom);
1893 /* 'receive' pending messages with -EAGAIN */
1895 goto put_dev_and_continue;
1896 } else if (!chkstop && aq->chkstop) {
1898 aq->chkstop = false;
1899 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
1900 _ap_queue_init_state(aq);
1901 spin_unlock_bh(&aq->lock);
1902 AP_DBF_DBG("%s(%d,%d) queue dev checkstop off\n",
1903 __func__, ac->id, dom);
1904 goto put_dev_and_continue;
1906 /* config state change */
1907 if (decfg && aq->config) {
1908 /* config off this queue device */
1910 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1911 aq->dev_state = AP_DEV_STATE_ERROR;
1912 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1914 spin_unlock_bh(&aq->lock);
1915 AP_DBF_DBG("%s(%d,%d) queue dev config off\n",
1916 __func__, ac->id, dom);
1917 ap_send_config_uevent(&aq->ap_dev, aq->config);
1918 /* 'receive' pending messages with -EAGAIN */
1920 goto put_dev_and_continue;
1921 } else if (!decfg && !aq->config) {
1922 /* config on this queue device */
1924 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
1925 _ap_queue_init_state(aq);
1926 spin_unlock_bh(&aq->lock);
1927 AP_DBF_DBG("%s(%d,%d) queue dev config on\n",
1928 __func__, ac->id, dom);
1929 ap_send_config_uevent(&aq->ap_dev, aq->config);
1930 goto put_dev_and_continue;
1932 /* handle other error states */
1933 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1934 spin_unlock_bh(&aq->lock);
1935 /* 'receive' pending messages with -EAGAIN */
1937 /* re-init (with reset) the queue device */
1938 ap_queue_init_state(aq);
1939 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
1940 __func__, ac->id, dom);
1941 goto put_dev_and_continue;
1943 spin_unlock_bh(&aq->lock);
1944 put_dev_and_continue:
1950 * Helper function for ap_scan_bus().
1951 * Does the scan bus job for the given adapter id.
1953 static inline void ap_scan_adapter(int ap)
1955 struct ap_tapq_hwinfo hwinfo;
1956 int rc, dom, comp_type;
1957 bool decfg, chkstop;
1962 /* Is there currently a card device for this adapter ? */
1963 dev = bus_find_device(&ap_bus_type, NULL,
1965 __match_card_device_with_id);
1966 ac = dev ? to_ap_card(dev) : NULL;
1968 /* Adapter not in configuration ? */
1969 if (!ap_test_config_card_id(ap)) {
1971 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
1973 ap_scan_rm_card_dev_and_queue_devs(ac);
1980 * Adapter ap is valid in the current configuration. So do some checks:
1981 * If no card device exists, build one. If a card device exists, check
1982 * for type and functions changed. For all this we need to find a valid
1986 for (dom = 0; dom <= ap_max_domain_id; dom++)
1987 if (ap_test_config_usage_domain(dom)) {
1988 qid = AP_MKQID(ap, dom);
1989 if (ap_queue_info(qid, &hwinfo, &decfg, &chkstop) > 0)
1992 if (dom > ap_max_domain_id) {
1993 /* Could not find one valid APQN for this adapter */
1995 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
1997 ap_scan_rm_card_dev_and_queue_devs(ac);
2000 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
2006 /* No apdater type info available, an unusable adapter */
2008 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
2010 ap_scan_rm_card_dev_and_queue_devs(ac);
2013 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
2018 hwinfo.value &= TAPQ_CARD_HWINFO_MASK; /* filter card specific hwinfo */
2020 /* Check APQN against existing card device for changes */
2021 if (ac->hwinfo.at != hwinfo.at) {
2022 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
2023 __func__, ap, hwinfo.at);
2024 ap_scan_rm_card_dev_and_queue_devs(ac);
2027 } else if (ac->hwinfo.fac != hwinfo.fac) {
2028 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
2029 __func__, ap, hwinfo.fac);
2030 ap_scan_rm_card_dev_and_queue_devs(ac);
2034 /* handle checkstop state change */
2035 if (chkstop && !ac->chkstop) {
2038 AP_DBF_INFO("%s(%d) card dev checkstop on\n",
2040 } else if (!chkstop && ac->chkstop) {
2042 ac->chkstop = false;
2043 AP_DBF_INFO("%s(%d) card dev checkstop off\n",
2046 /* handle config state change */
2047 if (decfg && ac->config) {
2049 AP_DBF_INFO("%s(%d) card dev config off\n",
2051 ap_send_config_uevent(&ac->ap_dev, ac->config);
2052 } else if (!decfg && !ac->config) {
2054 AP_DBF_INFO("%s(%d) card dev config on\n",
2056 ap_send_config_uevent(&ac->ap_dev, ac->config);
2062 /* Build a new card device */
2063 comp_type = ap_get_compatible_type(qid, hwinfo.at, hwinfo.fac);
2065 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
2066 __func__, ap, hwinfo.at);
2069 ac = ap_card_create(ap, hwinfo, comp_type);
2071 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
2075 ac->config = !decfg;
2076 ac->chkstop = chkstop;
2077 dev = &ac->ap_dev.device;
2078 dev->bus = &ap_bus_type;
2079 dev->parent = ap_root_device;
2080 dev_set_name(dev, "card%02x", ap);
2081 /* maybe enlarge ap_max_msg_size to support this card */
2082 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
2083 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
2084 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
2086 atomic_read(&ap_max_msg_size));
2088 /* Register the new card device with AP bus */
2089 rc = device_register(dev);
2091 AP_DBF_WARN("%s(%d) device_register() failed\n",
2096 /* get it and thus adjust reference counter */
2099 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
2100 __func__, ap, hwinfo.at, hwinfo.fac);
2102 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
2103 __func__, ap, hwinfo.at, hwinfo.fac);
2105 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
2106 __func__, ap, hwinfo.at, hwinfo.fac);
2109 /* Verify the domains and the queue devices for this card */
2110 ap_scan_domains(ac);
2112 /* release the card device */
2113 put_device(&ac->ap_dev.device);
2117 * ap_get_configuration - get the host AP configuration
2119 * Stores the host AP configuration information returned from the previous call
2120 * to Query Configuration Information (QCI), then retrieves and stores the
2121 * current AP configuration returned from QCI.
2123 * Return: true if the host AP configuration changed between calls to QCI;
2124 * otherwise, return false.
2126 static bool ap_get_configuration(void)
2128 if (!ap_qci_info) /* QCI not supported */
2131 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2132 ap_fetch_qci_info(ap_qci_info);
2134 return memcmp(ap_qci_info, ap_qci_info_old,
2135 sizeof(struct ap_config_info)) != 0;
2139 * ap_scan_bus(): Scan the AP bus for new devices
2140 * Runs periodically, workqueue timer (ap_config_time)
2141 * @unused: Unused pointer.
2143 static void ap_scan_bus(struct work_struct *unused)
2145 int ap, config_changed = 0;
2147 /* config change notify */
2148 config_changed = ap_get_configuration();
2150 notify_config_changed();
2153 AP_DBF_DBG("%s running\n", __func__);
2155 /* loop over all possible adapters */
2156 for (ap = 0; ap <= ap_max_adapter_id; ap++)
2157 ap_scan_adapter(ap);
2159 /* scan complete notify */
2161 notify_scan_complete();
2163 /* check if there is at least one queue available with default domain */
2164 if (ap_domain_index >= 0) {
2165 struct device *dev =
2166 bus_find_device(&ap_bus_type, NULL,
2167 (void *)(long)ap_domain_index,
2168 __match_queue_device_with_queue_id);
2172 AP_DBF_INFO("%s no queue device with default domain %d available\n",
2173 __func__, ap_domain_index);
2176 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
2177 AP_DBF_DBG("%s init scan complete\n", __func__);
2178 ap_send_init_scan_done_uevent();
2179 ap_check_bindings_complete();
2182 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
2185 static void ap_config_timeout(struct timer_list *unused)
2187 queue_work(system_long_wq, &ap_scan_work);
2190 static int __init ap_debug_init(void)
2192 ap_dbf_info = debug_register("ap", 2, 1,
2193 DBF_MAX_SPRINTF_ARGS * sizeof(long));
2194 debug_register_view(ap_dbf_info, &debug_sprintf_view);
2195 debug_set_level(ap_dbf_info, DBF_ERR);
2200 static void __init ap_perms_init(void)
2202 /* all resources usable if no kernel parameter string given */
2203 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
2204 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2205 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2207 /* apm kernel parameter string */
2209 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
2210 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2214 /* aqm kernel parameter string */
2216 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
2217 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2223 * ap_module_init(): The module initialization code.
2225 * Initializes the module.
2227 static int __init ap_module_init(void)
2231 rc = ap_debug_init();
2235 if (!ap_instructions_available()) {
2236 pr_warn("The hardware system does not support AP instructions\n");
2240 /* init ap_queue hashtable */
2241 hash_init(ap_queues);
2243 /* set up the AP permissions (ioctls, ap and aq masks) */
2246 /* Get AP configuration data if available */
2249 /* check default domain setting */
2250 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
2251 (ap_domain_index >= 0 &&
2252 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
2253 pr_warn("%d is not a valid cryptographic domain\n",
2255 ap_domain_index = -1;
2258 /* enable interrupts if available */
2259 if (ap_interrupts_available() && ap_useirq) {
2260 rc = register_adapter_interrupt(&ap_airq);
2261 ap_irq_flag = (rc == 0);
2264 /* Create /sys/bus/ap. */
2265 rc = bus_register(&ap_bus_type);
2269 /* Create /sys/devices/ap. */
2270 ap_root_device = root_device_register("ap");
2271 rc = PTR_ERR_OR_ZERO(ap_root_device);
2274 ap_root_device->bus = &ap_bus_type;
2276 /* Setup the AP bus rescan timer. */
2277 timer_setup(&ap_config_timer, ap_config_timeout, 0);
2280 * Setup the high resolution poll timer.
2281 * If we are running under z/VM adjust polling to z/VM polling rate.
2284 poll_high_timeout = 1500000;
2285 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2286 ap_poll_timer.function = ap_poll_timeout;
2288 /* Start the low priority AP bus poll thread. */
2289 if (ap_thread_flag) {
2290 rc = ap_poll_thread_start();
2295 queue_work(system_long_wq, &ap_scan_work);
2300 hrtimer_cancel(&ap_poll_timer);
2301 root_device_unregister(ap_root_device);
2303 bus_unregister(&ap_bus_type);
2306 unregister_adapter_interrupt(&ap_airq);
2310 device_initcall(ap_module_init);