Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * IUCV base infrastructure.
   4 *
   5 * Copyright IBM Corp. 2001, 2009
   6 *
   7 * Author(s):
   8 *    Original source:
   9 *	Alan Altmark (Alan_Altmark@us.ibm.com)	Sept. 2000
  10 *	Xenia Tkatschow (xenia@us.ibm.com)
  11 *    2Gb awareness and general cleanup:
  12 *	Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
  13 *    Rewritten for af_iucv:
  14 *	Martin Schwidefsky <schwidefsky@de.ibm.com>
  15 *    PM functions:
  16 *	Ursula Braun (ursula.braun@de.ibm.com)
  17 *
  18 * Documentation used:
  19 *    The original source
  20 *    CP Programming Service, IBM document # SC24-5760
  21 */
  22
  23#define KMSG_COMPONENT "iucv"
  24#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  25
  26#include <linux/kernel_stat.h>
  27#include <linux/module.h>
  28#include <linux/moduleparam.h>
  29#include <linux/spinlock.h>
  30#include <linux/kernel.h>
  31#include <linux/slab.h>
  32#include <linux/init.h>
  33#include <linux/interrupt.h>
  34#include <linux/list.h>
  35#include <linux/errno.h>
  36#include <linux/err.h>
  37#include <linux/device.h>
  38#include <linux/cpu.h>
  39#include <linux/reboot.h>
  40#include <net/iucv/iucv.h>
  41#include <linux/atomic.h>
  42#include <asm/ebcdic.h>
  43#include <asm/io.h>
  44#include <asm/irq.h>
  45#include <asm/smp.h>
  46
  47/*
  48 * FLAGS:
  49 * All flags are defined in the field IPFLAGS1 of each function
  50 * and can be found in CP Programming Services.
  51 * IPSRCCLS - Indicates you have specified a source class.
  52 * IPTRGCLS - Indicates you have specified a target class.
  53 * IPFGPID  - Indicates you have specified a pathid.
  54 * IPFGMID  - Indicates you have specified a message ID.
  55 * IPNORPY  - Indicates a one-way message. No reply expected.
  56 * IPALL    - Indicates that all paths are affected.
  57 */
  58#define IUCV_IPSRCCLS	0x01
  59#define IUCV_IPTRGCLS	0x01
  60#define IUCV_IPFGPID	0x02
  61#define IUCV_IPFGMID	0x04
  62#define IUCV_IPNORPY	0x10
  63#define IUCV_IPALL	0x80
  64
  65static int iucv_bus_match(struct device *dev, const struct device_driver *drv)
  66{
  67	return 0;
  68}
  69
  70const struct bus_type iucv_bus = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  71	.name = "iucv",
  72	.match = iucv_bus_match,
 
  73};
  74EXPORT_SYMBOL(iucv_bus);
  75
  76static struct device *iucv_root;
  77
  78static void iucv_release_device(struct device *device)
  79{
  80	kfree(device);
  81}
  82
  83struct device *iucv_alloc_device(const struct attribute_group **attrs,
  84				 struct device_driver *driver,
  85				 void *priv, const char *fmt, ...)
  86{
  87	struct device *dev;
  88	va_list vargs;
  89	char buf[20];
  90	int rc;
  91
  92	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  93	if (!dev)
  94		goto out_error;
  95	va_start(vargs, fmt);
  96	vsnprintf(buf, sizeof(buf), fmt, vargs);
  97	rc = dev_set_name(dev, "%s", buf);
  98	va_end(vargs);
  99	if (rc)
 100		goto out_error;
 101	dev->bus = &iucv_bus;
 102	dev->parent = iucv_root;
 103	dev->driver = driver;
 104	dev->groups = attrs;
 105	dev->release = iucv_release_device;
 106	dev_set_drvdata(dev, priv);
 107	return dev;
 108
 109out_error:
 110	kfree(dev);
 111	return NULL;
 112}
 113EXPORT_SYMBOL(iucv_alloc_device);
 114
 115static int iucv_available;
 116
 117/* General IUCV interrupt structure */
 118struct iucv_irq_data {
 119	u16 ippathid;
 120	u8  ipflags1;
 121	u8  iptype;
 122	u32 res2[9];
 123};
 124
 125struct iucv_irq_list {
 126	struct list_head list;
 127	struct iucv_irq_data data;
 128};
 129
 130static struct iucv_irq_data *iucv_irq_data[NR_CPUS];
 131static cpumask_t iucv_buffer_cpumask = { CPU_BITS_NONE };
 132static cpumask_t iucv_irq_cpumask = { CPU_BITS_NONE };
 133
 134/*
 135 * Queue of interrupt buffers lock for delivery via the tasklet
 136 * (fast but can't call smp_call_function).
 137 */
 138static LIST_HEAD(iucv_task_queue);
 139
 140/*
 141 * The tasklet for fast delivery of iucv interrupts.
 142 */
 143static void iucv_tasklet_fn(unsigned long);
 144static DECLARE_TASKLET_OLD(iucv_tasklet, iucv_tasklet_fn);
 145
 146/*
 147 * Queue of interrupt buffers for delivery via a work queue
 148 * (slower but can call smp_call_function).
 149 */
 150static LIST_HEAD(iucv_work_queue);
 151
 152/*
 153 * The work element to deliver path pending interrupts.
 154 */
 155static void iucv_work_fn(struct work_struct *work);
 156static DECLARE_WORK(iucv_work, iucv_work_fn);
 157
 158/*
 159 * Spinlock protecting task and work queue.
 160 */
 161static DEFINE_SPINLOCK(iucv_queue_lock);
 162
 163enum iucv_command_codes {
 164	IUCV_QUERY = 0,
 165	IUCV_RETRIEVE_BUFFER = 2,
 166	IUCV_SEND = 4,
 167	IUCV_RECEIVE = 5,
 168	IUCV_REPLY = 6,
 169	IUCV_REJECT = 8,
 170	IUCV_PURGE = 9,
 171	IUCV_ACCEPT = 10,
 172	IUCV_CONNECT = 11,
 173	IUCV_DECLARE_BUFFER = 12,
 174	IUCV_QUIESCE = 13,
 175	IUCV_RESUME = 14,
 176	IUCV_SEVER = 15,
 177	IUCV_SETMASK = 16,
 178	IUCV_SETCONTROLMASK = 17,
 179};
 180
 181/*
 182 * Error messages that are used with the iucv_sever function. They get
 183 * converted to EBCDIC.
 184 */
 185static char iucv_error_no_listener[16] = "NO LISTENER";
 186static char iucv_error_no_memory[16] = "NO MEMORY";
 187static char iucv_error_pathid[16] = "INVALID PATHID";
 188
 189/*
 190 * iucv_handler_list: List of registered handlers.
 191 */
 192static LIST_HEAD(iucv_handler_list);
 193
 194/*
 195 * iucv_path_table: array of pointers to iucv_path structures.
 196 */
 197static struct iucv_path **iucv_path_table;
 198static unsigned long iucv_max_pathid;
 199
 200/*
 201 * iucv_lock: spinlock protecting iucv_handler_list and iucv_pathid_table
 202 */
 203static DEFINE_SPINLOCK(iucv_table_lock);
 204
 205/*
 206 * iucv_active_cpu: contains the number of the cpu executing the tasklet
 207 * or the work handler. Needed for iucv_path_sever called from tasklet.
 208 */
 209static int iucv_active_cpu = -1;
 210
 211/*
 212 * Mutex and wait queue for iucv_register/iucv_unregister.
 213 */
 214static DEFINE_MUTEX(iucv_register_mutex);
 215
 216/*
 217 * Counter for number of non-smp capable handlers.
 218 */
 219static int iucv_nonsmp_handler;
 220
 221/*
 222 * IUCV control data structure. Used by iucv_path_accept, iucv_path_connect,
 223 * iucv_path_quiesce and iucv_path_sever.
 224 */
 225struct iucv_cmd_control {
 226	u16 ippathid;
 227	u8  ipflags1;
 228	u8  iprcode;
 229	u16 ipmsglim;
 230	u16 res1;
 231	u8  ipvmid[8];
 232	u8  ipuser[16];
 233	u8  iptarget[8];
 234} __attribute__ ((packed,aligned(8)));
 235
 236/*
 237 * Data in parameter list iucv structure. Used by iucv_message_send,
 238 * iucv_message_send2way and iucv_message_reply.
 239 */
 240struct iucv_cmd_dpl {
 241	u16 ippathid;
 242	u8  ipflags1;
 243	u8  iprcode;
 244	u32 ipmsgid;
 245	u32 iptrgcls;
 246	u8  iprmmsg[8];
 247	u32 ipsrccls;
 248	u32 ipmsgtag;
 249	dma32_t ipbfadr2;
 250	u32 ipbfln2f;
 251	u32 res;
 252} __attribute__ ((packed,aligned(8)));
 253
 254/*
 255 * Data in buffer iucv structure. Used by iucv_message_receive,
 256 * iucv_message_reject, iucv_message_send, iucv_message_send2way
 257 * and iucv_declare_cpu.
 258 */
 259struct iucv_cmd_db {
 260	u16 ippathid;
 261	u8  ipflags1;
 262	u8  iprcode;
 263	u32 ipmsgid;
 264	u32 iptrgcls;
 265	dma32_t ipbfadr1;
 266	u32 ipbfln1f;
 267	u32 ipsrccls;
 268	u32 ipmsgtag;
 269	dma32_t ipbfadr2;
 270	u32 ipbfln2f;
 271	u32 res;
 272} __attribute__ ((packed,aligned(8)));
 273
 274/*
 275 * Purge message iucv structure. Used by iucv_message_purge.
 276 */
 277struct iucv_cmd_purge {
 278	u16 ippathid;
 279	u8  ipflags1;
 280	u8  iprcode;
 281	u32 ipmsgid;
 282	u8  ipaudit[3];
 283	u8  res1[5];
 284	u32 res2;
 285	u32 ipsrccls;
 286	u32 ipmsgtag;
 287	u32 res3[3];
 288} __attribute__ ((packed,aligned(8)));
 289
 290/*
 291 * Set mask iucv structure. Used by iucv_enable_cpu.
 292 */
 293struct iucv_cmd_set_mask {
 294	u8  ipmask;
 295	u8  res1[2];
 296	u8  iprcode;
 297	u32 res2[9];
 298} __attribute__ ((packed,aligned(8)));
 299
 300union iucv_param {
 301	struct iucv_cmd_control ctrl;
 302	struct iucv_cmd_dpl dpl;
 303	struct iucv_cmd_db db;
 304	struct iucv_cmd_purge purge;
 305	struct iucv_cmd_set_mask set_mask;
 306};
 307
 308/*
 309 * Anchor for per-cpu IUCV command parameter block.
 310 */
 311static union iucv_param *iucv_param[NR_CPUS];
 312static union iucv_param *iucv_param_irq[NR_CPUS];
 313
 314/**
 315 * __iucv_call_b2f0
 316 * @command: identifier of IUCV call to CP.
 317 * @parm: pointer to a struct iucv_parm block
 318 *
 319 * Calls CP to execute IUCV commands.
 320 *
 321 * Returns the result of the CP IUCV call.
 322 */
 323static inline int __iucv_call_b2f0(int command, union iucv_param *parm)
 324{
 325	unsigned long reg1 = virt_to_phys(parm);
 326	int cc;
 
 327
 
 
 328	asm volatile(
 329		"	lgr	0,%[reg0]\n"
 330		"	lgr	1,%[reg1]\n"
 331		"	.long	0xb2f01000\n"
 332		"	ipm	%[cc]\n"
 333		"	srl	%[cc],28\n"
 334		: [cc] "=&d" (cc), "+m" (*parm)
 335		: [reg0] "d" ((unsigned long)command),
 336		  [reg1] "d" (reg1)
 337		: "cc", "0", "1");
 338	return cc;
 339}
 340
 341static inline int iucv_call_b2f0(int command, union iucv_param *parm)
 342{
 343	int ccode;
 344
 345	ccode = __iucv_call_b2f0(command, parm);
 346	return ccode == 1 ? parm->ctrl.iprcode : ccode;
 347}
 348
 349/*
 350 * iucv_query_maxconn
 351 *
 352 * Determines the maximum number of connections that may be established.
 353 *
 354 * Returns the maximum number of connections or -EPERM is IUCV is not
 355 * available.
 356 */
 357static int __iucv_query_maxconn(void *param, unsigned long *max_pathid)
 358{
 359	unsigned long reg1 = virt_to_phys(param);
 360	int cc;
 
 361
 
 
 362	asm volatile (
 363		"	lghi	0,%[cmd]\n"
 364		"	lgr	1,%[reg1]\n"
 365		"	.long	0xb2f01000\n"
 366		"	ipm	%[cc]\n"
 367		"	srl	%[cc],28\n"
 368		"	lgr	%[reg1],1\n"
 369		: [cc] "=&d" (cc), [reg1] "+&d" (reg1)
 370		: [cmd] "K" (IUCV_QUERY)
 371		: "cc", "0", "1");
 372	*max_pathid = reg1;
 373	return cc;
 374}
 375
 376static int iucv_query_maxconn(void)
 377{
 378	unsigned long max_pathid;
 379	void *param;
 380	int ccode;
 381
 382	param = kzalloc(sizeof(union iucv_param), GFP_KERNEL | GFP_DMA);
 383	if (!param)
 384		return -ENOMEM;
 385	ccode = __iucv_query_maxconn(param, &max_pathid);
 386	if (ccode == 0)
 387		iucv_max_pathid = max_pathid;
 388	kfree(param);
 389	return ccode ? -EPERM : 0;
 390}
 391
 392/**
 393 * iucv_allow_cpu
 394 * @data: unused
 395 *
 396 * Allow iucv interrupts on this cpu.
 397 */
 398static void iucv_allow_cpu(void *data)
 399{
 400	int cpu = smp_processor_id();
 401	union iucv_param *parm;
 402
 403	/*
 404	 * Enable all iucv interrupts.
 405	 * ipmask contains bits for the different interrupts
 406	 *	0x80 - Flag to allow nonpriority message pending interrupts
 407	 *	0x40 - Flag to allow priority message pending interrupts
 408	 *	0x20 - Flag to allow nonpriority message completion interrupts
 409	 *	0x10 - Flag to allow priority message completion interrupts
 410	 *	0x08 - Flag to allow IUCV control interrupts
 411	 */
 412	parm = iucv_param_irq[cpu];
 413	memset(parm, 0, sizeof(union iucv_param));
 414	parm->set_mask.ipmask = 0xf8;
 415	iucv_call_b2f0(IUCV_SETMASK, parm);
 416
 417	/*
 418	 * Enable all iucv control interrupts.
 419	 * ipmask contains bits for the different interrupts
 420	 *	0x80 - Flag to allow pending connections interrupts
 421	 *	0x40 - Flag to allow connection complete interrupts
 422	 *	0x20 - Flag to allow connection severed interrupts
 423	 *	0x10 - Flag to allow connection quiesced interrupts
 424	 *	0x08 - Flag to allow connection resumed interrupts
 425	 */
 426	memset(parm, 0, sizeof(union iucv_param));
 427	parm->set_mask.ipmask = 0xf8;
 428	iucv_call_b2f0(IUCV_SETCONTROLMASK, parm);
 429	/* Set indication that iucv interrupts are allowed for this cpu. */
 430	cpumask_set_cpu(cpu, &iucv_irq_cpumask);
 431}
 432
 433/**
 434 * iucv_block_cpu
 435 * @data: unused
 436 *
 437 * Block iucv interrupts on this cpu.
 438 */
 439static void iucv_block_cpu(void *data)
 440{
 441	int cpu = smp_processor_id();
 442	union iucv_param *parm;
 443
 444	/* Disable all iucv interrupts. */
 445	parm = iucv_param_irq[cpu];
 446	memset(parm, 0, sizeof(union iucv_param));
 447	iucv_call_b2f0(IUCV_SETMASK, parm);
 448
 449	/* Clear indication that iucv interrupts are allowed for this cpu. */
 450	cpumask_clear_cpu(cpu, &iucv_irq_cpumask);
 451}
 452
 453/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 454 * iucv_declare_cpu
 455 * @data: unused
 456 *
 457 * Declare a interrupt buffer on this cpu.
 458 */
 459static void iucv_declare_cpu(void *data)
 460{
 461	int cpu = smp_processor_id();
 462	union iucv_param *parm;
 463	int rc;
 464
 465	if (cpumask_test_cpu(cpu, &iucv_buffer_cpumask))
 466		return;
 467
 468	/* Declare interrupt buffer. */
 469	parm = iucv_param_irq[cpu];
 470	memset(parm, 0, sizeof(union iucv_param));
 471	parm->db.ipbfadr1 = virt_to_dma32(iucv_irq_data[cpu]);
 472	rc = iucv_call_b2f0(IUCV_DECLARE_BUFFER, parm);
 473	if (rc) {
 474		char *err = "Unknown";
 475		switch (rc) {
 476		case 0x03:
 477			err = "Directory error";
 478			break;
 479		case 0x0a:
 480			err = "Invalid length";
 481			break;
 482		case 0x13:
 483			err = "Buffer already exists";
 484			break;
 485		case 0x3e:
 486			err = "Buffer overlap";
 487			break;
 488		case 0x5c:
 489			err = "Paging or storage error";
 490			break;
 491		}
 492		pr_warn("Defining an interrupt buffer on CPU %i failed with 0x%02x (%s)\n",
 493			cpu, rc, err);
 494		return;
 495	}
 496
 497	/* Set indication that an iucv buffer exists for this cpu. */
 498	cpumask_set_cpu(cpu, &iucv_buffer_cpumask);
 499
 500	if (iucv_nonsmp_handler == 0 || cpumask_empty(&iucv_irq_cpumask))
 501		/* Enable iucv interrupts on this cpu. */
 502		iucv_allow_cpu(NULL);
 503	else
 504		/* Disable iucv interrupts on this cpu. */
 505		iucv_block_cpu(NULL);
 506}
 507
 508/**
 509 * iucv_retrieve_cpu
 510 * @data: unused
 511 *
 512 * Retrieve interrupt buffer on this cpu.
 513 */
 514static void iucv_retrieve_cpu(void *data)
 515{
 516	int cpu = smp_processor_id();
 517	union iucv_param *parm;
 518
 519	if (!cpumask_test_cpu(cpu, &iucv_buffer_cpumask))
 520		return;
 521
 522	/* Block iucv interrupts. */
 523	iucv_block_cpu(NULL);
 524
 525	/* Retrieve interrupt buffer. */
 526	parm = iucv_param_irq[cpu];
 527	iucv_call_b2f0(IUCV_RETRIEVE_BUFFER, parm);
 528
 529	/* Clear indication that an iucv buffer exists for this cpu. */
 530	cpumask_clear_cpu(cpu, &iucv_buffer_cpumask);
 531}
 532
 533/*
 534 * iucv_setmask_mp
 535 *
 536 * Allow iucv interrupts on all cpus.
 537 */
 538static void iucv_setmask_mp(void)
 539{
 540	int cpu;
 541
 542	cpus_read_lock();
 543	for_each_online_cpu(cpu)
 544		/* Enable all cpus with a declared buffer. */
 545		if (cpumask_test_cpu(cpu, &iucv_buffer_cpumask) &&
 546		    !cpumask_test_cpu(cpu, &iucv_irq_cpumask))
 547			smp_call_function_single(cpu, iucv_allow_cpu,
 548						 NULL, 1);
 549	cpus_read_unlock();
 550}
 551
 552/*
 553 * iucv_setmask_up
 554 *
 555 * Allow iucv interrupts on a single cpu.
 556 */
 557static void iucv_setmask_up(void)
 558{
 559	static cpumask_t cpumask;
 560	int cpu;
 561
 562	/* Disable all cpu but the first in cpu_irq_cpumask. */
 563	cpumask_copy(&cpumask, &iucv_irq_cpumask);
 564	cpumask_clear_cpu(cpumask_first(&iucv_irq_cpumask), &cpumask);
 565	for_each_cpu(cpu, &cpumask)
 566		smp_call_function_single(cpu, iucv_block_cpu, NULL, 1);
 567}
 568
 569/*
 570 * iucv_enable
 571 *
 572 * This function makes iucv ready for use. It allocates the pathid
 573 * table, declares an iucv interrupt buffer and enables the iucv
 574 * interrupts. Called when the first user has registered an iucv
 575 * handler.
 576 */
 577static int iucv_enable(void)
 578{
 579	size_t alloc_size;
 580	int cpu, rc;
 581
 582	cpus_read_lock();
 583	rc = -ENOMEM;
 584	alloc_size = iucv_max_pathid * sizeof(*iucv_path_table);
 585	iucv_path_table = kzalloc(alloc_size, GFP_KERNEL);
 586	if (!iucv_path_table)
 587		goto out;
 588	/* Declare per cpu buffers. */
 589	rc = -EIO;
 590	for_each_online_cpu(cpu)
 591		smp_call_function_single(cpu, iucv_declare_cpu, NULL, 1);
 592	if (cpumask_empty(&iucv_buffer_cpumask))
 593		/* No cpu could declare an iucv buffer. */
 594		goto out;
 595	cpus_read_unlock();
 596	return 0;
 597out:
 598	kfree(iucv_path_table);
 599	iucv_path_table = NULL;
 600	cpus_read_unlock();
 601	return rc;
 602}
 603
 604/*
 605 * iucv_disable
 606 *
 607 * This function shuts down iucv. It disables iucv interrupts, retrieves
 608 * the iucv interrupt buffer and frees the pathid table. Called after the
 609 * last user unregister its iucv handler.
 610 */
 611static void iucv_disable(void)
 612{
 613	cpus_read_lock();
 614	on_each_cpu(iucv_retrieve_cpu, NULL, 1);
 615	kfree(iucv_path_table);
 616	iucv_path_table = NULL;
 617	cpus_read_unlock();
 618}
 619
 620static int iucv_cpu_dead(unsigned int cpu)
 621{
 622	kfree(iucv_param_irq[cpu]);
 623	iucv_param_irq[cpu] = NULL;
 624	kfree(iucv_param[cpu]);
 625	iucv_param[cpu] = NULL;
 626	kfree(iucv_irq_data[cpu]);
 627	iucv_irq_data[cpu] = NULL;
 628	return 0;
 629}
 630
 631static int iucv_cpu_prepare(unsigned int cpu)
 632{
 633	/* Note: GFP_DMA used to get memory below 2G */
 634	iucv_irq_data[cpu] = kmalloc_node(sizeof(struct iucv_irq_data),
 635			     GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
 636	if (!iucv_irq_data[cpu])
 637		goto out_free;
 638
 639	/* Allocate parameter blocks. */
 640	iucv_param[cpu] = kmalloc_node(sizeof(union iucv_param),
 641			  GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
 642	if (!iucv_param[cpu])
 643		goto out_free;
 644
 645	iucv_param_irq[cpu] = kmalloc_node(sizeof(union iucv_param),
 646			  GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
 647	if (!iucv_param_irq[cpu])
 648		goto out_free;
 649
 650	return 0;
 651
 652out_free:
 653	iucv_cpu_dead(cpu);
 654	return -ENOMEM;
 655}
 656
 657static int iucv_cpu_online(unsigned int cpu)
 658{
 659	if (!iucv_path_table)
 660		return 0;
 661	iucv_declare_cpu(NULL);
 662	return 0;
 663}
 664
 665static int iucv_cpu_down_prep(unsigned int cpu)
 666{
 667	cpumask_var_t cpumask;
 668	int ret = 0;
 669
 670	if (!iucv_path_table)
 671		return 0;
 672
 673	if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
 674		return -ENOMEM;
 675
 676	cpumask_copy(cpumask, &iucv_buffer_cpumask);
 677	cpumask_clear_cpu(cpu, cpumask);
 678	if (cpumask_empty(cpumask)) {
 679		/* Can't offline last IUCV enabled cpu. */
 680		ret = -EINVAL;
 681		goto __free_cpumask;
 682	}
 683
 684	iucv_retrieve_cpu(NULL);
 685	if (!cpumask_empty(&iucv_irq_cpumask))
 686		goto __free_cpumask;
 687
 688	smp_call_function_single(cpumask_first(&iucv_buffer_cpumask),
 689				 iucv_allow_cpu, NULL, 1);
 690
 691__free_cpumask:
 692	free_cpumask_var(cpumask);
 693	return ret;
 694}
 695
 696/**
 697 * iucv_sever_pathid
 698 * @pathid: path identification number.
 699 * @userdata: 16-bytes of user data.
 700 *
 701 * Sever an iucv path to free up the pathid. Used internally.
 702 */
 703static int iucv_sever_pathid(u16 pathid, u8 *userdata)
 704{
 705	union iucv_param *parm;
 706
 707	parm = iucv_param_irq[smp_processor_id()];
 708	memset(parm, 0, sizeof(union iucv_param));
 709	if (userdata)
 710		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
 711	parm->ctrl.ippathid = pathid;
 712	return iucv_call_b2f0(IUCV_SEVER, parm);
 713}
 714
 715/**
 716 * __iucv_cleanup_queue
 717 * @dummy: unused dummy argument
 718 *
 719 * Nop function called via smp_call_function to force work items from
 720 * pending external iucv interrupts to the work queue.
 721 */
 722static void __iucv_cleanup_queue(void *dummy)
 723{
 724}
 725
 726/**
 727 * iucv_cleanup_queue
 728 *
 729 * Function called after a path has been severed to find all remaining
 730 * work items for the now stale pathid. The caller needs to hold the
 731 * iucv_table_lock.
 732 */
 733static void iucv_cleanup_queue(void)
 734{
 735	struct iucv_irq_list *p, *n;
 736
 737	/*
 738	 * When a path is severed, the pathid can be reused immediately
 739	 * on a iucv connect or a connection pending interrupt. Remove
 740	 * all entries from the task queue that refer to a stale pathid
 741	 * (iucv_path_table[ix] == NULL). Only then do the iucv connect
 742	 * or deliver the connection pending interrupt. To get all the
 743	 * pending interrupts force them to the work queue by calling
 744	 * an empty function on all cpus.
 745	 */
 746	smp_call_function(__iucv_cleanup_queue, NULL, 1);
 747	spin_lock_irq(&iucv_queue_lock);
 748	list_for_each_entry_safe(p, n, &iucv_task_queue, list) {
 749		/* Remove stale work items from the task queue. */
 750		if (iucv_path_table[p->data.ippathid] == NULL) {
 751			list_del(&p->list);
 752			kfree(p);
 753		}
 754	}
 755	spin_unlock_irq(&iucv_queue_lock);
 756}
 757
 758/**
 759 * iucv_register:
 760 * @handler: address of iucv handler structure
 761 * @smp: != 0 indicates that the handler can deal with out of order messages
 762 *
 763 * Registers a driver with IUCV.
 764 *
 765 * Returns 0 on success, -ENOMEM if the memory allocation for the pathid
 766 * table failed, or -EIO if IUCV_DECLARE_BUFFER failed on all cpus.
 767 */
 768int iucv_register(struct iucv_handler *handler, int smp)
 769{
 770	int rc;
 771
 772	if (!iucv_available)
 773		return -ENOSYS;
 774	mutex_lock(&iucv_register_mutex);
 775	if (!smp)
 776		iucv_nonsmp_handler++;
 777	if (list_empty(&iucv_handler_list)) {
 778		rc = iucv_enable();
 779		if (rc)
 780			goto out_mutex;
 781	} else if (!smp && iucv_nonsmp_handler == 1)
 782		iucv_setmask_up();
 783	INIT_LIST_HEAD(&handler->paths);
 784
 785	spin_lock_bh(&iucv_table_lock);
 786	list_add_tail(&handler->list, &iucv_handler_list);
 787	spin_unlock_bh(&iucv_table_lock);
 788	rc = 0;
 789out_mutex:
 790	mutex_unlock(&iucv_register_mutex);
 791	return rc;
 792}
 793EXPORT_SYMBOL(iucv_register);
 794
 795/**
 796 * iucv_unregister
 797 * @handler:  address of iucv handler structure
 798 * @smp: != 0 indicates that the handler can deal with out of order messages
 799 *
 800 * Unregister driver from IUCV.
 801 */
 802void iucv_unregister(struct iucv_handler *handler, int smp)
 803{
 804	struct iucv_path *p, *n;
 805
 806	mutex_lock(&iucv_register_mutex);
 807	spin_lock_bh(&iucv_table_lock);
 808	/* Remove handler from the iucv_handler_list. */
 809	list_del_init(&handler->list);
 810	/* Sever all pathids still referring to the handler. */
 811	list_for_each_entry_safe(p, n, &handler->paths, list) {
 812		iucv_sever_pathid(p->pathid, NULL);
 813		iucv_path_table[p->pathid] = NULL;
 814		list_del(&p->list);
 815		iucv_path_free(p);
 816	}
 817	spin_unlock_bh(&iucv_table_lock);
 818	if (!smp)
 819		iucv_nonsmp_handler--;
 820	if (list_empty(&iucv_handler_list))
 821		iucv_disable();
 822	else if (!smp && iucv_nonsmp_handler == 0)
 823		iucv_setmask_mp();
 824	mutex_unlock(&iucv_register_mutex);
 825}
 826EXPORT_SYMBOL(iucv_unregister);
 827
 828static int iucv_reboot_event(struct notifier_block *this,
 829			     unsigned long event, void *ptr)
 830{
 831	int i;
 832
 833	if (cpumask_empty(&iucv_irq_cpumask))
 834		return NOTIFY_DONE;
 835
 836	cpus_read_lock();
 837	on_each_cpu_mask(&iucv_irq_cpumask, iucv_block_cpu, NULL, 1);
 838	preempt_disable();
 839	for (i = 0; i < iucv_max_pathid; i++) {
 840		if (iucv_path_table[i])
 841			iucv_sever_pathid(i, NULL);
 842	}
 843	preempt_enable();
 844	cpus_read_unlock();
 845	iucv_disable();
 846	return NOTIFY_DONE;
 847}
 848
 849static struct notifier_block iucv_reboot_notifier = {
 850	.notifier_call = iucv_reboot_event,
 851};
 852
 853/**
 854 * iucv_path_accept
 855 * @path: address of iucv path structure
 856 * @handler: address of iucv handler structure
 857 * @userdata: 16 bytes of data reflected to the communication partner
 858 * @private: private data passed to interrupt handlers for this path
 859 *
 860 * This function is issued after the user received a connection pending
 861 * external interrupt and now wishes to complete the IUCV communication path.
 862 *
 863 * Returns the result of the CP IUCV call.
 864 */
 865int iucv_path_accept(struct iucv_path *path, struct iucv_handler *handler,
 866		     u8 *userdata, void *private)
 867{
 868	union iucv_param *parm;
 869	int rc;
 870
 871	local_bh_disable();
 872	if (cpumask_empty(&iucv_buffer_cpumask)) {
 873		rc = -EIO;
 874		goto out;
 875	}
 876	/* Prepare parameter block. */
 877	parm = iucv_param[smp_processor_id()];
 878	memset(parm, 0, sizeof(union iucv_param));
 879	parm->ctrl.ippathid = path->pathid;
 880	parm->ctrl.ipmsglim = path->msglim;
 881	if (userdata)
 882		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
 883	parm->ctrl.ipflags1 = path->flags;
 884
 885	rc = iucv_call_b2f0(IUCV_ACCEPT, parm);
 886	if (!rc) {
 887		path->private = private;
 888		path->msglim = parm->ctrl.ipmsglim;
 889		path->flags = parm->ctrl.ipflags1;
 890	}
 891out:
 892	local_bh_enable();
 893	return rc;
 894}
 895EXPORT_SYMBOL(iucv_path_accept);
 896
 897/**
 898 * iucv_path_connect
 899 * @path: address of iucv path structure
 900 * @handler: address of iucv handler structure
 901 * @userid: 8-byte user identification
 902 * @system: 8-byte target system identification
 903 * @userdata: 16 bytes of data reflected to the communication partner
 904 * @private: private data passed to interrupt handlers for this path
 905 *
 906 * This function establishes an IUCV path. Although the connect may complete
 907 * successfully, you are not able to use the path until you receive an IUCV
 908 * Connection Complete external interrupt.
 909 *
 910 * Returns the result of the CP IUCV call.
 911 */
 912int iucv_path_connect(struct iucv_path *path, struct iucv_handler *handler,
 913		      u8 *userid, u8 *system, u8 *userdata,
 914		      void *private)
 915{
 916	union iucv_param *parm;
 917	int rc;
 918
 919	spin_lock_bh(&iucv_table_lock);
 920	iucv_cleanup_queue();
 921	if (cpumask_empty(&iucv_buffer_cpumask)) {
 922		rc = -EIO;
 923		goto out;
 924	}
 925	parm = iucv_param[smp_processor_id()];
 926	memset(parm, 0, sizeof(union iucv_param));
 927	parm->ctrl.ipmsglim = path->msglim;
 928	parm->ctrl.ipflags1 = path->flags;
 929	if (userid) {
 930		memcpy(parm->ctrl.ipvmid, userid, sizeof(parm->ctrl.ipvmid));
 931		ASCEBC(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
 932		EBC_TOUPPER(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
 933	}
 934	if (system) {
 935		memcpy(parm->ctrl.iptarget, system,
 936		       sizeof(parm->ctrl.iptarget));
 937		ASCEBC(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
 938		EBC_TOUPPER(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
 939	}
 940	if (userdata)
 941		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
 942
 943	rc = iucv_call_b2f0(IUCV_CONNECT, parm);
 944	if (!rc) {
 945		if (parm->ctrl.ippathid < iucv_max_pathid) {
 946			path->pathid = parm->ctrl.ippathid;
 947			path->msglim = parm->ctrl.ipmsglim;
 948			path->flags = parm->ctrl.ipflags1;
 949			path->handler = handler;
 950			path->private = private;
 951			list_add_tail(&path->list, &handler->paths);
 952			iucv_path_table[path->pathid] = path;
 953		} else {
 954			iucv_sever_pathid(parm->ctrl.ippathid,
 955					  iucv_error_pathid);
 956			rc = -EIO;
 957		}
 958	}
 959out:
 960	spin_unlock_bh(&iucv_table_lock);
 961	return rc;
 962}
 963EXPORT_SYMBOL(iucv_path_connect);
 964
 965/**
 966 * iucv_path_quiesce:
 967 * @path: address of iucv path structure
 968 * @userdata: 16 bytes of data reflected to the communication partner
 969 *
 970 * This function temporarily suspends incoming messages on an IUCV path.
 971 * You can later reactivate the path by invoking the iucv_resume function.
 972 *
 973 * Returns the result from the CP IUCV call.
 974 */
 975int iucv_path_quiesce(struct iucv_path *path, u8 *userdata)
 976{
 977	union iucv_param *parm;
 978	int rc;
 979
 980	local_bh_disable();
 981	if (cpumask_empty(&iucv_buffer_cpumask)) {
 982		rc = -EIO;
 983		goto out;
 984	}
 985	parm = iucv_param[smp_processor_id()];
 986	memset(parm, 0, sizeof(union iucv_param));
 987	if (userdata)
 988		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
 989	parm->ctrl.ippathid = path->pathid;
 990	rc = iucv_call_b2f0(IUCV_QUIESCE, parm);
 991out:
 992	local_bh_enable();
 993	return rc;
 994}
 995EXPORT_SYMBOL(iucv_path_quiesce);
 996
 997/**
 998 * iucv_path_resume:
 999 * @path: address of iucv path structure
1000 * @userdata: 16 bytes of data reflected to the communication partner
1001 *
1002 * This function resumes incoming messages on an IUCV path that has
1003 * been stopped with iucv_path_quiesce.
1004 *
1005 * Returns the result from the CP IUCV call.
1006 */
1007int iucv_path_resume(struct iucv_path *path, u8 *userdata)
1008{
1009	union iucv_param *parm;
1010	int rc;
1011
1012	local_bh_disable();
1013	if (cpumask_empty(&iucv_buffer_cpumask)) {
1014		rc = -EIO;
1015		goto out;
1016	}
1017	parm = iucv_param[smp_processor_id()];
1018	memset(parm, 0, sizeof(union iucv_param));
1019	if (userdata)
1020		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
1021	parm->ctrl.ippathid = path->pathid;
1022	rc = iucv_call_b2f0(IUCV_RESUME, parm);
1023out:
1024	local_bh_enable();
1025	return rc;
1026}
1027
1028/**
1029 * iucv_path_sever
1030 * @path: address of iucv path structure
1031 * @userdata: 16 bytes of data reflected to the communication partner
1032 *
1033 * This function terminates an IUCV path.
1034 *
1035 * Returns the result from the CP IUCV call.
1036 */
1037int iucv_path_sever(struct iucv_path *path, u8 *userdata)
1038{
1039	int rc;
1040
1041	preempt_disable();
1042	if (cpumask_empty(&iucv_buffer_cpumask)) {
1043		rc = -EIO;
1044		goto out;
1045	}
1046	if (iucv_active_cpu != smp_processor_id())
1047		spin_lock_bh(&iucv_table_lock);
1048	rc = iucv_sever_pathid(path->pathid, userdata);
1049	iucv_path_table[path->pathid] = NULL;
1050	list_del_init(&path->list);
1051	if (iucv_active_cpu != smp_processor_id())
1052		spin_unlock_bh(&iucv_table_lock);
1053out:
1054	preempt_enable();
1055	return rc;
1056}
1057EXPORT_SYMBOL(iucv_path_sever);
1058
1059/**
1060 * iucv_message_purge
1061 * @path: address of iucv path structure
1062 * @msg: address of iucv msg structure
1063 * @srccls: source class of message
1064 *
1065 * Cancels a message you have sent.
1066 *
1067 * Returns the result from the CP IUCV call.
1068 */
1069int iucv_message_purge(struct iucv_path *path, struct iucv_message *msg,
1070		       u32 srccls)
1071{
1072	union iucv_param *parm;
1073	int rc;
1074
1075	local_bh_disable();
1076	if (cpumask_empty(&iucv_buffer_cpumask)) {
1077		rc = -EIO;
1078		goto out;
1079	}
1080	parm = iucv_param[smp_processor_id()];
1081	memset(parm, 0, sizeof(union iucv_param));
1082	parm->purge.ippathid = path->pathid;
1083	parm->purge.ipmsgid = msg->id;
1084	parm->purge.ipsrccls = srccls;
1085	parm->purge.ipflags1 = IUCV_IPSRCCLS | IUCV_IPFGMID | IUCV_IPFGPID;
1086	rc = iucv_call_b2f0(IUCV_PURGE, parm);
1087	if (!rc) {
1088		msg->audit = (*(u32 *) &parm->purge.ipaudit) >> 8;
1089		msg->tag = parm->purge.ipmsgtag;
1090	}
1091out:
1092	local_bh_enable();
1093	return rc;
1094}
1095EXPORT_SYMBOL(iucv_message_purge);
1096
1097/**
1098 * iucv_message_receive_iprmdata
1099 * @path: address of iucv path structure
1100 * @msg: address of iucv msg structure
1101 * @flags: how the message is received (IUCV_IPBUFLST)
1102 * @buffer: address of data buffer or address of struct iucv_array
1103 * @size: length of data buffer
1104 * @residual:
1105 *
1106 * Internal function used by iucv_message_receive and __iucv_message_receive
1107 * to receive RMDATA data stored in struct iucv_message.
1108 */
1109static int iucv_message_receive_iprmdata(struct iucv_path *path,
1110					 struct iucv_message *msg,
1111					 u8 flags, void *buffer,
1112					 size_t size, size_t *residual)
1113{
1114	struct iucv_array *array;
1115	u8 *rmmsg;
1116	size_t copy;
1117
1118	/*
1119	 * Message is 8 bytes long and has been stored to the
1120	 * message descriptor itself.
1121	 */
1122	if (residual)
1123		*residual = abs(size - 8);
1124	rmmsg = msg->rmmsg;
1125	if (flags & IUCV_IPBUFLST) {
1126		/* Copy to struct iucv_array. */
1127		size = (size < 8) ? size : 8;
1128		for (array = buffer; size > 0; array++) {
1129			copy = min_t(size_t, size, array->length);
1130			memcpy(dma32_to_virt(array->address), rmmsg, copy);
 
1131			rmmsg += copy;
1132			size -= copy;
1133		}
1134	} else {
1135		/* Copy to direct buffer. */
1136		memcpy(buffer, rmmsg, min_t(size_t, size, 8));
1137	}
1138	return 0;
1139}
1140
1141/**
1142 * __iucv_message_receive
1143 * @path: address of iucv path structure
1144 * @msg: address of iucv msg structure
1145 * @flags: how the message is received (IUCV_IPBUFLST)
1146 * @buffer: address of data buffer or address of struct iucv_array
1147 * @size: length of data buffer
1148 * @residual:
1149 *
1150 * This function receives messages that are being sent to you over
1151 * established paths. This function will deal with RMDATA messages
1152 * embedded in struct iucv_message as well.
1153 *
1154 * Locking:	no locking
1155 *
1156 * Returns the result from the CP IUCV call.
1157 */
1158int __iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
1159			   u8 flags, void *buffer, size_t size, size_t *residual)
1160{
1161	union iucv_param *parm;
1162	int rc;
1163
1164	if (msg->flags & IUCV_IPRMDATA)
1165		return iucv_message_receive_iprmdata(path, msg, flags,
1166						     buffer, size, residual);
1167	if (cpumask_empty(&iucv_buffer_cpumask))
1168		return -EIO;
1169
 
1170	parm = iucv_param[smp_processor_id()];
1171	memset(parm, 0, sizeof(union iucv_param));
1172	parm->db.ipbfadr1 = virt_to_dma32(buffer);
1173	parm->db.ipbfln1f = (u32) size;
1174	parm->db.ipmsgid = msg->id;
1175	parm->db.ippathid = path->pathid;
1176	parm->db.iptrgcls = msg->class;
1177	parm->db.ipflags1 = (flags | IUCV_IPFGPID |
1178			     IUCV_IPFGMID | IUCV_IPTRGCLS);
1179	rc = iucv_call_b2f0(IUCV_RECEIVE, parm);
1180	if (!rc || rc == 5) {
1181		msg->flags = parm->db.ipflags1;
1182		if (residual)
1183			*residual = parm->db.ipbfln1f;
1184	}
 
1185	return rc;
1186}
1187EXPORT_SYMBOL(__iucv_message_receive);
1188
1189/**
1190 * iucv_message_receive
1191 * @path: address of iucv path structure
1192 * @msg: address of iucv msg structure
1193 * @flags: how the message is received (IUCV_IPBUFLST)
1194 * @buffer: address of data buffer or address of struct iucv_array
1195 * @size: length of data buffer
1196 * @residual:
1197 *
1198 * This function receives messages that are being sent to you over
1199 * established paths. This function will deal with RMDATA messages
1200 * embedded in struct iucv_message as well.
1201 *
1202 * Locking:	local_bh_enable/local_bh_disable
1203 *
1204 * Returns the result from the CP IUCV call.
1205 */
1206int iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
1207			 u8 flags, void *buffer, size_t size, size_t *residual)
1208{
1209	int rc;
1210
1211	if (msg->flags & IUCV_IPRMDATA)
1212		return iucv_message_receive_iprmdata(path, msg, flags,
1213						     buffer, size, residual);
1214	local_bh_disable();
1215	rc = __iucv_message_receive(path, msg, flags, buffer, size, residual);
1216	local_bh_enable();
1217	return rc;
1218}
1219EXPORT_SYMBOL(iucv_message_receive);
1220
1221/**
1222 * iucv_message_reject
1223 * @path: address of iucv path structure
1224 * @msg: address of iucv msg structure
1225 *
1226 * The reject function refuses a specified message. Between the time you
1227 * are notified of a message and the time that you complete the message,
1228 * the message may be rejected.
1229 *
1230 * Returns the result from the CP IUCV call.
1231 */
1232int iucv_message_reject(struct iucv_path *path, struct iucv_message *msg)
1233{
1234	union iucv_param *parm;
1235	int rc;
1236
1237	local_bh_disable();
1238	if (cpumask_empty(&iucv_buffer_cpumask)) {
1239		rc = -EIO;
1240		goto out;
1241	}
1242	parm = iucv_param[smp_processor_id()];
1243	memset(parm, 0, sizeof(union iucv_param));
1244	parm->db.ippathid = path->pathid;
1245	parm->db.ipmsgid = msg->id;
1246	parm->db.iptrgcls = msg->class;
1247	parm->db.ipflags1 = (IUCV_IPTRGCLS | IUCV_IPFGMID | IUCV_IPFGPID);
1248	rc = iucv_call_b2f0(IUCV_REJECT, parm);
1249out:
1250	local_bh_enable();
1251	return rc;
1252}
1253EXPORT_SYMBOL(iucv_message_reject);
1254
1255/**
1256 * iucv_message_reply
1257 * @path: address of iucv path structure
1258 * @msg: address of iucv msg structure
1259 * @flags: how the reply is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1260 * @reply: address of reply data buffer or address of struct iucv_array
1261 * @size: length of reply data buffer
1262 *
1263 * This function responds to the two-way messages that you receive. You
1264 * must identify completely the message to which you wish to reply. ie,
1265 * pathid, msgid, and trgcls. Prmmsg signifies the data is moved into
1266 * the parameter list.
1267 *
1268 * Returns the result from the CP IUCV call.
1269 */
1270int iucv_message_reply(struct iucv_path *path, struct iucv_message *msg,
1271		       u8 flags, void *reply, size_t size)
1272{
1273	union iucv_param *parm;
1274	int rc;
1275
1276	local_bh_disable();
1277	if (cpumask_empty(&iucv_buffer_cpumask)) {
1278		rc = -EIO;
1279		goto out;
1280	}
1281	parm = iucv_param[smp_processor_id()];
1282	memset(parm, 0, sizeof(union iucv_param));
1283	if (flags & IUCV_IPRMDATA) {
1284		parm->dpl.ippathid = path->pathid;
1285		parm->dpl.ipflags1 = flags;
1286		parm->dpl.ipmsgid = msg->id;
1287		parm->dpl.iptrgcls = msg->class;
1288		memcpy(parm->dpl.iprmmsg, reply, min_t(size_t, size, 8));
1289	} else {
1290		parm->db.ipbfadr1 = virt_to_dma32(reply);
1291		parm->db.ipbfln1f = (u32) size;
1292		parm->db.ippathid = path->pathid;
1293		parm->db.ipflags1 = flags;
1294		parm->db.ipmsgid = msg->id;
1295		parm->db.iptrgcls = msg->class;
1296	}
1297	rc = iucv_call_b2f0(IUCV_REPLY, parm);
1298out:
1299	local_bh_enable();
1300	return rc;
1301}
1302EXPORT_SYMBOL(iucv_message_reply);
1303
1304/**
1305 * __iucv_message_send
1306 * @path: address of iucv path structure
1307 * @msg: address of iucv msg structure
1308 * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1309 * @srccls: source class of message
1310 * @buffer: address of send buffer or address of struct iucv_array
1311 * @size: length of send buffer
1312 *
1313 * This function transmits data to another application. Data to be
1314 * transmitted is in a buffer and this is a one-way message and the
1315 * receiver will not reply to the message.
1316 *
1317 * Locking:	no locking
1318 *
1319 * Returns the result from the CP IUCV call.
1320 */
1321int __iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
1322		      u8 flags, u32 srccls, void *buffer, size_t size)
1323{
1324	union iucv_param *parm;
1325	int rc;
1326
1327	if (cpumask_empty(&iucv_buffer_cpumask)) {
1328		rc = -EIO;
1329		goto out;
1330	}
1331	parm = iucv_param[smp_processor_id()];
1332	memset(parm, 0, sizeof(union iucv_param));
1333	if (flags & IUCV_IPRMDATA) {
1334		/* Message of 8 bytes can be placed into the parameter list. */
1335		parm->dpl.ippathid = path->pathid;
1336		parm->dpl.ipflags1 = flags | IUCV_IPNORPY;
1337		parm->dpl.iptrgcls = msg->class;
1338		parm->dpl.ipsrccls = srccls;
1339		parm->dpl.ipmsgtag = msg->tag;
1340		memcpy(parm->dpl.iprmmsg, buffer, 8);
1341	} else {
1342		parm->db.ipbfadr1 = virt_to_dma32(buffer);
1343		parm->db.ipbfln1f = (u32) size;
1344		parm->db.ippathid = path->pathid;
1345		parm->db.ipflags1 = flags | IUCV_IPNORPY;
1346		parm->db.iptrgcls = msg->class;
1347		parm->db.ipsrccls = srccls;
1348		parm->db.ipmsgtag = msg->tag;
1349	}
1350	rc = iucv_call_b2f0(IUCV_SEND, parm);
1351	if (!rc)
1352		msg->id = parm->db.ipmsgid;
1353out:
1354	return rc;
1355}
1356EXPORT_SYMBOL(__iucv_message_send);
1357
1358/**
1359 * iucv_message_send
1360 * @path: address of iucv path structure
1361 * @msg: address of iucv msg structure
1362 * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1363 * @srccls: source class of message
1364 * @buffer: address of send buffer or address of struct iucv_array
1365 * @size: length of send buffer
1366 *
1367 * This function transmits data to another application. Data to be
1368 * transmitted is in a buffer and this is a one-way message and the
1369 * receiver will not reply to the message.
1370 *
1371 * Locking:	local_bh_enable/local_bh_disable
1372 *
1373 * Returns the result from the CP IUCV call.
1374 */
1375int iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
1376		      u8 flags, u32 srccls, void *buffer, size_t size)
1377{
1378	int rc;
1379
1380	local_bh_disable();
1381	rc = __iucv_message_send(path, msg, flags, srccls, buffer, size);
1382	local_bh_enable();
1383	return rc;
1384}
1385EXPORT_SYMBOL(iucv_message_send);
1386
1387/**
1388 * iucv_message_send2way
1389 * @path: address of iucv path structure
1390 * @msg: address of iucv msg structure
1391 * @flags: how the message is sent and the reply is received
1392 *	   (IUCV_IPRMDATA, IUCV_IPBUFLST, IUCV_IPPRTY, IUCV_ANSLST)
1393 * @srccls: source class of message
1394 * @buffer: address of send buffer or address of struct iucv_array
1395 * @size: length of send buffer
1396 * @answer: address of answer buffer or address of struct iucv_array
1397 * @asize: size of reply buffer
1398 * @residual: ignored
1399 *
1400 * This function transmits data to another application. Data to be
1401 * transmitted is in a buffer. The receiver of the send is expected to
1402 * reply to the message and a buffer is provided into which IUCV moves
1403 * the reply to this message.
1404 *
1405 * Returns the result from the CP IUCV call.
1406 */
1407int iucv_message_send2way(struct iucv_path *path, struct iucv_message *msg,
1408			  u8 flags, u32 srccls, void *buffer, size_t size,
1409			  void *answer, size_t asize, size_t *residual)
1410{
1411	union iucv_param *parm;
1412	int rc;
1413
1414	local_bh_disable();
1415	if (cpumask_empty(&iucv_buffer_cpumask)) {
1416		rc = -EIO;
1417		goto out;
1418	}
1419	parm = iucv_param[smp_processor_id()];
1420	memset(parm, 0, sizeof(union iucv_param));
1421	if (flags & IUCV_IPRMDATA) {
1422		parm->dpl.ippathid = path->pathid;
1423		parm->dpl.ipflags1 = path->flags;	/* priority message */
1424		parm->dpl.iptrgcls = msg->class;
1425		parm->dpl.ipsrccls = srccls;
1426		parm->dpl.ipmsgtag = msg->tag;
1427		parm->dpl.ipbfadr2 = virt_to_dma32(answer);
1428		parm->dpl.ipbfln2f = (u32) asize;
1429		memcpy(parm->dpl.iprmmsg, buffer, 8);
1430	} else {
1431		parm->db.ippathid = path->pathid;
1432		parm->db.ipflags1 = path->flags;	/* priority message */
1433		parm->db.iptrgcls = msg->class;
1434		parm->db.ipsrccls = srccls;
1435		parm->db.ipmsgtag = msg->tag;
1436		parm->db.ipbfadr1 = virt_to_dma32(buffer);
1437		parm->db.ipbfln1f = (u32) size;
1438		parm->db.ipbfadr2 = virt_to_dma32(answer);
1439		parm->db.ipbfln2f = (u32) asize;
1440	}
1441	rc = iucv_call_b2f0(IUCV_SEND, parm);
1442	if (!rc)
1443		msg->id = parm->db.ipmsgid;
1444out:
1445	local_bh_enable();
1446	return rc;
1447}
1448EXPORT_SYMBOL(iucv_message_send2way);
1449
 
 
 
 
 
 
 
1450struct iucv_path_pending {
1451	u16 ippathid;
1452	u8  ipflags1;
1453	u8  iptype;
1454	u16 ipmsglim;
1455	u16 res1;
1456	u8  ipvmid[8];
1457	u8  ipuser[16];
1458	u32 res3;
1459	u8  ippollfg;
1460	u8  res4[3];
1461} __packed;
1462
1463/**
1464 * iucv_path_pending
1465 * @data: Pointer to external interrupt buffer
1466 *
1467 * Process connection pending work item. Called from tasklet while holding
1468 * iucv_table_lock.
1469 */
1470static void iucv_path_pending(struct iucv_irq_data *data)
1471{
1472	struct iucv_path_pending *ipp = (void *) data;
1473	struct iucv_handler *handler;
1474	struct iucv_path *path;
1475	char *error;
1476
1477	BUG_ON(iucv_path_table[ipp->ippathid]);
1478	/* New pathid, handler found. Create a new path struct. */
1479	error = iucv_error_no_memory;
1480	path = iucv_path_alloc(ipp->ipmsglim, ipp->ipflags1, GFP_ATOMIC);
1481	if (!path)
1482		goto out_sever;
1483	path->pathid = ipp->ippathid;
1484	iucv_path_table[path->pathid] = path;
1485	EBCASC(ipp->ipvmid, 8);
1486
1487	/* Call registered handler until one is found that wants the path. */
1488	list_for_each_entry(handler, &iucv_handler_list, list) {
1489		if (!handler->path_pending)
1490			continue;
1491		/*
1492		 * Add path to handler to allow a call to iucv_path_sever
1493		 * inside the path_pending function. If the handler returns
1494		 * an error remove the path from the handler again.
1495		 */
1496		list_add(&path->list, &handler->paths);
1497		path->handler = handler;
1498		if (!handler->path_pending(path, ipp->ipvmid, ipp->ipuser))
1499			return;
1500		list_del(&path->list);
1501		path->handler = NULL;
1502	}
1503	/* No handler wanted the path. */
1504	iucv_path_table[path->pathid] = NULL;
1505	iucv_path_free(path);
1506	error = iucv_error_no_listener;
1507out_sever:
1508	iucv_sever_pathid(ipp->ippathid, error);
1509}
1510
 
 
 
 
 
 
 
1511struct iucv_path_complete {
1512	u16 ippathid;
1513	u8  ipflags1;
1514	u8  iptype;
1515	u16 ipmsglim;
1516	u16 res1;
1517	u8  res2[8];
1518	u8  ipuser[16];
1519	u32 res3;
1520	u8  ippollfg;
1521	u8  res4[3];
1522} __packed;
1523
1524/**
1525 * iucv_path_complete
1526 * @data: Pointer to external interrupt buffer
1527 *
1528 * Process connection complete work item. Called from tasklet while holding
1529 * iucv_table_lock.
1530 */
1531static void iucv_path_complete(struct iucv_irq_data *data)
1532{
1533	struct iucv_path_complete *ipc = (void *) data;
1534	struct iucv_path *path = iucv_path_table[ipc->ippathid];
1535
1536	if (path)
1537		path->flags = ipc->ipflags1;
1538	if (path && path->handler && path->handler->path_complete)
1539		path->handler->path_complete(path, ipc->ipuser);
1540}
1541
 
 
 
 
 
 
 
1542struct iucv_path_severed {
1543	u16 ippathid;
1544	u8  res1;
1545	u8  iptype;
1546	u32 res2;
1547	u8  res3[8];
1548	u8  ipuser[16];
1549	u32 res4;
1550	u8  ippollfg;
1551	u8  res5[3];
1552} __packed;
1553
1554/**
1555 * iucv_path_severed
1556 * @data: Pointer to external interrupt buffer
1557 *
1558 * Process connection severed work item. Called from tasklet while holding
1559 * iucv_table_lock.
1560 */
1561static void iucv_path_severed(struct iucv_irq_data *data)
1562{
1563	struct iucv_path_severed *ips = (void *) data;
1564	struct iucv_path *path = iucv_path_table[ips->ippathid];
1565
1566	if (!path || !path->handler)	/* Already severed */
1567		return;
1568	if (path->handler->path_severed)
1569		path->handler->path_severed(path, ips->ipuser);
1570	else {
1571		iucv_sever_pathid(path->pathid, NULL);
1572		iucv_path_table[path->pathid] = NULL;
1573		list_del(&path->list);
1574		iucv_path_free(path);
1575	}
1576}
1577
 
 
 
 
 
 
 
1578struct iucv_path_quiesced {
1579	u16 ippathid;
1580	u8  res1;
1581	u8  iptype;
1582	u32 res2;
1583	u8  res3[8];
1584	u8  ipuser[16];
1585	u32 res4;
1586	u8  ippollfg;
1587	u8  res5[3];
1588} __packed;
1589
1590/**
1591 * iucv_path_quiesced
1592 * @data: Pointer to external interrupt buffer
1593 *
1594 * Process connection quiesced work item. Called from tasklet while holding
1595 * iucv_table_lock.
1596 */
1597static void iucv_path_quiesced(struct iucv_irq_data *data)
1598{
1599	struct iucv_path_quiesced *ipq = (void *) data;
1600	struct iucv_path *path = iucv_path_table[ipq->ippathid];
1601
1602	if (path && path->handler && path->handler->path_quiesced)
1603		path->handler->path_quiesced(path, ipq->ipuser);
1604}
1605
 
 
 
 
 
 
 
1606struct iucv_path_resumed {
1607	u16 ippathid;
1608	u8  res1;
1609	u8  iptype;
1610	u32 res2;
1611	u8  res3[8];
1612	u8  ipuser[16];
1613	u32 res4;
1614	u8  ippollfg;
1615	u8  res5[3];
1616} __packed;
1617
1618/**
1619 * iucv_path_resumed
1620 * @data: Pointer to external interrupt buffer
1621 *
1622 * Process connection resumed work item. Called from tasklet while holding
1623 * iucv_table_lock.
1624 */
1625static void iucv_path_resumed(struct iucv_irq_data *data)
1626{
1627	struct iucv_path_resumed *ipr = (void *) data;
1628	struct iucv_path *path = iucv_path_table[ipr->ippathid];
1629
1630	if (path && path->handler && path->handler->path_resumed)
1631		path->handler->path_resumed(path, ipr->ipuser);
1632}
1633
 
 
 
 
 
 
 
1634struct iucv_message_complete {
1635	u16 ippathid;
1636	u8  ipflags1;
1637	u8  iptype;
1638	u32 ipmsgid;
1639	u32 ipaudit;
1640	u8  iprmmsg[8];
1641	u32 ipsrccls;
1642	u32 ipmsgtag;
1643	u32 res;
1644	u32 ipbfln2f;
1645	u8  ippollfg;
1646	u8  res2[3];
1647} __packed;
1648
1649/**
1650 * iucv_message_complete
1651 * @data: Pointer to external interrupt buffer
1652 *
1653 * Process message complete work item. Called from tasklet while holding
1654 * iucv_table_lock.
1655 */
1656static void iucv_message_complete(struct iucv_irq_data *data)
1657{
1658	struct iucv_message_complete *imc = (void *) data;
1659	struct iucv_path *path = iucv_path_table[imc->ippathid];
1660	struct iucv_message msg;
1661
1662	if (path && path->handler && path->handler->message_complete) {
1663		msg.flags = imc->ipflags1;
1664		msg.id = imc->ipmsgid;
1665		msg.audit = imc->ipaudit;
1666		memcpy(msg.rmmsg, imc->iprmmsg, 8);
1667		msg.class = imc->ipsrccls;
1668		msg.tag = imc->ipmsgtag;
1669		msg.length = imc->ipbfln2f;
1670		path->handler->message_complete(path, &msg);
1671	}
1672}
1673
 
 
 
 
 
 
 
1674struct iucv_message_pending {
1675	u16 ippathid;
1676	u8  ipflags1;
1677	u8  iptype;
1678	u32 ipmsgid;
1679	u32 iptrgcls;
1680	struct {
1681		union {
1682			u32 iprmmsg1_u32;
1683			u8  iprmmsg1[4];
1684		} ln1msg1;
1685		union {
1686			u32 ipbfln1f;
1687			u8  iprmmsg2[4];
1688		} ln1msg2;
1689	} rmmsg;
1690	u32 res1[3];
1691	u32 ipbfln2f;
1692	u8  ippollfg;
1693	u8  res2[3];
1694} __packed;
1695
1696/**
1697 * iucv_message_pending
1698 * @data: Pointer to external interrupt buffer
1699 *
1700 * Process message pending work item. Called from tasklet while holding
1701 * iucv_table_lock.
1702 */
1703static void iucv_message_pending(struct iucv_irq_data *data)
1704{
1705	struct iucv_message_pending *imp = (void *) data;
1706	struct iucv_path *path = iucv_path_table[imp->ippathid];
1707	struct iucv_message msg;
1708
1709	if (path && path->handler && path->handler->message_pending) {
1710		msg.flags = imp->ipflags1;
1711		msg.id = imp->ipmsgid;
1712		msg.class = imp->iptrgcls;
1713		if (imp->ipflags1 & IUCV_IPRMDATA) {
1714			memcpy(msg.rmmsg, &imp->rmmsg, 8);
1715			msg.length = 8;
1716		} else
1717			msg.length = imp->rmmsg.ln1msg2.ipbfln1f;
1718		msg.reply_size = imp->ipbfln2f;
1719		path->handler->message_pending(path, &msg);
1720	}
1721}
1722
1723/*
1724 * iucv_tasklet_fn:
1725 *
1726 * This tasklet loops over the queue of irq buffers created by
1727 * iucv_external_interrupt, calls the appropriate action handler
1728 * and then frees the buffer.
1729 */
1730static void iucv_tasklet_fn(unsigned long ignored)
1731{
1732	typedef void iucv_irq_fn(struct iucv_irq_data *);
1733	static iucv_irq_fn *irq_fn[] = {
1734		[0x02] = iucv_path_complete,
1735		[0x03] = iucv_path_severed,
1736		[0x04] = iucv_path_quiesced,
1737		[0x05] = iucv_path_resumed,
1738		[0x06] = iucv_message_complete,
1739		[0x07] = iucv_message_complete,
1740		[0x08] = iucv_message_pending,
1741		[0x09] = iucv_message_pending,
1742	};
1743	LIST_HEAD(task_queue);
1744	struct iucv_irq_list *p, *n;
1745
1746	/* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
1747	if (!spin_trylock(&iucv_table_lock)) {
1748		tasklet_schedule(&iucv_tasklet);
1749		return;
1750	}
1751	iucv_active_cpu = smp_processor_id();
1752
1753	spin_lock_irq(&iucv_queue_lock);
1754	list_splice_init(&iucv_task_queue, &task_queue);
1755	spin_unlock_irq(&iucv_queue_lock);
1756
1757	list_for_each_entry_safe(p, n, &task_queue, list) {
1758		list_del_init(&p->list);
1759		irq_fn[p->data.iptype](&p->data);
1760		kfree(p);
1761	}
1762
1763	iucv_active_cpu = -1;
1764	spin_unlock(&iucv_table_lock);
1765}
1766
1767/*
1768 * iucv_work_fn:
1769 *
1770 * This work function loops over the queue of path pending irq blocks
1771 * created by iucv_external_interrupt, calls the appropriate action
1772 * handler and then frees the buffer.
1773 */
1774static void iucv_work_fn(struct work_struct *work)
1775{
1776	LIST_HEAD(work_queue);
1777	struct iucv_irq_list *p, *n;
1778
1779	/* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
1780	spin_lock_bh(&iucv_table_lock);
1781	iucv_active_cpu = smp_processor_id();
1782
1783	spin_lock_irq(&iucv_queue_lock);
1784	list_splice_init(&iucv_work_queue, &work_queue);
1785	spin_unlock_irq(&iucv_queue_lock);
1786
1787	iucv_cleanup_queue();
1788	list_for_each_entry_safe(p, n, &work_queue, list) {
1789		list_del_init(&p->list);
1790		iucv_path_pending(&p->data);
1791		kfree(p);
1792	}
1793
1794	iucv_active_cpu = -1;
1795	spin_unlock_bh(&iucv_table_lock);
1796}
1797
1798/*
1799 * iucv_external_interrupt
 
1800 *
1801 * Handles external interrupts coming in from CP.
1802 * Places the interrupt buffer on a queue and schedules iucv_tasklet_fn().
1803 */
1804static void iucv_external_interrupt(struct ext_code ext_code,
1805				    unsigned int param32, unsigned long param64)
1806{
1807	struct iucv_irq_data *p;
1808	struct iucv_irq_list *work;
1809
1810	inc_irq_stat(IRQEXT_IUC);
1811	p = iucv_irq_data[smp_processor_id()];
1812	if (p->ippathid >= iucv_max_pathid) {
1813		WARN_ON(p->ippathid >= iucv_max_pathid);
1814		iucv_sever_pathid(p->ippathid, iucv_error_no_listener);
1815		return;
1816	}
1817	BUG_ON(p->iptype  < 0x01 || p->iptype > 0x09);
1818	work = kmalloc(sizeof(struct iucv_irq_list), GFP_ATOMIC);
1819	if (!work) {
1820		pr_warn("iucv_external_interrupt: out of memory\n");
1821		return;
1822	}
1823	memcpy(&work->data, p, sizeof(work->data));
1824	spin_lock(&iucv_queue_lock);
1825	if (p->iptype == 0x01) {
1826		/* Path pending interrupt. */
1827		list_add_tail(&work->list, &iucv_work_queue);
1828		schedule_work(&iucv_work);
1829	} else {
1830		/* The other interrupts. */
1831		list_add_tail(&work->list, &iucv_task_queue);
1832		tasklet_schedule(&iucv_tasklet);
1833	}
1834	spin_unlock(&iucv_queue_lock);
1835}
1836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1837struct iucv_interface iucv_if = {
1838	.message_receive = iucv_message_receive,
1839	.__message_receive = __iucv_message_receive,
1840	.message_reply = iucv_message_reply,
1841	.message_reject = iucv_message_reject,
1842	.message_send = iucv_message_send,
1843	.__message_send = __iucv_message_send,
1844	.message_send2way = iucv_message_send2way,
1845	.message_purge = iucv_message_purge,
1846	.path_accept = iucv_path_accept,
1847	.path_connect = iucv_path_connect,
1848	.path_quiesce = iucv_path_quiesce,
1849	.path_resume = iucv_path_resume,
1850	.path_sever = iucv_path_sever,
1851	.iucv_register = iucv_register,
1852	.iucv_unregister = iucv_unregister,
1853	.bus = NULL,
1854	.root = NULL,
1855};
1856EXPORT_SYMBOL(iucv_if);
1857
1858static enum cpuhp_state iucv_online;
1859/**
1860 * iucv_init
1861 *
1862 * Allocates and initializes various data structures.
1863 */
1864static int __init iucv_init(void)
1865{
1866	int rc;
1867
1868	if (!MACHINE_IS_VM) {
1869		rc = -EPROTONOSUPPORT;
1870		goto out;
1871	}
1872	system_ctl_set_bit(0, CR0_IUCV_BIT);
1873	rc = iucv_query_maxconn();
1874	if (rc)
1875		goto out_ctl;
1876	rc = register_external_irq(EXT_IRQ_IUCV, iucv_external_interrupt);
1877	if (rc)
1878		goto out_ctl;
1879	iucv_root = root_device_register("iucv");
1880	if (IS_ERR(iucv_root)) {
1881		rc = PTR_ERR(iucv_root);
1882		goto out_int;
1883	}
1884
1885	rc = cpuhp_setup_state(CPUHP_NET_IUCV_PREPARE, "net/iucv:prepare",
1886			       iucv_cpu_prepare, iucv_cpu_dead);
1887	if (rc)
1888		goto out_dev;
1889	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "net/iucv:online",
1890			       iucv_cpu_online, iucv_cpu_down_prep);
1891	if (rc < 0)
1892		goto out_prep;
1893	iucv_online = rc;
1894
1895	rc = register_reboot_notifier(&iucv_reboot_notifier);
1896	if (rc)
1897		goto out_remove_hp;
1898	ASCEBC(iucv_error_no_listener, 16);
1899	ASCEBC(iucv_error_no_memory, 16);
1900	ASCEBC(iucv_error_pathid, 16);
1901	iucv_available = 1;
1902	rc = bus_register(&iucv_bus);
1903	if (rc)
1904		goto out_reboot;
1905	iucv_if.root = iucv_root;
1906	iucv_if.bus = &iucv_bus;
1907	return 0;
1908
1909out_reboot:
1910	unregister_reboot_notifier(&iucv_reboot_notifier);
1911out_remove_hp:
1912	cpuhp_remove_state(iucv_online);
1913out_prep:
1914	cpuhp_remove_state(CPUHP_NET_IUCV_PREPARE);
1915out_dev:
1916	root_device_unregister(iucv_root);
1917out_int:
1918	unregister_external_irq(EXT_IRQ_IUCV, iucv_external_interrupt);
1919out_ctl:
1920	system_ctl_clear_bit(0, 1);
1921out:
1922	return rc;
1923}
1924
1925/**
1926 * iucv_exit
1927 *
1928 * Frees everything allocated from iucv_init.
1929 */
1930static void __exit iucv_exit(void)
1931{
1932	struct iucv_irq_list *p, *n;
1933
1934	spin_lock_irq(&iucv_queue_lock);
1935	list_for_each_entry_safe(p, n, &iucv_task_queue, list)
1936		kfree(p);
1937	list_for_each_entry_safe(p, n, &iucv_work_queue, list)
1938		kfree(p);
1939	spin_unlock_irq(&iucv_queue_lock);
1940	unregister_reboot_notifier(&iucv_reboot_notifier);
1941
1942	cpuhp_remove_state_nocalls(iucv_online);
1943	cpuhp_remove_state(CPUHP_NET_IUCV_PREPARE);
1944	root_device_unregister(iucv_root);
1945	bus_unregister(&iucv_bus);
1946	unregister_external_irq(EXT_IRQ_IUCV, iucv_external_interrupt);
1947}
1948
1949subsys_initcall(iucv_init);
1950module_exit(iucv_exit);
1951
1952MODULE_AUTHOR("(C) 2001 IBM Corp. by Fritz Elfert <felfert@millenux.com>");
1953MODULE_DESCRIPTION("Linux for S/390 IUCV lowlevel driver");
1954MODULE_LICENSE("GPL");
v5.4
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * IUCV base infrastructure.
   4 *
   5 * Copyright IBM Corp. 2001, 2009
   6 *
   7 * Author(s):
   8 *    Original source:
   9 *	Alan Altmark (Alan_Altmark@us.ibm.com)	Sept. 2000
  10 *	Xenia Tkatschow (xenia@us.ibm.com)
  11 *    2Gb awareness and general cleanup:
  12 *	Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
  13 *    Rewritten for af_iucv:
  14 *	Martin Schwidefsky <schwidefsky@de.ibm.com>
  15 *    PM functions:
  16 *	Ursula Braun (ursula.braun@de.ibm.com)
  17 *
  18 * Documentation used:
  19 *    The original source
  20 *    CP Programming Service, IBM document # SC24-5760
  21 */
  22
  23#define KMSG_COMPONENT "iucv"
  24#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  25
  26#include <linux/kernel_stat.h>
  27#include <linux/module.h>
  28#include <linux/moduleparam.h>
  29#include <linux/spinlock.h>
  30#include <linux/kernel.h>
  31#include <linux/slab.h>
  32#include <linux/init.h>
  33#include <linux/interrupt.h>
  34#include <linux/list.h>
  35#include <linux/errno.h>
  36#include <linux/err.h>
  37#include <linux/device.h>
  38#include <linux/cpu.h>
  39#include <linux/reboot.h>
  40#include <net/iucv/iucv.h>
  41#include <linux/atomic.h>
  42#include <asm/ebcdic.h>
  43#include <asm/io.h>
  44#include <asm/irq.h>
  45#include <asm/smp.h>
  46
  47/*
  48 * FLAGS:
  49 * All flags are defined in the field IPFLAGS1 of each function
  50 * and can be found in CP Programming Services.
  51 * IPSRCCLS - Indicates you have specified a source class.
  52 * IPTRGCLS - Indicates you have specified a target class.
  53 * IPFGPID  - Indicates you have specified a pathid.
  54 * IPFGMID  - Indicates you have specified a message ID.
  55 * IPNORPY  - Indicates a one-way message. No reply expected.
  56 * IPALL    - Indicates that all paths are affected.
  57 */
  58#define IUCV_IPSRCCLS	0x01
  59#define IUCV_IPTRGCLS	0x01
  60#define IUCV_IPFGPID	0x02
  61#define IUCV_IPFGMID	0x04
  62#define IUCV_IPNORPY	0x10
  63#define IUCV_IPALL	0x80
  64
  65static int iucv_bus_match(struct device *dev, struct device_driver *drv)
  66{
  67	return 0;
  68}
  69
  70enum iucv_pm_states {
  71	IUCV_PM_INITIAL = 0,
  72	IUCV_PM_FREEZING = 1,
  73	IUCV_PM_THAWING = 2,
  74	IUCV_PM_RESTORING = 3,
  75};
  76static enum iucv_pm_states iucv_pm_state;
  77
  78static int iucv_pm_prepare(struct device *);
  79static void iucv_pm_complete(struct device *);
  80static int iucv_pm_freeze(struct device *);
  81static int iucv_pm_thaw(struct device *);
  82static int iucv_pm_restore(struct device *);
  83
  84static const struct dev_pm_ops iucv_pm_ops = {
  85	.prepare = iucv_pm_prepare,
  86	.complete = iucv_pm_complete,
  87	.freeze = iucv_pm_freeze,
  88	.thaw = iucv_pm_thaw,
  89	.restore = iucv_pm_restore,
  90};
  91
  92struct bus_type iucv_bus = {
  93	.name = "iucv",
  94	.match = iucv_bus_match,
  95	.pm = &iucv_pm_ops,
  96};
  97EXPORT_SYMBOL(iucv_bus);
  98
  99struct device *iucv_root;
 100EXPORT_SYMBOL(iucv_root);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 101
 102static int iucv_available;
 103
 104/* General IUCV interrupt structure */
 105struct iucv_irq_data {
 106	u16 ippathid;
 107	u8  ipflags1;
 108	u8  iptype;
 109	u32 res2[8];
 110};
 111
 112struct iucv_irq_list {
 113	struct list_head list;
 114	struct iucv_irq_data data;
 115};
 116
 117static struct iucv_irq_data *iucv_irq_data[NR_CPUS];
 118static cpumask_t iucv_buffer_cpumask = { CPU_BITS_NONE };
 119static cpumask_t iucv_irq_cpumask = { CPU_BITS_NONE };
 120
 121/*
 122 * Queue of interrupt buffers lock for delivery via the tasklet
 123 * (fast but can't call smp_call_function).
 124 */
 125static LIST_HEAD(iucv_task_queue);
 126
 127/*
 128 * The tasklet for fast delivery of iucv interrupts.
 129 */
 130static void iucv_tasklet_fn(unsigned long);
 131static DECLARE_TASKLET(iucv_tasklet, iucv_tasklet_fn,0);
 132
 133/*
 134 * Queue of interrupt buffers for delivery via a work queue
 135 * (slower but can call smp_call_function).
 136 */
 137static LIST_HEAD(iucv_work_queue);
 138
 139/*
 140 * The work element to deliver path pending interrupts.
 141 */
 142static void iucv_work_fn(struct work_struct *work);
 143static DECLARE_WORK(iucv_work, iucv_work_fn);
 144
 145/*
 146 * Spinlock protecting task and work queue.
 147 */
 148static DEFINE_SPINLOCK(iucv_queue_lock);
 149
 150enum iucv_command_codes {
 151	IUCV_QUERY = 0,
 152	IUCV_RETRIEVE_BUFFER = 2,
 153	IUCV_SEND = 4,
 154	IUCV_RECEIVE = 5,
 155	IUCV_REPLY = 6,
 156	IUCV_REJECT = 8,
 157	IUCV_PURGE = 9,
 158	IUCV_ACCEPT = 10,
 159	IUCV_CONNECT = 11,
 160	IUCV_DECLARE_BUFFER = 12,
 161	IUCV_QUIESCE = 13,
 162	IUCV_RESUME = 14,
 163	IUCV_SEVER = 15,
 164	IUCV_SETMASK = 16,
 165	IUCV_SETCONTROLMASK = 17,
 166};
 167
 168/*
 169 * Error messages that are used with the iucv_sever function. They get
 170 * converted to EBCDIC.
 171 */
 172static char iucv_error_no_listener[16] = "NO LISTENER";
 173static char iucv_error_no_memory[16] = "NO MEMORY";
 174static char iucv_error_pathid[16] = "INVALID PATHID";
 175
 176/*
 177 * iucv_handler_list: List of registered handlers.
 178 */
 179static LIST_HEAD(iucv_handler_list);
 180
 181/*
 182 * iucv_path_table: an array of iucv_path structures.
 183 */
 184static struct iucv_path **iucv_path_table;
 185static unsigned long iucv_max_pathid;
 186
 187/*
 188 * iucv_lock: spinlock protecting iucv_handler_list and iucv_pathid_table
 189 */
 190static DEFINE_SPINLOCK(iucv_table_lock);
 191
 192/*
 193 * iucv_active_cpu: contains the number of the cpu executing the tasklet
 194 * or the work handler. Needed for iucv_path_sever called from tasklet.
 195 */
 196static int iucv_active_cpu = -1;
 197
 198/*
 199 * Mutex and wait queue for iucv_register/iucv_unregister.
 200 */
 201static DEFINE_MUTEX(iucv_register_mutex);
 202
 203/*
 204 * Counter for number of non-smp capable handlers.
 205 */
 206static int iucv_nonsmp_handler;
 207
 208/*
 209 * IUCV control data structure. Used by iucv_path_accept, iucv_path_connect,
 210 * iucv_path_quiesce and iucv_path_sever.
 211 */
 212struct iucv_cmd_control {
 213	u16 ippathid;
 214	u8  ipflags1;
 215	u8  iprcode;
 216	u16 ipmsglim;
 217	u16 res1;
 218	u8  ipvmid[8];
 219	u8  ipuser[16];
 220	u8  iptarget[8];
 221} __attribute__ ((packed,aligned(8)));
 222
 223/*
 224 * Data in parameter list iucv structure. Used by iucv_message_send,
 225 * iucv_message_send2way and iucv_message_reply.
 226 */
 227struct iucv_cmd_dpl {
 228	u16 ippathid;
 229	u8  ipflags1;
 230	u8  iprcode;
 231	u32 ipmsgid;
 232	u32 iptrgcls;
 233	u8  iprmmsg[8];
 234	u32 ipsrccls;
 235	u32 ipmsgtag;
 236	u32 ipbfadr2;
 237	u32 ipbfln2f;
 238	u32 res;
 239} __attribute__ ((packed,aligned(8)));
 240
 241/*
 242 * Data in buffer iucv structure. Used by iucv_message_receive,
 243 * iucv_message_reject, iucv_message_send, iucv_message_send2way
 244 * and iucv_declare_cpu.
 245 */
 246struct iucv_cmd_db {
 247	u16 ippathid;
 248	u8  ipflags1;
 249	u8  iprcode;
 250	u32 ipmsgid;
 251	u32 iptrgcls;
 252	u32 ipbfadr1;
 253	u32 ipbfln1f;
 254	u32 ipsrccls;
 255	u32 ipmsgtag;
 256	u32 ipbfadr2;
 257	u32 ipbfln2f;
 258	u32 res;
 259} __attribute__ ((packed,aligned(8)));
 260
 261/*
 262 * Purge message iucv structure. Used by iucv_message_purge.
 263 */
 264struct iucv_cmd_purge {
 265	u16 ippathid;
 266	u8  ipflags1;
 267	u8  iprcode;
 268	u32 ipmsgid;
 269	u8  ipaudit[3];
 270	u8  res1[5];
 271	u32 res2;
 272	u32 ipsrccls;
 273	u32 ipmsgtag;
 274	u32 res3[3];
 275} __attribute__ ((packed,aligned(8)));
 276
 277/*
 278 * Set mask iucv structure. Used by iucv_enable_cpu.
 279 */
 280struct iucv_cmd_set_mask {
 281	u8  ipmask;
 282	u8  res1[2];
 283	u8  iprcode;
 284	u32 res2[9];
 285} __attribute__ ((packed,aligned(8)));
 286
 287union iucv_param {
 288	struct iucv_cmd_control ctrl;
 289	struct iucv_cmd_dpl dpl;
 290	struct iucv_cmd_db db;
 291	struct iucv_cmd_purge purge;
 292	struct iucv_cmd_set_mask set_mask;
 293};
 294
 295/*
 296 * Anchor for per-cpu IUCV command parameter block.
 297 */
 298static union iucv_param *iucv_param[NR_CPUS];
 299static union iucv_param *iucv_param_irq[NR_CPUS];
 300
 301/**
 302 * iucv_call_b2f0
 303 * @code: identifier of IUCV call to CP.
 304 * @parm: pointer to a struct iucv_parm block
 305 *
 306 * Calls CP to execute IUCV commands.
 307 *
 308 * Returns the result of the CP IUCV call.
 309 */
 310static inline int __iucv_call_b2f0(int command, union iucv_param *parm)
 311{
 312	register unsigned long reg0 asm ("0");
 313	register unsigned long reg1 asm ("1");
 314	int ccode;
 315
 316	reg0 = command;
 317	reg1 = (unsigned long)parm;
 318	asm volatile(
 319		"	.long 0xb2f01000\n"
 320		"	ipm	%0\n"
 321		"	srl	%0,28\n"
 322		: "=d" (ccode), "=m" (*parm), "+d" (reg0), "+a" (reg1)
 323		:  "m" (*parm) : "cc");
 324	return ccode;
 
 
 
 
 325}
 326
 327static inline int iucv_call_b2f0(int command, union iucv_param *parm)
 328{
 329	int ccode;
 330
 331	ccode = __iucv_call_b2f0(command, parm);
 332	return ccode == 1 ? parm->ctrl.iprcode : ccode;
 333}
 334
 335/**
 336 * iucv_query_maxconn
 337 *
 338 * Determines the maximum number of connections that may be established.
 339 *
 340 * Returns the maximum number of connections or -EPERM is IUCV is not
 341 * available.
 342 */
 343static int __iucv_query_maxconn(void *param, unsigned long *max_pathid)
 344{
 345	register unsigned long reg0 asm ("0");
 346	register unsigned long reg1 asm ("1");
 347	int ccode;
 348
 349	reg0 = IUCV_QUERY;
 350	reg1 = (unsigned long) param;
 351	asm volatile (
 
 
 352		"	.long	0xb2f01000\n"
 353		"	ipm	%0\n"
 354		"	srl	%0,28\n"
 355		: "=d" (ccode), "+d" (reg0), "+d" (reg1) : : "cc");
 
 
 
 356	*max_pathid = reg1;
 357	return ccode;
 358}
 359
 360static int iucv_query_maxconn(void)
 361{
 362	unsigned long max_pathid;
 363	void *param;
 364	int ccode;
 365
 366	param = kzalloc(sizeof(union iucv_param), GFP_KERNEL | GFP_DMA);
 367	if (!param)
 368		return -ENOMEM;
 369	ccode = __iucv_query_maxconn(param, &max_pathid);
 370	if (ccode == 0)
 371		iucv_max_pathid = max_pathid;
 372	kfree(param);
 373	return ccode ? -EPERM : 0;
 374}
 375
 376/**
 377 * iucv_allow_cpu
 378 * @data: unused
 379 *
 380 * Allow iucv interrupts on this cpu.
 381 */
 382static void iucv_allow_cpu(void *data)
 383{
 384	int cpu = smp_processor_id();
 385	union iucv_param *parm;
 386
 387	/*
 388	 * Enable all iucv interrupts.
 389	 * ipmask contains bits for the different interrupts
 390	 *	0x80 - Flag to allow nonpriority message pending interrupts
 391	 *	0x40 - Flag to allow priority message pending interrupts
 392	 *	0x20 - Flag to allow nonpriority message completion interrupts
 393	 *	0x10 - Flag to allow priority message completion interrupts
 394	 *	0x08 - Flag to allow IUCV control interrupts
 395	 */
 396	parm = iucv_param_irq[cpu];
 397	memset(parm, 0, sizeof(union iucv_param));
 398	parm->set_mask.ipmask = 0xf8;
 399	iucv_call_b2f0(IUCV_SETMASK, parm);
 400
 401	/*
 402	 * Enable all iucv control interrupts.
 403	 * ipmask contains bits for the different interrupts
 404	 *	0x80 - Flag to allow pending connections interrupts
 405	 *	0x40 - Flag to allow connection complete interrupts
 406	 *	0x20 - Flag to allow connection severed interrupts
 407	 *	0x10 - Flag to allow connection quiesced interrupts
 408	 *	0x08 - Flag to allow connection resumed interrupts
 409	 */
 410	memset(parm, 0, sizeof(union iucv_param));
 411	parm->set_mask.ipmask = 0xf8;
 412	iucv_call_b2f0(IUCV_SETCONTROLMASK, parm);
 413	/* Set indication that iucv interrupts are allowed for this cpu. */
 414	cpumask_set_cpu(cpu, &iucv_irq_cpumask);
 415}
 416
 417/**
 418 * iucv_block_cpu
 419 * @data: unused
 420 *
 421 * Block iucv interrupts on this cpu.
 422 */
 423static void iucv_block_cpu(void *data)
 424{
 425	int cpu = smp_processor_id();
 426	union iucv_param *parm;
 427
 428	/* Disable all iucv interrupts. */
 429	parm = iucv_param_irq[cpu];
 430	memset(parm, 0, sizeof(union iucv_param));
 431	iucv_call_b2f0(IUCV_SETMASK, parm);
 432
 433	/* Clear indication that iucv interrupts are allowed for this cpu. */
 434	cpumask_clear_cpu(cpu, &iucv_irq_cpumask);
 435}
 436
 437/**
 438 * iucv_block_cpu_almost
 439 * @data: unused
 440 *
 441 * Allow connection-severed interrupts only on this cpu.
 442 */
 443static void iucv_block_cpu_almost(void *data)
 444{
 445	int cpu = smp_processor_id();
 446	union iucv_param *parm;
 447
 448	/* Allow iucv control interrupts only */
 449	parm = iucv_param_irq[cpu];
 450	memset(parm, 0, sizeof(union iucv_param));
 451	parm->set_mask.ipmask = 0x08;
 452	iucv_call_b2f0(IUCV_SETMASK, parm);
 453	/* Allow iucv-severed interrupt only */
 454	memset(parm, 0, sizeof(union iucv_param));
 455	parm->set_mask.ipmask = 0x20;
 456	iucv_call_b2f0(IUCV_SETCONTROLMASK, parm);
 457
 458	/* Clear indication that iucv interrupts are allowed for this cpu. */
 459	cpumask_clear_cpu(cpu, &iucv_irq_cpumask);
 460}
 461
 462/**
 463 * iucv_declare_cpu
 464 * @data: unused
 465 *
 466 * Declare a interrupt buffer on this cpu.
 467 */
 468static void iucv_declare_cpu(void *data)
 469{
 470	int cpu = smp_processor_id();
 471	union iucv_param *parm;
 472	int rc;
 473
 474	if (cpumask_test_cpu(cpu, &iucv_buffer_cpumask))
 475		return;
 476
 477	/* Declare interrupt buffer. */
 478	parm = iucv_param_irq[cpu];
 479	memset(parm, 0, sizeof(union iucv_param));
 480	parm->db.ipbfadr1 = virt_to_phys(iucv_irq_data[cpu]);
 481	rc = iucv_call_b2f0(IUCV_DECLARE_BUFFER, parm);
 482	if (rc) {
 483		char *err = "Unknown";
 484		switch (rc) {
 485		case 0x03:
 486			err = "Directory error";
 487			break;
 488		case 0x0a:
 489			err = "Invalid length";
 490			break;
 491		case 0x13:
 492			err = "Buffer already exists";
 493			break;
 494		case 0x3e:
 495			err = "Buffer overlap";
 496			break;
 497		case 0x5c:
 498			err = "Paging or storage error";
 499			break;
 500		}
 501		pr_warn("Defining an interrupt buffer on CPU %i failed with 0x%02x (%s)\n",
 502			cpu, rc, err);
 503		return;
 504	}
 505
 506	/* Set indication that an iucv buffer exists for this cpu. */
 507	cpumask_set_cpu(cpu, &iucv_buffer_cpumask);
 508
 509	if (iucv_nonsmp_handler == 0 || cpumask_empty(&iucv_irq_cpumask))
 510		/* Enable iucv interrupts on this cpu. */
 511		iucv_allow_cpu(NULL);
 512	else
 513		/* Disable iucv interrupts on this cpu. */
 514		iucv_block_cpu(NULL);
 515}
 516
 517/**
 518 * iucv_retrieve_cpu
 519 * @data: unused
 520 *
 521 * Retrieve interrupt buffer on this cpu.
 522 */
 523static void iucv_retrieve_cpu(void *data)
 524{
 525	int cpu = smp_processor_id();
 526	union iucv_param *parm;
 527
 528	if (!cpumask_test_cpu(cpu, &iucv_buffer_cpumask))
 529		return;
 530
 531	/* Block iucv interrupts. */
 532	iucv_block_cpu(NULL);
 533
 534	/* Retrieve interrupt buffer. */
 535	parm = iucv_param_irq[cpu];
 536	iucv_call_b2f0(IUCV_RETRIEVE_BUFFER, parm);
 537
 538	/* Clear indication that an iucv buffer exists for this cpu. */
 539	cpumask_clear_cpu(cpu, &iucv_buffer_cpumask);
 540}
 541
 542/**
 543 * iucv_setmask_smp
 544 *
 545 * Allow iucv interrupts on all cpus.
 546 */
 547static void iucv_setmask_mp(void)
 548{
 549	int cpu;
 550
 551	get_online_cpus();
 552	for_each_online_cpu(cpu)
 553		/* Enable all cpus with a declared buffer. */
 554		if (cpumask_test_cpu(cpu, &iucv_buffer_cpumask) &&
 555		    !cpumask_test_cpu(cpu, &iucv_irq_cpumask))
 556			smp_call_function_single(cpu, iucv_allow_cpu,
 557						 NULL, 1);
 558	put_online_cpus();
 559}
 560
 561/**
 562 * iucv_setmask_up
 563 *
 564 * Allow iucv interrupts on a single cpu.
 565 */
 566static void iucv_setmask_up(void)
 567{
 568	cpumask_t cpumask;
 569	int cpu;
 570
 571	/* Disable all cpu but the first in cpu_irq_cpumask. */
 572	cpumask_copy(&cpumask, &iucv_irq_cpumask);
 573	cpumask_clear_cpu(cpumask_first(&iucv_irq_cpumask), &cpumask);
 574	for_each_cpu(cpu, &cpumask)
 575		smp_call_function_single(cpu, iucv_block_cpu, NULL, 1);
 576}
 577
 578/**
 579 * iucv_enable
 580 *
 581 * This function makes iucv ready for use. It allocates the pathid
 582 * table, declares an iucv interrupt buffer and enables the iucv
 583 * interrupts. Called when the first user has registered an iucv
 584 * handler.
 585 */
 586static int iucv_enable(void)
 587{
 588	size_t alloc_size;
 589	int cpu, rc;
 590
 591	get_online_cpus();
 592	rc = -ENOMEM;
 593	alloc_size = iucv_max_pathid * sizeof(struct iucv_path);
 594	iucv_path_table = kzalloc(alloc_size, GFP_KERNEL);
 595	if (!iucv_path_table)
 596		goto out;
 597	/* Declare per cpu buffers. */
 598	rc = -EIO;
 599	for_each_online_cpu(cpu)
 600		smp_call_function_single(cpu, iucv_declare_cpu, NULL, 1);
 601	if (cpumask_empty(&iucv_buffer_cpumask))
 602		/* No cpu could declare an iucv buffer. */
 603		goto out;
 604	put_online_cpus();
 605	return 0;
 606out:
 607	kfree(iucv_path_table);
 608	iucv_path_table = NULL;
 609	put_online_cpus();
 610	return rc;
 611}
 612
 613/**
 614 * iucv_disable
 615 *
 616 * This function shuts down iucv. It disables iucv interrupts, retrieves
 617 * the iucv interrupt buffer and frees the pathid table. Called after the
 618 * last user unregister its iucv handler.
 619 */
 620static void iucv_disable(void)
 621{
 622	get_online_cpus();
 623	on_each_cpu(iucv_retrieve_cpu, NULL, 1);
 624	kfree(iucv_path_table);
 625	iucv_path_table = NULL;
 626	put_online_cpus();
 627}
 628
 629static int iucv_cpu_dead(unsigned int cpu)
 630{
 631	kfree(iucv_param_irq[cpu]);
 632	iucv_param_irq[cpu] = NULL;
 633	kfree(iucv_param[cpu]);
 634	iucv_param[cpu] = NULL;
 635	kfree(iucv_irq_data[cpu]);
 636	iucv_irq_data[cpu] = NULL;
 637	return 0;
 638}
 639
 640static int iucv_cpu_prepare(unsigned int cpu)
 641{
 642	/* Note: GFP_DMA used to get memory below 2G */
 643	iucv_irq_data[cpu] = kmalloc_node(sizeof(struct iucv_irq_data),
 644			     GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
 645	if (!iucv_irq_data[cpu])
 646		goto out_free;
 647
 648	/* Allocate parameter blocks. */
 649	iucv_param[cpu] = kmalloc_node(sizeof(union iucv_param),
 650			  GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
 651	if (!iucv_param[cpu])
 652		goto out_free;
 653
 654	iucv_param_irq[cpu] = kmalloc_node(sizeof(union iucv_param),
 655			  GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
 656	if (!iucv_param_irq[cpu])
 657		goto out_free;
 658
 659	return 0;
 660
 661out_free:
 662	iucv_cpu_dead(cpu);
 663	return -ENOMEM;
 664}
 665
 666static int iucv_cpu_online(unsigned int cpu)
 667{
 668	if (!iucv_path_table)
 669		return 0;
 670	iucv_declare_cpu(NULL);
 671	return 0;
 672}
 673
 674static int iucv_cpu_down_prep(unsigned int cpu)
 675{
 676	cpumask_t cpumask;
 
 677
 678	if (!iucv_path_table)
 679		return 0;
 680
 681	cpumask_copy(&cpumask, &iucv_buffer_cpumask);
 682	cpumask_clear_cpu(cpu, &cpumask);
 683	if (cpumask_empty(&cpumask))
 
 
 
 684		/* Can't offline last IUCV enabled cpu. */
 685		return -EINVAL;
 
 
 686
 687	iucv_retrieve_cpu(NULL);
 688	if (!cpumask_empty(&iucv_irq_cpumask))
 689		return 0;
 
 690	smp_call_function_single(cpumask_first(&iucv_buffer_cpumask),
 691				 iucv_allow_cpu, NULL, 1);
 692	return 0;
 
 
 
 693}
 694
 695/**
 696 * iucv_sever_pathid
 697 * @pathid: path identification number.
 698 * @userdata: 16-bytes of user data.
 699 *
 700 * Sever an iucv path to free up the pathid. Used internally.
 701 */
 702static int iucv_sever_pathid(u16 pathid, u8 *userdata)
 703{
 704	union iucv_param *parm;
 705
 706	parm = iucv_param_irq[smp_processor_id()];
 707	memset(parm, 0, sizeof(union iucv_param));
 708	if (userdata)
 709		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
 710	parm->ctrl.ippathid = pathid;
 711	return iucv_call_b2f0(IUCV_SEVER, parm);
 712}
 713
 714/**
 715 * __iucv_cleanup_queue
 716 * @dummy: unused dummy argument
 717 *
 718 * Nop function called via smp_call_function to force work items from
 719 * pending external iucv interrupts to the work queue.
 720 */
 721static void __iucv_cleanup_queue(void *dummy)
 722{
 723}
 724
 725/**
 726 * iucv_cleanup_queue
 727 *
 728 * Function called after a path has been severed to find all remaining
 729 * work items for the now stale pathid. The caller needs to hold the
 730 * iucv_table_lock.
 731 */
 732static void iucv_cleanup_queue(void)
 733{
 734	struct iucv_irq_list *p, *n;
 735
 736	/*
 737	 * When a path is severed, the pathid can be reused immediately
 738	 * on a iucv connect or a connection pending interrupt. Remove
 739	 * all entries from the task queue that refer to a stale pathid
 740	 * (iucv_path_table[ix] == NULL). Only then do the iucv connect
 741	 * or deliver the connection pending interrupt. To get all the
 742	 * pending interrupts force them to the work queue by calling
 743	 * an empty function on all cpus.
 744	 */
 745	smp_call_function(__iucv_cleanup_queue, NULL, 1);
 746	spin_lock_irq(&iucv_queue_lock);
 747	list_for_each_entry_safe(p, n, &iucv_task_queue, list) {
 748		/* Remove stale work items from the task queue. */
 749		if (iucv_path_table[p->data.ippathid] == NULL) {
 750			list_del(&p->list);
 751			kfree(p);
 752		}
 753	}
 754	spin_unlock_irq(&iucv_queue_lock);
 755}
 756
 757/**
 758 * iucv_register:
 759 * @handler: address of iucv handler structure
 760 * @smp: != 0 indicates that the handler can deal with out of order messages
 761 *
 762 * Registers a driver with IUCV.
 763 *
 764 * Returns 0 on success, -ENOMEM if the memory allocation for the pathid
 765 * table failed, or -EIO if IUCV_DECLARE_BUFFER failed on all cpus.
 766 */
 767int iucv_register(struct iucv_handler *handler, int smp)
 768{
 769	int rc;
 770
 771	if (!iucv_available)
 772		return -ENOSYS;
 773	mutex_lock(&iucv_register_mutex);
 774	if (!smp)
 775		iucv_nonsmp_handler++;
 776	if (list_empty(&iucv_handler_list)) {
 777		rc = iucv_enable();
 778		if (rc)
 779			goto out_mutex;
 780	} else if (!smp && iucv_nonsmp_handler == 1)
 781		iucv_setmask_up();
 782	INIT_LIST_HEAD(&handler->paths);
 783
 784	spin_lock_bh(&iucv_table_lock);
 785	list_add_tail(&handler->list, &iucv_handler_list);
 786	spin_unlock_bh(&iucv_table_lock);
 787	rc = 0;
 788out_mutex:
 789	mutex_unlock(&iucv_register_mutex);
 790	return rc;
 791}
 792EXPORT_SYMBOL(iucv_register);
 793
 794/**
 795 * iucv_unregister
 796 * @handler:  address of iucv handler structure
 797 * @smp: != 0 indicates that the handler can deal with out of order messages
 798 *
 799 * Unregister driver from IUCV.
 800 */
 801void iucv_unregister(struct iucv_handler *handler, int smp)
 802{
 803	struct iucv_path *p, *n;
 804
 805	mutex_lock(&iucv_register_mutex);
 806	spin_lock_bh(&iucv_table_lock);
 807	/* Remove handler from the iucv_handler_list. */
 808	list_del_init(&handler->list);
 809	/* Sever all pathids still referring to the handler. */
 810	list_for_each_entry_safe(p, n, &handler->paths, list) {
 811		iucv_sever_pathid(p->pathid, NULL);
 812		iucv_path_table[p->pathid] = NULL;
 813		list_del(&p->list);
 814		iucv_path_free(p);
 815	}
 816	spin_unlock_bh(&iucv_table_lock);
 817	if (!smp)
 818		iucv_nonsmp_handler--;
 819	if (list_empty(&iucv_handler_list))
 820		iucv_disable();
 821	else if (!smp && iucv_nonsmp_handler == 0)
 822		iucv_setmask_mp();
 823	mutex_unlock(&iucv_register_mutex);
 824}
 825EXPORT_SYMBOL(iucv_unregister);
 826
 827static int iucv_reboot_event(struct notifier_block *this,
 828			     unsigned long event, void *ptr)
 829{
 830	int i;
 831
 832	if (cpumask_empty(&iucv_irq_cpumask))
 833		return NOTIFY_DONE;
 834
 835	get_online_cpus();
 836	on_each_cpu_mask(&iucv_irq_cpumask, iucv_block_cpu, NULL, 1);
 837	preempt_disable();
 838	for (i = 0; i < iucv_max_pathid; i++) {
 839		if (iucv_path_table[i])
 840			iucv_sever_pathid(i, NULL);
 841	}
 842	preempt_enable();
 843	put_online_cpus();
 844	iucv_disable();
 845	return NOTIFY_DONE;
 846}
 847
 848static struct notifier_block iucv_reboot_notifier = {
 849	.notifier_call = iucv_reboot_event,
 850};
 851
 852/**
 853 * iucv_path_accept
 854 * @path: address of iucv path structure
 855 * @handler: address of iucv handler structure
 856 * @userdata: 16 bytes of data reflected to the communication partner
 857 * @private: private data passed to interrupt handlers for this path
 858 *
 859 * This function is issued after the user received a connection pending
 860 * external interrupt and now wishes to complete the IUCV communication path.
 861 *
 862 * Returns the result of the CP IUCV call.
 863 */
 864int iucv_path_accept(struct iucv_path *path, struct iucv_handler *handler,
 865		     u8 *userdata, void *private)
 866{
 867	union iucv_param *parm;
 868	int rc;
 869
 870	local_bh_disable();
 871	if (cpumask_empty(&iucv_buffer_cpumask)) {
 872		rc = -EIO;
 873		goto out;
 874	}
 875	/* Prepare parameter block. */
 876	parm = iucv_param[smp_processor_id()];
 877	memset(parm, 0, sizeof(union iucv_param));
 878	parm->ctrl.ippathid = path->pathid;
 879	parm->ctrl.ipmsglim = path->msglim;
 880	if (userdata)
 881		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
 882	parm->ctrl.ipflags1 = path->flags;
 883
 884	rc = iucv_call_b2f0(IUCV_ACCEPT, parm);
 885	if (!rc) {
 886		path->private = private;
 887		path->msglim = parm->ctrl.ipmsglim;
 888		path->flags = parm->ctrl.ipflags1;
 889	}
 890out:
 891	local_bh_enable();
 892	return rc;
 893}
 894EXPORT_SYMBOL(iucv_path_accept);
 895
 896/**
 897 * iucv_path_connect
 898 * @path: address of iucv path structure
 899 * @handler: address of iucv handler structure
 900 * @userid: 8-byte user identification
 901 * @system: 8-byte target system identification
 902 * @userdata: 16 bytes of data reflected to the communication partner
 903 * @private: private data passed to interrupt handlers for this path
 904 *
 905 * This function establishes an IUCV path. Although the connect may complete
 906 * successfully, you are not able to use the path until you receive an IUCV
 907 * Connection Complete external interrupt.
 908 *
 909 * Returns the result of the CP IUCV call.
 910 */
 911int iucv_path_connect(struct iucv_path *path, struct iucv_handler *handler,
 912		      u8 *userid, u8 *system, u8 *userdata,
 913		      void *private)
 914{
 915	union iucv_param *parm;
 916	int rc;
 917
 918	spin_lock_bh(&iucv_table_lock);
 919	iucv_cleanup_queue();
 920	if (cpumask_empty(&iucv_buffer_cpumask)) {
 921		rc = -EIO;
 922		goto out;
 923	}
 924	parm = iucv_param[smp_processor_id()];
 925	memset(parm, 0, sizeof(union iucv_param));
 926	parm->ctrl.ipmsglim = path->msglim;
 927	parm->ctrl.ipflags1 = path->flags;
 928	if (userid) {
 929		memcpy(parm->ctrl.ipvmid, userid, sizeof(parm->ctrl.ipvmid));
 930		ASCEBC(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
 931		EBC_TOUPPER(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
 932	}
 933	if (system) {
 934		memcpy(parm->ctrl.iptarget, system,
 935		       sizeof(parm->ctrl.iptarget));
 936		ASCEBC(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
 937		EBC_TOUPPER(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
 938	}
 939	if (userdata)
 940		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
 941
 942	rc = iucv_call_b2f0(IUCV_CONNECT, parm);
 943	if (!rc) {
 944		if (parm->ctrl.ippathid < iucv_max_pathid) {
 945			path->pathid = parm->ctrl.ippathid;
 946			path->msglim = parm->ctrl.ipmsglim;
 947			path->flags = parm->ctrl.ipflags1;
 948			path->handler = handler;
 949			path->private = private;
 950			list_add_tail(&path->list, &handler->paths);
 951			iucv_path_table[path->pathid] = path;
 952		} else {
 953			iucv_sever_pathid(parm->ctrl.ippathid,
 954					  iucv_error_pathid);
 955			rc = -EIO;
 956		}
 957	}
 958out:
 959	spin_unlock_bh(&iucv_table_lock);
 960	return rc;
 961}
 962EXPORT_SYMBOL(iucv_path_connect);
 963
 964/**
 965 * iucv_path_quiesce:
 966 * @path: address of iucv path structure
 967 * @userdata: 16 bytes of data reflected to the communication partner
 968 *
 969 * This function temporarily suspends incoming messages on an IUCV path.
 970 * You can later reactivate the path by invoking the iucv_resume function.
 971 *
 972 * Returns the result from the CP IUCV call.
 973 */
 974int iucv_path_quiesce(struct iucv_path *path, u8 *userdata)
 975{
 976	union iucv_param *parm;
 977	int rc;
 978
 979	local_bh_disable();
 980	if (cpumask_empty(&iucv_buffer_cpumask)) {
 981		rc = -EIO;
 982		goto out;
 983	}
 984	parm = iucv_param[smp_processor_id()];
 985	memset(parm, 0, sizeof(union iucv_param));
 986	if (userdata)
 987		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
 988	parm->ctrl.ippathid = path->pathid;
 989	rc = iucv_call_b2f0(IUCV_QUIESCE, parm);
 990out:
 991	local_bh_enable();
 992	return rc;
 993}
 994EXPORT_SYMBOL(iucv_path_quiesce);
 995
 996/**
 997 * iucv_path_resume:
 998 * @path: address of iucv path structure
 999 * @userdata: 16 bytes of data reflected to the communication partner
1000 *
1001 * This function resumes incoming messages on an IUCV path that has
1002 * been stopped with iucv_path_quiesce.
1003 *
1004 * Returns the result from the CP IUCV call.
1005 */
1006int iucv_path_resume(struct iucv_path *path, u8 *userdata)
1007{
1008	union iucv_param *parm;
1009	int rc;
1010
1011	local_bh_disable();
1012	if (cpumask_empty(&iucv_buffer_cpumask)) {
1013		rc = -EIO;
1014		goto out;
1015	}
1016	parm = iucv_param[smp_processor_id()];
1017	memset(parm, 0, sizeof(union iucv_param));
1018	if (userdata)
1019		memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
1020	parm->ctrl.ippathid = path->pathid;
1021	rc = iucv_call_b2f0(IUCV_RESUME, parm);
1022out:
1023	local_bh_enable();
1024	return rc;
1025}
1026
1027/**
1028 * iucv_path_sever
1029 * @path: address of iucv path structure
1030 * @userdata: 16 bytes of data reflected to the communication partner
1031 *
1032 * This function terminates an IUCV path.
1033 *
1034 * Returns the result from the CP IUCV call.
1035 */
1036int iucv_path_sever(struct iucv_path *path, u8 *userdata)
1037{
1038	int rc;
1039
1040	preempt_disable();
1041	if (cpumask_empty(&iucv_buffer_cpumask)) {
1042		rc = -EIO;
1043		goto out;
1044	}
1045	if (iucv_active_cpu != smp_processor_id())
1046		spin_lock_bh(&iucv_table_lock);
1047	rc = iucv_sever_pathid(path->pathid, userdata);
1048	iucv_path_table[path->pathid] = NULL;
1049	list_del_init(&path->list);
1050	if (iucv_active_cpu != smp_processor_id())
1051		spin_unlock_bh(&iucv_table_lock);
1052out:
1053	preempt_enable();
1054	return rc;
1055}
1056EXPORT_SYMBOL(iucv_path_sever);
1057
1058/**
1059 * iucv_message_purge
1060 * @path: address of iucv path structure
1061 * @msg: address of iucv msg structure
1062 * @srccls: source class of message
1063 *
1064 * Cancels a message you have sent.
1065 *
1066 * Returns the result from the CP IUCV call.
1067 */
1068int iucv_message_purge(struct iucv_path *path, struct iucv_message *msg,
1069		       u32 srccls)
1070{
1071	union iucv_param *parm;
1072	int rc;
1073
1074	local_bh_disable();
1075	if (cpumask_empty(&iucv_buffer_cpumask)) {
1076		rc = -EIO;
1077		goto out;
1078	}
1079	parm = iucv_param[smp_processor_id()];
1080	memset(parm, 0, sizeof(union iucv_param));
1081	parm->purge.ippathid = path->pathid;
1082	parm->purge.ipmsgid = msg->id;
1083	parm->purge.ipsrccls = srccls;
1084	parm->purge.ipflags1 = IUCV_IPSRCCLS | IUCV_IPFGMID | IUCV_IPFGPID;
1085	rc = iucv_call_b2f0(IUCV_PURGE, parm);
1086	if (!rc) {
1087		msg->audit = (*(u32 *) &parm->purge.ipaudit) >> 8;
1088		msg->tag = parm->purge.ipmsgtag;
1089	}
1090out:
1091	local_bh_enable();
1092	return rc;
1093}
1094EXPORT_SYMBOL(iucv_message_purge);
1095
1096/**
1097 * iucv_message_receive_iprmdata
1098 * @path: address of iucv path structure
1099 * @msg: address of iucv msg structure
1100 * @flags: how the message is received (IUCV_IPBUFLST)
1101 * @buffer: address of data buffer or address of struct iucv_array
1102 * @size: length of data buffer
1103 * @residual:
1104 *
1105 * Internal function used by iucv_message_receive and __iucv_message_receive
1106 * to receive RMDATA data stored in struct iucv_message.
1107 */
1108static int iucv_message_receive_iprmdata(struct iucv_path *path,
1109					 struct iucv_message *msg,
1110					 u8 flags, void *buffer,
1111					 size_t size, size_t *residual)
1112{
1113	struct iucv_array *array;
1114	u8 *rmmsg;
1115	size_t copy;
1116
1117	/*
1118	 * Message is 8 bytes long and has been stored to the
1119	 * message descriptor itself.
1120	 */
1121	if (residual)
1122		*residual = abs(size - 8);
1123	rmmsg = msg->rmmsg;
1124	if (flags & IUCV_IPBUFLST) {
1125		/* Copy to struct iucv_array. */
1126		size = (size < 8) ? size : 8;
1127		for (array = buffer; size > 0; array++) {
1128			copy = min_t(size_t, size, array->length);
1129			memcpy((u8 *)(addr_t) array->address,
1130				rmmsg, copy);
1131			rmmsg += copy;
1132			size -= copy;
1133		}
1134	} else {
1135		/* Copy to direct buffer. */
1136		memcpy(buffer, rmmsg, min_t(size_t, size, 8));
1137	}
1138	return 0;
1139}
1140
1141/**
1142 * __iucv_message_receive
1143 * @path: address of iucv path structure
1144 * @msg: address of iucv msg structure
1145 * @flags: how the message is received (IUCV_IPBUFLST)
1146 * @buffer: address of data buffer or address of struct iucv_array
1147 * @size: length of data buffer
1148 * @residual:
1149 *
1150 * This function receives messages that are being sent to you over
1151 * established paths. This function will deal with RMDATA messages
1152 * embedded in struct iucv_message as well.
1153 *
1154 * Locking:	no locking
1155 *
1156 * Returns the result from the CP IUCV call.
1157 */
1158int __iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
1159			   u8 flags, void *buffer, size_t size, size_t *residual)
1160{
1161	union iucv_param *parm;
1162	int rc;
1163
1164	if (msg->flags & IUCV_IPRMDATA)
1165		return iucv_message_receive_iprmdata(path, msg, flags,
1166						     buffer, size, residual);
1167	 if (cpumask_empty(&iucv_buffer_cpumask)) {
1168		rc = -EIO;
1169		goto out;
1170	}
1171	parm = iucv_param[smp_processor_id()];
1172	memset(parm, 0, sizeof(union iucv_param));
1173	parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1174	parm->db.ipbfln1f = (u32) size;
1175	parm->db.ipmsgid = msg->id;
1176	parm->db.ippathid = path->pathid;
1177	parm->db.iptrgcls = msg->class;
1178	parm->db.ipflags1 = (flags | IUCV_IPFGPID |
1179			     IUCV_IPFGMID | IUCV_IPTRGCLS);
1180	rc = iucv_call_b2f0(IUCV_RECEIVE, parm);
1181	if (!rc || rc == 5) {
1182		msg->flags = parm->db.ipflags1;
1183		if (residual)
1184			*residual = parm->db.ipbfln1f;
1185	}
1186out:
1187	return rc;
1188}
1189EXPORT_SYMBOL(__iucv_message_receive);
1190
1191/**
1192 * iucv_message_receive
1193 * @path: address of iucv path structure
1194 * @msg: address of iucv msg structure
1195 * @flags: how the message is received (IUCV_IPBUFLST)
1196 * @buffer: address of data buffer or address of struct iucv_array
1197 * @size: length of data buffer
1198 * @residual:
1199 *
1200 * This function receives messages that are being sent to you over
1201 * established paths. This function will deal with RMDATA messages
1202 * embedded in struct iucv_message as well.
1203 *
1204 * Locking:	local_bh_enable/local_bh_disable
1205 *
1206 * Returns the result from the CP IUCV call.
1207 */
1208int iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
1209			 u8 flags, void *buffer, size_t size, size_t *residual)
1210{
1211	int rc;
1212
1213	if (msg->flags & IUCV_IPRMDATA)
1214		return iucv_message_receive_iprmdata(path, msg, flags,
1215						     buffer, size, residual);
1216	local_bh_disable();
1217	rc = __iucv_message_receive(path, msg, flags, buffer, size, residual);
1218	local_bh_enable();
1219	return rc;
1220}
1221EXPORT_SYMBOL(iucv_message_receive);
1222
1223/**
1224 * iucv_message_reject
1225 * @path: address of iucv path structure
1226 * @msg: address of iucv msg structure
1227 *
1228 * The reject function refuses a specified message. Between the time you
1229 * are notified of a message and the time that you complete the message,
1230 * the message may be rejected.
1231 *
1232 * Returns the result from the CP IUCV call.
1233 */
1234int iucv_message_reject(struct iucv_path *path, struct iucv_message *msg)
1235{
1236	union iucv_param *parm;
1237	int rc;
1238
1239	local_bh_disable();
1240	if (cpumask_empty(&iucv_buffer_cpumask)) {
1241		rc = -EIO;
1242		goto out;
1243	}
1244	parm = iucv_param[smp_processor_id()];
1245	memset(parm, 0, sizeof(union iucv_param));
1246	parm->db.ippathid = path->pathid;
1247	parm->db.ipmsgid = msg->id;
1248	parm->db.iptrgcls = msg->class;
1249	parm->db.ipflags1 = (IUCV_IPTRGCLS | IUCV_IPFGMID | IUCV_IPFGPID);
1250	rc = iucv_call_b2f0(IUCV_REJECT, parm);
1251out:
1252	local_bh_enable();
1253	return rc;
1254}
1255EXPORT_SYMBOL(iucv_message_reject);
1256
1257/**
1258 * iucv_message_reply
1259 * @path: address of iucv path structure
1260 * @msg: address of iucv msg structure
1261 * @flags: how the reply is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1262 * @reply: address of reply data buffer or address of struct iucv_array
1263 * @size: length of reply data buffer
1264 *
1265 * This function responds to the two-way messages that you receive. You
1266 * must identify completely the message to which you wish to reply. ie,
1267 * pathid, msgid, and trgcls. Prmmsg signifies the data is moved into
1268 * the parameter list.
1269 *
1270 * Returns the result from the CP IUCV call.
1271 */
1272int iucv_message_reply(struct iucv_path *path, struct iucv_message *msg,
1273		       u8 flags, void *reply, size_t size)
1274{
1275	union iucv_param *parm;
1276	int rc;
1277
1278	local_bh_disable();
1279	if (cpumask_empty(&iucv_buffer_cpumask)) {
1280		rc = -EIO;
1281		goto out;
1282	}
1283	parm = iucv_param[smp_processor_id()];
1284	memset(parm, 0, sizeof(union iucv_param));
1285	if (flags & IUCV_IPRMDATA) {
1286		parm->dpl.ippathid = path->pathid;
1287		parm->dpl.ipflags1 = flags;
1288		parm->dpl.ipmsgid = msg->id;
1289		parm->dpl.iptrgcls = msg->class;
1290		memcpy(parm->dpl.iprmmsg, reply, min_t(size_t, size, 8));
1291	} else {
1292		parm->db.ipbfadr1 = (u32)(addr_t) reply;
1293		parm->db.ipbfln1f = (u32) size;
1294		parm->db.ippathid = path->pathid;
1295		parm->db.ipflags1 = flags;
1296		parm->db.ipmsgid = msg->id;
1297		parm->db.iptrgcls = msg->class;
1298	}
1299	rc = iucv_call_b2f0(IUCV_REPLY, parm);
1300out:
1301	local_bh_enable();
1302	return rc;
1303}
1304EXPORT_SYMBOL(iucv_message_reply);
1305
1306/**
1307 * __iucv_message_send
1308 * @path: address of iucv path structure
1309 * @msg: address of iucv msg structure
1310 * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1311 * @srccls: source class of message
1312 * @buffer: address of send buffer or address of struct iucv_array
1313 * @size: length of send buffer
1314 *
1315 * This function transmits data to another application. Data to be
1316 * transmitted is in a buffer and this is a one-way message and the
1317 * receiver will not reply to the message.
1318 *
1319 * Locking:	no locking
1320 *
1321 * Returns the result from the CP IUCV call.
1322 */
1323int __iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
1324		      u8 flags, u32 srccls, void *buffer, size_t size)
1325{
1326	union iucv_param *parm;
1327	int rc;
1328
1329	if (cpumask_empty(&iucv_buffer_cpumask)) {
1330		rc = -EIO;
1331		goto out;
1332	}
1333	parm = iucv_param[smp_processor_id()];
1334	memset(parm, 0, sizeof(union iucv_param));
1335	if (flags & IUCV_IPRMDATA) {
1336		/* Message of 8 bytes can be placed into the parameter list. */
1337		parm->dpl.ippathid = path->pathid;
1338		parm->dpl.ipflags1 = flags | IUCV_IPNORPY;
1339		parm->dpl.iptrgcls = msg->class;
1340		parm->dpl.ipsrccls = srccls;
1341		parm->dpl.ipmsgtag = msg->tag;
1342		memcpy(parm->dpl.iprmmsg, buffer, 8);
1343	} else {
1344		parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1345		parm->db.ipbfln1f = (u32) size;
1346		parm->db.ippathid = path->pathid;
1347		parm->db.ipflags1 = flags | IUCV_IPNORPY;
1348		parm->db.iptrgcls = msg->class;
1349		parm->db.ipsrccls = srccls;
1350		parm->db.ipmsgtag = msg->tag;
1351	}
1352	rc = iucv_call_b2f0(IUCV_SEND, parm);
1353	if (!rc)
1354		msg->id = parm->db.ipmsgid;
1355out:
1356	return rc;
1357}
1358EXPORT_SYMBOL(__iucv_message_send);
1359
1360/**
1361 * iucv_message_send
1362 * @path: address of iucv path structure
1363 * @msg: address of iucv msg structure
1364 * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1365 * @srccls: source class of message
1366 * @buffer: address of send buffer or address of struct iucv_array
1367 * @size: length of send buffer
1368 *
1369 * This function transmits data to another application. Data to be
1370 * transmitted is in a buffer and this is a one-way message and the
1371 * receiver will not reply to the message.
1372 *
1373 * Locking:	local_bh_enable/local_bh_disable
1374 *
1375 * Returns the result from the CP IUCV call.
1376 */
1377int iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
1378		      u8 flags, u32 srccls, void *buffer, size_t size)
1379{
1380	int rc;
1381
1382	local_bh_disable();
1383	rc = __iucv_message_send(path, msg, flags, srccls, buffer, size);
1384	local_bh_enable();
1385	return rc;
1386}
1387EXPORT_SYMBOL(iucv_message_send);
1388
1389/**
1390 * iucv_message_send2way
1391 * @path: address of iucv path structure
1392 * @msg: address of iucv msg structure
1393 * @flags: how the message is sent and the reply is received
1394 *	   (IUCV_IPRMDATA, IUCV_IPBUFLST, IUCV_IPPRTY, IUCV_ANSLST)
1395 * @srccls: source class of message
1396 * @buffer: address of send buffer or address of struct iucv_array
1397 * @size: length of send buffer
1398 * @ansbuf: address of answer buffer or address of struct iucv_array
1399 * @asize: size of reply buffer
 
1400 *
1401 * This function transmits data to another application. Data to be
1402 * transmitted is in a buffer. The receiver of the send is expected to
1403 * reply to the message and a buffer is provided into which IUCV moves
1404 * the reply to this message.
1405 *
1406 * Returns the result from the CP IUCV call.
1407 */
1408int iucv_message_send2way(struct iucv_path *path, struct iucv_message *msg,
1409			  u8 flags, u32 srccls, void *buffer, size_t size,
1410			  void *answer, size_t asize, size_t *residual)
1411{
1412	union iucv_param *parm;
1413	int rc;
1414
1415	local_bh_disable();
1416	if (cpumask_empty(&iucv_buffer_cpumask)) {
1417		rc = -EIO;
1418		goto out;
1419	}
1420	parm = iucv_param[smp_processor_id()];
1421	memset(parm, 0, sizeof(union iucv_param));
1422	if (flags & IUCV_IPRMDATA) {
1423		parm->dpl.ippathid = path->pathid;
1424		parm->dpl.ipflags1 = path->flags;	/* priority message */
1425		parm->dpl.iptrgcls = msg->class;
1426		parm->dpl.ipsrccls = srccls;
1427		parm->dpl.ipmsgtag = msg->tag;
1428		parm->dpl.ipbfadr2 = (u32)(addr_t) answer;
1429		parm->dpl.ipbfln2f = (u32) asize;
1430		memcpy(parm->dpl.iprmmsg, buffer, 8);
1431	} else {
1432		parm->db.ippathid = path->pathid;
1433		parm->db.ipflags1 = path->flags;	/* priority message */
1434		parm->db.iptrgcls = msg->class;
1435		parm->db.ipsrccls = srccls;
1436		parm->db.ipmsgtag = msg->tag;
1437		parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1438		parm->db.ipbfln1f = (u32) size;
1439		parm->db.ipbfadr2 = (u32)(addr_t) answer;
1440		parm->db.ipbfln2f = (u32) asize;
1441	}
1442	rc = iucv_call_b2f0(IUCV_SEND, parm);
1443	if (!rc)
1444		msg->id = parm->db.ipmsgid;
1445out:
1446	local_bh_enable();
1447	return rc;
1448}
1449EXPORT_SYMBOL(iucv_message_send2way);
1450
1451/**
1452 * iucv_path_pending
1453 * @data: Pointer to external interrupt buffer
1454 *
1455 * Process connection pending work item. Called from tasklet while holding
1456 * iucv_table_lock.
1457 */
1458struct iucv_path_pending {
1459	u16 ippathid;
1460	u8  ipflags1;
1461	u8  iptype;
1462	u16 ipmsglim;
1463	u16 res1;
1464	u8  ipvmid[8];
1465	u8  ipuser[16];
1466	u32 res3;
1467	u8  ippollfg;
1468	u8  res4[3];
1469} __packed;
1470
 
 
 
 
 
 
 
1471static void iucv_path_pending(struct iucv_irq_data *data)
1472{
1473	struct iucv_path_pending *ipp = (void *) data;
1474	struct iucv_handler *handler;
1475	struct iucv_path *path;
1476	char *error;
1477
1478	BUG_ON(iucv_path_table[ipp->ippathid]);
1479	/* New pathid, handler found. Create a new path struct. */
1480	error = iucv_error_no_memory;
1481	path = iucv_path_alloc(ipp->ipmsglim, ipp->ipflags1, GFP_ATOMIC);
1482	if (!path)
1483		goto out_sever;
1484	path->pathid = ipp->ippathid;
1485	iucv_path_table[path->pathid] = path;
1486	EBCASC(ipp->ipvmid, 8);
1487
1488	/* Call registered handler until one is found that wants the path. */
1489	list_for_each_entry(handler, &iucv_handler_list, list) {
1490		if (!handler->path_pending)
1491			continue;
1492		/*
1493		 * Add path to handler to allow a call to iucv_path_sever
1494		 * inside the path_pending function. If the handler returns
1495		 * an error remove the path from the handler again.
1496		 */
1497		list_add(&path->list, &handler->paths);
1498		path->handler = handler;
1499		if (!handler->path_pending(path, ipp->ipvmid, ipp->ipuser))
1500			return;
1501		list_del(&path->list);
1502		path->handler = NULL;
1503	}
1504	/* No handler wanted the path. */
1505	iucv_path_table[path->pathid] = NULL;
1506	iucv_path_free(path);
1507	error = iucv_error_no_listener;
1508out_sever:
1509	iucv_sever_pathid(ipp->ippathid, error);
1510}
1511
1512/**
1513 * iucv_path_complete
1514 * @data: Pointer to external interrupt buffer
1515 *
1516 * Process connection complete work item. Called from tasklet while holding
1517 * iucv_table_lock.
1518 */
1519struct iucv_path_complete {
1520	u16 ippathid;
1521	u8  ipflags1;
1522	u8  iptype;
1523	u16 ipmsglim;
1524	u16 res1;
1525	u8  res2[8];
1526	u8  ipuser[16];
1527	u32 res3;
1528	u8  ippollfg;
1529	u8  res4[3];
1530} __packed;
1531
 
 
 
 
 
 
 
1532static void iucv_path_complete(struct iucv_irq_data *data)
1533{
1534	struct iucv_path_complete *ipc = (void *) data;
1535	struct iucv_path *path = iucv_path_table[ipc->ippathid];
1536
1537	if (path)
1538		path->flags = ipc->ipflags1;
1539	if (path && path->handler && path->handler->path_complete)
1540		path->handler->path_complete(path, ipc->ipuser);
1541}
1542
1543/**
1544 * iucv_path_severed
1545 * @data: Pointer to external interrupt buffer
1546 *
1547 * Process connection severed work item. Called from tasklet while holding
1548 * iucv_table_lock.
1549 */
1550struct iucv_path_severed {
1551	u16 ippathid;
1552	u8  res1;
1553	u8  iptype;
1554	u32 res2;
1555	u8  res3[8];
1556	u8  ipuser[16];
1557	u32 res4;
1558	u8  ippollfg;
1559	u8  res5[3];
1560} __packed;
1561
 
 
 
 
 
 
 
1562static void iucv_path_severed(struct iucv_irq_data *data)
1563{
1564	struct iucv_path_severed *ips = (void *) data;
1565	struct iucv_path *path = iucv_path_table[ips->ippathid];
1566
1567	if (!path || !path->handler)	/* Already severed */
1568		return;
1569	if (path->handler->path_severed)
1570		path->handler->path_severed(path, ips->ipuser);
1571	else {
1572		iucv_sever_pathid(path->pathid, NULL);
1573		iucv_path_table[path->pathid] = NULL;
1574		list_del(&path->list);
1575		iucv_path_free(path);
1576	}
1577}
1578
1579/**
1580 * iucv_path_quiesced
1581 * @data: Pointer to external interrupt buffer
1582 *
1583 * Process connection quiesced work item. Called from tasklet while holding
1584 * iucv_table_lock.
1585 */
1586struct iucv_path_quiesced {
1587	u16 ippathid;
1588	u8  res1;
1589	u8  iptype;
1590	u32 res2;
1591	u8  res3[8];
1592	u8  ipuser[16];
1593	u32 res4;
1594	u8  ippollfg;
1595	u8  res5[3];
1596} __packed;
1597
 
 
 
 
 
 
 
1598static void iucv_path_quiesced(struct iucv_irq_data *data)
1599{
1600	struct iucv_path_quiesced *ipq = (void *) data;
1601	struct iucv_path *path = iucv_path_table[ipq->ippathid];
1602
1603	if (path && path->handler && path->handler->path_quiesced)
1604		path->handler->path_quiesced(path, ipq->ipuser);
1605}
1606
1607/**
1608 * iucv_path_resumed
1609 * @data: Pointer to external interrupt buffer
1610 *
1611 * Process connection resumed work item. Called from tasklet while holding
1612 * iucv_table_lock.
1613 */
1614struct iucv_path_resumed {
1615	u16 ippathid;
1616	u8  res1;
1617	u8  iptype;
1618	u32 res2;
1619	u8  res3[8];
1620	u8  ipuser[16];
1621	u32 res4;
1622	u8  ippollfg;
1623	u8  res5[3];
1624} __packed;
1625
 
 
 
 
 
 
 
1626static void iucv_path_resumed(struct iucv_irq_data *data)
1627{
1628	struct iucv_path_resumed *ipr = (void *) data;
1629	struct iucv_path *path = iucv_path_table[ipr->ippathid];
1630
1631	if (path && path->handler && path->handler->path_resumed)
1632		path->handler->path_resumed(path, ipr->ipuser);
1633}
1634
1635/**
1636 * iucv_message_complete
1637 * @data: Pointer to external interrupt buffer
1638 *
1639 * Process message complete work item. Called from tasklet while holding
1640 * iucv_table_lock.
1641 */
1642struct iucv_message_complete {
1643	u16 ippathid;
1644	u8  ipflags1;
1645	u8  iptype;
1646	u32 ipmsgid;
1647	u32 ipaudit;
1648	u8  iprmmsg[8];
1649	u32 ipsrccls;
1650	u32 ipmsgtag;
1651	u32 res;
1652	u32 ipbfln2f;
1653	u8  ippollfg;
1654	u8  res2[3];
1655} __packed;
1656
 
 
 
 
 
 
 
1657static void iucv_message_complete(struct iucv_irq_data *data)
1658{
1659	struct iucv_message_complete *imc = (void *) data;
1660	struct iucv_path *path = iucv_path_table[imc->ippathid];
1661	struct iucv_message msg;
1662
1663	if (path && path->handler && path->handler->message_complete) {
1664		msg.flags = imc->ipflags1;
1665		msg.id = imc->ipmsgid;
1666		msg.audit = imc->ipaudit;
1667		memcpy(msg.rmmsg, imc->iprmmsg, 8);
1668		msg.class = imc->ipsrccls;
1669		msg.tag = imc->ipmsgtag;
1670		msg.length = imc->ipbfln2f;
1671		path->handler->message_complete(path, &msg);
1672	}
1673}
1674
1675/**
1676 * iucv_message_pending
1677 * @data: Pointer to external interrupt buffer
1678 *
1679 * Process message pending work item. Called from tasklet while holding
1680 * iucv_table_lock.
1681 */
1682struct iucv_message_pending {
1683	u16 ippathid;
1684	u8  ipflags1;
1685	u8  iptype;
1686	u32 ipmsgid;
1687	u32 iptrgcls;
1688	union {
1689		u32 iprmmsg1_u32;
1690		u8  iprmmsg1[4];
1691	} ln1msg1;
1692	union {
1693		u32 ipbfln1f;
1694		u8  iprmmsg2[4];
1695	} ln1msg2;
 
 
1696	u32 res1[3];
1697	u32 ipbfln2f;
1698	u8  ippollfg;
1699	u8  res2[3];
1700} __packed;
1701
 
 
 
 
 
 
 
1702static void iucv_message_pending(struct iucv_irq_data *data)
1703{
1704	struct iucv_message_pending *imp = (void *) data;
1705	struct iucv_path *path = iucv_path_table[imp->ippathid];
1706	struct iucv_message msg;
1707
1708	if (path && path->handler && path->handler->message_pending) {
1709		msg.flags = imp->ipflags1;
1710		msg.id = imp->ipmsgid;
1711		msg.class = imp->iptrgcls;
1712		if (imp->ipflags1 & IUCV_IPRMDATA) {
1713			memcpy(msg.rmmsg, imp->ln1msg1.iprmmsg1, 8);
1714			msg.length = 8;
1715		} else
1716			msg.length = imp->ln1msg2.ipbfln1f;
1717		msg.reply_size = imp->ipbfln2f;
1718		path->handler->message_pending(path, &msg);
1719	}
1720}
1721
1722/**
1723 * iucv_tasklet_fn:
1724 *
1725 * This tasklet loops over the queue of irq buffers created by
1726 * iucv_external_interrupt, calls the appropriate action handler
1727 * and then frees the buffer.
1728 */
1729static void iucv_tasklet_fn(unsigned long ignored)
1730{
1731	typedef void iucv_irq_fn(struct iucv_irq_data *);
1732	static iucv_irq_fn *irq_fn[] = {
1733		[0x02] = iucv_path_complete,
1734		[0x03] = iucv_path_severed,
1735		[0x04] = iucv_path_quiesced,
1736		[0x05] = iucv_path_resumed,
1737		[0x06] = iucv_message_complete,
1738		[0x07] = iucv_message_complete,
1739		[0x08] = iucv_message_pending,
1740		[0x09] = iucv_message_pending,
1741	};
1742	LIST_HEAD(task_queue);
1743	struct iucv_irq_list *p, *n;
1744
1745	/* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
1746	if (!spin_trylock(&iucv_table_lock)) {
1747		tasklet_schedule(&iucv_tasklet);
1748		return;
1749	}
1750	iucv_active_cpu = smp_processor_id();
1751
1752	spin_lock_irq(&iucv_queue_lock);
1753	list_splice_init(&iucv_task_queue, &task_queue);
1754	spin_unlock_irq(&iucv_queue_lock);
1755
1756	list_for_each_entry_safe(p, n, &task_queue, list) {
1757		list_del_init(&p->list);
1758		irq_fn[p->data.iptype](&p->data);
1759		kfree(p);
1760	}
1761
1762	iucv_active_cpu = -1;
1763	spin_unlock(&iucv_table_lock);
1764}
1765
1766/**
1767 * iucv_work_fn:
1768 *
1769 * This work function loops over the queue of path pending irq blocks
1770 * created by iucv_external_interrupt, calls the appropriate action
1771 * handler and then frees the buffer.
1772 */
1773static void iucv_work_fn(struct work_struct *work)
1774{
1775	LIST_HEAD(work_queue);
1776	struct iucv_irq_list *p, *n;
1777
1778	/* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
1779	spin_lock_bh(&iucv_table_lock);
1780	iucv_active_cpu = smp_processor_id();
1781
1782	spin_lock_irq(&iucv_queue_lock);
1783	list_splice_init(&iucv_work_queue, &work_queue);
1784	spin_unlock_irq(&iucv_queue_lock);
1785
1786	iucv_cleanup_queue();
1787	list_for_each_entry_safe(p, n, &work_queue, list) {
1788		list_del_init(&p->list);
1789		iucv_path_pending(&p->data);
1790		kfree(p);
1791	}
1792
1793	iucv_active_cpu = -1;
1794	spin_unlock_bh(&iucv_table_lock);
1795}
1796
1797/**
1798 * iucv_external_interrupt
1799 * @code: irq code
1800 *
1801 * Handles external interrupts coming in from CP.
1802 * Places the interrupt buffer on a queue and schedules iucv_tasklet_fn().
1803 */
1804static void iucv_external_interrupt(struct ext_code ext_code,
1805				    unsigned int param32, unsigned long param64)
1806{
1807	struct iucv_irq_data *p;
1808	struct iucv_irq_list *work;
1809
1810	inc_irq_stat(IRQEXT_IUC);
1811	p = iucv_irq_data[smp_processor_id()];
1812	if (p->ippathid >= iucv_max_pathid) {
1813		WARN_ON(p->ippathid >= iucv_max_pathid);
1814		iucv_sever_pathid(p->ippathid, iucv_error_no_listener);
1815		return;
1816	}
1817	BUG_ON(p->iptype  < 0x01 || p->iptype > 0x09);
1818	work = kmalloc(sizeof(struct iucv_irq_list), GFP_ATOMIC);
1819	if (!work) {
1820		pr_warn("iucv_external_interrupt: out of memory\n");
1821		return;
1822	}
1823	memcpy(&work->data, p, sizeof(work->data));
1824	spin_lock(&iucv_queue_lock);
1825	if (p->iptype == 0x01) {
1826		/* Path pending interrupt. */
1827		list_add_tail(&work->list, &iucv_work_queue);
1828		schedule_work(&iucv_work);
1829	} else {
1830		/* The other interrupts. */
1831		list_add_tail(&work->list, &iucv_task_queue);
1832		tasklet_schedule(&iucv_tasklet);
1833	}
1834	spin_unlock(&iucv_queue_lock);
1835}
1836
1837static int iucv_pm_prepare(struct device *dev)
1838{
1839	int rc = 0;
1840
1841#ifdef CONFIG_PM_DEBUG
1842	printk(KERN_INFO "iucv_pm_prepare\n");
1843#endif
1844	if (dev->driver && dev->driver->pm && dev->driver->pm->prepare)
1845		rc = dev->driver->pm->prepare(dev);
1846	return rc;
1847}
1848
1849static void iucv_pm_complete(struct device *dev)
1850{
1851#ifdef CONFIG_PM_DEBUG
1852	printk(KERN_INFO "iucv_pm_complete\n");
1853#endif
1854	if (dev->driver && dev->driver->pm && dev->driver->pm->complete)
1855		dev->driver->pm->complete(dev);
1856}
1857
1858/**
1859 * iucv_path_table_empty() - determine if iucv path table is empty
1860 *
1861 * Returns 0 if there are still iucv pathes defined
1862 *	   1 if there are no iucv pathes defined
1863 */
1864static int iucv_path_table_empty(void)
1865{
1866	int i;
1867
1868	for (i = 0; i < iucv_max_pathid; i++) {
1869		if (iucv_path_table[i])
1870			return 0;
1871	}
1872	return 1;
1873}
1874
1875/**
1876 * iucv_pm_freeze() - Freeze PM callback
1877 * @dev:	iucv-based device
1878 *
1879 * disable iucv interrupts
1880 * invoke callback function of the iucv-based driver
1881 * shut down iucv, if no iucv-pathes are established anymore
1882 */
1883static int iucv_pm_freeze(struct device *dev)
1884{
1885	int cpu;
1886	struct iucv_irq_list *p, *n;
1887	int rc = 0;
1888
1889#ifdef CONFIG_PM_DEBUG
1890	printk(KERN_WARNING "iucv_pm_freeze\n");
1891#endif
1892	if (iucv_pm_state != IUCV_PM_FREEZING) {
1893		for_each_cpu(cpu, &iucv_irq_cpumask)
1894			smp_call_function_single(cpu, iucv_block_cpu_almost,
1895						 NULL, 1);
1896		cancel_work_sync(&iucv_work);
1897		list_for_each_entry_safe(p, n, &iucv_work_queue, list) {
1898			list_del_init(&p->list);
1899			iucv_sever_pathid(p->data.ippathid,
1900					  iucv_error_no_listener);
1901			kfree(p);
1902		}
1903	}
1904	iucv_pm_state = IUCV_PM_FREEZING;
1905	if (dev->driver && dev->driver->pm && dev->driver->pm->freeze)
1906		rc = dev->driver->pm->freeze(dev);
1907	if (iucv_path_table_empty())
1908		iucv_disable();
1909	return rc;
1910}
1911
1912/**
1913 * iucv_pm_thaw() - Thaw PM callback
1914 * @dev:	iucv-based device
1915 *
1916 * make iucv ready for use again: allocate path table, declare interrupt buffers
1917 *				  and enable iucv interrupts
1918 * invoke callback function of the iucv-based driver
1919 */
1920static int iucv_pm_thaw(struct device *dev)
1921{
1922	int rc = 0;
1923
1924#ifdef CONFIG_PM_DEBUG
1925	printk(KERN_WARNING "iucv_pm_thaw\n");
1926#endif
1927	iucv_pm_state = IUCV_PM_THAWING;
1928	if (!iucv_path_table) {
1929		rc = iucv_enable();
1930		if (rc)
1931			goto out;
1932	}
1933	if (cpumask_empty(&iucv_irq_cpumask)) {
1934		if (iucv_nonsmp_handler)
1935			/* enable interrupts on one cpu */
1936			iucv_allow_cpu(NULL);
1937		else
1938			/* enable interrupts on all cpus */
1939			iucv_setmask_mp();
1940	}
1941	if (dev->driver && dev->driver->pm && dev->driver->pm->thaw)
1942		rc = dev->driver->pm->thaw(dev);
1943out:
1944	return rc;
1945}
1946
1947/**
1948 * iucv_pm_restore() - Restore PM callback
1949 * @dev:	iucv-based device
1950 *
1951 * make iucv ready for use again: allocate path table, declare interrupt buffers
1952 *				  and enable iucv interrupts
1953 * invoke callback function of the iucv-based driver
1954 */
1955static int iucv_pm_restore(struct device *dev)
1956{
1957	int rc = 0;
1958
1959#ifdef CONFIG_PM_DEBUG
1960	printk(KERN_WARNING "iucv_pm_restore %p\n", iucv_path_table);
1961#endif
1962	if ((iucv_pm_state != IUCV_PM_RESTORING) && iucv_path_table)
1963		pr_warn("Suspending Linux did not completely close all IUCV connections\n");
1964	iucv_pm_state = IUCV_PM_RESTORING;
1965	if (cpumask_empty(&iucv_irq_cpumask)) {
1966		rc = iucv_query_maxconn();
1967		rc = iucv_enable();
1968		if (rc)
1969			goto out;
1970	}
1971	if (dev->driver && dev->driver->pm && dev->driver->pm->restore)
1972		rc = dev->driver->pm->restore(dev);
1973out:
1974	return rc;
1975}
1976
1977struct iucv_interface iucv_if = {
1978	.message_receive = iucv_message_receive,
1979	.__message_receive = __iucv_message_receive,
1980	.message_reply = iucv_message_reply,
1981	.message_reject = iucv_message_reject,
1982	.message_send = iucv_message_send,
1983	.__message_send = __iucv_message_send,
1984	.message_send2way = iucv_message_send2way,
1985	.message_purge = iucv_message_purge,
1986	.path_accept = iucv_path_accept,
1987	.path_connect = iucv_path_connect,
1988	.path_quiesce = iucv_path_quiesce,
1989	.path_resume = iucv_path_resume,
1990	.path_sever = iucv_path_sever,
1991	.iucv_register = iucv_register,
1992	.iucv_unregister = iucv_unregister,
1993	.bus = NULL,
1994	.root = NULL,
1995};
1996EXPORT_SYMBOL(iucv_if);
1997
1998static enum cpuhp_state iucv_online;
1999/**
2000 * iucv_init
2001 *
2002 * Allocates and initializes various data structures.
2003 */
2004static int __init iucv_init(void)
2005{
2006	int rc;
2007
2008	if (!MACHINE_IS_VM) {
2009		rc = -EPROTONOSUPPORT;
2010		goto out;
2011	}
2012	ctl_set_bit(0, 1);
2013	rc = iucv_query_maxconn();
2014	if (rc)
2015		goto out_ctl;
2016	rc = register_external_irq(EXT_IRQ_IUCV, iucv_external_interrupt);
2017	if (rc)
2018		goto out_ctl;
2019	iucv_root = root_device_register("iucv");
2020	if (IS_ERR(iucv_root)) {
2021		rc = PTR_ERR(iucv_root);
2022		goto out_int;
2023	}
2024
2025	rc = cpuhp_setup_state(CPUHP_NET_IUCV_PREPARE, "net/iucv:prepare",
2026			       iucv_cpu_prepare, iucv_cpu_dead);
2027	if (rc)
2028		goto out_dev;
2029	rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "net/iucv:online",
2030			       iucv_cpu_online, iucv_cpu_down_prep);
2031	if (rc < 0)
2032		goto out_prep;
2033	iucv_online = rc;
2034
2035	rc = register_reboot_notifier(&iucv_reboot_notifier);
2036	if (rc)
2037		goto out_remove_hp;
2038	ASCEBC(iucv_error_no_listener, 16);
2039	ASCEBC(iucv_error_no_memory, 16);
2040	ASCEBC(iucv_error_pathid, 16);
2041	iucv_available = 1;
2042	rc = bus_register(&iucv_bus);
2043	if (rc)
2044		goto out_reboot;
2045	iucv_if.root = iucv_root;
2046	iucv_if.bus = &iucv_bus;
2047	return 0;
2048
2049out_reboot:
2050	unregister_reboot_notifier(&iucv_reboot_notifier);
2051out_remove_hp:
2052	cpuhp_remove_state(iucv_online);
2053out_prep:
2054	cpuhp_remove_state(CPUHP_NET_IUCV_PREPARE);
2055out_dev:
2056	root_device_unregister(iucv_root);
2057out_int:
2058	unregister_external_irq(EXT_IRQ_IUCV, iucv_external_interrupt);
2059out_ctl:
2060	ctl_clear_bit(0, 1);
2061out:
2062	return rc;
2063}
2064
2065/**
2066 * iucv_exit
2067 *
2068 * Frees everything allocated from iucv_init.
2069 */
2070static void __exit iucv_exit(void)
2071{
2072	struct iucv_irq_list *p, *n;
2073
2074	spin_lock_irq(&iucv_queue_lock);
2075	list_for_each_entry_safe(p, n, &iucv_task_queue, list)
2076		kfree(p);
2077	list_for_each_entry_safe(p, n, &iucv_work_queue, list)
2078		kfree(p);
2079	spin_unlock_irq(&iucv_queue_lock);
2080	unregister_reboot_notifier(&iucv_reboot_notifier);
2081
2082	cpuhp_remove_state_nocalls(iucv_online);
2083	cpuhp_remove_state(CPUHP_NET_IUCV_PREPARE);
2084	root_device_unregister(iucv_root);
2085	bus_unregister(&iucv_bus);
2086	unregister_external_irq(EXT_IRQ_IUCV, iucv_external_interrupt);
2087}
2088
2089subsys_initcall(iucv_init);
2090module_exit(iucv_exit);
2091
2092MODULE_AUTHOR("(C) 2001 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
2093MODULE_DESCRIPTION("Linux for S/390 IUCV lowlevel driver");
2094MODULE_LICENSE("GPL");