⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 starfire.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* starfire.c: Linux device driver for the Adaptec Starfire network adapter. *//*	Written 1998-2000 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 and updates available at	http://www.scyld.com/network/starfire.html	-----------------------------------------------------------	Linux kernel-specific changes:		LK1.1.1 (jgarzik):	- Use PCI driver interface	- Fix MOD_xxx races	- softnet fixups	LK1.1.2 (jgarzik):	- Merge Becker version 0.15	LK1.1.3 (Andrew Morton)	- Timer cleanups		LK1.1.4 (jgarzik):	- Merge Becker version 1.03*//* These identify the driver base version and may not be removed. */static const char version1[] ="starfire.c:v1.03 7/26/2000  Written by Donald Becker <becker@scyld.com>\n";static const char version2[] =" Updates and info at http://www.scyld.com/network/starfire.html\n";static const char version3[] =" (unofficial 2.4.x kernel port, version 1.1.4, August 10, 2000)\n";/* The user-configurable values.   These may be modified when a driver module is loaded.*//* Used for tuning interrupt latency vs. overhead. */static int interrupt_mitigation = 0x0;static int debug = 1;			/* 1 normal messages, 0 quiet .. 7 verbose. */static int max_interrupt_work = 20;static int mtu = 0;/* Maximum number of multicast addresses to filter (vs. rx-all-multicast).   The Starfire has a 512 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. */static int rx_copybreak = 0;/* Used to pass the media type, etc.   Both 'options[]' and 'full_duplex[]' exist for driver interoperability.   The media type is usually passed in 'options[]'.*/#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. *//* The "native" ring sizes are either 256 or 2048.   However in some modes a descriptor may be marked to wrap the ring earlier.   The driver allocates a single page for each descriptor ring, constraining   the maximum size in an architecture-dependent way.*/#define RX_RING_SIZE	256#define TX_RING_SIZE	32/* The completion queues are fixed at 1024 entries i.e. 4K or 8KB. */#define DONE_Q_SIZE	1024/* 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.*/#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/version.h>#include <linux/module.h>#if LINUX_VERSION_CODE < 0x20300  &&  defined(MODVERSIONS)#include <linux/modversions.h>#endif#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/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/init.h>#include <asm/processor.h>		/* Processor type for cache alignment. */#include <asm/bitops.h>#include <asm/io.h>MODULE_AUTHOR("Donald Becker <becker@scyld.com>");MODULE_DESCRIPTION("Adaptec Starfire Ethernet driver");MODULE_PARM(max_interrupt_work, "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");/*				Theory of OperationI. Board CompatibilityThis driver is for the Adaptec 6915 "Starfire" 64 bit PCI Ethernet adapter.II. Board-specific settingsIII. Driver operationIIIa. Ring buffersThe Starfire hardware uses multiple fixed-size descriptor queues/rings.  Thering sizes are set fixed by the hardware, but may optionally be wrappedearlier by the END bit in the descriptor.This driver uses that hardware queue size for the Rx ring, where a largenumber of entries has no ill effect beyond increases the potential backlog.The Tx ring is wrapped with the END bit, since a large hardware Tx queuedisables the queue layer priority ordering and we have no mechanism toutilize the hardware two-level priority queue.  When modifying theRX/TX_RING_SIZE pay close attention to page sizes and the ring-empty warninglevels.IIIb/c. Transmit/Receive StructureSee the Adaptec manual for the many possible structures, and options foreach structure.  There are far too many to document here.For transmit this driver uses type 1 transmit descriptors, and relies onautomatic minimum-length padding.  It does not use the completion queueconsumer index, but instead checks for non-zero status entries.For receive this driver uses type 0 receive descriptors.  The driverallocates full frame size skbuffs for the Rx ring buffers, so all framesshould fit in a single descriptor.  The driver does not use the completionqueue consumer index, but instead checks for non-zero status entries.When an incoming frame is less than RX_COPYBREAK bytes long, a fresh skbuffis allocated and the frame is copied to the new skbuff.  When the incomingframe is larger, the skbuff is passed directly up the protocol stack.Buffers consumed this way are replaced by newly allocated skbuffs in a laterphase of receive.A notable aspect of operation is that unaligned buffers are not permitted bythe Starfire hardware.  The IP header at offset 14 in an ethernet frame thusisn't longword aligned, which may cause problems on some machinee.g. Alphas.  Copied frames are put into the skbuff at an offset of "+2",16-byte aligning the 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 Adaptec Starfire manuals, available only from Adaptec.http://www.scyld.com/expert/100mbps.htmlhttp://www.scyld.com/expert/NWay.htmlIVc. Errata*/enum chip_capability_flags {CanHaveMII=1, };#define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_MEM | PCI_ADDR0)#define MEM_ADDR_SZ 0x80000		/* And maps in 0.5MB(!).  */#if 0#define ADDR_64BITS 1			/* This chip uses 64 bit addresses. */#endif#define HAS_IP_COPYSUM 1enum chipset {	CH_6915 = 0,};static struct pci_device_id starfire_pci_tbl[] __devinitdata = {	{ 0x9004, 0x6915, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_6915 },	{ 0, }};MODULE_DEVICE_TABLE(pci, starfire_pci_tbl);/* A chip capabilities table, matching the CH_xxx entries in xxx_pci_tbl[] above. */static struct chip_info {	const char *name;	int io_size;	int drv_flags;} netdrv_tbl[] __devinitdata = {	{ "Adaptec Starfire 6915", MEM_ADDR_SZ, CanHaveMII },};/* 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 register_offsets {	PCIDeviceConfig=0x50040, GenCtrl=0x50070, IntrTimerCtrl=0x50074,	IntrClear=0x50080, IntrStatus=0x50084, IntrEnable=0x50088,	MIICtrl=0x52000, StationAddr=0x50120, EEPROMCtrl=0x51000,	TxDescCtrl=0x50090,	TxRingPtr=0x50098, HiPriTxRingPtr=0x50094, /* Low and High priority. */	TxRingHiAddr=0x5009C,		/* 64 bit address extension. */	TxProducerIdx=0x500A0, TxConsumerIdx=0x500A4,	TxThreshold=0x500B0,	CompletionHiAddr=0x500B4, TxCompletionAddr=0x500B8,	RxCompletionAddr=0x500BC, RxCompletionQ2Addr=0x500C0,	CompletionQConsumerIdx=0x500C4,	RxDescQCtrl=0x500D4, RxDescQHiAddr=0x500DC, RxDescQAddr=0x500E0,	RxDescQIdx=0x500E8, RxDMAStatus=0x500F0, RxFilterMode=0x500F4,	TxMode=0x55000,};/* Bits in the interrupt status/mask registers. */enum intr_status_bits {	IntrNormalSummary=0x8000,	IntrAbnormalSummary=0x02000000,	IntrRxDone=0x0300, IntrRxEmpty=0x10040, IntrRxPCIErr=0x80000,	IntrTxDone=0x4000, IntrTxEmpty=0x1000, IntrTxPCIErr=0x80000,	StatsMax=0x08000000, LinkChange=0xf0000000,	IntrTxDataLow=0x00040000,};/* Bits in the RxFilterMode register. */enum rx_mode_bits {	AcceptBroadcast=0x04, AcceptAllMulticast=0x02, AcceptAll=0x01,	AcceptMulticast=0x10, AcceptMyPhys=0xE040,};/* The Rx and Tx buffer descriptors. */struct starfire_rx_desc {	u32 rxaddr;					/* Optionally 64 bits. */};enum rx_desc_bits {	RxDescValid=1, RxDescEndRing=2,};/* Completion queue entry.   You must update the page allocation, init_ring and the shift count in rx()   if using a larger format. */struct rx_done_desc {	u32 status;					/* Low 16 bits is length. */#ifdef full_rx_status	u32 status2;	u16 vlanid;	u16 csum; 			/* partial checksum */	u32 timestamp;#endif};enum rx_done_bits {	RxOK=0x20000000, RxFIFOErr=0x10000000, RxBufQ2=0x08000000,};/* Type 1 Tx descriptor. */struct starfire_tx_desc {	u32 status;					/* Upper bits are status, lower 16 length. */	u32 addr;};enum tx_desc_bits {	TxDescID=0xB1010000,		/* Also marks single fragment, add CRC.  */	TxDescIntr=0x08000000, TxRingWrap=0x04000000,};struct tx_done_report {	u32 status;					/* timestamp, index. */#if 0	u32 intrstatus;				/* interrupt status */#endif};#define PRIV_ALIGN	15 	/* Required alignment mask */struct ring_info {	struct sk_buff *skb;	dma_addr_t mapping;};struct netdev_private {	/* Descriptor rings first for alignment. */	struct starfire_rx_desc *rx_ring;	struct starfire_tx_desc *tx_ring;	dma_addr_t rx_ring_dma;	dma_addr_t tx_ring_dma;	/* The addresses of rx/tx-in-place skbuffs. */	struct ring_info rx_info[RX_RING_SIZE];	struct ring_info tx_info[TX_RING_SIZE];	/* Pointers to completion queues (full pages).  I should cache line pad..*/	u8 pad0[100];	struct rx_done_desc *rx_done_q;	dma_addr_t rx_done_q_dma;	unsigned int rx_done;	struct tx_done_report *tx_done_q;	unsigned int tx_done;	dma_addr_t tx_done_q_dma;	struct net_device_stats stats;	struct timer_list timer;	/* Media monitoring timer. */	struct pci_dev *pci_dev;	/* Frequently used values: keep some adjacent for cache effect. */	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. */	/* These values are keep track of the transceiver/media in use. */	unsigned int full_duplex:1,			/* Full-duplex operation requested. */		medialock:1,					/* Xcvr set to fixed speed/duplex. */		rx_flowctrl:1,		tx_flowctrl:1;					/* Use 802.3x flow control. */	unsigned int default_port:4;		/* Last dev->if_port value. */	u32 tx_mode;	u8 tx_threshold;	/* MII transceiver section. */	int mii_cnt;						/* MII device addresses. */	u16 advertising;					/* NWay media advertisement */	unsigned char phys[2];				/* MII device addresses. */};static int  mdio_read(struct net_device *dev, int phy_id, int location);static void mdio_write(struct net_device *dev, int phy_id, int location, int value);static int  netdev_open(struct net_device *dev);static void check_duplex(struct net_device *dev, int startup);static void netdev_timer(unsigned long data);static void tx_timeout(struct net_device *dev);static void init_ring(struct net_device *dev);static int  start_tx(struct sk_buff *skb, struct net_device *dev);static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);static void netdev_error(struct net_device *dev, int intr_status);static int  netdev_rx(struct net_device *dev);static void netdev_error(struct net_device *dev, int intr_status);static void set_rx_mode(struct net_device *dev);static struct net_device_stats *get_stats(struct net_device *dev);static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);static int  netdev_close(struct net_device *dev);static int __devinit starfire_init_one (struct pci_dev *pdev,					const struct pci_device_id *ent){	struct netdev_private *np;	int i, irq, option, chip_idx = ent->driver_data;	struct net_device *dev;	static int card_idx = -1;	static int printed_version = 0;	long ioaddr;	int drv_flags, io_size = netdrv_tbl[chip_idx].io_size;	card_idx++;	option = card_idx < MAX_UNITS ? options[card_idx] : 0;		if (!printed_version++)		printk(KERN_INFO "%s" KERN_INFO "%s" KERN_INFO "%s",		       version1, version2, version3);	ioaddr = pci_resource_start (pdev, 0);	if (!ioaddr || ((pci_resource_flags (pdev, 0) & IORESOURCE_MEM) == 0)) {		printk (KERN_ERR "starfire %d: no PCI MEM resources, aborting\n", card_idx);		return -ENODEV;	}		dev = init_etherdev(NULL, sizeof(*np));	if (!dev) {		printk (KERN_ERR "starfire %d: cannot alloc etherdev, aborting\n", card_idx);		return -ENOMEM;	}		irq = pdev->irq; 	if (request_mem_region (ioaddr, io_size, dev->name) == NULL) {		printk (KERN_ERR "starfire %d: resource 0x%x @ 0x%lx busy, aborting\n",			card_idx, io_size, ioaddr);		goto err_out_free_netdev;	}		if (pci_enable_device (pdev))		goto err_out_free_res;		ioaddr = (long) ioremap (ioaddr, io_size);	if (!ioaddr) {		printk (KERN_ERR "starfire %d: cannot remap 0x%x @ 0x%lx, aborting\n",			card_idx, io_size, ioaddr);		goto err_out_free_res;	}	pci_set_master (pdev);		printk(KERN_INFO "%s: %s at 0x%lx, ",		   dev->name, netdrv_tbl[chip_idx].name, ioaddr);	/* Serial EEPROM reads are hidden by the hardware. */	for (i = 0; i < 6; i++)		dev->dev_addr[i] = readb(ioaddr + EEPROMCtrl + 20-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);#if ! defined(final_version) /* Dump the EEPROM contents during development. */	if (debug > 4)		for (i = 0; i < 0x20; i++)			printk("%2.2x%s", (unsigned int)readb(ioaddr + EEPROMCtrl + i),				   i % 16 != 15 ? " " : "\n");#endif	/* Reset the chip to erase previous misconfiguration. */	writel(1, ioaddr + PCIDeviceConfig);	dev->base_addr = ioaddr;	dev->irq = irq;	np = dev->priv;	pdev->driver_data = dev;	np->pci_dev = pdev;	drv_flags = netdrv_tbl[chip_idx].drv_flags;	if (dev->mem_start)		option = dev->mem_start;	/* The lower four bits are the media type. */	if (option > 0) {		if (option & 0x200)			np->full_duplex = 1;		np->default_port = option & 15;		if (np->default_port)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -