blob: ea099910b4e99466db57f1c2868cc889234e99d3 [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/**
Felix Beck1749a812008-04-17 07:46:28 +0200169 * ap_test_queue(): Test adjunct processor queue.
170 * @qid: The AP queue number
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200171 * @info: Pointer to queue descriptor
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200172 *
Felix Beck1749a812008-04-17 07:46:28 +0200173 * Returns AP queue status structure.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200174 */
175static inline struct ap_queue_status
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200176ap_test_queue(ap_qid_t qid, unsigned long *info)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200177{
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200178 if (test_facility(15))
Heiko Carstens11d37672016-06-20 14:00:27 +0200179 qid |= 1UL << 23; /* set APFT T bit*/
Martin Schwidefsky0db78552016-09-21 12:48:54 +0200180 return ap_tapq(qid, info);
Holger Dengler75014552012-08-28 16:41:50 +0200181}
Holger Dengler75014552012-08-28 16:41:50 +0200182
Heiko Carstens11d37672016-06-20 14:00:27 +0200183static inline int ap_query_configuration(void)
184{
185 if (!ap_configuration)
186 return -EOPNOTSUPP;
Martin Schwidefsky0db78552016-09-21 12:48:54 +0200187 return ap_qci(ap_configuration);
Heiko Carstens11d37672016-06-20 14:00:27 +0200188}
189
Holger Dengler6bed05b2011-07-24 10:48:25 +0200190/**
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200191 * ap_init_configuration(): Allocate and query configuration array.
192 */
193static void ap_init_configuration(void)
194{
195 if (!ap_configuration_available())
196 return;
197
198 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
199 if (!ap_configuration)
200 return;
201 if (ap_query_configuration() != 0) {
202 kfree(ap_configuration);
203 ap_configuration = NULL;
204 return;
205 }
206}
207
208/*
209 * ap_test_config(): helper function to extract the nrth bit
210 * within the unsigned int array field.
211 */
212static inline int ap_test_config(unsigned int *field, unsigned int nr)
213{
214 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
215}
216
217/*
218 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
219 * @id AP card ID
220 *
221 * Returns 0 if the card is not configured
222 * 1 if the card is configured or
223 * if the configuration information is not available
224 */
225static inline int ap_test_config_card_id(unsigned int id)
226{
227 if (!ap_configuration) /* QCI not supported */
228 return 1;
229 return ap_test_config(ap_configuration->apm, id);
230}
231
232/*
233 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
234 * @domain AP usage domain ID
235 *
236 * Returns 0 if the usage domain is not configured
237 * 1 if the usage domain is configured or
238 * if the configuration information is not available
239 */
240static inline int ap_test_config_domain(unsigned int domain)
241{
242 if (!ap_configuration) /* QCI not supported */
243 return domain < 16;
244 return ap_test_config(ap_configuration->aqm, domain);
245}
246
247/**
Felix Beck1749a812008-04-17 07:46:28 +0200248 * ap_query_queue(): Check if an AP queue is available.
249 * @qid: The AP queue number
250 * @queue_depth: Pointer to queue depth value
251 * @device_type: Pointer to device type value
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200252 * @facilities: Pointer to facility indicator
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200253 */
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200254static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
255 unsigned int *facilities)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200256{
257 struct ap_queue_status status;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200258 unsigned long info;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200259 int nd;
260
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200261 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200262 return -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200263
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200264 status = ap_test_queue(qid, &info);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200265 switch (status.response_code) {
266 case AP_RESPONSE_NORMAL:
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200267 *queue_depth = (int)(info & 0xff);
268 *device_type = (int)((info >> 24) & 0xff);
269 *facilities = (unsigned int)(info >> 32);
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200270 /* Update maximum domain id */
271 nd = (info >> 16) & 0xff;
Ingo Tuchschererc1c13682016-11-02 10:23:24 +0100272 /* if N bit is available, z13 and newer */
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200273 if ((info & (1UL << 57)) && nd > 0)
274 ap_max_domain_id = nd;
Ingo Tuchschererc1c13682016-11-02 10:23:24 +0100275 else /* older machine types */
276 ap_max_domain_id = 15;
Harald Freudenberger14878422016-10-27 08:57:39 +0200277 switch (*device_type) {
278 /* For CEX2 and CEX3 the available functions
279 * are not refrected by the facilities bits.
280 * Instead it is coded into the type. So here
281 * modify the function bits based on the type.
282 */
283 case AP_DEVICE_TYPE_CEX2A:
284 case AP_DEVICE_TYPE_CEX3A:
285 *facilities |= 0x08000000;
286 break;
287 case AP_DEVICE_TYPE_CEX2C:
288 case AP_DEVICE_TYPE_CEX3C:
289 *facilities |= 0x10000000;
290 break;
291 default:
292 break;
293 }
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200294 return 0;
295 case AP_RESPONSE_Q_NOT_AVAIL:
296 case AP_RESPONSE_DECONFIGURED:
297 case AP_RESPONSE_CHECKSTOPPED:
298 case AP_RESPONSE_INVALID_ADDRESS:
299 return -ENODEV;
300 case AP_RESPONSE_RESET_IN_PROGRESS:
301 case AP_RESPONSE_OTHERWISE_CHANGED:
302 case AP_RESPONSE_BUSY:
303 return -EBUSY;
304 default:
305 BUG();
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200306 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200307}
308
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200309void ap_wait(enum ap_wait wait)
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200310{
311 ktime_t hr_time;
312
313 switch (wait) {
314 case AP_WAIT_AGAIN:
315 case AP_WAIT_INTERRUPT:
316 if (ap_using_interrupts())
317 break;
318 if (ap_poll_kthread) {
319 wake_up(&ap_poll_wait);
320 break;
321 }
322 /* Fall through */
323 case AP_WAIT_TIMEOUT:
324 spin_lock_bh(&ap_poll_timer_lock);
325 if (!hrtimer_is_queued(&ap_poll_timer)) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100326 hr_time = poll_timeout;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200327 hrtimer_forward_now(&ap_poll_timer, hr_time);
328 hrtimer_restart(&ap_poll_timer);
329 }
330 spin_unlock_bh(&ap_poll_timer_lock);
331 break;
332 case AP_WAIT_NONE:
333 default:
334 break;
335 }
336}
337
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200338/**
339 * ap_request_timeout(): Handling of request timeouts
340 * @data: Holds the AP device.
341 *
342 * Handles request timeouts.
343 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200344void ap_request_timeout(unsigned long data)
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200345{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200346 struct ap_queue *aq = (struct ap_queue *) data;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200347
348 if (ap_suspend_flag)
349 return;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200350 spin_lock_bh(&aq->lock);
351 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
352 spin_unlock_bh(&aq->lock);
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200353}
354
355/**
356 * ap_poll_timeout(): AP receive polling for finished AP requests.
357 * @unused: Unused pointer.
358 *
359 * Schedules the AP tasklet using a high resolution timer.
360 */
361static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
362{
363 if (!ap_suspend_flag)
364 tasklet_schedule(&ap_tasklet);
365 return HRTIMER_NORESTART;
366}
367
368/**
369 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
370 * @airq: pointer to adapter interrupt descriptor
371 */
372static void ap_interrupt_handler(struct airq_struct *airq)
373{
374 inc_irq_stat(IRQIO_APB);
375 if (!ap_suspend_flag)
376 tasklet_schedule(&ap_tasklet);
377}
378
379/**
380 * ap_tasklet_fn(): Tasklet to poll all AP devices.
381 * @dummy: Unused variable
382 *
383 * Poll all AP devices on the bus.
384 */
385static void ap_tasklet_fn(unsigned long dummy)
386{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200387 struct ap_card *ac;
388 struct ap_queue *aq;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200389 enum ap_wait wait = AP_WAIT_NONE;
390
391 /* Reset the indicator if interrupts are used. Thus new interrupts can
392 * be received. Doing it in the beginning of the tasklet is therefor
393 * important that no requests on any AP get lost.
394 */
395 if (ap_using_interrupts())
396 xchg(ap_airq.lsi_ptr, 0);
397
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200398 spin_lock_bh(&ap_list_lock);
399 for_each_ap_card(ac) {
400 for_each_ap_queue(aq, ac) {
401 spin_lock_bh(&aq->lock);
402 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
403 spin_unlock_bh(&aq->lock);
404 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200405 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200406 spin_unlock_bh(&ap_list_lock);
407
408 ap_wait(wait);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200409}
410
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200411static int ap_pending_requests(void)
412{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200413 struct ap_card *ac;
414 struct ap_queue *aq;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200415
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200416 spin_lock_bh(&ap_list_lock);
417 for_each_ap_card(ac) {
418 for_each_ap_queue(aq, ac) {
419 if (aq->queue_count == 0)
420 continue;
421 spin_unlock_bh(&ap_list_lock);
422 return 1;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200423 }
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200424 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200425 spin_unlock_bh(&ap_list_lock);
426 return 0;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200427}
428
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200429/**
430 * ap_poll_thread(): Thread that polls for finished requests.
431 * @data: Unused pointer
432 *
433 * AP bus poll thread. The purpose of this thread is to poll for
434 * finished requests in a loop if there is a "free" cpu - that is
435 * a cpu that doesn't have anything better to do. The polling stops
436 * as soon as there is another task or if all messages have been
437 * delivered.
438 */
439static int ap_poll_thread(void *data)
440{
441 DECLARE_WAITQUEUE(wait, current);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200442
443 set_user_nice(current, MAX_NICE);
444 set_freezable();
445 while (!kthread_should_stop()) {
446 add_wait_queue(&ap_poll_wait, &wait);
447 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200448 if (ap_suspend_flag || !ap_pending_requests()) {
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200449 schedule();
450 try_to_freeze();
451 }
452 set_current_state(TASK_RUNNING);
453 remove_wait_queue(&ap_poll_wait, &wait);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200454 if (need_resched()) {
455 schedule();
456 try_to_freeze();
457 continue;
458 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200459 ap_tasklet_fn(0);
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200460 }
461
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200462 return 0;
463}
464
465static int ap_poll_thread_start(void)
466{
467 int rc;
468
469 if (ap_using_interrupts() || ap_poll_kthread)
470 return 0;
471 mutex_lock(&ap_poll_thread_mutex);
472 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
473 rc = PTR_RET(ap_poll_kthread);
474 if (rc)
475 ap_poll_kthread = NULL;
476 mutex_unlock(&ap_poll_thread_mutex);
477 return rc;
478}
479
480static void ap_poll_thread_stop(void)
481{
482 if (!ap_poll_kthread)
483 return;
484 mutex_lock(&ap_poll_thread_mutex);
485 kthread_stop(ap_poll_kthread);
486 ap_poll_kthread = NULL;
487 mutex_unlock(&ap_poll_thread_mutex);
488}
489
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200490#define is_card_dev(x) ((x)->parent == ap_root_device)
491#define is_queue_dev(x) ((x)->parent != ap_root_device)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200492
493/**
Felix Beck1749a812008-04-17 07:46:28 +0200494 * ap_bus_match()
495 * @dev: Pointer to device
496 * @drv: Pointer to device_driver
497 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200498 * AP bus driver registration/unregistration.
499 */
500static int ap_bus_match(struct device *dev, struct device_driver *drv)
501{
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200502 struct ap_driver *ap_drv = to_ap_drv(drv);
503 struct ap_device_id *id;
504
Felix Beck1749a812008-04-17 07:46:28 +0200505 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200506 * Compare device type of the device with the list of
507 * supported types of the device_driver.
508 */
509 for (id = ap_drv->ids; id->match_flags; id++) {
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200510 if (is_card_dev(dev) &&
511 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
512 id->dev_type == to_ap_dev(dev)->device_type)
513 return 1;
514 if (is_queue_dev(dev) &&
515 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
516 id->dev_type == to_ap_dev(dev)->device_type)
517 return 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200518 }
519 return 0;
520}
521
522/**
Felix Beck1749a812008-04-17 07:46:28 +0200523 * ap_uevent(): Uevent function for AP devices.
524 * @dev: Pointer to device
525 * @env: Pointer to kobj_uevent_env
526 *
527 * It sets up a single environment variable DEV_TYPE which contains the
528 * hardware device type.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200529 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200530static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200531{
532 struct ap_device *ap_dev = to_ap_dev(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200533 int retval = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200534
535 if (!ap_dev)
536 return -ENODEV;
537
538 /* Set up DEV_TYPE environment variable. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200539 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700540 if (retval)
541 return retval;
542
Cornelia Huck66a4263b2006-12-04 15:40:10 +0100543 /* Add MODALIAS= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200544 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700545
Eric Rannaudbf624562007-03-30 22:23:12 -0700546 return retval;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200547}
548
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100549static int ap_dev_suspend(struct device *dev)
Felix Beck772f5472009-06-22 12:08:16 +0200550{
551 struct ap_device *ap_dev = to_ap_dev(dev);
Felix Beck772f5472009-06-22 12:08:16 +0200552
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200553 if (ap_dev->drv && ap_dev->drv->suspend)
554 ap_dev->drv->suspend(ap_dev);
555 return 0;
556}
557
558static int ap_dev_resume(struct device *dev)
559{
560 struct ap_device *ap_dev = to_ap_dev(dev);
561
562 if (ap_dev->drv && ap_dev->drv->resume)
563 ap_dev->drv->resume(ap_dev);
Felix Beck772f5472009-06-22 12:08:16 +0200564 return 0;
565}
566
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200567static void ap_bus_suspend(void)
568{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100569 AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
570
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200571 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
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100606 AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
607
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200608 /* remove all queue devices */
609 bus_for_each_dev(&ap_bus_type, NULL, NULL,
610 __ap_queue_devices_unregister);
611 /* remove all card devices */
612 bus_for_each_dev(&ap_bus_type, NULL, NULL,
613 __ap_card_devices_unregister);
614
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200615 /* Reset thin interrupt setting */
616 if (ap_interrupts_available() && !ap_using_interrupts()) {
617 rc = register_adapter_interrupt(&ap_airq);
618 ap_airq_flag = (rc == 0);
Felix Beck5314af62009-09-22 22:58:51 +0200619 }
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200620 if (!ap_interrupts_available() && ap_using_interrupts()) {
621 unregister_adapter_interrupt(&ap_airq);
622 ap_airq_flag = 0;
623 }
624 /* Reset domain */
625 if (!user_set_domain)
626 ap_domain_index = -1;
627 /* Get things going again */
628 ap_suspend_flag = 0;
629 if (ap_airq_flag)
630 xchg(ap_airq.lsi_ptr, 0);
631 tasklet_enable(&ap_tasklet);
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200632 queue_work(system_long_wq, &ap_scan_work);
Felix Beck772f5472009-06-22 12:08:16 +0200633}
634
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200635static int ap_power_event(struct notifier_block *this, unsigned long event,
636 void *ptr)
637{
638 switch (event) {
639 case PM_HIBERNATION_PREPARE:
640 case PM_SUSPEND_PREPARE:
641 ap_bus_suspend();
642 break;
643 case PM_POST_HIBERNATION:
644 case PM_POST_SUSPEND:
645 ap_bus_resume();
646 break;
647 default:
648 break;
649 }
650 return NOTIFY_DONE;
651}
652static struct notifier_block ap_power_notifier = {
653 .notifier_call = ap_power_event,
654};
655
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200656static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100657
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200658static struct bus_type ap_bus_type = {
659 .name = "ap",
660 .match = &ap_bus_match,
661 .uevent = &ap_uevent,
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100662 .pm = &ap_bus_pm_ops,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200663};
664
665static int ap_device_probe(struct device *dev)
666{
667 struct ap_device *ap_dev = to_ap_dev(dev);
668 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
669 int rc;
670
Harald Freudenbergere3850502017-05-24 10:26:29 +0200671 /* Add queue/card to list of active queues/cards */
672 spin_lock_bh(&ap_list_lock);
673 if (is_card_dev(dev))
674 list_add(&to_ap_card(dev)->list, &ap_card_list);
675 else
676 list_add(&to_ap_queue(dev)->list,
677 &to_ap_queue(dev)->card->queues);
678 spin_unlock_bh(&ap_list_lock);
679
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200680 ap_dev->drv = ap_drv;
681 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
Harald Freudenbergere3850502017-05-24 10:26:29 +0200682
683 if (rc) {
684 spin_lock_bh(&ap_list_lock);
685 if (is_card_dev(dev))
686 list_del_init(&to_ap_card(dev)->list);
687 else
688 list_del_init(&to_ap_queue(dev)->list);
689 spin_unlock_bh(&ap_list_lock);
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200690 ap_dev->drv = NULL;
Harald Freudenbergere3850502017-05-24 10:26:29 +0200691 }
692
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200693 return rc;
694}
695
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200696static int ap_device_remove(struct device *dev)
697{
698 struct ap_device *ap_dev = to_ap_dev(dev);
699 struct ap_driver *ap_drv = ap_dev->drv;
700
Harald Freudenbergere3850502017-05-24 10:26:29 +0200701 if (ap_drv->remove)
702 ap_drv->remove(ap_dev);
703
704 /* Remove queue/card from list of active queues/cards */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200705 spin_lock_bh(&ap_list_lock);
706 if (is_card_dev(dev))
707 list_del_init(&to_ap_card(dev)->list);
708 else
709 list_del_init(&to_ap_queue(dev)->list);
710 spin_unlock_bh(&ap_list_lock);
Harald Freudenbergere3850502017-05-24 10:26:29 +0200711
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200712 return 0;
713}
714
715int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
716 char *name)
717{
718 struct device_driver *drv = &ap_drv->driver;
719
Sascha Silbee3877532015-10-27 18:29:52 +0100720 if (!initialised)
721 return -ENODEV;
722
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200723 drv->bus = &ap_bus_type;
724 drv->probe = ap_device_probe;
725 drv->remove = ap_device_remove;
726 drv->owner = owner;
727 drv->name = name;
728 return driver_register(drv);
729}
730EXPORT_SYMBOL(ap_driver_register);
731
732void ap_driver_unregister(struct ap_driver *ap_drv)
733{
734 driver_unregister(&ap_drv->driver);
735}
736EXPORT_SYMBOL(ap_driver_unregister);
737
Holger Denglerdabecb22012-09-10 21:34:26 +0200738void ap_bus_force_rescan(void)
739{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200740 if (ap_suspend_flag)
741 return;
Ingo Tuchscherer56bbe682013-04-12 17:52:08 +0200742 /* processing a asynchronous bus rescan */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200743 del_timer(&ap_config_timer);
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200744 queue_work(system_long_wq, &ap_scan_work);
745 flush_work(&ap_scan_work);
Holger Denglerdabecb22012-09-10 21:34:26 +0200746}
747EXPORT_SYMBOL(ap_bus_force_rescan);
748
Felix Beck1749a812008-04-17 07:46:28 +0200749/*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200750 * AP bus attributes.
751 */
752static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
753{
754 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
755}
756
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200757static ssize_t ap_domain_store(struct bus_type *bus,
758 const char *buf, size_t count)
759{
760 int domain;
761
762 if (sscanf(buf, "%i\n", &domain) != 1 ||
763 domain < 0 || domain > ap_max_domain_id)
764 return -EINVAL;
765 spin_lock_bh(&ap_domain_lock);
766 ap_domain_index = domain;
767 spin_unlock_bh(&ap_domain_lock);
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100768
769 AP_DBF(DBF_DEBUG, "store new default domain=%d\n", domain);
770
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200771 return count;
772}
773
774static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200775
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100776static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
777{
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200778 if (!ap_configuration) /* QCI not supported */
779 return snprintf(buf, PAGE_SIZE, "not supported\n");
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200780
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200781 return snprintf(buf, PAGE_SIZE,
782 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100783 ap_configuration->adm[0], ap_configuration->adm[1],
784 ap_configuration->adm[2], ap_configuration->adm[3],
785 ap_configuration->adm[4], ap_configuration->adm[5],
786 ap_configuration->adm[6], ap_configuration->adm[7]);
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100787}
788
789static BUS_ATTR(ap_control_domain_mask, 0444,
790 ap_control_domain_mask_show, NULL);
791
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200792static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
793{
794 if (!ap_configuration) /* QCI not supported */
795 return snprintf(buf, PAGE_SIZE, "not supported\n");
796
797 return snprintf(buf, PAGE_SIZE,
798 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
799 ap_configuration->aqm[0], ap_configuration->aqm[1],
800 ap_configuration->aqm[2], ap_configuration->aqm[3],
801 ap_configuration->aqm[4], ap_configuration->aqm[5],
802 ap_configuration->aqm[6], ap_configuration->aqm[7]);
803}
804
805static BUS_ATTR(ap_usage_domain_mask, 0444,
806 ap_usage_domain_mask_show, NULL);
807
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200808static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
809{
810 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
811}
812
Felix Beckcb17a632008-12-25 13:38:41 +0100813static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
814{
815 return snprintf(buf, PAGE_SIZE, "%d\n",
816 ap_using_interrupts() ? 1 : 0);
817}
818
819static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
820
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200821static ssize_t ap_config_time_store(struct bus_type *bus,
822 const char *buf, size_t count)
823{
824 int time;
825
826 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
827 return -EINVAL;
828 ap_config_time = time;
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200829 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200830 return count;
831}
832
833static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
834
835static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
836{
837 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
838}
839
840static ssize_t ap_poll_thread_store(struct bus_type *bus,
841 const char *buf, size_t count)
842{
843 int flag, rc;
844
845 if (sscanf(buf, "%d\n", &flag) != 1)
846 return -EINVAL;
847 if (flag) {
848 rc = ap_poll_thread_start();
849 if (rc)
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200850 count = rc;
851 } else
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200852 ap_poll_thread_stop();
853 return count;
854}
855
856static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
857
Felix Beckfe137232008-07-14 09:59:08 +0200858static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
859{
860 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
861}
862
863static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
864 size_t count)
865{
866 unsigned long long time;
867 ktime_t hr_time;
868
869 /* 120 seconds = maximum poll interval */
Felix Beckcb17a632008-12-25 13:38:41 +0100870 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
871 time > 120000000000ULL)
Felix Beckfe137232008-07-14 09:59:08 +0200872 return -EINVAL;
873 poll_timeout = time;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100874 hr_time = poll_timeout;
Felix Beckfe137232008-07-14 09:59:08 +0200875
Ingo Tuchscherer8cc2af72015-04-28 10:31:44 +0200876 spin_lock_bh(&ap_poll_timer_lock);
877 hrtimer_cancel(&ap_poll_timer);
878 hrtimer_set_expires(&ap_poll_timer, hr_time);
879 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
880 spin_unlock_bh(&ap_poll_timer_lock);
881
Felix Beckfe137232008-07-14 09:59:08 +0200882 return count;
883}
884
885static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
886
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100887static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
888{
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200889 int max_domain_id;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100890
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200891 if (ap_configuration)
892 max_domain_id = ap_max_domain_id ? : -1;
893 else
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100894 max_domain_id = 15;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100895 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
896}
897
898static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
899
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200900static struct bus_attribute *const ap_bus_attrs[] = {
901 &bus_attr_ap_domain,
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100902 &bus_attr_ap_control_domain_mask,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200903 &bus_attr_ap_usage_domain_mask,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200904 &bus_attr_config_time,
905 &bus_attr_poll_thread,
Felix Beckcb17a632008-12-25 13:38:41 +0100906 &bus_attr_ap_interrupts,
Felix Beckfe137232008-07-14 09:59:08 +0200907 &bus_attr_poll_timeout,
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +0100908 &bus_attr_ap_max_domain_id,
Felix Beckfe137232008-07-14 09:59:08 +0200909 NULL,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200910};
911
Holger Dengler75014552012-08-28 16:41:50 +0200912/**
Felix Beck1749a812008-04-17 07:46:28 +0200913 * ap_select_domain(): Select an AP domain.
914 *
915 * Pick one of the 16 AP domains.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200916 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100917static int ap_select_domain(void)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200918{
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200919 int count, max_count, best_domain;
920 struct ap_queue_status status;
921 int i, j;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200922
Felix Beck1749a812008-04-17 07:46:28 +0200923 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200924 * We want to use a single domain. Either the one specified with
925 * the "domain=" parameter or the domain with the maximum number
926 * of devices.
927 */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200928 spin_lock_bh(&ap_domain_lock);
929 if (ap_domain_index >= 0) {
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200930 /* Domain has already been selected. */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200931 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200932 return 0;
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200933 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200934 best_domain = -1;
935 max_count = 0;
936 for (i = 0; i < AP_DOMAINS; i++) {
Holger Dengler75014552012-08-28 16:41:50 +0200937 if (!ap_test_config_domain(i))
938 continue;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200939 count = 0;
940 for (j = 0; j < AP_DEVICES; j++) {
Holger Dengler75014552012-08-28 16:41:50 +0200941 if (!ap_test_config_card_id(j))
942 continue;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200943 status = ap_test_queue(AP_MKQID(j, i), NULL);
944 if (status.response_code != AP_RESPONSE_NORMAL)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200945 continue;
946 count++;
947 }
948 if (count > max_count) {
949 max_count = count;
950 best_domain = i;
951 }
952 }
953 if (best_domain >= 0){
954 ap_domain_index = best_domain;
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200955 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200956 return 0;
957 }
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200958 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200959 return -ENODEV;
960}
961
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200962/*
963 * helper function to be used with bus_find_dev
964 * matches for the card device with the given id
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200965 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200966static int __match_card_device_with_id(struct device *dev, void *data)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200967{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200968 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200969}
970
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200971/* helper function to be used with bus_find_dev
972 * matches for the queue device with a given qid
973 */
974static int __match_queue_device_with_qid(struct device *dev, void *data)
975{
976 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
977}
978
979/**
980 * ap_scan_bus(): Scan the AP bus for new devices
981 * Runs periodically, workqueue timer (ap_config_time)
982 */
Al Viro4927b3f2006-12-06 19:18:20 +0000983static void ap_scan_bus(struct work_struct *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200984{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200985 struct ap_queue *aq;
986 struct ap_card *ac;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200987 struct device *dev;
988 ap_qid_t qid;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200989 int depth = 0, type = 0;
990 unsigned int functions = 0;
991 int rc, id, dom, borked, domains;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200992
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100993 AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
994
Holger Dengler75014552012-08-28 16:41:50 +0200995 ap_query_configuration();
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200996 if (ap_select_domain() != 0)
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200997 goto out;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200998
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200999 for (id = 0; id < AP_DEVICES; id++) {
1000 /* check if device is registered */
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001001 dev = bus_find_device(&ap_bus_type, NULL,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001002 (void *)(long) id,
1003 __match_card_device_with_id);
1004 ac = dev ? to_ap_card(dev) : NULL;
1005 if (!ap_test_config_card_id(id)) {
1006 if (dev) {
1007 /* Card device has been removed from
1008 * configuration, remove the belonging
1009 * queue devices.
1010 */
1011 bus_for_each_dev(&ap_bus_type, NULL,
1012 (void *)(long) id,
1013 __ap_queue_devices_with_id_unregister);
1014 /* now remove the card device */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001015 device_unregister(dev);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001016 put_device(dev);
1017 }
1018 continue;
1019 }
1020 /* According to the configuration there should be a card
1021 * device, so check if there is at least one valid queue
1022 * and maybe create queue devices and the card device.
1023 */
1024 domains = 0;
1025 for (dom = 0; dom < AP_DOMAINS; dom++) {
1026 qid = AP_MKQID(id, dom);
1027 dev = bus_find_device(&ap_bus_type, NULL,
1028 (void *)(long) qid,
1029 __match_queue_device_with_qid);
1030 aq = dev ? to_ap_queue(dev) : NULL;
1031 if (!ap_test_config_domain(dom)) {
1032 if (dev) {
1033 /* Queue device exists but has been
1034 * removed from configuration.
1035 */
1036 device_unregister(dev);
1037 put_device(dev);
1038 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001039 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001040 }
1041 rc = ap_query_queue(qid, &depth, &type, &functions);
1042 if (dev) {
1043 spin_lock_bh(&aq->lock);
1044 if (rc == -ENODEV ||
1045 /* adapter reconfiguration */
1046 (ac && ac->functions != functions))
1047 aq->state = AP_STATE_BORKED;
1048 borked = aq->state == AP_STATE_BORKED;
1049 spin_unlock_bh(&aq->lock);
1050 if (borked) /* Remove broken device */
1051 device_unregister(dev);
1052 put_device(dev);
1053 if (!borked) {
1054 domains++;
1055 continue;
1056 }
1057 }
1058 if (rc)
1059 continue;
1060 /* new queue device needed */
1061 if (!ac) {
1062 /* but first create the card device */
1063 ac = ap_card_create(id, depth,
1064 type, functions);
1065 if (!ac)
1066 continue;
1067 ac->ap_dev.device.bus = &ap_bus_type;
1068 ac->ap_dev.device.parent = ap_root_device;
1069 dev_set_name(&ac->ap_dev.device,
1070 "card%02x", id);
1071 /* Register card with AP bus */
1072 rc = device_register(&ac->ap_dev.device);
1073 if (rc) {
1074 put_device(&ac->ap_dev.device);
1075 ac = NULL;
1076 break;
1077 }
1078 /* get it and thus adjust reference counter */
1079 get_device(&ac->ap_dev.device);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001080 }
1081 /* now create the new queue device */
1082 aq = ap_queue_create(qid, type);
1083 if (!aq)
1084 continue;
1085 aq->card = ac;
1086 aq->ap_dev.device.bus = &ap_bus_type;
1087 aq->ap_dev.device.parent = &ac->ap_dev.device;
1088 dev_set_name(&aq->ap_dev.device,
1089 "%02x.%04x", id, dom);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001090 /* Start with a device reset */
1091 spin_lock_bh(&aq->lock);
1092 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
1093 spin_unlock_bh(&aq->lock);
1094 /* Register device */
1095 rc = device_register(&aq->ap_dev.device);
1096 if (rc) {
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001097 put_device(&aq->ap_dev.device);
1098 continue;
1099 }
1100 domains++;
1101 } /* end domain loop */
1102 if (ac) {
1103 /* remove card dev if there are no queue devices */
1104 if (!domains)
1105 device_unregister(&ac->ap_dev.device);
1106 put_device(&ac->ap_dev.device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001107 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001108 } /* end device loop */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001109out:
1110 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001111}
1112
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001113static void ap_config_timeout(unsigned long ptr)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001114{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001115 if (ap_suspend_flag)
1116 return;
Martin Schwidefsky8139b892015-07-27 12:47:40 +02001117 queue_work(system_long_wq, &ap_scan_work);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001118}
1119
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001120static void ap_reset_all(void)
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001121{
1122 int i, j;
1123
Ingo Tuchscherer170387a2014-09-08 13:24:13 +02001124 for (i = 0; i < AP_DOMAINS; i++) {
1125 if (!ap_test_config_domain(i))
1126 continue;
1127 for (j = 0; j < AP_DEVICES; j++) {
1128 if (!ap_test_config_card_id(j))
1129 continue;
Martin Schwidefsky0db78552016-09-21 12:48:54 +02001130 ap_rapq(AP_MKQID(j, i));
Ingo Tuchscherer170387a2014-09-08 13:24:13 +02001131 }
1132 }
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001133}
1134
1135static struct reset_call ap_reset_call = {
Ralph Wuerthner13e742b2006-12-15 17:18:17 +01001136 .fn = ap_reset_all,
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001137};
1138
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001139int __init ap_debug_init(void)
1140{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001141 ap_dbf_info = debug_register("ap", 1, 1,
1142 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1143 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1144 debug_set_level(ap_dbf_info, DBF_ERR);
1145
1146 return 0;
1147}
1148
1149void ap_debug_exit(void)
1150{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001151 debug_unregister(ap_dbf_info);
1152}
1153
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001154/**
Felix Beck1749a812008-04-17 07:46:28 +02001155 * ap_module_init(): The module initialization code.
1156 *
1157 * Initializes the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001158 */
1159int __init ap_module_init(void)
1160{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001161 int max_domain_id;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001162 int rc, i;
1163
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001164 rc = ap_debug_init();
1165 if (rc)
1166 return rc;
1167
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001168 if (ap_instructions_available() != 0) {
1169 pr_warn("The hardware system does not support AP instructions\n");
1170 return -ENODEV;
1171 }
1172
1173 /* Get AP configuration data if available */
1174 ap_init_configuration();
1175
1176 if (ap_configuration)
1177 max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1);
1178 else
1179 max_domain_id = 15;
1180 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1181 pr_warn("%d is not a valid cryptographic domain\n",
1182 ap_domain_index);
Wei Yongjun33c388b2016-07-31 11:47:06 +02001183 rc = -EINVAL;
1184 goto out_free;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001185 }
Felix Beck5314af62009-09-22 22:58:51 +02001186 /* In resume callback we need to know if the user had set the domain.
1187 * If so, we can not just reset it.
1188 */
1189 if (ap_domain_index >= 0)
1190 user_set_domain = 1;
1191
Felix Beckcb17a632008-12-25 13:38:41 +01001192 if (ap_interrupts_available()) {
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001193 rc = register_adapter_interrupt(&ap_airq);
1194 ap_airq_flag = (rc == 0);
Felix Beckcb17a632008-12-25 13:38:41 +01001195 }
1196
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001197 register_reset_call(&ap_reset_call);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001198
1199 /* Create /sys/bus/ap. */
1200 rc = bus_register(&ap_bus_type);
1201 if (rc)
1202 goto out;
1203 for (i = 0; ap_bus_attrs[i]; i++) {
1204 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1205 if (rc)
1206 goto out_bus;
1207 }
1208
1209 /* Create /sys/devices/ap. */
Mark McLoughlin035da162008-12-15 12:58:29 +00001210 ap_root_device = root_device_register("ap");
Thomas Meyerba8da212013-06-01 11:51:13 +02001211 rc = PTR_RET(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001212 if (rc)
1213 goto out_bus;
1214
Felix Beck1749a812008-04-17 07:46:28 +02001215 /* Setup the AP bus rescan timer. */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001216 setup_timer(&ap_config_timer, ap_config_timeout, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001217
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001218 /*
1219 * Setup the high resultion poll timer.
Felix Beckfe137232008-07-14 09:59:08 +02001220 * If we are running under z/VM adjust polling to z/VM polling rate.
1221 */
1222 if (MACHINE_IS_VM)
1223 poll_timeout = 1500000;
Felix Beck93521312009-12-07 12:52:00 +01001224 spin_lock_init(&ap_poll_timer_lock);
Felix Beckfe137232008-07-14 09:59:08 +02001225 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1226 ap_poll_timer.function = ap_poll_timeout;
1227
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001228 /* Start the low priority AP bus poll thread. */
1229 if (ap_thread_flag) {
1230 rc = ap_poll_thread_start();
1231 if (rc)
1232 goto out_work;
1233 }
1234
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001235 rc = register_pm_notifier(&ap_power_notifier);
1236 if (rc)
1237 goto out_pm;
1238
Martin Schwidefsky8139b892015-07-27 12:47:40 +02001239 queue_work(system_long_wq, &ap_scan_work);
Sascha Silbee3877532015-10-27 18:29:52 +01001240 initialised = true;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001241
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001242 return 0;
1243
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001244out_pm:
1245 ap_poll_thread_stop();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001246out_work:
Felix Beckfe137232008-07-14 09:59:08 +02001247 hrtimer_cancel(&ap_poll_timer);
Mark McLoughlin035da162008-12-15 12:58:29 +00001248 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001249out_bus:
1250 while (i--)
1251 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1252 bus_unregister(&ap_bus_type);
1253out:
Ralph Wuerthner85eca852006-12-08 15:54:07 +01001254 unregister_reset_call(&ap_reset_call);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001255 if (ap_using_interrupts())
1256 unregister_adapter_interrupt(&ap_airq);
Wei Yongjun33c388b2016-07-31 11:47:06 +02001257out_free:
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001258 kfree(ap_configuration);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001259 return rc;
1260}
Paul Gortmaker50a0d462017-02-09 09:48:10 -05001261device_initcall(ap_module_init);