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/export.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/err.h>
23 #include <linux/freezer.h>
24 #include <linux/interrupt.h>
25 #include <linux/workqueue.h>
26 #include <linux/slab.h>
27 #include <linux/notifier.h>
28 #include <linux/kthread.h>
29 #include <linux/mutex.h>
30 #include <asm/machine.h>
33 #include <linux/atomic.h>
35 #include <linux/hrtimer.h>
36 #include <linux/ktime.h>
37 #include <asm/facility.h>
38 #include <linux/crypto.h>
39 #include <linux/mod_devicetable.h>
40 #include <linux/debugfs.h>
41 #include <linux/ctype.h>
42 #include <linux/module.h>
45 #include <linux/mempool.h>
50 MODULE_AUTHOR("IBM Corporation");
51 MODULE_DESCRIPTION("Adjunct Processor Bus driver");
52 MODULE_LICENSE("GPL");
54 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
55 static DEFINE_SPINLOCK(ap_domain_lock);
56 module_param_named(domain, ap_domain_index, int, 0440);
57 MODULE_PARM_DESC(domain, "domain index for ap devices");
58 EXPORT_SYMBOL(ap_domain_index);
60 static int ap_thread_flag;
61 module_param_named(poll_thread, ap_thread_flag, int, 0440);
62 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
65 module_param_named(apmask, apm_str, charp, 0440);
66 MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
69 module_param_named(aqmask, aqm_str, charp, 0440);
70 MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
72 static int ap_useirq = 1;
73 module_param_named(useirq, ap_useirq, int, 0440);
74 MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on).");
76 atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
77 EXPORT_SYMBOL(ap_max_msg_size);
79 static struct device *ap_root_device;
81 /* Hashtable of all queue devices on the AP bus */
82 DEFINE_HASHTABLE(ap_queues, 8);
83 /* lock used for the ap_queues hashtable */
84 DEFINE_SPINLOCK(ap_queues_lock);
86 /* Default permissions (ioctl, card and domain masking) */
87 struct ap_perms ap_perms;
88 EXPORT_SYMBOL(ap_perms);
89 DEFINE_MUTEX(ap_perms_mutex);
90 EXPORT_SYMBOL(ap_perms_mutex);
92 /* # of bindings complete since init */
93 static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
95 /* completion for APQN bindings complete */
96 static DECLARE_COMPLETION(ap_apqn_bindings_complete);
98 static struct ap_config_info qci[2];
99 static struct ap_config_info *const ap_qci_info = &qci[0];
100 static struct ap_config_info *const ap_qci_info_old = &qci[1];
103 * AP bus related debug feature things.
105 debug_info_t *ap_dbf_info;
108 * There is a need for a do-not-allocate-memory path through the AP bus
109 * layer. The pkey layer may be triggered via the in-kernel interface from
110 * a protected key crypto algorithm (namely PAES) to convert a secure key
111 * into a protected key. This happens in a workqueue context, so sleeping
112 * is allowed but memory allocations causing IO operations are not permitted.
113 * To accomplish this, an AP message memory pool with pre-allocated space
114 * is established. When ap_init_apmsg() with use_mempool set to true is
115 * called, instead of kmalloc() the ap message buffer is allocated from
116 * the ap_msg_pool. This pool only holds a limited amount of buffers:
117 * ap_msg_pool_min_items with the item size AP_DEFAULT_MAX_MSG_SIZE and
118 * exactly one of these items (if available) is returned if ap_init_apmsg()
119 * with the use_mempool arg set to true is called. When this pool is exhausted
120 * and use_mempool is set true, ap_init_apmsg() returns -ENOMEM without
121 * any attempt to allocate memory and the caller has to deal with that.
123 static mempool_t *ap_msg_pool;
124 static unsigned int ap_msg_pool_min_items = 8;
125 module_param_named(msgpool_min_items, ap_msg_pool_min_items, uint, 0440);
126 MODULE_PARM_DESC(msgpool_min_items, "AP message pool minimal items");
129 * AP bus rescan related things.
131 static bool ap_scan_bus(void);
132 static bool ap_scan_bus_result; /* result of last ap_scan_bus() */
133 static DEFINE_MUTEX(ap_scan_bus_mutex); /* mutex ap_scan_bus() invocations */
134 static struct task_struct *ap_scan_bus_task; /* thread holding the scan mutex */
135 static atomic64_t ap_scan_bus_count; /* counter ap_scan_bus() invocations */
136 static int ap_scan_bus_time = AP_CONFIG_TIME;
137 static struct timer_list ap_scan_bus_timer;
138 static void ap_scan_bus_wq_callback(struct work_struct *);
139 static DECLARE_WORK(ap_scan_bus_work, ap_scan_bus_wq_callback);
142 * Tasklet & timer for AP request polling and interrupts
144 static void ap_tasklet_fn(unsigned long);
145 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
146 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
147 static struct task_struct *ap_poll_kthread;
148 static DEFINE_MUTEX(ap_poll_thread_mutex);
149 static DEFINE_SPINLOCK(ap_poll_timer_lock);
150 static struct hrtimer ap_poll_timer;
152 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
153 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
155 static unsigned long poll_high_timeout = 250000UL;
158 * Some state machine states only require a low frequency polling.
159 * We use 25 Hz frequency for these.
161 static unsigned long poll_low_timeout = 40000000UL;
163 /* Maximum domain id, if not given via qci */
164 static int ap_max_domain_id = 15;
165 /* Maximum adapter id, if not given via qci */
166 static int ap_max_adapter_id = 63;
168 static const struct bus_type ap_bus_type;
170 /* Adapter interrupt definitions */
171 static void ap_interrupt_handler(struct airq_struct *airq,
172 struct tpi_info *tpi_info);
174 static bool ap_irq_flag;
176 static struct airq_struct ap_airq = {
177 .handler = ap_interrupt_handler,
182 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
184 * Returns the address of the local-summary-indicator of the adapter
185 * interrupt handler for AP, or NULL if adapter interrupts are not
188 void *ap_airq_ptr(void)
191 return ap_airq.lsi_ptr;
196 * ap_interrupts_available(): Test if AP interrupts are available.
198 * Returns 1 if AP interrupts are available.
200 static int ap_interrupts_available(void)
202 return test_facility(65);
206 * ap_qci_available(): Test if AP configuration
207 * information can be queried via QCI subfunction.
209 * Returns 1 if subfunction PQAP(QCI) is available.
211 static int ap_qci_available(void)
213 return test_facility(12);
217 * ap_apft_available(): Test if AP facilities test (APFT)
218 * facility is available.
220 * Returns 1 if APFT is available.
222 static int ap_apft_available(void)
224 return test_facility(15);
228 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
230 * Returns 1 if the QACT subfunction is available.
232 static inline int ap_qact_available(void)
234 return ap_qci_info->qact;
238 * ap_sb_available(): Test if the AP secure binding facility is available.
240 * Returns 1 if secure binding facility is available.
242 int ap_sb_available(void)
244 return ap_qci_info->apsb;
248 * ap_is_se_guest(): Check for SE guest with AP pass-through support.
250 bool ap_is_se_guest(void)
252 return is_prot_virt_guest() && ap_sb_available();
254 EXPORT_SYMBOL(ap_is_se_guest);
257 * ap_init_qci_info(): Allocate and query qci config info.
258 * Does also update the static variables ap_max_domain_id
259 * and ap_max_adapter_id if this info is available.
261 static void __init ap_init_qci_info(void)
263 if (!ap_qci_available() ||
264 ap_qci(ap_qci_info)) {
265 AP_DBF_INFO("%s QCI not supported\n", __func__);
268 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
269 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
271 if (ap_qci_info->apxa) {
272 if (ap_qci_info->na) {
273 ap_max_adapter_id = ap_qci_info->na;
274 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
275 __func__, ap_max_adapter_id);
277 if (ap_qci_info->nd) {
278 ap_max_domain_id = ap_qci_info->nd;
279 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
280 __func__, ap_max_domain_id);
286 * ap_test_config(): helper function to extract the nrth bit
287 * within the unsigned int array field.
289 static inline int ap_test_config(unsigned int *field, unsigned int nr)
291 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
295 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
297 * Returns 0 if the card is not configured
298 * 1 if the card is configured or
299 * if the configuration information is not available
301 static inline int ap_test_config_card_id(unsigned int id)
303 if (id > ap_max_adapter_id)
305 if (ap_qci_info->flags)
306 return ap_test_config(ap_qci_info->apm, id);
311 * ap_test_config_usage_domain(): Test, whether an AP usage domain
314 * Returns 0 if the usage domain is not configured
315 * 1 if the usage domain is configured or
316 * if the configuration information is not available
318 int ap_test_config_usage_domain(unsigned int domain)
320 if (domain > ap_max_domain_id)
322 if (ap_qci_info->flags)
323 return ap_test_config(ap_qci_info->aqm, domain);
326 EXPORT_SYMBOL(ap_test_config_usage_domain);
329 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
331 * @domain AP control domain ID
333 * Returns 1 if the control domain is configured
334 * 0 in all other cases
336 int ap_test_config_ctrl_domain(unsigned int domain)
338 if (!ap_qci_info || domain > ap_max_domain_id)
340 return ap_test_config(ap_qci_info->adm, domain);
342 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
345 * ap_queue_info(): Check and get AP queue info.
346 * Returns: 1 if APQN exists and info is filled,
347 * 0 if APQN seems to exist but there is no info
348 * available (eg. caused by an asynch pending error)
349 * -1 invalid APQN, TAPQ error or AP queue status which
350 * indicates there is no APQN.
352 static int ap_queue_info(ap_qid_t qid, struct ap_tapq_hwinfo *hwinfo,
353 bool *decfg, bool *cstop)
355 struct ap_queue_status status;
359 /* make sure we don't run into a specifiation exception */
360 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
361 AP_QID_QUEUE(qid) > ap_max_domain_id)
364 /* call TAPQ on this APQN */
365 status = ap_test_queue(qid, ap_apft_available(), hwinfo);
367 switch (status.response_code) {
368 case AP_RESPONSE_NORMAL:
369 case AP_RESPONSE_RESET_IN_PROGRESS:
370 case AP_RESPONSE_DECONFIGURED:
371 case AP_RESPONSE_CHECKSTOPPED:
372 case AP_RESPONSE_BUSY:
373 /* For all these RCs the tapq info should be available */
376 /* On a pending async error the info should be available */
382 /* There should be at least one of the mode bits set */
383 if (WARN_ON_ONCE(!hwinfo->value))
386 *decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
387 *cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
392 void ap_wait(enum ap_sm_wait wait)
397 case AP_SM_WAIT_AGAIN:
398 case AP_SM_WAIT_INTERRUPT:
401 if (ap_poll_kthread) {
402 wake_up(&ap_poll_wait);
406 case AP_SM_WAIT_LOW_TIMEOUT:
407 case AP_SM_WAIT_HIGH_TIMEOUT:
408 spin_lock_bh(&ap_poll_timer_lock);
409 if (!hrtimer_is_queued(&ap_poll_timer)) {
411 wait == AP_SM_WAIT_LOW_TIMEOUT ?
412 poll_low_timeout : poll_high_timeout;
413 hrtimer_forward_now(&ap_poll_timer, hr_time);
414 hrtimer_restart(&ap_poll_timer);
416 spin_unlock_bh(&ap_poll_timer_lock);
418 case AP_SM_WAIT_NONE:
425 * ap_request_timeout(): Handling of request timeouts
426 * @t: timer making this callback
428 * Handles request timeouts.
430 void ap_request_timeout(struct timer_list *t)
432 struct ap_queue *aq = timer_container_of(aq, t, timeout);
434 spin_lock_bh(&aq->lock);
435 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
436 spin_unlock_bh(&aq->lock);
440 * ap_poll_timeout(): AP receive polling for finished AP requests.
441 * @unused: Unused pointer.
443 * Schedules the AP tasklet using a high resolution timer.
445 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
447 tasklet_schedule(&ap_tasklet);
448 return HRTIMER_NORESTART;
452 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
453 * @airq: pointer to adapter interrupt descriptor
456 static void ap_interrupt_handler(struct airq_struct *airq,
457 struct tpi_info *tpi_info)
459 inc_irq_stat(IRQIO_APB);
460 tasklet_schedule(&ap_tasklet);
464 * ap_tasklet_fn(): Tasklet to poll all AP devices.
465 * @dummy: Unused variable
467 * Poll all AP devices on the bus.
469 static void ap_tasklet_fn(unsigned long dummy)
473 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
475 /* Reset the indicator if interrupts are used. Thus new interrupts can
476 * be received. Doing it in the beginning of the tasklet is therefore
477 * important that no requests on any AP get lost.
480 WRITE_ONCE(*ap_airq.lsi_ptr, 0);
482 spin_lock_bh(&ap_queues_lock);
483 hash_for_each(ap_queues, bkt, aq, hnode) {
484 spin_lock_bh(&aq->lock);
485 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
486 spin_unlock_bh(&aq->lock);
488 spin_unlock_bh(&ap_queues_lock);
493 static int ap_pending_requests(void)
498 spin_lock_bh(&ap_queues_lock);
499 hash_for_each(ap_queues, bkt, aq, hnode) {
500 if (aq->queue_count == 0)
502 spin_unlock_bh(&ap_queues_lock);
505 spin_unlock_bh(&ap_queues_lock);
510 * ap_poll_thread(): Thread that polls for finished requests.
511 * @data: Unused pointer
513 * AP bus poll thread. The purpose of this thread is to poll for
514 * finished requests in a loop if there is a "free" cpu - that is
515 * a cpu that doesn't have anything better to do. The polling stops
516 * as soon as there is another task or if all messages have been
519 static int ap_poll_thread(void *data)
521 DECLARE_WAITQUEUE(wait, current);
523 set_user_nice(current, MAX_NICE);
525 while (!kthread_should_stop()) {
526 add_wait_queue(&ap_poll_wait, &wait);
527 set_current_state(TASK_INTERRUPTIBLE);
528 if (!ap_pending_requests()) {
532 set_current_state(TASK_RUNNING);
533 remove_wait_queue(&ap_poll_wait, &wait);
534 if (need_resched()) {
545 static int ap_poll_thread_start(void)
549 if (ap_irq_flag || ap_poll_kthread)
551 mutex_lock(&ap_poll_thread_mutex);
552 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
553 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
555 ap_poll_kthread = NULL;
556 mutex_unlock(&ap_poll_thread_mutex);
560 static void ap_poll_thread_stop(void)
562 if (!ap_poll_kthread)
564 mutex_lock(&ap_poll_thread_mutex);
565 kthread_stop(ap_poll_kthread);
566 ap_poll_kthread = NULL;
567 mutex_unlock(&ap_poll_thread_mutex);
570 #define is_card_dev(x) ((x)->parent == ap_root_device)
571 #define is_queue_dev(x) ((x)->parent != ap_root_device)
574 * ap_init_apmsg() - Initialize ap_message.
576 int ap_init_apmsg(struct ap_message *ap_msg, u32 flags)
578 unsigned int maxmsgsize;
580 memset(ap_msg, 0, sizeof(*ap_msg));
581 ap_msg->flags = flags;
583 if (flags & AP_MSG_FLAG_MEMPOOL) {
584 ap_msg->msg = mempool_alloc_preallocated(ap_msg_pool);
587 ap_msg->bufsize = AP_DEFAULT_MAX_MSG_SIZE;
591 maxmsgsize = atomic_read(&ap_max_msg_size);
592 ap_msg->msg = kmalloc(maxmsgsize, GFP_KERNEL);
595 ap_msg->bufsize = maxmsgsize;
599 EXPORT_SYMBOL(ap_init_apmsg);
602 * ap_release_apmsg() - Release ap_message.
604 void ap_release_apmsg(struct ap_message *ap_msg)
606 if (ap_msg->flags & AP_MSG_FLAG_MEMPOOL) {
607 memzero_explicit(ap_msg->msg, ap_msg->bufsize);
608 mempool_free(ap_msg->msg, ap_msg_pool);
610 kfree_sensitive(ap_msg->msg);
613 EXPORT_SYMBOL(ap_release_apmsg);
617 * @dev: Pointer to device
618 * @drv: Pointer to device_driver
620 * AP bus driver registration/unregistration.
622 static int ap_bus_match(struct device *dev, const struct device_driver *drv)
624 const struct ap_driver *ap_drv = to_ap_drv(drv);
625 struct ap_device_id *id;
628 * Compare device type of the device with the list of
629 * supported types of the device_driver.
631 for (id = ap_drv->ids; id->match_flags; id++) {
632 if (is_card_dev(dev) &&
633 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
634 id->dev_type == to_ap_dev(dev)->device_type)
636 if (is_queue_dev(dev) &&
637 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
638 id->dev_type == to_ap_dev(dev)->device_type)
645 * ap_uevent(): Uevent function for AP devices.
646 * @dev: Pointer to device
647 * @env: Pointer to kobj_uevent_env
649 * It sets up a single environment variable DEV_TYPE which contains the
650 * hardware device type.
652 static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env)
655 const struct ap_device *ap_dev = to_ap_dev(dev);
657 /* Uevents from ap bus core don't need extensions to the env */
658 if (dev == ap_root_device)
661 if (is_card_dev(dev)) {
662 struct ap_card *ac = to_ap_card(&ap_dev->device);
664 /* Set up DEV_TYPE environment variable. */
665 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
669 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
673 /* Add MODE=<accel|cca|ep11> */
674 if (ac->hwinfo.accel)
675 rc = add_uevent_var(env, "MODE=accel");
676 else if (ac->hwinfo.cca)
677 rc = add_uevent_var(env, "MODE=cca");
678 else if (ac->hwinfo.ep11)
679 rc = add_uevent_var(env, "MODE=ep11");
683 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
685 /* Add MODE=<accel|cca|ep11> */
686 if (aq->card->hwinfo.accel)
687 rc = add_uevent_var(env, "MODE=accel");
688 else if (aq->card->hwinfo.cca)
689 rc = add_uevent_var(env, "MODE=cca");
690 else if (aq->card->hwinfo.ep11)
691 rc = add_uevent_var(env, "MODE=ep11");
699 static void ap_send_init_scan_done_uevent(void)
701 char *envp[] = { "INITSCAN=done", NULL };
703 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
706 static void ap_send_bindings_complete_uevent(void)
709 char *envp[] = { "BINDINGS=complete", buf, NULL };
711 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
712 atomic64_inc_return(&ap_bindings_complete_count));
713 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
716 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
719 char *envp[] = { buf, NULL };
721 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
723 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
725 EXPORT_SYMBOL(ap_send_config_uevent);
727 void ap_send_online_uevent(struct ap_device *ap_dev, int online)
730 char *envp[] = { buf, NULL };
732 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
734 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
736 EXPORT_SYMBOL(ap_send_online_uevent);
738 static void ap_send_mask_changed_uevent(unsigned long *newapm,
739 unsigned long *newaqm)
742 char *envp[] = { buf, NULL };
745 snprintf(buf, sizeof(buf),
746 "APMASK=0x%016lx%016lx%016lx%016lx\n",
747 newapm[0], newapm[1], newapm[2], newapm[3]);
749 snprintf(buf, sizeof(buf),
750 "AQMASK=0x%016lx%016lx%016lx%016lx\n",
751 newaqm[0], newaqm[1], newaqm[2], newaqm[3]);
753 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
757 * calc # of bound APQNs
760 struct __ap_calc_ctrs {
765 static int __ap_calc_helper(struct device *dev, void *arg)
767 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg;
769 if (is_queue_dev(dev)) {
778 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
780 struct __ap_calc_ctrs ctrs;
782 memset(&ctrs, 0, sizeof(ctrs));
783 bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper);
790 * After ap bus scan do check if all existing APQNs are
791 * bound to device drivers.
793 static void ap_check_bindings_complete(void)
795 unsigned int apqns, bound;
797 if (atomic64_read(&ap_scan_bus_count) >= 1) {
798 ap_calc_bound_apqns(&apqns, &bound);
799 if (bound == apqns) {
800 if (!completion_done(&ap_apqn_bindings_complete)) {
801 complete_all(&ap_apqn_bindings_complete);
802 ap_send_bindings_complete_uevent();
803 pr_debug("all apqn bindings complete\n");
810 * Interface to wait for the AP bus to have done one initial ap bus
811 * scan and all detected APQNs have been bound to device drivers.
812 * If these both conditions are not fulfilled, this function blocks
813 * on a condition with wait_for_completion_interruptible_timeout().
814 * If these both conditions are fulfilled (before the timeout hits)
815 * the return value is 0. If the timeout (in jiffies) hits instead
816 * -ETIME is returned. On failures negative return values are
817 * returned to the caller.
819 int ap_wait_apqn_bindings_complete(unsigned long timeout)
824 if (completion_done(&ap_apqn_bindings_complete))
828 l = wait_for_completion_interruptible_timeout(
829 &ap_apqn_bindings_complete, timeout);
831 l = wait_for_completion_interruptible(
832 &ap_apqn_bindings_complete);
834 rc = l == -ERESTARTSYS ? -EINTR : l;
835 else if (l == 0 && timeout)
838 pr_debug("rc=%d\n", rc);
841 EXPORT_SYMBOL(ap_wait_apqn_bindings_complete);
843 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
845 if (is_queue_dev(dev) &&
846 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data)
847 device_unregister(dev);
851 static int __ap_revise_reserved(struct device *dev, void *dummy)
853 int rc, card, queue, devres, drvres;
855 if (is_queue_dev(dev)) {
856 card = AP_QID_CARD(to_ap_queue(dev)->qid);
857 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
858 mutex_lock(&ap_perms_mutex);
859 devres = test_bit_inv(card, ap_perms.apm) &&
860 test_bit_inv(queue, ap_perms.aqm);
861 mutex_unlock(&ap_perms_mutex);
862 drvres = to_ap_drv(dev->driver)->flags
863 & AP_DRIVER_FLAG_DEFAULT;
864 if (!!devres != !!drvres) {
865 pr_debug("reprobing queue=%02x.%04x\n", card, queue);
866 rc = device_reprobe(dev);
868 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
869 __func__, card, queue);
876 static void ap_bus_revise_bindings(void)
878 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
882 * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
883 * default host driver or not.
884 * @card: the APID of the adapter card to check
885 * @queue: the APQI of the queue to check
887 * Note: the ap_perms_mutex must be locked by the caller of this function.
889 * Return: an int specifying whether the AP adapter is reserved for the host (1)
892 int ap_owned_by_def_drv(int card, int queue)
896 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
899 if (test_bit_inv(card, ap_perms.apm) &&
900 test_bit_inv(queue, ap_perms.aqm))
905 EXPORT_SYMBOL(ap_owned_by_def_drv);
908 * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
909 * a set is reserved for the host drivers
911 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
912 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
914 * Note: the ap_perms_mutex must be locked by the caller of this function.
916 * Return: an int specifying whether each APQN is reserved for the host (1) or
919 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
922 int card, queue, rc = 0;
924 for (card = 0; !rc && card < AP_DEVICES; card++)
925 if (test_bit_inv(card, apm) &&
926 test_bit_inv(card, ap_perms.apm))
927 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
928 if (test_bit_inv(queue, aqm) &&
929 test_bit_inv(queue, ap_perms.aqm))
934 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
936 static int ap_device_probe(struct device *dev)
938 struct ap_device *ap_dev = to_ap_dev(dev);
939 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
940 int card, queue, devres, drvres, rc = -ENODEV;
942 if (!get_device(dev))
945 if (is_queue_dev(dev)) {
947 * If the apqn is marked as reserved/used by ap bus and
948 * default drivers, only probe with drivers with the default
949 * flag set. If it is not marked, only probe with drivers
950 * with the default flag not set.
952 card = AP_QID_CARD(to_ap_queue(dev)->qid);
953 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
954 mutex_lock(&ap_perms_mutex);
955 devres = test_bit_inv(card, ap_perms.apm) &&
956 test_bit_inv(queue, ap_perms.aqm);
957 mutex_unlock(&ap_perms_mutex);
958 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
959 if (!!devres != !!drvres)
964 * Rearm the bindings complete completion to trigger
965 * bindings complete when all devices are bound again
967 reinit_completion(&ap_apqn_bindings_complete);
969 /* Add queue/card to list of active queues/cards */
970 spin_lock_bh(&ap_queues_lock);
971 if (is_queue_dev(dev))
972 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
973 to_ap_queue(dev)->qid);
974 spin_unlock_bh(&ap_queues_lock);
976 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
979 spin_lock_bh(&ap_queues_lock);
980 if (is_queue_dev(dev))
981 hash_del(&to_ap_queue(dev)->hnode);
982 spin_unlock_bh(&ap_queues_lock);
991 static void ap_device_remove(struct device *dev)
993 struct ap_device *ap_dev = to_ap_dev(dev);
994 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
996 /* prepare ap queue device removal */
997 if (is_queue_dev(dev))
998 ap_queue_prepare_remove(to_ap_queue(dev));
1000 /* driver's chance to clean up gracefully */
1002 ap_drv->remove(ap_dev);
1004 /* now do the ap queue device remove */
1005 if (is_queue_dev(dev))
1006 ap_queue_remove(to_ap_queue(dev));
1008 /* Remove queue/card from list of active queues/cards */
1009 spin_lock_bh(&ap_queues_lock);
1010 if (is_queue_dev(dev))
1011 hash_del(&to_ap_queue(dev)->hnode);
1012 spin_unlock_bh(&ap_queues_lock);
1017 struct ap_queue *ap_get_qdev(ap_qid_t qid)
1020 struct ap_queue *aq;
1022 spin_lock_bh(&ap_queues_lock);
1023 hash_for_each(ap_queues, bkt, aq, hnode) {
1024 if (aq->qid == qid) {
1025 get_device(&aq->ap_dev.device);
1026 spin_unlock_bh(&ap_queues_lock);
1030 spin_unlock_bh(&ap_queues_lock);
1034 EXPORT_SYMBOL(ap_get_qdev);
1036 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1039 struct device_driver *drv = &ap_drv->driver;
1042 drv->bus = &ap_bus_type;
1045 rc = driver_register(drv);
1047 ap_check_bindings_complete();
1051 EXPORT_SYMBOL(ap_driver_register);
1053 void ap_driver_unregister(struct ap_driver *ap_drv)
1055 driver_unregister(&ap_drv->driver);
1057 EXPORT_SYMBOL(ap_driver_unregister);
1060 * Enforce a synchronous AP bus rescan.
1061 * Returns true if the bus scan finds a change in the AP configuration
1062 * and AP devices have been added or deleted when this function returns.
1064 bool ap_bus_force_rescan(void)
1066 unsigned long scan_counter = atomic64_read(&ap_scan_bus_count);
1069 pr_debug("> scan counter=%lu\n", scan_counter);
1071 /* Only trigger AP bus scans after the initial scan is done */
1072 if (scan_counter <= 0)
1076 * There is one unlikely but nevertheless valid scenario where the
1077 * thread holding the mutex may try to send some crypto load but
1078 * all cards are offline so a rescan is triggered which causes
1079 * a recursive call of ap_bus_force_rescan(). A simple return if
1080 * the mutex is already locked by this thread solves this.
1082 if (mutex_is_locked(&ap_scan_bus_mutex)) {
1083 if (ap_scan_bus_task == current)
1087 /* Try to acquire the AP scan bus mutex */
1088 if (mutex_trylock(&ap_scan_bus_mutex)) {
1089 /* mutex acquired, run the AP bus scan */
1090 ap_scan_bus_task = current;
1091 ap_scan_bus_result = ap_scan_bus();
1092 rc = ap_scan_bus_result;
1093 ap_scan_bus_task = NULL;
1094 mutex_unlock(&ap_scan_bus_mutex);
1099 * Mutex acquire failed. So there is currently another task
1100 * already running the AP bus scan. Then let's simple wait
1101 * for the lock which means the other task has finished and
1102 * stored the result in ap_scan_bus_result.
1104 if (mutex_lock_interruptible(&ap_scan_bus_mutex)) {
1105 /* some error occurred, ignore and go out */
1108 rc = ap_scan_bus_result;
1109 mutex_unlock(&ap_scan_bus_mutex);
1112 pr_debug("rc=%d\n", rc);
1115 EXPORT_SYMBOL(ap_bus_force_rescan);
1118 * A config change has happened, force an ap bus rescan.
1120 static int ap_bus_cfg_chg(struct notifier_block *nb,
1121 unsigned long action, void *data)
1123 if (action != CHSC_NOTIFY_AP_CFG)
1126 pr_debug("config change, forcing bus rescan\n");
1128 ap_bus_force_rescan();
1133 static struct notifier_block ap_bus_nb = {
1134 .notifier_call = ap_bus_cfg_chg,
1137 int ap_hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1141 /* bits needs to be a multiple of 8 */
1145 if (str[0] == '0' && str[1] == 'x')
1150 for (i = 0; isxdigit(*str) && i < bits; str++) {
1151 b = hex_to_bin(*str);
1152 for (n = 0; n < 4; n++)
1153 if (b & (0x08 >> n))
1154 set_bit_inv(i + n, bitmap);
1164 EXPORT_SYMBOL(ap_hex2bitmap);
1167 * modify_bitmap() - parse bitmask argument and modify an existing
1168 * bit mask accordingly. A concatenation (done with ',') of these
1169 * terms is recognized:
1170 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1171 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1172 * 0...bits-1; the leading + or - is required. Here are some examples:
1173 * +0-15,+32,-128,-0xFF
1174 * -0-255,+1-16,+0x128
1175 * +1,+2,+3,+4,-5,-7-10
1176 * Returns the new bitmap after all changes have been applied. Every
1177 * positive value in the string will set a bit and every negative value
1178 * in the string will clear a bit. As a bit may be touched more than once,
1179 * the last 'operation' wins:
1180 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1181 * cleared again. All other bits are unmodified.
1183 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1185 unsigned long a, i, z;
1188 /* bits needs to be a multiple of 8 */
1194 if (sign != '+' && sign != '-')
1196 a = z = simple_strtoul(str, &np, 0);
1197 if (str == np || a >= bits)
1201 z = simple_strtoul(++str, &np, 0);
1202 if (str == np || a > z || z >= bits)
1206 for (i = a; i <= z; i++)
1208 set_bit_inv(i, bitmap);
1210 clear_bit_inv(i, bitmap);
1211 while (*str == ',' || *str == '\n')
1218 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1219 unsigned long *newmap)
1224 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1225 if (*str == '+' || *str == '-') {
1226 memcpy(newmap, bitmap, size);
1227 rc = modify_bitmap(str, newmap, bits);
1229 memset(newmap, 0, size);
1230 rc = ap_hex2bitmap(str, newmap, bits);
1235 int ap_parse_mask_str(const char *str,
1236 unsigned long *bitmap, int bits,
1239 unsigned long *newmap, size;
1242 /* bits needs to be a multiple of 8 */
1246 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1247 newmap = kmalloc(size, GFP_KERNEL);
1250 if (mutex_lock_interruptible(lock)) {
1252 return -ERESTARTSYS;
1254 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
1256 memcpy(bitmap, newmap, size);
1261 EXPORT_SYMBOL(ap_parse_mask_str);
1264 * AP bus attributes.
1267 static ssize_t ap_domain_show(const struct bus_type *bus, char *buf)
1269 return sysfs_emit(buf, "%d\n", ap_domain_index);
1272 static ssize_t ap_domain_store(const struct bus_type *bus,
1273 const char *buf, size_t count)
1277 if (sscanf(buf, "%i\n", &domain) != 1 ||
1278 domain < 0 || domain > ap_max_domain_id ||
1279 !test_bit_inv(domain, ap_perms.aqm))
1282 spin_lock_bh(&ap_domain_lock);
1283 ap_domain_index = domain;
1284 spin_unlock_bh(&ap_domain_lock);
1286 AP_DBF_INFO("%s stored new default domain=%d\n",
1292 static BUS_ATTR_RW(ap_domain);
1294 static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf)
1296 if (!ap_qci_info->flags) /* QCI not supported */
1297 return sysfs_emit(buf, "not supported\n");
1299 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1300 ap_qci_info->adm[0], ap_qci_info->adm[1],
1301 ap_qci_info->adm[2], ap_qci_info->adm[3],
1302 ap_qci_info->adm[4], ap_qci_info->adm[5],
1303 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1306 static BUS_ATTR_RO(ap_control_domain_mask);
1308 static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf)
1310 if (!ap_qci_info->flags) /* QCI not supported */
1311 return sysfs_emit(buf, "not supported\n");
1313 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1314 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1315 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1316 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1317 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1320 static BUS_ATTR_RO(ap_usage_domain_mask);
1322 static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf)
1324 if (!ap_qci_info->flags) /* QCI not supported */
1325 return sysfs_emit(buf, "not supported\n");
1327 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1328 ap_qci_info->apm[0], ap_qci_info->apm[1],
1329 ap_qci_info->apm[2], ap_qci_info->apm[3],
1330 ap_qci_info->apm[4], ap_qci_info->apm[5],
1331 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1334 static BUS_ATTR_RO(ap_adapter_mask);
1336 static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf)
1338 return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0);
1341 static BUS_ATTR_RO(ap_interrupts);
1343 static ssize_t config_time_show(const struct bus_type *bus, char *buf)
1345 return sysfs_emit(buf, "%d\n", ap_scan_bus_time);
1348 static ssize_t config_time_store(const struct bus_type *bus,
1349 const char *buf, size_t count)
1353 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1355 ap_scan_bus_time = time;
1356 mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ);
1360 static BUS_ATTR_RW(config_time);
1362 static ssize_t poll_thread_show(const struct bus_type *bus, char *buf)
1364 return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0);
1367 static ssize_t poll_thread_store(const struct bus_type *bus,
1368 const char *buf, size_t count)
1373 rc = kstrtobool(buf, &value);
1378 rc = ap_poll_thread_start();
1382 ap_poll_thread_stop();
1387 static BUS_ATTR_RW(poll_thread);
1389 static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf)
1391 return sysfs_emit(buf, "%lu\n", poll_high_timeout);
1394 static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf,
1397 unsigned long value;
1401 rc = kstrtoul(buf, 0, &value);
1405 /* 120 seconds = maximum poll interval */
1406 if (value > 120000000000UL)
1408 poll_high_timeout = value;
1409 hr_time = poll_high_timeout;
1411 spin_lock_bh(&ap_poll_timer_lock);
1412 hrtimer_cancel(&ap_poll_timer);
1413 hrtimer_set_expires(&ap_poll_timer, hr_time);
1414 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1415 spin_unlock_bh(&ap_poll_timer_lock);
1420 static BUS_ATTR_RW(poll_timeout);
1422 static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf)
1424 return sysfs_emit(buf, "%d\n", ap_max_domain_id);
1427 static BUS_ATTR_RO(ap_max_domain_id);
1429 static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf)
1431 return sysfs_emit(buf, "%d\n", ap_max_adapter_id);
1434 static BUS_ATTR_RO(ap_max_adapter_id);
1436 static ssize_t apmask_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.apm[0], ap_perms.apm[1],
1444 ap_perms.apm[2], ap_perms.apm[3]);
1445 mutex_unlock(&ap_perms_mutex);
1450 static int __verify_card_reservations(struct device_driver *drv, void *data)
1453 struct ap_driver *ap_drv = to_ap_drv(drv);
1454 unsigned long *newapm = (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(newapm, ap_perms.aqm);
1469 /* release the driver's module */
1470 module_put(drv->owner);
1475 static int apmask_commit(unsigned long *newapm)
1478 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1481 * Check if any bits in the apmask have been set which will
1482 * result in queues being removed from non-default drivers
1484 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1485 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1486 __verify_card_reservations);
1491 memcpy(ap_perms.apm, newapm, APMASKSIZE);
1496 static ssize_t apmask_store(const struct bus_type *bus, const char *buf,
1499 int rc, changes = 0;
1500 DECLARE_BITMAP(newapm, AP_DEVICES);
1502 if (mutex_lock_interruptible(&ap_perms_mutex))
1503 return -ERESTARTSYS;
1505 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1509 changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
1511 rc = apmask_commit(newapm);
1514 mutex_unlock(&ap_perms_mutex);
1519 ap_bus_revise_bindings();
1520 ap_send_mask_changed_uevent(newapm, NULL);
1526 static BUS_ATTR_RW(apmask);
1528 static ssize_t aqmask_show(const struct bus_type *bus, char *buf)
1532 if (mutex_lock_interruptible(&ap_perms_mutex))
1533 return -ERESTARTSYS;
1534 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1535 ap_perms.aqm[0], ap_perms.aqm[1],
1536 ap_perms.aqm[2], ap_perms.aqm[3]);
1537 mutex_unlock(&ap_perms_mutex);
1542 static int __verify_queue_reservations(struct device_driver *drv, void *data)
1545 struct ap_driver *ap_drv = to_ap_drv(drv);
1546 unsigned long *newaqm = (unsigned long *)data;
1549 * increase the driver's module refcounter to be sure it is not
1550 * going away when we invoke the callback function.
1552 if (!try_module_get(drv->owner))
1555 if (ap_drv->in_use) {
1556 rc = ap_drv->in_use(ap_perms.apm, newaqm);
1561 /* release the driver's module */
1562 module_put(drv->owner);
1567 static int aqmask_commit(unsigned long *newaqm)
1570 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1573 * Check if any bits in the aqmask have been set which will
1574 * result in queues being removed from non-default drivers
1576 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1577 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1578 __verify_queue_reservations);
1583 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1588 static ssize_t aqmask_store(const struct bus_type *bus, const char *buf,
1591 int rc, changes = 0;
1592 DECLARE_BITMAP(newaqm, AP_DOMAINS);
1594 if (mutex_lock_interruptible(&ap_perms_mutex))
1595 return -ERESTARTSYS;
1597 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1601 changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
1603 rc = aqmask_commit(newaqm);
1606 mutex_unlock(&ap_perms_mutex);
1611 ap_bus_revise_bindings();
1612 ap_send_mask_changed_uevent(NULL, newaqm);
1618 static BUS_ATTR_RW(aqmask);
1620 static ssize_t scans_show(const struct bus_type *bus, char *buf)
1622 return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count));
1625 static ssize_t scans_store(const struct bus_type *bus, const char *buf,
1628 AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1630 ap_bus_force_rescan();
1635 static BUS_ATTR_RW(scans);
1637 static ssize_t bindings_show(const struct bus_type *bus, char *buf)
1640 unsigned int apqns, n;
1642 ap_calc_bound_apqns(&apqns, &n);
1643 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1644 rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns);
1646 rc = sysfs_emit(buf, "%u/%u\n", n, apqns);
1651 static BUS_ATTR_RO(bindings);
1653 static ssize_t features_show(const struct bus_type *bus, char *buf)
1657 if (!ap_qci_info->flags) /* QCI not supported */
1658 return sysfs_emit(buf, "-\n");
1660 if (ap_qci_info->apsc)
1661 n += sysfs_emit_at(buf, n, "APSC ");
1662 if (ap_qci_info->apxa)
1663 n += sysfs_emit_at(buf, n, "APXA ");
1664 if (ap_qci_info->qact)
1665 n += sysfs_emit_at(buf, n, "QACT ");
1666 if (ap_qci_info->rc8a)
1667 n += sysfs_emit_at(buf, n, "RC8A ");
1668 if (ap_qci_info->apsb)
1669 n += sysfs_emit_at(buf, n, "APSB ");
1671 sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n");
1676 static BUS_ATTR_RO(features);
1678 static struct attribute *ap_bus_attrs[] = {
1679 &bus_attr_ap_domain.attr,
1680 &bus_attr_ap_control_domain_mask.attr,
1681 &bus_attr_ap_usage_domain_mask.attr,
1682 &bus_attr_ap_adapter_mask.attr,
1683 &bus_attr_config_time.attr,
1684 &bus_attr_poll_thread.attr,
1685 &bus_attr_ap_interrupts.attr,
1686 &bus_attr_poll_timeout.attr,
1687 &bus_attr_ap_max_domain_id.attr,
1688 &bus_attr_ap_max_adapter_id.attr,
1689 &bus_attr_apmask.attr,
1690 &bus_attr_aqmask.attr,
1691 &bus_attr_scans.attr,
1692 &bus_attr_bindings.attr,
1693 &bus_attr_features.attr,
1696 ATTRIBUTE_GROUPS(ap_bus);
1698 static const struct bus_type ap_bus_type = {
1700 .bus_groups = ap_bus_groups,
1701 .match = &ap_bus_match,
1702 .uevent = &ap_uevent,
1703 .probe = ap_device_probe,
1704 .remove = ap_device_remove,
1708 * ap_select_domain(): Select an AP domain if possible and we haven't
1709 * already done so before.
1711 static void ap_select_domain(void)
1713 struct ap_queue_status status;
1717 * Choose the default domain. Either the one specified with
1718 * the "domain=" parameter or the first domain with at least
1721 spin_lock_bh(&ap_domain_lock);
1722 if (ap_domain_index >= 0) {
1723 /* Domain has already been selected. */
1726 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1727 if (!ap_test_config_usage_domain(dom) ||
1728 !test_bit_inv(dom, ap_perms.aqm))
1730 for (card = 0; card <= ap_max_adapter_id; card++) {
1731 if (!ap_test_config_card_id(card) ||
1732 !test_bit_inv(card, ap_perms.apm))
1734 status = ap_test_queue(AP_MKQID(card, dom),
1735 ap_apft_available(),
1737 if (status.response_code == AP_RESPONSE_NORMAL)
1740 if (card <= ap_max_adapter_id)
1743 if (dom <= ap_max_domain_id) {
1744 ap_domain_index = dom;
1745 AP_DBF_INFO("%s new default domain is %d\n",
1746 __func__, ap_domain_index);
1749 spin_unlock_bh(&ap_domain_lock);
1753 * This function checks the type and returns either 0 for not
1754 * supported or the highest compatible type value (which may
1755 * include the input type value).
1757 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1761 /* < CEX4 is not supported */
1762 if (rawtype < AP_DEVICE_TYPE_CEX4) {
1763 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1764 __func__, AP_QID_CARD(qid),
1765 AP_QID_QUEUE(qid), rawtype);
1768 /* up to CEX8 known and fully supported */
1769 if (rawtype <= AP_DEVICE_TYPE_CEX8)
1772 * unknown new type > CEX8, check for compatibility
1773 * to the highest known and supported type which is
1774 * currently CEX8 with the help of the QACT function.
1776 if (ap_qact_available()) {
1777 struct ap_queue_status status;
1778 union ap_qact_ap_info apinfo = {0};
1780 apinfo.mode = (func >> 26) & 0x07;
1781 apinfo.cat = AP_DEVICE_TYPE_CEX8;
1782 status = ap_qact(qid, 0, &apinfo);
1783 if (status.response_code == AP_RESPONSE_NORMAL &&
1784 apinfo.cat >= AP_DEVICE_TYPE_CEX4 &&
1785 apinfo.cat <= AP_DEVICE_TYPE_CEX8)
1786 comp_type = apinfo.cat;
1789 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1790 __func__, AP_QID_CARD(qid),
1791 AP_QID_QUEUE(qid), rawtype);
1792 else if (comp_type != rawtype)
1793 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1794 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1795 rawtype, comp_type);
1800 * Helper function to be used with bus_find_dev
1801 * matches for the card device with the given id
1803 static int __match_card_device_with_id(struct device *dev, const void *data)
1805 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
1809 * Helper function to be used with bus_find_dev
1810 * matches for the queue device with a given qid
1812 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1814 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
1818 * Helper function to be used with bus_find_dev
1819 * matches any queue device with given queue id
1821 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1823 return is_queue_dev(dev) &&
1824 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
1827 /* Helper function for notify_config_changed */
1828 static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1830 struct ap_driver *ap_drv = to_ap_drv(drv);
1832 if (try_module_get(drv->owner)) {
1833 if (ap_drv->on_config_changed)
1834 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1835 module_put(drv->owner);
1841 /* Notify all drivers about an qci config change */
1842 static inline void notify_config_changed(void)
1844 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1845 __drv_notify_config_changed);
1848 /* Helper function for notify_scan_complete */
1849 static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1851 struct ap_driver *ap_drv = to_ap_drv(drv);
1853 if (try_module_get(drv->owner)) {
1854 if (ap_drv->on_scan_complete)
1855 ap_drv->on_scan_complete(ap_qci_info,
1857 module_put(drv->owner);
1863 /* Notify all drivers about bus scan complete */
1864 static inline void notify_scan_complete(void)
1866 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1867 __drv_notify_scan_complete);
1871 * Helper function for ap_scan_bus().
1872 * Remove card device and associated queue devices.
1874 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1876 bus_for_each_dev(&ap_bus_type, NULL,
1877 (void *)(long)ac->id,
1878 __ap_queue_devices_with_id_unregister);
1879 device_unregister(&ac->ap_dev.device);
1883 * Helper function for ap_scan_bus().
1884 * Does the scan bus job for all the domains within
1885 * a valid adapter given by an ap_card ptr.
1887 static inline void ap_scan_domains(struct ap_card *ac)
1889 struct ap_tapq_hwinfo hwinfo;
1890 bool decfg, chkstop;
1891 struct ap_queue *aq;
1897 * Go through the configuration for the domains and compare them
1898 * to the existing queue devices. Also take care of the config
1899 * and error state for the queue devices.
1902 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1903 qid = AP_MKQID(ac->id, dom);
1904 dev = bus_find_device(&ap_bus_type, NULL,
1906 __match_queue_device_with_qid);
1907 aq = dev ? to_ap_queue(dev) : NULL;
1908 if (!ap_test_config_usage_domain(dom)) {
1910 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
1911 __func__, ac->id, dom);
1912 device_unregister(dev);
1914 goto put_dev_and_continue;
1916 /* domain is valid, get info from this APQN */
1917 rc = ap_queue_info(qid, &hwinfo, &decfg, &chkstop);
1921 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1922 __func__, ac->id, dom);
1923 device_unregister(dev);
1927 goto put_dev_and_continue;
1931 /* if no queue device exists, create a new one */
1933 aq = ap_queue_create(qid, ac);
1935 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1936 __func__, ac->id, dom);
1939 aq->config = !decfg;
1940 aq->chkstop = chkstop;
1941 aq->se_bstate = hwinfo.bs;
1942 dev = &aq->ap_dev.device;
1943 dev->bus = &ap_bus_type;
1944 dev->parent = &ac->ap_dev.device;
1945 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1946 /* register queue device */
1947 rc = device_register(dev);
1949 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1950 __func__, ac->id, dom);
1951 goto put_dev_and_continue;
1953 /* get it and thus adjust reference counter */
1956 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
1957 __func__, ac->id, dom);
1958 } else if (chkstop) {
1959 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1960 __func__, ac->id, dom);
1962 /* nudge the queue's state machine */
1963 ap_queue_init_state(aq);
1964 AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
1965 __func__, ac->id, dom);
1967 goto put_dev_and_continue;
1969 /* handle state changes on already existing queue device */
1970 spin_lock_bh(&aq->lock);
1972 aq->se_bstate = hwinfo.bs;
1973 /* checkstop state */
1974 if (chkstop && !aq->chkstop) {
1977 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1978 aq->dev_state = AP_DEV_STATE_ERROR;
1979 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1981 spin_unlock_bh(&aq->lock);
1982 pr_debug("(%d,%d) queue dev checkstop on\n",
1984 /* 'receive' pending messages with -EAGAIN */
1986 goto put_dev_and_continue;
1987 } else if (!chkstop && aq->chkstop) {
1989 aq->chkstop = false;
1990 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
1991 _ap_queue_init_state(aq);
1992 spin_unlock_bh(&aq->lock);
1993 pr_debug("(%d,%d) queue dev checkstop off\n",
1995 goto put_dev_and_continue;
1997 /* config state change */
1998 if (decfg && aq->config) {
1999 /* config off this queue device */
2001 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
2002 aq->dev_state = AP_DEV_STATE_ERROR;
2003 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
2005 spin_unlock_bh(&aq->lock);
2006 pr_debug("(%d,%d) queue dev config off\n",
2008 ap_send_config_uevent(&aq->ap_dev, aq->config);
2009 /* 'receive' pending messages with -EAGAIN */
2011 goto put_dev_and_continue;
2012 } else if (!decfg && !aq->config) {
2013 /* config on this queue device */
2015 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
2016 _ap_queue_init_state(aq);
2017 spin_unlock_bh(&aq->lock);
2018 pr_debug("(%d,%d) queue dev config on\n",
2020 ap_send_config_uevent(&aq->ap_dev, aq->config);
2021 goto put_dev_and_continue;
2023 /* handle other error states */
2024 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
2025 spin_unlock_bh(&aq->lock);
2026 /* 'receive' pending messages with -EAGAIN */
2028 /* re-init (with reset) the queue device */
2029 ap_queue_init_state(aq);
2030 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
2031 __func__, ac->id, dom);
2032 goto put_dev_and_continue;
2034 spin_unlock_bh(&aq->lock);
2035 put_dev_and_continue:
2041 * Helper function for ap_scan_bus().
2042 * Does the scan bus job for the given adapter id.
2044 static inline void ap_scan_adapter(int ap)
2046 struct ap_tapq_hwinfo hwinfo;
2047 int rc, dom, comp_type;
2048 bool decfg, chkstop;
2053 /* Is there currently a card device for this adapter ? */
2054 dev = bus_find_device(&ap_bus_type, NULL,
2056 __match_card_device_with_id);
2057 ac = dev ? to_ap_card(dev) : NULL;
2059 /* Adapter not in configuration ? */
2060 if (!ap_test_config_card_id(ap)) {
2062 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
2064 ap_scan_rm_card_dev_and_queue_devs(ac);
2071 * Adapter ap is valid in the current configuration. So do some checks:
2072 * If no card device exists, build one. If a card device exists, check
2073 * for type and functions changed. For all this we need to find a valid
2077 for (dom = 0; dom <= ap_max_domain_id; dom++)
2078 if (ap_test_config_usage_domain(dom)) {
2079 qid = AP_MKQID(ap, dom);
2080 if (ap_queue_info(qid, &hwinfo, &decfg, &chkstop) > 0)
2083 if (dom > ap_max_domain_id) {
2084 /* Could not find one valid APQN for this adapter */
2086 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
2088 ap_scan_rm_card_dev_and_queue_devs(ac);
2091 pr_debug("(%d) no type info (no APQN found), ignored\n",
2097 /* No apdater type info available, an unusable adapter */
2099 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
2101 ap_scan_rm_card_dev_and_queue_devs(ac);
2104 pr_debug("(%d) no valid type (0) info, ignored\n", ap);
2108 hwinfo.value &= TAPQ_CARD_HWINFO_MASK; /* filter card specific hwinfo */
2110 /* Check APQN against existing card device for changes */
2111 if (ac->hwinfo.at != hwinfo.at) {
2112 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
2113 __func__, ap, hwinfo.at);
2114 ap_scan_rm_card_dev_and_queue_devs(ac);
2117 } else if (ac->hwinfo.fac != hwinfo.fac) {
2118 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
2119 __func__, ap, hwinfo.fac);
2120 ap_scan_rm_card_dev_and_queue_devs(ac);
2124 /* handle checkstop state change */
2125 if (chkstop && !ac->chkstop) {
2128 AP_DBF_INFO("%s(%d) card dev checkstop on\n",
2130 } else if (!chkstop && ac->chkstop) {
2132 ac->chkstop = false;
2133 AP_DBF_INFO("%s(%d) card dev checkstop off\n",
2136 /* handle config state change */
2137 if (decfg && ac->config) {
2139 AP_DBF_INFO("%s(%d) card dev config off\n",
2141 ap_send_config_uevent(&ac->ap_dev, ac->config);
2142 } else if (!decfg && !ac->config) {
2144 AP_DBF_INFO("%s(%d) card dev config on\n",
2146 ap_send_config_uevent(&ac->ap_dev, ac->config);
2152 /* Build a new card device */
2153 comp_type = ap_get_compatible_type(qid, hwinfo.at, hwinfo.fac);
2155 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
2156 __func__, ap, hwinfo.at);
2159 ac = ap_card_create(ap, hwinfo, comp_type);
2161 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
2165 ac->config = !decfg;
2166 ac->chkstop = chkstop;
2167 dev = &ac->ap_dev.device;
2168 dev->bus = &ap_bus_type;
2169 dev->parent = ap_root_device;
2170 dev_set_name(dev, "card%02x", ap);
2171 /* maybe enlarge ap_max_msg_size to support this card */
2172 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
2173 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
2174 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
2176 atomic_read(&ap_max_msg_size));
2178 /* Register the new card device with AP bus */
2179 rc = device_register(dev);
2181 AP_DBF_WARN("%s(%d) device_register() failed\n",
2186 /* get it and thus adjust reference counter */
2189 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
2190 __func__, ap, hwinfo.at, hwinfo.fac);
2192 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
2193 __func__, ap, hwinfo.at, hwinfo.fac);
2195 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
2196 __func__, ap, hwinfo.at, hwinfo.fac);
2199 /* Verify the domains and the queue devices for this card */
2200 ap_scan_domains(ac);
2202 /* release the card device */
2203 put_device(&ac->ap_dev.device);
2207 * ap_get_configuration - get the host AP configuration
2209 * Stores the host AP configuration information returned from the previous call
2210 * to Query Configuration Information (QCI), then retrieves and stores the
2211 * current AP configuration returned from QCI.
2213 * Return: true if the host AP configuration changed between calls to QCI;
2214 * otherwise, return false.
2216 static bool ap_get_configuration(void)
2218 if (!ap_qci_info->flags) /* QCI not supported */
2221 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2222 ap_qci(ap_qci_info);
2224 return memcmp(ap_qci_info, ap_qci_info_old,
2225 sizeof(struct ap_config_info)) != 0;
2229 * ap_config_has_new_aps - Check current against old qci info if
2230 * new adapters have appeared. Returns true if at least one new
2231 * adapter in the apm mask is showing up. Existing adapters or
2232 * receding adapters are not counted.
2234 static bool ap_config_has_new_aps(void)
2237 unsigned long m[BITS_TO_LONGS(AP_DEVICES)];
2239 if (!ap_qci_info->flags)
2242 bitmap_andnot(m, (unsigned long *)ap_qci_info->apm,
2243 (unsigned long *)ap_qci_info_old->apm, AP_DEVICES);
2244 if (!bitmap_empty(m, AP_DEVICES))
2251 * ap_config_has_new_doms - Check current against old qci info if
2252 * new (usage) domains have appeared. Returns true if at least one
2253 * new domain in the aqm mask is showing up. Existing domains or
2254 * receding domains are not counted.
2256 static bool ap_config_has_new_doms(void)
2258 unsigned long m[BITS_TO_LONGS(AP_DOMAINS)];
2260 if (!ap_qci_info->flags)
2263 bitmap_andnot(m, (unsigned long *)ap_qci_info->aqm,
2264 (unsigned long *)ap_qci_info_old->aqm, AP_DOMAINS);
2265 if (!bitmap_empty(m, AP_DOMAINS))
2272 * ap_scan_bus(): Scan the AP bus for new devices
2273 * Always run under mutex ap_scan_bus_mutex protection
2274 * which needs to get locked/unlocked by the caller!
2275 * Returns true if any config change has been detected
2276 * during the scan, otherwise false.
2278 static bool ap_scan_bus(void)
2280 bool config_changed;
2285 /* (re-)fetch configuration via QCI */
2286 config_changed = ap_get_configuration();
2287 if (config_changed) {
2288 if (ap_config_has_new_aps() || ap_config_has_new_doms()) {
2290 * Appearance of new adapters and/or domains need to
2291 * build new ap devices which need to get bound to an
2292 * device driver. Thus reset the APQN bindings complete
2295 reinit_completion(&ap_apqn_bindings_complete);
2297 /* post a config change notify */
2298 notify_config_changed();
2302 /* loop over all possible adapters */
2303 for (ap = 0; ap <= ap_max_adapter_id; ap++)
2304 ap_scan_adapter(ap);
2306 /* scan complete notify */
2308 notify_scan_complete();
2310 /* check if there is at least one queue available with default domain */
2311 if (ap_domain_index >= 0) {
2312 struct device *dev =
2313 bus_find_device(&ap_bus_type, NULL,
2314 (void *)(long)ap_domain_index,
2315 __match_queue_device_with_queue_id);
2319 AP_DBF_INFO("%s no queue device with default domain %d available\n",
2320 __func__, ap_domain_index);
2323 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
2324 pr_debug("init scan complete\n");
2325 ap_send_init_scan_done_uevent();
2328 ap_check_bindings_complete();
2330 mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ);
2332 pr_debug("< config_changed=%d\n", config_changed);
2334 return config_changed;
2338 * Callback for the ap_scan_bus_timer
2339 * Runs periodically, workqueue timer (ap_scan_bus_time)
2341 static void ap_scan_bus_timer_callback(struct timer_list *unused)
2344 * schedule work into the system long wq which when
2345 * the work is finally executed, calls the AP bus scan.
2347 queue_work(system_long_wq, &ap_scan_bus_work);
2351 * Callback for the ap_scan_bus_work
2353 static void ap_scan_bus_wq_callback(struct work_struct *unused)
2356 * Try to invoke an ap_scan_bus(). If the mutex acquisition
2357 * fails there is currently another task already running the
2358 * AP scan bus and there is no need to wait and re-trigger the
2359 * scan again. Please note at the end of the scan bus function
2360 * the AP scan bus timer is re-armed which triggers then the
2361 * ap_scan_bus_timer_callback which enqueues a work into the
2362 * system_long_wq which invokes this function here again.
2364 if (mutex_trylock(&ap_scan_bus_mutex)) {
2365 ap_scan_bus_task = current;
2366 ap_scan_bus_result = ap_scan_bus();
2367 ap_scan_bus_task = NULL;
2368 mutex_unlock(&ap_scan_bus_mutex);
2372 static inline void __exit ap_async_exit(void)
2375 ap_poll_thread_stop();
2376 chsc_notifier_unregister(&ap_bus_nb);
2377 cancel_work(&ap_scan_bus_work);
2378 hrtimer_cancel(&ap_poll_timer);
2379 timer_delete(&ap_scan_bus_timer);
2382 static inline int __init ap_async_init(void)
2386 /* Setup the AP bus rescan timer. */
2387 timer_setup(&ap_scan_bus_timer, ap_scan_bus_timer_callback, 0);
2390 * Setup the high resolution poll timer.
2391 * If we are running under z/VM adjust polling to z/VM polling rate.
2393 if (machine_is_vm())
2394 poll_high_timeout = 1500000;
2395 hrtimer_setup(&ap_poll_timer, ap_poll_timeout, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2397 queue_work(system_long_wq, &ap_scan_bus_work);
2399 rc = chsc_notifier_register(&ap_bus_nb);
2403 /* Start the low priority AP bus poll thread. */
2404 if (!ap_thread_flag)
2407 rc = ap_poll_thread_start();
2414 chsc_notifier_unregister(&ap_bus_nb);
2416 cancel_work(&ap_scan_bus_work);
2417 hrtimer_cancel(&ap_poll_timer);
2418 timer_delete(&ap_scan_bus_timer);
2422 static inline void ap_irq_exit(void)
2425 unregister_adapter_interrupt(&ap_airq);
2428 static inline int __init ap_irq_init(void)
2432 if (!ap_interrupts_available() || !ap_useirq)
2435 rc = register_adapter_interrupt(&ap_airq);
2436 ap_irq_flag = (rc == 0);
2441 static inline void ap_debug_exit(void)
2443 debug_unregister(ap_dbf_info);
2446 static inline int __init ap_debug_init(void)
2448 ap_dbf_info = debug_register("ap", 2, 1,
2449 AP_DBF_MAX_SPRINTF_ARGS * sizeof(long));
2450 debug_register_view(ap_dbf_info, &debug_sprintf_view);
2451 debug_set_level(ap_dbf_info, DBF_ERR);
2456 static void __init ap_perms_init(void)
2458 /* all resources usable if no kernel parameter string given */
2459 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
2460 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2461 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2463 /* apm kernel parameter string */
2465 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
2466 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2470 /* aqm kernel parameter string */
2472 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
2473 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2479 * ap_module_init(): The module initialization code.
2481 * Initializes the module.
2483 static int __init ap_module_init(void)
2487 rc = ap_debug_init();
2491 if (!ap_instructions_available()) {
2492 pr_warn("The hardware system does not support AP instructions\n");
2496 /* init ap_queue hashtable */
2497 hash_init(ap_queues);
2499 /* create ap msg buffer memory pool */
2500 ap_msg_pool = mempool_create_kmalloc_pool(ap_msg_pool_min_items,
2501 AP_DEFAULT_MAX_MSG_SIZE);
2507 /* set up the AP permissions (ioctls, ap and aq masks) */
2510 /* Get AP configuration data if available */
2513 /* check default domain setting */
2514 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
2515 (ap_domain_index >= 0 &&
2516 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
2517 pr_warn("%d is not a valid cryptographic domain\n",
2519 ap_domain_index = -1;
2522 /* Create /sys/bus/ap. */
2523 rc = bus_register(&ap_bus_type);
2527 /* Create /sys/devices/ap. */
2528 ap_root_device = root_device_register("ap");
2529 rc = PTR_ERR_OR_ZERO(ap_root_device);
2532 ap_root_device->bus = &ap_bus_type;
2534 /* enable interrupts if available */
2539 /* Setup asynchronous work (timers, workqueue, etc). */
2540 rc = ap_async_init();
2549 root_device_unregister(ap_root_device);
2551 bus_unregister(&ap_bus_type);
2553 mempool_destroy(ap_msg_pool);
2558 static void __exit ap_module_exit(void)
2562 root_device_unregister(ap_root_device);
2563 bus_unregister(&ap_bus_type);
2564 mempool_destroy(ap_msg_pool);
2568 module_init(ap_module_init);
2569 module_exit(ap_module_exit);