summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/i386/pc386/3c509/3c509.c
blob: cf06a85916db3c5a10032201891a763ceb5b4929 (plain) (blame)
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
/**********************************************************************************
 * $Header$
 *
 * Ported by Rosimildo da Silva.
 * ConnectTel,Inc.
 * e-mail: rdasilva@connecttel.com
 *
 * MODULE DESCRIPTION:
 * RTEMS driver for 3COM 3C509 Ethernet Card.
 * The driver has been tested on PC with a single network card.
 *
 *
 * This driver was based on the FreeBSD implementation( if_ep.c ) of the 3c5x9
 * family and on the network framework of the RTEMS network driver.
 * ( WD80x3 by  Eric Norum ).
 * See notes below:
 *
 ******************************************************************************
 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Herb Peyerl.
 * 4. The name of Herb Peyerl may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *******************************************************************************
 *
 * RTEMS driver for M68360 WD1 Ethernet
 *
 * W. Eric Norum
 * Saskatchewan Accelerator Laboratory
 * University of Saskatchewan
 * Saskatoon, Saskatchewan, CANADA
 * eric@skatter.usask.ca
 *******************************************************************************
 *
 *
 * MODIFICATION/HISTORY:
 * $Log$
 * Revision 1.2  1999/12/13 21:21:31  joel
 * Warning removal patch from Philip A. Prindeville <philipp@zembu.com>.
 *
 * Revision 1.1  1999/05/14 16:23:42  joel
 * Added 3COM 3C509 driver from Rosimildo DaSilva <rdasilva@connecttel.com>.
 *
 *
 **********************************************************************************/

#include <bsp.h>

#include <stdio.h>
#include <stdarg.h>
#include <rtems/error.h>
#include <rtems/rtems_bsdnet.h>

#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/libkern.h>

#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>

#include <irq.h>

/* Local includes */
#include "3c509.h"
#include "elink.h"

/* #define	ET_MINLEN 60 */		/* minimum message length */

/*
 * Number of WDs supported by this driver
 */
#define NWDDRIVER	1

/*
 * Default number of buffer descriptors set aside for this driver.
 * The number of transmit buffer descriptors has to be quite large
 * since a single frame often uses four or more buffer descriptors.
 */
/*
#define RX_BUF_COUNT     15
#define TX_BUF_COUNT     4
#define TX_BD_PER_BUF    4
*/

/*
 * RTEMS event used by interrupt handler to signal driver tasks.
 * This must not be any of the events used by the network task synchronization.
 */
#define INTERRUPT_EVENT			RTEMS_EVENT_1

/*
 * RTEMS event used to start transmit daemon.
 * This must not be the same as INTERRUPT_EVENT.
 */
#define START_TRANSMIT_EVENT	RTEMS_EVENT_2

/*
 * Receive buffer size -- Allow for a full ethernet packet including CRC
 */

/*
#define RBUF_SIZE	1520

#if (MCLBYTES < RBUF_SIZE)
# error "Driver must have MCLBYTES > RBUF_SIZE"
#endif
*/

/* network driver name */
#define NET_DRIVER_NAME		"ep"

/*
 * Per device structure.
 *
 * XXX Note:  id_conflicts should either become an array of things we're
 * specifically allowed to conflict with or be subsumed into some
 * more powerful mechanism for detecting and dealing with multiple types
 * of non-fatal conflict.  -jkh XXX
 */
struct isa_device
{
  int	id_id;		/* device id */
  int	id_unit;	/* unit number */
  int	id_iobase;	/* base i/o address */
  u_int	id_irq;		/* interrupt request */
};


struct ep_board
{
  int epb_addr;	        /* address of this board */
  char epb_used;		/* was this entry already used for configuring ? */
       					/* data from EEPROM for later use */
  u_short eth_addr[3];	/* Ethernet address */
  u_short prod_id;		/* product ID */
  u_short res_cfg;		/* resource configuration */
};


/*
 * Ethernet software status per interface.
 */
struct ep_softc
{
    struct arpcom arpcom;	/* Ethernet common part		 */
    int ep_io_addr;			/* i/o bus address		 	 */
    struct mbuf *top, *mcur;
    short cur_len;
    u_short ep_connectors;	/* Connectors on this card.	 */
    u_char ep_connector;	/* Configured connector.	 */
    int stat;				/* some flags 				 */
    struct ep_board *epb;
    int unit;

    rtems_irq_connect_data   irqInfo;
    rtems_id		     	 rxDaemonTid;
    rtems_id		     	 txDaemonTid;

    int              	 acceptBroadcast;

    short tx_underrun;
    short rx_no_first;
    short rx_no_mbuf;
    short rx_bpf_disc;
    short rx_overrunf;
    short rx_overrunl;
};


/*  static unsigned long loopc; */
static volatile unsigned long overrun;
static volatile unsigned long resend;
static struct ep_softc ep_softc[ NWDDRIVER ];
static struct isa_device isa_dev[ NWDDRIVER ] =
{
  { 0, 	   		/* device id 		  		*/
    0, 	   		/* unit number 		  		*/
    -1,	    	/* base i/o address   ???	*/
    0     		/* interrupt request  ???	*/
  }
};

static  u_long	ep_unit;
static  int	    ep_boards;
struct	ep_board ep_board[ EP_MAX_BOARDS + 1];
static	int ep_current_tag = EP_LAST_TAG + 1;
static	char *ep_conn_type[] = {"UTP", "AUI", "???", "BNC"};

#define ep_ftst(f) (sc->stat&(f))
#define ep_fset(f) (sc->stat|=(f))
#define ep_frst(f) (sc->stat&=~(f))


