[S390] sclp: simplify vt220 cleanup logic
[linux-2.6-block.git] / drivers / s390 / crypto / ap_bus.c
CommitLineData
1534c382
MS
1/*
2 * linux/drivers/s390/crypto/ap_bus.c
3 *
4 * Copyright (C) 2006 IBM Corporation
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Ralph Wuerthner <rwuerthn@de.ibm.com>
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
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/err.h>
30#include <linux/interrupt.h>
31#include <linux/workqueue.h>
32#include <linux/notifier.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <asm/s390_rdev.h>
85eca850 36#include <asm/reset.h>
1534c382
MS
37
38#include "ap_bus.h"
39
40/* Some prototypes. */
4927b3f7 41static void ap_scan_bus(struct work_struct *);
1534c382
MS
42static void ap_poll_all(unsigned long);
43static void ap_poll_timeout(unsigned long);
44static int ap_poll_thread_start(void);
45static void ap_poll_thread_stop(void);
af512ed0 46static void ap_request_timeout(unsigned long);
1534c382 47
1749a81d 48/*
1534c382
MS
49 * Module description.
50 */
51MODULE_AUTHOR("IBM Corporation");
52MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
53 "Copyright 2006 IBM Corporation");
54MODULE_LICENSE("GPL");
55
1749a81d 56/*
1534c382
MS
57 * Module parameter
58 */
59int ap_domain_index = -1; /* Adjunct Processor Domain Index */
60module_param_named(domain, ap_domain_index, int, 0000);
61MODULE_PARM_DESC(domain, "domain index for ap devices");
62EXPORT_SYMBOL(ap_domain_index);
63
b90b34c6 64static int ap_thread_flag = 0;
1534c382 65module_param_named(poll_thread, ap_thread_flag, int, 0000);
b90b34c6 66MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
1534c382
MS
67
68static struct device *ap_root_device = NULL;
cf352ce0
RW
69static DEFINE_SPINLOCK(ap_device_lock);
70static LIST_HEAD(ap_device_list);
1534c382 71
1749a81d 72/*
1534c382
MS
73 * Workqueue & timer for bus rescan.
74 */
75static struct workqueue_struct *ap_work_queue;
76static struct timer_list ap_config_timer;
77static int ap_config_time = AP_CONFIG_TIME;
4927b3f7 78static DECLARE_WORK(ap_config_work, ap_scan_bus);
1534c382 79
1749a81d 80/*
1534c382
MS
81 * Tasklet & timer for AP request polling.
82 */
83static struct timer_list ap_poll_timer = TIMER_INITIALIZER(ap_poll_timeout,0,0);
84static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
85static atomic_t ap_poll_requests = ATOMIC_INIT(0);
86static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
87static struct task_struct *ap_poll_kthread = NULL;
88static DEFINE_MUTEX(ap_poll_thread_mutex);
89
90/**
1749a81d 91 * ap_intructions_available() - Test if AP instructions are available.
1534c382 92 *
1749a81d 93 * Returns 0 if the AP instructions are installed.
1534c382
MS
94 */
95static inline int ap_instructions_available(void)
96{
97 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
98 register unsigned long reg1 asm ("1") = -ENODEV;
99 register unsigned long reg2 asm ("2") = 0UL;
100
101 asm volatile(
102 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
103 "0: la %1,0\n"
104 "1:\n"
105 EX_TABLE(0b, 1b)
106 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
107 return reg1;
108}
109
110/**
1749a81d
FB
111 * ap_test_queue(): Test adjunct processor queue.
112 * @qid: The AP queue number
113 * @queue_depth: Pointer to queue depth value
114 * @device_type: Pointer to device type value
1534c382 115 *
1749a81d 116 * Returns AP queue status structure.
1534c382
MS
117 */
118static inline struct ap_queue_status
119ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
120{
121 register unsigned long reg0 asm ("0") = qid;
122 register struct ap_queue_status reg1 asm ("1");
123 register unsigned long reg2 asm ("2") = 0UL;
124
125 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
126 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
127 *device_type = (int) (reg2 >> 24);
128 *queue_depth = (int) (reg2 & 0xff);
129 return reg1;
130}
131
132/**
1749a81d
FB
133 * ap_reset_queue(): Reset adjunct processor queue.
134 * @qid: The AP queue number
1534c382 135 *
1749a81d 136 * Returns AP queue status structure.
1534c382
MS
137 */
138static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
139{
140 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
141 register struct ap_queue_status reg1 asm ("1");
142 register unsigned long reg2 asm ("2") = 0UL;
143
144 asm volatile(
145 ".long 0xb2af0000" /* PQAP(RAPQ) */
146 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
147 return reg1;
148}
149
150/**
1749a81d
FB
151 * __ap_send(): Send message to adjunct processor queue.
152 * @qid: The AP queue number
153 * @psmid: The program supplied message identifier
154 * @msg: The message text
155 * @length: The message length
1534c382 156 *
1749a81d 157 * Returns AP queue status structure.
1534c382 158 * Condition code 1 on NQAP can't happen because the L bit is 1.
1534c382
MS
159 * Condition code 2 on NQAP also means the send is incomplete,
160 * because a segment boundary was reached. The NQAP is repeated.
161 */
162static inline struct ap_queue_status
163__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
164{
165 typedef struct { char _[length]; } msgblock;
166 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
167 register struct ap_queue_status reg1 asm ("1");
168 register unsigned long reg2 asm ("2") = (unsigned long) msg;
169 register unsigned long reg3 asm ("3") = (unsigned long) length;
170 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
171 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
172
173 asm volatile (
174 "0: .long 0xb2ad0042\n" /* DQAP */
175 " brc 2,0b"
176 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
177 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
178 : "cc" );
179 return reg1;
180}
181
182int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
183{
184 struct ap_queue_status status;
185
186 status = __ap_send(qid, psmid, msg, length);
187 switch (status.response_code) {
188 case AP_RESPONSE_NORMAL:
189 return 0;
190 case AP_RESPONSE_Q_FULL:
af512ed0 191 case AP_RESPONSE_RESET_IN_PROGRESS:
1534c382
MS
192 return -EBUSY;
193 default: /* Device is gone. */
194 return -ENODEV;
195 }
196}
197EXPORT_SYMBOL(ap_send);
198
1749a81d
FB
199/**
200 * __ap_recv(): Receive message from adjunct processor queue.
201 * @qid: The AP queue number
202 * @psmid: Pointer to program supplied message identifier
203 * @msg: The message text
204 * @length: The message length
1534c382 205 *
1749a81d 206 * Returns AP queue status structure.
1534c382
MS
207 * Condition code 1 on DQAP means the receive has taken place
208 * but only partially. The response is incomplete, hence the
209 * DQAP is repeated.
1534c382
MS
210 * Condition code 2 on DQAP also means the receive is incomplete,
211 * this time because a segment boundary was reached. Again, the
212 * DQAP is repeated.
1534c382
MS
213 * Note that gpr2 is used by the DQAP instruction to keep track of
214 * any 'residual' length, in case the instruction gets interrupted.
215 * Hence it gets zeroed before the instruction.
216 */
217static inline struct ap_queue_status
218__ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
219{
220 typedef struct { char _[length]; } msgblock;
221 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
222 register struct ap_queue_status reg1 asm ("1");
223 register unsigned long reg2 asm("2") = 0UL;
224 register unsigned long reg4 asm("4") = (unsigned long) msg;
225 register unsigned long reg5 asm("5") = (unsigned long) length;
226 register unsigned long reg6 asm("6") = 0UL;
227 register unsigned long reg7 asm("7") = 0UL;
228
229
230 asm volatile(
231 "0: .long 0xb2ae0064\n"
232 " brc 6,0b\n"
233 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
234 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
235 "=m" (*(msgblock *) msg) : : "cc" );
236 *psmid = (((unsigned long long) reg6) << 32) + reg7;
237 return reg1;
238}
239
240int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
241{
242 struct ap_queue_status status;
243
244 status = __ap_recv(qid, psmid, msg, length);
245 switch (status.response_code) {
246 case AP_RESPONSE_NORMAL:
247 return 0;
248 case AP_RESPONSE_NO_PENDING_REPLY:
249 if (status.queue_empty)
250 return -ENOENT;
251 return -EBUSY;
af512ed0
RW
252 case AP_RESPONSE_RESET_IN_PROGRESS:
253 return -EBUSY;
1534c382
MS
254 default:
255 return -ENODEV;
256 }
257}
258EXPORT_SYMBOL(ap_recv);
259
260/**
1749a81d
FB
261 * ap_query_queue(): Check if an AP queue is available.
262 * @qid: The AP queue number
263 * @queue_depth: Pointer to queue depth value
264 * @device_type: Pointer to device type value
265 *
266 * The test is repeated for AP_MAX_RESET times.
1534c382
MS
267 */
268static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
269{
270 struct ap_queue_status status;
271 int t_depth, t_device_type, rc, i;
272
273 rc = -EBUSY;
274 for (i = 0; i < AP_MAX_RESET; i++) {
275 status = ap_test_queue(qid, &t_depth, &t_device_type);
276 switch (status.response_code) {
277 case AP_RESPONSE_NORMAL:
278 *queue_depth = t_depth + 1;
279 *device_type = t_device_type;
280 rc = 0;
281 break;
282 case AP_RESPONSE_Q_NOT_AVAIL:
283 rc = -ENODEV;
284 break;
285 case AP_RESPONSE_RESET_IN_PROGRESS:
286 break;
287 case AP_RESPONSE_DECONFIGURED:
288 rc = -ENODEV;
289 break;
290 case AP_RESPONSE_CHECKSTOPPED:
291 rc = -ENODEV;
292 break;
293 case AP_RESPONSE_BUSY:
294 break;
295 default:
296 BUG();
297 }
298 if (rc != -EBUSY)
299 break;
300 if (i < AP_MAX_RESET - 1)
301 udelay(5);
302 }
303 return rc;
304}
305
306/**
1749a81d
FB
307 * ap_init_queue(): Reset an AP queue.
308 * @qid: The AP queue number
309 *
1534c382 310 * Reset an AP queue and wait for it to become available again.
1534c382
MS
311 */
312static int ap_init_queue(ap_qid_t qid)
313{
314 struct ap_queue_status status;
315 int rc, dummy, i;
316
317 rc = -ENODEV;
318 status = ap_reset_queue(qid);
319 for (i = 0; i < AP_MAX_RESET; i++) {
320 switch (status.response_code) {
321 case AP_RESPONSE_NORMAL:
322 if (status.queue_empty)
323 rc = 0;
324 break;
325 case AP_RESPONSE_Q_NOT_AVAIL:
326 case AP_RESPONSE_DECONFIGURED:
327 case AP_RESPONSE_CHECKSTOPPED:
328 i = AP_MAX_RESET; /* return with -ENODEV */
329 break;
330 case AP_RESPONSE_RESET_IN_PROGRESS:
af512ed0 331 rc = -EBUSY;
1534c382
MS
332 case AP_RESPONSE_BUSY:
333 default:
334 break;
335 }
af512ed0 336 if (rc != -ENODEV && rc != -EBUSY)
1534c382
MS
337 break;
338 if (i < AP_MAX_RESET - 1) {
339 udelay(5);
340 status = ap_test_queue(qid, &dummy, &dummy);
341 }
342 }
343 return rc;
344}
345
af512ed0 346/**
1749a81d
FB
347 * ap_increase_queue_count(): Arm request timeout.
348 * @ap_dev: Pointer to an AP device.
349 *
350 * Arm request timeout if an AP device was idle and a new request is submitted.
af512ed0
RW
351 */
352static void ap_increase_queue_count(struct ap_device *ap_dev)
353{
354 int timeout = ap_dev->drv->request_timeout;
355
356 ap_dev->queue_count++;
357 if (ap_dev->queue_count == 1) {
358 mod_timer(&ap_dev->timeout, jiffies + timeout);
359 ap_dev->reset = AP_RESET_ARMED;
360 }
361}
362
363/**
1749a81d
FB
364 * ap_decrease_queue_count(): Decrease queue count.
365 * @ap_dev: Pointer to an AP device.
366 *
367 * If AP device is still alive, re-schedule request timeout if there are still
af512ed0
RW
368 * pending requests.
369 */
370static void ap_decrease_queue_count(struct ap_device *ap_dev)
371{
372 int timeout = ap_dev->drv->request_timeout;
373
374 ap_dev->queue_count--;
375 if (ap_dev->queue_count > 0)
376 mod_timer(&ap_dev->timeout, jiffies + timeout);
377 else
1749a81d 378 /*
af512ed0
RW
379 * The timeout timer should to be disabled now - since
380 * del_timer_sync() is very expensive, we just tell via the
381 * reset flag to ignore the pending timeout timer.
382 */
383 ap_dev->reset = AP_RESET_IGNORE;
384}
385
1749a81d 386/*
1534c382
MS
387 * AP device related attributes.
388 */
389static ssize_t ap_hwtype_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
392 struct ap_device *ap_dev = to_ap_dev(dev);
393 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
394}
395static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
396
397static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
398 char *buf)
399{
400 struct ap_device *ap_dev = to_ap_dev(dev);
401 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
402}
403static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
404
405static ssize_t ap_request_count_show(struct device *dev,
406 struct device_attribute *attr,
407 char *buf)
408{
409 struct ap_device *ap_dev = to_ap_dev(dev);
410 int rc;
411
412 spin_lock_bh(&ap_dev->lock);
413 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
414 spin_unlock_bh(&ap_dev->lock);
415 return rc;
416}
417
418static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
419
420static ssize_t ap_modalias_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
423 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
424}
425
426static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
427
428static struct attribute *ap_dev_attrs[] = {
429 &dev_attr_hwtype.attr,
430 &dev_attr_depth.attr,
431 &dev_attr_request_count.attr,
432 &dev_attr_modalias.attr,
433 NULL
434};
435static struct attribute_group ap_dev_attr_group = {
436 .attrs = ap_dev_attrs
437};
438
439/**
1749a81d
FB
440 * ap_bus_match()
441 * @dev: Pointer to device
442 * @drv: Pointer to device_driver
443 *
1534c382
MS
444 * AP bus driver registration/unregistration.
445 */
446static int ap_bus_match(struct device *dev, struct device_driver *drv)
447{
448 struct ap_device *ap_dev = to_ap_dev(dev);
449 struct ap_driver *ap_drv = to_ap_drv(drv);
450 struct ap_device_id *id;
451
1749a81d 452 /*
1534c382
MS
453 * Compare device type of the device with the list of
454 * supported types of the device_driver.
455 */
456 for (id = ap_drv->ids; id->match_flags; id++) {
457 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
458 (id->dev_type != ap_dev->device_type))
459 continue;
460 return 1;
461 }
462 return 0;
463}
464
465/**
1749a81d
FB
466 * ap_uevent(): Uevent function for AP devices.
467 * @dev: Pointer to device
468 * @env: Pointer to kobj_uevent_env
469 *
470 * It sets up a single environment variable DEV_TYPE which contains the
471 * hardware device type.
1534c382 472 */
7eff2e7a 473static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
1534c382
MS
474{
475 struct ap_device *ap_dev = to_ap_dev(dev);
7eff2e7a 476 int retval = 0;
1534c382
MS
477
478 if (!ap_dev)
479 return -ENODEV;
480
481 /* Set up DEV_TYPE environment variable. */
7eff2e7a 482 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
bf62456e
ER
483 if (retval)
484 return retval;
485
66a4263b 486 /* Add MODALIAS= */
7eff2e7a 487 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
bf62456e 488
bf62456e 489 return retval;
1534c382
MS
490}
491
492static struct bus_type ap_bus_type = {
493 .name = "ap",
494 .match = &ap_bus_match,
495 .uevent = &ap_uevent,
496};
497
498static int ap_device_probe(struct device *dev)
499{
500 struct ap_device *ap_dev = to_ap_dev(dev);
501 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
502 int rc;
503
504 ap_dev->drv = ap_drv;
505 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
faa582ca
RW
506 if (!rc) {
507 spin_lock_bh(&ap_device_lock);
508 list_add(&ap_dev->list, &ap_device_list);
509 spin_unlock_bh(&ap_device_lock);
510 }
1534c382
MS
511 return rc;
512}
513
514/**
1749a81d
FB
515 * __ap_flush_queue(): Flush requests.
516 * @ap_dev: Pointer to the AP device
517 *
1534c382 518 * Flush all requests from the request/pending queue of an AP device.
1534c382 519 */
4d284cac 520static void __ap_flush_queue(struct ap_device *ap_dev)
1534c382
MS
521{
522 struct ap_message *ap_msg, *next;
523
524 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
525 list_del_init(&ap_msg->list);
526 ap_dev->pendingq_count--;
527 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
528 }
529 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
530 list_del_init(&ap_msg->list);
531 ap_dev->requestq_count--;
532 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
533 }
534}
535
536void ap_flush_queue(struct ap_device *ap_dev)
537{
538 spin_lock_bh(&ap_dev->lock);
539 __ap_flush_queue(ap_dev);
540 spin_unlock_bh(&ap_dev->lock);
541}
542EXPORT_SYMBOL(ap_flush_queue);
543
544static int ap_device_remove(struct device *dev)
545{
546 struct ap_device *ap_dev = to_ap_dev(dev);
547 struct ap_driver *ap_drv = ap_dev->drv;
548
4e56296d 549 ap_flush_queue(ap_dev);
af512ed0 550 del_timer_sync(&ap_dev->timeout);
cf352ce0
RW
551 spin_lock_bh(&ap_device_lock);
552 list_del_init(&ap_dev->list);
553 spin_unlock_bh(&ap_device_lock);
faa582ca
RW
554 if (ap_drv->remove)
555 ap_drv->remove(ap_dev);
e675c0d2
RW
556 spin_lock_bh(&ap_dev->lock);
557 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
558 spin_unlock_bh(&ap_dev->lock);
1534c382
MS
559 return 0;
560}
561
562int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
563 char *name)
564{
565 struct device_driver *drv = &ap_drv->driver;
566
567 drv->bus = &ap_bus_type;
568 drv->probe = ap_device_probe;
569 drv->remove = ap_device_remove;
570 drv->owner = owner;
571 drv->name = name;
572 return driver_register(drv);
573}
574EXPORT_SYMBOL(ap_driver_register);
575
576void ap_driver_unregister(struct ap_driver *ap_drv)
577{
578 driver_unregister(&ap_drv->driver);
579}
580EXPORT_SYMBOL(ap_driver_unregister);
581
1749a81d 582/*
1534c382
MS
583 * AP bus attributes.
584 */
585static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
586{
587 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
588}
589
590static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
591
592static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
593{
594 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
595}
596
597static ssize_t ap_config_time_store(struct bus_type *bus,
598 const char *buf, size_t count)
599{
600 int time;
601
602 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
603 return -EINVAL;
604 ap_config_time = time;
605 if (!timer_pending(&ap_config_timer) ||
606 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
607 ap_config_timer.expires = jiffies + ap_config_time * HZ;
608 add_timer(&ap_config_timer);
609 }
610 return count;
611}
612
613static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
614
615static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
616{
617 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
618}
619
620static ssize_t ap_poll_thread_store(struct bus_type *bus,
621 const char *buf, size_t count)
622{
623 int flag, rc;
624
625 if (sscanf(buf, "%d\n", &flag) != 1)
626 return -EINVAL;
627 if (flag) {
628 rc = ap_poll_thread_start();
629 if (rc)
630 return rc;
631 }
632 else
633 ap_poll_thread_stop();
634 return count;
635}
636
637static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
638
639static struct bus_attribute *const ap_bus_attrs[] = {
640 &bus_attr_ap_domain,
641 &bus_attr_config_time,
642 &bus_attr_poll_thread,
643 NULL
644};
645
646/**
1749a81d
FB
647 * ap_select_domain(): Select an AP domain.
648 *
649 * Pick one of the 16 AP domains.
1534c382 650 */
4d284cac 651static int ap_select_domain(void)
1534c382
MS
652{
653 int queue_depth, device_type, count, max_count, best_domain;
654 int rc, i, j;
655
1749a81d 656 /*
1534c382
MS
657 * We want to use a single domain. Either the one specified with
658 * the "domain=" parameter or the domain with the maximum number
659 * of devices.
660 */
661 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
662 /* Domain has already been selected. */
663 return 0;
664 best_domain = -1;
665 max_count = 0;
666 for (i = 0; i < AP_DOMAINS; i++) {
667 count = 0;
668 for (j = 0; j < AP_DEVICES; j++) {
669 ap_qid_t qid = AP_MKQID(j, i);
670 rc = ap_query_queue(qid, &queue_depth, &device_type);
671 if (rc)
672 continue;
673 count++;
674 }
675 if (count > max_count) {
676 max_count = count;
677 best_domain = i;
678 }
679 }
680 if (best_domain >= 0){
681 ap_domain_index = best_domain;
682 return 0;
683 }
684 return -ENODEV;
685}
686
687/**
1749a81d 688 * ap_probe_device_type(): Find the device type of an AP.
1534c382 689 * @ap_dev: pointer to the AP device.
1749a81d
FB
690 *
691 * Find the device type if query queue returned a device type of 0.
1534c382
MS
692 */
693static int ap_probe_device_type(struct ap_device *ap_dev)
694{
695 static unsigned char msg[] = {
696 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
697 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
698 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
699 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
700 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
701 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
702 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
703 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
704 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
705 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
706 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
707 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
708 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
709 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
710 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
711 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
712 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
713 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
714 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
715 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
716 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
717 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
718 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
719 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
720 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
721 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
722 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
723 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
724 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
725 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
726 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
727 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
728 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
729 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
730 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
731 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
732 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
733 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
734 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
735 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
736 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
737 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
738 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
739 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
740 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
741 };
742 struct ap_queue_status status;
743 unsigned long long psmid;
744 char *reply;
745 int rc, i;
746
747 reply = (void *) get_zeroed_page(GFP_KERNEL);
748 if (!reply) {
749 rc = -ENOMEM;
750 goto out;
751 }
752
753 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
754 msg, sizeof(msg));
755 if (status.response_code != AP_RESPONSE_NORMAL) {
756 rc = -ENODEV;
757 goto out_free;
758 }
759
760 /* Wait for the test message to complete. */
761 for (i = 0; i < 6; i++) {
762 mdelay(300);
763 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
764 if (status.response_code == AP_RESPONSE_NORMAL &&
765 psmid == 0x0102030405060708ULL)
766 break;
767 }
768 if (i < 6) {
769 /* Got an answer. */
770 if (reply[0] == 0x00 && reply[1] == 0x86)
771 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
772 else
773 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
774 rc = 0;
775 } else
776 rc = -ENODEV;
777
778out_free:
779 free_page((unsigned long) reply);
780out:
781 return rc;
782}
783
784/**
1749a81d
FB
785 * __ap_scan_bus(): Scan the AP bus.
786 * @dev: Pointer to device
787 * @data: Pointer to data
788 *
789 * Scan the AP bus for new devices.
1534c382
MS
790 */
791static int __ap_scan_bus(struct device *dev, void *data)
792{
793 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
794}
795
796static void ap_device_release(struct device *dev)
797{
798 struct ap_device *ap_dev = to_ap_dev(dev);
799
800 kfree(ap_dev);
801}
802
4927b3f7 803static void ap_scan_bus(struct work_struct *unused)
1534c382
MS
804{
805 struct ap_device *ap_dev;
806 struct device *dev;
807 ap_qid_t qid;
808 int queue_depth, device_type;
809 int rc, i;
810
811 if (ap_select_domain() != 0)
812 return;
813 for (i = 0; i < AP_DEVICES; i++) {
814 qid = AP_MKQID(i, ap_domain_index);
815 dev = bus_find_device(&ap_bus_type, NULL,
816 (void *)(unsigned long)qid,
817 __ap_scan_bus);
f3b017d8 818 rc = ap_query_queue(qid, &queue_depth, &device_type);
c6a48264 819 if (dev) {
af512ed0
RW
820 if (rc == -EBUSY) {
821 set_current_state(TASK_UNINTERRUPTIBLE);
822 schedule_timeout(AP_RESET_TIMEOUT);
823 rc = ap_query_queue(qid, &queue_depth,
824 &device_type);
825 }
c6a48264
RW
826 ap_dev = to_ap_dev(dev);
827 spin_lock_bh(&ap_dev->lock);
828 if (rc || ap_dev->unregistered) {
829 spin_unlock_bh(&ap_dev->lock);
c6a48264 830 device_unregister(dev);
af512ed0 831 put_device(dev);
c6a48264 832 continue;
af512ed0
RW
833 }
834 spin_unlock_bh(&ap_dev->lock);
1534c382
MS
835 put_device(dev);
836 continue;
837 }
1534c382
MS
838 if (rc)
839 continue;
840 rc = ap_init_queue(qid);
841 if (rc)
842 continue;
843 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
844 if (!ap_dev)
845 break;
846 ap_dev->qid = qid;
847 ap_dev->queue_depth = queue_depth;
4e56296d 848 ap_dev->unregistered = 1;
1534c382
MS
849 spin_lock_init(&ap_dev->lock);
850 INIT_LIST_HEAD(&ap_dev->pendingq);
851 INIT_LIST_HEAD(&ap_dev->requestq);
cf352ce0 852 INIT_LIST_HEAD(&ap_dev->list);
af512ed0
RW
853 setup_timer(&ap_dev->timeout, ap_request_timeout,
854 (unsigned long) ap_dev);
1534c382
MS
855 if (device_type == 0)
856 ap_probe_device_type(ap_dev);
857 else
858 ap_dev->device_type = device_type;
859
860 ap_dev->device.bus = &ap_bus_type;
861 ap_dev->device.parent = ap_root_device;
862 snprintf(ap_dev->device.bus_id, BUS_ID_SIZE, "card%02x",
863 AP_QID_DEVICE(ap_dev->qid));
864 ap_dev->device.release = ap_device_release;
865 rc = device_register(&ap_dev->device);
866 if (rc) {
867 kfree(ap_dev);
868 continue;
869 }
870 /* Add device attributes. */
871 rc = sysfs_create_group(&ap_dev->device.kobj,
872 &ap_dev_attr_group);
4e56296d
RW
873 if (!rc) {
874 spin_lock_bh(&ap_dev->lock);
875 ap_dev->unregistered = 0;
876 spin_unlock_bh(&ap_dev->lock);
877 }
878 else
1534c382
MS
879 device_unregister(&ap_dev->device);
880 }
881}
882
883static void
884ap_config_timeout(unsigned long ptr)
885{
886 queue_work(ap_work_queue, &ap_config_work);
887 ap_config_timer.expires = jiffies + ap_config_time * HZ;
888 add_timer(&ap_config_timer);
889}
890
891/**
1749a81d
FB
892 * ap_schedule_poll_timer(): Schedule poll timer.
893 *
1534c382
MS
894 * Set up the timer to run the poll tasklet
895 */
896static inline void ap_schedule_poll_timer(void)
897{
898 if (timer_pending(&ap_poll_timer))
899 return;
900 mod_timer(&ap_poll_timer, jiffies + AP_POLL_TIME);
901}
902
903/**
1749a81d 904 * ap_poll_read(): Receive pending reply messages from an AP device.
1534c382
MS
905 * @ap_dev: pointer to the AP device
906 * @flags: pointer to control flags, bit 2^0 is set if another poll is
907 * required, bit 2^1 is set if the poll timer needs to get armed
1749a81d 908 *
1534c382
MS
909 * Returns 0 if the device is still present, -ENODEV if not.
910 */
4d284cac 911static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1534c382
MS
912{
913 struct ap_queue_status status;
914 struct ap_message *ap_msg;
915
916 if (ap_dev->queue_count <= 0)
917 return 0;
918 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
919 ap_dev->reply->message, ap_dev->reply->length);
920 switch (status.response_code) {
921 case AP_RESPONSE_NORMAL:
922 atomic_dec(&ap_poll_requests);
af512ed0 923 ap_decrease_queue_count(ap_dev);
1534c382
MS
924 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
925 if (ap_msg->psmid != ap_dev->reply->psmid)
926 continue;
927 list_del_init(&ap_msg->list);
928 ap_dev->pendingq_count--;
929 ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
930 break;
931 }
932 if (ap_dev->queue_count > 0)
933 *flags |= 1;
934 break;
935 case AP_RESPONSE_NO_PENDING_REPLY:
936 if (status.queue_empty) {
937 /* The card shouldn't forget requests but who knows. */
e675c0d2 938 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1534c382
MS
939 ap_dev->queue_count = 0;
940 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
941 ap_dev->requestq_count += ap_dev->pendingq_count;
942 ap_dev->pendingq_count = 0;
943 } else
944 *flags |= 2;
945 break;
946 default:
947 return -ENODEV;
948 }
949 return 0;
950}
951
952/**
1749a81d 953 * ap_poll_write(): Send messages from the request queue to an AP device.
1534c382
MS
954 * @ap_dev: pointer to the AP device
955 * @flags: pointer to control flags, bit 2^0 is set if another poll is
956 * required, bit 2^1 is set if the poll timer needs to get armed
1749a81d 957 *
1534c382
MS
958 * Returns 0 if the device is still present, -ENODEV if not.
959 */
4d284cac 960static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1534c382
MS
961{
962 struct ap_queue_status status;
963 struct ap_message *ap_msg;
964
965 if (ap_dev->requestq_count <= 0 ||
966 ap_dev->queue_count >= ap_dev->queue_depth)
967 return 0;
968 /* Start the next request on the queue. */
969 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
970 status = __ap_send(ap_dev->qid, ap_msg->psmid,
971 ap_msg->message, ap_msg->length);
972 switch (status.response_code) {
973 case AP_RESPONSE_NORMAL:
974 atomic_inc(&ap_poll_requests);
af512ed0 975 ap_increase_queue_count(ap_dev);
1534c382
MS
976 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
977 ap_dev->requestq_count--;
978 ap_dev->pendingq_count++;
979 if (ap_dev->queue_count < ap_dev->queue_depth &&
980 ap_dev->requestq_count > 0)
981 *flags |= 1;
982 *flags |= 2;
983 break;
984 case AP_RESPONSE_Q_FULL:
af512ed0 985 case AP_RESPONSE_RESET_IN_PROGRESS:
1534c382
MS
986 *flags |= 2;
987 break;
988 case AP_RESPONSE_MESSAGE_TOO_BIG:
989 return -EINVAL;
990 default:
991 return -ENODEV;
992 }
993 return 0;
994}
995
996/**
1749a81d 997 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1534c382
MS
998 * @ap_dev: pointer to the bus device
999 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1000 * required, bit 2^1 is set if the poll timer needs to get armed
1749a81d
FB
1001 *
1002 * Poll AP device for pending replies and send new messages. If either
1003 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1534c382
MS
1004 * Returns 0.
1005 */
1006static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1007{
1008 int rc;
1009
1010 rc = ap_poll_read(ap_dev, flags);
1011 if (rc)
1012 return rc;
1013 return ap_poll_write(ap_dev, flags);
1014}
1015
1016/**
1749a81d 1017 * __ap_queue_message(): Queue a message to a device.
1534c382
MS
1018 * @ap_dev: pointer to the AP device
1019 * @ap_msg: the message to be queued
1749a81d
FB
1020 *
1021 * Queue a message to a device. Returns 0 if successful.
1534c382
MS
1022 */
1023static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1024{
1025 struct ap_queue_status status;
1026
1027 if (list_empty(&ap_dev->requestq) &&
1028 ap_dev->queue_count < ap_dev->queue_depth) {
1029 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1030 ap_msg->message, ap_msg->length);
1031 switch (status.response_code) {
1032 case AP_RESPONSE_NORMAL:
1033 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1034 atomic_inc(&ap_poll_requests);
1035 ap_dev->pendingq_count++;
af512ed0 1036 ap_increase_queue_count(ap_dev);
1534c382
MS
1037 ap_dev->total_request_count++;
1038 break;
1039 case AP_RESPONSE_Q_FULL:
af512ed0 1040 case AP_RESPONSE_RESET_IN_PROGRESS:
1534c382
MS
1041 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1042 ap_dev->requestq_count++;
1043 ap_dev->total_request_count++;
1044 return -EBUSY;
1045 case AP_RESPONSE_MESSAGE_TOO_BIG:
1046 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1047 return -EINVAL;
1048 default: /* Device is gone. */
1049 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1050 return -ENODEV;
1051 }
1052 } else {
1053 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1054 ap_dev->requestq_count++;
1055 ap_dev->total_request_count++;
1056 return -EBUSY;
1057 }
1058 ap_schedule_poll_timer();
1059 return 0;
1060}
1061
1062void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1063{
1064 unsigned long flags;
1065 int rc;
1066
1067 spin_lock_bh(&ap_dev->lock);
1068 if (!ap_dev->unregistered) {
1069 /* Make room on the queue by polling for finished requests. */
1070 rc = ap_poll_queue(ap_dev, &flags);
1071 if (!rc)
1072 rc = __ap_queue_message(ap_dev, ap_msg);
1073 if (!rc)
1074 wake_up(&ap_poll_wait);
4e56296d
RW
1075 if (rc == -ENODEV)
1076 ap_dev->unregistered = 1;
1534c382
MS
1077 } else {
1078 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
c6a48264 1079 rc = -ENODEV;
1534c382
MS
1080 }
1081 spin_unlock_bh(&ap_dev->lock);
1082 if (rc == -ENODEV)
1083 device_unregister(&ap_dev->device);
1084}
1085EXPORT_SYMBOL(ap_queue_message);
1086
1087/**
1749a81d
FB
1088 * ap_cancel_message(): Cancel a crypto request.
1089 * @ap_dev: The AP device that has the message queued
1090 * @ap_msg: The message that is to be removed
1091 *
1534c382 1092 * Cancel a crypto request. This is done by removing the request
1749a81d 1093 * from the device pending or request queue. Note that the
1534c382
MS
1094 * request stays on the AP queue. When it finishes the message
1095 * reply will be discarded because the psmid can't be found.
1534c382
MS
1096 */
1097void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1098{
1099 struct ap_message *tmp;
1100
1101 spin_lock_bh(&ap_dev->lock);
1102 if (!list_empty(&ap_msg->list)) {
1103 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1104 if (tmp->psmid == ap_msg->psmid) {
1105 ap_dev->pendingq_count--;
1106 goto found;
1107 }
1108 ap_dev->requestq_count--;
1109 found:
1110 list_del_init(&ap_msg->list);
1111 }
1112 spin_unlock_bh(&ap_dev->lock);
1113}
1114EXPORT_SYMBOL(ap_cancel_message);
1115
1116/**
1749a81d
FB
1117 * ap_poll_timeout(): AP receive polling for finished AP requests.
1118 * @unused: Unused variable.
1119 *
1120 * Schedules the AP tasklet.
1534c382
MS
1121 */
1122static void ap_poll_timeout(unsigned long unused)
1123{
1124 tasklet_schedule(&ap_tasklet);
1125}
1126
af512ed0 1127/**
1749a81d
FB
1128 * ap_reset(): Reset a not responding AP device.
1129 * @ap_dev: Pointer to the AP device
1130 *
af512ed0
RW
1131 * Reset a not responding AP device and move all requests from the
1132 * pending queue to the request queue.
1133 */
1134static void ap_reset(struct ap_device *ap_dev)
1135{
1136 int rc;
1137
1138 ap_dev->reset = AP_RESET_IGNORE;
1139 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1140 ap_dev->queue_count = 0;
1141 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1142 ap_dev->requestq_count += ap_dev->pendingq_count;
1143 ap_dev->pendingq_count = 0;
1144 rc = ap_init_queue(ap_dev->qid);
1145 if (rc == -ENODEV)
1146 ap_dev->unregistered = 1;
1147}
1148
cf352ce0 1149static int __ap_poll_all(struct ap_device *ap_dev, unsigned long *flags)
1534c382 1150{
1534c382
MS
1151 spin_lock(&ap_dev->lock);
1152 if (!ap_dev->unregistered) {
c6a48264 1153 if (ap_poll_queue(ap_dev, flags))
4e56296d 1154 ap_dev->unregistered = 1;
af512ed0
RW
1155 if (ap_dev->reset == AP_RESET_DO)
1156 ap_reset(ap_dev);
c6a48264 1157 }
1534c382 1158 spin_unlock(&ap_dev->lock);
1534c382
MS
1159 return 0;
1160}
1161
1749a81d
FB
1162/**
1163 * ap_poll_all(): Poll all AP devices.
1164 * @dummy: Unused variable
1165 *
1166 * Poll all AP devices on the bus in a round robin fashion. Continue
1167 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1168 * of the control flags has been set arm the poll timer.
1169 */
1534c382
MS
1170static void ap_poll_all(unsigned long dummy)
1171{
1172 unsigned long flags;
cf352ce0 1173 struct ap_device *ap_dev;
1534c382
MS
1174
1175 do {
1176 flags = 0;
cf352ce0
RW
1177 spin_lock(&ap_device_lock);
1178 list_for_each_entry(ap_dev, &ap_device_list, list) {
1179 __ap_poll_all(ap_dev, &flags);
1180 }
1181 spin_unlock(&ap_device_lock);
1534c382
MS
1182 } while (flags & 1);
1183 if (flags & 2)
1184 ap_schedule_poll_timer();
1185}
1186
1187/**
1749a81d
FB
1188 * ap_poll_thread(): Thread that polls for finished requests.
1189 * @data: Unused pointer
1190 *
1534c382
MS
1191 * AP bus poll thread. The purpose of this thread is to poll for
1192 * finished requests in a loop if there is a "free" cpu - that is
1193 * a cpu that doesn't have anything better to do. The polling stops
1194 * as soon as there is another task or if all messages have been
1195 * delivered.
1196 */
1197static int ap_poll_thread(void *data)
1198{
1199 DECLARE_WAITQUEUE(wait, current);
1200 unsigned long flags;
1201 int requests;
cf352ce0 1202 struct ap_device *ap_dev;
1534c382 1203
d83682b3 1204 set_user_nice(current, 19);
1534c382
MS
1205 while (1) {
1206 if (need_resched()) {
1207 schedule();
1208 continue;
1209 }
1210 add_wait_queue(&ap_poll_wait, &wait);
1211 set_current_state(TASK_INTERRUPTIBLE);
1212 if (kthread_should_stop())
1213 break;
1214 requests = atomic_read(&ap_poll_requests);
1215 if (requests <= 0)
1216 schedule();
1217 set_current_state(TASK_RUNNING);
1218 remove_wait_queue(&ap_poll_wait, &wait);
1219
1534c382 1220 flags = 0;
cf352ce0
RW
1221 spin_lock_bh(&ap_device_lock);
1222 list_for_each_entry(ap_dev, &ap_device_list, list) {
1223 __ap_poll_all(ap_dev, &flags);
1224 }
1225 spin_unlock_bh(&ap_device_lock);
1534c382
MS
1226 }
1227 set_current_state(TASK_RUNNING);
1228 remove_wait_queue(&ap_poll_wait, &wait);
1229 return 0;
1230}
1231
1232static int ap_poll_thread_start(void)
1233{
1234 int rc;
1235
1236 mutex_lock(&ap_poll_thread_mutex);
1237 if (!ap_poll_kthread) {
1238 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1239 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1240 if (rc)
1241 ap_poll_kthread = NULL;
1242 }
1243 else
1244 rc = 0;
1245 mutex_unlock(&ap_poll_thread_mutex);
1246 return rc;
1247}
1248
1249static void ap_poll_thread_stop(void)
1250{
1251 mutex_lock(&ap_poll_thread_mutex);
1252 if (ap_poll_kthread) {
1253 kthread_stop(ap_poll_kthread);
1254 ap_poll_kthread = NULL;
1255 }
1256 mutex_unlock(&ap_poll_thread_mutex);
1257}
1258
af512ed0 1259/**
1749a81d
FB
1260 * ap_request_timeout(): Handling of request timeouts
1261 * @data: Holds the AP device.
1262 *
1263 * Handles request timeouts.
af512ed0
RW
1264 */
1265static void ap_request_timeout(unsigned long data)
1266{
1267 struct ap_device *ap_dev = (struct ap_device *) data;
1268
1269 if (ap_dev->reset == AP_RESET_ARMED)
1270 ap_dev->reset = AP_RESET_DO;
1271}
1272
13e742ba
RW
1273static void ap_reset_domain(void)
1274{
1275 int i;
1276
39aa7cf6
RW
1277 if (ap_domain_index != -1)
1278 for (i = 0; i < AP_DEVICES; i++)
1279 ap_reset_queue(AP_MKQID(i, ap_domain_index));
13e742ba
RW
1280}
1281
1282static void ap_reset_all(void)
85eca850
RW
1283{
1284 int i, j;
1285
1286 for (i = 0; i < AP_DOMAINS; i++)
1287 for (j = 0; j < AP_DEVICES; j++)
1288 ap_reset_queue(AP_MKQID(j, i));
1289}
1290
1291static struct reset_call ap_reset_call = {
13e742ba 1292 .fn = ap_reset_all,
85eca850
RW
1293};
1294
1534c382 1295/**
1749a81d
FB
1296 * ap_module_init(): The module initialization code.
1297 *
1298 * Initializes the module.
1534c382
MS
1299 */
1300int __init ap_module_init(void)
1301{
1302 int rc, i;
1303
1304 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1305 printk(KERN_WARNING "Invalid param: domain = %d. "
1306 " Not loading.\n", ap_domain_index);
1307 return -EINVAL;
1308 }
1309 if (ap_instructions_available() != 0) {
1310 printk(KERN_WARNING "AP instructions not installed.\n");
1311 return -ENODEV;
1312 }
85eca850 1313 register_reset_call(&ap_reset_call);
1534c382
MS
1314
1315 /* Create /sys/bus/ap. */
1316 rc = bus_register(&ap_bus_type);
1317 if (rc)
1318 goto out;
1319 for (i = 0; ap_bus_attrs[i]; i++) {
1320 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1321 if (rc)
1322 goto out_bus;
1323 }
1324
1325 /* Create /sys/devices/ap. */
1326 ap_root_device = s390_root_dev_register("ap");
1327 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1328 if (rc)
1329 goto out_bus;
1330
1331 ap_work_queue = create_singlethread_workqueue("kapwork");
1332 if (!ap_work_queue) {
1333 rc = -ENOMEM;
1334 goto out_root;
1335 }
1336
1337 if (ap_select_domain() == 0)
1338 ap_scan_bus(NULL);
1339
1749a81d 1340 /* Setup the AP bus rescan timer. */
1534c382
MS
1341 init_timer(&ap_config_timer);
1342 ap_config_timer.function = ap_config_timeout;
1343 ap_config_timer.data = 0;
1344 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1345 add_timer(&ap_config_timer);
1346
1347 /* Start the low priority AP bus poll thread. */
1348 if (ap_thread_flag) {
1349 rc = ap_poll_thread_start();
1350 if (rc)
1351 goto out_work;
1352 }
1353
1354 return 0;
1355
1356out_work:
1357 del_timer_sync(&ap_config_timer);
1358 del_timer_sync(&ap_poll_timer);
1359 destroy_workqueue(ap_work_queue);
1360out_root:
1361 s390_root_dev_unregister(ap_root_device);
1362out_bus:
1363 while (i--)
1364 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1365 bus_unregister(&ap_bus_type);
1366out:
85eca850 1367 unregister_reset_call(&ap_reset_call);
1534c382
MS
1368 return rc;
1369}
1370
1371static int __ap_match_all(struct device *dev, void *data)
1372{
1373 return 1;
1374}
1375
1376/**
1749a81d
FB
1377 * ap_modules_exit(): The module termination code
1378 *
1379 * Terminates the module.
1534c382
MS
1380 */
1381void ap_module_exit(void)
1382{
1383 int i;
1384 struct device *dev;
1385
13e742ba 1386 ap_reset_domain();
1534c382
MS
1387 ap_poll_thread_stop();
1388 del_timer_sync(&ap_config_timer);
1389 del_timer_sync(&ap_poll_timer);
1390 destroy_workqueue(ap_work_queue);
13e742ba 1391 tasklet_kill(&ap_tasklet);
1534c382
MS
1392 s390_root_dev_unregister(ap_root_device);
1393 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1394 __ap_match_all)))
1395 {
1396 device_unregister(dev);
1397 put_device(dev);
1398 }
1399 for (i = 0; ap_bus_attrs[i]; i++)
1400 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1401 bus_unregister(&ap_bus_type);
85eca850 1402 unregister_reset_call(&ap_reset_call);
1534c382
MS
1403}
1404
1405#ifndef CONFIG_ZCRYPT_MONOLITHIC
1406module_init(ap_module_init);
1407module_exit(ap_module_exit);
1408#endif