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