/* forward declarations for functions */
static int ep_attach( struct ep_softc *sc );
static int ep_isa_probe( struct isa_device *is );
static void epinit(  struct ep_softc *sc );
static void epread( register struct ep_softc *sc );
static void epstart( struct ifnet *ifp );
static void epread( register struct ep_softc *sc );
static int ep_isa_attach( struct isa_device *is );
static int get_eeprom_data( int id_port, int offset );
static void ep_intr( struct ep_softc *sc );

/* external functions */
extern void Wait_X_ms( unsigned int timeToWait );  /* timer.c ??? */


/**********************************************************************************
 *
 * DESCRIPTION: Writes a buffer of data to the I/O port. The data is sent to the
 *				port as 32 bits units( 4 bytes ).
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static __inline void outsl( unsigned short io_addr, unsigned char *out_data, int len )
{
   u_long *pl = ( u_long *)out_data;
   while( len-- )
   {
     outport_long( io_addr, *pl );
	 pl++;
   }
}

/**********************************************************************************
 *
 * DESCRIPTION: Writes a buffer of data to the I/O port. The data is sent to the
 *				port as 16 bits units( 2 bytes ).
 *
 * RETURNS:
 *
 **********************************************************************************/
static __inline void outsw( unsigned short io_addr, unsigned char *out_data, int len )
{
   u_short *ps = ( u_short *)out_data;
   while( len-- )
   {
     outport_word( io_addr, *ps );
	 ps++;
   }
}

/**********************************************************************************
 *
 * DESCRIPTION: Writes a buffer of data to the I/O port. The data is sent to the
 *				port as 8 bits units( 1 byte ).
 *
 * RETURNS: nothing
 *
 **********************************************************************************/
static __inline void outsb( unsigned short io_addr, unsigned char *out_data, int len )
{
   while( len-- )
   {
     outport_byte( io_addr, *out_data );
	 out_data++;
   }
}


/**********************************************************************************
 *
 * DESCRIPTION: Read a buffer of data from an I/O port. The data is read as 16 bits
 *			    units or 2 bytes.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static __inline void insw( unsigned short io_addr, unsigned char *in_data, int len )
{
   u_short *ps = ( u_short *)in_data;
   while( len-- )
   {
     inport_word( io_addr, *ps );
	 ps++;
   }
}

/**********************************************************************************
 *
 * DESCRIPTION: Read a buffer of data from an I/O port. The data is read as 32 bits
 *			    units or 4 bytes.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static __inline void insl( unsigned short io_addr, unsigned char *in_data, int len )
{
   u_long *pl = ( u_long *)in_data;
   while( len-- )
   {
     inport_long( io_addr, *pl );
	 pl++;
   }
}

/**********************************************************************************
 *
 * DESCRIPTION: Read a buffer of data from an I/O port. The data is read as 8 bits
 *			    units or 1 bytes.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static __inline void insb( unsigned short io_addr, unsigned char *in_data, int len )
{
   while( len-- )
   {
     inport_byte( io_addr, *in_data++ );
   }
}


/**********************************************************************************
 *
 * DESCRIPTION: Writes a word to the I/O port.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
/*
 * Routine to output a word as defined in FreeBSD.
 */
static __inline void outw( unsigned short io_addr, unsigned short out_data )
{
  outport_word( io_addr, out_data );
}


/**********************************************************************************
 *
 * DESCRIPTION:  Routine to read a word as defined in FreeBSD.
 *
 * RETURNS: nothing
 *
 **********************************************************************************/
static __inline unsigned short inw( unsigned short io_addr )
{
  unsigned short in_data;
  inport_word( io_addr, in_data );
  return in_data;
}

/**********************************************************************************
 *
 * DESCRIPTION: Routine to output a word as defined in FreeBSD.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
void __inline outb( unsigned short io_addr, unsigned char out_data )
{
  outport_byte( io_addr, out_data );
}

/**********************************************************************************
 *
 * DESCRIPTION: Routine to read a word as defined in FreeBSD.
 *
 * RETURNS: byte read.
 *
 **********************************************************************************/
static __inline unsigned char inb( unsigned short io_addr )
{
  unsigned char in_data;
  inport_byte( io_addr, in_data );
  return in_data;
}


/**********************************************************************************
 *
 * DESCRIPTION:
 * We get eeprom data from the id_port given an offset into the eeprom.
 * Basically; after the ID_sequence is sent to all of the cards; they enter
 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
 * the eeprom data.  We then read the port 16 times and with every read; the
 * cards check for contention (ie: if one card writes a 0 bit and another
 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
 * compares the data on the bus; if there is a difference then that card goes
 * into ID_WAIT state again). In the meantime; one bit of data is returned in
 * the AX register which is conveniently returned to us by inb().  Hence; we
 * read 16 times getting one bit of data with each read.
 *
 * RETURNS: 16 bit word from the EEPROM
 *
 **********************************************************************************/
static int get_eeprom_data( int id_port, int offset )
{
    int i, data = 0;
    outb(id_port, 0x80 + offset);
    Wait_X_ms( 1 );
    for (i = 0; i < 16; i++)
	    data = (data << 1) | (inw(id_port) & 1);
    return( data );
}


/**********************************************************************************
 *
 * DESCRIPTION: Waits until the EEPROM of the card is ready to be accessed.
 *
 * RETURNS: 0 - not ready; 1 - ok
 *
 **********************************************************************************/
static int eeprom_rdy( struct ep_softc *sc )
{
    int i;

    for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++)
	    continue;
    if (i >= MAX_EEPROMBUSY)
    {
	   printf("ep%d: eeprom failed to come ready.\n", sc->unit);
	   return (0);
    }
    return (1);
}

