blob: 9f5a201c4c87861f6b77c6081b03a8d58d571366 [file] [log] [blame]
Greg Kroah-Hartman812141a2017-11-14 18:38:01 +01001// SPDX-License-Identifier: GPL-2.0+
Martin Schwidefsky1534c382006-09-20 15:58:25 +02002/*
Holger Dengler75014552012-08-28 16:41:50 +02003 * Copyright IBM Corp. 2006, 2012
Martin Schwidefsky1534c382006-09-20 15:58:25 +02004 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Ralph Wuerthner <rwuerthn@de.ibm.com>
Felix Beckcb17a632008-12-25 13:38:41 +01007 * Felix Beck <felix.beck@de.ibm.com>
Holger Dengler6bed05b2011-07-24 10:48:25 +02008 * Holger Dengler <hd@linux.vnet.ibm.com>
Martin Schwidefsky1534c382006-09-20 15:58:25 +02009 *
10 * Adjunct processor bus.
Martin Schwidefsky1534c382006-09-20 15:58:25 +020011 */
12
Martin Schwidefsky136f7a12008-12-25 13:39:46 +010013#define KMSG_COMPONENT "ap"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
Holger Dengler62d146f2011-01-05 12:47:38 +010016#include <linux/kernel_stat.h>
Paul Gortmaker50a0d462017-02-09 09:48:10 -050017#include <linux/moduleparam.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020018#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/interrupt.h>
22#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020024#include <linux/notifier.h>
25#include <linux/kthread.h>
26#include <linux/mutex.h>
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +020027#include <linux/suspend.h>
Felix Beckcb17a632008-12-25 13:38:41 +010028#include <asm/airq.h>
Arun Sharma600634972011-07-26 16:09:06 -070029#include <linux/atomic.h>
Felix Beckcb17a632008-12-25 13:38:41 +010030#include <asm/isc.h>
Felix Beckfe137232008-07-14 09:59:08 +020031#include <linux/hrtimer.h>
32#include <linux/ktime.h>
David Howellsa0616cd2012-03-28 18:30:02 +010033#include <asm/facility.h>
Kees Cook5d26a102014-11-20 17:05:53 -080034#include <linux/crypto.h>
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020035#include <linux/mod_devicetable.h>
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010036#include <linux/debugfs.h>
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +020037#include <linux/ctype.h>
Martin Schwidefsky1534c382006-09-20 15:58:25 +020038
39#include "ap_bus.h"
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010040#include "ap_debug.h"
Martin Schwidefsky1534c382006-09-20 15:58:25 +020041
Felix Beck1749a812008-04-17 07:46:28 +020042/*
Paul Gortmaker50a0d462017-02-09 09:48:10 -050043 * Module parameters; note though this file itself isn't modular.
Martin Schwidefsky1534c382006-09-20 15:58:25 +020044 */
45int ap_domain_index = -1; /* Adjunct Processor Domain Index */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +020046static DEFINE_SPINLOCK(ap_domain_lock);
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020047module_param_named(domain, ap_domain_index, int, 0440);
Martin Schwidefsky1534c382006-09-20 15:58:25 +020048MODULE_PARM_DESC(domain, "domain index for ap devices");
49EXPORT_SYMBOL(ap_domain_index);
50
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020051static int ap_thread_flag;
52module_param_named(poll_thread, ap_thread_flag, int, 0440);
Felix Beckb90b34c2008-02-09 18:24:30 +010053MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
Martin Schwidefsky1534c382006-09-20 15:58:25 +020054
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +020055static char *apm_str;
56module_param_named(apmask, apm_str, charp, 0440);
57MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
58
59static char *aqm_str;
60module_param_named(aqmask, aqm_str, charp, 0440);
61MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
62
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020063static struct device *ap_root_device;
64
65DEFINE_SPINLOCK(ap_list_lock);
66LIST_HEAD(ap_card_list);
67
Harald Freudenberger00fab232018-09-17 16:18:41 +020068/* Default permissions (ioctl, card and domain masking) */
69struct ap_perms ap_perms;
70EXPORT_SYMBOL(ap_perms);
71DEFINE_MUTEX(ap_perms_mutex);
72EXPORT_SYMBOL(ap_perms_mutex);
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +020073
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);
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020096static struct task_struct *ap_poll_kthread;
Martin Schwidefsky1534c382006-09-20 15:58:25 +020097static 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;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +0200100/*
101 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
102 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
103 */
Felix Beckfe137232008-07-14 09:59:08 +0200104static unsigned long long poll_timeout = 250000;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200105
Felix Beck772f5472009-06-22 12:08:16 +0200106/* Suspend flag */
107static int ap_suspend_flag;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200108/* Maximum domain id */
109static int ap_max_domain_id;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +0200110/*
111 * Flag to check if domain was set through module parameter domain=. This is
Felix Beck5314af62009-09-22 22:58:51 +0200112 * important when supsend and resume is done in a z/VM environment where the
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +0200113 * domain might change.
114 */
115static int user_set_domain;
Felix Beck772f5472009-06-22 12:08:16 +0200116static struct bus_type ap_bus_type;
117
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200118/* Adapter interrupt definitions */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200119static void ap_interrupt_handler(struct airq_struct *airq);
120
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200121static int ap_airq_flag;
122
123static struct airq_struct ap_airq = {
124 .handler = ap_interrupt_handler,
125 .isc = AP_ISC,
126};
127
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200128/**
Felix Beckcb17a632008-12-25 13:38:41 +0100129 * ap_using_interrupts() - Returns non-zero if interrupt support is
130 * available.
131 */
132static inline int ap_using_interrupts(void)
133{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200134 return ap_airq_flag;
Felix Beckcb17a632008-12-25 13:38:41 +0100135}
136
137/**
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200138 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
139 *
140 * Returns the address of the local-summary-indicator of the adapter
141 * interrupt handler for AP, or NULL if adapter interrupts are not
142 * available.
143 */
144void *ap_airq_ptr(void)
145{
146 if (ap_using_interrupts())
147 return ap_airq.lsi_ptr;
148 return NULL;
149}
150
151/**
Felix Beckcb17a632008-12-25 13:38:41 +0100152 * ap_interrupts_available(): Test if AP interrupts are available.
153 *
154 * Returns 1 if AP interrupts are available.
155 */
156static int ap_interrupts_available(void)
157{
Heiko Carstens86cd7412015-02-14 11:23:21 +0100158 return test_facility(65);
Felix Beckcb17a632008-12-25 13:38:41 +0100159}
160
161/**
Holger Dengler75014552012-08-28 16:41:50 +0200162 * ap_configuration_available(): Test if AP configuration
163 * information is available.
164 *
165 * Returns 1 if AP configuration information is available.
166 */
167static int ap_configuration_available(void)
168{
Heiko Carstens86cd7412015-02-14 11:23:21 +0100169 return test_facility(12);
Holger Dengler75014552012-08-28 16:41:50 +0200170}
171
172/**
Tony Krowiake7fc5142016-11-08 07:09:13 +0100173 * ap_apft_available(): Test if AP facilities test (APFT)
174 * facility is available.
175 *
176 * Returns 1 if APFT is is available.
177 */
178static int ap_apft_available(void)
179{
180 return test_facility(15);
181}
182
Harald Freudenberger9a564102017-10-16 12:28:35 +0200183/*
184 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
185 *
186 * Returns 1 if the QACT subfunction is available.
187 */
188static inline int ap_qact_available(void)
189{
190 if (ap_configuration)
191 return ap_configuration->qact;
192 return 0;
193}
194
Harald Freudenberger050349b2016-11-08 11:54:28 +0100195/*
196 * ap_query_configuration(): Fetch cryptographic config info
197 *
198 * Returns the ap configuration info fetched via PQAP(QCI).
199 * On success 0 is returned, on failure a negative errno
200 * is returned, e.g. if the PQAP(QCI) instruction is not
201 * available, the return value will be -EOPNOTSUPP.
202 */
Harald Freudenbergerf1b0a4342018-06-12 15:42:36 +0200203static inline int ap_query_configuration(struct ap_config_info *info)
Heiko Carstens11d37672016-06-20 14:00:27 +0200204{
Harald Freudenberger050349b2016-11-08 11:54:28 +0100205 if (!ap_configuration_available())
Heiko Carstens11d37672016-06-20 14:00:27 +0200206 return -EOPNOTSUPP;
Harald Freudenberger050349b2016-11-08 11:54:28 +0100207 if (!info)
208 return -EINVAL;
209 return ap_qci(info);
Heiko Carstens11d37672016-06-20 14:00:27 +0200210}
Harald Freudenberger050349b2016-11-08 11:54:28 +0100211EXPORT_SYMBOL(ap_query_configuration);
Heiko Carstens11d37672016-06-20 14:00:27 +0200212
Holger Dengler6bed05b2011-07-24 10:48:25 +0200213/**
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200214 * ap_init_configuration(): Allocate and query configuration array.
215 */
216static void ap_init_configuration(void)
217{
218 if (!ap_configuration_available())
219 return;
220
221 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
222 if (!ap_configuration)
223 return;
Harald Freudenberger050349b2016-11-08 11:54:28 +0100224 if (ap_query_configuration(ap_configuration) != 0) {
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200225 kfree(ap_configuration);
226 ap_configuration = NULL;
227 return;
228 }
229}
230
231/*
232 * ap_test_config(): helper function to extract the nrth bit
233 * within the unsigned int array field.
234 */
235static inline int ap_test_config(unsigned int *field, unsigned int nr)
236{
237 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
238}
239
240/*
241 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
242 * @id AP card ID
243 *
244 * Returns 0 if the card is not configured
245 * 1 if the card is configured or
246 * if the configuration information is not available
247 */
248static inline int ap_test_config_card_id(unsigned int id)
249{
250 if (!ap_configuration) /* QCI not supported */
251 return 1;
252 return ap_test_config(ap_configuration->apm, id);
253}
254
255/*
256 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
257 * @domain AP usage domain ID
258 *
259 * Returns 0 if the usage domain is not configured
260 * 1 if the usage domain is configured or
261 * if the configuration information is not available
262 */
263static inline int ap_test_config_domain(unsigned int domain)
264{
265 if (!ap_configuration) /* QCI not supported */
266 return domain < 16;
267 return ap_test_config(ap_configuration->aqm, domain);
268}
269
270/**
Felix Beck1749a812008-04-17 07:46:28 +0200271 * ap_query_queue(): Check if an AP queue is available.
272 * @qid: The AP queue number
273 * @queue_depth: Pointer to queue depth value
274 * @device_type: Pointer to device type value
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200275 * @facilities: Pointer to facility indicator
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200276 */
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200277static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
278 unsigned int *facilities)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200279{
280 struct ap_queue_status status;
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200281 unsigned long info;
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200282 int nd;
283
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200284 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200285 return -ENODEV;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200286
Tony Krowiake7fc5142016-11-08 07:09:13 +0100287 status = ap_test_queue(qid, ap_apft_available(), &info);
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200288 switch (status.response_code) {
289 case AP_RESPONSE_NORMAL:
Martin Schwidefsky6acbe212015-06-26 15:40:41 +0200290 *queue_depth = (int)(info & 0xff);
291 *device_type = (int)((info >> 24) & 0xff);
292 *facilities = (unsigned int)(info >> 32);
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200293 /* Update maximum domain id */
294 nd = (info >> 16) & 0xff;
Ingo Tuchschererc1c13682016-11-02 10:23:24 +0100295 /* if N bit is available, z13 and newer */
Martin Schwidefsky889875a2015-06-26 16:55:35 +0200296 if ((info & (1UL << 57)) && nd > 0)
297 ap_max_domain_id = nd;
Ingo Tuchschererc1c13682016-11-02 10:23:24 +0100298 else /* older machine types */
299 ap_max_domain_id = 15;
Harald Freudenberger14878422016-10-27 08:57:39 +0200300 switch (*device_type) {
301 /* For CEX2 and CEX3 the available functions
302 * are not refrected by the facilities bits.
303 * Instead it is coded into the type. So here
304 * modify the function bits based on the type.
305 */
306 case AP_DEVICE_TYPE_CEX2A:
307 case AP_DEVICE_TYPE_CEX3A:
308 *facilities |= 0x08000000;
309 break;
310 case AP_DEVICE_TYPE_CEX2C:
311 case AP_DEVICE_TYPE_CEX3C:
312 *facilities |= 0x10000000;
313 break;
314 default:
315 break;
316 }
Ingo Tuchschererc50a1602015-06-17 16:19:15 +0200317 return 0;
318 case AP_RESPONSE_Q_NOT_AVAIL:
319 case AP_RESPONSE_DECONFIGURED:
320 case AP_RESPONSE_CHECKSTOPPED:
321 case AP_RESPONSE_INVALID_ADDRESS:
322 return -ENODEV;
323 case AP_RESPONSE_RESET_IN_PROGRESS:
324 case AP_RESPONSE_OTHERWISE_CHANGED:
325 case AP_RESPONSE_BUSY:
326 return -EBUSY;
327 default:
328 BUG();
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200329 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200330}
331
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200332void ap_wait(enum ap_wait wait)
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200333{
334 ktime_t hr_time;
335
336 switch (wait) {
337 case AP_WAIT_AGAIN:
338 case AP_WAIT_INTERRUPT:
339 if (ap_using_interrupts())
340 break;
341 if (ap_poll_kthread) {
342 wake_up(&ap_poll_wait);
343 break;
344 }
345 /* Fall through */
346 case AP_WAIT_TIMEOUT:
347 spin_lock_bh(&ap_poll_timer_lock);
348 if (!hrtimer_is_queued(&ap_poll_timer)) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100349 hr_time = poll_timeout;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200350 hrtimer_forward_now(&ap_poll_timer, hr_time);
351 hrtimer_restart(&ap_poll_timer);
352 }
353 spin_unlock_bh(&ap_poll_timer_lock);
354 break;
355 case AP_WAIT_NONE:
356 default:
357 break;
358 }
359}
360
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200361/**
362 * ap_request_timeout(): Handling of request timeouts
Kees Cookcefbeb52017-10-25 03:27:37 -0700363 * @t: timer making this callback
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200364 *
365 * Handles request timeouts.
366 */
Kees Cookcefbeb52017-10-25 03:27:37 -0700367void ap_request_timeout(struct timer_list *t)
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200368{
Kees Cookcefbeb52017-10-25 03:27:37 -0700369 struct ap_queue *aq = from_timer(aq, t, timeout);
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200370
371 if (ap_suspend_flag)
372 return;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200373 spin_lock_bh(&aq->lock);
374 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
375 spin_unlock_bh(&aq->lock);
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200376}
377
378/**
379 * ap_poll_timeout(): AP receive polling for finished AP requests.
380 * @unused: Unused pointer.
381 *
382 * Schedules the AP tasklet using a high resolution timer.
383 */
384static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
385{
386 if (!ap_suspend_flag)
387 tasklet_schedule(&ap_tasklet);
388 return HRTIMER_NORESTART;
389}
390
391/**
392 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
393 * @airq: pointer to adapter interrupt descriptor
394 */
395static void ap_interrupt_handler(struct airq_struct *airq)
396{
397 inc_irq_stat(IRQIO_APB);
398 if (!ap_suspend_flag)
399 tasklet_schedule(&ap_tasklet);
400}
401
402/**
403 * ap_tasklet_fn(): Tasklet to poll all AP devices.
404 * @dummy: Unused variable
405 *
406 * Poll all AP devices on the bus.
407 */
408static void ap_tasklet_fn(unsigned long dummy)
409{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200410 struct ap_card *ac;
411 struct ap_queue *aq;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200412 enum ap_wait wait = AP_WAIT_NONE;
413
414 /* Reset the indicator if interrupts are used. Thus new interrupts can
415 * be received. Doing it in the beginning of the tasklet is therefor
416 * important that no requests on any AP get lost.
417 */
418 if (ap_using_interrupts())
419 xchg(ap_airq.lsi_ptr, 0);
420
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200421 spin_lock_bh(&ap_list_lock);
422 for_each_ap_card(ac) {
423 for_each_ap_queue(aq, ac) {
424 spin_lock_bh(&aq->lock);
425 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
426 spin_unlock_bh(&aq->lock);
427 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200428 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200429 spin_unlock_bh(&ap_list_lock);
430
431 ap_wait(wait);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200432}
433
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200434static int ap_pending_requests(void)
435{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200436 struct ap_card *ac;
437 struct ap_queue *aq;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200438
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200439 spin_lock_bh(&ap_list_lock);
440 for_each_ap_card(ac) {
441 for_each_ap_queue(aq, ac) {
442 if (aq->queue_count == 0)
443 continue;
444 spin_unlock_bh(&ap_list_lock);
445 return 1;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200446 }
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200447 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200448 spin_unlock_bh(&ap_list_lock);
449 return 0;
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200450}
451
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200452/**
453 * ap_poll_thread(): Thread that polls for finished requests.
454 * @data: Unused pointer
455 *
456 * AP bus poll thread. The purpose of this thread is to poll for
457 * finished requests in a loop if there is a "free" cpu - that is
458 * a cpu that doesn't have anything better to do. The polling stops
459 * as soon as there is another task or if all messages have been
460 * delivered.
461 */
462static int ap_poll_thread(void *data)
463{
464 DECLARE_WAITQUEUE(wait, current);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200465
466 set_user_nice(current, MAX_NICE);
467 set_freezable();
468 while (!kthread_should_stop()) {
469 add_wait_queue(&ap_poll_wait, &wait);
470 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200471 if (ap_suspend_flag || !ap_pending_requests()) {
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200472 schedule();
473 try_to_freeze();
474 }
475 set_current_state(TASK_RUNNING);
476 remove_wait_queue(&ap_poll_wait, &wait);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200477 if (need_resched()) {
478 schedule();
479 try_to_freeze();
480 continue;
481 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200482 ap_tasklet_fn(0);
Martin Schwidefsky9af3e042016-09-21 14:12:53 +0200483 }
484
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200485 return 0;
486}
487
488static int ap_poll_thread_start(void)
489{
490 int rc;
491
492 if (ap_using_interrupts() || ap_poll_kthread)
493 return 0;
494 mutex_lock(&ap_poll_thread_mutex);
495 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
Gustavo A. R. Silva9c705202018-07-18 23:42:05 -0500496 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200497 if (rc)
498 ap_poll_kthread = NULL;
499 mutex_unlock(&ap_poll_thread_mutex);
500 return rc;
501}
502
503static void ap_poll_thread_stop(void)
504{
505 if (!ap_poll_kthread)
506 return;
507 mutex_lock(&ap_poll_thread_mutex);
508 kthread_stop(ap_poll_kthread);
509 ap_poll_kthread = NULL;
510 mutex_unlock(&ap_poll_thread_mutex);
511}
512
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200513#define is_card_dev(x) ((x)->parent == ap_root_device)
514#define is_queue_dev(x) ((x)->parent != ap_root_device)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200515
516/**
Felix Beck1749a812008-04-17 07:46:28 +0200517 * ap_bus_match()
518 * @dev: Pointer to device
519 * @drv: Pointer to device_driver
520 *
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200521 * AP bus driver registration/unregistration.
522 */
523static int ap_bus_match(struct device *dev, struct device_driver *drv)
524{
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200525 struct ap_driver *ap_drv = to_ap_drv(drv);
526 struct ap_device_id *id;
527
Felix Beck1749a812008-04-17 07:46:28 +0200528 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200529 * Compare device type of the device with the list of
530 * supported types of the device_driver.
531 */
532 for (id = ap_drv->ids; id->match_flags; id++) {
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200533 if (is_card_dev(dev) &&
534 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
535 id->dev_type == to_ap_dev(dev)->device_type)
536 return 1;
537 if (is_queue_dev(dev) &&
538 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
539 id->dev_type == to_ap_dev(dev)->device_type)
540 return 1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200541 }
542 return 0;
543}
544
545/**
Felix Beck1749a812008-04-17 07:46:28 +0200546 * ap_uevent(): Uevent function for AP devices.
547 * @dev: Pointer to device
548 * @env: Pointer to kobj_uevent_env
549 *
550 * It sets up a single environment variable DEV_TYPE which contains the
551 * hardware device type.
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200552 */
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +0200553static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200554{
555 struct ap_device *ap_dev = to_ap_dev(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200556 int retval = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200557
558 if (!ap_dev)
559 return -ENODEV;
560
561 /* Set up DEV_TYPE environment variable. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200562 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700563 if (retval)
564 return retval;
565
Cornelia Huck66a4263b2006-12-04 15:40:10 +0100566 /* Add MODALIAS= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200567 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
Eric Rannaudbf624562007-03-30 22:23:12 -0700568
Eric Rannaudbf624562007-03-30 22:23:12 -0700569 return retval;
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200570}
571
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100572static int ap_dev_suspend(struct device *dev)
Felix Beck772f5472009-06-22 12:08:16 +0200573{
574 struct ap_device *ap_dev = to_ap_dev(dev);
Felix Beck772f5472009-06-22 12:08:16 +0200575
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200576 if (ap_dev->drv && ap_dev->drv->suspend)
577 ap_dev->drv->suspend(ap_dev);
578 return 0;
579}
580
581static int ap_dev_resume(struct device *dev)
582{
583 struct ap_device *ap_dev = to_ap_dev(dev);
584
585 if (ap_dev->drv && ap_dev->drv->resume)
586 ap_dev->drv->resume(ap_dev);
Felix Beck772f5472009-06-22 12:08:16 +0200587 return 0;
588}
589
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200590static void ap_bus_suspend(void)
591{
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +0200592 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100593
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200594 ap_suspend_flag = 1;
595 /*
596 * Disable scanning for devices, thus we do not want to scan
597 * for them after removing.
598 */
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200599 flush_work(&ap_scan_work);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200600 tasklet_disable(&ap_tasklet);
601}
602
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200603static int __ap_card_devices_unregister(struct device *dev, void *dummy)
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200604{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200605 if (is_card_dev(dev))
606 device_unregister(dev);
607 return 0;
608}
609
610static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
611{
612 if (is_queue_dev(dev))
613 device_unregister(dev);
614 return 0;
615}
616
617static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
618{
619 if (is_queue_dev(dev) &&
620 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
621 device_unregister(dev);
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200622 return 0;
623}
624
625static void ap_bus_resume(void)
626{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +0200627 int rc;
Felix Beck772f5472009-06-22 12:08:16 +0200628
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +0200629 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100630
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200631 /* remove all queue devices */
632 bus_for_each_dev(&ap_bus_type, NULL, NULL,
633 __ap_queue_devices_unregister);
634 /* remove all card devices */
635 bus_for_each_dev(&ap_bus_type, NULL, NULL,
636 __ap_card_devices_unregister);
637
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200638 /* Reset thin interrupt setting */
639 if (ap_interrupts_available() && !ap_using_interrupts()) {
640 rc = register_adapter_interrupt(&ap_airq);
641 ap_airq_flag = (rc == 0);
Felix Beck5314af62009-09-22 22:58:51 +0200642 }
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200643 if (!ap_interrupts_available() && ap_using_interrupts()) {
644 unregister_adapter_interrupt(&ap_airq);
645 ap_airq_flag = 0;
646 }
647 /* Reset domain */
648 if (!user_set_domain)
649 ap_domain_index = -1;
650 /* Get things going again */
651 ap_suspend_flag = 0;
652 if (ap_airq_flag)
653 xchg(ap_airq.lsi_ptr, 0);
654 tasklet_enable(&ap_tasklet);
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200655 queue_work(system_long_wq, &ap_scan_work);
Felix Beck772f5472009-06-22 12:08:16 +0200656}
657
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200658static int ap_power_event(struct notifier_block *this, unsigned long event,
659 void *ptr)
660{
661 switch (event) {
662 case PM_HIBERNATION_PREPARE:
663 case PM_SUSPEND_PREPARE:
664 ap_bus_suspend();
665 break;
666 case PM_POST_HIBERNATION:
667 case PM_POST_SUSPEND:
668 ap_bus_resume();
669 break;
670 default:
671 break;
672 }
673 return NOTIFY_DONE;
674}
675static struct notifier_block ap_power_notifier = {
676 .notifier_call = ap_power_event,
677};
678
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200679static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100680
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200681static struct bus_type ap_bus_type = {
682 .name = "ap",
683 .match = &ap_bus_match,
684 .uevent = &ap_uevent,
Lars-Peter Clausen3e488c92016-11-22 22:06:00 +0100685 .pm = &ap_bus_pm_ops,
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200686};
687
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +0200688static int __ap_revise_reserved(struct device *dev, void *dummy)
689{
690 int rc, card, queue, devres, drvres;
691
692 if (is_queue_dev(dev)) {
693 card = AP_QID_CARD(to_ap_queue(dev)->qid);
694 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
695 mutex_lock(&ap_perms_mutex);
696 devres = test_bit_inv(card, ap_perms.apm)
697 && test_bit_inv(queue, ap_perms.aqm);
698 mutex_unlock(&ap_perms_mutex);
699 drvres = to_ap_drv(dev->driver)->flags
700 & AP_DRIVER_FLAG_DEFAULT;
701 if (!!devres != !!drvres) {
702 AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
703 card, queue);
704 rc = device_reprobe(dev);
705 }
706 }
707
708 return 0;
709}
710
711static void ap_bus_revise_bindings(void)
712{
713 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
714}
715
716int ap_owned_by_def_drv(int card, int queue)
717{
718 int rc = 0;
719
720 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
721 return -EINVAL;
722
723 mutex_lock(&ap_perms_mutex);
724
725 if (test_bit_inv(card, ap_perms.apm)
726 && test_bit_inv(queue, ap_perms.aqm))
727 rc = 1;
728
729 mutex_unlock(&ap_perms_mutex);
730
731 return rc;
732}
733EXPORT_SYMBOL(ap_owned_by_def_drv);
734
735int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
736 unsigned long *aqm)
737{
738 int card, queue, rc = 0;
739
740 mutex_lock(&ap_perms_mutex);
741
742 for (card = 0; !rc && card < AP_DEVICES; card++)
743 if (test_bit_inv(card, apm) &&
744 test_bit_inv(card, ap_perms.apm))
745 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
746 if (test_bit_inv(queue, aqm) &&
747 test_bit_inv(queue, ap_perms.aqm))
748 rc = 1;
749
750 mutex_unlock(&ap_perms_mutex);
751
752 return rc;
753}
754EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
755
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200756static int ap_device_probe(struct device *dev)
757{
758 struct ap_device *ap_dev = to_ap_dev(dev);
759 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +0200760 int card, queue, devres, drvres, rc;
761
762 if (is_queue_dev(dev)) {
763 /*
764 * If the apqn is marked as reserved/used by ap bus and
765 * default drivers, only probe with drivers with the default
766 * flag set. If it is not marked, only probe with drivers
767 * with the default flag not set.
768 */
769 card = AP_QID_CARD(to_ap_queue(dev)->qid);
770 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
771 mutex_lock(&ap_perms_mutex);
772 devres = test_bit_inv(card, ap_perms.apm)
773 && test_bit_inv(queue, ap_perms.aqm);
774 mutex_unlock(&ap_perms_mutex);
775 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
776 if (!!devres != !!drvres)
777 return -ENODEV;
Harald Freudenberger104f7082018-11-09 14:59:24 +0100778 /* (re-)init queue's state machine */
779 ap_queue_reinit_state(to_ap_queue(dev));
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +0200780 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200781
Harald Freudenbergere3850502017-05-24 10:26:29 +0200782 /* Add queue/card to list of active queues/cards */
783 spin_lock_bh(&ap_list_lock);
784 if (is_card_dev(dev))
785 list_add(&to_ap_card(dev)->list, &ap_card_list);
786 else
787 list_add(&to_ap_queue(dev)->list,
788 &to_ap_queue(dev)->card->queues);
789 spin_unlock_bh(&ap_list_lock);
790
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200791 ap_dev->drv = ap_drv;
792 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
Harald Freudenbergere3850502017-05-24 10:26:29 +0200793
794 if (rc) {
795 spin_lock_bh(&ap_list_lock);
796 if (is_card_dev(dev))
797 list_del_init(&to_ap_card(dev)->list);
798 else
799 list_del_init(&to_ap_queue(dev)->list);
800 spin_unlock_bh(&ap_list_lock);
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +0200801 ap_dev->drv = NULL;
Harald Freudenbergere3850502017-05-24 10:26:29 +0200802 }
803
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200804 return rc;
805}
806
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200807static int ap_device_remove(struct device *dev)
808{
809 struct ap_device *ap_dev = to_ap_dev(dev);
810 struct ap_driver *ap_drv = ap_dev->drv;
811
Harald Freudenberger104f7082018-11-09 14:59:24 +0100812 if (is_queue_dev(dev))
813 ap_queue_remove(to_ap_queue(dev));
Harald Freudenbergere3850502017-05-24 10:26:29 +0200814 if (ap_drv->remove)
815 ap_drv->remove(ap_dev);
816
817 /* Remove queue/card from list of active queues/cards */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200818 spin_lock_bh(&ap_list_lock);
819 if (is_card_dev(dev))
820 list_del_init(&to_ap_card(dev)->list);
821 else
822 list_del_init(&to_ap_queue(dev)->list);
823 spin_unlock_bh(&ap_list_lock);
Harald Freudenbergere3850502017-05-24 10:26:29 +0200824
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200825 return 0;
826}
827
828int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
829 char *name)
830{
831 struct device_driver *drv = &ap_drv->driver;
832
Sascha Silbee3877532015-10-27 18:29:52 +0100833 if (!initialised)
834 return -ENODEV;
835
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200836 drv->bus = &ap_bus_type;
837 drv->probe = ap_device_probe;
838 drv->remove = ap_device_remove;
839 drv->owner = owner;
840 drv->name = name;
841 return driver_register(drv);
842}
843EXPORT_SYMBOL(ap_driver_register);
844
845void ap_driver_unregister(struct ap_driver *ap_drv)
846{
847 driver_unregister(&ap_drv->driver);
848}
849EXPORT_SYMBOL(ap_driver_unregister);
850
Holger Denglerdabecb22012-09-10 21:34:26 +0200851void ap_bus_force_rescan(void)
852{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +0200853 if (ap_suspend_flag)
854 return;
Ingo Tuchscherer56bbe682013-04-12 17:52:08 +0200855 /* processing a asynchronous bus rescan */
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +0200856 del_timer(&ap_config_timer);
Martin Schwidefsky8139b892015-07-27 12:47:40 +0200857 queue_work(system_long_wq, &ap_scan_work);
858 flush_work(&ap_scan_work);
Holger Denglerdabecb22012-09-10 21:34:26 +0200859}
860EXPORT_SYMBOL(ap_bus_force_rescan);
861
Felix Beck1749a812008-04-17 07:46:28 +0200862/*
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200863 * hex2bitmap() - parse hex mask string and set bitmap.
864 * Valid strings are "0x012345678" with at least one valid hex number.
865 * Rest of the bitmap to the right is padded with 0. No spaces allowed
866 * within the string, the leading 0x may be omitted.
867 * Returns the bitmask with exactly the bits set as given by the hex
868 * string (both in big endian order).
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200869 */
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +0200870static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
871{
872 int i, n, b;
873
874 /* bits needs to be a multiple of 8 */
875 if (bits & 0x07)
876 return -EINVAL;
877
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +0200878 if (str[0] == '0' && str[1] == 'x')
879 str++;
880 if (*str == 'x')
881 str++;
882
883 for (i = 0; isxdigit(*str) && i < bits; str++) {
884 b = hex_to_bin(*str);
885 for (n = 0; n < 4; n++)
886 if (b & (0x08 >> n))
887 set_bit_inv(i + n, bitmap);
888 i += 4;
889 }
890
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200891 if (*str == '\n')
892 str++;
893 if (*str)
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +0200894 return -EINVAL;
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200895 return 0;
896}
897
898/*
Martin Schwidefskyfa108f92018-09-05 07:45:11 +0200899 * modify_bitmap() - parse bitmask argument and modify an existing
900 * bit mask accordingly. A concatenation (done with ',') of these
901 * terms is recognized:
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200902 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
903 * <bitnr> may be any valid number (hex, decimal or octal) in the range
904 * 0...bits-1; the leading + or - is required. Here are some examples:
905 * +0-15,+32,-128,-0xFF
906 * -0-255,+1-16,+0x128
907 * +1,+2,+3,+4,-5,-7-10
Martin Schwidefskyfa108f92018-09-05 07:45:11 +0200908 * Returns the new bitmap after all changes have been applied. Every
909 * positive value in the string will set a bit and every negative value
910 * in the string will clear a bit. As a bit may be touched more than once,
911 * the last 'operation' wins:
912 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
913 * cleared again. All other bits are unmodified.
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200914 */
Martin Schwidefskyfa108f92018-09-05 07:45:11 +0200915static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200916{
917 int a, i, z;
918 char *np, sign;
919
920 /* bits needs to be a multiple of 8 */
921 if (bits & 0x07)
922 return -EINVAL;
923
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200924 while (*str) {
925 sign = *str++;
926 if (sign != '+' && sign != '-')
927 return -EINVAL;
928 a = z = simple_strtoul(str, &np, 0);
929 if (str == np || a >= bits)
930 return -EINVAL;
931 str = np;
932 if (*str == '-') {
933 z = simple_strtoul(++str, &np, 0);
934 if (str == np || a > z || z >= bits)
935 return -EINVAL;
936 str = np;
937 }
938 for (i = a; i <= z; i++)
Martin Schwidefskyfa108f92018-09-05 07:45:11 +0200939 if (sign == '+')
940 set_bit_inv(i, bitmap);
941 else
942 clear_bit_inv(i, bitmap);
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200943 while (*str == ',' || *str == '\n')
944 str++;
945 }
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +0200946
947 return 0;
948}
949
Harald Freudenberger00fab232018-09-17 16:18:41 +0200950int ap_parse_mask_str(const char *str,
951 unsigned long *bitmap, int bits,
952 struct mutex *lock)
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200953{
Martin Schwidefskyfa108f92018-09-05 07:45:11 +0200954 unsigned long *newmap, size;
955 int rc;
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200956
957 /* bits needs to be a multiple of 8 */
958 if (bits & 0x07)
959 return -EINVAL;
960
Martin Schwidefskyfa108f92018-09-05 07:45:11 +0200961 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
962 newmap = kmalloc(size, GFP_KERNEL);
963 if (!newmap)
964 return -ENOMEM;
965 if (mutex_lock_interruptible(lock)) {
966 kfree(newmap);
967 return -ERESTARTSYS;
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200968 }
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200969
Martin Schwidefskyfa108f92018-09-05 07:45:11 +0200970 if (*str == '+' || *str == '-') {
971 memcpy(newmap, bitmap, size);
972 rc = modify_bitmap(str, newmap, bits);
973 } else {
974 memset(newmap, 0, size);
975 rc = hex2bitmap(str, newmap, bits);
976 }
977 if (rc == 0)
978 memcpy(bitmap, newmap, size);
979 mutex_unlock(lock);
980 kfree(newmap);
981 return rc;
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200982}
Harald Freudenberger00fab232018-09-17 16:18:41 +0200983EXPORT_SYMBOL(ap_parse_mask_str);
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +0200984
985/*
986 * AP bus attributes.
987 */
988
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200989static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
990{
991 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
992}
993
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +0200994static ssize_t ap_domain_store(struct bus_type *bus,
995 const char *buf, size_t count)
996{
997 int domain;
998
999 if (sscanf(buf, "%i\n", &domain) != 1 ||
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001000 domain < 0 || domain > ap_max_domain_id ||
1001 !test_bit_inv(domain, ap_perms.aqm))
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02001002 return -EINVAL;
1003 spin_lock_bh(&ap_domain_lock);
1004 ap_domain_index = domain;
1005 spin_unlock_bh(&ap_domain_lock);
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001006
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001007 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001008
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02001009 return count;
1010}
1011
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001012static BUS_ATTR_RW(ap_domain);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001013
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001014static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1015{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001016 if (!ap_configuration) /* QCI not supported */
1017 return snprintf(buf, PAGE_SIZE, "not supported\n");
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001018
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001019 return snprintf(buf, PAGE_SIZE,
1020 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001021 ap_configuration->adm[0], ap_configuration->adm[1],
1022 ap_configuration->adm[2], ap_configuration->adm[3],
1023 ap_configuration->adm[4], ap_configuration->adm[5],
1024 ap_configuration->adm[6], ap_configuration->adm[7]);
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001025}
1026
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001027static BUS_ATTR_RO(ap_control_domain_mask);
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001028
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001029static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1030{
1031 if (!ap_configuration) /* QCI not supported */
1032 return snprintf(buf, PAGE_SIZE, "not supported\n");
1033
1034 return snprintf(buf, PAGE_SIZE,
1035 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1036 ap_configuration->aqm[0], ap_configuration->aqm[1],
1037 ap_configuration->aqm[2], ap_configuration->aqm[3],
1038 ap_configuration->aqm[4], ap_configuration->aqm[5],
1039 ap_configuration->aqm[6], ap_configuration->aqm[7]);
1040}
1041
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001042static BUS_ATTR_RO(ap_usage_domain_mask);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001043
Harald Freudenbergeraa55bf52018-10-01 15:29:01 +02001044static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1045{
1046 if (!ap_configuration) /* QCI not supported */
1047 return snprintf(buf, PAGE_SIZE, "not supported\n");
1048
1049 return snprintf(buf, PAGE_SIZE,
1050 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1051 ap_configuration->apm[0], ap_configuration->apm[1],
1052 ap_configuration->apm[2], ap_configuration->apm[3],
1053 ap_configuration->apm[4], ap_configuration->apm[5],
1054 ap_configuration->apm[6], ap_configuration->apm[7]);
1055}
1056
1057static BUS_ATTR_RO(ap_adapter_mask);
1058
Felix Beckcb17a632008-12-25 13:38:41 +01001059static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1060{
1061 return snprintf(buf, PAGE_SIZE, "%d\n",
1062 ap_using_interrupts() ? 1 : 0);
1063}
1064
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001065static BUS_ATTR_RO(ap_interrupts);
Felix Beckcb17a632008-12-25 13:38:41 +01001066
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001067static ssize_t config_time_show(struct bus_type *bus, char *buf)
1068{
1069 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1070}
1071
1072static ssize_t config_time_store(struct bus_type *bus,
1073 const char *buf, size_t count)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001074{
1075 int time;
1076
1077 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1078 return -EINVAL;
1079 ap_config_time = time;
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001080 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001081 return count;
1082}
1083
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001084static BUS_ATTR_RW(config_time);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001085
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001086static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001087{
1088 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1089}
1090
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001091static ssize_t poll_thread_store(struct bus_type *bus,
1092 const char *buf, size_t count)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001093{
1094 int flag, rc;
1095
1096 if (sscanf(buf, "%d\n", &flag) != 1)
1097 return -EINVAL;
1098 if (flag) {
1099 rc = ap_poll_thread_start();
1100 if (rc)
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001101 count = rc;
1102 } else
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001103 ap_poll_thread_stop();
1104 return count;
1105}
1106
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001107static BUS_ATTR_RW(poll_thread);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001108
Felix Beckfe137232008-07-14 09:59:08 +02001109static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1110{
1111 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1112}
1113
1114static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1115 size_t count)
1116{
1117 unsigned long long time;
1118 ktime_t hr_time;
1119
1120 /* 120 seconds = maximum poll interval */
Felix Beckcb17a632008-12-25 13:38:41 +01001121 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1122 time > 120000000000ULL)
Felix Beckfe137232008-07-14 09:59:08 +02001123 return -EINVAL;
1124 poll_timeout = time;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001125 hr_time = poll_timeout;
Felix Beckfe137232008-07-14 09:59:08 +02001126
Ingo Tuchscherer8cc2af72015-04-28 10:31:44 +02001127 spin_lock_bh(&ap_poll_timer_lock);
1128 hrtimer_cancel(&ap_poll_timer);
1129 hrtimer_set_expires(&ap_poll_timer, hr_time);
1130 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1131 spin_unlock_bh(&ap_poll_timer_lock);
1132
Felix Beckfe137232008-07-14 09:59:08 +02001133 return count;
1134}
1135
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001136static BUS_ATTR_RW(poll_timeout);
Felix Beckfe137232008-07-14 09:59:08 +02001137
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001138static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1139{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001140 int max_domain_id;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001141
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001142 if (ap_configuration)
1143 max_domain_id = ap_max_domain_id ? : -1;
1144 else
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001145 max_domain_id = 15;
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001146 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1147}
1148
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001149static BUS_ATTR_RO(ap_max_domain_id);
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001150
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001151static ssize_t apmask_show(struct bus_type *bus, char *buf)
1152{
1153 int rc;
1154
1155 if (mutex_lock_interruptible(&ap_perms_mutex))
1156 return -ERESTARTSYS;
1157 rc = snprintf(buf, PAGE_SIZE,
1158 "0x%016lx%016lx%016lx%016lx\n",
1159 ap_perms.apm[0], ap_perms.apm[1],
1160 ap_perms.apm[2], ap_perms.apm[3]);
1161 mutex_unlock(&ap_perms_mutex);
1162
1163 return rc;
1164}
1165
1166static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1167 size_t count)
1168{
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +02001169 int rc;
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001170
Harald Freudenberger00fab232018-09-17 16:18:41 +02001171 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +02001172 if (rc)
1173 return rc;
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001174
1175 ap_bus_revise_bindings();
1176
1177 return count;
1178}
1179
1180static BUS_ATTR_RW(apmask);
1181
1182static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1183{
1184 int rc;
1185
1186 if (mutex_lock_interruptible(&ap_perms_mutex))
1187 return -ERESTARTSYS;
1188 rc = snprintf(buf, PAGE_SIZE,
1189 "0x%016lx%016lx%016lx%016lx\n",
1190 ap_perms.aqm[0], ap_perms.aqm[1],
1191 ap_perms.aqm[2], ap_perms.aqm[3]);
1192 mutex_unlock(&ap_perms_mutex);
1193
1194 return rc;
1195}
1196
1197static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1198 size_t count)
1199{
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +02001200 int rc;
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001201
Harald Freudenberger00fab232018-09-17 16:18:41 +02001202 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +02001203 if (rc)
1204 return rc;
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001205
1206 ap_bus_revise_bindings();
1207
1208 return count;
1209}
1210
1211static BUS_ATTR_RW(aqmask);
1212
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001213static struct bus_attribute *const ap_bus_attrs[] = {
1214 &bus_attr_ap_domain,
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001215 &bus_attr_ap_control_domain_mask,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001216 &bus_attr_ap_usage_domain_mask,
Harald Freudenbergeraa55bf52018-10-01 15:29:01 +02001217 &bus_attr_ap_adapter_mask,
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001218 &bus_attr_config_time,
1219 &bus_attr_poll_thread,
Felix Beckcb17a632008-12-25 13:38:41 +01001220 &bus_attr_ap_interrupts,
Felix Beckfe137232008-07-14 09:59:08 +02001221 &bus_attr_poll_timeout,
Ingo Tuchscherer5bc334b2015-01-23 13:27:04 +01001222 &bus_attr_ap_max_domain_id,
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001223 &bus_attr_apmask,
1224 &bus_attr_aqmask,
Felix Beckfe137232008-07-14 09:59:08 +02001225 NULL,
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001226};
1227
Holger Dengler75014552012-08-28 16:41:50 +02001228/**
Halil Pasic1c472d462018-09-17 15:23:03 +02001229 * ap_select_domain(): Select an AP domain if possible and we haven't
1230 * already done so before.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001231 */
Halil Pasic1c472d462018-09-17 15:23:03 +02001232static void ap_select_domain(void)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001233{
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001234 int count, max_count, best_domain;
1235 struct ap_queue_status status;
1236 int i, j;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001237
Felix Beck1749a812008-04-17 07:46:28 +02001238 /*
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001239 * We want to use a single domain. Either the one specified with
1240 * the "domain=" parameter or the domain with the maximum number
1241 * of devices.
1242 */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02001243 spin_lock_bh(&ap_domain_lock);
1244 if (ap_domain_index >= 0) {
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001245 /* Domain has already been selected. */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02001246 spin_unlock_bh(&ap_domain_lock);
Halil Pasic1c472d462018-09-17 15:23:03 +02001247 return;
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02001248 }
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001249 best_domain = -1;
1250 max_count = 0;
1251 for (i = 0; i < AP_DOMAINS; i++) {
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001252 if (!ap_test_config_domain(i) ||
1253 !test_bit_inv(i, ap_perms.aqm))
Holger Dengler75014552012-08-28 16:41:50 +02001254 continue;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001255 count = 0;
1256 for (j = 0; j < AP_DEVICES; j++) {
Holger Dengler75014552012-08-28 16:41:50 +02001257 if (!ap_test_config_card_id(j))
1258 continue;
Tony Krowiake7fc5142016-11-08 07:09:13 +01001259 status = ap_test_queue(AP_MKQID(j, i),
1260 ap_apft_available(),
1261 NULL);
Martin Schwidefsky6acbe212015-06-26 15:40:41 +02001262 if (status.response_code != AP_RESPONSE_NORMAL)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001263 continue;
1264 count++;
1265 }
1266 if (count > max_count) {
1267 max_count = count;
1268 best_domain = i;
1269 }
1270 }
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001271 if (best_domain >= 0) {
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001272 ap_domain_index = best_domain;
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001273 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001274 }
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02001275 spin_unlock_bh(&ap_domain_lock);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001276}
1277
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001278/*
Harald Freudenberger9a564102017-10-16 12:28:35 +02001279 * This function checks the type and returns either 0 for not
1280 * supported or the highest compatible type value (which may
1281 * include the input type value).
1282 */
1283static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1284{
1285 int comp_type = 0;
1286
1287 /* < CEX2A is not supported */
1288 if (rawtype < AP_DEVICE_TYPE_CEX2A)
1289 return 0;
1290 /* up to CEX6 known and fully supported */
1291 if (rawtype <= AP_DEVICE_TYPE_CEX6)
1292 return rawtype;
1293 /*
1294 * unknown new type > CEX6, check for compatibility
1295 * to the highest known and supported type which is
1296 * currently CEX6 with the help of the QACT function.
1297 */
1298 if (ap_qact_available()) {
1299 struct ap_queue_status status;
Harald Freudenberger56c5c682017-10-30 12:10:54 +01001300 union ap_qact_ap_info apinfo = {0};
Harald Freudenberger9a564102017-10-16 12:28:35 +02001301
1302 apinfo.mode = (func >> 26) & 0x07;
1303 apinfo.cat = AP_DEVICE_TYPE_CEX6;
1304 status = ap_qact(qid, 0, &apinfo);
1305 if (status.response_code == AP_RESPONSE_NORMAL
1306 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1307 && apinfo.cat <= AP_DEVICE_TYPE_CEX6)
1308 comp_type = apinfo.cat;
1309 }
1310 if (!comp_type)
1311 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
1312 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1313 else if (comp_type != rawtype)
1314 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
1315 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
1316 return comp_type;
1317}
1318
1319/*
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001320 * helper function to be used with bus_find_dev
1321 * matches for the card device with the given id
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001322 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001323static int __match_card_device_with_id(struct device *dev, void *data)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001324{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001325 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001326}
1327
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001328/* helper function to be used with bus_find_dev
1329 * matches for the queue device with a given qid
1330 */
1331static int __match_queue_device_with_qid(struct device *dev, void *data)
1332{
1333 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1334}
1335
1336/**
1337 * ap_scan_bus(): Scan the AP bus for new devices
1338 * Runs periodically, workqueue timer (ap_config_time)
1339 */
Al Viro4927b3f2006-12-06 19:18:20 +00001340static void ap_scan_bus(struct work_struct *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001341{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001342 struct ap_queue *aq;
1343 struct ap_card *ac;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001344 struct device *dev;
1345 ap_qid_t qid;
Harald Freudenberger9a564102017-10-16 12:28:35 +02001346 int comp_type, depth = 0, type = 0;
1347 unsigned int func = 0;
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001348 int rc, id, dom, borked, domains, defdomdevs = 0;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001349
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001350 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001351
Harald Freudenberger050349b2016-11-08 11:54:28 +01001352 ap_query_configuration(ap_configuration);
Halil Pasic1c472d462018-09-17 15:23:03 +02001353 ap_select_domain();
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001354
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001355 for (id = 0; id < AP_DEVICES; id++) {
1356 /* check if device is registered */
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001357 dev = bus_find_device(&ap_bus_type, NULL,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001358 (void *)(long) id,
1359 __match_card_device_with_id);
1360 ac = dev ? to_ap_card(dev) : NULL;
1361 if (!ap_test_config_card_id(id)) {
1362 if (dev) {
1363 /* Card device has been removed from
1364 * configuration, remove the belonging
1365 * queue devices.
1366 */
1367 bus_for_each_dev(&ap_bus_type, NULL,
1368 (void *)(long) id,
1369 __ap_queue_devices_with_id_unregister);
1370 /* now remove the card device */
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001371 device_unregister(dev);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001372 put_device(dev);
1373 }
1374 continue;
1375 }
1376 /* According to the configuration there should be a card
1377 * device, so check if there is at least one valid queue
1378 * and maybe create queue devices and the card device.
1379 */
1380 domains = 0;
1381 for (dom = 0; dom < AP_DOMAINS; dom++) {
1382 qid = AP_MKQID(id, dom);
1383 dev = bus_find_device(&ap_bus_type, NULL,
1384 (void *)(long) qid,
1385 __match_queue_device_with_qid);
1386 aq = dev ? to_ap_queue(dev) : NULL;
1387 if (!ap_test_config_domain(dom)) {
1388 if (dev) {
1389 /* Queue device exists but has been
1390 * removed from configuration.
1391 */
1392 device_unregister(dev);
1393 put_device(dev);
1394 }
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001395 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001396 }
Harald Freudenberger9a564102017-10-16 12:28:35 +02001397 rc = ap_query_queue(qid, &depth, &type, &func);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001398 if (dev) {
1399 spin_lock_bh(&aq->lock);
1400 if (rc == -ENODEV ||
1401 /* adapter reconfiguration */
Harald Freudenberger9a564102017-10-16 12:28:35 +02001402 (ac && ac->functions != func))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001403 aq->state = AP_STATE_BORKED;
1404 borked = aq->state == AP_STATE_BORKED;
1405 spin_unlock_bh(&aq->lock);
1406 if (borked) /* Remove broken device */
1407 device_unregister(dev);
1408 put_device(dev);
1409 if (!borked) {
1410 domains++;
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001411 if (dom == ap_domain_index)
1412 defdomdevs++;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001413 continue;
1414 }
1415 }
1416 if (rc)
1417 continue;
Harald Freudenberger9a564102017-10-16 12:28:35 +02001418 /* a new queue device is needed, check out comp type */
1419 comp_type = ap_get_compatible_type(qid, type, func);
1420 if (!comp_type)
1421 continue;
1422 /* maybe a card device needs to be created first */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001423 if (!ac) {
Harald Freudenberger9a564102017-10-16 12:28:35 +02001424 ac = ap_card_create(id, depth, type,
1425 comp_type, func);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001426 if (!ac)
1427 continue;
1428 ac->ap_dev.device.bus = &ap_bus_type;
1429 ac->ap_dev.device.parent = ap_root_device;
1430 dev_set_name(&ac->ap_dev.device,
1431 "card%02x", id);
1432 /* Register card with AP bus */
1433 rc = device_register(&ac->ap_dev.device);
1434 if (rc) {
1435 put_device(&ac->ap_dev.device);
1436 ac = NULL;
1437 break;
1438 }
1439 /* get it and thus adjust reference counter */
1440 get_device(&ac->ap_dev.device);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001441 }
1442 /* now create the new queue device */
Harald Freudenberger9a564102017-10-16 12:28:35 +02001443 aq = ap_queue_create(qid, comp_type);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001444 if (!aq)
1445 continue;
1446 aq->card = ac;
1447 aq->ap_dev.device.bus = &ap_bus_type;
1448 aq->ap_dev.device.parent = &ac->ap_dev.device;
1449 dev_set_name(&aq->ap_dev.device,
1450 "%02x.%04x", id, dom);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001451 /* Register device */
1452 rc = device_register(&aq->ap_dev.device);
1453 if (rc) {
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001454 put_device(&aq->ap_dev.device);
1455 continue;
1456 }
1457 domains++;
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001458 if (dom == ap_domain_index)
1459 defdomdevs++;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001460 } /* end domain loop */
1461 if (ac) {
1462 /* remove card dev if there are no queue devices */
1463 if (!domains)
1464 device_unregister(&ac->ap_dev.device);
1465 put_device(&ac->ap_dev.device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001466 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001467 } /* end device loop */
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001468
Halil Pasic1c472d462018-09-17 15:23:03 +02001469 if (ap_domain_index >= 0 && defdomdevs < 1)
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001470 AP_DBF(DBF_INFO,
1471 "no queue device with default domain %d available\n",
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001472 ap_domain_index);
1473
Martin Schwidefskyfcd0d1f2015-07-23 10:55:59 +02001474 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001475}
1476
Kees Cookcefbeb52017-10-25 03:27:37 -07001477static void ap_config_timeout(struct timer_list *unused)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001478{
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001479 if (ap_suspend_flag)
1480 return;
Martin Schwidefsky8139b892015-07-27 12:47:40 +02001481 queue_work(system_long_wq, &ap_scan_work);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001482}
1483
Harald Freudenbergerefda7ad2018-04-04 07:14:17 +02001484static int __init ap_debug_init(void)
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001485{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001486 ap_dbf_info = debug_register("ap", 1, 1,
1487 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1488 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1489 debug_set_level(ap_dbf_info, DBF_ERR);
1490
1491 return 0;
1492}
1493
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001494static void __init ap_perms_init(void)
1495{
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +02001496 /* all resources useable if no kernel parameter string given */
Harald Freudenberger00fab232018-09-17 16:18:41 +02001497 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001498 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1499 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1500
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +02001501 /* apm kernel parameter string */
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001502 if (apm_str) {
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +02001503 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
Harald Freudenberger00fab232018-09-17 16:18:41 +02001504 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1505 &ap_perms_mutex);
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001506 }
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001507
Harald Freudenberger3d8f60d32018-08-20 15:27:45 +02001508 /* aqm kernel parameter string */
1509 if (aqm_str) {
1510 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
Harald Freudenberger00fab232018-09-17 16:18:41 +02001511 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1512 &ap_perms_mutex);
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001513 }
1514}
1515
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001516/**
Felix Beck1749a812008-04-17 07:46:28 +02001517 * ap_module_init(): The module initialization code.
1518 *
1519 * Initializes the module.
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001520 */
Harald Freudenbergerefda7ad2018-04-04 07:14:17 +02001521static int __init ap_module_init(void)
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001522{
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001523 int max_domain_id;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001524 int rc, i;
1525
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01001526 rc = ap_debug_init();
1527 if (rc)
1528 return rc;
1529
Harald Freudenberger23951032018-08-09 11:59:34 +02001530 if (!ap_instructions_available()) {
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001531 pr_warn("The hardware system does not support AP instructions\n");
1532 return -ENODEV;
1533 }
1534
Harald Freudenberger00fab232018-09-17 16:18:41 +02001535 /* set up the AP permissions (ioctls, ap and aq masks) */
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001536 ap_perms_init();
1537
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001538 /* Get AP configuration data if available */
1539 ap_init_configuration();
1540
1541 if (ap_configuration)
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001542 max_domain_id =
1543 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001544 else
1545 max_domain_id = 15;
Harald Freudenberger7e0bdbe2018-07-20 08:36:53 +02001546 if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
1547 (ap_domain_index >= 0 &&
1548 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001549 pr_warn("%d is not a valid cryptographic domain\n",
1550 ap_domain_index);
Harald Freudenbergerac994e82017-05-12 16:35:14 +02001551 ap_domain_index = -1;
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001552 }
Felix Beck5314af62009-09-22 22:58:51 +02001553 /* In resume callback we need to know if the user had set the domain.
1554 * If so, we can not just reset it.
1555 */
1556 if (ap_domain_index >= 0)
1557 user_set_domain = 1;
1558
Felix Beckcb17a632008-12-25 13:38:41 +01001559 if (ap_interrupts_available()) {
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001560 rc = register_adapter_interrupt(&ap_airq);
1561 ap_airq_flag = (rc == 0);
Felix Beckcb17a632008-12-25 13:38:41 +01001562 }
1563
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001564 /* Create /sys/bus/ap. */
1565 rc = bus_register(&ap_bus_type);
1566 if (rc)
1567 goto out;
1568 for (i = 0; ap_bus_attrs[i]; i++) {
1569 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1570 if (rc)
1571 goto out_bus;
1572 }
1573
1574 /* Create /sys/devices/ap. */
Mark McLoughlin035da162008-12-15 12:58:29 +00001575 ap_root_device = root_device_register("ap");
Gustavo A. R. Silva9c705202018-07-18 23:42:05 -05001576 rc = PTR_ERR_OR_ZERO(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001577 if (rc)
1578 goto out_bus;
1579
Felix Beck1749a812008-04-17 07:46:28 +02001580 /* Setup the AP bus rescan timer. */
Kees Cookcefbeb52017-10-25 03:27:37 -07001581 timer_setup(&ap_config_timer, ap_config_timeout, 0);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001582
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001583 /*
1584 * Setup the high resultion poll timer.
Felix Beckfe137232008-07-14 09:59:08 +02001585 * If we are running under z/VM adjust polling to z/VM polling rate.
1586 */
1587 if (MACHINE_IS_VM)
1588 poll_timeout = 1500000;
Felix Beck93521312009-12-07 12:52:00 +01001589 spin_lock_init(&ap_poll_timer_lock);
Felix Beckfe137232008-07-14 09:59:08 +02001590 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1591 ap_poll_timer.function = ap_poll_timeout;
1592
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001593 /* Start the low priority AP bus poll thread. */
1594 if (ap_thread_flag) {
1595 rc = ap_poll_thread_start();
1596 if (rc)
1597 goto out_work;
1598 }
1599
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001600 rc = register_pm_notifier(&ap_power_notifier);
1601 if (rc)
1602 goto out_pm;
1603
Martin Schwidefsky8139b892015-07-27 12:47:40 +02001604 queue_work(system_long_wq, &ap_scan_work);
Sascha Silbee3877532015-10-27 18:29:52 +01001605 initialised = true;
Martin Schwidefsky3f3007a2015-09-14 17:01:23 +02001606
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001607 return 0;
1608
Martin Schwidefsky83e9d5d2015-07-17 13:43:18 +02001609out_pm:
1610 ap_poll_thread_stop();
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001611out_work:
Felix Beckfe137232008-07-14 09:59:08 +02001612 hrtimer_cancel(&ap_poll_timer);
Mark McLoughlin035da162008-12-15 12:58:29 +00001613 root_device_unregister(ap_root_device);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001614out_bus:
1615 while (i--)
1616 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1617 bus_unregister(&ap_bus_type);
1618out:
Martin Schwidefskyf4eae942013-06-24 10:30:41 +02001619 if (ap_using_interrupts())
1620 unregister_adapter_interrupt(&ap_airq);
Martin Schwidefsky889875a2015-06-26 16:55:35 +02001621 kfree(ap_configuration);
Martin Schwidefsky1534c382006-09-20 15:58:25 +02001622 return rc;
1623}
Paul Gortmaker50a0d462017-02-09 09:48:10 -05001624device_initcall(ap_module_init);