[AF_IUCV/IUCV] : Add missing section annotations
[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;
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;
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 = percpu_ptr(iucv_param, smp_processor_id());
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 = percpu_ptr(iucv_param, smp_processor_id());
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 interupt 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 = percpu_ptr(iucv_param, cpu);
405         memset(parm, 0, sizeof(union iucv_param));
406         parm->db.ipbfadr1 = virt_to_phys(percpu_ptr(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 = percpu_ptr(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         preempt_disable();
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_on(iucv_allow_cpu, NULL, 0, 1, cpu);
483         preempt_enable();
484 }
485
486 /**
487  * iucv_setmask_up
488  *
489  * Allow iucv interrupts on a single cpu.
490  */
491 static void iucv_setmask_up(void)
492 {
493         cpumask_t cpumask;
494         int cpu;
495
496         /* Disable all cpu but the first in cpu_irq_cpumask. */
497         cpumask = iucv_irq_cpumask;
498         cpu_clear(first_cpu(iucv_irq_cpumask), cpumask);
499         for_each_cpu_mask(cpu, cpumask)
500                 smp_call_function_on(iucv_block_cpu, NULL, 0, 1, cpu);
501 }
502
503 /**
504  * iucv_enable
505  *
506  * This function makes iucv ready for use. It allocates the pathid
507  * table, declares an iucv interrupt buffer and enables the iucv
508  * interrupts. Called when the first user has registered an iucv
509  * handler.
510  */
511 static int iucv_enable(void)
512 {
513         size_t alloc_size;
514         int cpu, rc;
515
516         rc = -ENOMEM;
517         alloc_size = iucv_max_pathid * sizeof(struct iucv_path);
518         iucv_path_table = kzalloc(alloc_size, GFP_KERNEL);
519         if (!iucv_path_table)
520                 goto out;
521         /* Declare per cpu buffers. */
522         rc = -EIO;
523         preempt_disable();
524         for_each_online_cpu(cpu)
525                 smp_call_function_on(iucv_declare_cpu, NULL, 0, 1, cpu);
526         preempt_enable();
527         if (cpus_empty(iucv_buffer_cpumask))
528                 /* No cpu could declare an iucv buffer. */
529                 goto out_path;
530         return 0;
531
532 out_path:
533         kfree(iucv_path_table);
534 out:
535         return rc;
536 }
537
538 /**
539  * iucv_disable
540  *
541  * This function shuts down iucv. It disables iucv interrupts, retrieves
542  * the iucv interrupt buffer and frees the pathid table. Called after the
543  * last user unregister its iucv handler.
544  */
545 static void iucv_disable(void)
546 {
547         on_each_cpu(iucv_retrieve_cpu, NULL, 0, 1);
548         kfree(iucv_path_table);
549 }
550
551 static int __cpuinit iucv_cpu_notify(struct notifier_block *self,
552                                      unsigned long action, void *hcpu)
553 {
554         cpumask_t cpumask;
555         long cpu = (long) hcpu;
556
557         switch (action) {
558         case CPU_UP_PREPARE:
559                 if (!percpu_populate(iucv_irq_data,
560                                      sizeof(struct iucv_irq_data),
561                                      GFP_KERNEL|GFP_DMA, cpu))
562                         return NOTIFY_BAD;
563                 if (!percpu_populate(iucv_param, sizeof(union iucv_param),
564                                      GFP_KERNEL|GFP_DMA, cpu)) {
565                         percpu_depopulate(iucv_irq_data, cpu);
566                         return NOTIFY_BAD;
567                 }
568                 break;
569         case CPU_UP_CANCELED:
570         case CPU_DEAD:
571                 percpu_depopulate(iucv_param, cpu);
572                 percpu_depopulate(iucv_irq_data, cpu);
573                 break;
574         case CPU_ONLINE:
575         case CPU_DOWN_FAILED:
576                 smp_call_function_on(iucv_declare_cpu, NULL, 0, 1, cpu);
577                 break;
578         case CPU_DOWN_PREPARE:
579                 cpumask = iucv_buffer_cpumask;
580                 cpu_clear(cpu, cpumask);
581                 if (cpus_empty(cpumask))
582                         /* Can't offline last IUCV enabled cpu. */
583                         return NOTIFY_BAD;
584                 smp_call_function_on(iucv_retrieve_cpu, NULL, 0, 1, cpu);
585                 if (cpus_empty(iucv_irq_cpumask))
586                         smp_call_function_on(iucv_allow_cpu, NULL, 0, 1,
587                                              first_cpu(iucv_buffer_cpumask));
588                 break;
589         }
590         return NOTIFY_OK;
591 }
592
593 static struct notifier_block __cpuinitdata iucv_cpu_notifier = {
594         .notifier_call = iucv_cpu_notify,
595 };
596
597 /**
598  * iucv_sever_pathid
599  * @pathid: path identification number.
600  * @userdata: 16-bytes of user data.
601  *
602  * Sever an iucv path to free up the pathid. Used internally.
603  */
604 static int iucv_sever_pathid(u16 pathid, u8 userdata[16])
605 {
606         union iucv_param *parm;
607
608         parm = percpu_ptr(iucv_param, smp_processor_id());
609         memset(parm, 0, sizeof(union iucv_param));
610         if (userdata)
611                 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
612         parm->ctrl.ippathid = pathid;
613         return iucv_call_b2f0(IUCV_SEVER, parm);
614 }
615
616 #ifdef CONFIG_SMP
617 /**
618  * __iucv_cleanup_queue
619  * @dummy: unused dummy argument
620  *
621  * Nop function called via smp_call_function to force work items from
622  * pending external iucv interrupts to the work queue.
623  */
624 static void __iucv_cleanup_queue(void *dummy)
625 {
626 }
627 #endif
628
629 /**
630  * iucv_cleanup_queue
631  *
632  * Function called after a path has been severed to find all remaining
633  * work items for the now stale pathid. The caller needs to hold the
634  * iucv_table_lock.
635  */
636 static void iucv_cleanup_queue(void)
637 {
638         struct iucv_irq_list *p, *n;
639
640         /*
641          * When a path is severed, the pathid can be reused immediatly
642          * on a iucv connect or a connection pending interrupt. Remove
643          * all entries from the task queue that refer to a stale pathid
644          * (iucv_path_table[ix] == NULL). Only then do the iucv connect
645          * or deliver the connection pending interrupt. To get all the
646          * pending interrupts force them to the work queue by calling
647          * an empty function on all cpus.
648          */
649         smp_call_function(__iucv_cleanup_queue, NULL, 0, 1);
650         spin_lock_irq(&iucv_queue_lock);
651         list_for_each_entry_safe(p, n, &iucv_task_queue, list) {
652                 /* Remove stale work items from the task queue. */
653                 if (iucv_path_table[p->data.ippathid] == NULL) {
654                         list_del(&p->list);
655                         kfree(p);
656                 }
657         }
658         spin_unlock_irq(&iucv_queue_lock);
659 }
660
661 /**
662  * iucv_register:
663  * @handler: address of iucv handler structure
664  * @smp: != 0 indicates that the handler can deal with out of order messages
665  *
666  * Registers a driver with IUCV.
667  *
668  * Returns 0 on success, -ENOMEM if the memory allocation for the pathid
669  * table failed, or -EIO if IUCV_DECLARE_BUFFER failed on all cpus.
670  */
671 int iucv_register(struct iucv_handler *handler, int smp)
672 {
673         int rc;
674
675         if (!iucv_available)
676                 return -ENOSYS;
677         mutex_lock(&iucv_register_mutex);
678         if (!smp)
679                 iucv_nonsmp_handler++;
680         if (list_empty(&iucv_handler_list)) {
681                 rc = iucv_enable();
682                 if (rc)
683                         goto out_mutex;
684         } else if (!smp && iucv_nonsmp_handler == 1)
685                 iucv_setmask_up();
686         INIT_LIST_HEAD(&handler->paths);
687
688         spin_lock_irq(&iucv_table_lock);
689         list_add_tail(&handler->list, &iucv_handler_list);
690         spin_unlock_irq(&iucv_table_lock);
691         rc = 0;
692 out_mutex:
693         mutex_unlock(&iucv_register_mutex);
694         return rc;
695 }
696 EXPORT_SYMBOL(iucv_register);
697
698 /**
699  * iucv_unregister
700  * @handler:  address of iucv handler structure
701  * @smp: != 0 indicates that the handler can deal with out of order messages
702  *
703  * Unregister driver from IUCV.
704  */
705 void iucv_unregister(struct iucv_handler *handler, int smp)
706 {
707         struct iucv_path *p, *n;
708
709         mutex_lock(&iucv_register_mutex);
710         spin_lock_bh(&iucv_table_lock);
711         /* Remove handler from the iucv_handler_list. */
712         list_del_init(&handler->list);
713         /* Sever all pathids still refering to the handler. */
714         list_for_each_entry_safe(p, n, &handler->paths, list) {
715                 iucv_sever_pathid(p->pathid, NULL);
716                 iucv_path_table[p->pathid] = NULL;
717                 list_del(&p->list);
718                 iucv_path_free(p);
719         }
720         spin_unlock_bh(&iucv_table_lock);
721         if (!smp)
722                 iucv_nonsmp_handler--;
723         if (list_empty(&iucv_handler_list))
724                 iucv_disable();
725         else if (!smp && iucv_nonsmp_handler == 0)
726                 iucv_setmask_mp();
727         mutex_unlock(&iucv_register_mutex);
728 }
729 EXPORT_SYMBOL(iucv_unregister);
730
731 /**
732  * iucv_path_accept
733  * @path: address of iucv path structure
734  * @handler: address of iucv handler structure
735  * @userdata: 16 bytes of data reflected to the communication partner
736  * @private: private data passed to interrupt handlers for this path
737  *
738  * This function is issued after the user received a connection pending
739  * external interrupt and now wishes to complete the IUCV communication path.
740  *
741  * Returns the result of the CP IUCV call.
742  */
743 int iucv_path_accept(struct iucv_path *path, struct iucv_handler *handler,
744                      u8 userdata[16], void *private)
745 {
746         union iucv_param *parm;
747         int rc;
748
749         local_bh_disable();
750         /* Prepare parameter block. */
751         parm = percpu_ptr(iucv_param, smp_processor_id());
752         memset(parm, 0, sizeof(union iucv_param));
753         parm->ctrl.ippathid = path->pathid;
754         parm->ctrl.ipmsglim = path->msglim;
755         if (userdata)
756                 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
757         parm->ctrl.ipflags1 = path->flags;
758
759         rc = iucv_call_b2f0(IUCV_ACCEPT, parm);
760         if (!rc) {
761                 path->private = private;
762                 path->msglim = parm->ctrl.ipmsglim;
763                 path->flags = parm->ctrl.ipflags1;
764         }
765         local_bh_enable();
766         return rc;
767 }
768 EXPORT_SYMBOL(iucv_path_accept);
769
770 /**
771  * iucv_path_connect
772  * @path: address of iucv path structure
773  * @handler: address of iucv handler structure
774  * @userid: 8-byte user identification
775  * @system: 8-byte target system identification
776  * @userdata: 16 bytes of data reflected to the communication partner
777  * @private: private data passed to interrupt handlers for this path
778  *
779  * This function establishes an IUCV path. Although the connect may complete
780  * successfully, you are not able to use the path until you receive an IUCV
781  * Connection Complete external interrupt.
782  *
783  * Returns the result of the CP IUCV call.
784  */
785 int iucv_path_connect(struct iucv_path *path, struct iucv_handler *handler,
786                       u8 userid[8], u8 system[8], u8 userdata[16],
787                       void *private)
788 {
789         union iucv_param *parm;
790         int rc;
791
792         BUG_ON(in_atomic());
793         spin_lock_bh(&iucv_table_lock);
794         iucv_cleanup_queue();
795         parm = percpu_ptr(iucv_param, smp_processor_id());
796         memset(parm, 0, sizeof(union iucv_param));
797         parm->ctrl.ipmsglim = path->msglim;
798         parm->ctrl.ipflags1 = path->flags;
799         if (userid) {
800                 memcpy(parm->ctrl.ipvmid, userid, sizeof(parm->ctrl.ipvmid));
801                 ASCEBC(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
802                 EBC_TOUPPER(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
803         }
804         if (system) {
805                 memcpy(parm->ctrl.iptarget, system,
806                        sizeof(parm->ctrl.iptarget));
807                 ASCEBC(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
808                 EBC_TOUPPER(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
809         }
810         if (userdata)
811                 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
812
813         rc = iucv_call_b2f0(IUCV_CONNECT, parm);
814         if (!rc) {
815                 if (parm->ctrl.ippathid < iucv_max_pathid) {
816                         path->pathid = parm->ctrl.ippathid;
817                         path->msglim = parm->ctrl.ipmsglim;
818                         path->flags = parm->ctrl.ipflags1;
819                         path->handler = handler;
820                         path->private = private;
821                         list_add_tail(&path->list, &handler->paths);
822                         iucv_path_table[path->pathid] = path;
823                 } else {
824                         iucv_sever_pathid(parm->ctrl.ippathid,
825                                           iucv_error_pathid);
826                         rc = -EIO;
827                 }
828         }
829         spin_unlock_bh(&iucv_table_lock);
830         return rc;
831 }
832 EXPORT_SYMBOL(iucv_path_connect);
833
834 /**
835  * iucv_path_quiesce:
836  * @path: address of iucv path structure
837  * @userdata: 16 bytes of data reflected to the communication partner
838  *
839  * This function temporarily suspends incoming messages on an IUCV path.
840  * You can later reactivate the path by invoking the iucv_resume function.
841  *
842  * Returns the result from the CP IUCV call.
843  */
844 int iucv_path_quiesce(struct iucv_path *path, u8 userdata[16])
845 {
846         union iucv_param *parm;
847         int rc;
848
849         local_bh_disable();
850         parm = percpu_ptr(iucv_param, smp_processor_id());
851         memset(parm, 0, sizeof(union iucv_param));
852         if (userdata)
853                 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
854         parm->ctrl.ippathid = path->pathid;
855         rc = iucv_call_b2f0(IUCV_QUIESCE, parm);
856         local_bh_enable();
857         return rc;
858 }
859 EXPORT_SYMBOL(iucv_path_quiesce);
860
861 /**
862  * iucv_path_resume:
863  * @path: address of iucv path structure
864  * @userdata: 16 bytes of data reflected to the communication partner
865  *
866  * This function resumes incoming messages on an IUCV path that has
867  * been stopped with iucv_path_quiesce.
868  *
869  * Returns the result from the CP IUCV call.
870  */
871 int iucv_path_resume(struct iucv_path *path, u8 userdata[16])
872 {
873         union iucv_param *parm;
874         int rc;
875
876         local_bh_disable();
877         parm = percpu_ptr(iucv_param, smp_processor_id());
878         memset(parm, 0, sizeof(union iucv_param));
879         if (userdata)
880                 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
881         parm->ctrl.ippathid = path->pathid;
882         rc = iucv_call_b2f0(IUCV_RESUME, parm);
883         local_bh_enable();
884         return rc;
885 }
886
887 /**
888  * iucv_path_sever
889  * @path: address of iucv path structure
890  * @userdata: 16 bytes of data reflected to the communication partner
891  *
892  * This function terminates an IUCV path.
893  *
894  * Returns the result from the CP IUCV call.
895  */
896 int iucv_path_sever(struct iucv_path *path, u8 userdata[16])
897 {
898         int rc;
899
900         preempt_disable();
901         if (iucv_active_cpu != smp_processor_id())
902                 spin_lock_bh(&iucv_table_lock);
903         rc = iucv_sever_pathid(path->pathid, userdata);
904         if (!rc) {
905                 iucv_path_table[path->pathid] = NULL;
906                 list_del_init(&path->list);
907         }
908         if (iucv_active_cpu != smp_processor_id())
909                 spin_unlock_bh(&iucv_table_lock);
910         preempt_enable();
911         return rc;
912 }
913 EXPORT_SYMBOL(iucv_path_sever);
914
915 /**
916  * iucv_message_purge
917  * @path: address of iucv path structure
918  * @msg: address of iucv msg structure
919  * @srccls: source class of message
920  *
921  * Cancels a message you have sent.
922  *
923  * Returns the result from the CP IUCV call.
924  */
925 int iucv_message_purge(struct iucv_path *path, struct iucv_message *msg,
926                        u32 srccls)
927 {
928         union iucv_param *parm;
929         int rc;
930
931         local_bh_disable();
932         parm = percpu_ptr(iucv_param, smp_processor_id());
933         memset(parm, 0, sizeof(union iucv_param));
934         parm->purge.ippathid = path->pathid;
935         parm->purge.ipmsgid = msg->id;
936         parm->purge.ipsrccls = srccls;
937         parm->purge.ipflags1 = IUCV_IPSRCCLS | IUCV_IPFGMID | IUCV_IPFGPID;
938         rc = iucv_call_b2f0(IUCV_PURGE, parm);
939         if (!rc) {
940                 msg->audit = (*(u32 *) &parm->purge.ipaudit) >> 8;
941                 msg->tag = parm->purge.ipmsgtag;
942         }
943         local_bh_enable();
944         return rc;
945 }
946 EXPORT_SYMBOL(iucv_message_purge);
947
948 /**
949  * iucv_message_receive
950  * @path: address of iucv path structure
951  * @msg: address of iucv msg structure
952  * @flags: how the message is received (IUCV_IPBUFLST)
953  * @buffer: address of data buffer or address of struct iucv_array
954  * @size: length of data buffer
955  * @residual:
956  *
957  * This function receives messages that are being sent to you over
958  * established paths. This function will deal with RMDATA messages
959  * embedded in struct iucv_message as well.
960  *
961  * Returns the result from the CP IUCV call.
962  */
963 int iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
964                          u8 flags, void *buffer, size_t size, size_t *residual)
965 {
966         union iucv_param *parm;
967         struct iucv_array *array;
968         u8 *rmmsg;
969         size_t copy;
970         int rc;
971
972         if (msg->flags & IUCV_IPRMDATA) {
973                 /*
974                  * Message is 8 bytes long and has been stored to the
975                  * message descriptor itself.
976                  */
977                 rc = (size < 8) ? 5 : 0;
978                 if (residual)
979                         *residual = abs(size - 8);
980                 rmmsg = msg->rmmsg;
981                 if (flags & IUCV_IPBUFLST) {
982                         /* Copy to struct iucv_array. */
983                         size = (size < 8) ? size : 8;
984                         for (array = buffer; size > 0; array++) {
985                                 copy = min_t(size_t, size, array->length);
986                                 memcpy((u8 *)(addr_t) array->address,
987                                        rmmsg, copy);
988                                 rmmsg += copy;
989                                 size -= copy;
990                         }
991                 } else {
992                         /* Copy to direct buffer. */
993                         memcpy(buffer, rmmsg, min_t(size_t, size, 8));
994                 }
995                 return 0;
996         }
997
998         local_bh_disable();
999         parm = percpu_ptr(iucv_param, smp_processor_id());
1000         memset(parm, 0, sizeof(union iucv_param));
1001         parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1002         parm->db.ipbfln1f = (u32) size;
1003         parm->db.ipmsgid = msg->id;
1004         parm->db.ippathid = path->pathid;
1005         parm->db.iptrgcls = msg->class;
1006         parm->db.ipflags1 = (flags | IUCV_IPFGPID |
1007                              IUCV_IPFGMID | IUCV_IPTRGCLS);
1008         rc = iucv_call_b2f0(IUCV_RECEIVE, parm);
1009         if (!rc || rc == 5) {
1010                 msg->flags = parm->db.ipflags1;
1011                 if (residual)
1012                         *residual = parm->db.ipbfln1f;
1013         }
1014         local_bh_enable();
1015         return rc;
1016 }
1017 EXPORT_SYMBOL(iucv_message_receive);
1018
1019 /**
1020  * iucv_message_reject
1021  * @path: address of iucv path structure
1022  * @msg: address of iucv msg structure
1023  *
1024  * The reject function refuses a specified message. Between the time you
1025  * are notified of a message and the time that you complete the message,
1026  * the message may be rejected.
1027  *
1028  * Returns the result from the CP IUCV call.
1029  */
1030 int iucv_message_reject(struct iucv_path *path, struct iucv_message *msg)
1031 {
1032         union iucv_param *parm;
1033         int rc;
1034
1035         local_bh_disable();
1036         parm = percpu_ptr(iucv_param, smp_processor_id());
1037         memset(parm, 0, sizeof(union iucv_param));
1038         parm->db.ippathid = path->pathid;
1039         parm->db.ipmsgid = msg->id;
1040         parm->db.iptrgcls = msg->class;
1041         parm->db.ipflags1 = (IUCV_IPTRGCLS | IUCV_IPFGMID | IUCV_IPFGPID);
1042         rc = iucv_call_b2f0(IUCV_REJECT, parm);
1043         local_bh_enable();
1044         return rc;
1045 }
1046 EXPORT_SYMBOL(iucv_message_reject);
1047
1048 /**
1049  * iucv_message_reply
1050  * @path: address of iucv path structure
1051  * @msg: address of iucv msg structure
1052  * @flags: how the reply is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1053  * @reply: address of reply data buffer or address of struct iucv_array
1054  * @size: length of reply data buffer
1055  *
1056  * This function responds to the two-way messages that you receive. You
1057  * must identify completely the message to which you wish to reply. ie,
1058  * pathid, msgid, and trgcls. Prmmsg signifies the data is moved into
1059  * the parameter list.
1060  *
1061  * Returns the result from the CP IUCV call.
1062  */
1063 int iucv_message_reply(struct iucv_path *path, struct iucv_message *msg,
1064                        u8 flags, void *reply, size_t size)
1065 {
1066         union iucv_param *parm;
1067         int rc;
1068
1069         local_bh_disable();
1070         parm = percpu_ptr(iucv_param, smp_processor_id());
1071         memset(parm, 0, sizeof(union iucv_param));
1072         if (flags & IUCV_IPRMDATA) {
1073                 parm->dpl.ippathid = path->pathid;
1074                 parm->dpl.ipflags1 = flags;
1075                 parm->dpl.ipmsgid = msg->id;
1076                 parm->dpl.iptrgcls = msg->class;
1077                 memcpy(parm->dpl.iprmmsg, reply, min_t(size_t, size, 8));
1078         } else {
1079                 parm->db.ipbfadr1 = (u32)(addr_t) reply;
1080                 parm->db.ipbfln1f = (u32) size;
1081                 parm->db.ippathid = path->pathid;
1082                 parm->db.ipflags1 = flags;
1083                 parm->db.ipmsgid = msg->id;
1084                 parm->db.iptrgcls = msg->class;
1085         }
1086         rc = iucv_call_b2f0(IUCV_REPLY, parm);
1087         local_bh_enable();
1088         return rc;
1089 }
1090 EXPORT_SYMBOL(iucv_message_reply);
1091
1092 /**
1093  * iucv_message_send
1094  * @path: address of iucv path structure
1095  * @msg: address of iucv msg structure
1096  * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1097  * @srccls: source class of message
1098  * @buffer: address of send buffer or address of struct iucv_array
1099  * @size: length of send buffer
1100  *
1101  * This function transmits data to another application. Data to be
1102  * transmitted is in a buffer and this is a one-way message and the
1103  * receiver will not reply to the message.
1104  *
1105  * Returns the result from the CP IUCV call.
1106  */
1107 int iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
1108                       u8 flags, u32 srccls, void *buffer, size_t size)
1109 {
1110         union iucv_param *parm;
1111         int rc;
1112
1113         local_bh_disable();
1114         parm = percpu_ptr(iucv_param, smp_processor_id());
1115         memset(parm, 0, sizeof(union iucv_param));
1116         if (flags & IUCV_IPRMDATA) {
1117                 /* Message of 8 bytes can be placed into the parameter list. */
1118                 parm->dpl.ippathid = path->pathid;
1119                 parm->dpl.ipflags1 = flags | IUCV_IPNORPY;
1120                 parm->dpl.iptrgcls = msg->class;
1121                 parm->dpl.ipsrccls = srccls;
1122                 parm->dpl.ipmsgtag = msg->tag;
1123                 memcpy(parm->dpl.iprmmsg, buffer, 8);
1124         } else {
1125                 parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1126                 parm->db.ipbfln1f = (u32) size;
1127                 parm->db.ippathid = path->pathid;
1128                 parm->db.ipflags1 = flags | IUCV_IPNORPY;
1129                 parm->db.iptrgcls = msg->class;
1130                 parm->db.ipsrccls = srccls;
1131                 parm->db.ipmsgtag = msg->tag;
1132         }
1133         rc = iucv_call_b2f0(IUCV_SEND, parm);
1134         if (!rc)
1135                 msg->id = parm->db.ipmsgid;
1136         local_bh_enable();
1137         return rc;
1138 }
1139 EXPORT_SYMBOL(iucv_message_send);
1140
1141 /**
1142  * iucv_message_send2way
1143  * @path: address of iucv path structure
1144  * @msg: address of iucv msg structure
1145  * @flags: how the message is sent and the reply is received
1146  *         (IUCV_IPRMDATA, IUCV_IPBUFLST, IUCV_IPPRTY, IUCV_ANSLST)
1147  * @srccls: source class of message
1148  * @buffer: address of send buffer or address of struct iucv_array
1149  * @size: length of send buffer
1150  * @ansbuf: address of answer buffer or address of struct iucv_array
1151  * @asize: size of reply buffer
1152  *
1153  * This function transmits data to another application. Data to be
1154  * transmitted is in a buffer. The receiver of the send is expected to
1155  * reply to the message and a buffer is provided into which IUCV moves
1156  * the reply to this message.
1157  *
1158  * Returns the result from the CP IUCV call.
1159  */
1160 int iucv_message_send2way(struct iucv_path *path, struct iucv_message *msg,
1161                           u8 flags, u32 srccls, void *buffer, size_t size,
1162                           void *answer, size_t asize, size_t *residual)
1163 {
1164         union iucv_param *parm;
1165         int rc;
1166
1167         local_bh_disable();
1168         parm = percpu_ptr(iucv_param, smp_processor_id());
1169         memset(parm, 0, sizeof(union iucv_param));
1170         if (flags & IUCV_IPRMDATA) {
1171                 parm->dpl.ippathid = path->pathid;
1172                 parm->dpl.ipflags1 = path->flags;       /* priority message */
1173                 parm->dpl.iptrgcls = msg->class;
1174                 parm->dpl.ipsrccls = srccls;
1175                 parm->dpl.ipmsgtag = msg->tag;
1176                 parm->dpl.ipbfadr2 = (u32)(addr_t) answer;
1177                 parm->dpl.ipbfln2f = (u32) asize;
1178                 memcpy(parm->dpl.iprmmsg, buffer, 8);
1179         } else {
1180                 parm->db.ippathid = path->pathid;
1181                 parm->db.ipflags1 = path->flags;        /* priority message */
1182                 parm->db.iptrgcls = msg->class;
1183                 parm->db.ipsrccls = srccls;
1184                 parm->db.ipmsgtag = msg->tag;
1185                 parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1186                 parm->db.ipbfln1f = (u32) size;
1187                 parm->db.ipbfadr2 = (u32)(addr_t) answer;
1188                 parm->db.ipbfln2f = (u32) asize;
1189         }
1190         rc = iucv_call_b2f0(IUCV_SEND, parm);
1191         if (!rc)
1192                 msg->id = parm->db.ipmsgid;
1193         local_bh_enable();
1194         return rc;
1195 }
1196 EXPORT_SYMBOL(iucv_message_send2way);
1197
1198 /**
1199  * iucv_path_pending
1200  * @data: Pointer to external interrupt buffer
1201  *
1202  * Process connection pending work item. Called from tasklet while holding
1203  * iucv_table_lock.
1204  */
1205 struct iucv_path_pending {
1206         u16 ippathid;
1207         u8  ipflags1;
1208         u8  iptype;
1209         u16 ipmsglim;
1210         u16 res1;
1211         u8  ipvmid[8];
1212         u8  ipuser[16];
1213         u32 res3;
1214         u8  ippollfg;
1215         u8  res4[3];
1216 } __attribute__ ((packed));
1217
1218 static void iucv_path_pending(struct iucv_irq_data *data)
1219 {
1220         struct iucv_path_pending *ipp = (void *) data;
1221         struct iucv_handler *handler;
1222         struct iucv_path *path;
1223         char *error;
1224
1225         BUG_ON(iucv_path_table[ipp->ippathid]);
1226         /* New pathid, handler found. Create a new path struct. */
1227         error = iucv_error_no_memory;
1228         path = iucv_path_alloc(ipp->ipmsglim, ipp->ipflags1, GFP_ATOMIC);
1229         if (!path)
1230                 goto out_sever;
1231         path->pathid = ipp->ippathid;
1232         iucv_path_table[path->pathid] = path;
1233         EBCASC(ipp->ipvmid, 8);
1234
1235         /* Call registered handler until one is found that wants the path. */
1236         list_for_each_entry(handler, &iucv_handler_list, list) {
1237                 if (!handler->path_pending)
1238                         continue;
1239                 /*
1240                  * Add path to handler to allow a call to iucv_path_sever
1241                  * inside the path_pending function. If the handler returns
1242                  * an error remove the path from the handler again.
1243                  */
1244                 list_add(&path->list, &handler->paths);
1245                 path->handler = handler;
1246                 if (!handler->path_pending(path, ipp->ipvmid, ipp->ipuser))
1247                         return;
1248                 list_del(&path->list);
1249                 path->handler = NULL;
1250         }
1251         /* No handler wanted the path. */
1252         iucv_path_table[path->pathid] = NULL;
1253         iucv_path_free(path);
1254         error = iucv_error_no_listener;
1255 out_sever:
1256         iucv_sever_pathid(ipp->ippathid, error);
1257 }
1258
1259 /**
1260  * iucv_path_complete
1261  * @data: Pointer to external interrupt buffer
1262  *
1263  * Process connection complete work item. Called from tasklet while holding
1264  * iucv_table_lock.
1265  */
1266 struct iucv_path_complete {
1267         u16 ippathid;
1268         u8  ipflags1;
1269         u8  iptype;
1270         u16 ipmsglim;
1271         u16 res1;
1272         u8  res2[8];
1273         u8  ipuser[16];
1274         u32 res3;
1275         u8  ippollfg;
1276         u8  res4[3];
1277 } __attribute__ ((packed));
1278
1279 static void iucv_path_complete(struct iucv_irq_data *data)
1280 {
1281         struct iucv_path_complete *ipc = (void *) data;
1282         struct iucv_path *path = iucv_path_table[ipc->ippathid];
1283
1284         if (path && path->handler && path->handler->path_complete)
1285                 path->handler->path_complete(path, ipc->ipuser);
1286 }
1287
1288 /**
1289  * iucv_path_severed
1290  * @data: Pointer to external interrupt buffer
1291  *
1292  * Process connection severed work item. Called from tasklet while holding
1293  * iucv_table_lock.
1294  */
1295 struct iucv_path_severed {
1296         u16 ippathid;
1297         u8  res1;
1298         u8  iptype;
1299         u32 res2;
1300         u8  res3[8];
1301         u8  ipuser[16];
1302         u32 res4;
1303         u8  ippollfg;
1304         u8  res5[3];
1305 } __attribute__ ((packed));
1306
1307 static void iucv_path_severed(struct iucv_irq_data *data)
1308 {
1309         struct iucv_path_severed *ips = (void *) data;
1310         struct iucv_path *path = iucv_path_table[ips->ippathid];
1311
1312         if (!path || !path->handler)    /* Already severed */
1313                 return;
1314         if (path->handler->path_severed)
1315                 path->handler->path_severed(path, ips->ipuser);
1316         else {
1317                 iucv_sever_pathid(path->pathid, NULL);
1318                 iucv_path_table[path->pathid] = NULL;
1319                 list_del_init(&path->list);
1320                 iucv_path_free(path);
1321         }
1322 }
1323
1324 /**
1325  * iucv_path_quiesced
1326  * @data: Pointer to external interrupt buffer
1327  *
1328  * Process connection quiesced work item. Called from tasklet while holding
1329  * iucv_table_lock.
1330  */
1331 struct iucv_path_quiesced {
1332         u16 ippathid;
1333         u8  res1;
1334         u8  iptype;
1335         u32 res2;
1336         u8  res3[8];
1337         u8  ipuser[16];
1338         u32 res4;
1339         u8  ippollfg;
1340         u8  res5[3];
1341 } __attribute__ ((packed));
1342
1343 static void iucv_path_quiesced(struct iucv_irq_data *data)
1344 {
1345         struct iucv_path_quiesced *ipq = (void *) data;
1346         struct iucv_path *path = iucv_path_table[ipq->ippathid];
1347
1348         if (path && path->handler && path->handler->path_quiesced)
1349                 path->handler->path_quiesced(path, ipq->ipuser);
1350 }
1351
1352 /**
1353  * iucv_path_resumed
1354  * @data: Pointer to external interrupt buffer
1355  *
1356  * Process connection resumed work item. Called from tasklet while holding
1357  * iucv_table_lock.
1358  */
1359 struct iucv_path_resumed {
1360         u16 ippathid;
1361         u8  res1;
1362         u8  iptype;
1363         u32 res2;
1364         u8  res3[8];
1365         u8  ipuser[16];
1366         u32 res4;
1367         u8  ippollfg;
1368         u8  res5[3];
1369 } __attribute__ ((packed));
1370
1371 static void iucv_path_resumed(struct iucv_irq_data *data)
1372 {
1373         struct iucv_path_resumed *ipr = (void *) data;
1374         struct iucv_path *path = iucv_path_table[ipr->ippathid];
1375
1376         if (path && path->handler && path->handler->path_resumed)
1377                 path->handler->path_resumed(path, ipr->ipuser);
1378 }
1379
1380 /**
1381  * iucv_message_complete
1382  * @data: Pointer to external interrupt buffer
1383  *
1384  * Process message complete work item. Called from tasklet while holding
1385  * iucv_table_lock.
1386  */
1387 struct iucv_message_complete {
1388         u16 ippathid;
1389         u8  ipflags1;
1390         u8  iptype;
1391         u32 ipmsgid;
1392         u32 ipaudit;
1393         u8  iprmmsg[8];
1394         u32 ipsrccls;
1395         u32 ipmsgtag;
1396         u32 res;
1397         u32 ipbfln2f;
1398         u8  ippollfg;
1399         u8  res2[3];
1400 } __attribute__ ((packed));
1401
1402 static void iucv_message_complete(struct iucv_irq_data *data)
1403 {
1404         struct iucv_message_complete *imc = (void *) data;
1405         struct iucv_path *path = iucv_path_table[imc->ippathid];
1406         struct iucv_message msg;
1407
1408         if (path && path->handler && path->handler->message_complete) {
1409                 msg.flags = imc->ipflags1;
1410                 msg.id = imc->ipmsgid;
1411                 msg.audit = imc->ipaudit;
1412                 memcpy(msg.rmmsg, imc->iprmmsg, 8);
1413                 msg.class = imc->ipsrccls;
1414                 msg.tag = imc->ipmsgtag;
1415                 msg.length = imc->ipbfln2f;
1416                 path->handler->message_complete(path, &msg);
1417         }
1418 }
1419
1420 /**
1421  * iucv_message_pending
1422  * @data: Pointer to external interrupt buffer
1423  *
1424  * Process message pending work item. Called from tasklet while holding
1425  * iucv_table_lock.
1426  */
1427 struct iucv_message_pending {
1428         u16 ippathid;
1429         u8  ipflags1;
1430         u8  iptype;
1431         u32 ipmsgid;
1432         u32 iptrgcls;
1433         union {
1434                 u32 iprmmsg1_u32;
1435                 u8  iprmmsg1[4];
1436         } ln1msg1;
1437         union {
1438                 u32 ipbfln1f;
1439                 u8  iprmmsg2[4];
1440         } ln1msg2;
1441         u32 res1[3];
1442         u32 ipbfln2f;
1443         u8  ippollfg;
1444         u8  res2[3];
1445 } __attribute__ ((packed));
1446
1447 static void iucv_message_pending(struct iucv_irq_data *data)
1448 {
1449         struct iucv_message_pending *imp = (void *) data;
1450         struct iucv_path *path = iucv_path_table[imp->ippathid];
1451         struct iucv_message msg;
1452
1453         if (path && path->handler && path->handler->message_pending) {
1454                 msg.flags = imp->ipflags1;
1455                 msg.id = imp->ipmsgid;
1456                 msg.class = imp->iptrgcls;
1457                 if (imp->ipflags1 & IUCV_IPRMDATA) {
1458                         memcpy(msg.rmmsg, imp->ln1msg1.iprmmsg1, 8);
1459                         msg.length = 8;
1460                 } else
1461                         msg.length = imp->ln1msg2.ipbfln1f;
1462                 msg.reply_size = imp->ipbfln2f;
1463                 path->handler->message_pending(path, &msg);
1464         }
1465 }
1466
1467 /**
1468  * iucv_tasklet_fn:
1469  *
1470  * This tasklet loops over the queue of irq buffers created by
1471  * iucv_external_interrupt, calls the appropriate action handler
1472  * and then frees the buffer.
1473  */
1474 static void iucv_tasklet_fn(unsigned long ignored)
1475 {
1476         typedef void iucv_irq_fn(struct iucv_irq_data *);
1477         static iucv_irq_fn *irq_fn[] = {
1478                 [0x02] = iucv_path_complete,
1479                 [0x03] = iucv_path_severed,
1480                 [0x04] = iucv_path_quiesced,
1481                 [0x05] = iucv_path_resumed,
1482                 [0x06] = iucv_message_complete,
1483                 [0x07] = iucv_message_complete,
1484                 [0x08] = iucv_message_pending,
1485                 [0x09] = iucv_message_pending,
1486         };
1487         struct list_head task_queue = LIST_HEAD_INIT(task_queue);
1488         struct iucv_irq_list *p, *n;
1489
1490         /* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
1491         spin_lock(&iucv_table_lock);
1492         iucv_active_cpu = smp_processor_id();
1493
1494         spin_lock_irq(&iucv_queue_lock);
1495         list_splice_init(&iucv_task_queue, &task_queue);
1496         spin_unlock_irq(&iucv_queue_lock);
1497
1498         list_for_each_entry_safe(p, n, &task_queue, list) {
1499                 list_del_init(&p->list);
1500                 irq_fn[p->data.iptype](&p->data);
1501                 kfree(p);
1502         }
1503
1504         iucv_active_cpu = -1;
1505         spin_unlock(&iucv_table_lock);
1506 }
1507
1508 /**
1509  * iucv_work_fn:
1510  *
1511  * This work function loops over the queue of path pending irq blocks
1512  * created by iucv_external_interrupt, calls the appropriate action
1513  * handler and then frees the buffer.
1514  */
1515 static void iucv_work_fn(struct work_struct *work)
1516 {
1517         typedef void iucv_irq_fn(struct iucv_irq_data *);
1518         struct list_head work_queue = LIST_HEAD_INIT(work_queue);
1519         struct iucv_irq_list *p, *n;
1520
1521         /* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
1522         spin_lock_bh(&iucv_table_lock);
1523         iucv_active_cpu = smp_processor_id();
1524
1525         spin_lock_irq(&iucv_queue_lock);
1526         list_splice_init(&iucv_work_queue, &work_queue);
1527         spin_unlock_irq(&iucv_queue_lock);
1528
1529         iucv_cleanup_queue();
1530         list_for_each_entry_safe(p, n, &work_queue, list) {
1531                 list_del_init(&p->list);
1532                 iucv_path_pending(&p->data);
1533                 kfree(p);
1534         }
1535
1536         iucv_active_cpu = -1;
1537         spin_unlock_bh(&iucv_table_lock);
1538 }
1539
1540 /**
1541  * iucv_external_interrupt
1542  * @code: irq code
1543  *
1544  * Handles external interrupts coming in from CP.
1545  * Places the interrupt buffer on a queue and schedules iucv_tasklet_fn().
1546  */
1547 static void iucv_external_interrupt(u16 code)
1548 {
1549         struct iucv_irq_data *p;
1550         struct iucv_irq_list *work;
1551
1552         p = percpu_ptr(iucv_irq_data, smp_processor_id());
1553         if (p->ippathid >= iucv_max_pathid) {
1554                 printk(KERN_WARNING "iucv_do_int: Got interrupt with "
1555                        "pathid %d > max_connections (%ld)\n",
1556                        p->ippathid, iucv_max_pathid - 1);
1557                 iucv_sever_pathid(p->ippathid, iucv_error_no_listener);
1558                 return;
1559         }
1560         if (p->iptype  < 0x01 || p->iptype > 0x09) {
1561                 printk(KERN_ERR "iucv_do_int: unknown iucv interrupt\n");
1562                 return;
1563         }
1564         work = kmalloc(sizeof(struct iucv_irq_list), GFP_ATOMIC);
1565         if (!work) {
1566                 printk(KERN_WARNING "iucv_external_interrupt: out of memory\n");
1567                 return;
1568         }
1569         memcpy(&work->data, p, sizeof(work->data));
1570         spin_lock(&iucv_queue_lock);
1571         if (p->iptype == 0x01) {
1572                 /* Path pending interrupt. */
1573                 list_add_tail(&work->list, &iucv_work_queue);
1574                 schedule_work(&iucv_work);
1575         } else {
1576                 /* The other interrupts. */
1577                 list_add_tail(&work->list, &iucv_task_queue);
1578                 tasklet_schedule(&iucv_tasklet);
1579         }
1580         spin_unlock(&iucv_queue_lock);
1581 }
1582
1583 /**
1584  * iucv_init
1585  *
1586  * Allocates and initializes various data structures.
1587  */
1588 static int __init iucv_init(void)
1589 {
1590         int rc;
1591
1592         if (!MACHINE_IS_VM) {
1593                 rc = -EPROTONOSUPPORT;
1594                 goto out;
1595         }
1596         rc = iucv_query_maxconn();
1597         if (rc)
1598                 goto out;
1599         rc = register_external_interrupt(0x4000, iucv_external_interrupt);
1600         if (rc)
1601                 goto out;
1602         rc = bus_register(&iucv_bus);
1603         if (rc)
1604                 goto out_int;
1605         iucv_root = s390_root_dev_register("iucv");
1606         if (IS_ERR(iucv_root)) {
1607                 rc = PTR_ERR(iucv_root);
1608                 goto out_bus;
1609         }
1610         /* Note: GFP_DMA used to get memory below 2G */
1611         iucv_irq_data = percpu_alloc(sizeof(struct iucv_irq_data),
1612                                      GFP_KERNEL|GFP_DMA);
1613         if (!iucv_irq_data) {
1614                 rc = -ENOMEM;
1615                 goto out_root;
1616         }
1617         /* Allocate parameter blocks. */
1618         iucv_param = percpu_alloc(sizeof(union iucv_param),
1619                                   GFP_KERNEL|GFP_DMA);
1620         if (!iucv_param) {
1621                 rc = -ENOMEM;
1622                 goto out_extint;
1623         }
1624         register_hotcpu_notifier(&iucv_cpu_notifier);
1625         ASCEBC(iucv_error_no_listener, 16);
1626         ASCEBC(iucv_error_no_memory, 16);
1627         ASCEBC(iucv_error_pathid, 16);
1628         iucv_available = 1;
1629         return 0;
1630
1631 out_extint:
1632         percpu_free(iucv_irq_data);
1633 out_root:
1634         s390_root_dev_unregister(iucv_root);
1635 out_bus:
1636         bus_unregister(&iucv_bus);
1637 out_int:
1638         unregister_external_interrupt(0x4000, iucv_external_interrupt);
1639 out:
1640         return rc;
1641 }
1642
1643 /**
1644  * iucv_exit
1645  *
1646  * Frees everything allocated from iucv_init.
1647  */
1648 static void __exit iucv_exit(void)
1649 {
1650         struct iucv_irq_list *p, *n;
1651
1652         spin_lock_irq(&iucv_queue_lock);
1653         list_for_each_entry_safe(p, n, &iucv_task_queue, list)
1654                 kfree(p);
1655         list_for_each_entry_safe(p, n, &iucv_work_queue, list)
1656                 kfree(p);
1657         spin_unlock_irq(&iucv_queue_lock);
1658         unregister_hotcpu_notifier(&iucv_cpu_notifier);
1659         percpu_free(iucv_param);
1660         percpu_free(iucv_irq_data);
1661         s390_root_dev_unregister(iucv_root);
1662         bus_unregister(&iucv_bus);
1663         unregister_external_interrupt(0x4000, iucv_external_interrupt);
1664 }
1665
1666 subsys_initcall(iucv_init);
1667 module_exit(iucv_exit);
1668
1669 MODULE_AUTHOR("(C) 2001 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
1670 MODULE_DESCRIPTION("Linux for S/390 IUCV lowlevel driver");
1671 MODULE_LICENSE("GPL");