ap_bus.c 39.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2
/*
3
 * Copyright IBM Corp. 2006, 2012
4 5 6
 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
 *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
 *	      Ralph Wuerthner <rwuerthn@de.ibm.com>
F
Felix Beck 已提交
7
 *	      Felix Beck <felix.beck@de.ibm.com>
8
 *	      Holger Dengler <hd@linux.vnet.ibm.com>
9 10 11 12
 *
 * Adjunct processor bus.
 */

13 14 15
#define KMSG_COMPONENT "ap"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

16
#include <linux/kernel_stat.h>
17
#include <linux/moduleparam.h>
18 19 20 21 22
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
23
#include <linux/slab.h>
24 25 26
#include <linux/notifier.h>
#include <linux/kthread.h>
#include <linux/mutex.h>
27
#include <linux/suspend.h>
F
Felix Beck 已提交
28
#include <asm/airq.h>
A
Arun Sharma 已提交
29
#include <linux/atomic.h>
F
Felix Beck 已提交
30
#include <asm/isc.h>
31 32
#include <linux/hrtimer.h>
#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 45
 */
int ap_domain_index = -1;	/* Adjunct Processor Domain Index */
46
static DEFINE_SPINLOCK(ap_domain_lock);
47
module_param_named(domain, ap_domain_index, int, 0440);
48 49 50
MODULE_PARM_DESC(domain, "domain index for ap devices");
EXPORT_SYMBOL(ap_domain_index);

51 52
static int ap_thread_flag;
module_param_named(poll_thread, ap_thread_flag, int, 0440);
53
MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
54

55 56 57 58 59 60 61 62
static char *apm_str;
module_param_named(apmask, apm_str, charp, 0440);
MODULE_PARM_DESC(apmask, "AP bus adapter mask.");

static char *aqm_str;
module_param_named(aqmask, aqm_str, charp, 0440);
MODULE_PARM_DESC(aqmask, "AP bus domain mask.");

63 64 65 66 67
static struct device *ap_root_device;

DEFINE_SPINLOCK(ap_list_lock);
LIST_HEAD(ap_card_list);

68 69 70 71 72
/* Default permissions (ioctl, card and domain masking) */
struct ap_perms ap_perms;
EXPORT_SYMBOL(ap_perms);
DEFINE_MUTEX(ap_perms_mutex);
EXPORT_SYMBOL(ap_perms_mutex);
73

74
static struct ap_config_info *ap_configuration;
75
static bool initialised;
76

77 78 79 80 81
/*
 * AP bus related debug feature things.
 */
debug_info_t *ap_dbf_info;

82
/*
83
 * Workqueue timer for bus rescan.
84 85 86
 */
static struct timer_list ap_config_timer;
static int ap_config_time = AP_CONFIG_TIME;
87
static void ap_scan_bus(struct work_struct *);
88
static DECLARE_WORK(ap_scan_work, ap_scan_bus);
89

90
/*
F
Felix Beck 已提交
91
 * Tasklet & timer for AP request polling and interrupts
92
 */
93 94
static void ap_tasklet_fn(unsigned long);
static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
95
static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
96
static struct task_struct *ap_poll_kthread;
97
static DEFINE_MUTEX(ap_poll_thread_mutex);
98
static DEFINE_SPINLOCK(ap_poll_timer_lock);
99
static struct hrtimer ap_poll_timer;
100 101 102 103
/*
 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
 */
104
static unsigned long long poll_timeout = 250000;
105

106 107
/* Suspend flag */
static int ap_suspend_flag;
108 109
/* Maximum domain id */
static int ap_max_domain_id;
110 111
/*
 * Flag to check if domain was set through module parameter domain=. This is
112
 * important when supsend and resume is done in a z/VM environment where the
113 114 115
 * domain might change.
 */
static int user_set_domain;
116 117
static struct bus_type ap_bus_type;

118
/* Adapter interrupt definitions */
119 120
static void ap_interrupt_handler(struct airq_struct *airq);

121 122 123 124 125 126 127
static int ap_airq_flag;

static struct airq_struct ap_airq = {
	.handler = ap_interrupt_handler,
	.isc = AP_ISC,
};

F
Felix Beck 已提交
128 129 130 131 132 133
/**
 * ap_using_interrupts() - Returns non-zero if interrupt support is
 * available.
 */
static inline int ap_using_interrupts(void)
{
134
	return ap_airq_flag;
F
Felix Beck 已提交
135 136
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150
/**
 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
 *
 * Returns the address of the local-summary-indicator of the adapter
 * interrupt handler for AP, or NULL if adapter interrupts are not
 * available.
 */
void *ap_airq_ptr(void)
{
	if (ap_using_interrupts())
		return ap_airq.lsi_ptr;
	return NULL;
}

F
Felix Beck 已提交
151 152 153 154 155 156 157
/**
 * ap_interrupts_available(): Test if AP interrupts are available.
 *
 * Returns 1 if AP interrupts are available.
 */
static int ap_interrupts_available(void)
{
158
	return test_facility(65);
F
Felix Beck 已提交
159 160
}

161 162 163 164 165 166 167 168
/**
 * ap_configuration_available(): Test if AP configuration
 * information is available.
 *
 * Returns 1 if AP configuration information is available.
 */
static int ap_configuration_available(void)
{
169
	return test_facility(12);
170 171
}

172 173 174 175 176 177 178 179 180 181 182
/**
 * ap_apft_available(): Test if AP facilities test (APFT)
 * facility is available.
 *
 * Returns 1 if APFT is is available.
 */
static int ap_apft_available(void)
{
	return test_facility(15);
}

183 184 185 186 187 188 189 190 191 192 193 194
/*
 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
 *
 * Returns 1 if the QACT subfunction is available.
 */
static inline int ap_qact_available(void)
{
	if (ap_configuration)
		return ap_configuration->qact;
	return 0;
}

195 196 197 198 199 200 201 202
/*
 * ap_query_configuration(): Fetch cryptographic config info
 *
 * Returns the ap configuration info fetched via PQAP(QCI).
 * On success 0 is returned, on failure a negative errno
 * is returned, e.g. if the PQAP(QCI) instruction is not
 * available, the return value will be -EOPNOTSUPP.
 */
203
static inline int ap_query_configuration(struct ap_config_info *info)
204
{
205
	if (!ap_configuration_available())
206
		return -EOPNOTSUPP;
207 208 209
	if (!info)
		return -EINVAL;
	return ap_qci(info);
210
}
211
EXPORT_SYMBOL(ap_query_configuration);
212

213 214 215 216 217 218 219 220 221 222 223
/**
 * ap_init_configuration(): Allocate and query configuration array.
 */
static void ap_init_configuration(void)
{
	if (!ap_configuration_available())
		return;

	ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
	if (!ap_configuration)
		return;
224
	if (ap_query_configuration(ap_configuration) != 0) {
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		kfree(ap_configuration);
		ap_configuration = NULL;
		return;
	}
}

/*
 * ap_test_config(): helper function to extract the nrth bit
 *		     within the unsigned int array field.
 */
static inline int ap_test_config(unsigned int *field, unsigned int nr)
{
	return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
}

/*
 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
 * @id AP card ID
 *
 * Returns 0 if the card is not configured
 *	   1 if the card is configured or
 *	     if the configuration information is not available
 */
static inline int ap_test_config_card_id(unsigned int id)
{
	if (!ap_configuration)	/* QCI not supported */
		return 1;
	return ap_test_config(ap_configuration->apm, id);
}

/*
 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
 * @domain AP usage domain ID
 *
 * Returns 0 if the usage domain is not configured
 *	   1 if the usage domain is configured or
 *	     if the configuration information is not available
 */
static inline int ap_test_config_domain(unsigned int domain)
{
	if (!ap_configuration)	/* QCI not supported */
		return domain < 16;
	return ap_test_config(ap_configuration->aqm, domain);
}

270
/**
271 272 273 274
 * ap_query_queue(): Check if an AP queue is available.
 * @qid: The AP queue number
 * @queue_depth: Pointer to queue depth value
 * @device_type: Pointer to device type value
275
 * @facilities: Pointer to facility indicator
276
 */
277 278
static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
			  unsigned int *facilities)
279 280
{
	struct ap_queue_status status;
281
	unsigned long info;
282 283
	int nd;

284
	if (!ap_test_config_card_id(AP_QID_CARD(qid)))
285
		return -ENODEV;
286

287
	status = ap_test_queue(qid, ap_apft_available(), &info);
288 289
	switch (status.response_code) {
	case AP_RESPONSE_NORMAL:
290 291 292
		*queue_depth = (int)(info & 0xff);
		*device_type = (int)((info >> 24) & 0xff);
		*facilities = (unsigned int)(info >> 32);
293 294
		/* Update maximum domain id */
		nd = (info >> 16) & 0xff;
295
		/* if N bit is available, z13 and newer */
296 297
		if ((info & (1UL << 57)) && nd > 0)
			ap_max_domain_id = nd;
298 299
		else /* older machine types */
			ap_max_domain_id = 15;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
		switch (*device_type) {
			/* For CEX2 and CEX3 the available functions
			 * are not refrected by the facilities bits.
			 * Instead it is coded into the type. So here
			 * modify the function bits based on the type.
			 */
		case AP_DEVICE_TYPE_CEX2A:
		case AP_DEVICE_TYPE_CEX3A:
			*facilities |= 0x08000000;
			break;
		case AP_DEVICE_TYPE_CEX2C:
		case AP_DEVICE_TYPE_CEX3C:
			*facilities |= 0x10000000;
			break;
		default:
			break;
		}
317 318 319 320 321 322 323 324 325 326 327 328
		return 0;
	case AP_RESPONSE_Q_NOT_AVAIL:
	case AP_RESPONSE_DECONFIGURED:
	case AP_RESPONSE_CHECKSTOPPED:
	case AP_RESPONSE_INVALID_ADDRESS:
		return -ENODEV;
	case AP_RESPONSE_RESET_IN_PROGRESS:
	case AP_RESPONSE_OTHERWISE_CHANGED:
	case AP_RESPONSE_BUSY:
		return -EBUSY;
	default:
		BUG();
329 330 331
	}
}

332
void ap_wait(enum ap_wait wait)
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
{
	ktime_t hr_time;

	switch (wait) {
	case AP_WAIT_AGAIN:
	case AP_WAIT_INTERRUPT:
		if (ap_using_interrupts())
			break;
		if (ap_poll_kthread) {
			wake_up(&ap_poll_wait);
			break;
		}
		/* Fall through */
	case AP_WAIT_TIMEOUT:
		spin_lock_bh(&ap_poll_timer_lock);
		if (!hrtimer_is_queued(&ap_poll_timer)) {
T
Thomas Gleixner 已提交
349
			hr_time = poll_timeout;
350 351 352 353 354 355 356 357 358 359 360 361 362
			hrtimer_forward_now(&ap_poll_timer, hr_time);
			hrtimer_restart(&ap_poll_timer);
		}
		spin_unlock_bh(&ap_poll_timer_lock);
		break;
	case AP_WAIT_NONE:
	default:
		break;
	}
}

/**
 * ap_request_timeout(): Handling of request timeouts
363
 * @t: timer making this callback
364 365 366
 *
 * Handles request timeouts.
 */
367
void ap_request_timeout(struct timer_list *t)
368
{
369
	struct ap_queue *aq = from_timer(aq, t, timeout);
370 371 372

	if (ap_suspend_flag)
		return;
373 374 375
	spin_lock_bh(&aq->lock);
	ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
	spin_unlock_bh(&aq->lock);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
}

/**
 * ap_poll_timeout(): AP receive polling for finished AP requests.
 * @unused: Unused pointer.
 *
 * Schedules the AP tasklet using a high resolution timer.
 */
static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
{
	if (!ap_suspend_flag)
		tasklet_schedule(&ap_tasklet);
	return HRTIMER_NORESTART;
}

/**
 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
 * @airq: pointer to adapter interrupt descriptor
 */
static void ap_interrupt_handler(struct airq_struct *airq)
{
	inc_irq_stat(IRQIO_APB);
	if (!ap_suspend_flag)
		tasklet_schedule(&ap_tasklet);
}

/**
 * ap_tasklet_fn(): Tasklet to poll all AP devices.
 * @dummy: Unused variable
 *
 * Poll all AP devices on the bus.
 */
