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