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