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