File: /usr/src/linux/drivers/s390/net/netiucv.c

1     /*
2      *  drivers/s390/net/netiucv.c
3      *    Network driver for VM using iucv
4      *
5      *  S/390 version
6      *    Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
7      *    Author(s): Stefan Hegewald <hegewald@de.ibm.com>
8      *               Hartmut Penner <hpenner@de.ibm.com>
9      *
10      *
11      *    2.3 Updates Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
12      *                Martin Schwidefsky (schwidefsky@de.ibm.com)
13      *
14      *    Re-write:   Alan Altmark (Alan_Altmark@us.ibm.com)  Sept. 2000
15      *                Uses iucv.c kernel module for IUCV services. 
16      *
17      *    2.4 Updates Alan Altmark (Alan_Altmark@us.ibm.com)  June 2001
18      *                Update to use changed IUCV (iucv.c) interface.
19      *
20      * -------------------------------------------------------------------------- 
21      *  An IUCV frame consists of one or more packets preceded by a 16-bit
22      *  header.   The header contains the offset to the next packet header,
23      *  measured from the beginning of the _frame_.  If zero, there are no more
24      *  packets in the frame.  Consider a frame which contains a 10-byte packet
25      *  followed by a 20-byte packet:
26      *        +-----+----------------+--------------------------------+-----+
27      *        |h'12'| 10-byte packet |h'34'|  20-byte packet          |h'00'|
28      *        +-----+----------------+-----+--------------------------+-----+
29      * Offset: 0     2                12    14                         34  
30      *
31      *  This means that each header will always have a larger value than the
32      *  previous one (except for the final zero header, of course).
33      *  
34      *  For outbound packets, we send ONE frame per packet.  So, our frame is:
35      *       AL2(packet length+2), packet, AL2(0)
36      *  The maximum packet size is the MTU, so the maximum IUCV frame we send
37      *  is MTU+4 bytes.
38      *
39      *  For inbound frames, we don't care how long the frame is.  We tear apart
40      *  the frame, processing packets up to MTU size in length, until no more
41      *  packets remain in the frame.
42      *
43      * --------------------------------------------------------------------------
44      *  The code uses the 2.3.43 network driver interfaces.  If compiled on an
45      *  an older level of the kernel, the module provides its own macros.
46      *  Doc is in Linux Weekly News (lwn.net) memo from David Miller, 9 Feb 2000.
47      *  There are a few other places with 2.3-specific enhancements.
48      *
49      * --------------------------------------------------------------------------
50     */
51     //#define DEBUG 1
52     //#define DEBUG2 1
53     //#define IPDEBUG 1
54     #define LEVEL "1.1"
55     
56     /* If MAX_DEVICES increased, add initialization data to iucv_netdev[] array */
57     /* (See bottom of program.)						    */
58     #define MAX_DEVICES 20		/* Allows "iucv0" to "iucv19"   */
59     #define MAX_VM_MTU 32764	/* 32K IUCV buffer, minus 4     */
60     #define MAX_TX_Q 50		/* Maximum pending TX           */
61     
62     #include <linux/version.h>
63     #include <linux/kernel.h>
64     
65     #ifdef MODULE
66     #include <linux/module.h>
67     MODULE_AUTHOR
68         ("(C) 2000 IBM Corporation by Alan Altmark (Alan_Altmark@us.ibm.com)");
69     MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver " LEVEL);
70     MODULE_PARM (iucv, "1-" __MODULE_STRING (MAX_DEVICES) "s");
71     MODULE_PARM_DESC (iucv,
72     		  "Specify the userids associated with iucv0-iucv9:\n"
73     		  "iucv=userid1,userid2,...,userid10\n");
74     #ifdef MODVERSIONS
75     #include <linux/modversions.h>
76     #endif
77     #else
78     #define MOD_INC_USE_COUNT
79     #define MOD_DEC_USE_COUNT
80     #endif
81     
82     #include <linux/sched.h>	/* task queues                  */
83     #include <linux/malloc.h>	/* kmalloc()                    */
84     #include <linux/errno.h>	/* error codes                  */
85     #include <linux/types.h>	/* size_t                       */
86     #include <linux/interrupt.h>	/* mark_bh                      */
87     #include <linux/netdevice.h>	/* struct net_device, etc.      */
88     #include <linux/if_arp.h>	/* ARPHRD_SLIP                  */
89     #include <linux/ip.h>		/* IP header                    */
90     #include <linux/skbuff.h>	/* skb                          */
91     #include <linux/init.h>		/* __setup()                    */
92     #include <asm/string.h>		/* memset, memcpy, etc.         */
93     #include "iucv.h"
94     
95     #if defined( DEBUG )
96     #undef KERN_INFO
97     #undef KERN_DEBUG
98     #undef KERN_NOTICE
99     #undef KERN_ERR
100     #define KERN_INFO    KERN_EMERG
101     #define KERN_DEBUG   KERN_EMERG
102     #define KERN_NOTICE  KERN_EMERG
103     #define KERN_ERR     KERN_EMERG
104     #endif
105     
106     #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
107     typedef struct net_device net_device;
108     #else
109     typedef struct device net_device;
110     #endif
111     
112     static __inline__ int
113     netif_is_busy (net_device * dev)
114     {
115     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,45))
116     	return (dev->tbusy);
117     #else
118     	return (test_bit (__LINK_STATE_XOFF, &dev->flags));
119     #endif
120     }
121     
122     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,45))
123     	/* Provide our own 2.3.45 interfaces */
124     #define netif_enter_interrupt(dev) dev->interrupt=1
125     #define netif_exit_interrupt(dev) dev->interrupt=0
126     #define netif_start(dev) dev->start=1
127     #define netif_stop(dev) dev->start=0
128     
129     static __inline__ void
130     netif_stop_queue (net_device * dev)
131     {
132     	dev->tbusy = 1;
133     }
134     
135     static __inline__ void
136     netif_start_queue (net_device * dev)
137     {
138     	dev->tbusy = 0;
139     }
140     
141     static __inline__ void
142     netif_wake_queue (net_device * dev)
143     {
144     	dev->tbusy = 0;
145     	mark_bh (NET_BH);
146     }
147     
148     #else
149     	/* As of 2.3.45, we don't do these things anymore */
150     #define netif_enter_interrupt(dev)
151     #define netif_exit_interrupt(dev)
152     #define netif_start(dev)
153     #define netif_stop(dev)
154     #endif
155     
156     static int iucv_start (net_device *);
157     static int iucv_stop (net_device *);
158     static int iucv_change_mtu (net_device *, int);
159     static int iucv_init (net_device *);
160     static void iucv_rx (net_device *, u32, uchar *, int);
161     static int iucv_tx (struct sk_buff *, net_device *);
162     
163     static void connection_severed (iucv_ConnectionSevered *, void *);
164     static void connection_pending (iucv_ConnectionPending *, void *);
165     static void connection_complete (iucv_ConnectionComplete *, void *);
166     static void message_pending (iucv_MessagePending *, void *);
167     static void send_complete (iucv_MessageComplete *, void *);
168     
169     void register_iucv_dev (int, char *);
170     
171     static iucv_interrupt_ops_t netiucv_ops = {
172     	&connection_pending,
173     	&connection_complete,
174     	&connection_severed,
175     	NULL,			/* Quiesced             */
176     	NULL,			/* Resumed              */
177     	&message_pending,	/* Message pending      */
178     	&send_complete		/* Message complete     */
179     };
180     
181     static char iucv_userid[MAX_DEVICES][8];
182     net_device iucv_netdev[MAX_DEVICES];
183     
184     /* This structure is private to each device. It contains the    */
185     /* information necessary to do IUCV operations.                 */
186     struct iucv_priv {
187     	struct net_device_stats stats;
188     	net_device *dev;
189     	iucv_handle_t handle;
190     	uchar userid[9];	/* Printable userid */
191     	uchar userid2[8];	/* Used for IUCV operations */
192     
193     	/* Note: atomic_compare_and_swap() return value is backwards */
194     	/*       from what you might think: FALSE=0=OK, TRUE=1=FAIL  */
195     	atomic_t state;
196     #define FREE 0
197     #define CONNECTING 1
198     #define CONNECTED 2
199     	u16 pathid;
200     };
201     
202     uchar iucv_host[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
203     uchar iucvMagic[16] = { 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
204     	0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
205     };
206     
207     /* This mask means the 16-byte IUCV "magic" and the origin userid must */
208     /* match exactly as specified in order to give connection_pending()    */
209     /* control. 							       */
210     const char mask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
211     	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
212     	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
213     };
214     
215     #if defined( DEBUG2 ) || defined( IPDEBUG )
216     /*--------------------------*/
217     /* Dump buffer formatted    */
218     /*--------------------------*/
219     static void
220     dumpit (char *buf, int len)
221     {
222     	int i;
223     	printk (KERN_DEBUG);
224     	for (i = 0; i < len; i++) {
225     		if (!(i % 32) && i != 0)
226     			printk ("\n");
227     		else if (!(i % 4) && i != 0)
228     			printk (" ");
229     		printk ("%02X", buf[i]);
230     	}
231     	if (len % 32)
232     		printk ("\n");
233     }
234     #endif
235     
236     /*-----------------------------------------------------------------*/
237     /* Open a connection to another Linux or VM TCP/IP stack.          */
238     /* Called by kernel.						   */
239     /*                                                                 */
240     /* 1. Register a handler. (Up to now, any attempt by another stack */
241     /*    has been rejected by the IUCV handler.)  We give the handler */
242     /*    the net_device* so that we can locate the dev associated     */
243     /*    with the partner userid if he tries to connect to us or      */
244     /*    if the connection is broken.                                 */
245     /*                                                                 */
246     /* 2. Connect to remote stack.  If we get a connection pending     */
247     /*    interrupt while we're in the middle of connecting, don't     */
248     /*    worry.  VM will sever its and use ours, because the DEVICE   */
249     /*    is defined to be:                                            */
250     /*           DEVICE devname IUCV 0 0 linuxvm A                     */
251     /*        or DEVICE devname IUCV 0 0 linuxvm B                     */
252     /*    In EBCDIC, "0" (0xF0) is greater than "A" (0xC1) or "B", so  */
253     /*    win all races.  We will sever any connects that occur while  */
254     /*    we are connecting.  The "0 0" is where we get iucvMagic from.*/
255     /*								   */
256     /*    FIXME: If two Linux machines get into this race condition,   */
257     /*           both will sever.  Manual intervention required.       */
258     /*           Need a better IUCV "hello"-like function that permits */
259     /*           some negotiation.  But can't do that until VM TCP/IP  */
260     /*           would support it.                                     */
261     /*                                                                 */
262     /* 3. Return 0 to indicate device ok.  Anything else is an error.  */
263     /*-----------------------------------------------------------------*/
264     static int
265     iucv_start (net_device * dev)
266     {
267     	int rc, i;
268     	struct iucv_priv *p = (struct iucv_priv *) dev->priv;
269     
270     	pr_debug ("iucv_start(%s)\n", dev->name);
271     
272     	if (p == NULL) {
273     		/* Allocate priv data */
274     		p = (struct iucv_priv *) kmalloc (sizeof (struct iucv_priv),
275     						  GFP_ATOMIC);
276     		if (p == NULL) {
277     			printk (KERN_CRIT "%s: no memory for dev->priv.\n",
278     				dev->name);
279     			return -ENOMEM;
280     		}
281     		memset (p, 0, sizeof (struct iucv_priv));
282     		dev->priv = p;
283     		p->dev = dev;
284     
285     		memcpy (p->userid, iucv_userid[dev - iucv_netdev], 8);	/* Save userid */
286     		memcpy (p->userid2, p->userid, 8);	/* Again, with feeling.  */
287     
288     		for (i = 0; i < 8; i++) {	/* Change userid to printable form */
289     			if (p->userid[i] == ' ') {
290     				p->userid[i] = '\0';
291     				break;
292     			}
293     		}
294     		p->userid[8] = '\0';
295     		atomic_set (&p->state, FREE);
296     		p->handle =
297     		    iucv_register_program (iucvMagic, p->userid2, (char *) mask,
298     					   &netiucv_ops, (void *) dev);
299     		if (p->handle <= 0) {
300     			printk (KERN_ERR
301     				"%s: iucv_register_program error, rc=%p\n",
302     				dev->name, p->handle);
303     			dev->priv = NULL;
304     			kfree (p);
305     			return -ENODEV;
306     		}
307     		pr_debug ("state@ = %p\n", &p->state);
308     		MOD_INC_USE_COUNT;
309     	}
310     
311     	if (atomic_compare_and_swap (FREE, CONNECTING, &p->state) != 0) {
312     		pr_debug ("Other side connecting during start\n");
313     		return 0;
314     	}
315     
316     	rc =
317     	    iucv_connect (&(p->pathid), MAX_TX_Q, iucvMagic, p->userid2,
318     			  iucv_host, 0, NULL, NULL, p->handle, p);
319     
320     	/* Some errors are not fatal.  In these cases we will report "OK". */
321     	switch (rc) {
322     	case 0:		/* Wait for connection to complete */
323     		pr_debug ("...waiting for connection to complete...");
324     		return 0;
325     	case 11:		/* Wait for parter to connect */
326     		printk (KERN_NOTICE "%s: "
327     			"User %s is not available now.\n",
328     			dev->name, p->userid);
329     		atomic_set (&p->state, FREE);
330     		return 0;
331     	case 12:		/* Wait for partner to connect */
332     		printk (KERN_NOTICE "%s: "
333     			"User %s is not ready to talk now.\n",
334     			dev->name, p->userid);
335     		atomic_set (&p->state, FREE);
336     		return 0;
337     	case 13:		/* Fatal */
338     		printk (KERN_ERR "%s: "
339     			"You have too many IUCV connections."
340     			"Check MAXCONN in CP directory.\n", dev->name);
341     		break;
342     	case 14:		/* Fatal */
343     		printk (KERN_ERR "%s: "
344     			"User %s has too many IUCV connections."
345     			"Check MAXCONN in CP directory.\n",
346     			dev->name, p->userid);
347     		break;
348     	case 15:		/* Fatal */
349     		printk (KERN_ERR "%s: "
350     			"No IUCV authorization found in CP directory.\n",
351     			dev->name);
352     		break;
353     	default:		/* Really fatal! Should not occur!! */
354     		printk (KERN_ERR "%s: "
355     			"return code %i from iucv_connect()\n", dev->name, rc);
356     	}
357     
358     	rc = iucv_unregister_program (p->handle);
359     	dev->priv = NULL;
360     	kfree (p);
361     	MOD_DEC_USE_COUNT;
362     	return -ENODEV;
363     }				/* end iucv_start() */
364     
365     /*********************************************************************/
366     /* Our connection TO another stack has been accepted.                */
367     /*********************************************************************/
368     static void
369     connection_complete (iucv_ConnectionComplete * cci, void *pgm_data)
370     {
371     	struct iucv_priv *p = (struct iucv_priv *) pgm_data;
372     	pr_debug ("...%s connection complete... txq=%u\n",
373     		  p->dev->name, cci->ipmsglim);
374     	atomic_set (&p->state, CONNECTED);
375     	p->pathid = cci->ippathid;
376     	p->dev->tx_queue_len = cci->ipmsglim;
377     	netif_start (p->dev);
378     	netif_start_queue (p->dev);
379     	printk (KERN_NOTICE "%s: Connection to user %s is up\n",
380     		p->dev->name, p->userid);
381     }				/* end connection_complete() */
382     
383     /*********************************************************************/
384     /* A connection FROM another stack is pending.  If we are in the     */
385     /* middle of connecting, sever the new connection.                   */
386     /*								     */
387     /* We only get here if we've done an iucv_register(), so we know     */
388     /* the remote user is the correct user.                              */
389     /*********************************************************************/
390     static void
391     connection_pending (iucv_ConnectionPending * cpi, void *pgm_data)
392     {
393     	/* Only get this far if handler is set up, so we know userid is ok. */
394     	/* and the device is started.                                       */
395     	/* pgm_data is different for this one.  We get dev*, not priv*.     */
396     	net_device *dev = (net_device *) pgm_data;
397     	struct iucv_priv *p = (struct iucv_priv *) dev->priv;
398     	int rc;
399     	u16 msglimit;
400     	uchar udata[16];
401     
402     	/* If we're not waiting on a connect, reject the connection */
403     	if (atomic_compare_and_swap (FREE, CONNECTING, &p->state) != 0) {
404     		iucv_sever (cpi->ippathid, udata);
405     		return;
406     	}
407     
408     	rc = iucv_accept (cpi->ippathid,	/* Path id                      */
409     			  MAX_TX_Q,	/* desired IUCV msg limit       */
410     			  udata,	/* user_Data                    */
411     			  0,	/* No flags                     */
412     			  p->handle,	/* registration handle          */
413     			  p,	/* private data                 */
414     			  NULL,	/* don't care about output flags */
415     			  &msglimit);	/* Actual IUCV msg limit        */
416     	if (rc != 0) {
417     		atomic_set (&p->state, FREE);
418     		printk (KERN_ERR "%s: iucv accept failed rc=%i\n",
419     			p->dev->name, rc);
420     	} else {
421     		atomic_set (&p->state, CONNECTED);
422     		p->pathid = cpi->ippathid;
423     		p->dev->tx_queue_len = (u32) msglimit;
424     		netif_start (p->dev);
425     		netif_start_queue (p->dev);
426     		printk (KERN_NOTICE "%s: Connection to user %s is up\n",
427     			p->dev->name, p->userid);
428     	}
429     }				/* end connection_pending() */
430     
431     /*********************************************************************/
432     /* Our connection to another stack has been severed.                 */
433     /*********************************************************************/
434     static void
435     connection_severed (iucv_ConnectionSevered * eib, void *pgm_data)
436     {
437     	struct iucv_priv *p = (struct iucv_priv *) pgm_data;
438     
439     	printk (KERN_INFO "%s: Connection to user %s is down\n",
440     		p->dev->name, p->userid);
441     
442     	/* FIXME: We can also get a severed interrupt while in
443     	          state CONNECTING!  Fix the state machine ... */
444     #if 0
445     	if (atomic_compare_and_swap (CONNECTED, FREE, &p->state) != 0)
446     		return;		/* In case reconnect in progress already */
447     #else
448     	atomic_set (&p->state, FREE);
449     #endif
450     
451     	netif_stop_queue (p->dev);
452     	netif_stop (p->dev);
453     }				/* end connection_severed() */
454     
455     /*-----------------------------------------------------*/
456     /* STOP device.                   Called by kernel.    */
457     /*-----------------------------------------------------*/
458     static int
459     iucv_stop (net_device * dev)
460     {
461     	int rc = 0;
462     	struct iucv_priv *p;
463     	pr_debug ("%s: iucv_stop\n", dev->name);
464     
465     	netif_stop_queue (dev);
466     	netif_stop (dev);
467     
468     	p = (struct iucv_priv *) (dev->priv);
469     	if (p == NULL)
470     		return 0;
471     
472     	/* Unregister will sever associated connections */
473     	rc = iucv_unregister_program (p->handle);
474     	dev->priv = NULL;
475     	kfree (p);
476     	MOD_DEC_USE_COUNT;
477     	return 0;
478     }				/* end  iucv_stop() */
479     
480     /*---------------------------------------------------------------------*/
481     /* Inbound packets from other host are ready for receipt.  Receive     */
482     /* them (they arrive as a single transmission), break them up into     */
483     /* separate packets, and send them to the "generic" packet processor.  */
484     /*---------------------------------------------------------------------*/
485     static void
486     message_pending (iucv_MessagePending * mpi, void *pgm_data)
487     {
488     	struct iucv_priv *p = (struct iucv_priv *) pgm_data;
489     	int rc;
490     	u32 buffer_length;
491     	u16 packet_offset, prev_offset = 0;
492     	void *buffer;
493     
494     	buffer_length = mpi->ln1msg2.ipbfln1f;
495     	pr_debug ("%s: MP id=%i Length=%u\n",
496     		  p->dev->name, mpi->ipmsgid, buffer_length);
497     
498     	buffer = kmalloc (buffer_length, GFP_ATOMIC | GFP_DMA);
499     	if (buffer == NULL) {
500     		p->stats.rx_dropped++;
501     		return;
502     	}
503     	rc = iucv_receive (p->pathid, mpi->ipmsgid, mpi->iptrgcls,
504     			   buffer, buffer_length, NULL, NULL, NULL);
505     
506     	if (rc != 0 || buffer_length < 5) {
507     		printk (KERN_INFO
508     			"%s: IUCV rcv error. rc=%X ID=%i length=%u\n",
509     			p->dev->name, rc, mpi->ipmsgid, buffer_length);
510     		p->stats.rx_errors++;
511     		kfree (buffer);
512     		return;
513     	}
514     
515     	packet_offset = *((u16 *) buffer);
516     
517     	while (packet_offset != 0) {
518     		if (packet_offset <= prev_offset
519     		    || packet_offset > buffer_length - 2) {
520     			printk (KERN_INFO "%s: bad inbound packet offset %u, "
521     				"prev %u, total %u\n", p->dev->name,
522     				packet_offset, prev_offset, buffer_length);
523     			p->stats.rx_errors++;
524     			break;
525     		} else {
526     			/* Kick the packet upstairs */
527     			iucv_rx (p->dev, mpi->ipmsgid,
528     				 buffer + prev_offset + 2,
529     				 packet_offset - prev_offset - 2);
530     			prev_offset = packet_offset;
531     			packet_offset = *((u16 *) (buffer + packet_offset));
532     		}
533     	}
534     
535     	kfree (buffer);
536     	return;
537     }				/* end message_pending() */
538     
539     /*-------------------------------------------------------------*/
540     /* Add meta-data to packet and send upstairs.                  */
541     /*-------------------------------------------------------------*/
542     static void
543     iucv_rx (net_device * dev, u32 msgid, uchar * buf, int len)
544     {
545     	struct iucv_priv *p = (struct iucv_priv *) dev->priv;
546     	struct sk_buff *skb;
547     
548     #ifdef IPDEBUG
549     	printk (KERN_DEBUG "RX id=%i\n", msgid);
550     	dumpit (buf, 20);
551     	dumpit (buf + 20, 20);
552     #endif
553     
554     	pr_debug ("%s: RX len=%u\n", p->dev->name, len);
555     
556     	if (len > p->dev->mtu) {
557     		printk (KERN_INFO
558     			"%s: inbound packet id# %i length %u exceeds MTU %i\n",
559     			p->dev->name, msgid, len, p->dev->mtu);
560     		p->stats.rx_errors++;
561     		return;
562     	}
563     
564     	skb = dev_alloc_skb (len);
565     	if (!skb) {
566     		p->stats.rx_dropped++;
567     		return;
568     	}
569     
570     	/* If not enough room, skb_put will panic */
571     	memcpy (skb_put (skb, len), buf, len);
572     
573     	/* Write metadata, and then pass to the receive level.  Since we */
574     	/* are not an Ethernet device, we have special fields to set.    */
575     	/* This is all boilerplace, not to be messed with.               */
576     	skb->dev = p->dev;	/* Set device       */
577     	skb->mac.raw = skb->data;	/* Point to packet  */
578     	skb->pkt_type = PACKET_HOST;	/* ..for this host. */
579     	skb->protocol = htons (ETH_P_IP);	/* IP packet        */
580     	skb->ip_summed = CHECKSUM_UNNECESSARY;	/* No checksum      */
581     	p->stats.rx_packets++;
582     	p->stats.rx_bytes += len;
583     	netif_rx (skb);
584     
585     	return;
586     }				/* end  iucv_rx() */
587     
588     /*-------------------------------------------------------------*/
589     /* TRANSMIT a packet.            	    Called by kernel.  */
590     /* This function deals with hw details of packet transmission. */
591     /*-------------------------------------------------------------*/
592     static int
593     iucv_tx (struct sk_buff *skb, net_device * dev)
594     {
595     	int rc, pktlen;
596     	u32 framelen, msgid;
597     	void *frame;
598     	struct iucv_priv *p = (struct iucv_priv *) dev->priv;
599     
600     	if (skb == NULL) {	/* Nothing to do */
601     		printk (KERN_WARNING "%s: TX Kernel passed null sk_buffer\n",
602     			dev->name);
603     		p->stats.tx_dropped++;
604     		return -EIO;
605     	}
606     
607     	if (netif_is_busy (dev))
608     		return -EBUSY;
609     
610     	dev->trans_start = jiffies;	/* save the timestamp */
611     
612     	/* IUCV frame will be released when MessageComplete   */
613     	/* interrupt is received.                             */
614     	pktlen = skb->len;
615     	framelen = pktlen + 4;
616     
617     	frame = kmalloc (framelen, GFP_ATOMIC | GFP_DMA);
618     	if (!frame) {
619     		p->stats.tx_dropped++;
620     		dev_kfree_skb (skb);
621     		return 0;
622     	}
623     
624     	netif_stop_queue (dev);	/* transmission is busy */
625     
626     	*(u16 *) frame = pktlen + 2;	/* Set header   */
627     	memcpy (frame + 2, skb->data, pktlen);	/* Copy data    */
628     	memset (frame + pktlen + 2, 0, 2);	/* Set trailer  */
629     
630     	/* Ok, now the frame is ready for transmission: send it. */
631     	rc = iucv_send (p->pathid, &msgid, 0, 0,
632     			(u32) frame,	/* Msg tag      */
633     			0,	/* No flags     */
634     			frame, framelen);
635     	if (rc == 0) {
636     #ifdef IPDEBUG
637     		printk (KERN_DEBUG "TX id=%i\n", msgid);
638     		dumpit (skb->data, 20);
639     		dumpit (skb->data + 20, 20);
640     #endif
641     		pr_debug ("%s: tx START %i.%i @=%p len=%i\n",
642     			  p->dev->name, p->pathid, msgid, frame, framelen);
643     		p->stats.tx_packets++;
644     	} else {
645     		if (rc == 3)	/* Exceeded MSGLIMIT */
646     			p->stats.tx_dropped++;
647     		else {
648     			p->stats.tx_errors++;
649     			printk (KERN_INFO "%s: tx ERROR id=%i.%i rc=%i\n",
650     				p->dev->name, p->pathid, msgid, rc);
651     		}
652     		/* We won't get interrupt.  Free frame now. */
653     		kfree (frame);
654     	}
655     	dev_kfree_skb (skb);	/* Finished with skb            */
656     
657     	netif_wake_queue (p->dev);
658     	return 0;
659     }				/* end iucv_tx() */
660     
661     /*-----------------------------------------------------------*/
662     /* SEND COMPLETE                    Called by IUCV handler.  */
663     /* Free the IUCV frame that was used for this transmission.  */
664     /*-----------------------------------------------------------*/
665     static void
666     send_complete (iucv_MessageComplete * mci, void *pgm_data)
667     {
668     	void *frame;
669     #ifdef DEBUG
670     	struct iucv_priv *p = (struct iucv_priv *) pgm_data;
671     #endif
672     	frame = (void *) (ulong) mci->ipmsgtag;
673     	kfree (frame);
674     	pr_debug ("%s: TX DONE %i.%i @=%p\n",
675     		  p->dev->name, mci->ippathid, mci->ipmsgid, frame);
676     }				/* end send_complete() */
677     
678     /*-----------------------------------------------------------*/
679     /* STATISTICS reporting.                  Called by kernel.  */
680     /*-----------------------------------------------------------*/
681     static struct net_device_stats *
682     iucv_stats (net_device * dev)
683     {
684     	struct iucv_priv *p = (struct iucv_priv *) dev->priv;
685     	return &p->stats;
686     }				/* end iucv_stats() */
687     
688     /*-----------------------------------------------------------*/
689     /* MTU change    .                        Called by kernel.  */
690     /* IUCV can handle mtu sizes from 576 (the IP architectural  */
691     /* minimum) up to maximum supported by VM.  I don't think IP */
692     /* pays attention to new mtu until device is restarted.      */
693     /*-----------------------------------------------------------*/
694     static int
695     iucv_change_mtu (net_device * dev, int new_mtu)
696     {
697     	if ((new_mtu < 576) || (new_mtu > MAX_VM_MTU))
698     		return -EINVAL;
699     	dev->mtu = new_mtu;
700     	return 0;
701     }				/* end iucv_change_mtu() */
702     
703     /*-----------------------------------------------------------*/
704     /* INIT device.                           Called by kernel.  */
705     /* Called by register_netdev() in kernel.                    */
706     /*-----------------------------------------------------------*/
707     static int
708     iucv_init (net_device * dev)
709     {
710     	dev->open = iucv_start;
711     	dev->stop = iucv_stop;
712     	dev->hard_start_xmit = iucv_tx;
713     	dev->get_stats = iucv_stats;
714     	dev->change_mtu = iucv_change_mtu;
715     	dev->hard_header_len = 0;
716     	dev->addr_len = 0;
717     	dev->type = ARPHRD_SLIP;
718     	dev->tx_queue_len = MAX_TX_Q;	/* Default - updated based on IUCV */
719     	/* keep the default flags, just add NOARP and POINTOPOINT */
720     	dev->flags |= IFF_NOARP | IFF_POINTOPOINT;
721     	dev->mtu = 9216;
722     
723     	dev_init_buffers (dev);
724     	pr_debug ("%s: iucv_init  dev@=%p\n", dev->name, dev);
725     	return 0;
726     }
727     
728     #ifndef MODULE
729     /*-----------------------------------------------------------------*/
730     /* Process iucv=userid1,...,useridn kernel parameter.              */
731     /*                                                                 */
732     /* Each user id provided will be associated with device 'iucvnn'.  */
733     /* iucv_init will be called to initialize each device.             */
734     /*-----------------------------------------------------------------*/
735     #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
736     #define init_return(a) return a
737     static int __init
738     iucv_setup (char *iucv)
739     #else
740     #define init_return(a) return
741     __initfunc (void iucv_setup (char *iucv, int *ints))
742     #endif
743     {
744     	int i, devnumber;
745     	char *s;
746     	char temp_userid[9];
747     
748     	i = devnumber = 0;
749     	memset (temp_userid, ' ', 8);
750     	temp_userid[8] = '\0';
751     	printk (KERN_NOTICE "netiucv: IUCV network driver " LEVEL "\n");
752     
753     	if (!iucv)
754     		init_return (0);
755     
756     	for (s = iucv; *s != '\0'; s++) {
757     		if (*s == ' ')	/* Compress out blanks */
758     			continue;
759     
760     		if (devnumber >= MAX_DEVICES) {
761     			printk (KERN_ERR "More than %i IUCV hosts specified\n",
762     				MAX_DEVICES);
763     			init_return (-ENODEV);
764     		}
765     
766     		if (*s != ',') {
767     			temp_userid[i++] = *s;
768     
769     			if (i == 8 || *(s + 1) == ',' || *(s + 1) == '\0') {
770     				register_iucv_dev (devnumber, temp_userid);
771     				devnumber++;
772     				i = 0;
773     				memset (temp_userid, ' ', 8);
774     				if (*(s + 1) != '\0')
775     					*(s + 1) = ' ';
776     			}
777     		}
778     	}			/* while */
779     
780     	init_return (1);
781     }
782     
783     #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
784     __setup ("iucv=", iucv_setup);
785     #endif
786     #else				/* BUILT AS MODULE */
787     /*-------------------------------------------------------------------*/
788     /* Process iucv=userid1,...,useridn module paramter.                 */
789     /*                                                                   */
790     /* insmod passes the module an array of string pointers, each of     */
791     /* which points to a userid.  The commas are stripped out by insmod. */
792     /* MODULE_PARM defines the name of the array.  (See start of module.)*/
793     /*                                                                   */
794     /* Each user id provided will be associated with device 'iucvnn'.    */
795     /* iucv_init will be called to initialize each device.               */
796     /*-------------------------------------------------------------------*/
797     char *iucv[MAX_DEVICES] = { NULL };
798     int
799     init_module (void)
800     {
801     	int i;
802     	printk (KERN_NOTICE "netiucv: IUCV network driver " LEVEL "\n");
803     	for (i = 0; i < MAX_DEVICES; i++) {
804     		if (iucv[i] == NULL)
805     			break;
806     		register_iucv_dev (i, iucv[i]);
807     	}
808     	return 0;
809     }
810     
811     void
812     cleanup_module (void)
813     {
814     	int i;
815     	for (i = 0; i < MAX_DEVICES; i++) {
816     		if (iucv[i])
817     			unregister_netdev (&iucv_netdev[i]);
818     	}
819     	return;
820     }
821     #endif				/* MODULE */
822     
823     void
824     register_iucv_dev (int devnumber, char *userid)
825     {
826     	int rc;
827     	net_device *dev;
828     
829     	memset (iucv_userid[devnumber], ' ', 8);
830     	memcpy (iucv_userid[devnumber], userid,
831     		min_t(unsigned int, strlen(userid), 8));
832     	dev = &iucv_netdev[devnumber];
833     	sprintf (dev->name, "iucv%i", devnumber);
834     
835     	pr_debug ("netiucv: registering %s\n", dev->name);
836     
837     	if ((rc = register_netdev (dev))) {
838     		printk (KERN_ERR
839     			"netiucv: register_netdev(%s) error %i\n",
840     			dev->name, rc);
841     	}
842     	return;
843     }
844     
845     /* These structures are static because setup() can be called very */
846     /* early in kernel init if this module is built into the kernel.  */
847     /* Certainly no kmalloc() is available, probably no C runtime.    */
848     /* If support changed to be module only, this can all be done     */
849     /* dynamically.                                                   */
850     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
851     static char iucv_names[MAX_DEVICES][8];	/* Allows "iucvXXX" plus null */
852     #endif
853     net_device iucv_netdev[MAX_DEVICES] = {
854     	{
855     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
856     		name: &iucv_names[0][0],  /* Name filled in at load time  */
857     #endif
858     		init: iucv_init           /* probe function               */
859     	},
860     	{
861     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
862     		name: &iucv_names[1][0],  /* Name filled in at load time  */
863     #endif
864     		init: iucv_init           /* probe function               */
865     	},
866     	{
867     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
868     		name: &iucv_names[2][0],  /* Name filled in at load time  */
869     #endif
870     		init: iucv_init           /* probe function               */
871     	},
872     	{
873     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
874     		name: &iucv_names[3][0],  /* Name filled in at load time  */
875     #endif
876     		init: iucv_init           /* probe function               */
877     	},
878     	{
879     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
880     		name: &iucv_names[4][0],  /* Name filled in at load time  */
881     #endif
882     		init: iucv_init           /* probe function               */
883     	},
884     	{
885     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
886     		name: &iucv_names[5][0],  /* Name filled in at load time  */
887     #endif
888     		init: iucv_init           /* probe function               */
889     	},
890     	{
891     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
892     		name: &iucv_names[6][0],  /* Name filled in at load time  */
893     #endif
894     		init: iucv_init           /* probe function               */
895     	},
896     	{
897     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
898     		name: &iucv_names[7][0],  /* Name filled in at load time  */
899     #endif
900     		init: iucv_init           /* probe function               */
901     	},
902     	{
903     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
904     		name: &iucv_names[8][0],  /* Name filled in at load time  */
905     #endif
906     		init: iucv_init           /* probe function               */
907     	},
908     	{
909     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
910     		name: &iucv_names[9][0],  /* Name filled in at load time  */
911     #endif
912     		init: iucv_init           /* probe function               */
913     	},
914     	{
915     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
916     		name: &iucv_names[10][0], /* Name filled in at load time  */
917     #endif
918     		init: iucv_init           /* probe function               */
919     	},
920     	{
921     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
922     		name: &iucv_names[11][0], /* Name filled in at load time  */
923     #endif
924     		init: iucv_init           /* probe function               */
925     	},
926     	{
927     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
928     		name: &iucv_names[12][0], /* Name filled in at load time  */
929     #endif
930     		init: iucv_init           /* probe function               */
931     	},
932     	{
933     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
934     		name: &iucv_names[13][0], /* Name filled in at load time  */
935     #endif
936     		init: iucv_init           /* probe function               */
937     	},
938     	{
939     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
940     		name: &iucv_names[14][0], /* Name filled in at load time  */
941     #endif
942     		init: iucv_init           /* probe function               */
943     	},
944     	{
945     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
946     		name: &iucv_names[15][0], /* Name filled in at load time  */
947     #endif
948     		init: iucv_init           /* probe function               */
949     	},
950     	{
951     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
952     		name: &iucv_names[16][0], /* Name filled in at load time  */
953     #endif
954     		init: iucv_init           /* probe function               */
955     	},
956     	{
957     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
958     		name: &iucv_names[17][0], /* Name filled in at load time  */
959     #endif
960     		init: iucv_init           /* probe function               */
961     	},
962     	{
963     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
964     		name: &iucv_names[18][0], /* Name filled in at load time  */
965     #endif
966     		init: iucv_init           /* probe function               */
967     	},
968     	{
969     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
970     		name: &iucv_names[19][0], /* Name filled in at load time  */
971     #endif
972     		init: iucv_init           /* probe function               */
973     	},
974     };
975