📄 lance.c
字号:
/* lance.c: An AMD LANCE ethernet driver for linux. *//* Written 1993 by Donald Becker. Copyright 1993 United States Government as represented by the Director, National Security Agency. This software may be used and distributed according to the terms of the GNU Public License, incorporated herein by reference. This driver is for the Allied Telesis AT1500 and HP J2405A, and should work with most other LANCE-based bus-master (NE2100 clone) ethercards. The author may be reached as becker@super.org or C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715*/static char *version = "lance.c:v0.14g 12/21/93 becker@super.org\n";#include <linux/config.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/string.h>#include <linux/ptrace.h>#include <linux/errno.h>#include <linux/ioport.h>#include <linux/malloc.h>#include <linux/interrupt.h>#include <asm/bitops.h>#include <asm/io.h>#include <asm/dma.h>#include "dev.h"#include "eth.h"#include "skbuff.h"#include "arp.h"#ifndef HAVE_PORTRESERVE#define check_region(addr, size) 0#define snarf_region(addr, size) do ; while(0)#endif#ifndef HAVE_ALLOC_SKB#define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)#define kfree_skbmem(buff, size) kfree_s(buff,size)#endifstruct device *init_etherdev(struct device *dev, int sizeof_private, unsigned long *mem_startp);#ifdef LANCE_DEBUGint lance_debug = LANCE_DEBUG;#elseint lance_debug = 1;#endif#ifndef LANCE_DMA#define LANCE_DMA 5#endif/* Theory of OperationI. Board CompatibilityThis device driver is designed for the AMD 79C960, the "PCnet-ISAsingle-chip ethernet controller for ISA". This chip is used in a widevariety of boards from vendors such as Allied Telesis, HP, Kingston,and Boca. This driver is also intended to work with older AMD 7990designs, such as the NE1500 and NE2100. For convenience, I use the nameLANCE to refer to either AMD chip.II. Board-specific settingsThe driver is designed to work the boards that use the fasterbus-master mode, rather than in shared memory mode. (Only older designshave on-board buffer memory needed to support the slower shared memory mode.)Most boards have jumpered settings for the I/O base, IRQ line, and DMA channel.This driver probes the likely base addresses, {0x300, 0x320, 0x340, 0x360}.After the board is found it generates an DMA-timeout interrupt and usesautoIRQ to find the IRQ line. The DMA channel defaults to LANCE_DMA, or itcan be set with the low bits of the otherwise-unused dev->mem_start value.The HP-J2405A board is an exception: with this board it's easy to read theEEPROM-set values for the base, IRQ, and DMA. Of course you must already_know_ the base address, but that entry is for changing the EEPROM.III. Driver operationIIIa. Ring buffersThe LANCE uses ring buffers of Tx and Rx descriptors. Each entry describesthe base and length of the data buffer, along with status bits. The lengthof these buffers is set by LANCE_LOG_{RX,TX}_BUFFERS, which is log_2() ofthe buffer length (rather than being directly the buffer length) forimplementation ease. The current values are 2 (Tx) and 4 (Rx), which leads toring sizes of 4 (Tx) and 16 (Rx). Increasing the number of ring entriesneedlessly uses extra space and reduces the chance that an upper layer willbe able to reorder queued Tx packets based on priority. Decreasing the numberof entries makes it more difficult to achieve back-to-back packet transmissionand increases the chance that Rx ring will overflow. (Consider the worst caseof receiving back-to-back minimum-sized packets.)The LANCE has the capability to "chain" both Rx and Tx buffers, but this driverstatically allocates full-sized (slightly oversized -- PKT_BUF_SZ) buffers toavoid the administrative overhead. For the Rx side this avoids dynamicallyallocating full-sized buffers "just in case", at the expense of amemory-to-memory data copy for each packet received. For most systems thisis an good tradeoff: the Rx buffer will always be in low memory, the copyis inexpensive, and it primes the cache for later packet processing. For Txthe buffers are only used when needed as low-memory bounce buffers.IIIB. 16M memory limitations.For the ISA bus master mode all structures used directly by the LANCE,the initialization block, Rx and Tx rings, and data buffers, must beaccessable from the ISA bus, i.e. in the lower 16M of real memory.This is a problem for current Linux kernels on >16M machines. The networkdevices are initialized after memory initialization, and the kernel doles outmemory from the top of memory downward. The current solution is to have aspecial network initialization routine that's called before memoryinitialization; this will eventually be generalized for all network devices.As mentioned before, low-memory "bounce-buffers" are used when needed.IIIC. SynchronizationThe driver runs as two independent, single-threaded flows of control. Oneis the send-packet routine, which enforces single-threaded use by thedev->tbusy flag. The other thread is the interrupt handler, which is singlethreaded by the hardware and other software.The send packet thread has partial control over the Tx ring and 'dev->tbusy'flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the nextqueue slot is empty, it clears the tbusy flag when finished otherwise it setsthe 'lp->tx_full' flag.The interrupt handler has exclusive control over the Rx ring and records statsfrom the Tx ring. (The Tx-done interrupt can't be selectively turned off, sowe can't avoid the interrupt overhead by having the Tx routine reap the Txstats.) After reaping the stats, it marks the queue entry as empty by settingthe 'base' to zero. Iff the 'lp->tx_full' flag is set, it clears both thetx_full and tbusy flags.*//* Set the number of Tx and Rx buffers, using Log_2(# buffers). Reasonable default values are 4 Tx buffers, and 16 Rx buffers. That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4). */#ifndef LANCE_LOG_TX_BUFFERS#define LANCE_LOG_TX_BUFFERS 4#define LANCE_LOG_RX_BUFFERS 4#endif#define TX_RING_SIZE (1 << (LANCE_LOG_TX_BUFFERS))#define TX_RING_MOD_MASK (TX_RING_SIZE - 1)#define TX_RING_LEN_BITS ((LANCE_LOG_TX_BUFFERS) << 29)#define RX_RING_SIZE (1 << (LANCE_LOG_RX_BUFFERS))#define RX_RING_MOD_MASK (RX_RING_SIZE - 1)#define RX_RING_LEN_BITS ((LANCE_LOG_RX_BUFFERS) << 29)#define PKT_BUF_SZ 1544/* Offsets from base I/O address. */#define LANCE_DATA 0x10#define LANCE_ADDR 0x12#define LANCE_RESET 0x14#define LANCE_BUS_IF 0x16#define LANCE_TOTAL_SIZE 0x18/* The LANCE Rx and Tx ring descriptors. */struct lance_rx_head { int base; short buf_length; /* This length is 2's complement (negative)! */ short msg_length; /* This length is "normal". */};struct lance_tx_head { int base; short length; /* Length is 2's complement (negative)! */ short misc;};/* The LANCE initialization block, described in databook. */struct lance_init_block { unsigned short mode; /* Pre-set mode (reg. 15) */ unsigned char phys_addr[6]; /* Physical ethernet address */ unsigned filter[2]; /* Multicast filter (unused). */ /* Receive and transmit ring base, along with extra bits. */ unsigned rx_ring; /* Tx and Rx ring base pointers */ unsigned tx_ring;};struct lance_private { char devname[8]; /* These must aligned on 8-byte boundaries. */ struct lance_rx_head rx_ring[RX_RING_SIZE]; struct lance_tx_head tx_ring[TX_RING_SIZE]; struct lance_init_block init_block; long rx_buffs; /* Address of Rx and Tx buffers. */ /* Tx low-memory "bounce buffer" address. */ char (*tx_bounce_buffs)[PKT_BUF_SZ]; int cur_rx, cur_tx; /* The next free ring entry */ int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */ int dma; struct enet_statistics stats; char old_lance; int pad0, pad1; /* Used for alignment */};unsigned long lance_probe1(short ioaddr, unsigned long mem_start);static int lance_open(struct device *dev);static void lance_init_ring(struct device *dev);static int lance_start_xmit(struct sk_buff *skb, struct device *dev);static int lance_rx(struct device *dev);static void lance_interrupt(int reg_ptr);static int lance_close(struct device *dev);static struct enet_statistics *lance_get_stats(struct device *dev);#ifdef HAVE_MULTICASTstatic void set_multicast_list(struct device *dev, int num_addrs, void *addrs);#endifunsigned long lance_init(unsigned long mem_start, unsigned long mem_end){ int *port, ports[] = {0x300, 0x320, 0x340, 0x360, 0}; for (port = &ports[0]; *port; port++) { int ioaddr = *port; if ( check_region(ioaddr, LANCE_TOTAL_SIZE) == 0 && inb(ioaddr + 14) == 0x57 && inb(ioaddr + 15) == 0x57) { mem_start = lance_probe1(ioaddr, mem_start); } } return mem_start;}unsigned long lance_probe1(short ioaddr, unsigned long mem_start){ struct device *dev; struct lance_private *lp; int hpJ2405A = 0; int i, reset_val; hpJ2405A = (inb(ioaddr) == 0x08 && inb(ioaddr+1) == 0x00 && inb(ioaddr+2) == 0x09); /* Reset the LANCE. */ reset_val = inw(ioaddr+LANCE_RESET); /* Reset the LANCE */ /* The Un-Reset needed is only needed for the real NE2100, and will confuse the HP board. */ if (!hpJ2405A) outw(reset_val, ioaddr+LANCE_RESET); outw(0x0000, ioaddr+LANCE_ADDR); /* Switch to window 0 */ if (inw(ioaddr+LANCE_DATA) != 0x0004) return mem_start; dev = init_etherdev(0, sizeof(struct lance_private) + PKT_BUF_SZ*(RX_RING_SIZE + TX_RING_SIZE), &mem_start); printk("%s: LANCE at %#3x,", dev->name, ioaddr); /* There is a 16 byte station address PROM at the base address. The first six bytes are the station address. */ for (i = 0; i < 6; i++) printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i)); dev->base_addr = ioaddr; snarf_region(ioaddr, LANCE_TOTAL_SIZE); /* Make certain the data structures used by the LANCE are aligned. */ dev->priv = (void *)(((int)dev->priv + 7) & ~7); lp = (struct lance_private *)dev->priv; lp->rx_buffs = (long)dev->priv + sizeof(struct lance_private); lp->tx_bounce_buffs = (char (*)[PKT_BUF_SZ]) (lp->rx_buffs + PKT_BUF_SZ*RX_RING_SIZE);#ifndef final_version /* This should never happen. */ if ((int)(lp->rx_ring) & 0x07) { printk(" **ERROR** LANCE Rx and Tx rings not on even boundary.\n"); return mem_start; }#endif outw(88, ioaddr+LANCE_ADDR); lp->old_lance = (inw(ioaddr+LANCE_DATA) != 0x3003);#if defined(notdef) printk(lp->old_lance ? " original LANCE (%04x)" : " PCnet-ISA LANCE (%04x)", inw(ioaddr+LANCE_DATA));#endif lp->init_block.mode = 0x0003; /* Disable Rx and Tx. */ for (i = 0; i < 6; i++) lp->init_block.phys_addr[i] = dev->dev_addr[i]; lp->init_block.filter[0] = 0x00000000; lp->init_block.filter[1] = 0x00000000; lp->init_block.rx_ring = (int)lp->rx_ring | RX_RING_LEN_BITS; lp->init_block.tx_ring = (int)lp->tx_ring | TX_RING_LEN_BITS; outw(0x0001, ioaddr+LANCE_ADDR); outw((short) (int) &lp->init_block, ioaddr+LANCE_DATA); outw(0x0002, ioaddr+LANCE_ADDR); outw(((int)&lp->init_block) >> 16, ioaddr+LANCE_DATA); outw(0x0000, ioaddr+LANCE_ADDR); if (hpJ2405A) { char dma_tbl[4] = {3, 5, 6, 7}; char irq_tbl[8] = {3, 4, 5, 9, 10, 11, 12, 15}; short reset_val = inw(ioaddr+LANCE_RESET); dev->dma = dma_tbl[(reset_val >> 2) & 3]; dev->irq = irq_tbl[(reset_val >> 4) & 7]; printk(" HP J2405A IRQ %d DMA %d.\n", dev->irq, dev->dma); } else { /* The DMA channel may be passed in on this parameter. */ if (dev->mem_start & 0x07) dev->dma = dev->mem_start & 0x07; else if (dev->dma == 0) dev->dma = LANCE_DMA; /* To auto-IRQ we enable the initialization-done and DMA err, interrupts. For now we will always get a DMA error. */ if (dev->irq < 2) { autoirq_setup(0); /* Trigger an initialization just for the interrupt. */ outw(0x0041, ioaddr+LANCE_DATA); dev->irq = autoirq_report(1); if (dev->irq) printk(", probed IRQ %d, fixed at DMA %d.\n", dev->irq, dev->dma); else { printk(", failed to detect IRQ line.\n"); return mem_start; } } else printk(" assigned IRQ %d DMA %d.\n", dev->irq, dev->dma); } if (! lp->old_lance) { /* Turn on auto-select of media (10baseT or BNC) so that the user can watch the LEDs even if the board isn't opened. */ outw(0x0002, ioaddr+LANCE_ADDR); outw(0x0002, ioaddr+LANCE_BUS_IF); } if (lance_debug > 0) printk(version); /* The LANCE-specific entries in the device structure. */ dev->open = &lance_open; dev->hard_start_xmit = &lance_start_xmit; dev->stop = &lance_close; dev->get_stats = &lance_get_stats;#ifdef HAVE_MULTICAST dev->set_multicast_list = &set_multicast_list;#endif return mem_start;}static intlance_open(struct device *dev){ struct lance_private *lp = (struct lance_private *)dev->priv; int ioaddr = dev->base_addr; int i; if (request_irq(dev->irq, &lance_interrupt)) { return -EAGAIN; } if (request_dma(dev->dma)) { free_irq(dev->irq); return -EAGAIN; } irq2dev_map[dev->irq] = dev; /* Reset the LANCE */ inw(ioaddr+LANCE_RESET); /* The DMA controller is used as a no-operation slave, "cascade mode". */ enable_dma(dev->dma); set_dma_mode(dev->dma, DMA_MODE_CASCADE); /* Un-Reset the LANCE, needed only for the NE2100. */ if (lp->old_lance) outw(0, ioaddr+LANCE_RESET); if (! lp->old_lance) { /* This is 79C960-specific: Turn on auto-select of media (AUI, BNC). */ outw(0x0002, ioaddr+LANCE_ADDR); outw(0x0002, ioaddr+LANCE_BUS_IF); } if (lance_debug > 1) printk("%s: lance_open() irq %d dma %d tx/rx rings %#x/%#x init %#x.\n", dev->name, dev->irq, dev->dma, (int) lp->tx_ring, (int) lp->rx_ring, (int) &lp->init_block); lance_init_ring(dev); /* Re-initialize the LANCE, and start it when done. */ outw(0x0001, ioaddr+LANCE_ADDR); outw((short) (int) &lp->init_block, ioaddr+LANCE_DATA); outw(0x0002, ioaddr+LANCE_ADDR); outw(((int)&lp->init_block) >> 16, ioaddr+LANCE_DATA); outw(0x0004, ioaddr+LANCE_ADDR); outw(0x0d15, ioaddr+LANCE_DATA); outw(0x0000, ioaddr+LANCE_ADDR); outw(0x0001, ioaddr+LANCE_DATA); dev->tbusy = 0; dev->interrupt = 0; dev->start = 1; i = 0; while (i++ < 100) if (inw(ioaddr+LANCE_DATA) & 0x0100) break; outw(0x0142, ioaddr+LANCE_DATA);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -