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