xref: /openbmc/linux/drivers/s390/crypto/ap_bus.c (revision 8730046c)
1 /*
2  * Copyright IBM Corp. 2006, 2012
3  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4  *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
5  *	      Ralph Wuerthner <rwuerthn@de.ibm.com>
6  *	      Felix Beck <felix.beck@de.ibm.com>
7  *	      Holger Dengler <hd@linux.vnet.ibm.com>
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 
26 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28 
29 #include <linux/kernel_stat.h>
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>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <linux/suspend.h>
41 #include <asm/reset.h>
42 #include <asm/airq.h>
43 #include <linux/atomic.h>
44 #include <asm/isc.h>
45 #include <linux/hrtimer.h>
46 #include <linux/ktime.h>
47 #include <asm/facility.h>
48 #include <linux/crypto.h>
49 #include <linux/mod_devicetable.h>
50 #include <linux/debugfs.h>
51 
52 #include "ap_bus.h"
53 #include "ap_asm.h"
54 #include "ap_debug.h"
55 
56 /*
57  * Module description.
58  */
59 MODULE_AUTHOR("IBM Corporation");
60 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
61 		   "Copyright IBM Corp. 2006, 2012");
62 MODULE_LICENSE("GPL");
63 MODULE_ALIAS_CRYPTO("z90crypt");
64 
65 /*
66  * Module parameter
67  */
68 int ap_domain_index = -1;	/* Adjunct Processor Domain Index */
69 static DEFINE_SPINLOCK(ap_domain_lock);
70 module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
71 MODULE_PARM_DESC(domain, "domain index for ap devices");
72 EXPORT_SYMBOL(ap_domain_index);
73 
74 static int ap_thread_flag = 0;
75 module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
76 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
77 
78 static struct device *ap_root_device;
79 
80 DEFINE_SPINLOCK(ap_list_lock);
81 LIST_HEAD(ap_card_list);
82 
83 static struct ap_config_info *ap_configuration;
84 static bool initialised;
85 
86 /*
87  * AP bus related debug feature things.
88  */
89 static struct dentry *ap_dbf_root;
90 debug_info_t *ap_dbf_info;
91 
92 /*
93  * Workqueue timer for bus rescan.
94  */
95 static struct timer_list ap_config_timer;
96 static int ap_config_time = AP_CONFIG_TIME;
97 static void ap_scan_bus(struct work_struct *);
98 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
99 
100 /*
101  * Tasklet & timer for AP request polling and interrupts
102  */
103 static void ap_tasklet_fn(unsigned long);
104 static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
105 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
106 static struct task_struct *ap_poll_kthread = NULL;
107 static DEFINE_MUTEX(ap_poll_thread_mutex);
108 static DEFINE_SPINLOCK(ap_poll_timer_lock);
109 static 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.*/
112 static unsigned long long poll_timeout = 250000;
113 
114 /* Suspend flag */
115 static int ap_suspend_flag;
116 /* Maximum domain id */
117 static int ap_max_domain_id;
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. */
121 static int user_set_domain = 0;
122 static struct bus_type ap_bus_type;
123 
124 /* Adapter interrupt definitions */
125 static void ap_interrupt_handler(struct airq_struct *airq);
126 
127 static int ap_airq_flag;
128 
129 static struct airq_struct ap_airq = {
130 	.handler = ap_interrupt_handler,
131 	.isc = AP_ISC,
132 };
133 
134 /**
135  * ap_using_interrupts() - Returns non-zero if interrupt support is
136  * available.
137  */
138 static inline int ap_using_interrupts(void)
139 {
140 	return ap_airq_flag;
141 }
142 
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  */
150 void *ap_airq_ptr(void)
151 {
152 	if (ap_using_interrupts())
153 		return ap_airq.lsi_ptr;
154 	return NULL;
155 }
156 
157 /**
158  * ap_interrupts_available(): Test if AP interrupts are available.
159  *
160  * Returns 1 if AP interrupts are available.
161  */
162 static int ap_interrupts_available(void)
163 {
164 	return test_facility(65);
165 }
166 
167 /**
168  * ap_configuration_available(): Test if AP configuration
169  * information is available.
170  *
171  * Returns 1 if AP configuration information is available.
172  */
173 static int ap_configuration_available(void)
174 {
175 	return test_facility(12);
176 }
177 
178 /**
179  * ap_test_queue(): Test adjunct processor queue.
180  * @qid: The AP queue number
181  * @info: Pointer to queue descriptor
182  *
183  * Returns AP queue status structure.
184  */
185 static inline struct ap_queue_status
186 ap_test_queue(ap_qid_t qid, unsigned long *info)
187 {
188 	if (test_facility(15))
189 		qid |= 1UL << 23;		/* set APFT T bit*/
190 	return ap_tapq(qid, info);
191 }
192 
193 static inline int ap_query_configuration(void)
194 {
195 	if (!ap_configuration)
196 		return -EOPNOTSUPP;
197 	return ap_qci(ap_configuration);
198 }
199 
200 /**
201  * ap_init_configuration(): Allocate and query configuration array.
202  */
203 static 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  */
222 static 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  */
235 static 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  */
250 static 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 
257 /**
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
262  * @facilities: Pointer to facility indicator
263  */
264 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
265 			  unsigned int *facilities)
266 {
267 	struct ap_queue_status status;
268 	unsigned long info;
269 	int nd;
270 
271 	if (!ap_test_config_card_id(AP_QID_CARD(qid)))
272 		return -ENODEV;
273 
274 	status = ap_test_queue(qid, &info);
275 	switch (status.response_code) {
276 	case AP_RESPONSE_NORMAL:
277 		*queue_depth = (int)(info & 0xff);
278 		*device_type = (int)((info >> 24) & 0xff);
279 		*facilities = (unsigned int)(info >> 32);
280 		/* Update maximum domain id */
281 		nd = (info >> 16) & 0xff;
282 		/* if N bit is available, z13 and newer */
283 		if ((info & (1UL << 57)) && nd > 0)
284 			ap_max_domain_id = nd;
285 		else /* older machine types */
286 			ap_max_domain_id = 15;
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 		}
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();
316 	}
317 }
318 
319 void ap_wait(enum ap_wait wait)
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)) {
336 			hr_time = poll_timeout;
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 
348 /**
349  * ap_request_timeout(): Handling of request timeouts
350  * @data: Holds the AP device.
351  *
352  * Handles request timeouts.
353  */
354 void ap_request_timeout(unsigned long data)
355 {
356 	struct ap_queue *aq = (struct ap_queue *) data;
357 
358 	if (ap_suspend_flag)
359 		return;
360 	spin_lock_bh(&aq->lock);
361 	ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
362 	spin_unlock_bh(&aq->lock);
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  */
371 static 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  */
382 static 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  */
395 static void ap_tasklet_fn(unsigned long dummy)
396 {
397 	struct ap_card *ac;
398 	struct ap_queue *aq;
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 
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 		}
415 	}
416 	spin_unlock_bh(&ap_list_lock);
417 
418 	ap_wait(wait);
419 }
420 
421 static int ap_pending_requests(void)
422 {
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;
433 		}
434 	}
435 	spin_unlock_bh(&ap_list_lock);
436 	return 0;
437 }
438 
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  */
449 static int ap_poll_thread(void *data)
450 {
451 	DECLARE_WAITQUEUE(wait, current);
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);
458 		if (ap_suspend_flag || !ap_pending_requests()) {
459 			schedule();
460 			try_to_freeze();
461 		}
462 		set_current_state(TASK_RUNNING);
463 		remove_wait_queue(&ap_poll_wait, &wait);
464 		if (need_resched()) {
465 			schedule();
466 			try_to_freeze();
467 			continue;
468 		}
469 		ap_tasklet_fn(0);
470 	}
471 
472 	return 0;
473 }
474 
475 static 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 
490 static 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 
500 #define is_card_dev(x) ((x)->parent == ap_root_device)
501 #define is_queue_dev(x) ((x)->parent != ap_root_device)
502 
503 /**
504  * ap_bus_match()
505  * @dev: Pointer to device
506  * @drv: Pointer to device_driver
507  *
508  * AP bus driver registration/unregistration.
509  */
510 static int ap_bus_match(struct device *dev, struct device_driver *drv)
511 {
512 	struct ap_driver *ap_drv = to_ap_drv(drv);
513 	struct ap_device_id *id;
514 
515 	/*
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++) {
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;
528 	}
529 	return 0;
530 }
531 
532 /**
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.
539  */
540 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
541 {
542 	struct ap_device *ap_dev = to_ap_dev(dev);
543 	int retval = 0;
544 
545 	if (!ap_dev)
546 		return -ENODEV;
547 
548 	/* Set up DEV_TYPE environment variable. */
549 	retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
550 	if (retval)
551 		return retval;
552 
553 	/* Add MODALIAS= */
554 	retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
555 
556 	return retval;
557 }
558 
559 static int ap_dev_suspend(struct device *dev)
560 {
561 	struct ap_device *ap_dev = to_ap_dev(dev);
562 
563 	if (ap_dev->drv && ap_dev->drv->suspend)
564 		ap_dev->drv->suspend(ap_dev);
565 	return 0;
566 }
567 
568 static 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);
574 	return 0;
575 }
576 
577 static void ap_bus_suspend(void)
578 {
579 	AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
580 
581 	ap_suspend_flag = 1;
582 	/*
583 	 * Disable scanning for devices, thus we do not want to scan
584 	 * for them after removing.
585 	 */
586 	flush_work(&ap_scan_work);
587 	tasklet_disable(&ap_tasklet);
588 }
589 
590 static 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 
597 static 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 
604 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
605 {
606 	if (is_queue_dev(dev) &&
607 	    AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
608 		device_unregister(dev);
609 	return 0;
610 }
611 
612 static void ap_bus_resume(void)
613 {
614 	int rc;
615 
616 	AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
617 
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 
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);
629 	}
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);
642 	queue_work(system_long_wq, &ap_scan_work);
643 }
644 
645 static 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;
661 }
662 static struct notifier_block ap_power_notifier = {
663 	.notifier_call = ap_power_event,
664 };
665 
666 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
667 
668 static struct bus_type ap_bus_type = {
669 	.name = "ap",
670 	.match = &ap_bus_match,
671 	.uevent = &ap_uevent,
672 	.pm = &ap_bus_pm_ops,
673 };
674 
675 static int ap_device_probe(struct device *dev)
676 {
677 	struct ap_device *ap_dev = to_ap_dev(dev);
678 	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
679 	int rc;
680 
681 	ap_dev->drv = ap_drv;
682 	rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
683 	if (rc)
684 		ap_dev->drv = NULL;
685 	return rc;
686 }
687 
688 static 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 
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);
699 	if (ap_drv->remove)
700 		ap_drv->remove(ap_dev);
701 	return 0;
702 }
703 
704 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
705 		       char *name)
706 {
707 	struct device_driver *drv = &ap_drv->driver;
708 
709 	if (!initialised)
710 		return -ENODEV;
711 
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 }
719 EXPORT_SYMBOL(ap_driver_register);
720 
721 void ap_driver_unregister(struct ap_driver *ap_drv)
722 {
723 	driver_unregister(&ap_drv->driver);
724 }
725 EXPORT_SYMBOL(ap_driver_unregister);
726 
727 void ap_bus_force_rescan(void)
728 {
729 	if (ap_suspend_flag)
730 		return;
731 	/* processing a asynchronous bus rescan */
732 	del_timer(&ap_config_timer);
733 	queue_work(system_long_wq, &ap_scan_work);
734 	flush_work(&ap_scan_work);
735 }
736 EXPORT_SYMBOL(ap_bus_force_rescan);
737 
738 /*
739  * AP bus attributes.
740  */
741 static 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 
746 static 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);
757 
758 	AP_DBF(DBF_DEBUG, "store new default domain=%d\n", domain);
759 
760 	return count;
761 }
762 
763 static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
764 
765 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
766 {
767 	if (!ap_configuration)	/* QCI not supported */
768 		return snprintf(buf, PAGE_SIZE, "not supported\n");
769 
770 	return snprintf(buf, PAGE_SIZE,
771 			"0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
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]);
776 }
777 
778 static BUS_ATTR(ap_control_domain_mask, 0444,
779 		ap_control_domain_mask_show, NULL);
780 
781 static 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 
794 static BUS_ATTR(ap_usage_domain_mask, 0444,
795 		ap_usage_domain_mask_show, NULL);
796 
797 static 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 
802 static 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 
808 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
809 
810 static 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;
818 	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
819 	return count;
820 }
821 
822 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
823 
824 static 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 
829 static 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)
839 			count = rc;
840 	} else
841 		ap_poll_thread_stop();
842 	return count;
843 }
844 
845 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
846 
847 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
848 {
849 	return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
850 }
851 
852 static 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 */
859 	if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
860 	    time > 120000000000ULL)
861 		return -EINVAL;
862 	poll_timeout = time;
863 	hr_time = poll_timeout;
864 
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 
871 	return count;
872 }
873 
874 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
875 
876 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
877 {
878 	int max_domain_id;
879 
880 	if (ap_configuration)
881 		max_domain_id = ap_max_domain_id ? : -1;
882 	else
883 		max_domain_id = 15;
884 	return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
885 }
886 
887 static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
888 
889 static struct bus_attribute *const ap_bus_attrs[] = {
890 	&bus_attr_ap_domain,
891 	&bus_attr_ap_control_domain_mask,
892 	&bus_attr_ap_usage_domain_mask,
893 	&bus_attr_config_time,
894 	&bus_attr_poll_thread,
895 	&bus_attr_ap_interrupts,
896 	&bus_attr_poll_timeout,
897 	&bus_attr_ap_max_domain_id,
898 	NULL,
899 };
900 
901 /**
902  * ap_select_domain(): Select an AP domain.
903  *
904  * Pick one of the 16 AP domains.
905  */
906 static int ap_select_domain(void)
907 {
908 	int count, max_count, best_domain;
909 	struct ap_queue_status status;
910 	int i, j;
911 
912 	/*
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 	 */
917 	spin_lock_bh(&ap_domain_lock);
918 	if (ap_domain_index >= 0) {
919 		/* Domain has already been selected. */
920 		spin_unlock_bh(&ap_domain_lock);
921 		return 0;
922 	}
923 	best_domain = -1;
924 	max_count = 0;
925 	for (i = 0; i < AP_DOMAINS; i++) {
926 		if (!ap_test_config_domain(i))
927 			continue;
928 		count = 0;
929 		for (j = 0; j < AP_DEVICES; j++) {
930 			if (!ap_test_config_card_id(j))
931 				continue;
932 			status = ap_test_queue(AP_MKQID(j, i), NULL);
933 			if (status.response_code != AP_RESPONSE_NORMAL)
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;
944 		spin_unlock_bh(&ap_domain_lock);
945 		return 0;
946 	}
947 	spin_unlock_bh(&ap_domain_lock);
948 	return -ENODEV;
949 }
950 
951 /*
952  * helper function to be used with bus_find_dev
953  * matches for the card device with the given id
954  */
955 static int __match_card_device_with_id(struct device *dev, void *data)
956 {
957 	return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
958 }
959 
960 /* helper function to be used with bus_find_dev
961  * matches for the queue device with a given qid
962  */
963 static 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  */
972 static void ap_scan_bus(struct work_struct *unused)
973 {
974 	struct ap_queue *aq;
975 	struct ap_card *ac;
976 	struct device *dev;
977 	ap_qid_t qid;
978 	int depth = 0, type = 0;
979 	unsigned int functions = 0;
980 	int rc, id, dom, borked, domains;
981 
982 	AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
983 
984 	ap_query_configuration();
985 	if (ap_select_domain() != 0)
986 		goto out;
987 
988 	for (id = 0; id < AP_DEVICES; id++) {
989 		/* check if device is registered */
990 		dev = bus_find_device(&ap_bus_type, NULL,
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 */
1004 				device_unregister(dev);
1005 				put_device(dev);
1006 			}
1007 			continue;
1008 		}
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);
1107 		}
1108 	} /* end device loop */
1109 out:
1110 	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1111 }
1112 
1113 static void ap_config_timeout(unsigned long ptr)
1114 {
1115 	if (ap_suspend_flag)
1116 		return;
1117 	queue_work(system_long_wq, &ap_scan_work);
1118 }
1119 
1120 static void ap_reset_domain(void)
1121 {
1122 	int i;
1123 
1124 	if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index))
1125 		return;
1126 	for (i = 0; i < AP_DEVICES; i++)
1127 		ap_rapq(AP_MKQID(i, ap_domain_index));
1128 }
1129 
1130 static void ap_reset_all(void)
1131 {
1132 	int i, j;
1133 
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;
1140 			ap_rapq(AP_MKQID(j, i));
1141 		}
1142 	}
1143 }
1144 
1145 static struct reset_call ap_reset_call = {
1146 	.fn = ap_reset_all,
1147 };
1148 
1149 int __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 
1160 void ap_debug_exit(void)
1161 {
1162 	debugfs_remove(ap_dbf_root);
1163 	debug_unregister(ap_dbf_info);
1164 }
1165 
1166 /**
1167  * ap_module_init(): The module initialization code.
1168  *
1169  * Initializes the module.
1170  */
1171 int __init ap_module_init(void)
1172 {
1173 	int max_domain_id;
1174 	int rc, i;
1175 
1176 	rc = ap_debug_init();
1177 	if (rc)
1178 		return rc;
1179 
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);
1195 		rc = -EINVAL;
1196 		goto out_free;
1197 	}
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 
1204 	if (ap_interrupts_available()) {
1205 		rc = register_adapter_interrupt(&ap_airq);
1206 		ap_airq_flag = (rc == 0);
1207 	}
1208 
1209 	register_reset_call(&ap_reset_call);
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. */
1222 	ap_root_device = root_device_register("ap");
1223 	rc = PTR_RET(ap_root_device);
1224 	if (rc)
1225 		goto out_bus;
1226 
1227 	/* Setup the AP bus rescan timer. */
1228 	setup_timer(&ap_config_timer, ap_config_timeout, 0);
1229 
1230 	/*
1231 	 * Setup the high resultion poll timer.
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;
1236 	spin_lock_init(&ap_poll_timer_lock);
1237 	hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1238 	ap_poll_timer.function = ap_poll_timeout;
1239 
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 
1247 	rc = register_pm_notifier(&ap_power_notifier);
1248 	if (rc)
1249 		goto out_pm;
1250 
1251 	queue_work(system_long_wq, &ap_scan_work);
1252 	initialised = true;
1253 
1254 	return 0;
1255 
1256 out_pm:
1257 	ap_poll_thread_stop();
1258 out_work:
1259 	hrtimer_cancel(&ap_poll_timer);
1260 	root_device_unregister(ap_root_device);
1261 out_bus:
1262 	while (i--)
1263 		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1264 	bus_unregister(&ap_bus_type);
1265 out:
1266 	unregister_reset_call(&ap_reset_call);
1267 	if (ap_using_interrupts())
1268 		unregister_adapter_interrupt(&ap_airq);
1269 out_free:
1270 	kfree(ap_configuration);
1271 	return rc;
1272 }
1273 
1274 /**
1275  * ap_modules_exit(): The module termination code
1276  *
1277  * Terminates the module.
1278  */
1279 void ap_module_exit(void)
1280 {
1281 	int i;
1282 
1283 	initialised = false;
1284 	ap_reset_domain();
1285 	ap_poll_thread_stop();
1286 	del_timer_sync(&ap_config_timer);
1287 	hrtimer_cancel(&ap_poll_timer);
1288 	tasklet_kill(&ap_tasklet);
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 */
1298 	for (i = 0; ap_bus_attrs[i]; i++)
1299 		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1300 	unregister_pm_notifier(&ap_power_notifier);
1301 	root_device_unregister(ap_root_device);
1302 	bus_unregister(&ap_bus_type);
1303 	kfree(ap_configuration);
1304 	unregister_reset_call(&ap_reset_call);
1305 	if (ap_using_interrupts())
1306 		unregister_adapter_interrupt(&ap_airq);
1307 
1308 	ap_debug_exit();
1309 }
1310 
1311 module_init(ap_module_init);
1312 module_exit(ap_module_exit);
1313