static void ap_tasklet_fn(unsigned long dummy)
{
410 411
	struct ap_card *ac;
	struct ap_queue *aq;
412 413 414 415 416 417 418 419 420
	enum ap_wait wait = AP_WAIT_NONE;

	/* Reset the indicator if interrupts are used. Thus new interrupts can
	 * be received. Doing it in the beginning of the tasklet is therefor
	 * important that no requests on any AP get lost.
	 */
	if (ap_using_interrupts())
		xchg(ap_airq.lsi_ptr, 0);

421 422 423 424 425 426 427
	spin_lock_bh(&ap_list_lock);
	for_each_ap_card(ac) {
		for_each_ap_queue(aq, ac) {
			spin_lock_bh(&aq->lock);
			wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
			spin_unlock_bh(&aq->lock);
		}
428
	}
429 430 431
	spin_unlock_bh(&ap_list_lock);

	ap_wait(wait);
432 433
}

434 435
static int ap_pending_requests(void)
{
436 437 438 439 440 441 442 443 444 445
	struct ap_card *ac;
	struct ap_queue *aq;

	spin_lock_bh(&ap_list_lock);
	for_each_ap_card(ac) {
		for_each_ap_queue(aq, ac) {
			if (aq->queue_count == 0)
				continue;
			spin_unlock_bh(&ap_list_lock);
			return 1;
446 447
		}
	}
448 449
	spin_unlock_bh(&ap_list_lock);
	return 0;
450 451
}

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
/**
 * ap_poll_thread(): Thread that polls for finished requests.
 * @data: Unused pointer
 *
 * AP bus poll thread. The purpose of this thread is to poll for
 * finished requests in a loop if there is a "free" cpu - that is
 * a cpu that doesn't have anything better to do. The polling stops
 * as soon as there is another task or if all messages have been
 * delivered.
 */
static int ap_poll_thread(void *data)
{
	DECLARE_WAITQUEUE(wait, current);

	set_user_nice(current, MAX_NICE);
	set_freezable();
	while (!kthread_should_stop()) {
		add_wait_queue(&ap_poll_wait, &wait);
		set_current_state(TASK_INTERRUPTIBLE);
471
		if (ap_suspend_flag || !ap_pending_requests()) {
472 473 474 475 476 477 478 479 480 481
			schedule();
			try_to_freeze();
		}
		set_current_state(TASK_RUNNING);
		remove_wait_queue(&ap_poll_wait, &wait);
		if (need_resched()) {
			schedule();
			try_to_freeze();
			continue;
		}
482
		ap_tasklet_fn(0);
483 484
	}

485 486 487 488 489 490 491 492 493 494 495
	return 0;
}

static int ap_poll_thread_start(void)
{
	int rc;

	if (ap_using_interrupts() || ap_poll_kthread)
		return 0;
	mutex_lock(&ap_poll_thread_mutex);
	ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
496
	rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	if (rc)
		ap_poll_kthread = NULL;
	mutex_unlock(&ap_poll_thread_mutex);
	return rc;
}

static void ap_poll_thread_stop(void)
{
	if (!ap_poll_kthread)
		return;
	mutex_lock(&ap_poll_thread_mutex);
	kthread_stop(ap_poll_kthread);
	ap_poll_kthread = NULL;
	mutex_unlock(&ap_poll_thread_mutex);
}

513 514
#define is_card_dev(x) ((x)->parent == ap_root_device)
#define is_queue_dev(x) ((x)->parent != ap_root_device)
515 516

/**
517 518 519 520
 * ap_bus_match()
 * @dev: Pointer to device
 * @drv: Pointer to device_driver
 *
521 522 523 524 525 526 527
 * AP bus driver registration/unregistration.
 */
static int ap_bus_match(struct device *dev, struct device_driver *drv)
{
	struct ap_driver *ap_drv = to_ap_drv(drv);
	struct ap_device_id *id;

528
	/*
529 530 531 532
	 * Compare device type of the device with the list of
	 * supported types of the device_driver.
	 */
	for (id = ap_drv->ids; id->match_flags; id++) {
533 534 535 536 537 538 539 540
		if (is_card_dev(dev) &&
		    id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
		    id->dev_type == to_ap_dev(dev)->device_type)
			return 1;
		if (is_queue_dev(dev) &&
		    id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
		    id->dev_type == to_ap_dev(dev)->device_type)
			return 1;
541 542 543 544 545
	}
	return 0;
}

/**
546 547 548 549 550 551
 * ap_uevent(): Uevent function for AP devices.
 * @dev: Pointer to device
 * @env: Pointer to kobj_uevent_env
 *
 * It sets up a single environment variable DEV_TYPE which contains the
 * hardware device type.
552
 */
553
static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
554 555
{
	struct ap_device *ap_dev = to_ap_dev(dev);
556
	int retval = 0;
557 558 559 560 561

	if (!ap_dev)
		return -ENODEV;

	/* Set up DEV_TYPE environment variable. */
562
	retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
563 564 565
	if (retval)
		return retval;

566
	/* Add MODALIAS= */
567
	retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
568 569

	return retval;
570 571
}

572
static int ap_dev_suspend(struct device *dev)
573 574 575
{
	struct ap_device *ap_dev = to_ap_dev(dev);

576 577 578 579 580 581 582 583 584 585 586
	if (ap_dev->drv && ap_dev->drv->suspend)
		ap_dev->drv->suspend(ap_dev);
	return 0;
}

static int ap_dev_resume(struct device *dev)
{
	struct ap_device *ap_dev = to_ap_dev(dev);

	if (ap_dev->drv && ap_dev->drv->resume)
		ap_dev->drv->resume(ap_dev);
587 588
	return 0;
}
589

590 591
static void ap_bus_suspend(void)
{
592
	AP_DBF(DBF_DEBUG, "%s running\n", __func__);
593

594 595 596 597 598
	ap_suspend_flag = 1;
	/*
	 * Disable scanning for devices, thus we do not want to scan
	 * for them after removing.
	 */
599
	flush_work(&ap_scan_work);
600 601 602
	tasklet_disable(&ap_tasklet);
}

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
static int __ap_card_devices_unregister(struct device *dev, void *dummy)
{
	if (is_card_dev(dev))
		device_unregister(dev);
	return 0;
}

static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
{
	if (is_queue_dev(dev))
		device_unregister(dev);
	return 0;
}

