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