blob: 5f0be20402726d7f6b9c0d4816cf32580077eb09 [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>
Paul Gortmaker50a0d462017-02-09 09:48:10 -050030#include <linux/moduleparam.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020031#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>
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010050#include <linux/debugfs.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020051
52#include "ap_bus.h"
Martin Schwidefsky0db78552016-09-21 12:48:54 +020053#include "ap_asm.h"
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010054#include "ap_debug.h"
Martin Schwidefsky1534c382006-09-20 15:58:25 +020055
Felix Beck1749a812008-04-17 07:46:28 +020056/*
Paul Gortmaker50a0d462017-02-09 09:48:10 -050057 * Module parameters; note though this file itself isn't modular.
Martin Schwidefsky1534c382006-09-20 15:58:25 +020058 */
59int ap_domain_index = -1; /* Adjunct Processor Domain Index */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +020060static DEFINE_SPINLOCK(ap_domain_lock);
Michael Veigelc1a42f42014-04-14 14:28:27 +020061module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020062MODULE_PARM_DESC(domain, "domain index for ap devices");
63EXPORT_SYMBOL(ap_domain_index);
64
Felix Beckb90b34c2008-02-09 18:24:30 +010065static int ap_thread_flag = 0;
Michael Veigelc1a42f42014-04-14 14:28:27 +020066module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
Felix Beckb90b34c2008-02-09 18:24:30 +010067MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
Martin Schwidefsky1534c382006-09-20 15:58:25 +020068
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020069static struct device *ap_root_device;
70
71DEFINE_SPINLOCK(ap_list_lock);
72LIST_HEAD(ap_card_list);
73
Holger Dengler75014552012-08-28 16:41:50 +020074static struct ap_config_info *ap_configuration;
Sascha Silbee3877532015-10-27 18:29:52 +010075static bool initialised;
Martin Schwidefsky1534c382006-09-20 15:58:25 +020076
Felix Beck1749a812008-04-17 07:46:28 +020077/*
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010078 * AP bus related debug feature things.
79 */
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010080debug_info_t *ap_dbf_info;
81
82/*
Martin Schwidefsky8139b892015-07-27 12:47:40 +020083 * Workqueue timer for bus rescan.
Martin Schwidefsky1534c382006-09-20 15:58:25 +020084 */
Martin Schwidefsky1534c382006-09-20 15:58:25 +020085static struct timer_list ap_config_timer;
86static int ap_config_time = AP_CONFIG_TIME;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +020087static void ap_scan_bus(struct work_struct *);
Martin Schwidefsky8139b892015-07-27 12:47:40 +020088static DECLARE_WORK(ap_scan_work, ap_scan_bus);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020089
Felix Beck1749a812008-04-17 07:46:28 +020090/*
Felix Beckcb17a632008-12-25 13:38:41 +010091 * Tasklet & timer for AP request polling and interrupts
Martin Schwidefsky1534c382006-09-20 15:58:25 +020092 */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +020093static void ap_tasklet_fn(unsigned long);
94static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020095static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
96static struct task_struct *ap_poll_kthread = NULL;
97static DEFINE_MUTEX(ap_poll_thread_mutex);
Felix Beck93521312009-12-07 12:52:00 +010098static DEFINE_SPINLOCK(ap_poll_timer_lock);
Felix Beckfe137232008-07-14 09:59:08 +020099static struct hrtimer ap_poll_timer;
100/* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
101 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
102static unsigned long long poll_timeout = 250000;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200103
Felix Beck772f5472009-06-22 12:08:16 +0200104/* Suspend flag */
105static int ap_suspend_flag;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200106/* Maximum domain id */
107static int ap_max_domain_id;
Felix Beck5314af62009-09-22 22:58:51 +0200108/* Flag to check if domain was set through module parameter domain=. This is
109 * important when supsend and resume is done in a z/VM environment where the
110 * domain might change. */
111static int user_set_domain = 0;
Felix Beck772f5472009-06-22 12:08:16 +0200112static struct bus_type ap_bus_type;
113
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200114/* Adapter interrupt definitions */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200115static void ap_interrupt_handler(struct airq_struct *airq);
116
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200117static int ap_airq_flag;
118
119static struct airq_struct ap_airq = {
120 .handler = ap_interrupt_handler,
121 .isc = AP_ISC,
122};
123
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200124/**
Felix Beckcb17a632008-12-25 13:38:41 +0100125 * ap_using_interrupts() - Returns non-zero if interrupt support is
126 * available.
127 */
128static inline int ap_using_interrupts(void)
129{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200130 return ap_airq_flag;
Felix Beckcb17a632008-12-25 13:38:41 +0100131}
132
133/**
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200134 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
135 *
136 * Returns the address of the local-summary-indicator of the adapter
137 * interrupt handler for AP, or NULL if adapter interrupts are not
138 * available.
139 */
140void *ap_airq_ptr(void)
141{
142 if (ap_using_interrupts())
143 return ap_airq.lsi_ptr;
144 return NULL;
145}
146
147/**
Felix Beckcb17a632008-12-25 13:38:41 +0100148 * ap_interrupts_available(): Test if AP interrupts are available.
149 *
150 * Returns 1 if AP interrupts are available.
151 */
152static int ap_interrupts_available(void)
153{
Heiko Carstens86cd7412015-02-14 11:23:21 +0100154 return test_facility(65);
Felix Beckcb17a632008-12-25 13:38:41 +0100155}
156
157/**
Holger Dengler75014552012-08-28 16:41:50 +0200158 * ap_configuration_available(): Test if AP configuration
159 * information is available.
160 *
161 * Returns 1 if AP configuration information is available.
162 */
163static int ap_configuration_available(void)
164{
Heiko Carstens86cd7412015-02-14 11:23:21 +0100165 return test_facility(12);
Holger Dengler75014552012-08-28 16:41:50 +0200166}
167
168/**
Tony Krowiake7fc5142016-11-08 07:09:13 +0100169 * ap_apft_available(): Test if AP facilities test (APFT)
170 * facility is available.
171 *
172 * Returns 1 if APFT is is available.
173 */
174static int ap_apft_available(void)
175{
176 return test_facility(15);
177}
178
179/**
Felix Beck1749a812008-04-17 07:46:28 +0200180 * ap_test_queue(): Test adjunct processor queue.
181 * @qid: The AP queue number
Tony Krowiake7fc5142016-11-08 07:09:13 +0100182 * @tbit: Test facilities bit
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200183 * @info: Pointer to queue descriptor
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200184 *
Felix Beck1749a812008-04-17 07:46:28 +0200185 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200186 */
Tony Krowiake7fc5142016-11-08 07:09:13 +0100187struct ap_queue_status ap_test_queue(ap_qid_t qid,
188 int tbit,
189 unsigned long *info)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200190{
Tony Krowiake7fc5142016-11-08 07:09:13 +0100191 if (tbit)
192 qid |= 1UL << 23; /* set T bit*/
Martin Schwidefsky0db78552016-09-21 12:48:54 +0200193 return ap_tapq(qid, info);
Holger Dengler75014552012-08-28 16:41:50 +0200194}
Tony Krowiake7fc5142016-11-08 07:09:13 +0100195EXPORT_SYMBOL(ap_test_queue);
Holger Dengler75014552012-08-28 16:41:50 +0200196
Harald Freudenberger050349b2016-11-08 11:54:28 +0100197/*
198 * ap_query_configuration(): Fetch cryptographic config info
199 *
200 * Returns the ap configuration info fetched via PQAP(QCI).
201 * On success 0 is returned, on failure a negative errno
202 * is returned, e.g. if the PQAP(QCI) instruction is not
203 * available, the return value will be -EOPNOTSUPP.
204 */
205int ap_query_configuration(struct ap_config_info *info)
Heiko Carstens11d37672016-06-20 14:00:27 +0200206{
Harald Freudenberger050349b2016-11-08 11:54:28 +0100207 if (!ap_configuration_available())
Heiko Carstens11d37672016-06-20 14:00:27 +0200208 return -EOPNOTSUPP;
Harald Freudenberger050349b2016-11-08 11:54:28 +0100209 if (!info)
210 return -EINVAL;
211 return ap_qci(info);
Heiko Carstens11d37672016-06-20 14:00:27 +0200212}
Harald Freudenberger050349b2016-11-08 11:54:28 +0100213EXPORT_SYMBOL(ap_query_configuration);
Heiko Carstens11d37672016-06-20 14:00:27 +0200214
Holger Dengler6bed05b2011-07-24 10:48:25 +0200215/**
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200216 * ap_init_configuration(): Allocate and query configuration array.
217 */
218static void ap_init_configuration(void)
219{
220 if (!ap_configuration_available())
221 return;
222
223 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
224 if (!ap_configuration)
225 return;
Harald Freudenberger050349b2016-11-08 11:54:28 +0100226 if (ap_query_configuration(ap_configuration) != 0) {
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200227 kfree(ap_configuration);
228 ap_configuration = NULL;
229 return;
230 }
231}
232
233/*
234 * ap_test_config(): helper function to extract the nrth bit
235 * within the unsigned int array field.
236 */
237static inline int ap_test_config(unsigned int *field, unsigned int nr)
238{
239 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
240}
241
242/*
243 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
244 * @id AP card ID
245 *
246 * Returns 0 if the card is not configured
247 * 1 if the card is configured or
248 * if the configuration information is not available
249 */
250static inline int ap_test_config_card_id(unsigned int id)
251{
252 if (!ap_configuration) /* QCI not supported */
253 return 1;
254 return ap_test_config(ap_configuration->apm, id);
255}
256
257/*
258 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
259 * @domain AP usage domain ID
260 *
261 * Returns 0 if the usage domain is not configured
262 * 1 if the usage domain is configured or
263 * if the configuration information is not available
264 */
265static inline int ap_test_config_domain(unsigned int domain)
266{
267 if (!ap_configuration) /* QCI not supported */
268 return domain < 16;
269 return ap_test_config(ap_configuration->aqm, domain);
270}
271
272/**
Felix Beck1749a812008-04-17 07:46:28 +0200273 * ap_query_queue(): Check if an AP queue is available.
274 * @qid: The AP queue number
275 * @queue_depth: Pointer to queue depth value
276 * @device_type: Pointer to device type value
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200277 * @facilities: Pointer to facility indicator
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200278 */
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200279static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
280 unsigned int *facilities)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200281{
282 struct ap_queue_status status;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200283 unsigned long info;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200284 int nd;
285
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200286 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200287 return -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200288
Tony Krowiake7fc5142016-11-08 07:09:13 +0100289 status = ap_test_queue(qid, ap_apft_available(), &info);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200290 switch (status.response_code) {
291 case AP_RESPONSE_NORMAL:
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200292 *queue_depth = (int)(info & 0xff);
293 *device_type = (int)((info >> 24) & 0xff);
294 *facilities = (unsigned int)(info >> 32);
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200295 /* Update maximum domain id */
296 nd = (info >> 16) & 0xff;
Ingo Tuchschererc1c13682016-11-02 10:23:24 +0100297 /* if N bit is available, z13 and newer */
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200298 if ((info & (1UL << 57)) && nd > 0)
299 ap_max_domain_id = nd;
Ingo Tuchschererc1c13682016-11-02 10:23:24 +0100300 else /* older machine types */
301 ap_max_domain_id = 15;
Harald Freudenberger14878422016-10-27 08:57:39 +0200302 switch (*device_type) {
303 /* For CEX2 and CEX3 the available functions
304 * are not refrected by the facilities bits.
305 * Instead it is coded into the type. So here
306 * modify the function bits based on the type.
307 */
308 case AP_DEVICE_TYPE_CEX2A:
309 case AP_DEVICE_TYPE_CEX3A:
310 *facilities |= 0x08000000;
311 break;
312 case AP_DEVICE_TYPE_CEX2C:
313 case AP_DEVICE_TYPE_CEX3C:
314 *facilities |= 0x10000000;
315 break;
316 default:
317 break;
318 }
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200319 return 0;
320 case AP_RESPONSE_Q_NOT_AVAIL:
321 case AP_RESPONSE_DECONFIGURED:
322 case AP_RESPONSE_CHECKSTOPPED:
323 case AP_RESPONSE_INVALID_ADDRESS:
324 return -ENODEV;
325 case AP_RESPONSE_RESET_IN_PROGRESS:
326 case AP_RESPONSE_OTHERWISE_CHANGED:
327 case AP_RESPONSE_BUSY:
328 return -EBUSY;
329 default:
330 BUG();
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200331 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200332}
333
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200334void ap_wait(enum ap_wait wait)
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200335{
336 ktime_t hr_time;
337
338 switch (wait) {
339 case AP_WAIT_AGAIN:
340 case AP_WAIT_INTERRUPT:
341 if (ap_using_interrupts())
342 break;
343 if (ap_poll_kthread) {
344 wake_up(&ap_poll_wait);
345 break;
346 }
347 /* Fall through */
348 case AP_WAIT_TIMEOUT:
349 spin_lock_bh(&ap_poll_timer_lock);
350 if (!hrtimer_is_queued(&ap_poll_timer)) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100351 hr_time = poll_timeout;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200352 hrtimer_forward_now(&ap_poll_timer, hr_time);
353 hrtimer_restart(&ap_poll_timer);
354 }
355 spin_unlock_bh(&ap_poll_timer_lock);
356 break;
357 case AP_WAIT_NONE:
358 default:
359 break;
360 }
361}
362
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200363/**
364 * ap_request_timeout(): Handling of request timeouts
365 * @data: Holds the AP device.
366 *
367 * Handles request timeouts.
368 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200369void ap_request_timeout(unsigned long data)
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200370{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200371 struct ap_queue *aq = (struct ap_queue *) data;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200372
373 if (ap_suspend_flag)
374 return;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200375 spin_lock_bh(&aq->lock);
376 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
377 spin_unlock_bh(&aq->lock);
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200378}
379
380/**
381 * ap_poll_timeout(): AP receive polling for finished AP requests.
382 * @unused: Unused pointer.
383 *
384 * Schedules the AP tasklet using a high resolution timer.
385 */
386static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
387{
388 if (!ap_suspend_flag)
389 tasklet_schedule(&ap_tasklet);
390 return HRTIMER_NORESTART;
391}
392
393/**
394 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
395 * @airq: pointer to adapter interrupt descriptor
396 */
397static void ap_interrupt_handler(struct airq_struct *airq)
398{
399 inc_irq_stat(IRQIO_APB);
400 if (!ap_suspend_flag)
401 tasklet_schedule(&ap_tasklet);
402}
403
404/**
405 * ap_tasklet_fn(): Tasklet to poll all AP devices.
406 * @dummy: Unused variable
407 *
408 * Poll all AP devices on the bus.
409 */
410static void ap_tasklet_fn(unsigned long dummy)
411{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200412 struct ap_card *ac;
413 struct ap_queue *aq;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200414 enum ap_wait wait = AP_WAIT_NONE;
415
416 /* Reset the indicator if interrupts are used. Thus new interrupts can
417 * be received. Doing it in the beginning of the tasklet is therefor
418 * important that no requests on any AP get lost.
419 */
420 if (ap_using_interrupts())
421 xchg(ap_airq.lsi_ptr, 0);
422
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200423 spin_lock_bh(&ap_list_lock);
424 for_each_ap_card(ac) {
425 for_each_ap_queue(aq, ac) {
426 spin_lock_bh(&aq->lock);
427 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
428 spin_unlock_bh(&aq->lock);
429 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200430 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200431 spin_unlock_bh(&ap_list_lock);
432
433 ap_wait(wait);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200434}
435
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200436static int ap_pending_requests(void)
437{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200438 struct ap_card *ac;
439 struct ap_queue *aq;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200440
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200441 spin_lock_bh(&ap_list_lock);
442 for_each_ap_card(ac) {
443 for_each_ap_queue(aq, ac) {
444 if (aq->queue_count == 0)
445 continue;
446 spin_unlock_bh(&ap_list_lock);
447 return 1;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200448 }
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200449 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200450 spin_unlock_bh(&ap_list_lock);
451 return 0;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200452}
453
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200454/**
455 * ap_poll_thread(): Thread that polls for finished requests.
456 * @data: Unused pointer
457 *
458 * AP bus poll thread. The purpose of this thread is to poll for
459 * finished requests in a loop if there is a "free" cpu - that is
460 * a cpu that doesn't have anything better to do. The polling stops
461 * as soon as there is another task or if all messages have been
462 * delivered.
463 */
464static int ap_poll_thread(void *data)
465{
466 DECLARE_WAITQUEUE(wait, current);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200467
468 set_user_nice(current, MAX_NICE);
469 set_freezable();
470 while (!kthread_should_stop()) {
471 add_wait_queue(&ap_poll_wait, &wait);
472 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200473 if (ap_suspend_flag || !ap_pending_requests()) {
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200474 schedule();
475 try_to_freeze();
476 }
477 set_current_state(TASK_RUNNING);
478 remove_wait_queue(&ap_poll_wait, &wait);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200479 if (need_resched()) {
480 schedule();
481 try_to_freeze();
482 continue;
483 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200484 ap_tasklet_fn(0);
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200485 }
486
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200487 return 0;
488}
489
490static int ap_poll_thread_start(void)
491{
492 int rc;
493
494 if (ap_using_interrupts() || ap_poll_kthread)
495 return 0;
496 mutex_lock(&ap_poll_thread_mutex);
497 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
498 rc = PTR_RET(ap_poll_kthread);
499 if (rc)
500 ap_poll_kthread = NULL;
501 mutex_unlock(&ap_poll_thread_mutex);
502 return rc;
503}
504
505static void ap_poll_thread_stop(void)
506{
507 if (!ap_poll_kthread)
508 return;
509 mutex_lock(&ap_poll_thread_mutex);
510 kthread_stop(ap_poll_kthread);
511 ap_poll_kthread = NULL;
512 mutex_unlock(&ap_poll_thread_mutex);
513}
514
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200515#define is_card_dev(x) ((x)->parent == ap_root_device)
516#define is_queue_dev(x) ((x)->parent != ap_root_device)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200517
518/**
Felix Beck1749a812008-04-17 07:46:28 +0200519 * ap_bus_match()
520 * @dev: Pointer to device
521 * @drv: Pointer to device_driver
522 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200523 * AP bus driver registration/unregistration.
524 */
525static int ap_bus_match(struct device *dev, struct device_driver *drv)
526{
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200527 struct ap_driver *ap_drv = to_ap_drv(drv);
528 struct ap_device_id *id;
529
Felix Beck1749a812008-04-17 07:46:28 +0200530 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200531 * Compare device type of the device with the list of
532 * supported types of the device_driver.
533 */
534 for (id = ap_drv->ids; id->match_flags; id++) {
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200535 if (is_card_dev(dev) &&
536 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
537 id->dev_type == to_ap_dev(dev)->device_type)
538 return 1;
539 if (is_queue_dev(dev) &&
540 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
541 id->dev_type == to_ap_dev(dev)->device_type)
542 return 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200543 }
544 return 0;
545}
546
547/**
Felix Beck1749a812008-04-17 07:46:28 +0200548 * ap_uevent(): Uevent function for AP devices.
549 * @dev: Pointer to device
550 * @env: Pointer to kobj_uevent_env
551 *
552 * It sets up a single environment variable DEV_TYPE which contains the
553 * hardware device type.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200554 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200555static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200556{
557 struct ap_device *ap_dev = to_ap_dev(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200558 int retval = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200559
560 if (!ap_dev)
561 return -ENODEV;
562
563 /* Set up DEV_TYPE environment variable. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200564 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700565 if (retval)
566 return retval;
567
Cornelia Huck66a4263b2006-12-04 15:40:10 +0100568 /* Add MODALIAS= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200569 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700570
Eric Rannaudbf624562007-03-30 22:23:12 -0700571 return retval;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200572}
573
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100574static int ap_dev_suspend(struct device *dev)
Felix Beck772f5472009-06-22 12:08:16 +0200575{
576 struct ap_device *ap_dev = to_ap_dev(dev);
Felix Beck772f5472009-06-22 12:08:16 +0200577
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200578 if (ap_dev->drv && ap_dev->drv->suspend)
579 ap_dev->drv->suspend(ap_dev);
580 return 0;
581}
582
583static int ap_dev_resume(struct device *dev)
584{
585 struct ap_device *ap_dev = to_ap_dev(dev);
586
587 if (ap_dev->drv && ap_dev->drv->resume)
588 ap_dev->drv->resume(ap_dev);
Felix Beck772f5472009-06-22 12:08:16 +0200589 return 0;
590}
591
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200592static void ap_bus_suspend(void)
593{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100594 AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
595
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200596 ap_suspend_flag = 1;
597 /*
598 * Disable scanning for devices, thus we do not want to scan
599 * for them after removing.
600 */
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200601 flush_work(&ap_scan_work);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200602 tasklet_disable(&ap_tasklet);
603}
604
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200605static int __ap_card_devices_unregister(struct device *dev, void *dummy)
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200606{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200607 if (is_card_dev(dev))
608 device_unregister(dev);
609 return 0;
610}
611
612static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
613{
614 if (is_queue_dev(dev))
615 device_unregister(dev);
616 return 0;
617}
618
619static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
620{
621 if (is_queue_dev(dev) &&
622 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
623 device_unregister(dev);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200624 return 0;
625}
626
627static void ap_bus_resume(void)
628{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200629 int rc;
Felix Beck772f5472009-06-22 12:08:16 +0200630
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100631 AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
632
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200633 /* remove all queue devices */
634 bus_for_each_dev(&ap_bus_type, NULL, NULL,
635 __ap_queue_devices_unregister);
636 /* remove all card devices */
637 bus_for_each_dev(&ap_bus_type, NULL, NULL,
638 __ap_card_devices_unregister);
639
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200640 /* Reset thin interrupt setting */
641 if (ap_interrupts_available() && !ap_using_interrupts()) {
642 rc = register_adapter_interrupt(&ap_airq);
643 ap_airq_flag = (rc == 0);
Felix Beck5314af62009-09-22 22:58:51 +0200644 }
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200645 if (!ap_interrupts_available() && ap_using_interrupts()) {
646 unregister_adapter_interrupt(&ap_airq);
647 ap_airq_flag = 0;
648 }
649 /* Reset domain */
650 if (!user_set_domain)
651 ap_domain_index = -1;
652 /* Get things going again */
653 ap_suspend_flag = 0;
654 if (ap_airq_flag)
655 xchg(ap_airq.lsi_ptr, 0);
656 tasklet_enable(&ap_tasklet);
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200657 queue_work(system_long_wq, &ap_scan_work);
Felix Beck772f5472009-06-22 12:08:16 +0200658}
659
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200660static int ap_power_event(struct notifier_block *this, unsigned long event,
661 void *ptr)
662{
663 switch (event) {
664 case PM_HIBERNATION_PREPARE:
665 case PM_SUSPEND_PREPARE:
666 ap_bus_suspend();
667 break;
668 case PM_POST_HIBERNATION:
669 case PM_POST_SUSPEND:
670 ap_bus_resume();
671 break;
672 default:
673 break;
674 }
675 return NOTIFY_DONE;
676}
677static struct notifier_block ap_power_notifier = {
678 .notifier_call = ap_power_event,
679};
680
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200681static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100682
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200683static struct bus_type ap_bus_type = {
684 .name = "ap",
685 .match = &ap_bus_match,
686 .uevent = &ap_uevent,
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100687 .pm = &ap_bus_pm_ops,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200688};
689
690static int ap_device_probe(struct device *dev)
691{
692 struct ap_device *ap_dev = to_ap_dev(dev);
693 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
694 int rc;
695
Harald Freudenbergere3850502017-05-24 10:26:29 +0200696 /* Add queue/card to list of active queues/cards */
697 spin_lock_bh(&ap_list_lock);
698 if (is_card_dev(dev))
699 list_add(&to_ap_card(dev)->list, &ap_card_list);
700 else
701 list_add(&to_ap_queue(dev)->list,
702 &to_ap_queue(dev)->card->queues);
703 spin_unlock_bh(&ap_list_lock);
704
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200705 ap_dev->drv = ap_drv;
706 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
Harald Freudenbergere3850502017-05-24 10:26:29 +0200707
708 if (rc) {
709 spin_lock_bh(&ap_list_lock);
710 if (is_card_dev(dev))
711 list_del_init(&to_ap_card(dev)->list);
712 else
713 list_del_init(&to_ap_queue(dev)->list);
714 spin_unlock_bh(&ap_list_lock);
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200715 ap_dev->drv = NULL;
Harald Freudenbergere3850502017-05-24 10:26:29 +0200716 }
717
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200718 return rc;
719}
720
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200721static int ap_device_remove(struct device *dev)
722{
723 struct ap_device *ap_dev = to_ap_dev(dev);
724 struct ap_driver *ap_drv = ap_dev->drv;
725
Harald Freudenbergere3850502017-05-24 10:26:29 +0200726 if (ap_drv->remove)
727 ap_drv->remove(ap_dev);
728
729 /* Remove queue/card from list of active queues/cards */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200730 spin_lock_bh(&ap_list_lock);
731 if (is_card_dev(dev))
732 list_del_init(&to_ap_card(dev)->list);
733 else
734 list_del_init(&to_ap_queue(dev)->list);
735 spin_unlock_bh(&ap_list_lock);
Harald Freudenbergere3850502017-05-24 10:26:29 +0200736
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200737 return 0;
738}
739
740int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
741 char *name)
742{
743 struct device_driver *drv = &ap_drv->driver;
744
Sascha Silbee3877532015-10-27 18:29:52 +0100745 if (!initialised)
746 return -ENODEV;
747
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200748 drv->bus = &ap_bus_type;
749 drv->probe = ap_device_probe;
750 drv->remove = ap_device_remove;
751 drv->owner = owner;
752 drv->name = name;
753 return driver_register(drv);
754}
755EXPORT_SYMBOL(ap_driver_register);
756
757void ap_driver_unregister(struct ap_driver *ap_drv)
758{
759 driver_unregister(&ap_drv->driver);
760}
761EXPORT_SYMBOL(ap_driver_unregister);
762
Holger Denglerdabecb22012-09-10 21:34:26 +0200763void ap_bus_force_rescan(void)
764{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200765 if (ap_suspend_flag)
766 return;
Ingo Tuchscherer56bbe682013-04-12 17:52:08 +0200767 /* processing a asynchronous bus rescan */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200768 del_timer(&ap_config_timer);
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200769 queue_work(system_long_wq, &ap_scan_work);
770 flush_work(&ap_scan_work);
Holger Denglerdabecb22012-09-10 21:34:26 +0200771}
772EXPORT_SYMBOL(ap_bus_force_rescan);
773
Felix Beck1749a812008-04-17 07:46:28 +0200774/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200775 * AP bus attributes.
776 */
777static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
778{
779 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
780}
781
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200782static ssize_t ap_domain_store(struct bus_type *bus,
783 const char *buf, size_t count)
784{
785 int domain;
786
787 if (sscanf(buf, "%i\n", &domain) != 1 ||
788 domain < 0 || domain > ap_max_domain_id)
789 return -EINVAL;
790 spin_lock_bh(&ap_domain_lock);
791 ap_domain_index = domain;
792 spin_unlock_bh(&ap_domain_lock);
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100793
Harald Freudenbergerac994e82017-05-12 16:35:14 +0200794 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100795
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200796 return count;
797}
798
799static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200800
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +0100801static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
802{
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200803 if (!ap_configuration) /* QCI not supported */
804 return snprintf(buf, PAGE_SIZE, "not supported\n");
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200805
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200806 return snprintf(buf, PAGE_SIZE,
807 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +0100808 ap_configuration->adm[0], ap_configuration->adm[1],
809 ap_configuration->adm[2], ap_configuration->adm[3],
810 ap_configuration->adm[4], ap_configuration->adm[5],
811 ap_configuration->adm[6], ap_configuration->adm[7]);
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +0100812}
813
814static BUS_ATTR(ap_control_domain_mask, 0444,
815 ap_control_domain_mask_show, NULL);
816
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200817static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
818{
819 if (!ap_configuration) /* QCI not supported */
820 return snprintf(buf, PAGE_SIZE, "not supported\n");
821
822 return snprintf(buf, PAGE_SIZE,
823 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
824 ap_configuration->aqm[0], ap_configuration->aqm[1],
825 ap_configuration->aqm[2], ap_configuration->aqm[3],
826 ap_configuration->aqm[4], ap_configuration->aqm[5],
827 ap_configuration->aqm[6], ap_configuration->aqm[7]);
828}
829
830static BUS_ATTR(ap_usage_domain_mask, 0444,
831 ap_usage_domain_mask_show, NULL);
832
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200833static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
834{
835 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
836}
837
Felix Beckcb17a632008-12-25 13:38:41 +0100838static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
839{
840 return snprintf(buf, PAGE_SIZE, "%d\n",
841 ap_using_interrupts() ? 1 : 0);
842}
843
844static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
845
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200846static ssize_t ap_config_time_store(struct bus_type *bus,
847 const char *buf, size_t count)
848{
849 int time;
850
851 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
852 return -EINVAL;
853 ap_config_time = time;
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200854 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200855 return count;
856}
857
858static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
859
860static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
861{
862 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
863}
864
865static ssize_t ap_poll_thread_store(struct bus_type *bus,
866 const char *buf, size_t count)
867{
868 int flag, rc;
869
870 if (sscanf(buf, "%d\n", &flag) != 1)
871 return -EINVAL;
872 if (flag) {
873 rc = ap_poll_thread_start();
874 if (rc)
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200875 count = rc;
876 } else
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200877 ap_poll_thread_stop();
878 return count;
879}
880
881static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
882
Felix Beckfe137232008-07-14 09:59:08 +0200883static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
884{
885 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
886}
887
888static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
889 size_t count)
890{
891 unsigned long long time;
892 ktime_t hr_time;
893
894 /* 120 seconds = maximum poll interval */
Felix Beckcb17a632008-12-25 13:38:41 +0100895 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
896 time > 120000000000ULL)
Felix Beckfe137232008-07-14 09:59:08 +0200897 return -EINVAL;
898 poll_timeout = time;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100899 hr_time = poll_timeout;
Felix Beckfe137232008-07-14 09:59:08 +0200900
Ingo Tuchscherer8cc2af72015-04-28 10:31:44 +0200901 spin_lock_bh(&ap_poll_timer_lock);
902 hrtimer_cancel(&ap_poll_timer);
903 hrtimer_set_expires(&ap_poll_timer, hr_time);
904 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
905 spin_unlock_bh(&ap_poll_timer_lock);
906
Felix Beckfe137232008-07-14 09:59:08 +0200907 return count;
908}
909
910static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
911
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100912static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
913{
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200914 int max_domain_id;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100915
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200916 if (ap_configuration)
917 max_domain_id = ap_max_domain_id ? : -1;
918 else
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100919 max_domain_id = 15;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100920 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
921}
922
923static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
924
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200925static struct bus_attribute *const ap_bus_attrs[] = {
926 &bus_attr_ap_domain,
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +0100927 &bus_attr_ap_control_domain_mask,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200928 &bus_attr_ap_usage_domain_mask,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200929 &bus_attr_config_time,
930 &bus_attr_poll_thread,
Felix Beckcb17a632008-12-25 13:38:41 +0100931 &bus_attr_ap_interrupts,
Felix Beckfe137232008-07-14 09:59:08 +0200932 &bus_attr_poll_timeout,
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100933 &bus_attr_ap_max_domain_id,
Felix Beckfe137232008-07-14 09:59:08 +0200934 NULL,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200935};
936
Holger Dengler75014552012-08-28 16:41:50 +0200937/**
Felix Beck1749a812008-04-17 07:46:28 +0200938 * ap_select_domain(): Select an AP domain.
939 *
940 * Pick one of the 16 AP domains.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200941 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100942static int ap_select_domain(void)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200943{
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200944 int count, max_count, best_domain;
945 struct ap_queue_status status;
946 int i, j;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200947
Felix Beck1749a812008-04-17 07:46:28 +0200948 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200949 * We want to use a single domain. Either the one specified with
950 * the "domain=" parameter or the domain with the maximum number
951 * of devices.
952 */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200953 spin_lock_bh(&ap_domain_lock);
954 if (ap_domain_index >= 0) {
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200955 /* Domain has already been selected. */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200956 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200957 return 0;
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200958 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200959 best_domain = -1;
960 max_count = 0;
961 for (i = 0; i < AP_DOMAINS; i++) {
Holger Dengler75014552012-08-28 16:41:50 +0200962 if (!ap_test_config_domain(i))
963 continue;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200964 count = 0;
965 for (j = 0; j < AP_DEVICES; j++) {
Holger Dengler75014552012-08-28 16:41:50 +0200966 if (!ap_test_config_card_id(j))
967 continue;
Tony Krowiake7fc5142016-11-08 07:09:13 +0100968 status = ap_test_queue(AP_MKQID(j, i),
969 ap_apft_available(),
970 NULL);
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200971 if (status.response_code != AP_RESPONSE_NORMAL)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200972 continue;
973 count++;
974 }
975 if (count > max_count) {
976 max_count = count;
977 best_domain = i;
978 }
979 }
980 if (best_domain >= 0){
981 ap_domain_index = best_domain;
Harald Freudenbergerac994e82017-05-12 16:35:14 +0200982 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200983 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200984 return 0;
985 }
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200986 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200987 return -ENODEV;
988}
989
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200990/*
991 * helper function to be used with bus_find_dev
992 * matches for the card device with the given id
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200993 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200994static int __match_card_device_with_id(struct device *dev, void *data)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200995{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200996 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200997}
998
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200999/* helper function to be used with bus_find_dev
1000 * matches for the queue device with a given qid
1001 */
1002static int __match_queue_device_with_qid(struct device *dev, void *data)
1003{
1004 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1005}
1006
1007/**
1008 * ap_scan_bus(): Scan the AP bus for new devices
1009 * Runs periodically, workqueue timer (ap_config_time)
1010 */
Al Viro4927b3f2006-12-06 19:18:20 +00001011static void ap_scan_bus(struct work_struct *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001012{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001013 struct ap_queue *aq;
1014 struct ap_card *ac;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001015 struct device *dev;
1016 ap_qid_t qid;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001017 int depth = 0, type = 0;
1018 unsigned int functions = 0;
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001019 int rc, id, dom, borked, domains, defdomdevs = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001020
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001021 AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
1022
Harald Freudenberger050349b2016-11-08 11:54:28 +01001023 ap_query_configuration(ap_configuration);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001024 if (ap_select_domain() != 0)
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001025 goto out;
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001026
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001027 for (id = 0; id < AP_DEVICES; id++) {
1028 /* check if device is registered */
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001029 dev = bus_find_device(&ap_bus_type, NULL,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001030 (void *)(long) id,
1031 __match_card_device_with_id);
1032 ac = dev ? to_ap_card(dev) : NULL;
1033 if (!ap_test_config_card_id(id)) {
1034 if (dev) {
1035 /* Card device has been removed from
1036 * configuration, remove the belonging
1037 * queue devices.
1038 */
1039 bus_for_each_dev(&ap_bus_type, NULL,
1040 (void *)(long) id,
1041 __ap_queue_devices_with_id_unregister);
1042 /* now remove the card device */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001043 device_unregister(dev);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001044 put_device(dev);
1045 }
1046 continue;
1047 }
1048 /* According to the configuration there should be a card
1049 * device, so check if there is at least one valid queue
1050 * and maybe create queue devices and the card device.
1051 */
1052 domains = 0;
1053 for (dom = 0; dom < AP_DOMAINS; dom++) {
1054 qid = AP_MKQID(id, dom);
1055 dev = bus_find_device(&ap_bus_type, NULL,
1056 (void *)(long) qid,
1057 __match_queue_device_with_qid);
1058 aq = dev ? to_ap_queue(dev) : NULL;
1059 if (!ap_test_config_domain(dom)) {
1060 if (dev) {
1061 /* Queue device exists but has been
1062 * removed from configuration.
1063 */
1064 device_unregister(dev);
1065 put_device(dev);
1066 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001067 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001068 }
1069 rc = ap_query_queue(qid, &depth, &type, &functions);
1070 if (dev) {
1071 spin_lock_bh(&aq->lock);
1072 if (rc == -ENODEV ||
1073 /* adapter reconfiguration */
1074 (ac && ac->functions != functions))
1075 aq->state = AP_STATE_BORKED;
1076 borked = aq->state == AP_STATE_BORKED;
1077 spin_unlock_bh(&aq->lock);
1078 if (borked) /* Remove broken device */
1079 device_unregister(dev);
1080 put_device(dev);
1081 if (!borked) {
1082 domains++;
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001083 if (dom == ap_domain_index)
1084 defdomdevs++;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001085 continue;
1086 }
1087 }
1088 if (rc)
1089 continue;
1090 /* new queue device needed */
1091 if (!ac) {
1092 /* but first create the card device */
1093 ac = ap_card_create(id, depth,
1094 type, functions);
1095 if (!ac)
1096 continue;
1097 ac->ap_dev.device.bus = &ap_bus_type;
1098 ac->ap_dev.device.parent = ap_root_device;
1099 dev_set_name(&ac->ap_dev.device,
1100 "card%02x", id);
1101 /* Register card with AP bus */
1102 rc = device_register(&ac->ap_dev.device);
1103 if (rc) {
1104 put_device(&ac->ap_dev.device);
1105 ac = NULL;
1106 break;
1107 }
1108 /* get it and thus adjust reference counter */
1109 get_device(&ac->ap_dev.device);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001110 }
1111 /* now create the new queue device */
1112 aq = ap_queue_create(qid, type);
1113 if (!aq)
1114 continue;
1115 aq->card = ac;
1116 aq->ap_dev.device.bus = &ap_bus_type;
1117 aq->ap_dev.device.parent = &ac->ap_dev.device;
1118 dev_set_name(&aq->ap_dev.device,
1119 "%02x.%04x", id, dom);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001120 /* Start with a device reset */
1121 spin_lock_bh(&aq->lock);
1122 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
1123 spin_unlock_bh(&aq->lock);
1124 /* Register device */
1125 rc = device_register(&aq->ap_dev.device);
1126 if (rc) {
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001127 put_device(&aq->ap_dev.device);
1128 continue;
1129 }
1130 domains++;
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001131 if (dom == ap_domain_index)
1132 defdomdevs++;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001133 } /* end domain loop */
1134 if (ac) {
1135 /* remove card dev if there are no queue devices */
1136 if (!domains)
1137 device_unregister(&ac->ap_dev.device);
1138 put_device(&ac->ap_dev.device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001139 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001140 } /* end device loop */
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001141
1142 if (defdomdevs < 1)
1143 AP_DBF(DBF_INFO, "no queue device with default domain %d available\n",
1144 ap_domain_index);
1145
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001146out:
1147 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001148}
1149
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001150static void ap_config_timeout(unsigned long ptr)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001151{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001152 if (ap_suspend_flag)
1153 return;
Martin Schwidefsky8139b892015-07-27 12:47:40 +02001154 queue_work(system_long_wq, &ap_scan_work);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001155}
1156
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001157static void ap_reset_all(void)
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001158{
1159 int i, j;
1160
Ingo Tuchscherer170387a2014-09-08 13:24:13 +02001161 for (i = 0; i < AP_DOMAINS; i++) {
1162 if (!ap_test_config_domain(i))
1163 continue;
1164 for (j = 0; j < AP_DEVICES; j++) {
1165 if (!ap_test_config_card_id(j))
1166 continue;
Martin Schwidefsky0db78552016-09-21 12:48:54 +02001167 ap_rapq(AP_MKQID(j, i));
Ingo Tuchscherer170387a2014-09-08 13:24:13 +02001168 }
1169 }
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001170}
1171
1172static struct reset_call ap_reset_call = {
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001173 .fn = ap_reset_all,
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001174};
1175
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001176int __init ap_debug_init(void)
1177{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001178 ap_dbf_info = debug_register("ap", 1, 1,
1179 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1180 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1181 debug_set_level(ap_dbf_info, DBF_ERR);
1182
1183 return 0;
1184}
1185
1186void ap_debug_exit(void)
1187{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001188 debug_unregister(ap_dbf_info);
1189}
1190
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001191/**
Felix Beck1749a812008-04-17 07:46:28 +02001192 * ap_module_init(): The module initialization code.
1193 *
1194 * Initializes the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001195 */
1196int __init ap_module_init(void)
1197{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001198 int max_domain_id;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001199 int rc, i;
1200
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001201 rc = ap_debug_init();
1202 if (rc)
1203 return rc;
1204
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001205 if (ap_instructions_available() != 0) {
1206 pr_warn("The hardware system does not support AP instructions\n");
1207 return -ENODEV;
1208 }
1209
1210 /* Get AP configuration data if available */
1211 ap_init_configuration();
1212
1213 if (ap_configuration)
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001214 max_domain_id =
1215 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001216 else
1217 max_domain_id = 15;
1218 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1219 pr_warn("%d is not a valid cryptographic domain\n",
1220 ap_domain_index);
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001221 ap_domain_index = -1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001222 }
Felix Beck5314af62009-09-22 22:58:51 +02001223 /* In resume callback we need to know if the user had set the domain.
1224 * If so, we can not just reset it.
1225 */
1226 if (ap_domain_index >= 0)
1227 user_set_domain = 1;
1228
Felix Beckcb17a632008-12-25 13:38:41 +01001229 if (ap_interrupts_available()) {
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001230 rc = register_adapter_interrupt(&ap_airq);
1231 ap_airq_flag = (rc == 0);
Felix Beckcb17a632008-12-25 13:38:41 +01001232 }
1233
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001234 register_reset_call(&ap_reset_call);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001235
1236 /* Create /sys/bus/ap. */
1237 rc = bus_register(&ap_bus_type);
1238 if (rc)
1239 goto out;
1240 for (i = 0; ap_bus_attrs[i]; i++) {
1241 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1242 if (rc)
1243 goto out_bus;
1244 }
1245
1246 /* Create /sys/devices/ap. */
Mark McLoughlin035da162008-12-15 12:58:29 +00001247 ap_root_device = root_device_register("ap");
Thomas Meyerba8da212013-06-01 11:51:13 +02001248 rc = PTR_RET(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001249 if (rc)
1250 goto out_bus;
1251
Felix Beck1749a812008-04-17 07:46:28 +02001252 /* Setup the AP bus rescan timer. */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001253 setup_timer(&ap_config_timer, ap_config_timeout, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001254
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001255 /*
1256 * Setup the high resultion poll timer.
Felix Beckfe137232008-07-14 09:59:08 +02001257 * If we are running under z/VM adjust polling to z/VM polling rate.
1258 */
1259 if (MACHINE_IS_VM)
1260 poll_timeout = 1500000;
Felix Beck93521312009-12-07 12:52:00 +01001261 spin_lock_init(&ap_poll_timer_lock);
Felix Beckfe137232008-07-14 09:59:08 +02001262 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1263 ap_poll_timer.function = ap_poll_timeout;
1264
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001265 /* Start the low priority AP bus poll thread. */
1266 if (ap_thread_flag) {
1267 rc = ap_poll_thread_start();
1268 if (rc)
1269 goto out_work;
1270 }
1271
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001272 rc = register_pm_notifier(&ap_power_notifier);
1273 if (rc)
1274 goto out_pm;
1275
Martin Schwidefsky8139b892015-07-27 12:47:40 +02001276 queue_work(system_long_wq, &ap_scan_work);
Sascha Silbee3877532015-10-27 18:29:52 +01001277 initialised = true;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001278
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001279 return 0;
1280
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001281out_pm:
1282 ap_poll_thread_stop();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001283out_work:
Felix Beckfe137232008-07-14 09:59:08 +02001284 hrtimer_cancel(&ap_poll_timer);
Mark McLoughlin035da162008-12-15 12:58:29 +00001285 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001286out_bus:
1287 while (i--)
1288 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1289 bus_unregister(&ap_bus_type);
1290out:
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001291 unregister_reset_call(&ap_reset_call);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001292 if (ap_using_interrupts())
1293 unregister_adapter_interrupt(&ap_airq);
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001294 kfree(ap_configuration);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001295 return rc;
1296}
Paul Gortmaker50a0d462017-02-09 09:48:10 -05001297device_initcall(ap_module_init);