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