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