/**********************************************************************************
 *
 * DESCRIPTION:
 * get_e: gets a 16 bits word from the EEPROM.
 * We must have set the window before call this routine.
 *
 * RETURNS: data from EEPROM
 *
 **********************************************************************************/
u_short get_e(  struct ep_softc *sc, int offset )
{
    if( !eeprom_rdy(sc) )
	   return (0xffff);
    outw(BASE + EP_W0_EEPROM_COMMAND, EEPROM_CMD_RD | offset );
    if( !eeprom_rdy(sc) )
	   return( 0xffff );
    return( inw( BASE + EP_W0_EEPROM_DATA ) );
}


/**********************************************************************************
 *
 * DESCRIPTION:
 * Driver interrupt handler. This routine is called by the RTEMS kernel when this
 * interrupt is raised.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static rtems_isr ap_interrupt_handler( rtems_vector_number v )
{
  struct ep_softc *sc = (struct ep_softc *)&ep_softc[ 0 ];

  /* de-activate any pending interrrupt, and sent and event to interrupt task
   * to process all events required by this interrupt.
   */
  outw( BASE + EP_COMMAND, SET_INTR_MASK ); 	/* disable all Ints */
  rtems_event_send( sc->rxDaemonTid, INTERRUPT_EVENT );
}


/**********************************************************************************
 *
 * DESCRIPTION:
 *
 * RETURNS:
 *
 **********************************************************************************/
static void nopOn(const rtems_irq_connect_data* notUsed)
{
  /* does nothing */
}

/**********************************************************************************
 *
 * DESCRIPTION:
 *
 * RETURNS:
 *
 **********************************************************************************/
static int _3c509_IsOn(const rtems_irq_connect_data* irq)
{
  return BSP_irq_enabled_at_i8259s (irq->name);
}


/**********************************************************************************
 *
 * DESCRIPTION:
 * Initializes the ethernet hardware.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static void _3c509_initialize_hardware (struct ep_softc *sc)
{
  rtems_status_code st;

  epinit( sc );

  /*
   * Set up interrupts
   */
  sc->irqInfo.hdl = ( rtems_irq_hdl )ap_interrupt_handler;
  sc->irqInfo.on  = nopOn;
  sc->irqInfo.off = nopOn;
  sc->irqInfo.isOn = _3c509_IsOn;

  printf ("3c509: IRQ with Kernel: %d\n", sc->irqInfo.name );
  st = BSP_install_rtems_irq_handler( &sc->irqInfo );
  if( !st )
  {
    rtems_panic ("Can't attach WD interrupt handler for irq %d\n", sc->irqInfo.name );
  }
}

/**********************************************************************************
 *
 * DESCRIPTION: Driver interrupt daemon.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static void _3c509_rxDaemon (void *arg)
{
  struct ep_softc *dp = (struct ep_softc *)&ep_softc[ 0 ];
  rtems_event_set events;

  printf ("3C509: RX Daemon is starting.\n");
  for( ;; )
  {
    /* printk( "R-" ); */
    rtems_bsdnet_event_receive( INTERRUPT_EVENT,
				                 RTEMS_WAIT | RTEMS_EVENT_ANY,
				                 RTEMS_NO_TIMEOUT,
				                 &events );
    /* printk( "R+" ); */
    ep_intr( dp );
    epstart( &dp->arpcom.ac_if );
  }
  printf ("3C509: RX Daemon is finishing.\n");
}


/**********************************************************************************
 *
 * DESCRIPTION: Driver transmit daemon
 *
 * RETURNS:
 *
 **********************************************************************************/
static void _3c509_txDaemon (void *arg)
{
    struct ep_softc *sc = (struct ep_softc *)&ep_softc[0];
	struct ifnet *ifp = &sc->arpcom.ac_if;
	rtems_event_set events;

    printf ("3C509: TX Daemon is starting.\n");
    for( ;; )
	{
		/*
		 * Wait for packet
		 */
	  /* printk( "T-\n" ); */
		rtems_bsdnet_event_receive( START_TRANSMIT_EVENT,
									RTEMS_EVENT_ANY | RTEMS_WAIT,
									RTEMS_NO_TIMEOUT,
									&events );
		/*	printk( "T+\n" ); */
      epstart( ifp );
      while( ifp->if_flags & IFF_OACTIVE )
          epstart( ifp );
	}
   printf ("3C509: TX Daemon is finishing.\n");
}


/**********************************************************************************
 *
 * DESCRIPTION: Activates the trabsmitter task...
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static void _3c509_start (struct ifnet *ifp)
{
	struct ep_softc *sc = ifp->if_softc;
	/*    printk ("S");  */
	ifp->if_flags |= IFF_OACTIVE;
	rtems_event_send( sc->txDaemonTid, START_TRANSMIT_EVENT );
}

/**********************************************************************************
 *
 * DESCRIPTION: Initialize and start the device
 *
 * RETURNS:
 *
 **********************************************************************************/
static void _3c509_init (void *arg)
{
  struct ep_softc *sc = arg;
  struct ifnet *ifp = &sc->arpcom.ac_if;

  printf ("3C509: Initialization called.\n");
  if (sc->txDaemonTid == 0) {

    /*
     * Set up WD hardware
     */
    _3c509_initialize_hardware (sc);
    printf ("3C509: starting network driver tasks..\n");
    /*
     * Start driver tasks
     */
    sc->txDaemonTid = rtems_bsdnet_newproc ("APtx", 4096, _3c509_txDaemon, sc);
    sc->rxDaemonTid = rtems_bsdnet_newproc ("APrx", 4096, _3c509_rxDaemon, sc);
  }

  /*
   * Tell the world that we're running.
   */
   ifp->if_flags |= IFF_RUNNING;
}

