linux-bk/drivers/net/lance.c
<<
>>
Prefs
   1/* lance.c: An AMD LANCE/PCnet ethernet driver for Linux. */
   2/*
   3        Written/copyright 1993-1998 by Donald Becker.
   4
   5        Copyright 1993 United States Government as represented by the
   6        Director, National Security Agency.
   7        This software may be used and distributed according to the terms
   8        of the GNU General Public License, incorporated herein by reference.
   9
  10        This driver is for the Allied Telesis AT1500 and HP J2405A, and should work
  11        with most other LANCE-based bus-master (NE2100/NE2500) ethercards.
  12
  13        The author may be reached as becker@scyld.com, or C/O
  14        Scyld Computing Corporation
  15        410 Severn Ave., Suite 210
  16        Annapolis MD 21403
  17
  18        Andrey V. Savochkin:
  19        - alignment problem with 1.3.* kernel and some minor changes.
  20        Thomas Bogendoerfer (tsbogend@bigbug.franken.de):
  21        - added support for Linux/Alpha, but removed most of it, because
  22        it worked only for the PCI chip. 
  23      - added hook for the 32bit lance driver
  24      - added PCnetPCI II (79C970A) to chip table
  25        Paul Gortmaker (gpg109@rsphy1.anu.edu.au):
  26        - hopefully fix above so Linux/Alpha can use ISA cards too.
  27    8/20/96 Fixed 7990 autoIRQ failure and reversed unneeded alignment -djb
  28    v1.12 10/27/97 Module support -djb
  29    v1.14  2/3/98 Module support modified, made PCI support optional -djb
  30    v1.15 5/27/99 Fixed bug in the cleanup_module(). dev->priv was freed
  31                  before unregister_netdev() which caused NULL pointer
  32                  reference later in the chain (in rtnetlink_fill_ifinfo())
  33                  -- Mika Kuoppala <miku@iki.fi>
  34    
  35    Forward ported v1.14 to 2.1.129, merged the PCI and misc changes from
  36    the 2.1 version of the old driver - Alan Cox
  37
  38    Get rid of check_region, check kmalloc return in lance_probe1
  39    Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 11/01/2001
  40*/
  41
  42static const char version[] = "lance.c:v1.15ac 1999/11/13 dplatt@3do.com, becker@cesdis.gsfc.nasa.gov\n";
  43
  44#include <linux/module.h>
  45#include <linux/kernel.h>
  46#include <linux/string.h>
  47#include <linux/errno.h>
  48#include <linux/ioport.h>
  49#include <linux/slab.h>
  50#include <linux/interrupt.h>
  51#include <linux/pci.h>
  52#include <linux/init.h>
  53#include <linux/netdevice.h>
  54#include <linux/etherdevice.h>
  55#include <linux/skbuff.h>
  56
  57#include <asm/bitops.h>
  58#include <asm/io.h>
  59#include <asm/dma.h>
  60
  61static unsigned int lance_portlist[] __initdata = { 0x300, 0x320, 0x340, 0x360, 0};
  62int lance_probe(struct net_device *dev);
  63static int lance_probe1(struct net_device *dev, int ioaddr, int irq, int options);
  64
  65#ifdef LANCE_DEBUG
  66static int lance_debug = LANCE_DEBUG;
  67#else
  68static int lance_debug = 1;
  69#endif
  70
  71/*
  72                                Theory of Operation
  73
  74I. Board Compatibility
  75
  76This device driver is designed for the AMD 79C960, the "PCnet-ISA
  77single-chip ethernet controller for ISA".  This chip is used in a wide
  78variety of boards from vendors such as Allied Telesis, HP, Kingston,
  79and Boca.  This driver is also intended to work with older AMD 7990
  80designs, such as the NE1500 and NE2100, and newer 79C961.  For convenience,
  81I use the name LANCE to refer to all of the AMD chips, even though it properly
  82refers only to the original 7990.
  83
  84II. Board-specific settings
  85
  86The driver is designed to work the boards that use the faster
  87bus-master mode, rather than in shared memory mode.      (Only older designs
  88have on-board buffer memory needed to support the slower shared memory mode.)
  89
  90Most ISA boards have jumpered settings for the I/O base, IRQ line, and DMA
  91channel.  This driver probes the likely base addresses:
  92{0x300, 0x320, 0x340, 0x360}.
  93After the board is found it generates a DMA-timeout interrupt and uses
  94autoIRQ to find the IRQ line.  The DMA channel can be set with the low bits
  95of the otherwise-unused dev->mem_start value (aka PARAM1).  If unset it is
  96probed for by enabling each free DMA channel in turn and checking if
  97initialization succeeds.
  98
  99The HP-J2405A board is an exception: with this board it is easy to read the
 100EEPROM-set values for the base, IRQ, and DMA.  (Of course you must already
 101_know_ the base address -- that field is for writing the EEPROM.)
 102
 103III. Driver operation
 104
 105IIIa. Ring buffers
 106The LANCE uses ring buffers of Tx and Rx descriptors.  Each entry describes
 107the base and length of the data buffer, along with status bits.  The length
 108of these buffers is set by LANCE_LOG_{RX,TX}_BUFFERS, which is log_2() of
 109the buffer length (rather than being directly the buffer length) for
 110implementation ease.  The current values are 2 (Tx) and 4 (Rx), which leads to
 111ring sizes of 4 (Tx) and 16 (Rx).  Increasing the number of ring entries
 112needlessly uses extra space and reduces the chance that an upper layer will
 113be able to reorder queued Tx packets based on priority.  Decreasing the number
 114of entries makes it more difficult to achieve back-to-back packet transmission
 115and increases the chance that Rx ring will overflow.  (Consider the worst case
 116of receiving back-to-back minimum-sized packets.)
 117
 118The LANCE has the capability to "chain" both Rx and Tx buffers, but this driver
 119statically allocates full-sized (slightly oversized -- PKT_BUF_SZ) buffers to
 120avoid the administrative overhead. For the Rx side this avoids dynamically
 121allocating full-sized buffers "just in case", at the expense of a
 122memory-to-memory data copy for each packet received.  For most systems this
 123is a good tradeoff: the Rx buffer will always be in low memory, the copy
 124is inexpensive, and it primes the cache for later packet processing.  For Tx
 125the buffers are only used when needed as low-memory bounce buffers.
 126
 127IIIB. 16M memory limitations.
 128For the ISA bus master mode all structures used directly by the LANCE,
 129the initialization block, Rx and Tx rings, and data buffers, must be
 130accessible from the ISA bus, i.e. in the lower 16M of real memory.
 131This is a problem for current Linux kernels on >16M machines. The network
 132devices are initialized after memory initialization, and the kernel doles out
 133memory from the top of memory downward.  The current solution is to have a
 134special network initialization routine that's called before memory
 135initialization; this will eventually be generalized for all network devices.
 136As mentioned before, low-memory "bounce-buffers" are used when needed.
 137
 138IIIC. Synchronization
 139The driver runs as two independent, single-threaded flows of control.  One
 140is the send-packet routine, which enforces single-threaded use by the
 141dev->tbusy flag.  The other thread is the interrupt handler, which is single
 142threaded by the hardware and other software.
 143
 144The send packet thread has partial control over the Tx ring and 'dev->tbusy'
 145flag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
 146queue slot is empty, it clears the tbusy flag when finished otherwise it sets
 147the 'lp->tx_full' flag.
 148
 149The interrupt handler has exclusive control over the Rx ring and records stats
 150from the Tx ring. (The Tx-done interrupt can't be selectively turned off, so
 151we can't avoid the interrupt overhead by having the Tx routine reap the Tx
 152stats.)  After reaping the stats, it marks the queue entry as empty by setting
 153the 'base' to zero. Iff the 'lp->tx_full' flag is set, it clears both the
 154tx_full and tbusy flags.
 155
 156*/
 157
 158/* Set the number of Tx and Rx buffers, using Log_2(# buffers).
 159   Reasonable default values are 16 Tx buffers, and 16 Rx buffers.
 160   That translates to 4 and 4 (16 == 2^^4).
 161   This is a compile-time option for efficiency.
 162   */
 163#ifndef LANCE_LOG_TX_BUFFERS
 164#define LANCE_LOG_TX_BUFFERS 4
 165#define LANCE_LOG_RX_BUFFERS 4
 166#endif
 167
 168#define TX_RING_SIZE                    (1 << (LANCE_LOG_TX_BUFFERS))
 169#define TX_RING_MOD_MASK                (TX_RING_SIZE - 1)
 170#define TX_RING_LEN_BITS                ((LANCE_LOG_TX_BUFFERS) << 29)
 171
 172#define RX_RING_SIZE                    (1 << (LANCE_LOG_RX_BUFFERS))
 173#define RX_RING_MOD_MASK                (RX_RING_SIZE - 1)
 174#define RX_RING_LEN_BITS                ((LANCE_LOG_RX_BUFFERS) << 29)
 175
 176#define PKT_BUF_SZ              1544
 177
 178/* Offsets from base I/O address. */
 179#define LANCE_DATA 0x10
 180#define LANCE_ADDR 0x12
 181#define LANCE_RESET 0x14
 182#define LANCE_BUS_IF 0x16
 183#define LANCE_TOTAL_SIZE 0x18
 184
 185#define TX_TIMEOUT      20
 186
 187/* The LANCE Rx and Tx ring descriptors. */
 188struct lance_rx_head {
 189        s32 base;
 190        s16 buf_length;                 /* This length is 2s complement (negative)! */
 191        s16 msg_length;                 /* This length is "normal". */
 192};
 193
 194struct lance_tx_head {
 195        s32 base;
 196        s16 length;                             /* Length is 2s complement (negative)! */
 197        s16 misc;
 198};
 199
 200/* The LANCE initialization block, described in databook. */
 201struct lance_init_block {
 202        u16 mode;               /* Pre-set mode (reg. 15) */
 203        u8  phys_addr[6]; /* Physical ethernet address */
 204        u32 filter[2];                  /* Multicast filter (unused). */
 205        /* Receive and transmit ring base, along with extra bits. */
 206        u32  rx_ring;                   /* Tx and Rx ring base pointers */
 207        u32  tx_ring;
 208};
 209
 210struct lance_private {
 211        /* The Tx and Rx ring entries must be aligned on 8-byte boundaries. */
 212        struct lance_rx_head rx_ring[RX_RING_SIZE];
 213        struct lance_tx_head tx_ring[TX_RING_SIZE];
 214        struct lance_init_block init_block;
 215        const char *name;
 216        /* The saved address of a sent-in-place packet/buffer, for skfree(). */
 217        struct sk_buff* tx_skbuff[TX_RING_SIZE];
 218        /* The addresses of receive-in-place skbuffs. */
 219        struct sk_buff* rx_skbuff[RX_RING_SIZE];
 220        unsigned long rx_buffs;         /* Address of Rx and Tx buffers. */
 221        /* Tx low-memory "bounce buffer" address. */
 222        char (*tx_bounce_buffs)[PKT_BUF_SZ];
 223        int cur_rx, cur_tx;                     /* The next free ring entry */
 224        int dirty_rx, dirty_tx;         /* The ring entries to be free()ed. */
 225        int dma;
 226        struct net_device_stats stats;
 227        unsigned char chip_version;     /* See lance_chip_type. */
 228        spinlock_t devlock;
 229};
 230
 231#define LANCE_MUST_PAD          0x00000001
 232#define LANCE_ENABLE_AUTOSELECT 0x00000002
 233#define LANCE_MUST_REINIT_RING  0x00000004
 234#define LANCE_MUST_UNRESET      0x00000008
 235#define LANCE_HAS_MISSED_FRAME  0x00000010
 236
 237/* A mapping from the chip ID number to the part number and features.
 238   These are from the datasheets -- in real life the '970 version
 239   reportedly has the same ID as the '965. */
 240static struct lance_chip_type {
 241        int id_number;
 242        const char *name;
 243        int flags;
 244} chip_table[] = {
 245        {0x0000, "LANCE 7990",                          /* Ancient lance chip.  */
 246                LANCE_MUST_PAD + LANCE_MUST_UNRESET},
 247        {0x0003, "PCnet/ISA 79C960",            /* 79C960 PCnet/ISA.  */
 248                LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
 249                        LANCE_HAS_MISSED_FRAME},
 250        {0x2260, "PCnet/ISA+ 79C961",           /* 79C961 PCnet/ISA+, Plug-n-Play.  */
 251                LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
 252                        LANCE_HAS_MISSED_FRAME},
 253        {0x2420, "PCnet/PCI 79C970",            /* 79C970 or 79C974 PCnet-SCSI, PCI. */
 254                LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
 255                        LANCE_HAS_MISSED_FRAME},
 256        /* Bug: the PCnet/PCI actually uses the PCnet/VLB ID number, so just call
 257                it the PCnet32. */
 258        {0x2430, "PCnet32",                                     /* 79C965 PCnet for VL bus. */
 259                LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
 260                        LANCE_HAS_MISSED_FRAME},
 261        {0x2621, "PCnet/PCI-II 79C970A",        /* 79C970A PCInetPCI II. */
 262                LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
 263                        LANCE_HAS_MISSED_FRAME},
 264        {0x0,    "PCnet (unknown)",
 265                LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
 266                        LANCE_HAS_MISSED_FRAME},
 267};
 268
 269enum {OLD_LANCE = 0, PCNET_ISA=1, PCNET_ISAP=2, PCNET_PCI=3, PCNET_VLB=4, PCNET_PCI_II=5, LANCE_UNKNOWN=6};
 270
 271
 272/* Non-zero if lance_probe1() needs to allocate low-memory bounce buffers.
 273   Assume yes until we know the memory size. */
 274static unsigned char lance_need_isa_bounce_buffers = 1;
 275
 276static int lance_open(struct net_device *dev);
 277static int lance_open_fail(struct net_device *dev);
 278static void lance_init_ring(struct net_device *dev, int mode);
 279static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev);
 280static int lance_rx(struct net_device *dev);
 281static irqreturn_t lance_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 282static int lance_close(struct net_device *dev);
 283static struct net_device_stats *lance_get_stats(struct net_device *dev);
 284static void set_multicast_list(struct net_device *dev);
 285static void lance_tx_timeout (struct net_device *dev);
 286
 287
 288
 289#ifdef MODULE
 290#define MAX_CARDS               8       /* Max number of interfaces (cards) per module */
 291
 292static struct net_device dev_lance[MAX_CARDS];
 293static int io[MAX_CARDS];
 294static int dma[MAX_CARDS];
 295static int irq[MAX_CARDS];
 296
 297MODULE_PARM(io, "1-" __MODULE_STRING(MAX_CARDS) "i");
 298MODULE_PARM(dma, "1-" __MODULE_STRING(MAX_CARDS) "i");
 299MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
 300MODULE_PARM(lance_debug, "i");
 301MODULE_PARM_DESC(io, "LANCE/PCnet I/O base address(es),required");
 302MODULE_PARM_DESC(dma, "LANCE/PCnet ISA DMA channel (ignored for some devices)");
 303MODULE_PARM_DESC(irq, "LANCE/PCnet IRQ number (ignored for some devices)");
 304MODULE_PARM_DESC(lance_debug, "LANCE/PCnet debug level (0-7)");
 305
 306int init_module(void)
 307{
 308        int this_dev, found = 0;
 309
 310        for (this_dev = 0; this_dev < MAX_CARDS; this_dev++) {
 311                struct net_device *dev = &dev_lance[this_dev];
 312                dev->irq = irq[this_dev];
 313                dev->base_addr = io[this_dev];
 314                dev->dma = dma[this_dev];
 315                dev->init = lance_probe;
 316                if (io[this_dev] == 0)  {
 317                        if (this_dev != 0) break; /* only complain once */
 318                        printk(KERN_NOTICE "lance.c: Module autoprobing not allowed. Append \"io=0xNNN\" value(s).\n");
 319                        return -EPERM;
 320                }
 321                if (register_netdev(dev) != 0) {
 322                        printk(KERN_WARNING "lance.c: No PCnet/LANCE card found (i/o = 0x%x).\n", io[this_dev]);
 323                        if (found != 0) return 0;       /* Got at least one. */
 324                        return -ENXIO;
 325                }
 326                found++;
 327        }
 328
 329        return 0;
 330}
 331
 332void cleanup_module(void)
 333{
 334        int this_dev;
 335
 336        for (this_dev = 0; this_dev < MAX_CARDS; this_dev++) {
 337                struct net_device *dev = &dev_lance[this_dev];
 338                if (dev->priv != NULL) {
 339                        unregister_netdev(dev); 
 340                        free_dma(dev->dma);
 341                        release_region(dev->base_addr, LANCE_TOTAL_SIZE);
 342                        kfree(dev->priv);
 343                        dev->priv = NULL;
 344                }
 345        }
 346}
 347#endif /* MODULE */
 348MODULE_LICENSE("GPL");
 349
 350
 351/* Starting in v2.1.*, the LANCE/PCnet probe is now similar to the other
 352   board probes now that kmalloc() can allocate ISA DMA-able regions.
 353   This also allows the LANCE driver to be used as a module.
 354   */
 355int __init lance_probe(struct net_device *dev)
 356{
 357        int *port, result;
 358
 359        if (high_memory <= phys_to_virt(16*1024*1024))
 360                lance_need_isa_bounce_buffers = 0;
 361
 362        for (port = lance_portlist; *port; port++) {
 363                int ioaddr = *port;
 364                struct resource *r = request_region(ioaddr, LANCE_TOTAL_SIZE,
 365                                                        "lance-probe");
 366
 367                if (r) {
 368                        /* Detect "normal" 0x57 0x57 and the NI6510EB 0x52 0x44
 369                           signatures w/ minimal I/O reads */
 370                        char offset15, offset14 = inb(ioaddr + 14);
 371                        
 372                        if ((offset14 == 0x52 || offset14 == 0x57) &&
 373                                ((offset15 = inb(ioaddr + 15)) == 0x57 ||
 374                                 offset15 == 0x44)) {
 375                                result = lance_probe1(dev, ioaddr, 0, 0);
 376                                if (!result) {
 377                                        struct lance_private *lp = dev->priv;
 378                                        int ver = lp->chip_version;
 379
 380                                        r->name = chip_table[ver].name;
 381                                        return 0;
 382                                }
 383                        }
 384                        release_region(ioaddr, LANCE_TOTAL_SIZE);
 385                }
 386        }
 387        return -ENODEV;
 388}
 389
 390static int __init lance_probe1(struct net_device *dev, int ioaddr, int irq, int options)
 391{
 392        struct lance_private *lp;
 393        long dma_channels;                      /* Mark spuriously-busy DMA channels */
 394        int i, reset_val, lance_version;
 395        const char *chipname;
 396        /* Flags for specific chips or boards. */
 397        unsigned char hpJ2405A = 0;             /* HP ISA adaptor */
 398        int hp_builtin = 0;                     /* HP on-board ethernet. */
 399        static int did_version;                 /* Already printed version info. */
 400        unsigned long flags;
 401
 402        /* First we look for special cases.
 403           Check for HP's on-board ethernet by looking for 'HP' in the BIOS.
 404           There are two HP versions, check the BIOS for the configuration port.
 405           This method provided by L. Julliard, Laurent_Julliard@grenoble.hp.com.
 406           */
 407        if (isa_readw(0x000f0102) == 0x5048)  {
 408                static const short ioaddr_table[] = { 0x300, 0x320, 0x340, 0x360};
 409                int hp_port = (isa_readl(0x000f00f1) & 1)  ? 0x499 : 0x99;
 410                /* We can have boards other than the built-in!  Verify this is on-board. */
 411                if ((inb(hp_port) & 0xc0) == 0x80
 412                        && ioaddr_table[inb(hp_port) & 3] == ioaddr)
 413                        hp_builtin = hp_port;
 414        }
 415        /* We also recognize the HP Vectra on-board here, but check below. */
 416        hpJ2405A = (inb(ioaddr) == 0x08 && inb(ioaddr+1) == 0x00
 417                                && inb(ioaddr+2) == 0x09);
 418
 419        /* Reset the LANCE.      */
 420        reset_val = inw(ioaddr+LANCE_RESET); /* Reset the LANCE */
 421
 422        /* The Un-Reset needed is only needed for the real NE2100, and will
 423           confuse the HP board. */
 424        if (!hpJ2405A)
 425                outw(reset_val, ioaddr+LANCE_RESET);
 426
 427        outw(0x0000, ioaddr+LANCE_ADDR); /* Switch to window 0 */
 428        if (inw(ioaddr+LANCE_DATA) != 0x0004)
 429                return -ENODEV;
 430
 431        /* Get the version of the chip. */
 432        outw(88, ioaddr+LANCE_ADDR);
 433        if (inw(ioaddr+LANCE_ADDR) != 88) {
 434                lance_version = 0;
 435        } else {                                                        /* Good, it's a newer chip. */
 436                int chip_version = inw(ioaddr+LANCE_DATA);
 437                outw(89, ioaddr+LANCE_ADDR);
 438                chip_version |= inw(ioaddr+LANCE_DATA) << 16;
 439                if (lance_debug > 2)
 440                        printk("  LANCE chip version is %#x.\n", chip_version);
 441                if ((chip_version & 0xfff) != 0x003)
 442                        return -ENODEV;
 443                chip_version = (chip_version >> 12) & 0xffff;
 444                for (lance_version = 1; chip_table[lance_version].id_number; lance_version++) {
 445                        if (chip_table[lance_version].id_number == chip_version)
 446                                break;
 447                }
 448        }
 449
 450        /* We can't use init_etherdev() to allocate dev->priv because it must
 451           a ISA DMA-able region. */
 452        dev = init_etherdev(dev, 0);
 453        if (!dev)
 454                return -ENOMEM;
 455        SET_MODULE_OWNER(dev);
 456        dev->open = lance_open_fail;
 457        chipname = chip_table[lance_version].name;
 458        printk("%s: %s at %#3x,", dev->name, chipname, ioaddr);
 459
 460        /* There is a 16 byte station address PROM at the base address.
 461           The first six bytes are the station address. */
 462        for (i = 0; i < 6; i++)
 463                printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
 464
 465        dev->base_addr = ioaddr;
 466        /* Make certain the data structures used by the LANCE are aligned and DMAble. */
 467                
 468        lp = (struct lance_private *)(((unsigned long)kmalloc(sizeof(*lp)+7,
 469                                           GFP_DMA | GFP_KERNEL)+7) & ~7);
 470        if(lp==NULL)
 471                return -ENODEV;
 472        if (lance_debug > 6) printk(" (#0x%05lx)", (unsigned long)lp);
 473        memset(lp, 0, sizeof(*lp));
 474        dev->priv = lp;
 475        lp->name = chipname;
 476        lp->rx_buffs = (unsigned long)kmalloc(PKT_BUF_SZ*RX_RING_SIZE,
 477                                                  GFP_DMA | GFP_KERNEL);
 478        if (!lp->rx_buffs)
 479                goto out_lp;
 480        if (lance_need_isa_bounce_buffers) {
 481                lp->tx_bounce_buffs = kmalloc(PKT_BUF_SZ*TX_RING_SIZE,
 482                                                  GFP_DMA | GFP_KERNEL);
 483                if (!lp->tx_bounce_buffs)
 484                        goto out_rx;
 485        } else
 486                lp->tx_bounce_buffs = NULL;
 487
 488        lp->chip_version = lance_version;
 489        lp->devlock = SPIN_LOCK_UNLOCKED;
 490
 491        lp->init_block.mode = 0x0003;           /* Disable Rx and Tx. */
 492        for (i = 0; i < 6; i++)
 493                lp->init_block.phys_addr[i] = dev->dev_addr[i];
 494        lp->init_block.filter[0] = 0x00000000;
 495        lp->init_block.filter[1] = 0x00000000;
 496        lp->init_block.rx_ring = ((u32)isa_virt_to_bus(lp->rx_ring) & 0xffffff) | RX_RING_LEN_BITS;
 497        lp->init_block.tx_ring = ((u32)isa_virt_to_bus(lp->tx_ring) & 0xffffff) | TX_RING_LEN_BITS;
 498
 499        outw(0x0001, ioaddr+LANCE_ADDR);
 500        inw(ioaddr+LANCE_ADDR);
 501        outw((short) (u32) isa_virt_to_bus(&lp->init_block), ioaddr+LANCE_DATA);
 502        outw(0x0002, ioaddr+LANCE_ADDR);
 503        inw(ioaddr+LANCE_ADDR);
 504        outw(((u32)isa_virt_to_bus(&lp->init_block)) >> 16, ioaddr+LANCE_DATA);
 505        outw(0x0000, ioaddr+LANCE_ADDR);
 506        inw(ioaddr+LANCE_ADDR);
 507
 508        if (irq) {                                      /* Set iff PCI card. */
 509                dev->dma = 4;                   /* Native bus-master, no DMA channel needed. */
 510                dev->irq = irq;
 511        } else if (hp_builtin) {
 512                static const char dma_tbl[4] = {3, 5, 6, 0};
 513                static const char irq_tbl[4] = {3, 4, 5, 9};
 514                unsigned char port_val = inb(hp_builtin);
 515                dev->dma = dma_tbl[(port_val >> 4) & 3];
 516                dev->irq = irq_tbl[(port_val >> 2) & 3];
 517                printk(" HP Vectra IRQ %d DMA %d.\n", dev->irq, dev->dma);
 518        } else if (hpJ2405A) {
 519                static const char dma_tbl[4] = {3, 5, 6, 7};
 520                static const char irq_tbl[8] = {3, 4, 5, 9, 10, 11, 12, 15};
 521                short reset_val = inw(ioaddr+LANCE_RESET);
 522                dev->dma = dma_tbl[(reset_val >> 2) & 3];
 523                dev->irq = irq_tbl[(reset_val >> 4) & 7];
 524                printk(" HP J2405A IRQ %d DMA %d.\n", dev->irq, dev->dma);
 525        } else if (lance_version == PCNET_ISAP) {               /* The plug-n-play version. */
 526                short bus_info;
 527                outw(8, ioaddr+LANCE_ADDR);
 528                bus_info = inw(ioaddr+LANCE_BUS_IF);
 529                dev->dma = bus_info & 0x07;
 530                dev->irq = (bus_info >> 4) & 0x0F;
 531        } else {
 532                /* The DMA channel may be passed in PARAM1. */
 533                if (dev->mem_start & 0x07)
 534                        dev->dma = dev->mem_start & 0x07;
 535        }
 536
 537        if (dev->dma == 0) {
 538                /* Read the DMA channel status register, so that we can avoid
 539                   stuck DMA channels in the DMA detection below. */
 540                dma_channels = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
 541                        (inb(DMA2_STAT_REG) & 0xf0);
 542        }
 543        if (dev->irq >= 2)
 544                printk(" assigned IRQ %d", dev->irq);
 545        else if (lance_version != 0)  { /* 7990 boards need DMA detection first. */
 546                unsigned long irq_mask;
 547
 548                /* To auto-IRQ we enable the initialization-done and DMA error
 549                   interrupts. For ISA boards we get a DMA error, but VLB and PCI
 550                   boards will work. */
 551                irq_mask = probe_irq_on();
 552
 553                /* Trigger an initialization just for the interrupt. */
 554                outw(0x0041, ioaddr+LANCE_DATA);
 555
 556                mdelay(20);
 557                dev->irq = probe_irq_off(irq_mask);
 558                if (dev->irq)
 559                        printk(", probed IRQ %d", dev->irq);
 560                else {
 561                        printk(", failed to detect IRQ line.\n");
 562                        return -ENODEV;
 563                }
 564
 565                /* Check for the initialization done bit, 0x0100, which means
 566                   that we don't need a DMA channel. */
 567                if (inw(ioaddr+LANCE_DATA) & 0x0100)
 568                        dev->dma = 4;
 569        }
 570
 571        if (dev->dma == 4) {
 572                printk(", no DMA needed.\n");
 573        } else if (dev->dma) {
 574                if (request_dma(dev->dma, chipname)) {
 575                        printk("DMA %d allocation failed.\n", dev->dma);
 576                        return -ENODEV;
 577                } else
 578                        printk(", assigned DMA %d.\n", dev->dma);
 579        } else {                        /* OK, we have to auto-DMA. */
 580                for (i = 0; i < 4; i++) {
 581                        static const char dmas[] = { 5, 6, 7, 3 };
 582                        int dma = dmas[i];
 583                        int boguscnt;
 584
 585                        /* Don't enable a permanently busy DMA channel, or the machine
 586                           will hang. */
 587                        if (test_bit(dma, &dma_channels))
 588                                continue;
 589                        outw(0x7f04, ioaddr+LANCE_DATA); /* Clear the memory error bits. */
 590                        if (request_dma(dma, chipname))
 591                                continue;
 592                                
 593                        flags=claim_dma_lock();
 594                        set_dma_mode(dma, DMA_MODE_CASCADE);
 595                        enable_dma(dma);
 596                        release_dma_lock(flags);
 597
 598                        /* Trigger an initialization. */
 599                        outw(0x0001, ioaddr+LANCE_DATA);
 600                        for (boguscnt = 100; boguscnt > 0; --boguscnt)
 601                                if (inw(ioaddr+LANCE_DATA) & 0x0900)
 602                                        break;
 603                        if (inw(ioaddr+LANCE_DATA) & 0x0100) {
 604                                dev->dma = dma;
 605                                printk(", DMA %d.\n", dev->dma);
 606                                break;
 607                        } else {
 608                                flags=claim_dma_lock();
 609                                disable_dma(dma);
 610                                release_dma_lock(flags);
 611                                free_dma(dma);
 612                        }
 613                }
 614                if (i == 4) {                   /* Failure: bail. */
 615                        printk("DMA detection failed.\n");
 616                        return -ENODEV;
 617                }
 618        }
 619
 620        if (lance_version == 0 && dev->irq == 0) {
 621                /* We may auto-IRQ now that we have a DMA channel. */
 622                /* Trigger an initialization just for the interrupt. */
 623                unsigned long irq_mask;
 624
 625                irq_mask = probe_irq_on();
 626                outw(0x0041, ioaddr+LANCE_DATA);
 627
 628                mdelay(40);
 629                dev->irq = probe_irq_off(irq_mask);
 630                if (dev->irq == 0) {
 631                        printk("  Failed to detect the 7990 IRQ line.\n");
 632                        return -ENODEV;
 633                }
 634                printk("  Auto-IRQ detected IRQ%d.\n", dev->irq);
 635        }
 636
 637        if (chip_table[lp->chip_version].flags & LANCE_ENABLE_AUTOSELECT) {
 638                /* Turn on auto-select of media (10baseT or BNC) so that the user
 639                   can watch the LEDs even if the board isn't opened. */
 640                outw(0x0002, ioaddr+LANCE_ADDR);
 641                /* Don't touch 10base2 power bit. */
 642                outw(inw(ioaddr+LANCE_BUS_IF) | 0x0002, ioaddr+LANCE_BUS_IF);
 643        }
 644
 645        if (lance_debug > 0  &&  did_version++ == 0)
 646                printk(version);
 647
 648        /* The LANCE-specific entries in the device structure. */
 649        dev->open = lance_open;
 650        dev->hard_start_xmit = lance_start_xmit;
 651        dev->stop = lance_close;
 652        dev->get_stats = lance_get_stats;
 653        dev->set_multicast_list = set_multicast_list;
 654        dev->tx_timeout = lance_tx_timeout;
 655        dev->watchdog_timeo = TX_TIMEOUT;
 656
 657        return 0;
 658out_rx: kfree((void*)lp->rx_buffs);
 659out_lp: kfree(lp);
 660        return -ENOMEM;
 661}
 662
 663static int
 664lance_open_fail(struct net_device *dev)
 665{
 666        return -ENODEV;
 667}
 668
 669
 670
 671static int
 672lance_open(struct net_device *dev)
 673{
 674        struct lance_private *lp = dev->priv;
 675        int ioaddr = dev->base_addr;
 676        int i;
 677
 678        if (dev->irq == 0 ||
 679                request_irq(dev->irq, &lance_interrupt, 0, lp->name, dev)) {
 680                return -EAGAIN;
 681        }
 682
 683        /* We used to allocate DMA here, but that was silly.
 684           DMA lines can't be shared!  We now permanently allocate them. */
 685
 686        /* Reset the LANCE */
 687        inw(ioaddr+LANCE_RESET);
 688
 689        /* The DMA controller is used as a no-operation slave, "cascade mode". */
 690        if (dev->dma != 4) {
 691                unsigned long flags=claim_dma_lock();
 692                enable_dma(dev->dma);
 693                set_dma_mode(dev->dma, DMA_MODE_CASCADE);
 694                release_dma_lock(flags);
 695        }
 696
 697        /* Un-Reset the LANCE, needed only for the NE2100. */
 698        if (chip_table[lp->chip_version].flags & LANCE_MUST_UNRESET)
 699                outw(0, ioaddr+LANCE_RESET);
 700
 701        if (chip_table[lp->chip_version].flags & LANCE_ENABLE_AUTOSELECT) {
 702                /* This is 79C960-specific: Turn on auto-select of media (AUI, BNC). */
 703                outw(0x0002, ioaddr+LANCE_ADDR);
 704                /* Only touch autoselect bit. */
 705                outw(inw(ioaddr+LANCE_BUS_IF) | 0x0002, ioaddr+LANCE_BUS_IF);
 706        }
 707
 708        if (lance_debug > 1)
 709                printk("%s: lance_open() irq %d dma %d tx/rx rings %#x/%#x init %#x.\n",
 710                           dev->name, dev->irq, dev->dma,
 711                           (u32) isa_virt_to_bus(lp->tx_ring),
 712                           (u32) isa_virt_to_bus(lp->rx_ring),
 713                           (u32) isa_virt_to_bus(&lp->init_block));
 714
 715        lance_init_ring(dev, GFP_KERNEL);
 716        /* Re-initialize the LANCE, and start it when done. */
 717        outw(0x0001, ioaddr+LANCE_ADDR);
 718        outw((short) (u32) isa_virt_to_bus(&lp->init_block), ioaddr+LANCE_DATA);
 719        outw(0x0002, ioaddr+LANCE_ADDR);
 720        outw(((u32)isa_virt_to_bus(&lp->init_block)) >> 16, ioaddr+LANCE_DATA);
 721
 722        outw(0x0004, ioaddr+LANCE_ADDR);
 723        outw(0x0915, ioaddr+LANCE_DATA);
 724
 725        outw(0x0000, ioaddr+LANCE_ADDR);
 726        outw(0x0001, ioaddr+LANCE_DATA);
 727
 728        netif_start_queue (dev);
 729
 730        i = 0;
 731        while (i++ < 100)
 732                if (inw(ioaddr+LANCE_DATA) & 0x0100)
 733                        break;
 734        /* 
 735         * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
 736         * reports that doing so triggers a bug in the '974.
 737         */
 738        outw(0x0042, ioaddr+LANCE_DATA);
 739
 740        if (lance_debug > 2)
 741                printk("%s: LANCE open after %d ticks, init block %#x csr0 %4.4x.\n",
 742                           dev->name, i, (u32) isa_virt_to_bus(&lp->init_block), inw(ioaddr+LANCE_DATA));
 743
 744        return 0;                                       /* Always succeed */
 745}
 746
 747/* The LANCE has been halted for one reason or another (busmaster memory
 748   arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
 749   etc.).  Modern LANCE variants always reload their ring-buffer
 750   configuration when restarted, so we must reinitialize our ring
 751   context before restarting.  As part of this reinitialization,
 752   find all packets still on the Tx ring and pretend that they had been
 753   sent (in effect, drop the packets on the floor) - the higher-level
 754   protocols will time out and retransmit.  It'd be better to shuffle
 755   these skbs to a temp list and then actually re-Tx them after
 756   restarting the chip, but I'm too lazy to do so right now.  dplatt@3do.com
 757*/
 758
 759static void 
 760lance_purge_ring(struct net_device *dev)
 761{
 762        struct lance_private *lp = dev->priv;
 763        int i;
 764
 765        /* Free all the skbuffs in the Rx and Tx queues. */
 766        for (i = 0; i < RX_RING_SIZE; i++) {
 767                struct sk_buff *skb = lp->rx_skbuff[i];
 768                lp->rx_skbuff[i] = 0;
 769                lp->rx_ring[i].base = 0;                /* Not owned by LANCE chip. */
 770                if (skb)
 771                        dev_kfree_skb_any(skb);
 772        }
 773        for (i = 0; i < TX_RING_SIZE; i++) {
 774                if (lp->tx_skbuff[i]) {
 775                        dev_kfree_skb_any(lp->tx_skbuff[i]);
 776                        lp->tx_skbuff[i] = NULL;
 777                }
 778        }
 779}
 780
 781
 782/* Initialize the LANCE Rx and Tx rings. */
 783static void
 784lance_init_ring(struct net_device *dev, int gfp)
 785{
 786        struct lance_private *lp = dev->priv;
 787        int i;
 788
 789        lp->cur_rx = lp->cur_tx = 0;
 790        lp->dirty_rx = lp->dirty_tx = 0;
 791
 792        for (i = 0; i < RX_RING_SIZE; i++) {
 793                struct sk_buff *skb;
 794                void *rx_buff;
 795
 796                skb = alloc_skb(PKT_BUF_SZ, GFP_DMA | gfp);
 797                lp->rx_skbuff[i] = skb;
 798                if (skb) {
 799                        skb->dev = dev;
 800                        rx_buff = skb->tail;
 801                } else
 802                        rx_buff = kmalloc(PKT_BUF_SZ, GFP_DMA | gfp);
 803                if (rx_buff == NULL)
 804                        lp->rx_ring[i].base = 0;
 805                else
 806                        lp->rx_ring[i].base = (u32)isa_virt_to_bus(rx_buff) | 0x80000000;
 807                lp->rx_ring[i].buf_length = -PKT_BUF_SZ;
 808        }
 809        /* The Tx buffer address is filled in as needed, but we do need to clear
 810           the upper ownership bit. */
 811        for (i = 0; i < TX_RING_SIZE; i++) {
 812                lp->tx_skbuff[i] = 0;
 813                lp->tx_ring[i].base = 0;
 814        }
 815
 816        lp->init_block.mode = 0x0000;
 817        for (i = 0; i < 6; i++)
 818                lp->init_block.phys_addr[i] = dev->dev_addr[i];
 819        lp->init_block.filter[0] = 0x00000000;
 820        lp->init_block.filter[1] = 0x00000000;
 821        lp->init_block.rx_ring = ((u32)isa_virt_to_bus(lp->rx_ring) & 0xffffff) | RX_RING_LEN_BITS;
 822        lp->init_block.tx_ring = ((u32)isa_virt_to_bus(lp->tx_ring) & 0xffffff) | TX_RING_LEN_BITS;
 823}
 824
 825static void
 826lance_restart(struct net_device *dev, unsigned int csr0_bits, int must_reinit)
 827{
 828        struct lance_private *lp = dev->priv;
 829
 830        if (must_reinit ||
 831                (chip_table[lp->chip_version].flags & LANCE_MUST_REINIT_RING)) {
 832                lance_purge_ring(dev);
 833                lance_init_ring(dev, GFP_ATOMIC);
 834        }
 835        outw(0x0000,    dev->base_addr + LANCE_ADDR);
 836        outw(csr0_bits, dev->base_addr + LANCE_DATA);
 837}
 838
 839
 840static void lance_tx_timeout (struct net_device *dev)
 841{
 842        struct lance_private *lp = (struct lance_private *) dev->priv;
 843        int ioaddr = dev->base_addr;
 844
 845        outw (0, ioaddr + LANCE_ADDR);
 846        printk ("%s: transmit timed out, status %4.4x, resetting.\n",
 847                dev->name, inw (ioaddr + LANCE_DATA));
 848        outw (0x0004, ioaddr + LANCE_DATA);
 849        lp->stats.tx_errors++;
 850#ifndef final_version
 851        if (lance_debug > 3) {
 852                int i;
 853                printk (" Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
 854                  lp->dirty_tx, lp->cur_tx, netif_queue_stopped(dev) ? " (full)" : "",
 855                        lp->cur_rx);
 856                for (i = 0; i < RX_RING_SIZE; i++)
 857                        printk ("%s %08x %04x %04x", i & 0x3 ? "" : "\n ",
 858                         lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
 859                                lp->rx_ring[i].msg_length);
 860                for (i = 0; i < TX_RING_SIZE; i++)
 861                        printk ("%s %08x %04x %04x", i & 0x3 ? "" : "\n ",
 862                             lp->tx_ring[i].base, -lp->tx_ring[i].length,
 863                                lp->tx_ring[i].misc);
 864                printk ("\n");
 865        }
 866#endif
 867        lance_restart (dev, 0x0043, 1);
 868
 869        dev->trans_start = jiffies;
 870        netif_wake_queue (dev);
 871}
 872
 873
 874static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
 875{
 876        struct lance_private *lp = dev->priv;
 877        int ioaddr = dev->base_addr;
 878        int entry;
 879        unsigned long flags;
 880
 881        spin_lock_irqsave(&lp->devlock, flags);
 882
 883        if (lance_debug > 3) {
 884                outw(0x0000, ioaddr+LANCE_ADDR);
 885                printk("%s: lance_start_xmit() called, csr0 %4.4x.\n", dev->name,
 886                           inw(ioaddr+LANCE_DATA));
 887                outw(0x0000, ioaddr+LANCE_DATA);
 888        }
 889
 890        /* Fill in a Tx ring entry */
 891
 892        /* Mask to ring buffer boundary. */
 893        entry = lp->cur_tx & TX_RING_MOD_MASK;
 894
 895        /* Caution: the write order is important here, set the base address
 896           with the "ownership" bits last. */
 897
 898        /* The old LANCE chips doesn't automatically pad buffers to min. size. */
 899        if (chip_table[lp->chip_version].flags & LANCE_MUST_PAD) {
 900                if (skb->len < ETH_ZLEN) {
 901                        skb = skb_padto(skb, ETH_ZLEN);
 902                        if (skb == NULL)
 903                                goto out;
 904                        lp->tx_ring[entry].length = -ETH_ZLEN;
 905                }
 906                else 
 907                        lp->tx_ring[entry].length = -skb->len;
 908        } else
 909                lp->tx_ring[entry].length = -skb->len;
 910
 911        lp->tx_ring[entry].misc = 0x0000;
 912
 913        lp->stats.tx_bytes += skb->len;
 914
 915        /* If any part of this buffer is >16M we must copy it to a low-memory
 916           buffer. */
 917        if ((u32)isa_virt_to_bus(skb->data) + skb->len > 0x01000000) {
 918                if (lance_debug > 5)
 919                        printk("%s: bouncing a high-memory packet (%#x).\n",
 920                                   dev->name, (u32)isa_virt_to_bus(skb->data));
 921                memcpy(&lp->tx_bounce_buffs[entry], skb->data, skb->len);
 922                lp->tx_ring[entry].base =
 923                        ((u32)isa_virt_to_bus((lp->tx_bounce_buffs + entry)) & 0xffffff) | 0x83000000;
 924                dev_kfree_skb(skb);
 925        } else {
 926                lp->tx_skbuff[entry] = skb;
 927                lp->tx_ring[entry].base = ((u32)isa_virt_to_bus(skb->data) & 0xffffff) | 0x83000000;
 928        }
 929        lp->cur_tx++;
 930
 931        /* Trigger an immediate send poll. */
 932        outw(0x0000, ioaddr+LANCE_ADDR);
 933        outw(0x0048, ioaddr+LANCE_DATA);
 934
 935        dev->trans_start = jiffies;
 936
 937        if ((lp->cur_tx - lp->dirty_tx) >= TX_RING_SIZE)
 938                netif_stop_queue(dev);
 939
 940out:
 941        spin_unlock_irqrestore(&lp->devlock, flags);
 942        return 0;
 943}
 944
 945/* The LANCE interrupt handler. */
 946static irqreturn_t
 947lance_interrupt(int irq, void *dev_id, struct pt_regs * regs)
 948{
 949        struct net_device *dev = dev_id;
 950        struct lance_private *lp;
 951        int csr0, ioaddr, boguscnt=10;
 952        int must_restart;
 953
 954        if (dev == NULL) {
 955                printk ("lance_interrupt(): irq %d for unknown device.\n", irq);
 956                return IRQ_NONE;
 957        }
 958
 959        ioaddr = dev->base_addr;
 960        lp = dev->priv;
 961        
 962        spin_lock (&lp->devlock);
 963
 964        outw(0x00, dev->base_addr + LANCE_ADDR);
 965        while ((csr0 = inw(dev->base_addr + LANCE_DATA)) & 0x8600
 966                   && --boguscnt >= 0) {
 967                /* Acknowledge all of the current interrupt sources ASAP. */
 968                outw(csr0 & ~0x004f, dev->base_addr + LANCE_DATA);
 969
 970                must_restart = 0;
 971
 972                if (lance_debug > 5)
 973                        printk("%s: interrupt  csr0=%#2.2x new csr=%#2.2x.\n",
 974                                   dev->name, csr0, inw(dev->base_addr + LANCE_DATA));
 975
 976                if (csr0 & 0x0400)                      /* Rx interrupt */
 977                        lance_rx(dev);
 978
 979                if (csr0 & 0x0200) {            /* Tx-done interrupt */
 980                        int dirty_tx = lp->dirty_tx;
 981
 982                        while (dirty_tx < lp->cur_tx) {
 983                                int entry = dirty_tx & TX_RING_MOD_MASK;
 984                                int status = lp->tx_ring[entry].base;
 985                        
 986                                if (status < 0)
 987                                        break;                  /* It still hasn't been Txed */
 988
 989                                lp->tx_ring[entry].base = 0;
 990
 991                                if (status & 0x40000000) {
 992                                        /* There was an major error, log it. */
 993                                        int err_status = lp->tx_ring[entry].misc;
 994                                        lp->stats.tx_errors++;
 995                                        if (err_status & 0x0400) lp->stats.tx_aborted_errors++;
 996                                        if (err_status & 0x0800) lp->stats.tx_carrier_errors++;
 997                                        if (err_status & 0x1000) lp->stats.tx_window_errors++;
 998                                        if (err_status & 0x4000) {
 999                                                /* Ackk!  On FIFO errors the Tx unit is turned off! */
1000                                                lp->stats.tx_fifo_errors++;
1001                                                /* Remove this verbosity later! */
1002                                                printk("%s: Tx FIFO error! Status %4.4x.\n",
1003                                                           dev->name, csr0);
1004                                                /* Restart the chip. */
1005                                                must_restart = 1;
1006                                        }
1007                                } else {
1008                                        if (status & 0x18000000)
1009                                                lp->stats.collisions++;
1010                                        lp->stats.tx_packets++;
1011                                }
1012
1013                                /* We must free the original skb if it's not a data-only copy
1014                                   in the bounce buffer. */
1015                                if (lp->tx_skbuff[entry]) {
1016                                        dev_kfree_skb_irq(lp->tx_skbuff[entry]);
1017                                        lp->tx_skbuff[entry] = 0;
1018                                }
1019                                dirty_tx++;
1020                        }
1021
1022#ifndef final_version
1023                        if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1024                                printk("out-of-sync dirty pointer, %d vs. %d, full=%s.\n",
1025                                           dirty_tx, lp->cur_tx,
1026                                           netif_queue_stopped(dev) ? "yes" : "no");
1027                                dirty_tx += TX_RING_SIZE;
1028                        }
1029#endif
1030
1031                        /* if the ring is no longer full, accept more packets */
1032                        if (netif_queue_stopped(dev) &&
1033                            dirty_tx > lp->cur_tx - TX_RING_SIZE + 2)
1034                                netif_wake_queue (dev);
1035
1036                        lp->dirty_tx = dirty_tx;
1037                }
1038
1039                /* Log misc errors. */
1040                if (csr0 & 0x4000) lp->stats.tx_errors++; /* Tx babble. */
1041                if (csr0 & 0x1000) lp->stats.rx_errors++; /* Missed a Rx frame. */
1042                if (csr0 & 0x0800) {
1043                        printk("%s: Bus master arbitration failure, status %4.4x.\n",
1044                                   dev->name, csr0);
1045                        /* Restart the chip. */
1046                        must_restart = 1;
1047                }
1048
1049                if (must_restart) {
1050                        /* stop the chip to clear the error condition, then restart */
1051                        outw(0x0000, dev->base_addr + LANCE_ADDR);
1052                        outw(0x0004, dev->base_addr + LANCE_DATA);
1053                        lance_restart(dev, 0x0002, 0);
1054                }
1055        }
1056
1057        /* Clear any other interrupt, and set interrupt enable. */
1058        outw(0x0000, dev->base_addr + LANCE_ADDR);
1059        outw(0x7940, dev->base_addr + LANCE_DATA);
1060
1061        if (lance_debug > 4)
1062                printk("%s: exiting interrupt, csr%d=%#4.4x.\n",
1063                           dev->name, inw(ioaddr + LANCE_ADDR),
1064                           inw(dev->base_addr + LANCE_DATA));
1065
1066        spin_unlock (&lp->devlock);
1067        return IRQ_HANDLED;
1068}
1069
1070static int
1071lance_rx(struct net_device *dev)
1072{
1073        struct lance_private *lp = dev->priv;
1074        int entry = lp->cur_rx & RX_RING_MOD_MASK;
1075        int i;
1076                
1077        /* If we own the next entry, it's a new packet. Send it up. */
1078        while (lp->rx_ring[entry].base >= 0) {
1079                int status = lp->rx_ring[entry].base >> 24;
1080
1081                if (status != 0x03) {                   /* There was an error. */
1082                        /* There is a tricky error noted by John Murphy,
1083                           <murf@perftech.com> to Russ Nelson: Even with full-sized
1084                           buffers it's possible for a jabber packet to use two
1085                           buffers, with only the last correctly noting the error. */
1086                        if (status & 0x01)      /* Only count a general error at the */
1087                                lp->stats.rx_errors++; /* end of a packet.*/
1088                        if (status & 0x20) lp->stats.rx_frame_errors++;
1089                        if (status & 0x10) lp->stats.rx_over_errors++;
1090                        if (status & 0x08) lp->stats.rx_crc_errors++;
1091                        if (status & 0x04) lp->stats.rx_fifo_errors++;
1092                        lp->rx_ring[entry].base &= 0x03ffffff;
1093                }
1094                else 
1095                {
1096                        /* Malloc up new buffer, compatible with net3. */
1097                        short pkt_len = (lp->rx_ring[entry].msg_length & 0xfff)-4;
1098                        struct sk_buff *skb;
1099                        
1100                        if(pkt_len<60)
1101                        {
1102                                printk("%s: Runt packet!\n",dev->name);
1103                                lp->stats.rx_errors++;
1104                        }
1105                        else
1106                        {
1107                                skb = dev_alloc_skb(pkt_len+2);
1108                                if (skb == NULL) 
1109                                {
1110                                        printk("%s: Memory squeeze, deferring packet.\n", dev->name);
1111                                        for (i=0; i < RX_RING_SIZE; i++)
1112                                                if (lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].base < 0)
1113                                                        break;
1114
1115                                        if (i > RX_RING_SIZE -2) 
1116                                        {
1117                                                lp->stats.rx_dropped++;
1118                                                lp->rx_ring[entry].base |= 0x80000000;
1119                                                lp->cur_rx++;
1120                                        }
1121                                        break;
1122                                }
1123                                skb->dev = dev;
1124                                skb_reserve(skb,2);     /* 16 byte align */
1125                                skb_put(skb,pkt_len);   /* Make room */
1126                                eth_copy_and_sum(skb,
1127                                        (unsigned char *)isa_bus_to_virt((lp->rx_ring[entry].base & 0x00ffffff)),
1128                                        pkt_len,0);
1129                                skb->protocol=eth_type_trans(skb,dev);
1130                                netif_rx(skb);
1131                                dev->last_rx = jiffies;
1132                                lp->stats.rx_packets++;
1133                                lp->stats.rx_bytes+=pkt_len;
1134                        }
1135                }
1136                /* The docs say that the buffer length isn't touched, but Andrew Boyd
1137                   of QNX reports that some revs of the 79C965 clear it. */
1138                lp->rx_ring[entry].buf_length = -PKT_BUF_SZ;
1139                lp->rx_ring[entry].base |= 0x80000000;
1140                entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1141        }
1142
1143        /* We should check that at least two ring entries are free.      If not,
1144           we should free one and mark stats->rx_dropped++. */
1145
1146        return 0;
1147}
1148
1149static int
1150lance_close(struct net_device *dev)
1151{
1152        int ioaddr = dev->base_addr;
1153        struct lance_private *lp = dev->priv;
1154
1155        netif_stop_queue (dev);
1156
1157        if (chip_table[lp->chip_version].flags & LANCE_HAS_MISSED_FRAME) {
1158                outw(112, ioaddr+LANCE_ADDR);
1159                lp->stats.rx_missed_errors = inw(ioaddr+LANCE_DATA);
1160        }
1161        outw(0, ioaddr+LANCE_ADDR);
1162
1163        if (lance_debug > 1)
1164                printk("%s: Shutting down ethercard, status was %2.2x.\n",
1165                           dev->name, inw(ioaddr+LANCE_DATA));
1166
1167        /* We stop the LANCE here -- it occasionally polls
1168           memory if we don't. */
1169        outw(0x0004, ioaddr+LANCE_DATA);
1170
1171        if (dev->dma != 4)
1172        {
1173                unsigned long flags=claim_dma_lock();
1174                disable_dma(dev->dma);
1175                release_dma_lock(flags);
1176        }
1177        free_irq(dev->irq, dev);
1178
1179        lance_purge_ring(dev);
1180
1181        return 0;
1182}
1183
1184static struct net_device_stats *lance_get_stats(struct net_device *dev)
1185{
1186        struct lance_private *lp = dev->priv;
1187
1188        if (chip_table[lp->chip_version].flags & LANCE_HAS_MISSED_FRAME) {
1189                short ioaddr = dev->base_addr;
1190                short saved_addr;
1191                unsigned long flags;
1192
1193                spin_lock_irqsave(&lp->devlock, flags);
1194                saved_addr = inw(ioaddr+LANCE_ADDR);
1195                outw(112, ioaddr+LANCE_ADDR);
1196                lp->stats.rx_missed_errors = inw(ioaddr+LANCE_DATA);
1197                outw(saved_addr, ioaddr+LANCE_ADDR);
1198                spin_unlock_irqrestore(&lp->devlock, flags);
1199        }
1200
1201        return &lp->stats;
1202}
1203
1204/* Set or clear the multicast filter for this adaptor.
1205 */
1206
1207static void set_multicast_list(struct net_device *dev)
1208{
1209        short ioaddr = dev->base_addr;
1210
1211        outw(0, ioaddr+LANCE_ADDR);
1212        outw(0x0004, ioaddr+LANCE_DATA); /* Temporarily stop the lance.  */
1213
1214        if (dev->flags&IFF_PROMISC) {
1215                /* Log any net taps. */
1216                printk("%s: Promiscuous mode enabled.\n", dev->name);
1217                outw(15, ioaddr+LANCE_ADDR);
1218                outw(0x8000, ioaddr+LANCE_DATA); /* Set promiscuous mode */
1219        } else {
1220                short multicast_table[4];
1221                int i;
1222                int num_addrs=dev->mc_count;
1223                if(dev->flags&IFF_ALLMULTI)
1224                        num_addrs=1;
1225                /* FIXIT: We don't use the multicast table, but rely on upper-layer filtering. */
1226                memset(multicast_table, (num_addrs == 0) ? 0 : -1, sizeof(multicast_table));
1227                for (i = 0; i < 4; i++) {
1228                        outw(8 + i, ioaddr+LANCE_ADDR);
1229                        outw(multicast_table[i], ioaddr+LANCE_DATA);
1230                }
1231                outw(15, ioaddr+LANCE_ADDR);
1232                outw(0x0000, ioaddr+LANCE_DATA); /* Unset promiscuous mode */
1233        }
1234
1235        lance_restart(dev, 0x0142, 0); /*  Resume normal operation */
1236
1237}
1238
1239
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.