ap_bus.c 51.7 KB
Newer Older
1
/*
2
 * Copyright IBM Corp. 2006, 2012
3 4 5
 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
 *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
 *	      Ralph Wuerthner <rwuerthn@de.ibm.com>
F
Felix Beck 已提交
6
 *	      Felix Beck <felix.beck@de.ibm.com>
7
 *	      Holger Dengler <hd@linux.vnet.ibm.com>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Adjunct processor bus.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

26 27 28
#define KMSG_COMPONENT "ap"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

29
#include <linux/kernel_stat.h>
30 31 32 33 34 35
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
36
#include <linux/slab.h>
37 38 39
#include <linux/notifier.h>
#include <linux/kthread.h>
#include <linux/mutex.h>
40
#include <asm/reset.h>
F
Felix Beck 已提交
41
#include <asm/airq.h>
A
Arun Sharma 已提交
42
#include <linux/atomic.h>
F
Felix Beck 已提交
43
#include <asm/isc.h>
44 45
#include <linux/hrtimer.h>
#include <linux/ktime.h>
46
#include <asm/facility.h>
47 48 49 50

#include "ap_bus.h"

/* Some prototypes. */
51
static void ap_scan_bus(struct work_struct *);
52
static void ap_poll_all(unsigned long);
53
static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
54 55
static int ap_poll_thread_start(void);
static void ap_poll_thread_stop(void);
56
static void ap_request_timeout(unsigned long);
F
Felix Beck 已提交
57
static inline void ap_schedule_poll_timer(void);
58 59 60 61 62 63
static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
static int ap_device_remove(struct device *dev);
static int ap_device_probe(struct device *dev);
static void ap_interrupt_handler(void *unused1, void *unused2);
static void ap_reset(struct ap_device *ap_dev);
static void ap_config_timeout(unsigned long ptr);
64
static int ap_select_domain(void);
65
static void ap_query_configuration(void);
66

67
/*
68 69 70
 * Module description.
 */
MODULE_AUTHOR("IBM Corporation");
71 72
MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
		   "Copyright IBM Corp. 2006, 2012");
73 74
MODULE_LICENSE("GPL");

75
/*
76 77 78 79 80 81 82
 * Module parameter
 */
int ap_domain_index = -1;	/* Adjunct Processor Domain Index */
module_param_named(domain, ap_domain_index, int, 0000);
MODULE_PARM_DESC(domain, "domain index for ap devices");
EXPORT_SYMBOL(ap_domain_index);

83
static int ap_thread_flag = 0;
84
module_param_named(poll_thread, ap_thread_flag, int, 0000);
85
MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
86 87

static struct device *ap_root_device = NULL;
88
static struct ap_config_info *ap_configuration;
89
static DEFINE_SPINLOCK(ap_device_list_lock);
90
static LIST_HEAD(ap_device_list);
91

92
/*
93 94 95 96 97
 * Workqueue & timer for bus rescan.
 */
static struct workqueue_struct *ap_work_queue;
static struct timer_list ap_config_timer;
static int ap_config_time = AP_CONFIG_TIME;
98
static DECLARE_WORK(ap_config_work, ap_scan_bus);
99

100
/*
F
Felix Beck 已提交
101
 * Tasklet & timer for AP request polling and interrupts
102 103 104 105 106 107
 */
static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
static atomic_t ap_poll_requests = ATOMIC_INIT(0);
static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
static struct task_struct *ap_poll_kthread = NULL;
static DEFINE_MUTEX(ap_poll_thread_mutex);
108
static DEFINE_SPINLOCK(ap_poll_timer_lock);
F
Felix Beck 已提交
109
static void *ap_interrupt_indicator;
110 111 112 113
static struct hrtimer ap_poll_timer;
/* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
static unsigned long long poll_timeout = 250000;
114

115 116
/* Suspend flag */
static int ap_suspend_flag;
117 118 119 120
/* Flag to check if domain was set through module parameter domain=. This is
 * important when supsend and resume is done in a z/VM environment where the
 * domain might change. */
static int user_set_domain = 0;
121 122
static struct bus_type ap_bus_type;

F
Felix Beck 已提交
123 124 125 126 127 128 129 130 131
/**
 * ap_using_interrupts() - Returns non-zero if interrupt support is
 * available.
 */
static inline int ap_using_interrupts(void)
{
	return ap_interrupt_indicator != NULL;
}

132
/**
133
 * ap_intructions_available() - Test if AP instructions are available.
134
 *
135
 * Returns 0 if the AP instructions are installed.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
 */
static inline int ap_instructions_available(void)
{
	register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
	register unsigned long reg1 asm ("1") = -ENODEV;
	register unsigned long reg2 asm ("2") = 0UL;

	asm volatile(
		"   .long 0xb2af0000\n"		/* PQAP(TAPQ) */
		"0: la    %1,0\n"
		"1:\n"
		EX_TABLE(0b, 1b)
		: "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
	return reg1;
}

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

162 163 164 165 166 167
/**
 * ap_configuration_available(): Test if AP configuration
 * information is available.
 *
 * Returns 1 if AP configuration information is available.
 */
H
Heiko Carstens 已提交
168
#ifdef CONFIG_64BIT
169 170 171 172
static int ap_configuration_available(void)
{
	return test_facility(2) && test_facility(12);
}
H
Heiko Carstens 已提交
173
#endif
174

175
/**
176 177 178 179
 * ap_test_queue(): Test adjunct processor queue.
 * @qid: The AP queue number
 * @queue_depth: Pointer to queue depth value
 * @device_type: Pointer to device type value
180
 *
181
 * Returns AP queue status structure.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
 */
static inline struct ap_queue_status
ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
{
	register unsigned long reg0 asm ("0") = qid;
	register struct ap_queue_status reg1 asm ("1");
	register unsigned long reg2 asm ("2") = 0UL;

	asm volatile(".long 0xb2af0000"		/* PQAP(TAPQ) */
		     : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
	*device_type = (int) (reg2 >> 24);
	*queue_depth = (int) (reg2 & 0xff);
	return reg1;
}

/**
198 199
 * ap_reset_queue(): Reset adjunct processor queue.
 * @qid: The AP queue number
200
 *
201
 * Returns AP queue status structure.
202 203 204 205 206 207 208 209 210 211 212 213 214
 */
static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
{
	register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
	register struct ap_queue_status reg1 asm ("1");
	register unsigned long reg2 asm ("2") = 0UL;

	asm volatile(
		".long 0xb2af0000"		/* PQAP(RAPQ) */
		: "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
	return reg1;
}

F
Felix Beck 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
#ifdef CONFIG_64BIT
/**
 * ap_queue_interruption_control(): Enable interruption for a specific AP.
 * @qid: The AP queue number
 * @ind: The notification indicator byte
 *
 * Returns AP queue status.
 */
static inline struct ap_queue_status
ap_queue_interruption_control(ap_qid_t qid, void *ind)
{
	register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
	register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
	register struct ap_queue_status reg1_out asm ("1");
	register void *reg2 asm ("2") = ind;
	asm volatile(
231
		".long 0xb2af0000"		/* PQAP(AQIC) */
F
Felix Beck 已提交
232 233 234 235 236 237 238
		: "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
		:
		: "cc" );
	return reg1_out;
}
#endif

239 240 241
#ifdef CONFIG_64BIT
static inline struct ap_queue_status
__ap_query_functions(ap_qid_t qid, unsigned int *functions)
242 243
{
	register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
244 245
	register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
	register unsigned long reg2 asm ("2");
246 247

	asm volatile(
248
		".long 0xb2af0000\n"		/* PQAP(TAPQ) */
249 250 251
		"0:\n"
		EX_TABLE(0b, 0b)
		: "+d" (reg0), "+d" (reg1), "=d" (reg2)
252 253 254
		:
		: "cc");

255
	*functions = (unsigned int)(reg2 >> 32);
256 257
	return reg1;
}
258
#endif
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
#ifdef CONFIG_64BIT
static inline int __ap_query_configuration(struct ap_config_info *config)
{
	register unsigned long reg0 asm ("0") = 0x04000000UL;
	register unsigned long reg1 asm ("1") = -EINVAL;
	register unsigned char *reg2 asm ("2") = (unsigned char *)config;

	asm volatile(
		".long 0xb2af0000\n"		/* PQAP(QCI) */
		"0: la    %1,0\n"
		"1:\n"
		EX_TABLE(0b, 1b)
		: "+d" (reg0), "+d" (reg1), "+d" (reg2)
		:
		: "cc");

	return reg1;
}
#endif

280
/**
281
 * ap_query_functions(): Query supported functions.
282
 * @qid: The AP queue number
283
 * @functions: Pointer to functions field.
284
 *
285 286 287 288 289
 * Returns
 *   0	     on success.
 *   -ENODEV  if queue not valid.
 *   -EBUSY   if device busy.
 *   -EINVAL  if query function is not supported
290
 */
291
static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
292
{
293
#ifdef CONFIG_64BIT
294
	struct ap_queue_status status;
295 296
	int i;
	status = __ap_query_functions(qid, functions);
297 298

	for (i = 0; i < AP_MAX_RESET; i++) {
299 300 301
		if (ap_queue_status_invalid_test(&status))
			return -ENODEV;

302 303
		switch (status.response_code) {
		case AP_RESPONSE_NORMAL:
304
			return 0;
305 306 307 308 309 310 311
		case AP_RESPONSE_RESET_IN_PROGRESS:
		case AP_RESPONSE_BUSY:
			break;
		case AP_RESPONSE_Q_NOT_AVAIL:
		case AP_RESPONSE_DECONFIGURED:
		case AP_RESPONSE_CHECKSTOPPED:
		case AP_RESPONSE_INVALID_ADDRESS:
312
			return -ENODEV;
313 314 315 316 317 318 319
		case AP_RESPONSE_OTHERWISE_CHANGED:
			break;
		default:
			break;
		}
		if (i < AP_MAX_RESET - 1) {
			udelay(5);
320
			status = __ap_query_functions(qid, functions);
321 322
		}
	}
323 324 325 326 327 328
	return -EBUSY;
#else
	return -EINVAL;
#endif
}

F
Felix Beck 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
/**
 * ap_queue_enable_interruption(): Enable interruption on an AP.
 * @qid: The AP queue number
 * @ind: the notification indicator byte
 *
 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
 * on the return value it waits a while and tests the AP queue if interrupts
 * have been switched on using ap_test_queue().
 */
static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
{
#ifdef CONFIG_64BIT
	struct ap_queue_status status;
	int t_depth, t_device_type, rc, i;

	rc = -EBUSY;
	status = ap_queue_interruption_control(qid, ind);

	for (i = 0; i < AP_MAX_RESET; i++) {
		switch (status.response_code) {
		case AP_RESPONSE_NORMAL:
			if (status.int_enabled)
				return 0;
			break;
		case AP_RESPONSE_RESET_IN_PROGRESS:
		case AP_RESPONSE_BUSY:
355 356 357 358 359 360
			if (i < AP_MAX_RESET - 1) {
				udelay(5);
				status = ap_queue_interruption_control(qid,
								       ind);
				continue;
			}
F
Felix Beck 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
			break;
		case AP_RESPONSE_Q_NOT_AVAIL:
		case AP_RESPONSE_DECONFIGURED:
		case AP_RESPONSE_CHECKSTOPPED:
		case AP_RESPONSE_INVALID_ADDRESS:
			return -ENODEV;
		case AP_RESPONSE_OTHERWISE_CHANGED:
			if (status.int_enabled)
				return 0;
			break;
		default:
			break;
		}
		if (i < AP_MAX_RESET - 1) {
			udelay(5);
			status = ap_test_queue(qid, &t_depth, &t_device_type);
		}
	}
	return rc;
#else
	return -EINVAL;
#endif
}

385
/**
386 387 388 389 390
 * __ap_send(): Send message to adjunct processor queue.
 * @qid: The AP queue number
 * @psmid: The program supplied message identifier
 * @msg: The message text
 * @length: The message length
391
 * @special: Special Bit
392
 *
393
 * Returns AP queue status structure.
394 395 396 397 398
 * Condition code 1 on NQAP can't happen because the L bit is 1.
 * Condition code 2 on NQAP also means the send is incomplete,
 * because a segment boundary was reached. The NQAP is repeated.
 */
static inline struct ap_queue_status
399 400
__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
	  unsigned int special)
401 402 403 404 405 406 407 408 409
{
	typedef struct { char _[length]; } msgblock;
	register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
	register struct ap_queue_status reg1 asm ("1");
	register unsigned long reg2 asm ("2") = (unsigned long) msg;
	register unsigned long reg3 asm ("3") = (unsigned long) length;
	register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
	register unsigned long reg5 asm ("5") = (unsigned int) psmid;

410 411 412
	if (special == 1)
		reg0 |= 0x400000UL;

413
	asm volatile (
414
		"0: .long 0xb2ad0042\n"		/* NQAP */
415 416 417 418 419 420 421 422 423 424 425
		"   brc   2,0b"
		: "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
		: "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
		: "cc" );
	return reg1;
}

int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
{
	struct ap_queue_status status;

426
	status = __ap_send(qid, psmid, msg, length, 0);
427 428 429 430
	switch (status.response_code) {
	case AP_RESPONSE_NORMAL:
		return 0;
	case AP_RESPONSE_Q_FULL:
431
	case AP_RESPONSE_RESET_IN_PROGRESS:
432
		return -EBUSY;
433 434
	case AP_RESPONSE_REQ_FAC_NOT_INST:
		return -EINVAL;
435 436 437 438 439 440
	default:	/* Device is gone. */
		return -ENODEV;
	}
}
EXPORT_SYMBOL(ap_send);

441 442 443 444 445 446
/**
 * __ap_recv(): Receive message from adjunct processor queue.
 * @qid: The AP queue number
 * @psmid: Pointer to program supplied message identifier
 * @msg: The message text
 * @length: The message length
447
 *
448
 * Returns AP queue status structure.
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
 * Condition code 1 on DQAP means the receive has taken place
 * but only partially.	The response is incomplete, hence the
 * DQAP is repeated.
 * Condition code 2 on DQAP also means the receive is incomplete,
 * this time because a segment boundary was reached. Again, the
 * DQAP is repeated.
 * Note that gpr2 is used by the DQAP instruction to keep track of
 * any 'residual' length, in case the instruction gets interrupted.
 * Hence it gets zeroed before the instruction.
 */
static inline struct ap_queue_status
__ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
{
	typedef struct { char _[length]; } msgblock;
	register unsigned long reg0 asm("0") = qid | 0x80000000UL;
	register struct ap_queue_status reg1 asm ("1");
	register unsigned long reg2 asm("2") = 0UL;
	register unsigned long reg4 asm("4") = (unsigned long) msg;
	register unsigned long reg5 asm("5") = (unsigned long) length;
	register unsigned long reg6 asm("6") = 0UL;
	register unsigned long reg7 asm("7") = 0UL;


	asm volatile(
473
		"0: .long 0xb2ae0064\n"		/* DQAP */
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
		"   brc   6,0b\n"
		: "+d" (reg0), "=d" (reg1), "+d" (reg2),
		"+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
		"=m" (*(msgblock *) msg) : : "cc" );
	*psmid = (((unsigned long long) reg6) << 32) + reg7;
	return reg1;
}

int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
{
	struct ap_queue_status status;

	status = __ap_recv(qid, psmid, msg, length);
	switch (status.response_code) {
	case AP_RESPONSE_NORMAL:
		return 0;
	case AP_RESPONSE_NO_PENDING_REPLY:
		if (status.queue_empty)
			return -ENOENT;
		return -EBUSY;
494 495
	case AP_RESPONSE_RESET_IN_PROGRESS:
		return -EBUSY;
496 497 498 499 500 501 502
	default:
		return -ENODEV;
	}
}
EXPORT_SYMBOL(ap_recv);

/**
503 504 505 506 507 508
 * 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
 *
 * The test is repeated for AP_MAX_RESET times.
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
 */
static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
{
	struct ap_queue_status status;
	int t_depth, t_device_type, rc, i;

	rc = -EBUSY;
	for (i = 0; i < AP_MAX_RESET; i++) {
		status = ap_test_queue(qid, &t_depth, &t_device_type);
		switch (status.response_code) {
		case AP_RESPONSE_NORMAL:
			*queue_depth = t_depth + 1;
			*device_type = t_device_type;
			rc = 0;
			break;
		case AP_RESPONSE_Q_NOT_AVAIL:
			rc = -ENODEV;
			break;
		case AP_RESPONSE_RESET_IN_PROGRESS:
			break;
		case AP_RESPONSE_DECONFIGURED:
			rc = -ENODEV;
			break;
		case AP_RESPONSE_CHECKSTOPPED:
			rc = -ENODEV;
			break;
F
Felix Beck 已提交
535 536 537 538 539
		case AP_RESPONSE_INVALID_ADDRESS:
			rc = -ENODEV;
			break;
		case AP_RESPONSE_OTHERWISE_CHANGED:
			break;
540 541 542 543 544 545 546 547 548 549 550 551 552 553
		case AP_RESPONSE_BUSY:
			break;
		default:
			BUG();
		}
		if (rc != -EBUSY)
			break;
		if (i < AP_MAX_RESET - 1)
			udelay(5);
	}
	return rc;
}

/**
554 555 556
 * ap_init_queue(): Reset an AP queue.
 * @qid: The AP queue number
 *
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
 * Reset an AP queue and wait for it to become available again.
 */
static int ap_init_queue(ap_qid_t qid)
{
	struct ap_queue_status status;
	int rc, dummy, i;

	rc = -ENODEV;
	status = ap_reset_queue(qid);
	for (i = 0; i < AP_MAX_RESET; i++) {
		switch (status.response_code) {
		case AP_RESPONSE_NORMAL:
			if (status.queue_empty)
				rc = 0;
			break;
		case AP_RESPONSE_Q_NOT_AVAIL:
		case AP_RESPONSE_DECONFIGURED:
		case AP_RESPONSE_CHECKSTOPPED:
			i = AP_MAX_RESET;	/* return with -ENODEV */
			break;
		case AP_RESPONSE_RESET_IN_PROGRESS:
578
			rc = -EBUSY;
579 580 581 582
		case AP_RESPONSE_BUSY:
		default:
			break;
		}
583
		if (rc != -ENODEV && rc != -EBUSY)
584 585 586 587 588 589
			break;
		if (i < AP_MAX_RESET - 1) {
			udelay(5);
			status = ap_test_queue(qid, &dummy, &dummy);
		}
	}
F
Felix Beck 已提交
590 591 592 593 594 595 596 597 598
	if (rc == 0 && ap_using_interrupts()) {
		rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
		/* If interruption mode is supported by the machine,
		* but an AP can not be enabled for interruption then
		* the AP will be discarded.    */
		if (rc)
			pr_err("Registering adapter interrupts for "
			       "AP %d failed\n", AP_QID_DEVICE(qid));
	}
599 600 601
	return rc;
}

602
/**
603 604 605 606
 * ap_increase_queue_count(): Arm request timeout.
 * @ap_dev: Pointer to an AP device.
 *
 * Arm request timeout if an AP device was idle and a new request is submitted.
607 608 609 610 611 612 613 614 615 616 617 618 619
 */
static void ap_increase_queue_count(struct ap_device *ap_dev)
{
	int timeout = ap_dev->drv->request_timeout;

	ap_dev->queue_count++;
	if (ap_dev->queue_count == 1) {
		mod_timer(&ap_dev->timeout, jiffies + timeout);
		ap_dev->reset = AP_RESET_ARMED;
	}
}

/**
620 621 622 623
 * ap_decrease_queue_count(): Decrease queue count.
 * @ap_dev: Pointer to an AP device.
 *
 * If AP device is still alive, re-schedule request timeout if there are still
624 625 626 627 628 629 630 631 632 633
 * pending requests.
 */
static void ap_decrease_queue_count(struct ap_device *ap_dev)
{
	int timeout = ap_dev->drv->request_timeout;

	ap_dev->queue_count--;
	if (ap_dev->queue_count > 0)
		mod_timer(&ap_dev->timeout, jiffies + timeout);
	else
634
		/*
635 636 637 638 639 640 641
		 * The timeout timer should to be disabled now - since
		 * del_timer_sync() is very expensive, we just tell via the
		 * reset flag to ignore the pending timeout timer.
		 */
		ap_dev->reset = AP_RESET_IGNORE;
}

642
/*
643 644 645 646 647 648 649 650 651
 * AP device related attributes.
 */
static ssize_t ap_hwtype_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
}

652
static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
653 654 655 656 657 658 659
static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
			     char *buf)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
}

