1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2006, 2012
10 * Adjunct processor bus.
13 #define KMSG_COMPONENT "ap"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/kernel_stat.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/err.h>
21 #include <linux/freezer.h>
22 #include <linux/interrupt.h>
23 #include <linux/workqueue.h>
24 #include <linux/slab.h>
25 #include <linux/notifier.h>
26 #include <linux/kthread.h>
27 #include <linux/mutex.h>
29 #include <linux/atomic.h>
31 #include <linux/hrtimer.h>
32 #include <linux/ktime.h>
33 #include <asm/facility.h>
34 #include <linux/crypto.h>
35 #include <linux/mod_devicetable.h>
36 #include <linux/debugfs.h>
37 #include <linux/ctype.h>
43 * Module parameters; note though this file itself isn't modular.
45 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
46 static DEFINE_SPINLOCK(ap_domain_lock);
47 module_param_named(domain, ap_domain_index, int, 0440);
48 MODULE_PARM_DESC(domain, "domain index for ap devices");
49 EXPORT_SYMBOL(ap_domain_index);
51 static int ap_thread_flag;
52 module_param_named(poll_thread, ap_thread_flag, int, 0440);
53 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
56 module_param_named(apmask, apm_str, charp, 0440);
57 MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
60 module_param_named(aqmask, aqm_str, charp, 0440);
61 MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
63 static struct device *ap_root_device;
65 /* Hashtable of all queue devices on the AP bus */
66 DEFINE_HASHTABLE(ap_queues, 8);
67 /* lock used for the ap_queues hashtable */
68 DEFINE_SPINLOCK(ap_queues_lock);
70 /* Default permissions (ioctl, card and domain masking) */
71 struct ap_perms ap_perms;
72 EXPORT_SYMBOL(ap_perms);
73 DEFINE_MUTEX(ap_perms_mutex);
74 EXPORT_SYMBOL(ap_perms_mutex);
76 static struct ap_config_info *ap_qci_info;
79 * AP bus related debug feature things.
81 debug_info_t *ap_dbf_info;
84 * Workqueue timer for bus rescan.
86 static struct timer_list ap_config_timer;
87 static int ap_config_time = AP_CONFIG_TIME;
88 static void ap_scan_bus(struct work_struct *);
89 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
92 * Tasklet & timer for AP request polling and interrupts
94 static void ap_tasklet_fn(unsigned long);
95 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
96 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
97 static struct task_struct *ap_poll_kthread;
98 static DEFINE_MUTEX(ap_poll_thread_mutex);
99 static DEFINE_SPINLOCK(ap_poll_timer_lock);
100 static struct hrtimer ap_poll_timer;
102 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
103 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
105 static unsigned long long poll_timeout = 250000;
107 /* Maximum domain id, if not given via qci */
108 static int ap_max_domain_id = 15;
109 /* Maximum adapter id, if not given via qci */
110 static int ap_max_adapter_id = 63;
112 static struct bus_type ap_bus_type;
114 /* Adapter interrupt definitions */
115 static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
117 static int ap_airq_flag;
119 static struct airq_struct ap_airq = {
120 .handler = ap_interrupt_handler,
125 * ap_using_interrupts() - Returns non-zero if interrupt support is
128 static inline int ap_using_interrupts(void)
134 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
136 * Returns the address of the local-summary-indicator of the adapter
137 * interrupt handler for AP, or NULL if adapter interrupts are not
140 void *ap_airq_ptr(void)
142 if (ap_using_interrupts())
143 return ap_airq.lsi_ptr;
148 * ap_interrupts_available(): Test if AP interrupts are available.
150 * Returns 1 if AP interrupts are available.
152 static int ap_interrupts_available(void)
154 return test_facility(65);
158 * ap_qci_available(): Test if AP configuration
159 * information can be queried via QCI subfunction.
161 * Returns 1 if subfunction PQAP(QCI) is available.
163 static int ap_qci_available(void)
165 return test_facility(12);
169 * ap_apft_available(): Test if AP facilities test (APFT)
170 * facility is available.
172 * Returns 1 if APFT is is available.
174 static int ap_apft_available(void)
176 return test_facility(15);
180 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
182 * Returns 1 if the QACT subfunction is available.
184 static inline int ap_qact_available(void)
187 return ap_qci_info->qact;
192 * ap_fetch_qci_info(): Fetch cryptographic config info
194 * Returns the ap configuration info fetched via PQAP(QCI).
195 * On success 0 is returned, on failure a negative errno
196 * is returned, e.g. if the PQAP(QCI) instruction is not
197 * available, the return value will be -EOPNOTSUPP.
199 static inline int ap_fetch_qci_info(struct ap_config_info *info)
201 if (!ap_qci_available())
209 * ap_init_qci_info(): Allocate and query qci config info.
210 * Does also update the static variables ap_max_domain_id
211 * and ap_max_adapter_id if this info is available.
214 static void __init ap_init_qci_info(void)
216 if (!ap_qci_available()) {
217 AP_DBF(DBF_INFO, "%s QCI not supported\n", __func__);
221 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
224 if (ap_fetch_qci_info(ap_qci_info) != 0) {
229 AP_DBF(DBF_INFO, "%s successful fetched initial qci info\n", __func__);
231 if (ap_qci_info->apxa) {
232 if (ap_qci_info->Na) {
233 ap_max_adapter_id = ap_qci_info->Na;
234 AP_DBF(DBF_INFO, "%s new ap_max_adapter_id is %d\n",
235 __func__, ap_max_adapter_id);
237 if (ap_qci_info->Nd) {
238 ap_max_domain_id = ap_qci_info->Nd;
239 AP_DBF(DBF_INFO, "%s new ap_max_domain_id is %d\n",
240 __func__, ap_max_domain_id);
246 * ap_test_config(): helper function to extract the nrth bit
247 * within the unsigned int array field.
249 static inline int ap_test_config(unsigned int *field, unsigned int nr)
251 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
255 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
257 * Returns 0 if the card is not configured
258 * 1 if the card is configured or
259 * if the configuration information is not available
261 static inline int ap_test_config_card_id(unsigned int id)
263 if (id > ap_max_adapter_id)
266 return ap_test_config(ap_qci_info->apm, id);
271 * ap_test_config_usage_domain(): Test, whether an AP usage domain
274 * Returns 0 if the usage domain is not configured
275 * 1 if the usage domain is configured or
276 * if the configuration information is not available
278 int ap_test_config_usage_domain(unsigned int domain)
280 if (domain > ap_max_domain_id)
283 return ap_test_config(ap_qci_info->aqm, domain);
286 EXPORT_SYMBOL(ap_test_config_usage_domain);
289 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
291 * @domain AP control domain ID
293 * Returns 1 if the control domain is configured
294 * 0 in all other cases
296 int ap_test_config_ctrl_domain(unsigned int domain)
298 if (!ap_qci_info || domain > ap_max_domain_id)
300 return ap_test_config(ap_qci_info->adm, domain);
302 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
305 * ap_queue_info(): Check and get AP queue info.
306 * Returns true if TAPQ succeeded and the info is filled or
309 static bool ap_queue_info(ap_qid_t qid, int *q_type,
310 unsigned int *q_fac, int *q_depth)
312 struct ap_queue_status status;
313 unsigned long info = 0;
315 /* make sure we don't run into a specifiation exception */
316 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
317 AP_QID_QUEUE(qid) > ap_max_domain_id)
320 /* call TAPQ on this APQN */
321 status = ap_test_queue(qid, ap_apft_available(), &info);
322 switch (status.response_code) {
323 case AP_RESPONSE_NORMAL:
324 case AP_RESPONSE_RESET_IN_PROGRESS:
326 * According to the architecture in all these cases the
327 * info should be filled. All bits 0 is not possible as
328 * there is at least one of the mode bits set.
330 if (WARN_ON_ONCE(!info))
332 *q_type = (int)((info >> 24) & 0xff);
333 *q_fac = (unsigned int)(info >> 32);
334 *q_depth = (int)(info & 0xff);
336 /* For CEX2 and CEX3 the available functions
337 * are not reflected by the facilities bits.
338 * Instead it is coded into the type. So here
339 * modify the function bits based on the type.
341 case AP_DEVICE_TYPE_CEX2A:
342 case AP_DEVICE_TYPE_CEX3A:
343 *q_fac |= 0x08000000;
345 case AP_DEVICE_TYPE_CEX2C:
346 case AP_DEVICE_TYPE_CEX3C:
347 *q_fac |= 0x10000000;
355 * A response code which indicates, there is no info available.
361 void ap_wait(enum ap_sm_wait wait)
366 case AP_SM_WAIT_AGAIN:
367 case AP_SM_WAIT_INTERRUPT:
368 if (ap_using_interrupts())
370 if (ap_poll_kthread) {
371 wake_up(&ap_poll_wait);
375 case AP_SM_WAIT_TIMEOUT:
376 spin_lock_bh(&ap_poll_timer_lock);
377 if (!hrtimer_is_queued(&ap_poll_timer)) {
378 hr_time = poll_timeout;
379 hrtimer_forward_now(&ap_poll_timer, hr_time);
380 hrtimer_restart(&ap_poll_timer);
382 spin_unlock_bh(&ap_poll_timer_lock);
384 case AP_SM_WAIT_NONE:
391 * ap_request_timeout(): Handling of request timeouts
392 * @t: timer making this callback
394 * Handles request timeouts.
396 void ap_request_timeout(struct timer_list *t)
398 struct ap_queue *aq = from_timer(aq, t, timeout);
400 spin_lock_bh(&aq->lock);
401 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
402 spin_unlock_bh(&aq->lock);
406 * ap_poll_timeout(): AP receive polling for finished AP requests.
407 * @unused: Unused pointer.
409 * Schedules the AP tasklet using a high resolution timer.
411 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
413 tasklet_schedule(&ap_tasklet);
414 return HRTIMER_NORESTART;
418 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
419 * @airq: pointer to adapter interrupt descriptor
421 static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
423 inc_irq_stat(IRQIO_APB);
424 tasklet_schedule(&ap_tasklet);
428 * ap_tasklet_fn(): Tasklet to poll all AP devices.
429 * @dummy: Unused variable
431 * Poll all AP devices on the bus.
433 static void ap_tasklet_fn(unsigned long dummy)
437 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
439 /* Reset the indicator if interrupts are used. Thus new interrupts can
440 * be received. Doing it in the beginning of the tasklet is therefor
441 * important that no requests on any AP get lost.
443 if (ap_using_interrupts())
444 xchg(ap_airq.lsi_ptr, 0);
446 spin_lock_bh(&ap_queues_lock);
447 hash_for_each(ap_queues, bkt, aq, hnode) {
448 spin_lock_bh(&aq->lock);
449 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
450 spin_unlock_bh(&aq->lock);
452 spin_unlock_bh(&ap_queues_lock);
457 static int ap_pending_requests(void)
462 spin_lock_bh(&ap_queues_lock);
463 hash_for_each(ap_queues, bkt, aq, hnode) {
464 if (aq->queue_count == 0)
466 spin_unlock_bh(&ap_queues_lock);
469 spin_unlock_bh(&ap_queues_lock);
474 * ap_poll_thread(): Thread that polls for finished requests.
475 * @data: Unused pointer
477 * AP bus poll thread. The purpose of this thread is to poll for
478 * finished requests in a loop if there is a "free" cpu - that is
479 * a cpu that doesn't have anything better to do. The polling stops
480 * as soon as there is another task or if all messages have been
483 static int ap_poll_thread(void *data)
485 DECLARE_WAITQUEUE(wait, current);
487 set_user_nice(current, MAX_NICE);
489 while (!kthread_should_stop()) {
490 add_wait_queue(&ap_poll_wait, &wait);
491 set_current_state(TASK_INTERRUPTIBLE);
492 if (!ap_pending_requests()) {
496 set_current_state(TASK_RUNNING);
497 remove_wait_queue(&ap_poll_wait, &wait);
498 if (need_resched()) {
509 static int ap_poll_thread_start(void)
513 if (ap_using_interrupts() || ap_poll_kthread)
515 mutex_lock(&ap_poll_thread_mutex);
516 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
517 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
519 ap_poll_kthread = NULL;
520 mutex_unlock(&ap_poll_thread_mutex);
524 static void ap_poll_thread_stop(void)
526 if (!ap_poll_kthread)
528 mutex_lock(&ap_poll_thread_mutex);
529 kthread_stop(ap_poll_kthread);
530 ap_poll_kthread = NULL;
531 mutex_unlock(&ap_poll_thread_mutex);
534 #define is_card_dev(x) ((x)->parent == ap_root_device)
535 #define is_queue_dev(x) ((x)->parent != ap_root_device)
539 * @dev: Pointer to device
540 * @drv: Pointer to device_driver
542 * AP bus driver registration/unregistration.
544 static int ap_bus_match(struct device *dev, struct device_driver *drv)
546 struct ap_driver *ap_drv = to_ap_drv(drv);
547 struct ap_device_id *id;
550 * Compare device type of the device with the list of
551 * supported types of the device_driver.
553 for (id = ap_drv->ids; id->match_flags; id++) {
554 if (is_card_dev(dev) &&
555 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
556 id->dev_type == to_ap_dev(dev)->device_type)
558 if (is_queue_dev(dev) &&
559 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
560 id->dev_type == to_ap_dev(dev)->device_type)
567 * ap_uevent(): Uevent function for AP devices.
568 * @dev: Pointer to device
569 * @env: Pointer to kobj_uevent_env
571 * It sets up a single environment variable DEV_TYPE which contains the
572 * hardware device type.
574 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
576 struct ap_device *ap_dev = to_ap_dev(dev);
582 /* Set up DEV_TYPE environment variable. */
583 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
588 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
593 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
595 if (is_queue_dev(dev) &&
596 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
597 device_unregister(dev);
601 static struct bus_type ap_bus_type = {
603 .match = &ap_bus_match,
604 .uevent = &ap_uevent,
607 static int __ap_revise_reserved(struct device *dev, void *dummy)
609 int rc, card, queue, devres, drvres;
611 if (is_queue_dev(dev)) {
612 card = AP_QID_CARD(to_ap_queue(dev)->qid);
613 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
614 mutex_lock(&ap_perms_mutex);
615 devres = test_bit_inv(card, ap_perms.apm)
616 && test_bit_inv(queue, ap_perms.aqm);
617 mutex_unlock(&ap_perms_mutex);
618 drvres = to_ap_drv(dev->driver)->flags
619 & AP_DRIVER_FLAG_DEFAULT;
620 if (!!devres != !!drvres) {
621 AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
623 rc = device_reprobe(dev);
630 static void ap_bus_revise_bindings(void)
632 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
635 int ap_owned_by_def_drv(int card, int queue)
639 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
642 mutex_lock(&ap_perms_mutex);
644 if (test_bit_inv(card, ap_perms.apm)
645 && test_bit_inv(queue, ap_perms.aqm))
648 mutex_unlock(&ap_perms_mutex);
652 EXPORT_SYMBOL(ap_owned_by_def_drv);
654 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
657 int card, queue, rc = 0;
659 mutex_lock(&ap_perms_mutex);
661 for (card = 0; !rc && card < AP_DEVICES; card++)
662 if (test_bit_inv(card, apm) &&
663 test_bit_inv(card, ap_perms.apm))
664 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
665 if (test_bit_inv(queue, aqm) &&
666 test_bit_inv(queue, ap_perms.aqm))
669 mutex_unlock(&ap_perms_mutex);
673 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
675 static int ap_device_probe(struct device *dev)
677 struct ap_device *ap_dev = to_ap_dev(dev);
678 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
679 int card, queue, devres, drvres, rc;
681 if (is_queue_dev(dev)) {
683 * If the apqn is marked as reserved/used by ap bus and
684 * default drivers, only probe with drivers with the default
685 * flag set. If it is not marked, only probe with drivers
686 * with the default flag not set.
688 card = AP_QID_CARD(to_ap_queue(dev)->qid);
689 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
690 mutex_lock(&ap_perms_mutex);
691 devres = test_bit_inv(card, ap_perms.apm)
692 && test_bit_inv(queue, ap_perms.aqm);
693 mutex_unlock(&ap_perms_mutex);
694 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
695 if (!!devres != !!drvres)
699 /* Add queue/card to list of active queues/cards */
700 spin_lock_bh(&ap_queues_lock);
701 if (is_queue_dev(dev))
702 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
703 to_ap_queue(dev)->qid);
704 spin_unlock_bh(&ap_queues_lock);
706 ap_dev->drv = ap_drv;
707 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
710 spin_lock_bh(&ap_queues_lock);
711 if (is_queue_dev(dev))
712 hash_del(&to_ap_queue(dev)->hnode);
713 spin_unlock_bh(&ap_queues_lock);
720 static int ap_device_remove(struct device *dev)
722 struct ap_device *ap_dev = to_ap_dev(dev);
723 struct ap_driver *ap_drv = ap_dev->drv;
725 /* prepare ap queue device removal */
726 if (is_queue_dev(dev))
727 ap_queue_prepare_remove(to_ap_queue(dev));
729 /* driver's chance to clean up gracefully */
731 ap_drv->remove(ap_dev);
733 /* now do the ap queue device remove */
734 if (is_queue_dev(dev))
735 ap_queue_remove(to_ap_queue(dev));
737 /* Remove queue/card from list of active queues/cards */
738 spin_lock_bh(&ap_queues_lock);
739 if (is_queue_dev(dev))
740 hash_del(&to_ap_queue(dev)->hnode);
741 spin_unlock_bh(&ap_queues_lock);
746 struct ap_queue *ap_get_qdev(ap_qid_t qid)
751 spin_lock_bh(&ap_queues_lock);
752 hash_for_each(ap_queues, bkt, aq, hnode) {
753 if (aq->qid == qid) {
754 get_device(&aq->ap_dev.device);
755 spin_unlock_bh(&ap_queues_lock);
759 spin_unlock_bh(&ap_queues_lock);
763 EXPORT_SYMBOL(ap_get_qdev);
765 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
768 struct device_driver *drv = &ap_drv->driver;
770 drv->bus = &ap_bus_type;
771 drv->probe = ap_device_probe;
772 drv->remove = ap_device_remove;
775 return driver_register(drv);
777 EXPORT_SYMBOL(ap_driver_register);
779 void ap_driver_unregister(struct ap_driver *ap_drv)
781 driver_unregister(&ap_drv->driver);
783 EXPORT_SYMBOL(ap_driver_unregister);
785 void ap_bus_force_rescan(void)
787 /* processing a asynchronous bus rescan */
788 del_timer(&ap_config_timer);
789 queue_work(system_long_wq, &ap_scan_work);
790 flush_work(&ap_scan_work);
792 EXPORT_SYMBOL(ap_bus_force_rescan);
795 * A config change has happened, force an ap bus rescan.
797 void ap_bus_cfg_chg(void)
799 AP_DBF(DBF_INFO, "%s config change, forcing bus rescan\n", __func__);
801 ap_bus_force_rescan();
805 * hex2bitmap() - parse hex mask string and set bitmap.
806 * Valid strings are "0x012345678" with at least one valid hex number.
807 * Rest of the bitmap to the right is padded with 0. No spaces allowed
808 * within the string, the leading 0x may be omitted.
809 * Returns the bitmask with exactly the bits set as given by the hex
810 * string (both in big endian order).
812 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
816 /* bits needs to be a multiple of 8 */
820 if (str[0] == '0' && str[1] == 'x')
825 for (i = 0; isxdigit(*str) && i < bits; str++) {
826 b = hex_to_bin(*str);
827 for (n = 0; n < 4; n++)
829 set_bit_inv(i + n, bitmap);
841 * modify_bitmap() - parse bitmask argument and modify an existing
842 * bit mask accordingly. A concatenation (done with ',') of these
843 * terms is recognized:
844 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
845 * <bitnr> may be any valid number (hex, decimal or octal) in the range
846 * 0...bits-1; the leading + or - is required. Here are some examples:
847 * +0-15,+32,-128,-0xFF
848 * -0-255,+1-16,+0x128
849 * +1,+2,+3,+4,-5,-7-10
850 * Returns the new bitmap after all changes have been applied. Every
851 * positive value in the string will set a bit and every negative value
852 * in the string will clear a bit. As a bit may be touched more than once,
853 * the last 'operation' wins:
854 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
855 * cleared again. All other bits are unmodified.
857 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
862 /* bits needs to be a multiple of 8 */
868 if (sign != '+' && sign != '-')
870 a = z = simple_strtoul(str, &np, 0);
871 if (str == np || a >= bits)
875 z = simple_strtoul(++str, &np, 0);
876 if (str == np || a > z || z >= bits)
880 for (i = a; i <= z; i++)
882 set_bit_inv(i, bitmap);
884 clear_bit_inv(i, bitmap);
885 while (*str == ',' || *str == '\n')
892 int ap_parse_mask_str(const char *str,
893 unsigned long *bitmap, int bits,
896 unsigned long *newmap, size;
899 /* bits needs to be a multiple of 8 */
903 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
904 newmap = kmalloc(size, GFP_KERNEL);
907 if (mutex_lock_interruptible(lock)) {
912 if (*str == '+' || *str == '-') {
913 memcpy(newmap, bitmap, size);
914 rc = modify_bitmap(str, newmap, bits);
916 memset(newmap, 0, size);
917 rc = hex2bitmap(str, newmap, bits);
920 memcpy(bitmap, newmap, size);
925 EXPORT_SYMBOL(ap_parse_mask_str);
931 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
933 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
936 static ssize_t ap_domain_store(struct bus_type *bus,
937 const char *buf, size_t count)
941 if (sscanf(buf, "%i\n", &domain) != 1 ||
942 domain < 0 || domain > ap_max_domain_id ||
943 !test_bit_inv(domain, ap_perms.aqm))
946 spin_lock_bh(&ap_domain_lock);
947 ap_domain_index = domain;
948 spin_unlock_bh(&ap_domain_lock);
950 AP_DBF(DBF_INFO, "stored new default domain=%d\n", domain);
955 static BUS_ATTR_RW(ap_domain);
957 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
959 if (!ap_qci_info) /* QCI not supported */
960 return scnprintf(buf, PAGE_SIZE, "not supported\n");
962 return scnprintf(buf, PAGE_SIZE,
963 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
964 ap_qci_info->adm[0], ap_qci_info->adm[1],
965 ap_qci_info->adm[2], ap_qci_info->adm[3],
966 ap_qci_info->adm[4], ap_qci_info->adm[5],
967 ap_qci_info->adm[6], ap_qci_info->adm[7]);
970 static BUS_ATTR_RO(ap_control_domain_mask);
972 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
974 if (!ap_qci_info) /* QCI not supported */
975 return scnprintf(buf, PAGE_SIZE, "not supported\n");
977 return scnprintf(buf, PAGE_SIZE,
978 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
979 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
980 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
981 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
982 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
985 static BUS_ATTR_RO(ap_usage_domain_mask);
987 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
989 if (!ap_qci_info) /* QCI not supported */
990 return scnprintf(buf, PAGE_SIZE, "not supported\n");
992 return scnprintf(buf, PAGE_SIZE,
993 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
994 ap_qci_info->apm[0], ap_qci_info->apm[1],
995 ap_qci_info->apm[2], ap_qci_info->apm[3],
996 ap_qci_info->apm[4], ap_qci_info->apm[5],
997 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1000 static BUS_ATTR_RO(ap_adapter_mask);
1002 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1004 return scnprintf(buf, PAGE_SIZE, "%d\n",
1005 ap_using_interrupts() ? 1 : 0);
1008 static BUS_ATTR_RO(ap_interrupts);
1010 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1012 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1015 static ssize_t config_time_store(struct bus_type *bus,
1016 const char *buf, size_t count)
1020 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1022 ap_config_time = time;
1023 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1027 static BUS_ATTR_RW(config_time);
1029 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1031 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1034 static ssize_t poll_thread_store(struct bus_type *bus,
1035 const char *buf, size_t count)
1039 if (sscanf(buf, "%d\n", &flag) != 1)
1042 rc = ap_poll_thread_start();
1046 ap_poll_thread_stop();
1050 static BUS_ATTR_RW(poll_thread);
1052 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1054 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1057 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1060 unsigned long long time;
1063 /* 120 seconds = maximum poll interval */
1064 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1065 time > 120000000000ULL)
1067 poll_timeout = time;
1068 hr_time = poll_timeout;
1070 spin_lock_bh(&ap_poll_timer_lock);
1071 hrtimer_cancel(&ap_poll_timer);
1072 hrtimer_set_expires(&ap_poll_timer, hr_time);
1073 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1074 spin_unlock_bh(&ap_poll_timer_lock);
1079 static BUS_ATTR_RW(poll_timeout);
1081 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1083 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1086 static BUS_ATTR_RO(ap_max_domain_id);
1088 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1090 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1093 static BUS_ATTR_RO(ap_max_adapter_id);
1095 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1099 if (mutex_lock_interruptible(&ap_perms_mutex))
1100 return -ERESTARTSYS;
1101 rc = scnprintf(buf, PAGE_SIZE,
1102 "0x%016lx%016lx%016lx%016lx\n",
1103 ap_perms.apm[0], ap_perms.apm[1],
1104 ap_perms.apm[2], ap_perms.apm[3]);
1105 mutex_unlock(&ap_perms_mutex);
1110 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1115 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1119 ap_bus_revise_bindings();
1124 static BUS_ATTR_RW(apmask);
1126 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1130 if (mutex_lock_interruptible(&ap_perms_mutex))
1131 return -ERESTARTSYS;
1132 rc = scnprintf(buf, PAGE_SIZE,
1133 "0x%016lx%016lx%016lx%016lx\n",
1134 ap_perms.aqm[0], ap_perms.aqm[1],
1135 ap_perms.aqm[2], ap_perms.aqm[3]);
1136 mutex_unlock(&ap_perms_mutex);
1141 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1146 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1150 ap_bus_revise_bindings();
1155 static BUS_ATTR_RW(aqmask);
1157 static struct bus_attribute *const ap_bus_attrs[] = {
1158 &bus_attr_ap_domain,
1159 &bus_attr_ap_control_domain_mask,
1160 &bus_attr_ap_usage_domain_mask,
1161 &bus_attr_ap_adapter_mask,
1162 &bus_attr_config_time,
1163 &bus_attr_poll_thread,
1164 &bus_attr_ap_interrupts,
1165 &bus_attr_poll_timeout,
1166 &bus_attr_ap_max_domain_id,
1167 &bus_attr_ap_max_adapter_id,
1174 * ap_select_domain(): Select an AP domain if possible and we haven't
1175 * already done so before.
1177 static void ap_select_domain(void)
1179 struct ap_queue_status status;
1183 * Choose the default domain. Either the one specified with
1184 * the "domain=" parameter or the first domain with at least
1187 spin_lock_bh(&ap_domain_lock);
1188 if (ap_domain_index >= 0) {
1189 /* Domain has already been selected. */
1192 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1193 if (!ap_test_config_usage_domain(dom) ||
1194 !test_bit_inv(dom, ap_perms.aqm))
1196 for (card = 0; card <= ap_max_adapter_id; card++) {
1197 if (!ap_test_config_card_id(card) ||
1198 !test_bit_inv(card, ap_perms.apm))
1200 status = ap_test_queue(AP_MKQID(card, dom),
1201 ap_apft_available(),
1203 if (status.response_code == AP_RESPONSE_NORMAL)
1206 if (card <= ap_max_adapter_id)
1209 if (dom <= ap_max_domain_id) {
1210 ap_domain_index = dom;
1211 AP_DBF(DBF_DEBUG, "%s new default domain is %d\n",
1212 __func__, ap_domain_index);
1215 spin_unlock_bh(&ap_domain_lock);
1219 * This function checks the type and returns either 0 for not
1220 * supported or the highest compatible type value (which may
1221 * include the input type value).
1223 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1227 /* < CEX2A is not supported */
1228 if (rawtype < AP_DEVICE_TYPE_CEX2A)
1230 /* up to CEX7 known and fully supported */
1231 if (rawtype <= AP_DEVICE_TYPE_CEX7)
1234 * unknown new type > CEX7, check for compatibility
1235 * to the highest known and supported type which is
1236 * currently CEX7 with the help of the QACT function.
1238 if (ap_qact_available()) {
1239 struct ap_queue_status status;
1240 union ap_qact_ap_info apinfo = {0};
1242 apinfo.mode = (func >> 26) & 0x07;
1243 apinfo.cat = AP_DEVICE_TYPE_CEX7;
1244 status = ap_qact(qid, 0, &apinfo);
1245 if (status.response_code == AP_RESPONSE_NORMAL
1246 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1247 && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
1248 comp_type = apinfo.cat;
1251 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
1252 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1253 else if (comp_type != rawtype)
1254 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
1255 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
1260 * Helper function to be used with bus_find_dev
1261 * matches for the card device with the given id
1263 static int __match_card_device_with_id(struct device *dev, const void *data)
1265 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1269 * Helper function to be used with bus_find_dev
1270 * matches for the queue device with a given qid
1272 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1274 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1278 * Helper function to be used with bus_find_dev
1279 * matches any queue device with given queue id
1281 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1283 return is_queue_dev(dev)
1284 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1288 * Helper function for ap_scan_bus().
1289 * Does the scan bus job for the given adapter id.
1291 static void _ap_scan_bus_adapter(int id)
1298 struct ap_queue *aq;
1299 int rc, dom, depth, type, comp_type;
1301 /* check if there is a card device registered with this id */
1302 dev = bus_find_device(&ap_bus_type, NULL,
1304 __match_card_device_with_id);
1305 ac = dev ? to_ap_card(dev) : NULL;
1306 if (!ap_test_config_card_id(id)) {
1308 /* Card device has been removed from configuration */
1309 bus_for_each_dev(&ap_bus_type, NULL,
1311 __ap_queue_devices_with_id_unregister);
1312 device_unregister(dev);
1319 * This card id is enabled in the configuration. If we already have
1320 * a card device with this id, check if type and functions are still
1321 * the very same. Also verify that at least one queue is available.
1324 /* find the first valid queue */
1325 for (dom = 0; dom < AP_DOMAINS; dom++) {
1326 qid = AP_MKQID(id, dom);
1327 if (ap_queue_info(qid, &type, &func, &depth))
1331 if (dom >= AP_DOMAINS) {
1332 /* no accessible queue on this card */
1334 } else if (ac->raw_hwtype != type) {
1335 /* card type has changed */
1336 AP_DBF(DBF_INFO, "card=%02x type changed.\n", id);
1338 } else if (ac->functions != func) {
1339 /* card functions have changed */
1340 AP_DBF(DBF_INFO, "card=%02x functions changed.\n", id);
1344 /* unregister card device and associated queues */
1345 bus_for_each_dev(&ap_bus_type, NULL,
1347 __ap_queue_devices_with_id_unregister);
1348 device_unregister(dev);
1350 /* go back if there is no valid queue on this card */
1351 if (dom >= AP_DOMAINS)
1358 * Go through all possible queue ids. Check and maybe create or release
1359 * queue devices for this card. If there exists no card device yet,
1360 * create a card device also.
1362 for (dom = 0; dom < AP_DOMAINS; dom++) {
1363 qid = AP_MKQID(id, dom);
1364 dev = bus_find_device(&ap_bus_type, NULL,
1366 __match_queue_device_with_qid);
1367 aq = dev ? to_ap_queue(dev) : NULL;
1368 if (!ap_test_config_usage_domain(dom)) {
1370 /* Queue device exists but has been
1371 * removed from configuration.
1373 device_unregister(dev);
1378 /* try to fetch infos about this queue */
1379 broken = !ap_queue_info(qid, &type, &func, &depth);
1382 spin_lock_bh(&aq->lock);
1383 broken = aq->sm_state == AP_SM_STATE_BORKED;
1384 spin_unlock_bh(&aq->lock);
1387 /* Remove broken device */
1389 "removing broken queue=%02x.%04x\n",
1391 device_unregister(dev);
1398 /* a new queue device is needed, check out comp type */
1399 comp_type = ap_get_compatible_type(qid, type, func);
1402 /* maybe a card device needs to be created first */
1404 ac = ap_card_create(id, depth, type, comp_type, func);
1407 ac->ap_dev.device.bus = &ap_bus_type;
1408 ac->ap_dev.device.parent = ap_root_device;
1409 dev_set_name(&ac->ap_dev.device, "card%02x", id);
1410 /* Register card device with AP bus */
1411 rc = device_register(&ac->ap_dev.device);
1413 put_device(&ac->ap_dev.device);
1417 /* get it and thus adjust reference counter */
1418 get_device(&ac->ap_dev.device);
1420 /* now create the new queue device */
1421 aq = ap_queue_create(qid, comp_type);
1425 aq->ap_dev.device.bus = &ap_bus_type;
1426 aq->ap_dev.device.parent = &ac->ap_dev.device;
1427 dev_set_name(&aq->ap_dev.device, "%02x.%04x", id, dom);
1428 /* Register queue device */
1429 rc = device_register(&aq->ap_dev.device);
1431 put_device(&aq->ap_dev.device);
1434 } /* end domain loop */
1437 put_device(&ac->ap_dev.device);
1441 * ap_scan_bus(): Scan the AP bus for new devices
1442 * Runs periodically, workqueue timer (ap_config_time)
1444 static void ap_scan_bus(struct work_struct *unused)
1448 ap_fetch_qci_info(ap_qci_info);
1451 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
1453 /* loop over all possible adapters */
1454 for (id = 0; id < AP_DEVICES; id++)
1455 _ap_scan_bus_adapter(id);
1457 /* check if there is at least one queue available with default domain */
1458 if (ap_domain_index >= 0) {
1459 struct device *dev =
1460 bus_find_device(&ap_bus_type, NULL,
1461 (void *)(long) ap_domain_index,
1462 __match_queue_device_with_queue_id);
1467 "no queue device with default domain %d available\n",
1471 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1474 static void ap_config_timeout(struct timer_list *unused)
1476 queue_work(system_long_wq, &ap_scan_work);
1479 static int __init ap_debug_init(void)
1481 ap_dbf_info = debug_register("ap", 1, 1,
1482 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1483 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1484 debug_set_level(ap_dbf_info, DBF_ERR);
1489 static void __init ap_perms_init(void)
1491 /* all resources useable if no kernel parameter string given */
1492 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1493 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1494 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1496 /* apm kernel parameter string */
1498 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1499 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1503 /* aqm kernel parameter string */
1505 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1506 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1512 * ap_module_init(): The module initialization code.
1514 * Initializes the module.
1516 static int __init ap_module_init(void)
1520 rc = ap_debug_init();
1524 if (!ap_instructions_available()) {
1525 pr_warn("The hardware system does not support AP instructions\n");
1529 /* init ap_queue hashtable */
1530 hash_init(ap_queues);
1532 /* set up the AP permissions (ioctls, ap and aq masks) */
1535 /* Get AP configuration data if available */
1538 /* check default domain setting */
1539 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
1540 (ap_domain_index >= 0 &&
1541 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1542 pr_warn("%d is not a valid cryptographic domain\n",
1544 ap_domain_index = -1;
1547 /* enable interrupts if available */
1548 if (ap_interrupts_available()) {
1549 rc = register_adapter_interrupt(&ap_airq);
1550 ap_airq_flag = (rc == 0);
1553 /* Create /sys/bus/ap. */
1554 rc = bus_register(&ap_bus_type);
1557 for (i = 0; ap_bus_attrs[i]; i++) {
1558 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1563 /* Create /sys/devices/ap. */
1564 ap_root_device = root_device_register("ap");
1565 rc = PTR_ERR_OR_ZERO(ap_root_device);
1569 /* Setup the AP bus rescan timer. */
1570 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1573 * Setup the high resultion poll timer.
1574 * If we are running under z/VM adjust polling to z/VM polling rate.
1577 poll_timeout = 1500000;
1578 spin_lock_init(&ap_poll_timer_lock);
1579 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1580 ap_poll_timer.function = ap_poll_timeout;
1582 /* Start the low priority AP bus poll thread. */
1583 if (ap_thread_flag) {
1584 rc = ap_poll_thread_start();
1589 queue_work(system_long_wq, &ap_scan_work);
1594 hrtimer_cancel(&ap_poll_timer);
1595 root_device_unregister(ap_root_device);
1598 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1599 bus_unregister(&ap_bus_type);
1601 if (ap_using_interrupts())
1602 unregister_adapter_interrupt(&ap_airq);
1606 device_initcall(ap_module_init);