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