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