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/interrupt.h>
22 #include <linux/workqueue.h>
23 #include <linux/slab.h>
24 #include <linux/notifier.h>
25 #include <linux/kthread.h>
26 #include <linux/mutex.h>
27 #include <linux/suspend.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 DEFINE_SPINLOCK(ap_list_lock);
66 LIST_HEAD(ap_card_list);
68 /* Default permissions (ioctl, card and domain masking) */
69 struct ap_perms ap_perms;
70 EXPORT_SYMBOL(ap_perms);
71 DEFINE_MUTEX(ap_perms_mutex);
72 EXPORT_SYMBOL(ap_perms_mutex);
74 static struct ap_config_info *ap_configuration;
75 static bool initialised;
78 * AP bus related debug feature things.
80 debug_info_t *ap_dbf_info;
83 * Workqueue timer for bus rescan.
85 static struct timer_list ap_config_timer;
86 static int ap_config_time = AP_CONFIG_TIME;
87 static void ap_scan_bus(struct work_struct *);
88 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
91 * Tasklet & timer for AP request polling and interrupts
93 static void ap_tasklet_fn(unsigned long);
94 static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
95 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
96 static struct task_struct *ap_poll_kthread;
97 static DEFINE_MUTEX(ap_poll_thread_mutex);
98 static DEFINE_SPINLOCK(ap_poll_timer_lock);
99 static struct hrtimer ap_poll_timer;
101 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
102 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
104 static unsigned long long poll_timeout = 250000;
107 static int ap_suspend_flag;
108 /* Maximum domain id */
109 static int ap_max_domain_id;
111 * Flag to check if domain was set through module parameter domain=. This is
112 * important when supsend and resume is done in a z/VM environment where the
113 * domain might change.
115 static int user_set_domain;
116 static struct bus_type ap_bus_type;
118 /* Adapter interrupt definitions */
119 static void ap_interrupt_handler(struct airq_struct *airq);
121 static int ap_airq_flag;
123 static struct airq_struct ap_airq = {
124 .handler = ap_interrupt_handler,
129 * ap_using_interrupts() - Returns non-zero if interrupt support is
132 static inline int ap_using_interrupts(void)
138 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
140 * Returns the address of the local-summary-indicator of the adapter
141 * interrupt handler for AP, or NULL if adapter interrupts are not
144 void *ap_airq_ptr(void)
146 if (ap_using_interrupts())
147 return ap_airq.lsi_ptr;
152 * ap_interrupts_available(): Test if AP interrupts are available.
154 * Returns 1 if AP interrupts are available.
156 static int ap_interrupts_available(void)
158 return test_facility(65);
162 * ap_configuration_available(): Test if AP configuration
163 * information is available.
165 * Returns 1 if AP configuration information is available.
167 static int ap_configuration_available(void)
169 return test_facility(12);
173 * ap_apft_available(): Test if AP facilities test (APFT)
174 * facility is available.
176 * Returns 1 if APFT is is available.
178 static int ap_apft_available(void)
180 return test_facility(15);
184 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
186 * Returns 1 if the QACT subfunction is available.
188 static inline int ap_qact_available(void)
190 if (ap_configuration)
191 return ap_configuration->qact;
196 * ap_query_configuration(): Fetch cryptographic config info
198 * Returns the ap configuration info fetched via PQAP(QCI).
199 * On success 0 is returned, on failure a negative errno
200 * is returned, e.g. if the PQAP(QCI) instruction is not
201 * available, the return value will be -EOPNOTSUPP.
203 static inline int ap_query_configuration(struct ap_config_info *info)
205 if (!ap_configuration_available())
211 EXPORT_SYMBOL(ap_query_configuration);
214 * ap_init_configuration(): Allocate and query configuration array.
216 static void ap_init_configuration(void)
218 if (!ap_configuration_available())
221 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
222 if (!ap_configuration)
224 if (ap_query_configuration(ap_configuration) != 0) {
225 kfree(ap_configuration);
226 ap_configuration = NULL;
232 * ap_test_config(): helper function to extract the nrth bit
233 * within the unsigned int array field.
235 static inline int ap_test_config(unsigned int *field, unsigned int nr)
237 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
241 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
244 * Returns 0 if the card is not configured
245 * 1 if the card is configured or
246 * if the configuration information is not available
248 static inline int ap_test_config_card_id(unsigned int id)
250 if (!ap_configuration) /* QCI not supported */
252 return ap_test_config(ap_configuration->apm, id);
256 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
257 * @domain AP usage domain ID
259 * Returns 0 if the usage domain is not configured
260 * 1 if the usage domain is configured or
261 * if the configuration information is not available
263 static inline int ap_test_config_domain(unsigned int domain)
265 if (!ap_configuration) /* QCI not supported */
267 return ap_test_config(ap_configuration->aqm, domain);
271 * ap_query_queue(): Check if an AP queue is available.
272 * @qid: The AP queue number
273 * @queue_depth: Pointer to queue depth value
274 * @device_type: Pointer to device type value
275 * @facilities: Pointer to facility indicator
277 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
278 unsigned int *facilities)
280 struct ap_queue_status status;
284 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
287 status = ap_test_queue(qid, ap_apft_available(), &info);
288 switch (status.response_code) {
289 case AP_RESPONSE_NORMAL:
290 *queue_depth = (int)(info & 0xff);
291 *device_type = (int)((info >> 24) & 0xff);
292 *facilities = (unsigned int)(info >> 32);
293 /* Update maximum domain id */
294 nd = (info >> 16) & 0xff;
295 /* if N bit is available, z13 and newer */
296 if ((info & (1UL << 57)) && nd > 0)
297 ap_max_domain_id = nd;
298 else /* older machine types */
299 ap_max_domain_id = 15;
300 switch (*device_type) {
301 /* For CEX2 and CEX3 the available functions
302 * are not refrected by the facilities bits.
303 * Instead it is coded into the type. So here
304 * modify the function bits based on the type.
306 case AP_DEVICE_TYPE_CEX2A:
307 case AP_DEVICE_TYPE_CEX3A:
308 *facilities |= 0x08000000;
310 case AP_DEVICE_TYPE_CEX2C:
311 case AP_DEVICE_TYPE_CEX3C:
312 *facilities |= 0x10000000;
318 case AP_RESPONSE_Q_NOT_AVAIL:
319 case AP_RESPONSE_DECONFIGURED:
320 case AP_RESPONSE_CHECKSTOPPED:
321 case AP_RESPONSE_INVALID_ADDRESS:
323 case AP_RESPONSE_RESET_IN_PROGRESS:
324 case AP_RESPONSE_OTHERWISE_CHANGED:
325 case AP_RESPONSE_BUSY:
332 void ap_wait(enum ap_wait wait)
338 case AP_WAIT_INTERRUPT:
339 if (ap_using_interrupts())
341 if (ap_poll_kthread) {
342 wake_up(&ap_poll_wait);
346 case AP_WAIT_TIMEOUT:
347 spin_lock_bh(&ap_poll_timer_lock);
348 if (!hrtimer_is_queued(&ap_poll_timer)) {
349 hr_time = poll_timeout;
350 hrtimer_forward_now(&ap_poll_timer, hr_time);
351 hrtimer_restart(&ap_poll_timer);
353 spin_unlock_bh(&ap_poll_timer_lock);
362 * ap_request_timeout(): Handling of request timeouts
363 * @t: timer making this callback
365 * Handles request timeouts.
367 void ap_request_timeout(struct timer_list *t)
369 struct ap_queue *aq = from_timer(aq, t, timeout);
373 spin_lock_bh(&aq->lock);
374 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
375 spin_unlock_bh(&aq->lock);
379 * ap_poll_timeout(): AP receive polling for finished AP requests.
380 * @unused: Unused pointer.
382 * Schedules the AP tasklet using a high resolution timer.
384 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
386 if (!ap_suspend_flag)
387 tasklet_schedule(&ap_tasklet);
388 return HRTIMER_NORESTART;
392 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
393 * @airq: pointer to adapter interrupt descriptor
395 static void ap_interrupt_handler(struct airq_struct *airq)
397 inc_irq_stat(IRQIO_APB);
398 if (!ap_suspend_flag)
399 tasklet_schedule(&ap_tasklet);
403 * ap_tasklet_fn(): Tasklet to poll all AP devices.
404 * @dummy: Unused variable
406 * Poll all AP devices on the bus.
408 static void ap_tasklet_fn(unsigned long dummy)
412 enum ap_wait wait = AP_WAIT_NONE;
414 /* Reset the indicator if interrupts are used. Thus new interrupts can
415 * be received. Doing it in the beginning of the tasklet is therefor
416 * important that no requests on any AP get lost.
418 if (ap_using_interrupts())
419 xchg(ap_airq.lsi_ptr, 0);
421 spin_lock_bh(&ap_list_lock);
422 for_each_ap_card(ac) {
423 for_each_ap_queue(aq, ac) {
424 spin_lock_bh(&aq->lock);
425 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
426 spin_unlock_bh(&aq->lock);
429 spin_unlock_bh(&ap_list_lock);
434 static int ap_pending_requests(void)
439 spin_lock_bh(&ap_list_lock);
440 for_each_ap_card(ac) {
441 for_each_ap_queue(aq, ac) {
442 if (aq->queue_count == 0)
444 spin_unlock_bh(&ap_list_lock);
448 spin_unlock_bh(&ap_list_lock);
453 * ap_poll_thread(): Thread that polls for finished requests.
454 * @data: Unused pointer
456 * AP bus poll thread. The purpose of this thread is to poll for
457 * finished requests in a loop if there is a "free" cpu - that is
458 * a cpu that doesn't have anything better to do. The polling stops
459 * as soon as there is another task or if all messages have been
462 static int ap_poll_thread(void *data)
464 DECLARE_WAITQUEUE(wait, current);
466 set_user_nice(current, MAX_NICE);
468 while (!kthread_should_stop()) {
469 add_wait_queue(&ap_poll_wait, &wait);
470 set_current_state(TASK_INTERRUPTIBLE);
471 if (ap_suspend_flag || !ap_pending_requests()) {
475 set_current_state(TASK_RUNNING);
476 remove_wait_queue(&ap_poll_wait, &wait);
477 if (need_resched()) {
488 static int ap_poll_thread_start(void)
492 if (ap_using_interrupts() || ap_poll_kthread)
494 mutex_lock(&ap_poll_thread_mutex);
495 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
496 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
498 ap_poll_kthread = NULL;
499 mutex_unlock(&ap_poll_thread_mutex);
503 static void ap_poll_thread_stop(void)
505 if (!ap_poll_kthread)
507 mutex_lock(&ap_poll_thread_mutex);
508 kthread_stop(ap_poll_kthread);
509 ap_poll_kthread = NULL;
510 mutex_unlock(&ap_poll_thread_mutex);
513 #define is_card_dev(x) ((x)->parent == ap_root_device)
514 #define is_queue_dev(x) ((x)->parent != ap_root_device)
518 * @dev: Pointer to device
519 * @drv: Pointer to device_driver
521 * AP bus driver registration/unregistration.
523 static int ap_bus_match(struct device *dev, struct device_driver *drv)
525 struct ap_driver *ap_drv = to_ap_drv(drv);
526 struct ap_device_id *id;
529 * Compare device type of the device with the list of
530 * supported types of the device_driver.
532 for (id = ap_drv->ids; id->match_flags; id++) {
533 if (is_card_dev(dev) &&
534 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
535 id->dev_type == to_ap_dev(dev)->device_type)
537 if (is_queue_dev(dev) &&
538 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
539 id->dev_type == to_ap_dev(dev)->device_type)
546 * ap_uevent(): Uevent function for AP devices.
547 * @dev: Pointer to device
548 * @env: Pointer to kobj_uevent_env
550 * It sets up a single environment variable DEV_TYPE which contains the
551 * hardware device type.
553 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
555 struct ap_device *ap_dev = to_ap_dev(dev);
561 /* Set up DEV_TYPE environment variable. */
562 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
567 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
572 static int ap_dev_suspend(struct device *dev)
574 struct ap_device *ap_dev = to_ap_dev(dev);
576 if (ap_dev->drv && ap_dev->drv->suspend)
577 ap_dev->drv->suspend(ap_dev);
581 static int ap_dev_resume(struct device *dev)
583 struct ap_device *ap_dev = to_ap_dev(dev);
585 if (ap_dev->drv && ap_dev->drv->resume)
586 ap_dev->drv->resume(ap_dev);
590 static void ap_bus_suspend(void)
592 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
596 * Disable scanning for devices, thus we do not want to scan
597 * for them after removing.
599 flush_work(&ap_scan_work);
600 tasklet_disable(&ap_tasklet);
603 static int __ap_card_devices_unregister(struct device *dev, void *dummy)
605 if (is_card_dev(dev))
606 device_unregister(dev);
610 static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
612 if (is_queue_dev(dev))
613 device_unregister(dev);
617 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
619 if (is_queue_dev(dev) &&
620 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
621 device_unregister(dev);
625 static void ap_bus_resume(void)
629 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
631 /* remove all queue devices */
632 bus_for_each_dev(&ap_bus_type, NULL, NULL,
633 __ap_queue_devices_unregister);
634 /* remove all card devices */
635 bus_for_each_dev(&ap_bus_type, NULL, NULL,
636 __ap_card_devices_unregister);
638 /* Reset thin interrupt setting */
639 if (ap_interrupts_available() && !ap_using_interrupts()) {
640 rc = register_adapter_interrupt(&ap_airq);
641 ap_airq_flag = (rc == 0);
643 if (!ap_interrupts_available() && ap_using_interrupts()) {
644 unregister_adapter_interrupt(&ap_airq);
648 if (!user_set_domain)
649 ap_domain_index = -1;
650 /* Get things going again */
653 xchg(ap_airq.lsi_ptr, 0);
654 tasklet_enable(&ap_tasklet);
655 queue_work(system_long_wq, &ap_scan_work);
658 static int ap_power_event(struct notifier_block *this, unsigned long event,
662 case PM_HIBERNATION_PREPARE:
663 case PM_SUSPEND_PREPARE:
666 case PM_POST_HIBERNATION:
667 case PM_POST_SUSPEND:
675 static struct notifier_block ap_power_notifier = {
676 .notifier_call = ap_power_event,
679 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
681 static struct bus_type ap_bus_type = {
683 .match = &ap_bus_match,
684 .uevent = &ap_uevent,
685 .pm = &ap_bus_pm_ops,
688 static int __ap_revise_reserved(struct device *dev, void *dummy)
690 int rc, card, queue, devres, drvres;
692 if (is_queue_dev(dev)) {
693 card = AP_QID_CARD(to_ap_queue(dev)->qid);
694 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
695 mutex_lock(&ap_perms_mutex);
696 devres = test_bit_inv(card, ap_perms.apm)
697 && test_bit_inv(queue, ap_perms.aqm);
698 mutex_unlock(&ap_perms_mutex);
699 drvres = to_ap_drv(dev->driver)->flags
700 & AP_DRIVER_FLAG_DEFAULT;
701 if (!!devres != !!drvres) {
702 AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
704 rc = device_reprobe(dev);
711 static void ap_bus_revise_bindings(void)
713 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
716 int ap_owned_by_def_drv(int card, int queue)
720 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
723 mutex_lock(&ap_perms_mutex);
725 if (test_bit_inv(card, ap_perms.apm)
726 && test_bit_inv(queue, ap_perms.aqm))
729 mutex_unlock(&ap_perms_mutex);
733 EXPORT_SYMBOL(ap_owned_by_def_drv);
735 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
738 int card, queue, rc = 0;
740 mutex_lock(&ap_perms_mutex);
742 for (card = 0; !rc && card < AP_DEVICES; card++)
743 if (test_bit_inv(card, apm) &&
744 test_bit_inv(card, ap_perms.apm))
745 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
746 if (test_bit_inv(queue, aqm) &&
747 test_bit_inv(queue, ap_perms.aqm))
750 mutex_unlock(&ap_perms_mutex);
754 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
756 static int ap_device_probe(struct device *dev)
758 struct ap_device *ap_dev = to_ap_dev(dev);
759 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
760 int card, queue, devres, drvres, rc;
762 if (is_queue_dev(dev)) {
764 * If the apqn is marked as reserved/used by ap bus and
765 * default drivers, only probe with drivers with the default
766 * flag set. If it is not marked, only probe with drivers
767 * with the default flag not set.
769 card = AP_QID_CARD(to_ap_queue(dev)->qid);
770 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
771 mutex_lock(&ap_perms_mutex);
772 devres = test_bit_inv(card, ap_perms.apm)
773 && test_bit_inv(queue, ap_perms.aqm);
774 mutex_unlock(&ap_perms_mutex);
775 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
776 if (!!devres != !!drvres)
778 /* (re-)init queue's state machine */
779 ap_queue_reinit_state(to_ap_queue(dev));
782 /* Add queue/card to list of active queues/cards */
783 spin_lock_bh(&ap_list_lock);
784 if (is_card_dev(dev))
785 list_add(&to_ap_card(dev)->list, &ap_card_list);
787 list_add(&to_ap_queue(dev)->list,
788 &to_ap_queue(dev)->card->queues);
789 spin_unlock_bh(&ap_list_lock);
791 ap_dev->drv = ap_drv;
792 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
795 spin_lock_bh(&ap_list_lock);
796 if (is_card_dev(dev))
797 list_del_init(&to_ap_card(dev)->list);
799 list_del_init(&to_ap_queue(dev)->list);
800 spin_unlock_bh(&ap_list_lock);
807 static int ap_device_remove(struct device *dev)
809 struct ap_device *ap_dev = to_ap_dev(dev);
810 struct ap_driver *ap_drv = ap_dev->drv;
812 if (is_queue_dev(dev))
813 ap_queue_remove(to_ap_queue(dev));
815 ap_drv->remove(ap_dev);
817 /* Remove queue/card from list of active queues/cards */
818 spin_lock_bh(&ap_list_lock);
819 if (is_card_dev(dev))
820 list_del_init(&to_ap_card(dev)->list);
822 list_del_init(&to_ap_queue(dev)->list);
823 spin_unlock_bh(&ap_list_lock);
828 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
831 struct device_driver *drv = &ap_drv->driver;
836 drv->bus = &ap_bus_type;
837 drv->probe = ap_device_probe;
838 drv->remove = ap_device_remove;
841 return driver_register(drv);
843 EXPORT_SYMBOL(ap_driver_register);
845 void ap_driver_unregister(struct ap_driver *ap_drv)
847 driver_unregister(&ap_drv->driver);
849 EXPORT_SYMBOL(ap_driver_unregister);
851 void ap_bus_force_rescan(void)
855 /* processing a asynchronous bus rescan */
856 del_timer(&ap_config_timer);
857 queue_work(system_long_wq, &ap_scan_work);
858 flush_work(&ap_scan_work);
860 EXPORT_SYMBOL(ap_bus_force_rescan);
863 * hex2bitmap() - parse hex mask string and set bitmap.
864 * Valid strings are "0x012345678" with at least one valid hex number.
865 * Rest of the bitmap to the right is padded with 0. No spaces allowed
866 * within the string, the leading 0x may be omitted.
867 * Returns the bitmask with exactly the bits set as given by the hex
868 * string (both in big endian order).
870 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
874 /* bits needs to be a multiple of 8 */
878 if (str[0] == '0' && str[1] == 'x')
883 for (i = 0; isxdigit(*str) && i < bits; str++) {
884 b = hex_to_bin(*str);
885 for (n = 0; n < 4; n++)
887 set_bit_inv(i + n, bitmap);
899 * modify_bitmap() - parse bitmask argument and modify an existing
900 * bit mask accordingly. A concatenation (done with ',') of these
901 * terms is recognized:
902 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
903 * <bitnr> may be any valid number (hex, decimal or octal) in the range
904 * 0...bits-1; the leading + or - is required. Here are some examples:
905 * +0-15,+32,-128,-0xFF
906 * -0-255,+1-16,+0x128
907 * +1,+2,+3,+4,-5,-7-10
908 * Returns the new bitmap after all changes have been applied. Every
909 * positive value in the string will set a bit and every negative value
910 * in the string will clear a bit. As a bit may be touched more than once,
911 * the last 'operation' wins:
912 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
913 * cleared again. All other bits are unmodified.
915 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
920 /* bits needs to be a multiple of 8 */
926 if (sign != '+' && sign != '-')
928 a = z = simple_strtoul(str, &np, 0);
929 if (str == np || a >= bits)
933 z = simple_strtoul(++str, &np, 0);
934 if (str == np || a > z || z >= bits)
938 for (i = a; i <= z; i++)
940 set_bit_inv(i, bitmap);
942 clear_bit_inv(i, bitmap);
943 while (*str == ',' || *str == '\n')
950 int ap_parse_mask_str(const char *str,
951 unsigned long *bitmap, int bits,
954 unsigned long *newmap, size;
957 /* bits needs to be a multiple of 8 */
961 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
962 newmap = kmalloc(size, GFP_KERNEL);
965 if (mutex_lock_interruptible(lock)) {
970 if (*str == '+' || *str == '-') {
971 memcpy(newmap, bitmap, size);
972 rc = modify_bitmap(str, newmap, bits);
974 memset(newmap, 0, size);
975 rc = hex2bitmap(str, newmap, bits);
978 memcpy(bitmap, newmap, size);
983 EXPORT_SYMBOL(ap_parse_mask_str);
989 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
991 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
994 static ssize_t ap_domain_store(struct bus_type *bus,
995 const char *buf, size_t count)
999 if (sscanf(buf, "%i\n", &domain) != 1 ||
1000 domain < 0 || domain > ap_max_domain_id ||
1001 !test_bit_inv(domain, ap_perms.aqm))
1003 spin_lock_bh(&ap_domain_lock);
1004 ap_domain_index = domain;
1005 spin_unlock_bh(&ap_domain_lock);
1007 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
1012 static BUS_ATTR_RW(ap_domain);
1014 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1016 if (!ap_configuration) /* QCI not supported */
1017 return snprintf(buf, PAGE_SIZE, "not supported\n");
1019 return snprintf(buf, PAGE_SIZE,
1020 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1021 ap_configuration->adm[0], ap_configuration->adm[1],
1022 ap_configuration->adm[2], ap_configuration->adm[3],
1023 ap_configuration->adm[4], ap_configuration->adm[5],
1024 ap_configuration->adm[6], ap_configuration->adm[7]);
1027 static BUS_ATTR_RO(ap_control_domain_mask);
1029 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1031 if (!ap_configuration) /* QCI not supported */
1032 return snprintf(buf, PAGE_SIZE, "not supported\n");
1034 return snprintf(buf, PAGE_SIZE,
1035 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1036 ap_configuration->aqm[0], ap_configuration->aqm[1],
1037 ap_configuration->aqm[2], ap_configuration->aqm[3],
1038 ap_configuration->aqm[4], ap_configuration->aqm[5],
1039 ap_configuration->aqm[6], ap_configuration->aqm[7]);
1042 static BUS_ATTR_RO(ap_usage_domain_mask);
1044 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1046 if (!ap_configuration) /* QCI not supported */
1047 return snprintf(buf, PAGE_SIZE, "not supported\n");
1049 return snprintf(buf, PAGE_SIZE,
1050 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1051 ap_configuration->apm[0], ap_configuration->apm[1],
1052 ap_configuration->apm[2], ap_configuration->apm[3],
1053 ap_configuration->apm[4], ap_configuration->apm[5],
1054 ap_configuration->apm[6], ap_configuration->apm[7]);
1057 static BUS_ATTR_RO(ap_adapter_mask);
1059 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1061 return snprintf(buf, PAGE_SIZE, "%d\n",
1062 ap_using_interrupts() ? 1 : 0);
1065 static BUS_ATTR_RO(ap_interrupts);
1067 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1069 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1072 static ssize_t config_time_store(struct bus_type *bus,
1073 const char *buf, size_t count)
1077 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1079 ap_config_time = time;
1080 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1084 static BUS_ATTR_RW(config_time);
1086 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1088 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1091 static ssize_t poll_thread_store(struct bus_type *bus,
1092 const char *buf, size_t count)
1096 if (sscanf(buf, "%d\n", &flag) != 1)
1099 rc = ap_poll_thread_start();
1103 ap_poll_thread_stop();
1107 static BUS_ATTR_RW(poll_thread);
1109 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1111 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1114 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1117 unsigned long long time;
1120 /* 120 seconds = maximum poll interval */
1121 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1122 time > 120000000000ULL)
1124 poll_timeout = time;
1125 hr_time = poll_timeout;
1127 spin_lock_bh(&ap_poll_timer_lock);
1128 hrtimer_cancel(&ap_poll_timer);
1129 hrtimer_set_expires(&ap_poll_timer, hr_time);
1130 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1131 spin_unlock_bh(&ap_poll_timer_lock);
1136 static BUS_ATTR_RW(poll_timeout);
1138 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1142 if (ap_configuration)
1143 max_domain_id = ap_max_domain_id ? : -1;
1146 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1149 static BUS_ATTR_RO(ap_max_domain_id);
1151 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1155 if (mutex_lock_interruptible(&ap_perms_mutex))
1156 return -ERESTARTSYS;
1157 rc = snprintf(buf, PAGE_SIZE,
1158 "0x%016lx%016lx%016lx%016lx\n",
1159 ap_perms.apm[0], ap_perms.apm[1],
1160 ap_perms.apm[2], ap_perms.apm[3]);
1161 mutex_unlock(&ap_perms_mutex);
1166 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1171 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1175 ap_bus_revise_bindings();
1180 static BUS_ATTR_RW(apmask);
1182 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1186 if (mutex_lock_interruptible(&ap_perms_mutex))
1187 return -ERESTARTSYS;
1188 rc = snprintf(buf, PAGE_SIZE,
1189 "0x%016lx%016lx%016lx%016lx\n",
1190 ap_perms.aqm[0], ap_perms.aqm[1],
1191 ap_perms.aqm[2], ap_perms.aqm[3]);
1192 mutex_unlock(&ap_perms_mutex);
1197 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1202 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1206 ap_bus_revise_bindings();
1211 static BUS_ATTR_RW(aqmask);
1213 static struct bus_attribute *const ap_bus_attrs[] = {
1214 &bus_attr_ap_domain,
1215 &bus_attr_ap_control_domain_mask,
1216 &bus_attr_ap_usage_domain_mask,
1217 &bus_attr_ap_adapter_mask,
1218 &bus_attr_config_time,
1219 &bus_attr_poll_thread,
1220 &bus_attr_ap_interrupts,
1221 &bus_attr_poll_timeout,
1222 &bus_attr_ap_max_domain_id,
1229 * ap_select_domain(): Select an AP domain if possible and we haven't
1230 * already done so before.
1232 static void ap_select_domain(void)
1234 int count, max_count, best_domain;
1235 struct ap_queue_status status;
1239 * We want to use a single domain. Either the one specified with
1240 * the "domain=" parameter or the domain with the maximum number
1243 spin_lock_bh(&ap_domain_lock);
1244 if (ap_domain_index >= 0) {
1245 /* Domain has already been selected. */
1246 spin_unlock_bh(&ap_domain_lock);
1251 for (i = 0; i < AP_DOMAINS; i++) {
1252 if (!ap_test_config_domain(i) ||
1253 !test_bit_inv(i, ap_perms.aqm))
1256 for (j = 0; j < AP_DEVICES; j++) {
1257 if (!ap_test_config_card_id(j))
1259 status = ap_test_queue(AP_MKQID(j, i),
1260 ap_apft_available(),
1262 if (status.response_code != AP_RESPONSE_NORMAL)
1266 if (count > max_count) {
1271 if (best_domain >= 0) {
1272 ap_domain_index = best_domain;
1273 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
1275 spin_unlock_bh(&ap_domain_lock);
1279 * This function checks the type and returns either 0 for not
1280 * supported or the highest compatible type value (which may
1281 * include the input type value).
1283 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1287 /* < CEX2A is not supported */
1288 if (rawtype < AP_DEVICE_TYPE_CEX2A)
1290 /* up to CEX6 known and fully supported */
1291 if (rawtype <= AP_DEVICE_TYPE_CEX6)
1294 * unknown new type > CEX6, check for compatibility
1295 * to the highest known and supported type which is
1296 * currently CEX6 with the help of the QACT function.
1298 if (ap_qact_available()) {
1299 struct ap_queue_status status;
1300 union ap_qact_ap_info apinfo = {0};
1302 apinfo.mode = (func >> 26) & 0x07;
1303 apinfo.cat = AP_DEVICE_TYPE_CEX6;
1304 status = ap_qact(qid, 0, &apinfo);
1305 if (status.response_code == AP_RESPONSE_NORMAL
1306 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1307 && apinfo.cat <= AP_DEVICE_TYPE_CEX6)
1308 comp_type = apinfo.cat;
1311 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
1312 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1313 else if (comp_type != rawtype)
1314 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
1315 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
1320 * helper function to be used with bus_find_dev
1321 * matches for the card device with the given id
1323 static int __match_card_device_with_id(struct device *dev, void *data)
1325 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
1328 /* helper function to be used with bus_find_dev
1329 * matches for the queue device with a given qid
1331 static int __match_queue_device_with_qid(struct device *dev, void *data)
1333 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1337 * ap_scan_bus(): Scan the AP bus for new devices
1338 * Runs periodically, workqueue timer (ap_config_time)
1340 static void ap_scan_bus(struct work_struct *unused)
1342 struct ap_queue *aq;
1346 int comp_type, depth = 0, type = 0;
1347 unsigned int func = 0;
1348 int rc, id, dom, borked, domains, defdomdevs = 0;
1350 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
1352 ap_query_configuration(ap_configuration);
1355 for (id = 0; id < AP_DEVICES; id++) {
1356 /* check if device is registered */
1357 dev = bus_find_device(&ap_bus_type, NULL,
1359 __match_card_device_with_id);
1360 ac = dev ? to_ap_card(dev) : NULL;
1361 if (!ap_test_config_card_id(id)) {
1363 /* Card device has been removed from
1364 * configuration, remove the belonging
1367 bus_for_each_dev(&ap_bus_type, NULL,
1369 __ap_queue_devices_with_id_unregister);
1370 /* now remove the card device */
1371 device_unregister(dev);
1376 /* According to the configuration there should be a card
1377 * device, so check if there is at least one valid queue
1378 * and maybe create queue devices and the card device.
1381 for (dom = 0; dom < AP_DOMAINS; dom++) {
1382 qid = AP_MKQID(id, dom);
1383 dev = bus_find_device(&ap_bus_type, NULL,
1385 __match_queue_device_with_qid);
1386 aq = dev ? to_ap_queue(dev) : NULL;
1387 if (!ap_test_config_domain(dom)) {
1389 /* Queue device exists but has been
1390 * removed from configuration.
1392 device_unregister(dev);
1397 rc = ap_query_queue(qid, &depth, &type, &func);
1399 spin_lock_bh(&aq->lock);
1400 if (rc == -ENODEV ||
1401 /* adapter reconfiguration */
1402 (ac && ac->functions != func))
1403 aq->state = AP_STATE_BORKED;
1404 borked = aq->state == AP_STATE_BORKED;
1405 spin_unlock_bh(&aq->lock);
1406 if (borked) /* Remove broken device */
1407 device_unregister(dev);
1411 if (dom == ap_domain_index)
1418 /* a new queue device is needed, check out comp type */
1419 comp_type = ap_get_compatible_type(qid, type, func);
1422 /* maybe a card device needs to be created first */
1424 ac = ap_card_create(id, depth, type,
1428 ac->ap_dev.device.bus = &ap_bus_type;
1429 ac->ap_dev.device.parent = ap_root_device;
1430 dev_set_name(&ac->ap_dev.device,
1432 /* Register card with AP bus */
1433 rc = device_register(&ac->ap_dev.device);
1435 put_device(&ac->ap_dev.device);
1439 /* get it and thus adjust reference counter */
1440 get_device(&ac->ap_dev.device);
1442 /* now create the new queue device */
1443 aq = ap_queue_create(qid, comp_type);
1447 aq->ap_dev.device.bus = &ap_bus_type;
1448 aq->ap_dev.device.parent = &ac->ap_dev.device;
1449 dev_set_name(&aq->ap_dev.device,
1450 "%02x.%04x", id, dom);
1451 /* Register device */
1452 rc = device_register(&aq->ap_dev.device);
1454 put_device(&aq->ap_dev.device);
1458 if (dom == ap_domain_index)
1460 } /* end domain loop */
1462 /* remove card dev if there are no queue devices */
1464 device_unregister(&ac->ap_dev.device);
1465 put_device(&ac->ap_dev.device);
1467 } /* end device loop */
1469 if (ap_domain_index >= 0 && defdomdevs < 1)
1471 "no queue device with default domain %d available\n",
1474 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1477 static void ap_config_timeout(struct timer_list *unused)
1479 if (ap_suspend_flag)
1481 queue_work(system_long_wq, &ap_scan_work);
1484 static int __init ap_debug_init(void)
1486 ap_dbf_info = debug_register("ap", 1, 1,
1487 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1488 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1489 debug_set_level(ap_dbf_info, DBF_ERR);
1494 static void __init ap_perms_init(void)
1496 /* all resources useable if no kernel parameter string given */
1497 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1498 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1499 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1501 /* apm kernel parameter string */
1503 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1504 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1508 /* aqm kernel parameter string */
1510 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1511 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1517 * ap_module_init(): The module initialization code.
1519 * Initializes the module.
1521 static int __init ap_module_init(void)
1526 rc = ap_debug_init();
1530 if (!ap_instructions_available()) {
1531 pr_warn("The hardware system does not support AP instructions\n");
1535 /* set up the AP permissions (ioctls, ap and aq masks) */
1538 /* Get AP configuration data if available */
1539 ap_init_configuration();
1541 if (ap_configuration)
1543 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1546 if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
1547 (ap_domain_index >= 0 &&
1548 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1549 pr_warn("%d is not a valid cryptographic domain\n",
1551 ap_domain_index = -1;
1553 /* In resume callback we need to know if the user had set the domain.
1554 * If so, we can not just reset it.
1556 if (ap_domain_index >= 0)
1557 user_set_domain = 1;
1559 if (ap_interrupts_available()) {
1560 rc = register_adapter_interrupt(&ap_airq);
1561 ap_airq_flag = (rc == 0);
1564 /* Create /sys/bus/ap. */
1565 rc = bus_register(&ap_bus_type);
1568 for (i = 0; ap_bus_attrs[i]; i++) {
1569 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1574 /* Create /sys/devices/ap. */
1575 ap_root_device = root_device_register("ap");
1576 rc = PTR_ERR_OR_ZERO(ap_root_device);
1580 /* Setup the AP bus rescan timer. */
1581 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1584 * Setup the high resultion poll timer.
1585 * If we are running under z/VM adjust polling to z/VM polling rate.
1588 poll_timeout = 1500000;
1589 spin_lock_init(&ap_poll_timer_lock);
1590 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1591 ap_poll_timer.function = ap_poll_timeout;
1593 /* Start the low priority AP bus poll thread. */
1594 if (ap_thread_flag) {
1595 rc = ap_poll_thread_start();
1600 rc = register_pm_notifier(&ap_power_notifier);
1604 queue_work(system_long_wq, &ap_scan_work);
1610 ap_poll_thread_stop();
1612 hrtimer_cancel(&ap_poll_timer);
1613 root_device_unregister(ap_root_device);
1616 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1617 bus_unregister(&ap_bus_type);
1619 if (ap_using_interrupts())
1620 unregister_adapter_interrupt(&ap_airq);
1621 kfree(ap_configuration);
1624 device_initcall(ap_module_init);