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