Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / s390 / crypto / ap_bus.c
blobae258a4b4e5ea8866333fec3204dba7513fbda5c
1 /*
2 * Copyright IBM Corp. 2006
3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
6 * Felix Beck <felix.beck@de.ibm.com>
7 * Holger Dengler <hd@linux.vnet.ibm.com>
9 * Adjunct processor bus.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
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>
41 #include <asm/airq.h>
42 #include <linux/atomic.h>
43 #include <asm/isc.h>
44 #include <linux/hrtimer.h>
45 #include <linux/ktime.h>
46 #include <asm/facility.h>
48 #include "ap_bus.h"
50 /* Some prototypes. */
51 static void ap_scan_bus(struct work_struct *);
52 static void ap_poll_all(unsigned long);
53 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
54 static int ap_poll_thread_start(void);
55 static void ap_poll_thread_stop(void);
56 static void ap_request_timeout(unsigned long);
57 static inline void ap_schedule_poll_timer(void);
58 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
59 static int ap_device_remove(struct device *dev);
60 static int ap_device_probe(struct device *dev);
61 static void ap_interrupt_handler(void *unused1, void *unused2);
62 static void ap_reset(struct ap_device *ap_dev);
63 static void ap_config_timeout(unsigned long ptr);
64 static int ap_select_domain(void);
67 * Module description.
69 MODULE_AUTHOR("IBM Corporation");
70 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
71 "Copyright IBM Corp. 2006");
72 MODULE_LICENSE("GPL");
75 * Module parameter
77 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
78 module_param_named(domain, ap_domain_index, int, 0000);
79 MODULE_PARM_DESC(domain, "domain index for ap devices");
80 EXPORT_SYMBOL(ap_domain_index);
82 static int ap_thread_flag = 0;
83 module_param_named(poll_thread, ap_thread_flag, int, 0000);
84 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
86 static struct device *ap_root_device = NULL;
87 static DEFINE_SPINLOCK(ap_device_list_lock);
88 static LIST_HEAD(ap_device_list);
91 * Workqueue & timer for bus rescan.
93 static struct workqueue_struct *ap_work_queue;
94 static struct timer_list ap_config_timer;
95 static int ap_config_time = AP_CONFIG_TIME;
96 static DECLARE_WORK(ap_config_work, ap_scan_bus);
99 * Tasklet & timer for AP request polling and interrupts
101 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
102 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
103 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
104 static struct task_struct *ap_poll_kthread = NULL;
105 static DEFINE_MUTEX(ap_poll_thread_mutex);
106 static DEFINE_SPINLOCK(ap_poll_timer_lock);
107 static void *ap_interrupt_indicator;
108 static struct hrtimer ap_poll_timer;
109 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
110 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
111 static unsigned long long poll_timeout = 250000;
113 /* Suspend flag */
114 static int ap_suspend_flag;
115 /* Flag to check if domain was set through module parameter domain=. This is
116 * important when supsend and resume is done in a z/VM environment where the
117 * domain might change. */
118 static int user_set_domain = 0;
119 static struct bus_type ap_bus_type;
122 * ap_using_interrupts() - Returns non-zero if interrupt support is
123 * available.
125 static inline int ap_using_interrupts(void)
127 return ap_interrupt_indicator != NULL;
131 * ap_intructions_available() - Test if AP instructions are available.
133 * Returns 0 if the AP instructions are installed.
135 static inline int ap_instructions_available(void)
137 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
138 register unsigned long reg1 asm ("1") = -ENODEV;
139 register unsigned long reg2 asm ("2") = 0UL;
141 asm volatile(
142 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
143 "0: la %1,0\n"
144 "1:\n"
145 EX_TABLE(0b, 1b)
146 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
147 return reg1;
151 * ap_interrupts_available(): Test if AP interrupts are available.
153 * Returns 1 if AP interrupts are available.
155 static int ap_interrupts_available(void)
157 return test_facility(2) && test_facility(65);
161 * ap_test_queue(): Test adjunct processor queue.
162 * @qid: The AP queue number
163 * @queue_depth: Pointer to queue depth value
164 * @device_type: Pointer to device type value
166 * Returns AP queue status structure.
168 static inline struct ap_queue_status
169 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
171 register unsigned long reg0 asm ("0") = qid;
172 register struct ap_queue_status reg1 asm ("1");
173 register unsigned long reg2 asm ("2") = 0UL;
175 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
176 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
177 *device_type = (int) (reg2 >> 24);
178 *queue_depth = (int) (reg2 & 0xff);
179 return reg1;
183 * ap_reset_queue(): Reset adjunct processor queue.
184 * @qid: The AP queue number
186 * Returns AP queue status structure.
188 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
190 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
191 register struct ap_queue_status reg1 asm ("1");
192 register unsigned long reg2 asm ("2") = 0UL;
194 asm volatile(
195 ".long 0xb2af0000" /* PQAP(RAPQ) */
196 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
197 return reg1;
200 #ifdef CONFIG_64BIT
202 * ap_queue_interruption_control(): Enable interruption for a specific AP.
203 * @qid: The AP queue number
204 * @ind: The notification indicator byte
206 * Returns AP queue status.
208 static inline struct ap_queue_status
209 ap_queue_interruption_control(ap_qid_t qid, void *ind)
211 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
212 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
213 register struct ap_queue_status reg1_out asm ("1");
214 register void *reg2 asm ("2") = ind;
215 asm volatile(
216 ".long 0xb2af0000" /* PQAP(AQIC) */
217 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
219 : "cc" );
220 return reg1_out;
222 #endif
224 #ifdef CONFIG_64BIT
225 static inline struct ap_queue_status
226 __ap_query_functions(ap_qid_t qid, unsigned int *functions)
228 register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
229 register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
230 register unsigned long reg2 asm ("2");
232 asm volatile(
233 ".long 0xb2af0000\n" /* PQAP(TAPQ) */
234 "0:\n"
235 EX_TABLE(0b, 0b)
236 : "+d" (reg0), "+d" (reg1), "=d" (reg2)
238 : "cc");
240 *functions = (unsigned int)(reg2 >> 32);
241 return reg1;
243 #endif
246 * ap_query_functions(): Query supported functions.
247 * @qid: The AP queue number
248 * @functions: Pointer to functions field.
250 * Returns
251 * 0 on success.
252 * -ENODEV if queue not valid.
253 * -EBUSY if device busy.
254 * -EINVAL if query function is not supported
256 static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
258 #ifdef CONFIG_64BIT
259 struct ap_queue_status status;
260 int i;
261 status = __ap_query_functions(qid, functions);
263 for (i = 0; i < AP_MAX_RESET; i++) {
264 if (ap_queue_status_invalid_test(&status))
265 return -ENODEV;
267 switch (status.response_code) {
268 case AP_RESPONSE_NORMAL:
269 return 0;
270 case AP_RESPONSE_RESET_IN_PROGRESS:
271 case AP_RESPONSE_BUSY:
272 break;
273 case AP_RESPONSE_Q_NOT_AVAIL:
274 case AP_RESPONSE_DECONFIGURED:
275 case AP_RESPONSE_CHECKSTOPPED:
276 case AP_RESPONSE_INVALID_ADDRESS:
277 return -ENODEV;
278 case AP_RESPONSE_OTHERWISE_CHANGED:
279 break;
280 default:
281 break;
283 if (i < AP_MAX_RESET - 1) {
284 udelay(5);
285 status = __ap_query_functions(qid, functions);
288 return -EBUSY;
289 #else
290 return -EINVAL;
291 #endif
295 * ap_4096_commands_availablen(): Check for availability of 4096 bit RSA
296 * support.
297 * @qid: The AP queue number
299 * Returns 1 if 4096 bit RSA keys are support fo the AP, returns 0 if not.
301 int ap_4096_commands_available(ap_qid_t qid)
303 unsigned int functions;
305 if (ap_query_functions(qid, &functions))
306 return 0;
308 return test_ap_facility(functions, 1) &&
309 test_ap_facility(functions, 2);
311 EXPORT_SYMBOL(ap_4096_commands_available);
314 * ap_queue_enable_interruption(): Enable interruption on an AP.
315 * @qid: The AP queue number
316 * @ind: the notification indicator byte
318 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
319 * on the return value it waits a while and tests the AP queue if interrupts
320 * have been switched on using ap_test_queue().
322 static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
324 #ifdef CONFIG_64BIT
325 struct ap_queue_status status;
326 int t_depth, t_device_type, rc, i;
328 rc = -EBUSY;
329 status = ap_queue_interruption_control(qid, ind);
331 for (i = 0; i < AP_MAX_RESET; i++) {
332 switch (status.response_code) {
333 case AP_RESPONSE_NORMAL:
334 if (status.int_enabled)
335 return 0;
336 break;
337 case AP_RESPONSE_RESET_IN_PROGRESS:
338 case AP_RESPONSE_BUSY:
339 if (i < AP_MAX_RESET - 1) {
340 udelay(5);
341 status = ap_queue_interruption_control(qid,
342 ind);
343 continue;
345 break;
346 case AP_RESPONSE_Q_NOT_AVAIL:
347 case AP_RESPONSE_DECONFIGURED:
348 case AP_RESPONSE_CHECKSTOPPED:
349 case AP_RESPONSE_INVALID_ADDRESS:
350 return -ENODEV;
351 case AP_RESPONSE_OTHERWISE_CHANGED:
352 if (status.int_enabled)
353 return 0;
354 break;
355 default:
356 break;
358 if (i < AP_MAX_RESET - 1) {
359 udelay(5);
360 status = ap_test_queue(qid, &t_depth, &t_device_type);
363 return rc;
364 #else
365 return -EINVAL;
366 #endif
370 * __ap_send(): Send message to adjunct processor queue.
371 * @qid: The AP queue number
372 * @psmid: The program supplied message identifier
373 * @msg: The message text
374 * @length: The message length
375 * @special: Special Bit
377 * Returns AP queue status structure.
378 * Condition code 1 on NQAP can't happen because the L bit is 1.
379 * Condition code 2 on NQAP also means the send is incomplete,
380 * because a segment boundary was reached. The NQAP is repeated.
382 static inline struct ap_queue_status
383 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
384 unsigned int special)
386 typedef struct { char _[length]; } msgblock;
387 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
388 register struct ap_queue_status reg1 asm ("1");
389 register unsigned long reg2 asm ("2") = (unsigned long) msg;
390 register unsigned long reg3 asm ("3") = (unsigned long) length;
391 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
392 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
394 if (special == 1)
395 reg0 |= 0x400000UL;
397 asm volatile (
398 "0: .long 0xb2ad0042\n" /* NQAP */
399 " brc 2,0b"
400 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
401 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
402 : "cc" );
403 return reg1;
406 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
408 struct ap_queue_status status;
410 status = __ap_send(qid, psmid, msg, length, 0);
411 switch (status.response_code) {
412 case AP_RESPONSE_NORMAL:
413 return 0;
414 case AP_RESPONSE_Q_FULL:
415 case AP_RESPONSE_RESET_IN_PROGRESS:
416 return -EBUSY;
417 case AP_RESPONSE_REQ_FAC_NOT_INST:
418 return -EINVAL;
419 default: /* Device is gone. */
420 return -ENODEV;
423 EXPORT_SYMBOL(ap_send);
426 * __ap_recv(): Receive message from adjunct processor queue.
427 * @qid: The AP queue number
428 * @psmid: Pointer to program supplied message identifier
429 * @msg: The message text
430 * @length: The message length
432 * Returns AP queue status structure.
433 * Condition code 1 on DQAP means the receive has taken place
434 * but only partially. The response is incomplete, hence the
435 * DQAP is repeated.
436 * Condition code 2 on DQAP also means the receive is incomplete,
437 * this time because a segment boundary was reached. Again, the
438 * DQAP is repeated.
439 * Note that gpr2 is used by the DQAP instruction to keep track of
440 * any 'residual' length, in case the instruction gets interrupted.
441 * Hence it gets zeroed before the instruction.
443 static inline struct ap_queue_status
444 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
446 typedef struct { char _[length]; } msgblock;
447 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
448 register struct ap_queue_status reg1 asm ("1");
449 register unsigned long reg2 asm("2") = 0UL;
450 register unsigned long reg4 asm("4") = (unsigned long) msg;
451 register unsigned long reg5 asm("5") = (unsigned long) length;
452 register unsigned long reg6 asm("6") = 0UL;
453 register unsigned long reg7 asm("7") = 0UL;
456 asm volatile(
457 "0: .long 0xb2ae0064\n" /* DQAP */
458 " brc 6,0b\n"
459 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
460 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
461 "=m" (*(msgblock *) msg) : : "cc" );
462 *psmid = (((unsigned long long) reg6) << 32) + reg7;
463 return reg1;
466 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
468 struct ap_queue_status status;
470 status = __ap_recv(qid, psmid, msg, length);
471 switch (status.response_code) {
472 case AP_RESPONSE_NORMAL:
473 return 0;
474 case AP_RESPONSE_NO_PENDING_REPLY:
475 if (status.queue_empty)
476 return -ENOENT;
477 return -EBUSY;
478 case AP_RESPONSE_RESET_IN_PROGRESS:
479 return -EBUSY;
480 default:
481 return -ENODEV;
484 EXPORT_SYMBOL(ap_recv);
487 * ap_query_queue(): Check if an AP queue is available.
488 * @qid: The AP queue number
489 * @queue_depth: Pointer to queue depth value
490 * @device_type: Pointer to device type value
492 * The test is repeated for AP_MAX_RESET times.
494 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
496 struct ap_queue_status status;
497 int t_depth, t_device_type, rc, i;
499 rc = -EBUSY;
500 for (i = 0; i < AP_MAX_RESET; i++) {
501 status = ap_test_queue(qid, &t_depth, &t_device_type);
502 switch (status.response_code) {
503 case AP_RESPONSE_NORMAL:
504 *queue_depth = t_depth + 1;
505 *device_type = t_device_type;
506 rc = 0;
507 break;
508 case AP_RESPONSE_Q_NOT_AVAIL:
509 rc = -ENODEV;
510 break;
511 case AP_RESPONSE_RESET_IN_PROGRESS:
512 break;
513 case AP_RESPONSE_DECONFIGURED:
514 rc = -ENODEV;
515 break;
516 case AP_RESPONSE_CHECKSTOPPED:
517 rc = -ENODEV;
518 break;
519 case AP_RESPONSE_INVALID_ADDRESS:
520 rc = -ENODEV;
521 break;
522 case AP_RESPONSE_OTHERWISE_CHANGED:
523 break;
524 case AP_RESPONSE_BUSY:
525 break;
526 default:
527 BUG();
529 if (rc != -EBUSY)
530 break;
531 if (i < AP_MAX_RESET - 1)
532 udelay(5);
534 return rc;
538 * ap_init_queue(): Reset an AP queue.
539 * @qid: The AP queue number
541 * Reset an AP queue and wait for it to become available again.
543 static int ap_init_queue(ap_qid_t qid)
545 struct ap_queue_status status;
546 int rc, dummy, i;
548 rc = -ENODEV;
549 status = ap_reset_queue(qid);
550 for (i = 0; i < AP_MAX_RESET; i++) {
551 switch (status.response_code) {
552 case AP_RESPONSE_NORMAL:
553 if (status.queue_empty)
554 rc = 0;
555 break;
556 case AP_RESPONSE_Q_NOT_AVAIL:
557 case AP_RESPONSE_DECONFIGURED:
558 case AP_RESPONSE_CHECKSTOPPED:
559 i = AP_MAX_RESET; /* return with -ENODEV */
560 break;
561 case AP_RESPONSE_RESET_IN_PROGRESS:
562 rc = -EBUSY;
563 case AP_RESPONSE_BUSY:
564 default:
565 break;
567 if (rc != -ENODEV && rc != -EBUSY)
568 break;
569 if (i < AP_MAX_RESET - 1) {
570 udelay(5);
571 status = ap_test_queue(qid, &dummy, &dummy);
574 if (rc == 0 && ap_using_interrupts()) {
575 rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
576 /* If interruption mode is supported by the machine,
577 * but an AP can not be enabled for interruption then
578 * the AP will be discarded. */
579 if (rc)
580 pr_err("Registering adapter interrupts for "
581 "AP %d failed\n", AP_QID_DEVICE(qid));
583 return rc;
587 * ap_increase_queue_count(): Arm request timeout.
588 * @ap_dev: Pointer to an AP device.
590 * Arm request timeout if an AP device was idle and a new request is submitted.
592 static void ap_increase_queue_count(struct ap_device *ap_dev)
594 int timeout = ap_dev->drv->request_timeout;
596 ap_dev->queue_count++;
597 if (ap_dev->queue_count == 1) {
598 mod_timer(&ap_dev->timeout, jiffies + timeout);
599 ap_dev->reset = AP_RESET_ARMED;
604 * ap_decrease_queue_count(): Decrease queue count.
605 * @ap_dev: Pointer to an AP device.
607 * If AP device is still alive, re-schedule request timeout if there are still
608 * pending requests.
610 static void ap_decrease_queue_count(struct ap_device *ap_dev)
612 int timeout = ap_dev->drv->request_timeout;
614 ap_dev->queue_count--;
615 if (ap_dev->queue_count > 0)
616 mod_timer(&ap_dev->timeout, jiffies + timeout);
617 else
619 * The timeout timer should to be disabled now - since
620 * del_timer_sync() is very expensive, we just tell via the
621 * reset flag to ignore the pending timeout timer.
623 ap_dev->reset = AP_RESET_IGNORE;
627 * AP device related attributes.
629 static ssize_t ap_hwtype_show(struct device *dev,
630 struct device_attribute *attr, char *buf)
632 struct ap_device *ap_dev = to_ap_dev(dev);
633 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
636 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
637 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
638 char *buf)
640 struct ap_device *ap_dev = to_ap_dev(dev);
641 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
644 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
645 static ssize_t ap_request_count_show(struct device *dev,
646 struct device_attribute *attr,
647 char *buf)
649 struct ap_device *ap_dev = to_ap_dev(dev);
650 int rc;
652 spin_lock_bh(&ap_dev->lock);
653 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
654 spin_unlock_bh(&ap_dev->lock);
655 return rc;
658 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
660 static ssize_t ap_modalias_show(struct device *dev,
661 struct device_attribute *attr, char *buf)
663 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
666 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
668 static struct attribute *ap_dev_attrs[] = {
669 &dev_attr_hwtype.attr,
670 &dev_attr_depth.attr,
671 &dev_attr_request_count.attr,
672 &dev_attr_modalias.attr,
673 NULL
675 static struct attribute_group ap_dev_attr_group = {
676 .attrs = ap_dev_attrs
680 * ap_bus_match()
681 * @dev: Pointer to device
682 * @drv: Pointer to device_driver
684 * AP bus driver registration/unregistration.
686 static int ap_bus_match(struct device *dev, struct device_driver *drv)
688 struct ap_device *ap_dev = to_ap_dev(dev);
689 struct ap_driver *ap_drv = to_ap_drv(drv);
690 struct ap_device_id *id;
693 * Compare device type of the device with the list of
694 * supported types of the device_driver.
696 for (id = ap_drv->ids; id->match_flags; id++) {
697 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
698 (id->dev_type != ap_dev->device_type))
699 continue;
700 return 1;
702 return 0;
706 * ap_uevent(): Uevent function for AP devices.
707 * @dev: Pointer to device
708 * @env: Pointer to kobj_uevent_env
710 * It sets up a single environment variable DEV_TYPE which contains the
711 * hardware device type.
713 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
715 struct ap_device *ap_dev = to_ap_dev(dev);
716 int retval = 0;
718 if (!ap_dev)
719 return -ENODEV;
721 /* Set up DEV_TYPE environment variable. */
722 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
723 if (retval)
724 return retval;
726 /* Add MODALIAS= */
727 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
729 return retval;
732 static int ap_bus_suspend(struct device *dev, pm_message_t state)
734 struct ap_device *ap_dev = to_ap_dev(dev);
735 unsigned long flags;
737 if (!ap_suspend_flag) {
738 ap_suspend_flag = 1;
740 /* Disable scanning for devices, thus we do not want to scan
741 * for them after removing.
743 del_timer_sync(&ap_config_timer);
744 if (ap_work_queue != NULL) {
745 destroy_workqueue(ap_work_queue);
746 ap_work_queue = NULL;
749 tasklet_disable(&ap_tasklet);
751 /* Poll on the device until all requests are finished. */
752 do {
753 flags = 0;
754 spin_lock_bh(&ap_dev->lock);
755 __ap_poll_device(ap_dev, &flags);
756 spin_unlock_bh(&ap_dev->lock);
757 } while ((flags & 1) || (flags & 2));
759 spin_lock_bh(&ap_dev->lock);
760 ap_dev->unregistered = 1;
761 spin_unlock_bh(&ap_dev->lock);
763 return 0;
766 static int ap_bus_resume(struct device *dev)
768 int rc = 0;
769 struct ap_device *ap_dev = to_ap_dev(dev);
771 if (ap_suspend_flag) {
772 ap_suspend_flag = 0;
773 if (!ap_interrupts_available())
774 ap_interrupt_indicator = NULL;
775 if (!user_set_domain) {
776 ap_domain_index = -1;
777 ap_select_domain();
779 init_timer(&ap_config_timer);
780 ap_config_timer.function = ap_config_timeout;
781 ap_config_timer.data = 0;
782 ap_config_timer.expires = jiffies + ap_config_time * HZ;
783 add_timer(&ap_config_timer);
784 ap_work_queue = create_singlethread_workqueue("kapwork");
785 if (!ap_work_queue)
786 return -ENOMEM;
787 tasklet_enable(&ap_tasklet);
788 if (!ap_using_interrupts())
789 ap_schedule_poll_timer();
790 else
791 tasklet_schedule(&ap_tasklet);
792 if (ap_thread_flag)
793 rc = ap_poll_thread_start();
795 if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
796 spin_lock_bh(&ap_dev->lock);
797 ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
798 ap_domain_index);
799 spin_unlock_bh(&ap_dev->lock);
801 queue_work(ap_work_queue, &ap_config_work);
803 return rc;
806 static struct bus_type ap_bus_type = {
807 .name = "ap",
808 .match = &ap_bus_match,
809 .uevent = &ap_uevent,
810 .suspend = ap_bus_suspend,
811 .resume = ap_bus_resume
814 static int ap_device_probe(struct device *dev)
816 struct ap_device *ap_dev = to_ap_dev(dev);
817 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
818 int rc;
820 ap_dev->drv = ap_drv;
821 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
822 if (!rc) {
823 spin_lock_bh(&ap_device_list_lock);
824 list_add(&ap_dev->list, &ap_device_list);
825 spin_unlock_bh(&ap_device_list_lock);
827 return rc;
831 * __ap_flush_queue(): Flush requests.
832 * @ap_dev: Pointer to the AP device
834 * Flush all requests from the request/pending queue of an AP device.
836 static void __ap_flush_queue(struct ap_device *ap_dev)
838 struct ap_message *ap_msg, *next;
840 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
841 list_del_init(&ap_msg->list);
842 ap_dev->pendingq_count--;
843 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
845 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
846 list_del_init(&ap_msg->list);
847 ap_dev->requestq_count--;
848 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
852 void ap_flush_queue(struct ap_device *ap_dev)
854 spin_lock_bh(&ap_dev->lock);
855 __ap_flush_queue(ap_dev);
856 spin_unlock_bh(&ap_dev->lock);
858 EXPORT_SYMBOL(ap_flush_queue);
860 static int ap_device_remove(struct device *dev)
862 struct ap_device *ap_dev = to_ap_dev(dev);
863 struct ap_driver *ap_drv = ap_dev->drv;
865 ap_flush_queue(ap_dev);
866 del_timer_sync(&ap_dev->timeout);
867 spin_lock_bh(&ap_device_list_lock);
868 list_del_init(&ap_dev->list);
869 spin_unlock_bh(&ap_device_list_lock);
870 if (ap_drv->remove)
871 ap_drv->remove(ap_dev);
872 spin_lock_bh(&ap_dev->lock);
873 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
874 spin_unlock_bh(&ap_dev->lock);
875 return 0;
878 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
879 char *name)
881 struct device_driver *drv = &ap_drv->driver;
883 drv->bus = &ap_bus_type;
884 drv->probe = ap_device_probe;
885 drv->remove = ap_device_remove;
886 drv->owner = owner;
887 drv->name = name;
888 return driver_register(drv);
890 EXPORT_SYMBOL(ap_driver_register);
892 void ap_driver_unregister(struct ap_driver *ap_drv)
894 driver_unregister(&ap_drv->driver);
896 EXPORT_SYMBOL(ap_driver_unregister);
899 * AP bus attributes.
901 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
903 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
906 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
908 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
910 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
913 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
915 return snprintf(buf, PAGE_SIZE, "%d\n",
916 ap_using_interrupts() ? 1 : 0);
919 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
921 static ssize_t ap_config_time_store(struct bus_type *bus,
922 const char *buf, size_t count)
924 int time;
926 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
927 return -EINVAL;
928 ap_config_time = time;
929 if (!timer_pending(&ap_config_timer) ||
930 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
931 ap_config_timer.expires = jiffies + ap_config_time * HZ;
932 add_timer(&ap_config_timer);
934 return count;
937 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
939 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
941 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
944 static ssize_t ap_poll_thread_store(struct bus_type *bus,
945 const char *buf, size_t count)
947 int flag, rc;
949 if (sscanf(buf, "%d\n", &flag) != 1)
950 return -EINVAL;
951 if (flag) {
952 rc = ap_poll_thread_start();
953 if (rc)
954 return rc;
956 else
957 ap_poll_thread_stop();
958 return count;
961 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
963 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
965 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
968 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
969 size_t count)
971 unsigned long long time;
972 ktime_t hr_time;
974 /* 120 seconds = maximum poll interval */
975 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
976 time > 120000000000ULL)
977 return -EINVAL;
978 poll_timeout = time;
979 hr_time = ktime_set(0, poll_timeout);
981 if (!hrtimer_is_queued(&ap_poll_timer) ||
982 !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
983 hrtimer_set_expires(&ap_poll_timer, hr_time);
984 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
986 return count;
989 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
991 static struct bus_attribute *const ap_bus_attrs[] = {
992 &bus_attr_ap_domain,
993 &bus_attr_config_time,
994 &bus_attr_poll_thread,
995 &bus_attr_ap_interrupts,
996 &bus_attr_poll_timeout,
997 NULL,
1001 * ap_select_domain(): Select an AP domain.
1003 * Pick one of the 16 AP domains.
1005 static int ap_select_domain(void)
1007 int queue_depth, device_type, count, max_count, best_domain;
1008 int rc, i, j;
1011 * We want to use a single domain. Either the one specified with
1012 * the "domain=" parameter or the domain with the maximum number
1013 * of devices.
1015 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
1016 /* Domain has already been selected. */
1017 return 0;
1018 best_domain = -1;
1019 max_count = 0;
1020 for (i = 0; i < AP_DOMAINS; i++) {
1021 count = 0;
1022 for (j = 0; j < AP_DEVICES; j++) {
1023 ap_qid_t qid = AP_MKQID(j, i);
1024 rc = ap_query_queue(qid, &queue_depth, &device_type);
1025 if (rc)
1026 continue;
1027 count++;
1029 if (count > max_count) {
1030 max_count = count;
1031 best_domain = i;
1034 if (best_domain >= 0){
1035 ap_domain_index = best_domain;
1036 return 0;
1038 return -ENODEV;
1042 * ap_probe_device_type(): Find the device type of an AP.
1043 * @ap_dev: pointer to the AP device.
1045 * Find the device type if query queue returned a device type of 0.
1047 static int ap_probe_device_type(struct ap_device *ap_dev)
1049 static unsigned char msg[] = {
1050 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1051 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1052 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1053 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1054 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1055 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1056 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1057 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1058 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1059 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1060 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1061 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1062 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1063 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1064 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1065 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1066 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1067 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1068 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1069 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1070 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1071 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1072 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1073 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1074 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1075 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1076 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1077 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1078 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1079 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1080 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1081 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1082 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1083 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1084 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1085 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1086 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1087 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1088 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1089 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1090 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1091 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1092 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1093 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1094 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1096 struct ap_queue_status status;
1097 unsigned long long psmid;
1098 char *reply;
1099 int rc, i;
1101 reply = (void *) get_zeroed_page(GFP_KERNEL);
1102 if (!reply) {
1103 rc = -ENOMEM;
1104 goto out;
1107 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1108 msg, sizeof(msg), 0);
1109 if (status.response_code != AP_RESPONSE_NORMAL) {
1110 rc = -ENODEV;
1111 goto out_free;
1114 /* Wait for the test message to complete. */
1115 for (i = 0; i < 6; i++) {
1116 mdelay(300);
1117 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1118 if (status.response_code == AP_RESPONSE_NORMAL &&
1119 psmid == 0x0102030405060708ULL)
1120 break;
1122 if (i < 6) {
1123 /* Got an answer. */
1124 if (reply[0] == 0x00 && reply[1] == 0x86)
1125 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1126 else
1127 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1128 rc = 0;
1129 } else
1130 rc = -ENODEV;
1132 out_free:
1133 free_page((unsigned long) reply);
1134 out:
1135 return rc;
1138 static void ap_interrupt_handler(void *unused1, void *unused2)
1140 kstat_cpu(smp_processor_id()).irqs[IOINT_APB]++;
1141 tasklet_schedule(&ap_tasklet);
1145 * __ap_scan_bus(): Scan the AP bus.
1146 * @dev: Pointer to device
1147 * @data: Pointer to data
1149 * Scan the AP bus for new devices.
1151 static int __ap_scan_bus(struct device *dev, void *data)
1153 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1156 static void ap_device_release(struct device *dev)
1158 struct ap_device *ap_dev = to_ap_dev(dev);
1160 kfree(ap_dev);
1163 static void ap_scan_bus(struct work_struct *unused)
1165 struct ap_device *ap_dev;
1166 struct device *dev;
1167 ap_qid_t qid;
1168 int queue_depth, device_type;
1169 unsigned int device_functions;
1170 int rc, i;
1172 if (ap_select_domain() != 0)
1173 return;
1174 for (i = 0; i < AP_DEVICES; i++) {
1175 qid = AP_MKQID(i, ap_domain_index);
1176 dev = bus_find_device(&ap_bus_type, NULL,
1177 (void *)(unsigned long)qid,
1178 __ap_scan_bus);
1179 rc = ap_query_queue(qid, &queue_depth, &device_type);
1180 if (dev) {
1181 if (rc == -EBUSY) {
1182 set_current_state(TASK_UNINTERRUPTIBLE);
1183 schedule_timeout(AP_RESET_TIMEOUT);
1184 rc = ap_query_queue(qid, &queue_depth,
1185 &device_type);
1187 ap_dev = to_ap_dev(dev);
1188 spin_lock_bh(&ap_dev->lock);
1189 if (rc || ap_dev->unregistered) {
1190 spin_unlock_bh(&ap_dev->lock);
1191 if (ap_dev->unregistered)
1192 i--;
1193 device_unregister(dev);
1194 put_device(dev);
1195 continue;
1197 spin_unlock_bh(&ap_dev->lock);
1198 put_device(dev);
1199 continue;
1201 if (rc)
1202 continue;
1203 rc = ap_init_queue(qid);
1204 if (rc)
1205 continue;
1206 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1207 if (!ap_dev)
1208 break;
1209 ap_dev->qid = qid;
1210 ap_dev->queue_depth = queue_depth;
1211 ap_dev->unregistered = 1;
1212 spin_lock_init(&ap_dev->lock);
1213 INIT_LIST_HEAD(&ap_dev->pendingq);
1214 INIT_LIST_HEAD(&ap_dev->requestq);
1215 INIT_LIST_HEAD(&ap_dev->list);
1216 setup_timer(&ap_dev->timeout, ap_request_timeout,
1217 (unsigned long) ap_dev);
1218 switch (device_type) {
1219 case 0:
1220 if (ap_probe_device_type(ap_dev)) {
1221 kfree(ap_dev);
1222 continue;
1224 break;
1225 case 10:
1226 if (ap_query_functions(qid, &device_functions)) {
1227 kfree(ap_dev);
1228 continue;
1230 if (test_ap_facility(device_functions, 3))
1231 ap_dev->device_type = AP_DEVICE_TYPE_CEX3C;
1232 else if (test_ap_facility(device_functions, 4))
1233 ap_dev->device_type = AP_DEVICE_TYPE_CEX3A;
1234 else {
1235 kfree(ap_dev);
1236 continue;
1238 break;
1239 default:
1240 ap_dev->device_type = device_type;
1243 ap_dev->device.bus = &ap_bus_type;
1244 ap_dev->device.parent = ap_root_device;
1245 if (dev_set_name(&ap_dev->device, "card%02x",
1246 AP_QID_DEVICE(ap_dev->qid))) {
1247 kfree(ap_dev);
1248 continue;
1250 ap_dev->device.release = ap_device_release;
1251 rc = device_register(&ap_dev->device);
1252 if (rc) {
1253 put_device(&ap_dev->device);
1254 continue;
1256 /* Add device attributes. */
1257 rc = sysfs_create_group(&ap_dev->device.kobj,
1258 &ap_dev_attr_group);
1259 if (!rc) {
1260 spin_lock_bh(&ap_dev->lock);
1261 ap_dev->unregistered = 0;
1262 spin_unlock_bh(&ap_dev->lock);
1264 else
1265 device_unregister(&ap_dev->device);
1269 static void
1270 ap_config_timeout(unsigned long ptr)
1272 queue_work(ap_work_queue, &ap_config_work);
1273 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1274 add_timer(&ap_config_timer);
1278 * __ap_schedule_poll_timer(): Schedule poll timer.
1280 * Set up the timer to run the poll tasklet
1282 static inline void __ap_schedule_poll_timer(void)
1284 ktime_t hr_time;
1286 spin_lock_bh(&ap_poll_timer_lock);
1287 if (hrtimer_is_queued(&ap_poll_timer) || ap_suspend_flag)
1288 goto out;
1289 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1290 hr_time = ktime_set(0, poll_timeout);
1291 hrtimer_forward_now(&ap_poll_timer, hr_time);
1292 hrtimer_restart(&ap_poll_timer);
1294 out:
1295 spin_unlock_bh(&ap_poll_timer_lock);
1299 * ap_schedule_poll_timer(): Schedule poll timer.
1301 * Set up the timer to run the poll tasklet
1303 static inline void ap_schedule_poll_timer(void)
1305 if (ap_using_interrupts())
1306 return;
1307 __ap_schedule_poll_timer();
1311 * ap_poll_read(): Receive pending reply messages from an AP device.
1312 * @ap_dev: pointer to the AP device
1313 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1314 * required, bit 2^1 is set if the poll timer needs to get armed
1316 * Returns 0 if the device is still present, -ENODEV if not.
1318 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1320 struct ap_queue_status status;
1321 struct ap_message *ap_msg;
1323 if (ap_dev->queue_count <= 0)
1324 return 0;
1325 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1326 ap_dev->reply->message, ap_dev->reply->length);
1327 switch (status.response_code) {
1328 case AP_RESPONSE_NORMAL:
1329 atomic_dec(&ap_poll_requests);
1330 ap_decrease_queue_count(ap_dev);
1331 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1332 if (ap_msg->psmid != ap_dev->reply->psmid)
1333 continue;
1334 list_del_init(&ap_msg->list);
1335 ap_dev->pendingq_count--;
1336 ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
1337 break;
1339 if (ap_dev->queue_count > 0)
1340 *flags |= 1;
1341 break;
1342 case AP_RESPONSE_NO_PENDING_REPLY:
1343 if (status.queue_empty) {
1344 /* The card shouldn't forget requests but who knows. */
1345 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1346 ap_dev->queue_count = 0;
1347 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1348 ap_dev->requestq_count += ap_dev->pendingq_count;
1349 ap_dev->pendingq_count = 0;
1350 } else
1351 *flags |= 2;
1352 break;
1353 default:
1354 return -ENODEV;
1356 return 0;
1360 * ap_poll_write(): Send messages from the request queue to an AP device.
1361 * @ap_dev: pointer to the AP device
1362 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1363 * required, bit 2^1 is set if the poll timer needs to get armed
1365 * Returns 0 if the device is still present, -ENODEV if not.
1367 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1369 struct ap_queue_status status;
1370 struct ap_message *ap_msg;
1372 if (ap_dev->requestq_count <= 0 ||
1373 ap_dev->queue_count >= ap_dev->queue_depth)
1374 return 0;
1375 /* Start the next request on the queue. */
1376 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1377 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1378 ap_msg->message, ap_msg->length, ap_msg->special);
1379 switch (status.response_code) {
1380 case AP_RESPONSE_NORMAL:
1381 atomic_inc(&ap_poll_requests);
1382 ap_increase_queue_count(ap_dev);
1383 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1384 ap_dev->requestq_count--;
1385 ap_dev->pendingq_count++;
1386 if (ap_dev->queue_count < ap_dev->queue_depth &&
1387 ap_dev->requestq_count > 0)
1388 *flags |= 1;
1389 *flags |= 2;
1390 break;
1391 case AP_RESPONSE_RESET_IN_PROGRESS:
1392 __ap_schedule_poll_timer();
1393 case AP_RESPONSE_Q_FULL:
1394 *flags |= 2;
1395 break;
1396 case AP_RESPONSE_MESSAGE_TOO_BIG:
1397 case AP_RESPONSE_REQ_FAC_NOT_INST:
1398 return -EINVAL;
1399 default:
1400 return -ENODEV;
1402 return 0;
1406 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1407 * @ap_dev: pointer to the bus device
1408 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1409 * required, bit 2^1 is set if the poll timer needs to get armed
1411 * Poll AP device for pending replies and send new messages. If either
1412 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1413 * Returns 0.
1415 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1417 int rc;
1419 rc = ap_poll_read(ap_dev, flags);
1420 if (rc)
1421 return rc;
1422 return ap_poll_write(ap_dev, flags);
1426 * __ap_queue_message(): Queue a message to a device.
1427 * @ap_dev: pointer to the AP device
1428 * @ap_msg: the message to be queued
1430 * Queue a message to a device. Returns 0 if successful.
1432 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1434 struct ap_queue_status status;
1436 if (list_empty(&ap_dev->requestq) &&
1437 ap_dev->queue_count < ap_dev->queue_depth) {
1438 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1439 ap_msg->message, ap_msg->length,
1440 ap_msg->special);
1441 switch (status.response_code) {
1442 case AP_RESPONSE_NORMAL:
1443 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1444 atomic_inc(&ap_poll_requests);
1445 ap_dev->pendingq_count++;
1446 ap_increase_queue_count(ap_dev);
1447 ap_dev->total_request_count++;
1448 break;
1449 case AP_RESPONSE_Q_FULL:
1450 case AP_RESPONSE_RESET_IN_PROGRESS:
1451 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1452 ap_dev->requestq_count++;
1453 ap_dev->total_request_count++;
1454 return -EBUSY;
1455 case AP_RESPONSE_REQ_FAC_NOT_INST:
1456 case AP_RESPONSE_MESSAGE_TOO_BIG:
1457 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1458 return -EINVAL;
1459 default: /* Device is gone. */
1460 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1461 return -ENODEV;
1463 } else {
1464 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1465 ap_dev->requestq_count++;
1466 ap_dev->total_request_count++;
1467 return -EBUSY;
1469 ap_schedule_poll_timer();
1470 return 0;
1473 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1475 unsigned long flags;
1476 int rc;
1478 /* For asynchronous message handling a valid receive-callback
1479 * is required. */
1480 BUG_ON(!ap_msg->receive);
1482 spin_lock_bh(&ap_dev->lock);
1483 if (!ap_dev->unregistered) {
1484 /* Make room on the queue by polling for finished requests. */
1485 rc = ap_poll_queue(ap_dev, &flags);
1486 if (!rc)
1487 rc = __ap_queue_message(ap_dev, ap_msg);
1488 if (!rc)
1489 wake_up(&ap_poll_wait);
1490 if (rc == -ENODEV)
1491 ap_dev->unregistered = 1;
1492 } else {
1493 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1494 rc = -ENODEV;
1496 spin_unlock_bh(&ap_dev->lock);
1497 if (rc == -ENODEV)
1498 device_unregister(&ap_dev->device);
1500 EXPORT_SYMBOL(ap_queue_message);
1503 * ap_cancel_message(): Cancel a crypto request.
1504 * @ap_dev: The AP device that has the message queued
1505 * @ap_msg: The message that is to be removed
1507 * Cancel a crypto request. This is done by removing the request
1508 * from the device pending or request queue. Note that the
1509 * request stays on the AP queue. When it finishes the message
1510 * reply will be discarded because the psmid can't be found.
1512 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1514 struct ap_message *tmp;
1516 spin_lock_bh(&ap_dev->lock);
1517 if (!list_empty(&ap_msg->list)) {
1518 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1519 if (tmp->psmid == ap_msg->psmid) {
1520 ap_dev->pendingq_count--;
1521 goto found;
1523 ap_dev->requestq_count--;
1524 found:
1525 list_del_init(&ap_msg->list);
1527 spin_unlock_bh(&ap_dev->lock);
1529 EXPORT_SYMBOL(ap_cancel_message);
1532 * ap_poll_timeout(): AP receive polling for finished AP requests.
1533 * @unused: Unused pointer.
1535 * Schedules the AP tasklet using a high resolution timer.
1537 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1539 tasklet_schedule(&ap_tasklet);
1540 return HRTIMER_NORESTART;
1544 * ap_reset(): Reset a not responding AP device.
1545 * @ap_dev: Pointer to the AP device
1547 * Reset a not responding AP device and move all requests from the
1548 * pending queue to the request queue.
1550 static void ap_reset(struct ap_device *ap_dev)
1552 int rc;
1554 ap_dev->reset = AP_RESET_IGNORE;
1555 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1556 ap_dev->queue_count = 0;
1557 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1558 ap_dev->requestq_count += ap_dev->pendingq_count;
1559 ap_dev->pendingq_count = 0;
1560 rc = ap_init_queue(ap_dev->qid);
1561 if (rc == -ENODEV)
1562 ap_dev->unregistered = 1;
1563 else
1564 __ap_schedule_poll_timer();
1567 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1569 if (!ap_dev->unregistered) {
1570 if (ap_poll_queue(ap_dev, flags))
1571 ap_dev->unregistered = 1;
1572 if (ap_dev->reset == AP_RESET_DO)
1573 ap_reset(ap_dev);
1575 return 0;
1579 * ap_poll_all(): Poll all AP devices.
1580 * @dummy: Unused variable
1582 * Poll all AP devices on the bus in a round robin fashion. Continue
1583 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1584 * of the control flags has been set arm the poll timer.
1586 static void ap_poll_all(unsigned long dummy)
1588 unsigned long flags;
1589 struct ap_device *ap_dev;
1591 /* Reset the indicator if interrupts are used. Thus new interrupts can
1592 * be received. Doing it in the beginning of the tasklet is therefor
1593 * important that no requests on any AP get lost.
1595 if (ap_using_interrupts())
1596 xchg((u8 *)ap_interrupt_indicator, 0);
1597 do {
1598 flags = 0;
1599 spin_lock(&ap_device_list_lock);
1600 list_for_each_entry(ap_dev, &ap_device_list, list) {
1601 spin_lock(&ap_dev->lock);
1602 __ap_poll_device(ap_dev, &flags);
1603 spin_unlock(&ap_dev->lock);
1605 spin_unlock(&ap_device_list_lock);
1606 } while (flags & 1);
1607 if (flags & 2)
1608 ap_schedule_poll_timer();
1612 * ap_poll_thread(): Thread that polls for finished requests.
1613 * @data: Unused pointer
1615 * AP bus poll thread. The purpose of this thread is to poll for
1616 * finished requests in a loop if there is a "free" cpu - that is
1617 * a cpu that doesn't have anything better to do. The polling stops
1618 * as soon as there is another task or if all messages have been
1619 * delivered.
1621 static int ap_poll_thread(void *data)
1623 DECLARE_WAITQUEUE(wait, current);
1624 unsigned long flags;
1625 int requests;
1626 struct ap_device *ap_dev;
1628 set_user_nice(current, 19);
1629 while (1) {
1630 if (ap_suspend_flag)
1631 return 0;
1632 if (need_resched()) {
1633 schedule();
1634 continue;
1636 add_wait_queue(&ap_poll_wait, &wait);
1637 set_current_state(TASK_INTERRUPTIBLE);
1638 if (kthread_should_stop())
1639 break;
1640 requests = atomic_read(&ap_poll_requests);
1641 if (requests <= 0)
1642 schedule();
1643 set_current_state(TASK_RUNNING);
1644 remove_wait_queue(&ap_poll_wait, &wait);
1646 flags = 0;
1647 spin_lock_bh(&ap_device_list_lock);
1648 list_for_each_entry(ap_dev, &ap_device_list, list) {
1649 spin_lock(&ap_dev->lock);
1650 __ap_poll_device(ap_dev, &flags);
1651 spin_unlock(&ap_dev->lock);
1653 spin_unlock_bh(&ap_device_list_lock);
1655 set_current_state(TASK_RUNNING);
1656 remove_wait_queue(&ap_poll_wait, &wait);
1657 return 0;
1660 static int ap_poll_thread_start(void)
1662 int rc;
1664 if (ap_using_interrupts() || ap_suspend_flag)
1665 return 0;
1666 mutex_lock(&ap_poll_thread_mutex);
1667 if (!ap_poll_kthread) {
1668 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1669 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1670 if (rc)
1671 ap_poll_kthread = NULL;
1673 else
1674 rc = 0;
1675 mutex_unlock(&ap_poll_thread_mutex);
1676 return rc;
1679 static void ap_poll_thread_stop(void)
1681 mutex_lock(&ap_poll_thread_mutex);
1682 if (ap_poll_kthread) {
1683 kthread_stop(ap_poll_kthread);
1684 ap_poll_kthread = NULL;
1686 mutex_unlock(&ap_poll_thread_mutex);
1690 * ap_request_timeout(): Handling of request timeouts
1691 * @data: Holds the AP device.
1693 * Handles request timeouts.
1695 static void ap_request_timeout(unsigned long data)
1697 struct ap_device *ap_dev = (struct ap_device *) data;
1699 if (ap_dev->reset == AP_RESET_ARMED) {
1700 ap_dev->reset = AP_RESET_DO;
1702 if (ap_using_interrupts())
1703 tasklet_schedule(&ap_tasklet);
1707 static void ap_reset_domain(void)
1709 int i;
1711 if (ap_domain_index != -1)
1712 for (i = 0; i < AP_DEVICES; i++)
1713 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1716 static void ap_reset_all(void)
1718 int i, j;
1720 for (i = 0; i < AP_DOMAINS; i++)
1721 for (j = 0; j < AP_DEVICES; j++)
1722 ap_reset_queue(AP_MKQID(j, i));
1725 static struct reset_call ap_reset_call = {
1726 .fn = ap_reset_all,
1730 * ap_module_init(): The module initialization code.
1732 * Initializes the module.
1734 int __init ap_module_init(void)
1736 int rc, i;
1738 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1739 pr_warning("%d is not a valid cryptographic domain\n",
1740 ap_domain_index);
1741 return -EINVAL;
1743 /* In resume callback we need to know if the user had set the domain.
1744 * If so, we can not just reset it.
1746 if (ap_domain_index >= 0)
1747 user_set_domain = 1;
1749 if (ap_instructions_available() != 0) {
1750 pr_warning("The hardware system does not support "
1751 "AP instructions\n");
1752 return -ENODEV;
1754 if (ap_interrupts_available()) {
1755 isc_register(AP_ISC);
1756 ap_interrupt_indicator = s390_register_adapter_interrupt(
1757 &ap_interrupt_handler, NULL, AP_ISC);
1758 if (IS_ERR(ap_interrupt_indicator)) {
1759 ap_interrupt_indicator = NULL;
1760 isc_unregister(AP_ISC);
1764 register_reset_call(&ap_reset_call);
1766 /* Create /sys/bus/ap. */
1767 rc = bus_register(&ap_bus_type);
1768 if (rc)
1769 goto out;
1770 for (i = 0; ap_bus_attrs[i]; i++) {
1771 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1772 if (rc)
1773 goto out_bus;
1776 /* Create /sys/devices/ap. */
1777 ap_root_device = root_device_register("ap");
1778 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1779 if (rc)
1780 goto out_bus;
1782 ap_work_queue = create_singlethread_workqueue("kapwork");
1783 if (!ap_work_queue) {
1784 rc = -ENOMEM;
1785 goto out_root;
1788 if (ap_select_domain() == 0)
1789 ap_scan_bus(NULL);
1791 /* Setup the AP bus rescan timer. */
1792 init_timer(&ap_config_timer);
1793 ap_config_timer.function = ap_config_timeout;
1794 ap_config_timer.data = 0;
1795 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1796 add_timer(&ap_config_timer);
1798 /* Setup the high resultion poll timer.
1799 * If we are running under z/VM adjust polling to z/VM polling rate.
1801 if (MACHINE_IS_VM)
1802 poll_timeout = 1500000;
1803 spin_lock_init(&ap_poll_timer_lock);
1804 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1805 ap_poll_timer.function = ap_poll_timeout;
1807 /* Start the low priority AP bus poll thread. */
1808 if (ap_thread_flag) {
1809 rc = ap_poll_thread_start();
1810 if (rc)
1811 goto out_work;
1814 return 0;
1816 out_work:
1817 del_timer_sync(&ap_config_timer);
1818 hrtimer_cancel(&ap_poll_timer);
1819 destroy_workqueue(ap_work_queue);
1820 out_root:
1821 root_device_unregister(ap_root_device);
1822 out_bus:
1823 while (i--)
1824 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1825 bus_unregister(&ap_bus_type);
1826 out:
1827 unregister_reset_call(&ap_reset_call);
1828 if (ap_using_interrupts()) {
1829 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1830 isc_unregister(AP_ISC);
1832 return rc;
1835 static int __ap_match_all(struct device *dev, void *data)
1837 return 1;
1841 * ap_modules_exit(): The module termination code
1843 * Terminates the module.
1845 void ap_module_exit(void)
1847 int i;
1848 struct device *dev;
1850 ap_reset_domain();
1851 ap_poll_thread_stop();
1852 del_timer_sync(&ap_config_timer);
1853 hrtimer_cancel(&ap_poll_timer);
1854 destroy_workqueue(ap_work_queue);
1855 tasklet_kill(&ap_tasklet);
1856 root_device_unregister(ap_root_device);
1857 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1858 __ap_match_all)))
1860 device_unregister(dev);
1861 put_device(dev);
1863 for (i = 0; ap_bus_attrs[i]; i++)
1864 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1865 bus_unregister(&ap_bus_type);
1866 unregister_reset_call(&ap_reset_call);
1867 if (ap_using_interrupts()) {
1868 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1869 isc_unregister(AP_ISC);
1873 module_init(ap_module_init);
1874 module_exit(ap_module_exit);