660
static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
static ssize_t ap_request_count_show(struct device *dev,
				     struct device_attribute *attr,
				     char *buf)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	int rc;

	spin_lock_bh(&ap_dev->lock);
	rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
	spin_unlock_bh(&ap_dev->lock);
	return rc;
}

static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
static ssize_t ap_requestq_count_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	int rc;

	spin_lock_bh(&ap_dev->lock);
	rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
	spin_unlock_bh(&ap_dev->lock);
	return rc;
}

static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);

static ssize_t ap_pendingq_count_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	int rc;

	spin_lock_bh(&ap_dev->lock);
	rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
	spin_unlock_bh(&ap_dev->lock);
	return rc;
}

static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);

704 705 706 707 708 709 710 711
static ssize_t ap_modalias_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
}

static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);

712 713 714 715 716 717 718 719 720
static ssize_t ap_functions_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
}

static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);

721 722 723 724
static struct attribute *ap_dev_attrs[] = {
	&dev_attr_hwtype.attr,
	&dev_attr_depth.attr,
	&dev_attr_request_count.attr,
725 726
	&dev_attr_requestq_count.attr,
	&dev_attr_pendingq_count.attr,
727
	&dev_attr_modalias.attr,
728
	&dev_attr_ap_functions.attr,
729 730 731 732 733 734 735
	NULL
};
static struct attribute_group ap_dev_attr_group = {
	.attrs = ap_dev_attrs
};

