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