blob: 7224bf7fa9bf1db740711f6aeab98579329127c4 [file] [log] [blame]
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001/*
Holger Dengler75014552012-08-28 16:41:50 +02002 * Copyright IBM Corp. 2006, 2012
Martin Schwidefsky1534c382006-09-20 15:58:25 +02003 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
Felix Beckcb17a632008-12-25 13:38:41 +01006 * Felix Beck <felix.beck@de.ibm.com>
Holger Dengler6bed05b2011-07-24 10:48:25 +02007 * Holger Dengler <hd@linux.vnet.ibm.com>
Martin Schwidefsky1534c382006-09-20 15:58:25 +02008 *
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
Martin Schwidefsky136f7a12008-12-25 13:39:46 +010026#define KMSG_COMPONENT "ap"
27#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28
Holger Dengler62d146f2011-01-05 12:47:38 +010029#include <linux/kernel_stat.h>
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020037#include <linux/notifier.h>
38#include <linux/kthread.h>
39#include <linux/mutex.h>
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +020040#include <linux/suspend.h>
Ralph Wuerthner85eca852006-12-08 15:54:07 +010041#include <asm/reset.h>
Felix Beckcb17a632008-12-25 13:38:41 +010042#include <asm/airq.h>
Arun Sharma600634972011-07-26 16:09:06 -070043#include <linux/atomic.h>
Felix Beckcb17a632008-12-25 13:38:41 +010044#include <asm/isc.h>
Felix Beckfe137232008-07-14 09:59:08 +020045#include <linux/hrtimer.h>
46#include <linux/ktime.h>
David Howellsa0616cd2012-03-28 18:30:02 +010047#include <asm/facility.h>
Kees Cook5d26a102014-11-20 17:05:53 -080048#include <linux/crypto.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020049
50#include "ap_bus.h"
51
52/* Some prototypes. */
Al Viro4927b3f2006-12-06 19:18:20 +000053static void ap_scan_bus(struct work_struct *);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020054static void ap_poll_all(unsigned long);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +020055static void ap_request_timeout(unsigned long);
Felix Beck772f5472009-06-22 12:08:16 +020056static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
57static int ap_device_remove(struct device *dev);
58static int ap_device_probe(struct device *dev);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020059static void ap_interrupt_handler(struct airq_struct *airq);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +020060static void ap_reset(struct ap_device *ap_dev, unsigned long *flags);
Felix Beck5314af62009-09-22 22:58:51 +020061static int ap_select_domain(void);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020062
Felix Beck1749a812008-04-17 07:46:28 +020063/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +020064 * Module description.
65 */
66MODULE_AUTHOR("IBM Corporation");
Holger Dengler75014552012-08-28 16:41:50 +020067MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
68 "Copyright IBM Corp. 2006, 2012");
Martin Schwidefsky1534c382006-09-20 15:58:25 +020069MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -080070MODULE_ALIAS_CRYPTO("z90crypt");
Martin Schwidefsky1534c382006-09-20 15:58:25 +020071
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 */
Michael Veigelc1a42f42014-04-14 14:28:27 +020076module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020077MODULE_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;
Michael Veigelc1a42f42014-04-14 14:28:27 +020081module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
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;
Holger Dengler75014552012-08-28 16:41:50 +020085static struct ap_config_info *ap_configuration;
Christian Maaser43c207e62008-12-25 13:38:42 +010086static DEFINE_SPINLOCK(ap_device_list_lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +010087static LIST_HEAD(ap_device_list);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020088
Felix Beck1749a812008-04-17 07:46:28 +020089/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +020090 * Workqueue & timer for bus rescan.
91 */
92static struct workqueue_struct *ap_work_queue;
93static struct timer_list ap_config_timer;
94static int ap_config_time = AP_CONFIG_TIME;
Al Viro4927b3f2006-12-06 19:18:20 +000095static DECLARE_WORK(ap_config_work, ap_scan_bus);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020096
Felix Beck1749a812008-04-17 07:46:28 +020097/*
Felix Beckcb17a632008-12-25 13:38:41 +010098 * Tasklet & timer for AP request polling and interrupts
Martin Schwidefsky1534c382006-09-20 15:58:25 +020099 */
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200100static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
101static atomic_t ap_poll_requests = ATOMIC_INIT(0);
102static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
103static struct task_struct *ap_poll_kthread = NULL;
104static DEFINE_MUTEX(ap_poll_thread_mutex);
Felix Beck93521312009-12-07 12:52:00 +0100105static DEFINE_SPINLOCK(ap_poll_timer_lock);
Felix Beckfe137232008-07-14 09:59:08 +0200106static struct hrtimer ap_poll_timer;
107/* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
108 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
109static unsigned long long poll_timeout = 250000;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200110
Felix Beck772f5472009-06-22 12:08:16 +0200111/* Suspend flag */
112static int ap_suspend_flag;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200113/* Maximum domain id */
114static int ap_max_domain_id;
Felix Beck5314af62009-09-22 22:58:51 +0200115/* Flag to check if domain was set through module parameter domain=. This is
116 * important when supsend and resume is done in a z/VM environment where the
117 * domain might change. */
118static int user_set_domain = 0;
Felix Beck772f5472009-06-22 12:08:16 +0200119static struct bus_type ap_bus_type;
120
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200121/* Adapter interrupt definitions */
122static int ap_airq_flag;
123
124static struct airq_struct ap_airq = {
125 .handler = ap_interrupt_handler,
126 .isc = AP_ISC,
127};
128
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200129/**
Felix Beckcb17a632008-12-25 13:38:41 +0100130 * ap_using_interrupts() - Returns non-zero if interrupt support is
131 * available.
132 */
133static inline int ap_using_interrupts(void)
134{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200135 return ap_airq_flag;
Felix Beckcb17a632008-12-25 13:38:41 +0100136}
137
138/**
Felix Beck1749a812008-04-17 07:46:28 +0200139 * ap_intructions_available() - Test if AP instructions are available.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200140 *
Felix Beck1749a812008-04-17 07:46:28 +0200141 * Returns 0 if the AP instructions are installed.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200142 */
143static inline int ap_instructions_available(void)
144{
145 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
146 register unsigned long reg1 asm ("1") = -ENODEV;
147 register unsigned long reg2 asm ("2") = 0UL;
148
149 asm volatile(
150 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
151 "0: la %1,0\n"
152 "1:\n"
153 EX_TABLE(0b, 1b)
154 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
155 return reg1;
156}
157
158/**
Felix Beckcb17a632008-12-25 13:38:41 +0100159 * ap_interrupts_available(): Test if AP interrupts are available.
160 *
161 * Returns 1 if AP interrupts are available.
162 */
163static int ap_interrupts_available(void)
164{
Heiko Carstens86cd7412015-02-14 11:23:21 +0100165 return test_facility(65);
Felix Beckcb17a632008-12-25 13:38:41 +0100166}
167
168/**
Holger Dengler75014552012-08-28 16:41:50 +0200169 * ap_configuration_available(): Test if AP configuration
170 * information is available.
171 *
172 * Returns 1 if AP configuration information is available.
173 */
174static int ap_configuration_available(void)
175{
Heiko Carstens86cd7412015-02-14 11:23:21 +0100176 return test_facility(12);
Holger Dengler75014552012-08-28 16:41:50 +0200177}
178
179/**
Felix Beck1749a812008-04-17 07:46:28 +0200180 * ap_test_queue(): Test adjunct processor queue.
181 * @qid: The AP queue number
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200182 * @info: Pointer to queue descriptor
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200183 *
Felix Beck1749a812008-04-17 07:46:28 +0200184 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200185 */
186static inline struct ap_queue_status
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200187ap_test_queue(ap_qid_t qid, unsigned long *info)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200188{
189 register unsigned long reg0 asm ("0") = qid;
190 register struct ap_queue_status reg1 asm ("1");
191 register unsigned long reg2 asm ("2") = 0UL;
192
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200193 if (test_facility(15))
194 reg0 |= 1UL << 23; /* set APFT T bit*/
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200195 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
196 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200197 if (info)
198 *info = reg2;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200199 return reg1;
200}
201
202/**
Felix Beck1749a812008-04-17 07:46:28 +0200203 * ap_reset_queue(): Reset adjunct processor queue.
204 * @qid: The AP queue number
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200205 *
Felix Beck1749a812008-04-17 07:46:28 +0200206 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200207 */
208static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
209{
210 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
211 register struct ap_queue_status reg1 asm ("1");
212 register unsigned long reg2 asm ("2") = 0UL;
213
214 asm volatile(
215 ".long 0xb2af0000" /* PQAP(RAPQ) */
216 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
217 return reg1;
218}
219
Felix Beckcb17a632008-12-25 13:38:41 +0100220/**
221 * ap_queue_interruption_control(): Enable interruption for a specific AP.
222 * @qid: The AP queue number
223 * @ind: The notification indicator byte
224 *
225 * Returns AP queue status.
226 */
227static inline struct ap_queue_status
228ap_queue_interruption_control(ap_qid_t qid, void *ind)
229{
230 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
231 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
232 register struct ap_queue_status reg1_out asm ("1");
233 register void *reg2 asm ("2") = ind;
234 asm volatile(
Holger Denglera7475af2012-05-16 14:10:26 +0200235 ".long 0xb2af0000" /* PQAP(AQIC) */
Felix Beckcb17a632008-12-25 13:38:41 +0100236 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
237 :
238 : "cc" );
239 return reg1_out;
240}
Felix Beckcb17a632008-12-25 13:38:41 +0100241
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200242/**
243 * ap_query_configuration(): Get AP configuration data
244 *
245 * Returns 0 on success, or -EOPNOTSUPP.
246 */
247static inline int ap_query_configuration(void)
Holger Dengler75014552012-08-28 16:41:50 +0200248{
249 register unsigned long reg0 asm ("0") = 0x04000000UL;
250 register unsigned long reg1 asm ("1") = -EINVAL;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200251 register void *reg2 asm ("2") = (void *) ap_configuration;
Holger Dengler75014552012-08-28 16:41:50 +0200252
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200253 if (!ap_configuration)
254 return -EOPNOTSUPP;
Holger Dengler75014552012-08-28 16:41:50 +0200255 asm volatile(
256 ".long 0xb2af0000\n" /* PQAP(QCI) */
257 "0: la %1,0\n"
258 "1:\n"
259 EX_TABLE(0b, 1b)
260 : "+d" (reg0), "+d" (reg1), "+d" (reg2)
261 :
262 : "cc");
263
264 return reg1;
265}
Holger Dengler75014552012-08-28 16:41:50 +0200266
Holger Dengler6bed05b2011-07-24 10:48:25 +0200267/**
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200268 * ap_init_configuration(): Allocate and query configuration array.
269 */
270static void ap_init_configuration(void)
271{
272 if (!ap_configuration_available())
273 return;
274
275 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
276 if (!ap_configuration)
277 return;
278 if (ap_query_configuration() != 0) {
279 kfree(ap_configuration);
280 ap_configuration = NULL;
281 return;
282 }
283}
284
285/*
286 * ap_test_config(): helper function to extract the nrth bit
287 * within the unsigned int array field.
288 */
289static inline int ap_test_config(unsigned int *field, unsigned int nr)
290{
291 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
292}
293
294/*
295 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
296 * @id AP card ID
297 *
298 * Returns 0 if the card is not configured
299 * 1 if the card is configured or
300 * if the configuration information is not available
301 */
302static inline int ap_test_config_card_id(unsigned int id)
303{
304 if (!ap_configuration) /* QCI not supported */
305 return 1;
306 return ap_test_config(ap_configuration->apm, id);
307}
308
309/*
310 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
311 * @domain AP usage domain ID
312 *
313 * Returns 0 if the usage domain is not configured
314 * 1 if the usage domain is configured or
315 * if the configuration information is not available
316 */
317static inline int ap_test_config_domain(unsigned int domain)
318{
319 if (!ap_configuration) /* QCI not supported */
320 return domain < 16;
321 return ap_test_config(ap_configuration->aqm, domain);
322}
323
324/**
Felix Beckcb17a632008-12-25 13:38:41 +0100325 * ap_queue_enable_interruption(): Enable interruption on an AP.
326 * @qid: The AP queue number
327 * @ind: the notification indicator byte
328 *
329 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
330 * on the return value it waits a while and tests the AP queue if interrupts
331 * have been switched on using ap_test_queue().
332 */
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200333static int ap_queue_enable_interruption(struct ap_device *ap_dev, void *ind)
Felix Beckcb17a632008-12-25 13:38:41 +0100334{
Felix Beckcb17a632008-12-25 13:38:41 +0100335 struct ap_queue_status status;
Felix Beckcb17a632008-12-25 13:38:41 +0100336
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200337 status = ap_queue_interruption_control(ap_dev->qid, ind);
338 switch (status.response_code) {
339 case AP_RESPONSE_NORMAL:
340 case AP_RESPONSE_OTHERWISE_CHANGED:
341 return 0;
342 case AP_RESPONSE_Q_NOT_AVAIL:
343 case AP_RESPONSE_DECONFIGURED:
344 case AP_RESPONSE_CHECKSTOPPED:
345 case AP_RESPONSE_INVALID_ADDRESS:
346 return -ENODEV;
347 case AP_RESPONSE_RESET_IN_PROGRESS:
348 case AP_RESPONSE_BUSY:
349 default:
350 return -EBUSY;
Felix Beckcb17a632008-12-25 13:38:41 +0100351 }
Felix Beckcb17a632008-12-25 13:38:41 +0100352}
353
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200354/**
Felix Beck1749a812008-04-17 07:46:28 +0200355 * __ap_send(): Send message to adjunct processor queue.
356 * @qid: The AP queue number
357 * @psmid: The program supplied message identifier
358 * @msg: The message text
359 * @length: The message length
Felix Becka6a5d732009-12-07 12:51:55 +0100360 * @special: Special Bit
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200361 *
Felix Beck1749a812008-04-17 07:46:28 +0200362 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200363 * Condition code 1 on NQAP can't happen because the L bit is 1.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200364 * Condition code 2 on NQAP also means the send is incomplete,
365 * because a segment boundary was reached. The NQAP is repeated.
366 */
367static inline struct ap_queue_status
Felix Becka6a5d732009-12-07 12:51:55 +0100368__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
369 unsigned int special)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200370{
371 typedef struct { char _[length]; } msgblock;
372 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
373 register struct ap_queue_status reg1 asm ("1");
374 register unsigned long reg2 asm ("2") = (unsigned long) msg;
375 register unsigned long reg3 asm ("3") = (unsigned long) length;
376 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
Heiko Carstens7d6c3b42013-09-07 11:07:22 +0200377 register unsigned long reg5 asm ("5") = psmid & 0xffffffff;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200378
Felix Becka6a5d732009-12-07 12:51:55 +0100379 if (special == 1)
380 reg0 |= 0x400000UL;
381
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200382 asm volatile (
Holger Denglera7475af2012-05-16 14:10:26 +0200383 "0: .long 0xb2ad0042\n" /* NQAP */
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200384 " brc 2,0b"
385 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
386 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
387 : "cc" );
388 return reg1;
389}
390
391int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
392{
393 struct ap_queue_status status;
394
Felix Becka6a5d732009-12-07 12:51:55 +0100395 status = __ap_send(qid, psmid, msg, length, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200396 switch (status.response_code) {
397 case AP_RESPONSE_NORMAL:
398 return 0;
399 case AP_RESPONSE_Q_FULL:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200400 case AP_RESPONSE_RESET_IN_PROGRESS:
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200401 return -EBUSY;
Felix Becka6a5d732009-12-07 12:51:55 +0100402 case AP_RESPONSE_REQ_FAC_NOT_INST:
403 return -EINVAL;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200404 default: /* Device is gone. */
405 return -ENODEV;
406 }
407}
408EXPORT_SYMBOL(ap_send);
409
Felix Beck1749a812008-04-17 07:46:28 +0200410/**
411 * __ap_recv(): Receive message from adjunct processor queue.
412 * @qid: The AP queue number
413 * @psmid: Pointer to program supplied message identifier
414 * @msg: The message text
415 * @length: The message length
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200416 *
Felix Beck1749a812008-04-17 07:46:28 +0200417 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200418 * Condition code 1 on DQAP means the receive has taken place
419 * but only partially. The response is incomplete, hence the
420 * DQAP is repeated.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200421 * Condition code 2 on DQAP also means the receive is incomplete,
422 * this time because a segment boundary was reached. Again, the
423 * DQAP is repeated.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200424 * Note that gpr2 is used by the DQAP instruction to keep track of
425 * any 'residual' length, in case the instruction gets interrupted.
426 * Hence it gets zeroed before the instruction.
427 */
428static inline struct ap_queue_status
429__ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
430{
431 typedef struct { char _[length]; } msgblock;
432 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
433 register struct ap_queue_status reg1 asm ("1");
434 register unsigned long reg2 asm("2") = 0UL;
435 register unsigned long reg4 asm("4") = (unsigned long) msg;
436 register unsigned long reg5 asm("5") = (unsigned long) length;
437 register unsigned long reg6 asm("6") = 0UL;
438 register unsigned long reg7 asm("7") = 0UL;
439
440
441 asm volatile(
Holger Denglera7475af2012-05-16 14:10:26 +0200442 "0: .long 0xb2ae0064\n" /* DQAP */
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200443 " brc 6,0b\n"
444 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
445 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
446 "=m" (*(msgblock *) msg) : : "cc" );
447 *psmid = (((unsigned long long) reg6) << 32) + reg7;
448 return reg1;
449}
450
451int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
452{
453 struct ap_queue_status status;
454
455 status = __ap_recv(qid, psmid, msg, length);
456 switch (status.response_code) {
457 case AP_RESPONSE_NORMAL:
458 return 0;
459 case AP_RESPONSE_NO_PENDING_REPLY:
460 if (status.queue_empty)
461 return -ENOENT;
462 return -EBUSY;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200463 case AP_RESPONSE_RESET_IN_PROGRESS:
464 return -EBUSY;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200465 default:
466 return -ENODEV;
467 }
468}
469EXPORT_SYMBOL(ap_recv);
470
471/**
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200472 * __ap_schedule_poll_timer(): Schedule poll timer.
473 *
474 * Set up the timer to run the poll tasklet
475 */
476static inline void __ap_schedule_poll_timer(void)
477{
478 ktime_t hr_time;
479
480 spin_lock_bh(&ap_poll_timer_lock);
481 if (!hrtimer_is_queued(&ap_poll_timer) && !ap_suspend_flag) {
482 hr_time = ktime_set(0, poll_timeout);
483 hrtimer_forward_now(&ap_poll_timer, hr_time);
484 hrtimer_restart(&ap_poll_timer);
485 }
486 spin_unlock_bh(&ap_poll_timer_lock);
487}
488
489/**
490 * ap_schedule_poll_timer(): Schedule poll timer.
491 *
492 * Set up the timer to run the poll tasklet
493 */
494static inline void ap_schedule_poll_timer(void)
495{
496 if (ap_using_interrupts())
497 return;
498 __ap_schedule_poll_timer();
499}
500
501
502/**
Felix Beck1749a812008-04-17 07:46:28 +0200503 * ap_query_queue(): Check if an AP queue is available.
504 * @qid: The AP queue number
505 * @queue_depth: Pointer to queue depth value
506 * @device_type: Pointer to device type value
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200507 * @facilities: Pointer to facility indicator
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200508 */
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200509static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
510 unsigned int *facilities)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200511{
512 struct ap_queue_status status;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200513 unsigned long info;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200514 int nd;
515
516 if (!ap_test_config_card_id(AP_QID_DEVICE(qid)))
517 return -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200518
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200519 status = ap_test_queue(qid, &info);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200520 switch (status.response_code) {
521 case AP_RESPONSE_NORMAL:
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200522 *queue_depth = (int)(info & 0xff);
523 *device_type = (int)((info >> 24) & 0xff);
524 *facilities = (unsigned int)(info >> 32);
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200525 /* Update maximum domain id */
526 nd = (info >> 16) & 0xff;
527 if ((info & (1UL << 57)) && nd > 0)
528 ap_max_domain_id = nd;
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200529 return 0;
530 case AP_RESPONSE_Q_NOT_AVAIL:
531 case AP_RESPONSE_DECONFIGURED:
532 case AP_RESPONSE_CHECKSTOPPED:
533 case AP_RESPONSE_INVALID_ADDRESS:
534 return -ENODEV;
535 case AP_RESPONSE_RESET_IN_PROGRESS:
536 case AP_RESPONSE_OTHERWISE_CHANGED:
537 case AP_RESPONSE_BUSY:
538 return -EBUSY;
539 default:
540 BUG();
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200541 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200542}
543
544/**
Felix Beck1749a812008-04-17 07:46:28 +0200545 * ap_init_queue(): Reset an AP queue.
546 * @qid: The AP queue number
547 *
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200548 * Submit the Reset command to an AP queue.
549 * Since the reset is asynchron set the state to 'RESET_IN_PROGRESS'
550 * and check later via ap_poll_queue() if the reset is done.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200551 */
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200552static int ap_init_queue(struct ap_device *ap_dev)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200553{
554 struct ap_queue_status status;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200555
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200556 status = ap_reset_queue(ap_dev->qid);
557 switch (status.response_code) {
558 case AP_RESPONSE_NORMAL:
559 ap_dev->interrupt = AP_INTR_DISABLED;
560 ap_dev->reset = AP_RESET_IN_PROGRESS;
561 return 0;
562 case AP_RESPONSE_RESET_IN_PROGRESS:
563 case AP_RESPONSE_BUSY:
564 return -EBUSY;
565 case AP_RESPONSE_Q_NOT_AVAIL:
566 case AP_RESPONSE_DECONFIGURED:
567 case AP_RESPONSE_CHECKSTOPPED:
568 default:
569 return -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200570 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200571}
572
573/**
Felix Beck1749a812008-04-17 07:46:28 +0200574 * ap_increase_queue_count(): Arm request timeout.
575 * @ap_dev: Pointer to an AP device.
576 *
577 * Arm request timeout if an AP device was idle and a new request is submitted.
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200578 */
579static void ap_increase_queue_count(struct ap_device *ap_dev)
580{
581 int timeout = ap_dev->drv->request_timeout;
582
583 ap_dev->queue_count++;
584 if (ap_dev->queue_count == 1) {
585 mod_timer(&ap_dev->timeout, jiffies + timeout);
586 ap_dev->reset = AP_RESET_ARMED;
587 }
588}
589
590/**
Felix Beck1749a812008-04-17 07:46:28 +0200591 * ap_decrease_queue_count(): Decrease queue count.
592 * @ap_dev: Pointer to an AP device.
593 *
594 * If AP device is still alive, re-schedule request timeout if there are still
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200595 * pending requests.
596 */
597static void ap_decrease_queue_count(struct ap_device *ap_dev)
598{
599 int timeout = ap_dev->drv->request_timeout;
600
601 ap_dev->queue_count--;
602 if (ap_dev->queue_count > 0)
603 mod_timer(&ap_dev->timeout, jiffies + timeout);
604 else
Felix Beck1749a812008-04-17 07:46:28 +0200605 /*
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200606 * The timeout timer should to be disabled now - since
607 * del_timer_sync() is very expensive, we just tell via the
608 * reset flag to ignore the pending timeout timer.
609 */
610 ap_dev->reset = AP_RESET_IGNORE;
611}
612
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200613/**
614 * ap_poll_thread(): Thread that polls for finished requests.
615 * @data: Unused pointer
616 *
617 * AP bus poll thread. The purpose of this thread is to poll for
618 * finished requests in a loop if there is a "free" cpu - that is
619 * a cpu that doesn't have anything better to do. The polling stops
620 * as soon as there is another task or if all messages have been
621 * delivered.
622 */
623static int ap_poll_thread(void *data)
624{
625 DECLARE_WAITQUEUE(wait, current);
626 unsigned long flags;
627 struct ap_device *ap_dev;
628
629 set_user_nice(current, MAX_NICE);
630 set_freezable();
631 while (!kthread_should_stop()) {
632 add_wait_queue(&ap_poll_wait, &wait);
633 set_current_state(TASK_INTERRUPTIBLE);
634 if (ap_suspend_flag ||
635 atomic_read(&ap_poll_requests) <= 0) {
636 schedule();
637 try_to_freeze();
638 }
639 set_current_state(TASK_RUNNING);
640 remove_wait_queue(&ap_poll_wait, &wait);
641
642 if (need_resched()) {
643 schedule();
644 try_to_freeze();
645 continue;
646 }
647
648 flags = 0;
649 spin_lock_bh(&ap_device_list_lock);
650 list_for_each_entry(ap_dev, &ap_device_list, list) {
651 spin_lock(&ap_dev->lock);
652 __ap_poll_device(ap_dev, &flags);
653 spin_unlock(&ap_dev->lock);
654 }
655 spin_unlock_bh(&ap_device_list_lock);
656 } while (!kthread_should_stop());
657 return 0;
658}
659
660static int ap_poll_thread_start(void)
661{
662 int rc;
663
664 if (ap_using_interrupts() || ap_poll_kthread)
665 return 0;
666 mutex_lock(&ap_poll_thread_mutex);
667 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
668 rc = PTR_RET(ap_poll_kthread);
669 if (rc)
670 ap_poll_kthread = NULL;
671 mutex_unlock(&ap_poll_thread_mutex);
672 return rc;
673}
674
675static void ap_poll_thread_stop(void)
676{
677 if (!ap_poll_kthread)
678 return;
679 mutex_lock(&ap_poll_thread_mutex);
680 kthread_stop(ap_poll_kthread);
681 ap_poll_kthread = NULL;
682 mutex_unlock(&ap_poll_thread_mutex);
683}
684
Felix Beck1749a812008-04-17 07:46:28 +0200685/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200686 * AP device related attributes.
687 */
688static ssize_t ap_hwtype_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
690{
691 struct ap_device *ap_dev = to_ap_dev(dev);
692 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
693}
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200694
Christian Maaser43c207e62008-12-25 13:38:42 +0100695static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
Ingo Tuchscherer42f4dd612014-10-02 14:48:46 +0200696
697static ssize_t ap_raw_hwtype_show(struct device *dev,
698 struct device_attribute *attr, char *buf)
699{
700 struct ap_device *ap_dev = to_ap_dev(dev);
701
702 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->raw_hwtype);
703}
704
705static DEVICE_ATTR(raw_hwtype, 0444, ap_raw_hwtype_show, NULL);
706
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200707static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
708 char *buf)
709{
710 struct ap_device *ap_dev = to_ap_dev(dev);
711 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
712}
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200713
Christian Maaser43c207e62008-12-25 13:38:42 +0100714static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200715static ssize_t ap_request_count_show(struct device *dev,
716 struct device_attribute *attr,
717 char *buf)
718{
719 struct ap_device *ap_dev = to_ap_dev(dev);
720 int rc;
721
722 spin_lock_bh(&ap_dev->lock);
723 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
724 spin_unlock_bh(&ap_dev->lock);
725 return rc;
726}
727
728static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
729
Holger Denglerb26bd942012-08-28 16:43:48 +0200730static ssize_t ap_requestq_count_show(struct device *dev,
731 struct device_attribute *attr, char *buf)
732{
733 struct ap_device *ap_dev = to_ap_dev(dev);
734 int rc;
735
736 spin_lock_bh(&ap_dev->lock);
737 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
738 spin_unlock_bh(&ap_dev->lock);
739 return rc;
740}
741
742static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
743
744static ssize_t ap_pendingq_count_show(struct device *dev,
745 struct device_attribute *attr, char *buf)
746{
747 struct ap_device *ap_dev = to_ap_dev(dev);
748 int rc;
749
750 spin_lock_bh(&ap_dev->lock);
751 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
752 spin_unlock_bh(&ap_dev->lock);
753 return rc;
754}
755
756static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
757
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200758static ssize_t ap_reset_show(struct device *dev,
759 struct device_attribute *attr, char *buf)
760{
761 struct ap_device *ap_dev = to_ap_dev(dev);
762 int rc = 0;
763
764 spin_lock_bh(&ap_dev->lock);
765 switch (ap_dev->reset) {
766 case AP_RESET_IGNORE:
767 rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
768 break;
769 case AP_RESET_ARMED:
770 rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
771 break;
772 case AP_RESET_DO:
773 rc = snprintf(buf, PAGE_SIZE, "Reset Timer expired.\n");
774 break;
775 case AP_RESET_IN_PROGRESS:
776 rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
777 break;
778 default:
779 break;
780 }
781 spin_unlock_bh(&ap_dev->lock);
782 return rc;
783}
784
785static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL);
786
787static ssize_t ap_interrupt_show(struct device *dev,
788 struct device_attribute *attr, char *buf)
789{
790 struct ap_device *ap_dev = to_ap_dev(dev);
791 int rc = 0;
792
793 spin_lock_bh(&ap_dev->lock);
794 switch (ap_dev->interrupt) {
795 case AP_INTR_DISABLED:
796 rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
797 break;
798 case AP_INTR_ENABLED:
799 rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
800 break;
801 case AP_INTR_IN_PROGRESS:
802 rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
803 break;
804 }
805 spin_unlock_bh(&ap_dev->lock);
806 return rc;
807}
808
809static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL);
810
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200811static ssize_t ap_modalias_show(struct device *dev,
812 struct device_attribute *attr, char *buf)
813{
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200814 return sprintf(buf, "ap:t%02X\n", to_ap_dev(dev)->device_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200815}
816
817static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
818
Holger Denglerb26bd942012-08-28 16:43:48 +0200819static ssize_t ap_functions_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
821{
822 struct ap_device *ap_dev = to_ap_dev(dev);
823 return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
824}
825
826static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
827
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200828static struct attribute *ap_dev_attrs[] = {
829 &dev_attr_hwtype.attr,
Ingo Tuchscherer42f4dd612014-10-02 14:48:46 +0200830 &dev_attr_raw_hwtype.attr,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200831 &dev_attr_depth.attr,
832 &dev_attr_request_count.attr,
Holger Denglerb26bd942012-08-28 16:43:48 +0200833 &dev_attr_requestq_count.attr,
834 &dev_attr_pendingq_count.attr,
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200835 &dev_attr_reset.attr,
836 &dev_attr_interrupt.attr,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200837 &dev_attr_modalias.attr,
Holger Denglerb26bd942012-08-28 16:43:48 +0200838 &dev_attr_ap_functions.attr,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200839 NULL
840};
841static struct attribute_group ap_dev_attr_group = {
842 .attrs = ap_dev_attrs
843};
844
845/**
Felix Beck1749a812008-04-17 07:46:28 +0200846 * ap_bus_match()
847 * @dev: Pointer to device
848 * @drv: Pointer to device_driver
849 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200850 * AP bus driver registration/unregistration.
851 */
852static int ap_bus_match(struct device *dev, struct device_driver *drv)
853{
854 struct ap_device *ap_dev = to_ap_dev(dev);
855 struct ap_driver *ap_drv = to_ap_drv(drv);
856 struct ap_device_id *id;
857
Felix Beck1749a812008-04-17 07:46:28 +0200858 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200859 * Compare device type of the device with the list of
860 * supported types of the device_driver.
861 */
862 for (id = ap_drv->ids; id->match_flags; id++) {
863 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
864 (id->dev_type != ap_dev->device_type))
865 continue;
866 return 1;
867 }
868 return 0;
869}
870
871/**
Felix Beck1749a812008-04-17 07:46:28 +0200872 * ap_uevent(): Uevent function for AP devices.
873 * @dev: Pointer to device
874 * @env: Pointer to kobj_uevent_env
875 *
876 * It sets up a single environment variable DEV_TYPE which contains the
877 * hardware device type.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200878 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200879static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200880{
881 struct ap_device *ap_dev = to_ap_dev(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200882 int retval = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200883
884 if (!ap_dev)
885 return -ENODEV;
886
887 /* Set up DEV_TYPE environment variable. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200888 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700889 if (retval)
890 return retval;
891
Cornelia Huck66a4263b2006-12-04 15:40:10 +0100892 /* Add MODALIAS= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200893 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700894
Eric Rannaudbf624562007-03-30 22:23:12 -0700895 return retval;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200896}
897
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200898static int ap_dev_suspend(struct device *dev, pm_message_t state)
Felix Beck772f5472009-06-22 12:08:16 +0200899{
900 struct ap_device *ap_dev = to_ap_dev(dev);
901 unsigned long flags;
902
Felix Beck772f5472009-06-22 12:08:16 +0200903 /* Poll on the device until all requests are finished. */
904 do {
905 flags = 0;
Felix Beck95f15562009-09-11 10:28:51 +0200906 spin_lock_bh(&ap_dev->lock);
Felix Beck772f5472009-06-22 12:08:16 +0200907 __ap_poll_device(ap_dev, &flags);
Felix Beck95f15562009-09-11 10:28:51 +0200908 spin_unlock_bh(&ap_dev->lock);
Felix Beck772f5472009-06-22 12:08:16 +0200909 } while ((flags & 1) || (flags & 2));
910
Felix Beck772f5472009-06-22 12:08:16 +0200911 return 0;
912}
913
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200914static int ap_dev_resume(struct device *dev)
Felix Beck772f5472009-06-22 12:08:16 +0200915{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200916 return 0;
917}
918
919static void ap_bus_suspend(void)
920{
921 ap_suspend_flag = 1;
922 /*
923 * Disable scanning for devices, thus we do not want to scan
924 * for them after removing.
925 */
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200926 flush_workqueue(ap_work_queue);
927 tasklet_disable(&ap_tasklet);
928}
929
930static int __ap_devices_unregister(struct device *dev, void *dummy)
931{
932 device_unregister(dev);
933 return 0;
934}
935
936static void ap_bus_resume(void)
937{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200938 int rc;
Felix Beck772f5472009-06-22 12:08:16 +0200939
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200940 /* Unconditionally remove all AP devices */
941 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
942 /* Reset thin interrupt setting */
943 if (ap_interrupts_available() && !ap_using_interrupts()) {
944 rc = register_adapter_interrupt(&ap_airq);
945 ap_airq_flag = (rc == 0);
Felix Beck5314af62009-09-22 22:58:51 +0200946 }
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200947 if (!ap_interrupts_available() && ap_using_interrupts()) {
948 unregister_adapter_interrupt(&ap_airq);
949 ap_airq_flag = 0;
950 }
951 /* Reset domain */
952 if (!user_set_domain)
953 ap_domain_index = -1;
954 /* Get things going again */
955 ap_suspend_flag = 0;
956 if (ap_airq_flag)
957 xchg(ap_airq.lsi_ptr, 0);
958 tasklet_enable(&ap_tasklet);
Felix Beck5314af62009-09-22 22:58:51 +0200959 queue_work(ap_work_queue, &ap_config_work);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200960 wake_up(&ap_poll_wait);
Felix Beck772f5472009-06-22 12:08:16 +0200961}
962
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200963static int ap_power_event(struct notifier_block *this, unsigned long event,
964 void *ptr)
965{
966 switch (event) {
967 case PM_HIBERNATION_PREPARE:
968 case PM_SUSPEND_PREPARE:
969 ap_bus_suspend();
970 break;
971 case PM_POST_HIBERNATION:
972 case PM_POST_SUSPEND:
973 ap_bus_resume();
974 break;
975 default:
976 break;
977 }
978 return NOTIFY_DONE;
979}
980static struct notifier_block ap_power_notifier = {
981 .notifier_call = ap_power_event,
982};
983
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200984static struct bus_type ap_bus_type = {
985 .name = "ap",
986 .match = &ap_bus_match,
987 .uevent = &ap_uevent,
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200988 .suspend = ap_dev_suspend,
989 .resume = ap_dev_resume,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200990};
991
992static int ap_device_probe(struct device *dev)
993{
994 struct ap_device *ap_dev = to_ap_dev(dev);
995 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
996 int rc;
997
998 ap_dev->drv = ap_drv;
Ingo Tuchscherer666e68e2014-07-14 19:11:48 +0200999
1000 spin_lock_bh(&ap_device_list_lock);
1001 list_add(&ap_dev->list, &ap_device_list);
1002 spin_unlock_bh(&ap_device_list_lock);
1003
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001004 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
Ingo Tuchscherer666e68e2014-07-14 19:11:48 +02001005 if (rc) {
Christian Maaser43c207e62008-12-25 13:38:42 +01001006 spin_lock_bh(&ap_device_list_lock);
Ingo Tuchscherer666e68e2014-07-14 19:11:48 +02001007 list_del_init(&ap_dev->list);
Christian Maaser43c207e62008-12-25 13:38:42 +01001008 spin_unlock_bh(&ap_device_list_lock);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001009 } else {
1010 if (ap_dev->reset == AP_RESET_IN_PROGRESS ||
1011 ap_dev->interrupt == AP_INTR_IN_PROGRESS)
1012 __ap_schedule_poll_timer();
Ralph Wuerthnerfaa582c2008-03-05 12:37:13 +01001013 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001014 return rc;
1015}
1016
1017/**
Felix Beck1749a812008-04-17 07:46:28 +02001018 * __ap_flush_queue(): Flush requests.
1019 * @ap_dev: Pointer to the AP device
1020 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001021 * Flush all requests from the request/pending queue of an AP device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001022 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +01001023static void __ap_flush_queue(struct ap_device *ap_dev)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001024{
1025 struct ap_message *ap_msg, *next;
1026
1027 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
1028 list_del_init(&ap_msg->list);
1029 ap_dev->pendingq_count--;
Martin Schwidefskyf58fe332015-09-14 16:59:27 +02001030 ap_msg->rc = -EAGAIN;
1031 ap_msg->receive(ap_dev, ap_msg, NULL);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001032 }
1033 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
1034 list_del_init(&ap_msg->list);
1035 ap_dev->requestq_count--;
Martin Schwidefskyf58fe332015-09-14 16:59:27 +02001036 ap_msg->rc = -EAGAIN;
1037 ap_msg->receive(ap_dev, ap_msg, NULL);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001038 }
1039}
1040
1041void ap_flush_queue(struct ap_device *ap_dev)
1042{
1043 spin_lock_bh(&ap_dev->lock);
1044 __ap_flush_queue(ap_dev);
1045 spin_unlock_bh(&ap_dev->lock);
1046}
1047EXPORT_SYMBOL(ap_flush_queue);
1048
1049static int ap_device_remove(struct device *dev)
1050{
1051 struct ap_device *ap_dev = to_ap_dev(dev);
1052 struct ap_driver *ap_drv = ap_dev->drv;
1053
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001054 ap_flush_queue(ap_dev);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001055 del_timer_sync(&ap_dev->timeout);
Christian Maaser43c207e62008-12-25 13:38:42 +01001056 spin_lock_bh(&ap_device_list_lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001057 list_del_init(&ap_dev->list);
Christian Maaser43c207e62008-12-25 13:38:42 +01001058 spin_unlock_bh(&ap_device_list_lock);
Ralph Wuerthnerfaa582c2008-03-05 12:37:13 +01001059 if (ap_drv->remove)
1060 ap_drv->remove(ap_dev);
Ralph Wuerthnere675c0d2007-03-26 20:42:43 +02001061 spin_lock_bh(&ap_dev->lock);
1062 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1063 spin_unlock_bh(&ap_dev->lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001064 return 0;
1065}
1066
1067int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1068 char *name)
1069{
1070 struct device_driver *drv = &ap_drv->driver;
1071
1072 drv->bus = &ap_bus_type;
1073 drv->probe = ap_device_probe;
1074 drv->remove = ap_device_remove;
1075 drv->owner = owner;
1076 drv->name = name;
1077 return driver_register(drv);
1078}
1079EXPORT_SYMBOL(ap_driver_register);
1080
1081void ap_driver_unregister(struct ap_driver *ap_drv)
1082{
1083 driver_unregister(&ap_drv->driver);
1084}
1085EXPORT_SYMBOL(ap_driver_unregister);
1086
Holger Denglerdabecb22012-09-10 21:34:26 +02001087void ap_bus_force_rescan(void)
1088{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001089 if (ap_suspend_flag)
1090 return;
Ingo Tuchscherer56bbe682013-04-12 17:52:08 +02001091 /* processing a asynchronous bus rescan */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001092 del_timer(&ap_config_timer);
Ingo Tuchscherer56bbe682013-04-12 17:52:08 +02001093 queue_work(ap_work_queue, &ap_config_work);
1094 flush_work(&ap_config_work);
Holger Denglerdabecb22012-09-10 21:34:26 +02001095}
1096EXPORT_SYMBOL(ap_bus_force_rescan);
1097
Felix Beck1749a812008-04-17 07:46:28 +02001098/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001099 * AP bus attributes.
1100 */
1101static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1102{
1103 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1104}
1105
1106static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
1107
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001108static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1109{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001110 if (!ap_configuration) /* QCI not supported */
1111 return snprintf(buf, PAGE_SIZE, "not supported\n");
1112 if (!test_facility(76))
1113 /* format 0 - 16 bit domain field */
1114 return snprintf(buf, PAGE_SIZE, "%08x%08x\n",
1115 ap_configuration->adm[0],
1116 ap_configuration->adm[1]);
1117 /* format 1 - 256 bit domain field */
1118 return snprintf(buf, PAGE_SIZE,
1119 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001120 ap_configuration->adm[0], ap_configuration->adm[1],
1121 ap_configuration->adm[2], ap_configuration->adm[3],
1122 ap_configuration->adm[4], ap_configuration->adm[5],
1123 ap_configuration->adm[6], ap_configuration->adm[7]);
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001124}
1125
1126static BUS_ATTR(ap_control_domain_mask, 0444,
1127 ap_control_domain_mask_show, NULL);
1128
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001129static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
1130{
1131 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1132}
1133
Felix Beckcb17a632008-12-25 13:38:41 +01001134static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1135{
1136 return snprintf(buf, PAGE_SIZE, "%d\n",
1137 ap_using_interrupts() ? 1 : 0);
1138}
1139
1140static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
1141
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001142static ssize_t ap_config_time_store(struct bus_type *bus,
1143 const char *buf, size_t count)
1144{
1145 int time;
1146
1147 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1148 return -EINVAL;
1149 ap_config_time = time;
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001150 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001151 return count;
1152}
1153
1154static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
1155
1156static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
1157{
1158 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1159}
1160
1161static ssize_t ap_poll_thread_store(struct bus_type *bus,
1162 const char *buf, size_t count)
1163{
1164 int flag, rc;
1165
1166 if (sscanf(buf, "%d\n", &flag) != 1)
1167 return -EINVAL;
1168 if (flag) {
1169 rc = ap_poll_thread_start();
1170 if (rc)
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001171 count = rc;
1172 } else
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001173 ap_poll_thread_stop();
1174 return count;
1175}
1176
1177static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
1178
Felix Beckfe137232008-07-14 09:59:08 +02001179static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1180{
1181 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1182}
1183
1184static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1185 size_t count)
1186{
1187 unsigned long long time;
1188 ktime_t hr_time;
1189
1190 /* 120 seconds = maximum poll interval */
Felix Beckcb17a632008-12-25 13:38:41 +01001191 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1192 time > 120000000000ULL)
Felix Beckfe137232008-07-14 09:59:08 +02001193 return -EINVAL;
1194 poll_timeout = time;
1195 hr_time = ktime_set(0, poll_timeout);
1196
Ingo Tuchscherer8cc2af72015-04-28 10:31:44 +02001197 spin_lock_bh(&ap_poll_timer_lock);
1198 hrtimer_cancel(&ap_poll_timer);
1199 hrtimer_set_expires(&ap_poll_timer, hr_time);
1200 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1201 spin_unlock_bh(&ap_poll_timer_lock);
1202
Felix Beckfe137232008-07-14 09:59:08 +02001203 return count;
1204}
1205
1206static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
1207
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001208static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1209{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001210 int max_domain_id;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001211
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001212 if (ap_configuration)
1213 max_domain_id = ap_max_domain_id ? : -1;
1214 else
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001215 max_domain_id = 15;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001216 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1217}
1218
1219static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
1220
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001221static struct bus_attribute *const ap_bus_attrs[] = {
1222 &bus_attr_ap_domain,
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001223 &bus_attr_ap_control_domain_mask,
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001224 &bus_attr_config_time,
1225 &bus_attr_poll_thread,
Felix Beckcb17a632008-12-25 13:38:41 +01001226 &bus_attr_ap_interrupts,
Felix Beckfe137232008-07-14 09:59:08 +02001227 &bus_attr_poll_timeout,
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001228 &bus_attr_ap_max_domain_id,
Felix Beckfe137232008-07-14 09:59:08 +02001229 NULL,
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001230};
1231
Holger Dengler75014552012-08-28 16:41:50 +02001232/**
Felix Beck1749a812008-04-17 07:46:28 +02001233 * ap_select_domain(): Select an AP domain.
1234 *
1235 * Pick one of the 16 AP domains.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001236 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +01001237static int ap_select_domain(void)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001238{
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001239 int count, max_count, best_domain;
1240 struct ap_queue_status status;
1241 int i, j;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001242
Felix Beck1749a812008-04-17 07:46:28 +02001243 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001244 * We want to use a single domain. Either the one specified with
1245 * the "domain=" parameter or the domain with the maximum number
1246 * of devices.
1247 */
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001248 if (ap_domain_index >= 0)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001249 /* Domain has already been selected. */
1250 return 0;
1251 best_domain = -1;
1252 max_count = 0;
1253 for (i = 0; i < AP_DOMAINS; i++) {
Holger Dengler75014552012-08-28 16:41:50 +02001254 if (!ap_test_config_domain(i))
1255 continue;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001256 count = 0;
1257 for (j = 0; j < AP_DEVICES; j++) {
Holger Dengler75014552012-08-28 16:41:50 +02001258 if (!ap_test_config_card_id(j))
1259 continue;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001260 status = ap_test_queue(AP_MKQID(j, i), NULL);
1261 if (status.response_code != AP_RESPONSE_NORMAL)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001262 continue;
1263 count++;
1264 }
1265 if (count > max_count) {
1266 max_count = count;
1267 best_domain = i;
1268 }
1269 }
1270 if (best_domain >= 0){
1271 ap_domain_index = best_domain;
1272 return 0;
1273 }
1274 return -ENODEV;
1275}
1276
1277/**
Felix Beck1749a812008-04-17 07:46:28 +02001278 * ap_probe_device_type(): Find the device type of an AP.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001279 * @ap_dev: pointer to the AP device.
Felix Beck1749a812008-04-17 07:46:28 +02001280 *
1281 * Find the device type if query queue returned a device type of 0.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001282 */
1283static int ap_probe_device_type(struct ap_device *ap_dev)
1284{
1285 static unsigned char msg[] = {
1286 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1287 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1288 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1289 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1290 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1291 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1292 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1293 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1294 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1295 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1296 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1297 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1298 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1299 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1300 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1301 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1302 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1303 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1304 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1305 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1306 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1307 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1308 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1309 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1310 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1311 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1312 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1313 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1314 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1315 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1316 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1317 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1318 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1319 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1320 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1321 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1322 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1323 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1324 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1325 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1326 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1327 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1328 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1329 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1330 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1331 };
1332 struct ap_queue_status status;
1333 unsigned long long psmid;
1334 char *reply;
1335 int rc, i;
1336
1337 reply = (void *) get_zeroed_page(GFP_KERNEL);
1338 if (!reply) {
1339 rc = -ENOMEM;
1340 goto out;
1341 }
1342
1343 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
Felix Becka6a5d732009-12-07 12:51:55 +01001344 msg, sizeof(msg), 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001345 if (status.response_code != AP_RESPONSE_NORMAL) {
1346 rc = -ENODEV;
1347 goto out_free;
1348 }
1349
1350 /* Wait for the test message to complete. */
1351 for (i = 0; i < 6; i++) {
Heiko Carstense4e18992015-08-17 07:59:47 +02001352 msleep(300);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001353 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1354 if (status.response_code == AP_RESPONSE_NORMAL &&
1355 psmid == 0x0102030405060708ULL)
1356 break;
1357 }
1358 if (i < 6) {
1359 /* Got an answer. */
1360 if (reply[0] == 0x00 && reply[1] == 0x86)
1361 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1362 else
1363 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1364 rc = 0;
1365 } else
1366 rc = -ENODEV;
1367
1368out_free:
1369 free_page((unsigned long) reply);
1370out:
1371 return rc;
1372}
1373
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001374static void ap_interrupt_handler(struct airq_struct *airq)
Felix Beckcb17a632008-12-25 13:38:41 +01001375{
Heiko Carstens420f42e2013-01-02 15:18:18 +01001376 inc_irq_stat(IRQIO_APB);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001377 if (!ap_suspend_flag)
1378 tasklet_schedule(&ap_tasklet);
Felix Beckcb17a632008-12-25 13:38:41 +01001379}
1380
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001381/**
Felix Beck1749a812008-04-17 07:46:28 +02001382 * __ap_scan_bus(): Scan the AP bus.
1383 * @dev: Pointer to device
1384 * @data: Pointer to data
1385 *
1386 * Scan the AP bus for new devices.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001387 */
1388static int __ap_scan_bus(struct device *dev, void *data)
1389{
1390 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1391}
1392
1393static void ap_device_release(struct device *dev)
1394{
1395 struct ap_device *ap_dev = to_ap_dev(dev);
1396
1397 kfree(ap_dev);
1398}
1399
Al Viro4927b3f2006-12-06 19:18:20 +00001400static void ap_scan_bus(struct work_struct *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001401{
1402 struct ap_device *ap_dev;
1403 struct device *dev;
1404 ap_qid_t qid;
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001405 int queue_depth = 0, device_type = 0;
Holger Dengler6bed05b2011-07-24 10:48:25 +02001406 unsigned int device_functions;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001407 int rc, i;
1408
Holger Dengler75014552012-08-28 16:41:50 +02001409 ap_query_configuration();
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001410 if (ap_select_domain() != 0)
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001411 goto out;
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001412
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001413 for (i = 0; i < AP_DEVICES; i++) {
1414 qid = AP_MKQID(i, ap_domain_index);
1415 dev = bus_find_device(&ap_bus_type, NULL,
1416 (void *)(unsigned long)qid,
1417 __ap_scan_bus);
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001418 rc = ap_query_queue(qid, &queue_depth, &device_type,
1419 &device_functions);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001420 if (dev) {
1421 ap_dev = to_ap_dev(dev);
1422 spin_lock_bh(&ap_dev->lock);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001423 if (rc == -ENODEV || ap_dev->unregistered) {
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001424 spin_unlock_bh(&ap_dev->lock);
Felix Beck5314af62009-09-22 22:58:51 +02001425 if (ap_dev->unregistered)
1426 i--;
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001427 device_unregister(dev);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001428 put_device(dev);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001429 continue;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001430 }
1431 spin_unlock_bh(&ap_dev->lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001432 put_device(dev);
1433 continue;
1434 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001435 if (rc)
1436 continue;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001437 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1438 if (!ap_dev)
1439 break;
1440 ap_dev->qid = qid;
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001441 rc = ap_init_queue(ap_dev);
1442 if ((rc != 0) && (rc != -EBUSY)) {
1443 kfree(ap_dev);
1444 continue;
1445 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001446 ap_dev->queue_depth = queue_depth;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001447 ap_dev->raw_hwtype = device_type;
1448 ap_dev->device_type = device_type;
1449 ap_dev->functions = device_functions;
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001450 ap_dev->unregistered = 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001451 spin_lock_init(&ap_dev->lock);
1452 INIT_LIST_HEAD(&ap_dev->pendingq);
1453 INIT_LIST_HEAD(&ap_dev->requestq);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001454 INIT_LIST_HEAD(&ap_dev->list);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001455 setup_timer(&ap_dev->timeout, ap_request_timeout,
1456 (unsigned long) ap_dev);
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001457 if (ap_dev->device_type == 0)
Holger Dengler1e2076f2012-08-28 16:48:29 +02001458 /* device type probing for old cards */
Holger Denglercf2d0072011-05-23 10:24:30 +02001459 if (ap_probe_device_type(ap_dev)) {
1460 kfree(ap_dev);
1461 continue;
1462 }
Holger Dengler1e2076f2012-08-28 16:48:29 +02001463
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001464 ap_dev->device.bus = &ap_bus_type;
1465 ap_dev->device.parent = ap_root_device;
Felix Beckedc44fa2009-09-11 10:28:52 +02001466 if (dev_set_name(&ap_dev->device, "card%02x",
1467 AP_QID_DEVICE(ap_dev->qid))) {
1468 kfree(ap_dev);
1469 continue;
1470 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001471 ap_dev->device.release = ap_device_release;
1472 rc = device_register(&ap_dev->device);
1473 if (rc) {
Sebastian Ottc6304932009-09-11 10:28:38 +02001474 put_device(&ap_dev->device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001475 continue;
1476 }
1477 /* Add device attributes. */
1478 rc = sysfs_create_group(&ap_dev->device.kobj,
1479 &ap_dev_attr_group);
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001480 if (!rc) {
1481 spin_lock_bh(&ap_dev->lock);
1482 ap_dev->unregistered = 0;
1483 spin_unlock_bh(&ap_dev->lock);
1484 }
1485 else
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001486 device_unregister(&ap_dev->device);
1487 }
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001488out:
1489 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001490}
1491
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001492static void ap_config_timeout(unsigned long ptr)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001493{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001494 if (ap_suspend_flag)
1495 return;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001496 queue_work(ap_work_queue, &ap_config_work);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001497}
1498
1499/**
Felix Beck1749a812008-04-17 07:46:28 +02001500 * ap_poll_read(): Receive pending reply messages from an AP device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001501 * @ap_dev: pointer to the AP device
1502 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1503 * required, bit 2^1 is set if the poll timer needs to get armed
Felix Beck1749a812008-04-17 07:46:28 +02001504 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001505 * Returns 0 if the device is still present, -ENODEV if not.
1506 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +01001507static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001508{
1509 struct ap_queue_status status;
1510 struct ap_message *ap_msg;
1511
1512 if (ap_dev->queue_count <= 0)
1513 return 0;
1514 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1515 ap_dev->reply->message, ap_dev->reply->length);
1516 switch (status.response_code) {
1517 case AP_RESPONSE_NORMAL:
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001518 ap_dev->interrupt = status.int_enabled;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001519 atomic_dec(&ap_poll_requests);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001520 ap_decrease_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001521 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1522 if (ap_msg->psmid != ap_dev->reply->psmid)
1523 continue;
1524 list_del_init(&ap_msg->list);
1525 ap_dev->pendingq_count--;
Holger Dengler54a8f562012-05-16 14:08:22 +02001526 ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001527 break;
1528 }
1529 if (ap_dev->queue_count > 0)
1530 *flags |= 1;
1531 break;
1532 case AP_RESPONSE_NO_PENDING_REPLY:
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001533 ap_dev->interrupt = status.int_enabled;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001534 if (status.queue_empty) {
1535 /* The card shouldn't forget requests but who knows. */
Ralph Wuerthnere675c0d2007-03-26 20:42:43 +02001536 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001537 ap_dev->queue_count = 0;
1538 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1539 ap_dev->requestq_count += ap_dev->pendingq_count;
1540 ap_dev->pendingq_count = 0;
1541 } else
1542 *flags |= 2;
1543 break;
1544 default:
1545 return -ENODEV;
1546 }
1547 return 0;
1548}
1549
1550/**
Felix Beck1749a812008-04-17 07:46:28 +02001551 * ap_poll_write(): Send messages from the request queue to an AP device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001552 * @ap_dev: pointer to the AP device
1553 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1554 * required, bit 2^1 is set if the poll timer needs to get armed
Felix Beck1749a812008-04-17 07:46:28 +02001555 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001556 * Returns 0 if the device is still present, -ENODEV if not.
1557 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +01001558static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001559{
1560 struct ap_queue_status status;
1561 struct ap_message *ap_msg;
1562
1563 if (ap_dev->requestq_count <= 0 ||
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001564 (ap_dev->queue_count >= ap_dev->queue_depth) ||
1565 (ap_dev->reset == AP_RESET_IN_PROGRESS))
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001566 return 0;
1567 /* Start the next request on the queue. */
1568 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1569 status = __ap_send(ap_dev->qid, ap_msg->psmid,
Felix Becka6a5d732009-12-07 12:51:55 +01001570 ap_msg->message, ap_msg->length, ap_msg->special);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001571 switch (status.response_code) {
1572 case AP_RESPONSE_NORMAL:
1573 atomic_inc(&ap_poll_requests);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001574 ap_increase_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001575 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1576 ap_dev->requestq_count--;
1577 ap_dev->pendingq_count++;
1578 if (ap_dev->queue_count < ap_dev->queue_depth &&
1579 ap_dev->requestq_count > 0)
1580 *flags |= 1;
1581 *flags |= 2;
1582 break;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001583 case AP_RESPONSE_RESET_IN_PROGRESS:
Holger Denglerbc615de2011-11-14 11:19:04 +01001584 __ap_schedule_poll_timer();
1585 case AP_RESPONSE_Q_FULL:
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001586 *flags |= 2;
1587 break;
1588 case AP_RESPONSE_MESSAGE_TOO_BIG:
Felix Becka6a5d732009-12-07 12:51:55 +01001589 case AP_RESPONSE_REQ_FAC_NOT_INST:
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001590 return -EINVAL;
1591 default:
1592 return -ENODEV;
1593 }
1594 return 0;
1595}
1596
1597/**
Felix Beck1749a812008-04-17 07:46:28 +02001598 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001599 * Check if the queue has a pending reset. In case it's done re-enable
1600 * interrupts, otherwise reschedule the poll_timer for another attempt.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001601 * @ap_dev: pointer to the bus device
1602 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1603 * required, bit 2^1 is set if the poll timer needs to get armed
Felix Beck1749a812008-04-17 07:46:28 +02001604 *
1605 * Poll AP device for pending replies and send new messages. If either
1606 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001607 * Returns 0.
1608 */
1609static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1610{
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001611 struct ap_queue_status status;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001612 int rc;
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001613
1614 if (ap_dev->reset == AP_RESET_IN_PROGRESS) {
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001615 status = ap_test_queue(ap_dev->qid, NULL);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001616 switch (status.response_code) {
1617 case AP_RESPONSE_NORMAL:
1618 ap_dev->reset = AP_RESET_IGNORE;
1619 if (ap_using_interrupts()) {
1620 rc = ap_queue_enable_interruption(
1621 ap_dev, ap_airq.lsi_ptr);
1622 if (!rc)
1623 ap_dev->interrupt = AP_INTR_IN_PROGRESS;
1624 else if (rc == -ENODEV) {
1625 pr_err("Registering adapter interrupts for "
1626 "AP %d failed\n", AP_QID_DEVICE(ap_dev->qid));
1627 return rc;
1628 }
1629 }
1630 /* fall through */
1631 case AP_RESPONSE_BUSY:
1632 case AP_RESPONSE_RESET_IN_PROGRESS:
1633 *flags |= AP_POLL_AFTER_TIMEOUT;
1634 break;
1635 case AP_RESPONSE_Q_NOT_AVAIL:
1636 case AP_RESPONSE_DECONFIGURED:
1637 case AP_RESPONSE_CHECKSTOPPED:
1638 return -ENODEV;
1639 default:
1640 break;
1641 }
1642 }
1643
1644 if ((ap_dev->reset != AP_RESET_IN_PROGRESS) &&
1645 (ap_dev->interrupt == AP_INTR_IN_PROGRESS)) {
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001646 status = ap_test_queue(ap_dev->qid, NULL);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001647 if (ap_using_interrupts()) {
1648 if (status.int_enabled == 1)
1649 ap_dev->interrupt = AP_INTR_ENABLED;
1650 else
1651 *flags |= AP_POLL_AFTER_TIMEOUT;
1652 } else
1653 ap_dev->interrupt = AP_INTR_DISABLED;
1654 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001655
1656 rc = ap_poll_read(ap_dev, flags);
1657 if (rc)
1658 return rc;
1659 return ap_poll_write(ap_dev, flags);
1660}
1661
1662/**
Felix Beck1749a812008-04-17 07:46:28 +02001663 * __ap_queue_message(): Queue a message to a device.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001664 * @ap_dev: pointer to the AP device
1665 * @ap_msg: the message to be queued
Felix Beck1749a812008-04-17 07:46:28 +02001666 *
1667 * Queue a message to a device. Returns 0 if successful.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001668 */
1669static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1670{
1671 struct ap_queue_status status;
1672
1673 if (list_empty(&ap_dev->requestq) &&
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001674 (ap_dev->queue_count < ap_dev->queue_depth) &&
1675 (ap_dev->reset != AP_RESET_IN_PROGRESS)) {
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001676 status = __ap_send(ap_dev->qid, ap_msg->psmid,
Felix Becka6a5d732009-12-07 12:51:55 +01001677 ap_msg->message, ap_msg->length,
1678 ap_msg->special);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001679 switch (status.response_code) {
1680 case AP_RESPONSE_NORMAL:
1681 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1682 atomic_inc(&ap_poll_requests);
1683 ap_dev->pendingq_count++;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001684 ap_increase_queue_count(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001685 ap_dev->total_request_count++;
1686 break;
1687 case AP_RESPONSE_Q_FULL:
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001688 case AP_RESPONSE_RESET_IN_PROGRESS:
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001689 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1690 ap_dev->requestq_count++;
1691 ap_dev->total_request_count++;
1692 return -EBUSY;
Felix Becka6a5d732009-12-07 12:51:55 +01001693 case AP_RESPONSE_REQ_FAC_NOT_INST:
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001694 case AP_RESPONSE_MESSAGE_TOO_BIG:
Martin Schwidefskyf58fe332015-09-14 16:59:27 +02001695 ap_msg->rc = -EINVAL;
1696 ap_msg->receive(ap_dev, ap_msg, NULL);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001697 return -EINVAL;
1698 default: /* Device is gone. */
Martin Schwidefskyf58fe332015-09-14 16:59:27 +02001699 ap_msg->rc = -ENODEV;
1700 ap_msg->receive(ap_dev, ap_msg, NULL);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001701 return -ENODEV;
1702 }
1703 } else {
1704 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1705 ap_dev->requestq_count++;
1706 ap_dev->total_request_count++;
1707 return -EBUSY;
1708 }
1709 ap_schedule_poll_timer();
1710 return 0;
1711}
1712
1713void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1714{
1715 unsigned long flags;
1716 int rc;
1717
Holger Dengler54a8f562012-05-16 14:08:22 +02001718 /* For asynchronous message handling a valid receive-callback
1719 * is required. */
1720 BUG_ON(!ap_msg->receive);
1721
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001722 spin_lock_bh(&ap_dev->lock);
1723 if (!ap_dev->unregistered) {
1724 /* Make room on the queue by polling for finished requests. */
1725 rc = ap_poll_queue(ap_dev, &flags);
1726 if (!rc)
1727 rc = __ap_queue_message(ap_dev, ap_msg);
1728 if (!rc)
1729 wake_up(&ap_poll_wait);
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001730 if (rc == -ENODEV)
1731 ap_dev->unregistered = 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001732 } else {
Martin Schwidefskyf58fe332015-09-14 16:59:27 +02001733 ap_msg->rc = -ENODEV;
1734 ap_msg->receive(ap_dev, ap_msg, NULL);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001735 rc = -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001736 }
1737 spin_unlock_bh(&ap_dev->lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001738}
1739EXPORT_SYMBOL(ap_queue_message);
1740
1741/**
Felix Beck1749a812008-04-17 07:46:28 +02001742 * ap_cancel_message(): Cancel a crypto request.
1743 * @ap_dev: The AP device that has the message queued
1744 * @ap_msg: The message that is to be removed
1745 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001746 * Cancel a crypto request. This is done by removing the request
Felix Beck1749a812008-04-17 07:46:28 +02001747 * from the device pending or request queue. Note that the
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001748 * request stays on the AP queue. When it finishes the message
1749 * reply will be discarded because the psmid can't be found.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001750 */
1751void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1752{
1753 struct ap_message *tmp;
1754
1755 spin_lock_bh(&ap_dev->lock);
1756 if (!list_empty(&ap_msg->list)) {
1757 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1758 if (tmp->psmid == ap_msg->psmid) {
1759 ap_dev->pendingq_count--;
1760 goto found;
1761 }
1762 ap_dev->requestq_count--;
1763 found:
1764 list_del_init(&ap_msg->list);
1765 }
1766 spin_unlock_bh(&ap_dev->lock);
1767}
1768EXPORT_SYMBOL(ap_cancel_message);
1769
1770/**
Felix Beck1749a812008-04-17 07:46:28 +02001771 * ap_poll_timeout(): AP receive polling for finished AP requests.
Felix Beckfe137232008-07-14 09:59:08 +02001772 * @unused: Unused pointer.
Felix Beck1749a812008-04-17 07:46:28 +02001773 *
Felix Beckfe137232008-07-14 09:59:08 +02001774 * Schedules the AP tasklet using a high resolution timer.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001775 */
Felix Beckfe137232008-07-14 09:59:08 +02001776static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001777{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001778 if (!ap_suspend_flag)
1779 tasklet_schedule(&ap_tasklet);
Felix Beckfe137232008-07-14 09:59:08 +02001780 return HRTIMER_NORESTART;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001781}
1782
1783/**
Felix Beck1749a812008-04-17 07:46:28 +02001784 * ap_reset(): Reset a not responding AP device.
1785 * @ap_dev: Pointer to the AP device
1786 *
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001787 * Reset a not responding AP device and move all requests from the
1788 * pending queue to the request queue.
1789 */
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001790static void ap_reset(struct ap_device *ap_dev, unsigned long *flags)
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001791{
1792 int rc;
1793
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001794 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1795 ap_dev->queue_count = 0;
1796 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1797 ap_dev->requestq_count += ap_dev->pendingq_count;
1798 ap_dev->pendingq_count = 0;
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001799 rc = ap_init_queue(ap_dev);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001800 if (rc == -ENODEV)
1801 ap_dev->unregistered = 1;
Holger Dengler75464962011-12-01 13:32:23 +01001802 else
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001803 *flags |= AP_POLL_AFTER_TIMEOUT;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001804}
1805
Christian Maaser43c207e62008-12-25 13:38:42 +01001806static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001807{
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001808 if (!ap_dev->unregistered) {
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001809 if (ap_poll_queue(ap_dev, flags))
Ralph Wuerthner4e562962006-10-04 20:02:05 +02001810 ap_dev->unregistered = 1;
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001811 if (ap_dev->reset == AP_RESET_DO)
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001812 ap_reset(ap_dev, flags);
Ralph Wuerthnerc6a48262007-03-26 20:42:42 +02001813 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001814 return 0;
1815}
1816
Felix Beck1749a812008-04-17 07:46:28 +02001817/**
1818 * ap_poll_all(): Poll all AP devices.
1819 * @dummy: Unused variable
1820 *
1821 * Poll all AP devices on the bus in a round robin fashion. Continue
1822 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1823 * of the control flags has been set arm the poll timer.
1824 */
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001825static void ap_poll_all(unsigned long dummy)
1826{
1827 unsigned long flags;
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001828 struct ap_device *ap_dev;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001829
Felix Beckcb17a632008-12-25 13:38:41 +01001830 /* Reset the indicator if interrupts are used. Thus new interrupts can
1831 * be received. Doing it in the beginning of the tasklet is therefor
1832 * important that no requests on any AP get lost.
1833 */
1834 if (ap_using_interrupts())
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001835 xchg(ap_airq.lsi_ptr, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001836 do {
1837 flags = 0;
Christian Maaser43c207e62008-12-25 13:38:42 +01001838 spin_lock(&ap_device_list_lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001839 list_for_each_entry(ap_dev, &ap_device_list, list) {
Felix Beck95f15562009-09-11 10:28:51 +02001840 spin_lock(&ap_dev->lock);
Christian Maaser43c207e62008-12-25 13:38:42 +01001841 __ap_poll_device(ap_dev, &flags);
Felix Beck95f15562009-09-11 10:28:51 +02001842 spin_unlock(&ap_dev->lock);
Ralph Wuerthnercf352ce2007-03-19 13:19:14 +01001843 }
Christian Maaser43c207e62008-12-25 13:38:42 +01001844 spin_unlock(&ap_device_list_lock);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +02001845 } while (flags & AP_POLL_IMMEDIATELY);
1846 if (flags & AP_POLL_AFTER_TIMEOUT)
1847 __ap_schedule_poll_timer();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001848}
1849
1850/**
Felix Beck1749a812008-04-17 07:46:28 +02001851 * ap_request_timeout(): Handling of request timeouts
1852 * @data: Holds the AP device.
1853 *
1854 * Handles request timeouts.
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001855 */
1856static void ap_request_timeout(unsigned long data)
1857{
1858 struct ap_device *ap_dev = (struct ap_device *) data;
1859
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001860 if (ap_suspend_flag)
1861 return;
Felix Beckcb17a632008-12-25 13:38:41 +01001862 if (ap_dev->reset == AP_RESET_ARMED) {
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001863 ap_dev->reset = AP_RESET_DO;
Felix Beckcb17a632008-12-25 13:38:41 +01001864
1865 if (ap_using_interrupts())
1866 tasklet_schedule(&ap_tasklet);
1867 }
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +02001868}
1869
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001870static void ap_reset_domain(void)
1871{
1872 int i;
1873
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001874 if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index))
1875 return;
1876 for (i = 0; i < AP_DEVICES; i++)
1877 ap_reset_queue(AP_MKQID(i, ap_domain_index));
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001878}
1879
1880static void ap_reset_all(void)
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001881{
1882 int i, j;
1883
Ingo Tuchscherer170387a2014-09-08 13:24:13 +02001884 for (i = 0; i < AP_DOMAINS; i++) {
1885 if (!ap_test_config_domain(i))
1886 continue;
1887 for (j = 0; j < AP_DEVICES; j++) {
1888 if (!ap_test_config_card_id(j))
1889 continue;
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001890 ap_reset_queue(AP_MKQID(j, i));
Ingo Tuchscherer170387a2014-09-08 13:24:13 +02001891 }
1892 }
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001893}
1894
1895static struct reset_call ap_reset_call = {
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001896 .fn = ap_reset_all,
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001897};
1898
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001899/**
Felix Beck1749a812008-04-17 07:46:28 +02001900 * ap_module_init(): The module initialization code.
1901 *
1902 * Initializes the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001903 */
1904int __init ap_module_init(void)
1905{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001906 int max_domain_id;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001907 int rc, i;
1908
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001909 if (ap_instructions_available() != 0) {
1910 pr_warn("The hardware system does not support AP instructions\n");
1911 return -ENODEV;
1912 }
1913
1914 /* Get AP configuration data if available */
1915 ap_init_configuration();
1916
1917 if (ap_configuration)
1918 max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1);
1919 else
1920 max_domain_id = 15;
1921 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1922 pr_warn("%d is not a valid cryptographic domain\n",
1923 ap_domain_index);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001924 return -EINVAL;
1925 }
Felix Beck5314af62009-09-22 22:58:51 +02001926 /* In resume callback we need to know if the user had set the domain.
1927 * If so, we can not just reset it.
1928 */
1929 if (ap_domain_index >= 0)
1930 user_set_domain = 1;
1931
Felix Beckcb17a632008-12-25 13:38:41 +01001932 if (ap_interrupts_available()) {
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001933 rc = register_adapter_interrupt(&ap_airq);
1934 ap_airq_flag = (rc == 0);
Felix Beckcb17a632008-12-25 13:38:41 +01001935 }
1936
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001937 register_reset_call(&ap_reset_call);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001938
1939 /* Create /sys/bus/ap. */
1940 rc = bus_register(&ap_bus_type);
1941 if (rc)
1942 goto out;
1943 for (i = 0; ap_bus_attrs[i]; i++) {
1944 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1945 if (rc)
1946 goto out_bus;
1947 }
1948
1949 /* Create /sys/devices/ap. */
Mark McLoughlin035da162008-12-15 12:58:29 +00001950 ap_root_device = root_device_register("ap");
Thomas Meyerba8da212013-06-01 11:51:13 +02001951 rc = PTR_RET(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001952 if (rc)
1953 goto out_bus;
1954
1955 ap_work_queue = create_singlethread_workqueue("kapwork");
1956 if (!ap_work_queue) {
1957 rc = -ENOMEM;
1958 goto out_root;
1959 }
1960
1961 if (ap_select_domain() == 0)
1962 ap_scan_bus(NULL);
1963
Felix Beck1749a812008-04-17 07:46:28 +02001964 /* Setup the AP bus rescan timer. */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001965 setup_timer(&ap_config_timer, ap_config_timeout, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001966 add_timer(&ap_config_timer);
1967
Felix Beckfe137232008-07-14 09:59:08 +02001968 /* Setup the high resultion poll timer.
1969 * If we are running under z/VM adjust polling to z/VM polling rate.
1970 */
1971 if (MACHINE_IS_VM)
1972 poll_timeout = 1500000;
Felix Beck93521312009-12-07 12:52:00 +01001973 spin_lock_init(&ap_poll_timer_lock);
Felix Beckfe137232008-07-14 09:59:08 +02001974 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1975 ap_poll_timer.function = ap_poll_timeout;
1976
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001977 /* Start the low priority AP bus poll thread. */
1978 if (ap_thread_flag) {
1979 rc = ap_poll_thread_start();
1980 if (rc)
1981 goto out_work;
1982 }
1983
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001984 rc = register_pm_notifier(&ap_power_notifier);
1985 if (rc)
1986 goto out_pm;
1987
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001988 return 0;
1989
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001990out_pm:
1991 ap_poll_thread_stop();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001992out_work:
1993 del_timer_sync(&ap_config_timer);
Felix Beckfe137232008-07-14 09:59:08 +02001994 hrtimer_cancel(&ap_poll_timer);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001995 destroy_workqueue(ap_work_queue);
1996out_root:
Mark McLoughlin035da162008-12-15 12:58:29 +00001997 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001998out_bus:
1999 while (i--)
2000 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
2001 bus_unregister(&ap_bus_type);
2002out:
Ralph Wuerthner85eca852006-12-08 15:54:07 +01002003 unregister_reset_call(&ap_reset_call);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02002004 if (ap_using_interrupts())
2005 unregister_adapter_interrupt(&ap_airq);
Martin Schwidefsky889875a2015-06-26 16:55:35 +02002006 kfree(ap_configuration);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002007 return rc;
2008}
2009
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002010/**
Felix Beck1749a812008-04-17 07:46:28 +02002011 * ap_modules_exit(): The module termination code
2012 *
2013 * Terminates the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002014 */
2015void ap_module_exit(void)
2016{
2017 int i;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002018
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01002019 ap_reset_domain();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002020 ap_poll_thread_stop();
2021 del_timer_sync(&ap_config_timer);
Felix Beckfe137232008-07-14 09:59:08 +02002022 hrtimer_cancel(&ap_poll_timer);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002023 destroy_workqueue(ap_work_queue);
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01002024 tasklet_kill(&ap_tasklet);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02002025 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002026 for (i = 0; ap_bus_attrs[i]; i++)
2027 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02002028 unregister_pm_notifier(&ap_power_notifier);
Ingo Tuchschererf60b8d42015-04-30 15:36:48 +02002029 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002030 bus_unregister(&ap_bus_type);
Martin Schwidefsky889875a2015-06-26 16:55:35 +02002031 kfree(ap_configuration);
Ralph Wuerthner85eca852006-12-08 15:54:07 +01002032 unregister_reset_call(&ap_reset_call);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02002033 if (ap_using_interrupts())
2034 unregister_adapter_interrupt(&ap_airq);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002035}
2036
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002037module_init(ap_module_init);
2038module_exit(ap_module_exit);