Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 * linux/drivers/s390/crypto/ap_bus.c
   3 *
   4 * Copyright (C) 2006 IBM Corporation
   5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
   6 *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
   7 *	      Ralph Wuerthner <rwuerthn@de.ibm.com>
   8 *	      Felix Beck <felix.beck@de.ibm.com>
   9 *	      Holger Dengler <hd@linux.vnet.ibm.com>
  10 *
  11 * Adjunct processor bus.
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of the GNU General Public License as published by
  15 * the Free Software Foundation; either version 2, or (at your option)
  16 * any later version.
  17 *
  18 * This program is distributed in the hope that it will be useful,
  19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21 * GNU General Public License for more details.
  22 *
  23 * You should have received a copy of the GNU General Public License
  24 * along with this program; if not, write to the Free Software
  25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26 */
  27
  28#define KMSG_COMPONENT "ap"
  29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  30
  31#include <linux/kernel_stat.h>
  32#include <linux/module.h>
  33#include <linux/init.h>
  34#include <linux/delay.h>
  35#include <linux/err.h>
  36#include <linux/interrupt.h>
  37#include <linux/workqueue.h>
  38#include <linux/slab.h>
  39#include <linux/notifier.h>
  40#include <linux/kthread.h>
  41#include <linux/mutex.h>
 
  42#include <asm/reset.h>
  43#include <asm/airq.h>
  44#include <linux/atomic.h>
  45#include <asm/system.h>
  46#include <asm/isc.h>
  47#include <linux/hrtimer.h>
  48#include <linux/ktime.h>
 
 
 
 
  49
  50#include "ap_bus.h"
  51
  52/* Some prototypes. */
  53static void ap_scan_bus(struct work_struct *);
  54static void ap_poll_all(unsigned long);
  55static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
  56static int ap_poll_thread_start(void);
  57static void ap_poll_thread_stop(void);
  58static void ap_request_timeout(unsigned long);
  59static inline void ap_schedule_poll_timer(void);
  60static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
  61static int ap_device_remove(struct device *dev);
  62static int ap_device_probe(struct device *dev);
  63static void ap_interrupt_handler(void *unused1, void *unused2);
  64static void ap_reset(struct ap_device *ap_dev);
  65static void ap_config_timeout(unsigned long ptr);
  66static int ap_select_domain(void);
  67
  68/*
  69 * Module description.
  70 */
  71MODULE_AUTHOR("IBM Corporation");
  72MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
  73		   "Copyright 2006 IBM Corporation");
  74MODULE_LICENSE("GPL");
 
  75
  76/*
  77 * Module parameter
  78 */
  79int ap_domain_index = -1;	/* Adjunct Processor Domain Index */
  80module_param_named(domain, ap_domain_index, int, 0000);
 
  81MODULE_PARM_DESC(domain, "domain index for ap devices");
  82EXPORT_SYMBOL(ap_domain_index);
  83
  84static int ap_thread_flag = 0;
  85module_param_named(poll_thread, ap_thread_flag, int, 0000);
  86MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
  87
  88static struct device *ap_root_device = NULL;
  89static DEFINE_SPINLOCK(ap_device_list_lock);
  90static LIST_HEAD(ap_device_list);
 
 
 
 
  91
  92/*
  93 * Workqueue & timer for bus rescan.
 
 
 
 
 
 
  94 */
  95static struct workqueue_struct *ap_work_queue;
  96static struct timer_list ap_config_timer;
  97static int ap_config_time = AP_CONFIG_TIME;
  98static DECLARE_WORK(ap_config_work, ap_scan_bus);
 
  99
 100/*
 101 * Tasklet & timer for AP request polling and interrupts
 102 */
 103static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
 104static atomic_t ap_poll_requests = ATOMIC_INIT(0);
 105static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
 106static struct task_struct *ap_poll_kthread = NULL;
 107static DEFINE_MUTEX(ap_poll_thread_mutex);
 108static DEFINE_SPINLOCK(ap_poll_timer_lock);
 109static void *ap_interrupt_indicator;
 110static struct hrtimer ap_poll_timer;
 111/* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
 112 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
 113static unsigned long long poll_timeout = 250000;
 114
 115/* Suspend flag */
 116static int ap_suspend_flag;
 
 
 117/* Flag to check if domain was set through module parameter domain=. This is
 118 * important when supsend and resume is done in a z/VM environment where the
 119 * domain might change. */
 120static int user_set_domain = 0;
 121static struct bus_type ap_bus_type;
 122
 
 
 
 
 
 
 
 
 
 
 123/**
 124 * ap_using_interrupts() - Returns non-zero if interrupt support is
 125 * available.
 126 */
 127static inline int ap_using_interrupts(void)
 128{
 129	return ap_interrupt_indicator != NULL;
 130}
 131
 132/**
 133 * ap_intructions_available() - Test if AP instructions are available.
 134 *
 135 * Returns 0 if the AP instructions are installed.
 
 
 136 */
 137static inline int ap_instructions_available(void)
 138{
 139	register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
 140	register unsigned long reg1 asm ("1") = -ENODEV;
 141	register unsigned long reg2 asm ("2") = 0UL;
 142
 143	asm volatile(
 144		"   .long 0xb2af0000\n"		/* PQAP(TAPQ) */
 145		"0: la    %1,0\n"
 146		"1:\n"
 147		EX_TABLE(0b, 1b)
 148		: "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
 149	return reg1;
 150}
 151
 152/**
 153 * ap_interrupts_available(): Test if AP interrupts are available.
 154 *
 155 * Returns 1 if AP interrupts are available.
 156 */
 157static int ap_interrupts_available(void)
 158{
 159	return test_facility(2) && test_facility(65);
 160}
 161
 162/**
 163 * ap_test_queue(): Test adjunct processor queue.
 164 * @qid: The AP queue number
 165 * @queue_depth: Pointer to queue depth value
 166 * @device_type: Pointer to device type value
 167 *
 168 * Returns AP queue status structure.
 169 */
 170static inline struct ap_queue_status
 171ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
 172{
 173	register unsigned long reg0 asm ("0") = qid;
 174	register struct ap_queue_status reg1 asm ("1");
 175	register unsigned long reg2 asm ("2") = 0UL;
 176
 177	asm volatile(".long 0xb2af0000"		/* PQAP(TAPQ) */
 178		     : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
 179	*device_type = (int) (reg2 >> 24);
 180	*queue_depth = (int) (reg2 & 0xff);
 181	return reg1;
 182}
 183
 184/**
 185 * ap_reset_queue(): Reset adjunct processor queue.
 186 * @qid: The AP queue number
 
 187 *
 188 * Returns AP queue status structure.
 189 */
 190static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
 191{
 192	register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
 193	register struct ap_queue_status reg1 asm ("1");
 194	register unsigned long reg2 asm ("2") = 0UL;
 195
 196	asm volatile(
 197		".long 0xb2af0000"		/* PQAP(RAPQ) */
 198		: "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
 199	return reg1;
 200}
 201
 202#ifdef CONFIG_64BIT
 203/**
 204 * ap_queue_interruption_control(): Enable interruption for a specific AP.
 205 * @qid: The AP queue number
 206 * @ind: The notification indicator byte
 207 *
 208 * Returns AP queue status.
 209 */
 210static inline struct ap_queue_status
 211ap_queue_interruption_control(ap_qid_t qid, void *ind)
 212{
 213	register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
 214	register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
 215	register struct ap_queue_status reg1_out asm ("1");
 216	register void *reg2 asm ("2") = ind;
 217	asm volatile(
 218		".long 0xb2af0000"		/* PQAP(RAPQ) */
 219		: "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
 220		:
 221		: "cc" );
 222	return reg1_out;
 223}
 224#endif
 225
 226#ifdef CONFIG_64BIT
 227static inline struct ap_queue_status
 228__ap_query_functions(ap_qid_t qid, unsigned int *functions)
 229{
 230	register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
 231	register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
 232	register unsigned long reg2 asm ("2");
 233
 234	asm volatile(
 235		".long 0xb2af0000\n"
 236		"0:\n"
 237		EX_TABLE(0b, 0b)
 238		: "+d" (reg0), "+d" (reg1), "=d" (reg2)
 239		:
 240		: "cc");
 241
 242	*functions = (unsigned int)(reg2 >> 32);
 243	return reg1;
 244}
 245#endif
 246
 247/**
 248 * ap_query_functions(): Query supported functions.
 249 * @qid: The AP queue number
 250 * @functions: Pointer to functions field.
 251 *
 252 * Returns
 253 *   0	     on success.
 254 *   -ENODEV  if queue not valid.
 255 *   -EBUSY   if device busy.
 256 *   -EINVAL  if query function is not supported
 257 */
 258static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
 259{
 260#ifdef CONFIG_64BIT
 261	struct ap_queue_status status;
 262	int i;
 263	status = __ap_query_functions(qid, functions);
 264
 265	for (i = 0; i < AP_MAX_RESET; i++) {
 266		if (ap_queue_status_invalid_test(&status))
 267			return -ENODEV;
 268
 269		switch (status.response_code) {
 270		case AP_RESPONSE_NORMAL:
 271			return 0;
 272		case AP_RESPONSE_RESET_IN_PROGRESS:
 273		case AP_RESPONSE_BUSY:
 274			break;
 275		case AP_RESPONSE_Q_NOT_AVAIL:
 276		case AP_RESPONSE_DECONFIGURED:
 277		case AP_RESPONSE_CHECKSTOPPED:
 278		case AP_RESPONSE_INVALID_ADDRESS:
 279			return -ENODEV;
 280		case AP_RESPONSE_OTHERWISE_CHANGED:
 281			break;
 282		default:
 283			break;
 284		}
 285		if (i < AP_MAX_RESET - 1) {
 286			udelay(5);
 287			status = __ap_query_functions(qid, functions);
 288		}
 289	}
 290	return -EBUSY;
 291#else
 292	return -EINVAL;
 293#endif
 294}
 295
 296/**
 297 * ap_4096_commands_availablen(): Check for availability of 4096 bit RSA
 298 * support.
 299 * @qid: The AP queue number
 300 *
 301 * Returns 1 if 4096 bit RSA keys are support fo the AP, returns 0 if not.
 302 */
 303int ap_4096_commands_available(ap_qid_t qid)
 304{
 305	unsigned int functions;
 306
 307	if (ap_query_functions(qid, &functions))
 308		return 0;
 309
 310	return test_ap_facility(functions, 1) &&
 311	       test_ap_facility(functions, 2);
 312}
 313EXPORT_SYMBOL(ap_4096_commands_available);
 314
 315/**
 316 * ap_queue_enable_interruption(): Enable interruption on an AP.
 317 * @qid: The AP queue number
 318 * @ind: the notification indicator byte
 319 *
 320 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
 321 * on the return value it waits a while and tests the AP queue if interrupts
 322 * have been switched on using ap_test_queue().
 323 */
 324static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
 325{
 326#ifdef CONFIG_64BIT
 327	struct ap_queue_status status;
 328	int t_depth, t_device_type, rc, i;
 329
 330	rc = -EBUSY;
 331	status = ap_queue_interruption_control(qid, ind);
 332
 333	for (i = 0; i < AP_MAX_RESET; i++) {
 334		switch (status.response_code) {
 335		case AP_RESPONSE_NORMAL:
 336			if (status.int_enabled)
 337				return 0;
 338			break;
 339		case AP_RESPONSE_RESET_IN_PROGRESS:
 340		case AP_RESPONSE_BUSY:
 341			break;
 342		case AP_RESPONSE_Q_NOT_AVAIL:
 343		case AP_RESPONSE_DECONFIGURED:
 344		case AP_RESPONSE_CHECKSTOPPED:
 345		case AP_RESPONSE_INVALID_ADDRESS:
 346			return -ENODEV;
 347		case AP_RESPONSE_OTHERWISE_CHANGED:
 348			if (status.int_enabled)
 349				return 0;
 350			break;
 351		default:
 352			break;
 353		}
 354		if (i < AP_MAX_RESET - 1) {
 355			udelay(5);
 356			status = ap_test_queue(qid, &t_depth, &t_device_type);
 357		}
 358	}
 359	return rc;
 360#else
 361	return -EINVAL;
 362#endif
 363}
 364
 365/**
 366 * __ap_send(): Send message to adjunct processor queue.
 367 * @qid: The AP queue number
 368 * @psmid: The program supplied message identifier
 369 * @msg: The message text
 370 * @length: The message length
 371 * @special: Special Bit
 372 *
 373 * Returns AP queue status structure.
 374 * Condition code 1 on NQAP can't happen because the L bit is 1.
 375 * Condition code 2 on NQAP also means the send is incomplete,
 376 * because a segment boundary was reached. The NQAP is repeated.
 377 */
 378static inline struct ap_queue_status
 379__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
 380	  unsigned int special)
 381{
 382	typedef struct { char _[length]; } msgblock;
 383	register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
 384	register struct ap_queue_status reg1 asm ("1");
 385	register unsigned long reg2 asm ("2") = (unsigned long) msg;
 386	register unsigned long reg3 asm ("3") = (unsigned long) length;
 387	register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
 388	register unsigned long reg5 asm ("5") = (unsigned int) psmid;
 389
 390	if (special == 1)
 391		reg0 |= 0x400000UL;
 392
 393	asm volatile (
 394		"0: .long 0xb2ad0042\n"		/* DQAP */
 395		"   brc   2,0b"
 396		: "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
 397		: "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
 398		: "cc" );
 399	return reg1;
 400}
 401
 402int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
 403{
 404	struct ap_queue_status status;
 405
 406	status = __ap_send(qid, psmid, msg, length, 0);
 407	switch (status.response_code) {
 408	case AP_RESPONSE_NORMAL:
 409		return 0;
 410	case AP_RESPONSE_Q_FULL:
 411	case AP_RESPONSE_RESET_IN_PROGRESS:
 412		return -EBUSY;
 413	case AP_RESPONSE_REQ_FAC_NOT_INST:
 414		return -EINVAL;
 415	default:	/* Device is gone. */
 416		return -ENODEV;
 417	}
 418}
 419EXPORT_SYMBOL(ap_send);
 420
 421/**
 422 * __ap_recv(): Receive message from adjunct processor queue.
 423 * @qid: The AP queue number
 424 * @psmid: Pointer to program supplied message identifier
 425 * @msg: The message text
 426 * @length: The message length
 427 *
 428 * Returns AP queue status structure.
 429 * Condition code 1 on DQAP means the receive has taken place
 430 * but only partially.	The response is incomplete, hence the
 431 * DQAP is repeated.
 432 * Condition code 2 on DQAP also means the receive is incomplete,
 433 * this time because a segment boundary was reached. Again, the
 434 * DQAP is repeated.
 435 * Note that gpr2 is used by the DQAP instruction to keep track of
 436 * any 'residual' length, in case the instruction gets interrupted.
 437 * Hence it gets zeroed before the instruction.
 438 */
 439static inline struct ap_queue_status
 440__ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
 441{
 442	typedef struct { char _[length]; } msgblock;
 443	register unsigned long reg0 asm("0") = qid | 0x80000000UL;
 444	register struct ap_queue_status reg1 asm ("1");
 445	register unsigned long reg2 asm("2") = 0UL;
 446	register unsigned long reg4 asm("4") = (unsigned long) msg;
 447	register unsigned long reg5 asm("5") = (unsigned long) length;
 448	register unsigned long reg6 asm("6") = 0UL;
 449	register unsigned long reg7 asm("7") = 0UL;
 450
 451
 452	asm volatile(
 453		"0: .long 0xb2ae0064\n"
 454		"   brc   6,0b\n"
 455		: "+d" (reg0), "=d" (reg1), "+d" (reg2),
 456		"+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
 457		"=m" (*(msgblock *) msg) : : "cc" );
 458	*psmid = (((unsigned long long) reg6) << 32) + reg7;
 459	return reg1;
 460}
 461
 462int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
 463{
 464	struct ap_queue_status status;
 
 
 465
 466	status = __ap_recv(qid, psmid, msg, length);
 
 
 
 467	switch (status.response_code) {
 468	case AP_RESPONSE_NORMAL:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 469		return 0;
 470	case AP_RESPONSE_NO_PENDING_REPLY:
 471		if (status.queue_empty)
 472			return -ENOENT;
 473		return -EBUSY;
 
 474	case AP_RESPONSE_RESET_IN_PROGRESS:
 
 
 475		return -EBUSY;
 476	default:
 477		return -ENODEV;
 478	}
 479}
 480EXPORT_SYMBOL(ap_recv);
 481
 482/**
 483 * ap_query_queue(): Check if an AP queue is available.
 484 * @qid: The AP queue number
 485 * @queue_depth: Pointer to queue depth value
 486 * @device_type: Pointer to device type value
 487 *
 488 * The test is repeated for AP_MAX_RESET times.
 489 */
 490static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
 491{
 492	struct ap_queue_status status;
 493	int t_depth, t_device_type, rc, i;
 494
 495	rc = -EBUSY;
 496	for (i = 0; i < AP_MAX_RESET; i++) {
 497		status = ap_test_queue(qid, &t_depth, &t_device_type);
 498		switch (status.response_code) {
 499		case AP_RESPONSE_NORMAL:
 500			*queue_depth = t_depth + 1;
 501			*device_type = t_device_type;
 502			rc = 0;
 503			break;
 504		case AP_RESPONSE_Q_NOT_AVAIL:
 505			rc = -ENODEV;
 506			break;
 507		case AP_RESPONSE_RESET_IN_PROGRESS:
 508			break;
 509		case AP_RESPONSE_DECONFIGURED:
 510			rc = -ENODEV;
 511			break;
 512		case AP_RESPONSE_CHECKSTOPPED:
 513			rc = -ENODEV;
 514			break;
 515		case AP_RESPONSE_INVALID_ADDRESS:
 516			rc = -ENODEV;
 517			break;
 518		case AP_RESPONSE_OTHERWISE_CHANGED:
 519			break;
 520		case AP_RESPONSE_BUSY:
 
 521			break;
 522		default:
 523			BUG();
 524		}
 525		if (rc != -EBUSY)
 526			break;
 527		if (i < AP_MAX_RESET - 1)
 528			udelay(5);
 
 
 
 
 
 
 
 
 
 529	}
 530	return rc;
 531}
 532
 533/**
 534 * ap_init_queue(): Reset an AP queue.
 535 * @qid: The AP queue number
 536 *
 537 * Reset an AP queue and wait for it to become available again.
 538 */
 539static int ap_init_queue(ap_qid_t qid)
 540{
 541	struct ap_queue_status status;
 542	int rc, dummy, i;
 543
 544	rc = -ENODEV;
 545	status = ap_reset_queue(qid);
 546	for (i = 0; i < AP_MAX_RESET; i++) {
 547		switch (status.response_code) {
 548		case AP_RESPONSE_NORMAL:
 549			if (status.queue_empty)
 550				rc = 0;
 551			break;
 552		case AP_RESPONSE_Q_NOT_AVAIL:
 553		case AP_RESPONSE_DECONFIGURED:
 554		case AP_RESPONSE_CHECKSTOPPED:
 555			i = AP_MAX_RESET;	/* return with -ENODEV */
 556			break;
 557		case AP_RESPONSE_RESET_IN_PROGRESS:
 558			rc = -EBUSY;
 559		case AP_RESPONSE_BUSY:
 560		default:
 561			break;
 562		}
 563		if (rc != -ENODEV && rc != -EBUSY)
 564			break;
 565		if (i < AP_MAX_RESET - 1) {
 566			udelay(5);
 567			status = ap_test_queue(qid, &dummy, &dummy);
 568		}
 569	}
 570	if (rc == 0 && ap_using_interrupts()) {
 571		rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
 572		/* If interruption mode is supported by the machine,
 573		* but an AP can not be enabled for interruption then
 574		* the AP will be discarded.    */
 575		if (rc)
 576			pr_err("Registering adapter interrupts for "
 577			       "AP %d failed\n", AP_QID_DEVICE(qid));
 578	}
 579	return rc;
 580}
 581
 582/**
 583 * ap_increase_queue_count(): Arm request timeout.
 584 * @ap_dev: Pointer to an AP device.
 585 *
 586 * Arm request timeout if an AP device was idle and a new request is submitted.
 587 */
 588static void ap_increase_queue_count(struct ap_device *ap_dev)
 589{
 590	int timeout = ap_dev->drv->request_timeout;
 
 
 
 591
 592	ap_dev->queue_count++;
 593	if (ap_dev->queue_count == 1) {
 594		mod_timer(&ap_dev->timeout, jiffies + timeout);
 595		ap_dev->reset = AP_RESET_ARMED;
 596	}
 
 
 
 
 597}
 598
 599/**
 600 * ap_decrease_queue_count(): Decrease queue count.
 601 * @ap_dev: Pointer to an AP device.
 602 *
 603 * If AP device is still alive, re-schedule request timeout if there are still
 604 * pending requests.
 605 */
 606static void ap_decrease_queue_count(struct ap_device *ap_dev)
 607{
 608	int timeout = ap_dev->drv->request_timeout;
 
 
 609
 610	ap_dev->queue_count--;
 611	if (ap_dev->queue_count > 0)
 612		mod_timer(&ap_dev->timeout, jiffies + timeout);
 613	else
 614		/*
 615		 * The timeout timer should to be disabled now - since
 616		 * del_timer_sync() is very expensive, we just tell via the
 617		 * reset flag to ignore the pending timeout timer.
 618		 */
 619		ap_dev->reset = AP_RESET_IGNORE;
 
 
 
 
 
 
 
 
 620}
 621
 622/*
 623 * AP device related attributes.
 624 */
 625static ssize_t ap_hwtype_show(struct device *dev,
 626			      struct device_attribute *attr, char *buf)
 627{
 628	struct ap_device *ap_dev = to_ap_dev(dev);
 629	return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
 
 
 
 
 
 
 
 
 
 
 
 
 630}
 631
 632static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
 633static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
 634			     char *buf)
 
 
 
 
 
 
 
 
 635{
 636	struct ap_device *ap_dev = to_ap_dev(dev);
 637	return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 638}
 639
 640static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
 641static ssize_t ap_request_count_show(struct device *dev,
 642				     struct device_attribute *attr,
 643				     char *buf)
 644{
 645	struct ap_device *ap_dev = to_ap_dev(dev);
 646	int rc;
 647
 648	spin_lock_bh(&ap_dev->lock);
 649	rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
 650	spin_unlock_bh(&ap_dev->lock);
 
 
 
 
 
 651	return rc;
 652}
 653
 654static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
 655
 656static ssize_t ap_modalias_show(struct device *dev,
 657				struct device_attribute *attr, char *buf)
 658{
 659	return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
 
 
 
 
 
 660}
 661
 662static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
 663
 664static struct attribute *ap_dev_attrs[] = {
 665	&dev_attr_hwtype.attr,
 666	&dev_attr_depth.attr,
 667	&dev_attr_request_count.attr,
 668	&dev_attr_modalias.attr,
 669	NULL
 670};
 671static struct attribute_group ap_dev_attr_group = {
 672	.attrs = ap_dev_attrs
 673};
 674
 675/**
 676 * ap_bus_match()
 677 * @dev: Pointer to device
 678 * @drv: Pointer to device_driver
 679 *
 680 * AP bus driver registration/unregistration.
 681 */
 682static int ap_bus_match(struct device *dev, struct device_driver *drv)
 683{
 684	struct ap_device *ap_dev = to_ap_dev(dev);
 685	struct ap_driver *ap_drv = to_ap_drv(drv);
 686	struct ap_device_id *id;
 687
 688	/*
 689	 * Compare device type of the device with the list of
 690	 * supported types of the device_driver.
 691	 */
 692	for (id = ap_drv->ids; id->match_flags; id++) {
 693		if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
 694		    (id->dev_type != ap_dev->device_type))
 695			continue;
 696		return 1;
 
 
 
 
 697	}
 698	return 0;
 699}
 700
 701/**
 702 * ap_uevent(): Uevent function for AP devices.
 703 * @dev: Pointer to device
 704 * @env: Pointer to kobj_uevent_env
 705 *
 706 * It sets up a single environment variable DEV_TYPE which contains the
 707 * hardware device type.
 708 */
 709static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
 710{
 711	struct ap_device *ap_dev = to_ap_dev(dev);
 712	int retval = 0;
 713
 714	if (!ap_dev)
 715		return -ENODEV;
 716
 717	/* Set up DEV_TYPE environment variable. */
 718	retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
 719	if (retval)
 720		return retval;
 721
 722	/* Add MODALIAS= */
 723	retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
 724
 725	return retval;
 726}
 727
 728static int ap_bus_suspend(struct device *dev, pm_message_t state)
 729{
 730	struct ap_device *ap_dev = to_ap_dev(dev);
 731	unsigned long flags;
 732
 733	if (!ap_suspend_flag) {
 734		ap_suspend_flag = 1;
 
 
 735
 736		/* Disable scanning for devices, thus we do not want to scan
 737		 * for them after removing.
 738		 */
 739		del_timer_sync(&ap_config_timer);
 740		if (ap_work_queue != NULL) {
 741			destroy_workqueue(ap_work_queue);
 742			ap_work_queue = NULL;
 743		}
 744
 745		tasklet_disable(&ap_tasklet);
 746	}
 747	/* Poll on the device until all requests are finished. */
 748	do {
 749		flags = 0;
 750		spin_lock_bh(&ap_dev->lock);
 751		__ap_poll_device(ap_dev, &flags);
 752		spin_unlock_bh(&ap_dev->lock);
 753	} while ((flags & 1) || (flags & 2));
 754
 755	spin_lock_bh(&ap_dev->lock);
 756	ap_dev->unregistered = 1;
 757	spin_unlock_bh(&ap_dev->lock);
 758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 759	return 0;
 760}
 761
 762static int ap_bus_resume(struct device *dev)
 763{
 764	int rc = 0;
 765	struct ap_device *ap_dev = to_ap_dev(dev);
 
 
 766
 767	if (ap_suspend_flag) {
 768		ap_suspend_flag = 0;
 769		if (!ap_interrupts_available())
 770			ap_interrupt_indicator = NULL;
 771		if (!user_set_domain) {
 772			ap_domain_index = -1;
 773			ap_select_domain();
 774		}
 775		init_timer(&ap_config_timer);
 776		ap_config_timer.function = ap_config_timeout;
 777		ap_config_timer.data = 0;
 778		ap_config_timer.expires = jiffies + ap_config_time * HZ;
 779		add_timer(&ap_config_timer);
 780		ap_work_queue = create_singlethread_workqueue("kapwork");
 781		if (!ap_work_queue)
 782			return -ENOMEM;
 783		tasklet_enable(&ap_tasklet);
 784		if (!ap_using_interrupts())
 785			ap_schedule_poll_timer();
 786		else
 787			tasklet_schedule(&ap_tasklet);
 788		if (ap_thread_flag)
 789			rc = ap_poll_thread_start();
 790	}
 791	if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
 792		spin_lock_bh(&ap_dev->lock);
 793		ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
 794				       ap_domain_index);
 795		spin_unlock_bh(&ap_dev->lock);
 796	}
 797	queue_work(ap_work_queue, &ap_config_work);
 798
 799	return rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 800}
 
 
 
 
 
 801
 802static struct bus_type ap_bus_type = {
 803	.name = "ap",
 804	.match = &ap_bus_match,
 805	.uevent = &ap_uevent,
 806	.suspend = ap_bus_suspend,
 807	.resume = ap_bus_resume
 808};
 809
 810static int ap_device_probe(struct device *dev)
 811{
 812	struct ap_device *ap_dev = to_ap_dev(dev);
 813	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
 814	int rc;
 815
 816	ap_dev->drv = ap_drv;
 817	rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
 818	if (!rc) {
 819		spin_lock_bh(&ap_device_list_lock);
 820		list_add(&ap_dev->list, &ap_device_list);
 821		spin_unlock_bh(&ap_device_list_lock);
 822	}
 823	return rc;
 824}
 825
 826/**
 827 * __ap_flush_queue(): Flush requests.
 828 * @ap_dev: Pointer to the AP device
 829 *
 830 * Flush all requests from the request/pending queue of an AP device.
 831 */
 832static void __ap_flush_queue(struct ap_device *ap_dev)
 833{
 834	struct ap_message *ap_msg, *next;
 835
 836	list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
 837		list_del_init(&ap_msg->list);
 838		ap_dev->pendingq_count--;
 839		ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
 840	}
 841	list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
 842		list_del_init(&ap_msg->list);
 843		ap_dev->requestq_count--;
 844		ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
 845	}
 846}
 847
 848void ap_flush_queue(struct ap_device *ap_dev)
 849{
 850	spin_lock_bh(&ap_dev->lock);
 851	__ap_flush_queue(ap_dev);
 852	spin_unlock_bh(&ap_dev->lock);
 853}
 854EXPORT_SYMBOL(ap_flush_queue);
 855
 856static int ap_device_remove(struct device *dev)
 857{
 858	struct ap_device *ap_dev = to_ap_dev(dev);
 859	struct ap_driver *ap_drv = ap_dev->drv;
 860
 861	ap_flush_queue(ap_dev);
 862	del_timer_sync(&ap_dev->timeout);
 863	spin_lock_bh(&ap_device_list_lock);
 864	list_del_init(&ap_dev->list);
 865	spin_unlock_bh(&ap_device_list_lock);
 
 866	if (ap_drv->remove)
 867		ap_drv->remove(ap_dev);
 868	spin_lock_bh(&ap_dev->lock);
 869	atomic_sub(ap_dev->queue_count, &ap_poll_requests);
 870	spin_unlock_bh(&ap_dev->lock);
 871	return 0;
 872}
 873
 874int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
 875		       char *name)
 876{
 877	struct device_driver *drv = &ap_drv->driver;
 878
 
 
 
 879	drv->bus = &ap_bus_type;
 880	drv->probe = ap_device_probe;
 881	drv->remove = ap_device_remove;
 882	drv->owner = owner;
 883	drv->name = name;
 884	return driver_register(drv);
 885}
 886EXPORT_SYMBOL(ap_driver_register);
 887
 888void ap_driver_unregister(struct ap_driver *ap_drv)
 889{
 890	driver_unregister(&ap_drv->driver);
 891}
 892EXPORT_SYMBOL(ap_driver_unregister);
 893
 
 
 
 
 
 
 
 
 
 
 
 894/*
 895 * AP bus attributes.
 896 */
 897static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
 898{
 899	return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
 900}
 901
 902static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 903
 904static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
 905{
 906	return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
 907}
 908
 909static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
 910{
 911	return snprintf(buf, PAGE_SIZE, "%d\n",
 912			ap_using_interrupts() ? 1 : 0);
 913}
 914
 915static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
 916
 917static ssize_t ap_config_time_store(struct bus_type *bus,
 918				    const char *buf, size_t count)
 919{
 920	int time;
 921
 922	if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
 923		return -EINVAL;
 924	ap_config_time = time;
 925	if (!timer_pending(&ap_config_timer) ||
 926	    !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
 927		ap_config_timer.expires = jiffies + ap_config_time * HZ;
 928		add_timer(&ap_config_timer);
 929	}
 930	return count;
 931}
 932
 933static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
 934
 935static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
 936{
 937	return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
 938}
 939
 940static ssize_t ap_poll_thread_store(struct bus_type *bus,
 941				    const char *buf, size_t count)
 942{
 943	int flag, rc;
 944
 945	if (sscanf(buf, "%d\n", &flag) != 1)
 946		return -EINVAL;
 947	if (flag) {
 948		rc = ap_poll_thread_start();
 949		if (rc)
 950			return rc;
 951	}
 952	else
 953		ap_poll_thread_stop();
 954	return count;
 955}
 956
 957static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
 958
 959static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
 960{
 961	return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
 962}
 963
 964static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
 965				  size_t count)
 966{
 967	unsigned long long time;
 968	ktime_t hr_time;
 969
 970	/* 120 seconds = maximum poll interval */
 971	if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
 972	    time > 120000000000ULL)
 973		return -EINVAL;
 974	poll_timeout = time;
 975	hr_time = ktime_set(0, poll_timeout);
 
 
 
 
 
 
 976
 977	if (!hrtimer_is_queued(&ap_poll_timer) ||
 978	    !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
 979		hrtimer_set_expires(&ap_poll_timer, hr_time);
 980		hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
 981	}
 982	return count;
 983}
 984
 985static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
 986
 
 
 
 
 
 
 
 
 
 
 
 
 
 987static struct bus_attribute *const ap_bus_attrs[] = {
 988	&bus_attr_ap_domain,
 
 
 989	&bus_attr_config_time,
 990	&bus_attr_poll_thread,
 991	&bus_attr_ap_interrupts,
 992	&bus_attr_poll_timeout,
 
 993	NULL,
 994};
 995
 996/**
 997 * ap_select_domain(): Select an AP domain.
 998 *
 999 * Pick one of the 16 AP domains.
1000 */
1001static int ap_select_domain(void)
1002{
1003	int queue_depth, device_type, count, max_count, best_domain;
1004	int rc, i, j;
 
1005
1006	/*
1007	 * We want to use a single domain. Either the one specified with
1008	 * the "domain=" parameter or the domain with the maximum number
1009	 * of devices.
1010	 */
1011	if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
 
1012		/* Domain has already been selected. */
 
1013		return 0;
 
1014	best_domain = -1;
1015	max_count = 0;
1016	for (i = 0; i < AP_DOMAINS; i++) {
 
 
1017		count = 0;
1018		for (j = 0; j < AP_DEVICES; j++) {
1019			ap_qid_t qid = AP_MKQID(j, i);
1020			rc = ap_query_queue(qid, &queue_depth, &device_type);
1021			if (rc)
 
1022				continue;
1023			count++;
1024		}
1025		if (count > max_count) {
1026			max_count = count;
1027			best_domain = i;
1028		}
1029	}
1030	if (best_domain >= 0){
1031		ap_domain_index = best_domain;
 
1032		return 0;
1033	}
 
1034	return -ENODEV;
1035}
1036
1037/**
1038 * ap_probe_device_type(): Find the device type of an AP.
1039 * @ap_dev: pointer to the AP device.
1040 *
1041 * Find the device type if query queue returned a device type of 0.
1042 */
1043static int ap_probe_device_type(struct ap_device *ap_dev)
1044{
1045	static unsigned char msg[] = {
1046		0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1047		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1048		0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1049		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1050		0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1051		0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1052		0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1053		0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1054		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1055		0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1056		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1057		0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1058		0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1059		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1060		0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1061		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1062		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1063		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1064		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1065		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1066		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1067		0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1068		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1069		0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1070		0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1071		0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1072		0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1073		0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1074		0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1075		0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1076		0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1077		0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1078		0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1079		0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1080		0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1081		0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1082		0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1083		0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1084		0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1085		0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1086		0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1087		0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1088		0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1089		0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1090		0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1091	};
1092	struct ap_queue_status status;
1093	unsigned long long psmid;
1094	char *reply;
1095	int rc, i;
1096
1097	reply = (void *) get_zeroed_page(GFP_KERNEL);
1098	if (!reply) {
1099		rc = -ENOMEM;
1100		goto out;
1101	}
1102
1103	status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1104			   msg, sizeof(msg), 0);
1105	if (status.response_code != AP_RESPONSE_NORMAL) {
1106		rc = -ENODEV;
1107		goto out_free;
1108	}
1109
1110	/* Wait for the test message to complete. */
1111	for (i = 0; i < 6; i++) {
1112		mdelay(300);
1113		status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1114		if (status.response_code == AP_RESPONSE_NORMAL &&
1115		    psmid == 0x0102030405060708ULL)
1116			break;
1117	}
1118	if (i < 6) {
1119		/* Got an answer. */
1120		if (reply[0] == 0x00 && reply[1] == 0x86)
1121			ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1122		else
1123			ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1124		rc = 0;
1125	} else
1126		rc = -ENODEV;
1127
1128out_free:
1129	free_page((unsigned long) reply);
1130out:
1131	return rc;
1132}
1133
1134static void ap_interrupt_handler(void *unused1, void *unused2)
 
 
 
