2 * Copyright IBM Corp. 2006, 2012
3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
6 * Felix Beck <felix.beck@de.ibm.com>
7 * Holger Dengler <hd@linux.vnet.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 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/kernel_stat.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <asm/reset.h>
42 #include <linux/atomic.h>
44 #include <linux/hrtimer.h>
45 #include <linux/ktime.h>
46 #include <asm/facility.h>
50 /* Some prototypes. */
51 static void ap_scan_bus(struct work_struct *);
52 static void ap_poll_all(unsigned long);
53 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
54 static int ap_poll_thread_start(void);
55 static void ap_poll_thread_stop(void);
56 static void ap_request_timeout(unsigned long);
57 static inline void ap_schedule_poll_timer(void);
58 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
59 static int ap_device_remove(struct device *dev);
60 static int ap_device_probe(struct device *dev);
61 static void ap_interrupt_handler(struct airq_struct *airq);
62 static void ap_reset(struct ap_device *ap_dev);
63 static void ap_config_timeout(unsigned long ptr);
64 static int ap_select_domain(void);
65 static void ap_query_configuration(void);
70 MODULE_AUTHOR("IBM Corporation");
71 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
72 "Copyright IBM Corp. 2006, 2012");
73 MODULE_LICENSE("GPL");
78 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
79 module_param_named(domain, ap_domain_index, int, 0000);
80 MODULE_PARM_DESC(domain, "domain index for ap devices");
81 EXPORT_SYMBOL(ap_domain_index);
83 static int ap_thread_flag = 0;
84 module_param_named(poll_thread, ap_thread_flag, int, 0000);
85 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
87 static struct device *ap_root_device = NULL;
88 static struct ap_config_info *ap_configuration;
89 static DEFINE_SPINLOCK(ap_device_list_lock);
90 static LIST_HEAD(ap_device_list);
93 * Workqueue & timer for bus rescan.
95 static struct workqueue_struct *ap_work_queue;
96 static struct timer_list ap_config_timer;
97 static int ap_config_time = AP_CONFIG_TIME;
98 static DECLARE_WORK(ap_config_work, ap_scan_bus);
101 * Tasklet & timer for AP request polling and interrupts
103 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
104 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
105 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
106 static struct task_struct *ap_poll_kthread = NULL;
107 static DEFINE_MUTEX(ap_poll_thread_mutex);
108 static DEFINE_SPINLOCK(ap_poll_timer_lock);
109 static struct hrtimer ap_poll_timer;
110 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
111 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
112 static unsigned long long poll_timeout = 250000;
115 static int ap_suspend_flag;
116 /* Flag to check if domain was set through module parameter domain=. This is
117 * important when supsend and resume is done in a z/VM environment where the
118 * domain might change. */
119 static int user_set_domain = 0;
120 static struct bus_type ap_bus_type;
122 /* Adapter interrupt definitions */
123 static int ap_airq_flag;
125 static struct airq_struct ap_airq = {
126 .handler = ap_interrupt_handler,
131 * ap_using_interrupts() - Returns non-zero if interrupt support is
134 static inline int ap_using_interrupts(void)
140 * ap_intructions_available() - Test if AP instructions are available.
142 * Returns 0 if the AP instructions are installed.
144 static inline int ap_instructions_available(void)
146 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
147 register unsigned long reg1 asm ("1") = -ENODEV;
148 register unsigned long reg2 asm ("2") = 0UL;
151 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
155 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
160 * ap_interrupts_available(): Test if AP interrupts are available.
162 * Returns 1 if AP interrupts are available.
164 static int ap_interrupts_available(void)
166 return test_facility(2) && test_facility(65);
170 * ap_configuration_available(): Test if AP configuration
171 * information is available.
173 * Returns 1 if AP configuration information is available.
176 static int ap_configuration_available(void)
178 return test_facility(2) && test_facility(12);
183 * ap_test_queue(): Test adjunct processor queue.
184 * @qid: The AP queue number
185 * @queue_depth: Pointer to queue depth value
186 * @device_type: Pointer to device type value
188 * Returns AP queue status structure.
190 static inline struct ap_queue_status
191 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
193 register unsigned long reg0 asm ("0") = qid;
194 register struct ap_queue_status reg1 asm ("1");
195 register unsigned long reg2 asm ("2") = 0UL;
197 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
198 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
199 *device_type = (int) (reg2 >> 24);
200 *queue_depth = (int) (reg2 & 0xff);
205 * ap_reset_queue(): Reset adjunct processor queue.
206 * @qid: The AP queue number
208 * Returns AP queue status structure.
210 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
212 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
213 register struct ap_queue_status reg1 asm ("1");
214 register unsigned long reg2 asm ("2") = 0UL;
217 ".long 0xb2af0000" /* PQAP(RAPQ) */
218 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
224 * ap_queue_interruption_control(): Enable interruption for a specific AP.
225 * @qid: The AP queue number
226 * @ind: The notification indicator byte
228 * Returns AP queue status.
230 static inline struct ap_queue_status
231 ap_queue_interruption_control(ap_qid_t qid, void *ind)
233 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
234 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
235 register struct ap_queue_status reg1_out asm ("1");
236 register void *reg2 asm ("2") = ind;
238 ".long 0xb2af0000" /* PQAP(AQIC) */
239 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
247 static inline struct ap_queue_status
248 __ap_query_functions(ap_qid_t qid, unsigned int *functions)
250 register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
251 register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
252 register unsigned long reg2 asm ("2");
255 ".long 0xb2af0000\n" /* PQAP(TAPQ) */
258 : "+d" (reg0), "+d" (reg1), "=d" (reg2)
262 *functions = (unsigned int)(reg2 >> 32);
268 static inline int __ap_query_configuration(struct ap_config_info *config)
270 register unsigned long reg0 asm ("0") = 0x04000000UL;
271 register unsigned long reg1 asm ("1") = -EINVAL;
272 register unsigned char *reg2 asm ("2") = (unsigned char *)config;
275 ".long 0xb2af0000\n" /* PQAP(QCI) */
279 : "+d" (reg0), "+d" (reg1), "+d" (reg2)
288 * ap_query_functions(): Query supported functions.
289 * @qid: The AP queue number
290 * @functions: Pointer to functions field.
294 * -ENODEV if queue not valid.
295 * -EBUSY if device busy.
296 * -EINVAL if query function is not supported
298 static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
301 struct ap_queue_status status;
303 status = __ap_query_functions(qid, functions);
305 for (i = 0; i < AP_MAX_RESET; i++) {
306 if (ap_queue_status_invalid_test(&status))
309 switch (status.response_code) {
310 case AP_RESPONSE_NORMAL:
312 case AP_RESPONSE_RESET_IN_PROGRESS:
313 case AP_RESPONSE_BUSY:
315 case AP_RESPONSE_Q_NOT_AVAIL:
316 case AP_RESPONSE_DECONFIGURED:
317 case AP_RESPONSE_CHECKSTOPPED:
318 case AP_RESPONSE_INVALID_ADDRESS:
320 case AP_RESPONSE_OTHERWISE_CHANGED:
325 if (i < AP_MAX_RESET - 1) {
327 status = __ap_query_functions(qid, functions);
337 * ap_queue_enable_interruption(): Enable interruption on an AP.
338 * @qid: The AP queue number
339 * @ind: the notification indicator byte
341 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
342 * on the return value it waits a while and tests the AP queue if interrupts
343 * have been switched on using ap_test_queue().
345 static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
348 struct ap_queue_status status;
349 int t_depth, t_device_type, rc, i;
352 status = ap_queue_interruption_control(qid, ind);
354 for (i = 0; i < AP_MAX_RESET; i++) {
355 switch (status.response_code) {
356 case AP_RESPONSE_NORMAL:
357 if (status.int_enabled)
360 case AP_RESPONSE_RESET_IN_PROGRESS:
361 case AP_RESPONSE_BUSY:
362 if (i < AP_MAX_RESET - 1) {
364 status = ap_queue_interruption_control(qid,
369 case AP_RESPONSE_Q_NOT_AVAIL:
370 case AP_RESPONSE_DECONFIGURED:
371 case AP_RESPONSE_CHECKSTOPPED:
372 case AP_RESPONSE_INVALID_ADDRESS:
374 case AP_RESPONSE_OTHERWISE_CHANGED:
375 if (status.int_enabled)
381 if (i < AP_MAX_RESET - 1) {
383 status = ap_test_queue(qid, &t_depth, &t_device_type);
393 * __ap_send(): Send message to adjunct processor queue.
394 * @qid: The AP queue number
395 * @psmid: The program supplied message identifier
396 * @msg: The message text
397 * @length: The message length
398 * @special: Special Bit
400 * Returns AP queue status structure.
401 * Condition code 1 on NQAP can't happen because the L bit is 1.
402 * Condition code 2 on NQAP also means the send is incomplete,
403 * because a segment boundary was reached. The NQAP is repeated.
405 static inline struct ap_queue_status
406 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
407 unsigned int special)
409 typedef struct { char _[length]; } msgblock;
410 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
411 register struct ap_queue_status reg1 asm ("1");
412 register unsigned long reg2 asm ("2") = (unsigned long) msg;
413 register unsigned long reg3 asm ("3") = (unsigned long) length;
414 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
415 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
421 "0: .long 0xb2ad0042\n" /* NQAP */
423 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
424 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
429 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
431 struct ap_queue_status status;
433 status = __ap_send(qid, psmid, msg, length, 0);
434 switch (status.response_code) {
435 case AP_RESPONSE_NORMAL:
437 case AP_RESPONSE_Q_FULL:
438 case AP_RESPONSE_RESET_IN_PROGRESS:
440 case AP_RESPONSE_REQ_FAC_NOT_INST:
442 default: /* Device is gone. */
446 EXPORT_SYMBOL(ap_send);
449 * __ap_recv(): Receive message from adjunct processor queue.
450 * @qid: The AP queue number
451 * @psmid: Pointer to program supplied message identifier
452 * @msg: The message text
453 * @length: The message length
455 * Returns AP queue status structure.
456 * Condition code 1 on DQAP means the receive has taken place
457 * but only partially. The response is incomplete, hence the
459 * Condition code 2 on DQAP also means the receive is incomplete,
460 * this time because a segment boundary was reached. Again, the
462 * Note that gpr2 is used by the DQAP instruction to keep track of
463 * any 'residual' length, in case the instruction gets interrupted.
464 * Hence it gets zeroed before the instruction.
466 static inline struct ap_queue_status
467 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
469 typedef struct { char _[length]; } msgblock;
470 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
471 register struct ap_queue_status reg1 asm ("1");
472 register unsigned long reg2 asm("2") = 0UL;
473 register unsigned long reg4 asm("4") = (unsigned long) msg;
474 register unsigned long reg5 asm("5") = (unsigned long) length;
475 register unsigned long reg6 asm("6") = 0UL;
476 register unsigned long reg7 asm("7") = 0UL;
480 "0: .long 0xb2ae0064\n" /* DQAP */
482 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
483 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
484 "=m" (*(msgblock *) msg) : : "cc" );
485 *psmid = (((unsigned long long) reg6) << 32) + reg7;
489 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
491 struct ap_queue_status status;
493 status = __ap_recv(qid, psmid, msg, length);
494 switch (status.response_code) {
495 case AP_RESPONSE_NORMAL:
497 case AP_RESPONSE_NO_PENDING_REPLY:
498 if (status.queue_empty)
501 case AP_RESPONSE_RESET_IN_PROGRESS:
507 EXPORT_SYMBOL(ap_recv);
510 * ap_query_queue(): Check if an AP queue is available.
511 * @qid: The AP queue number
512 * @queue_depth: Pointer to queue depth value
513 * @device_type: Pointer to device type value
515 * The test is repeated for AP_MAX_RESET times.
517 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
519 struct ap_queue_status status;
520 int t_depth, t_device_type, rc, i;
523 for (i = 0; i < AP_MAX_RESET; i++) {
524 status = ap_test_queue(qid, &t_depth, &t_device_type);
525 switch (status.response_code) {
526 case AP_RESPONSE_NORMAL:
527 *queue_depth = t_depth + 1;
528 *device_type = t_device_type;
531 case AP_RESPONSE_Q_NOT_AVAIL:
534 case AP_RESPONSE_RESET_IN_PROGRESS:
536 case AP_RESPONSE_DECONFIGURED:
539 case AP_RESPONSE_CHECKSTOPPED:
542 case AP_RESPONSE_INVALID_ADDRESS:
545 case AP_RESPONSE_OTHERWISE_CHANGED:
547 case AP_RESPONSE_BUSY:
554 if (i < AP_MAX_RESET - 1)
561 * ap_init_queue(): Reset an AP queue.
562 * @qid: The AP queue number
564 * Reset an AP queue and wait for it to become available again.
566 static int ap_init_queue(ap_qid_t qid)
568 struct ap_queue_status status;
572 status = ap_reset_queue(qid);
573 for (i = 0; i < AP_MAX_RESET; i++) {
574 switch (status.response_code) {
575 case AP_RESPONSE_NORMAL:
576 if (status.queue_empty)
579 case AP_RESPONSE_Q_NOT_AVAIL:
580 case AP_RESPONSE_DECONFIGURED:
581 case AP_RESPONSE_CHECKSTOPPED:
582 i = AP_MAX_RESET; /* return with -ENODEV */
584 case AP_RESPONSE_RESET_IN_PROGRESS:
586 case AP_RESPONSE_BUSY:
590 if (rc != -ENODEV && rc != -EBUSY)
592 if (i < AP_MAX_RESET - 1) {
594 status = ap_test_queue(qid, &dummy, &dummy);
597 if (rc == 0 && ap_using_interrupts()) {
598 rc = ap_queue_enable_interruption(qid, ap_airq.lsi_ptr);
599 /* If interruption mode is supported by the machine,
600 * but an AP can not be enabled for interruption then
601 * the AP will be discarded. */
603 pr_err("Registering adapter interrupts for "
604 "AP %d failed\n", AP_QID_DEVICE(qid));
610 * ap_increase_queue_count(): Arm request timeout.
611 * @ap_dev: Pointer to an AP device.
613 * Arm request timeout if an AP device was idle and a new request is submitted.
615 static void ap_increase_queue_count(struct ap_device *ap_dev)
617 int timeout = ap_dev->drv->request_timeout;
619 ap_dev->queue_count++;
620 if (ap_dev->queue_count == 1) {
621 mod_timer(&ap_dev->timeout, jiffies + timeout);
622 ap_dev->reset = AP_RESET_ARMED;
627 * ap_decrease_queue_count(): Decrease queue count.
628 * @ap_dev: Pointer to an AP device.
630 * If AP device is still alive, re-schedule request timeout if there are still
633 static void ap_decrease_queue_count(struct ap_device *ap_dev)
635 int timeout = ap_dev->drv->request_timeout;
637 ap_dev->queue_count--;
638 if (ap_dev->queue_count > 0)
639 mod_timer(&ap_dev->timeout, jiffies + timeout);
642 * The timeout timer should to be disabled now - since
643 * del_timer_sync() is very expensive, we just tell via the
644 * reset flag to ignore the pending timeout timer.
646 ap_dev->reset = AP_RESET_IGNORE;
650 * AP device related attributes.
652 static ssize_t ap_hwtype_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
655 struct ap_device *ap_dev = to_ap_dev(dev);
656 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
659 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
660 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
663 struct ap_device *ap_dev = to_ap_dev(dev);
664 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
667 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
668 static ssize_t ap_request_count_show(struct device *dev,
669 struct device_attribute *attr,
672 struct ap_device *ap_dev = to_ap_dev(dev);
675 spin_lock_bh(&ap_dev->lock);
676 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
677 spin_unlock_bh(&ap_dev->lock);
681 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
683 static ssize_t ap_requestq_count_show(struct device *dev,
684 struct device_attribute *attr, char *buf)
686 struct ap_device *ap_dev = to_ap_dev(dev);
689 spin_lock_bh(&ap_dev->lock);
690 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
691 spin_unlock_bh(&ap_dev->lock);
695 static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
697 static ssize_t ap_pendingq_count_show(struct device *dev,
698 struct device_attribute *attr, char *buf)
700 struct ap_device *ap_dev = to_ap_dev(dev);
703 spin_lock_bh(&ap_dev->lock);
704 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
705 spin_unlock_bh(&ap_dev->lock);
709 static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
711 static ssize_t ap_modalias_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
714 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
717 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
719 static ssize_t ap_functions_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
722 struct ap_device *ap_dev = to_ap_dev(dev);
723 return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
726 static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
728 static struct attribute *ap_dev_attrs[] = {
729 &dev_attr_hwtype.attr,
730 &dev_attr_depth.attr,
731 &dev_attr_request_count.attr,
732 &dev_attr_requestq_count.attr,
733 &dev_attr_pendingq_count.attr,
734 &dev_attr_modalias.attr,
735 &dev_attr_ap_functions.attr,
738 static struct attribute_group ap_dev_attr_group = {
739 .attrs = ap_dev_attrs
744 * @dev: Pointer to device
745 * @drv: Pointer to device_driver
747 * AP bus driver registration/unregistration.
749 static int ap_bus_match(struct device *dev, struct device_driver *drv)
751 struct ap_device *ap_dev = to_ap_dev(dev);
752 struct ap_driver *ap_drv = to_ap_drv(drv);
753 struct ap_device_id *id;
756 * Compare device type of the device with the list of
757 * supported types of the device_driver.
759 for (id = ap_drv->ids; id->match_flags; id++) {
760 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
761 (id->dev_type != ap_dev->device_type))
769 * ap_uevent(): Uevent function for AP devices.
770 * @dev: Pointer to device
771 * @env: Pointer to kobj_uevent_env
773 * It sets up a single environment variable DEV_TYPE which contains the
774 * hardware device type.
776 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
778 struct ap_device *ap_dev = to_ap_dev(dev);
784 /* Set up DEV_TYPE environment variable. */
785 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
790 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
795 static int ap_bus_suspend(struct device *dev, pm_message_t state)
797 struct ap_device *ap_dev = to_ap_dev(dev);
800 if (!ap_suspend_flag) {
803 /* Disable scanning for devices, thus we do not want to scan
804 * for them after removing.
806 del_timer_sync(&ap_config_timer);
807 if (ap_work_queue != NULL) {
808 destroy_workqueue(ap_work_queue);
809 ap_work_queue = NULL;
812 tasklet_disable(&ap_tasklet);
814 /* Poll on the device until all requests are finished. */
817 spin_lock_bh(&ap_dev->lock);
818 __ap_poll_device(ap_dev, &flags);
819 spin_unlock_bh(&ap_dev->lock);
820 } while ((flags & 1) || (flags & 2));
822 spin_lock_bh(&ap_dev->lock);
823 ap_dev->unregistered = 1;
824 spin_unlock_bh(&ap_dev->lock);
829 static int ap_bus_resume(struct device *dev)
831 struct ap_device *ap_dev = to_ap_dev(dev);
834 if (ap_suspend_flag) {
836 if (ap_interrupts_available()) {
837 if (!ap_using_interrupts()) {
838 rc = register_adapter_interrupt(&ap_airq);
839 ap_airq_flag = (rc == 0);
842 if (ap_using_interrupts()) {
843 unregister_adapter_interrupt(&ap_airq);
847 ap_query_configuration();
848 if (!user_set_domain) {
849 ap_domain_index = -1;
852 init_timer(&ap_config_timer);
853 ap_config_timer.function = ap_config_timeout;
854 ap_config_timer.data = 0;
855 ap_config_timer.expires = jiffies + ap_config_time * HZ;
856 add_timer(&ap_config_timer);
857 ap_work_queue = create_singlethread_workqueue("kapwork");
860 tasklet_enable(&ap_tasklet);
861 if (!ap_using_interrupts())
862 ap_schedule_poll_timer();
864 tasklet_schedule(&ap_tasklet);
866 rc = ap_poll_thread_start();
871 if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
872 spin_lock_bh(&ap_dev->lock);
873 ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
875 spin_unlock_bh(&ap_dev->lock);
877 queue_work(ap_work_queue, &ap_config_work);
882 static struct bus_type ap_bus_type = {
884 .match = &ap_bus_match,
885 .uevent = &ap_uevent,
886 .suspend = ap_bus_suspend,
887 .resume = ap_bus_resume
890 static int ap_device_probe(struct device *dev)
892 struct ap_device *ap_dev = to_ap_dev(dev);
893 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
896 ap_dev->drv = ap_drv;
897 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
899 spin_lock_bh(&ap_device_list_lock);
900 list_add(&ap_dev->list, &ap_device_list);
901 spin_unlock_bh(&ap_device_list_lock);
907 * __ap_flush_queue(): Flush requests.
908 * @ap_dev: Pointer to the AP device
910 * Flush all requests from the request/pending queue of an AP device.
912 static void __ap_flush_queue(struct ap_device *ap_dev)
914 struct ap_message *ap_msg, *next;
916 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
917 list_del_init(&ap_msg->list);
918 ap_dev->pendingq_count--;
919 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
921 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
922 list_del_init(&ap_msg->list);
923 ap_dev->requestq_count--;
924 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
928 void ap_flush_queue(struct ap_device *ap_dev)
930 spin_lock_bh(&ap_dev->lock);
931 __ap_flush_queue(ap_dev);
932 spin_unlock_bh(&ap_dev->lock);
934 EXPORT_SYMBOL(ap_flush_queue);
936 static int ap_device_remove(struct device *dev)
938 struct ap_device *ap_dev = to_ap_dev(dev);
939 struct ap_driver *ap_drv = ap_dev->drv;
941 ap_flush_queue(ap_dev);
942 del_timer_sync(&ap_dev->timeout);
943 spin_lock_bh(&ap_device_list_lock);
944 list_del_init(&ap_dev->list);
945 spin_unlock_bh(&ap_device_list_lock);
947 ap_drv->remove(ap_dev);
948 spin_lock_bh(&ap_dev->lock);
949 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
950 spin_unlock_bh(&ap_dev->lock);
954 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
957 struct device_driver *drv = &ap_drv->driver;
959 drv->bus = &ap_bus_type;
960 drv->probe = ap_device_probe;
961 drv->remove = ap_device_remove;
964 return driver_register(drv);
966 EXPORT_SYMBOL(ap_driver_register);
968 void ap_driver_unregister(struct ap_driver *ap_drv)
970 driver_unregister(&ap_drv->driver);
972 EXPORT_SYMBOL(ap_driver_unregister);
974 void ap_bus_force_rescan(void)
976 /* reconfigure the AP bus rescan timer. */
977 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
978 /* processing a asynchronous bus rescan */
979 queue_work(ap_work_queue, &ap_config_work);
980 flush_work(&ap_config_work);
982 EXPORT_SYMBOL(ap_bus_force_rescan);
987 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
989 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
992 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
994 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
996 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
999 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1001 return snprintf(buf, PAGE_SIZE, "%d\n",
1002 ap_using_interrupts() ? 1 : 0);
1005 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
1007 static ssize_t ap_config_time_store(struct bus_type *bus,
1008 const char *buf, size_t count)
1012 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1014 ap_config_time = time;
1015 if (!timer_pending(&ap_config_timer) ||
1016 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
1017 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1018 add_timer(&ap_config_timer);
1023 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
1025 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
1027 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1030 static ssize_t ap_poll_thread_store(struct bus_type *bus,
1031 const char *buf, size_t count)
1035 if (sscanf(buf, "%d\n", &flag) != 1)
1038 rc = ap_poll_thread_start();
1043 ap_poll_thread_stop();
1047 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
1049 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1051 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1054 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1057 unsigned long long time;
1060 /* 120 seconds = maximum poll interval */
1061 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1062 time > 120000000000ULL)
1064 poll_timeout = time;
1065 hr_time = ktime_set(0, poll_timeout);
1067 if (!hrtimer_is_queued(&ap_poll_timer) ||
1068 !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
1069 hrtimer_set_expires(&ap_poll_timer, hr_time);
1070 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1075 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
1077 static struct bus_attribute *const ap_bus_attrs[] = {
1078 &bus_attr_ap_domain,
1079 &bus_attr_config_time,
1080 &bus_attr_poll_thread,
1081 &bus_attr_ap_interrupts,
1082 &bus_attr_poll_timeout,
1086 static inline int ap_test_config(unsigned int *field, unsigned int nr)
1090 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
1094 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
1097 * Returns 0 if the card is not configured
1098 * 1 if the card is configured or
1099 * if the configuration information is not available
1101 static inline int ap_test_config_card_id(unsigned int id)
1103 if (!ap_configuration)
1105 return ap_test_config(ap_configuration->apm, id);
1109 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
1110 * @domain AP usage domain ID
1112 * Returns 0 if the usage domain is not configured
1113 * 1 if the usage domain is configured or
1114 * if the configuration information is not available
1116 static inline int ap_test_config_domain(unsigned int domain)
1118 if (!ap_configuration)
1120 return ap_test_config(ap_configuration->aqm, domain);
1124 * ap_query_configuration(): Query AP configuration information.
1126 * Query information of installed cards and configured domains from AP.
1128 static void ap_query_configuration(void)
1131 if (ap_configuration_available()) {
1132 if (!ap_configuration)
1134 kzalloc(sizeof(struct ap_config_info),
1136 if (ap_configuration)
1137 __ap_query_configuration(ap_configuration);
1139 ap_configuration = NULL;
1141 ap_configuration = NULL;
1146 * ap_select_domain(): Select an AP domain.
1148 * Pick one of the 16 AP domains.
1150 static int ap_select_domain(void)
1152 int queue_depth, device_type, count, max_count, best_domain;
1157 * We want to use a single domain. Either the one specified with
1158 * the "domain=" parameter or the domain with the maximum number
1161 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
1162 /* Domain has already been selected. */
1166 for (i = 0; i < AP_DOMAINS; i++) {
1167 if (!ap_test_config_domain(i))
1170 for (j = 0; j < AP_DEVICES; j++) {
1171 if (!ap_test_config_card_id(j))
1173 qid = AP_MKQID(j, i);
1174 rc = ap_query_queue(qid, &queue_depth, &device_type);
1179 if (count > max_count) {
1184 if (best_domain >= 0){
1185 ap_domain_index = best_domain;
1192 * ap_probe_device_type(): Find the device type of an AP.
1193 * @ap_dev: pointer to the AP device.
1195 * Find the device type if query queue returned a device type of 0.
1197 static int ap_probe_device_type(struct ap_device *ap_dev)
1199 static unsigned char msg[] = {
1200 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1201 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1202 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1203 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1204 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1205 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1206 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1207 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1208 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1209 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1210 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1211 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1212 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1213 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1214 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1215 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1216 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1217 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1218 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1219 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1220 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1221 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1222 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1223 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1224 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1225 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1226 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1227 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1228 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1229 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1230 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1231 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1232 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1233 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1234 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1235 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1236 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1237 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1238 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1239 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1240 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1241 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1242 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1243 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1244 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1246 struct ap_queue_status status;
1247 unsigned long long psmid;
1251 reply = (void *) get_zeroed_page(GFP_KERNEL);
1257 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1258 msg, sizeof(msg), 0);
1259 if (status.response_code != AP_RESPONSE_NORMAL) {
1264 /* Wait for the test message to complete. */
1265 for (i = 0; i < 6; i++) {
1267 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1268 if (status.response_code == AP_RESPONSE_NORMAL &&
1269 psmid == 0x0102030405060708ULL)
1273 /* Got an answer. */
1274 if (reply[0] == 0x00 && reply[1] == 0x86)
1275 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1277 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1283 free_page((unsigned long) reply);
1288 static void ap_interrupt_handler(struct airq_struct *airq)
1290 inc_irq_stat(IRQIO_APB);
1291 tasklet_schedule(&ap_tasklet);
1295 * __ap_scan_bus(): Scan the AP bus.
1296 * @dev: Pointer to device
1297 * @data: Pointer to data
1299 * Scan the AP bus for new devices.
1301 static int __ap_scan_bus(struct device *dev, void *data)
1303 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1306 static void ap_device_release(struct device *dev)
1308 struct ap_device *ap_dev = to_ap_dev(dev);
1313 static void ap_scan_bus(struct work_struct *unused)
1315 struct ap_device *ap_dev;
1318 int queue_depth, device_type;
1319 unsigned int device_functions;
1322 ap_query_configuration();
1323 if (ap_select_domain() != 0) {
1326 for (i = 0; i < AP_DEVICES; i++) {
1327 qid = AP_MKQID(i, ap_domain_index);
1328 dev = bus_find_device(&ap_bus_type, NULL,
1329 (void *)(unsigned long)qid,
1331 if (ap_test_config_card_id(i))
1332 rc = ap_query_queue(qid, &queue_depth, &device_type);
1337 set_current_state(TASK_UNINTERRUPTIBLE);
1338 schedule_timeout(AP_RESET_TIMEOUT);
1339 rc = ap_query_queue(qid, &queue_depth,
1342 ap_dev = to_ap_dev(dev);
1343 spin_lock_bh(&ap_dev->lock);
1344 if (rc || ap_dev->unregistered) {
1345 spin_unlock_bh(&ap_dev->lock);
1346 if (ap_dev->unregistered)
1348 device_unregister(dev);
1352 spin_unlock_bh(&ap_dev->lock);
1358 rc = ap_init_queue(qid);
1361 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1365 ap_dev->queue_depth = queue_depth;
1366 ap_dev->unregistered = 1;
1367 spin_lock_init(&ap_dev->lock);
1368 INIT_LIST_HEAD(&ap_dev->pendingq);
1369 INIT_LIST_HEAD(&ap_dev->requestq);
1370 INIT_LIST_HEAD(&ap_dev->list);
1371 setup_timer(&ap_dev->timeout, ap_request_timeout,
1372 (unsigned long) ap_dev);
1373 switch (device_type) {
1375 /* device type probing for old cards */
1376 if (ap_probe_device_type(ap_dev)) {
1382 ap_dev->device_type = device_type;
1385 rc = ap_query_functions(qid, &device_functions);
1387 ap_dev->functions = device_functions;
1389 ap_dev->functions = 0u;
1391 ap_dev->device.bus = &ap_bus_type;
1392 ap_dev->device.parent = ap_root_device;
1393 if (dev_set_name(&ap_dev->device, "card%02x",
1394 AP_QID_DEVICE(ap_dev->qid))) {
1398 ap_dev->device.release = ap_device_release;
1399 rc = device_register(&ap_dev->device);
1401 put_device(&ap_dev->device);
1404 /* Add device attributes. */
1405 rc = sysfs_create_group(&ap_dev->device.kobj,
1406 &ap_dev_attr_group);
1408 spin_lock_bh(&ap_dev->lock);
1409 ap_dev->unregistered = 0;
1410 spin_unlock_bh(&ap_dev->lock);
1413 device_unregister(&ap_dev->device);
1418 ap_config_timeout(unsigned long ptr)
1420 queue_work(ap_work_queue, &ap_config_work);
1421 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1422 add_timer(&ap_config_timer);
1426 * __ap_schedule_poll_timer(): Schedule poll timer.
1428 * Set up the timer to run the poll tasklet
1430 static inline void __ap_schedule_poll_timer(void)
1434 spin_lock_bh(&ap_poll_timer_lock);
1435 if (hrtimer_is_queued(&ap_poll_timer) || ap_suspend_flag)
1437 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1438 hr_time = ktime_set(0, poll_timeout);
1439 hrtimer_forward_now(&ap_poll_timer, hr_time);
1440 hrtimer_restart(&ap_poll_timer);
1443 spin_unlock_bh(&ap_poll_timer_lock);
1447 * ap_schedule_poll_timer(): Schedule poll timer.
1449 * Set up the timer to run the poll tasklet
1451 static inline void ap_schedule_poll_timer(void)
1453 if (ap_using_interrupts())
1455 __ap_schedule_poll_timer();
1459 * ap_poll_read(): Receive pending reply messages from an AP device.
1460 * @ap_dev: pointer to the AP device
1461 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1462 * required, bit 2^1 is set if the poll timer needs to get armed
1464 * Returns 0 if the device is still present, -ENODEV if not.
1466 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1468 struct ap_queue_status status;
1469 struct ap_message *ap_msg;
1471 if (ap_dev->queue_count <= 0)
1473 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1474 ap_dev->reply->message, ap_dev->reply->length);
1475 switch (status.response_code) {
1476 case AP_RESPONSE_NORMAL:
1477 atomic_dec(&ap_poll_requests);
1478 ap_decrease_queue_count(ap_dev);
1479 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1480 if (ap_msg->psmid != ap_dev->reply->psmid)
1482 list_del_init(&ap_msg->list);
1483 ap_dev->pendingq_count--;
1484 ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
1487 if (ap_dev->queue_count > 0)
1490 case AP_RESPONSE_NO_PENDING_REPLY:
1491 if (status.queue_empty) {
1492 /* The card shouldn't forget requests but who knows. */
1493 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1494 ap_dev->queue_count = 0;
1495 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1496 ap_dev->requestq_count += ap_dev->pendingq_count;
1497 ap_dev->pendingq_count = 0;
1508 * ap_poll_write(): Send messages from the request queue to an AP device.
1509 * @ap_dev: pointer to the AP device
1510 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1511 * required, bit 2^1 is set if the poll timer needs to get armed
1513 * Returns 0 if the device is still present, -ENODEV if not.
1515 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1517 struct ap_queue_status status;
1518 struct ap_message *ap_msg;
1520 if (ap_dev->requestq_count <= 0 ||
1521 ap_dev->queue_count >= ap_dev->queue_depth)
1523 /* Start the next request on the queue. */
1524 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1525 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1526 ap_msg->message, ap_msg->length, ap_msg->special);
1527 switch (status.response_code) {
1528 case AP_RESPONSE_NORMAL:
1529 atomic_inc(&ap_poll_requests);
1530 ap_increase_queue_count(ap_dev);
1531 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1532 ap_dev->requestq_count--;
1533 ap_dev->pendingq_count++;
1534 if (ap_dev->queue_count < ap_dev->queue_depth &&
1535 ap_dev->requestq_count > 0)
1539 case AP_RESPONSE_RESET_IN_PROGRESS:
1540 __ap_schedule_poll_timer();
1541 case AP_RESPONSE_Q_FULL:
1544 case AP_RESPONSE_MESSAGE_TOO_BIG:
1545 case AP_RESPONSE_REQ_FAC_NOT_INST:
1554 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1555 * @ap_dev: pointer to the bus device
1556 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1557 * required, bit 2^1 is set if the poll timer needs to get armed
1559 * Poll AP device for pending replies and send new messages. If either
1560 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1563 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1567 rc = ap_poll_read(ap_dev, flags);
1570 return ap_poll_write(ap_dev, flags);
1574 * __ap_queue_message(): Queue a message to a device.
1575 * @ap_dev: pointer to the AP device
1576 * @ap_msg: the message to be queued
1578 * Queue a message to a device. Returns 0 if successful.
1580 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1582 struct ap_queue_status status;
1584 if (list_empty(&ap_dev->requestq) &&
1585 ap_dev->queue_count < ap_dev->queue_depth) {
1586 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1587 ap_msg->message, ap_msg->length,
1589 switch (status.response_code) {
1590 case AP_RESPONSE_NORMAL:
1591 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1592 atomic_inc(&ap_poll_requests);
1593 ap_dev->pendingq_count++;
1594 ap_increase_queue_count(ap_dev);
1595 ap_dev->total_request_count++;
1597 case AP_RESPONSE_Q_FULL:
1598 case AP_RESPONSE_RESET_IN_PROGRESS:
1599 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1600 ap_dev->requestq_count++;
1601 ap_dev->total_request_count++;
1603 case AP_RESPONSE_REQ_FAC_NOT_INST:
1604 case AP_RESPONSE_MESSAGE_TOO_BIG:
1605 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1607 default: /* Device is gone. */
1608 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1612 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1613 ap_dev->requestq_count++;
1614 ap_dev->total_request_count++;
1617 ap_schedule_poll_timer();
1621 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1623 unsigned long flags;
1626 /* For asynchronous message handling a valid receive-callback
1628 BUG_ON(!ap_msg->receive);
1630 spin_lock_bh(&ap_dev->lock);
1631 if (!ap_dev->unregistered) {
1632 /* Make room on the queue by polling for finished requests. */
1633 rc = ap_poll_queue(ap_dev, &flags);
1635 rc = __ap_queue_message(ap_dev, ap_msg);
1637 wake_up(&ap_poll_wait);
1639 ap_dev->unregistered = 1;
1641 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1644 spin_unlock_bh(&ap_dev->lock);
1646 device_unregister(&ap_dev->device);
1648 EXPORT_SYMBOL(ap_queue_message);
1651 * ap_cancel_message(): Cancel a crypto request.
1652 * @ap_dev: The AP device that has the message queued
1653 * @ap_msg: The message that is to be removed
1655 * Cancel a crypto request. This is done by removing the request
1656 * from the device pending or request queue. Note that the
1657 * request stays on the AP queue. When it finishes the message
1658 * reply will be discarded because the psmid can't be found.
1660 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1662 struct ap_message *tmp;
1664 spin_lock_bh(&ap_dev->lock);
1665 if (!list_empty(&ap_msg->list)) {
1666 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1667 if (tmp->psmid == ap_msg->psmid) {
1668 ap_dev->pendingq_count--;
1671 ap_dev->requestq_count--;
1673 list_del_init(&ap_msg->list);
1675 spin_unlock_bh(&ap_dev->lock);
1677 EXPORT_SYMBOL(ap_cancel_message);
1680 * ap_poll_timeout(): AP receive polling for finished AP requests.
1681 * @unused: Unused pointer.
1683 * Schedules the AP tasklet using a high resolution timer.
1685 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1687 tasklet_schedule(&ap_tasklet);
1688 return HRTIMER_NORESTART;
1692 * ap_reset(): Reset a not responding AP device.
1693 * @ap_dev: Pointer to the AP device
1695 * Reset a not responding AP device and move all requests from the
1696 * pending queue to the request queue.
1698 static void ap_reset(struct ap_device *ap_dev)
1702 ap_dev->reset = AP_RESET_IGNORE;
1703 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1704 ap_dev->queue_count = 0;
1705 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1706 ap_dev->requestq_count += ap_dev->pendingq_count;
1707 ap_dev->pendingq_count = 0;
1708 rc = ap_init_queue(ap_dev->qid);
1710 ap_dev->unregistered = 1;
1712 __ap_schedule_poll_timer();
1715 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1717 if (!ap_dev->unregistered) {
1718 if (ap_poll_queue(ap_dev, flags))
1719 ap_dev->unregistered = 1;
1720 if (ap_dev->reset == AP_RESET_DO)
1727 * ap_poll_all(): Poll all AP devices.
1728 * @dummy: Unused variable
1730 * Poll all AP devices on the bus in a round robin fashion. Continue
1731 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1732 * of the control flags has been set arm the poll timer.
1734 static void ap_poll_all(unsigned long dummy)
1736 unsigned long flags;
1737 struct ap_device *ap_dev;
1739 /* Reset the indicator if interrupts are used. Thus new interrupts can
1740 * be received. Doing it in the beginning of the tasklet is therefor
1741 * important that no requests on any AP get lost.
1743 if (ap_using_interrupts())
1744 xchg(ap_airq.lsi_ptr, 0);
1747 spin_lock(&ap_device_list_lock);
1748 list_for_each_entry(ap_dev, &ap_device_list, list) {
1749 spin_lock(&ap_dev->lock);
1750 __ap_poll_device(ap_dev, &flags);
1751 spin_unlock(&ap_dev->lock);
1753 spin_unlock(&ap_device_list_lock);
1754 } while (flags & 1);
1756 ap_schedule_poll_timer();
1760 * ap_poll_thread(): Thread that polls for finished requests.
1761 * @data: Unused pointer
1763 * AP bus poll thread. The purpose of this thread is to poll for
1764 * finished requests in a loop if there is a "free" cpu - that is
1765 * a cpu that doesn't have anything better to do. The polling stops
1766 * as soon as there is another task or if all messages have been
1769 static int ap_poll_thread(void *data)
1771 DECLARE_WAITQUEUE(wait, current);
1772 unsigned long flags;
1774 struct ap_device *ap_dev;
1776 set_user_nice(current, 19);
1778 if (ap_suspend_flag)
1780 if (need_resched()) {
1784 add_wait_queue(&ap_poll_wait, &wait);
1785 set_current_state(TASK_INTERRUPTIBLE);
1786 if (kthread_should_stop())
1788 requests = atomic_read(&ap_poll_requests);
1791 set_current_state(TASK_RUNNING);
1792 remove_wait_queue(&ap_poll_wait, &wait);
1795 spin_lock_bh(&ap_device_list_lock);
1796 list_for_each_entry(ap_dev, &ap_device_list, list) {
1797 spin_lock(&ap_dev->lock);
1798 __ap_poll_device(ap_dev, &flags);
1799 spin_unlock(&ap_dev->lock);
1801 spin_unlock_bh(&ap_device_list_lock);
1803 set_current_state(TASK_RUNNING);
1804 remove_wait_queue(&ap_poll_wait, &wait);
1808 static int ap_poll_thread_start(void)
1812 if (ap_using_interrupts() || ap_suspend_flag)
1814 mutex_lock(&ap_poll_thread_mutex);
1815 if (!ap_poll_kthread) {
1816 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1817 rc = PTR_RET(ap_poll_kthread);
1819 ap_poll_kthread = NULL;
1823 mutex_unlock(&ap_poll_thread_mutex);
1827 static void ap_poll_thread_stop(void)
1829 mutex_lock(&ap_poll_thread_mutex);
1830 if (ap_poll_kthread) {
1831 kthread_stop(ap_poll_kthread);
1832 ap_poll_kthread = NULL;
1834 mutex_unlock(&ap_poll_thread_mutex);
1838 * ap_request_timeout(): Handling of request timeouts
1839 * @data: Holds the AP device.
1841 * Handles request timeouts.
1843 static void ap_request_timeout(unsigned long data)
1845 struct ap_device *ap_dev = (struct ap_device *) data;
1847 if (ap_dev->reset == AP_RESET_ARMED) {
1848 ap_dev->reset = AP_RESET_DO;
1850 if (ap_using_interrupts())
1851 tasklet_schedule(&ap_tasklet);
1855 static void ap_reset_domain(void)
1859 if (ap_domain_index != -1)
1860 for (i = 0; i < AP_DEVICES; i++)
1861 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1864 static void ap_reset_all(void)
1868 for (i = 0; i < AP_DOMAINS; i++)
1869 for (j = 0; j < AP_DEVICES; j++)
1870 ap_reset_queue(AP_MKQID(j, i));
1873 static struct reset_call ap_reset_call = {
1878 * ap_module_init(): The module initialization code.
1880 * Initializes the module.
1882 int __init ap_module_init(void)
1886 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1887 pr_warning("%d is not a valid cryptographic domain\n",
1891 /* In resume callback we need to know if the user had set the domain.
1892 * If so, we can not just reset it.
1894 if (ap_domain_index >= 0)
1895 user_set_domain = 1;
1897 if (ap_instructions_available() != 0) {
1898 pr_warning("The hardware system does not support "
1899 "AP instructions\n");
1902 if (ap_interrupts_available()) {
1903 rc = register_adapter_interrupt(&ap_airq);
1904 ap_airq_flag = (rc == 0);
1907 register_reset_call(&ap_reset_call);
1909 /* Create /sys/bus/ap. */
1910 rc = bus_register(&ap_bus_type);
1913 for (i = 0; ap_bus_attrs[i]; i++) {
1914 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1919 /* Create /sys/devices/ap. */
1920 ap_root_device = root_device_register("ap");
1921 rc = PTR_RET(ap_root_device);
1925 ap_work_queue = create_singlethread_workqueue("kapwork");
1926 if (!ap_work_queue) {
1931 ap_query_configuration();
1932 if (ap_select_domain() == 0)
1935 /* Setup the AP bus rescan timer. */
1936 init_timer(&ap_config_timer);
1937 ap_config_timer.function = ap_config_timeout;
1938 ap_config_timer.data = 0;
1939 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1940 add_timer(&ap_config_timer);
1942 /* Setup the high resultion poll timer.
1943 * If we are running under z/VM adjust polling to z/VM polling rate.
1946 poll_timeout = 1500000;
1947 spin_lock_init(&ap_poll_timer_lock);
1948 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1949 ap_poll_timer.function = ap_poll_timeout;
1951 /* Start the low priority AP bus poll thread. */
1952 if (ap_thread_flag) {
1953 rc = ap_poll_thread_start();
1961 del_timer_sync(&ap_config_timer);
1962 hrtimer_cancel(&ap_poll_timer);
1963 destroy_workqueue(ap_work_queue);
1965 root_device_unregister(ap_root_device);
1968 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1969 bus_unregister(&ap_bus_type);
1971 unregister_reset_call(&ap_reset_call);
1972 if (ap_using_interrupts())
1973 unregister_adapter_interrupt(&ap_airq);
1977 static int __ap_match_all(struct device *dev, void *data)
1983 * ap_modules_exit(): The module termination code
1985 * Terminates the module.
1987 void ap_module_exit(void)
1993 ap_poll_thread_stop();
1994 del_timer_sync(&ap_config_timer);
1995 hrtimer_cancel(&ap_poll_timer);
1996 destroy_workqueue(ap_work_queue);
1997 tasklet_kill(&ap_tasklet);
1998 root_device_unregister(ap_root_device);
1999 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
2002 device_unregister(dev);
2005 for (i = 0; ap_bus_attrs[i]; i++)
2006 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
2007 bus_unregister(&ap_bus_type);
2008 unregister_reset_call(&ap_reset_call);
2009 if (ap_using_interrupts())
2010 unregister_adapter_interrupt(&ap_airq);
2013 module_init(ap_module_init);
2014 module_exit(ap_module_exit);