1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright IBM Corp. 2006, 2012
4 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5 *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
6 *	      Ralph Wuerthner <rwuerthn@de.ibm.com>
7 *	      Felix Beck <felix.beck@de.ibm.com>
8 *	      Holger Dengler <hd@linux.vnet.ibm.com>
9 *
10 * Adjunct processor bus.
11 */
12
13#define KMSG_COMPONENT "ap"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16#include <linux/kernel_stat.h>
17#include <linux/moduleparam.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/freezer.h>
22#include <linux/interrupt.h>
23#include <linux/workqueue.h>
24#include <linux/slab.h>
25#include <linux/notifier.h>
26#include <linux/kthread.h>
27#include <linux/mutex.h>
28#include <asm/airq.h>
29#include <linux/atomic.h>
30#include <asm/isc.h>
31#include <linux/hrtimer.h>
32#include <linux/ktime.h>
33#include <asm/facility.h>
34#include <linux/crypto.h>
35#include <linux/mod_devicetable.h>
36#include <linux/debugfs.h>
37#include <linux/ctype.h>
38
39#include "ap_bus.h"
40#include "ap_debug.h"
41
42/*
43 * Module parameters; note though this file itself isn't modular.
44 */
45int ap_domain_index = -1;	/* Adjunct Processor Domain Index */
46static DEFINE_SPINLOCK(ap_domain_lock);
47module_param_named(domain, ap_domain_index, int, 0440);
48MODULE_PARM_DESC(domain, "domain index for ap devices");
49EXPORT_SYMBOL(ap_domain_index);
50
51static int ap_thread_flag;
52module_param_named(poll_thread, ap_thread_flag, int, 0440);
53MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
54
55static char *apm_str;
56module_param_named(apmask, apm_str, charp, 0440);
57MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
58
59static char *aqm_str;
60module_param_named(aqmask, aqm_str, charp, 0440);
61MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
62
63static struct device *ap_root_device;
64
65/* Hashtable of all queue devices on the AP bus */
66DEFINE_HASHTABLE(ap_queues, 8);
67/* lock used for the ap_queues hashtable */
68DEFINE_SPINLOCK(ap_queues_lock);
69
70/* Default permissions (ioctl, card and domain masking) */
71struct ap_perms ap_perms;
72EXPORT_SYMBOL(ap_perms);
73DEFINE_MUTEX(ap_perms_mutex);
74EXPORT_SYMBOL(ap_perms_mutex);
75
76static struct ap_config_info *ap_qci_info;
77
78/*
79 * AP bus related debug feature things.
80 */
81debug_info_t *ap_dbf_info;
82
83/*
84 * Workqueue timer for bus rescan.
85 */
86static struct timer_list ap_config_timer;
87static int ap_config_time = AP_CONFIG_TIME;
88static void ap_scan_bus(struct work_struct *);
89static DECLARE_WORK(ap_scan_work, ap_scan_bus);
90
91/*
92 * Tasklet & timer for AP request polling and interrupts
93 */
94static void ap_tasklet_fn(unsigned long);
95static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
96static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
97static struct task_struct *ap_poll_kthread;
98static DEFINE_MUTEX(ap_poll_thread_mutex);
99static DEFINE_SPINLOCK(ap_poll_timer_lock);
100static struct hrtimer ap_poll_timer;
101/*
102 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
103 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
104 */
105static unsigned long long poll_timeout = 250000;
106
107/* Maximum domain id, if not given via qci */
108static int ap_max_domain_id = 15;
109/* Maximum adapter id, if not given via qci */
110static int ap_max_adapter_id = 63;
111
112static struct bus_type ap_bus_type;
113
114/* Adapter interrupt definitions */
115static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
116
117static bool ap_irq_flag;
118
119static struct airq_struct ap_airq = {
120	.handler = ap_interrupt_handler,
121	.isc = AP_ISC,
122};
123
124/**
125 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
126 *
127 * Returns the address of the local-summary-indicator of the adapter
128 * interrupt handler for AP, or NULL if adapter interrupts are not
129 * available.
130 */
131void *ap_airq_ptr(void)
132{
133	if (ap_irq_flag)
134		return ap_airq.lsi_ptr;
135	return NULL;
136}
137
138/**
139 * ap_interrupts_available(): Test if AP interrupts are available.
140 *
141 * Returns 1 if AP interrupts are available.
142 */
143static int ap_interrupts_available(void)
144{
145	return test_facility(65);
146}
147
148/**
149 * ap_qci_available(): Test if AP configuration
150 * information can be queried via QCI subfunction.
151 *
152 * Returns 1 if subfunction PQAP(QCI) is available.
153 */
154static int ap_qci_available(void)
155{
156	return test_facility(12);
157}
158
159/**
160 * ap_apft_available(): Test if AP facilities test (APFT)
161 * facility is available.
162 *
163 * Returns 1 if APFT is is available.
164 */
165static int ap_apft_available(void)
166{
167	return test_facility(15);
168}
169
170/*
171 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
172 *
173 * Returns 1 if the QACT subfunction is available.
174 */
175static inline int ap_qact_available(void)
176{
177	if (ap_qci_info)
178		return ap_qci_info->qact;
179	return 0;
180}
181
182/*
183 * ap_fetch_qci_info(): Fetch cryptographic config info
184 *
185 * Returns the ap configuration info fetched via PQAP(QCI).
186 * On success 0 is returned, on failure a negative errno
187 * is returned, e.g. if the PQAP(QCI) instruction is not
188 * available, the return value will be -EOPNOTSUPP.
189 */
190static inline int ap_fetch_qci_info(struct ap_config_info *info)
191{
192	if (!ap_qci_available())
193		return -EOPNOTSUPP;
194	if (!info)
195		return -EINVAL;
196	return ap_qci(info);
197}
198
199/**
200 * ap_init_qci_info(): Allocate and query qci config info.
201 * Does also update the static variables ap_max_domain_id
202 * and ap_max_adapter_id if this info is available.
203
204 */
205static void __init ap_init_qci_info(void)
206{
207	if (!ap_qci_available()) {
208		AP_DBF_INFO("%s QCI not supported\n", __func__);
209		return;
210	}
211
212	ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
213	if (!ap_qci_info)
214		return;
215	if (ap_fetch_qci_info(ap_qci_info) != 0) {
216		kfree(ap_qci_info);
217		ap_qci_info = NULL;
218		return;
219	}
220	AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
221
222	if (ap_qci_info->apxa) {
223		if (ap_qci_info->Na) {
224			ap_max_adapter_id = ap_qci_info->Na;
225			AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
226				    __func__, ap_max_adapter_id);
227		}
228		if (ap_qci_info->Nd) {
229			ap_max_domain_id = ap_qci_info->Nd;
230			AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
231				    __func__, ap_max_domain_id);
232		}
233	}
234}
235
236/*
237 * ap_test_config(): helper function to extract the nrth bit
238 *		     within the unsigned int array field.
239 */
240static inline int ap_test_config(unsigned int *field, unsigned int nr)
241{
242	return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
243}
244
245/*
246 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
247 *
248 * Returns 0 if the card is not configured
249 *	   1 if the card is configured or
250 *	     if the configuration information is not available
251 */
252static inline int ap_test_config_card_id(unsigned int id)
253{
254	if (id > ap_max_adapter_id)
255		return 0;
256	if (ap_qci_info)
257		return ap_test_config(ap_qci_info->apm, id);
258	return 1;
259}
260
261/*
262 * ap_test_config_usage_domain(): Test, whether an AP usage domain
263 * is configured.
264 *
265 * Returns 0 if the usage domain is not configured
266 *	   1 if the usage domain is configured or
267 *	     if the configuration information is not available
268 */
269int ap_test_config_usage_domain(unsigned int domain)
270{
271	if (domain > ap_max_domain_id)
272		return 0;
273	if (ap_qci_info)
274		return ap_test_config(ap_qci_info->aqm, domain);
275	return 1;
276}
277EXPORT_SYMBOL(ap_test_config_usage_domain);
278
279/*
280 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
281 * is configured.
282 * @domain AP control domain ID
283 *
284 * Returns 1 if the control domain is configured
285 *	   0 in all other cases
286 */
287int ap_test_config_ctrl_domain(unsigned int domain)
288{
289	if (!ap_qci_info || domain > ap_max_domain_id)
290		return 0;
291	return ap_test_config(ap_qci_info->adm, domain);
292}
293EXPORT_SYMBOL(ap_test_config_ctrl_domain);
294
295/*
296 * ap_queue_info(): Check and get AP queue info.
297 * Returns true if TAPQ succeeded and the info is filled or
298 * false otherwise.
299 */
300static bool ap_queue_info(ap_qid_t qid, int *q_type,
301			  unsigned int *q_fac, int *q_depth, bool *q_decfg)
302{
303	struct ap_queue_status status;
304	unsigned long info = 0;
305
306	/* make sure we don't run into a specifiation exception */
307	if (AP_QID_CARD(qid) > ap_max_adapter_id ||
308	    AP_QID_QUEUE(qid) > ap_max_domain_id)
309		return false;
310
311	/* call TAPQ on this APQN */
312	status = ap_test_queue(qid, ap_apft_available(), &info);
313	switch (status.response_code) {
314	case AP_RESPONSE_NORMAL:
315	case AP_RESPONSE_RESET_IN_PROGRESS:
316	case AP_RESPONSE_DECONFIGURED:
317	case AP_RESPONSE_CHECKSTOPPED:
318	case AP_RESPONSE_BUSY:
319		/*
320		 * According to the architecture in all these cases the
321		 * info should be filled. All bits 0 is not possible as
322		 * there is at least one of the mode bits set.
323		 */
324		if (WARN_ON_ONCE(!info))
325			return false;
326		*q_type = (int)((info >> 24) & 0xff);
327		*q_fac = (unsigned int)(info >> 32);
328		*q_depth = (int)(info & 0xff);
329		*q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
330		switch (*q_type) {
331			/* For CEX2 and CEX3 the available functions
332			 * are not reflected by the facilities bits.
333			 * Instead it is coded into the type. So here
334			 * modify the function bits based on the type.
335			 */
336		case AP_DEVICE_TYPE_CEX2A:
337		case AP_DEVICE_TYPE_CEX3A:
338			*q_fac |= 0x08000000;
339			break;
340		case AP_DEVICE_TYPE_CEX2C:
341		case AP_DEVICE_TYPE_CEX3C:
342			*q_fac |= 0x10000000;
343			break;
344		default:
345			break;
346		}
347		return true;
348	default:
349		/*
350		 * A response code which indicates, there is no info available.
351		 */
352		return false;
353	}
354}
355
356void ap_wait(enum ap_sm_wait wait)
357{
358	ktime_t hr_time;
359
360	switch (wait) {
361	case AP_SM_WAIT_AGAIN:
362	case AP_SM_WAIT_INTERRUPT:
363		if (ap_irq_flag)
364			break;
365		if (ap_poll_kthread) {
366			wake_up(&ap_poll_wait);
367			break;
368		}
369		fallthrough;
370	case AP_SM_WAIT_TIMEOUT:
371		spin_lock_bh(&ap_poll_timer_lock);
372		if (!hrtimer_is_queued(&ap_poll_timer)) {
373			hr_time = poll_timeout;
374			hrtimer_forward_now(&ap_poll_timer, hr_time);
375			hrtimer_restart(&ap_poll_timer);
376		}
377		spin_unlock_bh(&ap_poll_timer_lock);
378		break;
379	case AP_SM_WAIT_NONE:
380	default:
381		break;
382	}
383}
384
385/**
386 * ap_request_timeout(): Handling of request timeouts
387 * @t: timer making this callback
388 *
389 * Handles request timeouts.
390 */
391void ap_request_timeout(struct timer_list *t)
392{
393	struct ap_queue *aq = from_timer(aq, t, timeout);
394
395	spin_lock_bh(&aq->lock);
396	ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
397	spin_unlock_bh(&aq->lock);
398}
399
400/**
401 * ap_poll_timeout(): AP receive polling for finished AP requests.
402 * @unused: Unused pointer.
403 *
404 * Schedules the AP tasklet using a high resolution timer.
405 */
406static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
407{
408	tasklet_schedule(&ap_tasklet);
409	return HRTIMER_NORESTART;
410}
411
412/**
413 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
414 * @airq: pointer to adapter interrupt descriptor
415 */
416static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
417{
418	inc_irq_stat(IRQIO_APB);
419	tasklet_schedule(&ap_tasklet);
420}
421
422/**
423 * ap_tasklet_fn(): Tasklet to poll all AP devices.
424 * @dummy: Unused variable
425 *
426 * Poll all AP devices on the bus.
427 */
428static void ap_tasklet_fn(unsigned long dummy)
429{
430	int bkt;
431	struct ap_queue *aq;
432	enum ap_sm_wait wait = AP_SM_WAIT_NONE;
433
434	/* Reset the indicator if interrupts are used. Thus new interrupts can
435	 * be received. Doing it in the beginning of the tasklet is therefor
436	 * important that no requests on any AP get lost.
437	 */
438	if (ap_irq_flag)
439		xchg(ap_airq.lsi_ptr, 0);
440
441	spin_lock_bh(&ap_queues_lock);
442	hash_for_each(ap_queues, bkt, aq, hnode) {
443		spin_lock_bh(&aq->lock);
444		wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
445		spin_unlock_bh(&aq->lock);
446	}
447	spin_unlock_bh(&ap_queues_lock);
448
449	ap_wait(wait);
450}
451
452static int ap_pending_requests(void)
453{
454	int bkt;
455	struct ap_queue *aq;
456
457	spin_lock_bh(&ap_queues_lock);
458	hash_for_each(ap_queues, bkt, aq, hnode) {
459		if (aq->queue_count == 0)
460			continue;
461		spin_unlock_bh(&ap_queues_lock);
462		return 1;
463	}
464	spin_unlock_bh(&ap_queues_lock);
465	return 0;
466}
467
468/**
469 * ap_poll_thread(): Thread that polls for finished requests.
470 * @data: Unused pointer
471 *
472 * AP bus poll thread. The purpose of this thread is to poll for
473 * finished requests in a loop if there is a "free" cpu - that is
474 * a cpu that doesn't have anything better to do. The polling stops
475 * as soon as there is another task or if all messages have been
476 * delivered.
477 */
478static int ap_poll_thread(void *data)
479{
480	DECLARE_WAITQUEUE(wait, current);
481
482	set_user_nice(current, MAX_NICE);
483	set_freezable();
484	while (!kthread_should_stop()) {
485		add_wait_queue(&ap_poll_wait, &wait);
486		set_current_state(TASK_INTERRUPTIBLE);
487		if (!ap_pending_requests()) {
488			schedule();
489			try_to_freeze();
490		}
491		set_current_state(TASK_RUNNING);
492		remove_wait_queue(&ap_poll_wait, &wait);
493		if (need_resched()) {
494			schedule();
495			try_to_freeze();
496			continue;
497		}
498		ap_tasklet_fn(0);
499	}
500
501	return 0;
502}
503
504static int ap_poll_thread_start(void)
505{
506	int rc;
507
508	if (ap_irq_flag || ap_poll_kthread)
509		return 0;
510	mutex_lock(&ap_poll_thread_mutex);
511	ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
512	rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
513	if (rc)
514		ap_poll_kthread = NULL;
515	mutex_unlock(&ap_poll_thread_mutex);
516	return rc;
517}
518
519static void ap_poll_thread_stop(void)
520{
521	if (!ap_poll_kthread)
522		return;
523	mutex_lock(&ap_poll_thread_mutex);
524	kthread_stop(ap_poll_kthread);
525	ap_poll_kthread = NULL;
526	mutex_unlock(&ap_poll_thread_mutex);
527}
528
529#define is_card_dev(x) ((x)->parent == ap_root_device)
530#define is_queue_dev(x) ((x)->parent != ap_root_device)
531
532/**
533 * ap_bus_match()
534 * @dev: Pointer to device
535 * @drv: Pointer to device_driver
536 *
537 * AP bus driver registration/unregistration.
538 */
539static int ap_bus_match(struct device *dev, struct device_driver *drv)
540{
541	struct ap_driver *ap_drv = to_ap_drv(drv);
542	struct ap_device_id *id;
543
544	/*
545	 * Compare device type of the device with the list of
546	 * supported types of the device_driver.
547	 */
548	for (id = ap_drv->ids; id->match_flags; id++) {
549		if (is_card_dev(dev) &&
550		    id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
551		    id->dev_type == to_ap_dev(dev)->device_type)
552			return 1;
553		if (is_queue_dev(dev) &&
554		    id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
555		    id->dev_type == to_ap_dev(dev)->device_type)
556			return 1;
557	}
558	return 0;
559}
560
561/**
562 * ap_uevent(): Uevent function for AP devices.
563 * @dev: Pointer to device
564 * @env: Pointer to kobj_uevent_env
565 *
566 * It sets up a single environment variable DEV_TYPE which contains the
567 * hardware device type.
568 */
569static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
570{
571	struct ap_device *ap_dev = to_ap_dev(dev);
572	int retval = 0;
573
574	if (!ap_dev)
575		return -ENODEV;
576
577	/* Set up DEV_TYPE environment variable. */
578	retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
579	if (retval)
580		return retval;
581
582	/* Add MODALIAS= */
583	retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
584
585	return retval;
586}
587
588static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
589{
590	if (is_queue_dev(dev) &&
591	    AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
592		device_unregister(dev);
593	return 0;
594}
595
596static struct bus_type ap_bus_type = {
597	.name = "ap",
598	.match = &ap_bus_match,
599	.uevent = &ap_uevent,
600};
601
602static int __ap_revise_reserved(struct device *dev, void *dummy)
603{
604	int rc, card, queue, devres, drvres;
605
606	if (is_queue_dev(dev)) {
607		card = AP_QID_CARD(to_ap_queue(dev)->qid);
608		queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
609		mutex_lock(&ap_perms_mutex);
610		devres = test_bit_inv(card, ap_perms.apm)
611			&& test_bit_inv(queue, ap_perms.aqm);
612		mutex_unlock(&ap_perms_mutex);
613		drvres = to_ap_drv(dev->driver)->flags
614			& AP_DRIVER_FLAG_DEFAULT;
615		if (!!devres != !!drvres) {
616			AP_DBF_DBG("reprobing queue=%02x.%04x\n",
617				   card, queue);
618			rc = device_reprobe(dev);
619		}
620	}
621
622	return 0;
623}
624
625static void ap_bus_revise_bindings(void)
626{
627	bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
628}
629
630int ap_owned_by_def_drv(int card, int queue)
631{
632	int rc = 0;
633
634	if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
635		return -EINVAL;
636
637	mutex_lock(&ap_perms_mutex);
638
639	if (test_bit_inv(card, ap_perms.apm)
640	    && test_bit_inv(queue, ap_perms.aqm))
641		rc = 1;
642
643	mutex_unlock(&ap_perms_mutex);
644
645	return rc;
646}
647EXPORT_SYMBOL(ap_owned_by_def_drv);
648
649int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
650				       unsigned long *aqm)
651{
652	int card, queue, rc = 0;
653
654	mutex_lock(&ap_perms_mutex);
655
656	for (card = 0; !rc && card < AP_DEVICES; card++)
657		if (test_bit_inv(card, apm) &&
658		    test_bit_inv(card, ap_perms.apm))
659			for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
660				if (test_bit_inv(queue, aqm) &&
661				    test_bit_inv(queue, ap_perms.aqm))
662					rc = 1;
663
664	mutex_unlock(&ap_perms_mutex);
665
666	return rc;
667}
668EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
669
670static int ap_device_probe(struct device *dev)
671{
672	struct ap_device *ap_dev = to_ap_dev(dev);
673	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
674	int card, queue, devres, drvres, rc = -ENODEV;
675
676	if (!get_device(dev))
677		return rc;
678
679	if (is_queue_dev(dev)) {
680		/*
681		 * If the apqn is marked as reserved/used by ap bus and
682		 * default drivers, only probe with drivers with the default
683		 * flag set. If it is not marked, only probe with drivers
684		 * with the default flag not set.
685		 */
686		card = AP_QID_CARD(to_ap_queue(dev)->qid);
687		queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
688		mutex_lock(&ap_perms_mutex);
689		devres = test_bit_inv(card, ap_perms.apm)
690			&& test_bit_inv(queue, ap_perms.aqm);
691		mutex_unlock(&ap_perms_mutex);
692		drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
693		if (!!devres != !!drvres)
694			goto out;
695	}
696
697	/* Add queue/card to list of active queues/cards */
698	spin_lock_bh(&ap_queues_lock);
699	if (is_queue_dev(dev))
700		hash_add(ap_queues, &to_ap_queue(dev)->hnode,
701			 to_ap_queue(dev)->qid);
702	spin_unlock_bh(&ap_queues_lock);
703
704	ap_dev->drv = ap_drv;
705	rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
706
707	if (rc) {
708		spin_lock_bh(&ap_queues_lock);
709		if (is_queue_dev(dev))
710			hash_del(&to_ap_queue(dev)->hnode);
711		spin_unlock_bh(&ap_queues_lock);
712		ap_dev->drv = NULL;
713	}
714
715out:
716	if (rc)
717		put_device(dev);
718	return rc;
719}
720
721static int ap_device_remove(struct device *dev)
722{
723	struct ap_device *ap_dev = to_ap_dev(dev);
724	struct ap_driver *ap_drv = ap_dev->drv;
725
726	/* prepare ap queue device removal */
727	if (is_queue_dev(dev))
728		ap_queue_prepare_remove(to_ap_queue(dev));
729
730	/* driver's chance to clean up gracefully */
731	if (ap_drv->remove)
732		ap_drv->remove(ap_dev);
733
734	/* now do the ap queue device remove */
735	if (is_queue_dev(dev))
736		ap_queue_remove(to_ap_queue(dev));
737
738	/* Remove queue/card from list of active queues/cards */
739	spin_lock_bh(&ap_queues_lock);
740	if (is_queue_dev(dev))
741		hash_del(&to_ap_queue(dev)->hnode);
742	spin_unlock_bh(&ap_queues_lock);
743
744	put_device(dev);
745
746	return 0;
747}
748
749struct ap_queue *ap_get_qdev(ap_qid_t qid)
750{
751	int bkt;
752	struct ap_queue *aq;
753
754	spin_lock_bh(&ap_queues_lock);
755	hash_for_each(ap_queues, bkt, aq, hnode) {
756		if (aq->qid == qid) {
757			get_device(&aq->ap_dev.device);
758			spin_unlock_bh(&ap_queues_lock);
759			return aq;
760		}
761	}
762	spin_unlock_bh(&ap_queues_lock);
763
764	return NULL;
765}
766EXPORT_SYMBOL(ap_get_qdev);
767
768int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
769		       char *name)
770{
771	struct device_driver *drv = &ap_drv->driver;
772
773	drv->bus = &ap_bus_type;
774	drv->probe = ap_device_probe;
775	drv->remove = ap_device_remove;
776	drv->owner = owner;
777	drv->name = name;
778	return driver_register(drv);
779}
780EXPORT_SYMBOL(ap_driver_register);
781
782void ap_driver_unregister(struct ap_driver *ap_drv)
783{
784	driver_unregister(&ap_drv->driver);
785}
786EXPORT_SYMBOL(ap_driver_unregister);
787
788void ap_bus_force_rescan(void)
789{
790	/* processing a asynchronous bus rescan */
791	del_timer(&ap_config_timer);
792	queue_work(system_long_wq, &ap_scan_work);
793	flush_work(&ap_scan_work);
794}
795EXPORT_SYMBOL(ap_bus_force_rescan);
796
797/*
798* A config change has happened, force an ap bus rescan.
799*/
800void ap_bus_cfg_chg(void)
801{
802	AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
803
804	ap_bus_force_rescan();
805}
806
807/*
808 * hex2bitmap() - parse hex mask string and set bitmap.
809 * Valid strings are "0x012345678" with at least one valid hex number.
810 * Rest of the bitmap to the right is padded with 0. No spaces allowed
811 * within the string, the leading 0x may be omitted.
812 * Returns the bitmask with exactly the bits set as given by the hex
813 * string (both in big endian order).
814 */
815static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
816{
817	int i, n, b;
818
819	/* bits needs to be a multiple of 8 */
820	if (bits & 0x07)
821		return -EINVAL;
822
823	if (str[0] == '0' && str[1] == 'x')
824		str++;
825	if (*str == 'x')
826		str++;
827
828	for (i = 0; isxdigit(*str) && i < bits; str++) {
829		b = hex_to_bin(*str);
830		for (n = 0; n < 4; n++)
831			if (b & (0x08 >> n))
832				set_bit_inv(i + n, bitmap);
833		i += 4;
834	}
835
836	if (*str == '\n')
837		str++;
838	if (*str)
839		return -EINVAL;
840	return 0;
841}
842
843/*
844 * modify_bitmap() - parse bitmask argument and modify an existing
845 * bit mask accordingly. A concatenation (done with ',') of these
846 * terms is recognized:
847 *   +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
848 * <bitnr> may be any valid number (hex, decimal or octal) in the range
849 * 0...bits-1; the leading + or - is required. Here are some examples:
850 *   +0-15,+32,-128,-0xFF
851 *   -0-255,+1-16,+0x128
852 *   +1,+2,+3,+4,-5,-7-10
853 * Returns the new bitmap after all changes have been applied. Every
854 * positive value in the string will set a bit and every negative value
855 * in the string will clear a bit. As a bit may be touched more than once,
856 * the last 'operation' wins:
857 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
858 * cleared again. All other bits are unmodified.
859 */
860static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
861{
862	int a, i, z;
863	char *np, sign;
864
865	/* bits needs to be a multiple of 8 */
866	if (bits & 0x07)
867		return -EINVAL;
868
869	while (*str) {
870		sign = *str++;
871		if (sign != '+' && sign != '-')
872			return -EINVAL;
873		a = z = simple_strtoul(str, &np, 0);
874		if (str == np || a >= bits)
875			return -EINVAL;
876		str = np;
877		if (*str == '-') {
878			z = simple_strtoul(++str, &np, 0);
879			if (str == np || a > z || z >= bits)
880				return -EINVAL;
881			str = np;
882		}
883		for (i = a; i <= z; i++)
884			if (sign == '+')
885				set_bit_inv(i, bitmap);
886			else
887				clear_bit_inv(i, bitmap);
888		while (*str == ',' || *str == '\n')
889			str++;
890	}
891
892	return 0;
893}
894
895int ap_parse_mask_str(const char *str,
896		      unsigned long *bitmap, int bits,
897		      struct mutex *lock)
898{
899	unsigned long *newmap, size;
900	int rc;
901
902	/* bits needs to be a multiple of 8 */
903	if (bits & 0x07)
904		return -EINVAL;
905
906	size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
907	newmap = kmalloc(size, GFP_KERNEL);
908	if (!newmap)
909		return -ENOMEM;
910	if (mutex_lock_interruptible(lock)) {
911		kfree(newmap);
912		return -ERESTARTSYS;
913	}
914
915	if (*str == '+' || *str == '-') {
916		memcpy(newmap, bitmap, size);
917		rc = modify_bitmap(str, newmap, bits);
918	} else {
919		memset(newmap, 0, size);
920		rc = hex2bitmap(str, newmap, bits);
921	}
922	if (rc == 0)
923		memcpy(bitmap, newmap, size);
924	mutex_unlock(lock);
925	kfree(newmap);
926	return rc;
927}
928EXPORT_SYMBOL(ap_parse_mask_str);
929
930/*
931 * AP bus attributes.
932 */
933
934static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
935{
936	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
937}
938
939static ssize_t ap_domain_store(struct bus_type *bus,
940			       const char *buf, size_t count)
941{
942	int domain;
943
944	if (sscanf(buf, "%i\n", &domain) != 1 ||
945	    domain < 0 || domain > ap_max_domain_id ||
946	    !test_bit_inv(domain, ap_perms.aqm))
947		return -EINVAL;
948
949	spin_lock_bh(&ap_domain_lock);
950	ap_domain_index = domain;
951	spin_unlock_bh(&ap_domain_lock);
952
953	AP_DBF_INFO("stored new default domain=%d\n", domain);
954
955	return count;
956}
957
958static BUS_ATTR_RW(ap_domain);
959
960static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
961{
962	if (!ap_qci_info)	/* QCI not supported */
963		return scnprintf(buf, PAGE_SIZE, "not supported\n");
964
965	return scnprintf(buf, PAGE_SIZE,
966			 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
967			 ap_qci_info->adm[0], ap_qci_info->adm[1],
968			 ap_qci_info->adm[2], ap_qci_info->adm[3],
969			 ap_qci_info->adm[4], ap_qci_info->adm[5],
970			 ap_qci_info->adm[6], ap_qci_info->adm[7]);
971}
972
973static BUS_ATTR_RO(ap_control_domain_mask);
974
975static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
976{
977	if (!ap_qci_info)	/* QCI not supported */
978		return scnprintf(buf, PAGE_SIZE, "not supported\n");
979
980	return scnprintf(buf, PAGE_SIZE,
981			 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
982			 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
983			 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
984			 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
985			 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
986}
987
988static BUS_ATTR_RO(ap_usage_domain_mask);
989
990static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
991{
992	if (!ap_qci_info)	/* QCI not supported */
993		return scnprintf(buf, PAGE_SIZE, "not supported\n");
994
995	return scnprintf(buf, PAGE_SIZE,
996			 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
997			 ap_qci_info->apm[0], ap_qci_info->apm[1],
998			 ap_qci_info->apm[2], ap_qci_info->apm[3],
999			 ap_qci_info->apm[4], ap_qci_info->apm[5],
1000			 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1001}
1002
1003static BUS_ATTR_RO(ap_adapter_mask);
1004
1005static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1006{
1007	return scnprintf(buf, PAGE_SIZE, "%d\n",
1008			 ap_irq_flag ? 1 : 0);
1009}
1010
1011static BUS_ATTR_RO(ap_interrupts);
1012
1013static ssize_t config_time_show(struct bus_type *bus, char *buf)
1014{
1015	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1016}
1017
1018static ssize_t config_time_store(struct bus_type *bus,
1019				 const char *buf, size_t count)
1020{
1021	int time;
1022
1023	if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1024		return -EINVAL;
1025	ap_config_time = time;
1026	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1027	return count;
1028}
1029
1030static BUS_ATTR_RW(config_time);
1031
1032static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1033{
1034	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1035}
1036
1037static ssize_t poll_thread_store(struct bus_type *bus,
1038				 const char *buf, size_t count)
1039{
1040	int flag, rc;
1041
1042	if (sscanf(buf, "%d\n", &flag) != 1)
1043		return -EINVAL;
1044	if (flag) {
1045		rc = ap_poll_thread_start();
1046		if (rc)
1047			count = rc;
1048	} else
1049		ap_poll_thread_stop();
1050	return count;
1051}
1052
1053static BUS_ATTR_RW(poll_thread);
1054
1055static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1056{
1057	return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1058}
1059
1060static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1061				  size_t count)
1062{
1063	unsigned long long time;
1064	ktime_t hr_time;
1065
1066	/* 120 seconds = maximum poll interval */
1067	if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1068	    time > 120000000000ULL)
1069		return -EINVAL;
1070	poll_timeout = time;
1071	hr_time = poll_timeout;
1072
1073	spin_lock_bh(&ap_poll_timer_lock);
1074	hrtimer_cancel(&ap_poll_timer);
1075	hrtimer_set_expires(&ap_poll_timer, hr_time);
1076	hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1077	spin_unlock_bh(&ap_poll_timer_lock);
1078
1079	return count;
1080}
1081
1082static BUS_ATTR_RW(poll_timeout);
1083
1084static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1085{
1086	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1087}
1088
1089static BUS_ATTR_RO(ap_max_domain_id);
1090
1091static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1092{
1093	return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1094}
1095
1096static BUS_ATTR_RO(ap_max_adapter_id);
1097
1098static ssize_t apmask_show(struct bus_type *bus, char *buf)
1099{
1100	int rc;
1101
1102	if (mutex_lock_interruptible(&ap_perms_mutex))
1103		return -ERESTARTSYS;
1104	rc = scnprintf(buf, PAGE_SIZE,
1105		       "0x%016lx%016lx%016lx%016lx\n",
1106		       ap_perms.apm[0], ap_perms.apm[1],
1107		       ap_perms.apm[2], ap_perms.apm[3]);
1108	mutex_unlock(&ap_perms_mutex);
1109
1110	return rc;
1111}
1112
1113static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1114			    size_t count)
1115{
1116	int rc;
1117
1118	rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1119	if (rc)
1120		return rc;
1121
1122	ap_bus_revise_bindings();
1123
1124	return count;
1125}
1126
1127static BUS_ATTR_RW(apmask);
1128
1129static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1130{
1131	int rc;
1132
1133	if (mutex_lock_interruptible(&ap_perms_mutex))
1134		return -ERESTARTSYS;
1135	rc = scnprintf(buf, PAGE_SIZE,
1136		       "0x%016lx%016lx%016lx%016lx\n",
1137		       ap_perms.aqm[0], ap_perms.aqm[1],
1138		       ap_perms.aqm[2], ap_perms.aqm[3]);
1139	mutex_unlock(&ap_perms_mutex);
1140
1141	return rc;
1142}
1143
1144static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1145			    size_t count)
1146{
1147	int rc;
1148
1149	rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1150	if (rc)
1151		return rc;
1152
1153	ap_bus_revise_bindings();
1154
1155	return count;
1156}
1157
1158static BUS_ATTR_RW(aqmask);
1159
1160static struct bus_attribute *const ap_bus_attrs[] = {
1161	&bus_attr_ap_domain,
1162	&bus_attr_ap_control_domain_mask,
1163	&bus_attr_ap_usage_domain_mask,
1164	&bus_attr_ap_adapter_mask,
1165	&bus_attr_config_time,
1166	&bus_attr_poll_thread,
1167	&bus_attr_ap_interrupts,
1168	&bus_attr_poll_timeout,
1169	&bus_attr_ap_max_domain_id,
1170	&bus_attr_ap_max_adapter_id,
1171	&bus_attr_apmask,
1172	&bus_attr_aqmask,
1173	NULL,
1174};
1175
1176/**
1177 * ap_select_domain(): Select an AP domain if possible and we haven't
1178 * already done so before.
1179 */
1180static void ap_select_domain(void)
1181{
1182	struct ap_queue_status status;
1183	int card, dom;
1184
1185	/*
1186	 * Choose the default domain. Either the one specified with
1187	 * the "domain=" parameter or the first domain with at least
1188	 * one valid APQN.
1189	 */
1190	spin_lock_bh(&ap_domain_lock);
1191	if (ap_domain_index >= 0) {
1192		/* Domain has already been selected. */
1193		goto out;
1194	}
1195	for (dom = 0; dom <= ap_max_domain_id; dom++) {
1196		if (!ap_test_config_usage_domain(dom) ||
1197		    !test_bit_inv(dom, ap_perms.aqm))
1198			continue;
1199		for (card = 0; card <= ap_max_adapter_id; card++) {
1200			if (!ap_test_config_card_id(card) ||
1201			    !test_bit_inv(card, ap_perms.apm))
1202				continue;
1203			status = ap_test_queue(AP_MKQID(card, dom),
1204					       ap_apft_available(),
1205					       NULL);
1206			if (status.response_code == AP_RESPONSE_NORMAL)
1207				break;
1208		}
1209		if (card <= ap_max_adapter_id)
1210			break;
1211	}
1212	if (dom <= ap_max_domain_id) {
1213		ap_domain_index = dom;
1214		AP_DBF_INFO("%s new default domain is %d\n",
1215			    __func__, ap_domain_index);
1216	}
1217out:
1218	spin_unlock_bh(&ap_domain_lock);
1219}
1220
1221/*
1222 * This function checks the type and returns either 0 for not
1223 * supported or the highest compatible type value (which may
1224 * include the input type value).
1225 */
1226static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1227{
1228	int comp_type = 0;
1229
1230	/* < CEX2A is not supported */
1231	if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1232		AP_DBF_WARN("get_comp_type queue=%02x.%04x unsupported type %d\n",
1233			    AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1234		return 0;
1235	}
1236	/* up to CEX7 known and fully supported */
1237	if (rawtype <= AP_DEVICE_TYPE_CEX7)
1238		return rawtype;
1239	/*
1240	 * unknown new type > CEX7, check for compatibility
1241	 * to the highest known and supported type which is
1242	 * currently CEX7 with the help of the QACT function.
1243	 */
1244	if (ap_qact_available()) {
1245		struct ap_queue_status status;
1246		union ap_qact_ap_info apinfo = {0};
1247
1248		apinfo.mode = (func >> 26) & 0x07;
1249		apinfo.cat = AP_DEVICE_TYPE_CEX7;
1250		status = ap_qact(qid, 0, &apinfo);
1251		if (status.response_code == AP_RESPONSE_NORMAL
1252		    && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1253		    && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
1254			comp_type = apinfo.cat;
1255	}
1256	if (!comp_type)
1257		AP_DBF_WARN("get_comp_type queue=%02x.%04x unable to map type %d\n",
1258			    AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1259	else if (comp_type != rawtype)
1260		AP_DBF_INFO("get_comp_type queue=%02x.%04x map type %d to %d\n",
1261			    AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1262			    rawtype, comp_type);
1263	return comp_type;
1264}
1265
1266/*
1267 * Helper function to be used with bus_find_dev
1268 * matches for the card device with the given id
1269 */
1270static int __match_card_device_with_id(struct device *dev, const void *data)
1271{
1272	return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1273}
1274
1275/*
1276 * Helper function to be used with bus_find_dev
1277 * matches for the queue device with a given qid
1278 */
1279static int __match_queue_device_with_qid(struct device *dev, const void *data)
1280{
1281	return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1282}
1283
1284/*
1285 * Helper function to be used with bus_find_dev
1286 * matches any queue device with given queue id
1287 */
1288static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1289{
1290	return is_queue_dev(dev)
1291		&& AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1292}
1293
1294/*
1295 * Helper function for ap_scan_bus().
1296 * Remove card device and associated queue devices.
1297 */
1298static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1299{
1300	bus_for_each_dev(&ap_bus_type, NULL,
1301			 (void *)(long) ac->id,
1302			 __ap_queue_devices_with_id_unregister);
1303	device_unregister(&ac->ap_dev.device);
1304}
1305
1306/*
1307 * Helper function for ap_scan_bus().
1308 * Does the scan bus job for all the domains within
1309 * a valid adapter given by an ap_card ptr.
1310 */
1311static inline void ap_scan_domains(struct ap_card *ac)
1312{
1313	bool decfg;
1314	ap_qid_t qid;
1315	unsigned int func;
1316	struct device *dev;
1317	struct ap_queue *aq;
1318	int rc, dom, depth, type;
1319
1320	/*
1321	 * Go through the configuration for the domains and compare them
1322	 * to the existing queue devices. Also take care of the config
1323	 * and error state for the queue devices.
1324	 */
1325
1326	for (dom = 0; dom <= ap_max_domain_id; dom++) {
1327		qid = AP_MKQID(ac->id, dom);
1328		dev = bus_find_device(&ap_bus_type, NULL,
1329				      (void *)(long) qid,
1330				      __match_queue_device_with_qid);
1331		aq = dev ? to_ap_queue(dev) : NULL;
1332		if (!ap_test_config_usage_domain(dom)) {
1333			if (dev) {
1334				AP_DBF_INFO("%s(%d,%d) not in config any more, rm queue device\n",
1335					    __func__, ac->id, dom);
1336				device_unregister(dev);
1337				put_device(dev);
1338			}
1339			continue;
1340		}
1341		/* domain is valid, get info from this APQN */
1342		if (!ap_queue_info(qid, &type, &func, &depth, &decfg)) {
1343			if (aq) {
1344				AP_DBF_INFO(
1345					"%s(%d,%d) ap_queue_info() not successful, rm queue device\n",
1346					__func__, ac->id, dom);
1347				device_unregister(dev);
1348				put_device(dev);
1349			}
1350			continue;
1351		}
1352		/* if no queue device exists, create a new one */
1353		if (!aq) {
1354			aq = ap_queue_create(qid, ac->ap_dev.device_type);
1355			if (!aq) {
1356				AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1357					    __func__, ac->id, dom);
1358				continue;
1359			}
1360			aq->card = ac;
1361			aq->config = !decfg;
1362			dev = &aq->ap_dev.device;
1363			dev->bus = &ap_bus_type;
1364			dev->parent = &ac->ap_dev.device;
1365			dev_set_name(dev, "%02x.%04x", ac->id, dom);
1366			/* register queue device */
1367			rc = device_register(dev);
1368			if (rc) {
1369				AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1370					    __func__, ac->id, dom);
1371				goto put_dev_and_continue;
1372			}
1373			/* get it and thus adjust reference counter */
1374			get_device(dev);
1375			if (decfg)
1376				AP_DBF_INFO("%s(%d,%d) new (decfg) queue device created\n",
1377					    __func__, ac->id, dom);
1378			else
1379				AP_DBF_INFO("%s(%d,%d) new queue device created\n",
1380					    __func__, ac->id, dom);
1381			goto put_dev_and_continue;
1382		}
1383		/* Check config state on the already existing queue device */
1384		spin_lock_bh(&aq->lock);
1385		if (decfg && aq->config) {
1386			/* config off this queue device */
1387			aq->config = false;
1388			if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1389				aq->dev_state = AP_DEV_STATE_ERROR;
1390				aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1391			}
1392			spin_unlock_bh(&aq->lock);
1393			AP_DBF_INFO("%s(%d,%d) queue device config off\n",
1394				    __func__, ac->id, dom);
1395			/* 'receive' pending messages with -EAGAIN */
1396			ap_flush_queue(aq);
1397			goto put_dev_and_continue;
1398		}
1399		if (!decfg && !aq->config) {
1400			/* config on this queue device */
1401			aq->config = true;
1402			if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1403				aq->dev_state = AP_DEV_STATE_OPERATING;
1404				aq->sm_state = AP_SM_STATE_RESET_START;
1405			}
1406			spin_unlock_bh(&aq->lock);
1407			AP_DBF_INFO("%s(%d,%d) queue device config on\n",
1408				    __func__, ac->id, dom);
1409			goto put_dev_and_continue;
1410		}
1411		/* handle other error states */
1412		if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1413			spin_unlock_bh(&aq->lock);
1414			/* 'receive' pending messages with -EAGAIN */
1415			ap_flush_queue(aq);
1416			/* re-init (with reset) the queue device */
1417			ap_queue_init_state(aq);
1418			AP_DBF_INFO("%s(%d,%d) queue device reinit enforced\n",
1419				    __func__, ac->id, dom);
1420			goto put_dev_and_continue;
1421		}
1422		spin_unlock_bh(&aq->lock);
1423put_dev_and_continue:
1424		put_device(dev);
1425	}
1426}
1427
1428/*
1429 * Helper function for ap_scan_bus().
1430 * Does the scan bus job for the given adapter id.
1431 */
1432static inline void ap_scan_adapter(int ap)
1433{
1434	bool decfg;
1435	ap_qid_t qid;
1436	unsigned int func;
1437	struct device *dev;
1438	struct ap_card *ac;
1439	int rc, dom, depth, type, comp_type;
1440
1441	/* Is there currently a card device for this adapter ? */
1442	dev = bus_find_device(&ap_bus_type, NULL,
1443			      (void *)(long) ap,
1444			      __match_card_device_with_id);
1445	ac = dev ? to_ap_card(dev) : NULL;
1446
1447	/* Adapter not in configuration ? */
1448	if (!ap_test_config_card_id(ap)) {
1449		if (ac) {
1450			AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devices\n",
1451				    __func__, ap);
1452			ap_scan_rm_card_dev_and_queue_devs(ac);
1453			put_device(dev);
1454		}
1455		return;
1456	}
1457
1458	/*
1459	 * Adapter ap is valid in the current configuration. So do some checks:
1460	 * If no card device exists, build one. If a card device exists, check
1461	 * for type and functions changed. For all this we need to find a valid
1462	 * APQN first.
1463	 */
1464
1465	for (dom = 0; dom <= ap_max_domain_id; dom++)
1466		if (ap_test_config_usage_domain(dom)) {
1467			qid = AP_MKQID(ap, dom);
1468			if (ap_queue_info(qid, &type, &func, &depth, &decfg))
1469				break;
1470		}
1471	if (dom > ap_max_domain_id) {
1472		/* Could not find a valid APQN for this adapter */
1473		if (ac) {
1474			AP_DBF_INFO(
1475				"%s(%d) no type info (no APQN found), rm card and queue devices\n",
1476				__func__, ap);
1477			ap_scan_rm_card_dev_and_queue_devs(ac);
1478			put_device(dev);
1479		} else {
1480			AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1481				   __func__, ap);
1482		}
1483		return;
1484	}
1485	if (!type) {
1486		/* No apdater type info available, an unusable adapter */
1487		if (ac) {
1488			AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devices\n",
1489				    __func__, ap);
1490			ap_scan_rm_card_dev_and_queue_devs(ac);
1491			put_device(dev);
1492		} else {
1493			AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1494				   __func__, ap);
1495		}
1496		return;
1497	}
1498
1499	if (ac) {
1500		/* Check APQN against existing card device for changes */
1501		if (ac->raw_hwtype != type) {
1502			AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devices\n",
1503				    __func__, ap, type);
1504			ap_scan_rm_card_dev_and_queue_devs(ac);
1505			put_device(dev);
1506			ac = NULL;
1507		} else if (ac->functions != func) {
1508			AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devices\n",
1509				    __func__, ap, type);
1510			ap_scan_rm_card_dev_and_queue_devs(ac);
1511			put_device(dev);
1512			ac = NULL;
1513		} else {
1514			if (decfg && ac->config) {
1515				ac->config = false;
1516				AP_DBF_INFO("%s(%d) card device config off\n",
1517					    __func__, ap);
1518
1519			}
1520			if (!decfg && !ac->config) {
1521				ac->config = true;
1522				AP_DBF_INFO("%s(%d) card device config on\n",
1523					    __func__, ap);
1524			}
1525		}
1526	}
1527
1528	if (!ac) {
1529		/* Build a new card device */
1530		comp_type = ap_get_compatible_type(qid, type, func);
1531		if (!comp_type) {
1532			AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
1533				    __func__, ap, type);
1534			return;
1535		}
1536		ac = ap_card_create(ap, depth, type, comp_type, func);
1537		if (!ac) {
1538			AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
1539				    __func__, ap);
1540			return;
1541		}
1542		ac->config = !decfg;
1543		dev = &ac->ap_dev.device;
1544		dev->bus = &ap_bus_type;
1545		dev->parent = ap_root_device;
1546		dev_set_name(dev, "card%02x", ap);
1547		/* Register the new card device with AP bus */
1548		rc = device_register(dev);
1549		if (rc) {
1550			AP_DBF_WARN("%s(%d) device_register() failed\n",
1551				    __func__, ap);
1552			put_device(dev);
1553			return;
1554		}
1555		/* get it and thus adjust reference counter */
1556		get_device(dev);
1557		if (decfg)
1558			AP_DBF_INFO("%s(%d) new (decfg) card device type=%d func=0x%08x created\n",
1559				    __func__, ap, type, func);
1560		else
1561			AP_DBF_INFO("%s(%d) new card device type=%d func=0x%08x created\n",
1562				    __func__, ap, type, func);
1563	}
1564
1565	/* Verify the domains and the queue devices for this card */
1566	ap_scan_domains(ac);
1567
1568	/* release the card device */
1569	put_device(&ac->ap_dev.device);
1570}
1571
1572/**
1573 * ap_scan_bus(): Scan the AP bus for new devices
1574 * Runs periodically, workqueue timer (ap_config_time)
1575 */
1576static void ap_scan_bus(struct work_struct *unused)
1577{
1578	int ap;
1579
1580	ap_fetch_qci_info(ap_qci_info);
1581	ap_select_domain();
1582
1583	AP_DBF_DBG("%s running\n", __func__);
1584
1585	/* loop over all possible adapters */
1586	for (ap = 0; ap <= ap_max_adapter_id; ap++)
1587		ap_scan_adapter(ap);
1588
1589	/* check if there is at least one queue available with default domain */
1590	if (ap_domain_index >= 0) {
1591		struct device *dev =
1592			bus_find_device(&ap_bus_type, NULL,
1593					(void *)(long) ap_domain_index,
1594					__match_queue_device_with_queue_id);
1595		if (dev)
1596			put_device(dev);
1597		else
1598			AP_DBF_INFO("no queue device with default domain %d available\n",
1599				    ap_domain_index);
1600	}
1601
1602	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1603}
1604
1605static void ap_config_timeout(struct timer_list *unused)
1606{
1607	queue_work(system_long_wq, &ap_scan_work);
1608}
1609
1610static int __init ap_debug_init(void)
1611{
1612	ap_dbf_info = debug_register("ap", 1, 1,
1613				     DBF_MAX_SPRINTF_ARGS * sizeof(long));
1614	debug_register_view(ap_dbf_info, &debug_sprintf_view);
1615	debug_set_level(ap_dbf_info, DBF_ERR);
1616
1617	return 0;
1618}
1619
1620static void __init ap_perms_init(void)
1621{
1622	/* all resources useable if no kernel parameter string given */
1623	memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1624	memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1625	memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1626
1627	/* apm kernel parameter string */
1628	if (apm_str) {
1629		memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1630		ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1631				  &ap_perms_mutex);
1632	}
1633
1634	/* aqm kernel parameter string */
1635	if (aqm_str) {
1636		memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1637		ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1638				  &ap_perms_mutex);
1639	}
1640}
1641
1642/**
1643 * ap_module_init(): The module initialization code.
1644 *
1645 * Initializes the module.
1646 */
1647static int __init ap_module_init(void)
1648{
1649	int rc, i;
1650
1651	rc = ap_debug_init();
1652	if (rc)
1653		return rc;
1654
1655	if (!ap_instructions_available()) {
1656		pr_warn("The hardware system does not support AP instructions\n");
1657		return -ENODEV;
1658	}
1659
1660	/* init ap_queue hashtable */
1661	hash_init(ap_queues);
1662
1663	/* set up the AP permissions (ioctls, ap and aq masks) */
1664	ap_perms_init();
1665
1666	/* Get AP configuration data if available */
1667	ap_init_qci_info();
1668
1669	/* check default domain setting */
1670	if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
1671	    (ap_domain_index >= 0 &&
1672	     !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1673		pr_warn("%d is not a valid cryptographic domain\n",
1674			ap_domain_index);
1675		ap_domain_index = -1;
1676	}
1677
1678	/* enable interrupts if available */
1679	if (ap_interrupts_available()) {
1680		rc = register_adapter_interrupt(&ap_airq);
1681		ap_irq_flag = (rc == 0);
1682	}
1683
1684	/* Create /sys/bus/ap. */
1685	rc = bus_register(&ap_bus_type);
1686	if (rc)
1687		goto out;
1688	for (i = 0; ap_bus_attrs[i]; i++) {
1689		rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1690		if (rc)
1691			goto out_bus;
1692	}
1693
1694	/* Create /sys/devices/ap. */
1695	ap_root_device = root_device_register("ap");
1696	rc = PTR_ERR_OR_ZERO(ap_root_device);
1697	if (rc)
1698		goto out_bus;
1699
1700	/* Setup the AP bus rescan timer. */
1701	timer_setup(&ap_config_timer, ap_config_timeout, 0);
1702
1703	/*
1704	 * Setup the high resultion poll timer.
1705	 * If we are running under z/VM adjust polling to z/VM polling rate.
1706	 */
1707	if (MACHINE_IS_VM)
1708		poll_timeout = 1500000;
1709	hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1710	ap_poll_timer.function = ap_poll_timeout;
1711
1712	/* Start the low priority AP bus poll thread. */
1713	if (ap_thread_flag) {
1714		rc = ap_poll_thread_start();
1715		if (rc)
1716			goto out_work;
1717	}
1718
1719	queue_work(system_long_wq, &ap_scan_work);
1720
1721	return 0;
1722
1723out_work:
1724	hrtimer_cancel(&ap_poll_timer);
1725	root_device_unregister(ap_root_device);
1726out_bus:
1727	while (i--)
1728		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1729	bus_unregister(&ap_bus_type);
1730out:
1731	if (ap_irq_flag)
1732		unregister_adapter_interrupt(&ap_airq);
1733	kfree(ap_qci_info);
1734	return rc;
1735}
1736device_initcall(ap_module_init);
1737