1135{
1136	kstat_cpu(smp_processor_id()).irqs[IOINT_APB]++;
1137	tasklet_schedule(&ap_tasklet);
1138}
1139
1140/**
1141 * __ap_scan_bus(): Scan the AP bus.
1142 * @dev: Pointer to device
1143 * @data: Pointer to data
1144 *
1145 * Scan the AP bus for new devices.
1146 */
1147static int __ap_scan_bus(struct device *dev, void *data)
1148{
1149	return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1150}
1151
1152static void ap_device_release(struct device *dev)
1153{
1154	struct ap_device *ap_dev = to_ap_dev(dev);
1155
1156	kfree(ap_dev);
1157}
1158
1159static void ap_scan_bus(struct work_struct *unused)
1160{
1161	struct ap_device *ap_dev;
 
1162	struct device *dev;
1163	ap_qid_t qid;
1164	int queue_depth, device_type;
1165	unsigned int device_functions;
1166	int rc, i;
 
 
1167
 
1168	if (ap_select_domain() != 0)
1169		return;
1170	for (i = 0; i < AP_DEVICES; i++) {
1171		qid = AP_MKQID(i, ap_domain_index);
 
1172		dev = bus_find_device(&ap_bus_type, NULL,
1173				      (void *)(unsigned long)qid,
1174				      __ap_scan_bus);
1175		rc = ap_query_queue(qid, &queue_depth, &device_type);
1176		if (dev) {
1177			if (rc == -EBUSY) {
1178				set_current_state(TASK_UNINTERRUPTIBLE);
1179				schedule_timeout(AP_RESET_TIMEOUT);
1180				rc = ap_query_queue(qid, &queue_depth,
1181						    &device_type);
1182			}
1183			ap_dev = to_ap_dev(dev);
1184			spin_lock_bh(&ap_dev->lock);
1185			if (rc || ap_dev->unregistered) {
1186				spin_unlock_bh(&ap_dev->lock);
1187				if (ap_dev->unregistered)
1188					i--;
1189				device_unregister(dev);
1190				put_device(dev);
1191				continue;
1192			}
1193			spin_unlock_bh(&ap_dev->lock);
1194			put_device(dev);
1195			continue;
1196		}
1197		if (rc)
1198			continue;
1199		rc = ap_init_queue(qid);
1200		if (rc)
1201			continue;
1202		ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1203		if (!ap_dev)
1204			break;
1205		ap_dev->qid = qid;
1206		ap_dev->queue_depth = queue_depth;
1207		ap_dev->unregistered = 1;
1208		spin_lock_init(&ap_dev->lock);
1209		INIT_LIST_HEAD(&ap_dev->pendingq);
1210		INIT_LIST_HEAD(&ap_dev->requestq);
1211		INIT_LIST_HEAD(&ap_dev->list);
1212		setup_timer(&ap_dev->timeout, ap_request_timeout,
1213			    (unsigned long) ap_dev);
1214		switch (device_type) {
1215		case 0:
1216			if (ap_probe_device_type(ap_dev)) {
1217				kfree(ap_dev);
1218				continue;
1219			}
1220			break;
1221		case 10:
1222			if (ap_query_functions(qid, &device_functions)) {
1223				kfree(ap_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1224				continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1225			}
1226			if (test_ap_facility(device_functions, 3))
1227				ap_dev->device_type = AP_DEVICE_TYPE_CEX3C;
1228			else if (test_ap_facility(device_functions, 4))
1229				ap_dev->device_type = AP_DEVICE_TYPE_CEX3A;
1230			else {
1231				kfree(ap_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1232				continue;
1233			}
1234			break;
1235		default:
1236			ap_dev->device_type = device_type;
1237		}
1238
1239		ap_dev->device.bus = &ap_bus_type;
1240		ap_dev->device.parent = ap_root_device;
1241		if (dev_set_name(&ap_dev->device, "card%02x",
1242				 AP_QID_DEVICE(ap_dev->qid))) {
1243			kfree(ap_dev);
1244			continue;
1245		}
1246		ap_dev->device.release = ap_device_release;
1247		rc = device_register(&ap_dev->device);
1248		if (rc) {
1249			put_device(&ap_dev->device);
1250			continue;
1251		}
1252		/* Add device attributes. */
1253		rc = sysfs_create_group(&ap_dev->device.kobj,
1254					&ap_dev_attr_group);
1255		if (!rc) {
1256			spin_lock_bh(&ap_dev->lock);
1257			ap_dev->unregistered = 0;
1258			spin_unlock_bh(&ap_dev->lock);
1259		}
1260		else
1261			device_unregister(&ap_dev->device);
1262	}
1263}
1264
1265static void
1266ap_config_timeout(unsigned long ptr)
1267{
1268	queue_work(ap_work_queue, &ap_config_work);
1269	ap_config_timer.expires = jiffies + ap_config_time * HZ;
1270	add_timer(&ap_config_timer);
1271}
1272
1273/**
1274 * ap_schedule_poll_timer(): Schedule poll timer.
1275 *
1276 * Set up the timer to run the poll tasklet
1277 */
1278static inline void ap_schedule_poll_timer(void)
1279{
1280	ktime_t hr_time;
1281
1282	spin_lock_bh(&ap_poll_timer_lock);
1283	if (ap_using_interrupts() || ap_suspend_flag)
1284		goto out;
1285	if (hrtimer_is_queued(&ap_poll_timer))
1286		goto out;
1287	if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1288		hr_time = ktime_set(0, poll_timeout);
1289		hrtimer_forward_now(&ap_poll_timer, hr_time);
1290		hrtimer_restart(&ap_poll_timer);
1291	}
1292out:
1293	spin_unlock_bh(&ap_poll_timer_lock);
1294}
1295
1296/**
1297 * ap_poll_read(): Receive pending reply messages from an AP device.
1298 * @ap_dev: pointer to the AP device
1299 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1300 *	   required, bit 2^1 is set if the poll timer needs to get armed
1301 *
1302 * Returns 0 if the device is still present, -ENODEV if not.
1303 */
1304static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1305{
1306	struct ap_queue_status status;
1307	struct ap_message *ap_msg;
1308
1309	if (ap_dev->queue_count <= 0)
1310		return 0;
1311	status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1312			   ap_dev->reply->message, ap_dev->reply->length);
1313	switch (status.response_code) {
1314	case AP_RESPONSE_NORMAL:
1315		atomic_dec(&ap_poll_requests);
1316		ap_decrease_queue_count(ap_dev);
1317		list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1318			if (ap_msg->psmid != ap_dev->reply->psmid)
1319				continue;
1320			list_del_init(&ap_msg->list);
1321			ap_dev->pendingq_count--;
1322			ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
1323			break;
1324		}
1325		if (ap_dev->queue_count > 0)
1326			*flags |= 1;
1327		break;
1328	case AP_RESPONSE_NO_PENDING_REPLY:
1329		if (status.queue_empty) {
1330			/* The card shouldn't forget requests but who knows. */
1331			atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1332			ap_dev->queue_count = 0;
1333			list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1334			ap_dev->requestq_count += ap_dev->pendingq_count;
1335			ap_dev->pendingq_count = 0;
1336		} else
1337			*flags |= 2;
1338		break;
1339	default:
1340		return -ENODEV;
1341	}
1342	return 0;
1343}
1344
1345/**
1346 * ap_poll_write(): Send messages from the request queue to an AP device.
1347 * @ap_dev: pointer to the AP device
1348 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1349 *	   required, bit 2^1 is set if the poll timer needs to get armed
1350 *
1351 * Returns 0 if the device is still present, -ENODEV if not.
1352 */
1353static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1354{
1355	struct ap_queue_status status;
1356	struct ap_message *ap_msg;
1357
1358	if (ap_dev->requestq_count <= 0 ||
1359	    ap_dev->queue_count >= ap_dev->queue_depth)
1360		return 0;
1361	/* Start the next request on the queue. */
1362	ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1363	status = __ap_send(ap_dev->qid, ap_msg->psmid,
1364			   ap_msg->message, ap_msg->length, ap_msg->special);
1365	switch (status.response_code) {
1366	case AP_RESPONSE_NORMAL:
1367		atomic_inc(&ap_poll_requests);
1368		ap_increase_queue_count(ap_dev);
1369		list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1370		ap_dev->requestq_count--;
1371		ap_dev->pendingq_count++;
1372		if (ap_dev->queue_count < ap_dev->queue_depth &&
1373		    ap_dev->requestq_count > 0)
1374			*flags |= 1;
1375		*flags |= 2;
1376		break;
1377	case AP_RESPONSE_Q_FULL:
1378	case AP_RESPONSE_RESET_IN_PROGRESS:
1379		*flags |= 2;
1380		break;
1381	case AP_RESPONSE_MESSAGE_TOO_BIG:
1382	case AP_RESPONSE_REQ_FAC_NOT_INST:
1383		return -EINVAL;
1384	default:
1385		return -ENODEV;
1386	}
1387	return 0;
1388}
1389
1390/**
1391 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1392 * @ap_dev: pointer to the bus device
1393 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1394 *	   required, bit 2^1 is set if the poll timer needs to get armed
1395 *
1396 * Poll AP device for pending replies and send new messages. If either
1397 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1398 * Returns 0.
1399 */
1400static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1401{
1402	int rc;
1403
1404	rc = ap_poll_read(ap_dev, flags);
1405	if (rc)
1406		return rc;
1407	return ap_poll_write(ap_dev, flags);
1408}
1409
1410/**
1411 * __ap_queue_message(): Queue a message to a device.
1412 * @ap_dev: pointer to the AP device
1413 * @ap_msg: the message to be queued
1414 *
1415 * Queue a message to a device. Returns 0 if successful.
1416 */
1417static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1418{
1419	struct ap_queue_status status;
 
 
 
 
1420
1421	if (list_empty(&ap_dev->requestq) &&
1422	    ap_dev->queue_count < ap_dev->queue_depth) {
1423		status = __ap_send(ap_dev->qid, ap_msg->psmid,
1424				   ap_msg->message, ap_msg->length,
1425				   ap_msg->special);
1426		switch (status.response_code) {
1427		case AP_RESPONSE_NORMAL:
1428			list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1429			atomic_inc(&ap_poll_requests);
1430			ap_dev->pendingq_count++;
1431			ap_increase_queue_count(ap_dev);
1432			ap_dev->total_request_count++;
1433			break;
1434		case AP_RESPONSE_Q_FULL:
1435		case AP_RESPONSE_RESET_IN_PROGRESS:
1436			list_add_tail(&ap_msg->list, &ap_dev->requestq);
1437			ap_dev->requestq_count++;
1438			ap_dev->total_request_count++;
1439			return -EBUSY;
1440		case AP_RESPONSE_REQ_FAC_NOT_INST:
1441		case AP_RESPONSE_MESSAGE_TOO_BIG:
1442			ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1443			return -EINVAL;
1444		default:	/* Device is gone. */
1445			ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1446			return -ENODEV;
1447		}
1448	} else {
1449		list_add_tail(&ap_msg->list, &ap_dev->requestq);
1450		ap_dev->requestq_count++;
1451		ap_dev->total_request_count++;
1452		return -EBUSY;
1453	}
1454	ap_schedule_poll_timer();
1455	return 0;
1456}
1457
1458void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1459{
1460	unsigned long flags;
1461	int rc;
1462
1463	spin_lock_bh(&ap_dev->lock);
1464	if (!ap_dev->unregistered) {
1465		/* Make room on the queue by polling for finished requests. */
1466		rc = ap_poll_queue(ap_dev, &flags);
1467		if (!rc)
1468			rc = __ap_queue_message(ap_dev, ap_msg);
1469		if (!rc)
1470			wake_up(&ap_poll_wait);
1471		if (rc == -ENODEV)
1472			ap_dev->unregistered = 1;
1473	} else {
1474		ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1475		rc = -ENODEV;
1476	}
1477	spin_unlock_bh(&ap_dev->lock);
1478	if (rc == -ENODEV)
1479		device_unregister(&ap_dev->device);
1480}
1481EXPORT_SYMBOL(ap_queue_message);
1482
1483/**
1484 * ap_cancel_message(): Cancel a crypto request.
1485 * @ap_dev: The AP device that has the message queued
1486 * @ap_msg: The message that is to be removed
1487 *
1488 * Cancel a crypto request. This is done by removing the request
1489 * from the device pending or request queue. Note that the
1490 * request stays on the AP queue. When it finishes the message
1491 * reply will be discarded because the psmid can't be found.
1492 */
1493void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1494{
1495	struct ap_message *tmp;
1496
1497	spin_lock_bh(&ap_dev->lock);
1498	if (!list_empty(&ap_msg->list)) {
1499		list_for_each_entry(tmp, &ap_dev->pendingq, list)
1500			if (tmp->psmid == ap_msg->psmid) {
1501				ap_dev->pendingq_count--;
1502				goto found;
1503			}
1504		ap_dev->requestq_count--;
1505	found:
1506		list_del_init(&ap_msg->list);
1507	}
1508	spin_unlock_bh(&ap_dev->lock);
1509}
1510EXPORT_SYMBOL(ap_cancel_message);
1511
1512/**
1513 * ap_poll_timeout(): AP receive polling for finished AP requests.
1514 * @unused: Unused pointer.
1515 *
1516 * Schedules the AP tasklet using a high resolution timer.
1517 */
1518static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1519{
1520	tasklet_schedule(&ap_tasklet);
1521	return HRTIMER_NORESTART;
1522}
1523
1524/**
1525 * ap_reset(): Reset a not responding AP device.
1526 * @ap_dev: Pointer to the AP device
1527 *
1528 * Reset a not responding AP device and move all requests from the
1529 * pending queue to the request queue.
1530 */
1531static void ap_reset(struct ap_device *ap_dev)
1532{
1533	int rc;
1534
1535	ap_dev->reset = AP_RESET_IGNORE;
1536	atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1537	ap_dev->queue_count = 0;
1538	list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1539	ap_dev->requestq_count += ap_dev->pendingq_count;
1540	ap_dev->pendingq_count = 0;
1541	rc = ap_init_queue(ap_dev->qid);
1542	if (rc == -ENODEV)
1543		ap_dev->unregistered = 1;
1544}
1545
1546static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1547{
1548	if (!ap_dev->unregistered) {
1549		if (ap_poll_queue(ap_dev, flags))
1550			ap_dev->unregistered = 1;
1551		if (ap_dev->reset == AP_RESET_DO)
1552			ap_reset(ap_dev);
1553	}
1554	return 0;
1555}
1556
1557/**
1558 * ap_poll_all(): Poll all AP devices.
1559 * @dummy: Unused variable
1560 *
1561 * Poll all AP devices on the bus in a round robin fashion. Continue
1562 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1563 * of the control flags has been set arm the poll timer.
1564 */
1565static void ap_poll_all(unsigned long dummy)
1566{
1567	unsigned long flags;
1568	struct ap_device *ap_dev;
1569
1570	/* Reset the indicator if interrupts are used. Thus new interrupts can
1571	 * be received. Doing it in the beginning of the tasklet is therefor
1572	 * important that no requests on any AP get lost.
1573	 */
1574	if (ap_using_interrupts())
1575		xchg((u8 *)ap_interrupt_indicator, 0);
1576	do {
1577		flags = 0;
1578		spin_lock(&ap_device_list_lock);
1579		list_for_each_entry(ap_dev, &ap_device_list, list) {
1580			spin_lock(&ap_dev->lock);
1581			__ap_poll_device(ap_dev, &flags);
1582			spin_unlock(&ap_dev->lock);
1583		}
1584		spin_unlock(&ap_device_list_lock);
1585	} while (flags & 1);
1586	if (flags & 2)
1587		ap_schedule_poll_timer();
1588}
1589
1590/**
1591 * ap_poll_thread(): Thread that polls for finished requests.
1592 * @data: Unused pointer
1593 *
1594 * AP bus poll thread. The purpose of this thread is to poll for
1595 * finished requests in a loop if there is a "free" cpu - that is
1596 * a cpu that doesn't have anything better to do. The polling stops
1597 * as soon as there is another task or if all messages have been
1598 * delivered.
1599 */
1600static int ap_poll_thread(void *data)
1601{
1602	DECLARE_WAITQUEUE(wait, current);
1603	unsigned long flags;
1604	int requests;
1605	struct ap_device *ap_dev;
1606
1607	set_user_nice(current, 19);
1608	while (1) {
1609		if (ap_suspend_flag)
1610			return 0;
1611		if (need_resched()) {
1612			schedule();
1613			continue;
1614		}
1615		add_wait_queue(&ap_poll_wait, &wait);
1616		set_current_state(TASK_INTERRUPTIBLE);
1617		if (kthread_should_stop())
1618			break;
1619		requests = atomic_read(&ap_poll_requests);
1620		if (requests <= 0)
1621			schedule();
1622		set_current_state(TASK_RUNNING);
1623		remove_wait_queue(&ap_poll_wait, &wait);
1624
1625		flags = 0;
1626		spin_lock_bh(&ap_device_list_lock);
1627		list_for_each_entry(ap_dev, &ap_device_list, list) {
1628			spin_lock(&ap_dev->lock);
1629			__ap_poll_device(ap_dev, &flags);
1630			spin_unlock(&ap_dev->lock);
1631		}
1632		spin_unlock_bh(&ap_device_list_lock);
1633	}
1634	set_current_state(TASK_RUNNING);
1635	remove_wait_queue(&ap_poll_wait, &wait);
1636	return 0;
1637}
1638
1639static int ap_poll_thread_start(void)
1640{
1641	int rc;
1642
1643	if (ap_using_interrupts() || ap_suspend_flag)
1644		return 0;
1645	mutex_lock(&ap_poll_thread_mutex);
1646	if (!ap_poll_kthread) {
1647		ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1648		rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1649		if (rc)
1650			ap_poll_kthread = NULL;
1651	}
1652	else
1653		rc = 0;
1654	mutex_unlock(&ap_poll_thread_mutex);
1655	return rc;
1656}
1657
1658static void ap_poll_thread_stop(void)
1659{
1660	mutex_lock(&ap_poll_thread_mutex);
1661	if (ap_poll_kthread) {
1662		kthread_stop(ap_poll_kthread);
1663		ap_poll_kthread = NULL;
1664	}
1665	mutex_unlock(&ap_poll_thread_mutex);
1666}
1667
1668/**
1669 * ap_request_timeout(): Handling of request timeouts
1670 * @data: Holds the AP device.
1671 *
1672 * Handles request timeouts.
1673 */
1674static void ap_request_timeout(unsigned long data)
1675{
1676	struct ap_device *ap_dev = (struct ap_device *) data;
1677
1678	if (ap_dev->reset == AP_RESET_ARMED) {
1679		ap_dev->reset = AP_RESET_DO;
1680
1681		if (ap_using_interrupts())
1682			tasklet_schedule(&ap_tasklet);
1683	}
1684}
1685
1686static void ap_reset_domain(void)
1687{
1688	int i;
1689
1690	if (ap_domain_index != -1)
1691		for (i = 0; i < AP_DEVICES; i++)
1692			ap_reset_queue(AP_MKQID(i, ap_domain_index));
1693}
1694
1695static void ap_reset_all(void)
1696{
1697	int i, j;
1698
1699	for (i = 0; i < AP_DOMAINS; i++)
1700		for (j = 0; j < AP_DEVICES; j++)
1701			ap_reset_queue(AP_MKQID(j, i));
1702}
1703
1704static struct reset_call ap_reset_call = {
1705	.fn = ap_reset_all,
1706};
1707
1708/**
1709 * ap_module_init(): The module initialization code.
1710 *
1711 * Initializes the module.
1712 */
1713int __init ap_module_init(void)
1714{
1715	int rc, i;
1716
1717	if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1718		pr_warning("%d is not a valid cryptographic domain\n",
1719			   ap_domain_index);
1720		return -EINVAL;
1721	}
1722	/* In resume callback we need to know if the user had set the domain.
1723	 * If so, we can not just reset it.
1724	 */
1725	if (ap_domain_index >= 0)
1726		user_set_domain = 1;
1727
1728	if (ap_instructions_available() != 0) {
1729		pr_warning("The hardware system does not support "
1730			   "AP instructions\n");
1731		return -ENODEV;
1732	}
1733	if (ap_interrupts_available()) {
1734		isc_register(AP_ISC);
1735		ap_interrupt_indicator = s390_register_adapter_interrupt(
1736			&ap_interrupt_handler, NULL, AP_ISC);
1737		if (IS_ERR(ap_interrupt_indicator)) {
1738			ap_interrupt_indicator = NULL;
1739			isc_unregister(AP_ISC);
1740		}
1741	}
1742
1743	register_reset_call(&ap_reset_call);
1744
1745	/* Create /sys/bus/ap. */
1746	rc = bus_register(&ap_bus_type);
1747	if (rc)
1748		goto out;
1749	for (i = 0; ap_bus_attrs[i]; i++) {
1750		rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1751		if (rc)
1752			goto out_bus;
1753	}
1754
1755	/* Create /sys/devices/ap. */
1756	ap_root_device = root_device_register("ap");
1757	rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1758	if (rc)
1759		goto out_bus;
1760
1761	ap_work_queue = create_singlethread_workqueue("kapwork");
1762	if (!ap_work_queue) {
1763		rc = -ENOMEM;
1764		goto out_root;
1765	}
1766
1767	if (ap_select_domain() == 0)
1768		ap_scan_bus(NULL);
1769
1770	/* Setup the AP bus rescan timer. */
1771	init_timer(&ap_config_timer);
1772	ap_config_timer.function = ap_config_timeout;
1773	ap_config_timer.data = 0;
1774	ap_config_timer.expires = jiffies + ap_config_time * HZ;
1775	add_timer(&ap_config_timer);
1776
1777	/* Setup the high resultion poll timer.
 
1778	 * If we are running under z/VM adjust polling to z/VM polling rate.
1779	 */
1780	if (MACHINE_IS_VM)
1781		poll_timeout = 1500000;
1782	spin_lock_init(&ap_poll_timer_lock);
1783	hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1784	ap_poll_timer.function = ap_poll_timeout;
1785
1786	/* Start the low priority AP bus poll thread. */
1787	if (ap_thread_flag) {
1788		rc = ap_poll_thread_start();
1789		if (rc)
1790			goto out_work;
1791	}
1792
 
 
 
 
 
 
 