/**********************************************************************************
 *
 * DESCRIPTION: Stop the device
 *
 * RETURNS:
 *
 **********************************************************************************/
static void _3c509_stop (struct ep_softc *sc)
{
  struct ifnet *ifp = &sc->arpcom.ac_if;
  ifp->if_flags &= ~IFF_RUNNING;

  printf ("3C509: stop() called.\n");
  /*
   * Stop the transmitter
   */
  outw(BASE + EP_COMMAND, RX_DISABLE);
  outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
  while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
  outw(BASE + EP_COMMAND, TX_DISABLE);
  outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
  outw(BASE + EP_COMMAND, RX_RESET);
  outw(BASE + EP_COMMAND, TX_RESET);
  while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
  outw(BASE + EP_COMMAND, C_INTR_LATCH);
  outw(BASE + EP_COMMAND, SET_RD_0_MASK);
  outw(BASE + EP_COMMAND, SET_INTR_MASK);
  outw(BASE + EP_COMMAND, SET_RX_FILTER);
}


/**********************************************************************************
 *
 * DESCRIPTION:  Show interface statistics
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static void _3c509_stats (struct ep_softc *sc)
{
  struct ifnet *ifp = &sc->arpcom.ac_if;
  printf ("3C509: stats() called.\n");
  printf("\tStat: %x\n", sc->stat);
  printf("\tIpackets=%ld, Opackets=%ld\n", ifp->if_ipackets, ifp->if_opackets);
  printf("\tNOF=%d, NOMB=%d, BPFD=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
           sc->rx_no_first, sc->rx_no_mbuf, sc->rx_bpf_disc, sc->rx_overrunf,
           sc->rx_overrunl, sc->tx_underrun );
}

/**********************************************************************************
 *
 * DESCRIPTION: Driver ioctl handler
 *
 * RETURNS:
 *
 **********************************************************************************/
static int _3c509_ioctl (struct ifnet *ifp, int command, caddr_t data)
{
	struct ep_softc *sc = ifp->if_softc;
	int error = 0;

    printf ("3C509: ioctl() called.\n");
	switch (command) {
	case SIOCGIFADDR:
	case SIOCSIFADDR:
		ether_ioctl (ifp, command, data);
		break;

	case SIOCSIFFLAGS:
		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
		case IFF_RUNNING:
			_3c509_stop (sc);
			break;

		case IFF_UP:
			_3c509_init (sc);
			break;

		case IFF_UP | IFF_RUNNING:
			_3c509_stop (sc);
			_3c509_init (sc);
			break;

		default:
			break;
		}
		break;

	case SIO_RTEMS_SHOW_STATS:
		_3c509_stats( sc );
		break;

	/*
	 * FIXME: All sorts of multicast commands need to be added here!
	 */
	default:
		error = EINVAL;
		break;
	}
	return error;
}

/**********************************************************************************
 *
 * DESCRIPTION:
 * Attaches this network driver to the system. This function is called by the network
 * interface during the initialization of the system.
 *
 * RETURNS: - 1 - success; 0 - fail to initialize
 *
 **********************************************************************************/
int rtems_3c509_driver_attach (struct rtems_bsdnet_ifconfig *config )
{
	struct ep_softc *sc;
	struct ifnet *ifp;
	int mtu;
	int i;

    printf ("3C509: attach() called.\n");

	/*
	 * init some variables
	 */
	overrun = 0;
	resend = 0;
    ep_unit = 0;
    ep_boards = 0;

	/*
	 * Find a free driver
	 */
	for (i = 0 ; i < NWDDRIVER ; i++) {
		sc = &ep_softc[i];
		ifp = &sc->arpcom.ac_if;
		if (ifp->if_softc == NULL)
			break;
	}
	if (i >= NWDDRIVER)
	{
		printf ("Too many 3C509 drivers.\n");
		return 0;
	}

	/*
	 * Process options
	 */
	if( config->hardware_address )
	{
	  memcpy (sc->arpcom.ac_enaddr, config->hardware_address, ETHER_ADDR_LEN);
	}
	else
	{
	  /* set it to something ... */
	  memset (sc->arpcom.ac_enaddr, 0x08,ETHER_ADDR_LEN);
	}
	if (config->mtu)
		mtu = config->mtu;
	else
		mtu = ETHERMTU;

	if (config->irno)
		sc->irqInfo.name = config->irno;
	else
		sc->irqInfo.name = 10;

	if (config->port)
		sc->ep_io_addr = config->port;
	else
		sc->ep_io_addr = 0x300;

	sc->acceptBroadcast = !config->ignore_broadcast;

    printf ("3C509: isa_probe() looking for a card...\n");
    if( !ep_isa_probe( &isa_dev[ 0 ] ) )
	{
	   printf ("3C509: isa_probe() fail to find a board.\n");
	   return 0;
	}

	/* A board has been found, so proceed with the installation of the driver */
    ep_isa_attach( &isa_dev[ 0 ] );
	/*
	 * Set up network interface values
	 */

	ifp->if_softc = sc;
	ifp->if_unit = i;
	ifp->if_name = NET_DRIVER_NAME;
	ifp->if_mtu = mtu;
	ifp->if_init = _3c509_init;
	ifp->if_ioctl = _3c509_ioctl;
	ifp->if_start = _3c509_start;
	ifp->if_output = ether_output;
    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	if( ifp->if_snd.ifq_maxlen == 0 )
	{
	   ifp->if_snd.ifq_maxlen = ifqmaxlen;
	}
 	/*
	 * Attach the interface
	 */
	if_attach (ifp);
	ether_ifattach (ifp);
    printf ("3C509: attach() is complete.\n");
	return 1;
}


