2 * Copyright IBM Corp. 2006, 2012
3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
6 * Felix Beck <felix.beck@de.ibm.com>
7 * Holger Dengler <hd@linux.vnet.ibm.com>
9 * Adjunct processor bus.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/kernel_stat.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <asm/reset.h>
42 #include <linux/atomic.h>
44 #include <linux/hrtimer.h>
45 #include <linux/ktime.h>
46 #include <asm/facility.h>
50 /* Some prototypes. */
51 static void ap_scan_bus(struct work_struct
*);
52 static void ap_poll_all(unsigned long);
53 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*);
54 static int ap_poll_thread_start(void);
55 static void ap_poll_thread_stop(void);
56 static void ap_request_timeout(unsigned long);
57 static inline void ap_schedule_poll_timer(void);
58 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
);
59 static int ap_device_remove(struct device
*dev
);
60 static int ap_device_probe(struct device
*dev
);
61 static void ap_interrupt_handler(void *unused1
, void *unused2
);
62 static void ap_reset(struct ap_device
*ap_dev
);
63 static void ap_config_timeout(unsigned long ptr
);
64 static int ap_select_domain(void);
65 static void ap_query_configuration(void);
70 MODULE_AUTHOR("IBM Corporation");
71 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
72 "Copyright IBM Corp. 2006, 2012");
73 MODULE_LICENSE("GPL");
78 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
79 module_param_named(domain
, ap_domain_index
, int, 0000);
80 MODULE_PARM_DESC(domain
, "domain index for ap devices");
81 EXPORT_SYMBOL(ap_domain_index
);
83 static int ap_thread_flag
= 0;
84 module_param_named(poll_thread
, ap_thread_flag
, int, 0000);
85 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
87 static struct device
*ap_root_device
= NULL
;
88 static struct ap_config_info
*ap_configuration
;
89 static DEFINE_SPINLOCK(ap_device_list_lock
);
90 static LIST_HEAD(ap_device_list
);
93 * Workqueue & timer for bus rescan.
95 static struct workqueue_struct
*ap_work_queue
;
96 static struct timer_list ap_config_timer
;
97 static int ap_config_time
= AP_CONFIG_TIME
;
98 static DECLARE_WORK(ap_config_work
, ap_scan_bus
);
101 * Tasklet & timer for AP request polling and interrupts
103 static DECLARE_TASKLET(ap_tasklet
, ap_poll_all
, 0);
104 static atomic_t ap_poll_requests
= ATOMIC_INIT(0);
105 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
106 static struct task_struct
*ap_poll_kthread
= NULL
;
107 static DEFINE_MUTEX(ap_poll_thread_mutex
);
108 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
109 static void *ap_interrupt_indicator
;
110 static struct hrtimer ap_poll_timer
;
111 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
112 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
113 static unsigned long long poll_timeout
= 250000;
116 static int ap_suspend_flag
;
117 /* Flag to check if domain was set through module parameter domain=. This is
118 * important when supsend and resume is done in a z/VM environment where the
119 * domain might change. */
120 static int user_set_domain
= 0;
121 static struct bus_type ap_bus_type
;
124 * ap_using_interrupts() - Returns non-zero if interrupt support is
127 static inline int ap_using_interrupts(void)
129 return ap_interrupt_indicator
!= NULL
;
133 * ap_intructions_available() - Test if AP instructions are available.
135 * Returns 0 if the AP instructions are installed.
137 static inline int ap_instructions_available(void)
139 register unsigned long reg0
asm ("0") = AP_MKQID(0,0);
140 register unsigned long reg1
asm ("1") = -ENODEV
;
141 register unsigned long reg2
asm ("2") = 0UL;
144 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
148 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
) : : "cc" );
153 * ap_interrupts_available(): Test if AP interrupts are available.
155 * Returns 1 if AP interrupts are available.
157 static int ap_interrupts_available(void)
159 return test_facility(2) && test_facility(65);
163 * ap_configuration_available(): Test if AP configuration
164 * information is available.
166 * Returns 1 if AP configuration information is available.
169 static int ap_configuration_available(void)
171 return test_facility(2) && test_facility(12);
176 * ap_test_queue(): Test adjunct processor queue.
177 * @qid: The AP queue number
178 * @queue_depth: Pointer to queue depth value
179 * @device_type: Pointer to device type value
181 * Returns AP queue status structure.
183 static inline struct ap_queue_status
184 ap_test_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
186 register unsigned long reg0
asm ("0") = qid
;
187 register struct ap_queue_status reg1
asm ("1");
188 register unsigned long reg2
asm ("2") = 0UL;
190 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
191 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
192 *device_type
= (int) (reg2
>> 24);
193 *queue_depth
= (int) (reg2
& 0xff);
198 * ap_reset_queue(): Reset adjunct processor queue.
199 * @qid: The AP queue number
201 * Returns AP queue status structure.
203 static inline struct ap_queue_status
ap_reset_queue(ap_qid_t qid
)
205 register unsigned long reg0
asm ("0") = qid
| 0x01000000UL
;
206 register struct ap_queue_status reg1
asm ("1");
207 register unsigned long reg2
asm ("2") = 0UL;
210 ".long 0xb2af0000" /* PQAP(RAPQ) */
211 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
217 * ap_queue_interruption_control(): Enable interruption for a specific AP.
218 * @qid: The AP queue number
219 * @ind: The notification indicator byte
221 * Returns AP queue status.
223 static inline struct ap_queue_status
224 ap_queue_interruption_control(ap_qid_t qid
, void *ind
)
226 register unsigned long reg0
asm ("0") = qid
| 0x03000000UL
;
227 register unsigned long reg1_in
asm ("1") = 0x0000800000000000UL
| AP_ISC
;
228 register struct ap_queue_status reg1_out
asm ("1");
229 register void *reg2
asm ("2") = ind
;
231 ".long 0xb2af0000" /* PQAP(AQIC) */
232 : "+d" (reg0
), "+d" (reg1_in
), "=d" (reg1_out
), "+d" (reg2
)
240 static inline struct ap_queue_status
241 __ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
243 register unsigned long reg0
asm ("0") = 0UL | qid
| (1UL << 23);
244 register struct ap_queue_status reg1
asm ("1") = AP_QUEUE_STATUS_INVALID
;
245 register unsigned long reg2
asm ("2");
248 ".long 0xb2af0000\n" /* PQAP(TAPQ) */
251 : "+d" (reg0
), "+d" (reg1
), "=d" (reg2
)
255 *functions
= (unsigned int)(reg2
>> 32);
261 static inline int __ap_query_configuration(struct ap_config_info
*config
)
263 register unsigned long reg0
asm ("0") = 0x04000000UL
;
264 register unsigned long reg1
asm ("1") = -EINVAL
;
265 register unsigned char *reg2
asm ("2") = (unsigned char *)config
;
268 ".long 0xb2af0000\n" /* PQAP(QCI) */
272 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
)
281 * ap_query_functions(): Query supported functions.
282 * @qid: The AP queue number
283 * @functions: Pointer to functions field.
287 * -ENODEV if queue not valid.
288 * -EBUSY if device busy.
289 * -EINVAL if query function is not supported
291 static int ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
294 struct ap_queue_status status
;
296 status
= __ap_query_functions(qid
, functions
);
298 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
299 if (ap_queue_status_invalid_test(&status
))
302 switch (status
.response_code
) {
303 case AP_RESPONSE_NORMAL
:
305 case AP_RESPONSE_RESET_IN_PROGRESS
:
306 case AP_RESPONSE_BUSY
:
308 case AP_RESPONSE_Q_NOT_AVAIL
:
309 case AP_RESPONSE_DECONFIGURED
:
310 case AP_RESPONSE_CHECKSTOPPED
:
311 case AP_RESPONSE_INVALID_ADDRESS
:
313 case AP_RESPONSE_OTHERWISE_CHANGED
:
318 if (i
< AP_MAX_RESET
- 1) {
320 status
= __ap_query_functions(qid
, functions
);
330 * ap_queue_enable_interruption(): Enable interruption on an AP.
331 * @qid: The AP queue number
332 * @ind: the notification indicator byte
334 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
335 * on the return value it waits a while and tests the AP queue if interrupts
336 * have been switched on using ap_test_queue().
338 static int ap_queue_enable_interruption(ap_qid_t qid
, void *ind
)
341 struct ap_queue_status status
;
342 int t_depth
, t_device_type
, rc
, i
;
345 status
= ap_queue_interruption_control(qid
, ind
);
347 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
348 switch (status
.response_code
) {
349 case AP_RESPONSE_NORMAL
:
350 if (status
.int_enabled
)
353 case AP_RESPONSE_RESET_IN_PROGRESS
:
354 case AP_RESPONSE_BUSY
:
355 if (i
< AP_MAX_RESET
- 1) {
357 status
= ap_queue_interruption_control(qid
,
362 case AP_RESPONSE_Q_NOT_AVAIL
:
363 case AP_RESPONSE_DECONFIGURED
:
364 case AP_RESPONSE_CHECKSTOPPED
:
365 case AP_RESPONSE_INVALID_ADDRESS
:
367 case AP_RESPONSE_OTHERWISE_CHANGED
:
368 if (status
.int_enabled
)
374 if (i
< AP_MAX_RESET
- 1) {
376 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
386 * __ap_send(): Send message to adjunct processor queue.
387 * @qid: The AP queue number
388 * @psmid: The program supplied message identifier
389 * @msg: The message text
390 * @length: The message length
391 * @special: Special Bit
393 * Returns AP queue status structure.
394 * Condition code 1 on NQAP can't happen because the L bit is 1.
395 * Condition code 2 on NQAP also means the send is incomplete,
396 * because a segment boundary was reached. The NQAP is repeated.
398 static inline struct ap_queue_status
399 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
,
400 unsigned int special
)
402 typedef struct { char _
[length
]; } msgblock
;
403 register unsigned long reg0
asm ("0") = qid
| 0x40000000UL
;
404 register struct ap_queue_status reg1
asm ("1");
405 register unsigned long reg2
asm ("2") = (unsigned long) msg
;
406 register unsigned long reg3
asm ("3") = (unsigned long) length
;
407 register unsigned long reg4
asm ("4") = (unsigned int) (psmid
>> 32);
408 register unsigned long reg5
asm ("5") = (unsigned int) psmid
;
414 "0: .long 0xb2ad0042\n" /* NQAP */
416 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
), "+d" (reg3
)
417 : "d" (reg4
), "d" (reg5
), "m" (*(msgblock
*) msg
)
422 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
424 struct ap_queue_status status
;
426 status
= __ap_send(qid
, psmid
, msg
, length
, 0);
427 switch (status
.response_code
) {
428 case AP_RESPONSE_NORMAL
:
430 case AP_RESPONSE_Q_FULL
:
431 case AP_RESPONSE_RESET_IN_PROGRESS
:
433 case AP_RESPONSE_REQ_FAC_NOT_INST
:
435 default: /* Device is gone. */
439 EXPORT_SYMBOL(ap_send
);
442 * __ap_recv(): Receive message from adjunct processor queue.
443 * @qid: The AP queue number
444 * @psmid: Pointer to program supplied message identifier
445 * @msg: The message text
446 * @length: The message length
448 * Returns AP queue status structure.
449 * Condition code 1 on DQAP means the receive has taken place
450 * but only partially. The response is incomplete, hence the
452 * Condition code 2 on DQAP also means the receive is incomplete,
453 * this time because a segment boundary was reached. Again, the
455 * Note that gpr2 is used by the DQAP instruction to keep track of
456 * any 'residual' length, in case the instruction gets interrupted.
457 * Hence it gets zeroed before the instruction.
459 static inline struct ap_queue_status
460 __ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
462 typedef struct { char _
[length
]; } msgblock
;
463 register unsigned long reg0
asm("0") = qid
| 0x80000000UL
;
464 register struct ap_queue_status reg1
asm ("1");
465 register unsigned long reg2
asm("2") = 0UL;
466 register unsigned long reg4
asm("4") = (unsigned long) msg
;
467 register unsigned long reg5
asm("5") = (unsigned long) length
;
468 register unsigned long reg6
asm("6") = 0UL;
469 register unsigned long reg7
asm("7") = 0UL;
473 "0: .long 0xb2ae0064\n" /* DQAP */
475 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
),
476 "+d" (reg4
), "+d" (reg5
), "+d" (reg6
), "+d" (reg7
),
477 "=m" (*(msgblock
*) msg
) : : "cc" );
478 *psmid
= (((unsigned long long) reg6
) << 32) + reg7
;
482 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
484 struct ap_queue_status status
;
486 status
= __ap_recv(qid
, psmid
, msg
, length
);
487 switch (status
.response_code
) {
488 case AP_RESPONSE_NORMAL
:
490 case AP_RESPONSE_NO_PENDING_REPLY
:
491 if (status
.queue_empty
)
494 case AP_RESPONSE_RESET_IN_PROGRESS
:
500 EXPORT_SYMBOL(ap_recv
);
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
508 * The test is repeated for AP_MAX_RESET times.
510 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
512 struct ap_queue_status status
;
513 int t_depth
, t_device_type
, rc
, i
;
516 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
517 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
518 switch (status
.response_code
) {
519 case AP_RESPONSE_NORMAL
:
520 *queue_depth
= t_depth
+ 1;
521 *device_type
= t_device_type
;
524 case AP_RESPONSE_Q_NOT_AVAIL
:
527 case AP_RESPONSE_RESET_IN_PROGRESS
:
529 case AP_RESPONSE_DECONFIGURED
:
532 case AP_RESPONSE_CHECKSTOPPED
:
535 case AP_RESPONSE_INVALID_ADDRESS
:
538 case AP_RESPONSE_OTHERWISE_CHANGED
:
540 case AP_RESPONSE_BUSY
:
547 if (i
< AP_MAX_RESET
- 1)
554 * ap_init_queue(): Reset an AP queue.
555 * @qid: The AP queue number
557 * Reset an AP queue and wait for it to become available again.
559 static int ap_init_queue(ap_qid_t qid
)
561 struct ap_queue_status status
;
565 status
= ap_reset_queue(qid
);
566 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
567 switch (status
.response_code
) {
568 case AP_RESPONSE_NORMAL
:
569 if (status
.queue_empty
)
572 case AP_RESPONSE_Q_NOT_AVAIL
:
573 case AP_RESPONSE_DECONFIGURED
:
574 case AP_RESPONSE_CHECKSTOPPED
:
575 i
= AP_MAX_RESET
; /* return with -ENODEV */
577 case AP_RESPONSE_RESET_IN_PROGRESS
:
579 case AP_RESPONSE_BUSY
:
583 if (rc
!= -ENODEV
&& rc
!= -EBUSY
)
585 if (i
< AP_MAX_RESET
- 1) {
587 status
= ap_test_queue(qid
, &dummy
, &dummy
);
590 if (rc
== 0 && ap_using_interrupts()) {
591 rc
= ap_queue_enable_interruption(qid
, ap_interrupt_indicator
);
592 /* If interruption mode is supported by the machine,
593 * but an AP can not be enabled for interruption then
594 * the AP will be discarded. */
596 pr_err("Registering adapter interrupts for "
597 "AP %d failed\n", AP_QID_DEVICE(qid
));
603 * ap_increase_queue_count(): Arm request timeout.
604 * @ap_dev: Pointer to an AP device.
606 * Arm request timeout if an AP device was idle and a new request is submitted.
608 static void ap_increase_queue_count(struct ap_device
*ap_dev
)
610 int timeout
= ap_dev
->drv
->request_timeout
;
612 ap_dev
->queue_count
++;
613 if (ap_dev
->queue_count
== 1) {
614 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
615 ap_dev
->reset
= AP_RESET_ARMED
;
620 * ap_decrease_queue_count(): Decrease queue count.
621 * @ap_dev: Pointer to an AP device.
623 * If AP device is still alive, re-schedule request timeout if there are still
626 static void ap_decrease_queue_count(struct ap_device
*ap_dev
)
628 int timeout
= ap_dev
->drv
->request_timeout
;
630 ap_dev
->queue_count
--;
631 if (ap_dev
->queue_count
> 0)
632 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
635 * The timeout timer should to be disabled now - since
636 * del_timer_sync() is very expensive, we just tell via the
637 * reset flag to ignore the pending timeout timer.
639 ap_dev
->reset
= AP_RESET_IGNORE
;
643 * AP device related attributes.
645 static ssize_t
ap_hwtype_show(struct device
*dev
,
646 struct device_attribute
*attr
, char *buf
)
648 struct ap_device
*ap_dev
= to_ap_dev(dev
);
649 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->device_type
);
652 static DEVICE_ATTR(hwtype
, 0444, ap_hwtype_show
, NULL
);
653 static ssize_t
ap_depth_show(struct device
*dev
, struct device_attribute
*attr
,
656 struct ap_device
*ap_dev
= to_ap_dev(dev
);
657 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->queue_depth
);
660 static DEVICE_ATTR(depth
, 0444, ap_depth_show
, NULL
);
661 static ssize_t
ap_request_count_show(struct device
*dev
,
662 struct device_attribute
*attr
,
665 struct ap_device
*ap_dev
= to_ap_dev(dev
);
668 spin_lock_bh(&ap_dev
->lock
);
669 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->total_request_count
);
670 spin_unlock_bh(&ap_dev
->lock
);
674 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
676 static ssize_t
ap_requestq_count_show(struct device
*dev
,
677 struct device_attribute
*attr
, char *buf
)
679 struct ap_device
*ap_dev
= to_ap_dev(dev
);
682 spin_lock_bh(&ap_dev
->lock
);
683 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->requestq_count
);
684 spin_unlock_bh(&ap_dev
->lock
);
688 static DEVICE_ATTR(requestq_count
, 0444, ap_requestq_count_show
, NULL
);
690 static ssize_t
ap_pendingq_count_show(struct device
*dev
,
691 struct device_attribute
*attr
, char *buf
)
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
->pendingq_count
);
698 spin_unlock_bh(&ap_dev
->lock
);
702 static DEVICE_ATTR(pendingq_count
, 0444, ap_pendingq_count_show
, NULL
);
704 static ssize_t
ap_modalias_show(struct device
*dev
,
705 struct device_attribute
*attr
, char *buf
)
707 return sprintf(buf
, "ap:t%02X", to_ap_dev(dev
)->device_type
);
710 static DEVICE_ATTR(modalias
, 0444, ap_modalias_show
, NULL
);
712 static ssize_t
ap_functions_show(struct device
*dev
,
713 struct device_attribute
*attr
, char *buf
)
715 struct ap_device
*ap_dev
= to_ap_dev(dev
);
716 return snprintf(buf
, PAGE_SIZE
, "0x%08X\n", ap_dev
->functions
);
719 static DEVICE_ATTR(ap_functions
, 0444, ap_functions_show
, NULL
);
721 static struct attribute
*ap_dev_attrs
[] = {
722 &dev_attr_hwtype
.attr
,
723 &dev_attr_depth
.attr
,
724 &dev_attr_request_count
.attr
,
725 &dev_attr_requestq_count
.attr
,
726 &dev_attr_pendingq_count
.attr
,
727 &dev_attr_modalias
.attr
,
728 &dev_attr_ap_functions
.attr
,
731 static struct attribute_group ap_dev_attr_group
= {
732 .attrs
= ap_dev_attrs
737 * @dev: Pointer to device
738 * @drv: Pointer to device_driver
740 * AP bus driver registration/unregistration.
742 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
744 struct ap_device
*ap_dev
= to_ap_dev(dev
);
745 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
746 struct ap_device_id
*id
;
749 * Compare device type of the device with the list of
750 * supported types of the device_driver.
752 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
753 if ((id
->match_flags
& AP_DEVICE_ID_MATCH_DEVICE_TYPE
) &&
754 (id
->dev_type
!= ap_dev
->device_type
))
762 * ap_uevent(): Uevent function for AP devices.
763 * @dev: Pointer to device
764 * @env: Pointer to kobj_uevent_env
766 * It sets up a single environment variable DEV_TYPE which contains the
767 * hardware device type.
769 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
771 struct ap_device
*ap_dev
= to_ap_dev(dev
);
777 /* Set up DEV_TYPE environment variable. */
778 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
783 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
788 static int ap_bus_suspend(struct device
*dev
, pm_message_t state
)
790 struct ap_device
*ap_dev
= to_ap_dev(dev
);
793 if (!ap_suspend_flag
) {
796 /* Disable scanning for devices, thus we do not want to scan
797 * for them after removing.
799 del_timer_sync(&ap_config_timer
);
800 if (ap_work_queue
!= NULL
) {
801 destroy_workqueue(ap_work_queue
);
802 ap_work_queue
= NULL
;
805 tasklet_disable(&ap_tasklet
);
807 /* Poll on the device until all requests are finished. */
810 spin_lock_bh(&ap_dev
->lock
);
811 __ap_poll_device(ap_dev
, &flags
);
812 spin_unlock_bh(&ap_dev
->lock
);
813 } while ((flags
& 1) || (flags
& 2));
815 spin_lock_bh(&ap_dev
->lock
);
816 ap_dev
->unregistered
= 1;
817 spin_unlock_bh(&ap_dev
->lock
);
822 static int ap_bus_resume(struct device
*dev
)
825 struct ap_device
*ap_dev
= to_ap_dev(dev
);
827 if (ap_suspend_flag
) {
829 if (!ap_interrupts_available())
830 ap_interrupt_indicator
= NULL
;
831 ap_query_configuration();
832 if (!user_set_domain
) {
833 ap_domain_index
= -1;
836 init_timer(&ap_config_timer
);
837 ap_config_timer
.function
= ap_config_timeout
;
838 ap_config_timer
.data
= 0;
839 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
840 add_timer(&ap_config_timer
);
841 ap_work_queue
= create_singlethread_workqueue("kapwork");
844 tasklet_enable(&ap_tasklet
);
845 if (!ap_using_interrupts())
846 ap_schedule_poll_timer();
848 tasklet_schedule(&ap_tasklet
);
850 rc
= ap_poll_thread_start();
852 if (AP_QID_QUEUE(ap_dev
->qid
) != ap_domain_index
) {
853 spin_lock_bh(&ap_dev
->lock
);
854 ap_dev
->qid
= AP_MKQID(AP_QID_DEVICE(ap_dev
->qid
),
856 spin_unlock_bh(&ap_dev
->lock
);
858 queue_work(ap_work_queue
, &ap_config_work
);
863 static struct bus_type ap_bus_type
= {
865 .match
= &ap_bus_match
,
866 .uevent
= &ap_uevent
,
867 .suspend
= ap_bus_suspend
,
868 .resume
= ap_bus_resume
871 static int ap_device_probe(struct device
*dev
)
873 struct ap_device
*ap_dev
= to_ap_dev(dev
);
874 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
877 ap_dev
->drv
= ap_drv
;
878 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
880 spin_lock_bh(&ap_device_list_lock
);
881 list_add(&ap_dev
->list
, &ap_device_list
);
882 spin_unlock_bh(&ap_device_list_lock
);
888 * __ap_flush_queue(): Flush requests.
889 * @ap_dev: Pointer to the AP device
891 * Flush all requests from the request/pending queue of an AP device.
893 static void __ap_flush_queue(struct ap_device
*ap_dev
)
895 struct ap_message
*ap_msg
, *next
;
897 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->pendingq
, list
) {
898 list_del_init(&ap_msg
->list
);
899 ap_dev
->pendingq_count
--;
900 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
902 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->requestq
, list
) {
903 list_del_init(&ap_msg
->list
);
904 ap_dev
->requestq_count
--;
905 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
909 void ap_flush_queue(struct ap_device
*ap_dev
)
911 spin_lock_bh(&ap_dev
->lock
);
912 __ap_flush_queue(ap_dev
);
913 spin_unlock_bh(&ap_dev
->lock
);
915 EXPORT_SYMBOL(ap_flush_queue
);
917 static int ap_device_remove(struct device
*dev
)
919 struct ap_device
*ap_dev
= to_ap_dev(dev
);
920 struct ap_driver
*ap_drv
= ap_dev
->drv
;
922 ap_flush_queue(ap_dev
);
923 del_timer_sync(&ap_dev
->timeout
);
924 spin_lock_bh(&ap_device_list_lock
);
925 list_del_init(&ap_dev
->list
);
926 spin_unlock_bh(&ap_device_list_lock
);
928 ap_drv
->remove(ap_dev
);
929 spin_lock_bh(&ap_dev
->lock
);
930 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
931 spin_unlock_bh(&ap_dev
->lock
);
935 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
938 struct device_driver
*drv
= &ap_drv
->driver
;
940 drv
->bus
= &ap_bus_type
;
941 drv
->probe
= ap_device_probe
;
942 drv
->remove
= ap_device_remove
;
945 return driver_register(drv
);
947 EXPORT_SYMBOL(ap_driver_register
);
949 void ap_driver_unregister(struct ap_driver
*ap_drv
)
951 driver_unregister(&ap_drv
->driver
);
953 EXPORT_SYMBOL(ap_driver_unregister
);
955 void ap_bus_force_rescan(void)
957 /* reconfigure the AP bus rescan timer. */
958 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
959 /* processing a asynchronous bus rescan */
960 queue_work(ap_work_queue
, &ap_config_work
);
961 flush_work(&ap_config_work
);
963 EXPORT_SYMBOL(ap_bus_force_rescan
);
968 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
970 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
973 static BUS_ATTR(ap_domain
, 0444, ap_domain_show
, NULL
);
975 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
977 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
980 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
982 return snprintf(buf
, PAGE_SIZE
, "%d\n",
983 ap_using_interrupts() ? 1 : 0);
986 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
988 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
989 const char *buf
, size_t count
)
993 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
995 ap_config_time
= time
;
996 if (!timer_pending(&ap_config_timer
) ||
997 !mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
)) {
998 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
999 add_timer(&ap_config_timer
);
1004 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
1006 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
1008 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
1011 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
1012 const char *buf
, size_t count
)
1016 if (sscanf(buf
, "%d\n", &flag
) != 1)
1019 rc
= ap_poll_thread_start();
1024 ap_poll_thread_stop();
1028 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
1030 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
1032 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
1035 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
1038 unsigned long long time
;
1041 /* 120 seconds = maximum poll interval */
1042 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
1043 time
> 120000000000ULL)
1045 poll_timeout
= time
;
1046 hr_time
= ktime_set(0, poll_timeout
);
1048 if (!hrtimer_is_queued(&ap_poll_timer
) ||
1049 !hrtimer_forward(&ap_poll_timer
, hrtimer_get_expires(&ap_poll_timer
), hr_time
)) {
1050 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
1051 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
1056 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
1058 static struct bus_attribute
*const ap_bus_attrs
[] = {
1059 &bus_attr_ap_domain
,
1060 &bus_attr_config_time
,
1061 &bus_attr_poll_thread
,
1062 &bus_attr_ap_interrupts
,
1063 &bus_attr_poll_timeout
,
1067 static inline int ap_test_config(unsigned int *field
, unsigned int nr
)
1071 return ap_test_bit((field
+ (nr
>> 5)), (nr
& 0x1f));
1075 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
1078 * Returns 0 if the card is not configured
1079 * 1 if the card is configured or
1080 * if the configuration information is not available
1082 static inline int ap_test_config_card_id(unsigned int id
)
1084 if (!ap_configuration
)
1086 return ap_test_config(ap_configuration
->apm
, id
);
1090 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
1091 * @domain AP usage domain ID
1093 * Returns 0 if the usage domain is not configured
1094 * 1 if the usage domain is configured or
1095 * if the configuration information is not available
1097 static inline int ap_test_config_domain(unsigned int domain
)
1099 if (!ap_configuration
)
1101 return ap_test_config(ap_configuration
->aqm
, domain
);
1105 * ap_query_configuration(): Query AP configuration information.
1107 * Query information of installed cards and configured domains from AP.
1109 static void ap_query_configuration(void)
1112 if (ap_configuration_available()) {
1113 if (!ap_configuration
)
1115 kzalloc(sizeof(struct ap_config_info
),
1117 if (ap_configuration
)
1118 __ap_query_configuration(ap_configuration
);
1120 ap_configuration
= NULL
;
1122 ap_configuration
= NULL
;
1127 * ap_select_domain(): Select an AP domain.
1129 * Pick one of the 16 AP domains.
1131 static int ap_select_domain(void)
1133 int queue_depth
, device_type
, count
, max_count
, best_domain
;
1138 * We want to use a single domain. Either the one specified with
1139 * the "domain=" parameter or the domain with the maximum number
1142 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
)
1143 /* Domain has already been selected. */
1147 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1148 if (!ap_test_config_domain(i
))
1151 for (j
= 0; j
< AP_DEVICES
; j
++) {
1152 if (!ap_test_config_card_id(j
))
1154 qid
= AP_MKQID(j
, i
);
1155 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1160 if (count
> max_count
) {
1165 if (best_domain
>= 0){
1166 ap_domain_index
= best_domain
;
1173 * ap_probe_device_type(): Find the device type of an AP.
1174 * @ap_dev: pointer to the AP device.
1176 * Find the device type if query queue returned a device type of 0.
1178 static int ap_probe_device_type(struct ap_device
*ap_dev
)
1180 static unsigned char msg
[] = {
1181 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1182 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1183 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1184 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1185 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1186 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1187 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1188 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1189 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1190 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1191 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1192 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1193 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1194 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1195 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1196 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1197 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1198 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1199 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1200 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1201 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1202 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1203 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1204 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1205 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1206 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1207 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1208 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1209 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1210 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1211 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1212 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1213 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1214 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1215 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1216 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1217 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1218 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1219 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1220 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1221 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1222 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1223 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1224 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1225 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1227 struct ap_queue_status status
;
1228 unsigned long long psmid
;
1232 reply
= (void *) get_zeroed_page(GFP_KERNEL
);
1238 status
= __ap_send(ap_dev
->qid
, 0x0102030405060708ULL
,
1239 msg
, sizeof(msg
), 0);
1240 if (status
.response_code
!= AP_RESPONSE_NORMAL
) {
1245 /* Wait for the test message to complete. */
1246 for (i
= 0; i
< 6; i
++) {
1248 status
= __ap_recv(ap_dev
->qid
, &psmid
, reply
, 4096);
1249 if (status
.response_code
== AP_RESPONSE_NORMAL
&&
1250 psmid
== 0x0102030405060708ULL
)
1254 /* Got an answer. */
1255 if (reply
[0] == 0x00 && reply
[1] == 0x86)
1256 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICC
;
1258 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICA
;
1264 free_page((unsigned long) reply
);
1269 static void ap_interrupt_handler(void *unused1
, void *unused2
)
1271 inc_irq_stat(IRQIO_APB
);
1272 tasklet_schedule(&ap_tasklet
);
1276 * __ap_scan_bus(): Scan the AP bus.
1277 * @dev: Pointer to device
1278 * @data: Pointer to data
1280 * Scan the AP bus for new devices.
1282 static int __ap_scan_bus(struct device
*dev
, void *data
)
1284 return to_ap_dev(dev
)->qid
== (ap_qid_t
)(unsigned long) data
;
1287 static void ap_device_release(struct device
*dev
)
1289 struct ap_device
*ap_dev
= to_ap_dev(dev
);
1294 static void ap_scan_bus(struct work_struct
*unused
)
1296 struct ap_device
*ap_dev
;
1299 int queue_depth
, device_type
;
1300 unsigned int device_functions
;
1303 ap_query_configuration();
1304 if (ap_select_domain() != 0) {
1307 for (i
= 0; i
< AP_DEVICES
; i
++) {
1308 qid
= AP_MKQID(i
, ap_domain_index
);
1309 dev
= bus_find_device(&ap_bus_type
, NULL
,
1310 (void *)(unsigned long)qid
,
1312 if (ap_test_config_card_id(i
))
1313 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1318 set_current_state(TASK_UNINTERRUPTIBLE
);
1319 schedule_timeout(AP_RESET_TIMEOUT
);
1320 rc
= ap_query_queue(qid
, &queue_depth
,
1323 ap_dev
= to_ap_dev(dev
);
1324 spin_lock_bh(&ap_dev
->lock
);
1325 if (rc
|| ap_dev
->unregistered
) {
1326 spin_unlock_bh(&ap_dev
->lock
);
1327 if (ap_dev
->unregistered
)
1329 device_unregister(dev
);
1333 spin_unlock_bh(&ap_dev
->lock
);
1339 rc
= ap_init_queue(qid
);
1342 ap_dev
= kzalloc(sizeof(*ap_dev
), GFP_KERNEL
);
1346 ap_dev
->queue_depth
= queue_depth
;
1347 ap_dev
->unregistered
= 1;
1348 spin_lock_init(&ap_dev
->lock
);
1349 INIT_LIST_HEAD(&ap_dev
->pendingq
);
1350 INIT_LIST_HEAD(&ap_dev
->requestq
);
1351 INIT_LIST_HEAD(&ap_dev
->list
);
1352 setup_timer(&ap_dev
->timeout
, ap_request_timeout
,
1353 (unsigned long) ap_dev
);
1354 switch (device_type
) {
1356 /* device type probing for old cards */
1357 if (ap_probe_device_type(ap_dev
)) {
1363 ap_dev
->device_type
= device_type
;
1366 rc
= ap_query_functions(qid
, &device_functions
);
1368 ap_dev
->functions
= device_functions
;
1370 ap_dev
->functions
= 0u;
1372 ap_dev
->device
.bus
= &ap_bus_type
;
1373 ap_dev
->device
.parent
= ap_root_device
;
1374 if (dev_set_name(&ap_dev
->device
, "card%02x",
1375 AP_QID_DEVICE(ap_dev
->qid
))) {
1379 ap_dev
->device
.release
= ap_device_release
;
1380 rc
= device_register(&ap_dev
->device
);
1382 put_device(&ap_dev
->device
);
1385 /* Add device attributes. */
1386 rc
= sysfs_create_group(&ap_dev
->device
.kobj
,
1387 &ap_dev_attr_group
);
1389 spin_lock_bh(&ap_dev
->lock
);
1390 ap_dev
->unregistered
= 0;
1391 spin_unlock_bh(&ap_dev
->lock
);
1394 device_unregister(&ap_dev
->device
);
1399 ap_config_timeout(unsigned long ptr
)
1401 queue_work(ap_work_queue
, &ap_config_work
);
1402 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1403 add_timer(&ap_config_timer
);
1407 * __ap_schedule_poll_timer(): Schedule poll timer.
1409 * Set up the timer to run the poll tasklet
1411 static inline void __ap_schedule_poll_timer(void)
1415 spin_lock_bh(&ap_poll_timer_lock
);
1416 if (hrtimer_is_queued(&ap_poll_timer
) || ap_suspend_flag
)
1418 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer
)) <= 0) {
1419 hr_time
= ktime_set(0, poll_timeout
);
1420 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
1421 hrtimer_restart(&ap_poll_timer
);
1424 spin_unlock_bh(&ap_poll_timer_lock
);
1428 * ap_schedule_poll_timer(): Schedule poll timer.
1430 * Set up the timer to run the poll tasklet
1432 static inline void ap_schedule_poll_timer(void)
1434 if (ap_using_interrupts())
1436 __ap_schedule_poll_timer();
1440 * ap_poll_read(): Receive pending reply messages from an AP device.
1441 * @ap_dev: pointer to the AP device
1442 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1443 * required, bit 2^1 is set if the poll timer needs to get armed
1445 * Returns 0 if the device is still present, -ENODEV if not.
1447 static int ap_poll_read(struct ap_device
*ap_dev
, unsigned long *flags
)
1449 struct ap_queue_status status
;
1450 struct ap_message
*ap_msg
;
1452 if (ap_dev
->queue_count
<= 0)
1454 status
= __ap_recv(ap_dev
->qid
, &ap_dev
->reply
->psmid
,
1455 ap_dev
->reply
->message
, ap_dev
->reply
->length
);
1456 switch (status
.response_code
) {
1457 case AP_RESPONSE_NORMAL
:
1458 atomic_dec(&ap_poll_requests
);
1459 ap_decrease_queue_count(ap_dev
);
1460 list_for_each_entry(ap_msg
, &ap_dev
->pendingq
, list
) {
1461 if (ap_msg
->psmid
!= ap_dev
->reply
->psmid
)
1463 list_del_init(&ap_msg
->list
);
1464 ap_dev
->pendingq_count
--;
1465 ap_msg
->receive(ap_dev
, ap_msg
, ap_dev
->reply
);
1468 if (ap_dev
->queue_count
> 0)
1471 case AP_RESPONSE_NO_PENDING_REPLY
:
1472 if (status
.queue_empty
) {
1473 /* The card shouldn't forget requests but who knows. */
1474 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1475 ap_dev
->queue_count
= 0;
1476 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1477 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1478 ap_dev
->pendingq_count
= 0;
1489 * ap_poll_write(): Send messages from the request queue to an AP device.
1490 * @ap_dev: pointer to the AP device
1491 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1492 * required, bit 2^1 is set if the poll timer needs to get armed
1494 * Returns 0 if the device is still present, -ENODEV if not.
1496 static int ap_poll_write(struct ap_device
*ap_dev
, unsigned long *flags
)
1498 struct ap_queue_status status
;
1499 struct ap_message
*ap_msg
;
1501 if (ap_dev
->requestq_count
<= 0 ||
1502 ap_dev
->queue_count
>= ap_dev
->queue_depth
)
1504 /* Start the next request on the queue. */
1505 ap_msg
= list_entry(ap_dev
->requestq
.next
, struct ap_message
, list
);
1506 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1507 ap_msg
->message
, ap_msg
->length
, ap_msg
->special
);
1508 switch (status
.response_code
) {
1509 case AP_RESPONSE_NORMAL
:
1510 atomic_inc(&ap_poll_requests
);
1511 ap_increase_queue_count(ap_dev
);
1512 list_move_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1513 ap_dev
->requestq_count
--;
1514 ap_dev
->pendingq_count
++;
1515 if (ap_dev
->queue_count
< ap_dev
->queue_depth
&&
1516 ap_dev
->requestq_count
> 0)
1520 case AP_RESPONSE_RESET_IN_PROGRESS
:
1521 __ap_schedule_poll_timer();
1522 case AP_RESPONSE_Q_FULL
:
1525 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1526 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1535 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1536 * @ap_dev: pointer to the bus device
1537 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1538 * required, bit 2^1 is set if the poll timer needs to get armed
1540 * Poll AP device for pending replies and send new messages. If either
1541 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1544 static inline int ap_poll_queue(struct ap_device
*ap_dev
, unsigned long *flags
)
1548 rc
= ap_poll_read(ap_dev
, flags
);
1551 return ap_poll_write(ap_dev
, flags
);
1555 * __ap_queue_message(): Queue a message to a device.
1556 * @ap_dev: pointer to the AP device
1557 * @ap_msg: the message to be queued
1559 * Queue a message to a device. Returns 0 if successful.
1561 static int __ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1563 struct ap_queue_status status
;
1565 if (list_empty(&ap_dev
->requestq
) &&
1566 ap_dev
->queue_count
< ap_dev
->queue_depth
) {
1567 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1568 ap_msg
->message
, ap_msg
->length
,
1570 switch (status
.response_code
) {
1571 case AP_RESPONSE_NORMAL
:
1572 list_add_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1573 atomic_inc(&ap_poll_requests
);
1574 ap_dev
->pendingq_count
++;
1575 ap_increase_queue_count(ap_dev
);
1576 ap_dev
->total_request_count
++;
1578 case AP_RESPONSE_Q_FULL
:
1579 case AP_RESPONSE_RESET_IN_PROGRESS
:
1580 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1581 ap_dev
->requestq_count
++;
1582 ap_dev
->total_request_count
++;
1584 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1585 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1586 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-EINVAL
));
1588 default: /* Device is gone. */
1589 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1593 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1594 ap_dev
->requestq_count
++;
1595 ap_dev
->total_request_count
++;
1598 ap_schedule_poll_timer();
1602 void ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1604 unsigned long flags
;
1607 /* For asynchronous message handling a valid receive-callback
1609 BUG_ON(!ap_msg
->receive
);
1611 spin_lock_bh(&ap_dev
->lock
);
1612 if (!ap_dev
->unregistered
) {
1613 /* Make room on the queue by polling for finished requests. */
1614 rc
= ap_poll_queue(ap_dev
, &flags
);
1616 rc
= __ap_queue_message(ap_dev
, ap_msg
);
1618 wake_up(&ap_poll_wait
);
1620 ap_dev
->unregistered
= 1;
1622 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1625 spin_unlock_bh(&ap_dev
->lock
);
1627 device_unregister(&ap_dev
->device
);
1629 EXPORT_SYMBOL(ap_queue_message
);
1632 * ap_cancel_message(): Cancel a crypto request.
1633 * @ap_dev: The AP device that has the message queued
1634 * @ap_msg: The message that is to be removed
1636 * Cancel a crypto request. This is done by removing the request
1637 * from the device pending or request queue. Note that the
1638 * request stays on the AP queue. When it finishes the message
1639 * reply will be discarded because the psmid can't be found.
1641 void ap_cancel_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1643 struct ap_message
*tmp
;
1645 spin_lock_bh(&ap_dev
->lock
);
1646 if (!list_empty(&ap_msg
->list
)) {
1647 list_for_each_entry(tmp
, &ap_dev
->pendingq
, list
)
1648 if (tmp
->psmid
== ap_msg
->psmid
) {
1649 ap_dev
->pendingq_count
--;
1652 ap_dev
->requestq_count
--;
1654 list_del_init(&ap_msg
->list
);
1656 spin_unlock_bh(&ap_dev
->lock
);
1658 EXPORT_SYMBOL(ap_cancel_message
);
1661 * ap_poll_timeout(): AP receive polling for finished AP requests.
1662 * @unused: Unused pointer.
1664 * Schedules the AP tasklet using a high resolution timer.
1666 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
1668 tasklet_schedule(&ap_tasklet
);
1669 return HRTIMER_NORESTART
;
1673 * ap_reset(): Reset a not responding AP device.
1674 * @ap_dev: Pointer to the AP device
1676 * Reset a not responding AP device and move all requests from the
1677 * pending queue to the request queue.
1679 static void ap_reset(struct ap_device
*ap_dev
)
1683 ap_dev
->reset
= AP_RESET_IGNORE
;
1684 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1685 ap_dev
->queue_count
= 0;
1686 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1687 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1688 ap_dev
->pendingq_count
= 0;
1689 rc
= ap_init_queue(ap_dev
->qid
);
1691 ap_dev
->unregistered
= 1;
1693 __ap_schedule_poll_timer();
1696 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
)
1698 if (!ap_dev
->unregistered
) {
1699 if (ap_poll_queue(ap_dev
, flags
))
1700 ap_dev
->unregistered
= 1;
1701 if (ap_dev
->reset
== AP_RESET_DO
)
1708 * ap_poll_all(): Poll all AP devices.
1709 * @dummy: Unused variable
1711 * Poll all AP devices on the bus in a round robin fashion. Continue
1712 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1713 * of the control flags has been set arm the poll timer.
1715 static void ap_poll_all(unsigned long dummy
)
1717 unsigned long flags
;
1718 struct ap_device
*ap_dev
;
1720 /* Reset the indicator if interrupts are used. Thus new interrupts can
1721 * be received. Doing it in the beginning of the tasklet is therefor
1722 * important that no requests on any AP get lost.
1724 if (ap_using_interrupts())
1725 xchg((u8
*)ap_interrupt_indicator
, 0);
1728 spin_lock(&ap_device_list_lock
);
1729 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1730 spin_lock(&ap_dev
->lock
);
1731 __ap_poll_device(ap_dev
, &flags
);
1732 spin_unlock(&ap_dev
->lock
);
1734 spin_unlock(&ap_device_list_lock
);
1735 } while (flags
& 1);
1737 ap_schedule_poll_timer();
1741 * ap_poll_thread(): Thread that polls for finished requests.
1742 * @data: Unused pointer
1744 * AP bus poll thread. The purpose of this thread is to poll for
1745 * finished requests in a loop if there is a "free" cpu - that is
1746 * a cpu that doesn't have anything better to do. The polling stops
1747 * as soon as there is another task or if all messages have been
1750 static int ap_poll_thread(void *data
)
1752 DECLARE_WAITQUEUE(wait
, current
);
1753 unsigned long flags
;
1755 struct ap_device
*ap_dev
;
1757 set_user_nice(current
, 19);
1759 if (ap_suspend_flag
)
1761 if (need_resched()) {
1765 add_wait_queue(&ap_poll_wait
, &wait
);
1766 set_current_state(TASK_INTERRUPTIBLE
);
1767 if (kthread_should_stop())
1769 requests
= atomic_read(&ap_poll_requests
);
1772 set_current_state(TASK_RUNNING
);
1773 remove_wait_queue(&ap_poll_wait
, &wait
);
1776 spin_lock_bh(&ap_device_list_lock
);
1777 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1778 spin_lock(&ap_dev
->lock
);
1779 __ap_poll_device(ap_dev
, &flags
);
1780 spin_unlock(&ap_dev
->lock
);
1782 spin_unlock_bh(&ap_device_list_lock
);
1784 set_current_state(TASK_RUNNING
);
1785 remove_wait_queue(&ap_poll_wait
, &wait
);
1789 static int ap_poll_thread_start(void)
1793 if (ap_using_interrupts() || ap_suspend_flag
)
1795 mutex_lock(&ap_poll_thread_mutex
);
1796 if (!ap_poll_kthread
) {
1797 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
1798 rc
= IS_ERR(ap_poll_kthread
) ? PTR_ERR(ap_poll_kthread
) : 0;
1800 ap_poll_kthread
= NULL
;
1804 mutex_unlock(&ap_poll_thread_mutex
);
1808 static void ap_poll_thread_stop(void)
1810 mutex_lock(&ap_poll_thread_mutex
);
1811 if (ap_poll_kthread
) {
1812 kthread_stop(ap_poll_kthread
);
1813 ap_poll_kthread
= NULL
;
1815 mutex_unlock(&ap_poll_thread_mutex
);
1819 * ap_request_timeout(): Handling of request timeouts
1820 * @data: Holds the AP device.
1822 * Handles request timeouts.
1824 static void ap_request_timeout(unsigned long data
)
1826 struct ap_device
*ap_dev
= (struct ap_device
*) data
;
1828 if (ap_dev
->reset
== AP_RESET_ARMED
) {
1829 ap_dev
->reset
= AP_RESET_DO
;
1831 if (ap_using_interrupts())
1832 tasklet_schedule(&ap_tasklet
);
1836 static void ap_reset_domain(void)
1840 if (ap_domain_index
!= -1)
1841 for (i
= 0; i
< AP_DEVICES
; i
++)
1842 ap_reset_queue(AP_MKQID(i
, ap_domain_index
));
1845 static void ap_reset_all(void)
1849 for (i
= 0; i
< AP_DOMAINS
; i
++)
1850 for (j
= 0; j
< AP_DEVICES
; j
++)
1851 ap_reset_queue(AP_MKQID(j
, i
));
1854 static struct reset_call ap_reset_call
= {
1859 * ap_module_init(): The module initialization code.
1861 * Initializes the module.
1863 int __init
ap_module_init(void)
1867 if (ap_domain_index
< -1 || ap_domain_index
>= AP_DOMAINS
) {
1868 pr_warning("%d is not a valid cryptographic domain\n",
1872 /* In resume callback we need to know if the user had set the domain.
1873 * If so, we can not just reset it.
1875 if (ap_domain_index
>= 0)
1876 user_set_domain
= 1;
1878 if (ap_instructions_available() != 0) {
1879 pr_warning("The hardware system does not support "
1880 "AP instructions\n");
1883 if (ap_interrupts_available()) {
1884 isc_register(AP_ISC
);
1885 ap_interrupt_indicator
= s390_register_adapter_interrupt(
1886 &ap_interrupt_handler
, NULL
, AP_ISC
);
1887 if (IS_ERR(ap_interrupt_indicator
)) {
1888 ap_interrupt_indicator
= NULL
;
1889 isc_unregister(AP_ISC
);
1893 register_reset_call(&ap_reset_call
);
1895 /* Create /sys/bus/ap. */
1896 rc
= bus_register(&ap_bus_type
);
1899 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1900 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1905 /* Create /sys/devices/ap. */
1906 ap_root_device
= root_device_register("ap");
1907 rc
= IS_ERR(ap_root_device
) ? PTR_ERR(ap_root_device
) : 0;
1911 ap_work_queue
= create_singlethread_workqueue("kapwork");
1912 if (!ap_work_queue
) {
1917 ap_query_configuration();
1918 if (ap_select_domain() == 0)
1921 /* Setup the AP bus rescan timer. */
1922 init_timer(&ap_config_timer
);
1923 ap_config_timer
.function
= ap_config_timeout
;
1924 ap_config_timer
.data
= 0;
1925 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1926 add_timer(&ap_config_timer
);
1928 /* Setup the high resultion poll timer.
1929 * If we are running under z/VM adjust polling to z/VM polling rate.
1932 poll_timeout
= 1500000;
1933 spin_lock_init(&ap_poll_timer_lock
);
1934 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1935 ap_poll_timer
.function
= ap_poll_timeout
;
1937 /* Start the low priority AP bus poll thread. */
1938 if (ap_thread_flag
) {
1939 rc
= ap_poll_thread_start();
1947 del_timer_sync(&ap_config_timer
);
1948 hrtimer_cancel(&ap_poll_timer
);
1949 destroy_workqueue(ap_work_queue
);
1951 root_device_unregister(ap_root_device
);
1954 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1955 bus_unregister(&ap_bus_type
);
1957 unregister_reset_call(&ap_reset_call
);
1958 if (ap_using_interrupts()) {
1959 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1960 isc_unregister(AP_ISC
);
1965 static int __ap_match_all(struct device
*dev
, void *data
)
1971 * ap_modules_exit(): The module termination code
1973 * Terminates the module.
1975 void ap_module_exit(void)
1981 ap_poll_thread_stop();
1982 del_timer_sync(&ap_config_timer
);
1983 hrtimer_cancel(&ap_poll_timer
);
1984 destroy_workqueue(ap_work_queue
);
1985 tasklet_kill(&ap_tasklet
);
1986 root_device_unregister(ap_root_device
);
1987 while ((dev
= bus_find_device(&ap_bus_type
, NULL
, NULL
,
1990 device_unregister(dev
);
1993 for (i
= 0; ap_bus_attrs
[i
]; i
++)
1994 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1995 bus_unregister(&ap_bus_type
);
1996 unregister_reset_call(&ap_reset_call
);
1997 if (ap_using_interrupts()) {
1998 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1999 isc_unregister(AP_ISC
);
2003 module_init(ap_module_init
);
2004 module_exit(ap_module_exit
);