2 * Copyright IBM Corp. 2006, 2012
3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
6 * Felix Beck <felix.beck@de.ibm.com>
7 * Holger Dengler <hd@linux.vnet.ibm.com>
9 * Adjunct processor bus.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/kernel_stat.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <asm/reset.h>
42 #include <linux/atomic.h>
44 #include <linux/hrtimer.h>
45 #include <linux/ktime.h>
46 #include <asm/facility.h>
47 #include <linux/crypto.h>
51 /* Some prototypes. */
52 static void ap_scan_bus(struct work_struct
*);
53 static void ap_poll_all(unsigned long);
54 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*);
55 static int ap_poll_thread_start(void);
56 static void ap_poll_thread_stop(void);
57 static void ap_request_timeout(unsigned long);
58 static inline void ap_schedule_poll_timer(void);
59 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
);
60 static int ap_device_remove(struct device
*dev
);
61 static int ap_device_probe(struct device
*dev
);
62 static void ap_interrupt_handler(struct airq_struct
*airq
);
63 static void ap_reset(struct ap_device
*ap_dev
);
64 static void ap_config_timeout(unsigned long ptr
);
65 static int ap_select_domain(void);
66 static void ap_query_configuration(void);
71 MODULE_AUTHOR("IBM Corporation");
72 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
73 "Copyright IBM Corp. 2006, 2012");
74 MODULE_LICENSE("GPL");
75 MODULE_ALIAS_CRYPTO("z90crypt");
80 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
81 module_param_named(domain
, ap_domain_index
, int, 0000);
82 MODULE_PARM_DESC(domain
, "domain index for ap devices");
83 EXPORT_SYMBOL(ap_domain_index
);
85 static int ap_thread_flag
= 0;
86 module_param_named(poll_thread
, ap_thread_flag
, int, 0000);
87 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
89 static struct device
*ap_root_device
= NULL
;
90 static struct ap_config_info
*ap_configuration
;
91 static DEFINE_SPINLOCK(ap_device_list_lock
);
92 static LIST_HEAD(ap_device_list
);
95 * Workqueue & timer for bus rescan.
97 static struct workqueue_struct
*ap_work_queue
;
98 static struct timer_list ap_config_timer
;
99 static int ap_config_time
= AP_CONFIG_TIME
;
100 static DECLARE_WORK(ap_config_work
, ap_scan_bus
);
103 * Tasklet & timer for AP request polling and interrupts
105 static DECLARE_TASKLET(ap_tasklet
, ap_poll_all
, 0);
106 static atomic_t ap_poll_requests
= ATOMIC_INIT(0);
107 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
108 static struct task_struct
*ap_poll_kthread
= NULL
;
109 static DEFINE_MUTEX(ap_poll_thread_mutex
);
110 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
111 static struct hrtimer ap_poll_timer
;
112 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
113 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
114 static unsigned long long poll_timeout
= 250000;
117 static int ap_suspend_flag
;
118 /* Flag to check if domain was set through module parameter domain=. This is
119 * important when supsend and resume is done in a z/VM environment where the
120 * domain might change. */
121 static int user_set_domain
= 0;
122 static struct bus_type ap_bus_type
;
124 /* Adapter interrupt definitions */
125 static int ap_airq_flag
;
127 static struct airq_struct ap_airq
= {
128 .handler
= ap_interrupt_handler
,
133 * ap_using_interrupts() - Returns non-zero if interrupt support is
136 static inline int ap_using_interrupts(void)
142 * ap_intructions_available() - Test if AP instructions are available.
144 * Returns 0 if the AP instructions are installed.
146 static inline int ap_instructions_available(void)
148 register unsigned long reg0
asm ("0") = AP_MKQID(0,0);
149 register unsigned long reg1
asm ("1") = -ENODEV
;
150 register unsigned long reg2
asm ("2") = 0UL;
153 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
157 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
) : : "cc" );
162 * ap_interrupts_available(): Test if AP interrupts are available.
164 * Returns 1 if AP interrupts are available.
166 static int ap_interrupts_available(void)
168 return test_facility(2) && test_facility(65);
172 * ap_configuration_available(): Test if AP configuration
173 * information is available.
175 * Returns 1 if AP configuration information is available.
178 static int ap_configuration_available(void)
180 return test_facility(2) && test_facility(12);
185 * ap_test_queue(): Test adjunct processor queue.
186 * @qid: The AP queue number
187 * @queue_depth: Pointer to queue depth value
188 * @device_type: Pointer to device type value
190 * Returns AP queue status structure.
192 static inline struct ap_queue_status
193 ap_test_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
195 register unsigned long reg0
asm ("0") = qid
;
196 register struct ap_queue_status reg1
asm ("1");
197 register unsigned long reg2
asm ("2") = 0UL;
199 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
200 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
201 *device_type
= (int) (reg2
>> 24);
202 *queue_depth
= (int) (reg2
& 0xff);
207 * ap_reset_queue(): Reset adjunct processor queue.
208 * @qid: The AP queue number
210 * Returns AP queue status structure.
212 static inline struct ap_queue_status
ap_reset_queue(ap_qid_t qid
)
214 register unsigned long reg0
asm ("0") = qid
| 0x01000000UL
;
215 register struct ap_queue_status reg1
asm ("1");
216 register unsigned long reg2
asm ("2") = 0UL;
219 ".long 0xb2af0000" /* PQAP(RAPQ) */
220 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
226 * ap_queue_interruption_control(): Enable interruption for a specific AP.
227 * @qid: The AP queue number
228 * @ind: The notification indicator byte
230 * Returns AP queue status.
232 static inline struct ap_queue_status
233 ap_queue_interruption_control(ap_qid_t qid
, void *ind
)
235 register unsigned long reg0
asm ("0") = qid
| 0x03000000UL
;
236 register unsigned long reg1_in
asm ("1") = 0x0000800000000000UL
| AP_ISC
;
237 register struct ap_queue_status reg1_out
asm ("1");
238 register void *reg2
asm ("2") = ind
;
240 ".long 0xb2af0000" /* PQAP(AQIC) */
241 : "+d" (reg0
), "+d" (reg1_in
), "=d" (reg1_out
), "+d" (reg2
)
249 static inline struct ap_queue_status
250 __ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
252 register unsigned long reg0
asm ("0") = 0UL | qid
| (1UL << 23);
253 register struct ap_queue_status reg1
asm ("1") = AP_QUEUE_STATUS_INVALID
;
254 register unsigned long reg2
asm ("2");
257 ".long 0xb2af0000\n" /* PQAP(TAPQ) */
260 : "+d" (reg0
), "+d" (reg1
), "=d" (reg2
)
264 *functions
= (unsigned int)(reg2
>> 32);
270 static inline int __ap_query_configuration(struct ap_config_info
*config
)
272 register unsigned long reg0
asm ("0") = 0x04000000UL
;
273 register unsigned long reg1
asm ("1") = -EINVAL
;
274 register unsigned char *reg2
asm ("2") = (unsigned char *)config
;
277 ".long 0xb2af0000\n" /* PQAP(QCI) */
281 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
)
290 * ap_query_functions(): Query supported functions.
291 * @qid: The AP queue number
292 * @functions: Pointer to functions field.
296 * -ENODEV if queue not valid.
297 * -EBUSY if device busy.
298 * -EINVAL if query function is not supported
300 static int ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
303 struct ap_queue_status status
;
305 status
= __ap_query_functions(qid
, functions
);
307 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
308 if (ap_queue_status_invalid_test(&status
))
311 switch (status
.response_code
) {
312 case AP_RESPONSE_NORMAL
:
314 case AP_RESPONSE_RESET_IN_PROGRESS
:
315 case AP_RESPONSE_BUSY
:
317 case AP_RESPONSE_Q_NOT_AVAIL
:
318 case AP_RESPONSE_DECONFIGURED
:
319 case AP_RESPONSE_CHECKSTOPPED
:
320 case AP_RESPONSE_INVALID_ADDRESS
:
322 case AP_RESPONSE_OTHERWISE_CHANGED
:
327 if (i
< AP_MAX_RESET
- 1) {
329 status
= __ap_query_functions(qid
, functions
);
339 * ap_queue_enable_interruption(): Enable interruption on an AP.
340 * @qid: The AP queue number
341 * @ind: the notification indicator byte
343 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
344 * on the return value it waits a while and tests the AP queue if interrupts
345 * have been switched on using ap_test_queue().
347 static int ap_queue_enable_interruption(ap_qid_t qid
, void *ind
)
350 struct ap_queue_status status
;
351 int t_depth
, t_device_type
, rc
, i
;
354 status
= ap_queue_interruption_control(qid
, ind
);
356 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
357 switch (status
.response_code
) {
358 case AP_RESPONSE_NORMAL
:
359 if (status
.int_enabled
)
362 case AP_RESPONSE_RESET_IN_PROGRESS
:
363 case AP_RESPONSE_BUSY
:
364 if (i
< AP_MAX_RESET
- 1) {
366 status
= ap_queue_interruption_control(qid
,
371 case AP_RESPONSE_Q_NOT_AVAIL
:
372 case AP_RESPONSE_DECONFIGURED
:
373 case AP_RESPONSE_CHECKSTOPPED
:
374 case AP_RESPONSE_INVALID_ADDRESS
:
376 case AP_RESPONSE_OTHERWISE_CHANGED
:
377 if (status
.int_enabled
)
383 if (i
< AP_MAX_RESET
- 1) {
385 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
395 * __ap_send(): Send message to adjunct processor queue.
396 * @qid: The AP queue number
397 * @psmid: The program supplied message identifier
398 * @msg: The message text
399 * @length: The message length
400 * @special: Special Bit
402 * Returns AP queue status structure.
403 * Condition code 1 on NQAP can't happen because the L bit is 1.
404 * Condition code 2 on NQAP also means the send is incomplete,
405 * because a segment boundary was reached. The NQAP is repeated.
407 static inline struct ap_queue_status
408 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
,
409 unsigned int special
)
411 typedef struct { char _
[length
]; } msgblock
;
412 register unsigned long reg0
asm ("0") = qid
| 0x40000000UL
;
413 register struct ap_queue_status reg1
asm ("1");
414 register unsigned long reg2
asm ("2") = (unsigned long) msg
;
415 register unsigned long reg3
asm ("3") = (unsigned long) length
;
416 register unsigned long reg4
asm ("4") = (unsigned int) (psmid
>> 32);
417 register unsigned long reg5
asm ("5") = psmid
& 0xffffffff;
423 "0: .long 0xb2ad0042\n" /* NQAP */
425 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
), "+d" (reg3
)
426 : "d" (reg4
), "d" (reg5
), "m" (*(msgblock
*) msg
)
431 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
433 struct ap_queue_status status
;
435 status
= __ap_send(qid
, psmid
, msg
, length
, 0);
436 switch (status
.response_code
) {
437 case AP_RESPONSE_NORMAL
:
439 case AP_RESPONSE_Q_FULL
:
440 case AP_RESPONSE_RESET_IN_PROGRESS
:
442 case AP_RESPONSE_REQ_FAC_NOT_INST
:
444 default: /* Device is gone. */
448 EXPORT_SYMBOL(ap_send
);
451 * __ap_recv(): Receive message from adjunct processor queue.
452 * @qid: The AP queue number
453 * @psmid: Pointer to program supplied message identifier
454 * @msg: The message text
455 * @length: The message length
457 * Returns AP queue status structure.
458 * Condition code 1 on DQAP means the receive has taken place
459 * but only partially. The response is incomplete, hence the
461 * Condition code 2 on DQAP also means the receive is incomplete,
462 * this time because a segment boundary was reached. Again, the
464 * Note that gpr2 is used by the DQAP instruction to keep track of
465 * any 'residual' length, in case the instruction gets interrupted.
466 * Hence it gets zeroed before the instruction.
468 static inline struct ap_queue_status
469 __ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
471 typedef struct { char _
[length
]; } msgblock
;
472 register unsigned long reg0
asm("0") = qid
| 0x80000000UL
;
473 register struct ap_queue_status reg1
asm ("1");
474 register unsigned long reg2
asm("2") = 0UL;
475 register unsigned long reg4
asm("4") = (unsigned long) msg
;
476 register unsigned long reg5
asm("5") = (unsigned long) length
;
477 register unsigned long reg6
asm("6") = 0UL;
478 register unsigned long reg7
asm("7") = 0UL;
482 "0: .long 0xb2ae0064\n" /* DQAP */
484 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
),
485 "+d" (reg4
), "+d" (reg5
), "+d" (reg6
), "+d" (reg7
),
486 "=m" (*(msgblock
*) msg
) : : "cc" );
487 *psmid
= (((unsigned long long) reg6
) << 32) + reg7
;
491 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
493 struct ap_queue_status status
;
495 status
= __ap_recv(qid
, psmid
, msg
, length
);
496 switch (status
.response_code
) {
497 case AP_RESPONSE_NORMAL
:
499 case AP_RESPONSE_NO_PENDING_REPLY
:
500 if (status
.queue_empty
)
503 case AP_RESPONSE_RESET_IN_PROGRESS
:
509 EXPORT_SYMBOL(ap_recv
);
512 * ap_query_queue(): Check if an AP queue is available.
513 * @qid: The AP queue number
514 * @queue_depth: Pointer to queue depth value
515 * @device_type: Pointer to device type value
517 * The test is repeated for AP_MAX_RESET times.
519 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
521 struct ap_queue_status status
;
522 int t_depth
, t_device_type
, rc
, i
;
525 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
526 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
527 switch (status
.response_code
) {
528 case AP_RESPONSE_NORMAL
:
529 *queue_depth
= t_depth
+ 1;
530 *device_type
= t_device_type
;
533 case AP_RESPONSE_Q_NOT_AVAIL
:
536 case AP_RESPONSE_RESET_IN_PROGRESS
:
538 case AP_RESPONSE_DECONFIGURED
:
541 case AP_RESPONSE_CHECKSTOPPED
:
544 case AP_RESPONSE_INVALID_ADDRESS
:
547 case AP_RESPONSE_OTHERWISE_CHANGED
:
549 case AP_RESPONSE_BUSY
:
556 if (i
< AP_MAX_RESET
- 1)
563 * ap_init_queue(): Reset an AP queue.
564 * @qid: The AP queue number
566 * Reset an AP queue and wait for it to become available again.
568 static int ap_init_queue(ap_qid_t qid
)
570 struct ap_queue_status status
;
574 status
= ap_reset_queue(qid
);
575 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
576 switch (status
.response_code
) {
577 case AP_RESPONSE_NORMAL
:
578 if (status
.queue_empty
)
581 case AP_RESPONSE_Q_NOT_AVAIL
:
582 case AP_RESPONSE_DECONFIGURED
:
583 case AP_RESPONSE_CHECKSTOPPED
:
584 i
= AP_MAX_RESET
; /* return with -ENODEV */
586 case AP_RESPONSE_RESET_IN_PROGRESS
:
588 case AP_RESPONSE_BUSY
:
592 if (rc
!= -ENODEV
&& rc
!= -EBUSY
)
594 if (i
< AP_MAX_RESET
- 1) {
596 status
= ap_test_queue(qid
, &dummy
, &dummy
);
599 if (rc
== 0 && ap_using_interrupts()) {
600 rc
= ap_queue_enable_interruption(qid
, ap_airq
.lsi_ptr
);
601 /* If interruption mode is supported by the machine,
602 * but an AP can not be enabled for interruption then
603 * the AP will be discarded. */
605 pr_err("Registering adapter interrupts for "
606 "AP %d failed\n", AP_QID_DEVICE(qid
));
612 * ap_increase_queue_count(): Arm request timeout.
613 * @ap_dev: Pointer to an AP device.
615 * Arm request timeout if an AP device was idle and a new request is submitted.
617 static void ap_increase_queue_count(struct ap_device
*ap_dev
)
619 int timeout
= ap_dev
->drv
->request_timeout
;
621 ap_dev
->queue_count
++;
622 if (ap_dev
->queue_count
== 1) {
623 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
624 ap_dev
->reset
= AP_RESET_ARMED
;
629 * ap_decrease_queue_count(): Decrease queue count.
630 * @ap_dev: Pointer to an AP device.
632 * If AP device is still alive, re-schedule request timeout if there are still
635 static void ap_decrease_queue_count(struct ap_device
*ap_dev
)
637 int timeout
= ap_dev
->drv
->request_timeout
;
639 ap_dev
->queue_count
--;
640 if (ap_dev
->queue_count
> 0)
641 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
644 * The timeout timer should to be disabled now - since
645 * del_timer_sync() is very expensive, we just tell via the
646 * reset flag to ignore the pending timeout timer.
648 ap_dev
->reset
= AP_RESET_IGNORE
;
652 * AP device related attributes.
654 static ssize_t
ap_hwtype_show(struct device
*dev
,
655 struct device_attribute
*attr
, char *buf
)
657 struct ap_device
*ap_dev
= to_ap_dev(dev
);
658 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->device_type
);
661 static DEVICE_ATTR(hwtype
, 0444, ap_hwtype_show
, NULL
);
662 static ssize_t
ap_depth_show(struct device
*dev
, struct device_attribute
*attr
,
665 struct ap_device
*ap_dev
= to_ap_dev(dev
);
666 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->queue_depth
);
669 static DEVICE_ATTR(depth
, 0444, ap_depth_show
, NULL
);
670 static ssize_t
ap_request_count_show(struct device
*dev
,
671 struct device_attribute
*attr
,
674 struct ap_device
*ap_dev
= to_ap_dev(dev
);
677 spin_lock_bh(&ap_dev
->lock
);
678 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->total_request_count
);
679 spin_unlock_bh(&ap_dev
->lock
);
683 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
685 static ssize_t
ap_requestq_count_show(struct device
*dev
,
686 struct device_attribute
*attr
, char *buf
)
688 struct ap_device
*ap_dev
= to_ap_dev(dev
);
691 spin_lock_bh(&ap_dev
->lock
);
692 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->requestq_count
);
693 spin_unlock_bh(&ap_dev
->lock
);
697 static DEVICE_ATTR(requestq_count
, 0444, ap_requestq_count_show
, NULL
);
699 static ssize_t
ap_pendingq_count_show(struct device
*dev
,
700 struct device_attribute
*attr
, char *buf
)
702 struct ap_device
*ap_dev
= to_ap_dev(dev
);
705 spin_lock_bh(&ap_dev
->lock
);
706 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->pendingq_count
);
707 spin_unlock_bh(&ap_dev
->lock
);
711 static DEVICE_ATTR(pendingq_count
, 0444, ap_pendingq_count_show
, NULL
);
713 static ssize_t
ap_modalias_show(struct device
*dev
,
714 struct device_attribute
*attr
, char *buf
)
716 return sprintf(buf
, "ap:t%02X", to_ap_dev(dev
)->device_type
);
719 static DEVICE_ATTR(modalias
, 0444, ap_modalias_show
, NULL
);
721 static ssize_t
ap_functions_show(struct device
*dev
,
722 struct device_attribute
*attr
, char *buf
)
724 struct ap_device
*ap_dev
= to_ap_dev(dev
);
725 return snprintf(buf
, PAGE_SIZE
, "0x%08X\n", ap_dev
->functions
);
728 static DEVICE_ATTR(ap_functions
, 0444, ap_functions_show
, NULL
);
730 static struct attribute
*ap_dev_attrs
[] = {
731 &dev_attr_hwtype
.attr
,
732 &dev_attr_depth
.attr
,
733 &dev_attr_request_count
.attr
,
734 &dev_attr_requestq_count
.attr
,
735 &dev_attr_pendingq_count
.attr
,
736 &dev_attr_modalias
.attr
,
737 &dev_attr_ap_functions
.attr
,
740 static struct attribute_group ap_dev_attr_group
= {
741 .attrs
= ap_dev_attrs
746 * @dev: Pointer to device
747 * @drv: Pointer to device_driver
749 * AP bus driver registration/unregistration.
751 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
753 struct ap_device
*ap_dev
= to_ap_dev(dev
);
754 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
755 struct ap_device_id
*id
;
758 * Compare device type of the device with the list of
759 * supported types of the device_driver.
761 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
762 if ((id
->match_flags
& AP_DEVICE_ID_MATCH_DEVICE_TYPE
) &&
763 (id
->dev_type
!= ap_dev
->device_type
))
771 * ap_uevent(): Uevent function for AP devices.
772 * @dev: Pointer to device
773 * @env: Pointer to kobj_uevent_env
775 * It sets up a single environment variable DEV_TYPE which contains the
776 * hardware device type.
778 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
780 struct ap_device
*ap_dev
= to_ap_dev(dev
);
786 /* Set up DEV_TYPE environment variable. */
787 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
792 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
797 static int ap_bus_suspend(struct device
*dev
, pm_message_t state
)
799 struct ap_device
*ap_dev
= to_ap_dev(dev
);
802 if (!ap_suspend_flag
) {
805 /* Disable scanning for devices, thus we do not want to scan
806 * for them after removing.
808 del_timer_sync(&ap_config_timer
);
809 if (ap_work_queue
!= NULL
) {
810 destroy_workqueue(ap_work_queue
);
811 ap_work_queue
= NULL
;
814 tasklet_disable(&ap_tasklet
);
816 /* Poll on the device until all requests are finished. */
819 spin_lock_bh(&ap_dev
->lock
);
820 __ap_poll_device(ap_dev
, &flags
);
821 spin_unlock_bh(&ap_dev
->lock
);
822 } while ((flags
& 1) || (flags
& 2));
824 spin_lock_bh(&ap_dev
->lock
);
825 ap_dev
->unregistered
= 1;
826 spin_unlock_bh(&ap_dev
->lock
);
831 static int ap_bus_resume(struct device
*dev
)
833 struct ap_device
*ap_dev
= to_ap_dev(dev
);
836 if (ap_suspend_flag
) {
838 if (ap_interrupts_available()) {
839 if (!ap_using_interrupts()) {
840 rc
= register_adapter_interrupt(&ap_airq
);
841 ap_airq_flag
= (rc
== 0);
844 if (ap_using_interrupts()) {
845 unregister_adapter_interrupt(&ap_airq
);
849 ap_query_configuration();
850 if (!user_set_domain
) {
851 ap_domain_index
= -1;
854 init_timer(&ap_config_timer
);
855 ap_config_timer
.function
= ap_config_timeout
;
856 ap_config_timer
.data
= 0;
857 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
858 add_timer(&ap_config_timer
);
859 ap_work_queue
= create_singlethread_workqueue("kapwork");
862 tasklet_enable(&ap_tasklet
);
863 if (!ap_using_interrupts())
864 ap_schedule_poll_timer();
866 tasklet_schedule(&ap_tasklet
);
868 rc
= ap_poll_thread_start();
873 if (AP_QID_QUEUE(ap_dev
->qid
) != ap_domain_index
) {
874 spin_lock_bh(&ap_dev
->lock
);
875 ap_dev
->qid
= AP_MKQID(AP_QID_DEVICE(ap_dev
->qid
),
877 spin_unlock_bh(&ap_dev
->lock
);
879 queue_work(ap_work_queue
, &ap_config_work
);
884 static struct bus_type ap_bus_type
= {
886 .match
= &ap_bus_match
,
887 .uevent
= &ap_uevent
,
888 .suspend
= ap_bus_suspend
,
889 .resume
= ap_bus_resume
892 static int ap_device_probe(struct device
*dev
)
894 struct ap_device
*ap_dev
= to_ap_dev(dev
);
895 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
898 ap_dev
->drv
= ap_drv
;
899 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
901 spin_lock_bh(&ap_device_list_lock
);
902 list_add(&ap_dev
->list
, &ap_device_list
);
903 spin_unlock_bh(&ap_device_list_lock
);
909 * __ap_flush_queue(): Flush requests.
910 * @ap_dev: Pointer to the AP device
912 * Flush all requests from the request/pending queue of an AP device.
914 static void __ap_flush_queue(struct ap_device
*ap_dev
)
916 struct ap_message
*ap_msg
, *next
;
918 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->pendingq
, list
) {
919 list_del_init(&ap_msg
->list
);
920 ap_dev
->pendingq_count
--;
921 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
923 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->requestq
, list
) {
924 list_del_init(&ap_msg
->list
);
925 ap_dev
->requestq_count
--;
926 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
930 void ap_flush_queue(struct ap_device
*ap_dev
)
932 spin_lock_bh(&ap_dev
->lock
);
933 __ap_flush_queue(ap_dev
);
934 spin_unlock_bh(&ap_dev
->lock
);
936 EXPORT_SYMBOL(ap_flush_queue
);
938 static int ap_device_remove(struct device
*dev
)
940 struct ap_device
*ap_dev
= to_ap_dev(dev
);
941 struct ap_driver
*ap_drv
= ap_dev
->drv
;
943 ap_flush_queue(ap_dev
);
944 del_timer_sync(&ap_dev
->timeout
);
945 spin_lock_bh(&ap_device_list_lock
);
946 list_del_init(&ap_dev
->list
);
947 spin_unlock_bh(&ap_device_list_lock
);
949 ap_drv
->remove(ap_dev
);
950 spin_lock_bh(&ap_dev
->lock
);
951 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
952 spin_unlock_bh(&ap_dev
->lock
);
956 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
959 struct device_driver
*drv
= &ap_drv
->driver
;
961 drv
->bus
= &ap_bus_type
;
962 drv
->probe
= ap_device_probe
;
963 drv
->remove
= ap_device_remove
;
966 return driver_register(drv
);
968 EXPORT_SYMBOL(ap_driver_register
);
970 void ap_driver_unregister(struct ap_driver
*ap_drv
)
972 driver_unregister(&ap_drv
->driver
);
974 EXPORT_SYMBOL(ap_driver_unregister
);
976 void ap_bus_force_rescan(void)
978 /* reconfigure the AP bus rescan timer. */
979 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
980 /* processing a asynchronous bus rescan */
981 queue_work(ap_work_queue
, &ap_config_work
);
982 flush_work(&ap_config_work
);
984 EXPORT_SYMBOL(ap_bus_force_rescan
);
989 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
991 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
994 static BUS_ATTR(ap_domain
, 0444, ap_domain_show
, NULL
);
996 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
998 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
1001 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
1003 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1004 ap_using_interrupts() ? 1 : 0);
1007 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
1009 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
1010 const char *buf
, size_t count
)
1014 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
1016 ap_config_time
= time
;
1017 if (!timer_pending(&ap_config_timer
) ||
1018 !mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
)) {
1019 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1020 add_timer(&ap_config_timer
);
1025 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
1027 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
1029 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
1032 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
1033 const char *buf
, size_t count
)
1037 if (sscanf(buf
, "%d\n", &flag
) != 1)
1040 rc
= ap_poll_thread_start();
1045 ap_poll_thread_stop();
1049 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
1051 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
1053 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
1056 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
1059 unsigned long long time
;
1062 /* 120 seconds = maximum poll interval */
1063 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
1064 time
> 120000000000ULL)
1066 poll_timeout
= time
;
1067 hr_time
= ktime_set(0, poll_timeout
);
1069 if (!hrtimer_is_queued(&ap_poll_timer
) ||
1070 !hrtimer_forward(&ap_poll_timer
, hrtimer_get_expires(&ap_poll_timer
), hr_time
)) {
1071 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
1072 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
1077 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
1079 static struct bus_attribute
*const ap_bus_attrs
[] = {
1080 &bus_attr_ap_domain
,
1081 &bus_attr_config_time
,
1082 &bus_attr_poll_thread
,
1083 &bus_attr_ap_interrupts
,
1084 &bus_attr_poll_timeout
,
1088 static inline int ap_test_config(unsigned int *field
, unsigned int nr
)
1092 return ap_test_bit((field
+ (nr
>> 5)), (nr
& 0x1f));
1096 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
1099 * Returns 0 if the card is not configured
1100 * 1 if the card is configured or
1101 * if the configuration information is not available
1103 static inline int ap_test_config_card_id(unsigned int id
)
1105 if (!ap_configuration
)
1107 return ap_test_config(ap_configuration
->apm
, id
);
1111 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
1112 * @domain AP usage domain ID
1114 * Returns 0 if the usage domain is not configured
1115 * 1 if the usage domain is configured or
1116 * if the configuration information is not available
1118 static inline int ap_test_config_domain(unsigned int domain
)
1120 if (!ap_configuration
)
1122 return ap_test_config(ap_configuration
->aqm
, domain
);
1126 * ap_query_configuration(): Query AP configuration information.
1128 * Query information of installed cards and configured domains from AP.
1130 static void ap_query_configuration(void)
1133 if (ap_configuration_available()) {
1134 if (!ap_configuration
)
1136 kzalloc(sizeof(struct ap_config_info
),
1138 if (ap_configuration
)
1139 __ap_query_configuration(ap_configuration
);
1141 ap_configuration
= NULL
;
1143 ap_configuration
= NULL
;
1148 * ap_select_domain(): Select an AP domain.
1150 * Pick one of the 16 AP domains.
1152 static int ap_select_domain(void)
1154 int queue_depth
, device_type
, count
, max_count
, best_domain
;
1159 * We want to use a single domain. Either the one specified with
1160 * the "domain=" parameter or the domain with the maximum number
1163 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
)
1164 /* Domain has already been selected. */
1168 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1169 if (!ap_test_config_domain(i
))
1172 for (j
= 0; j
< AP_DEVICES
; j
++) {
1173 if (!ap_test_config_card_id(j
))
1175 qid
= AP_MKQID(j
, i
);
1176 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1181 if (count
> max_count
) {
1186 if (best_domain
>= 0){
1187 ap_domain_index
= best_domain
;
1194 * ap_probe_device_type(): Find the device type of an AP.
1195 * @ap_dev: pointer to the AP device.
1197 * Find the device type if query queue returned a device type of 0.
1199 static int ap_probe_device_type(struct ap_device
*ap_dev
)
1201 static unsigned char msg
[] = {
1202 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1203 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1204 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1205 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1206 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1207 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1208 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1209 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1210 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1211 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1212 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1213 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1214 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1215 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1216 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1217 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1218 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1219 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1220 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1221 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1222 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1223 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1224 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1225 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1226 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1227 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1228 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1229 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1230 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1231 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1232 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1233 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1234 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1235 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1236 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1237 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1238 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1239 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1240 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1241 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1242 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1243 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1244 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1245 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1246 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1248 struct ap_queue_status status
;
1249 unsigned long long psmid
;
1253 reply
= (void *) get_zeroed_page(GFP_KERNEL
);
1259 status
= __ap_send(ap_dev
->qid
, 0x0102030405060708ULL
,
1260 msg
, sizeof(msg
), 0);
1261 if (status
.response_code
!= AP_RESPONSE_NORMAL
) {
1266 /* Wait for the test message to complete. */
1267 for (i
= 0; i
< 6; i
++) {
1269 status
= __ap_recv(ap_dev
->qid
, &psmid
, reply
, 4096);
1270 if (status
.response_code
== AP_RESPONSE_NORMAL
&&
1271 psmid
== 0x0102030405060708ULL
)
1275 /* Got an answer. */
1276 if (reply
[0] == 0x00 && reply
[1] == 0x86)
1277 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICC
;
1279 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICA
;
1285 free_page((unsigned long) reply
);
1290 static void ap_interrupt_handler(struct airq_struct
*airq
)
1292 inc_irq_stat(IRQIO_APB
);
1293 tasklet_schedule(&ap_tasklet
);
1297 * __ap_scan_bus(): Scan the AP bus.
1298 * @dev: Pointer to device
1299 * @data: Pointer to data
1301 * Scan the AP bus for new devices.
1303 static int __ap_scan_bus(struct device
*dev
, void *data
)
1305 return to_ap_dev(dev
)->qid
== (ap_qid_t
)(unsigned long) data
;
1308 static void ap_device_release(struct device
*dev
)
1310 struct ap_device
*ap_dev
= to_ap_dev(dev
);
1315 static void ap_scan_bus(struct work_struct
*unused
)
1317 struct ap_device
*ap_dev
;
1320 int queue_depth
, device_type
;
1321 unsigned int device_functions
;
1324 ap_query_configuration();
1325 if (ap_select_domain() != 0) {
1328 for (i
= 0; i
< AP_DEVICES
; i
++) {
1329 qid
= AP_MKQID(i
, ap_domain_index
);
1330 dev
= bus_find_device(&ap_bus_type
, NULL
,
1331 (void *)(unsigned long)qid
,
1333 if (ap_test_config_card_id(i
))
1334 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1339 set_current_state(TASK_UNINTERRUPTIBLE
);
1340 schedule_timeout(AP_RESET_TIMEOUT
);
1341 rc
= ap_query_queue(qid
, &queue_depth
,
1344 ap_dev
= to_ap_dev(dev
);
1345 spin_lock_bh(&ap_dev
->lock
);
1346 if (rc
|| ap_dev
->unregistered
) {
1347 spin_unlock_bh(&ap_dev
->lock
);
1348 if (ap_dev
->unregistered
)
1350 device_unregister(dev
);
1354 spin_unlock_bh(&ap_dev
->lock
);
1360 rc
= ap_init_queue(qid
);
1363 ap_dev
= kzalloc(sizeof(*ap_dev
), GFP_KERNEL
);
1367 ap_dev
->queue_depth
= queue_depth
;
1368 ap_dev
->unregistered
= 1;
1369 spin_lock_init(&ap_dev
->lock
);
1370 INIT_LIST_HEAD(&ap_dev
->pendingq
);
1371 INIT_LIST_HEAD(&ap_dev
->requestq
);
1372 INIT_LIST_HEAD(&ap_dev
->list
);
1373 setup_timer(&ap_dev
->timeout
, ap_request_timeout
,
1374 (unsigned long) ap_dev
);
1375 switch (device_type
) {
1377 /* device type probing for old cards */
1378 if (ap_probe_device_type(ap_dev
)) {
1384 ap_dev
->device_type
= device_type
;
1387 rc
= ap_query_functions(qid
, &device_functions
);
1389 ap_dev
->functions
= device_functions
;
1391 ap_dev
->functions
= 0u;
1393 ap_dev
->device
.bus
= &ap_bus_type
;
1394 ap_dev
->device
.parent
= ap_root_device
;
1395 if (dev_set_name(&ap_dev
->device
, "card%02x",
1396 AP_QID_DEVICE(ap_dev
->qid
))) {
1400 ap_dev
->device
.release
= ap_device_release
;
1401 rc
= device_register(&ap_dev
->device
);
1403 put_device(&ap_dev
->device
);
1406 /* Add device attributes. */
1407 rc
= sysfs_create_group(&ap_dev
->device
.kobj
,
1408 &ap_dev_attr_group
);
1410 spin_lock_bh(&ap_dev
->lock
);
1411 ap_dev
->unregistered
= 0;
1412 spin_unlock_bh(&ap_dev
->lock
);
1415 device_unregister(&ap_dev
->device
);
1420 ap_config_timeout(unsigned long ptr
)
1422 queue_work(ap_work_queue
, &ap_config_work
);
1423 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1424 add_timer(&ap_config_timer
);
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)
1436 spin_lock_bh(&ap_poll_timer_lock
);
1437 if (hrtimer_is_queued(&ap_poll_timer
) || ap_suspend_flag
)
1439 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer
)) <= 0) {
1440 hr_time
= ktime_set(0, poll_timeout
);
1441 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
1442 hrtimer_restart(&ap_poll_timer
);
1445 spin_unlock_bh(&ap_poll_timer_lock
);
1449 * ap_schedule_poll_timer(): Schedule poll timer.
1451 * Set up the timer to run the poll tasklet
1453 static inline void ap_schedule_poll_timer(void)
1455 if (ap_using_interrupts())
1457 __ap_schedule_poll_timer();
1461 * ap_poll_read(): Receive pending reply messages from an AP device.
1462 * @ap_dev: pointer to the AP device
1463 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1464 * required, bit 2^1 is set if the poll timer needs to get armed
1466 * Returns 0 if the device is still present, -ENODEV if not.
1468 static int ap_poll_read(struct ap_device
*ap_dev
, unsigned long *flags
)
1470 struct ap_queue_status status
;
1471 struct ap_message
*ap_msg
;
1473 if (ap_dev
->queue_count
<= 0)
1475 status
= __ap_recv(ap_dev
->qid
, &ap_dev
->reply
->psmid
,
1476 ap_dev
->reply
->message
, ap_dev
->reply
->length
);
1477 switch (status
.response_code
) {
1478 case AP_RESPONSE_NORMAL
:
1479 atomic_dec(&ap_poll_requests
);
1480 ap_decrease_queue_count(ap_dev
);
1481 list_for_each_entry(ap_msg
, &ap_dev
->pendingq
, list
) {
1482 if (ap_msg
->psmid
!= ap_dev
->reply
->psmid
)
1484 list_del_init(&ap_msg
->list
);
1485 ap_dev
->pendingq_count
--;
1486 ap_msg
->receive(ap_dev
, ap_msg
, ap_dev
->reply
);
1489 if (ap_dev
->queue_count
> 0)
1492 case AP_RESPONSE_NO_PENDING_REPLY
:
1493 if (status
.queue_empty
) {
1494 /* The card shouldn't forget requests but who knows. */
1495 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1496 ap_dev
->queue_count
= 0;
1497 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1498 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1499 ap_dev
->pendingq_count
= 0;
1510 * ap_poll_write(): Send messages from the request queue to an AP device.
1511 * @ap_dev: pointer to the AP device
1512 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1513 * required, bit 2^1 is set if the poll timer needs to get armed
1515 * Returns 0 if the device is still present, -ENODEV if not.
1517 static int ap_poll_write(struct ap_device
*ap_dev
, unsigned long *flags
)
1519 struct ap_queue_status status
;
1520 struct ap_message
*ap_msg
;
1522 if (ap_dev
->requestq_count
<= 0 ||
1523 ap_dev
->queue_count
>= ap_dev
->queue_depth
)
1525 /* Start the next request on the queue. */
1526 ap_msg
= list_entry(ap_dev
->requestq
.next
, struct ap_message
, list
);
1527 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1528 ap_msg
->message
, ap_msg
->length
, ap_msg
->special
);
1529 switch (status
.response_code
) {
1530 case AP_RESPONSE_NORMAL
:
1531 atomic_inc(&ap_poll_requests
);
1532 ap_increase_queue_count(ap_dev
);
1533 list_move_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1534 ap_dev
->requestq_count
--;
1535 ap_dev
->pendingq_count
++;
1536 if (ap_dev
->queue_count
< ap_dev
->queue_depth
&&
1537 ap_dev
->requestq_count
> 0)
1541 case AP_RESPONSE_RESET_IN_PROGRESS
:
1542 __ap_schedule_poll_timer();
1543 case AP_RESPONSE_Q_FULL
:
1546 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1547 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1556 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1557 * @ap_dev: pointer to the bus device
1558 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1559 * required, bit 2^1 is set if the poll timer needs to get armed
1561 * Poll AP device for pending replies and send new messages. If either
1562 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1565 static inline int ap_poll_queue(struct ap_device
*ap_dev
, unsigned long *flags
)
1569 rc
= ap_poll_read(ap_dev
, flags
);
1572 return ap_poll_write(ap_dev
, flags
);
1576 * __ap_queue_message(): Queue a message to a device.
1577 * @ap_dev: pointer to the AP device
1578 * @ap_msg: the message to be queued
1580 * Queue a message to a device. Returns 0 if successful.
1582 static int __ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1584 struct ap_queue_status status
;
1586 if (list_empty(&ap_dev
->requestq
) &&
1587 ap_dev
->queue_count
< ap_dev
->queue_depth
) {
1588 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1589 ap_msg
->message
, ap_msg
->length
,
1591 switch (status
.response_code
) {
1592 case AP_RESPONSE_NORMAL
:
1593 list_add_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1594 atomic_inc(&ap_poll_requests
);
1595 ap_dev
->pendingq_count
++;
1596 ap_increase_queue_count(ap_dev
);
1597 ap_dev
->total_request_count
++;
1599 case AP_RESPONSE_Q_FULL
:
1600 case AP_RESPONSE_RESET_IN_PROGRESS
:
1601 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1602 ap_dev
->requestq_count
++;
1603 ap_dev
->total_request_count
++;
1605 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1606 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1607 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-EINVAL
));
1609 default: /* Device is gone. */
1610 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1614 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1615 ap_dev
->requestq_count
++;
1616 ap_dev
->total_request_count
++;
1619 ap_schedule_poll_timer();
1623 void ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1625 unsigned long flags
;
1628 /* For asynchronous message handling a valid receive-callback
1630 BUG_ON(!ap_msg
->receive
);
1632 spin_lock_bh(&ap_dev
->lock
);
1633 if (!ap_dev
->unregistered
) {
1634 /* Make room on the queue by polling for finished requests. */
1635 rc
= ap_poll_queue(ap_dev
, &flags
);
1637 rc
= __ap_queue_message(ap_dev
, ap_msg
);
1639 wake_up(&ap_poll_wait
);
1641 ap_dev
->unregistered
= 1;
1643 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1646 spin_unlock_bh(&ap_dev
->lock
);
1648 device_unregister(&ap_dev
->device
);
1650 EXPORT_SYMBOL(ap_queue_message
);
1653 * ap_cancel_message(): Cancel a crypto request.
1654 * @ap_dev: The AP device that has the message queued
1655 * @ap_msg: The message that is to be removed
1657 * Cancel a crypto request. This is done by removing the request
1658 * from the device pending or request queue. Note that the
1659 * request stays on the AP queue. When it finishes the message
1660 * reply will be discarded because the psmid can't be found.
1662 void ap_cancel_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1664 struct ap_message
*tmp
;
1666 spin_lock_bh(&ap_dev
->lock
);
1667 if (!list_empty(&ap_msg
->list
)) {
1668 list_for_each_entry(tmp
, &ap_dev
->pendingq
, list
)
1669 if (tmp
->psmid
== ap_msg
->psmid
) {
1670 ap_dev
->pendingq_count
--;
1673 ap_dev
->requestq_count
--;
1675 list_del_init(&ap_msg
->list
);
1677 spin_unlock_bh(&ap_dev
->lock
);
1679 EXPORT_SYMBOL(ap_cancel_message
);
1682 * ap_poll_timeout(): AP receive polling for finished AP requests.
1683 * @unused: Unused pointer.
1685 * Schedules the AP tasklet using a high resolution timer.
1687 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
1689 tasklet_schedule(&ap_tasklet
);
1690 return HRTIMER_NORESTART
;
1694 * ap_reset(): Reset a not responding AP device.
1695 * @ap_dev: Pointer to the AP device
1697 * Reset a not responding AP device and move all requests from the
1698 * pending queue to the request queue.
1700 static void ap_reset(struct ap_device
*ap_dev
)
1704 ap_dev
->reset
= AP_RESET_IGNORE
;
1705 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1706 ap_dev
->queue_count
= 0;
1707 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1708 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1709 ap_dev
->pendingq_count
= 0;
1710 rc
= ap_init_queue(ap_dev
->qid
);
1712 ap_dev
->unregistered
= 1;
1714 __ap_schedule_poll_timer();
1717 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
)
1719 if (!ap_dev
->unregistered
) {
1720 if (ap_poll_queue(ap_dev
, flags
))
1721 ap_dev
->unregistered
= 1;
1722 if (ap_dev
->reset
== AP_RESET_DO
)
1729 * ap_poll_all(): Poll all AP devices.
1730 * @dummy: Unused variable
1732 * Poll all AP devices on the bus in a round robin fashion. Continue
1733 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1734 * of the control flags has been set arm the poll timer.
1736 static void ap_poll_all(unsigned long dummy
)
1738 unsigned long flags
;
1739 struct ap_device
*ap_dev
;
1741 /* Reset the indicator if interrupts are used. Thus new interrupts can
1742 * be received. Doing it in the beginning of the tasklet is therefor
1743 * important that no requests on any AP get lost.
1745 if (ap_using_interrupts())
1746 xchg(ap_airq
.lsi_ptr
, 0);
1749 spin_lock(&ap_device_list_lock
);
1750 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1751 spin_lock(&ap_dev
->lock
);
1752 __ap_poll_device(ap_dev
, &flags
);
1753 spin_unlock(&ap_dev
->lock
);
1755 spin_unlock(&ap_device_list_lock
);
1756 } while (flags
& 1);
1758 ap_schedule_poll_timer();
1762 * ap_poll_thread(): Thread that polls for finished requests.
1763 * @data: Unused pointer
1765 * AP bus poll thread. The purpose of this thread is to poll for
1766 * finished requests in a loop if there is a "free" cpu - that is
1767 * a cpu that doesn't have anything better to do. The polling stops
1768 * as soon as there is another task or if all messages have been
1771 static int ap_poll_thread(void *data
)
1773 DECLARE_WAITQUEUE(wait
, current
);
1774 unsigned long flags
;
1776 struct ap_device
*ap_dev
;
1778 set_user_nice(current
, 19);
1780 if (ap_suspend_flag
)
1782 if (need_resched()) {
1786 add_wait_queue(&ap_poll_wait
, &wait
);
1787 set_current_state(TASK_INTERRUPTIBLE
);
1788 if (kthread_should_stop())
1790 requests
= atomic_read(&ap_poll_requests
);
1793 set_current_state(TASK_RUNNING
);
1794 remove_wait_queue(&ap_poll_wait
, &wait
);
1797 spin_lock_bh(&ap_device_list_lock
);
1798 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1799 spin_lock(&ap_dev
->lock
);
1800 __ap_poll_device(ap_dev
, &flags
);
1801 spin_unlock(&ap_dev
->lock
);
1803 spin_unlock_bh(&ap_device_list_lock
);
1805 set_current_state(TASK_RUNNING
);
1806 remove_wait_queue(&ap_poll_wait
, &wait
);
1810 static int ap_poll_thread_start(void)
1814 if (ap_using_interrupts() || ap_suspend_flag
)
1816 mutex_lock(&ap_poll_thread_mutex
);
1817 if (!ap_poll_kthread
) {
1818 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
1819 rc
= PTR_RET(ap_poll_kthread
);
1821 ap_poll_kthread
= NULL
;
1825 mutex_unlock(&ap_poll_thread_mutex
);
1829 static void ap_poll_thread_stop(void)
1831 mutex_lock(&ap_poll_thread_mutex
);
1832 if (ap_poll_kthread
) {
1833 kthread_stop(ap_poll_kthread
);
1834 ap_poll_kthread
= NULL
;
1836 mutex_unlock(&ap_poll_thread_mutex
);
1840 * ap_request_timeout(): Handling of request timeouts
1841 * @data: Holds the AP device.
1843 * Handles request timeouts.
1845 static void ap_request_timeout(unsigned long data
)
1847 struct ap_device
*ap_dev
= (struct ap_device
*) data
;
1849 if (ap_dev
->reset
== AP_RESET_ARMED
) {
1850 ap_dev
->reset
= AP_RESET_DO
;
1852 if (ap_using_interrupts())
1853 tasklet_schedule(&ap_tasklet
);
1857 static void ap_reset_domain(void)
1861 if (ap_domain_index
!= -1)
1862 for (i
= 0; i
< AP_DEVICES
; i
++)
1863 ap_reset_queue(AP_MKQID(i
, ap_domain_index
));
1866 static void ap_reset_all(void)
1870 for (i
= 0; i
< AP_DOMAINS
; i
++)
1871 for (j
= 0; j
< AP_DEVICES
; j
++)
1872 ap_reset_queue(AP_MKQID(j
, i
));
1875 static struct reset_call ap_reset_call
= {
1880 * ap_module_init(): The module initialization code.
1882 * Initializes the module.
1884 int __init
ap_module_init(void)
1888 if (ap_domain_index
< -1 || ap_domain_index
>= AP_DOMAINS
) {
1889 pr_warning("%d is not a valid cryptographic domain\n",
1893 /* In resume callback we need to know if the user had set the domain.
1894 * If so, we can not just reset it.
1896 if (ap_domain_index
>= 0)
1897 user_set_domain
= 1;
1899 if (ap_instructions_available() != 0) {
1900 pr_warning("The hardware system does not support "
1901 "AP instructions\n");
1904 if (ap_interrupts_available()) {
1905 rc
= register_adapter_interrupt(&ap_airq
);
1906 ap_airq_flag
= (rc
== 0);
1909 register_reset_call(&ap_reset_call
);
1911 /* Create /sys/bus/ap. */
1912 rc
= bus_register(&ap_bus_type
);
1915 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
1916 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1921 /* Create /sys/devices/ap. */
1922 ap_root_device
= root_device_register("ap");
1923 rc
= PTR_RET(ap_root_device
);
1927 ap_work_queue
= create_singlethread_workqueue("kapwork");
1928 if (!ap_work_queue
) {
1933 ap_query_configuration();
1934 if (ap_select_domain() == 0)
1937 /* Setup the AP bus rescan timer. */
1938 init_timer(&ap_config_timer
);
1939 ap_config_timer
.function
= ap_config_timeout
;
1940 ap_config_timer
.data
= 0;
1941 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1942 add_timer(&ap_config_timer
);
1944 /* Setup the high resultion poll timer.
1945 * If we are running under z/VM adjust polling to z/VM polling rate.
1948 poll_timeout
= 1500000;
1949 spin_lock_init(&ap_poll_timer_lock
);
1950 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1951 ap_poll_timer
.function
= ap_poll_timeout
;
1953 /* Start the low priority AP bus poll thread. */
1954 if (ap_thread_flag
) {
1955 rc
= ap_poll_thread_start();
1963 del_timer_sync(&ap_config_timer
);
1964 hrtimer_cancel(&ap_poll_timer
);
1965 destroy_workqueue(ap_work_queue
);
1967 root_device_unregister(ap_root_device
);
1970 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
1971 bus_unregister(&ap_bus_type
);
1973 unregister_reset_call(&ap_reset_call
);
1974 if (ap_using_interrupts())
1975 unregister_adapter_interrupt(&ap_airq
);
1979 static int __ap_match_all(struct device
*dev
, void *data
)
1985 * ap_modules_exit(): The module termination code
1987 * Terminates the module.
1989 void ap_module_exit(void)
1995 ap_poll_thread_stop();
1996 del_timer_sync(&ap_config_timer
);
1997 hrtimer_cancel(&ap_poll_timer
);
1998 destroy_workqueue(ap_work_queue
);
1999 tasklet_kill(&ap_tasklet
);
2000 root_device_unregister(ap_root_device
);
2001 while ((dev
= bus_find_device(&ap_bus_type
, NULL
, NULL
,
2004 device_unregister(dev
);
2007 for (i
= 0; ap_bus_attrs
[i
]; i
++)
2008 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
2009 bus_unregister(&ap_bus_type
);
2010 unregister_reset_call(&ap_reset_call
);
2011 if (ap_using_interrupts())
2012 unregister_adapter_interrupt(&ap_airq
);
2015 module_init(ap_module_init
);
2016 module_exit(ap_module_exit
);