2 * Copyright IBM Corp. 2006, 2012
3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
6 * Felix Beck <felix.beck@de.ibm.com>
7 * Holger Dengler <hd@linux.vnet.ibm.com>
9 * Adjunct processor bus.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/kernel_stat.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <linux/suspend.h>
41 #include <asm/reset.h>
43 #include <linux/atomic.h>
45 #include <linux/hrtimer.h>
46 #include <linux/ktime.h>
47 #include <asm/facility.h>
48 #include <linux/crypto.h>
49 #include <linux/mod_devicetable.h>
50 #include <linux/debugfs.h>
57 * Module parameters; note though this file itself isn't modular.
59 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
60 static DEFINE_SPINLOCK(ap_domain_lock
);
61 module_param_named(domain
, ap_domain_index
, int, S_IRUSR
|S_IRGRP
);
62 MODULE_PARM_DESC(domain
, "domain index for ap devices");
63 EXPORT_SYMBOL(ap_domain_index
);
65 static int ap_thread_flag
= 0;
66 module_param_named(poll_thread
, ap_thread_flag
, int, S_IRUSR
|S_IRGRP
);
67 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
69 static struct device
*ap_root_device
;
71 DEFINE_SPINLOCK(ap_list_lock
);
72 LIST_HEAD(ap_card_list
);
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
= NULL
;
97 static DEFINE_MUTEX(ap_poll_thread_mutex
);
98 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
99 static struct hrtimer ap_poll_timer
;
100 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
101 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
102 static unsigned long long poll_timeout
= 250000;
105 static int ap_suspend_flag
;
106 /* Maximum domain id */
107 static int ap_max_domain_id
;
108 /* Flag to check if domain was set through module parameter domain=. This is
109 * important when supsend and resume is done in a z/VM environment where the
110 * domain might change. */
111 static int user_set_domain
= 0;
112 static struct bus_type ap_bus_type
;
114 /* Adapter interrupt definitions */
115 static void ap_interrupt_handler(struct airq_struct
*airq
);
117 static int ap_airq_flag
;
119 static struct airq_struct ap_airq
= {
120 .handler
= ap_interrupt_handler
,
125 * ap_using_interrupts() - Returns non-zero if interrupt support is
128 static inline int ap_using_interrupts(void)
134 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
136 * Returns the address of the local-summary-indicator of the adapter
137 * interrupt handler for AP, or NULL if adapter interrupts are not
140 void *ap_airq_ptr(void)
142 if (ap_using_interrupts())
143 return ap_airq
.lsi_ptr
;
148 * ap_interrupts_available(): Test if AP interrupts are available.
150 * Returns 1 if AP interrupts are available.
152 static int ap_interrupts_available(void)
154 return test_facility(65);
158 * ap_configuration_available(): Test if AP configuration
159 * information is available.
161 * Returns 1 if AP configuration information is available.
163 static int ap_configuration_available(void)
165 return test_facility(12);
169 * ap_apft_available(): Test if AP facilities test (APFT)
170 * facility is available.
172 * Returns 1 if APFT is is available.
174 static int ap_apft_available(void)
176 return test_facility(15);
180 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
182 * Returns 1 if the QACT subfunction is available.
184 static inline int ap_qact_available(void)
186 if (ap_configuration
)
187 return ap_configuration
->qact
;
192 * ap_test_queue(): Test adjunct processor queue.
193 * @qid: The AP queue number
194 * @tbit: Test facilities bit
195 * @info: Pointer to queue descriptor
197 * Returns AP queue status structure.
199 struct ap_queue_status
ap_test_queue(ap_qid_t qid
,
204 qid
|= 1UL << 23; /* set T bit*/
205 return ap_tapq(qid
, info
);
207 EXPORT_SYMBOL(ap_test_queue
);
210 * ap_query_configuration(): Fetch cryptographic config info
212 * Returns the ap configuration info fetched via PQAP(QCI).
213 * On success 0 is returned, on failure a negative errno
214 * is returned, e.g. if the PQAP(QCI) instruction is not
215 * available, the return value will be -EOPNOTSUPP.
217 int ap_query_configuration(struct ap_config_info
*info
)
219 if (!ap_configuration_available())
225 EXPORT_SYMBOL(ap_query_configuration
);
228 * ap_init_configuration(): Allocate and query configuration array.
230 static void ap_init_configuration(void)
232 if (!ap_configuration_available())
235 ap_configuration
= kzalloc(sizeof(*ap_configuration
), GFP_KERNEL
);
236 if (!ap_configuration
)
238 if (ap_query_configuration(ap_configuration
) != 0) {
239 kfree(ap_configuration
);
240 ap_configuration
= NULL
;
246 * ap_test_config(): helper function to extract the nrth bit
247 * within the unsigned int array field.
249 static inline int ap_test_config(unsigned int *field
, unsigned int nr
)
251 return ap_test_bit((field
+ (nr
>> 5)), (nr
& 0x1f));
255 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
258 * Returns 0 if the card is not configured
259 * 1 if the card is configured or
260 * if the configuration information is not available
262 static inline int ap_test_config_card_id(unsigned int id
)
264 if (!ap_configuration
) /* QCI not supported */
266 return ap_test_config(ap_configuration
->apm
, id
);
270 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
271 * @domain AP usage domain ID
273 * Returns 0 if the usage domain is not configured
274 * 1 if the usage domain is configured or
275 * if the configuration information is not available
277 static inline int ap_test_config_domain(unsigned int domain
)
279 if (!ap_configuration
) /* QCI not supported */
281 return ap_test_config(ap_configuration
->aqm
, domain
);
285 * ap_query_queue(): Check if an AP queue is available.
286 * @qid: The AP queue number
287 * @queue_depth: Pointer to queue depth value
288 * @device_type: Pointer to device type value
289 * @facilities: Pointer to facility indicator
291 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
,
292 unsigned int *facilities
)
294 struct ap_queue_status status
;
298 if (!ap_test_config_card_id(AP_QID_CARD(qid
)))
301 status
= ap_test_queue(qid
, ap_apft_available(), &info
);
302 switch (status
.response_code
) {
303 case AP_RESPONSE_NORMAL
:
304 *queue_depth
= (int)(info
& 0xff);
305 *device_type
= (int)((info
>> 24) & 0xff);
306 *facilities
= (unsigned int)(info
>> 32);
307 /* Update maximum domain id */
308 nd
= (info
>> 16) & 0xff;
309 /* if N bit is available, z13 and newer */
310 if ((info
& (1UL << 57)) && nd
> 0)
311 ap_max_domain_id
= nd
;
312 else /* older machine types */
313 ap_max_domain_id
= 15;
314 switch (*device_type
) {
315 /* For CEX2 and CEX3 the available functions
316 * are not refrected by the facilities bits.
317 * Instead it is coded into the type. So here
318 * modify the function bits based on the type.
320 case AP_DEVICE_TYPE_CEX2A
:
321 case AP_DEVICE_TYPE_CEX3A
:
322 *facilities
|= 0x08000000;
324 case AP_DEVICE_TYPE_CEX2C
:
325 case AP_DEVICE_TYPE_CEX3C
:
326 *facilities
|= 0x10000000;
332 case AP_RESPONSE_Q_NOT_AVAIL
:
333 case AP_RESPONSE_DECONFIGURED
:
334 case AP_RESPONSE_CHECKSTOPPED
:
335 case AP_RESPONSE_INVALID_ADDRESS
:
337 case AP_RESPONSE_RESET_IN_PROGRESS
:
338 case AP_RESPONSE_OTHERWISE_CHANGED
:
339 case AP_RESPONSE_BUSY
:
346 void ap_wait(enum ap_wait wait
)
352 case AP_WAIT_INTERRUPT
:
353 if (ap_using_interrupts())
355 if (ap_poll_kthread
) {
356 wake_up(&ap_poll_wait
);
360 case AP_WAIT_TIMEOUT
:
361 spin_lock_bh(&ap_poll_timer_lock
);
362 if (!hrtimer_is_queued(&ap_poll_timer
)) {
363 hr_time
= poll_timeout
;
364 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
365 hrtimer_restart(&ap_poll_timer
);
367 spin_unlock_bh(&ap_poll_timer_lock
);
376 * ap_request_timeout(): Handling of request timeouts
377 * @t: timer making this callback
379 * Handles request timeouts.
381 void ap_request_timeout(struct timer_list
*t
)
383 struct ap_queue
*aq
= from_timer(aq
, t
, timeout
);
387 spin_lock_bh(&aq
->lock
);
388 ap_wait(ap_sm_event(aq
, AP_EVENT_TIMEOUT
));
389 spin_unlock_bh(&aq
->lock
);
393 * ap_poll_timeout(): AP receive polling for finished AP requests.
394 * @unused: Unused pointer.
396 * Schedules the AP tasklet using a high resolution timer.
398 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
400 if (!ap_suspend_flag
)
401 tasklet_schedule(&ap_tasklet
);
402 return HRTIMER_NORESTART
;
406 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
407 * @airq: pointer to adapter interrupt descriptor
409 static void ap_interrupt_handler(struct airq_struct
*airq
)
411 inc_irq_stat(IRQIO_APB
);
412 if (!ap_suspend_flag
)
413 tasklet_schedule(&ap_tasklet
);
417 * ap_tasklet_fn(): Tasklet to poll all AP devices.
418 * @dummy: Unused variable
420 * Poll all AP devices on the bus.
422 static void ap_tasklet_fn(unsigned long dummy
)
426 enum ap_wait wait
= AP_WAIT_NONE
;
428 /* Reset the indicator if interrupts are used. Thus new interrupts can
429 * be received. Doing it in the beginning of the tasklet is therefor
430 * important that no requests on any AP get lost.
432 if (ap_using_interrupts())
433 xchg(ap_airq
.lsi_ptr
, 0);
435 spin_lock_bh(&ap_list_lock
);
436 for_each_ap_card(ac
) {
437 for_each_ap_queue(aq
, ac
) {
438 spin_lock_bh(&aq
->lock
);
439 wait
= min(wait
, ap_sm_event_loop(aq
, AP_EVENT_POLL
));
440 spin_unlock_bh(&aq
->lock
);
443 spin_unlock_bh(&ap_list_lock
);
448 static int ap_pending_requests(void)
453 spin_lock_bh(&ap_list_lock
);
454 for_each_ap_card(ac
) {
455 for_each_ap_queue(aq
, ac
) {
456 if (aq
->queue_count
== 0)
458 spin_unlock_bh(&ap_list_lock
);
462 spin_unlock_bh(&ap_list_lock
);
467 * ap_poll_thread(): Thread that polls for finished requests.
468 * @data: Unused pointer
470 * AP bus poll thread. The purpose of this thread is to poll for
471 * finished requests in a loop if there is a "free" cpu - that is
472 * a cpu that doesn't have anything better to do. The polling stops
473 * as soon as there is another task or if all messages have been
476 static int ap_poll_thread(void *data
)
478 DECLARE_WAITQUEUE(wait
, current
);
480 set_user_nice(current
, MAX_NICE
);
482 while (!kthread_should_stop()) {
483 add_wait_queue(&ap_poll_wait
, &wait
);
484 set_current_state(TASK_INTERRUPTIBLE
);
485 if (ap_suspend_flag
|| !ap_pending_requests()) {
489 set_current_state(TASK_RUNNING
);
490 remove_wait_queue(&ap_poll_wait
, &wait
);
491 if (need_resched()) {
502 static int ap_poll_thread_start(void)
506 if (ap_using_interrupts() || ap_poll_kthread
)
508 mutex_lock(&ap_poll_thread_mutex
);
509 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
510 rc
= PTR_RET(ap_poll_kthread
);
512 ap_poll_kthread
= NULL
;
513 mutex_unlock(&ap_poll_thread_mutex
);
517 static void ap_poll_thread_stop(void)
519 if (!ap_poll_kthread
)
521 mutex_lock(&ap_poll_thread_mutex
);
522 kthread_stop(ap_poll_kthread
);
523 ap_poll_kthread
= NULL
;
524 mutex_unlock(&ap_poll_thread_mutex
);
527 #define is_card_dev(x) ((x)->parent == ap_root_device)
528 #define is_queue_dev(x) ((x)->parent != ap_root_device)
532 * @dev: Pointer to device
533 * @drv: Pointer to device_driver
535 * AP bus driver registration/unregistration.
537 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
539 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
540 struct ap_device_id
*id
;
543 * Compare device type of the device with the list of
544 * supported types of the device_driver.
546 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
547 if (is_card_dev(dev
) &&
548 id
->match_flags
& AP_DEVICE_ID_MATCH_CARD_TYPE
&&
549 id
->dev_type
== to_ap_dev(dev
)->device_type
)
551 if (is_queue_dev(dev
) &&
552 id
->match_flags
& AP_DEVICE_ID_MATCH_QUEUE_TYPE
&&
553 id
->dev_type
== to_ap_dev(dev
)->device_type
)
560 * ap_uevent(): Uevent function for AP devices.
561 * @dev: Pointer to device
562 * @env: Pointer to kobj_uevent_env
564 * It sets up a single environment variable DEV_TYPE which contains the
565 * hardware device type.
567 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
569 struct ap_device
*ap_dev
= to_ap_dev(dev
);
575 /* Set up DEV_TYPE environment variable. */
576 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
581 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
586 static int ap_dev_suspend(struct device
*dev
)
588 struct ap_device
*ap_dev
= to_ap_dev(dev
);
590 if (ap_dev
->drv
&& ap_dev
->drv
->suspend
)
591 ap_dev
->drv
->suspend(ap_dev
);
595 static int ap_dev_resume(struct device
*dev
)
597 struct ap_device
*ap_dev
= to_ap_dev(dev
);
599 if (ap_dev
->drv
&& ap_dev
->drv
->resume
)
600 ap_dev
->drv
->resume(ap_dev
);
604 static void ap_bus_suspend(void)
606 AP_DBF(DBF_DEBUG
, "ap_bus_suspend running\n");
610 * Disable scanning for devices, thus we do not want to scan
611 * for them after removing.
613 flush_work(&ap_scan_work
);
614 tasklet_disable(&ap_tasklet
);
617 static int __ap_card_devices_unregister(struct device
*dev
, void *dummy
)
619 if (is_card_dev(dev
))
620 device_unregister(dev
);
624 static int __ap_queue_devices_unregister(struct device
*dev
, void *dummy
)
626 if (is_queue_dev(dev
))
627 device_unregister(dev
);
631 static int __ap_queue_devices_with_id_unregister(struct device
*dev
, void *data
)
633 if (is_queue_dev(dev
) &&
634 AP_QID_CARD(to_ap_queue(dev
)->qid
) == (int)(long) data
)
635 device_unregister(dev
);
639 static void ap_bus_resume(void)
643 AP_DBF(DBF_DEBUG
, "ap_bus_resume running\n");
645 /* remove all queue devices */
646 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
647 __ap_queue_devices_unregister
);
648 /* remove all card devices */
649 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
650 __ap_card_devices_unregister
);
652 /* Reset thin interrupt setting */
653 if (ap_interrupts_available() && !ap_using_interrupts()) {
654 rc
= register_adapter_interrupt(&ap_airq
);
655 ap_airq_flag
= (rc
== 0);
657 if (!ap_interrupts_available() && ap_using_interrupts()) {
658 unregister_adapter_interrupt(&ap_airq
);
662 if (!user_set_domain
)
663 ap_domain_index
= -1;
664 /* Get things going again */
667 xchg(ap_airq
.lsi_ptr
, 0);
668 tasklet_enable(&ap_tasklet
);
669 queue_work(system_long_wq
, &ap_scan_work
);
672 static int ap_power_event(struct notifier_block
*this, unsigned long event
,
676 case PM_HIBERNATION_PREPARE
:
677 case PM_SUSPEND_PREPARE
:
680 case PM_POST_HIBERNATION
:
681 case PM_POST_SUSPEND
:
689 static struct notifier_block ap_power_notifier
= {
690 .notifier_call
= ap_power_event
,
693 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops
, ap_dev_suspend
, ap_dev_resume
);
695 static struct bus_type ap_bus_type
= {
697 .match
= &ap_bus_match
,
698 .uevent
= &ap_uevent
,
699 .pm
= &ap_bus_pm_ops
,
702 static int ap_device_probe(struct device
*dev
)
704 struct ap_device
*ap_dev
= to_ap_dev(dev
);
705 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
708 /* Add queue/card to list of active queues/cards */
709 spin_lock_bh(&ap_list_lock
);
710 if (is_card_dev(dev
))
711 list_add(&to_ap_card(dev
)->list
, &ap_card_list
);
713 list_add(&to_ap_queue(dev
)->list
,
714 &to_ap_queue(dev
)->card
->queues
);
715 spin_unlock_bh(&ap_list_lock
);
717 ap_dev
->drv
= ap_drv
;
718 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
721 spin_lock_bh(&ap_list_lock
);
722 if (is_card_dev(dev
))
723 list_del_init(&to_ap_card(dev
)->list
);
725 list_del_init(&to_ap_queue(dev
)->list
);
726 spin_unlock_bh(&ap_list_lock
);
733 static int ap_device_remove(struct device
*dev
)
735 struct ap_device
*ap_dev
= to_ap_dev(dev
);
736 struct ap_driver
*ap_drv
= ap_dev
->drv
;
739 ap_drv
->remove(ap_dev
);
741 /* Remove queue/card from list of active queues/cards */
742 spin_lock_bh(&ap_list_lock
);
743 if (is_card_dev(dev
))
744 list_del_init(&to_ap_card(dev
)->list
);
746 list_del_init(&to_ap_queue(dev
)->list
);
747 spin_unlock_bh(&ap_list_lock
);
752 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
755 struct device_driver
*drv
= &ap_drv
->driver
;
760 drv
->bus
= &ap_bus_type
;
761 drv
->probe
= ap_device_probe
;
762 drv
->remove
= ap_device_remove
;
765 return driver_register(drv
);
767 EXPORT_SYMBOL(ap_driver_register
);
769 void ap_driver_unregister(struct ap_driver
*ap_drv
)
771 driver_unregister(&ap_drv
->driver
);
773 EXPORT_SYMBOL(ap_driver_unregister
);
775 void ap_bus_force_rescan(void)
779 /* processing a asynchronous bus rescan */
780 del_timer(&ap_config_timer
);
781 queue_work(system_long_wq
, &ap_scan_work
);
782 flush_work(&ap_scan_work
);
784 EXPORT_SYMBOL(ap_bus_force_rescan
);
789 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
791 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
794 static ssize_t
ap_domain_store(struct bus_type
*bus
,
795 const char *buf
, size_t count
)
799 if (sscanf(buf
, "%i\n", &domain
) != 1 ||
800 domain
< 0 || domain
> ap_max_domain_id
)
802 spin_lock_bh(&ap_domain_lock
);
803 ap_domain_index
= domain
;
804 spin_unlock_bh(&ap_domain_lock
);
806 AP_DBF(DBF_DEBUG
, "stored new default domain=%d\n", domain
);
811 static BUS_ATTR(ap_domain
, 0644, ap_domain_show
, ap_domain_store
);
813 static ssize_t
ap_control_domain_mask_show(struct bus_type
*bus
, char *buf
)
815 if (!ap_configuration
) /* QCI not supported */
816 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
818 return snprintf(buf
, PAGE_SIZE
,
819 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
820 ap_configuration
->adm
[0], ap_configuration
->adm
[1],
821 ap_configuration
->adm
[2], ap_configuration
->adm
[3],
822 ap_configuration
->adm
[4], ap_configuration
->adm
[5],
823 ap_configuration
->adm
[6], ap_configuration
->adm
[7]);
826 static BUS_ATTR(ap_control_domain_mask
, 0444,
827 ap_control_domain_mask_show
, NULL
);
829 static ssize_t
ap_usage_domain_mask_show(struct bus_type
*bus
, char *buf
)
831 if (!ap_configuration
) /* QCI not supported */
832 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
834 return snprintf(buf
, PAGE_SIZE
,
835 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
836 ap_configuration
->aqm
[0], ap_configuration
->aqm
[1],
837 ap_configuration
->aqm
[2], ap_configuration
->aqm
[3],
838 ap_configuration
->aqm
[4], ap_configuration
->aqm
[5],
839 ap_configuration
->aqm
[6], ap_configuration
->aqm
[7]);
842 static BUS_ATTR(ap_usage_domain_mask
, 0444,
843 ap_usage_domain_mask_show
, NULL
);
845 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
847 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
850 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
852 return snprintf(buf
, PAGE_SIZE
, "%d\n",
853 ap_using_interrupts() ? 1 : 0);
856 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
858 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
859 const char *buf
, size_t count
)
863 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
865 ap_config_time
= time
;
866 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
870 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
872 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
874 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
877 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
878 const char *buf
, size_t count
)
882 if (sscanf(buf
, "%d\n", &flag
) != 1)
885 rc
= ap_poll_thread_start();
889 ap_poll_thread_stop();
893 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
895 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
897 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
900 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
903 unsigned long long time
;
906 /* 120 seconds = maximum poll interval */
907 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
908 time
> 120000000000ULL)
911 hr_time
= poll_timeout
;
913 spin_lock_bh(&ap_poll_timer_lock
);
914 hrtimer_cancel(&ap_poll_timer
);
915 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
916 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
917 spin_unlock_bh(&ap_poll_timer_lock
);
922 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
924 static ssize_t
ap_max_domain_id_show(struct bus_type
*bus
, char *buf
)
928 if (ap_configuration
)
929 max_domain_id
= ap_max_domain_id
? : -1;
932 return snprintf(buf
, PAGE_SIZE
, "%d\n", max_domain_id
);
935 static BUS_ATTR(ap_max_domain_id
, 0444, ap_max_domain_id_show
, NULL
);
937 static struct bus_attribute
*const ap_bus_attrs
[] = {
939 &bus_attr_ap_control_domain_mask
,
940 &bus_attr_ap_usage_domain_mask
,
941 &bus_attr_config_time
,
942 &bus_attr_poll_thread
,
943 &bus_attr_ap_interrupts
,
944 &bus_attr_poll_timeout
,
945 &bus_attr_ap_max_domain_id
,
950 * ap_select_domain(): Select an AP domain.
952 * Pick one of the 16 AP domains.
954 static int ap_select_domain(void)
956 int count
, max_count
, best_domain
;
957 struct ap_queue_status status
;
961 * We want to use a single domain. Either the one specified with
962 * the "domain=" parameter or the domain with the maximum number
965 spin_lock_bh(&ap_domain_lock
);
966 if (ap_domain_index
>= 0) {
967 /* Domain has already been selected. */
968 spin_unlock_bh(&ap_domain_lock
);
973 for (i
= 0; i
< AP_DOMAINS
; i
++) {
974 if (!ap_test_config_domain(i
))
977 for (j
= 0; j
< AP_DEVICES
; j
++) {
978 if (!ap_test_config_card_id(j
))
980 status
= ap_test_queue(AP_MKQID(j
, i
),
983 if (status
.response_code
!= AP_RESPONSE_NORMAL
)
987 if (count
> max_count
) {
992 if (best_domain
>= 0){
993 ap_domain_index
= best_domain
;
994 AP_DBF(DBF_DEBUG
, "new ap_domain_index=%d\n", ap_domain_index
);
995 spin_unlock_bh(&ap_domain_lock
);
998 spin_unlock_bh(&ap_domain_lock
);
1003 * This function checks the type and returns either 0 for not
1004 * supported or the highest compatible type value (which may
1005 * include the input type value).
1007 static int ap_get_compatible_type(ap_qid_t qid
, int rawtype
, unsigned int func
)
1011 /* < CEX2A is not supported */
1012 if (rawtype
< AP_DEVICE_TYPE_CEX2A
)
1014 /* up to CEX6 known and fully supported */
1015 if (rawtype
<= AP_DEVICE_TYPE_CEX6
)
1018 * unknown new type > CEX6, check for compatibility
1019 * to the highest known and supported type which is
1020 * currently CEX6 with the help of the QACT function.
1022 if (ap_qact_available()) {
1023 struct ap_queue_status status
;
1024 union ap_qact_ap_info apinfo
= {0};
1026 apinfo
.mode
= (func
>> 26) & 0x07;
1027 apinfo
.cat
= AP_DEVICE_TYPE_CEX6
;
1028 status
= ap_qact(qid
, 0, &apinfo
);
1029 if (status
.response_code
== AP_RESPONSE_NORMAL
1030 && apinfo
.cat
>= AP_DEVICE_TYPE_CEX2A
1031 && apinfo
.cat
<= AP_DEVICE_TYPE_CEX6
)
1032 comp_type
= apinfo
.cat
;
1035 AP_DBF(DBF_WARN
, "queue=%02x.%04x unable to map type %d\n",
1036 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
), rawtype
);
1037 else if (comp_type
!= rawtype
)
1038 AP_DBF(DBF_INFO
, "queue=%02x.%04x map type %d to %d\n",
1039 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
), rawtype
, comp_type
);
1044 * helper function to be used with bus_find_dev
1045 * matches for the card device with the given id
1047 static int __match_card_device_with_id(struct device
*dev
, void *data
)
1049 return is_card_dev(dev
) && to_ap_card(dev
)->id
== (int)(long) data
;
1052 /* helper function to be used with bus_find_dev
1053 * matches for the queue device with a given qid
1055 static int __match_queue_device_with_qid(struct device
*dev
, void *data
)
1057 return is_queue_dev(dev
) && to_ap_queue(dev
)->qid
== (int)(long) data
;
1061 * ap_scan_bus(): Scan the AP bus for new devices
1062 * Runs periodically, workqueue timer (ap_config_time)
1064 static void ap_scan_bus(struct work_struct
*unused
)
1066 struct ap_queue
*aq
;
1070 int comp_type
, depth
= 0, type
= 0;
1071 unsigned int func
= 0;
1072 int rc
, id
, dom
, borked
, domains
, defdomdevs
= 0;
1074 AP_DBF(DBF_DEBUG
, "ap_scan_bus running\n");
1076 ap_query_configuration(ap_configuration
);
1077 if (ap_select_domain() != 0)
1080 for (id
= 0; id
< AP_DEVICES
; id
++) {
1081 /* check if device is registered */
1082 dev
= bus_find_device(&ap_bus_type
, NULL
,
1084 __match_card_device_with_id
);
1085 ac
= dev
? to_ap_card(dev
) : NULL
;
1086 if (!ap_test_config_card_id(id
)) {
1088 /* Card device has been removed from
1089 * configuration, remove the belonging
1092 bus_for_each_dev(&ap_bus_type
, NULL
,
1094 __ap_queue_devices_with_id_unregister
);
1095 /* now remove the card device */
1096 device_unregister(dev
);
1101 /* According to the configuration there should be a card
1102 * device, so check if there is at least one valid queue
1103 * and maybe create queue devices and the card device.
1106 for (dom
= 0; dom
< AP_DOMAINS
; dom
++) {
1107 qid
= AP_MKQID(id
, dom
);
1108 dev
= bus_find_device(&ap_bus_type
, NULL
,
1110 __match_queue_device_with_qid
);
1111 aq
= dev
? to_ap_queue(dev
) : NULL
;
1112 if (!ap_test_config_domain(dom
)) {
1114 /* Queue device exists but has been
1115 * removed from configuration.
1117 device_unregister(dev
);
1122 rc
= ap_query_queue(qid
, &depth
, &type
, &func
);
1124 spin_lock_bh(&aq
->lock
);
1125 if (rc
== -ENODEV
||
1126 /* adapter reconfiguration */
1127 (ac
&& ac
->functions
!= func
))
1128 aq
->state
= AP_STATE_BORKED
;
1129 borked
= aq
->state
== AP_STATE_BORKED
;
1130 spin_unlock_bh(&aq
->lock
);
1131 if (borked
) /* Remove broken device */
1132 device_unregister(dev
);
1136 if (dom
== ap_domain_index
)
1143 /* a new queue device is needed, check out comp type */
1144 comp_type
= ap_get_compatible_type(qid
, type
, func
);
1147 /* maybe a card device needs to be created first */
1149 ac
= ap_card_create(id
, depth
, type
,
1153 ac
->ap_dev
.device
.bus
= &ap_bus_type
;
1154 ac
->ap_dev
.device
.parent
= ap_root_device
;
1155 dev_set_name(&ac
->ap_dev
.device
,
1157 /* Register card with AP bus */
1158 rc
= device_register(&ac
->ap_dev
.device
);
1160 put_device(&ac
->ap_dev
.device
);
1164 /* get it and thus adjust reference counter */
1165 get_device(&ac
->ap_dev
.device
);
1167 /* now create the new queue device */
1168 aq
= ap_queue_create(qid
, comp_type
);
1172 aq
->ap_dev
.device
.bus
= &ap_bus_type
;
1173 aq
->ap_dev
.device
.parent
= &ac
->ap_dev
.device
;
1174 dev_set_name(&aq
->ap_dev
.device
,
1175 "%02x.%04x", id
, dom
);
1176 /* Start with a device reset */
1177 spin_lock_bh(&aq
->lock
);
1178 ap_wait(ap_sm_event(aq
, AP_EVENT_POLL
));
1179 spin_unlock_bh(&aq
->lock
);
1180 /* Register device */
1181 rc
= device_register(&aq
->ap_dev
.device
);
1183 put_device(&aq
->ap_dev
.device
);
1187 if (dom
== ap_domain_index
)
1189 } /* end domain loop */
1191 /* remove card dev if there are no queue devices */
1193 device_unregister(&ac
->ap_dev
.device
);
1194 put_device(&ac
->ap_dev
.device
);
1196 } /* end device loop */
1199 AP_DBF(DBF_INFO
, "no queue device with default domain %d available\n",
1203 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1206 static void ap_config_timeout(struct timer_list
*unused
)
1208 if (ap_suspend_flag
)
1210 queue_work(system_long_wq
, &ap_scan_work
);
1213 static void ap_reset_all(void)
1217 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1218 if (!ap_test_config_domain(i
))
1220 for (j
= 0; j
< AP_DEVICES
; j
++) {
1221 if (!ap_test_config_card_id(j
))
1223 ap_rapq(AP_MKQID(j
, i
));
1228 static struct reset_call ap_reset_call
= {
1232 int __init
ap_debug_init(void)
1234 ap_dbf_info
= debug_register("ap", 1, 1,
1235 DBF_MAX_SPRINTF_ARGS
* sizeof(long));
1236 debug_register_view(ap_dbf_info
, &debug_sprintf_view
);
1237 debug_set_level(ap_dbf_info
, DBF_ERR
);
1242 void ap_debug_exit(void)
1244 debug_unregister(ap_dbf_info
);
1248 * ap_module_init(): The module initialization code.
1250 * Initializes the module.
1252 int __init
ap_module_init(void)
1257 rc
= ap_debug_init();
1261 if (ap_instructions_available() != 0) {
1262 pr_warn("The hardware system does not support AP instructions\n");
1266 /* Get AP configuration data if available */
1267 ap_init_configuration();
1269 if (ap_configuration
)
1271 ap_max_domain_id
? ap_max_domain_id
: AP_DOMAINS
- 1;
1274 if (ap_domain_index
< -1 || ap_domain_index
> max_domain_id
) {
1275 pr_warn("%d is not a valid cryptographic domain\n",
1277 ap_domain_index
= -1;
1279 /* In resume callback we need to know if the user had set the domain.
1280 * If so, we can not just reset it.
1282 if (ap_domain_index
>= 0)
1283 user_set_domain
= 1;
1285 if (ap_interrupts_available()) {
1286 rc
= register_adapter_interrupt(&ap_airq
);
1287 ap_airq_flag
= (rc
== 0);
1290 register_reset_call(&ap_reset_call
);
1292 /* Create /sys/bus/ap. */
1293 rc
= bus_register(&ap_bus_type
);
1296 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1297 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1302 /* Create /sys/devices/ap. */
1303 ap_root_device
= root_device_register("ap");
1304 rc
= PTR_RET(ap_root_device
);
1308 /* Setup the AP bus rescan timer. */
1309 timer_setup(&ap_config_timer
, ap_config_timeout
, 0);
1312 * Setup the high resultion poll timer.
1313 * If we are running under z/VM adjust polling to z/VM polling rate.
1316 poll_timeout
= 1500000;
1317 spin_lock_init(&ap_poll_timer_lock
);
1318 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1319 ap_poll_timer
.function
= ap_poll_timeout
;
1321 /* Start the low priority AP bus poll thread. */
1322 if (ap_thread_flag
) {
1323 rc
= ap_poll_thread_start();
1328 rc
= register_pm_notifier(&ap_power_notifier
);
1332 queue_work(system_long_wq
, &ap_scan_work
);
1338 ap_poll_thread_stop();
1340 hrtimer_cancel(&ap_poll_timer
);
1341 root_device_unregister(ap_root_device
);
1344 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1345 bus_unregister(&ap_bus_type
);
1347 unregister_reset_call(&ap_reset_call
);
1348 if (ap_using_interrupts())
1349 unregister_adapter_interrupt(&ap_airq
);
1350 kfree(ap_configuration
);
1353 device_initcall(ap_module_init
);