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