1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2006, 2012
4 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Ralph Wuerthner <rwuerthn@de.ibm.com>
7 * Felix Beck <felix.beck@de.ibm.com>
8 * Holger Dengler <hd@linux.vnet.ibm.com>
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 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;
106 /* Maximum domain id */
107 static int ap_max_domain_id
;
109 static struct bus_type ap_bus_type
;
111 /* Adapter interrupt definitions */
112 static void ap_interrupt_handler(struct airq_struct
*airq
, bool floating
);
114 static int ap_airq_flag
;
116 static struct airq_struct ap_airq
= {
117 .handler
= ap_interrupt_handler
,
122 * ap_using_interrupts() - Returns non-zero if interrupt support is
125 static inline int ap_using_interrupts(void)
131 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
133 * Returns the address of the local-summary-indicator of the adapter
134 * interrupt handler for AP, or NULL if adapter interrupts are not
137 void *ap_airq_ptr(void)
139 if (ap_using_interrupts())
140 return ap_airq
.lsi_ptr
;
145 * ap_interrupts_available(): Test if AP interrupts are available.
147 * Returns 1 if AP interrupts are available.
149 static int ap_interrupts_available(void)
151 return test_facility(65);
155 * ap_configuration_available(): Test if AP configuration
156 * information is available.
158 * Returns 1 if AP configuration information is available.
160 static int ap_configuration_available(void)
162 return test_facility(12);
166 * ap_apft_available(): Test if AP facilities test (APFT)
167 * facility is available.
169 * Returns 1 if APFT is is available.
171 static int ap_apft_available(void)
173 return test_facility(15);
177 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
179 * Returns 1 if the QACT subfunction is available.
181 static inline int ap_qact_available(void)
183 if (ap_configuration
)
184 return ap_configuration
->qact
;
189 * ap_query_configuration(): Fetch cryptographic config info
191 * Returns the ap configuration info fetched via PQAP(QCI).
192 * On success 0 is returned, on failure a negative errno
193 * is returned, e.g. if the PQAP(QCI) instruction is not
194 * available, the return value will be -EOPNOTSUPP.
196 static inline int ap_query_configuration(struct ap_config_info
*info
)
198 if (!ap_configuration_available())
206 * ap_init_configuration(): Allocate and query configuration array.
208 static void ap_init_configuration(void)
210 if (!ap_configuration_available())
213 ap_configuration
= kzalloc(sizeof(*ap_configuration
), GFP_KERNEL
);
214 if (!ap_configuration
)
216 if (ap_query_configuration(ap_configuration
) != 0) {
217 kfree(ap_configuration
);
218 ap_configuration
= NULL
;
224 * ap_test_config(): helper function to extract the nrth bit
225 * within the unsigned int array field.
227 static inline int ap_test_config(unsigned int *field
, unsigned int nr
)
229 return ap_test_bit((field
+ (nr
>> 5)), (nr
& 0x1f));
233 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
236 * Returns 0 if the card is not configured
237 * 1 if the card is configured or
238 * if the configuration information is not available
240 static inline int ap_test_config_card_id(unsigned int id
)
242 if (!ap_configuration
) /* QCI not supported */
243 /* only ids 0...3F may be probed */
244 return id
< 0x40 ? 1 : 0;
245 return ap_test_config(ap_configuration
->apm
, id
);
249 * ap_test_config_usage_domain(): Test, whether an AP usage domain
251 * @domain AP usage domain ID
253 * Returns 0 if the usage domain is not configured
254 * 1 if the usage domain is configured or
255 * if the configuration information is not available
257 int ap_test_config_usage_domain(unsigned int domain
)
259 if (!ap_configuration
) /* QCI not supported */
261 return ap_test_config(ap_configuration
->aqm
, domain
);
263 EXPORT_SYMBOL(ap_test_config_usage_domain
);
266 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
268 * @domain AP control domain ID
270 * Returns 1 if the control domain is configured
271 * 0 in all other cases
273 int ap_test_config_ctrl_domain(unsigned int domain
)
275 if (!ap_configuration
) /* QCI not supported */
277 return ap_test_config(ap_configuration
->adm
, domain
);
279 EXPORT_SYMBOL(ap_test_config_ctrl_domain
);
282 * ap_query_queue(): Check if an AP queue is available.
283 * @qid: The AP queue number
284 * @queue_depth: Pointer to queue depth value
285 * @device_type: Pointer to device type value
286 * @facilities: Pointer to facility indicator
288 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
,
289 unsigned int *facilities
)
291 struct ap_queue_status status
;
295 if (!ap_test_config_card_id(AP_QID_CARD(qid
)))
298 status
= ap_test_queue(qid
, ap_apft_available(), &info
);
299 switch (status
.response_code
) {
300 case AP_RESPONSE_NORMAL
:
301 *queue_depth
= (int)(info
& 0xff);
302 *device_type
= (int)((info
>> 24) & 0xff);
303 *facilities
= (unsigned int)(info
>> 32);
304 /* Update maximum domain id */
305 nd
= (info
>> 16) & 0xff;
306 /* if N bit is available, z13 and newer */
307 if ((info
& (1UL << 57)) && nd
> 0)
308 ap_max_domain_id
= nd
;
309 else /* older machine types */
310 ap_max_domain_id
= 15;
311 switch (*device_type
) {
312 /* For CEX2 and CEX3 the available functions
313 * are not reflected by the facilities bits.
314 * Instead it is coded into the type. So here
315 * modify the function bits based on the type.
317 case AP_DEVICE_TYPE_CEX2A
:
318 case AP_DEVICE_TYPE_CEX3A
:
319 *facilities
|= 0x08000000;
321 case AP_DEVICE_TYPE_CEX2C
:
322 case AP_DEVICE_TYPE_CEX3C
:
323 *facilities
|= 0x10000000;
329 case AP_RESPONSE_Q_NOT_AVAIL
:
330 case AP_RESPONSE_DECONFIGURED
:
331 case AP_RESPONSE_CHECKSTOPPED
:
332 case AP_RESPONSE_INVALID_ADDRESS
:
334 case AP_RESPONSE_RESET_IN_PROGRESS
:
335 case AP_RESPONSE_OTHERWISE_CHANGED
:
336 case AP_RESPONSE_BUSY
:
343 void ap_wait(enum ap_wait wait
)
349 case AP_WAIT_INTERRUPT
:
350 if (ap_using_interrupts())
352 if (ap_poll_kthread
) {
353 wake_up(&ap_poll_wait
);
357 case AP_WAIT_TIMEOUT
:
358 spin_lock_bh(&ap_poll_timer_lock
);
359 if (!hrtimer_is_queued(&ap_poll_timer
)) {
360 hr_time
= poll_timeout
;
361 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
362 hrtimer_restart(&ap_poll_timer
);
364 spin_unlock_bh(&ap_poll_timer_lock
);
373 * ap_request_timeout(): Handling of request timeouts
374 * @t: timer making this callback
376 * Handles request timeouts.
378 void ap_request_timeout(struct timer_list
*t
)
380 struct ap_queue
*aq
= from_timer(aq
, t
, timeout
);
382 spin_lock_bh(&aq
->lock
);
383 ap_wait(ap_sm_event(aq
, AP_EVENT_TIMEOUT
));
384 spin_unlock_bh(&aq
->lock
);
388 * ap_poll_timeout(): AP receive polling for finished AP requests.
389 * @unused: Unused pointer.
391 * Schedules the AP tasklet using a high resolution timer.
393 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
395 tasklet_schedule(&ap_tasklet
);
396 return HRTIMER_NORESTART
;
400 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
401 * @airq: pointer to adapter interrupt descriptor
403 static void ap_interrupt_handler(struct airq_struct
*airq
, bool floating
)
405 inc_irq_stat(IRQIO_APB
);
406 tasklet_schedule(&ap_tasklet
);
410 * ap_tasklet_fn(): Tasklet to poll all AP devices.
411 * @dummy: Unused variable
413 * Poll all AP devices on the bus.
415 static void ap_tasklet_fn(unsigned long dummy
)
419 enum ap_wait wait
= AP_WAIT_NONE
;
421 /* Reset the indicator if interrupts are used. Thus new interrupts can
422 * be received. Doing it in the beginning of the tasklet is therefor
423 * important that no requests on any AP get lost.
425 if (ap_using_interrupts())
426 xchg(ap_airq
.lsi_ptr
, 0);
428 spin_lock_bh(&ap_list_lock
);
429 for_each_ap_card(ac
) {
430 for_each_ap_queue(aq
, ac
) {
431 spin_lock_bh(&aq
->lock
);
432 wait
= min(wait
, ap_sm_event_loop(aq
, AP_EVENT_POLL
));
433 spin_unlock_bh(&aq
->lock
);
436 spin_unlock_bh(&ap_list_lock
);
441 static int ap_pending_requests(void)
446 spin_lock_bh(&ap_list_lock
);
447 for_each_ap_card(ac
) {
448 for_each_ap_queue(aq
, ac
) {
449 if (aq
->queue_count
== 0)
451 spin_unlock_bh(&ap_list_lock
);
455 spin_unlock_bh(&ap_list_lock
);
460 * ap_poll_thread(): Thread that polls for finished requests.
461 * @data: Unused pointer
463 * AP bus poll thread. The purpose of this thread is to poll for
464 * finished requests in a loop if there is a "free" cpu - that is
465 * a cpu that doesn't have anything better to do. The polling stops
466 * as soon as there is another task or if all messages have been
469 static int ap_poll_thread(void *data
)
471 DECLARE_WAITQUEUE(wait
, current
);
473 set_user_nice(current
, MAX_NICE
);
475 while (!kthread_should_stop()) {
476 add_wait_queue(&ap_poll_wait
, &wait
);
477 set_current_state(TASK_INTERRUPTIBLE
);
478 if (!ap_pending_requests()) {
482 set_current_state(TASK_RUNNING
);
483 remove_wait_queue(&ap_poll_wait
, &wait
);
484 if (need_resched()) {
495 static int ap_poll_thread_start(void)
499 if (ap_using_interrupts() || ap_poll_kthread
)
501 mutex_lock(&ap_poll_thread_mutex
);
502 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
503 rc
= PTR_ERR_OR_ZERO(ap_poll_kthread
);
505 ap_poll_kthread
= NULL
;
506 mutex_unlock(&ap_poll_thread_mutex
);
510 static void ap_poll_thread_stop(void)
512 if (!ap_poll_kthread
)
514 mutex_lock(&ap_poll_thread_mutex
);
515 kthread_stop(ap_poll_kthread
);
516 ap_poll_kthread
= NULL
;
517 mutex_unlock(&ap_poll_thread_mutex
);
520 #define is_card_dev(x) ((x)->parent == ap_root_device)
521 #define is_queue_dev(x) ((x)->parent != ap_root_device)
525 * @dev: Pointer to device
526 * @drv: Pointer to device_driver
528 * AP bus driver registration/unregistration.
530 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
532 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
533 struct ap_device_id
*id
;
536 * Compare device type of the device with the list of
537 * supported types of the device_driver.
539 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
540 if (is_card_dev(dev
) &&
541 id
->match_flags
& AP_DEVICE_ID_MATCH_CARD_TYPE
&&
542 id
->dev_type
== to_ap_dev(dev
)->device_type
)
544 if (is_queue_dev(dev
) &&
545 id
->match_flags
& AP_DEVICE_ID_MATCH_QUEUE_TYPE
&&
546 id
->dev_type
== to_ap_dev(dev
)->device_type
)
553 * ap_uevent(): Uevent function for AP devices.
554 * @dev: Pointer to device
555 * @env: Pointer to kobj_uevent_env
557 * It sets up a single environment variable DEV_TYPE which contains the
558 * hardware device type.
560 static int ap_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
562 struct ap_device
*ap_dev
= to_ap_dev(dev
);
568 /* Set up DEV_TYPE environment variable. */
569 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
574 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
579 static int __ap_queue_devices_with_id_unregister(struct device
*dev
, void *data
)
581 if (is_queue_dev(dev
) &&
582 AP_QID_CARD(to_ap_queue(dev
)->qid
) == (int)(long) data
)
583 device_unregister(dev
);
587 static struct bus_type ap_bus_type
= {
589 .match
= &ap_bus_match
,
590 .uevent
= &ap_uevent
,
593 static int __ap_revise_reserved(struct device
*dev
, void *dummy
)
595 int rc
, card
, queue
, devres
, drvres
;
597 if (is_queue_dev(dev
)) {
598 card
= AP_QID_CARD(to_ap_queue(dev
)->qid
);
599 queue
= AP_QID_QUEUE(to_ap_queue(dev
)->qid
);
600 mutex_lock(&ap_perms_mutex
);
601 devres
= test_bit_inv(card
, ap_perms
.apm
)
602 && test_bit_inv(queue
, ap_perms
.aqm
);
603 mutex_unlock(&ap_perms_mutex
);
604 drvres
= to_ap_drv(dev
->driver
)->flags
605 & AP_DRIVER_FLAG_DEFAULT
;
606 if (!!devres
!= !!drvres
) {
607 AP_DBF(DBF_DEBUG
, "reprobing queue=%02x.%04x\n",
609 rc
= device_reprobe(dev
);
616 static void ap_bus_revise_bindings(void)
618 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
, __ap_revise_reserved
);
621 int ap_owned_by_def_drv(int card
, int queue
)
625 if (card
< 0 || card
>= AP_DEVICES
|| queue
< 0 || queue
>= AP_DOMAINS
)
628 mutex_lock(&ap_perms_mutex
);
630 if (test_bit_inv(card
, ap_perms
.apm
)
631 && test_bit_inv(queue
, ap_perms
.aqm
))
634 mutex_unlock(&ap_perms_mutex
);
638 EXPORT_SYMBOL(ap_owned_by_def_drv
);
640 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm
,
643 int card
, queue
, rc
= 0;
645 mutex_lock(&ap_perms_mutex
);
647 for (card
= 0; !rc
&& card
< AP_DEVICES
; card
++)
648 if (test_bit_inv(card
, apm
) &&
649 test_bit_inv(card
, ap_perms
.apm
))
650 for (queue
= 0; !rc
&& queue
< AP_DOMAINS
; queue
++)
651 if (test_bit_inv(queue
, aqm
) &&
652 test_bit_inv(queue
, ap_perms
.aqm
))
655 mutex_unlock(&ap_perms_mutex
);
659 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv
);
661 static int ap_device_probe(struct device
*dev
)
663 struct ap_device
*ap_dev
= to_ap_dev(dev
);
664 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
665 int card
, queue
, devres
, drvres
, rc
;
667 if (is_queue_dev(dev
)) {
669 * If the apqn is marked as reserved/used by ap bus and
670 * default drivers, only probe with drivers with the default
671 * flag set. If it is not marked, only probe with drivers
672 * with the default flag not set.
674 card
= AP_QID_CARD(to_ap_queue(dev
)->qid
);
675 queue
= AP_QID_QUEUE(to_ap_queue(dev
)->qid
);
676 mutex_lock(&ap_perms_mutex
);
677 devres
= test_bit_inv(card
, ap_perms
.apm
)
678 && test_bit_inv(queue
, ap_perms
.aqm
);
679 mutex_unlock(&ap_perms_mutex
);
680 drvres
= ap_drv
->flags
& AP_DRIVER_FLAG_DEFAULT
;
681 if (!!devres
!= !!drvres
)
685 /* Add queue/card to list of active queues/cards */
686 spin_lock_bh(&ap_list_lock
);
687 if (is_card_dev(dev
))
688 list_add(&to_ap_card(dev
)->list
, &ap_card_list
);
690 list_add(&to_ap_queue(dev
)->list
,
691 &to_ap_queue(dev
)->card
->queues
);
692 spin_unlock_bh(&ap_list_lock
);
694 ap_dev
->drv
= ap_drv
;
695 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
698 spin_lock_bh(&ap_list_lock
);
699 if (is_card_dev(dev
))
700 list_del_init(&to_ap_card(dev
)->list
);
702 list_del_init(&to_ap_queue(dev
)->list
);
703 spin_unlock_bh(&ap_list_lock
);
710 static int ap_device_remove(struct device
*dev
)
712 struct ap_device
*ap_dev
= to_ap_dev(dev
);
713 struct ap_driver
*ap_drv
= ap_dev
->drv
;
715 /* prepare ap queue device removal */
716 if (is_queue_dev(dev
))
717 ap_queue_prepare_remove(to_ap_queue(dev
));
719 /* driver's chance to clean up gracefully */
721 ap_drv
->remove(ap_dev
);
723 /* now do the ap queue device remove */
724 if (is_queue_dev(dev
))
725 ap_queue_remove(to_ap_queue(dev
));
727 /* Remove queue/card from list of active queues/cards */
728 spin_lock_bh(&ap_list_lock
);
729 if (is_card_dev(dev
))
730 list_del_init(&to_ap_card(dev
)->list
);
732 list_del_init(&to_ap_queue(dev
)->list
);
733 spin_unlock_bh(&ap_list_lock
);
738 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
741 struct device_driver
*drv
= &ap_drv
->driver
;
746 drv
->bus
= &ap_bus_type
;
747 drv
->probe
= ap_device_probe
;
748 drv
->remove
= ap_device_remove
;
751 return driver_register(drv
);
753 EXPORT_SYMBOL(ap_driver_register
);
755 void ap_driver_unregister(struct ap_driver
*ap_drv
)
757 driver_unregister(&ap_drv
->driver
);
759 EXPORT_SYMBOL(ap_driver_unregister
);
761 void ap_bus_force_rescan(void)
763 /* processing a asynchronous bus rescan */
764 del_timer(&ap_config_timer
);
765 queue_work(system_long_wq
, &ap_scan_work
);
766 flush_work(&ap_scan_work
);
768 EXPORT_SYMBOL(ap_bus_force_rescan
);
771 * A config change has happened, force an ap bus rescan.
773 void ap_bus_cfg_chg(void)
775 AP_DBF(DBF_INFO
, "%s config change, forcing bus rescan\n", __func__
);
777 ap_bus_force_rescan();
781 * hex2bitmap() - parse hex mask string and set bitmap.
782 * Valid strings are "0x012345678" with at least one valid hex number.
783 * Rest of the bitmap to the right is padded with 0. No spaces allowed
784 * within the string, the leading 0x may be omitted.
785 * Returns the bitmask with exactly the bits set as given by the hex
786 * string (both in big endian order).
788 static int hex2bitmap(const char *str
, unsigned long *bitmap
, int bits
)
792 /* bits needs to be a multiple of 8 */
796 if (str
[0] == '0' && str
[1] == 'x')
801 for (i
= 0; isxdigit(*str
) && i
< bits
; str
++) {
802 b
= hex_to_bin(*str
);
803 for (n
= 0; n
< 4; n
++)
805 set_bit_inv(i
+ n
, bitmap
);
817 * modify_bitmap() - parse bitmask argument and modify an existing
818 * bit mask accordingly. A concatenation (done with ',') of these
819 * terms is recognized:
820 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
821 * <bitnr> may be any valid number (hex, decimal or octal) in the range
822 * 0...bits-1; the leading + or - is required. Here are some examples:
823 * +0-15,+32,-128,-0xFF
824 * -0-255,+1-16,+0x128
825 * +1,+2,+3,+4,-5,-7-10
826 * Returns the new bitmap after all changes have been applied. Every
827 * positive value in the string will set a bit and every negative value
828 * in the string will clear a bit. As a bit may be touched more than once,
829 * the last 'operation' wins:
830 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
831 * cleared again. All other bits are unmodified.
833 static int modify_bitmap(const char *str
, unsigned long *bitmap
, int bits
)
838 /* bits needs to be a multiple of 8 */
844 if (sign
!= '+' && sign
!= '-')
846 a
= z
= simple_strtoul(str
, &np
, 0);
847 if (str
== np
|| a
>= bits
)
851 z
= simple_strtoul(++str
, &np
, 0);
852 if (str
== np
|| a
> z
|| z
>= bits
)
856 for (i
= a
; i
<= z
; i
++)
858 set_bit_inv(i
, bitmap
);
860 clear_bit_inv(i
, bitmap
);
861 while (*str
== ',' || *str
== '\n')
868 int ap_parse_mask_str(const char *str
,
869 unsigned long *bitmap
, int bits
,
872 unsigned long *newmap
, size
;
875 /* bits needs to be a multiple of 8 */
879 size
= BITS_TO_LONGS(bits
)*sizeof(unsigned long);
880 newmap
= kmalloc(size
, GFP_KERNEL
);
883 if (mutex_lock_interruptible(lock
)) {
888 if (*str
== '+' || *str
== '-') {
889 memcpy(newmap
, bitmap
, size
);
890 rc
= modify_bitmap(str
, newmap
, bits
);
892 memset(newmap
, 0, size
);
893 rc
= hex2bitmap(str
, newmap
, bits
);
896 memcpy(bitmap
, newmap
, size
);
901 EXPORT_SYMBOL(ap_parse_mask_str
);
907 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
909 return scnprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
912 static ssize_t
ap_domain_store(struct bus_type
*bus
,
913 const char *buf
, size_t count
)
917 if (sscanf(buf
, "%i\n", &domain
) != 1 ||
918 domain
< 0 || domain
> ap_max_domain_id
||
919 !test_bit_inv(domain
, ap_perms
.aqm
))
921 spin_lock_bh(&ap_domain_lock
);
922 ap_domain_index
= domain
;
923 spin_unlock_bh(&ap_domain_lock
);
925 AP_DBF(DBF_DEBUG
, "stored new default domain=%d\n", domain
);
930 static BUS_ATTR_RW(ap_domain
);
932 static ssize_t
ap_control_domain_mask_show(struct bus_type
*bus
, char *buf
)
934 if (!ap_configuration
) /* QCI not supported */
935 return scnprintf(buf
, PAGE_SIZE
, "not supported\n");
937 return scnprintf(buf
, PAGE_SIZE
,
938 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
939 ap_configuration
->adm
[0], ap_configuration
->adm
[1],
940 ap_configuration
->adm
[2], ap_configuration
->adm
[3],
941 ap_configuration
->adm
[4], ap_configuration
->adm
[5],
942 ap_configuration
->adm
[6], ap_configuration
->adm
[7]);
945 static BUS_ATTR_RO(ap_control_domain_mask
);
947 static ssize_t
ap_usage_domain_mask_show(struct bus_type
*bus
, char *buf
)
949 if (!ap_configuration
) /* QCI not supported */
950 return scnprintf(buf
, PAGE_SIZE
, "not supported\n");
952 return scnprintf(buf
, PAGE_SIZE
,
953 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
954 ap_configuration
->aqm
[0], ap_configuration
->aqm
[1],
955 ap_configuration
->aqm
[2], ap_configuration
->aqm
[3],
956 ap_configuration
->aqm
[4], ap_configuration
->aqm
[5],
957 ap_configuration
->aqm
[6], ap_configuration
->aqm
[7]);
960 static BUS_ATTR_RO(ap_usage_domain_mask
);
962 static ssize_t
ap_adapter_mask_show(struct bus_type
*bus
, char *buf
)
964 if (!ap_configuration
) /* QCI not supported */
965 return scnprintf(buf
, PAGE_SIZE
, "not supported\n");
967 return scnprintf(buf
, PAGE_SIZE
,
968 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
969 ap_configuration
->apm
[0], ap_configuration
->apm
[1],
970 ap_configuration
->apm
[2], ap_configuration
->apm
[3],
971 ap_configuration
->apm
[4], ap_configuration
->apm
[5],
972 ap_configuration
->apm
[6], ap_configuration
->apm
[7]);
975 static BUS_ATTR_RO(ap_adapter_mask
);
977 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
979 return scnprintf(buf
, PAGE_SIZE
, "%d\n",
980 ap_using_interrupts() ? 1 : 0);
983 static BUS_ATTR_RO(ap_interrupts
);
985 static ssize_t
config_time_show(struct bus_type
*bus
, char *buf
)
987 return scnprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
990 static ssize_t
config_time_store(struct bus_type
*bus
,
991 const char *buf
, size_t count
)
995 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
997 ap_config_time
= time
;
998 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1002 static BUS_ATTR_RW(config_time
);
1004 static ssize_t
poll_thread_show(struct bus_type
*bus
, char *buf
)
1006 return scnprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
1009 static ssize_t
poll_thread_store(struct bus_type
*bus
,
1010 const char *buf
, size_t count
)
1014 if (sscanf(buf
, "%d\n", &flag
) != 1)
1017 rc
= ap_poll_thread_start();
1021 ap_poll_thread_stop();
1025 static BUS_ATTR_RW(poll_thread
);
1027 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
1029 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
1032 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
1035 unsigned long long time
;
1038 /* 120 seconds = maximum poll interval */
1039 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
1040 time
> 120000000000ULL)
1042 poll_timeout
= time
;
1043 hr_time
= poll_timeout
;
1045 spin_lock_bh(&ap_poll_timer_lock
);
1046 hrtimer_cancel(&ap_poll_timer
);
1047 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
1048 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
1049 spin_unlock_bh(&ap_poll_timer_lock
);
1054 static BUS_ATTR_RW(poll_timeout
);
1056 static ssize_t
ap_max_domain_id_show(struct bus_type
*bus
, char *buf
)
1060 if (ap_configuration
)
1061 max_domain_id
= ap_max_domain_id
? : -1;
1064 return scnprintf(buf
, PAGE_SIZE
, "%d\n", max_domain_id
);
1067 static BUS_ATTR_RO(ap_max_domain_id
);
1069 static ssize_t
apmask_show(struct bus_type
*bus
, char *buf
)
1073 if (mutex_lock_interruptible(&ap_perms_mutex
))
1074 return -ERESTARTSYS
;
1075 rc
= scnprintf(buf
, PAGE_SIZE
,
1076 "0x%016lx%016lx%016lx%016lx\n",
1077 ap_perms
.apm
[0], ap_perms
.apm
[1],
1078 ap_perms
.apm
[2], ap_perms
.apm
[3]);
1079 mutex_unlock(&ap_perms_mutex
);
1084 static ssize_t
apmask_store(struct bus_type
*bus
, const char *buf
,
1089 rc
= ap_parse_mask_str(buf
, ap_perms
.apm
, AP_DEVICES
, &ap_perms_mutex
);
1093 ap_bus_revise_bindings();
1098 static BUS_ATTR_RW(apmask
);
1100 static ssize_t
aqmask_show(struct bus_type
*bus
, char *buf
)
1104 if (mutex_lock_interruptible(&ap_perms_mutex
))
1105 return -ERESTARTSYS
;
1106 rc
= scnprintf(buf
, PAGE_SIZE
,
1107 "0x%016lx%016lx%016lx%016lx\n",
1108 ap_perms
.aqm
[0], ap_perms
.aqm
[1],
1109 ap_perms
.aqm
[2], ap_perms
.aqm
[3]);
1110 mutex_unlock(&ap_perms_mutex
);
1115 static ssize_t
aqmask_store(struct bus_type
*bus
, const char *buf
,
1120 rc
= ap_parse_mask_str(buf
, ap_perms
.aqm
, AP_DOMAINS
, &ap_perms_mutex
);
1124 ap_bus_revise_bindings();
1129 static BUS_ATTR_RW(aqmask
);
1131 static struct bus_attribute
*const ap_bus_attrs
[] = {
1132 &bus_attr_ap_domain
,
1133 &bus_attr_ap_control_domain_mask
,
1134 &bus_attr_ap_usage_domain_mask
,
1135 &bus_attr_ap_adapter_mask
,
1136 &bus_attr_config_time
,
1137 &bus_attr_poll_thread
,
1138 &bus_attr_ap_interrupts
,
1139 &bus_attr_poll_timeout
,
1140 &bus_attr_ap_max_domain_id
,
1147 * ap_select_domain(): Select an AP domain if possible and we haven't
1148 * already done so before.
1150 static void ap_select_domain(void)
1152 int count
, max_count
, best_domain
;
1153 struct ap_queue_status status
;
1157 * We want to use a single domain. Either the one specified with
1158 * the "domain=" parameter or the domain with the maximum number
1161 spin_lock_bh(&ap_domain_lock
);
1162 if (ap_domain_index
>= 0) {
1163 /* Domain has already been selected. */
1164 spin_unlock_bh(&ap_domain_lock
);
1169 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1170 if (!ap_test_config_usage_domain(i
) ||
1171 !test_bit_inv(i
, ap_perms
.aqm
))
1174 for (j
= 0; j
< AP_DEVICES
; j
++) {
1175 if (!ap_test_config_card_id(j
))
1177 status
= ap_test_queue(AP_MKQID(j
, i
),
1178 ap_apft_available(),
1180 if (status
.response_code
!= AP_RESPONSE_NORMAL
)
1184 if (count
> max_count
) {
1189 if (best_domain
>= 0) {
1190 ap_domain_index
= best_domain
;
1191 AP_DBF(DBF_DEBUG
, "new ap_domain_index=%d\n", ap_domain_index
);
1193 spin_unlock_bh(&ap_domain_lock
);
1197 * This function checks the type and returns either 0 for not
1198 * supported or the highest compatible type value (which may
1199 * include the input type value).
1201 static int ap_get_compatible_type(ap_qid_t qid
, int rawtype
, unsigned int func
)
1205 /* < CEX2A is not supported */
1206 if (rawtype
< AP_DEVICE_TYPE_CEX2A
)
1208 /* up to CEX7 known and fully supported */
1209 if (rawtype
<= AP_DEVICE_TYPE_CEX7
)
1212 * unknown new type > CEX7, check for compatibility
1213 * to the highest known and supported type which is
1214 * currently CEX7 with the help of the QACT function.
1216 if (ap_qact_available()) {
1217 struct ap_queue_status status
;
1218 union ap_qact_ap_info apinfo
= {0};
1220 apinfo
.mode
= (func
>> 26) & 0x07;
1221 apinfo
.cat
= AP_DEVICE_TYPE_CEX7
;
1222 status
= ap_qact(qid
, 0, &apinfo
);
1223 if (status
.response_code
== AP_RESPONSE_NORMAL
1224 && apinfo
.cat
>= AP_DEVICE_TYPE_CEX2A
1225 && apinfo
.cat
<= AP_DEVICE_TYPE_CEX7
)
1226 comp_type
= apinfo
.cat
;
1229 AP_DBF(DBF_WARN
, "queue=%02x.%04x unable to map type %d\n",
1230 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
), rawtype
);
1231 else if (comp_type
!= rawtype
)
1232 AP_DBF(DBF_INFO
, "queue=%02x.%04x map type %d to %d\n",
1233 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
), rawtype
, comp_type
);
1238 * Helper function to be used with bus_find_dev
1239 * matches for the card device with the given id
1241 static int __match_card_device_with_id(struct device
*dev
, const void *data
)
1243 return is_card_dev(dev
) && to_ap_card(dev
)->id
== (int)(long)(void *) data
;
1247 * Helper function to be used with bus_find_dev
1248 * matches for the queue device with a given qid
1250 static int __match_queue_device_with_qid(struct device
*dev
, const void *data
)
1252 return is_queue_dev(dev
) && to_ap_queue(dev
)->qid
== (int)(long) data
;
1256 * Helper function to be used with bus_find_dev
1257 * matches any queue device with given queue id
1259 static int __match_queue_device_with_queue_id(struct device
*dev
, const void *data
)
1261 return is_queue_dev(dev
)
1262 && AP_QID_QUEUE(to_ap_queue(dev
)->qid
) == (int)(long) data
;
1266 * Helper function for ap_scan_bus().
1267 * Does the scan bus job for the given adapter id.
1269 static void _ap_scan_bus_adapter(int id
)
1275 struct ap_queue
*aq
;
1276 int rc
, dom
, depth
, type
, comp_type
, borked
;
1278 /* check if there is a card device registered with this id */
1279 dev
= bus_find_device(&ap_bus_type
, NULL
,
1281 __match_card_device_with_id
);
1282 ac
= dev
? to_ap_card(dev
) : NULL
;
1283 if (!ap_test_config_card_id(id
)) {
1285 /* Card device has been removed from configuration */
1286 bus_for_each_dev(&ap_bus_type
, NULL
,
1288 __ap_queue_devices_with_id_unregister
);
1289 device_unregister(dev
);
1296 * This card id is enabled in the configuration. If we already have
1297 * a card device with this id, check if type and functions are still
1298 * the very same. Also verify that at least one queue is available.
1301 /* find the first valid queue */
1302 for (dom
= 0; dom
< AP_DOMAINS
; dom
++) {
1303 qid
= AP_MKQID(id
, dom
);
1304 if (ap_query_queue(qid
, &depth
, &type
, &func
) == 0)
1308 if (dom
>= AP_DOMAINS
) {
1309 /* no accessible queue on this card */
1311 } else if (ac
->raw_hwtype
!= type
) {
1312 /* card type has changed */
1313 AP_DBF(DBF_INFO
, "card=%02x type changed.\n", id
);
1315 } else if (ac
->functions
!= func
) {
1316 /* card functions have changed */
1317 AP_DBF(DBF_INFO
, "card=%02x functions changed.\n", id
);
1321 /* unregister card device and associated queues */
1322 bus_for_each_dev(&ap_bus_type
, NULL
,
1324 __ap_queue_devices_with_id_unregister
);
1325 device_unregister(dev
);
1327 /* go back if there is no valid queue on this card */
1328 if (dom
>= AP_DOMAINS
)
1335 * Go through all possible queue ids. Check and maybe create or release
1336 * queue devices for this card. If there exists no card device yet,
1337 * create a card device also.
1339 for (dom
= 0; dom
< AP_DOMAINS
; dom
++) {
1340 qid
= AP_MKQID(id
, dom
);
1341 dev
= bus_find_device(&ap_bus_type
, NULL
,
1343 __match_queue_device_with_qid
);
1344 aq
= dev
? to_ap_queue(dev
) : NULL
;
1345 if (!ap_test_config_usage_domain(dom
)) {
1347 /* Queue device exists but has been
1348 * removed from configuration.
1350 device_unregister(dev
);
1355 /* try to fetch infos about this queue */
1356 rc
= ap_query_queue(qid
, &depth
, &type
, &func
);
1361 spin_lock_bh(&aq
->lock
);
1362 borked
= aq
->state
== AP_STATE_BORKED
;
1363 spin_unlock_bh(&aq
->lock
);
1366 /* Remove broken device */
1368 "removing broken queue=%02x.%04x\n",
1370 device_unregister(dev
);
1377 /* a new queue device is needed, check out comp type */
1378 comp_type
= ap_get_compatible_type(qid
, type
, func
);
1381 /* maybe a card device needs to be created first */
1383 ac
= ap_card_create(id
, depth
, type
, comp_type
, func
);
1386 ac
->ap_dev
.device
.bus
= &ap_bus_type
;
1387 ac
->ap_dev
.device
.parent
= ap_root_device
;
1388 dev_set_name(&ac
->ap_dev
.device
, "card%02x", id
);
1389 /* Register card device with AP bus */
1390 rc
= device_register(&ac
->ap_dev
.device
);
1392 put_device(&ac
->ap_dev
.device
);
1396 /* get it and thus adjust reference counter */
1397 get_device(&ac
->ap_dev
.device
);
1399 /* now create the new queue device */
1400 aq
= ap_queue_create(qid
, comp_type
);
1404 aq
->ap_dev
.device
.bus
= &ap_bus_type
;
1405 aq
->ap_dev
.device
.parent
= &ac
->ap_dev
.device
;
1406 dev_set_name(&aq
->ap_dev
.device
, "%02x.%04x", id
, dom
);
1407 /* Register queue device */
1408 rc
= device_register(&aq
->ap_dev
.device
);
1410 put_device(&aq
->ap_dev
.device
);
1413 } /* end domain loop */
1416 put_device(&ac
->ap_dev
.device
);
1420 * ap_scan_bus(): Scan the AP bus for new devices
1421 * Runs periodically, workqueue timer (ap_config_time)
1423 static void ap_scan_bus(struct work_struct
*unused
)
1427 AP_DBF(DBF_DEBUG
, "%s running\n", __func__
);
1429 ap_query_configuration(ap_configuration
);
1432 /* loop over all possible adapters */
1433 for (id
= 0; id
< AP_DEVICES
; id
++)
1434 _ap_scan_bus_adapter(id
);
1436 /* check if there is at least one queue available with default domain */
1437 if (ap_domain_index
>= 0) {
1438 struct device
*dev
=
1439 bus_find_device(&ap_bus_type
, NULL
,
1440 (void *)(long) ap_domain_index
,
1441 __match_queue_device_with_queue_id
);
1446 "no queue device with default domain %d available\n",
1450 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1453 static void ap_config_timeout(struct timer_list
*unused
)
1455 queue_work(system_long_wq
, &ap_scan_work
);
1458 static int __init
ap_debug_init(void)
1460 ap_dbf_info
= debug_register("ap", 1, 1,
1461 DBF_MAX_SPRINTF_ARGS
* sizeof(long));
1462 debug_register_view(ap_dbf_info
, &debug_sprintf_view
);
1463 debug_set_level(ap_dbf_info
, DBF_ERR
);
1468 static void __init
ap_perms_init(void)
1470 /* all resources useable if no kernel parameter string given */
1471 memset(&ap_perms
.ioctlm
, 0xFF, sizeof(ap_perms
.ioctlm
));
1472 memset(&ap_perms
.apm
, 0xFF, sizeof(ap_perms
.apm
));
1473 memset(&ap_perms
.aqm
, 0xFF, sizeof(ap_perms
.aqm
));
1475 /* apm kernel parameter string */
1477 memset(&ap_perms
.apm
, 0, sizeof(ap_perms
.apm
));
1478 ap_parse_mask_str(apm_str
, ap_perms
.apm
, AP_DEVICES
,
1482 /* aqm kernel parameter string */
1484 memset(&ap_perms
.aqm
, 0, sizeof(ap_perms
.aqm
));
1485 ap_parse_mask_str(aqm_str
, ap_perms
.aqm
, AP_DOMAINS
,
1491 * ap_module_init(): The module initialization code.
1493 * Initializes the module.
1495 static int __init
ap_module_init(void)
1500 rc
= ap_debug_init();
1504 if (!ap_instructions_available()) {
1505 pr_warn("The hardware system does not support AP instructions\n");
1509 /* set up the AP permissions (ioctls, ap and aq masks) */
1512 /* Get AP configuration data if available */
1513 ap_init_configuration();
1515 if (ap_configuration
)
1517 ap_max_domain_id
? ap_max_domain_id
: AP_DOMAINS
- 1;
1520 if (ap_domain_index
< -1 || ap_domain_index
> max_domain_id
||
1521 (ap_domain_index
>= 0 &&
1522 !test_bit_inv(ap_domain_index
, ap_perms
.aqm
))) {
1523 pr_warn("%d is not a valid cryptographic domain\n",
1525 ap_domain_index
= -1;
1528 if (ap_interrupts_available()) {
1529 rc
= register_adapter_interrupt(&ap_airq
);
1530 ap_airq_flag
= (rc
== 0);
1533 /* Create /sys/bus/ap. */
1534 rc
= bus_register(&ap_bus_type
);
1537 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1538 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1543 /* Create /sys/devices/ap. */
1544 ap_root_device
= root_device_register("ap");
1545 rc
= PTR_ERR_OR_ZERO(ap_root_device
);
1549 /* Setup the AP bus rescan timer. */
1550 timer_setup(&ap_config_timer
, ap_config_timeout
, 0);
1553 * Setup the high resultion poll timer.
1554 * If we are running under z/VM adjust polling to z/VM polling rate.
1557 poll_timeout
= 1500000;
1558 spin_lock_init(&ap_poll_timer_lock
);
1559 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1560 ap_poll_timer
.function
= ap_poll_timeout
;
1562 /* Start the low priority AP bus poll thread. */
1563 if (ap_thread_flag
) {
1564 rc
= ap_poll_thread_start();
1569 queue_work(system_long_wq
, &ap_scan_work
);
1575 hrtimer_cancel(&ap_poll_timer
);
1576 root_device_unregister(ap_root_device
);
1579 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1580 bus_unregister(&ap_bus_type
);
1582 if (ap_using_interrupts())
1583 unregister_adapter_interrupt(&ap_airq
);
1584 kfree(ap_configuration
);
1587 device_initcall(ap_module_init
);