static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
618
{
619 620 621
	if (is_queue_dev(dev) &&
	    AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
		device_unregister(dev);
622 623 624 625
	return 0;
}

static void ap_bus_resume(void)
626
{
627
	int rc;
628

629
	AP_DBF(DBF_DEBUG, "%s running\n", __func__);
630

631 632 633 634 635 636 637
	/* remove all queue devices */
	bus_for_each_dev(&ap_bus_type, NULL, NULL,
			 __ap_queue_devices_unregister);
	/* remove all card devices */
	bus_for_each_dev(&ap_bus_type, NULL, NULL,
			 __ap_card_devices_unregister);

638 639 640 641
	/* Reset thin interrupt setting */
	if (ap_interrupts_available() && !ap_using_interrupts()) {
		rc = register_adapter_interrupt(&ap_airq);
		ap_airq_flag = (rc == 0);
642
	}
643 644 645 646 647 648 649 650 651 652 653 654
	if (!ap_interrupts_available() && ap_using_interrupts()) {
		unregister_adapter_interrupt(&ap_airq);
		ap_airq_flag = 0;
	}
	/* Reset domain */
	if (!user_set_domain)
		ap_domain_index = -1;
	/* Get things going again */
	ap_suspend_flag = 0;
	if (ap_airq_flag)
		xchg(ap_airq.lsi_ptr, 0);
	tasklet_enable(&ap_tasklet);
655
	queue_work(system_long_wq, &ap_scan_work);
656
}
657

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
static int ap_power_event(struct notifier_block *this, unsigned long event,
			  void *ptr)
{
	switch (event) {
	case PM_HIBERNATION_PREPARE:
	case PM_SUSPEND_PREPARE:
		ap_bus_suspend();
		break;
	case PM_POST_HIBERNATION:
	case PM_POST_SUSPEND:
		ap_bus_resume();
		break;
	default:
		break;
	}
	return NOTIFY_DONE;
674
}
675 676 677
static struct notifier_block ap_power_notifier = {
	.notifier_call = ap_power_event,
};
678

679
static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
680

681 682 683 684
static struct bus_type ap_bus_type = {
	.name = "ap",
	.match = &ap_bus_match,
	.uevent = &ap_uevent,
685
	.pm = &ap_bus_pm_ops,
686 687
};

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
static int __ap_revise_reserved(struct device *dev, void *dummy)
{
	int rc, card, queue, devres, drvres;

	if (is_queue_dev(dev)) {
		card = AP_QID_CARD(to_ap_queue(dev)->qid);
		queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
		mutex_lock(&ap_perms_mutex);
		devres = test_bit_inv(card, ap_perms.apm)
			&& test_bit_inv(queue, ap_perms.aqm);
		mutex_unlock(&ap_perms_mutex);
		drvres = to_ap_drv(dev->driver)->flags
			& AP_DRIVER_FLAG_DEFAULT;
		if (!!devres != !!drvres) {
			AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
			       card, queue);
			rc = device_reprobe(dev);
		}
	}

	return 0;
}

static void ap_bus_revise_bindings(void)
{
	bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
}

int ap_owned_by_def_drv(int card, int queue)
{
	int rc = 0;

	if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
		return -EINVAL;

	mutex_lock(&ap_perms_mutex);

	if (test_bit_inv(card, ap_perms.apm)
	    && test_bit_inv(queue, ap_perms.aqm))
		rc = 1;

	mutex_unlock(&ap_perms_mutex);

	return rc;
}
EXPORT_SYMBOL(ap_owned_by_def_drv);

int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
				       unsigned long *aqm)
{
	int card, queue, rc = 0;

	mutex_lock(&ap_perms_mutex);

	for (card = 0; !rc && card < AP_DEVICES; card++)
		if (test_bit_inv(card, apm) &&
		    test_bit_inv(card, ap_perms.apm))
			for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
				if (test_bit_inv(queue, aqm) &&
				    test_bit_inv(queue, ap_perms.aqm))
					rc = 1;

	mutex_unlock(&ap_perms_mutex);

	return rc;
}
EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);

756 757 758
static int ap_device_probe(struct device *dev)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
759
	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	int card, queue, devres, drvres, rc;

	if (is_queue_dev(dev)) {
		/*
		 * If the apqn is marked as reserved/used by ap bus and
		 * default drivers, only probe with drivers with the default
		 * flag set. If it is not marked, only probe with drivers
		 * with the default flag not set.
		 */
		card = AP_QID_CARD(to_ap_queue(dev)->qid);
		queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
		mutex_lock(&ap_perms_mutex);
		devres = test_bit_inv(card, ap_perms.apm)
			&& test_bit_inv(queue, ap_perms.aqm);
		mutex_unlock(&ap_perms_mutex);
		drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
		if (!!devres != !!drvres)
			return -ENODEV;
	}
779

780 781 782 783 784 785 786 787 788
	/* Add queue/card to list of active queues/cards */
	spin_lock_bh(&ap_list_lock);
	if (is_card_dev(dev))
		list_add(&to_ap_card(dev)->list, &ap_card_list);
	else
		list_add(&to_ap_queue(dev)->list,
			 &to_ap_queue(dev)->card->queues);
	spin_unlock_bh(&ap_list_lock);

789
	ap_dev->drv = ap_drv;
790
	rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
791 792 793 794 795 796 797 798

	if (rc) {
		spin_lock_bh(&ap_list_lock);
		if (is_card_dev(dev))
			list_del_init(&to_ap_card(dev)->list);
		else
			list_del_init(&to_ap_queue(dev)->list);
		spin_unlock_bh(&ap_list_lock);
799
		ap_dev->drv = NULL;
800 801
	}

802 803 804 805 806 807 808 809
	return rc;
}

static int ap_device_remove(struct device *dev)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	struct ap_driver *ap_drv = ap_dev->drv;

810 811 812 813
	if (ap_drv->remove)
		ap_drv->remove(ap_dev);

	/* Remove queue/card from list of active queues/cards */
814 815 816 817 818 819
	spin_lock_bh(&ap_list_lock);
	if (is_card_dev(dev))
		list_del_init(&to_ap_card(dev)->list);
	else
		list_del_init(&to_ap_queue(dev)->list);
	spin_unlock_bh(&ap_list_lock);
820

821 822 823 824 825 826 827 828
	return 0;
}

int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
		       char *name)
{
	struct device_driver *drv = &ap_drv->driver;

829 830 831
	if (!initialised)
		return -ENODEV;

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
	drv->bus = &ap_bus_type;
	drv->probe = ap_device_probe;
	drv->remove = ap_device_remove;
	drv->owner = owner;
	drv->name = name;
	return driver_register(drv);
}
EXPORT_SYMBOL(ap_driver_register);

void ap_driver_unregister(struct ap_driver *ap_drv)
{
	driver_unregister(&ap_drv->driver);
}
EXPORT_SYMBOL(ap_driver_unregister);

847 848
void ap_bus_force_rescan(void)
{
849 850
	if (ap_suspend_flag)
		return;
851
	/* processing a asynchronous bus rescan */
852
	del_timer(&ap_config_timer);
853 854
	queue_work(system_long_wq, &ap_scan_work);
	flush_work(&ap_scan_work);
855 856 857
}
EXPORT_SYMBOL(ap_bus_force_rescan);

858
/*
859 860 861 862 863 864
 * hex2bitmap() - parse hex mask string and set bitmap.
 * Valid strings are "0x012345678" with at least one valid hex number.
 * Rest of the bitmap to the right is padded with 0. No spaces allowed
 * within the string, the leading 0x may be omitted.
 * Returns the bitmask with exactly the bits set as given by the hex
 * string (both in big endian order).
865
 */
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
{
	int i, n, b;

	/* bits needs to be a multiple of 8 */
	if (bits & 0x07)
		return -EINVAL;

	if (str[0] == '0' && str[1] == 'x')
		str++;
	if (*str == 'x')
		str++;

	for (i = 0; isxdigit(*str) && i < bits; str++) {
		b = hex_to_bin(*str);
		for (n = 0; n < 4; n++)
			if (b & (0x08 >> n))
				set_bit_inv(i + n, bitmap);
		i += 4;
	}

887 888 889 890 891 892 893 894
	if (*str == '\n')
		str++;
	if (*str)
		return -EINVAL;
	return 0;
}

/*
895 896 897
 * modify_bitmap() - parse bitmask argument and modify an existing
 * bit mask accordingly. A concatenation (done with ',') of these
 * terms is recognized:
898 899 900 901 902 903
 *   +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
 * <bitnr> may be any valid number (hex, decimal or octal) in the range
 * 0...bits-1; the leading + or - is required. Here are some examples:
 *   +0-15,+32,-128,-0xFF
 *   -0-255,+1-16,+0x128
 *   +1,+2,+3,+4,-5,-7-10
904 905 906 907 908 909
 * Returns the new bitmap after all changes have been applied. Every
 * positive value in the string will set a bit and every negative value
 * in the string will clear a bit. As a bit may be touched more than once,
 * the last 'operation' wins:
 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
 * cleared again. All other bits are unmodified.
910
 */
911
static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
912 913 914 915 916 917
{
	int a, i, z;
	char *np, sign;

	/* bits needs to be a multiple of 8 */
	if (bits & 0x07)
918 919
		return -EINVAL;

920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
	while (*str) {
		sign = *str++;
		if (sign != '+' && sign != '-')
			return -EINVAL;
		a = z = simple_strtoul(str, &np, 0);
		if (str == np || a >= bits)
			return -EINVAL;
		str = np;
		if (*str == '-') {
			z = simple_strtoul(++str, &np, 0);
			if (str == np || a > z || z >= bits)
				return -EINVAL;
			str = np;
		}
		for (i = a; i <= z; i++)
935 936 937 938
			if (sign == '+')
				set_bit_inv(i, bitmap);
			else
				clear_bit_inv(i, bitmap);
939 940 941 942
		while (*str == ',' || *str == '\n')
			str++;
	}

943 944 945
	return 0;
}

946 947 948
int ap_parse_mask_str(const char *str,
		      unsigned long *bitmap, int bits,
		      struct mutex *lock)
949
{
950 951
	unsigned long *newmap, size;
	int rc;
952 953 954 955 956

	/* bits needs to be a multiple of 8 */
	if (bits & 0x07)
		return -EINVAL;

957 958 959 960 961 962 963 964 965
	size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
	newmap = kmalloc(size, GFP_KERNEL);
	if (!newmap)
		return -ENOMEM;
	if (mutex_lock_interruptible(lock)) {
		kfree(newmap);
		return -ERESTARTSYS;
	}

966
	if (*str == '+' || *str == '-') {
967 968
		memcpy(newmap, bitmap, size);
		rc = modify_bitmap(str, newmap, bits);
969
	} else {
970 971
		memset(newmap, 0, size);
		rc = hex2bitmap(str, newmap, bits);
972
	}
973 974
	if (rc == 0)
		memcpy(bitmap, newmap, size);
975
	mutex_unlock(lock);
976 977
	kfree(newmap);
	return rc;
978
}
979
EXPORT_SYMBOL(ap_parse_mask_str);
980 981 982 983 984

/*
 * AP bus attributes.
 */

985 986 987 988 989
static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
}

990 991 992 993 994 995
static ssize_t ap_domain_store(struct bus_type *bus,
			       const char *buf, size_t count)
{
	int domain;

	if (sscanf(buf, "%i\n", &domain) != 1 ||
996 997
	    domain < 0 || domain > ap_max_domain_id ||
	    !test_bit_inv(domain, ap_perms.aqm))
998 999 1000 1001
		return -EINVAL;
	spin_lock_bh(&ap_domain_lock);
	ap_domain_index = domain;
	spin_unlock_bh(&ap_domain_lock);
1002

1003
	AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
1004

1005 1006 1007
	return count;
}

1008
static BUS_ATTR_RW(ap_domain);
1009

1010 1011
static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
{
1012 1013
	if (!ap_configuration)	/* QCI not supported */
		return snprintf(buf, PAGE_SIZE, "not supported\n");
1014

1015 1016
	return snprintf(buf, PAGE_SIZE,
			"0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1017 1018 1019 1020 1021 1022
			ap_configuration->adm[0], ap_configuration->adm[1],
			ap_configuration->adm[2], ap_configuration->adm[3],
			ap_configuration->adm[4], ap_configuration->adm[5],
			ap_configuration->adm[6], ap_configuration->adm[7]);
}