1793	return 0;
1794
 
 
1795out_work:
1796	del_timer_sync(&ap_config_timer);
1797	hrtimer_cancel(&ap_poll_timer);
1798	destroy_workqueue(ap_work_queue);
1799out_root:
1800	root_device_unregister(ap_root_device);
1801out_bus:
1802	while (i--)
1803		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1804	bus_unregister(&ap_bus_type);
1805out:
1806	unregister_reset_call(&ap_reset_call);
1807	if (ap_using_interrupts()) {
1808		s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1809		isc_unregister(AP_ISC);
1810	}
1811	return rc;
1812}
1813
1814static int __ap_match_all(struct device *dev, void *data)
1815{
1816	return 1;
1817}
1818
1819/**
1820 * ap_modules_exit(): The module termination code
1821 *
1822 * Terminates the module.
1823 */
1824void ap_module_exit(void)
1825{
1826	int i;
1827	struct device *dev;
1828
 
1829	ap_reset_domain();
1830	ap_poll_thread_stop();
1831	del_timer_sync(&ap_config_timer);
1832	hrtimer_cancel(&ap_poll_timer);
1833	destroy_workqueue(ap_work_queue);
1834	tasklet_kill(&ap_tasklet);
1835	root_device_unregister(ap_root_device);
1836	while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1837		    __ap_match_all)))
1838	{
1839		device_unregister(dev);
1840		put_device(dev);
1841	}
 
 
1842	for (i = 0; ap_bus_attrs[i]; i++)
1843		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
 
 
1844	bus_unregister(&ap_bus_type);
 