/**
736 737 738 739
 * ap_bus_match()
 * @dev: Pointer to device
 * @drv: Pointer to device_driver
 *
740 741 742 743 744 745 746 747
 * AP bus driver registration/unregistration.
 */
static int ap_bus_match(struct device *dev, struct device_driver *drv)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	struct ap_driver *ap_drv = to_ap_drv(drv);
	struct ap_device_id *id;

748
	/*
749 750 751 752 753 754 755 756 757 758 759 760 761
	 * 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++) {
		if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
		    (id->dev_type != ap_dev->device_type))
			continue;
		return 1;
	}
	return 0;
}

/**
762 763 764 765 766 767
 * 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.
768
 */
769
static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
770 771
{
	struct ap_device *ap_dev = to_ap_dev(dev);
772
	int retval = 0;
773 774 775 776 777

	if (!ap_dev)
		return -ENODEV;

	/* Set up DEV_TYPE environment variable. */
778
	retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
779 780 781
	if (retval)
		return retval;

782
	/* Add MODALIAS= */
783
	retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
784 785

	return retval;
786 787
}

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
static int ap_bus_suspend(struct device *dev, pm_message_t state)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	unsigned long flags;

	if (!ap_suspend_flag) {
		ap_suspend_flag = 1;

		/* Disable scanning for devices, thus we do not want to scan
		 * for them after removing.
		 */
		del_timer_sync(&ap_config_timer);
		if (ap_work_queue != NULL) {
			destroy_workqueue(ap_work_queue);
			ap_work_queue = NULL;
		}
804

805 806 807 808 809
		tasklet_disable(&ap_tasklet);
	}
	/* Poll on the device until all requests are finished. */
	do {
		flags = 0;
810
		spin_lock_bh(&ap_dev->lock);
811
		__ap_poll_device(ap_dev, &flags);
812
		spin_unlock_bh(&ap_dev->lock);
813 814
	} while ((flags & 1) || (flags & 2));

815 816 817 818
	spin_lock_bh(&ap_dev->lock);
	ap_dev->unregistered = 1;
	spin_unlock_bh(&ap_dev->lock);

819 820 821 822 823 824 825 826 827 828 829 830
	return 0;
}

static int ap_bus_resume(struct device *dev)
{
	int rc = 0;
	struct ap_device *ap_dev = to_ap_dev(dev);

	if (ap_suspend_flag) {
		ap_suspend_flag = 0;
		if (!ap_interrupts_available())
			ap_interrupt_indicator = NULL;
831
		ap_query_configuration();
832 833 834 835
		if (!user_set_domain) {
			ap_domain_index = -1;
			ap_select_domain();
		}
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
		init_timer(&ap_config_timer);
		ap_config_timer.function = ap_config_timeout;
		ap_config_timer.data = 0;
		ap_config_timer.expires = jiffies + ap_config_time * HZ;
		add_timer(&ap_config_timer);
		ap_work_queue = create_singlethread_workqueue("kapwork");
		if (!ap_work_queue)
			return -ENOMEM;
		tasklet_enable(&ap_tasklet);
		if (!ap_using_interrupts())
			ap_schedule_poll_timer();
		else
			tasklet_schedule(&ap_tasklet);
		if (ap_thread_flag)
			rc = ap_poll_thread_start();
	}
852 853 854 855 856 857 858
	if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
		spin_lock_bh(&ap_dev->lock);
		ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
				       ap_domain_index);
		spin_unlock_bh(&ap_dev->lock);
	}
	queue_work(ap_work_queue, &ap_config_work);
859 860 861 862

	return rc;
}

863 864 865 866
static struct bus_type ap_bus_type = {
	.name = "ap",
	.match = &ap_bus_match,
	.uevent = &ap_uevent,
867 868
	.suspend = ap_bus_suspend,
	.resume = ap_bus_resume
869 870 871 872 873 874 875 876 877 878
};

static int ap_device_probe(struct device *dev)
{
	struct ap_device *ap_dev = to_ap_dev(dev);
	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
	int rc;

	ap_dev->drv = ap_drv;
	rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
879
	if (!rc) {
880
		spin_lock_bh(&ap_device_list_lock);
881
		list_add(&ap_dev->list, &ap_device_list);
882
		spin_unlock_bh(&ap_device_list_lock);
883
	}
884 885 886 887
	return rc;
}

/**
888 889 890
 * __ap_flush_queue(): Flush requests.
 * @ap_dev: Pointer to the AP device
 *
891 892
 * Flush all requests from the request/pending queue of an AP device.
 */
893
static void __ap_flush_queue(struct ap_device *ap_dev)
894 895 896 897 898 899
{
	struct ap_message *ap_msg, *next;

	list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
		list_del_init(&ap_msg->list);
		ap_dev->pendingq_count--;
900
		ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
901 902 903 904
	}
	list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
		list_del_init(&ap_msg->list);
		ap_dev->requestq_count--;
905
		ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	}
}

void ap_flush_queue(struct ap_device *ap_dev)
{
	spin_lock_bh(&ap_dev->lock);
	__ap_flush_queue(ap_dev);
	spin_unlock_bh(&ap_dev->lock);
}
EXPORT_SYMBOL(ap_flush_queue);

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;

922
	ap_flush_queue(ap_dev);
923
	del_timer_sync(&ap_dev->timeout);
924
	spin_lock_bh(&ap_device_list_lock);
925
	list_del_init(&ap_dev->list);
926
	spin_unlock_bh(&ap_device_list_lock);
927 928
	if (ap_drv->remove)
		ap_drv->remove(ap_dev);
929 930 931
	spin_lock_bh(&ap_dev->lock);
	atomic_sub(ap_dev->queue_count, &ap_poll_requests);
	spin_unlock_bh(&ap_dev->lock);
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
	return 0;
}

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

	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);

955 956 957 958 959 960 961 962 963 964 965 966 967 968
void ap_bus_force_rescan(void)
{
	/* Delete the AP bus rescan timer. */
	del_timer(&ap_config_timer);

	/* processing a synchonuous bus rescan */
	ap_scan_bus(NULL);

	/* Setup the AP bus rescan timer again. */
	ap_config_timer.expires = jiffies + ap_config_time * HZ;
	add_timer(&ap_config_timer);
}
EXPORT_SYMBOL(ap_bus_force_rescan);

969
/*
970 971 972 973 974 975 976 977 978 979 980 981 982 983
 * AP bus attributes.
 */
static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
}

static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);

static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
}

F
Felix Beck 已提交
984 985 986 987 988 989 990 991
static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n",
			ap_using_interrupts() ? 1 : 0);
}