1023
static BUS_ATTR_RO(ap_control_domain_mask);
1024

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
{
	if (!ap_configuration)	/* QCI not supported */
		return snprintf(buf, PAGE_SIZE, "not supported\n");

	return snprintf(buf, PAGE_SIZE,
			"0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
			ap_configuration->aqm[0], ap_configuration->aqm[1],
			ap_configuration->aqm[2], ap_configuration->aqm[3],
			ap_configuration->aqm[4], ap_configuration->aqm[5],
			ap_configuration->aqm[6], ap_configuration->aqm[7]);
}

1038
static BUS_ATTR_RO(ap_usage_domain_mask);
1039

F
Felix Beck 已提交
1040 1041 1042 1043 1044 1045
static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n",
			ap_using_interrupts() ? 1 : 0);
}

1046 1047 1048 1049 1050 1051
static BUS_ATTR_RO(ap_interrupts);

static ssize_t config_time_show(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
}
F
Felix Beck 已提交
1052

1053 1054
static ssize_t config_time_store(struct bus_type *bus,
				 const char *buf, size_t count)
1055 1056 1057 1058 1059 1060
{
	int time;

	if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
		return -EINVAL;
	ap_config_time = time;
1061
	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1062 1063 1064
	return count;
}

1065
static BUS_ATTR_RW(config_time);
1066

1067
static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1068 1069 1070 1071
{
	return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
}

1072 1073
static ssize_t poll_thread_store(struct bus_type *bus,
				 const char *buf, size_t count)
1074 1075 1076 1077 1078 1079 1080 1081
{
	int flag, rc;

	if (sscanf(buf, "%d\n", &flag) != 1)
		return -EINVAL;
	if (flag) {
		rc = ap_poll_thread_start();
		if (rc)
1082 1083
			count = rc;
	} else
1084 1085 1086 1087
		ap_poll_thread_stop();
	return count;
}

1088
static BUS_ATTR_RW(poll_thread);
1089

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
}

static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
				  size_t count)
{
	unsigned long long time;
	ktime_t hr_time;

	/* 120 seconds = maximum poll interval */
F
Felix Beck 已提交
1102 1103
	if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
	    time > 120000000000ULL)
1104 1105
		return -EINVAL;
	poll_timeout = time;
T
Thomas Gleixner 已提交
1106
	hr_time = poll_timeout;
1107

1108 1109 1110 1111 1112 1113
	spin_lock_bh(&ap_poll_timer_lock);
	hrtimer_cancel(&ap_poll_timer);
	hrtimer_set_expires(&ap_poll_timer, hr_time);
	hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
	spin_unlock_bh(&ap_poll_timer_lock);

1114 1115 1116
	return count;
}

1117
static BUS_ATTR_RW(poll_timeout);
1118

1119 1120
static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
{
1121 1122 1123 1124 1125
	int max_domain_id;

	if (ap_configuration)
		max_domain_id = ap_max_domain_id ? : -1;
	else
1126 1127 1128 1129
		max_domain_id = 15;
	return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
}

1130
static BUS_ATTR_RO(ap_max_domain_id);
1131

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
static ssize_t apmask_show(struct bus_type *bus, char *buf)
{
	int rc;

	if (mutex_lock_interruptible(&ap_perms_mutex))
		return -ERESTARTSYS;
	rc = snprintf(buf, PAGE_SIZE,
		      "0x%016lx%016lx%016lx%016lx\n",
		      ap_perms.apm[0], ap_perms.apm[1],
		      ap_perms.apm[2], ap_perms.apm[3]);
	mutex_unlock(&ap_perms_mutex);

	return rc;
}

static ssize_t apmask_store(struct bus_type *bus, const char *buf,
			    size_t count)
{
1150
	int rc;
1151

1152
	rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1153 1154
	if (rc)
		return rc;
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

	ap_bus_revise_bindings();

	return count;
}

static BUS_ATTR_RW(apmask);

static ssize_t aqmask_show(struct bus_type *bus, char *buf)
{
	int rc;

	if (mutex_lock_interruptible(&ap_perms_mutex))
		return -ERESTARTSYS;
	rc = snprintf(buf, PAGE_SIZE,
		      "0x%016lx%016lx%016lx%016lx\n",
		      ap_perms.aqm[0], ap_perms.aqm[1],
		      ap_perms.aqm[2], ap_perms.aqm[3]);
	mutex_unlock(&ap_perms_mutex);

	return rc;
}

static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
			    size_t count)
{
1181
	int rc;
1182

1183
	rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1184 1185
	if (rc)
		return rc;
1186 1187 1188 1189 1190 1191 1192 1193

	ap_bus_revise_bindings();

	return count;
}

static BUS_ATTR_RW(aqmask);

1194 1195
static struct bus_attribute *const ap_bus_attrs[] = {
	&bus_attr_ap_domain,
1196
	&bus_attr_ap_control_domain_mask,
1197
	&bus_attr_ap_usage_domain_mask,
1198 1199
	&bus_attr_config_time,
	&bus_attr_poll_thread,
F
Felix Beck 已提交
1200
	&bus_attr_ap_interrupts,
1201
	&bus_attr_poll_timeout,
1202
	&bus_attr_ap_max_domain_id,
1203 1204
	&bus_attr_apmask,
	&bus_attr_aqmask,
1205
	NULL,
1206 1207 1208
};

/**
1209 1210
 * ap_select_domain(): Select an AP domain if possible and we haven't
 * already done so before.
1211
 */
