📄 sundance.c
字号:
/* sundance.c: A Linux device driver for the Sundance ST201 "Alta". *//* Written 1999-2003 by Donald Becker. This software may be used and distributed according to the terms of the GNU General Public License (GPL), incorporated herein by reference. Drivers based on or derived from this code fall under the GPL and must retain the authorship, copyright and license notice. This file is not a complete program and may only be used when the entire operating system is licensed under the GPL. The author may be reached as becker@scyld.com, or C/O Scyld Computing Corporation 410 Severn Ave., Suite 210 Annapolis MD 21403 Support information and updates available at http://www.scyld.com/network/sundance.html*//* These identify the driver base version and may not be removed. */static const char version1[] ="sundance.c:v1.11 2/4/2003 Written by Donald Becker <becker@scyld.com>\n";static const char version2[] =" http://www.scyld.com/network/sundance.html\n";/* Updated to recommendations in pci-skeleton v2.12. *//* Automatically extracted configuration info:probe-func: sundance_probeconfig-in: tristate 'Sundance ST201 "Alta" PCI Ethernet support' CONFIG_SUNDANCEc-help-name: Sundance ST201 "Alta" PCI Ethernet supportc-help-symbol: CONFIG_SUNDANCEc-help: This driver is for the Sundance ST201 "Alta" and Kendin KS8723, asc-help: used on the D-Link DFE-550 and DFE-580.c-help: Design information, usage details and updates are available fromc-help: http://www.scyld.com/network/sundance.html*//* The user-configurable values. These may be modified when a driver module is loaded.*//* Message enable level: 0..31 = no..all messages. See NETIF_MSG docs. */static int debug = 2;/* Maximum events (Rx packets, etc.) to handle at each interrupt. */static int max_interrupt_work = 20;/* Maximum number of multicast addresses to filter (vs. rx-all-multicast). The sundance uses a 64 element hash table based on the Ethernet CRC. */static int multicast_filter_limit = 32;/* Set the copy breakpoint for the copy-only-tiny-frames scheme. Setting to > 1518 effectively disables this feature. This chip can receive into any byte alignment buffers, so word-oriented archs do not need a copy-align of the IP header. */static int rx_copybreak = 0;/* Used to pass the media type, etc. Both 'options[]' and 'full_duplex[]' should exist for driver interoperability. The media type is usually passed in 'options[]'. The default is autonegotation for speed and duplex. This should rarely be overridden. Use option values 0x10/0x20 for 10Mbps, 0x100,0x200 for 100Mbps. Use option values 0x10 and 0x100 for forcing half duplex fixed speed. Use option values 0x20 and 0x200 for forcing full duplex operation.*/#define MAX_UNITS 8 /* More are supported, limit only on options */static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};/* Operational parameters that are set at compile time. *//* Ring sizes are a power of two only for compile efficiency. The compiler will convert <unsigned>'%'<2^N> into a bit mask. There must be at least five Tx entries for the tx_full hysteresis, and more than 31 requires modifying the Tx status handling error recovery. Leave a inactive gap in the Tx ring for better cache behavior. Making the Tx ring too large decreases the effectiveness of channel bonding and packet priority. Large receive rings waste memory and impact buffer accounting. The driver need to protect against interrupt latency and the kernel not reserving enough available memory.*/#define TX_RING_SIZE 16#define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */#define RX_RING_SIZE 32/* Operational parameters that usually are not changed. *//* Time in jiffies before concluding the transmitter is hung. */#define TX_TIMEOUT (6*HZ)/* Allocation size of Rx buffers with normal sized Ethernet frames. Do not change this value without good reason. This is not a limit, but a way to keep a consistent allocation size among drivers. */#define PKT_BUF_SZ 1536/* Set iff a MII transceiver on any interface requires mdio preamble. This only set with older tranceivers, so the extra code size of a per-interface flag is not worthwhile. */static char mii_preamble_required = 0;#ifndef __KERNEL__#define __KERNEL__#endif#if !defined(__OPTIMIZE__)#warning You must compile this file with the correct options!#warning See the last lines of the source file.#error You must compile this driver with "-O".#endif/* Include files, designed to support most kernel versions 2.0.0 and later. */#include <linux/config.h>#if defined(CONFIG_SMP) && ! defined(__SMP__)#define __SMP__#endif#if defined(MODULE) && defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)#define MODVERSIONS#endif#include <linux/version.h>#if defined(MODVERSIONS)#include <linux/modversions.h>#endif#include <linux/module.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/errno.h>#include <linux/ioport.h>#if LINUX_VERSION_CODE >= 0x20400#include <linux/slab.h>#else#include <linux/malloc.h>#endif#include <linux/interrupt.h>#include <linux/pci.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <asm/bitops.h>#include <asm/io.h>#if LINUX_VERSION_CODE >= 0x20300#include <linux/spinlock.h>#elif LINUX_VERSION_CODE >= 0x20200#include <asm/spinlock.h>#endif#ifdef INLINE_PCISCAN#include "k_compat.h"#else#include "pci-scan.h"#include "kern_compat.h"#endif/* Condensed operations for readability. */#define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))#define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))#if (LINUX_VERSION_CODE >= 0x20100) && defined(MODULE)char kernel_version[] = UTS_RELEASE;#endifMODULE_AUTHOR("Donald Becker <becker@scyld.com>");MODULE_DESCRIPTION("Sundance Alta Ethernet driver");MODULE_LICENSE("GPL");MODULE_PARM(max_interrupt_work, "i");MODULE_PARM(debug, "i");MODULE_PARM(rx_copybreak, "i");MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");MODULE_PARM(multicast_filter_limit, "i");MODULE_PARM_DESC(debug, "Driver message level (0-31)");MODULE_PARM_DESC(options, "Force transceiver type or fixed speed+duplex");MODULE_PARM_DESC(max_interrupt_work, "Driver maximum events handled per interrupt");MODULE_PARM_DESC(full_duplex, "Non-zero to set forced full duplex (deprecated).");MODULE_PARM_DESC(rx_copybreak, "Breakpoint in bytes for copy-only-tiny-frames");MODULE_PARM_DESC(multicast_filter_limit, "Multicast addresses before switching to Rx-all-multicast");/* Theory of OperationI. Board CompatibilityThis driver is designed for the Sundance Technologies "Alta" ST201 chip.The Kendin KS8723 is the same design with an integrated transceiver andnew quirks.II. Board-specific settingsThis is an all-in-one chip, so there are no board-specific settings.III. Driver operationIIIa. Ring buffersThis driver uses two statically allocated fixed-size descriptor listsformed into rings by a branch from the final descriptor to the beginning ofthe list. The ring sizes are set at compile time by RX/TX_RING_SIZE.Some chips explicitly use only 2^N sized rings, while others use a'next descriptor' pointer that the driver forms into rings.IIIb/c. Transmit/Receive StructureThis driver uses a zero-copy receive and transmit scheme.The driver allocates full frame size skbuffs for the Rx ring buffers atopen() time and passes the skb->data field to the chip as receive databuffers. When an incoming frame is less than RX_COPYBREAK bytes long,a fresh skbuff is allocated and the frame is copied to the new skbuff.When the incoming frame is larger, the skbuff is passed directly up theprotocol stack. Buffers consumed this way are replaced by newly allocatedskbuffs in a later phase of receives.The RX_COPYBREAK value is chosen to trade-off the memory wasted byusing a full-sized skbuff for small frames vs. the copying costs of largerframes. New boards are typically used in generously configured machinesand the underfilled buffers have negligible impact compared to the benefit ofa single allocation size, so the default value of zero results in nevercopying packets. When copying is done, the cost is usually mitigated by usinga combined copy/checksum routine. Copying also preloads the cache, which ismost useful with small frames.A subtle aspect of the operation is that the IP header at offset 14 in anethernet frame isn't longword aligned for further processing.Unaligned buffers are permitted by the Sundance hardware, soframes are received into the skbuff at an offset of "+2", 16-byte aligningthe IP header.IIId. 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 interrupt handling 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. After reaping the stats, it marks the Tx queue entry asempty by incrementing the dirty_tx mark. Iff the 'lp->tx_full' flag is set, itclears both the tx_full and tbusy flags.IV. NotesIVb. ReferencesThe Sundance ST201 datasheet, preliminary version.The Kendin KS8723 datasheet, preliminary version.http://www.scyld.com/expert/100mbps.htmlhttp://www.scyld.com/expert/NWay.htmlIVc. Errata*//* Work-around for Kendin chip bugs. This will be reversed after tracking down all of the chip access quirks in memory mode. */#ifndef USE_MEM_OPS#define USE_IO_OPS 1#endifstatic void *sundance_probe1(struct pci_dev *pdev, void *init_dev, long ioaddr, int irq, int chip_idx, int find_cnt);static int sundance_pwr_event(void *dev_instance, int event);enum chip_capability_flags {CanHaveMII=1, KendinPktDropBug=2, };#ifdef USE_IO_OPS#define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_IO | PCI_ADDR0)#else#define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_MEM | PCI_ADDR1)#endifstatic struct pci_id_info pci_id_tbl[] = { {"D-Link DFE-580TX (Kendin/Sundance ST201 Alta)", {0x10021186, 0xffffffff, 0x10121186, 0xffffffff, 0x14, 0xff}, PCI_IOTYPE, 128, CanHaveMII|KendinPktDropBug}, {"D-Link DFE-580TX (Sundance ST201)", {0x10021186, 0xffffffff, 0x10121186, 0xffffffff, }, PCI_IOTYPE, 128, CanHaveMII|KendinPktDropBug}, {"D-Link DFE-550FX 100baseFx (Sundance ST201)", {0x10031186, 0xffffffff, }, PCI_IOTYPE, 128, CanHaveMII|KendinPktDropBug}, {"OEM Sundance Technology ST201", {0x10021186, 0xffffffff, }, PCI_IOTYPE, 128, CanHaveMII}, {"Sundance Technology Alta", {0x020113F0, 0xffffffff, }, PCI_IOTYPE, 128, CanHaveMII}, {0,}, /* 0 terminated list. */};struct drv_id_info sundance_drv_id = { "sundance", PCI_HOTSWAP, PCI_CLASS_NETWORK_ETHERNET<<8, pci_id_tbl, sundance_probe1, sundance_pwr_event };/* This driver was written to use PCI memory space, however x86-oriented hardware often uses I/O space accesses. */#ifdef USE_IO_OPS#undef readb#undef readw#undef readl#undef writeb#undef writew#undef writel#define readb inb#define readw inw#define readl inl#define writeb outb#define writew outw#define writel outl#endif/* Offsets to the device registers. Unlike software-only systems, device drivers interact with complex hardware. It's not useful to define symbolic names for every register bit in the device. The name can only partially document the semantics and make the driver longer and more difficult to read. In general, only the important configuration values or bits changed multiple times should be defined symbolically.*/enum alta_offsets { DMACtrl=0x00, TxListPtr=0x04, TxDMACtrl=0x08, TxDescPoll=0x0a, RxDMAStatus=0x0c, RxListPtr=0x10, RxDMACtrl=0x14, RxDescPoll=0x16, LEDCtrl=0x1a, ASICCtrl=0x30, EEData=0x34, EECtrl=0x36, TxThreshold=0x3c, FlashAddr=0x40, FlashData=0x44, WakeEvent=0x45, TxStatus=0x46, DownCounter=0x48, IntrClear=0x4a, IntrEnable=0x4c, IntrStatus=0x4e, MACCtrl0=0x50, MACCtrl1=0x52, StationAddr=0x54, MaxFrameSize=0x5A, RxMode=0x5c, MIICtrl=0x5e, MulticastFilter0=0x60, MulticastFilter1=0x64, RxOctetsLow=0x68, RxOctetsHigh=0x6a, TxOctetsLow=0x6c, TxOctetsHigh=0x6e, TxFramesOK=0x70, RxFramesOK=0x72, StatsCarrierError=0x74, StatsLateColl=0x75, StatsMultiColl=0x76, StatsOneColl=0x77, StatsTxDefer=0x78, RxMissed=0x79, StatsTxXSDefer=0x7a, StatsTxAbort=0x7b, StatsBcastTx=0x7c, StatsBcastRx=0x7d, StatsMcastTx=0x7e, StatsMcastRx=0x7f, /* Aliased and bogus values! */ RxStatus=0x0c,};/* Bits in the interrupt status/mask registers. */enum intr_status_bits { IntrSummary=0x0001, IntrPCIErr=0x0002, IntrMACCtrl=0x0008, IntrTxDone=0x0004, IntrRxDone=0x0010, IntrRxStart=0x0020, IntrDrvRqst=0x0040, StatsMax=0x0080, LinkChange=0x0100, IntrTxDMADone=0x0200, IntrRxDMADone=0x0400,};/* Bits in the RxMode register. */enum rx_mode_bits { AcceptAllIPMulti=0x20, AcceptMultiHash=0x10, AcceptAll=0x08, AcceptBroadcast=0x04, AcceptMulticast=0x02, AcceptMyPhys=0x01,};/* Bits in MACCtrl. */enum mac_ctrl0_bits { EnbFullDuplex=0x20, EnbRcvLargeFrame=0x40, EnbFlowCtrl=0x100, EnbPassRxCRC=0x200,};enum mac_ctrl1_bits { StatsEnable=0x0020, StatsDisable=0x0040, StatsEnabled=0x0080, TxEnable=0x0100, TxDisable=0x0200, TxEnabled=0x0400, RxEnable=0x0800, RxDisable=0x1000, RxEnabled=0x2000,};/* The Rx and Tx buffer descriptors. Using only 32 bit fields simplifies software endian correction. This structure must be aligned, and should avoid spanning cache lines.*/struct netdev_desc { u32 next_desc; u32 status; struct desc_frag { u32 addr, length; } frag[1];};/* Bits in netdev_desc.status */enum desc_status_bits { DescOwn=0x8000, DescEndPacket=0x4000, DescEndRing=0x2000, DescTxDMADone=0x10000, LastFrag=0x80000000, DescIntrOnTx=0x8000, DescIntrOnDMADone=0x80000000,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -