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/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 (card and domain masking) */
69 static struct ap_perms
{
70 DECLARE_BITMAP(apm
, AP_DEVICES
);
71 DECLARE_BITMAP(aqm
, AP_DOMAINS
);
73 static DEFINE_MUTEX(ap_perms_mutex
);
75 static struct ap_config_info
*ap_configuration
;
76 static bool initialised
;
79 * AP bus related debug feature things.
81 debug_info_t
*ap_dbf_info
;
84 * Workqueue timer for bus rescan.
86 static struct timer_list ap_config_timer
;
87 static int ap_config_time
= AP_CONFIG_TIME
;
88 static void ap_scan_bus(struct work_struct
*);
89 static DECLARE_WORK(ap_scan_work
, ap_scan_bus
);
92 * Tasklet & timer for AP request polling and interrupts
94 static void ap_tasklet_fn(unsigned long);
95 static DECLARE_TASKLET(ap_tasklet
, ap_tasklet_fn
, 0);
96 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
97 static struct task_struct
*ap_poll_kthread
;
98 static DEFINE_MUTEX(ap_poll_thread_mutex
);
99 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
100 static struct hrtimer ap_poll_timer
;
102 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
103 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
105 static unsigned long long poll_timeout
= 250000;
108 static int ap_suspend_flag
;
109 /* Maximum domain id */
110 static int ap_max_domain_id
;
112 * Flag to check if domain was set through module parameter domain=. This is
113 * important when supsend and resume is done in a z/VM environment where the
114 * domain might change.
116 static int user_set_domain
;
117 static struct bus_type ap_bus_type
;
119 /* Adapter interrupt definitions */
120 static void ap_interrupt_handler(struct airq_struct
*airq
);
122 static int ap_airq_flag
;
124 static struct airq_struct ap_airq
= {
125 .handler
= ap_interrupt_handler
,
130 * ap_using_interrupts() - Returns non-zero if interrupt support is
133 static inline int ap_using_interrupts(void)
139 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
141 * Returns the address of the local-summary-indicator of the adapter
142 * interrupt handler for AP, or NULL if adapter interrupts are not
145 void *ap_airq_ptr(void)
147 if (ap_using_interrupts())
148 return ap_airq
.lsi_ptr
;
153 * ap_interrupts_available(): Test if AP interrupts are available.
155 * Returns 1 if AP interrupts are available.
157 static int ap_interrupts_available(void)
159 return test_facility(65);
163 * ap_configuration_available(): Test if AP configuration
164 * information is available.
166 * Returns 1 if AP configuration information is available.
168 static int ap_configuration_available(void)
170 return test_facility(12);
174 * ap_apft_available(): Test if AP facilities test (APFT)
175 * facility is available.
177 * Returns 1 if APFT is is available.
179 static int ap_apft_available(void)
181 return test_facility(15);
185 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
187 * Returns 1 if the QACT subfunction is available.
189 static inline int ap_qact_available(void)
191 if (ap_configuration
)
192 return ap_configuration
->qact
;
197 * ap_query_configuration(): Fetch cryptographic config info
199 * Returns the ap configuration info fetched via PQAP(QCI).
200 * On success 0 is returned, on failure a negative errno
201 * is returned, e.g. if the PQAP(QCI) instruction is not
202 * available, the return value will be -EOPNOTSUPP.
204 static inline int ap_query_configuration(struct ap_config_info
*info
)
206 if (!ap_configuration_available())
212 EXPORT_SYMBOL(ap_query_configuration
);
215 * ap_init_configuration(): Allocate and query configuration array.
217 static void ap_init_configuration(void)
219 if (!ap_configuration_available())
222 ap_configuration
= kzalloc(sizeof(*ap_configuration
), GFP_KERNEL
);
223 if (!ap_configuration
)
225 if (ap_query_configuration(ap_configuration
) != 0) {
226 kfree(ap_configuration
);
227 ap_configuration
= NULL
;
233 * ap_test_config(): helper function to extract the nrth bit
234 * within the unsigned int array field.
236 static inline int ap_test_config(unsigned int *field
, unsigned int nr
)
238 return ap_test_bit((field
+ (nr
>> 5)), (nr
& 0x1f));
242 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
245 * Returns 0 if the card is not configured
246 * 1 if the card is configured or
247 * if the configuration information is not available
249 static inline int ap_test_config_card_id(unsigned int id
)
251 if (!ap_configuration
) /* QCI not supported */
252 /* only ids 0...3F may be probed */
253 return id
< 0x40 ? 1 : 0;
254 return ap_test_config(ap_configuration
->apm
, id
);
258 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
259 * @domain AP usage domain ID
261 * Returns 0 if the usage domain is not configured
262 * 1 if the usage domain is configured or
263 * if the configuration information is not available
265 static inline int ap_test_config_domain(unsigned int domain
)
267 if (!ap_configuration
) /* QCI not supported */
269 return ap_test_config(ap_configuration
->aqm
, domain
);
273 * ap_query_queue(): Check if an AP queue is available.
274 * @qid: The AP queue number
275 * @queue_depth: Pointer to queue depth value
276 * @device_type: Pointer to device type value
277 * @facilities: Pointer to facility indicator
279 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
,
280 unsigned int *facilities
)
282 struct ap_queue_status status
;
286 if (!ap_test_config_card_id(AP_QID_CARD(qid
)))
289 status
= ap_test_queue(qid
, ap_apft_available(), &info
);
290 switch (status
.response_code
) {
291 case AP_RESPONSE_NORMAL
:
292 *queue_depth
= (int)(info
& 0xff);
293 *device_type
= (int)((info
>> 24) & 0xff);
294 *facilities
= (unsigned int)(info
>> 32);
295 /* Update maximum domain id */
296 nd
= (info
>> 16) & 0xff;
297 /* if N bit is available, z13 and newer */
298 if ((info
& (1UL << 57)) && nd
> 0)
299 ap_max_domain_id
= nd
;
300 else /* older machine types */
301 ap_max_domain_id
= 15;
302 switch (*device_type
) {
303 /* For CEX2 and CEX3 the available functions
304 * are not refrected by the facilities bits.
305 * Instead it is coded into the type. So here
306 * modify the function bits based on the type.
308 case AP_DEVICE_TYPE_CEX2A
:
309 case AP_DEVICE_TYPE_CEX3A
:
310 *facilities
|= 0x08000000;
312 case AP_DEVICE_TYPE_CEX2C
:
313 case AP_DEVICE_TYPE_CEX3C
:
314 *facilities
|= 0x10000000;
320 case AP_RESPONSE_Q_NOT_AVAIL
:
321 case AP_RESPONSE_DECONFIGURED
:
322 case AP_RESPONSE_CHECKSTOPPED
:
323 case AP_RESPONSE_INVALID_ADDRESS
:
325 case AP_RESPONSE_RESET_IN_PROGRESS
:
326 case AP_RESPONSE_OTHERWISE_CHANGED
:
327 case AP_RESPONSE_BUSY
:
334 void ap_wait(enum ap_wait wait
)
340 case AP_WAIT_INTERRUPT
:
341 if (ap_using_interrupts())
343 if (ap_poll_kthread
) {
344 wake_up(&ap_poll_wait
);
348 case AP_WAIT_TIMEOUT
:
349 spin_lock_bh(&ap_poll_timer_lock
);
350 if (!hrtimer_is_queued(&ap_poll_timer
)) {
351 hr_time
= poll_timeout
;
352 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
353 hrtimer_restart(&ap_poll_timer
);
355 spin_unlock_bh(&ap_poll_timer_lock
);
364 * ap_request_timeout(): Handling of request timeouts
365 * @t: timer making this callback
367 * Handles request timeouts.
369 void ap_request_timeout(struct timer_list
*t
)
371 struct ap_queue
*aq
= from_timer(aq
, t
, timeout
);
375 spin_lock_bh(&aq
->lock
);
376 ap_wait(ap_sm_event(aq
, AP_EVENT_TIMEOUT
));
377 spin_unlock_bh(&aq
->lock
);
381 * ap_poll_timeout(): AP receive polling for finished AP requests.
382 * @unused: Unused pointer.
384 * Schedules the AP tasklet using a high resolution timer.
386 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
388 if (!ap_suspend_flag
)
389 tasklet_schedule(&ap_tasklet
);
390 return HRTIMER_NORESTART
;
394 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
395 * @airq: pointer to adapter interrupt descriptor
397 static void ap_interrupt_handler(struct airq_struct
*airq
)
399 inc_irq_stat(IRQIO_APB
);
400 if (!ap_suspend_flag
)
401 tasklet_schedule(&ap_tasklet
);
405 * ap_tasklet_fn(): Tasklet to poll all AP devices.
406 * @dummy: Unused variable
408 * Poll all AP devices on the bus.
410 static void ap_tasklet_fn(unsigned long dummy
)
414 enum ap_wait wait
= AP_WAIT_NONE
;
416 /* Reset the indicator if interrupts are used. Thus new interrupts can
417 * be received. Doing it in the beginning of the tasklet is therefor
418 * important that no requests on any AP get lost.
420 if (ap_using_interrupts())
421 xchg(ap_airq
.lsi_ptr
, 0);
423 spin_lock_bh(&ap_list_lock
);
424 for_each_ap_card(ac
) {
425 for_each_ap_queue(aq
, ac
) {
426 spin_lock_bh(&aq
->lock
);
427 wait
= min(wait
, ap_sm_event_loop(aq
, AP_EVENT_POLL
));
428 spin_unlock_bh(&aq
->lock
);
431 spin_unlock_bh(&ap_list_lock
);
436 static int ap_pending_requests(void)
441 spin_lock_bh(&ap_list_lock
);
442 for_each_ap_card(ac
) {
443 for_each_ap_queue(aq
, ac
) {
444 if (aq
->queue_count
== 0)
446 spin_unlock_bh(&ap_list_lock
);
450 spin_unlock_bh(&ap_list_lock
);
455 * ap_poll_thread(): Thread that polls for finished requests.
456 * @data: Unused pointer
458 * AP bus poll thread. The purpose of this thread is to poll for
459 * finished requests in a loop if there is a "free" cpu - that is
460 * a cpu that doesn't have anything better to do. The polling stops
461 * as soon as there is another task or if all messages have been
464 static int ap_poll_thread(void *data
)
466 DECLARE_WAITQUEUE(wait
, current
);
468 set_user_nice(current
, MAX_NICE
);
470 while (!kthread_should_stop()) {
471 add_wait_queue(&ap_poll_wait
, &wait
);
472 set_current_state(TASK_INTERRUPTIBLE
);
473 if (ap_suspend_flag
|| !ap_pending_requests()) {
477 set_current_state(TASK_RUNNING
);
478 remove_wait_queue(&ap_poll_wait
, &wait
);
479 if (need_resched()) {
490 static int ap_poll_thread_start(void)
494 if (ap_using_interrupts() || ap_poll_kthread
)
496 mutex_lock(&ap_poll_thread_mutex
);
497 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
498 rc
= PTR_ERR_OR_ZERO(ap_poll_kthread
);
500 ap_poll_kthread
= NULL
;
501 mutex_unlock(&ap_poll_thread_mutex
);
505 static void ap_poll_thread_stop(void)
507 if (!ap_poll_kthread
)
509 mutex_lock(&ap_poll_thread_mutex
);
510 kthread_stop(ap_poll_kthread
);
511 ap_poll_kthread
= NULL
;
512 mutex_unlock(&ap_poll_thread_mutex
);
515 #define is_card_dev(x) ((x)->parent == ap_root_device)
516 #define is_queue_dev(x) ((x)->parent != ap_root_device)
520 * @dev: Pointer to device
521 * @drv: Pointer to device_driver
523 * AP bus driver registration/unregistration.
525 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
527 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
528 struct ap_device_id
*id
;
531 * Compare device type of the device with the list of
532 * supported types of the device_driver.
534 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
535 if (is_card_dev(dev
) &&
536 id
->match_flags
& AP_DEVICE_ID_MATCH_CARD_TYPE
&&
537 id
->dev_type
== to_ap_dev(dev
)->device_type
)
539 if (is_queue_dev(dev
) &&
540 id
->match_flags
& AP_DEVICE_ID_MATCH_QUEUE_TYPE
&&
541 id
->dev_type
== to_ap_dev(dev
)->device_type
)
548 * ap_uevent(): Uevent function for AP devices.
549 * @dev: Pointer to device
550 * @env: Pointer to kobj_uevent_env
552 * It sets up a single environment variable DEV_TYPE which contains the
553 * hardware device type.
555 static int ap_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
557 struct ap_device
*ap_dev
= to_ap_dev(dev
);
563 /* Set up DEV_TYPE environment variable. */
564 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
569 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
574 static int ap_dev_suspend(struct device
*dev
)
576 struct ap_device
*ap_dev
= to_ap_dev(dev
);
578 if (ap_dev
->drv
&& ap_dev
->drv
->suspend
)
579 ap_dev
->drv
->suspend(ap_dev
);
583 static int ap_dev_resume(struct device
*dev
)
585 struct ap_device
*ap_dev
= to_ap_dev(dev
);
587 if (ap_dev
->drv
&& ap_dev
->drv
->resume
)
588 ap_dev
->drv
->resume(ap_dev
);
592 static void ap_bus_suspend(void)
594 AP_DBF(DBF_DEBUG
, "%s running\n", __func__
);
598 * Disable scanning for devices, thus we do not want to scan
599 * for them after removing.
601 flush_work(&ap_scan_work
);
602 tasklet_disable(&ap_tasklet
);
605 static int __ap_card_devices_unregister(struct device
*dev
, void *dummy
)
607 if (is_card_dev(dev
))
608 device_unregister(dev
);
612 static int __ap_queue_devices_unregister(struct device
*dev
, void *dummy
)
614 if (is_queue_dev(dev
))
615 device_unregister(dev
);
619 static int __ap_queue_devices_with_id_unregister(struct device
*dev
, void *data
)
621 if (is_queue_dev(dev
) &&
622 AP_QID_CARD(to_ap_queue(dev
)->qid
) == (int)(long) data
)
623 device_unregister(dev
);
627 static void ap_bus_resume(void)
631 AP_DBF(DBF_DEBUG
, "%s running\n", __func__
);
633 /* remove all queue devices */
634 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
635 __ap_queue_devices_unregister
);
636 /* remove all card devices */
637 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
638 __ap_card_devices_unregister
);
640 /* Reset thin interrupt setting */
641 if (ap_interrupts_available() && !ap_using_interrupts()) {
642 rc
= register_adapter_interrupt(&ap_airq
);
643 ap_airq_flag
= (rc
== 0);
645 if (!ap_interrupts_available() && ap_using_interrupts()) {
646 unregister_adapter_interrupt(&ap_airq
);
650 if (!user_set_domain
)
651 ap_domain_index
= -1;
652 /* Get things going again */
655 xchg(ap_airq
.lsi_ptr
, 0);
656 tasklet_enable(&ap_tasklet
);
657 queue_work(system_long_wq
, &ap_scan_work
);
660 static int ap_power_event(struct notifier_block
*this, unsigned long event
,
664 case PM_HIBERNATION_PREPARE
:
665 case PM_SUSPEND_PREPARE
:
668 case PM_POST_HIBERNATION
:
669 case PM_POST_SUSPEND
:
677 static struct notifier_block ap_power_notifier
= {
678 .notifier_call
= ap_power_event
,
681 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops
, ap_dev_suspend
, ap_dev_resume
);
683 static struct bus_type ap_bus_type
= {
685 .match
= &ap_bus_match
,
686 .uevent
= &ap_uevent
,
687 .pm
= &ap_bus_pm_ops
,
690 static int __ap_revise_reserved(struct device
*dev
, void *dummy
)
692 int rc
, card
, queue
, devres
, drvres
;
694 if (is_queue_dev(dev
)) {
695 card
= AP_QID_CARD(to_ap_queue(dev
)->qid
);
696 queue
= AP_QID_QUEUE(to_ap_queue(dev
)->qid
);
697 mutex_lock(&ap_perms_mutex
);
698 devres
= test_bit_inv(card
, ap_perms
.apm
)
699 && test_bit_inv(queue
, ap_perms
.aqm
);
700 mutex_unlock(&ap_perms_mutex
);
701 drvres
= to_ap_drv(dev
->driver
)->flags
702 & AP_DRIVER_FLAG_DEFAULT
;
703 if (!!devres
!= !!drvres
) {
704 AP_DBF(DBF_DEBUG
, "reprobing queue=%02x.%04x\n",
706 rc
= device_reprobe(dev
);
713 static void ap_bus_revise_bindings(void)
715 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
, __ap_revise_reserved
);
718 int ap_owned_by_def_drv(int card
, int queue
)
722 if (card
< 0 || card
>= AP_DEVICES
|| queue
< 0 || queue
>= AP_DOMAINS
)
725 mutex_lock(&ap_perms_mutex
);
727 if (test_bit_inv(card
, ap_perms
.apm
)
728 && test_bit_inv(queue
, ap_perms
.aqm
))
731 mutex_unlock(&ap_perms_mutex
);
735 EXPORT_SYMBOL(ap_owned_by_def_drv
);
737 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm
,
740 int card
, queue
, rc
= 0;
742 mutex_lock(&ap_perms_mutex
);
744 for (card
= 0; !rc
&& card
< AP_DEVICES
; card
++)
745 if (test_bit_inv(card
, apm
) &&
746 test_bit_inv(card
, ap_perms
.apm
))
747 for (queue
= 0; !rc
&& queue
< AP_DOMAINS
; queue
++)
748 if (test_bit_inv(queue
, aqm
) &&
749 test_bit_inv(queue
, ap_perms
.aqm
))
752 mutex_unlock(&ap_perms_mutex
);
756 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv
);
758 static int ap_device_probe(struct device
*dev
)
760 struct ap_device
*ap_dev
= to_ap_dev(dev
);
761 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
762 int card
, queue
, devres
, drvres
, rc
;
764 if (is_queue_dev(dev
)) {
766 * If the apqn is marked as reserved/used by ap bus and
767 * default drivers, only probe with drivers with the default
768 * flag set. If it is not marked, only probe with drivers
769 * with the default flag not set.
771 card
= AP_QID_CARD(to_ap_queue(dev
)->qid
);
772 queue
= AP_QID_QUEUE(to_ap_queue(dev
)->qid
);
773 mutex_lock(&ap_perms_mutex
);
774 devres
= test_bit_inv(card
, ap_perms
.apm
)
775 && test_bit_inv(queue
, ap_perms
.aqm
);
776 mutex_unlock(&ap_perms_mutex
);
777 drvres
= ap_drv
->flags
& AP_DRIVER_FLAG_DEFAULT
;
778 if (!!devres
!= !!drvres
)
780 /* (re-)init queue's state machine */
781 ap_queue_reinit_state(to_ap_queue(dev
));
784 /* Add queue/card to list of active queues/cards */
785 spin_lock_bh(&ap_list_lock
);
786 if (is_card_dev(dev
))
787 list_add(&to_ap_card(dev
)->list
, &ap_card_list
);
789 list_add(&to_ap_queue(dev
)->list
,
790 &to_ap_queue(dev
)->card
->queues
);
791 spin_unlock_bh(&ap_list_lock
);
793 ap_dev
->drv
= ap_drv
;
794 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
797 spin_lock_bh(&ap_list_lock
);
798 if (is_card_dev(dev
))
799 list_del_init(&to_ap_card(dev
)->list
);
801 list_del_init(&to_ap_queue(dev
)->list
);
802 spin_unlock_bh(&ap_list_lock
);
809 static int ap_device_remove(struct device
*dev
)
811 struct ap_device
*ap_dev
= to_ap_dev(dev
);
812 struct ap_driver
*ap_drv
= ap_dev
->drv
;
814 if (is_queue_dev(dev
))
815 ap_queue_remove(to_ap_queue(dev
));
817 ap_drv
->remove(ap_dev
);
819 /* Remove queue/card from list of active queues/cards */
820 spin_lock_bh(&ap_list_lock
);
821 if (is_card_dev(dev
))
822 list_del_init(&to_ap_card(dev
)->list
);
824 list_del_init(&to_ap_queue(dev
)->list
);
825 spin_unlock_bh(&ap_list_lock
);
830 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
833 struct device_driver
*drv
= &ap_drv
->driver
;
838 drv
->bus
= &ap_bus_type
;
839 drv
->probe
= ap_device_probe
;
840 drv
->remove
= ap_device_remove
;
843 return driver_register(drv
);
845 EXPORT_SYMBOL(ap_driver_register
);
847 void ap_driver_unregister(struct ap_driver
*ap_drv
)
849 driver_unregister(&ap_drv
->driver
);
851 EXPORT_SYMBOL(ap_driver_unregister
);
853 void ap_bus_force_rescan(void)
857 /* processing a asynchronous bus rescan */
858 del_timer(&ap_config_timer
);
859 queue_work(system_long_wq
, &ap_scan_work
);
860 flush_work(&ap_scan_work
);
862 EXPORT_SYMBOL(ap_bus_force_rescan
);
865 * hex2bitmap() - parse hex mask string and set bitmap.
866 * Valid strings are "0x012345678" with at least one valid hex number.
867 * Rest of the bitmap to the right is padded with 0. No spaces allowed
868 * within the string, the leading 0x may be omitted.
869 * Returns the bitmask with exactly the bits set as given by the hex
870 * string (both in big endian order).
872 static int hex2bitmap(const char *str
, unsigned long *bitmap
, int bits
)
876 /* bits needs to be a multiple of 8 */
880 if (str
[0] == '0' && str
[1] == 'x')
885 for (i
= 0; isxdigit(*str
) && i
< bits
; str
++) {
886 b
= hex_to_bin(*str
);
887 for (n
= 0; n
< 4; n
++)
889 set_bit_inv(i
+ n
, bitmap
);
901 * modify_bitmap() - parse bitmask argument and modify an existing
902 * bit mask accordingly. A concatenation (done with ',') of these
903 * terms is recognized:
904 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
905 * <bitnr> may be any valid number (hex, decimal or octal) in the range
906 * 0...bits-1; the leading + or - is required. Here are some examples:
907 * +0-15,+32,-128,-0xFF
908 * -0-255,+1-16,+0x128
909 * +1,+2,+3,+4,-5,-7-10
910 * Returns the new bitmap after all changes have been applied. Every
911 * positive value in the string will set a bit and every negative value
912 * in the string will clear a bit. As a bit may be touched more than once,
913 * the last 'operation' wins:
914 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
915 * cleared again. All other bits are unmodified.
917 static int modify_bitmap(const char *str
, unsigned long *bitmap
, int bits
)
922 /* bits needs to be a multiple of 8 */
928 if (sign
!= '+' && sign
!= '-')
930 a
= z
= simple_strtoul(str
, &np
, 0);
931 if (str
== np
|| a
>= bits
)
935 z
= simple_strtoul(++str
, &np
, 0);
936 if (str
== np
|| a
> z
|| z
>= bits
)
940 for (i
= a
; i
<= z
; i
++)
942 set_bit_inv(i
, bitmap
);
944 clear_bit_inv(i
, bitmap
);
945 while (*str
== ',' || *str
== '\n')
953 * process_mask_arg() - parse a bitmap string and clear/set the
954 * bits in the bitmap accordingly. The string may be given as
955 * absolute value, a hex string like 0x1F2E3D4C5B6A" simple over-
956 * writing the current content of the bitmap. Or as relative string
957 * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
958 * bits are cleared or set. Distinction is done based on the very
959 * first character which may be '+' or '-' for the relative string
960 * and othewise assume to be an absolute value string. If parsing fails
961 * a negative errno value is returned. All arguments and bitmaps are
964 static int process_mask_arg(const char *str
,
965 unsigned long *bitmap
, int bits
,
968 unsigned long *newmap
, size
;
971 /* bits needs to be a multiple of 8 */
975 size
= BITS_TO_LONGS(bits
)*sizeof(unsigned long);
976 newmap
= kmalloc(size
, GFP_KERNEL
);
979 if (mutex_lock_interruptible(lock
)) {
984 if (*str
== '+' || *str
== '-') {
985 memcpy(newmap
, bitmap
, size
);
986 rc
= modify_bitmap(str
, newmap
, bits
);
988 memset(newmap
, 0, size
);
989 rc
= hex2bitmap(str
, newmap
, bits
);
992 memcpy(bitmap
, newmap
, size
);
1002 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
1004 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
1007 static ssize_t
ap_domain_store(struct bus_type
*bus
,
1008 const char *buf
, size_t count
)
1012 if (sscanf(buf
, "%i\n", &domain
) != 1 ||
1013 domain
< 0 || domain
> ap_max_domain_id
||
1014 !test_bit_inv(domain
, ap_perms
.aqm
))
1016 spin_lock_bh(&ap_domain_lock
);
1017 ap_domain_index
= domain
;
1018 spin_unlock_bh(&ap_domain_lock
);
1020 AP_DBF(DBF_DEBUG
, "stored new default domain=%d\n", domain
);
1025 static BUS_ATTR_RW(ap_domain
);
1027 static ssize_t
ap_control_domain_mask_show(struct bus_type
*bus
, char *buf
)
1029 if (!ap_configuration
) /* QCI not supported */
1030 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
1032 return snprintf(buf
, PAGE_SIZE
,
1033 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1034 ap_configuration
->adm
[0], ap_configuration
->adm
[1],
1035 ap_configuration
->adm
[2], ap_configuration
->adm
[3],
1036 ap_configuration
->adm
[4], ap_configuration
->adm
[5],
1037 ap_configuration
->adm
[6], ap_configuration
->adm
[7]);
1040 static BUS_ATTR_RO(ap_control_domain_mask
);
1042 static ssize_t
ap_usage_domain_mask_show(struct bus_type
*bus
, char *buf
)
1044 if (!ap_configuration
) /* QCI not supported */
1045 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
1047 return snprintf(buf
, PAGE_SIZE
,
1048 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1049 ap_configuration
->aqm
[0], ap_configuration
->aqm
[1],
1050 ap_configuration
->aqm
[2], ap_configuration
->aqm
[3],
1051 ap_configuration
->aqm
[4], ap_configuration
->aqm
[5],
1052 ap_configuration
->aqm
[6], ap_configuration
->aqm
[7]);
1055 static BUS_ATTR_RO(ap_usage_domain_mask
);
1057 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
1059 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1060 ap_using_interrupts() ? 1 : 0);
1063 static BUS_ATTR_RO(ap_interrupts
);
1065 static ssize_t
config_time_show(struct bus_type
*bus
, char *buf
)
1067 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
1070 static ssize_t
config_time_store(struct bus_type
*bus
,
1071 const char *buf
, size_t count
)
1075 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
1077 ap_config_time
= time
;
1078 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1082 static BUS_ATTR_RW(config_time
);
1084 static ssize_t
poll_thread_show(struct bus_type
*bus
, char *buf
)
1086 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
1089 static ssize_t
poll_thread_store(struct bus_type
*bus
,
1090 const char *buf
, size_t count
)
1094 if (sscanf(buf
, "%d\n", &flag
) != 1)
1097 rc
= ap_poll_thread_start();
1101 ap_poll_thread_stop();
1105 static BUS_ATTR_RW(poll_thread
);
1107 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
1109 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
1112 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
1115 unsigned long long time
;
1118 /* 120 seconds = maximum poll interval */
1119 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
1120 time
> 120000000000ULL)
1122 poll_timeout
= time
;
1123 hr_time
= poll_timeout
;
1125 spin_lock_bh(&ap_poll_timer_lock
);
1126 hrtimer_cancel(&ap_poll_timer
);
1127 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
1128 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
1129 spin_unlock_bh(&ap_poll_timer_lock
);
1134 static BUS_ATTR_RW(poll_timeout
);
1136 static ssize_t
ap_max_domain_id_show(struct bus_type
*bus
, char *buf
)
1140 if (ap_configuration
)
1141 max_domain_id
= ap_max_domain_id
? : -1;
1144 return snprintf(buf
, PAGE_SIZE
, "%d\n", max_domain_id
);
1147 static BUS_ATTR_RO(ap_max_domain_id
);
1149 static ssize_t
apmask_show(struct bus_type
*bus
, char *buf
)
1153 if (mutex_lock_interruptible(&ap_perms_mutex
))
1154 return -ERESTARTSYS
;
1155 rc
= snprintf(buf
, PAGE_SIZE
,
1156 "0x%016lx%016lx%016lx%016lx\n",
1157 ap_perms
.apm
[0], ap_perms
.apm
[1],
1158 ap_perms
.apm
[2], ap_perms
.apm
[3]);
1159 mutex_unlock(&ap_perms_mutex
);
1164 static ssize_t
apmask_store(struct bus_type
*bus
, const char *buf
,
1169 rc
= process_mask_arg(buf
, ap_perms
.apm
, AP_DEVICES
, &ap_perms_mutex
);
1173 ap_bus_revise_bindings();
1178 static BUS_ATTR_RW(apmask
);
1180 static ssize_t
aqmask_show(struct bus_type
*bus
, char *buf
)
1184 if (mutex_lock_interruptible(&ap_perms_mutex
))
1185 return -ERESTARTSYS
;
1186 rc
= snprintf(buf
, PAGE_SIZE
,
1187 "0x%016lx%016lx%016lx%016lx\n",
1188 ap_perms
.aqm
[0], ap_perms
.aqm
[1],
1189 ap_perms
.aqm
[2], ap_perms
.aqm
[3]);
1190 mutex_unlock(&ap_perms_mutex
);
1195 static ssize_t
aqmask_store(struct bus_type
*bus
, const char *buf
,
1200 rc
= process_mask_arg(buf
, ap_perms
.aqm
, AP_DOMAINS
, &ap_perms_mutex
);
1204 ap_bus_revise_bindings();
1209 static BUS_ATTR_RW(aqmask
);
1211 static struct bus_attribute
*const ap_bus_attrs
[] = {
1212 &bus_attr_ap_domain
,
1213 &bus_attr_ap_control_domain_mask
,
1214 &bus_attr_ap_usage_domain_mask
,
1215 &bus_attr_config_time
,
1216 &bus_attr_poll_thread
,
1217 &bus_attr_ap_interrupts
,
1218 &bus_attr_poll_timeout
,
1219 &bus_attr_ap_max_domain_id
,
1226 * ap_select_domain(): Select an AP domain if possible and we haven't
1227 * already done so before.
1229 static void ap_select_domain(void)
1231 int count
, max_count
, best_domain
;
1232 struct ap_queue_status status
;
1236 * We want to use a single domain. Either the one specified with
1237 * the "domain=" parameter or the domain with the maximum number
1240 spin_lock_bh(&ap_domain_lock
);
1241 if (ap_domain_index
>= 0) {
1242 /* Domain has already been selected. */
1243 spin_unlock_bh(&ap_domain_lock
);
1248 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1249 if (!ap_test_config_domain(i
) ||
1250 !test_bit_inv(i
, ap_perms
.aqm
))
1253 for (j
= 0; j
< AP_DEVICES
; j
++) {
1254 if (!ap_test_config_card_id(j
))
1256 status
= ap_test_queue(AP_MKQID(j
, i
),
1257 ap_apft_available(),
1259 if (status
.response_code
!= AP_RESPONSE_NORMAL
)
1263 if (count
> max_count
) {
1268 if (best_domain
>= 0) {
1269 ap_domain_index
= best_domain
;
1270 AP_DBF(DBF_DEBUG
, "new ap_domain_index=%d\n", ap_domain_index
);
1272 spin_unlock_bh(&ap_domain_lock
);
1276 * This function checks the type and returns either 0 for not
1277 * supported or the highest compatible type value (which may
1278 * include the input type value).
1280 static int ap_get_compatible_type(ap_qid_t qid
, int rawtype
, unsigned int func
)
1284 /* < CEX2A is not supported */
1285 if (rawtype
< AP_DEVICE_TYPE_CEX2A
)
1287 /* up to CEX6 known and fully supported */
1288 if (rawtype
<= AP_DEVICE_TYPE_CEX6
)
1291 * unknown new type > CEX6, check for compatibility
1292 * to the highest known and supported type which is
1293 * currently CEX6 with the help of the QACT function.
1295 if (ap_qact_available()) {
1296 struct ap_queue_status status
;
1297 union ap_qact_ap_info apinfo
= {0};
1299 apinfo
.mode
= (func
>> 26) & 0x07;
1300 apinfo
.cat
= AP_DEVICE_TYPE_CEX6
;
1301 status
= ap_qact(qid
, 0, &apinfo
);
1302 if (status
.response_code
== AP_RESPONSE_NORMAL
1303 && apinfo
.cat
>= AP_DEVICE_TYPE_CEX2A
1304 && apinfo
.cat
<= AP_DEVICE_TYPE_CEX6
)
1305 comp_type
= apinfo
.cat
;
1308 AP_DBF(DBF_WARN
, "queue=%02x.%04x unable to map type %d\n",
1309 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
), rawtype
);
1310 else if (comp_type
!= rawtype
)
1311 AP_DBF(DBF_INFO
, "queue=%02x.%04x map type %d to %d\n",
1312 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
), rawtype
, comp_type
);
1317 * helper function to be used with bus_find_dev
1318 * matches for the card device with the given id
1320 static int __match_card_device_with_id(struct device
*dev
, void *data
)
1322 return is_card_dev(dev
) && to_ap_card(dev
)->id
== (int)(long) data
;
1325 /* helper function to be used with bus_find_dev
1326 * matches for the queue device with a given qid
1328 static int __match_queue_device_with_qid(struct device
*dev
, void *data
)
1330 return is_queue_dev(dev
) && to_ap_queue(dev
)->qid
== (int)(long) data
;
1334 * ap_scan_bus(): Scan the AP bus for new devices
1335 * Runs periodically, workqueue timer (ap_config_time)
1337 static void ap_scan_bus(struct work_struct
*unused
)
1339 struct ap_queue
*aq
;
1343 int comp_type
, depth
= 0, type
= 0;
1344 unsigned int func
= 0;
1345 int rc
, id
, dom
, borked
, domains
, defdomdevs
= 0;
1347 AP_DBF(DBF_DEBUG
, "%s running\n", __func__
);
1349 ap_query_configuration(ap_configuration
);
1352 for (id
= 0; id
< AP_DEVICES
; id
++) {
1353 /* check if device is registered */
1354 dev
= bus_find_device(&ap_bus_type
, NULL
,
1356 __match_card_device_with_id
);
1357 ac
= dev
? to_ap_card(dev
) : NULL
;
1358 if (!ap_test_config_card_id(id
)) {
1360 /* Card device has been removed from
1361 * configuration, remove the belonging
1364 bus_for_each_dev(&ap_bus_type
, NULL
,
1366 __ap_queue_devices_with_id_unregister
);
1367 /* now remove the card device */
1368 device_unregister(dev
);
1373 /* According to the configuration there should be a card
1374 * device, so check if there is at least one valid queue
1375 * and maybe create queue devices and the card device.
1378 for (dom
= 0; dom
< AP_DOMAINS
; dom
++) {
1379 qid
= AP_MKQID(id
, dom
);
1380 dev
= bus_find_device(&ap_bus_type
, NULL
,
1382 __match_queue_device_with_qid
);
1383 aq
= dev
? to_ap_queue(dev
) : NULL
;
1384 if (!ap_test_config_domain(dom
)) {
1386 /* Queue device exists but has been
1387 * removed from configuration.
1389 device_unregister(dev
);
1394 rc
= ap_query_queue(qid
, &depth
, &type
, &func
);
1396 spin_lock_bh(&aq
->lock
);
1397 if (rc
== -ENODEV
||
1398 /* adapter reconfiguration */
1399 (ac
&& ac
->functions
!= func
))
1400 aq
->state
= AP_STATE_BORKED
;
1401 borked
= aq
->state
== AP_STATE_BORKED
;
1402 spin_unlock_bh(&aq
->lock
);
1403 if (borked
) /* Remove broken device */
1404 device_unregister(dev
);
1408 if (dom
== ap_domain_index
)
1415 /* a new queue device is needed, check out comp type */
1416 comp_type
= ap_get_compatible_type(qid
, type
, func
);
1419 /* maybe a card device needs to be created first */
1421 ac
= ap_card_create(id
, depth
, type
,
1425 ac
->ap_dev
.device
.bus
= &ap_bus_type
;
1426 ac
->ap_dev
.device
.parent
= ap_root_device
;
1427 dev_set_name(&ac
->ap_dev
.device
,
1429 /* Register card with AP bus */
1430 rc
= device_register(&ac
->ap_dev
.device
);
1432 put_device(&ac
->ap_dev
.device
);
1436 /* get it and thus adjust reference counter */
1437 get_device(&ac
->ap_dev
.device
);
1439 /* now create the new queue device */
1440 aq
= ap_queue_create(qid
, comp_type
);
1444 aq
->ap_dev
.device
.bus
= &ap_bus_type
;
1445 aq
->ap_dev
.device
.parent
= &ac
->ap_dev
.device
;
1446 dev_set_name(&aq
->ap_dev
.device
,
1447 "%02x.%04x", id
, dom
);
1448 /* Register device */
1449 rc
= device_register(&aq
->ap_dev
.device
);
1451 put_device(&aq
->ap_dev
.device
);
1455 if (dom
== ap_domain_index
)
1457 } /* end domain loop */
1459 /* remove card dev if there are no queue devices */
1461 device_unregister(&ac
->ap_dev
.device
);
1462 put_device(&ac
->ap_dev
.device
);
1464 } /* end device loop */
1466 if (ap_domain_index
>= 0 && defdomdevs
< 1)
1468 "no queue device with default domain %d available\n",
1471 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1474 static void ap_config_timeout(struct timer_list
*unused
)
1476 if (ap_suspend_flag
)
1478 queue_work(system_long_wq
, &ap_scan_work
);
1481 static int __init
ap_debug_init(void)
1483 ap_dbf_info
= debug_register("ap", 1, 1,
1484 DBF_MAX_SPRINTF_ARGS
* sizeof(long));
1485 debug_register_view(ap_dbf_info
, &debug_sprintf_view
);
1486 debug_set_level(ap_dbf_info
, DBF_ERR
);
1491 static void __init
ap_perms_init(void)
1493 /* all resources useable if no kernel parameter string given */
1494 memset(&ap_perms
.apm
, 0xFF, sizeof(ap_perms
.apm
));
1495 memset(&ap_perms
.aqm
, 0xFF, sizeof(ap_perms
.aqm
));
1497 /* apm kernel parameter string */
1499 memset(&ap_perms
.apm
, 0, sizeof(ap_perms
.apm
));
1500 process_mask_arg(apm_str
, ap_perms
.apm
, AP_DEVICES
,
1504 /* aqm kernel parameter string */
1506 memset(&ap_perms
.aqm
, 0, sizeof(ap_perms
.aqm
));
1507 process_mask_arg(aqm_str
, ap_perms
.aqm
, AP_DOMAINS
,
1513 * ap_module_init(): The module initialization code.
1515 * Initializes the module.
1517 static int __init
ap_module_init(void)
1522 rc
= ap_debug_init();
1526 if (!ap_instructions_available()) {
1527 pr_warn("The hardware system does not support AP instructions\n");
1531 /* set up the AP permissions (ap and aq masks) */
1534 /* Get AP configuration data if available */
1535 ap_init_configuration();
1537 if (ap_configuration
)
1539 ap_max_domain_id
? ap_max_domain_id
: AP_DOMAINS
- 1;
1542 if (ap_domain_index
< -1 || ap_domain_index
> max_domain_id
||
1543 (ap_domain_index
>= 0 &&
1544 !test_bit_inv(ap_domain_index
, ap_perms
.aqm
))) {
1545 pr_warn("%d is not a valid cryptographic domain\n",
1547 ap_domain_index
= -1;
1549 /* In resume callback we need to know if the user had set the domain.
1550 * If so, we can not just reset it.
1552 if (ap_domain_index
>= 0)
1553 user_set_domain
= 1;
1555 if (ap_interrupts_available()) {
1556 rc
= register_adapter_interrupt(&ap_airq
);
1557 ap_airq_flag
= (rc
== 0);
1560 /* Create /sys/bus/ap. */
1561 rc
= bus_register(&ap_bus_type
);
1564 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1565 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1570 /* Create /sys/devices/ap. */
1571 ap_root_device
= root_device_register("ap");
1572 rc
= PTR_ERR_OR_ZERO(ap_root_device
);
1576 /* Setup the AP bus rescan timer. */
1577 timer_setup(&ap_config_timer
, ap_config_timeout
, 0);
1580 * Setup the high resultion poll timer.
1581 * If we are running under z/VM adjust polling to z/VM polling rate.
1584 poll_timeout
= 1500000;
1585 spin_lock_init(&ap_poll_timer_lock
);
1586 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1587 ap_poll_timer
.function
= ap_poll_timeout
;
1589 /* Start the low priority AP bus poll thread. */
1590 if (ap_thread_flag
) {
1591 rc
= ap_poll_thread_start();
1596 rc
= register_pm_notifier(&ap_power_notifier
);
1600 queue_work(system_long_wq
, &ap_scan_work
);
1606 ap_poll_thread_stop();
1608 hrtimer_cancel(&ap_poll_timer
);
1609 root_device_unregister(ap_root_device
);
1612 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1613 bus_unregister(&ap_bus_type
);
1615 if (ap_using_interrupts())
1616 unregister_adapter_interrupt(&ap_airq
);
1617 kfree(ap_configuration
);
1620 device_initcall(ap_module_init
);