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>
47 #include <linux/crypto.h>
51 /* Some prototypes. */
52 static void ap_scan_bus(struct work_struct *);
53 static void ap_poll_all(unsigned long);
54 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
55 static int ap_poll_thread_start(void);
56 static void ap_poll_thread_stop(void);
57 static void ap_request_timeout(unsigned long);
58 static inline void ap_schedule_poll_timer(void);
59 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
60 static int ap_device_remove(struct device *dev);
61 static int ap_device_probe(struct device *dev);
62 static void ap_interrupt_handler(struct airq_struct *airq);
63 static void ap_reset(struct ap_device *ap_dev);
64 static void ap_config_timeout(unsigned long ptr);
65 static int ap_select_domain(void);
66 static void ap_query_configuration(void);
71 MODULE_AUTHOR("IBM Corporation");
72 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
73 "Copyright IBM Corp. 2006, 2012");
74 MODULE_LICENSE("GPL");
75 MODULE_ALIAS_CRYPTO("z90crypt");
80 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
81 module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
82 MODULE_PARM_DESC(domain, "domain index for ap devices");
83 EXPORT_SYMBOL(ap_domain_index);
85 static int ap_thread_flag = 0;
86 module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
87 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
89 static struct device *ap_root_device = NULL;
90 static struct ap_config_info *ap_configuration;
91 static DEFINE_SPINLOCK(ap_device_list_lock);
92 static LIST_HEAD(ap_device_list);
95 * Workqueue & timer for bus rescan.
97 static struct workqueue_struct *ap_work_queue;
98 static struct timer_list ap_config_timer;
99 static int ap_config_time = AP_CONFIG_TIME;
100 static DECLARE_WORK(ap_config_work, ap_scan_bus);
103 * Tasklet & timer for AP request polling and interrupts
105 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
106 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
107 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
108 static struct task_struct *ap_poll_kthread = NULL;
109 static DEFINE_MUTEX(ap_poll_thread_mutex);
110 static DEFINE_SPINLOCK(ap_poll_timer_lock);
111 static struct hrtimer ap_poll_timer;
112 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
113 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
114 static unsigned long long poll_timeout = 250000;
117 static int ap_suspend_flag;
118 /* Flag to check if domain was set through module parameter domain=. This is
119 * important when supsend and resume is done in a z/VM environment where the
120 * domain might change. */
121 static int user_set_domain = 0;
122 static struct bus_type ap_bus_type;
124 /* Adapter interrupt definitions */
125 static int ap_airq_flag;
127 static struct airq_struct ap_airq = {
128 .handler = ap_interrupt_handler,
133 * ap_using_interrupts() - Returns non-zero if interrupt support is
136 static inline int ap_using_interrupts(void)
142 * ap_intructions_available() - Test if AP instructions are available.
144 * Returns 0 if the AP instructions are installed.
146 static inline int ap_instructions_available(void)
148 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
149 register unsigned long reg1 asm ("1") = -ENODEV;
150 register unsigned long reg2 asm ("2") = 0UL;
153 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
157 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
162 * ap_interrupts_available(): Test if AP interrupts are available.
164 * Returns 1 if AP interrupts are available.
166 static int ap_interrupts_available(void)
168 return test_facility(65);
172 * ap_configuration_available(): Test if AP configuration
173 * information is available.
175 * Returns 1 if AP configuration information is available.
177 static int ap_configuration_available(void)
179 return 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_query_facilities(): PQAP(TAPQ) query facilities.
206 * @qid: The AP queue number
208 * Returns content of general register 2 after the PQAP(TAPQ)
209 * instruction was called.
211 static inline unsigned long ap_query_facilities(ap_qid_t qid)
213 register unsigned long reg0 asm ("0") = qid | 0x00800000UL;
214 register unsigned long reg1 asm ("1");
215 register unsigned long reg2 asm ("2") = 0UL;
217 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
218 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
223 * ap_reset_queue(): Reset adjunct processor queue.
224 * @qid: The AP queue number
226 * Returns AP queue status structure.
228 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
230 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
231 register struct ap_queue_status reg1 asm ("1");
232 register unsigned long reg2 asm ("2") = 0UL;
235 ".long 0xb2af0000" /* PQAP(RAPQ) */
236 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
241 * ap_queue_interruption_control(): Enable interruption for a specific AP.
242 * @qid: The AP queue number
243 * @ind: The notification indicator byte
245 * Returns AP queue status.
247 static inline struct ap_queue_status
248 ap_queue_interruption_control(ap_qid_t qid, void *ind)
250 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
251 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
252 register struct ap_queue_status reg1_out asm ("1");
253 register void *reg2 asm ("2") = ind;
255 ".long 0xb2af0000" /* PQAP(AQIC) */
256 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
262 static inline struct ap_queue_status
263 __ap_query_functions(ap_qid_t qid, unsigned int *functions)
265 register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
266 register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
267 register unsigned long reg2 asm ("2");
270 ".long 0xb2af0000\n" /* PQAP(TAPQ) */
273 : "+d" (reg0), "+d" (reg1), "=d" (reg2)
277 *functions = (unsigned int)(reg2 >> 32);
281 static inline int __ap_query_configuration(struct ap_config_info *config)
283 register unsigned long reg0 asm ("0") = 0x04000000UL;
284 register unsigned long reg1 asm ("1") = -EINVAL;
285 register unsigned char *reg2 asm ("2") = (unsigned char *)config;
288 ".long 0xb2af0000\n" /* PQAP(QCI) */
292 : "+d" (reg0), "+d" (reg1), "+d" (reg2)
300 * ap_query_functions(): Query supported functions.
301 * @qid: The AP queue number
302 * @functions: Pointer to functions field.
306 * -ENODEV if queue not valid.
307 * -EBUSY if device busy.
308 * -EINVAL if query function is not supported
310 static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
312 struct ap_queue_status status;
314 status = __ap_query_functions(qid, functions);
316 for (i = 0; i < AP_MAX_RESET; i++) {
317 if (ap_queue_status_invalid_test(&status))
320 switch (status.response_code) {
321 case AP_RESPONSE_NORMAL:
323 case AP_RESPONSE_RESET_IN_PROGRESS:
324 case AP_RESPONSE_BUSY:
326 case AP_RESPONSE_Q_NOT_AVAIL:
327 case AP_RESPONSE_DECONFIGURED:
328 case AP_RESPONSE_CHECKSTOPPED:
329 case AP_RESPONSE_INVALID_ADDRESS:
331 case AP_RESPONSE_OTHERWISE_CHANGED:
336 if (i < AP_MAX_RESET - 1) {
338 status = __ap_query_functions(qid, functions);
345 * ap_queue_enable_interruption(): Enable interruption on an AP.
346 * @qid: The AP queue number
347 * @ind: the notification indicator byte
349 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
350 * on the return value it waits a while and tests the AP queue if interrupts
351 * have been switched on using ap_test_queue().
353 static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
355 struct ap_queue_status status;
356 int t_depth, t_device_type, rc, i;
359 status = ap_queue_interruption_control(qid, ind);
361 for (i = 0; i < AP_MAX_RESET; i++) {
362 switch (status.response_code) {
363 case AP_RESPONSE_NORMAL:
364 if (status.int_enabled)
367 case AP_RESPONSE_RESET_IN_PROGRESS:
368 case AP_RESPONSE_BUSY:
369 if (i < AP_MAX_RESET - 1) {
371 status = ap_queue_interruption_control(qid,
376 case AP_RESPONSE_Q_NOT_AVAIL:
377 case AP_RESPONSE_DECONFIGURED:
378 case AP_RESPONSE_CHECKSTOPPED:
379 case AP_RESPONSE_INVALID_ADDRESS:
381 case AP_RESPONSE_OTHERWISE_CHANGED:
382 if (status.int_enabled)
388 if (i < AP_MAX_RESET - 1) {
390 status = ap_test_queue(qid, &t_depth, &t_device_type);
397 * __ap_send(): Send message to adjunct processor queue.
398 * @qid: The AP queue number
399 * @psmid: The program supplied message identifier
400 * @msg: The message text
401 * @length: The message length
402 * @special: Special Bit
404 * Returns AP queue status structure.
405 * Condition code 1 on NQAP can't happen because the L bit is 1.
406 * Condition code 2 on NQAP also means the send is incomplete,
407 * because a segment boundary was reached. The NQAP is repeated.
409 static inline struct ap_queue_status
410 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
411 unsigned int special)
413 typedef struct { char _[length]; } msgblock;
414 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
415 register struct ap_queue_status reg1 asm ("1");
416 register unsigned long reg2 asm ("2") = (unsigned long) msg;
417 register unsigned long reg3 asm ("3") = (unsigned long) length;
418 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
419 register unsigned long reg5 asm ("5") = psmid & 0xffffffff;
425 "0: .long 0xb2ad0042\n" /* NQAP */
427 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
428 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
433 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
435 struct ap_queue_status status;
437 status = __ap_send(qid, psmid, msg, length, 0);
438 switch (status.response_code) {
439 case AP_RESPONSE_NORMAL:
441 case AP_RESPONSE_Q_FULL:
442 case AP_RESPONSE_RESET_IN_PROGRESS:
444 case AP_RESPONSE_REQ_FAC_NOT_INST:
446 default: /* Device is gone. */
450 EXPORT_SYMBOL(ap_send);
453 * __ap_recv(): Receive message from adjunct processor queue.
454 * @qid: The AP queue number
455 * @psmid: Pointer to program supplied message identifier
456 * @msg: The message text
457 * @length: The message length
459 * Returns AP queue status structure.
460 * Condition code 1 on DQAP means the receive has taken place
461 * but only partially. The response is incomplete, hence the
463 * Condition code 2 on DQAP also means the receive is incomplete,
464 * this time because a segment boundary was reached. Again, the
466 * Note that gpr2 is used by the DQAP instruction to keep track of
467 * any 'residual' length, in case the instruction gets interrupted.
468 * Hence it gets zeroed before the instruction.
470 static inline struct ap_queue_status
471 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
473 typedef struct { char _[length]; } msgblock;
474 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
475 register struct ap_queue_status reg1 asm ("1");
476 register unsigned long reg2 asm("2") = 0UL;
477 register unsigned long reg4 asm("4") = (unsigned long) msg;
478 register unsigned long reg5 asm("5") = (unsigned long) length;
479 register unsigned long reg6 asm("6") = 0UL;
480 register unsigned long reg7 asm("7") = 0UL;
484 "0: .long 0xb2ae0064\n" /* DQAP */
486 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
487 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
488 "=m" (*(msgblock *) msg) : : "cc" );
489 *psmid = (((unsigned long long) reg6) << 32) + reg7;
493 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
495 struct ap_queue_status status;
497 status = __ap_recv(qid, psmid, msg, length);
498 switch (status.response_code) {
499 case AP_RESPONSE_NORMAL:
501 case AP_RESPONSE_NO_PENDING_REPLY:
502 if (status.queue_empty)
505 case AP_RESPONSE_RESET_IN_PROGRESS:
511 EXPORT_SYMBOL(ap_recv);
514 * ap_query_queue(): Check if an AP queue is available.
515 * @qid: The AP queue number
516 * @queue_depth: Pointer to queue depth value
517 * @device_type: Pointer to device type value
519 * The test is repeated for AP_MAX_RESET times.
521 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
523 struct ap_queue_status status;
524 int t_depth, t_device_type, rc, i;
527 for (i = 0; i < AP_MAX_RESET; i++) {
528 status = ap_test_queue(qid, &t_depth, &t_device_type);
529 switch (status.response_code) {
530 case AP_RESPONSE_NORMAL:
531 *queue_depth = t_depth + 1;
532 *device_type = t_device_type;
535 case AP_RESPONSE_Q_NOT_AVAIL:
538 case AP_RESPONSE_RESET_IN_PROGRESS:
540 case AP_RESPONSE_DECONFIGURED:
543 case AP_RESPONSE_CHECKSTOPPED:
546 case AP_RESPONSE_INVALID_ADDRESS:
549 case AP_RESPONSE_OTHERWISE_CHANGED:
551 case AP_RESPONSE_BUSY:
558 if (i < AP_MAX_RESET - 1)
565 * ap_init_queue(): Reset an AP queue.
566 * @qid: The AP queue number
568 * Reset an AP queue and wait for it to become available again.
570 static int ap_init_queue(ap_qid_t qid)
572 struct ap_queue_status status;
576 status = ap_reset_queue(qid);
577 for (i = 0; i < AP_MAX_RESET; i++) {
578 switch (status.response_code) {
579 case AP_RESPONSE_NORMAL:
580 if (status.queue_empty)
583 case AP_RESPONSE_Q_NOT_AVAIL:
584 case AP_RESPONSE_DECONFIGURED:
585 case AP_RESPONSE_CHECKSTOPPED:
586 i = AP_MAX_RESET; /* return with -ENODEV */
588 case AP_RESPONSE_RESET_IN_PROGRESS:
590 case AP_RESPONSE_BUSY:
594 if (rc != -ENODEV && rc != -EBUSY)
596 if (i < AP_MAX_RESET - 1) {
597 /* Time we are waiting until we give up (0.7sec * 90).
598 * Since the actual request (in progress) will not
599 * interrupted immediately for the reset command,
600 * we have to be patient. In worst case we have to
601 * wait 60sec + reset time (some msec).
603 schedule_timeout(AP_RESET_TIMEOUT);
604 status = ap_test_queue(qid, &dummy, &dummy);
607 if (rc == 0 && ap_using_interrupts()) {
608 rc = ap_queue_enable_interruption(qid, ap_airq.lsi_ptr);
609 /* If interruption mode is supported by the machine,
610 * but an AP can not be enabled for interruption then
611 * the AP will be discarded. */
613 pr_err("Registering adapter interrupts for "
614 "AP %d failed\n", AP_QID_DEVICE(qid));
620 * ap_increase_queue_count(): Arm request timeout.
621 * @ap_dev: Pointer to an AP device.
623 * Arm request timeout if an AP device was idle and a new request is submitted.
625 static void ap_increase_queue_count(struct ap_device *ap_dev)
627 int timeout = ap_dev->drv->request_timeout;
629 ap_dev->queue_count++;
630 if (ap_dev->queue_count == 1) {
631 mod_timer(&ap_dev->timeout, jiffies + timeout);
632 ap_dev->reset = AP_RESET_ARMED;
637 * ap_decrease_queue_count(): Decrease queue count.
638 * @ap_dev: Pointer to an AP device.
640 * If AP device is still alive, re-schedule request timeout if there are still
643 static void ap_decrease_queue_count(struct ap_device *ap_dev)
645 int timeout = ap_dev->drv->request_timeout;
647 ap_dev->queue_count--;
648 if (ap_dev->queue_count > 0)
649 mod_timer(&ap_dev->timeout, jiffies + timeout);
652 * The timeout timer should to be disabled now - since
653 * del_timer_sync() is very expensive, we just tell via the
654 * reset flag to ignore the pending timeout timer.
656 ap_dev->reset = AP_RESET_IGNORE;
660 * AP device related attributes.
662 static ssize_t ap_hwtype_show(struct device *dev,
663 struct device_attribute *attr, char *buf)
665 struct ap_device *ap_dev = to_ap_dev(dev);
666 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
669 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
671 static ssize_t ap_raw_hwtype_show(struct device *dev,
672 struct device_attribute *attr, char *buf)
674 struct ap_device *ap_dev = to_ap_dev(dev);
676 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->raw_hwtype);
679 static DEVICE_ATTR(raw_hwtype, 0444, ap_raw_hwtype_show, NULL);
681 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
684 struct ap_device *ap_dev = to_ap_dev(dev);
685 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
688 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
689 static ssize_t ap_request_count_show(struct device *dev,
690 struct device_attribute *attr,
693 struct ap_device *ap_dev = to_ap_dev(dev);
696 spin_lock_bh(&ap_dev->lock);
697 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
698 spin_unlock_bh(&ap_dev->lock);
702 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
704 static ssize_t ap_requestq_count_show(struct device *dev,
705 struct device_attribute *attr, char *buf)
707 struct ap_device *ap_dev = to_ap_dev(dev);
710 spin_lock_bh(&ap_dev->lock);
711 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
712 spin_unlock_bh(&ap_dev->lock);
716 static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
718 static ssize_t ap_pendingq_count_show(struct device *dev,
719 struct device_attribute *attr, char *buf)
721 struct ap_device *ap_dev = to_ap_dev(dev);
724 spin_lock_bh(&ap_dev->lock);
725 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
726 spin_unlock_bh(&ap_dev->lock);
730 static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
732 static ssize_t ap_modalias_show(struct device *dev,
733 struct device_attribute *attr, char *buf)
735 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
738 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
740 static ssize_t ap_functions_show(struct device *dev,
741 struct device_attribute *attr, char *buf)
743 struct ap_device *ap_dev = to_ap_dev(dev);
744 return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
747 static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
749 static struct attribute *ap_dev_attrs[] = {
750 &dev_attr_hwtype.attr,
751 &dev_attr_raw_hwtype.attr,
752 &dev_attr_depth.attr,
753 &dev_attr_request_count.attr,
754 &dev_attr_requestq_count.attr,
755 &dev_attr_pendingq_count.attr,
756 &dev_attr_modalias.attr,
757 &dev_attr_ap_functions.attr,
760 static struct attribute_group ap_dev_attr_group = {
761 .attrs = ap_dev_attrs
766 * @dev: Pointer to device
767 * @drv: Pointer to device_driver
769 * AP bus driver registration/unregistration.
771 static int ap_bus_match(struct device *dev, struct device_driver *drv)
773 struct ap_device *ap_dev = to_ap_dev(dev);
774 struct ap_driver *ap_drv = to_ap_drv(drv);
775 struct ap_device_id *id;
778 * Compare device type of the device with the list of
779 * supported types of the device_driver.
781 for (id = ap_drv->ids; id->match_flags; id++) {
782 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
783 (id->dev_type != ap_dev->device_type))
791 * ap_uevent(): Uevent function for AP devices.
792 * @dev: Pointer to device
793 * @env: Pointer to kobj_uevent_env
795 * It sets up a single environment variable DEV_TYPE which contains the
796 * hardware device type.
798 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
800 struct ap_device *ap_dev = to_ap_dev(dev);
806 /* Set up DEV_TYPE environment variable. */
807 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
812 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
817 static int ap_bus_suspend(struct device *dev, pm_message_t state)
819 struct ap_device *ap_dev = to_ap_dev(dev);
822 if (!ap_suspend_flag) {
825 /* Disable scanning for devices, thus we do not want to scan
826 * for them after removing.
828 del_timer_sync(&ap_config_timer);
829 if (ap_work_queue != NULL) {
830 destroy_workqueue(ap_work_queue);
831 ap_work_queue = NULL;
834 tasklet_disable(&ap_tasklet);
836 /* Poll on the device until all requests are finished. */
839 spin_lock_bh(&ap_dev->lock);
840 __ap_poll_device(ap_dev, &flags);
841 spin_unlock_bh(&ap_dev->lock);
842 } while ((flags & 1) || (flags & 2));
844 spin_lock_bh(&ap_dev->lock);
845 ap_dev->unregistered = 1;
846 spin_unlock_bh(&ap_dev->lock);
851 static int ap_bus_resume(struct device *dev)
853 struct ap_device *ap_dev = to_ap_dev(dev);
856 if (ap_suspend_flag) {
858 if (ap_interrupts_available()) {
859 if (!ap_using_interrupts()) {
860 rc = register_adapter_interrupt(&ap_airq);
861 ap_airq_flag = (rc == 0);
864 if (ap_using_interrupts()) {
865 unregister_adapter_interrupt(&ap_airq);
869 ap_query_configuration();
870 if (!user_set_domain) {
871 ap_domain_index = -1;
874 init_timer(&ap_config_timer);
875 ap_config_timer.function = ap_config_timeout;
876 ap_config_timer.data = 0;
877 ap_config_timer.expires = jiffies + ap_config_time * HZ;
878 add_timer(&ap_config_timer);
879 ap_work_queue = create_singlethread_workqueue("kapwork");
882 tasklet_enable(&ap_tasklet);
883 if (!ap_using_interrupts())
884 ap_schedule_poll_timer();
886 tasklet_schedule(&ap_tasklet);
888 rc = ap_poll_thread_start();
893 if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
894 spin_lock_bh(&ap_dev->lock);
895 ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
897 spin_unlock_bh(&ap_dev->lock);
899 queue_work(ap_work_queue, &ap_config_work);
904 static struct bus_type ap_bus_type = {
906 .match = &ap_bus_match,
907 .uevent = &ap_uevent,
908 .suspend = ap_bus_suspend,
909 .resume = ap_bus_resume
912 static int ap_device_probe(struct device *dev)
914 struct ap_device *ap_dev = to_ap_dev(dev);
915 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
918 ap_dev->drv = ap_drv;
920 spin_lock_bh(&ap_device_list_lock);
921 list_add(&ap_dev->list, &ap_device_list);
922 spin_unlock_bh(&ap_device_list_lock);
924 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
926 spin_lock_bh(&ap_device_list_lock);
927 list_del_init(&ap_dev->list);
928 spin_unlock_bh(&ap_device_list_lock);
934 * __ap_flush_queue(): Flush requests.
935 * @ap_dev: Pointer to the AP device
937 * Flush all requests from the request/pending queue of an AP device.
939 static void __ap_flush_queue(struct ap_device *ap_dev)
941 struct ap_message *ap_msg, *next;
943 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
944 list_del_init(&ap_msg->list);
945 ap_dev->pendingq_count--;
946 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
948 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
949 list_del_init(&ap_msg->list);
950 ap_dev->requestq_count--;
951 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
955 void ap_flush_queue(struct ap_device *ap_dev)
957 spin_lock_bh(&ap_dev->lock);
958 __ap_flush_queue(ap_dev);
959 spin_unlock_bh(&ap_dev->lock);
961 EXPORT_SYMBOL(ap_flush_queue);
963 static int ap_device_remove(struct device *dev)
965 struct ap_device *ap_dev = to_ap_dev(dev);
966 struct ap_driver *ap_drv = ap_dev->drv;
968 ap_flush_queue(ap_dev);
969 del_timer_sync(&ap_dev->timeout);
970 spin_lock_bh(&ap_device_list_lock);
971 list_del_init(&ap_dev->list);
972 spin_unlock_bh(&ap_device_list_lock);
974 ap_drv->remove(ap_dev);
975 spin_lock_bh(&ap_dev->lock);
976 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
977 spin_unlock_bh(&ap_dev->lock);
981 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
984 struct device_driver *drv = &ap_drv->driver;
986 drv->bus = &ap_bus_type;
987 drv->probe = ap_device_probe;
988 drv->remove = ap_device_remove;
991 return driver_register(drv);
993 EXPORT_SYMBOL(ap_driver_register);
995 void ap_driver_unregister(struct ap_driver *ap_drv)
997 driver_unregister(&ap_drv->driver);
999 EXPORT_SYMBOL(ap_driver_unregister);
1001 void ap_bus_force_rescan(void)
1003 /* reconfigure the AP bus rescan timer. */
1004 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1005 /* processing a asynchronous bus rescan */
1006 queue_work(ap_work_queue, &ap_config_work);
1007 flush_work(&ap_config_work);
1009 EXPORT_SYMBOL(ap_bus_force_rescan);
1012 * ap_test_config(): helper function to extract the nrth bit
1013 * within the unsigned int array field.
1015 static inline int ap_test_config(unsigned int *field, unsigned int nr)
1019 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
1023 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
1026 * Returns 0 if the card is not configured
1027 * 1 if the card is configured or
1028 * if the configuration information is not available
1030 static inline int ap_test_config_card_id(unsigned int id)
1032 if (!ap_configuration)
1034 return ap_test_config(ap_configuration->apm, id);
1038 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
1039 * @domain AP usage domain ID
1041 * Returns 0 if the usage domain is not configured
1042 * 1 if the usage domain is configured or
1043 * if the configuration information is not available
1045 static inline int ap_test_config_domain(unsigned int domain)
1047 if (!ap_configuration) /* QCI not supported */
1049 return 1; /* then domains 0...15 are configured */
1053 return ap_test_config(ap_configuration->aqm, domain);
1057 * AP bus attributes.
1059 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1061 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1064 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
1066 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1068 if (ap_configuration != NULL) { /* QCI not supported */
1069 if (test_facility(76)) { /* format 1 - 256 bit domain field */
1070 return snprintf(buf, PAGE_SIZE,
1071 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1072 ap_configuration->adm[0], ap_configuration->adm[1],
1073 ap_configuration->adm[2], ap_configuration->adm[3],
1074 ap_configuration->adm[4], ap_configuration->adm[5],
1075 ap_configuration->adm[6], ap_configuration->adm[7]);
1076 } else { /* format 0 - 16 bit domain field */
1077 return snprintf(buf, PAGE_SIZE, "%08x%08x\n",
1078 ap_configuration->adm[0], ap_configuration->adm[1]);
1081 return snprintf(buf, PAGE_SIZE, "not supported\n");
1085 static BUS_ATTR(ap_control_domain_mask, 0444,
1086 ap_control_domain_mask_show, NULL);
1088 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
1090 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1093 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1095 return snprintf(buf, PAGE_SIZE, "%d\n",
1096 ap_using_interrupts() ? 1 : 0);
1099 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
1101 static ssize_t ap_config_time_store(struct bus_type *bus,
1102 const char *buf, size_t count)
1106 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1108 ap_config_time = time;
1109 if (!timer_pending(&ap_config_timer) ||
1110 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
1111 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1112 add_timer(&ap_config_timer);
1117 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
1119 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
1121 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1124 static ssize_t ap_poll_thread_store(struct bus_type *bus,
1125 const char *buf, size_t count)
1129 if (sscanf(buf, "%d\n", &flag) != 1)
1132 rc = ap_poll_thread_start();
1137 ap_poll_thread_stop();
1141 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
1143 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1145 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1148 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1151 unsigned long long time;
1154 /* 120 seconds = maximum poll interval */
1155 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1156 time > 120000000000ULL)
1158 poll_timeout = time;
1159 hr_time = ktime_set(0, poll_timeout);
1161 spin_lock_bh(&ap_poll_timer_lock);
1162 hrtimer_cancel(&ap_poll_timer);
1163 hrtimer_set_expires(&ap_poll_timer, hr_time);
1164 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1165 spin_unlock_bh(&ap_poll_timer_lock);
1170 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
1172 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1175 int i, nd, max_domain_id = -1;
1176 unsigned long fbits;
1178 if (ap_configuration) {
1179 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS) {
1180 for (i = 0; i < AP_DEVICES; i++) {
1181 if (!ap_test_config_card_id(i))
1183 qid = AP_MKQID(i, ap_domain_index);
1184 fbits = ap_query_facilities(qid);
1185 if (fbits & (1UL << 57)) {
1186 /* the N bit is 0, Nd field is filled */
1187 nd = (int)((fbits & 0x00FF0000UL)>>16);
1193 /* N bit is 1, max 16 domains */
1200 /* no APXA support, older machines with max 16 domains */
1203 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1206 static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
1208 static struct bus_attribute *const ap_bus_attrs[] = {
1209 &bus_attr_ap_domain,
1210 &bus_attr_ap_control_domain_mask,
1211 &bus_attr_config_time,
1212 &bus_attr_poll_thread,
1213 &bus_attr_ap_interrupts,
1214 &bus_attr_poll_timeout,
1215 &bus_attr_ap_max_domain_id,
1220 * ap_query_configuration(): Query AP configuration information.
1222 * Query information of installed cards and configured domains from AP.
1224 static void ap_query_configuration(void)
1226 if (ap_configuration_available()) {
1227 if (!ap_configuration)
1229 kzalloc(sizeof(struct ap_config_info),
1231 if (ap_configuration)
1232 __ap_query_configuration(ap_configuration);
1234 ap_configuration = NULL;
1238 * ap_select_domain(): Select an AP domain.
1240 * Pick one of the 16 AP domains.
1242 static int ap_select_domain(void)
1244 int queue_depth, device_type, count, max_count, best_domain;
1248 /* IF APXA isn't installed, only 16 domains could be defined */
1249 if (!ap_configuration->ap_extended && (ap_domain_index > 15))
1253 * We want to use a single domain. Either the one specified with
1254 * the "domain=" parameter or the domain with the maximum number
1257 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
1258 /* Domain has already been selected. */
1262 for (i = 0; i < AP_DOMAINS; i++) {
1263 if (!ap_test_config_domain(i))
1266 for (j = 0; j < AP_DEVICES; j++) {
1267 if (!ap_test_config_card_id(j))
1269 qid = AP_MKQID(j, i);
1270 rc = ap_query_queue(qid, &queue_depth, &device_type);
1275 if (count > max_count) {
1280 if (best_domain >= 0){
1281 ap_domain_index = best_domain;
1288 * ap_probe_device_type(): Find the device type of an AP.
1289 * @ap_dev: pointer to the AP device.
1291 * Find the device type if query queue returned a device type of 0.
1293 static int ap_probe_device_type(struct ap_device *ap_dev)
1295 static unsigned char msg[] = {
1296 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1297 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1298 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1299 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1300 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1301 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1302 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1303 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1304 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1305 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1306 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1307 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1308 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1309 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1310 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1311 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1312 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1313 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1314 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1315 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1316 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1317 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1318 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1319 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1320 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1321 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1322 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1323 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1324 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1325 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1326 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1327 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1328 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1329 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1330 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1331 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1332 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1333 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1334 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1335 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1336 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1337 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1338 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1339 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1340 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1342 struct ap_queue_status status;
1343 unsigned long long psmid;
1347 reply = (void *) get_zeroed_page(GFP_KERNEL);
1353 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1354 msg, sizeof(msg), 0);
1355 if (status.response_code != AP_RESPONSE_NORMAL) {
1360 /* Wait for the test message to complete. */
1361 for (i = 0; i < 6; i++) {
1363 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1364 if (status.response_code == AP_RESPONSE_NORMAL &&
1365 psmid == 0x0102030405060708ULL)
1369 /* Got an answer. */
1370 if (reply[0] == 0x00 && reply[1] == 0x86)
1371 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1373 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1379 free_page((unsigned long) reply);
1384 static void ap_interrupt_handler(struct airq_struct *airq)
1386 inc_irq_stat(IRQIO_APB);
1387 tasklet_schedule(&ap_tasklet);
1391 * __ap_scan_bus(): Scan the AP bus.
1392 * @dev: Pointer to device
1393 * @data: Pointer to data
1395 * Scan the AP bus for new devices.
1397 static int __ap_scan_bus(struct device *dev, void *data)
1399 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1402 static void ap_device_release(struct device *dev)
1404 struct ap_device *ap_dev = to_ap_dev(dev);
1409 static void ap_scan_bus(struct work_struct *unused)
1411 struct ap_device *ap_dev;
1414 int queue_depth, device_type;
1415 unsigned int device_functions;
1418 ap_query_configuration();
1419 if (ap_select_domain() != 0) {
1422 for (i = 0; i < AP_DEVICES; i++) {
1423 qid = AP_MKQID(i, ap_domain_index);
1424 dev = bus_find_device(&ap_bus_type, NULL,
1425 (void *)(unsigned long)qid,
1427 if (ap_test_config_card_id(i))
1428 rc = ap_query_queue(qid, &queue_depth, &device_type);
1433 set_current_state(TASK_UNINTERRUPTIBLE);
1434 schedule_timeout(AP_RESET_TIMEOUT);
1435 rc = ap_query_queue(qid, &queue_depth,
1438 ap_dev = to_ap_dev(dev);
1439 spin_lock_bh(&ap_dev->lock);
1440 if (rc || ap_dev->unregistered) {
1441 spin_unlock_bh(&ap_dev->lock);
1442 if (ap_dev->unregistered)
1444 device_unregister(dev);
1448 spin_unlock_bh(&ap_dev->lock);
1454 rc = ap_init_queue(qid);
1457 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1461 ap_dev->queue_depth = queue_depth;
1462 ap_dev->unregistered = 1;
1463 spin_lock_init(&ap_dev->lock);
1464 INIT_LIST_HEAD(&ap_dev->pendingq);
1465 INIT_LIST_HEAD(&ap_dev->requestq);
1466 INIT_LIST_HEAD(&ap_dev->list);
1467 setup_timer(&ap_dev->timeout, ap_request_timeout,
1468 (unsigned long) ap_dev);
1469 switch (device_type) {
1471 /* device type probing for old cards */
1472 if (ap_probe_device_type(ap_dev)) {
1478 ap_dev->device_type = device_type;
1480 ap_dev->raw_hwtype = device_type;
1482 rc = ap_query_functions(qid, &device_functions);
1484 ap_dev->functions = device_functions;
1486 ap_dev->functions = 0u;
1488 ap_dev->device.bus = &ap_bus_type;
1489 ap_dev->device.parent = ap_root_device;
1490 if (dev_set_name(&ap_dev->device, "card%02x",
1491 AP_QID_DEVICE(ap_dev->qid))) {
1495 ap_dev->device.release = ap_device_release;
1496 rc = device_register(&ap_dev->device);
1498 put_device(&ap_dev->device);
1501 /* Add device attributes. */
1502 rc = sysfs_create_group(&ap_dev->device.kobj,
1503 &ap_dev_attr_group);
1505 spin_lock_bh(&ap_dev->lock);
1506 ap_dev->unregistered = 0;
1507 spin_unlock_bh(&ap_dev->lock);
1510 device_unregister(&ap_dev->device);
1515 ap_config_timeout(unsigned long ptr)
1517 queue_work(ap_work_queue, &ap_config_work);
1518 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1519 add_timer(&ap_config_timer);
1523 * __ap_schedule_poll_timer(): Schedule poll timer.
1525 * Set up the timer to run the poll tasklet
1527 static inline void __ap_schedule_poll_timer(void)
1531 spin_lock_bh(&ap_poll_timer_lock);
1532 if (!hrtimer_is_queued(&ap_poll_timer) && !ap_suspend_flag) {
1533 hr_time = ktime_set(0, poll_timeout);
1534 hrtimer_forward_now(&ap_poll_timer, hr_time);
1535 hrtimer_restart(&ap_poll_timer);
1537 spin_unlock_bh(&ap_poll_timer_lock);
1541 * ap_schedule_poll_timer(): Schedule poll timer.
1543 * Set up the timer to run the poll tasklet
1545 static inline void ap_schedule_poll_timer(void)
1547 if (ap_using_interrupts())
1549 __ap_schedule_poll_timer();
1553 * ap_poll_read(): Receive pending reply messages from an AP device.
1554 * @ap_dev: pointer to the AP device
1555 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1556 * required, bit 2^1 is set if the poll timer needs to get armed
1558 * Returns 0 if the device is still present, -ENODEV if not.
1560 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1562 struct ap_queue_status status;
1563 struct ap_message *ap_msg;
1565 if (ap_dev->queue_count <= 0)
1567 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1568 ap_dev->reply->message, ap_dev->reply->length);
1569 switch (status.response_code) {
1570 case AP_RESPONSE_NORMAL:
1571 atomic_dec(&ap_poll_requests);
1572 ap_decrease_queue_count(ap_dev);
1573 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1574 if (ap_msg->psmid != ap_dev->reply->psmid)
1576 list_del_init(&ap_msg->list);
1577 ap_dev->pendingq_count--;
1578 ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
1581 if (ap_dev->queue_count > 0)
1584 case AP_RESPONSE_NO_PENDING_REPLY:
1585 if (status.queue_empty) {
1586 /* The card shouldn't forget requests but who knows. */
1587 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1588 ap_dev->queue_count = 0;
1589 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1590 ap_dev->requestq_count += ap_dev->pendingq_count;
1591 ap_dev->pendingq_count = 0;
1602 * ap_poll_write(): Send messages from the request queue to an AP device.
1603 * @ap_dev: pointer to the AP device
1604 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1605 * required, bit 2^1 is set if the poll timer needs to get armed
1607 * Returns 0 if the device is still present, -ENODEV if not.
1609 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1611 struct ap_queue_status status;
1612 struct ap_message *ap_msg;
1614 if (ap_dev->requestq_count <= 0 ||
1615 ap_dev->queue_count >= ap_dev->queue_depth)
1617 /* Start the next request on the queue. */
1618 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1619 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1620 ap_msg->message, ap_msg->length, ap_msg->special);
1621 switch (status.response_code) {
1622 case AP_RESPONSE_NORMAL:
1623 atomic_inc(&ap_poll_requests);
1624 ap_increase_queue_count(ap_dev);
1625 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1626 ap_dev->requestq_count--;
1627 ap_dev->pendingq_count++;
1628 if (ap_dev->queue_count < ap_dev->queue_depth &&
1629 ap_dev->requestq_count > 0)
1633 case AP_RESPONSE_RESET_IN_PROGRESS:
1634 __ap_schedule_poll_timer();
1635 case AP_RESPONSE_Q_FULL:
1638 case AP_RESPONSE_MESSAGE_TOO_BIG:
1639 case AP_RESPONSE_REQ_FAC_NOT_INST:
1648 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1649 * @ap_dev: pointer to the bus device
1650 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1651 * required, bit 2^1 is set if the poll timer needs to get armed
1653 * Poll AP device for pending replies and send new messages. If either
1654 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1657 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1661 rc = ap_poll_read(ap_dev, flags);
1664 return ap_poll_write(ap_dev, flags);
1668 * __ap_queue_message(): Queue a message to a device.
1669 * @ap_dev: pointer to the AP device
1670 * @ap_msg: the message to be queued
1672 * Queue a message to a device. Returns 0 if successful.
1674 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1676 struct ap_queue_status status;
1678 if (list_empty(&ap_dev->requestq) &&
1679 ap_dev->queue_count < ap_dev->queue_depth) {
1680 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1681 ap_msg->message, ap_msg->length,
1683 switch (status.response_code) {
1684 case AP_RESPONSE_NORMAL:
1685 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1686 atomic_inc(&ap_poll_requests);
1687 ap_dev->pendingq_count++;
1688 ap_increase_queue_count(ap_dev);
1689 ap_dev->total_request_count++;
1691 case AP_RESPONSE_Q_FULL:
1692 case AP_RESPONSE_RESET_IN_PROGRESS:
1693 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1694 ap_dev->requestq_count++;
1695 ap_dev->total_request_count++;
1697 case AP_RESPONSE_REQ_FAC_NOT_INST:
1698 case AP_RESPONSE_MESSAGE_TOO_BIG:
1699 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1701 default: /* Device is gone. */
1702 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1706 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1707 ap_dev->requestq_count++;
1708 ap_dev->total_request_count++;
1711 ap_schedule_poll_timer();
1715 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1717 unsigned long flags;
1720 /* For asynchronous message handling a valid receive-callback
1722 BUG_ON(!ap_msg->receive);
1724 spin_lock_bh(&ap_dev->lock);
1725 if (!ap_dev->unregistered) {
1726 /* Make room on the queue by polling for finished requests. */
1727 rc = ap_poll_queue(ap_dev, &flags);
1729 rc = __ap_queue_message(ap_dev, ap_msg);
1731 wake_up(&ap_poll_wait);
1733 ap_dev->unregistered = 1;
1735 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1738 spin_unlock_bh(&ap_dev->lock);
1740 device_unregister(&ap_dev->device);
1742 EXPORT_SYMBOL(ap_queue_message);
1745 * ap_cancel_message(): Cancel a crypto request.
1746 * @ap_dev: The AP device that has the message queued
1747 * @ap_msg: The message that is to be removed
1749 * Cancel a crypto request. This is done by removing the request
1750 * from the device pending or request queue. Note that the
1751 * request stays on the AP queue. When it finishes the message
1752 * reply will be discarded because the psmid can't be found.
1754 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1756 struct ap_message *tmp;
1758 spin_lock_bh(&ap_dev->lock);
1759 if (!list_empty(&ap_msg->list)) {
1760 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1761 if (tmp->psmid == ap_msg->psmid) {
1762 ap_dev->pendingq_count--;
1765 ap_dev->requestq_count--;
1767 list_del_init(&ap_msg->list);
1769 spin_unlock_bh(&ap_dev->lock);
1771 EXPORT_SYMBOL(ap_cancel_message);
1774 * ap_poll_timeout(): AP receive polling for finished AP requests.
1775 * @unused: Unused pointer.
1777 * Schedules the AP tasklet using a high resolution timer.
1779 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1781 tasklet_schedule(&ap_tasklet);
1782 return HRTIMER_NORESTART;
1786 * ap_reset(): Reset a not responding AP device.
1787 * @ap_dev: Pointer to the AP device
1789 * Reset a not responding AP device and move all requests from the
1790 * pending queue to the request queue.
1792 static void ap_reset(struct ap_device *ap_dev)
1796 ap_dev->reset = AP_RESET_IGNORE;
1797 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1798 ap_dev->queue_count = 0;
1799 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1800 ap_dev->requestq_count += ap_dev->pendingq_count;
1801 ap_dev->pendingq_count = 0;
1802 rc = ap_init_queue(ap_dev->qid);
1804 ap_dev->unregistered = 1;
1806 __ap_schedule_poll_timer();
1809 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1811 if (!ap_dev->unregistered) {
1812 if (ap_poll_queue(ap_dev, flags))
1813 ap_dev->unregistered = 1;
1814 if (ap_dev->reset == AP_RESET_DO)
1821 * ap_poll_all(): Poll all AP devices.
1822 * @dummy: Unused variable
1824 * Poll all AP devices on the bus in a round robin fashion. Continue
1825 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1826 * of the control flags has been set arm the poll timer.
1828 static void ap_poll_all(unsigned long dummy)
1830 unsigned long flags;
1831 struct ap_device *ap_dev;
1833 /* Reset the indicator if interrupts are used. Thus new interrupts can
1834 * be received. Doing it in the beginning of the tasklet is therefor
1835 * important that no requests on any AP get lost.
1837 if (ap_using_interrupts())
1838 xchg(ap_airq.lsi_ptr, 0);
1841 spin_lock(&ap_device_list_lock);
1842 list_for_each_entry(ap_dev, &ap_device_list, list) {
1843 spin_lock(&ap_dev->lock);
1844 __ap_poll_device(ap_dev, &flags);
1845 spin_unlock(&ap_dev->lock);
1847 spin_unlock(&ap_device_list_lock);
1848 } while (flags & 1);
1850 ap_schedule_poll_timer();
1854 * ap_poll_thread(): Thread that polls for finished requests.
1855 * @data: Unused pointer
1857 * AP bus poll thread. The purpose of this thread is to poll for
1858 * finished requests in a loop if there is a "free" cpu - that is
1859 * a cpu that doesn't have anything better to do. The polling stops
1860 * as soon as there is another task or if all messages have been
1863 static int ap_poll_thread(void *data)
1865 DECLARE_WAITQUEUE(wait, current);
1866 unsigned long flags;
1868 struct ap_device *ap_dev;
1870 set_user_nice(current, MAX_NICE);
1872 if (ap_suspend_flag)
1874 if (need_resched()) {
1878 add_wait_queue(&ap_poll_wait, &wait);
1879 set_current_state(TASK_INTERRUPTIBLE);
1880 if (kthread_should_stop())
1882 requests = atomic_read(&ap_poll_requests);
1885 set_current_state(TASK_RUNNING);
1886 remove_wait_queue(&ap_poll_wait, &wait);
1889 spin_lock_bh(&ap_device_list_lock);
1890 list_for_each_entry(ap_dev, &ap_device_list, list) {
1891 spin_lock(&ap_dev->lock);
1892 __ap_poll_device(ap_dev, &flags);
1893 spin_unlock(&ap_dev->lock);
1895 spin_unlock_bh(&ap_device_list_lock);
1897 set_current_state(TASK_RUNNING);
1898 remove_wait_queue(&ap_poll_wait, &wait);
1902 static int ap_poll_thread_start(void)
1906 if (ap_using_interrupts() || ap_suspend_flag)
1908 mutex_lock(&ap_poll_thread_mutex);
1909 if (!ap_poll_kthread) {
1910 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1911 rc = PTR_RET(ap_poll_kthread);
1913 ap_poll_kthread = NULL;
1917 mutex_unlock(&ap_poll_thread_mutex);
1921 static void ap_poll_thread_stop(void)
1923 mutex_lock(&ap_poll_thread_mutex);
1924 if (ap_poll_kthread) {
1925 kthread_stop(ap_poll_kthread);
1926 ap_poll_kthread = NULL;
1928 mutex_unlock(&ap_poll_thread_mutex);
1932 * ap_request_timeout(): Handling of request timeouts
1933 * @data: Holds the AP device.
1935 * Handles request timeouts.
1937 static void ap_request_timeout(unsigned long data)
1939 struct ap_device *ap_dev = (struct ap_device *) data;
1941 if (ap_dev->reset == AP_RESET_ARMED) {
1942 ap_dev->reset = AP_RESET_DO;
1944 if (ap_using_interrupts())
1945 tasklet_schedule(&ap_tasklet);
1949 static void ap_reset_domain(void)
1953 if ((ap_domain_index != -1) && (ap_test_config_domain(ap_domain_index)))
1954 for (i = 0; i < AP_DEVICES; i++)
1955 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1958 static void ap_reset_all(void)
1962 for (i = 0; i < AP_DOMAINS; i++) {
1963 if (!ap_test_config_domain(i))
1965 for (j = 0; j < AP_DEVICES; j++) {
1966 if (!ap_test_config_card_id(j))
1968 ap_reset_queue(AP_MKQID(j, i));
1973 static struct reset_call ap_reset_call = {
1978 * ap_module_init(): The module initialization code.
1980 * Initializes the module.
1982 int __init ap_module_init(void)
1986 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1987 pr_warning("%d is not a valid cryptographic domain\n",
1991 /* In resume callback we need to know if the user had set the domain.
1992 * If so, we can not just reset it.
1994 if (ap_domain_index >= 0)
1995 user_set_domain = 1;
1997 if (ap_instructions_available() != 0) {
1998 pr_warning("The hardware system does not support "
1999 "AP instructions\n");
2002 if (ap_interrupts_available()) {
2003 rc = register_adapter_interrupt(&ap_airq);
2004 ap_airq_flag = (rc == 0);
2007 register_reset_call(&ap_reset_call);
2009 /* Create /sys/bus/ap. */
2010 rc = bus_register(&ap_bus_type);
2013 for (i = 0; ap_bus_attrs[i]; i++) {
2014 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
2019 /* Create /sys/devices/ap. */
2020 ap_root_device = root_device_register("ap");
2021 rc = PTR_RET(ap_root_device);
2025 ap_work_queue = create_singlethread_workqueue("kapwork");
2026 if (!ap_work_queue) {
2031 ap_query_configuration();
2032 if (ap_select_domain() == 0)
2035 /* Setup the AP bus rescan timer. */
2036 init_timer(&ap_config_timer);
2037 ap_config_timer.function = ap_config_timeout;
2038 ap_config_timer.data = 0;
2039 ap_config_timer.expires = jiffies + ap_config_time * HZ;
2040 add_timer(&ap_config_timer);
2042 /* Setup the high resultion poll timer.
2043 * If we are running under z/VM adjust polling to z/VM polling rate.
2046 poll_timeout = 1500000;
2047 spin_lock_init(&ap_poll_timer_lock);
2048 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2049 ap_poll_timer.function = ap_poll_timeout;
2051 /* Start the low priority AP bus poll thread. */
2052 if (ap_thread_flag) {
2053 rc = ap_poll_thread_start();
2061 del_timer_sync(&ap_config_timer);
2062 hrtimer_cancel(&ap_poll_timer);
2063 destroy_workqueue(ap_work_queue);
2065 root_device_unregister(ap_root_device);
2068 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
2069 bus_unregister(&ap_bus_type);
2071 unregister_reset_call(&ap_reset_call);
2072 if (ap_using_interrupts())
2073 unregister_adapter_interrupt(&ap_airq);
2077 static int __ap_match_all(struct device *dev, void *data)
2083 * ap_modules_exit(): The module termination code
2085 * Terminates the module.
2087 void ap_module_exit(void)
2093 ap_poll_thread_stop();
2094 del_timer_sync(&ap_config_timer);
2095 hrtimer_cancel(&ap_poll_timer);
2096 destroy_workqueue(ap_work_queue);
2097 tasklet_kill(&ap_tasklet);
2098 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
2101 device_unregister(dev);
2104 for (i = 0; ap_bus_attrs[i]; i++)
2105 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
2106 root_device_unregister(ap_root_device);
2107 bus_unregister(&ap_bus_type);
2108 unregister_reset_call(&ap_reset_call);
2109 if (ap_using_interrupts())
2110 unregister_adapter_interrupt(&ap_airq);
2113 module_init(ap_module_init);
2114 module_exit(ap_module_exit);