/**********************************************************************************
 *
 * DESCRIPTION:
 * This function looks for a 3COM card 3c5x9 in an isa bus. If a board is found, it
 * returns a structure describing the caracteristics of the card. It returns zero when
 * card can not be found.
 *
 * RETURNS:  	0 - fail - could not find a card...
 *				<>  description of the card.
 *
 **********************************************************************************/
static struct ep_board *ep_look_for_board_at( struct isa_device *is )
{
    int data, i, j, id_port = ELINK_ID_PORT;
    int count = 0;

    if(ep_current_tag == (EP_LAST_TAG + 1) )
    {
    	/* Come here just one time */
		ep_current_tag--;

        /* Look for the ISA boards. Init and leave them actived */
		outb(id_port, 0);
		outb(id_port, 0);

		elink_idseq(0xCF);
		elink_reset();
      Wait_X_ms( 10 );  /* RPS: assuming delay in miliseconds */
		for (i = 0; i < EP_MAX_BOARDS; i++)
        {
	    	outb(id_port, 0);
		    outb(id_port, 0);
		    elink_idseq(0xCF);

	    	data = get_eeprom_data(id_port, EEPROM_MFG_ID);
		    if (data != MFG_ID)
				break;

		    /* resolve contention using the Ethernet address */
		    for (j = 0; j < 3; j++)
		        get_eeprom_data(id_port, j);

		    /* and save this address for later use */

		    for (j = 0; j < 3; j++)
			   ep_board[ep_boards].eth_addr[j] = get_eeprom_data(id_port, j);

		    ep_board[ep_boards].res_cfg = get_eeprom_data(id_port, EEPROM_RESOURCE_CFG);
		    ep_board[ep_boards].prod_id = get_eeprom_data(id_port, EEPROM_PROD_ID);
		    ep_board[ep_boards].epb_used = 0;
#ifdef PC98
		    ep_board[ep_boards].epb_addr =
				(get_eeprom_data(id_port, EEPROM_ADDR_CFG) & 0x1f) * 0x100 + 0x40d0;
#else
		    ep_board[ep_boards].epb_addr =
				(get_eeprom_data(id_port, EEPROM_ADDR_CFG) & 0x1f) * 0x10 + 0x200;
		    if (ep_board[ep_boards].epb_addr > 0x3E0)
				/* Board in EISA configuration mode */
				continue;
#endif /* PC98 */

		    outb(id_port, ep_current_tag);	/* tags board */
		    outb(id_port, ACTIVATE_ADAPTER_TO_CONFIG);
		    ep_boards++;
	    	count++;
		    ep_current_tag--;
		}
		ep_board[ep_boards].epb_addr = 0;
		if( count )
		{
	    	printf("%d 3C5x9 board(s) on ISA found at", count);
		    for (j = 0; ep_board[j].epb_addr; j++)
			   if( ep_board[j].epb_addr <= 0x3E0 )
			       printf(" 0x%x", ep_board[j].epb_addr );
 		    printf("\n");
		}
    }

    /* we have two cases:
     *
     *  1. Device was configured with 'port ?'
     *      In this case we search for the first unused card in list
     *
     *  2. Device was configured with 'port xxx'
     *      In this case we search for the unused card with that address
     *
     */

    if (IS_BASE == -1)
    { /* port? */
		for (i = 0; ep_board[i].epb_addr && ep_board[i].epb_used; i++) ;
		if (ep_board[i].epb_addr == 0)
		      return 0;

		IS_BASE = ep_board[i].epb_addr;
		ep_board[i].epb_used = 1;
		return &ep_board[ i ];
    }
    else
    {
		for (i = 0; ep_board[i].epb_addr && ep_board[i].epb_addr != IS_BASE;  i++ ) ;
		if (ep_board[i].epb_used || ep_board[i].epb_addr != IS_BASE)
			    return 0;
		if (inw(IS_BASE + EP_W0_EEPROM_COMMAND) & EEPROM_TST_MODE)
		{
	    	printf("ep%d: 3c5x9 at 0x%x in PnP mode. Disable PnP mode!\n",
	    			is->id_unit, IS_BASE );
		}
		ep_board[i].epb_used = 1;
		return &ep_board[i];
    }
}



/**********************************************************************************
 *
 * DESCRIPTION:
 * This routine checks if there card installed on the machine.
 *
 * RETURNS: 0 - no card founded.
 *			16 - size of the IO range for the card.
 *
 **********************************************************************************/