1845	unregister_reset_call(&ap_reset_call);
1846	if (ap_using_interrupts()) {
1847		s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1848		isc_unregister(AP_ISC);
1849	}
1850}
1851
1852#ifndef CONFIG_ZCRYPT_MONOLITHIC
1853module_init(ap_module_init);
1854module_exit(ap_module_exit);
1855#endif
v4.10.11
   1/*
   2 * Copyright IBM Corp. 2006, 2012
 
 
   3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
   4 *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
   5 *	      Ralph Wuerthner <rwuerthn@de.ibm.com>
   6 *	      Felix Beck <felix.beck@de.ibm.com>
   7 *	      Holger Dengler <hd@linux.vnet.ibm.com>
   8 *
   9 * Adjunct processor bus.
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2, or (at your option)
  14 * any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19 * GNU General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU General Public License
  22 * along with this program; if not, write to the Free Software
  23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24 */
  25
  26#define KMSG_COMPONENT "ap"
  27#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  28
  29#include <linux/kernel_stat.h>
  30#include <linux/module.h>
  31#include <linux/init.h>
  32#include <linux/delay.h>
  33#include <linux/err.h>
  34#include <linux/interrupt.h>
  35#include <linux/workqueue.h>
  36#include <linux/slab.h>
  37#include <linux/notifier.h>
  38#include <linux/kthread.h>
  39#include <linux/mutex.h>
  40#include <linux/suspend.h>
  41#include <asm/reset.h>
  42#include <asm/airq.h>
  43#include <linux/atomic.h>
 
  44#include <asm/isc.h>
  45#include <linux/hrtimer.h>
  46#include <linux/ktime.h>
  47#include <asm/facility.h>
  48#include <linux/crypto.h>
  49#include <linux/mod_devicetable.h>
  50#include <linux/debugfs.h>
  51
  52#include "ap_bus.h"
  53#include "ap_asm.h"
  54#include "ap_debug.h"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  55
  56/*
  57 * Module description.
  58 */
  59MODULE_AUTHOR("IBM Corporation");
  60MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
  61		   "Copyright IBM Corp. 2006, 2012");
  62MODULE_LICENSE("GPL");
  63MODULE_ALIAS_CRYPTO("z90crypt");
  64
  65/*
  66 * Module parameter
  67 */
  68int ap_domain_index = -1;	/* Adjunct Processor Domain Index */
  69static DEFINE_SPINLOCK(ap_domain_lock);
  70module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
  71MODULE_PARM_DESC(domain, "domain index for ap devices");
  72EXPORT_SYMBOL(ap_domain_index);
  73
  74static int ap_thread_flag = 0;
  75module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
  76MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
  77
  78static struct device *ap_root_device;
  79
  80DEFINE_SPINLOCK(ap_list_lock);
  81LIST_HEAD(ap_card_list);
  82
  83static struct ap_config_info *ap_configuration;
  84static bool initialised;
  85
  86/*
  87 * AP bus related debug feature things.
  88 */
  89static struct dentry *ap_dbf_root;
  90debug_info_t *ap_dbf_info;
  91
  92/*
  93 * Workqueue timer for bus rescan.
  94 */
 
  95static struct timer_list ap_config_timer;
  96static int ap_config_time = AP_CONFIG_TIME;
  97static void ap_scan_bus(struct work_struct *);
  98static DECLARE_WORK(ap_scan_work, ap_scan_bus);
  99
 100/*
 101 * Tasklet & timer for AP request polling and interrupts
 102 */
 103static void ap_tasklet_fn(unsigned long);
 104static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
 105static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
 106static struct task_struct *ap_poll_kthread = NULL;
 107static DEFINE_MUTEX(ap_poll_thread_mutex);
 108static DEFINE_SPINLOCK(ap_poll_timer_lock);
 
 109static struct hrtimer ap_poll_timer;
 110/* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
 111 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
 112static unsigned long long poll_timeout = 250000;
 113
 114/* Suspend flag */
 115static int ap_suspend_flag;
 116/* Maximum domain id */
 117static int ap_max_domain_id;
 118/* Flag to check if domain was set through module parameter domain=. This is
 119 * important when supsend and resume is done in a z/VM environment where the
 120 * domain might change. */
 121static int user_set_domain = 0;
 122static struct bus_type ap_bus_type;
 123
 124/* Adapter interrupt definitions */
 125static void ap_interrupt_handler(struct airq_struct *airq);
 126
 127static int ap_airq_flag;
 128
 129static struct airq_struct ap_airq = {
 130	.handler = ap_interrupt_handler,
 131	.isc = AP_ISC,
 132};
 133
 134/**
 135 * ap_using_interrupts() - Returns non-zero if interrupt support is
 136 * available.
 137 */
 138static inline int ap_using_interrupts(void)
 139{
 140	return ap_airq_flag;
 141}
 142
 143/**
 144 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
 145 *
 146 * Returns the address of the local-summary-indicator of the adapter
 147 * interrupt handler for AP, or NULL if adapter interrupts are not
 148 * available.
 149 */
 150void *ap_airq_ptr(void)
 151{
 152	if (ap_using_interrupts())
 153		return ap_airq.lsi_ptr;
 154	return NULL;
 
 
 
 
 
 
 
 
 155}
 156
 157/**
 158 * ap_interrupts_available(): Test if AP interrupts are available.
 159 *
 160 * Returns 1 if AP interrupts are available.
 161 */
 162static int ap_interrupts_available(void)
 163{
 164	return test_facility(65);
 165}
 166
 167/**
 168 * ap_configuration_available(): Test if AP configuration
 169 * information is available.
 
 
 170 *
 171 * Returns 1 if AP configuration information is available.
 172 */
 173static int ap_configuration_available(void)
 
 174{
 175	return test_facility(12);
 
 
 
 
 
 
 
 
 176}
 177
 178/**
 179 * ap_test_queue(): Test adjunct processor queue.
 180 * @qid: The AP queue number
 181 * @info: Pointer to queue descriptor
 182 *
 183 * Returns AP queue status structure.
 184 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 185static inline struct ap_queue_status
 186ap_test_queue(ap_qid_t qid, unsigned long *info)
 187{
 188	if (test_facility(15))
 189		qid |= 1UL << 23;		/* set APFT T bit*/
 190	return ap_tapq(qid, info);
 
 
 
 
 
 
 
 191}
 
 192
 193static inline int ap_query_configuration(void)
 
 
 194{
 195	if (!ap_configuration)
 196		return -EOPNOTSUPP;
 197	return ap_qci(ap_configuration);
 
 
 
 
 
 
 
 
 
 
 
 198}
 
 199
 200/**
 201 * ap_init_configuration(): Allocate and query configuration array.
 
 
 
 
 
 
 
 
 202 */
 203static void ap_init_configuration(void)
 204{
 205	if (!ap_configuration_available())
 206		return;
 
 
 207
 208	ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
 209	if (!ap_configuration)
 210		return;
 211	if (ap_query_configuration() != 0) {
 212		kfree(ap_configuration);
 213		ap_configuration = NULL;
 214		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 215	}
 
 
 
 
 216}
 217
 218/*
 219 * ap_test_config(): helper function to extract the nrth bit
 220 *		     within the unsigned int array field.
 
 
 
 221 */
 222static inline int ap_test_config(unsigned int *field, unsigned int nr)
 223{
 224	return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
 
 
 
 
 
 
 225}
 
 226
 227/*
 228 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
 229 * @id AP card ID
 
 230 *
 231 * Returns 0 if the card is not configured
 232 *	   1 if the card is configured or
 233 *	     if the configuration information is not available
 234 */
 235static inline int ap_test_config_card_id(unsigned int id)
 236{
 237	if (!ap_configuration)	/* QCI not supported */
 238		return 1;
 239	return ap_test_config(ap_configuration->apm, id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 240}
 241
 242/*
 243 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
 244 * @domain AP usage domain ID
 
 
 
 
 245 *
 246 * Returns 0 if the usage domain is not configured
 247 *	   1 if the usage domain is configured or
 248 *	     if the configuration information is not available
 249 */
 250static inline int ap_test_config_domain(unsigned int domain)
 251{
 252	if (!ap_configuration)	/* QCI not supported */
 253		return domain < 16;
 254	return ap_test_config(ap_configuration->aqm, domain);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 255}
 
 256
 257/**
 258 * ap_query_queue(): Check if an AP queue is available.
 259 * @qid: The AP queue number
 260 * @queue_depth: Pointer to queue depth value
 261 * @device_type: Pointer to device type value
 262 * @facilities: Pointer to facility indicator
 
 
 
 
 
 
 
 
 
 
 
 263 */
 264static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
 265			  unsigned int *facilities)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 266{
 267	struct ap_queue_status status;
 268	unsigned long info;
 269	int nd;
 270
 271	if (!ap_test_config_card_id(AP_QID_CARD(qid)))
 272		return -ENODEV;
 273
 274	status = ap_test_queue(qid, &info);
 275	switch (status.response_code) {
 276	case AP_RESPONSE_NORMAL:
 277		*queue_depth = (int)(info & 0xff);
 278		*device_type = (int)((info >> 24) & 0xff);
 279		*facilities = (unsigned int)(info >> 32);
 280		/* Update maximum domain id */
 281		nd = (info >> 16) & 0xff;
 282		/* if N bit is available, z13 and newer */
 283		if ((info & (1UL << 57)) && nd > 0)
 284			ap_max_domain_id = nd;
 285		else /* older machine types */
 286			ap_max_domain_id = 15;
 287		switch (*device_type) {
 288			/* For CEX2 and CEX3 the available functions
 289			 * are not refrected by the facilities bits.
 290			 * Instead it is coded into the type. So here
 291			 * modify the function bits based on the type.
 292			 */
 293		case AP_DEVICE_TYPE_CEX2A:
 294		case AP_DEVICE_TYPE_CEX3A:
 295			*facilities |= 0x08000000;
 296			break;
 297		case AP_DEVICE_TYPE_CEX2C:
 298		case AP_DEVICE_TYPE_CEX3C:
 299			*facilities |= 0x10000000;
 300			break;
 301		default:
 302			break;
 303		}
 304		return 0;
 305	case AP_RESPONSE_Q_NOT_AVAIL:
 306	case AP_RESPONSE_DECONFIGURED:
 307	case AP_RESPONSE_CHECKSTOPPED:
 308	case AP_RESPONSE_INVALID_ADDRESS:
 309		return -ENODEV;
 310	case AP_RESPONSE_RESET_IN_PROGRESS:
 311	case AP_RESPONSE_OTHERWISE_CHANGED:
 312	case AP_RESPONSE_BUSY:
 313		return -EBUSY;
 314	default:
 315		BUG();
 316	}
 317}
 
 318
 319void ap_wait(enum ap_wait wait)
 
 
 
 
 
 
 
 
 320{
 321	ktime_t hr_time;
 
 322
 323	switch (wait) {
 324	case AP_WAIT_AGAIN:
 325	case AP_WAIT_INTERRUPT:
 326		if (ap_using_interrupts())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 327			break;
 328		if (ap_poll_kthread) {
 329			wake_up(&ap_poll_wait);
 330			break;
 
 
 331		}
 332		/* Fall through */
 333	case AP_WAIT_TIMEOUT:
 334		spin_lock_bh(&ap_poll_timer_lock);
 335		if (!hrtimer_is_queued(&ap_poll_timer)) {
 336			hr_time = poll_timeout;
 337			hrtimer_forward_now(&ap_poll_timer, hr_time);
 338			hrtimer_restart(&ap_poll_timer);
 339		}
 340		spin_unlock_bh(&ap_poll_timer_lock);
 341		break;
 342	case AP_WAIT_NONE:
 343	default:
 344		break;
 345	}
 
 346}
 347
 348/**
 349 * ap_request_timeout(): Handling of request timeouts
 350 * @data: Holds the AP device.
 351 *
 352 * Handles request timeouts.
 353 */
 354void ap_request_timeout(unsigned long data)
 355{
 356	struct ap_queue *aq = (struct ap_queue *) data;
 
 357
 358	if (ap_suspend_flag)
 359		return;
 360	spin_lock_bh(&aq->lock);
 361	ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
 362	spin_unlock_bh(&aq->lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 363}
 364
 365/**
 366 * ap_poll_timeout(): AP receive polling for finished AP requests.
 367 * @unused: Unused pointer.
 368 *
 369 * Schedules the AP tasklet using a high resolution timer.
 370 */
 371static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
 372{
 373	if (!ap_suspend_flag)
 374		tasklet_schedule(&ap_tasklet);
 375	return HRTIMER_NORESTART;
 376}
 377
 378/**
 379 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
 380 * @airq: pointer to adapter interrupt descriptor
 381 */
 382static void ap_interrupt_handler(struct airq_struct *airq)
 383{
 384	inc_irq_stat(IRQIO_APB);
 385	if (!ap_suspend_flag)
 386		tasklet_schedule(&ap_tasklet);
 387}
 388
 389/**
 390 * ap_tasklet_fn(): Tasklet to poll all AP devices.
 391 * @dummy: Unused variable
 392 *
 393 * Poll all AP devices on the bus.
 
 394 */
 395static void ap_tasklet_fn(unsigned long dummy)
 396{
 397	struct ap_card *ac;
 398	struct ap_queue *aq;
 399	enum ap_wait wait = AP_WAIT_NONE;
 400
 401	/* Reset the indicator if interrupts are used. Thus new interrupts can
 402	 * be received. Doing it in the beginning of the tasklet is therefor
 403	 * important that no requests on any AP get lost.
 404	 */
 405	if (ap_using_interrupts())
 406		xchg(ap_airq.lsi_ptr, 0);
 407
 408	spin_lock_bh(&ap_list_lock);
 409	for_each_ap_card(ac) {
 410		for_each_ap_queue(aq, ac) {
 411			spin_lock_bh(&aq->lock);
 412			wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
 413			spin_unlock_bh(&aq->lock);
 414		}
 415	}
 416	spin_unlock_bh(&ap_list_lock);
 417
 418	ap_wait(wait);
 419}
 420
 421static int ap_pending_requests(void)
 
 
 
 
 422{
 423	struct ap_card *ac;
 424	struct ap_queue *aq;
 425
 426	spin_lock_bh(&ap_list_lock);
 427	for_each_ap_card(ac) {
 428		for_each_ap_queue(aq, ac) {
 429			if (aq->queue_count == 0)
 430				continue;
 431			spin_unlock_bh(&ap_list_lock);
 432			return 1;
 433		}
 434	}
 435	spin_unlock_bh(&ap_list_lock);
 436	return 0;
 437}
 438
 439/**
 440 * ap_poll_thread(): Thread that polls for finished requests.
 441 * @data: Unused pointer
 442 *
 443 * AP bus poll thread. The purpose of this thread is to poll for
 444 * finished requests in a loop if there is a "free" cpu - that is
 445 * a cpu that doesn't have anything better to do. The polling stops
 446 * as soon as there is another task or if all messages have been
 447 * delivered.
 448 */
 449static int ap_poll_thread(void *data)
 450{
 451	DECLARE_WAITQUEUE(wait, current);
 452
 453	set_user_nice(current, MAX_NICE);
 454	set_freezable();
 455	while (!kthread_should_stop()) {
 456		add_wait_queue(&ap_poll_wait, &wait);
 457		set_current_state(TASK_INTERRUPTIBLE);
 458		if (ap_suspend_flag || !ap_pending_requests()) {
 459			schedule();
 460			try_to_freeze();
 461		}
 462		set_current_state(TASK_RUNNING);
 463		remove_wait_queue(&ap_poll_wait, &wait);
 464		if (need_resched()) {
 465			schedule();
 466			try_to_freeze();
 467			continue;
 468		}
 469		ap_tasklet_fn(0);
 470	}
 471
 472	return 0;
 473}
 474
 475static int ap_poll_thread_start(void)
 
 
 
 476{
 
 477	int rc;
 478
 479	if (ap_using_interrupts() || ap_poll_kthread)
 480		return 0;
 481	mutex_lock(&ap_poll_thread_mutex);
 482	ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
 483	rc = PTR_RET(ap_poll_kthread);
 484	if (rc)
 485		ap_poll_kthread = NULL;
 486	mutex_unlock(&ap_poll_thread_mutex);
 487	return rc;
 488}
 489
 490static void ap_poll_thread_stop(void)
 
 
 
 491{
 492	if (!ap_poll_kthread)
 493		return;
 494	mutex_lock(&ap_poll_thread_mutex);
 495	kthread_stop(ap_poll_kthread);
 496	ap_poll_kthread = NULL;
 497	mutex_unlock(&ap_poll_thread_mutex);
 498}
 499
 500#define is_card_dev(x) ((x)->parent == ap_root_device)
 501#define is_queue_dev(x) ((x)->parent != ap_root_device)
 
 
 
 
 
 
 
 
 
 
 502
 503/**
 504 * ap_bus_match()
 505 * @dev: Pointer to device
 506 * @drv: Pointer to device_driver
 507 *
 508 * AP bus driver registration/unregistration.
 509 */
 510static int ap_bus_match(struct device *dev, struct device_driver *drv)
 511{
 
 512	struct ap_driver *ap_drv = to_ap_drv(drv);
 513	struct ap_device_id *id;
 514
 515	/*
 516	 * Compare device type of the device with the list of
 517	 * supported types of the device_driver.
 518	 */
 519	for (id = ap_drv->ids; id->match_flags; id++) {
 520		if (is_card_dev(dev) &&
 521		    id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
 522		    id->dev_type == to_ap_dev(dev)->device_type)
 523			return 1;
 524		if (is_queue_dev(dev) &&
 525		    id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
 526		    id->dev_type == to_ap_dev(dev)->device_type)
 527			return 1;
 528	}
 529	return 0;
 530}
 531
 532/**
 533 * ap_uevent(): Uevent function for AP devices.
 534 * @dev: Pointer to device
 535 * @env: Pointer to kobj_uevent_env
 536 *
 537 * It sets up a single environment variable DEV_TYPE which contains the
 538 * hardware device type.
 539 */
 540static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
 541{
 542	struct ap_device *ap_dev = to_ap_dev(dev);
 543	int retval = 0;
 544
 545	if (!ap_dev)
 546		return -ENODEV;
 547
 548	/* Set up DEV_TYPE environment variable. */
 549	retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
 550	if (retval)
 551		return retval;
 552
 553	/* Add MODALIAS= */
 554	retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
 555
 556	return retval;
 557}
 558
 559static int ap_dev_suspend(struct device *dev)
 560{
 561	struct ap_device *ap_dev = to_ap_dev(dev);
 
 562
 563	if (ap_dev->drv && ap_dev->drv->suspend)
 564		ap_dev->drv->suspend(ap_dev);
 565	return 0;
 566}
 567
 568static int ap_dev_resume(struct device *dev)
 569{
 570	struct ap_device *ap_dev = to_ap_dev(dev);
 
 
 
 
 
 571
 572	if (ap_dev->drv && ap_dev->drv->resume)
 573		ap_dev->drv->resume(ap_dev);
 574	return 0;
 575}
 
 
 
 
 
 
 
 
 
 576
 577static void ap_bus_suspend(void)
 578{
 579	AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
 580
 581	ap_suspend_flag = 1;
 582	/*
 583	 * Disable scanning for devices, thus we do not want to scan
 584	 * for them after removing.
 585	 */
 586	flush_work(&ap_scan_work);
 587	tasklet_disable(&ap_tasklet);
 588}
 589
 590static int __ap_card_devices_unregister(struct device *dev, void *dummy)
 591{
 592	if (is_card_dev(dev))
 593		device_unregister(dev);
 594	return 0;
 595}
 596
 597static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
 598{
 599	if (is_queue_dev(dev))
 600		device_unregister(dev);
 601	return 0;
 602}
 603
 604static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
 605{
 606	if (is_queue_dev(dev) &&
 607	    AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
 608		device_unregister(dev);
 609	return 0;
 610}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 611
 612static void ap_bus_resume(void)
 613{
 614	int rc;
 615
 616	AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
 617
 618	/* remove all queue devices */
 619	bus_for_each_dev(&ap_bus_type, NULL, NULL,
 620			 __ap_queue_devices_unregister);
 621	/* remove all card devices */
 622	bus_for_each_dev(&ap_bus_type, NULL, NULL,
 623			 __ap_card_devices_unregister);
 624
 625	/* Reset thin interrupt setting */
 626	if (ap_interrupts_available() && !ap_using_interrupts()) {
 627		rc = register_adapter_interrupt(&ap_airq);
 628		ap_airq_flag = (rc == 0);
 629	}
 630	if (!ap_interrupts_available() && ap_using_interrupts()) {
 631		unregister_adapter_interrupt(&ap_airq);
 632		ap_airq_flag = 0;
 633	}
 634	/* Reset domain */
 635	if (!user_set_domain)
 636		ap_domain_index = -1;
 637	/* Get things going again */
 638	ap_suspend_flag = 0;
 639	if (ap_airq_flag)
 640		xchg(ap_airq.lsi_ptr, 0);
 641	tasklet_enable(&ap_tasklet);
 642	queue_work(system_long_wq, &ap_scan_work);
 643}
 644
 645static int ap_power_event(struct notifier_block *this, unsigned long event,
 646			  void *ptr)
 647{
 648	switch (event) {
 649	case PM_HIBERNATION_PREPARE:
 650	case PM_SUSPEND_PREPARE:
 651		ap_bus_suspend();
 652		break;
 653	case PM_POST_HIBERNATION:
 654	case PM_POST_SUSPEND:
 655		ap_bus_resume();
 656		break;
 657	default:
 658		break;
 659	}
 660	return NOTIFY_DONE;
 661}
 662static struct notifier_block ap_power_notifier = {
 663	.notifier_call = ap_power_event,
 664};
 665
 666static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
 667
 668static struct bus_type ap_bus_type = {
 669	.name = "ap",
 670	.match = &ap_bus_match,
 671	.uevent = &ap_uevent,
 672	.pm = &ap_bus_pm_ops,
 
 673};
 674
 675static int ap_device_probe(struct device *dev)
 676{
 677	struct ap_device *ap_dev = to_ap_dev(dev);
 678	struct ap_driver *ap_drv = to_ap_drv(dev->driver);
 679	int rc;
 680
 681	ap_dev->drv = ap_drv;
 682	rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
 683	if (rc)
 684		ap_dev->drv = NULL;
 
 
 
 685	return rc;
 686}
 687
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 688static int ap_device_remove(struct device *dev)
 689{
 690	struct ap_device *ap_dev = to_ap_dev(dev);
 691	struct ap_driver *ap_drv = ap_dev->drv;
 692
 693	spin_lock_bh(&ap_list_lock);
 694	if (is_card_dev(dev))
 695		list_del_init(&to_ap_card(dev)->list);
 696	else
 697		list_del_init(&to_ap_queue(dev)->list);
 698	spin_unlock_bh(&ap_list_lock);
 699	if (ap_drv->remove)
 700		ap_drv->remove(ap_dev);
 
 
 
 701	return 0;
 702}
 703
 704int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
 705		       char *name)
 706{
 707	struct device_driver *drv = &ap_drv->driver;
 708
 709	if (!initialised)
 710		return -ENODEV;
 711
 712	drv->bus = &ap_bus_type;
 713	drv->probe = ap_device_probe;
 714	drv->remove = ap_device_remove;
 715	drv->owner = owner;
 716	drv->name = name;
 717	return driver_register(drv);
 718}
 719EXPORT_SYMBOL(ap_driver_register);
 720
 721void ap_driver_unregister(struct ap_driver *ap_drv)
 722{
 723	driver_unregister(&ap_drv->driver);
 724}
 725EXPORT_SYMBOL(ap_driver_unregister);
 726
 727void ap_bus_force_rescan(void)
 728{
 729	if (ap_suspend_flag)
 730		return;
 731	/* processing a asynchronous bus rescan */
 732	del_timer(&ap_config_timer);
 733	queue_work(system_long_wq, &ap_scan_work);
 734	flush_work(&ap_scan_work);
 735}
 736EXPORT_SYMBOL(ap_bus_force_rescan);
 737
 738/*
 739 * AP bus attributes.
 740 */
 741static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
 742{
 743	return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
 744}
 745
 746static ssize_t ap_domain_store(struct bus_type *bus,
 747			       const char *buf, size_t count)
 748{
 749	int domain;
 750
 751	if (sscanf(buf, "%i\n", &domain) != 1 ||
 752	    domain < 0 || domain > ap_max_domain_id)
 753		return -EINVAL;
 754	spin_lock_bh(&ap_domain_lock);
 755	ap_domain_index = domain;
 756	spin_unlock_bh(&ap_domain_lock);
 757
 758	AP_DBF(DBF_DEBUG, "store new default domain=%d\n", domain);
 759
 760	return count;
 761}
 762
 763static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
 764
 765static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
 766{
 767	if (!ap_configuration)	/* QCI not supported */
 768		return snprintf(buf, PAGE_SIZE, "not supported\n");
 769
 770	return snprintf(buf, PAGE_SIZE,
 771			"0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
 772			ap_configuration->adm[0], ap_configuration->adm[1],
 773			ap_configuration->adm[2], ap_configuration->adm[3],
 774			ap_configuration->adm[4], ap_configuration->adm[5],
 775			ap_configuration->adm[6], ap_configuration->adm[7]);
 776}
 777
 778static BUS_ATTR(ap_control_domain_mask, 0444,
 779		ap_control_domain_mask_show, NULL);
 780
 781static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
 782{
 783	if (!ap_configuration)	/* QCI not supported */
 784		return snprintf(buf, PAGE_SIZE, "not supported\n");
 785
 786	return snprintf(buf, PAGE_SIZE,
 787			"0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
 788			ap_configuration->aqm[0], ap_configuration->aqm[1],
 789			ap_configuration->aqm[2], ap_configuration->aqm[3],
 790			ap_configuration->aqm[4], ap_configuration->aqm[5],
 791			ap_configuration->aqm[6], ap_configuration->aqm[7]);
 792}
 793
 794static BUS_ATTR(ap_usage_domain_mask, 0444,
 795		ap_usage_domain_mask_show, NULL);
 796
 797static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
 798{
 799	return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
 800}
 801
 802static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
 803{
 804	return snprintf(buf, PAGE_SIZE, "%d\n",
 805			ap_using_interrupts() ? 1 : 0);
 806}
 807
 808static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
 809
 810static ssize_t ap_config_time_store(struct bus_type *bus,
 811				    const char *buf, size_t count)
 812{
 813	int time;
 814
 815	if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
 816		return -EINVAL;
 817	ap_config_time = time;
 818	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
 
 
 
 
 819	return count;
 820}
 821
 822static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
 823
 824static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
 825{
 826	return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
 827}
 828
 829static ssize_t ap_poll_thread_store(struct bus_type *bus,
 830				    const char *buf, size_t count)
 831{
 832	int flag, rc;
 833
 834	if (sscanf(buf, "%d\n", &flag) != 1)
 835		return -EINVAL;
 836	if (flag) {
 837		rc = ap_poll_thread_start();
 838		if (rc)
 839			count = rc;
 840	} else
 
 841		ap_poll_thread_stop();
 842	return count;
 843}
 844
 845static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
 846
 847static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
 848{
 849	return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
 850}
 851
 852static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
 853				  size_t count)
 854{
 855	unsigned long long time;
 856	ktime_t hr_time;
 857
 858	/* 120 seconds = maximum poll interval */
 859	if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
 860	    time > 120000000000ULL)
 861		return -EINVAL;
 862	poll_timeout = time;
 863	hr_time = poll_timeout;
 864
 865	spin_lock_bh(&ap_poll_timer_lock);
 866	hrtimer_cancel(&ap_poll_timer);
 867	hrtimer_set_expires(&ap_poll_timer, hr_time);
 868	hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
 869	spin_unlock_bh(&ap_poll_timer_lock);
 870
 
 
 
 
 
 871	return count;
 872}
 873
 874static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
 875
 876static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
 877{
 878	int max_domain_id;
 879
 880	if (ap_configuration)
 881		max_domain_id = ap_max_domain_id ? : -1;
 882	else
 883		max_domain_id = 15;
 884	return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
 885}
 886
 887static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
 888
 889static struct bus_attribute *const ap_bus_attrs[] = {
 890	&bus_attr_ap_domain,
 891	&bus_attr_ap_control_domain_mask,
 892	&bus_attr_ap_usage_domain_mask,
 893	&bus_attr_config_time,
 894	&bus_attr_poll_thread,
 895	&bus_attr_ap_interrupts,
 896	&bus_attr_poll_timeout,
 897	&bus_attr_ap_max_domain_id,
 898	NULL,
 899};
 900
 901/**
 902 * ap_select_domain(): Select an AP domain.
 903 *
 904 * Pick one of the 16 AP domains.
 905 */
 906static int ap_select_domain(void)
 907{
 908	int count, max_count, best_domain;
 909	struct ap_queue_status status;
 910	int i, j;
 911
 912	/*
 913	 * We want to use a single domain. Either the one specified with
 914	 * the "domain=" parameter or the domain with the maximum number
 915	 * of devices.
 916	 */
 917	spin_lock_bh(&ap_domain_lock);
 918	if (ap_domain_index >= 0) {
 919		/* Domain has already been selected. */
 920		spin_unlock_bh(&ap_domain_lock);
 921		return 0;
 922	}
 923	best_domain = -1;
 924	max_count = 0;
 925	for (i = 0; i < AP_DOMAINS; i++) {
 926		if (!ap_test_config_domain(i))
 927			continue;
 928		count = 0;
 929		for (j = 0; j < AP_DEVICES; j++) {
 930			if (!ap_test_config_card_id(j))
 931				continue;
 932			status = ap_test_queue(AP_MKQID(j, i), NULL);
 933			if (status.response_code != AP_RESPONSE_NORMAL)
 934				continue;
 935			count++;
 936		}
 937		if (count > max_count) {
 938			max_count = count;
 939			best_domain = i;
 940		}
 941	}
 942	if (best_domain >= 0){
 943		ap_domain_index = best_domain;
 944		spin_unlock_bh(&ap_domain_lock);
 945		return 0;
 946	}
 947	spin_unlock_bh(&ap_domain_lock);
 948	return -ENODEV;
 949}
 950
 951/*
 952 * helper function to be used with bus_find_dev
 953 * matches for the card device with the given id
 
 
 954 */
 955static int __match_card_device_with_id(struct device *dev, void *data)
 956{
 957	return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 958}
 959
 960/* helper function to be used with bus_find_dev
 961 * matches for the queue device with a given qid
 962 */
 963static int __match_queue_device_with_qid(struct device *dev, void *data)
 964{
 965	return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
 
 966}
 967
 968/**
 969 * ap_scan_bus(): Scan the AP bus for new devices
 970 * Runs periodically, workqueue timer (ap_config_time)
 
 
 
 971 */
 
 
 
 
 
 
 
 
 
 
 
 
 972static void ap_scan_bus(struct work_struct *unused)
 973{
 974	struct ap_queue *aq;
 975	struct ap_card *ac;
 976	struct device *dev;
 977	ap_qid_t qid;
 978	int depth = 0, type = 0;
 979	unsigned int functions = 0;
 980	int rc, id, dom, borked, domains;
 981
 982	AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
 983
 984	ap_query_configuration();
 985	if (ap_select_domain() != 0)
 986		goto out;
 987
 988	for (id = 0; id < AP_DEVICES; id++) {
 989		/* check if device is registered */
 990		dev = bus_find_device(&ap_bus_type, NULL,
 991				      (void *)(long) id,
 992				      __match_card_device_with_id);
 993		ac = dev ? to_ap_card(dev) : NULL;
 994		if (!ap_test_config_card_id(id)) {
 995			if (dev) {
 996				/* Card device has been removed from
 997				 * configuration, remove the belonging
 998				 * queue devices.
 999				 */
1000				bus_for_each_dev(&ap_bus_type, NULL,
1001					(void *)(long) id,
1002					__ap_queue_devices_with_id_unregister);
1003				/* now remove the card device */
 
 
 
1004				device_unregister(dev);
1005				put_device(dev);
 
1006			}
 
 
1007			continue;
1008		}
1009		/* According to the configuration there should be a card
1010		 * device, so check if there is at least one valid queue
1011		 * and maybe create queue devices and the card device.
1012		 */
1013		domains = 0;
1014		for (dom = 0; dom < AP_DOMAINS; dom++) {
1015			qid = AP_MKQID(id, dom);
1016			dev = bus_find_device(&ap_bus_type, NULL,
1017					      (void *)(long) qid,
1018					      __match_queue_device_with_qid);
1019			aq = dev ? to_ap_queue(dev) : NULL;
1020			if (!ap_test_config_domain(dom)) {
1021				if (dev) {
1022					/* Queue device exists but has been
1023					 * removed from configuration.
1024					 */
1025					device_unregister(dev);
1026					put_device(dev);
1027				}
 
 
1028				continue;
1029			}
1030			rc = ap_query_queue(qid, &depth, &type, &functions);
1031			if (dev) {
1032				spin_lock_bh(&aq->lock);
1033				if (rc == -ENODEV ||
1034				    /* adapter reconfiguration */
1035				    (ac && ac->functions != functions))
1036					aq->state = AP_STATE_BORKED;
1037				borked = aq->state == AP_STATE_BORKED;
1038				spin_unlock_bh(&aq->lock);
1039				if (borked)	/* Remove broken device */
1040					device_unregister(dev);
1041				put_device(dev);
1042				if (!borked) {
1043					domains++;
1044					continue;
1045				}
1046			}
1047			if (rc)
1048				continue;
1049			/* new queue device needed */
1050			if (!ac) {
1051				/* but first create the card device */
1052				ac = ap_card_create(id, depth,
1053						    type, functions);
1054				if (!ac)
1055					continue;
1056				ac->ap_dev.device.bus = &ap_bus_type;
1057				ac->ap_dev.device.parent = ap_root_device;
1058				dev_set_name(&ac->ap_dev.device,
1059					     "card%02x", id);
1060				/* Register card with AP bus */
1061				rc = device_register(&ac->ap_dev.device);
1062				if (rc) {
1063					put_device(&ac->ap_dev.device);
1064					ac = NULL;
1065					break;
1066				}
1067				/* get it and thus adjust reference counter */
1068				get_device(&ac->ap_dev.device);
1069				/* Add card device to card list */
1070				spin_lock_bh(&ap_list_lock);
1071				list_add(&ac->list, &ap_card_list);
1072				spin_unlock_bh(&ap_list_lock);
1073			}
1074			/* now create the new queue device */
1075			aq = ap_queue_create(qid, type);
1076			if (!aq)
1077				continue;
1078			aq->card = ac;
1079			aq->ap_dev.device.bus = &ap_bus_type;
1080			aq->ap_dev.device.parent = &ac->ap_dev.device;
1081			dev_set_name(&aq->ap_dev.device,
1082				     "%02x.%04x", id, dom);
1083			/* Add queue device to card queue list */
1084			spin_lock_bh(&ap_list_lock);
1085			list_add(&aq->list, &ac->queues);
1086			spin_unlock_bh(&ap_list_lock);
1087			/* Start with a device reset */
1088			spin_lock_bh(&aq->lock);
1089			ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
1090			spin_unlock_bh(&aq->lock);
1091			/* Register device */
1092			rc = device_register(&aq->ap_dev.device);
1093			if (rc) {
1094				spin_lock_bh(&ap_list_lock);
1095				list_del_init(&aq->list);
1096				spin_unlock_bh(&ap_list_lock);
1097				put_device(&aq->ap_dev.device);
1098				continue;
1099			}
1100			domains++;
1101		} /* end domain loop */
1102		if (ac) {
1103			/* remove card dev if there are no queue devices */
1104			if (!domains)
1105				device_unregister(&ac->ap_dev.device);
1106			put_device(&ac->ap_dev.device);
 
 
 
 
1107		}
1108	} /* end device loop */
1109out:
1110	mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1111}
1112
1113static void ap_config_timeout(unsigned long ptr)
 