static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
static ssize_t ap_config_time_store(struct bus_type *bus,
				    const char *buf, size_t count)
{
	int time;

	if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
		return -EINVAL;
	ap_config_time = time;
	if (!timer_pending(&ap_config_timer) ||
	    !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
		ap_config_timer.expires = jiffies + ap_config_time * HZ;
		add_timer(&ap_config_timer);
	}
	return count;
}

static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);

static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
}

static ssize_t ap_poll_thread_store(struct bus_type *bus,
				    const char *buf, size_t count)
{
	int flag, rc;

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

static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
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 已提交
1046 1047
	if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
	    time > 120000000000ULL)
1048 1049 1050 1051 1052
		return -EINVAL;
	poll_timeout = time;
	hr_time = ktime_set(0, poll_timeout);

	if (!hrtimer_is_queued(&ap_poll_timer) ||
1053 1054 1055
	    !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
		hrtimer_set_expires(&ap_poll_timer, hr_time);
		hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1056 1057 1058 1059 1060 1061
	}
	return count;
}

static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);

1062 1063 1064 1065
static struct bus_attribute *const ap_bus_attrs[] = {
	&bus_attr_ap_domain,
	&bus_attr_config_time,
	&bus_attr_poll_thread,
F
Felix Beck 已提交
1066
	&bus_attr_ap_interrupts,
1067 1068
	&bus_attr_poll_timeout,
	NULL,
1069 1070
};

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
static inline int ap_test_config(unsigned int *field, unsigned int nr)
{
	if (nr > 0xFFu)
		return 0;
	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)
		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)
		return 1;
	return ap_test_config(ap_configuration->aqm, domain);
}

/**
 * ap_query_configuration(): Query AP configuration information.
 *
 * Query information of installed cards and configured domains from AP.
 */
static void ap_query_configuration(void)
{
#ifdef CONFIG_64BIT
	if (ap_configuration_available()) {
		if (!ap_configuration)
			ap_configuration =
				kzalloc(sizeof(struct ap_config_info),
					GFP_KERNEL);
		if (ap_configuration)
			__ap_query_configuration(ap_configuration);
	} else
		ap_configuration = NULL;
#else
	ap_configuration = NULL;
#endif
}

1130
/**
1131 1132 1133
 * ap_select_domain(): Select an AP domain.
 *
 * Pick one of the 16 AP domains.
1134
 */
1135
static int ap_select_domain(void)
1136 1137
{
	int queue_depth, device_type, count, max_count, best_domain;
1138
	ap_qid_t qid;
1139 1140
	int rc, i, j;

1141
	/*
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	 * We want to use a single domain. Either the one specified with
	 * the "domain=" parameter or the domain with the maximum number
	 * of devices.
	 */
	if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
		/* Domain has already been selected. */
		return 0;
	best_domain = -1;
	max_count = 0;
	for (i = 0; i < AP_DOMAINS; i++) {
1152 1153
		if (!ap_test_config_domain(i))
			continue;
1154 1155
		count = 0;
		for (j = 0; j < AP_DEVICES; j++) {
1156 1157 1158
			if (!ap_test_config_card_id(j))
				continue;
			qid = AP_MKQID(j, i);
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
			rc = ap_query_queue(qid, &queue_depth, &device_type);
			if (rc)
				continue;
			count++;
		}
		if (count > max_count) {
			max_count = count;
			best_domain = i;
		}
	}
	if (best_domain >= 0){
		ap_domain_index = best_domain;
		return 0;
	}
	return -ENODEV;
}

/**
1177
 * ap_probe_device_type(): Find the device type of an AP.
1178
 * @ap_dev: pointer to the AP device.
1179 1180
 *
 * Find the device type if query queue returned a device type of 0.
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
 */
static int ap_probe_device_type(struct ap_device *ap_dev)
{
	static unsigned char msg[] = {
		0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
		0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
		0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
		0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
		0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
		0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
		0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
		0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
		0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
		0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
		0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
		0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
		0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
		0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
		0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
		0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
		0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
		0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
		0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
		0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
		0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
		0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
		0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
		0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
		0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
	};
	struct ap_queue_status status;
	unsigned long long psmid;
	char *reply;
	int rc, i;

	reply = (void *) get_zeroed_page(GFP_KERNEL);
	if (!reply) {
		rc = -ENOMEM;
		goto out;
	}

	status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1243
			   msg, sizeof(msg), 0);
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	if (status.response_code != AP_RESPONSE_NORMAL) {
		rc = -ENODEV;
		goto out_free;
	}

	/* Wait for the test message to complete. */
	for (i = 0; i < 6; i++) {
		mdelay(300);
		status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
		if (status.response_code == AP_RESPONSE_NORMAL &&
		    psmid == 0x0102030405060708ULL)
			break;
	}
	if (i < 6) {
		/* Got an answer. */
		if (reply[0] == 0x00 && reply[1] == 0x86)
			ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
		else
			ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
		rc = 0;
	} else
		rc = -ENODEV;

out_free:
	free_page((unsigned long) reply);
out:
	return rc;
}

F
Felix Beck 已提交
1273 1274
static void ap_interrupt_handler(void *unused1, void *unused2)
{
1275
	inc_irq_stat(IRQIO_APB);
F
Felix Beck 已提交
1276 1277 1278
	tasklet_schedule(&ap_tasklet);
}

1279
/**
1280 1281 1282 1283 1284
 * __ap_scan_bus(): Scan the AP bus.
 * @dev: Pointer to device
 * @data: Pointer to data
 *
 * Scan the AP bus for new devices.
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
 */
static int __ap_scan_bus(struct device *dev, void *data)
{
	return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
}

static void ap_device_release(struct device *dev)
{
	struct ap_device *ap_dev = to_ap_dev(dev);

	kfree(ap_dev);
}

