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