1114{
1115	if (ap_suspend_flag)
1116		return;
1117	queue_work(system_long_wq, &ap_scan_work);
1118}
1119
1120static void ap_reset_domain(void)
 
 
 
 
 
1121{
1122	int i;
1123
1124	if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index))
1125		return;
1126	for (i = 0; i < AP_DEVICES; i++)
1127		ap_rapq(AP_MKQID(i, ap_domain_index));
 
 
 
 
 
 
 
 
1128}
1129
1130static void ap_reset_all(void)
 
 
 
 
 
 
 
 
1131{
1132	int i, j;
 
1133
1134	for (i = 0; i < AP_DOMAINS; i++) {
1135		if (!ap_test_config_domain(i))
1136			continue;
1137		for (j = 0; j < AP_DEVICES; j++) {
1138			if (!ap_test_config_card_id(j))
 
 
 
 
 
1139				continue;
1140			ap_rapq(AP_MKQID(j, i));
 
 
 
1141		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1142	}
 
1143}
1144
1145static struct reset_call ap_reset_call = {
1146	.fn = ap_reset_all,
1147};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1148
1149int __init ap_debug_init(void)
 
 
 
 
 
 
 
1150{
1151	ap_dbf_root = debugfs_create_dir("ap", NULL);
1152	ap_dbf_info = debug_register("ap", 1, 1,
1153				     DBF_MAX_SPRINTF_ARGS * sizeof(long));
1154	debug_register_view(ap_dbf_info, &debug_sprintf_view);
1155	debug_set_level(ap_dbf_info, DBF_ERR);
1156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1157	return 0;
1158}
1159
1160void ap_debug_exit(void)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1161{
1162	debugfs_remove(ap_dbf_root);
1163	debug_unregister(ap_dbf_info);
1164}
1165
1166/**
1167 * ap_module_init(): The module initialization code.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1168 *
1169 * Initializes the module.
 
 
1170 */
1171int __init ap_module_init(void)
1172{
1173	int max_domain_id;
1174	int rc, i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1175
1176	rc = ap_debug_init();
1177	if (rc)
1178		return rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1179
1180	if (ap_instructions_available() != 0) {
1181		pr_warn("The hardware system does not support AP instructions\n");
1182		return -ENODEV;
 
 
 
 
 
1183	}
 
 
 
 
1184
1185	/* Get AP configuration data if available */
1186	ap_init_configuration();
 
1187
1188	if (ap_configuration)
1189		max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1);
 
 
 
 
 
 
 
1190	else
1191		max_domain_id = 15;
1192	if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1193		pr_warn("%d is not a valid cryptographic domain\n",
1194			ap_domain_index);
1195		rc = -EINVAL;
1196		goto out_free;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1197	}
1198	/* In resume callback we need to know if the user had set the domain.
1199	 * If so, we can not just reset it.
1200	 */
1201	if (ap_domain_index >= 0)
1202		user_set_domain = 1;
1203
 
 
 
 
 
