Linux 6.10-rc7
[linux-block.git] / drivers / s390 / crypto / ap_bus.c
CommitLineData
812141a9 1// SPDX-License-Identifier: GPL-2.0+
1534c382 2/*
5ac8c724 3 * Copyright IBM Corp. 2006, 2023
1534c382
MS
4 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Ralph Wuerthner <rwuerthn@de.ibm.com>
cb17a636 7 * Felix Beck <felix.beck@de.ibm.com>
6bed05bc 8 * Holger Dengler <hd@linux.vnet.ibm.com>
837cd105 9 * Harald Freudenberger <freude@linux.ibm.com>
1534c382
MS
10 *
11 * Adjunct processor bus.
1534c382
MS
12 */
13
136f7a1c
MS
14#define KMSG_COMPONENT "ap"
15#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
62d146ff 17#include <linux/kernel_stat.h>
50a0d46c 18#include <linux/moduleparam.h>
1534c382
MS
19#include <linux/init.h>
20#include <linux/delay.h>
21#include <linux/err.h>
41677b1d 22#include <linux/freezer.h>
1534c382
MS
23#include <linux/interrupt.h>
24#include <linux/workqueue.h>
5a0e3ad6 25#include <linux/slab.h>
1534c382
MS
26#include <linux/notifier.h>
27#include <linux/kthread.h>
28#include <linux/mutex.h>
cb17a636 29#include <asm/airq.h>
d2197485 30#include <asm/tpi.h>
60063497 31#include <linux/atomic.h>
cb17a636 32#include <asm/isc.h>
fe137230
FB
33#include <linux/hrtimer.h>
34#include <linux/ktime.h>
a0616cde 35#include <asm/facility.h>
5d26a105 36#include <linux/crypto.h>
e28d2af4 37#include <linux/mod_devicetable.h>
cccd85bf 38#include <linux/debugfs.h>
7e0bdbe5 39#include <linux/ctype.h>
4f8206b8 40#include <linux/module.h>
d065bdb4 41#include <asm/uv.h>
2a483d33 42#include <asm/chsc.h>
1534c382
MS
43
44#include "ap_bus.h"
cccd85bf 45#include "ap_debug.h"
1534c382 46
12376084
HD
47MODULE_AUTHOR("IBM Corporation");
48MODULE_DESCRIPTION("Adjunct Processor Bus driver");
49MODULE_LICENSE("GPL");
50
1534c382 51int ap_domain_index = -1; /* Adjunct Processor Domain Index */
fc1d3f02 52static DEFINE_SPINLOCK(ap_domain_lock);
ac2b96f3 53module_param_named(domain, ap_domain_index, int, 0440);
1534c382
MS
54MODULE_PARM_DESC(domain, "domain index for ap devices");
55EXPORT_SYMBOL(ap_domain_index);
56
ac2b96f3
HF
57static int ap_thread_flag;
58module_param_named(poll_thread, ap_thread_flag, int, 0440);
b90b34c6 59MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
1534c382 60
7e0bdbe5
HF
61static char *apm_str;
62module_param_named(apmask, apm_str, charp, 0440);
63MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
64
65static char *aqm_str;
66module_param_named(aqmask, aqm_str, charp, 0440);
67MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
68
d0982725
HF
69static int ap_useirq = 1;
70module_param_named(useirq, ap_useirq, int, 0440);
71MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on).");
72
bd39654a
HF
73atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
74EXPORT_SYMBOL(ap_max_msg_size);
75
e28d2af4
IT
76static struct device *ap_root_device;
77
bc4b295e
HF
78/* Hashtable of all queue devices on the AP bus */
79DEFINE_HASHTABLE(ap_queues, 8);
80/* lock used for the ap_queues hashtable */
81DEFINE_SPINLOCK(ap_queues_lock);
e28d2af4 82
00fab235
HF
83/* Default permissions (ioctl, card and domain masking) */
84struct ap_perms ap_perms;
85EXPORT_SYMBOL(ap_perms);
86DEFINE_MUTEX(ap_perms_mutex);
87EXPORT_SYMBOL(ap_perms_mutex);
7e0bdbe5 88
0677519a
HF
89/* # of bindings complete since init */
90static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
91
778412ab
HF
92/* completion for APQN bindings complete */
93static DECLARE_COMPLETION(ap_apqn_bindings_complete);
837cd105 94
8dec9cb9
HD
95static struct ap_config_info qci[2];
96static struct ap_config_info *const ap_qci_info = &qci[0];
97static struct ap_config_info *const ap_qci_info_old = &qci[1];
1534c382 98
cccd85bf
HF
99/*
100 * AP bus related debug feature things.
101 */
cccd85bf
HF
102debug_info_t *ap_dbf_info;
103
1749a81d 104/*
99b3126e 105 * AP bus rescan related things.
1534c382 106 */
eacf5b36
HF
107static bool ap_scan_bus(void);
108static bool ap_scan_bus_result; /* result of last ap_scan_bus() */
109static DEFINE_MUTEX(ap_scan_bus_mutex); /* mutex ap_scan_bus() invocations */
99b3126e
HF
110static atomic64_t ap_scan_bus_count; /* counter ap_scan_bus() invocations */
111static int ap_scan_bus_time = AP_CONFIG_TIME;
112static struct timer_list ap_scan_bus_timer;
b5caf05e
HF
113static void ap_scan_bus_wq_callback(struct work_struct *);
114static DECLARE_WORK(ap_scan_bus_work, ap_scan_bus_wq_callback);
1534c382 115
1749a81d 116/*
cb17a636 117 * Tasklet & timer for AP request polling and interrupts
1534c382 118 */
3f3007af 119static void ap_tasklet_fn(unsigned long);
b13fecb1 120static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
1534c382 121static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
ac2b96f3 122static struct task_struct *ap_poll_kthread;
1534c382 123static DEFINE_MUTEX(ap_poll_thread_mutex);
93521314 124static DEFINE_SPINLOCK(ap_poll_timer_lock);
fe137230 125static struct hrtimer ap_poll_timer;
ac2b96f3
HF
126/*
127 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
128 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
129 */
263c8454
HF
130static unsigned long poll_high_timeout = 250000UL;
131
132/*
133 * Some state machine states only require a low frequency polling.
134 * We use 25 Hz frequency for these.
135 */
136static unsigned long poll_low_timeout = 40000000UL;
1534c382 137
c8337c47
HF
138/* Maximum domain id, if not given via qci */
139static int ap_max_domain_id = 15;
140/* Maximum adapter id, if not given via qci */
141static int ap_max_adapter_id = 63;
41677b1d 142
5b431787 143static const struct bus_type ap_bus_type;
772f5472 144
f4eae94f 145/* Adapter interrupt definitions */
d2197485
MR
146static void ap_interrupt_handler(struct airq_struct *airq,
147 struct tpi_info *tpi_info);
3f3007af 148
cabebb69 149static bool ap_irq_flag;
f4eae94f
MS
150
151static struct airq_struct ap_airq = {
152 .handler = ap_interrupt_handler,
153 .isc = AP_ISC,
154};
155
e28d2af4
IT
156/**
157 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
158 *
159 * Returns the address of the local-summary-indicator of the adapter
160 * interrupt handler for AP, or NULL if adapter interrupts are not
161 * available.
162 */
163void *ap_airq_ptr(void)
164{
cabebb69 165 if (ap_irq_flag)
e28d2af4
IT
166 return ap_airq.lsi_ptr;
167 return NULL;
168}
169
cb17a636
FB
170/**
171 * ap_interrupts_available(): Test if AP interrupts are available.
172 *
173 * Returns 1 if AP interrupts are available.
174 */
175static int ap_interrupts_available(void)
176{
86cd741b 177 return test_facility(65);
cb17a636
FB
178}
179
75014550 180/**
c8337c47
HF
181 * ap_qci_available(): Test if AP configuration
182 * information can be queried via QCI subfunction.
75014550 183 *
c8337c47 184 * Returns 1 if subfunction PQAP(QCI) is available.
75014550 185 */
c8337c47 186static int ap_qci_available(void)
75014550 187{
86cd741b 188 return test_facility(12);
75014550
HD
189}
190
e7fc5146
TK
191/**
192 * ap_apft_available(): Test if AP facilities test (APFT)
193 * facility is available.
194 *
2004b57c 195 * Returns 1 if APFT is available.
e7fc5146
TK
196 */
197static int ap_apft_available(void)
198{
199 return test_facility(15);
200}
201
9a564108
HF
202/*
203 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
204 *
205 * Returns 1 if the QACT subfunction is available.
206 */
207static inline int ap_qact_available(void)
208{
8dec9cb9 209 return ap_qci_info->qact;
9a564108
HF
210}
211
f6047040
HF
212/*
213 * ap_sb_available(): Test if the AP secure binding facility is available.
214 *
215 * Returns 1 if secure binding facility is available.
216 */
217int ap_sb_available(void)
218{
8dec9cb9 219 return ap_qci_info->apsb;
f6047040
HF
220}
221
386cb81e
HD
222/*
223 * ap_is_se_guest(): Check for SE guest with AP pass-through support.
224 */
225bool ap_is_se_guest(void)
226{
227 return is_prot_virt_guest() && ap_sb_available();
228}
229EXPORT_SYMBOL(ap_is_se_guest);
230
889875a1 231/**
c8337c47
HF
232 * ap_init_qci_info(): Allocate and query qci config info.
233 * Does also update the static variables ap_max_domain_id
234 * and ap_max_adapter_id if this info is available.
889875a1 235 */
c8337c47 236static void __init ap_init_qci_info(void)
889875a1 237{
8dec9cb9
HD
238 if (!ap_qci_available() ||
239 ap_qci(ap_qci_info)) {
2ea2a609 240 AP_DBF_INFO("%s QCI not supported\n", __func__);
889875a1 241 return;
c8337c47 242 }
8dec9cb9 243 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2ea2a609 244 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
c8337c47
HF
245
246 if (ap_qci_info->apxa) {
f6047040
HF
247 if (ap_qci_info->na) {
248 ap_max_adapter_id = ap_qci_info->na;
2ea2a609
HF
249 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
250 __func__, ap_max_adapter_id);
c8337c47 251 }
f6047040
HF
252 if (ap_qci_info->nd) {
253 ap_max_domain_id = ap_qci_info->nd;
2ea2a609
HF
254 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
255 __func__, ap_max_domain_id);
c8337c47
HF
256 }
257 }
889875a1
MS
258}
259
260/*
261 * ap_test_config(): helper function to extract the nrth bit
262 * within the unsigned int array field.
263 */
264static inline int ap_test_config(unsigned int *field, unsigned int nr)
265{
266 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
267}
268
269/*
270 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
889875a1
MS
271 *
272 * Returns 0 if the card is not configured
273 * 1 if the card is configured or
274 * if the configuration information is not available
275 */
276static inline int ap_test_config_card_id(unsigned int id)
277{
c8337c47
HF
278 if (id > ap_max_adapter_id)
279 return 0;
8dec9cb9 280 if (ap_qci_info->flags)
c8337c47
HF
281 return ap_test_config(ap_qci_info->apm, id);
282 return 1;
889875a1
MS
283}
284
285/*
7379e652
HF
286 * ap_test_config_usage_domain(): Test, whether an AP usage domain
287 * is configured.
889875a1
MS
288 *
289 * Returns 0 if the usage domain is not configured
290 * 1 if the usage domain is configured or
291 * if the configuration information is not available
292 */
7379e652 293int ap_test_config_usage_domain(unsigned int domain)
889875a1 294{
c8337c47
HF
295 if (domain > ap_max_domain_id)
296 return 0;
8dec9cb9 297 if (ap_qci_info->flags)
c8337c47
HF
298 return ap_test_config(ap_qci_info->aqm, domain);
299 return 1;
889875a1 300}
7379e652
HF
301EXPORT_SYMBOL(ap_test_config_usage_domain);
302
303/*
304 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
305 * is configured.
306 * @domain AP control domain ID
307 *
308 * Returns 1 if the control domain is configured
309 * 0 in all other cases
310 */
311int ap_test_config_ctrl_domain(unsigned int domain)
312{
c8337c47 313 if (!ap_qci_info || domain > ap_max_domain_id)
7379e652 314 return 0;
c8337c47 315 return ap_test_config(ap_qci_info->adm, domain);
7379e652
HF
316}
317EXPORT_SYMBOL(ap_test_config_ctrl_domain);
889875a1 318
c8337c47
HF
319/*
320 * ap_queue_info(): Check and get AP queue info.
038c5bed 321 * Returns: 1 if APQN exists and info is filled,
5cf1a563 322 * 0 if APQN seems to exist but there is no info
038c5bed
HF
323 * available (eg. caused by an asynch pending error)
324 * -1 invalid APQN, TAPQ error or AP queue status which
325 * indicates there is no APQN.
1534c382 326 */
d4c53ae8
HF
327static int ap_queue_info(ap_qid_t qid, struct ap_tapq_hwinfo *hwinfo,
328 bool *decfg, bool *cstop)
1534c382
MS
329{
330 struct ap_queue_status status;
bd39654a 331
d4c53ae8 332 hwinfo->value = 0;
889875a1 333
c8337c47
HF
334 /* make sure we don't run into a specifiation exception */
335 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
336 AP_QID_QUEUE(qid) > ap_max_domain_id)
038c5bed 337 return -1;
1534c382 338
c8337c47 339 /* call TAPQ on this APQN */
d4c53ae8 340 status = ap_test_queue(qid, ap_apft_available(), hwinfo);
038c5bed 341
c50a160c
IT
342 switch (status.response_code) {
343 case AP_RESPONSE_NORMAL:
c8337c47 344 case AP_RESPONSE_RESET_IN_PROGRESS:
4f2fcccd
HF
345 case AP_RESPONSE_DECONFIGURED:
346 case AP_RESPONSE_CHECKSTOPPED:
347 case AP_RESPONSE_BUSY:
5cf1a563
HF
348 /* For all these RCs the tapq info should be available */
349 break;
c50a160c 350 default:
5cf1a563
HF
351 /* On a pending async error the info should be available */
352 if (!status.async)
353 return -1;
354 break;
1534c382 355 }
5cf1a563
HF
356
357 /* There should be at least one of the mode bits set */
d4c53ae8 358 if (WARN_ON_ONCE(!hwinfo->value))
5cf1a563
HF
359 return 0;
360
d4c53ae8
HF
361 *decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
362 *cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
5cf1a563
HF
363
364 return 1;
1534c382
MS
365}
366
dc4b6ded 367void ap_wait(enum ap_sm_wait wait)
3f3007af
MS
368{
369 ktime_t hr_time;
370
371 switch (wait) {
dc4b6ded
HF
372 case AP_SM_WAIT_AGAIN:
373 case AP_SM_WAIT_INTERRUPT:
cabebb69 374 if (ap_irq_flag)
3f3007af
MS
375 break;
376 if (ap_poll_kthread) {
377 wake_up(&ap_poll_wait);
378 break;
379 }
fcf0220a 380 fallthrough;
263c8454
HF
381 case AP_SM_WAIT_LOW_TIMEOUT:
382 case AP_SM_WAIT_HIGH_TIMEOUT:
3f3007af
MS
383 spin_lock_bh(&ap_poll_timer_lock);
384 if (!hrtimer_is_queued(&ap_poll_timer)) {
263c8454
HF
385 hr_time =
386 wait == AP_SM_WAIT_LOW_TIMEOUT ?
387 poll_low_timeout : poll_high_timeout;
3f3007af
MS
388 hrtimer_forward_now(&ap_poll_timer, hr_time);
389 hrtimer_restart(&ap_poll_timer);
390 }
391 spin_unlock_bh(&ap_poll_timer_lock);
392 break;
dc4b6ded 393 case AP_SM_WAIT_NONE:
3f3007af
MS
394 default:
395 break;
396 }
397}
398
3f3007af
MS
399/**
400 * ap_request_timeout(): Handling of request timeouts
cefbeb5d 401 * @t: timer making this callback
3f3007af
MS
402 *
403 * Handles request timeouts.
404 */
cefbeb5d 405void ap_request_timeout(struct timer_list *t)
3f3007af 406{
cefbeb5d 407 struct ap_queue *aq = from_timer(aq, t, timeout);
3f3007af 408
e28d2af4 409 spin_lock_bh(&aq->lock);
dc4b6ded 410 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
e28d2af4 411 spin_unlock_bh(&aq->lock);
3f3007af
MS
412}
413
414/**
415 * ap_poll_timeout(): AP receive polling for finished AP requests.
416 * @unused: Unused pointer.
417 *
418 * Schedules the AP tasklet using a high resolution timer.
419 */
420static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
421{
41677b1d 422 tasklet_schedule(&ap_tasklet);
3f3007af
MS
423 return HRTIMER_NORESTART;
424}
425
426/**
427 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
428 * @airq: pointer to adapter interrupt descriptor
d2197485 429 * @tpi_info: ignored
3f3007af 430 */
d2197485
MR
431static void ap_interrupt_handler(struct airq_struct *airq,
432 struct tpi_info *tpi_info)
3f3007af
MS
433{
434 inc_irq_stat(IRQIO_APB);
41677b1d 435 tasklet_schedule(&ap_tasklet);
3f3007af
MS
436}
437
438/**
439 * ap_tasklet_fn(): Tasklet to poll all AP devices.
440 * @dummy: Unused variable
441 *
442 * Poll all AP devices on the bus.
443 */
444static void ap_tasklet_fn(unsigned long dummy)
445{
bc4b295e 446 int bkt;
e28d2af4 447 struct ap_queue *aq;
dc4b6ded 448 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
3f3007af
MS
449
450 /* Reset the indicator if interrupts are used. Thus new interrupts can
cada938a 451 * be received. Doing it in the beginning of the tasklet is therefore
3f3007af
MS
452 * important that no requests on any AP get lost.
453 */
cabebb69 454 if (ap_irq_flag)
3f3007af
MS
455 xchg(ap_airq.lsi_ptr, 0);
456
bc4b295e
HF
457 spin_lock_bh(&ap_queues_lock);
458 hash_for_each(ap_queues, bkt, aq, hnode) {
459 spin_lock_bh(&aq->lock);
dc4b6ded 460 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
bc4b295e 461 spin_unlock_bh(&aq->lock);
3f3007af 462 }
bc4b295e 463 spin_unlock_bh(&ap_queues_lock);
e28d2af4
IT
464
465 ap_wait(wait);
af512ed0
RW
466}
467
9af3e04e
MS
468static int ap_pending_requests(void)
469{
bc4b295e 470 int bkt;
e28d2af4
IT
471 struct ap_queue *aq;
472
bc4b295e
HF
473 spin_lock_bh(&ap_queues_lock);
474 hash_for_each(ap_queues, bkt, aq, hnode) {
475 if (aq->queue_count == 0)
476 continue;
477 spin_unlock_bh(&ap_queues_lock);
478 return 1;
9af3e04e 479 }
bc4b295e 480 spin_unlock_bh(&ap_queues_lock);
e28d2af4 481 return 0;
9af3e04e
MS
482}
483
83e9d5d2
MS
484/**
485 * ap_poll_thread(): Thread that polls for finished requests.
486 * @data: Unused pointer
487 *
488 * AP bus poll thread. The purpose of this thread is to poll for
489 * finished requests in a loop if there is a "free" cpu - that is
490 * a cpu that doesn't have anything better to do. The polling stops
491 * as soon as there is another task or if all messages have been
492 * delivered.
493 */
494static int ap_poll_thread(void *data)
495{
496 DECLARE_WAITQUEUE(wait, current);
83e9d5d2
MS
497
498 set_user_nice(current, MAX_NICE);
499 set_freezable();
500 while (!kthread_should_stop()) {
501 add_wait_queue(&ap_poll_wait, &wait);
502 set_current_state(TASK_INTERRUPTIBLE);
41677b1d 503 if (!ap_pending_requests()) {
83e9d5d2
MS
504 schedule();
505 try_to_freeze();
506 }
507 set_current_state(TASK_RUNNING);
508 remove_wait_queue(&ap_poll_wait, &wait);
83e9d5d2
MS
509 if (need_resched()) {
510 schedule();
511 try_to_freeze();
512 continue;
513 }
3f3007af 514 ap_tasklet_fn(0);
9af3e04e
MS
515 }
516
83e9d5d2
MS
517 return 0;
518}
519
520static int ap_poll_thread_start(void)
521{
522 int rc;
523
cabebb69 524 if (ap_irq_flag || ap_poll_kthread)
83e9d5d2
MS
525 return 0;
526 mutex_lock(&ap_poll_thread_mutex);
527 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
9c705206 528 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
83e9d5d2
MS
529 if (rc)
530 ap_poll_kthread = NULL;
531 mutex_unlock(&ap_poll_thread_mutex);
532 return rc;
533}
534
535static void ap_poll_thread_stop(void)
536{
537 if (!ap_poll_kthread)
538 return;
539 mutex_lock(&ap_poll_thread_mutex);
540 kthread_stop(ap_poll_kthread);
541 ap_poll_kthread = NULL;
542 mutex_unlock(&ap_poll_thread_mutex);
543}
544
e28d2af4
IT
545#define is_card_dev(x) ((x)->parent == ap_root_device)
546#define is_queue_dev(x) ((x)->parent != ap_root_device)
1534c382
MS
547
548/**
1749a81d
FB
549 * ap_bus_match()
550 * @dev: Pointer to device
551 * @drv: Pointer to device_driver
552 *
1534c382
MS
553 * AP bus driver registration/unregistration.
554 */
555static int ap_bus_match(struct device *dev, struct device_driver *drv)
556{
1534c382
MS
557 struct ap_driver *ap_drv = to_ap_drv(drv);
558 struct ap_device_id *id;
559
1749a81d 560 /*
1534c382
MS
561 * Compare device type of the device with the list of
562 * supported types of the device_driver.
563 */
564 for (id = ap_drv->ids; id->match_flags; id++) {
e28d2af4
IT
565 if (is_card_dev(dev) &&
566 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
567 id->dev_type == to_ap_dev(dev)->device_type)
568 return 1;
569 if (is_queue_dev(dev) &&
570 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
571 id->dev_type == to_ap_dev(dev)->device_type)
572 return 1;
1534c382
MS
573 }
574 return 0;
575}
576
577/**
1749a81d
FB
578 * ap_uevent(): Uevent function for AP devices.
579 * @dev: Pointer to device
580 * @env: Pointer to kobj_uevent_env
581 *
582 * It sets up a single environment variable DEV_TYPE which contains the
583 * hardware device type.
1534c382 584 */
2a81ada3 585static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env)
1534c382 586{
df6f508c 587 int rc = 0;
2a81ada3 588 const struct ap_device *ap_dev = to_ap_dev(dev);
1534c382 589
837cd105
HF
590 /* Uevents from ap bus core don't need extensions to the env */
591 if (dev == ap_root_device)
592 return 0;
1534c382 593
df6f508c
HF
594 if (is_card_dev(dev)) {
595 struct ap_card *ac = to_ap_card(&ap_dev->device);
bf62456e 596
df6f508c
HF
597 /* Set up DEV_TYPE environment variable. */
598 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
599 if (rc)
600 return rc;
601 /* Add MODALIAS= */
602 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
603 if (rc)
604 return rc;
605
606 /* Add MODE=<accel|cca|ep11> */
d4c53ae8 607 if (ac->hwinfo.accel)
df6f508c 608 rc = add_uevent_var(env, "MODE=accel");
d4c53ae8 609 else if (ac->hwinfo.cca)
df6f508c 610 rc = add_uevent_var(env, "MODE=cca");
d4c53ae8 611 else if (ac->hwinfo.ep11)
df6f508c
HF
612 rc = add_uevent_var(env, "MODE=ep11");
613 if (rc)
614 return rc;
615 } else {
616 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
617
618 /* Add MODE=<accel|cca|ep11> */
d4c53ae8 619 if (aq->card->hwinfo.accel)
df6f508c 620 rc = add_uevent_var(env, "MODE=accel");
d4c53ae8 621 else if (aq->card->hwinfo.cca)
df6f508c 622 rc = add_uevent_var(env, "MODE=cca");
d4c53ae8 623 else if (aq->card->hwinfo.ep11)
df6f508c
HF
624 rc = add_uevent_var(env, "MODE=ep11");
625 if (rc)
626 return rc;
627 }
837cd105
HF
628
629 return 0;
630}
631
632static void ap_send_init_scan_done_uevent(void)
633{
634 char *envp[] = { "INITSCAN=done", NULL };
635
636 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
637}
638
639static void ap_send_bindings_complete_uevent(void)
640{
0677519a
HF
641 char buf[32];
642 char *envp[] = { "BINDINGS=complete", buf, NULL };
837cd105 643
0677519a
HF
644 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
645 atomic64_inc_return(&ap_bindings_complete_count));
837cd105
HF
646 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
647}
648
df6f508c
HF
649void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
650{
651 char buf[16];
652 char *envp[] = { buf, NULL };
653
654 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
655
656 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
657}
658EXPORT_SYMBOL(ap_send_config_uevent);
659
660void ap_send_online_uevent(struct ap_device *ap_dev, int online)
661{
662 char buf[16];
663 char *envp[] = { buf, NULL };
664
665 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
666
667 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
668}
669EXPORT_SYMBOL(ap_send_online_uevent);
670
d9b38e9d
HF
671static void ap_send_mask_changed_uevent(unsigned long *newapm,
672 unsigned long *newaqm)
673{
674 char buf[100];
675 char *envp[] = { buf, NULL };
676
677 if (newapm)
678 snprintf(buf, sizeof(buf),
679 "APMASK=0x%016lx%016lx%016lx%016lx\n",
680 newapm[0], newapm[1], newapm[2], newapm[3]);
681 else
682 snprintf(buf, sizeof(buf),
683 "AQMASK=0x%016lx%016lx%016lx%016lx\n",
684 newaqm[0], newaqm[1], newaqm[2], newaqm[3]);
685
686 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
687}
688
837cd105
HF
689/*
690 * calc # of bound APQNs
691 */
692
693struct __ap_calc_ctrs {
694 unsigned int apqns;
695 unsigned int bound;
696};
697
698static int __ap_calc_helper(struct device *dev, void *arg)
699{
2004b57c 700 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg;
837cd105
HF
701
702 if (is_queue_dev(dev)) {
703 pctrs->apqns++;
c8c68c5f 704 if (dev->driver)
837cd105
HF
705 pctrs->bound++;
706 }
707
708 return 0;
709}
710
711static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
712{
713 struct __ap_calc_ctrs ctrs;
714
715 memset(&ctrs, 0, sizeof(ctrs));
2004b57c 716 bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper);
bf62456e 717
837cd105
HF
718 *apqns = ctrs.apqns;
719 *bound = ctrs.bound;
1534c382
MS
720}
721
837cd105 722/*
778412ab 723 * After ap bus scan do check if all existing APQNs are
837cd105
HF
724 * bound to device drivers.
725 */
726static void ap_check_bindings_complete(void)
727{
728 unsigned int apqns, bound;
729
730 if (atomic64_read(&ap_scan_bus_count) >= 1) {
731 ap_calc_bound_apqns(&apqns, &bound);
732 if (bound == apqns) {
778412ab
HF
733 if (!completion_done(&ap_apqn_bindings_complete)) {
734 complete_all(&ap_apqn_bindings_complete);
306d6bda 735 ap_send_bindings_complete_uevent();
778412ab 736 pr_debug("%s all apqn bindings complete\n", __func__);
837cd105 737 }
837cd105
HF
738 }
739 }
740}
741
742/*
743 * Interface to wait for the AP bus to have done one initial ap bus
744 * scan and all detected APQNs have been bound to device drivers.
745 * If these both conditions are not fulfilled, this function blocks
746 * on a condition with wait_for_completion_interruptible_timeout().
747 * If these both conditions are fulfilled (before the timeout hits)
748 * the return value is 0. If the timeout (in jiffies) hits instead
749 * -ETIME is returned. On failures negative return values are
750 * returned to the caller.
751 */
778412ab 752int ap_wait_apqn_bindings_complete(unsigned long timeout)
837cd105 753{
778412ab 754 int rc = 0;
837cd105
HF
755 long l;
756
778412ab 757 if (completion_done(&ap_apqn_bindings_complete))
837cd105
HF
758 return 0;
759
760 if (timeout)
761 l = wait_for_completion_interruptible_timeout(
778412ab 762 &ap_apqn_bindings_complete, timeout);
837cd105
HF
763 else
764 l = wait_for_completion_interruptible(
778412ab 765 &ap_apqn_bindings_complete);
837cd105 766 if (l < 0)
778412ab 767 rc = l == -ERESTARTSYS ? -EINTR : l;
837cd105 768 else if (l == 0 && timeout)
778412ab 769 rc = -ETIME;
837cd105 770
778412ab
HF
771 pr_debug("%s rc=%d\n", __func__, rc);
772 return rc;
837cd105 773}
778412ab 774EXPORT_SYMBOL(ap_wait_apqn_bindings_complete);
837cd105 775
e28d2af4 776static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
83e9d5d2 777{
e28d2af4 778 if (is_queue_dev(dev) &&
2004b57c 779 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data)
e28d2af4 780 device_unregister(dev);
83e9d5d2
MS
781 return 0;
782}
783
7e0bdbe5
HF
784static int __ap_revise_reserved(struct device *dev, void *dummy)
785{
786 int rc, card, queue, devres, drvres;
787
788 if (is_queue_dev(dev)) {
789 card = AP_QID_CARD(to_ap_queue(dev)->qid);
790 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
791 mutex_lock(&ap_perms_mutex);
2004b57c
HF
792 devres = test_bit_inv(card, ap_perms.apm) &&
793 test_bit_inv(queue, ap_perms.aqm);
7e0bdbe5
HF
794 mutex_unlock(&ap_perms_mutex);
795 drvres = to_ap_drv(dev->driver)->flags
796 & AP_DRIVER_FLAG_DEFAULT;
797 if (!!devres != !!drvres) {
08b2c370
HF
798 pr_debug("%s reprobing queue=%02x.%04x\n",
799 __func__, card, queue);
7e0bdbe5 800 rc = device_reprobe(dev);
132c1e74
HF
801 if (rc)
802 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
803 __func__, card, queue);
7e0bdbe5
HF
804 }
805 }
806
807 return 0;
808}
809
810static void ap_bus_revise_bindings(void)
811{
812 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
813}
814
3f85d1df
TK
815/**
816 * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
817 * default host driver or not.
818 * @card: the APID of the adapter card to check
819 * @queue: the APQI of the queue to check
820 *
821 * Note: the ap_perms_mutex must be locked by the caller of this function.
822 *
823 * Return: an int specifying whether the AP adapter is reserved for the host (1)
824 * or not (0).
825 */
7e0bdbe5
HF
826int ap_owned_by_def_drv(int card, int queue)
827{
828 int rc = 0;
829
830 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
831 return -EINVAL;
832
2004b57c
HF
833 if (test_bit_inv(card, ap_perms.apm) &&
834 test_bit_inv(queue, ap_perms.aqm))
7e0bdbe5
HF
835 rc = 1;
836
7e0bdbe5
HF
837 return rc;
838}
839EXPORT_SYMBOL(ap_owned_by_def_drv);
840
3f85d1df
TK
841/**
842 * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
843 * a set is reserved for the host drivers
844 * or not.
845 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
846 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
847 *
848 * Note: the ap_perms_mutex must be locked by the caller of this function.
849 *
850 * Return: an int specifying whether each APQN is reserved for the host (1) or
851 * not (0)
852 */
7e0bdbe5
HF
853int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
854 unsigned long *aqm)
855{
856 int card, queue, rc = 0;
857
7e0bdbe5
HF
858 for (card = 0; !rc && card < AP_DEVICES; card++)
859 if (test_bit_inv(card, apm) &&
860 test_bit_inv(card, ap_perms.apm))
861 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
862 if (test_bit_inv(queue, aqm) &&
863 test_bit_inv(queue, ap_perms.aqm))
864 rc = 1;
865
7e0bdbe5
HF
866 return rc;
867}
868EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
869
1534c382
MS
870static int ap_device_probe(struct device *dev)
871{
872 struct ap_device *ap_dev = to_ap_dev(dev);
3f3007af 873 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
29c2680f
HF
874 int card, queue, devres, drvres, rc = -ENODEV;
875
876 if (!get_device(dev))
877 return rc;
7e0bdbe5
HF
878
879 if (is_queue_dev(dev)) {
880 /*
881 * If the apqn is marked as reserved/used by ap bus and
882 * default drivers, only probe with drivers with the default
883 * flag set. If it is not marked, only probe with drivers
884 * with the default flag not set.
885 */
886 card = AP_QID_CARD(to_ap_queue(dev)->qid);
887 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
888 mutex_lock(&ap_perms_mutex);
2004b57c
HF
889 devres = test_bit_inv(card, ap_perms.apm) &&
890 test_bit_inv(queue, ap_perms.aqm);
7e0bdbe5
HF
891 mutex_unlock(&ap_perms_mutex);
892 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
893 if (!!devres != !!drvres)
29c2680f 894 goto out;
7e0bdbe5 895 }
666e68e0 896
306d6bda
HF
897 /*
898 * Rearm the bindings complete completion to trigger
899 * bindings complete when all devices are bound again
900 */
901 reinit_completion(&ap_apqn_bindings_complete);
902
e3850508 903 /* Add queue/card to list of active queues/cards */
bc4b295e
HF
904 spin_lock_bh(&ap_queues_lock);
905 if (is_queue_dev(dev))
906 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
907 to_ap_queue(dev)->qid);
908 spin_unlock_bh(&ap_queues_lock);
e3850508 909
1534c382 910 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
e3850508
HF
911
912 if (rc) {
bc4b295e
HF
913 spin_lock_bh(&ap_queues_lock);
914 if (is_queue_dev(dev))
915 hash_del(&to_ap_queue(dev)->hnode);
916 spin_unlock_bh(&ap_queues_lock);
2004b57c 917 }
e3850508 918
29c2680f
HF
919out:
920 if (rc)
921 put_device(dev);
1534c382
MS
922 return rc;
923}
924
fc7a6209 925static void ap_device_remove(struct device *dev)
1534c382
MS
926{
927 struct ap_device *ap_dev = to_ap_dev(dev);
c8c68c5f 928 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
1534c382 929
01396a37 930 /* prepare ap queue device removal */
104f708f 931 if (is_queue_dev(dev))
01396a37
HF
932 ap_queue_prepare_remove(to_ap_queue(dev));
933
934 /* driver's chance to clean up gracefully */
e3850508
HF
935 if (ap_drv->remove)
936 ap_drv->remove(ap_dev);
937
01396a37
HF
938 /* now do the ap queue device remove */
939 if (is_queue_dev(dev))
940 ap_queue_remove(to_ap_queue(dev));
941
e3850508 942 /* Remove queue/card from list of active queues/cards */
bc4b295e
HF
943 spin_lock_bh(&ap_queues_lock);
944 if (is_queue_dev(dev))
945 hash_del(&to_ap_queue(dev)->hnode);
946 spin_unlock_bh(&ap_queues_lock);
e3850508 947
29c2680f 948 put_device(dev);
1534c382
MS
949}
950
bc4b295e
HF
951struct ap_queue *ap_get_qdev(ap_qid_t qid)
952{
953 int bkt;
954 struct ap_queue *aq;
955
956 spin_lock_bh(&ap_queues_lock);
957 hash_for_each(ap_queues, bkt, aq, hnode) {
958 if (aq->qid == qid) {
959 get_device(&aq->ap_dev.device);
960 spin_unlock_bh(&ap_queues_lock);
961 return aq;
962 }
963 }
964 spin_unlock_bh(&ap_queues_lock);
965
966 return NULL;
967}
968EXPORT_SYMBOL(ap_get_qdev);
969
1534c382
MS
970int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
971 char *name)
972{
973 struct device_driver *drv = &ap_drv->driver;
974
975 drv->bus = &ap_bus_type;
1534c382
MS
976 drv->owner = owner;
977 drv->name = name;
978 return driver_register(drv);
979}
980EXPORT_SYMBOL(ap_driver_register);
981
982void ap_driver_unregister(struct ap_driver *ap_drv)
983{
984 driver_unregister(&ap_drv->driver);
985}
986EXPORT_SYMBOL(ap_driver_unregister);
987
eacf5b36
HF
988/*
989 * Enforce a synchronous AP bus rescan.
990 * Returns true if the bus scan finds a change in the AP configuration
991 * and AP devices have been added or deleted when this function returns.
992 */
993bool ap_bus_force_rescan(void)
dabecb29 994{
eacf5b36
HF
995 unsigned long scan_counter = atomic64_read(&ap_scan_bus_count);
996 bool rc = false;
997
998 pr_debug(">%s scan counter=%lu\n", __func__, scan_counter);
999
e14aec23 1000 /* Only trigger AP bus scans after the initial scan is done */
eacf5b36
HF
1001 if (scan_counter <= 0)
1002 goto out;
e14aec23 1003
eacf5b36
HF
1004 /* Try to acquire the AP scan bus mutex */
1005 if (mutex_trylock(&ap_scan_bus_mutex)) {
1006 /* mutex acquired, run the AP bus scan */
1007 ap_scan_bus_result = ap_scan_bus();
1008 rc = ap_scan_bus_result;
1009 mutex_unlock(&ap_scan_bus_mutex);
1010 goto out;
1011 }
1012
1013 /*
1014 * Mutex acquire failed. So there is currently another task
1015 * already running the AP bus scan. Then let's simple wait
1016 * for the lock which means the other task has finished and
1017 * stored the result in ap_scan_bus_result.
1018 */
1019 if (mutex_lock_interruptible(&ap_scan_bus_mutex)) {
1020 /* some error occurred, ignore and go out */
1021 goto out;
1022 }
1023 rc = ap_scan_bus_result;
1024 mutex_unlock(&ap_scan_bus_mutex);
1025
1026out:
1027 pr_debug("%s rc=%d\n", __func__, rc);
1028 return rc;
dabecb29
HD
1029}
1030EXPORT_SYMBOL(ap_bus_force_rescan);
1031
0d9c038f 1032/*
2004b57c
HF
1033 * A config change has happened, force an ap bus rescan.
1034 */
2a483d33
HD
1035static int ap_bus_cfg_chg(struct notifier_block *nb,
1036 unsigned long action, void *data)
0d9c038f 1037{
2a483d33
HD
1038 if (action != CHSC_NOTIFY_AP_CFG)
1039 return NOTIFY_DONE;
1040
08b2c370 1041 pr_debug("%s config change, forcing bus rescan\n", __func__);
0d9c038f
TK
1042
1043 ap_bus_force_rescan();
2a483d33
HD
1044
1045 return NOTIFY_OK;
0d9c038f
TK
1046}
1047
2a483d33
HD
1048static struct notifier_block ap_bus_nb = {
1049 .notifier_call = ap_bus_cfg_chg,
1050};
1051
6e697394 1052int ap_hex2bitmap(const char *str, unsigned long *bitmap, int bits)
7e0bdbe5
HF
1053{
1054 int i, n, b;
1055
1056 /* bits needs to be a multiple of 8 */
1057 if (bits & 0x07)
1058 return -EINVAL;
1059
7e0bdbe5
HF
1060 if (str[0] == '0' && str[1] == 'x')
1061 str++;
1062 if (*str == 'x')
1063 str++;
1064
1065 for (i = 0; isxdigit(*str) && i < bits; str++) {
1066 b = hex_to_bin(*str);
1067 for (n = 0; n < 4; n++)
1068 if (b & (0x08 >> n))
1069 set_bit_inv(i + n, bitmap);
1070 i += 4;
1071 }
1072
3d8f60d3
HF
1073 if (*str == '\n')
1074 str++;
1075 if (*str)
1076 return -EINVAL;
1077 return 0;
1078}
6e697394 1079EXPORT_SYMBOL(ap_hex2bitmap);
3d8f60d3
HF
1080
1081/*
fa108f95
MS
1082 * modify_bitmap() - parse bitmask argument and modify an existing
1083 * bit mask accordingly. A concatenation (done with ',') of these
1084 * terms is recognized:
3d8f60d3
HF
1085 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1086 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1087 * 0...bits-1; the leading + or - is required. Here are some examples:
1088 * +0-15,+32,-128,-0xFF
1089 * -0-255,+1-16,+0x128
1090 * +1,+2,+3,+4,-5,-7-10
fa108f95
MS
1091 * Returns the new bitmap after all changes have been applied. Every
1092 * positive value in the string will set a bit and every negative value
1093 * in the string will clear a bit. As a bit may be touched more than once,
1094 * the last 'operation' wins:
1095 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1096 * cleared again. All other bits are unmodified.
3d8f60d3 1097 */
fa108f95 1098static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
3d8f60d3 1099{
d4f9d5a9 1100 unsigned long a, i, z;
3d8f60d3
HF
1101 char *np, sign;
1102
1103 /* bits needs to be a multiple of 8 */
1104 if (bits & 0x07)
7e0bdbe5
HF
1105 return -EINVAL;
1106
3d8f60d3
HF
1107 while (*str) {
1108 sign = *str++;
1109 if (sign != '+' && sign != '-')
1110 return -EINVAL;
1111 a = z = simple_strtoul(str, &np, 0);
1112 if (str == np || a >= bits)
1113 return -EINVAL;
1114 str = np;
1115 if (*str == '-') {
1116 z = simple_strtoul(++str, &np, 0);
1117 if (str == np || a > z || z >= bits)
1118 return -EINVAL;
1119 str = np;
1120 }
1121 for (i = a; i <= z; i++)
fa108f95
MS
1122 if (sign == '+')
1123 set_bit_inv(i, bitmap);
1124 else
1125 clear_bit_inv(i, bitmap);
3d8f60d3
HF
1126 while (*str == ',' || *str == '\n')
1127 str++;
1128 }
1129
7e0bdbe5
HF
1130 return 0;
1131}
1132
4f8206b8
TK
1133static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1134 unsigned long *newmap)
1135{
1136 unsigned long size;
1137 int rc;
1138
1139 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1140 if (*str == '+' || *str == '-') {
1141 memcpy(newmap, bitmap, size);
1142 rc = modify_bitmap(str, newmap, bits);
1143 } else {
1144 memset(newmap, 0, size);
6e697394 1145 rc = ap_hex2bitmap(str, newmap, bits);
4f8206b8
TK
1146 }
1147 return rc;
1148}
1149
00fab235
HF
1150int ap_parse_mask_str(const char *str,
1151 unsigned long *bitmap, int bits,
1152 struct mutex *lock)
3d8f60d3 1153{
fa108f95
MS
1154 unsigned long *newmap, size;
1155 int rc;
3d8f60d3
HF
1156
1157 /* bits needs to be a multiple of 8 */
1158 if (bits & 0x07)
1159 return -EINVAL;
1160
2004b57c 1161 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
fa108f95
MS
1162 newmap = kmalloc(size, GFP_KERNEL);
1163 if (!newmap)
1164 return -ENOMEM;
1165 if (mutex_lock_interruptible(lock)) {
1166 kfree(newmap);
1167 return -ERESTARTSYS;
1168 }
4f8206b8 1169 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
fa108f95
MS
1170 if (rc == 0)
1171 memcpy(bitmap, newmap, size);
3d8f60d3 1172 mutex_unlock(lock);
fa108f95
MS
1173 kfree(newmap);
1174 return rc;
3d8f60d3 1175}
00fab235 1176EXPORT_SYMBOL(ap_parse_mask_str);
3d8f60d3
HF
1177
1178/*
1179 * AP bus attributes.
1180 */
1181
75cff725 1182static ssize_t ap_domain_show(const struct bus_type *bus, char *buf)
1534c382 1183{
964d581d 1184 return sysfs_emit(buf, "%d\n", ap_domain_index);
1534c382
MS
1185}
1186
75cff725 1187static ssize_t ap_domain_store(const struct bus_type *bus,
fc1d3f02
IT
1188 const char *buf, size_t count)
1189{
1190 int domain;
1191
1192 if (sscanf(buf, "%i\n", &domain) != 1 ||
7e0bdbe5
HF
1193 domain < 0 || domain > ap_max_domain_id ||
1194 !test_bit_inv(domain, ap_perms.aqm))
fc1d3f02 1195 return -EINVAL;
c8337c47 1196
fc1d3f02
IT
1197 spin_lock_bh(&ap_domain_lock);
1198 ap_domain_index = domain;
1199 spin_unlock_bh(&ap_domain_lock);
cccd85bf 1200
3f74eb5f
HF
1201 AP_DBF_INFO("%s stored new default domain=%d\n",
1202 __func__, domain);
cccd85bf 1203
fc1d3f02
IT
1204 return count;
1205}
1206
ac2b96f3 1207static BUS_ATTR_RW(ap_domain);
1534c382 1208
75cff725 1209static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf)
91f3e3ea 1210{
8dec9cb9 1211 if (!ap_qci_info->flags) /* QCI not supported */
964d581d 1212 return sysfs_emit(buf, "not supported\n");
40501c70 1213
964d581d
HF
1214 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1215 ap_qci_info->adm[0], ap_qci_info->adm[1],
1216 ap_qci_info->adm[2], ap_qci_info->adm[3],
1217 ap_qci_info->adm[4], ap_qci_info->adm[5],
1218 ap_qci_info->adm[6], ap_qci_info->adm[7]);
91f3e3ea
IT
1219}
1220
ac2b96f3 1221static BUS_ATTR_RO(ap_control_domain_mask);
91f3e3ea 1222
75cff725 1223static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf)
e28d2af4 1224{
8dec9cb9 1225 if (!ap_qci_info->flags) /* QCI not supported */
964d581d 1226 return sysfs_emit(buf, "not supported\n");
40501c70 1227
964d581d
HF
1228 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1229 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1230 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1231 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1232 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
e28d2af4
IT
1233}
1234
ac2b96f3 1235static BUS_ATTR_RO(ap_usage_domain_mask);
1534c382 1236
75cff725 1237static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf)
aa55bf5f 1238{
8dec9cb9 1239 if (!ap_qci_info->flags) /* QCI not supported */
964d581d 1240 return sysfs_emit(buf, "not supported\n");
40501c70 1241
964d581d
HF
1242 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1243 ap_qci_info->apm[0], ap_qci_info->apm[1],
1244 ap_qci_info->apm[2], ap_qci_info->apm[3],
1245 ap_qci_info->apm[4], ap_qci_info->apm[5],
1246 ap_qci_info->apm[6], ap_qci_info->apm[7]);
aa55bf5f
HF
1247}
1248
1249static BUS_ATTR_RO(ap_adapter_mask);
1250
75cff725 1251static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf)
cb17a636 1252{
964d581d 1253 return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0);
cb17a636
FB
1254}
1255
ac2b96f3
HF
1256static BUS_ATTR_RO(ap_interrupts);
1257
75cff725 1258static ssize_t config_time_show(const struct bus_type *bus, char *buf)
ac2b96f3 1259{
99b3126e 1260 return sysfs_emit(buf, "%d\n", ap_scan_bus_time);
ac2b96f3 1261}
cb17a636 1262
75cff725 1263static ssize_t config_time_store(const struct bus_type *bus,
ac2b96f3 1264 const char *buf, size_t count)
1534c382
MS
1265{
1266 int time;
1267
1268 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1269 return -EINVAL;
99b3126e
HF
1270 ap_scan_bus_time = time;
1271 mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ);
1534c382
MS
1272 return count;
1273}
1274
ac2b96f3 1275static BUS_ATTR_RW(config_time);
1534c382 1276
75cff725 1277static ssize_t poll_thread_show(const struct bus_type *bus, char *buf)
1534c382 1278{
964d581d 1279 return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0);
1534c382
MS
1280}
1281
75cff725 1282static ssize_t poll_thread_store(const struct bus_type *bus,
ac2b96f3 1283 const char *buf, size_t count)
1534c382 1284{
263c8454
HF
1285 bool value;
1286 int rc;
1534c382 1287
263c8454
HF
1288 rc = kstrtobool(buf, &value);
1289 if (rc)
1290 return rc;
1291
1292 if (value) {
1534c382
MS
1293 rc = ap_poll_thread_start();
1294 if (rc)
83e9d5d2 1295 count = rc;
2004b57c 1296 } else {
1534c382 1297 ap_poll_thread_stop();
2004b57c 1298 }
1534c382
MS
1299 return count;
1300}
1301
ac2b96f3 1302static BUS_ATTR_RW(poll_thread);
1534c382 1303
75cff725 1304static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf)
fe137230 1305{
263c8454 1306 return sysfs_emit(buf, "%lu\n", poll_high_timeout);
fe137230
FB
1307}
1308
75cff725 1309static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf,
fe137230
FB
1310 size_t count)
1311{
263c8454 1312 unsigned long value;
fe137230 1313 ktime_t hr_time;
263c8454
HF
1314 int rc;
1315
1316 rc = kstrtoul(buf, 0, &value);
1317 if (rc)
1318 return rc;
fe137230
FB
1319
1320 /* 120 seconds = maximum poll interval */
263c8454 1321 if (value > 120000000000UL)
fe137230 1322 return -EINVAL;
263c8454
HF
1323 poll_high_timeout = value;
1324 hr_time = poll_high_timeout;
fe137230 1325
8cc2af7c
IT
1326 spin_lock_bh(&ap_poll_timer_lock);
1327 hrtimer_cancel(&ap_poll_timer);
1328 hrtimer_set_expires(&ap_poll_timer, hr_time);
1329 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1330 spin_unlock_bh(&ap_poll_timer_lock);
1331
fe137230
FB
1332 return count;
1333}
1334
ac2b96f3 1335static BUS_ATTR_RW(poll_timeout);
fe137230 1336
75cff725 1337static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf)
5bc334bf 1338{
964d581d 1339 return sysfs_emit(buf, "%d\n", ap_max_domain_id);
5bc334bf
IT
1340}
1341
ac2b96f3 1342static BUS_ATTR_RO(ap_max_domain_id);
5bc334bf 1343
75cff725 1344static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf)
c8337c47 1345{
964d581d 1346 return sysfs_emit(buf, "%d\n", ap_max_adapter_id);
c8337c47
HF
1347}
1348
1349static BUS_ATTR_RO(ap_max_adapter_id);
1350
75cff725 1351static ssize_t apmask_show(const struct bus_type *bus, char *buf)
7e0bdbe5
HF
1352{
1353 int rc;
1354
1355 if (mutex_lock_interruptible(&ap_perms_mutex))
1356 return -ERESTARTSYS;
964d581d
HF
1357 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1358 ap_perms.apm[0], ap_perms.apm[1],
1359 ap_perms.apm[2], ap_perms.apm[3]);
7e0bdbe5
HF
1360 mutex_unlock(&ap_perms_mutex);
1361
1362 return rc;
1363}
1364
4f8206b8
TK
1365static int __verify_card_reservations(struct device_driver *drv, void *data)
1366{
1367 int rc = 0;
1368 struct ap_driver *ap_drv = to_ap_drv(drv);
1369 unsigned long *newapm = (unsigned long *)data;
1370
1371 /*
1372 * increase the driver's module refcounter to be sure it is not
1373 * going away when we invoke the callback function.
1374 */
1375 if (!try_module_get(drv->owner))
1376 return 0;
1377
1378 if (ap_drv->in_use) {
1379 rc = ap_drv->in_use(newapm, ap_perms.aqm);
1380 if (rc)
1381 rc = -EBUSY;
1382 }
1383
1384 /* release the driver's module */
1385 module_put(drv->owner);
1386
1387 return rc;
1388}
1389
1390static int apmask_commit(unsigned long *newapm)
1391{
1392 int rc;
1393 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1394
1395 /*
1396 * Check if any bits in the apmask have been set which will
1397 * result in queues being removed from non-default drivers
1398 */
1399 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1400 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1401 __verify_card_reservations);
1402 if (rc)
1403 return rc;
1404 }
1405
1406 memcpy(ap_perms.apm, newapm, APMASKSIZE);
1407
1408 return 0;
1409}
1410
75cff725 1411static ssize_t apmask_store(const struct bus_type *bus, const char *buf,
7e0bdbe5
HF
1412 size_t count)
1413{
d9b38e9d 1414 int rc, changes = 0;
4f8206b8
TK
1415 DECLARE_BITMAP(newapm, AP_DEVICES);
1416
1417 if (mutex_lock_interruptible(&ap_perms_mutex))
1418 return -ERESTARTSYS;
7e0bdbe5 1419
4f8206b8
TK
1420 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1421 if (rc)
1422 goto done;
1423
d9b38e9d
HF
1424 changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
1425 if (changes)
1426 rc = apmask_commit(newapm);
4f8206b8
TK
1427
1428done:
1429 mutex_unlock(&ap_perms_mutex);
3d8f60d3
HF
1430 if (rc)
1431 return rc;
7e0bdbe5 1432
d9b38e9d
HF
1433 if (changes) {
1434 ap_bus_revise_bindings();
1435 ap_send_mask_changed_uevent(newapm, NULL);
1436 }
7e0bdbe5
HF
1437
1438 return count;
1439}
1440
1441static BUS_ATTR_RW(apmask);
1442
75cff725 1443static ssize_t aqmask_show(const struct bus_type *bus, char *buf)
7e0bdbe5
HF
1444{
1445 int rc;
1446
1447 if (mutex_lock_interruptible(&ap_perms_mutex))
1448 return -ERESTARTSYS;
964d581d
HF
1449 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1450 ap_perms.aqm[0], ap_perms.aqm[1],
1451 ap_perms.aqm[2], ap_perms.aqm[3]);
7e0bdbe5
HF
1452 mutex_unlock(&ap_perms_mutex);
1453
1454 return rc;
1455}
1456
4f8206b8
TK
1457static int __verify_queue_reservations(struct device_driver *drv, void *data)
1458{
1459 int rc = 0;
1460 struct ap_driver *ap_drv = to_ap_drv(drv);
1461 unsigned long *newaqm = (unsigned long *)data;
1462
1463 /*
1464 * increase the driver's module refcounter to be sure it is not
1465 * going away when we invoke the callback function.
1466 */
1467 if (!try_module_get(drv->owner))
1468 return 0;
1469
1470 if (ap_drv->in_use) {
1471 rc = ap_drv->in_use(ap_perms.apm, newaqm);
1472 if (rc)
2f23256c 1473 rc = -EBUSY;
4f8206b8
TK
1474 }
1475
1476 /* release the driver's module */
1477 module_put(drv->owner);
1478
1479 return rc;
1480}
1481
1482static int aqmask_commit(unsigned long *newaqm)
1483{
1484 int rc;
1485 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1486
1487 /*
1488 * Check if any bits in the aqmask have been set which will
1489 * result in queues being removed from non-default drivers
1490 */
1491 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1492 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1493 __verify_queue_reservations);
1494 if (rc)
1495 return rc;
1496 }
1497
1498 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1499
1500 return 0;
1501}
1502
75cff725 1503static ssize_t aqmask_store(const struct bus_type *bus, const char *buf,
7e0bdbe5
HF
1504 size_t count)
1505{
d9b38e9d 1506 int rc, changes = 0;
4f8206b8 1507 DECLARE_BITMAP(newaqm, AP_DOMAINS);
7e0bdbe5 1508
4f8206b8
TK
1509 if (mutex_lock_interruptible(&ap_perms_mutex))
1510 return -ERESTARTSYS;
1511
1512 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1513 if (rc)
1514 goto done;
1515
d9b38e9d
HF
1516 changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
1517 if (changes)
1518 rc = aqmask_commit(newaqm);
4f8206b8
TK
1519
1520done:
1521 mutex_unlock(&ap_perms_mutex);
3d8f60d3
HF
1522 if (rc)
1523 return rc;
7e0bdbe5 1524
d9b38e9d
HF
1525 if (changes) {
1526 ap_bus_revise_bindings();
1527 ap_send_mask_changed_uevent(NULL, newaqm);
1528 }
7e0bdbe5
HF
1529
1530 return count;
1531}
1532
1533static BUS_ATTR_RW(aqmask);
1534
75cff725 1535static ssize_t scans_show(const struct bus_type *bus, char *buf)
837cd105 1536{
964d581d 1537 return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count));
837cd105
HF
1538}
1539
75cff725 1540static ssize_t scans_store(const struct bus_type *bus, const char *buf,
8944d05f
HF
1541 size_t count)
1542{
1543 AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1544
1545 ap_bus_force_rescan();
1546
1547 return count;
1548}
1549
1550static BUS_ATTR_RW(scans);
837cd105 1551
75cff725 1552static ssize_t bindings_show(const struct bus_type *bus, char *buf)
837cd105
HF
1553{
1554 int rc;
1555 unsigned int apqns, n;
1556
1557 ap_calc_bound_apqns(&apqns, &n);
1558 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
964d581d 1559 rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns);
837cd105 1560 else
964d581d 1561 rc = sysfs_emit(buf, "%u/%u\n", n, apqns);
837cd105
HF
1562
1563 return rc;
1564}
1565
1566static BUS_ATTR_RO(bindings);
1567
10de638d 1568static ssize_t features_show(const struct bus_type *bus, char *buf)
d7b1813a
HF
1569{
1570 int n = 0;
1571
8dec9cb9 1572 if (!ap_qci_info->flags) /* QCI not supported */
d7b1813a
HF
1573 return sysfs_emit(buf, "-\n");
1574
1575 if (ap_qci_info->apsc)
1576 n += sysfs_emit_at(buf, n, "APSC ");
1577 if (ap_qci_info->apxa)
1578 n += sysfs_emit_at(buf, n, "APXA ");
1579 if (ap_qci_info->qact)
1580 n += sysfs_emit_at(buf, n, "QACT ");
1581 if (ap_qci_info->rc8a)
1582 n += sysfs_emit_at(buf, n, "RC8A ");
1583 if (ap_qci_info->apsb)
1584 n += sysfs_emit_at(buf, n, "APSB ");
1585
1586 sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n");
1587
1588 return n;
1589}
1590
1591static BUS_ATTR_RO(features);
1592
8cb4c20f
JW
1593static struct attribute *ap_bus_attrs[] = {
1594 &bus_attr_ap_domain.attr,
1595 &bus_attr_ap_control_domain_mask.attr,
1596 &bus_attr_ap_usage_domain_mask.attr,
1597 &bus_attr_ap_adapter_mask.attr,
1598 &bus_attr_config_time.attr,
1599 &bus_attr_poll_thread.attr,
1600 &bus_attr_ap_interrupts.attr,
1601 &bus_attr_poll_timeout.attr,
1602 &bus_attr_ap_max_domain_id.attr,
1603 &bus_attr_ap_max_adapter_id.attr,
1604 &bus_attr_apmask.attr,
1605 &bus_attr_aqmask.attr,
1606 &bus_attr_scans.attr,
1607 &bus_attr_bindings.attr,
d7b1813a 1608 &bus_attr_features.attr,
fe137230 1609 NULL,
1534c382 1610};
8cb4c20f
JW
1611ATTRIBUTE_GROUPS(ap_bus);
1612
5b431787 1613static const struct bus_type ap_bus_type = {
8cb4c20f
JW
1614 .name = "ap",
1615 .bus_groups = ap_bus_groups,
1616 .match = &ap_bus_match,
1617 .uevent = &ap_uevent,
95c09f03
JW
1618 .probe = ap_device_probe,
1619 .remove = ap_device_remove,
8cb4c20f 1620};
1534c382
MS
1621
1622/**
1c472d46
HP
1623 * ap_select_domain(): Select an AP domain if possible and we haven't
1624 * already done so before.
1534c382 1625 */
1c472d46 1626static void ap_select_domain(void)
1534c382 1627{
6acbe21f 1628 struct ap_queue_status status;
c8337c47 1629 int card, dom;
1534c382 1630
1749a81d 1631 /*
c8337c47
HF
1632 * Choose the default domain. Either the one specified with
1633 * the "domain=" parameter or the first domain with at least
1634 * one valid APQN.
1534c382 1635 */
fc1d3f02
IT
1636 spin_lock_bh(&ap_domain_lock);
1637 if (ap_domain_index >= 0) {
1534c382 1638 /* Domain has already been selected. */
c8337c47 1639 goto out;
fc1d3f02 1640 }
c8337c47
HF
1641 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1642 if (!ap_test_config_usage_domain(dom) ||
1643 !test_bit_inv(dom, ap_perms.aqm))
75014550 1644 continue;
c8337c47
HF
1645 for (card = 0; card <= ap_max_adapter_id; card++) {
1646 if (!ap_test_config_card_id(card) ||
1647 !test_bit_inv(card, ap_perms.apm))
75014550 1648 continue;
c8337c47 1649 status = ap_test_queue(AP_MKQID(card, dom),
e7fc5146
TK
1650 ap_apft_available(),
1651 NULL);
c8337c47
HF
1652 if (status.response_code == AP_RESPONSE_NORMAL)
1653 break;
1534c382 1654 }
c8337c47
HF
1655 if (card <= ap_max_adapter_id)
1656 break;
1534c382 1657 }
c8337c47
HF
1658 if (dom <= ap_max_domain_id) {
1659 ap_domain_index = dom;
2ea2a609
HF
1660 AP_DBF_INFO("%s new default domain is %d\n",
1661 __func__, ap_domain_index);
1534c382 1662 }
c8337c47 1663out:
fc1d3f02 1664 spin_unlock_bh(&ap_domain_lock);
1534c382
MS
1665}
1666
9a564108
HF
1667/*
1668 * This function checks the type and returns either 0 for not
1669 * supported or the highest compatible type value (which may
1670 * include the input type value).
1671 */
1672static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1673{
1674 int comp_type = 0;
1675
5ac8c724
HF
1676 /* < CEX4 is not supported */
1677 if (rawtype < AP_DEVICE_TYPE_CEX4) {
3f74eb5f
HF
1678 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1679 __func__, AP_QID_CARD(qid),
1680 AP_QID_QUEUE(qid), rawtype);
9a564108 1681 return 0;
2ea2a609 1682 }
985214af
HF
1683 /* up to CEX8 known and fully supported */
1684 if (rawtype <= AP_DEVICE_TYPE_CEX8)
9a564108
HF
1685 return rawtype;
1686 /*
985214af 1687 * unknown new type > CEX8, check for compatibility
9a564108 1688 * to the highest known and supported type which is
985214af 1689 * currently CEX8 with the help of the QACT function.
9a564108
HF
1690 */
1691 if (ap_qact_available()) {
1692 struct ap_queue_status status;
56c5c683 1693 union ap_qact_ap_info apinfo = {0};
9a564108
HF
1694
1695 apinfo.mode = (func >> 26) & 0x07;
985214af 1696 apinfo.cat = AP_DEVICE_TYPE_CEX8;
9a564108 1697 status = ap_qact(qid, 0, &apinfo);
2004b57c 1698 if (status.response_code == AP_RESPONSE_NORMAL &&
5ac8c724 1699 apinfo.cat >= AP_DEVICE_TYPE_CEX4 &&
2004b57c 1700 apinfo.cat <= AP_DEVICE_TYPE_CEX8)
9a564108
HF
1701 comp_type = apinfo.cat;
1702 }
1703 if (!comp_type)
3f74eb5f
HF
1704 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1705 __func__, AP_QID_CARD(qid),
1706 AP_QID_QUEUE(qid), rawtype);
9a564108 1707 else if (comp_type != rawtype)
3f74eb5f
HF
1708 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1709 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
2ea2a609 1710 rawtype, comp_type);
9a564108
HF
1711 return comp_type;
1712}
1713
e28d2af4 1714/*
a7b1868a 1715 * Helper function to be used with bus_find_dev
e28d2af4 1716 * matches for the card device with the given id
1534c382 1717 */
418e3ea1 1718static int __match_card_device_with_id(struct device *dev, const void *data)
1534c382 1719{
2004b57c 1720 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
1534c382
MS
1721}
1722
a7b1868a
HF
1723/*
1724 * Helper function to be used with bus_find_dev
e28d2af4
IT
1725 * matches for the queue device with a given qid
1726 */
418e3ea1 1727static int __match_queue_device_with_qid(struct device *dev, const void *data)
e28d2af4 1728{
2004b57c 1729 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
e28d2af4
IT
1730}
1731
b1af7528
HF
1732/*
1733 * Helper function to be used with bus_find_dev
1734 * matches any queue device with given queue id
1735 */
418e3ea1 1736static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
b1af7528 1737{
2004b57c
HF
1738 return is_queue_dev(dev) &&
1739 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
b1af7528
HF
1740}
1741
28391585
TK
1742/* Helper function for notify_config_changed */
1743static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1744{
1745 struct ap_driver *ap_drv = to_ap_drv(drv);
1746
1747 if (try_module_get(drv->owner)) {
1748 if (ap_drv->on_config_changed)
1749 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1750 module_put(drv->owner);
1751 }
1752
1753 return 0;
1754}
1755
1756/* Notify all drivers about an qci config change */
1757static inline void notify_config_changed(void)
1758{
1759 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1760 __drv_notify_config_changed);
1761}
1762
1763/* Helper function for notify_scan_complete */
1764static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1765{
1766 struct ap_driver *ap_drv = to_ap_drv(drv);
1767
1768 if (try_module_get(drv->owner)) {
1769 if (ap_drv->on_scan_complete)
1770 ap_drv->on_scan_complete(ap_qci_info,
1771 ap_qci_info_old);
1772 module_put(drv->owner);
1773 }
1774
1775 return 0;
1776}
1777
1778/* Notify all drivers about bus scan complete */
1779static inline void notify_scan_complete(void)
1780{
1781 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1782 __drv_notify_scan_complete);
1783}
1784
a7b1868a
HF
1785/*
1786 * Helper function for ap_scan_bus().
4f2fcccd
HF
1787 * Remove card device and associated queue devices.
1788 */
1789static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1790{
1791 bus_for_each_dev(&ap_bus_type, NULL,
2004b57c 1792 (void *)(long)ac->id,
4f2fcccd
HF
1793 __ap_queue_devices_with_id_unregister);
1794 device_unregister(&ac->ap_dev.device);
1795}
1796
1797/*
1798 * Helper function for ap_scan_bus().
1799 * Does the scan bus job for all the domains within
1800 * a valid adapter given by an ap_card ptr.
e28d2af4 1801 */
4f2fcccd 1802static inline void ap_scan_domains(struct ap_card *ac)
1534c382 1803{
d4c53ae8 1804 struct ap_tapq_hwinfo hwinfo;
a7e701db 1805 bool decfg, chkstop;
a7b1868a 1806 struct ap_queue *aq;
038c5bed 1807 struct device *dev;
038c5bed 1808 ap_qid_t qid;
d4c53ae8 1809 int rc, dom;
4f2fcccd
HF
1810
1811 /*
1812 * Go through the configuration for the domains and compare them
1813 * to the existing queue devices. Also take care of the config
1814 * and error state for the queue devices.
1815 */
1816
1817 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1818 qid = AP_MKQID(ac->id, dom);
1819 dev = bus_find_device(&ap_bus_type, NULL,
2004b57c 1820 (void *)(long)qid,
4f2fcccd
HF
1821 __match_queue_device_with_qid);
1822 aq = dev ? to_ap_queue(dev) : NULL;
1823 if (!ap_test_config_usage_domain(dom)) {
1824 if (dev) {
3f74eb5f 1825 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
4f2fcccd
HF
1826 __func__, ac->id, dom);
1827 device_unregister(dev);
4f2fcccd 1828 }
038c5bed 1829 goto put_dev_and_continue;
4f2fcccd
HF
1830 }
1831 /* domain is valid, get info from this APQN */
d4c53ae8 1832 rc = ap_queue_info(qid, &hwinfo, &decfg, &chkstop);
038c5bed
HF
1833 switch (rc) {
1834 case -1:
1835 if (dev) {
3f74eb5f
HF
1836 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1837 __func__, ac->id, dom);
4f2fcccd 1838 device_unregister(dev);
4f2fcccd 1839 }
038c5bed
HF
1840 fallthrough;
1841 case 0:
1842 goto put_dev_and_continue;
1843 default:
1844 break;
4f2fcccd
HF
1845 }
1846 /* if no queue device exists, create a new one */
1847 if (!aq) {
1848 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1849 if (!aq) {
1850 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1851 __func__, ac->id, dom);
1852 continue;
1853 }
1854 aq->card = ac;
1855 aq->config = !decfg;
a7e701db 1856 aq->chkstop = chkstop;
207022d3 1857 aq->se_bstate = hwinfo.bs;
4f2fcccd
HF
1858 dev = &aq->ap_dev.device;
1859 dev->bus = &ap_bus_type;
1860 dev->parent = &ac->ap_dev.device;
1861 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1862 /* register queue device */
1863 rc = device_register(dev);
1864 if (rc) {
1865 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1866 __func__, ac->id, dom);
1867 goto put_dev_and_continue;
1868 }
29c2680f
HF
1869 /* get it and thus adjust reference counter */
1870 get_device(dev);
32d1d920 1871 if (decfg) {
3f74eb5f 1872 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
4f2fcccd 1873 __func__, ac->id, dom);
32d1d920 1874 } else if (chkstop) {
a7e701db
HF
1875 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1876 __func__, ac->id, dom);
32d1d920
HF
1877 } else {
1878 /* nudge the queue's state machine */
1879 ap_queue_init_state(aq);
3f74eb5f 1880 AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
4f2fcccd 1881 __func__, ac->id, dom);
32d1d920 1882 }
4f2fcccd
HF
1883 goto put_dev_and_continue;
1884 }
a7e701db 1885 /* handle state changes on already existing queue device */
4f2fcccd 1886 spin_lock_bh(&aq->lock);
207022d3
HF
1887 /* SE bind state */
1888 aq->se_bstate = hwinfo.bs;
a7e701db
HF
1889 /* checkstop state */
1890 if (chkstop && !aq->chkstop) {
1891 /* checkstop on */
1892 aq->chkstop = true;
1893 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1894 aq->dev_state = AP_DEV_STATE_ERROR;
1895 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1896 }
1897 spin_unlock_bh(&aq->lock);
08b2c370
HF
1898 pr_debug("%s(%d,%d) queue dev checkstop on\n",
1899 __func__, ac->id, dom);
a7e701db
HF
1900 /* 'receive' pending messages with -EAGAIN */
1901 ap_flush_queue(aq);
1902 goto put_dev_and_continue;
1903 } else if (!chkstop && aq->chkstop) {
1904 /* checkstop off */
1905 aq->chkstop = false;
32d1d920
HF
1906 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
1907 _ap_queue_init_state(aq);
a7e701db 1908 spin_unlock_bh(&aq->lock);
08b2c370
HF
1909 pr_debug("%s(%d,%d) queue dev checkstop off\n",
1910 __func__, ac->id, dom);
a7e701db
HF
1911 goto put_dev_and_continue;
1912 }
1913 /* config state change */
4f2fcccd
HF
1914 if (decfg && aq->config) {
1915 /* config off this queue device */
1916 aq->config = false;
1917 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1918 aq->dev_state = AP_DEV_STATE_ERROR;
1919 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1920 }
1921 spin_unlock_bh(&aq->lock);
08b2c370
HF
1922 pr_debug("%s(%d,%d) queue dev config off\n",
1923 __func__, ac->id, dom);
df6f508c 1924 ap_send_config_uevent(&aq->ap_dev, aq->config);
4f2fcccd
HF
1925 /* 'receive' pending messages with -EAGAIN */
1926 ap_flush_queue(aq);
1927 goto put_dev_and_continue;
a7e701db 1928 } else if (!decfg && !aq->config) {
4f2fcccd
HF
1929 /* config on this queue device */
1930 aq->config = true;
32d1d920
HF
1931 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
1932 _ap_queue_init_state(aq);
4f2fcccd 1933 spin_unlock_bh(&aq->lock);
08b2c370
HF
1934 pr_debug("%s(%d,%d) queue dev config on\n",
1935 __func__, ac->id, dom);
df6f508c 1936 ap_send_config_uevent(&aq->ap_dev, aq->config);
4f2fcccd
HF
1937 goto put_dev_and_continue;
1938 }
1939 /* handle other error states */
1940 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1941 spin_unlock_bh(&aq->lock);
1942 /* 'receive' pending messages with -EAGAIN */
1943 ap_flush_queue(aq);
1944 /* re-init (with reset) the queue device */
1945 ap_queue_init_state(aq);
3f74eb5f 1946 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
4f2fcccd
HF
1947 __func__, ac->id, dom);
1948 goto put_dev_and_continue;
1949 }
1950 spin_unlock_bh(&aq->lock);
1951put_dev_and_continue:
1952 put_device(dev);
1953 }
1954}
1955
1956/*
1957 * Helper function for ap_scan_bus().
1958 * Does the scan bus job for the given adapter id.
1959 */
1960static inline void ap_scan_adapter(int ap)
1961{
d4c53ae8
HF
1962 struct ap_tapq_hwinfo hwinfo;
1963 int rc, dom, comp_type;
a7e701db 1964 bool decfg, chkstop;
4f2fcccd 1965 struct ap_card *ac;
038c5bed 1966 struct device *dev;
038c5bed 1967 ap_qid_t qid;
a7b1868a 1968
4f2fcccd 1969 /* Is there currently a card device for this adapter ? */
a7b1868a 1970 dev = bus_find_device(&ap_bus_type, NULL,
2004b57c 1971 (void *)(long)ap,
a7b1868a
HF
1972 __match_card_device_with_id);
1973 ac = dev ? to_ap_card(dev) : NULL;
4f2fcccd
HF
1974
1975 /* Adapter not in configuration ? */
1976 if (!ap_test_config_card_id(ap)) {
1977 if (ac) {
3f74eb5f 1978 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
4f2fcccd
HF
1979 __func__, ap);
1980 ap_scan_rm_card_dev_and_queue_devs(ac);
a7b1868a
HF
1981 put_device(dev);
1982 }
1983 return;
1984 }
cccd85bf 1985
a7b1868a 1986 /*
4f2fcccd
HF
1987 * Adapter ap is valid in the current configuration. So do some checks:
1988 * If no card device exists, build one. If a card device exists, check
1989 * for type and functions changed. For all this we need to find a valid
1990 * APQN first.
a7b1868a 1991 */
4f2fcccd
HF
1992
1993 for (dom = 0; dom <= ap_max_domain_id; dom++)
1994 if (ap_test_config_usage_domain(dom)) {
1995 qid = AP_MKQID(ap, dom);
d4c53ae8 1996 if (ap_queue_info(qid, &hwinfo, &decfg, &chkstop) > 0)
a7b1868a
HF
1997 break;
1998 }
4f2fcccd 1999 if (dom > ap_max_domain_id) {
038c5bed 2000 /* Could not find one valid APQN for this adapter */
4f2fcccd 2001 if (ac) {
3f74eb5f
HF
2002 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
2003 __func__, ap);
4f2fcccd
HF
2004 ap_scan_rm_card_dev_and_queue_devs(ac);
2005 put_device(dev);
2006 } else {
08b2c370
HF
2007 pr_debug("%s(%d) no type info (no APQN found), ignored\n",
2008 __func__, ap);
a7b1868a 2009 }
4f2fcccd
HF
2010 return;
2011 }
d4c53ae8 2012 if (!hwinfo.at) {
4f2fcccd
HF
2013 /* No apdater type info available, an unusable adapter */
2014 if (ac) {
3f74eb5f 2015 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
4f2fcccd
HF
2016 __func__, ap);
2017 ap_scan_rm_card_dev_and_queue_devs(ac);
a7b1868a 2018 put_device(dev);
4f2fcccd 2019 } else {
08b2c370
HF
2020 pr_debug("%s(%d) no valid type (0) info, ignored\n",
2021 __func__, ap);
a7b1868a 2022 }
4f2fcccd 2023 return;
a7b1868a 2024 }
d4c53ae8 2025 hwinfo.value &= TAPQ_CARD_HWINFO_MASK; /* filter card specific hwinfo */
4f2fcccd
HF
2026 if (ac) {
2027 /* Check APQN against existing card device for changes */
d4c53ae8 2028 if (ac->hwinfo.at != hwinfo.at) {
3f74eb5f 2029 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
d4c53ae8 2030 __func__, ap, hwinfo.at);
4f2fcccd
HF
2031 ap_scan_rm_card_dev_and_queue_devs(ac);
2032 put_device(dev);
2033 ac = NULL;
d4c53ae8 2034 } else if (ac->hwinfo.fac != hwinfo.fac) {
3f74eb5f 2035 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
d4c53ae8 2036 __func__, ap, hwinfo.fac);
4f2fcccd
HF
2037 ap_scan_rm_card_dev_and_queue_devs(ac);
2038 put_device(dev);
2039 ac = NULL;
2040 } else {
a7e701db
HF
2041 /* handle checkstop state change */
2042 if (chkstop && !ac->chkstop) {
2043 /* checkstop on */
2044 ac->chkstop = true;
2045 AP_DBF_INFO("%s(%d) card dev checkstop on\n",
2046 __func__, ap);
2047 } else if (!chkstop && ac->chkstop) {
2048 /* checkstop off */
2049 ac->chkstop = false;
2050 AP_DBF_INFO("%s(%d) card dev checkstop off\n",
2051 __func__, ap);
2052 }
2053 /* handle config state change */
4f2fcccd
HF
2054 if (decfg && ac->config) {
2055 ac->config = false;
3f74eb5f 2056 AP_DBF_INFO("%s(%d) card dev config off\n",
4f2fcccd 2057 __func__, ap);
df6f508c 2058 ap_send_config_uevent(&ac->ap_dev, ac->config);
a7e701db 2059 } else if (!decfg && !ac->config) {
4f2fcccd 2060 ac->config = true;
3f74eb5f 2061 AP_DBF_INFO("%s(%d) card dev config on\n",
4f2fcccd 2062 __func__, ap);
df6f508c 2063 ap_send_config_uevent(&ac->ap_dev, ac->config);
b1af7528 2064 }
a7b1868a 2065 }
4f2fcccd
HF
2066 }
2067
2068 if (!ac) {
2069 /* Build a new card device */
d4c53ae8 2070 comp_type = ap_get_compatible_type(qid, hwinfo.at, hwinfo.fac);
4f2fcccd
HF
2071 if (!comp_type) {
2072 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
d4c53ae8 2073 __func__, ap, hwinfo.at);
4f2fcccd
HF
2074 return;
2075 }
d4c53ae8 2076 ac = ap_card_create(ap, hwinfo, comp_type);
a7b1868a 2077 if (!ac) {
4f2fcccd
HF
2078 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
2079 __func__, ap);
2080 return;
a7b1868a 2081 }
4f2fcccd 2082 ac->config = !decfg;
a7e701db 2083 ac->chkstop = chkstop;
4f2fcccd
HF
2084 dev = &ac->ap_dev.device;
2085 dev->bus = &ap_bus_type;
2086 dev->parent = ap_root_device;
2087 dev_set_name(dev, "card%02x", ap);
bd39654a
HF
2088 /* maybe enlarge ap_max_msg_size to support this card */
2089 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
2090 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
2091 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
3f74eb5f
HF
2092 __func__, ap,
2093 atomic_read(&ap_max_msg_size));
bd39654a 2094 }
4f2fcccd
HF
2095 /* Register the new card device with AP bus */
2096 rc = device_register(dev);
a7b1868a 2097 if (rc) {
4f2fcccd
HF
2098 AP_DBF_WARN("%s(%d) device_register() failed\n",
2099 __func__, ap);
2100 put_device(dev);
2101 return;
3f3007af 2102 }
4f2fcccd
HF
2103 /* get it and thus adjust reference counter */
2104 get_device(dev);
2105 if (decfg)
3f74eb5f 2106 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
d4c53ae8 2107 __func__, ap, hwinfo.at, hwinfo.fac);
a7e701db
HF
2108 else if (chkstop)
2109 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
d4c53ae8 2110 __func__, ap, hwinfo.at, hwinfo.fac);
4f2fcccd 2111 else
3f74eb5f 2112 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
d4c53ae8 2113 __func__, ap, hwinfo.at, hwinfo.fac);
4f2fcccd
HF
2114 }
2115
2116 /* Verify the domains and the queue devices for this card */
2117 ap_scan_domains(ac);
a7b1868a 2118
4f2fcccd
HF
2119 /* release the card device */
2120 put_device(&ac->ap_dev.device);
a7b1868a 2121}
ac994e80 2122
28391585
TK
2123/**
2124 * ap_get_configuration - get the host AP configuration
2125 *
2126 * Stores the host AP configuration information returned from the previous call
2127 * to Query Configuration Information (QCI), then retrieves and stores the
2128 * current AP configuration returned from QCI.
2129 *
2130 * Return: true if the host AP configuration changed between calls to QCI;
2131 * otherwise, return false.
2132 */
2133static bool ap_get_configuration(void)
2134{
8dec9cb9 2135 if (!ap_qci_info->flags) /* QCI not supported */
0fef40be
HF
2136 return false;
2137
28391585 2138 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
8dec9cb9 2139 ap_qci(ap_qci_info);
28391585
TK
2140
2141 return memcmp(ap_qci_info, ap_qci_info_old,
2142 sizeof(struct ap_config_info)) != 0;
2143}
2144
778412ab
HF
2145/*
2146 * ap_config_has_new_aps - Check current against old qci info if
2147 * new adapters have appeared. Returns true if at least one new
2148 * adapter in the apm mask is showing up. Existing adapters or
2149 * receding adapters are not counted.
2150 */
2151static bool ap_config_has_new_aps(void)
2152{
2153
2154 unsigned long m[BITS_TO_LONGS(AP_DEVICES)];
2155
8dec9cb9 2156 if (!ap_qci_info->flags)
778412ab
HF
2157 return false;
2158
2159 bitmap_andnot(m, (unsigned long *)ap_qci_info->apm,
2160 (unsigned long *)ap_qci_info_old->apm, AP_DEVICES);
2161 if (!bitmap_empty(m, AP_DEVICES))
2162 return true;
2163
2164 return false;
2165}
2166
2167/*
2168 * ap_config_has_new_doms - Check current against old qci info if
2169 * new (usage) domains have appeared. Returns true if at least one
2170 * new domain in the aqm mask is showing up. Existing domains or
2171 * receding domains are not counted.
2172 */
2173static bool ap_config_has_new_doms(void)
2174{
2175 unsigned long m[BITS_TO_LONGS(AP_DOMAINS)];
2176
8dec9cb9 2177 if (!ap_qci_info->flags)
778412ab
HF
2178 return false;
2179
2180 bitmap_andnot(m, (unsigned long *)ap_qci_info->aqm,
2181 (unsigned long *)ap_qci_info_old->aqm, AP_DOMAINS);
2182 if (!bitmap_empty(m, AP_DOMAINS))
2183 return true;
2184
2185 return false;
2186}
2187
a7b1868a
HF
2188/**
2189 * ap_scan_bus(): Scan the AP bus for new devices
eacf5b36
HF
2190 * Always run under mutex ap_scan_bus_mutex protection
2191 * which needs to get locked/unlocked by the caller!
b5caf05e 2192 * Returns true if any config change has been detected
eacf5b36 2193 * during the scan, otherwise false.
a7b1868a 2194 */
b5caf05e 2195static bool ap_scan_bus(void)
a7b1868a 2196{
b5caf05e
HF
2197 bool config_changed;
2198 int ap;
a7b1868a 2199
08b2c370
HF
2200 pr_debug(">%s\n", __func__);
2201
778412ab 2202 /* (re-)fetch configuration via QCI */
28391585 2203 config_changed = ap_get_configuration();
778412ab
HF
2204 if (config_changed) {
2205 if (ap_config_has_new_aps() || ap_config_has_new_doms()) {
2206 /*
2207 * Appearance of new adapters and/or domains need to
2208 * build new ap devices which need to get bound to an
2209 * device driver. Thus reset the APQN bindings complete
2210 * completion.
2211 */
2212 reinit_completion(&ap_apqn_bindings_complete);
2213 }
2214 /* post a config change notify */
28391585 2215 notify_config_changed();
778412ab 2216 }
a7b1868a
HF
2217 ap_select_domain();
2218
2219 /* loop over all possible adapters */
4f2fcccd
HF
2220 for (ap = 0; ap <= ap_max_adapter_id; ap++)
2221 ap_scan_adapter(ap);
a7b1868a 2222
28391585
TK
2223 /* scan complete notify */
2224 if (config_changed)
2225 notify_scan_complete();
2226
a7b1868a
HF
2227 /* check if there is at least one queue available with default domain */
2228 if (ap_domain_index >= 0) {
2229 struct device *dev =
2230 bus_find_device(&ap_bus_type, NULL,
2004b57c 2231 (void *)(long)ap_domain_index,
b1af7528 2232 __match_queue_device_with_queue_id);
a7b1868a
HF
2233 if (dev)
2234 put_device(dev);
2235 else
3f74eb5f
HF
2236 AP_DBF_INFO("%s no queue device with default domain %d available\n",
2237 __func__, ap_domain_index);
a7b1868a 2238 }
ac994e80 2239
837cd105 2240 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
08b2c370 2241 pr_debug("%s init scan complete\n", __func__);
837cd105 2242 ap_send_init_scan_done_uevent();
837cd105
HF
2243 }
2244
778412ab
HF
2245 ap_check_bindings_complete();
2246
99b3126e 2247 mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ);
08b2c370 2248
b5caf05e
HF
2249 pr_debug("<%s config_changed=%d\n", __func__, config_changed);
2250
2251 return config_changed;
1534c382
MS
2252}
2253
99b3126e
HF
2254/*
2255 * Callback for the ap_scan_bus_timer
b5caf05e 2256 * Runs periodically, workqueue timer (ap_scan_bus_time)
99b3126e
HF
2257 */
2258static void ap_scan_bus_timer_callback(struct timer_list *unused)
1534c382 2259{
99b3126e
HF
2260 /*
2261 * schedule work into the system long wq which when
2262 * the work is finally executed, calls the AP bus scan.
2263 */
2264 queue_work(system_long_wq, &ap_scan_bus_work);
1534c382 2265}
13e742ba 2266
b5caf05e
HF
2267/*
2268 * Callback for the ap_scan_bus_work
2269 */
2270static void ap_scan_bus_wq_callback(struct work_struct *unused)
2271{
eacf5b36
HF
2272 /*
2273 * Try to invoke an ap_scan_bus(). If the mutex acquisition
2274 * fails there is currently another task already running the
2275 * AP scan bus and there is no need to wait and re-trigger the
2276 * scan again. Please note at the end of the scan bus function
2277 * the AP scan bus timer is re-armed which triggers then the
2278 * ap_scan_bus_timer_callback which enqueues a work into the
2279 * system_long_wq which invokes this function here again.
2280 */
2281 if (mutex_trylock(&ap_scan_bus_mutex)) {
2282 ap_scan_bus_result = ap_scan_bus();
2283 mutex_unlock(&ap_scan_bus_mutex);
2284 }
b5caf05e
HF
2285}
2286
12376084
HD
2287static inline void __exit ap_async_exit(void)
2288{
2289 if (ap_thread_flag)
2290 ap_poll_thread_stop();
2291 chsc_notifier_unregister(&ap_bus_nb);
2292 cancel_work(&ap_scan_bus_work);
2293 hrtimer_cancel(&ap_poll_timer);
2294 timer_delete(&ap_scan_bus_timer);
2295}
2296
170660cc
HD
2297static inline int __init ap_async_init(void)
2298{
2299 int rc;
2300
2301 /* Setup the AP bus rescan timer. */
2302 timer_setup(&ap_scan_bus_timer, ap_scan_bus_timer_callback, 0);
2303
2304 /*
2305 * Setup the high resolution poll timer.
2306 * If we are running under z/VM adjust polling to z/VM polling rate.
2307 */
2308 if (MACHINE_IS_VM)
2309 poll_high_timeout = 1500000;
2310 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2311 ap_poll_timer.function = ap_poll_timeout;
2312
2313 queue_work(system_long_wq, &ap_scan_bus_work);
2314
2a483d33
HD
2315 rc = chsc_notifier_register(&ap_bus_nb);
2316 if (rc)
2317 goto out;
2318
170660cc
HD
2319 /* Start the low priority AP bus poll thread. */
2320 if (!ap_thread_flag)
2321 return 0;
2322
2323 rc = ap_poll_thread_start();
2324 if (rc)
2a483d33 2325 goto out_notifier;
170660cc
HD
2326
2327 return 0;
2328
2a483d33
HD
2329out_notifier:
2330 chsc_notifier_unregister(&ap_bus_nb);
170660cc
HD
2331out:
2332 cancel_work(&ap_scan_bus_work);
2333 hrtimer_cancel(&ap_poll_timer);
2334 timer_delete(&ap_scan_bus_timer);
2335 return rc;
2336}
2337
2338static inline void ap_irq_exit(void)
2339{
2340 if (ap_irq_flag)
2341 unregister_adapter_interrupt(&ap_airq);
2342}
2343
2344static inline int __init ap_irq_init(void)
2345{
2346 int rc;
2347
2348 if (!ap_interrupts_available() || !ap_useirq)
2349 return 0;
2350
2351 rc = register_adapter_interrupt(&ap_airq);
2352 ap_irq_flag = (rc == 0);
2353
2354 return rc;
2355}
2356
2357static inline void ap_debug_exit(void)
2358{
2359 debug_unregister(ap_dbf_info);
2360}
2361
2362static inline int __init ap_debug_init(void)
cccd85bf 2363{
3f74eb5f 2364 ap_dbf_info = debug_register("ap", 2, 1,
88e4c0da 2365 AP_DBF_MAX_SPRINTF_ARGS * sizeof(long));
cccd85bf
HF
2366 debug_register_view(ap_dbf_info, &debug_sprintf_view);
2367 debug_set_level(ap_dbf_info, DBF_ERR);
2368
2369 return 0;
2370}
2371
7e0bdbe5
HF
2372static void __init ap_perms_init(void)
2373{
2004b57c 2374 /* all resources usable if no kernel parameter string given */
00fab235 2375 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
7e0bdbe5
HF
2376 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2377 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2378
3d8f60d3 2379 /* apm kernel parameter string */
7e0bdbe5 2380 if (apm_str) {
3d8f60d3 2381 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
00fab235
HF
2382 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2383 &ap_perms_mutex);
7e0bdbe5 2384 }
7e0bdbe5 2385
3d8f60d3
HF
2386 /* aqm kernel parameter string */
2387 if (aqm_str) {
2388 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
00fab235
HF
2389 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2390 &ap_perms_mutex);
7e0bdbe5
HF
2391 }
2392}
2393
1534c382 2394/**
1749a81d
FB
2395 * ap_module_init(): The module initialization code.
2396 *
2397 * Initializes the module.
1534c382 2398 */
efda7ade 2399static int __init ap_module_init(void)
1534c382 2400{
8cb4c20f 2401 int rc;
1534c382 2402
cccd85bf
HF
2403 rc = ap_debug_init();
2404 if (rc)
2405 return rc;
2406
2395103b 2407 if (!ap_instructions_available()) {
889875a1
MS
2408 pr_warn("The hardware system does not support AP instructions\n");
2409 return -ENODEV;
2410 }
2411
bc4b295e
HF
2412 /* init ap_queue hashtable */
2413 hash_init(ap_queues);
2414
00fab235 2415 /* set up the AP permissions (ioctls, ap and aq masks) */
7e0bdbe5
HF
2416 ap_perms_init();
2417
889875a1 2418 /* Get AP configuration data if available */
c8337c47
HF
2419 ap_init_qci_info();
2420
2421 /* check default domain setting */
2422 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
7e0bdbe5
HF
2423 (ap_domain_index >= 0 &&
2424 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
889875a1
MS
2425 pr_warn("%d is not a valid cryptographic domain\n",
2426 ap_domain_index);
ac994e80 2427 ap_domain_index = -1;
1534c382 2428 }
5314af69 2429
1534c382
MS
2430 /* Create /sys/bus/ap. */
2431 rc = bus_register(&ap_bus_type);
2432 if (rc)
3c7a3773 2433 goto out;
1534c382
MS
2434
2435 /* Create /sys/devices/ap. */
035da16f 2436 ap_root_device = root_device_register("ap");
9c705206 2437 rc = PTR_ERR_OR_ZERO(ap_root_device);
1534c382
MS
2438 if (rc)
2439 goto out_bus;
837cd105 2440 ap_root_device->bus = &ap_bus_type;
1534c382 2441
3c7a3773
HD
2442 /* enable interrupts if available */
2443 rc = ap_irq_init();
2444 if (rc)
2445 goto out_device;
2446
170660cc
HD
2447 /* Setup asynchronous work (timers, workqueue, etc). */
2448 rc = ap_async_init();
2449 if (rc)
3c7a3773 2450 goto out_irq;
3f3007af 2451
1534c382
MS
2452 return 0;
2453
3c7a3773
HD
2454out_irq:
2455 ap_irq_exit();
170660cc 2456out_device:
035da16f 2457 root_device_unregister(ap_root_device);
1534c382 2458out_bus:
1534c382
MS
2459 bus_unregister(&ap_bus_type);
2460out:
170660cc 2461 ap_debug_exit();
1534c382
MS
2462 return rc;
2463}
12376084
HD
2464
2465static void __exit ap_module_exit(void)
2466{
2467 ap_async_exit();
2468 ap_irq_exit();
2469 root_device_unregister(ap_root_device);
2470 bus_unregister(&ap_bus_type);
2471 ap_debug_exit();
2472}
2473
2474module_init(ap_module_init);
2475module_exit(ap_module_exit);