ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / eth16i.c
1 /* eth16i.c An ICL EtherTeam 16i and 32 EISA ethernet driver for Linux
2    
3    Written 1994-1999 by Mika Kuoppala
4    
5    Copyright (C) 1994-1999 by Mika Kuoppala
6    Based on skeleton.c and heavily on at1700.c by Donald Becker
7
8    This software may be used and distributed according to the terms
9    of the GNU General Public License, incorporated herein by reference.
10
11    The author may be reached as miku@iki.fi
12
13    This driver supports following cards :
14         - ICL EtherTeam 16i
15         - ICL EtherTeam 32 EISA 
16           (Uses true 32 bit transfers rather than 16i compability mode)
17
18    Example Module usage:
19         insmod eth16i.o io=0x2a0 mediatype=bnc
20
21         mediatype can be one of the following: bnc,tp,dix,auto,eprom
22
23         'auto' will try to autoprobe mediatype.
24         'eprom' will use whatever type defined in eprom.
25
26    I have benchmarked driver with PII/300Mhz as a ftp client
27    and 486/33Mhz as a ftp server. Top speed was 1128.37 kilobytes/sec.
28    
29    Sources:
30      - skeleton.c  a sample network driver core for linux,
31        written by Donald Becker <becker@scyld.com>
32      - at1700.c a driver for Allied Telesis AT1700, written 
33        by Donald Becker.
34      - e16iSRV.asm a Netware 3.X Server Driver for ICL EtherTeam16i
35        written by Markku Viima
36      - The Fujitsu MB86965 databook.
37    
38    Author thanks following persons due to their valueble assistance:    
39         Markku Viima (ICL)
40         Ari Valve (ICL)      
41         Donald Becker
42         Kurt Huwig <kurt@huwig.de>
43
44    Revision history:
45
46    Version      Date            Description
47    
48    0.01         15.12-94        Initial version (card detection)
49    0.02         23.01-95        Interrupt is now hooked correctly
50    0.03         01.02-95        Rewrote initialization part
51    0.04         07.02-95        Base skeleton done...
52                                 Made a few changes to signature checking
53                                 to make it a bit reliable.
54                                 - fixed bug in tx_buf mapping
55                                 - fixed bug in initialization (DLC_EN
56                                   wasn't enabled when initialization
57                                   was done.)
58    0.05         08.02-95        If there were more than one packet to send,
59                                 transmit was jammed due to invalid
60                                 register write...now fixed
61    0.06         19.02-95        Rewrote interrupt handling        
62    0.07         13.04-95        Wrote EEPROM read routines
63                                 Card configuration now set according to
64                                 data read from EEPROM
65    0.08         23.06-95        Wrote part that tries to probe used interface
66                                 port if AUTO is selected
67
68    0.09         01.09-95        Added module support
69    
70    0.10         04.09-95        Fixed receive packet allocation to work
71                                 with kernels > 1.3.x
72       
73    0.20         20.09-95        Added support for EtherTeam32 EISA      
74
75    0.21         17.10-95        Removed the unnecessary extern 
76                                 init_etherdev() declaration. Some
77                                 other cleanups.
78                                 
79    0.22         22.02-96        Receive buffer was not flushed
80                                 correctly when faulty packet was
81                                 received. Now fixed.
82
83    0.23         26.02-96        Made resetting the adapter      
84                                 more reliable.
85    
86    0.24         27.02-96        Rewrote faulty packet handling in eth16i_rx
87
88    0.25         22.05-96        kfree() was missing from cleanup_module.
89
90    0.26         11.06-96        Sometimes card was not found by 
91                                 check_signature(). Now made more reliable.
92    
93    0.27         23.06-96        Oops. 16 consecutive collisions halted 
94                                 adapter. Now will try to retransmit 
95                                 MAX_COL_16 times before finally giving up.
96    
97    0.28         28.10-97        Added dev_id parameter (NULL) for free_irq
98
99    0.29         29.10-97        Multiple card support for module users
100
101    0.30         30.10-97        Fixed irq allocation bug.
102                                 (request_irq moved from probe to open)
103
104    0.30a        21.08-98        Card detection made more relaxed. Driver
105                                 had problems with some TCP/IP-PROM boots
106                                 to find the card. Suggested by 
107                                 Kurt Huwig <kurt@huwig.de>
108
109    0.31         28.08-98        Media interface port can now be selected
110                                 with module parameters or kernel
111                                 boot parameters. 
112
113    0.32         31.08-98        IRQ was never freed if open/close 
114                                 pair wasn't called. Now fixed.
115    
116    0.33         10.09-98        When eth16i_open() was called after
117                                 eth16i_close() chip never recovered.
118                                 Now more shallow reset is made on
119                                 close.
120
121    0.34         29.06-99        Fixed one bad #ifdef.
122                                 Changed ioaddr -> io for consistency
123
124    0.35         01.07-99        transmit,-receive bytes were never
125                                 updated in stats. 
126
127    Bugs:
128         In some cases the media interface autoprobing code doesn't find 
129         the correct interface type. In this case you can 
130         manually choose the interface type in DOS with E16IC.EXE which is 
131         configuration software for EtherTeam16i and EtherTeam32 cards.
132         This is also true for IRQ setting. You cannot use module
133         parameter to configure IRQ of the card (yet). 
134
135    To do:
136         - Real multicast support
137         - Rewrite the media interface autoprobing code. Its _horrible_ !
138         - Possibly merge all the MB86965 specific code to external
139           module for use by eth16.c and Donald's at1700.c
140         - IRQ configuration with module parameter. I will do
141           this when i will get enough info about setting
142           irq without configuration utility.
143 */
144
145 static char *version = 
146     "eth16i.c: v0.35 01-Jul-1999 Mika Kuoppala (miku@iki.fi)\n";
147
148 #include <linux/module.h>
149 #include <linux/kernel.h>
150 #include <linux/types.h>                  
151 #include <linux/fcntl.h>                  
152 #include <linux/interrupt.h>              
153 #include <linux/ioport.h>                 
154 #include <linux/in.h>             
155 #include <linux/slab.h>           
156 #include <linux/string.h>                 
157 #include <linux/errno.h>
158 #include <linux/init.h>
159 #include <linux/spinlock.h>
160 #include <linux/netdevice.h>
161 #include <linux/etherdevice.h>
162 #include <linux/skbuff.h>
163
164 #include <asm/system.h>           
165 #include <asm/bitops.h>           
166 #include <asm/io.h>               
167 #include <asm/dma.h>
168
169
170
171 /* Few macros */
172 #define BIT(a)                 ( (1 << (a)) )  
173 #define BITSET(ioaddr, bnum)   ((outb(((inb(ioaddr)) | (bnum)), ioaddr))) 
174 #define BITCLR(ioaddr, bnum)   ((outb(((inb(ioaddr)) & (~(bnum))), ioaddr)))
175
176 /* This is the I/O address space for Etherteam 16i adapter. */
177 #define ETH16I_IO_EXTENT       32
178
179 /* Ticks before deciding that transmit has timed out */
180 #define TX_TIMEOUT             (400*HZ/1000)
181
182 /* Maximum loop count when receiving packets */
183 #define MAX_RX_LOOP            20
184
185 /* Some interrupt masks */
186 #define ETH16I_INTR_ON         0xef8a       /* Higher is receive mask */
187 #define ETH16I_INTR_OFF        0x0000
188          
189 /* Buffers header status byte meanings */
190 #define PKT_GOOD               BIT(5)
191 #define PKT_GOOD_RMT           BIT(4)
192 #define PKT_SHORT              BIT(3)
193 #define PKT_ALIGN_ERR          BIT(2)
194 #define PKT_CRC_ERR            BIT(1)
195 #define PKT_RX_BUF_OVERFLOW    BIT(0)
196
197 /* Transmit status register (DLCR0) */
198 #define TX_STATUS_REG          0
199 #define TX_DONE                BIT(7)
200 #define NET_BUSY               BIT(6)
201 #define TX_PKT_RCD             BIT(5)
202 #define CR_LOST                BIT(4)
203 #define TX_JABBER_ERR          BIT(3)
204 #define COLLISION              BIT(2)
205 #define COLLISIONS_16          BIT(1)
206
207 /* Receive status register (DLCR1) */
208 #define RX_STATUS_REG          1
209 #define RX_PKT                 BIT(7)  /* Packet received */
210 #define BUS_RD_ERR             BIT(6)
211 #define SHORT_PKT_ERR          BIT(3)
212 #define ALIGN_ERR              BIT(2)
213 #define CRC_ERR                BIT(1)
214 #define RX_BUF_OVERFLOW        BIT(0)
215               
216 /* Transmit Interrupt Enable Register (DLCR2) */
217 #define TX_INTR_REG            2
218 #define TX_INTR_DONE           BIT(7)
219 #define TX_INTR_COL            BIT(2)
220 #define TX_INTR_16_COL         BIT(1)
221
222 /* Receive Interrupt Enable Register (DLCR3) */
223 #define RX_INTR_REG            3
224 #define RX_INTR_RECEIVE        BIT(7)
225 #define RX_INTR_SHORT_PKT      BIT(3)
226 #define RX_INTR_CRC_ERR        BIT(1)
227 #define RX_INTR_BUF_OVERFLOW   BIT(0)
228
229 /* Transmit Mode Register (DLCR4) */
230 #define TRANSMIT_MODE_REG      4
231 #define LOOPBACK_CONTROL       BIT(1)
232 #define CONTROL_OUTPUT         BIT(2)
233
234 /* Receive Mode Register (DLCR5) */
235 #define RECEIVE_MODE_REG       5
236 #define RX_BUFFER_EMPTY        BIT(6)
237 #define ACCEPT_BAD_PACKETS     BIT(5)
238 #define RECEIVE_SHORT_ADDR     BIT(4)
239 #define ACCEPT_SHORT_PACKETS   BIT(3)
240 #define REMOTE_RESET           BIT(2)
241
242 #define ADDRESS_FILTER_MODE    BIT(1) | BIT(0)
243 #define REJECT_ALL             0
244 #define ACCEPT_ALL             3
245 #define MODE_1                 1            /* NODE ID, BC, MC, 2-24th bit */
246 #define MODE_2                 2            /* NODE ID, BC, MC, Hash Table */
247
248 /* Configuration Register 0 (DLCR6) */
249 #define CONFIG_REG_0           6
250 #define DLC_EN                 BIT(7)
251 #define SRAM_CYCLE_TIME_100NS  BIT(6)
252 #define SYSTEM_BUS_WIDTH_8     BIT(5)       /* 1 = 8bit, 0 = 16bit */
253 #define BUFFER_WIDTH_8         BIT(4)       /* 1 = 8bit, 0 = 16bit */
254 #define TBS1                   BIT(3)       
255 #define TBS0                   BIT(2)
256 #define SRAM_BS1               BIT(1)       /* 00=8kb,  01=16kb  */
257 #define SRAM_BS0               BIT(0)       /* 10=32kb, 11=64kb  */
258
259 #ifndef ETH16I_TX_BUF_SIZE                   /* 0 = 2kb, 1 = 4kb  */ 
260 #define ETH16I_TX_BUF_SIZE     3             /* 2 = 8kb, 3 = 16kb */
261 #endif                                      
262 #define TX_BUF_1x2048          0
263 #define TX_BUF_2x2048          1
264 #define TX_BUF_2x4098          2
265 #define TX_BUF_2x8192          3
266
267 /* Configuration Register 1 (DLCR7) */
268 #define CONFIG_REG_1           7
269 #define POWERUP                BIT(5)
270
271 /* Transmit start register */
272 #define TRANSMIT_START_REG     10
273 #define TRANSMIT_START_RB      2
274 #define TX_START               BIT(7)       /* Rest of register bit indicate*/
275                                             /* number of packets in tx buffer*/
276 /* Node ID registers (DLCR8-13) */
277 #define NODE_ID_0              8
278 #define NODE_ID_RB             0
279
280 /* Hash Table registers (HT8-15) */
281 #define HASH_TABLE_0           8
282 #define HASH_TABLE_RB          1
283
284 /* Buffer memory ports */
285 #define BUFFER_MEM_PORT_LB     8
286 #define DATAPORT               BUFFER_MEM_PORT_LB
287 #define BUFFER_MEM_PORT_HB     9
288
289 /* 16 Collision control register (BMPR11) */
290 #define COL_16_REG             11
291 #define HALT_ON_16             0x00
292 #define RETRANS_AND_HALT_ON_16 0x02
293
294 /* Maximum number of attempts to send after 16 concecutive collisions */
295 #define MAX_COL_16             10
296
297 /* DMA Burst and Transceiver Mode Register (BMPR13) */
298 #define TRANSCEIVER_MODE_REG   13
299 #define TRANSCEIVER_MODE_RB    2         
300 #define IO_BASE_UNLOCK         BIT(7)
301 #define LOWER_SQUELCH_TRESH    BIT(6)
302 #define LINK_TEST_DISABLE      BIT(5)
303 #define AUI_SELECT             BIT(4)
304 #define DIS_AUTO_PORT_SEL      BIT(3)
305
306 /* Filter Self Receive Register (BMPR14)  */
307 #define FILTER_SELF_RX_REG     14
308 #define SKIP_RX_PACKET         BIT(2)
309 #define FILTER_SELF_RECEIVE    BIT(0)
310
311 /* EEPROM Control Register (BMPR 16) */
312 #define EEPROM_CTRL_REG        16
313
314 /* EEPROM Data Register (BMPR 17) */
315 #define EEPROM_DATA_REG        17
316
317 /* NMC93CSx6 EEPROM Control Bits */
318 #define CS_0                   0x00
319 #define CS_1                   0x20
320 #define SK_0                   0x00
321 #define SK_1                   0x40
322 #define DI_0                   0x00
323 #define DI_1                   0x80
324
325 /* NMC93CSx6 EEPROM Instructions */
326 #define EEPROM_READ            0x80
327
328 /* NMC93CSx6 EEPROM Addresses */
329 #define E_NODEID_0             0x02
330 #define E_NODEID_1             0x03
331 #define E_NODEID_2             0x04
332 #define E_PORT_SELECT          0x14
333   #define E_PORT_BNC           0x00
334   #define E_PORT_DIX           0x01
335   #define E_PORT_TP            0x02
336   #define E_PORT_AUTO          0x03
337   #define E_PORT_FROM_EPROM    0x04
338 #define E_PRODUCT_CFG          0x30
339  
340
341 /* Macro to slow down io between EEPROM clock transitions */
342 #define eeprom_slow_io() do { int _i = 40; while(--_i > 0) { inb(0x80); }}while(0)
343
344 /* Jumperless Configuration Register (BMPR19) */
345 #define JUMPERLESS_CONFIG      19
346
347 /* ID ROM registers, writing to them also resets some parts of chip */
348 #define ID_ROM_0               24
349 #define ID_ROM_7               31
350 #define RESET                  ID_ROM_0
351
352 /* This is the I/O address list to be probed when seeking the card */
353 static unsigned int eth16i_portlist[] __initdata = {
354         0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300, 0 
355 };
356
357 static unsigned int eth32i_portlist[] __initdata = { 
358         0x1000, 0x2000, 0x3000, 0x4000, 0x5000, 0x6000, 0x7000, 0x8000,
359         0x9000, 0xA000, 0xB000, 0xC000, 0xD000, 0xE000, 0xF000, 0 
360 };
361
362 /* This is the Interrupt lookup table for Eth16i card */
363 static unsigned int eth16i_irqmap[] __initdata = { 9, 10, 5, 15, 0 };
364 #define NUM_OF_ISA_IRQS    4
365
366 /* This is the Interrupt lookup table for Eth32i card */
367 static unsigned int eth32i_irqmap[] __initdata = { 3, 5, 7, 9, 10, 11, 12, 15, 0 };  
368 #define EISA_IRQ_REG    0xc89
369 #define NUM_OF_EISA_IRQS   8
370
371 static unsigned int eth16i_tx_buf_map[] = { 2048, 2048, 4096, 8192 };
372
373 /* Use 0 for production, 1 for verification, >2 for debug */
374 #ifndef ETH16I_DEBUG
375 #define ETH16I_DEBUG 0
376 #endif
377 static unsigned int eth16i_debug = ETH16I_DEBUG;
378
379 /* Information for each board */
380
381 struct eth16i_local {
382         struct net_device_stats stats;
383         unsigned char     tx_started;
384         unsigned char     tx_buf_busy;
385         unsigned short    tx_queue;  /* Number of packets in transmit buffer */
386         unsigned short    tx_queue_len;         
387         unsigned int      tx_buf_size;
388         unsigned long     open_time;
389         unsigned long     tx_buffered_packets;
390         unsigned long     tx_buffered_bytes;
391         unsigned long     col_16;
392         spinlock_t        lock;
393 };
394
395 /* Function prototypes */
396
397 static int     eth16i_probe1(struct net_device *dev, int ioaddr);
398 static int     eth16i_check_signature(int ioaddr);
399 static int     eth16i_probe_port(int ioaddr);
400 static void    eth16i_set_port(int ioaddr, int porttype);
401 static int     eth16i_send_probe_packet(int ioaddr, unsigned char *b, int l);
402 static int     eth16i_receive_probe_packet(int ioaddr);
403 static int     eth16i_get_irq(int ioaddr);
404 static int     eth16i_read_eeprom(int ioaddr, int offset);
405 static int     eth16i_read_eeprom_word(int ioaddr);
406 static void    eth16i_eeprom_cmd(int ioaddr, unsigned char command);
407 static int     eth16i_open(struct net_device *dev);
408 static int     eth16i_close(struct net_device *dev);
409 static int     eth16i_tx(struct sk_buff *skb, struct net_device *dev);
410 static void    eth16i_rx(struct net_device *dev);
411 static void    eth16i_timeout(struct net_device *dev);
412 static irqreturn_t eth16i_interrupt(int irq, void *dev_id, struct pt_regs *regs);
413 static void    eth16i_reset(struct net_device *dev);
414 static void    eth16i_timeout(struct net_device *dev);
415 static void    eth16i_skip_packet(struct net_device *dev);
416 static void    eth16i_multicast(struct net_device *dev); 
417 static void    eth16i_select_regbank(unsigned char regbank, int ioaddr);
418 static void    eth16i_initialize(struct net_device *dev, int boot);
419
420 #if 0
421 static int     eth16i_set_irq(struct net_device *dev);
422 #endif
423
424 #ifdef MODULE
425 static ushort  eth16i_parse_mediatype(const char* s);
426 #endif
427
428 static struct net_device_stats *eth16i_get_stats(struct net_device *dev);
429
430 static char cardname[] __initdata = "ICL EtherTeam 16i/32";
431
432 static int __init do_eth16i_probe(struct net_device *dev)
433 {
434         int i;
435         int ioaddr;
436         int base_addr = dev->base_addr;
437     
438         SET_MODULE_OWNER(dev);
439
440         if(eth16i_debug > 4) 
441                 printk(KERN_DEBUG "Probing started for %s\n", cardname);
442
443         if(base_addr > 0x1ff)           /* Check only single location */
444                 return eth16i_probe1(dev, base_addr);
445         else if(base_addr != 0)         /* Don't probe at all */
446                 return -ENXIO;
447
448         /* Seek card from the ISA io address space */
449         for(i = 0; (ioaddr = eth16i_portlist[i]) ; i++)
450                 if(eth16i_probe1(dev, ioaddr) == 0)
451                         return 0;
452
453         /* Seek card from the EISA io address space */
454         for(i = 0; (ioaddr = eth32i_portlist[i]) ; i++)
455                 if(eth16i_probe1(dev, ioaddr) == 0)
456                         return 0;
457
458         return -ENODEV;
459 }
460
461 struct net_device * __init eth16i_probe(int unit)
462 {
463         struct net_device *dev = alloc_etherdev(sizeof(struct eth16i_local));
464         int err;
465
466         if (!dev)
467                 return ERR_PTR(-ENOMEM);
468
469         sprintf(dev->name, "eth%d", unit);
470         netdev_boot_setup_check(dev);
471
472         err = do_eth16i_probe(dev);
473         if (err)
474                 goto out;
475         err = register_netdev(dev);
476         if (err)
477                 goto out1;
478         return dev;
479 out1:
480         free_irq(dev->irq, dev);
481         release_region(dev->base_addr, ETH16I_IO_EXTENT);
482 out:
483         free_netdev(dev);
484         return ERR_PTR(err);
485 }
486
487 static int __init eth16i_probe1(struct net_device *dev, int ioaddr)
488 {
489         struct eth16i_local *lp = netdev_priv(dev);
490         static unsigned version_printed;
491         int retval;
492
493         /* Let's grab the region */
494         if (!request_region(ioaddr, ETH16I_IO_EXTENT, dev->name))
495                 return -EBUSY;
496
497         /*
498           The MB86985 chip has on register which holds information in which 
499           io address the chip lies. First read this register and compare
500           it to our current io address and if match then this could
501           be our chip.
502           */
503
504         if(ioaddr < 0x1000) {
505                 if(eth16i_portlist[(inb(ioaddr + JUMPERLESS_CONFIG) & 0x07)] 
506                    != ioaddr) {
507                         retval = -ENODEV;
508                         goto out;
509                 }
510         }
511
512         /* Now we will go a bit deeper and try to find the chip's signature */
513
514         if(eth16i_check_signature(ioaddr) != 0) {
515                 retval = -ENODEV;
516                 goto out;
517         }
518
519         /* 
520            Now it seems that we have found a ethernet chip in this particular
521            ioaddr. The MB86985 chip has this feature, that when you read a 
522            certain register it will increase it's io base address to next
523            configurable slot. Now when we have found the chip, first thing is
524            to make sure that the chip's ioaddr will hold still here.
525            */
526
527         eth16i_select_regbank(TRANSCEIVER_MODE_RB, ioaddr);
528         outb(0x00, ioaddr + TRANSCEIVER_MODE_REG);
529
530         outb(0x00, ioaddr + RESET);             /* Reset some parts of chip */
531         BITSET(ioaddr + CONFIG_REG_0, BIT(7));  /* Disable the data link */
532
533         if( (eth16i_debug & version_printed++) == 0)
534                 printk(KERN_INFO "%s", version);
535
536         dev->base_addr = ioaddr;
537         dev->irq = eth16i_get_irq(ioaddr);
538
539         /* Try to obtain interrupt vector */
540
541         if ((retval = request_irq(dev->irq, (void *)&eth16i_interrupt, 0, dev->name, dev))) {
542                 printk(KERN_WARNING "%s: %s at %#3x, but is unusable due conflicting IRQ %d.\n", 
543                        dev->name, cardname, ioaddr, dev->irq);
544                 goto out;
545         }
546
547         printk(KERN_INFO "%s: %s at %#3x, IRQ %d, ",
548                dev->name, cardname, ioaddr, dev->irq);
549
550
551         /* Now we will have to lock the chip's io address */
552         eth16i_select_regbank(TRANSCEIVER_MODE_RB, ioaddr);
553         outb(0x38, ioaddr + TRANSCEIVER_MODE_REG); 
554
555         eth16i_initialize(dev, 1); /* Initialize rest of the chip's registers */
556
557         /* Now let's same some energy by shutting down the chip ;) */
558         BITCLR(ioaddr + CONFIG_REG_1, POWERUP);
559
560         /* Initialize the device structure */
561         memset(lp, 0, sizeof(struct eth16i_local));
562         dev->open               = eth16i_open;
563         dev->stop               = eth16i_close;
564         dev->hard_start_xmit    = eth16i_tx;
565         dev->get_stats          = eth16i_get_stats;
566         dev->set_multicast_list = eth16i_multicast;
567         dev->tx_timeout         = eth16i_timeout;
568         dev->watchdog_timeo     = TX_TIMEOUT;
569         spin_lock_init(&lp->lock);
570         return 0;
571 out:
572         release_region(ioaddr, ETH16I_IO_EXTENT);
573         return retval;
574 }
575
576
577 static void eth16i_initialize(struct net_device *dev, int boot)
578 {
579         int ioaddr = dev->base_addr;
580         int i, node_w = 0;
581         unsigned char node_byte = 0;
582
583         /* Setup station address */
584         eth16i_select_regbank(NODE_ID_RB, ioaddr);
585         for(i = 0 ; i < 3 ; i++) {
586                 unsigned short node_val = eth16i_read_eeprom(ioaddr, E_NODEID_0 + i);
587                 ((unsigned short *)dev->dev_addr)[i] = ntohs(node_val);
588         }
589
590         for(i = 0; i < 6; i++) { 
591                 outb( ((unsigned char *)dev->dev_addr)[i], ioaddr + NODE_ID_0 + i);
592                 if(boot) {
593                         printk("%02x", inb(ioaddr + NODE_ID_0 + i));
594                         if(i != 5)
595                                 printk(":");
596                 }
597         }
598
599         /* Now we will set multicast addresses to accept none */
600         eth16i_select_regbank(HASH_TABLE_RB, ioaddr);
601         for(i = 0; i < 8; i++) 
602                 outb(0x00, ioaddr + HASH_TABLE_0 + i);
603
604         /*
605           Now let's disable the transmitter and receiver, set the buffer ram 
606           cycle time, bus width and buffer data path width. Also we shall
607           set transmit buffer size and total buffer size.
608           */
609
610         eth16i_select_regbank(2, ioaddr);
611
612         node_byte = 0;
613         node_w = eth16i_read_eeprom(ioaddr, E_PRODUCT_CFG);
614
615         if( (node_w & 0xFF00) == 0x0800)
616                 node_byte |= BUFFER_WIDTH_8;
617
618         node_byte |= SRAM_BS1;
619
620         if( (node_w & 0x00FF) == 64)
621                 node_byte |= SRAM_BS0;
622
623         node_byte |= DLC_EN | SRAM_CYCLE_TIME_100NS | (ETH16I_TX_BUF_SIZE << 2);
624
625         outb(node_byte, ioaddr + CONFIG_REG_0);
626
627         /* We shall halt the transmitting, if 16 collisions are detected */
628         outb(HALT_ON_16, ioaddr + COL_16_REG);
629
630 #ifdef MODULE
631         /* if_port already set by init_module() */
632 #else
633         dev->if_port = (dev->mem_start < E_PORT_FROM_EPROM) ? 
634                 dev->mem_start : E_PORT_FROM_EPROM;
635 #endif
636
637         /* Set interface port type */
638         if(boot) {
639                 char *porttype[] = {"BNC", "DIX", "TP", "AUTO", "FROM_EPROM" };
640
641                 switch(dev->if_port)
642                 {
643
644                 case E_PORT_FROM_EPROM:
645                         dev->if_port = eth16i_read_eeprom(ioaddr, E_PORT_SELECT);
646                         break;
647
648                 case E_PORT_AUTO:
649                         dev->if_port = eth16i_probe_port(ioaddr);
650                         break;
651                         
652                 case E_PORT_BNC:
653                 case E_PORT_TP:
654                 case E_PORT_DIX:
655                         break;
656                 }
657
658                 printk(" %s interface.\n", porttype[dev->if_port]);
659
660                 eth16i_set_port(ioaddr, dev->if_port);
661         }
662
663         /* Set Receive Mode to normal operation */
664         outb(MODE_2, ioaddr + RECEIVE_MODE_REG);
665 }
666
667 static int eth16i_probe_port(int ioaddr)
668 {
669         int i;
670         int retcode;
671         unsigned char dummy_packet[64];
672
673         /* Powerup the chip */
674         outb(0xc0 | POWERUP, ioaddr + CONFIG_REG_1);
675
676         BITSET(ioaddr + CONFIG_REG_0, DLC_EN);
677
678         eth16i_select_regbank(NODE_ID_RB, ioaddr);
679
680         for(i = 0; i < 6; i++) {
681                 dummy_packet[i] = inb(ioaddr + NODE_ID_0 + i);
682                 dummy_packet[i+6] = inb(ioaddr + NODE_ID_0 + i);
683         }
684
685         dummy_packet[12] = 0x00;
686         dummy_packet[13] = 0x04;
687         memset(dummy_packet + 14, 0, sizeof(dummy_packet) - 14);
688
689         eth16i_select_regbank(2, ioaddr);
690
691         for(i = 0; i < 3; i++) {
692                 BITSET(ioaddr + CONFIG_REG_0, DLC_EN);
693                 BITCLR(ioaddr + CONFIG_REG_0, DLC_EN);
694                 eth16i_set_port(ioaddr, i);
695
696                 if(eth16i_debug > 1)
697                         printk(KERN_DEBUG "Set port number %d\n", i);
698
699                 retcode = eth16i_send_probe_packet(ioaddr, dummy_packet, 64);
700                 if(retcode == 0) {
701                         retcode = eth16i_receive_probe_packet(ioaddr);
702                         if(retcode != -1) {
703                                 if(eth16i_debug > 1)
704                                         printk(KERN_DEBUG "Eth16i interface port found at %d\n", i);
705                                 return i;
706                         }
707                 }
708                 else {
709                         if(eth16i_debug > 1)
710                                 printk(KERN_DEBUG "TRANSMIT_DONE timeout when probing interface port\n");
711                 }
712         }
713
714         if( eth16i_debug > 1)
715                 printk(KERN_DEBUG "Using default port\n");
716
717         return E_PORT_BNC;
718 }
719
720 static void eth16i_set_port(int ioaddr, int porttype)
721
722         unsigned short temp = 0;
723
724         eth16i_select_regbank(TRANSCEIVER_MODE_RB, ioaddr);
725         outb(LOOPBACK_CONTROL, ioaddr + TRANSMIT_MODE_REG);
726
727         temp |= DIS_AUTO_PORT_SEL;
728
729         switch(porttype) {
730
731         case E_PORT_BNC :
732                 temp |= AUI_SELECT;
733                 break;
734
735         case E_PORT_TP :
736                 break;
737
738         case E_PORT_DIX :
739                 temp |= AUI_SELECT;
740                 BITSET(ioaddr + TRANSMIT_MODE_REG, CONTROL_OUTPUT);
741                 break;
742         }  
743
744         outb(temp, ioaddr + TRANSCEIVER_MODE_REG);
745
746         if(eth16i_debug > 1) {
747                 printk(KERN_DEBUG "TRANSMIT_MODE_REG = %x\n", inb(ioaddr + TRANSMIT_MODE_REG));
748                 printk(KERN_DEBUG "TRANSCEIVER_MODE_REG = %x\n", 
749                        inb(ioaddr+TRANSCEIVER_MODE_REG));
750         }
751 }
752
753 static int eth16i_send_probe_packet(int ioaddr, unsigned char *b, int l)
754 {
755         int starttime;
756
757         outb(0xff, ioaddr + TX_STATUS_REG);
758
759         outw(l, ioaddr + DATAPORT);
760         outsw(ioaddr + DATAPORT, (unsigned short *)b, (l + 1) >> 1);  
761
762         starttime = jiffies;
763         outb(TX_START | 1, ioaddr + TRANSMIT_START_REG); 
764
765         while( (inb(ioaddr + TX_STATUS_REG) & 0x80) == 0) {
766                 if( (jiffies - starttime) > TX_TIMEOUT) {
767                         return -1;
768                 }
769         }
770
771         return 0;
772 }
773
774 static int eth16i_receive_probe_packet(int ioaddr)
775 {
776         int starttime;
777
778         starttime = jiffies;
779
780         while((inb(ioaddr + TX_STATUS_REG) & 0x20) == 0) {
781                 if( (jiffies - starttime) > TX_TIMEOUT) {
782
783                         if(eth16i_debug > 1)
784                                 printk(KERN_DEBUG "Timeout occurred waiting transmit packet received\n");
785                         starttime = jiffies;
786                         while((inb(ioaddr + RX_STATUS_REG) & 0x80) == 0) {
787                                 if( (jiffies - starttime) > TX_TIMEOUT) {
788                                         if(eth16i_debug > 1)
789                                                 printk(KERN_DEBUG "Timeout occurred waiting receive packet\n");
790                                         return -1;
791                                 }
792                         }
793
794                         if(eth16i_debug > 1)
795                                 printk(KERN_DEBUG "RECEIVE_PACKET\n");
796                         return(0); /* Found receive packet */
797                 }
798         }
799
800         if(eth16i_debug > 1) {
801                 printk(KERN_DEBUG "TRANSMIT_PACKET_RECEIVED %x\n", inb(ioaddr + TX_STATUS_REG));
802                 printk(KERN_DEBUG "RX_STATUS_REG = %x\n", inb(ioaddr + RX_STATUS_REG));
803         }
804
805         return(0); /* Return success */
806 }
807
808 #if 0
809 static int eth16i_set_irq(struct net_device* dev)
810 {
811         const int ioaddr = dev->base_addr;
812         const int irq = dev->irq;
813         int i = 0;
814
815         if(ioaddr < 0x1000) {           
816                 while(eth16i_irqmap[i] && eth16i_irqmap[i] != irq)
817                         i++;
818         
819                 if(i < NUM_OF_ISA_IRQS) {
820                         u8 cbyte = inb(ioaddr + JUMPERLESS_CONFIG);
821                         cbyte = (cbyte & 0x3F) | (i << 6);
822                         outb(cbyte, ioaddr + JUMPERLESS_CONFIG);
823                         return 0;
824                 }
825         }
826         else {
827                 printk(KERN_NOTICE "%s: EISA Interrupt cannot be set. Use EISA Configuration utility.\n", dev->name);
828         }
829         
830         return -1;
831
832 }
833 #endif
834
835 static int __init eth16i_get_irq(int ioaddr)
836 {
837         unsigned char cbyte;
838
839         if( ioaddr < 0x1000) {
840                 cbyte = inb(ioaddr + JUMPERLESS_CONFIG);
841                 return( eth16i_irqmap[ ((cbyte & 0xC0) >> 6) ] );
842         } else {  /* Oh..the card is EISA so method getting IRQ different */
843                 unsigned short index = 0;
844                 cbyte = inb(ioaddr + EISA_IRQ_REG);
845                 while( (cbyte & 0x01) == 0) {
846                         cbyte = cbyte >> 1;
847                         index++;
848                 }
849                 return( eth32i_irqmap[ index ] );
850         }
851 }
852
853 static int __init eth16i_check_signature(int ioaddr)
854 {
855         int i;
856         unsigned char creg[4] = { 0 };
857
858         for(i = 0; i < 4 ; i++) {
859
860                 creg[i] = inb(ioaddr + TRANSMIT_MODE_REG + i);
861
862                 if(eth16i_debug > 1)
863                         printk("eth16i: read signature byte %x at %x\n", 
864                                creg[i],
865                                ioaddr + TRANSMIT_MODE_REG + i);
866         }
867
868         creg[0] &= 0x0F;      /* Mask collision cnr */
869         creg[2] &= 0x7F;      /* Mask DCLEN bit */
870
871 #if 0
872         /* 
873            This was removed because the card was sometimes left to state
874            from which it couldn't be find anymore. If there is need
875            to more strict check still this have to be fixed.
876            */
877         if( ! ((creg[0] == 0x06) && (creg[1] == 0x41)) ) {
878                 if(creg[1] != 0x42)
879                         return -1;
880         }
881 #endif
882
883         if( !((creg[2] == 0x36) && (creg[3] == 0xE0)) ) {
884                 creg[2] &= 0x40;
885                 creg[3] &= 0x03;
886                 
887                 if( !((creg[2] == 0x40) && (creg[3] == 0x00)) )
888                         return -1;
889         }
890         
891         if(eth16i_read_eeprom(ioaddr, E_NODEID_0) != 0)
892                 return -1;
893         
894         if((eth16i_read_eeprom(ioaddr, E_NODEID_1) & 0xFF00) != 0x4B00)
895                 return -1;
896
897         return 0;
898 }
899
900 static int eth16i_read_eeprom(int ioaddr, int offset)
901 {
902         int data = 0;
903
904         eth16i_eeprom_cmd(ioaddr, EEPROM_READ | offset);
905         outb(CS_1, ioaddr + EEPROM_CTRL_REG);
906         data = eth16i_read_eeprom_word(ioaddr);
907         outb(CS_0 | SK_0, ioaddr + EEPROM_CTRL_REG);
908
909         return(data);  
910 }
911
912 static int eth16i_read_eeprom_word(int ioaddr)
913 {
914         int i;
915         int data = 0;
916      
917         for(i = 16; i > 0; i--) {
918                 outb(CS_1 | SK_0, ioaddr + EEPROM_CTRL_REG);
919                 eeprom_slow_io();
920                 outb(CS_1 | SK_1, ioaddr + EEPROM_CTRL_REG);
921                 eeprom_slow_io();
922                 data = (data << 1) | 
923                         ((inb(ioaddr + EEPROM_DATA_REG) & DI_1) ? 1 : 0);
924                 
925                 eeprom_slow_io();
926         }
927
928         return(data);
929 }
930
931 static void eth16i_eeprom_cmd(int ioaddr, unsigned char command)
932 {
933         int i;
934
935         outb(CS_0 | SK_0, ioaddr + EEPROM_CTRL_REG);
936         outb(DI_0, ioaddr + EEPROM_DATA_REG);
937         outb(CS_1 | SK_0, ioaddr + EEPROM_CTRL_REG);
938         outb(DI_1, ioaddr + EEPROM_DATA_REG);
939         outb(CS_1 | SK_1, ioaddr + EEPROM_CTRL_REG);
940
941         for(i = 7; i >= 0; i--) {
942                 short cmd = ( (command & (1 << i)) ? DI_1 : DI_0 );
943                 outb(cmd, ioaddr + EEPROM_DATA_REG);
944                 outb(CS_1 | SK_0, ioaddr + EEPROM_CTRL_REG);
945                 eeprom_slow_io();
946                 outb(CS_1 | SK_1, ioaddr + EEPROM_CTRL_REG);
947                 eeprom_slow_io();
948         } 
949 }
950
951 static int eth16i_open(struct net_device *dev)
952 {
953         struct eth16i_local *lp = netdev_priv(dev);
954         int ioaddr = dev->base_addr;
955         
956         /* Powerup the chip */
957         outb(0xc0 | POWERUP, ioaddr + CONFIG_REG_1);
958
959         /* Initialize the chip */
960         eth16i_initialize(dev, 0);  
961
962         /* Set the transmit buffer size */
963         lp->tx_buf_size = eth16i_tx_buf_map[ETH16I_TX_BUF_SIZE & 0x03];
964
965         if(eth16i_debug > 0)
966                 printk(KERN_DEBUG "%s: transmit buffer size %d\n", 
967                        dev->name, lp->tx_buf_size);
968
969         /* Now enable Transmitter and Receiver sections */
970         BITCLR(ioaddr + CONFIG_REG_0, DLC_EN);
971
972         /* Now switch to register bank 2, for run time operation */
973         eth16i_select_regbank(2, ioaddr);
974
975         lp->open_time = jiffies;
976         lp->tx_started = 0;
977         lp->tx_queue = 0;
978         lp->tx_queue_len = 0;
979
980         /* Turn on interrupts*/
981         outw(ETH16I_INTR_ON, ioaddr + TX_INTR_REG);  
982
983         netif_start_queue(dev);
984         return 0;
985 }
986
987 static int eth16i_close(struct net_device *dev)
988 {
989         struct eth16i_local *lp = netdev_priv(dev);
990         int ioaddr = dev->base_addr;
991
992         eth16i_reset(dev);
993
994         /* Turn off interrupts*/
995         outw(ETH16I_INTR_OFF, ioaddr + TX_INTR_REG);  
996
997         netif_stop_queue(dev);
998         
999         lp->open_time = 0;
1000
1001         /* Disable transmit and receive */
1002         BITSET(ioaddr + CONFIG_REG_0, DLC_EN);
1003
1004         /* Reset the chip */
1005         /* outb(0xff, ioaddr + RESET); */
1006         /* outw(0xffff, ioaddr + TX_STATUS_REG);    */
1007         
1008         outb(0x00, ioaddr + CONFIG_REG_1);
1009
1010         return 0;
1011 }
1012
1013 static void eth16i_timeout(struct net_device *dev)
1014 {
1015         struct eth16i_local *lp = netdev_priv(dev);
1016         int ioaddr = dev->base_addr;
1017         /* 
1018            If we get here, some higher level has decided that 
1019            we are broken. There should really be a "kick me" 
1020            function call instead. 
1021            */
1022
1023         outw(ETH16I_INTR_OFF, ioaddr + TX_INTR_REG);
1024         printk(KERN_WARNING "%s: transmit timed out with status %04x, %s ?\n", 
1025                dev->name,
1026         inw(ioaddr + TX_STATUS_REG),  (inb(ioaddr + TX_STATUS_REG) & TX_DONE) ? 
1027                        "IRQ conflict" : "network cable problem");
1028
1029         dev->trans_start = jiffies;
1030
1031         /* Let's dump all registers */
1032         if(eth16i_debug > 0) { 
1033                 printk(KERN_DEBUG "%s: timeout: %02x %02x %02x %02x %02x %02x %02x %02x.\n",
1034                        dev->name, inb(ioaddr + 0), 
1035                        inb(ioaddr + 1), inb(ioaddr + 2), 
1036                        inb(ioaddr + 3), inb(ioaddr + 4), 
1037                        inb(ioaddr + 5),
1038                        inb(ioaddr + 6), inb(ioaddr + 7));
1039
1040                 printk(KERN_DEBUG "%s: transmit start reg: %02x. collision reg %02x\n",
1041                        dev->name, inb(ioaddr + TRANSMIT_START_REG),
1042                        inb(ioaddr + COL_16_REG));
1043                         printk(KERN_DEBUG "lp->tx_queue = %d\n", lp->tx_queue);
1044                 printk(KERN_DEBUG "lp->tx_queue_len = %d\n", lp->tx_queue_len);
1045                 printk(KERN_DEBUG "lp->tx_started = %d\n", lp->tx_started);
1046         }
1047         lp->stats.tx_errors++;
1048         eth16i_reset(dev);
1049         dev->trans_start = jiffies;
1050         outw(ETH16I_INTR_ON, ioaddr + TX_INTR_REG);
1051         netif_wake_queue(dev);
1052 }
1053
1054 static int eth16i_tx(struct sk_buff *skb, struct net_device *dev)
1055 {
1056         struct eth16i_local *lp = netdev_priv(dev);
1057         int ioaddr = dev->base_addr;
1058         int status = 0;
1059         ushort length = skb->len;
1060         unsigned char *buf;
1061         unsigned long flags;
1062
1063         if (length < ETH_ZLEN) {
1064                 skb = skb_padto(skb, ETH_ZLEN);
1065                 if (skb == NULL)
1066                         return 0;
1067                 length = ETH_ZLEN;
1068         }
1069         buf = skb->data;
1070
1071         netif_stop_queue(dev);
1072                 
1073         /* Turn off TX interrupts */
1074         outw(ETH16I_INTR_OFF, ioaddr + TX_INTR_REG);
1075         
1076         /* We would be better doing the disable_irq tricks the 3c509 does,
1077            that would make this suck a lot less */
1078            
1079         spin_lock_irqsave(&lp->lock, flags);
1080
1081         if( (length + 2) > (lp->tx_buf_size - lp->tx_queue_len)) {
1082                 if(eth16i_debug > 0) 
1083                         printk(KERN_WARNING "%s: Transmit buffer full.\n", dev->name);  
1084         } 
1085         else {
1086                 outw(length, ioaddr + DATAPORT);
1087
1088                 if( ioaddr < 0x1000 ) 
1089                         outsw(ioaddr + DATAPORT, buf, (length + 1) >> 1);
1090                 else {
1091                         unsigned char frag = length % 4;
1092                         outsl(ioaddr + DATAPORT, buf, length >> 2);
1093                         if( frag != 0 ) {
1094                                 outsw(ioaddr + DATAPORT, (buf + (length & 0xFFFC)), 1);
1095                                 if( frag == 3 ) 
1096                                         outsw(ioaddr + DATAPORT, 
1097                                               (buf + (length & 0xFFFC) + 2), 1);
1098                         }
1099                 }
1100                 lp->tx_buffered_packets++;
1101                 lp->tx_buffered_bytes = length;
1102                 lp->tx_queue++;
1103                 lp->tx_queue_len += length + 2;
1104         }
1105         lp->tx_buf_busy = 0;
1106
1107         if(lp->tx_started == 0) {
1108                 /* If the transmitter is idle..always trigger a transmit */
1109                 outb(TX_START | lp->tx_queue, ioaddr + TRANSMIT_START_REG);
1110                 lp->tx_queue = 0;
1111                 lp->tx_queue_len = 0;
1112                 dev->trans_start = jiffies;
1113                 lp->tx_started = 1;
1114                 netif_wake_queue(dev);
1115         }
1116         else if(lp->tx_queue_len < lp->tx_buf_size - (ETH_FRAME_LEN + 2)) {
1117                 /* There is still more room for one more packet in tx buffer */
1118                 netif_wake_queue(dev);
1119         }
1120                 
1121         spin_unlock_irqrestore(&lp->lock, flags);
1122         
1123         outw(ETH16I_INTR_ON, ioaddr + TX_INTR_REG);
1124         /* Turn TX interrupts back on */
1125         /* outb(TX_INTR_DONE | TX_INTR_16_COL, ioaddr + TX_INTR_REG); */
1126         status = 0;
1127         dev_kfree_skb(skb);
1128         return 0;
1129 }
1130
1131 static void eth16i_rx(struct net_device *dev)
1132 {
1133         struct eth16i_local *lp = netdev_priv(dev);
1134         int ioaddr = dev->base_addr;
1135         int boguscount = MAX_RX_LOOP;
1136
1137         /* Loop until all packets have been read */
1138         while( (inb(ioaddr + RECEIVE_MODE_REG) & RX_BUFFER_EMPTY) == 0) {
1139
1140                 /* Read status byte from receive buffer */ 
1141                 ushort status = inw(ioaddr + DATAPORT);
1142
1143                 /* Get the size of the packet from receive buffer */
1144                 ushort pkt_len = inw(ioaddr + DATAPORT);
1145
1146                 if(eth16i_debug > 4)
1147                         printk(KERN_DEBUG "%s: Receiving packet mode %02x status %04x.\n", 
1148                                dev->name, 
1149                                inb(ioaddr + RECEIVE_MODE_REG), status);
1150                 
1151                 if( !(status & PKT_GOOD) ) {
1152                         lp->stats.rx_errors++;
1153
1154                         if( (pkt_len < ETH_ZLEN) || (pkt_len > ETH_FRAME_LEN) ) {
1155                                 lp->stats.rx_length_errors++;
1156                                 eth16i_reset(dev);
1157                                 return;         
1158                         }
1159                         else { 
1160                                 eth16i_skip_packet(dev);
1161                                 lp->stats.rx_dropped++;
1162                         }       
1163                 }
1164                 else {   /* Ok so now we should have a good packet */
1165                         struct sk_buff *skb;
1166
1167                         skb = dev_alloc_skb(pkt_len + 3);
1168                         if( skb == NULL ) {
1169                                 printk(KERN_WARNING "%s: Could'n allocate memory for packet (len %d)\n", 
1170                                        dev->name, pkt_len);
1171                                 eth16i_skip_packet(dev);
1172                                 lp->stats.rx_dropped++;
1173                                 break;
1174                         }
1175
1176                         skb->dev = dev;
1177                         skb_reserve(skb,2);
1178                         
1179                         /* 
1180                            Now let's get the packet out of buffer.
1181                            size is (pkt_len + 1) >> 1, cause we are now reading words
1182                            and it have to be even aligned.
1183                            */ 
1184                         
1185                         if(ioaddr < 0x1000) 
1186                                 insw(ioaddr + DATAPORT, skb_put(skb, pkt_len), 
1187                                      (pkt_len + 1) >> 1);
1188                         else {  
1189                                 unsigned char *buf = skb_put(skb, pkt_len);
1190                                 unsigned char frag = pkt_len % 4;
1191
1192                                 insl(ioaddr + DATAPORT, buf, pkt_len >> 2);
1193
1194                                 if(frag != 0) {
1195                                         unsigned short rest[2];
1196                                         rest[0] = inw( ioaddr + DATAPORT );
1197                                         if(frag == 3)
1198                                                 rest[1] = inw( ioaddr + DATAPORT );
1199
1200                                         memcpy(buf + (pkt_len & 0xfffc), (char *)rest, frag);
1201                                 }
1202                         }
1203
1204                         skb->protocol=eth_type_trans(skb, dev);
1205
1206                         if( eth16i_debug > 5 ) {
1207                                 int i;
1208                                 printk(KERN_DEBUG "%s: Received packet of length %d.\n", 
1209                                        dev->name, pkt_len);
1210                                 for(i = 0; i < 14; i++) 
1211                                         printk(KERN_DEBUG " %02x", skb->data[i]);
1212                                 printk(KERN_DEBUG ".\n");
1213                         }
1214                         netif_rx(skb);
1215                         dev->last_rx = jiffies;
1216                         lp->stats.rx_packets++;
1217                         lp->stats.rx_bytes += pkt_len;
1218
1219                 } /* else */
1220
1221                 if(--boguscount <= 0)
1222                         break;
1223
1224         } /* while */
1225 }
1226
1227 static irqreturn_t eth16i_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1228 {
1229         struct net_device *dev = dev_id;
1230         struct eth16i_local *lp;
1231         int ioaddr = 0, status;
1232         int handled = 0;
1233
1234         ioaddr = dev->base_addr;
1235         lp = netdev_priv(dev);
1236
1237         /* Turn off all interrupts from adapter */
1238         outw(ETH16I_INTR_OFF, ioaddr + TX_INTR_REG);
1239
1240         /* eth16i_tx won't be called */
1241         spin_lock(&lp->lock);
1242
1243         status = inw(ioaddr + TX_STATUS_REG);      /* Get the status */
1244         outw(status, ioaddr + TX_STATUS_REG);      /* Clear status bits */
1245
1246         if (status)
1247                 handled = 1;
1248
1249         if(eth16i_debug > 3)
1250                 printk(KERN_DEBUG "%s: Interrupt with status %04x.\n", dev->name, status);
1251
1252         if( status & 0x7f00 ) {
1253
1254                 lp->stats.rx_errors++;
1255
1256                 if(status & (BUS_RD_ERR << 8) ) 
1257                         printk(KERN_WARNING "%s: Bus read error.\n",dev->name);
1258                 if(status & (SHORT_PKT_ERR << 8) )   lp->stats.rx_length_errors++;
1259                 if(status & (ALIGN_ERR << 8) )       lp->stats.rx_frame_errors++;
1260                 if(status & (CRC_ERR << 8) )        lp->stats.rx_crc_errors++;
1261                 if(status & (RX_BUF_OVERFLOW << 8) ) lp->stats.rx_over_errors++;
1262         }
1263         if( status & 0x001a) {
1264
1265                 lp->stats.tx_errors++;
1266
1267                 if(status & CR_LOST) lp->stats.tx_carrier_errors++;
1268                 if(status & TX_JABBER_ERR) lp->stats.tx_window_errors++;
1269
1270 #if 0          
1271                 if(status & COLLISION) {
1272                         lp->stats.collisions += 
1273                                 ((inb(ioaddr+TRANSMIT_MODE_REG) & 0xF0) >> 4);
1274                 }
1275 #endif
1276                 if(status & COLLISIONS_16) {
1277                         if(lp->col_16 < MAX_COL_16) { 
1278                                 lp->col_16++;
1279                                 lp->stats.collisions++;
1280                                 /* Resume transmitting, skip failed packet */
1281                                 outb(0x02, ioaddr + COL_16_REG);
1282                         }
1283                         else {
1284                                 printk(KERN_WARNING "%s: bailing out due to many consecutive 16-in-a-row collisions. Network cable problem?\n", dev->name);
1285                         }
1286                 }
1287         }
1288
1289         if( status & 0x00ff ) {          /* Let's check the transmit status reg */
1290
1291                 if(status & TX_DONE) {         /* The transmit has been done */
1292                         lp->stats.tx_packets = lp->tx_buffered_packets;
1293                         lp->stats.tx_bytes += lp->tx_buffered_bytes;
1294                         lp->col_16 = 0;            
1295
1296                         if(lp->tx_queue) {           /* Is there still packets ? */
1297                                 /* There was packet(s) so start transmitting and write also
1298                                    how many packets there is to be sended */
1299                                 outb(TX_START | lp->tx_queue, ioaddr + TRANSMIT_START_REG);
1300                                 lp->tx_queue = 0;
1301                                 lp->tx_queue_len = 0;
1302                                 lp->tx_started = 1;
1303                         }
1304                         else {
1305                                 lp->tx_started = 0;
1306                         }
1307                         netif_wake_queue(dev);
1308                 }
1309         }
1310
1311         if( ( status & 0x8000 ) || 
1312             ( (inb(ioaddr + RECEIVE_MODE_REG) & RX_BUFFER_EMPTY) == 0) ) {
1313                 eth16i_rx(dev);  /* We have packet in receive buffer */
1314         }  
1315         
1316         /* Turn interrupts back on */
1317         outw(ETH16I_INTR_ON, ioaddr + TX_INTR_REG);
1318         
1319         if(lp->tx_queue_len < lp->tx_buf_size - (ETH_FRAME_LEN + 2)) {
1320                 /* There is still more room for one more packet in tx buffer */
1321                 netif_wake_queue(dev);
1322         }
1323         
1324         spin_unlock(&lp->lock);
1325         
1326         return IRQ_RETVAL(handled);
1327 }
1328
1329 static void eth16i_skip_packet(struct net_device *dev)
1330 {        
1331         int ioaddr = dev->base_addr;
1332
1333         inw(ioaddr + DATAPORT);
1334         inw(ioaddr + DATAPORT);
1335         inw(ioaddr + DATAPORT);
1336
1337         outb(SKIP_RX_PACKET, ioaddr + FILTER_SELF_RX_REG);
1338         while( inb( ioaddr + FILTER_SELF_RX_REG ) != 0);
1339 }
1340
1341 static void eth16i_reset(struct net_device *dev)
1342 {
1343         struct eth16i_local *lp = netdev_priv(dev);
1344         int ioaddr = dev->base_addr;
1345
1346         if(eth16i_debug > 1) 
1347                 printk(KERN_DEBUG "%s: Resetting device.\n", dev->name);
1348
1349         BITSET(ioaddr + CONFIG_REG_0, DLC_EN);
1350         outw(0xffff, ioaddr + TX_STATUS_REG);    
1351         eth16i_select_regbank(2, ioaddr);
1352
1353         lp->tx_started = 0;
1354         lp->tx_buf_busy = 0;
1355         lp->tx_queue = 0;
1356         lp->tx_queue_len = 0;    
1357         BITCLR(ioaddr + CONFIG_REG_0, DLC_EN);
1358 }
1359
1360 static void eth16i_multicast(struct net_device *dev)
1361 {
1362         int ioaddr = dev->base_addr;
1363   
1364         if(dev->mc_count || dev->flags&(IFF_ALLMULTI|IFF_PROMISC)) 
1365         {
1366                 dev->flags|=IFF_PROMISC;        /* Must do this */
1367                 outb(3, ioaddr + RECEIVE_MODE_REG);    
1368         } else {
1369                 outb(2, ioaddr + RECEIVE_MODE_REG);
1370         }
1371 }
1372
1373 static struct net_device_stats *eth16i_get_stats(struct net_device *dev)
1374 {
1375         struct eth16i_local *lp = netdev_priv(dev);
1376         return &lp->stats;
1377 }
1378
1379 static void eth16i_select_regbank(unsigned char banknbr, int ioaddr)
1380 {
1381         unsigned char data;
1382
1383         data = inb(ioaddr + CONFIG_REG_1);
1384         outb( ((data & 0xF3) | ( (banknbr & 0x03) << 2)), ioaddr + CONFIG_REG_1); 
1385 }
1386
1387 #ifdef MODULE
1388
1389 static ushort eth16i_parse_mediatype(const char* s)
1390 {
1391         if(!s)
1392                 return E_PORT_FROM_EPROM;
1393         
1394         if (!strncmp(s, "bnc", 3))
1395                 return E_PORT_BNC;
1396         else if (!strncmp(s, "tp", 2))
1397                 return E_PORT_TP;
1398         else if (!strncmp(s, "dix", 3))
1399                 return E_PORT_DIX;
1400         else if (!strncmp(s, "auto", 4))
1401                 return E_PORT_AUTO;
1402         else
1403                 return E_PORT_FROM_EPROM;
1404 }
1405
1406 #define MAX_ETH16I_CARDS 4  /* Max number of Eth16i cards per module */
1407
1408 static struct net_device *dev_eth16i[MAX_ETH16I_CARDS];
1409 static int io[MAX_ETH16I_CARDS];
1410 #if 0
1411 static int irq[MAX_ETH16I_CARDS];
1412 #endif
1413 static char* mediatype[MAX_ETH16I_CARDS];
1414 static int debug = -1;
1415
1416 MODULE_AUTHOR("Mika Kuoppala <miku@iki.fi>");
1417 MODULE_DESCRIPTION("ICL EtherTeam 16i/32 driver");
1418 MODULE_LICENSE("GPL");
1419
1420
1421 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_ETH16I_CARDS) "i");
1422 MODULE_PARM_DESC(io, "eth16i I/O base address(es)");
1423
1424 #if 0
1425 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_ETH16I_CARDS) "i");
1426 MODULE_PARM_DESC(irq, "eth16i interrupt request number");
1427 #endif
1428
1429 MODULE_PARM(mediatype, "1-" __MODULE_STRING(MAX_ETH16I_CARDS) "s");
1430 MODULE_PARM_DESC(mediatype, "eth16i media type of interface(s) (bnc,tp,dix,auto,eprom)");
1431
1432 MODULE_PARM(debug, "i");
1433 MODULE_PARM_DESC(debug, "eth16i debug level (0-6)");
1434
1435 int init_module(void)
1436 {
1437         int this_dev, found = 0;
1438         struct net_device *dev;
1439
1440         for (this_dev = 0; this_dev < MAX_ETH16I_CARDS; this_dev++) {
1441                 dev = alloc_etherdev(sizeof(struct eth16i_local));
1442                 if (!dev)
1443                         break;
1444
1445                 dev->base_addr = io[this_dev];
1446
1447                 if(debug != -1)
1448                         eth16i_debug = debug;
1449
1450                 if(eth16i_debug > 1)
1451                         printk(KERN_NOTICE "eth16i(%d): interface type %s\n", this_dev, mediatype[this_dev] ? mediatype[this_dev] : "none" );
1452
1453                 dev->if_port = eth16i_parse_mediatype(mediatype[this_dev]);
1454
1455                 if(io[this_dev] == 0) {
1456                         if(this_dev != 0) /* Only autoprobe 1st one */
1457                                 break;
1458
1459                         printk(KERN_NOTICE "eth16i.c: Presently autoprobing (not recommended) for a single card.\n");
1460                 }
1461
1462                 if (do_eth16i_probe(dev) == 0) {
1463                         if (register_netdev(dev) == 0) {
1464                                 dev_eth16i[found++] = dev;
1465                                 continue;
1466                         }
1467                         free_irq(dev->irq, dev);
1468                         release_region(dev->base_addr, ETH16I_IO_EXTENT);
1469                 }
1470                 printk(KERN_WARNING "eth16i.c No Eth16i card found (i/o = 0x%x).\n",
1471                        io[this_dev]);
1472                 free_netdev(dev);
1473                 break;
1474         }
1475         if (found)
1476                 return 0;
1477         return -ENXIO;
1478 }
1479         
1480 void cleanup_module(void)
1481 {
1482         int this_dev;
1483
1484         for(this_dev = 0; this_dev < MAX_ETH16I_CARDS; this_dev++) {
1485                 struct net_device *dev = dev_eth16i[this_dev];
1486                 
1487                 if(dev->priv) {
1488                         unregister_netdev(dev);
1489                         free_irq(dev->irq, dev);
1490                         release_region(dev->base_addr, ETH16I_IO_EXTENT);
1491                         free_netdev(dev);
1492                 }
1493         }
1494 }
1495 #endif /* MODULE */
1496
1497 /*
1498  * Local variables:
1499  *  compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c eth16i.c"
1500  *  alt-compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict -prototypes -O6 -c eth16i.c"
1501  *  tab-width: 8
1502  *  c-basic-offset: 8
1503  *  c-indent-level: 8
1504  * End:
1505  */
1506
1507 /* End of file eth16i.c */