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>
39 /* Some prototypes. */
40 static void ap_scan_bus(struct work_struct *);
41 static void ap_poll_all(unsigned long);
42 static void ap_poll_timeout(unsigned long);
43 static int ap_poll_thread_start(void);
44 static void ap_poll_thread_stop(void);
49 MODULE_AUTHOR("IBM Corporation");
50 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
51 "Copyright 2006 IBM Corporation");
52 MODULE_LICENSE("GPL");
57 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
58 module_param_named(domain, ap_domain_index, int, 0000);
59 MODULE_PARM_DESC(domain, "domain index for ap devices");
60 EXPORT_SYMBOL(ap_domain_index);
62 static int ap_thread_flag = 1;
63 module_param_named(poll_thread, ap_thread_flag, int, 0000);
64 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 1 (on).");
66 static struct device *ap_root_device = NULL;
69 * Workqueue & timer for bus rescan.
71 static struct workqueue_struct *ap_work_queue;
72 static struct timer_list ap_config_timer;
73 static int ap_config_time = AP_CONFIG_TIME;
74 static DECLARE_WORK(ap_config_work, ap_scan_bus);
77 * Tasklet & timer for AP request polling.
79 static struct timer_list ap_poll_timer = TIMER_INITIALIZER(ap_poll_timeout,0,0);
80 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
81 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
82 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
83 static struct task_struct *ap_poll_kthread = NULL;
84 static DEFINE_MUTEX(ap_poll_thread_mutex);
87 * Test if ap instructions are available.
89 * Returns 0 if the ap instructions are installed.
91 static inline int ap_instructions_available(void)
93 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
94 register unsigned long reg1 asm ("1") = -ENODEV;
95 register unsigned long reg2 asm ("2") = 0UL;
98 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
102 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
107 * Test adjunct processor queue.
108 * @qid: the ap queue number
109 * @queue_depth: pointer to queue depth value
110 * @device_type: pointer to device type value
112 * Returns ap queue status structure.
114 static inline struct ap_queue_status
115 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
117 register unsigned long reg0 asm ("0") = qid;
118 register struct ap_queue_status reg1 asm ("1");
119 register unsigned long reg2 asm ("2") = 0UL;
121 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
122 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
123 *device_type = (int) (reg2 >> 24);
124 *queue_depth = (int) (reg2 & 0xff);
129 * Reset adjunct processor queue.
130 * @qid: the ap queue number
132 * Returns ap queue status structure.
134 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
136 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
137 register struct ap_queue_status reg1 asm ("1");
138 register unsigned long reg2 asm ("2") = 0UL;
141 ".long 0xb2af0000" /* PQAP(RAPQ) */
142 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
147 * Send message to adjunct processor queue.
148 * @qid: the ap queue number
149 * @psmid: the program supplied message identifier
150 * @msg: the message text
151 * @length: the message length
153 * Returns ap queue status structure.
155 * Condition code 1 on NQAP can't happen because the L bit is 1.
157 * Condition code 2 on NQAP also means the send is incomplete,
158 * because a segment boundary was reached. The NQAP is repeated.
160 static inline struct ap_queue_status
161 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
163 typedef struct { char _[length]; } msgblock;
164 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
165 register struct ap_queue_status reg1 asm ("1");
166 register unsigned long reg2 asm ("2") = (unsigned long) msg;
167 register unsigned long reg3 asm ("3") = (unsigned long) length;
168 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
169 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
172 "0: .long 0xb2ad0042\n" /* DQAP */
174 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
175 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
180 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
182 struct ap_queue_status status;
184 status = __ap_send(qid, psmid, msg, length);
185 switch (status.response_code) {
186 case AP_RESPONSE_NORMAL:
188 case AP_RESPONSE_Q_FULL:
190 default: /* Device is gone. */
194 EXPORT_SYMBOL(ap_send);
197 * Receive message from adjunct processor queue.
198 * @qid: the ap queue number
199 * @psmid: pointer to program supplied message identifier
200 * @msg: the message text
201 * @length: the message length
203 * Returns ap queue status structure.
205 * Condition code 1 on DQAP means the receive has taken place
206 * but only partially. The response is incomplete, hence the
209 * Condition code 2 on DQAP also means the receive is incomplete,
210 * 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)
256 EXPORT_SYMBOL(ap_recv);
259 * Check if an AP queue is available. The test is repeated for
260 * AP_MAX_RESET times.
261 * @qid: the ap queue number
262 * @queue_depth: pointer to queue depth value
263 * @device_type: pointer to device type value
265 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
267 struct ap_queue_status status;
268 int t_depth, t_device_type, rc, i;
271 for (i = 0; i < AP_MAX_RESET; i++) {
272 status = ap_test_queue(qid, &t_depth, &t_device_type);
273 switch (status.response_code) {
274 case AP_RESPONSE_NORMAL:
275 *queue_depth = t_depth + 1;
276 *device_type = t_device_type;
279 case AP_RESPONSE_Q_NOT_AVAIL:
282 case AP_RESPONSE_RESET_IN_PROGRESS:
284 case AP_RESPONSE_DECONFIGURED:
287 case AP_RESPONSE_CHECKSTOPPED:
290 case AP_RESPONSE_BUSY:
297 if (i < AP_MAX_RESET - 1)
304 * Reset an AP queue and wait for it to become available again.
305 * @qid: the ap queue number
307 static int ap_init_queue(ap_qid_t qid)
309 struct ap_queue_status status;
313 status = ap_reset_queue(qid);
314 for (i = 0; i < AP_MAX_RESET; i++) {
315 switch (status.response_code) {
316 case AP_RESPONSE_NORMAL:
317 if (status.queue_empty)
320 case AP_RESPONSE_Q_NOT_AVAIL:
321 case AP_RESPONSE_DECONFIGURED:
322 case AP_RESPONSE_CHECKSTOPPED:
323 i = AP_MAX_RESET; /* return with -ENODEV */
325 case AP_RESPONSE_RESET_IN_PROGRESS:
326 case AP_RESPONSE_BUSY:
332 if (i < AP_MAX_RESET - 1) {
334 status = ap_test_queue(qid, &dummy, &dummy);
341 * AP device related attributes.
343 static ssize_t ap_hwtype_show(struct device *dev,
344 struct device_attribute *attr, char *buf)
346 struct ap_device *ap_dev = to_ap_dev(dev);
347 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
349 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
351 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
354 struct ap_device *ap_dev = to_ap_dev(dev);
355 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
357 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
359 static ssize_t ap_request_count_show(struct device *dev,
360 struct device_attribute *attr,
363 struct ap_device *ap_dev = to_ap_dev(dev);
366 spin_lock_bh(&ap_dev->lock);
367 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
368 spin_unlock_bh(&ap_dev->lock);
372 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
374 static ssize_t ap_modalias_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
380 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
382 static struct attribute *ap_dev_attrs[] = {
383 &dev_attr_hwtype.attr,
384 &dev_attr_depth.attr,
385 &dev_attr_request_count.attr,
386 &dev_attr_modalias.attr,
389 static struct attribute_group ap_dev_attr_group = {
390 .attrs = ap_dev_attrs
394 * AP bus driver registration/unregistration.
396 static int ap_bus_match(struct device *dev, struct device_driver *drv)
398 struct ap_device *ap_dev = to_ap_dev(dev);
399 struct ap_driver *ap_drv = to_ap_drv(drv);
400 struct ap_device_id *id;
403 * Compare device type of the device with the list of
404 * supported types of the device_driver.
406 for (id = ap_drv->ids; id->match_flags; id++) {
407 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
408 (id->dev_type != ap_dev->device_type))
416 * uevent function for AP devices. It sets up a single environment
417 * variable DEV_TYPE which contains the hardware device type.
419 static int ap_uevent (struct device *dev, char **envp, int num_envp,
420 char *buffer, int buffer_size)
422 struct ap_device *ap_dev = to_ap_dev(dev);
428 /* Set up DEV_TYPE environment variable. */
430 length = scnprintf(buffer, buffer_size, "DEV_TYPE=%04X",
431 ap_dev->device_type);
432 if (buffer_size - length <= 0)
435 buffer_size -= length;
438 length = scnprintf(buffer, buffer_size, "MODALIAS=ap:t%02X",
439 ap_dev->device_type);
440 if (buffer_size - length <= 0)
446 static struct bus_type ap_bus_type = {
448 .match = &ap_bus_match,
449 .uevent = &ap_uevent,
452 static int ap_device_probe(struct device *dev)
454 struct ap_device *ap_dev = to_ap_dev(dev);
455 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
458 ap_dev->drv = ap_drv;
459 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
464 * Flush all requests from the request/pending queue of an AP device.
465 * @ap_dev: pointer to the AP device.
467 static inline void __ap_flush_queue(struct ap_device *ap_dev)
469 struct ap_message *ap_msg, *next;
471 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
472 list_del_init(&ap_msg->list);
473 ap_dev->pendingq_count--;
474 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
476 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
477 list_del_init(&ap_msg->list);
478 ap_dev->requestq_count--;
479 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
483 void ap_flush_queue(struct ap_device *ap_dev)
485 spin_lock_bh(&ap_dev->lock);
486 __ap_flush_queue(ap_dev);
487 spin_unlock_bh(&ap_dev->lock);
489 EXPORT_SYMBOL(ap_flush_queue);
491 static int ap_device_remove(struct device *dev)
493 struct ap_device *ap_dev = to_ap_dev(dev);
494 struct ap_driver *ap_drv = ap_dev->drv;
496 ap_flush_queue(ap_dev);
498 ap_drv->remove(ap_dev);
502 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
505 struct device_driver *drv = &ap_drv->driver;
507 drv->bus = &ap_bus_type;
508 drv->probe = ap_device_probe;
509 drv->remove = ap_device_remove;
512 return driver_register(drv);
514 EXPORT_SYMBOL(ap_driver_register);
516 void ap_driver_unregister(struct ap_driver *ap_drv)
518 driver_unregister(&ap_drv->driver);
520 EXPORT_SYMBOL(ap_driver_unregister);
525 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
527 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
530 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
532 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
534 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
537 static ssize_t ap_config_time_store(struct bus_type *bus,
538 const char *buf, size_t count)
542 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
544 ap_config_time = time;
545 if (!timer_pending(&ap_config_timer) ||
546 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
547 ap_config_timer.expires = jiffies + ap_config_time * HZ;
548 add_timer(&ap_config_timer);
553 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
555 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
557 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
560 static ssize_t ap_poll_thread_store(struct bus_type *bus,
561 const char *buf, size_t count)
565 if (sscanf(buf, "%d\n", &flag) != 1)
568 rc = ap_poll_thread_start();
573 ap_poll_thread_stop();
577 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
579 static struct bus_attribute *const ap_bus_attrs[] = {
581 &bus_attr_config_time,
582 &bus_attr_poll_thread,
587 * Pick one of the 16 ap domains.
589 static inline int ap_select_domain(void)
591 int queue_depth, device_type, count, max_count, best_domain;
595 * We want to use a single domain. Either the one specified with
596 * the "domain=" parameter or the domain with the maximum number
599 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
600 /* Domain has already been selected. */
604 for (i = 0; i < AP_DOMAINS; i++) {
606 for (j = 0; j < AP_DEVICES; j++) {
607 ap_qid_t qid = AP_MKQID(j, i);
608 rc = ap_query_queue(qid, &queue_depth, &device_type);
613 if (count > max_count) {
618 if (best_domain >= 0){
619 ap_domain_index = best_domain;
626 * Find the device type if query queue returned a device type of 0.
627 * @ap_dev: pointer to the AP device.
629 static int ap_probe_device_type(struct ap_device *ap_dev)
631 static unsigned char msg[] = {
632 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
633 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
634 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
635 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
636 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
637 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
638 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
639 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
640 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
641 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
642 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
643 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
644 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
645 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
646 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
647 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
648 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
649 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
650 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
651 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
652 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
653 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
654 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
655 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
656 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
657 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
658 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
659 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
660 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
661 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
662 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
663 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
664 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
665 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
666 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
667 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
668 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
669 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
670 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
671 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
672 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
673 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
674 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
675 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
676 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
678 struct ap_queue_status status;
679 unsigned long long psmid;
683 reply = (void *) get_zeroed_page(GFP_KERNEL);
689 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
691 if (status.response_code != AP_RESPONSE_NORMAL) {
696 /* Wait for the test message to complete. */
697 for (i = 0; i < 6; i++) {
699 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
700 if (status.response_code == AP_RESPONSE_NORMAL &&
701 psmid == 0x0102030405060708ULL)
706 if (reply[0] == 0x00 && reply[1] == 0x86)
707 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
709 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
715 free_page((unsigned long) reply);
721 * Scan the ap bus for new devices.
723 static int __ap_scan_bus(struct device *dev, void *data)
725 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
728 static void ap_device_release(struct device *dev)
730 struct ap_device *ap_dev = to_ap_dev(dev);
735 static void ap_scan_bus(struct work_struct *unused)
737 struct ap_device *ap_dev;
740 int queue_depth, device_type;
743 if (ap_select_domain() != 0)
745 for (i = 0; i < AP_DEVICES; i++) {
746 qid = AP_MKQID(i, ap_domain_index);
747 dev = bus_find_device(&ap_bus_type, NULL,
748 (void *)(unsigned long)qid,
750 rc = ap_query_queue(qid, &queue_depth, &device_type);
753 device_unregister(dev);
762 rc = ap_init_queue(qid);
765 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
769 ap_dev->queue_depth = queue_depth;
770 ap_dev->unregistered = 1;
771 spin_lock_init(&ap_dev->lock);
772 INIT_LIST_HEAD(&ap_dev->pendingq);
773 INIT_LIST_HEAD(&ap_dev->requestq);
774 if (device_type == 0)
775 ap_probe_device_type(ap_dev);
777 ap_dev->device_type = device_type;
779 ap_dev->device.bus = &ap_bus_type;
780 ap_dev->device.parent = ap_root_device;
781 snprintf(ap_dev->device.bus_id, BUS_ID_SIZE, "card%02x",
782 AP_QID_DEVICE(ap_dev->qid));
783 ap_dev->device.release = ap_device_release;
784 rc = device_register(&ap_dev->device);
789 /* Add device attributes. */
790 rc = sysfs_create_group(&ap_dev->device.kobj,
793 spin_lock_bh(&ap_dev->lock);
794 ap_dev->unregistered = 0;
795 spin_unlock_bh(&ap_dev->lock);
798 device_unregister(&ap_dev->device);
803 ap_config_timeout(unsigned long ptr)
805 queue_work(ap_work_queue, &ap_config_work);
806 ap_config_timer.expires = jiffies + ap_config_time * HZ;
807 add_timer(&ap_config_timer);
811 * Set up the timer to run the poll tasklet
813 static inline void ap_schedule_poll_timer(void)
815 if (timer_pending(&ap_poll_timer))
817 mod_timer(&ap_poll_timer, jiffies + AP_POLL_TIME);
821 * Receive pending reply messages from an AP device.
822 * @ap_dev: pointer to the AP device
823 * @flags: pointer to control flags, bit 2^0 is set if another poll is
824 * required, bit 2^1 is set if the poll timer needs to get armed
825 * Returns 0 if the device is still present, -ENODEV if not.
827 static inline int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
829 struct ap_queue_status status;
830 struct ap_message *ap_msg;
832 if (ap_dev->queue_count <= 0)
834 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
835 ap_dev->reply->message, ap_dev->reply->length);
836 switch (status.response_code) {
837 case AP_RESPONSE_NORMAL:
838 atomic_dec(&ap_poll_requests);
839 ap_dev->queue_count--;
840 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
841 if (ap_msg->psmid != ap_dev->reply->psmid)
843 list_del_init(&ap_msg->list);
844 ap_dev->pendingq_count--;
845 ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
848 if (ap_dev->queue_count > 0)
851 case AP_RESPONSE_NO_PENDING_REPLY:
852 if (status.queue_empty) {
853 /* The card shouldn't forget requests but who knows. */
854 ap_dev->queue_count = 0;
855 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
856 ap_dev->requestq_count += ap_dev->pendingq_count;
857 ap_dev->pendingq_count = 0;
868 * Send messages from the request queue to an AP device.
869 * @ap_dev: pointer to the AP device
870 * @flags: pointer to control flags, bit 2^0 is set if another poll is
871 * required, bit 2^1 is set if the poll timer needs to get armed
872 * Returns 0 if the device is still present, -ENODEV if not.
874 static inline int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
876 struct ap_queue_status status;
877 struct ap_message *ap_msg;
879 if (ap_dev->requestq_count <= 0 ||
880 ap_dev->queue_count >= ap_dev->queue_depth)
882 /* Start the next request on the queue. */
883 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
884 status = __ap_send(ap_dev->qid, ap_msg->psmid,
885 ap_msg->message, ap_msg->length);
886 switch (status.response_code) {
887 case AP_RESPONSE_NORMAL:
888 atomic_inc(&ap_poll_requests);
889 ap_dev->queue_count++;
890 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
891 ap_dev->requestq_count--;
892 ap_dev->pendingq_count++;
893 if (ap_dev->queue_count < ap_dev->queue_depth &&
894 ap_dev->requestq_count > 0)
898 case AP_RESPONSE_Q_FULL:
901 case AP_RESPONSE_MESSAGE_TOO_BIG:
910 * Poll AP device for pending replies and send new messages. If either
911 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
912 * @ap_dev: pointer to the bus device
913 * @flags: pointer to control flags, bit 2^0 is set if another poll is
914 * required, bit 2^1 is set if the poll timer needs to get armed
917 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
921 rc = ap_poll_read(ap_dev, flags);
924 return ap_poll_write(ap_dev, flags);
928 * Queue a message to a device.
929 * @ap_dev: pointer to the AP device
930 * @ap_msg: the message to be queued
932 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
934 struct ap_queue_status status;
936 if (list_empty(&ap_dev->requestq) &&
937 ap_dev->queue_count < ap_dev->queue_depth) {
938 status = __ap_send(ap_dev->qid, ap_msg->psmid,
939 ap_msg->message, ap_msg->length);
940 switch (status.response_code) {
941 case AP_RESPONSE_NORMAL:
942 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
943 atomic_inc(&ap_poll_requests);
944 ap_dev->pendingq_count++;
945 ap_dev->queue_count++;
946 ap_dev->total_request_count++;
948 case AP_RESPONSE_Q_FULL:
949 list_add_tail(&ap_msg->list, &ap_dev->requestq);
950 ap_dev->requestq_count++;
951 ap_dev->total_request_count++;
953 case AP_RESPONSE_MESSAGE_TOO_BIG:
954 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
956 default: /* Device is gone. */
957 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
961 list_add_tail(&ap_msg->list, &ap_dev->requestq);
962 ap_dev->requestq_count++;
963 ap_dev->total_request_count++;
966 ap_schedule_poll_timer();
970 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
975 spin_lock_bh(&ap_dev->lock);
976 if (!ap_dev->unregistered) {
977 /* Make room on the queue by polling for finished requests. */
978 rc = ap_poll_queue(ap_dev, &flags);
980 rc = __ap_queue_message(ap_dev, ap_msg);
982 wake_up(&ap_poll_wait);
984 ap_dev->unregistered = 1;
986 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
989 spin_unlock_bh(&ap_dev->lock);
991 device_unregister(&ap_dev->device);
993 EXPORT_SYMBOL(ap_queue_message);
996 * Cancel a crypto request. This is done by removing the request
997 * from the devive pendingq or requestq queue. Note that the
998 * request stays on the AP queue. When it finishes the message
999 * reply will be discarded because the psmid can't be found.
1000 * @ap_dev: AP device that has the message queued
1001 * @ap_msg: the message that is to be removed
1003 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1005 struct ap_message *tmp;
1007 spin_lock_bh(&ap_dev->lock);
1008 if (!list_empty(&ap_msg->list)) {
1009 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1010 if (tmp->psmid == ap_msg->psmid) {
1011 ap_dev->pendingq_count--;
1014 ap_dev->requestq_count--;
1016 list_del_init(&ap_msg->list);
1018 spin_unlock_bh(&ap_dev->lock);
1020 EXPORT_SYMBOL(ap_cancel_message);
1023 * AP receive polling for finished AP requests
1025 static void ap_poll_timeout(unsigned long unused)
1027 tasklet_schedule(&ap_tasklet);
1031 * Poll all AP devices on the bus in a round robin fashion. Continue
1032 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1033 * of the control flags has been set arm the poll timer.
1035 static int __ap_poll_all(struct device *dev, void *data)
1037 struct ap_device *ap_dev = to_ap_dev(dev);
1040 spin_lock(&ap_dev->lock);
1041 if (!ap_dev->unregistered) {
1042 rc = ap_poll_queue(to_ap_dev(dev), (unsigned long *) data);
1044 ap_dev->unregistered = 1;
1047 spin_unlock(&ap_dev->lock);
1049 device_unregister(&ap_dev->device);
1053 static void ap_poll_all(unsigned long dummy)
1055 unsigned long flags;
1059 bus_for_each_dev(&ap_bus_type, NULL, &flags, __ap_poll_all);
1060 } while (flags & 1);
1062 ap_schedule_poll_timer();
1066 * AP bus poll thread. The purpose of this thread is to poll for
1067 * finished requests in a loop if there is a "free" cpu - that is
1068 * a cpu that doesn't have anything better to do. The polling stops
1069 * as soon as there is another task or if all messages have been
1072 static int ap_poll_thread(void *data)
1074 DECLARE_WAITQUEUE(wait, current);
1075 unsigned long flags;
1078 set_user_nice(current, 19);
1080 if (need_resched()) {
1084 add_wait_queue(&ap_poll_wait, &wait);
1085 set_current_state(TASK_INTERRUPTIBLE);
1086 if (kthread_should_stop())
1088 requests = atomic_read(&ap_poll_requests);
1091 set_current_state(TASK_RUNNING);
1092 remove_wait_queue(&ap_poll_wait, &wait);
1096 bus_for_each_dev(&ap_bus_type, NULL, &flags, __ap_poll_all);
1099 set_current_state(TASK_RUNNING);
1100 remove_wait_queue(&ap_poll_wait, &wait);
1104 static int ap_poll_thread_start(void)
1108 mutex_lock(&ap_poll_thread_mutex);
1109 if (!ap_poll_kthread) {
1110 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1111 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1113 ap_poll_kthread = NULL;
1117 mutex_unlock(&ap_poll_thread_mutex);
1121 static void ap_poll_thread_stop(void)
1123 mutex_lock(&ap_poll_thread_mutex);
1124 if (ap_poll_kthread) {
1125 kthread_stop(ap_poll_kthread);
1126 ap_poll_kthread = NULL;
1128 mutex_unlock(&ap_poll_thread_mutex);
1132 * The module initialization code.
1134 int __init ap_module_init(void)
1138 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1139 printk(KERN_WARNING "Invalid param: domain = %d. "
1140 " Not loading.\n", ap_domain_index);
1143 if (ap_instructions_available() != 0) {
1144 printk(KERN_WARNING "AP instructions not installed.\n");
1148 /* Create /sys/bus/ap. */
1149 rc = bus_register(&ap_bus_type);
1152 for (i = 0; ap_bus_attrs[i]; i++) {
1153 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1158 /* Create /sys/devices/ap. */
1159 ap_root_device = s390_root_dev_register("ap");
1160 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1164 ap_work_queue = create_singlethread_workqueue("kapwork");
1165 if (!ap_work_queue) {
1170 if (ap_select_domain() == 0)
1173 /* Setup the ap bus rescan timer. */
1174 init_timer(&ap_config_timer);
1175 ap_config_timer.function = ap_config_timeout;
1176 ap_config_timer.data = 0;
1177 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1178 add_timer(&ap_config_timer);
1180 /* Start the low priority AP bus poll thread. */
1181 if (ap_thread_flag) {
1182 rc = ap_poll_thread_start();
1190 del_timer_sync(&ap_config_timer);
1191 del_timer_sync(&ap_poll_timer);
1192 destroy_workqueue(ap_work_queue);
1194 s390_root_dev_unregister(ap_root_device);
1197 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1198 bus_unregister(&ap_bus_type);
1203 static int __ap_match_all(struct device *dev, void *data)
1209 * The module termination code
1211 void ap_module_exit(void)
1216 ap_poll_thread_stop();
1217 del_timer_sync(&ap_config_timer);
1218 del_timer_sync(&ap_poll_timer);
1219 destroy_workqueue(ap_work_queue);
1220 s390_root_dev_unregister(ap_root_device);
1221 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1224 device_unregister(dev);
1227 for (i = 0; ap_bus_attrs[i]; i++)
1228 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1229 bus_unregister(&ap_bus_type);
1232 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1233 module_init(ap_module_init);
1234 module_exit(ap_module_exit);