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