1212
static void ap_select_domain(void)
1213
{
1214 1215 1216
	int count, max_count, best_domain;
	struct ap_queue_status status;
	int i, j;
1217

1218
	/*
1219 1220 1221 1222
	 * We want to use a single domain. Either the one specified with
	 * the "domain=" parameter or the domain with the maximum number
	 * of devices.
	 */
1223 1224
	spin_lock_bh(&ap_domain_lock);
	if (ap_domain_index >= 0) {
1225
		/* Domain has already been selected. */
1226
		spin_unlock_bh(&ap_domain_lock);
1227
		return;
1228
	}
1229 1230 1231
	best_domain = -1;
	max_count = 0;
	for (i = 0; i < AP_DOMAINS; i++) {
1232 1233
		if (!ap_test_config_domain(i) ||
		    !test_bit_inv(i, ap_perms.aqm))
1234
			continue;
1235 1236
		count = 0;
		for (j = 0; j < AP_DEVICES; j++) {
1237 1238
			if (!ap_test_config_card_id(j))
				continue;
1239 1240 1241
			status = ap_test_queue(AP_MKQID(j, i),
					       ap_apft_available(),
					       NULL);
1242
			if (status.response_code != AP_RESPONSE_NORMAL)
1243 1244 1245 1246 1247 1248 1249 1250
				continue;
			count++;
		}
		if (count > max_count) {
			max_count = count;
			best_domain = i;
		}
	}
1251
	if (best_domain >= 0) {
1252
		ap_domain_index = best_domain;
1253
		AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
1254
	}
1255
	spin_unlock_bh(&ap_domain_lock);
1256 1257
}

1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
/*
 * This function checks the type and returns either 0 for not
 * supported or the highest compatible type value (which may
 * include the input type value).
 */
static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
{
	int comp_type = 0;

	/* < CEX2A is not supported */
	if (rawtype < AP_DEVICE_TYPE_CEX2A)
		return 0;
	/* up to CEX6 known and fully supported */
	if (rawtype <= AP_DEVICE_TYPE_CEX6)
		return rawtype;
	/*
	 * unknown new type > CEX6, check for compatibility
	 * to the highest known and supported type which is
	 * currently CEX6 with the help of the QACT function.
	 */
	if (ap_qact_available()) {
		struct ap_queue_status status;
1280
		union ap_qact_ap_info apinfo = {0};
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298

		apinfo.mode = (func >> 26) & 0x07;
		apinfo.cat = AP_DEVICE_TYPE_CEX6;
		status = ap_qact(qid, 0, &apinfo);
		if (status.response_code == AP_RESPONSE_NORMAL
		    && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
		    && apinfo.cat <= AP_DEVICE_TYPE_CEX6)
			comp_type = apinfo.cat;
	}
	if (!comp_type)
		AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
		       AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
	else if (comp_type != rawtype)
		AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
		       AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
	return comp_type;
}

1299 1300 1301
/*
 * helper function to be used with bus_find_dev
 * matches for the card device with the given id
1302
 */
1303
static int __match_card_device_with_id(struct device *dev, void *data)
1304
{
1305
	return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
1306 1307
}

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
/* helper function to be used with bus_find_dev
 * matches for the queue device with a given qid
 */
static int __match_queue_device_with_qid(struct device *dev, void *data)
{
	return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
}

/**
 * ap_scan_bus(): Scan the AP bus for new devices
 * Runs periodically, workqueue timer (ap_config_time)
 */
1320
static void ap_scan_bus(struct work_struct *unused)
1321
{
1322 1323
	struct ap_queue *aq;
	struct ap_card *ac;
1324 1325
	struct device *dev;
	ap_qid_t qid;
1326 1327
	int comp_type, depth = 0, type = 0;
	unsigned int func = 0;
1328
	int rc, id, dom, borked, domains, defdomdevs = 0;
1329

1330
	AP_DBF(DBF_DEBUG, "%s running\n", __func__);
1331

1332
	ap_query_configuration(ap_configuration);
1333
	ap_select_domain();
1334

1335 1336
	for (id = 0; id < AP_DEVICES; id++) {
		/* check if device is registered */
1337
		dev = bus_find_device(&ap_bus_type, NULL,
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
				      (void *)(long) id,
				      __match_card_device_with_id);
		ac = dev ? to_ap_card(dev) : NULL;
		if (!ap_test_config_card_id(id)) {
			if (dev) {
				/* Card device has been removed from
				 * configuration, remove the belonging
				 * queue devices.
				 */
				bus_for_each_dev(&ap_bus_type, NULL,
					(void *)(long) id,
					__ap_queue_devices_with_id_unregister);
				/* now remove the card device */
1351
				device_unregister(dev);
1352 1353
				put_device(dev);
			}
1354 1355
			continue;
		}
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
		/* According to the configuration there should be a card
		 * device, so check if there is at least one valid queue
		 * and maybe create queue devices and the card device.
		 */
		domains = 0;
		for (dom = 0; dom < AP_DOMAINS; dom++) {
			qid = AP_MKQID(id, dom);
			dev = bus_find_device(&ap_bus_type, NULL,
					      (void *)(long) qid,
					      __match_queue_device_with_qid);
			aq = dev ? to_ap_queue(dev) : NULL;
			if (!ap_test_config_domain(dom)) {
				if (dev) {
					/* Queue device exists but has been
					 * removed from configuration.
					 */
					device_unregister(dev);
					put_device(dev);
				}
				continue;
			}
1377
			rc = ap_query_queue(qid, &depth, &type, &func);
1378 1379 1380 1381
			if (dev) {
				spin_lock_bh(&aq->lock);
				if (rc == -ENODEV ||
				    /* adapter reconfiguration */
1382
				    (ac && ac->functions != func))
1383 1384 1385 1386 1387 1388 1389 1390
					aq->state = AP_STATE_BORKED;
				borked = aq->state == AP_STATE_BORKED;
				spin_unlock_bh(&aq->lock);
				if (borked)	/* Remove broken device */
					device_unregister(dev);
				put_device(dev);
				if (!borked) {
					domains++;
1391 1392
					if (dom == ap_domain_index)
						defdomdevs++;
1393 1394 1395 1396 1397
					continue;
				}
			}
			if (rc)
				continue;
1398 1399 1400 1401 1402
			/* a new queue device is needed, check out comp type */
			comp_type = ap_get_compatible_type(qid, type, func);
			if (!comp_type)
				continue;
			/* maybe a card device needs to be created first */
1403
			if (!ac) {
1404 1405
				ac = ap_card_create(id, depth, type,
						    comp_type, func);
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
				if (!ac)
					continue;
				ac->ap_dev.device.bus = &ap_bus_type;
				ac->ap_dev.device.parent = ap_root_device;
				dev_set_name(&ac->ap_dev.device,
					     "card%02x", id);
				/* Register card with AP bus */
				rc = device_register(&ac->ap_dev.device);
				if (rc) {
					put_device(&ac->ap_dev.device);
					ac = NULL;
					break;
				}
				/* get it and thus adjust reference counter */
				get_device(&ac->ap_dev.device);
			}
			/* now create the new queue device */
1423
			aq = ap_queue_create(qid, comp_type);
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
			if (!aq)
				continue;
			aq->card = ac;
			aq->ap_dev.device.bus = &ap_bus_type;
			aq->ap_dev.device.parent = &ac->ap_dev.device;
			dev_set_name(&aq->ap_dev.device,
				     "%02x.%04x", id, dom);
			/* Start with a device reset */
			spin_lock_bh(&aq->lock);
			ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
			spin_unlock_bh(&aq->lock);
			/* Register device */
			rc = device_register(&aq->ap_dev.device);
			if (rc) {
				put_device(&aq->ap_dev.device);
				continue;
			}
			domains++;
1442 1443
			if (dom == ap_domain_index)
				defdomdevs++;
1444 1445 1446 1447 1448 1449
		} /* end domain loop */
		if (ac) {
			/* remove card dev if there are no queue devices */
			if (!domains)
				device_unregister(&ac->ap_dev.device);
			put_device(&ac->ap_dev.device);
1450
		}
1451
	} /* end device loop */
1452

1453
	if (ap_domain_index >= 0 && defdomdevs < 1)
1454 1455
		AP_DBF(DBF_INFO,
		       "no queue device with default domain %d available\n",
1456 1457
		       ap_domain_index);

1458
	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1459 1460
}

1461
static void ap_config_timeout(struct timer_list *unused)
1462
{
1463 1464
	if (ap_suspend_flag)
		return;
1465
	queue_work(system_long_wq, &ap_scan_work);
1466
}
1467

1468
static int __init ap_debug_init(void)
1469 1470 1471 1472 1473 1474 1475 1476 1477
{
	ap_dbf_info = debug_register("ap", 1, 1,
				     DBF_MAX_SPRINTF_ARGS * sizeof(long));
	debug_register_view(ap_dbf_info, &debug_sprintf_view);
	debug_set_level(ap_dbf_info, DBF_ERR);

	return 0;
}

1478 1479
static void __init ap_perms_init(void)
{
1480
	/* all resources useable if no kernel parameter string given */
1481
	memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1482 1483 1484
	memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
	memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));

1485
	/* apm kernel parameter string */
1486
	if (apm_str) {
1487
		memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1488 1489
		ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
				  &ap_perms_mutex);
1490 1491
	}

1492 1493 1494
	/* aqm kernel parameter string */
	if (aqm_str) {
		memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1495 1496
		ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
				  &ap_perms_mutex);
1497 1498 1499
	}
}

1500
/**
1501 1502 1503
 * ap_module_init(): The module initialization code.
 *
 * Initializes the module.
1504
 */
1505
static int __init ap_module_init(void)
1506
{
1507
	int max_domain_id;
1508 1509
	int rc, i;

1510 1511 1512 1513
	rc = ap_debug_init();
	if (rc)
		return rc;

1514
	if (!ap_instructions_available()) {
1515 1516 1517 1518
		pr_warn("The hardware system does not support AP instructions\n");
		return -ENODEV;
	}

1519
	/* set up the AP permissions (ioctls, ap and aq masks) */
1520 1521
	ap_perms_init();

1522 1523 1524 1525
	/* Get AP configuration data if available */
	ap_init_configuration();

	if (ap_configuration)
1526 1527
		max_domain_id =
			ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1528 1529
	else
		max_domain_id = 15;
1530 1531 1532
	if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
	    (ap_domain_index >= 0 &&
	     !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1533 1534
		pr_warn("%d is not a valid cryptographic domain\n",
			ap_domain_index);
1535
		ap_domain_index = -1;
1536
	}
1537 1538 1539 1540 1541 1542
	/* In resume callback we need to know if the user had set the domain.
	 * If so, we can not just reset it.
	 */
	if (ap_domain_index >= 0)
		user_set_domain = 1;

F
Felix Beck 已提交
1543
	if (ap_interrupts_available()) {
1544 1545
		rc = register_adapter_interrupt(&ap_airq);
		ap_airq_flag = (rc == 0);
F
Felix Beck 已提交
1546 1547
	}

1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
	/* Create /sys/bus/ap. */
	rc = bus_register(&ap_bus_type);
	if (rc)
		goto out;
	for (i = 0; ap_bus_attrs[i]; i++) {
		rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
		if (rc)
			goto out_bus;
	}

	/* Create /sys/devices/ap. */
M
Mark McLoughlin 已提交
1559
	ap_root_device = root_device_register("ap");
1560
	rc = PTR_ERR_OR_ZERO(ap_root_device);
1561 1562 1563
	if (rc)
		goto out_bus;

1564
	/* Setup the AP bus rescan timer. */
1565
	timer_setup(&ap_config_timer, ap_config_timeout, 0);
1566

1567 1568
	/*
	 * Setup the high resultion poll timer.
1569 1570 1571 1572
	 * If we are running under z/VM adjust polling to z/VM polling rate.
	 */
	if (MACHINE_IS_VM)
		poll_timeout = 1500000;
1573
	spin_lock_init(&ap_poll_timer_lock);
1574 1575 1576
	hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
	ap_poll_timer.function = ap_poll_timeout;

1577 1578 1579 1580 1581 1582 1583
	/* Start the low priority AP bus poll thread. */
	if (ap_thread_flag) {
		rc = ap_poll_thread_start();
		if (rc)
			goto out_work;
	}

1584 1585 1586 1587
	rc = register_pm_notifier(&ap_power_notifier);
	if (rc)
		goto out_pm;

1588
	queue_work(system_long_wq, &ap_scan_work);
1589
	initialised = true;
1590

1591 1592
	return 0;

1593 1594
out_pm:
	ap_poll_thread_stop();
1595
out_work:
1596
	hrtimer_cancel(&ap_poll_timer);
M
Mark McLoughlin 已提交
1597
	root_device_unregister(ap_root_device);
1598 1599 1600 1601 1602
out_bus:
	while (i--)
		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
	bus_unregister(&ap_bus_type);
out:
1603 1604
	if (ap_using_interrupts())
		unregister_adapter_interrupt(&ap_airq);
1605
	kfree(ap_configuration);
1606 1607
	return rc;
}
1608
device_initcall(ap_module_init);