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