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>
9 * Adjunct processor bus.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/err.h>
30 #include <linux/interrupt.h>
31 #include <linux/workqueue.h>
32 #include <linux/notifier.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <asm/s390_rdev.h>
36 #include <asm/reset.h>
40 /* Some prototypes. */
41 static void ap_scan_bus(struct work_struct
*);
42 static void ap_poll_all(unsigned long);
43 static void ap_poll_timeout(unsigned long);
44 static int ap_poll_thread_start(void);
45 static void ap_poll_thread_stop(void);
46 static void ap_request_timeout(unsigned long);
51 MODULE_AUTHOR("IBM Corporation");
52 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
53 "Copyright 2006 IBM Corporation");
54 MODULE_LICENSE("GPL");
59 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
60 module_param_named(domain
, ap_domain_index
, int, 0000);
61 MODULE_PARM_DESC(domain
, "domain index for ap devices");
62 EXPORT_SYMBOL(ap_domain_index
);
64 static int ap_thread_flag
= 0;
65 module_param_named(poll_thread
, ap_thread_flag
, int, 0000);
66 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
68 static struct device
*ap_root_device
= NULL
;
69 static DEFINE_SPINLOCK(ap_device_lock
);
70 static LIST_HEAD(ap_device_list
);
73 * Workqueue & timer for bus rescan.
75 static struct workqueue_struct
*ap_work_queue
;
76 static struct timer_list ap_config_timer
;
77 static int ap_config_time
= AP_CONFIG_TIME
;
78 static DECLARE_WORK(ap_config_work
, ap_scan_bus
);
81 * Tasklet & timer for AP request polling.
83 static struct timer_list ap_poll_timer
= TIMER_INITIALIZER(ap_poll_timeout
,0,0);
84 static DECLARE_TASKLET(ap_tasklet
, ap_poll_all
, 0);
85 static atomic_t ap_poll_requests
= ATOMIC_INIT(0);
86 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
87 static struct task_struct
*ap_poll_kthread
= NULL
;
88 static DEFINE_MUTEX(ap_poll_thread_mutex
);
91 * ap_intructions_available() - Test if AP instructions are available.
93 * Returns 0 if the AP instructions are installed.
95 static inline int ap_instructions_available(void)
97 register unsigned long reg0
asm ("0") = AP_MKQID(0,0);
98 register unsigned long reg1
asm ("1") = -ENODEV
;
99 register unsigned long reg2
asm ("2") = 0UL;
102 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
106 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
) : : "cc" );
111 * ap_test_queue(): Test adjunct processor queue.
112 * @qid: The AP queue number
113 * @queue_depth: Pointer to queue depth value
114 * @device_type: Pointer to device type value
116 * Returns AP queue status structure.
118 static inline struct ap_queue_status
119 ap_test_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
121 register unsigned long reg0
asm ("0") = qid
;
122 register struct ap_queue_status reg1
asm ("1");
123 register unsigned long reg2
asm ("2") = 0UL;
125 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
126 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
127 *device_type
= (int) (reg2
>> 24);
128 *queue_depth
= (int) (reg2
& 0xff);
133 * ap_reset_queue(): Reset adjunct processor queue.
134 * @qid: The AP queue number
136 * Returns AP queue status structure.
138 static inline struct ap_queue_status
ap_reset_queue(ap_qid_t qid
)
140 register unsigned long reg0
asm ("0") = qid
| 0x01000000UL
;
141 register struct ap_queue_status reg1
asm ("1");
142 register unsigned long reg2
asm ("2") = 0UL;
145 ".long 0xb2af0000" /* PQAP(RAPQ) */
146 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
151 * __ap_send(): Send message to adjunct processor queue.
152 * @qid: The AP queue number
153 * @psmid: The program supplied message identifier
154 * @msg: The message text
155 * @length: The message length
157 * Returns AP queue status structure.
158 * Condition code 1 on NQAP can't happen because the L bit is 1.
159 * Condition code 2 on NQAP also means the send is incomplete,
160 * because a segment boundary was reached. The NQAP is repeated.
162 static inline struct ap_queue_status
163 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
165 typedef struct { char _
[length
]; } msgblock
;
166 register unsigned long reg0
asm ("0") = qid
| 0x40000000UL
;
167 register struct ap_queue_status reg1
asm ("1");
168 register unsigned long reg2
asm ("2") = (unsigned long) msg
;
169 register unsigned long reg3
asm ("3") = (unsigned long) length
;
170 register unsigned long reg4
asm ("4") = (unsigned int) (psmid
>> 32);
171 register unsigned long reg5
asm ("5") = (unsigned int) psmid
;
174 "0: .long 0xb2ad0042\n" /* DQAP */
176 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
), "+d" (reg3
)
177 : "d" (reg4
), "d" (reg5
), "m" (*(msgblock
*) msg
)
182 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
184 struct ap_queue_status status
;
186 status
= __ap_send(qid
, psmid
, msg
, length
);
187 switch (status
.response_code
) {
188 case AP_RESPONSE_NORMAL
:
190 case AP_RESPONSE_Q_FULL
:
191 case AP_RESPONSE_RESET_IN_PROGRESS
:
193 default: /* Device is gone. */
197 EXPORT_SYMBOL(ap_send
);
200 * __ap_recv(): Receive message from adjunct processor queue.
201 * @qid: The AP queue number
202 * @psmid: Pointer to program supplied message identifier
203 * @msg: The message text
204 * @length: The message length
206 * Returns AP queue status structure.
207 * Condition code 1 on DQAP means the receive has taken place
208 * but only partially. The response is incomplete, hence the
210 * Condition code 2 on DQAP also means the receive is incomplete,
211 * this time because a segment boundary was reached. Again, the
213 * Note that gpr2 is used by the DQAP instruction to keep track of
214 * any 'residual' length, in case the instruction gets interrupted.
215 * Hence it gets zeroed before the instruction.
217 static inline struct ap_queue_status
218 __ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
220 typedef struct { char _
[length
]; } msgblock
;
221 register unsigned long reg0
asm("0") = qid
| 0x80000000UL
;
222 register struct ap_queue_status reg1
asm ("1");
223 register unsigned long reg2
asm("2") = 0UL;
224 register unsigned long reg4
asm("4") = (unsigned long) msg
;
225 register unsigned long reg5
asm("5") = (unsigned long) length
;
226 register unsigned long reg6
asm("6") = 0UL;
227 register unsigned long reg7
asm("7") = 0UL;
231 "0: .long 0xb2ae0064\n"
233 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
),
234 "+d" (reg4
), "+d" (reg5
), "+d" (reg6
), "+d" (reg7
),
235 "=m" (*(msgblock
*) msg
) : : "cc" );
236 *psmid
= (((unsigned long long) reg6
) << 32) + reg7
;
240 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
242 struct ap_queue_status status
;
244 status
= __ap_recv(qid
, psmid
, msg
, length
);
245 switch (status
.response_code
) {
246 case AP_RESPONSE_NORMAL
:
248 case AP_RESPONSE_NO_PENDING_REPLY
:
249 if (status
.queue_empty
)
252 case AP_RESPONSE_RESET_IN_PROGRESS
:
258 EXPORT_SYMBOL(ap_recv
);
261 * ap_query_queue(): Check if an AP queue is available.
262 * @qid: The AP queue number
263 * @queue_depth: Pointer to queue depth value
264 * @device_type: Pointer to device type value
266 * The test is repeated for AP_MAX_RESET times.
268 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
270 struct ap_queue_status status
;
271 int t_depth
, t_device_type
, rc
, i
;
274 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
275 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
276 switch (status
.response_code
) {
277 case AP_RESPONSE_NORMAL
:
278 *queue_depth
= t_depth
+ 1;
279 *device_type
= t_device_type
;
282 case AP_RESPONSE_Q_NOT_AVAIL
:
285 case AP_RESPONSE_RESET_IN_PROGRESS
:
287 case AP_RESPONSE_DECONFIGURED
:
290 case AP_RESPONSE_CHECKSTOPPED
:
293 case AP_RESPONSE_BUSY
:
300 if (i
< AP_MAX_RESET
- 1)
307 * ap_init_queue(): Reset an AP queue.
308 * @qid: The AP queue number
310 * Reset an AP queue and wait for it to become available again.
312 static int ap_init_queue(ap_qid_t qid
)
314 struct ap_queue_status status
;
318 status
= ap_reset_queue(qid
);
319 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
320 switch (status
.response_code
) {
321 case AP_RESPONSE_NORMAL
:
322 if (status
.queue_empty
)
325 case AP_RESPONSE_Q_NOT_AVAIL
:
326 case AP_RESPONSE_DECONFIGURED
:
327 case AP_RESPONSE_CHECKSTOPPED
:
328 i
= AP_MAX_RESET
; /* return with -ENODEV */
330 case AP_RESPONSE_RESET_IN_PROGRESS
:
332 case AP_RESPONSE_BUSY
:
336 if (rc
!= -ENODEV
&& rc
!= -EBUSY
)
338 if (i
< AP_MAX_RESET
- 1) {
340 status
= ap_test_queue(qid
, &dummy
, &dummy
);
347 * ap_increase_queue_count(): Arm request timeout.
348 * @ap_dev: Pointer to an AP device.
350 * Arm request timeout if an AP device was idle and a new request is submitted.
352 static void ap_increase_queue_count(struct ap_device
*ap_dev
)
354 int timeout
= ap_dev
->drv
->request_timeout
;
356 ap_dev
->queue_count
++;
357 if (ap_dev
->queue_count
== 1) {
358 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
359 ap_dev
->reset
= AP_RESET_ARMED
;
364 * ap_decrease_queue_count(): Decrease queue count.
365 * @ap_dev: Pointer to an AP device.
367 * If AP device is still alive, re-schedule request timeout if there are still
370 static void ap_decrease_queue_count(struct ap_device
*ap_dev
)
372 int timeout
= ap_dev
->drv
->request_timeout
;
374 ap_dev
->queue_count
--;
375 if (ap_dev
->queue_count
> 0)
376 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
379 * The timeout timer should to be disabled now - since
380 * del_timer_sync() is very expensive, we just tell via the
381 * reset flag to ignore the pending timeout timer.
383 ap_dev
->reset
= AP_RESET_IGNORE
;
387 * AP device related attributes.
389 static ssize_t
ap_hwtype_show(struct device
*dev
,
390 struct device_attribute
*attr
, char *buf
)
392 struct ap_device
*ap_dev
= to_ap_dev(dev
);
393 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->device_type
);
395 static DEVICE_ATTR(hwtype
, 0444, ap_hwtype_show
, NULL
);
397 static ssize_t
ap_depth_show(struct device
*dev
, struct device_attribute
*attr
,
400 struct ap_device
*ap_dev
= to_ap_dev(dev
);
401 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->queue_depth
);
403 static DEVICE_ATTR(depth
, 0444, ap_depth_show
, NULL
);
405 static ssize_t
ap_request_count_show(struct device
*dev
,
406 struct device_attribute
*attr
,
409 struct ap_device
*ap_dev
= to_ap_dev(dev
);
412 spin_lock_bh(&ap_dev
->lock
);
413 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->total_request_count
);
414 spin_unlock_bh(&ap_dev
->lock
);
418 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
420 static ssize_t
ap_modalias_show(struct device
*dev
,
421 struct device_attribute
*attr
, char *buf
)
423 return sprintf(buf
, "ap:t%02X", to_ap_dev(dev
)->device_type
);
426 static DEVICE_ATTR(modalias
, 0444, ap_modalias_show
, NULL
);
428 static struct attribute
*ap_dev_attrs
[] = {
429 &dev_attr_hwtype
.attr
,
430 &dev_attr_depth
.attr
,
431 &dev_attr_request_count
.attr
,
432 &dev_attr_modalias
.attr
,
435 static struct attribute_group ap_dev_attr_group
= {
436 .attrs
= ap_dev_attrs
441 * @dev: Pointer to device
442 * @drv: Pointer to device_driver
444 * AP bus driver registration/unregistration.
446 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
448 struct ap_device
*ap_dev
= to_ap_dev(dev
);
449 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
450 struct ap_device_id
*id
;
453 * Compare device type of the device with the list of
454 * supported types of the device_driver.
456 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
457 if ((id
->match_flags
& AP_DEVICE_ID_MATCH_DEVICE_TYPE
) &&
458 (id
->dev_type
!= ap_dev
->device_type
))
466 * ap_uevent(): Uevent function for AP devices.
467 * @dev: Pointer to device
468 * @env: Pointer to kobj_uevent_env
470 * It sets up a single environment variable DEV_TYPE which contains the
471 * hardware device type.
473 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
475 struct ap_device
*ap_dev
= to_ap_dev(dev
);
481 /* Set up DEV_TYPE environment variable. */
482 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
487 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
492 static struct bus_type ap_bus_type
= {
494 .match
= &ap_bus_match
,
495 .uevent
= &ap_uevent
,
498 static int ap_device_probe(struct device
*dev
)
500 struct ap_device
*ap_dev
= to_ap_dev(dev
);
501 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
504 ap_dev
->drv
= ap_drv
;
505 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
507 spin_lock_bh(&ap_device_lock
);
508 list_add(&ap_dev
->list
, &ap_device_list
);
509 spin_unlock_bh(&ap_device_lock
);
515 * __ap_flush_queue(): Flush requests.
516 * @ap_dev: Pointer to the AP device
518 * Flush all requests from the request/pending queue of an AP device.
520 static void __ap_flush_queue(struct ap_device
*ap_dev
)
522 struct ap_message
*ap_msg
, *next
;
524 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->pendingq
, list
) {
525 list_del_init(&ap_msg
->list
);
526 ap_dev
->pendingq_count
--;
527 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
529 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->requestq
, list
) {
530 list_del_init(&ap_msg
->list
);
531 ap_dev
->requestq_count
--;
532 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
536 void ap_flush_queue(struct ap_device
*ap_dev
)
538 spin_lock_bh(&ap_dev
->lock
);
539 __ap_flush_queue(ap_dev
);
540 spin_unlock_bh(&ap_dev
->lock
);
542 EXPORT_SYMBOL(ap_flush_queue
);
544 static int ap_device_remove(struct device
*dev
)
546 struct ap_device
*ap_dev
= to_ap_dev(dev
);
547 struct ap_driver
*ap_drv
= ap_dev
->drv
;
549 ap_flush_queue(ap_dev
);
550 del_timer_sync(&ap_dev
->timeout
);
551 spin_lock_bh(&ap_device_lock
);
552 list_del_init(&ap_dev
->list
);
553 spin_unlock_bh(&ap_device_lock
);
555 ap_drv
->remove(ap_dev
);
556 spin_lock_bh(&ap_dev
->lock
);
557 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
558 spin_unlock_bh(&ap_dev
->lock
);
562 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
565 struct device_driver
*drv
= &ap_drv
->driver
;
567 drv
->bus
= &ap_bus_type
;
568 drv
->probe
= ap_device_probe
;
569 drv
->remove
= ap_device_remove
;
572 return driver_register(drv
);
574 EXPORT_SYMBOL(ap_driver_register
);
576 void ap_driver_unregister(struct ap_driver
*ap_drv
)
578 driver_unregister(&ap_drv
->driver
);
580 EXPORT_SYMBOL(ap_driver_unregister
);
585 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
587 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
590 static BUS_ATTR(ap_domain
, 0444, ap_domain_show
, NULL
);
592 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
594 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
597 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
598 const char *buf
, size_t count
)
602 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
604 ap_config_time
= time
;
605 if (!timer_pending(&ap_config_timer
) ||
606 !mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
)) {
607 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
608 add_timer(&ap_config_timer
);
613 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
615 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
617 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
620 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
621 const char *buf
, size_t count
)
625 if (sscanf(buf
, "%d\n", &flag
) != 1)
628 rc
= ap_poll_thread_start();
633 ap_poll_thread_stop();
637 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
639 static struct bus_attribute
*const ap_bus_attrs
[] = {
641 &bus_attr_config_time
,
642 &bus_attr_poll_thread
,
647 * ap_select_domain(): Select an AP domain.
649 * Pick one of the 16 AP domains.
651 static int ap_select_domain(void)
653 int queue_depth
, device_type
, count
, max_count
, best_domain
;
657 * We want to use a single domain. Either the one specified with
658 * the "domain=" parameter or the domain with the maximum number
661 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
)
662 /* Domain has already been selected. */
666 for (i
= 0; i
< AP_DOMAINS
; i
++) {
668 for (j
= 0; j
< AP_DEVICES
; j
++) {
669 ap_qid_t qid
= AP_MKQID(j
, i
);
670 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
675 if (count
> max_count
) {
680 if (best_domain
>= 0){
681 ap_domain_index
= best_domain
;
688 * ap_probe_device_type(): Find the device type of an AP.
689 * @ap_dev: pointer to the AP device.
691 * Find the device type if query queue returned a device type of 0.
693 static int ap_probe_device_type(struct ap_device
*ap_dev
)
695 static unsigned char msg
[] = {
696 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
697 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
698 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
699 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
700 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
701 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
702 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
703 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
704 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
705 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
706 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
707 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
708 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
709 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
710 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
711 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
712 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
713 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
714 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
715 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
716 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
717 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
718 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
719 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
720 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
721 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
722 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
723 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
724 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
725 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
726 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
727 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
728 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
729 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
730 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
731 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
732 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
733 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
734 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
735 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
736 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
737 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
738 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
739 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
740 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
742 struct ap_queue_status status
;
743 unsigned long long psmid
;
747 reply
= (void *) get_zeroed_page(GFP_KERNEL
);
753 status
= __ap_send(ap_dev
->qid
, 0x0102030405060708ULL
,
755 if (status
.response_code
!= AP_RESPONSE_NORMAL
) {
760 /* Wait for the test message to complete. */
761 for (i
= 0; i
< 6; i
++) {
763 status
= __ap_recv(ap_dev
->qid
, &psmid
, reply
, 4096);
764 if (status
.response_code
== AP_RESPONSE_NORMAL
&&
765 psmid
== 0x0102030405060708ULL
)
770 if (reply
[0] == 0x00 && reply
[1] == 0x86)
771 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICC
;
773 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICA
;
779 free_page((unsigned long) reply
);
785 * __ap_scan_bus(): Scan the AP bus.
786 * @dev: Pointer to device
787 * @data: Pointer to data
789 * Scan the AP bus for new devices.
791 static int __ap_scan_bus(struct device
*dev
, void *data
)
793 return to_ap_dev(dev
)->qid
== (ap_qid_t
)(unsigned long) data
;
796 static void ap_device_release(struct device
*dev
)
798 struct ap_device
*ap_dev
= to_ap_dev(dev
);
803 static void ap_scan_bus(struct work_struct
*unused
)
805 struct ap_device
*ap_dev
;
808 int queue_depth
, device_type
;
811 if (ap_select_domain() != 0)
813 for (i
= 0; i
< AP_DEVICES
; i
++) {
814 qid
= AP_MKQID(i
, ap_domain_index
);
815 dev
= bus_find_device(&ap_bus_type
, NULL
,
816 (void *)(unsigned long)qid
,
818 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
821 set_current_state(TASK_UNINTERRUPTIBLE
);
822 schedule_timeout(AP_RESET_TIMEOUT
);
823 rc
= ap_query_queue(qid
, &queue_depth
,
826 ap_dev
= to_ap_dev(dev
);
827 spin_lock_bh(&ap_dev
->lock
);
828 if (rc
|| ap_dev
->unregistered
) {
829 spin_unlock_bh(&ap_dev
->lock
);
830 device_unregister(dev
);
834 spin_unlock_bh(&ap_dev
->lock
);
840 rc
= ap_init_queue(qid
);
843 ap_dev
= kzalloc(sizeof(*ap_dev
), GFP_KERNEL
);
847 ap_dev
->queue_depth
= queue_depth
;
848 ap_dev
->unregistered
= 1;
849 spin_lock_init(&ap_dev
->lock
);
850 INIT_LIST_HEAD(&ap_dev
->pendingq
);
851 INIT_LIST_HEAD(&ap_dev
->requestq
);
852 INIT_LIST_HEAD(&ap_dev
->list
);
853 setup_timer(&ap_dev
->timeout
, ap_request_timeout
,
854 (unsigned long) ap_dev
);
855 if (device_type
== 0)
856 ap_probe_device_type(ap_dev
);
858 ap_dev
->device_type
= device_type
;
860 ap_dev
->device
.bus
= &ap_bus_type
;
861 ap_dev
->device
.parent
= ap_root_device
;
862 snprintf(ap_dev
->device
.bus_id
, BUS_ID_SIZE
, "card%02x",
863 AP_QID_DEVICE(ap_dev
->qid
));
864 ap_dev
->device
.release
= ap_device_release
;
865 rc
= device_register(&ap_dev
->device
);
870 /* Add device attributes. */
871 rc
= sysfs_create_group(&ap_dev
->device
.kobj
,
874 spin_lock_bh(&ap_dev
->lock
);
875 ap_dev
->unregistered
= 0;
876 spin_unlock_bh(&ap_dev
->lock
);
879 device_unregister(&ap_dev
->device
);
884 ap_config_timeout(unsigned long ptr
)
886 queue_work(ap_work_queue
, &ap_config_work
);
887 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
888 add_timer(&ap_config_timer
);
892 * ap_schedule_poll_timer(): Schedule poll timer.
894 * Set up the timer to run the poll tasklet
896 static inline void ap_schedule_poll_timer(void)
898 if (timer_pending(&ap_poll_timer
))
900 mod_timer(&ap_poll_timer
, jiffies
+ AP_POLL_TIME
);
904 * ap_poll_read(): Receive pending reply messages from an AP device.
905 * @ap_dev: pointer to the AP device
906 * @flags: pointer to control flags, bit 2^0 is set if another poll is
907 * required, bit 2^1 is set if the poll timer needs to get armed
909 * Returns 0 if the device is still present, -ENODEV if not.
911 static int ap_poll_read(struct ap_device
*ap_dev
, unsigned long *flags
)
913 struct ap_queue_status status
;
914 struct ap_message
*ap_msg
;
916 if (ap_dev
->queue_count
<= 0)
918 status
= __ap_recv(ap_dev
->qid
, &ap_dev
->reply
->psmid
,
919 ap_dev
->reply
->message
, ap_dev
->reply
->length
);
920 switch (status
.response_code
) {
921 case AP_RESPONSE_NORMAL
:
922 atomic_dec(&ap_poll_requests
);
923 ap_decrease_queue_count(ap_dev
);
924 list_for_each_entry(ap_msg
, &ap_dev
->pendingq
, list
) {
925 if (ap_msg
->psmid
!= ap_dev
->reply
->psmid
)
927 list_del_init(&ap_msg
->list
);
928 ap_dev
->pendingq_count
--;
929 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ap_dev
->reply
);
932 if (ap_dev
->queue_count
> 0)
935 case AP_RESPONSE_NO_PENDING_REPLY
:
936 if (status
.queue_empty
) {
937 /* The card shouldn't forget requests but who knows. */
938 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
939 ap_dev
->queue_count
= 0;
940 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
941 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
942 ap_dev
->pendingq_count
= 0;
953 * ap_poll_write(): Send messages from the request queue to an AP device.
954 * @ap_dev: pointer to the AP device
955 * @flags: pointer to control flags, bit 2^0 is set if another poll is
956 * required, bit 2^1 is set if the poll timer needs to get armed
958 * Returns 0 if the device is still present, -ENODEV if not.
960 static int ap_poll_write(struct ap_device
*ap_dev
, unsigned long *flags
)
962 struct ap_queue_status status
;
963 struct ap_message
*ap_msg
;
965 if (ap_dev
->requestq_count
<= 0 ||
966 ap_dev
->queue_count
>= ap_dev
->queue_depth
)
968 /* Start the next request on the queue. */
969 ap_msg
= list_entry(ap_dev
->requestq
.next
, struct ap_message
, list
);
970 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
971 ap_msg
->message
, ap_msg
->length
);
972 switch (status
.response_code
) {
973 case AP_RESPONSE_NORMAL
:
974 atomic_inc(&ap_poll_requests
);
975 ap_increase_queue_count(ap_dev
);
976 list_move_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
977 ap_dev
->requestq_count
--;
978 ap_dev
->pendingq_count
++;
979 if (ap_dev
->queue_count
< ap_dev
->queue_depth
&&
980 ap_dev
->requestq_count
> 0)
984 case AP_RESPONSE_Q_FULL
:
985 case AP_RESPONSE_RESET_IN_PROGRESS
:
988 case AP_RESPONSE_MESSAGE_TOO_BIG
:
997 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
998 * @ap_dev: pointer to the bus device
999 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1000 * required, bit 2^1 is set if the poll timer needs to get armed
1002 * Poll AP device for pending replies and send new messages. If either
1003 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1006 static inline int ap_poll_queue(struct ap_device
*ap_dev
, unsigned long *flags
)
1010 rc
= ap_poll_read(ap_dev
, flags
);
1013 return ap_poll_write(ap_dev
, flags
);
1017 * __ap_queue_message(): Queue a message to a device.
1018 * @ap_dev: pointer to the AP device
1019 * @ap_msg: the message to be queued
1021 * Queue a message to a device. Returns 0 if successful.
1023 static int __ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1025 struct ap_queue_status status
;
1027 if (list_empty(&ap_dev
->requestq
) &&
1028 ap_dev
->queue_count
< ap_dev
->queue_depth
) {
1029 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1030 ap_msg
->message
, ap_msg
->length
);
1031 switch (status
.response_code
) {
1032 case AP_RESPONSE_NORMAL
:
1033 list_add_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1034 atomic_inc(&ap_poll_requests
);
1035 ap_dev
->pendingq_count
++;
1036 ap_increase_queue_count(ap_dev
);
1037 ap_dev
->total_request_count
++;
1039 case AP_RESPONSE_Q_FULL
:
1040 case AP_RESPONSE_RESET_IN_PROGRESS
:
1041 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1042 ap_dev
->requestq_count
++;
1043 ap_dev
->total_request_count
++;
1045 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1046 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-EINVAL
));
1048 default: /* Device is gone. */
1049 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1053 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1054 ap_dev
->requestq_count
++;
1055 ap_dev
->total_request_count
++;
1058 ap_schedule_poll_timer();
1062 void ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1064 unsigned long flags
;
1067 spin_lock_bh(&ap_dev
->lock
);
1068 if (!ap_dev
->unregistered
) {
1069 /* Make room on the queue by polling for finished requests. */
1070 rc
= ap_poll_queue(ap_dev
, &flags
);
1072 rc
= __ap_queue_message(ap_dev
, ap_msg
);
1074 wake_up(&ap_poll_wait
);
1076 ap_dev
->unregistered
= 1;
1078 ap_dev
->drv
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1081 spin_unlock_bh(&ap_dev
->lock
);
1083 device_unregister(&ap_dev
->device
);
1085 EXPORT_SYMBOL(ap_queue_message
);
1088 * ap_cancel_message(): Cancel a crypto request.
1089 * @ap_dev: The AP device that has the message queued
1090 * @ap_msg: The message that is to be removed
1092 * Cancel a crypto request. This is done by removing the request
1093 * from the device pending or request queue. Note that the
1094 * request stays on the AP queue. When it finishes the message
1095 * reply will be discarded because the psmid can't be found.
1097 void ap_cancel_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1099 struct ap_message
*tmp
;
1101 spin_lock_bh(&ap_dev
->lock
);
1102 if (!list_empty(&ap_msg
->list
)) {
1103 list_for_each_entry(tmp
, &ap_dev
->pendingq
, list
)
1104 if (tmp
->psmid
== ap_msg
->psmid
) {
1105 ap_dev
->pendingq_count
--;
1108 ap_dev
->requestq_count
--;
1110 list_del_init(&ap_msg
->list
);
1112 spin_unlock_bh(&ap_dev
->lock
);
1114 EXPORT_SYMBOL(ap_cancel_message
);
1117 * ap_poll_timeout(): AP receive polling for finished AP requests.
1118 * @unused: Unused variable.
1120 * Schedules the AP tasklet.
1122 static void ap_poll_timeout(unsigned long unused
)
1124 tasklet_schedule(&ap_tasklet
);
1128 * ap_reset(): Reset a not responding AP device.
1129 * @ap_dev: Pointer to the AP device
1131 * Reset a not responding AP device and move all requests from the
1132 * pending queue to the request queue.
1134 static void ap_reset(struct ap_device
*ap_dev
)
1138 ap_dev
->reset
= AP_RESET_IGNORE
;
1139 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1140 ap_dev
->queue_count
= 0;
1141 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1142 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1143 ap_dev
->pendingq_count
= 0;
1144 rc
= ap_init_queue(ap_dev
->qid
);
1146 ap_dev
->unregistered
= 1;
1149 static int __ap_poll_all(struct ap_device
*ap_dev
, unsigned long *flags
)
1151 spin_lock(&ap_dev
->lock
);
1152 if (!ap_dev
->unregistered
) {
1153 if (ap_poll_queue(ap_dev
, flags
))
1154 ap_dev
->unregistered
= 1;
1155 if (ap_dev
->reset
== AP_RESET_DO
)
1158 spin_unlock(&ap_dev
->lock
);
1163 * ap_poll_all(): Poll all AP devices.
1164 * @dummy: Unused variable
1166 * Poll all AP devices on the bus in a round robin fashion. Continue
1167 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1168 * of the control flags has been set arm the poll timer.
1170 static void ap_poll_all(unsigned long dummy
)
1172 unsigned long flags
;
1173 struct ap_device
*ap_dev
;
1177 spin_lock(&ap_device_lock
);
1178 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1179 __ap_poll_all(ap_dev
, &flags
);
1181 spin_unlock(&ap_device_lock
);
1182 } while (flags
& 1);
1184 ap_schedule_poll_timer();
1188 * ap_poll_thread(): Thread that polls for finished requests.
1189 * @data: Unused pointer
1191 * AP bus poll thread. The purpose of this thread is to poll for
1192 * finished requests in a loop if there is a "free" cpu - that is
1193 * a cpu that doesn't have anything better to do. The polling stops
1194 * as soon as there is another task or if all messages have been
1197 static int ap_poll_thread(void *data
)
1199 DECLARE_WAITQUEUE(wait
, current
);
1200 unsigned long flags
;
1202 struct ap_device
*ap_dev
;
1204 set_user_nice(current
, 19);
1206 if (need_resched()) {
1210 add_wait_queue(&ap_poll_wait
, &wait
);
1211 set_current_state(TASK_INTERRUPTIBLE
);
1212 if (kthread_should_stop())
1214 requests
= atomic_read(&ap_poll_requests
);
1217 set_current_state(TASK_RUNNING
);
1218 remove_wait_queue(&ap_poll_wait
, &wait
);
1221 spin_lock_bh(&ap_device_lock
);
1222 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1223 __ap_poll_all(ap_dev
, &flags
);
1225 spin_unlock_bh(&ap_device_lock
);
1227 set_current_state(TASK_RUNNING
);
1228 remove_wait_queue(&ap_poll_wait
, &wait
);
1232 static int ap_poll_thread_start(void)
1236 mutex_lock(&ap_poll_thread_mutex
);
1237 if (!ap_poll_kthread
) {
1238 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
1239 rc
= IS_ERR(ap_poll_kthread
) ? PTR_ERR(ap_poll_kthread
) : 0;
1241 ap_poll_kthread
= NULL
;
1245 mutex_unlock(&ap_poll_thread_mutex
);
1249 static void ap_poll_thread_stop(void)
1251 mutex_lock(&ap_poll_thread_mutex
);
1252 if (ap_poll_kthread
) {
1253 kthread_stop(ap_poll_kthread
);
1254 ap_poll_kthread
= NULL
;
1256 mutex_unlock(&ap_poll_thread_mutex
);
1260 * ap_request_timeout(): Handling of request timeouts
1261 * @data: Holds the AP device.
1263 * Handles request timeouts.
1265 static void ap_request_timeout(unsigned long data
)
1267 struct ap_device
*ap_dev
= (struct ap_device
*) data
;
1269 if (ap_dev
->reset
== AP_RESET_ARMED
)
1270 ap_dev
->reset
= AP_RESET_DO
;
1273 static void ap_reset_domain(void)
1277 if (ap_domain_index
!= -1)
1278 for (i
= 0; i
< AP_DEVICES
; i
++)
1279 ap_reset_queue(AP_MKQID(i
, ap_domain_index
));
1282 static void ap_reset_all(void)
1286 for (i
= 0; i
< AP_DOMAINS
; i
++)
1287 for (j
= 0; j
< AP_DEVICES
; j
++)
1288 ap_reset_queue(AP_MKQID(j
, i
));
1291 static struct reset_call ap_reset_call
= {
1296 * ap_module_init(): The module initialization code.
1298 * Initializes the module.
1300 int __init
ap_module_init(void)
1304 if (ap_domain_index
< -1 || ap_domain_index
>= AP_DOMAINS
) {
1305 printk(KERN_WARNING
"Invalid param: domain = %d. "
1306 " Not loading.\n", ap_domain_index
);
1309 if (ap_instructions_available() != 0) {
1310 printk(KERN_WARNING
"AP instructions not installed.\n");
1313 register_reset_call(&ap_reset_call
);
1315 /* Create /sys/bus/ap. */
1316 rc
= bus_register(&ap_bus_type
);
1319 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1320 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1325 /* Create /sys/devices/ap. */
1326 ap_root_device
= s390_root_dev_register("ap");
1327 rc
= IS_ERR(ap_root_device
) ? PTR_ERR(ap_root_device
) : 0;
1331 ap_work_queue
= create_singlethread_workqueue("kapwork");
1332 if (!ap_work_queue
) {
1337 if (ap_select_domain() == 0)
1340 /* Setup the AP bus rescan timer. */
1341 init_timer(&ap_config_timer
);
1342 ap_config_timer
.function
= ap_config_timeout
;
1343 ap_config_timer
.data
= 0;
1344 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1345 add_timer(&ap_config_timer
);
1347 /* Start the low priority AP bus poll thread. */
1348 if (ap_thread_flag
) {
1349 rc
= ap_poll_thread_start();
1357 del_timer_sync(&ap_config_timer
);
1358 del_timer_sync(&ap_poll_timer
);
1359 destroy_workqueue(ap_work_queue
);
1361 s390_root_dev_unregister(ap_root_device
);
1364 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1365 bus_unregister(&ap_bus_type
);
1367 unregister_reset_call(&ap_reset_call
);
1371 static int __ap_match_all(struct device
*dev
, void *data
)
1377 * ap_modules_exit(): The module termination code
1379 * Terminates the module.
1381 void ap_module_exit(void)
1387 ap_poll_thread_stop();
1388 del_timer_sync(&ap_config_timer
);
1389 del_timer_sync(&ap_poll_timer
);
1390 destroy_workqueue(ap_work_queue
);
1391 tasklet_kill(&ap_tasklet
);
1392 s390_root_dev_unregister(ap_root_device
);
1393 while ((dev
= bus_find_device(&ap_bus_type
, NULL
, NULL
,
1396 device_unregister(dev
);
1399 for (i
= 0; ap_bus_attrs
[i
]; i
++)
1400 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1401 bus_unregister(&ap_bus_type
);
1402 unregister_reset_call(&ap_reset_call
);
1405 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1406 module_init(ap_module_init
);
1407 module_exit(ap_module_exit
);