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/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <asm/reset.h>
42 #include <asm/atomic.h>
43 #include <asm/system.h>
45 #include <linux/hrtimer.h>
46 #include <linux/ktime.h>
50 /* Some prototypes. */
51 static void ap_scan_bus(struct work_struct
*);
52 static void ap_poll_all(unsigned long);
53 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*);
54 static int ap_poll_thread_start(void);
55 static void ap_poll_thread_stop(void);
56 static void ap_request_timeout(unsigned long);
57 static inline void ap_schedule_poll_timer(void);
58 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
);
59 static int ap_device_remove(struct device
*dev
);
60 static int ap_device_probe(struct device
*dev
);
61 static void ap_interrupt_handler(void *unused1
, void *unused2
);
62 static void ap_reset(struct ap_device
*ap_dev
);
63 static void ap_config_timeout(unsigned long ptr
);
64 static int ap_select_domain(void);
69 MODULE_AUTHOR("IBM Corporation");
70 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
71 "Copyright 2006 IBM Corporation");
72 MODULE_LICENSE("GPL");
77 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
78 module_param_named(domain
, ap_domain_index
, int, 0000);
79 MODULE_PARM_DESC(domain
, "domain index for ap devices");
80 EXPORT_SYMBOL(ap_domain_index
);
82 static int ap_thread_flag
= 0;
83 module_param_named(poll_thread
, ap_thread_flag
, int, 0000);
84 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
86 static struct device
*ap_root_device
= NULL
;
87 static DEFINE_SPINLOCK(ap_device_list_lock
);
88 static LIST_HEAD(ap_device_list
);
91 * Workqueue & timer for bus rescan.
93 static struct workqueue_struct
*ap_work_queue
;
94 static struct timer_list ap_config_timer
;
95 static int ap_config_time
= AP_CONFIG_TIME
;
96 static DECLARE_WORK(ap_config_work
, ap_scan_bus
);
99 * Tasklet & timer for AP request polling and interrupts
101 static DECLARE_TASKLET(ap_tasklet
, ap_poll_all
, 0);
102 static atomic_t ap_poll_requests
= ATOMIC_INIT(0);
103 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
104 static struct task_struct
*ap_poll_kthread
= NULL
;
105 static DEFINE_MUTEX(ap_poll_thread_mutex
);
106 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
107 static void *ap_interrupt_indicator
;
108 static struct hrtimer ap_poll_timer
;
109 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
110 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
111 static unsigned long long poll_timeout
= 250000;
114 static int ap_suspend_flag
;
115 /* Flag to check if domain was set through module parameter domain=. This is
116 * important when supsend and resume is done in a z/VM environment where the
117 * domain might change. */
118 static int user_set_domain
= 0;
119 static struct bus_type ap_bus_type
;
122 * ap_using_interrupts() - Returns non-zero if interrupt support is
125 static inline int ap_using_interrupts(void)
127 return ap_interrupt_indicator
!= NULL
;
131 * ap_intructions_available() - Test if AP instructions are available.
133 * Returns 0 if the AP instructions are installed.
135 static inline int ap_instructions_available(void)
137 register unsigned long reg0
asm ("0") = AP_MKQID(0,0);
138 register unsigned long reg1
asm ("1") = -ENODEV
;
139 register unsigned long reg2
asm ("2") = 0UL;
142 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
146 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
) : : "cc" );
151 * ap_interrupts_available(): Test if AP interrupts are available.
153 * Returns 1 if AP interrupts are available.
155 static int ap_interrupts_available(void)
157 unsigned long long facility_bits
[2];
159 if (stfle(facility_bits
, 2) <= 1)
161 if (!(facility_bits
[0] & (1ULL << 61)) ||
162 !(facility_bits
[1] & (1ULL << 62)))
168 * ap_test_queue(): Test adjunct processor queue.
169 * @qid: The AP queue number
170 * @queue_depth: Pointer to queue depth value
171 * @device_type: Pointer to device type value
173 * Returns AP queue status structure.
175 static inline struct ap_queue_status
176 ap_test_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
178 register unsigned long reg0
asm ("0") = qid
;
179 register struct ap_queue_status reg1
asm ("1");
180 register unsigned long reg2
asm ("2") = 0UL;
182 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
183 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
184 *device_type
= (int) (reg2
>> 24);
185 *queue_depth
= (int) (reg2
& 0xff);
190 * ap_reset_queue(): Reset adjunct processor queue.
191 * @qid: The AP queue number
193 * Returns AP queue status structure.
195 static inline struct ap_queue_status
ap_reset_queue(ap_qid_t qid
)
197 register unsigned long reg0
asm ("0") = qid
| 0x01000000UL
;
198 register struct ap_queue_status reg1
asm ("1");
199 register unsigned long reg2
asm ("2") = 0UL;
202 ".long 0xb2af0000" /* PQAP(RAPQ) */
203 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
209 * ap_queue_interruption_control(): Enable interruption for a specific AP.
210 * @qid: The AP queue number
211 * @ind: The notification indicator byte
213 * Returns AP queue status.
215 static inline struct ap_queue_status
216 ap_queue_interruption_control(ap_qid_t qid
, void *ind
)
218 register unsigned long reg0
asm ("0") = qid
| 0x03000000UL
;
219 register unsigned long reg1_in
asm ("1") = 0x0000800000000000UL
| AP_ISC
;
220 register struct ap_queue_status reg1_out
asm ("1");
221 register void *reg2
asm ("2") = ind
;
223 ".long 0xb2af0000" /* PQAP(RAPQ) */
224 : "+d" (reg0
), "+d" (reg1_in
), "=d" (reg1_out
), "+d" (reg2
)
232 * ap_queue_enable_interruption(): Enable interruption on an AP.
233 * @qid: The AP queue number
234 * @ind: the notification indicator byte
236 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
237 * on the return value it waits a while and tests the AP queue if interrupts
238 * have been switched on using ap_test_queue().
240 static int ap_queue_enable_interruption(ap_qid_t qid
, void *ind
)
243 struct ap_queue_status status
;
244 int t_depth
, t_device_type
, rc
, i
;
247 status
= ap_queue_interruption_control(qid
, ind
);
249 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
250 switch (status
.response_code
) {
251 case AP_RESPONSE_NORMAL
:
252 if (status
.int_enabled
)
255 case AP_RESPONSE_RESET_IN_PROGRESS
:
256 case AP_RESPONSE_BUSY
:
258 case AP_RESPONSE_Q_NOT_AVAIL
:
259 case AP_RESPONSE_DECONFIGURED
:
260 case AP_RESPONSE_CHECKSTOPPED
:
261 case AP_RESPONSE_INVALID_ADDRESS
:
263 case AP_RESPONSE_OTHERWISE_CHANGED
:
264 if (status
.int_enabled
)
270 if (i
< AP_MAX_RESET
- 1) {
272 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
282 * __ap_send(): Send message to adjunct processor queue.
283 * @qid: The AP queue number
284 * @psmid: The program supplied message identifier
285 * @msg: The message text
286 * @length: The message length
287 * @special: Special Bit
289 * Returns AP queue status structure.
290 * Condition code 1 on NQAP can't happen because the L bit is 1.
291 * Condition code 2 on NQAP also means the send is incomplete,
292 * because a segment boundary was reached. The NQAP is repeated.
294 static inline struct ap_queue_status
295 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
,
296 unsigned int special
)
298 typedef struct { char _
[length
]; } msgblock
;
299 register unsigned long reg0
asm ("0") = qid
| 0x40000000UL
;
300 register struct ap_queue_status reg1
asm ("1");
301 register unsigned long reg2
asm ("2") = (unsigned long) msg
;
302 register unsigned long reg3
asm ("3") = (unsigned long) length
;
303 register unsigned long reg4
asm ("4") = (unsigned int) (psmid
>> 32);
304 register unsigned long reg5
asm ("5") = (unsigned int) psmid
;
310 "0: .long 0xb2ad0042\n" /* DQAP */
312 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
), "+d" (reg3
)
313 : "d" (reg4
), "d" (reg5
), "m" (*(msgblock
*) msg
)
318 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
320 struct ap_queue_status status
;
322 status
= __ap_send(qid
, psmid
, msg
, length
, 0);
323 switch (status
.response_code
) {
324 case AP_RESPONSE_NORMAL
:
326 case AP_RESPONSE_Q_FULL
:
327 case AP_RESPONSE_RESET_IN_PROGRESS
:
329 case AP_RESPONSE_REQ_FAC_NOT_INST
:
331 default: /* Device is gone. */
335 EXPORT_SYMBOL(ap_send
);
338 * __ap_recv(): Receive message from adjunct processor queue.
339 * @qid: The AP queue number
340 * @psmid: Pointer to program supplied message identifier
341 * @msg: The message text
342 * @length: The message length
344 * Returns AP queue status structure.
345 * Condition code 1 on DQAP means the receive has taken place
346 * but only partially. The response is incomplete, hence the
348 * Condition code 2 on DQAP also means the receive is incomplete,
349 * this time because a segment boundary was reached. Again, the
351 * Note that gpr2 is used by the DQAP instruction to keep track of
352 * any 'residual' length, in case the instruction gets interrupted.
353 * Hence it gets zeroed before the instruction.
355 static inline struct ap_queue_status
356 __ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
358 typedef struct { char _
[length
]; } msgblock
;
359 register unsigned long reg0
asm("0") = qid
| 0x80000000UL
;
360 register struct ap_queue_status reg1
asm ("1");
361 register unsigned long reg2
asm("2") = 0UL;
362 register unsigned long reg4
asm("4") = (unsigned long) msg
;
363 register unsigned long reg5
asm("5") = (unsigned long) length
;
364 register unsigned long reg6
asm("6") = 0UL;
365 register unsigned long reg7
asm("7") = 0UL;
369 "0: .long 0xb2ae0064\n"
371 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
),
372 "+d" (reg4
), "+d" (reg5
), "+d" (reg6
), "+d" (reg7
),
373 "=m" (*(msgblock
*) msg
) : : "cc" );
374 *psmid
= (((unsigned long long) reg6
) << 32) + reg7
;
378 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
380 struct ap_queue_status status
;
382 status
= __ap_recv(qid
, psmid
, msg
, length
);
383 switch (status
.response_code
) {
384 case AP_RESPONSE_NORMAL
:
386 case AP_RESPONSE_NO_PENDING_REPLY
:
387 if (status
.queue_empty
)
390 case AP_RESPONSE_RESET_IN_PROGRESS
:
396 EXPORT_SYMBOL(ap_recv
);
399 * ap_query_queue(): Check if an AP queue is available.
400 * @qid: The AP queue number
401 * @queue_depth: Pointer to queue depth value
402 * @device_type: Pointer to device type value
404 * The test is repeated for AP_MAX_RESET times.
406 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
408 struct ap_queue_status status
;
409 int t_depth
, t_device_type
, rc
, i
;
412 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
413 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
414 switch (status
.response_code
) {
415 case AP_RESPONSE_NORMAL
:
416 *queue_depth
= t_depth
+ 1;
417 *device_type
= t_device_type
;
420 case AP_RESPONSE_Q_NOT_AVAIL
:
423 case AP_RESPONSE_RESET_IN_PROGRESS
:
425 case AP_RESPONSE_DECONFIGURED
:
428 case AP_RESPONSE_CHECKSTOPPED
:
431 case AP_RESPONSE_INVALID_ADDRESS
:
434 case AP_RESPONSE_OTHERWISE_CHANGED
:
436 case AP_RESPONSE_BUSY
:
443 if (i
< AP_MAX_RESET
- 1)
450 * ap_init_queue(): Reset an AP queue.
451 * @qid: The AP queue number
453 * Reset an AP queue and wait for it to become available again.
455 static int ap_init_queue(ap_qid_t qid
)
457 struct ap_queue_status status
;
461 status
= ap_reset_queue(qid
);
462 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
463 switch (status
.response_code
) {
464 case AP_RESPONSE_NORMAL
:
465 if (status
.queue_empty
)
468 case AP_RESPONSE_Q_NOT_AVAIL
:
469 case AP_RESPONSE_DECONFIGURED
:
470 case AP_RESPONSE_CHECKSTOPPED
:
471 i
= AP_MAX_RESET
; /* return with -ENODEV */
473 case AP_RESPONSE_RESET_IN_PROGRESS
:
475 case AP_RESPONSE_BUSY
:
479 if (rc
!= -ENODEV
&& rc
!= -EBUSY
)
481 if (i
< AP_MAX_RESET
- 1) {
483 status
= ap_test_queue(qid
, &dummy
, &dummy
);
486 if (rc
== 0 && ap_using_interrupts()) {
487 rc
= ap_queue_enable_interruption(qid
, ap_interrupt_indicator
);
488 /* If interruption mode is supported by the machine,
489 * but an AP can not be enabled for interruption then
490 * the AP will be discarded. */
492 pr_err("Registering adapter interrupts for "
493 "AP %d failed\n", AP_QID_DEVICE(qid
));
499 * ap_increase_queue_count(): Arm request timeout.
500 * @ap_dev: Pointer to an AP device.
502 * Arm request timeout if an AP device was idle and a new request is submitted.
504 static void ap_increase_queue_count(struct ap_device
*ap_dev
)
506 int timeout
= ap_dev
->drv
->request_timeout
;
508 ap_dev
->queue_count
++;
509 if (ap_dev
->queue_count
== 1) {
510 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
511 ap_dev
->reset
= AP_RESET_ARMED
;
516 * ap_decrease_queue_count(): Decrease queue count.
517 * @ap_dev: Pointer to an AP device.
519 * If AP device is still alive, re-schedule request timeout if there are still
522 static void ap_decrease_queue_count(struct ap_device
*ap_dev
)
524 int timeout
= ap_dev
->drv
->request_timeout
;
526 ap_dev
->queue_count
--;
527 if (ap_dev
->queue_count
> 0)
528 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
531 * The timeout timer should to be disabled now - since
532 * del_timer_sync() is very expensive, we just tell via the
533 * reset flag to ignore the pending timeout timer.
535 ap_dev
->reset
= AP_RESET_IGNORE
;
539 * AP device related attributes.
541 static ssize_t
ap_hwtype_show(struct device
*dev
,
542 struct device_attribute
*attr
, char *buf
)
544 struct ap_device
*ap_dev
= to_ap_dev(dev
);
545 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->device_type
);
548 static DEVICE_ATTR(hwtype
, 0444, ap_hwtype_show
, NULL
);
549 static ssize_t
ap_depth_show(struct device
*dev
, struct device_attribute
*attr
,
552 struct ap_device
*ap_dev
= to_ap_dev(dev
);
553 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->queue_depth
);
556 static DEVICE_ATTR(depth
, 0444, ap_depth_show
, NULL
);
557 static ssize_t
ap_request_count_show(struct device
*dev
,
558 struct device_attribute
*attr
,
561 struct ap_device
*ap_dev
= to_ap_dev(dev
);
564 spin_lock_bh(&ap_dev
->lock
);
565 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->total_request_count
);
566 spin_unlock_bh(&ap_dev
->lock
);
570 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
572 static ssize_t
ap_modalias_show(struct device
*dev
,
573 struct device_attribute
*attr
, char *buf
)
575 return sprintf(buf
, "ap:t%02X", to_ap_dev(dev
)->device_type
);
578 static DEVICE_ATTR(modalias
, 0444, ap_modalias_show
, NULL
);
580 static struct attribute
*ap_dev_attrs
[] = {
581 &dev_attr_hwtype
.attr
,
582 &dev_attr_depth
.attr
,
583 &dev_attr_request_count
.attr
,
584 &dev_attr_modalias
.attr
,
587 static struct attribute_group ap_dev_attr_group
= {
588 .attrs
= ap_dev_attrs
593 * @dev: Pointer to device
594 * @drv: Pointer to device_driver
596 * AP bus driver registration/unregistration.
598 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
600 struct ap_device
*ap_dev
= to_ap_dev(dev
);
601 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
602 struct ap_device_id
*id
;
605 * Compare device type of the device with the list of
606 * supported types of the device_driver.
608 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
609 if ((id
->match_flags
& AP_DEVICE_ID_MATCH_DEVICE_TYPE
) &&
610 (id
->dev_type
!= ap_dev
->device_type
))
618 * ap_uevent(): Uevent function for AP devices.
619 * @dev: Pointer to device
620 * @env: Pointer to kobj_uevent_env
622 * It sets up a single environment variable DEV_TYPE which contains the
623 * hardware device type.
625 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
627 struct ap_device
*ap_dev
= to_ap_dev(dev
);
633 /* Set up DEV_TYPE environment variable. */
634 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
639 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
644 static int ap_bus_suspend(struct device
*dev
, pm_message_t state
)
646 struct ap_device
*ap_dev
= to_ap_dev(dev
);
649 if (!ap_suspend_flag
) {
652 /* Disable scanning for devices, thus we do not want to scan
653 * for them after removing.
655 del_timer_sync(&ap_config_timer
);
656 if (ap_work_queue
!= NULL
) {
657 destroy_workqueue(ap_work_queue
);
658 ap_work_queue
= NULL
;
661 tasklet_disable(&ap_tasklet
);
663 /* Poll on the device until all requests are finished. */
666 spin_lock_bh(&ap_dev
->lock
);
667 __ap_poll_device(ap_dev
, &flags
);
668 spin_unlock_bh(&ap_dev
->lock
);
669 } while ((flags
& 1) || (flags
& 2));
671 spin_lock_bh(&ap_dev
->lock
);
672 ap_dev
->unregistered
= 1;
673 spin_unlock_bh(&ap_dev
->lock
);
678 static int ap_bus_resume(struct device
*dev
)
681 struct ap_device
*ap_dev
= to_ap_dev(dev
);
683 if (ap_suspend_flag
) {
685 if (!ap_interrupts_available())
686 ap_interrupt_indicator
= NULL
;
687 if (!user_set_domain
) {
688 ap_domain_index
= -1;
691 init_timer(&ap_config_timer
);
692 ap_config_timer
.function
= ap_config_timeout
;
693 ap_config_timer
.data
= 0;
694 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
695 add_timer(&ap_config_timer
);
696 ap_work_queue
= create_singlethread_workqueue("kapwork");
699 tasklet_enable(&ap_tasklet
);
700 if (!ap_using_interrupts())
701 ap_schedule_poll_timer();
703 tasklet_schedule(&ap_tasklet
);
705 rc
= ap_poll_thread_start();
707 if (AP_QID_QUEUE(ap_dev
->qid
) != ap_domain_index
) {
708 spin_lock_bh(&ap_dev
->lock
);
709 ap_dev
->qid
= AP_MKQID(AP_QID_DEVICE(ap_dev
->qid
),
711 spin_unlock_bh(&ap_dev
->lock
);
713 queue_work(ap_work_queue
, &ap_config_work
);
718 static struct bus_type ap_bus_type
= {
720 .match
= &ap_bus_match
,
721 .uevent
= &ap_uevent
,
722 .suspend
= ap_bus_suspend
,
723 .resume
= ap_bus_resume
726 static int ap_device_probe(struct device
*dev
)
728 struct ap_device
*ap_dev
= to_ap_dev(dev
);
729 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
732 ap_dev
->drv
= ap_drv
;
733 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
735 spin_lock_bh(&ap_device_list_lock
);
736 list_add(&ap_dev
->list
, &ap_device_list
);
737 spin_unlock_bh(&ap_device_list_lock
);
743 * __ap_flush_queue(): Flush requests.
744 * @ap_dev: Pointer to the AP device
746 * Flush all requests from the request/pending queue of an AP device.
748 static void __ap_flush_queue(struct ap_device
*ap_dev
)
750 struct ap_message
*ap_msg
, *next
;
752 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->pendingq
, list
) {
753 list_del_init(&ap_msg
->list
);
754 ap_dev
->pendingq_count
--;
755 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
757 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->requestq
, list
) {
758 list_del_init(&ap_msg
->list
);
759 ap_dev
->requestq_count
--;
760 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
764 void ap_flush_queue(struct ap_device
*ap_dev
)
766 spin_lock_bh(&ap_dev
->lock
);
767 __ap_flush_queue(ap_dev
);
768 spin_unlock_bh(&ap_dev
->lock
);
770 EXPORT_SYMBOL(ap_flush_queue
);
772 static int ap_device_remove(struct device
*dev
)
774 struct ap_device
*ap_dev
= to_ap_dev(dev
);
775 struct ap_driver
*ap_drv
= ap_dev
->drv
;
777 ap_flush_queue(ap_dev
);
778 del_timer_sync(&ap_dev
->timeout
);
779 spin_lock_bh(&ap_device_list_lock
);
780 list_del_init(&ap_dev
->list
);
781 spin_unlock_bh(&ap_device_list_lock
);
783 ap_drv
->remove(ap_dev
);
784 spin_lock_bh(&ap_dev
->lock
);
785 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
786 spin_unlock_bh(&ap_dev
->lock
);
790 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
793 struct device_driver
*drv
= &ap_drv
->driver
;
795 drv
->bus
= &ap_bus_type
;
796 drv
->probe
= ap_device_probe
;
797 drv
->remove
= ap_device_remove
;
800 return driver_register(drv
);
802 EXPORT_SYMBOL(ap_driver_register
);
804 void ap_driver_unregister(struct ap_driver
*ap_drv
)
806 driver_unregister(&ap_drv
->driver
);
808 EXPORT_SYMBOL(ap_driver_unregister
);
813 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
815 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
818 static BUS_ATTR(ap_domain
, 0444, ap_domain_show
, NULL
);
820 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
822 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
825 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
827 return snprintf(buf
, PAGE_SIZE
, "%d\n",
828 ap_using_interrupts() ? 1 : 0);
831 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
833 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
834 const char *buf
, size_t count
)
838 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
840 ap_config_time
= time
;
841 if (!timer_pending(&ap_config_timer
) ||
842 !mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
)) {
843 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
844 add_timer(&ap_config_timer
);
849 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
851 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
853 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
856 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
857 const char *buf
, size_t count
)
861 if (sscanf(buf
, "%d\n", &flag
) != 1)
864 rc
= ap_poll_thread_start();
869 ap_poll_thread_stop();
873 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
875 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
877 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
880 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
883 unsigned long long time
;
886 /* 120 seconds = maximum poll interval */
887 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
888 time
> 120000000000ULL)
891 hr_time
= ktime_set(0, poll_timeout
);
893 if (!hrtimer_is_queued(&ap_poll_timer
) ||
894 !hrtimer_forward(&ap_poll_timer
, hrtimer_get_expires(&ap_poll_timer
), hr_time
)) {
895 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
896 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
901 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
903 static struct bus_attribute
*const ap_bus_attrs
[] = {
905 &bus_attr_config_time
,
906 &bus_attr_poll_thread
,
907 &bus_attr_ap_interrupts
,
908 &bus_attr_poll_timeout
,
913 * ap_select_domain(): Select an AP domain.
915 * Pick one of the 16 AP domains.
917 static int ap_select_domain(void)
919 int queue_depth
, device_type
, count
, max_count
, best_domain
;
923 * We want to use a single domain. Either the one specified with
924 * the "domain=" parameter or the domain with the maximum number
927 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
)
928 /* Domain has already been selected. */
932 for (i
= 0; i
< AP_DOMAINS
; i
++) {
934 for (j
= 0; j
< AP_DEVICES
; j
++) {
935 ap_qid_t qid
= AP_MKQID(j
, i
);
936 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
941 if (count
> max_count
) {
946 if (best_domain
>= 0){
947 ap_domain_index
= best_domain
;
954 * ap_probe_device_type(): Find the device type of an AP.
955 * @ap_dev: pointer to the AP device.
957 * Find the device type if query queue returned a device type of 0.
959 static int ap_probe_device_type(struct ap_device
*ap_dev
)
961 static unsigned char msg
[] = {
962 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
963 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
964 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
965 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
966 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
967 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
968 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
969 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
970 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
971 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
972 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
973 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
974 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
975 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
976 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
977 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
978 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
979 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
980 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
981 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
982 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
983 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
984 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
985 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
986 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
987 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
988 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
989 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
990 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
991 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
992 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
993 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
994 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
995 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
996 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
997 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
998 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
999 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1000 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1001 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1002 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1003 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1004 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1005 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1006 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1008 struct ap_queue_status status
;
1009 unsigned long long psmid
;
1013 reply
= (void *) get_zeroed_page(GFP_KERNEL
);
1019 status
= __ap_send(ap_dev
->qid
, 0x0102030405060708ULL
,
1020 msg
, sizeof(msg
), 0);
1021 if (status
.response_code
!= AP_RESPONSE_NORMAL
) {
1026 /* Wait for the test message to complete. */
1027 for (i
= 0; i
< 6; i
++) {
1029 status
= __ap_recv(ap_dev
->qid
, &psmid
, reply
, 4096);
1030 if (status
.response_code
== AP_RESPONSE_NORMAL
&&
1031 psmid
== 0x0102030405060708ULL
)
1035 /* Got an answer. */
1036 if (reply
[0] == 0x00 && reply
[1] == 0x86)
1037 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICC
;
1039 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICA
;
1045 free_page((unsigned long) reply
);
1050 static void ap_interrupt_handler(void *unused1
, void *unused2
)
1052 tasklet_schedule(&ap_tasklet
);
1056 * __ap_scan_bus(): Scan the AP bus.
1057 * @dev: Pointer to device
1058 * @data: Pointer to data
1060 * Scan the AP bus for new devices.
1062 static int __ap_scan_bus(struct device
*dev
, void *data
)
1064 return to_ap_dev(dev
)->qid
== (ap_qid_t
)(unsigned long) data
;
1067 static void ap_device_release(struct device
*dev
)
1069 struct ap_device
*ap_dev
= to_ap_dev(dev
);
1074 static void ap_scan_bus(struct work_struct
*unused
)
1076 struct ap_device
*ap_dev
;
1079 int queue_depth
, device_type
;
1082 if (ap_select_domain() != 0)
1084 for (i
= 0; i
< AP_DEVICES
; i
++) {
1085 qid
= AP_MKQID(i
, ap_domain_index
);
1086 dev
= bus_find_device(&ap_bus_type
, NULL
,
1087 (void *)(unsigned long)qid
,
1089 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1092 set_current_state(TASK_UNINTERRUPTIBLE
);
1093 schedule_timeout(AP_RESET_TIMEOUT
);
1094 rc
= ap_query_queue(qid
, &queue_depth
,
1097 ap_dev
= to_ap_dev(dev
);
1098 spin_lock_bh(&ap_dev
->lock
);
1099 if (rc
|| ap_dev
->unregistered
) {
1100 spin_unlock_bh(&ap_dev
->lock
);
1101 if (ap_dev
->unregistered
)
1103 device_unregister(dev
);
1107 spin_unlock_bh(&ap_dev
->lock
);
1113 rc
= ap_init_queue(qid
);
1116 ap_dev
= kzalloc(sizeof(*ap_dev
), GFP_KERNEL
);
1120 ap_dev
->queue_depth
= queue_depth
;
1121 ap_dev
->unregistered
= 1;
1122 spin_lock_init(&ap_dev
->lock
);
1123 INIT_LIST_HEAD(&ap_dev
->pendingq
);
1124 INIT_LIST_HEAD(&ap_dev
->requestq
);
1125 INIT_LIST_HEAD(&ap_dev
->list
);
1126 setup_timer(&ap_dev
->timeout
, ap_request_timeout
,
1127 (unsigned long) ap_dev
);
1128 if (device_type
== 0)
1129 ap_probe_device_type(ap_dev
);
1131 ap_dev
->device_type
= device_type
;
1133 ap_dev
->device
.bus
= &ap_bus_type
;
1134 ap_dev
->device
.parent
= ap_root_device
;
1135 if (dev_set_name(&ap_dev
->device
, "card%02x",
1136 AP_QID_DEVICE(ap_dev
->qid
))) {
1140 ap_dev
->device
.release
= ap_device_release
;
1141 rc
= device_register(&ap_dev
->device
);
1143 put_device(&ap_dev
->device
);
1146 /* Add device attributes. */
1147 rc
= sysfs_create_group(&ap_dev
->device
.kobj
,
1148 &ap_dev_attr_group
);
1150 spin_lock_bh(&ap_dev
->lock
);
1151 ap_dev
->unregistered
= 0;
1152 spin_unlock_bh(&ap_dev
->lock
);
1155 device_unregister(&ap_dev
->device
);
1160 ap_config_timeout(unsigned long ptr
)
1162 queue_work(ap_work_queue
, &ap_config_work
);
1163 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1164 add_timer(&ap_config_timer
);
1168 * ap_schedule_poll_timer(): Schedule poll timer.
1170 * Set up the timer to run the poll tasklet
1172 static inline void ap_schedule_poll_timer(void)
1176 spin_lock_bh(&ap_poll_timer_lock
);
1177 if (ap_using_interrupts() || ap_suspend_flag
)
1179 if (hrtimer_is_queued(&ap_poll_timer
))
1181 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer
)) <= 0) {
1182 hr_time
= ktime_set(0, poll_timeout
);
1183 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
1184 hrtimer_restart(&ap_poll_timer
);
1187 spin_unlock_bh(&ap_poll_timer_lock
);
1191 * ap_poll_read(): Receive pending reply messages from an AP device.
1192 * @ap_dev: pointer to the AP device
1193 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1194 * required, bit 2^1 is set if the poll timer needs to get armed
1196 * Returns 0 if the device is still present, -ENODEV if not.
1198 static int ap_poll_read(struct ap_device
*ap_dev
, unsigned long *flags
)
1200 struct ap_queue_status status
;
1201 struct ap_message
*ap_msg
;
1203 if (ap_dev
->queue_count
<= 0)
1205 status
= __ap_recv(ap_dev
->qid
, &ap_dev
->reply
->psmid
,
1206 ap_dev
->reply
->message
, ap_dev
->reply
->length
);
1207 switch (status
.response_code
) {
1208 case AP_RESPONSE_NORMAL
:
1209 atomic_dec(&ap_poll_requests
);
1210 ap_decrease_queue_count(ap_dev
);
1211 list_for_each_entry(ap_msg
, &ap_dev
->pendingq
, list
) {
1212 if (ap_msg
->psmid
!= ap_dev
->reply
->psmid
)
1214 list_del_init(&ap_msg
->list
);
1215 ap_dev
->pendingq_count
--;
1216 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ap_dev
->reply
);
1219 if (ap_dev
->queue_count
> 0)
1222 case AP_RESPONSE_NO_PENDING_REPLY
:
1223 if (status
.queue_empty
) {
1224 /* The card shouldn't forget requests but who knows. */
1225 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1226 ap_dev
->queue_count
= 0;
1227 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1228 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1229 ap_dev
->pendingq_count
= 0;
1240 * ap_poll_write(): Send messages from the request queue to an AP device.
1241 * @ap_dev: pointer to the AP device
1242 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1243 * required, bit 2^1 is set if the poll timer needs to get armed
1245 * Returns 0 if the device is still present, -ENODEV if not.
1247 static int ap_poll_write(struct ap_device
*ap_dev
, unsigned long *flags
)
1249 struct ap_queue_status status
;
1250 struct ap_message
*ap_msg
;
1252 if (ap_dev
->requestq_count
<= 0 ||
1253 ap_dev
->queue_count
>= ap_dev
->queue_depth
)
1255 /* Start the next request on the queue. */
1256 ap_msg
= list_entry(ap_dev
->requestq
.next
, struct ap_message
, list
);
1257 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1258 ap_msg
->message
, ap_msg
->length
, ap_msg
->special
);
1259 switch (status
.response_code
) {
1260 case AP_RESPONSE_NORMAL
:
1261 atomic_inc(&ap_poll_requests
);
1262 ap_increase_queue_count(ap_dev
);
1263 list_move_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1264 ap_dev
->requestq_count
--;
1265 ap_dev
->pendingq_count
++;
1266 if (ap_dev
->queue_count
< ap_dev
->queue_depth
&&
1267 ap_dev
->requestq_count
> 0)
1271 case AP_RESPONSE_Q_FULL
:
1272 case AP_RESPONSE_RESET_IN_PROGRESS
:
1275 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1276 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1285 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1286 * @ap_dev: pointer to the bus device
1287 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1288 * required, bit 2^1 is set if the poll timer needs to get armed
1290 * Poll AP device for pending replies and send new messages. If either
1291 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1294 static inline int ap_poll_queue(struct ap_device
*ap_dev
, unsigned long *flags
)
1298 rc
= ap_poll_read(ap_dev
, flags
);
1301 return ap_poll_write(ap_dev
, flags
);
1305 * __ap_queue_message(): Queue a message to a device.
1306 * @ap_dev: pointer to the AP device
1307 * @ap_msg: the message to be queued
1309 * Queue a message to a device. Returns 0 if successful.
1311 static int __ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1313 struct ap_queue_status status
;
1315 if (list_empty(&ap_dev
->requestq
) &&
1316 ap_dev
->queue_count
< ap_dev
->queue_depth
) {
1317 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1318 ap_msg
->message
, ap_msg
->length
,
1320 switch (status
.response_code
) {
1321 case AP_RESPONSE_NORMAL
:
1322 list_add_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1323 atomic_inc(&ap_poll_requests
);
1324 ap_dev
->pendingq_count
++;
1325 ap_increase_queue_count(ap_dev
);
1326 ap_dev
->total_request_count
++;
1328 case AP_RESPONSE_Q_FULL
:
1329 case AP_RESPONSE_RESET_IN_PROGRESS
:
1330 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1331 ap_dev
->requestq_count
++;
1332 ap_dev
->total_request_count
++;
1334 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1335 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1336 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-EINVAL
));
1338 default: /* Device is gone. */
1339 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1343 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1344 ap_dev
->requestq_count
++;
1345 ap_dev
->total_request_count
++;
1348 ap_schedule_poll_timer();
1352 void ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1354 unsigned long flags
;
1357 spin_lock_bh(&ap_dev
->lock
);
1358 if (!ap_dev
->unregistered
) {
1359 /* Make room on the queue by polling for finished requests. */
1360 rc
= ap_poll_queue(ap_dev
, &flags
);
1362 rc
= __ap_queue_message(ap_dev
, ap_msg
);
1364 wake_up(&ap_poll_wait
);
1366 ap_dev
->unregistered
= 1;
1368 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1371 spin_unlock_bh(&ap_dev
->lock
);
1373 device_unregister(&ap_dev
->device
);
1375 EXPORT_SYMBOL(ap_queue_message
);
1378 * ap_cancel_message(): Cancel a crypto request.
1379 * @ap_dev: The AP device that has the message queued
1380 * @ap_msg: The message that is to be removed
1382 * Cancel a crypto request. This is done by removing the request
1383 * from the device pending or request queue. Note that the
1384 * request stays on the AP queue. When it finishes the message
1385 * reply will be discarded because the psmid can't be found.
1387 void ap_cancel_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1389 struct ap_message
*tmp
;
1391 spin_lock_bh(&ap_dev
->lock
);
1392 if (!list_empty(&ap_msg
->list
)) {
1393 list_for_each_entry(tmp
, &ap_dev
->pendingq
, list
)
1394 if (tmp
->psmid
== ap_msg
->psmid
) {
1395 ap_dev
->pendingq_count
--;
1398 ap_dev
->requestq_count
--;
1400 list_del_init(&ap_msg
->list
);
1402 spin_unlock_bh(&ap_dev
->lock
);
1404 EXPORT_SYMBOL(ap_cancel_message
);
1407 * ap_poll_timeout(): AP receive polling for finished AP requests.
1408 * @unused: Unused pointer.
1410 * Schedules the AP tasklet using a high resolution timer.
1412 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
1414 tasklet_schedule(&ap_tasklet
);
1415 return HRTIMER_NORESTART
;
1419 * ap_reset(): Reset a not responding AP device.
1420 * @ap_dev: Pointer to the AP device
1422 * Reset a not responding AP device and move all requests from the
1423 * pending queue to the request queue.
1425 static void ap_reset(struct ap_device
*ap_dev
)
1429 ap_dev
->reset
= AP_RESET_IGNORE
;
1430 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1431 ap_dev
->queue_count
= 0;
1432 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1433 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1434 ap_dev
->pendingq_count
= 0;
1435 rc
= ap_init_queue(ap_dev
->qid
);
1437 ap_dev
->unregistered
= 1;
1440 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
)
1442 if (!ap_dev
->unregistered
) {
1443 if (ap_poll_queue(ap_dev
, flags
))
1444 ap_dev
->unregistered
= 1;
1445 if (ap_dev
->reset
== AP_RESET_DO
)
1452 * ap_poll_all(): Poll all AP devices.
1453 * @dummy: Unused variable
1455 * Poll all AP devices on the bus in a round robin fashion. Continue
1456 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1457 * of the control flags has been set arm the poll timer.
1459 static void ap_poll_all(unsigned long dummy
)
1461 unsigned long flags
;
1462 struct ap_device
*ap_dev
;
1464 /* Reset the indicator if interrupts are used. Thus new interrupts can
1465 * be received. Doing it in the beginning of the tasklet is therefor
1466 * important that no requests on any AP get lost.
1468 if (ap_using_interrupts())
1469 xchg((u8
*)ap_interrupt_indicator
, 0);
1472 spin_lock(&ap_device_list_lock
);
1473 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1474 spin_lock(&ap_dev
->lock
);
1475 __ap_poll_device(ap_dev
, &flags
);
1476 spin_unlock(&ap_dev
->lock
);
1478 spin_unlock(&ap_device_list_lock
);
1479 } while (flags
& 1);
1481 ap_schedule_poll_timer();
1485 * ap_poll_thread(): Thread that polls for finished requests.
1486 * @data: Unused pointer
1488 * AP bus poll thread. The purpose of this thread is to poll for
1489 * finished requests in a loop if there is a "free" cpu - that is
1490 * a cpu that doesn't have anything better to do. The polling stops
1491 * as soon as there is another task or if all messages have been
1494 static int ap_poll_thread(void *data
)
1496 DECLARE_WAITQUEUE(wait
, current
);
1497 unsigned long flags
;
1499 struct ap_device
*ap_dev
;
1501 set_user_nice(current
, 19);
1503 if (ap_suspend_flag
)
1505 if (need_resched()) {
1509 add_wait_queue(&ap_poll_wait
, &wait
);
1510 set_current_state(TASK_INTERRUPTIBLE
);
1511 if (kthread_should_stop())
1513 requests
= atomic_read(&ap_poll_requests
);
1516 set_current_state(TASK_RUNNING
);
1517 remove_wait_queue(&ap_poll_wait
, &wait
);
1520 spin_lock_bh(&ap_device_list_lock
);
1521 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1522 spin_lock(&ap_dev
->lock
);
1523 __ap_poll_device(ap_dev
, &flags
);
1524 spin_unlock(&ap_dev
->lock
);
1526 spin_unlock_bh(&ap_device_list_lock
);
1528 set_current_state(TASK_RUNNING
);
1529 remove_wait_queue(&ap_poll_wait
, &wait
);
1533 static int ap_poll_thread_start(void)
1537 if (ap_using_interrupts() || ap_suspend_flag
)
1539 mutex_lock(&ap_poll_thread_mutex
);
1540 if (!ap_poll_kthread
) {
1541 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
1542 rc
= IS_ERR(ap_poll_kthread
) ? PTR_ERR(ap_poll_kthread
) : 0;
1544 ap_poll_kthread
= NULL
;
1548 mutex_unlock(&ap_poll_thread_mutex
);
1552 static void ap_poll_thread_stop(void)
1554 mutex_lock(&ap_poll_thread_mutex
);
1555 if (ap_poll_kthread
) {
1556 kthread_stop(ap_poll_kthread
);
1557 ap_poll_kthread
= NULL
;
1559 mutex_unlock(&ap_poll_thread_mutex
);
1563 * ap_request_timeout(): Handling of request timeouts
1564 * @data: Holds the AP device.
1566 * Handles request timeouts.
1568 static void ap_request_timeout(unsigned long data
)
1570 struct ap_device
*ap_dev
= (struct ap_device
*) data
;
1572 if (ap_dev
->reset
== AP_RESET_ARMED
) {
1573 ap_dev
->reset
= AP_RESET_DO
;
1575 if (ap_using_interrupts())
1576 tasklet_schedule(&ap_tasklet
);
1580 static void ap_reset_domain(void)
1584 if (ap_domain_index
!= -1)
1585 for (i
= 0; i
< AP_DEVICES
; i
++)
1586 ap_reset_queue(AP_MKQID(i
, ap_domain_index
));
1589 static void ap_reset_all(void)
1593 for (i
= 0; i
< AP_DOMAINS
; i
++)
1594 for (j
= 0; j
< AP_DEVICES
; j
++)
1595 ap_reset_queue(AP_MKQID(j
, i
));
1598 static struct reset_call ap_reset_call
= {
1603 * ap_module_init(): The module initialization code.
1605 * Initializes the module.
1607 int __init
ap_module_init(void)
1611 if (ap_domain_index
< -1 || ap_domain_index
>= AP_DOMAINS
) {
1612 pr_warning("%d is not a valid cryptographic domain\n",
1616 /* In resume callback we need to know if the user had set the domain.
1617 * If so, we can not just reset it.
1619 if (ap_domain_index
>= 0)
1620 user_set_domain
= 1;
1622 if (ap_instructions_available() != 0) {
1623 pr_warning("The hardware system does not support "
1624 "AP instructions\n");
1627 if (ap_interrupts_available()) {
1628 isc_register(AP_ISC
);
1629 ap_interrupt_indicator
= s390_register_adapter_interrupt(
1630 &ap_interrupt_handler
, NULL
, AP_ISC
);
1631 if (IS_ERR(ap_interrupt_indicator
)) {
1632 ap_interrupt_indicator
= NULL
;
1633 isc_unregister(AP_ISC
);
1637 register_reset_call(&ap_reset_call
);
1639 /* Create /sys/bus/ap. */
1640 rc
= bus_register(&ap_bus_type
);
1643 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1644 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1649 /* Create /sys/devices/ap. */
1650 ap_root_device
= root_device_register("ap");
1651 rc
= IS_ERR(ap_root_device
) ? PTR_ERR(ap_root_device
) : 0;
1655 ap_work_queue
= create_singlethread_workqueue("kapwork");
1656 if (!ap_work_queue
) {
1661 if (ap_select_domain() == 0)
1664 /* Setup the AP bus rescan timer. */
1665 init_timer(&ap_config_timer
);
1666 ap_config_timer
.function
= ap_config_timeout
;
1667 ap_config_timer
.data
= 0;
1668 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1669 add_timer(&ap_config_timer
);
1671 /* Setup the high resultion poll timer.
1672 * If we are running under z/VM adjust polling to z/VM polling rate.
1675 poll_timeout
= 1500000;
1676 spin_lock_init(&ap_poll_timer_lock
);
1677 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1678 ap_poll_timer
.function
= ap_poll_timeout
;
1680 /* Start the low priority AP bus poll thread. */
1681 if (ap_thread_flag
) {
1682 rc
= ap_poll_thread_start();
1690 del_timer_sync(&ap_config_timer
);
1691 hrtimer_cancel(&ap_poll_timer
);
1692 destroy_workqueue(ap_work_queue
);
1694 root_device_unregister(ap_root_device
);
1697 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1698 bus_unregister(&ap_bus_type
);
1700 unregister_reset_call(&ap_reset_call
);
1701 if (ap_using_interrupts()) {
1702 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1703 isc_unregister(AP_ISC
);
1708 static int __ap_match_all(struct device
*dev
, void *data
)
1714 * ap_modules_exit(): The module termination code
1716 * Terminates the module.
1718 void ap_module_exit(void)
1724 ap_poll_thread_stop();
1725 del_timer_sync(&ap_config_timer
);
1726 hrtimer_cancel(&ap_poll_timer
);
1727 destroy_workqueue(ap_work_queue
);
1728 tasklet_kill(&ap_tasklet
);
1729 root_device_unregister(ap_root_device
);
1730 while ((dev
= bus_find_device(&ap_bus_type
, NULL
, NULL
,
1733 device_unregister(dev
);
1736 for (i
= 0; ap_bus_attrs
[i
]; i
++)
1737 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1738 bus_unregister(&ap_bus_type
);
1739 unregister_reset_call(&ap_reset_call
);
1740 if (ap_using_interrupts()) {
1741 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1742 isc_unregister(AP_ISC
);
1746 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1747 module_init(ap_module_init
);
1748 module_exit(ap_module_exit
);