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