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