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