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

📄 8139cp.c

📁 linux和2410结合开发 用他可以生成2410所需的zImage文件
💻 C
📖 第 1 页 / 共 3 页
字号:
/* 8139cp.c: A Linux PCI Ethernet driver for the RealTek 8139C+ chips. *//*	Copyright 2001 Jeff Garzik <jgarzik@mandrakesoft.com>	Copyright (C) 2000, 2001 David S. Miller (davem@redhat.com) [sungem.c]	Copyright 2001 Manfred Spraul				    [natsemi.c]	Copyright 1999-2001 by Donald Becker.			    [natsemi.c]       	Written 1997-2001 by Donald Becker.			    [8139too.c]	Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>. [acenic.c]	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.	See the file COPYING in this distribution for more information.	TODO, in rough priority order:	* dev->tx_timeout	* LinkChg interrupt	* Support forcing media type with a module parameter,	  like dl2k.c/sundance.c	* Implement PCI suspend/resume	* Constants (module parms?) for Rx work limit	* support 64-bit PCI DMA	* Complete reset on PciErr	* Consider Rx interrupt mitigation using TimerIntr	* Implement 8139C+ statistics dump; maybe not...	  h/w stats can be reset only by software reset	* Rx checksumming	* Tx checksumming	* ETHTOOL_GREGS, ETHTOOL_[GS]WOL,	* Jumbo frames / dev->change_mtu	* Investigate using skb->priority with h/w VLAN priority	* Investigate using High Priority Tx Queue with skb->priority	* Adjust Rx FIFO threshold and Max Rx DMA burst on Rx FIFO error	* Adjust Tx FIFO threshold and Max Tx DMA burst on Tx FIFO error        * Implement Tx software interrupt mitigation via	          Tx descriptor bit */#define DRV_NAME		"8139cp"#define DRV_VERSION		"0.0.6"#define DRV_RELDATE		"Nov 19, 2001"#include <linux/module.h>#include <linux/kernel.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/init.h>#include <linux/pci.h>#include <linux/delay.h>#include <linux/ethtool.h>#include <linux/mii.h>#include <asm/io.h>#include <asm/uaccess.h>/* These identify the driver base version and may not be removed. */static char version[] __devinitdata =KERN_INFO DRV_NAME " 10/100 PCI Ethernet driver v" DRV_VERSION " (" DRV_RELDATE ")\n";MODULE_AUTHOR("Jeff Garzik <jgarzik@mandrakesoft.com>");MODULE_DESCRIPTION("RealTek RTL-8139C+ series 10/100 PCI Ethernet driver");MODULE_LICENSE("GPL");static int debug = -1;MODULE_PARM (debug, "i");MODULE_PARM_DESC (debug, "8139cp bitmapped message enable number");/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).   The RTL chips use a 64 element hash table based on the Ethernet CRC.  */static int multicast_filter_limit = 32;MODULE_PARM (multicast_filter_limit, "i");MODULE_PARM_DESC (multicast_filter_limit, "8139cp maximum number of filtered multicast addresses");#define PFX			DRV_NAME ": "#define CP_DEF_MSG_ENABLE	(NETIF_MSG_DRV		| \				 NETIF_MSG_PROBE 	| \				 NETIF_MSG_LINK)#define CP_REGS_SIZE		(0xff + 1)#define CP_RX_RING_SIZE		64#define CP_TX_RING_SIZE		64#define CP_RING_BYTES		\		((sizeof(struct cp_desc) * CP_RX_RING_SIZE) +	\		(sizeof(struct cp_desc) * CP_TX_RING_SIZE))#define NEXT_TX(N)		(((N) + 1) & (CP_TX_RING_SIZE - 1))#define NEXT_RX(N)		(((N) + 1) & (CP_RX_RING_SIZE - 1))#define TX_BUFFS_AVAIL(CP)					\	(((CP)->tx_tail <= (CP)->tx_head) ?			\	  (CP)->tx_tail + (CP_TX_RING_SIZE - 1) - (CP)->tx_head :	\	  (CP)->tx_tail - (CP)->tx_head - 1)#define PKT_BUF_SZ		1536	/* Size of each temporary Rx buffer.*/#define RX_OFFSET		2#define CP_INTERNAL_PHY		32/* The following settings are log_2(bytes)-4:  0 == 16 bytes .. 6==1024, 7==end of packet. */#define RX_FIFO_THRESH		5	/* Rx buffer level before first PCI xfer.  */#define RX_DMA_BURST		4	/* Maximum PCI burst, '4' is 256 */#define TX_DMA_BURST		6	/* Maximum PCI burst, '6' is 1024 */#define TX_EARLY_THRESH		256	/* Early Tx threshold, in bytes *//* Time in jiffies before concluding the transmitter is hung. */#define TX_TIMEOUT  (6*HZ)enum {	/* NIC register offsets */	MAC0		= 0x00,	/* Ethernet hardware address. */	MAR0		= 0x08,	/* Multicast filter. */	TxRingAddr	= 0x20, /* 64-bit start addr of Tx ring */	HiTxRingAddr	= 0x28, /* 64-bit start addr of high priority Tx ring */	Cmd		= 0x37, /* Command register */	IntrMask	= 0x3C, /* Interrupt mask */	IntrStatus	= 0x3E, /* Interrupt status */	TxConfig	= 0x40, /* Tx configuration */	ChipVersion	= 0x43, /* 8-bit chip version, inside TxConfig */	RxConfig	= 0x44, /* Rx configuration */	Cfg9346		= 0x50, /* EEPROM select/control; Cfg reg [un]lock */	Config1		= 0x52, /* Config1 */	Config3		= 0x59, /* Config3 */	Config4		= 0x5A, /* Config4 */	MultiIntr	= 0x5C, /* Multiple interrupt select */	BasicModeCtrl	= 0x62,	/* MII BMCR */	BasicModeStatus	= 0x64, /* MII BMSR */	NWayAdvert	= 0x66, /* MII ADVERTISE */	NWayLPAR	= 0x68, /* MII LPA */	NWayExpansion	= 0x6A, /* MII Expansion */	Config5		= 0xD8,	/* Config5 */	TxPoll		= 0xD9,	/* Tell chip to check Tx descriptors for work */	CpCmd		= 0xE0, /* C+ Command register (C+ mode only) */	RxRingAddr	= 0xE4, /* 64-bit start addr of Rx ring */	TxThresh	= 0xEC, /* Early Tx threshold */	OldRxBufAddr	= 0x30, /* DMA address of Rx ring buffer (C mode) */	OldTSD0		= 0x10, /* DMA address of first Tx desc (C mode) */	/* Tx and Rx status descriptors */	DescOwn		= (1 << 31), /* Descriptor is owned by NIC */	RingEnd		= (1 << 30), /* End of descriptor ring */	FirstFrag	= (1 << 29), /* First segment of a packet */	LastFrag	= (1 << 28), /* Final segment of a packet */	TxError		= (1 << 23), /* Tx error summary */	RxError		= (1 << 20), /* Rx error summary */	IPCS		= (1 << 18), /* Calculate IP checksum */	UDPCS		= (1 << 17), /* Calculate UDP/IP checksum */	TCPCS		= (1 << 16), /* Calculate TCP/IP checksum */	IPFail		= (1 << 15), /* IP checksum failed */	UDPFail		= (1 << 14), /* UDP/IP checksum failed */	TCPFail		= (1 << 13), /* TCP/IP checksum failed */	NormalTxPoll	= (1 << 6),  /* One or more normal Tx packets to send */	PID1		= (1 << 17), /* 2 protocol id bits:  0==non-IP, */	PID0		= (1 << 16), /* 1==UDP/IP, 2==TCP/IP, 3==IP */	TxFIFOUnder	= (1 << 25), /* Tx FIFO underrun */	TxOWC		= (1 << 22), /* Tx Out-of-window collision */	TxLinkFail	= (1 << 21), /* Link failed during Tx of packet */	TxMaxCol	= (1 << 20), /* Tx aborted due to excessive collisions */	TxColCntShift	= 16,	     /* Shift, to get 4-bit Tx collision cnt */	TxColCntMask	= 0x01 | 0x02 | 0x04 | 0x08, /* 4-bit collision count */	RxErrFrame	= (1 << 27), /* Rx frame alignment error */	RxMcast		= (1 << 26), /* Rx multicast packet rcv'd */	RxErrCRC	= (1 << 18), /* Rx CRC error */	RxErrRunt	= (1 << 19), /* Rx error, packet < 64 bytes */	RxErrLong	= (1 << 21), /* Rx error, packet > 4096 bytes */	RxErrFIFO	= (1 << 22), /* Rx error, FIFO overflowed, pkt bad */	/* RxConfig register */	RxCfgFIFOShift	= 13,	     /* Shift, to get Rx FIFO thresh value */	RxCfgDMAShift	= 8,	     /* Shift, to get Rx Max DMA value */	AcceptErr	= 0x20,	     /* Accept packets with CRC errors */	AcceptRunt	= 0x10,	     /* Accept runt (<64 bytes) packets */	AcceptBroadcast	= 0x08,	     /* Accept broadcast packets */	AcceptMulticast	= 0x04,	     /* Accept multicast packets */	AcceptMyPhys	= 0x02,	     /* Accept pkts with our MAC as dest */	AcceptAllPhys	= 0x01,	     /* Accept all pkts w/ physical dest */	/* IntrMask / IntrStatus registers */	PciErr		= (1 << 15), /* System error on the PCI bus */	TimerIntr	= (1 << 14), /* Asserted when TCTR reaches TimerInt value */	LenChg		= (1 << 13), /* Cable length change */	SWInt		= (1 << 8),  /* Software-requested interrupt */	TxEmpty		= (1 << 7),  /* No Tx descriptors available */	RxFIFOOvr	= (1 << 6),  /* Rx FIFO Overflow */	LinkChg		= (1 << 5),  /* Packet underrun, or link change */	RxEmpty		= (1 << 4),  /* No Rx descriptors available */	TxErr		= (1 << 3),  /* Tx error */	TxOK		= (1 << 2),  /* Tx packet sent */	RxErr		= (1 << 1),  /* Rx error */	RxOK		= (1 << 0),  /* Rx packet received */	IntrResvd	= (1 << 10), /* reserved, according to RealTek engineers,					but hardware likes to raise it */	IntrAll		= PciErr | TimerIntr | LenChg | SWInt | TxEmpty |			  RxFIFOOvr | LinkChg | RxEmpty | TxErr | TxOK |			  RxErr | RxOK | IntrResvd,	/* C mode command register */	CmdReset	= (1 << 4),  /* Enable to reset; self-clearing */	RxOn		= (1 << 3),  /* Rx mode enable */	TxOn		= (1 << 2),  /* Tx mode enable */	/* C+ mode command register */	RxChkSum	= (1 << 5),  /* Rx checksum offload enable */	PCIMulRW	= (1 << 3),  /* Enable PCI read/write multiple */	CpRxOn		= (1 << 1),  /* Rx mode enable */	CpTxOn		= (1 << 0),  /* Tx mode enable */	/* Cfg9436 EEPROM control register */	Cfg9346_Lock	= 0x00,	     /* Lock ConfigX/MII register access */	Cfg9346_Unlock	= 0xC0,	     /* Unlock ConfigX/MII register access */	/* TxConfig register */	IFG		= (1 << 25) | (1 << 24), /* standard IEEE interframe gap */	TxDMAShift	= 8,	     /* DMA burst value (0-7) is shift this many bits */	/* Early Tx Threshold register */	TxThreshMask	= 0x3f,	     /* Mask bits 5-0 */	TxThreshMax	= 2048,	     /* Max early Tx threshold */	/* Config1 register */	DriverLoaded	= (1 << 5),  /* Software marker, driver is loaded */	PMEnable	= (1 << 0),  /* Enable various PM features of chip */	/* Config3 register */	PARMEnable	= (1 << 6),  /* Enable auto-loading of PHY parms */	/* Config5 register */	PMEStatus	= (1 << 0),  /* PME status can be reset by PCI RST# */};static const unsigned int cp_intr_mask =	PciErr | LinkChg |	RxOK | RxErr | RxEmpty | RxFIFOOvr |	TxOK | TxErr | TxEmpty;static const unsigned int cp_rx_config =	  (RX_FIFO_THRESH << RxCfgFIFOShift) |	  (RX_DMA_BURST << RxCfgDMAShift);struct cp_desc {	u32		opts1;	u32		opts2;	u32		addr_lo;	u32		addr_hi;};struct ring_info {	struct sk_buff		*skb;	dma_addr_t		mapping;	unsigned		frag;};struct cp_extra_stats {	unsigned long		rx_frags;};struct cp_private {	unsigned		tx_head;	unsigned		tx_tail;	unsigned		rx_tail;	void			*regs;	struct net_device	*dev;	spinlock_t		lock;	struct cp_desc		*rx_ring;	struct cp_desc		*tx_ring;	struct ring_info	tx_skb[CP_TX_RING_SIZE];	struct ring_info	rx_skb[CP_RX_RING_SIZE];	unsigned		rx_buf_sz;	dma_addr_t		ring_dma;	u32			msg_enable;	struct net_device_stats net_stats;	struct cp_extra_stats	cp_stats;	struct pci_dev		*pdev;	u32			rx_config;	struct sk_buff		*frag_skb;	unsigned		dropping_frag : 1;	struct mii_if_info	mii_if;};#define cpr8(reg)	readb(cp->regs + (reg))#define cpr16(reg)	readw(cp->regs + (reg))#define cpr32(reg)	readl(cp->regs + (reg))#define cpw8(reg,val)	writeb((val), cp->regs + (reg))#define cpw16(reg,val)	writew((val), cp->regs + (reg))#define cpw32(reg,val)	writel((val), cp->regs + (reg))#define cpw8_f(reg,val) do {			\	writeb((val), cp->regs + (reg));	\	readb(cp->regs + (reg));		\	} while (0)#define cpw16_f(reg,val) do {			\	writew((val), cp->regs + (reg));	\	readw(cp->regs + (reg));		\	} while (0)#define cpw32_f(reg,val) do {			\	writel((val), cp->regs + (reg));	\	readl(cp->regs + (reg));		\	} while (0)static void __cp_set_rx_mode (struct net_device *dev);static void cp_tx (struct cp_private *cp);static void cp_clean_rings (struct cp_private *cp);static struct pci_device_id cp_pci_tbl[] __devinitdata = {	{ PCI_VENDOR_ID_REALTEK, PCI_DEVICE_ID_REALTEK_8139,	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ },};MODULE_DEVICE_TABLE(pci, cp_pci_tbl);static inline void cp_rx_skb (struct cp_private *cp, struct sk_buff *skb){	skb->protocol = eth_type_trans (skb, cp->dev);	cp->net_stats.rx_packets++;	cp->net_stats.rx_bytes += skb->len;	cp->dev->last_rx = jiffies;	netif_rx (skb);}static void cp_rx_err_acct (struct cp_private *cp, unsigned rx_tail,			    u32 status, u32 len){	if (netif_msg_rx_err (cp))		printk (KERN_DEBUG			"%s: rx err, slot %d status 0x%x len %d\n",			cp->dev->name, rx_tail, status, len);	cp->net_stats.rx_errors++;	if (status & RxErrFrame)		cp->net_stats.rx_frame_errors++;	if (status & RxErrCRC)		cp->net_stats.rx_crc_errors++;	if (status & RxErrRunt)		cp->net_stats.rx_length_errors++;	if (status & RxErrLong)		cp->net_stats.rx_length_errors++;	if (status & RxErrFIFO)		cp->net_stats.rx_fifo_errors++;}static void cp_rx_frag (struct cp_private *cp, unsigned rx_tail,			struct sk_buff *skb, u32 status, u32 len){	struct sk_buff *copy_skb, *frag_skb = cp->frag_skb;	unsigned orig_len = frag_skb ? frag_skb->len : 0;	unsigned target_len = orig_len + len;	unsigned first_frag = status & FirstFrag;	unsigned last_frag = status & LastFrag;	if (netif_msg_rx_status (cp))		printk (KERN_DEBUG "%s: rx %s%sfrag, slot %d status 0x%x len %d\n",			cp->dev->name,			cp->dropping_frag ? "dropping " : "",			first_frag ? "first " :			last_frag ? "last " : "",			rx_tail, status, len);	cp->cp_stats.rx_frags++;	if (!frag_skb && !first_frag)		cp->dropping_frag = 1;	if (cp->dropping_frag)		goto drop_frag;	copy_skb = dev_alloc_skb (target_len + RX_OFFSET);	if (!copy_skb) {		printk(KERN_WARNING "%s: rx slot %d alloc failed\n",		       cp->dev->name, rx_tail);		cp->dropping_frag = 1;drop_frag:		if (frag_skb) {			dev_kfree_skb_irq(frag_skb);			cp->frag_skb = NULL;		}		if (last_frag) {			cp->net_stats.rx_dropped++;			cp->dropping_frag = 0;		}		return;	}	copy_skb->dev = cp->dev;	skb_reserve(copy_skb, RX_OFFSET);	skb_put(copy_skb, target_len);	if (frag_skb) {		memcpy(copy_skb->data, frag_skb->data, orig_len);		dev_kfree_skb_irq(frag_skb);	}	pci_dma_sync_single(cp->pdev, cp->rx_skb[rx_tail].mapping,			    len, PCI_DMA_FROMDEVICE);	memcpy(copy_skb->data + orig_len, skb->data, len);	copy_skb->ip_summed = CHECKSUM_NONE;	if (last_frag) {		if (status & (RxError | RxErrFIFO)) {			cp_rx_err_acct(cp, rx_tail, status, len);			dev_kfree_skb_irq(copy_skb);		} else			cp_rx_skb(cp, copy_skb);		cp->frag_skb = NULL;	} else {		cp->frag_skb = copy_skb;	}}static void cp_rx (struct cp_private *cp){	unsigned rx_tail = cp->rx_tail;	unsigned rx_work = 100;	while (rx_work--) {		u32 status, len;		dma_addr_t mapping;		struct sk_buff *skb, *new_skb;		unsigned buflen;		skb = cp->rx_skb[rx_tail].skb;		if (!skb)			BUG();		rmb();		status = le32_to_cpu(cp->rx_ring[rx_tail].opts1);		if (status & DescOwn)			break;		len = (status & 0x1fff) - 4;		mapping = cp->rx_skb[rx_tail].mapping;		if ((status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag)) {			cp_rx_frag(cp, rx_tail, skb, status, len);			goto rx_next;		}		if (status & (RxError | RxErrFIFO)) {			cp_rx_err_acct(cp, rx_tail, status, len);			goto rx_next;		}		if (netif_msg_rx_status(cp))			printk(KERN_DEBUG "%s: rx slot %d status 0x%x len %d\n",			       cp->dev->name, rx_tail, status, len);		buflen = cp->rx_buf_sz + RX_OFFSET;		new_skb = dev_alloc_skb (buflen);		if (!new_skb) {			cp->net_stats.rx_dropped++;			goto rx_next;		}		skb_reserve(new_skb, RX_OFFSET);		new_skb->dev = cp->dev;		pci_unmap_single(cp->pdev, mapping,				 buflen, PCI_DMA_FROMDEVICE);		skb->ip_summed = CHECKSUM_NONE;		skb_put(skb, len);

⌨️ 快捷键说明

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