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 <linux/suspend.h>
41 #include <asm/reset.h>
43 #include <linux/atomic.h>
45 #include <linux/hrtimer.h>
46 #include <linux/ktime.h>
47 #include <asm/facility.h>
48 #include <linux/crypto.h>
52 /* Some prototypes. */
53 static void ap_scan_bus(struct work_struct *);
54 static void ap_poll_all(unsigned long);
55 static void ap_request_timeout(unsigned long);
56 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
57 static int ap_device_remove(struct device *dev);
58 static int ap_device_probe(struct device *dev);
59 static void ap_interrupt_handler(struct airq_struct *airq);
60 static void ap_reset(struct ap_device *ap_dev, unsigned long *flags);
61 static int ap_select_domain(void);
66 MODULE_AUTHOR("IBM Corporation");
67 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
68 "Copyright IBM Corp. 2006, 2012");
69 MODULE_LICENSE("GPL");
70 MODULE_ALIAS_CRYPTO("z90crypt");
75 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
76 module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
77 MODULE_PARM_DESC(domain, "domain index for ap devices");
78 EXPORT_SYMBOL(ap_domain_index);
80 static int ap_thread_flag = 0;
81 module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
82 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
84 static struct device *ap_root_device = NULL;
85 static struct ap_config_info *ap_configuration;
86 static DEFINE_SPINLOCK(ap_device_list_lock);
87 static LIST_HEAD(ap_device_list);
90 * Workqueue & timer for bus rescan.
92 static struct workqueue_struct *ap_work_queue;
93 static struct timer_list ap_config_timer;
94 static int ap_config_time = AP_CONFIG_TIME;
95 static DECLARE_WORK(ap_config_work, ap_scan_bus);
98 * Tasklet & timer for AP request polling and interrupts
100 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
101 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
102 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
103 static struct task_struct *ap_poll_kthread = NULL;
104 static DEFINE_MUTEX(ap_poll_thread_mutex);
105 static DEFINE_SPINLOCK(ap_poll_timer_lock);
106 static struct hrtimer ap_poll_timer;
107 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
108 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
109 static unsigned long long poll_timeout = 250000;
112 static int ap_suspend_flag;
113 /* Maximum domain id */
114 static int ap_max_domain_id;
115 /* Flag to check if domain was set through module parameter domain=. This is
116 * important when supsend and resume is done in a z/VM environment where the
117 * domain might change. */
118 static int user_set_domain = 0;
119 static struct bus_type ap_bus_type;
121 /* Adapter interrupt definitions */
122 static int ap_airq_flag;
124 static struct airq_struct ap_airq = {
125 .handler = ap_interrupt_handler,
130 * ap_using_interrupts() - Returns non-zero if interrupt support is
133 static inline int ap_using_interrupts(void)
139 * ap_intructions_available() - Test if AP instructions are available.
141 * Returns 0 if the AP instructions are installed.
143 static inline int ap_instructions_available(void)
145 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
146 register unsigned long reg1 asm ("1") = -ENODEV;
147 register unsigned long reg2 asm ("2") = 0UL;
150 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
154 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
159 * ap_interrupts_available(): Test if AP interrupts are available.
161 * Returns 1 if AP interrupts are available.
163 static int ap_interrupts_available(void)
165 return test_facility(65);
169 * ap_configuration_available(): Test if AP configuration
170 * information is available.
172 * Returns 1 if AP configuration information is available.
174 static int ap_configuration_available(void)
176 return test_facility(12);
180 * ap_test_queue(): Test adjunct processor queue.
181 * @qid: The AP queue number
182 * @info: Pointer to queue descriptor
184 * Returns AP queue status structure.
186 static inline struct ap_queue_status
187 ap_test_queue(ap_qid_t qid, unsigned long *info)
189 register unsigned long reg0 asm ("0") = qid;
190 register struct ap_queue_status reg1 asm ("1");
191 register unsigned long reg2 asm ("2") = 0UL;
193 if (test_facility(15))
194 reg0 |= 1UL << 23; /* set APFT T bit*/
195 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
196 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
203 * ap_reset_queue(): Reset adjunct processor queue.
204 * @qid: The AP queue number
206 * Returns AP queue status structure.
208 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
210 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
211 register struct ap_queue_status reg1 asm ("1");
212 register unsigned long reg2 asm ("2") = 0UL;
215 ".long 0xb2af0000" /* PQAP(RAPQ) */
216 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
221 * ap_queue_interruption_control(): Enable interruption for a specific AP.
222 * @qid: The AP queue number
223 * @ind: The notification indicator byte
225 * Returns AP queue status.
227 static inline struct ap_queue_status
228 ap_queue_interruption_control(ap_qid_t qid, void *ind)
230 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
231 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
232 register struct ap_queue_status reg1_out asm ("1");
233 register void *reg2 asm ("2") = ind;
235 ".long 0xb2af0000" /* PQAP(AQIC) */
236 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
243 * ap_query_configuration(): Get AP configuration data
245 * Returns 0 on success, or -EOPNOTSUPP.
247 static inline int ap_query_configuration(void)
249 register unsigned long reg0 asm ("0") = 0x04000000UL;
250 register unsigned long reg1 asm ("1") = -EINVAL;
251 register void *reg2 asm ("2") = (void *) ap_configuration;
253 if (!ap_configuration)
256 ".long 0xb2af0000\n" /* PQAP(QCI) */
260 : "+d" (reg0), "+d" (reg1), "+d" (reg2)
268 * ap_init_configuration(): Allocate and query configuration array.
270 static void ap_init_configuration(void)
272 if (!ap_configuration_available())
275 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
276 if (!ap_configuration)
278 if (ap_query_configuration() != 0) {
279 kfree(ap_configuration);
280 ap_configuration = NULL;
286 * ap_test_config(): helper function to extract the nrth bit
287 * within the unsigned int array field.
289 static inline int ap_test_config(unsigned int *field, unsigned int nr)
291 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
295 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
298 * Returns 0 if the card is not configured
299 * 1 if the card is configured or
300 * if the configuration information is not available
302 static inline int ap_test_config_card_id(unsigned int id)
304 if (!ap_configuration) /* QCI not supported */
306 return ap_test_config(ap_configuration->apm, id);
310 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
311 * @domain AP usage domain ID
313 * Returns 0 if the usage domain is not configured
314 * 1 if the usage domain is configured or
315 * if the configuration information is not available
317 static inline int ap_test_config_domain(unsigned int domain)
319 if (!ap_configuration) /* QCI not supported */
321 return ap_test_config(ap_configuration->aqm, domain);
325 * ap_queue_enable_interruption(): Enable interruption on an AP.
326 * @qid: The AP queue number
327 * @ind: the notification indicator byte
329 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
330 * on the return value it waits a while and tests the AP queue if interrupts
331 * have been switched on using ap_test_queue().
333 static int ap_queue_enable_interruption(struct ap_device *ap_dev, void *ind)
335 struct ap_queue_status status;
337 status = ap_queue_interruption_control(ap_dev->qid, ind);
338 switch (status.response_code) {
339 case AP_RESPONSE_NORMAL:
340 case AP_RESPONSE_OTHERWISE_CHANGED:
342 case AP_RESPONSE_Q_NOT_AVAIL:
343 case AP_RESPONSE_DECONFIGURED:
344 case AP_RESPONSE_CHECKSTOPPED:
345 case AP_RESPONSE_INVALID_ADDRESS:
347 case AP_RESPONSE_RESET_IN_PROGRESS:
348 case AP_RESPONSE_BUSY:
355 * __ap_send(): Send message to adjunct processor queue.
356 * @qid: The AP queue number
357 * @psmid: The program supplied message identifier
358 * @msg: The message text
359 * @length: The message length
360 * @special: Special Bit
362 * Returns AP queue status structure.
363 * Condition code 1 on NQAP can't happen because the L bit is 1.
364 * Condition code 2 on NQAP also means the send is incomplete,
365 * because a segment boundary was reached. The NQAP is repeated.
367 static inline struct ap_queue_status
368 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
369 unsigned int special)
371 typedef struct { char _[length]; } msgblock;
372 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
373 register struct ap_queue_status reg1 asm ("1");
374 register unsigned long reg2 asm ("2") = (unsigned long) msg;
375 register unsigned long reg3 asm ("3") = (unsigned long) length;
376 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
377 register unsigned long reg5 asm ("5") = psmid & 0xffffffff;
383 "0: .long 0xb2ad0042\n" /* NQAP */
385 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
386 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
391 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
393 struct ap_queue_status status;
395 status = __ap_send(qid, psmid, msg, length, 0);
396 switch (status.response_code) {
397 case AP_RESPONSE_NORMAL:
399 case AP_RESPONSE_Q_FULL:
400 case AP_RESPONSE_RESET_IN_PROGRESS:
402 case AP_RESPONSE_REQ_FAC_NOT_INST:
404 default: /* Device is gone. */
408 EXPORT_SYMBOL(ap_send);
411 * __ap_recv(): Receive message from adjunct processor queue.
412 * @qid: The AP queue number
413 * @psmid: Pointer to program supplied message identifier
414 * @msg: The message text
415 * @length: The message length
417 * Returns AP queue status structure.
418 * Condition code 1 on DQAP means the receive has taken place
419 * but only partially. The response is incomplete, hence the
421 * Condition code 2 on DQAP also means the receive is incomplete,
422 * this time because a segment boundary was reached. Again, the
424 * Note that gpr2 is used by the DQAP instruction to keep track of
425 * any 'residual' length, in case the instruction gets interrupted.
426 * Hence it gets zeroed before the instruction.
428 static inline struct ap_queue_status
429 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
431 typedef struct { char _[length]; } msgblock;
432 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
433 register struct ap_queue_status reg1 asm ("1");
434 register unsigned long reg2 asm("2") = 0UL;
435 register unsigned long reg4 asm("4") = (unsigned long) msg;
436 register unsigned long reg5 asm("5") = (unsigned long) length;
437 register unsigned long reg6 asm("6") = 0UL;
438 register unsigned long reg7 asm("7") = 0UL;
442 "0: .long 0xb2ae0064\n" /* DQAP */
444 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
445 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
446 "=m" (*(msgblock *) msg) : : "cc" );
447 *psmid = (((unsigned long long) reg6) << 32) + reg7;
451 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
453 struct ap_queue_status status;
455 status = __ap_recv(qid, psmid, msg, length);
456 switch (status.response_code) {
457 case AP_RESPONSE_NORMAL:
459 case AP_RESPONSE_NO_PENDING_REPLY:
460 if (status.queue_empty)
463 case AP_RESPONSE_RESET_IN_PROGRESS:
469 EXPORT_SYMBOL(ap_recv);
472 * __ap_schedule_poll_timer(): Schedule poll timer.
474 * Set up the timer to run the poll tasklet
476 static inline void __ap_schedule_poll_timer(void)
480 spin_lock_bh(&ap_poll_timer_lock);
481 if (!hrtimer_is_queued(&ap_poll_timer) && !ap_suspend_flag) {
482 hr_time = ktime_set(0, poll_timeout);
483 hrtimer_forward_now(&ap_poll_timer, hr_time);
484 hrtimer_restart(&ap_poll_timer);
486 spin_unlock_bh(&ap_poll_timer_lock);
490 * ap_schedule_poll_timer(): Schedule poll timer.
492 * Set up the timer to run the poll tasklet
494 static inline void ap_schedule_poll_timer(void)
496 if (ap_using_interrupts())
498 __ap_schedule_poll_timer();
503 * ap_query_queue(): Check if an AP queue is available.
504 * @qid: The AP queue number
505 * @queue_depth: Pointer to queue depth value
506 * @device_type: Pointer to device type value
507 * @facilities: Pointer to facility indicator
509 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
510 unsigned int *facilities)
512 struct ap_queue_status status;
516 if (!ap_test_config_card_id(AP_QID_DEVICE(qid)))
519 status = ap_test_queue(qid, &info);
520 switch (status.response_code) {
521 case AP_RESPONSE_NORMAL:
522 *queue_depth = (int)(info & 0xff);
523 *device_type = (int)((info >> 24) & 0xff);
524 *facilities = (unsigned int)(info >> 32);
525 /* Update maximum domain id */
526 nd = (info >> 16) & 0xff;
527 if ((info & (1UL << 57)) && nd > 0)
528 ap_max_domain_id = nd;
530 case AP_RESPONSE_Q_NOT_AVAIL:
531 case AP_RESPONSE_DECONFIGURED:
532 case AP_RESPONSE_CHECKSTOPPED:
533 case AP_RESPONSE_INVALID_ADDRESS:
535 case AP_RESPONSE_RESET_IN_PROGRESS:
536 case AP_RESPONSE_OTHERWISE_CHANGED:
537 case AP_RESPONSE_BUSY:
545 * ap_init_queue(): Reset an AP queue.
546 * @qid: The AP queue number
548 * Submit the Reset command to an AP queue.
549 * Since the reset is asynchron set the state to 'RESET_IN_PROGRESS'
550 * and check later via ap_poll_queue() if the reset is done.
552 static int ap_init_queue(struct ap_device *ap_dev)
554 struct ap_queue_status status;
556 status = ap_reset_queue(ap_dev->qid);
557 switch (status.response_code) {
558 case AP_RESPONSE_NORMAL:
559 ap_dev->interrupt = AP_INTR_DISABLED;
560 ap_dev->reset = AP_RESET_IN_PROGRESS;
562 case AP_RESPONSE_RESET_IN_PROGRESS:
563 case AP_RESPONSE_BUSY:
565 case AP_RESPONSE_Q_NOT_AVAIL:
566 case AP_RESPONSE_DECONFIGURED:
567 case AP_RESPONSE_CHECKSTOPPED:
574 * ap_increase_queue_count(): Arm request timeout.
575 * @ap_dev: Pointer to an AP device.
577 * Arm request timeout if an AP device was idle and a new request is submitted.
579 static void ap_increase_queue_count(struct ap_device *ap_dev)
581 int timeout = ap_dev->drv->request_timeout;
583 ap_dev->queue_count++;
584 if (ap_dev->queue_count == 1) {
585 mod_timer(&ap_dev->timeout, jiffies + timeout);
586 ap_dev->reset = AP_RESET_ARMED;
591 * ap_decrease_queue_count(): Decrease queue count.
592 * @ap_dev: Pointer to an AP device.
594 * If AP device is still alive, re-schedule request timeout if there are still
597 static void ap_decrease_queue_count(struct ap_device *ap_dev)
599 int timeout = ap_dev->drv->request_timeout;
601 ap_dev->queue_count--;
602 if (ap_dev->queue_count > 0)
603 mod_timer(&ap_dev->timeout, jiffies + timeout);
606 * The timeout timer should to be disabled now - since
607 * del_timer_sync() is very expensive, we just tell via the
608 * reset flag to ignore the pending timeout timer.
610 ap_dev->reset = AP_RESET_IGNORE;
614 * ap_poll_thread(): Thread that polls for finished requests.
615 * @data: Unused pointer
617 * AP bus poll thread. The purpose of this thread is to poll for
618 * finished requests in a loop if there is a "free" cpu - that is
619 * a cpu that doesn't have anything better to do. The polling stops
620 * as soon as there is another task or if all messages have been
623 static int ap_poll_thread(void *data)
625 DECLARE_WAITQUEUE(wait, current);
627 struct ap_device *ap_dev;
629 set_user_nice(current, MAX_NICE);
631 while (!kthread_should_stop()) {
632 add_wait_queue(&ap_poll_wait, &wait);
633 set_current_state(TASK_INTERRUPTIBLE);
634 if (ap_suspend_flag ||
635 atomic_read(&ap_poll_requests) <= 0) {
639 set_current_state(TASK_RUNNING);
640 remove_wait_queue(&ap_poll_wait, &wait);
642 if (need_resched()) {
649 spin_lock_bh(&ap_device_list_lock);
650 list_for_each_entry(ap_dev, &ap_device_list, list) {
651 spin_lock(&ap_dev->lock);
652 __ap_poll_device(ap_dev, &flags);
653 spin_unlock(&ap_dev->lock);
655 spin_unlock_bh(&ap_device_list_lock);
656 } while (!kthread_should_stop());
660 static int ap_poll_thread_start(void)
664 if (ap_using_interrupts() || ap_poll_kthread)
666 mutex_lock(&ap_poll_thread_mutex);
667 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
668 rc = PTR_RET(ap_poll_kthread);
670 ap_poll_kthread = NULL;
671 mutex_unlock(&ap_poll_thread_mutex);
675 static void ap_poll_thread_stop(void)
677 if (!ap_poll_kthread)
679 mutex_lock(&ap_poll_thread_mutex);
680 kthread_stop(ap_poll_kthread);
681 ap_poll_kthread = NULL;
682 mutex_unlock(&ap_poll_thread_mutex);
686 * AP device related attributes.
688 static ssize_t ap_hwtype_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
691 struct ap_device *ap_dev = to_ap_dev(dev);
692 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
695 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
697 static ssize_t ap_raw_hwtype_show(struct device *dev,
698 struct device_attribute *attr, char *buf)
700 struct ap_device *ap_dev = to_ap_dev(dev);
702 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->raw_hwtype);
705 static DEVICE_ATTR(raw_hwtype, 0444, ap_raw_hwtype_show, NULL);
707 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
710 struct ap_device *ap_dev = to_ap_dev(dev);
711 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
714 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
715 static ssize_t ap_request_count_show(struct device *dev,
716 struct device_attribute *attr,
719 struct ap_device *ap_dev = to_ap_dev(dev);
722 spin_lock_bh(&ap_dev->lock);
723 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
724 spin_unlock_bh(&ap_dev->lock);
728 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
730 static ssize_t ap_requestq_count_show(struct device *dev,
731 struct device_attribute *attr, char *buf)
733 struct ap_device *ap_dev = to_ap_dev(dev);
736 spin_lock_bh(&ap_dev->lock);
737 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
738 spin_unlock_bh(&ap_dev->lock);
742 static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
744 static ssize_t ap_pendingq_count_show(struct device *dev,
745 struct device_attribute *attr, char *buf)
747 struct ap_device *ap_dev = to_ap_dev(dev);
750 spin_lock_bh(&ap_dev->lock);
751 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
752 spin_unlock_bh(&ap_dev->lock);
756 static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
758 static ssize_t ap_reset_show(struct device *dev,
759 struct device_attribute *attr, char *buf)
761 struct ap_device *ap_dev = to_ap_dev(dev);
764 spin_lock_bh(&ap_dev->lock);
765 switch (ap_dev->reset) {
766 case AP_RESET_IGNORE:
767 rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
770 rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
773 rc = snprintf(buf, PAGE_SIZE, "Reset Timer expired.\n");
775 case AP_RESET_IN_PROGRESS:
776 rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
781 spin_unlock_bh(&ap_dev->lock);
785 static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL);
787 static ssize_t ap_interrupt_show(struct device *dev,
788 struct device_attribute *attr, char *buf)
790 struct ap_device *ap_dev = to_ap_dev(dev);
793 spin_lock_bh(&ap_dev->lock);
794 switch (ap_dev->interrupt) {
795 case AP_INTR_DISABLED:
796 rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
798 case AP_INTR_ENABLED:
799 rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
801 case AP_INTR_IN_PROGRESS:
802 rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
805 spin_unlock_bh(&ap_dev->lock);
809 static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL);
811 static ssize_t ap_modalias_show(struct device *dev,
812 struct device_attribute *attr, char *buf)
814 return sprintf(buf, "ap:t%02X\n", to_ap_dev(dev)->device_type);
817 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
819 static ssize_t ap_functions_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
822 struct ap_device *ap_dev = to_ap_dev(dev);
823 return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
826 static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
828 static struct attribute *ap_dev_attrs[] = {
829 &dev_attr_hwtype.attr,
830 &dev_attr_raw_hwtype.attr,
831 &dev_attr_depth.attr,
832 &dev_attr_request_count.attr,
833 &dev_attr_requestq_count.attr,
834 &dev_attr_pendingq_count.attr,
835 &dev_attr_reset.attr,
836 &dev_attr_interrupt.attr,
837 &dev_attr_modalias.attr,
838 &dev_attr_ap_functions.attr,
841 static struct attribute_group ap_dev_attr_group = {
842 .attrs = ap_dev_attrs
847 * @dev: Pointer to device
848 * @drv: Pointer to device_driver
850 * AP bus driver registration/unregistration.
852 static int ap_bus_match(struct device *dev, struct device_driver *drv)
854 struct ap_device *ap_dev = to_ap_dev(dev);
855 struct ap_driver *ap_drv = to_ap_drv(drv);
856 struct ap_device_id *id;
859 * Compare device type of the device with the list of
860 * supported types of the device_driver.
862 for (id = ap_drv->ids; id->match_flags; id++) {
863 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
864 (id->dev_type != ap_dev->device_type))
872 * ap_uevent(): Uevent function for AP devices.
873 * @dev: Pointer to device
874 * @env: Pointer to kobj_uevent_env
876 * It sets up a single environment variable DEV_TYPE which contains the
877 * hardware device type.
879 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
881 struct ap_device *ap_dev = to_ap_dev(dev);
887 /* Set up DEV_TYPE environment variable. */
888 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
893 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
898 static int ap_dev_suspend(struct device *dev, pm_message_t state)
900 struct ap_device *ap_dev = to_ap_dev(dev);
903 /* Poll on the device until all requests are finished. */
906 spin_lock_bh(&ap_dev->lock);
907 __ap_poll_device(ap_dev, &flags);
908 spin_unlock_bh(&ap_dev->lock);
909 } while ((flags & 1) || (flags & 2));
914 static int ap_dev_resume(struct device *dev)
919 static void ap_bus_suspend(void)
923 * Disable scanning for devices, thus we do not want to scan
924 * for them after removing.
926 flush_workqueue(ap_work_queue);
927 tasklet_disable(&ap_tasklet);
930 static int __ap_devices_unregister(struct device *dev, void *dummy)
932 device_unregister(dev);
936 static void ap_bus_resume(void)
940 /* Unconditionally remove all AP devices */
941 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
942 /* Reset thin interrupt setting */
943 if (ap_interrupts_available() && !ap_using_interrupts()) {
944 rc = register_adapter_interrupt(&ap_airq);
945 ap_airq_flag = (rc == 0);
947 if (!ap_interrupts_available() && ap_using_interrupts()) {
948 unregister_adapter_interrupt(&ap_airq);
952 if (!user_set_domain)
953 ap_domain_index = -1;
954 /* Get things going again */
957 xchg(ap_airq.lsi_ptr, 0);
958 tasklet_enable(&ap_tasklet);
959 queue_work(ap_work_queue, &ap_config_work);
960 wake_up(&ap_poll_wait);
963 static int ap_power_event(struct notifier_block *this, unsigned long event,
967 case PM_HIBERNATION_PREPARE:
968 case PM_SUSPEND_PREPARE:
971 case PM_POST_HIBERNATION:
972 case PM_POST_SUSPEND:
980 static struct notifier_block ap_power_notifier = {
981 .notifier_call = ap_power_event,
984 static struct bus_type ap_bus_type = {
986 .match = &ap_bus_match,
987 .uevent = &ap_uevent,
988 .suspend = ap_dev_suspend,
989 .resume = ap_dev_resume,
992 static int ap_device_probe(struct device *dev)
994 struct ap_device *ap_dev = to_ap_dev(dev);
995 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
998 ap_dev->drv = ap_drv;
1000 spin_lock_bh(&ap_device_list_lock);
1001 list_add(&ap_dev->list, &ap_device_list);
1002 spin_unlock_bh(&ap_device_list_lock);
1004 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
1006 spin_lock_bh(&ap_device_list_lock);
1007 list_del_init(&ap_dev->list);
1008 spin_unlock_bh(&ap_device_list_lock);
1010 if (ap_dev->reset == AP_RESET_IN_PROGRESS ||
1011 ap_dev->interrupt == AP_INTR_IN_PROGRESS)
1012 __ap_schedule_poll_timer();
1018 * __ap_flush_queue(): Flush requests.
1019 * @ap_dev: Pointer to the AP device
1021 * Flush all requests from the request/pending queue of an AP device.
1023 static void __ap_flush_queue(struct ap_device *ap_dev)
1025 struct ap_message *ap_msg, *next;
1027 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
1028 list_del_init(&ap_msg->list);
1029 ap_dev->pendingq_count--;
1030 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1032 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
1033 list_del_init(&ap_msg->list);
1034 ap_dev->requestq_count--;
1035 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1039 void ap_flush_queue(struct ap_device *ap_dev)
1041 spin_lock_bh(&ap_dev->lock);
1042 __ap_flush_queue(ap_dev);
1043 spin_unlock_bh(&ap_dev->lock);
1045 EXPORT_SYMBOL(ap_flush_queue);
1047 static int ap_device_remove(struct device *dev)
1049 struct ap_device *ap_dev = to_ap_dev(dev);
1050 struct ap_driver *ap_drv = ap_dev->drv;
1052 ap_flush_queue(ap_dev);
1053 del_timer_sync(&ap_dev->timeout);
1054 spin_lock_bh(&ap_device_list_lock);
1055 list_del_init(&ap_dev->list);
1056 spin_unlock_bh(&ap_device_list_lock);
1058 ap_drv->remove(ap_dev);
1059 spin_lock_bh(&ap_dev->lock);
1060 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1061 spin_unlock_bh(&ap_dev->lock);
1065 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1068 struct device_driver *drv = &ap_drv->driver;
1070 drv->bus = &ap_bus_type;
1071 drv->probe = ap_device_probe;
1072 drv->remove = ap_device_remove;
1075 return driver_register(drv);
1077 EXPORT_SYMBOL(ap_driver_register);
1079 void ap_driver_unregister(struct ap_driver *ap_drv)
1081 driver_unregister(&ap_drv->driver);
1083 EXPORT_SYMBOL(ap_driver_unregister);
1085 void ap_bus_force_rescan(void)
1087 if (ap_suspend_flag)
1089 /* processing a asynchronous bus rescan */
1090 del_timer(&ap_config_timer);
1091 queue_work(ap_work_queue, &ap_config_work);
1092 flush_work(&ap_config_work);
1094 EXPORT_SYMBOL(ap_bus_force_rescan);
1097 * AP bus attributes.
1099 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1101 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1104 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
1106 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1108 if (!ap_configuration) /* QCI not supported */
1109 return snprintf(buf, PAGE_SIZE, "not supported\n");
1110 if (!test_facility(76))
1111 /* format 0 - 16 bit domain field */
1112 return snprintf(buf, PAGE_SIZE, "%08x%08x\n",
1113 ap_configuration->adm[0],
1114 ap_configuration->adm[1]);
1115 /* format 1 - 256 bit domain field */
1116 return snprintf(buf, PAGE_SIZE,
1117 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1118 ap_configuration->adm[0], ap_configuration->adm[1],
1119 ap_configuration->adm[2], ap_configuration->adm[3],
1120 ap_configuration->adm[4], ap_configuration->adm[5],
1121 ap_configuration->adm[6], ap_configuration->adm[7]);
1124 static BUS_ATTR(ap_control_domain_mask, 0444,
1125 ap_control_domain_mask_show, NULL);
1127 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
1129 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1132 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1134 return snprintf(buf, PAGE_SIZE, "%d\n",
1135 ap_using_interrupts() ? 1 : 0);
1138 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
1140 static ssize_t ap_config_time_store(struct bus_type *bus,
1141 const char *buf, size_t count)
1145 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1147 ap_config_time = time;
1148 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1152 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
1154 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
1156 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1159 static ssize_t ap_poll_thread_store(struct bus_type *bus,
1160 const char *buf, size_t count)
1164 if (sscanf(buf, "%d\n", &flag) != 1)
1167 rc = ap_poll_thread_start();
1171 ap_poll_thread_stop();
1175 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
1177 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1179 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1182 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1185 unsigned long long time;
1188 /* 120 seconds = maximum poll interval */
1189 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1190 time > 120000000000ULL)
1192 poll_timeout = time;
1193 hr_time = ktime_set(0, poll_timeout);
1195 spin_lock_bh(&ap_poll_timer_lock);
1196 hrtimer_cancel(&ap_poll_timer);
1197 hrtimer_set_expires(&ap_poll_timer, hr_time);
1198 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1199 spin_unlock_bh(&ap_poll_timer_lock);
1204 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
1206 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1210 if (ap_configuration)
1211 max_domain_id = ap_max_domain_id ? : -1;
1214 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1217 static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
1219 static struct bus_attribute *const ap_bus_attrs[] = {
1220 &bus_attr_ap_domain,
1221 &bus_attr_ap_control_domain_mask,
1222 &bus_attr_config_time,
1223 &bus_attr_poll_thread,
1224 &bus_attr_ap_interrupts,
1225 &bus_attr_poll_timeout,
1226 &bus_attr_ap_max_domain_id,
1231 * ap_select_domain(): Select an AP domain.
1233 * Pick one of the 16 AP domains.
1235 static int ap_select_domain(void)
1237 int count, max_count, best_domain;
1238 struct ap_queue_status status;
1242 * We want to use a single domain. Either the one specified with
1243 * the "domain=" parameter or the domain with the maximum number
1246 if (ap_domain_index >= 0)
1247 /* Domain has already been selected. */
1251 for (i = 0; i < AP_DOMAINS; i++) {
1252 if (!ap_test_config_domain(i))
1255 for (j = 0; j < AP_DEVICES; j++) {
1256 if (!ap_test_config_card_id(j))
1258 status = ap_test_queue(AP_MKQID(j, i), NULL);
1259 if (status.response_code != AP_RESPONSE_NORMAL)
1263 if (count > max_count) {
1268 if (best_domain >= 0){
1269 ap_domain_index = best_domain;
1276 * ap_probe_device_type(): Find the device type of an AP.
1277 * @ap_dev: pointer to the AP device.
1279 * Find the device type if query queue returned a device type of 0.
1281 static int ap_probe_device_type(struct ap_device *ap_dev)
1283 static unsigned char msg[] = {
1284 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1285 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1286 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1287 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1288 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1289 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1290 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1291 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1292 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1293 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1294 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1295 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1296 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1297 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1298 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1299 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1300 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1301 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1302 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1303 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1304 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1305 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1306 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1307 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1308 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1309 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1310 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1311 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1312 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1313 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1314 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1315 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1316 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1317 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1318 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1319 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1320 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1321 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1322 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1323 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1324 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1325 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1326 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1327 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1328 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1330 struct ap_queue_status status;
1331 unsigned long long psmid;
1335 reply = (void *) get_zeroed_page(GFP_KERNEL);
1341 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1342 msg, sizeof(msg), 0);
1343 if (status.response_code != AP_RESPONSE_NORMAL) {
1348 /* Wait for the test message to complete. */
1349 for (i = 0; i < 6; i++) {
1351 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1352 if (status.response_code == AP_RESPONSE_NORMAL &&
1353 psmid == 0x0102030405060708ULL)
1357 /* Got an answer. */
1358 if (reply[0] == 0x00 && reply[1] == 0x86)
1359 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1361 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1367 free_page((unsigned long) reply);
1372 static void ap_interrupt_handler(struct airq_struct *airq)
1374 inc_irq_stat(IRQIO_APB);
1375 if (!ap_suspend_flag)
1376 tasklet_schedule(&ap_tasklet);
1380 * __ap_scan_bus(): Scan the AP bus.
1381 * @dev: Pointer to device
1382 * @data: Pointer to data
1384 * Scan the AP bus for new devices.
1386 static int __ap_scan_bus(struct device *dev, void *data)
1388 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1391 static void ap_device_release(struct device *dev)
1393 struct ap_device *ap_dev = to_ap_dev(dev);
1398 static void ap_scan_bus(struct work_struct *unused)
1400 struct ap_device *ap_dev;
1403 int queue_depth = 0, device_type = 0;
1404 unsigned int device_functions;
1407 ap_query_configuration();
1408 if (ap_select_domain() != 0)
1411 for (i = 0; i < AP_DEVICES; i++) {
1412 qid = AP_MKQID(i, ap_domain_index);
1413 dev = bus_find_device(&ap_bus_type, NULL,
1414 (void *)(unsigned long)qid,
1416 rc = ap_query_queue(qid, &queue_depth, &device_type,
1419 ap_dev = to_ap_dev(dev);
1420 spin_lock_bh(&ap_dev->lock);
1421 if (rc == -ENODEV || ap_dev->unregistered) {
1422 spin_unlock_bh(&ap_dev->lock);
1423 if (ap_dev->unregistered)
1425 device_unregister(dev);
1429 spin_unlock_bh(&ap_dev->lock);
1435 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1439 rc = ap_init_queue(ap_dev);
1440 if ((rc != 0) && (rc != -EBUSY)) {
1444 ap_dev->queue_depth = queue_depth;
1445 ap_dev->raw_hwtype = device_type;
1446 ap_dev->device_type = device_type;
1447 ap_dev->functions = device_functions;
1448 ap_dev->unregistered = 1;
1449 spin_lock_init(&ap_dev->lock);
1450 INIT_LIST_HEAD(&ap_dev->pendingq);
1451 INIT_LIST_HEAD(&ap_dev->requestq);
1452 INIT_LIST_HEAD(&ap_dev->list);
1453 setup_timer(&ap_dev->timeout, ap_request_timeout,
1454 (unsigned long) ap_dev);
1455 if (ap_dev->device_type == 0)
1456 /* device type probing for old cards */
1457 if (ap_probe_device_type(ap_dev)) {
1462 ap_dev->device.bus = &ap_bus_type;
1463 ap_dev->device.parent = ap_root_device;
1464 if (dev_set_name(&ap_dev->device, "card%02x",
1465 AP_QID_DEVICE(ap_dev->qid))) {
1469 ap_dev->device.release = ap_device_release;
1470 rc = device_register(&ap_dev->device);
1472 put_device(&ap_dev->device);
1475 /* Add device attributes. */
1476 rc = sysfs_create_group(&ap_dev->device.kobj,
1477 &ap_dev_attr_group);
1479 spin_lock_bh(&ap_dev->lock);
1480 ap_dev->unregistered = 0;
1481 spin_unlock_bh(&ap_dev->lock);
1484 device_unregister(&ap_dev->device);
1487 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1490 static void ap_config_timeout(unsigned long ptr)
1492 if (ap_suspend_flag)
1494 queue_work(ap_work_queue, &ap_config_work);
1498 * ap_poll_read(): Receive pending reply messages from an AP device.
1499 * @ap_dev: pointer to the AP device
1500 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1501 * required, bit 2^1 is set if the poll timer needs to get armed
1503 * Returns 0 if the device is still present, -ENODEV if not.
1505 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1507 struct ap_queue_status status;
1508 struct ap_message *ap_msg;
1510 if (ap_dev->queue_count <= 0)
1512 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1513 ap_dev->reply->message, ap_dev->reply->length);
1514 switch (status.response_code) {
1515 case AP_RESPONSE_NORMAL:
1516 ap_dev->interrupt = status.int_enabled;
1517 atomic_dec(&ap_poll_requests);
1518 ap_decrease_queue_count(ap_dev);
1519 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1520 if (ap_msg->psmid != ap_dev->reply->psmid)
1522 list_del_init(&ap_msg->list);
1523 ap_dev->pendingq_count--;
1524 ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
1527 if (ap_dev->queue_count > 0)
1530 case AP_RESPONSE_NO_PENDING_REPLY:
1531 ap_dev->interrupt = status.int_enabled;
1532 if (status.queue_empty) {
1533 /* The card shouldn't forget requests but who knows. */
1534 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1535 ap_dev->queue_count = 0;
1536 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1537 ap_dev->requestq_count += ap_dev->pendingq_count;
1538 ap_dev->pendingq_count = 0;
1549 * ap_poll_write(): Send messages from the request queue to an AP device.
1550 * @ap_dev: pointer to the AP device
1551 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1552 * required, bit 2^1 is set if the poll timer needs to get armed
1554 * Returns 0 if the device is still present, -ENODEV if not.
1556 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1558 struct ap_queue_status status;
1559 struct ap_message *ap_msg;
1561 if (ap_dev->requestq_count <= 0 ||
1562 (ap_dev->queue_count >= ap_dev->queue_depth) ||
1563 (ap_dev->reset == AP_RESET_IN_PROGRESS))
1565 /* Start the next request on the queue. */
1566 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1567 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1568 ap_msg->message, ap_msg->length, ap_msg->special);
1569 switch (status.response_code) {
1570 case AP_RESPONSE_NORMAL:
1571 atomic_inc(&ap_poll_requests);
1572 ap_increase_queue_count(ap_dev);
1573 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1574 ap_dev->requestq_count--;
1575 ap_dev->pendingq_count++;
1576 if (ap_dev->queue_count < ap_dev->queue_depth &&
1577 ap_dev->requestq_count > 0)
1581 case AP_RESPONSE_RESET_IN_PROGRESS:
1582 __ap_schedule_poll_timer();
1583 case AP_RESPONSE_Q_FULL:
1586 case AP_RESPONSE_MESSAGE_TOO_BIG:
1587 case AP_RESPONSE_REQ_FAC_NOT_INST:
1596 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1597 * Check if the queue has a pending reset. In case it's done re-enable
1598 * interrupts, otherwise reschedule the poll_timer for another attempt.
1599 * @ap_dev: pointer to the bus device
1600 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1601 * required, bit 2^1 is set if the poll timer needs to get armed
1603 * Poll AP device for pending replies and send new messages. If either
1604 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1607 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1609 struct ap_queue_status status;
1612 if (ap_dev->reset == AP_RESET_IN_PROGRESS) {
1613 status = ap_test_queue(ap_dev->qid, NULL);
1614 switch (status.response_code) {
1615 case AP_RESPONSE_NORMAL:
1616 ap_dev->reset = AP_RESET_IGNORE;
1617 if (ap_using_interrupts()) {
1618 rc = ap_queue_enable_interruption(
1619 ap_dev, ap_airq.lsi_ptr);
1621 ap_dev->interrupt = AP_INTR_IN_PROGRESS;
1622 else if (rc == -ENODEV) {
1623 pr_err("Registering adapter interrupts for "
1624 "AP %d failed\n", AP_QID_DEVICE(ap_dev->qid));
1629 case AP_RESPONSE_BUSY:
1630 case AP_RESPONSE_RESET_IN_PROGRESS:
1631 *flags |= AP_POLL_AFTER_TIMEOUT;
1633 case AP_RESPONSE_Q_NOT_AVAIL:
1634 case AP_RESPONSE_DECONFIGURED:
1635 case AP_RESPONSE_CHECKSTOPPED:
1642 if ((ap_dev->reset != AP_RESET_IN_PROGRESS) &&
1643 (ap_dev->interrupt == AP_INTR_IN_PROGRESS)) {
1644 status = ap_test_queue(ap_dev->qid, NULL);
1645 if (ap_using_interrupts()) {
1646 if (status.int_enabled == 1)
1647 ap_dev->interrupt = AP_INTR_ENABLED;
1649 *flags |= AP_POLL_AFTER_TIMEOUT;
1651 ap_dev->interrupt = AP_INTR_DISABLED;
1654 rc = ap_poll_read(ap_dev, flags);
1657 return ap_poll_write(ap_dev, flags);
1661 * __ap_queue_message(): Queue a message to a device.
1662 * @ap_dev: pointer to the AP device
1663 * @ap_msg: the message to be queued
1665 * Queue a message to a device. Returns 0 if successful.
1667 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1669 struct ap_queue_status status;
1671 if (list_empty(&ap_dev->requestq) &&
1672 (ap_dev->queue_count < ap_dev->queue_depth) &&
1673 (ap_dev->reset != AP_RESET_IN_PROGRESS)) {
1674 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1675 ap_msg->message, ap_msg->length,
1677 switch (status.response_code) {
1678 case AP_RESPONSE_NORMAL:
1679 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1680 atomic_inc(&ap_poll_requests);
1681 ap_dev->pendingq_count++;
1682 ap_increase_queue_count(ap_dev);
1683 ap_dev->total_request_count++;
1685 case AP_RESPONSE_Q_FULL:
1686 case AP_RESPONSE_RESET_IN_PROGRESS:
1687 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1688 ap_dev->requestq_count++;
1689 ap_dev->total_request_count++;
1691 case AP_RESPONSE_REQ_FAC_NOT_INST:
1692 case AP_RESPONSE_MESSAGE_TOO_BIG:
1693 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1695 default: /* Device is gone. */
1696 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1700 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1701 ap_dev->requestq_count++;
1702 ap_dev->total_request_count++;
1705 ap_schedule_poll_timer();
1709 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1711 unsigned long flags;
1714 /* For asynchronous message handling a valid receive-callback
1716 BUG_ON(!ap_msg->receive);
1718 spin_lock_bh(&ap_dev->lock);
1719 if (!ap_dev->unregistered) {
1720 /* Make room on the queue by polling for finished requests. */
1721 rc = ap_poll_queue(ap_dev, &flags);
1723 rc = __ap_queue_message(ap_dev, ap_msg);
1725 wake_up(&ap_poll_wait);
1727 ap_dev->unregistered = 1;
1729 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1732 spin_unlock_bh(&ap_dev->lock);
1734 EXPORT_SYMBOL(ap_queue_message);
1737 * ap_cancel_message(): Cancel a crypto request.
1738 * @ap_dev: The AP device that has the message queued
1739 * @ap_msg: The message that is to be removed
1741 * Cancel a crypto request. This is done by removing the request
1742 * from the device pending or request queue. Note that the
1743 * request stays on the AP queue. When it finishes the message
1744 * reply will be discarded because the psmid can't be found.
1746 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1748 struct ap_message *tmp;
1750 spin_lock_bh(&ap_dev->lock);
1751 if (!list_empty(&ap_msg->list)) {
1752 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1753 if (tmp->psmid == ap_msg->psmid) {
1754 ap_dev->pendingq_count--;
1757 ap_dev->requestq_count--;
1759 list_del_init(&ap_msg->list);
1761 spin_unlock_bh(&ap_dev->lock);
1763 EXPORT_SYMBOL(ap_cancel_message);
1766 * ap_poll_timeout(): AP receive polling for finished AP requests.
1767 * @unused: Unused pointer.
1769 * Schedules the AP tasklet using a high resolution timer.
1771 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1773 if (!ap_suspend_flag)
1774 tasklet_schedule(&ap_tasklet);
1775 return HRTIMER_NORESTART;
1779 * ap_reset(): Reset a not responding AP device.
1780 * @ap_dev: Pointer to the AP device
1782 * Reset a not responding AP device and move all requests from the
1783 * pending queue to the request queue.
1785 static void ap_reset(struct ap_device *ap_dev, unsigned long *flags)
1789 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1790 ap_dev->queue_count = 0;
1791 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1792 ap_dev->requestq_count += ap_dev->pendingq_count;
1793 ap_dev->pendingq_count = 0;
1794 rc = ap_init_queue(ap_dev);
1796 ap_dev->unregistered = 1;
1798 *flags |= AP_POLL_AFTER_TIMEOUT;
1801 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1803 if (!ap_dev->unregistered) {
1804 if (ap_poll_queue(ap_dev, flags))
1805 ap_dev->unregistered = 1;
1806 if (ap_dev->reset == AP_RESET_DO)
1807 ap_reset(ap_dev, flags);
1813 * ap_poll_all(): Poll all AP devices.
1814 * @dummy: Unused variable
1816 * Poll all AP devices on the bus in a round robin fashion. Continue
1817 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1818 * of the control flags has been set arm the poll timer.
1820 static void ap_poll_all(unsigned long dummy)
1822 unsigned long flags;
1823 struct ap_device *ap_dev;
1825 /* Reset the indicator if interrupts are used. Thus new interrupts can
1826 * be received. Doing it in the beginning of the tasklet is therefor
1827 * important that no requests on any AP get lost.
1829 if (ap_using_interrupts())
1830 xchg(ap_airq.lsi_ptr, 0);
1833 spin_lock(&ap_device_list_lock);
1834 list_for_each_entry(ap_dev, &ap_device_list, list) {
1835 spin_lock(&ap_dev->lock);
1836 __ap_poll_device(ap_dev, &flags);
1837 spin_unlock(&ap_dev->lock);
1839 spin_unlock(&ap_device_list_lock);
1840 } while (flags & AP_POLL_IMMEDIATELY);
1841 if (flags & AP_POLL_AFTER_TIMEOUT)
1842 __ap_schedule_poll_timer();
1846 * ap_request_timeout(): Handling of request timeouts
1847 * @data: Holds the AP device.
1849 * Handles request timeouts.
1851 static void ap_request_timeout(unsigned long data)
1853 struct ap_device *ap_dev = (struct ap_device *) data;
1855 if (ap_suspend_flag)
1857 if (ap_dev->reset == AP_RESET_ARMED) {
1858 ap_dev->reset = AP_RESET_DO;
1860 if (ap_using_interrupts())
1861 tasklet_schedule(&ap_tasklet);
1865 static void ap_reset_domain(void)
1869 if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index))
1871 for (i = 0; i < AP_DEVICES; i++)
1872 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1875 static void ap_reset_all(void)
1879 for (i = 0; i < AP_DOMAINS; i++) {
1880 if (!ap_test_config_domain(i))
1882 for (j = 0; j < AP_DEVICES; j++) {
1883 if (!ap_test_config_card_id(j))
1885 ap_reset_queue(AP_MKQID(j, i));
1890 static struct reset_call ap_reset_call = {
1895 * ap_module_init(): The module initialization code.
1897 * Initializes the module.
1899 int __init ap_module_init(void)
1904 if (ap_instructions_available() != 0) {
1905 pr_warn("The hardware system does not support AP instructions\n");
1909 /* Get AP configuration data if available */
1910 ap_init_configuration();
1912 if (ap_configuration)
1913 max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1);
1916 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1917 pr_warn("%d is not a valid cryptographic domain\n",
1921 /* In resume callback we need to know if the user had set the domain.
1922 * If so, we can not just reset it.
1924 if (ap_domain_index >= 0)
1925 user_set_domain = 1;
1927 if (ap_interrupts_available()) {
1928 rc = register_adapter_interrupt(&ap_airq);
1929 ap_airq_flag = (rc == 0);
1932 register_reset_call(&ap_reset_call);
1934 /* Create /sys/bus/ap. */
1935 rc = bus_register(&ap_bus_type);
1938 for (i = 0; ap_bus_attrs[i]; i++) {
1939 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1944 /* Create /sys/devices/ap. */
1945 ap_root_device = root_device_register("ap");
1946 rc = PTR_RET(ap_root_device);
1950 ap_work_queue = create_singlethread_workqueue("kapwork");
1951 if (!ap_work_queue) {
1956 if (ap_select_domain() == 0)
1959 /* Setup the AP bus rescan timer. */
1960 setup_timer(&ap_config_timer, ap_config_timeout, 0);
1961 add_timer(&ap_config_timer);
1963 /* Setup the high resultion poll timer.
1964 * If we are running under z/VM adjust polling to z/VM polling rate.
1967 poll_timeout = 1500000;
1968 spin_lock_init(&ap_poll_timer_lock);
1969 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1970 ap_poll_timer.function = ap_poll_timeout;
1972 /* Start the low priority AP bus poll thread. */
1973 if (ap_thread_flag) {
1974 rc = ap_poll_thread_start();
1979 rc = register_pm_notifier(&ap_power_notifier);
1986 ap_poll_thread_stop();
1988 del_timer_sync(&ap_config_timer);
1989 hrtimer_cancel(&ap_poll_timer);
1990 destroy_workqueue(ap_work_queue);
1992 root_device_unregister(ap_root_device);
1995 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1996 bus_unregister(&ap_bus_type);
1998 unregister_reset_call(&ap_reset_call);
1999 if (ap_using_interrupts())
2000 unregister_adapter_interrupt(&ap_airq);
2001 kfree(ap_configuration);
2006 * ap_modules_exit(): The module termination code
2008 * Terminates the module.
2010 void ap_module_exit(void)
2015 ap_poll_thread_stop();
2016 del_timer_sync(&ap_config_timer);
2017 hrtimer_cancel(&ap_poll_timer);
2018 destroy_workqueue(ap_work_queue);
2019 tasklet_kill(&ap_tasklet);
2020 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
2021 for (i = 0; ap_bus_attrs[i]; i++)
2022 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
2023 unregister_pm_notifier(&ap_power_notifier);
2024 root_device_unregister(ap_root_device);
2025 bus_unregister(&ap_bus_type);
2026 kfree(ap_configuration);
2027 unregister_reset_call(&ap_reset_call);
2028 if (ap_using_interrupts())
2029 unregister_adapter_interrupt(&ap_airq);
2032 module_init(ap_module_init);
2033 module_exit(ap_module_exit);