blob: d0c6fd3b1c19a02af51665e657fc102829d4d488 [file] [log] [blame]
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001/*
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>
Ralph Wuerthner85eca852006-12-08 15:54:07 +010036#include <asm/reset.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020037
38#include "ap_bus.h"
39
40/* Some prototypes. */
Al Viro4927b3f2006-12-06 19:18:20 +000041static void ap_scan_bus(struct work_struct *);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020042static 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);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +020046static void ap_request_timeout(unsigned long);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020047
48/**
49 * Module description.
50 */
51MODULE_AUTHOR("IBM Corporation");
52MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
53 "Copyright 2006 IBM Corporation");
54MODULE_LICENSE("GPL");
55
56/**
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
Felix Beckb90b34c2008-02-09 18:24:30 +010064static int ap_thread_flag = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +020065module_param_named(poll_thread, ap_thread_flag, int, 0000);
Felix Beckb90b34c2008-02-09 18:24:30 +010066MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
Martin Schwidefsky1534c382006-09-20 15:58:25 +020067
68static struct device *ap_root_device = NULL;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +010069static DEFINE_SPINLOCK(ap_device_lock);
70static LIST_HEAD(ap_device_list);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020071
72/**
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;
Al Viro4927b3f2006-12-06 19:18:20 +000078static DECLARE_WORK(ap_config_work, ap_scan_bus);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020079
80/**
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/**
91 * Test if ap instructions are available.
92 *
93 * Returns 0 if the ap instructions are installed.
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/**
111 * 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
115 *
116 * Returns ap queue status structure.
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/**
133 * Reset adjunct processor queue.
134 * @qid: the ap queue number
135 *
136 * Returns ap queue status structure.
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/**
151 * 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
156 *
157 * Returns ap queue status structure.
158 *
159 * Condition code 1 on NQAP can't happen because the L bit is 1.
160 *
161 * Condition code 2 on NQAP also means the send is incomplete,
162 * because a segment boundary was reached. The NQAP is repeated.
163 */
164static inline struct ap_queue_status
165__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
166{
167 typedef struct { char _[length]; } msgblock;
168 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
169 register struct ap_queue_status reg1 asm ("1");
170 register unsigned long reg2 asm ("2") = (unsigned long) msg;
171 register unsigned long reg3 asm ("3") = (unsigned long) length;
172 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
173 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
174
175 asm volatile (
176 "0: .long 0xb2ad0042\n" /* DQAP */
177 " brc 2,0b"
178 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
179 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
180 : "cc" );
181 return reg1;
182}
183
184int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
185{
186 struct ap_queue_status status;
187
188 status = __ap_send(qid, psmid, msg, length);
189 switch (status.response_code) {
190 case AP_RESPONSE_NORMAL:
191 return 0;
192 case AP_RESPONSE_Q_FULL:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200193 case AP_RESPONSE_RESET_IN_PROGRESS:
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200194 return -EBUSY;
195 default: /* Device is gone. */
196 return -ENODEV;
197 }
198}
199EXPORT_SYMBOL(ap_send);
200
201/*
202 * Receive message from adjunct processor queue.
203 * @qid: the ap queue number
204 * @psmid: pointer to program supplied message identifier
205 * @msg: the message text
206 * @length: the message length
207 *
208 * Returns ap queue status structure.
209 *
210 * Condition code 1 on DQAP means the receive has taken place
211 * but only partially. The response is incomplete, hence the
212 * DQAP is repeated.
213 *
214 * Condition code 2 on DQAP also means the receive is incomplete,
215 * this time because a segment boundary was reached. Again, the
216 * DQAP is repeated.
217 *
218 * Note that gpr2 is used by the DQAP instruction to keep track of
219 * any 'residual' length, in case the instruction gets interrupted.
220 * Hence it gets zeroed before the instruction.
221 */
222static inline struct ap_queue_status
223__ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
224{
225 typedef struct { char _[length]; } msgblock;
226 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
227 register struct ap_queue_status reg1 asm ("1");
228 register unsigned long reg2 asm("2") = 0UL;
229 register unsigned long reg4 asm("4") = (unsigned long) msg;
230 register unsigned long reg5 asm("5") = (unsigned long) length;
231 register unsigned long reg6 asm("6") = 0UL;
232 register unsigned long reg7 asm("7") = 0UL;
233
234
235 asm volatile(
236 "0: .long 0xb2ae0064\n"
237 " brc 6,0b\n"
238 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
239 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
240 "=m" (*(msgblock *) msg) : : "cc" );
241 *psmid = (((unsigned long long) reg6) << 32) + reg7;
242 return reg1;
243}
244
245int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
246{
247 struct ap_queue_status status;
248
249 status = __ap_recv(qid, psmid, msg, length);
250 switch (status.response_code) {
251 case AP_RESPONSE_NORMAL:
252 return 0;
253 case AP_RESPONSE_NO_PENDING_REPLY:
254 if (status.queue_empty)
255 return -ENOENT;
256 return -EBUSY;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200257 case AP_RESPONSE_RESET_IN_PROGRESS:
258 return -EBUSY;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200259 default:
260 return -ENODEV;
261 }
262}
263EXPORT_SYMBOL(ap_recv);
264
265/**
266 * Check if an AP queue is available. The test is repeated for
267 * AP_MAX_RESET times.
268 * @qid: the ap queue number
269 * @queue_depth: pointer to queue depth value
270 * @device_type: pointer to device type value
271 */
272static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
273{
274 struct ap_queue_status status;
275 int t_depth, t_device_type, rc, i;
276
277 rc = -EBUSY;
278 for (i = 0; i < AP_MAX_RESET; i++) {
279 status = ap_test_queue(qid, &t_depth, &t_device_type);
280 switch (status.response_code) {
281 case AP_RESPONSE_NORMAL:
282 *queue_depth = t_depth + 1;
283 *device_type = t_device_type;
284 rc = 0;
285 break;
286 case AP_RESPONSE_Q_NOT_AVAIL:
287 rc = -ENODEV;
288 break;
289 case AP_RESPONSE_RESET_IN_PROGRESS:
290 break;
291 case AP_RESPONSE_DECONFIGURED:
292 rc = -ENODEV;
293 break;
294 case AP_RESPONSE_CHECKSTOPPED:
295 rc = -ENODEV;
296 break;
297 case AP_RESPONSE_BUSY:
298 break;
299 default:
300 BUG();
301 }
302 if (rc != -EBUSY)
303 break;
304 if (i < AP_MAX_RESET - 1)
305 udelay(5);
306 }
307 return rc;
308}
309
310/**
311 * Reset an AP queue and wait for it to become available again.
312 * @qid: the ap queue number
313 */
314static int ap_init_queue(ap_qid_t qid)
315{
316 struct ap_queue_status status;
317 int rc, dummy, i;
318
319 rc = -ENODEV;
320 status = ap_reset_queue(qid);
321 for (i = 0; i < AP_MAX_RESET; i++) {
322 switch (status.response_code) {
323 case AP_RESPONSE_NORMAL:
324 if (status.queue_empty)
325 rc = 0;
326 break;
327 case AP_RESPONSE_Q_NOT_AVAIL:
328 case AP_RESPONSE_DECONFIGURED:
329 case AP_RESPONSE_CHECKSTOPPED:
330 i = AP_MAX_RESET; /* return with -ENODEV */
331 break;
332 case AP_RESPONSE_RESET_IN_PROGRESS:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200333 rc = -EBUSY;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200334 case AP_RESPONSE_BUSY:
335 default:
336 break;
337 }
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200338 if (rc != -ENODEV && rc != -EBUSY)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200339 break;
340 if (i < AP_MAX_RESET - 1) {
341 udelay(5);
342 status = ap_test_queue(qid, &dummy, &dummy);
343 }
344 }
345 return rc;
346}
347
348/**
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200349 * Arm request timeout if a AP device was idle and a new request is submitted.
350 */
351static void ap_increase_queue_count(struct ap_device *ap_dev)
352{
353 int timeout = ap_dev->drv->request_timeout;
354
355 ap_dev->queue_count++;
356 if (ap_dev->queue_count == 1) {
357 mod_timer(&ap_dev->timeout, jiffies + timeout);
358 ap_dev->reset = AP_RESET_ARMED;
359 }
360}
361
362/**
363 * AP device is still alive, re-schedule request timeout if there are still
364 * pending requests.
365 */
366static void ap_decrease_queue_count(struct ap_device *ap_dev)
367{
368 int timeout = ap_dev->drv->request_timeout;
369
370 ap_dev->queue_count--;
371 if (ap_dev->queue_count > 0)
372 mod_timer(&ap_dev->timeout, jiffies + timeout);
373 else
374 /**
375 * The timeout timer should to be disabled now - since
376 * del_timer_sync() is very expensive, we just tell via the
377 * reset flag to ignore the pending timeout timer.
378 */
379 ap_dev->reset = AP_RESET_IGNORE;
380}
381
382/**
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200383 * AP device related attributes.
384 */
385static ssize_t ap_hwtype_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
387{
388 struct ap_device *ap_dev = to_ap_dev(dev);
389 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
390}
391static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
392
393static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
394 char *buf)
395{
396 struct ap_device *ap_dev = to_ap_dev(dev);
397 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
398}
399static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
400
401static ssize_t ap_request_count_show(struct device *dev,
402 struct device_attribute *attr,
403 char *buf)
404{
405 struct ap_device *ap_dev = to_ap_dev(dev);
406 int rc;
407
408 spin_lock_bh(&ap_dev->lock);
409 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
410 spin_unlock_bh(&ap_dev->lock);
411 return rc;
412}
413
414static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
415
416static ssize_t ap_modalias_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
418{
419 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
420}
421
422static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
423
424static struct attribute *ap_dev_attrs[] = {
425 &dev_attr_hwtype.attr,
426 &dev_attr_depth.attr,
427 &dev_attr_request_count.attr,
428 &dev_attr_modalias.attr,
429 NULL
430};
431static struct attribute_group ap_dev_attr_group = {
432 .attrs = ap_dev_attrs
433};
434
435/**
436 * AP bus driver registration/unregistration.
437 */
438static int ap_bus_match(struct device *dev, struct device_driver *drv)
439{
440 struct ap_device *ap_dev = to_ap_dev(dev);
441 struct ap_driver *ap_drv = to_ap_drv(drv);
442 struct ap_device_id *id;
443
444 /**
445 * Compare device type of the device with the list of
446 * supported types of the device_driver.
447 */
448 for (id = ap_drv->ids; id->match_flags; id++) {
449 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
450 (id->dev_type != ap_dev->device_type))
451 continue;
452 return 1;
453 }
454 return 0;
455}
456
457/**
458 * uevent function for AP devices. It sets up a single environment
459 * variable DEV_TYPE which contains the hardware device type.
460 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200461static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200462{
463 struct ap_device *ap_dev = to_ap_dev(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200464 int retval = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200465
466 if (!ap_dev)
467 return -ENODEV;
468
469 /* Set up DEV_TYPE environment variable. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200470 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700471 if (retval)
472 return retval;
473
Cornelia Huck66a4263b2006-12-04 15:40:10 +0100474 /* Add MODALIAS= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200475 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700476
Eric Rannaudbf624562007-03-30 22:23:12 -0700477 return retval;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200478}
479
480static struct bus_type ap_bus_type = {
481 .name = "ap",
482 .match = &ap_bus_match,
483 .uevent = &ap_uevent,
484};
485
486static int ap_device_probe(struct device *dev)
487{
488 struct ap_device *ap_dev = to_ap_dev(dev);
489 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
490 int rc;
491
492 ap_dev->drv = ap_drv;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +0100493 spin_lock_bh(&ap_device_lock);
494 list_add(&ap_dev->list, &ap_device_list);
495 spin_unlock_bh(&ap_device_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200496 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200497 return rc;
498}
499
500/**
501 * Flush all requests from the request/pending queue of an AP device.
502 * @ap_dev: pointer to the AP device.
503 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100504static void __ap_flush_queue(struct ap_device *ap_dev)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200505{
506 struct ap_message *ap_msg, *next;
507
508 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
509 list_del_init(&ap_msg->list);
510 ap_dev->pendingq_count--;
511 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
512 }
513 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
514 list_del_init(&ap_msg->list);
515 ap_dev->requestq_count--;
516 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
517 }
518}
519
520void ap_flush_queue(struct ap_device *ap_dev)
521{
522 spin_lock_bh(&ap_dev->lock);
523 __ap_flush_queue(ap_dev);
524 spin_unlock_bh(&ap_dev->lock);
525}
526EXPORT_SYMBOL(ap_flush_queue);
527
528static int ap_device_remove(struct device *dev)
529{
530 struct ap_device *ap_dev = to_ap_dev(dev);
531 struct ap_driver *ap_drv = ap_dev->drv;
532
Ralph Wuerthner4e562962006-10-04 20:02:05 +0200533 ap_flush_queue(ap_dev);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200534 del_timer_sync(&ap_dev->timeout);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200535 if (ap_drv->remove)
536 ap_drv->remove(ap_dev);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +0100537 spin_lock_bh(&ap_device_lock);
538 list_del_init(&ap_dev->list);
539 spin_unlock_bh(&ap_device_lock);
Ralph Wuerthnere675c0d2007-03-26 20:42:43 +0200540 spin_lock_bh(&ap_dev->lock);
541 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
542 spin_unlock_bh(&ap_dev->lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200543 return 0;
544}
545
546int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
547 char *name)
548{
549 struct device_driver *drv = &ap_drv->driver;
550
551 drv->bus = &ap_bus_type;
552 drv->probe = ap_device_probe;
553 drv->remove = ap_device_remove;
554 drv->owner = owner;
555 drv->name = name;
556 return driver_register(drv);
557}
558EXPORT_SYMBOL(ap_driver_register);
559
560void ap_driver_unregister(struct ap_driver *ap_drv)
561{
562 driver_unregister(&ap_drv->driver);
563}
564EXPORT_SYMBOL(ap_driver_unregister);
565
566/**
567 * AP bus attributes.
568 */
569static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
570{
571 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
572}
573
574static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
575
576static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
577{
578 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
579}
580
581static ssize_t ap_config_time_store(struct bus_type *bus,
582 const char *buf, size_t count)
583{
584 int time;
585
586 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
587 return -EINVAL;
588 ap_config_time = time;
589 if (!timer_pending(&ap_config_timer) ||
590 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
591 ap_config_timer.expires = jiffies + ap_config_time * HZ;
592 add_timer(&ap_config_timer);
593 }
594 return count;
595}
596
597static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
598
599static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
600{
601 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
602}
603
604static ssize_t ap_poll_thread_store(struct bus_type *bus,
605 const char *buf, size_t count)
606{
607 int flag, rc;
608
609 if (sscanf(buf, "%d\n", &flag) != 1)
610 return -EINVAL;
611 if (flag) {
612 rc = ap_poll_thread_start();
613 if (rc)
614 return rc;
615 }
616 else
617 ap_poll_thread_stop();
618 return count;
619}
620
621static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
622
623static struct bus_attribute *const ap_bus_attrs[] = {
624 &bus_attr_ap_domain,
625 &bus_attr_config_time,
626 &bus_attr_poll_thread,
627 NULL
628};
629
630/**
631 * Pick one of the 16 ap domains.
632 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100633static int ap_select_domain(void)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200634{
635 int queue_depth, device_type, count, max_count, best_domain;
636 int rc, i, j;
637
638 /**
639 * We want to use a single domain. Either the one specified with
640 * the "domain=" parameter or the domain with the maximum number
641 * of devices.
642 */
643 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
644 /* Domain has already been selected. */
645 return 0;
646 best_domain = -1;
647 max_count = 0;
648 for (i = 0; i < AP_DOMAINS; i++) {
649 count = 0;
650 for (j = 0; j < AP_DEVICES; j++) {
651 ap_qid_t qid = AP_MKQID(j, i);
652 rc = ap_query_queue(qid, &queue_depth, &device_type);
653 if (rc)
654 continue;
655 count++;
656 }
657 if (count > max_count) {
658 max_count = count;
659 best_domain = i;
660 }
661 }
662 if (best_domain >= 0){
663 ap_domain_index = best_domain;
664 return 0;
665 }
666 return -ENODEV;
667}
668
669/**
670 * Find the device type if query queue returned a device type of 0.
671 * @ap_dev: pointer to the AP device.
672 */
673static int ap_probe_device_type(struct ap_device *ap_dev)
674{
675 static unsigned char msg[] = {
676 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
677 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
678 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
679 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
680 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
681 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
682 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
683 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
684 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
685 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
686 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
687 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
688 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
689 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
690 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
691 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
692 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
693 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
694 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
695 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
696 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
697 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
698 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
699 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
700 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
701 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
702 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
703 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
704 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
705 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
706 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
707 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
708 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
709 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
710 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
711 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
712 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
713 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
714 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
715 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
716 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
717 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
718 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
719 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
720 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
721 };
722 struct ap_queue_status status;
723 unsigned long long psmid;
724 char *reply;
725 int rc, i;
726
727 reply = (void *) get_zeroed_page(GFP_KERNEL);
728 if (!reply) {
729 rc = -ENOMEM;
730 goto out;
731 }
732
733 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
734 msg, sizeof(msg));
735 if (status.response_code != AP_RESPONSE_NORMAL) {
736 rc = -ENODEV;
737 goto out_free;
738 }
739
740 /* Wait for the test message to complete. */
741 for (i = 0; i < 6; i++) {
742 mdelay(300);
743 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
744 if (status.response_code == AP_RESPONSE_NORMAL &&
745 psmid == 0x0102030405060708ULL)
746 break;
747 }
748 if (i < 6) {
749 /* Got an answer. */
750 if (reply[0] == 0x00 && reply[1] == 0x86)
751 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
752 else
753 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
754 rc = 0;
755 } else
756 rc = -ENODEV;
757
758out_free:
759 free_page((unsigned long) reply);
760out:
761 return rc;
762}
763
764/**
765 * Scan the ap bus for new devices.
766 */
767static int __ap_scan_bus(struct device *dev, void *data)
768{
769 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
770}
771
772static void ap_device_release(struct device *dev)
773{
774 struct ap_device *ap_dev = to_ap_dev(dev);
775
776 kfree(ap_dev);
777}
778
Al Viro4927b3f2006-12-06 19:18:20 +0000779static void ap_scan_bus(struct work_struct *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200780{
781 struct ap_device *ap_dev;
782 struct device *dev;
783 ap_qid_t qid;
784 int queue_depth, device_type;
785 int rc, i;
786
787 if (ap_select_domain() != 0)
788 return;
789 for (i = 0; i < AP_DEVICES; i++) {
790 qid = AP_MKQID(i, ap_domain_index);
791 dev = bus_find_device(&ap_bus_type, NULL,
792 (void *)(unsigned long)qid,
793 __ap_scan_bus);
Ralph Wuerthnerf3b017d2006-10-27 12:39:26 +0200794 rc = ap_query_queue(qid, &queue_depth, &device_type);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +0200795 if (dev) {
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200796 if (rc == -EBUSY) {
797 set_current_state(TASK_UNINTERRUPTIBLE);
798 schedule_timeout(AP_RESET_TIMEOUT);
799 rc = ap_query_queue(qid, &queue_depth,
800 &device_type);
801 }
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +0200802 ap_dev = to_ap_dev(dev);
803 spin_lock_bh(&ap_dev->lock);
804 if (rc || ap_dev->unregistered) {
805 spin_unlock_bh(&ap_dev->lock);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +0200806 device_unregister(dev);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200807 put_device(dev);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +0200808 continue;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200809 }
810 spin_unlock_bh(&ap_dev->lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200811 put_device(dev);
812 continue;
813 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200814 if (rc)
815 continue;
816 rc = ap_init_queue(qid);
817 if (rc)
818 continue;
819 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
820 if (!ap_dev)
821 break;
822 ap_dev->qid = qid;
823 ap_dev->queue_depth = queue_depth;
Ralph Wuerthner4e562962006-10-04 20:02:05 +0200824 ap_dev->unregistered = 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200825 spin_lock_init(&ap_dev->lock);
826 INIT_LIST_HEAD(&ap_dev->pendingq);
827 INIT_LIST_HEAD(&ap_dev->requestq);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +0100828 INIT_LIST_HEAD(&ap_dev->list);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200829 setup_timer(&ap_dev->timeout, ap_request_timeout,
830 (unsigned long) ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200831 if (device_type == 0)
832 ap_probe_device_type(ap_dev);
833 else
834 ap_dev->device_type = device_type;
835
836 ap_dev->device.bus = &ap_bus_type;
837 ap_dev->device.parent = ap_root_device;
838 snprintf(ap_dev->device.bus_id, BUS_ID_SIZE, "card%02x",
839 AP_QID_DEVICE(ap_dev->qid));
840 ap_dev->device.release = ap_device_release;
841 rc = device_register(&ap_dev->device);
842 if (rc) {
843 kfree(ap_dev);
844 continue;
845 }
846 /* Add device attributes. */
847 rc = sysfs_create_group(&ap_dev->device.kobj,
848 &ap_dev_attr_group);
Ralph Wuerthner4e562962006-10-04 20:02:05 +0200849 if (!rc) {
850 spin_lock_bh(&ap_dev->lock);
851 ap_dev->unregistered = 0;
852 spin_unlock_bh(&ap_dev->lock);
853 }
854 else
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200855 device_unregister(&ap_dev->device);
856 }
857}
858
859static void
860ap_config_timeout(unsigned long ptr)
861{
862 queue_work(ap_work_queue, &ap_config_work);
863 ap_config_timer.expires = jiffies + ap_config_time * HZ;
864 add_timer(&ap_config_timer);
865}
866
867/**
868 * Set up the timer to run the poll tasklet
869 */
870static inline void ap_schedule_poll_timer(void)
871{
872 if (timer_pending(&ap_poll_timer))
873 return;
874 mod_timer(&ap_poll_timer, jiffies + AP_POLL_TIME);
875}
876
877/**
878 * Receive pending reply messages from an AP device.
879 * @ap_dev: pointer to the AP device
880 * @flags: pointer to control flags, bit 2^0 is set if another poll is
881 * required, bit 2^1 is set if the poll timer needs to get armed
882 * Returns 0 if the device is still present, -ENODEV if not.
883 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100884static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200885{
886 struct ap_queue_status status;
887 struct ap_message *ap_msg;
888
889 if (ap_dev->queue_count <= 0)
890 return 0;
891 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
892 ap_dev->reply->message, ap_dev->reply->length);
893 switch (status.response_code) {
894 case AP_RESPONSE_NORMAL:
895 atomic_dec(&ap_poll_requests);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200896 ap_decrease_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200897 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
898 if (ap_msg->psmid != ap_dev->reply->psmid)
899 continue;
900 list_del_init(&ap_msg->list);
901 ap_dev->pendingq_count--;
902 ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
903 break;
904 }
905 if (ap_dev->queue_count > 0)
906 *flags |= 1;
907 break;
908 case AP_RESPONSE_NO_PENDING_REPLY:
909 if (status.queue_empty) {
910 /* The card shouldn't forget requests but who knows. */
Ralph Wuerthnere675c0d2007-03-26 20:42:43 +0200911 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200912 ap_dev->queue_count = 0;
913 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
914 ap_dev->requestq_count += ap_dev->pendingq_count;
915 ap_dev->pendingq_count = 0;
916 } else
917 *flags |= 2;
918 break;
919 default:
920 return -ENODEV;
921 }
922 return 0;
923}
924
925/**
926 * Send messages from the request queue to an AP device.
927 * @ap_dev: pointer to the AP device
928 * @flags: pointer to control flags, bit 2^0 is set if another poll is
929 * required, bit 2^1 is set if the poll timer needs to get armed
930 * Returns 0 if the device is still present, -ENODEV if not.
931 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100932static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200933{
934 struct ap_queue_status status;
935 struct ap_message *ap_msg;
936
937 if (ap_dev->requestq_count <= 0 ||
938 ap_dev->queue_count >= ap_dev->queue_depth)
939 return 0;
940 /* Start the next request on the queue. */
941 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
942 status = __ap_send(ap_dev->qid, ap_msg->psmid,
943 ap_msg->message, ap_msg->length);
944 switch (status.response_code) {
945 case AP_RESPONSE_NORMAL:
946 atomic_inc(&ap_poll_requests);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200947 ap_increase_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200948 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
949 ap_dev->requestq_count--;
950 ap_dev->pendingq_count++;
951 if (ap_dev->queue_count < ap_dev->queue_depth &&
952 ap_dev->requestq_count > 0)
953 *flags |= 1;
954 *flags |= 2;
955 break;
956 case AP_RESPONSE_Q_FULL:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200957 case AP_RESPONSE_RESET_IN_PROGRESS:
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200958 *flags |= 2;
959 break;
960 case AP_RESPONSE_MESSAGE_TOO_BIG:
961 return -EINVAL;
962 default:
963 return -ENODEV;
964 }
965 return 0;
966}
967
968/**
969 * Poll AP device for pending replies and send new messages. If either
970 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
971 * @ap_dev: pointer to the bus device
972 * @flags: pointer to control flags, bit 2^0 is set if another poll is
973 * required, bit 2^1 is set if the poll timer needs to get armed
974 * Returns 0.
975 */
976static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
977{
978 int rc;
979
980 rc = ap_poll_read(ap_dev, flags);
981 if (rc)
982 return rc;
983 return ap_poll_write(ap_dev, flags);
984}
985
986/**
987 * Queue a message to a device.
988 * @ap_dev: pointer to the AP device
989 * @ap_msg: the message to be queued
990 */
991static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
992{
993 struct ap_queue_status status;
994
995 if (list_empty(&ap_dev->requestq) &&
996 ap_dev->queue_count < ap_dev->queue_depth) {
997 status = __ap_send(ap_dev->qid, ap_msg->psmid,
998 ap_msg->message, ap_msg->length);
999 switch (status.response_code) {
1000 case AP_RESPONSE_NORMAL:
1001 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1002 atomic_inc(&ap_poll_requests);
1003 ap_dev->pendingq_count++;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001004 ap_increase_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001005 ap_dev->total_request_count++;
1006 break;
1007 case AP_RESPONSE_Q_FULL:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001008 case AP_RESPONSE_RESET_IN_PROGRESS:
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001009 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1010 ap_dev->requestq_count++;
1011 ap_dev->total_request_count++;
1012 return -EBUSY;
1013 case AP_RESPONSE_MESSAGE_TOO_BIG:
1014 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1015 return -EINVAL;
1016 default: /* Device is gone. */
1017 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1018 return -ENODEV;
1019 }
1020 } else {
1021 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1022 ap_dev->requestq_count++;
1023 ap_dev->total_request_count++;
1024 return -EBUSY;
1025 }
1026 ap_schedule_poll_timer();
1027 return 0;
1028}
1029
1030void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1031{
1032 unsigned long flags;
1033 int rc;
1034
1035 spin_lock_bh(&ap_dev->lock);
1036 if (!ap_dev->unregistered) {
1037 /* Make room on the queue by polling for finished requests. */
1038 rc = ap_poll_queue(ap_dev, &flags);
1039 if (!rc)
1040 rc = __ap_queue_message(ap_dev, ap_msg);
1041 if (!rc)
1042 wake_up(&ap_poll_wait);
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001043 if (rc == -ENODEV)
1044 ap_dev->unregistered = 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001045 } else {
1046 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001047 rc = -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001048 }
1049 spin_unlock_bh(&ap_dev->lock);
1050 if (rc == -ENODEV)
1051 device_unregister(&ap_dev->device);
1052}
1053EXPORT_SYMBOL(ap_queue_message);
1054
1055/**
1056 * Cancel a crypto request. This is done by removing the request
1057 * from the devive pendingq or requestq queue. Note that the
1058 * request stays on the AP queue. When it finishes the message
1059 * reply will be discarded because the psmid can't be found.
1060 * @ap_dev: AP device that has the message queued
1061 * @ap_msg: the message that is to be removed
1062 */
1063void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1064{
1065 struct ap_message *tmp;
1066
1067 spin_lock_bh(&ap_dev->lock);
1068 if (!list_empty(&ap_msg->list)) {
1069 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1070 if (tmp->psmid == ap_msg->psmid) {
1071 ap_dev->pendingq_count--;
1072 goto found;
1073 }
1074 ap_dev->requestq_count--;
1075 found:
1076 list_del_init(&ap_msg->list);
1077 }
1078 spin_unlock_bh(&ap_dev->lock);
1079}
1080EXPORT_SYMBOL(ap_cancel_message);
1081
1082/**
1083 * AP receive polling for finished AP requests
1084 */
1085static void ap_poll_timeout(unsigned long unused)
1086{
1087 tasklet_schedule(&ap_tasklet);
1088}
1089
1090/**
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001091 * Reset a not responding AP device and move all requests from the
1092 * pending queue to the request queue.
1093 */
1094static void ap_reset(struct ap_device *ap_dev)
1095{
1096 int rc;
1097
1098 ap_dev->reset = AP_RESET_IGNORE;
1099 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1100 ap_dev->queue_count = 0;
1101 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1102 ap_dev->requestq_count += ap_dev->pendingq_count;
1103 ap_dev->pendingq_count = 0;
1104 rc = ap_init_queue(ap_dev->qid);
1105 if (rc == -ENODEV)
1106 ap_dev->unregistered = 1;
1107}
1108
1109/**
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001110 * Poll all AP devices on the bus in a round robin fashion. Continue
1111 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1112 * of the control flags has been set arm the poll timer.
1113 */
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001114static int __ap_poll_all(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001115{
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001116 spin_lock(&ap_dev->lock);
1117 if (!ap_dev->unregistered) {
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001118 if (ap_poll_queue(ap_dev, flags))
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001119 ap_dev->unregistered = 1;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001120 if (ap_dev->reset == AP_RESET_DO)
1121 ap_reset(ap_dev);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001122 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001123 spin_unlock(&ap_dev->lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001124 return 0;
1125}
1126
1127static void ap_poll_all(unsigned long dummy)
1128{
1129 unsigned long flags;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001130 struct ap_device *ap_dev;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001131
1132 do {
1133 flags = 0;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001134 spin_lock(&ap_device_lock);
1135 list_for_each_entry(ap_dev, &ap_device_list, list) {
1136 __ap_poll_all(ap_dev, &flags);
1137 }
1138 spin_unlock(&ap_device_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001139 } while (flags & 1);
1140 if (flags & 2)
1141 ap_schedule_poll_timer();
1142}
1143
1144/**
1145 * AP bus poll thread. The purpose of this thread is to poll for
1146 * finished requests in a loop if there is a "free" cpu - that is
1147 * a cpu that doesn't have anything better to do. The polling stops
1148 * as soon as there is another task or if all messages have been
1149 * delivered.
1150 */
1151static int ap_poll_thread(void *data)
1152{
1153 DECLARE_WAITQUEUE(wait, current);
1154 unsigned long flags;
1155 int requests;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001156 struct ap_device *ap_dev;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001157
Christian Borntraegerd83682b2006-10-06 16:38:22 +02001158 set_user_nice(current, 19);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001159 while (1) {
1160 if (need_resched()) {
1161 schedule();
1162 continue;
1163 }
1164 add_wait_queue(&ap_poll_wait, &wait);
1165 set_current_state(TASK_INTERRUPTIBLE);
1166 if (kthread_should_stop())
1167 break;
1168 requests = atomic_read(&ap_poll_requests);
1169 if (requests <= 0)
1170 schedule();
1171 set_current_state(TASK_RUNNING);
1172 remove_wait_queue(&ap_poll_wait, &wait);
1173
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001174 flags = 0;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001175 spin_lock_bh(&ap_device_lock);
1176 list_for_each_entry(ap_dev, &ap_device_list, list) {
1177 __ap_poll_all(ap_dev, &flags);
1178 }
1179 spin_unlock_bh(&ap_device_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001180 }
1181 set_current_state(TASK_RUNNING);
1182 remove_wait_queue(&ap_poll_wait, &wait);
1183 return 0;
1184}
1185
1186static int ap_poll_thread_start(void)
1187{
1188 int rc;
1189
1190 mutex_lock(&ap_poll_thread_mutex);
1191 if (!ap_poll_kthread) {
1192 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1193 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1194 if (rc)
1195 ap_poll_kthread = NULL;
1196 }
1197 else
1198 rc = 0;
1199 mutex_unlock(&ap_poll_thread_mutex);
1200 return rc;
1201}
1202
1203static void ap_poll_thread_stop(void)
1204{
1205 mutex_lock(&ap_poll_thread_mutex);
1206 if (ap_poll_kthread) {
1207 kthread_stop(ap_poll_kthread);
1208 ap_poll_kthread = NULL;
1209 }
1210 mutex_unlock(&ap_poll_thread_mutex);
1211}
1212
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001213/**
1214 * Handling of request timeouts
1215 */
1216static void ap_request_timeout(unsigned long data)
1217{
1218 struct ap_device *ap_dev = (struct ap_device *) data;
1219
1220 if (ap_dev->reset == AP_RESET_ARMED)
1221 ap_dev->reset = AP_RESET_DO;
1222}
1223
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001224static void ap_reset_domain(void)
1225{
1226 int i;
1227
Ralph Wuerthner39aa7cf2007-10-12 16:11:29 +02001228 if (ap_domain_index != -1)
1229 for (i = 0; i < AP_DEVICES; i++)
1230 ap_reset_queue(AP_MKQID(i, ap_domain_index));
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001231}
1232
1233static void ap_reset_all(void)
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001234{
1235 int i, j;
1236
1237 for (i = 0; i < AP_DOMAINS; i++)
1238 for (j = 0; j < AP_DEVICES; j++)
1239 ap_reset_queue(AP_MKQID(j, i));
1240}
1241
1242static struct reset_call ap_reset_call = {
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001243 .fn = ap_reset_all,
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001244};
1245
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001246/**
1247 * The module initialization code.
1248 */
1249int __init ap_module_init(void)
1250{
1251 int rc, i;
1252
1253 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1254 printk(KERN_WARNING "Invalid param: domain = %d. "
1255 " Not loading.\n", ap_domain_index);
1256 return -EINVAL;
1257 }
1258 if (ap_instructions_available() != 0) {
1259 printk(KERN_WARNING "AP instructions not installed.\n");
1260 return -ENODEV;
1261 }
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001262 register_reset_call(&ap_reset_call);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001263
1264 /* Create /sys/bus/ap. */
1265 rc = bus_register(&ap_bus_type);
1266 if (rc)
1267 goto out;
1268 for (i = 0; ap_bus_attrs[i]; i++) {
1269 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1270 if (rc)
1271 goto out_bus;
1272 }
1273
1274 /* Create /sys/devices/ap. */
1275 ap_root_device = s390_root_dev_register("ap");
1276 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1277 if (rc)
1278 goto out_bus;
1279
1280 ap_work_queue = create_singlethread_workqueue("kapwork");
1281 if (!ap_work_queue) {
1282 rc = -ENOMEM;
1283 goto out_root;
1284 }
1285
1286 if (ap_select_domain() == 0)
1287 ap_scan_bus(NULL);
1288
1289 /* Setup the ap bus rescan timer. */
1290 init_timer(&ap_config_timer);
1291 ap_config_timer.function = ap_config_timeout;
1292 ap_config_timer.data = 0;
1293 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1294 add_timer(&ap_config_timer);
1295
1296 /* Start the low priority AP bus poll thread. */
1297 if (ap_thread_flag) {
1298 rc = ap_poll_thread_start();
1299 if (rc)
1300 goto out_work;
1301 }
1302
1303 return 0;
1304
1305out_work:
1306 del_timer_sync(&ap_config_timer);
1307 del_timer_sync(&ap_poll_timer);
1308 destroy_workqueue(ap_work_queue);
1309out_root:
1310 s390_root_dev_unregister(ap_root_device);
1311out_bus:
1312 while (i--)
1313 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1314 bus_unregister(&ap_bus_type);
1315out:
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001316 unregister_reset_call(&ap_reset_call);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001317 return rc;
1318}
1319
1320static int __ap_match_all(struct device *dev, void *data)
1321{
1322 return 1;
1323}
1324
1325/**
1326 * The module termination code
1327 */
1328void ap_module_exit(void)
1329{
1330 int i;
1331 struct device *dev;
1332
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001333 ap_reset_domain();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001334 ap_poll_thread_stop();
1335 del_timer_sync(&ap_config_timer);
1336 del_timer_sync(&ap_poll_timer);
1337 destroy_workqueue(ap_work_queue);
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001338 tasklet_kill(&ap_tasklet);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001339 s390_root_dev_unregister(ap_root_device);
1340 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1341 __ap_match_all)))
1342 {
1343 device_unregister(dev);
1344 put_device(dev);
1345 }
1346 for (i = 0; ap_bus_attrs[i]; i++)
1347 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1348 bus_unregister(&ap_bus_type);
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001349 unregister_reset_call(&ap_reset_call);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001350}
1351
1352#ifndef CONFIG_ZCRYPT_MONOLITHIC
1353module_init(ap_module_init);
1354module_exit(ap_module_exit);
1355#endif