Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/drivers/acorn/net/ether3.c
0004  *
0005  *  Copyright (C) 1995-2000 Russell King
0006  *
0007  * SEEQ nq8005 ethernet driver for Acorn/ANT Ether3 card
0008  *  for Acorn machines
0009  *
0010  * By Russell King, with some suggestions from borris@ant.co.uk
0011  *
0012  * Changelog:
0013  * 1.04 RMK 29/02/1996  Won't pass packets that are from our ethernet
0014  *              address up to the higher levels - they're
0015  *              silently ignored.  I/F can now be put into
0016  *              multicast mode.  Receiver routine optimised.
0017  * 1.05 RMK 30/02/1996  Now claims interrupt at open when part of
0018  *              the kernel rather than when a module.
0019  * 1.06 RMK 02/03/1996  Various code cleanups
0020  * 1.07 RMK 13/10/1996  Optimised interrupt routine and transmit
0021  *              routines.
0022  * 1.08 RMK 14/10/1996  Fixed problem with too many packets,
0023  *              prevented the kernel message about dropped
0024  *              packets appearing too many times a second.
0025  *              Now does not disable all IRQs, only the IRQ
0026  *              used by this card.
0027  * 1.09 RMK 10/11/1996  Only enables TX irq when buffer space is low,
0028  *              but we still service the TX queue if we get a
0029  *              RX interrupt.
0030  * 1.10 RMK 15/07/1997  Fixed autoprobing of NQ8004.
0031  * 1.11 RMK 16/11/1997  Fixed autoprobing of NQ8005A.
0032  * 1.12 RMK 31/12/1997  Removed reference to dev_tint for Linux 2.1.
0033  *      RMK 27/06/1998  Changed asm/delay.h to linux/delay.h.
0034  * 1.13 RMK 29/06/1998  Fixed problem with transmission of packets.
0035  *              Chip seems to have a bug in, whereby if the
0036  *              packet starts two bytes from the end of the
0037  *              buffer, it corrupts the receiver chain, and
0038  *              never updates the transmit status correctly.
0039  * 1.14 RMK 07/01/1998  Added initial code for ETHERB addressing.
0040  * 1.15 RMK 30/04/1999  More fixes to the transmit routine for buggy
0041  *              hardware.
0042  * 1.16 RMK 10/02/2000  Updated for 2.3.43
0043  * 1.17 RMK 13/05/2000  Updated for 2.3.99-pre8
0044  */
0045 
0046 #include <linux/module.h>
0047 #include <linux/kernel.h>
0048 #include <linux/types.h>
0049 #include <linux/fcntl.h>
0050 #include <linux/interrupt.h>
0051 #include <linux/ioport.h>
0052 #include <linux/in.h>
0053 #include <linux/slab.h>
0054 #include <linux/string.h>
0055 #include <linux/errno.h>
0056 #include <linux/netdevice.h>
0057 #include <linux/etherdevice.h>
0058 #include <linux/skbuff.h>
0059 #include <linux/device.h>
0060 #include <linux/init.h>
0061 #include <linux/delay.h>
0062 #include <linux/bitops.h>
0063 
0064 #include <asm/ecard.h>
0065 #include <asm/io.h>
0066 
0067 static char version[] = "ether3 ethernet driver (c) 1995-2000 R.M.King v1.17\n";
0068 
0069 #include "ether3.h"
0070 
0071 static unsigned int net_debug = NET_DEBUG;
0072 
0073 static void ether3_setmulticastlist(struct net_device *dev);
0074 static int  ether3_rx(struct net_device *dev, unsigned int maxcnt);
0075 static void ether3_tx(struct net_device *dev);
0076 static int  ether3_open (struct net_device *dev);
0077 static netdev_tx_t  ether3_sendpacket(struct sk_buff *skb,
0078                       struct net_device *dev);
0079 static irqreturn_t ether3_interrupt (int irq, void *dev_id);
0080 static int  ether3_close (struct net_device *dev);
0081 static void ether3_setmulticastlist (struct net_device *dev);
0082 static void ether3_timeout(struct net_device *dev, unsigned int txqueue);
0083 
0084 #define BUS_16      2
0085 #define BUS_8       1
0086 #define BUS_UNKNOWN 0
0087 
0088 /* --------------------------------------------------------------------------- */
0089 
0090 typedef enum {
0091     buffer_write,
0092     buffer_read
0093 } buffer_rw_t;
0094 
0095 /*
0096  * ether3 read/write.  Slow things down a bit...
0097  * The SEEQ8005 doesn't like us writing to its registers
0098  * too quickly.
0099  */
0100 static inline void ether3_outb(int v, void __iomem *r)
0101 {
0102     writeb(v, r);
0103     udelay(1);
0104 }
0105 
0106 static inline void ether3_outw(int v, void __iomem *r)
0107 {
0108     writew(v, r);
0109     udelay(1);
0110 }
0111 #define ether3_inb(r)       ({ unsigned int __v = readb((r)); udelay(1); __v; })
0112 #define ether3_inw(r)       ({ unsigned int __v = readw((r)); udelay(1); __v; })
0113 
0114 static int
0115 ether3_setbuffer(struct net_device *dev, buffer_rw_t read, int start)
0116 {
0117     int timeout = 1000;
0118 
0119     ether3_outw(priv(dev)->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
0120     ether3_outw(priv(dev)->regs.command | CMD_FIFOWRITE, REG_COMMAND);
0121 
0122     while ((ether3_inw(REG_STATUS) & STAT_FIFOEMPTY) == 0) {
0123         if (!timeout--) {
0124             printk("%s: setbuffer broken\n", dev->name);
0125             priv(dev)->broken = 1;
0126             return 1;
0127         }
0128         udelay(1);
0129     }
0130 
0131     if (read == buffer_read) {
0132         ether3_outw(start, REG_DMAADDR);
0133         ether3_outw(priv(dev)->regs.command | CMD_FIFOREAD, REG_COMMAND);
0134     } else {
0135         ether3_outw(priv(dev)->regs.command | CMD_FIFOWRITE, REG_COMMAND);
0136         ether3_outw(start, REG_DMAADDR);
0137     }
0138     return 0;
0139 }
0140 
0141 /*
0142  * write data to the buffer memory
0143  */
0144 #define ether3_writebuffer(dev,data,length)         \
0145     writesw(REG_BUFWIN, (data), (length) >> 1)
0146 
0147 #define ether3_writeword(dev,data)              \
0148     writew((data), REG_BUFWIN)
0149 
0150 #define ether3_writelong(dev,data)  {           \
0151     void __iomem *reg_bufwin = REG_BUFWIN;          \
0152     writew((data), reg_bufwin);             \
0153     writew((data) >> 16, reg_bufwin);           \
0154 }
0155 
0156 /*
0157  * read data from the buffer memory
0158  */
0159 #define ether3_readbuffer(dev,data,length)          \
0160     readsw(REG_BUFWIN, (data), (length) >> 1)
0161 
0162 #define ether3_readword(dev)                    \
0163     readw(REG_BUFWIN)
0164 
0165 #define ether3_readlong(dev)                    \
0166     readw(REG_BUFWIN) | (readw(REG_BUFWIN) << 16)
0167 
0168 /*
0169  * Switch LED off...
0170  */
0171 static void ether3_ledoff(struct timer_list *t)
0172 {
0173     struct dev_priv *private = from_timer(private, t, timer);
0174     struct net_device *dev = private->dev;
0175 
0176     ether3_outw(priv(dev)->regs.config2 |= CFG2_CTRLO, REG_CONFIG2);
0177 }
0178 
0179 /*
0180  * switch LED on...
0181  */
0182 static inline void ether3_ledon(struct net_device *dev)
0183 {
0184     del_timer(&priv(dev)->timer);
0185     priv(dev)->timer.expires = jiffies + HZ / 50; /* leave on for 1/50th second */
0186     add_timer(&priv(dev)->timer);
0187     if (priv(dev)->regs.config2 & CFG2_CTRLO)
0188         ether3_outw(priv(dev)->regs.config2 &= ~CFG2_CTRLO, REG_CONFIG2);
0189 }
0190 
0191 /*
0192  * Read the ethernet address string from the on board rom.
0193  * This is an ascii string!!!
0194  */
0195 static int
0196 ether3_addr(char *addr, struct expansion_card *ec)
0197 {
0198     struct in_chunk_dir cd;
0199     char *s;
0200     
0201     if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
0202         int i;
0203         for (i = 0; i<6; i++) {
0204             addr[i] = simple_strtoul(s + 1, &s, 0x10);
0205             if (*s != (i==5?')' : ':' ))
0206                 break;
0207         }
0208         if (i == 6)
0209             return 0;
0210     }
0211     /* I wonder if we should even let the user continue in this case
0212      *   - no, it would be better to disable the device
0213      */
0214     printk(KERN_ERR "ether3: Couldn't read a valid MAC address from card.\n");
0215     return -ENODEV;
0216 }
0217 
0218 /* --------------------------------------------------------------------------- */
0219 
0220 static int
0221 ether3_ramtest(struct net_device *dev, unsigned char byte)
0222 {
0223     unsigned char *buffer = kmalloc(RX_END, GFP_KERNEL);
0224     int i,ret = 0;
0225     int max_errors = 4;
0226     int bad = -1;
0227 
0228     if (!buffer)
0229         return 1;
0230 
0231     memset(buffer, byte, RX_END);
0232     ether3_setbuffer(dev, buffer_write, 0);
0233     ether3_writebuffer(dev, buffer, TX_END);
0234     ether3_setbuffer(dev, buffer_write, RX_START);
0235     ether3_writebuffer(dev, buffer + RX_START, RX_LEN);
0236     memset(buffer, byte ^ 0xff, RX_END);
0237     ether3_setbuffer(dev, buffer_read, 0);
0238     ether3_readbuffer(dev, buffer, TX_END);
0239     ether3_setbuffer(dev, buffer_read, RX_START);
0240     ether3_readbuffer(dev, buffer + RX_START, RX_LEN);
0241 
0242     for (i = 0; i < RX_END; i++) {
0243         if (buffer[i] != byte) {
0244             if (max_errors > 0 && bad != buffer[i]) {
0245                 printk("%s: RAM failed with (%02X instead of %02X) at 0x%04X",
0246                        dev->name, buffer[i], byte, i);
0247                 ret = 2;
0248                 max_errors--;
0249                 bad = i;
0250             }
0251         } else {
0252             if (bad != -1) {
0253                 if (bad != i - 1)
0254                     printk(" - 0x%04X\n", i - 1);
0255                 printk("\n");
0256                 bad = -1;
0257             }
0258         }
0259     }
0260     if (bad != -1)
0261         printk(" - 0xffff\n");
0262     kfree(buffer);
0263 
0264     return ret;
0265 }
0266 
0267 /* ------------------------------------------------------------------------------- */
0268 
0269 static int ether3_init_2(struct net_device *dev)
0270 {
0271     int i;
0272 
0273     priv(dev)->regs.config1 = CFG1_RECVCOMPSTAT0|CFG1_DMABURST8;
0274     priv(dev)->regs.config2 = CFG2_CTRLO|CFG2_RECVCRC|CFG2_ERRENCRC;
0275     priv(dev)->regs.command = 0;
0276 
0277     /*
0278      * Set up our hardware address
0279      */
0280     ether3_outw(priv(dev)->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
0281     for (i = 0; i < 6; i++)
0282         ether3_outb(dev->dev_addr[i], REG_BUFWIN);
0283 
0284     if (dev->flags & IFF_PROMISC)
0285         priv(dev)->regs.config1 |= CFG1_RECVPROMISC;
0286     else if (dev->flags & IFF_MULTICAST)
0287         priv(dev)->regs.config1 |= CFG1_RECVSPECBRMULTI;
0288     else
0289         priv(dev)->regs.config1 |= CFG1_RECVSPECBROAD;
0290 
0291     /*
0292      * There is a problem with the NQ8005 in that it occasionally loses the
0293      * last two bytes.  To get round this problem, we receive the CRC as
0294      * well.  That way, if we do lose the last two, then it doesn't matter.
0295      */
0296     ether3_outw(priv(dev)->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
0297     ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
0298     ether3_outw(priv(dev)->rx_head, REG_RECVPTR);
0299     ether3_outw(0, REG_TRANSMITPTR);
0300     ether3_outw(priv(dev)->rx_head >> 8, REG_RECVEND);
0301     ether3_outw(priv(dev)->regs.config2, REG_CONFIG2);
0302     ether3_outw(priv(dev)->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
0303     ether3_outw(priv(dev)->regs.command, REG_COMMAND);
0304 
0305     i = ether3_ramtest(dev, 0x5A);
0306     if(i)
0307         return i;
0308     i = ether3_ramtest(dev, 0x1E);
0309     if(i)
0310         return i;
0311 
0312     ether3_setbuffer(dev, buffer_write, 0);
0313     ether3_writelong(dev, 0);
0314     return 0;
0315 }
0316 
0317 static void
0318 ether3_init_for_open(struct net_device *dev)
0319 {
0320     int i;
0321 
0322     /* Reset the chip */
0323     ether3_outw(CFG2_RESET, REG_CONFIG2);
0324     udelay(4);
0325 
0326     priv(dev)->regs.command = 0;
0327     ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
0328     while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON))
0329         barrier();
0330 
0331     ether3_outw(priv(dev)->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
0332     for (i = 0; i < 6; i++)
0333         ether3_outb(dev->dev_addr[i], REG_BUFWIN);
0334 
0335     priv(dev)->tx_head  = 0;
0336     priv(dev)->tx_tail  = 0;
0337     priv(dev)->regs.config2 |= CFG2_CTRLO;
0338     priv(dev)->rx_head  = RX_START;
0339 
0340     ether3_outw(priv(dev)->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
0341     ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
0342     ether3_outw(priv(dev)->rx_head, REG_RECVPTR);
0343     ether3_outw(priv(dev)->rx_head >> 8, REG_RECVEND);
0344     ether3_outw(0, REG_TRANSMITPTR);
0345     ether3_outw(priv(dev)->regs.config2, REG_CONFIG2);
0346     ether3_outw(priv(dev)->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
0347 
0348     ether3_setbuffer(dev, buffer_write, 0);
0349     ether3_writelong(dev, 0);
0350 
0351     priv(dev)->regs.command = CMD_ENINTRX | CMD_ENINTTX;
0352     ether3_outw(priv(dev)->regs.command | CMD_RXON, REG_COMMAND);
0353 }
0354 
0355 static inline int
0356 ether3_probe_bus_8(struct net_device *dev, int val)
0357 {
0358     int write_low, write_high, read_low, read_high;
0359 
0360     write_low = val & 255;
0361     write_high = val >> 8;
0362 
0363     printk(KERN_DEBUG "ether3_probe: write8 [%02X:%02X]", write_high, write_low);
0364 
0365     ether3_outb(write_low, REG_RECVPTR);
0366     ether3_outb(write_high, REG_RECVPTR + 4);
0367 
0368     read_low = ether3_inb(REG_RECVPTR);
0369     read_high = ether3_inb(REG_RECVPTR + 4);
0370 
0371     printk(", read8 [%02X:%02X]\n", read_high, read_low);
0372 
0373     return read_low == write_low && read_high == write_high;
0374 }
0375 
0376 static inline int
0377 ether3_probe_bus_16(struct net_device *dev, int val)
0378 {
0379     int read_val;
0380 
0381     ether3_outw(val, REG_RECVPTR);
0382     read_val = ether3_inw(REG_RECVPTR);
0383 
0384     printk(KERN_DEBUG "ether3_probe: write16 [%04X], read16 [%04X]\n", val, read_val);
0385 
0386     return read_val == val;
0387 }
0388 
0389 /*
0390  * Open/initialize the board.  This is called (in the current kernel)
0391  * sometime after booting when the 'ifconfig' program is run.
0392  *
0393  * This routine should set everything up anew at each open, even
0394  * registers that "should" only need to be set once at boot, so that
0395  * there is non-reboot way to recover if something goes wrong.
0396  */
0397 static int
0398 ether3_open(struct net_device *dev)
0399 {
0400     if (request_irq(dev->irq, ether3_interrupt, 0, "ether3", dev))
0401         return -EAGAIN;
0402 
0403     ether3_init_for_open(dev);
0404 
0405     netif_start_queue(dev);
0406 
0407     return 0;
0408 }
0409 
0410 /*
0411  * The inverse routine to ether3_open().
0412  */
0413 static int
0414 ether3_close(struct net_device *dev)
0415 {
0416     netif_stop_queue(dev);
0417 
0418     disable_irq(dev->irq);
0419 
0420     ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
0421     priv(dev)->regs.command = 0;
0422     while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON))
0423         barrier();
0424     ether3_outb(0x80, REG_CONFIG2 + 4);
0425     ether3_outw(0, REG_COMMAND);
0426 
0427     free_irq(dev->irq, dev);
0428 
0429     return 0;
0430 }
0431 
0432 /*
0433  * Set or clear promiscuous/multicast mode filter for this adaptor.
0434  *
0435  * We don't attempt any packet filtering.  The card may have a SEEQ 8004
0436  * in which does not have the other ethernet address registers present...
0437  */
0438 static void ether3_setmulticastlist(struct net_device *dev)
0439 {
0440     priv(dev)->regs.config1 &= ~CFG1_RECVPROMISC;
0441 
0442     if (dev->flags & IFF_PROMISC) {
0443         /* promiscuous mode */
0444         priv(dev)->regs.config1 |= CFG1_RECVPROMISC;
0445     } else if (dev->flags & IFF_ALLMULTI || !netdev_mc_empty(dev)) {
0446         priv(dev)->regs.config1 |= CFG1_RECVSPECBRMULTI;
0447     } else
0448         priv(dev)->regs.config1 |= CFG1_RECVSPECBROAD;
0449 
0450     ether3_outw(priv(dev)->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
0451 }
0452 
0453 static void ether3_timeout(struct net_device *dev, unsigned int txqueue)
0454 {
0455     unsigned long flags;
0456 
0457     del_timer(&priv(dev)->timer);
0458 
0459     local_irq_save(flags);
0460     printk(KERN_ERR "%s: transmit timed out, network cable problem?\n", dev->name);
0461     printk(KERN_ERR "%s: state: { status=%04X cfg1=%04X cfg2=%04X }\n", dev->name,
0462         ether3_inw(REG_STATUS), ether3_inw(REG_CONFIG1), ether3_inw(REG_CONFIG2));
0463     printk(KERN_ERR "%s: { rpr=%04X rea=%04X tpr=%04X }\n", dev->name,
0464         ether3_inw(REG_RECVPTR), ether3_inw(REG_RECVEND), ether3_inw(REG_TRANSMITPTR));
0465     printk(KERN_ERR "%s: tx head=%X tx tail=%X\n", dev->name,
0466         priv(dev)->tx_head, priv(dev)->tx_tail);
0467     ether3_setbuffer(dev, buffer_read, priv(dev)->tx_tail);
0468     printk(KERN_ERR "%s: packet status = %08X\n", dev->name, ether3_readlong(dev));
0469     local_irq_restore(flags);
0470 
0471     priv(dev)->regs.config2 |= CFG2_CTRLO;
0472     dev->stats.tx_errors += 1;
0473     ether3_outw(priv(dev)->regs.config2, REG_CONFIG2);
0474     priv(dev)->tx_head = priv(dev)->tx_tail = 0;
0475 
0476     netif_wake_queue(dev);
0477 }
0478 
0479 /*
0480  * Transmit a packet
0481  */
0482 static netdev_tx_t
0483 ether3_sendpacket(struct sk_buff *skb, struct net_device *dev)
0484 {
0485     unsigned long flags;
0486     unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
0487     unsigned int ptr, next_ptr;
0488 
0489     if (priv(dev)->broken) {
0490         dev_kfree_skb(skb);
0491         dev->stats.tx_dropped++;
0492         netif_start_queue(dev);
0493         return NETDEV_TX_OK;
0494     }
0495 
0496     length = (length + 1) & ~1;
0497     if (length != skb->len) {
0498         if (skb_padto(skb, length))
0499             goto out;
0500     }
0501 
0502     next_ptr = (priv(dev)->tx_head + 1) & 15;
0503 
0504     local_irq_save(flags);
0505 
0506     if (priv(dev)->tx_tail == next_ptr) {
0507         local_irq_restore(flags);
0508         return NETDEV_TX_BUSY;  /* unable to queue */
0509     }
0510 
0511     ptr      = 0x600 * priv(dev)->tx_head;
0512     priv(dev)->tx_head = next_ptr;
0513     next_ptr    *= 0x600;
0514 
0515 #define TXHDR_FLAGS (TXHDR_TRANSMIT|TXHDR_CHAINCONTINUE|TXHDR_DATAFOLLOWS|TXHDR_ENSUCCESS)
0516 
0517     ether3_setbuffer(dev, buffer_write, next_ptr);
0518     ether3_writelong(dev, 0);
0519     ether3_setbuffer(dev, buffer_write, ptr);
0520     ether3_writelong(dev, 0);
0521     ether3_writebuffer(dev, skb->data, length);
0522     ether3_writeword(dev, htons(next_ptr));
0523     ether3_writeword(dev, TXHDR_CHAINCONTINUE >> 16);
0524     ether3_setbuffer(dev, buffer_write, ptr);
0525     ether3_writeword(dev, htons((ptr + length + 4)));
0526     ether3_writeword(dev, TXHDR_FLAGS >> 16);
0527     ether3_ledon(dev);
0528 
0529     if (!(ether3_inw(REG_STATUS) & STAT_TXON)) {
0530         ether3_outw(ptr, REG_TRANSMITPTR);
0531         ether3_outw(priv(dev)->regs.command | CMD_TXON, REG_COMMAND);
0532     }
0533 
0534     next_ptr = (priv(dev)->tx_head + 1) & 15;
0535     local_irq_restore(flags);
0536 
0537     dev_kfree_skb(skb);
0538 
0539     if (priv(dev)->tx_tail == next_ptr)
0540         netif_stop_queue(dev);
0541 
0542  out:
0543     return NETDEV_TX_OK;
0544 }
0545 
0546 static irqreturn_t
0547 ether3_interrupt(int irq, void *dev_id)
0548 {
0549     struct net_device *dev = (struct net_device *)dev_id;
0550     unsigned int status, handled = IRQ_NONE;
0551 
0552 #if NET_DEBUG > 1
0553     if(net_debug & DEBUG_INT)
0554         printk("eth3irq: %d ", irq);
0555 #endif
0556 
0557     status = ether3_inw(REG_STATUS);
0558 
0559     if (status & STAT_INTRX) {
0560         ether3_outw(CMD_ACKINTRX | priv(dev)->regs.command, REG_COMMAND);
0561         ether3_rx(dev, 12);
0562         handled = IRQ_HANDLED;
0563     }
0564 
0565     if (status & STAT_INTTX) {
0566         ether3_outw(CMD_ACKINTTX | priv(dev)->regs.command, REG_COMMAND);
0567         ether3_tx(dev);
0568         handled = IRQ_HANDLED;
0569     }
0570 
0571 #if NET_DEBUG > 1
0572     if(net_debug & DEBUG_INT)
0573         printk("done\n");
0574 #endif
0575     return handled;
0576 }
0577 
0578 /*
0579  * If we have a good packet(s), get it/them out of the buffers.
0580  */
0581 static int ether3_rx(struct net_device *dev, unsigned int maxcnt)
0582 {
0583     unsigned int next_ptr = priv(dev)->rx_head, received = 0;
0584 
0585     ether3_ledon(dev);
0586 
0587     do {
0588         unsigned int this_ptr, status;
0589         unsigned char addrs[16];
0590 
0591         /*
0592          * read the first 16 bytes from the buffer.
0593          * This contains the status bytes etc and ethernet addresses,
0594          * and we also check the source ethernet address to see if
0595          * it originated from us.
0596          */
0597         {
0598             unsigned int temp_ptr;
0599             ether3_setbuffer(dev, buffer_read, next_ptr);
0600             temp_ptr = ether3_readword(dev);
0601             status = ether3_readword(dev);
0602             if ((status & (RXSTAT_DONE | RXHDR_CHAINCONTINUE | RXHDR_RECEIVE)) !=
0603                 (RXSTAT_DONE | RXHDR_CHAINCONTINUE) || !temp_ptr)
0604                 break;
0605 
0606             this_ptr = next_ptr + 4;
0607             next_ptr = ntohs(temp_ptr);
0608         }
0609         ether3_setbuffer(dev, buffer_read, this_ptr);
0610         ether3_readbuffer(dev, addrs+2, 12);
0611 
0612 if (next_ptr < RX_START || next_ptr >= RX_END) {
0613  printk("%s: bad next pointer @%04X: ", dev->name, priv(dev)->rx_head);
0614  printk("%02X %02X %02X %02X ", next_ptr >> 8, next_ptr & 255, status & 255, status >> 8);
0615  printk("%pM %pM\n", addrs + 2, addrs + 8);
0616  next_ptr = priv(dev)->rx_head;
0617  break;
0618 }
0619         /*
0620          * ignore our own packets...
0621          */
0622         if (!(*(unsigned long *)&dev->dev_addr[0] ^ *(unsigned long *)&addrs[2+6]) &&
0623             !(*(unsigned short *)&dev->dev_addr[4] ^ *(unsigned short *)&addrs[2+10])) {
0624             maxcnt ++; /* compensate for loopedback packet */
0625             ether3_outw(next_ptr >> 8, REG_RECVEND);
0626         } else
0627         if (!(status & (RXSTAT_OVERSIZE|RXSTAT_CRCERROR|RXSTAT_DRIBBLEERROR|RXSTAT_SHORTPACKET))) {
0628             unsigned int length = next_ptr - this_ptr;
0629             struct sk_buff *skb;
0630 
0631             if (next_ptr <= this_ptr)
0632                 length += RX_END - RX_START;
0633 
0634             skb = netdev_alloc_skb(dev, length + 2);
0635             if (skb) {
0636                 unsigned char *buf;
0637 
0638                 skb_reserve(skb, 2);
0639                 buf = skb_put(skb, length);
0640                 ether3_readbuffer(dev, buf + 12, length - 12);
0641                 ether3_outw(next_ptr >> 8, REG_RECVEND);
0642                 *(unsigned short *)(buf + 0)    = *(unsigned short *)(addrs + 2);
0643                 *(unsigned long *)(buf + 2) = *(unsigned long *)(addrs + 4);
0644                 *(unsigned long *)(buf + 6) = *(unsigned long *)(addrs + 8);
0645                 *(unsigned short *)(buf + 10)   = *(unsigned short *)(addrs + 12);
0646                 skb->protocol = eth_type_trans(skb, dev);
0647                 netif_rx(skb);
0648                 received ++;
0649             } else {
0650                 ether3_outw(next_ptr >> 8, REG_RECVEND);
0651                 dev->stats.rx_dropped++;
0652                 goto done;
0653             }
0654         } else {
0655             struct net_device_stats *stats = &dev->stats;
0656             ether3_outw(next_ptr >> 8, REG_RECVEND);
0657             if (status & RXSTAT_OVERSIZE)     stats->rx_over_errors ++;
0658             if (status & RXSTAT_CRCERROR)     stats->rx_crc_errors ++;
0659             if (status & RXSTAT_DRIBBLEERROR) stats->rx_fifo_errors ++;
0660             if (status & RXSTAT_SHORTPACKET)  stats->rx_length_errors ++;
0661             stats->rx_errors++;
0662         }
0663     }
0664     while (-- maxcnt);
0665 
0666 done:
0667     dev->stats.rx_packets += received;
0668     priv(dev)->rx_head = next_ptr;
0669     /*
0670      * If rx went off line, then that means that the buffer may be full.  We
0671      * have dropped at least one packet.
0672      */
0673     if (!(ether3_inw(REG_STATUS) & STAT_RXON)) {
0674         dev->stats.rx_dropped++;
0675         ether3_outw(next_ptr, REG_RECVPTR);
0676         ether3_outw(priv(dev)->regs.command | CMD_RXON, REG_COMMAND);
0677     }
0678 
0679     return maxcnt;
0680 }
0681 
0682 /*
0683  * Update stats for the transmitted packet(s)
0684  */
0685 static void ether3_tx(struct net_device *dev)
0686 {
0687     unsigned int tx_tail = priv(dev)->tx_tail;
0688     int max_work = 14;
0689 
0690     do {
0691             unsigned long status;
0692 
0693         /*
0694              * Read the packet header
0695          */
0696             ether3_setbuffer(dev, buffer_read, tx_tail * 0x600);
0697         status = ether3_readlong(dev);
0698 
0699         /*
0700          * Check to see if this packet has been transmitted
0701          */
0702         if ((status & (TXSTAT_DONE | TXHDR_TRANSMIT)) !=
0703             (TXSTAT_DONE | TXHDR_TRANSMIT))
0704             break;
0705 
0706         /*
0707          * Update errors
0708          */
0709         if (!(status & (TXSTAT_BABBLED | TXSTAT_16COLLISIONS)))
0710             dev->stats.tx_packets++;
0711         else {
0712             dev->stats.tx_errors++;
0713             if (status & TXSTAT_16COLLISIONS)
0714                 dev->stats.collisions += 16;
0715             if (status & TXSTAT_BABBLED)
0716                 dev->stats.tx_fifo_errors++;
0717         }
0718 
0719         tx_tail = (tx_tail + 1) & 15;
0720     } while (--max_work);
0721 
0722     if (priv(dev)->tx_tail != tx_tail) {
0723         priv(dev)->tx_tail = tx_tail;
0724         netif_wake_queue(dev);
0725     }
0726 }
0727 
0728 static void ether3_banner(void)
0729 {
0730     static unsigned version_printed = 0;
0731 
0732     if (net_debug && version_printed++ == 0)
0733         printk(KERN_INFO "%s", version);
0734 }
0735 
0736 static const struct net_device_ops ether3_netdev_ops = {
0737     .ndo_open       = ether3_open,
0738     .ndo_stop       = ether3_close,
0739     .ndo_start_xmit     = ether3_sendpacket,
0740     .ndo_set_rx_mode    = ether3_setmulticastlist,
0741     .ndo_tx_timeout     = ether3_timeout,
0742     .ndo_validate_addr  = eth_validate_addr,
0743     .ndo_set_mac_address    = eth_mac_addr,
0744 };
0745 
0746 static int
0747 ether3_probe(struct expansion_card *ec, const struct ecard_id *id)
0748 {
0749     const struct ether3_data *data = id->data;
0750     struct net_device *dev;
0751     int bus_type, ret;
0752     u8 addr[ETH_ALEN];
0753 
0754     ether3_banner();
0755 
0756     ret = ecard_request_resources(ec);
0757     if (ret)
0758         goto out;
0759 
0760     dev = alloc_etherdev(sizeof(struct dev_priv));
0761     if (!dev) {
0762         ret = -ENOMEM;
0763         goto release;
0764     }
0765 
0766     SET_NETDEV_DEV(dev, &ec->dev);
0767 
0768     priv(dev)->base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
0769     if (!priv(dev)->base) {
0770         ret = -ENOMEM;
0771         goto free;
0772     }
0773 
0774     ec->irqaddr = priv(dev)->base + data->base_offset;
0775     ec->irqmask = 0xf0;
0776 
0777     priv(dev)->seeq = priv(dev)->base + data->base_offset;
0778     dev->irq = ec->irq;
0779 
0780     ether3_addr(addr, ec);
0781     eth_hw_addr_set(dev, addr);
0782 
0783     priv(dev)->dev = dev;
0784     timer_setup(&priv(dev)->timer, ether3_ledoff, 0);
0785 
0786     /* Reset card...
0787      */
0788     ether3_outb(0x80, REG_CONFIG2 + 4);
0789     bus_type = BUS_UNKNOWN;
0790     udelay(4);
0791 
0792     /* Test using Receive Pointer (16-bit register) to find out
0793      * how the ether3 is connected to the bus...
0794      */
0795     if (ether3_probe_bus_8(dev, 0x100) &&
0796         ether3_probe_bus_8(dev, 0x201))
0797         bus_type = BUS_8;
0798 
0799     if (bus_type == BUS_UNKNOWN &&
0800         ether3_probe_bus_16(dev, 0x101) &&
0801         ether3_probe_bus_16(dev, 0x201))
0802         bus_type = BUS_16;
0803 
0804     switch (bus_type) {
0805     case BUS_UNKNOWN:
0806         printk(KERN_ERR "%s: unable to identify bus width\n", dev->name);
0807         ret = -ENODEV;
0808         goto free;
0809 
0810     case BUS_8:
0811         printk(KERN_ERR "%s: %s found, but is an unsupported "
0812             "8-bit card\n", dev->name, data->name);
0813         ret = -ENODEV;
0814         goto free;
0815 
0816     default:
0817         break;
0818     }
0819 
0820     if (ether3_init_2(dev)) {
0821         ret = -ENODEV;
0822         goto free;
0823     }
0824 
0825     dev->netdev_ops     = &ether3_netdev_ops;
0826     dev->watchdog_timeo = 5 * HZ / 100;
0827 
0828     ret = register_netdev(dev);
0829     if (ret)
0830         goto free;
0831 
0832     printk("%s: %s in slot %d, %pM\n",
0833            dev->name, data->name, ec->slot_no, dev->dev_addr);
0834 
0835     ecard_set_drvdata(ec, dev);
0836     return 0;
0837 
0838  free:
0839     free_netdev(dev);
0840  release:
0841     ecard_release_resources(ec);
0842  out:
0843     return ret;
0844 }
0845 
0846 static void ether3_remove(struct expansion_card *ec)
0847 {
0848     struct net_device *dev = ecard_get_drvdata(ec);
0849 
0850     ecard_set_drvdata(ec, NULL);
0851 
0852     unregister_netdev(dev);
0853     free_netdev(dev);
0854     ecard_release_resources(ec);
0855 }
0856 
0857 static struct ether3_data ether3 = {
0858     .name       = "ether3",
0859     .base_offset    = 0,
0860 };
0861 
0862 static struct ether3_data etherb = {
0863     .name       = "etherb",
0864     .base_offset    = 0x800,
0865 };
0866 
0867 static const struct ecard_id ether3_ids[] = {
0868     { MANU_ANT2, PROD_ANT_ETHER3, &ether3 },
0869     { MANU_ANT,  PROD_ANT_ETHER3, &ether3 },
0870     { MANU_ANT,  PROD_ANT_ETHERB, &etherb },
0871     { 0xffff, 0xffff }
0872 };
0873 
0874 static struct ecard_driver ether3_driver = {
0875     .probe      = ether3_probe,
0876     .remove     = ether3_remove,
0877     .id_table   = ether3_ids,
0878     .drv = {
0879         .name   = "ether3",
0880     },
0881 };
0882 
0883 static int __init ether3_init(void)
0884 {
0885     return ecard_register_driver(&ether3_driver);
0886 }
0887 
0888 static void __exit ether3_exit(void)
0889 {
0890     ecard_remove_driver(&ether3_driver);
0891 }
0892 
0893 module_init(ether3_init);
0894 module_exit(ether3_exit);
0895 
0896 MODULE_LICENSE("GPL");