1298
static void ap_scan_bus(struct work_struct *unused)
1299 1300 1301 1302 1303
{
	struct ap_device *ap_dev;
	struct device *dev;
	ap_qid_t qid;
	int queue_depth, device_type;
1304
	unsigned int device_functions;
1305 1306
	int rc, i;

1307
	ap_query_configuration();
1308 1309 1310 1311 1312 1313 1314
	if (ap_select_domain() != 0)
		return;
	for (i = 0; i < AP_DEVICES; i++) {
		qid = AP_MKQID(i, ap_domain_index);
		dev = bus_find_device(&ap_bus_type, NULL,
				      (void *)(unsigned long)qid,
				      __ap_scan_bus);
1315 1316 1317 1318
		if (ap_test_config_card_id(i))
			rc = ap_query_queue(qid, &queue_depth, &device_type);
		else
			rc = -ENODEV;
1319
		if (dev) {
1320 1321 1322 1323 1324 1325
			if (rc == -EBUSY) {
				set_current_state(TASK_UNINTERRUPTIBLE);
				schedule_timeout(AP_RESET_TIMEOUT);
				rc = ap_query_queue(qid, &queue_depth,
						    &device_type);
			}
1326 1327 1328 1329
			ap_dev = to_ap_dev(dev);
			spin_lock_bh(&ap_dev->lock);
			if (rc || ap_dev->unregistered) {
				spin_unlock_bh(&ap_dev->lock);
1330 1331
				if (ap_dev->unregistered)
					i--;
1332
				device_unregister(dev);
1333
				put_device(dev);
1334
				continue;
1335 1336
			}
			spin_unlock_bh(&ap_dev->lock);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
			put_device(dev);
			continue;
		}
		if (rc)
			continue;
		rc = ap_init_queue(qid);
		if (rc)
			continue;
		ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
		if (!ap_dev)
			break;
		ap_dev->qid = qid;
		ap_dev->queue_depth = queue_depth;
1350
		ap_dev->unregistered = 1;
1351 1352 1353
		spin_lock_init(&ap_dev->lock);
		INIT_LIST_HEAD(&ap_dev->pendingq);
		INIT_LIST_HEAD(&ap_dev->requestq);
1354
		INIT_LIST_HEAD(&ap_dev->list);
1355 1356
		setup_timer(&ap_dev->timeout, ap_request_timeout,
			    (unsigned long) ap_dev);
1357 1358
		switch (device_type) {
		case 0:
1359
			/* device type probing for old cards */
1360 1361 1362 1363
			if (ap_probe_device_type(ap_dev)) {
				kfree(ap_dev);
				continue;
			}
1364 1365
			break;
		default:
1366
			ap_dev->device_type = device_type;
1367
		}
1368

1369 1370 1371 1372 1373 1374
		rc = ap_query_functions(qid, &device_functions);
		if (!rc)
			ap_dev->functions = device_functions;
		else
			ap_dev->functions = 0u;

1375 1376
		ap_dev->device.bus = &ap_bus_type;
		ap_dev->device.parent = ap_root_device;
1377 1378 1379 1380 1381
		if (dev_set_name(&ap_dev->device, "card%02x",
				 AP_QID_DEVICE(ap_dev->qid))) {
			kfree(ap_dev);
			continue;
		}
1382 1383 1384
		ap_dev->device.release = ap_device_release;
		rc = device_register(&ap_dev->device);
		if (rc) {
1385
			put_device(&ap_dev->device);
1386 1387 1388 1389 1390
			continue;
		}
		/* Add device attributes. */
		rc = sysfs_create_group(&ap_dev->device.kobj,
					&ap_dev_attr_group);
1391 1392 1393 1394 1395 1396
		if (!rc) {
			spin_lock_bh(&ap_dev->lock);
			ap_dev->unregistered = 0;
			spin_unlock_bh(&ap_dev->lock);
		}
		else
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
			device_unregister(&ap_dev->device);
	}
}

static void
ap_config_timeout(unsigned long ptr)
{
	queue_work(ap_work_queue, &ap_config_work);
	ap_config_timer.expires = jiffies + ap_config_time * HZ;
	add_timer(&ap_config_timer);
}

/**
1410
 * __ap_schedule_poll_timer(): Schedule poll timer.
1411
 *
1412 1413
 * Set up the timer to run the poll tasklet
 */
1414
static inline void __ap_schedule_poll_timer(void)
1415
{
1416
	ktime_t hr_time;
1417 1418

	spin_lock_bh(&ap_poll_timer_lock);
1419
	if (hrtimer_is_queued(&ap_poll_timer) || ap_suspend_flag)
1420
		goto out;
1421 1422 1423 1424 1425
	if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
		hr_time = ktime_set(0, poll_timeout);
		hrtimer_forward_now(&ap_poll_timer, hr_time);
		hrtimer_restart(&ap_poll_timer);
	}
1426 1427
out:
	spin_unlock_bh(&ap_poll_timer_lock);
1428 1429
}

1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
/**
 * ap_schedule_poll_timer(): Schedule poll timer.
 *
 * Set up the timer to run the poll tasklet
 */
static inline void ap_schedule_poll_timer(void)
{
	if (ap_using_interrupts())
		return;
	__ap_schedule_poll_timer();
}

1442
/**
1443
 * ap_poll_read(): Receive pending reply messages from an AP device.
1444 1445 1446
 * @ap_dev: pointer to the AP device
 * @flags: pointer to control flags, bit 2^0 is set if another poll is
 *	   required, bit 2^1 is set if the poll timer needs to get armed
1447
 *
1448 1449
 * Returns 0 if the device is still present, -ENODEV if not.
 */
1450
static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
{
	struct ap_queue_status status;
	struct ap_message *ap_msg;

	if (ap_dev->queue_count <= 0)
		return 0;
	status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
			   ap_dev->reply->message, ap_dev->reply->length);
	switch (status.response_code) {
	case AP_RESPONSE_NORMAL:
		atomic_dec(&ap_poll_requests);
1462
		ap_decrease_queue_count(ap_dev);
1463 1464 1465 1466 1467
		list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
			if (ap_msg->psmid != ap_dev->reply->psmid)
				continue;
			list_del_init(&ap_msg->list);
			ap_dev->pendingq_count--;
1468
			ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
1469 1470 1471 1472 1473 1474 1475 1476
			break;
		}
		if (ap_dev->queue_count > 0)
			*flags |= 1;
		break;
	case AP_RESPONSE_NO_PENDING_REPLY:
		if (status.queue_empty) {
			/* The card shouldn't forget requests but who knows. */
1477
			atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
			ap_dev->queue_count = 0;
			list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
			ap_dev->requestq_count += ap_dev->pendingq_count;
			ap_dev->pendingq_count = 0;
		} else
			*flags |= 2;
		break;
	default:
		return -ENODEV;
	}
	return 0;
}

/**
1492
 * ap_poll_write(): Send messages from the request queue to an AP device.
1493 1494 1495
 * @ap_dev: pointer to the AP device
 * @flags: pointer to control flags, bit 2^0 is set if another poll is
 *	   required, bit 2^1 is set if the poll timer needs to get armed
1496
 *
1497 1498
 * Returns 0 if the device is still present, -ENODEV if not.
 */
1499
static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
{
	struct ap_queue_status status;
	struct ap_message *ap_msg;

	if (ap_dev->requestq_count <= 0 ||
	    ap_dev->queue_count >= ap_dev->queue_depth)
		return 0;
	/* Start the next request on the queue. */
	ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
	status = __ap_send(ap_dev->qid, ap_msg->psmid,
1510
			   ap_msg->message, ap_msg->length, ap_msg->special);
1511 1512 1513
	switch (status.response_code) {
	case AP_RESPONSE_NORMAL:
		atomic_inc(&ap_poll_requests);
1514
		ap_increase_queue_count(ap_dev);
1515 1516 1517 1518 1519 1520 1521 1522
		list_move_tail(&ap_msg->list, &ap_dev->pendingq);
		ap_dev->requestq_count--;
		ap_dev->pendingq_count++;
		if (ap_dev->queue_count < ap_dev->queue_depth &&
		    ap_dev->requestq_count > 0)
			*flags |= 1;
		*flags |= 2;
		break;
1523
	case AP_RESPONSE_RESET_IN_PROGRESS:
1524 1525
		__ap_schedule_poll_timer();
	case AP_RESPONSE_Q_FULL:
1526 1527 1528
		*flags |= 2;
		break;
	case AP_RESPONSE_MESSAGE_TOO_BIG:
1529
	case AP_RESPONSE_REQ_FAC_NOT_INST:
1530 1531 1532 1533 1534 1535 1536 1537
		return -EINVAL;
	default:
		return -ENODEV;
	}
	return 0;
}

/**
1538
 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1539 1540 1541
 * @ap_dev: pointer to the bus device
 * @flags: pointer to control flags, bit 2^0 is set if another poll is
 *	   required, bit 2^1 is set if the poll timer needs to get armed
1542 1543 1544
 *
 * Poll AP device for pending replies and send new messages. If either
 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
 * Returns 0.
 */
