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