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