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