2 * linux/drivers/s390/crypto/ap_bus.c
4 * Copyright (C) 2006 IBM Corporation
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Ralph Wuerthner <rwuerthn@de.ibm.com>
8 * Felix Beck <felix.beck@de.ibm.com>
9 * Holger Dengler <hd@linux.vnet.ibm.com>
11 * Adjunct processor bus.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define KMSG_COMPONENT "ap"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
31 #include <linux/kernel_stat.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #include <linux/err.h>
36 #include <linux/interrupt.h>
37 #include <linux/workqueue.h>
38 #include <linux/slab.h>
39 #include <linux/notifier.h>
40 #include <linux/kthread.h>
41 #include <linux/mutex.h>
42 #include <asm/reset.h>
44 #include <linux/atomic.h>
45 #include <asm/system.h>
47 #include <linux/hrtimer.h>
48 #include <linux/ktime.h>
52 /* Some prototypes. */
53 static void ap_scan_bus(struct work_struct
*);
54 static void ap_poll_all(unsigned long);
55 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*);
56 static int ap_poll_thread_start(void);
57 static void ap_poll_thread_stop(void);
58 static void ap_request_timeout(unsigned long);
59 static inline void ap_schedule_poll_timer(void);
60 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
);
61 static int ap_device_remove(struct device
*dev
);
62 static int ap_device_probe(struct device
*dev
);
63 static void ap_interrupt_handler(void *unused1
, void *unused2
);
64 static void ap_reset(struct ap_device
*ap_dev
);
65 static void ap_config_timeout(unsigned long ptr
);
66 static int ap_select_domain(void);
71 MODULE_AUTHOR("IBM Corporation");
72 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
73 "Copyright 2006 IBM Corporation");
74 MODULE_LICENSE("GPL");
79 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
80 module_param_named(domain
, ap_domain_index
, int, 0000);
81 MODULE_PARM_DESC(domain
, "domain index for ap devices");
82 EXPORT_SYMBOL(ap_domain_index
);
84 static int ap_thread_flag
= 0;
85 module_param_named(poll_thread
, ap_thread_flag
, int, 0000);
86 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
88 static struct device
*ap_root_device
= NULL
;
89 static DEFINE_SPINLOCK(ap_device_list_lock
);
90 static LIST_HEAD(ap_device_list
);
93 * Workqueue & timer for bus rescan.
95 static struct workqueue_struct
*ap_work_queue
;
96 static struct timer_list ap_config_timer
;
97 static int ap_config_time
= AP_CONFIG_TIME
;
98 static DECLARE_WORK(ap_config_work
, ap_scan_bus
);
101 * Tasklet & timer for AP request polling and interrupts
103 static DECLARE_TASKLET(ap_tasklet
, ap_poll_all
, 0);
104 static atomic_t ap_poll_requests
= ATOMIC_INIT(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 void *ap_interrupt_indicator
;
110 static struct hrtimer ap_poll_timer
;
111 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
112 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
113 static unsigned long long poll_timeout
= 250000;
116 static int ap_suspend_flag
;
117 /* Flag to check if domain was set through module parameter domain=. This is
118 * important when supsend and resume is done in a z/VM environment where the
119 * domain might change. */
120 static int user_set_domain
= 0;
121 static struct bus_type ap_bus_type
;
124 * ap_using_interrupts() - Returns non-zero if interrupt support is
127 static inline int ap_using_interrupts(void)
129 return ap_interrupt_indicator
!= NULL
;
133 * ap_intructions_available() - Test if AP instructions are available.
135 * Returns 0 if the AP instructions are installed.
137 static inline int ap_instructions_available(void)
139 register unsigned long reg0
asm ("0") = AP_MKQID(0,0);
140 register unsigned long reg1
asm ("1") = -ENODEV
;
141 register unsigned long reg2
asm ("2") = 0UL;
144 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
148 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
) : : "cc" );
153 * ap_interrupts_available(): Test if AP interrupts are available.
155 * Returns 1 if AP interrupts are available.
157 static int ap_interrupts_available(void)
159 return test_facility(2) && test_facility(65);
163 * ap_test_queue(): Test adjunct processor queue.
164 * @qid: The AP queue number
165 * @queue_depth: Pointer to queue depth value
166 * @device_type: Pointer to device type value
168 * Returns AP queue status structure.
170 static inline struct ap_queue_status
171 ap_test_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
173 register unsigned long reg0
asm ("0") = qid
;
174 register struct ap_queue_status reg1
asm ("1");
175 register unsigned long reg2
asm ("2") = 0UL;
177 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
178 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
179 *device_type
= (int) (reg2
>> 24);
180 *queue_depth
= (int) (reg2
& 0xff);
185 * ap_reset_queue(): Reset adjunct processor queue.
186 * @qid: The AP queue number
188 * Returns AP queue status structure.
190 static inline struct ap_queue_status
ap_reset_queue(ap_qid_t qid
)
192 register unsigned long reg0
asm ("0") = qid
| 0x01000000UL
;
193 register struct ap_queue_status reg1
asm ("1");
194 register unsigned long reg2
asm ("2") = 0UL;
197 ".long 0xb2af0000" /* PQAP(RAPQ) */
198 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
204 * ap_queue_interruption_control(): Enable interruption for a specific AP.
205 * @qid: The AP queue number
206 * @ind: The notification indicator byte
208 * Returns AP queue status.
210 static inline struct ap_queue_status
211 ap_queue_interruption_control(ap_qid_t qid
, void *ind
)
213 register unsigned long reg0
asm ("0") = qid
| 0x03000000UL
;
214 register unsigned long reg1_in
asm ("1") = 0x0000800000000000UL
| AP_ISC
;
215 register struct ap_queue_status reg1_out
asm ("1");
216 register void *reg2
asm ("2") = ind
;
218 ".long 0xb2af0000" /* PQAP(RAPQ) */
219 : "+d" (reg0
), "+d" (reg1_in
), "=d" (reg1_out
), "+d" (reg2
)
227 static inline struct ap_queue_status
228 __ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
230 register unsigned long reg0
asm ("0") = 0UL | qid
| (1UL << 23);
231 register struct ap_queue_status reg1
asm ("1") = AP_QUEUE_STATUS_INVALID
;
232 register unsigned long reg2
asm ("2");
238 : "+d" (reg0
), "+d" (reg1
), "=d" (reg2
)
242 *functions
= (unsigned int)(reg2
>> 32);
248 * ap_query_functions(): Query supported functions.
249 * @qid: The AP queue number
250 * @functions: Pointer to functions field.
254 * -ENODEV if queue not valid.
255 * -EBUSY if device busy.
256 * -EINVAL if query function is not supported
258 static int ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
261 struct ap_queue_status status
;
263 status
= __ap_query_functions(qid
, functions
);
265 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
266 if (ap_queue_status_invalid_test(&status
))
269 switch (status
.response_code
) {
270 case AP_RESPONSE_NORMAL
:
272 case AP_RESPONSE_RESET_IN_PROGRESS
:
273 case AP_RESPONSE_BUSY
:
275 case AP_RESPONSE_Q_NOT_AVAIL
:
276 case AP_RESPONSE_DECONFIGURED
:
277 case AP_RESPONSE_CHECKSTOPPED
:
278 case AP_RESPONSE_INVALID_ADDRESS
:
280 case AP_RESPONSE_OTHERWISE_CHANGED
:
285 if (i
< AP_MAX_RESET
- 1) {
287 status
= __ap_query_functions(qid
, functions
);
297 * ap_4096_commands_availablen(): Check for availability of 4096 bit RSA
299 * @qid: The AP queue number
301 * Returns 1 if 4096 bit RSA keys are support fo the AP, returns 0 if not.
303 int ap_4096_commands_available(ap_qid_t qid
)
305 unsigned int functions
;
307 if (ap_query_functions(qid
, &functions
))
310 return test_ap_facility(functions
, 1) &&
311 test_ap_facility(functions
, 2);
313 EXPORT_SYMBOL(ap_4096_commands_available
);
316 * ap_queue_enable_interruption(): Enable interruption on an AP.
317 * @qid: The AP queue number
318 * @ind: the notification indicator byte
320 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
321 * on the return value it waits a while and tests the AP queue if interrupts
322 * have been switched on using ap_test_queue().
324 static int ap_queue_enable_interruption(ap_qid_t qid
, void *ind
)
327 struct ap_queue_status status
;
328 int t_depth
, t_device_type
, rc
, i
;
331 status
= ap_queue_interruption_control(qid
, ind
);
333 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
334 switch (status
.response_code
) {
335 case AP_RESPONSE_NORMAL
:
336 if (status
.int_enabled
)
339 case AP_RESPONSE_RESET_IN_PROGRESS
:
340 case AP_RESPONSE_BUSY
:
342 case AP_RESPONSE_Q_NOT_AVAIL
:
343 case AP_RESPONSE_DECONFIGURED
:
344 case AP_RESPONSE_CHECKSTOPPED
:
345 case AP_RESPONSE_INVALID_ADDRESS
:
347 case AP_RESPONSE_OTHERWISE_CHANGED
:
348 if (status
.int_enabled
)
354 if (i
< AP_MAX_RESET
- 1) {
356 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
366 * __ap_send(): Send message to adjunct processor queue.
367 * @qid: The AP queue number
368 * @psmid: The program supplied message identifier
369 * @msg: The message text
370 * @length: The message length
371 * @special: Special Bit
373 * Returns AP queue status structure.
374 * Condition code 1 on NQAP can't happen because the L bit is 1.
375 * Condition code 2 on NQAP also means the send is incomplete,
376 * because a segment boundary was reached. The NQAP is repeated.
378 static inline struct ap_queue_status
379 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
,
380 unsigned int special
)
382 typedef struct { char _
[length
]; } msgblock
;
383 register unsigned long reg0
asm ("0") = qid
| 0x40000000UL
;
384 register struct ap_queue_status reg1
asm ("1");
385 register unsigned long reg2
asm ("2") = (unsigned long) msg
;
386 register unsigned long reg3
asm ("3") = (unsigned long) length
;
387 register unsigned long reg4
asm ("4") = (unsigned int) (psmid
>> 32);
388 register unsigned long reg5
asm ("5") = (unsigned int) psmid
;
394 "0: .long 0xb2ad0042\n" /* DQAP */
396 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
), "+d" (reg3
)
397 : "d" (reg4
), "d" (reg5
), "m" (*(msgblock
*) msg
)
402 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
404 struct ap_queue_status status
;
406 status
= __ap_send(qid
, psmid
, msg
, length
, 0);
407 switch (status
.response_code
) {
408 case AP_RESPONSE_NORMAL
:
410 case AP_RESPONSE_Q_FULL
:
411 case AP_RESPONSE_RESET_IN_PROGRESS
:
413 case AP_RESPONSE_REQ_FAC_NOT_INST
:
415 default: /* Device is gone. */
419 EXPORT_SYMBOL(ap_send
);
422 * __ap_recv(): Receive message from adjunct processor queue.
423 * @qid: The AP queue number
424 * @psmid: Pointer to program supplied message identifier
425 * @msg: The message text
426 * @length: The message length
428 * Returns AP queue status structure.
429 * Condition code 1 on DQAP means the receive has taken place
430 * but only partially. The response is incomplete, hence the
432 * Condition code 2 on DQAP also means the receive is incomplete,
433 * this time because a segment boundary was reached. Again, the
435 * Note that gpr2 is used by the DQAP instruction to keep track of
436 * any 'residual' length, in case the instruction gets interrupted.
437 * Hence it gets zeroed before the instruction.
439 static inline struct ap_queue_status
440 __ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
442 typedef struct { char _
[length
]; } msgblock
;
443 register unsigned long reg0
asm("0") = qid
| 0x80000000UL
;
444 register struct ap_queue_status reg1
asm ("1");
445 register unsigned long reg2
asm("2") = 0UL;
446 register unsigned long reg4
asm("4") = (unsigned long) msg
;
447 register unsigned long reg5
asm("5") = (unsigned long) length
;
448 register unsigned long reg6
asm("6") = 0UL;
449 register unsigned long reg7
asm("7") = 0UL;
453 "0: .long 0xb2ae0064\n"
455 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
),
456 "+d" (reg4
), "+d" (reg5
), "+d" (reg6
), "+d" (reg7
),
457 "=m" (*(msgblock
*) msg
) : : "cc" );
458 *psmid
= (((unsigned long long) reg6
) << 32) + reg7
;
462 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
464 struct ap_queue_status status
;
466 status
= __ap_recv(qid
, psmid
, msg
, length
);
467 switch (status
.response_code
) {
468 case AP_RESPONSE_NORMAL
:
470 case AP_RESPONSE_NO_PENDING_REPLY
:
471 if (status
.queue_empty
)
474 case AP_RESPONSE_RESET_IN_PROGRESS
:
480 EXPORT_SYMBOL(ap_recv
);
483 * ap_query_queue(): Check if an AP queue is available.
484 * @qid: The AP queue number
485 * @queue_depth: Pointer to queue depth value
486 * @device_type: Pointer to device type value
488 * The test is repeated for AP_MAX_RESET times.
490 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
492 struct ap_queue_status status
;
493 int t_depth
, t_device_type
, rc
, i
;
496 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
497 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
498 switch (status
.response_code
) {
499 case AP_RESPONSE_NORMAL
:
500 *queue_depth
= t_depth
+ 1;
501 *device_type
= t_device_type
;
504 case AP_RESPONSE_Q_NOT_AVAIL
:
507 case AP_RESPONSE_RESET_IN_PROGRESS
:
509 case AP_RESPONSE_DECONFIGURED
:
512 case AP_RESPONSE_CHECKSTOPPED
:
515 case AP_RESPONSE_INVALID_ADDRESS
:
518 case AP_RESPONSE_OTHERWISE_CHANGED
:
520 case AP_RESPONSE_BUSY
:
527 if (i
< AP_MAX_RESET
- 1)
534 * ap_init_queue(): Reset an AP queue.
535 * @qid: The AP queue number
537 * Reset an AP queue and wait for it to become available again.
539 static int ap_init_queue(ap_qid_t qid
)
541 struct ap_queue_status status
;
545 status
= ap_reset_queue(qid
);
546 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
547 switch (status
.response_code
) {
548 case AP_RESPONSE_NORMAL
:
549 if (status
.queue_empty
)
552 case AP_RESPONSE_Q_NOT_AVAIL
:
553 case AP_RESPONSE_DECONFIGURED
:
554 case AP_RESPONSE_CHECKSTOPPED
:
555 i
= AP_MAX_RESET
; /* return with -ENODEV */
557 case AP_RESPONSE_RESET_IN_PROGRESS
:
559 case AP_RESPONSE_BUSY
:
563 if (rc
!= -ENODEV
&& rc
!= -EBUSY
)
565 if (i
< AP_MAX_RESET
- 1) {
567 status
= ap_test_queue(qid
, &dummy
, &dummy
);
570 if (rc
== 0 && ap_using_interrupts()) {
571 rc
= ap_queue_enable_interruption(qid
, ap_interrupt_indicator
);
572 /* If interruption mode is supported by the machine,
573 * but an AP can not be enabled for interruption then
574 * the AP will be discarded. */
576 pr_err("Registering adapter interrupts for "
577 "AP %d failed\n", AP_QID_DEVICE(qid
));
583 * ap_increase_queue_count(): Arm request timeout.
584 * @ap_dev: Pointer to an AP device.
586 * Arm request timeout if an AP device was idle and a new request is submitted.
588 static void ap_increase_queue_count(struct ap_device
*ap_dev
)
590 int timeout
= ap_dev
->drv
->request_timeout
;
592 ap_dev
->queue_count
++;
593 if (ap_dev
->queue_count
== 1) {
594 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
595 ap_dev
->reset
= AP_RESET_ARMED
;
600 * ap_decrease_queue_count(): Decrease queue count.
601 * @ap_dev: Pointer to an AP device.
603 * If AP device is still alive, re-schedule request timeout if there are still
606 static void ap_decrease_queue_count(struct ap_device
*ap_dev
)
608 int timeout
= ap_dev
->drv
->request_timeout
;
610 ap_dev
->queue_count
--;
611 if (ap_dev
->queue_count
> 0)
612 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
615 * The timeout timer should to be disabled now - since
616 * del_timer_sync() is very expensive, we just tell via the
617 * reset flag to ignore the pending timeout timer.
619 ap_dev
->reset
= AP_RESET_IGNORE
;
623 * AP device related attributes.
625 static ssize_t
ap_hwtype_show(struct device
*dev
,
626 struct device_attribute
*attr
, char *buf
)
628 struct ap_device
*ap_dev
= to_ap_dev(dev
);
629 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->device_type
);
632 static DEVICE_ATTR(hwtype
, 0444, ap_hwtype_show
, NULL
);
633 static ssize_t
ap_depth_show(struct device
*dev
, struct device_attribute
*attr
,
636 struct ap_device
*ap_dev
= to_ap_dev(dev
);
637 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->queue_depth
);
640 static DEVICE_ATTR(depth
, 0444, ap_depth_show
, NULL
);
641 static ssize_t
ap_request_count_show(struct device
*dev
,
642 struct device_attribute
*attr
,
645 struct ap_device
*ap_dev
= to_ap_dev(dev
);
648 spin_lock_bh(&ap_dev
->lock
);
649 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->total_request_count
);
650 spin_unlock_bh(&ap_dev
->lock
);
654 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
656 static ssize_t
ap_modalias_show(struct device
*dev
,
657 struct device_attribute
*attr
, char *buf
)
659 return sprintf(buf
, "ap:t%02X", to_ap_dev(dev
)->device_type
);
662 static DEVICE_ATTR(modalias
, 0444, ap_modalias_show
, NULL
);
664 static struct attribute
*ap_dev_attrs
[] = {
665 &dev_attr_hwtype
.attr
,
666 &dev_attr_depth
.attr
,
667 &dev_attr_request_count
.attr
,
668 &dev_attr_modalias
.attr
,
671 static struct attribute_group ap_dev_attr_group
= {
672 .attrs
= ap_dev_attrs
677 * @dev: Pointer to device
678 * @drv: Pointer to device_driver
680 * AP bus driver registration/unregistration.
682 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
684 struct ap_device
*ap_dev
= to_ap_dev(dev
);
685 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
686 struct ap_device_id
*id
;
689 * Compare device type of the device with the list of
690 * supported types of the device_driver.
692 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
693 if ((id
->match_flags
& AP_DEVICE_ID_MATCH_DEVICE_TYPE
) &&
694 (id
->dev_type
!= ap_dev
->device_type
))
702 * ap_uevent(): Uevent function for AP devices.
703 * @dev: Pointer to device
704 * @env: Pointer to kobj_uevent_env
706 * It sets up a single environment variable DEV_TYPE which contains the
707 * hardware device type.
709 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
711 struct ap_device
*ap_dev
= to_ap_dev(dev
);
717 /* Set up DEV_TYPE environment variable. */
718 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
723 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
728 static int ap_bus_suspend(struct device
*dev
, pm_message_t state
)
730 struct ap_device
*ap_dev
= to_ap_dev(dev
);
733 if (!ap_suspend_flag
) {
736 /* Disable scanning for devices, thus we do not want to scan
737 * for them after removing.
739 del_timer_sync(&ap_config_timer
);
740 if (ap_work_queue
!= NULL
) {
741 destroy_workqueue(ap_work_queue
);
742 ap_work_queue
= NULL
;
745 tasklet_disable(&ap_tasklet
);
747 /* Poll on the device until all requests are finished. */
750 spin_lock_bh(&ap_dev
->lock
);
751 __ap_poll_device(ap_dev
, &flags
);
752 spin_unlock_bh(&ap_dev
->lock
);
753 } while ((flags
& 1) || (flags
& 2));
755 spin_lock_bh(&ap_dev
->lock
);
756 ap_dev
->unregistered
= 1;
757 spin_unlock_bh(&ap_dev
->lock
);
762 static int ap_bus_resume(struct device
*dev
)
765 struct ap_device
*ap_dev
= to_ap_dev(dev
);
767 if (ap_suspend_flag
) {
769 if (!ap_interrupts_available())
770 ap_interrupt_indicator
= NULL
;
771 if (!user_set_domain
) {
772 ap_domain_index
= -1;
775 init_timer(&ap_config_timer
);
776 ap_config_timer
.function
= ap_config_timeout
;
777 ap_config_timer
.data
= 0;
778 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
779 add_timer(&ap_config_timer
);
780 ap_work_queue
= create_singlethread_workqueue("kapwork");
783 tasklet_enable(&ap_tasklet
);
784 if (!ap_using_interrupts())
785 ap_schedule_poll_timer();
787 tasklet_schedule(&ap_tasklet
);
789 rc
= ap_poll_thread_start();
791 if (AP_QID_QUEUE(ap_dev
->qid
) != ap_domain_index
) {
792 spin_lock_bh(&ap_dev
->lock
);
793 ap_dev
->qid
= AP_MKQID(AP_QID_DEVICE(ap_dev
->qid
),
795 spin_unlock_bh(&ap_dev
->lock
);
797 queue_work(ap_work_queue
, &ap_config_work
);
802 static struct bus_type ap_bus_type
= {
804 .match
= &ap_bus_match
,
805 .uevent
= &ap_uevent
,
806 .suspend
= ap_bus_suspend
,
807 .resume
= ap_bus_resume
810 static int ap_device_probe(struct device
*dev
)
812 struct ap_device
*ap_dev
= to_ap_dev(dev
);
813 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
816 ap_dev
->drv
= ap_drv
;
817 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
819 spin_lock_bh(&ap_device_list_lock
);
820 list_add(&ap_dev
->list
, &ap_device_list
);
821 spin_unlock_bh(&ap_device_list_lock
);
827 * __ap_flush_queue(): Flush requests.
828 * @ap_dev: Pointer to the AP device
830 * Flush all requests from the request/pending queue of an AP device.
832 static void __ap_flush_queue(struct ap_device
*ap_dev
)
834 struct ap_message
*ap_msg
, *next
;
836 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->pendingq
, list
) {
837 list_del_init(&ap_msg
->list
);
838 ap_dev
->pendingq_count
--;
839 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
841 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->requestq
, list
) {
842 list_del_init(&ap_msg
->list
);
843 ap_dev
->requestq_count
--;
844 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
848 void ap_flush_queue(struct ap_device
*ap_dev
)
850 spin_lock_bh(&ap_dev
->lock
);
851 __ap_flush_queue(ap_dev
);
852 spin_unlock_bh(&ap_dev
->lock
);
854 EXPORT_SYMBOL(ap_flush_queue
);
856 static int ap_device_remove(struct device
*dev
)
858 struct ap_device
*ap_dev
= to_ap_dev(dev
);
859 struct ap_driver
*ap_drv
= ap_dev
->drv
;
861 ap_flush_queue(ap_dev
);
862 del_timer_sync(&ap_dev
->timeout
);
863 spin_lock_bh(&ap_device_list_lock
);
864 list_del_init(&ap_dev
->list
);
865 spin_unlock_bh(&ap_device_list_lock
);
867 ap_drv
->remove(ap_dev
);
868 spin_lock_bh(&ap_dev
->lock
);
869 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
870 spin_unlock_bh(&ap_dev
->lock
);
874 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
877 struct device_driver
*drv
= &ap_drv
->driver
;
879 drv
->bus
= &ap_bus_type
;
880 drv
->probe
= ap_device_probe
;
881 drv
->remove
= ap_device_remove
;
884 return driver_register(drv
);
886 EXPORT_SYMBOL(ap_driver_register
);
888 void ap_driver_unregister(struct ap_driver
*ap_drv
)
890 driver_unregister(&ap_drv
->driver
);
892 EXPORT_SYMBOL(ap_driver_unregister
);
897 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
899 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
902 static BUS_ATTR(ap_domain
, 0444, ap_domain_show
, NULL
);
904 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
906 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
909 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
911 return snprintf(buf
, PAGE_SIZE
, "%d\n",
912 ap_using_interrupts() ? 1 : 0);
915 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
917 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
918 const char *buf
, size_t count
)
922 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
924 ap_config_time
= time
;
925 if (!timer_pending(&ap_config_timer
) ||
926 !mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
)) {
927 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
928 add_timer(&ap_config_timer
);
933 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
935 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
937 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
940 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
941 const char *buf
, size_t count
)
945 if (sscanf(buf
, "%d\n", &flag
) != 1)
948 rc
= ap_poll_thread_start();
953 ap_poll_thread_stop();
957 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
959 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
961 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
964 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
967 unsigned long long time
;
970 /* 120 seconds = maximum poll interval */
971 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
972 time
> 120000000000ULL)
975 hr_time
= ktime_set(0, poll_timeout
);
977 if (!hrtimer_is_queued(&ap_poll_timer
) ||
978 !hrtimer_forward(&ap_poll_timer
, hrtimer_get_expires(&ap_poll_timer
), hr_time
)) {
979 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
980 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
985 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
987 static struct bus_attribute
*const ap_bus_attrs
[] = {
989 &bus_attr_config_time
,
990 &bus_attr_poll_thread
,
991 &bus_attr_ap_interrupts
,
992 &bus_attr_poll_timeout
,
997 * ap_select_domain(): Select an AP domain.
999 * Pick one of the 16 AP domains.
1001 static int ap_select_domain(void)
1003 int queue_depth
, device_type
, count
, max_count
, best_domain
;
1007 * We want to use a single domain. Either the one specified with
1008 * the "domain=" parameter or the domain with the maximum number
1011 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
)
1012 /* Domain has already been selected. */
1016 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1018 for (j
= 0; j
< AP_DEVICES
; j
++) {
1019 ap_qid_t qid
= AP_MKQID(j
, i
);
1020 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1025 if (count
> max_count
) {
1030 if (best_domain
>= 0){
1031 ap_domain_index
= best_domain
;
1038 * ap_probe_device_type(): Find the device type of an AP.
1039 * @ap_dev: pointer to the AP device.
1041 * Find the device type if query queue returned a device type of 0.
1043 static int ap_probe_device_type(struct ap_device
*ap_dev
)
1045 static unsigned char msg
[] = {
1046 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1047 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1048 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1049 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1050 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1051 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1052 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1053 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1054 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1055 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1056 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1057 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1058 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1059 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1060 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1061 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1062 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1063 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1064 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1065 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1066 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1067 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1068 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1069 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1070 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1071 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1072 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1073 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1074 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1075 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1076 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1077 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1078 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1079 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1080 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1081 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1082 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1083 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1084 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1085 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1086 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1087 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1088 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1089 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1090 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1092 struct ap_queue_status status
;
1093 unsigned long long psmid
;
1097 reply
= (void *) get_zeroed_page(GFP_KERNEL
);
1103 status
= __ap_send(ap_dev
->qid
, 0x0102030405060708ULL
,
1104 msg
, sizeof(msg
), 0);
1105 if (status
.response_code
!= AP_RESPONSE_NORMAL
) {
1110 /* Wait for the test message to complete. */
1111 for (i
= 0; i
< 6; i
++) {
1113 status
= __ap_recv(ap_dev
->qid
, &psmid
, reply
, 4096);
1114 if (status
.response_code
== AP_RESPONSE_NORMAL
&&
1115 psmid
== 0x0102030405060708ULL
)
1119 /* Got an answer. */
1120 if (reply
[0] == 0x00 && reply
[1] == 0x86)
1121 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICC
;
1123 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICA
;
1129 free_page((unsigned long) reply
);
1134 static void ap_interrupt_handler(void *unused1
, void *unused2
)
1136 kstat_cpu(smp_processor_id()).irqs
[IOINT_APB
]++;
1137 tasklet_schedule(&ap_tasklet
);
1141 * __ap_scan_bus(): Scan the AP bus.
1142 * @dev: Pointer to device
1143 * @data: Pointer to data
1145 * Scan the AP bus for new devices.
1147 static int __ap_scan_bus(struct device
*dev
, void *data
)
1149 return to_ap_dev(dev
)->qid
== (ap_qid_t
)(unsigned long) data
;
1152 static void ap_device_release(struct device
*dev
)
1154 struct ap_device
*ap_dev
= to_ap_dev(dev
);
1159 static void ap_scan_bus(struct work_struct
*unused
)
1161 struct ap_device
*ap_dev
;
1164 int queue_depth
, device_type
;
1165 unsigned int device_functions
;
1168 if (ap_select_domain() != 0)
1170 for (i
= 0; i
< AP_DEVICES
; i
++) {
1171 qid
= AP_MKQID(i
, ap_domain_index
);
1172 dev
= bus_find_device(&ap_bus_type
, NULL
,
1173 (void *)(unsigned long)qid
,
1175 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1178 set_current_state(TASK_UNINTERRUPTIBLE
);
1179 schedule_timeout(AP_RESET_TIMEOUT
);
1180 rc
= ap_query_queue(qid
, &queue_depth
,
1183 ap_dev
= to_ap_dev(dev
);
1184 spin_lock_bh(&ap_dev
->lock
);
1185 if (rc
|| ap_dev
->unregistered
) {
1186 spin_unlock_bh(&ap_dev
->lock
);
1187 if (ap_dev
->unregistered
)
1189 device_unregister(dev
);
1193 spin_unlock_bh(&ap_dev
->lock
);
1199 rc
= ap_init_queue(qid
);
1202 ap_dev
= kzalloc(sizeof(*ap_dev
), GFP_KERNEL
);
1206 ap_dev
->queue_depth
= queue_depth
;
1207 ap_dev
->unregistered
= 1;
1208 spin_lock_init(&ap_dev
->lock
);
1209 INIT_LIST_HEAD(&ap_dev
->pendingq
);
1210 INIT_LIST_HEAD(&ap_dev
->requestq
);
1211 INIT_LIST_HEAD(&ap_dev
->list
);
1212 setup_timer(&ap_dev
->timeout
, ap_request_timeout
,
1213 (unsigned long) ap_dev
);
1214 switch (device_type
) {
1216 if (ap_probe_device_type(ap_dev
)) {
1222 if (ap_query_functions(qid
, &device_functions
)) {
1226 if (test_ap_facility(device_functions
, 3))
1227 ap_dev
->device_type
= AP_DEVICE_TYPE_CEX3C
;
1228 else if (test_ap_facility(device_functions
, 4))
1229 ap_dev
->device_type
= AP_DEVICE_TYPE_CEX3A
;
1236 ap_dev
->device_type
= device_type
;
1239 ap_dev
->device
.bus
= &ap_bus_type
;
1240 ap_dev
->device
.parent
= ap_root_device
;
1241 if (dev_set_name(&ap_dev
->device
, "card%02x",
1242 AP_QID_DEVICE(ap_dev
->qid
))) {
1246 ap_dev
->device
.release
= ap_device_release
;
1247 rc
= device_register(&ap_dev
->device
);
1249 put_device(&ap_dev
->device
);
1252 /* Add device attributes. */
1253 rc
= sysfs_create_group(&ap_dev
->device
.kobj
,
1254 &ap_dev_attr_group
);
1256 spin_lock_bh(&ap_dev
->lock
);
1257 ap_dev
->unregistered
= 0;
1258 spin_unlock_bh(&ap_dev
->lock
);
1261 device_unregister(&ap_dev
->device
);
1266 ap_config_timeout(unsigned long ptr
)
1268 queue_work(ap_work_queue
, &ap_config_work
);
1269 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1270 add_timer(&ap_config_timer
);
1274 * __ap_schedule_poll_timer(): Schedule poll timer.
1276 * Set up the timer to run the poll tasklet
1278 static inline void __ap_schedule_poll_timer(void)
1282 spin_lock_bh(&ap_poll_timer_lock
);
1283 if (hrtimer_is_queued(&ap_poll_timer
) || ap_suspend_flag
)
1285 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer
)) <= 0) {
1286 hr_time
= ktime_set(0, poll_timeout
);
1287 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
1288 hrtimer_restart(&ap_poll_timer
);
1291 spin_unlock_bh(&ap_poll_timer_lock
);
1295 * ap_schedule_poll_timer(): Schedule poll timer.
1297 * Set up the timer to run the poll tasklet
1299 static inline void ap_schedule_poll_timer(void)
1301 if (ap_using_interrupts())
1303 __ap_schedule_poll_timer();
1307 * ap_poll_read(): Receive pending reply messages from an AP device.
1308 * @ap_dev: pointer to the AP device
1309 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1310 * required, bit 2^1 is set if the poll timer needs to get armed
1312 * Returns 0 if the device is still present, -ENODEV if not.
1314 static int ap_poll_read(struct ap_device
*ap_dev
, unsigned long *flags
)
1316 struct ap_queue_status status
;
1317 struct ap_message
*ap_msg
;
1319 if (ap_dev
->queue_count
<= 0)
1321 status
= __ap_recv(ap_dev
->qid
, &ap_dev
->reply
->psmid
,
1322 ap_dev
->reply
->message
, ap_dev
->reply
->length
);
1323 switch (status
.response_code
) {
1324 case AP_RESPONSE_NORMAL
:
1325 atomic_dec(&ap_poll_requests
);
1326 ap_decrease_queue_count(ap_dev
);
1327 list_for_each_entry(ap_msg
, &ap_dev
->pendingq
, list
) {
1328 if (ap_msg
->psmid
!= ap_dev
->reply
->psmid
)
1330 list_del_init(&ap_msg
->list
);
1331 ap_dev
->pendingq_count
--;
1332 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ap_dev
->reply
);
1335 if (ap_dev
->queue_count
> 0)
1338 case AP_RESPONSE_NO_PENDING_REPLY
:
1339 if (status
.queue_empty
) {
1340 /* The card shouldn't forget requests but who knows. */
1341 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1342 ap_dev
->queue_count
= 0;
1343 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1344 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1345 ap_dev
->pendingq_count
= 0;
1356 * ap_poll_write(): Send messages from the request queue to an AP device.
1357 * @ap_dev: pointer to the AP device
1358 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1359 * required, bit 2^1 is set if the poll timer needs to get armed
1361 * Returns 0 if the device is still present, -ENODEV if not.
1363 static int ap_poll_write(struct ap_device
*ap_dev
, unsigned long *flags
)
1365 struct ap_queue_status status
;
1366 struct ap_message
*ap_msg
;
1368 if (ap_dev
->requestq_count
<= 0 ||
1369 ap_dev
->queue_count
>= ap_dev
->queue_depth
)
1371 /* Start the next request on the queue. */
1372 ap_msg
= list_entry(ap_dev
->requestq
.next
, struct ap_message
, list
);
1373 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1374 ap_msg
->message
, ap_msg
->length
, ap_msg
->special
);
1375 switch (status
.response_code
) {
1376 case AP_RESPONSE_NORMAL
:
1377 atomic_inc(&ap_poll_requests
);
1378 ap_increase_queue_count(ap_dev
);
1379 list_move_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1380 ap_dev
->requestq_count
--;
1381 ap_dev
->pendingq_count
++;
1382 if (ap_dev
->queue_count
< ap_dev
->queue_depth
&&
1383 ap_dev
->requestq_count
> 0)
1387 case AP_RESPONSE_RESET_IN_PROGRESS
:
1388 __ap_schedule_poll_timer();
1389 case AP_RESPONSE_Q_FULL
:
1392 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1393 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1402 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1403 * @ap_dev: pointer to the bus device
1404 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1405 * required, bit 2^1 is set if the poll timer needs to get armed
1407 * Poll AP device for pending replies and send new messages. If either
1408 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1411 static inline int ap_poll_queue(struct ap_device
*ap_dev
, unsigned long *flags
)
1415 rc
= ap_poll_read(ap_dev
, flags
);
1418 return ap_poll_write(ap_dev
, flags
);
1422 * __ap_queue_message(): Queue a message to a device.
1423 * @ap_dev: pointer to the AP device
1424 * @ap_msg: the message to be queued
1426 * Queue a message to a device. Returns 0 if successful.
1428 static int __ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1430 struct ap_queue_status status
;
1432 if (list_empty(&ap_dev
->requestq
) &&
1433 ap_dev
->queue_count
< ap_dev
->queue_depth
) {
1434 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1435 ap_msg
->message
, ap_msg
->length
,
1437 switch (status
.response_code
) {
1438 case AP_RESPONSE_NORMAL
:
1439 list_add_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1440 atomic_inc(&ap_poll_requests
);
1441 ap_dev
->pendingq_count
++;
1442 ap_increase_queue_count(ap_dev
);
1443 ap_dev
->total_request_count
++;
1445 case AP_RESPONSE_Q_FULL
:
1446 case AP_RESPONSE_RESET_IN_PROGRESS
:
1447 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1448 ap_dev
->requestq_count
++;
1449 ap_dev
->total_request_count
++;
1451 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1452 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1453 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-EINVAL
));
1455 default: /* Device is gone. */
1456 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1460 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1461 ap_dev
->requestq_count
++;
1462 ap_dev
->total_request_count
++;
1465 ap_schedule_poll_timer();
1469 void ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1471 unsigned long flags
;
1474 spin_lock_bh(&ap_dev
->lock
);
1475 if (!ap_dev
->unregistered
) {
1476 /* Make room on the queue by polling for finished requests. */
1477 rc
= ap_poll_queue(ap_dev
, &flags
);
1479 rc
= __ap_queue_message(ap_dev
, ap_msg
);
1481 wake_up(&ap_poll_wait
);
1483 ap_dev
->unregistered
= 1;
1485 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1488 spin_unlock_bh(&ap_dev
->lock
);
1490 device_unregister(&ap_dev
->device
);
1492 EXPORT_SYMBOL(ap_queue_message
);
1495 * ap_cancel_message(): Cancel a crypto request.
1496 * @ap_dev: The AP device that has the message queued
1497 * @ap_msg: The message that is to be removed
1499 * Cancel a crypto request. This is done by removing the request
1500 * from the device pending or request queue. Note that the
1501 * request stays on the AP queue. When it finishes the message
1502 * reply will be discarded because the psmid can't be found.
1504 void ap_cancel_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1506 struct ap_message
*tmp
;
1508 spin_lock_bh(&ap_dev
->lock
);
1509 if (!list_empty(&ap_msg
->list
)) {
1510 list_for_each_entry(tmp
, &ap_dev
->pendingq
, list
)
1511 if (tmp
->psmid
== ap_msg
->psmid
) {
1512 ap_dev
->pendingq_count
--;
1515 ap_dev
->requestq_count
--;
1517 list_del_init(&ap_msg
->list
);
1519 spin_unlock_bh(&ap_dev
->lock
);
1521 EXPORT_SYMBOL(ap_cancel_message
);
1524 * ap_poll_timeout(): AP receive polling for finished AP requests.
1525 * @unused: Unused pointer.
1527 * Schedules the AP tasklet using a high resolution timer.
1529 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
1531 tasklet_schedule(&ap_tasklet
);
1532 return HRTIMER_NORESTART
;
1536 * ap_reset(): Reset a not responding AP device.
1537 * @ap_dev: Pointer to the AP device
1539 * Reset a not responding AP device and move all requests from the
1540 * pending queue to the request queue.
1542 static void ap_reset(struct ap_device
*ap_dev
)
1546 ap_dev
->reset
= AP_RESET_IGNORE
;
1547 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1548 ap_dev
->queue_count
= 0;
1549 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1550 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1551 ap_dev
->pendingq_count
= 0;
1552 rc
= ap_init_queue(ap_dev
->qid
);
1554 ap_dev
->unregistered
= 1;
1556 __ap_schedule_poll_timer();
1559 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
)
1561 if (!ap_dev
->unregistered
) {
1562 if (ap_poll_queue(ap_dev
, flags
))
1563 ap_dev
->unregistered
= 1;
1564 if (ap_dev
->reset
== AP_RESET_DO
)
1571 * ap_poll_all(): Poll all AP devices.
1572 * @dummy: Unused variable
1574 * Poll all AP devices on the bus in a round robin fashion. Continue
1575 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1576 * of the control flags has been set arm the poll timer.
1578 static void ap_poll_all(unsigned long dummy
)
1580 unsigned long flags
;
1581 struct ap_device
*ap_dev
;
1583 /* Reset the indicator if interrupts are used. Thus new interrupts can
1584 * be received. Doing it in the beginning of the tasklet is therefor
1585 * important that no requests on any AP get lost.
1587 if (ap_using_interrupts())
1588 xchg((u8
*)ap_interrupt_indicator
, 0);
1591 spin_lock(&ap_device_list_lock
);
1592 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1593 spin_lock(&ap_dev
->lock
);
1594 __ap_poll_device(ap_dev
, &flags
);
1595 spin_unlock(&ap_dev
->lock
);
1597 spin_unlock(&ap_device_list_lock
);
1598 } while (flags
& 1);
1600 ap_schedule_poll_timer();
1604 * ap_poll_thread(): Thread that polls for finished requests.
1605 * @data: Unused pointer
1607 * AP bus poll thread. The purpose of this thread is to poll for
1608 * finished requests in a loop if there is a "free" cpu - that is
1609 * a cpu that doesn't have anything better to do. The polling stops
1610 * as soon as there is another task or if all messages have been
1613 static int ap_poll_thread(void *data
)
1615 DECLARE_WAITQUEUE(wait
, current
);
1616 unsigned long flags
;
1618 struct ap_device
*ap_dev
;
1620 set_user_nice(current
, 19);
1622 if (ap_suspend_flag
)
1624 if (need_resched()) {
1628 add_wait_queue(&ap_poll_wait
, &wait
);
1629 set_current_state(TASK_INTERRUPTIBLE
);
1630 if (kthread_should_stop())
1632 requests
= atomic_read(&ap_poll_requests
);
1635 set_current_state(TASK_RUNNING
);
1636 remove_wait_queue(&ap_poll_wait
, &wait
);
1639 spin_lock_bh(&ap_device_list_lock
);
1640 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1641 spin_lock(&ap_dev
->lock
);
1642 __ap_poll_device(ap_dev
, &flags
);
1643 spin_unlock(&ap_dev
->lock
);
1645 spin_unlock_bh(&ap_device_list_lock
);
1647 set_current_state(TASK_RUNNING
);
1648 remove_wait_queue(&ap_poll_wait
, &wait
);
1652 static int ap_poll_thread_start(void)
1656 if (ap_using_interrupts() || ap_suspend_flag
)
1658 mutex_lock(&ap_poll_thread_mutex
);
1659 if (!ap_poll_kthread
) {
1660 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
1661 rc
= IS_ERR(ap_poll_kthread
) ? PTR_ERR(ap_poll_kthread
) : 0;
1663 ap_poll_kthread
= NULL
;
1667 mutex_unlock(&ap_poll_thread_mutex
);
1671 static void ap_poll_thread_stop(void)
1673 mutex_lock(&ap_poll_thread_mutex
);
1674 if (ap_poll_kthread
) {
1675 kthread_stop(ap_poll_kthread
);
1676 ap_poll_kthread
= NULL
;
1678 mutex_unlock(&ap_poll_thread_mutex
);
1682 * ap_request_timeout(): Handling of request timeouts
1683 * @data: Holds the AP device.
1685 * Handles request timeouts.
1687 static void ap_request_timeout(unsigned long data
)
1689 struct ap_device
*ap_dev
= (struct ap_device
*) data
;
1691 if (ap_dev
->reset
== AP_RESET_ARMED
) {
1692 ap_dev
->reset
= AP_RESET_DO
;
1694 if (ap_using_interrupts())
1695 tasklet_schedule(&ap_tasklet
);
1699 static void ap_reset_domain(void)
1703 if (ap_domain_index
!= -1)
1704 for (i
= 0; i
< AP_DEVICES
; i
++)
1705 ap_reset_queue(AP_MKQID(i
, ap_domain_index
));
1708 static void ap_reset_all(void)
1712 for (i
= 0; i
< AP_DOMAINS
; i
++)
1713 for (j
= 0; j
< AP_DEVICES
; j
++)
1714 ap_reset_queue(AP_MKQID(j
, i
));
1717 static struct reset_call ap_reset_call
= {
1722 * ap_module_init(): The module initialization code.
1724 * Initializes the module.
1726 int __init
ap_module_init(void)
1730 if (ap_domain_index
< -1 || ap_domain_index
>= AP_DOMAINS
) {
1731 pr_warning("%d is not a valid cryptographic domain\n",
1735 /* In resume callback we need to know if the user had set the domain.
1736 * If so, we can not just reset it.
1738 if (ap_domain_index
>= 0)
1739 user_set_domain
= 1;
1741 if (ap_instructions_available() != 0) {
1742 pr_warning("The hardware system does not support "
1743 "AP instructions\n");
1746 if (ap_interrupts_available()) {
1747 isc_register(AP_ISC
);
1748 ap_interrupt_indicator
= s390_register_adapter_interrupt(
1749 &ap_interrupt_handler
, NULL
, AP_ISC
);
1750 if (IS_ERR(ap_interrupt_indicator
)) {
1751 ap_interrupt_indicator
= NULL
;
1752 isc_unregister(AP_ISC
);
1756 register_reset_call(&ap_reset_call
);
1758 /* Create /sys/bus/ap. */
1759 rc
= bus_register(&ap_bus_type
);
1762 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1763 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1768 /* Create /sys/devices/ap. */
1769 ap_root_device
= root_device_register("ap");
1770 rc
= IS_ERR(ap_root_device
) ? PTR_ERR(ap_root_device
) : 0;
1774 ap_work_queue
= create_singlethread_workqueue("kapwork");
1775 if (!ap_work_queue
) {
1780 if (ap_select_domain() == 0)
1783 /* Setup the AP bus rescan timer. */
1784 init_timer(&ap_config_timer
);
1785 ap_config_timer
.function
= ap_config_timeout
;
1786 ap_config_timer
.data
= 0;
1787 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1788 add_timer(&ap_config_timer
);
1790 /* Setup the high resultion poll timer.
1791 * If we are running under z/VM adjust polling to z/VM polling rate.
1794 poll_timeout
= 1500000;
1795 spin_lock_init(&ap_poll_timer_lock
);
1796 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1797 ap_poll_timer
.function
= ap_poll_timeout
;
1799 /* Start the low priority AP bus poll thread. */
1800 if (ap_thread_flag
) {
1801 rc
= ap_poll_thread_start();
1809 del_timer_sync(&ap_config_timer
);
1810 hrtimer_cancel(&ap_poll_timer
);
1811 destroy_workqueue(ap_work_queue
);
1813 root_device_unregister(ap_root_device
);
1816 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1817 bus_unregister(&ap_bus_type
);
1819 unregister_reset_call(&ap_reset_call
);
1820 if (ap_using_interrupts()) {
1821 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1822 isc_unregister(AP_ISC
);
1827 static int __ap_match_all(struct device
*dev
, void *data
)
1833 * ap_modules_exit(): The module termination code
1835 * Terminates the module.
1837 void ap_module_exit(void)
1843 ap_poll_thread_stop();
1844 del_timer_sync(&ap_config_timer
);
1845 hrtimer_cancel(&ap_poll_timer
);
1846 destroy_workqueue(ap_work_queue
);
1847 tasklet_kill(&ap_tasklet
);
1848 root_device_unregister(ap_root_device
);
1849 while ((dev
= bus_find_device(&ap_bus_type
, NULL
, NULL
,
1852 device_unregister(dev
);
1855 for (i
= 0; ap_bus_attrs
[i
]; i
++)
1856 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1857 bus_unregister(&ap_bus_type
);
1858 unregister_reset_call(&ap_reset_call
);
1859 if (ap_using_interrupts()) {
1860 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1861 isc_unregister(AP_ISC
);
1865 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1866 module_init(ap_module_init
);
1867 module_exit(ap_module_exit
);