static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
{
	int rc;

	rc = ap_poll_read(ap_dev, flags);
	if (rc)
		return rc;
	return ap_poll_write(ap_dev, flags);
}

/**
1558
 * __ap_queue_message(): Queue a message to a device.
1559 1560
 * @ap_dev: pointer to the AP device
 * @ap_msg: the message to be queued
1561 1562
 *
 * Queue a message to a device. Returns 0 if successful.
1563 1564 1565 1566 1567 1568 1569 1570
 */
static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
{
	struct ap_queue_status status;

	if (list_empty(&ap_dev->requestq) &&
	    ap_dev->queue_count < ap_dev->queue_depth) {
		status = __ap_send(ap_dev->qid, ap_msg->psmid,
1571 1572
				   ap_msg->message, ap_msg->length,
				   ap_msg->special);
1573 1574 1575 1576 1577
		switch (status.response_code) {
		case AP_RESPONSE_NORMAL:
			list_add_tail(&ap_msg->list, &ap_dev->pendingq);
			atomic_inc(&ap_poll_requests);
			ap_dev->pendingq_count++;
1578
			ap_increase_queue_count(ap_dev);
1579 1580 1581
			ap_dev->total_request_count++;
			break;
		case AP_RESPONSE_Q_FULL:
1582
		case AP_RESPONSE_RESET_IN_PROGRESS:
1583 1584 1585 1586
			list_add_tail(&ap_msg->list, &ap_dev->requestq);
			ap_dev->requestq_count++;
			ap_dev->total_request_count++;
			return -EBUSY;
1587
		case AP_RESPONSE_REQ_FAC_NOT_INST:
1588
		case AP_RESPONSE_MESSAGE_TOO_BIG:
1589
			ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1590 1591
			return -EINVAL;
		default:	/* Device is gone. */
1592
			ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
			return -ENODEV;
		}
	} else {
		list_add_tail(&ap_msg->list, &ap_dev->requestq);
		ap_dev->requestq_count++;
		ap_dev->total_request_count++;
		return -EBUSY;
	}
	ap_schedule_poll_timer();
	return 0;
}

void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
{
	unsigned long flags;
	int rc;

1610 1611 1612 1613
	/* For asynchronous message handling a valid receive-callback
	 * is required. */
	BUG_ON(!ap_msg->receive);

1614 1615 1616 1617 1618 1619 1620 1621
	spin_lock_bh(&ap_dev->lock);
	if (!ap_dev->unregistered) {
		/* Make room on the queue by polling for finished requests. */
		rc = ap_poll_queue(ap_dev, &flags);
		if (!rc)
			rc = __ap_queue_message(ap_dev, ap_msg);
		if (!rc)
			wake_up(&ap_poll_wait);
1622 1623
		if (rc == -ENODEV)
			ap_dev->unregistered = 1;
1624
	} else {
1625
		ap_msg->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1626
		rc = -ENODEV;
1627 1628 1629 1630 1631 1632 1633 1634
	}
	spin_unlock_bh(&ap_dev->lock);
	if (rc == -ENODEV)
		device_unregister(&ap_dev->device);
}
EXPORT_SYMBOL(ap_queue_message);

/**
1635 1636 1637 1638
 * ap_cancel_message(): Cancel a crypto request.
 * @ap_dev: The AP device that has the message queued
 * @ap_msg: The message that is to be removed
 *
1639
 * Cancel a crypto request. This is done by removing the request
1640
 * from the device pending or request queue. Note that the
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
 * request stays on the AP queue. When it finishes the message
 * reply will be discarded because the psmid can't be found.
 */
void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
{
	struct ap_message *tmp;

	spin_lock_bh(&ap_dev->lock);
	if (!list_empty(&ap_msg->list)) {
		list_for_each_entry(tmp, &ap_dev->pendingq, list)
			if (tmp->psmid == ap_msg->psmid) {
				ap_dev->pendingq_count--;
				goto found;
			}
		ap_dev->requestq_count--;
	found:
		list_del_init(&ap_msg->list);
	}
	spin_unlock_bh(&ap_dev->lock);
}
EXPORT_SYMBOL(ap_cancel_message);

/**
1664
 * ap_poll_timeout(): AP receive polling for finished AP requests.
1665
 * @unused: Unused pointer.
1666
 *
1667
 * Schedules the AP tasklet using a high resolution timer.
1668
 */
1669
static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1670 1671
{
	tasklet_schedule(&ap_tasklet);
1672
	return HRTIMER_NORESTART;
1673 1674
}

1675
/**
1676 1677 1678
 * ap_reset(): Reset a not responding AP device.
 * @ap_dev: Pointer to the AP device
 *
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
 * Reset a not responding AP device and move all requests from the
 * pending queue to the request queue.
 */
static void ap_reset(struct ap_device *ap_dev)
{
	int rc;

	ap_dev->reset = AP_RESET_IGNORE;
	atomic_sub(ap_dev->queue_count, &ap_poll_requests);
	ap_dev->queue_count = 0;
	list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
	ap_dev->requestq_count += ap_dev->pendingq_count;
	ap_dev->pendingq_count = 0;
	rc = ap_init_queue(ap_dev->qid);
	if (rc == -ENODEV)
		ap_dev->unregistered = 1;
1695 1696
	else
		__ap_schedule_poll_timer();
1697 1698
}

1699
static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1700 1701
{
	if (!ap_dev->unregistered) {
1702
		if (ap_poll_queue(ap_dev, flags))
1703
			ap_dev->unregistered = 1;
1704 1705
		if (ap_dev->reset == AP_RESET_DO)
			ap_reset(ap_dev);
1706
	}
1707 1708 1709
	return 0;
}

1710 1711 1712 1713 1714 1715 1716 1717
/**
 * ap_poll_all(): Poll all AP devices.
 * @dummy: Unused variable
 *
 * Poll all AP devices on the bus in a round robin fashion. Continue
 * polling until bit 2^0 of the control flags is not set. If bit 2^1
 * of the control flags has been set arm the poll timer.
 */
