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