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