2 * Copyright IBM Corp. 2006, 2012
3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
6 * Felix Beck <felix.beck@de.ibm.com>
7 * Holger Dengler <hd@linux.vnet.ibm.com>
9 * Adjunct processor bus.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/kernel_stat.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <asm/reset.h>
42 #include <linux/atomic.h>
44 #include <linux/hrtimer.h>
45 #include <linux/ktime.h>
46 #include <asm/facility.h>
47 #include <linux/crypto.h>
51 /* Some prototypes. */
52 static void ap_scan_bus(struct work_struct
*);
53 static void ap_poll_all(unsigned long);
54 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*);
55 static int ap_poll_thread_start(void);
56 static void ap_poll_thread_stop(void);
57 static void ap_request_timeout(unsigned long);
58 static inline void ap_schedule_poll_timer(void);
59 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
);
60 static int ap_device_remove(struct device
*dev
);
61 static int ap_device_probe(struct device
*dev
);
62 static void ap_interrupt_handler(struct airq_struct
*airq
);
63 static void ap_reset(struct ap_device
*ap_dev
);
64 static void ap_config_timeout(unsigned long ptr
);
65 static int ap_select_domain(void);
66 static void ap_query_configuration(void);
71 MODULE_AUTHOR("IBM Corporation");
72 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
73 "Copyright IBM Corp. 2006, 2012");
74 MODULE_LICENSE("GPL");
75 MODULE_ALIAS_CRYPTO("z90crypt");
80 int ap_domain_index
= -1; /* Adjunct Processor Domain Index */
81 module_param_named(domain
, ap_domain_index
, int, S_IRUSR
|S_IRGRP
);
82 MODULE_PARM_DESC(domain
, "domain index for ap devices");
83 EXPORT_SYMBOL(ap_domain_index
);
85 static int ap_thread_flag
= 0;
86 module_param_named(poll_thread
, ap_thread_flag
, int, S_IRUSR
|S_IRGRP
);
87 MODULE_PARM_DESC(poll_thread
, "Turn on/off poll thread, default is 0 (off).");
89 static struct device
*ap_root_device
= NULL
;
90 static struct ap_config_info
*ap_configuration
;
91 static DEFINE_SPINLOCK(ap_device_list_lock
);
92 static LIST_HEAD(ap_device_list
);
95 * Workqueue & timer for bus rescan.
97 static struct workqueue_struct
*ap_work_queue
;
98 static struct timer_list ap_config_timer
;
99 static int ap_config_time
= AP_CONFIG_TIME
;
100 static DECLARE_WORK(ap_config_work
, ap_scan_bus
);
103 * Tasklet & timer for AP request polling and interrupts
105 static DECLARE_TASKLET(ap_tasklet
, ap_poll_all
, 0);
106 static atomic_t ap_poll_requests
= ATOMIC_INIT(0);
107 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait
);
108 static struct task_struct
*ap_poll_kthread
= NULL
;
109 static DEFINE_MUTEX(ap_poll_thread_mutex
);
110 static DEFINE_SPINLOCK(ap_poll_timer_lock
);
111 static struct hrtimer ap_poll_timer
;
112 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
113 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
114 static unsigned long long poll_timeout
= 250000;
117 static int ap_suspend_flag
;
118 /* Flag to check if domain was set through module parameter domain=. This is
119 * important when supsend and resume is done in a z/VM environment where the
120 * domain might change. */
121 static int user_set_domain
= 0;
122 static struct bus_type ap_bus_type
;
124 /* Adapter interrupt definitions */
125 static int ap_airq_flag
;
127 static struct airq_struct ap_airq
= {
128 .handler
= ap_interrupt_handler
,
133 * ap_using_interrupts() - Returns non-zero if interrupt support is
136 static inline int ap_using_interrupts(void)
142 * ap_intructions_available() - Test if AP instructions are available.
144 * Returns 0 if the AP instructions are installed.
146 static inline int ap_instructions_available(void)
148 register unsigned long reg0
asm ("0") = AP_MKQID(0,0);
149 register unsigned long reg1
asm ("1") = -ENODEV
;
150 register unsigned long reg2
asm ("2") = 0UL;
153 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
157 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
) : : "cc" );
162 * ap_interrupts_available(): Test if AP interrupts are available.
164 * Returns 1 if AP interrupts are available.
166 static int ap_interrupts_available(void)
168 return test_facility(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_query_facilities(): PQAP(TAPQ) query facilities.
208 * @qid: The AP queue number
210 * Returns content of general register 2 after the PQAP(TAPQ)
211 * instruction was called.
213 static inline unsigned long ap_query_facilities(ap_qid_t qid
)
215 register unsigned long reg0
asm ("0") = qid
| 0x00800000UL
;
216 register unsigned long reg1
asm ("1");
217 register unsigned long reg2
asm ("2") = 0UL;
219 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
220 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
225 * ap_reset_queue(): Reset adjunct processor queue.
226 * @qid: The AP queue number
228 * Returns AP queue status structure.
230 static inline struct ap_queue_status
ap_reset_queue(ap_qid_t qid
)
232 register unsigned long reg0
asm ("0") = qid
| 0x01000000UL
;
233 register struct ap_queue_status reg1
asm ("1");
234 register unsigned long reg2
asm ("2") = 0UL;
237 ".long 0xb2af0000" /* PQAP(RAPQ) */
238 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
) : : "cc");
244 * ap_queue_interruption_control(): Enable interruption for a specific AP.
245 * @qid: The AP queue number
246 * @ind: The notification indicator byte
248 * Returns AP queue status.
250 static inline struct ap_queue_status
251 ap_queue_interruption_control(ap_qid_t qid
, void *ind
)
253 register unsigned long reg0
asm ("0") = qid
| 0x03000000UL
;
254 register unsigned long reg1_in
asm ("1") = 0x0000800000000000UL
| AP_ISC
;
255 register struct ap_queue_status reg1_out
asm ("1");
256 register void *reg2
asm ("2") = ind
;
258 ".long 0xb2af0000" /* PQAP(AQIC) */
259 : "+d" (reg0
), "+d" (reg1_in
), "=d" (reg1_out
), "+d" (reg2
)
267 static inline struct ap_queue_status
268 __ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
270 register unsigned long reg0
asm ("0") = 0UL | qid
| (1UL << 23);
271 register struct ap_queue_status reg1
asm ("1") = AP_QUEUE_STATUS_INVALID
;
272 register unsigned long reg2
asm ("2");
275 ".long 0xb2af0000\n" /* PQAP(TAPQ) */
278 : "+d" (reg0
), "+d" (reg1
), "=d" (reg2
)
282 *functions
= (unsigned int)(reg2
>> 32);
288 static inline int __ap_query_configuration(struct ap_config_info
*config
)
290 register unsigned long reg0
asm ("0") = 0x04000000UL
;
291 register unsigned long reg1
asm ("1") = -EINVAL
;
292 register unsigned char *reg2
asm ("2") = (unsigned char *)config
;
295 ".long 0xb2af0000\n" /* PQAP(QCI) */
299 : "+d" (reg0
), "+d" (reg1
), "+d" (reg2
)
308 * ap_query_functions(): Query supported functions.
309 * @qid: The AP queue number
310 * @functions: Pointer to functions field.
314 * -ENODEV if queue not valid.
315 * -EBUSY if device busy.
316 * -EINVAL if query function is not supported
318 static int ap_query_functions(ap_qid_t qid
, unsigned int *functions
)
321 struct ap_queue_status status
;
323 status
= __ap_query_functions(qid
, functions
);
325 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
326 if (ap_queue_status_invalid_test(&status
))
329 switch (status
.response_code
) {
330 case AP_RESPONSE_NORMAL
:
332 case AP_RESPONSE_RESET_IN_PROGRESS
:
333 case AP_RESPONSE_BUSY
:
335 case AP_RESPONSE_Q_NOT_AVAIL
:
336 case AP_RESPONSE_DECONFIGURED
:
337 case AP_RESPONSE_CHECKSTOPPED
:
338 case AP_RESPONSE_INVALID_ADDRESS
:
340 case AP_RESPONSE_OTHERWISE_CHANGED
:
345 if (i
< AP_MAX_RESET
- 1) {
347 status
= __ap_query_functions(qid
, functions
);
357 * ap_queue_enable_interruption(): Enable interruption on an AP.
358 * @qid: The AP queue number
359 * @ind: the notification indicator byte
361 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
362 * on the return value it waits a while and tests the AP queue if interrupts
363 * have been switched on using ap_test_queue().
365 static int ap_queue_enable_interruption(ap_qid_t qid
, void *ind
)
368 struct ap_queue_status status
;
369 int t_depth
, t_device_type
, rc
, i
;
372 status
= ap_queue_interruption_control(qid
, ind
);
374 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
375 switch (status
.response_code
) {
376 case AP_RESPONSE_NORMAL
:
377 if (status
.int_enabled
)
380 case AP_RESPONSE_RESET_IN_PROGRESS
:
381 case AP_RESPONSE_BUSY
:
382 if (i
< AP_MAX_RESET
- 1) {
384 status
= ap_queue_interruption_control(qid
,
389 case AP_RESPONSE_Q_NOT_AVAIL
:
390 case AP_RESPONSE_DECONFIGURED
:
391 case AP_RESPONSE_CHECKSTOPPED
:
392 case AP_RESPONSE_INVALID_ADDRESS
:
394 case AP_RESPONSE_OTHERWISE_CHANGED
:
395 if (status
.int_enabled
)
401 if (i
< AP_MAX_RESET
- 1) {
403 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
413 * __ap_send(): Send message to adjunct processor queue.
414 * @qid: The AP queue number
415 * @psmid: The program supplied message identifier
416 * @msg: The message text
417 * @length: The message length
418 * @special: Special Bit
420 * Returns AP queue status structure.
421 * Condition code 1 on NQAP can't happen because the L bit is 1.
422 * Condition code 2 on NQAP also means the send is incomplete,
423 * because a segment boundary was reached. The NQAP is repeated.
425 static inline struct ap_queue_status
426 __ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
,
427 unsigned int special
)
429 typedef struct { char _
[length
]; } msgblock
;
430 register unsigned long reg0
asm ("0") = qid
| 0x40000000UL
;
431 register struct ap_queue_status reg1
asm ("1");
432 register unsigned long reg2
asm ("2") = (unsigned long) msg
;
433 register unsigned long reg3
asm ("3") = (unsigned long) length
;
434 register unsigned long reg4
asm ("4") = (unsigned int) (psmid
>> 32);
435 register unsigned long reg5
asm ("5") = psmid
& 0xffffffff;
441 "0: .long 0xb2ad0042\n" /* NQAP */
443 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
), "+d" (reg3
)
444 : "d" (reg4
), "d" (reg5
), "m" (*(msgblock
*) msg
)
449 int ap_send(ap_qid_t qid
, unsigned long long psmid
, void *msg
, size_t length
)
451 struct ap_queue_status status
;
453 status
= __ap_send(qid
, psmid
, msg
, length
, 0);
454 switch (status
.response_code
) {
455 case AP_RESPONSE_NORMAL
:
457 case AP_RESPONSE_Q_FULL
:
458 case AP_RESPONSE_RESET_IN_PROGRESS
:
460 case AP_RESPONSE_REQ_FAC_NOT_INST
:
462 default: /* Device is gone. */
466 EXPORT_SYMBOL(ap_send
);
469 * __ap_recv(): Receive message from adjunct processor queue.
470 * @qid: The AP queue number
471 * @psmid: Pointer to program supplied message identifier
472 * @msg: The message text
473 * @length: The message length
475 * Returns AP queue status structure.
476 * Condition code 1 on DQAP means the receive has taken place
477 * but only partially. The response is incomplete, hence the
479 * Condition code 2 on DQAP also means the receive is incomplete,
480 * this time because a segment boundary was reached. Again, the
482 * Note that gpr2 is used by the DQAP instruction to keep track of
483 * any 'residual' length, in case the instruction gets interrupted.
484 * Hence it gets zeroed before the instruction.
486 static inline struct ap_queue_status
487 __ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
489 typedef struct { char _
[length
]; } msgblock
;
490 register unsigned long reg0
asm("0") = qid
| 0x80000000UL
;
491 register struct ap_queue_status reg1
asm ("1");
492 register unsigned long reg2
asm("2") = 0UL;
493 register unsigned long reg4
asm("4") = (unsigned long) msg
;
494 register unsigned long reg5
asm("5") = (unsigned long) length
;
495 register unsigned long reg6
asm("6") = 0UL;
496 register unsigned long reg7
asm("7") = 0UL;
500 "0: .long 0xb2ae0064\n" /* DQAP */
502 : "+d" (reg0
), "=d" (reg1
), "+d" (reg2
),
503 "+d" (reg4
), "+d" (reg5
), "+d" (reg6
), "+d" (reg7
),
504 "=m" (*(msgblock
*) msg
) : : "cc" );
505 *psmid
= (((unsigned long long) reg6
) << 32) + reg7
;
509 int ap_recv(ap_qid_t qid
, unsigned long long *psmid
, void *msg
, size_t length
)
511 struct ap_queue_status status
;
513 status
= __ap_recv(qid
, psmid
, msg
, length
);
514 switch (status
.response_code
) {
515 case AP_RESPONSE_NORMAL
:
517 case AP_RESPONSE_NO_PENDING_REPLY
:
518 if (status
.queue_empty
)
521 case AP_RESPONSE_RESET_IN_PROGRESS
:
527 EXPORT_SYMBOL(ap_recv
);
530 * ap_query_queue(): Check if an AP queue is available.
531 * @qid: The AP queue number
532 * @queue_depth: Pointer to queue depth value
533 * @device_type: Pointer to device type value
535 * The test is repeated for AP_MAX_RESET times.
537 static int ap_query_queue(ap_qid_t qid
, int *queue_depth
, int *device_type
)
539 struct ap_queue_status status
;
540 int t_depth
, t_device_type
, rc
, i
;
543 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
544 status
= ap_test_queue(qid
, &t_depth
, &t_device_type
);
545 switch (status
.response_code
) {
546 case AP_RESPONSE_NORMAL
:
547 *queue_depth
= t_depth
+ 1;
548 *device_type
= t_device_type
;
551 case AP_RESPONSE_Q_NOT_AVAIL
:
554 case AP_RESPONSE_RESET_IN_PROGRESS
:
556 case AP_RESPONSE_DECONFIGURED
:
559 case AP_RESPONSE_CHECKSTOPPED
:
562 case AP_RESPONSE_INVALID_ADDRESS
:
565 case AP_RESPONSE_OTHERWISE_CHANGED
:
567 case AP_RESPONSE_BUSY
:
574 if (i
< AP_MAX_RESET
- 1)
581 * ap_init_queue(): Reset an AP queue.
582 * @qid: The AP queue number
584 * Reset an AP queue and wait for it to become available again.
586 static int ap_init_queue(ap_qid_t qid
)
588 struct ap_queue_status status
;
592 status
= ap_reset_queue(qid
);
593 for (i
= 0; i
< AP_MAX_RESET
; i
++) {
594 switch (status
.response_code
) {
595 case AP_RESPONSE_NORMAL
:
596 if (status
.queue_empty
)
599 case AP_RESPONSE_Q_NOT_AVAIL
:
600 case AP_RESPONSE_DECONFIGURED
:
601 case AP_RESPONSE_CHECKSTOPPED
:
602 i
= AP_MAX_RESET
; /* return with -ENODEV */
604 case AP_RESPONSE_RESET_IN_PROGRESS
:
606 case AP_RESPONSE_BUSY
:
610 if (rc
!= -ENODEV
&& rc
!= -EBUSY
)
612 if (i
< AP_MAX_RESET
- 1) {
613 /* Time we are waiting until we give up (0.7sec * 90).
614 * Since the actual request (in progress) will not
615 * interrupted immediately for the reset command,
616 * we have to be patient. In worst case we have to
617 * wait 60sec + reset time (some msec).
619 schedule_timeout(AP_RESET_TIMEOUT
);
620 status
= ap_test_queue(qid
, &dummy
, &dummy
);
623 if (rc
== 0 && ap_using_interrupts()) {
624 rc
= ap_queue_enable_interruption(qid
, ap_airq
.lsi_ptr
);
625 /* If interruption mode is supported by the machine,
626 * but an AP can not be enabled for interruption then
627 * the AP will be discarded. */
629 pr_err("Registering adapter interrupts for "
630 "AP %d failed\n", AP_QID_DEVICE(qid
));
636 * ap_increase_queue_count(): Arm request timeout.
637 * @ap_dev: Pointer to an AP device.
639 * Arm request timeout if an AP device was idle and a new request is submitted.
641 static void ap_increase_queue_count(struct ap_device
*ap_dev
)
643 int timeout
= ap_dev
->drv
->request_timeout
;
645 ap_dev
->queue_count
++;
646 if (ap_dev
->queue_count
== 1) {
647 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
648 ap_dev
->reset
= AP_RESET_ARMED
;
653 * ap_decrease_queue_count(): Decrease queue count.
654 * @ap_dev: Pointer to an AP device.
656 * If AP device is still alive, re-schedule request timeout if there are still
659 static void ap_decrease_queue_count(struct ap_device
*ap_dev
)
661 int timeout
= ap_dev
->drv
->request_timeout
;
663 ap_dev
->queue_count
--;
664 if (ap_dev
->queue_count
> 0)
665 mod_timer(&ap_dev
->timeout
, jiffies
+ timeout
);
668 * The timeout timer should to be disabled now - since
669 * del_timer_sync() is very expensive, we just tell via the
670 * reset flag to ignore the pending timeout timer.
672 ap_dev
->reset
= AP_RESET_IGNORE
;
676 * AP device related attributes.
678 static ssize_t
ap_hwtype_show(struct device
*dev
,
679 struct device_attribute
*attr
, char *buf
)
681 struct ap_device
*ap_dev
= to_ap_dev(dev
);
682 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->device_type
);
685 static DEVICE_ATTR(hwtype
, 0444, ap_hwtype_show
, NULL
);
687 static ssize_t
ap_raw_hwtype_show(struct device
*dev
,
688 struct device_attribute
*attr
, char *buf
)
690 struct ap_device
*ap_dev
= to_ap_dev(dev
);
692 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->raw_hwtype
);
695 static DEVICE_ATTR(raw_hwtype
, 0444, ap_raw_hwtype_show
, NULL
);
697 static ssize_t
ap_depth_show(struct device
*dev
, struct device_attribute
*attr
,
700 struct ap_device
*ap_dev
= to_ap_dev(dev
);
701 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->queue_depth
);
704 static DEVICE_ATTR(depth
, 0444, ap_depth_show
, NULL
);
705 static ssize_t
ap_request_count_show(struct device
*dev
,
706 struct device_attribute
*attr
,
709 struct ap_device
*ap_dev
= to_ap_dev(dev
);
712 spin_lock_bh(&ap_dev
->lock
);
713 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->total_request_count
);
714 spin_unlock_bh(&ap_dev
->lock
);
718 static DEVICE_ATTR(request_count
, 0444, ap_request_count_show
, NULL
);
720 static ssize_t
ap_requestq_count_show(struct device
*dev
,
721 struct device_attribute
*attr
, char *buf
)
723 struct ap_device
*ap_dev
= to_ap_dev(dev
);
726 spin_lock_bh(&ap_dev
->lock
);
727 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->requestq_count
);
728 spin_unlock_bh(&ap_dev
->lock
);
732 static DEVICE_ATTR(requestq_count
, 0444, ap_requestq_count_show
, NULL
);
734 static ssize_t
ap_pendingq_count_show(struct device
*dev
,
735 struct device_attribute
*attr
, char *buf
)
737 struct ap_device
*ap_dev
= to_ap_dev(dev
);
740 spin_lock_bh(&ap_dev
->lock
);
741 rc
= snprintf(buf
, PAGE_SIZE
, "%d\n", ap_dev
->pendingq_count
);
742 spin_unlock_bh(&ap_dev
->lock
);
746 static DEVICE_ATTR(pendingq_count
, 0444, ap_pendingq_count_show
, NULL
);
748 static ssize_t
ap_modalias_show(struct device
*dev
,
749 struct device_attribute
*attr
, char *buf
)
751 return sprintf(buf
, "ap:t%02X", to_ap_dev(dev
)->device_type
);
754 static DEVICE_ATTR(modalias
, 0444, ap_modalias_show
, NULL
);
756 static ssize_t
ap_functions_show(struct device
*dev
,
757 struct device_attribute
*attr
, char *buf
)
759 struct ap_device
*ap_dev
= to_ap_dev(dev
);
760 return snprintf(buf
, PAGE_SIZE
, "0x%08X\n", ap_dev
->functions
);
763 static DEVICE_ATTR(ap_functions
, 0444, ap_functions_show
, NULL
);
765 static struct attribute
*ap_dev_attrs
[] = {
766 &dev_attr_hwtype
.attr
,
767 &dev_attr_raw_hwtype
.attr
,
768 &dev_attr_depth
.attr
,
769 &dev_attr_request_count
.attr
,
770 &dev_attr_requestq_count
.attr
,
771 &dev_attr_pendingq_count
.attr
,
772 &dev_attr_modalias
.attr
,
773 &dev_attr_ap_functions
.attr
,
776 static struct attribute_group ap_dev_attr_group
= {
777 .attrs
= ap_dev_attrs
782 * @dev: Pointer to device
783 * @drv: Pointer to device_driver
785 * AP bus driver registration/unregistration.
787 static int ap_bus_match(struct device
*dev
, struct device_driver
*drv
)
789 struct ap_device
*ap_dev
= to_ap_dev(dev
);
790 struct ap_driver
*ap_drv
= to_ap_drv(drv
);
791 struct ap_device_id
*id
;
794 * Compare device type of the device with the list of
795 * supported types of the device_driver.
797 for (id
= ap_drv
->ids
; id
->match_flags
; id
++) {
798 if ((id
->match_flags
& AP_DEVICE_ID_MATCH_DEVICE_TYPE
) &&
799 (id
->dev_type
!= ap_dev
->device_type
))
807 * ap_uevent(): Uevent function for AP devices.
808 * @dev: Pointer to device
809 * @env: Pointer to kobj_uevent_env
811 * It sets up a single environment variable DEV_TYPE which contains the
812 * hardware device type.
814 static int ap_uevent (struct device
*dev
, struct kobj_uevent_env
*env
)
816 struct ap_device
*ap_dev
= to_ap_dev(dev
);
822 /* Set up DEV_TYPE environment variable. */
823 retval
= add_uevent_var(env
, "DEV_TYPE=%04X", ap_dev
->device_type
);
828 retval
= add_uevent_var(env
, "MODALIAS=ap:t%02X", ap_dev
->device_type
);
833 static int ap_bus_suspend(struct device
*dev
, pm_message_t state
)
835 struct ap_device
*ap_dev
= to_ap_dev(dev
);
838 if (!ap_suspend_flag
) {
841 /* Disable scanning for devices, thus we do not want to scan
842 * for them after removing.
844 del_timer_sync(&ap_config_timer
);
845 if (ap_work_queue
!= NULL
) {
846 destroy_workqueue(ap_work_queue
);
847 ap_work_queue
= NULL
;
850 tasklet_disable(&ap_tasklet
);
852 /* Poll on the device until all requests are finished. */
855 spin_lock_bh(&ap_dev
->lock
);
856 __ap_poll_device(ap_dev
, &flags
);
857 spin_unlock_bh(&ap_dev
->lock
);
858 } while ((flags
& 1) || (flags
& 2));
860 spin_lock_bh(&ap_dev
->lock
);
861 ap_dev
->unregistered
= 1;
862 spin_unlock_bh(&ap_dev
->lock
);
867 static int ap_bus_resume(struct device
*dev
)
869 struct ap_device
*ap_dev
= to_ap_dev(dev
);
872 if (ap_suspend_flag
) {
874 if (ap_interrupts_available()) {
875 if (!ap_using_interrupts()) {
876 rc
= register_adapter_interrupt(&ap_airq
);
877 ap_airq_flag
= (rc
== 0);
880 if (ap_using_interrupts()) {
881 unregister_adapter_interrupt(&ap_airq
);
885 ap_query_configuration();
886 if (!user_set_domain
) {
887 ap_domain_index
= -1;
890 init_timer(&ap_config_timer
);
891 ap_config_timer
.function
= ap_config_timeout
;
892 ap_config_timer
.data
= 0;
893 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
894 add_timer(&ap_config_timer
);
895 ap_work_queue
= create_singlethread_workqueue("kapwork");
898 tasklet_enable(&ap_tasklet
);
899 if (!ap_using_interrupts())
900 ap_schedule_poll_timer();
902 tasklet_schedule(&ap_tasklet
);
904 rc
= ap_poll_thread_start();
909 if (AP_QID_QUEUE(ap_dev
->qid
) != ap_domain_index
) {
910 spin_lock_bh(&ap_dev
->lock
);
911 ap_dev
->qid
= AP_MKQID(AP_QID_DEVICE(ap_dev
->qid
),
913 spin_unlock_bh(&ap_dev
->lock
);
915 queue_work(ap_work_queue
, &ap_config_work
);
920 static struct bus_type ap_bus_type
= {
922 .match
= &ap_bus_match
,
923 .uevent
= &ap_uevent
,
924 .suspend
= ap_bus_suspend
,
925 .resume
= ap_bus_resume
928 static int ap_device_probe(struct device
*dev
)
930 struct ap_device
*ap_dev
= to_ap_dev(dev
);
931 struct ap_driver
*ap_drv
= to_ap_drv(dev
->driver
);
934 ap_dev
->drv
= ap_drv
;
936 spin_lock_bh(&ap_device_list_lock
);
937 list_add(&ap_dev
->list
, &ap_device_list
);
938 spin_unlock_bh(&ap_device_list_lock
);
940 rc
= ap_drv
->probe
? ap_drv
->probe(ap_dev
) : -ENODEV
;
942 spin_lock_bh(&ap_device_list_lock
);
943 list_del_init(&ap_dev
->list
);
944 spin_unlock_bh(&ap_device_list_lock
);
950 * __ap_flush_queue(): Flush requests.
951 * @ap_dev: Pointer to the AP device
953 * Flush all requests from the request/pending queue of an AP device.
955 static void __ap_flush_queue(struct ap_device
*ap_dev
)
957 struct ap_message
*ap_msg
, *next
;
959 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->pendingq
, list
) {
960 list_del_init(&ap_msg
->list
);
961 ap_dev
->pendingq_count
--;
962 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
964 list_for_each_entry_safe(ap_msg
, next
, &ap_dev
->requestq
, list
) {
965 list_del_init(&ap_msg
->list
);
966 ap_dev
->requestq_count
--;
967 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
971 void ap_flush_queue(struct ap_device
*ap_dev
)
973 spin_lock_bh(&ap_dev
->lock
);
974 __ap_flush_queue(ap_dev
);
975 spin_unlock_bh(&ap_dev
->lock
);
977 EXPORT_SYMBOL(ap_flush_queue
);
979 static int ap_device_remove(struct device
*dev
)
981 struct ap_device
*ap_dev
= to_ap_dev(dev
);
982 struct ap_driver
*ap_drv
= ap_dev
->drv
;
984 ap_flush_queue(ap_dev
);
985 del_timer_sync(&ap_dev
->timeout
);
986 spin_lock_bh(&ap_device_list_lock
);
987 list_del_init(&ap_dev
->list
);
988 spin_unlock_bh(&ap_device_list_lock
);
990 ap_drv
->remove(ap_dev
);
991 spin_lock_bh(&ap_dev
->lock
);
992 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
993 spin_unlock_bh(&ap_dev
->lock
);
997 int ap_driver_register(struct ap_driver
*ap_drv
, struct module
*owner
,
1000 struct device_driver
*drv
= &ap_drv
->driver
;
1002 drv
->bus
= &ap_bus_type
;
1003 drv
->probe
= ap_device_probe
;
1004 drv
->remove
= ap_device_remove
;
1007 return driver_register(drv
);
1009 EXPORT_SYMBOL(ap_driver_register
);
1011 void ap_driver_unregister(struct ap_driver
*ap_drv
)
1013 driver_unregister(&ap_drv
->driver
);
1015 EXPORT_SYMBOL(ap_driver_unregister
);
1017 void ap_bus_force_rescan(void)
1019 /* reconfigure the AP bus rescan timer. */
1020 mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
);
1021 /* processing a asynchronous bus rescan */
1022 queue_work(ap_work_queue
, &ap_config_work
);
1023 flush_work(&ap_config_work
);
1025 EXPORT_SYMBOL(ap_bus_force_rescan
);
1028 * ap_test_config(): helper function to extract the nrth bit
1029 * within the unsigned int array field.
1031 static inline int ap_test_config(unsigned int *field
, unsigned int nr
)
1035 return ap_test_bit((field
+ (nr
>> 5)), (nr
& 0x1f));
1039 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
1042 * Returns 0 if the card is not configured
1043 * 1 if the card is configured or
1044 * if the configuration information is not available
1046 static inline int ap_test_config_card_id(unsigned int id
)
1048 if (!ap_configuration
)
1050 return ap_test_config(ap_configuration
->apm
, id
);
1054 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
1055 * @domain AP usage domain ID
1057 * Returns 0 if the usage domain is not configured
1058 * 1 if the usage domain is configured or
1059 * if the configuration information is not available
1061 static inline int ap_test_config_domain(unsigned int domain
)
1063 if (!ap_configuration
) /* QCI not supported */
1065 return 1; /* then domains 0...15 are configured */
1069 return ap_test_config(ap_configuration
->aqm
, domain
);
1073 * AP bus attributes.
1075 static ssize_t
ap_domain_show(struct bus_type
*bus
, char *buf
)
1077 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_domain_index
);
1080 static BUS_ATTR(ap_domain
, 0444, ap_domain_show
, NULL
);
1082 static ssize_t
ap_control_domain_mask_show(struct bus_type
*bus
, char *buf
)
1084 if (ap_configuration
!= NULL
) { /* QCI not supported */
1085 if (test_facility(76)) { /* format 1 - 256 bit domain field */
1086 return snprintf(buf
, PAGE_SIZE
,
1087 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1088 ap_configuration
->adm
[0], ap_configuration
->adm
[1],
1089 ap_configuration
->adm
[2], ap_configuration
->adm
[3],
1090 ap_configuration
->adm
[4], ap_configuration
->adm
[5],
1091 ap_configuration
->adm
[6], ap_configuration
->adm
[7]);
1092 } else { /* format 0 - 16 bit domain field */
1093 return snprintf(buf
, PAGE_SIZE
, "%08x%08x\n",
1094 ap_configuration
->adm
[0], ap_configuration
->adm
[1]);
1097 return snprintf(buf
, PAGE_SIZE
, "not supported\n");
1101 static BUS_ATTR(ap_control_domain_mask
, 0444,
1102 ap_control_domain_mask_show
, NULL
);
1104 static ssize_t
ap_config_time_show(struct bus_type
*bus
, char *buf
)
1106 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_config_time
);
1109 static ssize_t
ap_interrupts_show(struct bus_type
*bus
, char *buf
)
1111 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1112 ap_using_interrupts() ? 1 : 0);
1115 static BUS_ATTR(ap_interrupts
, 0444, ap_interrupts_show
, NULL
);
1117 static ssize_t
ap_config_time_store(struct bus_type
*bus
,
1118 const char *buf
, size_t count
)
1122 if (sscanf(buf
, "%d\n", &time
) != 1 || time
< 5 || time
> 120)
1124 ap_config_time
= time
;
1125 if (!timer_pending(&ap_config_timer
) ||
1126 !mod_timer(&ap_config_timer
, jiffies
+ ap_config_time
* HZ
)) {
1127 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1128 add_timer(&ap_config_timer
);
1133 static BUS_ATTR(config_time
, 0644, ap_config_time_show
, ap_config_time_store
);
1135 static ssize_t
ap_poll_thread_show(struct bus_type
*bus
, char *buf
)
1137 return snprintf(buf
, PAGE_SIZE
, "%d\n", ap_poll_kthread
? 1 : 0);
1140 static ssize_t
ap_poll_thread_store(struct bus_type
*bus
,
1141 const char *buf
, size_t count
)
1145 if (sscanf(buf
, "%d\n", &flag
) != 1)
1148 rc
= ap_poll_thread_start();
1153 ap_poll_thread_stop();
1157 static BUS_ATTR(poll_thread
, 0644, ap_poll_thread_show
, ap_poll_thread_store
);
1159 static ssize_t
poll_timeout_show(struct bus_type
*bus
, char *buf
)
1161 return snprintf(buf
, PAGE_SIZE
, "%llu\n", poll_timeout
);
1164 static ssize_t
poll_timeout_store(struct bus_type
*bus
, const char *buf
,
1167 unsigned long long time
;
1170 /* 120 seconds = maximum poll interval */
1171 if (sscanf(buf
, "%llu\n", &time
) != 1 || time
< 1 ||
1172 time
> 120000000000ULL)
1174 poll_timeout
= time
;
1175 hr_time
= ktime_set(0, poll_timeout
);
1177 if (!hrtimer_is_queued(&ap_poll_timer
) ||
1178 !hrtimer_forward(&ap_poll_timer
, hrtimer_get_expires(&ap_poll_timer
), hr_time
)) {
1179 hrtimer_set_expires(&ap_poll_timer
, hr_time
);
1180 hrtimer_start_expires(&ap_poll_timer
, HRTIMER_MODE_ABS
);
1185 static BUS_ATTR(poll_timeout
, 0644, poll_timeout_show
, poll_timeout_store
);
1187 static ssize_t
ap_max_domain_id_show(struct bus_type
*bus
, char *buf
)
1190 int i
, nd
, max_domain_id
= -1;
1191 unsigned long fbits
;
1193 if (ap_configuration
) {
1194 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
) {
1195 for (i
= 0; i
< AP_DEVICES
; i
++) {
1196 if (!ap_test_config_card_id(i
))
1198 qid
= AP_MKQID(i
, ap_domain_index
);
1199 fbits
= ap_query_facilities(qid
);
1200 if (fbits
& (1UL << 57)) {
1201 /* the N bit is 0, Nd field is filled */
1202 nd
= (int)((fbits
& 0x00FF0000UL
)>>16);
1208 /* N bit is 1, max 16 domains */
1215 /* no APXA support, older machines with max 16 domains */
1218 return snprintf(buf
, PAGE_SIZE
, "%d\n", max_domain_id
);
1221 static BUS_ATTR(ap_max_domain_id
, 0444, ap_max_domain_id_show
, NULL
);
1223 static struct bus_attribute
*const ap_bus_attrs
[] = {
1224 &bus_attr_ap_domain
,
1225 &bus_attr_ap_control_domain_mask
,
1226 &bus_attr_config_time
,
1227 &bus_attr_poll_thread
,
1228 &bus_attr_ap_interrupts
,
1229 &bus_attr_poll_timeout
,
1230 &bus_attr_ap_max_domain_id
,
1235 * ap_query_configuration(): Query AP configuration information.
1237 * Query information of installed cards and configured domains from AP.
1239 static void ap_query_configuration(void)
1242 if (ap_configuration_available()) {
1243 if (!ap_configuration
)
1245 kzalloc(sizeof(struct ap_config_info
),
1247 if (ap_configuration
)
1248 __ap_query_configuration(ap_configuration
);
1250 ap_configuration
= NULL
;
1252 ap_configuration
= NULL
;
1257 * ap_select_domain(): Select an AP domain.
1259 * Pick one of the 16 AP domains.
1261 static int ap_select_domain(void)
1263 int queue_depth
, device_type
, count
, max_count
, best_domain
;
1267 /* IF APXA isn't installed, only 16 domains could be defined */
1268 if (!ap_configuration
->ap_extended
&& (ap_domain_index
> 15))
1272 * We want to use a single domain. Either the one specified with
1273 * the "domain=" parameter or the domain with the maximum number
1276 if (ap_domain_index
>= 0 && ap_domain_index
< AP_DOMAINS
)
1277 /* Domain has already been selected. */
1281 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1282 if (!ap_test_config_domain(i
))
1285 for (j
= 0; j
< AP_DEVICES
; j
++) {
1286 if (!ap_test_config_card_id(j
))
1288 qid
= AP_MKQID(j
, i
);
1289 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1294 if (count
> max_count
) {
1299 if (best_domain
>= 0){
1300 ap_domain_index
= best_domain
;
1307 * ap_probe_device_type(): Find the device type of an AP.
1308 * @ap_dev: pointer to the AP device.
1310 * Find the device type if query queue returned a device type of 0.
1312 static int ap_probe_device_type(struct ap_device
*ap_dev
)
1314 static unsigned char msg
[] = {
1315 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1316 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1317 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1318 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1319 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1320 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1321 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1322 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1323 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1324 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1325 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1326 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1327 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1328 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1329 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1330 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1331 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1332 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1333 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1334 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1335 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1336 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1337 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1338 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1339 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1340 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1341 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1342 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1343 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1344 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1345 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1346 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1347 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1348 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1349 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1350 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1351 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1352 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1353 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1354 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1355 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1356 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1357 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1358 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1359 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1361 struct ap_queue_status status
;
1362 unsigned long long psmid
;
1366 reply
= (void *) get_zeroed_page(GFP_KERNEL
);
1372 status
= __ap_send(ap_dev
->qid
, 0x0102030405060708ULL
,
1373 msg
, sizeof(msg
), 0);
1374 if (status
.response_code
!= AP_RESPONSE_NORMAL
) {
1379 /* Wait for the test message to complete. */
1380 for (i
= 0; i
< 6; i
++) {
1382 status
= __ap_recv(ap_dev
->qid
, &psmid
, reply
, 4096);
1383 if (status
.response_code
== AP_RESPONSE_NORMAL
&&
1384 psmid
== 0x0102030405060708ULL
)
1388 /* Got an answer. */
1389 if (reply
[0] == 0x00 && reply
[1] == 0x86)
1390 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICC
;
1392 ap_dev
->device_type
= AP_DEVICE_TYPE_PCICA
;
1398 free_page((unsigned long) reply
);
1403 static void ap_interrupt_handler(struct airq_struct
*airq
)
1405 inc_irq_stat(IRQIO_APB
);
1406 tasklet_schedule(&ap_tasklet
);
1410 * __ap_scan_bus(): Scan the AP bus.
1411 * @dev: Pointer to device
1412 * @data: Pointer to data
1414 * Scan the AP bus for new devices.
1416 static int __ap_scan_bus(struct device
*dev
, void *data
)
1418 return to_ap_dev(dev
)->qid
== (ap_qid_t
)(unsigned long) data
;
1421 static void ap_device_release(struct device
*dev
)
1423 struct ap_device
*ap_dev
= to_ap_dev(dev
);
1428 static void ap_scan_bus(struct work_struct
*unused
)
1430 struct ap_device
*ap_dev
;
1433 int queue_depth
, device_type
;
1434 unsigned int device_functions
;
1437 ap_query_configuration();
1438 if (ap_select_domain() != 0) {
1441 for (i
= 0; i
< AP_DEVICES
; i
++) {
1442 qid
= AP_MKQID(i
, ap_domain_index
);
1443 dev
= bus_find_device(&ap_bus_type
, NULL
,
1444 (void *)(unsigned long)qid
,
1446 if (ap_test_config_card_id(i
))
1447 rc
= ap_query_queue(qid
, &queue_depth
, &device_type
);
1452 set_current_state(TASK_UNINTERRUPTIBLE
);
1453 schedule_timeout(AP_RESET_TIMEOUT
);
1454 rc
= ap_query_queue(qid
, &queue_depth
,
1457 ap_dev
= to_ap_dev(dev
);
1458 spin_lock_bh(&ap_dev
->lock
);
1459 if (rc
|| ap_dev
->unregistered
) {
1460 spin_unlock_bh(&ap_dev
->lock
);
1461 if (ap_dev
->unregistered
)
1463 device_unregister(dev
);
1467 spin_unlock_bh(&ap_dev
->lock
);
1473 rc
= ap_init_queue(qid
);
1476 ap_dev
= kzalloc(sizeof(*ap_dev
), GFP_KERNEL
);
1480 ap_dev
->queue_depth
= queue_depth
;
1481 ap_dev
->unregistered
= 1;
1482 spin_lock_init(&ap_dev
->lock
);
1483 INIT_LIST_HEAD(&ap_dev
->pendingq
);
1484 INIT_LIST_HEAD(&ap_dev
->requestq
);
1485 INIT_LIST_HEAD(&ap_dev
->list
);
1486 setup_timer(&ap_dev
->timeout
, ap_request_timeout
,
1487 (unsigned long) ap_dev
);
1488 switch (device_type
) {
1490 /* device type probing for old cards */
1491 if (ap_probe_device_type(ap_dev
)) {
1497 ap_dev
->device_type
= device_type
;
1499 ap_dev
->raw_hwtype
= device_type
;
1501 rc
= ap_query_functions(qid
, &device_functions
);
1503 ap_dev
->functions
= device_functions
;
1505 ap_dev
->functions
= 0u;
1507 ap_dev
->device
.bus
= &ap_bus_type
;
1508 ap_dev
->device
.parent
= ap_root_device
;
1509 if (dev_set_name(&ap_dev
->device
, "card%02x",
1510 AP_QID_DEVICE(ap_dev
->qid
))) {
1514 ap_dev
->device
.release
= ap_device_release
;
1515 rc
= device_register(&ap_dev
->device
);
1517 put_device(&ap_dev
->device
);
1520 /* Add device attributes. */
1521 rc
= sysfs_create_group(&ap_dev
->device
.kobj
,
1522 &ap_dev_attr_group
);
1524 spin_lock_bh(&ap_dev
->lock
);
1525 ap_dev
->unregistered
= 0;
1526 spin_unlock_bh(&ap_dev
->lock
);
1529 device_unregister(&ap_dev
->device
);
1534 ap_config_timeout(unsigned long ptr
)
1536 queue_work(ap_work_queue
, &ap_config_work
);
1537 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
1538 add_timer(&ap_config_timer
);
1542 * __ap_schedule_poll_timer(): Schedule poll timer.
1544 * Set up the timer to run the poll tasklet
1546 static inline void __ap_schedule_poll_timer(void)
1550 spin_lock_bh(&ap_poll_timer_lock
);
1551 if (hrtimer_is_queued(&ap_poll_timer
) || ap_suspend_flag
)
1553 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer
)) <= 0) {
1554 hr_time
= ktime_set(0, poll_timeout
);
1555 hrtimer_forward_now(&ap_poll_timer
, hr_time
);
1556 hrtimer_restart(&ap_poll_timer
);
1559 spin_unlock_bh(&ap_poll_timer_lock
);
1563 * ap_schedule_poll_timer(): Schedule poll timer.
1565 * Set up the timer to run the poll tasklet
1567 static inline void ap_schedule_poll_timer(void)
1569 if (ap_using_interrupts())
1571 __ap_schedule_poll_timer();
1575 * ap_poll_read(): Receive pending reply messages from an AP device.
1576 * @ap_dev: pointer to the AP device
1577 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1578 * required, bit 2^1 is set if the poll timer needs to get armed
1580 * Returns 0 if the device is still present, -ENODEV if not.
1582 static int ap_poll_read(struct ap_device
*ap_dev
, unsigned long *flags
)
1584 struct ap_queue_status status
;
1585 struct ap_message
*ap_msg
;
1587 if (ap_dev
->queue_count
<= 0)
1589 status
= __ap_recv(ap_dev
->qid
, &ap_dev
->reply
->psmid
,
1590 ap_dev
->reply
->message
, ap_dev
->reply
->length
);
1591 switch (status
.response_code
) {
1592 case AP_RESPONSE_NORMAL
:
1593 atomic_dec(&ap_poll_requests
);
1594 ap_decrease_queue_count(ap_dev
);
1595 list_for_each_entry(ap_msg
, &ap_dev
->pendingq
, list
) {
1596 if (ap_msg
->psmid
!= ap_dev
->reply
->psmid
)
1598 list_del_init(&ap_msg
->list
);
1599 ap_dev
->pendingq_count
--;
1600 ap_msg
->receive(ap_dev
, ap_msg
, ap_dev
->reply
);
1603 if (ap_dev
->queue_count
> 0)
1606 case AP_RESPONSE_NO_PENDING_REPLY
:
1607 if (status
.queue_empty
) {
1608 /* The card shouldn't forget requests but who knows. */
1609 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1610 ap_dev
->queue_count
= 0;
1611 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1612 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1613 ap_dev
->pendingq_count
= 0;
1624 * ap_poll_write(): Send messages from the request queue to an AP device.
1625 * @ap_dev: pointer to the AP device
1626 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1627 * required, bit 2^1 is set if the poll timer needs to get armed
1629 * Returns 0 if the device is still present, -ENODEV if not.
1631 static int ap_poll_write(struct ap_device
*ap_dev
, unsigned long *flags
)
1633 struct ap_queue_status status
;
1634 struct ap_message
*ap_msg
;
1636 if (ap_dev
->requestq_count
<= 0 ||
1637 ap_dev
->queue_count
>= ap_dev
->queue_depth
)
1639 /* Start the next request on the queue. */
1640 ap_msg
= list_entry(ap_dev
->requestq
.next
, struct ap_message
, list
);
1641 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1642 ap_msg
->message
, ap_msg
->length
, ap_msg
->special
);
1643 switch (status
.response_code
) {
1644 case AP_RESPONSE_NORMAL
:
1645 atomic_inc(&ap_poll_requests
);
1646 ap_increase_queue_count(ap_dev
);
1647 list_move_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1648 ap_dev
->requestq_count
--;
1649 ap_dev
->pendingq_count
++;
1650 if (ap_dev
->queue_count
< ap_dev
->queue_depth
&&
1651 ap_dev
->requestq_count
> 0)
1655 case AP_RESPONSE_RESET_IN_PROGRESS
:
1656 __ap_schedule_poll_timer();
1657 case AP_RESPONSE_Q_FULL
:
1660 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1661 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1670 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1671 * @ap_dev: pointer to the bus device
1672 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1673 * required, bit 2^1 is set if the poll timer needs to get armed
1675 * Poll AP device for pending replies and send new messages. If either
1676 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1679 static inline int ap_poll_queue(struct ap_device
*ap_dev
, unsigned long *flags
)
1683 rc
= ap_poll_read(ap_dev
, flags
);
1686 return ap_poll_write(ap_dev
, flags
);
1690 * __ap_queue_message(): Queue a message to a device.
1691 * @ap_dev: pointer to the AP device
1692 * @ap_msg: the message to be queued
1694 * Queue a message to a device. Returns 0 if successful.
1696 static int __ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1698 struct ap_queue_status status
;
1700 if (list_empty(&ap_dev
->requestq
) &&
1701 ap_dev
->queue_count
< ap_dev
->queue_depth
) {
1702 status
= __ap_send(ap_dev
->qid
, ap_msg
->psmid
,
1703 ap_msg
->message
, ap_msg
->length
,
1705 switch (status
.response_code
) {
1706 case AP_RESPONSE_NORMAL
:
1707 list_add_tail(&ap_msg
->list
, &ap_dev
->pendingq
);
1708 atomic_inc(&ap_poll_requests
);
1709 ap_dev
->pendingq_count
++;
1710 ap_increase_queue_count(ap_dev
);
1711 ap_dev
->total_request_count
++;
1713 case AP_RESPONSE_Q_FULL
:
1714 case AP_RESPONSE_RESET_IN_PROGRESS
:
1715 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1716 ap_dev
->requestq_count
++;
1717 ap_dev
->total_request_count
++;
1719 case AP_RESPONSE_REQ_FAC_NOT_INST
:
1720 case AP_RESPONSE_MESSAGE_TOO_BIG
:
1721 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-EINVAL
));
1723 default: /* Device is gone. */
1724 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1728 list_add_tail(&ap_msg
->list
, &ap_dev
->requestq
);
1729 ap_dev
->requestq_count
++;
1730 ap_dev
->total_request_count
++;
1733 ap_schedule_poll_timer();
1737 void ap_queue_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1739 unsigned long flags
;
1742 /* For asynchronous message handling a valid receive-callback
1744 BUG_ON(!ap_msg
->receive
);
1746 spin_lock_bh(&ap_dev
->lock
);
1747 if (!ap_dev
->unregistered
) {
1748 /* Make room on the queue by polling for finished requests. */
1749 rc
= ap_poll_queue(ap_dev
, &flags
);
1751 rc
= __ap_queue_message(ap_dev
, ap_msg
);
1753 wake_up(&ap_poll_wait
);
1755 ap_dev
->unregistered
= 1;
1757 ap_msg
->receive(ap_dev
, ap_msg
, ERR_PTR(-ENODEV
));
1760 spin_unlock_bh(&ap_dev
->lock
);
1762 device_unregister(&ap_dev
->device
);
1764 EXPORT_SYMBOL(ap_queue_message
);
1767 * ap_cancel_message(): Cancel a crypto request.
1768 * @ap_dev: The AP device that has the message queued
1769 * @ap_msg: The message that is to be removed
1771 * Cancel a crypto request. This is done by removing the request
1772 * from the device pending or request queue. Note that the
1773 * request stays on the AP queue. When it finishes the message
1774 * reply will be discarded because the psmid can't be found.
1776 void ap_cancel_message(struct ap_device
*ap_dev
, struct ap_message
*ap_msg
)
1778 struct ap_message
*tmp
;
1780 spin_lock_bh(&ap_dev
->lock
);
1781 if (!list_empty(&ap_msg
->list
)) {
1782 list_for_each_entry(tmp
, &ap_dev
->pendingq
, list
)
1783 if (tmp
->psmid
== ap_msg
->psmid
) {
1784 ap_dev
->pendingq_count
--;
1787 ap_dev
->requestq_count
--;
1789 list_del_init(&ap_msg
->list
);
1791 spin_unlock_bh(&ap_dev
->lock
);
1793 EXPORT_SYMBOL(ap_cancel_message
);
1796 * ap_poll_timeout(): AP receive polling for finished AP requests.
1797 * @unused: Unused pointer.
1799 * Schedules the AP tasklet using a high resolution timer.
1801 static enum hrtimer_restart
ap_poll_timeout(struct hrtimer
*unused
)
1803 tasklet_schedule(&ap_tasklet
);
1804 return HRTIMER_NORESTART
;
1808 * ap_reset(): Reset a not responding AP device.
1809 * @ap_dev: Pointer to the AP device
1811 * Reset a not responding AP device and move all requests from the
1812 * pending queue to the request queue.
1814 static void ap_reset(struct ap_device
*ap_dev
)
1818 ap_dev
->reset
= AP_RESET_IGNORE
;
1819 atomic_sub(ap_dev
->queue_count
, &ap_poll_requests
);
1820 ap_dev
->queue_count
= 0;
1821 list_splice_init(&ap_dev
->pendingq
, &ap_dev
->requestq
);
1822 ap_dev
->requestq_count
+= ap_dev
->pendingq_count
;
1823 ap_dev
->pendingq_count
= 0;
1824 rc
= ap_init_queue(ap_dev
->qid
);
1826 ap_dev
->unregistered
= 1;
1828 __ap_schedule_poll_timer();
1831 static int __ap_poll_device(struct ap_device
*ap_dev
, unsigned long *flags
)
1833 if (!ap_dev
->unregistered
) {
1834 if (ap_poll_queue(ap_dev
, flags
))
1835 ap_dev
->unregistered
= 1;
1836 if (ap_dev
->reset
== AP_RESET_DO
)
1843 * ap_poll_all(): Poll all AP devices.
1844 * @dummy: Unused variable
1846 * Poll all AP devices on the bus in a round robin fashion. Continue
1847 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1848 * of the control flags has been set arm the poll timer.
1850 static void ap_poll_all(unsigned long dummy
)
1852 unsigned long flags
;
1853 struct ap_device
*ap_dev
;
1855 /* Reset the indicator if interrupts are used. Thus new interrupts can
1856 * be received. Doing it in the beginning of the tasklet is therefor
1857 * important that no requests on any AP get lost.
1859 if (ap_using_interrupts())
1860 xchg(ap_airq
.lsi_ptr
, 0);
1863 spin_lock(&ap_device_list_lock
);
1864 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1865 spin_lock(&ap_dev
->lock
);
1866 __ap_poll_device(ap_dev
, &flags
);
1867 spin_unlock(&ap_dev
->lock
);
1869 spin_unlock(&ap_device_list_lock
);
1870 } while (flags
& 1);
1872 ap_schedule_poll_timer();
1876 * ap_poll_thread(): Thread that polls for finished requests.
1877 * @data: Unused pointer
1879 * AP bus poll thread. The purpose of this thread is to poll for
1880 * finished requests in a loop if there is a "free" cpu - that is
1881 * a cpu that doesn't have anything better to do. The polling stops
1882 * as soon as there is another task or if all messages have been
1885 static int ap_poll_thread(void *data
)
1887 DECLARE_WAITQUEUE(wait
, current
);
1888 unsigned long flags
;
1890 struct ap_device
*ap_dev
;
1892 set_user_nice(current
, MAX_NICE
);
1894 if (ap_suspend_flag
)
1896 if (need_resched()) {
1900 add_wait_queue(&ap_poll_wait
, &wait
);
1901 set_current_state(TASK_INTERRUPTIBLE
);
1902 if (kthread_should_stop())
1904 requests
= atomic_read(&ap_poll_requests
);
1907 set_current_state(TASK_RUNNING
);
1908 remove_wait_queue(&ap_poll_wait
, &wait
);
1911 spin_lock_bh(&ap_device_list_lock
);
1912 list_for_each_entry(ap_dev
, &ap_device_list
, list
) {
1913 spin_lock(&ap_dev
->lock
);
1914 __ap_poll_device(ap_dev
, &flags
);
1915 spin_unlock(&ap_dev
->lock
);
1917 spin_unlock_bh(&ap_device_list_lock
);
1919 set_current_state(TASK_RUNNING
);
1920 remove_wait_queue(&ap_poll_wait
, &wait
);
1924 static int ap_poll_thread_start(void)
1928 if (ap_using_interrupts() || ap_suspend_flag
)
1930 mutex_lock(&ap_poll_thread_mutex
);
1931 if (!ap_poll_kthread
) {
1932 ap_poll_kthread
= kthread_run(ap_poll_thread
, NULL
, "appoll");
1933 rc
= PTR_RET(ap_poll_kthread
);
1935 ap_poll_kthread
= NULL
;
1939 mutex_unlock(&ap_poll_thread_mutex
);
1943 static void ap_poll_thread_stop(void)
1945 mutex_lock(&ap_poll_thread_mutex
);
1946 if (ap_poll_kthread
) {
1947 kthread_stop(ap_poll_kthread
);
1948 ap_poll_kthread
= NULL
;
1950 mutex_unlock(&ap_poll_thread_mutex
);
1954 * ap_request_timeout(): Handling of request timeouts
1955 * @data: Holds the AP device.
1957 * Handles request timeouts.
1959 static void ap_request_timeout(unsigned long data
)
1961 struct ap_device
*ap_dev
= (struct ap_device
*) data
;
1963 if (ap_dev
->reset
== AP_RESET_ARMED
) {
1964 ap_dev
->reset
= AP_RESET_DO
;
1966 if (ap_using_interrupts())
1967 tasklet_schedule(&ap_tasklet
);
1971 static void ap_reset_domain(void)
1975 if (ap_domain_index
!= -1)
1976 for (i
= 0; i
< AP_DEVICES
; i
++)
1977 ap_reset_queue(AP_MKQID(i
, ap_domain_index
));
1980 static void ap_reset_all(void)
1984 for (i
= 0; i
< AP_DOMAINS
; i
++) {
1985 if (!ap_test_config_domain(i
))
1987 for (j
= 0; j
< AP_DEVICES
; j
++) {
1988 if (!ap_test_config_card_id(j
))
1990 ap_reset_queue(AP_MKQID(j
, i
));
1995 static struct reset_call ap_reset_call
= {
2000 * ap_module_init(): The module initialization code.
2002 * Initializes the module.
2004 int __init
ap_module_init(void)
2008 if (ap_domain_index
< -1 || ap_domain_index
>= AP_DOMAINS
) {
2009 pr_warning("%d is not a valid cryptographic domain\n",
2013 /* In resume callback we need to know if the user had set the domain.
2014 * If so, we can not just reset it.
2016 if (ap_domain_index
>= 0)
2017 user_set_domain
= 1;
2019 if (ap_instructions_available() != 0) {
2020 pr_warning("The hardware system does not support "
2021 "AP instructions\n");
2024 if (ap_interrupts_available()) {
2025 rc
= register_adapter_interrupt(&ap_airq
);
2026 ap_airq_flag
= (rc
== 0);
2029 register_reset_call(&ap_reset_call
);
2031 /* Create /sys/bus/ap. */
2032 rc
= bus_register(&ap_bus_type
);
2035 for (i
= 0; ap_bus_attrs
[i
]; i
++) {
2036 rc
= bus_create_file(&ap_bus_type
, ap_bus_attrs
[i
]);
2041 /* Create /sys/devices/ap. */
2042 ap_root_device
= root_device_register("ap");
2043 rc
= PTR_RET(ap_root_device
);
2047 ap_work_queue
= create_singlethread_workqueue("kapwork");
2048 if (!ap_work_queue
) {
2053 ap_query_configuration();
2054 if (ap_select_domain() == 0)
2057 /* Setup the AP bus rescan timer. */
2058 init_timer(&ap_config_timer
);
2059 ap_config_timer
.function
= ap_config_timeout
;
2060 ap_config_timer
.data
= 0;
2061 ap_config_timer
.expires
= jiffies
+ ap_config_time
* HZ
;
2062 add_timer(&ap_config_timer
);
2064 /* Setup the high resultion poll timer.
2065 * If we are running under z/VM adjust polling to z/VM polling rate.
2068 poll_timeout
= 1500000;
2069 spin_lock_init(&ap_poll_timer_lock
);
2070 hrtimer_init(&ap_poll_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
2071 ap_poll_timer
.function
= ap_poll_timeout
;
2073 /* Start the low priority AP bus poll thread. */
2074 if (ap_thread_flag
) {
2075 rc
= ap_poll_thread_start();
2083 del_timer_sync(&ap_config_timer
);
2084 hrtimer_cancel(&ap_poll_timer
);
2085 destroy_workqueue(ap_work_queue
);
2087 root_device_unregister(ap_root_device
);
2090 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
2091 bus_unregister(&ap_bus_type
);
2093 unregister_reset_call(&ap_reset_call
);
2094 if (ap_using_interrupts())
2095 unregister_adapter_interrupt(&ap_airq
);
2099 static int __ap_match_all(struct device
*dev
, void *data
)
2105 * ap_modules_exit(): The module termination code
2107 * Terminates the module.
2109 void ap_module_exit(void)
2115 ap_poll_thread_stop();
2116 del_timer_sync(&ap_config_timer
);
2117 hrtimer_cancel(&ap_poll_timer
);
2118 destroy_workqueue(ap_work_queue
);
2119 tasklet_kill(&ap_tasklet
);
2120 root_device_unregister(ap_root_device
);
2121 while ((dev
= bus_find_device(&ap_bus_type
, NULL
, NULL
,
2124 device_unregister(dev
);
2127 for (i
= 0; ap_bus_attrs
[i
]; i
++)
2128 bus_remove_file(&ap_bus_type
, ap_bus_attrs
[i
]);
2129 bus_unregister(&ap_bus_type
);
2130 unregister_reset_call(&ap_reset_call
);
2131 if (ap_using_interrupts())
2132 unregister_adapter_interrupt(&ap_airq
);
2135 module_init(ap_module_init
);
2136 module_exit(ap_module_exit
);