📄 yellowfin.c
字号:
/* yellowfin.c: A Packet Engines G-NIC ethernet driver for linux. *//* Written 1997-1999 by Donald Becker. 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 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@CESDIS.gsfc.nasa.gov, or C/O Center of Excellence in Space Data and Information Sciences Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771 Support and updates available at http://cesdis.gsfc.nasa.gov/linux/drivers/yellowfin.html*/static const char *version ="yellowfin.c:v1.03a 7/30/99 Written by Donald Becker, becker@cesdis.edu\n"" http://cesdis.gsfc.nasa.gov/linux/drivers/yellowfin.html\n";/* A few user-configurable values. */static int debug = 1;static int max_interrupt_work = 20;static int mtu = 0;#ifdef YF_PROTOTYPE /* Support for prototype hardware errata. *//* System-wide count of bogus-rx frames. */static int bogus_rx = 0;static int dma_ctrl = 0x004A0263; /* Constrained by errata */static int fifo_cfg = 0x0020; /* Bypass external Tx FIFO. */#elif YF_NEW /* A future perfect board :->. */static int dma_ctrl = 0x00CAC277; /* Override when loading module! */static int fifo_cfg = 0x0028;#elsestatic int dma_ctrl = 0x004A0263; /* Constrained by errata */static 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 = 0;/* 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 = 0;/* Operational parameters that are set at compile time. *//* Keep the ring sizes a power of two for efficiency. Making the Tx queue 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/* Operational parameters that usually are not changed. *//* Time in jiffies before concluding the transmitter is hung. */#define TX_TIMEOUT (2*HZ)#define yellowfin_debug debug#if !defined(__OPTIMIZE__) || !defined(__KERNEL__)#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 <linux/version.h>#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/malloc.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <linux/init.h>#include <asm/processor.h> /* Processor type for cache alignment. */#include <asm/unaligned.h>#include <asm/bitops.h>#include <asm/io.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>/* Condensed operations for readability. Compatibility defines are now in drv_compat.h */#define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))#define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))#ifdef USE_IO_OPS#define YF_INB inb#define YF_INW inw#define YF_INL inl#define YF_OUTB outb#define YF_OUTW outw#define YF_OUTL outl#else#define YF_INB readb#define YF_INW readw#define YF_INL readl#define YF_OUTB writeb#define YF_OUTW writew#define YF_OUTL writel#endif/* Theory of OperationI. Board CompatibilityThis device driver is designed for the Packet Engines "Yellowfin" GigabitEthernet adapter. The only PCA currently supported is the G-NIC 64-bitPCI card.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).*//* A few values that may be tweaked. */#define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*//* The rest of these values should never change. */enum capability_flags { HasMII=1, FullTxStatus=2, IsGigabit=4, HasMulticastBug=8, FullRxStatus=16, HasMACAddrBug=32, /* Really only on early revs. */};/* The PCI I/O space extent. */#define YELLOWFIN_SIZE 0x100#define YELLOWFIN_MODULE_NAME "yellowfin"#define PFX YELLOWFIN_MODULE_NAME ": "typedef enum { YELLOWFIN_GNIC, SYM83C885,} chip_t;struct chip_info { const char *name; int flags;};/* index by chip_t */static struct chip_info chip_info[] = { {"Yellowfin G-NIC Gigabit Ethernet", FullTxStatus | IsGigabit | HasMulticastBug | HasMACAddrBug}, {"Symbios SYM83C885", HasMII },};static struct pci_device_id yellowfin_pci_tbl[] __devinitdata = { { 0x1000, 0x0702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, YELLOWFIN_GNIC }, { 0x1000, 0x0701, PCI_ANY_ID, PCI_ANY_ID, 0, 0, SYM83C885 }, { 0, }};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 { u32 dbdma_cmd; u32 addr; u32 branch_addr; u32 result_status;};struct tx_status_words {#if defined(__powerpc__) 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};/* 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 */struct yellowfin_private { /* Descriptor rings first for alignment. Tx requires a second descriptor for status. */ struct yellowfin_desc rx_ring[RX_RING_SIZE]; struct yellowfin_desc tx_ring[TX_RING_SIZE*2]; /* The addresses of receive-in-place skbuffs. */ struct sk_buff* rx_skbuff[RX_RING_SIZE]; /* The saved address of a sent-in-place packet/buffer, for skfree(). */ struct sk_buff* tx_skbuff[TX_RING_SIZE]; struct tx_status_words tx_status[TX_RING_SIZE]; struct timer_list timer; /* Media selection timer. */ struct net_device_stats stats; /* Frequently used and paired value: keep adjacent for cache effect. */ struct pci_dev *pci_dev; int chip_id, flags; struct yellowfin_desc *rx_head_desc; 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[2]; /* MII device addresses. */ u32 pad[4]; /* Used for 32-byte alignment */ spinlock_t lock;};MODULE_AUTHOR("Donald Becker <becker@cesdis.gsfc.nasa.gov>");MODULE_DESCRIPTION("Packet Engines Yellowfin G-NIC Gigabit Ethernet driver");MODULE_PARM(max_interrupt_work, "i");MODULE_PARM(mtu, "i");MODULE_PARM(debug, "i");MODULE_PARM(rx_copybreak, "i");MODULE_PARM(gx_fix, "i");MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");static int read_eeprom(long ioaddr, int location);static int mdio_read(long ioaddr, int phy_id, int location);static void mdio_write(long ioaddr, int phy_id, int location, int value);#ifdef HAVE_PRIVATE_IOCTLstatic int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);#endifstatic 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 void yellowfin_interrupt(int irq, void *dev_instance, struct pt_regs *regs);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 struct net_device_stats *yellowfin_get_stats(struct net_device *dev);static void set_rx_mode(struct net_device *dev);static int __devinit read_eeprom(long ioaddr, int location){ int bogus_cnt = 10000; /* Typical 33Mhz: 1050 ticks */ YF_OUTB(location, ioaddr + EEAddr); YF_OUTB(0x30 | ((location >> 8) & 7), ioaddr + EECtrl); while ((YF_INB(ioaddr + EEStatus) & 0x80) && --bogus_cnt > 0) ; return YF_INB(ioaddr + EERead);}/* MII Managemen Data I/O accesses. These routines assume the MDIO controller is idle, and do not exit until the command is finished. */static int mdio_read(long ioaddr, int phy_id, int location){ int i; YF_OUTW((phy_id<<8) + location, ioaddr + MII_Addr); YF_OUTW(1, ioaddr + MII_Cmd); for (i = 10000; i >= 0; i--) if ((YF_INW(ioaddr + MII_Status) & 1) == 0) break; return YF_INW(ioaddr + MII_Rd_Data);}static void mdio_write(long ioaddr, int phy_id, int location, int value){ int i; YF_OUTW((phy_id<<8) + location, ioaddr + MII_Addr); YF_OUTW(value, ioaddr + MII_Wr_Data); /* Wait for the command to finish. */ for (i = 10000; i >= 0; i--) if ((YF_INW(ioaddr + MII_Status) & 1) == 0) break; return;}static int yellowfin_open(struct net_device *dev){ struct yellowfin_private *yp = (struct yellowfin_private *)dev->priv; long ioaddr = dev->base_addr; int i; /* Reset the chip. */ YF_OUTL(0x80000000, ioaddr + DMACtrl); if (request_irq(dev->irq, &yellowfin_interrupt, SA_SHIRQ, dev->name, dev)) return -EAGAIN; if (yellowfin_debug > 1) printk(KERN_DEBUG "%s: yellowfin_open() irq %d.\n", dev->name, dev->irq); yellowfin_init_ring(dev); YF_OUTL(virt_to_bus(yp->rx_ring), ioaddr + RxPtr); YF_OUTL(virt_to_bus(yp->tx_ring), ioaddr + TxPtr); for (i = 0; i < 6; i++) YF_OUTB(dev->dev_addr[i], ioaddr + StnAddr + i); /* Set up various condition 'select' registers. There are no options here. */ YF_OUTL(0x00800080, ioaddr + TxIntrSel); /* Interrupt on Tx abort */ YF_OUTL(0x00800080, ioaddr + TxBranchSel); /* Branch on Tx abort */ YF_OUTL(0x00400040, ioaddr + TxWaitSel); /* Wait on Tx status */ YF_OUTL(0x00400040, ioaddr + RxIntrSel); /* Interrupt on Rx done */ YF_OUTL(0x00400040, ioaddr + RxBranchSel); /* Branch on Rx error */ YF_OUTL(0x00400040, ioaddr + RxWaitSel); /* Wait on Rx done */ /* Initialize other registers: with so many this eventually this will converted to an offset/value list. */ YF_OUTL(dma_ctrl, ioaddr + DMACtrl); YF_OUTW(fifo_cfg, ioaddr + FIFOcfg); /* Enable automatic generation of flow control frames, period 0xffff. */ YF_OUTL(0x0030FFFF, ioaddr + FlowCtrl); yp->tx_threshold = 32; YF_OUTL(yp->tx_threshold, ioaddr + TxThreshold); if (dev->if_port == 0) dev->if_port = yp->default_port; netif_start_queue (dev); /* Setting the Rx mode will start the Rx process. */ if (yp->flags & IsGigabit) { /* We are always in full-duplex mode with gigabit! */ yp->full_duplex = 1; YF_OUTW(0x01CF, ioaddr + Cnfg); } else { YF_OUTW(0x0018, ioaddr + FrameGap0); /* 0060/4060 for non-MII 10baseT */ YF_OUTW(0x1018, ioaddr + FrameGap1); YF_OUTW(0x101C | (yp->full_duplex ? 2 : 0), ioaddr + Cnfg); } set_rx_mode(dev); /* Enable interrupts by setting the interrupt mask. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -