📄 lance.c
字号:
/* lance.c: An AMD LANCE/PCnet ethernet driver for Linux. *//* Written/copyright 1993-1998 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 General 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/NE2500) ethercards. The author may be reached as becker@scyld.com, or C/O Scyld Computing Corporation 410 Severn Ave., Suite 210 Annapolis MD 21403 Andrey V. Savochkin: - alignment problem with 1.3.* kernel and some minor changes. Thomas Bogendoerfer (tsbogend@bigbug.franken.de): - added support for Linux/Alpha, but removed most of it, because it worked only for the PCI chip. - added hook for the 32bit lance driver - added PCnetPCI II (79C970A) to chip table Paul Gortmaker (gpg109@rsphy1.anu.edu.au): - hopefully fix above so Linux/Alpha can use ISA cards too. 8/20/96 Fixed 7990 autoIRQ failure and reversed unneeded alignment -djb v1.12 10/27/97 Module support -djb v1.14 2/3/98 Module support modified, made PCI support optional -djb v1.15 5/27/99 Fixed bug in the cleanup_module(). dev->priv was freed before unregister_netdev() which caused NULL pointer reference later in the chain (in rtnetlink_fill_ifinfo()) -- Mika Kuoppala <miku@iki.fi> Forward ported v1.14 to 2.1.129, merged the PCI and misc changes from the 2.1 version of the old driver - Alan Cox Get rid of check_region, check kmalloc return in lance_probe1 Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 11/01/2001*/static const char version[] = "lance.c:v1.15ac 1999/11/13 dplatt@3do.com, becker@cesdis.gsfc.nasa.gov\n";#include <linux/module.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/slab.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <linux/init.h>#include <asm/bitops.h>#include <asm/io.h>#include <asm/dma.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>static unsigned int lance_portlist[] __initdata = { 0x300, 0x320, 0x340, 0x360, 0};int lance_probe(struct net_device *dev);static int lance_probe1(struct net_device *dev, int ioaddr, int irq, int options);#ifdef LANCE_DEBUGstatic int lance_debug = LANCE_DEBUG;#elsestatic int lance_debug = 1;#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, and newer 79C961. For convenience,I use the name LANCE to refer to all of the AMD chips, even though it properlyrefers only to the original 7990.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 ISA boards have jumpered settings for the I/O base, IRQ line, and DMAchannel. This driver probes the likely base addresses:{0x300, 0x320, 0x340, 0x360}.After the board is found it generates a DMA-timeout interrupt and usesautoIRQ to find the IRQ line. The DMA channel can be set with the low bitsof the otherwise-unused dev->mem_start value (aka PARAM1). If unset it isprobed for by enabling each free DMA channel in turn and checking ifinitialization succeeds.The HP-J2405A board is an exception: with this board it is easy to read theEEPROM-set values for the base, IRQ, and DMA. (Of course you must already_know_ the base address -- that field is for writing 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 a 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 beaccessible 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 16 Tx buffers, and 16 Rx buffers. That translates to 4 and 4 (16 == 2^^4). This is a compile-time option for efficiency. */#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#define TX_TIMEOUT 20/* The LANCE Rx and Tx ring descriptors. */struct lance_rx_head { s32 base; s16 buf_length; /* This length is 2s complement (negative)! */ s16 msg_length; /* This length is "normal". */};struct lance_tx_head { s32 base; s16 length; /* Length is 2s complement (negative)! */ s16 misc;};/* The LANCE initialization block, described in databook. */struct lance_init_block { u16 mode; /* Pre-set mode (reg. 15) */ u8 phys_addr[6]; /* Physical ethernet address */ u32 filter[2]; /* Multicast filter (unused). */ /* Receive and transmit ring base, along with extra bits. */ u32 rx_ring; /* Tx and Rx ring base pointers */ u32 tx_ring;};struct lance_private { /* The Tx and Rx ring entries must be 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; const char *name; /* The saved address of a sent-in-place packet/buffer, for skfree(). */ struct sk_buff* tx_skbuff[TX_RING_SIZE]; /* The addresses of receive-in-place skbuffs. */ struct sk_buff* rx_skbuff[RX_RING_SIZE]; unsigned 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 net_device_stats stats; unsigned char chip_version; /* See lance_chip_type. */ spinlock_t devlock;};#define LANCE_MUST_PAD 0x00000001#define LANCE_ENABLE_AUTOSELECT 0x00000002#define LANCE_MUST_REINIT_RING 0x00000004#define LANCE_MUST_UNRESET 0x00000008#define LANCE_HAS_MISSED_FRAME 0x00000010/* A mapping from the chip ID number to the part number and features. These are from the datasheets -- in real life the '970 version reportedly has the same ID as the '965. */static struct lance_chip_type { int id_number; const char *name; int flags;} chip_table[] = { {0x0000, "LANCE 7990", /* Ancient lance chip. */ LANCE_MUST_PAD + LANCE_MUST_UNRESET}, {0x0003, "PCnet/ISA 79C960", /* 79C960 PCnet/ISA. */ LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING + LANCE_HAS_MISSED_FRAME}, {0x2260, "PCnet/ISA+ 79C961", /* 79C961 PCnet/ISA+, Plug-n-Play. */ LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING + LANCE_HAS_MISSED_FRAME}, {0x2420, "PCnet/PCI 79C970", /* 79C970 or 79C974 PCnet-SCSI, PCI. */ LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING + LANCE_HAS_MISSED_FRAME}, /* Bug: the PCnet/PCI actually uses the PCnet/VLB ID number, so just call it the PCnet32. */ {0x2430, "PCnet32", /* 79C965 PCnet for VL bus. */ LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING + LANCE_HAS_MISSED_FRAME}, {0x2621, "PCnet/PCI-II 79C970A", /* 79C970A PCInetPCI II. */ LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING + LANCE_HAS_MISSED_FRAME}, {0x0, "PCnet (unknown)", LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING + LANCE_HAS_MISSED_FRAME},};enum {OLD_LANCE = 0, PCNET_ISA=1, PCNET_ISAP=2, PCNET_PCI=3, PCNET_VLB=4, PCNET_PCI_II=5, LANCE_UNKNOWN=6};/* Non-zero if lance_probe1() needs to allocate low-memory bounce buffers. Assume yes until we know the memory size. */static unsigned char lance_need_isa_bounce_buffers = 1;static int lance_open(struct net_device *dev);static int lance_open_fail(struct net_device *dev);static void lance_init_ring(struct net_device *dev, int mode);static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev);static int lance_rx(struct net_device *dev);static void lance_interrupt(int irq, void *dev_id, struct pt_regs *regs);static int lance_close(struct net_device *dev);static struct net_device_stats *lance_get_stats(struct net_device *dev);static void set_multicast_list(struct net_device *dev);static void lance_tx_timeout (struct net_device *dev);#ifdef MODULE#define MAX_CARDS 8 /* Max number of interfaces (cards) per module */static struct net_device dev_lance[MAX_CARDS];static int io[MAX_CARDS];static int dma[MAX_CARDS];static int irq[MAX_CARDS];MODULE_PARM(io, "1-" __MODULE_STRING(MAX_CARDS) "i");MODULE_PARM(dma, "1-" __MODULE_STRING(MAX_CARDS) "i");MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");MODULE_PARM(lance_debug, "i");MODULE_PARM_DESC(io, "LANCE/PCnet I/O base address(es),required");MODULE_PARM_DESC(dma, "LANCE/PCnet ISA DMA channel (ignored for some devices)");MODULE_PARM_DESC(irq, "LANCE/PCnet IRQ number (ignored for some devices)");MODULE_PARM_DESC(lance_debug, "LANCE/PCnet debug level (0-7)");int init_module(void){ int this_dev, found = 0; for (this_dev = 0; this_dev < MAX_CARDS; this_dev++) { struct net_device *dev = &dev_lance[this_dev]; dev->irq = irq[this_dev]; dev->base_addr = io[this_dev]; dev->dma = dma[this_dev]; dev->init = lance_probe; if (io[this_dev] == 0) { if (this_dev != 0) break; /* only complain once */ printk(KERN_NOTICE "lance.c: Module autoprobing not allowed. Append \"io=0xNNN\" value(s).\n"); return -EPERM; } if (register_netdev(dev) != 0) { printk(KERN_WARNING "lance.c: No PCnet/LANCE card found (i/o = 0x%x).\n", io[this_dev]); if (found != 0) return 0; /* Got at least one. */ return -ENXIO; } found++; } return 0;}void cleanup_module(void){ int this_dev; for (this_dev = 0; this_dev < MAX_CARDS; this_dev++) { struct net_device *dev = &dev_lance[this_dev]; if (dev->priv != NULL) { unregister_netdev(dev); free_dma(dev->dma); release_region(dev->base_addr, LANCE_TOTAL_SIZE); kfree(dev->priv); dev->priv = NULL; } }}#endif /* MODULE */MODULE_LICENSE("GPL");/* Starting in v2.1.*, the LANCE/PCnet probe is now similar to the other board probes now that kmalloc() can allocate ISA DMA-able regions. This also allows the LANCE driver to be used as a module. */int lance_probe(struct net_device *dev){ int *port, result; if (high_memory <= phys_to_virt(16*1024*1024)) lance_need_isa_bounce_buffers = 0; for (port = lance_portlist; *port; port++) { int ioaddr = *port; struct resource *r = request_region(ioaddr, LANCE_TOTAL_SIZE, "lance-probe"); if (r) { /* Detect "normal" 0x57 0x57 and the NI6510EB 0x52 0x44 signatures w/ minimal I/O reads */ char offset15, offset14 = inb(ioaddr + 14); if ((offset14 == 0x52 || offset14 == 0x57) && ((offset15 = inb(ioaddr + 15)) == 0x57 || offset15 == 0x44)) { result = lance_probe1(dev, ioaddr, 0, 0); if (!result) { struct lance_private *lp = dev->priv; int ver = lp->chip_version; r->name = chip_table[ver].name; return 0; } } release_resource(r); } } return -ENODEV;}static int __init lance_probe1(struct net_device *dev, int ioaddr, int irq, int options){ struct lance_private *lp; short dma_channels; /* Mark spuriously-busy DMA channels */ int i, reset_val, lance_version; const char *chipname; /* Flags for specific chips or boards. */ unsigned char hpJ2405A = 0; /* HP ISA adaptor */ int hp_builtin = 0; /* HP on-board ethernet. */ static int did_version; /* Already printed version info. */ unsigned long flags; /* First we look for special cases. Check for HP's on-board ethernet by looking for 'HP' in the BIOS. There are two HP versions, check the BIOS for the configuration port. This method provided by L. Julliard, Laurent_Julliard@grenoble.hp.com. */ if (isa_readw(0x000f0102) == 0x5048) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -