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