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