a6aee68d290917d59bffcb840e1dbc568b2c8d77
[linux-3.10.git] / drivers / s390 / crypto / ap_bus.c
1 /*
2  * linux/drivers/s390/crypto/ap_bus.c
3  *
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>
9  *
10  * Adjunct processor bus.
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/err.h>
31 #include <linux/interrupt.h>
32 #include <linux/workqueue.h>
33 #include <linux/notifier.h>
34 #include <linux/kthread.h>
35 #include <linux/mutex.h>
36 #include <asm/s390_rdev.h>
37 #include <asm/reset.h>
38 #include <asm/airq.h>
39 #include <asm/atomic.h>
40 #include <asm/system.h>
41 #include <asm/isc.h>
42 #include <linux/hrtimer.h>
43 #include <linux/ktime.h>
44
45 #include "ap_bus.h"
46
47 /* Some prototypes. */
48 static void ap_scan_bus(struct work_struct *);
49 static void ap_poll_all(unsigned long);
50 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
51 static int ap_poll_thread_start(void);
52 static void ap_poll_thread_stop(void);
53 static void ap_request_timeout(unsigned long);
54 static inline void ap_schedule_poll_timer(void);
55
56 /*
57  * Module description.
58  */
59 MODULE_AUTHOR("IBM Corporation");
60 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
61                    "Copyright 2006 IBM Corporation");
62 MODULE_LICENSE("GPL");
63
64 /*
65  * Module parameter
66  */
67 int ap_domain_index = -1;       /* Adjunct Processor Domain Index */
68 module_param_named(domain, ap_domain_index, int, 0000);
69 MODULE_PARM_DESC(domain, "domain index for ap devices");
70 EXPORT_SYMBOL(ap_domain_index);
71
72 static int ap_thread_flag = 0;
73 module_param_named(poll_thread, ap_thread_flag, int, 0000);
74 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
75
76 static struct device *ap_root_device = NULL;
77 static DEFINE_SPINLOCK(ap_device_list_lock);
78 static LIST_HEAD(ap_device_list);
79
80 /*
81  * Workqueue & timer for bus rescan.
82  */
83 static struct workqueue_struct *ap_work_queue;
84 static struct timer_list ap_config_timer;
85 static int ap_config_time = AP_CONFIG_TIME;
86 static DECLARE_WORK(ap_config_work, ap_scan_bus);
87
88 /*
89  * Tasklet & timer for AP request polling and interrupts
90  */
91 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
92 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
93 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
94 static struct task_struct *ap_poll_kthread = NULL;
95 static DEFINE_MUTEX(ap_poll_thread_mutex);
96 static void *ap_interrupt_indicator;
97 static struct hrtimer ap_poll_timer;
98 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
99  * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
100 static unsigned long long poll_timeout = 250000;
101
102 /**
103  * ap_using_interrupts() - Returns non-zero if interrupt support is
104  * available.
105  */
106 static inline int ap_using_interrupts(void)
107 {
108         return ap_interrupt_indicator != NULL;
109 }
110
111 /**
112  * ap_intructions_available() - Test if AP instructions are available.
113  *
114  * Returns 0 if the AP instructions are installed.
115  */
116 static inline int ap_instructions_available(void)
117 {
118         register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
119         register unsigned long reg1 asm ("1") = -ENODEV;
120         register unsigned long reg2 asm ("2") = 0UL;
121
122         asm volatile(
123                 "   .long 0xb2af0000\n"         /* PQAP(TAPQ) */
124                 "0: la    %1,0\n"
125                 "1:\n"
126                 EX_TABLE(0b, 1b)
127                 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
128         return reg1;
129 }
130
131 /**
132  * ap_interrupts_available(): Test if AP interrupts are available.
133  *
134  * Returns 1 if AP interrupts are available.
135  */
136 static int ap_interrupts_available(void)
137 {
138         unsigned long long facility_bits[2];
139
140         if (stfle(facility_bits, 2) <= 1)
141                 return 0;
142         if (!(facility_bits[0] & (1ULL << 61)) ||
143             !(facility_bits[1] & (1ULL << 62)))
144                 return 0;
145         return 1;
146 }
147
148 /**
149  * ap_test_queue(): Test adjunct processor queue.
150  * @qid: The AP queue number
151  * @queue_depth: Pointer to queue depth value
152  * @device_type: Pointer to device type value
153  *
154  * Returns AP queue status structure.
155  */
156 static inline struct ap_queue_status
157 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
158 {
159         register unsigned long reg0 asm ("0") = qid;
160         register struct ap_queue_status reg1 asm ("1");
161         register unsigned long reg2 asm ("2") = 0UL;
162
163         asm volatile(".long 0xb2af0000"         /* PQAP(TAPQ) */
164                      : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
165         *device_type = (int) (reg2 >> 24);
166         *queue_depth = (int) (reg2 & 0xff);
167         return reg1;
168 }
169
170 /**
171  * ap_reset_queue(): Reset adjunct processor queue.
172  * @qid: The AP queue number
173  *
174  * Returns AP queue status structure.
175  */
176 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
177 {
178         register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
179         register struct ap_queue_status reg1 asm ("1");
180         register unsigned long reg2 asm ("2") = 0UL;
181
182         asm volatile(
183                 ".long 0xb2af0000"              /* PQAP(RAPQ) */
184                 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
185         return reg1;
186 }
187
188 #ifdef CONFIG_64BIT
189 /**
190  * ap_queue_interruption_control(): Enable interruption for a specific AP.
191  * @qid: The AP queue number
192  * @ind: The notification indicator byte
193  *
194  * Returns AP queue status.
195  */
196 static inline struct ap_queue_status
197 ap_queue_interruption_control(ap_qid_t qid, void *ind)
198 {
199         register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
200         register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
201         register struct ap_queue_status reg1_out asm ("1");
202         register void *reg2 asm ("2") = ind;
203         asm volatile(
204                 ".long 0xb2af0000"              /* PQAP(RAPQ) */
205                 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
206                 :
207                 : "cc" );
208         return reg1_out;
209 }
210 #endif
211
212 /**
213  * ap_queue_enable_interruption(): Enable interruption on an AP.
214  * @qid: The AP queue number
215  * @ind: the notification indicator byte
216  *
217  * Enables interruption on AP queue via ap_queue_interruption_control(). Based
218  * on the return value it waits a while and tests the AP queue if interrupts
219  * have been switched on using ap_test_queue().
220  */
221 static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
222 {
223 #ifdef CONFIG_64BIT
224         struct ap_queue_status status;
225         int t_depth, t_device_type, rc, i;
226
227         rc = -EBUSY;
228         status = ap_queue_interruption_control(qid, ind);
229
230         for (i = 0; i < AP_MAX_RESET; i++) {
231                 switch (status.response_code) {
232                 case AP_RESPONSE_NORMAL:
233                         if (status.int_enabled)
234                                 return 0;
235                         break;
236                 case AP_RESPONSE_RESET_IN_PROGRESS:
237                 case AP_RESPONSE_BUSY:
238                         break;
239                 case AP_RESPONSE_Q_NOT_AVAIL:
240                 case AP_RESPONSE_DECONFIGURED:
241                 case AP_RESPONSE_CHECKSTOPPED:
242                 case AP_RESPONSE_INVALID_ADDRESS:
243                         return -ENODEV;
244                 case AP_RESPONSE_OTHERWISE_CHANGED:
245                         if (status.int_enabled)
246                                 return 0;
247                         break;
248                 default:
249                         break;
250                 }
251                 if (i < AP_MAX_RESET - 1) {
252                         udelay(5);
253                         status = ap_test_queue(qid, &t_depth, &t_device_type);
254                 }
255         }
256         return rc;
257 #else
258         return -EINVAL;
259 #endif
260 }
261
262 /**
263  * __ap_send(): Send message to adjunct processor queue.
264  * @qid: The AP queue number
265  * @psmid: The program supplied message identifier
266  * @msg: The message text
267  * @length: The message length
268  *
269  * Returns AP queue status structure.
270  * Condition code 1 on NQAP can't happen because the L bit is 1.
271  * Condition code 2 on NQAP also means the send is incomplete,
272  * because a segment boundary was reached. The NQAP is repeated.
273  */
274 static inline struct ap_queue_status
275 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
276 {
277         typedef struct { char _[length]; } msgblock;
278         register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
279         register struct ap_queue_status reg1 asm ("1");
280         register unsigned long reg2 asm ("2") = (unsigned long) msg;
281         register unsigned long reg3 asm ("3") = (unsigned long) length;
282         register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
283         register unsigned long reg5 asm ("5") = (unsigned int) psmid;
284
285         asm volatile (
286                 "0: .long 0xb2ad0042\n"         /* DQAP */
287                 "   brc   2,0b"
288                 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
289                 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
290                 : "cc" );
291         return reg1;
292 }
293
294 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
295 {
296         struct ap_queue_status status;
297
298         status = __ap_send(qid, psmid, msg, length);
299         switch (status.response_code) {
300         case AP_RESPONSE_NORMAL:
301                 return 0;
302         case AP_RESPONSE_Q_FULL:
303         case AP_RESPONSE_RESET_IN_PROGRESS:
304                 return -EBUSY;
305         default:        /* Device is gone. */
306                 return -ENODEV;
307         }
308 }
309 EXPORT_SYMBOL(ap_send);
310
311 /**
312  * __ap_recv(): Receive message from adjunct processor queue.
313  * @qid: The AP queue number
314  * @psmid: Pointer to program supplied message identifier
315  * @msg: The message text
316  * @length: The message length
317  *
318  * Returns AP queue status structure.
319  * Condition code 1 on DQAP means the receive has taken place
320  * but only partially.  The response is incomplete, hence the
321  * DQAP is repeated.
322  * Condition code 2 on DQAP also means the receive is incomplete,
323  * this time because a segment boundary was reached. Again, the
324  * DQAP is repeated.
325  * Note that gpr2 is used by the DQAP instruction to keep track of
326  * any 'residual' length, in case the instruction gets interrupted.
327  * Hence it gets zeroed before the instruction.
328  */
329 static inline struct ap_queue_status
330 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
331 {
332         typedef struct { char _[length]; } msgblock;
333         register unsigned long reg0 asm("0") = qid | 0x80000000UL;
334         register struct ap_queue_status reg1 asm ("1");
335         register unsigned long reg2 asm("2") = 0UL;
336         register unsigned long reg4 asm("4") = (unsigned long) msg;
337         register unsigned long reg5 asm("5") = (unsigned long) length;
338         register unsigned long reg6 asm("6") = 0UL;
339         register unsigned long reg7 asm("7") = 0UL;
340
341
342         asm volatile(
343                 "0: .long 0xb2ae0064\n"
344                 "   brc   6,0b\n"
345                 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
346                 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
347                 "=m" (*(msgblock *) msg) : : "cc" );
348         *psmid = (((unsigned long long) reg6) << 32) + reg7;
349         return reg1;
350 }
351
352 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
353 {
354         struct ap_queue_status status;
355
356         status = __ap_recv(qid, psmid, msg, length);
357         switch (status.response_code) {
358         case AP_RESPONSE_NORMAL:
359                 return 0;
360         case AP_RESPONSE_NO_PENDING_REPLY:
361                 if (status.queue_empty)
362                         return -ENOENT;
363                 return -EBUSY;
364         case AP_RESPONSE_RESET_IN_PROGRESS:
365                 return -EBUSY;
366         default:
367                 return -ENODEV;
368         }
369 }
370 EXPORT_SYMBOL(ap_recv);
371
372 /**
373  * ap_query_queue(): Check if an AP queue is available.
374  * @qid: The AP queue number
375  * @queue_depth: Pointer to queue depth value
376  * @device_type: Pointer to device type value
377  *
378  * The test is repeated for AP_MAX_RESET times.
379  */
380 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
381 {
382         struct ap_queue_status status;
383         int t_depth, t_device_type, rc, i;
384
385         rc = -EBUSY;
386         for (i = 0; i < AP_MAX_RESET; i++) {
387                 status = ap_test_queue(qid, &t_depth, &t_device_type);
388                 switch (status.response_code) {
389                 case AP_RESPONSE_NORMAL:
390                         *queue_depth = t_depth + 1;
391                         *device_type = t_device_type;
392                         rc = 0;
393                         break;
394                 case AP_RESPONSE_Q_NOT_AVAIL:
395                         rc = -ENODEV;
396                         break;
397                 case AP_RESPONSE_RESET_IN_PROGRESS:
398                         break;
399                 case AP_RESPONSE_DECONFIGURED:
400                         rc = -ENODEV;
401                         break;
402                 case AP_RESPONSE_CHECKSTOPPED:
403                         rc = -ENODEV;
404                         break;
405                 case AP_RESPONSE_INVALID_ADDRESS:
406                         rc = -ENODEV;
407                         break;
408                 case AP_RESPONSE_OTHERWISE_CHANGED:
409                         break;
410                 case AP_RESPONSE_BUSY:
411                         break;
412                 default:
413                         BUG();
414                 }
415                 if (rc != -EBUSY)
416                         break;
417                 if (i < AP_MAX_RESET - 1)
418                         udelay(5);
419         }
420         return rc;
421 }
422
423 /**
424  * ap_init_queue(): Reset an AP queue.
425  * @qid: The AP queue number
426  *
427  * Reset an AP queue and wait for it to become available again.
428  */
429 static int ap_init_queue(ap_qid_t qid)
430 {
431         struct ap_queue_status status;
432         int rc, dummy, i;
433
434         rc = -ENODEV;
435         status = ap_reset_queue(qid);
436         for (i = 0; i < AP_MAX_RESET; i++) {
437                 switch (status.response_code) {
438                 case AP_RESPONSE_NORMAL:
439                         if (status.queue_empty)
440                                 rc = 0;
441                         break;
442                 case AP_RESPONSE_Q_NOT_AVAIL:
443                 case AP_RESPONSE_DECONFIGURED:
444                 case AP_RESPONSE_CHECKSTOPPED:
445                         i = AP_MAX_RESET;       /* return with -ENODEV */
446                         break;
447                 case AP_RESPONSE_RESET_IN_PROGRESS:
448                         rc = -EBUSY;
449                 case AP_RESPONSE_BUSY:
450                 default:
451                         break;
452                 }
453                 if (rc != -ENODEV && rc != -EBUSY)
454                         break;
455                 if (i < AP_MAX_RESET - 1) {
456                         udelay(5);
457                         status = ap_test_queue(qid, &dummy, &dummy);
458                 }
459         }
460         if (rc == 0 && ap_using_interrupts()) {
461                 rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
462                 /* If interruption mode is supported by the machine,
463                 * but an AP can not be enabled for interruption then
464                 * the AP will be discarded.    */
465                 if (rc)
466                         pr_err("Registering adapter interrupts for "
467                                "AP %d failed\n", AP_QID_DEVICE(qid));
468         }
469         return rc;
470 }
471
472 /**
473  * ap_increase_queue_count(): Arm request timeout.
474  * @ap_dev: Pointer to an AP device.
475  *
476  * Arm request timeout if an AP device was idle and a new request is submitted.
477  */
478 static void ap_increase_queue_count(struct ap_device *ap_dev)
479 {
480         int timeout = ap_dev->drv->request_timeout;
481
482         ap_dev->queue_count++;
483         if (ap_dev->queue_count == 1) {
484                 mod_timer(&ap_dev->timeout, jiffies + timeout);
485                 ap_dev->reset = AP_RESET_ARMED;
486         }
487 }
488
489 /**
490  * ap_decrease_queue_count(): Decrease queue count.
491  * @ap_dev: Pointer to an AP device.
492  *
493  * If AP device is still alive, re-schedule request timeout if there are still
494  * pending requests.
495  */
496 static void ap_decrease_queue_count(struct ap_device *ap_dev)
497 {
498         int timeout = ap_dev->drv->request_timeout;
499
500         ap_dev->queue_count--;
501         if (ap_dev->queue_count > 0)
502                 mod_timer(&ap_dev->timeout, jiffies + timeout);
503         else
504                 /*
505                  * The timeout timer should to be disabled now - since
506                  * del_timer_sync() is very expensive, we just tell via the
507                  * reset flag to ignore the pending timeout timer.
508                  */
509                 ap_dev->reset = AP_RESET_IGNORE;
510 }
511
512 /*
513  * AP device related attributes.
514  */
515 static ssize_t ap_hwtype_show(struct device *dev,
516                               struct device_attribute *attr, char *buf)
517 {
518         struct ap_device *ap_dev = to_ap_dev(dev);
519         return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
520 }
521
522 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
523 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
524                              char *buf)
525 {
526         struct ap_device *ap_dev = to_ap_dev(dev);
527         return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
528 }
529
530 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
531 static ssize_t ap_request_count_show(struct device *dev,
532                                      struct device_attribute *attr,
533                                      char *buf)
534 {
535         struct ap_device *ap_dev = to_ap_dev(dev);
536         int rc;
537
538         spin_lock_bh(&ap_dev->lock);
539         rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
540         spin_unlock_bh(&ap_dev->lock);
541         return rc;
542 }
543
544 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
545
546 static ssize_t ap_modalias_show(struct device *dev,
547                                 struct device_attribute *attr, char *buf)
548 {
549         return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
550 }
551
552 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
553
554 static struct attribute *ap_dev_attrs[] = {
555         &dev_attr_hwtype.attr,
556         &dev_attr_depth.attr,
557         &dev_attr_request_count.attr,
558         &dev_attr_modalias.attr,
559         NULL
560 };
561 static struct attribute_group ap_dev_attr_group = {
562         .attrs = ap_dev_attrs
563 };
564
565 /**
566  * ap_bus_match()
567  * @dev: Pointer to device
568  * @drv: Pointer to device_driver
569  *
570  * AP bus driver registration/unregistration.
571  */
572 static int ap_bus_match(struct device *dev, struct device_driver *drv)
573 {
574         struct ap_device *ap_dev = to_ap_dev(dev);
575         struct ap_driver *ap_drv = to_ap_drv(drv);
576         struct ap_device_id *id;
577
578         /*
579          * Compare device type of the device with the list of
580          * supported types of the device_driver.
581          */
582         for (id = ap_drv->ids; id->match_flags; id++) {
583                 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
584                     (id->dev_type != ap_dev->device_type))
585                         continue;
586                 return 1;
587         }
588         return 0;
589 }
590
591 /**
592  * ap_uevent(): Uevent function for AP devices.
593  * @dev: Pointer to device
594  * @env: Pointer to kobj_uevent_env
595  *
596  * It sets up a single environment variable DEV_TYPE which contains the
597  * hardware device type.
598  */
599 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
600 {
601         struct ap_device *ap_dev = to_ap_dev(dev);
602         int retval = 0;
603
604         if (!ap_dev)
605                 return -ENODEV;
606
607         /* Set up DEV_TYPE environment variable. */
608         retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
609         if (retval)
610                 return retval;
611
612         /* Add MODALIAS= */
613         retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
614
615         return retval;
616 }
617
618 static struct bus_type ap_bus_type = {
619         .name = "ap",
620         .match = &ap_bus_match,
621         .uevent = &ap_uevent,
622 };
623
624 static int ap_device_probe(struct device *dev)
625 {
626         struct ap_device *ap_dev = to_ap_dev(dev);
627         struct ap_driver *ap_drv = to_ap_drv(dev->driver);
628         int rc;
629
630         ap_dev->drv = ap_drv;
631         rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
632         if (!rc) {
633                 spin_lock_bh(&ap_device_list_lock);
634                 list_add(&ap_dev->list, &ap_device_list);
635                 spin_unlock_bh(&ap_device_list_lock);
636         }
637         return rc;
638 }
639
640 /**
641  * __ap_flush_queue(): Flush requests.
642  * @ap_dev: Pointer to the AP device
643  *
644  * Flush all requests from the request/pending queue of an AP device.
645  */
646 static void __ap_flush_queue(struct ap_device *ap_dev)
647 {
648         struct ap_message *ap_msg, *next;
649
650         list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
651                 list_del_init(&ap_msg->list);
652                 ap_dev->pendingq_count--;
653                 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
654         }
655         list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
656                 list_del_init(&ap_msg->list);
657                 ap_dev->requestq_count--;
658                 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
659         }
660 }
661
662 void ap_flush_queue(struct ap_device *ap_dev)
663 {
664         spin_lock_bh(&ap_dev->lock);
665         __ap_flush_queue(ap_dev);
666         spin_unlock_bh(&ap_dev->lock);
667 }
668 EXPORT_SYMBOL(ap_flush_queue);
669
670 static int ap_device_remove(struct device *dev)
671 {
672         struct ap_device *ap_dev = to_ap_dev(dev);
673         struct ap_driver *ap_drv = ap_dev->drv;
674
675         ap_flush_queue(ap_dev);
676         del_timer_sync(&ap_dev->timeout);
677         spin_lock_bh(&ap_device_list_lock);
678         list_del_init(&ap_dev->list);
679         spin_unlock_bh(&ap_device_list_lock);
680         if (ap_drv->remove)
681                 ap_drv->remove(ap_dev);
682         spin_lock_bh(&ap_dev->lock);
683         atomic_sub(ap_dev->queue_count, &ap_poll_requests);
684         spin_unlock_bh(&ap_dev->lock);
685         return 0;
686 }
687
688 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
689                        char *name)
690 {
691         struct device_driver *drv = &ap_drv->driver;
692
693         drv->bus = &ap_bus_type;
694         drv->probe = ap_device_probe;
695         drv->remove = ap_device_remove;
696         drv->owner = owner;
697         drv->name = name;
698         return driver_register(drv);
699 }
700 EXPORT_SYMBOL(ap_driver_register);
701
702 void ap_driver_unregister(struct ap_driver *ap_drv)
703 {
704         driver_unregister(&ap_drv->driver);
705 }
706 EXPORT_SYMBOL(ap_driver_unregister);
707
708 /*
709  * AP bus attributes.
710  */
711 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
712 {
713         return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
714 }
715
716 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
717
718 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
719 {
720         return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
721 }
722
723 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
724 {
725         return snprintf(buf, PAGE_SIZE, "%d\n",
726                         ap_using_interrupts() ? 1 : 0);
727 }
728
729 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
730
731 static ssize_t ap_config_time_store(struct bus_type *bus,
732                                     const char *buf, size_t count)
733 {
734         int time;
735
736         if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
737                 return -EINVAL;
738         ap_config_time = time;
739         if (!timer_pending(&ap_config_timer) ||
740             !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
741                 ap_config_timer.expires = jiffies + ap_config_time * HZ;
742                 add_timer(&ap_config_timer);
743         }
744         return count;
745 }
746
747 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
748
749 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
750 {
751         return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
752 }
753
754 static ssize_t ap_poll_thread_store(struct bus_type *bus,
755                                     const char *buf, size_t count)
756 {
757         int flag, rc;
758
759         if (sscanf(buf, "%d\n", &flag) != 1)
760                 return -EINVAL;
761         if (flag) {
762                 rc = ap_poll_thread_start();
763                 if (rc)
764                         return rc;
765         }
766         else
767                 ap_poll_thread_stop();
768         return count;
769 }
770
771 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
772
773 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
774 {
775         return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
776 }
777
778 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
779                                   size_t count)
780 {
781         unsigned long long time;
782         ktime_t hr_time;
783
784         /* 120 seconds = maximum poll interval */
785         if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
786             time > 120000000000ULL)
787                 return -EINVAL;
788         poll_timeout = time;
789         hr_time = ktime_set(0, poll_timeout);
790
791         if (!hrtimer_is_queued(&ap_poll_timer) ||
792             !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
793                 hrtimer_set_expires(&ap_poll_timer, hr_time);
794                 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
795         }
796         return count;
797 }
798
799 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
800
801 static struct bus_attribute *const ap_bus_attrs[] = {
802         &bus_attr_ap_domain,
803         &bus_attr_config_time,
804         &bus_attr_poll_thread,
805         &bus_attr_ap_interrupts,
806         &bus_attr_poll_timeout,
807         NULL,
808 };
809
810 /**
811  * ap_select_domain(): Select an AP domain.
812  *
813  * Pick one of the 16 AP domains.
814  */
815 static int ap_select_domain(void)
816 {
817         int queue_depth, device_type, count, max_count, best_domain;
818         int rc, i, j;
819
820         /*
821          * We want to use a single domain. Either the one specified with
822          * the "domain=" parameter or the domain with the maximum number
823          * of devices.
824          */
825         if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
826                 /* Domain has already been selected. */
827                 return 0;
828         best_domain = -1;
829         max_count = 0;
830         for (i = 0; i < AP_DOMAINS; i++) {
831                 count = 0;
832                 for (j = 0; j < AP_DEVICES; j++) {
833                         ap_qid_t qid = AP_MKQID(j, i);
834                         rc = ap_query_queue(qid, &queue_depth, &device_type);
835                         if (rc)
836                                 continue;
837                         count++;
838                 }
839                 if (count > max_count) {
840                         max_count = count;
841                         best_domain = i;
842                 }
843         }
844         if (best_domain >= 0){
845                 ap_domain_index = best_domain;
846                 return 0;
847         }
848         return -ENODEV;
849 }
850
851 /**
852  * ap_probe_device_type(): Find the device type of an AP.
853  * @ap_dev: pointer to the AP device.
854  *
855  * Find the device type if query queue returned a device type of 0.
856  */
857 static int ap_probe_device_type(struct ap_device *ap_dev)
858 {
859         static unsigned char msg[] = {
860                 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
861                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
862                 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
863                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
864                 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
865                 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
866                 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
867                 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
868                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
869                 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
870                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
871                 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
872                 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
873                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
874                 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
875                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
876                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
877                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
878                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
879                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
880                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
881                 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
882                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
883                 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
884                 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
885                 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
886                 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
887                 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
888                 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
889                 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
890                 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
891                 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
892                 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
893                 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
894                 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
895                 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
896                 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
897                 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
898                 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
899                 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
900                 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
901                 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
902                 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
903                 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
904                 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
905         };
906         struct ap_queue_status status;
907         unsigned long long psmid;
908         char *reply;
909         int rc, i;
910
911         reply = (void *) get_zeroed_page(GFP_KERNEL);
912         if (!reply) {
913                 rc = -ENOMEM;
914                 goto out;
915         }
916
917         status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
918                            msg, sizeof(msg));
919         if (status.response_code != AP_RESPONSE_NORMAL) {
920                 rc = -ENODEV;
921                 goto out_free;
922         }
923
924         /* Wait for the test message to complete. */
925         for (i = 0; i < 6; i++) {
926                 mdelay(300);
927                 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
928                 if (status.response_code == AP_RESPONSE_NORMAL &&
929                     psmid == 0x0102030405060708ULL)
930                         break;
931         }
932         if (i < 6) {
933                 /* Got an answer. */
934                 if (reply[0] == 0x00 && reply[1] == 0x86)
935                         ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
936                 else
937                         ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
938                 rc = 0;
939         } else
940                 rc = -ENODEV;
941
942 out_free:
943         free_page((unsigned long) reply);
944 out:
945         return rc;
946 }
947
948 static void ap_interrupt_handler(void *unused1, void *unused2)
949 {
950         tasklet_schedule(&ap_tasklet);
951 }
952
953 /**
954  * __ap_scan_bus(): Scan the AP bus.
955  * @dev: Pointer to device
956  * @data: Pointer to data
957  *
958  * Scan the AP bus for new devices.
959  */
960 static int __ap_scan_bus(struct device *dev, void *data)
961 {
962         return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
963 }
964
965 static void ap_device_release(struct device *dev)
966 {
967         struct ap_device *ap_dev = to_ap_dev(dev);
968
969         kfree(ap_dev);
970 }
971
972 static void ap_scan_bus(struct work_struct *unused)
973 {
974         struct ap_device *ap_dev;
975         struct device *dev;
976         ap_qid_t qid;
977         int queue_depth, device_type;
978         int rc, i;
979
980         if (ap_select_domain() != 0)
981                 return;
982         for (i = 0; i < AP_DEVICES; i++) {
983                 qid = AP_MKQID(i, ap_domain_index);
984                 dev = bus_find_device(&ap_bus_type, NULL,
985                                       (void *)(unsigned long)qid,
986                                       __ap_scan_bus);
987                 rc = ap_query_queue(qid, &queue_depth, &device_type);
988                 if (dev) {
989                         if (rc == -EBUSY) {
990                                 set_current_state(TASK_UNINTERRUPTIBLE);
991                                 schedule_timeout(AP_RESET_TIMEOUT);
992                                 rc = ap_query_queue(qid, &queue_depth,
993                                                     &device_type);
994                         }
995                         ap_dev = to_ap_dev(dev);
996                         spin_lock_bh(&ap_dev->lock);
997                         if (rc || ap_dev->unregistered) {
998                                 spin_unlock_bh(&ap_dev->lock);
999                                 device_unregister(dev);
1000                                 put_device(dev);
1001                                 continue;
1002                         }
1003                         spin_unlock_bh(&ap_dev->lock);
1004                         put_device(dev);
1005                         continue;
1006                 }
1007                 if (rc)
1008                         continue;
1009                 rc = ap_init_queue(qid);
1010                 if (rc)
1011                         continue;
1012                 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1013                 if (!ap_dev)
1014                         break;
1015                 ap_dev->qid = qid;
1016                 ap_dev->queue_depth = queue_depth;
1017                 ap_dev->unregistered = 1;
1018                 spin_lock_init(&ap_dev->lock);
1019                 INIT_LIST_HEAD(&ap_dev->pendingq);
1020                 INIT_LIST_HEAD(&ap_dev->requestq);
1021                 INIT_LIST_HEAD(&ap_dev->list);
1022                 setup_timer(&ap_dev->timeout, ap_request_timeout,
1023                             (unsigned long) ap_dev);
1024                 if (device_type == 0)
1025                         ap_probe_device_type(ap_dev);
1026                 else
1027                         ap_dev->device_type = device_type;
1028
1029                 ap_dev->device.bus = &ap_bus_type;
1030                 ap_dev->device.parent = ap_root_device;
1031                 dev_set_name(&ap_dev->device, "card%02x",
1032                              AP_QID_DEVICE(ap_dev->qid));
1033                 ap_dev->device.release = ap_device_release;
1034                 rc = device_register(&ap_dev->device);
1035                 if (rc) {
1036                         kfree(ap_dev);
1037                         continue;
1038                 }
1039                 /* Add device attributes. */
1040                 rc = sysfs_create_group(&ap_dev->device.kobj,
1041                                         &ap_dev_attr_group);
1042                 if (!rc) {
1043                         spin_lock_bh(&ap_dev->lock);
1044                         ap_dev->unregistered = 0;
1045                         spin_unlock_bh(&ap_dev->lock);
1046                 }
1047                 else
1048                         device_unregister(&ap_dev->device);
1049         }
1050 }
1051
1052 static void
1053 ap_config_timeout(unsigned long ptr)
1054 {
1055         queue_work(ap_work_queue, &ap_config_work);
1056         ap_config_timer.expires = jiffies + ap_config_time * HZ;
1057         add_timer(&ap_config_timer);
1058 }
1059
1060 /**
1061  * ap_schedule_poll_timer(): Schedule poll timer.
1062  *
1063  * Set up the timer to run the poll tasklet
1064  */
1065 static inline void ap_schedule_poll_timer(void)
1066 {
1067         if (ap_using_interrupts())
1068                 return;
1069         if (hrtimer_is_queued(&ap_poll_timer))
1070                 return;
1071         hrtimer_start(&ap_poll_timer, ktime_set(0, poll_timeout),
1072                       HRTIMER_MODE_ABS);
1073 }
1074
1075 /**
1076  * ap_poll_read(): Receive pending reply messages from an AP device.
1077  * @ap_dev: pointer to the AP device
1078  * @flags: pointer to control flags, bit 2^0 is set if another poll is
1079  *         required, bit 2^1 is set if the poll timer needs to get armed
1080  *
1081  * Returns 0 if the device is still present, -ENODEV if not.
1082  */
1083 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1084 {
1085         struct ap_queue_status status;
1086         struct ap_message *ap_msg;
1087
1088         if (ap_dev->queue_count <= 0)
1089                 return 0;
1090         status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1091                            ap_dev->reply->message, ap_dev->reply->length);
1092         switch (status.response_code) {
1093         case AP_RESPONSE_NORMAL:
1094                 atomic_dec(&ap_poll_requests);
1095                 ap_decrease_queue_count(ap_dev);
1096                 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1097                         if (ap_msg->psmid != ap_dev->reply->psmid)
1098                                 continue;
1099                         list_del_init(&ap_msg->list);
1100                         ap_dev->pendingq_count--;
1101                         ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
1102                         break;
1103                 }
1104                 if (ap_dev->queue_count > 0)
1105                         *flags |= 1;
1106                 break;
1107         case AP_RESPONSE_NO_PENDING_REPLY:
1108                 if (status.queue_empty) {
1109                         /* The card shouldn't forget requests but who knows. */
1110                         atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1111                         ap_dev->queue_count = 0;
1112                         list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1113                         ap_dev->requestq_count += ap_dev->pendingq_count;
1114                         ap_dev->pendingq_count = 0;
1115                 } else
1116                         *flags |= 2;
1117                 break;
1118         default:
1119                 return -ENODEV;
1120         }
1121         return 0;
1122 }
1123
1124 /**
1125  * ap_poll_write(): Send messages from the request queue to an AP device.
1126  * @ap_dev: pointer to the AP device
1127  * @flags: pointer to control flags, bit 2^0 is set if another poll is
1128  *         required, bit 2^1 is set if the poll timer needs to get armed
1129  *
1130  * Returns 0 if the device is still present, -ENODEV if not.
1131  */
1132 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1133 {
1134         struct ap_queue_status status;
1135         struct ap_message *ap_msg;
1136
1137         if (ap_dev->requestq_count <= 0 ||
1138             ap_dev->queue_count >= ap_dev->queue_depth)
1139                 return 0;
1140         /* Start the next request on the queue. */
1141         ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1142         status = __ap_send(ap_dev->qid, ap_msg->psmid,
1143                            ap_msg->message, ap_msg->length);
1144         switch (status.response_code) {
1145         case AP_RESPONSE_NORMAL:
1146                 atomic_inc(&ap_poll_requests);
1147                 ap_increase_queue_count(ap_dev);
1148                 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1149                 ap_dev->requestq_count--;
1150                 ap_dev->pendingq_count++;
1151                 if (ap_dev->queue_count < ap_dev->queue_depth &&
1152                     ap_dev->requestq_count > 0)
1153                         *flags |= 1;
1154                 *flags |= 2;
1155                 break;
1156         case AP_RESPONSE_Q_FULL:
1157         case AP_RESPONSE_RESET_IN_PROGRESS:
1158                 *flags |= 2;
1159                 break;
1160         case AP_RESPONSE_MESSAGE_TOO_BIG:
1161                 return -EINVAL;
1162         default:
1163                 return -ENODEV;
1164         }
1165         return 0;
1166 }
1167
1168 /**
1169  * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1170  * @ap_dev: pointer to the bus device
1171  * @flags: pointer to control flags, bit 2^0 is set if another poll is
1172  *         required, bit 2^1 is set if the poll timer needs to get armed
1173  *
1174  * Poll AP device for pending replies and send new messages. If either
1175  * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1176  * Returns 0.
1177  */
1178 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1179 {
1180         int rc;
1181
1182         rc = ap_poll_read(ap_dev, flags);
1183         if (rc)
1184                 return rc;
1185         return ap_poll_write(ap_dev, flags);
1186 }
1187
1188 /**
1189  * __ap_queue_message(): Queue a message to a device.
1190  * @ap_dev: pointer to the AP device
1191  * @ap_msg: the message to be queued
1192  *
1193  * Queue a message to a device. Returns 0 if successful.
1194  */
1195 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1196 {
1197         struct ap_queue_status status;
1198
1199         if (list_empty(&ap_dev->requestq) &&
1200             ap_dev->queue_count < ap_dev->queue_depth) {
1201                 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1202                                    ap_msg->message, ap_msg->length);
1203                 switch (status.response_code) {
1204                 case AP_RESPONSE_NORMAL:
1205                         list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1206                         atomic_inc(&ap_poll_requests);
1207                         ap_dev->pendingq_count++;
1208                         ap_increase_queue_count(ap_dev);
1209                         ap_dev->total_request_count++;
1210                         break;
1211                 case AP_RESPONSE_Q_FULL:
1212                 case AP_RESPONSE_RESET_IN_PROGRESS:
1213                         list_add_tail(&ap_msg->list, &ap_dev->requestq);
1214                         ap_dev->requestq_count++;
1215                         ap_dev->total_request_count++;
1216                         return -EBUSY;
1217                 case AP_RESPONSE_MESSAGE_TOO_BIG:
1218                         ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1219                         return -EINVAL;
1220                 default:        /* Device is gone. */
1221                         ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1222                         return -ENODEV;
1223                 }
1224         } else {
1225                 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1226                 ap_dev->requestq_count++;
1227                 ap_dev->total_request_count++;
1228                 return -EBUSY;
1229         }
1230         ap_schedule_poll_timer();
1231         return 0;
1232 }
1233
1234 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1235 {
1236         unsigned long flags;
1237         int rc;
1238
1239         spin_lock_bh(&ap_dev->lock);
1240         if (!ap_dev->unregistered) {
1241                 /* Make room on the queue by polling for finished requests. */
1242                 rc = ap_poll_queue(ap_dev, &flags);
1243                 if (!rc)
1244                         rc = __ap_queue_message(ap_dev, ap_msg);
1245                 if (!rc)
1246                         wake_up(&ap_poll_wait);
1247                 if (rc == -ENODEV)
1248                         ap_dev->unregistered = 1;
1249         } else {
1250                 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1251                 rc = -ENODEV;
1252         }
1253         spin_unlock_bh(&ap_dev->lock);
1254         if (rc == -ENODEV)
1255                 device_unregister(&ap_dev->device);
1256 }
1257 EXPORT_SYMBOL(ap_queue_message);
1258
1259 /**
1260  * ap_cancel_message(): Cancel a crypto request.
1261  * @ap_dev: The AP device that has the message queued
1262  * @ap_msg: The message that is to be removed
1263  *
1264  * Cancel a crypto request. This is done by removing the request
1265  * from the device pending or request queue. Note that the
1266  * request stays on the AP queue. When it finishes the message
1267  * reply will be discarded because the psmid can't be found.
1268  */
1269 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1270 {
1271         struct ap_message *tmp;
1272
1273         spin_lock_bh(&ap_dev->lock);
1274         if (!list_empty(&ap_msg->list)) {
1275                 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1276                         if (tmp->psmid == ap_msg->psmid) {
1277                                 ap_dev->pendingq_count--;
1278                                 goto found;
1279                         }
1280                 ap_dev->requestq_count--;
1281         found:
1282                 list_del_init(&ap_msg->list);
1283         }
1284         spin_unlock_bh(&ap_dev->lock);
1285 }
1286 EXPORT_SYMBOL(ap_cancel_message);
1287
1288 /**
1289  * ap_poll_timeout(): AP receive polling for finished AP requests.
1290  * @unused: Unused pointer.
1291  *
1292  * Schedules the AP tasklet using a high resolution timer.
1293  */
1294 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1295 {
1296         tasklet_schedule(&ap_tasklet);
1297         return HRTIMER_NORESTART;
1298 }
1299
1300 /**
1301  * ap_reset(): Reset a not responding AP device.
1302  * @ap_dev: Pointer to the AP device
1303  *
1304  * Reset a not responding AP device and move all requests from the
1305  * pending queue to the request queue.
1306  */
1307 static void ap_reset(struct ap_device *ap_dev)
1308 {
1309         int rc;
1310
1311         ap_dev->reset = AP_RESET_IGNORE;
1312         atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1313         ap_dev->queue_count = 0;
1314         list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1315         ap_dev->requestq_count += ap_dev->pendingq_count;
1316         ap_dev->pendingq_count = 0;
1317         rc = ap_init_queue(ap_dev->qid);
1318         if (rc == -ENODEV)
1319                 ap_dev->unregistered = 1;
1320 }
1321
1322 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1323 {
1324         spin_lock(&ap_dev->lock);
1325         if (!ap_dev->unregistered) {
1326                 if (ap_poll_queue(ap_dev, flags))
1327                         ap_dev->unregistered = 1;
1328                 if (ap_dev->reset == AP_RESET_DO)
1329                         ap_reset(ap_dev);
1330         }
1331         spin_unlock(&ap_dev->lock);
1332         return 0;
1333 }
1334
1335 /**
1336  * ap_poll_all(): Poll all AP devices.
1337  * @dummy: Unused variable
1338  *
1339  * Poll all AP devices on the bus in a round robin fashion. Continue
1340  * polling until bit 2^0 of the control flags is not set. If bit 2^1
1341  * of the control flags has been set arm the poll timer.
1342  */
1343 static void ap_poll_all(unsigned long dummy)
1344 {
1345         unsigned long flags;
1346         struct ap_device *ap_dev;
1347
1348         /* Reset the indicator if interrupts are used. Thus new interrupts can
1349          * be received. Doing it in the beginning of the tasklet is therefor
1350          * important that no requests on any AP get lost.
1351          */
1352         if (ap_using_interrupts())
1353                 xchg((u8 *)ap_interrupt_indicator, 0);
1354         do {
1355                 flags = 0;
1356                 spin_lock(&ap_device_list_lock);
1357                 list_for_each_entry(ap_dev, &ap_device_list, list) {
1358                         __ap_poll_device(ap_dev, &flags);
1359                 }
1360                 spin_unlock(&ap_device_list_lock);
1361         } while (flags & 1);
1362         if (flags & 2)
1363                 ap_schedule_poll_timer();
1364 }
1365
1366 /**
1367  * ap_poll_thread(): Thread that polls for finished requests.
1368  * @data: Unused pointer
1369  *
1370  * AP bus poll thread. The purpose of this thread is to poll for
1371  * finished requests in a loop if there is a "free" cpu - that is
1372  * a cpu that doesn't have anything better to do. The polling stops
1373  * as soon as there is another task or if all messages have been
1374  * delivered.
1375  */
1376 static int ap_poll_thread(void *data)
1377 {
1378         DECLARE_WAITQUEUE(wait, current);
1379         unsigned long flags;
1380         int requests;
1381         struct ap_device *ap_dev;
1382
1383         set_user_nice(current, 19);
1384         while (1) {
1385                 if (need_resched()) {
1386                         schedule();
1387                         continue;
1388                 }
1389                 add_wait_queue(&ap_poll_wait, &wait);
1390                 set_current_state(TASK_INTERRUPTIBLE);
1391                 if (kthread_should_stop())
1392                         break;
1393                 requests = atomic_read(&ap_poll_requests);
1394                 if (requests <= 0)
1395                         schedule();
1396                 set_current_state(TASK_RUNNING);
1397                 remove_wait_queue(&ap_poll_wait, &wait);
1398
1399                 flags = 0;
1400                 spin_lock_bh(&ap_device_list_lock);
1401                 list_for_each_entry(ap_dev, &ap_device_list, list) {
1402                         __ap_poll_device(ap_dev, &flags);
1403                 }
1404                 spin_unlock_bh(&ap_device_list_lock);
1405         }
1406         set_current_state(TASK_RUNNING);
1407         remove_wait_queue(&ap_poll_wait, &wait);
1408         return 0;
1409 }
1410
1411 static int ap_poll_thread_start(void)
1412 {
1413         int rc;
1414
1415         if (ap_using_interrupts())
1416                 return 0;
1417         mutex_lock(&ap_poll_thread_mutex);
1418         if (!ap_poll_kthread) {
1419                 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1420                 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1421                 if (rc)
1422                         ap_poll_kthread = NULL;
1423         }
1424         else
1425                 rc = 0;
1426         mutex_unlock(&ap_poll_thread_mutex);
1427         return rc;
1428 }
1429
1430 static void ap_poll_thread_stop(void)
1431 {
1432         mutex_lock(&ap_poll_thread_mutex);
1433         if (ap_poll_kthread) {
1434                 kthread_stop(ap_poll_kthread);
1435                 ap_poll_kthread = NULL;
1436         }
1437         mutex_unlock(&ap_poll_thread_mutex);
1438 }
1439
1440 /**
1441  * ap_request_timeout(): Handling of request timeouts
1442  * @data: Holds the AP device.
1443  *
1444  * Handles request timeouts.
1445  */
1446 static void ap_request_timeout(unsigned long data)
1447 {
1448         struct ap_device *ap_dev = (struct ap_device *) data;
1449
1450         if (ap_dev->reset == AP_RESET_ARMED) {
1451                 ap_dev->reset = AP_RESET_DO;
1452
1453                 if (ap_using_interrupts())
1454                         tasklet_schedule(&ap_tasklet);
1455         }
1456 }
1457
1458 static void ap_reset_domain(void)
1459 {
1460         int i;
1461
1462         if (ap_domain_index != -1)
1463                 for (i = 0; i < AP_DEVICES; i++)
1464                         ap_reset_queue(AP_MKQID(i, ap_domain_index));
1465 }
1466
1467 static void ap_reset_all(void)
1468 {
1469         int i, j;
1470
1471         for (i = 0; i < AP_DOMAINS; i++)
1472                 for (j = 0; j < AP_DEVICES; j++)
1473                         ap_reset_queue(AP_MKQID(j, i));
1474 }
1475
1476 static struct reset_call ap_reset_call = {
1477         .fn = ap_reset_all,
1478 };
1479
1480 /**
1481  * ap_module_init(): The module initialization code.
1482  *
1483  * Initializes the module.
1484  */
1485 int __init ap_module_init(void)
1486 {
1487         int rc, i;
1488
1489         if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1490                 printk(KERN_WARNING "Invalid param: domain = %d. "
1491                        " Not loading.\n", ap_domain_index);
1492                 return -EINVAL;
1493         }
1494         if (ap_instructions_available() != 0) {
1495                 printk(KERN_WARNING "AP instructions not installed.\n");
1496                 return -ENODEV;
1497         }
1498         if (ap_interrupts_available()) {
1499                 isc_register(AP_ISC);
1500                 ap_interrupt_indicator = s390_register_adapter_interrupt(
1501                         &ap_interrupt_handler, NULL, AP_ISC);
1502                 if (IS_ERR(ap_interrupt_indicator)) {
1503                         ap_interrupt_indicator = NULL;
1504                         isc_unregister(AP_ISC);
1505                 }
1506         }
1507
1508         register_reset_call(&ap_reset_call);
1509
1510         /* Create /sys/bus/ap. */
1511         rc = bus_register(&ap_bus_type);
1512         if (rc)
1513                 goto out;
1514         for (i = 0; ap_bus_attrs[i]; i++) {
1515                 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1516                 if (rc)
1517                         goto out_bus;
1518         }
1519
1520         /* Create /sys/devices/ap. */
1521         ap_root_device = s390_root_dev_register("ap");
1522         rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1523         if (rc)
1524                 goto out_bus;
1525
1526         ap_work_queue = create_singlethread_workqueue("kapwork");
1527         if (!ap_work_queue) {
1528                 rc = -ENOMEM;
1529                 goto out_root;
1530         }
1531
1532         if (ap_select_domain() == 0)
1533                 ap_scan_bus(NULL);
1534
1535         /* Setup the AP bus rescan timer. */
1536         init_timer(&ap_config_timer);
1537         ap_config_timer.function = ap_config_timeout;
1538         ap_config_timer.data = 0;
1539         ap_config_timer.expires = jiffies + ap_config_time * HZ;
1540         add_timer(&ap_config_timer);
1541
1542         /* Setup the high resultion poll timer.
1543          * If we are running under z/VM adjust polling to z/VM polling rate.
1544          */
1545         if (MACHINE_IS_VM)
1546                 poll_timeout = 1500000;
1547         hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1548         ap_poll_timer.function = ap_poll_timeout;
1549
1550         /* Start the low priority AP bus poll thread. */
1551         if (ap_thread_flag) {
1552                 rc = ap_poll_thread_start();
1553                 if (rc)
1554                         goto out_work;
1555         }
1556
1557         return 0;
1558
1559 out_work:
1560         del_timer_sync(&ap_config_timer);
1561         hrtimer_cancel(&ap_poll_timer);
1562         destroy_workqueue(ap_work_queue);
1563 out_root:
1564         s390_root_dev_unregister(ap_root_device);
1565 out_bus:
1566         while (i--)
1567                 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1568         bus_unregister(&ap_bus_type);
1569 out:
1570         unregister_reset_call(&ap_reset_call);
1571         if (ap_using_interrupts()) {
1572                 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1573                 isc_unregister(AP_ISC);
1574         }
1575         return rc;
1576 }
1577
1578 static int __ap_match_all(struct device *dev, void *data)
1579 {
1580         return 1;
1581 }
1582
1583 /**
1584  * ap_modules_exit(): The module termination code
1585  *
1586  * Terminates the module.
1587  */
1588 void ap_module_exit(void)
1589 {
1590         int i;
1591         struct device *dev;
1592
1593         ap_reset_domain();
1594         ap_poll_thread_stop();
1595         del_timer_sync(&ap_config_timer);
1596         hrtimer_cancel(&ap_poll_timer);
1597         destroy_workqueue(ap_work_queue);
1598         tasklet_kill(&ap_tasklet);
1599         s390_root_dev_unregister(ap_root_device);
1600         while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1601                     __ap_match_all)))
1602         {
1603                 device_unregister(dev);
1604                 put_device(dev);
1605         }
1606         for (i = 0; ap_bus_attrs[i]; i++)
1607                 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1608         bus_unregister(&ap_bus_type);
1609         unregister_reset_call(&ap_reset_call);
1610         if (ap_using_interrupts()) {
1611                 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1612                 isc_unregister(AP_ISC);
1613         }
1614 }
1615
1616 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1617 module_init(ap_module_init);
1618 module_exit(ap_module_exit);
1619 #endif