2 * linux/drivers/s390/crypto/ap_bus.c
4 * Copyright (C) 2006 IBM Corporation
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Ralph Wuerthner <rwuerthn@de.ibm.com>
8 * Felix Beck <felix.beck@de.ibm.com>
10 * Adjunct processor bus.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define KMSG_COMPONENT "ap"
28 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/notifier.h>
37 #include <linux/kthread.h>
38 #include <linux/mutex.h>
39 #include <asm/reset.h>
41 #include <asm/atomic.h>
42 #include <asm/system.h>
44 #include <linux/hrtimer.h>
45 #include <linux/ktime.h>
49 /* Some prototypes. */
50 static void ap_scan_bus(struct work_struct *);
51 static void ap_poll_all(unsigned long);
52 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
53 static int ap_poll_thread_start(void);
54 static void ap_poll_thread_stop(void);
55 static void ap_request_timeout(unsigned long);
56 static inline void ap_schedule_poll_timer(void);
57 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
58 static int ap_device_remove(struct device *dev);
59 static int ap_device_probe(struct device *dev);
60 static void ap_interrupt_handler(void *unused1, void *unused2);
61 static void ap_reset(struct ap_device *ap_dev);
62 static void ap_config_timeout(unsigned long ptr);
67 MODULE_AUTHOR("IBM Corporation");
68 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
69 "Copyright 2006 IBM Corporation");
70 MODULE_LICENSE("GPL");
75 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
76 module_param_named(domain, ap_domain_index, int, 0000);
77 MODULE_PARM_DESC(domain, "domain index for ap devices");
78 EXPORT_SYMBOL(ap_domain_index);
80 static int ap_thread_flag = 0;
81 module_param_named(poll_thread, ap_thread_flag, int, 0000);
82 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
84 static struct device *ap_root_device = NULL;
85 static DEFINE_SPINLOCK(ap_device_list_lock);
86 static LIST_HEAD(ap_device_list);
89 * Workqueue & timer for bus rescan.
91 static struct workqueue_struct *ap_work_queue;
92 static struct timer_list ap_config_timer;
93 static int ap_config_time = AP_CONFIG_TIME;
94 static DECLARE_WORK(ap_config_work, ap_scan_bus);
97 * Tasklet & timer for AP request polling and interrupts
99 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
100 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
101 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
102 static struct task_struct *ap_poll_kthread = NULL;
103 static DEFINE_MUTEX(ap_poll_thread_mutex);
104 static void *ap_interrupt_indicator;
105 static struct hrtimer ap_poll_timer;
106 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
107 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
108 static unsigned long long poll_timeout = 250000;
111 static int ap_suspend_flag;
112 static struct bus_type ap_bus_type;
115 * ap_using_interrupts() - Returns non-zero if interrupt support is
118 static inline int ap_using_interrupts(void)
120 return ap_interrupt_indicator != NULL;
124 * ap_intructions_available() - Test if AP instructions are available.
126 * Returns 0 if the AP instructions are installed.
128 static inline int ap_instructions_available(void)
130 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
131 register unsigned long reg1 asm ("1") = -ENODEV;
132 register unsigned long reg2 asm ("2") = 0UL;
135 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
139 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
144 * ap_interrupts_available(): Test if AP interrupts are available.
146 * Returns 1 if AP interrupts are available.
148 static int ap_interrupts_available(void)
150 unsigned long long facility_bits[2];
152 if (stfle(facility_bits, 2) <= 1)
154 if (!(facility_bits[0] & (1ULL << 61)) ||
155 !(facility_bits[1] & (1ULL << 62)))
161 * ap_test_queue(): Test adjunct processor queue.
162 * @qid: The AP queue number
163 * @queue_depth: Pointer to queue depth value
164 * @device_type: Pointer to device type value
166 * Returns AP queue status structure.
168 static inline struct ap_queue_status
169 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
171 register unsigned long reg0 asm ("0") = qid;
172 register struct ap_queue_status reg1 asm ("1");
173 register unsigned long reg2 asm ("2") = 0UL;
175 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
176 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
177 *device_type = (int) (reg2 >> 24);
178 *queue_depth = (int) (reg2 & 0xff);
183 * ap_reset_queue(): Reset adjunct processor queue.
184 * @qid: The AP queue number
186 * Returns AP queue status structure.
188 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
190 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
191 register struct ap_queue_status reg1 asm ("1");
192 register unsigned long reg2 asm ("2") = 0UL;
195 ".long 0xb2af0000" /* PQAP(RAPQ) */
196 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
202 * ap_queue_interruption_control(): Enable interruption for a specific AP.
203 * @qid: The AP queue number
204 * @ind: The notification indicator byte
206 * Returns AP queue status.
208 static inline struct ap_queue_status
209 ap_queue_interruption_control(ap_qid_t qid, void *ind)
211 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
212 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
213 register struct ap_queue_status reg1_out asm ("1");
214 register void *reg2 asm ("2") = ind;
216 ".long 0xb2af0000" /* PQAP(RAPQ) */
217 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
225 * ap_queue_enable_interruption(): Enable interruption on an AP.
226 * @qid: The AP queue number
227 * @ind: the notification indicator byte
229 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
230 * on the return value it waits a while and tests the AP queue if interrupts
231 * have been switched on using ap_test_queue().
233 static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
236 struct ap_queue_status status;
237 int t_depth, t_device_type, rc, i;
240 status = ap_queue_interruption_control(qid, ind);
242 for (i = 0; i < AP_MAX_RESET; i++) {
243 switch (status.response_code) {
244 case AP_RESPONSE_NORMAL:
245 if (status.int_enabled)
248 case AP_RESPONSE_RESET_IN_PROGRESS:
249 case AP_RESPONSE_BUSY:
251 case AP_RESPONSE_Q_NOT_AVAIL:
252 case AP_RESPONSE_DECONFIGURED:
253 case AP_RESPONSE_CHECKSTOPPED:
254 case AP_RESPONSE_INVALID_ADDRESS:
256 case AP_RESPONSE_OTHERWISE_CHANGED:
257 if (status.int_enabled)
263 if (i < AP_MAX_RESET - 1) {
265 status = ap_test_queue(qid, &t_depth, &t_device_type);
275 * __ap_send(): Send message to adjunct processor queue.
276 * @qid: The AP queue number
277 * @psmid: The program supplied message identifier
278 * @msg: The message text
279 * @length: The message length
281 * Returns AP queue status structure.
282 * Condition code 1 on NQAP can't happen because the L bit is 1.
283 * Condition code 2 on NQAP also means the send is incomplete,
284 * because a segment boundary was reached. The NQAP is repeated.
286 static inline struct ap_queue_status
287 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
289 typedef struct { char _[length]; } msgblock;
290 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
291 register struct ap_queue_status reg1 asm ("1");
292 register unsigned long reg2 asm ("2") = (unsigned long) msg;
293 register unsigned long reg3 asm ("3") = (unsigned long) length;
294 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
295 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
298 "0: .long 0xb2ad0042\n" /* DQAP */
300 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
301 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
306 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
308 struct ap_queue_status status;
310 status = __ap_send(qid, psmid, msg, length);
311 switch (status.response_code) {
312 case AP_RESPONSE_NORMAL:
314 case AP_RESPONSE_Q_FULL:
315 case AP_RESPONSE_RESET_IN_PROGRESS:
317 default: /* Device is gone. */
321 EXPORT_SYMBOL(ap_send);
324 * __ap_recv(): Receive message from adjunct processor queue.
325 * @qid: The AP queue number
326 * @psmid: Pointer to program supplied message identifier
327 * @msg: The message text
328 * @length: The message length
330 * Returns AP queue status structure.
331 * Condition code 1 on DQAP means the receive has taken place
332 * but only partially. The response is incomplete, hence the
334 * Condition code 2 on DQAP also means the receive is incomplete,
335 * this time because a segment boundary was reached. Again, the
337 * Note that gpr2 is used by the DQAP instruction to keep track of
338 * any 'residual' length, in case the instruction gets interrupted.
339 * Hence it gets zeroed before the instruction.
341 static inline struct ap_queue_status
342 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
344 typedef struct { char _[length]; } msgblock;
345 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
346 register struct ap_queue_status reg1 asm ("1");
347 register unsigned long reg2 asm("2") = 0UL;
348 register unsigned long reg4 asm("4") = (unsigned long) msg;
349 register unsigned long reg5 asm("5") = (unsigned long) length;
350 register unsigned long reg6 asm("6") = 0UL;
351 register unsigned long reg7 asm("7") = 0UL;
355 "0: .long 0xb2ae0064\n"
357 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
358 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
359 "=m" (*(msgblock *) msg) : : "cc" );
360 *psmid = (((unsigned long long) reg6) << 32) + reg7;
364 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
366 struct ap_queue_status status;
368 status = __ap_recv(qid, psmid, msg, length);
369 switch (status.response_code) {
370 case AP_RESPONSE_NORMAL:
372 case AP_RESPONSE_NO_PENDING_REPLY:
373 if (status.queue_empty)
376 case AP_RESPONSE_RESET_IN_PROGRESS:
382 EXPORT_SYMBOL(ap_recv);
385 * ap_query_queue(): Check if an AP queue is available.
386 * @qid: The AP queue number
387 * @queue_depth: Pointer to queue depth value
388 * @device_type: Pointer to device type value
390 * The test is repeated for AP_MAX_RESET times.
392 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
394 struct ap_queue_status status;
395 int t_depth, t_device_type, rc, i;
398 for (i = 0; i < AP_MAX_RESET; i++) {
399 status = ap_test_queue(qid, &t_depth, &t_device_type);
400 switch (status.response_code) {
401 case AP_RESPONSE_NORMAL:
402 *queue_depth = t_depth + 1;
403 *device_type = t_device_type;
406 case AP_RESPONSE_Q_NOT_AVAIL:
409 case AP_RESPONSE_RESET_IN_PROGRESS:
411 case AP_RESPONSE_DECONFIGURED:
414 case AP_RESPONSE_CHECKSTOPPED:
417 case AP_RESPONSE_INVALID_ADDRESS:
420 case AP_RESPONSE_OTHERWISE_CHANGED:
422 case AP_RESPONSE_BUSY:
429 if (i < AP_MAX_RESET - 1)
436 * ap_init_queue(): Reset an AP queue.
437 * @qid: The AP queue number
439 * Reset an AP queue and wait for it to become available again.
441 static int ap_init_queue(ap_qid_t qid)
443 struct ap_queue_status status;
447 status = ap_reset_queue(qid);
448 for (i = 0; i < AP_MAX_RESET; i++) {
449 switch (status.response_code) {
450 case AP_RESPONSE_NORMAL:
451 if (status.queue_empty)
454 case AP_RESPONSE_Q_NOT_AVAIL:
455 case AP_RESPONSE_DECONFIGURED:
456 case AP_RESPONSE_CHECKSTOPPED:
457 i = AP_MAX_RESET; /* return with -ENODEV */
459 case AP_RESPONSE_RESET_IN_PROGRESS:
461 case AP_RESPONSE_BUSY:
465 if (rc != -ENODEV && rc != -EBUSY)
467 if (i < AP_MAX_RESET - 1) {
469 status = ap_test_queue(qid, &dummy, &dummy);
472 if (rc == 0 && ap_using_interrupts()) {
473 rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
474 /* If interruption mode is supported by the machine,
475 * but an AP can not be enabled for interruption then
476 * the AP will be discarded. */
478 pr_err("Registering adapter interrupts for "
479 "AP %d failed\n", AP_QID_DEVICE(qid));
485 * ap_increase_queue_count(): Arm request timeout.
486 * @ap_dev: Pointer to an AP device.
488 * Arm request timeout if an AP device was idle and a new request is submitted.
490 static void ap_increase_queue_count(struct ap_device *ap_dev)
492 int timeout = ap_dev->drv->request_timeout;
494 ap_dev->queue_count++;
495 if (ap_dev->queue_count == 1) {
496 mod_timer(&ap_dev->timeout, jiffies + timeout);
497 ap_dev->reset = AP_RESET_ARMED;
502 * ap_decrease_queue_count(): Decrease queue count.
503 * @ap_dev: Pointer to an AP device.
505 * If AP device is still alive, re-schedule request timeout if there are still
508 static void ap_decrease_queue_count(struct ap_device *ap_dev)
510 int timeout = ap_dev->drv->request_timeout;
512 ap_dev->queue_count--;
513 if (ap_dev->queue_count > 0)
514 mod_timer(&ap_dev->timeout, jiffies + timeout);
517 * The timeout timer should to be disabled now - since
518 * del_timer_sync() is very expensive, we just tell via the
519 * reset flag to ignore the pending timeout timer.
521 ap_dev->reset = AP_RESET_IGNORE;
525 * AP device related attributes.
527 static ssize_t ap_hwtype_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
530 struct ap_device *ap_dev = to_ap_dev(dev);
531 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
534 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
535 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
538 struct ap_device *ap_dev = to_ap_dev(dev);
539 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
542 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
543 static ssize_t ap_request_count_show(struct device *dev,
544 struct device_attribute *attr,
547 struct ap_device *ap_dev = to_ap_dev(dev);
550 spin_lock_bh(&ap_dev->lock);
551 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
552 spin_unlock_bh(&ap_dev->lock);
556 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
558 static ssize_t ap_modalias_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
561 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
564 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
566 static struct attribute *ap_dev_attrs[] = {
567 &dev_attr_hwtype.attr,
568 &dev_attr_depth.attr,
569 &dev_attr_request_count.attr,
570 &dev_attr_modalias.attr,
573 static struct attribute_group ap_dev_attr_group = {
574 .attrs = ap_dev_attrs
579 * @dev: Pointer to device
580 * @drv: Pointer to device_driver
582 * AP bus driver registration/unregistration.
584 static int ap_bus_match(struct device *dev, struct device_driver *drv)
586 struct ap_device *ap_dev = to_ap_dev(dev);
587 struct ap_driver *ap_drv = to_ap_drv(drv);
588 struct ap_device_id *id;
591 * Compare device type of the device with the list of
592 * supported types of the device_driver.
594 for (id = ap_drv->ids; id->match_flags; id++) {
595 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
596 (id->dev_type != ap_dev->device_type))
604 * ap_uevent(): Uevent function for AP devices.
605 * @dev: Pointer to device
606 * @env: Pointer to kobj_uevent_env
608 * It sets up a single environment variable DEV_TYPE which contains the
609 * hardware device type.
611 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
613 struct ap_device *ap_dev = to_ap_dev(dev);
619 /* Set up DEV_TYPE environment variable. */
620 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
625 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
630 static int ap_bus_suspend(struct device *dev, pm_message_t state)
632 struct ap_device *ap_dev = to_ap_dev(dev);
635 if (!ap_suspend_flag) {
638 /* Disable scanning for devices, thus we do not want to scan
639 * for them after removing.
641 del_timer_sync(&ap_config_timer);
642 if (ap_work_queue != NULL) {
643 destroy_workqueue(ap_work_queue);
644 ap_work_queue = NULL;
646 tasklet_disable(&ap_tasklet);
648 /* Poll on the device until all requests are finished. */
651 spin_lock_bh(&ap_dev->lock);
652 __ap_poll_device(ap_dev, &flags);
653 spin_unlock_bh(&ap_dev->lock);
654 } while ((flags & 1) || (flags & 2));
656 ap_device_remove(dev);
660 static int ap_bus_resume(struct device *dev)
663 struct ap_device *ap_dev = to_ap_dev(dev);
665 if (ap_suspend_flag) {
667 if (!ap_interrupts_available())
668 ap_interrupt_indicator = NULL;
669 ap_device_probe(dev);
671 setup_timer(&ap_dev->timeout, ap_request_timeout,
672 (unsigned long) ap_dev);
674 init_timer(&ap_config_timer);
675 ap_config_timer.function = ap_config_timeout;
676 ap_config_timer.data = 0;
677 ap_config_timer.expires = jiffies + ap_config_time * HZ;
678 add_timer(&ap_config_timer);
679 ap_work_queue = create_singlethread_workqueue("kapwork");
682 tasklet_enable(&ap_tasklet);
683 if (!ap_using_interrupts())
684 ap_schedule_poll_timer();
686 tasklet_schedule(&ap_tasklet);
688 rc = ap_poll_thread_start();
690 ap_device_probe(dev);
692 setup_timer(&ap_dev->timeout, ap_request_timeout,
693 (unsigned long) ap_dev);
699 static struct bus_type ap_bus_type = {
701 .match = &ap_bus_match,
702 .uevent = &ap_uevent,
703 .suspend = ap_bus_suspend,
704 .resume = ap_bus_resume
707 static int ap_device_probe(struct device *dev)
709 struct ap_device *ap_dev = to_ap_dev(dev);
710 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
713 ap_dev->drv = ap_drv;
714 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
716 spin_lock_bh(&ap_device_list_lock);
717 list_add(&ap_dev->list, &ap_device_list);
718 spin_unlock_bh(&ap_device_list_lock);
724 * __ap_flush_queue(): Flush requests.
725 * @ap_dev: Pointer to the AP device
727 * Flush all requests from the request/pending queue of an AP device.
729 static void __ap_flush_queue(struct ap_device *ap_dev)
731 struct ap_message *ap_msg, *next;
733 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
734 list_del_init(&ap_msg->list);
735 ap_dev->pendingq_count--;
736 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
738 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
739 list_del_init(&ap_msg->list);
740 ap_dev->requestq_count--;
741 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
745 void ap_flush_queue(struct ap_device *ap_dev)
747 spin_lock_bh(&ap_dev->lock);
748 __ap_flush_queue(ap_dev);
749 spin_unlock_bh(&ap_dev->lock);
751 EXPORT_SYMBOL(ap_flush_queue);
753 static int ap_device_remove(struct device *dev)
755 struct ap_device *ap_dev = to_ap_dev(dev);
756 struct ap_driver *ap_drv = ap_dev->drv;
758 ap_flush_queue(ap_dev);
759 del_timer_sync(&ap_dev->timeout);
760 spin_lock_bh(&ap_device_list_lock);
761 list_del_init(&ap_dev->list);
762 spin_unlock_bh(&ap_device_list_lock);
764 ap_drv->remove(ap_dev);
765 spin_lock_bh(&ap_dev->lock);
766 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
767 spin_unlock_bh(&ap_dev->lock);
771 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
774 struct device_driver *drv = &ap_drv->driver;
776 drv->bus = &ap_bus_type;
777 drv->probe = ap_device_probe;
778 drv->remove = ap_device_remove;
781 return driver_register(drv);
783 EXPORT_SYMBOL(ap_driver_register);
785 void ap_driver_unregister(struct ap_driver *ap_drv)
787 driver_unregister(&ap_drv->driver);
789 EXPORT_SYMBOL(ap_driver_unregister);
794 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
796 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
799 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
801 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
803 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
806 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
808 return snprintf(buf, PAGE_SIZE, "%d\n",
809 ap_using_interrupts() ? 1 : 0);
812 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
814 static ssize_t ap_config_time_store(struct bus_type *bus,
815 const char *buf, size_t count)
819 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
821 ap_config_time = time;
822 if (!timer_pending(&ap_config_timer) ||
823 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
824 ap_config_timer.expires = jiffies + ap_config_time * HZ;
825 add_timer(&ap_config_timer);
830 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
832 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
834 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
837 static ssize_t ap_poll_thread_store(struct bus_type *bus,
838 const char *buf, size_t count)
842 if (sscanf(buf, "%d\n", &flag) != 1)
845 rc = ap_poll_thread_start();
850 ap_poll_thread_stop();
854 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
856 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
858 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
861 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
864 unsigned long long time;
867 /* 120 seconds = maximum poll interval */
868 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
869 time > 120000000000ULL)
872 hr_time = ktime_set(0, poll_timeout);
874 if (!hrtimer_is_queued(&ap_poll_timer) ||
875 !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
876 hrtimer_set_expires(&ap_poll_timer, hr_time);
877 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
882 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
884 static struct bus_attribute *const ap_bus_attrs[] = {
886 &bus_attr_config_time,
887 &bus_attr_poll_thread,
888 &bus_attr_ap_interrupts,
889 &bus_attr_poll_timeout,
894 * ap_select_domain(): Select an AP domain.
896 * Pick one of the 16 AP domains.
898 static int ap_select_domain(void)
900 int queue_depth, device_type, count, max_count, best_domain;
904 * We want to use a single domain. Either the one specified with
905 * the "domain=" parameter or the domain with the maximum number
908 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
909 /* Domain has already been selected. */
913 for (i = 0; i < AP_DOMAINS; i++) {
915 for (j = 0; j < AP_DEVICES; j++) {
916 ap_qid_t qid = AP_MKQID(j, i);
917 rc = ap_query_queue(qid, &queue_depth, &device_type);
922 if (count > max_count) {
927 if (best_domain >= 0){
928 ap_domain_index = best_domain;
935 * ap_probe_device_type(): Find the device type of an AP.
936 * @ap_dev: pointer to the AP device.
938 * Find the device type if query queue returned a device type of 0.
940 static int ap_probe_device_type(struct ap_device *ap_dev)
942 static unsigned char msg[] = {
943 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
944 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
945 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
946 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
947 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
948 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
949 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
950 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
951 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
952 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
953 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
954 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
955 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
956 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
957 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
958 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
959 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
960 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
961 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
962 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
963 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
964 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
965 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
966 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
967 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
968 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
969 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
970 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
971 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
972 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
973 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
974 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
975 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
976 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
977 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
978 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
979 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
980 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
981 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
982 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
983 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
984 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
985 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
986 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
987 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
989 struct ap_queue_status status;
990 unsigned long long psmid;
994 reply = (void *) get_zeroed_page(GFP_KERNEL);
1000 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1002 if (status.response_code != AP_RESPONSE_NORMAL) {
1007 /* Wait for the test message to complete. */
1008 for (i = 0; i < 6; i++) {
1010 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1011 if (status.response_code == AP_RESPONSE_NORMAL &&
1012 psmid == 0x0102030405060708ULL)
1016 /* Got an answer. */
1017 if (reply[0] == 0x00 && reply[1] == 0x86)
1018 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1020 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1026 free_page((unsigned long) reply);
1031 static void ap_interrupt_handler(void *unused1, void *unused2)
1033 tasklet_schedule(&ap_tasklet);
1037 * __ap_scan_bus(): Scan the AP bus.
1038 * @dev: Pointer to device
1039 * @data: Pointer to data
1041 * Scan the AP bus for new devices.
1043 static int __ap_scan_bus(struct device *dev, void *data)
1045 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1048 static void ap_device_release(struct device *dev)
1050 struct ap_device *ap_dev = to_ap_dev(dev);
1055 static void ap_scan_bus(struct work_struct *unused)
1057 struct ap_device *ap_dev;
1060 int queue_depth, device_type;
1063 if (ap_select_domain() != 0)
1065 for (i = 0; i < AP_DEVICES; i++) {
1066 qid = AP_MKQID(i, ap_domain_index);
1067 dev = bus_find_device(&ap_bus_type, NULL,
1068 (void *)(unsigned long)qid,
1070 rc = ap_query_queue(qid, &queue_depth, &device_type);
1073 set_current_state(TASK_UNINTERRUPTIBLE);
1074 schedule_timeout(AP_RESET_TIMEOUT);
1075 rc = ap_query_queue(qid, &queue_depth,
1078 ap_dev = to_ap_dev(dev);
1079 spin_lock_bh(&ap_dev->lock);
1080 if (rc || ap_dev->unregistered) {
1081 spin_unlock_bh(&ap_dev->lock);
1082 device_unregister(dev);
1086 spin_unlock_bh(&ap_dev->lock);
1092 rc = ap_init_queue(qid);
1095 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1099 ap_dev->queue_depth = queue_depth;
1100 ap_dev->unregistered = 1;
1101 spin_lock_init(&ap_dev->lock);
1102 INIT_LIST_HEAD(&ap_dev->pendingq);
1103 INIT_LIST_HEAD(&ap_dev->requestq);
1104 INIT_LIST_HEAD(&ap_dev->list);
1105 setup_timer(&ap_dev->timeout, ap_request_timeout,
1106 (unsigned long) ap_dev);
1107 if (device_type == 0)
1108 ap_probe_device_type(ap_dev);
1110 ap_dev->device_type = device_type;
1112 ap_dev->device.bus = &ap_bus_type;
1113 ap_dev->device.parent = ap_root_device;
1114 dev_set_name(&ap_dev->device, "card%02x",
1115 AP_QID_DEVICE(ap_dev->qid));
1116 ap_dev->device.release = ap_device_release;
1117 rc = device_register(&ap_dev->device);
1119 put_device(&ap_dev->device);
1122 /* Add device attributes. */
1123 rc = sysfs_create_group(&ap_dev->device.kobj,
1124 &ap_dev_attr_group);
1126 spin_lock_bh(&ap_dev->lock);
1127 ap_dev->unregistered = 0;
1128 spin_unlock_bh(&ap_dev->lock);
1131 device_unregister(&ap_dev->device);
1136 ap_config_timeout(unsigned long ptr)
1138 queue_work(ap_work_queue, &ap_config_work);
1139 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1140 add_timer(&ap_config_timer);
1144 * ap_schedule_poll_timer(): Schedule poll timer.
1146 * Set up the timer to run the poll tasklet
1148 static inline void ap_schedule_poll_timer(void)
1151 if (ap_using_interrupts() || ap_suspend_flag)
1153 if (hrtimer_is_queued(&ap_poll_timer))
1155 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1156 hr_time = ktime_set(0, poll_timeout);
1157 hrtimer_forward_now(&ap_poll_timer, hr_time);
1158 hrtimer_restart(&ap_poll_timer);
1164 * ap_poll_read(): Receive pending reply messages from an AP device.
1165 * @ap_dev: pointer to the AP device
1166 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1167 * required, bit 2^1 is set if the poll timer needs to get armed
1169 * Returns 0 if the device is still present, -ENODEV if not.
1171 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1173 struct ap_queue_status status;
1174 struct ap_message *ap_msg;
1176 if (ap_dev->queue_count <= 0)
1178 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1179 ap_dev->reply->message, ap_dev->reply->length);
1180 switch (status.response_code) {
1181 case AP_RESPONSE_NORMAL:
1182 atomic_dec(&ap_poll_requests);
1183 ap_decrease_queue_count(ap_dev);
1184 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1185 if (ap_msg->psmid != ap_dev->reply->psmid)
1187 list_del_init(&ap_msg->list);
1188 ap_dev->pendingq_count--;
1189 ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
1192 if (ap_dev->queue_count > 0)
1195 case AP_RESPONSE_NO_PENDING_REPLY:
1196 if (status.queue_empty) {
1197 /* The card shouldn't forget requests but who knows. */
1198 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1199 ap_dev->queue_count = 0;
1200 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1201 ap_dev->requestq_count += ap_dev->pendingq_count;
1202 ap_dev->pendingq_count = 0;
1213 * ap_poll_write(): Send messages from the request queue to an AP device.
1214 * @ap_dev: pointer to the AP device
1215 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1216 * required, bit 2^1 is set if the poll timer needs to get armed
1218 * Returns 0 if the device is still present, -ENODEV if not.
1220 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1222 struct ap_queue_status status;
1223 struct ap_message *ap_msg;
1225 if (ap_dev->requestq_count <= 0 ||
1226 ap_dev->queue_count >= ap_dev->queue_depth)
1228 /* Start the next request on the queue. */
1229 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1230 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1231 ap_msg->message, ap_msg->length);
1232 switch (status.response_code) {
1233 case AP_RESPONSE_NORMAL:
1234 atomic_inc(&ap_poll_requests);
1235 ap_increase_queue_count(ap_dev);
1236 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1237 ap_dev->requestq_count--;
1238 ap_dev->pendingq_count++;
1239 if (ap_dev->queue_count < ap_dev->queue_depth &&
1240 ap_dev->requestq_count > 0)
1244 case AP_RESPONSE_Q_FULL:
1245 case AP_RESPONSE_RESET_IN_PROGRESS:
1248 case AP_RESPONSE_MESSAGE_TOO_BIG:
1257 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1258 * @ap_dev: pointer to the bus device
1259 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1260 * required, bit 2^1 is set if the poll timer needs to get armed
1262 * Poll AP device for pending replies and send new messages. If either
1263 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1266 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1270 rc = ap_poll_read(ap_dev, flags);
1273 return ap_poll_write(ap_dev, flags);
1277 * __ap_queue_message(): Queue a message to a device.
1278 * @ap_dev: pointer to the AP device
1279 * @ap_msg: the message to be queued
1281 * Queue a message to a device. Returns 0 if successful.
1283 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1285 struct ap_queue_status status;
1287 if (list_empty(&ap_dev->requestq) &&
1288 ap_dev->queue_count < ap_dev->queue_depth) {
1289 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1290 ap_msg->message, ap_msg->length);
1291 switch (status.response_code) {
1292 case AP_RESPONSE_NORMAL:
1293 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1294 atomic_inc(&ap_poll_requests);
1295 ap_dev->pendingq_count++;
1296 ap_increase_queue_count(ap_dev);
1297 ap_dev->total_request_count++;
1299 case AP_RESPONSE_Q_FULL:
1300 case AP_RESPONSE_RESET_IN_PROGRESS:
1301 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1302 ap_dev->requestq_count++;
1303 ap_dev->total_request_count++;
1305 case AP_RESPONSE_MESSAGE_TOO_BIG:
1306 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1308 default: /* Device is gone. */
1309 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1313 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1314 ap_dev->requestq_count++;
1315 ap_dev->total_request_count++;
1318 ap_schedule_poll_timer();
1322 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1324 unsigned long flags;
1327 spin_lock_bh(&ap_dev->lock);
1328 if (!ap_dev->unregistered) {
1329 /* Make room on the queue by polling for finished requests. */
1330 rc = ap_poll_queue(ap_dev, &flags);
1332 rc = __ap_queue_message(ap_dev, ap_msg);
1334 wake_up(&ap_poll_wait);
1336 ap_dev->unregistered = 1;
1338 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1341 spin_unlock_bh(&ap_dev->lock);
1343 device_unregister(&ap_dev->device);
1345 EXPORT_SYMBOL(ap_queue_message);
1348 * ap_cancel_message(): Cancel a crypto request.
1349 * @ap_dev: The AP device that has the message queued
1350 * @ap_msg: The message that is to be removed
1352 * Cancel a crypto request. This is done by removing the request
1353 * from the device pending or request queue. Note that the
1354 * request stays on the AP queue. When it finishes the message
1355 * reply will be discarded because the psmid can't be found.
1357 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1359 struct ap_message *tmp;
1361 spin_lock_bh(&ap_dev->lock);
1362 if (!list_empty(&ap_msg->list)) {
1363 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1364 if (tmp->psmid == ap_msg->psmid) {
1365 ap_dev->pendingq_count--;
1368 ap_dev->requestq_count--;
1370 list_del_init(&ap_msg->list);
1372 spin_unlock_bh(&ap_dev->lock);
1374 EXPORT_SYMBOL(ap_cancel_message);
1377 * ap_poll_timeout(): AP receive polling for finished AP requests.
1378 * @unused: Unused pointer.
1380 * Schedules the AP tasklet using a high resolution timer.
1382 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1384 tasklet_schedule(&ap_tasklet);
1385 return HRTIMER_NORESTART;
1389 * ap_reset(): Reset a not responding AP device.
1390 * @ap_dev: Pointer to the AP device
1392 * Reset a not responding AP device and move all requests from the
1393 * pending queue to the request queue.
1395 static void ap_reset(struct ap_device *ap_dev)
1399 ap_dev->reset = AP_RESET_IGNORE;
1400 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1401 ap_dev->queue_count = 0;
1402 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1403 ap_dev->requestq_count += ap_dev->pendingq_count;
1404 ap_dev->pendingq_count = 0;
1405 rc = ap_init_queue(ap_dev->qid);
1407 ap_dev->unregistered = 1;
1410 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1412 if (!ap_dev->unregistered) {
1413 if (ap_poll_queue(ap_dev, flags))
1414 ap_dev->unregistered = 1;
1415 if (ap_dev->reset == AP_RESET_DO)
1422 * ap_poll_all(): Poll all AP devices.
1423 * @dummy: Unused variable
1425 * Poll all AP devices on the bus in a round robin fashion. Continue
1426 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1427 * of the control flags has been set arm the poll timer.
1429 static void ap_poll_all(unsigned long dummy)
1431 unsigned long flags;
1432 struct ap_device *ap_dev;
1434 /* Reset the indicator if interrupts are used. Thus new interrupts can
1435 * be received. Doing it in the beginning of the tasklet is therefor
1436 * important that no requests on any AP get lost.
1438 if (ap_using_interrupts())
1439 xchg((u8 *)ap_interrupt_indicator, 0);
1442 spin_lock(&ap_device_list_lock);
1443 list_for_each_entry(ap_dev, &ap_device_list, list) {
1444 spin_lock(&ap_dev->lock);
1445 __ap_poll_device(ap_dev, &flags);
1446 spin_unlock(&ap_dev->lock);
1448 spin_unlock(&ap_device_list_lock);
1449 } while (flags & 1);
1451 ap_schedule_poll_timer();
1455 * ap_poll_thread(): Thread that polls for finished requests.
1456 * @data: Unused pointer
1458 * AP bus poll thread. The purpose of this thread is to poll for
1459 * finished requests in a loop if there is a "free" cpu - that is
1460 * a cpu that doesn't have anything better to do. The polling stops
1461 * as soon as there is another task or if all messages have been
1464 static int ap_poll_thread(void *data)
1466 DECLARE_WAITQUEUE(wait, current);
1467 unsigned long flags;
1469 struct ap_device *ap_dev;
1471 set_user_nice(current, 19);
1473 if (ap_suspend_flag)
1475 if (need_resched()) {
1479 add_wait_queue(&ap_poll_wait, &wait);
1480 set_current_state(TASK_INTERRUPTIBLE);
1481 if (kthread_should_stop())
1483 requests = atomic_read(&ap_poll_requests);
1486 set_current_state(TASK_RUNNING);
1487 remove_wait_queue(&ap_poll_wait, &wait);
1490 spin_lock_bh(&ap_device_list_lock);
1491 list_for_each_entry(ap_dev, &ap_device_list, list) {
1492 spin_lock(&ap_dev->lock);
1493 __ap_poll_device(ap_dev, &flags);
1494 spin_unlock(&ap_dev->lock);
1496 spin_unlock_bh(&ap_device_list_lock);
1498 set_current_state(TASK_RUNNING);
1499 remove_wait_queue(&ap_poll_wait, &wait);
1503 static int ap_poll_thread_start(void)
1507 if (ap_using_interrupts() || ap_suspend_flag)
1509 mutex_lock(&ap_poll_thread_mutex);
1510 if (!ap_poll_kthread) {
1511 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1512 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1514 ap_poll_kthread = NULL;
1518 mutex_unlock(&ap_poll_thread_mutex);
1522 static void ap_poll_thread_stop(void)
1524 mutex_lock(&ap_poll_thread_mutex);
1525 if (ap_poll_kthread) {
1526 kthread_stop(ap_poll_kthread);
1527 ap_poll_kthread = NULL;
1529 mutex_unlock(&ap_poll_thread_mutex);
1533 * ap_request_timeout(): Handling of request timeouts
1534 * @data: Holds the AP device.
1536 * Handles request timeouts.
1538 static void ap_request_timeout(unsigned long data)
1540 struct ap_device *ap_dev = (struct ap_device *) data;
1542 if (ap_dev->reset == AP_RESET_ARMED) {
1543 ap_dev->reset = AP_RESET_DO;
1545 if (ap_using_interrupts())
1546 tasklet_schedule(&ap_tasklet);
1550 static void ap_reset_domain(void)
1554 if (ap_domain_index != -1)
1555 for (i = 0; i < AP_DEVICES; i++)
1556 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1559 static void ap_reset_all(void)
1563 for (i = 0; i < AP_DOMAINS; i++)
1564 for (j = 0; j < AP_DEVICES; j++)
1565 ap_reset_queue(AP_MKQID(j, i));
1568 static struct reset_call ap_reset_call = {
1573 * ap_module_init(): The module initialization code.
1575 * Initializes the module.
1577 int __init ap_module_init(void)
1581 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1582 pr_warning("%d is not a valid cryptographic domain\n",
1586 if (ap_instructions_available() != 0) {
1587 pr_warning("The hardware system does not support "
1588 "AP instructions\n");
1591 if (ap_interrupts_available()) {
1592 isc_register(AP_ISC);
1593 ap_interrupt_indicator = s390_register_adapter_interrupt(
1594 &ap_interrupt_handler, NULL, AP_ISC);
1595 if (IS_ERR(ap_interrupt_indicator)) {
1596 ap_interrupt_indicator = NULL;
1597 isc_unregister(AP_ISC);
1601 register_reset_call(&ap_reset_call);
1603 /* Create /sys/bus/ap. */
1604 rc = bus_register(&ap_bus_type);
1607 for (i = 0; ap_bus_attrs[i]; i++) {
1608 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1613 /* Create /sys/devices/ap. */
1614 ap_root_device = root_device_register("ap");
1615 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1619 ap_work_queue = create_singlethread_workqueue("kapwork");
1620 if (!ap_work_queue) {
1625 if (ap_select_domain() == 0)
1628 /* Setup the AP bus rescan timer. */
1629 init_timer(&ap_config_timer);
1630 ap_config_timer.function = ap_config_timeout;
1631 ap_config_timer.data = 0;
1632 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1633 add_timer(&ap_config_timer);
1635 /* Setup the high resultion poll timer.
1636 * If we are running under z/VM adjust polling to z/VM polling rate.
1639 poll_timeout = 1500000;
1640 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1641 ap_poll_timer.function = ap_poll_timeout;
1643 /* Start the low priority AP bus poll thread. */
1644 if (ap_thread_flag) {
1645 rc = ap_poll_thread_start();
1653 del_timer_sync(&ap_config_timer);
1654 hrtimer_cancel(&ap_poll_timer);
1655 destroy_workqueue(ap_work_queue);
1657 root_device_unregister(ap_root_device);
1660 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1661 bus_unregister(&ap_bus_type);
1663 unregister_reset_call(&ap_reset_call);
1664 if (ap_using_interrupts()) {
1665 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1666 isc_unregister(AP_ISC);
1671 static int __ap_match_all(struct device *dev, void *data)
1677 * ap_modules_exit(): The module termination code
1679 * Terminates the module.
1681 void ap_module_exit(void)
1687 ap_poll_thread_stop();
1688 del_timer_sync(&ap_config_timer);
1689 hrtimer_cancel(&ap_poll_timer);
1690 destroy_workqueue(ap_work_queue);
1691 tasklet_kill(&ap_tasklet);
1692 root_device_unregister(ap_root_device);
1693 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1696 device_unregister(dev);
1699 for (i = 0; ap_bus_attrs[i]; i++)
1700 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1701 bus_unregister(&ap_bus_type);
1702 unregister_reset_call(&ap_reset_call);
1703 if (ap_using_interrupts()) {
1704 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1705 isc_unregister(AP_ISC);
1709 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1710 module_init(ap_module_init);
1711 module_exit(ap_module_exit);