root/drivers/net/lance.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. lance_init
  2. lance_probe1
  3. lance_open
  4. lance_init_ring
  5. lance_start_xmit
  6. lance_interrupt
  7. lance_rx
  8. lance_close
  9. lance_get_stats
  10. set_multicast_list

   1 /* lance.c: An AMD LANCE ethernet driver for linux. */
   2 /*
   3     Written 1993 by Donald Becker.
   4 
   5     Copyright 1993 United States Government as represented by the
   6     Director, National Security Agency.  This software may be used and
   7     distributed according to the terms of the GNU Public License,
   8     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 clone) ethercards.
  12 
  13     The author may be reached as becker@super.org or
  14     C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  15 */
  16 
  17 static char *version = "lance.c:v0.14g 12/21/93 becker@super.org\n";
  18 
  19 #include <linux/config.h>
  20 #include <linux/kernel.h>
  21 #include <linux/sched.h>
  22 #include <linux/string.h>
  23 #include <linux/ptrace.h>
  24 #include <linux/errno.h>
  25 #include <linux/ioport.h>
  26 #include <linux/malloc.h>
  27 #include <linux/interrupt.h>
  28 #include <asm/bitops.h>
  29 #include <asm/io.h>
  30 #include <asm/dma.h>
  31 
  32 #include <linux/netdevice.h>
  33 #include <linux/etherdevice.h>
  34 #include <linux/skbuff.h>
  35 
  36 #ifndef HAVE_PORTRESERVE
  37 #define check_region(addr, size)        0
  38 #define snarf_region(addr, size)        do ; while(0)
  39 #endif
  40 
  41 struct device *init_etherdev(struct device *dev, int sizeof_private,
  42                              unsigned long *mem_startp);
  43 
  44 #ifdef LANCE_DEBUG
  45 int lance_debug = LANCE_DEBUG;
  46 #else
  47 int lance_debug = 1;
  48 #endif
  49 
  50 #ifndef LANCE_DMA
  51 #define LANCE_DMA       5
  52 #endif
  53 
  54 /*
  55                 Theory of Operation
  56 
  57 I. Board Compatibility
  58 
  59 This device driver is designed for the AMD 79C960, the "PCnet-ISA
  60 single-chip ethernet controller for ISA".  This chip is used in a wide
  61 variety of boards from vendors such as Allied Telesis, HP, Kingston,
  62 and Boca.  This driver is also intended to work with older AMD 7990
  63 designs, such as the NE1500 and NE2100.  For convenience, I use the name
  64 LANCE to refer to either AMD chip.
  65 
  66 II. Board-specific settings
  67 
  68 The driver is designed to work the boards that use the faster
  69 bus-master mode, rather than in shared memory mode.  (Only older designs
  70 have on-board buffer memory needed to support the slower shared memory mode.)
  71 
  72 Most boards have jumpered settings for the I/O base, IRQ line, and DMA channel.
  73 This driver probes the likely base addresses, {0x300, 0x320, 0x340, 0x360}.
  74 After the board is found it generates an DMA-timeout interrupt and uses
  75 autoIRQ to find the IRQ line.  The DMA channel defaults to LANCE_DMA, or it
  76 can be set with the low bits of the otherwise-unused dev->mem_start value.
  77 
  78 The HP-J2405A board is an exception: with this board it's easy to read the
  79 EEPROM-set values for the base, IRQ, and DMA.  Of course you must already
  80 _know_ the base address, but that entry is for changing the EEPROM.
  81 
  82 III. Driver operation
  83 
  84 IIIa. Ring buffers
  85 The LANCE uses ring buffers of Tx and Rx descriptors.  Each entry describes
  86 the base and length of the data buffer, along with status bits.  The length
  87 of these buffers is set by LANCE_LOG_{RX,TX}_BUFFERS, which is log_2() of
  88 the buffer length (rather than being directly the buffer length) for
  89 implementation ease.  The current values are 2 (Tx) and 4 (Rx), which leads to
  90 ring sizes of 4 (Tx) and 16 (Rx).  Increasing the number of ring entries
  91 needlessly uses extra space and reduces the chance that an upper layer will
  92 be able to reorder queued Tx packets based on priority.  Decreasing the number
  93 of entries makes it more difficult to achieve back-to-back packet transmission
  94 and increases the chance that Rx ring will overflow.  (Consider the worst case
  95 of receiving back-to-back minimum-sized packets.)
  96 
  97 The LANCE has the capability to "chain" both Rx and Tx buffers, but this driver
  98 statically allocates full-sized (slightly oversized -- PKT_BUF_SZ) buffers to
  99 avoid the administrative overhead. For the Rx side this avoids dynamically
 100 allocating full-sized buffers "just in case", at the expense of a
 101 memory-to-memory data copy for each packet received.  For most systems this
 102 is an good tradeoff: the Rx buffer will always be in low memory, the copy
 103 is inexpensive, and it primes the cache for later packet processing.  For Tx
 104 the buffers are only used when needed as low-memory bounce buffers.
 105 
 106 IIIB. 16M memory limitations.
 107 For the ISA bus master mode all structures used directly by the LANCE,
 108 the initialization block, Rx and Tx rings, and data buffers, must be
 109 accessable from the ISA bus, i.e. in the lower 16M of real memory.
 110 This is a problem for current Linux kernels on >16M machines. The network
 111 devices are initialized after memory initialization, and the kernel doles out
 112 memory from the top of memory downward.  The current solution is to have a
 113 special network initialization routine that's called before memory
 114 initialization; this will eventually be generalized for all network devices.
 115 As mentioned before, low-memory "bounce-buffers" are used when needed.
 116 
 117 IIIC. Synchronization
 118 The driver runs as two independent, single-threaded flows of control.  One
 119 is the send-packet routine, which enforces single-threaded use by the
 120 dev->tbusy flag.  The other thread is the interrupt handler, which is single
 121 threaded by the hardware and other software.
 122 
 123 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
 124 flag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
 125 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
 126 the 'lp->tx_full' flag.
 127 
 128 The interrupt handler has exclusive control over the Rx ring and records stats
 129 from the Tx ring.  (The Tx-done interrupt can't be selectively turned off, so
 130 we can't avoid the interrupt overhead by having the Tx routine reap the Tx
 131 stats.)  After reaping the stats, it marks the queue entry as empty by setting
 132 the 'base' to zero.  Iff the 'lp->tx_full' flag is set, it clears both the
 133 tx_full and tbusy flags.
 134 
 135 */
 136 
 137 /* Set the number of Tx and Rx buffers, using Log_2(# buffers).
 138    Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
 139    That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4). */
 140 #ifndef LANCE_LOG_TX_BUFFERS
 141 #define LANCE_LOG_TX_BUFFERS 4
 142 #define LANCE_LOG_RX_BUFFERS 4
 143 #endif
 144 
 145 #define TX_RING_SIZE            (1 << (LANCE_LOG_TX_BUFFERS))
 146 #define TX_RING_MOD_MASK        (TX_RING_SIZE - 1)
 147 #define TX_RING_LEN_BITS        ((LANCE_LOG_TX_BUFFERS) << 29)
 148 
 149 #define RX_RING_SIZE            (1 << (LANCE_LOG_RX_BUFFERS))
 150 #define RX_RING_MOD_MASK        (RX_RING_SIZE - 1)
 151 #define RX_RING_LEN_BITS        ((LANCE_LOG_RX_BUFFERS) << 29)
 152 
 153 #define PKT_BUF_SZ      1544
 154 
 155 /* Offsets from base I/O address. */
 156 #define LANCE_DATA 0x10
 157 #define LANCE_ADDR 0x12
 158 #define LANCE_RESET 0x14
 159 #define LANCE_BUS_IF 0x16
 160 #define LANCE_TOTAL_SIZE 0x18
 161 
 162 /* The LANCE Rx and Tx ring descriptors. */
 163 struct lance_rx_head {
 164     int base;
 165     short buf_length;           /* This length is 2's complement (negative)! */
 166     short msg_length;           /* This length is "normal". */
 167 };
 168 
 169 struct lance_tx_head {
 170     int   base;
 171     short length;               /* Length is 2's complement (negative)! */
 172     short misc;
 173 };
 174 
 175 /* The LANCE initialization block, described in databook. */
 176 struct lance_init_block {
 177     unsigned short mode;        /* Pre-set mode (reg. 15) */
 178     unsigned char phys_addr[6]; /* Physical ethernet address */
 179     unsigned filter[2];         /* Multicast filter (unused). */
 180     /* Receive and transmit ring base, along with extra bits. */
 181     unsigned rx_ring;           /* Tx and Rx ring base pointers */
 182     unsigned tx_ring;
 183 };
 184 
 185 struct lance_private {
 186     char devname[8];
 187     /* These must aligned on 8-byte boundaries. */
 188     struct lance_rx_head rx_ring[RX_RING_SIZE];
 189     struct lance_tx_head tx_ring[TX_RING_SIZE];
 190     struct lance_init_block     init_block;
 191     long rx_buffs;              /* Address of Rx and Tx buffers. */
 192     /* Tx low-memory "bounce buffer" address. */
 193     char (*tx_bounce_buffs)[PKT_BUF_SZ];
 194     int cur_rx, cur_tx;         /* The next free ring entry */
 195     int dirty_rx, dirty_tx;     /* The ring entries to be free()ed. */
 196     int dma;
 197     struct enet_statistics stats;
 198     char old_lance;
 199     char lock;
 200     int pad0, pad1;             /* Used for alignment */
 201 };
 202 
 203 unsigned long lance_probe1(short ioaddr, unsigned long mem_start);
 204 static int lance_open(struct device *dev);
 205 static void lance_init_ring(struct device *dev);
 206 static int lance_start_xmit(struct sk_buff *skb, struct device *dev);
 207 static int lance_rx(struct device *dev);
 208 static void lance_interrupt(int reg_ptr);
 209 static int lance_close(struct device *dev);
 210 static struct enet_statistics *lance_get_stats(struct device *dev);
 211 #ifdef HAVE_MULTICAST
 212 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 213 #endif
 214 
 215 
 216 
 217 unsigned long lance_init(unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219     int *port, ports[] = {0x300, 0x320, 0x340, 0x360, 0};
 220 
 221     for (port = &ports[0]; *port; port++) {
 222         int ioaddr = *port;
 223 
 224         if (   check_region(ioaddr, LANCE_TOTAL_SIZE) == 0
 225             && inb(ioaddr + 14) == 0x57
 226             && inb(ioaddr + 15) == 0x57) {
 227             mem_start = lance_probe1(ioaddr, mem_start);
 228         }
 229     }
 230 
 231     return mem_start;
 232 }
 233 
 234 unsigned long lance_probe1(short ioaddr, unsigned long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236     struct device *dev;
 237     struct lance_private *lp;
 238     int hpJ2405A = 0;
 239     int i, reset_val;
 240 
 241     hpJ2405A = (inb(ioaddr) == 0x08 && inb(ioaddr+1) == 0x00
 242                 && inb(ioaddr+2) == 0x09);
 243 
 244     /* Reset the LANCE.  */
 245     reset_val = inw(ioaddr+LANCE_RESET); /* Reset the LANCE */
 246 
 247     /* The Un-Reset needed is only needed for the real NE2100, and will
 248        confuse the HP board. */
 249     if (!hpJ2405A)
 250         outw(reset_val, ioaddr+LANCE_RESET);
 251 
 252     outw(0x0000, ioaddr+LANCE_ADDR); /* Switch to window 0 */
 253     if (inw(ioaddr+LANCE_DATA) != 0x0004)
 254         return mem_start;
 255 
 256     dev = init_etherdev(0, sizeof(struct lance_private)
 257                         + PKT_BUF_SZ*(RX_RING_SIZE + TX_RING_SIZE),
 258                         &mem_start);
 259 
 260     printk("%s: LANCE at %#3x,", dev->name, ioaddr);
 261 
 262     /* There is a 16 byte station address PROM at the base address.
 263        The first six bytes are the station address. */
 264     for (i = 0; i < 6; i++)
 265         printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
 266 
 267     dev->base_addr = ioaddr;
 268     snarf_region(ioaddr, LANCE_TOTAL_SIZE);
 269 
 270     /* Make certain the data structures used by the LANCE are aligned. */
 271     dev->priv = (void *)(((int)dev->priv + 7) & ~7);
 272     lp = (struct lance_private *)dev->priv;
 273     lp->rx_buffs = (long)dev->priv + sizeof(struct lance_private);
 274     lp->tx_bounce_buffs = (char (*)[PKT_BUF_SZ])
 275                            (lp->rx_buffs + PKT_BUF_SZ*RX_RING_SIZE);
 276 
 277 #ifndef final_version
 278     /* This should never happen. */
 279     if ((int)(lp->rx_ring) & 0x07) {
 280         printk(" **ERROR** LANCE Rx and Tx rings not on even boundary.\n");
 281         return mem_start;
 282     }
 283 #endif
 284 
 285     outw(88, ioaddr+LANCE_ADDR);
 286     lp->old_lance = (inw(ioaddr+LANCE_DATA) != 0x3003);
 287 
 288 #if defined(notdef)
 289     printk(lp->old_lance ? " original LANCE (%04x)" : " PCnet-ISA LANCE (%04x)",
 290            inw(ioaddr+LANCE_DATA));
 291 #endif
 292 
 293     lp->init_block.mode = 0x0003;       /* Disable Rx and Tx. */
 294     for (i = 0; i < 6; i++)
 295         lp->init_block.phys_addr[i] = dev->dev_addr[i];
 296     lp->init_block.filter[0] = 0x00000000;
 297     lp->init_block.filter[1] = 0x00000000;
 298     lp->init_block.rx_ring = (int)lp->rx_ring | RX_RING_LEN_BITS;
 299     lp->init_block.tx_ring = (int)lp->tx_ring | TX_RING_LEN_BITS;
 300 
 301     outw(0x0001, ioaddr+LANCE_ADDR);
 302     outw((short) (int) &lp->init_block, ioaddr+LANCE_DATA);
 303     outw(0x0002, ioaddr+LANCE_ADDR);
 304     outw(((int)&lp->init_block) >> 16, ioaddr+LANCE_DATA);
 305     outw(0x0000, ioaddr+LANCE_ADDR);
 306 
 307     if (hpJ2405A) {
 308         char dma_tbl[4] = {3, 5, 6, 7};
 309         char irq_tbl[8] = {3, 4, 5, 9, 10, 11, 12, 15};
 310         short reset_val = inw(ioaddr+LANCE_RESET);
 311         dev->dma = dma_tbl[(reset_val >> 2) & 3];
 312         dev->irq = irq_tbl[(reset_val >> 4) & 7];
 313         printk(" HP J2405A IRQ %d DMA %d.\n", dev->irq, dev->dma);
 314     } else {
 315         /* The DMA channel may be passed in on this parameter. */
 316         if (dev->mem_start & 0x07)
 317             dev->dma = dev->mem_start & 0x07;
 318         else if (dev->dma == 0)
 319             dev->dma = LANCE_DMA;
 320 
 321         /* To auto-IRQ we enable the initialization-done and DMA err,
 322            interrupts. For now we will always get a DMA error. */
 323         if (dev->irq < 2) {
 324 
 325             autoirq_setup(0);
 326 
 327             /* Trigger an initialization just for the interrupt. */
 328             outw(0x0041, ioaddr+LANCE_DATA);
 329 
 330             dev->irq = autoirq_report(1);
 331             if (dev->irq)
 332                 printk(", probed IRQ %d, fixed at DMA %d.\n",
 333                        dev->irq, dev->dma);
 334             else {
 335                 printk(", failed to detect IRQ line.\n");
 336                 return mem_start;
 337             }
 338         } else
 339             printk(" assigned IRQ %d DMA %d.\n", dev->irq, dev->dma);
 340     }
 341 
 342     if (! lp->old_lance) {
 343         /* Turn on auto-select of media (10baseT or BNC) so that the user
 344            can watch the LEDs even if the board isn't opened. */
 345         outw(0x0002, ioaddr+LANCE_ADDR);
 346         outw(0x0002, ioaddr+LANCE_BUS_IF);
 347     }
 348 
 349     if (lance_debug > 0)
 350         printk(version);
 351 
 352     /* The LANCE-specific entries in the device structure. */
 353     dev->open = &lance_open;
 354     dev->hard_start_xmit = &lance_start_xmit;
 355     dev->stop = &lance_close;
 356     dev->get_stats = &lance_get_stats;
 357     dev->set_multicast_list = &set_multicast_list;
 358 
 359     return mem_start;
 360 }
 361 
 362 
 363 static int
 364 lance_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 365 {
 366     struct lance_private *lp = (struct lance_private *)dev->priv;
 367     int ioaddr = dev->base_addr;
 368     int i;
 369 
 370     if (request_irq(dev->irq, &lance_interrupt)) {
 371         return -EAGAIN;
 372     }
 373 
 374     if (request_dma(dev->dma)) {
 375         free_irq(dev->irq);
 376         return -EAGAIN;
 377     }
 378     irq2dev_map[dev->irq] = dev;
 379 
 380     /* Reset the LANCE */
 381     inw(ioaddr+LANCE_RESET);
 382 
 383     /* The DMA controller is used as a no-operation slave, "cascade mode". */
 384     enable_dma(dev->dma);
 385     set_dma_mode(dev->dma, DMA_MODE_CASCADE);
 386 
 387     /* Un-Reset the LANCE, needed only for the NE2100. */
 388     if (lp->old_lance)
 389         outw(0, ioaddr+LANCE_RESET);
 390 
 391     if (! lp->old_lance) {
 392         /* This is 79C960-specific: Turn on auto-select of media (AUI, BNC). */
 393         outw(0x0002, ioaddr+LANCE_ADDR);
 394         outw(0x0002, ioaddr+LANCE_BUS_IF);
 395     }
 396 
 397     if (lance_debug > 1)
 398         printk("%s: lance_open() irq %d dma %d tx/rx rings %#x/%#x init %#x.\n",
 399                dev->name, dev->irq, dev->dma, (int) lp->tx_ring, (int) lp->rx_ring,
 400                (int) &lp->init_block);
 401 
 402     lance_init_ring(dev);
 403     /* Re-initialize the LANCE, and start it when done. */
 404     outw(0x0001, ioaddr+LANCE_ADDR);
 405     outw((short) (int) &lp->init_block, ioaddr+LANCE_DATA);
 406     outw(0x0002, ioaddr+LANCE_ADDR);
 407     outw(((int)&lp->init_block) >> 16, ioaddr+LANCE_DATA);
 408 
 409     outw(0x0004, ioaddr+LANCE_ADDR);
 410     outw(0x0d15, ioaddr+LANCE_DATA);
 411 
 412     outw(0x0000, ioaddr+LANCE_ADDR);
 413     outw(0x0001, ioaddr+LANCE_DATA);
 414 
 415     dev->tbusy = 0;
 416     dev->interrupt = 0;
 417     dev->start = 1;
 418     i = 0;
 419     while (i++ < 100)
 420         if (inw(ioaddr+LANCE_DATA) & 0x0100)
 421             break;
 422     outw(0x0142, ioaddr+LANCE_DATA);
 423 
 424     if (lance_debug > 2)
 425         printk("%s: LANCE open after %d ticks, init block %#x csr0 %4.4x.\n",
 426                dev->name, i, (int) &lp->init_block, inw(ioaddr+LANCE_DATA));
 427 
 428     return 0;                   /* Always succeed */
 429 }
 430 
 431 /* Initialize the LANCE Rx and Tx rings. */
 432 static void
 433 lance_init_ring(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 434 {
 435     struct lance_private *lp = (struct lance_private *)dev->priv;
 436     int i;
 437 
 438     lp->lock = 0;
 439     lp->cur_rx = lp->cur_tx = 0;
 440     lp->dirty_rx = lp->dirty_tx = 0;
 441 
 442     for (i = 0; i < RX_RING_SIZE; i++) {
 443         lp->rx_ring[i].base = (lp->rx_buffs + i*PKT_BUF_SZ) | 0x80000000;
 444         lp->rx_ring[i].buf_length = -PKT_BUF_SZ;
 445     }
 446     /* The Tx buffer address is filled in as needed, but we do need to clear
 447        the upper ownership bit. */
 448     for (i = 0; i < TX_RING_SIZE; i++) {
 449         lp->tx_ring[i].base = 0;
 450     }
 451 
 452     lp->init_block.mode = 0x0000;
 453     for (i = 0; i < 6; i++)
 454         lp->init_block.phys_addr[i] = dev->dev_addr[i];
 455     lp->init_block.filter[0] = 0x00000000;
 456     lp->init_block.filter[1] = 0x00000000;
 457     lp->init_block.rx_ring = (int)lp->rx_ring | RX_RING_LEN_BITS;
 458     lp->init_block.tx_ring = (int)lp->tx_ring | TX_RING_LEN_BITS;
 459 }
 460 
 461 static int
 462 lance_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 463 {
 464     struct lance_private *lp = (struct lance_private *)dev->priv;
 465     int ioaddr = dev->base_addr;
 466     int entry;
 467 
 468     /* Transmitter timeout, serious problems. */
 469     if (dev->tbusy) {
 470         int tickssofar = jiffies - dev->trans_start;
 471         if (tickssofar < 10)
 472             return 1;
 473         outw(0, ioaddr+LANCE_ADDR);
 474         printk("%s: transmit timed out, status %4.4x, resetting.\n",
 475                dev->name, inw(ioaddr+LANCE_DATA));
 476         outw(0x0001, ioaddr+LANCE_DATA);
 477         lp->stats.tx_errors++;
 478 #ifndef final_version
 479         {
 480             int i;
 481             printk(" Ring data dump: dirty_tx %d cur_tx %d cur_rx %d.",
 482                    lp->dirty_tx, lp->cur_tx, lp->cur_rx);
 483             for (i = 0 ; i < RX_RING_SIZE; i++)
 484                 printk("%s %08x %04x %04x", i & 0x3 ? "" : "\n ",
 485                        lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
 486                        lp->rx_ring[i].msg_length);
 487             for (i = 0 ; i < TX_RING_SIZE; i++)
 488                 printk(" %s%08x %04x %04x", i & 0x3 ? "" : "\n ",
 489                        lp->tx_ring[i].base, -lp->tx_ring[i].length,
 490                        lp->tx_ring[i].misc);
 491             printk("\n");
 492         }
 493 #endif
 494         lance_init_ring(dev);
 495         outw(0x0043, ioaddr+LANCE_DATA);
 496 
 497         dev->tbusy=0;
 498         dev->trans_start = jiffies;
 499 
 500         return 0;
 501     }
 502 
 503     if (skb == NULL) {
 504         dev_tint(dev);
 505         return 0;
 506     }
 507 
 508     if (skb->len <= 0)
 509         return 0;
 510 
 511     if (lance_debug > 3) {
 512         outw(0x0000, ioaddr+LANCE_ADDR);
 513         printk("%s: lance_start_xmit() called, csr0 %4.4x.\n", dev->name,
 514                inw(ioaddr+LANCE_DATA));
 515         outw(0x0000, ioaddr+LANCE_DATA);
 516     }
 517 
 518     /* Block a timer-based transmit from overlapping.  This could better be
 519        done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 520     if (set_bit(0, (void*)&dev->tbusy) != 0) {
 521         printk("%s: Transmitter access conflict.\n", dev->name);
 522         return 1;
 523     }
 524 
 525     if (set_bit(0, (void*)&lp->lock) != 0) {
 526         if (lance_debug > 2)
 527             printk("%s: tx queue lock!.\n", dev->name);
 528         /* don't clear dev->tbusy flag. */
 529         return 1;
 530     }
 531 
 532     /* Fill in a Tx ring entry */
 533 
 534     /* Mask to ring buffer boundary. */
 535     entry = lp->cur_tx & TX_RING_MOD_MASK;
 536 
 537     /* Caution: the write order is important here, set the base address
 538        with the "ownership" bits last. */
 539 
 540     /* The old LANCE chips doesn't automatically pad buffers to min. size. */
 541     if (lp->old_lance) {
 542         lp->tx_ring[entry].length =
 543             -(ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN);
 544     } else
 545         lp->tx_ring[entry].length = -skb->len;
 546 
 547     lp->tx_ring[entry].misc = 0x0000;
 548 
 549     /* If any part of this buffer is >16M we must copy it to a low-memory
 550        buffer. */
 551     if ((int)(skb->data) + skb->len > 0x01000000) {
 552         if (lance_debug > 5)
 553             printk("%s: bouncing a high-memory packet (%#x).\n",
 554                    dev->name, (int)(skb->data));
 555         memcpy(&lp->tx_bounce_buffs[entry], skb->data, skb->len);
 556         lp->tx_ring[entry].base =
 557             (int)(lp->tx_bounce_buffs + entry) | 0x83000000;
 558         if (skb->free)
 559             kfree_skb (skb, FREE_WRITE);
 560     } else {
 561         /* We can't free the packet yet, so we inform the memory management
 562            code that we are still using it. */
 563 
 564         skb_kept_by_device(skb);
 565 
 566         lp->tx_ring[entry].base = (int)(skb->data) | 0x83000000;
 567     }
 568     lp->cur_tx++;
 569 
 570     /* Trigger an immediate send poll. */
 571     outw(0x0000, ioaddr+LANCE_ADDR);
 572     outw(0x0048, ioaddr+LANCE_DATA);
 573 
 574     dev->trans_start = jiffies;
 575 
 576     cli();
 577     lp->lock = 0;
 578     if (lp->tx_ring[(entry+1) & TX_RING_MOD_MASK].base == 0)
 579         dev->tbusy=0;
 580     sti();
 581 
 582     return 0;
 583 }
 584 
 585 /* The LANCE interrupt handler. */
 586 static void
 587 lance_interrupt(int reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 588 {
 589     int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
 590     struct device *dev = (struct device *)(irq2dev_map[irq]);
 591     struct lance_private *lp;
 592     int csr0, ioaddr;
 593 
 594     if (dev == NULL) {
 595         printk ("lance_interrupt(): irq %d for unknown device.\n", irq);
 596         return;
 597     }
 598 
 599     ioaddr = dev->base_addr;
 600     lp = (struct lance_private *)dev->priv;
 601     if (dev->interrupt)
 602         printk("%s: Re-entering the interrupt handler.\n", dev->name);
 603 
 604     dev->interrupt = 1;
 605 
 606     outw(0x00, dev->base_addr + LANCE_ADDR);
 607     csr0 = inw(dev->base_addr + LANCE_DATA);
 608 
 609     /* Acknowledge all of the current interrupt sources ASAP. */
 610     outw(csr0 & ~0x004f, dev->base_addr + LANCE_DATA);
 611 
 612     if (lance_debug > 5)
 613         printk("%s: interrupt  csr0=%#2.2x new csr=%#2.2x.\n",
 614                dev->name, csr0, inw(dev->base_addr + LANCE_DATA));
 615 
 616     if (csr0 & 0x0400)          /* Rx interrupt */
 617         lance_rx(dev);
 618 
 619     if (csr0 & 0x0200) {        /* Tx-done interrupt */
 620         int dirty_tx = lp->dirty_tx;
 621 
 622         while (dirty_tx < lp->cur_tx) {
 623             int entry = dirty_tx & TX_RING_MOD_MASK;
 624             int status = lp->tx_ring[entry].base;
 625             void *databuff;
 626             
 627             if (status < 0)
 628                 break;          /* It still hasn't been Txed */
 629 
 630             lp->tx_ring[entry].base = 0;
 631             databuff = (void*)(status & 0x00ffffff);
 632 
 633             if (status & 0x40000000) { /* There was an major error, log it. */
 634                 int err_status = lp->tx_ring[entry].misc;
 635                 lp->stats.tx_errors++;
 636                 if (err_status & 0x0400) lp->stats.tx_aborted_errors++;
 637                 if (err_status & 0x0800) lp->stats.tx_carrier_errors++;
 638                 if (err_status & 0x1000) lp->stats.tx_window_errors++;
 639                 if (err_status & 0x4000) lp->stats.tx_fifo_errors++;
 640                 /* Perhaps we should re-init() after the FIFO error. */
 641             } else {
 642                 if (status & 0x18000000)
 643                     lp->stats.collisions++;
 644                 lp->stats.tx_packets++;
 645             }
 646 
 647             /* We don't free the skb if it's a data-only copy in the bounce
 648                buffer.  The address checks here are sorted -- the first test
 649                should always work.  */
 650             if (databuff >= (void*)(&lp->tx_bounce_buffs[TX_RING_SIZE])
 651                 || databuff < (void*)(lp->tx_bounce_buffs)) {
 652                 struct sk_buff *skb = ((struct sk_buff *)databuff) - 1;
 653                 skb_device_release(skb,FREE_WRITE);
 654 
 655                 /* Warning: skb may well vanish at the point you call
 656                    device_release! */
 657             }
 658             dirty_tx++;
 659         }
 660 
 661 #ifndef final_version
 662         if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
 663             printk("out-of-sync dirty pointer, %d vs. %d.\n",
 664                    dirty_tx, lp->cur_tx);
 665             dirty_tx += TX_RING_SIZE;
 666         }
 667 #endif
 668 
 669         if (dev->tbusy  &&  dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
 670             /* The ring is no longer full, clear tbusy. */
 671             dev->tbusy = 0;
 672             mark_bh(NET_BH);
 673         }
 674 
 675         lp->dirty_tx = dirty_tx;
 676     }
 677 
 678     if (csr0 & 0x8000) {
 679         if (csr0 & 0x4000) lp->stats.tx_errors++;
 680         if (csr0 & 0x1000) lp->stats.rx_errors++;
 681     }
 682 
 683     /* Clear the interrupts we've handled. */
 684     outw(0x0000, dev->base_addr + LANCE_ADDR);
 685     outw(0x7f40, dev->base_addr + LANCE_DATA);
 686 
 687     if (lance_debug > 4)
 688         printk("%s: exiting interrupt, csr%d=%#4.4x.\n",
 689                dev->name, inw(ioaddr + LANCE_ADDR),
 690                inw(dev->base_addr + LANCE_DATA));
 691 
 692     dev->interrupt = 0;
 693     return;
 694 }
 695 
 696 static int
 697 lance_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 698 {
 699     struct lance_private *lp = (struct lance_private *)dev->priv;
 700     int entry = lp->cur_rx & RX_RING_MOD_MASK;
 701     int i;
 702         
 703     /* If we own the next entry, it's a new packet. Send it up. */
 704     while (lp->rx_ring[entry].base >= 0) {
 705         int status = lp->rx_ring[entry].base >> 24;
 706 
 707         if (status != 0x03) {           /* There was an error. */
 708             /* There is an tricky error noted by John Murphy,
 709                <murf@perftech.com> to Russ Nelson: Even with full-sized
 710                buffers it's possible for a jabber packet to use two
 711                buffers, with only the last correctly noting the error. */
 712             if (status & 0x01)  /* Only count a general error at the */
 713                 lp->stats.rx_errors++; /* end of a packet.*/
 714             if (status & 0x20) lp->stats.rx_frame_errors++;
 715             if (status & 0x10) lp->stats.rx_over_errors++;
 716             if (status & 0x08) lp->stats.rx_crc_errors++;
 717             if (status & 0x04) lp->stats.rx_fifo_errors++;
 718             lp->rx_ring[entry].base &= 0x03ffffff;
 719         } else {
 720             /* Malloc up new buffer, compatible with net-2e. */
 721             short pkt_len = lp->rx_ring[entry].msg_length;
 722             struct sk_buff *skb;
 723 
 724             skb = alloc_skb(pkt_len, GFP_ATOMIC);
 725             if (skb == NULL) {
 726                 printk("%s: Memory squeeze, deferring packet.\n", dev->name);
 727                 for (i=0; i < RX_RING_SIZE; i++)
 728                   if (lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].base < 0)
 729                     break;
 730 
 731                 if (i > RX_RING_SIZE -2) {
 732                   lp->stats.rx_dropped++;
 733                   lp->rx_ring[entry].base |= 0x80000000;
 734                   lp->cur_rx++;
 735                 }
 736                 break;
 737             }
 738             skb->len = pkt_len;
 739             skb->dev = dev;
 740             memcpy(skb->data,
 741                    (unsigned char *)(lp->rx_ring[entry].base & 0x00ffffff),
 742                    pkt_len);
 743             netif_rx(skb);
 744             lp->stats.rx_packets++;
 745         }
 746 
 747         lp->rx_ring[entry].base |= 0x80000000;
 748         entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
 749     }
 750 
 751     /* We should check that at least two ring entries are free.  If not,
 752        we should free one and mark stats->rx_dropped++. */
 753 
 754     return 0;
 755 }
 756 
 757 static int
 758 lance_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 759 {
 760     int ioaddr = dev->base_addr;
 761     struct lance_private *lp = (struct lance_private *)dev->priv;
 762 
 763     dev->start = 0;
 764     dev->tbusy = 1;
 765 
 766     outw(112, ioaddr+LANCE_ADDR);
 767     lp->stats.rx_missed_errors = inw(ioaddr+LANCE_DATA);
 768 
 769     outw(0, ioaddr+LANCE_ADDR);
 770 
 771     if (lance_debug > 1)
 772         printk("%s: Shutting down ethercard, status was %2.2x.\n",
 773                dev->name, inw(ioaddr+LANCE_DATA));
 774 
 775     /* We stop the LANCE here -- it occasionally polls
 776        memory if we don't. */
 777     outw(0x0004, ioaddr+LANCE_DATA);
 778 
 779     disable_dma(dev->dma);
 780 
 781     free_irq(dev->irq);
 782     free_dma(dev->dma);
 783 
 784     irq2dev_map[dev->irq] = 0;
 785 
 786     return 0;
 787 }
 788 
 789 static struct enet_statistics *
 790 lance_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 791 {
 792     struct lance_private *lp = (struct lance_private *)dev->priv;
 793     short ioaddr = dev->base_addr;
 794     short saved_addr;
 795 
 796     cli();
 797     saved_addr = inw(ioaddr+LANCE_ADDR);
 798     outw(112, ioaddr+LANCE_ADDR);
 799     lp->stats.rx_missed_errors = inw(ioaddr+LANCE_DATA);
 800     outw(saved_addr, ioaddr+LANCE_ADDR);
 801     sti();
 802 
 803     return &lp->stats;
 804 }
 805 
 806 /* Set or clear the multicast filter for this adaptor.
 807    num_addrs == -1      Promiscuous mode, receive all packets
 808    num_addrs == 0       Normal mode, clear multicast list
 809    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
 810                         best-effort filtering.
 811  */
 812 static void
 813 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 814 {
 815     short ioaddr = dev->base_addr;
 816 
 817     /* We take the simple way out and always enable promiscuous mode. */
 818     outw(0, ioaddr+LANCE_ADDR);
 819     outw(0x0004, ioaddr+LANCE_DATA); /* Temporarily stop the lance.  */
 820 
 821     outw(15, ioaddr+LANCE_ADDR);
 822     if (num_addrs >= 0) {
 823         short multicast_table[4];
 824         int i;
 825         /* We don't use the multicast table, but rely on upper-layer filtering. */
 826         memset(multicast_table, (num_addrs == 0) ? 0 : -1, sizeof(multicast_table));
 827         for (i = 0; i < 4; i++) {
 828             outw(8 + i, ioaddr+LANCE_ADDR);
 829             outw(multicast_table[i], ioaddr+LANCE_DATA);
 830         }
 831         outw(0x0000, ioaddr+LANCE_DATA); /* Unset promiscuous mode */
 832     } else {
 833         outw(0x8000, ioaddr+LANCE_DATA); /* Set promiscuous mode */
 834     }
 835 
 836     outw(0, ioaddr+LANCE_ADDR);
 837     outw(0x0142, ioaddr+LANCE_DATA); /* Resume normal operation. */
 838 }
 839 
 840 #ifdef HAVE_DEVLIST
 841 static unsigned int lance_portlist[] = {0x300, 0x320, 0x340, 0x360, 0};
 842 struct netdev_entry lance_drv =
 843 {"lance", lance_probe1, LANCE_TOTAL_SIZE, lance_portlist};
 844 #endif
 845 
 846 /*
 847  * Local variables:
 848  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c lance.c"
 849  * End:
 850  */

/* [previous][next][first][last][top][bottom][index][help] */