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

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