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