2 * Copyright IBM Corp. 2006
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);
69 MODULE_AUTHOR("IBM Corporation");
70 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
71 "Copyright IBM Corp. 2006");
72 MODULE_LICENSE("GPL");
77 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
78 module_param_named(domain
, ap_domain_index
, int, 0000);
79 MODULE_PARM_DESC(domain
, "domain index for ap devices");
80 EXPORT_SYMBOL(ap_domain_index
);
82 static int ap_thread_flag
= 0;
83 module_param_named(poll_thread
, ap_thread_flag
, int, 0000);
84 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
86 static struct device
*ap_root_device
= NULL
;
87 static DEFINE_SPINLOCK(ap_device_list_lock
);
88 static LIST_HEAD(ap_device_list
);
91 * Workqueue & timer for bus rescan.
93 static struct workqueue_struct
*ap_work_queue
;
94 static struct timer_list ap_config_timer
;
95 static int ap_config_time
= AP_CONFIG_TIME
;
96 static DECLARE_WORK(ap_config_work
, ap_scan_bus
);
99 * Tasklet & timer for AP request polling and interrupts
101 static DECLARE_TASKLET(ap_tasklet
, ap_poll_all
, 0);
102 static atomic_t ap_poll_requests
= ATOMIC_INIT(0);
103 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
104 static struct task_struct
*ap_poll_kthread
= NULL
;
105 static DEFINE_MUTEX(ap_poll_thread_mutex
);
106 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
107 static void *ap_interrupt_indicator
;
108 static struct hrtimer ap_poll_timer
;
109 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
110 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
111 static unsigned long long poll_timeout
= 250000;
114 static int ap_suspend_flag
;
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
;
122 * ap_using_interrupts() - Returns non-zero if interrupt support is
125 static inline int ap_using_interrupts(void)
127 return ap_interrupt_indicator
!= NULL
;
131 * ap_intructions_available() - Test if AP instructions are available.
133 * Returns 0 if the AP instructions are installed.
135 static inline int ap_instructions_available(void)
137 register unsigned long reg0
asm ("0") = AP_MKQID(0,0);
138 register unsigned long reg1
asm ("1") = -ENODEV
;
139 register unsigned long reg2
asm ("2") = 0UL;
142 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
146 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
) : : "cc" );
151 * ap_interrupts_available(): Test if AP interrupts are available.
153 * Returns 1 if AP interrupts are available.
155 static int ap_interrupts_available(void)
157 return test_facility(2) && test_facility(65);
161 * ap_test_queue(): Test adjunct processor queue.
162 * @qid: The AP queue number
163 * @queue_depth: Pointer to queue depth value
164 * @device_type: Pointer to device type value
166 * Returns AP queue status structure.
168 static inline struct ap_queue_status
169 ap_test_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
171 register unsigned long reg0
asm ("0") = qid
;
172 register struct ap_queue_status reg1
asm ("1");
173 register unsigned long reg2
asm ("2") = 0UL;
175 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
176 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
177 *device_type
= (int) (reg2
>> 24);
178 *queue_depth
= (int) (reg2
& 0xff);
183 * ap_reset_queue(): Reset adjunct processor queue.
184 * @qid: The AP queue number
186 * Returns AP queue status structure.
188 static inline struct ap_queue_status
ap_reset_queue(ap_qid_t qid
)
190 register unsigned long reg0
asm ("0") = qid
| 0x01000000UL
;
191 register struct ap_queue_status reg1
asm ("1");
192 register unsigned long reg2
asm ("2") = 0UL;
195 ".long 0xb2af0000" /* PQAP(RAPQ) */
196 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
202 * ap_queue_interruption_control(): Enable interruption for a specific AP.
203 * @qid: The AP queue number
204 * @ind: The notification indicator byte
206 * Returns AP queue status.
208 static inline struct ap_queue_status
209 ap_queue_interruption_control(ap_qid_t qid
, void *ind
)
211 register unsigned long reg0
asm ("0") = qid
| 0x03000000UL
;
212 register unsigned long reg1_in
asm ("1") = 0x0000800000000000UL
| AP_ISC
;
213 register struct ap_queue_status reg1_out
asm ("1");
214 register void *reg2
asm ("2") = ind
;
216 ".long 0xb2af0000" /* PQAP(AQIC) */
217 : "+d" (reg0
), "+d" (reg1_in
), "=d" (reg1_out
), "+d" (reg2
)
225 static inline struct ap_queue_status
226 __ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
228 register unsigned long reg0
asm ("0") = 0UL | qid
| (1UL << 23);
229 register struct ap_queue_status reg1
asm ("1") = AP_QUEUE_STATUS_INVALID
;
230 register unsigned long reg2
asm ("2");
233 ".long 0xb2af0000\n" /* PQAP(TAPQ) */
236 : "+d" (reg0
), "+d" (reg1
), "=d" (reg2
)
240 *functions
= (unsigned int)(reg2
>> 32);
246 * ap_query_functions(): Query supported functions.
247 * @qid: The AP queue number
248 * @functions: Pointer to functions field.
252 * -ENODEV if queue not valid.
253 * -EBUSY if device busy.
254 * -EINVAL if query function is not supported
256 static int ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
259 struct ap_queue_status status
;
261 status
= __ap_query_functions(qid
, functions
);
263 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
264 if (ap_queue_status_invalid_test(&status
))
267 switch (status
.response_code
) {
268 case AP_RESPONSE_NORMAL
:
270 case AP_RESPONSE_RESET_IN_PROGRESS
:
271 case AP_RESPONSE_BUSY
:
273 case AP_RESPONSE_Q_NOT_AVAIL
:
274 case AP_RESPONSE_DECONFIGURED
:
275 case AP_RESPONSE_CHECKSTOPPED
:
276 case AP_RESPONSE_INVALID_ADDRESS
:
278 case AP_RESPONSE_OTHERWISE_CHANGED
:
283 if (i
< AP_MAX_RESET
- 1) {
285 status
= __ap_query_functions(qid
, functions
);
295 * ap_4096_commands_availablen(): Check for availability of 4096 bit RSA
297 * @qid: The AP queue number
299 * Returns 1 if 4096 bit RSA keys are support fo the AP, returns 0 if not.
301 int ap_4096_commands_available(ap_qid_t qid
)
303 unsigned int functions
;
305 if (ap_query_functions(qid
, &functions
))
308 return test_ap_facility(functions
, 1) &&
309 test_ap_facility(functions
, 2);
311 EXPORT_SYMBOL(ap_4096_commands_available
);
314 * ap_queue_enable_interruption(): Enable interruption on an AP.
315 * @qid: The AP queue number
316 * @ind: the notification indicator byte
318 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
319 * on the return value it waits a while and tests the AP queue if interrupts
320 * have been switched on using ap_test_queue().
322 static int ap_queue_enable_interruption(ap_qid_t qid
, void *ind
)
325 struct ap_queue_status status
;
326 int t_depth
, t_device_type
, rc
, i
;
329 status
= ap_queue_interruption_control(qid
, ind
);
331 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
332 switch (status
.response_code
) {
333 case AP_RESPONSE_NORMAL
:
334 if (status
.int_enabled
)
337 case AP_RESPONSE_RESET_IN_PROGRESS
:
338 case AP_RESPONSE_BUSY
:
339 if (i
< AP_MAX_RESET
- 1) {
341 status
= ap_queue_interruption_control(qid
,
346 case AP_RESPONSE_Q_NOT_AVAIL
:
347 case AP_RESPONSE_DECONFIGURED
:
348 case AP_RESPONSE_CHECKSTOPPED
:
349 case AP_RESPONSE_INVALID_ADDRESS
:
351 case AP_RESPONSE_OTHERWISE_CHANGED
:
352 if (status
.int_enabled
)
358 if (i
< AP_MAX_RESET
- 1) {
360 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
370 * __ap_send(): Send message to adjunct processor queue.
371 * @qid: The AP queue number
372 * @psmid: The program supplied message identifier
373 * @msg: The message text
374 * @length: The message length
375 * @special: Special Bit
377 * Returns AP queue status structure.
378 * Condition code 1 on NQAP can't happen because the L bit is 1.
379 * Condition code 2 on NQAP also means the send is incomplete,
380 * because a segment boundary was reached. The NQAP is repeated.
382 static inline struct ap_queue_status
383 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
,
384 unsigned int special
)
386 typedef struct { char _
[length
]; } msgblock
;
387 register unsigned long reg0
asm ("0") = qid
| 0x40000000UL
;
388 register struct ap_queue_status reg1
asm ("1");
389 register unsigned long reg2
asm ("2") = (unsigned long) msg
;
390 register unsigned long reg3
asm ("3") = (unsigned long) length
;
391 register unsigned long reg4
asm ("4") = (unsigned int) (psmid
>> 32);
392 register unsigned long reg5
asm ("5") = (unsigned int) psmid
;
398 "0: .long 0xb2ad0042\n" /* NQAP */
400 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
), "+d" (reg3
)
401 : "d" (reg4
), "d" (reg5
), "m" (*(msgblock
*) msg
)
406 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
408 struct ap_queue_status status
;
410 status
= __ap_send(qid
, psmid
, msg
, length
, 0);
411 switch (status
.response_code
) {
412 case AP_RESPONSE_NORMAL
:
414 case AP_RESPONSE_Q_FULL
:
415 case AP_RESPONSE_RESET_IN_PROGRESS
:
417 case AP_RESPONSE_REQ_FAC_NOT_INST
:
419 default: /* Device is gone. */
423 EXPORT_SYMBOL(ap_send
);
426 * __ap_recv(): Receive message from adjunct processor queue.
427 * @qid: The AP queue number
428 * @psmid: Pointer to program supplied message identifier
429 * @msg: The message text
430 * @length: The message length
432 * Returns AP queue status structure.
433 * Condition code 1 on DQAP means the receive has taken place
434 * but only partially. The response is incomplete, hence the
436 * Condition code 2 on DQAP also means the receive is incomplete,
437 * this time because a segment boundary was reached. Again, the
439 * Note that gpr2 is used by the DQAP instruction to keep track of
440 * any 'residual' length, in case the instruction gets interrupted.
441 * Hence it gets zeroed before the instruction.
443 static inline struct ap_queue_status
444 __ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
446 typedef struct { char _
[length
]; } msgblock
;
447 register unsigned long reg0
asm("0") = qid
| 0x80000000UL
;
448 register struct ap_queue_status reg1
asm ("1");
449 register unsigned long reg2
asm("2") = 0UL;
450 register unsigned long reg4
asm("4") = (unsigned long) msg
;
451 register unsigned long reg5
asm("5") = (unsigned long) length
;
452 register unsigned long reg6
asm("6") = 0UL;
453 register unsigned long reg7
asm("7") = 0UL;
457 "0: .long 0xb2ae0064\n" /* DQAP */
459 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
),
460 "+d" (reg4
), "+d" (reg5
), "+d" (reg6
), "+d" (reg7
),
461 "=m" (*(msgblock
*) msg
) : : "cc" );
462 *psmid
= (((unsigned long long) reg6
) << 32) + reg7
;
466 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
468 struct ap_queue_status status
;
470 status
= __ap_recv(qid
, psmid
, msg
, length
);
471 switch (status
.response_code
) {
472 case AP_RESPONSE_NORMAL
:
474 case AP_RESPONSE_NO_PENDING_REPLY
:
475 if (status
.queue_empty
)
478 case AP_RESPONSE_RESET_IN_PROGRESS
:
484 EXPORT_SYMBOL(ap_recv
);
487 * ap_query_queue(): Check if an AP queue is available.
488 * @qid: The AP queue number
489 * @queue_depth: Pointer to queue depth value
490 * @device_type: Pointer to device type value
492 * The test is repeated for AP_MAX_RESET times.
494 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
496 struct ap_queue_status status
;
497 int t_depth
, t_device_type
, rc
, i
;
500 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
501 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
502 switch (status
.response_code
) {
503 case AP_RESPONSE_NORMAL
:
504 *queue_depth
= t_depth
+ 1;
505 *device_type
= t_device_type
;
508 case AP_RESPONSE_Q_NOT_AVAIL
:
511 case AP_RESPONSE_RESET_IN_PROGRESS
:
513 case AP_RESPONSE_DECONFIGURED
:
516 case AP_RESPONSE_CHECKSTOPPED
:
519 case AP_RESPONSE_INVALID_ADDRESS
:
522 case AP_RESPONSE_OTHERWISE_CHANGED
:
524 case AP_RESPONSE_BUSY
:
531 if (i
< AP_MAX_RESET
- 1)
538 * ap_init_queue(): Reset an AP queue.
539 * @qid: The AP queue number
541 * Reset an AP queue and wait for it to become available again.
543 static int ap_init_queue(ap_qid_t qid
)
545 struct ap_queue_status status
;
549 status
= ap_reset_queue(qid
);
550 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
551 switch (status
.response_code
) {
552 case AP_RESPONSE_NORMAL
:
553 if (status
.queue_empty
)
556 case AP_RESPONSE_Q_NOT_AVAIL
:
557 case AP_RESPONSE_DECONFIGURED
:
558 case AP_RESPONSE_CHECKSTOPPED
:
559 i
= AP_MAX_RESET
; /* return with -ENODEV */
561 case AP_RESPONSE_RESET_IN_PROGRESS
:
563 case AP_RESPONSE_BUSY
:
567 if (rc
!= -ENODEV
&& rc
!= -EBUSY
)
569 if (i
< AP_MAX_RESET
- 1) {
571 status
= ap_test_queue(qid
, &dummy
, &dummy
);
574 if (rc
== 0 && ap_using_interrupts()) {
575 rc
= ap_queue_enable_interruption(qid
, ap_interrupt_indicator
);
576 /* If interruption mode is supported by the machine,
577 * but an AP can not be enabled for interruption then
578 * the AP will be discarded. */
580 pr_err("Registering adapter interrupts for "
581 "AP %d failed\n", AP_QID_DEVICE(qid
));
587 * ap_increase_queue_count(): Arm request timeout.
588 * @ap_dev: Pointer to an AP device.
590 * Arm request timeout if an AP device was idle and a new request is submitted.
592 static void ap_increase_queue_count(struct ap_device
*ap_dev
)
594 int timeout
= ap_dev
->drv
->request_timeout
;
596 ap_dev
->queue_count
++;
597 if (ap_dev
->queue_count
== 1) {
598 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
599 ap_dev
->reset
= AP_RESET_ARMED
;
604 * ap_decrease_queue_count(): Decrease queue count.
605 * @ap_dev: Pointer to an AP device.
607 * If AP device is still alive, re-schedule request timeout if there are still
610 static void ap_decrease_queue_count(struct ap_device
*ap_dev
)
612 int timeout
= ap_dev
->drv
->request_timeout
;
614 ap_dev
->queue_count
--;
615 if (ap_dev
->queue_count
> 0)
616 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
619 * The timeout timer should to be disabled now - since
620 * del_timer_sync() is very expensive, we just tell via the
621 * reset flag to ignore the pending timeout timer.
623 ap_dev
->reset
= AP_RESET_IGNORE
;
627 * AP device related attributes.
629 static ssize_t
ap_hwtype_show(struct device
*dev
,
630 struct device_attribute
*attr
, char *buf
)
632 struct ap_device
*ap_dev
= to_ap_dev(dev
);
633 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->device_type
);
636 static DEVICE_ATTR(hwtype
, 0444, ap_hwtype_show
, NULL
);
637 static ssize_t
ap_depth_show(struct device
*dev
, struct device_attribute
*attr
,
640 struct ap_device
*ap_dev
= to_ap_dev(dev
);
641 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->queue_depth
);
644 static DEVICE_ATTR(depth
, 0444, ap_depth_show
, NULL
);
645 static ssize_t
ap_request_count_show(struct device
*dev
,
646 struct device_attribute
*attr
,
649 struct ap_device
*ap_dev
= to_ap_dev(dev
);
652 spin_lock_bh(&ap_dev
->lock
);
653 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->total_request_count
);
654 spin_unlock_bh(&ap_dev
->lock
);
658 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
660 static ssize_t
ap_modalias_show(struct device
*dev
,
661 struct device_attribute
*attr
, char *buf
)
663 return sprintf(buf
, "ap:t%02X", to_ap_dev(dev
)->device_type
);
666 static DEVICE_ATTR(modalias
, 0444, ap_modalias_show
, NULL
);
668 static struct attribute
*ap_dev_attrs
[] = {
669 &dev_attr_hwtype
.attr
,
670 &dev_attr_depth
.attr
,
671 &dev_attr_request_count
.attr
,
672 &dev_attr_modalias
.attr
,
675 static struct attribute_group ap_dev_attr_group
= {
676 .attrs
= ap_dev_attrs
681 * @dev: Pointer to device
682 * @drv: Pointer to device_driver
684 * AP bus driver registration/unregistration.
686 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
688 struct ap_device
*ap_dev
= to_ap_dev(dev
);
689 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
690 struct ap_device_id
*id
;
693 * Compare device type of the device with the list of
694 * supported types of the device_driver.
696 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
697 if ((id
->match_flags
& AP_DEVICE_ID_MATCH_DEVICE_TYPE
) &&
698 (id
->dev_type
!= ap_dev
->device_type
))
706 * ap_uevent(): Uevent function for AP devices.
707 * @dev: Pointer to device
708 * @env: Pointer to kobj_uevent_env
710 * It sets up a single environment variable DEV_TYPE which contains the
711 * hardware device type.
713 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
715 struct ap_device
*ap_dev
= to_ap_dev(dev
);
721 /* Set up DEV_TYPE environment variable. */
722 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
727 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
732 static int ap_bus_suspend(struct device
*dev
, pm_message_t state
)
734 struct ap_device
*ap_dev
= to_ap_dev(dev
);
737 if (!ap_suspend_flag
) {
740 /* Disable scanning for devices, thus we do not want to scan
741 * for them after removing.
743 del_timer_sync(&ap_config_timer
);
744 if (ap_work_queue
!= NULL
) {
745 destroy_workqueue(ap_work_queue
);
746 ap_work_queue
= NULL
;
749 tasklet_disable(&ap_tasklet
);
751 /* Poll on the device until all requests are finished. */
754 spin_lock_bh(&ap_dev
->lock
);
755 __ap_poll_device(ap_dev
, &flags
);
756 spin_unlock_bh(&ap_dev
->lock
);
757 } while ((flags
& 1) || (flags
& 2));
759 spin_lock_bh(&ap_dev
->lock
);
760 ap_dev
->unregistered
= 1;
761 spin_unlock_bh(&ap_dev
->lock
);
766 static int ap_bus_resume(struct device
*dev
)
769 struct ap_device
*ap_dev
= to_ap_dev(dev
);
771 if (ap_suspend_flag
) {
773 if (!ap_interrupts_available())
774 ap_interrupt_indicator
= NULL
;
775 if (!user_set_domain
) {
776 ap_domain_index
= -1;
779 init_timer(&ap_config_timer
);
780 ap_config_timer
.function
= ap_config_timeout
;
781 ap_config_timer
.data
= 0;
782 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
783 add_timer(&ap_config_timer
);
784 ap_work_queue
= create_singlethread_workqueue("kapwork");
787 tasklet_enable(&ap_tasklet
);
788 if (!ap_using_interrupts())
789 ap_schedule_poll_timer();
791 tasklet_schedule(&ap_tasklet
);
793 rc
= ap_poll_thread_start();
795 if (AP_QID_QUEUE(ap_dev
->qid
) != ap_domain_index
) {
796 spin_lock_bh(&ap_dev
->lock
);
797 ap_dev
->qid
= AP_MKQID(AP_QID_DEVICE(ap_dev
->qid
),
799 spin_unlock_bh(&ap_dev
->lock
);
801 queue_work(ap_work_queue
, &ap_config_work
);
806 static struct bus_type ap_bus_type
= {
808 .match
= &ap_bus_match
,
809 .uevent
= &ap_uevent
,
810 .suspend
= ap_bus_suspend
,
811 .resume
= ap_bus_resume
814 static int ap_device_probe(struct device
*dev
)
816 struct ap_device
*ap_dev
= to_ap_dev(dev
);
817 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
820 ap_dev
->drv
= ap_drv
;
821 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
823 spin_lock_bh(&ap_device_list_lock
);
824 list_add(&ap_dev
->list
, &ap_device_list
);
825 spin_unlock_bh(&ap_device_list_lock
);
831 * __ap_flush_queue(): Flush requests.
832 * @ap_dev: Pointer to the AP device
834 * Flush all requests from the request/pending queue of an AP device.
836 static void __ap_flush_queue(struct ap_device
*ap_dev
)
838 struct ap_message
*ap_msg
, *next
;
840 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->pendingq
, list
) {
841 list_del_init(&ap_msg
->list
);
842 ap_dev
->pendingq_count
--;
843 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
845 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->requestq
, list
) {
846 list_del_init(&ap_msg
->list
);
847 ap_dev
->requestq_count
--;
848 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
852 void ap_flush_queue(struct ap_device
*ap_dev
)
854 spin_lock_bh(&ap_dev
->lock
);
855 __ap_flush_queue(ap_dev
);
856 spin_unlock_bh(&ap_dev
->lock
);
858 EXPORT_SYMBOL(ap_flush_queue
);
860 static int ap_device_remove(struct device
*dev
)
862 struct ap_device
*ap_dev
= to_ap_dev(dev
);
863 struct ap_driver
*ap_drv
= ap_dev
->drv
;
865 ap_flush_queue(ap_dev
);
866 del_timer_sync(&ap_dev
->timeout
);
867 spin_lock_bh(&ap_device_list_lock
);
868 list_del_init(&ap_dev
->list
);
869 spin_unlock_bh(&ap_device_list_lock
);
871 ap_drv
->remove(ap_dev
);
872 spin_lock_bh(&ap_dev
->lock
);
873 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
874 spin_unlock_bh(&ap_dev
->lock
);
878 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
881 struct device_driver
*drv
= &ap_drv
->driver
;
883 drv
->bus
= &ap_bus_type
;
884 drv
->probe
= ap_device_probe
;
885 drv
->remove
= ap_device_remove
;
888 return driver_register(drv
);
890 EXPORT_SYMBOL(ap_driver_register
);
892 void ap_driver_unregister(struct ap_driver
*ap_drv
)
894 driver_unregister(&ap_drv
->driver
);
896 EXPORT_SYMBOL(ap_driver_unregister
);
901 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
903 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
906 static BUS_ATTR(ap_domain
, 0444, ap_domain_show
, NULL
);
908 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
910 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
913 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
915 return snprintf(buf
, PAGE_SIZE
, "%d\n",
916 ap_using_interrupts() ? 1 : 0);
919 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
921 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
922 const char *buf
, size_t count
)
926 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
928 ap_config_time
= time
;
929 if (!timer_pending(&ap_config_timer
) ||
930 !mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
)) {
931 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
932 add_timer(&ap_config_timer
);
937 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
939 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
941 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
944 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
945 const char *buf
, size_t count
)
949 if (sscanf(buf
, "%d\n", &flag
) != 1)
952 rc
= ap_poll_thread_start();
957 ap_poll_thread_stop();
961 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
963 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
965 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
968 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
971 unsigned long long time
;
974 /* 120 seconds = maximum poll interval */
975 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
976 time
> 120000000000ULL)
979 hr_time
= ktime_set(0, poll_timeout
);
981 if (!hrtimer_is_queued(&ap_poll_timer
) ||
982 !hrtimer_forward(&ap_poll_timer
, hrtimer_get_expires(&ap_poll_timer
), hr_time
)) {
983 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
984 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
989 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
991 static struct bus_attribute
*const ap_bus_attrs
[] = {
993 &bus_attr_config_time
,
994 &bus_attr_poll_thread
,
995 &bus_attr_ap_interrupts
,
996 &bus_attr_poll_timeout
,
1001 * ap_select_domain(): Select an AP domain.
1003 * Pick one of the 16 AP domains.
1005 static int ap_select_domain(void)
1007 int queue_depth
, device_type
, count
, max_count
, best_domain
;
1011 * We want to use a single domain. Either the one specified with
1012 * the "domain=" parameter or the domain with the maximum number
1015 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
)
1016 /* Domain has already been selected. */
1020 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1022 for (j
= 0; j
< AP_DEVICES
; j
++) {
1023 ap_qid_t qid
= AP_MKQID(j
, i
);
1024 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1029 if (count
> max_count
) {
1034 if (best_domain
>= 0){
1035 ap_domain_index
= best_domain
;
1042 * ap_probe_device_type(): Find the device type of an AP.
1043 * @ap_dev: pointer to the AP device.
1045 * Find the device type if query queue returned a device type of 0.
1047 static int ap_probe_device_type(struct ap_device
*ap_dev
)
1049 static unsigned char msg
[] = {
1050 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1051 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1052 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1053 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1054 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1055 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1056 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1057 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1058 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1059 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1060 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1061 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1062 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1063 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1064 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1065 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1066 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1067 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1068 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1069 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1070 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1071 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1072 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1073 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1074 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1075 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1076 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1077 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1078 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1079 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1080 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1081 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1082 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1083 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1084 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1085 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1086 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1087 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1088 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1089 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1090 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1091 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1092 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1093 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1094 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1096 struct ap_queue_status status
;
1097 unsigned long long psmid
;
1101 reply
= (void *) get_zeroed_page(GFP_KERNEL
);
1107 status
= __ap_send(ap_dev
->qid
, 0x0102030405060708ULL
,
1108 msg
, sizeof(msg
), 0);
1109 if (status
.response_code
!= AP_RESPONSE_NORMAL
) {
1114 /* Wait for the test message to complete. */
1115 for (i
= 0; i
< 6; i
++) {
1117 status
= __ap_recv(ap_dev
->qid
, &psmid
, reply
, 4096);
1118 if (status
.response_code
== AP_RESPONSE_NORMAL
&&
1119 psmid
== 0x0102030405060708ULL
)
1123 /* Got an answer. */
1124 if (reply
[0] == 0x00 && reply
[1] == 0x86)
1125 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICC
;
1127 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICA
;
1133 free_page((unsigned long) reply
);
1138 static void ap_interrupt_handler(void *unused1
, void *unused2
)
1140 kstat_cpu(smp_processor_id()).irqs
[IOINT_APB
]++;
1141 tasklet_schedule(&ap_tasklet
);
1145 * __ap_scan_bus(): Scan the AP bus.
1146 * @dev: Pointer to device
1147 * @data: Pointer to data
1149 * Scan the AP bus for new devices.
1151 static int __ap_scan_bus(struct device
*dev
, void *data
)
1153 return to_ap_dev(dev
)->qid
== (ap_qid_t
)(unsigned long) data
;
1156 static void ap_device_release(struct device
*dev
)
1158 struct ap_device
*ap_dev
= to_ap_dev(dev
);
1163 static void ap_scan_bus(struct work_struct
*unused
)
1165 struct ap_device
*ap_dev
;
1168 int queue_depth
, device_type
;
1169 unsigned int device_functions
;
1172 if (ap_select_domain() != 0)
1174 for (i
= 0; i
< AP_DEVICES
; i
++) {
1175 qid
= AP_MKQID(i
, ap_domain_index
);
1176 dev
= bus_find_device(&ap_bus_type
, NULL
,
1177 (void *)(unsigned long)qid
,
1179 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1182 set_current_state(TASK_UNINTERRUPTIBLE
);
1183 schedule_timeout(AP_RESET_TIMEOUT
);
1184 rc
= ap_query_queue(qid
, &queue_depth
,
1187 ap_dev
= to_ap_dev(dev
);
1188 spin_lock_bh(&ap_dev
->lock
);
1189 if (rc
|| ap_dev
->unregistered
) {
1190 spin_unlock_bh(&ap_dev
->lock
);
1191 if (ap_dev
->unregistered
)
1193 device_unregister(dev
);
1197 spin_unlock_bh(&ap_dev
->lock
);
1203 rc
= ap_init_queue(qid
);
1206 ap_dev
= kzalloc(sizeof(*ap_dev
), GFP_KERNEL
);
1210 ap_dev
->queue_depth
= queue_depth
;
1211 ap_dev
->unregistered
= 1;
1212 spin_lock_init(&ap_dev
->lock
);
1213 INIT_LIST_HEAD(&ap_dev
->pendingq
);
1214 INIT_LIST_HEAD(&ap_dev
->requestq
);
1215 INIT_LIST_HEAD(&ap_dev
->list
);
1216 setup_timer(&ap_dev
->timeout
, ap_request_timeout
,
1217 (unsigned long) ap_dev
);
1218 switch (device_type
) {
1220 if (ap_probe_device_type(ap_dev
)) {
1226 if (ap_query_functions(qid
, &device_functions
)) {
1230 if (test_ap_facility(device_functions
, 3))
1231 ap_dev
->device_type
= AP_DEVICE_TYPE_CEX3C
;
1232 else if (test_ap_facility(device_functions
, 4))
1233 ap_dev
->device_type
= AP_DEVICE_TYPE_CEX3A
;
1240 ap_dev
->device_type
= device_type
;
1243 ap_dev
->device
.bus
= &ap_bus_type
;
1244 ap_dev
->device
.parent
= ap_root_device
;
1245 if (dev_set_name(&ap_dev
->device
, "card%02x",
1246 AP_QID_DEVICE(ap_dev
->qid
))) {
1250 ap_dev
->device
.release
= ap_device_release
;
1251 rc
= device_register(&ap_dev
->device
);
1253 put_device(&ap_dev
->device
);
1256 /* Add device attributes. */
1257 rc
= sysfs_create_group(&ap_dev
->device
.kobj
,
1258 &ap_dev_attr_group
);
1260 spin_lock_bh(&ap_dev
->lock
);
1261 ap_dev
->unregistered
= 0;
1262 spin_unlock_bh(&ap_dev
->lock
);
1265 device_unregister(&ap_dev
->device
);
1270 ap_config_timeout(unsigned long ptr
)
1272 queue_work(ap_work_queue
, &ap_config_work
);
1273 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1274 add_timer(&ap_config_timer
);
1278 * __ap_schedule_poll_timer(): Schedule poll timer.
1280 * Set up the timer to run the poll tasklet
1282 static inline void __ap_schedule_poll_timer(void)
1286 spin_lock_bh(&ap_poll_timer_lock
);
1287 if (hrtimer_is_queued(&ap_poll_timer
) || ap_suspend_flag
)
1289 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer
)) <= 0) {
1290 hr_time
= ktime_set(0, poll_timeout
);
1291 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
1292 hrtimer_restart(&ap_poll_timer
);
1295 spin_unlock_bh(&ap_poll_timer_lock
);
1299 * ap_schedule_poll_timer(): Schedule poll timer.
1301 * Set up the timer to run the poll tasklet
1303 static inline void ap_schedule_poll_timer(void)
1305 if (ap_using_interrupts())
1307 __ap_schedule_poll_timer();
1311 * ap_poll_read(): Receive pending reply messages from an AP device.
1312 * @ap_dev: pointer to the AP device
1313 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1314 * required, bit 2^1 is set if the poll timer needs to get armed
1316 * Returns 0 if the device is still present, -ENODEV if not.
1318 static int ap_poll_read(struct ap_device
*ap_dev
, unsigned long *flags
)
1320 struct ap_queue_status status
;
1321 struct ap_message
*ap_msg
;
1323 if (ap_dev
->queue_count
<= 0)
1325 status
= __ap_recv(ap_dev
->qid
, &ap_dev
->reply
->psmid
,
1326 ap_dev
->reply
->message
, ap_dev
->reply
->length
);
1327 switch (status
.response_code
) {
1328 case AP_RESPONSE_NORMAL
:
1329 atomic_dec(&ap_poll_requests
);
1330 ap_decrease_queue_count(ap_dev
);
1331 list_for_each_entry(ap_msg
, &ap_dev
->pendingq
, list
) {
1332 if (ap_msg
->psmid
!= ap_dev
->reply
->psmid
)
1334 list_del_init(&ap_msg
->list
);
1335 ap_dev
->pendingq_count
--;
1336 ap_msg
->receive(ap_dev
, ap_msg
, ap_dev
->reply
);
1339 if (ap_dev
->queue_count
> 0)
1342 case AP_RESPONSE_NO_PENDING_REPLY
:
1343 if (status
.queue_empty
) {
1344 /* The card shouldn't forget requests but who knows. */
1345 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1346 ap_dev
->queue_count
= 0;
1347 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1348 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1349 ap_dev
->pendingq_count
= 0;
1360 * ap_poll_write(): Send messages from the request queue to an AP device.
1361 * @ap_dev: pointer to the AP device
1362 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1363 * required, bit 2^1 is set if the poll timer needs to get armed
1365 * Returns 0 if the device is still present, -ENODEV if not.
1367 static int ap_poll_write(struct ap_device
*ap_dev
, unsigned long *flags
)
1369 struct ap_queue_status status
;
1370 struct ap_message
*ap_msg
;
1372 if (ap_dev
->requestq_count
<= 0 ||
1373 ap_dev
->queue_count
>= ap_dev
->queue_depth
)
1375 /* Start the next request on the queue. */
1376 ap_msg
= list_entry(ap_dev
->requestq
.next
, struct ap_message
, list
);
1377 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1378 ap_msg
->message
, ap_msg
->length
, ap_msg
->special
);
1379 switch (status
.response_code
) {
1380 case AP_RESPONSE_NORMAL
:
1381 atomic_inc(&ap_poll_requests
);
1382 ap_increase_queue_count(ap_dev
);
1383 list_move_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1384 ap_dev
->requestq_count
--;
1385 ap_dev
->pendingq_count
++;
1386 if (ap_dev
->queue_count
< ap_dev
->queue_depth
&&
1387 ap_dev
->requestq_count
> 0)
1391 case AP_RESPONSE_RESET_IN_PROGRESS
:
1392 __ap_schedule_poll_timer();
1393 case AP_RESPONSE_Q_FULL
:
1396 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1397 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1406 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1407 * @ap_dev: pointer to the bus device
1408 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1409 * required, bit 2^1 is set if the poll timer needs to get armed
1411 * Poll AP device for pending replies and send new messages. If either
1412 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1415 static inline int ap_poll_queue(struct ap_device
*ap_dev
, unsigned long *flags
)
1419 rc
= ap_poll_read(ap_dev
, flags
);
1422 return ap_poll_write(ap_dev
, flags
);
1426 * __ap_queue_message(): Queue a message to a device.
1427 * @ap_dev: pointer to the AP device
1428 * @ap_msg: the message to be queued
1430 * Queue a message to a device. Returns 0 if successful.
1432 static int __ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1434 struct ap_queue_status status
;
1436 if (list_empty(&ap_dev
->requestq
) &&
1437 ap_dev
->queue_count
< ap_dev
->queue_depth
) {
1438 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1439 ap_msg
->message
, ap_msg
->length
,
1441 switch (status
.response_code
) {
1442 case AP_RESPONSE_NORMAL
:
1443 list_add_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1444 atomic_inc(&ap_poll_requests
);
1445 ap_dev
->pendingq_count
++;
1446 ap_increase_queue_count(ap_dev
);
1447 ap_dev
->total_request_count
++;
1449 case AP_RESPONSE_Q_FULL
:
1450 case AP_RESPONSE_RESET_IN_PROGRESS
:
1451 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1452 ap_dev
->requestq_count
++;
1453 ap_dev
->total_request_count
++;
1455 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1456 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1457 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-EINVAL
));
1459 default: /* Device is gone. */
1460 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1464 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1465 ap_dev
->requestq_count
++;
1466 ap_dev
->total_request_count
++;
1469 ap_schedule_poll_timer();
1473 void ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1475 unsigned long flags
;
1478 /* For asynchronous message handling a valid receive-callback
1480 BUG_ON(!ap_msg
->receive
);
1482 spin_lock_bh(&ap_dev
->lock
);
1483 if (!ap_dev
->unregistered
) {
1484 /* Make room on the queue by polling for finished requests. */
1485 rc
= ap_poll_queue(ap_dev
, &flags
);
1487 rc
= __ap_queue_message(ap_dev
, ap_msg
);
1489 wake_up(&ap_poll_wait
);
1491 ap_dev
->unregistered
= 1;
1493 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1496 spin_unlock_bh(&ap_dev
->lock
);
1498 device_unregister(&ap_dev
->device
);
1500 EXPORT_SYMBOL(ap_queue_message
);
1503 * ap_cancel_message(): Cancel a crypto request.
1504 * @ap_dev: The AP device that has the message queued
1505 * @ap_msg: The message that is to be removed
1507 * Cancel a crypto request. This is done by removing the request
1508 * from the device pending or request queue. Note that the
1509 * request stays on the AP queue. When it finishes the message
1510 * reply will be discarded because the psmid can't be found.
1512 void ap_cancel_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1514 struct ap_message
*tmp
;
1516 spin_lock_bh(&ap_dev
->lock
);
1517 if (!list_empty(&ap_msg
->list
)) {
1518 list_for_each_entry(tmp
, &ap_dev
->pendingq
, list
)
1519 if (tmp
->psmid
== ap_msg
->psmid
) {
1520 ap_dev
->pendingq_count
--;
1523 ap_dev
->requestq_count
--;
1525 list_del_init(&ap_msg
->list
);
1527 spin_unlock_bh(&ap_dev
->lock
);
1529 EXPORT_SYMBOL(ap_cancel_message
);
1532 * ap_poll_timeout(): AP receive polling for finished AP requests.
1533 * @unused: Unused pointer.
1535 * Schedules the AP tasklet using a high resolution timer.
1537 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
1539 tasklet_schedule(&ap_tasklet
);
1540 return HRTIMER_NORESTART
;
1544 * ap_reset(): Reset a not responding AP device.
1545 * @ap_dev: Pointer to the AP device
1547 * Reset a not responding AP device and move all requests from the
1548 * pending queue to the request queue.
1550 static void ap_reset(struct ap_device
*ap_dev
)
1554 ap_dev
->reset
= AP_RESET_IGNORE
;
1555 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1556 ap_dev
->queue_count
= 0;
1557 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1558 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1559 ap_dev
->pendingq_count
= 0;
1560 rc
= ap_init_queue(ap_dev
->qid
);
1562 ap_dev
->unregistered
= 1;
1564 __ap_schedule_poll_timer();
1567 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
)
1569 if (!ap_dev
->unregistered
) {
1570 if (ap_poll_queue(ap_dev
, flags
))
1571 ap_dev
->unregistered
= 1;
1572 if (ap_dev
->reset
== AP_RESET_DO
)
1579 * ap_poll_all(): Poll all AP devices.
1580 * @dummy: Unused variable
1582 * Poll all AP devices on the bus in a round robin fashion. Continue
1583 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1584 * of the control flags has been set arm the poll timer.
1586 static void ap_poll_all(unsigned long dummy
)
1588 unsigned long flags
;
1589 struct ap_device
*ap_dev
;
1591 /* Reset the indicator if interrupts are used. Thus new interrupts can
1592 * be received. Doing it in the beginning of the tasklet is therefor
1593 * important that no requests on any AP get lost.
1595 if (ap_using_interrupts())
1596 xchg((u8
*)ap_interrupt_indicator
, 0);
1599 spin_lock(&ap_device_list_lock
);
1600 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1601 spin_lock(&ap_dev
->lock
);
1602 __ap_poll_device(ap_dev
, &flags
);
1603 spin_unlock(&ap_dev
->lock
);
1605 spin_unlock(&ap_device_list_lock
);
1606 } while (flags
& 1);
1608 ap_schedule_poll_timer();
1612 * ap_poll_thread(): Thread that polls for finished requests.
1613 * @data: Unused pointer
1615 * AP bus poll thread. The purpose of this thread is to poll for
1616 * finished requests in a loop if there is a "free" cpu - that is
1617 * a cpu that doesn't have anything better to do. The polling stops
1618 * as soon as there is another task or if all messages have been
1621 static int ap_poll_thread(void *data
)
1623 DECLARE_WAITQUEUE(wait
, current
);
1624 unsigned long flags
;
1626 struct ap_device
*ap_dev
;
1628 set_user_nice(current
, 19);
1630 if (ap_suspend_flag
)
1632 if (need_resched()) {
1636 add_wait_queue(&ap_poll_wait
, &wait
);
1637 set_current_state(TASK_INTERRUPTIBLE
);
1638 if (kthread_should_stop())
1640 requests
= atomic_read(&ap_poll_requests
);
1643 set_current_state(TASK_RUNNING
);
1644 remove_wait_queue(&ap_poll_wait
, &wait
);
1647 spin_lock_bh(&ap_device_list_lock
);
1648 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1649 spin_lock(&ap_dev
->lock
);
1650 __ap_poll_device(ap_dev
, &flags
);
1651 spin_unlock(&ap_dev
->lock
);
1653 spin_unlock_bh(&ap_device_list_lock
);
1655 set_current_state(TASK_RUNNING
);
1656 remove_wait_queue(&ap_poll_wait
, &wait
);
1660 static int ap_poll_thread_start(void)
1664 if (ap_using_interrupts() || ap_suspend_flag
)
1666 mutex_lock(&ap_poll_thread_mutex
);
1667 if (!ap_poll_kthread
) {
1668 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
1669 rc
= IS_ERR(ap_poll_kthread
) ? PTR_ERR(ap_poll_kthread
) : 0;
1671 ap_poll_kthread
= NULL
;
1675 mutex_unlock(&ap_poll_thread_mutex
);
1679 static void ap_poll_thread_stop(void)
1681 mutex_lock(&ap_poll_thread_mutex
);
1682 if (ap_poll_kthread
) {
1683 kthread_stop(ap_poll_kthread
);
1684 ap_poll_kthread
= NULL
;
1686 mutex_unlock(&ap_poll_thread_mutex
);
1690 * ap_request_timeout(): Handling of request timeouts
1691 * @data: Holds the AP device.
1693 * Handles request timeouts.
1695 static void ap_request_timeout(unsigned long data
)
1697 struct ap_device
*ap_dev
= (struct ap_device
*) data
;
1699 if (ap_dev
->reset
== AP_RESET_ARMED
) {
1700 ap_dev
->reset
= AP_RESET_DO
;
1702 if (ap_using_interrupts())
1703 tasklet_schedule(&ap_tasklet
);
1707 static void ap_reset_domain(void)
1711 if (ap_domain_index
!= -1)
1712 for (i
= 0; i
< AP_DEVICES
; i
++)
1713 ap_reset_queue(AP_MKQID(i
, ap_domain_index
));
1716 static void ap_reset_all(void)
1720 for (i
= 0; i
< AP_DOMAINS
; i
++)
1721 for (j
= 0; j
< AP_DEVICES
; j
++)
1722 ap_reset_queue(AP_MKQID(j
, i
));
1725 static struct reset_call ap_reset_call
= {
1730 * ap_module_init(): The module initialization code.
1732 * Initializes the module.
1734 int __init
ap_module_init(void)
1738 if (ap_domain_index
< -1 || ap_domain_index
>= AP_DOMAINS
) {
1739 pr_warning("%d is not a valid cryptographic domain\n",
1743 /* In resume callback we need to know if the user had set the domain.
1744 * If so, we can not just reset it.
1746 if (ap_domain_index
>= 0)
1747 user_set_domain
= 1;
1749 if (ap_instructions_available() != 0) {
1750 pr_warning("The hardware system does not support "
1751 "AP instructions\n");
1754 if (ap_interrupts_available()) {
1755 isc_register(AP_ISC
);
1756 ap_interrupt_indicator
= s390_register_adapter_interrupt(
1757 &ap_interrupt_handler
, NULL
, AP_ISC
);
1758 if (IS_ERR(ap_interrupt_indicator
)) {
1759 ap_interrupt_indicator
= NULL
;
1760 isc_unregister(AP_ISC
);
1764 register_reset_call(&ap_reset_call
);
1766 /* Create /sys/bus/ap. */
1767 rc
= bus_register(&ap_bus_type
);
1770 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1771 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1776 /* Create /sys/devices/ap. */
1777 ap_root_device
= root_device_register("ap");
1778 rc
= IS_ERR(ap_root_device
) ? PTR_ERR(ap_root_device
) : 0;
1782 ap_work_queue
= create_singlethread_workqueue("kapwork");
1783 if (!ap_work_queue
) {
1788 if (ap_select_domain() == 0)
1791 /* Setup the AP bus rescan timer. */
1792 init_timer(&ap_config_timer
);
1793 ap_config_timer
.function
= ap_config_timeout
;
1794 ap_config_timer
.data
= 0;
1795 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1796 add_timer(&ap_config_timer
);
1798 /* Setup the high resultion poll timer.
1799 * If we are running under z/VM adjust polling to z/VM polling rate.
1802 poll_timeout
= 1500000;
1803 spin_lock_init(&ap_poll_timer_lock
);
1804 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1805 ap_poll_timer
.function
= ap_poll_timeout
;
1807 /* Start the low priority AP bus poll thread. */
1808 if (ap_thread_flag
) {
1809 rc
= ap_poll_thread_start();
1817 del_timer_sync(&ap_config_timer
);
1818 hrtimer_cancel(&ap_poll_timer
);
1819 destroy_workqueue(ap_work_queue
);
1821 root_device_unregister(ap_root_device
);
1824 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1825 bus_unregister(&ap_bus_type
);
1827 unregister_reset_call(&ap_reset_call
);
1828 if (ap_using_interrupts()) {
1829 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1830 isc_unregister(AP_ISC
);
1835 static int __ap_match_all(struct device
*dev
, void *data
)
1841 * ap_modules_exit(): The module termination code
1843 * Terminates the module.
1845 void ap_module_exit(void)
1851 ap_poll_thread_stop();
1852 del_timer_sync(&ap_config_timer
);
1853 hrtimer_cancel(&ap_poll_timer
);
1854 destroy_workqueue(ap_work_queue
);
1855 tasklet_kill(&ap_tasklet
);
1856 root_device_unregister(ap_root_device
);
1857 while ((dev
= bus_find_device(&ap_bus_type
, NULL
, NULL
,
1860 device_unregister(dev
);
1863 for (i
= 0; ap_bus_attrs
[i
]; i
++)
1864 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1865 bus_unregister(&ap_bus_type
);
1866 unregister_reset_call(&ap_reset_call
);
1867 if (ap_using_interrupts()) {
1868 s390_unregister_adapter_interrupt(ap_interrupt_indicator
, AP_ISC
);
1869 isc_unregister(AP_ISC
);
1873 module_init(ap_module_init
);
1874 module_exit(ap_module_exit
);