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>
10 * Adjunct processor bus.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define KMSG_COMPONENT "ap"
28 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30 #include <linux/kernel_stat.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/err.h>
35 #include <linux/interrupt.h>
36 #include <linux/workqueue.h>
37 #include <linux/slab.h>
38 #include <linux/notifier.h>
39 #include <linux/kthread.h>
40 #include <linux/mutex.h>
41 #include <asm/reset.h>
43 #include <asm/atomic.h>
44 #include <asm/system.h>
46 #include <linux/hrtimer.h>
47 #include <linux/ktime.h>
51 /* Some prototypes. */
52 static void ap_scan_bus(struct work_struct
*);
53 static void ap_poll_all(unsigned long);
54 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*);
55 static int ap_poll_thread_start(void);
56 static void ap_poll_thread_stop(void);
57 static void ap_request_timeout(unsigned long);
58 static inline void ap_schedule_poll_timer(void);
59 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
);
60 static int ap_device_remove(struct device
*dev
);
61 static int ap_device_probe(struct device
*dev
);
62 static void ap_interrupt_handler(void *unused1
, void *unused2
);
63 static void ap_reset(struct ap_device
*ap_dev
);
64 static void ap_config_timeout(unsigned long ptr
);
65 static int ap_select_domain(void);
70 MODULE_AUTHOR("IBM Corporation");
71 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
72 "Copyright 2006 IBM Corporation");
73 MODULE_LICENSE("GPL");
78 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
79 module_param_named(domain
, ap_domain_index
, int, 0000);
80 MODULE_PARM_DESC(domain
, "domain index for ap devices");
81 EXPORT_SYMBOL(ap_domain_index
);
83 static int ap_thread_flag
= 0;
84 module_param_named(poll_thread
, ap_thread_flag
, int, 0000);
85 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
87 static struct device
*ap_root_device
= NULL
;
88 static DEFINE_SPINLOCK(ap_device_list_lock
);
89 static LIST_HEAD(ap_device_list
);
92 * Workqueue & timer for bus rescan.
94 static struct workqueue_struct
*ap_work_queue
;
95 static struct timer_list ap_config_timer
;
96 static int ap_config_time
= AP_CONFIG_TIME
;
97 static DECLARE_WORK(ap_config_work
, ap_scan_bus
);
100 * Tasklet & timer for AP request polling and interrupts
102 static DECLARE_TASKLET(ap_tasklet
, ap_poll_all
, 0);
103 static atomic_t ap_poll_requests
= ATOMIC_INIT(0);
104 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
105 static struct task_struct
*ap_poll_kthread
= NULL
;
106 static DEFINE_MUTEX(ap_poll_thread_mutex
);
107 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
108 static void *ap_interrupt_indicator
;
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 /* Flag to check if domain was set through module parameter domain=. This is
117 * important when supsend and resume is done in a z/VM environment where the
118 * domain might change. */
119 static int user_set_domain
= 0;
120 static struct bus_type ap_bus_type
;
123 * ap_using_interrupts() - Returns non-zero if interrupt support is
126 static inline int ap_using_interrupts(void)
128 return ap_interrupt_indicator
!= NULL
;
132 * ap_intructions_available() - Test if AP instructions are available.
134 * Returns 0 if the AP instructions are installed.
136 static inline int ap_instructions_available(void)
138 register unsigned long reg0
asm ("0") = AP_MKQID(0,0);
139 register unsigned long reg1
asm ("1") = -ENODEV
;
140 register unsigned long reg2
asm ("2") = 0UL;
143 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
147 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
) : : "cc" );
152 * ap_interrupts_available(): Test if AP interrupts are available.
154 * Returns 1 if AP interrupts are available.
156 static int ap_interrupts_available(void)
158 return test_facility(2) && test_facility(65);
162 * ap_test_queue(): Test adjunct processor queue.
163 * @qid: The AP queue number
164 * @queue_depth: Pointer to queue depth value
165 * @device_type: Pointer to device type value
167 * Returns AP queue status structure.
169 static inline struct ap_queue_status
170 ap_test_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
172 register unsigned long reg0
asm ("0") = qid
;
173 register struct ap_queue_status reg1
asm ("1");
174 register unsigned long reg2
asm ("2") = 0UL;
176 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
177 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
178 *device_type
= (int) (reg2
>> 24);
179 *queue_depth
= (int) (reg2
& 0xff);
184 * ap_reset_queue(): Reset adjunct processor queue.
185 * @qid: The AP queue number
187 * Returns AP queue status structure.
189 static inline struct ap_queue_status
ap_reset_queue(ap_qid_t qid
)
191 register unsigned long reg0
asm ("0") = qid
| 0x01000000UL
;
192 register struct ap_queue_status reg1
asm ("1");
193 register unsigned long reg2
asm ("2") = 0UL;
196 ".long 0xb2af0000" /* PQAP(RAPQ) */
197 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
203 * ap_queue_interruption_control(): Enable interruption for a specific AP.
204 * @qid: The AP queue number
205 * @ind: The notification indicator byte
207 * Returns AP queue status.
209 static inline struct ap_queue_status
210 ap_queue_interruption_control(ap_qid_t qid
, void *ind
)
212 register unsigned long reg0
asm ("0") = qid
| 0x03000000UL
;
213 register unsigned long reg1_in
asm ("1") = 0x0000800000000000UL
| AP_ISC
;
214 register struct ap_queue_status reg1_out
asm ("1");
215 register void *reg2
asm ("2") = ind
;
217 ".long 0xb2af0000" /* PQAP(RAPQ) */
218 : "+d" (reg0
), "+d" (reg1_in
), "=d" (reg1_out
), "+d" (reg2
)
225 static inline struct ap_queue_status
__ap_4096_commands_available(ap_qid_t qid
,
228 register unsigned long reg0
asm ("0") = 0UL | qid
| (1UL << 23);
229 register struct ap_queue_status reg1
asm ("1");
230 register unsigned long reg2
asm ("2") = 0UL;
237 : "+d" (reg0
), "=d" (reg1
), "=d" (reg2
)
241 if (reg2
& 0x6000000000000000ULL
)
250 * ap_4096_commands_availablen(): Check for availability of 4096 bit RSA
252 * @qid: The AP queue number
254 * Returns 1 if 4096 bit RSA keys are support fo the AP, returns 0 if not.
256 int ap_4096_commands_available(ap_qid_t qid
)
258 struct ap_queue_status status
;
260 status
= __ap_4096_commands_available(qid
, &support
);
262 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
263 switch (status
.response_code
) {
264 case AP_RESPONSE_NORMAL
:
266 case AP_RESPONSE_RESET_IN_PROGRESS
:
267 case AP_RESPONSE_BUSY
:
269 case AP_RESPONSE_Q_NOT_AVAIL
:
270 case AP_RESPONSE_DECONFIGURED
:
271 case AP_RESPONSE_CHECKSTOPPED
:
272 case AP_RESPONSE_INVALID_ADDRESS
:
274 case AP_RESPONSE_OTHERWISE_CHANGED
:
279 if (i
< AP_MAX_RESET
- 1) {
281 status
= __ap_4096_commands_available(qid
, &support
);
286 EXPORT_SYMBOL(ap_4096_commands_available
);
289 * ap_queue_enable_interruption(): Enable interruption on an AP.
290 * @qid: The AP queue number
291 * @ind: the notification indicator byte
293 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
294 * on the return value it waits a while and tests the AP queue if interrupts
295 * have been switched on using ap_test_queue().
297 static int ap_queue_enable_interruption(ap_qid_t qid
, void *ind
)
300 struct ap_queue_status status
;
301 int t_depth
, t_device_type
, rc
, i
;
304 status
= ap_queue_interruption_control(qid
, ind
);
306 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
307 switch (status
.response_code
) {
308 case AP_RESPONSE_NORMAL
:
309 if (status
.int_enabled
)
312 case AP_RESPONSE_RESET_IN_PROGRESS
:
313 case AP_RESPONSE_BUSY
:
315 case AP_RESPONSE_Q_NOT_AVAIL
:
316 case AP_RESPONSE_DECONFIGURED
:
317 case AP_RESPONSE_CHECKSTOPPED
:
318 case AP_RESPONSE_INVALID_ADDRESS
:
320 case AP_RESPONSE_OTHERWISE_CHANGED
:
321 if (status
.int_enabled
)
327 if (i
< AP_MAX_RESET
- 1) {
329 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
339 * __ap_send(): Send message to adjunct processor queue.
340 * @qid: The AP queue number
341 * @psmid: The program supplied message identifier
342 * @msg: The message text
343 * @length: The message length
344 * @special: Special Bit
346 * Returns AP queue status structure.
347 * Condition code 1 on NQAP can't happen because the L bit is 1.
348 * Condition code 2 on NQAP also means the send is incomplete,
349 * because a segment boundary was reached. The NQAP is repeated.
351 static inline struct ap_queue_status
352 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
,
353 unsigned int special
)
355 typedef struct { char _
[length
]; } msgblock
;
356 register unsigned long reg0
asm ("0") = qid
| 0x40000000UL
;
357 register struct ap_queue_status reg1
asm ("1");
358 register unsigned long reg2
asm ("2") = (unsigned long) msg
;
359 register unsigned long reg3
asm ("3") = (unsigned long) length
;
360 register unsigned long reg4
asm ("4") = (unsigned int) (psmid
>> 32);
361 register unsigned long reg5
asm ("5") = (unsigned int) psmid
;
367 "0: .long 0xb2ad0042\n" /* DQAP */
369 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
), "+d" (reg3
)
370 : "d" (reg4
), "d" (reg5
), "m" (*(msgblock
*) msg
)
375 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
377 struct ap_queue_status status
;
379 status
= __ap_send(qid
, psmid
, msg
, length
, 0);
380 switch (status
.response_code
) {
381 case AP_RESPONSE_NORMAL
:
383 case AP_RESPONSE_Q_FULL
:
384 case AP_RESPONSE_RESET_IN_PROGRESS
:
386 case AP_RESPONSE_REQ_FAC_NOT_INST
:
388 default: /* Device is gone. */
392 EXPORT_SYMBOL(ap_send
);
395 * __ap_recv(): Receive message from adjunct processor queue.
396 * @qid: The AP queue number
397 * @psmid: Pointer to program supplied message identifier
398 * @msg: The message text
399 * @length: The message length
401 * Returns AP queue status structure.
402 * Condition code 1 on DQAP means the receive has taken place
403 * but only partially. The response is incomplete, hence the
405 * Condition code 2 on DQAP also means the receive is incomplete,
406 * this time because a segment boundary was reached. Again, the
408 * Note that gpr2 is used by the DQAP instruction to keep track of
409 * any 'residual' length, in case the instruction gets interrupted.
410 * Hence it gets zeroed before the instruction.
412 static inline struct ap_queue_status
413 __ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
415 typedef struct { char _
[length
]; } msgblock
;
416 register unsigned long reg0
asm("0") = qid
| 0x80000000UL
;
417 register struct ap_queue_status reg1
asm ("1");
418 register unsigned long reg2
asm("2") = 0UL;
419 register unsigned long reg4
asm("4") = (unsigned long) msg
;
420 register unsigned long reg5
asm("5") = (unsigned long) length
;
421 register unsigned long reg6
asm("6") = 0UL;
422 register unsigned long reg7
asm("7") = 0UL;
426 "0: .long 0xb2ae0064\n"
428 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
),
429 "+d" (reg4
), "+d" (reg5
), "+d" (reg6
), "+d" (reg7
),
430 "=m" (*(msgblock
*) msg
) : : "cc" );
431 *psmid
= (((unsigned long long) reg6
) << 32) + reg7
;
435 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
437 struct ap_queue_status status
;
439 status
= __ap_recv(qid
, psmid
, msg
, length
);
440 switch (status
.response_code
) {
441 case AP_RESPONSE_NORMAL
:
443 case AP_RESPONSE_NO_PENDING_REPLY
:
444 if (status
.queue_empty
)
447 case AP_RESPONSE_RESET_IN_PROGRESS
:
453 EXPORT_SYMBOL(ap_recv
);
456 * ap_query_queue(): Check if an AP queue is available.
457 * @qid: The AP queue number
458 * @queue_depth: Pointer to queue depth value
459 * @device_type: Pointer to device type value
461 * The test is repeated for AP_MAX_RESET times.
463 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
465 struct ap_queue_status status
;
466 int t_depth
, t_device_type
, rc
, i
;
469 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
470 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
471 switch (status
.response_code
) {
472 case AP_RESPONSE_NORMAL
:
473 *queue_depth
= t_depth
+ 1;
474 *device_type
= t_device_type
;
477 case AP_RESPONSE_Q_NOT_AVAIL
:
480 case AP_RESPONSE_RESET_IN_PROGRESS
:
482 case AP_RESPONSE_DECONFIGURED
:
485 case AP_RESPONSE_CHECKSTOPPED
:
488 case AP_RESPONSE_INVALID_ADDRESS
:
491 case AP_RESPONSE_OTHERWISE_CHANGED
:
493 case AP_RESPONSE_BUSY
:
500 if (i
< AP_MAX_RESET
- 1)
507 * ap_init_queue(): Reset an AP queue.
508 * @qid: The AP queue number
510 * Reset an AP queue and wait for it to become available again.
512 static int ap_init_queue(ap_qid_t qid
)
514 struct ap_queue_status status
;
518 status
= ap_reset_queue(qid
);
519 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
520 switch (status
.response_code
) {
521 case AP_RESPONSE_NORMAL
:
522 if (status
.queue_empty
)
525 case AP_RESPONSE_Q_NOT_AVAIL
:
526 case AP_RESPONSE_DECONFIGURED
:
527 case AP_RESPONSE_CHECKSTOPPED
:
528 i
= AP_MAX_RESET
; /* return with -ENODEV */
530 case AP_RESPONSE_RESET_IN_PROGRESS
:
532 case AP_RESPONSE_BUSY
:
536 if (rc
!= -ENODEV
&& rc
!= -EBUSY
)
538 if (i
< AP_MAX_RESET
- 1) {
540 status
= ap_test_queue(qid
, &dummy
, &dummy
);
543 if (rc
== 0 && ap_using_interrupts()) {
544 rc
= ap_queue_enable_interruption(qid
, ap_interrupt_indicator
);
545 /* If interruption mode is supported by the machine,
546 * but an AP can not be enabled for interruption then
547 * the AP will be discarded. */
549 pr_err("Registering adapter interrupts for "
550 "AP %d failed\n", AP_QID_DEVICE(qid
));
556 * ap_increase_queue_count(): Arm request timeout.
557 * @ap_dev: Pointer to an AP device.
559 * Arm request timeout if an AP device was idle and a new request is submitted.
561 static void ap_increase_queue_count(struct ap_device
*ap_dev
)
563 int timeout
= ap_dev
->drv
->request_timeout
;
565 ap_dev
->queue_count
++;
566 if (ap_dev
->queue_count
== 1) {
567 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
568 ap_dev
->reset
= AP_RESET_ARMED
;
573 * ap_decrease_queue_count(): Decrease queue count.
574 * @ap_dev: Pointer to an AP device.
576 * If AP device is still alive, re-schedule request timeout if there are still
579 static void ap_decrease_queue_count(struct ap_device
*ap_dev
)
581 int timeout
= ap_dev
->drv
->request_timeout
;
583 ap_dev
->queue_count
--;
584 if (ap_dev
->queue_count
> 0)
585 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
588 * The timeout timer should to be disabled now - since
589 * del_timer_sync() is very expensive, we just tell via the
590 * reset flag to ignore the pending timeout timer.
592 ap_dev
->reset
= AP_RESET_IGNORE
;
596 * AP device related attributes.
598 static ssize_t
ap_hwtype_show(struct device
*dev
,
599 struct device_attribute
*attr
, char *buf
)
601 struct ap_device
*ap_dev
= to_ap_dev(dev
);
602 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->device_type
);
605 static DEVICE_ATTR(hwtype
, 0444, ap_hwtype_show
, NULL
);
606 static ssize_t
ap_depth_show(struct device
*dev
, struct device_attribute
*attr
,
609 struct ap_device
*ap_dev
= to_ap_dev(dev
);
610 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->queue_depth
);
613 static DEVICE_ATTR(depth
, 0444, ap_depth_show
, NULL
);
614 static ssize_t
ap_request_count_show(struct device
*dev
,
615 struct device_attribute
*attr
,
618 struct ap_device
*ap_dev
= to_ap_dev(dev
);
621 spin_lock_bh(&ap_dev
->lock
);
622 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->total_request_count
);
623 spin_unlock_bh(&ap_dev
->lock
);
627 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
629 static ssize_t
ap_modalias_show(struct device
*dev
,
630 struct device_attribute
*attr
, char *buf
)
632 return sprintf(buf
, "ap:t%02X", to_ap_dev(dev
)->device_type
);
635 static DEVICE_ATTR(modalias
, 0444, ap_modalias_show
, NULL
);
637 static struct attribute
*ap_dev_attrs
[] = {
638 &dev_attr_hwtype
.attr
,
639 &dev_attr_depth
.attr
,
640 &dev_attr_request_count
.attr
,
641 &dev_attr_modalias
.attr
,
644 static struct attribute_group ap_dev_attr_group
= {
645 .attrs
= ap_dev_attrs
650 * @dev: Pointer to device
651 * @drv: Pointer to device_driver
653 * AP bus driver registration/unregistration.
655 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
657 struct ap_device
*ap_dev
= to_ap_dev(dev
);
658 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
659 struct ap_device_id
*id
;
662 * Compare device type of the device with the list of
663 * supported types of the device_driver.
665 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
666 if ((id
->match_flags
& AP_DEVICE_ID_MATCH_DEVICE_TYPE
) &&
667 (id
->dev_type
!= ap_dev
->device_type
))
675 * ap_uevent(): Uevent function for AP devices.
676 * @dev: Pointer to device
677 * @env: Pointer to kobj_uevent_env
679 * It sets up a single environment variable DEV_TYPE which contains the
680 * hardware device type.
682 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
684 struct ap_device
*ap_dev
= to_ap_dev(dev
);
690 /* Set up DEV_TYPE environment variable. */
691 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
696 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
701 static int ap_bus_suspend(struct device
*dev
, pm_message_t state
)
703 struct ap_device
*ap_dev
= to_ap_dev(dev
);
706 if (!ap_suspend_flag
) {
709 /* Disable scanning for devices, thus we do not want to scan
710 * for them after removing.
712 del_timer_sync(&ap_config_timer
);
713 if (ap_work_queue
!= NULL
) {
714 destroy_workqueue(ap_work_queue
);
715 ap_work_queue
= NULL
;
718 tasklet_disable(&ap_tasklet
);
720 /* Poll on the device until all requests are finished. */
723 spin_lock_bh(&ap_dev
->lock
);
724 __ap_poll_device(ap_dev
, &flags
);
725 spin_unlock_bh(&ap_dev
->lock
);
726 } while ((flags
& 1) || (flags
& 2));
728 spin_lock_bh(&ap_dev
->lock
);
729 ap_dev
->unregistered
= 1;
730 spin_unlock_bh(&ap_dev
->lock
);
735 static int ap_bus_resume(struct device
*dev
)
738 struct ap_device
*ap_dev
= to_ap_dev(dev
);
740 if (ap_suspend_flag
) {
742 if (!ap_interrupts_available())
743 ap_interrupt_indicator
= NULL
;
744 if (!user_set_domain
) {
745 ap_domain_index
= -1;
748 init_timer(&ap_config_timer
);
749 ap_config_timer
.function
= ap_config_timeout
;
750 ap_config_timer
.data
= 0;
751 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
752 add_timer(&ap_config_timer
);
753 ap_work_queue
= create_singlethread_workqueue("kapwork");
756 tasklet_enable(&ap_tasklet
);
757 if (!ap_using_interrupts())
758 ap_schedule_poll_timer();
760 tasklet_schedule(&ap_tasklet
);
762 rc
= ap_poll_thread_start();
764 if (AP_QID_QUEUE(ap_dev
->qid
) != ap_domain_index
) {
765 spin_lock_bh(&ap_dev
->lock
);
766 ap_dev
->qid
= AP_MKQID(AP_QID_DEVICE(ap_dev
->qid
),
768 spin_unlock_bh(&ap_dev
->lock
);
770 queue_work(ap_work_queue
, &ap_config_work
);
775 static struct bus_type ap_bus_type
= {
777 .match
= &ap_bus_match
,
778 .uevent
= &ap_uevent
,
779 .suspend
= ap_bus_suspend
,
780 .resume
= ap_bus_resume
783 static int ap_device_probe(struct device
*dev
)
785 struct ap_device
*ap_dev
= to_ap_dev(dev
);
786 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
789 ap_dev
->drv
= ap_drv
;
790 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
792 spin_lock_bh(&ap_device_list_lock
);
793 list_add(&ap_dev
->list
, &ap_device_list
);
794 spin_unlock_bh(&ap_device_list_lock
);
800 * __ap_flush_queue(): Flush requests.
801 * @ap_dev: Pointer to the AP device
803 * Flush all requests from the request/pending queue of an AP device.
805 static void __ap_flush_queue(struct ap_device
*ap_dev
)
807 struct ap_message
*ap_msg
, *next
;
809 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->pendingq
, list
) {
810 list_del_init(&ap_msg
->list
);
811 ap_dev
->pendingq_count
--;
812 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
814 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->requestq
, list
) {
815 list_del_init(&ap_msg
->list
);
816 ap_dev
->requestq_count
--;
817 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
821 void ap_flush_queue(struct ap_device
*ap_dev
)
823 spin_lock_bh(&ap_dev
->lock
);
824 __ap_flush_queue(ap_dev
);
825 spin_unlock_bh(&ap_dev
->lock
);
827 EXPORT_SYMBOL(ap_flush_queue
);
829 static int ap_device_remove(struct device
*dev
)
831 struct ap_device
*ap_dev
= to_ap_dev(dev
);
832 struct ap_driver
*ap_drv
= ap_dev
->drv
;
834 ap_flush_queue(ap_dev
);
835 del_timer_sync(&ap_dev
->timeout
);
836 spin_lock_bh(&ap_device_list_lock
);
837 list_del_init(&ap_dev
->list
);
838 spin_unlock_bh(&ap_device_list_lock
);
840 ap_drv
->remove(ap_dev
);
841 spin_lock_bh(&ap_dev
->lock
);
842 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
843 spin_unlock_bh(&ap_dev
->lock
);
847 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
850 struct device_driver
*drv
= &ap_drv
->driver
;
852 drv
->bus
= &ap_bus_type
;
853 drv
->probe
= ap_device_probe
;
854 drv
->remove
= ap_device_remove
;
857 return driver_register(drv
);
859 EXPORT_SYMBOL(ap_driver_register
);
861 void ap_driver_unregister(struct ap_driver
*ap_drv
)
863 driver_unregister(&ap_drv
->driver
);
865 EXPORT_SYMBOL(ap_driver_unregister
);
870 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
872 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
875 static BUS_ATTR(ap_domain
, 0444, ap_domain_show
, NULL
);
877 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
879 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
882 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
884 return snprintf(buf
, PAGE_SIZE
, "%d\n",
885 ap_using_interrupts() ? 1 : 0);
888 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
890 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
891 const char *buf
, size_t count
)
895 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
897 ap_config_time
= time
;
898 if (!timer_pending(&ap_config_timer
) ||
899 !mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
)) {
900 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
901 add_timer(&ap_config_timer
);
906 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
908 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
910 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
913 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
914 const char *buf
, size_t count
)
918 if (sscanf(buf
, "%d\n", &flag
) != 1)
921 rc
= ap_poll_thread_start();
926 ap_poll_thread_stop();
930 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
932 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
934 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
937 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
940 unsigned long long time
;
943 /* 120 seconds = maximum poll interval */
944 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
945 time
> 120000000000ULL)
948 hr_time
= ktime_set(0, poll_timeout
);
950 if (!hrtimer_is_queued(&ap_poll_timer
) ||
951 !hrtimer_forward(&ap_poll_timer
, hrtimer_get_expires(&ap_poll_timer
), hr_time
)) {
952 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
953 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
958 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
960 static struct bus_attribute
*const ap_bus_attrs
[] = {
962 &bus_attr_config_time
,
963 &bus_attr_poll_thread
,
964 &bus_attr_ap_interrupts
,
965 &bus_attr_poll_timeout
,
970 * ap_select_domain(): Select an AP domain.
972 * Pick one of the 16 AP domains.
974 static int ap_select_domain(void)
976 int queue_depth
, device_type
, count
, max_count
, best_domain
;
980 * We want to use a single domain. Either the one specified with
981 * the "domain=" parameter or the domain with the maximum number
984 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
)
985 /* Domain has already been selected. */
989 for (i
= 0; i
< AP_DOMAINS
; i
++) {
991 for (j
= 0; j
< AP_DEVICES
; j
++) {
992 ap_qid_t qid
= AP_MKQID(j
, i
);
993 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
998 if (count
> max_count
) {
1003 if (best_domain
>= 0){
1004 ap_domain_index
= best_domain
;
1011 * ap_probe_device_type(): Find the device type of an AP.
1012 * @ap_dev: pointer to the AP device.
1014 * Find the device type if query queue returned a device type of 0.
1016 static int ap_probe_device_type(struct ap_device
*ap_dev
)
1018 static unsigned char msg
[] = {
1019 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1020 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1021 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1022 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1023 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1024 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1025 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1026 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1027 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1028 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1029 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1030 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1031 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1032 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1033 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1034 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1035 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1036 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1037 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1038 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1039 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1040 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1041 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1042 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1043 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1044 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1045 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1046 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1047 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1048 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1049 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1050 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1051 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1052 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1053 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1054 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1055 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1056 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1057 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1058 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1059 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1060 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1061 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1062 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1063 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1065 struct ap_queue_status status
;
1066 unsigned long long psmid
;
1070 reply
= (void *) get_zeroed_page(GFP_KERNEL
);
1076 status
= __ap_send(ap_dev
->qid
, 0x0102030405060708ULL
,
1077 msg
, sizeof(msg
), 0);
1078 if (status
.response_code
!= AP_RESPONSE_NORMAL
) {
1083 /* Wait for the test message to complete. */
1084 for (i
= 0; i
< 6; i
++) {
1086 status
= __ap_recv(ap_dev
->qid
, &psmid
, reply
, 4096);
1087 if (status
.response_code
== AP_RESPONSE_NORMAL
&&
1088 psmid
== 0x0102030405060708ULL
)
1092 /* Got an answer. */
1093 if (reply
[0] == 0x00 && reply
[1] == 0x86)
1094 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICC
;
1096 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICA
;
1102 free_page((unsigned long) reply
);
1107 static void ap_interrupt_handler(void *unused1
, void *unused2
)
1109 kstat_cpu(smp_processor_id()).irqs
[IOINT_APB
]++;
1110 tasklet_schedule(&ap_tasklet
);
1114 * __ap_scan_bus(): Scan the AP bus.
1115 * @dev: Pointer to device
1116 * @data: Pointer to data
1118 * Scan the AP bus for new devices.
1120 static int __ap_scan_bus(struct device
*dev
, void *data
)
1122 return to_ap_dev(dev
)->qid
== (ap_qid_t
)(unsigned long) data
;
1125 static void ap_device_release(struct device
*dev
)
1127 struct ap_device
*ap_dev
= to_ap_dev(dev
);
1132 static void ap_scan_bus(struct work_struct
*unused
)
1134 struct ap_device
*ap_dev
;
1137 int queue_depth
, device_type
;
1140 if (ap_select_domain() != 0)
1142 for (i
= 0; i
< AP_DEVICES
; i
++) {
1143 qid
= AP_MKQID(i
, ap_domain_index
);
1144 dev
= bus_find_device(&ap_bus_type
, NULL
,
1145 (void *)(unsigned long)qid
,
1147 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1150 set_current_state(TASK_UNINTERRUPTIBLE
);
1151 schedule_timeout(AP_RESET_TIMEOUT
);
1152 rc
= ap_query_queue(qid
, &queue_depth
,
1155 ap_dev
= to_ap_dev(dev
);
1156 spin_lock_bh(&ap_dev
->lock
);
1157 if (rc
|| ap_dev
->unregistered
) {
1158 spin_unlock_bh(&ap_dev
->lock
);
1159 if (ap_dev
->unregistered
)
1161 device_unregister(dev
);
1165 spin_unlock_bh(&ap_dev
->lock
);
1171 rc
= ap_init_queue(qid
);
1174 ap_dev
= kzalloc(sizeof(*ap_dev
), GFP_KERNEL
);
1178 ap_dev
->queue_depth
= queue_depth
;
1179 ap_dev
->unregistered
= 1;
1180 spin_lock_init(&ap_dev
->lock
);
1181 INIT_LIST_HEAD(&ap_dev
->pendingq
);
1182 INIT_LIST_HEAD(&ap_dev
->requestq
);
1183 INIT_LIST_HEAD(&ap_dev
->list
);
1184 setup_timer(&ap_dev
->timeout
, ap_request_timeout
,
1185 (unsigned long) ap_dev
);
1186 if (device_type
== 0) {
1187 if (ap_probe_device_type(ap_dev
)) {
1193 ap_dev
->device_type
= device_type
;
1195 ap_dev
->device
.bus
= &ap_bus_type
;
1196 ap_dev
->device
.parent
= ap_root_device
;
1197 if (dev_set_name(&ap_dev
->device
, "card%02x",
1198 AP_QID_DEVICE(ap_dev
->qid
))) {
1202 ap_dev
->device
.release
= ap_device_release
;
1203 rc
= device_register(&ap_dev
->device
);
1205 put_device(&ap_dev
->device
);
1208 /* Add device attributes. */
1209 rc
= sysfs_create_group(&ap_dev
->device
.kobj
,
1210 &ap_dev_attr_group
);
1212 spin_lock_bh(&ap_dev
->lock
);
1213 ap_dev
->unregistered
= 0;
1214 spin_unlock_bh(&ap_dev
->lock
);
1217 device_unregister(&ap_dev
->device
);
1222 ap_config_timeout(unsigned long ptr
)
1224 queue_work(ap_work_queue
, &ap_config_work
);
1225 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1226 add_timer(&ap_config_timer
);
1230 * ap_schedule_poll_timer(): Schedule poll timer.
1232 * Set up the timer to run the poll tasklet
1234 static inline void ap_schedule_poll_timer(void)
1238 spin_lock_bh(&ap_poll_timer_lock
);
1239 if (ap_using_interrupts() || ap_suspend_flag
)
1241 if (hrtimer_is_queued(&ap_poll_timer
))
1243 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer
)) <= 0) {
1244 hr_time
= ktime_set(0, poll_timeout
);
1245 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
1246 hrtimer_restart(&ap_poll_timer
);
1249 spin_unlock_bh(&ap_poll_timer_lock
);
1253 * ap_poll_read(): Receive pending reply messages from an AP device.
1254 * @ap_dev: pointer to the AP device
1255 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1256 * required, bit 2^1 is set if the poll timer needs to get armed
1258 * Returns 0 if the device is still present, -ENODEV if not.
1260 static int ap_poll_read(struct ap_device
*ap_dev
, unsigned long *flags
)
1262 struct ap_queue_status status
;
1263 struct ap_message
*ap_msg
;
1265 if (ap_dev
->queue_count
<= 0)
1267 status
= __ap_recv(ap_dev
->qid
, &ap_dev
->reply
->psmid
,
1268 ap_dev
->reply
->message
, ap_dev
->reply
->length
);
1269 switch (status
.response_code
) {
1270 case AP_RESPONSE_NORMAL
:
1271 atomic_dec(&ap_poll_requests
);
1272 ap_decrease_queue_count(ap_dev
);
1273 list_for_each_entry(ap_msg
, &ap_dev
->pendingq
, list
) {
1274 if (ap_msg
->psmid
!= ap_dev
->reply
->psmid
)
1276 list_del_init(&ap_msg
->list
);
1277 ap_dev
->pendingq_count
--;
1278 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ap_dev
->reply
);
1281 if (ap_dev
->queue_count
> 0)
1284 case AP_RESPONSE_NO_PENDING_REPLY
:
1285 if (status
.queue_empty
) {
1286 /* The card shouldn't forget requests but who knows. */
1287 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1288 ap_dev
->queue_count
= 0;
1289 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1290 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1291 ap_dev
->pendingq_count
= 0;
1302 * ap_poll_write(): Send messages from the request queue to an AP device.
1303 * @ap_dev: pointer to the AP device
1304 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1305 * required, bit 2^1 is set if the poll timer needs to get armed
1307 * Returns 0 if the device is still present, -ENODEV if not.
1309 static int ap_poll_write(struct ap_device
*ap_dev
, unsigned long *flags
)
1311 struct ap_queue_status status
;
1312 struct ap_message
*ap_msg
;
1314 if (ap_dev
->requestq_count
<= 0 ||
1315 ap_dev
->queue_count
>= ap_dev
->queue_depth
)
1317 /* Start the next request on the queue. */
1318 ap_msg
= list_entry(ap_dev
->requestq
.next
, struct ap_message
, list
);
1319 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1320 ap_msg
->message
, ap_msg
->length
, ap_msg
->special
);
1321 switch (status
.response_code
) {
1322 case AP_RESPONSE_NORMAL
:
1323 atomic_inc(&ap_poll_requests
);
1324 ap_increase_queue_count(ap_dev
);
1325 list_move_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1326 ap_dev
->requestq_count
--;
1327 ap_dev
->pendingq_count
++;
1328 if (ap_dev
->queue_count
< ap_dev
->queue_depth
&&
1329 ap_dev
->requestq_count
> 0)
1333 case AP_RESPONSE_Q_FULL
:
1334 case AP_RESPONSE_RESET_IN_PROGRESS
:
1337 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1338 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1347 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1348 * @ap_dev: pointer to the bus device
1349 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1350 * required, bit 2^1 is set if the poll timer needs to get armed
1352 * Poll AP device for pending replies and send new messages. If either
1353 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1356 static inline int ap_poll_queue(struct ap_device
*ap_dev
, unsigned long *flags
)
1360 rc
= ap_poll_read(ap_dev
, flags
);
1363 return ap_poll_write(ap_dev
, flags
);
1367 * __ap_queue_message(): Queue a message to a device.
1368 * @ap_dev: pointer to the AP device
1369 * @ap_msg: the message to be queued
1371 * Queue a message to a device. Returns 0 if successful.
1373 static int __ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1375 struct ap_queue_status status
;
1377 if (list_empty(&ap_dev
->requestq
) &&
1378 ap_dev
->queue_count
< ap_dev
->queue_depth
) {
1379 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1380 ap_msg
->message
, ap_msg
->length
,
1382 switch (status
.response_code
) {
1383 case AP_RESPONSE_NORMAL
:
1384 list_add_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1385 atomic_inc(&ap_poll_requests
);
1386 ap_dev
->pendingq_count
++;
1387 ap_increase_queue_count(ap_dev
);
1388 ap_dev
->total_request_count
++;
1390 case AP_RESPONSE_Q_FULL
:
1391 case AP_RESPONSE_RESET_IN_PROGRESS
:
1392 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1393 ap_dev
->requestq_count
++;
1394 ap_dev
->total_request_count
++;
1396 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1397 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1398 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-EINVAL
));
1400 default: /* Device is gone. */
1401 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1405 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1406 ap_dev
->requestq_count
++;
1407 ap_dev
->total_request_count
++;
1410 ap_schedule_poll_timer();
1414 void ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1416 unsigned long flags
;
1419 spin_lock_bh(&ap_dev
->lock
);
1420 if (!ap_dev
->unregistered
) {
1421 /* Make room on the queue by polling for finished requests. */
1422 rc
= ap_poll_queue(ap_dev
, &flags
);
1424 rc
= __ap_queue_message(ap_dev
, ap_msg
);
1426 wake_up(&ap_poll_wait
);
1428 ap_dev
->unregistered
= 1;
1430 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1433 spin_unlock_bh(&ap_dev
->lock
);
1435 device_unregister(&ap_dev
->device
);
1437 EXPORT_SYMBOL(ap_queue_message
);
1440 * ap_cancel_message(): Cancel a crypto request.
1441 * @ap_dev: The AP device that has the message queued
1442 * @ap_msg: The message that is to be removed
1444 * Cancel a crypto request. This is done by removing the request
1445 * from the device pending or request queue. Note that the
1446 * request stays on the AP queue. When it finishes the message
1447 * reply will be discarded because the psmid can't be found.
1449 void ap_cancel_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1451 struct ap_message
*tmp
;
1453 spin_lock_bh(&ap_dev
->lock
);
1454 if (!list_empty(&ap_msg
->list
)) {
1455 list_for_each_entry(tmp
, &ap_dev
->pendingq
, list
)
1456 if (tmp
->psmid
== ap_msg
->psmid
) {
1457 ap_dev
->pendingq_count
--;
1460 ap_dev
->requestq_count
--;
1462 list_del_init(&ap_msg
->list
);
1464 spin_unlock_bh(&ap_dev
->lock
);
1466 EXPORT_SYMBOL(ap_cancel_message
);
1469 * ap_poll_timeout(): AP receive polling for finished AP requests.
1470 * @unused: Unused pointer.
1472 * Schedules the AP tasklet using a high resolution timer.
1474 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
1476 tasklet_schedule(&ap_tasklet
);
1477 return HRTIMER_NORESTART
;
1481 * ap_reset(): Reset a not responding AP device.
1482 * @ap_dev: Pointer to the AP device
1484 * Reset a not responding AP device and move all requests from the
1485 * pending queue to the request queue.
1487 static void ap_reset(struct ap_device
*ap_dev
)
1491 ap_dev
->reset
= AP_RESET_IGNORE
;
1492 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1493 ap_dev
->queue_count
= 0;
1494 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1495 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1496 ap_dev
->pendingq_count
= 0;
1497 rc
= ap_init_queue(ap_dev
->qid
);
1499 ap_dev
->unregistered
= 1;
1502 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
)
1504 if (!ap_dev
->unregistered
) {
1505 if (ap_poll_queue(ap_dev
, flags
))
1506 ap_dev
->unregistered
= 1;
1507 if (ap_dev
->reset
== AP_RESET_DO
)
1514 * ap_poll_all(): Poll all AP devices.
1515 * @dummy: Unused variable
1517 * Poll all AP devices on the bus in a round robin fashion. Continue
1518 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1519 * of the control flags has been set arm the poll timer.
1521 static void ap_poll_all(unsigned long dummy
)
1523 unsigned long flags
;
1524 struct ap_device
*ap_dev
;
1526 /* Reset the indicator if interrupts are used. Thus new interrupts can
1527 * be received. Doing it in the beginning of the tasklet is therefor
1528 * important that no requests on any AP get lost.
1530 if (ap_using_interrupts())
1531 xchg((u8
*)ap_interrupt_indicator
, 0);
1534 spin_lock(&ap_device_list_lock
);
1535 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1536 spin_lock(&ap_dev
->lock
);
1537 __ap_poll_device(ap_dev
, &flags
);
1538 spin_unlock(&ap_dev
->lock
);
1540 spin_unlock(&ap_device_list_lock
);
1541 } while (flags
& 1);
1543 ap_schedule_poll_timer();
1547 * ap_poll_thread(): Thread that polls for finished requests.
1548 * @data: Unused pointer
1550 * AP bus poll thread. The purpose of this thread is to poll for
1551 * finished requests in a loop if there is a "free" cpu - that is
1552 * a cpu that doesn't have anything better to do. The polling stops
1553 * as soon as there is another task or if all messages have been
1556 static int ap_poll_thread(void *data
)
1558 DECLARE_WAITQUEUE(wait
, current
);
1559 unsigned long flags
;
1561 struct ap_device
*ap_dev
;
1563 set_user_nice(current
, 19);
1565 if (ap_suspend_flag
)
1567 if (need_resched()) {
1571 add_wait_queue(&ap_poll_wait
, &wait
);
1572 set_current_state(TASK_INTERRUPTIBLE
);
1573 if (kthread_should_stop())
1575 requests
= atomic_read(&ap_poll_requests
);
1578 set_current_state(TASK_RUNNING
);
1579 remove_wait_queue(&ap_poll_wait
, &wait
);
1582 spin_lock_bh(&ap_device_list_lock
);
1583 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1584 spin_lock(&ap_dev
->lock
);
1585 __ap_poll_device(ap_dev
, &flags
);
1586 spin_unlock(&ap_dev
->lock
);
1588 spin_unlock_bh(&ap_device_list_lock
);
1590 set_current_state(TASK_RUNNING
);
1591 remove_wait_queue(&ap_poll_wait
, &wait
);
1595 static int ap_poll_thread_start(void)
1599 if (ap_using_interrupts() || ap_suspend_flag
)
1601 mutex_lock(&ap_poll_thread_mutex
);
1602 if (!ap_poll_kthread
) {
1603 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
1604 rc
= IS_ERR(ap_poll_kthread
) ? PTR_ERR(ap_poll_kthread
) : 0;
1606 ap_poll_kthread
= NULL
;
1610 mutex_unlock(&ap_poll_thread_mutex
);
1614 static void ap_poll_thread_stop(void)
1616 mutex_lock(&ap_poll_thread_mutex
);
1617 if (ap_poll_kthread
) {
1618 kthread_stop(ap_poll_kthread
);
1619 ap_poll_kthread
= NULL
;
1621 mutex_unlock(&ap_poll_thread_mutex
);
1625 * ap_request_timeout(): Handling of request timeouts
1626 * @data: Holds the AP device.
1628 * Handles request timeouts.
1630 static void ap_request_timeout(unsigned long data
)
1632 struct ap_device
*ap_dev
= (struct ap_device
*) data
;
1634 if (ap_dev
->reset
== AP_RESET_ARMED
) {
1635 ap_dev
->reset
= AP_RESET_DO
;
1637 if (ap_using_interrupts())
1638 tasklet_schedule(&ap_tasklet
);
1642 static void ap_reset_domain(void)
1646 if (ap_domain_index
!= -1)
1647 for (i
= 0; i
< AP_DEVICES
; i
++)
1648 ap_reset_queue(AP_MKQID(i
, ap_domain_index
));
1651 static void ap_reset_all(void)
1655 for (i
= 0; i
< AP_DOMAINS
; i
++)
1656 for (j
= 0; j
< AP_DEVICES
; j
++)
1657 ap_reset_queue(AP_MKQID(j
, i
));
1660 static struct reset_call ap_reset_call
= {
1665 * ap_module_init(): The module initialization code.
1667 * Initializes the module.
1669 int __init
ap_module_init(void)
1673 if (ap_domain_index
< -1 || ap_domain_index
>= AP_DOMAINS
) {
1674 pr_warning("%d is not a valid cryptographic domain\n",
1678 /* In resume callback we need to know if the user had set the domain.
1679 * If so, we can not just reset it.
1681 if (ap_domain_index
>= 0)
1682 user_set_domain
= 1;
1684 if (ap_instructions_available() != 0) {
1685 pr_warning("The hardware system does not support "
1686 "AP instructions\n");
1689 if (ap_interrupts_available()) {
1690 isc_register(AP_ISC
);
1691 ap_interrupt_indicator
= s390_register_adapter_interrupt(
1692 &ap_interrupt_handler
, NULL
, AP_ISC
);
1693 if (IS_ERR(ap_interrupt_indicator
)) {
1694 ap_interrupt_indicator
= NULL
;
1695 isc_unregister(AP_ISC
);
1699 register_reset_call(&ap_reset_call
);
1701 /* Create /sys/bus/ap. */
1702 rc
= bus_register(&ap_bus_type
);
1705 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1706 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1711 /* Create /sys/devices/ap. */
1712 ap_root_device
= root_device_register("ap");
1713 rc
= IS_ERR(ap_root_device
) ? PTR_ERR(ap_root_device
) : 0;
1717 ap_work_queue
= create_singlethread_workqueue("kapwork");
1718 if (!ap_work_queue
) {
1723 if (ap_select_domain() == 0)
1726 /* Setup the AP bus rescan timer. */
1727 init_timer(&ap_config_timer
);
1728 ap_config_timer
.function
= ap_config_timeout
;
1729 ap_config_timer
.data
= 0;
1730 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1731 add_timer(&ap_config_timer
);
1733 /* Setup the high resultion poll timer.
1734 * If we are running under z/VM adjust polling to z/VM polling rate.
1737 poll_timeout
= 1500000;
1738 spin_lock_init(&ap_poll_timer_lock
);
1739 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1740 ap_poll_timer
.function
= ap_poll_timeout
;
1742 /* Start the low priority AP bus poll thread. */
1743 if (ap_thread_flag
) {
1744 rc
= ap_poll_thread_start();
1752 del_timer_sync(&ap_config_timer
);
1753 hrtimer_cancel(&ap_poll_timer
);
1754 destroy_workqueue(ap_work_queue
);
1756 root_device_unregister(ap_root_device
);
1759 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1760 bus_unregister(&ap_bus_type
);
1762 unregister_reset_call(&ap_reset_call
);
1763 if (ap_using_interrupts()) {
1764 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1765 isc_unregister(AP_ISC
);
1770 static int __ap_match_all(struct device
*dev
, void *data
)
1776 * ap_modules_exit(): The module termination code
1778 * Terminates the module.
1780 void ap_module_exit(void)
1786 ap_poll_thread_stop();
1787 del_timer_sync(&ap_config_timer
);
1788 hrtimer_cancel(&ap_poll_timer
);
1789 destroy_workqueue(ap_work_queue
);
1790 tasklet_kill(&ap_tasklet
);
1791 root_device_unregister(ap_root_device
);
1792 while ((dev
= bus_find_device(&ap_bus_type
, NULL
, NULL
,
1795 device_unregister(dev
);
1798 for (i
= 0; ap_bus_attrs
[i
]; i
++)
1799 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1800 bus_unregister(&ap_bus_type
);
1801 unregister_reset_call(&ap_reset_call
);
1802 if (ap_using_interrupts()) {
1803 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1804 isc_unregister(AP_ISC
);
1808 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1809 module_init(ap_module_init
);
1810 module_exit(ap_module_exit
);