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