Merge mulgrave-w:git/scsi-misc-2.6
[pandora-kernel.git] / drivers / s390 / net / iucv.c
1 /*
2  * IUCV network driver
3  *
4  * Copyright (C) 2001 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  *
12  * Documentation used:
13  *    The original source
14  *    CP Programming Service, IBM document # SC24-5760
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2, or (at your option)
19  * any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  *
30  */
31
32 /* #define DEBUG */
33
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36
37 #include <linux/spinlock.h>
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/interrupt.h>
42 #include <linux/list.h>
43 #include <linux/errno.h>
44 #include <linux/err.h>
45 #include <linux/device.h>
46 #include <asm/atomic.h>
47 #include "iucv.h"
48 #include <asm/io.h>
49 #include <asm/s390_ext.h>
50 #include <asm/ebcdic.h>
51 #include <asm/smp.h>
52 #include <asm/s390_rdev.h>
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  * IPFGMCL  - 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  * IPANSLST - Indicates that you are using an address list for
62  *            reply data
63  * IPBUFLST - Indicates that you are using an address list for
64  *            message data
65  */
66
67 #define IPSRCCLS        0x01
68 #define IPFGMCL         0x01
69 #define IPFGPID         0x02
70 #define IPFGMID         0x04
71 #define IPANSLST        0x08
72 #define IPBUFLST        0x40
73
74 static int
75 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
85 struct device *iucv_root;
86
87 /* General IUCV interrupt structure */
88 typedef struct {
89         __u16 ippathid;
90         __u8  res1;
91         __u8  iptype;
92         __u32 res2;
93         __u8  ipvmid[8];
94         __u8  res3[24];
95 } iucv_GeneralInterrupt;
96
97 static iucv_GeneralInterrupt *iucv_external_int_buffer = NULL;
98
99 /* Spin Lock declaration */
100
101 static DEFINE_SPINLOCK(iucv_lock);
102
103 static int messagesDisabled = 0;
104
105 /***************INTERRUPT HANDLING ***************/
106
107 typedef struct {
108         struct list_head queue;
109         iucv_GeneralInterrupt data;
110 } iucv_irqdata;
111
112 static struct list_head  iucv_irq_queue;
113 static DEFINE_SPINLOCK(iucv_irq_queue_lock);
114
115 /*
116  *Internal function prototypes
117  */
118 static void iucv_tasklet_handler(unsigned long);
119 static void iucv_irq_handler(struct pt_regs *, __u16);
120
121 static DECLARE_TASKLET(iucv_tasklet,iucv_tasklet_handler,0);
122
123 /************ FUNCTION ID'S ****************************/
124
125 #define ACCEPT          10
126 #define CONNECT         11
127 #define DECLARE_BUFFER  12
128 #define PURGE           9
129 #define QUERY           0
130 #define QUIESCE         13
131 #define RECEIVE         5
132 #define REJECT          8
133 #define REPLY           6
134 #define RESUME          14
135 #define RETRIEVE_BUFFER 2
136 #define SEND            4
137 #define SETMASK         16
138 #define SEVER           15
139
140 /**
141  * Structure: handler
142  * members: list - list management.
143  *          structure: id
144  *             userid - 8 char array of machine identification
145  *             user_data - 16 char array for user identification
146  *             mask - 24 char array used to compare the 2 previous
147  *          interrupt_table - vector of interrupt functions.
148  *          pgm_data -  ulong, application data that is passed
149  *                      to the interrupt handlers
150 */
151 typedef struct handler_t {
152         struct list_head list;
153         struct {
154                 __u8 userid[8];
155                 __u8 user_data[16];
156                 __u8 mask[24];
157         }                    id;
158         iucv_interrupt_ops_t *interrupt_table;
159         void                 *pgm_data;
160 } handler;
161
162 /**
163  * iucv_handler_table: List of registered handlers.
164  */
165 static struct list_head iucv_handler_table;
166
167 /**
168  * iucv_pathid_table: an array of *handler pointing into
169  *                    iucv_handler_table for fast indexing by pathid;
170  */
171 static handler **iucv_pathid_table;
172
173 static unsigned long max_connections;
174
175 /**
176  * iucv_cpuid: contains the logical cpu number of the cpu which
177  * has declared the iucv buffer by issuing DECLARE_BUFFER.
178  * If no cpu has done the initialization iucv_cpuid contains -1.
179  */
180 static int iucv_cpuid = -1;
181 /**
182  * register_flag: is 0 when external interrupt has not been registered
183  */
184 static int register_flag;
185
186 /****************FIVE 40-BYTE PARAMETER STRUCTURES******************/
187 /* Data struct 1: iparml_control
188  * Used for iucv_accept
189  *          iucv_connect
190  *          iucv_quiesce
191  *          iucv_resume
192  *          iucv_sever
193  *          iucv_retrieve_buffer
194  * Data struct 2: iparml_dpl     (data in parameter list)
195  * Used for iucv_send_prmmsg
196  *          iucv_send2way_prmmsg
197  *          iucv_send2way_prmmsg_array
198  *          iucv_reply_prmmsg
199  * Data struct 3: iparml_db       (data in a buffer)
200  * Used for iucv_receive
201  *          iucv_receive_array
202  *          iucv_reject
203  *          iucv_reply
204  *          iucv_reply_array
205  *          iucv_send
206  *          iucv_send_array
207  *          iucv_send2way
208  *          iucv_send2way_array
209  *          iucv_declare_buffer
210  * Data struct 4: iparml_purge
211  * Used for iucv_purge
212  *          iucv_query
213  * Data struct 5: iparml_set_mask
214  * Used for iucv_set_mask
215  */
216
217 typedef struct {
218         __u16 ippathid;
219         __u8  ipflags1;
220         __u8  iprcode;
221         __u16 ipmsglim;
222         __u16 res1;
223         __u8  ipvmid[8];
224         __u8  ipuser[16];
225         __u8  iptarget[8];
226 } iparml_control;
227
228 typedef struct {
229         __u16 ippathid;
230         __u8  ipflags1;
231         __u8  iprcode;
232         __u32 ipmsgid;
233         __u32 iptrgcls;
234         __u8  iprmmsg[8];
235         __u32 ipsrccls;
236         __u32 ipmsgtag;
237         __u32 ipbfadr2;
238         __u32 ipbfln2f;
239         __u32 res;
240 } iparml_dpl;
241
242 typedef struct {
243         __u16 ippathid;
244         __u8  ipflags1;
245         __u8  iprcode;
246         __u32 ipmsgid;
247         __u32 iptrgcls;
248         __u32 ipbfadr1;
249         __u32 ipbfln1f;
250         __u32 ipsrccls;
251         __u32 ipmsgtag;
252         __u32 ipbfadr2;
253         __u32 ipbfln2f;
254         __u32 res;
255 } iparml_db;
256
257 typedef struct {
258         __u16 ippathid;
259         __u8  ipflags1;
260         __u8  iprcode;
261         __u32 ipmsgid;
262         __u8  ipaudit[3];
263         __u8  res1[5];
264         __u32 res2;
265         __u32 ipsrccls;
266         __u32 ipmsgtag;
267         __u32 res3[3];
268 } iparml_purge;
269
270 typedef struct {
271         __u8  ipmask;
272         __u8  res1[2];
273         __u8  iprcode;
274         __u32 res2[9];
275 } iparml_set_mask;
276
277 typedef struct {
278         union {
279                 iparml_control  p_ctrl;
280                 iparml_dpl      p_dpl;
281                 iparml_db       p_db;
282                 iparml_purge    p_purge;
283                 iparml_set_mask p_set_mask;
284         } param;
285         atomic_t in_use;
286         __u32    res;
287 }  __attribute__ ((aligned(8))) iucv_param;
288 #define PARAM_POOL_SIZE (PAGE_SIZE / sizeof(iucv_param))
289
290 static iucv_param * iucv_param_pool;
291
292 MODULE_AUTHOR("(C) 2001 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
293 MODULE_DESCRIPTION("Linux for S/390 IUCV lowlevel driver");
294 MODULE_LICENSE("GPL");
295
296 /*
297  * Debugging stuff
298  *******************************************************************************/
299
300
301 #ifdef DEBUG
302 static int debuglevel = 0;
303
304 module_param(debuglevel, int, 0);
305 MODULE_PARM_DESC(debuglevel,
306  "Specifies the debug level (0=off ... 3=all)");
307
308 static void
309 iucv_dumpit(char *title, void *buf, int len)
310 {
311         int i;
312         __u8 *p = (__u8 *)buf;
313
314         if (debuglevel < 3)
315                 return;
316
317         printk(KERN_DEBUG "%s\n", title);
318         printk("  ");
319         for (i = 0; i < len; i++) {
320                 if (!(i % 16) && i != 0)
321                         printk ("\n  ");
322                 else if (!(i % 4) && i != 0)
323                         printk(" ");
324                 printk("%02X", *p++);
325         }
326         if (len % 16)
327                 printk ("\n");
328         return;
329 }
330 #define iucv_debug(lvl, fmt, args...) \
331 do { \
332         if (debuglevel >= lvl) \
333                 printk(KERN_DEBUG "%s: " fmt "\n", __FUNCTION__ , ## args); \
334 } while (0)
335
336 #else
337
338 #define iucv_debug(lvl, fmt, args...)   do { } while (0)
339 #define iucv_dumpit(title, buf, len)    do { } while (0)
340
341 #endif
342
343 /*
344  * Internal functions
345  *******************************************************************************/
346
347 /**
348  * print start banner
349  */
350 static void
351 iucv_banner(void)
352 {
353         printk(KERN_INFO "IUCV lowlevel driver initialized\n");
354 }
355
356 /**
357  * iucv_init - Initialization
358  *
359  * Allocates and initializes various data structures.
360  */
361 static int
362 iucv_init(void)
363 {
364         int ret;
365
366         if (iucv_external_int_buffer)
367                 return 0;
368
369         if (!MACHINE_IS_VM) {
370                 printk(KERN_ERR "IUCV: IUCV connection needs VM as base\n");
371                 return -EPROTONOSUPPORT;
372         }
373
374         ret = bus_register(&iucv_bus);
375         if (ret) {
376                 printk(KERN_ERR "IUCV: failed to register bus.\n");
377                 return ret;
378         }
379
380         iucv_root = s390_root_dev_register("iucv");
381         if (IS_ERR(iucv_root)) {
382                 printk(KERN_ERR "IUCV: failed to register iucv root.\n");
383                 bus_unregister(&iucv_bus);
384                 return PTR_ERR(iucv_root);
385         }
386
387         /* Note: GFP_DMA used used to get memory below 2G */
388         iucv_external_int_buffer = kzalloc(sizeof(iucv_GeneralInterrupt),
389                                            GFP_KERNEL|GFP_DMA);
390         if (!iucv_external_int_buffer) {
391                 printk(KERN_WARNING
392                        "%s: Could not allocate external interrupt buffer\n",
393                        __FUNCTION__);
394                 s390_root_dev_unregister(iucv_root);
395                 bus_unregister(&iucv_bus);
396                 return -ENOMEM;
397         }
398
399         /* Initialize parameter pool */
400         iucv_param_pool = kzalloc(sizeof(iucv_param) * PARAM_POOL_SIZE,
401                                   GFP_KERNEL|GFP_DMA);
402         if (!iucv_param_pool) {
403                 printk(KERN_WARNING "%s: Could not allocate param pool\n",
404                        __FUNCTION__);
405                 kfree(iucv_external_int_buffer);
406                 iucv_external_int_buffer = NULL;
407                 s390_root_dev_unregister(iucv_root);
408                 bus_unregister(&iucv_bus);
409                 return -ENOMEM;
410         }
411
412         /* Initialize irq queue */
413         INIT_LIST_HEAD(&iucv_irq_queue);
414
415         /* Initialize handler table */
416         INIT_LIST_HEAD(&iucv_handler_table);
417
418         iucv_banner();
419         return 0;
420 }
421
422 /**
423  * iucv_exit - De-Initialization
424  *
425  * Frees everything allocated from iucv_init.
426  */
427 static int iucv_retrieve_buffer (void);
428
429 static void
430 iucv_exit(void)
431 {
432         iucv_retrieve_buffer();
433         kfree(iucv_external_int_buffer);
434         iucv_external_int_buffer = NULL;
435         kfree(iucv_param_pool);
436         iucv_param_pool = NULL;
437         s390_root_dev_unregister(iucv_root);
438         bus_unregister(&iucv_bus);
439         printk(KERN_INFO "IUCV lowlevel driver unloaded\n");
440 }
441
442 /**
443  * grab_param: - Get a parameter buffer from the pre-allocated pool.
444  *
445  * This function searches for an unused element in the pre-allocated pool
446  * of parameter buffers. If one is found, it marks it "in use" and returns
447  * a pointer to it. The calling function is responsible for releasing it
448  * when it has finished its usage.
449  *
450  * Returns: A pointer to iucv_param.
451  */
452 static __inline__ iucv_param *
453 grab_param(void)
454 {
455         iucv_param *ptr;
456         static int hint = 0;
457
458         ptr = iucv_param_pool + hint;
459         do {
460                 ptr++;
461                 if (ptr >= iucv_param_pool + PARAM_POOL_SIZE)
462                         ptr = iucv_param_pool;
463         } while (atomic_cmpxchg(&ptr->in_use, 0, 1) != 0);
464         hint = ptr - iucv_param_pool;
465
466         memset(&ptr->param, 0, sizeof(ptr->param));
467         return ptr;
468 }
469
470 /**
471  * release_param - Release a parameter buffer.
472  * @p: A pointer to a struct iucv_param, previously obtained by calling
473  *     grab_param().
474  *
475  * This function marks the specified parameter buffer "unused".
476  */
477 static __inline__ void
478 release_param(void *p)
479 {
480         atomic_set(&((iucv_param *)p)->in_use, 0);
481 }
482
483 /**
484  * iucv_add_handler: - Add a new handler
485  * @new_handler: handle that is being entered into chain.
486  *
487  * Places new handle on iucv_handler_table, if identical handler is not
488  * found.
489  *
490  * Returns: 0 on success, !0 on failure (handler already in chain).
491  */
492 static int
493 iucv_add_handler (handler *new)
494 {
495         ulong flags;
496
497         iucv_debug(1, "entering");
498         iucv_dumpit("handler:", new, sizeof(handler));
499
500         spin_lock_irqsave (&iucv_lock, flags);
501         if (!list_empty(&iucv_handler_table)) {
502                 struct list_head *lh;
503
504                 /**
505                  * Search list for handler with identical id. If one
506                  * is found, the new handler is _not_ added.
507                  */
508                 list_for_each(lh, &iucv_handler_table) {
509                         handler *h = list_entry(lh, handler, list);
510                         if (!memcmp(&new->id, &h->id, sizeof(h->id))) {
511                                 iucv_debug(1, "ret 1");
512                                 spin_unlock_irqrestore (&iucv_lock, flags);
513                                 return 1;
514                         }
515                 }
516         }
517         /**
518          * If we get here, no handler was found.
519          */
520         INIT_LIST_HEAD(&new->list);
521         list_add(&new->list, &iucv_handler_table);
522         spin_unlock_irqrestore (&iucv_lock, flags);
523
524         iucv_debug(1, "exiting");
525         return 0;
526 }
527
528 /**
529  * b2f0:
530  * @code: identifier of IUCV call to CP.
531  * @parm: pointer to 40 byte iparml area passed to CP
532  *
533  * Calls CP to execute IUCV commands.
534  *
535  * Returns: return code from CP's IUCV call
536  */
537 static __inline__ ulong
538 b2f0(__u32 code, void *parm)
539 {
540         iucv_dumpit("iparml before b2f0 call:", parm, sizeof(iucv_param));
541
542         asm volatile (
543                 "LRA   1,0(%1)\n\t"
544                 "LR    0,%0\n\t"
545                 ".long 0xb2f01000"
546                 :
547                 : "d" (code), "a" (parm)
548                 : "0", "1"
549                 );
550
551         iucv_dumpit("iparml after b2f0 call:", parm, sizeof(iucv_param));
552
553         return (unsigned long)*((__u8 *)(parm + 3));
554 }
555
556 /*
557  * Name: iucv_add_pathid
558  * Purpose: Adds a path id to the system.
559  * Input: pathid -  pathid that is going to be entered into system
560  *        handle -  address of handler that the pathid will be associated
561  *                 with.
562  *        pgm_data - token passed in by application.
563  * Output: 0: successful addition of pathid
564  *         - EINVAL - pathid entry is being used by another application
565  *         - ENOMEM - storage allocation for a new pathid table failed
566 */
567 static int
568 __iucv_add_pathid(__u16 pathid, handler *handler)
569 {
570
571         iucv_debug(1, "entering");
572
573         iucv_debug(1, "handler is pointing to %p", handler);
574
575         if (pathid > (max_connections - 1))
576                 return -EINVAL;
577
578         if (iucv_pathid_table[pathid]) {
579                 iucv_debug(1, "pathid entry is %p", iucv_pathid_table[pathid]);
580                 printk(KERN_WARNING
581                        "%s: Pathid being used, error.\n", __FUNCTION__);
582                 return -EINVAL;
583         }
584         iucv_pathid_table[pathid] = handler;
585
586         iucv_debug(1, "exiting");
587         return 0;
588 }                               /* end of add_pathid function */
589
590 static int
591 iucv_add_pathid(__u16 pathid, handler *handler)
592 {
593         ulong flags;
594         int rc;
595
596         spin_lock_irqsave (&iucv_lock, flags);
597         rc = __iucv_add_pathid(pathid, handler);
598         spin_unlock_irqrestore (&iucv_lock, flags);
599         return rc;
600 }
601
602 static void
603 iucv_remove_pathid(__u16 pathid)
604 {
605         ulong flags;
606
607         if (pathid > (max_connections - 1))
608                 return;
609
610         spin_lock_irqsave (&iucv_lock, flags);
611         iucv_pathid_table[pathid] = NULL;
612         spin_unlock_irqrestore (&iucv_lock, flags);
613 }
614
615 /**
616  * iucv_declare_buffer_cpuid
617  * Register at VM for subsequent IUCV operations. This is executed
618  * on the reserved CPU iucv_cpuid. Called from iucv_declare_buffer().
619  */
620 static void
621 iucv_declare_buffer_cpuid (void *result)
622 {
623         iparml_db *parm;
624
625         parm = (iparml_db *)grab_param();
626         parm->ipbfadr1 = virt_to_phys(iucv_external_int_buffer);
627         if ((*((ulong *)result) = b2f0(DECLARE_BUFFER, parm)) == 1)
628                 *((ulong *)result) = parm->iprcode;
629         release_param(parm);
630 }
631
632 /**
633  * iucv_retrieve_buffer_cpuid:
634  * Unregister IUCV usage at VM. This is always executed on the same
635  * cpu that registered the buffer to VM.
636  * Called from iucv_retrieve_buffer().
637  */
638 static void
639 iucv_retrieve_buffer_cpuid (void *cpu)
640 {
641         iparml_control *parm;
642
643         parm = (iparml_control *)grab_param();
644         b2f0(RETRIEVE_BUFFER, parm);
645         release_param(parm);
646 }
647
648 /**
649  * Name: iucv_declare_buffer
650  * Purpose: Specifies the guests real address of an external
651  *          interrupt.
652  * Input: void
653  * Output: iprcode - return code from b2f0 call
654  */
655 static int
656 iucv_declare_buffer (void)
657 {
658         unsigned long flags;
659         ulong b2f0_result;
660
661         iucv_debug(1, "entering");
662         b2f0_result = -ENODEV;
663         spin_lock_irqsave (&iucv_lock, flags);
664         if (iucv_cpuid == -1) {
665                 /* Reserve any cpu for use by iucv. */
666                 iucv_cpuid = smp_get_cpu(CPU_MASK_ALL);
667                 spin_unlock_irqrestore (&iucv_lock, flags);
668                 smp_call_function_on(iucv_declare_buffer_cpuid,
669                         &b2f0_result, 0, 1, iucv_cpuid);
670                 if (b2f0_result) {
671                         smp_put_cpu(iucv_cpuid);
672                         iucv_cpuid = -1;
673                 }
674                 iucv_debug(1, "Address of EIB = %p", iucv_external_int_buffer);
675         } else {
676                 spin_unlock_irqrestore (&iucv_lock, flags);
677                 b2f0_result = 0;
678         }
679         iucv_debug(1, "exiting");
680         return b2f0_result;
681 }
682
683 /**
684  * iucv_retrieve_buffer:
685  *
686  * Terminates all use of IUCV.
687  * Returns: return code from CP
688  */
689 static int
690 iucv_retrieve_buffer (void)
691 {
692         iucv_debug(1, "entering");
693         if (iucv_cpuid != -1) {
694                 smp_call_function_on(iucv_retrieve_buffer_cpuid,
695                                      NULL, 0, 1, iucv_cpuid);
696                 /* Release the cpu reserved by iucv_declare_buffer. */
697                 smp_put_cpu(iucv_cpuid);
698                 iucv_cpuid = -1;
699         }
700         iucv_debug(1, "exiting");
701         return 0;
702 }
703
704 /**
705  * iucv_remove_handler:
706  * @users_handler: handler to be removed
707  *
708  * Remove handler when application unregisters.
709  */
710 static void
711 iucv_remove_handler(handler *handler)
712 {
713         unsigned long flags;
714
715         if ((!iucv_pathid_table) || (!handler))
716                 return;
717
718         iucv_debug(1, "entering");
719
720         spin_lock_irqsave (&iucv_lock, flags);
721         list_del(&handler->list);
722         if (list_empty(&iucv_handler_table)) {
723                 if (register_flag) {
724                         unregister_external_interrupt(0x4000, iucv_irq_handler);
725                         register_flag = 0;
726                 }
727         }
728         spin_unlock_irqrestore (&iucv_lock, flags);
729
730         iucv_debug(1, "exiting");
731         return;
732 }
733
734 /**
735  * iucv_register_program:
736  * @pgmname:  user identification
737  * @userid:   machine identification
738  * @pgmmask:  Indicates which bits in the pgmname and userid combined will be
739  *            used to determine who is given control.
740  * @ops:      Address of interrupt handler table.
741  * @pgm_data: Application data to be passed to interrupt handlers.
742  *
743  * Registers an application with IUCV.
744  * Returns:
745  *           The address of handler, or NULL on failure.
746  * NOTE on pgmmask:
747  *   If pgmname, userid and pgmmask are provided, pgmmask is entered into the
748  *   handler as is.
749  *   If pgmmask is NULL, the internal mask is set to all 0xff's
750  *   When userid is NULL, the first 8 bytes of the internal mask are forced
751  *   to 0x00.
752  *   If pgmmask and userid are NULL, the first 8 bytes of the internal mask
753  *   are forced to 0x00 and the last 16 bytes to 0xff.
754  */
755
756 iucv_handle_t
757 iucv_register_program (__u8 pgmname[16],
758                        __u8 userid[8],
759                        __u8 pgmmask[24],
760                        iucv_interrupt_ops_t * ops, void *pgm_data)
761 {
762         ulong rc = 0;           /* return code from function calls */
763         handler *new_handler;
764
765         iucv_debug(1, "entering");
766
767         if (ops == NULL) {
768                 /* interrupt table is not defined */
769                 printk(KERN_WARNING "%s: Interrupt table is not defined, "
770                        "exiting\n", __FUNCTION__);
771                 return NULL;
772         }
773         if (!pgmname) {
774                 printk(KERN_WARNING "%s: pgmname not provided\n", __FUNCTION__);
775                 return NULL;
776         }
777
778         /* Allocate handler entry */
779         new_handler = (handler *)kmalloc(sizeof(handler), GFP_ATOMIC);
780         if (new_handler == NULL) {
781                 printk(KERN_WARNING "%s: storage allocation for new handler "
782                        "failed.\n", __FUNCTION__);
783                 return NULL;
784         }
785
786         if (!iucv_pathid_table) {
787                 if (iucv_init()) {
788                         kfree(new_handler);
789                         return NULL;
790                 }
791
792                 max_connections = iucv_query_maxconn();
793                 iucv_pathid_table = kcalloc(max_connections, sizeof(handler *),
794                                         GFP_ATOMIC);
795                 if (iucv_pathid_table == NULL) {
796                         printk(KERN_WARNING "%s: iucv_pathid_table storage "
797                                "allocation failed\n", __FUNCTION__);
798                         kfree(new_handler);
799                         return NULL;
800                 }
801         }
802         memset(new_handler, 0, sizeof (handler));
803         memcpy(new_handler->id.user_data, pgmname,
804                 sizeof (new_handler->id.user_data));
805         if (userid) {
806                 memcpy (new_handler->id.userid, userid,
807                         sizeof (new_handler->id.userid));
808                 ASCEBC (new_handler->id.userid,
809                         sizeof (new_handler->id.userid));
810                 EBC_TOUPPER (new_handler->id.userid,
811                              sizeof (new_handler->id.userid));
812
813                 if (pgmmask) {
814                         memcpy (new_handler->id.mask, pgmmask,
815                                 sizeof (new_handler->id.mask));
816                 } else {
817                         memset (new_handler->id.mask, 0xFF,
818                                 sizeof (new_handler->id.mask));
819                 }
820         } else {
821                 if (pgmmask) {
822                         memcpy (new_handler->id.mask, pgmmask,
823                                 sizeof (new_handler->id.mask));
824                 } else {
825                         memset (new_handler->id.mask, 0xFF,
826                                 sizeof (new_handler->id.mask));
827                 }
828                 memset (new_handler->id.userid, 0x00,
829                         sizeof (new_handler->id.userid));
830         }
831         /* fill in the rest of handler */
832         new_handler->pgm_data = pgm_data;
833         new_handler->interrupt_table = ops;
834
835         /*
836          * Check if someone else is registered with same pgmname, userid
837          * and mask. If someone is already registered with same pgmname,
838          * userid and mask, registration will fail and NULL will be returned
839          * to the application.
840          * If identical handler not found, then handler is added to list.
841          */
842         rc = iucv_add_handler(new_handler);
843         if (rc) {
844                 printk(KERN_WARNING "%s: Someone already registered with same "
845                        "pgmname, userid, pgmmask\n", __FUNCTION__);
846                 kfree (new_handler);
847                 return NULL;
848         }
849
850         rc = iucv_declare_buffer();
851         if (rc) {
852                 char *err = "Unknown";
853                 iucv_remove_handler(new_handler);
854                 kfree(new_handler);
855                 switch(rc) {
856                 case 0x03:
857                         err = "Directory error";
858                         break;
859                 case 0x0a:
860                         err = "Invalid length";
861                         break;
862                 case 0x13:
863                         err = "Buffer already exists";
864                         break;
865                 case 0x3e:
866                         err = "Buffer overlap";
867                         break;
868                 case 0x5c:
869                         err = "Paging or storage error";
870                         break;
871                 }
872                 printk(KERN_WARNING "%s: iucv_declare_buffer "
873                        "returned error 0x%02lx (%s)\n", __FUNCTION__, rc, err);
874                 return NULL;
875         }
876         if (!register_flag) {
877                 /* request the 0x4000 external interrupt */
878                 rc = register_external_interrupt (0x4000, iucv_irq_handler);
879                 if (rc) {
880                         iucv_remove_handler(new_handler);
881                         kfree (new_handler);
882                         printk(KERN_WARNING "%s: "
883                                "register_external_interrupt returned %ld\n",
884                                __FUNCTION__, rc);
885                         return NULL;
886
887                 }
888                 register_flag = 1;
889         }
890         iucv_debug(1, "exiting");
891         return new_handler;
892 }                               /* end of register function */
893
894 /**
895  * iucv_unregister_program:
896  * @handle: address of handler
897  *
898  * Unregister application with IUCV.
899  * Returns:
900  *   0 on success, -EINVAL, if specified handle is invalid.
901  */
902
903 int
904 iucv_unregister_program (iucv_handle_t handle)
905 {
906         handler *h = NULL;
907         struct list_head *lh;
908         int i;
909         ulong flags;
910
911         iucv_debug(1, "entering");
912         iucv_debug(1, "address of handler is %p", h);
913
914         /* Checking if handle is valid  */
915         spin_lock_irqsave (&iucv_lock, flags);
916         list_for_each(lh, &iucv_handler_table) {
917                 if ((handler *)handle == list_entry(lh, handler, list)) {
918                         h = (handler *)handle;
919                         break;
920                 }
921         }
922         if (!h) {
923                 spin_unlock_irqrestore (&iucv_lock, flags);
924                 if (handle)
925                         printk(KERN_WARNING
926                                "%s: Handler not found in iucv_handler_table.\n",
927                                __FUNCTION__);
928                 else
929                         printk(KERN_WARNING
930                                "%s: NULL handle passed by application.\n",
931                                __FUNCTION__);
932                 return -EINVAL;
933         }
934
935         /**
936          * First, walk thru iucv_pathid_table and sever any pathid which is
937          * still pointing to the handler to be removed.
938          */
939         for (i = 0; i < max_connections; i++)
940                 if (iucv_pathid_table[i] == h) {
941                         spin_unlock_irqrestore (&iucv_lock, flags);
942                         iucv_sever(i, h->id.user_data);
943                         spin_lock_irqsave(&iucv_lock, flags);
944                 }
945         spin_unlock_irqrestore (&iucv_lock, flags);
946
947         iucv_remove_handler(h);
948         kfree(h);
949
950         iucv_debug(1, "exiting");
951         return 0;
952 }
953
954 /**
955  * iucv_accept:
956  * @pathid:             Path identification number
957  * @msglim_reqstd:      The number of outstanding messages requested.
958  * @user_data:          Data specified by the iucv_connect function.
959  * @flags1:             Contains options for this path.
960  *     - IPPRTY (0x20)   Specifies if you want to send priority message.
961  *     - IPRMDATA (0x80) Specifies whether your program can handle a message
962  *                       in the parameter list.
963  *     - IPQUSCE (0x40)  Specifies whether you want to quiesce the path being
964  *                       established.
965  * @handle:             Address of handler.
966  * @pgm_data:           Application data passed to interrupt handlers.
967  * @flags1_out:         Pointer to an int. If not NULL, on return the options for
968  *                      the path are stored at the given location:
969  *     - IPPRTY (0x20)  Indicates you may send a priority message.
970  * @msglim:             Pointer to an __u16. If not NULL, on return the maximum
971  *                      number of outstanding messages is stored at the given
972  *                      location.
973  *
974  * This function is issued after the user receives a Connection Pending external
975  * interrupt and now wishes to complete the IUCV communication path.
976  * Returns:
977  *   return code from CP
978  */
979 int
980 iucv_accept(__u16 pathid, __u16 msglim_reqstd,
981              __u8 user_data[16], int flags1,
982              iucv_handle_t handle, void *pgm_data,
983              int *flags1_out, __u16 * msglim)
984 {
985         ulong b2f0_result = 0;
986         ulong flags;
987         struct list_head *lh;
988         handler *h = NULL;
989         iparml_control *parm;
990
991         iucv_debug(1, "entering");
992         iucv_debug(1, "pathid = %d", pathid);
993
994         /* Checking if handle is valid  */
995         spin_lock_irqsave (&iucv_lock, flags);
996         list_for_each(lh, &iucv_handler_table) {
997                 if ((handler *)handle == list_entry(lh, handler, list)) {
998                         h = (handler *)handle;
999                         break;
1000                 }
1001         }
1002         spin_unlock_irqrestore (&iucv_lock, flags);
1003
1004         if (!h) {
1005                 if (handle)
1006                         printk(KERN_WARNING
1007                                "%s: Handler not found in iucv_handler_table.\n",
1008                                __FUNCTION__);
1009                 else
1010                         printk(KERN_WARNING
1011                                "%s: NULL handle passed by application.\n",
1012                                __FUNCTION__);
1013                 return -EINVAL;
1014         }
1015
1016         parm = (iparml_control *)grab_param();
1017
1018         parm->ippathid = pathid;
1019         parm->ipmsglim = msglim_reqstd;
1020         if (user_data)
1021                 memcpy(parm->ipuser, user_data, sizeof(parm->ipuser));
1022
1023         parm->ipflags1 = (__u8)flags1;
1024         b2f0_result = b2f0(ACCEPT, parm);
1025
1026         if (!b2f0_result) {
1027                 if (msglim)
1028                         *msglim = parm->ipmsglim;
1029                 if (pgm_data)
1030                         h->pgm_data = pgm_data;
1031                 if (flags1_out)
1032                         *flags1_out = (parm->ipflags1 & IPPRTY) ? IPPRTY : 0;
1033         }
1034         release_param(parm);
1035
1036         iucv_debug(1, "exiting");
1037         return b2f0_result;
1038 }
1039
1040 /**
1041  * iucv_connect:
1042  * @pathid:        Path identification number
1043  * @msglim_reqstd: Number of outstanding messages requested
1044  * @user_data:     16-byte user data
1045  * @userid:        8-byte of user identification
1046  * @system_name:   8-byte identifying the system name
1047  * @flags1:        Specifies options for this path:
1048  *     - IPPRTY (0x20)   Specifies if you want to send priority message.
1049  *     - IPRMDATA (0x80) Specifies whether your program can handle a message
1050  *                       in  the parameter list.
1051  *     - IPQUSCE (0x40)  Specifies whether you want to quiesce the path being
1052  *                       established.
1053  *     - IPLOCAL (0x01)  Allows an application to force the partner to be on the
1054  *                       local system. If local is specified then target class
1055  *                       cannot be specified.
1056  * @flags1_out:    Pointer to an int. If not NULL, on return the options for
1057  *                 the path are stored at the given location:
1058  *     - IPPRTY (0x20)   Indicates you may send a priority message.
1059  * @msglim:        Pointer to an __u16. If not NULL, on return the maximum
1060  *                 number of outstanding messages is stored at the given
1061  *                 location.
1062  * @handle:        Address of handler.
1063  * @pgm_data:      Application data to be passed to interrupt handlers.
1064  *
1065  * This function establishes an IUCV path. Although the connect may complete
1066  * successfully, you are not able to use the path until you receive an IUCV
1067  * Connection Complete external interrupt.
1068  * Returns: return code from CP, or one of the following
1069  *     - ENOMEM
1070  *     - return code from iucv_declare_buffer
1071  *     - EINVAL - invalid handle passed by application
1072  *     - EINVAL - pathid address is NULL
1073  *     - ENOMEM - pathid table storage allocation failed
1074  *     - return code from internal function add_pathid
1075  */
1076 int
1077 iucv_connect (__u16 *pathid, __u16 msglim_reqstd,
1078               __u8 user_data[16], __u8 userid[8],
1079               __u8 system_name[8], int flags1,
1080               int *flags1_out, __u16 * msglim,
1081               iucv_handle_t handle, void *pgm_data)
1082 {
1083         iparml_control *parm;
1084         iparml_control local_parm;
1085         struct list_head *lh;
1086         ulong b2f0_result = 0;
1087         ulong flags;
1088         int add_pathid_result = 0;
1089         handler *h = NULL;
1090         __u8 no_memory[16] = "NO MEMORY";
1091
1092         iucv_debug(1, "entering");
1093
1094         /* Checking if handle is valid  */
1095         spin_lock_irqsave (&iucv_lock, flags);
1096         list_for_each(lh, &iucv_handler_table) {
1097                 if ((handler *)handle == list_entry(lh, handler, list)) {
1098                         h = (handler *)handle;
1099                         break;
1100                 }
1101         }
1102         spin_unlock_irqrestore (&iucv_lock, flags);
1103
1104         if (!h) {
1105                 if (handle)
1106                         printk(KERN_WARNING
1107                                "%s: Handler not found in iucv_handler_table.\n",
1108                                __FUNCTION__);
1109                 else
1110                         printk(KERN_WARNING
1111                                "%s: NULL handle passed by application.\n",
1112                                __FUNCTION__);
1113                 return -EINVAL;
1114         }
1115
1116         if (pathid == NULL) {
1117                 printk(KERN_WARNING "%s: NULL pathid pointer\n",
1118                        __FUNCTION__);
1119                 return -EINVAL;
1120         }
1121
1122         parm = (iparml_control *)grab_param();
1123
1124         parm->ipmsglim = msglim_reqstd;
1125
1126         if (user_data)
1127                 memcpy(parm->ipuser, user_data, sizeof(parm->ipuser));
1128
1129         if (userid) {
1130                 memcpy(parm->ipvmid, userid, sizeof(parm->ipvmid));
1131                 ASCEBC(parm->ipvmid, sizeof(parm->ipvmid));
1132                 EBC_TOUPPER(parm->ipvmid, sizeof(parm->ipvmid));
1133         }
1134
1135         if (system_name) {
1136                 memcpy(parm->iptarget, system_name, sizeof(parm->iptarget));
1137                 ASCEBC(parm->iptarget, sizeof(parm->iptarget));
1138                 EBC_TOUPPER(parm->iptarget, sizeof(parm->iptarget));
1139         }
1140
1141         /* In order to establish an IUCV connection, the procedure is:
1142          *
1143          * b2f0(CONNECT)
1144          * take the ippathid from the b2f0 call
1145          * register the handler to the ippathid
1146          *
1147          * Unfortunately, the ConnectionEstablished message gets sent after the
1148          * b2f0(CONNECT) call but before the register is handled.
1149          *
1150          * In order for this race condition to be eliminated, the IUCV Control
1151          * Interrupts must be disabled for the above procedure.
1152          *
1153          * David Kennedy <dkennedy@linuxcare.com>
1154          */
1155
1156         /* Enable everything but IUCV Control messages */
1157         iucv_setmask(~(AllInterrupts));
1158         messagesDisabled = 1;
1159
1160         spin_lock_irqsave (&iucv_lock, flags);
1161         parm->ipflags1 = (__u8)flags1;
1162         b2f0_result = b2f0(CONNECT, parm);
1163         memcpy(&local_parm, parm, sizeof(local_parm));
1164         release_param(parm);
1165         parm = &local_parm;
1166         if (!b2f0_result)
1167                 add_pathid_result = __iucv_add_pathid(parm->ippathid, h);
1168         spin_unlock_irqrestore (&iucv_lock, flags);
1169
1170         if (b2f0_result) {
1171                 iucv_setmask(~0);
1172                 messagesDisabled = 0;
1173                 return b2f0_result;
1174         }
1175
1176         *pathid = parm->ippathid;
1177
1178         /* Enable everything again */
1179         iucv_setmask(IUCVControlInterruptsFlag);
1180
1181         if (msglim)
1182                 *msglim = parm->ipmsglim;
1183         if (flags1_out)
1184                 *flags1_out = (parm->ipflags1 & IPPRTY) ? IPPRTY : 0;
1185
1186         if (add_pathid_result) {
1187                 iucv_sever(*pathid, no_memory);
1188                 printk(KERN_WARNING "%s: add_pathid failed with rc ="
1189                         " %d\n", __FUNCTION__, add_pathid_result);
1190                 return(add_pathid_result);
1191         }
1192
1193         iucv_debug(1, "exiting");
1194         return b2f0_result;
1195 }
1196
1197 /**
1198  * iucv_purge:
1199  * @pathid: Path identification number
1200  * @msgid:  Message ID of message to purge.
1201  * @srccls: Message class of the message to purge.
1202  * @audit:  Pointer to an __u32. If not NULL, on return, information about
1203  *          asynchronous errors that may have affected the normal completion
1204  *          of this message ist stored at the given location.
1205  *
1206  * Cancels a message you have sent.
1207  * Returns: return code from CP
1208  */
1209 int
1210 iucv_purge (__u16 pathid, __u32 msgid, __u32 srccls, __u32 *audit)
1211 {
1212         iparml_purge *parm;
1213         ulong b2f0_result = 0;
1214
1215         iucv_debug(1, "entering");
1216         iucv_debug(1, "pathid = %d", pathid);
1217
1218         parm = (iparml_purge *)grab_param();
1219
1220         parm->ipmsgid = msgid;
1221         parm->ippathid = pathid;
1222         parm->ipsrccls = srccls;
1223         parm->ipflags1 |= (IPSRCCLS | IPFGMID | IPFGPID);
1224         b2f0_result = b2f0(PURGE, parm);
1225
1226         if (!b2f0_result && audit) {
1227                 memcpy(audit, parm->ipaudit, sizeof(parm->ipaudit));
1228                 /* parm->ipaudit has only 3 bytes */
1229                 *audit >>= 8;
1230         }
1231
1232         release_param(parm);
1233
1234         iucv_debug(1, "b2f0_result = %ld", b2f0_result);
1235         iucv_debug(1, "exiting");
1236         return b2f0_result;
1237 }
1238
1239 /**
1240  * iucv_query_generic:
1241  * @want_maxconn: Flag, describing which value is to be returned.
1242  *
1243  * Helper function for iucv_query_maxconn() and iucv_query_bufsize().
1244  *
1245  * Returns: The buffersize, if want_maxconn is 0; the maximum number of
1246  *           connections, if want_maxconn is 1 or an error-code < 0 on failure.
1247  */
1248 static int
1249 iucv_query_generic(int want_maxconn)
1250 {
1251         iparml_purge *parm = (iparml_purge *)grab_param();
1252         int bufsize, maxconn;
1253         int ccode;
1254
1255         /**
1256          * Call b2f0 and store R0 (max buffer size),
1257          * R1 (max connections) and CC.
1258          */
1259         asm volatile (
1260                 "LRA   1,0(%4)\n\t"
1261                 "LR    0,%3\n\t"
1262                 ".long 0xb2f01000\n\t"
1263                 "IPM   %0\n\t"
1264                 "SRL   %0,28\n\t"
1265                 "ST    0,%1\n\t"
1266                 "ST    1,%2\n\t"
1267                 : "=d" (ccode), "=m" (bufsize), "=m" (maxconn)
1268                 : "d" (QUERY), "a" (parm)
1269                 : "0", "1", "cc"
1270                 );
1271         release_param(parm);
1272
1273         if (ccode)
1274                 return -EPERM;
1275         if (want_maxconn)
1276                 return maxconn;
1277         return bufsize;
1278 }
1279
1280 /**
1281  * iucv_query_maxconn:
1282  *
1283  * Determines the maximum number of connections thay may be established.
1284  *
1285  * Returns: Maximum number of connections that can be.
1286  */
1287 ulong
1288 iucv_query_maxconn(void)
1289 {
1290         return iucv_query_generic(1);
1291 }
1292
1293 /**
1294  * iucv_query_bufsize:
1295  *
1296  * Determines the size of the external interrupt buffer.
1297  *
1298  * Returns: Size of external interrupt buffer.
1299  */
1300 ulong
1301 iucv_query_bufsize (void)
1302 {
1303         return iucv_query_generic(0);
1304 }
1305
1306 /**
1307  * iucv_quiesce:
1308  * @pathid:    Path identification number
1309  * @user_data: 16-byte user data
1310  *
1311  * Temporarily suspends incoming messages on an IUCV path.
1312  * You can later reactivate the path by invoking the iucv_resume function.
1313  * Returns: return code from CP
1314  */
1315 int
1316 iucv_quiesce (__u16 pathid, __u8 user_data[16])
1317 {
1318         iparml_control *parm;
1319         ulong b2f0_result = 0;
1320
1321         iucv_debug(1, "entering");
1322         iucv_debug(1, "pathid = %d", pathid);
1323
1324         parm = (iparml_control *)grab_param();
1325
1326         memcpy(parm->ipuser, user_data, sizeof(parm->ipuser));
1327         parm->ippathid = pathid;
1328
1329         b2f0_result = b2f0(QUIESCE, parm);
1330         release_param(parm);
1331
1332         iucv_debug(1, "b2f0_result = %ld", b2f0_result);
1333         iucv_debug(1, "exiting");
1334
1335         return b2f0_result;
1336 }
1337
1338 /**
1339  * iucv_receive:
1340  * @pathid: Path identification number.
1341  * @buffer: Address of buffer to receive. Must be below 2G.
1342  * @buflen: Length of buffer to receive.
1343  * @msgid:  Specifies the message ID.
1344  * @trgcls: Specifies target class.
1345  * @flags1_out: Receives options for path on return.
1346  *    - IPNORPY (0x10)  Specifies whether a reply is required
1347  *    - IPPRTY (0x20)   Specifies if you want to send priority message
1348  *    - IPRMDATA (0x80) Specifies the data is contained in the parameter list
1349  * @residual_buffer: Receives the address of buffer updated by the number
1350  *                   of bytes you have received on return.
1351  * @residual_length: On return, receives one of the following values:
1352  *    - 0                          If the receive buffer is the same length as
1353  *                                 the message.
1354  *    - Remaining bytes in buffer  If the receive buffer is longer than the
1355  *                                 message.
1356  *    - Remaining bytes in message If the receive buffer is shorter than the
1357  *                                 message.
1358  *
1359  * This function receives messages that are being sent to you over established
1360  * paths.
1361  * Returns: return code from CP IUCV call; If the receive buffer is shorter
1362  *   than the message, always 5
1363  *   -EINVAL - buffer address is pointing to NULL
1364  */
1365 int
1366 iucv_receive (__u16 pathid, __u32 msgid, __u32 trgcls,
1367               void *buffer, ulong buflen,
1368               int *flags1_out, ulong * residual_buffer, ulong * residual_length)
1369 {
1370         iparml_db *parm;
1371         ulong b2f0_result;
1372         int moved = 0;  /* number of bytes moved from parmlist to buffer */
1373
1374         iucv_debug(2, "entering");
1375
1376         if (!buffer)
1377                 return -EINVAL;
1378
1379         parm = (iparml_db *)grab_param();
1380
1381         parm->ipbfadr1 = (__u32) (addr_t) buffer;
1382         parm->ipbfln1f = (__u32) ((ulong) buflen);
1383         parm->ipmsgid = msgid;
1384         parm->ippathid = pathid;
1385         parm->iptrgcls = trgcls;
1386         parm->ipflags1 = (IPFGPID | IPFGMID | IPFGMCL);
1387
1388         b2f0_result = b2f0(RECEIVE, parm);
1389
1390         if (!b2f0_result || b2f0_result == 5) {
1391                 if (flags1_out) {
1392                         iucv_debug(2, "*flags1_out = %d", *flags1_out);
1393                         *flags1_out = (parm->ipflags1 & (~0x07));
1394                         iucv_debug(2, "*flags1_out = %d", *flags1_out);
1395                 }
1396
1397                 if (!(parm->ipflags1 & IPRMDATA)) {     /*msg not in parmlist */
1398                         if (residual_length)
1399                                 *residual_length = parm->ipbfln1f;
1400
1401                         if (residual_buffer)
1402                                 *residual_buffer = parm->ipbfadr1;
1403                 } else {
1404                         moved = min_t (unsigned long, buflen, 8);
1405
1406                         memcpy ((char *) buffer,
1407                                 (char *) &parm->ipbfadr1, moved);
1408
1409                         if (buflen < 8)
1410                                 b2f0_result = 5;
1411
1412                         if (residual_length)
1413                                 *residual_length = abs (buflen - 8);
1414
1415                         if (residual_buffer)
1416                                 *residual_buffer = (ulong) (buffer + moved);
1417                 }
1418         }
1419         release_param(parm);
1420
1421         iucv_debug(2, "exiting");
1422         return b2f0_result;
1423 }
1424
1425 /*
1426  * Name: iucv_receive_array
1427  * Purpose: This function receives messages that are being sent to you
1428  *          over established paths.
1429  * Input: pathid - path identification number
1430  *        buffer - address of array of buffers
1431  *        buflen - total length of buffers
1432  *        msgid - specifies the message ID.
1433  *        trgcls - specifies target class
1434  * Output:
1435  *        flags1_out: Options for path.
1436  *          IPNORPY - 0x10 specifies whether a reply is required
1437  *          IPPRTY - 0x20 specifies if you want to send priority message
1438  *         IPRMDATA - 0x80 specifies the data is contained in the parameter list
1439  *       residual_buffer - address points to the current list entry IUCV
1440  *                         is working on.
1441  *       residual_length -
1442  *              Contains one of the following values, if the receive buffer is:
1443  *               The same length as the message, this field is zero.
1444  *               Longer than the message, this field contains the number of
1445  *                bytes remaining in the buffer.
1446  *               Shorter than the message, this field contains the residual
1447  *                count (that is, the number of bytes remaining in the
1448  *                message that does not fit into the buffer. In this case
1449  *                b2f0_result = 5.
1450  * Return: b2f0_result - return code from CP
1451  *         (-EINVAL) - buffer address is NULL
1452  */
1453 int
1454 iucv_receive_array (__u16 pathid,
1455                     __u32 msgid, __u32 trgcls,
1456                     iucv_array_t * buffer, ulong buflen,
1457                     int *flags1_out,
1458                     ulong * residual_buffer, ulong * residual_length)
1459 {
1460         iparml_db *parm;
1461         ulong b2f0_result;
1462         int i = 0, moved = 0, need_to_move = 8, dyn_len;
1463
1464         iucv_debug(2, "entering");
1465
1466         if (!buffer)
1467                 return -EINVAL;
1468
1469         parm = (iparml_db *)grab_param();
1470
1471         parm->ipbfadr1 = (__u32) ((ulong) buffer);
1472         parm->ipbfln1f = (__u32) buflen;
1473         parm->ipmsgid = msgid;
1474         parm->ippathid = pathid;
1475         parm->iptrgcls = trgcls;
1476         parm->ipflags1 = (IPBUFLST | IPFGPID | IPFGMID | IPFGMCL);
1477
1478         b2f0_result = b2f0(RECEIVE, parm);
1479
1480         if (!b2f0_result || b2f0_result == 5) {
1481
1482                 if (flags1_out) {
1483                         iucv_debug(2, "*flags1_out = %d", *flags1_out);
1484                         *flags1_out = (parm->ipflags1 & (~0x07));
1485                         iucv_debug(2, "*flags1_out = %d", *flags1_out);
1486                 }
1487
1488                 if (!(parm->ipflags1 & IPRMDATA)) {     /*msg not in parmlist */
1489
1490                         if (residual_length)
1491                                 *residual_length = parm->ipbfln1f;
1492
1493                         if (residual_buffer)
1494                                 *residual_buffer = parm->ipbfadr1;
1495
1496                 } else {
1497                         /* copy msg from parmlist to users array. */
1498
1499                         while ((moved < 8) && (moved < buflen)) {
1500                                 dyn_len =
1501                                     min_t (unsigned int,
1502                                          (buffer + i)->length, need_to_move);
1503
1504                                 memcpy ((char *)((ulong)((buffer + i)->address)),
1505                                         ((char *) &parm->ipbfadr1) + moved,
1506                                         dyn_len);
1507
1508                                 moved += dyn_len;
1509                                 need_to_move -= dyn_len;
1510
1511                                 (buffer + i)->address =
1512                                         (__u32)
1513                                 ((ulong)(__u8 *) ((ulong)(buffer + i)->address)
1514                                                 + dyn_len);
1515
1516                                 (buffer + i)->length -= dyn_len;
1517                                 i++;
1518                         }
1519
1520                         if (need_to_move)       /* buflen < 8 bytes */
1521                                 b2f0_result = 5;
1522
1523                         if (residual_length)
1524                                 *residual_length = abs (buflen - 8);
1525
1526                         if (residual_buffer) {
1527                                 if (!moved)
1528                                         *residual_buffer = (ulong) buffer;
1529                                 else
1530                                         *residual_buffer =
1531                                             (ulong) (buffer + (i - 1));
1532                         }
1533
1534                 }
1535         }
1536         release_param(parm);
1537
1538         iucv_debug(2, "exiting");
1539         return b2f0_result;
1540 }
1541
1542 /**
1543  * iucv_reject:
1544  * @pathid: Path identification number.
1545  * @msgid:  Message ID of the message to reject.
1546  * @trgcls: Target class of the message to reject.
1547  * Returns: return code from CP
1548  *
1549  * Refuses a specified message. Between the time you are notified of a
1550  * message and the time that you complete the message, the message may
1551  * be rejected.
1552  */
1553 int
1554 iucv_reject (__u16 pathid, __u32 msgid, __u32 trgcls)
1555 {
1556         iparml_db *parm;
1557         ulong b2f0_result = 0;
1558
1559         iucv_debug(1, "entering");
1560         iucv_debug(1, "pathid = %d", pathid);
1561
1562         parm = (iparml_db *)grab_param();
1563
1564         parm->ippathid = pathid;
1565         parm->ipmsgid = msgid;
1566         parm->iptrgcls = trgcls;
1567         parm->ipflags1 = (IPFGMCL | IPFGMID | IPFGPID);
1568
1569         b2f0_result = b2f0(REJECT, parm);
1570         release_param(parm);
1571
1572         iucv_debug(1, "b2f0_result = %ld", b2f0_result);
1573         iucv_debug(1, "exiting");
1574
1575         return b2f0_result;
1576 }
1577
1578 /*
1579  * Name: iucv_reply
1580  * Purpose: This function responds to the two-way messages that you
1581  *          receive. You must identify completely the message to
1582  *          which you wish to reply. ie, pathid, msgid, and trgcls.
1583  * Input: pathid - path identification number
1584  *        msgid - specifies the message ID.
1585  *        trgcls - specifies target class
1586  *        flags1 - option for path
1587  *                 IPPRTY- 0x20 - specifies if you want to send priority message
1588  *        buffer - address of reply buffer
1589  *        buflen - length of reply buffer
1590  * Output: ipbfadr2 - Address of buffer updated by the number
1591  *                    of bytes you have moved.
1592  *         ipbfln2f - Contains one of the following values:
1593  *              If the answer buffer is the same length as the reply, this field
1594  *               contains zero.
1595  *              If the answer buffer is longer than the reply, this field contains
1596  *               the number of bytes remaining in the buffer.
1597  *              If the answer buffer is shorter than the reply, this field contains
1598  *               a residual count (that is, the number of bytes remianing in the
1599  *               reply that does not fit into the buffer. In this
1600  *                case b2f0_result = 5.
1601  * Return: b2f0_result - return code from CP
1602  *         (-EINVAL) - buffer address is NULL
1603  */
1604 int
1605 iucv_reply (__u16 pathid,
1606             __u32 msgid, __u32 trgcls,
1607             int flags1,
1608             void *buffer, ulong buflen, ulong * ipbfadr2, ulong * ipbfln2f)
1609 {
1610         iparml_db *parm;
1611         ulong b2f0_result;
1612
1613         iucv_debug(2, "entering");
1614
1615         if (!buffer)
1616                 return -EINVAL;
1617
1618         parm = (iparml_db *)grab_param();
1619
1620         parm->ipbfadr2 = (__u32) ((ulong) buffer);
1621         parm->ipbfln2f = (__u32) buflen;        /* length of message */
1622         parm->ippathid = pathid;
1623         parm->ipmsgid = msgid;
1624         parm->iptrgcls = trgcls;
1625         parm->ipflags1 = (__u8) flags1; /* priority message */
1626
1627         b2f0_result = b2f0(REPLY, parm);
1628
1629         if ((!b2f0_result) || (b2f0_result == 5)) {
1630                 if (ipbfadr2)
1631                         *ipbfadr2 = parm->ipbfadr2;
1632                 if (ipbfln2f)
1633                         *ipbfln2f = parm->ipbfln2f;
1634         }
1635         release_param(parm);
1636
1637         iucv_debug(2, "exiting");
1638
1639         return b2f0_result;
1640 }
1641
1642 /*
1643  * Name: iucv_reply_array
1644  * Purpose: This function responds to the two-way messages that you
1645  *          receive. You must identify completely the message to
1646  *          which you wish to reply. ie, pathid, msgid, and trgcls.
1647  *          The array identifies a list of addresses and lengths of
1648  *          discontiguous buffers that contains the reply data.
1649  * Input: pathid - path identification number
1650  *        msgid - specifies the message ID.
1651  *        trgcls - specifies target class
1652  *        flags1 - option for path
1653  *                 IPPRTY- specifies if you want to send priority message
1654  *        buffer - address of array of reply buffers
1655  *        buflen - total length of reply buffers
1656  * Output: ipbfadr2 - Address of buffer which IUCV is currently working on.
1657  *         ipbfln2f - Contains one of the following values:
1658  *              If the answer buffer is the same length as the reply, this field
1659  *               contains zero.
1660  *              If the answer buffer is longer than the reply, this field contains
1661  *               the number of bytes remaining in the buffer.
1662  *              If the answer buffer is shorter than the reply, this field contains
1663  *               a residual count (that is, the number of bytes remianing in the
1664  *               reply that does not fit into the buffer. In this
1665  *               case b2f0_result = 5.
1666  * Return: b2f0_result - return code from CP
1667  *             (-EINVAL) - buffer address is NULL
1668 */
1669 int
1670 iucv_reply_array (__u16 pathid,
1671                   __u32 msgid, __u32 trgcls,
1672                   int flags1,
1673                   iucv_array_t * buffer,
1674                   ulong buflen, ulong * ipbfadr2, ulong * ipbfln2f)
1675 {
1676         iparml_db *parm;
1677         ulong b2f0_result;
1678
1679         iucv_debug(2, "entering");
1680
1681         if (!buffer)
1682                 return -EINVAL;
1683
1684         parm = (iparml_db *)grab_param();
1685
1686         parm->ipbfadr2 = (__u32) ((ulong) buffer);
1687         parm->ipbfln2f = buflen;        /* length of message */
1688         parm->ippathid = pathid;
1689         parm->ipmsgid = msgid;
1690         parm->iptrgcls = trgcls;
1691         parm->ipflags1 = (IPANSLST | flags1);
1692
1693         b2f0_result = b2f0(REPLY, parm);
1694
1695         if ((!b2f0_result) || (b2f0_result == 5)) {
1696
1697                 if (ipbfadr2)
1698                         *ipbfadr2 = parm->ipbfadr2;
1699                 if (ipbfln2f)
1700                         *ipbfln2f = parm->ipbfln2f;
1701         }
1702         release_param(parm);
1703
1704         iucv_debug(2, "exiting");
1705
1706         return b2f0_result;
1707 }
1708
1709 /*
1710  * Name: iucv_reply_prmmsg
1711  * Purpose: This function responds to the two-way messages that you
1712  *          receive. You must identify completely the message to
1713  *          which you wish to reply. ie, pathid, msgid, and trgcls.
1714  *          Prmmsg signifies the data is moved into the
1715  *          parameter list.
1716  * Input: pathid - path identification number
1717  *        msgid - specifies the message ID.
1718  *        trgcls - specifies target class
1719  *        flags1 - option for path
1720  *                 IPPRTY- specifies if you want to send priority message
1721  *        prmmsg - 8-bytes of data to be placed into the parameter
1722  *                 list.
1723  * Output: NA
1724  * Return: b2f0_result - return code from CP
1725 */
1726 int
1727 iucv_reply_prmmsg (__u16 pathid,
1728                    __u32 msgid, __u32 trgcls, int flags1, __u8 prmmsg[8])
1729 {
1730         iparml_dpl *parm;
1731         ulong b2f0_result;
1732
1733         iucv_debug(2, "entering");
1734
1735         parm = (iparml_dpl *)grab_param();
1736
1737         parm->ippathid = pathid;
1738         parm->ipmsgid = msgid;
1739         parm->iptrgcls = trgcls;
1740         memcpy(parm->iprmmsg, prmmsg, sizeof (parm->iprmmsg));
1741         parm->ipflags1 = (IPRMDATA | flags1);
1742
1743         b2f0_result = b2f0(REPLY, parm);
1744         release_param(parm);
1745
1746         iucv_debug(2, "exiting");
1747
1748         return b2f0_result;
1749 }
1750
1751 /**
1752  * iucv_resume:
1753  * @pathid:    Path identification number
1754  * @user_data: 16-byte of user data
1755  *
1756  * This function restores communication over a quiesced path.
1757  * Returns: return code from CP
1758  */
1759 int
1760 iucv_resume (__u16 pathid, __u8 user_data[16])
1761 {
1762         iparml_control *parm;
1763         ulong b2f0_result = 0;
1764
1765         iucv_debug(1, "entering");
1766         iucv_debug(1, "pathid = %d", pathid);
1767
1768         parm = (iparml_control *)grab_param();
1769
1770         memcpy (parm->ipuser, user_data, sizeof (*user_data));
1771         parm->ippathid = pathid;
1772
1773         b2f0_result = b2f0(RESUME, parm);
1774         release_param(parm);
1775
1776         iucv_debug(1, "exiting");
1777
1778         return b2f0_result;
1779 }
1780
1781 /*
1782  * Name: iucv_send
1783  * Purpose: sends messages
1784  * Input: pathid - ushort, pathid
1785  *        msgid  - ulong *, id of message returned to caller
1786  *        trgcls - ulong, target message class
1787  *        srccls - ulong, source message class
1788  *        msgtag - ulong, message tag
1789  *        flags1  - Contains options for this path.
1790  *              IPPRTY - Ox20 - specifies if you want to send a priority message.
1791  *        buffer - pointer to buffer
1792  *        buflen - ulong, length of buffer
1793  * Output: b2f0_result - return code from b2f0 call
1794  *         msgid - returns message id
1795  */
1796 int
1797 iucv_send (__u16 pathid, __u32 * msgid,
1798            __u32 trgcls, __u32 srccls,
1799            __u32 msgtag, int flags1, void *buffer, ulong buflen)
1800 {
1801         iparml_db *parm;
1802         ulong b2f0_result;
1803
1804         iucv_debug(2, "entering");
1805
1806         if (!buffer)
1807                 return -EINVAL;
1808
1809         parm = (iparml_db *)grab_param();
1810
1811         parm->ipbfadr1 = (__u32) ((ulong) buffer);
1812         parm->ippathid = pathid;
1813         parm->iptrgcls = trgcls;
1814         parm->ipbfln1f = (__u32) buflen;        /* length of message */
1815         parm->ipsrccls = srccls;
1816         parm->ipmsgtag = msgtag;
1817         parm->ipflags1 = (IPNORPY | flags1);    /* one way priority message */
1818
1819         b2f0_result = b2f0(SEND, parm);
1820
1821         if ((!b2f0_result) && (msgid))
1822                 *msgid = parm->ipmsgid;
1823         release_param(parm);
1824
1825         iucv_debug(2, "exiting");
1826
1827         return b2f0_result;
1828 }
1829
1830 /*
1831  * Name: iucv_send_array
1832  * Purpose: This function transmits data to another application.
1833  *          The contents of buffer is the address of the array of
1834  *          addresses and lengths of discontiguous buffers that hold
1835  *          the message text. This is a one-way message and the
1836  *          receiver will not reply to the message.
1837  * Input: pathid - path identification number
1838  *        trgcls - specifies target class
1839  *        srccls - specifies the source message class
1840  *        msgtag - specifies a tag to be associated witht the message
1841  *        flags1 - option for path
1842  *                 IPPRTY- specifies if you want to send priority message
1843  *        buffer - address of array of send buffers
1844  *        buflen - total length of send buffers
1845  * Output: msgid - specifies the message ID.
1846  * Return: b2f0_result - return code from CP
1847  *         (-EINVAL) - buffer address is NULL
1848  */
1849 int
1850 iucv_send_array (__u16 pathid,
1851                  __u32 * msgid,
1852                  __u32 trgcls,
1853                  __u32 srccls,
1854                  __u32 msgtag, int flags1, iucv_array_t * buffer, ulong buflen)
1855 {
1856         iparml_db *parm;
1857         ulong b2f0_result;
1858
1859         iucv_debug(2, "entering");
1860
1861         if (!buffer)
1862                 return -EINVAL;
1863
1864         parm = (iparml_db *)grab_param();
1865
1866         parm->ippathid = pathid;
1867         parm->iptrgcls = trgcls;
1868         parm->ipbfadr1 = (__u32) ((ulong) buffer);
1869         parm->ipbfln1f = (__u32) buflen;        /* length of message */
1870         parm->ipsrccls = srccls;
1871         parm->ipmsgtag = msgtag;
1872         parm->ipflags1 = (IPNORPY | IPBUFLST | flags1);
1873         b2f0_result = b2f0(SEND, parm);
1874
1875         if ((!b2f0_result) && (msgid))
1876                 *msgid = parm->ipmsgid;
1877         release_param(parm);
1878
1879         iucv_debug(2, "exiting");
1880         return b2f0_result;
1881 }
1882
1883 /*
1884  * Name: iucv_send_prmmsg
1885  * Purpose: This function transmits data to another application.
1886  *          Prmmsg specifies that the 8-bytes of data are to be moved
1887  *          into the parameter list. This is a one-way message and the
1888  *          receiver will not reply to the message.
1889  * Input: pathid - path identification number
1890  *        trgcls - specifies target class
1891  *        srccls - specifies the source message class
1892  *        msgtag - specifies a tag to be associated with the message
1893  *        flags1 - option for path
1894  *                 IPPRTY- specifies if you want to send priority message
1895  *        prmmsg - 8-bytes of data to be placed into parameter list
1896  * Output: msgid - specifies the message ID.
1897  * Return: b2f0_result - return code from CP
1898 */
1899 int
1900 iucv_send_prmmsg (__u16 pathid,
1901                   __u32 * msgid,
1902                   __u32 trgcls,
1903                   __u32 srccls, __u32 msgtag, int flags1, __u8 prmmsg[8])
1904 {
1905         iparml_dpl *parm;
1906         ulong b2f0_result;
1907
1908         iucv_debug(2, "entering");
1909
1910         parm = (iparml_dpl *)grab_param();
1911
1912         parm->ippathid = pathid;
1913         parm->iptrgcls = trgcls;
1914         parm->ipsrccls = srccls;
1915         parm->ipmsgtag = msgtag;
1916         parm->ipflags1 = (IPRMDATA | IPNORPY | flags1);
1917         memcpy(parm->iprmmsg, prmmsg, sizeof(parm->iprmmsg));
1918
1919         b2f0_result = b2f0(SEND, parm);
1920
1921         if ((!b2f0_result) && (msgid))
1922                 *msgid = parm->ipmsgid;
1923         release_param(parm);
1924
1925         iucv_debug(2, "exiting");
1926
1927         return b2f0_result;
1928 }
1929
1930 /*
1931  * Name: iucv_send2way
1932  * Purpose: This function transmits data to another application.
1933  *          Data to be transmitted is in a buffer. The receiver
1934  *          of the send is expected to reply to the message and
1935  *          a buffer is provided into which IUCV moves the reply
1936  *          to this message.
1937  * Input: pathid - path identification number
1938  *        trgcls - specifies target class
1939  *        srccls - specifies the source message class
1940  *        msgtag - specifies a tag associated with the message
1941  *        flags1 - option for path
1942  *                 IPPRTY- specifies if you want to send priority message
1943  *        buffer - address of send buffer
1944  *        buflen - length of send buffer
1945  *        ansbuf - address of buffer to reply with
1946  *        anslen - length of buffer to reply with
1947  * Output: msgid - specifies the message ID.
1948  * Return: b2f0_result - return code from CP
1949  *         (-EINVAL) - buffer or ansbuf address is NULL
1950  */
1951 int
1952 iucv_send2way (__u16 pathid,
1953                __u32 * msgid,
1954                __u32 trgcls,
1955                __u32 srccls,
1956                __u32 msgtag,
1957                int flags1,
1958                void *buffer, ulong buflen, void *ansbuf, ulong anslen)
1959 {
1960         iparml_db *parm;
1961         ulong b2f0_result;
1962
1963         iucv_debug(2, "entering");
1964
1965         if (!buffer || !ansbuf)
1966                 return -EINVAL;
1967
1968         parm = (iparml_db *)grab_param();
1969
1970         parm->ippathid = pathid;
1971         parm->iptrgcls = trgcls;
1972         parm->ipbfadr1 = (__u32) ((ulong) buffer);
1973         parm->ipbfln1f = (__u32) buflen;        /* length of message */
1974         parm->ipbfadr2 = (__u32) ((ulong) ansbuf);
1975         parm->ipbfln2f = (__u32) anslen;
1976         parm->ipsrccls = srccls;
1977         parm->ipmsgtag = msgtag;
1978         parm->ipflags1 = flags1;        /* priority message */
1979
1980         b2f0_result = b2f0(SEND, parm);
1981
1982         if ((!b2f0_result) && (msgid))
1983                 *msgid = parm->ipmsgid;
1984         release_param(parm);
1985
1986         iucv_debug(2, "exiting");
1987
1988         return b2f0_result;
1989 }
1990
1991 /*
1992  * Name: iucv_send2way_array
1993  * Purpose: This function transmits data to another application.
1994  *          The contents of buffer is the address of the array of
1995  *          addresses and lengths of discontiguous buffers that hold
1996  *          the message text. The receiver of the send is expected to
1997  *          reply to the message and a buffer is provided into which
1998  *          IUCV moves the reply to this message.
1999  * Input: pathid - path identification number
2000  *        trgcls - specifies target class
2001  *        srccls - specifies the source message class
2002  *        msgtag - spcifies a tag to be associated with the message
2003  *        flags1 - option for path
2004  *                 IPPRTY- specifies if you want to send priority message
2005  *        buffer - address of array of send buffers
2006  *        buflen - total length of send buffers
2007  *        ansbuf - address of buffer to reply with
2008  *        anslen - length of buffer to reply with
2009  * Output: msgid - specifies the message ID.
2010  * Return: b2f0_result - return code from CP
2011  *         (-EINVAL) - buffer address is NULL
2012  */
2013 int
2014 iucv_send2way_array (__u16 pathid,
2015                      __u32 * msgid,
2016                      __u32 trgcls,
2017                      __u32 srccls,
2018                      __u32 msgtag,
2019                      int flags1,
2020                      iucv_array_t * buffer,
2021                      ulong buflen, iucv_array_t * ansbuf, ulong anslen)
2022 {
2023         iparml_db *parm;
2024         ulong b2f0_result;
2025
2026         iucv_debug(2, "entering");
2027
2028         if (!buffer || !ansbuf)
2029                 return -EINVAL;
2030
2031         parm = (iparml_db *)grab_param();
2032
2033         parm->ippathid = pathid;
2034         parm->iptrgcls = trgcls;
2035         parm->ipbfadr1 = (__u32) ((ulong) buffer);
2036         parm->ipbfln1f = (__u32) buflen;        /* length of message */
2037         parm->ipbfadr2 = (__u32) ((ulong) ansbuf);
2038         parm->ipbfln2f = (__u32) anslen;
2039         parm->ipsrccls = srccls;
2040         parm->ipmsgtag = msgtag;
2041         parm->ipflags1 = (IPBUFLST | IPANSLST | flags1);
2042         b2f0_result = b2f0(SEND, parm);
2043         if ((!b2f0_result) && (msgid))
2044                 *msgid = parm->ipmsgid;
2045         release_param(parm);
2046
2047         iucv_debug(2, "exiting");
2048         return b2f0_result;
2049 }
2050
2051 /*
2052  * Name: iucv_send2way_prmmsg
2053  * Purpose: This function transmits data to another application.
2054  *          Prmmsg specifies that the 8-bytes of data are to be moved
2055  *          into the parameter list. This is a two-way message and the
2056  *          receiver of the message is expected to reply. A buffer
2057  *          is provided into which IUCV moves the reply to this
2058  *          message.
2059  * Input: pathid - path identification number
2060  *        trgcls - specifies target class
2061  *        srccls - specifies the source message class
2062  *        msgtag - specifies a tag to be associated with the message
2063  *        flags1 - option for path
2064  *                 IPPRTY- specifies if you want to send priority message
2065  *        prmmsg - 8-bytes of data to be placed in parameter list
2066  *        ansbuf - address of buffer to reply with
2067  *        anslen - length of buffer to reply with
2068  * Output: msgid - specifies the message ID.
2069  * Return: b2f0_result - return code from CP
2070  *         (-EINVAL) - buffer address is NULL
2071 */
2072 int
2073 iucv_send2way_prmmsg (__u16 pathid,
2074                       __u32 * msgid,
2075                       __u32 trgcls,
2076                       __u32 srccls,
2077                       __u32 msgtag,
2078                       ulong flags1, __u8 prmmsg[8], void *ansbuf, ulong anslen)
2079 {
2080         iparml_dpl *parm;
2081         ulong b2f0_result;
2082
2083         iucv_debug(2, "entering");
2084
2085         if (!ansbuf)
2086                 return -EINVAL;
2087
2088         parm = (iparml_dpl *)grab_param();
2089
2090         parm->ippathid = pathid;
2091         parm->iptrgcls = trgcls;
2092         parm->ipsrccls = srccls;
2093         parm->ipmsgtag = msgtag;
2094         parm->ipbfadr2 = (__u32) ((ulong) ansbuf);
2095         parm->ipbfln2f = (__u32) anslen;
2096         parm->ipflags1 = (IPRMDATA | flags1);   /* message in prmlist */
2097         memcpy(parm->iprmmsg, prmmsg, sizeof(parm->iprmmsg));
2098
2099         b2f0_result = b2f0(SEND, parm);
2100
2101         if ((!b2f0_result) && (msgid))
2102                 *msgid = parm->ipmsgid;
2103         release_param(parm);
2104
2105         iucv_debug(2, "exiting");
2106
2107         return b2f0_result;
2108 }
2109
2110 /*
2111  * Name: iucv_send2way_prmmsg_array
2112  * Purpose: This function transmits data to another application.
2113  *          Prmmsg specifies that the 8-bytes of data are to be moved
2114  *          into the parameter list. This is a two-way message and the
2115  *          receiver of the message is expected to reply. A buffer
2116  *          is provided into which IUCV moves the reply to this
2117  *          message. The contents of ansbuf is the address of the
2118  *          array of addresses and lengths of discontiguous buffers
2119  *          that contain the reply.
2120  * Input: pathid - path identification number
2121  *        trgcls - specifies target class
2122  *        srccls - specifies the source message class
2123  *        msgtag - specifies a tag to be associated with the message
2124  *        flags1 - option for path
2125  *                 IPPRTY- specifies if you want to send priority message
2126  *        prmmsg - 8-bytes of data to be placed into the parameter list
2127  *        ansbuf - address of buffer to reply with
2128  *        anslen - length of buffer to reply with
2129  * Output: msgid - specifies the message ID.
2130  * Return: b2f0_result - return code from CP
2131  *         (-EINVAL) - ansbuf address is NULL
2132  */
2133 int
2134 iucv_send2way_prmmsg_array (__u16 pathid,
2135                             __u32 * msgid,
2136                             __u32 trgcls,
2137                             __u32 srccls,
2138                             __u32 msgtag,
2139                             int flags1,
2140                             __u8 prmmsg[8],
2141                             iucv_array_t * ansbuf, ulong anslen)
2142 {
2143         iparml_dpl *parm;
2144         ulong b2f0_result;
2145
2146         iucv_debug(2, "entering");
2147
2148         if (!ansbuf)
2149                 return -EINVAL;
2150
2151         parm = (iparml_dpl *)grab_param();
2152
2153         parm->ippathid = pathid;
2154         parm->iptrgcls = trgcls;
2155         parm->ipsrccls = srccls;
2156         parm->ipmsgtag = msgtag;
2157         parm->ipbfadr2 = (__u32) ((ulong) ansbuf);
2158         parm->ipbfln2f = (__u32) anslen;
2159         parm->ipflags1 = (IPRMDATA | IPANSLST | flags1);
2160         memcpy(parm->iprmmsg, prmmsg, sizeof(parm->iprmmsg));
2161         b2f0_result = b2f0(SEND, parm);
2162         if ((!b2f0_result) && (msgid))
2163                 *msgid = parm->ipmsgid;
2164         release_param(parm);
2165
2166         iucv_debug(2, "exiting");
2167         return b2f0_result;
2168 }
2169
2170 void
2171 iucv_setmask_cpuid (void *result)
2172 {
2173         iparml_set_mask *parm;
2174
2175         iucv_debug(1, "entering");
2176         parm = (iparml_set_mask *)grab_param();
2177         parm->ipmask = *((__u8*)result);
2178         *((ulong *)result) = b2f0(SETMASK, parm);
2179         release_param(parm);
2180
2181         iucv_debug(1, "b2f0_result = %ld", *((ulong *)result));
2182         iucv_debug(1, "exiting");
2183 }
2184
2185 /*
2186  * Name: iucv_setmask
2187  * Purpose: This function enables or disables the following IUCV
2188  *          external interruptions: Nonpriority and priority message
2189  *          interrupts, nonpriority and priority reply interrupts.
2190  * Input: SetMaskFlag - options for interrupts
2191  *           0x80 - Nonpriority_MessagePendingInterruptsFlag
2192  *           0x40 - Priority_MessagePendingInterruptsFlag
2193  *           0x20 - Nonpriority_MessageCompletionInterruptsFlag
2194  *           0x10 - Priority_MessageCompletionInterruptsFlag
2195  *           0x08 - IUCVControlInterruptsFlag
2196  * Output: NA
2197  * Return: b2f0_result - return code from CP
2198 */
2199 int
2200 iucv_setmask (int SetMaskFlag)
2201 {
2202         union {
2203                 ulong result;
2204                 __u8  param;
2205         } u;
2206         int cpu;
2207
2208         u.param = SetMaskFlag;
2209         cpu = get_cpu();
2210         smp_call_function_on(iucv_setmask_cpuid, &u, 0, 1, iucv_cpuid);
2211         put_cpu();
2212
2213         return u.result;
2214 }
2215
2216 /**
2217  * iucv_sever:
2218  * @pathid:    Path identification number
2219  * @user_data: 16-byte of user data
2220  *
2221  * This function terminates an iucv path.
2222  * Returns: return code from CP
2223  */
2224 int
2225 iucv_sever(__u16 pathid, __u8 user_data[16])
2226 {
2227         iparml_control *parm;
2228         ulong b2f0_result = 0;
2229
2230         iucv_debug(1, "entering");
2231         parm = (iparml_control *)grab_param();
2232
2233         memcpy(parm->ipuser, user_data, sizeof(parm->ipuser));
2234         parm->ippathid = pathid;
2235
2236         b2f0_result = b2f0(SEVER, parm);
2237
2238         if (!b2f0_result)
2239                 iucv_remove_pathid(pathid);
2240         release_param(parm);
2241
2242         iucv_debug(1, "exiting");
2243         return b2f0_result;
2244 }
2245
2246 /*
2247  * Interrupt Handlers
2248  *******************************************************************************/
2249
2250 /**
2251  * iucv_irq_handler:
2252  * @regs: Current registers
2253  * @code: irq code
2254  *
2255  * Handles external interrupts coming in from CP.
2256  * Places the interrupt buffer on a queue and schedules iucv_tasklet_handler().
2257  */
2258 static void
2259 iucv_irq_handler(struct pt_regs *regs, __u16 code)
2260 {
2261         iucv_irqdata *irqdata;
2262
2263         irqdata = kmalloc(sizeof(iucv_irqdata), GFP_ATOMIC);
2264         if (!irqdata) {
2265                 printk(KERN_WARNING "%s: out of memory\n", __FUNCTION__);
2266                 return;
2267         }
2268
2269         memcpy(&irqdata->data, iucv_external_int_buffer,
2270                sizeof(iucv_GeneralInterrupt));
2271
2272         spin_lock(&iucv_irq_queue_lock);
2273         list_add_tail(&irqdata->queue, &iucv_irq_queue);
2274         spin_unlock(&iucv_irq_queue_lock);
2275
2276         tasklet_schedule(&iucv_tasklet);
2277 }
2278
2279 /**
2280  * iucv_do_int:
2281  * @int_buf: Pointer to copy of external interrupt buffer
2282  *
2283  * The workhorse for handling interrupts queued by iucv_irq_handler().
2284  * This function is called from the bottom half iucv_tasklet_handler().
2285  */
2286 static void
2287 iucv_do_int(iucv_GeneralInterrupt * int_buf)
2288 {
2289         handler *h = NULL;
2290         struct list_head *lh;
2291         ulong flags;
2292         iucv_interrupt_ops_t *interrupt = NULL; /* interrupt addresses */
2293         __u8 temp_buff1[24], temp_buff2[24];    /* masked handler id. */
2294         int rc = 0, j = 0;
2295         __u8 no_listener[16] = "NO LISTENER";
2296
2297         iucv_debug(2, "entering, pathid %d, type %02X",
2298                  int_buf->ippathid, int_buf->iptype);
2299         iucv_dumpit("External Interrupt Buffer:",
2300                     int_buf, sizeof(iucv_GeneralInterrupt));
2301
2302         ASCEBC (no_listener, 16);
2303
2304         if (int_buf->iptype != 01) {
2305                 if ((int_buf->ippathid) > (max_connections - 1)) {
2306                         printk(KERN_WARNING "%s: Got interrupt with pathid %d"
2307                                " > max_connections (%ld)\n", __FUNCTION__,
2308                                int_buf->ippathid, max_connections - 1);
2309                 } else {
2310                         h = iucv_pathid_table[int_buf->ippathid];
2311                         interrupt = h->interrupt_table;
2312                         iucv_dumpit("Handler:", h, sizeof(handler));
2313                 }
2314         }
2315
2316         /* end of if statement */
2317         switch (int_buf->iptype) {
2318                 case 0x01:              /* connection pending */
2319                         if (messagesDisabled) {
2320                             iucv_setmask(~0);
2321                             messagesDisabled = 0;
2322                         }
2323                         spin_lock_irqsave(&iucv_lock, flags);
2324                         list_for_each(lh, &iucv_handler_table) {
2325                                 h = list_entry(lh, handler, list);
2326                                 memcpy(temp_buff1, &(int_buf->ipvmid), 24);
2327                                 memcpy(temp_buff2, &(h->id.userid), 24);
2328                                 for (j = 0; j < 24; j++) {
2329                                         temp_buff1[j] &= (h->id.mask)[j];
2330                                         temp_buff2[j] &= (h->id.mask)[j];
2331                                 }
2332
2333                                 iucv_dumpit("temp_buff1:",
2334                                             temp_buff1, sizeof(temp_buff1));
2335                                 iucv_dumpit("temp_buff2",
2336                                             temp_buff2, sizeof(temp_buff2));
2337
2338                                 if (!memcmp (temp_buff1, temp_buff2, 24)) {
2339
2340                                         iucv_debug(2,
2341                                                    "found a matching handler");
2342                                         break;
2343                                 } else
2344                                         h = NULL;
2345                         }
2346                         spin_unlock_irqrestore (&iucv_lock, flags);
2347                         if (h) {
2348                                 /* ADD PATH TO PATHID TABLE */
2349                                 rc = iucv_add_pathid(int_buf->ippathid, h);
2350                                 if (rc) {
2351                                         iucv_sever (int_buf->ippathid,
2352                                                     no_listener);
2353                                         iucv_debug(1,
2354                                                    "add_pathid failed, rc = %d",
2355                                                    rc);
2356                                 } else {
2357                                         interrupt = h->interrupt_table;
2358                                         if (interrupt->ConnectionPending) {
2359                                                 EBCASC (int_buf->ipvmid, 8);
2360                                                 interrupt->ConnectionPending(
2361                                                         (iucv_ConnectionPending *)int_buf,
2362                                                         h->pgm_data);
2363                                         } else
2364                                                 iucv_sever(int_buf->ippathid,
2365                                                            no_listener);
2366                                 }
2367                         } else
2368                                 iucv_sever(int_buf->ippathid, no_listener);
2369                         break;
2370
2371                 case 0x02:              /*connection complete */
2372                         if (messagesDisabled) {
2373                             iucv_setmask(~0);
2374                             messagesDisabled = 0;
2375                         }
2376                         if (h) {
2377                                 if (interrupt->ConnectionComplete)
2378                                 {
2379                                         interrupt->ConnectionComplete(
2380                                                 (iucv_ConnectionComplete *)int_buf,
2381                                                 h->pgm_data);
2382                                 }
2383                                 else
2384                                         iucv_debug(1,
2385                                                    "ConnectionComplete not called");
2386                         } else
2387                                 iucv_sever(int_buf->ippathid, no_listener);
2388                         break;
2389
2390                 case 0x03:              /* connection severed */
2391                         if (messagesDisabled) {
2392                             iucv_setmask(~0);
2393                             messagesDisabled = 0;
2394                         }
2395                         if (h) {
2396                                 if (interrupt->ConnectionSevered)
2397                                         interrupt->ConnectionSevered(
2398                                                 (iucv_ConnectionSevered *)int_buf,
2399                                                 h->pgm_data);
2400
2401                                 else
2402                                         iucv_sever (int_buf->ippathid, no_listener);
2403                         } else
2404                                 iucv_sever(int_buf->ippathid, no_listener);
2405                         break;
2406
2407                 case 0x04:              /* connection quiesced */
2408                         if (messagesDisabled) {
2409                             iucv_setmask(~0);
2410                             messagesDisabled = 0;
2411                         }
2412                         if (h) {
2413                                 if (interrupt->ConnectionQuiesced)
2414                                         interrupt->ConnectionQuiesced(
2415                                                 (iucv_ConnectionQuiesced *)int_buf,
2416                                                 h->pgm_data);
2417                                 else
2418                                         iucv_debug(1,
2419                                                    "ConnectionQuiesced not called");
2420                         }
2421                         break;
2422
2423                 case 0x05:              /* connection resumed */
2424                         if (messagesDisabled) {
2425                             iucv_setmask(~0);
2426                             messagesDisabled = 0;
2427                         }
2428                         if (h) {
2429                                 if (interrupt->ConnectionResumed)
2430                                         interrupt->ConnectionResumed(
2431                                                 (iucv_ConnectionResumed *)int_buf,
2432                                                 h->pgm_data);
2433                                 else
2434                                         iucv_debug(1,
2435                                                    "ConnectionResumed not called");
2436                         }
2437                         break;
2438
2439                 case 0x06:              /* priority message complete */
2440                 case 0x07:              /* nonpriority message complete */
2441                         if (h) {
2442                                 if (interrupt->MessageComplete)
2443                                         interrupt->MessageComplete(
2444                                                 (iucv_MessageComplete *)int_buf,
2445                                                 h->pgm_data);
2446                                 else
2447                                         iucv_debug(2,
2448                                                    "MessageComplete not called");
2449                         }
2450                         break;
2451
2452                 case 0x08:              /* priority message pending  */
2453                 case 0x09:              /* nonpriority message pending  */
2454                         if (h) {
2455                                 if (interrupt->MessagePending)
2456                                         interrupt->MessagePending(
2457                                                 (iucv_MessagePending *) int_buf,
2458                                                 h->pgm_data);
2459                                 else
2460                                         iucv_debug(2,
2461                                                    "MessagePending not called");
2462                         }
2463                         break;
2464                 default:                /* unknown iucv type */
2465                         printk(KERN_WARNING "%s: unknown iucv interrupt\n",
2466                                __FUNCTION__);
2467                         break;
2468         }                       /* end switch */
2469
2470         iucv_debug(2, "exiting pathid %d, type %02X",
2471                  int_buf->ippathid, int_buf->iptype);
2472
2473         return;
2474 }
2475
2476 /**
2477  * iucv_tasklet_handler:
2478  *
2479  * This function loops over the queue of irq buffers and runs iucv_do_int()
2480  * on every queue element.
2481  */
2482 static void
2483 iucv_tasklet_handler(unsigned long ignored)
2484 {
2485         struct list_head head;
2486         struct list_head *next;
2487         ulong  flags;
2488
2489         spin_lock_irqsave(&iucv_irq_queue_lock, flags);
2490         list_add(&head, &iucv_irq_queue);
2491         list_del_init(&iucv_irq_queue);
2492         spin_unlock_irqrestore (&iucv_irq_queue_lock, flags);
2493
2494         next = head.next;
2495         while (next != &head) {
2496                 iucv_irqdata *p = list_entry(next, iucv_irqdata, queue);
2497
2498                 next = next->next;
2499                 iucv_do_int(&p->data);
2500                 kfree(p);
2501         }
2502
2503         return;
2504 }
2505
2506 subsys_initcall(iucv_init);
2507 module_exit(iucv_exit);
2508
2509 /**
2510  * Export all public stuff
2511  */
2512 EXPORT_SYMBOL (iucv_bus);
2513 EXPORT_SYMBOL (iucv_root);
2514 EXPORT_SYMBOL (iucv_accept);
2515 EXPORT_SYMBOL (iucv_connect);
2516 #if 0
2517 EXPORT_SYMBOL (iucv_purge);
2518 EXPORT_SYMBOL (iucv_query_maxconn);
2519 EXPORT_SYMBOL (iucv_query_bufsize);
2520 EXPORT_SYMBOL (iucv_quiesce);
2521 #endif
2522 EXPORT_SYMBOL (iucv_receive);
2523 #if 0
2524 EXPORT_SYMBOL (iucv_receive_array);
2525 #endif
2526 EXPORT_SYMBOL (iucv_reject);
2527 #if 0
2528 EXPORT_SYMBOL (iucv_reply);
2529 EXPORT_SYMBOL (iucv_reply_array);
2530 EXPORT_SYMBOL (iucv_resume);
2531 #endif
2532 EXPORT_SYMBOL (iucv_reply_prmmsg);
2533 EXPORT_SYMBOL (iucv_send);
2534 EXPORT_SYMBOL (iucv_send2way);
2535 EXPORT_SYMBOL (iucv_send2way_array);
2536 EXPORT_SYMBOL (iucv_send2way_prmmsg);
2537 EXPORT_SYMBOL (iucv_send2way_prmmsg_array);
2538 #if 0
2539 EXPORT_SYMBOL (iucv_send_array);
2540 EXPORT_SYMBOL (iucv_send_prmmsg);
2541 EXPORT_SYMBOL (iucv_setmask);
2542 #endif
2543 EXPORT_SYMBOL (iucv_sever);
2544 EXPORT_SYMBOL (iucv_register_program);
2545 EXPORT_SYMBOL (iucv_unregister_program);