static int ep_isa_probe( struct isa_device *is )
{
    struct ep_softc *sc;
    struct ep_board *epb;
    u_short k;

    /* try to find a 3COM 3c5x9 .... */
    if( (epb = ep_look_for_board_at(is)) == 0 )
		return (0);

    sc = &ep_softc[ 0 ];
    sc->ep_io_addr = epb->epb_addr;
    sc->epb = epb;

    /*
     * The iobase was found and MFG_ID was 0x6d50. PROD_ID should be
     * 0x9[0-f]50	(IBM-PC)
     * 0x9[0-f]5[0-f]	(PC-98)
     */
    GO_WINDOW(0);
    k = sc->epb->prod_id;
#ifdef PC98
    if ((k & 0xf0f0) != (PROD_ID & 0xf0f0))
    {
#else
    if ((k & 0xf0ff) != (PROD_ID & 0xf0ff))
    {
#endif
	    printf("ep_isa_probe: ignoring model %04x\n", k );
/*        ep_unit--;  */
        return (0);
    }
    k = sc->epb->res_cfg;
    k >>= 12;

    /* Now we have two cases again:
     *
     *  1. Device was configured with 'irq?'
     *      In this case we use irq read from the board
     *
     *  2. Device was configured with 'irq xxx'
     *      In this case we set up the board to use specified interrupt
     *
     */

    if (is->id_irq == 0)
    {    /* irq? */
       is->id_irq = ( k == 2 ) ? 9 : k;
    }

    sc->stat = 0;	/* 16 bit access */

    /* By now, the adapter is already activated */

    return (EP_IOSIZE);	 /* 16 bytes of I/O space used. */
}



/**********************************************************************************
 *
 * DESCRIPTION:
 * This routine attaches this network driver and the network interface routines.
 *
 * RETURNS: 0 - failed to attach
 *			1 - success
 *
 **********************************************************************************/
static int ep_isa_attach( struct isa_device *is )
{
    struct ep_softc *sc = &ep_softc[ 0 ];
    u_short config;
    int irq;

    sc->ep_connectors = 0;
    config = inw( IS_BASE + EP_W0_CONFIG_CTRL );
    if (config & IS_AUI)
    {
	   sc->ep_connectors |= AUI;
    }
    if (config & IS_BNC)
    {
	   sc->ep_connectors |= BNC;
    }
    if (config & IS_UTP)
    {
	   sc->ep_connectors |= UTP;
    }
    if( !(sc->ep_connectors & 7) )
       printf( "no connectors!" );
    sc->ep_connector = inw(BASE + EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS;

    /*
     * Write IRQ value to board
     */

    irq = is->id_irq;
	/* update the interrupt line number to registered with kernel */
	sc->irqInfo.name = irq;

    GO_WINDOW( 0 );
    SET_IRQ( BASE, irq );

    printf( "3C509: I/O=0x%x, IRQ=%d, CONNECTOR=%s, ",
	    sc->ep_io_addr, sc->irqInfo.name,ep_conn_type[ sc->ep_connector ] );

    ep_attach( sc );
    return 1;
}

/**********************************************************************************
 *
 * DESCRIPTION: Completes the initialization/attachement of the driver.
 *
 * RETURNS: 0 - ok.
 *
 **********************************************************************************/
static int ep_attach( struct ep_softc *sc )
{
    u_short *p;
    int i;

    /*
     * Setup the station address
     */
    p = (u_short *) &sc->arpcom.ac_enaddr;
    GO_WINDOW(2);
    printf("ADDRESS=" );
    for (i = 0; i < 3; i++)
    {
	    p[i] = htons( sc->epb->eth_addr[i] );
        outw( BASE + EP_W2_ADDR_0 + (i * 2), ntohs( p[i] ) );
        printf("%04x ", (u_short)ntohs( p[i] ) );
    }
    printf("\n" );

    sc->rx_no_first = sc->rx_no_mbuf =
	sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
	sc->tx_underrun = 0;

    ep_fset( F_RX_FIRST );
    sc->top = sc->mcur = 0;
    return 0;
}


/**********************************************************************************
 *
 * DESCRIPTION:
 * Initializes the card.
 * The order in here seems important. Otherwise we may not receive interrupts. ?!
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static void epinit( struct ep_softc *sc )
{
    register struct ifnet *ifp = &sc->arpcom.ac_if;
    int i, j;

    while( inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS ) ;
    GO_WINDOW(0);
    outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
    GO_WINDOW(4);
    outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
    GO_WINDOW(0);

    /* Disable the card */
    outw(BASE + EP_W0_CONFIG_CTRL, 0);

    /* Enable the card */
    outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);

    GO_WINDOW(2);

    /* Reload the ether_addr. */
    for (i = 0; i < 6; i++)
       outb(BASE + EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);

    outw(BASE + EP_COMMAND, RX_RESET);
    outw(BASE + EP_COMMAND, TX_RESET);
    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);

    /* Window 1 is operating window */
    GO_WINDOW(1);
    for (i = 0; i < 31; i++)
       inb(BASE + EP_W1_TX_STATUS);

    /* get rid of stray intr's */
    outw(BASE + EP_COMMAND, ACK_INTR | 0xff);

    outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);

    outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);

    if (ifp->if_flags & IFF_PROMISC)
	   outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
	 						    FIL_GROUP | FIL_BRDCST | FIL_ALL);
    else
	   outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | FIL_GROUP | FIL_BRDCST);

     /*
      * S.B.
      *
      * Now behavior was slightly changed:
      *
      * if any of flags link[0-2] is used and its connector is
      * physically present the following connectors are used:
      *
      *   link0 - AUI * highest precedence
      *   link1 - BNC
      *   link2 - UTP * lowest precedence
      *
      * If none of them is specified then
      * connector specified in the EEPROM is used
      * (if present on card or AUI if not).
      *
      */

    /* Set the xcvr. */
    if (ifp->if_flags & IFF_LINK0 && sc->ep_connectors & AUI)
    {
	   i = ACF_CONNECTOR_AUI;
    }
    else if (ifp->if_flags & IFF_LINK1 && sc->ep_connectors & BNC)
    {
	   i = ACF_CONNECTOR_BNC;
    }
    else if (ifp->if_flags & IFF_LINK2 && sc->ep_connectors & UTP)
    {
	   i = ACF_CONNECTOR_UTP;
    }
    else
    {
	   i = sc->ep_connector;
    }
    GO_WINDOW(0);
    j = inw(BASE + EP_W0_ADDRESS_CFG) & 0x3fff;
    outw(BASE + EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));

    switch(i)
    {
      case ACF_CONNECTOR_UTP:
	  if (sc->ep_connectors & UTP)
	  {
	    GO_WINDOW(4);
	    outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
	  }
	  break;

      case ACF_CONNECTOR_BNC:
	   if (sc->ep_connectors & BNC)
	   {
	    outw(BASE + EP_COMMAND, START_TRANSCEIVER);
	    Wait_X_ms( 1 );
	   }
       break;

      case ACF_CONNECTOR_AUI:
  	    /* nothing to do */
		break;

      default:
	   printf("ep%d: strange connector type in EEPROM: assuming AUI\n", sc->unit);
	   break;
    }

    outw(BASE + EP_COMMAND, RX_ENABLE);
    outw(BASE + EP_COMMAND, TX_ENABLE);

    ifp->if_flags |= IFF_RUNNING;
    ifp->if_flags &= ~IFF_OACTIVE;	/* just in case */


    sc->rx_no_first = sc->rx_no_mbuf =
	sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
	sc->tx_underrun = 0;

    ep_fset(F_RX_FIRST);
    if( sc->top )
    {
	   m_freem( sc->top );
	   sc->top = sc->mcur = 0;
    }
    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
    outw(BASE + EP_COMMAND, SET_TX_START_THRESH | 16);

    /*
     * Store up a bunch of mbuf's for use later. (MAX_MBS). First we free up
     * any that we had in case we're being called from intr or somewhere
     * else.
     */

    GO_WINDOW(1);
}


