GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / s390 / crypto / ap_bus.c
blob91c6028d7b74016157bba24247da45cf5aac141d
1 /*
2 * linux/drivers/s390/crypto/ap_bus.c
4 * Copyright (C) 2006 IBM Corporation
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Ralph Wuerthner <rwuerthn@de.ibm.com>
8 * Felix Beck <felix.beck@de.ibm.com>
10 * Adjunct processor bus.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define KMSG_COMPONENT "ap"
28 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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 <asm/atomic.h>
43 #include <asm/system.h>
44 #include <asm/isc.h>
45 #include <linux/hrtimer.h>
46 #include <linux/ktime.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 2006 IBM Corporation");
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 unsigned long long facility_bits[2];
159 if (stfle(facility_bits, 2) <= 1)
160 return 0;
161 if (!(facility_bits[0] & (1ULL << 61)) ||
162 !(facility_bits[1] & (1ULL << 62)))
163 return 0;
164 return 1;
168 * ap_test_queue(): Test adjunct processor queue.
169 * @qid: The AP queue number
170 * @queue_depth: Pointer to queue depth value
171 * @device_type: Pointer to device type value
173 * Returns AP queue status structure.
175 static inline struct ap_queue_status
176 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
178 register unsigned long reg0 asm ("0") = qid;
179 register struct ap_queue_status reg1 asm ("1");
180 register unsigned long reg2 asm ("2") = 0UL;
182 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
183 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
184 *device_type = (int) (reg2 >> 24);
185 *queue_depth = (int) (reg2 & 0xff);
186 return reg1;
190 * ap_reset_queue(): Reset adjunct processor queue.
191 * @qid: The AP queue number
193 * Returns AP queue status structure.
195 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
197 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
198 register struct ap_queue_status reg1 asm ("1");
199 register unsigned long reg2 asm ("2") = 0UL;
201 asm volatile(
202 ".long 0xb2af0000" /* PQAP(RAPQ) */
203 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
204 return reg1;
207 #ifdef CONFIG_64BIT
209 * ap_queue_interruption_control(): Enable interruption for a specific AP.
210 * @qid: The AP queue number
211 * @ind: The notification indicator byte
213 * Returns AP queue status.
215 static inline struct ap_queue_status
216 ap_queue_interruption_control(ap_qid_t qid, void *ind)
218 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
219 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
220 register struct ap_queue_status reg1_out asm ("1");
221 register void *reg2 asm ("2") = ind;
222 asm volatile(
223 ".long 0xb2af0000" /* PQAP(RAPQ) */
224 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
226 : "cc" );
227 return reg1_out;
229 #endif
232 * ap_queue_enable_interruption(): Enable interruption on an AP.
233 * @qid: The AP queue number
234 * @ind: the notification indicator byte
236 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
237 * on the return value it waits a while and tests the AP queue if interrupts
238 * have been switched on using ap_test_queue().
240 static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
242 #ifdef CONFIG_64BIT
243 struct ap_queue_status status;
244 int t_depth, t_device_type, rc, i;
246 rc = -EBUSY;
247 status = ap_queue_interruption_control(qid, ind);
249 for (i = 0; i < AP_MAX_RESET; i++) {
250 switch (status.response_code) {
251 case AP_RESPONSE_NORMAL:
252 if (status.int_enabled)
253 return 0;
254 break;
255 case AP_RESPONSE_RESET_IN_PROGRESS:
256 case AP_RESPONSE_BUSY:
257 break;
258 case AP_RESPONSE_Q_NOT_AVAIL:
259 case AP_RESPONSE_DECONFIGURED:
260 case AP_RESPONSE_CHECKSTOPPED:
261 case AP_RESPONSE_INVALID_ADDRESS:
262 return -ENODEV;
263 case AP_RESPONSE_OTHERWISE_CHANGED:
264 if (status.int_enabled)
265 return 0;
266 break;
267 default:
268 break;
270 if (i < AP_MAX_RESET - 1) {
271 udelay(5);
272 status = ap_test_queue(qid, &t_depth, &t_device_type);
275 return rc;
276 #else
277 return -EINVAL;
278 #endif
282 * __ap_send(): Send message to adjunct processor queue.
283 * @qid: The AP queue number
284 * @psmid: The program supplied message identifier
285 * @msg: The message text
286 * @length: The message length
287 * @special: Special Bit
289 * Returns AP queue status structure.
290 * Condition code 1 on NQAP can't happen because the L bit is 1.
291 * Condition code 2 on NQAP also means the send is incomplete,
292 * because a segment boundary was reached. The NQAP is repeated.
294 static inline struct ap_queue_status
295 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
296 unsigned int special)
298 typedef struct { char _[length]; } msgblock;
299 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
300 register struct ap_queue_status reg1 asm ("1");
301 register unsigned long reg2 asm ("2") = (unsigned long) msg;
302 register unsigned long reg3 asm ("3") = (unsigned long) length;
303 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
304 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
306 if (special == 1)
307 reg0 |= 0x400000UL;
309 asm volatile (
310 "0: .long 0xb2ad0042\n" /* DQAP */
311 " brc 2,0b"
312 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
313 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
314 : "cc" );
315 return reg1;
318 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
320 struct ap_queue_status status;
322 status = __ap_send(qid, psmid, msg, length, 0);
323 switch (status.response_code) {
324 case AP_RESPONSE_NORMAL:
325 return 0;
326 case AP_RESPONSE_Q_FULL:
327 case AP_RESPONSE_RESET_IN_PROGRESS:
328 return -EBUSY;
329 case AP_RESPONSE_REQ_FAC_NOT_INST:
330 return -EINVAL;
331 default: /* Device is gone. */
332 return -ENODEV;
335 EXPORT_SYMBOL(ap_send);
338 * __ap_recv(): Receive message from adjunct processor queue.
339 * @qid: The AP queue number
340 * @psmid: Pointer to program supplied message identifier
341 * @msg: The message text
342 * @length: The message length
344 * Returns AP queue status structure.
345 * Condition code 1 on DQAP means the receive has taken place
346 * but only partially. The response is incomplete, hence the
347 * DQAP is repeated.
348 * Condition code 2 on DQAP also means the receive is incomplete,
349 * this time because a segment boundary was reached. Again, the
350 * DQAP is repeated.
351 * Note that gpr2 is used by the DQAP instruction to keep track of
352 * any 'residual' length, in case the instruction gets interrupted.
353 * Hence it gets zeroed before the instruction.
355 static inline struct ap_queue_status
356 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
358 typedef struct { char _[length]; } msgblock;
359 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
360 register struct ap_queue_status reg1 asm ("1");
361 register unsigned long reg2 asm("2") = 0UL;
362 register unsigned long reg4 asm("4") = (unsigned long) msg;
363 register unsigned long reg5 asm("5") = (unsigned long) length;
364 register unsigned long reg6 asm("6") = 0UL;
365 register unsigned long reg7 asm("7") = 0UL;
368 asm volatile(
369 "0: .long 0xb2ae0064\n"
370 " brc 6,0b\n"
371 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
372 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
373 "=m" (*(msgblock *) msg) : : "cc" );
374 *psmid = (((unsigned long long) reg6) << 32) + reg7;
375 return reg1;
378 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
380 struct ap_queue_status status;
382 status = __ap_recv(qid, psmid, msg, length);
383 switch (status.response_code) {
384 case AP_RESPONSE_NORMAL:
385 return 0;
386 case AP_RESPONSE_NO_PENDING_REPLY:
387 if (status.queue_empty)
388 return -ENOENT;
389 return -EBUSY;
390 case AP_RESPONSE_RESET_IN_PROGRESS:
391 return -EBUSY;
392 default:
393 return -ENODEV;
396 EXPORT_SYMBOL(ap_recv);
399 * ap_query_queue(): Check if an AP queue is available.
400 * @qid: The AP queue number
401 * @queue_depth: Pointer to queue depth value
402 * @device_type: Pointer to device type value
404 * The test is repeated for AP_MAX_RESET times.
406 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
408 struct ap_queue_status status;
409 int t_depth, t_device_type, rc, i;
411 rc = -EBUSY;
412 for (i = 0; i < AP_MAX_RESET; i++) {
413 status = ap_test_queue(qid, &t_depth, &t_device_type);
414 switch (status.response_code) {
415 case AP_RESPONSE_NORMAL:
416 *queue_depth = t_depth + 1;
417 *device_type = t_device_type;
418 rc = 0;
419 break;
420 case AP_RESPONSE_Q_NOT_AVAIL:
421 rc = -ENODEV;
422 break;
423 case AP_RESPONSE_RESET_IN_PROGRESS:
424 break;
425 case AP_RESPONSE_DECONFIGURED:
426 rc = -ENODEV;
427 break;
428 case AP_RESPONSE_CHECKSTOPPED:
429 rc = -ENODEV;
430 break;
431 case AP_RESPONSE_INVALID_ADDRESS:
432 rc = -ENODEV;
433 break;
434 case AP_RESPONSE_OTHERWISE_CHANGED:
435 break;
436 case AP_RESPONSE_BUSY:
437 break;
438 default:
439 BUG();
441 if (rc != -EBUSY)
442 break;
443 if (i < AP_MAX_RESET - 1)
444 udelay(5);
446 return rc;
450 * ap_init_queue(): Reset an AP queue.
451 * @qid: The AP queue number
453 * Reset an AP queue and wait for it to become available again.
455 static int ap_init_queue(ap_qid_t qid)
457 struct ap_queue_status status;
458 int rc, dummy, i;
460 rc = -ENODEV;
461 status = ap_reset_queue(qid);
462 for (i = 0; i < AP_MAX_RESET; i++) {
463 switch (status.response_code) {
464 case AP_RESPONSE_NORMAL:
465 if (status.queue_empty)
466 rc = 0;
467 break;
468 case AP_RESPONSE_Q_NOT_AVAIL:
469 case AP_RESPONSE_DECONFIGURED:
470 case AP_RESPONSE_CHECKSTOPPED:
471 i = AP_MAX_RESET; /* return with -ENODEV */
472 break;
473 case AP_RESPONSE_RESET_IN_PROGRESS:
474 rc = -EBUSY;
475 case AP_RESPONSE_BUSY:
476 default:
477 break;
479 if (rc != -ENODEV && rc != -EBUSY)
480 break;
481 if (i < AP_MAX_RESET - 1) {
482 udelay(5);
483 status = ap_test_queue(qid, &dummy, &dummy);
486 if (rc == 0 && ap_using_interrupts()) {
487 rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
488 /* If interruption mode is supported by the machine,
489 * but an AP can not be enabled for interruption then
490 * the AP will be discarded. */
491 if (rc)
492 pr_err("Registering adapter interrupts for "
493 "AP %d failed\n", AP_QID_DEVICE(qid));
495 return rc;
499 * ap_increase_queue_count(): Arm request timeout.
500 * @ap_dev: Pointer to an AP device.
502 * Arm request timeout if an AP device was idle and a new request is submitted.
504 static void ap_increase_queue_count(struct ap_device *ap_dev)
506 int timeout = ap_dev->drv->request_timeout;
508 ap_dev->queue_count++;
509 if (ap_dev->queue_count == 1) {
510 mod_timer(&ap_dev->timeout, jiffies + timeout);
511 ap_dev->reset = AP_RESET_ARMED;
516 * ap_decrease_queue_count(): Decrease queue count.
517 * @ap_dev: Pointer to an AP device.
519 * If AP device is still alive, re-schedule request timeout if there are still
520 * pending requests.
522 static void ap_decrease_queue_count(struct ap_device *ap_dev)
524 int timeout = ap_dev->drv->request_timeout;
526 ap_dev->queue_count--;
527 if (ap_dev->queue_count > 0)
528 mod_timer(&ap_dev->timeout, jiffies + timeout);
529 else
531 * The timeout timer should to be disabled now - since
532 * del_timer_sync() is very expensive, we just tell via the
533 * reset flag to ignore the pending timeout timer.
535 ap_dev->reset = AP_RESET_IGNORE;
539 * AP device related attributes.
541 static ssize_t ap_hwtype_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
544 struct ap_device *ap_dev = to_ap_dev(dev);
545 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
548 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
549 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
550 char *buf)
552 struct ap_device *ap_dev = to_ap_dev(dev);
553 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
556 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
557 static ssize_t ap_request_count_show(struct device *dev,
558 struct device_attribute *attr,
559 char *buf)
561 struct ap_device *ap_dev = to_ap_dev(dev);
562 int rc;
564 spin_lock_bh(&ap_dev->lock);
565 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
566 spin_unlock_bh(&ap_dev->lock);
567 return rc;
570 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
572 static ssize_t ap_modalias_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
575 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
578 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
580 static struct attribute *ap_dev_attrs[] = {
581 &dev_attr_hwtype.attr,
582 &dev_attr_depth.attr,
583 &dev_attr_request_count.attr,
584 &dev_attr_modalias.attr,
585 NULL
587 static struct attribute_group ap_dev_attr_group = {
588 .attrs = ap_dev_attrs
592 * ap_bus_match()
593 * @dev: Pointer to device
594 * @drv: Pointer to device_driver
596 * AP bus driver registration/unregistration.
598 static int ap_bus_match(struct device *dev, struct device_driver *drv)
600 struct ap_device *ap_dev = to_ap_dev(dev);
601 struct ap_driver *ap_drv = to_ap_drv(drv);
602 struct ap_device_id *id;
605 * Compare device type of the device with the list of
606 * supported types of the device_driver.
608 for (id = ap_drv->ids; id->match_flags; id++) {
609 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
610 (id->dev_type != ap_dev->device_type))
611 continue;
612 return 1;
614 return 0;
618 * ap_uevent(): Uevent function for AP devices.
619 * @dev: Pointer to device
620 * @env: Pointer to kobj_uevent_env
622 * It sets up a single environment variable DEV_TYPE which contains the
623 * hardware device type.
625 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
627 struct ap_device *ap_dev = to_ap_dev(dev);
628 int retval = 0;
630 if (!ap_dev)
631 return -ENODEV;
633 /* Set up DEV_TYPE environment variable. */
634 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
635 if (retval)
636 return retval;
638 /* Add MODALIAS= */
639 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
641 return retval;
644 static int ap_bus_suspend(struct device *dev, pm_message_t state)
646 struct ap_device *ap_dev = to_ap_dev(dev);
647 unsigned long flags;
649 if (!ap_suspend_flag) {
650 ap_suspend_flag = 1;
652 /* Disable scanning for devices, thus we do not want to scan
653 * for them after removing.
655 del_timer_sync(&ap_config_timer);
656 if (ap_work_queue != NULL) {
657 destroy_workqueue(ap_work_queue);
658 ap_work_queue = NULL;
661 tasklet_disable(&ap_tasklet);
663 /* Poll on the device until all requests are finished. */
664 do {
665 flags = 0;
666 spin_lock_bh(&ap_dev->lock);
667 __ap_poll_device(ap_dev, &flags);
668 spin_unlock_bh(&ap_dev->lock);
669 } while ((flags & 1) || (flags & 2));
671 spin_lock_bh(&ap_dev->lock);
672 ap_dev->unregistered = 1;
673 spin_unlock_bh(&ap_dev->lock);
675 return 0;
678 static int ap_bus_resume(struct device *dev)
680 int rc = 0;
681 struct ap_device *ap_dev = to_ap_dev(dev);
683 if (ap_suspend_flag) {
684 ap_suspend_flag = 0;
685 if (!ap_interrupts_available())
686 ap_interrupt_indicator = NULL;
687 if (!user_set_domain) {
688 ap_domain_index = -1;
689 ap_select_domain();
691 init_timer(&ap_config_timer);
692 ap_config_timer.function = ap_config_timeout;
693 ap_config_timer.data = 0;
694 ap_config_timer.expires = jiffies + ap_config_time * HZ;
695 add_timer(&ap_config_timer);
696 ap_work_queue = create_singlethread_workqueue("kapwork");
697 if (!ap_work_queue)
698 return -ENOMEM;
699 tasklet_enable(&ap_tasklet);
700 if (!ap_using_interrupts())
701 ap_schedule_poll_timer();
702 else
703 tasklet_schedule(&ap_tasklet);
704 if (ap_thread_flag)
705 rc = ap_poll_thread_start();
707 if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
708 spin_lock_bh(&ap_dev->lock);
709 ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
710 ap_domain_index);
711 spin_unlock_bh(&ap_dev->lock);
713 queue_work(ap_work_queue, &ap_config_work);
715 return rc;
718 static struct bus_type ap_bus_type = {
719 .name = "ap",
720 .match = &ap_bus_match,
721 .uevent = &ap_uevent,
722 .suspend = ap_bus_suspend,
723 .resume = ap_bus_resume
726 static int ap_device_probe(struct device *dev)
728 struct ap_device *ap_dev = to_ap_dev(dev);
729 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
730 int rc;
732 ap_dev->drv = ap_drv;
733 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
734 if (!rc) {
735 spin_lock_bh(&ap_device_list_lock);
736 list_add(&ap_dev->list, &ap_device_list);
737 spin_unlock_bh(&ap_device_list_lock);
739 return rc;
743 * __ap_flush_queue(): Flush requests.
744 * @ap_dev: Pointer to the AP device
746 * Flush all requests from the request/pending queue of an AP device.
748 static void __ap_flush_queue(struct ap_device *ap_dev)
750 struct ap_message *ap_msg, *next;
752 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
753 list_del_init(&ap_msg->list);
754 ap_dev->pendingq_count--;
755 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
757 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
758 list_del_init(&ap_msg->list);
759 ap_dev->requestq_count--;
760 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
764 void ap_flush_queue(struct ap_device *ap_dev)
766 spin_lock_bh(&ap_dev->lock);
767 __ap_flush_queue(ap_dev);
768 spin_unlock_bh(&ap_dev->lock);
770 EXPORT_SYMBOL(ap_flush_queue);
772 static int ap_device_remove(struct device *dev)
774 struct ap_device *ap_dev = to_ap_dev(dev);
775 struct ap_driver *ap_drv = ap_dev->drv;
777 ap_flush_queue(ap_dev);
778 del_timer_sync(&ap_dev->timeout);
779 spin_lock_bh(&ap_device_list_lock);
780 list_del_init(&ap_dev->list);
781 spin_unlock_bh(&ap_device_list_lock);
782 if (ap_drv->remove)
783 ap_drv->remove(ap_dev);
784 spin_lock_bh(&ap_dev->lock);
785 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
786 spin_unlock_bh(&ap_dev->lock);
787 return 0;
790 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
791 char *name)
793 struct device_driver *drv = &ap_drv->driver;
795 drv->bus = &ap_bus_type;
796 drv->probe = ap_device_probe;
797 drv->remove = ap_device_remove;
798 drv->owner = owner;
799 drv->name = name;
800 return driver_register(drv);
802 EXPORT_SYMBOL(ap_driver_register);
804 void ap_driver_unregister(struct ap_driver *ap_drv)
806 driver_unregister(&ap_drv->driver);
808 EXPORT_SYMBOL(ap_driver_unregister);
811 * AP bus attributes.
813 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
815 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
818 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
820 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
822 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
825 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
827 return snprintf(buf, PAGE_SIZE, "%d\n",
828 ap_using_interrupts() ? 1 : 0);
831 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
833 static ssize_t ap_config_time_store(struct bus_type *bus,
834 const char *buf, size_t count)
836 int time;
838 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
839 return -EINVAL;
840 ap_config_time = time;
841 if (!timer_pending(&ap_config_timer) ||
842 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
843 ap_config_timer.expires = jiffies + ap_config_time * HZ;
844 add_timer(&ap_config_timer);
846 return count;
849 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
851 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
853 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
856 static ssize_t ap_poll_thread_store(struct bus_type *bus,
857 const char *buf, size_t count)
859 int flag, rc;
861 if (sscanf(buf, "%d\n", &flag) != 1)
862 return -EINVAL;
863 if (flag) {
864 rc = ap_poll_thread_start();
865 if (rc)
866 return rc;
868 else
869 ap_poll_thread_stop();
870 return count;
873 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
875 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
877 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
880 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
881 size_t count)
883 unsigned long long time;
884 ktime_t hr_time;
886 /* 120 seconds = maximum poll interval */
887 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
888 time > 120000000000ULL)
889 return -EINVAL;
890 poll_timeout = time;
891 hr_time = ktime_set(0, poll_timeout);
893 if (!hrtimer_is_queued(&ap_poll_timer) ||
894 !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
895 hrtimer_set_expires(&ap_poll_timer, hr_time);
896 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
898 return count;
901 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
903 static struct bus_attribute *const ap_bus_attrs[] = {
904 &bus_attr_ap_domain,
905 &bus_attr_config_time,
906 &bus_attr_poll_thread,
907 &bus_attr_ap_interrupts,
908 &bus_attr_poll_timeout,
909 NULL,
913 * ap_select_domain(): Select an AP domain.
915 * Pick one of the 16 AP domains.
917 static int ap_select_domain(void)
919 int queue_depth, device_type, count, max_count, best_domain;
920 int rc, i, j;
923 * We want to use a single domain. Either the one specified with
924 * the "domain=" parameter or the domain with the maximum number
925 * of devices.
927 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
928 /* Domain has already been selected. */
929 return 0;
930 best_domain = -1;
931 max_count = 0;
932 for (i = 0; i < AP_DOMAINS; i++) {
933 count = 0;
934 for (j = 0; j < AP_DEVICES; j++) {
935 ap_qid_t qid = AP_MKQID(j, i);
936 rc = ap_query_queue(qid, &queue_depth, &device_type);
937 if (rc)
938 continue;
939 count++;
941 if (count > max_count) {
942 max_count = count;
943 best_domain = i;
946 if (best_domain >= 0){
947 ap_domain_index = best_domain;
948 return 0;
950 return -ENODEV;
954 * ap_probe_device_type(): Find the device type of an AP.
955 * @ap_dev: pointer to the AP device.
957 * Find the device type if query queue returned a device type of 0.
959 static int ap_probe_device_type(struct ap_device *ap_dev)
961 static unsigned char msg[] = {
962 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
963 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
964 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
965 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
966 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
967 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
968 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
969 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
970 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
971 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
972 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
973 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
974 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
975 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
976 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
977 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
978 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
979 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
980 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
981 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
982 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
983 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
984 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
985 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
986 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
987 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
988 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
989 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
990 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
991 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
992 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
993 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
994 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
995 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
996 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
997 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
998 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
999 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1000 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1001 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1002 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1003 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1004 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1005 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1006 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1008 struct ap_queue_status status;
1009 unsigned long long psmid;
1010 char *reply;
1011 int rc, i;
1013 reply = (void *) get_zeroed_page(GFP_KERNEL);
1014 if (!reply) {
1015 rc = -ENOMEM;
1016 goto out;
1019 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1020 msg, sizeof(msg), 0);
1021 if (status.response_code != AP_RESPONSE_NORMAL) {
1022 rc = -ENODEV;
1023 goto out_free;
1026 /* Wait for the test message to complete. */
1027 for (i = 0; i < 6; i++) {
1028 mdelay(300);
1029 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1030 if (status.response_code == AP_RESPONSE_NORMAL &&
1031 psmid == 0x0102030405060708ULL)
1032 break;
1034 if (i < 6) {
1035 /* Got an answer. */
1036 if (reply[0] == 0x00 && reply[1] == 0x86)
1037 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1038 else
1039 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1040 rc = 0;
1041 } else
1042 rc = -ENODEV;
1044 out_free:
1045 free_page((unsigned long) reply);
1046 out:
1047 return rc;
1050 static void ap_interrupt_handler(void *unused1, void *unused2)
1052 tasklet_schedule(&ap_tasklet);
1056 * __ap_scan_bus(): Scan the AP bus.
1057 * @dev: Pointer to device
1058 * @data: Pointer to data
1060 * Scan the AP bus for new devices.
1062 static int __ap_scan_bus(struct device *dev, void *data)
1064 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1067 static void ap_device_release(struct device *dev)
1069 struct ap_device *ap_dev = to_ap_dev(dev);
1071 kfree(ap_dev);
1074 static void ap_scan_bus(struct work_struct *unused)
1076 struct ap_device *ap_dev;
1077 struct device *dev;
1078 ap_qid_t qid;
1079 int queue_depth, device_type;
1080 int rc, i;
1082 if (ap_select_domain() != 0)
1083 return;
1084 for (i = 0; i < AP_DEVICES; i++) {
1085 qid = AP_MKQID(i, ap_domain_index);
1086 dev = bus_find_device(&ap_bus_type, NULL,
1087 (void *)(unsigned long)qid,
1088 __ap_scan_bus);
1089 rc = ap_query_queue(qid, &queue_depth, &device_type);
1090 if (dev) {
1091 if (rc == -EBUSY) {
1092 set_current_state(TASK_UNINTERRUPTIBLE);
1093 schedule_timeout(AP_RESET_TIMEOUT);
1094 rc = ap_query_queue(qid, &queue_depth,
1095 &device_type);
1097 ap_dev = to_ap_dev(dev);
1098 spin_lock_bh(&ap_dev->lock);
1099 if (rc || ap_dev->unregistered) {
1100 spin_unlock_bh(&ap_dev->lock);
1101 if (ap_dev->unregistered)
1102 i--;
1103 device_unregister(dev);
1104 put_device(dev);
1105 continue;
1107 spin_unlock_bh(&ap_dev->lock);
1108 put_device(dev);
1109 continue;
1111 if (rc)
1112 continue;
1113 rc = ap_init_queue(qid);
1114 if (rc)
1115 continue;
1116 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1117 if (!ap_dev)
1118 break;
1119 ap_dev->qid = qid;
1120 ap_dev->queue_depth = queue_depth;
1121 ap_dev->unregistered = 1;
1122 spin_lock_init(&ap_dev->lock);
1123 INIT_LIST_HEAD(&ap_dev->pendingq);
1124 INIT_LIST_HEAD(&ap_dev->requestq);
1125 INIT_LIST_HEAD(&ap_dev->list);
1126 setup_timer(&ap_dev->timeout, ap_request_timeout,
1127 (unsigned long) ap_dev);
1128 if (device_type == 0)
1129 ap_probe_device_type(ap_dev);
1130 else
1131 ap_dev->device_type = device_type;
1133 ap_dev->device.bus = &ap_bus_type;
1134 ap_dev->device.parent = ap_root_device;
1135 if (dev_set_name(&ap_dev->device, "card%02x",
1136 AP_QID_DEVICE(ap_dev->qid))) {
1137 kfree(ap_dev);
1138 continue;
1140 ap_dev->device.release = ap_device_release;
1141 rc = device_register(&ap_dev->device);
1142 if (rc) {
1143 put_device(&ap_dev->device);
1144 continue;
1146 /* Add device attributes. */
1147 rc = sysfs_create_group(&ap_dev->device.kobj,
1148 &ap_dev_attr_group);
1149 if (!rc) {
1150 spin_lock_bh(&ap_dev->lock);
1151 ap_dev->unregistered = 0;
1152 spin_unlock_bh(&ap_dev->lock);
1154 else
1155 device_unregister(&ap_dev->device);
1159 static void
1160 ap_config_timeout(unsigned long ptr)
1162 queue_work(ap_work_queue, &ap_config_work);
1163 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1164 add_timer(&ap_config_timer);
1168 * ap_schedule_poll_timer(): Schedule poll timer.
1170 * Set up the timer to run the poll tasklet
1172 static inline void ap_schedule_poll_timer(void)
1174 ktime_t hr_time;
1176 spin_lock_bh(&ap_poll_timer_lock);
1177 if (ap_using_interrupts() || ap_suspend_flag)
1178 goto out;
1179 if (hrtimer_is_queued(&ap_poll_timer))
1180 goto out;
1181 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1182 hr_time = ktime_set(0, poll_timeout);
1183 hrtimer_forward_now(&ap_poll_timer, hr_time);
1184 hrtimer_restart(&ap_poll_timer);
1186 out:
1187 spin_unlock_bh(&ap_poll_timer_lock);
1191 * ap_poll_read(): Receive pending reply messages from an AP device.
1192 * @ap_dev: pointer to the AP device
1193 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1194 * required, bit 2^1 is set if the poll timer needs to get armed
1196 * Returns 0 if the device is still present, -ENODEV if not.
1198 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1200 struct ap_queue_status status;
1201 struct ap_message *ap_msg;
1203 if (ap_dev->queue_count <= 0)
1204 return 0;
1205 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1206 ap_dev->reply->message, ap_dev->reply->length);
1207 switch (status.response_code) {
1208 case AP_RESPONSE_NORMAL:
1209 atomic_dec(&ap_poll_requests);
1210 ap_decrease_queue_count(ap_dev);
1211 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1212 if (ap_msg->psmid != ap_dev->reply->psmid)
1213 continue;
1214 list_del_init(&ap_msg->list);
1215 ap_dev->pendingq_count--;
1216 ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
1217 break;
1219 if (ap_dev->queue_count > 0)
1220 *flags |= 1;
1221 break;
1222 case AP_RESPONSE_NO_PENDING_REPLY:
1223 if (status.queue_empty) {
1224 /* The card shouldn't forget requests but who knows. */
1225 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1226 ap_dev->queue_count = 0;
1227 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1228 ap_dev->requestq_count += ap_dev->pendingq_count;
1229 ap_dev->pendingq_count = 0;
1230 } else
1231 *flags |= 2;
1232 break;
1233 default:
1234 return -ENODEV;
1236 return 0;
1240 * ap_poll_write(): Send messages from the request queue to an AP device.
1241 * @ap_dev: pointer to the AP device
1242 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1243 * required, bit 2^1 is set if the poll timer needs to get armed
1245 * Returns 0 if the device is still present, -ENODEV if not.
1247 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1249 struct ap_queue_status status;
1250 struct ap_message *ap_msg;
1252 if (ap_dev->requestq_count <= 0 ||
1253 ap_dev->queue_count >= ap_dev->queue_depth)
1254 return 0;
1255 /* Start the next request on the queue. */
1256 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1257 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1258 ap_msg->message, ap_msg->length, ap_msg->special);
1259 switch (status.response_code) {
1260 case AP_RESPONSE_NORMAL:
1261 atomic_inc(&ap_poll_requests);
1262 ap_increase_queue_count(ap_dev);
1263 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1264 ap_dev->requestq_count--;
1265 ap_dev->pendingq_count++;
1266 if (ap_dev->queue_count < ap_dev->queue_depth &&
1267 ap_dev->requestq_count > 0)
1268 *flags |= 1;
1269 *flags |= 2;
1270 break;
1271 case AP_RESPONSE_Q_FULL:
1272 case AP_RESPONSE_RESET_IN_PROGRESS:
1273 *flags |= 2;
1274 break;
1275 case AP_RESPONSE_MESSAGE_TOO_BIG:
1276 case AP_RESPONSE_REQ_FAC_NOT_INST:
1277 return -EINVAL;
1278 default:
1279 return -ENODEV;
1281 return 0;
1285 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1286 * @ap_dev: pointer to the bus device
1287 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1288 * required, bit 2^1 is set if the poll timer needs to get armed
1290 * Poll AP device for pending replies and send new messages. If either
1291 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1292 * Returns 0.
1294 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1296 int rc;
1298 rc = ap_poll_read(ap_dev, flags);
1299 if (rc)
1300 return rc;
1301 return ap_poll_write(ap_dev, flags);
1305 * __ap_queue_message(): Queue a message to a device.
1306 * @ap_dev: pointer to the AP device
1307 * @ap_msg: the message to be queued
1309 * Queue a message to a device. Returns 0 if successful.
1311 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1313 struct ap_queue_status status;
1315 if (list_empty(&ap_dev->requestq) &&
1316 ap_dev->queue_count < ap_dev->queue_depth) {
1317 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1318 ap_msg->message, ap_msg->length,
1319 ap_msg->special);
1320 switch (status.response_code) {
1321 case AP_RESPONSE_NORMAL:
1322 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1323 atomic_inc(&ap_poll_requests);
1324 ap_dev->pendingq_count++;
1325 ap_increase_queue_count(ap_dev);
1326 ap_dev->total_request_count++;
1327 break;
1328 case AP_RESPONSE_Q_FULL:
1329 case AP_RESPONSE_RESET_IN_PROGRESS:
1330 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1331 ap_dev->requestq_count++;
1332 ap_dev->total_request_count++;
1333 return -EBUSY;
1334 case AP_RESPONSE_REQ_FAC_NOT_INST:
1335 case AP_RESPONSE_MESSAGE_TOO_BIG:
1336 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1337 return -EINVAL;
1338 default: /* Device is gone. */
1339 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1340 return -ENODEV;
1342 } else {
1343 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1344 ap_dev->requestq_count++;
1345 ap_dev->total_request_count++;
1346 return -EBUSY;
1348 ap_schedule_poll_timer();
1349 return 0;
1352 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1354 unsigned long flags;
1355 int rc;
1357 spin_lock_bh(&ap_dev->lock);
1358 if (!ap_dev->unregistered) {
1359 /* Make room on the queue by polling for finished requests. */
1360 rc = ap_poll_queue(ap_dev, &flags);
1361 if (!rc)
1362 rc = __ap_queue_message(ap_dev, ap_msg);
1363 if (!rc)
1364 wake_up(&ap_poll_wait);
1365 if (rc == -ENODEV)
1366 ap_dev->unregistered = 1;
1367 } else {
1368 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1369 rc = -ENODEV;
1371 spin_unlock_bh(&ap_dev->lock);
1372 if (rc == -ENODEV)
1373 device_unregister(&ap_dev->device);
1375 EXPORT_SYMBOL(ap_queue_message);
1378 * ap_cancel_message(): Cancel a crypto request.
1379 * @ap_dev: The AP device that has the message queued
1380 * @ap_msg: The message that is to be removed
1382 * Cancel a crypto request. This is done by removing the request
1383 * from the device pending or request queue. Note that the
1384 * request stays on the AP queue. When it finishes the message
1385 * reply will be discarded because the psmid can't be found.
1387 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1389 struct ap_message *tmp;
1391 spin_lock_bh(&ap_dev->lock);
1392 if (!list_empty(&ap_msg->list)) {
1393 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1394 if (tmp->psmid == ap_msg->psmid) {
1395 ap_dev->pendingq_count--;
1396 goto found;
1398 ap_dev->requestq_count--;
1399 found:
1400 list_del_init(&ap_msg->list);
1402 spin_unlock_bh(&ap_dev->lock);
1404 EXPORT_SYMBOL(ap_cancel_message);
1407 * ap_poll_timeout(): AP receive polling for finished AP requests.
1408 * @unused: Unused pointer.
1410 * Schedules the AP tasklet using a high resolution timer.
1412 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1414 tasklet_schedule(&ap_tasklet);
1415 return HRTIMER_NORESTART;
1419 * ap_reset(): Reset a not responding AP device.
1420 * @ap_dev: Pointer to the AP device
1422 * Reset a not responding AP device and move all requests from the
1423 * pending queue to the request queue.
1425 static void ap_reset(struct ap_device *ap_dev)
1427 int rc;
1429 ap_dev->reset = AP_RESET_IGNORE;
1430 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1431 ap_dev->queue_count = 0;
1432 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1433 ap_dev->requestq_count += ap_dev->pendingq_count;
1434 ap_dev->pendingq_count = 0;
1435 rc = ap_init_queue(ap_dev->qid);
1436 if (rc == -ENODEV)
1437 ap_dev->unregistered = 1;
1440 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1442 if (!ap_dev->unregistered) {
1443 if (ap_poll_queue(ap_dev, flags))
1444 ap_dev->unregistered = 1;
1445 if (ap_dev->reset == AP_RESET_DO)
1446 ap_reset(ap_dev);
1448 return 0;
1452 * ap_poll_all(): Poll all AP devices.
1453 * @dummy: Unused variable
1455 * Poll all AP devices on the bus in a round robin fashion. Continue
1456 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1457 * of the control flags has been set arm the poll timer.
1459 static void ap_poll_all(unsigned long dummy)
1461 unsigned long flags;
1462 struct ap_device *ap_dev;
1464 /* Reset the indicator if interrupts are used. Thus new interrupts can
1465 * be received. Doing it in the beginning of the tasklet is therefor
1466 * important that no requests on any AP get lost.
1468 if (ap_using_interrupts())
1469 xchg((u8 *)ap_interrupt_indicator, 0);
1470 do {
1471 flags = 0;
1472 spin_lock(&ap_device_list_lock);
1473 list_for_each_entry(ap_dev, &ap_device_list, list) {
1474 spin_lock(&ap_dev->lock);
1475 __ap_poll_device(ap_dev, &flags);
1476 spin_unlock(&ap_dev->lock);
1478 spin_unlock(&ap_device_list_lock);
1479 } while (flags & 1);
1480 if (flags & 2)
1481 ap_schedule_poll_timer();
1485 * ap_poll_thread(): Thread that polls for finished requests.
1486 * @data: Unused pointer
1488 * AP bus poll thread. The purpose of this thread is to poll for
1489 * finished requests in a loop if there is a "free" cpu - that is
1490 * a cpu that doesn't have anything better to do. The polling stops
1491 * as soon as there is another task or if all messages have been
1492 * delivered.
1494 static int ap_poll_thread(void *data)
1496 DECLARE_WAITQUEUE(wait, current);
1497 unsigned long flags;
1498 int requests;
1499 struct ap_device *ap_dev;
1501 set_user_nice(current, 19);
1502 while (1) {
1503 if (ap_suspend_flag)
1504 return 0;
1505 if (need_resched()) {
1506 schedule();
1507 continue;
1509 add_wait_queue(&ap_poll_wait, &wait);
1510 set_current_state(TASK_INTERRUPTIBLE);
1511 if (kthread_should_stop())
1512 break;
1513 requests = atomic_read(&ap_poll_requests);
1514 if (requests <= 0)
1515 schedule();
1516 set_current_state(TASK_RUNNING);
1517 remove_wait_queue(&ap_poll_wait, &wait);
1519 flags = 0;
1520 spin_lock_bh(&ap_device_list_lock);
1521 list_for_each_entry(ap_dev, &ap_device_list, list) {
1522 spin_lock(&ap_dev->lock);
1523 __ap_poll_device(ap_dev, &flags);
1524 spin_unlock(&ap_dev->lock);
1526 spin_unlock_bh(&ap_device_list_lock);
1528 set_current_state(TASK_RUNNING);
1529 remove_wait_queue(&ap_poll_wait, &wait);
1530 return 0;
1533 static int ap_poll_thread_start(void)
1535 int rc;
1537 if (ap_using_interrupts() || ap_suspend_flag)
1538 return 0;
1539 mutex_lock(&ap_poll_thread_mutex);
1540 if (!ap_poll_kthread) {
1541 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1542 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1543 if (rc)
1544 ap_poll_kthread = NULL;
1546 else
1547 rc = 0;
1548 mutex_unlock(&ap_poll_thread_mutex);
1549 return rc;
1552 static void ap_poll_thread_stop(void)
1554 mutex_lock(&ap_poll_thread_mutex);
1555 if (ap_poll_kthread) {
1556 kthread_stop(ap_poll_kthread);
1557 ap_poll_kthread = NULL;
1559 mutex_unlock(&ap_poll_thread_mutex);
1563 * ap_request_timeout(): Handling of request timeouts
1564 * @data: Holds the AP device.
1566 * Handles request timeouts.
1568 static void ap_request_timeout(unsigned long data)
1570 struct ap_device *ap_dev = (struct ap_device *) data;
1572 if (ap_dev->reset == AP_RESET_ARMED) {
1573 ap_dev->reset = AP_RESET_DO;
1575 if (ap_using_interrupts())
1576 tasklet_schedule(&ap_tasklet);
1580 static void ap_reset_domain(void)
1582 int i;
1584 if (ap_domain_index != -1)
1585 for (i = 0; i < AP_DEVICES; i++)
1586 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1589 static void ap_reset_all(void)
1591 int i, j;
1593 for (i = 0; i < AP_DOMAINS; i++)
1594 for (j = 0; j < AP_DEVICES; j++)
1595 ap_reset_queue(AP_MKQID(j, i));
1598 static struct reset_call ap_reset_call = {
1599 .fn = ap_reset_all,
1603 * ap_module_init(): The module initialization code.
1605 * Initializes the module.
1607 int __init ap_module_init(void)
1609 int rc, i;
1611 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1612 pr_warning("%d is not a valid cryptographic domain\n",
1613 ap_domain_index);
1614 return -EINVAL;
1616 /* In resume callback we need to know if the user had set the domain.
1617 * If so, we can not just reset it.
1619 if (ap_domain_index >= 0)
1620 user_set_domain = 1;
1622 if (ap_instructions_available() != 0) {
1623 pr_warning("The hardware system does not support "
1624 "AP instructions\n");
1625 return -ENODEV;
1627 if (ap_interrupts_available()) {
1628 isc_register(AP_ISC);
1629 ap_interrupt_indicator = s390_register_adapter_interrupt(
1630 &ap_interrupt_handler, NULL, AP_ISC);
1631 if (IS_ERR(ap_interrupt_indicator)) {
1632 ap_interrupt_indicator = NULL;
1633 isc_unregister(AP_ISC);
1637 register_reset_call(&ap_reset_call);
1639 /* Create /sys/bus/ap. */
1640 rc = bus_register(&ap_bus_type);
1641 if (rc)
1642 goto out;
1643 for (i = 0; ap_bus_attrs[i]; i++) {
1644 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1645 if (rc)
1646 goto out_bus;
1649 /* Create /sys/devices/ap. */
1650 ap_root_device = root_device_register("ap");
1651 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1652 if (rc)
1653 goto out_bus;
1655 ap_work_queue = create_singlethread_workqueue("kapwork");
1656 if (!ap_work_queue) {
1657 rc = -ENOMEM;
1658 goto out_root;
1661 if (ap_select_domain() == 0)
1662 ap_scan_bus(NULL);
1664 /* Setup the AP bus rescan timer. */
1665 init_timer(&ap_config_timer);
1666 ap_config_timer.function = ap_config_timeout;
1667 ap_config_timer.data = 0;
1668 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1669 add_timer(&ap_config_timer);
1671 /* Setup the high resultion poll timer.
1672 * If we are running under z/VM adjust polling to z/VM polling rate.
1674 if (MACHINE_IS_VM)
1675 poll_timeout = 1500000;
1676 spin_lock_init(&ap_poll_timer_lock);
1677 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1678 ap_poll_timer.function = ap_poll_timeout;
1680 /* Start the low priority AP bus poll thread. */
1681 if (ap_thread_flag) {
1682 rc = ap_poll_thread_start();
1683 if (rc)
1684 goto out_work;
1687 return 0;
1689 out_work:
1690 del_timer_sync(&ap_config_timer);
1691 hrtimer_cancel(&ap_poll_timer);
1692 destroy_workqueue(ap_work_queue);
1693 out_root:
1694 root_device_unregister(ap_root_device);
1695 out_bus:
1696 while (i--)
1697 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1698 bus_unregister(&ap_bus_type);
1699 out:
1700 unregister_reset_call(&ap_reset_call);
1701 if (ap_using_interrupts()) {
1702 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1703 isc_unregister(AP_ISC);
1705 return rc;
1708 static int __ap_match_all(struct device *dev, void *data)
1710 return 1;
1714 * ap_modules_exit(): The module termination code
1716 * Terminates the module.
1718 void ap_module_exit(void)
1720 int i;
1721 struct device *dev;
1723 ap_reset_domain();
1724 ap_poll_thread_stop();
1725 del_timer_sync(&ap_config_timer);
1726 hrtimer_cancel(&ap_poll_timer);
1727 destroy_workqueue(ap_work_queue);
1728 tasklet_kill(&ap_tasklet);
1729 root_device_unregister(ap_root_device);
1730 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1731 __ap_match_all)))
1733 device_unregister(dev);
1734 put_device(dev);
1736 for (i = 0; ap_bus_attrs[i]; i++)
1737 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1738 bus_unregister(&ap_bus_type);
1739 unregister_reset_call(&ap_reset_call);
1740 if (ap_using_interrupts()) {
1741 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1742 isc_unregister(AP_ISC);
1746 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1747 module_init(ap_module_init);
1748 module_exit(ap_module_exit);
1749 #endif