1718 1719 1720
static void ap_poll_all(unsigned long dummy)
{
	unsigned long flags;
1721
	struct ap_device *ap_dev;
1722

F
Felix Beck 已提交
1723 1724 1725 1726 1727 1728
	/* 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((u8 *)ap_interrupt_indicator, 0);
1729 1730
	do {
		flags = 0;
1731
		spin_lock(&ap_device_list_lock);
1732
		list_for_each_entry(ap_dev, &ap_device_list, list) {
1733
			spin_lock(&ap_dev->lock);
1734
			__ap_poll_device(ap_dev, &flags);
1735
			spin_unlock(&ap_dev->lock);
1736
		}
1737
		spin_unlock(&ap_device_list_lock);
1738 1739 1740 1741 1742 1743
	} while (flags & 1);
	if (flags & 2)
		ap_schedule_poll_timer();
}

/**
1744 1745 1746
 * ap_poll_thread(): Thread that polls for finished requests.
 * @data: Unused pointer
 *
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
 * 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);
	unsigned long flags;
	int requests;
1758
	struct ap_device *ap_dev;
1759

1760
	set_user_nice(current, 19);
1761
	while (1) {
1762 1763
		if (ap_suspend_flag)
			return 0;
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
		if (need_resched()) {
			schedule();
			continue;
		}
		add_wait_queue(&ap_poll_wait, &wait);
		set_current_state(TASK_INTERRUPTIBLE);
		if (kthread_should_stop())
			break;
		requests = atomic_read(&ap_poll_requests);
		if (requests <= 0)
			schedule();
		set_current_state(TASK_RUNNING);
		remove_wait_queue(&ap_poll_wait, &wait);

		flags = 0;
1779
		spin_lock_bh(&ap_device_list_lock);
1780
		list_for_each_entry(ap_dev, &ap_device_list, list) {
1781
			spin_lock(&ap_dev->lock);
1782
			__ap_poll_device(ap_dev, &flags);
1783
			spin_unlock(&ap_dev->lock);
1784
		}
1785
		spin_unlock_bh(&ap_device_list_lock);
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&ap_poll_wait, &wait);
	return 0;
}

static int ap_poll_thread_start(void)
{
	int rc;

1796
	if (ap_using_interrupts() || ap_suspend_flag)
F
Felix Beck 已提交
1797
		return 0;
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
	mutex_lock(&ap_poll_thread_mutex);
	if (!ap_poll_kthread) {
		ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
		rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
		if (rc)
			ap_poll_kthread = NULL;
	}
	else
		rc = 0;
	mutex_unlock(&ap_poll_thread_mutex);
	return rc;
}

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

1821
/**
1822 1823 1824 1825
 * ap_request_timeout(): Handling of request timeouts
 * @data: Holds the AP device.
 *
 * Handles request timeouts.
1826 1827 1828 1829 1830
 */
static void ap_request_timeout(unsigned long data)
{
	struct ap_device *ap_dev = (struct ap_device *) data;

F
Felix Beck 已提交
1831
	if (ap_dev->reset == AP_RESET_ARMED) {
1832
		ap_dev->reset = AP_RESET_DO;
F
Felix Beck 已提交
1833 1834 1835 1836

		if (ap_using_interrupts())
			tasklet_schedule(&ap_tasklet);
	}
1837 1838
}

1839 1840 1841 1842
static void ap_reset_domain(void)
{
	int i;

1843 1844 1845
	if (ap_domain_index != -1)
		for (i = 0; i < AP_DEVICES; i++)
			ap_reset_queue(AP_MKQID(i, ap_domain_index));
1846 1847 1848
}

static void ap_reset_all(void)
1849 1850 1851 1852 1853 1854 1855 1856 1857
{
	int i, j;

	for (i = 0; i < AP_DOMAINS; i++)
		for (j = 0; j < AP_DEVICES; j++)
			ap_reset_queue(AP_MKQID(j, i));
}

static struct reset_call ap_reset_call = {
1858
	.fn = ap_reset_all,
1859 1860
};

1861
/**
1862 1863 1864
 * ap_module_init(): The module initialization code.
 *
 * Initializes the module.
1865 1866 1867 1868 1869 1870
 */
int __init ap_module_init(void)
{
	int rc, i;

	if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1871 1872
		pr_warning("%d is not a valid cryptographic domain\n",
			   ap_domain_index);
1873 1874
		return -EINVAL;
	}
1875 1876 1877 1878 1879 1880
	/* 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;

1881
	if (ap_instructions_available() != 0) {
1882 1883
		pr_warning("The hardware system does not support "
			   "AP instructions\n");
1884 1885
		return -ENODEV;
	}
F
Felix Beck 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
	if (ap_interrupts_available()) {
		isc_register(AP_ISC);
		ap_interrupt_indicator = s390_register_adapter_interrupt(
			&ap_interrupt_handler, NULL, AP_ISC);
		if (IS_ERR(ap_interrupt_indicator)) {
			ap_interrupt_indicator = NULL;
			isc_unregister(AP_ISC);
		}
	}

1896
	register_reset_call(&ap_reset_call);
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908

	/* 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 已提交
1909
	ap_root_device = root_device_register("ap");
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
	rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
	if (rc)
		goto out_bus;

	ap_work_queue = create_singlethread_workqueue("kapwork");
	if (!ap_work_queue) {
		rc = -ENOMEM;
		goto out_root;
	}

1920
	ap_query_configuration();
1921 1922 1923
	if (ap_select_domain() == 0)
		ap_scan_bus(NULL);

1924
	/* Setup the AP bus rescan timer. */
1925 1926 1927 1928 1929 1930
	init_timer(&ap_config_timer);
	ap_config_timer.function = ap_config_timeout;
	ap_config_timer.data = 0;
	ap_config_timer.expires = jiffies + ap_config_time * HZ;
	add_timer(&ap_config_timer);

1931 1932 1933 1934 1935
	/* Setup the high resultion poll timer.
	 * If we are running under z/VM adjust polling to z/VM polling rate.
	 */
	if (MACHINE_IS_VM)
		poll_timeout = 1500000;
1936
	spin_lock_init(&ap_poll_timer_lock);
1937 1938 1939
	hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
	ap_poll_timer.function = ap_poll_timeout;

1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
	/* Start the low priority AP bus poll thread. */
	if (ap_thread_flag) {
		rc = ap_poll_thread_start();
		if (rc)
			goto out_work;
	}

	return 0;

out_work:
	del_timer_sync(&ap_config_timer);
1951
	hrtimer_cancel(&ap_poll_timer);
1952 1953
	destroy_workqueue(ap_work_queue);
out_root:
M
Mark McLoughlin 已提交
1954
	root_device_unregister(ap_root_device);
1955 1956 1957 1958 1959
out_bus:
	while (i--)
		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
	bus_unregister(&ap_bus_type);
out:
1960
	unregister_reset_call(&ap_reset_call);
F
Felix Beck 已提交
1961 1962 1963 1964
	if (ap_using_interrupts()) {
		s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
		isc_unregister(AP_ISC);
	}
1965 1966 1967 1968 1969 1970 1971 1972 1973
	return rc;
}

static int __ap_match_all(struct device *dev, void *data)
{
	return 1;
}

/**
1974 1975 1976
 * ap_modules_exit(): The module termination code
 *
 * Terminates the module.
1977 1978 1979 1980 1981 1982
 */
void ap_module_exit(void)
{
	int i;
	struct device *dev;

1983
	ap_reset_domain();
1984 1985
	ap_poll_thread_stop();
	del_timer_sync(&ap_config_timer);
1986
	hrtimer_cancel(&ap_poll_timer);
1987
	destroy_workqueue(ap_work_queue);
1988
	tasklet_kill(&ap_tasklet);
M
Mark McLoughlin 已提交
1989
	root_device_unregister(ap_root_device);
1990 1991 1992 1993 1994 1995 1996 1997 1998
	while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
		    __ap_match_all)))
	{
		device_unregister(dev);
		put_device(dev);
	}
	for (i = 0; ap_bus_attrs[i]; i++)
		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
	bus_unregister(&ap_bus_type);
1999
	unregister_reset_call(&ap_reset_call);
F
Felix Beck 已提交
2000 2001 2002 2003
	if (ap_using_interrupts()) {
		s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
		isc_unregister(AP_ISC);
	}
2004 2005 2006 2007
}

module_init(ap_module_init);
module_exit(ap_module_exit);