1204	if (ap_interrupts_available()) {
1205		rc = register_adapter_interrupt(&ap_airq);
1206		ap_airq_flag = (rc == 0);
 
 
 
 
 
1207	}
1208
1209	register_reset_call(&ap_reset_call);
1210
1211	/* Create /sys/bus/ap. */
1212	rc = bus_register(&ap_bus_type);
1213	if (rc)
1214		goto out;
1215	for (i = 0; ap_bus_attrs[i]; i++) {
1216		rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1217		if (rc)
1218			goto out_bus;
1219	}
1220
1221	/* Create /sys/devices/ap. */
1222	ap_root_device = root_device_register("ap");
1223	rc = PTR_RET(ap_root_device);
1224	if (rc)
1225		goto out_bus;
1226
 
 
 
 
 
 
 
 
 
1227	/* Setup the AP bus rescan timer. */
1228	setup_timer(&ap_config_timer, ap_config_timeout, 0);
 
 
 
 
1229
1230	/*
1231	 * Setup the high resultion poll timer.
1232	 * If we are running under z/VM adjust polling to z/VM polling rate.
1233	 */
1234	if (MACHINE_IS_VM)
1235		poll_timeout = 1500000;
1236	spin_lock_init(&ap_poll_timer_lock);
1237	hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1238	ap_poll_timer.function = ap_poll_timeout;
1239
1240	/* Start the low priority AP bus poll thread. */
1241	if (ap_thread_flag) {
1242		rc = ap_poll_thread_start();
1243		if (rc)
1244			goto out_work;
1245	}
1246
1247	rc = register_pm_notifier(&ap_power_notifier);
1248	if (rc)
1249		goto out_pm;
1250
1251	queue_work(system_long_wq, &ap_scan_work);
1252	initialised = true;
1253
1254	return 0;
1255
1256out_pm:
1257	ap_poll_thread_stop();
1258out_work:
 
