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