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