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