Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/*
 *  drivers/s390/net/netiucv.c
 *    Network driver for VM using iucv
 *
 *  S/390 version
 *    Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
 *    Author(s): Stefan Hegewald <hegewald@de.ibm.com>
 *               Hartmut Penner <hpenner@de.ibm.com>
 *
 *
 *    2.3 Updates Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
 *                Martin Schwidefsky (schwidefsky@de.ibm.com)
 *
 *    Re-write:   Alan Altmark (Alan_Altmark@us.ibm.com)  Sept. 2000
 *                Uses iucv.c kernel module for IUCV services. 
 *
 *    2.4 Updates Alan Altmark (Alan_Altmark@us.ibm.com)  June 2001
 *                Update to use changed IUCV (iucv.c) interface.
 *
 * -------------------------------------------------------------------------- 
 *  An IUCV frame consists of one or more packets preceded by a 16-bit
 *  header.   The header contains the offset to the next packet header,
 *  measured from the beginning of the _frame_.  If zero, there are no more
 *  packets in the frame.  Consider a frame which contains a 10-byte packet
 *  followed by a 20-byte packet:
 *        +-----+----------------+--------------------------------+-----+
 *        |h'12'| 10-byte packet |h'34'|  20-byte packet          |h'00'|
 *        +-----+----------------+-----+--------------------------+-----+
 * Offset: 0     2                12    14                         34  
 *
 *  This means that each header will always have a larger value than the
 *  previous one (except for the final zero header, of course).
 *  
 *  For outbound packets, we send ONE frame per packet.  So, our frame is:
 *       AL2(packet length+2), packet, AL2(0)
 *  The maximum packet size is the MTU, so the maximum IUCV frame we send
 *  is MTU+4 bytes.
 *
 *  For inbound frames, we don't care how long the frame is.  We tear apart
 *  the frame, processing packets up to MTU size in length, until no more
 *  packets remain in the frame.
 *
 * --------------------------------------------------------------------------
 *  The code uses the 2.3.43 network driver interfaces.  If compiled on an
 *  an older level of the kernel, the module provides its own macros.
 *  Doc is in Linux Weekly News (lwn.net) memo from David Miller, 9 Feb 2000.
 *  There are a few other places with 2.3-specific enhancements.
 *
 * --------------------------------------------------------------------------
*/
//#define DEBUG 1
//#define DEBUG2 1
//#define IPDEBUG 1
#define LEVEL "1.1"

/* If MAX_DEVICES increased, add initialization data to iucv_netdev[] array */
/* (See bottom of program.)						    */
#define MAX_DEVICES 20		/* Allows "iucv0" to "iucv19"   */
#define MAX_VM_MTU 32764	/* 32K IUCV buffer, minus 4     */
#define MAX_TX_Q 50		/* Maximum pending TX           */

#include <linux/version.h>
#include <linux/kernel.h>

#ifdef MODULE
#include <linux/module.h>
MODULE_AUTHOR
    ("(C) 2000 IBM Corporation by Alan Altmark (Alan_Altmark@us.ibm.com)");
MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver " LEVEL);
MODULE_PARM (iucv, "1-" __MODULE_STRING (MAX_DEVICES) "s");
MODULE_PARM_DESC (iucv,
		  "Specify the userids associated with iucv0-iucv9:\n"
		  "iucv=userid1,userid2,...,userid10\n");
#ifdef MODVERSIONS
#include <linux/modversions.h>
#endif
#else
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
#endif

#include <linux/sched.h>	/* task queues                  */
#include <linux/malloc.h>	/* kmalloc()                    */
#include <linux/errno.h>	/* error codes                  */
#include <linux/types.h>	/* size_t                       */
#include <linux/interrupt.h>	/* mark_bh                      */
#include <linux/netdevice.h>	/* struct net_device, etc.      */
#include <linux/if_arp.h>	/* ARPHRD_SLIP                  */
#include <linux/ip.h>		/* IP header                    */
#include <linux/skbuff.h>	/* skb                          */
#include <linux/init.h>		/* __setup()                    */
#include <asm/string.h>		/* memset, memcpy, etc.         */
#include "iucv.h"

#if defined( DEBUG )
#undef KERN_INFO
#undef KERN_DEBUG
#undef KERN_NOTICE
#undef KERN_ERR
#define KERN_INFO    KERN_EMERG
#define KERN_DEBUG   KERN_EMERG
#define KERN_NOTICE  KERN_EMERG
#define KERN_ERR     KERN_EMERG
#endif

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
typedef struct net_device net_device;
#else
typedef struct device net_device;
#endif

