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