]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/crypto/ap_bus.c
s390/zcrypt: enable AP bus scan without a valid default domain
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / crypto / ap_bus.c
CommitLineData
812141a9 1// SPDX-License-Identifier: GPL-2.0+
1534c382 2/*
75014550 3 * Copyright IBM Corp. 2006, 2012
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>
1534c382
MS
9 *
10 * Adjunct processor bus.
1534c382
MS
11 */
12
136f7a1c
MS
13#define KMSG_COMPONENT "ap"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
62d146ff 16#include <linux/kernel_stat.h>
50a0d46c 17#include <linux/moduleparam.h>
1534c382
MS
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/interrupt.h>
22#include <linux/workqueue.h>
5a0e3ad6 23#include <linux/slab.h>
1534c382
MS
24#include <linux/notifier.h>
25#include <linux/kthread.h>
26#include <linux/mutex.h>
83e9d5d2 27#include <linux/suspend.h>
85eca850 28#include <asm/reset.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>
3cb86ad0 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);
cf65c71d 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
cf65c71d
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
3cb86ad0
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
e28d2af4
IT
64static struct device *ap_root_device;
65
66DEFINE_SPINLOCK(ap_list_lock);
67LIST_HEAD(ap_card_list);
68
3cb86ad0
HF
69/* Default permissions (card and domain masking) */
70static struct ap_perms {
71 DECLARE_BITMAP(apm, AP_DEVICES);
72 DECLARE_BITMAP(aqm, AP_DOMAINS);
73} ap_perms;
74static DEFINE_MUTEX(ap_perms_mutex);
75
75014550 76static struct ap_config_info *ap_configuration;
e387753c 77static bool initialised;
1534c382 78
cccd85bf
HF
79/*
80 * AP bus related debug feature things.
81 */
cccd85bf
HF
82debug_info_t *ap_dbf_info;
83
1749a81d 84/*
8139b89d 85 * Workqueue timer for bus rescan.
1534c382 86 */
1534c382
MS
87static struct timer_list ap_config_timer;
88static int ap_config_time = AP_CONFIG_TIME;
3f3007af 89static void ap_scan_bus(struct work_struct *);
8139b89d 90static DECLARE_WORK(ap_scan_work, ap_scan_bus);
1534c382 91
1749a81d 92/*
cb17a636 93 * Tasklet & timer for AP request polling and interrupts
1534c382 94 */
3f3007af
MS
95static void ap_tasklet_fn(unsigned long);
96static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
1534c382 97static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
cf65c71d 98static struct task_struct *ap_poll_kthread;
1534c382 99static DEFINE_MUTEX(ap_poll_thread_mutex);
93521314 100static DEFINE_SPINLOCK(ap_poll_timer_lock);
fe137230 101static struct hrtimer ap_poll_timer;
cf65c71d
HF
102/*
103 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
104 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
105 */
fe137230 106static unsigned long long poll_timeout = 250000;
1534c382 107
772f5472
FB
108/* Suspend flag */
109static int ap_suspend_flag;
889875a1
MS
110/* Maximum domain id */
111static int ap_max_domain_id;
cf65c71d
HF
112/*
113 * Flag to check if domain was set through module parameter domain=. This is
5314af69 114 * important when supsend and resume is done in a z/VM environment where the
cf65c71d
HF
115 * domain might change.
116 */
117static int user_set_domain;
772f5472
FB
118static struct bus_type ap_bus_type;
119
f4eae94f 120/* Adapter interrupt definitions */
3f3007af
MS
121static void ap_interrupt_handler(struct airq_struct *airq);
122
f4eae94f
MS
123static int ap_airq_flag;
124
125static struct airq_struct ap_airq = {
126 .handler = ap_interrupt_handler,
127 .isc = AP_ISC,
128};
129
cb17a636
FB
130/**
131 * ap_using_interrupts() - Returns non-zero if interrupt support is
132 * available.
133 */
134static inline int ap_using_interrupts(void)
135{
f4eae94f 136 return ap_airq_flag;
cb17a636
FB
137}
138
e28d2af4
IT
139/**
140 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
141 *
142 * Returns the address of the local-summary-indicator of the adapter
143 * interrupt handler for AP, or NULL if adapter interrupts are not
144 * available.
145 */
146void *ap_airq_ptr(void)
147{
148 if (ap_using_interrupts())
149 return ap_airq.lsi_ptr;
150 return NULL;
151}
152
cb17a636
FB
153/**
154 * ap_interrupts_available(): Test if AP interrupts are available.
155 *
156 * Returns 1 if AP interrupts are available.
157 */
158static int ap_interrupts_available(void)
159{
86cd741b 160 return test_facility(65);
cb17a636
FB
161}
162
75014550
HD
163/**
164 * ap_configuration_available(): Test if AP configuration
165 * information is available.
166 *
167 * Returns 1 if AP configuration information is available.
168 */
169static int ap_configuration_available(void)
170{
86cd741b 171 return test_facility(12);
75014550
HD
172}
173
e7fc5146
TK
174/**
175 * ap_apft_available(): Test if AP facilities test (APFT)
176 * facility is available.
177 *
178 * Returns 1 if APFT is is available.
179 */
180static int ap_apft_available(void)
181{
182 return test_facility(15);
183}
184
9a564108
HF
185/*
186 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
187 *
188 * Returns 1 if the QACT subfunction is available.
189 */
190static inline int ap_qact_available(void)
191{
192 if (ap_configuration)
193 return ap_configuration->qact;
194 return 0;
195}
196
050349b5
HF
197/*
198 * ap_query_configuration(): Fetch cryptographic config info
199 *
200 * Returns the ap configuration info fetched via PQAP(QCI).
201 * On success 0 is returned, on failure a negative errno
202 * is returned, e.g. if the PQAP(QCI) instruction is not
203 * available, the return value will be -EOPNOTSUPP.
204 */
d62a6fae 205static inline int ap_query_configuration(struct ap_config_info *info)
11d37673 206{
050349b5 207 if (!ap_configuration_available())
11d37673 208 return -EOPNOTSUPP;
050349b5
HF
209 if (!info)
210 return -EINVAL;
211 return ap_qci(info);
11d37673 212}
050349b5 213EXPORT_SYMBOL(ap_query_configuration);
11d37673 214
889875a1
MS
215/**
216 * ap_init_configuration(): Allocate and query configuration array.
217 */
218static void ap_init_configuration(void)
219{
220 if (!ap_configuration_available())
221 return;
222
223 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
224 if (!ap_configuration)
225 return;
050349b5 226 if (ap_query_configuration(ap_configuration) != 0) {
889875a1
MS
227 kfree(ap_configuration);
228 ap_configuration = NULL;
229 return;
230 }
231}
232
233/*
234 * ap_test_config(): helper function to extract the nrth bit
235 * within the unsigned int array field.
236 */
237static inline int ap_test_config(unsigned int *field, unsigned int nr)
238{
239 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
240}
241
242/*
243 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
244 * @id AP card ID
245 *
246 * Returns 0 if the card is not configured
247 * 1 if the card is configured or
248 * if the configuration information is not available
249 */
250static inline int ap_test_config_card_id(unsigned int id)
251{
252 if (!ap_configuration) /* QCI not supported */
8a557e93
HF
253 /* only ids 0...3F may be probed */
254 return id < 0x40 ? 1 : 0;
889875a1
MS
255 return ap_test_config(ap_configuration->apm, id);
256}
257
258/*
c83450ad
HF
259 * ap_test_config_usage_domain(): Test, whether an AP usage domain
260 * is configured.
889875a1
MS
261 * @domain AP usage domain ID
262 *
263 * Returns 0 if the usage domain is not configured
264 * 1 if the usage domain is configured or
265 * if the configuration information is not available
266 */
c83450ad 267int ap_test_config_usage_domain(unsigned int domain)
889875a1
MS
268{
269 if (!ap_configuration) /* QCI not supported */
270 return domain < 16;
271 return ap_test_config(ap_configuration->aqm, domain);
272}
c83450ad
HF
273EXPORT_SYMBOL(ap_test_config_usage_domain);
274
275/*
276 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
277 * is configured.
278 * @domain AP control domain ID
279 *
280 * Returns 1 if the control domain is configured
281 * 0 in all other cases
282 */
283int ap_test_config_ctrl_domain(unsigned int domain)
284{
285 if (!ap_configuration) /* QCI not supported */
286 return 0;
287 return ap_test_config(ap_configuration->adm, domain);
288}
289EXPORT_SYMBOL(ap_test_config_ctrl_domain);
889875a1 290
1534c382 291/**
1749a81d
FB
292 * ap_query_queue(): Check if an AP queue is available.
293 * @qid: The AP queue number
294 * @queue_depth: Pointer to queue depth value
295 * @device_type: Pointer to device type value
6acbe21f 296 * @facilities: Pointer to facility indicator
1534c382 297 */
6acbe21f
MS
298static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
299 unsigned int *facilities)
1534c382
MS
300{
301 struct ap_queue_status status;
6acbe21f 302 unsigned long info;
889875a1
MS
303 int nd;
304
e28d2af4 305 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
889875a1 306 return -ENODEV;
1534c382 307
e7fc5146 308 status = ap_test_queue(qid, ap_apft_available(), &info);
c50a160c
IT
309 switch (status.response_code) {
310 case AP_RESPONSE_NORMAL:
6acbe21f
MS
311 *queue_depth = (int)(info & 0xff);
312 *device_type = (int)((info >> 24) & 0xff);
313 *facilities = (unsigned int)(info >> 32);
889875a1
MS
314 /* Update maximum domain id */
315 nd = (info >> 16) & 0xff;
c1c1368d 316 /* if N bit is available, z13 and newer */
889875a1
MS
317 if ((info & (1UL << 57)) && nd > 0)
318 ap_max_domain_id = nd;
c1c1368d
IT
319 else /* older machine types */
320 ap_max_domain_id = 15;
14878424
HF
321 switch (*device_type) {
322 /* For CEX2 and CEX3 the available functions
323 * are not refrected by the facilities bits.
324 * Instead it is coded into the type. So here
325 * modify the function bits based on the type.
326 */
327 case AP_DEVICE_TYPE_CEX2A:
328 case AP_DEVICE_TYPE_CEX3A:
329 *facilities |= 0x08000000;
330 break;
331 case AP_DEVICE_TYPE_CEX2C:
332 case AP_DEVICE_TYPE_CEX3C:
333 *facilities |= 0x10000000;
334 break;
335 default:
336 break;
337 }
c50a160c
IT
338 return 0;
339 case AP_RESPONSE_Q_NOT_AVAIL:
340 case AP_RESPONSE_DECONFIGURED:
341 case AP_RESPONSE_CHECKSTOPPED:
342 case AP_RESPONSE_INVALID_ADDRESS:
343 return -ENODEV;
344 case AP_RESPONSE_RESET_IN_PROGRESS:
345 case AP_RESPONSE_OTHERWISE_CHANGED:
346 case AP_RESPONSE_BUSY:
347 return -EBUSY;
348 default:
349 BUG();
1534c382 350 }
1534c382
MS
351}
352
e28d2af4 353void ap_wait(enum ap_wait wait)
3f3007af
MS
354{
355 ktime_t hr_time;
356
357 switch (wait) {
358 case AP_WAIT_AGAIN:
359 case AP_WAIT_INTERRUPT:
360 if (ap_using_interrupts())
361 break;
362 if (ap_poll_kthread) {
363 wake_up(&ap_poll_wait);
364 break;
365 }
366 /* Fall through */
367 case AP_WAIT_TIMEOUT:
368 spin_lock_bh(&ap_poll_timer_lock);
369 if (!hrtimer_is_queued(&ap_poll_timer)) {
8b0e1953 370 hr_time = poll_timeout;
3f3007af
MS
371 hrtimer_forward_now(&ap_poll_timer, hr_time);
372 hrtimer_restart(&ap_poll_timer);
373 }
374 spin_unlock_bh(&ap_poll_timer_lock);
375 break;
376 case AP_WAIT_NONE:
377 default:
378 break;
379 }
380}
381
3f3007af
MS
382/**
383 * ap_request_timeout(): Handling of request timeouts
cefbeb5d 384 * @t: timer making this callback
3f3007af
MS
385 *
386 * Handles request timeouts.
387 */
cefbeb5d 388void ap_request_timeout(struct timer_list *t)
3f3007af 389{
cefbeb5d 390 struct ap_queue *aq = from_timer(aq, t, timeout);
3f3007af
MS
391
392 if (ap_suspend_flag)
393 return;
e28d2af4
IT
394 spin_lock_bh(&aq->lock);
395 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
396 spin_unlock_bh(&aq->lock);
3f3007af
MS
397}
398
399/**
400 * ap_poll_timeout(): AP receive polling for finished AP requests.
401 * @unused: Unused pointer.
402 *
403 * Schedules the AP tasklet using a high resolution timer.
404 */
405static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
406{
407 if (!ap_suspend_flag)
408 tasklet_schedule(&ap_tasklet);
409 return HRTIMER_NORESTART;
410}
411
412/**
413 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
414 * @airq: pointer to adapter interrupt descriptor
415 */
416static void ap_interrupt_handler(struct airq_struct *airq)
417{
418 inc_irq_stat(IRQIO_APB);
419 if (!ap_suspend_flag)
420 tasklet_schedule(&ap_tasklet);
421}
422
423/**
424 * ap_tasklet_fn(): Tasklet to poll all AP devices.
425 * @dummy: Unused variable
426 *
427 * Poll all AP devices on the bus.
428 */
429static void ap_tasklet_fn(unsigned long dummy)
430{
e28d2af4
IT
431 struct ap_card *ac;
432 struct ap_queue *aq;
3f3007af
MS
433 enum ap_wait wait = AP_WAIT_NONE;
434
435 /* Reset the indicator if interrupts are used. Thus new interrupts can
436 * be received. Doing it in the beginning of the tasklet is therefor
437 * important that no requests on any AP get lost.
438 */
439 if (ap_using_interrupts())
440 xchg(ap_airq.lsi_ptr, 0);
441
e28d2af4
IT
442 spin_lock_bh(&ap_list_lock);
443 for_each_ap_card(ac) {
444 for_each_ap_queue(aq, ac) {
445 spin_lock_bh(&aq->lock);
446 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
447 spin_unlock_bh(&aq->lock);
448 }
3f3007af 449 }
e28d2af4
IT
450 spin_unlock_bh(&ap_list_lock);
451
452 ap_wait(wait);
af512ed0
RW
453}
454
9af3e04e
MS
455static int ap_pending_requests(void)
456{
e28d2af4
IT
457 struct ap_card *ac;
458 struct ap_queue *aq;
459
460 spin_lock_bh(&ap_list_lock);
461 for_each_ap_card(ac) {
462 for_each_ap_queue(aq, ac) {
463 if (aq->queue_count == 0)
464 continue;
465 spin_unlock_bh(&ap_list_lock);
466 return 1;
9af3e04e 467 }
9af3e04e 468 }
e28d2af4
IT
469 spin_unlock_bh(&ap_list_lock);
470 return 0;
9af3e04e
MS
471}
472
83e9d5d2
MS
473/**
474 * ap_poll_thread(): Thread that polls for finished requests.
475 * @data: Unused pointer
476 *
477 * AP bus poll thread. The purpose of this thread is to poll for
478 * finished requests in a loop if there is a "free" cpu - that is
479 * a cpu that doesn't have anything better to do. The polling stops
480 * as soon as there is another task or if all messages have been
481 * delivered.
482 */
483static int ap_poll_thread(void *data)
484{
485 DECLARE_WAITQUEUE(wait, current);
83e9d5d2
MS
486
487 set_user_nice(current, MAX_NICE);
488 set_freezable();
489 while (!kthread_should_stop()) {
490 add_wait_queue(&ap_poll_wait, &wait);
491 set_current_state(TASK_INTERRUPTIBLE);
9af3e04e 492 if (ap_suspend_flag || !ap_pending_requests()) {
83e9d5d2
MS
493 schedule();
494 try_to_freeze();
495 }
496 set_current_state(TASK_RUNNING);
497 remove_wait_queue(&ap_poll_wait, &wait);
83e9d5d2
MS
498 if (need_resched()) {
499 schedule();
500 try_to_freeze();
501 continue;
502 }
3f3007af 503 ap_tasklet_fn(0);
9af3e04e
MS
504 }
505
83e9d5d2
MS
506 return 0;
507}
508
509static int ap_poll_thread_start(void)
510{
511 int rc;
512
513 if (ap_using_interrupts() || ap_poll_kthread)
514 return 0;
515 mutex_lock(&ap_poll_thread_mutex);
516 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
517 rc = PTR_RET(ap_poll_kthread);
518 if (rc)
519 ap_poll_kthread = NULL;
520 mutex_unlock(&ap_poll_thread_mutex);
521 return rc;
522}
523
524static void ap_poll_thread_stop(void)
525{
526 if (!ap_poll_kthread)
527 return;
528 mutex_lock(&ap_poll_thread_mutex);
529 kthread_stop(ap_poll_kthread);
530 ap_poll_kthread = NULL;
531 mutex_unlock(&ap_poll_thread_mutex);
532}
533
e28d2af4
IT
534#define is_card_dev(x) ((x)->parent == ap_root_device)
535#define is_queue_dev(x) ((x)->parent != ap_root_device)
1534c382
MS
536
537/**
1749a81d
FB
538 * ap_bus_match()
539 * @dev: Pointer to device
540 * @drv: Pointer to device_driver
541 *
1534c382
MS
542 * AP bus driver registration/unregistration.
543 */
544static int ap_bus_match(struct device *dev, struct device_driver *drv)
545{
1534c382
MS
546 struct ap_driver *ap_drv = to_ap_drv(drv);
547 struct ap_device_id *id;
548
1749a81d 549 /*
1534c382
MS
550 * Compare device type of the device with the list of
551 * supported types of the device_driver.
552 */
553 for (id = ap_drv->ids; id->match_flags; id++) {
e28d2af4
IT
554 if (is_card_dev(dev) &&
555 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
556 id->dev_type == to_ap_dev(dev)->device_type)
557 return 1;
558 if (is_queue_dev(dev) &&
559 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
560 id->dev_type == to_ap_dev(dev)->device_type)
561 return 1;
1534c382
MS
562 }
563 return 0;
564}
565
566/**
1749a81d
FB
567 * ap_uevent(): Uevent function for AP devices.
568 * @dev: Pointer to device
569 * @env: Pointer to kobj_uevent_env
570 *
571 * It sets up a single environment variable DEV_TYPE which contains the
572 * hardware device type.
1534c382 573 */
cf65c71d 574static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
1534c382
MS
575{
576 struct ap_device *ap_dev = to_ap_dev(dev);
7eff2e7a 577 int retval = 0;
1534c382
MS
578
579 if (!ap_dev)
580 return -ENODEV;
581
582 /* Set up DEV_TYPE environment variable. */
7eff2e7a 583 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
bf62456e
ER
584 if (retval)
585 return retval;
586
66a4263b 587 /* Add MODALIAS= */
7eff2e7a 588 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
bf62456e 589
bf62456e 590 return retval;
1534c382
MS
591}
592
3e488c95 593static int ap_dev_suspend(struct device *dev)
772f5472
FB
594{
595 struct ap_device *ap_dev = to_ap_dev(dev);
772f5472 596
e28d2af4
IT
597 if (ap_dev->drv && ap_dev->drv->suspend)
598 ap_dev->drv->suspend(ap_dev);
599 return 0;
600}
601
602static int ap_dev_resume(struct device *dev)
603{
604 struct ap_device *ap_dev = to_ap_dev(dev);
605
606 if (ap_dev->drv && ap_dev->drv->resume)
607 ap_dev->drv->resume(ap_dev);
83e9d5d2
MS
608 return 0;
609}
5314af69 610
83e9d5d2
MS
611static void ap_bus_suspend(void)
612{
cf65c71d 613 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
cccd85bf 614
83e9d5d2
MS
615 ap_suspend_flag = 1;
616 /*
617 * Disable scanning for devices, thus we do not want to scan
618 * for them after removing.
619 */
8139b89d 620 flush_work(&ap_scan_work);
83e9d5d2
MS
621 tasklet_disable(&ap_tasklet);
622}
623
e28d2af4
IT
624static int __ap_card_devices_unregister(struct device *dev, void *dummy)
625{
626 if (is_card_dev(dev))
627 device_unregister(dev);
628 return 0;
629}
630
631static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
632{
633 if (is_queue_dev(dev))
634 device_unregister(dev);
635 return 0;
636}
637
638static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
83e9d5d2 639{
e28d2af4
IT
640 if (is_queue_dev(dev) &&
641 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
642 device_unregister(dev);
83e9d5d2
MS
643 return 0;
644}
645
646static void ap_bus_resume(void)
772f5472 647{
f4eae94f 648 int rc;
772f5472 649
cf65c71d 650 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
cccd85bf 651
e28d2af4
IT
652 /* remove all queue devices */
653 bus_for_each_dev(&ap_bus_type, NULL, NULL,
654 __ap_queue_devices_unregister);
655 /* remove all card devices */
656 bus_for_each_dev(&ap_bus_type, NULL, NULL,
657 __ap_card_devices_unregister);
658
83e9d5d2
MS
659 /* Reset thin interrupt setting */
660 if (ap_interrupts_available() && !ap_using_interrupts()) {
661 rc = register_adapter_interrupt(&ap_airq);
662 ap_airq_flag = (rc == 0);
5314af69 663 }
83e9d5d2
MS
664 if (!ap_interrupts_available() && ap_using_interrupts()) {
665 unregister_adapter_interrupt(&ap_airq);
666 ap_airq_flag = 0;
667 }
668 /* Reset domain */
669 if (!user_set_domain)
670 ap_domain_index = -1;
671 /* Get things going again */
672 ap_suspend_flag = 0;
673 if (ap_airq_flag)
674 xchg(ap_airq.lsi_ptr, 0);
675 tasklet_enable(&ap_tasklet);
8139b89d 676 queue_work(system_long_wq, &ap_scan_work);
83e9d5d2 677}
772f5472 678
83e9d5d2
MS
679static int ap_power_event(struct notifier_block *this, unsigned long event,
680 void *ptr)
681{
682 switch (event) {
683 case PM_HIBERNATION_PREPARE:
684 case PM_SUSPEND_PREPARE:
685 ap_bus_suspend();
686 break;
687 case PM_POST_HIBERNATION:
688 case PM_POST_SUSPEND:
689 ap_bus_resume();
690 break;
691 default:
692 break;
693 }
694 return NOTIFY_DONE;
772f5472 695}
83e9d5d2
MS
696static struct notifier_block ap_power_notifier = {
697 .notifier_call = ap_power_event,
698};
772f5472 699
e28d2af4 700static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
3e488c95 701
1534c382
MS
702static struct bus_type ap_bus_type = {
703 .name = "ap",
704 .match = &ap_bus_match,
705 .uevent = &ap_uevent,
3e488c95 706 .pm = &ap_bus_pm_ops,
1534c382
MS
707};
708
3cb86ad0
HF
709static int __ap_revise_reserved(struct device *dev, void *dummy)
710{
711 int rc, card, queue, devres, drvres;
712
713 if (is_queue_dev(dev)) {
714 card = AP_QID_CARD(to_ap_queue(dev)->qid);
715 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
716 mutex_lock(&ap_perms_mutex);
717 devres = test_bit_inv(card, ap_perms.apm)
718 && test_bit_inv(queue, ap_perms.aqm);
719 mutex_unlock(&ap_perms_mutex);
720 drvres = to_ap_drv(dev->driver)->flags
721 & AP_DRIVER_FLAG_DEFAULT;
722 if (!!devres != !!drvres) {
723 AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
724 card, queue);
725 rc = device_reprobe(dev);
726 }
727 }
728
729 return 0;
730}
731
732static void ap_bus_revise_bindings(void)
733{
734 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
735}
736
737int ap_owned_by_def_drv(int card, int queue)
738{
739 int rc = 0;
740
741 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
742 return -EINVAL;
743
744 mutex_lock(&ap_perms_mutex);
745
746 if (test_bit_inv(card, ap_perms.apm)
747 && test_bit_inv(queue, ap_perms.aqm))
748 rc = 1;
749
750 mutex_unlock(&ap_perms_mutex);
751
752 return rc;
753}
754EXPORT_SYMBOL(ap_owned_by_def_drv);
755
756int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
757 unsigned long *aqm)
758{
759 int card, queue, rc = 0;
760
761 mutex_lock(&ap_perms_mutex);
762
763 for (card = 0; !rc && card < AP_DEVICES; card++)
764 if (test_bit_inv(card, apm) &&
765 test_bit_inv(card, ap_perms.apm))
766 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
767 if (test_bit_inv(queue, aqm) &&
768 test_bit_inv(queue, ap_perms.aqm))
769 rc = 1;
770
771 mutex_unlock(&ap_perms_mutex);
772
773 return rc;
774}
775EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
776
1534c382
MS
777static int ap_device_probe(struct device *dev)
778{
779 struct ap_device *ap_dev = to_ap_dev(dev);
3f3007af 780 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
3cb86ad0
HF
781 int card, queue, devres, drvres, rc;
782
783 if (is_queue_dev(dev)) {
784 /*
785 * If the apqn is marked as reserved/used by ap bus and
786 * default drivers, only probe with drivers with the default
787 * flag set. If it is not marked, only probe with drivers
788 * with the default flag not set.
789 */
790 card = AP_QID_CARD(to_ap_queue(dev)->qid);
791 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
792 mutex_lock(&ap_perms_mutex);
793 devres = test_bit_inv(card, ap_perms.apm)
794 && test_bit_inv(queue, ap_perms.aqm);
795 mutex_unlock(&ap_perms_mutex);
796 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
797 if (!!devres != !!drvres)
798 return -ENODEV;
de43b416
HF
799 /* (re-)init queue's state machine */
800 ap_queue_reinit_state(to_ap_queue(dev));
3cb86ad0 801 }
666e68e0 802
e3850508
HF
803 /* Add queue/card to list of active queues/cards */
804 spin_lock_bh(&ap_list_lock);
805 if (is_card_dev(dev))
806 list_add(&to_ap_card(dev)->list, &ap_card_list);
807 else
808 list_add(&to_ap_queue(dev)->list,
809 &to_ap_queue(dev)->card->queues);
810 spin_unlock_bh(&ap_list_lock);
811
3f3007af 812 ap_dev->drv = ap_drv;
1534c382 813 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
e3850508
HF
814
815 if (rc) {
816 spin_lock_bh(&ap_list_lock);
817 if (is_card_dev(dev))
818 list_del_init(&to_ap_card(dev)->list);
819 else
820 list_del_init(&to_ap_queue(dev)->list);
821 spin_unlock_bh(&ap_list_lock);
3f3007af 822 ap_dev->drv = NULL;
e3850508
HF
823 }
824
1534c382
MS
825 return rc;
826}
827
1534c382
MS
828static int ap_device_remove(struct device *dev)
829{
830 struct ap_device *ap_dev = to_ap_dev(dev);
831 struct ap_driver *ap_drv = ap_dev->drv;
832
de43b416
HF
833 if (is_queue_dev(dev))
834 ap_queue_remove(to_ap_queue(dev));
e3850508
HF
835 if (ap_drv->remove)
836 ap_drv->remove(ap_dev);
837
838 /* Remove queue/card from list of active queues/cards */
e28d2af4
IT
839 spin_lock_bh(&ap_list_lock);
840 if (is_card_dev(dev))
841 list_del_init(&to_ap_card(dev)->list);
842 else
843 list_del_init(&to_ap_queue(dev)->list);
844 spin_unlock_bh(&ap_list_lock);
e3850508 845
1534c382
MS
846 return 0;
847}
848
849int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
850 char *name)
851{
852 struct device_driver *drv = &ap_drv->driver;
853
e387753c
SS
854 if (!initialised)
855 return -ENODEV;
856
1534c382
MS
857 drv->bus = &ap_bus_type;
858 drv->probe = ap_device_probe;
859 drv->remove = ap_device_remove;
860 drv->owner = owner;
861 drv->name = name;
862 return driver_register(drv);
863}
864EXPORT_SYMBOL(ap_driver_register);
865
866void ap_driver_unregister(struct ap_driver *ap_drv)
867{
868 driver_unregister(&ap_drv->driver);
869}
870EXPORT_SYMBOL(ap_driver_unregister);
871
dabecb29
HD
872void ap_bus_force_rescan(void)
873{
83e9d5d2
MS
874 if (ap_suspend_flag)
875 return;
56bbe686 876 /* processing a asynchronous bus rescan */
fcd0d1f6 877 del_timer(&ap_config_timer);
8139b89d
MS
878 queue_work(system_long_wq, &ap_scan_work);
879 flush_work(&ap_scan_work);
dabecb29
HD
880}
881EXPORT_SYMBOL(ap_bus_force_rescan);
882
1749a81d 883/*
f7ea641f
HF
884 * hex2bitmap() - parse hex mask string and set bitmap.
885 * Valid strings are "0x012345678" with at least one valid hex number.
886 * Rest of the bitmap to the right is padded with 0. No spaces allowed
887 * within the string, the leading 0x may be omitted.
888 * Returns the bitmask with exactly the bits set as given by the hex
889 * string (both in big endian order).
1534c382 890 */
3cb86ad0
HF
891static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
892{
893 int i, n, b;
894
895 /* bits needs to be a multiple of 8 */
896 if (bits & 0x07)
897 return -EINVAL;
898
3cb86ad0
HF
899 if (str[0] == '0' && str[1] == 'x')
900 str++;
901 if (*str == 'x')
902 str++;
903
904 for (i = 0; isxdigit(*str) && i < bits; str++) {
905 b = hex_to_bin(*str);
906 for (n = 0; n < 4; n++)
907 if (b & (0x08 >> n))
908 set_bit_inv(i + n, bitmap);
909 i += 4;
910 }
911
f7ea641f
HF
912 if (*str == '\n')
913 str++;
914 if (*str)
915 return -EINVAL;
916 return 0;
917}
918
919/*
071eda99
MS
920 * modify_bitmap() - parse bitmask argument and modify an existing
921 * bit mask accordingly. A concatenation (done with ',') of these
922 * terms is recognized:
f7ea641f
HF
923 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
924 * <bitnr> may be any valid number (hex, decimal or octal) in the range
925 * 0...bits-1; the leading + or - is required. Here are some examples:
926 * +0-15,+32,-128,-0xFF
927 * -0-255,+1-16,+0x128
928 * +1,+2,+3,+4,-5,-7-10
071eda99
MS
929 * Returns the new bitmap after all changes have been applied. Every
930 * positive value in the string will set a bit and every negative value
931 * in the string will clear a bit. As a bit may be touched more than once,
932 * the last 'operation' wins:
933 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
934 * cleared again. All other bits are unmodified.
f7ea641f 935 */
071eda99 936static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
f7ea641f
HF
937{
938 int a, i, z;
939 char *np, sign;
940
941 /* bits needs to be a multiple of 8 */
942 if (bits & 0x07)
3cb86ad0
HF
943 return -EINVAL;
944
f7ea641f
HF
945 while (*str) {
946 sign = *str++;
947 if (sign != '+' && sign != '-')
948 return -EINVAL;
949 a = z = simple_strtoul(str, &np, 0);
950 if (str == np || a >= bits)
951 return -EINVAL;
952 str = np;
953 if (*str == '-') {
954 z = simple_strtoul(++str, &np, 0);
955 if (str == np || a > z || z >= bits)
956 return -EINVAL;
957 str = np;
958 }
959 for (i = a; i <= z; i++)
071eda99
MS
960 if (sign == '+')
961 set_bit_inv(i, bitmap);
962 else
963 clear_bit_inv(i, bitmap);
f7ea641f
HF
964 while (*str == ',' || *str == '\n')
965 str++;
966 }
967
3cb86ad0
HF
968 return 0;
969}
970
f7ea641f
HF
971/*
972 * process_mask_arg() - parse a bitmap string and clear/set the
973 * bits in the bitmap accordingly. The string may be given as
974 * absolute value, a hex string like 0x1F2E3D4C5B6A" simple over-
975 * writing the current content of the bitmap. Or as relative string
976 * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
977 * bits are cleared or set. Distinction is done based on the very
978 * first character which may be '+' or '-' for the relative string
979 * and othewise assume to be an absolute value string. If parsing fails
980 * a negative errno value is returned. All arguments and bitmaps are
981 * big endian order.
982 */
983static int process_mask_arg(const char *str,
984 unsigned long *bitmap, int bits,
985 struct mutex *lock)
986{
071eda99
MS
987 unsigned long *newmap, size;
988 int rc;
f7ea641f
HF
989
990 /* bits needs to be a multiple of 8 */
991 if (bits & 0x07)
992 return -EINVAL;
993
071eda99
MS
994 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
995 newmap = kmalloc(size, GFP_KERNEL);
996 if (!newmap)
997 return -ENOMEM;
998 if (mutex_lock_interruptible(lock)) {
999 kfree(newmap);
1000 return -ERESTARTSYS;
1001 }
1002
f7ea641f 1003 if (*str == '+' || *str == '-') {
071eda99
MS
1004 memcpy(newmap, bitmap, size);
1005 rc = modify_bitmap(str, newmap, bits);
f7ea641f 1006 } else {
071eda99
MS
1007 memset(newmap, 0, size);
1008 rc = hex2bitmap(str, newmap, bits);
f7ea641f 1009 }
071eda99
MS
1010 if (rc == 0)
1011 memcpy(bitmap, newmap, size);
f7ea641f 1012 mutex_unlock(lock);
071eda99
MS
1013 kfree(newmap);
1014 return rc;
f7ea641f
HF
1015}
1016
1017/*
1018 * AP bus attributes.
1019 */
1020
1534c382
MS
1021static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1022{
1023 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1024}
1025
fc1d3f02
IT
1026static ssize_t ap_domain_store(struct bus_type *bus,
1027 const char *buf, size_t count)
1028{
1029 int domain;
1030
1031 if (sscanf(buf, "%i\n", &domain) != 1 ||
3cb86ad0
HF
1032 domain < 0 || domain > ap_max_domain_id ||
1033 !test_bit_inv(domain, ap_perms.aqm))
fc1d3f02
IT
1034 return -EINVAL;
1035 spin_lock_bh(&ap_domain_lock);
1036 ap_domain_index = domain;
1037 spin_unlock_bh(&ap_domain_lock);
cccd85bf 1038
ac994e80 1039 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
cccd85bf 1040
fc1d3f02
IT
1041 return count;
1042}
1043
cf65c71d 1044static BUS_ATTR_RW(ap_domain);
1534c382 1045
91f3e3ea
IT
1046static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1047{
889875a1
MS
1048 if (!ap_configuration) /* QCI not supported */
1049 return snprintf(buf, PAGE_SIZE, "not supported\n");
e28d2af4 1050
889875a1
MS
1051 return snprintf(buf, PAGE_SIZE,
1052 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
91f3e3ea
IT
1053 ap_configuration->adm[0], ap_configuration->adm[1],
1054 ap_configuration->adm[2], ap_configuration->adm[3],
1055 ap_configuration->adm[4], ap_configuration->adm[5],
1056 ap_configuration->adm[6], ap_configuration->adm[7]);
91f3e3ea
IT
1057}
1058
cf65c71d 1059static BUS_ATTR_RO(ap_control_domain_mask);
91f3e3ea 1060
e28d2af4
IT
1061static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1062{
1063 if (!ap_configuration) /* QCI not supported */
1064 return snprintf(buf, PAGE_SIZE, "not supported\n");
1065
1066 return snprintf(buf, PAGE_SIZE,
1067 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1068 ap_configuration->aqm[0], ap_configuration->aqm[1],
1069 ap_configuration->aqm[2], ap_configuration->aqm[3],
1070 ap_configuration->aqm[4], ap_configuration->aqm[5],
1071 ap_configuration->aqm[6], ap_configuration->aqm[7]);
1072}
1073
cf65c71d 1074static BUS_ATTR_RO(ap_usage_domain_mask);
1534c382 1075
cb17a636
FB
1076static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1077{
1078 return snprintf(buf, PAGE_SIZE, "%d\n",
1079 ap_using_interrupts() ? 1 : 0);
1080}
1081
cf65c71d
HF
1082static BUS_ATTR_RO(ap_interrupts);
1083
1084static ssize_t config_time_show(struct bus_type *bus, char *buf)
1085{
1086 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1087}
cb17a636 1088
cf65c71d
HF
1089static ssize_t config_time_store(struct bus_type *bus,
1090 const char *buf, size_t count)
1534c382
MS
1091{
1092 int time;
1093
1094 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1095 return -EINVAL;
1096 ap_config_time = time;
fcd0d1f6 1097 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1534c382
MS
1098 return count;
1099}
1100
cf65c71d 1101static BUS_ATTR_RW(config_time);
1534c382 1102
cf65c71d 1103static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1534c382
MS
1104{
1105 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1106}
1107
cf65c71d
HF
1108static ssize_t poll_thread_store(struct bus_type *bus,
1109 const char *buf, size_t count)
1534c382
MS
1110{
1111 int flag, rc;
1112
1113 if (sscanf(buf, "%d\n", &flag) != 1)
1114 return -EINVAL;
1115 if (flag) {
1116 rc = ap_poll_thread_start();
1117 if (rc)
83e9d5d2
MS
1118 count = rc;
1119 } else
1534c382
MS
1120 ap_poll_thread_stop();
1121 return count;
1122}
1123
cf65c71d 1124static BUS_ATTR_RW(poll_thread);
1534c382 1125
fe137230
FB
1126static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1127{
1128 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1129}
1130
1131static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1132 size_t count)
1133{
1134 unsigned long long time;
1135 ktime_t hr_time;
1136
1137 /* 120 seconds = maximum poll interval */
cb17a636
FB
1138 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1139 time > 120000000000ULL)
fe137230
FB
1140 return -EINVAL;
1141 poll_timeout = time;
8b0e1953 1142 hr_time = poll_timeout;
fe137230 1143
8cc2af7c
IT
1144 spin_lock_bh(&ap_poll_timer_lock);
1145 hrtimer_cancel(&ap_poll_timer);
1146 hrtimer_set_expires(&ap_poll_timer, hr_time);
1147 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1148 spin_unlock_bh(&ap_poll_timer_lock);
1149
fe137230
FB
1150 return count;
1151}
1152
cf65c71d 1153static BUS_ATTR_RW(poll_timeout);
fe137230 1154
5bc334bf
IT
1155static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1156{
889875a1
MS
1157 int max_domain_id;
1158
1159 if (ap_configuration)
1160 max_domain_id = ap_max_domain_id ? : -1;
1161 else
5bc334bf 1162 max_domain_id = 15;
5bc334bf
IT
1163 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1164}
1165
cf65c71d 1166static BUS_ATTR_RO(ap_max_domain_id);
5bc334bf 1167
3cb86ad0
HF
1168static ssize_t apmask_show(struct bus_type *bus, char *buf)
1169{
1170 int rc;
1171
1172 if (mutex_lock_interruptible(&ap_perms_mutex))
1173 return -ERESTARTSYS;
1174 rc = snprintf(buf, PAGE_SIZE,
1175 "0x%016lx%016lx%016lx%016lx\n",
1176 ap_perms.apm[0], ap_perms.apm[1],
1177 ap_perms.apm[2], ap_perms.apm[3]);
1178 mutex_unlock(&ap_perms_mutex);
1179
1180 return rc;
1181}
1182
1183static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1184 size_t count)
1185{
f7ea641f 1186 int rc;
3cb86ad0 1187
f7ea641f
HF
1188 rc = process_mask_arg(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1189 if (rc)
1190 return rc;
3cb86ad0
HF
1191
1192 ap_bus_revise_bindings();
1193
1194 return count;
1195}
1196
1197static BUS_ATTR_RW(apmask);
1198
1199static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1200{
1201 int rc;
1202
1203 if (mutex_lock_interruptible(&ap_perms_mutex))
1204 return -ERESTARTSYS;
1205 rc = snprintf(buf, PAGE_SIZE,
1206 "0x%016lx%016lx%016lx%016lx\n",
1207 ap_perms.aqm[0], ap_perms.aqm[1],
1208 ap_perms.aqm[2], ap_perms.aqm[3]);
1209 mutex_unlock(&ap_perms_mutex);
1210
1211 return rc;
1212}
1213
1214static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1215 size_t count)
1216{
f7ea641f 1217 int rc;
3cb86ad0 1218
f7ea641f
HF
1219 rc = process_mask_arg(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1220 if (rc)
1221 return rc;
3cb86ad0
HF
1222
1223 ap_bus_revise_bindings();
1224
1225 return count;
1226}
1227
1228static BUS_ATTR_RW(aqmask);
1229
1534c382
MS
1230static struct bus_attribute *const ap_bus_attrs[] = {
1231 &bus_attr_ap_domain,
91f3e3ea 1232 &bus_attr_ap_control_domain_mask,
e28d2af4 1233 &bus_attr_ap_usage_domain_mask,
1534c382
MS
1234 &bus_attr_config_time,
1235 &bus_attr_poll_thread,
cb17a636 1236 &bus_attr_ap_interrupts,
fe137230 1237 &bus_attr_poll_timeout,
5bc334bf 1238 &bus_attr_ap_max_domain_id,
3cb86ad0
HF
1239 &bus_attr_apmask,
1240 &bus_attr_aqmask,
fe137230 1241 NULL,
1534c382
MS
1242};
1243
1244/**
198f8be8
HP
1245 * ap_select_domain(): Select an AP domain if possible and we haven't
1246 * already done so before.
1534c382 1247 */
198f8be8 1248static void ap_select_domain(void)
1534c382 1249{
6acbe21f
MS
1250 int count, max_count, best_domain;
1251 struct ap_queue_status status;
1252 int i, j;
1534c382 1253
1749a81d 1254 /*
1534c382
MS
1255 * We want to use a single domain. Either the one specified with
1256 * the "domain=" parameter or the domain with the maximum number
1257 * of devices.
1258 */
fc1d3f02
IT
1259 spin_lock_bh(&ap_domain_lock);
1260 if (ap_domain_index >= 0) {
1534c382 1261 /* Domain has already been selected. */
fc1d3f02 1262 spin_unlock_bh(&ap_domain_lock);
198f8be8 1263 return;
fc1d3f02 1264 }
1534c382
MS
1265 best_domain = -1;
1266 max_count = 0;
1267 for (i = 0; i < AP_DOMAINS; i++) {
c83450ad 1268 if (!ap_test_config_usage_domain(i) ||
3cb86ad0 1269 !test_bit_inv(i, ap_perms.aqm))
75014550 1270 continue;
1534c382
MS
1271 count = 0;
1272 for (j = 0; j < AP_DEVICES; j++) {
75014550
HD
1273 if (!ap_test_config_card_id(j))
1274 continue;
e7fc5146
TK
1275 status = ap_test_queue(AP_MKQID(j, i),
1276 ap_apft_available(),
1277 NULL);
6acbe21f 1278 if (status.response_code != AP_RESPONSE_NORMAL)
1534c382
MS
1279 continue;
1280 count++;
1281 }
1282 if (count > max_count) {
1283 max_count = count;
1284 best_domain = i;
1285 }
1286 }
cf65c71d 1287 if (best_domain >= 0) {
1534c382 1288 ap_domain_index = best_domain;
ac994e80 1289 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
1534c382 1290 }
fc1d3f02 1291 spin_unlock_bh(&ap_domain_lock);
1534c382
MS
1292}
1293
9a564108
HF
1294/*
1295 * This function checks the type and returns either 0 for not
1296 * supported or the highest compatible type value (which may
1297 * include the input type value).
1298 */
1299static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1300{
1301 int comp_type = 0;
1302
1303 /* < CEX2A is not supported */
1304 if (rawtype < AP_DEVICE_TYPE_CEX2A)
1305 return 0;
1306 /* up to CEX6 known and fully supported */
1307 if (rawtype <= AP_DEVICE_TYPE_CEX6)
1308 return rawtype;
1309 /*
1310 * unknown new type > CEX6, check for compatibility
1311 * to the highest known and supported type which is
1312 * currently CEX6 with the help of the QACT function.
1313 */
1314 if (ap_qact_available()) {
1315 struct ap_queue_status status;
56c5c683 1316 union ap_qact_ap_info apinfo = {0};
9a564108
HF
1317
1318 apinfo.mode = (func >> 26) & 0x07;
1319 apinfo.cat = AP_DEVICE_TYPE_CEX6;
1320 status = ap_qact(qid, 0, &apinfo);
1321 if (status.response_code == AP_RESPONSE_NORMAL
1322 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1323 && apinfo.cat <= AP_DEVICE_TYPE_CEX6)
1324 comp_type = apinfo.cat;
1325 }
1326 if (!comp_type)
1327 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
1328 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1329 else if (comp_type != rawtype)
1330 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
1331 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
1332 return comp_type;
1333}
1334
e28d2af4
IT
1335/*
1336 * helper function to be used with bus_find_dev
1337 * matches for the card device with the given id
1534c382 1338 */
e28d2af4 1339static int __match_card_device_with_id(struct device *dev, void *data)
1534c382 1340{
e28d2af4 1341 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
1534c382
MS
1342}
1343
e28d2af4
IT
1344/* helper function to be used with bus_find_dev
1345 * matches for the queue device with a given qid
1346 */
1347static int __match_queue_device_with_qid(struct device *dev, void *data)
1348{
1349 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1350}
1351
1352/**
1353 * ap_scan_bus(): Scan the AP bus for new devices
1354 * Runs periodically, workqueue timer (ap_config_time)
1355 */
4927b3f7 1356static void ap_scan_bus(struct work_struct *unused)
1534c382 1357{
e28d2af4
IT
1358 struct ap_queue *aq;
1359 struct ap_card *ac;
1534c382
MS
1360 struct device *dev;
1361 ap_qid_t qid;
9a564108
HF
1362 int comp_type, depth = 0, type = 0;
1363 unsigned int func = 0;
ac994e80 1364 int rc, id, dom, borked, domains, defdomdevs = 0;
1534c382 1365
cf65c71d 1366 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
cccd85bf 1367
050349b5 1368 ap_query_configuration(ap_configuration);
198f8be8 1369 ap_select_domain();
889875a1 1370
e28d2af4
IT
1371 for (id = 0; id < AP_DEVICES; id++) {
1372 /* check if device is registered */
1534c382 1373 dev = bus_find_device(&ap_bus_type, NULL,
e28d2af4
IT
1374 (void *)(long) id,
1375 __match_card_device_with_id);
1376 ac = dev ? to_ap_card(dev) : NULL;
1377 if (!ap_test_config_card_id(id)) {
1378 if (dev) {
1379 /* Card device has been removed from
1380 * configuration, remove the belonging
1381 * queue devices.
1382 */
1383 bus_for_each_dev(&ap_bus_type, NULL,
1384 (void *)(long) id,
1385 __ap_queue_devices_with_id_unregister);
1386 /* now remove the card device */
3f3007af 1387 device_unregister(dev);
e28d2af4
IT
1388 put_device(dev);
1389 }
1534c382
MS
1390 continue;
1391 }
e28d2af4
IT
1392 /* According to the configuration there should be a card
1393 * device, so check if there is at least one valid queue
1394 * and maybe create queue devices and the card device.
1395 */
1396 domains = 0;
1397 for (dom = 0; dom < AP_DOMAINS; dom++) {
1398 qid = AP_MKQID(id, dom);
1399 dev = bus_find_device(&ap_bus_type, NULL,
1400 (void *)(long) qid,
1401 __match_queue_device_with_qid);
1402 aq = dev ? to_ap_queue(dev) : NULL;
c83450ad 1403 if (!ap_test_config_usage_domain(dom)) {
e28d2af4
IT
1404 if (dev) {
1405 /* Queue device exists but has been
1406 * removed from configuration.
1407 */
1408 device_unregister(dev);
1409 put_device(dev);
1410 }
1411 continue;
1412 }
9a564108 1413 rc = ap_query_queue(qid, &depth, &type, &func);
e28d2af4
IT
1414 if (dev) {
1415 spin_lock_bh(&aq->lock);
1416 if (rc == -ENODEV ||
1417 /* adapter reconfiguration */
9a564108 1418 (ac && ac->functions != func))
e28d2af4
IT
1419 aq->state = AP_STATE_BORKED;
1420 borked = aq->state == AP_STATE_BORKED;
1421 spin_unlock_bh(&aq->lock);
1422 if (borked) /* Remove broken device */
1423 device_unregister(dev);
1424 put_device(dev);
1425 if (!borked) {
1426 domains++;
ac994e80
HF
1427 if (dom == ap_domain_index)
1428 defdomdevs++;
e28d2af4
IT
1429 continue;
1430 }
1431 }
1432 if (rc)
1433 continue;
9a564108
HF
1434 /* a new queue device is needed, check out comp type */
1435 comp_type = ap_get_compatible_type(qid, type, func);
1436 if (!comp_type)
1437 continue;
1438 /* maybe a card device needs to be created first */
e28d2af4 1439 if (!ac) {
9a564108
HF
1440 ac = ap_card_create(id, depth, type,
1441 comp_type, func);
e28d2af4
IT
1442 if (!ac)
1443 continue;
1444 ac->ap_dev.device.bus = &ap_bus_type;
1445 ac->ap_dev.device.parent = ap_root_device;
1446 dev_set_name(&ac->ap_dev.device,
1447 "card%02x", id);
1448 /* Register card with AP bus */
1449 rc = device_register(&ac->ap_dev.device);
1450 if (rc) {
1451 put_device(&ac->ap_dev.device);
1452 ac = NULL;
1453 break;
1454 }
1455 /* get it and thus adjust reference counter */
1456 get_device(&ac->ap_dev.device);
e28d2af4
IT
1457 }
1458 /* now create the new queue device */
9a564108 1459 aq = ap_queue_create(qid, comp_type);
e28d2af4
IT
1460 if (!aq)
1461 continue;
1462 aq->card = ac;
1463 aq->ap_dev.device.bus = &ap_bus_type;
1464 aq->ap_dev.device.parent = &ac->ap_dev.device;
1465 dev_set_name(&aq->ap_dev.device,
1466 "%02x.%04x", id, dom);
e28d2af4
IT
1467 /* Register device */
1468 rc = device_register(&aq->ap_dev.device);
1469 if (rc) {
e28d2af4
IT
1470 put_device(&aq->ap_dev.device);
1471 continue;
1472 }
1473 domains++;
ac994e80
HF
1474 if (dom == ap_domain_index)
1475 defdomdevs++;
e28d2af4
IT
1476 } /* end domain loop */
1477 if (ac) {
1478 /* remove card dev if there are no queue devices */
1479 if (!domains)
1480 device_unregister(&ac->ap_dev.device);
1481 put_device(&ac->ap_dev.device);
3f3007af 1482 }
e28d2af4 1483 } /* end device loop */
ac994e80 1484
198f8be8 1485 if (ap_domain_index >= 0 && defdomdevs < 1)
cf65c71d
HF
1486 AP_DBF(DBF_INFO,
1487 "no queue device with default domain %d available\n",
ac994e80
HF
1488 ap_domain_index);
1489
fcd0d1f6 1490 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1534c382
MS
1491}
1492
cefbeb5d 1493static void ap_config_timeout(struct timer_list *unused)
1534c382 1494{
83e9d5d2
MS
1495 if (ap_suspend_flag)
1496 return;
8139b89d 1497 queue_work(system_long_wq, &ap_scan_work);
1534c382 1498}
13e742ba
RW
1499
1500static void ap_reset_all(void)
85eca850
RW
1501{
1502 int i, j;
1503
170387a8 1504 for (i = 0; i < AP_DOMAINS; i++) {
c83450ad 1505 if (!ap_test_config_usage_domain(i))
170387a8
IT
1506 continue;
1507 for (j = 0; j < AP_DEVICES; j++) {
1508 if (!ap_test_config_card_id(j))
1509 continue;
0db78559 1510 ap_rapq(AP_MKQID(j, i));
170387a8
IT
1511 }
1512 }
85eca850
RW
1513}
1514
1515static struct reset_call ap_reset_call = {
13e742ba 1516 .fn = ap_reset_all,
85eca850
RW
1517};
1518
cccd85bf
HF
1519int __init ap_debug_init(void)
1520{
cccd85bf
HF
1521 ap_dbf_info = debug_register("ap", 1, 1,
1522 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1523 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1524 debug_set_level(ap_dbf_info, DBF_ERR);
1525
1526 return 0;
1527}
1528
3cb86ad0
HF
1529static void __init ap_perms_init(void)
1530{
f7ea641f 1531 /* all resources useable if no kernel parameter string given */
3cb86ad0
HF
1532 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1533 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1534
f7ea641f 1535 /* apm kernel parameter string */
3cb86ad0 1536 if (apm_str) {
f7ea641f
HF
1537 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1538 process_mask_arg(apm_str, ap_perms.apm, AP_DEVICES,
1539 &ap_perms_mutex);
3cb86ad0 1540 }
3cb86ad0 1541
f7ea641f
HF
1542 /* aqm kernel parameter string */
1543 if (aqm_str) {
1544 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1545 process_mask_arg(aqm_str, ap_perms.aqm, AP_DOMAINS,
1546 &ap_perms_mutex);
3cb86ad0
HF
1547 }
1548}
1549
1534c382 1550/**
1749a81d
FB
1551 * ap_module_init(): The module initialization code.
1552 *
1553 * Initializes the module.
1534c382
MS
1554 */
1555int __init ap_module_init(void)
1556{
889875a1 1557 int max_domain_id;
1534c382
MS
1558 int rc, i;
1559
cccd85bf
HF
1560 rc = ap_debug_init();
1561 if (rc)
1562 return rc;
1563
94fdf2e7 1564 if (!ap_instructions_available()) {
889875a1
MS
1565 pr_warn("The hardware system does not support AP instructions\n");
1566 return -ENODEV;
1567 }
1568
3cb86ad0
HF
1569 /* set up the AP permissions (ap and aq masks) */
1570 ap_perms_init();
1571
889875a1
MS
1572 /* Get AP configuration data if available */
1573 ap_init_configuration();
1574
1575 if (ap_configuration)
ac994e80
HF
1576 max_domain_id =
1577 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
889875a1
MS
1578 else
1579 max_domain_id = 15;
3cb86ad0
HF
1580 if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
1581 (ap_domain_index >= 0 &&
1582 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
889875a1
MS
1583 pr_warn("%d is not a valid cryptographic domain\n",
1584 ap_domain_index);
ac994e80 1585 ap_domain_index = -1;
1534c382 1586 }
5314af69
FB
1587 /* In resume callback we need to know if the user had set the domain.
1588 * If so, we can not just reset it.
1589 */
1590 if (ap_domain_index >= 0)
1591 user_set_domain = 1;
1592
cb17a636 1593 if (ap_interrupts_available()) {
f4eae94f
MS
1594 rc = register_adapter_interrupt(&ap_airq);
1595 ap_airq_flag = (rc == 0);
cb17a636
FB
1596 }
1597
85eca850 1598 register_reset_call(&ap_reset_call);
1534c382
MS
1599
1600 /* Create /sys/bus/ap. */
1601 rc = bus_register(&ap_bus_type);
1602 if (rc)
1603 goto out;
1604 for (i = 0; ap_bus_attrs[i]; i++) {
1605 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1606 if (rc)
1607 goto out_bus;
1608 }
1609
1610 /* Create /sys/devices/ap. */
035da16f 1611 ap_root_device = root_device_register("ap");
ba8da213 1612 rc = PTR_RET(ap_root_device);
1534c382
MS
1613 if (rc)
1614 goto out_bus;
1615
1749a81d 1616 /* Setup the AP bus rescan timer. */
cefbeb5d 1617 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1534c382 1618
3f3007af
MS
1619 /*
1620 * Setup the high resultion poll timer.
fe137230
FB
1621 * If we are running under z/VM adjust polling to z/VM polling rate.
1622 */
1623 if (MACHINE_IS_VM)
1624 poll_timeout = 1500000;
93521314 1625 spin_lock_init(&ap_poll_timer_lock);
fe137230
FB
1626 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1627 ap_poll_timer.function = ap_poll_timeout;
1628
1534c382
MS
1629 /* Start the low priority AP bus poll thread. */
1630 if (ap_thread_flag) {
1631 rc = ap_poll_thread_start();
1632 if (rc)
1633 goto out_work;
1634 }
1635
83e9d5d2
MS
1636 rc = register_pm_notifier(&ap_power_notifier);
1637 if (rc)
1638 goto out_pm;
1639
8139b89d 1640 queue_work(system_long_wq, &ap_scan_work);
e387753c 1641 initialised = true;
3f3007af 1642
1534c382
MS
1643 return 0;
1644
83e9d5d2
MS
1645out_pm:
1646 ap_poll_thread_stop();
1534c382 1647out_work:
fe137230 1648 hrtimer_cancel(&ap_poll_timer);
035da16f 1649 root_device_unregister(ap_root_device);
1534c382
MS
1650out_bus:
1651 while (i--)
1652 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1653 bus_unregister(&ap_bus_type);
1654out:
85eca850 1655 unregister_reset_call(&ap_reset_call);
f4eae94f
MS
1656 if (ap_using_interrupts())
1657 unregister_adapter_interrupt(&ap_airq);
889875a1 1658 kfree(ap_configuration);
1534c382
MS
1659 return rc;
1660}
50a0d46c 1661device_initcall(ap_module_init);