static __inline__ int
netif_is_busy (net_device * dev)
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,45))
	return (dev->tbusy);
#else
	return (test_bit (__LINK_STATE_XOFF, &dev->flags));
#endif
}

#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,45))
	/* Provide our own 2.3.45 interfaces */
#define netif_enter_interrupt(dev) dev->interrupt=1
#define netif_exit_interrupt(dev) dev->interrupt=0
#define netif_start(dev) dev->start=1
#define netif_stop(dev) dev->start=0

static __inline__ void
netif_stop_queue (net_device * dev)
{
	dev->tbusy = 1;
}

static __inline__ void
netif_start_queue (net_device * dev)
{
	dev->tbusy = 0;
}

static __inline__ void
netif_wake_queue (net_device * dev)
{
	dev->tbusy = 0;
	mark_bh (NET_BH);
}

#else
	/* As of 2.3.45, we don't do these things anymore */
#define netif_enter_interrupt(dev)
#define netif_exit_interrupt(dev)
#define netif_start(dev)
#define netif_stop(dev)
#endif

static int iucv_start (net_device *);
static int iucv_stop (net_device *);
static int iucv_change_mtu (net_device *, int);
static int iucv_init (net_device *);
static void iucv_rx (net_device *, u32, uchar *, int);
static int iucv_tx (struct sk_buff *, net_device *);

static void connection_severed (iucv_ConnectionSevered *, void *);
static void connection_pending (iucv_ConnectionPending *, void *);
static void connection_complete (iucv_ConnectionComplete *, void *);
static void message_pending (iucv_MessagePending *, void *);
static void send_complete (iucv_MessageComplete *, void *);

void register_iucv_dev (int, char *);

static iucv_interrupt_ops_t netiucv_ops = {
	&connection_pending,
	&connection_complete,
	&connection_severed,
	NULL,			/* Quiesced             */
	NULL,			/* Resumed              */
	&message_pending,	/* Message pending      */
	&send_complete		/* Message complete     */
};

static char iucv_userid[MAX_DEVICES][8];
net_device iucv_netdev[MAX_DEVICES];

/* This structure is private to each device. It contains the    */
/* information necessary to do IUCV operations.                 */
struct iucv_priv {
	struct net_device_stats stats;
	net_device *dev;
	iucv_handle_t handle;
	uchar userid[9];	/* Printable userid */
	uchar userid2[8];	/* Used for IUCV operations */

	/* Note: atomic_compare_and_swap() return value is backwards */
	/*       from what you might think: FALSE=0=OK, TRUE=1=FAIL  */
	atomic_t state;
#define FREE 0
#define CONNECTING 1
#define CONNECTED 2
	u16 pathid;
};

