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