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