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