bus: Make remove callback return void
[linux-2.6-block.git] / drivers / s390 / crypto / ap_bus.c
CommitLineData
812141a9 1// SPDX-License-Identifier: GPL-2.0+
1534c382 2/*
0677519a 3 * Copyright IBM Corp. 2006, 2021
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>
60063497 30#include <linux/atomic.h>
cb17a636 31#include <asm/isc.h>
fe137230
FB
32#include <linux/hrtimer.h>
33#include <linux/ktime.h>
a0616cde 34#include <asm/facility.h>
5d26a105 35#include <linux/crypto.h>
e28d2af4 36#include <linux/mod_devicetable.h>
cccd85bf 37#include <linux/debugfs.h>
7e0bdbe5 38#include <linux/ctype.h>
1534c382
MS
39
40#include "ap_bus.h"
cccd85bf 41#include "ap_debug.h"
1534c382 42
1749a81d 43/*
50a0d46c 44 * Module parameters; note though this file itself isn't modular.
1534c382
MS
45 */
46int ap_domain_index = -1; /* Adjunct Processor Domain Index */
fc1d3f02 47static DEFINE_SPINLOCK(ap_domain_lock);
ac2b96f3 48module_param_named(domain, ap_domain_index, int, 0440);
1534c382
MS
49MODULE_PARM_DESC(domain, "domain index for ap devices");
50EXPORT_SYMBOL(ap_domain_index);
51
ac2b96f3
HF
52static int ap_thread_flag;
53module_param_named(poll_thread, ap_thread_flag, int, 0440);
b90b34c6 54MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
1534c382 55
7e0bdbe5
HF
56static char *apm_str;
57module_param_named(apmask, apm_str, charp, 0440);
58MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
59
60static char *aqm_str;
61module_param_named(aqmask, aqm_str, charp, 0440);
62MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
63
bd39654a
HF
64atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
65EXPORT_SYMBOL(ap_max_msg_size);
66
e28d2af4
IT
67static struct device *ap_root_device;
68
bc4b295e
HF
69/* Hashtable of all queue devices on the AP bus */
70DEFINE_HASHTABLE(ap_queues, 8);
71/* lock used for the ap_queues hashtable */
72DEFINE_SPINLOCK(ap_queues_lock);
e28d2af4 73
00fab235
HF
74/* Default permissions (ioctl, card and domain masking) */
75struct ap_perms ap_perms;
76EXPORT_SYMBOL(ap_perms);
77DEFINE_MUTEX(ap_perms_mutex);
78EXPORT_SYMBOL(ap_perms_mutex);
7e0bdbe5 79
837cd105
HF
80/* # of bus scans since init */
81static atomic64_t ap_scan_bus_count;
82
0677519a
HF
83/* # of bindings complete since init */
84static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
85
837cd105
HF
86/* completion for initial APQN bindings complete */
87static DECLARE_COMPLETION(ap_init_apqn_bindings_complete);
88
c8337c47 89static struct ap_config_info *ap_qci_info;
1534c382 90
cccd85bf
HF
91/*
92 * AP bus related debug feature things.
93 */
cccd85bf
HF
94debug_info_t *ap_dbf_info;
95
1749a81d 96/*
8139b89d 97 * Workqueue timer for bus rescan.
1534c382 98 */
1534c382
MS
99static struct timer_list ap_config_timer;
100static int ap_config_time = AP_CONFIG_TIME;
3f3007af 101static void ap_scan_bus(struct work_struct *);
8139b89d 102static DECLARE_WORK(ap_scan_work, ap_scan_bus);
1534c382 103
1749a81d 104/*
cb17a636 105 * Tasklet & timer for AP request polling and interrupts
1534c382 106 */
3f3007af 107static void ap_tasklet_fn(unsigned long);
b13fecb1 108static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
1534c382 109static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
ac2b96f3 110static struct task_struct *ap_poll_kthread;
1534c382 111static DEFINE_MUTEX(ap_poll_thread_mutex);
93521314 112static DEFINE_SPINLOCK(ap_poll_timer_lock);
fe137230 113static struct hrtimer ap_poll_timer;
ac2b96f3
HF
114/*
115 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
116 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
117 */
fe137230 118static unsigned long long poll_timeout = 250000;
1534c382 119
c8337c47
HF
120/* Maximum domain id, if not given via qci */
121static int ap_max_domain_id = 15;
122/* Maximum adapter id, if not given via qci */
123static int ap_max_adapter_id = 63;
41677b1d 124
772f5472
FB
125static struct bus_type ap_bus_type;
126
f4eae94f 127/* Adapter interrupt definitions */
30e63ef2 128static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
3f3007af 129
f4eae94f
MS
130static int ap_airq_flag;
131
132static struct airq_struct ap_airq = {
133 .handler = ap_interrupt_handler,
134 .isc = AP_ISC,
135};
136
cb17a636
FB
137/**
138 * ap_using_interrupts() - Returns non-zero if interrupt support is
139 * available.
140 */
141static inline int ap_using_interrupts(void)
142{
f4eae94f 143 return ap_airq_flag;
cb17a636
FB
144}
145
e28d2af4
IT
146/**
147 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
148 *
149 * Returns the address of the local-summary-indicator of the adapter
150 * interrupt handler for AP, or NULL if adapter interrupts are not
151 * available.
152 */
153void *ap_airq_ptr(void)
154{
155 if (ap_using_interrupts())
156 return ap_airq.lsi_ptr;
157 return NULL;
158}
159
cb17a636
FB
160/**
161 * ap_interrupts_available(): Test if AP interrupts are available.
162 *
163 * Returns 1 if AP interrupts are available.
164 */
165static int ap_interrupts_available(void)
166{
86cd741b 167 return test_facility(65);
cb17a636
FB
168}
169
75014550 170/**
c8337c47
HF
171 * ap_qci_available(): Test if AP configuration
172 * information can be queried via QCI subfunction.
75014550 173 *
c8337c47 174 * Returns 1 if subfunction PQAP(QCI) is available.
75014550 175 */
c8337c47 176static int ap_qci_available(void)
75014550 177{
86cd741b 178 return test_facility(12);
75014550
HD
179}
180
e7fc5146
TK
181/**
182 * ap_apft_available(): Test if AP facilities test (APFT)
183 * facility is available.
184 *
185 * Returns 1 if APFT is is available.
186 */
187static int ap_apft_available(void)
188{
189 return test_facility(15);
190}
191
9a564108
HF
192/*
193 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
194 *
195 * Returns 1 if the QACT subfunction is available.
196 */
197static inline int ap_qact_available(void)
198{
c8337c47
HF
199 if (ap_qci_info)
200 return ap_qci_info->qact;
9a564108
HF
201 return 0;
202}
203
050349b5 204/*
c8337c47 205 * ap_fetch_qci_info(): Fetch cryptographic config info
050349b5
HF
206 *
207 * Returns the ap configuration info fetched via PQAP(QCI).
208 * On success 0 is returned, on failure a negative errno
209 * is returned, e.g. if the PQAP(QCI) instruction is not
210 * available, the return value will be -EOPNOTSUPP.
211 */
c8337c47 212static inline int ap_fetch_qci_info(struct ap_config_info *info)
11d37673 213{
c8337c47 214 if (!ap_qci_available())
11d37673 215 return -EOPNOTSUPP;
050349b5
HF
216 if (!info)
217 return -EINVAL;
218 return ap_qci(info);
11d37673
HC
219}
220
889875a1 221/**
c8337c47
HF
222 * ap_init_qci_info(): Allocate and query qci config info.
223 * Does also update the static variables ap_max_domain_id
224 * and ap_max_adapter_id if this info is available.
225
889875a1 226 */
c8337c47 227static void __init ap_init_qci_info(void)
889875a1 228{
c8337c47 229 if (!ap_qci_available()) {
2ea2a609 230 AP_DBF_INFO("%s QCI not supported\n", __func__);
889875a1 231 return;
c8337c47 232 }
889875a1 233
c8337c47
HF
234 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
235 if (!ap_qci_info)
889875a1 236 return;
c8337c47
HF
237 if (ap_fetch_qci_info(ap_qci_info) != 0) {
238 kfree(ap_qci_info);
239 ap_qci_info = NULL;
889875a1
MS
240 return;
241 }
2ea2a609 242 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
c8337c47
HF
243
244 if (ap_qci_info->apxa) {
245 if (ap_qci_info->Na) {
246 ap_max_adapter_id = ap_qci_info->Na;
2ea2a609
HF
247 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
248 __func__, ap_max_adapter_id);
c8337c47
HF
249 }
250 if (ap_qci_info->Nd) {
251 ap_max_domain_id = ap_qci_info->Nd;
2ea2a609
HF
252 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
253 __func__, ap_max_domain_id);
c8337c47
HF
254 }
255 }
889875a1
MS
256}
257
258/*
259 * ap_test_config(): helper function to extract the nrth bit
260 * within the unsigned int array field.
261 */
262static inline int ap_test_config(unsigned int *field, unsigned int nr)
263{
264 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
265}
266
267/*
268 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
889875a1
MS
269 *
270 * Returns 0 if the card is not configured
271 * 1 if the card is configured or
272 * if the configuration information is not available
273 */
274static inline int ap_test_config_card_id(unsigned int id)
275{
c8337c47
HF
276 if (id > ap_max_adapter_id)
277 return 0;
278 if (ap_qci_info)
279 return ap_test_config(ap_qci_info->apm, id);
280 return 1;
889875a1
MS
281}
282
283/*
7379e652
HF
284 * ap_test_config_usage_domain(): Test, whether an AP usage domain
285 * is configured.
889875a1
MS
286 *
287 * Returns 0 if the usage domain is not configured
288 * 1 if the usage domain is configured or
289 * if the configuration information is not available
290 */
7379e652 291int ap_test_config_usage_domain(unsigned int domain)
889875a1 292{
c8337c47
HF
293 if (domain > ap_max_domain_id)
294 return 0;
295 if (ap_qci_info)
296 return ap_test_config(ap_qci_info->aqm, domain);
297 return 1;
889875a1 298}
7379e652
HF
299EXPORT_SYMBOL(ap_test_config_usage_domain);
300
301/*
302 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
303 * is configured.
304 * @domain AP control domain ID
305 *
306 * Returns 1 if the control domain is configured
307 * 0 in all other cases
308 */
309int ap_test_config_ctrl_domain(unsigned int domain)
310{
c8337c47 311 if (!ap_qci_info || domain > ap_max_domain_id)
7379e652 312 return 0;
c8337c47 313 return ap_test_config(ap_qci_info->adm, domain);
7379e652
HF
314}
315EXPORT_SYMBOL(ap_test_config_ctrl_domain);
889875a1 316
c8337c47
HF
317/*
318 * ap_queue_info(): Check and get AP queue info.
319 * Returns true if TAPQ succeeded and the info is filled or
320 * false otherwise.
1534c382 321 */
bd39654a
HF
322static bool ap_queue_info(ap_qid_t qid, int *q_type, unsigned int *q_fac,
323 int *q_depth, int *q_ml, bool *q_decfg)
1534c382
MS
324{
325 struct ap_queue_status status;
bd39654a
HF
326 union {
327 unsigned long value;
328 struct {
329 unsigned int fac : 32; /* facility bits */
330 unsigned int at : 8; /* ap type */
331 unsigned int _res1 : 8;
332 unsigned int _res2 : 4;
333 unsigned int ml : 4; /* apxl ml */
334 unsigned int _res3 : 4;
335 unsigned int qd : 4; /* queue depth */
336 } tapq_gr2;
337 } tapq_info;
338
339 tapq_info.value = 0;
889875a1 340
c8337c47
HF
341 /* make sure we don't run into a specifiation exception */
342 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
343 AP_QID_QUEUE(qid) > ap_max_domain_id)
344 return false;
1534c382 345
c8337c47 346 /* call TAPQ on this APQN */
bd39654a 347 status = ap_test_queue(qid, ap_apft_available(), &tapq_info.value);
c50a160c
IT
348 switch (status.response_code) {
349 case AP_RESPONSE_NORMAL:
c8337c47 350 case AP_RESPONSE_RESET_IN_PROGRESS:
4f2fcccd
HF
351 case AP_RESPONSE_DECONFIGURED:
352 case AP_RESPONSE_CHECKSTOPPED:
353 case AP_RESPONSE_BUSY:
c8337c47
HF
354 /*
355 * According to the architecture in all these cases the
356 * info should be filled. All bits 0 is not possible as
357 * there is at least one of the mode bits set.
358 */
bd39654a 359 if (WARN_ON_ONCE(!tapq_info.value))
c8337c47 360 return false;
bd39654a
HF
361 *q_type = tapq_info.tapq_gr2.at;
362 *q_fac = tapq_info.tapq_gr2.fac;
363 *q_depth = tapq_info.tapq_gr2.qd;
364 *q_ml = tapq_info.tapq_gr2.ml;
4f2fcccd 365 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
c8337c47 366 switch (*q_type) {
14878424 367 /* For CEX2 and CEX3 the available functions
a7b1868a 368 * are not reflected by the facilities bits.
14878424
HF
369 * Instead it is coded into the type. So here
370 * modify the function bits based on the type.
371 */
372 case AP_DEVICE_TYPE_CEX2A:
373 case AP_DEVICE_TYPE_CEX3A:
c8337c47 374 *q_fac |= 0x08000000;
14878424
HF
375 break;
376 case AP_DEVICE_TYPE_CEX2C:
377 case AP_DEVICE_TYPE_CEX3C:
c8337c47 378 *q_fac |= 0x10000000;
14878424
HF
379 break;
380 default:
381 break;
382 }
c8337c47 383 return true;
c50a160c 384 default:
c8337c47
HF
385 /*
386 * A response code which indicates, there is no info available.
387 */
388 return false;
1534c382 389 }
1534c382
MS
390}
391
dc4b6ded 392void ap_wait(enum ap_sm_wait wait)
3f3007af
MS
393{
394 ktime_t hr_time;
395
396 switch (wait) {
dc4b6ded
HF
397 case AP_SM_WAIT_AGAIN:
398 case AP_SM_WAIT_INTERRUPT:
3f3007af
MS
399 if (ap_using_interrupts())
400 break;
401 if (ap_poll_kthread) {
402 wake_up(&ap_poll_wait);
403 break;
404 }
fcf0220a 405 fallthrough;
dc4b6ded 406 case AP_SM_WAIT_TIMEOUT:
3f3007af
MS
407 spin_lock_bh(&ap_poll_timer_lock);
408 if (!hrtimer_is_queued(&ap_poll_timer)) {
8b0e1953 409 hr_time = poll_timeout;
3f3007af
MS
410 hrtimer_forward_now(&ap_poll_timer, hr_time);
411 hrtimer_restart(&ap_poll_timer);
412 }
413 spin_unlock_bh(&ap_poll_timer_lock);
414 break;
dc4b6ded 415 case AP_SM_WAIT_NONE:
3f3007af
MS
416 default:
417 break;
418 }
419}
420
3f3007af
MS
421/**
422 * ap_request_timeout(): Handling of request timeouts
cefbeb5d 423 * @t: timer making this callback
3f3007af
MS
424 *
425 * Handles request timeouts.
426 */
cefbeb5d 427void ap_request_timeout(struct timer_list *t)
3f3007af 428{
cefbeb5d 429 struct ap_queue *aq = from_timer(aq, t, timeout);
3f3007af 430
e28d2af4 431 spin_lock_bh(&aq->lock);
dc4b6ded 432 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
e28d2af4 433 spin_unlock_bh(&aq->lock);
3f3007af
MS
434}
435
436/**
437 * ap_poll_timeout(): AP receive polling for finished AP requests.
438 * @unused: Unused pointer.
439 *
440 * Schedules the AP tasklet using a high resolution timer.
441 */
442static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
443{
41677b1d 444 tasklet_schedule(&ap_tasklet);
3f3007af
MS
445 return HRTIMER_NORESTART;
446}
447
448/**
449 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
450 * @airq: pointer to adapter interrupt descriptor
451 */
30e63ef2 452static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
3f3007af
MS
453{
454 inc_irq_stat(IRQIO_APB);
41677b1d 455 tasklet_schedule(&ap_tasklet);
3f3007af
MS
456}
457
458/**
459 * ap_tasklet_fn(): Tasklet to poll all AP devices.
460 * @dummy: Unused variable
461 *
462 * Poll all AP devices on the bus.
463 */
464static void ap_tasklet_fn(unsigned long dummy)
465{
bc4b295e 466 int bkt;
e28d2af4 467 struct ap_queue *aq;
dc4b6ded 468 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
3f3007af
MS
469
470 /* Reset the indicator if interrupts are used. Thus new interrupts can
471 * be received. Doing it in the beginning of the tasklet is therefor
472 * important that no requests on any AP get lost.
473 */
474 if (ap_using_interrupts())
475 xchg(ap_airq.lsi_ptr, 0);
476
bc4b295e
HF
477 spin_lock_bh(&ap_queues_lock);
478 hash_for_each(ap_queues, bkt, aq, hnode) {
479 spin_lock_bh(&aq->lock);
dc4b6ded 480 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
bc4b295e 481 spin_unlock_bh(&aq->lock);
3f3007af 482 }
bc4b295e 483 spin_unlock_bh(&ap_queues_lock);
e28d2af4
IT
484
485 ap_wait(wait);
af512ed0
RW
486}
487
9af3e04e
MS
488static int ap_pending_requests(void)
489{
bc4b295e 490 int bkt;
e28d2af4
IT
491 struct ap_queue *aq;
492
bc4b295e
HF
493 spin_lock_bh(&ap_queues_lock);
494 hash_for_each(ap_queues, bkt, aq, hnode) {
495 if (aq->queue_count == 0)
496 continue;
497 spin_unlock_bh(&ap_queues_lock);
498 return 1;
9af3e04e 499 }
bc4b295e 500 spin_unlock_bh(&ap_queues_lock);
e28d2af4 501 return 0;
9af3e04e
MS
502}
503
83e9d5d2
MS
504/**
505 * ap_poll_thread(): Thread that polls for finished requests.
506 * @data: Unused pointer
507 *
508 * AP bus poll thread. The purpose of this thread is to poll for
509 * finished requests in a loop if there is a "free" cpu - that is
510 * a cpu that doesn't have anything better to do. The polling stops
511 * as soon as there is another task or if all messages have been
512 * delivered.
513 */
514static int ap_poll_thread(void *data)
515{
516 DECLARE_WAITQUEUE(wait, current);
83e9d5d2
MS
517
518 set_user_nice(current, MAX_NICE);
519 set_freezable();
520 while (!kthread_should_stop()) {
521 add_wait_queue(&ap_poll_wait, &wait);
522 set_current_state(TASK_INTERRUPTIBLE);
41677b1d 523 if (!ap_pending_requests()) {
83e9d5d2
MS
524 schedule();
525 try_to_freeze();
526 }
527 set_current_state(TASK_RUNNING);
528 remove_wait_queue(&ap_poll_wait, &wait);
83e9d5d2
MS
529 if (need_resched()) {
530 schedule();
531 try_to_freeze();
532 continue;
533 }
3f3007af 534 ap_tasklet_fn(0);
9af3e04e
MS
535 }
536
83e9d5d2
MS
537 return 0;
538}
539
540static int ap_poll_thread_start(void)
541{
542 int rc;
543
544 if (ap_using_interrupts() || ap_poll_kthread)
545 return 0;
546 mutex_lock(&ap_poll_thread_mutex);
547 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
9c705206 548 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
83e9d5d2
MS
549 if (rc)
550 ap_poll_kthread = NULL;
551 mutex_unlock(&ap_poll_thread_mutex);
552 return rc;
553}
554
555static void ap_poll_thread_stop(void)
556{
557 if (!ap_poll_kthread)
558 return;
559 mutex_lock(&ap_poll_thread_mutex);
560 kthread_stop(ap_poll_kthread);
561 ap_poll_kthread = NULL;
562 mutex_unlock(&ap_poll_thread_mutex);
563}
564
e28d2af4
IT
565#define is_card_dev(x) ((x)->parent == ap_root_device)
566#define is_queue_dev(x) ((x)->parent != ap_root_device)
1534c382
MS
567
568/**
1749a81d
FB
569 * ap_bus_match()
570 * @dev: Pointer to device
571 * @drv: Pointer to device_driver
572 *
1534c382
MS
573 * AP bus driver registration/unregistration.
574 */
575static int ap_bus_match(struct device *dev, struct device_driver *drv)
576{
1534c382
MS
577 struct ap_driver *ap_drv = to_ap_drv(drv);
578 struct ap_device_id *id;
579
1749a81d 580 /*
1534c382
MS
581 * Compare device type of the device with the list of
582 * supported types of the device_driver.
583 */
584 for (id = ap_drv->ids; id->match_flags; id++) {
e28d2af4
IT
585 if (is_card_dev(dev) &&
586 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
587 id->dev_type == to_ap_dev(dev)->device_type)
588 return 1;
589 if (is_queue_dev(dev) &&
590 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
591 id->dev_type == to_ap_dev(dev)->device_type)
592 return 1;
1534c382
MS
593 }
594 return 0;
595}
596
597/**
1749a81d
FB
598 * ap_uevent(): Uevent function for AP devices.
599 * @dev: Pointer to device
600 * @env: Pointer to kobj_uevent_env
601 *
602 * It sets up a single environment variable DEV_TYPE which contains the
603 * hardware device type.
1534c382 604 */
ac2b96f3 605static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
1534c382 606{
df6f508c 607 int rc = 0;
1534c382 608 struct ap_device *ap_dev = to_ap_dev(dev);
1534c382 609
837cd105
HF
610 /* Uevents from ap bus core don't need extensions to the env */
611 if (dev == ap_root_device)
612 return 0;
1534c382 613
df6f508c
HF
614 if (is_card_dev(dev)) {
615 struct ap_card *ac = to_ap_card(&ap_dev->device);
bf62456e 616
df6f508c
HF
617 /* Set up DEV_TYPE environment variable. */
618 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
619 if (rc)
620 return rc;
621 /* Add MODALIAS= */
622 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
623 if (rc)
624 return rc;
625
626 /* Add MODE=<accel|cca|ep11> */
627 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL))
628 rc = add_uevent_var(env, "MODE=accel");
629 else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
630 rc = add_uevent_var(env, "MODE=cca");
631 else if (ap_test_bit(&ac->functions, AP_FUNC_EP11))
632 rc = add_uevent_var(env, "MODE=ep11");
633 if (rc)
634 return rc;
635 } else {
636 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
637
638 /* Add MODE=<accel|cca|ep11> */
639 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL))
640 rc = add_uevent_var(env, "MODE=accel");
641 else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
642 rc = add_uevent_var(env, "MODE=cca");
643 else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11))
644 rc = add_uevent_var(env, "MODE=ep11");
645 if (rc)
646 return rc;
647 }
837cd105
HF
648
649 return 0;
650}
651
652static void ap_send_init_scan_done_uevent(void)
653{
654 char *envp[] = { "INITSCAN=done", NULL };
655
656 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
657}
658
659static void ap_send_bindings_complete_uevent(void)
660{
0677519a
HF
661 char buf[32];
662 char *envp[] = { "BINDINGS=complete", buf, NULL };
837cd105 663
0677519a
HF
664 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
665 atomic64_inc_return(&ap_bindings_complete_count));
837cd105
HF
666 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
667}
668
df6f508c
HF
669void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
670{
671 char buf[16];
672 char *envp[] = { buf, NULL };
673
674 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
675
676 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
677}
678EXPORT_SYMBOL(ap_send_config_uevent);
679
680void ap_send_online_uevent(struct ap_device *ap_dev, int online)
681{
682 char buf[16];
683 char *envp[] = { buf, NULL };
684
685 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
686
687 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
688}
689EXPORT_SYMBOL(ap_send_online_uevent);
690
837cd105
HF
691/*
692 * calc # of bound APQNs
693 */
694
695struct __ap_calc_ctrs {
696 unsigned int apqns;
697 unsigned int bound;
698};
699
700static int __ap_calc_helper(struct device *dev, void *arg)
701{
702 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *) arg;
703
704 if (is_queue_dev(dev)) {
705 pctrs->apqns++;
706 if ((to_ap_dev(dev))->drv)
707 pctrs->bound++;
708 }
709
710 return 0;
711}
712
713static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
714{
715 struct __ap_calc_ctrs ctrs;
716
717 memset(&ctrs, 0, sizeof(ctrs));
718 bus_for_each_dev(&ap_bus_type, NULL, (void *) &ctrs, __ap_calc_helper);
bf62456e 719
837cd105
HF
720 *apqns = ctrs.apqns;
721 *bound = ctrs.bound;
1534c382
MS
722}
723
837cd105
HF
724/*
725 * After initial ap bus scan do check if all existing APQNs are
726 * bound to device drivers.
727 */
728static void ap_check_bindings_complete(void)
729{
730 unsigned int apqns, bound;
731
732 if (atomic64_read(&ap_scan_bus_count) >= 1) {
733 ap_calc_bound_apqns(&apqns, &bound);
734 if (bound == apqns) {
735 if (!completion_done(&ap_init_apqn_bindings_complete)) {
736 complete_all(&ap_init_apqn_bindings_complete);
737 AP_DBF(DBF_INFO, "%s complete\n", __func__);
738 }
739 ap_send_bindings_complete_uevent();
740 }
741 }
742}
743
744/*
745 * Interface to wait for the AP bus to have done one initial ap bus
746 * scan and all detected APQNs have been bound to device drivers.
747 * If these both conditions are not fulfilled, this function blocks
748 * on a condition with wait_for_completion_interruptible_timeout().
749 * If these both conditions are fulfilled (before the timeout hits)
750 * the return value is 0. If the timeout (in jiffies) hits instead
751 * -ETIME is returned. On failures negative return values are
752 * returned to the caller.
753 */
754int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
755{
756 long l;
757
758 if (completion_done(&ap_init_apqn_bindings_complete))
759 return 0;
760
761 if (timeout)
762 l = wait_for_completion_interruptible_timeout(
763 &ap_init_apqn_bindings_complete, timeout);
764 else
765 l = wait_for_completion_interruptible(
766 &ap_init_apqn_bindings_complete);
767 if (l < 0)
768 return l == -ERESTARTSYS ? -EINTR : l;
769 else if (l == 0 && timeout)
770 return -ETIME;
771
772 return 0;
773}
774EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
775
e28d2af4 776static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
83e9d5d2 777{
e28d2af4
IT
778 if (is_queue_dev(dev) &&
779 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
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);
792 devres = test_bit_inv(card, ap_perms.apm)
793 && test_bit_inv(queue, ap_perms.aqm);
794 mutex_unlock(&ap_perms_mutex);
795 drvres = to_ap_drv(dev->driver)->flags
796 & AP_DRIVER_FLAG_DEFAULT;
797 if (!!devres != !!drvres) {
2ea2a609
HF
798 AP_DBF_DBG("reprobing queue=%02x.%04x\n",
799 card, queue);
7e0bdbe5
HF
800 rc = device_reprobe(dev);
801 }
802 }
803
804 return 0;
805}
806
807static void ap_bus_revise_bindings(void)
808{
809 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
810}
811
812int ap_owned_by_def_drv(int card, int queue)
813{
814 int rc = 0;
815
816 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
817 return -EINVAL;
818
819 mutex_lock(&ap_perms_mutex);
820
821 if (test_bit_inv(card, ap_perms.apm)
822 && test_bit_inv(queue, ap_perms.aqm))
823 rc = 1;
824
825 mutex_unlock(&ap_perms_mutex);
826
827 return rc;
828}
829EXPORT_SYMBOL(ap_owned_by_def_drv);
830
831int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
832 unsigned long *aqm)
833{
834 int card, queue, rc = 0;
835
836 mutex_lock(&ap_perms_mutex);
837
838 for (card = 0; !rc && card < AP_DEVICES; card++)
839 if (test_bit_inv(card, apm) &&
840 test_bit_inv(card, ap_perms.apm))
841 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
842 if (test_bit_inv(queue, aqm) &&
843 test_bit_inv(queue, ap_perms.aqm))
844 rc = 1;
845
846 mutex_unlock(&ap_perms_mutex);
847
848 return rc;
849}
850EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
851
1534c382
MS
852static int ap_device_probe(struct device *dev)
853{
854 struct ap_device *ap_dev = to_ap_dev(dev);
3f3007af 855 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
29c2680f
HF
856 int card, queue, devres, drvres, rc = -ENODEV;
857
858 if (!get_device(dev))
859 return rc;
7e0bdbe5
HF
860
861 if (is_queue_dev(dev)) {
862 /*
863 * If the apqn is marked as reserved/used by ap bus and
864 * default drivers, only probe with drivers with the default
865 * flag set. If it is not marked, only probe with drivers
866 * with the default flag not set.
867 */
868 card = AP_QID_CARD(to_ap_queue(dev)->qid);
869 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
870 mutex_lock(&ap_perms_mutex);
871 devres = test_bit_inv(card, ap_perms.apm)
872 && test_bit_inv(queue, ap_perms.aqm);
873 mutex_unlock(&ap_perms_mutex);
874 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
875 if (!!devres != !!drvres)
29c2680f 876 goto out;
7e0bdbe5 877 }
666e68e0 878
e3850508 879 /* Add queue/card to list of active queues/cards */
bc4b295e
HF
880 spin_lock_bh(&ap_queues_lock);
881 if (is_queue_dev(dev))
882 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
883 to_ap_queue(dev)->qid);
884 spin_unlock_bh(&ap_queues_lock);
e3850508 885
3f3007af 886 ap_dev->drv = ap_drv;
1534c382 887 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
e3850508
HF
888
889 if (rc) {
bc4b295e
HF
890 spin_lock_bh(&ap_queues_lock);
891 if (is_queue_dev(dev))
892 hash_del(&to_ap_queue(dev)->hnode);
893 spin_unlock_bh(&ap_queues_lock);
3f3007af 894 ap_dev->drv = NULL;
837cd105
HF
895 } else
896 ap_check_bindings_complete();
e3850508 897
29c2680f
HF
898out:
899 if (rc)
900 put_device(dev);
1534c382
MS
901 return rc;
902}
903
fc7a6209 904static void ap_device_remove(struct device *dev)
1534c382
MS
905{
906 struct ap_device *ap_dev = to_ap_dev(dev);
907 struct ap_driver *ap_drv = ap_dev->drv;
908
01396a37 909 /* prepare ap queue device removal */
104f708f 910 if (is_queue_dev(dev))
01396a37
HF
911 ap_queue_prepare_remove(to_ap_queue(dev));
912
913 /* driver's chance to clean up gracefully */
e3850508
HF
914 if (ap_drv->remove)
915 ap_drv->remove(ap_dev);
916
01396a37
HF
917 /* now do the ap queue device remove */
918 if (is_queue_dev(dev))
919 ap_queue_remove(to_ap_queue(dev));
920
e3850508 921 /* Remove queue/card from list of active queues/cards */
bc4b295e
HF
922 spin_lock_bh(&ap_queues_lock);
923 if (is_queue_dev(dev))
924 hash_del(&to_ap_queue(dev)->hnode);
925 spin_unlock_bh(&ap_queues_lock);
837cd105 926 ap_dev->drv = NULL;
e3850508 927
29c2680f 928 put_device(dev);
1534c382
MS
929}
930
bc4b295e
HF
931struct ap_queue *ap_get_qdev(ap_qid_t qid)
932{
933 int bkt;
934 struct ap_queue *aq;
935
936 spin_lock_bh(&ap_queues_lock);
937 hash_for_each(ap_queues, bkt, aq, hnode) {
938 if (aq->qid == qid) {
939 get_device(&aq->ap_dev.device);
940 spin_unlock_bh(&ap_queues_lock);
941 return aq;
942 }
943 }
944 spin_unlock_bh(&ap_queues_lock);
945
946 return NULL;
947}
948EXPORT_SYMBOL(ap_get_qdev);
949
1534c382
MS
950int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
951 char *name)
952{
953 struct device_driver *drv = &ap_drv->driver;
954
955 drv->bus = &ap_bus_type;
1534c382
MS
956 drv->owner = owner;
957 drv->name = name;
958 return driver_register(drv);
959}
960EXPORT_SYMBOL(ap_driver_register);
961
962void ap_driver_unregister(struct ap_driver *ap_drv)
963{
964 driver_unregister(&ap_drv->driver);
965}
966EXPORT_SYMBOL(ap_driver_unregister);
967
dabecb29
HD
968void ap_bus_force_rescan(void)
969{
56bbe686 970 /* processing a asynchronous bus rescan */
fcd0d1f6 971 del_timer(&ap_config_timer);
8139b89d
MS
972 queue_work(system_long_wq, &ap_scan_work);
973 flush_work(&ap_scan_work);
dabecb29
HD
974}
975EXPORT_SYMBOL(ap_bus_force_rescan);
976
0d9c038f
TK
977/*
978* A config change has happened, force an ap bus rescan.
979*/
980void ap_bus_cfg_chg(void)
981{
2ea2a609 982 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
0d9c038f
TK
983
984 ap_bus_force_rescan();
985}
986
1749a81d 987/*
3d8f60d3
HF
988 * hex2bitmap() - parse hex mask string and set bitmap.
989 * Valid strings are "0x012345678" with at least one valid hex number.
990 * Rest of the bitmap to the right is padded with 0. No spaces allowed
991 * within the string, the leading 0x may be omitted.
992 * Returns the bitmask with exactly the bits set as given by the hex
993 * string (both in big endian order).
1534c382 994 */
7e0bdbe5
HF
995static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
996{
997 int i, n, b;
998
999 /* bits needs to be a multiple of 8 */
1000 if (bits & 0x07)
1001 return -EINVAL;
1002
7e0bdbe5
HF
1003 if (str[0] == '0' && str[1] == 'x')
1004 str++;
1005 if (*str == 'x')
1006 str++;
1007
1008 for (i = 0; isxdigit(*str) && i < bits; str++) {
1009 b = hex_to_bin(*str);
1010 for (n = 0; n < 4; n++)
1011 if (b & (0x08 >> n))
1012 set_bit_inv(i + n, bitmap);
1013 i += 4;
1014 }
1015
3d8f60d3
HF
1016 if (*str == '\n')
1017 str++;
1018 if (*str)
1019 return -EINVAL;
1020 return 0;
1021}
1022
1023/*
fa108f95
MS
1024 * modify_bitmap() - parse bitmask argument and modify an existing
1025 * bit mask accordingly. A concatenation (done with ',') of these
1026 * terms is recognized:
3d8f60d3
HF
1027 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1028 * <bitnr> may be any valid number (hex, decimal or octal) in the range
1029 * 0...bits-1; the leading + or - is required. Here are some examples:
1030 * +0-15,+32,-128,-0xFF
1031 * -0-255,+1-16,+0x128
1032 * +1,+2,+3,+4,-5,-7-10
fa108f95
MS
1033 * Returns the new bitmap after all changes have been applied. Every
1034 * positive value in the string will set a bit and every negative value
1035 * in the string will clear a bit. As a bit may be touched more than once,
1036 * the last 'operation' wins:
1037 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1038 * cleared again. All other bits are unmodified.
3d8f60d3 1039 */
fa108f95 1040static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
3d8f60d3
HF
1041{
1042 int a, i, z;
1043 char *np, sign;
1044
1045 /* bits needs to be a multiple of 8 */
1046 if (bits & 0x07)
7e0bdbe5
HF
1047 return -EINVAL;
1048
3d8f60d3
HF
1049 while (*str) {
1050 sign = *str++;
1051 if (sign != '+' && sign != '-')
1052 return -EINVAL;
1053 a = z = simple_strtoul(str, &np, 0);
1054 if (str == np || a >= bits)
1055 return -EINVAL;
1056 str = np;
1057 if (*str == '-') {
1058 z = simple_strtoul(++str, &np, 0);
1059 if (str == np || a > z || z >= bits)
1060 return -EINVAL;
1061 str = np;
1062 }
1063 for (i = a; i <= z; i++)
fa108f95
MS
1064 if (sign == '+')
1065 set_bit_inv(i, bitmap);
1066 else
1067 clear_bit_inv(i, bitmap);
3d8f60d3
HF
1068 while (*str == ',' || *str == '\n')
1069 str++;
1070 }
1071
7e0bdbe5
HF
1072 return 0;
1073}
1074
00fab235
HF
1075int ap_parse_mask_str(const char *str,
1076 unsigned long *bitmap, int bits,
1077 struct mutex *lock)
3d8f60d3 1078{
fa108f95
MS
1079 unsigned long *newmap, size;
1080 int rc;
3d8f60d3
HF
1081
1082 /* bits needs to be a multiple of 8 */
1083 if (bits & 0x07)
1084 return -EINVAL;
1085
fa108f95
MS
1086 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
1087 newmap = kmalloc(size, GFP_KERNEL);
1088 if (!newmap)
1089 return -ENOMEM;
1090 if (mutex_lock_interruptible(lock)) {
1091 kfree(newmap);
1092 return -ERESTARTSYS;
1093 }
1094
3d8f60d3 1095 if (*str == '+' || *str == '-') {
fa108f95
MS
1096 memcpy(newmap, bitmap, size);
1097 rc = modify_bitmap(str, newmap, bits);
3d8f60d3 1098 } else {
fa108f95
MS
1099 memset(newmap, 0, size);
1100 rc = hex2bitmap(str, newmap, bits);
3d8f60d3 1101 }
fa108f95
MS
1102 if (rc == 0)
1103 memcpy(bitmap, newmap, size);
3d8f60d3 1104 mutex_unlock(lock);
fa108f95
MS
1105 kfree(newmap);
1106 return rc;
3d8f60d3 1107}
00fab235 1108EXPORT_SYMBOL(ap_parse_mask_str);
3d8f60d3
HF
1109
1110/*
1111 * AP bus attributes.
1112 */
1113
1534c382
MS
1114static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1115{
40501c70 1116 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1534c382
MS
1117}
1118
fc1d3f02
IT
1119static ssize_t ap_domain_store(struct bus_type *bus,
1120 const char *buf, size_t count)
1121{
1122 int domain;
1123
1124 if (sscanf(buf, "%i\n", &domain) != 1 ||
7e0bdbe5
HF
1125 domain < 0 || domain > ap_max_domain_id ||
1126 !test_bit_inv(domain, ap_perms.aqm))
fc1d3f02 1127 return -EINVAL;
c8337c47 1128
fc1d3f02
IT
1129 spin_lock_bh(&ap_domain_lock);
1130 ap_domain_index = domain;
1131 spin_unlock_bh(&ap_domain_lock);
cccd85bf 1132
2ea2a609 1133 AP_DBF_INFO("stored new default domain=%d\n", domain);
cccd85bf 1134
fc1d3f02
IT
1135 return count;
1136}
1137
ac2b96f3 1138static BUS_ATTR_RW(ap_domain);
1534c382 1139
91f3e3ea
IT
1140static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1141{
c8337c47 1142 if (!ap_qci_info) /* QCI not supported */
40501c70
HF
1143 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1144
1145 return scnprintf(buf, PAGE_SIZE,
1146 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
c8337c47
HF
1147 ap_qci_info->adm[0], ap_qci_info->adm[1],
1148 ap_qci_info->adm[2], ap_qci_info->adm[3],
1149 ap_qci_info->adm[4], ap_qci_info->adm[5],
1150 ap_qci_info->adm[6], ap_qci_info->adm[7]);
91f3e3ea
IT
1151}
1152
ac2b96f3 1153static BUS_ATTR_RO(ap_control_domain_mask);
91f3e3ea 1154
e28d2af4
IT
1155static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1156{
c8337c47 1157 if (!ap_qci_info) /* QCI not supported */
40501c70
HF
1158 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1159
1160 return scnprintf(buf, PAGE_SIZE,
1161 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
c8337c47
HF
1162 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1163 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1164 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1165 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
e28d2af4
IT
1166}
1167
ac2b96f3 1168static BUS_ATTR_RO(ap_usage_domain_mask);
1534c382 1169
aa55bf5f
HF
1170static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1171{
c8337c47 1172 if (!ap_qci_info) /* QCI not supported */
40501c70
HF
1173 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1174
1175 return scnprintf(buf, PAGE_SIZE,
1176 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
c8337c47
HF
1177 ap_qci_info->apm[0], ap_qci_info->apm[1],
1178 ap_qci_info->apm[2], ap_qci_info->apm[3],
1179 ap_qci_info->apm[4], ap_qci_info->apm[5],
1180 ap_qci_info->apm[6], ap_qci_info->apm[7]);
aa55bf5f
HF
1181}
1182
1183static BUS_ATTR_RO(ap_adapter_mask);
1184
cb17a636
FB
1185static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1186{
40501c70
HF
1187 return scnprintf(buf, PAGE_SIZE, "%d\n",
1188 ap_using_interrupts() ? 1 : 0);
cb17a636
FB
1189}
1190
ac2b96f3
HF
1191static BUS_ATTR_RO(ap_interrupts);
1192
1193static ssize_t config_time_show(struct bus_type *bus, char *buf)
1194{
40501c70 1195 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
ac2b96f3 1196}
cb17a636 1197
ac2b96f3
HF
1198static ssize_t config_time_store(struct bus_type *bus,
1199 const char *buf, size_t count)
1534c382
MS
1200{
1201 int time;
1202
1203 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1204 return -EINVAL;
1205 ap_config_time = time;
fcd0d1f6 1206 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1534c382
MS
1207 return count;
1208}
1209
ac2b96f3 1210static BUS_ATTR_RW(config_time);
1534c382 1211
ac2b96f3 1212static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1534c382 1213{
40501c70 1214 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1534c382
MS
1215}
1216
ac2b96f3
HF
1217static ssize_t poll_thread_store(struct bus_type *bus,
1218 const char *buf, size_t count)
1534c382
MS
1219{
1220 int flag, rc;
1221
1222 if (sscanf(buf, "%d\n", &flag) != 1)
1223 return -EINVAL;
1224 if (flag) {
1225 rc = ap_poll_thread_start();
1226 if (rc)
83e9d5d2
MS
1227 count = rc;
1228 } else
1534c382
MS
1229 ap_poll_thread_stop();
1230 return count;
1231}
1232
ac2b96f3 1233static BUS_ATTR_RW(poll_thread);
1534c382 1234
fe137230
FB
1235static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1236{
40501c70 1237 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
fe137230
FB
1238}
1239
1240static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1241 size_t count)
1242{
1243 unsigned long long time;
1244 ktime_t hr_time;
1245
1246 /* 120 seconds = maximum poll interval */
cb17a636
FB
1247 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1248 time > 120000000000ULL)
fe137230
FB
1249 return -EINVAL;
1250 poll_timeout = time;
8b0e1953 1251 hr_time = poll_timeout;
fe137230 1252
8cc2af7c
IT
1253 spin_lock_bh(&ap_poll_timer_lock);
1254 hrtimer_cancel(&ap_poll_timer);
1255 hrtimer_set_expires(&ap_poll_timer, hr_time);
1256 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1257 spin_unlock_bh(&ap_poll_timer_lock);
1258
fe137230
FB
1259 return count;
1260}
1261
ac2b96f3 1262static BUS_ATTR_RW(poll_timeout);
fe137230 1263
5bc334bf
IT
1264static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1265{
c8337c47 1266 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
5bc334bf
IT
1267}
1268
ac2b96f3 1269static BUS_ATTR_RO(ap_max_domain_id);
5bc334bf 1270
c8337c47
HF
1271static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1272{
1273 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1274}
1275
1276static BUS_ATTR_RO(ap_max_adapter_id);
1277
7e0bdbe5
HF
1278static ssize_t apmask_show(struct bus_type *bus, char *buf)
1279{
1280 int rc;
1281
1282 if (mutex_lock_interruptible(&ap_perms_mutex))
1283 return -ERESTARTSYS;
40501c70
HF
1284 rc = scnprintf(buf, PAGE_SIZE,
1285 "0x%016lx%016lx%016lx%016lx\n",
1286 ap_perms.apm[0], ap_perms.apm[1],
1287 ap_perms.apm[2], ap_perms.apm[3]);
7e0bdbe5
HF
1288 mutex_unlock(&ap_perms_mutex);
1289
1290 return rc;
1291}
1292
1293static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1294 size_t count)
1295{
3d8f60d3 1296 int rc;
7e0bdbe5 1297
00fab235 1298 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
3d8f60d3
HF
1299 if (rc)
1300 return rc;
7e0bdbe5
HF
1301
1302 ap_bus_revise_bindings();
1303
1304 return count;
1305}
1306
1307static BUS_ATTR_RW(apmask);
1308
1309static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1310{
1311 int rc;
1312
1313 if (mutex_lock_interruptible(&ap_perms_mutex))
1314 return -ERESTARTSYS;
40501c70
HF
1315 rc = scnprintf(buf, PAGE_SIZE,
1316 "0x%016lx%016lx%016lx%016lx\n",
1317 ap_perms.aqm[0], ap_perms.aqm[1],
1318 ap_perms.aqm[2], ap_perms.aqm[3]);
7e0bdbe5
HF
1319 mutex_unlock(&ap_perms_mutex);
1320
1321 return rc;
1322}
1323
1324static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1325 size_t count)
1326{
3d8f60d3 1327 int rc;
7e0bdbe5 1328
00fab235 1329 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
3d8f60d3
HF
1330 if (rc)
1331 return rc;
7e0bdbe5
HF
1332
1333 ap_bus_revise_bindings();
1334
1335 return count;
1336}
1337
1338static BUS_ATTR_RW(aqmask);
1339
837cd105
HF
1340static ssize_t scans_show(struct bus_type *bus, char *buf)
1341{
1342 return scnprintf(buf, PAGE_SIZE, "%llu\n",
1343 atomic64_read(&ap_scan_bus_count));
1344}
1345
1346static BUS_ATTR_RO(scans);
1347
1348static ssize_t bindings_show(struct bus_type *bus, char *buf)
1349{
1350 int rc;
1351 unsigned int apqns, n;
1352
1353 ap_calc_bound_apqns(&apqns, &n);
1354 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1355 rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns);
1356 else
1357 rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns);
1358
1359 return rc;
1360}
1361
1362static BUS_ATTR_RO(bindings);
1363
8cb4c20f
JW
1364static struct attribute *ap_bus_attrs[] = {
1365 &bus_attr_ap_domain.attr,
1366 &bus_attr_ap_control_domain_mask.attr,
1367 &bus_attr_ap_usage_domain_mask.attr,
1368 &bus_attr_ap_adapter_mask.attr,
1369 &bus_attr_config_time.attr,
1370 &bus_attr_poll_thread.attr,
1371 &bus_attr_ap_interrupts.attr,
1372 &bus_attr_poll_timeout.attr,
1373 &bus_attr_ap_max_domain_id.attr,
1374 &bus_attr_ap_max_adapter_id.attr,
1375 &bus_attr_apmask.attr,
1376 &bus_attr_aqmask.attr,
1377 &bus_attr_scans.attr,
1378 &bus_attr_bindings.attr,
fe137230 1379 NULL,
1534c382 1380};
8cb4c20f
JW
1381ATTRIBUTE_GROUPS(ap_bus);
1382
1383static struct bus_type ap_bus_type = {
1384 .name = "ap",
1385 .bus_groups = ap_bus_groups,
1386 .match = &ap_bus_match,
1387 .uevent = &ap_uevent,
95c09f03
JW
1388 .probe = ap_device_probe,
1389 .remove = ap_device_remove,
8cb4c20f 1390};
1534c382
MS
1391
1392/**
1c472d46
HP
1393 * ap_select_domain(): Select an AP domain if possible and we haven't
1394 * already done so before.
1534c382 1395 */
1c472d46 1396static void ap_select_domain(void)
1534c382 1397{
6acbe21f 1398 struct ap_queue_status status;
c8337c47 1399 int card, dom;
1534c382 1400
1749a81d 1401 /*
c8337c47
HF
1402 * Choose the default domain. Either the one specified with
1403 * the "domain=" parameter or the first domain with at least
1404 * one valid APQN.
1534c382 1405 */
fc1d3f02
IT
1406 spin_lock_bh(&ap_domain_lock);
1407 if (ap_domain_index >= 0) {
1534c382 1408 /* Domain has already been selected. */
c8337c47 1409 goto out;
fc1d3f02 1410 }
c8337c47
HF
1411 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1412 if (!ap_test_config_usage_domain(dom) ||
1413 !test_bit_inv(dom, ap_perms.aqm))
75014550 1414 continue;
c8337c47
HF
1415 for (card = 0; card <= ap_max_adapter_id; card++) {
1416 if (!ap_test_config_card_id(card) ||
1417 !test_bit_inv(card, ap_perms.apm))
75014550 1418 continue;
c8337c47 1419 status = ap_test_queue(AP_MKQID(card, dom),
e7fc5146
TK
1420 ap_apft_available(),
1421 NULL);
c8337c47
HF
1422 if (status.response_code == AP_RESPONSE_NORMAL)
1423 break;
1534c382 1424 }
c8337c47
HF
1425 if (card <= ap_max_adapter_id)
1426 break;
1534c382 1427 }
c8337c47
HF
1428 if (dom <= ap_max_domain_id) {
1429 ap_domain_index = dom;
2ea2a609
HF
1430 AP_DBF_INFO("%s new default domain is %d\n",
1431 __func__, ap_domain_index);
1534c382 1432 }
c8337c47 1433out:
fc1d3f02 1434 spin_unlock_bh(&ap_domain_lock);
1534c382
MS
1435}
1436
9a564108
HF
1437/*
1438 * This function checks the type and returns either 0 for not
1439 * supported or the highest compatible type value (which may
1440 * include the input type value).
1441 */
1442static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1443{
1444 int comp_type = 0;
1445
1446 /* < CEX2A is not supported */
2ea2a609
HF
1447 if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1448 AP_DBF_WARN("get_comp_type queue=%02x.%04x unsupported type %d\n",
1449 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
9a564108 1450 return 0;
2ea2a609 1451 }
cf2957f3
HF
1452 /* up to CEX7 known and fully supported */
1453 if (rawtype <= AP_DEVICE_TYPE_CEX7)
9a564108
HF
1454 return rawtype;
1455 /*
cf2957f3 1456 * unknown new type > CEX7, check for compatibility
9a564108 1457 * to the highest known and supported type which is
cf2957f3 1458 * currently CEX7 with the help of the QACT function.
9a564108
HF
1459 */
1460 if (ap_qact_available()) {
1461 struct ap_queue_status status;
56c5c683 1462 union ap_qact_ap_info apinfo = {0};
9a564108
HF
1463
1464 apinfo.mode = (func >> 26) & 0x07;
cf2957f3 1465 apinfo.cat = AP_DEVICE_TYPE_CEX7;
9a564108
HF
1466 status = ap_qact(qid, 0, &apinfo);
1467 if (status.response_code == AP_RESPONSE_NORMAL
1468 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
cf2957f3 1469 && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
9a564108
HF
1470 comp_type = apinfo.cat;
1471 }
1472 if (!comp_type)
2ea2a609
HF
1473 AP_DBF_WARN("get_comp_type queue=%02x.%04x unable to map type %d\n",
1474 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
9a564108 1475 else if (comp_type != rawtype)
2ea2a609
HF
1476 AP_DBF_INFO("get_comp_type queue=%02x.%04x map type %d to %d\n",
1477 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1478 rawtype, comp_type);
9a564108
HF
1479 return comp_type;
1480}
1481
e28d2af4 1482/*
a7b1868a 1483 * Helper function to be used with bus_find_dev
e28d2af4 1484 * matches for the card device with the given id
1534c382 1485 */
418e3ea1 1486static int __match_card_device_with_id(struct device *dev, const void *data)
1534c382 1487{
418e3ea1 1488 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1534c382
MS
1489}
1490
a7b1868a
HF
1491/*
1492 * Helper function to be used with bus_find_dev
e28d2af4
IT
1493 * matches for the queue device with a given qid
1494 */
418e3ea1 1495static int __match_queue_device_with_qid(struct device *dev, const void *data)
e28d2af4
IT
1496{
1497 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1498}
1499
b1af7528
HF
1500/*
1501 * Helper function to be used with bus_find_dev
1502 * matches any queue device with given queue id
1503 */
418e3ea1 1504static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
b1af7528
HF
1505{
1506 return is_queue_dev(dev)
1507 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1508}
1509
a7b1868a
HF
1510/*
1511 * Helper function for ap_scan_bus().
4f2fcccd
HF
1512 * Remove card device and associated queue devices.
1513 */
1514static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1515{
1516 bus_for_each_dev(&ap_bus_type, NULL,
1517 (void *)(long) ac->id,
1518 __ap_queue_devices_with_id_unregister);
1519 device_unregister(&ac->ap_dev.device);
1520}
1521
1522/*
1523 * Helper function for ap_scan_bus().
1524 * Does the scan bus job for all the domains within
1525 * a valid adapter given by an ap_card ptr.
e28d2af4 1526 */
4f2fcccd 1527static inline void ap_scan_domains(struct ap_card *ac)
1534c382 1528{
4f2fcccd 1529 bool decfg;
a7b1868a
HF
1530 ap_qid_t qid;
1531 unsigned int func;
1534c382 1532 struct device *dev;
a7b1868a 1533 struct ap_queue *aq;
bd39654a 1534 int rc, dom, depth, type, ml;
4f2fcccd
HF
1535
1536 /*
1537 * Go through the configuration for the domains and compare them
1538 * to the existing queue devices. Also take care of the config
1539 * and error state for the queue devices.
1540 */
1541
1542 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1543 qid = AP_MKQID(ac->id, dom);
1544 dev = bus_find_device(&ap_bus_type, NULL,
1545 (void *)(long) qid,
1546 __match_queue_device_with_qid);
1547 aq = dev ? to_ap_queue(dev) : NULL;
1548 if (!ap_test_config_usage_domain(dom)) {
1549 if (dev) {
1550 AP_DBF_INFO("%s(%d,%d) not in config any more, rm queue device\n",
1551 __func__, ac->id, dom);
1552 device_unregister(dev);
1553 put_device(dev);
1554 }
1555 continue;
1556 }
1557 /* domain is valid, get info from this APQN */
bd39654a 1558 if (!ap_queue_info(qid, &type, &func, &depth, &ml, &decfg)) {
4f2fcccd
HF
1559 if (aq) {
1560 AP_DBF_INFO(
1561 "%s(%d,%d) ap_queue_info() not successful, rm queue device\n",
1562 __func__, ac->id, dom);
1563 device_unregister(dev);
1564 put_device(dev);
1565 }
1566 continue;
1567 }
1568 /* if no queue device exists, create a new one */
1569 if (!aq) {
1570 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1571 if (!aq) {
1572 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1573 __func__, ac->id, dom);
1574 continue;
1575 }
1576 aq->card = ac;
1577 aq->config = !decfg;
1578 dev = &aq->ap_dev.device;
1579 dev->bus = &ap_bus_type;
1580 dev->parent = &ac->ap_dev.device;
1581 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1582 /* register queue device */
1583 rc = device_register(dev);
1584 if (rc) {
1585 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1586 __func__, ac->id, dom);
1587 goto put_dev_and_continue;
1588 }
29c2680f
HF
1589 /* get it and thus adjust reference counter */
1590 get_device(dev);
4f2fcccd
HF
1591 if (decfg)
1592 AP_DBF_INFO("%s(%d,%d) new (decfg) queue device created\n",
1593 __func__, ac->id, dom);
1594 else
1595 AP_DBF_INFO("%s(%d,%d) new queue device created\n",
1596 __func__, ac->id, dom);
1597 goto put_dev_and_continue;
1598 }
1599 /* Check config state on the already existing queue device */
1600 spin_lock_bh(&aq->lock);
1601 if (decfg && aq->config) {
1602 /* config off this queue device */
1603 aq->config = false;
1604 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1605 aq->dev_state = AP_DEV_STATE_ERROR;
1606 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1607 }
1608 spin_unlock_bh(&aq->lock);
1609 AP_DBF_INFO("%s(%d,%d) queue device config off\n",
1610 __func__, ac->id, dom);
df6f508c 1611 ap_send_config_uevent(&aq->ap_dev, aq->config);
4f2fcccd
HF
1612 /* 'receive' pending messages with -EAGAIN */
1613 ap_flush_queue(aq);
1614 goto put_dev_and_continue;
1615 }
1616 if (!decfg && !aq->config) {
1617 /* config on this queue device */
1618 aq->config = true;
1619 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1620 aq->dev_state = AP_DEV_STATE_OPERATING;
1621 aq->sm_state = AP_SM_STATE_RESET_START;
1622 }
1623 spin_unlock_bh(&aq->lock);
1624 AP_DBF_INFO("%s(%d,%d) queue device config on\n",
1625 __func__, ac->id, dom);
df6f508c 1626 ap_send_config_uevent(&aq->ap_dev, aq->config);
4f2fcccd
HF
1627 goto put_dev_and_continue;
1628 }
1629 /* handle other error states */
1630 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1631 spin_unlock_bh(&aq->lock);
1632 /* 'receive' pending messages with -EAGAIN */
1633 ap_flush_queue(aq);
1634 /* re-init (with reset) the queue device */
1635 ap_queue_init_state(aq);
1636 AP_DBF_INFO("%s(%d,%d) queue device reinit enforced\n",
1637 __func__, ac->id, dom);
1638 goto put_dev_and_continue;
1639 }
1640 spin_unlock_bh(&aq->lock);
1641put_dev_and_continue:
1642 put_device(dev);
1643 }
1644}
1645
1646/*
1647 * Helper function for ap_scan_bus().
1648 * Does the scan bus job for the given adapter id.
1649 */
1650static inline void ap_scan_adapter(int ap)
1651{
1652 bool decfg;
1653 ap_qid_t qid;
1654 unsigned int func;
1655 struct device *dev;
1656 struct ap_card *ac;
bd39654a 1657 int rc, dom, depth, type, comp_type, ml;
a7b1868a 1658
4f2fcccd 1659 /* Is there currently a card device for this adapter ? */
a7b1868a 1660 dev = bus_find_device(&ap_bus_type, NULL,
4f2fcccd 1661 (void *)(long) ap,
a7b1868a
HF
1662 __match_card_device_with_id);
1663 ac = dev ? to_ap_card(dev) : NULL;
4f2fcccd
HF
1664
1665 /* Adapter not in configuration ? */
1666 if (!ap_test_config_card_id(ap)) {
1667 if (ac) {
1668 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devices\n",
1669 __func__, ap);
1670 ap_scan_rm_card_dev_and_queue_devs(ac);
a7b1868a
HF
1671 put_device(dev);
1672 }
1673 return;
1674 }
cccd85bf 1675
a7b1868a 1676 /*
4f2fcccd
HF
1677 * Adapter ap is valid in the current configuration. So do some checks:
1678 * If no card device exists, build one. If a card device exists, check
1679 * for type and functions changed. For all this we need to find a valid
1680 * APQN first.
a7b1868a 1681 */
4f2fcccd
HF
1682
1683 for (dom = 0; dom <= ap_max_domain_id; dom++)
1684 if (ap_test_config_usage_domain(dom)) {
1685 qid = AP_MKQID(ap, dom);
bd39654a
HF
1686 if (ap_queue_info(qid, &type, &func,
1687 &depth, &ml, &decfg))
a7b1868a
HF
1688 break;
1689 }
4f2fcccd
HF
1690 if (dom > ap_max_domain_id) {
1691 /* Could not find a valid APQN for this adapter */
1692 if (ac) {
1693 AP_DBF_INFO(
1694 "%s(%d) no type info (no APQN found), rm card and queue devices\n",
1695 __func__, ap);
1696 ap_scan_rm_card_dev_and_queue_devs(ac);
1697 put_device(dev);
1698 } else {
1699 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1700 __func__, ap);
a7b1868a 1701 }
4f2fcccd
HF
1702 return;
1703 }
1704 if (!type) {
1705 /* No apdater type info available, an unusable adapter */
1706 if (ac) {
1707 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devices\n",
1708 __func__, ap);
1709 ap_scan_rm_card_dev_and_queue_devs(ac);
a7b1868a 1710 put_device(dev);
4f2fcccd
HF
1711 } else {
1712 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1713 __func__, ap);
a7b1868a 1714 }
4f2fcccd 1715 return;
a7b1868a 1716 }
889875a1 1717
4f2fcccd
HF
1718 if (ac) {
1719 /* Check APQN against existing card device for changes */
1720 if (ac->raw_hwtype != type) {
1721 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devices\n",
1722 __func__, ap, type);
1723 ap_scan_rm_card_dev_and_queue_devs(ac);
1724 put_device(dev);
1725 ac = NULL;
1726 } else if (ac->functions != func) {
1727 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devices\n",
1728 __func__, ap, type);
1729 ap_scan_rm_card_dev_and_queue_devs(ac);
1730 put_device(dev);
1731 ac = NULL;
1732 } else {
1733 if (decfg && ac->config) {
1734 ac->config = false;
1735 AP_DBF_INFO("%s(%d) card device config off\n",
1736 __func__, ap);
df6f508c 1737 ap_send_config_uevent(&ac->ap_dev, ac->config);
e28d2af4 1738 }
4f2fcccd
HF
1739 if (!decfg && !ac->config) {
1740 ac->config = true;
1741 AP_DBF_INFO("%s(%d) card device config on\n",
1742 __func__, ap);
df6f508c 1743 ap_send_config_uevent(&ac->ap_dev, ac->config);
b1af7528 1744 }
a7b1868a 1745 }
4f2fcccd
HF
1746 }
1747
1748 if (!ac) {
1749 /* Build a new card device */
a7b1868a 1750 comp_type = ap_get_compatible_type(qid, type, func);
4f2fcccd
HF
1751 if (!comp_type) {
1752 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
1753 __func__, ap, type);
1754 return;
1755 }
bd39654a 1756 ac = ap_card_create(ap, depth, type, comp_type, func, ml);
a7b1868a 1757 if (!ac) {
4f2fcccd
HF
1758 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
1759 __func__, ap);
1760 return;
a7b1868a 1761 }
4f2fcccd
HF
1762 ac->config = !decfg;
1763 dev = &ac->ap_dev.device;
1764 dev->bus = &ap_bus_type;
1765 dev->parent = ap_root_device;
1766 dev_set_name(dev, "card%02x", ap);
bd39654a
HF
1767 /* maybe enlarge ap_max_msg_size to support this card */
1768 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
1769 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
1770 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
1771 __func__, ap, atomic_read(&ap_max_msg_size));
1772 }
4f2fcccd
HF
1773 /* Register the new card device with AP bus */
1774 rc = device_register(dev);
a7b1868a 1775 if (rc) {
4f2fcccd
HF
1776 AP_DBF_WARN("%s(%d) device_register() failed\n",
1777 __func__, ap);
1778 put_device(dev);
1779 return;
3f3007af 1780 }
4f2fcccd
HF
1781 /* get it and thus adjust reference counter */
1782 get_device(dev);
1783 if (decfg)
1784 AP_DBF_INFO("%s(%d) new (decfg) card device type=%d func=0x%08x created\n",
1785 __func__, ap, type, func);
1786 else
1787 AP_DBF_INFO("%s(%d) new card device type=%d func=0x%08x created\n",
1788 __func__, ap, type, func);
1789 }
1790
1791 /* Verify the domains and the queue devices for this card */
1792 ap_scan_domains(ac);
a7b1868a 1793
4f2fcccd
HF
1794 /* release the card device */
1795 put_device(&ac->ap_dev.device);
a7b1868a 1796}
ac994e80 1797
a7b1868a
HF
1798/**
1799 * ap_scan_bus(): Scan the AP bus for new devices
1800 * Runs periodically, workqueue timer (ap_config_time)
1801 */
1802static void ap_scan_bus(struct work_struct *unused)
1803{
4f2fcccd 1804 int ap;
a7b1868a 1805
c8337c47 1806 ap_fetch_qci_info(ap_qci_info);
a7b1868a
HF
1807 ap_select_domain();
1808
2ea2a609 1809 AP_DBF_DBG("%s running\n", __func__);
c8337c47 1810
a7b1868a 1811 /* loop over all possible adapters */
4f2fcccd
HF
1812 for (ap = 0; ap <= ap_max_adapter_id; ap++)
1813 ap_scan_adapter(ap);
a7b1868a
HF
1814
1815 /* check if there is at least one queue available with default domain */
1816 if (ap_domain_index >= 0) {
1817 struct device *dev =
1818 bus_find_device(&ap_bus_type, NULL,
1819 (void *)(long) ap_domain_index,
b1af7528 1820 __match_queue_device_with_queue_id);
a7b1868a
HF
1821 if (dev)
1822 put_device(dev);
1823 else
2ea2a609
HF
1824 AP_DBF_INFO("no queue device with default domain %d available\n",
1825 ap_domain_index);
a7b1868a 1826 }
ac994e80 1827
837cd105
HF
1828 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
1829 AP_DBF(DBF_DEBUG, "%s init scan complete\n", __func__);
1830 ap_send_init_scan_done_uevent();
1831 ap_check_bindings_complete();
1832 }
1833
fcd0d1f6 1834 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1534c382
MS
1835}
1836
cefbeb5d 1837static void ap_config_timeout(struct timer_list *unused)
1534c382 1838{
8139b89d 1839 queue_work(system_long_wq, &ap_scan_work);
1534c382 1840}
13e742ba 1841
efda7ade 1842static int __init ap_debug_init(void)
cccd85bf 1843{
cccd85bf
HF
1844 ap_dbf_info = debug_register("ap", 1, 1,
1845 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1846 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1847 debug_set_level(ap_dbf_info, DBF_ERR);
1848
1849 return 0;
1850}
1851
7e0bdbe5
HF
1852static void __init ap_perms_init(void)
1853{
3d8f60d3 1854 /* all resources useable if no kernel parameter string given */
00fab235 1855 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
7e0bdbe5
HF
1856 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1857 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1858
3d8f60d3 1859 /* apm kernel parameter string */
7e0bdbe5 1860 if (apm_str) {
3d8f60d3 1861 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
00fab235
HF
1862 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1863 &ap_perms_mutex);
7e0bdbe5 1864 }
7e0bdbe5 1865
3d8f60d3
HF
1866 /* aqm kernel parameter string */
1867 if (aqm_str) {
1868 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
00fab235
HF
1869 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1870 &ap_perms_mutex);
7e0bdbe5
HF
1871 }
1872}
1873
1534c382 1874/**
1749a81d
FB
1875 * ap_module_init(): The module initialization code.
1876 *
1877 * Initializes the module.
1534c382 1878 */
efda7ade 1879static int __init ap_module_init(void)
1534c382 1880{
8cb4c20f 1881 int rc;
1534c382 1882
cccd85bf
HF
1883 rc = ap_debug_init();
1884 if (rc)
1885 return rc;
1886
2395103b 1887 if (!ap_instructions_available()) {
889875a1
MS
1888 pr_warn("The hardware system does not support AP instructions\n");
1889 return -ENODEV;
1890 }
1891
bc4b295e
HF
1892 /* init ap_queue hashtable */
1893 hash_init(ap_queues);
1894
00fab235 1895 /* set up the AP permissions (ioctls, ap and aq masks) */
7e0bdbe5
HF
1896 ap_perms_init();
1897
889875a1 1898 /* Get AP configuration data if available */
c8337c47
HF
1899 ap_init_qci_info();
1900
1901 /* check default domain setting */
1902 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
7e0bdbe5
HF
1903 (ap_domain_index >= 0 &&
1904 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
889875a1
MS
1905 pr_warn("%d is not a valid cryptographic domain\n",
1906 ap_domain_index);
ac994e80 1907 ap_domain_index = -1;
1534c382 1908 }
5314af69 1909
c8337c47 1910 /* enable interrupts if available */
cb17a636 1911 if (ap_interrupts_available()) {
f4eae94f
MS
1912 rc = register_adapter_interrupt(&ap_airq);
1913 ap_airq_flag = (rc == 0);
cb17a636
FB
1914 }
1915
1534c382
MS
1916 /* Create /sys/bus/ap. */
1917 rc = bus_register(&ap_bus_type);
1918 if (rc)
1919 goto out;
1534c382
MS
1920
1921 /* Create /sys/devices/ap. */
035da16f 1922 ap_root_device = root_device_register("ap");
9c705206 1923 rc = PTR_ERR_OR_ZERO(ap_root_device);
1534c382
MS
1924 if (rc)
1925 goto out_bus;
837cd105 1926 ap_root_device->bus = &ap_bus_type;
1534c382 1927
1749a81d 1928 /* Setup the AP bus rescan timer. */
cefbeb5d 1929 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1534c382 1930
3f3007af
MS
1931 /*
1932 * Setup the high resultion poll timer.
fe137230
FB
1933 * If we are running under z/VM adjust polling to z/VM polling rate.
1934 */
1935 if (MACHINE_IS_VM)
1936 poll_timeout = 1500000;
1937 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1938 ap_poll_timer.function = ap_poll_timeout;
1939
1534c382
MS
1940 /* Start the low priority AP bus poll thread. */
1941 if (ap_thread_flag) {
1942 rc = ap_poll_thread_start();
1943 if (rc)
1944 goto out_work;
1945 }
1946
8139b89d 1947 queue_work(system_long_wq, &ap_scan_work);
3f3007af 1948
1534c382
MS
1949 return 0;
1950
1951out_work:
fe137230 1952 hrtimer_cancel(&ap_poll_timer);
035da16f 1953 root_device_unregister(ap_root_device);
1534c382 1954out_bus:
1534c382
MS
1955 bus_unregister(&ap_bus_type);
1956out:
f4eae94f
MS
1957 if (ap_using_interrupts())
1958 unregister_adapter_interrupt(&ap_airq);
c8337c47 1959 kfree(ap_qci_info);
1534c382
MS
1960 return rc;
1961}
50a0d46c 1962device_initcall(ap_module_init);