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