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_configuration;
77 static bool initialised;
80 * AP bus related debug feature things.
82 debug_info_t *ap_dbf_info;
85 * Workqueue timer for bus rescan.
87 static struct timer_list ap_config_timer;
88 static int ap_config_time = AP_CONFIG_TIME;
89 static void ap_scan_bus(struct work_struct *);
90 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
93 * Tasklet & timer for AP request polling and interrupts
95 static void ap_tasklet_fn(unsigned long);
96 static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
97 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
98 static struct task_struct *ap_poll_kthread;
99 static DEFINE_MUTEX(ap_poll_thread_mutex);
100 static DEFINE_SPINLOCK(ap_poll_timer_lock);
101 static struct hrtimer ap_poll_timer;
103 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
104 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
106 static unsigned long long poll_timeout = 250000;
108 /* Maximum domain id */
109 static int ap_max_domain_id;
111 static struct bus_type ap_bus_type;
113 /* Adapter interrupt definitions */
114 static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
116 static int ap_airq_flag;
118 static struct airq_struct ap_airq = {
119 .handler = ap_interrupt_handler,
124 * ap_using_interrupts() - Returns non-zero if interrupt support is
127 static inline int ap_using_interrupts(void)
133 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
135 * Returns the address of the local-summary-indicator of the adapter
136 * interrupt handler for AP, or NULL if adapter interrupts are not
139 void *ap_airq_ptr(void)
141 if (ap_using_interrupts())
142 return ap_airq.lsi_ptr;
147 * ap_interrupts_available(): Test if AP interrupts are available.
149 * Returns 1 if AP interrupts are available.
151 static int ap_interrupts_available(void)
153 return test_facility(65);
157 * ap_configuration_available(): Test if AP configuration
158 * information is available.
160 * Returns 1 if AP configuration information is available.
162 static int ap_configuration_available(void)
164 return test_facility(12);
168 * ap_apft_available(): Test if AP facilities test (APFT)
169 * facility is available.
171 * Returns 1 if APFT is is available.
173 static int ap_apft_available(void)
175 return test_facility(15);
179 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
181 * Returns 1 if the QACT subfunction is available.
183 static inline int ap_qact_available(void)
185 if (ap_configuration)
186 return ap_configuration->qact;
191 * ap_query_configuration(): Fetch cryptographic config info
193 * Returns the ap configuration info fetched via PQAP(QCI).
194 * On success 0 is returned, on failure a negative errno
195 * is returned, e.g. if the PQAP(QCI) instruction is not
196 * available, the return value will be -EOPNOTSUPP.
198 static inline int ap_query_configuration(struct ap_config_info *info)
200 if (!ap_configuration_available())
208 * ap_init_configuration(): Allocate and query configuration array.
210 static void ap_init_configuration(void)
212 if (!ap_configuration_available())
215 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
216 if (!ap_configuration)
218 if (ap_query_configuration(ap_configuration) != 0) {
219 kfree(ap_configuration);
220 ap_configuration = NULL;
226 * ap_test_config(): helper function to extract the nrth bit
227 * within the unsigned int array field.
229 static inline int ap_test_config(unsigned int *field, unsigned int nr)
231 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
235 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
238 * Returns 0 if the card is not configured
239 * 1 if the card is configured or
240 * if the configuration information is not available
242 static inline int ap_test_config_card_id(unsigned int id)
244 if (!ap_configuration) /* QCI not supported */
245 /* only ids 0...3F may be probed */
246 return id < 0x40 ? 1 : 0;
247 return ap_test_config(ap_configuration->apm, id);
251 * ap_test_config_usage_domain(): Test, whether an AP usage domain
253 * @domain AP usage domain ID
255 * Returns 0 if the usage domain is not configured
256 * 1 if the usage domain is configured or
257 * if the configuration information is not available
259 int ap_test_config_usage_domain(unsigned int domain)
261 if (!ap_configuration) /* QCI not supported */
263 return ap_test_config(ap_configuration->aqm, domain);
265 EXPORT_SYMBOL(ap_test_config_usage_domain);
268 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
270 * @domain AP control domain ID
272 * Returns 1 if the control domain is configured
273 * 0 in all other cases
275 int ap_test_config_ctrl_domain(unsigned int domain)
277 if (!ap_configuration) /* QCI not supported */
279 return ap_test_config(ap_configuration->adm, domain);
281 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
284 * ap_query_queue(): Check if an AP queue is available.
285 * @qid: The AP queue number
286 * @queue_depth: Pointer to queue depth value
287 * @device_type: Pointer to device type value
288 * @facilities: Pointer to facility indicator
290 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
291 unsigned int *facilities)
293 struct ap_queue_status status;
297 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
300 status = ap_test_queue(qid, ap_apft_available(), &info);
301 switch (status.response_code) {
302 case AP_RESPONSE_NORMAL:
303 *queue_depth = (int)(info & 0xff);
304 *device_type = (int)((info >> 24) & 0xff);
305 *facilities = (unsigned int)(info >> 32);
306 /* Update maximum domain id */
307 nd = (info >> 16) & 0xff;
308 /* if N bit is available, z13 and newer */
309 if ((info & (1UL << 57)) && nd > 0)
310 ap_max_domain_id = nd;
311 else /* older machine types */
312 ap_max_domain_id = 15;
313 switch (*device_type) {
314 /* For CEX2 and CEX3 the available functions
315 * are not reflected by the facilities bits.
316 * Instead it is coded into the type. So here
317 * modify the function bits based on the type.
319 case AP_DEVICE_TYPE_CEX2A:
320 case AP_DEVICE_TYPE_CEX3A:
321 *facilities |= 0x08000000;
323 case AP_DEVICE_TYPE_CEX2C:
324 case AP_DEVICE_TYPE_CEX3C:
325 *facilities |= 0x10000000;
331 case AP_RESPONSE_Q_NOT_AVAIL:
332 case AP_RESPONSE_DECONFIGURED:
333 case AP_RESPONSE_CHECKSTOPPED:
334 case AP_RESPONSE_INVALID_ADDRESS:
336 case AP_RESPONSE_RESET_IN_PROGRESS:
337 case AP_RESPONSE_OTHERWISE_CHANGED:
338 case AP_RESPONSE_BUSY:
345 void ap_wait(enum ap_wait wait)
351 case AP_WAIT_INTERRUPT:
352 if (ap_using_interrupts())
354 if (ap_poll_kthread) {
355 wake_up(&ap_poll_wait);
359 case AP_WAIT_TIMEOUT:
360 spin_lock_bh(&ap_poll_timer_lock);
361 if (!hrtimer_is_queued(&ap_poll_timer)) {
362 hr_time = poll_timeout;
363 hrtimer_forward_now(&ap_poll_timer, hr_time);
364 hrtimer_restart(&ap_poll_timer);
366 spin_unlock_bh(&ap_poll_timer_lock);
375 * ap_request_timeout(): Handling of request timeouts
376 * @t: timer making this callback
378 * Handles request timeouts.
380 void ap_request_timeout(struct timer_list *t)
382 struct ap_queue *aq = from_timer(aq, t, timeout);
384 spin_lock_bh(&aq->lock);
385 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
386 spin_unlock_bh(&aq->lock);
390 * ap_poll_timeout(): AP receive polling for finished AP requests.
391 * @unused: Unused pointer.
393 * Schedules the AP tasklet using a high resolution timer.
395 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
397 tasklet_schedule(&ap_tasklet);
398 return HRTIMER_NORESTART;
402 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
403 * @airq: pointer to adapter interrupt descriptor
405 static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
407 inc_irq_stat(IRQIO_APB);
408 tasklet_schedule(&ap_tasklet);
412 * ap_tasklet_fn(): Tasklet to poll all AP devices.
413 * @dummy: Unused variable
415 * Poll all AP devices on the bus.
417 static void ap_tasklet_fn(unsigned long dummy)
421 enum ap_wait wait = AP_WAIT_NONE;
423 /* Reset the indicator if interrupts are used. Thus new interrupts can
424 * be received. Doing it in the beginning of the tasklet is therefor
425 * important that no requests on any AP get lost.
427 if (ap_using_interrupts())
428 xchg(ap_airq.lsi_ptr, 0);
430 spin_lock_bh(&ap_queues_lock);
431 hash_for_each(ap_queues, bkt, aq, hnode) {
432 spin_lock_bh(&aq->lock);
433 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
434 spin_unlock_bh(&aq->lock);
436 spin_unlock_bh(&ap_queues_lock);
441 static int ap_pending_requests(void)
446 spin_lock_bh(&ap_queues_lock);
447 hash_for_each(ap_queues, bkt, aq, hnode) {
448 if (aq->queue_count == 0)
450 spin_unlock_bh(&ap_queues_lock);
453 spin_unlock_bh(&ap_queues_lock);
458 * ap_poll_thread(): Thread that polls for finished requests.
459 * @data: Unused pointer
461 * AP bus poll thread. The purpose of this thread is to poll for
462 * finished requests in a loop if there is a "free" cpu - that is
463 * a cpu that doesn't have anything better to do. The polling stops
464 * as soon as there is another task or if all messages have been
467 static int ap_poll_thread(void *data)
469 DECLARE_WAITQUEUE(wait, current);
471 set_user_nice(current, MAX_NICE);
473 while (!kthread_should_stop()) {
474 add_wait_queue(&ap_poll_wait, &wait);
475 set_current_state(TASK_INTERRUPTIBLE);
476 if (!ap_pending_requests()) {
480 set_current_state(TASK_RUNNING);
481 remove_wait_queue(&ap_poll_wait, &wait);
482 if (need_resched()) {
493 static int ap_poll_thread_start(void)
497 if (ap_using_interrupts() || ap_poll_kthread)
499 mutex_lock(&ap_poll_thread_mutex);
500 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
501 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
503 ap_poll_kthread = NULL;
504 mutex_unlock(&ap_poll_thread_mutex);
508 static void ap_poll_thread_stop(void)
510 if (!ap_poll_kthread)
512 mutex_lock(&ap_poll_thread_mutex);
513 kthread_stop(ap_poll_kthread);
514 ap_poll_kthread = NULL;
515 mutex_unlock(&ap_poll_thread_mutex);
518 #define is_card_dev(x) ((x)->parent == ap_root_device)
519 #define is_queue_dev(x) ((x)->parent != ap_root_device)
523 * @dev: Pointer to device
524 * @drv: Pointer to device_driver
526 * AP bus driver registration/unregistration.
528 static int ap_bus_match(struct device *dev, struct device_driver *drv)
530 struct ap_driver *ap_drv = to_ap_drv(drv);
531 struct ap_device_id *id;
534 * Compare device type of the device with the list of
535 * supported types of the device_driver.
537 for (id = ap_drv->ids; id->match_flags; id++) {
538 if (is_card_dev(dev) &&
539 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
540 id->dev_type == to_ap_dev(dev)->device_type)
542 if (is_queue_dev(dev) &&
543 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
544 id->dev_type == to_ap_dev(dev)->device_type)
551 * ap_uevent(): Uevent function for AP devices.
552 * @dev: Pointer to device
553 * @env: Pointer to kobj_uevent_env
555 * It sets up a single environment variable DEV_TYPE which contains the
556 * hardware device type.
558 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
560 struct ap_device *ap_dev = to_ap_dev(dev);
566 /* Set up DEV_TYPE environment variable. */
567 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
572 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
577 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
579 if (is_queue_dev(dev) &&
580 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
581 device_unregister(dev);
585 static struct bus_type ap_bus_type = {
587 .match = &ap_bus_match,
588 .uevent = &ap_uevent,
591 static int __ap_revise_reserved(struct device *dev, void *dummy)
593 int rc, card, queue, devres, drvres;
595 if (is_queue_dev(dev)) {
596 card = AP_QID_CARD(to_ap_queue(dev)->qid);
597 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
598 mutex_lock(&ap_perms_mutex);
599 devres = test_bit_inv(card, ap_perms.apm)
600 && test_bit_inv(queue, ap_perms.aqm);
601 mutex_unlock(&ap_perms_mutex);
602 drvres = to_ap_drv(dev->driver)->flags
603 & AP_DRIVER_FLAG_DEFAULT;
604 if (!!devres != !!drvres) {
605 AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
607 rc = device_reprobe(dev);
614 static void ap_bus_revise_bindings(void)
616 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
619 int ap_owned_by_def_drv(int card, int queue)
623 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
626 mutex_lock(&ap_perms_mutex);
628 if (test_bit_inv(card, ap_perms.apm)
629 && test_bit_inv(queue, ap_perms.aqm))
632 mutex_unlock(&ap_perms_mutex);
636 EXPORT_SYMBOL(ap_owned_by_def_drv);
638 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
641 int card, queue, rc = 0;
643 mutex_lock(&ap_perms_mutex);
645 for (card = 0; !rc && card < AP_DEVICES; card++)
646 if (test_bit_inv(card, apm) &&
647 test_bit_inv(card, ap_perms.apm))
648 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
649 if (test_bit_inv(queue, aqm) &&
650 test_bit_inv(queue, ap_perms.aqm))
653 mutex_unlock(&ap_perms_mutex);
657 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
659 static int ap_device_probe(struct device *dev)
661 struct ap_device *ap_dev = to_ap_dev(dev);
662 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
663 int card, queue, devres, drvres, rc;
665 if (is_queue_dev(dev)) {
667 * If the apqn is marked as reserved/used by ap bus and
668 * default drivers, only probe with drivers with the default
669 * flag set. If it is not marked, only probe with drivers
670 * with the default flag not set.
672 card = AP_QID_CARD(to_ap_queue(dev)->qid);
673 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
674 mutex_lock(&ap_perms_mutex);
675 devres = test_bit_inv(card, ap_perms.apm)
676 && test_bit_inv(queue, ap_perms.aqm);
677 mutex_unlock(&ap_perms_mutex);
678 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
679 if (!!devres != !!drvres)
683 /* Add queue/card to list of active queues/cards */
684 spin_lock_bh(&ap_queues_lock);
685 if (is_queue_dev(dev))
686 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
687 to_ap_queue(dev)->qid);
688 spin_unlock_bh(&ap_queues_lock);
690 ap_dev->drv = ap_drv;
691 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
694 spin_lock_bh(&ap_queues_lock);
695 if (is_queue_dev(dev))
696 hash_del(&to_ap_queue(dev)->hnode);
697 spin_unlock_bh(&ap_queues_lock);
704 static int ap_device_remove(struct device *dev)
706 struct ap_device *ap_dev = to_ap_dev(dev);
707 struct ap_driver *ap_drv = ap_dev->drv;
709 /* prepare ap queue device removal */
710 if (is_queue_dev(dev))
711 ap_queue_prepare_remove(to_ap_queue(dev));
713 /* driver's chance to clean up gracefully */
715 ap_drv->remove(ap_dev);
717 /* now do the ap queue device remove */
718 if (is_queue_dev(dev))
719 ap_queue_remove(to_ap_queue(dev));
721 /* Remove queue/card from list of active queues/cards */
722 spin_lock_bh(&ap_queues_lock);
723 if (is_queue_dev(dev))
724 hash_del(&to_ap_queue(dev)->hnode);
725 spin_unlock_bh(&ap_queues_lock);
730 struct ap_queue *ap_get_qdev(ap_qid_t qid)
735 spin_lock_bh(&ap_queues_lock);
736 hash_for_each(ap_queues, bkt, aq, hnode) {
737 if (aq->qid == qid) {
738 get_device(&aq->ap_dev.device);
739 spin_unlock_bh(&ap_queues_lock);
743 spin_unlock_bh(&ap_queues_lock);
747 EXPORT_SYMBOL(ap_get_qdev);
749 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
752 struct device_driver *drv = &ap_drv->driver;
757 drv->bus = &ap_bus_type;
758 drv->probe = ap_device_probe;
759 drv->remove = ap_device_remove;
762 return driver_register(drv);
764 EXPORT_SYMBOL(ap_driver_register);
766 void ap_driver_unregister(struct ap_driver *ap_drv)
768 driver_unregister(&ap_drv->driver);
770 EXPORT_SYMBOL(ap_driver_unregister);
772 void ap_bus_force_rescan(void)
774 /* processing a asynchronous bus rescan */
775 del_timer(&ap_config_timer);
776 queue_work(system_long_wq, &ap_scan_work);
777 flush_work(&ap_scan_work);
779 EXPORT_SYMBOL(ap_bus_force_rescan);
782 * A config change has happened, force an ap bus rescan.
784 void ap_bus_cfg_chg(void)
786 AP_DBF(DBF_INFO, "%s config change, forcing bus rescan\n", __func__);
788 ap_bus_force_rescan();
792 * hex2bitmap() - parse hex mask string and set bitmap.
793 * Valid strings are "0x012345678" with at least one valid hex number.
794 * Rest of the bitmap to the right is padded with 0. No spaces allowed
795 * within the string, the leading 0x may be omitted.
796 * Returns the bitmask with exactly the bits set as given by the hex
797 * string (both in big endian order).
799 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
803 /* bits needs to be a multiple of 8 */
807 if (str[0] == '0' && str[1] == 'x')
812 for (i = 0; isxdigit(*str) && i < bits; str++) {
813 b = hex_to_bin(*str);
814 for (n = 0; n < 4; n++)
816 set_bit_inv(i + n, bitmap);
828 * modify_bitmap() - parse bitmask argument and modify an existing
829 * bit mask accordingly. A concatenation (done with ',') of these
830 * terms is recognized:
831 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
832 * <bitnr> may be any valid number (hex, decimal or octal) in the range
833 * 0...bits-1; the leading + or - is required. Here are some examples:
834 * +0-15,+32,-128,-0xFF
835 * -0-255,+1-16,+0x128
836 * +1,+2,+3,+4,-5,-7-10
837 * Returns the new bitmap after all changes have been applied. Every
838 * positive value in the string will set a bit and every negative value
839 * in the string will clear a bit. As a bit may be touched more than once,
840 * the last 'operation' wins:
841 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
842 * cleared again. All other bits are unmodified.
844 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
849 /* bits needs to be a multiple of 8 */
855 if (sign != '+' && sign != '-')
857 a = z = simple_strtoul(str, &np, 0);
858 if (str == np || a >= bits)
862 z = simple_strtoul(++str, &np, 0);
863 if (str == np || a > z || z >= bits)
867 for (i = a; i <= z; i++)
869 set_bit_inv(i, bitmap);
871 clear_bit_inv(i, bitmap);
872 while (*str == ',' || *str == '\n')
879 int ap_parse_mask_str(const char *str,
880 unsigned long *bitmap, int bits,
883 unsigned long *newmap, size;
886 /* bits needs to be a multiple of 8 */
890 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
891 newmap = kmalloc(size, GFP_KERNEL);
894 if (mutex_lock_interruptible(lock)) {
899 if (*str == '+' || *str == '-') {
900 memcpy(newmap, bitmap, size);
901 rc = modify_bitmap(str, newmap, bits);
903 memset(newmap, 0, size);
904 rc = hex2bitmap(str, newmap, bits);
907 memcpy(bitmap, newmap, size);
912 EXPORT_SYMBOL(ap_parse_mask_str);
918 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
920 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
923 static ssize_t ap_domain_store(struct bus_type *bus,
924 const char *buf, size_t count)
928 if (sscanf(buf, "%i\n", &domain) != 1 ||
929 domain < 0 || domain > ap_max_domain_id ||
930 !test_bit_inv(domain, ap_perms.aqm))
932 spin_lock_bh(&ap_domain_lock);
933 ap_domain_index = domain;
934 spin_unlock_bh(&ap_domain_lock);
936 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
941 static BUS_ATTR_RW(ap_domain);
943 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
945 if (!ap_configuration) /* QCI not supported */
946 return scnprintf(buf, PAGE_SIZE, "not supported\n");
948 return scnprintf(buf, PAGE_SIZE,
949 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
950 ap_configuration->adm[0], ap_configuration->adm[1],
951 ap_configuration->adm[2], ap_configuration->adm[3],
952 ap_configuration->adm[4], ap_configuration->adm[5],
953 ap_configuration->adm[6], ap_configuration->adm[7]);
956 static BUS_ATTR_RO(ap_control_domain_mask);
958 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
960 if (!ap_configuration) /* QCI not supported */
961 return scnprintf(buf, PAGE_SIZE, "not supported\n");
963 return scnprintf(buf, PAGE_SIZE,
964 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
965 ap_configuration->aqm[0], ap_configuration->aqm[1],
966 ap_configuration->aqm[2], ap_configuration->aqm[3],
967 ap_configuration->aqm[4], ap_configuration->aqm[5],
968 ap_configuration->aqm[6], ap_configuration->aqm[7]);
971 static BUS_ATTR_RO(ap_usage_domain_mask);
973 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
975 if (!ap_configuration) /* QCI not supported */
976 return scnprintf(buf, PAGE_SIZE, "not supported\n");
978 return scnprintf(buf, PAGE_SIZE,
979 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
980 ap_configuration->apm[0], ap_configuration->apm[1],
981 ap_configuration->apm[2], ap_configuration->apm[3],
982 ap_configuration->apm[4], ap_configuration->apm[5],
983 ap_configuration->apm[6], ap_configuration->apm[7]);
986 static BUS_ATTR_RO(ap_adapter_mask);
988 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
990 return scnprintf(buf, PAGE_SIZE, "%d\n",
991 ap_using_interrupts() ? 1 : 0);
994 static BUS_ATTR_RO(ap_interrupts);
996 static ssize_t config_time_show(struct bus_type *bus, char *buf)
998 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1001 static ssize_t config_time_store(struct bus_type *bus,
1002 const char *buf, size_t count)
1006 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1008 ap_config_time = time;
1009 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1013 static BUS_ATTR_RW(config_time);
1015 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1017 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1020 static ssize_t poll_thread_store(struct bus_type *bus,
1021 const char *buf, size_t count)
1025 if (sscanf(buf, "%d\n", &flag) != 1)
1028 rc = ap_poll_thread_start();
1032 ap_poll_thread_stop();
1036 static BUS_ATTR_RW(poll_thread);
1038 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1040 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1043 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1046 unsigned long long time;
1049 /* 120 seconds = maximum poll interval */
1050 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1051 time > 120000000000ULL)
1053 poll_timeout = time;
1054 hr_time = poll_timeout;
1056 spin_lock_bh(&ap_poll_timer_lock);
1057 hrtimer_cancel(&ap_poll_timer);
1058 hrtimer_set_expires(&ap_poll_timer, hr_time);
1059 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1060 spin_unlock_bh(&ap_poll_timer_lock);
1065 static BUS_ATTR_RW(poll_timeout);
1067 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1071 if (ap_configuration)
1072 max_domain_id = ap_max_domain_id ? : -1;
1075 return scnprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1078 static BUS_ATTR_RO(ap_max_domain_id);
1080 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1084 if (mutex_lock_interruptible(&ap_perms_mutex))
1085 return -ERESTARTSYS;
1086 rc = scnprintf(buf, PAGE_SIZE,
1087 "0x%016lx%016lx%016lx%016lx\n",
1088 ap_perms.apm[0], ap_perms.apm[1],
1089 ap_perms.apm[2], ap_perms.apm[3]);
1090 mutex_unlock(&ap_perms_mutex);
1095 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1100 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1104 ap_bus_revise_bindings();
1109 static BUS_ATTR_RW(apmask);
1111 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1115 if (mutex_lock_interruptible(&ap_perms_mutex))
1116 return -ERESTARTSYS;
1117 rc = scnprintf(buf, PAGE_SIZE,
1118 "0x%016lx%016lx%016lx%016lx\n",
1119 ap_perms.aqm[0], ap_perms.aqm[1],
1120 ap_perms.aqm[2], ap_perms.aqm[3]);
1121 mutex_unlock(&ap_perms_mutex);
1126 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1131 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1135 ap_bus_revise_bindings();
1140 static BUS_ATTR_RW(aqmask);
1142 static struct bus_attribute *const ap_bus_attrs[] = {
1143 &bus_attr_ap_domain,
1144 &bus_attr_ap_control_domain_mask,
1145 &bus_attr_ap_usage_domain_mask,
1146 &bus_attr_ap_adapter_mask,
1147 &bus_attr_config_time,
1148 &bus_attr_poll_thread,
1149 &bus_attr_ap_interrupts,
1150 &bus_attr_poll_timeout,
1151 &bus_attr_ap_max_domain_id,
1158 * ap_select_domain(): Select an AP domain if possible and we haven't
1159 * already done so before.
1161 static void ap_select_domain(void)
1163 int count, max_count, best_domain;
1164 struct ap_queue_status status;
1168 * We want to use a single domain. Either the one specified with
1169 * the "domain=" parameter or the domain with the maximum number
1172 spin_lock_bh(&ap_domain_lock);
1173 if (ap_domain_index >= 0) {
1174 /* Domain has already been selected. */
1175 spin_unlock_bh(&ap_domain_lock);
1180 for (i = 0; i < AP_DOMAINS; i++) {
1181 if (!ap_test_config_usage_domain(i) ||
1182 !test_bit_inv(i, ap_perms.aqm))
1185 for (j = 0; j < AP_DEVICES; j++) {
1186 if (!ap_test_config_card_id(j))
1188 status = ap_test_queue(AP_MKQID(j, i),
1189 ap_apft_available(),
1191 if (status.response_code != AP_RESPONSE_NORMAL)
1195 if (count > max_count) {
1200 if (best_domain >= 0) {
1201 ap_domain_index = best_domain;
1202 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
1204 spin_unlock_bh(&ap_domain_lock);
1208 * This function checks the type and returns either 0 for not
1209 * supported or the highest compatible type value (which may
1210 * include the input type value).
1212 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1216 /* < CEX2A is not supported */
1217 if (rawtype < AP_DEVICE_TYPE_CEX2A)
1219 /* up to CEX7 known and fully supported */
1220 if (rawtype <= AP_DEVICE_TYPE_CEX7)
1223 * unknown new type > CEX7, check for compatibility
1224 * to the highest known and supported type which is
1225 * currently CEX7 with the help of the QACT function.
1227 if (ap_qact_available()) {
1228 struct ap_queue_status status;
1229 union ap_qact_ap_info apinfo = {0};
1231 apinfo.mode = (func >> 26) & 0x07;
1232 apinfo.cat = AP_DEVICE_TYPE_CEX7;
1233 status = ap_qact(qid, 0, &apinfo);
1234 if (status.response_code == AP_RESPONSE_NORMAL
1235 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1236 && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
1237 comp_type = apinfo.cat;
1240 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
1241 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1242 else if (comp_type != rawtype)
1243 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
1244 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
1249 * Helper function to be used with bus_find_dev
1250 * matches for the card device with the given id
1252 static int __match_card_device_with_id(struct device *dev, const void *data)
1254 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1258 * Helper function to be used with bus_find_dev
1259 * matches for the queue device with a given qid
1261 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1263 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1267 * Helper function to be used with bus_find_dev
1268 * matches any queue device with given queue id
1270 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1272 return is_queue_dev(dev)
1273 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1277 * Helper function for ap_scan_bus().
1278 * Does the scan bus job for the given adapter id.
1280 static void _ap_scan_bus_adapter(int id)
1286 struct ap_queue *aq;
1287 int rc, dom, depth, type, comp_type, borked;
1289 /* check if there is a card device registered with this id */
1290 dev = bus_find_device(&ap_bus_type, NULL,
1292 __match_card_device_with_id);
1293 ac = dev ? to_ap_card(dev) : NULL;
1294 if (!ap_test_config_card_id(id)) {
1296 /* Card device has been removed from configuration */
1297 bus_for_each_dev(&ap_bus_type, NULL,
1299 __ap_queue_devices_with_id_unregister);
1300 device_unregister(dev);
1307 * This card id is enabled in the configuration. If we already have
1308 * a card device with this id, check if type and functions are still
1309 * the very same. Also verify that at least one queue is available.
1312 /* find the first valid queue */
1313 for (dom = 0; dom < AP_DOMAINS; dom++) {
1314 qid = AP_MKQID(id, dom);
1315 if (ap_query_queue(qid, &depth, &type, &func) == 0)
1319 if (dom >= AP_DOMAINS) {
1320 /* no accessible queue on this card */
1322 } else if (ac->raw_hwtype != type) {
1323 /* card type has changed */
1324 AP_DBF(DBF_INFO, "card=%02x type changed.\n", id);
1326 } else if (ac->functions != func) {
1327 /* card functions have changed */
1328 AP_DBF(DBF_INFO, "card=%02x functions changed.\n", id);
1332 /* unregister card device and associated queues */
1333 bus_for_each_dev(&ap_bus_type, NULL,
1335 __ap_queue_devices_with_id_unregister);
1336 device_unregister(dev);
1338 /* go back if there is no valid queue on this card */
1339 if (dom >= AP_DOMAINS)
1346 * Go through all possible queue ids. Check and maybe create or release
1347 * queue devices for this card. If there exists no card device yet,
1348 * create a card device also.
1350 for (dom = 0; dom < AP_DOMAINS; dom++) {
1351 qid = AP_MKQID(id, dom);
1352 dev = bus_find_device(&ap_bus_type, NULL,
1354 __match_queue_device_with_qid);
1355 aq = dev ? to_ap_queue(dev) : NULL;
1356 if (!ap_test_config_usage_domain(dom)) {
1358 /* Queue device exists but has been
1359 * removed from configuration.
1361 device_unregister(dev);
1366 /* try to fetch infos about this queue */
1367 rc = ap_query_queue(qid, &depth, &type, &func);
1372 spin_lock_bh(&aq->lock);
1373 borked = aq->state == AP_STATE_BORKED;
1374 spin_unlock_bh(&aq->lock);
1377 /* Remove broken device */
1379 "removing broken queue=%02x.%04x\n",
1381 device_unregister(dev);
1388 /* a new queue device is needed, check out comp type */
1389 comp_type = ap_get_compatible_type(qid, type, func);
1392 /* maybe a card device needs to be created first */
1394 ac = ap_card_create(id, depth, type, comp_type, func);
1397 ac->ap_dev.device.bus = &ap_bus_type;
1398 ac->ap_dev.device.parent = ap_root_device;
1399 dev_set_name(&ac->ap_dev.device, "card%02x", id);
1400 /* Register card device with AP bus */
1401 rc = device_register(&ac->ap_dev.device);
1403 put_device(&ac->ap_dev.device);
1407 /* get it and thus adjust reference counter */
1408 get_device(&ac->ap_dev.device);
1410 /* now create the new queue device */
1411 aq = ap_queue_create(qid, comp_type);
1415 aq->ap_dev.device.bus = &ap_bus_type;
1416 aq->ap_dev.device.parent = &ac->ap_dev.device;
1417 dev_set_name(&aq->ap_dev.device, "%02x.%04x", id, dom);
1418 /* Register queue device */
1419 rc = device_register(&aq->ap_dev.device);
1421 put_device(&aq->ap_dev.device);
1424 } /* end domain loop */
1427 put_device(&ac->ap_dev.device);
1431 * ap_scan_bus(): Scan the AP bus for new devices
1432 * Runs periodically, workqueue timer (ap_config_time)
1434 static void ap_scan_bus(struct work_struct *unused)
1438 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
1440 ap_query_configuration(ap_configuration);
1443 /* loop over all possible adapters */
1444 for (id = 0; id < AP_DEVICES; id++)
1445 _ap_scan_bus_adapter(id);
1447 /* check if there is at least one queue available with default domain */
1448 if (ap_domain_index >= 0) {
1449 struct device *dev =
1450 bus_find_device(&ap_bus_type, NULL,
1451 (void *)(long) ap_domain_index,
1452 __match_queue_device_with_queue_id);
1457 "no queue device with default domain %d available\n",
1461 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1464 static void ap_config_timeout(struct timer_list *unused)
1466 queue_work(system_long_wq, &ap_scan_work);
1469 static int __init ap_debug_init(void)
1471 ap_dbf_info = debug_register("ap", 1, 1,
1472 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1473 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1474 debug_set_level(ap_dbf_info, DBF_ERR);
1479 static void __init ap_perms_init(void)
1481 /* all resources useable if no kernel parameter string given */
1482 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1483 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1484 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1486 /* apm kernel parameter string */
1488 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1489 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1493 /* aqm kernel parameter string */
1495 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1496 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1502 * ap_module_init(): The module initialization code.
1504 * Initializes the module.
1506 static int __init ap_module_init(void)
1511 rc = ap_debug_init();
1515 if (!ap_instructions_available()) {
1516 pr_warn("The hardware system does not support AP instructions\n");
1520 /* init ap_queue hashtable */
1521 hash_init(ap_queues);
1523 /* set up the AP permissions (ioctls, ap and aq masks) */
1526 /* Get AP configuration data if available */
1527 ap_init_configuration();
1529 if (ap_configuration)
1531 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1534 if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
1535 (ap_domain_index >= 0 &&
1536 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1537 pr_warn("%d is not a valid cryptographic domain\n",
1539 ap_domain_index = -1;
1542 if (ap_interrupts_available()) {
1543 rc = register_adapter_interrupt(&ap_airq);
1544 ap_airq_flag = (rc == 0);
1547 /* Create /sys/bus/ap. */
1548 rc = bus_register(&ap_bus_type);
1551 for (i = 0; ap_bus_attrs[i]; i++) {
1552 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1557 /* Create /sys/devices/ap. */
1558 ap_root_device = root_device_register("ap");
1559 rc = PTR_ERR_OR_ZERO(ap_root_device);
1563 /* Setup the AP bus rescan timer. */
1564 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1567 * Setup the high resultion poll timer.
1568 * If we are running under z/VM adjust polling to z/VM polling rate.
1571 poll_timeout = 1500000;
1572 spin_lock_init(&ap_poll_timer_lock);
1573 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1574 ap_poll_timer.function = ap_poll_timeout;
1576 /* Start the low priority AP bus poll thread. */
1577 if (ap_thread_flag) {
1578 rc = ap_poll_thread_start();
1583 queue_work(system_long_wq, &ap_scan_work);
1589 hrtimer_cancel(&ap_poll_timer);
1590 root_device_unregister(ap_root_device);
1593 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1594 bus_unregister(&ap_bus_type);
1596 if (ap_using_interrupts())
1597 unregister_adapter_interrupt(&ap_airq);
1598 kfree(ap_configuration);
1601 device_initcall(ap_module_init);