Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / drivers / net / eexpress.c
1 /* Intel EtherExpress 16 device driver for Linux
2  *
3  * Written by John Sullivan, 1995
4  *  based on original code by Donald Becker, with changes by
5  *  Alan Cox and Pauline Middelink.
6  *
7  * Support for 8-bit mode by Zoltan Szilagyi <zoltans@cs.arizona.edu>
8  *
9  * Many modifications, and currently maintained, by
10  *  Philip Blundell <philb@gnu.org>
11  * Added the Compaq LTE  Alan Cox <alan@lxorguk.ukuu.org.uk>
12  * Added MCA support Adam Fritzler
13  *
14  * Note - this driver is experimental still - it has problems on faster
15  * machines. Someone needs to sit down and go through it line by line with
16  * a databook...
17  */
18
19 /* The EtherExpress 16 is a fairly simple card, based on a shared-memory
20  * design using the i82586 Ethernet coprocessor.  It bears no relationship,
21  * as far as I know, to the similarly-named "EtherExpress Pro" range.
22  *
23  * Historically, Linux support for these cards has been very bad.  However,
24  * things seem to be getting better slowly.
25  */
26
27 /* If your card is confused about what sort of interface it has (eg it
28  * persistently reports "10baseT" when none is fitted), running 'SOFTSET /BART'
29  * or 'SOFTSET /LISA' from DOS seems to help.
30  */
31
32 /* Here's the scoop on memory mapping.
33  *
34  * There are three ways to access EtherExpress card memory: either using the
35  * shared-memory mapping, or using PIO through the dataport, or using PIO
36  * through the "shadow memory" ports.
37  *
38  * The shadow memory system works by having the card map some of its memory
39  * as follows:
40  *
41  * (the low five bits of the SMPTR are ignored)
42  *
43  *  base+0x4000..400f      memory at SMPTR+0..15
44  *  base+0x8000..800f      memory at SMPTR+16..31
45  *  base+0xc000..c007      dubious stuff (memory at SMPTR+16..23 apparently)
46  *  base+0xc008..c00f      memory at 0x0008..0x000f
47  *
48  * This last set (the one at c008) is particularly handy because the SCB
49  * lives at 0x0008.  So that set of ports gives us easy random access to data
50  * in the SCB without having to mess around setting up pointers and the like.
51  * We always use this method to access the SCB (via the scb_xx() functions).
52  *
53  * Dataport access works by aiming the appropriate (read or write) pointer
54  * at the first address you're interested in, and then reading or writing from
55  * the dataport.  The pointers auto-increment after each transfer.  We use
56  * this for data transfer.
57  *
58  * We don't use the shared-memory system because it allegedly doesn't work on
59  * all cards, and because it's a bit more prone to go wrong (it's one more
60  * thing to configure...).
61  */
62
63 /* Known bugs:
64  *
65  * - The card seems to want to give us two interrupts every time something
66  *   happens, where just one would be better.
67  */
68
69 /*
70  *
71  * Note by Zoltan Szilagyi 10-12-96:
72  *
73  * I've succeeded in eliminating the "CU wedged" messages, and hence the
74  * lockups, which were only occurring with cards running in 8-bit mode ("force
75  * 8-bit operation" in Intel's SoftSet utility). This version of the driver
76  * sets the 82586 and the ASIC to 8-bit mode at startup; it also stops the
77  * CU before submitting a packet for transmission, and then restarts it as soon
78  * as the process of handing the packet is complete. This is definitely an
79  * unnecessary slowdown if the card is running in 16-bit mode; therefore one
80  * should detect 16-bit vs 8-bit mode from the EEPROM settings and act
81  * accordingly. In 8-bit mode with this bugfix I'm getting about 150 K/s for
82  * ftp's, which is significantly better than I get in DOS, so the overhead of
83  * stopping and restarting the CU with each transmit is not prohibitive in
84  * practice.
85  *
86  * Update by David Woodhouse 11/5/99:
87  *
88  * I've seen "CU wedged" messages in 16-bit mode, on the Alpha architecture.
89  * I assume that this is because 16-bit accesses are actually handled as two
90  * 8-bit accesses.
91  */
92
93 #ifdef __alpha__
94 #define LOCKUP16 1
95 #endif
96 #ifndef LOCKUP16
97 #define LOCKUP16 0
98 #endif
99
100 #include <linux/module.h>
101 #include <linux/kernel.h>
102 #include <linux/types.h>
103 #include <linux/fcntl.h>
104 #include <linux/interrupt.h>
105 #include <linux/ioport.h>
106 #include <linux/string.h>
107 #include <linux/in.h>
108 #include <linux/delay.h>
109 #include <linux/errno.h>
110 #include <linux/init.h>
111 #include <linux/netdevice.h>
112 #include <linux/etherdevice.h>
113 #include <linux/skbuff.h>
114 #include <linux/slab.h>
115 #include <linux/mca-legacy.h>
116 #include <linux/spinlock.h>
117 #include <linux/bitops.h>
118 #include <linux/jiffies.h>
119
120 #include <asm/system.h>
121 #include <asm/io.h>
122 #include <asm/irq.h>
123
124 #ifndef NET_DEBUG
125 #define NET_DEBUG 4
126 #endif
127
128 #include "eexpress.h"
129
130 #define EEXP_IO_EXTENT  16
131
132 /*
133  * Private data declarations
134  */
135
136 struct net_local
137 {
138         unsigned long last_tx;       /* jiffies when last transmit started */
139         unsigned long init_time;     /* jiffies when eexp_hw_init586 called */
140         unsigned short rx_first;     /* first rx buf, same as RX_BUF_START */
141         unsigned short rx_last;      /* last rx buf */
142         unsigned short rx_ptr;       /* first rx buf to look at */
143         unsigned short tx_head;      /* next free tx buf */
144         unsigned short tx_reap;      /* first in-use tx buf */
145         unsigned short tx_tail;      /* previous tx buf to tx_head */
146         unsigned short tx_link;      /* last known-executing tx buf */
147         unsigned short last_tx_restart;   /* set to tx_link when we
148                                              restart the CU */
149         unsigned char started;
150         unsigned short rx_buf_start;
151         unsigned short rx_buf_end;
152         unsigned short num_tx_bufs;
153         unsigned short num_rx_bufs;
154         unsigned char width;         /* 0 for 16bit, 1 for 8bit */
155         unsigned char was_promisc;
156         unsigned char old_mc_count;
157         spinlock_t lock;
158 };
159
160 /* This is the code and data that is downloaded to the EtherExpress card's
161  * memory at boot time.
162  */
163
164 static unsigned short start_code[] = {
165 /* 0x0000 */
166         0x0001,                 /* ISCP: busy - cleared after reset */
167         0x0008,0x0000,0x0000,   /* offset,address (lo,hi) of SCB */
168
169         0x0000,0x0000,          /* SCB: status, commands */
170         0x0000,0x0000,          /* links to first command block,
171                                    first receive descriptor */
172         0x0000,0x0000,          /* CRC error, alignment error counts */
173         0x0000,0x0000,          /* out of resources, overrun error counts */
174
175         0x0000,0x0000,          /* pad */
176         0x0000,0x0000,
177
178 /* 0x20 -- start of 82586 CU program */
179 #define CONF_LINK 0x20
180         0x0000,Cmd_Config,
181         0x0032,                 /* link to next command */
182         0x080c,                 /* 12 bytes follow : fifo threshold=8 */
183         0x2e40,                 /* don't rx bad frames
184                                  * SRDY/ARDY => ext. sync. : preamble len=8
185                                  * take addresses from data buffers
186                                  * 6 bytes/address
187                                  */
188         0x6000,                 /* default backoff method & priority
189                                  * interframe spacing = 0x60 */
190         0xf200,                 /* slot time=0x200
191                                  * max collision retry = 0xf */
192 #define CONF_PROMISC  0x2e
193         0x0000,                 /* no HDLC : normal CRC : enable broadcast
194                                  * disable promiscuous/multicast modes */
195         0x003c,                 /* minimum frame length = 60 octets) */
196
197         0x0000,Cmd_SetAddr,
198         0x003e,                 /* link to next command */
199 #define CONF_HWADDR  0x38
200         0x0000,0x0000,0x0000,   /* hardware address placed here */
201
202         0x0000,Cmd_MCast,
203         0x0076,                 /* link to next command */
204 #define CONF_NR_MULTICAST 0x44
205         0x0000,                 /* number of bytes in multicast address(es) */
206 #define CONF_MULTICAST 0x46
207         0x0000, 0x0000, 0x0000, /* some addresses */
208         0x0000, 0x0000, 0x0000,
209         0x0000, 0x0000, 0x0000,
210         0x0000, 0x0000, 0x0000,
211         0x0000, 0x0000, 0x0000,
212         0x0000, 0x0000, 0x0000,
213         0x0000, 0x0000, 0x0000,
214         0x0000, 0x0000, 0x0000,
215
216 #define CONF_DIAG_RESULT  0x76
217         0x0000, Cmd_Diag,
218         0x007c,                 /* link to next command */
219
220         0x0000,Cmd_TDR|Cmd_INT,
221         0x0084,
222 #define CONF_TDR_RESULT  0x82
223         0x0000,
224
225         0x0000,Cmd_END|Cmd_Nop, /* end of configure sequence */
226         0x0084                  /* dummy link */
227 };
228
229 /* maps irq number to EtherExpress magic value */
230 static char irqrmap[] = { 0,0,1,2,3,4,0,0,0,1,5,6,0,0,0,0 };
231
232 #ifdef CONFIG_MCA_LEGACY
233 /* mapping of the first four bits of the second POS register */
234 static unsigned short mca_iomap[] = {
235         0x270, 0x260, 0x250, 0x240, 0x230, 0x220, 0x210, 0x200,
236         0x370, 0x360, 0x350, 0x340, 0x330, 0x320, 0x310, 0x300
237 };
238 /* bits 5-7 of the second POS register */
239 static char mca_irqmap[] = { 12, 9, 3, 4, 5, 10, 11, 15 };
240 #endif
241
242 /*
243  * Prototypes for Linux interface
244  */
245
246 static int eexp_open(struct net_device *dev);
247 static int eexp_close(struct net_device *dev);
248 static void eexp_timeout(struct net_device *dev);
249 static netdev_tx_t eexp_xmit(struct sk_buff *buf,
250                              struct net_device *dev);
251
252 static irqreturn_t eexp_irq(int irq, void *dev_addr);
253 static void eexp_set_multicast(struct net_device *dev);
254
255 /*
256  * Prototypes for hardware access functions
257  */
258
259 static void eexp_hw_rx_pio(struct net_device *dev);
260 static void eexp_hw_tx_pio(struct net_device *dev, unsigned short *buf,
261                        unsigned short len);
262 static int eexp_hw_probe(struct net_device *dev,unsigned short ioaddr);
263 static unsigned short eexp_hw_readeeprom(unsigned short ioaddr,
264                                          unsigned char location);
265
266 static unsigned short eexp_hw_lasttxstat(struct net_device *dev);
267 static void eexp_hw_txrestart(struct net_device *dev);
268
269 static void eexp_hw_txinit    (struct net_device *dev);
270 static void eexp_hw_rxinit    (struct net_device *dev);
271
272 static void eexp_hw_init586   (struct net_device *dev);
273 static void eexp_setup_filter (struct net_device *dev);
274
275 static char *eexp_ifmap[]={"AUI", "BNC", "RJ45"};
276 enum eexp_iftype {AUI=0, BNC=1, TPE=2};
277
278 #define STARTED_RU      2
279 #define STARTED_CU      1
280
281 /*
282  * Primitive hardware access functions.
283  */
284
285 static inline unsigned short scb_status(struct net_device *dev)
286 {
287         return inw(dev->base_addr + 0xc008);
288 }
289
290 static inline unsigned short scb_rdcmd(struct net_device *dev)
291 {
292         return inw(dev->base_addr + 0xc00a);
293 }
294
295 static inline void scb_command(struct net_device *dev, unsigned short cmd)
296 {
297         outw(cmd, dev->base_addr + 0xc00a);
298 }
299
300 static inline void scb_wrcbl(struct net_device *dev, unsigned short val)
301 {
302         outw(val, dev->base_addr + 0xc00c);
303 }
304
305 static inline void scb_wrrfa(struct net_device *dev, unsigned short val)
306 {
307         outw(val, dev->base_addr + 0xc00e);
308 }
309
310 static inline void set_loopback(struct net_device *dev)
311 {
312         outb(inb(dev->base_addr + Config) | 2, dev->base_addr + Config);
313 }
314
315 static inline void clear_loopback(struct net_device *dev)
316 {
317         outb(inb(dev->base_addr + Config) & ~2, dev->base_addr + Config);
318 }
319
320 static inline unsigned short int SHADOW(short int addr)
321 {
322         addr &= 0x1f;
323         if (addr > 0xf) addr += 0x3ff0;
324         return addr + 0x4000;
325 }
326
327 /*
328  * Linux interface
329  */
330
331 /*
332  * checks for presence of EtherExpress card
333  */
334
335 static int __init do_express_probe(struct net_device *dev)
336 {
337         unsigned short *port;
338         static unsigned short ports[] = { 0x240,0x300,0x310,0x270,0x320,0x340,0 };
339         unsigned short ioaddr = dev->base_addr;
340         int dev_irq = dev->irq;
341         int err;
342
343         dev->if_port = 0xff; /* not set */
344
345 #ifdef CONFIG_MCA_LEGACY
346         if (MCA_bus) {
347                 int slot = 0;
348
349                 /*
350                  * Only find one card at a time.  Subsequent calls
351                  * will find others, however, proper multicard MCA
352                  * probing and setup can't be done with the
353                  * old-style Space.c init routines.  -- ASF
354                  */
355                 while (slot != MCA_NOTFOUND) {
356                         int pos0, pos1;
357
358                         slot = mca_find_unused_adapter(0x628B, slot);
359                         if (slot == MCA_NOTFOUND)
360                                 break;
361
362                         pos0 = mca_read_stored_pos(slot, 2);
363                         pos1 = mca_read_stored_pos(slot, 3);
364                         ioaddr = mca_iomap[pos1&0xf];
365
366                         dev->irq = mca_irqmap[(pos1>>4)&0x7];
367
368                         /*
369                          * XXX: Transciever selection is done
370                          * differently on the MCA version.
371                          * How to get it to select something
372                          * other than external/AUI is currently
373                          * unknown.  This code is just for looks. -- ASF
374                          */
375                         if ((pos0 & 0x7) == 0x1)
376                                 dev->if_port = AUI;
377                         else if ((pos0 & 0x7) == 0x5) {
378                                 if (pos1 & 0x80)
379                                         dev->if_port = BNC;
380                                 else
381                                         dev->if_port = TPE;
382                         }
383
384                         mca_set_adapter_name(slot, "Intel EtherExpress 16 MCA");
385                         mca_set_adapter_procfn(slot, NULL, dev);
386                         mca_mark_as_used(slot);
387
388                         break;
389                 }
390         }
391 #endif
392         if (ioaddr&0xfe00) {
393                 if (!request_region(ioaddr, EEXP_IO_EXTENT, "EtherExpress"))
394                         return -EBUSY;
395                 err = eexp_hw_probe(dev,ioaddr);
396                 release_region(ioaddr, EEXP_IO_EXTENT);
397                 return err;
398         } else if (ioaddr)
399                 return -ENXIO;
400
401         for (port=&ports[0] ; *port ; port++ )
402         {
403                 unsigned short sum = 0;
404                 int i;
405                 if (!request_region(*port, EEXP_IO_EXTENT, "EtherExpress"))
406                         continue;
407                 for ( i=0 ; i<4 ; i++ )
408                 {
409                         unsigned short t;
410                         t = inb(*port + ID_PORT);
411                         sum |= (t>>4) << ((t & 0x03)<<2);
412                 }
413                 if (sum==0xbaba && !eexp_hw_probe(dev,*port)) {
414                         release_region(*port, EEXP_IO_EXTENT);
415                         return 0;
416                 }
417                 release_region(*port, EEXP_IO_EXTENT);
418                 dev->irq = dev_irq;
419         }
420         return -ENODEV;
421 }
422
423 #ifndef MODULE
424 struct net_device * __init express_probe(int unit)
425 {
426         struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
427         int err;
428
429         if (!dev)
430                 return ERR_PTR(-ENOMEM);
431
432         sprintf(dev->name, "eth%d", unit);
433         netdev_boot_setup_check(dev);
434
435         err = do_express_probe(dev);
436         if (!err)
437                 return dev;
438         free_netdev(dev);
439         return ERR_PTR(err);
440 }
441 #endif
442
443 /*
444  * open and initialize the adapter, ready for use
445  */
446
447 static int eexp_open(struct net_device *dev)
448 {
449         int ret;
450         unsigned short ioaddr = dev->base_addr;
451         struct net_local *lp = netdev_priv(dev);
452
453 #if NET_DEBUG > 6
454         printk(KERN_DEBUG "%s: eexp_open()\n", dev->name);
455 #endif
456
457         if (!dev->irq || !irqrmap[dev->irq])
458                 return -ENXIO;
459
460         ret = request_irq(dev->irq, eexp_irq, 0, dev->name, dev);
461         if (ret)
462                 return ret;
463
464         if (!request_region(ioaddr, EEXP_IO_EXTENT, "EtherExpress")) {
465                 printk(KERN_WARNING "EtherExpress io port %x, is busy.\n"
466                         , ioaddr);
467                 goto err_out1;
468         }
469         if (!request_region(ioaddr+0x4000, EEXP_IO_EXTENT, "EtherExpress shadow")) {
470                 printk(KERN_WARNING "EtherExpress io port %x, is busy.\n"
471                         , ioaddr+0x4000);
472                 goto err_out2;
473         }
474         if (!request_region(ioaddr+0x8000, EEXP_IO_EXTENT, "EtherExpress shadow")) {
475                 printk(KERN_WARNING "EtherExpress io port %x, is busy.\n"
476                         , ioaddr+0x8000);
477                 goto err_out3;
478         }
479         if (!request_region(ioaddr+0xc000, EEXP_IO_EXTENT, "EtherExpress shadow")) {
480                 printk(KERN_WARNING "EtherExpress io port %x, is busy.\n"
481                         , ioaddr+0xc000);
482                 goto err_out4;
483         }
484
485         if (lp->width) {
486                 printk("%s: forcing ASIC to 8-bit mode\n", dev->name);
487                 outb(inb(dev->base_addr+Config)&~4, dev->base_addr+Config);
488         }
489
490         eexp_hw_init586(dev);
491         netif_start_queue(dev);
492 #if NET_DEBUG > 6
493         printk(KERN_DEBUG "%s: leaving eexp_open()\n", dev->name);
494 #endif
495         return 0;
496
497         err_out4:
498                 release_region(ioaddr+0x8000, EEXP_IO_EXTENT);
499         err_out3:
500                 release_region(ioaddr+0x4000, EEXP_IO_EXTENT);
501         err_out2:
502                 release_region(ioaddr, EEXP_IO_EXTENT);
503         err_out1:
504                 free_irq(dev->irq, dev);
505                 return -EBUSY;
506 }
507
508 /*
509  * close and disable the interface, leaving the 586 in reset.
510  */
511
512 static int eexp_close(struct net_device *dev)
513 {
514         unsigned short ioaddr = dev->base_addr;
515         struct net_local *lp = netdev_priv(dev);
516
517         int irq = dev->irq;
518
519         netif_stop_queue(dev);
520
521         outb(SIRQ_dis|irqrmap[irq],ioaddr+SET_IRQ);
522         lp->started = 0;
523         scb_command(dev, SCB_CUsuspend|SCB_RUsuspend);
524         outb(0,ioaddr+SIGNAL_CA);
525         free_irq(irq,dev);
526         outb(i586_RST,ioaddr+EEPROM_Ctrl);
527         release_region(ioaddr, EEXP_IO_EXTENT);
528         release_region(ioaddr+0x4000, 16);
529         release_region(ioaddr+0x8000, 16);
530         release_region(ioaddr+0xc000, 16);
531
532         return 0;
533 }
534
535 /*
536  * This gets called when a higher level thinks we are broken.  Check that
537  * nothing has become jammed in the CU.
538  */
539
540 static void unstick_cu(struct net_device *dev)
541 {
542         struct net_local *lp = netdev_priv(dev);
543         unsigned short ioaddr = dev->base_addr;
544
545         if (lp->started)
546         {
547                 if (time_after(jiffies, dev->trans_start + 50))
548                 {
549                         if (lp->tx_link==lp->last_tx_restart)
550                         {
551                                 unsigned short boguscount=200,rsst;
552                                 printk(KERN_WARNING "%s: Retransmit timed out, status %04x, resetting...\n",
553                                        dev->name, scb_status(dev));
554                                 eexp_hw_txinit(dev);
555                                 lp->last_tx_restart = 0;
556                                 scb_wrcbl(dev, lp->tx_link);
557                                 scb_command(dev, SCB_CUstart);
558                                 outb(0,ioaddr+SIGNAL_CA);
559                                 while (!SCB_complete(rsst=scb_status(dev)))
560                                 {
561                                         if (!--boguscount)
562                                         {
563                                                 boguscount=200;
564                                                 printk(KERN_WARNING "%s: Reset timed out status %04x, retrying...\n",
565                                                        dev->name,rsst);
566                                                 scb_wrcbl(dev, lp->tx_link);
567                                                 scb_command(dev, SCB_CUstart);
568                                                 outb(0,ioaddr+SIGNAL_CA);
569                                         }
570                                 }
571                                 netif_wake_queue(dev);
572                         }
573                         else
574                         {
575                                 unsigned short status = scb_status(dev);
576                                 if (SCB_CUdead(status))
577                                 {
578                                         unsigned short txstatus = eexp_hw_lasttxstat(dev);
579                                         printk(KERN_WARNING "%s: Transmit timed out, CU not active status %04x %04x, restarting...\n",
580                                                dev->name, status, txstatus);
581                                         eexp_hw_txrestart(dev);
582                                 }
583                                 else
584                                 {
585                                         unsigned short txstatus = eexp_hw_lasttxstat(dev);
586                                         if (netif_queue_stopped(dev) && !txstatus)
587                                         {
588                                                 printk(KERN_WARNING "%s: CU wedged, status %04x %04x, resetting...\n",
589                                                        dev->name,status,txstatus);
590                                                 eexp_hw_init586(dev);
591                                                 netif_wake_queue(dev);
592                                         }
593                                         else
594                                         {
595                                                 printk(KERN_WARNING "%s: transmit timed out\n", dev->name);
596                                         }
597                                 }
598                         }
599                 }
600         }
601         else
602         {
603                 if (time_after(jiffies, lp->init_time + 10))
604                 {
605                         unsigned short status = scb_status(dev);
606                         printk(KERN_WARNING "%s: i82586 startup timed out, status %04x, resetting...\n",
607                                dev->name, status);
608                         eexp_hw_init586(dev);
609                         netif_wake_queue(dev);
610                 }
611         }
612 }
613
614 static void eexp_timeout(struct net_device *dev)
615 {
616         struct net_local *lp = netdev_priv(dev);
617 #ifdef CONFIG_SMP
618         unsigned long flags;
619 #endif
620         int status;
621
622         disable_irq(dev->irq);
623
624         /*
625          *      Best would be to use synchronize_irq(); spin_lock() here
626          *      lets make it work first..
627          */
628
629 #ifdef CONFIG_SMP
630         spin_lock_irqsave(&lp->lock, flags);
631 #endif
632
633         status = scb_status(dev);
634         unstick_cu(dev);
635         printk(KERN_INFO "%s: transmit timed out, %s?\n", dev->name,
636                (SCB_complete(status)?"lost interrupt":
637                 "board on fire"));
638         dev->stats.tx_errors++;
639         lp->last_tx = jiffies;
640         if (!SCB_complete(status)) {
641                 scb_command(dev, SCB_CUabort);
642                 outb(0,dev->base_addr+SIGNAL_CA);
643         }
644         netif_wake_queue(dev);
645 #ifdef CONFIG_SMP
646         spin_unlock_irqrestore(&lp->lock, flags);
647 #endif
648 }
649
650 /*
651  * Called to transmit a packet, or to allow us to right ourselves
652  * if the kernel thinks we've died.
653  */
654 static netdev_tx_t eexp_xmit(struct sk_buff *buf, struct net_device *dev)
655 {
656         short length = buf->len;
657 #ifdef CONFIG_SMP
658         struct net_local *lp = netdev_priv(dev);
659         unsigned long flags;
660 #endif
661
662 #if NET_DEBUG > 6
663         printk(KERN_DEBUG "%s: eexp_xmit()\n", dev->name);
664 #endif
665
666         if (buf->len < ETH_ZLEN) {
667                 if (skb_padto(buf, ETH_ZLEN))
668                         return NETDEV_TX_OK;
669                 length = ETH_ZLEN;
670         }
671
672         disable_irq(dev->irq);
673
674         /*
675          *      Best would be to use synchronize_irq(); spin_lock() here
676          *      lets make it work first..
677          */
678
679 #ifdef CONFIG_SMP
680         spin_lock_irqsave(&lp->lock, flags);
681 #endif
682
683         {
684                 unsigned short *data = (unsigned short *)buf->data;
685
686                 dev->stats.tx_bytes += length;
687
688                 eexp_hw_tx_pio(dev,data,length);
689         }
690         dev_kfree_skb(buf);
691 #ifdef CONFIG_SMP
692         spin_unlock_irqrestore(&lp->lock, flags);
693 #endif
694         enable_irq(dev->irq);
695         return NETDEV_TX_OK;
696 }
697
698 /*
699  * Handle an EtherExpress interrupt
700  * If we've finished initializing, start the RU and CU up.
701  * If we've already started, reap tx buffers, handle any received packets,
702  * check to make sure we've not become wedged.
703  */
704
705 static unsigned short eexp_start_irq(struct net_device *dev,
706                                      unsigned short status)
707 {
708         unsigned short ack_cmd = SCB_ack(status);
709         struct net_local *lp = netdev_priv(dev);
710         unsigned short ioaddr = dev->base_addr;
711         if ((dev->flags & IFF_UP) && !(lp->started & STARTED_CU)) {
712                 short diag_status, tdr_status;
713                 while (SCB_CUstat(status)==2)
714                         status = scb_status(dev);
715 #if NET_DEBUG > 4
716                 printk("%s: CU went non-active (status %04x)\n",
717                        dev->name, status);
718 #endif
719
720                 outw(CONF_DIAG_RESULT & ~31, ioaddr + SM_PTR);
721                 diag_status = inw(ioaddr + SHADOW(CONF_DIAG_RESULT));
722                 if (diag_status & 1<<11) {
723                         printk(KERN_WARNING "%s: 82586 failed self-test\n",
724                                dev->name);
725                 } else if (!(diag_status & 1<<13)) {
726                         printk(KERN_WARNING "%s: 82586 self-test failed to complete\n", dev->name);
727                 }
728
729                 outw(CONF_TDR_RESULT & ~31, ioaddr + SM_PTR);
730                 tdr_status = inw(ioaddr + SHADOW(CONF_TDR_RESULT));
731                 if (tdr_status & (TDR_SHORT|TDR_OPEN)) {
732                         printk(KERN_WARNING "%s: TDR reports cable %s at %d tick%s\n", dev->name, (tdr_status & TDR_SHORT)?"short":"broken", tdr_status & TDR_TIME, ((tdr_status & TDR_TIME) != 1) ? "s" : "");
733                 }
734                 else if (tdr_status & TDR_XCVRPROBLEM) {
735                         printk(KERN_WARNING "%s: TDR reports transceiver problem\n", dev->name);
736                 }
737                 else if (tdr_status & TDR_LINKOK) {
738 #if NET_DEBUG > 4
739                         printk(KERN_DEBUG "%s: TDR reports link OK\n", dev->name);
740 #endif
741                 } else {
742                         printk("%s: TDR is ga-ga (status %04x)\n", dev->name,
743                                tdr_status);
744                 }
745
746                 lp->started |= STARTED_CU;
747                 scb_wrcbl(dev, lp->tx_link);
748                 /* if the RU isn't running, start it now */
749                 if (!(lp->started & STARTED_RU)) {
750                         ack_cmd |= SCB_RUstart;
751                         scb_wrrfa(dev, lp->rx_buf_start);
752                         lp->rx_ptr = lp->rx_buf_start;
753                         lp->started |= STARTED_RU;
754                 }
755                 ack_cmd |= SCB_CUstart | 0x2000;
756         }
757
758         if ((dev->flags & IFF_UP) && !(lp->started & STARTED_RU) && SCB_RUstat(status)==4)
759                 lp->started|=STARTED_RU;
760
761         return ack_cmd;
762 }
763
764 static void eexp_cmd_clear(struct net_device *dev)
765 {
766         unsigned long int oldtime = jiffies;
767         while (scb_rdcmd(dev) && (time_before(jiffies, oldtime + 10)));
768         if (scb_rdcmd(dev)) {
769                 printk("%s: command didn't clear\n", dev->name);
770         }
771 }
772
773 static irqreturn_t eexp_irq(int dummy, void *dev_info)
774 {
775         struct net_device *dev = dev_info;
776         struct net_local *lp;
777         unsigned short ioaddr,status,ack_cmd;
778         unsigned short old_read_ptr, old_write_ptr;
779
780         lp = netdev_priv(dev);
781         ioaddr = dev->base_addr;
782
783         spin_lock(&lp->lock);
784
785         old_read_ptr = inw(ioaddr+READ_PTR);
786         old_write_ptr = inw(ioaddr+WRITE_PTR);
787
788         outb(SIRQ_dis|irqrmap[dev->irq], ioaddr+SET_IRQ);
789
790         status = scb_status(dev);
791
792 #if NET_DEBUG > 4
793         printk(KERN_DEBUG "%s: interrupt (status %x)\n", dev->name, status);
794 #endif
795
796         if (lp->started == (STARTED_CU | STARTED_RU)) {
797
798                 do {
799                         eexp_cmd_clear(dev);
800
801                         ack_cmd = SCB_ack(status);
802                         scb_command(dev, ack_cmd);
803                         outb(0,ioaddr+SIGNAL_CA);
804
805                         eexp_cmd_clear(dev);
806
807                         if (SCB_complete(status)) {
808                                 if (!eexp_hw_lasttxstat(dev)) {
809                                         printk("%s: tx interrupt but no status\n", dev->name);
810                                 }
811                         }
812
813                         if (SCB_rxdframe(status))
814                                 eexp_hw_rx_pio(dev);
815
816                         status = scb_status(dev);
817                 } while (status & 0xc000);
818
819                 if (SCB_RUdead(status))
820                 {
821                         printk(KERN_WARNING "%s: RU stopped: status %04x\n",
822                                dev->name,status);
823 #if 0
824                         printk(KERN_WARNING "%s: cur_rfd=%04x, cur_rbd=%04x\n", dev->name, lp->cur_rfd, lp->cur_rbd);
825                         outw(lp->cur_rfd, ioaddr+READ_PTR);
826                         printk(KERN_WARNING "%s: [%04x]\n", dev->name, inw(ioaddr+DATAPORT));
827                         outw(lp->cur_rfd+6, ioaddr+READ_PTR);
828                         printk(KERN_WARNING "%s: rbd is %04x\n", dev->name, rbd= inw(ioaddr+DATAPORT));
829                         outw(rbd, ioaddr+READ_PTR);
830                         printk(KERN_WARNING "%s: [%04x %04x] ", dev->name, inw(ioaddr+DATAPORT), inw(ioaddr+DATAPORT));
831                         outw(rbd+8, ioaddr+READ_PTR);
832                         printk("[%04x]\n", inw(ioaddr+DATAPORT));
833 #endif
834                         dev->stats.rx_errors++;
835 #if 1
836                         eexp_hw_rxinit(dev);
837 #else
838                         lp->cur_rfd = lp->first_rfd;
839 #endif
840                         scb_wrrfa(dev, lp->rx_buf_start);
841                         scb_command(dev, SCB_RUstart);
842                         outb(0,ioaddr+SIGNAL_CA);
843                 }
844         } else {
845                 if (status & 0x8000)
846                         ack_cmd = eexp_start_irq(dev, status);
847                 else
848                         ack_cmd = SCB_ack(status);
849                 scb_command(dev, ack_cmd);
850                 outb(0,ioaddr+SIGNAL_CA);
851         }
852
853         eexp_cmd_clear(dev);
854
855         outb(SIRQ_en|irqrmap[dev->irq], ioaddr+SET_IRQ);
856
857 #if NET_DEBUG > 6
858         printk("%s: leaving eexp_irq()\n", dev->name);
859 #endif
860         outw(old_read_ptr, ioaddr+READ_PTR);
861         outw(old_write_ptr, ioaddr+WRITE_PTR);
862
863         spin_unlock(&lp->lock);
864         return IRQ_HANDLED;
865 }
866
867 /*
868  * Hardware access functions
869  */
870
871 /*
872  * Set the cable type to use.
873  */
874
875 static void eexp_hw_set_interface(struct net_device *dev)
876 {
877         unsigned char oldval = inb(dev->base_addr + 0x300e);
878         oldval &= ~0x82;
879         switch (dev->if_port) {
880         case TPE:
881                 oldval |= 0x2;
882         case BNC:
883                 oldval |= 0x80;
884                 break;
885         }
886         outb(oldval, dev->base_addr+0x300e);
887         mdelay(20);
888 }
889
890 /*
891  * Check all the receive buffers, and hand any received packets
892  * to the upper levels. Basic sanity check on each frame
893  * descriptor, though we don't bother trying to fix broken ones.
894  */
895
896 static void eexp_hw_rx_pio(struct net_device *dev)
897 {
898         struct net_local *lp = netdev_priv(dev);
899         unsigned short rx_block = lp->rx_ptr;
900         unsigned short boguscount = lp->num_rx_bufs;
901         unsigned short ioaddr = dev->base_addr;
902         unsigned short status;
903
904 #if NET_DEBUG > 6
905         printk(KERN_DEBUG "%s: eexp_hw_rx()\n", dev->name);
906 #endif
907
908         do {
909                 unsigned short rfd_cmd, rx_next, pbuf, pkt_len;
910
911                 outw(rx_block, ioaddr + READ_PTR);
912                 status = inw(ioaddr + DATAPORT);
913
914                 if (FD_Done(status))
915                 {
916                         rfd_cmd = inw(ioaddr + DATAPORT);
917                         rx_next = inw(ioaddr + DATAPORT);
918                         pbuf = inw(ioaddr + DATAPORT);
919
920                         outw(pbuf, ioaddr + READ_PTR);
921                         pkt_len = inw(ioaddr + DATAPORT);
922
923                         if (rfd_cmd!=0x0000)
924                         {
925                                 printk(KERN_WARNING "%s: rfd_cmd not zero:0x%04x\n",
926                                        dev->name, rfd_cmd);
927                                 continue;
928                         }
929                         else if (pbuf!=rx_block+0x16)
930                         {
931                                 printk(KERN_WARNING "%s: rfd and rbd out of sync 0x%04x 0x%04x\n",
932                                        dev->name, rx_block+0x16, pbuf);
933                                 continue;
934                         }
935                         else if ((pkt_len & 0xc000)!=0xc000)
936                         {
937                                 printk(KERN_WARNING "%s: EOF or F not set on received buffer (%04x)\n",
938                                        dev->name, pkt_len & 0xc000);
939                                 continue;
940                         }
941                         else if (!FD_OK(status))
942                         {
943                                 dev->stats.rx_errors++;
944                                 if (FD_CRC(status))
945                                         dev->stats.rx_crc_errors++;
946                                 if (FD_Align(status))
947                                         dev->stats.rx_frame_errors++;
948                                 if (FD_Resrc(status))
949                                         dev->stats.rx_fifo_errors++;
950                                 if (FD_DMA(status))
951                                         dev->stats.rx_over_errors++;
952                                 if (FD_Short(status))
953                                         dev->stats.rx_length_errors++;
954                         }
955                         else
956                         {
957                                 struct sk_buff *skb;
958                                 pkt_len &= 0x3fff;
959                                 skb = dev_alloc_skb(pkt_len+16);
960                                 if (skb == NULL)
961                                 {
962                                         printk(KERN_WARNING "%s: Memory squeeze, dropping packet\n",dev->name);
963                                         dev->stats.rx_dropped++;
964                                         break;
965                                 }
966                                 skb_reserve(skb, 2);
967                                 outw(pbuf+10, ioaddr+READ_PTR);
968                                 insw(ioaddr+DATAPORT, skb_put(skb,pkt_len),(pkt_len+1)>>1);
969                                 skb->protocol = eth_type_trans(skb,dev);
970                                 netif_rx(skb);
971                                 dev->stats.rx_packets++;
972                                 dev->stats.rx_bytes += pkt_len;
973                         }
974                         outw(rx_block, ioaddr+WRITE_PTR);
975                         outw(0, ioaddr+DATAPORT);
976                         outw(0, ioaddr+DATAPORT);
977                         rx_block = rx_next;
978                 }
979         } while (FD_Done(status) && boguscount--);
980         lp->rx_ptr = rx_block;
981 }
982
983 /*
984  * Hand a packet to the card for transmission
985  * If we get here, we MUST have already checked
986  * to make sure there is room in the transmit
987  * buffer region.
988  */
989
990 static void eexp_hw_tx_pio(struct net_device *dev, unsigned short *buf,
991                        unsigned short len)
992 {
993         struct net_local *lp = netdev_priv(dev);
994         unsigned short ioaddr = dev->base_addr;
995
996         if (LOCKUP16 || lp->width) {
997                 /* Stop the CU so that there is no chance that it
998                    jumps off to a bogus address while we are writing the
999                    pointer to the next transmit packet in 8-bit mode --
1000                    this eliminates the "CU wedged" errors in 8-bit mode.
1001                    (Zoltan Szilagyi 10-12-96) */
1002                 scb_command(dev, SCB_CUsuspend);
1003                 outw(0xFFFF, ioaddr+SIGNAL_CA);
1004         }
1005
1006         outw(lp->tx_head, ioaddr + WRITE_PTR);
1007
1008         outw(0x0000, ioaddr + DATAPORT);
1009         outw(Cmd_INT|Cmd_Xmit, ioaddr + DATAPORT);
1010         outw(lp->tx_head+0x08, ioaddr + DATAPORT);
1011         outw(lp->tx_head+0x0e, ioaddr + DATAPORT);
1012
1013         outw(0x0000, ioaddr + DATAPORT);
1014         outw(0x0000, ioaddr + DATAPORT);
1015         outw(lp->tx_head+0x08, ioaddr + DATAPORT);
1016
1017         outw(0x8000|len, ioaddr + DATAPORT);
1018         outw(-1, ioaddr + DATAPORT);
1019         outw(lp->tx_head+0x16, ioaddr + DATAPORT);
1020         outw(0, ioaddr + DATAPORT);
1021
1022         outsw(ioaddr + DATAPORT, buf, (len+1)>>1);
1023
1024         outw(lp->tx_tail+0xc, ioaddr + WRITE_PTR);
1025         outw(lp->tx_head, ioaddr + DATAPORT);
1026
1027         dev->trans_start = jiffies;
1028         lp->tx_tail = lp->tx_head;
1029         if (lp->tx_head==TX_BUF_START+((lp->num_tx_bufs-1)*TX_BUF_SIZE))
1030                 lp->tx_head = TX_BUF_START;
1031         else
1032                 lp->tx_head += TX_BUF_SIZE;
1033         if (lp->tx_head != lp->tx_reap)
1034                 netif_wake_queue(dev);
1035
1036         if (LOCKUP16 || lp->width) {
1037                 /* Restart the CU so that the packet can actually
1038                    be transmitted. (Zoltan Szilagyi 10-12-96) */
1039                 scb_command(dev, SCB_CUresume);
1040                 outw(0xFFFF, ioaddr+SIGNAL_CA);
1041         }
1042
1043         dev->stats.tx_packets++;
1044         lp->last_tx = jiffies;
1045 }
1046
1047 static const struct net_device_ops eexp_netdev_ops = {
1048         .ndo_open               = eexp_open,
1049         .ndo_stop               = eexp_close,
1050         .ndo_start_xmit         = eexp_xmit,
1051         .ndo_set_multicast_list = eexp_set_multicast,
1052         .ndo_tx_timeout         = eexp_timeout,
1053         .ndo_change_mtu         = eth_change_mtu,
1054         .ndo_set_mac_address    = eth_mac_addr,
1055         .ndo_validate_addr      = eth_validate_addr,
1056 };
1057
1058 /*
1059  * Sanity check the suspected EtherExpress card
1060  * Read hardware address, reset card, size memory and initialize buffer
1061  * memory pointers. These are held in netdev_priv(), in case someone has more
1062  * than one card in a machine.
1063  */
1064
1065 static int __init eexp_hw_probe(struct net_device *dev, unsigned short ioaddr)
1066 {
1067         unsigned short hw_addr[3];
1068         unsigned char buswidth;
1069         unsigned int memory_size;
1070         int i;
1071         unsigned short xsum = 0;
1072         struct net_local *lp = netdev_priv(dev);
1073
1074         printk("%s: EtherExpress 16 at %#x ",dev->name,ioaddr);
1075
1076         outb(ASIC_RST, ioaddr+EEPROM_Ctrl);
1077         outb(0, ioaddr+EEPROM_Ctrl);
1078         udelay(500);
1079         outb(i586_RST, ioaddr+EEPROM_Ctrl);
1080
1081         hw_addr[0] = eexp_hw_readeeprom(ioaddr,2);
1082         hw_addr[1] = eexp_hw_readeeprom(ioaddr,3);
1083         hw_addr[2] = eexp_hw_readeeprom(ioaddr,4);
1084
1085         /* Standard Address or Compaq LTE Address */
1086         if (!((hw_addr[2]==0x00aa && ((hw_addr[1] & 0xff00)==0x0000)) ||
1087               (hw_addr[2]==0x0080 && ((hw_addr[1] & 0xff00)==0x5F00))))
1088         {
1089                 printk(" rejected: invalid address %04x%04x%04x\n",
1090                         hw_addr[2],hw_addr[1],hw_addr[0]);
1091                 return -ENODEV;
1092         }
1093
1094         /* Calculate the EEPROM checksum.  Carry on anyway if it's bad,
1095          * though.
1096          */
1097         for (i = 0; i < 64; i++)
1098                 xsum += eexp_hw_readeeprom(ioaddr, i);
1099         if (xsum != 0xbaba)
1100                 printk(" (bad EEPROM xsum 0x%02x)", xsum);
1101
1102         dev->base_addr = ioaddr;
1103         for ( i=0 ; i<6 ; i++ )
1104                 dev->dev_addr[i] = ((unsigned char *)hw_addr)[5-i];
1105
1106         {
1107                 static char irqmap[]={0, 9, 3, 4, 5, 10, 11, 0};
1108                 unsigned short setupval = eexp_hw_readeeprom(ioaddr,0);
1109
1110                 /* Use the IRQ from EEPROM if none was given */
1111                 if (!dev->irq)
1112                         dev->irq = irqmap[setupval>>13];
1113
1114                 if (dev->if_port == 0xff) {
1115                         dev->if_port = !(setupval & 0x1000) ? AUI :
1116                                 eexp_hw_readeeprom(ioaddr,5) & 0x1 ? TPE : BNC;
1117                 }
1118
1119                 buswidth = !((setupval & 0x400) >> 10);
1120         }
1121
1122         memset(lp, 0, sizeof(struct net_local));
1123         spin_lock_init(&lp->lock);
1124
1125         printk("(IRQ %d, %s connector, %d-bit bus", dev->irq,
1126                eexp_ifmap[dev->if_port], buswidth?8:16);
1127
1128         if (!request_region(dev->base_addr + 0x300e, 1, "EtherExpress"))
1129                 return -EBUSY;
1130
1131         eexp_hw_set_interface(dev);
1132
1133         release_region(dev->base_addr + 0x300e, 1);
1134
1135         /* Find out how much RAM we have on the card */
1136         outw(0, dev->base_addr + WRITE_PTR);
1137         for (i = 0; i < 32768; i++)
1138                 outw(0, dev->base_addr + DATAPORT);
1139
1140         for (memory_size = 0; memory_size < 64; memory_size++)
1141         {
1142                 outw(memory_size<<10, dev->base_addr + READ_PTR);
1143                 if (inw(dev->base_addr+DATAPORT))
1144                         break;
1145                 outw(memory_size<<10, dev->base_addr + WRITE_PTR);
1146                 outw(memory_size | 0x5000, dev->base_addr+DATAPORT);
1147                 outw(memory_size<<10, dev->base_addr + READ_PTR);
1148                 if (inw(dev->base_addr+DATAPORT) != (memory_size | 0x5000))
1149                         break;
1150         }
1151
1152         /* Sort out the number of buffers.  We may have 16, 32, 48 or 64k
1153          * of RAM to play with.
1154          */
1155         lp->num_tx_bufs = 4;
1156         lp->rx_buf_end = 0x3ff6;
1157         switch (memory_size)
1158         {
1159         case 64:
1160                 lp->rx_buf_end += 0x4000;
1161         case 48:
1162                 lp->num_tx_bufs += 4;
1163                 lp->rx_buf_end += 0x4000;
1164         case 32:
1165                 lp->rx_buf_end += 0x4000;
1166         case 16:
1167                 printk(", %dk RAM)\n", memory_size);
1168                 break;
1169         default:
1170                 printk(") bad memory size (%dk).\n", memory_size);
1171                 return -ENODEV;
1172                 break;
1173         }
1174
1175         lp->rx_buf_start = TX_BUF_START + (lp->num_tx_bufs*TX_BUF_SIZE);
1176         lp->width = buswidth;
1177
1178         dev->netdev_ops = &eexp_netdev_ops;
1179         dev->watchdog_timeo = 2*HZ;
1180
1181         return register_netdev(dev);
1182 }
1183
1184 /*
1185  * Read a word from the EtherExpress on-board serial EEPROM.
1186  * The EEPROM contains 64 words of 16 bits.
1187  */
1188 static unsigned short __init eexp_hw_readeeprom(unsigned short ioaddr,
1189                                                     unsigned char location)
1190 {
1191         unsigned short cmd = 0x180|(location&0x7f);
1192         unsigned short rval = 0,wval = EC_CS|i586_RST;
1193         int i;
1194
1195         outb(EC_CS|i586_RST,ioaddr+EEPROM_Ctrl);
1196         for (i=0x100 ; i ; i>>=1 )
1197         {
1198                 if (cmd&i)
1199                         wval |= EC_Wr;
1200                 else
1201                         wval &= ~EC_Wr;
1202
1203                 outb(wval,ioaddr+EEPROM_Ctrl);
1204                 outb(wval|EC_Clk,ioaddr+EEPROM_Ctrl);
1205                 eeprom_delay();
1206                 outb(wval,ioaddr+EEPROM_Ctrl);
1207                 eeprom_delay();
1208         }
1209         wval &= ~EC_Wr;
1210         outb(wval,ioaddr+EEPROM_Ctrl);
1211         for (i=0x8000 ; i ; i>>=1 )
1212         {
1213                 outb(wval|EC_Clk,ioaddr+EEPROM_Ctrl);
1214                 eeprom_delay();
1215                 if (inb(ioaddr+EEPROM_Ctrl)&EC_Rd)
1216                         rval |= i;
1217                 outb(wval,ioaddr+EEPROM_Ctrl);
1218                 eeprom_delay();
1219         }
1220         wval &= ~EC_CS;
1221         outb(wval|EC_Clk,ioaddr+EEPROM_Ctrl);
1222         eeprom_delay();
1223         outb(wval,ioaddr+EEPROM_Ctrl);
1224         eeprom_delay();
1225         return rval;
1226 }
1227
1228 /*
1229  * Reap tx buffers and return last transmit status.
1230  * if ==0 then either:
1231  *    a) we're not transmitting anything, so why are we here?
1232  *    b) we've died.
1233  * otherwise, Stat_Busy(return) means we've still got some packets
1234  * to transmit, Stat_Done(return) means our buffers should be empty
1235  * again
1236  */
1237
1238 static unsigned short eexp_hw_lasttxstat(struct net_device *dev)
1239 {
1240         struct net_local *lp = netdev_priv(dev);
1241         unsigned short tx_block = lp->tx_reap;
1242         unsigned short status;
1243
1244         if (!netif_queue_stopped(dev) && lp->tx_head==lp->tx_reap)
1245                 return 0x0000;
1246
1247         do
1248         {
1249                 outw(tx_block & ~31, dev->base_addr + SM_PTR);
1250                 status = inw(dev->base_addr + SHADOW(tx_block));
1251                 if (!Stat_Done(status))
1252                 {
1253                         lp->tx_link = tx_block;
1254                         return status;
1255                 }
1256                 else
1257                 {
1258                         lp->last_tx_restart = 0;
1259                         dev->stats.collisions += Stat_NoColl(status);
1260                         if (!Stat_OK(status))
1261                         {
1262                                 char *whatsup = NULL;
1263                                 dev->stats.tx_errors++;
1264                                 if (Stat_Abort(status))
1265                                         dev->stats.tx_aborted_errors++;
1266                                 if (Stat_TNoCar(status)) {
1267                                         whatsup = "aborted, no carrier";
1268                                         dev->stats.tx_carrier_errors++;
1269                                 }
1270                                 if (Stat_TNoCTS(status)) {
1271                                         whatsup = "aborted, lost CTS";
1272                                         dev->stats.tx_carrier_errors++;
1273                                 }
1274                                 if (Stat_TNoDMA(status)) {
1275                                         whatsup = "FIFO underran";
1276                                         dev->stats.tx_fifo_errors++;
1277                                 }
1278                                 if (Stat_TXColl(status)) {
1279                                         whatsup = "aborted, too many collisions";
1280                                         dev->stats.tx_aborted_errors++;
1281                                 }
1282                                 if (whatsup)
1283                                         printk(KERN_INFO "%s: transmit %s\n",
1284                                                dev->name, whatsup);
1285                         }
1286                         else
1287                                 dev->stats.tx_packets++;
1288                 }
1289                 if (tx_block == TX_BUF_START+((lp->num_tx_bufs-1)*TX_BUF_SIZE))
1290                         lp->tx_reap = tx_block = TX_BUF_START;
1291                 else
1292                         lp->tx_reap = tx_block += TX_BUF_SIZE;
1293                 netif_wake_queue(dev);
1294         }
1295         while (lp->tx_reap != lp->tx_head);
1296
1297         lp->tx_link = lp->tx_tail + 0x08;
1298
1299         return status;
1300 }
1301
1302 /*
1303  * This should never happen. It is called when some higher routine detects
1304  * that the CU has stopped, to try to restart it from the last packet we knew
1305  * we were working on, or the idle loop if we had finished for the time.
1306  */
1307
1308 static void eexp_hw_txrestart(struct net_device *dev)
1309 {
1310         struct net_local *lp = netdev_priv(dev);
1311         unsigned short ioaddr = dev->base_addr;
1312
1313         lp->last_tx_restart = lp->tx_link;
1314         scb_wrcbl(dev, lp->tx_link);
1315         scb_command(dev, SCB_CUstart);
1316         outb(0,ioaddr+SIGNAL_CA);
1317
1318         {
1319                 unsigned short boguscount=50,failcount=5;
1320                 while (!scb_status(dev))
1321                 {
1322                         if (!--boguscount)
1323                         {
1324                                 if (--failcount)
1325                                 {
1326                                         printk(KERN_WARNING "%s: CU start timed out, status %04x, cmd %04x\n", dev->name, scb_status(dev), scb_rdcmd(dev));
1327                                         scb_wrcbl(dev, lp->tx_link);
1328                                         scb_command(dev, SCB_CUstart);
1329                                         outb(0,ioaddr+SIGNAL_CA);
1330                                         boguscount = 100;
1331                                 }
1332                                 else
1333                                 {
1334                                         printk(KERN_WARNING "%s: Failed to restart CU, resetting board...\n",dev->name);
1335                                         eexp_hw_init586(dev);
1336                                         netif_wake_queue(dev);
1337                                         return;
1338                                 }
1339                         }
1340                 }
1341         }
1342 }
1343
1344 /*
1345  * Writes down the list of transmit buffers into card memory.  Each
1346  * entry consists of an 82586 transmit command, followed by a jump
1347  * pointing to itself.  When we want to transmit a packet, we write
1348  * the data into the appropriate transmit buffer and then modify the
1349  * preceding jump to point at the new transmit command.  This means that
1350  * the 586 command unit is continuously active.
1351  */
1352
1353 static void eexp_hw_txinit(struct net_device *dev)
1354 {
1355         struct net_local *lp = netdev_priv(dev);
1356         unsigned short tx_block = TX_BUF_START;
1357         unsigned short curtbuf;
1358         unsigned short ioaddr = dev->base_addr;
1359
1360         for ( curtbuf=0 ; curtbuf<lp->num_tx_bufs ; curtbuf++ )
1361         {
1362                 outw(tx_block, ioaddr + WRITE_PTR);
1363
1364                 outw(0x0000, ioaddr + DATAPORT);
1365                 outw(Cmd_INT|Cmd_Xmit, ioaddr + DATAPORT);
1366                 outw(tx_block+0x08, ioaddr + DATAPORT);
1367                 outw(tx_block+0x0e, ioaddr + DATAPORT);
1368
1369                 outw(0x0000, ioaddr + DATAPORT);
1370                 outw(0x0000, ioaddr + DATAPORT);
1371                 outw(tx_block+0x08, ioaddr + DATAPORT);
1372
1373                 outw(0x8000, ioaddr + DATAPORT);
1374                 outw(-1, ioaddr + DATAPORT);
1375                 outw(tx_block+0x16, ioaddr + DATAPORT);
1376                 outw(0x0000, ioaddr + DATAPORT);
1377
1378                 tx_block += TX_BUF_SIZE;
1379         }
1380         lp->tx_head = TX_BUF_START;
1381         lp->tx_reap = TX_BUF_START;
1382         lp->tx_tail = tx_block - TX_BUF_SIZE;
1383         lp->tx_link = lp->tx_tail + 0x08;
1384         lp->rx_buf_start = tx_block;
1385
1386 }
1387
1388 /*
1389  * Write the circular list of receive buffer descriptors to card memory.
1390  * The end of the list isn't marked, which means that the 82586 receive
1391  * unit will loop until buffers become available (this avoids it giving us
1392  * "out of resources" messages).
1393  */
1394
1395 static void eexp_hw_rxinit(struct net_device *dev)
1396 {
1397         struct net_local *lp = netdev_priv(dev);
1398         unsigned short rx_block = lp->rx_buf_start;
1399         unsigned short ioaddr = dev->base_addr;
1400
1401         lp->num_rx_bufs = 0;
1402         lp->rx_first = lp->rx_ptr = rx_block;
1403         do
1404         {
1405                 lp->num_rx_bufs++;
1406
1407                 outw(rx_block, ioaddr + WRITE_PTR);
1408
1409                 outw(0, ioaddr + DATAPORT);  outw(0, ioaddr+DATAPORT);
1410                 outw(rx_block + RX_BUF_SIZE, ioaddr+DATAPORT);
1411                 outw(0xffff, ioaddr+DATAPORT);
1412
1413                 outw(0x0000, ioaddr+DATAPORT);
1414                 outw(0xdead, ioaddr+DATAPORT);
1415                 outw(0xdead, ioaddr+DATAPORT);
1416                 outw(0xdead, ioaddr+DATAPORT);
1417                 outw(0xdead, ioaddr+DATAPORT);
1418                 outw(0xdead, ioaddr+DATAPORT);
1419                 outw(0xdead, ioaddr+DATAPORT);
1420
1421                 outw(0x0000, ioaddr+DATAPORT);
1422                 outw(rx_block + RX_BUF_SIZE + 0x16, ioaddr+DATAPORT);
1423                 outw(rx_block + 0x20, ioaddr+DATAPORT);
1424                 outw(0, ioaddr+DATAPORT);
1425                 outw(RX_BUF_SIZE-0x20, ioaddr+DATAPORT);
1426
1427                 lp->rx_last = rx_block;
1428                 rx_block += RX_BUF_SIZE;
1429         } while (rx_block <= lp->rx_buf_end-RX_BUF_SIZE);
1430
1431
1432         /* Make first Rx frame descriptor point to first Rx buffer
1433            descriptor */
1434         outw(lp->rx_first + 6, ioaddr+WRITE_PTR);
1435         outw(lp->rx_first + 0x16, ioaddr+DATAPORT);
1436
1437         /* Close Rx frame descriptor ring */
1438         outw(lp->rx_last + 4, ioaddr+WRITE_PTR);
1439         outw(lp->rx_first, ioaddr+DATAPORT);
1440
1441         /* Close Rx buffer descriptor ring */
1442         outw(lp->rx_last + 0x16 + 2, ioaddr+WRITE_PTR);
1443         outw(lp->rx_first + 0x16, ioaddr+DATAPORT);
1444
1445 }
1446
1447 /*
1448  * Un-reset the 586, and start the configuration sequence. We don't wait for
1449  * this to finish, but allow the interrupt handler to start the CU and RU for
1450  * us.  We can't start the receive/transmission system up before we know that
1451  * the hardware is configured correctly.
1452  */
1453
1454 static void eexp_hw_init586(struct net_device *dev)
1455 {
1456         struct net_local *lp = netdev_priv(dev);
1457         unsigned short ioaddr = dev->base_addr;
1458         int i;
1459
1460 #if NET_DEBUG > 6
1461         printk("%s: eexp_hw_init586()\n", dev->name);
1462 #endif
1463
1464         lp->started = 0;
1465
1466         set_loopback(dev);
1467
1468         outb(SIRQ_dis|irqrmap[dev->irq],ioaddr+SET_IRQ);
1469
1470         /* Download the startup code */
1471         outw(lp->rx_buf_end & ~31, ioaddr + SM_PTR);
1472         outw(lp->width?0x0001:0x0000, ioaddr + 0x8006);
1473         outw(0x0000, ioaddr + 0x8008);
1474         outw(0x0000, ioaddr + 0x800a);
1475         outw(0x0000, ioaddr + 0x800c);
1476         outw(0x0000, ioaddr + 0x800e);
1477
1478         for (i = 0; i < ARRAY_SIZE(start_code) * 2; i+=32) {
1479                 int j;
1480                 outw(i, ioaddr + SM_PTR);
1481                 for (j = 0; j < 16 && (i+j)/2 < ARRAY_SIZE(start_code); j+=2)
1482                         outw(start_code[(i+j)/2],
1483                              ioaddr+0x4000+j);
1484                 for (j = 0; j < 16 && (i+j+16)/2 < ARRAY_SIZE(start_code); j+=2)
1485                         outw(start_code[(i+j+16)/2],
1486                              ioaddr+0x8000+j);
1487         }
1488
1489         /* Do we want promiscuous mode or multicast? */
1490         outw(CONF_PROMISC & ~31, ioaddr+SM_PTR);
1491         i = inw(ioaddr+SHADOW(CONF_PROMISC));
1492         outw((dev->flags & IFF_PROMISC)?(i|1):(i & ~1),
1493              ioaddr+SHADOW(CONF_PROMISC));
1494         lp->was_promisc = dev->flags & IFF_PROMISC;
1495 #if 0
1496         eexp_setup_filter(dev);
1497 #endif
1498
1499         /* Write our hardware address */
1500         outw(CONF_HWADDR & ~31, ioaddr+SM_PTR);
1501         outw(((unsigned short *)dev->dev_addr)[0], ioaddr+SHADOW(CONF_HWADDR));
1502         outw(((unsigned short *)dev->dev_addr)[1],
1503              ioaddr+SHADOW(CONF_HWADDR+2));
1504         outw(((unsigned short *)dev->dev_addr)[2],
1505              ioaddr+SHADOW(CONF_HWADDR+4));
1506
1507         eexp_hw_txinit(dev);
1508         eexp_hw_rxinit(dev);
1509
1510         outb(0,ioaddr+EEPROM_Ctrl);
1511         mdelay(5);
1512
1513         scb_command(dev, 0xf000);
1514         outb(0,ioaddr+SIGNAL_CA);
1515
1516         outw(0, ioaddr+SM_PTR);
1517
1518         {
1519                 unsigned short rboguscount=50,rfailcount=5;
1520                 while (inw(ioaddr+0x4000))
1521                 {
1522                         if (!--rboguscount)
1523                         {
1524                                 printk(KERN_WARNING "%s: i82586 reset timed out, kicking...\n",
1525                                         dev->name);
1526                                 scb_command(dev, 0);
1527                                 outb(0,ioaddr+SIGNAL_CA);
1528                                 rboguscount = 100;
1529                                 if (!--rfailcount)
1530                                 {
1531                                         printk(KERN_WARNING "%s: i82586 not responding, giving up.\n",
1532                                                 dev->name);
1533                                         return;
1534                                 }
1535                         }
1536                 }
1537         }
1538
1539         scb_wrcbl(dev, CONF_LINK);
1540         scb_command(dev, 0xf000|SCB_CUstart);
1541         outb(0,ioaddr+SIGNAL_CA);
1542
1543         {
1544                 unsigned short iboguscount=50,ifailcount=5;
1545                 while (!scb_status(dev))
1546                 {
1547                         if (!--iboguscount)
1548                         {
1549                                 if (--ifailcount)
1550                                 {
1551                                         printk(KERN_WARNING "%s: i82586 initialization timed out, status %04x, cmd %04x\n",
1552                                                 dev->name, scb_status(dev), scb_rdcmd(dev));
1553                                         scb_wrcbl(dev, CONF_LINK);
1554                                         scb_command(dev, 0xf000|SCB_CUstart);
1555                                         outb(0,ioaddr+SIGNAL_CA);
1556                                         iboguscount = 100;
1557                                 }
1558                                 else
1559                                 {
1560                                         printk(KERN_WARNING "%s: Failed to initialize i82586, giving up.\n",dev->name);
1561                                         return;
1562                                 }
1563                         }
1564                 }
1565         }
1566
1567         clear_loopback(dev);
1568         outb(SIRQ_en|irqrmap[dev->irq],ioaddr+SET_IRQ);
1569
1570         lp->init_time = jiffies;
1571 #if NET_DEBUG > 6
1572         printk("%s: leaving eexp_hw_init586()\n", dev->name);
1573 #endif
1574         return;
1575 }
1576
1577 static void eexp_setup_filter(struct net_device *dev)
1578 {
1579         struct dev_mc_list *dmi;
1580         unsigned short ioaddr = dev->base_addr;
1581         int count = dev->mc_count;
1582         int i;
1583         if (count > 8) {
1584                 printk(KERN_INFO "%s: too many multicast addresses (%d)\n",
1585                        dev->name, count);
1586                 count = 8;
1587         }
1588
1589         outw(CONF_NR_MULTICAST & ~31, ioaddr+SM_PTR);
1590         outw(6*count, ioaddr+SHADOW(CONF_NR_MULTICAST));
1591         for (i = 0, dmi = dev->mc_list; i < count; i++, dmi = dmi->next) {
1592                 unsigned short *data;
1593                 if (!dmi) {
1594                         printk(KERN_INFO "%s: too few multicast addresses\n", dev->name);
1595                         break;
1596                 }
1597                 if (dmi->dmi_addrlen != ETH_ALEN) {
1598                         printk(KERN_INFO "%s: invalid multicast address length given.\n", dev->name);
1599                         continue;
1600                 }
1601                 data = (unsigned short *)dmi->dmi_addr;
1602                 outw((CONF_MULTICAST+(6*i)) & ~31, ioaddr+SM_PTR);
1603                 outw(data[0], ioaddr+SHADOW(CONF_MULTICAST+(6*i)));
1604                 outw((CONF_MULTICAST+(6*i)+2) & ~31, ioaddr+SM_PTR);
1605                 outw(data[1], ioaddr+SHADOW(CONF_MULTICAST+(6*i)+2));
1606                 outw((CONF_MULTICAST+(6*i)+4) & ~31, ioaddr+SM_PTR);
1607                 outw(data[2], ioaddr+SHADOW(CONF_MULTICAST+(6*i)+4));
1608         }
1609 }
1610
1611 /*
1612  * Set or clear the multicast filter for this adaptor.
1613  */
1614 static void
1615 eexp_set_multicast(struct net_device *dev)
1616 {
1617         unsigned short ioaddr = dev->base_addr;
1618         struct net_local *lp = netdev_priv(dev);
1619         int kick = 0, i;
1620         if ((dev->flags & IFF_PROMISC) != lp->was_promisc) {
1621                 outw(CONF_PROMISC & ~31, ioaddr+SM_PTR);
1622                 i = inw(ioaddr+SHADOW(CONF_PROMISC));
1623                 outw((dev->flags & IFF_PROMISC)?(i|1):(i & ~1),
1624                      ioaddr+SHADOW(CONF_PROMISC));
1625                 lp->was_promisc = dev->flags & IFF_PROMISC;
1626                 kick = 1;
1627         }
1628         if (!(dev->flags & IFF_PROMISC)) {
1629                 eexp_setup_filter(dev);
1630                 if (lp->old_mc_count != dev->mc_count) {
1631                         kick = 1;
1632                         lp->old_mc_count = dev->mc_count;
1633                 }
1634         }
1635         if (kick) {
1636                 unsigned long oj;
1637                 scb_command(dev, SCB_CUsuspend);
1638                 outb(0, ioaddr+SIGNAL_CA);
1639                 outb(0, ioaddr+SIGNAL_CA);
1640 #if 0
1641                 printk("%s: waiting for CU to go suspended\n", dev->name);
1642 #endif
1643                 oj = jiffies;
1644                 while ((SCB_CUstat(scb_status(dev)) == 2) &&
1645                        (time_before(jiffies, oj + 2000)));
1646                 if (SCB_CUstat(scb_status(dev)) == 2)
1647                         printk("%s: warning, CU didn't stop\n", dev->name);
1648                 lp->started &= ~(STARTED_CU);
1649                 scb_wrcbl(dev, CONF_LINK);
1650                 scb_command(dev, SCB_CUstart);
1651                 outb(0, ioaddr+SIGNAL_CA);
1652         }
1653 }
1654
1655
1656 /*
1657  * MODULE stuff
1658  */
1659
1660 #ifdef MODULE
1661
1662 #define EEXP_MAX_CARDS     4    /* max number of cards to support */
1663
1664 static struct net_device *dev_eexp[EEXP_MAX_CARDS];
1665 static int irq[EEXP_MAX_CARDS];
1666 static int io[EEXP_MAX_CARDS];
1667
1668 module_param_array(io, int, NULL, 0);
1669 module_param_array(irq, int, NULL, 0);
1670 MODULE_PARM_DESC(io, "EtherExpress 16 I/O base address(es)");
1671 MODULE_PARM_DESC(irq, "EtherExpress 16 IRQ number(s)");
1672 MODULE_LICENSE("GPL");
1673
1674
1675 /* Ideally the user would give us io=, irq= for every card.  If any parameters
1676  * are specified, we verify and then use them.  If no parameters are given, we
1677  * autoprobe for one card only.
1678  */
1679 int __init init_module(void)
1680 {
1681         struct net_device *dev;
1682         int this_dev, found = 0;
1683
1684         for (this_dev = 0; this_dev < EEXP_MAX_CARDS; this_dev++) {
1685                 dev = alloc_etherdev(sizeof(struct net_local));
1686                 dev->irq = irq[this_dev];
1687                 dev->base_addr = io[this_dev];
1688                 if (io[this_dev] == 0) {
1689                         if (this_dev)
1690                                 break;
1691                         printk(KERN_NOTICE "eexpress.c: Module autoprobe not recommended, give io=xx.\n");
1692                 }
1693                 if (do_express_probe(dev) == 0) {
1694                         dev_eexp[this_dev] = dev;
1695                         found++;
1696                         continue;
1697                 }
1698                 printk(KERN_WARNING "eexpress.c: Failed to register card at 0x%x.\n", io[this_dev]);
1699                 free_netdev(dev);
1700                 break;
1701         }
1702         if (found)
1703                 return 0;
1704         return -ENXIO;
1705 }
1706
1707 void __exit cleanup_module(void)
1708 {
1709         int this_dev;
1710
1711         for (this_dev = 0; this_dev < EEXP_MAX_CARDS; this_dev++) {
1712                 struct net_device *dev = dev_eexp[this_dev];
1713                 if (dev) {
1714                         unregister_netdev(dev);
1715                         free_netdev(dev);
1716                 }
1717         }
1718 }
1719 #endif
1720
1721 /*
1722  * Local Variables:
1723  *  c-file-style: "linux"
1724  *  tab-width: 8
1725  * End:
1726  */