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/module.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>
59 MODULE_AUTHOR("IBM Corporation");
60 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
61 "Copyright IBM Corp. 2006, 2012");
62 MODULE_LICENSE("GPL");
63 MODULE_ALIAS_CRYPTO("z90crypt");
68 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
69 static DEFINE_SPINLOCK(ap_domain_lock
);
70 module_param_named(domain
, ap_domain_index
, int, S_IRUSR
|S_IRGRP
);
71 MODULE_PARM_DESC(domain
, "domain index for ap devices");
72 EXPORT_SYMBOL(ap_domain_index
);
74 static int ap_thread_flag
= 0;
75 module_param_named(poll_thread
, ap_thread_flag
, int, S_IRUSR
|S_IRGRP
);
76 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
78 static struct device
*ap_root_device
;
80 DEFINE_SPINLOCK(ap_list_lock
);
81 LIST_HEAD(ap_card_list
);
83 static struct ap_config_info
*ap_configuration
;
84 static bool initialised
;
87 * AP bus related debug feature things.
89 static struct dentry
*ap_dbf_root
;
90 debug_info_t
*ap_dbf_info
;
93 * Workqueue timer for bus rescan.
95 static struct timer_list ap_config_timer
;
96 static int ap_config_time
= AP_CONFIG_TIME
;
97 static void ap_scan_bus(struct work_struct
*);
98 static DECLARE_WORK(ap_scan_work
, ap_scan_bus
);
101 * Tasklet & timer for AP request polling and interrupts
103 static void ap_tasklet_fn(unsigned long);
104 static DECLARE_TASKLET(ap_tasklet
, ap_tasklet_fn
, 0);
105 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
106 static struct task_struct
*ap_poll_kthread
= NULL
;
107 static DEFINE_MUTEX(ap_poll_thread_mutex
);
108 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
109 static struct hrtimer ap_poll_timer
;
110 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
111 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
112 static unsigned long long poll_timeout
= 250000;
115 static int ap_suspend_flag
;
116 /* Maximum domain id */
117 static int ap_max_domain_id
;
118 /* Flag to check if domain was set through module parameter domain=. This is
119 * important when supsend and resume is done in a z/VM environment where the
120 * domain might change. */
121 static int user_set_domain
= 0;
122 static struct bus_type ap_bus_type
;
124 /* Adapter interrupt definitions */
125 static void ap_interrupt_handler(struct airq_struct
*airq
);
127 static int ap_airq_flag
;
129 static struct airq_struct ap_airq
= {
130 .handler
= ap_interrupt_handler
,
135 * ap_using_interrupts() - Returns non-zero if interrupt support is
138 static inline int ap_using_interrupts(void)
144 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
146 * Returns the address of the local-summary-indicator of the adapter
147 * interrupt handler for AP, or NULL if adapter interrupts are not
150 void *ap_airq_ptr(void)
152 if (ap_using_interrupts())
153 return ap_airq
.lsi_ptr
;
158 * ap_interrupts_available(): Test if AP interrupts are available.
160 * Returns 1 if AP interrupts are available.
162 static int ap_interrupts_available(void)
164 return test_facility(65);
168 * ap_configuration_available(): Test if AP configuration
169 * information is available.
171 * Returns 1 if AP configuration information is available.
173 static int ap_configuration_available(void)
175 return test_facility(12);
179 * ap_test_queue(): Test adjunct processor queue.
180 * @qid: The AP queue number
181 * @info: Pointer to queue descriptor
183 * Returns AP queue status structure.
185 static inline struct ap_queue_status
186 ap_test_queue(ap_qid_t qid
, unsigned long *info
)
188 if (test_facility(15))
189 qid
|= 1UL << 23; /* set APFT T bit*/
190 return ap_tapq(qid
, info
);
193 static inline int ap_query_configuration(void)
195 if (!ap_configuration
)
197 return ap_qci(ap_configuration
);
201 * ap_init_configuration(): Allocate and query configuration array.
203 static void ap_init_configuration(void)
205 if (!ap_configuration_available())
208 ap_configuration
= kzalloc(sizeof(*ap_configuration
), GFP_KERNEL
);
209 if (!ap_configuration
)
211 if (ap_query_configuration() != 0) {
212 kfree(ap_configuration
);
213 ap_configuration
= NULL
;
219 * ap_test_config(): helper function to extract the nrth bit
220 * within the unsigned int array field.
222 static inline int ap_test_config(unsigned int *field
, unsigned int nr
)
224 return ap_test_bit((field
+ (nr
>> 5)), (nr
& 0x1f));
228 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
231 * Returns 0 if the card is not configured
232 * 1 if the card is configured or
233 * if the configuration information is not available
235 static inline int ap_test_config_card_id(unsigned int id
)
237 if (!ap_configuration
) /* QCI not supported */
239 return ap_test_config(ap_configuration
->apm
, id
);
243 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
244 * @domain AP usage domain ID
246 * Returns 0 if the usage domain is not configured
247 * 1 if the usage domain is configured or
248 * if the configuration information is not available
250 static inline int ap_test_config_domain(unsigned int domain
)
252 if (!ap_configuration
) /* QCI not supported */
254 return ap_test_config(ap_configuration
->aqm
, domain
);
258 * ap_query_queue(): Check if an AP queue is available.
259 * @qid: The AP queue number
260 * @queue_depth: Pointer to queue depth value
261 * @device_type: Pointer to device type value
262 * @facilities: Pointer to facility indicator
264 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
,
265 unsigned int *facilities
)
267 struct ap_queue_status status
;
271 if (!ap_test_config_card_id(AP_QID_CARD(qid
)))
274 status
= ap_test_queue(qid
, &info
);
275 switch (status
.response_code
) {
276 case AP_RESPONSE_NORMAL
:
277 *queue_depth
= (int)(info
& 0xff);
278 *device_type
= (int)((info
>> 24) & 0xff);
279 *facilities
= (unsigned int)(info
>> 32);
280 /* Update maximum domain id */
281 nd
= (info
>> 16) & 0xff;
282 /* if N bit is available, z13 and newer */
283 if ((info
& (1UL << 57)) && nd
> 0)
284 ap_max_domain_id
= nd
;
285 else /* older machine types */
286 ap_max_domain_id
= 15;
287 switch (*device_type
) {
288 /* For CEX2 and CEX3 the available functions
289 * are not refrected by the facilities bits.
290 * Instead it is coded into the type. So here
291 * modify the function bits based on the type.
293 case AP_DEVICE_TYPE_CEX2A
:
294 case AP_DEVICE_TYPE_CEX3A
:
295 *facilities
|= 0x08000000;
297 case AP_DEVICE_TYPE_CEX2C
:
298 case AP_DEVICE_TYPE_CEX3C
:
299 *facilities
|= 0x10000000;
305 case AP_RESPONSE_Q_NOT_AVAIL
:
306 case AP_RESPONSE_DECONFIGURED
:
307 case AP_RESPONSE_CHECKSTOPPED
:
308 case AP_RESPONSE_INVALID_ADDRESS
:
310 case AP_RESPONSE_RESET_IN_PROGRESS
:
311 case AP_RESPONSE_OTHERWISE_CHANGED
:
312 case AP_RESPONSE_BUSY
:
319 void ap_wait(enum ap_wait wait
)
325 case AP_WAIT_INTERRUPT
:
326 if (ap_using_interrupts())
328 if (ap_poll_kthread
) {
329 wake_up(&ap_poll_wait
);
333 case AP_WAIT_TIMEOUT
:
334 spin_lock_bh(&ap_poll_timer_lock
);
335 if (!hrtimer_is_queued(&ap_poll_timer
)) {
336 hr_time
= poll_timeout
;
337 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
338 hrtimer_restart(&ap_poll_timer
);
340 spin_unlock_bh(&ap_poll_timer_lock
);
349 * ap_request_timeout(): Handling of request timeouts
350 * @data: Holds the AP device.
352 * Handles request timeouts.
354 void ap_request_timeout(unsigned long data
)
356 struct ap_queue
*aq
= (struct ap_queue
*) data
;
360 spin_lock_bh(&aq
->lock
);
361 ap_wait(ap_sm_event(aq
, AP_EVENT_TIMEOUT
));
362 spin_unlock_bh(&aq
->lock
);
366 * ap_poll_timeout(): AP receive polling for finished AP requests.
367 * @unused: Unused pointer.
369 * Schedules the AP tasklet using a high resolution timer.
371 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
373 if (!ap_suspend_flag
)
374 tasklet_schedule(&ap_tasklet
);
375 return HRTIMER_NORESTART
;
379 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
380 * @airq: pointer to adapter interrupt descriptor
382 static void ap_interrupt_handler(struct airq_struct
*airq
)
384 inc_irq_stat(IRQIO_APB
);
385 if (!ap_suspend_flag
)
386 tasklet_schedule(&ap_tasklet
);
390 * ap_tasklet_fn(): Tasklet to poll all AP devices.
391 * @dummy: Unused variable
393 * Poll all AP devices on the bus.
395 static void ap_tasklet_fn(unsigned long dummy
)
399 enum ap_wait wait
= AP_WAIT_NONE
;
401 /* Reset the indicator if interrupts are used. Thus new interrupts can
402 * be received. Doing it in the beginning of the tasklet is therefor
403 * important that no requests on any AP get lost.
405 if (ap_using_interrupts())
406 xchg(ap_airq
.lsi_ptr
, 0);
408 spin_lock_bh(&ap_list_lock
);
409 for_each_ap_card(ac
) {
410 for_each_ap_queue(aq
, ac
) {
411 spin_lock_bh(&aq
->lock
);
412 wait
= min(wait
, ap_sm_event_loop(aq
, AP_EVENT_POLL
));
413 spin_unlock_bh(&aq
->lock
);
416 spin_unlock_bh(&ap_list_lock
);
421 static int ap_pending_requests(void)
426 spin_lock_bh(&ap_list_lock
);
427 for_each_ap_card(ac
) {
428 for_each_ap_queue(aq
, ac
) {
429 if (aq
->queue_count
== 0)
431 spin_unlock_bh(&ap_list_lock
);
435 spin_unlock_bh(&ap_list_lock
);
440 * ap_poll_thread(): Thread that polls for finished requests.
441 * @data: Unused pointer
443 * AP bus poll thread. The purpose of this thread is to poll for
444 * finished requests in a loop if there is a "free" cpu - that is
445 * a cpu that doesn't have anything better to do. The polling stops
446 * as soon as there is another task or if all messages have been
449 static int ap_poll_thread(void *data
)
451 DECLARE_WAITQUEUE(wait
, current
);
453 set_user_nice(current
, MAX_NICE
);
455 while (!kthread_should_stop()) {
456 add_wait_queue(&ap_poll_wait
, &wait
);
457 set_current_state(TASK_INTERRUPTIBLE
);
458 if (ap_suspend_flag
|| !ap_pending_requests()) {
462 set_current_state(TASK_RUNNING
);
463 remove_wait_queue(&ap_poll_wait
, &wait
);
464 if (need_resched()) {
475 static int ap_poll_thread_start(void)
479 if (ap_using_interrupts() || ap_poll_kthread
)
481 mutex_lock(&ap_poll_thread_mutex
);
482 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
483 rc
= PTR_RET(ap_poll_kthread
);
485 ap_poll_kthread
= NULL
;
486 mutex_unlock(&ap_poll_thread_mutex
);
490 static void ap_poll_thread_stop(void)
492 if (!ap_poll_kthread
)
494 mutex_lock(&ap_poll_thread_mutex
);
495 kthread_stop(ap_poll_kthread
);
496 ap_poll_kthread
= NULL
;
497 mutex_unlock(&ap_poll_thread_mutex
);
500 #define is_card_dev(x) ((x)->parent == ap_root_device)
501 #define is_queue_dev(x) ((x)->parent != ap_root_device)
505 * @dev: Pointer to device
506 * @drv: Pointer to device_driver
508 * AP bus driver registration/unregistration.
510 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
512 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
513 struct ap_device_id
*id
;
516 * Compare device type of the device with the list of
517 * supported types of the device_driver.
519 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
520 if (is_card_dev(dev
) &&
521 id
->match_flags
& AP_DEVICE_ID_MATCH_CARD_TYPE
&&
522 id
->dev_type
== to_ap_dev(dev
)->device_type
)
524 if (is_queue_dev(dev
) &&
525 id
->match_flags
& AP_DEVICE_ID_MATCH_QUEUE_TYPE
&&
526 id
->dev_type
== to_ap_dev(dev
)->device_type
)
533 * ap_uevent(): Uevent function for AP devices.
534 * @dev: Pointer to device
535 * @env: Pointer to kobj_uevent_env
537 * It sets up a single environment variable DEV_TYPE which contains the
538 * hardware device type.
540 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
542 struct ap_device
*ap_dev
= to_ap_dev(dev
);
548 /* Set up DEV_TYPE environment variable. */
549 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
554 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
559 static int ap_dev_suspend(struct device
*dev
)
561 struct ap_device
*ap_dev
= to_ap_dev(dev
);
563 if (ap_dev
->drv
&& ap_dev
->drv
->suspend
)
564 ap_dev
->drv
->suspend(ap_dev
);
568 static int ap_dev_resume(struct device
*dev
)
570 struct ap_device
*ap_dev
= to_ap_dev(dev
);
572 if (ap_dev
->drv
&& ap_dev
->drv
->resume
)
573 ap_dev
->drv
->resume(ap_dev
);
577 static void ap_bus_suspend(void)
579 AP_DBF(DBF_DEBUG
, "ap_bus_suspend running\n");
583 * Disable scanning for devices, thus we do not want to scan
584 * for them after removing.
586 flush_work(&ap_scan_work
);
587 tasklet_disable(&ap_tasklet
);
590 static int __ap_card_devices_unregister(struct device
*dev
, void *dummy
)
592 if (is_card_dev(dev
))
593 device_unregister(dev
);
597 static int __ap_queue_devices_unregister(struct device
*dev
, void *dummy
)
599 if (is_queue_dev(dev
))
600 device_unregister(dev
);
604 static int __ap_queue_devices_with_id_unregister(struct device
*dev
, void *data
)
606 if (is_queue_dev(dev
) &&
607 AP_QID_CARD(to_ap_queue(dev
)->qid
) == (int)(long) data
)
608 device_unregister(dev
);
612 static void ap_bus_resume(void)
616 AP_DBF(DBF_DEBUG
, "ap_bus_resume running\n");
618 /* remove all queue devices */
619 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
620 __ap_queue_devices_unregister
);
621 /* remove all card devices */
622 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
623 __ap_card_devices_unregister
);
625 /* Reset thin interrupt setting */
626 if (ap_interrupts_available() && !ap_using_interrupts()) {
627 rc
= register_adapter_interrupt(&ap_airq
);
628 ap_airq_flag
= (rc
== 0);
630 if (!ap_interrupts_available() && ap_using_interrupts()) {
631 unregister_adapter_interrupt(&ap_airq
);
635 if (!user_set_domain
)
636 ap_domain_index
= -1;
637 /* Get things going again */
640 xchg(ap_airq
.lsi_ptr
, 0);
641 tasklet_enable(&ap_tasklet
);
642 queue_work(system_long_wq
, &ap_scan_work
);
645 static int ap_power_event(struct notifier_block
*this, unsigned long event
,
649 case PM_HIBERNATION_PREPARE
:
650 case PM_SUSPEND_PREPARE
:
653 case PM_POST_HIBERNATION
:
654 case PM_POST_SUSPEND
:
662 static struct notifier_block ap_power_notifier
= {
663 .notifier_call
= ap_power_event
,
666 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops
, ap_dev_suspend
, ap_dev_resume
);
668 static struct bus_type ap_bus_type
= {
670 .match
= &ap_bus_match
,
671 .uevent
= &ap_uevent
,
672 .pm
= &ap_bus_pm_ops
,
675 static int ap_device_probe(struct device
*dev
)
677 struct ap_device
*ap_dev
= to_ap_dev(dev
);
678 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
681 ap_dev
->drv
= ap_drv
;
682 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
688 static int ap_device_remove(struct device
*dev
)
690 struct ap_device
*ap_dev
= to_ap_dev(dev
);
691 struct ap_driver
*ap_drv
= ap_dev
->drv
;
693 spin_lock_bh(&ap_list_lock
);
694 if (is_card_dev(dev
))
695 list_del_init(&to_ap_card(dev
)->list
);
697 list_del_init(&to_ap_queue(dev
)->list
);
698 spin_unlock_bh(&ap_list_lock
);
700 ap_drv
->remove(ap_dev
);
704 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
707 struct device_driver
*drv
= &ap_drv
->driver
;
712 drv
->bus
= &ap_bus_type
;
713 drv
->probe
= ap_device_probe
;
714 drv
->remove
= ap_device_remove
;
717 return driver_register(drv
);
719 EXPORT_SYMBOL(ap_driver_register
);
721 void ap_driver_unregister(struct ap_driver
*ap_drv
)
723 driver_unregister(&ap_drv
->driver
);
725 EXPORT_SYMBOL(ap_driver_unregister
);
727 void ap_bus_force_rescan(void)
731 /* processing a asynchronous bus rescan */
732 del_timer(&ap_config_timer
);
733 queue_work(system_long_wq
, &ap_scan_work
);
734 flush_work(&ap_scan_work
);
736 EXPORT_SYMBOL(ap_bus_force_rescan
);
741 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
743 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
746 static ssize_t
ap_domain_store(struct bus_type
*bus
,
747 const char *buf
, size_t count
)
751 if (sscanf(buf
, "%i\n", &domain
) != 1 ||
752 domain
< 0 || domain
> ap_max_domain_id
)
754 spin_lock_bh(&ap_domain_lock
);
755 ap_domain_index
= domain
;
756 spin_unlock_bh(&ap_domain_lock
);
758 AP_DBF(DBF_DEBUG
, "store new default domain=%d\n", domain
);
763 static BUS_ATTR(ap_domain
, 0644, ap_domain_show
, ap_domain_store
);
765 static ssize_t
ap_control_domain_mask_show(struct bus_type
*bus
, char *buf
)
767 if (!ap_configuration
) /* QCI not supported */
768 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
770 return snprintf(buf
, PAGE_SIZE
,
771 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
772 ap_configuration
->adm
[0], ap_configuration
->adm
[1],
773 ap_configuration
->adm
[2], ap_configuration
->adm
[3],
774 ap_configuration
->adm
[4], ap_configuration
->adm
[5],
775 ap_configuration
->adm
[6], ap_configuration
->adm
[7]);
778 static BUS_ATTR(ap_control_domain_mask
, 0444,
779 ap_control_domain_mask_show
, NULL
);
781 static ssize_t
ap_usage_domain_mask_show(struct bus_type
*bus
, char *buf
)
783 if (!ap_configuration
) /* QCI not supported */
784 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
786 return snprintf(buf
, PAGE_SIZE
,
787 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
788 ap_configuration
->aqm
[0], ap_configuration
->aqm
[1],
789 ap_configuration
->aqm
[2], ap_configuration
->aqm
[3],
790 ap_configuration
->aqm
[4], ap_configuration
->aqm
[5],
791 ap_configuration
->aqm
[6], ap_configuration
->aqm
[7]);
794 static BUS_ATTR(ap_usage_domain_mask
, 0444,
795 ap_usage_domain_mask_show
, NULL
);
797 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
799 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
802 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
804 return snprintf(buf
, PAGE_SIZE
, "%d\n",
805 ap_using_interrupts() ? 1 : 0);
808 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
810 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
811 const char *buf
, size_t count
)
815 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
817 ap_config_time
= time
;
818 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
822 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
824 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
826 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
829 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
830 const char *buf
, size_t count
)
834 if (sscanf(buf
, "%d\n", &flag
) != 1)
837 rc
= ap_poll_thread_start();
841 ap_poll_thread_stop();
845 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
847 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
849 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
852 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
855 unsigned long long time
;
858 /* 120 seconds = maximum poll interval */
859 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
860 time
> 120000000000ULL)
863 hr_time
= poll_timeout
;
865 spin_lock_bh(&ap_poll_timer_lock
);
866 hrtimer_cancel(&ap_poll_timer
);
867 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
868 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
869 spin_unlock_bh(&ap_poll_timer_lock
);
874 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
876 static ssize_t
ap_max_domain_id_show(struct bus_type
*bus
, char *buf
)
880 if (ap_configuration
)
881 max_domain_id
= ap_max_domain_id
? : -1;
884 return snprintf(buf
, PAGE_SIZE
, "%d\n", max_domain_id
);
887 static BUS_ATTR(ap_max_domain_id
, 0444, ap_max_domain_id_show
, NULL
);
889 static struct bus_attribute
*const ap_bus_attrs
[] = {
891 &bus_attr_ap_control_domain_mask
,
892 &bus_attr_ap_usage_domain_mask
,
893 &bus_attr_config_time
,
894 &bus_attr_poll_thread
,
895 &bus_attr_ap_interrupts
,
896 &bus_attr_poll_timeout
,
897 &bus_attr_ap_max_domain_id
,
902 * ap_select_domain(): Select an AP domain.
904 * Pick one of the 16 AP domains.
906 static int ap_select_domain(void)
908 int count
, max_count
, best_domain
;
909 struct ap_queue_status status
;
913 * We want to use a single domain. Either the one specified with
914 * the "domain=" parameter or the domain with the maximum number
917 spin_lock_bh(&ap_domain_lock
);
918 if (ap_domain_index
>= 0) {
919 /* Domain has already been selected. */
920 spin_unlock_bh(&ap_domain_lock
);
925 for (i
= 0; i
< AP_DOMAINS
; i
++) {
926 if (!ap_test_config_domain(i
))
929 for (j
= 0; j
< AP_DEVICES
; j
++) {
930 if (!ap_test_config_card_id(j
))
932 status
= ap_test_queue(AP_MKQID(j
, i
), NULL
);
933 if (status
.response_code
!= AP_RESPONSE_NORMAL
)
937 if (count
> max_count
) {
942 if (best_domain
>= 0){
943 ap_domain_index
= best_domain
;
944 spin_unlock_bh(&ap_domain_lock
);
947 spin_unlock_bh(&ap_domain_lock
);
952 * helper function to be used with bus_find_dev
953 * matches for the card device with the given id
955 static int __match_card_device_with_id(struct device
*dev
, void *data
)
957 return is_card_dev(dev
) && to_ap_card(dev
)->id
== (int)(long) data
;
960 /* helper function to be used with bus_find_dev
961 * matches for the queue device with a given qid
963 static int __match_queue_device_with_qid(struct device
*dev
, void *data
)
965 return is_queue_dev(dev
) && to_ap_queue(dev
)->qid
== (int)(long) data
;
969 * ap_scan_bus(): Scan the AP bus for new devices
970 * Runs periodically, workqueue timer (ap_config_time)
972 static void ap_scan_bus(struct work_struct
*unused
)
978 int depth
= 0, type
= 0;
979 unsigned int functions
= 0;
980 int rc
, id
, dom
, borked
, domains
;
982 AP_DBF(DBF_DEBUG
, "ap_scan_bus running\n");
984 ap_query_configuration();
985 if (ap_select_domain() != 0)
988 for (id
= 0; id
< AP_DEVICES
; id
++) {
989 /* check if device is registered */
990 dev
= bus_find_device(&ap_bus_type
, NULL
,
992 __match_card_device_with_id
);
993 ac
= dev
? to_ap_card(dev
) : NULL
;
994 if (!ap_test_config_card_id(id
)) {
996 /* Card device has been removed from
997 * configuration, remove the belonging
1000 bus_for_each_dev(&ap_bus_type
, NULL
,
1002 __ap_queue_devices_with_id_unregister
);
1003 /* now remove the card device */
1004 device_unregister(dev
);
1009 /* According to the configuration there should be a card
1010 * device, so check if there is at least one valid queue
1011 * and maybe create queue devices and the card device.
1014 for (dom
= 0; dom
< AP_DOMAINS
; dom
++) {
1015 qid
= AP_MKQID(id
, dom
);
1016 dev
= bus_find_device(&ap_bus_type
, NULL
,
1018 __match_queue_device_with_qid
);
1019 aq
= dev
? to_ap_queue(dev
) : NULL
;
1020 if (!ap_test_config_domain(dom
)) {
1022 /* Queue device exists but has been
1023 * removed from configuration.
1025 device_unregister(dev
);
1030 rc
= ap_query_queue(qid
, &depth
, &type
, &functions
);
1032 spin_lock_bh(&aq
->lock
);
1033 if (rc
== -ENODEV
||
1034 /* adapter reconfiguration */
1035 (ac
&& ac
->functions
!= functions
))
1036 aq
->state
= AP_STATE_BORKED
;
1037 borked
= aq
->state
== AP_STATE_BORKED
;
1038 spin_unlock_bh(&aq
->lock
);
1039 if (borked
) /* Remove broken device */
1040 device_unregister(dev
);
1049 /* new queue device needed */
1051 /* but first create the card device */
1052 ac
= ap_card_create(id
, depth
,
1056 ac
->ap_dev
.device
.bus
= &ap_bus_type
;
1057 ac
->ap_dev
.device
.parent
= ap_root_device
;
1058 dev_set_name(&ac
->ap_dev
.device
,
1060 /* Register card with AP bus */
1061 rc
= device_register(&ac
->ap_dev
.device
);
1063 put_device(&ac
->ap_dev
.device
);
1067 /* get it and thus adjust reference counter */
1068 get_device(&ac
->ap_dev
.device
);
1069 /* Add card device to card list */
1070 spin_lock_bh(&ap_list_lock
);
1071 list_add(&ac
->list
, &ap_card_list
);
1072 spin_unlock_bh(&ap_list_lock
);
1074 /* now create the new queue device */
1075 aq
= ap_queue_create(qid
, type
);
1079 aq
->ap_dev
.device
.bus
= &ap_bus_type
;
1080 aq
->ap_dev
.device
.parent
= &ac
->ap_dev
.device
;
1081 dev_set_name(&aq
->ap_dev
.device
,
1082 "%02x.%04x", id
, dom
);
1083 /* Add queue device to card queue list */
1084 spin_lock_bh(&ap_list_lock
);
1085 list_add(&aq
->list
, &ac
->queues
);
1086 spin_unlock_bh(&ap_list_lock
);
1087 /* Start with a device reset */
1088 spin_lock_bh(&aq
->lock
);
1089 ap_wait(ap_sm_event(aq
, AP_EVENT_POLL
));
1090 spin_unlock_bh(&aq
->lock
);
1091 /* Register device */
1092 rc
= device_register(&aq
->ap_dev
.device
);
1094 spin_lock_bh(&ap_list_lock
);
1095 list_del_init(&aq
->list
);
1096 spin_unlock_bh(&ap_list_lock
);
1097 put_device(&aq
->ap_dev
.device
);
1101 } /* end domain loop */
1103 /* remove card dev if there are no queue devices */
1105 device_unregister(&ac
->ap_dev
.device
);
1106 put_device(&ac
->ap_dev
.device
);
1108 } /* end device loop */
1110 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1113 static void ap_config_timeout(unsigned long ptr
)
1115 if (ap_suspend_flag
)
1117 queue_work(system_long_wq
, &ap_scan_work
);
1120 static void ap_reset_domain(void)
1124 if (ap_domain_index
== -1 || !ap_test_config_domain(ap_domain_index
))
1126 for (i
= 0; i
< AP_DEVICES
; i
++)
1127 ap_rapq(AP_MKQID(i
, ap_domain_index
));
1130 static void ap_reset_all(void)
1134 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1135 if (!ap_test_config_domain(i
))
1137 for (j
= 0; j
< AP_DEVICES
; j
++) {
1138 if (!ap_test_config_card_id(j
))
1140 ap_rapq(AP_MKQID(j
, i
));
1145 static struct reset_call ap_reset_call
= {
1149 int __init
ap_debug_init(void)
1151 ap_dbf_root
= debugfs_create_dir("ap", NULL
);
1152 ap_dbf_info
= debug_register("ap", 1, 1,
1153 DBF_MAX_SPRINTF_ARGS
* sizeof(long));
1154 debug_register_view(ap_dbf_info
, &debug_sprintf_view
);
1155 debug_set_level(ap_dbf_info
, DBF_ERR
);
1160 void ap_debug_exit(void)
1162 debugfs_remove(ap_dbf_root
);
1163 debug_unregister(ap_dbf_info
);
1167 * ap_module_init(): The module initialization code.
1169 * Initializes the module.
1171 int __init
ap_module_init(void)
1176 rc
= ap_debug_init();
1180 if (ap_instructions_available() != 0) {
1181 pr_warn("The hardware system does not support AP instructions\n");
1185 /* Get AP configuration data if available */
1186 ap_init_configuration();
1188 if (ap_configuration
)
1189 max_domain_id
= ap_max_domain_id
? : (AP_DOMAINS
- 1);
1192 if (ap_domain_index
< -1 || ap_domain_index
> max_domain_id
) {
1193 pr_warn("%d is not a valid cryptographic domain\n",
1198 /* In resume callback we need to know if the user had set the domain.
1199 * If so, we can not just reset it.
1201 if (ap_domain_index
>= 0)
1202 user_set_domain
= 1;
1204 if (ap_interrupts_available()) {
1205 rc
= register_adapter_interrupt(&ap_airq
);
1206 ap_airq_flag
= (rc
== 0);
1209 register_reset_call(&ap_reset_call
);
1211 /* Create /sys/bus/ap. */
1212 rc
= bus_register(&ap_bus_type
);
1215 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1216 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1221 /* Create /sys/devices/ap. */
1222 ap_root_device
= root_device_register("ap");
1223 rc
= PTR_RET(ap_root_device
);
1227 /* Setup the AP bus rescan timer. */
1228 setup_timer(&ap_config_timer
, ap_config_timeout
, 0);
1231 * Setup the high resultion poll timer.
1232 * If we are running under z/VM adjust polling to z/VM polling rate.
1235 poll_timeout
= 1500000;
1236 spin_lock_init(&ap_poll_timer_lock
);
1237 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1238 ap_poll_timer
.function
= ap_poll_timeout
;
1240 /* Start the low priority AP bus poll thread. */
1241 if (ap_thread_flag
) {
1242 rc
= ap_poll_thread_start();
1247 rc
= register_pm_notifier(&ap_power_notifier
);
1251 queue_work(system_long_wq
, &ap_scan_work
);
1257 ap_poll_thread_stop();
1259 hrtimer_cancel(&ap_poll_timer
);
1260 root_device_unregister(ap_root_device
);
1263 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1264 bus_unregister(&ap_bus_type
);
1266 unregister_reset_call(&ap_reset_call
);
1267 if (ap_using_interrupts())
1268 unregister_adapter_interrupt(&ap_airq
);
1270 kfree(ap_configuration
);
1275 * ap_modules_exit(): The module termination code
1277 * Terminates the module.
1279 void ap_module_exit(void)
1283 initialised
= false;
1285 ap_poll_thread_stop();
1286 del_timer_sync(&ap_config_timer
);
1287 hrtimer_cancel(&ap_poll_timer
);
1288 tasklet_kill(&ap_tasklet
);
1290 /* first remove queue devices */
1291 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
1292 __ap_queue_devices_unregister
);
1293 /* now remove the card devices */
1294 bus_for_each_dev(&ap_bus_type
, NULL
, NULL
,
1295 __ap_card_devices_unregister
);
1297 /* remove bus attributes */
1298 for (i
= 0; ap_bus_attrs
[i
]; i
++)
1299 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1300 unregister_pm_notifier(&ap_power_notifier
);
1301 root_device_unregister(ap_root_device
);
1302 bus_unregister(&ap_bus_type
);
1303 kfree(ap_configuration
);
1304 unregister_reset_call(&ap_reset_call
);
1305 if (ap_using_interrupts())
1306 unregister_adapter_interrupt(&ap_airq
);
1311 module_init(ap_module_init
);
1312 module_exit(ap_module_exit
);