vserver 1.9.5.x5
[linux-2.6.git] / drivers / net / arm / ether3.c
1 /*
2  *  linux/drivers/acorn/net/ether3.c
3  *
4  *  Copyright (C) 1995-2000 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * SEEQ nq8005 ethernet driver for Acorn/ANT Ether3 card
11  *  for Acorn machines
12  *
13  * By Russell King, with some suggestions from borris@ant.co.uk
14  *
15  * Changelog:
16  * 1.04 RMK     29/02/1996      Won't pass packets that are from our ethernet
17  *                              address up to the higher levels - they're
18  *                              silently ignored.  I/F can now be put into
19  *                              multicast mode.  Receiver routine optimised.
20  * 1.05 RMK     30/02/1996      Now claims interrupt at open when part of
21  *                              the kernel rather than when a module.
22  * 1.06 RMK     02/03/1996      Various code cleanups
23  * 1.07 RMK     13/10/1996      Optimised interrupt routine and transmit
24  *                              routines.
25  * 1.08 RMK     14/10/1996      Fixed problem with too many packets,
26  *                              prevented the kernel message about dropped
27  *                              packets appearing too many times a second.
28  *                              Now does not disable all IRQs, only the IRQ
29  *                              used by this card.
30  * 1.09 RMK     10/11/1996      Only enables TX irq when buffer space is low,
31  *                              but we still service the TX queue if we get a
32  *                              RX interrupt.
33  * 1.10 RMK     15/07/1997      Fixed autoprobing of NQ8004.
34  * 1.11 RMK     16/11/1997      Fixed autoprobing of NQ8005A.
35  * 1.12 RMK     31/12/1997      Removed reference to dev_tint for Linux 2.1.
36  *      RMK     27/06/1998      Changed asm/delay.h to linux/delay.h.
37  * 1.13 RMK     29/06/1998      Fixed problem with transmission of packets.
38  *                              Chip seems to have a bug in, whereby if the
39  *                              packet starts two bytes from the end of the
40  *                              buffer, it corrupts the receiver chain, and
41  *                              never updates the transmit status correctly.
42  * 1.14 RMK     07/01/1998      Added initial code for ETHERB addressing.
43  * 1.15 RMK     30/04/1999      More fixes to the transmit routine for buggy
44  *                              hardware.
45  * 1.16 RMK     10/02/2000      Updated for 2.3.43
46  * 1.17 RMK     13/05/2000      Updated for 2.3.99-pre8
47  */
48
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/sched.h>
52 #include <linux/types.h>
53 #include <linux/fcntl.h>
54 #include <linux/interrupt.h>
55 #include <linux/ptrace.h>
56 #include <linux/ioport.h>
57 #include <linux/in.h>
58 #include <linux/slab.h>
59 #include <linux/string.h>
60 #include <linux/errno.h>
61 #include <linux/netdevice.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
64 #include <linux/device.h>
65 #include <linux/init.h>
66 #include <linux/delay.h>
67 #include <linux/bitops.h>
68
69 #include <asm/system.h>
70 #include <asm/ecard.h>
71 #include <asm/io.h>
72 #include <asm/irq.h>
73
74 static char version[] __initdata = "ether3 ethernet driver (c) 1995-2000 R.M.King v1.17\n";
75
76 #include "ether3.h"
77
78 static unsigned int net_debug = NET_DEBUG;
79
80 static void     ether3_setmulticastlist(struct net_device *dev);
81 static int      ether3_rx(struct net_device *dev, struct dev_priv *priv, unsigned int maxcnt);
82 static void     ether3_tx(struct net_device *dev, struct dev_priv *priv);
83 static int      ether3_open (struct net_device *dev);
84 static int      ether3_sendpacket (struct sk_buff *skb, struct net_device *dev);
85 static irqreturn_t ether3_interrupt (int irq, void *dev_id, struct pt_regs *regs);
86 static int      ether3_close (struct net_device *dev);
87 static struct net_device_stats *ether3_getstats (struct net_device *dev);
88 static void     ether3_setmulticastlist (struct net_device *dev);
89 static void     ether3_timeout(struct net_device *dev);
90
91 #define BUS_16          2
92 #define BUS_8           1
93 #define BUS_UNKNOWN     0
94
95 /* --------------------------------------------------------------------------- */
96
97 typedef enum {
98         buffer_write,
99         buffer_read
100 } buffer_rw_t;
101
102 /*
103  * ether3 read/write.  Slow things down a bit...
104  * The SEEQ8005 doesn't like us writing to its registers
105  * too quickly.
106  */
107 static inline void ether3_outb(int v, const int r)
108 {
109         outb(v, r);
110         udelay(1);
111 }
112
113 static inline void ether3_outw(int v, const int r)
114 {
115         outw(v, r);
116         udelay(1);
117 }
118 #define ether3_inb(r)           ({ unsigned int __v = inb((r)); udelay(1); __v; })
119 #define ether3_inw(r)           ({ unsigned int __v = inw((r)); udelay(1); __v; })
120
121 static int
122 ether3_setbuffer(struct net_device *dev, buffer_rw_t read, int start)
123 {
124         struct dev_priv *priv = netdev_priv(dev);
125         int timeout = 1000;
126
127         ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
128         ether3_outw(priv->regs.command | CMD_FIFOWRITE, REG_COMMAND);
129
130         while ((ether3_inw(REG_STATUS) & STAT_FIFOEMPTY) == 0) {
131                 if (!timeout--) {
132                         printk("%s: setbuffer broken\n", dev->name);
133                         priv->broken = 1;
134                         return 1;
135                 }
136                 udelay(1);
137         }
138
139         if (read == buffer_read) {
140                 ether3_outw(start, REG_DMAADDR);
141                 ether3_outw(priv->regs.command | CMD_FIFOREAD, REG_COMMAND);
142         } else {
143                 ether3_outw(priv->regs.command | CMD_FIFOWRITE, REG_COMMAND);
144                 ether3_outw(start, REG_DMAADDR);
145         }
146         return 0;
147 }
148
149 /*
150  * write data to the buffer memory
151  */
152 #define ether3_writebuffer(dev,data,length)                     \
153         outsw(REG_BUFWIN, (data), (length) >> 1)
154
155 #define ether3_writeword(dev,data)                              \
156         outw((data), REG_BUFWIN)
157
158 #define ether3_writelong(dev,data)      {                       \
159         unsigned long reg_bufwin = REG_BUFWIN;                  \
160         outw((data), reg_bufwin);                               \
161         outw((data) >> 16, reg_bufwin);                         \
162 }
163
164 /*
165  * read data from the buffer memory
166  */
167 #define ether3_readbuffer(dev,data,length)                      \
168         insw(REG_BUFWIN, (data), (length) >> 1)
169
170 #define ether3_readword(dev)                                    \
171         inw(REG_BUFWIN)
172
173 #define ether3_readlong(dev)                                    \
174         inw(REG_BUFWIN) | (inw(REG_BUFWIN) << 16)
175
176 /*
177  * Switch LED off...
178  */
179 static void
180 ether3_ledoff(unsigned long data)
181 {
182         struct net_device *dev = (struct net_device *)data;
183         struct dev_priv *priv = netdev_priv(dev);
184         ether3_outw(priv->regs.config2 |= CFG2_CTRLO, REG_CONFIG2);
185 }
186
187 /*
188  * switch LED on...
189  */
190 static inline void
191 ether3_ledon(struct net_device *dev, struct dev_priv *priv)
192 {
193         del_timer(&priv->timer);
194         priv->timer.expires = jiffies + HZ / 50; /* leave on for 1/50th second */
195         priv->timer.data = (unsigned long)dev;
196         priv->timer.function = ether3_ledoff;
197         add_timer(&priv->timer);
198         if (priv->regs.config2 & CFG2_CTRLO)
199                 ether3_outw(priv->regs.config2 &= ~CFG2_CTRLO, REG_CONFIG2);
200 }
201
202 /*
203  * Read the ethernet address string from the on board rom.
204  * This is an ascii string!!!
205  */
206 static int __init
207 ether3_addr(char *addr, struct expansion_card *ec)
208 {
209         struct in_chunk_dir cd;
210         char *s;
211         
212         if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
213                 int i;
214                 for (i = 0; i<6; i++) {
215                         addr[i] = simple_strtoul(s + 1, &s, 0x10);
216                         if (*s != (i==5?')' : ':' ))
217                                 break;
218                 }
219                 if (i == 6)
220                         return 0;
221         }
222         /* I wonder if we should even let the user continue in this case
223          *   - no, it would be better to disable the device
224          */
225         printk(KERN_ERR "ether3: Couldn't read a valid MAC address from card.\n");
226         return -ENODEV;
227 }
228
229 /* --------------------------------------------------------------------------- */
230
231 static int __init
232 ether3_ramtest(struct net_device *dev, unsigned char byte)
233 {
234         unsigned char *buffer = kmalloc(RX_END, GFP_KERNEL);
235         int i,ret = 0;
236         int max_errors = 4;
237         int bad = -1;
238
239         if (!buffer)
240                 return 1;
241
242         memset(buffer, byte, RX_END);
243         ether3_setbuffer(dev, buffer_write, 0);
244         ether3_writebuffer(dev, buffer, TX_END);
245         ether3_setbuffer(dev, buffer_write, RX_START);
246         ether3_writebuffer(dev, buffer + RX_START, RX_LEN);
247         memset(buffer, byte ^ 0xff, RX_END);
248         ether3_setbuffer(dev, buffer_read, 0);
249         ether3_readbuffer(dev, buffer, TX_END);
250         ether3_setbuffer(dev, buffer_read, RX_START);
251         ether3_readbuffer(dev, buffer + RX_START, RX_LEN);
252
253         for (i = 0; i < RX_END; i++) {
254                 if (buffer[i] != byte) {
255                         if (max_errors > 0 && bad != buffer[i]) {
256                                 printk("%s: RAM failed with (%02X instead of %02X) at 0x%04X",
257                                        dev->name, buffer[i], byte, i);
258                                 ret = 2;
259                                 max_errors--;
260                                 bad = i;
261                         }
262                 } else {
263                         if (bad != -1) {
264                                 if (bad != i - 1)
265                                         printk(" - 0x%04X\n", i - 1);
266                                 printk("\n");
267                                 bad = -1;
268                         }
269                 }
270         }
271         if (bad != -1)
272                 printk(" - 0xffff\n");
273         kfree(buffer);
274
275         return ret;
276 }
277
278 /* ------------------------------------------------------------------------------- */
279
280 static int __init
281 ether3_init_2(struct net_device *dev)
282 {
283         struct dev_priv *priv = netdev_priv(dev);
284         int i;
285
286         priv->regs.config1 = CFG1_RECVCOMPSTAT0|CFG1_DMABURST8;
287         priv->regs.config2 = CFG2_CTRLO|CFG2_RECVCRC|CFG2_ERRENCRC;
288         priv->regs.command = 0;
289
290         /*
291          * Set up our hardware address
292          */
293         ether3_outw(priv->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
294         for (i = 0; i < 6; i++)
295                 ether3_outb(dev->dev_addr[i], REG_BUFWIN);
296
297         if (dev->flags & IFF_PROMISC)
298                 priv->regs.config1 |= CFG1_RECVPROMISC;
299         else if (dev->flags & IFF_MULTICAST)
300                 priv->regs.config1 |= CFG1_RECVSPECBRMULTI;
301         else
302                 priv->regs.config1 |= CFG1_RECVSPECBROAD;
303
304         /*
305          * There is a problem with the NQ8005 in that it occasionally loses the
306          * last two bytes.  To get round this problem, we receive the CRC as
307          * well.  That way, if we do lose the last two, then it doesn't matter.
308          */
309         ether3_outw(priv->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
310         ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
311         ether3_outw(priv->rx_head, REG_RECVPTR);
312         ether3_outw(0, REG_TRANSMITPTR);
313         ether3_outw(priv->rx_head >> 8, REG_RECVEND);
314         ether3_outw(priv->regs.config2, REG_CONFIG2);
315         ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
316         ether3_outw(priv->regs.command, REG_COMMAND);
317
318         i = ether3_ramtest(dev, 0x5A);
319         if(i)
320                 return i;
321         i = ether3_ramtest(dev, 0x1E);
322         if(i)
323                 return i;
324
325         ether3_setbuffer(dev, buffer_write, 0);
326         ether3_writelong(dev, 0);
327         return 0;
328 }
329
330 static void
331 ether3_init_for_open(struct net_device *dev)
332 {
333         struct dev_priv *priv = netdev_priv(dev);
334         int i;
335
336         memset(&priv->stats, 0, sizeof(struct net_device_stats));
337
338         /* Reset the chip */
339         ether3_outw(CFG2_RESET, REG_CONFIG2);
340         udelay(4);
341
342         priv->regs.command = 0;
343         ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
344         while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON));
345
346         ether3_outw(priv->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
347         for (i = 0; i < 6; i++)
348                 ether3_outb(dev->dev_addr[i], REG_BUFWIN);
349
350         priv->tx_head   = 0;
351         priv->tx_tail   = 0;
352         priv->regs.config2 |= CFG2_CTRLO;
353         priv->rx_head   = RX_START;
354
355         ether3_outw(priv->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
356         ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
357         ether3_outw(priv->rx_head, REG_RECVPTR);
358         ether3_outw(priv->rx_head >> 8, REG_RECVEND);
359         ether3_outw(0, REG_TRANSMITPTR);
360         ether3_outw(priv->regs.config2, REG_CONFIG2);
361         ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
362
363         ether3_setbuffer(dev, buffer_write, 0);
364         ether3_writelong(dev, 0);
365
366         priv->regs.command = CMD_ENINTRX | CMD_ENINTTX;
367         ether3_outw(priv->regs.command | CMD_RXON, REG_COMMAND);
368 }
369
370 static inline int
371 ether3_probe_bus_8(struct net_device *dev, int val)
372 {
373         int write_low, write_high, read_low, read_high;
374
375         write_low = val & 255;
376         write_high = val >> 8;
377
378         printk(KERN_DEBUG "ether3_probe: write8 [%02X:%02X]", write_high, write_low);
379
380         ether3_outb(write_low, REG_RECVPTR);
381         ether3_outb(write_high, REG_RECVPTR + 1);
382
383         read_low = ether3_inb(REG_RECVPTR);
384         read_high = ether3_inb(REG_RECVPTR + 1);
385
386         printk(", read8 [%02X:%02X]\n", read_high, read_low);
387
388         return read_low == write_low && read_high == write_high;
389 }
390
391 static inline int
392 ether3_probe_bus_16(struct net_device *dev, int val)
393 {
394         int read_val;
395
396         ether3_outw(val, REG_RECVPTR);
397         read_val = ether3_inw(REG_RECVPTR);
398
399         printk(KERN_DEBUG "ether3_probe: write16 [%04X], read16 [%04X]\n", val, read_val);
400
401         return read_val == val;
402 }
403
404 /*
405  * Open/initialize the board.  This is called (in the current kernel)
406  * sometime after booting when the 'ifconfig' program is run.
407  *
408  * This routine should set everything up anew at each open, even
409  * registers that "should" only need to be set once at boot, so that
410  * there is non-reboot way to recover if something goes wrong.
411  */
412 static int
413 ether3_open(struct net_device *dev)
414 {
415         if (!is_valid_ether_addr(dev->dev_addr)) {
416                 printk(KERN_WARNING "%s: invalid ethernet MAC address\n",
417                         dev->name);
418                 return -EINVAL;
419         }
420
421         if (request_irq(dev->irq, ether3_interrupt, 0, "ether3", dev))
422                 return -EAGAIN;
423
424         ether3_init_for_open(dev);
425
426         netif_start_queue(dev);
427
428         return 0;
429 }
430
431 /*
432  * The inverse routine to ether3_open().
433  */
434 static int
435 ether3_close(struct net_device *dev)
436 {
437         struct dev_priv *priv = netdev_priv(dev);
438
439         netif_stop_queue(dev);
440
441         disable_irq(dev->irq);
442
443         ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
444         priv->regs.command = 0;
445         while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON));
446         ether3_outb(0x80, REG_CONFIG2 + 1);
447         ether3_outw(0, REG_COMMAND);
448
449         free_irq(dev->irq, dev);
450
451         return 0;
452 }
453
454 /*
455  * Get the current statistics.  This may be called with the card open or
456  * closed.
457  */
458 static struct net_device_stats *ether3_getstats(struct net_device *dev)
459 {
460         struct dev_priv *priv = netdev_priv(dev);
461         return &priv->stats;
462 }
463
464 /*
465  * Set or clear promiscuous/multicast mode filter for this adaptor.
466  *
467  * We don't attempt any packet filtering.  The card may have a SEEQ 8004
468  * in which does not have the other ethernet address registers present...
469  */
470 static void ether3_setmulticastlist(struct net_device *dev)
471 {
472         struct dev_priv *priv = netdev_priv(dev);
473
474         priv->regs.config1 &= ~CFG1_RECVPROMISC;
475
476         if (dev->flags & IFF_PROMISC) {
477                 /* promiscuous mode */
478                 priv->regs.config1 |= CFG1_RECVPROMISC;
479         } else if (dev->flags & IFF_ALLMULTI) {
480                 priv->regs.config1 |= CFG1_RECVSPECBRMULTI;
481         } else
482                 priv->regs.config1 |= CFG1_RECVSPECBROAD;
483
484         ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
485 }
486
487 static void
488 ether3_timeout(struct net_device *dev)
489 {
490         struct dev_priv *priv = netdev_priv(dev);
491         unsigned long flags;
492
493         del_timer(&priv->timer);
494
495         local_irq_save(flags);
496         printk(KERN_ERR "%s: transmit timed out, network cable problem?\n", dev->name);
497         printk(KERN_ERR "%s: state: { status=%04X cfg1=%04X cfg2=%04X }\n", dev->name,
498                 ether3_inw(REG_STATUS), ether3_inw(REG_CONFIG1), ether3_inw(REG_CONFIG2));
499         printk(KERN_ERR "%s: { rpr=%04X rea=%04X tpr=%04X }\n", dev->name,
500                 ether3_inw(REG_RECVPTR), ether3_inw(REG_RECVEND), ether3_inw(REG_TRANSMITPTR));
501         printk(KERN_ERR "%s: tx head=%X tx tail=%X\n", dev->name,
502                 priv->tx_head, priv->tx_tail);
503         ether3_setbuffer(dev, buffer_read, priv->tx_tail);
504         printk(KERN_ERR "%s: packet status = %08X\n", dev->name, ether3_readlong(dev));
505         local_irq_restore(flags);
506
507         priv->regs.config2 |= CFG2_CTRLO;
508         priv->stats.tx_errors += 1;
509         ether3_outw(priv->regs.config2, REG_CONFIG2);
510         priv->tx_head = priv->tx_tail = 0;
511
512         netif_wake_queue(dev);
513 }
514
515 /*
516  * Transmit a packet
517  */
518 static int
519 ether3_sendpacket(struct sk_buff *skb, struct net_device *dev)
520 {
521         struct dev_priv *priv = netdev_priv(dev);
522         unsigned long flags;
523         unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
524         unsigned int ptr, next_ptr;
525
526         if (priv->broken) {
527                 dev_kfree_skb(skb);
528                 priv->stats.tx_dropped ++;
529                 netif_start_queue(dev);
530                 return 0;
531         }
532
533         length = (length + 1) & ~1;
534         if (length != skb->len) {
535                 skb = skb_padto(skb, length);
536                 if (skb == NULL)
537                         goto out;
538         }
539
540         next_ptr = (priv->tx_head + 1) & 15;
541
542         local_irq_save(flags);
543
544         if (priv->tx_tail == next_ptr) {
545                 local_irq_restore(flags);
546                 return 1;       /* unable to queue */
547         }
548
549         dev->trans_start = jiffies;
550         ptr              = 0x600 * priv->tx_head;
551         priv->tx_head    = next_ptr;
552         next_ptr        *= 0x600;
553
554 #define TXHDR_FLAGS (TXHDR_TRANSMIT|TXHDR_CHAINCONTINUE|TXHDR_DATAFOLLOWS|TXHDR_ENSUCCESS)
555
556         ether3_setbuffer(dev, buffer_write, next_ptr);
557         ether3_writelong(dev, 0);
558         ether3_setbuffer(dev, buffer_write, ptr);
559         ether3_writelong(dev, 0);
560         ether3_writebuffer(dev, skb->data, length);
561         ether3_writeword(dev, htons(next_ptr));
562         ether3_writeword(dev, TXHDR_CHAINCONTINUE >> 16);
563         ether3_setbuffer(dev, buffer_write, ptr);
564         ether3_writeword(dev, htons((ptr + length + 4)));
565         ether3_writeword(dev, TXHDR_FLAGS >> 16);
566         ether3_ledon(dev, priv);
567
568         if (!(ether3_inw(REG_STATUS) & STAT_TXON)) {
569                 ether3_outw(ptr, REG_TRANSMITPTR);
570                 ether3_outw(priv->regs.command | CMD_TXON, REG_COMMAND);
571         }
572
573         next_ptr = (priv->tx_head + 1) & 15;
574         local_irq_restore(flags);
575
576         dev_kfree_skb(skb);
577
578         if (priv->tx_tail == next_ptr)
579                 netif_stop_queue(dev);
580
581  out:
582         return 0;
583 }
584
585 static irqreturn_t
586 ether3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
587 {
588         struct net_device *dev = (struct net_device *)dev_id;
589         struct dev_priv *priv;
590         unsigned int status, handled = IRQ_NONE;
591
592 #if NET_DEBUG > 1
593         if(net_debug & DEBUG_INT)
594                 printk("eth3irq: %d ", irq);
595 #endif
596
597         priv = netdev_priv(dev);
598
599         status = ether3_inw(REG_STATUS);
600
601         if (status & STAT_INTRX) {
602                 ether3_outw(CMD_ACKINTRX | priv->regs.command, REG_COMMAND);
603                 ether3_rx(dev, priv, 12);
604                 handled = IRQ_HANDLED;
605         }
606
607         if (status & STAT_INTTX) {
608                 ether3_outw(CMD_ACKINTTX | priv->regs.command, REG_COMMAND);
609                 ether3_tx(dev, priv);
610                 handled = IRQ_HANDLED;
611         }
612
613 #if NET_DEBUG > 1
614         if(net_debug & DEBUG_INT)
615                 printk("done\n");
616 #endif
617         return handled;
618 }
619
620 /*
621  * If we have a good packet(s), get it/them out of the buffers.
622  */
623 static int
624 ether3_rx(struct net_device *dev, struct dev_priv *priv, unsigned int maxcnt)
625 {
626         unsigned int next_ptr = priv->rx_head, received = 0;
627         ether3_ledon(dev, priv);
628
629         do {
630                 unsigned int this_ptr, status;
631                 unsigned char addrs[16];
632
633                 /*
634                  * read the first 16 bytes from the buffer.
635                  * This contains the status bytes etc and ethernet addresses,
636                  * and we also check the source ethernet address to see if
637                  * it originated from us.
638                  */
639                 {
640                         unsigned int temp_ptr;
641                         ether3_setbuffer(dev, buffer_read, next_ptr);
642                         temp_ptr = ether3_readword(dev);
643                         status = ether3_readword(dev);
644                         if ((status & (RXSTAT_DONE | RXHDR_CHAINCONTINUE | RXHDR_RECEIVE)) !=
645                                 (RXSTAT_DONE | RXHDR_CHAINCONTINUE) || !temp_ptr)
646                                 break;
647
648                         this_ptr = next_ptr + 4;
649                         next_ptr = ntohs(temp_ptr);
650                 }
651                 ether3_setbuffer(dev, buffer_read, this_ptr);
652                 ether3_readbuffer(dev, addrs+2, 12);
653
654 if (next_ptr < RX_START || next_ptr >= RX_END) {
655  int i;
656  printk("%s: bad next pointer @%04X: ", dev->name, priv->rx_head);
657  printk("%02X %02X %02X %02X ", next_ptr >> 8, next_ptr & 255, status & 255, status >> 8);
658  for (i = 2; i < 14; i++)
659    printk("%02X ", addrs[i]);
660  printk("\n");
661  next_ptr = priv->rx_head;
662  break;
663 }
664                 /*
665                  * ignore our own packets...
666                  */
667                 if (!(*(unsigned long *)&dev->dev_addr[0] ^ *(unsigned long *)&addrs[2+6]) &&
668                     !(*(unsigned short *)&dev->dev_addr[4] ^ *(unsigned short *)&addrs[2+10])) {
669                         maxcnt ++; /* compensate for loopedback packet */
670                         ether3_outw(next_ptr >> 8, REG_RECVEND);
671                 } else
672                 if (!(status & (RXSTAT_OVERSIZE|RXSTAT_CRCERROR|RXSTAT_DRIBBLEERROR|RXSTAT_SHORTPACKET))) {
673                         unsigned int length = next_ptr - this_ptr;
674                         struct sk_buff *skb;
675
676                         if (next_ptr <= this_ptr)
677                                 length += RX_END - RX_START;
678
679                         skb = dev_alloc_skb(length + 2);
680                         if (skb) {
681                                 unsigned char *buf;
682
683                                 skb->dev = dev;
684                                 skb_reserve(skb, 2);
685                                 buf = skb_put(skb, length);
686                                 ether3_readbuffer(dev, buf + 12, length - 12);
687                                 ether3_outw(next_ptr >> 8, REG_RECVEND);
688                                 *(unsigned short *)(buf + 0)    = *(unsigned short *)(addrs + 2);
689                                 *(unsigned long *)(buf + 2)     = *(unsigned long *)(addrs + 4);
690                                 *(unsigned long *)(buf + 6)     = *(unsigned long *)(addrs + 8);
691                                 *(unsigned short *)(buf + 10)   = *(unsigned short *)(addrs + 12);
692                                 skb->protocol = eth_type_trans(skb, dev);
693                                 netif_rx(skb);
694                                 received ++;
695                         } else
696                                 goto dropping;
697                 } else {
698                         struct net_device_stats *stats = &priv->stats;
699                         ether3_outw(next_ptr >> 8, REG_RECVEND);
700                         if (status & RXSTAT_OVERSIZE)     stats->rx_over_errors ++;
701                         if (status & RXSTAT_CRCERROR)     stats->rx_crc_errors ++;
702                         if (status & RXSTAT_DRIBBLEERROR) stats->rx_fifo_errors ++;
703                         if (status & RXSTAT_SHORTPACKET)  stats->rx_length_errors ++;
704                         stats->rx_errors++;
705                 }
706         }
707         while (-- maxcnt);
708
709 done:
710         priv->stats.rx_packets += received;
711         priv->rx_head = next_ptr;
712         /*
713          * If rx went off line, then that means that the buffer may be full.  We
714          * have dropped at least one packet.
715          */
716         if (!(ether3_inw(REG_STATUS) & STAT_RXON)) {
717                 priv->stats.rx_dropped ++;
718                 ether3_outw(next_ptr, REG_RECVPTR);
719                 ether3_outw(priv->regs.command | CMD_RXON, REG_COMMAND);
720         }
721
722         return maxcnt;
723
724 dropping:{
725         static unsigned long last_warned;
726
727         ether3_outw(next_ptr >> 8, REG_RECVEND);
728         /*
729          * Don't print this message too many times...
730          */
731         if (time_after(jiffies, last_warned + 10 * HZ)) {
732                 last_warned = jiffies;
733                 printk("%s: memory squeeze, dropping packet.\n", dev->name);
734         }
735         priv->stats.rx_dropped ++;
736         goto done;
737         }
738 }
739
740 /*
741  * Update stats for the transmitted packet(s)
742  */
743 static void
744 ether3_tx(struct net_device *dev, struct dev_priv *priv)
745 {
746         unsigned int tx_tail = priv->tx_tail;
747         int max_work = 14;
748
749         do {
750                 unsigned long status;
751
752                 /*
753                  * Read the packet header
754                  */
755                 ether3_setbuffer(dev, buffer_read, tx_tail * 0x600);
756                 status = ether3_readlong(dev);
757
758                 /*
759                  * Check to see if this packet has been transmitted
760                  */
761                 if ((status & (TXSTAT_DONE | TXHDR_TRANSMIT)) !=
762                     (TXSTAT_DONE | TXHDR_TRANSMIT))
763                         break;
764
765                 /*
766                  * Update errors
767                  */
768                 if (!(status & (TXSTAT_BABBLED | TXSTAT_16COLLISIONS)))
769                         priv->stats.tx_packets++;
770                 else {
771                         priv->stats.tx_errors ++;
772                         if (status & TXSTAT_16COLLISIONS) priv->stats.collisions += 16;
773                         if (status & TXSTAT_BABBLED) priv->stats.tx_fifo_errors ++;
774                 }
775
776                 tx_tail = (tx_tail + 1) & 15;
777         } while (--max_work);
778
779         if (priv->tx_tail != tx_tail) {
780                 priv->tx_tail = tx_tail;
781                 netif_wake_queue(dev);
782         }
783 }
784
785 static void __init ether3_banner(void)
786 {
787         static unsigned version_printed = 0;
788
789         if (net_debug && version_printed++ == 0)
790                 printk(KERN_INFO "%s", version);
791 }
792
793 static const char * __init
794 ether3_get_dev(struct net_device *dev, struct expansion_card *ec)
795 {
796         const char *name = "ether3";
797
798         dev->base_addr = ecard_address(ec, ECARD_MEMC, 0);
799         dev->irq = ec->irq;
800
801         if (ec->cid.manufacturer == MANU_ANT &&
802             ec->cid.product == PROD_ANT_ETHERB) {
803                 dev->base_addr += 0x200;
804                 name = "etherb";
805         }
806
807         ec->irqaddr = (volatile unsigned char *)ioaddr(dev->base_addr);
808         ec->irqmask = 0xf0;
809
810         ether3_addr(dev->dev_addr, ec);
811
812         return name;
813 }
814
815 static int __devinit
816 ether3_probe(struct expansion_card *ec, const struct ecard_id *id)
817 {
818         struct net_device *dev;
819         struct dev_priv *priv;
820         const char *name;
821         int i, bus_type, ret;
822
823         ether3_banner();
824
825         dev = alloc_etherdev(sizeof(struct dev_priv));
826         if (!dev) {
827                 ret = -ENOMEM;
828                 goto out;
829         }
830
831         SET_MODULE_OWNER(dev);
832
833         name = ether3_get_dev(dev, ec);
834         if (!name) {
835                 ret = -ENODEV;
836                 goto free;
837         }
838
839         /*
840          * this will not fail - the nature of the bus ensures this
841          */
842         if (!request_region(dev->base_addr, 128, dev->name)) {
843                 ret = -EBUSY;
844                 goto free;
845         }
846
847         priv = netdev_priv(dev);
848         init_timer(&priv->timer);
849
850         /* Reset card...
851          */
852         ether3_outb(0x80, REG_CONFIG2 + 1);
853         bus_type = BUS_UNKNOWN;
854         udelay(4);
855
856         /* Test using Receive Pointer (16-bit register) to find out
857          * how the ether3 is connected to the bus...
858          */
859         if (ether3_probe_bus_8(dev, 0x100) &&
860             ether3_probe_bus_8(dev, 0x201))
861                 bus_type = BUS_8;
862
863         if (bus_type == BUS_UNKNOWN &&
864             ether3_probe_bus_16(dev, 0x101) &&
865             ether3_probe_bus_16(dev, 0x201))
866                 bus_type = BUS_16;
867
868         switch (bus_type) {
869         case BUS_UNKNOWN:
870                 printk(KERN_ERR "%s: unable to identify bus width\n", dev->name);
871                 ret = -ENODEV;
872                 goto failed;
873
874         case BUS_8:
875                 printk(KERN_ERR "%s: %s found, but is an unsupported "
876                         "8-bit card\n", dev->name, name);
877                 ret = -ENODEV;
878                 goto failed;
879
880         default:
881                 break;
882         }
883
884         if (ether3_init_2(dev)) {
885                 ret = -ENODEV;
886                 goto failed;
887         }
888
889         dev->open               = ether3_open;
890         dev->stop               = ether3_close;
891         dev->hard_start_xmit    = ether3_sendpacket;
892         dev->get_stats          = ether3_getstats;
893         dev->set_multicast_list = ether3_setmulticastlist;
894         dev->tx_timeout         = ether3_timeout;
895         dev->watchdog_timeo     = 5 * HZ / 100;
896
897         ret = register_netdev(dev);
898         if (ret)
899                 goto failed;
900
901         printk("%s: %s in slot %d, ", dev->name, name, ec->slot_no);
902         for (i = 0; i < 6; i++)
903                 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
904
905         ecard_set_drvdata(ec, dev);
906         return 0;
907
908 failed:
909         release_region(dev->base_addr, 128);
910 free:
911         free_netdev(dev);
912 out:
913         return ret;
914 }
915
916 static void __devexit ether3_remove(struct expansion_card *ec)
917 {
918         struct net_device *dev = ecard_get_drvdata(ec);
919
920         ecard_set_drvdata(ec, NULL);
921
922         unregister_netdev(dev);
923         release_region(dev->base_addr, 128);
924         free_netdev(dev);
925 }
926
927 static const struct ecard_id ether3_ids[] = {
928         { MANU_ANT2, PROD_ANT_ETHER3 },
929         { MANU_ANT,  PROD_ANT_ETHER3 },
930         { MANU_ANT,  PROD_ANT_ETHERB },
931         { 0xffff, 0xffff }
932 };
933
934 static struct ecard_driver ether3_driver = {
935         .probe          = ether3_probe,
936         .remove         = __devexit_p(ether3_remove),
937         .id_table       = ether3_ids,
938         .drv = {
939                 .name   = "ether3",
940         },
941 };
942
943 static int __init ether3_init(void)
944 {
945         return ecard_register_driver(&ether3_driver);
946 }
947
948 static void __exit ether3_exit(void)
949 {
950         ecard_remove_driver(&ether3_driver);
951 }
952
953 module_init(ether3_init);
954 module_exit(ether3_exit);
955
956 MODULE_LICENSE("GPL");