yellowfin.c
来自「linux 内核源代码」· C语言 代码 · 共 1,437 行 · 第 1/4 页
C
1,437 行
/* yellowfin.c: A Packet Engines G-NIC ethernet driver for linux. *//* Written 1997-2001 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. This driver is for the Packet Engines G-NIC PCI Gigabit Ethernet adapter. It also supports the Symbios Logic version of the same chip core. The author may be reached as becker@scyld.com, or C/O Scyld Computing Corporation 410 Severn Ave., Suite 210 Annapolis MD 21403 Support and updates available at http://www.scyld.com/network/yellowfin.html [link no longer provides useful info -jgarzik]*/#define DRV_NAME "yellowfin"#define DRV_VERSION "2.1"#define DRV_RELDATE "Sep 11, 2006"#define PFX DRV_NAME ": "/* The user-configurable values. These may be modified when a driver module is loaded.*/static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. *//* Maximum events (Rx packets, etc.) to handle at each interrupt. */static int max_interrupt_work = 20;static int mtu;#ifdef YF_PROTOTYPE /* Support for prototype hardware errata. *//* System-wide count of bogus-rx frames. */static int bogus_rx;static int dma_ctrl = 0x004A0263; /* Constrained by errata */static int fifo_cfg = 0x0020; /* Bypass external Tx FIFO. */#elif defined(YF_NEW) /* A future perfect board :->. */static int dma_ctrl = 0x00CAC277; /* Override when loading module! */static int fifo_cfg = 0x0028;#elsestatic const int dma_ctrl = 0x004A0263; /* Constrained by errata */static const int fifo_cfg = 0x0020; /* Bypass external Tx FIFO. */#endif/* Set the copy breakpoint for the copy-only-tiny-frames scheme. Setting to > 1514 effectively disables this feature. */static int rx_copybreak;/* Used to pass the media type, etc. No media types are currently defined. These exist for driver interoperability.*/#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};/* Do ugly workaround for GX server chipset errata. */static int gx_fix;/* Operational parameters that are set at compile time. *//* Keep the ring sizes a power of two for efficiency. Making the Tx ring too long decreases the effectiveness of channel bonding and packet priority. There are no ill effects from too-large receive rings. */#define TX_RING_SIZE 16#define TX_QUEUE_SIZE 12 /* Must be > 4 && <= TX_RING_SIZE */#define RX_RING_SIZE 64#define STATUS_TOTAL_SIZE TX_RING_SIZE*sizeof(struct tx_status_words)#define TX_TOTAL_SIZE 2*TX_RING_SIZE*sizeof(struct yellowfin_desc)#define RX_TOTAL_SIZE RX_RING_SIZE*sizeof(struct yellowfin_desc)/* Operational parameters that usually are not changed. *//* Time in jiffies before concluding the transmitter is hung. */#define TX_TIMEOUT (2*HZ)#define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/#define yellowfin_debug debug#include <linux/module.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/timer.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 <linux/mii.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/ethtool.h>#include <linux/crc32.h>#include <linux/bitops.h>#include <asm/uaccess.h>#include <asm/processor.h> /* Processor type for cache alignment. */#include <asm/unaligned.h>#include <asm/io.h>/* These identify the driver base version and may not be removed. */static char version[] __devinitdata =KERN_INFO DRV_NAME ".c:v1.05 1/09/2001 Written by Donald Becker <becker@scyld.com>\n"KERN_INFO " (unofficial 2.4.x port, " DRV_VERSION ", " DRV_RELDATE ")\n";MODULE_AUTHOR("Donald Becker <becker@scyld.com>");MODULE_DESCRIPTION("Packet Engines Yellowfin G-NIC Gigabit Ethernet driver");MODULE_LICENSE("GPL");module_param(max_interrupt_work, int, 0);module_param(mtu, int, 0);module_param(debug, int, 0);module_param(rx_copybreak, int, 0);module_param_array(options, int, NULL, 0);module_param_array(full_duplex, int, NULL, 0);module_param(gx_fix, int, 0);MODULE_PARM_DESC(max_interrupt_work, "G-NIC maximum events handled per interrupt");MODULE_PARM_DESC(mtu, "G-NIC MTU (all boards)");MODULE_PARM_DESC(debug, "G-NIC debug level (0-7)");MODULE_PARM_DESC(rx_copybreak, "G-NIC copy breakpoint for copy-only-tiny-frames");MODULE_PARM_DESC(options, "G-NIC: Bits 0-3: media type, bit 17: full duplex");MODULE_PARM_DESC(full_duplex, "G-NIC full duplex setting(s) (1)");MODULE_PARM_DESC(gx_fix, "G-NIC: enable GX server chipset bug workaround (0-1)");/* Theory of OperationI. Board CompatibilityThis device driver is designed for the Packet Engines "Yellowfin" GigabitEthernet adapter. The G-NIC 64-bit PCI card is supported, as well as theSymbios 53C885E dual function chip.II. Board-specific settingsPCI bus devices are configured by the system at boot time, so no jumpersneed to be set on the board. The system BIOS preferably should assign thePCI INTA signal to an otherwise unused system IRQ line.Note: Kernel versions earlier than 1.3.73 do not support shared PCIinterrupt lines.III. Driver operationIIIa. Ring buffersThe Yellowfin uses the Descriptor Based DMA Architecture specified by Apple.This is a descriptor list scheme similar to that used by the EEPro100 andTulip. This 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.The driver allocates full frame size skbuffs for the Rx ring buffers atopen() time and passes the skb->data field to the Yellowfin 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 and replaced by a newly allocated skbuff.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. For small frames the copying cost is negligible (esp. consideringthat we are pre-loading the cache with immediately useful headerinformation). For large frames the copying cost is non-trivial, and thelarger copy might flush the cache of useful data.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 'yp->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 'yp->tx_full' flag is set, itclears both the tx_full and tbusy flags.IV. NotesThanks to Kim Stearns of Packet Engines for providing a pair of G-NIC boards.Thanks to Bruce Faust of Digitalscape for providing both their SYM53C885 boardand an AlphaStation to verifty the Alpha port!IVb. ReferencesYellowfin Engineering Design Specification, 4/23/97 Preliminary/ConfidentialSymbios SYM53C885 PCI-SCSI/Fast Ethernet Multifunction Controller Preliminary Data Manual v3.0http://cesdis.gsfc.nasa.gov/linux/misc/NWay.htmlhttp://cesdis.gsfc.nasa.gov/linux/misc/100mbps.htmlIVc. ErrataSee Packet Engines confidential appendix (prototype chips only).*/enum capability_flags { HasMII=1, FullTxStatus=2, IsGigabit=4, HasMulticastBug=8, FullRxStatus=16, HasMACAddrBug=32, /* Only on early revs. */ DontUseEeprom=64, /* Don't read the MAC from the EEPROm. */};/* The PCI I/O space extent. */enum { YELLOWFIN_SIZE = 0x100,};struct pci_id_info { const char *name; struct match_info { int pci, pci_mask, subsystem, subsystem_mask; int revision, revision_mask; /* Only 8 bits. */ } id; int drv_flags; /* Driver use, intended as capability flags. */};static const struct pci_id_info pci_id_tbl[] = { {"Yellowfin G-NIC Gigabit Ethernet", { 0x07021000, 0xffffffff}, FullTxStatus | IsGigabit | HasMulticastBug | HasMACAddrBug | DontUseEeprom}, {"Symbios SYM83C885", { 0x07011000, 0xffffffff}, HasMII | DontUseEeprom }, { }};static const struct pci_device_id yellowfin_pci_tbl[] = { { 0x1000, 0x0702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, { 0x1000, 0x0701, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 }, { }};MODULE_DEVICE_TABLE (pci, yellowfin_pci_tbl);/* Offsets to the Yellowfin registers. Various sizes and alignments. */enum yellowfin_offsets { TxCtrl=0x00, TxStatus=0x04, TxPtr=0x0C, TxIntrSel=0x10, TxBranchSel=0x14, TxWaitSel=0x18, RxCtrl=0x40, RxStatus=0x44, RxPtr=0x4C, RxIntrSel=0x50, RxBranchSel=0x54, RxWaitSel=0x58, EventStatus=0x80, IntrEnb=0x82, IntrClear=0x84, IntrStatus=0x86, ChipRev=0x8C, DMACtrl=0x90, TxThreshold=0x94, Cnfg=0xA0, FrameGap0=0xA2, FrameGap1=0xA4, MII_Cmd=0xA6, MII_Addr=0xA8, MII_Wr_Data=0xAA, MII_Rd_Data=0xAC, MII_Status=0xAE, RxDepth=0xB8, FlowCtrl=0xBC, AddrMode=0xD0, StnAddr=0xD2, HashTbl=0xD8, FIFOcfg=0xF8, EEStatus=0xF0, EECtrl=0xF1, EEAddr=0xF2, EERead=0xF3, EEWrite=0xF4, EEFeature=0xF5,};/* The Yellowfin Rx and Tx buffer descriptors. Elements are written as 32 bit for endian portability. */struct yellowfin_desc { __le32 dbdma_cmd; __le32 addr; __le32 branch_addr; __le32 result_status;};struct tx_status_words {#ifdef __BIG_ENDIAN u16 tx_errs; u16 tx_cnt; u16 paused; u16 total_tx_cnt;#else /* Little endian chips. */ u16 tx_cnt; u16 tx_errs; u16 total_tx_cnt; u16 paused;#endif /* __BIG_ENDIAN */};/* Bits in yellowfin_desc.cmd */enum desc_cmd_bits { CMD_TX_PKT=0x10000000, CMD_RX_BUF=0x20000000, CMD_TXSTATUS=0x30000000, CMD_NOP=0x60000000, CMD_STOP=0x70000000, BRANCH_ALWAYS=0x0C0000, INTR_ALWAYS=0x300000, WAIT_ALWAYS=0x030000, BRANCH_IFTRUE=0x040000,};/* Bits in yellowfin_desc.status */enum desc_status_bits { RX_EOP=0x0040, };/* Bits in the interrupt status/mask registers. */enum intr_status_bits { IntrRxDone=0x01, IntrRxInvalid=0x02, IntrRxPCIFault=0x04,IntrRxPCIErr=0x08, IntrTxDone=0x10, IntrTxInvalid=0x20, IntrTxPCIFault=0x40,IntrTxPCIErr=0x80, IntrEarlyRx=0x100, IntrWakeup=0x200, };#define PRIV_ALIGN 31 /* Required alignment mask */#define MII_CNT 4struct yellowfin_private { /* Descriptor rings first for alignment. Tx requires a second descriptor for status. */ struct yellowfin_desc *rx_ring; struct yellowfin_desc *tx_ring; struct sk_buff* rx_skbuff[RX_RING_SIZE]; struct sk_buff* tx_skbuff[TX_RING_SIZE]; dma_addr_t rx_ring_dma; dma_addr_t tx_ring_dma; struct tx_status_words *tx_status; dma_addr_t tx_status_dma; struct timer_list timer; /* Media selection timer. */ /* Frequently used and paired value: keep adjacent for cache effect. */ int chip_id, drv_flags; struct pci_dev *pci_dev; unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */ unsigned int rx_buf_sz; /* Based on MTU+slack. */ struct tx_status_words *tx_tail_desc; unsigned int cur_tx, dirty_tx; int tx_threshold; unsigned int tx_full:1; /* The Tx queue is full. */ unsigned int full_duplex:1; /* Full-duplex operation requested. */ unsigned int duplex_lock:1; unsigned int medialock:1; /* Do not sense media. */ unsigned int default_port:4; /* Last dev->if_port value. */ /* MII transceiver section. */ int mii_cnt; /* MII device addresses. */ u16 advertising; /* NWay media advertisement */ unsigned char phys[MII_CNT]; /* MII device addresses, only first one used */ spinlock_t lock; void __iomem *base;};static int read_eeprom(void __iomem *ioaddr, int location);static int mdio_read(void __iomem *ioaddr, int phy_id, int location);static void mdio_write(void __iomem *ioaddr, int phy_id, int location, int value);static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);static int yellowfin_open(struct net_device *dev);static void yellowfin_timer(unsigned long data);static void yellowfin_tx_timeout(struct net_device *dev);static void yellowfin_init_ring(struct net_device *dev);static int yellowfin_start_xmit(struct sk_buff *skb, struct net_device *dev);static irqreturn_t yellowfin_interrupt(int irq, void *dev_instance);static int yellowfin_rx(struct net_device *dev);static void yellowfin_error(struct net_device *dev, int intr_status);static int yellowfin_close(struct net_device *dev);static void set_rx_mode(struct net_device *dev);static const struct ethtool_ops ethtool_ops;static int __devinit yellowfin_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?