📄 yellowfin.c
字号:
/* yellowfin.c: A Packet Engines G-NIC ethernet driver for linux. *//* Written 1997-1998 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.02 7/26/98 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 max_interrupt_work = 20;static int min_pci_latency = 64;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};/* Operational parameters that are set at compile time. *//* Keep the ring sizes a power of two for efficiency. Making the Tx ring too large 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 RX_RING_SIZE 32/* Operational parameters that usually are not changed. *//* Time in jiffies before concluding the transmitter is hung. */#define TX_TIMEOUT ((2000*HZ)/1000)#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/ptrace.h>#include <linux/errno.h>#include <linux/ioport.h>#include <linux/malloc.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <asm/processor.h> /* Processor type for cache alignment. */#include <asm/bitops.h>#include <asm/io.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>/* Kernel compatibility defines, most common to the PCCard package. */#include <linux/version.h> /* Evil and unneccessary */#define RUN_AT(x) (jiffies + (x))#if (LINUX_VERSION_CODE < 0x20123)#define test_and_set_bit(val, addr) set_bit(val, addr)#endif#if LINUX_VERSION_CODE <= 0x20139#define net_device_stats enet_statistics#define NETSTATS_VER2#endif#if LINUX_VERSION_CODE < 0x20155#define PCI_SUPPORT_VER1#define pci_present pcibios_present#endif#if LINUX_VERSION_CODE < 0x20159#define DEV_FREE_SKB(skb) dev_kfree_skb(skb, FREE_WRITE);#else#define DEV_FREE_SKB(skb) dev_kfree_skb(skb);#endif/* The PCI I/O space extent. */#define YELLOWFIN_TOTAL_SIZE 0x100int yellowfin_debug = 1;/* 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.*/#ifndef PCI_VENDOR_ID_PKT_ENG /* To be defined in linux/pci.h */#define PCI_VENDOR_ID_PKT_ENG 0x1000 /* Hmm, likely number.. */#define PCI_DEVICE_ID_SYM58C885 0x0701#define PCI_DEVICE_ID_YELLOWFIN 0x0702#endif/* The rest of these values should never change. */static void yellowfin_timer(unsigned long data);enum capability_flags {HasMII=1, FullTxStatus=2};static struct chip_info { u16 vendor_id, device_id, device_id_mask, pci_flags; const char *name; void (*media_timer)(unsigned long data); u32 chip_rev; /* As read from ChipRev, not PCI dev ID. */ int flags;} chip_tbl[] = { {0x1000, 0x0702, 0xffff, 0, "Yellowfin G-NIC Gbit Ethernet", yellowfin_timer, 0x0702, FullTxStatus}, {0x1000, 0x0701, 0xffff, 0, "Symbios SYM83C885", yellowfin_timer, 0x0701, HasMII}, {0,},};/* 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, 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. */struct yellowfin_desc { u16 request_cnt; u16 cmd; u32 addr; u32 branch_addr; u16 result_cnt; u16 status;};struct tx_status_words { u16 tx_cnt; u16 tx_errs; u16 total_tx_cnt; u16 paused;};/* Bits in yellowfin_desc.cmd */enum desc_cmd_bits { CMD_TX_PKT=0x1000, CMD_RX_BUF=0x2000, CMD_TXSTATUS=0x3000, CMD_NOP=0x6000, CMD_STOP=0x7000, BRANCH_ALWAYS=0x0C, INTR_ALWAYS=0x30, WAIT_ALWAYS=0x03, BRANCH_IFTRUE=0x04,};/* 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, };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]; const char *product_name; struct device *next_module; /* 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 enet_statistics stats; /* Frequently used and paired value: keep adjacent for cache effect. */ int chip_id; int in_interrupt; struct yellowfin_desc *rx_head_desc; struct tx_status_words *tx_tail_desc; unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */ unsigned int cur_tx, dirty_tx; unsigned int rx_buf_sz; /* Based on MTU+slack. */ 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 */};#ifdef MODULE#if LINUX_VERSION_CODE > 0x20115MODULE_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(min_pci_latency, "i");MODULE_PARM(mtu, "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");#endif#endifstatic struct device *yellowfin_probe1(struct device *dev, long ioaddr, int irq, int chip_id, int options);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 device *dev, struct ifreq *rq, int cmd);#endifstatic int yellowfin_open(struct device *dev);static void yellowfin_timer(unsigned long data);static void yellowfin_tx_timeout(struct device *dev);static void yellowfin_init_ring(struct device *dev);static int yellowfin_start_xmit(struct sk_buff *skb, struct device *dev);static void yellowfin_interrupt(int irq, void *dev_instance, struct pt_regs *regs);static int yellowfin_rx(struct device *dev);static void yellowfin_error(struct device *dev, int intr_status);static int yellowfin_close(struct device *dev);static struct enet_statistics *yellowfin_get_stats(struct device *dev);static void set_rx_mode(struct device *dev);/* A list of all installed Yellowfin devices, for removing the driver module. */static struct device *root_yellowfin_dev = NULL;int yellowfin_probe(struct device *dev){ int cards_found = 0; int pci_index = 0; unsigned char pci_bus, pci_device_fn; if ( ! pci_present()) return -ENODEV; for (;pci_index < 0xff; pci_index++) { u8 pci_latency; u16 pci_command, new_command, vendor, device; int chip_idx; int irq; long ioaddr; if (pcibios_find_class (PCI_CLASS_NETWORK_ETHERNET << 8, pci_index, &pci_bus, &pci_device_fn) != PCIBIOS_SUCCESSFUL) break; pcibios_read_config_word(pci_bus, pci_device_fn, PCI_VENDOR_ID, &vendor); pcibios_read_config_word(pci_bus, pci_device_fn, PCI_DEVICE_ID, &device); for (chip_idx = 0; chip_tbl[chip_idx].vendor_id; chip_idx++) if (vendor == chip_tbl[chip_idx].vendor_id && (device & chip_tbl[chip_idx].device_id_mask) == chip_tbl[chip_idx].device_id) break; if (chip_tbl[chip_idx].vendor_id == 0) /* Compiled out! */ continue; {#if LINUX_VERSION_CODE >= 0x20155 || PCI_SUPPORT_1 struct pci_dev *pdev = pci_find_slot(pci_bus, pci_device_fn); ioaddr = pdev->base_address[0]; irq = pdev->irq;#else u32 pci_ioaddr; u8 pci_irq_line; pcibios_read_config_byte(pci_bus, pci_device_fn, PCI_INTERRUPT_LINE, &pci_irq_line); pcibios_read_config_dword(pci_bus, pci_device_fn, PCI_BASE_ADDRESS_0, &pci_ioaddr); ioaddr = pci_ioaddr; irq = pci_irq_line;#endif } /* Remove I/O space marker in bit 0. */ ioaddr &= ~3; if (yellowfin_debug > 2) printk(KERN_INFO "Found %s at I/O %#lx, IRQ %d.\n", chip_tbl[chip_idx].name, ioaddr, irq); if (check_region(ioaddr, YELLOWFIN_TOTAL_SIZE)) continue; pcibios_read_config_word(pci_bus, pci_device_fn, PCI_COMMAND, &pci_command); new_command = pci_command | PCI_COMMAND_MASTER|PCI_COMMAND_IO; if (pci_command != new_command) { printk(KERN_INFO " The PCI BIOS has not enabled the" " device at %d/%d! Updating PCI command %4.4x->%4.4x.\n", pci_bus, pci_device_fn, pci_command, new_command); pcibios_write_config_word(pci_bus, pci_device_fn, PCI_COMMAND, new_command); } dev = yellowfin_probe1(dev, ioaddr, irq, chip_idx, cards_found); if (dev) { /* Get and check the bus-master and latency values. */ pcibios_read_config_byte(pci_bus, pci_device_fn, PCI_LATENCY_TIMER, &pci_latency); if (pci_latency < min_pci_latency) { printk(KERN_INFO " PCI latency timer (CFLT) is " "unreasonably low at %d. Setting to %d clocks.\n", pci_latency, min_pci_latency); pcibios_write_config_byte(pci_bus, pci_device_fn, PCI_LATENCY_TIMER, min_pci_latency); } else if (yellowfin_debug > 1) printk(KERN_INFO " PCI latency timer (CFLT) is %#x.\n", pci_latency); dev = 0; cards_found++; } } return cards_found ? 0 : -ENODEV;}static struct device *yellowfin_probe1(struct device *dev, long ioaddr, int irq, int chip_id, int card_idx){ static int did_version = 0; /* Already printed version info. */ struct yellowfin_private *yp; int option, i; if (yellowfin_debug > 0 && did_version++ == 0) printk(version); dev = init_etherdev(dev, sizeof(struct yellowfin_private)); printk(KERN_INFO "%s: %s type %8x at 0x%lx, ", dev->name, chip_tbl[chip_id].name, inl(ioaddr + ChipRev), ioaddr); if (inw(ioaddr + ChipRev) == 0x0702) for (i = 0; i < 6; i++) dev->dev_addr[i] = inb(ioaddr + StnAddr + i); else { int ee_offset = (read_eeprom(ioaddr, 6) == 0xff ? 0x100 : 0); for (i = 0; i < 6; i++) dev->dev_addr[i] = read_eeprom(ioaddr, ee_offset + i); } for (i = 0; i < 5; i++) printk("%2.2x:", dev->dev_addr[i]); printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq); /* Reset the chip. */ outl(0x80000000, ioaddr + DMACtrl); /* We do a request_region() only to register /proc/ioports info. */ request_region(ioaddr, YELLOWFIN_TOTAL_SIZE, dev->name); dev->base_addr = ioaddr; dev->irq = irq;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -