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