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