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>
37 #include <linux/hrtimer.h>
38 #include <linux/ktime.h>
42 /* Some prototypes. */
43 static void ap_scan_bus(struct work_struct *);
44 static void ap_poll_all(unsigned long);
45 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
46 static int ap_poll_thread_start(void);
47 static void ap_poll_thread_stop(void);
48 static void ap_request_timeout(unsigned long);
53 MODULE_AUTHOR("IBM Corporation");
54 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
55 "Copyright 2006 IBM Corporation");
56 MODULE_LICENSE("GPL");
61 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
62 module_param_named(domain, ap_domain_index, int, 0000);
63 MODULE_PARM_DESC(domain, "domain index for ap devices");
64 EXPORT_SYMBOL(ap_domain_index);
66 static int ap_thread_flag = 0;
67 module_param_named(poll_thread, ap_thread_flag, int, 0000);
68 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
70 static struct device *ap_root_device = NULL;
71 static DEFINE_SPINLOCK(ap_device_lock);
72 static LIST_HEAD(ap_device_list);
75 * Workqueue & timer for bus rescan.
77 static struct workqueue_struct *ap_work_queue;
78 static struct timer_list ap_config_timer;
79 static int ap_config_time = AP_CONFIG_TIME;
80 static DECLARE_WORK(ap_config_work, ap_scan_bus);
83 * Tasklet & timer for AP request polling.
85 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
86 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
87 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
88 static struct task_struct *ap_poll_kthread = NULL;
89 static DEFINE_MUTEX(ap_poll_thread_mutex);
90 static struct hrtimer ap_poll_timer;
91 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
92 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
93 static unsigned long long poll_timeout = 250000;
96 * ap_intructions_available() - Test if AP instructions are available.
98 * Returns 0 if the AP instructions are installed.
100 static inline int ap_instructions_available(void)
102 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
103 register unsigned long reg1 asm ("1") = -ENODEV;
104 register unsigned long reg2 asm ("2") = 0UL;
107 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
111 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
116 * ap_test_queue(): Test adjunct processor queue.
117 * @qid: The AP queue number
118 * @queue_depth: Pointer to queue depth value
119 * @device_type: Pointer to device type value
121 * Returns AP queue status structure.
123 static inline struct ap_queue_status
124 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
126 register unsigned long reg0 asm ("0") = qid;
127 register struct ap_queue_status reg1 asm ("1");
128 register unsigned long reg2 asm ("2") = 0UL;
130 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
131 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
132 *device_type = (int) (reg2 >> 24);
133 *queue_depth = (int) (reg2 & 0xff);
138 * ap_reset_queue(): Reset adjunct processor queue.
139 * @qid: The AP queue number
141 * Returns AP queue status structure.
143 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
145 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
146 register struct ap_queue_status reg1 asm ("1");
147 register unsigned long reg2 asm ("2") = 0UL;
150 ".long 0xb2af0000" /* PQAP(RAPQ) */
151 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
156 * __ap_send(): Send message to adjunct processor queue.
157 * @qid: The AP queue number
158 * @psmid: The program supplied message identifier
159 * @msg: The message text
160 * @length: The message length
162 * Returns AP queue status structure.
163 * Condition code 1 on NQAP can't happen because the L bit is 1.
164 * Condition code 2 on NQAP also means the send is incomplete,
165 * because a segment boundary was reached. The NQAP is repeated.
167 static inline struct ap_queue_status
168 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
170 typedef struct { char _[length]; } msgblock;
171 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
172 register struct ap_queue_status reg1 asm ("1");
173 register unsigned long reg2 asm ("2") = (unsigned long) msg;
174 register unsigned long reg3 asm ("3") = (unsigned long) length;
175 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
176 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
179 "0: .long 0xb2ad0042\n" /* DQAP */
181 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
182 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
187 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
189 struct ap_queue_status status;
191 status = __ap_send(qid, psmid, msg, length);
192 switch (status.response_code) {
193 case AP_RESPONSE_NORMAL:
195 case AP_RESPONSE_Q_FULL:
196 case AP_RESPONSE_RESET_IN_PROGRESS:
198 default: /* Device is gone. */
202 EXPORT_SYMBOL(ap_send);
205 * __ap_recv(): Receive message from adjunct processor queue.
206 * @qid: The AP queue number
207 * @psmid: Pointer to program supplied message identifier
208 * @msg: The message text
209 * @length: The message length
211 * Returns AP queue status structure.
212 * Condition code 1 on DQAP means the receive has taken place
213 * but only partially. The response is incomplete, hence the
215 * Condition code 2 on DQAP also means the receive is incomplete,
216 * this time because a segment boundary was reached. Again, the
218 * Note that gpr2 is used by the DQAP instruction to keep track of
219 * any 'residual' length, in case the instruction gets interrupted.
220 * Hence it gets zeroed before the instruction.
222 static inline struct ap_queue_status
223 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
225 typedef struct { char _[length]; } msgblock;
226 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
227 register struct ap_queue_status reg1 asm ("1");
228 register unsigned long reg2 asm("2") = 0UL;
229 register unsigned long reg4 asm("4") = (unsigned long) msg;
230 register unsigned long reg5 asm("5") = (unsigned long) length;
231 register unsigned long reg6 asm("6") = 0UL;
232 register unsigned long reg7 asm("7") = 0UL;
236 "0: .long 0xb2ae0064\n"
238 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
239 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
240 "=m" (*(msgblock *) msg) : : "cc" );
241 *psmid = (((unsigned long long) reg6) << 32) + reg7;
245 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
247 struct ap_queue_status status;
249 status = __ap_recv(qid, psmid, msg, length);
250 switch (status.response_code) {
251 case AP_RESPONSE_NORMAL:
253 case AP_RESPONSE_NO_PENDING_REPLY:
254 if (status.queue_empty)
257 case AP_RESPONSE_RESET_IN_PROGRESS:
263 EXPORT_SYMBOL(ap_recv);
266 * ap_query_queue(): Check if an AP queue is available.
267 * @qid: The AP queue number
268 * @queue_depth: Pointer to queue depth value
269 * @device_type: Pointer to device type value
271 * The test is repeated for AP_MAX_RESET times.
273 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
275 struct ap_queue_status status;
276 int t_depth, t_device_type, rc, i;
279 for (i = 0; i < AP_MAX_RESET; i++) {
280 status = ap_test_queue(qid, &t_depth, &t_device_type);
281 switch (status.response_code) {
282 case AP_RESPONSE_NORMAL:
283 *queue_depth = t_depth + 1;
284 *device_type = t_device_type;
287 case AP_RESPONSE_Q_NOT_AVAIL:
290 case AP_RESPONSE_RESET_IN_PROGRESS:
292 case AP_RESPONSE_DECONFIGURED:
295 case AP_RESPONSE_CHECKSTOPPED:
298 case AP_RESPONSE_BUSY:
305 if (i < AP_MAX_RESET - 1)
312 * ap_init_queue(): Reset an AP queue.
313 * @qid: The AP queue number
315 * Reset an AP queue and wait for it to become available again.
317 static int ap_init_queue(ap_qid_t qid)
319 struct ap_queue_status status;
323 status = ap_reset_queue(qid);
324 for (i = 0; i < AP_MAX_RESET; i++) {
325 switch (status.response_code) {
326 case AP_RESPONSE_NORMAL:
327 if (status.queue_empty)
330 case AP_RESPONSE_Q_NOT_AVAIL:
331 case AP_RESPONSE_DECONFIGURED:
332 case AP_RESPONSE_CHECKSTOPPED:
333 i = AP_MAX_RESET; /* return with -ENODEV */
335 case AP_RESPONSE_RESET_IN_PROGRESS:
337 case AP_RESPONSE_BUSY:
341 if (rc != -ENODEV && rc != -EBUSY)
343 if (i < AP_MAX_RESET - 1) {
345 status = ap_test_queue(qid, &dummy, &dummy);
352 * ap_increase_queue_count(): Arm request timeout.
353 * @ap_dev: Pointer to an AP device.
355 * Arm request timeout if an AP device was idle and a new request is submitted.
357 static void ap_increase_queue_count(struct ap_device *ap_dev)
359 int timeout = ap_dev->drv->request_timeout;
361 ap_dev->queue_count++;
362 if (ap_dev->queue_count == 1) {
363 mod_timer(&ap_dev->timeout, jiffies + timeout);
364 ap_dev->reset = AP_RESET_ARMED;
369 * ap_decrease_queue_count(): Decrease queue count.
370 * @ap_dev: Pointer to an AP device.
372 * If AP device is still alive, re-schedule request timeout if there are still
375 static void ap_decrease_queue_count(struct ap_device *ap_dev)
377 int timeout = ap_dev->drv->request_timeout;
379 ap_dev->queue_count--;
380 if (ap_dev->queue_count > 0)
381 mod_timer(&ap_dev->timeout, jiffies + timeout);
384 * The timeout timer should to be disabled now - since
385 * del_timer_sync() is very expensive, we just tell via the
386 * reset flag to ignore the pending timeout timer.
388 ap_dev->reset = AP_RESET_IGNORE;
392 * AP device related attributes.
394 static ssize_t ap_hwtype_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
397 struct ap_device *ap_dev = to_ap_dev(dev);
398 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
400 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
402 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
405 struct ap_device *ap_dev = to_ap_dev(dev);
406 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
408 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
410 static ssize_t ap_request_count_show(struct device *dev,
411 struct device_attribute *attr,
414 struct ap_device *ap_dev = to_ap_dev(dev);
417 spin_lock_bh(&ap_dev->lock);
418 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
419 spin_unlock_bh(&ap_dev->lock);
423 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
425 static ssize_t ap_modalias_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
431 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
433 static struct attribute *ap_dev_attrs[] = {
434 &dev_attr_hwtype.attr,
435 &dev_attr_depth.attr,
436 &dev_attr_request_count.attr,
437 &dev_attr_modalias.attr,
440 static struct attribute_group ap_dev_attr_group = {
441 .attrs = ap_dev_attrs
446 * @dev: Pointer to device
447 * @drv: Pointer to device_driver
449 * AP bus driver registration/unregistration.
451 static int ap_bus_match(struct device *dev, struct device_driver *drv)
453 struct ap_device *ap_dev = to_ap_dev(dev);
454 struct ap_driver *ap_drv = to_ap_drv(drv);
455 struct ap_device_id *id;
458 * Compare device type of the device with the list of
459 * supported types of the device_driver.
461 for (id = ap_drv->ids; id->match_flags; id++) {
462 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
463 (id->dev_type != ap_dev->device_type))
471 * ap_uevent(): Uevent function for AP devices.
472 * @dev: Pointer to device
473 * @env: Pointer to kobj_uevent_env
475 * It sets up a single environment variable DEV_TYPE which contains the
476 * hardware device type.
478 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
480 struct ap_device *ap_dev = to_ap_dev(dev);
486 /* Set up DEV_TYPE environment variable. */
487 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
492 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
497 static struct bus_type ap_bus_type = {
499 .match = &ap_bus_match,
500 .uevent = &ap_uevent,
503 static int ap_device_probe(struct device *dev)
505 struct ap_device *ap_dev = to_ap_dev(dev);
506 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
509 ap_dev->drv = ap_drv;
510 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
512 spin_lock_bh(&ap_device_lock);
513 list_add(&ap_dev->list, &ap_device_list);
514 spin_unlock_bh(&ap_device_lock);
520 * __ap_flush_queue(): Flush requests.
521 * @ap_dev: Pointer to the AP device
523 * Flush all requests from the request/pending queue of an AP device.
525 static void __ap_flush_queue(struct ap_device *ap_dev)
527 struct ap_message *ap_msg, *next;
529 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
530 list_del_init(&ap_msg->list);
531 ap_dev->pendingq_count--;
532 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
534 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
535 list_del_init(&ap_msg->list);
536 ap_dev->requestq_count--;
537 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
541 void ap_flush_queue(struct ap_device *ap_dev)
543 spin_lock_bh(&ap_dev->lock);
544 __ap_flush_queue(ap_dev);
545 spin_unlock_bh(&ap_dev->lock);
547 EXPORT_SYMBOL(ap_flush_queue);
549 static int ap_device_remove(struct device *dev)
551 struct ap_device *ap_dev = to_ap_dev(dev);
552 struct ap_driver *ap_drv = ap_dev->drv;
554 ap_flush_queue(ap_dev);
555 del_timer_sync(&ap_dev->timeout);
556 spin_lock_bh(&ap_device_lock);
557 list_del_init(&ap_dev->list);
558 spin_unlock_bh(&ap_device_lock);
560 ap_drv->remove(ap_dev);
561 spin_lock_bh(&ap_dev->lock);
562 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
563 spin_unlock_bh(&ap_dev->lock);
567 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
570 struct device_driver *drv = &ap_drv->driver;
572 drv->bus = &ap_bus_type;
573 drv->probe = ap_device_probe;
574 drv->remove = ap_device_remove;
577 return driver_register(drv);
579 EXPORT_SYMBOL(ap_driver_register);
581 void ap_driver_unregister(struct ap_driver *ap_drv)
583 driver_unregister(&ap_drv->driver);
585 EXPORT_SYMBOL(ap_driver_unregister);
590 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
592 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
595 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
597 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
599 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
602 static ssize_t ap_config_time_store(struct bus_type *bus,
603 const char *buf, size_t count)
607 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
609 ap_config_time = time;
610 if (!timer_pending(&ap_config_timer) ||
611 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
612 ap_config_timer.expires = jiffies + ap_config_time * HZ;
613 add_timer(&ap_config_timer);
618 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
620 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
622 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
625 static ssize_t ap_poll_thread_store(struct bus_type *bus,
626 const char *buf, size_t count)
630 if (sscanf(buf, "%d\n", &flag) != 1)
633 rc = ap_poll_thread_start();
638 ap_poll_thread_stop();
642 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
644 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
646 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
649 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
652 unsigned long long time;
655 /* 120 seconds = maximum poll interval */
656 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 || time > 120000000000)
659 hr_time = ktime_set(0, poll_timeout);
661 if (!hrtimer_is_queued(&ap_poll_timer) ||
662 !hrtimer_forward(&ap_poll_timer, ap_poll_timer.expires, hr_time)) {
663 ap_poll_timer.expires = hr_time;
664 hrtimer_start(&ap_poll_timer, hr_time, HRTIMER_MODE_ABS);
669 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
671 static struct bus_attribute *const ap_bus_attrs[] = {
673 &bus_attr_config_time,
674 &bus_attr_poll_thread,
675 &bus_attr_poll_timeout,
680 * ap_select_domain(): Select an AP domain.
682 * Pick one of the 16 AP domains.
684 static int ap_select_domain(void)
686 int queue_depth, device_type, count, max_count, best_domain;
690 * We want to use a single domain. Either the one specified with
691 * the "domain=" parameter or the domain with the maximum number
694 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
695 /* Domain has already been selected. */
699 for (i = 0; i < AP_DOMAINS; i++) {
701 for (j = 0; j < AP_DEVICES; j++) {
702 ap_qid_t qid = AP_MKQID(j, i);
703 rc = ap_query_queue(qid, &queue_depth, &device_type);
708 if (count > max_count) {
713 if (best_domain >= 0){
714 ap_domain_index = best_domain;
721 * ap_probe_device_type(): Find the device type of an AP.
722 * @ap_dev: pointer to the AP device.
724 * Find the device type if query queue returned a device type of 0.
726 static int ap_probe_device_type(struct ap_device *ap_dev)
728 static unsigned char msg[] = {
729 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
730 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
731 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
732 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
733 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
734 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
735 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
736 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
737 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
738 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
739 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
740 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
741 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
742 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
743 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
744 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
745 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
746 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
747 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
748 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
749 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
750 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
751 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
752 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
753 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
754 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
755 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
756 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
757 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
758 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
759 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
760 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
761 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
762 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
763 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
764 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
765 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
766 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
767 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
768 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
769 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
770 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
771 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
772 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
773 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
775 struct ap_queue_status status;
776 unsigned long long psmid;
780 reply = (void *) get_zeroed_page(GFP_KERNEL);
786 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
788 if (status.response_code != AP_RESPONSE_NORMAL) {
793 /* Wait for the test message to complete. */
794 for (i = 0; i < 6; i++) {
796 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
797 if (status.response_code == AP_RESPONSE_NORMAL &&
798 psmid == 0x0102030405060708ULL)
803 if (reply[0] == 0x00 && reply[1] == 0x86)
804 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
806 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
812 free_page((unsigned long) reply);
818 * __ap_scan_bus(): Scan the AP bus.
819 * @dev: Pointer to device
820 * @data: Pointer to data
822 * Scan the AP bus for new devices.
824 static int __ap_scan_bus(struct device *dev, void *data)
826 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
829 static void ap_device_release(struct device *dev)
831 struct ap_device *ap_dev = to_ap_dev(dev);
836 static void ap_scan_bus(struct work_struct *unused)
838 struct ap_device *ap_dev;
841 int queue_depth, device_type;
844 if (ap_select_domain() != 0)
846 for (i = 0; i < AP_DEVICES; i++) {
847 qid = AP_MKQID(i, ap_domain_index);
848 dev = bus_find_device(&ap_bus_type, NULL,
849 (void *)(unsigned long)qid,
851 rc = ap_query_queue(qid, &queue_depth, &device_type);
854 set_current_state(TASK_UNINTERRUPTIBLE);
855 schedule_timeout(AP_RESET_TIMEOUT);
856 rc = ap_query_queue(qid, &queue_depth,
859 ap_dev = to_ap_dev(dev);
860 spin_lock_bh(&ap_dev->lock);
861 if (rc || ap_dev->unregistered) {
862 spin_unlock_bh(&ap_dev->lock);
863 device_unregister(dev);
867 spin_unlock_bh(&ap_dev->lock);
873 rc = ap_init_queue(qid);
876 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
880 ap_dev->queue_depth = queue_depth;
881 ap_dev->unregistered = 1;
882 spin_lock_init(&ap_dev->lock);
883 INIT_LIST_HEAD(&ap_dev->pendingq);
884 INIT_LIST_HEAD(&ap_dev->requestq);
885 INIT_LIST_HEAD(&ap_dev->list);
886 setup_timer(&ap_dev->timeout, ap_request_timeout,
887 (unsigned long) ap_dev);
888 if (device_type == 0)
889 ap_probe_device_type(ap_dev);
891 ap_dev->device_type = device_type;
893 ap_dev->device.bus = &ap_bus_type;
894 ap_dev->device.parent = ap_root_device;
895 dev_set_name(&ap_dev->device, "card%02x",
896 AP_QID_DEVICE(ap_dev->qid));
897 ap_dev->device.release = ap_device_release;
898 rc = device_register(&ap_dev->device);
903 /* Add device attributes. */
904 rc = sysfs_create_group(&ap_dev->device.kobj,
907 spin_lock_bh(&ap_dev->lock);
908 ap_dev->unregistered = 0;
909 spin_unlock_bh(&ap_dev->lock);
912 device_unregister(&ap_dev->device);
917 ap_config_timeout(unsigned long ptr)
919 queue_work(ap_work_queue, &ap_config_work);
920 ap_config_timer.expires = jiffies + ap_config_time * HZ;
921 add_timer(&ap_config_timer);
925 * ap_schedule_poll_timer(): Schedule poll timer.
927 * Set up the timer to run the poll tasklet
929 static inline void ap_schedule_poll_timer(void)
931 if (hrtimer_is_queued(&ap_poll_timer))
933 hrtimer_start(&ap_poll_timer, ktime_set(0, poll_timeout),
938 * ap_poll_read(): Receive pending reply messages from an AP device.
939 * @ap_dev: pointer to the AP device
940 * @flags: pointer to control flags, bit 2^0 is set if another poll is
941 * required, bit 2^1 is set if the poll timer needs to get armed
943 * Returns 0 if the device is still present, -ENODEV if not.
945 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
947 struct ap_queue_status status;
948 struct ap_message *ap_msg;
950 if (ap_dev->queue_count <= 0)
952 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
953 ap_dev->reply->message, ap_dev->reply->length);
954 switch (status.response_code) {
955 case AP_RESPONSE_NORMAL:
956 atomic_dec(&ap_poll_requests);
957 ap_decrease_queue_count(ap_dev);
958 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
959 if (ap_msg->psmid != ap_dev->reply->psmid)
961 list_del_init(&ap_msg->list);
962 ap_dev->pendingq_count--;
963 ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
966 if (ap_dev->queue_count > 0)
969 case AP_RESPONSE_NO_PENDING_REPLY:
970 if (status.queue_empty) {
971 /* The card shouldn't forget requests but who knows. */
972 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
973 ap_dev->queue_count = 0;
974 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
975 ap_dev->requestq_count += ap_dev->pendingq_count;
976 ap_dev->pendingq_count = 0;
987 * ap_poll_write(): Send messages from the request queue to an AP device.
988 * @ap_dev: pointer to the AP device
989 * @flags: pointer to control flags, bit 2^0 is set if another poll is
990 * required, bit 2^1 is set if the poll timer needs to get armed
992 * Returns 0 if the device is still present, -ENODEV if not.
994 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
996 struct ap_queue_status status;
997 struct ap_message *ap_msg;
999 if (ap_dev->requestq_count <= 0 ||
1000 ap_dev->queue_count >= ap_dev->queue_depth)
1002 /* Start the next request on the queue. */
1003 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1004 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1005 ap_msg->message, ap_msg->length);
1006 switch (status.response_code) {
1007 case AP_RESPONSE_NORMAL:
1008 atomic_inc(&ap_poll_requests);
1009 ap_increase_queue_count(ap_dev);
1010 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1011 ap_dev->requestq_count--;
1012 ap_dev->pendingq_count++;
1013 if (ap_dev->queue_count < ap_dev->queue_depth &&
1014 ap_dev->requestq_count > 0)
1018 case AP_RESPONSE_Q_FULL:
1019 case AP_RESPONSE_RESET_IN_PROGRESS:
1022 case AP_RESPONSE_MESSAGE_TOO_BIG:
1031 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1032 * @ap_dev: pointer to the bus device
1033 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1034 * required, bit 2^1 is set if the poll timer needs to get armed
1036 * Poll AP device for pending replies and send new messages. If either
1037 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1040 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1044 rc = ap_poll_read(ap_dev, flags);
1047 return ap_poll_write(ap_dev, flags);
1051 * __ap_queue_message(): Queue a message to a device.
1052 * @ap_dev: pointer to the AP device
1053 * @ap_msg: the message to be queued
1055 * Queue a message to a device. Returns 0 if successful.
1057 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1059 struct ap_queue_status status;
1061 if (list_empty(&ap_dev->requestq) &&
1062 ap_dev->queue_count < ap_dev->queue_depth) {
1063 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1064 ap_msg->message, ap_msg->length);
1065 switch (status.response_code) {
1066 case AP_RESPONSE_NORMAL:
1067 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1068 atomic_inc(&ap_poll_requests);
1069 ap_dev->pendingq_count++;
1070 ap_increase_queue_count(ap_dev);
1071 ap_dev->total_request_count++;
1073 case AP_RESPONSE_Q_FULL:
1074 case AP_RESPONSE_RESET_IN_PROGRESS:
1075 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1076 ap_dev->requestq_count++;
1077 ap_dev->total_request_count++;
1079 case AP_RESPONSE_MESSAGE_TOO_BIG:
1080 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1082 default: /* Device is gone. */
1083 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1087 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1088 ap_dev->requestq_count++;
1089 ap_dev->total_request_count++;
1092 ap_schedule_poll_timer();
1096 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1098 unsigned long flags;
1101 spin_lock_bh(&ap_dev->lock);
1102 if (!ap_dev->unregistered) {
1103 /* Make room on the queue by polling for finished requests. */
1104 rc = ap_poll_queue(ap_dev, &flags);
1106 rc = __ap_queue_message(ap_dev, ap_msg);
1108 wake_up(&ap_poll_wait);
1110 ap_dev->unregistered = 1;
1112 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1115 spin_unlock_bh(&ap_dev->lock);
1117 device_unregister(&ap_dev->device);
1119 EXPORT_SYMBOL(ap_queue_message);
1122 * ap_cancel_message(): Cancel a crypto request.
1123 * @ap_dev: The AP device that has the message queued
1124 * @ap_msg: The message that is to be removed
1126 * Cancel a crypto request. This is done by removing the request
1127 * from the device pending or request queue. Note that the
1128 * request stays on the AP queue. When it finishes the message
1129 * reply will be discarded because the psmid can't be found.
1131 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1133 struct ap_message *tmp;
1135 spin_lock_bh(&ap_dev->lock);
1136 if (!list_empty(&ap_msg->list)) {
1137 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1138 if (tmp->psmid == ap_msg->psmid) {
1139 ap_dev->pendingq_count--;
1142 ap_dev->requestq_count--;
1144 list_del_init(&ap_msg->list);
1146 spin_unlock_bh(&ap_dev->lock);
1148 EXPORT_SYMBOL(ap_cancel_message);
1151 * ap_poll_timeout(): AP receive polling for finished AP requests.
1152 * @unused: Unused pointer.
1154 * Schedules the AP tasklet using a high resolution timer.
1156 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1158 tasklet_schedule(&ap_tasklet);
1159 return HRTIMER_NORESTART;
1163 * ap_reset(): Reset a not responding AP device.
1164 * @ap_dev: Pointer to the AP device
1166 * Reset a not responding AP device and move all requests from the
1167 * pending queue to the request queue.
1169 static void ap_reset(struct ap_device *ap_dev)
1173 ap_dev->reset = AP_RESET_IGNORE;
1174 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1175 ap_dev->queue_count = 0;
1176 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1177 ap_dev->requestq_count += ap_dev->pendingq_count;
1178 ap_dev->pendingq_count = 0;
1179 rc = ap_init_queue(ap_dev->qid);
1181 ap_dev->unregistered = 1;
1184 static int __ap_poll_all(struct ap_device *ap_dev, unsigned long *flags)
1186 spin_lock(&ap_dev->lock);
1187 if (!ap_dev->unregistered) {
1188 if (ap_poll_queue(ap_dev, flags))
1189 ap_dev->unregistered = 1;
1190 if (ap_dev->reset == AP_RESET_DO)
1193 spin_unlock(&ap_dev->lock);
1198 * ap_poll_all(): Poll all AP devices.
1199 * @dummy: Unused variable
1201 * Poll all AP devices on the bus in a round robin fashion. Continue
1202 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1203 * of the control flags has been set arm the poll timer.
1205 static void ap_poll_all(unsigned long dummy)
1207 unsigned long flags;
1208 struct ap_device *ap_dev;
1212 spin_lock(&ap_device_lock);
1213 list_for_each_entry(ap_dev, &ap_device_list, list) {
1214 __ap_poll_all(ap_dev, &flags);
1216 spin_unlock(&ap_device_lock);
1217 } while (flags & 1);
1219 ap_schedule_poll_timer();
1223 * ap_poll_thread(): Thread that polls for finished requests.
1224 * @data: Unused pointer
1226 * AP bus poll thread. The purpose of this thread is to poll for
1227 * finished requests in a loop if there is a "free" cpu - that is
1228 * a cpu that doesn't have anything better to do. The polling stops
1229 * as soon as there is another task or if all messages have been
1232 static int ap_poll_thread(void *data)
1234 DECLARE_WAITQUEUE(wait, current);
1235 unsigned long flags;
1237 struct ap_device *ap_dev;
1239 set_user_nice(current, 19);
1241 if (need_resched()) {
1245 add_wait_queue(&ap_poll_wait, &wait);
1246 set_current_state(TASK_INTERRUPTIBLE);
1247 if (kthread_should_stop())
1249 requests = atomic_read(&ap_poll_requests);
1252 set_current_state(TASK_RUNNING);
1253 remove_wait_queue(&ap_poll_wait, &wait);
1256 spin_lock_bh(&ap_device_lock);
1257 list_for_each_entry(ap_dev, &ap_device_list, list) {
1258 __ap_poll_all(ap_dev, &flags);
1260 spin_unlock_bh(&ap_device_lock);
1262 set_current_state(TASK_RUNNING);
1263 remove_wait_queue(&ap_poll_wait, &wait);
1267 static int ap_poll_thread_start(void)
1271 mutex_lock(&ap_poll_thread_mutex);
1272 if (!ap_poll_kthread) {
1273 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1274 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1276 ap_poll_kthread = NULL;
1280 mutex_unlock(&ap_poll_thread_mutex);
1284 static void ap_poll_thread_stop(void)
1286 mutex_lock(&ap_poll_thread_mutex);
1287 if (ap_poll_kthread) {
1288 kthread_stop(ap_poll_kthread);
1289 ap_poll_kthread = NULL;
1291 mutex_unlock(&ap_poll_thread_mutex);
1295 * ap_request_timeout(): Handling of request timeouts
1296 * @data: Holds the AP device.
1298 * Handles request timeouts.
1300 static void ap_request_timeout(unsigned long data)
1302 struct ap_device *ap_dev = (struct ap_device *) data;
1304 if (ap_dev->reset == AP_RESET_ARMED)
1305 ap_dev->reset = AP_RESET_DO;
1308 static void ap_reset_domain(void)
1312 if (ap_domain_index != -1)
1313 for (i = 0; i < AP_DEVICES; i++)
1314 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1317 static void ap_reset_all(void)
1321 for (i = 0; i < AP_DOMAINS; i++)
1322 for (j = 0; j < AP_DEVICES; j++)
1323 ap_reset_queue(AP_MKQID(j, i));
1326 static struct reset_call ap_reset_call = {
1331 * ap_module_init(): The module initialization code.
1333 * Initializes the module.
1335 int __init ap_module_init(void)
1339 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1340 printk(KERN_WARNING "Invalid param: domain = %d. "
1341 " Not loading.\n", ap_domain_index);
1344 if (ap_instructions_available() != 0) {
1345 printk(KERN_WARNING "AP instructions not installed.\n");
1348 register_reset_call(&ap_reset_call);
1350 /* Create /sys/bus/ap. */
1351 rc = bus_register(&ap_bus_type);
1354 for (i = 0; ap_bus_attrs[i]; i++) {
1355 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1360 /* Create /sys/devices/ap. */
1361 ap_root_device = s390_root_dev_register("ap");
1362 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1366 ap_work_queue = create_singlethread_workqueue("kapwork");
1367 if (!ap_work_queue) {
1372 if (ap_select_domain() == 0)
1375 /* Setup the AP bus rescan timer. */
1376 init_timer(&ap_config_timer);
1377 ap_config_timer.function = ap_config_timeout;
1378 ap_config_timer.data = 0;
1379 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1380 add_timer(&ap_config_timer);
1382 /* Setup the high resultion poll timer.
1383 * If we are running under z/VM adjust polling to z/VM polling rate.
1386 poll_timeout = 1500000;
1387 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1388 ap_poll_timer.function = ap_poll_timeout;
1390 /* Start the low priority AP bus poll thread. */
1391 if (ap_thread_flag) {
1392 rc = ap_poll_thread_start();
1400 del_timer_sync(&ap_config_timer);
1401 hrtimer_cancel(&ap_poll_timer);
1402 destroy_workqueue(ap_work_queue);
1404 s390_root_dev_unregister(ap_root_device);
1407 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1408 bus_unregister(&ap_bus_type);
1410 unregister_reset_call(&ap_reset_call);
1414 static int __ap_match_all(struct device *dev, void *data)
1420 * ap_modules_exit(): The module termination code
1422 * Terminates the module.
1424 void ap_module_exit(void)
1430 ap_poll_thread_stop();
1431 del_timer_sync(&ap_config_timer);
1432 hrtimer_cancel(&ap_poll_timer);
1433 destroy_workqueue(ap_work_queue);
1434 tasklet_kill(&ap_tasklet);
1435 s390_root_dev_unregister(ap_root_device);
1436 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1439 device_unregister(dev);
1442 for (i = 0; ap_bus_attrs[i]; i++)
1443 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1444 bus_unregister(&ap_bus_type);
1445 unregister_reset_call(&ap_reset_call);
1448 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1449 module_init(ap_module_init);
1450 module_exit(ap_module_exit);