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