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