static const char padmap[] = {0, 3, 2, 1};

/**********************************************************************************
 *
 * DESCRIPTION: Routine to transmit frames to the card.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static void epstart( struct ifnet *ifp )
{
    register struct ep_softc *sc = ifp->if_softc;
    register u_int len;
    register struct mbuf *m;
    struct mbuf *top;
    int pad;

    while( inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS )
        ;
startagain:
    /*    printk( "S-" ); */

    /* Sneak a peek at the next packet */
    m = ifp->if_snd.ifq_head;
    if (m == 0)
    {
       ifp->if_flags &= ~IFF_OACTIVE;
       return;
    }

    for( len = 0, top = m; m; m = m->m_next )
         len += m->m_len;

    pad = padmap[ len & 3 ];

    /*
     * The 3c509 automatically pads short packets to minimum ethernet length,
     * but we drop packets that are too large. Perhaps we should truncate
     * them instead?
     */
    if( len + pad > ETHER_MAX_LEN )
    {
	   /* packet is obviously too large: toss it */
       ++ifp->if_oerrors;
       IF_DEQUEUE( &ifp->if_snd, m );
       m_freem( m );
   	   goto readcheck;
    }
    if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4)
    {
	   /* no room in FIFO */
	   outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
       /* make sure */
	   if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4)
	   {
	       ifp->if_flags |= IFF_OACTIVE;
	       return;
	   }
    }
    IF_DEQUEUE( &ifp->if_snd, m );
    outw(BASE + EP_W1_TX_PIO_WR_1, len);
    outw(BASE + EP_W1_TX_PIO_WR_1, 0x0);	/* Second dword meaningless */

    for (top = m; m != 0; m = m->m_next)
	{
       if( ep_ftst(F_ACCESS_32_BITS ) )
       {
	      outsl( BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 4 );
	      if( m->m_len & 3 )
             outsb(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t) + (m->m_len & (~3)), m->m_len & 3 );
       }
       else
       {
	      outsw( BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 2 );
	      if( m->m_len & 1 )
		  outb( BASE + EP_W1_TX_PIO_WR_1, *(mtod(m, caddr_t) + m->m_len - 1) );
	   }
	}
    while( pad-- )
	{
  	   outb(BASE + EP_W1_TX_PIO_WR_1, 0);	/* Padding */
	}
    ifp->if_timer = 2;
    ifp->if_opackets++;
    m_freem(top);

/*    goto startagain;   */
    /*
     * Is another packet coming in? We don't want to overflow the tiny RX
     * fifo.
     */
readcheck:
    if( inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK )
    {
	/*
	 * we check if we have packets left, in that case we prepare to come
	 * back later
	 */
	   if( ifp->if_snd.ifq_head )
	   {
	      outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
       }
	   return;
    }
    goto startagain;
}