uchar iucv_host[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uchar iucvMagic[16] = { 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
	0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
};

/* This mask means the 16-byte IUCV "magic" and the origin userid must */
/* match exactly as specified in order to give connection_pending()    */
/* control. 							       */
const char mask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

#if defined( DEBUG2 ) || defined( IPDEBUG )
/*--------------------------*/
/* Dump buffer formatted    */
/*--------------------------*/
static void
dumpit (char *buf, int len)
{
	int i;
	printk (KERN_DEBUG);
	for (i = 0; i < len; i++) {
		if (!(i % 32) && i != 0)
			printk ("\n");
		else if (!(i % 4) && i != 0)
			printk (" ");
		printk ("%02X", buf[i]);
	}
	if (len % 32)
		printk ("\n");
}
#endif

/*-----------------------------------------------------------------*/
/* Open a connection to another Linux or VM TCP/IP stack.          */
/* Called by kernel.						   */
/*                                                                 */
/* 1. Register a handler. (Up to now, any attempt by another stack */
/*    has been rejected by the IUCV handler.)  We give the handler */
/*    the net_device* so that we can locate the dev associated     */
/*    with the partner userid if he tries to connect to us or      */
/*    if the connection is broken.                                 */
/*                                                                 */
/* 2. Connect to remote stack.  If we get a connection pending     */
/*    interrupt while we're in the middle of connecting, don't     */
/*    worry.  VM will sever its and use ours, because the DEVICE   */
/*    is defined to be:                                            */
/*           DEVICE devname IUCV 0 0 linuxvm A                     */
/*        or DEVICE devname IUCV 0 0 linuxvm B                     */
/*    In EBCDIC, "0" (0xF0) is greater than "A" (0xC1) or "B", so  */
/*    win all races.  We will sever any connects that occur while  */
/*    we are connecting.  The "0 0" is where we get iucvMagic from.*/
/*								   */
/*    FIXME: If two Linux machines get into this race condition,   */
/*           both will sever.  Manual intervention required.       */
/*           Need a better IUCV "hello"-like function that permits */
/*           some negotiation.  But can't do that until VM TCP/IP  */
/*           would support it.                                     */
/*                                                                 */
/* 3. Return 0 to indicate device ok.  Anything else is an error.  */
/*-----------------------------------------------------------------*/
static int
iucv_start (net_device * dev)
{
	int rc, i;
	struct iucv_priv *p = (struct iucv_priv *) dev->priv;

	pr_debug ("iucv_start(%s)\n", dev->name);

	if (p == NULL) {
		/* Allocate priv data */
		p = (struct iucv_priv *) kmalloc (sizeof (struct iucv_priv),
						  GFP_ATOMIC);
		if (p == NULL) {
			printk (KERN_CRIT "%s: no memory for dev->priv.\n",
				dev->name);
			return -ENOMEM;
		}
		memset (p, 0, sizeof (struct iucv_priv));
		dev->priv = p;
		p->dev = dev;

		memcpy (p->userid, iucv_userid[dev - iucv_netdev], 8);	/* Save userid */
		memcpy (p->userid2, p->userid, 8);	/* Again, with feeling.  */

		for (i = 0; i < 8; i++) {	/* Change userid to printable form */
			if (p->userid[i] == ' ') {
				p->userid[i] = '\0';
				break;
			}
		}
		p->userid[8] = '\0';
		atomic_set (&p->state, FREE);
		p->handle =
		    iucv_register_program (iucvMagic, p->userid2, (char *) mask,
					   &netiucv_ops, (void *) dev);
		if (p->handle <= 0) {
			printk (KERN_ERR
				"%s: iucv_register_program error, rc=%p\n",
				dev->name, p->handle);
			dev->priv = NULL;
			kfree (p);
			return -ENODEV;
		}
		pr_debug ("state@ = %p\n", &p->state);
		MOD_INC_USE_COUNT;
	}

	if (atomic_compare_and_swap (FREE, CONNECTING, &p->state) != 0) {
		pr_debug ("Other side connecting during start\n");
		return 0;
	}

	rc =
	    iucv_connect (&(p->pathid), MAX_TX_Q, iucvMagic, p->userid2,
			  iucv_host, 0, NULL, NULL, p->handle, p);

	/* Some errors are not fatal.  In these cases we will report "OK". */
	switch (rc) {
	case 0:		/* Wait for connection to complete */
		pr_debug ("...waiting for connection to complete...");
		return 0;
	case 11:		/* Wait for parter to connect */
		printk (KERN_NOTICE "%s: "
			"User %s is not available now.\n",
			dev->name, p->userid);
		atomic_set (&p->state, FREE);
		return 0;
	case 12:		/* Wait for partner to connect */
		printk (KERN_NOTICE "%s: "
			"User %s is not ready to talk now.\n",
			dev->name, p->userid);
		atomic_set (&p->state, FREE);
		return 0;
	case 13:		/* Fatal */
		printk (KERN_ERR "%s: "
			"You have too many IUCV connections."
			"Check MAXCONN in CP directory.\n", dev->name);
		break;
	case 14:		/* Fatal */
		printk (KERN_ERR "%s: "
			"User %s has too many IUCV connections."
			"Check MAXCONN in CP directory.\n",
			dev->name, p->userid);
		break;
	case 15:		/* Fatal */
		printk (KERN_ERR "%s: "
			"No IUCV authorization found in CP directory.\n",
			dev->name);
		break;
	default:		/* Really fatal! Should not occur!! */
		printk (KERN_ERR "%s: "
			"return code %i from iucv_connect()\n", dev->name, rc);
	}

	rc = iucv_unregister_program (p->handle);
	dev->priv = NULL;
	kfree (p);
	MOD_DEC_USE_COUNT;
	return -ENODEV;
}				/* end iucv_start() */

/*********************************************************************/
/* Our connection TO another stack has been accepted.                */
/*********************************************************************/
static void
connection_complete (iucv_ConnectionComplete * cci, void *pgm_data)
{
	struct iucv_priv *p = (struct iucv_priv *) pgm_data;
	pr_debug ("...%s connection complete... txq=%u\n",
		  p->dev->name, cci->ipmsglim);
	atomic_set (&p->state, CONNECTED);
	p->pathid = cci->ippathid;
	p->dev->tx_queue_len = cci->ipmsglim;
	netif_start (p->dev);
	netif_start_queue (p->dev);
	printk (KERN_NOTICE "%s: Connection to user %s is up\n",
		p->dev->name, p->userid);
}				/* end connection_complete() */

/*********************************************************************/
/* A connection FROM another stack is pending.  If we are in the     */
/* middle of connecting, sever the new connection.                   */
/*								     */
/* We only get here if we've done an iucv_register(), so we know     */
/* the remote user is the correct user.                              */
/*********************************************************************/
static void
connection_pending (iucv_ConnectionPending * cpi, void *pgm_data)
{
	/* Only get this far if handler is set up, so we know userid is ok. */
	/* and the device is started.                                       */
	/* pgm_data is different for this one.  We get dev*, not priv*.     */
	net_device *dev = (net_device *) pgm_data;
	struct iucv_priv *p = (struct iucv_priv *) dev->priv;
	int rc;
	u16 msglimit;
	uchar udata[16];

	/* If we're not waiting on a connect, reject the connection */
	if (atomic_compare_and_swap (FREE, CONNECTING, &p->state) != 0) {
		iucv_sever (cpi->ippathid, udata);
		return;
	}

	rc = iucv_accept (cpi->ippathid,	/* Path id                      */
			  MAX_TX_Q,	/* desired IUCV msg limit       */
			  udata,	/* user_Data                    */
			  0,	/* No flags                     */
			  p->handle,	/* registration handle          */
			  p,	/* private data                 */
			  NULL,	/* don't care about output flags */
			  &msglimit);	/* Actual IUCV msg limit        */
	if (rc != 0) {
		atomic_set (&p->state, FREE);
		printk (KERN_ERR "%s: iucv accept failed rc=%i\n",
			p->dev->name, rc);
	} else {
		atomic_set (&p->state, CONNECTED);
		p->pathid = cpi->ippathid;
		p->dev->tx_queue_len = (u32) msglimit;
		netif_start (p->dev);
		netif_start_queue (p->dev);
		printk (KERN_NOTICE "%s: Connection to user %s is up\n",
			p->dev->name, p->userid);
	}
}				/* end connection_pending() */

/*********************************************************************/
/* Our connection to another stack has been severed.                 */
/*********************************************************************/
static void
connection_severed (iucv_ConnectionSevered * eib, void *pgm_data)
{
	struct iucv_priv *p = (struct iucv_priv *) pgm_data;

	printk (KERN_INFO "%s: Connection to user %s is down\n",
		p->dev->name, p->userid);

	/* FIXME: We can also get a severed interrupt while in
	          state CONNECTING!  Fix the state machine ... */
#if 0
	if (atomic_compare_and_swap (CONNECTED, FREE, &p->state) != 0)
		return;		/* In case reconnect in progress already */
#else
	atomic_set (&p->state, FREE);
#endif

	netif_stop_queue (p->dev);
	netif_stop (p->dev);
}				/* end connection_severed() */

/*-----------------------------------------------------*/
/* STOP device.                   Called by kernel.    */
/*-----------------------------------------------------*/
static int
iucv_stop (net_device * dev)
{
	int rc = 0;
	struct iucv_priv *p;
	pr_debug ("%s: iucv_stop\n", dev->name);

	netif_stop_queue (dev);
	netif_stop (dev);

	p = (struct iucv_priv *) (dev->priv);
	if (p == NULL)
		return 0;

	/* Unregister will sever associated connections */
	rc = iucv_unregister_program (p->handle);
	dev->priv = NULL;
	kfree (p);
	MOD_DEC_USE_COUNT;
	return 0;
}				/* end  iucv_stop() */

/*---------------------------------------------------------------------*/
/* Inbound packets from other host are ready for receipt.  Receive     */
/* them (they arrive as a single transmission), break them up into     */
/* separate packets, and send them to the "generic" packet processor.  */
/*---------------------------------------------------------------------*/
static void
message_pending (iucv_MessagePending * mpi, void *pgm_data)
{
	struct iucv_priv *p = (struct iucv_priv *) pgm_data;
	int rc;
	u32 buffer_length;
	u16 packet_offset, prev_offset = 0;
	void *buffer;

	buffer_length = mpi->ln1msg2.ipbfln1f;
	pr_debug ("%s: MP id=%i Length=%u\n",
		  p->dev->name, mpi->ipmsgid, buffer_length);

	buffer = kmalloc (buffer_length, GFP_ATOMIC | GFP_DMA);
	if (buffer == NULL) {
		p->stats.rx_dropped++;
		return;
	}
	rc = iucv_receive (p->pathid, mpi->ipmsgid, mpi->iptrgcls,
			   buffer, buffer_length, NULL, NULL, NULL);

	if (rc != 0 || buffer_length < 5) {
		printk (KERN_INFO
			"%s: IUCV rcv error. rc=%X ID=%i length=%u\n",
			p->dev->name, rc, mpi->ipmsgid, buffer_length);
		p->stats.rx_errors++;
		kfree (buffer);
		return;
	}

	packet_offset = *((u16 *) buffer);

	while (packet_offset != 0) {
		if (packet_offset <= prev_offset
		    || packet_offset > buffer_length - 2) {
			printk (KERN_INFO "%s: bad inbound packet offset %u, "
				"prev %u, total %u\n", p->dev->name,
				packet_offset, prev_offset, buffer_length);
			p->stats.rx_errors++;
			break;
		} else {
			/* Kick the packet upstairs */
			iucv_rx (p->dev, mpi->ipmsgid,
				 buffer + prev_offset + 2,
				 packet_offset - prev_offset - 2);
			prev_offset = packet_offset;
			packet_offset = *((u16 *) (buffer + packet_offset));
		}
	}

	kfree (buffer);
	return;
}				/* end message_pending() */

/*-------------------------------------------------------------*/
/* Add meta-data to packet and send upstairs.                  */
/*-------------------------------------------------------------*/
static void
iucv_rx (net_device * dev, u32 msgid, uchar * buf, int len)
{
	struct iucv_priv *p = (struct iucv_priv *) dev->priv;
	struct sk_buff *skb;

#ifdef IPDEBUG
	printk (KERN_DEBUG "RX id=%i\n", msgid);
	dumpit (buf, 20);
	dumpit (buf + 20, 20);
#endif

	pr_debug ("%s: RX len=%u\n", p->dev->name, len);

	if (len > p->dev->mtu) {
		printk (KERN_INFO
			"%s: inbound packet id# %i length %u exceeds MTU %i\n",
			p->dev->name, msgid, len, p->dev->mtu);
		p->stats.rx_errors++;
		return;
	}

	skb = dev_alloc_skb (len);
	if (!skb) {
		p->stats.rx_dropped++;
		return;
	}

	/* If not enough room, skb_put will panic */
	memcpy (skb_put (skb, len), buf, len);

	/* Write metadata, and then pass to the receive level.  Since we */
	/* are not an Ethernet device, we have special fields to set.    */
	/* This is all boilerplace, not to be messed with.               */
	skb->dev = p->dev;	/* Set device       */
	skb->mac.raw = skb->data;	/* Point to packet  */
	skb->pkt_type = PACKET_HOST;	/* ..for this host. */
	skb->protocol = htons (ETH_P_IP);	/* IP packet        */
	skb->ip_summed = CHECKSUM_UNNECESSARY;	/* No checksum      */
	p->stats.rx_packets++;
	p->stats.rx_bytes += len;
	netif_rx (skb);

	return;
}				/* end  iucv_rx() */

/*-------------------------------------------------------------*/
/* TRANSMIT a packet.            	    Called by kernel.  */
/* This function deals with hw details of packet transmission. */
/*-------------------------------------------------------------*/
static int
iucv_tx (struct sk_buff *skb, net_device * dev)
{
	int rc, pktlen;
	u32 framelen, msgid;
	void *frame;
	struct iucv_priv *p = (struct iucv_priv *) dev->priv;

	if (skb == NULL) {	/* Nothing to do */
		printk (KERN_WARNING "%s: TX Kernel passed null sk_buffer\n",
			dev->name);
		p->stats.tx_dropped++;
		return -EIO;
	}

	if (netif_is_busy (dev))
		return -EBUSY;

	dev->trans_start = jiffies;	/* save the timestamp */

	/* IUCV frame will be released when MessageComplete   */
	/* interrupt is received.                             */
	pktlen = skb->len;
	framelen = pktlen + 4;

	frame = kmalloc (framelen, GFP_ATOMIC | GFP_DMA);
	if (!frame) {
		p->stats.tx_dropped++;
		dev_kfree_skb (skb);
		return 0;
	}

	netif_stop_queue (dev);	/* transmission is busy */

	*(u16 *) frame = pktlen + 2;	/* Set header   */
	memcpy (frame + 2, skb->data, pktlen);	/* Copy data    */
	memset (frame + pktlen + 2, 0, 2);	/* Set trailer  */

	/* Ok, now the frame is ready for transmission: send it. */
	rc = iucv_send (p->pathid, &msgid, 0, 0,
			(u32) frame,	/* Msg tag      */
			0,	/* No flags     */
			frame, framelen);
	if (rc == 0) {
#ifdef IPDEBUG
		printk (KERN_DEBUG "TX id=%i\n", msgid);
		dumpit (skb->data, 20);
		dumpit (skb->data + 20, 20);
#endif
		pr_debug ("%s: tx START %i.%i @=%p len=%i\n",
			  p->dev->name, p->pathid, msgid, frame, framelen);
		p->stats.tx_packets++;
	} else {
		if (rc == 3)	/* Exceeded MSGLIMIT */
			p->stats.tx_dropped++;
		else {
			p->stats.tx_errors++;
			printk (KERN_INFO "%s: tx ERROR id=%i.%i rc=%i\n",
				p->dev->name, p->pathid, msgid, rc);
		}
		/* We won't get interrupt.  Free frame now. */
		kfree (frame);
	}
	dev_kfree_skb (skb);	/* Finished with skb            */

	netif_wake_queue (p->dev);
	return 0;
}				/* end iucv_tx() */

/*-----------------------------------------------------------*/
/* SEND COMPLETE                    Called by IUCV handler.  */
/* Free the IUCV frame that was used for this transmission.  */
/*-----------------------------------------------------------*/
static void
send_complete (iucv_MessageComplete * mci, void *pgm_data)
{
	void *frame;
#ifdef DEBUG
	struct iucv_priv *p = (struct iucv_priv *) pgm_data;
#endif
	frame = (void *) (ulong) mci->ipmsgtag;
	kfree (frame);
	pr_debug ("%s: TX DONE %i.%i @=%p\n",
		  p->dev->name, mci->ippathid, mci->ipmsgid, frame);
}				/* end send_complete() */

/*-----------------------------------------------------------*/
/* STATISTICS reporting.                  Called by kernel.  */
/*-----------------------------------------------------------*/
static struct net_device_stats *
iucv_stats (net_device * dev)
{
	struct iucv_priv *p = (struct iucv_priv *) dev->priv;
	return &p->stats;
}				/* end iucv_stats() */

/*-----------------------------------------------------------*/
/* MTU change    .                        Called by kernel.  */
/* IUCV can handle mtu sizes from 576 (the IP architectural  */
/* minimum) up to maximum supported by VM.  I don't think IP */
/* pays attention to new mtu until device is restarted.      */
/*-----------------------------------------------------------*/
static int
iucv_change_mtu (net_device * dev, int new_mtu)
{
	if ((new_mtu < 576) || (new_mtu > MAX_VM_MTU))
		return -EINVAL;
	dev->mtu = new_mtu;
	return 0;
}				/* end iucv_change_mtu() */

/*-----------------------------------------------------------*/
/* INIT device.                           Called by kernel.  */
/* Called by register_netdev() in kernel.                    */
/*-----------------------------------------------------------*/
static int
iucv_init (net_device * dev)
{
	dev->open = iucv_start;
	dev->stop = iucv_stop;
	dev->hard_start_xmit = iucv_tx;
	dev->get_stats = iucv_stats;
	dev->change_mtu = iucv_change_mtu;
	dev->hard_header_len = 0;
	dev->addr_len = 0;
	dev->type = ARPHRD_SLIP;
	dev->tx_queue_len = MAX_TX_Q;	/* Default - updated based on IUCV */
	/* keep the default flags, just add NOARP and POINTOPOINT */
	dev->flags |= IFF_NOARP | IFF_POINTOPOINT;
	dev->mtu = 9216;

	dev_init_buffers (dev);
	pr_debug ("%s: iucv_init  dev@=%p\n", dev->name, dev);
	return 0;
}

#ifndef MODULE
/*-----------------------------------------------------------------*/
/* Process iucv=userid1,...,useridn kernel parameter.              */
/*                                                                 */
/* Each user id provided will be associated with device 'iucvnn'.  */
/* iucv_init will be called to initialize each device.             */
/*-----------------------------------------------------------------*/
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
#define init_return(a) return a
static int __init
iucv_setup (char *iucv)
#else
#define init_return(a) return
__initfunc (void iucv_setup (char *iucv, int *ints))
#endif
{
	int i, devnumber;
	char *s;
	char temp_userid[9];

	i = devnumber = 0;
	memset (temp_userid, ' ', 8);
	temp_userid[8] = '\0';
	printk (KERN_NOTICE "netiucv: IUCV network driver " LEVEL "\n");

	if (!iucv)
		init_return (0);

	for (s = iucv; *s != '\0'; s++) {
		if (*s == ' ')	/* Compress out blanks */
			continue;

		if (devnumber >= MAX_DEVICES) {
			printk (KERN_ERR "More than %i IUCV hosts specified\n",
				MAX_DEVICES);
			init_return (-ENODEV);
		}

		if (*s != ',') {
			temp_userid[i++] = *s;

			if (i == 8 || *(s + 1) == ',' || *(s + 1) == '\0') {
				register_iucv_dev (devnumber, temp_userid);
				devnumber++;
				i = 0;
				memset (temp_userid, ' ', 8);
				if (*(s + 1) != '\0')
					*(s + 1) = ' ';
			}
		}
	}			/* while */

	init_return (1);
}

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
__setup ("iucv=", iucv_setup);
#endif
#else				/* BUILT AS MODULE */
/*-------------------------------------------------------------------*/
/* Process iucv=userid1,...,useridn module paramter.                 */
/*                                                                   */
/* insmod passes the module an array of string pointers, each of     */
/* which points to a userid.  The commas are stripped out by insmod. */
/* MODULE_PARM defines the name of the array.  (See start of module.)*/
/*                                                                   */
/* Each user id provided will be associated with device 'iucvnn'.    */
/* iucv_init will be called to initialize each device.               */
/*-------------------------------------------------------------------*/
char *iucv[MAX_DEVICES] = { NULL };
int
init_module (void)
{
	int i;
	printk (KERN_NOTICE "netiucv: IUCV network driver " LEVEL "\n");
	for (i = 0; i < MAX_DEVICES; i++) {
		if (iucv[i] == NULL)
			break;
		register_iucv_dev (i, iucv[i]);
	}
	return 0;
}

void
cleanup_module (void)
{
	int i;
	for (i = 0; i < MAX_DEVICES; i++) {
		if (iucv[i])
			unregister_netdev (&iucv_netdev[i]);
	}
	return;
}
#endif				/* MODULE */

void
register_iucv_dev (int devnumber, char *userid)
{
	int rc;
	net_device *dev;

	memset (iucv_userid[devnumber], ' ', 8);
	memcpy (iucv_userid[devnumber], userid,
		min_t(unsigned int, strlen(userid), 8));
	dev = &iucv_netdev[devnumber];
	sprintf (dev->name, "iucv%i", devnumber);

	pr_debug ("netiucv: registering %s\n", dev->name);

	if ((rc = register_netdev (dev))) {
		printk (KERN_ERR
			"netiucv: register_netdev(%s) error %i\n",
			dev->name, rc);
	}
	return;
}

/* These structures are static because setup() can be called very */
/* early in kernel init if this module is built into the kernel.  */
/* Certainly no kmalloc() is available, probably no C runtime.    */
/* If support changed to be module only, this can all be done     */
/* dynamically.                                                   */
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
static char iucv_names[MAX_DEVICES][8];	/* Allows "iucvXXX" plus null */
#endif
net_device iucv_netdev[MAX_DEVICES] = {
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[0][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[1][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[2][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[3][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[4][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[5][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[6][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[7][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[8][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[9][0],  /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[10][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[11][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[12][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[13][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[14][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[15][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[16][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[17][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[18][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
	{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0))
		name: &iucv_names[19][0], /* Name filled in at load time  */
#endif
		init: iucv_init           /* probe function               */
	},
};