blob: 78c99ae42b1f9081ec3c0a165bb6a494dbdb6e51 [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>
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020049#include <linux/mod_devicetable.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020050
51#include "ap_bus.h"
Martin Schwidefsky0db78552016-09-21 12:48:54 +020052#include "ap_asm.h"
Martin Schwidefsky1534c382006-09-20 15:58:25 +020053
Felix Beck1749a812008-04-17 07:46:28 +020054/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +020055 * Module description.
56 */
57MODULE_AUTHOR("IBM Corporation");
Holger Dengler75014552012-08-28 16:41:50 +020058MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
59 "Copyright IBM Corp. 2006, 2012");
Martin Schwidefsky1534c382006-09-20 15:58:25 +020060MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -080061MODULE_ALIAS_CRYPTO("z90crypt");
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 parameter
65 */
66int ap_domain_index = -1; /* Adjunct Processor Domain Index */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +020067static DEFINE_SPINLOCK(ap_domain_lock);
Michael Veigelc1a42f42014-04-14 14:28:27 +020068module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020069MODULE_PARM_DESC(domain, "domain index for ap devices");
70EXPORT_SYMBOL(ap_domain_index);
71
Felix Beckb90b34c2008-02-09 18:24:30 +010072static int ap_thread_flag = 0;
Michael Veigelc1a42f42014-04-14 14:28:27 +020073module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
Felix Beckb90b34c2008-02-09 18:24:30 +010074MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
Martin Schwidefsky1534c382006-09-20 15:58:25 +020075
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020076static struct device *ap_root_device;
77
78DEFINE_SPINLOCK(ap_list_lock);
79LIST_HEAD(ap_card_list);
80
Holger Dengler75014552012-08-28 16:41:50 +020081static struct ap_config_info *ap_configuration;
Sascha Silbee3877532015-10-27 18:29:52 +010082static bool initialised;
Martin Schwidefsky1534c382006-09-20 15:58:25 +020083
Felix Beck1749a812008-04-17 07:46:28 +020084/*
Martin Schwidefsky8139b892015-07-27 12:47:40 +020085 * Workqueue timer for bus rescan.
Martin Schwidefsky1534c382006-09-20 15:58:25 +020086 */
Martin Schwidefsky1534c382006-09-20 15:58:25 +020087static struct timer_list ap_config_timer;
88static int ap_config_time = AP_CONFIG_TIME;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +020089static void ap_scan_bus(struct work_struct *);
Martin Schwidefsky8139b892015-07-27 12:47:40 +020090static DECLARE_WORK(ap_scan_work, ap_scan_bus);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020091
Felix Beck1749a812008-04-17 07:46:28 +020092/*
Felix Beckcb17a632008-12-25 13:38:41 +010093 * Tasklet & timer for AP request polling and interrupts
Martin Schwidefsky1534c382006-09-20 15:58:25 +020094 */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +020095static void ap_tasklet_fn(unsigned long);
96static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020097static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
98static struct task_struct *ap_poll_kthread = NULL;
99static DEFINE_MUTEX(ap_poll_thread_mutex);
Felix Beck93521312009-12-07 12:52:00 +0100100static DEFINE_SPINLOCK(ap_poll_timer_lock);
Felix Beckfe137232008-07-14 09:59:08 +0200101static struct hrtimer ap_poll_timer;
102/* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
103 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
104static unsigned long long poll_timeout = 250000;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200105
Felix Beck772f5472009-06-22 12:08:16 +0200106/* Suspend flag */
107static int ap_suspend_flag;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200108/* Maximum domain id */
109static int ap_max_domain_id;
Felix Beck5314af62009-09-22 22:58:51 +0200110/* Flag to check if domain was set through module parameter domain=. This is
111 * important when supsend and resume is done in a z/VM environment where the
112 * domain might change. */
113static int user_set_domain = 0;
Felix Beck772f5472009-06-22 12:08:16 +0200114static struct bus_type ap_bus_type;
115
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200116/* Adapter interrupt definitions */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200117static void ap_interrupt_handler(struct airq_struct *airq);
118
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200119static int ap_airq_flag;
120
121static struct airq_struct ap_airq = {
122 .handler = ap_interrupt_handler,
123 .isc = AP_ISC,
124};
125
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200126/**
Felix Beckcb17a632008-12-25 13:38:41 +0100127 * ap_using_interrupts() - Returns non-zero if interrupt support is
128 * available.
129 */
130static inline int ap_using_interrupts(void)
131{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200132 return ap_airq_flag;
Felix Beckcb17a632008-12-25 13:38:41 +0100133}
134
135/**
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200136 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
137 *
138 * Returns the address of the local-summary-indicator of the adapter
139 * interrupt handler for AP, or NULL if adapter interrupts are not
140 * available.
141 */
142void *ap_airq_ptr(void)
143{
144 if (ap_using_interrupts())
145 return ap_airq.lsi_ptr;
146 return NULL;
147}
148
149/**
Felix Beckcb17a632008-12-25 13:38:41 +0100150 * ap_interrupts_available(): Test if AP interrupts are available.
151 *
152 * Returns 1 if AP interrupts are available.
153 */
154static int ap_interrupts_available(void)
155{
Heiko Carstens86cd7412015-02-14 11:23:21 +0100156 return test_facility(65);
Felix Beckcb17a632008-12-25 13:38:41 +0100157}
158
159/**
Holger Dengler75014552012-08-28 16:41:50 +0200160 * ap_configuration_available(): Test if AP configuration
161 * information is available.
162 *
163 * Returns 1 if AP configuration information is available.
164 */
165static int ap_configuration_available(void)
166{
Heiko Carstens86cd7412015-02-14 11:23:21 +0100167 return test_facility(12);
Holger Dengler75014552012-08-28 16:41:50 +0200168}
169
170/**
Felix Beck1749a812008-04-17 07:46:28 +0200171 * ap_test_queue(): Test adjunct processor queue.
172 * @qid: The AP queue number
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200173 * @info: Pointer to queue descriptor
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200174 *
Felix Beck1749a812008-04-17 07:46:28 +0200175 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200176 */
177static inline struct ap_queue_status
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200178ap_test_queue(ap_qid_t qid, unsigned long *info)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200179{
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200180 if (test_facility(15))
Heiko Carstens11d37672016-06-20 14:00:27 +0200181 qid |= 1UL << 23; /* set APFT T bit*/
Martin Schwidefsky0db78552016-09-21 12:48:54 +0200182 return ap_tapq(qid, info);
Holger Dengler75014552012-08-28 16:41:50 +0200183}
Holger Dengler75014552012-08-28 16:41:50 +0200184
Heiko Carstens11d37672016-06-20 14:00:27 +0200185static inline int ap_query_configuration(void)
186{
187 if (!ap_configuration)
188 return -EOPNOTSUPP;
Martin Schwidefsky0db78552016-09-21 12:48:54 +0200189 return ap_qci(ap_configuration);
Heiko Carstens11d37672016-06-20 14:00:27 +0200190}
191
Holger Dengler6bed05b2011-07-24 10:48:25 +0200192/**
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200193 * ap_init_configuration(): Allocate and query configuration array.
194 */
195static void ap_init_configuration(void)
196{
197 if (!ap_configuration_available())
198 return;
199
200 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
201 if (!ap_configuration)
202 return;
203 if (ap_query_configuration() != 0) {
204 kfree(ap_configuration);
205 ap_configuration = NULL;
206 return;
207 }
208}
209
210/*
211 * ap_test_config(): helper function to extract the nrth bit
212 * within the unsigned int array field.
213 */
214static inline int ap_test_config(unsigned int *field, unsigned int nr)
215{
216 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
217}
218
219/*
220 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
221 * @id AP card ID
222 *
223 * Returns 0 if the card is not configured
224 * 1 if the card is configured or
225 * if the configuration information is not available
226 */
227static inline int ap_test_config_card_id(unsigned int id)
228{
229 if (!ap_configuration) /* QCI not supported */
230 return 1;
231 return ap_test_config(ap_configuration->apm, id);
232}
233
234/*
235 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
236 * @domain AP usage domain ID
237 *
238 * Returns 0 if the usage domain is not configured
239 * 1 if the usage domain is configured or
240 * if the configuration information is not available
241 */
242static inline int ap_test_config_domain(unsigned int domain)
243{
244 if (!ap_configuration) /* QCI not supported */
245 return domain < 16;
246 return ap_test_config(ap_configuration->aqm, domain);
247}
248
249/**
Felix Beck1749a812008-04-17 07:46:28 +0200250 * ap_query_queue(): Check if an AP queue is available.
251 * @qid: The AP queue number
252 * @queue_depth: Pointer to queue depth value
253 * @device_type: Pointer to device type value
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200254 * @facilities: Pointer to facility indicator
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200255 */
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200256static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
257 unsigned int *facilities)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200258{
259 struct ap_queue_status status;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200260 unsigned long info;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200261 int nd;
262
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200263 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200264 return -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200265
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200266 status = ap_test_queue(qid, &info);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200267 switch (status.response_code) {
268 case AP_RESPONSE_NORMAL:
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200269 *queue_depth = (int)(info & 0xff);
270 *device_type = (int)((info >> 24) & 0xff);
271 *facilities = (unsigned int)(info >> 32);
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200272 /* Update maximum domain id */
273 nd = (info >> 16) & 0xff;
Ingo Tuchschererc1c13682016-11-02 10:23:24 +0100274 /* if N bit is available, z13 and newer */
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200275 if ((info & (1UL << 57)) && nd > 0)
276 ap_max_domain_id = nd;
Ingo Tuchschererc1c13682016-11-02 10:23:24 +0100277 else /* older machine types */
278 ap_max_domain_id = 15;
Harald Freudenberger14878422016-10-27 08:57:39 +0200279 switch (*device_type) {
280 /* For CEX2 and CEX3 the available functions
281 * are not refrected by the facilities bits.
282 * Instead it is coded into the type. So here
283 * modify the function bits based on the type.
284 */
285 case AP_DEVICE_TYPE_CEX2A:
286 case AP_DEVICE_TYPE_CEX3A:
287 *facilities |= 0x08000000;
288 break;
289 case AP_DEVICE_TYPE_CEX2C:
290 case AP_DEVICE_TYPE_CEX3C:
291 *facilities |= 0x10000000;
292 break;
293 default:
294 break;
295 }
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200296 return 0;
297 case AP_RESPONSE_Q_NOT_AVAIL:
298 case AP_RESPONSE_DECONFIGURED:
299 case AP_RESPONSE_CHECKSTOPPED:
300 case AP_RESPONSE_INVALID_ADDRESS:
301 return -ENODEV;
302 case AP_RESPONSE_RESET_IN_PROGRESS:
303 case AP_RESPONSE_OTHERWISE_CHANGED:
304 case AP_RESPONSE_BUSY:
305 return -EBUSY;
306 default:
307 BUG();
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200308 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200309}
310
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200311void ap_wait(enum ap_wait wait)
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200312{
313 ktime_t hr_time;
314
315 switch (wait) {
316 case AP_WAIT_AGAIN:
317 case AP_WAIT_INTERRUPT:
318 if (ap_using_interrupts())
319 break;
320 if (ap_poll_kthread) {
321 wake_up(&ap_poll_wait);
322 break;
323 }
324 /* Fall through */
325 case AP_WAIT_TIMEOUT:
326 spin_lock_bh(&ap_poll_timer_lock);
327 if (!hrtimer_is_queued(&ap_poll_timer)) {
328 hr_time = ktime_set(0, poll_timeout);
329 hrtimer_forward_now(&ap_poll_timer, hr_time);
330 hrtimer_restart(&ap_poll_timer);
331 }
332 spin_unlock_bh(&ap_poll_timer_lock);
333 break;
334 case AP_WAIT_NONE:
335 default:
336 break;
337 }
338}
339
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200340/**
341 * ap_request_timeout(): Handling of request timeouts
342 * @data: Holds the AP device.
343 *
344 * Handles request timeouts.
345 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200346void ap_request_timeout(unsigned long data)
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200347{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200348 struct ap_queue *aq = (struct ap_queue *) data;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200349
350 if (ap_suspend_flag)
351 return;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200352 spin_lock_bh(&aq->lock);
353 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
354 spin_unlock_bh(&aq->lock);
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200355}
356
357/**
358 * ap_poll_timeout(): AP receive polling for finished AP requests.
359 * @unused: Unused pointer.
360 *
361 * Schedules the AP tasklet using a high resolution timer.
362 */
363static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
364{
365 if (!ap_suspend_flag)
366 tasklet_schedule(&ap_tasklet);
367 return HRTIMER_NORESTART;
368}
369
370/**
371 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
372 * @airq: pointer to adapter interrupt descriptor
373 */
374static void ap_interrupt_handler(struct airq_struct *airq)
375{
376 inc_irq_stat(IRQIO_APB);
377 if (!ap_suspend_flag)
378 tasklet_schedule(&ap_tasklet);
379}
380
381/**
382 * ap_tasklet_fn(): Tasklet to poll all AP devices.
383 * @dummy: Unused variable
384 *
385 * Poll all AP devices on the bus.
386 */
387static void ap_tasklet_fn(unsigned long dummy)
388{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200389 struct ap_card *ac;
390 struct ap_queue *aq;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200391 enum ap_wait wait = AP_WAIT_NONE;
392
393 /* Reset the indicator if interrupts are used. Thus new interrupts can
394 * be received. Doing it in the beginning of the tasklet is therefor
395 * important that no requests on any AP get lost.
396 */
397 if (ap_using_interrupts())
398 xchg(ap_airq.lsi_ptr, 0);
399
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200400 spin_lock_bh(&ap_list_lock);
401 for_each_ap_card(ac) {
402 for_each_ap_queue(aq, ac) {
403 spin_lock_bh(&aq->lock);
404 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
405 spin_unlock_bh(&aq->lock);
406 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200407 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200408 spin_unlock_bh(&ap_list_lock);
409
410 ap_wait(wait);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200411}
412
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200413static int ap_pending_requests(void)
414{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200415 struct ap_card *ac;
416 struct ap_queue *aq;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200417
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200418 spin_lock_bh(&ap_list_lock);
419 for_each_ap_card(ac) {
420 for_each_ap_queue(aq, ac) {
421 if (aq->queue_count == 0)
422 continue;
423 spin_unlock_bh(&ap_list_lock);
424 return 1;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200425 }
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200426 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200427 spin_unlock_bh(&ap_list_lock);
428 return 0;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200429}
430
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200431/**
432 * ap_poll_thread(): Thread that polls for finished requests.
433 * @data: Unused pointer
434 *
435 * AP bus poll thread. The purpose of this thread is to poll for
436 * finished requests in a loop if there is a "free" cpu - that is
437 * a cpu that doesn't have anything better to do. The polling stops
438 * as soon as there is another task or if all messages have been
439 * delivered.
440 */
441static int ap_poll_thread(void *data)
442{
443 DECLARE_WAITQUEUE(wait, current);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200444
445 set_user_nice(current, MAX_NICE);
446 set_freezable();
447 while (!kthread_should_stop()) {
448 add_wait_queue(&ap_poll_wait, &wait);
449 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200450 if (ap_suspend_flag || !ap_pending_requests()) {
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200451 schedule();
452 try_to_freeze();
453 }
454 set_current_state(TASK_RUNNING);
455 remove_wait_queue(&ap_poll_wait, &wait);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200456 if (need_resched()) {
457 schedule();
458 try_to_freeze();
459 continue;
460 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200461 ap_tasklet_fn(0);
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200462 }
463
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200464 return 0;
465}
466
467static int ap_poll_thread_start(void)
468{
469 int rc;
470
471 if (ap_using_interrupts() || ap_poll_kthread)
472 return 0;
473 mutex_lock(&ap_poll_thread_mutex);
474 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
475 rc = PTR_RET(ap_poll_kthread);
476 if (rc)
477 ap_poll_kthread = NULL;
478 mutex_unlock(&ap_poll_thread_mutex);
479 return rc;
480}
481
482static void ap_poll_thread_stop(void)
483{
484 if (!ap_poll_kthread)
485 return;
486 mutex_lock(&ap_poll_thread_mutex);
487 kthread_stop(ap_poll_kthread);
488 ap_poll_kthread = NULL;
489 mutex_unlock(&ap_poll_thread_mutex);
490}
491
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200492#define is_card_dev(x) ((x)->parent == ap_root_device)
493#define is_queue_dev(x) ((x)->parent != ap_root_device)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200494
495/**
Felix Beck1749a812008-04-17 07:46:28 +0200496 * ap_bus_match()
497 * @dev: Pointer to device
498 * @drv: Pointer to device_driver
499 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200500 * AP bus driver registration/unregistration.
501 */
502static int ap_bus_match(struct device *dev, struct device_driver *drv)
503{
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200504 struct ap_driver *ap_drv = to_ap_drv(drv);
505 struct ap_device_id *id;
506
Felix Beck1749a812008-04-17 07:46:28 +0200507 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200508 * Compare device type of the device with the list of
509 * supported types of the device_driver.
510 */
511 for (id = ap_drv->ids; id->match_flags; id++) {
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200512 if (is_card_dev(dev) &&
513 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
514 id->dev_type == to_ap_dev(dev)->device_type)
515 return 1;
516 if (is_queue_dev(dev) &&
517 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
518 id->dev_type == to_ap_dev(dev)->device_type)
519 return 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200520 }
521 return 0;
522}
523
524/**
Felix Beck1749a812008-04-17 07:46:28 +0200525 * ap_uevent(): Uevent function for AP devices.
526 * @dev: Pointer to device
527 * @env: Pointer to kobj_uevent_env
528 *
529 * It sets up a single environment variable DEV_TYPE which contains the
530 * hardware device type.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200531 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200532static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200533{
534 struct ap_device *ap_dev = to_ap_dev(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200535 int retval = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200536
537 if (!ap_dev)
538 return -ENODEV;
539
540 /* Set up DEV_TYPE environment variable. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200541 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700542 if (retval)
543 return retval;
544
Cornelia Huck66a4263b2006-12-04 15:40:10 +0100545 /* Add MODALIAS= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200546 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700547
Eric Rannaudbf624562007-03-30 22:23:12 -0700548 return retval;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200549}
550
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100551static int ap_dev_suspend(struct device *dev)
Felix Beck772f5472009-06-22 12:08:16 +0200552{
553 struct ap_device *ap_dev = to_ap_dev(dev);
Felix Beck772f5472009-06-22 12:08:16 +0200554
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200555 if (ap_dev->drv && ap_dev->drv->suspend)
556 ap_dev->drv->suspend(ap_dev);
557 return 0;
558}
559
560static int ap_dev_resume(struct device *dev)
561{
562 struct ap_device *ap_dev = to_ap_dev(dev);
563
564 if (ap_dev->drv && ap_dev->drv->resume)
565 ap_dev->drv->resume(ap_dev);
Felix Beck772f5472009-06-22 12:08:16 +0200566 return 0;
567}
568
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200569static void ap_bus_suspend(void)
570{
571 ap_suspend_flag = 1;
572 /*
573 * Disable scanning for devices, thus we do not want to scan
574 * for them after removing.
575 */
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200576 flush_work(&ap_scan_work);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200577 tasklet_disable(&ap_tasklet);
578}
579
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200580static int __ap_card_devices_unregister(struct device *dev, void *dummy)
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200581{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200582 if (is_card_dev(dev))
583 device_unregister(dev);
584 return 0;
585}
586
587static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
588{
589 if (is_queue_dev(dev))
590 device_unregister(dev);
591 return 0;
592}
593
594static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
595{
596 if (is_queue_dev(dev) &&
597 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
598 device_unregister(dev);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200599 return 0;
600}
601
602static void ap_bus_resume(void)
603{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200604 int rc;
Felix Beck772f5472009-06-22 12:08:16 +0200605
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200606 /* remove all queue devices */
607 bus_for_each_dev(&ap_bus_type, NULL, NULL,
608 __ap_queue_devices_unregister);
609 /* remove all card devices */
610 bus_for_each_dev(&ap_bus_type, NULL, NULL,
611 __ap_card_devices_unregister);
612
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200613 /* Reset thin interrupt setting */
614 if (ap_interrupts_available() && !ap_using_interrupts()) {
615 rc = register_adapter_interrupt(&ap_airq);
616 ap_airq_flag = (rc == 0);
Felix Beck5314af62009-09-22 22:58:51 +0200617 }
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200618 if (!ap_interrupts_available() && ap_using_interrupts()) {
619 unregister_adapter_interrupt(&ap_airq);
620 ap_airq_flag = 0;
621 }
622 /* Reset domain */
623 if (!user_set_domain)
624 ap_domain_index = -1;
625 /* Get things going again */
626 ap_suspend_flag = 0;
627 if (ap_airq_flag)
628 xchg(ap_airq.lsi_ptr, 0);
629 tasklet_enable(&ap_tasklet);
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200630 queue_work(system_long_wq, &ap_scan_work);
Felix Beck772f5472009-06-22 12:08:16 +0200631}
632
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200633static int ap_power_event(struct notifier_block *this, unsigned long event,
634 void *ptr)
635{
636 switch (event) {
637 case PM_HIBERNATION_PREPARE:
638 case PM_SUSPEND_PREPARE:
639 ap_bus_suspend();
640 break;
641 case PM_POST_HIBERNATION:
642 case PM_POST_SUSPEND:
643 ap_bus_resume();
644 break;
645 default:
646 break;
647 }
648 return NOTIFY_DONE;
649}
650static struct notifier_block ap_power_notifier = {
651 .notifier_call = ap_power_event,
652};
653
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200654static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100655
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200656static struct bus_type ap_bus_type = {
657 .name = "ap",
658 .match = &ap_bus_match,
659 .uevent = &ap_uevent,
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100660 .pm = &ap_bus_pm_ops,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200661};
662
663static int ap_device_probe(struct device *dev)
664{
665 struct ap_device *ap_dev = to_ap_dev(dev);
666 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
667 int rc;
668
669 ap_dev->drv = ap_drv;
670 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200671 if (rc)
672 ap_dev->drv = NULL;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200673 return rc;
674}
675
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200676static int ap_device_remove(struct device *dev)
677{
678 struct ap_device *ap_dev = to_ap_dev(dev);
679 struct ap_driver *ap_drv = ap_dev->drv;
680
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200681 spin_lock_bh(&ap_list_lock);
682 if (is_card_dev(dev))
683 list_del_init(&to_ap_card(dev)->list);
684 else
685 list_del_init(&to_ap_queue(dev)->list);
686 spin_unlock_bh(&ap_list_lock);
Ralph Wuerthnerfaa582c2008-03-05 12:37:13 +0100687 if (ap_drv->remove)
688 ap_drv->remove(ap_dev);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200689 return 0;
690}
691
692int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
693 char *name)
694{
695 struct device_driver *drv = &ap_drv->driver;
696
Sascha Silbee3877532015-10-27 18:29:52 +0100697 if (!initialised)
698 return -ENODEV;
699
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200700 drv->bus = &ap_bus_type;
701 drv->probe = ap_device_probe;
702 drv->remove = ap_device_remove;
703 drv->owner = owner;
704 drv->name = name;
705 return driver_register(drv);
706}
707EXPORT_SYMBOL(ap_driver_register);
708
709void ap_driver_unregister(struct ap_driver *ap_drv)
710{
711 driver_unregister(&ap_drv->driver);
712}
713EXPORT_SYMBOL(ap_driver_unregister);
714
Holger Denglerdabecb22012-09-10 21:34:26 +0200715void ap_bus_force_rescan(void)
716{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200717 if (ap_suspend_flag)
718 return;
Ingo Tuchscherer56bbe682013-04-12 17:52:08 +0200719 /* processing a asynchronous bus rescan */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200720 del_timer(&ap_config_timer);
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200721 queue_work(system_long_wq, &ap_scan_work);
722 flush_work(&ap_scan_work);
Holger Denglerdabecb22012-09-10 21:34:26 +0200723}
724EXPORT_SYMBOL(ap_bus_force_rescan);
725
Felix Beck1749a812008-04-17 07:46:28 +0200726/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200727 * AP bus attributes.
728 */
729static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
730{
731 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
732}
733
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200734static ssize_t ap_domain_store(struct bus_type *bus,
735 const char *buf, size_t count)
736{
737 int domain;
738
739 if (sscanf(buf, "%i\n", &domain) != 1 ||
740 domain < 0 || domain > ap_max_domain_id)
741 return -EINVAL;
742 spin_lock_bh(&ap_domain_lock);
743 ap_domain_index = domain;
744 spin_unlock_bh(&ap_domain_lock);
745 return count;
746}
747
748static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200749
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100750static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
751{
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200752 if (!ap_configuration) /* QCI not supported */
753 return snprintf(buf, PAGE_SIZE, "not supported\n");
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200754
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200755 return snprintf(buf, PAGE_SIZE,
756 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100757 ap_configuration->adm[0], ap_configuration->adm[1],
758 ap_configuration->adm[2], ap_configuration->adm[3],
759 ap_configuration->adm[4], ap_configuration->adm[5],
760 ap_configuration->adm[6], ap_configuration->adm[7]);
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100761}
762
763static BUS_ATTR(ap_control_domain_mask, 0444,
764 ap_control_domain_mask_show, NULL);
765
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200766static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
767{
768 if (!ap_configuration) /* QCI not supported */
769 return snprintf(buf, PAGE_SIZE, "not supported\n");
770
771 return snprintf(buf, PAGE_SIZE,
772 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
773 ap_configuration->aqm[0], ap_configuration->aqm[1],
774 ap_configuration->aqm[2], ap_configuration->aqm[3],
775 ap_configuration->aqm[4], ap_configuration->aqm[5],
776 ap_configuration->aqm[6], ap_configuration->aqm[7]);
777}
778
779static BUS_ATTR(ap_usage_domain_mask, 0444,
780 ap_usage_domain_mask_show, NULL);
781
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200782static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
783{
784 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
785}
786
Felix Beckcb17a632008-12-25 13:38:41 +0100787static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
788{
789 return snprintf(buf, PAGE_SIZE, "%d\n",
790 ap_using_interrupts() ? 1 : 0);
791}
792
793static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
794
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200795static ssize_t ap_config_time_store(struct bus_type *bus,
796 const char *buf, size_t count)
797{
798 int time;
799
800 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
801 return -EINVAL;
802 ap_config_time = time;
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200803 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200804 return count;
805}
806
807static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
808
809static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
810{
811 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
812}
813
814static ssize_t ap_poll_thread_store(struct bus_type *bus,
815 const char *buf, size_t count)
816{
817 int flag, rc;
818
819 if (sscanf(buf, "%d\n", &flag) != 1)
820 return -EINVAL;
821 if (flag) {
822 rc = ap_poll_thread_start();
823 if (rc)
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200824 count = rc;
825 } else
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200826 ap_poll_thread_stop();
827 return count;
828}
829
830static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
831
Felix Beckfe137232008-07-14 09:59:08 +0200832static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
833{
834 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
835}
836
837static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
838 size_t count)
839{
840 unsigned long long time;
841 ktime_t hr_time;
842
843 /* 120 seconds = maximum poll interval */
Felix Beckcb17a632008-12-25 13:38:41 +0100844 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
845 time > 120000000000ULL)
Felix Beckfe137232008-07-14 09:59:08 +0200846 return -EINVAL;
847 poll_timeout = time;
848 hr_time = ktime_set(0, poll_timeout);
849
Ingo Tuchscherer8cc2af72015-04-28 10:31:44 +0200850 spin_lock_bh(&ap_poll_timer_lock);
851 hrtimer_cancel(&ap_poll_timer);
852 hrtimer_set_expires(&ap_poll_timer, hr_time);
853 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
854 spin_unlock_bh(&ap_poll_timer_lock);
855
Felix Beckfe137232008-07-14 09:59:08 +0200856 return count;
857}
858
859static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
860
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100861static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
862{
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200863 int max_domain_id;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100864
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200865 if (ap_configuration)
866 max_domain_id = ap_max_domain_id ? : -1;
867 else
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100868 max_domain_id = 15;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100869 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
870}
871
872static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
873
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200874static struct bus_attribute *const ap_bus_attrs[] = {
875 &bus_attr_ap_domain,
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100876 &bus_attr_ap_control_domain_mask,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200877 &bus_attr_ap_usage_domain_mask,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200878 &bus_attr_config_time,
879 &bus_attr_poll_thread,
Felix Beckcb17a632008-12-25 13:38:41 +0100880 &bus_attr_ap_interrupts,
Felix Beckfe137232008-07-14 09:59:08 +0200881 &bus_attr_poll_timeout,
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100882 &bus_attr_ap_max_domain_id,
Felix Beckfe137232008-07-14 09:59:08 +0200883 NULL,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200884};
885
Holger Dengler75014552012-08-28 16:41:50 +0200886/**
Felix Beck1749a812008-04-17 07:46:28 +0200887 * ap_select_domain(): Select an AP domain.
888 *
889 * Pick one of the 16 AP domains.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200890 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100891static int ap_select_domain(void)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200892{
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200893 int count, max_count, best_domain;
894 struct ap_queue_status status;
895 int i, j;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200896
Felix Beck1749a812008-04-17 07:46:28 +0200897 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200898 * We want to use a single domain. Either the one specified with
899 * the "domain=" parameter or the domain with the maximum number
900 * of devices.
901 */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200902 spin_lock_bh(&ap_domain_lock);
903 if (ap_domain_index >= 0) {
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200904 /* Domain has already been selected. */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200905 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200906 return 0;
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200907 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200908 best_domain = -1;
909 max_count = 0;
910 for (i = 0; i < AP_DOMAINS; i++) {
Holger Dengler75014552012-08-28 16:41:50 +0200911 if (!ap_test_config_domain(i))
912 continue;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200913 count = 0;
914 for (j = 0; j < AP_DEVICES; j++) {
Holger Dengler75014552012-08-28 16:41:50 +0200915 if (!ap_test_config_card_id(j))
916 continue;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200917 status = ap_test_queue(AP_MKQID(j, i), NULL);
918 if (status.response_code != AP_RESPONSE_NORMAL)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200919 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;
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200929 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200930 return 0;
931 }
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200932 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200933 return -ENODEV;
934}
935
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200936/*
937 * helper function to be used with bus_find_dev
938 * matches for the card device with the given id
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200939 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200940static int __match_card_device_with_id(struct device *dev, void *data)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200941{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200942 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200943}
944
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200945/* helper function to be used with bus_find_dev
946 * matches for the queue device with a given qid
947 */
948static int __match_queue_device_with_qid(struct device *dev, void *data)
949{
950 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
951}
952
953/**
954 * ap_scan_bus(): Scan the AP bus for new devices
955 * Runs periodically, workqueue timer (ap_config_time)
956 */
Al Viro4927b3f2006-12-06 19:18:20 +0000957static void ap_scan_bus(struct work_struct *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200958{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200959 struct ap_queue *aq;
960 struct ap_card *ac;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200961 struct device *dev;
962 ap_qid_t qid;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200963 int depth = 0, type = 0;
964 unsigned int functions = 0;
965 int rc, id, dom, borked, domains;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200966
Holger Dengler75014552012-08-28 16:41:50 +0200967 ap_query_configuration();
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200968 if (ap_select_domain() != 0)
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200969 goto out;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200970
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200971 for (id = 0; id < AP_DEVICES; id++) {
972 /* check if device is registered */
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200973 dev = bus_find_device(&ap_bus_type, NULL,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200974 (void *)(long) id,
975 __match_card_device_with_id);
976 ac = dev ? to_ap_card(dev) : NULL;
977 if (!ap_test_config_card_id(id)) {
978 if (dev) {
979 /* Card device has been removed from
980 * configuration, remove the belonging
981 * queue devices.
982 */
983 bus_for_each_dev(&ap_bus_type, NULL,
984 (void *)(long) id,
985 __ap_queue_devices_with_id_unregister);
986 /* now remove the card device */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200987 device_unregister(dev);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200988 put_device(dev);
989 }
990 continue;
991 }
992 /* According to the configuration there should be a card
993 * device, so check if there is at least one valid queue
994 * and maybe create queue devices and the card device.
995 */
996 domains = 0;
997 for (dom = 0; dom < AP_DOMAINS; dom++) {
998 qid = AP_MKQID(id, dom);
999 dev = bus_find_device(&ap_bus_type, NULL,
1000 (void *)(long) qid,
1001 __match_queue_device_with_qid);
1002 aq = dev ? to_ap_queue(dev) : NULL;
1003 if (!ap_test_config_domain(dom)) {
1004 if (dev) {
1005 /* Queue device exists but has been
1006 * removed from configuration.
1007 */
1008 device_unregister(dev);
1009 put_device(dev);
1010 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001011 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001012 }
1013 rc = ap_query_queue(qid, &depth, &type, &functions);
1014 if (dev) {
1015 spin_lock_bh(&aq->lock);
1016 if (rc == -ENODEV ||
1017 /* adapter reconfiguration */
1018 (ac && ac->functions != functions))
1019 aq->state = AP_STATE_BORKED;
1020 borked = aq->state == AP_STATE_BORKED;
1021 spin_unlock_bh(&aq->lock);
1022 if (borked) /* Remove broken device */
1023 device_unregister(dev);
1024 put_device(dev);
1025 if (!borked) {
1026 domains++;
1027 continue;
1028 }
1029 }
1030 if (rc)
1031 continue;
1032 /* new queue device needed */
1033 if (!ac) {
1034 /* but first create the card device */
1035 ac = ap_card_create(id, depth,
1036 type, functions);
1037 if (!ac)
1038 continue;
1039 ac->ap_dev.device.bus = &ap_bus_type;
1040 ac->ap_dev.device.parent = ap_root_device;
1041 dev_set_name(&ac->ap_dev.device,
1042 "card%02x", id);
1043 /* Register card with AP bus */
1044 rc = device_register(&ac->ap_dev.device);
1045 if (rc) {
1046 put_device(&ac->ap_dev.device);
1047 ac = NULL;
1048 break;
1049 }
1050 /* get it and thus adjust reference counter */
1051 get_device(&ac->ap_dev.device);
1052 /* Add card device to card list */
1053 spin_lock_bh(&ap_list_lock);
1054 list_add(&ac->list, &ap_card_list);
1055 spin_unlock_bh(&ap_list_lock);
1056 }
1057 /* now create the new queue device */
1058 aq = ap_queue_create(qid, type);
1059 if (!aq)
1060 continue;
1061 aq->card = ac;
1062 aq->ap_dev.device.bus = &ap_bus_type;
1063 aq->ap_dev.device.parent = &ac->ap_dev.device;
1064 dev_set_name(&aq->ap_dev.device,
1065 "%02x.%04x", id, dom);
1066 /* Add queue device to card queue list */
1067 spin_lock_bh(&ap_list_lock);
1068 list_add(&aq->list, &ac->queues);
1069 spin_unlock_bh(&ap_list_lock);
1070 /* Start with a device reset */
1071 spin_lock_bh(&aq->lock);
1072 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
1073 spin_unlock_bh(&aq->lock);
1074 /* Register device */
1075 rc = device_register(&aq->ap_dev.device);
1076 if (rc) {
1077 spin_lock_bh(&ap_list_lock);
1078 list_del_init(&aq->list);
1079 spin_unlock_bh(&ap_list_lock);
1080 put_device(&aq->ap_dev.device);
1081 continue;
1082 }
1083 domains++;
1084 } /* end domain loop */
1085 if (ac) {
1086 /* remove card dev if there are no queue devices */
1087 if (!domains)
1088 device_unregister(&ac->ap_dev.device);
1089 put_device(&ac->ap_dev.device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001090 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001091 } /* end device loop */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001092out:
1093 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001094}
1095
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001096static void ap_config_timeout(unsigned long ptr)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001097{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001098 if (ap_suspend_flag)
1099 return;
Martin Schwidefsky8139b892015-07-27 12:47:40 +02001100 queue_work(system_long_wq, &ap_scan_work);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001101}
1102
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001103static void ap_reset_domain(void)
1104{
1105 int i;
1106
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001107 if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index))
1108 return;
1109 for (i = 0; i < AP_DEVICES; i++)
Martin Schwidefsky0db78552016-09-21 12:48:54 +02001110 ap_rapq(AP_MKQID(i, ap_domain_index));
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001111}
1112
1113static void ap_reset_all(void)
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001114{
1115 int i, j;
1116
Ingo Tuchscherer170387a2014-09-08 13:24:13 +02001117 for (i = 0; i < AP_DOMAINS; i++) {
1118 if (!ap_test_config_domain(i))
1119 continue;
1120 for (j = 0; j < AP_DEVICES; j++) {
1121 if (!ap_test_config_card_id(j))
1122 continue;
Martin Schwidefsky0db78552016-09-21 12:48:54 +02001123 ap_rapq(AP_MKQID(j, i));
Ingo Tuchscherer170387a2014-09-08 13:24:13 +02001124 }
1125 }
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001126}
1127
1128static struct reset_call ap_reset_call = {
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001129 .fn = ap_reset_all,
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001130};
1131
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001132/**
Felix Beck1749a812008-04-17 07:46:28 +02001133 * ap_module_init(): The module initialization code.
1134 *
1135 * Initializes the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001136 */
1137int __init ap_module_init(void)
1138{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001139 int max_domain_id;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001140 int rc, i;
1141
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001142 if (ap_instructions_available() != 0) {
1143 pr_warn("The hardware system does not support AP instructions\n");
1144 return -ENODEV;
1145 }
1146
1147 /* Get AP configuration data if available */
1148 ap_init_configuration();
1149
1150 if (ap_configuration)
1151 max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1);
1152 else
1153 max_domain_id = 15;
1154 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1155 pr_warn("%d is not a valid cryptographic domain\n",
1156 ap_domain_index);
Wei Yongjun33c388b2016-07-31 11:47:06 +02001157 rc = -EINVAL;
1158 goto out_free;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001159 }
Felix Beck5314af62009-09-22 22:58:51 +02001160 /* In resume callback we need to know if the user had set the domain.
1161 * If so, we can not just reset it.
1162 */
1163 if (ap_domain_index >= 0)
1164 user_set_domain = 1;
1165
Felix Beckcb17a632008-12-25 13:38:41 +01001166 if (ap_interrupts_available()) {
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001167 rc = register_adapter_interrupt(&ap_airq);
1168 ap_airq_flag = (rc == 0);
Felix Beckcb17a632008-12-25 13:38:41 +01001169 }
1170
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001171 register_reset_call(&ap_reset_call);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001172
1173 /* Create /sys/bus/ap. */
1174 rc = bus_register(&ap_bus_type);
1175 if (rc)
1176 goto out;
1177 for (i = 0; ap_bus_attrs[i]; i++) {
1178 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1179 if (rc)
1180 goto out_bus;
1181 }
1182
1183 /* Create /sys/devices/ap. */
Mark McLoughlin035da162008-12-15 12:58:29 +00001184 ap_root_device = root_device_register("ap");
Thomas Meyerba8da212013-06-01 11:51:13 +02001185 rc = PTR_RET(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001186 if (rc)
1187 goto out_bus;
1188
Felix Beck1749a812008-04-17 07:46:28 +02001189 /* Setup the AP bus rescan timer. */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001190 setup_timer(&ap_config_timer, ap_config_timeout, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001191
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001192 /*
1193 * Setup the high resultion poll timer.
Felix Beckfe137232008-07-14 09:59:08 +02001194 * If we are running under z/VM adjust polling to z/VM polling rate.
1195 */
1196 if (MACHINE_IS_VM)
1197 poll_timeout = 1500000;
Felix Beck93521312009-12-07 12:52:00 +01001198 spin_lock_init(&ap_poll_timer_lock);
Felix Beckfe137232008-07-14 09:59:08 +02001199 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1200 ap_poll_timer.function = ap_poll_timeout;
1201
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001202 /* Start the low priority AP bus poll thread. */
1203 if (ap_thread_flag) {
1204 rc = ap_poll_thread_start();
1205 if (rc)
1206 goto out_work;
1207 }
1208
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001209 rc = register_pm_notifier(&ap_power_notifier);
1210 if (rc)
1211 goto out_pm;
1212
Martin Schwidefsky8139b892015-07-27 12:47:40 +02001213 queue_work(system_long_wq, &ap_scan_work);
Sascha Silbee3877532015-10-27 18:29:52 +01001214 initialised = true;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001215
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001216 return 0;
1217
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001218out_pm:
1219 ap_poll_thread_stop();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001220out_work:
Felix Beckfe137232008-07-14 09:59:08 +02001221 hrtimer_cancel(&ap_poll_timer);
Mark McLoughlin035da162008-12-15 12:58:29 +00001222 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001223out_bus:
1224 while (i--)
1225 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1226 bus_unregister(&ap_bus_type);
1227out:
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001228 unregister_reset_call(&ap_reset_call);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001229 if (ap_using_interrupts())
1230 unregister_adapter_interrupt(&ap_airq);
Wei Yongjun33c388b2016-07-31 11:47:06 +02001231out_free:
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001232 kfree(ap_configuration);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001233 return rc;
1234}
1235
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001236/**
Felix Beck1749a812008-04-17 07:46:28 +02001237 * ap_modules_exit(): The module termination code
1238 *
1239 * Terminates the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001240 */
1241void ap_module_exit(void)
1242{
1243 int i;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001244
Sascha Silbee3877532015-10-27 18:29:52 +01001245 initialised = false;
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001246 ap_reset_domain();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001247 ap_poll_thread_stop();
1248 del_timer_sync(&ap_config_timer);
Felix Beckfe137232008-07-14 09:59:08 +02001249 hrtimer_cancel(&ap_poll_timer);
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001250 tasklet_kill(&ap_tasklet);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001251
1252 /* first remove queue devices */
1253 bus_for_each_dev(&ap_bus_type, NULL, NULL,
1254 __ap_queue_devices_unregister);
1255 /* now remove the card devices */
1256 bus_for_each_dev(&ap_bus_type, NULL, NULL,
1257 __ap_card_devices_unregister);
1258
1259 /* remove bus attributes */
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001260 for (i = 0; ap_bus_attrs[i]; i++)
1261 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001262 unregister_pm_notifier(&ap_power_notifier);
Ingo Tuchschererf60b8d42015-04-30 15:36:48 +02001263 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001264 bus_unregister(&ap_bus_type);
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001265 kfree(ap_configuration);
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001266 unregister_reset_call(&ap_reset_call);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001267 if (ap_using_interrupts())
1268 unregister_adapter_interrupt(&ap_airq);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001269}
1270
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001271module_init(ap_module_init);
1272module_exit(ap_module_exit);