Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / drivers / acorn / net / ether3.c
blob11e3824810c12d4e11499b90044210b21ffe1265
1 /*
2 * linux/drivers/net/ether3.c
4 * SEEQ nq8005 ethernet driver for Acorn/ANT Ether3 card
5 * for Acorn machines
7 * By Russell King, with some suggestions from borris@ant.co.uk
9 * Changelog:
10 * 1.04 RMK 29/02/1996 Won't pass packets that are from our ethernet
11 * address up to the higher levels - they're
12 * silently ignored. I/F can now be put into
13 * multicast mode. Receiver routine optimised.
14 * 1.05 RMK 30/02/1996 Now claims interrupt at open when part of
15 * the kernel rather than when a module.
16 * 1.06 RMK 02/03/1996 Various code cleanups
17 * 1.07 RMK 13/10/1996 Optimised interrupt routine and transmit
18 * routines.
19 * 1.08 RMK 14/10/1996 Fixed problem with too many packets,
20 * prevented the kernel message about dropped
21 * packets appearing too many times a second.
22 * Now does not disable all IRQs, only the IRQ
23 * used by this card.
24 * 1.09 RMK 10/11/1996 Only enables TX irq when buffer space is low,
25 * but we still service the TX queue if we get a
26 * RX interrupt.
27 * 1.10 RMK 15/07/1997 Fixed autoprobing of NQ8004.
28 * 1.11 RMK 16/11/1997 Fixed autoprobing of NQ8005A.
29 * 1.12 RMK 31/12/1997 Removed reference to dev_tint for Linux 2.1.
30 * RMK 27/06/1998 Changed asm/delay.h to linux/delay.h.
31 * 1.13 RMK 29/06/1998 Fixed problem with transmission of packets.
32 * Chip seems to have a bug in, whereby if the
33 * packet starts two bytes from the end of the
34 * buffer, it corrupts the receiver chain, and
35 * never updates the transmit status correctly.
36 * 1.14 RMK 07/01/1998 Added initial code for ETHERB addressing.
37 * 1.15 RMK 30/04/1999 More fixes to the transmit routine for buggy
38 * hardware.
39 * 1.16 RMK 10/02/2000 Updated for 2.3.43
40 * 1.17 RMK 13/05/2000 Updated for 2.3.99-pre8
43 static char *version = "ether3 ethernet driver (c) 1995-2000 R.M.King v1.17\n";
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/sched.h>
48 #include <linux/types.h>
49 #include <linux/fcntl.h>
50 #include <linux/interrupt.h>
51 #include <linux/ptrace.h>
52 #include <linux/ioport.h>
53 #include <linux/in.h>
54 #include <linux/malloc.h>
55 #include <linux/string.h>
56 #include <linux/errno.h>
57 #include <linux/netdevice.h>
58 #include <linux/etherdevice.h>
59 #include <linux/skbuff.h>
60 #include <linux/init.h>
61 #include <linux/delay.h>
63 #include <asm/system.h>
64 #include <asm/bitops.h>
65 #include <asm/ecard.h>
66 #include <asm/io.h>
67 #include <asm/irq.h>
69 #include "ether3.h"
71 static unsigned int net_debug = NET_DEBUG;
72 static const card_ids __init ether3_cids[] = {
73 { MANU_ANT2, PROD_ANT_ETHER3 },
74 { MANU_ANT, PROD_ANT_ETHER3 },
75 { MANU_ANT, PROD_ANT_ETHERB }, /* trial - will etherb work? */
76 { 0xffff, 0xffff }
79 static void ether3_setmulticastlist(struct net_device *dev);
80 static int ether3_rx(struct net_device *dev, struct dev_priv *priv, unsigned int maxcnt);
81 static void ether3_tx(struct net_device *dev, struct dev_priv *priv);
82 static int ether3_open (struct net_device *dev);
83 static int ether3_sendpacket (struct sk_buff *skb, struct net_device *dev);
84 static void ether3_interrupt (int irq, void *dev_id, struct pt_regs *regs);
85 static int ether3_close (struct net_device *dev);
86 static struct enet_statistics *ether3_getstats (struct net_device *dev);
87 static void ether3_setmulticastlist (struct net_device *dev);
88 static void ether3_timeout(struct net_device *dev);
90 #define BUS_16 2
91 #define BUS_8 1
92 #define BUS_UNKNOWN 0
95 * I'm not sure what address we should default to if the internal one
96 * is corrupted...
98 unsigned char def_eth_addr[6] = {0x00, 'L', 'i', 'n', 'u', 'x'};
100 /* --------------------------------------------------------------------------- */
102 typedef enum {
103 buffer_write,
104 buffer_read
105 } buffer_rw_t;
108 * ether3 read/write. Slow things down a bit...
109 * The SEEQ8005 doesn't like us writing to it's registers
110 * too quickly.
112 static inline void ether3_outb(int v, const int r)
114 outb(v, r);
115 udelay(1);
118 static inline void ether3_outw(int v, const int r)
120 outw(v, r);
121 udelay(1);
123 #define ether3_inb(r) ({ unsigned int __v = inb((r)); udelay(1); __v; })
124 #define ether3_inw(r) ({ unsigned int __v = inw((r)); udelay(1); __v; })
126 static int
127 ether3_setbuffer(struct net_device *dev, buffer_rw_t read, int start)
129 struct dev_priv *priv = (struct dev_priv *)dev->priv;
130 int timeout = 1000;
132 ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
133 ether3_outw(priv->regs.command | CMD_FIFOWRITE, REG_COMMAND);
135 while ((ether3_inw(REG_STATUS) & STAT_FIFOEMPTY) == 0) {
136 if (!timeout--) {
137 printk("%s: setbuffer broken\n", dev->name);
138 priv->broken = 1;
139 return 1;
141 udelay(1);
144 if (read == buffer_read) {
145 ether3_outw(start, REG_DMAADDR);
146 ether3_outw(priv->regs.command | CMD_FIFOREAD, REG_COMMAND);
147 } else {
148 ether3_outw(priv->regs.command | CMD_FIFOWRITE, REG_COMMAND);
149 ether3_outw(start, REG_DMAADDR);
151 return 0;
155 * write data to the buffer memory
157 #define ether3_writebuffer(dev,data,length) \
158 outsw(REG_BUFWIN, (data), (length) >> 1)
160 #define ether3_writeword(dev,data) \
161 outw((data), REG_BUFWIN)
163 #define ether3_writelong(dev,data) { \
164 unsigned long reg_bufwin = REG_BUFWIN; \
165 outw((data), reg_bufwin); \
166 outw((data) >> 16, reg_bufwin); \
170 * read data from the buffer memory
172 #define ether3_readbuffer(dev,data,length) \
173 insw(REG_BUFWIN, (data), (length) >> 1)
175 #define ether3_readword(dev) \
176 inw(REG_BUFWIN)
178 #define ether3_readlong(dev) \
179 inw(REG_BUFWIN) | (inw(REG_BUFWIN) << 16)
182 * Switch LED off...
184 static void
185 ether3_ledoff(unsigned long data)
187 struct net_device *dev = (struct net_device *)data;
188 struct dev_priv *priv = (struct dev_priv *)dev->priv;
189 ether3_outw(priv->regs.config2 |= CFG2_CTRLO, REG_CONFIG2);
193 * switch LED on...
195 static inline void
196 ether3_ledon(struct net_device *dev, struct dev_priv *priv)
198 del_timer(&priv->timer);
199 priv->timer.expires = jiffies + HZ / 50; /* leave on for 1/50th second */
200 priv->timer.data = (unsigned long)dev;
201 priv->timer.function = ether3_ledoff;
202 add_timer(&priv->timer);
203 if (priv->regs.config2 & CFG2_CTRLO)
204 ether3_outw(priv->regs.config2 &= ~CFG2_CTRLO, REG_CONFIG2);
208 * Read the ethernet address string from the on board rom.
209 * This is an ascii string!!!
211 static void __init
212 ether3_addr(char *addr, struct expansion_card *ec)
214 struct in_chunk_dir cd;
215 char *s;
217 if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
218 int i;
219 for (i = 0; i<6; i++) {
220 addr[i] = simple_strtoul(s + 1, &s, 0x10);
221 if (*s != (i==5?')' : ':' ))
222 break;
224 if (i == 6)
225 return;
227 /* I wonder if we should even let the user continue in this case
228 * - no, it would be better to disable the device
230 printk(KERN_ERR "ether3: Couldn't read a valid MAC address from card.\n");
231 memcpy(addr, def_eth_addr, 6);
234 /* --------------------------------------------------------------------------- */
236 static int __init
237 ether3_ramtest(struct net_device *dev, unsigned char byte)
239 unsigned char *buffer = kmalloc(RX_END, GFP_KERNEL);
240 int i,ret = 0;
241 int max_errors = 4;
242 int bad = -1;
244 if (!buffer)
245 return 1;
247 memset(buffer, byte, RX_END);
248 ether3_setbuffer(dev, buffer_write, 0);
249 ether3_writebuffer(dev, buffer, TX_END);
250 ether3_setbuffer(dev, buffer_write, RX_START);
251 ether3_writebuffer(dev, buffer + RX_START, RX_LEN);
252 memset(buffer, byte ^ 0xff, RX_END);
253 ether3_setbuffer(dev, buffer_read, 0);
254 ether3_readbuffer(dev, buffer, TX_END);
255 ether3_setbuffer(dev, buffer_read, RX_START);
256 ether3_readbuffer(dev, buffer + RX_START, RX_LEN);
258 for (i = 0; i < RX_END; i++) {
259 if (buffer[i] != byte) {
260 if (max_errors > 0 && bad != buffer[i]) {
261 printk("%s: RAM failed with (%02X instead of %02X) at 0x%04X",
262 dev->name, buffer[i], byte, i);
263 ret = 2;
264 max_errors--;
265 bad = i;
267 } else {
268 if (bad != -1) {
269 if (bad != i - 1)
270 printk(" - 0x%04X\n", i - 1);
271 printk("\n");
272 bad = -1;
276 if (bad != -1)
277 printk(" - 0xffff\n");
278 kfree(buffer);
280 return ret;
283 /* ------------------------------------------------------------------------------- */
285 static int __init
286 ether3_init_2(struct net_device *dev)
288 struct dev_priv *priv = (struct dev_priv *)dev->priv;
289 int i;
291 priv->regs.config1 = CFG1_RECVCOMPSTAT0|CFG1_DMABURST8;
292 priv->regs.config2 = CFG2_CTRLO|CFG2_RECVCRC|CFG2_ERRENCRC;
293 priv->regs.command = 0;
296 * Set up our hardware address
298 ether3_outw(priv->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
299 for (i = 0; i < 6; i++)
300 ether3_outb(dev->dev_addr[i], REG_BUFWIN);
302 if (dev->flags & IFF_PROMISC)
303 priv->regs.config1 |= CFG1_RECVPROMISC;
304 else if (dev->flags & IFF_MULTICAST)
305 priv->regs.config1 |= CFG1_RECVSPECBRMULTI;
306 else
307 priv->regs.config1 |= CFG1_RECVSPECBROAD;
310 * There is a problem with the NQ8005 in that it occasionally loses the
311 * last two bytes. To get round this problem, we receive the CRC as
312 * well. That way, if we do loose the last two, then it doesn't matter.
314 ether3_outw(priv->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
315 ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
316 ether3_outw(priv->rx_head, REG_RECVPTR);
317 ether3_outw(0, REG_TRANSMITPTR);
318 ether3_outw(priv->rx_head >> 8, REG_RECVEND);
319 ether3_outw(priv->regs.config2, REG_CONFIG2);
320 ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
321 ether3_outw(priv->regs.command, REG_COMMAND);
323 i = ether3_ramtest(dev, 0x5A);
324 if(i)
325 return i;
326 i = ether3_ramtest(dev, 0x1E);
327 if(i)
328 return i;
330 ether3_setbuffer(dev, buffer_write, 0);
331 ether3_writelong(dev, 0);
332 return 0;
335 static void
336 ether3_init_for_open(struct net_device *dev)
338 struct dev_priv *priv = (struct dev_priv *)dev->priv;
339 int i;
341 memset(&priv->stats, 0, sizeof(struct enet_statistics));
343 /* Reset the chip */
344 ether3_outw(CFG2_RESET, REG_CONFIG2);
345 udelay(4);
347 priv->regs.command = 0;
348 ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
349 while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON));
351 ether3_outw(priv->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
352 for (i = 0; i < 6; i++)
353 ether3_outb(dev->dev_addr[i], REG_BUFWIN);
355 priv->tx_head = 0;
356 priv->tx_tail = 0;
357 priv->regs.config2 |= CFG2_CTRLO;
358 priv->rx_head = RX_START;
360 ether3_outw(priv->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
361 ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
362 ether3_outw(priv->rx_head, REG_RECVPTR);
363 ether3_outw(priv->rx_head >> 8, REG_RECVEND);
364 ether3_outw(0, REG_TRANSMITPTR);
365 ether3_outw(priv->regs.config2, REG_CONFIG2);
366 ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
368 ether3_setbuffer(dev, buffer_write, 0);
369 ether3_writelong(dev, 0);
371 priv->regs.command = CMD_ENINTRX | CMD_ENINTTX;
372 ether3_outw(priv->regs.command | CMD_RXON, REG_COMMAND);
375 static inline int
376 ether3_probe_bus_8(struct net_device *dev, int val)
378 int write_low, write_high, read_low, read_high;
380 write_low = val & 255;
381 write_high = val >> 8;
383 printk(KERN_DEBUG "ether3_probe: write8 [%02X:%02X]", write_high, write_low);
385 ether3_outb(write_low, REG_RECVPTR);
386 ether3_outb(write_high, REG_RECVPTR + 1);
388 read_low = ether3_inb(REG_RECVPTR);
389 read_high = ether3_inb(REG_RECVPTR + 1);
391 printk(", read8 [%02X:%02X]\n", read_high, read_low);
393 return read_low == write_low && read_high == write_high;
396 static inline int
397 ether3_probe_bus_16(struct net_device *dev, int val)
399 int read_val;
401 ether3_outw(val, REG_RECVPTR);
402 read_val = ether3_inw(REG_RECVPTR);
404 printk(KERN_DEBUG "ether3_probe: write16 [%04X], read16 [%04X]\n", val, read_val);
406 return read_val == val;
410 * Open/initialize the board. This is called (in the current kernel)
411 * sometime after booting when the 'ifconfig' program is run.
413 * This routine should set everything up anew at each open, even
414 * registers that "should" only need to be set once at boot, so that
415 * there is non-reboot way to recover if something goes wrong.
417 static int
418 ether3_open(struct net_device *dev)
420 MOD_INC_USE_COUNT;
422 if (request_irq(dev->irq, ether3_interrupt, 0, "ether3", dev)) {
423 MOD_DEC_USE_COUNT;
424 return -EAGAIN;
427 ether3_init_for_open(dev);
429 netif_start_queue(dev);
431 return 0;
435 * The inverse routine to ether3_open().
437 static int
438 ether3_close(struct net_device *dev)
440 struct dev_priv *priv = (struct dev_priv *)dev->priv;
442 netif_stop_queue(dev);
444 disable_irq(dev->irq);
446 ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
447 priv->regs.command = 0;
448 while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON));
449 ether3_outb(0x80, REG_CONFIG2 + 1);
450 ether3_outw(0, REG_COMMAND);
452 free_irq(dev->irq, dev);
454 MOD_DEC_USE_COUNT;
455 return 0;
459 * Get the current statistics. This may be called with the card open or
460 * closed.
462 static struct enet_statistics *ether3_getstats(struct net_device *dev)
464 struct dev_priv *priv = (struct dev_priv *)dev->priv;
465 return &priv->stats;
469 * Set or clear promiscuous/multicast mode filter for this adaptor.
471 * We don't attempt any packet filtering. The card may have a SEEQ 8004
472 * in which does not have the other ethernet address registers present...
474 static void ether3_setmulticastlist(struct net_device *dev)
476 struct dev_priv *priv = (struct dev_priv *)dev->priv;
478 priv->regs.config1 &= ~CFG1_RECVPROMISC;
480 if (dev->flags & IFF_PROMISC) {
481 /* promiscuous mode */
482 priv->regs.config1 |= CFG1_RECVPROMISC;
483 } else if (dev->flags & IFF_ALLMULTI) {
484 priv->regs.config1 |= CFG1_RECVSPECBRMULTI;
485 } else
486 priv->regs.config1 |= CFG1_RECVSPECBROAD;
488 ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
491 static void
492 ether3_timeout(struct net_device *dev)
494 struct dev_priv *priv = (struct dev_priv *)dev->priv;
495 unsigned long flags;
497 del_timer(&priv->timer);
499 save_flags_cli(flags);
500 printk(KERN_ERR "%s: transmit timed out, network cable problem?\n", dev->name);
501 printk(KERN_ERR "%s: state: { status=%04X cfg1=%04X cfg2=%04X }\n", dev->name,
502 ether3_inw(REG_STATUS), ether3_inw(REG_CONFIG1), ether3_inw(REG_CONFIG2));
503 printk(KERN_ERR "%s: { rpr=%04X rea=%04X tpr=%04X }\n", dev->name,
504 ether3_inw(REG_RECVPTR), ether3_inw(REG_RECVEND), ether3_inw(REG_TRANSMITPTR));
505 printk(KERN_ERR "%s: tx head=%X tx tail=%X\n", dev->name,
506 priv->tx_head, priv->tx_tail);
507 ether3_setbuffer(dev, buffer_read, priv->tx_tail);
508 printk(KERN_ERR "%s: packet status = %08X\n", dev->name, ether3_readlong(dev));
509 restore_flags(flags);
511 priv->regs.config2 |= CFG2_CTRLO;
512 priv->stats.tx_errors += 1;
513 ether3_outw(priv->regs.config2, REG_CONFIG2);
514 priv->tx_head = priv->tx_tail = 0;
516 netif_wake_queue(dev);
520 * Transmit a packet
522 static int
523 ether3_sendpacket(struct sk_buff *skb, struct net_device *dev)
525 struct dev_priv *priv = (struct dev_priv *)dev->priv;
526 unsigned long flags;
527 unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
528 unsigned int ptr, next_ptr;
530 length = (length + 1) & ~1;
532 if (priv->broken) {
533 dev_kfree_skb(skb);
534 priv->stats.tx_dropped ++;
535 netif_start_queue(dev);
536 return 0;
539 next_ptr = (priv->tx_head + 1) & 15;
541 save_flags_cli(flags);
543 if (priv->tx_tail == next_ptr) {
544 restore_flags(flags);
545 return 1; /* unable to queue */
548 dev->trans_start = jiffies;
549 ptr = 0x600 * priv->tx_head;
550 priv->tx_head = next_ptr;
551 next_ptr *= 0x600;
553 #define TXHDR_FLAGS (TXHDR_TRANSMIT|TXHDR_CHAINCONTINUE|TXHDR_DATAFOLLOWS|TXHDR_ENSUCCESS)
555 ether3_setbuffer(dev, buffer_write, next_ptr);
556 ether3_writelong(dev, 0);
557 ether3_setbuffer(dev, buffer_write, ptr);
558 ether3_writelong(dev, 0);
559 ether3_writebuffer(dev, skb->data, length);
560 ether3_writeword(dev, htons(next_ptr));
561 ether3_writeword(dev, TXHDR_CHAINCONTINUE >> 16);
562 ether3_setbuffer(dev, buffer_write, ptr);
563 ether3_writeword(dev, htons((ptr + length + 4)));
564 ether3_writeword(dev, TXHDR_FLAGS >> 16);
565 ether3_ledon(dev, priv);
567 if (!(ether3_inw(REG_STATUS) & STAT_TXON)) {
568 ether3_outw(ptr, REG_TRANSMITPTR);
569 ether3_outw(priv->regs.command | CMD_TXON, REG_COMMAND);
572 next_ptr = (priv->tx_head + 1) & 15;
573 restore_flags(flags);
575 dev_kfree_skb(skb);
577 if (priv->tx_tail == next_ptr)
578 netif_stop_queue(dev);
580 return 0;
583 static void
584 ether3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
586 struct net_device *dev = (struct net_device *)dev_id;
587 struct dev_priv *priv;
588 unsigned int status;
590 #if NET_DEBUG > 1
591 if(net_debug & DEBUG_INT)
592 printk("eth3irq: %d ", irq);
593 #endif
595 priv = (struct dev_priv *)dev->priv;
597 status = ether3_inw(REG_STATUS);
599 if (status & STAT_INTRX) {
600 ether3_outw(CMD_ACKINTRX | priv->regs.command, REG_COMMAND);
601 ether3_rx(dev, priv, 12);
604 if (status & STAT_INTTX) {
605 ether3_outw(CMD_ACKINTTX | priv->regs.command, REG_COMMAND);
606 ether3_tx(dev, priv);
609 #if NET_DEBUG > 1
610 if(net_debug & DEBUG_INT)
611 printk("done\n");
612 #endif
616 * If we have a good packet(s), get it/them out of the buffers.
618 static int
619 ether3_rx(struct net_device *dev, struct dev_priv *priv, unsigned int maxcnt)
621 unsigned int next_ptr = priv->rx_head, received = 0;
622 ether3_ledon(dev, priv);
624 do {
625 unsigned int this_ptr, status;
626 unsigned char addrs[16];
629 * read the first 16 bytes from the buffer.
630 * This contains the status bytes etc and ethernet addresses,
631 * and we also check the source ethernet address to see if
632 * it originated from us.
635 unsigned int temp_ptr;
636 ether3_setbuffer(dev, buffer_read, next_ptr);
637 temp_ptr = ether3_readword(dev);
638 status = ether3_readword(dev);
639 if ((status & (RXSTAT_DONE | RXHDR_CHAINCONTINUE | RXHDR_RECEIVE)) !=
640 (RXSTAT_DONE | RXHDR_CHAINCONTINUE) || !temp_ptr)
641 break;
643 this_ptr = next_ptr + 4;
644 next_ptr = ntohs(temp_ptr);
646 ether3_setbuffer(dev, buffer_read, this_ptr);
647 ether3_readbuffer(dev, addrs+2, 12);
649 if (next_ptr < RX_START || next_ptr >= RX_END) {
650 int i;
651 printk("%s: bad next pointer @%04X: ", dev->name, priv->rx_head);
652 printk("%02X %02X %02X %02X ", next_ptr >> 8, next_ptr & 255, status & 255, status >> 8);
653 for (i = 2; i < 14; i++)
654 printk("%02X ", addrs[i]);
655 printk("\n");
656 next_ptr = priv->rx_head;
657 break;
660 * ignore our own packets...
662 if (!(*(unsigned long *)&dev->dev_addr[0] ^ *(unsigned long *)&addrs[2+6]) &&
663 !(*(unsigned short *)&dev->dev_addr[4] ^ *(unsigned short *)&addrs[2+10])) {
664 maxcnt ++; /* compensate for loopedback packet */
665 ether3_outw(next_ptr >> 8, REG_RECVEND);
666 } else
667 if (!(status & (RXSTAT_OVERSIZE|RXSTAT_CRCERROR|RXSTAT_DRIBBLEERROR|RXSTAT_SHORTPACKET))) {
668 unsigned int length = next_ptr - this_ptr;
669 struct sk_buff *skb;
671 if (next_ptr <= this_ptr)
672 length += RX_END - RX_START;
674 skb = dev_alloc_skb(length + 2);
675 if (skb) {
676 unsigned char *buf;
678 skb->dev = dev;
679 skb_reserve(skb, 2);
680 buf = skb_put(skb, length);
681 ether3_readbuffer(dev, buf + 12, length - 12);
682 ether3_outw(next_ptr >> 8, REG_RECVEND);
683 *(unsigned short *)(buf + 0) = *(unsigned short *)(addrs + 2);
684 *(unsigned long *)(buf + 2) = *(unsigned long *)(addrs + 4);
685 *(unsigned long *)(buf + 6) = *(unsigned long *)(addrs + 8);
686 *(unsigned short *)(buf + 10) = *(unsigned short *)(addrs + 12);
687 skb->protocol = eth_type_trans(skb, dev);
688 netif_rx(skb);
689 received ++;
690 } else
691 goto dropping;
692 } else {
693 struct enet_statistics *stats = &priv->stats;
694 ether3_outw(next_ptr >> 8, REG_RECVEND);
695 if (status & RXSTAT_OVERSIZE) stats->rx_over_errors ++;
696 if (status & RXSTAT_CRCERROR) stats->rx_crc_errors ++;
697 if (status & RXSTAT_DRIBBLEERROR) stats->rx_fifo_errors ++;
698 if (status & RXSTAT_SHORTPACKET) stats->rx_length_errors ++;
699 stats->rx_errors++;
702 while (-- maxcnt);
704 done:
705 priv->stats.rx_packets += received;
706 priv->rx_head = next_ptr;
708 * If rx went off line, then that means that the buffer may be full. We
709 * have dropped at least one packet.
711 if (!(ether3_inw(REG_STATUS) & STAT_RXON)) {
712 priv->stats.rx_dropped ++;
713 ether3_outw(next_ptr, REG_RECVPTR);
714 ether3_outw(priv->regs.command | CMD_RXON, REG_COMMAND);
717 return maxcnt;
719 dropping:{
720 static unsigned long last_warned;
722 ether3_outw(next_ptr >> 8, REG_RECVEND);
724 * Don't print this message too many times...
726 if (jiffies - last_warned > 30 * HZ) {
727 last_warned = jiffies;
728 printk("%s: memory squeeze, dropping packet.\n", dev->name);
730 priv->stats.rx_dropped ++;
731 goto done;
736 * Update stats for the transmitted packet(s)
738 static void
739 ether3_tx(struct net_device *dev, struct dev_priv *priv)
741 unsigned int tx_tail = priv->tx_tail;
742 int max_work = 14;
744 do {
745 unsigned long status;
748 * Read the packet header
750 ether3_setbuffer(dev, buffer_read, tx_tail * 0x600);
751 status = ether3_readlong(dev);
754 * Check to see if this packet has been transmitted
756 if ((status & (TXSTAT_DONE | TXHDR_TRANSMIT)) !=
757 (TXSTAT_DONE | TXHDR_TRANSMIT))
758 break;
761 * Update errors
763 if (!(status & (TXSTAT_BABBLED | TXSTAT_16COLLISIONS)))
764 priv->stats.tx_packets++;
765 else {
766 priv->stats.tx_errors ++;
767 if (status & TXSTAT_16COLLISIONS) priv->stats.collisions += 16;
768 if (status & TXSTAT_BABBLED) priv->stats.tx_fifo_errors ++;
771 tx_tail = (tx_tail + 1) & 15;
772 } while (--max_work);
774 if (priv->tx_tail != tx_tail) {
775 priv->tx_tail = tx_tail;
776 netif_wake_queue(dev);
780 static void __init ether3_banner(void)
782 static unsigned version_printed = 0;
784 if (net_debug && version_printed++ == 0)
785 printk(version);
788 static const char * __init
789 ether3_get_dev(struct net_device *dev, struct expansion_card *ec)
791 const char *name = "ether3";
792 dev->base_addr = ecard_address(ec, ECARD_MEMC, 0);
793 dev->irq = ec->irq;
795 if (ec->cid.manufacturer == MANU_ANT &&
796 ec->cid.product == PROD_ANT_ETHERB) {
797 dev->base_addr += 0x200;
798 name = "etherb";
801 ec->irqaddr = (volatile unsigned char *)ioaddr(dev->base_addr);
802 ec->irqmask = 0xf0;
804 ether3_addr(dev->dev_addr, ec);
806 return name;
809 static struct net_device * __init ether3_init_one(struct expansion_card *ec)
811 struct net_device *dev;
812 struct dev_priv *priv;
813 const char *name;
814 int i, bus_type;
816 ether3_banner();
818 ecard_claim(ec);
820 dev = init_etherdev(NULL, sizeof(struct dev_priv));
821 if (!dev)
822 goto out;
824 name = ether3_get_dev(dev, ec);
827 * this will not fail - the nature of the bus ensures this
829 request_region(dev->base_addr, 128, dev->name);
831 priv = (struct dev_priv *) dev->priv;
833 /* Reset card...
835 ether3_outb(0x80, REG_CONFIG2 + 1);
836 bus_type = BUS_UNKNOWN;
837 udelay(4);
839 /* Test using Receive Pointer (16-bit register) to find out
840 * how the ether3 is connected to the bus...
842 if (ether3_probe_bus_8(dev, 0x100) &&
843 ether3_probe_bus_8(dev, 0x201))
844 bus_type = BUS_8;
846 if (bus_type == BUS_UNKNOWN &&
847 ether3_probe_bus_16(dev, 0x101) &&
848 ether3_probe_bus_16(dev, 0x201))
849 bus_type = BUS_16;
851 switch (bus_type) {
852 case BUS_UNKNOWN:
853 printk(KERN_ERR "%s: unable to identify bus width\n", dev->name);
854 goto failed;
856 case BUS_8:
857 printk(KERN_ERR "%s: %s found, but is an unsupported "
858 "8-bit card\n", dev->name, name);
859 goto failed;
861 default:
862 break;
865 printk("%s: %s at %lx, IRQ%d, ether address ",
866 dev->name, name, dev->base_addr, dev->irq);
867 for (i = 0; i < 6; i++)
868 printk(i == 5 ? "%2.2x\n" : "%2.2x:", dev->dev_addr[i]);
870 if (ether3_init_2(dev))
871 goto failed;
873 dev->open = ether3_open;
874 dev->stop = ether3_close;
875 dev->hard_start_xmit = ether3_sendpacket;
876 dev->get_stats = ether3_getstats;
877 dev->set_multicast_list = ether3_setmulticastlist;
878 dev->tx_timeout = ether3_timeout;
879 dev->watchdog_timeo = 5 * HZ / 100;
880 return 0;
882 failed:
883 release_region(dev->base_addr, 128);
884 unregister_netdev(dev);
885 kfree(dev);
886 out:
887 ecard_release(ec);
888 return NULL;
891 static struct expansion_card *e_card[MAX_ECARDS];
892 static struct net_device *e_dev[MAX_ECARDS];
894 static int ether3_init(void)
896 int i, ret = -ENODEV;
898 ecard_startfind();
900 for (i = 0; i < MAX_ECARDS; i++) {
901 struct net_device *dev;
902 struct expansion_card *ec;
904 ec = ecard_find(0, ether3_cids);
905 if (!ec)
906 break;
908 dev = ether3_init_one(ec);
909 if (!dev)
910 break;
912 e_card[i] = ec;
913 e_dev[i] = dev;
914 ret = 0;
917 return ret;
920 static void ether3_exit(void)
922 int i;
924 for (i = 0; i < MAX_ECARDS; i++) {
925 if (e_dev[i]) {
926 unregister_netdev(e_dev[i]);
927 release_region(e_dev[i]->base_addr, 128);
928 kfree(e_dev[i]);
929 e_dev[i] = NULL;
931 if (e_card[i]) {
932 ecard_release(e_card[i]);
933 e_card[i] = NULL;
938 module_init(ether3_init);
939 module_exit(ether3_exit);