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