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