blob: 090b32a339c6042863862f8ab8006b7a47352896 [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>
Felix Beckcb17a632008-12-25 13:38:41 +01008 * Felix Beck <felix.beck@de.ibm.com>
Martin Schwidefsky1534c382006-09-20 15:58:25 +02009 *
10 * Adjunct processor bus.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
Martin Schwidefsky136f7a12008-12-25 13:39:46 +010027#define KMSG_COMPONENT "ap"
28#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29
Martin Schwidefsky1534c382006-09-20 15:58:25 +020030#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>
36#include <linux/notifier.h>
37#include <linux/kthread.h>
38#include <linux/mutex.h>
Ralph Wuerthner85eca852006-12-08 15:54:07 +010039#include <asm/reset.h>
Felix Beckcb17a632008-12-25 13:38:41 +010040#include <asm/airq.h>
41#include <asm/atomic.h>
42#include <asm/system.h>
43#include <asm/isc.h>
Felix Beckfe137232008-07-14 09:59:08 +020044#include <linux/hrtimer.h>
45#include <linux/ktime.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020046
47#include "ap_bus.h"
48
49/* Some prototypes. */
Al Viro4927b3f2006-12-06 19:18:20 +000050static void ap_scan_bus(struct work_struct *);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020051static void ap_poll_all(unsigned long);
Felix Beckfe137232008-07-14 09:59:08 +020052static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020053static int ap_poll_thread_start(void);
54static void ap_poll_thread_stop(void);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +020055static void ap_request_timeout(unsigned long);
Felix Beckcb17a632008-12-25 13:38:41 +010056static inline void ap_schedule_poll_timer(void);
Felix Beck772f5472009-06-22 12:08:16 +020057static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
58static int ap_device_remove(struct device *dev);
59static int ap_device_probe(struct device *dev);
60static void ap_interrupt_handler(void *unused1, void *unused2);
61static void ap_reset(struct ap_device *ap_dev);
62static void ap_config_timeout(unsigned long ptr);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020063
Felix Beck1749a812008-04-17 07:46:28 +020064/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +020065 * Module description.
66 */
67MODULE_AUTHOR("IBM Corporation");
68MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
69 "Copyright 2006 IBM Corporation");
70MODULE_LICENSE("GPL");
71
Felix Beck1749a812008-04-17 07:46:28 +020072/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +020073 * Module parameter
74 */
75int ap_domain_index = -1; /* Adjunct Processor Domain Index */
76module_param_named(domain, ap_domain_index, int, 0000);
77MODULE_PARM_DESC(domain, "domain index for ap devices");
78EXPORT_SYMBOL(ap_domain_index);
79
Felix Beckb90b34c2008-02-09 18:24:30 +010080static int ap_thread_flag = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +020081module_param_named(poll_thread, ap_thread_flag, int, 0000);
Felix Beckb90b34c2008-02-09 18:24:30 +010082MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
Martin Schwidefsky1534c382006-09-20 15:58:25 +020083
84static struct device *ap_root_device = NULL;
Christian Maaser43c207e62008-12-25 13:38:42 +010085static DEFINE_SPINLOCK(ap_device_list_lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +010086static LIST_HEAD(ap_device_list);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020087
Felix Beck1749a812008-04-17 07:46:28 +020088/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +020089 * Workqueue & timer for bus rescan.
90 */
91static struct workqueue_struct *ap_work_queue;
92static struct timer_list ap_config_timer;
93static int ap_config_time = AP_CONFIG_TIME;
Al Viro4927b3f2006-12-06 19:18:20 +000094static DECLARE_WORK(ap_config_work, ap_scan_bus);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020095
Felix Beck1749a812008-04-17 07:46:28 +020096/*
Felix Beckcb17a632008-12-25 13:38:41 +010097 * Tasklet & timer for AP request polling and interrupts
Martin Schwidefsky1534c382006-09-20 15:58:25 +020098 */
Martin Schwidefsky1534c382006-09-20 15:58:25 +020099static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
100static atomic_t ap_poll_requests = ATOMIC_INIT(0);
101static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
102static struct task_struct *ap_poll_kthread = NULL;
103static DEFINE_MUTEX(ap_poll_thread_mutex);
Felix Beckcb17a632008-12-25 13:38:41 +0100104static void *ap_interrupt_indicator;
Felix Beckfe137232008-07-14 09:59:08 +0200105static struct hrtimer ap_poll_timer;
106/* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
107 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
108static unsigned long long poll_timeout = 250000;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200109
Felix Beck772f5472009-06-22 12:08:16 +0200110/* Suspend flag */
111static int ap_suspend_flag;
112static struct bus_type ap_bus_type;
113
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200114/**
Felix Beckcb17a632008-12-25 13:38:41 +0100115 * ap_using_interrupts() - Returns non-zero if interrupt support is
116 * available.
117 */
118static inline int ap_using_interrupts(void)
119{
120 return ap_interrupt_indicator != NULL;
121}
122
123/**
Felix Beck1749a812008-04-17 07:46:28 +0200124 * ap_intructions_available() - Test if AP instructions are available.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200125 *
Felix Beck1749a812008-04-17 07:46:28 +0200126 * Returns 0 if the AP instructions are installed.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200127 */
128static inline int ap_instructions_available(void)
129{
130 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
131 register unsigned long reg1 asm ("1") = -ENODEV;
132 register unsigned long reg2 asm ("2") = 0UL;
133
134 asm volatile(
135 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
136 "0: la %1,0\n"
137 "1:\n"
138 EX_TABLE(0b, 1b)
139 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
140 return reg1;
141}
142
143/**
Felix Beckcb17a632008-12-25 13:38:41 +0100144 * ap_interrupts_available(): Test if AP interrupts are available.
145 *
146 * Returns 1 if AP interrupts are available.
147 */
148static int ap_interrupts_available(void)
149{
150 unsigned long long facility_bits[2];
151
152 if (stfle(facility_bits, 2) <= 1)
153 return 0;
154 if (!(facility_bits[0] & (1ULL << 61)) ||
155 !(facility_bits[1] & (1ULL << 62)))
156 return 0;
157 return 1;
158}
159
160/**
Felix Beck1749a812008-04-17 07:46:28 +0200161 * ap_test_queue(): Test adjunct processor queue.
162 * @qid: The AP queue number
163 * @queue_depth: Pointer to queue depth value
164 * @device_type: Pointer to device type value
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200165 *
Felix Beck1749a812008-04-17 07:46:28 +0200166 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200167 */
168static inline struct ap_queue_status
169ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
170{
171 register unsigned long reg0 asm ("0") = qid;
172 register struct ap_queue_status reg1 asm ("1");
173 register unsigned long reg2 asm ("2") = 0UL;
174
175 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
176 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
177 *device_type = (int) (reg2 >> 24);
178 *queue_depth = (int) (reg2 & 0xff);
179 return reg1;
180}
181
182/**
Felix Beck1749a812008-04-17 07:46:28 +0200183 * ap_reset_queue(): Reset adjunct processor queue.
184 * @qid: The AP queue number
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200185 *
Felix Beck1749a812008-04-17 07:46:28 +0200186 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200187 */
188static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
189{
190 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
191 register struct ap_queue_status reg1 asm ("1");
192 register unsigned long reg2 asm ("2") = 0UL;
193
194 asm volatile(
195 ".long 0xb2af0000" /* PQAP(RAPQ) */
196 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
197 return reg1;
198}
199
Felix Beckcb17a632008-12-25 13:38:41 +0100200#ifdef CONFIG_64BIT
201/**
202 * ap_queue_interruption_control(): Enable interruption for a specific AP.
203 * @qid: The AP queue number
204 * @ind: The notification indicator byte
205 *
206 * Returns AP queue status.
207 */
208static inline struct ap_queue_status
209ap_queue_interruption_control(ap_qid_t qid, void *ind)
210{
211 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
212 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
213 register struct ap_queue_status reg1_out asm ("1");
214 register void *reg2 asm ("2") = ind;
215 asm volatile(
216 ".long 0xb2af0000" /* PQAP(RAPQ) */
217 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
218 :
219 : "cc" );
220 return reg1_out;
221}
222#endif
223
224/**
225 * ap_queue_enable_interruption(): Enable interruption on an AP.
226 * @qid: The AP queue number
227 * @ind: the notification indicator byte
228 *
229 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
230 * on the return value it waits a while and tests the AP queue if interrupts
231 * have been switched on using ap_test_queue().
232 */
233static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
234{
235#ifdef CONFIG_64BIT
236 struct ap_queue_status status;
237 int t_depth, t_device_type, rc, i;
238
239 rc = -EBUSY;
240 status = ap_queue_interruption_control(qid, ind);
241
242 for (i = 0; i < AP_MAX_RESET; i++) {
243 switch (status.response_code) {
244 case AP_RESPONSE_NORMAL:
245 if (status.int_enabled)
246 return 0;
247 break;
248 case AP_RESPONSE_RESET_IN_PROGRESS:
249 case AP_RESPONSE_BUSY:
250 break;
251 case AP_RESPONSE_Q_NOT_AVAIL:
252 case AP_RESPONSE_DECONFIGURED:
253 case AP_RESPONSE_CHECKSTOPPED:
254 case AP_RESPONSE_INVALID_ADDRESS:
255 return -ENODEV;
256 case AP_RESPONSE_OTHERWISE_CHANGED:
257 if (status.int_enabled)
258 return 0;
259 break;
260 default:
261 break;
262 }
263 if (i < AP_MAX_RESET - 1) {
264 udelay(5);
265 status = ap_test_queue(qid, &t_depth, &t_device_type);
266 }
267 }
268 return rc;
269#else
270 return -EINVAL;
271#endif
272}
273
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200274/**
Felix Beck1749a812008-04-17 07:46:28 +0200275 * __ap_send(): Send message to adjunct processor queue.
276 * @qid: The AP queue number
277 * @psmid: The program supplied message identifier
278 * @msg: The message text
279 * @length: The message length
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200280 *
Felix Beck1749a812008-04-17 07:46:28 +0200281 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200282 * Condition code 1 on NQAP can't happen because the L bit is 1.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200283 * Condition code 2 on NQAP also means the send is incomplete,
284 * because a segment boundary was reached. The NQAP is repeated.
285 */
286static inline struct ap_queue_status
287__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
288{
289 typedef struct { char _[length]; } msgblock;
290 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
291 register struct ap_queue_status reg1 asm ("1");
292 register unsigned long reg2 asm ("2") = (unsigned long) msg;
293 register unsigned long reg3 asm ("3") = (unsigned long) length;
294 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
295 register unsigned long reg5 asm ("5") = (unsigned int) psmid;
296
297 asm volatile (
298 "0: .long 0xb2ad0042\n" /* DQAP */
299 " brc 2,0b"
300 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
301 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
302 : "cc" );
303 return reg1;
304}
305
306int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
307{
308 struct ap_queue_status status;
309
310 status = __ap_send(qid, psmid, msg, length);
311 switch (status.response_code) {
312 case AP_RESPONSE_NORMAL:
313 return 0;
314 case AP_RESPONSE_Q_FULL:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200315 case AP_RESPONSE_RESET_IN_PROGRESS:
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200316 return -EBUSY;
317 default: /* Device is gone. */
318 return -ENODEV;
319 }
320}
321EXPORT_SYMBOL(ap_send);
322
Felix Beck1749a812008-04-17 07:46:28 +0200323/**
324 * __ap_recv(): Receive message from adjunct processor queue.
325 * @qid: The AP queue number
326 * @psmid: Pointer to program supplied message identifier
327 * @msg: The message text
328 * @length: The message length
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200329 *
Felix Beck1749a812008-04-17 07:46:28 +0200330 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200331 * Condition code 1 on DQAP means the receive has taken place
332 * but only partially. The response is incomplete, hence the
333 * DQAP is repeated.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200334 * Condition code 2 on DQAP also means the receive is incomplete,
335 * this time because a segment boundary was reached. Again, the
336 * DQAP is repeated.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200337 * Note that gpr2 is used by the DQAP instruction to keep track of
338 * any 'residual' length, in case the instruction gets interrupted.
339 * Hence it gets zeroed before the instruction.
340 */
341static inline struct ap_queue_status
342__ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
343{
344 typedef struct { char _[length]; } msgblock;
345 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
346 register struct ap_queue_status reg1 asm ("1");
347 register unsigned long reg2 asm("2") = 0UL;
348 register unsigned long reg4 asm("4") = (unsigned long) msg;
349 register unsigned long reg5 asm("5") = (unsigned long) length;
350 register unsigned long reg6 asm("6") = 0UL;
351 register unsigned long reg7 asm("7") = 0UL;
352
353
354 asm volatile(
355 "0: .long 0xb2ae0064\n"
356 " brc 6,0b\n"
357 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
358 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
359 "=m" (*(msgblock *) msg) : : "cc" );
360 *psmid = (((unsigned long long) reg6) << 32) + reg7;
361 return reg1;
362}
363
364int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
365{
366 struct ap_queue_status status;
367
368 status = __ap_recv(qid, psmid, msg, length);
369 switch (status.response_code) {
370 case AP_RESPONSE_NORMAL:
371 return 0;
372 case AP_RESPONSE_NO_PENDING_REPLY:
373 if (status.queue_empty)
374 return -ENOENT;
375 return -EBUSY;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200376 case AP_RESPONSE_RESET_IN_PROGRESS:
377 return -EBUSY;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200378 default:
379 return -ENODEV;
380 }
381}
382EXPORT_SYMBOL(ap_recv);
383
384/**
Felix Beck1749a812008-04-17 07:46:28 +0200385 * ap_query_queue(): Check if an AP queue is available.
386 * @qid: The AP queue number
387 * @queue_depth: Pointer to queue depth value
388 * @device_type: Pointer to device type value
389 *
390 * The test is repeated for AP_MAX_RESET times.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200391 */
392static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
393{
394 struct ap_queue_status status;
395 int t_depth, t_device_type, rc, i;
396
397 rc = -EBUSY;
398 for (i = 0; i < AP_MAX_RESET; i++) {
399 status = ap_test_queue(qid, &t_depth, &t_device_type);
400 switch (status.response_code) {
401 case AP_RESPONSE_NORMAL:
402 *queue_depth = t_depth + 1;
403 *device_type = t_device_type;
404 rc = 0;
405 break;
406 case AP_RESPONSE_Q_NOT_AVAIL:
407 rc = -ENODEV;
408 break;
409 case AP_RESPONSE_RESET_IN_PROGRESS:
410 break;
411 case AP_RESPONSE_DECONFIGURED:
412 rc = -ENODEV;
413 break;
414 case AP_RESPONSE_CHECKSTOPPED:
415 rc = -ENODEV;
416 break;
Felix Beckcb17a632008-12-25 13:38:41 +0100417 case AP_RESPONSE_INVALID_ADDRESS:
418 rc = -ENODEV;
419 break;
420 case AP_RESPONSE_OTHERWISE_CHANGED:
421 break;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200422 case AP_RESPONSE_BUSY:
423 break;
424 default:
425 BUG();
426 }
427 if (rc != -EBUSY)
428 break;
429 if (i < AP_MAX_RESET - 1)
430 udelay(5);
431 }
432 return rc;
433}
434
435/**
Felix Beck1749a812008-04-17 07:46:28 +0200436 * ap_init_queue(): Reset an AP queue.
437 * @qid: The AP queue number
438 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200439 * Reset an AP queue and wait for it to become available again.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200440 */
441static int ap_init_queue(ap_qid_t qid)
442{
443 struct ap_queue_status status;
444 int rc, dummy, i;
445
446 rc = -ENODEV;
447 status = ap_reset_queue(qid);
448 for (i = 0; i < AP_MAX_RESET; i++) {
449 switch (status.response_code) {
450 case AP_RESPONSE_NORMAL:
451 if (status.queue_empty)
452 rc = 0;
453 break;
454 case AP_RESPONSE_Q_NOT_AVAIL:
455 case AP_RESPONSE_DECONFIGURED:
456 case AP_RESPONSE_CHECKSTOPPED:
457 i = AP_MAX_RESET; /* return with -ENODEV */
458 break;
459 case AP_RESPONSE_RESET_IN_PROGRESS:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200460 rc = -EBUSY;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200461 case AP_RESPONSE_BUSY:
462 default:
463 break;
464 }
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200465 if (rc != -ENODEV && rc != -EBUSY)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200466 break;
467 if (i < AP_MAX_RESET - 1) {
468 udelay(5);
469 status = ap_test_queue(qid, &dummy, &dummy);
470 }
471 }
Felix Beckcb17a632008-12-25 13:38:41 +0100472 if (rc == 0 && ap_using_interrupts()) {
473 rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
474 /* If interruption mode is supported by the machine,
475 * but an AP can not be enabled for interruption then
476 * the AP will be discarded. */
477 if (rc)
478 pr_err("Registering adapter interrupts for "
479 "AP %d failed\n", AP_QID_DEVICE(qid));
480 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200481 return rc;
482}
483
484/**
Felix Beck1749a812008-04-17 07:46:28 +0200485 * ap_increase_queue_count(): Arm request timeout.
486 * @ap_dev: Pointer to an AP device.
487 *
488 * Arm request timeout if an AP device was idle and a new request is submitted.
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200489 */
490static void ap_increase_queue_count(struct ap_device *ap_dev)
491{
492 int timeout = ap_dev->drv->request_timeout;
493
494 ap_dev->queue_count++;
495 if (ap_dev->queue_count == 1) {
496 mod_timer(&ap_dev->timeout, jiffies + timeout);
497 ap_dev->reset = AP_RESET_ARMED;
498 }
499}
500
501/**
Felix Beck1749a812008-04-17 07:46:28 +0200502 * ap_decrease_queue_count(): Decrease queue count.
503 * @ap_dev: Pointer to an AP device.
504 *
505 * If AP device is still alive, re-schedule request timeout if there are still
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200506 * pending requests.
507 */
508static void ap_decrease_queue_count(struct ap_device *ap_dev)
509{
510 int timeout = ap_dev->drv->request_timeout;
511
512 ap_dev->queue_count--;
513 if (ap_dev->queue_count > 0)
514 mod_timer(&ap_dev->timeout, jiffies + timeout);
515 else
Felix Beck1749a812008-04-17 07:46:28 +0200516 /*
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200517 * The timeout timer should to be disabled now - since
518 * del_timer_sync() is very expensive, we just tell via the
519 * reset flag to ignore the pending timeout timer.
520 */
521 ap_dev->reset = AP_RESET_IGNORE;
522}
523
Felix Beck1749a812008-04-17 07:46:28 +0200524/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200525 * AP device related attributes.
526 */
527static ssize_t ap_hwtype_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
529{
530 struct ap_device *ap_dev = to_ap_dev(dev);
531 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
532}
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200533
Christian Maaser43c207e62008-12-25 13:38:42 +0100534static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200535static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
536 char *buf)
537{
538 struct ap_device *ap_dev = to_ap_dev(dev);
539 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
540}
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200541
Christian Maaser43c207e62008-12-25 13:38:42 +0100542static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200543static ssize_t ap_request_count_show(struct device *dev,
544 struct device_attribute *attr,
545 char *buf)
546{
547 struct ap_device *ap_dev = to_ap_dev(dev);
548 int rc;
549
550 spin_lock_bh(&ap_dev->lock);
551 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
552 spin_unlock_bh(&ap_dev->lock);
553 return rc;
554}
555
556static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
557
558static ssize_t ap_modalias_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
561 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
562}
563
564static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
565
566static struct attribute *ap_dev_attrs[] = {
567 &dev_attr_hwtype.attr,
568 &dev_attr_depth.attr,
569 &dev_attr_request_count.attr,
570 &dev_attr_modalias.attr,
571 NULL
572};
573static struct attribute_group ap_dev_attr_group = {
574 .attrs = ap_dev_attrs
575};
576
577/**
Felix Beck1749a812008-04-17 07:46:28 +0200578 * ap_bus_match()
579 * @dev: Pointer to device
580 * @drv: Pointer to device_driver
581 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200582 * AP bus driver registration/unregistration.
583 */
584static int ap_bus_match(struct device *dev, struct device_driver *drv)
585{
586 struct ap_device *ap_dev = to_ap_dev(dev);
587 struct ap_driver *ap_drv = to_ap_drv(drv);
588 struct ap_device_id *id;
589
Felix Beck1749a812008-04-17 07:46:28 +0200590 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200591 * Compare device type of the device with the list of
592 * supported types of the device_driver.
593 */
594 for (id = ap_drv->ids; id->match_flags; id++) {
595 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
596 (id->dev_type != ap_dev->device_type))
597 continue;
598 return 1;
599 }
600 return 0;
601}
602
603/**
Felix Beck1749a812008-04-17 07:46:28 +0200604 * ap_uevent(): Uevent function for AP devices.
605 * @dev: Pointer to device
606 * @env: Pointer to kobj_uevent_env
607 *
608 * It sets up a single environment variable DEV_TYPE which contains the
609 * hardware device type.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200610 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200611static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200612{
613 struct ap_device *ap_dev = to_ap_dev(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200614 int retval = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200615
616 if (!ap_dev)
617 return -ENODEV;
618
619 /* Set up DEV_TYPE environment variable. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200620 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700621 if (retval)
622 return retval;
623
Cornelia Huck66a4263b2006-12-04 15:40:10 +0100624 /* Add MODALIAS= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200625 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700626
Eric Rannaudbf624562007-03-30 22:23:12 -0700627 return retval;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200628}
629
Felix Beck772f5472009-06-22 12:08:16 +0200630static int ap_bus_suspend(struct device *dev, pm_message_t state)
631{
632 struct ap_device *ap_dev = to_ap_dev(dev);
633 unsigned long flags;
634
635 if (!ap_suspend_flag) {
636 ap_suspend_flag = 1;
637
638 /* Disable scanning for devices, thus we do not want to scan
639 * for them after removing.
640 */
641 del_timer_sync(&ap_config_timer);
642 if (ap_work_queue != NULL) {
643 destroy_workqueue(ap_work_queue);
644 ap_work_queue = NULL;
645 }
646 tasklet_disable(&ap_tasklet);
647 }
648 /* Poll on the device until all requests are finished. */
649 do {
650 flags = 0;
Felix Beck95f15562009-09-11 10:28:51 +0200651 spin_lock_bh(&ap_dev->lock);
Felix Beck772f5472009-06-22 12:08:16 +0200652 __ap_poll_device(ap_dev, &flags);
Felix Beck95f15562009-09-11 10:28:51 +0200653 spin_unlock_bh(&ap_dev->lock);
Felix Beck772f5472009-06-22 12:08:16 +0200654 } while ((flags & 1) || (flags & 2));
655
656 ap_device_remove(dev);
657 return 0;
658}
659
660static int ap_bus_resume(struct device *dev)
661{
662 int rc = 0;
663 struct ap_device *ap_dev = to_ap_dev(dev);
664
665 if (ap_suspend_flag) {
666 ap_suspend_flag = 0;
667 if (!ap_interrupts_available())
668 ap_interrupt_indicator = NULL;
669 ap_device_probe(dev);
670 ap_reset(ap_dev);
671 setup_timer(&ap_dev->timeout, ap_request_timeout,
672 (unsigned long) ap_dev);
673 ap_scan_bus(NULL);
674 init_timer(&ap_config_timer);
675 ap_config_timer.function = ap_config_timeout;
676 ap_config_timer.data = 0;
677 ap_config_timer.expires = jiffies + ap_config_time * HZ;
678 add_timer(&ap_config_timer);
679 ap_work_queue = create_singlethread_workqueue("kapwork");
680 if (!ap_work_queue)
681 return -ENOMEM;
682 tasklet_enable(&ap_tasklet);
683 if (!ap_using_interrupts())
684 ap_schedule_poll_timer();
685 else
686 tasklet_schedule(&ap_tasklet);
687 if (ap_thread_flag)
688 rc = ap_poll_thread_start();
689 } else {
690 ap_device_probe(dev);
691 ap_reset(ap_dev);
692 setup_timer(&ap_dev->timeout, ap_request_timeout,
693 (unsigned long) ap_dev);
694 }
695
696 return rc;
697}
698
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200699static struct bus_type ap_bus_type = {
700 .name = "ap",
701 .match = &ap_bus_match,
702 .uevent = &ap_uevent,
Felix Beck772f5472009-06-22 12:08:16 +0200703 .suspend = ap_bus_suspend,
704 .resume = ap_bus_resume
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200705};
706
707static int ap_device_probe(struct device *dev)
708{
709 struct ap_device *ap_dev = to_ap_dev(dev);
710 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
711 int rc;
712
713 ap_dev->drv = ap_drv;
714 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
Ralph Wuerthnerfaa582c2008-03-05 12:37:13 +0100715 if (!rc) {
Christian Maaser43c207e62008-12-25 13:38:42 +0100716 spin_lock_bh(&ap_device_list_lock);
Ralph Wuerthnerfaa582c2008-03-05 12:37:13 +0100717 list_add(&ap_dev->list, &ap_device_list);
Christian Maaser43c207e62008-12-25 13:38:42 +0100718 spin_unlock_bh(&ap_device_list_lock);
Ralph Wuerthnerfaa582c2008-03-05 12:37:13 +0100719 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200720 return rc;
721}
722
723/**
Felix Beck1749a812008-04-17 07:46:28 +0200724 * __ap_flush_queue(): Flush requests.
725 * @ap_dev: Pointer to the AP device
726 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200727 * Flush all requests from the request/pending queue of an AP device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200728 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100729static void __ap_flush_queue(struct ap_device *ap_dev)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200730{
731 struct ap_message *ap_msg, *next;
732
733 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
734 list_del_init(&ap_msg->list);
735 ap_dev->pendingq_count--;
736 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
737 }
738 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
739 list_del_init(&ap_msg->list);
740 ap_dev->requestq_count--;
741 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
742 }
743}
744
745void ap_flush_queue(struct ap_device *ap_dev)
746{
747 spin_lock_bh(&ap_dev->lock);
748 __ap_flush_queue(ap_dev);
749 spin_unlock_bh(&ap_dev->lock);
750}
751EXPORT_SYMBOL(ap_flush_queue);
752
753static int ap_device_remove(struct device *dev)
754{
755 struct ap_device *ap_dev = to_ap_dev(dev);
756 struct ap_driver *ap_drv = ap_dev->drv;
757
Ralph Wuerthner4e562962006-10-04 20:02:05 +0200758 ap_flush_queue(ap_dev);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200759 del_timer_sync(&ap_dev->timeout);
Christian Maaser43c207e62008-12-25 13:38:42 +0100760 spin_lock_bh(&ap_device_list_lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +0100761 list_del_init(&ap_dev->list);
Christian Maaser43c207e62008-12-25 13:38:42 +0100762 spin_unlock_bh(&ap_device_list_lock);
Ralph Wuerthnerfaa582c2008-03-05 12:37:13 +0100763 if (ap_drv->remove)
764 ap_drv->remove(ap_dev);
Ralph Wuerthnere675c0d2007-03-26 20:42:43 +0200765 spin_lock_bh(&ap_dev->lock);
766 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
767 spin_unlock_bh(&ap_dev->lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200768 return 0;
769}
770
771int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
772 char *name)
773{
774 struct device_driver *drv = &ap_drv->driver;
775
776 drv->bus = &ap_bus_type;
777 drv->probe = ap_device_probe;
778 drv->remove = ap_device_remove;
779 drv->owner = owner;
780 drv->name = name;
781 return driver_register(drv);
782}
783EXPORT_SYMBOL(ap_driver_register);
784
785void ap_driver_unregister(struct ap_driver *ap_drv)
786{
787 driver_unregister(&ap_drv->driver);
788}
789EXPORT_SYMBOL(ap_driver_unregister);
790
Felix Beck1749a812008-04-17 07:46:28 +0200791/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200792 * AP bus attributes.
793 */
794static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
795{
796 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
797}
798
799static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
800
801static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
802{
803 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
804}
805
Felix Beckcb17a632008-12-25 13:38:41 +0100806static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
807{
808 return snprintf(buf, PAGE_SIZE, "%d\n",
809 ap_using_interrupts() ? 1 : 0);
810}
811
812static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
813
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200814static ssize_t ap_config_time_store(struct bus_type *bus,
815 const char *buf, size_t count)
816{
817 int time;
818
819 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
820 return -EINVAL;
821 ap_config_time = time;
822 if (!timer_pending(&ap_config_timer) ||
823 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
824 ap_config_timer.expires = jiffies + ap_config_time * HZ;
825 add_timer(&ap_config_timer);
826 }
827 return count;
828}
829
830static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
831
832static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
833{
834 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
835}
836
837static ssize_t ap_poll_thread_store(struct bus_type *bus,
838 const char *buf, size_t count)
839{
840 int flag, rc;
841
842 if (sscanf(buf, "%d\n", &flag) != 1)
843 return -EINVAL;
844 if (flag) {
845 rc = ap_poll_thread_start();
846 if (rc)
847 return rc;
848 }
849 else
850 ap_poll_thread_stop();
851 return count;
852}
853
854static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
855
Felix Beckfe137232008-07-14 09:59:08 +0200856static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
857{
858 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
859}
860
861static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
862 size_t count)
863{
864 unsigned long long time;
865 ktime_t hr_time;
866
867 /* 120 seconds = maximum poll interval */
Felix Beckcb17a632008-12-25 13:38:41 +0100868 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
869 time > 120000000000ULL)
Felix Beckfe137232008-07-14 09:59:08 +0200870 return -EINVAL;
871 poll_timeout = time;
872 hr_time = ktime_set(0, poll_timeout);
873
874 if (!hrtimer_is_queued(&ap_poll_timer) ||
Arjan van de Ven6c644ea2008-09-01 15:20:30 -0700875 !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
876 hrtimer_set_expires(&ap_poll_timer, hr_time);
877 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
Felix Beckfe137232008-07-14 09:59:08 +0200878 }
879 return count;
880}
881
882static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
883
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200884static struct bus_attribute *const ap_bus_attrs[] = {
885 &bus_attr_ap_domain,
886 &bus_attr_config_time,
887 &bus_attr_poll_thread,
Felix Beckcb17a632008-12-25 13:38:41 +0100888 &bus_attr_ap_interrupts,
Felix Beckfe137232008-07-14 09:59:08 +0200889 &bus_attr_poll_timeout,
890 NULL,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200891};
892
893/**
Felix Beck1749a812008-04-17 07:46:28 +0200894 * ap_select_domain(): Select an AP domain.
895 *
896 * Pick one of the 16 AP domains.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200897 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100898static int ap_select_domain(void)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200899{
900 int queue_depth, device_type, count, max_count, best_domain;
901 int rc, i, j;
902
Felix Beck1749a812008-04-17 07:46:28 +0200903 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200904 * We want to use a single domain. Either the one specified with
905 * the "domain=" parameter or the domain with the maximum number
906 * of devices.
907 */
908 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
909 /* Domain has already been selected. */
910 return 0;
911 best_domain = -1;
912 max_count = 0;
913 for (i = 0; i < AP_DOMAINS; i++) {
914 count = 0;
915 for (j = 0; j < AP_DEVICES; j++) {
916 ap_qid_t qid = AP_MKQID(j, i);
917 rc = ap_query_queue(qid, &queue_depth, &device_type);
918 if (rc)
919 continue;
920 count++;
921 }
922 if (count > max_count) {
923 max_count = count;
924 best_domain = i;
925 }
926 }
927 if (best_domain >= 0){
928 ap_domain_index = best_domain;
929 return 0;
930 }
931 return -ENODEV;
932}
933
934/**
Felix Beck1749a812008-04-17 07:46:28 +0200935 * ap_probe_device_type(): Find the device type of an AP.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200936 * @ap_dev: pointer to the AP device.
Felix Beck1749a812008-04-17 07:46:28 +0200937 *
938 * Find the device type if query queue returned a device type of 0.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200939 */
940static int ap_probe_device_type(struct ap_device *ap_dev)
941{
942 static unsigned char msg[] = {
943 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
944 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
945 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
946 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
947 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
948 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
949 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
950 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
951 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
952 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
953 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
954 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
955 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
956 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
957 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
958 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
959 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
960 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
961 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
962 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
963 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
964 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
965 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
966 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
967 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
968 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
969 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
970 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
971 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
972 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
973 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
974 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
975 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
976 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
977 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
978 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
979 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
980 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
981 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
982 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
983 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
984 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
985 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
986 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
987 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
988 };
989 struct ap_queue_status status;
990 unsigned long long psmid;
991 char *reply;
992 int rc, i;
993
994 reply = (void *) get_zeroed_page(GFP_KERNEL);
995 if (!reply) {
996 rc = -ENOMEM;
997 goto out;
998 }
999
1000 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1001 msg, sizeof(msg));
1002 if (status.response_code != AP_RESPONSE_NORMAL) {
1003 rc = -ENODEV;
1004 goto out_free;
1005 }
1006
1007 /* Wait for the test message to complete. */
1008 for (i = 0; i < 6; i++) {
1009 mdelay(300);
1010 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1011 if (status.response_code == AP_RESPONSE_NORMAL &&
1012 psmid == 0x0102030405060708ULL)
1013 break;
1014 }
1015 if (i < 6) {
1016 /* Got an answer. */
1017 if (reply[0] == 0x00 && reply[1] == 0x86)
1018 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1019 else
1020 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1021 rc = 0;
1022 } else
1023 rc = -ENODEV;
1024
1025out_free:
1026 free_page((unsigned long) reply);
1027out:
1028 return rc;
1029}
1030
Felix Beckcb17a632008-12-25 13:38:41 +01001031static void ap_interrupt_handler(void *unused1, void *unused2)
1032{
1033 tasklet_schedule(&ap_tasklet);
1034}
1035
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001036/**
Felix Beck1749a812008-04-17 07:46:28 +02001037 * __ap_scan_bus(): Scan the AP bus.
1038 * @dev: Pointer to device
1039 * @data: Pointer to data
1040 *
1041 * Scan the AP bus for new devices.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001042 */
1043static int __ap_scan_bus(struct device *dev, void *data)
1044{
1045 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1046}
1047
1048static void ap_device_release(struct device *dev)
1049{
1050 struct ap_device *ap_dev = to_ap_dev(dev);
1051
1052 kfree(ap_dev);
1053}
1054
Al Viro4927b3f2006-12-06 19:18:20 +00001055static void ap_scan_bus(struct work_struct *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001056{
1057 struct ap_device *ap_dev;
1058 struct device *dev;
1059 ap_qid_t qid;
1060 int queue_depth, device_type;
1061 int rc, i;
1062
1063 if (ap_select_domain() != 0)
1064 return;
1065 for (i = 0; i < AP_DEVICES; i++) {
1066 qid = AP_MKQID(i, ap_domain_index);
1067 dev = bus_find_device(&ap_bus_type, NULL,
1068 (void *)(unsigned long)qid,
1069 __ap_scan_bus);
Ralph Wuerthnerf3b017d2006-10-27 12:39:26 +02001070 rc = ap_query_queue(qid, &queue_depth, &device_type);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001071 if (dev) {
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001072 if (rc == -EBUSY) {
1073 set_current_state(TASK_UNINTERRUPTIBLE);
1074 schedule_timeout(AP_RESET_TIMEOUT);
1075 rc = ap_query_queue(qid, &queue_depth,
1076 &device_type);
1077 }
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001078 ap_dev = to_ap_dev(dev);
1079 spin_lock_bh(&ap_dev->lock);
1080 if (rc || ap_dev->unregistered) {
1081 spin_unlock_bh(&ap_dev->lock);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001082 device_unregister(dev);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001083 put_device(dev);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001084 continue;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001085 }
1086 spin_unlock_bh(&ap_dev->lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001087 put_device(dev);
1088 continue;
1089 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001090 if (rc)
1091 continue;
1092 rc = ap_init_queue(qid);
1093 if (rc)
1094 continue;
1095 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1096 if (!ap_dev)
1097 break;
1098 ap_dev->qid = qid;
1099 ap_dev->queue_depth = queue_depth;
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001100 ap_dev->unregistered = 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001101 spin_lock_init(&ap_dev->lock);
1102 INIT_LIST_HEAD(&ap_dev->pendingq);
1103 INIT_LIST_HEAD(&ap_dev->requestq);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001104 INIT_LIST_HEAD(&ap_dev->list);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001105 setup_timer(&ap_dev->timeout, ap_request_timeout,
1106 (unsigned long) ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001107 if (device_type == 0)
1108 ap_probe_device_type(ap_dev);
1109 else
1110 ap_dev->device_type = device_type;
1111
1112 ap_dev->device.bus = &ap_bus_type;
1113 ap_dev->device.parent = ap_root_device;
Felix Beckedc44fa2009-09-11 10:28:52 +02001114 if (dev_set_name(&ap_dev->device, "card%02x",
1115 AP_QID_DEVICE(ap_dev->qid))) {
1116 kfree(ap_dev);
1117 continue;
1118 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001119 ap_dev->device.release = ap_device_release;
1120 rc = device_register(&ap_dev->device);
1121 if (rc) {
Sebastian Ottc6304932009-09-11 10:28:38 +02001122 put_device(&ap_dev->device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001123 continue;
1124 }
1125 /* Add device attributes. */
1126 rc = sysfs_create_group(&ap_dev->device.kobj,
1127 &ap_dev_attr_group);
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001128 if (!rc) {
1129 spin_lock_bh(&ap_dev->lock);
1130 ap_dev->unregistered = 0;
1131 spin_unlock_bh(&ap_dev->lock);
1132 }
1133 else
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001134 device_unregister(&ap_dev->device);
1135 }
1136}
1137
1138static void
1139ap_config_timeout(unsigned long ptr)
1140{
1141 queue_work(ap_work_queue, &ap_config_work);
1142 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1143 add_timer(&ap_config_timer);
1144}
1145
1146/**
Felix Beck1749a812008-04-17 07:46:28 +02001147 * ap_schedule_poll_timer(): Schedule poll timer.
1148 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001149 * Set up the timer to run the poll tasklet
1150 */
1151static inline void ap_schedule_poll_timer(void)
1152{
Felix Beck8d406c62009-07-24 12:39:53 +02001153 ktime_t hr_time;
Felix Beck772f5472009-06-22 12:08:16 +02001154 if (ap_using_interrupts() || ap_suspend_flag)
Felix Beckcb17a632008-12-25 13:38:41 +01001155 return;
Felix Beckfe137232008-07-14 09:59:08 +02001156 if (hrtimer_is_queued(&ap_poll_timer))
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001157 return;
Felix Beck8d406c62009-07-24 12:39:53 +02001158 if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1159 hr_time = ktime_set(0, poll_timeout);
1160 hrtimer_forward_now(&ap_poll_timer, hr_time);
1161 hrtimer_restart(&ap_poll_timer);
1162 }
1163 return;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001164}
1165
1166/**
Felix Beck1749a812008-04-17 07:46:28 +02001167 * ap_poll_read(): Receive pending reply messages from an AP device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001168 * @ap_dev: pointer to the AP device
1169 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1170 * required, bit 2^1 is set if the poll timer needs to get armed
Felix Beck1749a812008-04-17 07:46:28 +02001171 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001172 * Returns 0 if the device is still present, -ENODEV if not.
1173 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +01001174static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001175{
1176 struct ap_queue_status status;
1177 struct ap_message *ap_msg;
1178
1179 if (ap_dev->queue_count <= 0)
1180 return 0;
1181 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1182 ap_dev->reply->message, ap_dev->reply->length);
1183 switch (status.response_code) {
1184 case AP_RESPONSE_NORMAL:
1185 atomic_dec(&ap_poll_requests);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001186 ap_decrease_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001187 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1188 if (ap_msg->psmid != ap_dev->reply->psmid)
1189 continue;
1190 list_del_init(&ap_msg->list);
1191 ap_dev->pendingq_count--;
1192 ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
1193 break;
1194 }
1195 if (ap_dev->queue_count > 0)
1196 *flags |= 1;
1197 break;
1198 case AP_RESPONSE_NO_PENDING_REPLY:
1199 if (status.queue_empty) {
1200 /* The card shouldn't forget requests but who knows. */
Ralph Wuerthnere675c0d2007-03-26 20:42:43 +02001201 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001202 ap_dev->queue_count = 0;
1203 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1204 ap_dev->requestq_count += ap_dev->pendingq_count;
1205 ap_dev->pendingq_count = 0;
1206 } else
1207 *flags |= 2;
1208 break;
1209 default:
1210 return -ENODEV;
1211 }
1212 return 0;
1213}
1214
1215/**
Felix Beck1749a812008-04-17 07:46:28 +02001216 * ap_poll_write(): Send messages from the request queue to an AP device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001217 * @ap_dev: pointer to the AP device
1218 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1219 * required, bit 2^1 is set if the poll timer needs to get armed
Felix Beck1749a812008-04-17 07:46:28 +02001220 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001221 * Returns 0 if the device is still present, -ENODEV if not.
1222 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +01001223static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001224{
1225 struct ap_queue_status status;
1226 struct ap_message *ap_msg;
1227
1228 if (ap_dev->requestq_count <= 0 ||
1229 ap_dev->queue_count >= ap_dev->queue_depth)
1230 return 0;
1231 /* Start the next request on the queue. */
1232 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1233 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1234 ap_msg->message, ap_msg->length);
1235 switch (status.response_code) {
1236 case AP_RESPONSE_NORMAL:
1237 atomic_inc(&ap_poll_requests);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001238 ap_increase_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001239 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1240 ap_dev->requestq_count--;
1241 ap_dev->pendingq_count++;
1242 if (ap_dev->queue_count < ap_dev->queue_depth &&
1243 ap_dev->requestq_count > 0)
1244 *flags |= 1;
1245 *flags |= 2;
1246 break;
1247 case AP_RESPONSE_Q_FULL:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001248 case AP_RESPONSE_RESET_IN_PROGRESS:
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001249 *flags |= 2;
1250 break;
1251 case AP_RESPONSE_MESSAGE_TOO_BIG:
1252 return -EINVAL;
1253 default:
1254 return -ENODEV;
1255 }
1256 return 0;
1257}
1258
1259/**
Felix Beck1749a812008-04-17 07:46:28 +02001260 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001261 * @ap_dev: pointer to the bus device
1262 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1263 * required, bit 2^1 is set if the poll timer needs to get armed
Felix Beck1749a812008-04-17 07:46:28 +02001264 *
1265 * Poll AP device for pending replies and send new messages. If either
1266 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001267 * Returns 0.
1268 */
1269static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1270{
1271 int rc;
1272
1273 rc = ap_poll_read(ap_dev, flags);
1274 if (rc)
1275 return rc;
1276 return ap_poll_write(ap_dev, flags);
1277}
1278
1279/**
Felix Beck1749a812008-04-17 07:46:28 +02001280 * __ap_queue_message(): Queue a message to a device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001281 * @ap_dev: pointer to the AP device
1282 * @ap_msg: the message to be queued
Felix Beck1749a812008-04-17 07:46:28 +02001283 *
1284 * Queue a message to a device. Returns 0 if successful.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001285 */
1286static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1287{
1288 struct ap_queue_status status;
1289
1290 if (list_empty(&ap_dev->requestq) &&
1291 ap_dev->queue_count < ap_dev->queue_depth) {
1292 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1293 ap_msg->message, ap_msg->length);
1294 switch (status.response_code) {
1295 case AP_RESPONSE_NORMAL:
1296 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1297 atomic_inc(&ap_poll_requests);
1298 ap_dev->pendingq_count++;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001299 ap_increase_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001300 ap_dev->total_request_count++;
1301 break;
1302 case AP_RESPONSE_Q_FULL:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001303 case AP_RESPONSE_RESET_IN_PROGRESS:
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001304 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1305 ap_dev->requestq_count++;
1306 ap_dev->total_request_count++;
1307 return -EBUSY;
1308 case AP_RESPONSE_MESSAGE_TOO_BIG:
1309 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1310 return -EINVAL;
1311 default: /* Device is gone. */
1312 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1313 return -ENODEV;
1314 }
1315 } else {
1316 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1317 ap_dev->requestq_count++;
1318 ap_dev->total_request_count++;
1319 return -EBUSY;
1320 }
1321 ap_schedule_poll_timer();
1322 return 0;
1323}
1324
1325void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1326{
1327 unsigned long flags;
1328 int rc;
1329
1330 spin_lock_bh(&ap_dev->lock);
1331 if (!ap_dev->unregistered) {
1332 /* Make room on the queue by polling for finished requests. */
1333 rc = ap_poll_queue(ap_dev, &flags);
1334 if (!rc)
1335 rc = __ap_queue_message(ap_dev, ap_msg);
1336 if (!rc)
1337 wake_up(&ap_poll_wait);
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001338 if (rc == -ENODEV)
1339 ap_dev->unregistered = 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001340 } else {
1341 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001342 rc = -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001343 }
1344 spin_unlock_bh(&ap_dev->lock);
1345 if (rc == -ENODEV)
1346 device_unregister(&ap_dev->device);
1347}
1348EXPORT_SYMBOL(ap_queue_message);
1349
1350/**
Felix Beck1749a812008-04-17 07:46:28 +02001351 * ap_cancel_message(): Cancel a crypto request.
1352 * @ap_dev: The AP device that has the message queued
1353 * @ap_msg: The message that is to be removed
1354 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001355 * Cancel a crypto request. This is done by removing the request
Felix Beck1749a812008-04-17 07:46:28 +02001356 * from the device pending or request queue. Note that the
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001357 * request stays on the AP queue. When it finishes the message
1358 * reply will be discarded because the psmid can't be found.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001359 */
1360void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1361{
1362 struct ap_message *tmp;
1363
1364 spin_lock_bh(&ap_dev->lock);
1365 if (!list_empty(&ap_msg->list)) {
1366 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1367 if (tmp->psmid == ap_msg->psmid) {
1368 ap_dev->pendingq_count--;
1369 goto found;
1370 }
1371 ap_dev->requestq_count--;
1372 found:
1373 list_del_init(&ap_msg->list);
1374 }
1375 spin_unlock_bh(&ap_dev->lock);
1376}
1377EXPORT_SYMBOL(ap_cancel_message);
1378
1379/**
Felix Beck1749a812008-04-17 07:46:28 +02001380 * ap_poll_timeout(): AP receive polling for finished AP requests.
Felix Beckfe137232008-07-14 09:59:08 +02001381 * @unused: Unused pointer.
Felix Beck1749a812008-04-17 07:46:28 +02001382 *
Felix Beckfe137232008-07-14 09:59:08 +02001383 * Schedules the AP tasklet using a high resolution timer.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001384 */
Felix Beckfe137232008-07-14 09:59:08 +02001385static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001386{
1387 tasklet_schedule(&ap_tasklet);
Felix Beckfe137232008-07-14 09:59:08 +02001388 return HRTIMER_NORESTART;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001389}
1390
1391/**
Felix Beck1749a812008-04-17 07:46:28 +02001392 * ap_reset(): Reset a not responding AP device.
1393 * @ap_dev: Pointer to the AP device
1394 *
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001395 * Reset a not responding AP device and move all requests from the
1396 * pending queue to the request queue.
1397 */
1398static void ap_reset(struct ap_device *ap_dev)
1399{
1400 int rc;
1401
1402 ap_dev->reset = AP_RESET_IGNORE;
1403 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1404 ap_dev->queue_count = 0;
1405 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1406 ap_dev->requestq_count += ap_dev->pendingq_count;
1407 ap_dev->pendingq_count = 0;
1408 rc = ap_init_queue(ap_dev->qid);
1409 if (rc == -ENODEV)
1410 ap_dev->unregistered = 1;
1411}
1412
Christian Maaser43c207e62008-12-25 13:38:42 +01001413static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001414{
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001415 if (!ap_dev->unregistered) {
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001416 if (ap_poll_queue(ap_dev, flags))
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001417 ap_dev->unregistered = 1;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001418 if (ap_dev->reset == AP_RESET_DO)
1419 ap_reset(ap_dev);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001420 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001421 return 0;
1422}
1423
Felix Beck1749a812008-04-17 07:46:28 +02001424/**
1425 * ap_poll_all(): Poll all AP devices.
1426 * @dummy: Unused variable
1427 *
1428 * Poll all AP devices on the bus in a round robin fashion. Continue
1429 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1430 * of the control flags has been set arm the poll timer.
1431 */
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001432static void ap_poll_all(unsigned long dummy)
1433{
1434 unsigned long flags;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001435 struct ap_device *ap_dev;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001436
Felix Beckcb17a632008-12-25 13:38:41 +01001437 /* Reset the indicator if interrupts are used. Thus new interrupts can
1438 * be received. Doing it in the beginning of the tasklet is therefor
1439 * important that no requests on any AP get lost.
1440 */
1441 if (ap_using_interrupts())
1442 xchg((u8 *)ap_interrupt_indicator, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001443 do {
1444 flags = 0;
Christian Maaser43c207e62008-12-25 13:38:42 +01001445 spin_lock(&ap_device_list_lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001446 list_for_each_entry(ap_dev, &ap_device_list, list) {
Felix Beck95f15562009-09-11 10:28:51 +02001447 spin_lock(&ap_dev->lock);
Christian Maaser43c207e62008-12-25 13:38:42 +01001448 __ap_poll_device(ap_dev, &flags);
Felix Beck95f15562009-09-11 10:28:51 +02001449 spin_unlock(&ap_dev->lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001450 }
Christian Maaser43c207e62008-12-25 13:38:42 +01001451 spin_unlock(&ap_device_list_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001452 } while (flags & 1);
1453 if (flags & 2)
1454 ap_schedule_poll_timer();
1455}
1456
1457/**
Felix Beck1749a812008-04-17 07:46:28 +02001458 * ap_poll_thread(): Thread that polls for finished requests.
1459 * @data: Unused pointer
1460 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001461 * AP bus poll thread. The purpose of this thread is to poll for
1462 * finished requests in a loop if there is a "free" cpu - that is
1463 * a cpu that doesn't have anything better to do. The polling stops
1464 * as soon as there is another task or if all messages have been
1465 * delivered.
1466 */
1467static int ap_poll_thread(void *data)
1468{
1469 DECLARE_WAITQUEUE(wait, current);
1470 unsigned long flags;
1471 int requests;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001472 struct ap_device *ap_dev;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001473
Christian Borntraegerd83682b2006-10-06 16:38:22 +02001474 set_user_nice(current, 19);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001475 while (1) {
Felix Beck772f5472009-06-22 12:08:16 +02001476 if (ap_suspend_flag)
1477 return 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001478 if (need_resched()) {
1479 schedule();
1480 continue;
1481 }
1482 add_wait_queue(&ap_poll_wait, &wait);
1483 set_current_state(TASK_INTERRUPTIBLE);
1484 if (kthread_should_stop())
1485 break;
1486 requests = atomic_read(&ap_poll_requests);
1487 if (requests <= 0)
1488 schedule();
1489 set_current_state(TASK_RUNNING);
1490 remove_wait_queue(&ap_poll_wait, &wait);
1491
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001492 flags = 0;
Christian Maaser43c207e62008-12-25 13:38:42 +01001493 spin_lock_bh(&ap_device_list_lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001494 list_for_each_entry(ap_dev, &ap_device_list, list) {
Felix Beck95f15562009-09-11 10:28:51 +02001495 spin_lock(&ap_dev->lock);
Christian Maaser43c207e62008-12-25 13:38:42 +01001496 __ap_poll_device(ap_dev, &flags);
Felix Beck95f15562009-09-11 10:28:51 +02001497 spin_unlock(&ap_dev->lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001498 }
Christian Maaser43c207e62008-12-25 13:38:42 +01001499 spin_unlock_bh(&ap_device_list_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001500 }
1501 set_current_state(TASK_RUNNING);
1502 remove_wait_queue(&ap_poll_wait, &wait);
1503 return 0;
1504}
1505
1506static int ap_poll_thread_start(void)
1507{
1508 int rc;
1509
Felix Beck772f5472009-06-22 12:08:16 +02001510 if (ap_using_interrupts() || ap_suspend_flag)
Felix Beckcb17a632008-12-25 13:38:41 +01001511 return 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001512 mutex_lock(&ap_poll_thread_mutex);
1513 if (!ap_poll_kthread) {
1514 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1515 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1516 if (rc)
1517 ap_poll_kthread = NULL;
1518 }
1519 else
1520 rc = 0;
1521 mutex_unlock(&ap_poll_thread_mutex);
1522 return rc;
1523}
1524
1525static void ap_poll_thread_stop(void)
1526{
1527 mutex_lock(&ap_poll_thread_mutex);
1528 if (ap_poll_kthread) {
1529 kthread_stop(ap_poll_kthread);
1530 ap_poll_kthread = NULL;
1531 }
1532 mutex_unlock(&ap_poll_thread_mutex);
1533}
1534
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001535/**
Felix Beck1749a812008-04-17 07:46:28 +02001536 * ap_request_timeout(): Handling of request timeouts
1537 * @data: Holds the AP device.
1538 *
1539 * Handles request timeouts.
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001540 */
1541static void ap_request_timeout(unsigned long data)
1542{
1543 struct ap_device *ap_dev = (struct ap_device *) data;
1544
Felix Beckcb17a632008-12-25 13:38:41 +01001545 if (ap_dev->reset == AP_RESET_ARMED) {
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001546 ap_dev->reset = AP_RESET_DO;
Felix Beckcb17a632008-12-25 13:38:41 +01001547
1548 if (ap_using_interrupts())
1549 tasklet_schedule(&ap_tasklet);
1550 }
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001551}
1552
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001553static void ap_reset_domain(void)
1554{
1555 int i;
1556
Ralph Wuerthner39aa7cf2007-10-12 16:11:29 +02001557 if (ap_domain_index != -1)
1558 for (i = 0; i < AP_DEVICES; i++)
1559 ap_reset_queue(AP_MKQID(i, ap_domain_index));
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001560}
1561
1562static void ap_reset_all(void)
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001563{
1564 int i, j;
1565
1566 for (i = 0; i < AP_DOMAINS; i++)
1567 for (j = 0; j < AP_DEVICES; j++)
1568 ap_reset_queue(AP_MKQID(j, i));
1569}
1570
1571static struct reset_call ap_reset_call = {
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001572 .fn = ap_reset_all,
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001573};
1574
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001575/**
Felix Beck1749a812008-04-17 07:46:28 +02001576 * ap_module_init(): The module initialization code.
1577 *
1578 * Initializes the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001579 */
1580int __init ap_module_init(void)
1581{
1582 int rc, i;
1583
1584 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
Martin Schwidefsky136f7a12008-12-25 13:39:46 +01001585 pr_warning("%d is not a valid cryptographic domain\n",
1586 ap_domain_index);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001587 return -EINVAL;
1588 }
1589 if (ap_instructions_available() != 0) {
Martin Schwidefsky136f7a12008-12-25 13:39:46 +01001590 pr_warning("The hardware system does not support "
1591 "AP instructions\n");
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001592 return -ENODEV;
1593 }
Felix Beckcb17a632008-12-25 13:38:41 +01001594 if (ap_interrupts_available()) {
1595 isc_register(AP_ISC);
1596 ap_interrupt_indicator = s390_register_adapter_interrupt(
1597 &ap_interrupt_handler, NULL, AP_ISC);
1598 if (IS_ERR(ap_interrupt_indicator)) {
1599 ap_interrupt_indicator = NULL;
1600 isc_unregister(AP_ISC);
1601 }
1602 }
1603
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001604 register_reset_call(&ap_reset_call);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001605
1606 /* Create /sys/bus/ap. */
1607 rc = bus_register(&ap_bus_type);
1608 if (rc)
1609 goto out;
1610 for (i = 0; ap_bus_attrs[i]; i++) {
1611 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1612 if (rc)
1613 goto out_bus;
1614 }
1615
1616 /* Create /sys/devices/ap. */
Mark McLoughlin035da162008-12-15 12:58:29 +00001617 ap_root_device = root_device_register("ap");
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001618 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1619 if (rc)
1620 goto out_bus;
1621
1622 ap_work_queue = create_singlethread_workqueue("kapwork");
1623 if (!ap_work_queue) {
1624 rc = -ENOMEM;
1625 goto out_root;
1626 }
1627
1628 if (ap_select_domain() == 0)
1629 ap_scan_bus(NULL);
1630
Felix Beck1749a812008-04-17 07:46:28 +02001631 /* Setup the AP bus rescan timer. */
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001632 init_timer(&ap_config_timer);
1633 ap_config_timer.function = ap_config_timeout;
1634 ap_config_timer.data = 0;
1635 ap_config_timer.expires = jiffies + ap_config_time * HZ;
1636 add_timer(&ap_config_timer);
1637
Felix Beckfe137232008-07-14 09:59:08 +02001638 /* Setup the high resultion poll timer.
1639 * If we are running under z/VM adjust polling to z/VM polling rate.
1640 */
1641 if (MACHINE_IS_VM)
1642 poll_timeout = 1500000;
1643 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1644 ap_poll_timer.function = ap_poll_timeout;
1645
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001646 /* Start the low priority AP bus poll thread. */
1647 if (ap_thread_flag) {
1648 rc = ap_poll_thread_start();
1649 if (rc)
1650 goto out_work;
1651 }
1652
1653 return 0;
1654
1655out_work:
1656 del_timer_sync(&ap_config_timer);
Felix Beckfe137232008-07-14 09:59:08 +02001657 hrtimer_cancel(&ap_poll_timer);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001658 destroy_workqueue(ap_work_queue);
1659out_root:
Mark McLoughlin035da162008-12-15 12:58:29 +00001660 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001661out_bus:
1662 while (i--)
1663 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1664 bus_unregister(&ap_bus_type);
1665out:
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001666 unregister_reset_call(&ap_reset_call);
Felix Beckcb17a632008-12-25 13:38:41 +01001667 if (ap_using_interrupts()) {
1668 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1669 isc_unregister(AP_ISC);
1670 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001671 return rc;
1672}
1673
1674static int __ap_match_all(struct device *dev, void *data)
1675{
1676 return 1;
1677}
1678
1679/**
Felix Beck1749a812008-04-17 07:46:28 +02001680 * ap_modules_exit(): The module termination code
1681 *
1682 * Terminates the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001683 */
1684void ap_module_exit(void)
1685{
1686 int i;
1687 struct device *dev;
1688
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001689 ap_reset_domain();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001690 ap_poll_thread_stop();
1691 del_timer_sync(&ap_config_timer);
Felix Beckfe137232008-07-14 09:59:08 +02001692 hrtimer_cancel(&ap_poll_timer);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001693 destroy_workqueue(ap_work_queue);
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001694 tasklet_kill(&ap_tasklet);
Mark McLoughlin035da162008-12-15 12:58:29 +00001695 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001696 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1697 __ap_match_all)))
1698 {
1699 device_unregister(dev);
1700 put_device(dev);
1701 }
1702 for (i = 0; ap_bus_attrs[i]; i++)
1703 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1704 bus_unregister(&ap_bus_type);
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001705 unregister_reset_call(&ap_reset_call);
Felix Beckcb17a632008-12-25 13:38:41 +01001706 if (ap_using_interrupts()) {
1707 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1708 isc_unregister(AP_ISC);
1709 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001710}
1711
1712#ifndef CONFIG_ZCRYPT_MONOLITHIC
1713module_init(ap_module_init);
1714module_exit(ap_module_exit);
1715#endif