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