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