1259	hrtimer_cancel(&ap_poll_timer);
 
 
1260	root_device_unregister(ap_root_device);
1261out_bus:
1262	while (i--)
1263		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1264	bus_unregister(&ap_bus_type);
1265out:
1266	unregister_reset_call(&ap_reset_call);
1267	if (ap_using_interrupts())
1268		unregister_adapter_interrupt(&ap_airq);
1269out_free:
1270	kfree(ap_configuration);
1271	return rc;
1272}
1273
 
 
 
 
 
1274/**
1275 * ap_modules_exit(): The module termination code
1276 *
1277 * Terminates the module.
1278 */
1279void ap_module_exit(void)
1280{
1281	int i;
 
1282
1283	initialised = false;
1284	ap_reset_domain();
1285	ap_poll_thread_stop();
1286	del_timer_sync(&ap_config_timer);
1287	hrtimer_cancel(&ap_poll_timer);
 
1288	tasklet_kill(&ap_tasklet);
1289
1290	/* first remove queue devices */
1291	bus_for_each_dev(&ap_bus_type, NULL, NULL,
1292			 __ap_queue_devices_unregister);
1293	/* now remove the card devices */
1294	bus_for_each_dev(&ap_bus_type, NULL, NULL,
1295			 __ap_card_devices_unregister);
1296
1297	/* remove bus attributes */
1298	for (i = 0; ap_bus_attrs[i]; i++)
1299		bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1300	unregister_pm_notifier(&ap_power_notifier);
1301	root_device_unregister(ap_root_device);
1302	bus_unregister(&ap_bus_type);
1303	kfree(ap_configuration);
1304	unregister_reset_call(&ap_reset_call);
1305	if (ap_using_interrupts())
1306		unregister_adapter_interrupt(&ap_airq);
1307
1308	ap_debug_exit();
1309}
1310
 
1311module_init(ap_module_init);
1312module_exit(ap_module_exit);