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>
28 #include <asm/reset.h>
30 #include <linux/atomic.h>
32 #include <linux/hrtimer.h>
33 #include <linux/ktime.h>
34 #include <asm/facility.h>
35 #include <linux/crypto.h>
36 #include <linux/mod_devicetable.h>
37 #include <linux/debugfs.h>
38 #include <linux/ctype.h>
44 * Module parameters; note though this file itself isn't modular.
46 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
47 static DEFINE_SPINLOCK(ap_domain_lock
);
48 module_param_named(domain
, ap_domain_index
, int, 0440);
49 MODULE_PARM_DESC(domain
, "domain index for ap devices");
50 EXPORT_SYMBOL(ap_domain_index
);
52 static int ap_thread_flag
;
53 module_param_named(poll_thread
, ap_thread_flag
, int, 0440);
54 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
57 module_param_named(apmask
, apm_str
, charp
, 0440);
58 MODULE_PARM_DESC(apmask
, "AP bus adapter mask.");
61 module_param_named(aqmask
, aqm_str
, charp
, 0440);
62 MODULE_PARM_DESC(aqmask
, "AP bus domain mask.");
64 static struct device
*ap_root_device
;
66 DEFINE_SPINLOCK(ap_list_lock
);
67 LIST_HEAD(ap_card_list
);
69 /* Default permissions (card and domain masking) */
70 static struct ap_perms
{
71 DECLARE_BITMAP(apm
, AP_DEVICES
);
72 DECLARE_BITMAP(aqm
, AP_DOMAINS
);
74 static DEFINE_MUTEX(ap_perms_mutex
);
76 static struct ap_config_info
*ap_configuration
;
77 static bool initialised
;
80 * AP bus related debug feature things.
82 debug_info_t
*ap_dbf_info
;
85 * Workqueue timer for bus rescan.
87 static struct timer_list ap_config_timer
;
88 static int ap_config_time
= AP_CONFIG_TIME
;
89 static void ap_scan_bus(struct work_struct
*);
90 static DECLARE_WORK(ap_scan_work
, ap_scan_bus
);
93 * Tasklet & timer for AP request polling and interrupts
95 static void ap_tasklet_fn(unsigned long);
96 static DECLARE_TASKLET(ap_tasklet
, ap_tasklet_fn
, 0);
97 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
98 static struct task_struct
*ap_poll_kthread
;
99 static DEFINE_MUTEX(ap_poll_thread_mutex
);
100 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
101 static struct hrtimer ap_poll_timer
;
103 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
104 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
106 static unsigned long long poll_timeout
= 250000;
109 static int ap_suspend_flag
;
110 /* Maximum domain id */
111 static int ap_max_domain_id
;
113 * Flag to check if domain was set through module parameter domain=. This is
114 * important when supsend and resume is done in a z/VM environment where the
115 * domain might change.
117 static int user_set_domain
;
118 static struct bus_type ap_bus_type
;
120 /* Adapter interrupt definitions */
121 static void ap_interrupt_handler(struct airq_struct
*airq
);
123 static int ap_airq_flag
;
125 static struct airq_struct ap_airq
= {
126 .handler
= ap_interrupt_handler
,
131 * ap_using_interrupts() - Returns non-zero if interrupt support is
134 static inline int ap_using_interrupts(void)
140 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
142 * Returns the address of the local-summary-indicator of the adapter
143 * interrupt handler for AP, or NULL if adapter interrupts are not
146 void *ap_airq_ptr(void)
148 if (ap_using_interrupts())
149 return ap_airq
.lsi_ptr
;
154 * ap_interrupts_available(): Test if AP interrupts are available.
156 * Returns 1 if AP interrupts are available.
158 static int ap_interrupts_available(void)
160 return test_facility(65);
164 * ap_configuration_available(): Test if AP configuration
165 * information is available.
167 * Returns 1 if AP configuration information is available.
169 static int ap_configuration_available(void)
171 return test_facility(12);
175 * ap_apft_available(): Test if AP facilities test (APFT)
176 * facility is available.
178 * Returns 1 if APFT is is available.
180 static int ap_apft_available(void)
182 return test_facility(15);
186 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
188 * Returns 1 if the QACT subfunction is available.
190 static inline int ap_qact_available(void)
192 if (ap_configuration
)
193 return ap_configuration
->qact
;
198 * ap_query_configuration(): Fetch cryptographic config info
200 * Returns the ap configuration info fetched via PQAP(QCI).
201 * On success 0 is returned, on failure a negative errno
202 * is returned, e.g. if the PQAP(QCI) instruction is not
203 * available, the return value will be -EOPNOTSUPP.
205 static inline int ap_query_configuration(struct ap_config_info
*info
)
207 if (!ap_configuration_available())
213 EXPORT_SYMBOL(ap_query_configuration
);
216 * ap_init_configuration(): Allocate and query configuration array.
218 static void ap_init_configuration(void)
220 if (!ap_configuration_available())
223 ap_configuration
= kzalloc(sizeof(*ap_configuration
), GFP_KERNEL
);
224 if (!ap_configuration
)
226 if (ap_query_configuration(ap_configuration
) != 0) {
227 kfree(ap_configuration
);
228 ap_configuration
= NULL
;
234 * ap_test_config(): helper function to extract the nrth bit
235 * within the unsigned int array field.
237 static inline int ap_test_config(unsigned int *field
, unsigned int nr
)
239 return ap_test_bit((field
+ (nr
>> 5)), (nr
& 0x1f));
243 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
246 * Returns 0 if the card is not configured
247 * 1 if the card is configured or
248 * if the configuration information is not available
250 static inline int ap_test_config_card_id(unsigned int id
)
252 if (!ap_configuration
) /* QCI not supported */
253 /* only ids 0...3F may be probed */
254 return id
< 0x40 ? 1 : 0;
255 return ap_test_config(ap_configuration
->apm
, id
);
259 * ap_test_config_usage_domain(): Test, whether an AP usage domain
261 * @domain AP usage domain ID
263 * Returns 0 if the usage domain is not configured
264 * 1 if the usage domain is configured or
265 * if the configuration information is not available
267 int ap_test_config_usage_domain(unsigned int domain
)
269 if (!ap_configuration
) /* QCI not supported */
271 return ap_test_config(ap_configuration
->aqm
, domain
);
273 EXPORT_SYMBOL(ap_test_config_usage_domain
);
276 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
278 * @domain AP control domain ID
280 * Returns 1 if the control domain is configured
281 * 0 in all other cases
283 int ap_test_config_ctrl_domain(unsigned int domain
)
285 if (!ap_configuration
) /* QCI not supported */
287 return ap_test_config(ap_configuration
->adm
, domain
);
289 EXPORT_SYMBOL(ap_test_config_ctrl_domain
);
292 * ap_query_queue(): Check if an AP queue is available.
293 * @qid: The AP queue number
294 * @queue_depth: Pointer to queue depth value
295 * @device_type: Pointer to device type value
296 * @facilities: Pointer to facility indicator
298 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
,
299 unsigned int *facilities
)
301 struct ap_queue_status status
;
305 if (!ap_test_config_card_id(AP_QID_CARD(qid
)))
308 status
= ap_test_queue(qid
, ap_apft_available(), &info
);
309 switch (status
.response_code
) {
310 case AP_RESPONSE_NORMAL
:
311 *queue_depth
= (int)(info
& 0xff);
312 *device_type
= (int)((info
>> 24) & 0xff);
313 *facilities
= (unsigned int)(info
>> 32);
314 /* Update maximum domain id */
315 nd
= (info
>> 16) & 0xff;
316 /* if N bit is available, z13 and newer */
317 if ((info
& (1UL << 57)) && nd
> 0)
318 ap_max_domain_id
= nd
;
319 else /* older machine types */
320 ap_max_domain_id
= 15;
321 switch (*device_type
) {
322 /* For CEX2 and CEX3 the available functions
323 * are not refrected by the facilities bits.
324 * Instead it is coded into the type. So here
325 * modify the function bits based on the type.
327 case AP_DEVICE_TYPE_CEX2A
:
328 case AP_DEVICE_TYPE_CEX3A
:
329 *facilities
|= 0x08000000;
331 case AP_DEVICE_TYPE_CEX2C
:
332 case AP_DEVICE_TYPE_CEX3C
:
333 *facilities
|= 0x10000000;
339 case AP_RESPONSE_Q_NOT_AVAIL
:
340 case AP_RESPONSE_DECONFIGURED
:
341 case AP_RESPONSE_CHECKSTOPPED
:
342 case AP_RESPONSE_INVALID_ADDRESS
:
344 case AP_RESPONSE_RESET_IN_PROGRESS
:
345 case AP_RESPONSE_OTHERWISE_CHANGED
:
346 case AP_RESPONSE_BUSY
:
353 void ap_wait(enum ap_wait wait
)
359 case AP_WAIT_INTERRUPT
:
360 if (ap_using_interrupts())
362 if (ap_poll_kthread
) {
363 wake_up(&ap_poll_wait
);
367 case AP_WAIT_TIMEOUT
:
368 spin_lock_bh(&ap_poll_timer_lock
);
369 if (!hrtimer_is_queued(&ap_poll_timer
)) {
370 hr_time
= poll_timeout
;
371 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
372 hrtimer_restart(&ap_poll_timer
);
374 spin_unlock_bh(&ap_poll_timer_lock
);
383 * ap_request_timeout(): Handling of request timeouts
384 * @t: timer making this callback
386 * Handles request timeouts.
388 void ap_request_timeout(struct timer_list
*t
)
390 struct ap_queue
*aq
= from_timer(aq
, t
, timeout
);
394 spin_lock_bh(&aq
->lock
);
395 ap_wait(ap_sm_event(aq
, AP_EVENT_TIMEOUT
));
396 spin_unlock_bh(&aq
->lock
);
400 * ap_poll_timeout(): AP receive polling for finished AP requests.
401 * @unused: Unused pointer.
403 * Schedules the AP tasklet using a high resolution timer.
405 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
407 if (!ap_suspend_flag
)
408 tasklet_schedule(&ap_tasklet
);
409 return HRTIMER_NORESTART
;
413 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
414 * @airq: pointer to adapter interrupt descriptor
416 static void ap_interrupt_handler(struct airq_struct
*airq
)
418 inc_irq_stat(IRQIO_APB
);
419 if (!ap_suspend_flag
)
420 tasklet_schedule(&ap_tasklet
);
424 * ap_tasklet_fn(): Tasklet to poll all AP devices.
425 * @dummy: Unused variable
427 * Poll all AP devices on the bus.
429 static void ap_tasklet_fn(unsigned long dummy
)
433 enum ap_wait wait
= AP_WAIT_NONE
;
435 /* Reset the indicator if interrupts are used. Thus new interrupts can
436 * be received. Doing it in the beginning of the tasklet is therefor
437 * important that no requests on any AP get lost.
439 if (ap_using_interrupts())
440 xchg(ap_airq
.lsi_ptr
, 0);
442 spin_lock_bh(&ap_list_lock
);
443 for_each_ap_card(ac
) {
444 for_each_ap_queue(aq
, ac
) {
445 spin_lock_bh(&aq
->lock
);
446 wait
= min(wait
, ap_sm_event_loop(aq
, AP_EVENT_POLL
));
447 spin_unlock_bh(&aq
->lock
);
450 spin_unlock_bh(&ap_list_lock
);
455 static int ap_pending_requests(void)
460 spin_lock_bh(&ap_list_lock
);
461 for_each_ap_card(ac
) {
462 for_each_ap_queue(aq
, ac
) {
463 if (aq
->queue_count
== 0)
465 spin_unlock_bh(&ap_list_lock
);
469 spin_unlock_bh(&ap_list_lock
);
474 * ap_poll_thread(): Thread that polls for finished requests.
475 * @data: Unused pointer
477 * AP bus poll thread. The purpose of this thread is to poll for
478 * finished requests in a loop if there is a "free" cpu - that is
479 * a cpu that doesn't have anything better to do. The polling stops
480 * as soon as there is another task or if all messages have been
483 static int ap_poll_thread(void *data
)
485 DECLARE_WAITQUEUE(wait
, current
);
487 set_user_nice(current
, MAX_NICE
);
489 while (!kthread_should_stop()) {
490 add_wait_queue(&ap_poll_wait
, &wait
);
491 set_current_state(TASK_INTERRUPTIBLE
);
492 if (ap_suspend_flag
|| !ap_pending_requests()) {
496 set_current_state(TASK_RUNNING
);
497 remove_wait_queue(&ap_poll_wait
, &wait
);
498 if (need_resched()) {
509 static int ap_poll_thread_start(void)
513 if (ap_using_interrupts() || ap_poll_kthread
)
515 mutex_lock(&ap_poll_thread_mutex
);
516 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
517 rc
= PTR_RET(ap_poll_kthread
);
519 ap_poll_kthread
= NULL
;
520 mutex_unlock(&ap_poll_thread_mutex
);
524 static void ap_poll_thread_stop(void)
526 if (!ap_poll_kthread
)
528 mutex_lock(&ap_poll_thread_mutex
);
529 kthread_stop(ap_poll_kthread
);
530 ap_poll_kthread
= NULL
;
531 mutex_unlock(&ap_poll_thread_mutex
);
534 #define is_card_dev(x) ((x)->parent == ap_root_device)
535 #define is_queue_dev(x) ((x)->parent != ap_root_device)
539 * @dev: Pointer to device
540 * @drv: Pointer to device_driver
542 * AP bus driver registration/unregistration.
544 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
546 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
547 struct ap_device_id
*id
;
550 * Compare device type of the device with the list of
551 * supported types of the device_driver.
553 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
554 if (is_card_dev(dev
) &&
555 id
->match_flags
& AP_DEVICE_ID_MATCH_CARD_TYPE
&&
556 id
->dev_type
== to_ap_dev(dev
)->device_type
)
558 if (is_queue_dev(dev
) &&
559 id
->match_flags
& AP_DEVICE_ID_MATCH_QUEUE_TYPE
&&
560 id
->dev_type
== to_ap_dev(dev
)->device_type
)
567 * ap_uevent(): Uevent function for AP devices.
568 * @dev: Pointer to device
569 * @env: Pointer to kobj_uevent_env
571 * It sets up a single environment variable DEV_TYPE which contains the
572 * hardware device type.
574 static int ap_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
576 struct ap_device
*ap_dev
= to_ap_dev(dev
);
582 /* Set up DEV_TYPE environment variable. */
583 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
588 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
593 static int ap_dev_suspend(struct device
*dev
)
595 struct ap_device
*ap_dev
= to_ap_dev(dev
);
597 if (ap_dev
->drv
&& ap_dev
->drv
->suspend
)
598 ap_dev
->drv
->suspend(ap_dev
);
602 static int ap_dev_resume(struct device
*dev
)
604 struct ap_device
*ap_dev
= to_ap_dev(dev
);
606 if (ap_dev
->drv
&& ap_dev
->drv
->resume
)
607 ap_dev
->drv
->resume(ap_dev
);
611 static void ap_bus_suspend(void)
613 AP_DBF(DBF_DEBUG
, "%s running\n", __func__
);
617 * Disable scanning for devices, thus we do not want to scan
618 * for them after removing.
620 flush_work(&ap_scan_work
);
621 tasklet_disable(&ap_tasklet
);
624 static int __ap_card_devices_unregister(struct device
*dev
, void *dummy
)
626 if (is_card_dev(dev
))
627 device_unregister(dev
);
631 static int __ap_queue_devices_unregister(struct device
*dev
, void *dummy
)
633 if (is_queue_dev(dev
))
634 device_unregister(dev
);
638 static int __ap_queue_devices_with_id_unregister(struct device
*dev
, void *data
)
640 if (is_queue_dev(dev
) &&
641 AP_QID_CARD(to_ap_queue(dev
)->qid
) == (int)(long) data
)
642 device_unregister(dev
);
646 static void ap_bus_resume(void)
650 AP_DBF(DBF_DEBUG
, "%s running\n", __func__
);
652 /* remove all queue devices */
653 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
654 __ap_queue_devices_unregister
);
655 /* remove all card devices */
656 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
657 __ap_card_devices_unregister
);
659 /* Reset thin interrupt setting */
660 if (ap_interrupts_available() && !ap_using_interrupts()) {
661 rc
= register_adapter_interrupt(&ap_airq
);
662 ap_airq_flag
= (rc
== 0);
664 if (!ap_interrupts_available() && ap_using_interrupts()) {
665 unregister_adapter_interrupt(&ap_airq
);
669 if (!user_set_domain
)
670 ap_domain_index
= -1;
671 /* Get things going again */
674 xchg(ap_airq
.lsi_ptr
, 0);
675 tasklet_enable(&ap_tasklet
);
676 queue_work(system_long_wq
, &ap_scan_work
);
679 static int ap_power_event(struct notifier_block
*this, unsigned long event
,
683 case PM_HIBERNATION_PREPARE
:
684 case PM_SUSPEND_PREPARE
:
687 case PM_POST_HIBERNATION
:
688 case PM_POST_SUSPEND
:
696 static struct notifier_block ap_power_notifier
= {
697 .notifier_call
= ap_power_event
,
700 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops
, ap_dev_suspend
, ap_dev_resume
);
702 static struct bus_type ap_bus_type
= {
704 .match
= &ap_bus_match
,
705 .uevent
= &ap_uevent
,
706 .pm
= &ap_bus_pm_ops
,
709 static int __ap_revise_reserved(struct device
*dev
, void *dummy
)
711 int rc
, card
, queue
, devres
, drvres
;
713 if (is_queue_dev(dev
)) {
714 card
= AP_QID_CARD(to_ap_queue(dev
)->qid
);
715 queue
= AP_QID_QUEUE(to_ap_queue(dev
)->qid
);
716 mutex_lock(&ap_perms_mutex
);
717 devres
= test_bit_inv(card
, ap_perms
.apm
)
718 && test_bit_inv(queue
, ap_perms
.aqm
);
719 mutex_unlock(&ap_perms_mutex
);
720 drvres
= to_ap_drv(dev
->driver
)->flags
721 & AP_DRIVER_FLAG_DEFAULT
;
722 if (!!devres
!= !!drvres
) {
723 AP_DBF(DBF_DEBUG
, "reprobing queue=%02x.%04x\n",
725 rc
= device_reprobe(dev
);
732 static void ap_bus_revise_bindings(void)
734 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
, __ap_revise_reserved
);
737 int ap_owned_by_def_drv(int card
, int queue
)
741 if (card
< 0 || card
>= AP_DEVICES
|| queue
< 0 || queue
>= AP_DOMAINS
)
744 mutex_lock(&ap_perms_mutex
);
746 if (test_bit_inv(card
, ap_perms
.apm
)
747 && test_bit_inv(queue
, ap_perms
.aqm
))
750 mutex_unlock(&ap_perms_mutex
);
754 EXPORT_SYMBOL(ap_owned_by_def_drv
);
756 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm
,
759 int card
, queue
, rc
= 0;
761 mutex_lock(&ap_perms_mutex
);
763 for (card
= 0; !rc
&& card
< AP_DEVICES
; card
++)
764 if (test_bit_inv(card
, apm
) &&
765 test_bit_inv(card
, ap_perms
.apm
))
766 for (queue
= 0; !rc
&& queue
< AP_DOMAINS
; queue
++)
767 if (test_bit_inv(queue
, aqm
) &&
768 test_bit_inv(queue
, ap_perms
.aqm
))
771 mutex_unlock(&ap_perms_mutex
);
775 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv
);
777 static int ap_device_probe(struct device
*dev
)
779 struct ap_device
*ap_dev
= to_ap_dev(dev
);
780 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
781 int card
, queue
, devres
, drvres
, rc
;
783 if (is_queue_dev(dev
)) {
785 * If the apqn is marked as reserved/used by ap bus and
786 * default drivers, only probe with drivers with the default
787 * flag set. If it is not marked, only probe with drivers
788 * with the default flag not set.
790 card
= AP_QID_CARD(to_ap_queue(dev
)->qid
);
791 queue
= AP_QID_QUEUE(to_ap_queue(dev
)->qid
);
792 mutex_lock(&ap_perms_mutex
);
793 devres
= test_bit_inv(card
, ap_perms
.apm
)
794 && test_bit_inv(queue
, ap_perms
.aqm
);
795 mutex_unlock(&ap_perms_mutex
);
796 drvres
= ap_drv
->flags
& AP_DRIVER_FLAG_DEFAULT
;
797 if (!!devres
!= !!drvres
)
799 /* (re-)init queue's state machine */
800 ap_queue_reinit_state(to_ap_queue(dev
));
803 /* Add queue/card to list of active queues/cards */
804 spin_lock_bh(&ap_list_lock
);
805 if (is_card_dev(dev
))
806 list_add(&to_ap_card(dev
)->list
, &ap_card_list
);
808 list_add(&to_ap_queue(dev
)->list
,
809 &to_ap_queue(dev
)->card
->queues
);
810 spin_unlock_bh(&ap_list_lock
);
812 ap_dev
->drv
= ap_drv
;
813 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
816 spin_lock_bh(&ap_list_lock
);
817 if (is_card_dev(dev
))
818 list_del_init(&to_ap_card(dev
)->list
);
820 list_del_init(&to_ap_queue(dev
)->list
);
821 spin_unlock_bh(&ap_list_lock
);
828 static int ap_device_remove(struct device
*dev
)
830 struct ap_device
*ap_dev
= to_ap_dev(dev
);
831 struct ap_driver
*ap_drv
= ap_dev
->drv
;
833 if (is_queue_dev(dev
))
834 ap_queue_remove(to_ap_queue(dev
));
836 ap_drv
->remove(ap_dev
);
838 /* Remove queue/card from list of active queues/cards */
839 spin_lock_bh(&ap_list_lock
);
840 if (is_card_dev(dev
))
841 list_del_init(&to_ap_card(dev
)->list
);
843 list_del_init(&to_ap_queue(dev
)->list
);
844 spin_unlock_bh(&ap_list_lock
);
849 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
852 struct device_driver
*drv
= &ap_drv
->driver
;
857 drv
->bus
= &ap_bus_type
;
858 drv
->probe
= ap_device_probe
;
859 drv
->remove
= ap_device_remove
;
862 return driver_register(drv
);
864 EXPORT_SYMBOL(ap_driver_register
);
866 void ap_driver_unregister(struct ap_driver
*ap_drv
)
868 driver_unregister(&ap_drv
->driver
);
870 EXPORT_SYMBOL(ap_driver_unregister
);
872 void ap_bus_force_rescan(void)
876 /* processing a asynchronous bus rescan */
877 del_timer(&ap_config_timer
);
878 queue_work(system_long_wq
, &ap_scan_work
);
879 flush_work(&ap_scan_work
);
881 EXPORT_SYMBOL(ap_bus_force_rescan
);
884 * hex2bitmap() - parse hex mask string and set bitmap.
885 * Valid strings are "0x012345678" with at least one valid hex number.
886 * Rest of the bitmap to the right is padded with 0. No spaces allowed
887 * within the string, the leading 0x may be omitted.
888 * Returns the bitmask with exactly the bits set as given by the hex
889 * string (both in big endian order).
891 static int hex2bitmap(const char *str
, unsigned long *bitmap
, int bits
)
895 /* bits needs to be a multiple of 8 */
899 if (str
[0] == '0' && str
[1] == 'x')
904 for (i
= 0; isxdigit(*str
) && i
< bits
; str
++) {
905 b
= hex_to_bin(*str
);
906 for (n
= 0; n
< 4; n
++)
908 set_bit_inv(i
+ n
, bitmap
);
920 * modify_bitmap() - parse bitmask argument and modify an existing
921 * bit mask accordingly. A concatenation (done with ',') of these
922 * terms is recognized:
923 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
924 * <bitnr> may be any valid number (hex, decimal or octal) in the range
925 * 0...bits-1; the leading + or - is required. Here are some examples:
926 * +0-15,+32,-128,-0xFF
927 * -0-255,+1-16,+0x128
928 * +1,+2,+3,+4,-5,-7-10
929 * Returns the new bitmap after all changes have been applied. Every
930 * positive value in the string will set a bit and every negative value
931 * in the string will clear a bit. As a bit may be touched more than once,
932 * the last 'operation' wins:
933 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
934 * cleared again. All other bits are unmodified.
936 static int modify_bitmap(const char *str
, unsigned long *bitmap
, int bits
)
941 /* bits needs to be a multiple of 8 */
947 if (sign
!= '+' && sign
!= '-')
949 a
= z
= simple_strtoul(str
, &np
, 0);
950 if (str
== np
|| a
>= bits
)
954 z
= simple_strtoul(++str
, &np
, 0);
955 if (str
== np
|| a
> z
|| z
>= bits
)
959 for (i
= a
; i
<= z
; i
++)
961 set_bit_inv(i
, bitmap
);
963 clear_bit_inv(i
, bitmap
);
964 while (*str
== ',' || *str
== '\n')
972 * process_mask_arg() - parse a bitmap string and clear/set the
973 * bits in the bitmap accordingly. The string may be given as
974 * absolute value, a hex string like 0x1F2E3D4C5B6A" simple over-
975 * writing the current content of the bitmap. Or as relative string
976 * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
977 * bits are cleared or set. Distinction is done based on the very
978 * first character which may be '+' or '-' for the relative string
979 * and othewise assume to be an absolute value string. If parsing fails
980 * a negative errno value is returned. All arguments and bitmaps are
983 static int process_mask_arg(const char *str
,
984 unsigned long *bitmap
, int bits
,
987 unsigned long *newmap
, size
;
990 /* bits needs to be a multiple of 8 */
994 size
= BITS_TO_LONGS(bits
)*sizeof(unsigned long);
995 newmap
= kmalloc(size
, GFP_KERNEL
);
998 if (mutex_lock_interruptible(lock
)) {
1000 return -ERESTARTSYS
;
1003 if (*str
== '+' || *str
== '-') {
1004 memcpy(newmap
, bitmap
, size
);
1005 rc
= modify_bitmap(str
, newmap
, bits
);
1007 memset(newmap
, 0, size
);
1008 rc
= hex2bitmap(str
, newmap
, bits
);
1011 memcpy(bitmap
, newmap
, size
);
1018 * AP bus attributes.
1021 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
1023 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
1026 static ssize_t
ap_domain_store(struct bus_type
*bus
,
1027 const char *buf
, size_t count
)
1031 if (sscanf(buf
, "%i\n", &domain
) != 1 ||
1032 domain
< 0 || domain
> ap_max_domain_id
||
1033 !test_bit_inv(domain
, ap_perms
.aqm
))
1035 spin_lock_bh(&ap_domain_lock
);
1036 ap_domain_index
= domain
;
1037 spin_unlock_bh(&ap_domain_lock
);
1039 AP_DBF(DBF_DEBUG
, "stored new default domain=%d\n", domain
);
1044 static BUS_ATTR_RW(ap_domain
);
1046 static ssize_t
ap_control_domain_mask_show(struct bus_type
*bus
, char *buf
)
1048 if (!ap_configuration
) /* QCI not supported */
1049 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
1051 return snprintf(buf
, PAGE_SIZE
,
1052 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1053 ap_configuration
->adm
[0], ap_configuration
->adm
[1],
1054 ap_configuration
->adm
[2], ap_configuration
->adm
[3],
1055 ap_configuration
->adm
[4], ap_configuration
->adm
[5],
1056 ap_configuration
->adm
[6], ap_configuration
->adm
[7]);
1059 static BUS_ATTR_RO(ap_control_domain_mask
);
1061 static ssize_t
ap_usage_domain_mask_show(struct bus_type
*bus
, char *buf
)
1063 if (!ap_configuration
) /* QCI not supported */
1064 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
1066 return snprintf(buf
, PAGE_SIZE
,
1067 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1068 ap_configuration
->aqm
[0], ap_configuration
->aqm
[1],
1069 ap_configuration
->aqm
[2], ap_configuration
->aqm
[3],
1070 ap_configuration
->aqm
[4], ap_configuration
->aqm
[5],
1071 ap_configuration
->aqm
[6], ap_configuration
->aqm
[7]);
1074 static BUS_ATTR_RO(ap_usage_domain_mask
);
1076 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
1078 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1079 ap_using_interrupts() ? 1 : 0);
1082 static BUS_ATTR_RO(ap_interrupts
);
1084 static ssize_t
config_time_show(struct bus_type
*bus
, char *buf
)
1086 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
1089 static ssize_t
config_time_store(struct bus_type
*bus
,
1090 const char *buf
, size_t count
)
1094 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
1096 ap_config_time
= time
;
1097 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1101 static BUS_ATTR_RW(config_time
);
1103 static ssize_t
poll_thread_show(struct bus_type
*bus
, char *buf
)
1105 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
1108 static ssize_t
poll_thread_store(struct bus_type
*bus
,
1109 const char *buf
, size_t count
)
1113 if (sscanf(buf
, "%d\n", &flag
) != 1)
1116 rc
= ap_poll_thread_start();
1120 ap_poll_thread_stop();
1124 static BUS_ATTR_RW(poll_thread
);
1126 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
1128 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
1131 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
1134 unsigned long long time
;
1137 /* 120 seconds = maximum poll interval */
1138 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
1139 time
> 120000000000ULL)
1141 poll_timeout
= time
;
1142 hr_time
= poll_timeout
;
1144 spin_lock_bh(&ap_poll_timer_lock
);
1145 hrtimer_cancel(&ap_poll_timer
);
1146 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
1147 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
1148 spin_unlock_bh(&ap_poll_timer_lock
);
1153 static BUS_ATTR_RW(poll_timeout
);
1155 static ssize_t
ap_max_domain_id_show(struct bus_type
*bus
, char *buf
)
1159 if (ap_configuration
)
1160 max_domain_id
= ap_max_domain_id
? : -1;
1163 return snprintf(buf
, PAGE_SIZE
, "%d\n", max_domain_id
);
1166 static BUS_ATTR_RO(ap_max_domain_id
);
1168 static ssize_t
apmask_show(struct bus_type
*bus
, char *buf
)
1172 if (mutex_lock_interruptible(&ap_perms_mutex
))
1173 return -ERESTARTSYS
;
1174 rc
= snprintf(buf
, PAGE_SIZE
,
1175 "0x%016lx%016lx%016lx%016lx\n",
1176 ap_perms
.apm
[0], ap_perms
.apm
[1],
1177 ap_perms
.apm
[2], ap_perms
.apm
[3]);
1178 mutex_unlock(&ap_perms_mutex
);
1183 static ssize_t
apmask_store(struct bus_type
*bus
, const char *buf
,
1188 rc
= process_mask_arg(buf
, ap_perms
.apm
, AP_DEVICES
, &ap_perms_mutex
);
1192 ap_bus_revise_bindings();
1197 static BUS_ATTR_RW(apmask
);
1199 static ssize_t
aqmask_show(struct bus_type
*bus
, char *buf
)
1203 if (mutex_lock_interruptible(&ap_perms_mutex
))
1204 return -ERESTARTSYS
;
1205 rc
= snprintf(buf
, PAGE_SIZE
,
1206 "0x%016lx%016lx%016lx%016lx\n",
1207 ap_perms
.aqm
[0], ap_perms
.aqm
[1],
1208 ap_perms
.aqm
[2], ap_perms
.aqm
[3]);
1209 mutex_unlock(&ap_perms_mutex
);
1214 static ssize_t
aqmask_store(struct bus_type
*bus
, const char *buf
,
1219 rc
= process_mask_arg(buf
, ap_perms
.aqm
, AP_DOMAINS
, &ap_perms_mutex
);
1223 ap_bus_revise_bindings();
1228 static BUS_ATTR_RW(aqmask
);
1230 static struct bus_attribute
*const ap_bus_attrs
[] = {
1231 &bus_attr_ap_domain
,
1232 &bus_attr_ap_control_domain_mask
,
1233 &bus_attr_ap_usage_domain_mask
,
1234 &bus_attr_config_time
,
1235 &bus_attr_poll_thread
,
1236 &bus_attr_ap_interrupts
,
1237 &bus_attr_poll_timeout
,
1238 &bus_attr_ap_max_domain_id
,
1245 * ap_select_domain(): Select an AP domain if possible and we haven't
1246 * already done so before.
1248 static void ap_select_domain(void)
1250 int count
, max_count
, best_domain
;
1251 struct ap_queue_status status
;
1255 * We want to use a single domain. Either the one specified with
1256 * the "domain=" parameter or the domain with the maximum number
1259 spin_lock_bh(&ap_domain_lock
);
1260 if (ap_domain_index
>= 0) {
1261 /* Domain has already been selected. */
1262 spin_unlock_bh(&ap_domain_lock
);
1267 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1268 if (!ap_test_config_usage_domain(i
) ||
1269 !test_bit_inv(i
, ap_perms
.aqm
))
1272 for (j
= 0; j
< AP_DEVICES
; j
++) {
1273 if (!ap_test_config_card_id(j
))
1275 status
= ap_test_queue(AP_MKQID(j
, i
),
1276 ap_apft_available(),
1278 if (status
.response_code
!= AP_RESPONSE_NORMAL
)
1282 if (count
> max_count
) {
1287 if (best_domain
>= 0) {
1288 ap_domain_index
= best_domain
;
1289 AP_DBF(DBF_DEBUG
, "new ap_domain_index=%d\n", ap_domain_index
);
1291 spin_unlock_bh(&ap_domain_lock
);
1295 * This function checks the type and returns either 0 for not
1296 * supported or the highest compatible type value (which may
1297 * include the input type value).
1299 static int ap_get_compatible_type(ap_qid_t qid
, int rawtype
, unsigned int func
)
1303 /* < CEX2A is not supported */
1304 if (rawtype
< AP_DEVICE_TYPE_CEX2A
)
1306 /* up to CEX6 known and fully supported */
1307 if (rawtype
<= AP_DEVICE_TYPE_CEX6
)
1310 * unknown new type > CEX6, check for compatibility
1311 * to the highest known and supported type which is
1312 * currently CEX6 with the help of the QACT function.
1314 if (ap_qact_available()) {
1315 struct ap_queue_status status
;
1316 union ap_qact_ap_info apinfo
= {0};
1318 apinfo
.mode
= (func
>> 26) & 0x07;
1319 apinfo
.cat
= AP_DEVICE_TYPE_CEX6
;
1320 status
= ap_qact(qid
, 0, &apinfo
);
1321 if (status
.response_code
== AP_RESPONSE_NORMAL
1322 && apinfo
.cat
>= AP_DEVICE_TYPE_CEX2A
1323 && apinfo
.cat
<= AP_DEVICE_TYPE_CEX6
)
1324 comp_type
= apinfo
.cat
;
1327 AP_DBF(DBF_WARN
, "queue=%02x.%04x unable to map type %d\n",
1328 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
), rawtype
);
1329 else if (comp_type
!= rawtype
)
1330 AP_DBF(DBF_INFO
, "queue=%02x.%04x map type %d to %d\n",
1331 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
), rawtype
, comp_type
);
1336 * helper function to be used with bus_find_dev
1337 * matches for the card device with the given id
1339 static int __match_card_device_with_id(struct device
*dev
, void *data
)
1341 return is_card_dev(dev
) && to_ap_card(dev
)->id
== (int)(long) data
;
1344 /* helper function to be used with bus_find_dev
1345 * matches for the queue device with a given qid
1347 static int __match_queue_device_with_qid(struct device
*dev
, void *data
)
1349 return is_queue_dev(dev
) && to_ap_queue(dev
)->qid
== (int)(long) data
;
1353 * ap_scan_bus(): Scan the AP bus for new devices
1354 * Runs periodically, workqueue timer (ap_config_time)
1356 static void ap_scan_bus(struct work_struct
*unused
)
1358 struct ap_queue
*aq
;
1362 int comp_type
, depth
= 0, type
= 0;
1363 unsigned int func
= 0;
1364 int rc
, id
, dom
, borked
, domains
, defdomdevs
= 0;
1366 AP_DBF(DBF_DEBUG
, "%s running\n", __func__
);
1368 ap_query_configuration(ap_configuration
);
1371 for (id
= 0; id
< AP_DEVICES
; id
++) {
1372 /* check if device is registered */
1373 dev
= bus_find_device(&ap_bus_type
, NULL
,
1375 __match_card_device_with_id
);
1376 ac
= dev
? to_ap_card(dev
) : NULL
;
1377 if (!ap_test_config_card_id(id
)) {
1379 /* Card device has been removed from
1380 * configuration, remove the belonging
1383 bus_for_each_dev(&ap_bus_type
, NULL
,
1385 __ap_queue_devices_with_id_unregister
);
1386 /* now remove the card device */
1387 device_unregister(dev
);
1392 /* According to the configuration there should be a card
1393 * device, so check if there is at least one valid queue
1394 * and maybe create queue devices and the card device.
1397 for (dom
= 0; dom
< AP_DOMAINS
; dom
++) {
1398 qid
= AP_MKQID(id
, dom
);
1399 dev
= bus_find_device(&ap_bus_type
, NULL
,
1401 __match_queue_device_with_qid
);
1402 aq
= dev
? to_ap_queue(dev
) : NULL
;
1403 if (!ap_test_config_usage_domain(dom
)) {
1405 /* Queue device exists but has been
1406 * removed from configuration.
1408 device_unregister(dev
);
1413 rc
= ap_query_queue(qid
, &depth
, &type
, &func
);
1415 spin_lock_bh(&aq
->lock
);
1416 if (rc
== -ENODEV
||
1417 /* adapter reconfiguration */
1418 (ac
&& ac
->functions
!= func
))
1419 aq
->state
= AP_STATE_BORKED
;
1420 borked
= aq
->state
== AP_STATE_BORKED
;
1421 spin_unlock_bh(&aq
->lock
);
1422 if (borked
) /* Remove broken device */
1423 device_unregister(dev
);
1427 if (dom
== ap_domain_index
)
1434 /* a new queue device is needed, check out comp type */
1435 comp_type
= ap_get_compatible_type(qid
, type
, func
);
1438 /* maybe a card device needs to be created first */
1440 ac
= ap_card_create(id
, depth
, type
,
1444 ac
->ap_dev
.device
.bus
= &ap_bus_type
;
1445 ac
->ap_dev
.device
.parent
= ap_root_device
;
1446 dev_set_name(&ac
->ap_dev
.device
,
1448 /* Register card with AP bus */
1449 rc
= device_register(&ac
->ap_dev
.device
);
1451 put_device(&ac
->ap_dev
.device
);
1455 /* get it and thus adjust reference counter */
1456 get_device(&ac
->ap_dev
.device
);
1458 /* now create the new queue device */
1459 aq
= ap_queue_create(qid
, comp_type
);
1463 aq
->ap_dev
.device
.bus
= &ap_bus_type
;
1464 aq
->ap_dev
.device
.parent
= &ac
->ap_dev
.device
;
1465 dev_set_name(&aq
->ap_dev
.device
,
1466 "%02x.%04x", id
, dom
);
1467 /* Register device */
1468 rc
= device_register(&aq
->ap_dev
.device
);
1470 put_device(&aq
->ap_dev
.device
);
1474 if (dom
== ap_domain_index
)
1476 } /* end domain loop */
1478 /* remove card dev if there are no queue devices */
1480 device_unregister(&ac
->ap_dev
.device
);
1481 put_device(&ac
->ap_dev
.device
);
1483 } /* end device loop */
1485 if (ap_domain_index
>= 0 && defdomdevs
< 1)
1487 "no queue device with default domain %d available\n",
1490 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1493 static void ap_config_timeout(struct timer_list
*unused
)
1495 if (ap_suspend_flag
)
1497 queue_work(system_long_wq
, &ap_scan_work
);
1500 static void ap_reset_all(void)
1504 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1505 if (!ap_test_config_usage_domain(i
))
1507 for (j
= 0; j
< AP_DEVICES
; j
++) {
1508 if (!ap_test_config_card_id(j
))
1510 ap_rapq(AP_MKQID(j
, i
));
1515 static struct reset_call ap_reset_call
= {
1519 int __init
ap_debug_init(void)
1521 ap_dbf_info
= debug_register("ap", 1, 1,
1522 DBF_MAX_SPRINTF_ARGS
* sizeof(long));
1523 debug_register_view(ap_dbf_info
, &debug_sprintf_view
);
1524 debug_set_level(ap_dbf_info
, DBF_ERR
);
1529 static void __init
ap_perms_init(void)
1531 /* all resources useable if no kernel parameter string given */
1532 memset(&ap_perms
.apm
, 0xFF, sizeof(ap_perms
.apm
));
1533 memset(&ap_perms
.aqm
, 0xFF, sizeof(ap_perms
.aqm
));
1535 /* apm kernel parameter string */
1537 memset(&ap_perms
.apm
, 0, sizeof(ap_perms
.apm
));
1538 process_mask_arg(apm_str
, ap_perms
.apm
, AP_DEVICES
,
1542 /* aqm kernel parameter string */
1544 memset(&ap_perms
.aqm
, 0, sizeof(ap_perms
.aqm
));
1545 process_mask_arg(aqm_str
, ap_perms
.aqm
, AP_DOMAINS
,
1551 * ap_module_init(): The module initialization code.
1553 * Initializes the module.
1555 int __init
ap_module_init(void)
1560 rc
= ap_debug_init();
1564 if (!ap_instructions_available()) {
1565 pr_warn("The hardware system does not support AP instructions\n");
1569 /* set up the AP permissions (ap and aq masks) */
1572 /* Get AP configuration data if available */
1573 ap_init_configuration();
1575 if (ap_configuration
)
1577 ap_max_domain_id
? ap_max_domain_id
: AP_DOMAINS
- 1;
1580 if (ap_domain_index
< -1 || ap_domain_index
> max_domain_id
||
1581 (ap_domain_index
>= 0 &&
1582 !test_bit_inv(ap_domain_index
, ap_perms
.aqm
))) {
1583 pr_warn("%d is not a valid cryptographic domain\n",
1585 ap_domain_index
= -1;
1587 /* In resume callback we need to know if the user had set the domain.
1588 * If so, we can not just reset it.
1590 if (ap_domain_index
>= 0)
1591 user_set_domain
= 1;
1593 if (ap_interrupts_available()) {
1594 rc
= register_adapter_interrupt(&ap_airq
);
1595 ap_airq_flag
= (rc
== 0);
1598 register_reset_call(&ap_reset_call
);
1600 /* Create /sys/bus/ap. */
1601 rc
= bus_register(&ap_bus_type
);
1604 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1605 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1610 /* Create /sys/devices/ap. */
1611 ap_root_device
= root_device_register("ap");
1612 rc
= PTR_RET(ap_root_device
);
1616 /* Setup the AP bus rescan timer. */
1617 timer_setup(&ap_config_timer
, ap_config_timeout
, 0);
1620 * Setup the high resultion poll timer.
1621 * If we are running under z/VM adjust polling to z/VM polling rate.
1624 poll_timeout
= 1500000;
1625 spin_lock_init(&ap_poll_timer_lock
);
1626 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1627 ap_poll_timer
.function
= ap_poll_timeout
;
1629 /* Start the low priority AP bus poll thread. */
1630 if (ap_thread_flag
) {
1631 rc
= ap_poll_thread_start();
1636 rc
= register_pm_notifier(&ap_power_notifier
);
1640 queue_work(system_long_wq
, &ap_scan_work
);
1646 ap_poll_thread_stop();
1648 hrtimer_cancel(&ap_poll_timer
);
1649 root_device_unregister(ap_root_device
);
1652 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1653 bus_unregister(&ap_bus_type
);
1655 unregister_reset_call(&ap_reset_call
);
1656 if (ap_using_interrupts())
1657 unregister_adapter_interrupt(&ap_airq
);
1658 kfree(ap_configuration
);
1661 device_initcall(ap_module_init
);