mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / s390 / crypto / ap_bus.c
blob2efa66c6914ec932ff3ca9a594fd16993b5c7943
1 /*
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)
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>
47 #include <linux/crypto.h>
49 #include "ap_bus.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);
69 * Module description.
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");
78 * Module parameter
80 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
81 module_param_named(domain, ap_domain_index, int, 0000);
82 MODULE_PARM_DESC(domain, "domain index for ap devices");
83 EXPORT_SYMBOL(ap_domain_index);
85 static int ap_thread_flag = 0;
86 module_param_named(poll_thread, ap_thread_flag, int, 0000);
87 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
89 static struct device *ap_root_device = NULL;
90 static struct ap_config_info *ap_configuration;
91 static DEFINE_SPINLOCK(ap_device_list_lock);
92 static LIST_HEAD(ap_device_list);
95 * Workqueue & timer for bus rescan.
97 static struct workqueue_struct *ap_work_queue;
98 static struct timer_list ap_config_timer;
99 static int ap_config_time = AP_CONFIG_TIME;
100 static DECLARE_WORK(ap_config_work, ap_scan_bus);
103 * Tasklet & timer for AP request polling and interrupts
105 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
106 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
107 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
108 static struct task_struct *ap_poll_kthread = NULL;
109 static DEFINE_MUTEX(ap_poll_thread_mutex);
110 static DEFINE_SPINLOCK(ap_poll_timer_lock);
111 static struct hrtimer ap_poll_timer;
112 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
113 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
114 static unsigned long long poll_timeout = 250000;
116 /* Suspend flag */
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,
129 .isc = AP_ISC,
133 * ap_using_interrupts() - Returns non-zero if interrupt support is
134 * available.
136 static inline int ap_using_interrupts(void)
138 return ap_airq_flag;
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;
152 asm volatile(
153 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
154 "0: la %1,0\n"
155 "1:\n"
156 EX_TABLE(0b, 1b)
157 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
158 return reg1;
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.
177 #ifdef CONFIG_64BIT
178 static int ap_configuration_available(void)
180 return test_facility(2) && test_facility(12);
182 #endif
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);
203 return reg1;
207 * ap_reset_queue(): Reset adjunct processor queue.
208 * @qid: The AP queue number
210 * Returns AP queue status structure.
212 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
214 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
215 register struct ap_queue_status reg1 asm ("1");
216 register unsigned long reg2 asm ("2") = 0UL;
218 asm volatile(
219 ".long 0xb2af0000" /* PQAP(RAPQ) */
220 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
221 return reg1;
224 #ifdef CONFIG_64BIT
226 * ap_queue_interruption_control(): Enable interruption for a specific AP.
227 * @qid: The AP queue number
228 * @ind: The notification indicator byte
230 * Returns AP queue status.
232 static inline struct ap_queue_status
233 ap_queue_interruption_control(ap_qid_t qid, void *ind)
235 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
236 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
237 register struct ap_queue_status reg1_out asm ("1");
238 register void *reg2 asm ("2") = ind;
239 asm volatile(
240 ".long 0xb2af0000" /* PQAP(AQIC) */
241 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
243 : "cc" );
244 return reg1_out;
246 #endif
248 #ifdef CONFIG_64BIT
249 static inline struct ap_queue_status
250 __ap_query_functions(ap_qid_t qid, unsigned int *functions)
252 register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
253 register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
254 register unsigned long reg2 asm ("2");
256 asm volatile(
257 ".long 0xb2af0000\n" /* PQAP(TAPQ) */
258 "0:\n"
259 EX_TABLE(0b, 0b)
260 : "+d" (reg0), "+d" (reg1), "=d" (reg2)
262 : "cc");
264 *functions = (unsigned int)(reg2 >> 32);
265 return reg1;
267 #endif
269 #ifdef CONFIG_64BIT
270 static inline int __ap_query_configuration(struct ap_config_info *config)
272 register unsigned long reg0 asm ("0") = 0x04000000UL;
273 register unsigned long reg1 asm ("1") = -EINVAL;
274 register unsigned char *reg2 asm ("2") = (unsigned char *)config;
276 asm volatile(
277 ".long 0xb2af0000\n" /* PQAP(QCI) */
278 "0: la %1,0\n"
279 "1:\n"
280 EX_TABLE(0b, 1b)
281 : "+d" (reg0), "+d" (reg1), "+d" (reg2)
283 : "cc");
285 return reg1;
287 #endif
290 * ap_query_functions(): Query supported functions.
291 * @qid: The AP queue number
292 * @functions: Pointer to functions field.
294 * Returns
295 * 0 on success.
296 * -ENODEV if queue not valid.
297 * -EBUSY if device busy.
298 * -EINVAL if query function is not supported
300 static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
302 #ifdef CONFIG_64BIT
303 struct ap_queue_status status;
304 int i;
305 status = __ap_query_functions(qid, functions);
307 for (i = 0; i < AP_MAX_RESET; i++) {
308 if (ap_queue_status_invalid_test(&status))
309 return -ENODEV;
311 switch (status.response_code) {
312 case AP_RESPONSE_NORMAL:
313 return 0;
314 case AP_RESPONSE_RESET_IN_PROGRESS:
315 case AP_RESPONSE_BUSY:
316 break;
317 case AP_RESPONSE_Q_NOT_AVAIL:
318 case AP_RESPONSE_DECONFIGURED:
319 case AP_RESPONSE_CHECKSTOPPED:
320 case AP_RESPONSE_INVALID_ADDRESS:
321 return -ENODEV;
322 case AP_RESPONSE_OTHERWISE_CHANGED:
323 break;
324 default:
325 break;
327 if (i < AP_MAX_RESET - 1) {
328 udelay(5);
329 status = __ap_query_functions(qid, functions);
332 return -EBUSY;
333 #else
334 return -EINVAL;
335 #endif
339 * ap_queue_enable_interruption(): Enable interruption on an AP.
340 * @qid: The AP queue number
341 * @ind: the notification indicator byte
343 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
344 * on the return value it waits a while and tests the AP queue if interrupts
345 * have been switched on using ap_test_queue().
347 static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
349 #ifdef CONFIG_64BIT
350 struct ap_queue_status status;
351 int t_depth, t_device_type, rc, i;
353 rc = -EBUSY;
354 status = ap_queue_interruption_control(qid, ind);
356 for (i = 0; i < AP_MAX_RESET; i++) {
357 switch (status.response_code) {
358 case AP_RESPONSE_NORMAL:
359 if (status.int_enabled)
360 return 0;
361 break;
362 case AP_RESPONSE_RESET_IN_PROGRESS:
363 case AP_RESPONSE_BUSY:
364 if (i < AP_MAX_RESET - 1) {
365 udelay(5);
366 status = ap_queue_interruption_control(qid,
367 ind);
368 continue;
370 break;
371 case AP_RESPONSE_Q_NOT_AVAIL:
372 case AP_RESPONSE_DECONFIGURED:
373 case AP_RESPONSE_CHECKSTOPPED:
374 case AP_RESPONSE_INVALID_ADDRESS:
375 return -ENODEV;
376 case AP_RESPONSE_OTHERWISE_CHANGED:
377 if (status.int_enabled)
378 return 0;
379 break;
380 default:
381 break;
383 if (i < AP_MAX_RESET - 1) {
384 udelay(5);
385 status = ap_test_queue(qid, &t_depth, &t_device_type);
388 return rc;
389 #else
390 return -EINVAL;
391 #endif
395 * __ap_send(): Send message to adjunct processor queue.
396 * @qid: The AP queue number
397 * @psmid: The program supplied message identifier
398 * @msg: The message text
399 * @length: The message length
400 * @special: Special Bit
402 * Returns AP queue status structure.
403 * Condition code 1 on NQAP can't happen because the L bit is 1.
404 * Condition code 2 on NQAP also means the send is incomplete,
405 * because a segment boundary was reached. The NQAP is repeated.
407 static inline struct ap_queue_status
408 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
409 unsigned int special)
411 typedef struct { char _[length]; } msgblock;
412 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
413 register struct ap_queue_status reg1 asm ("1");
414 register unsigned long reg2 asm ("2") = (unsigned long) msg;
415 register unsigned long reg3 asm ("3") = (unsigned long) length;
416 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
417 register unsigned long reg5 asm ("5") = psmid & 0xffffffff;
419 if (special == 1)
420 reg0 |= 0x400000UL;
422 asm volatile (
423 "0: .long 0xb2ad0042\n" /* NQAP */
424 " brc 2,0b"
425 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
426 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
427 : "cc" );
428 return reg1;
431 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
433 struct ap_queue_status status;
435 status = __ap_send(qid, psmid, msg, length, 0);
436 switch (status.response_code) {
437 case AP_RESPONSE_NORMAL:
438 return 0;
439 case AP_RESPONSE_Q_FULL:
440 case AP_RESPONSE_RESET_IN_PROGRESS:
441 return -EBUSY;
442 case AP_RESPONSE_REQ_FAC_NOT_INST:
443 return -EINVAL;
444 default: /* Device is gone. */
445 return -ENODEV;
448 EXPORT_SYMBOL(ap_send);
451 * __ap_recv(): Receive message from adjunct processor queue.
452 * @qid: The AP queue number
453 * @psmid: Pointer to program supplied message identifier
454 * @msg: The message text
455 * @length: The message length
457 * Returns AP queue status structure.
458 * Condition code 1 on DQAP means the receive has taken place
459 * but only partially. The response is incomplete, hence the
460 * DQAP is repeated.
461 * Condition code 2 on DQAP also means the receive is incomplete,
462 * this time because a segment boundary was reached. Again, the
463 * DQAP is repeated.
464 * Note that gpr2 is used by the DQAP instruction to keep track of
465 * any 'residual' length, in case the instruction gets interrupted.
466 * Hence it gets zeroed before the instruction.
468 static inline struct ap_queue_status
469 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
471 typedef struct { char _[length]; } msgblock;
472 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
473 register struct ap_queue_status reg1 asm ("1");
474 register unsigned long reg2 asm("2") = 0UL;
475 register unsigned long reg4 asm("4") = (unsigned long) msg;
476 register unsigned long reg5 asm("5") = (unsigned long) length;
477 register unsigned long reg6 asm("6") = 0UL;
478 register unsigned long reg7 asm("7") = 0UL;
481 asm volatile(
482 "0: .long 0xb2ae0064\n" /* DQAP */
483 " brc 6,0b\n"
484 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
485 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
486 "=m" (*(msgblock *) msg) : : "cc" );
487 *psmid = (((unsigned long long) reg6) << 32) + reg7;
488 return reg1;
491 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
493 struct ap_queue_status status;
495 status = __ap_recv(qid, psmid, msg, length);
496 switch (status.response_code) {
497 case AP_RESPONSE_NORMAL:
498 return 0;
499 case AP_RESPONSE_NO_PENDING_REPLY:
500 if (status.queue_empty)
501 return -ENOENT;
502 return -EBUSY;
503 case AP_RESPONSE_RESET_IN_PROGRESS:
504 return -EBUSY;
505 default:
506 return -ENODEV;
509 EXPORT_SYMBOL(ap_recv);
512 * ap_query_queue(): Check if an AP queue is available.
513 * @qid: The AP queue number
514 * @queue_depth: Pointer to queue depth value
515 * @device_type: Pointer to device type value
517 * The test is repeated for AP_MAX_RESET times.
519 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
521 struct ap_queue_status status;
522 int t_depth, t_device_type, rc, i;
524 rc = -EBUSY;
525 for (i = 0; i < AP_MAX_RESET; i++) {
526 status = ap_test_queue(qid, &t_depth, &t_device_type);
527 switch (status.response_code) {
528 case AP_RESPONSE_NORMAL:
529 *queue_depth = t_depth + 1;
530 *device_type = t_device_type;
531 rc = 0;
532 break;
533 case AP_RESPONSE_Q_NOT_AVAIL:
534 rc = -ENODEV;
535 break;
536 case AP_RESPONSE_RESET_IN_PROGRESS:
537 break;
538 case AP_RESPONSE_DECONFIGURED:
539 rc = -ENODEV;
540 break;
541 case AP_RESPONSE_CHECKSTOPPED:
542 rc = -ENODEV;
543 break;
544 case AP_RESPONSE_INVALID_ADDRESS:
545 rc = -ENODEV;
546 break;
547 case AP_RESPONSE_OTHERWISE_CHANGED:
548 break;
549 case AP_RESPONSE_BUSY:
550 break;
551 default:
552 BUG();
554 if (rc != -EBUSY)
555 break;
556 if (i < AP_MAX_RESET - 1)
557 udelay(5);
559 return rc;
563 * ap_init_queue(): Reset an AP queue.
564 * @qid: The AP queue number
566 * Reset an AP queue and wait for it to become available again.
568 static int ap_init_queue(ap_qid_t qid)
570 struct ap_queue_status status;
571 int rc, dummy, i;
573 rc = -ENODEV;
574 status = ap_reset_queue(qid);
575 for (i = 0; i < AP_MAX_RESET; i++) {
576 switch (status.response_code) {
577 case AP_RESPONSE_NORMAL:
578 if (status.queue_empty)
579 rc = 0;
580 break;
581 case AP_RESPONSE_Q_NOT_AVAIL:
582 case AP_RESPONSE_DECONFIGURED:
583 case AP_RESPONSE_CHECKSTOPPED:
584 i = AP_MAX_RESET; /* return with -ENODEV */
585 break;
586 case AP_RESPONSE_RESET_IN_PROGRESS:
587 rc = -EBUSY;
588 case AP_RESPONSE_BUSY:
589 default:
590 break;
592 if (rc != -ENODEV && rc != -EBUSY)
593 break;
594 if (i < AP_MAX_RESET - 1) {
595 udelay(5);
596 status = ap_test_queue(qid, &dummy, &dummy);
599 if (rc == 0 && ap_using_interrupts()) {
600 rc = ap_queue_enable_interruption(qid, ap_airq.lsi_ptr);
601 /* If interruption mode is supported by the machine,
602 * but an AP can not be enabled for interruption then
603 * the AP will be discarded. */
604 if (rc)
605 pr_err("Registering adapter interrupts for "
606 "AP %d failed\n", AP_QID_DEVICE(qid));
608 return rc;
612 * ap_increase_queue_count(): Arm request timeout.
613 * @ap_dev: Pointer to an AP device.
615 * Arm request timeout if an AP device was idle and a new request is submitted.
617 static void ap_increase_queue_count(struct ap_device *ap_dev)
619 int timeout = ap_dev->drv->request_timeout;
621 ap_dev->queue_count++;
622 if (ap_dev->queue_count == 1) {
623 mod_timer(&ap_dev->timeout, jiffies + timeout);
624 ap_dev->reset = AP_RESET_ARMED;
629 * ap_decrease_queue_count(): Decrease queue count.
630 * @ap_dev: Pointer to an AP device.
632 * If AP device is still alive, re-schedule request timeout if there are still
633 * pending requests.
635 static void ap_decrease_queue_count(struct ap_device *ap_dev)
637 int timeout = ap_dev->drv->request_timeout;
639 ap_dev->queue_count--;
640 if (ap_dev->queue_count > 0)
641 mod_timer(&ap_dev->timeout, jiffies + timeout);
642 else
644 * The timeout timer should to be disabled now - since
645 * del_timer_sync() is very expensive, we just tell via the
646 * reset flag to ignore the pending timeout timer.
648 ap_dev->reset = AP_RESET_IGNORE;
652 * AP device related attributes.
654 static ssize_t ap_hwtype_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
657 struct ap_device *ap_dev = to_ap_dev(dev);
658 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
661 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
662 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
663 char *buf)
665 struct ap_device *ap_dev = to_ap_dev(dev);
666 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
669 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
670 static ssize_t ap_request_count_show(struct device *dev,
671 struct device_attribute *attr,
672 char *buf)
674 struct ap_device *ap_dev = to_ap_dev(dev);
675 int rc;
677 spin_lock_bh(&ap_dev->lock);
678 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
679 spin_unlock_bh(&ap_dev->lock);
680 return rc;
683 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
685 static ssize_t ap_requestq_count_show(struct device *dev,
686 struct device_attribute *attr, char *buf)
688 struct ap_device *ap_dev = to_ap_dev(dev);
689 int rc;
691 spin_lock_bh(&ap_dev->lock);
692 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
693 spin_unlock_bh(&ap_dev->lock);
694 return rc;
697 static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
699 static ssize_t ap_pendingq_count_show(struct device *dev,
700 struct device_attribute *attr, char *buf)
702 struct ap_device *ap_dev = to_ap_dev(dev);
703 int rc;
705 spin_lock_bh(&ap_dev->lock);
706 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
707 spin_unlock_bh(&ap_dev->lock);
708 return rc;
711 static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
713 static ssize_t ap_modalias_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
716 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
719 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
721 static ssize_t ap_functions_show(struct device *dev,
722 struct device_attribute *attr, char *buf)
724 struct ap_device *ap_dev = to_ap_dev(dev);
725 return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
728 static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
730 static struct attribute *ap_dev_attrs[] = {
731 &dev_attr_hwtype.attr,
732 &dev_attr_depth.attr,
733 &dev_attr_request_count.attr,
734 &dev_attr_requestq_count.attr,
735 &dev_attr_pendingq_count.attr,
736 &dev_attr_modalias.attr,
737 &dev_attr_ap_functions.attr,
738 NULL
740 static struct attribute_group ap_dev_attr_group = {
741 .attrs = ap_dev_attrs
745 * ap_bus_match()
746 * @dev: Pointer to device
747 * @drv: Pointer to device_driver
749 * AP bus driver registration/unregistration.
751 static int ap_bus_match(struct device *dev, struct device_driver *drv)
753 struct ap_device *ap_dev = to_ap_dev(dev);
754 struct ap_driver *ap_drv = to_ap_drv(drv);
755 struct ap_device_id *id;
758 * Compare device type of the device with the list of
759 * supported types of the device_driver.
761 for (id = ap_drv->ids; id->match_flags; id++) {
762 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
763 (id->dev_type != ap_dev->device_type))
764 continue;
765 return 1;
767 return 0;
771 * ap_uevent(): Uevent function for AP devices.
772 * @dev: Pointer to device
773 * @env: Pointer to kobj_uevent_env
775 * It sets up a single environment variable DEV_TYPE which contains the
776 * hardware device type.
778 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
780 struct ap_device *ap_dev = to_ap_dev(dev);
781 int retval = 0;
783 if (!ap_dev)
784 return -ENODEV;
786 /* Set up DEV_TYPE environment variable. */
787 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
788 if (retval)
789 return retval;
791 /* Add MODALIAS= */
792 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
794 return retval;
797 static int ap_bus_suspend(struct device *dev, pm_message_t state)
799 struct ap_device *ap_dev = to_ap_dev(dev);
800 unsigned long flags;
802 if (!ap_suspend_flag) {
803 ap_suspend_flag = 1;
805 /* Disable scanning for devices, thus we do not want to scan
806 * for them after removing.
808 del_timer_sync(&ap_config_timer);
809 if (ap_work_queue != NULL) {
810 destroy_workqueue(ap_work_queue);
811 ap_work_queue = NULL;
814 tasklet_disable(&ap_tasklet);
816 /* Poll on the device until all requests are finished. */
817 do {
818 flags = 0;
819 spin_lock_bh(&ap_dev->lock);
820 __ap_poll_device(ap_dev, &flags);
821 spin_unlock_bh(&ap_dev->lock);
822 } while ((flags & 1) || (flags & 2));
824 spin_lock_bh(&ap_dev->lock);
825 ap_dev->unregistered = 1;
826 spin_unlock_bh(&ap_dev->lock);
828 return 0;
831 static int ap_bus_resume(struct device *dev)
833 struct ap_device *ap_dev = to_ap_dev(dev);
834 int rc;
836 if (ap_suspend_flag) {
837 ap_suspend_flag = 0;
838 if (ap_interrupts_available()) {
839 if (!ap_using_interrupts()) {
840 rc = register_adapter_interrupt(&ap_airq);
841 ap_airq_flag = (rc == 0);
843 } else {
844 if (ap_using_interrupts()) {
845 unregister_adapter_interrupt(&ap_airq);
846 ap_airq_flag = 0;
849 ap_query_configuration();
850 if (!user_set_domain) {
851 ap_domain_index = -1;
852 ap_select_domain();
854 init_timer(&ap_config_timer);
855 ap_config_timer.function = ap_config_timeout;
856 ap_config_timer.data = 0;
857 ap_config_timer.expires = jiffies + ap_config_time * HZ;
858 add_timer(&ap_config_timer);
859 ap_work_queue = create_singlethread_workqueue("kapwork");
860 if (!ap_work_queue)
861 return -ENOMEM;
862 tasklet_enable(&ap_tasklet);
863 if (!ap_using_interrupts())
864 ap_schedule_poll_timer();
865 else
866 tasklet_schedule(&ap_tasklet);
867 if (ap_thread_flag)
868 rc = ap_poll_thread_start();
869 else
870 rc = 0;
871 } else
872 rc = 0;
873 if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
874 spin_lock_bh(&ap_dev->lock);
875 ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
876 ap_domain_index);
877 spin_unlock_bh(&ap_dev->lock);
879 queue_work(ap_work_queue, &ap_config_work);
881 return rc;
884 static struct bus_type ap_bus_type = {
885 .name = "ap",
886 .match = &ap_bus_match,
887 .uevent = &ap_uevent,
888 .suspend = ap_bus_suspend,
889 .resume = ap_bus_resume
892 static int ap_device_probe(struct device *dev)
894 struct ap_device *ap_dev = to_ap_dev(dev);
895 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
896 int rc;
898 ap_dev->drv = ap_drv;
899 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
900 if (!rc) {
901 spin_lock_bh(&ap_device_list_lock);
902 list_add(&ap_dev->list, &ap_device_list);
903 spin_unlock_bh(&ap_device_list_lock);
905 return rc;
909 * __ap_flush_queue(): Flush requests.
910 * @ap_dev: Pointer to the AP device
912 * Flush all requests from the request/pending queue of an AP device.
914 static void __ap_flush_queue(struct ap_device *ap_dev)
916 struct ap_message *ap_msg, *next;
918 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
919 list_del_init(&ap_msg->list);
920 ap_dev->pendingq_count--;
921 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
923 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
924 list_del_init(&ap_msg->list);
925 ap_dev->requestq_count--;
926 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
930 void ap_flush_queue(struct ap_device *ap_dev)
932 spin_lock_bh(&ap_dev->lock);
933 __ap_flush_queue(ap_dev);
934 spin_unlock_bh(&ap_dev->lock);
936 EXPORT_SYMBOL(ap_flush_queue);
938 static int ap_device_remove(struct device *dev)
940 struct ap_device *ap_dev = to_ap_dev(dev);
941 struct ap_driver *ap_drv = ap_dev->drv;
943 ap_flush_queue(ap_dev);
944 del_timer_sync(&ap_dev->timeout);
945 spin_lock_bh(&ap_device_list_lock);
946 list_del_init(&ap_dev->list);
947 spin_unlock_bh(&ap_device_list_lock);
948 if (ap_drv->remove)
949 ap_drv->remove(ap_dev);
950 spin_lock_bh(&ap_dev->lock);
951 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
952 spin_unlock_bh(&ap_dev->lock);
953 return 0;
956 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
957 char *name)
959 struct device_driver *drv = &ap_drv->driver;
961 drv->bus = &ap_bus_type;
962 drv->probe = ap_device_probe;
963 drv->remove = ap_device_remove;
964 drv->owner = owner;
965 drv->name = name;
966 return driver_register(drv);
968 EXPORT_SYMBOL(ap_driver_register);
970 void ap_driver_unregister(struct ap_driver *ap_drv)
972 driver_unregister(&ap_drv->driver);
974 EXPORT_SYMBOL(ap_driver_unregister);
976 void ap_bus_force_rescan(void)
978 /* reconfigure the AP bus rescan timer. */
979 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
980 /* processing a asynchronous bus rescan */
981 queue_work(ap_work_queue, &ap_config_work);
982 flush_work(&ap_config_work);
984 EXPORT_SYMBOL(ap_bus_force_rescan);
987 * AP bus attributes.
989 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
991 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
994 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
996 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
998 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1001 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1003 return snprintf(buf, PAGE_SIZE, "%d\n",
1004 ap_using_interrupts() ? 1 : 0);
1007 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
1009 static ssize_t ap_config_time_store(struct bus_type *bus,
1010 const char *buf, size_t count)
1012 int time;
1014 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1015 return -EINVAL;
1016 ap_config_time = time;
1017 if (!timer_pending(&ap_config_timer) ||
1018 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
1019 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1020 add_timer(&ap_config_timer);
1022 return count;
1025 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
1027 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
1029 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1032 static ssize_t ap_poll_thread_store(struct bus_type *bus,
1033 const char *buf, size_t count)
1035 int flag, rc;
1037 if (sscanf(buf, "%d\n", &flag) != 1)
1038 return -EINVAL;
1039 if (flag) {
1040 rc = ap_poll_thread_start();
1041 if (rc)
1042 return rc;
1044 else
1045 ap_poll_thread_stop();
1046 return count;
1049 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
1051 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1053 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1056 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1057 size_t count)
1059 unsigned long long time;
1060 ktime_t hr_time;
1062 /* 120 seconds = maximum poll interval */
1063 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1064 time > 120000000000ULL)
1065 return -EINVAL;
1066 poll_timeout = time;
1067 hr_time = ktime_set(0, poll_timeout);
1069 if (!hrtimer_is_queued(&ap_poll_timer) ||
1070 !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
1071 hrtimer_set_expires(&ap_poll_timer, hr_time);
1072 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1074 return count;
1077 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
1079 static struct bus_attribute *const ap_bus_attrs[] = {
1080 &bus_attr_ap_domain,
1081 &bus_attr_config_time,
1082 &bus_attr_poll_thread,
1083 &bus_attr_ap_interrupts,
1084 &bus_attr_poll_timeout,
1085 NULL,
1088 static inline int ap_test_config(unsigned int *field, unsigned int nr)
1090 if (nr > 0xFFu)
1091 return 0;
1092 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
1096 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
1097 * @id AP card ID
1099 * Returns 0 if the card is not configured
1100 * 1 if the card is configured or
1101 * if the configuration information is not available
1103 static inline int ap_test_config_card_id(unsigned int id)
1105 if (!ap_configuration)
1106 return 1;
1107 return ap_test_config(ap_configuration->apm, id);
1111 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
1112 * @domain AP usage domain ID
1114 * Returns 0 if the usage domain is not configured
1115 * 1 if the usage domain is configured or
1116 * if the configuration information is not available
1118 static inline int ap_test_config_domain(unsigned int domain)
1120 if (!ap_configuration)
1121 return 1;
1122 return ap_test_config(ap_configuration->aqm, domain);
1126 * ap_query_configuration(): Query AP configuration information.
1128 * Query information of installed cards and configured domains from AP.
1130 static void ap_query_configuration(void)
1132 #ifdef CONFIG_64BIT
1133 if (ap_configuration_available()) {
1134 if (!ap_configuration)
1135 ap_configuration =
1136 kzalloc(sizeof(struct ap_config_info),
1137 GFP_KERNEL);
1138 if (ap_configuration)
1139 __ap_query_configuration(ap_configuration);
1140 } else
1141 ap_configuration = NULL;
1142 #else
1143 ap_configuration = NULL;
1144 #endif
1148 * ap_select_domain(): Select an AP domain.
1150 * Pick one of the 16 AP domains.
1152 static int ap_select_domain(void)
1154 int queue_depth, device_type, count, max_count, best_domain;
1155 ap_qid_t qid;
1156 int rc, i, j;
1159 * We want to use a single domain. Either the one specified with
1160 * the "domain=" parameter or the domain with the maximum number
1161 * of devices.
1163 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
1164 /* Domain has already been selected. */
1165 return 0;
1166 best_domain = -1;
1167 max_count = 0;
1168 for (i = 0; i < AP_DOMAINS; i++) {
1169 if (!ap_test_config_domain(i))
1170 continue;
1171 count = 0;
1172 for (j = 0; j < AP_DEVICES; j++) {
1173 if (!ap_test_config_card_id(j))
1174 continue;
1175 qid = AP_MKQID(j, i);
1176 rc = ap_query_queue(qid, &queue_depth, &device_type);
1177 if (rc)
1178 continue;
1179 count++;
1181 if (count > max_count) {
1182 max_count = count;
1183 best_domain = i;
1186 if (best_domain >= 0){
1187 ap_domain_index = best_domain;
1188 return 0;
1190 return -ENODEV;
1194 * ap_probe_device_type(): Find the device type of an AP.
1195 * @ap_dev: pointer to the AP device.
1197 * Find the device type if query queue returned a device type of 0.
1199 static int ap_probe_device_type(struct ap_device *ap_dev)
1201 static unsigned char msg[] = {
1202 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1203 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1204 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1205 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1206 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1207 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1208 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1209 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1210 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1211 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1212 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1213 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1214 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1215 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1216 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1217 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1218 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1219 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1220 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1221 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1222 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1223 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1224 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1225 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1226 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1227 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1228 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1229 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1230 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1231 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1232 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1233 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1234 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1235 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1236 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1237 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1238 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1239 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1240 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1241 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1242 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1243 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1244 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1245 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1246 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1248 struct ap_queue_status status;
1249 unsigned long long psmid;
1250 char *reply;
1251 int rc, i;
1253 reply = (void *) get_zeroed_page(GFP_KERNEL);
1254 if (!reply) {
1255 rc = -ENOMEM;
1256 goto out;
1259 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1260 msg, sizeof(msg), 0);
1261 if (status.response_code != AP_RESPONSE_NORMAL) {
1262 rc = -ENODEV;
1263 goto out_free;
1266 /* Wait for the test message to complete. */
1267 for (i = 0; i < 6; i++) {
1268 mdelay(300);
1269 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1270 if (status.response_code == AP_RESPONSE_NORMAL &&
1271 psmid == 0x0102030405060708ULL)
1272 break;
1274 if (i < 6) {
1275 /* Got an answer. */
1276 if (reply[0] == 0x00 && reply[1] == 0x86)
1277 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1278 else
1279 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1280 rc = 0;
1281 } else
1282 rc = -ENODEV;
1284 out_free:
1285 free_page((unsigned long) reply);
1286 out:
1287 return rc;
1290 static void ap_interrupt_handler(struct airq_struct *airq)
1292 inc_irq_stat(IRQIO_APB);
1293 tasklet_schedule(&ap_tasklet);
1297 * __ap_scan_bus(): Scan the AP bus.
1298 * @dev: Pointer to device
1299 * @data: Pointer to data
1301 * Scan the AP bus for new devices.
1303 static int __ap_scan_bus(struct device *dev, void *data)
1305 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1308 static void ap_device_release(struct device *dev)
1310 struct ap_device *ap_dev = to_ap_dev(dev);
1312 kfree(ap_dev);
1315 static void ap_scan_bus(struct work_struct *unused)
1317 struct ap_device *ap_dev;
1318 struct device *dev;
1319 ap_qid_t qid;
1320 int queue_depth, device_type;
1321 unsigned int device_functions;
1322 int rc, i;
1324 ap_query_configuration();
1325 if (ap_select_domain() != 0) {
1326 return;
1328 for (i = 0; i < AP_DEVICES; i++) {
1329 qid = AP_MKQID(i, ap_domain_index);
1330 dev = bus_find_device(&ap_bus_type, NULL,
1331 (void *)(unsigned long)qid,
1332 __ap_scan_bus);
1333 if (ap_test_config_card_id(i))
1334 rc = ap_query_queue(qid, &queue_depth, &device_type);
1335 else
1336 rc = -ENODEV;
1337 if (dev) {
1338 if (rc == -EBUSY) {
1339 set_current_state(TASK_UNINTERRUPTIBLE);
1340 schedule_timeout(AP_RESET_TIMEOUT);
1341 rc = ap_query_queue(qid, &queue_depth,
1342 &device_type);
1344 ap_dev = to_ap_dev(dev);
1345 spin_lock_bh(&ap_dev->lock);
1346 if (rc || ap_dev->unregistered) {
1347 spin_unlock_bh(&ap_dev->lock);
1348 if (ap_dev->unregistered)
1349 i--;
1350 device_unregister(dev);
1351 put_device(dev);
1352 continue;
1354 spin_unlock_bh(&ap_dev->lock);
1355 put_device(dev);
1356 continue;
1358 if (rc)
1359 continue;
1360 rc = ap_init_queue(qid);
1361 if (rc)
1362 continue;
1363 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1364 if (!ap_dev)
1365 break;
1366 ap_dev->qid = qid;
1367 ap_dev->queue_depth = queue_depth;
1368 ap_dev->unregistered = 1;
1369 spin_lock_init(&ap_dev->lock);
1370 INIT_LIST_HEAD(&ap_dev->pendingq);
1371 INIT_LIST_HEAD(&ap_dev->requestq);
1372 INIT_LIST_HEAD(&ap_dev->list);
1373 setup_timer(&ap_dev->timeout, ap_request_timeout,
1374 (unsigned long) ap_dev);
1375 switch (device_type) {
1376 case 0:
1377 /* device type probing for old cards */
1378 if (ap_probe_device_type(ap_dev)) {
1379 kfree(ap_dev);
1380 continue;
1382 break;
1383 default:
1384 ap_dev->device_type = device_type;
1387 rc = ap_query_functions(qid, &device_functions);
1388 if (!rc)
1389 ap_dev->functions = device_functions;
1390 else
1391 ap_dev->functions = 0u;
1393 ap_dev->device.bus = &ap_bus_type;
1394 ap_dev->device.parent = ap_root_device;
1395 if (dev_set_name(&ap_dev->device, "card%02x",
1396 AP_QID_DEVICE(ap_dev->qid))) {
1397 kfree(ap_dev);
1398 continue;
1400 ap_dev->device.release = ap_device_release;
1401 rc = device_register(&ap_dev->device);
1402 if (rc) {
1403 put_device(&ap_dev->device);
1404 continue;
1406 /* Add device attributes. */
1407 rc = sysfs_create_group(&ap_dev->device.kobj,
1408 &ap_dev_attr_group);
1409 if (!rc) {
1410 spin_lock_bh(&ap_dev->lock);
1411 ap_dev->unregistered = 0;
1412 spin_unlock_bh(&ap_dev->lock);
1414 else
1415 device_unregister(&ap_dev->device);
1419 static void
1420 ap_config_timeout(unsigned long ptr)
1422 queue_work(ap_work_queue, &ap_config_work);
1423 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1424 add_timer(&ap_config_timer);
1428 * __ap_schedule_poll_timer(): Schedule poll timer.
1430 * Set up the timer to run the poll tasklet
1432 static inline void __ap_schedule_poll_timer(void)
1434 ktime_t hr_time;
1436 spin_lock_bh(&ap_poll_timer_lock);
1437 if (hrtimer_is_queued(&ap_poll_timer) || ap_suspend_flag)
1438 goto out;
1439 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1440 hr_time = ktime_set(0, poll_timeout);
1441 hrtimer_forward_now(&ap_poll_timer, hr_time);
1442 hrtimer_restart(&ap_poll_timer);
1444 out:
1445 spin_unlock_bh(&ap_poll_timer_lock);
1449 * ap_schedule_poll_timer(): Schedule poll timer.
1451 * Set up the timer to run the poll tasklet
1453 static inline void ap_schedule_poll_timer(void)
1455 if (ap_using_interrupts())
1456 return;
1457 __ap_schedule_poll_timer();
1461 * ap_poll_read(): Receive pending reply messages from an AP device.
1462 * @ap_dev: pointer to the AP device
1463 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1464 * required, bit 2^1 is set if the poll timer needs to get armed
1466 * Returns 0 if the device is still present, -ENODEV if not.
1468 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1470 struct ap_queue_status status;
1471 struct ap_message *ap_msg;
1473 if (ap_dev->queue_count <= 0)
1474 return 0;
1475 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1476 ap_dev->reply->message, ap_dev->reply->length);
1477 switch (status.response_code) {
1478 case AP_RESPONSE_NORMAL:
1479 atomic_dec(&ap_poll_requests);
1480 ap_decrease_queue_count(ap_dev);
1481 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1482 if (ap_msg->psmid != ap_dev->reply->psmid)
1483 continue;
1484 list_del_init(&ap_msg->list);
1485 ap_dev->pendingq_count--;
1486 ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
1487 break;
1489 if (ap_dev->queue_count > 0)
1490 *flags |= 1;
1491 break;
1492 case AP_RESPONSE_NO_PENDING_REPLY:
1493 if (status.queue_empty) {
1494 /* The card shouldn't forget requests but who knows. */
1495 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1496 ap_dev->queue_count = 0;
1497 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1498 ap_dev->requestq_count += ap_dev->pendingq_count;
1499 ap_dev->pendingq_count = 0;
1500 } else
1501 *flags |= 2;
1502 break;
1503 default:
1504 return -ENODEV;
1506 return 0;
1510 * ap_poll_write(): Send messages from the request queue to an AP device.
1511 * @ap_dev: pointer to the AP device
1512 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1513 * required, bit 2^1 is set if the poll timer needs to get armed
1515 * Returns 0 if the device is still present, -ENODEV if not.
1517 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1519 struct ap_queue_status status;
1520 struct ap_message *ap_msg;
1522 if (ap_dev->requestq_count <= 0 ||
1523 ap_dev->queue_count >= ap_dev->queue_depth)
1524 return 0;
1525 /* Start the next request on the queue. */
1526 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1527 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1528 ap_msg->message, ap_msg->length, ap_msg->special);
1529 switch (status.response_code) {
1530 case AP_RESPONSE_NORMAL:
1531 atomic_inc(&ap_poll_requests);
1532 ap_increase_queue_count(ap_dev);
1533 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1534 ap_dev->requestq_count--;
1535 ap_dev->pendingq_count++;
1536 if (ap_dev->queue_count < ap_dev->queue_depth &&
1537 ap_dev->requestq_count > 0)
1538 *flags |= 1;
1539 *flags |= 2;
1540 break;
1541 case AP_RESPONSE_RESET_IN_PROGRESS:
1542 __ap_schedule_poll_timer();
1543 case AP_RESPONSE_Q_FULL:
1544 *flags |= 2;
1545 break;
1546 case AP_RESPONSE_MESSAGE_TOO_BIG:
1547 case AP_RESPONSE_REQ_FAC_NOT_INST:
1548 return -EINVAL;
1549 default:
1550 return -ENODEV;
1552 return 0;
1556 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1557 * @ap_dev: pointer to the bus device
1558 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1559 * required, bit 2^1 is set if the poll timer needs to get armed
1561 * Poll AP device for pending replies and send new messages. If either
1562 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1563 * Returns 0.
1565 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1567 int rc;
1569 rc = ap_poll_read(ap_dev, flags);
1570 if (rc)
1571 return rc;
1572 return ap_poll_write(ap_dev, flags);
1576 * __ap_queue_message(): Queue a message to a device.
1577 * @ap_dev: pointer to the AP device
1578 * @ap_msg: the message to be queued
1580 * Queue a message to a device. Returns 0 if successful.
1582 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1584 struct ap_queue_status status;
1586 if (list_empty(&ap_dev->requestq) &&
1587 ap_dev->queue_count < ap_dev->queue_depth) {
1588 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1589 ap_msg->message, ap_msg->length,
1590 ap_msg->special);
1591 switch (status.response_code) {
1592 case AP_RESPONSE_NORMAL:
1593 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1594 atomic_inc(&ap_poll_requests);
1595 ap_dev->pendingq_count++;
1596 ap_increase_queue_count(ap_dev);
1597 ap_dev->total_request_count++;
1598 break;
1599 case AP_RESPONSE_Q_FULL:
1600 case AP_RESPONSE_RESET_IN_PROGRESS:
1601 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1602 ap_dev->requestq_count++;
1603 ap_dev->total_request_count++;
1604 return -EBUSY;
1605 case AP_RESPONSE_REQ_FAC_NOT_INST:
1606 case AP_RESPONSE_MESSAGE_TOO_BIG:
1607 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1608 return -EINVAL;
1609 default: /* Device is gone. */
1610 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1611 return -ENODEV;
1613 } else {
1614 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1615 ap_dev->requestq_count++;
1616 ap_dev->total_request_count++;
1617 return -EBUSY;
1619 ap_schedule_poll_timer();
1620 return 0;
1623 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1625 unsigned long flags;
1626 int rc;
1628 /* For asynchronous message handling a valid receive-callback
1629 * is required. */
1630 BUG_ON(!ap_msg->receive);
1632 spin_lock_bh(&ap_dev->lock);
1633 if (!ap_dev->unregistered) {
1634 /* Make room on the queue by polling for finished requests. */
1635 rc = ap_poll_queue(ap_dev, &flags);
1636 if (!rc)
1637 rc = __ap_queue_message(ap_dev, ap_msg);
1638 if (!rc)
1639 wake_up(&ap_poll_wait);
1640 if (rc == -ENODEV)
1641 ap_dev->unregistered = 1;
1642 } else {
1643 ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1644 rc = -ENODEV;
1646 spin_unlock_bh(&ap_dev->lock);
1647 if (rc == -ENODEV)
1648 device_unregister(&ap_dev->device);
1650 EXPORT_SYMBOL(ap_queue_message);
1653 * ap_cancel_message(): Cancel a crypto request.
1654 * @ap_dev: The AP device that has the message queued
1655 * @ap_msg: The message that is to be removed
1657 * Cancel a crypto request. This is done by removing the request
1658 * from the device pending or request queue. Note that the
1659 * request stays on the AP queue. When it finishes the message
1660 * reply will be discarded because the psmid can't be found.
1662 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1664 struct ap_message *tmp;
1666 spin_lock_bh(&ap_dev->lock);
1667 if (!list_empty(&ap_msg->list)) {
1668 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1669 if (tmp->psmid == ap_msg->psmid) {
1670 ap_dev->pendingq_count--;
1671 goto found;
1673 ap_dev->requestq_count--;
1674 found:
1675 list_del_init(&ap_msg->list);
1677 spin_unlock_bh(&ap_dev->lock);
1679 EXPORT_SYMBOL(ap_cancel_message);
1682 * ap_poll_timeout(): AP receive polling for finished AP requests.
1683 * @unused: Unused pointer.
1685 * Schedules the AP tasklet using a high resolution timer.
1687 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1689 tasklet_schedule(&ap_tasklet);
1690 return HRTIMER_NORESTART;
1694 * ap_reset(): Reset a not responding AP device.
1695 * @ap_dev: Pointer to the AP device
1697 * Reset a not responding AP device and move all requests from the
1698 * pending queue to the request queue.
1700 static void ap_reset(struct ap_device *ap_dev)
1702 int rc;
1704 ap_dev->reset = AP_RESET_IGNORE;
1705 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1706 ap_dev->queue_count = 0;
1707 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1708 ap_dev->requestq_count += ap_dev->pendingq_count;
1709 ap_dev->pendingq_count = 0;
1710 rc = ap_init_queue(ap_dev->qid);
1711 if (rc == -ENODEV)
1712 ap_dev->unregistered = 1;
1713 else
1714 __ap_schedule_poll_timer();
1717 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1719 if (!ap_dev->unregistered) {
1720 if (ap_poll_queue(ap_dev, flags))
1721 ap_dev->unregistered = 1;
1722 if (ap_dev->reset == AP_RESET_DO)
1723 ap_reset(ap_dev);
1725 return 0;
1729 * ap_poll_all(): Poll all AP devices.
1730 * @dummy: Unused variable
1732 * Poll all AP devices on the bus in a round robin fashion. Continue
1733 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1734 * of the control flags has been set arm the poll timer.
1736 static void ap_poll_all(unsigned long dummy)
1738 unsigned long flags;
1739 struct ap_device *ap_dev;
1741 /* Reset the indicator if interrupts are used. Thus new interrupts can
1742 * be received. Doing it in the beginning of the tasklet is therefor
1743 * important that no requests on any AP get lost.
1745 if (ap_using_interrupts())
1746 xchg(ap_airq.lsi_ptr, 0);
1747 do {
1748 flags = 0;
1749 spin_lock(&ap_device_list_lock);
1750 list_for_each_entry(ap_dev, &ap_device_list, list) {
1751 spin_lock(&ap_dev->lock);
1752 __ap_poll_device(ap_dev, &flags);
1753 spin_unlock(&ap_dev->lock);
1755 spin_unlock(&ap_device_list_lock);
1756 } while (flags & 1);
1757 if (flags & 2)
1758 ap_schedule_poll_timer();
1762 * ap_poll_thread(): Thread that polls for finished requests.
1763 * @data: Unused pointer
1765 * AP bus poll thread. The purpose of this thread is to poll for
1766 * finished requests in a loop if there is a "free" cpu - that is
1767 * a cpu that doesn't have anything better to do. The polling stops
1768 * as soon as there is another task or if all messages have been
1769 * delivered.
1771 static int ap_poll_thread(void *data)
1773 DECLARE_WAITQUEUE(wait, current);
1774 unsigned long flags;
1775 int requests;
1776 struct ap_device *ap_dev;
1778 set_user_nice(current, 19);
1779 while (1) {
1780 if (ap_suspend_flag)
1781 return 0;
1782 if (need_resched()) {
1783 schedule();
1784 continue;
1786 add_wait_queue(&ap_poll_wait, &wait);
1787 set_current_state(TASK_INTERRUPTIBLE);
1788 if (kthread_should_stop())
1789 break;
1790 requests = atomic_read(&ap_poll_requests);
1791 if (requests <= 0)
1792 schedule();
1793 set_current_state(TASK_RUNNING);
1794 remove_wait_queue(&ap_poll_wait, &wait);
1796 flags = 0;
1797 spin_lock_bh(&ap_device_list_lock);
1798 list_for_each_entry(ap_dev, &ap_device_list, list) {
1799 spin_lock(&ap_dev->lock);
1800 __ap_poll_device(ap_dev, &flags);
1801 spin_unlock(&ap_dev->lock);
1803 spin_unlock_bh(&ap_device_list_lock);
1805 set_current_state(TASK_RUNNING);
1806 remove_wait_queue(&ap_poll_wait, &wait);
1807 return 0;
1810 static int ap_poll_thread_start(void)
1812 int rc;
1814 if (ap_using_interrupts() || ap_suspend_flag)
1815 return 0;
1816 mutex_lock(&ap_poll_thread_mutex);
1817 if (!ap_poll_kthread) {
1818 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1819 rc = PTR_RET(ap_poll_kthread);
1820 if (rc)
1821 ap_poll_kthread = NULL;
1823 else
1824 rc = 0;
1825 mutex_unlock(&ap_poll_thread_mutex);
1826 return rc;
1829 static void ap_poll_thread_stop(void)
1831 mutex_lock(&ap_poll_thread_mutex);
1832 if (ap_poll_kthread) {
1833 kthread_stop(ap_poll_kthread);
1834 ap_poll_kthread = NULL;
1836 mutex_unlock(&ap_poll_thread_mutex);
1840 * ap_request_timeout(): Handling of request timeouts
1841 * @data: Holds the AP device.
1843 * Handles request timeouts.
1845 static void ap_request_timeout(unsigned long data)
1847 struct ap_device *ap_dev = (struct ap_device *) data;
1849 if (ap_dev->reset == AP_RESET_ARMED) {
1850 ap_dev->reset = AP_RESET_DO;
1852 if (ap_using_interrupts())
1853 tasklet_schedule(&ap_tasklet);
1857 static void ap_reset_domain(void)
1859 int i;
1861 if (ap_domain_index != -1)
1862 for (i = 0; i < AP_DEVICES; i++)
1863 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1866 static void ap_reset_all(void)
1868 int i, j;
1870 for (i = 0; i < AP_DOMAINS; i++)
1871 for (j = 0; j < AP_DEVICES; j++)
1872 ap_reset_queue(AP_MKQID(j, i));
1875 static struct reset_call ap_reset_call = {
1876 .fn = ap_reset_all,
1880 * ap_module_init(): The module initialization code.
1882 * Initializes the module.
1884 int __init ap_module_init(void)
1886 int rc, i;
1888 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1889 pr_warning("%d is not a valid cryptographic domain\n",
1890 ap_domain_index);
1891 return -EINVAL;
1893 /* In resume callback we need to know if the user had set the domain.
1894 * If so, we can not just reset it.
1896 if (ap_domain_index >= 0)
1897 user_set_domain = 1;
1899 if (ap_instructions_available() != 0) {
1900 pr_warning("The hardware system does not support "
1901 "AP instructions\n");
1902 return -ENODEV;
1904 if (ap_interrupts_available()) {
1905 rc = register_adapter_interrupt(&ap_airq);
1906 ap_airq_flag = (rc == 0);
1909 register_reset_call(&ap_reset_call);
1911 /* Create /sys/bus/ap. */
1912 rc = bus_register(&ap_bus_type);
1913 if (rc)
1914 goto out;
1915 for (i = 0; ap_bus_attrs[i]; i++) {
1916 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1917 if (rc)
1918 goto out_bus;
1921 /* Create /sys/devices/ap. */
1922 ap_root_device = root_device_register("ap");
1923 rc = PTR_RET(ap_root_device);
1924 if (rc)
1925 goto out_bus;
1927 ap_work_queue = create_singlethread_workqueue("kapwork");
1928 if (!ap_work_queue) {
1929 rc = -ENOMEM;
1930 goto out_root;
1933 ap_query_configuration();
1934 if (ap_select_domain() == 0)
1935 ap_scan_bus(NULL);
1937 /* Setup the AP bus rescan timer. */
1938 init_timer(&ap_config_timer);
1939 ap_config_timer.function = ap_config_timeout;
1940 ap_config_timer.data = 0;
1941 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1942 add_timer(&ap_config_timer);
1944 /* Setup the high resultion poll timer.
1945 * If we are running under z/VM adjust polling to z/VM polling rate.
1947 if (MACHINE_IS_VM)
1948 poll_timeout = 1500000;
1949 spin_lock_init(&ap_poll_timer_lock);
1950 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1951 ap_poll_timer.function = ap_poll_timeout;
1953 /* Start the low priority AP bus poll thread. */
1954 if (ap_thread_flag) {
1955 rc = ap_poll_thread_start();
1956 if (rc)
1957 goto out_work;
1960 return 0;
1962 out_work:
1963 del_timer_sync(&ap_config_timer);
1964 hrtimer_cancel(&ap_poll_timer);
1965 destroy_workqueue(ap_work_queue);
1966 out_root:
1967 root_device_unregister(ap_root_device);
1968 out_bus:
1969 while (i--)
1970 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1971 bus_unregister(&ap_bus_type);
1972 out:
1973 unregister_reset_call(&ap_reset_call);
1974 if (ap_using_interrupts())
1975 unregister_adapter_interrupt(&ap_airq);
1976 return rc;
1979 static int __ap_match_all(struct device *dev, void *data)
1981 return 1;
1985 * ap_modules_exit(): The module termination code
1987 * Terminates the module.
1989 void ap_module_exit(void)
1991 int i;
1992 struct device *dev;
1994 ap_reset_domain();
1995 ap_poll_thread_stop();
1996 del_timer_sync(&ap_config_timer);
1997 hrtimer_cancel(&ap_poll_timer);
1998 destroy_workqueue(ap_work_queue);
1999 tasklet_kill(&ap_tasklet);
2000 root_device_unregister(ap_root_device);
2001 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
2002 __ap_match_all)))
2004 device_unregister(dev);
2005 put_device(dev);
2007 for (i = 0; ap_bus_attrs[i]; i++)
2008 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
2009 bus_unregister(&ap_bus_type);
2010 unregister_reset_call(&ap_reset_call);
2011 if (ap_using_interrupts())
2012 unregister_adapter_interrupt(&ap_airq);
2015 module_init(ap_module_init);
2016 module_exit(ap_module_exit);