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