/**********************************************************************************
 *
 * DESCRIPTION: Routine to read frames from the card.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static void epread( register struct ep_softc *sc )
{
    struct ether_header *eh;
    struct mbuf *top, *mcur, *m;
    struct ifnet *ifp;
    int lenthisone;

    short rx_fifo2, status;
    register short rx_fifo;

    ifp = &sc->arpcom.ac_if;
    status = inw( BASE + EP_W1_RX_STATUS );

read_again:

    if (status & ERR_RX)
    {
	   ++ifp->if_ierrors;
	   if( status & ERR_RX_OVERRUN )
	   {
	    /*
	     * we can think the rx latency is actually greather than we
	     * expect
	     */
	    if( ep_ftst(F_RX_FIRST) )
 		   sc->rx_overrunf++;
	    else
		   sc->rx_overrunl++;

	   }
	   goto out;
    }
    rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;

    if( ep_ftst( F_RX_FIRST ) )
    {
	   MGETHDR( m, M_DONTWAIT, MT_DATA );
       if( !m )
	      goto out;
	   if( rx_fifo >= MINCLSIZE )
	      MCLGET( m, M_DONTWAIT );
	   sc->top = sc->mcur = top = m;
#define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
#define EOFF    (EROUND - sizeof(struct ether_header))
	   top->m_data += EOFF;

  	   /* Read what should be the header. */
	   insw(BASE + EP_W1_RX_PIO_RD_1, mtod(top, caddr_t), sizeof(struct ether_header) / 2);
	   top->m_len = sizeof(struct ether_header);
	   rx_fifo -= sizeof(struct ether_header);
	   sc->cur_len = rx_fifo2;
    }
    else
    {
	   /* come here if we didn't have a complete packet last time */
	   top = sc->top;
	   m = sc->mcur;
	   sc->cur_len += rx_fifo2;
    }

    /* Reads what is left in the RX FIFO */
    while (rx_fifo > 0)
    {
	   lenthisone = min( rx_fifo, M_TRAILINGSPACE(m) );
	   if( lenthisone == 0 )
	   {	/* no room in this one */
	       mcur = m;
	       MGET(m, M_WAIT, MT_DATA);
	       if (!m)
		      goto out;
	       if (rx_fifo >= MINCLSIZE)
		       MCLGET(m, M_WAIT);
	       m->m_len = 0;
	       mcur->m_next = m;
	       lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
	   }
	   if( ep_ftst( F_ACCESS_32_BITS ) )
	   { /* default for EISA configured cards*/
	      insl( BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len, lenthisone / 4);
	      m->m_len += (lenthisone & ~3);
	      if (lenthisone & 3)
		     insb(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len, lenthisone & 3);
	      m->m_len += (lenthisone & 3);
	    }
	    else
	    {
	      insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len, lenthisone / 2);
	      m->m_len += lenthisone;
	      if( lenthisone & 1 )
		     *(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
	   }
	   rx_fifo -= lenthisone;
    }

    if( status & ERR_RX_INCOMPLETE)
    {	/* we haven't received the complete packet */
	    sc->mcur = m;
     	sc->rx_no_first++;	/* to know how often we come here */
     	ep_frst( F_RX_FIRST );
	    if( !((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE) )
	    {
	      /* we see if by now, the packet has completly arrived */
	      goto read_again;
	    }
	    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
	    return;
    }
    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
    ++ifp->if_ipackets;
    ep_fset(F_RX_FIRST);
    top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
    top->m_pkthdr.len = sc->cur_len;

    eh = mtod(top, struct ether_header *);
    m_adj(top, sizeof(struct ether_header));
    ether_input(ifp, eh, top);
    sc->top = 0;
    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
          ;
    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
    return;

out:
    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
    if (sc->top)
    {
	   m_freem(sc->top);
	   sc->top = 0;
       sc->rx_no_mbuf++;
    }
    ep_fset(F_RX_FIRST);
    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS) ;
    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
}



/**********************************************************************************
 *
 * DESCRIPTION:
 * This routine handles interrupts. It is called from the "RX" task whenever
 * the ISR post an event to the task.
 * This is basically the "isr" from the FreeBSD driver.
 *
 * RETURNS: nothing.
 *
 **********************************************************************************/
static void ep_intr( struct ep_softc *sc )
{
  register int status;
  struct ifnet *ifp;
  ifp = &sc->arpcom.ac_if;

rescan:

  /*  printk( "I-" ); */
  while( ( status = inw(BASE + EP_STATUS)) & S_5_INTS )
  {
	/* first acknowledge all interrupt sources */
    outw( BASE + EP_COMMAND, ACK_INTR | ( status & S_MASK ) );

	if( status & ( S_RX_COMPLETE | S_RX_EARLY ) )
	{
	    epread( sc );
	    continue;
	}
	if (status & S_TX_AVAIL)
	{
	    /* we need ACK */
	    ifp->if_timer = 0;
	    ifp->if_flags &= ~IFF_OACTIVE;
	    GO_WINDOW(1);
	    inw(BASE + EP_W1_FREE_TX);
	    epstart(ifp);
	}
	if (status & S_CARD_FAILURE)
	{
	    ifp->if_timer = 0;
	    printf("\nep%d:\n\tStatus: %x\n", sc->unit, status);
	    GO_WINDOW(4);
	    printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
	    printf("\tStat: %x\n", sc->stat);
	    printf("\tIpackets=%ld, Opackets=%ld\n", ifp->if_ipackets, ifp->if_opackets);
	    printf("\tNOF=%d, NOMB=%d, BPFD=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
		   sc->rx_no_first, sc->rx_no_mbuf, sc->rx_bpf_disc, sc->rx_overrunf,
		   sc->rx_overrunl, sc->tx_underrun);

	    printf("ep%d: Status: %x (input buffer overflow)\n", sc->unit, status);
	    ++ifp->if_ierrors;
	    epinit(sc);
	    return;
	}
	if (status & S_TX_COMPLETE)
	{
	    ifp->if_timer = 0;
	    /* we  need ACK. we do it at the end */
	    /*
	     * We need to read TX_STATUS until we get a 0 status in order to
	     * turn off the interrupt flag.
	     */
	    while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE)
	    {
		   if (status & TXS_SUCCES_INTR_REQ)
		        ;
		   else if( status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION ) )
		   {
		      outw(BASE + EP_COMMAND, TX_RESET);
		      if (status & TXS_UNDERRUN)
		      {
			     sc->tx_underrun++;
		      }
		      else
		      {
			      if( status & TXS_JABBER )
			           ;
		          else	/* TXS_MAX_COLLISION - we shouldn't get here */
			           ++ifp->if_collisions;
		      }
		      ++ifp->if_oerrors;
		      outw(BASE + EP_COMMAND, TX_ENABLE);
		     /*
		      * To have a tx_avail_int but giving the chance to the
		      * Reception
		      */
		      if( ifp->if_snd.ifq_head )
		      {
			     outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
		      }
		   }
		   outb( BASE + EP_W1_TX_STATUS, 0x0 );	/* pops up the next status */
	    } /* while */
	    ifp->if_flags &= ~IFF_OACTIVE;
	    GO_WINDOW(1);
	    inw(BASE + EP_W1_FREE_TX);
	    epstart( ifp );
	}  /* end TX_COMPLETE */
  }
  outw(BASE + EP_COMMAND, C_INTR_LATCH);	/* ACK int Latch */
  if( (status = inw(BASE + EP_STATUS) ) & S_5_INTS )
      goto rescan;

  /* re-enable Ints */
  outw( BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS );
  /* printk( "I+" ); */
}