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

📄 8139too.c

📁 rtl8139网卡
💻 C
📖 第 1 页 / 共 5 页
字号:
/*	8139too.c: A RealTek RTL-8139 Fast Ethernet driver for Linux.	Maintained by Jeff Garzik <jgarzik@pobox.com>	Copyright 2000-2002 Jeff Garzik	Much code comes from Donald Becker's rtl8139.c driver,	versions 1.13 and older.  This driver was originally based	on rtl8139.c version 1.07.  Header of rtl8139.c version 1.13:	-----<snip>-----        	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 boards based on the RTL8129 and RTL8139		PCI ethernet chips.		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/rtl8139.html		Twister-tuning table provided by Kinston		<shangh@realtek.com.tw>.	-----<snip>-----	This software may be used and distributed according to the terms	of the GNU General Public License, incorporated herein by reference.	Contributors:		Donald Becker - he wrote the original driver, kudos to him!		(but please don't e-mail him for support, this isn't his driver)		Tigran Aivazian - bug fixes, skbuff free cleanup		Martin Mares - suggestions for PCI cleanup		David S. Miller - PCI DMA and softnet updates		Ernst Gill - fixes ported from BSD driver		Daniel Kobras - identified specific locations of			posted MMIO write bugginess		Gerard Sharp - bug fix, testing and feedback		David Ford - Rx ring wrap fix		Dan DeMaggio - swapped RTL8139 cards with me, and allowed me		to find and fix a crucial bug on older chipsets.		Donald Becker/Chris Butterworth/Marcus Westergren -		Noticed various Rx packet size-related buglets.		Santiago Garcia Mantinan - testing and feedback		Jens David - 2.2.x kernel backports		Martin Dennett - incredibly helpful insight on undocumented		features of the 8139 chips		Jean-Jacques Michel - bug fix		Tobias Ringstr鰉 - Rx interrupt status checking suggestion		Andrew Morton - Clear blocked signals, avoid		buffer overrun setting current->comm.		Kalle Olavi Niemitalo - Wake-on-LAN ioctls		Robert Kuebel - Save kernel thread from dying on any signal.	Submitting bug reports:		"rtl8139-diag -mmmaaavvveefN" output		enable RTL8139_DEBUG below, and look at 'dmesg' or kernel log*/#define DRV_NAME	"8139too"#define DRV_VERSION	"0.9.28"#include <linux/module.h>#include <linux/kernel.h>#include <linux/compiler.h>#include <linux/pci.h>#include <linux/init.h>#include <linux/ioport.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/rtnetlink.h>#include <linux/delay.h>#include <linux/ethtool.h>#include <linux/mii.h>#include <linux/completion.h>#include <linux/crc32.h>#include <asm/io.h>#include <asm/uaccess.h>#include <asm/irq.h>#define RTL8139_DRIVER_NAME   DRV_NAME " Fast Ethernet driver " DRV_VERSION#define PFX DRV_NAME ": "/* Default Message level */#define RTL8139_DEF_MSG_ENABLE   (NETIF_MSG_DRV   | \                                 NETIF_MSG_PROBE  | \                                 NETIF_MSG_LINK)/* enable PIO instead of MMIO, if CONFIG_8139TOO_PIO is selected */#ifdef CONFIG_8139TOO_PIO#define USE_IO_OPS 1#endif/* define to 1, 2 or 3 to enable copious debugging info */#define RTL8139_DEBUG 0/* define to 1 to disable lightweight runtime debugging checks */#undef RTL8139_NDEBUG#if RTL8139_DEBUG/* note: prints function name for you */#  define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)#else#  define DPRINTK(fmt, args...)#endif#ifdef RTL8139_NDEBUG#  define assert(expr) do {} while (0)#else#  define assert(expr) \        if(unlikely(!(expr))) {				        \        printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n",	\        #expr,__FILE__,__FUNCTION__,__LINE__);		        \        }#endif/* A few user-configurable values. *//* media options */#define MAX_UNITS 8static int media[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};/* 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;/* bitmapped message enable number */static int debug = -1;/* * Receive ring size * Warning: 64K ring has hardware issues and may lock up. */#if defined(CONFIG_SH_DREAMCAST)#define RX_BUF_IDX	1	/* 16K ring */#else#define RX_BUF_IDX	2	/* 32K ring */#endif#define RX_BUF_LEN	(8192 << RX_BUF_IDX)#define RX_BUF_PAD	16#define RX_BUF_WRAP_PAD 2048 /* spare padding to handle lack of packet wrap */#if RX_BUF_LEN == 65536#define RX_BUF_TOT_LEN	RX_BUF_LEN#else#define RX_BUF_TOT_LEN	(RX_BUF_LEN + RX_BUF_PAD + RX_BUF_WRAP_PAD)#endif/* Number of Tx descriptor registers. */#define NUM_TX_DESC	4/* max supported ethernet frame size -- must be at least (dev->mtu+14+4).*/#define MAX_ETH_FRAME_SIZE	1536/* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */#define TX_BUF_SIZE	MAX_ETH_FRAME_SIZE#define TX_BUF_TOT_LEN	(TX_BUF_SIZE * NUM_TX_DESC)/* PCI Tuning Parameters   Threshold is bytes transferred to chip before transmission starts. */#define TX_FIFO_THRESH 256	/* In bytes, rounded down to 32 byte units. *//* The following settings are log_2(bytes)-4:  0 == 16 bytes .. 6==1024, 7==end of packet. */#define RX_FIFO_THRESH	7	/* Rx buffer level before first PCI xfer.  */#define RX_DMA_BURST	7	/* Maximum PCI burst, '6' is 1024 */#define TX_DMA_BURST	6	/* Maximum PCI burst, '6' is 1024 */#define TX_RETRY	8	/* 0-15.  retries = 16 + (TX_RETRY * 16) *//* Operational parameters that usually are not changed. *//* Time in jiffies before concluding the transmitter is hung. */#define TX_TIMEOUT  (6*HZ)enum {	HAS_MII_XCVR = 0x010000,	HAS_CHIP_XCVR = 0x020000,	HAS_LNK_CHNG = 0x040000,};#define RTL_NUM_STATS 4		/* number of ETHTOOL_GSTATS u64's */#define RTL_REGS_VER 1		/* version of reg. data in ETHTOOL_GREGS */#define RTL_MIN_IO_SIZE 0x80#define RTL8139B_IO_SIZE 256#define RTL8129_CAPS	HAS_MII_XCVR#define RTL8139_CAPS	HAS_CHIP_XCVR|HAS_LNK_CHNGtypedef enum {	RTL8139 = 0,	RTL8129,} board_t;/* indexed by board_t, above */static const struct {	const char *name;	u32 hw_flags;} board_info[] __devinitdata = {	{ "RealTek RTL8139", RTL8139_CAPS },	{ "RealTek RTL8129", RTL8129_CAPS },};static struct pci_device_id rtl8139_pci_tbl[] = {	{0x10ec, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x10ec, 0x8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x1113, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x1500, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x4033, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x1186, 0x1300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x1186, 0x1340, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x13d1, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x1259, 0xa117, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x1259, 0xa11e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x14ea, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x14ea, 0xab07, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x11db, 0x1234, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x1432, 0x9130, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x02ac, 0x1012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x018a, 0x0106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x126c, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x1743, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },	{0x021b, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },#ifdef CONFIG_SH_SECUREEDGE5410	/* Bogus 8139 silicon reports 8129 without external PROM :-( */	{0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },#endif#ifdef CONFIG_8139TOO_8129	{0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8129 },#endif	/* some crazy cards report invalid vendor ids like	 * 0x0001 here.  The other ids are valid and constant,	 * so we simply don't match on the main vendor id.	 */	{PCI_ANY_ID, 0x8139, 0x10ec, 0x8139, 0, 0, RTL8139 },	{PCI_ANY_ID, 0x8139, 0x1186, 0x1300, 0, 0, RTL8139 },	{PCI_ANY_ID, 0x8139, 0x13d1, 0xab06, 0, 0, RTL8139 },	{0,}};MODULE_DEVICE_TABLE (pci, rtl8139_pci_tbl);static struct {	const char str[ETH_GSTRING_LEN];} ethtool_stats_keys[] = {	{ "early_rx" },	{ "tx_buf_mapped" },	{ "tx_timeouts" },	{ "rx_lost_in_ring" },};/* The rest of these values should never change. *//* Symbolic offsets to registers. */enum RTL8139_registers {	MAC0 = 0,		/* Ethernet hardware address. */	MAR0 = 8,		/* Multicast filter. */	TxStatus0 = 0x10,	/* Transmit status (Four 32bit registers). */	TxAddr0 = 0x20,		/* Tx descriptors (also four 32bit). */	RxBuf = 0x30,	ChipCmd = 0x37,	RxBufPtr = 0x38,	RxBufAddr = 0x3A,	IntrMask = 0x3C,	IntrStatus = 0x3E,	TxConfig = 0x40,	RxConfig = 0x44,	Timer = 0x48,		/* A general-purpose counter. */	RxMissed = 0x4C,	/* 24 bits valid, write clears. */	Cfg9346 = 0x50,	Config0 = 0x51,	Config1 = 0x52,	FlashReg = 0x54,	MediaStatus = 0x58,	Config3 = 0x59,	Config4 = 0x5A,		/* absent on RTL-8139A */	HltClk = 0x5B,	MultiIntr = 0x5C,	TxSummary = 0x60,	BasicModeCtrl = 0x62,	BasicModeStatus = 0x64,	NWayAdvert = 0x66,	NWayLPAR = 0x68,	NWayExpansion = 0x6A,	/* Undocumented registers, but required for proper operation. */	FIFOTMS = 0x70,		/* FIFO Control and test. */	CSCR = 0x74,		/* Chip Status and Configuration Register. */	PARA78 = 0x78,	PARA7c = 0x7c,		/* Magic transceiver parameter register. */	Config5 = 0xD8,		/* absent on RTL-8139A */};enum ClearBitMasks {	MultiIntrClear = 0xF000,	ChipCmdClear = 0xE2,	Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),};enum ChipCmdBits {	CmdReset = 0x10,	CmdRxEnb = 0x08,	CmdTxEnb = 0x04,	RxBufEmpty = 0x01,};/* Interrupt register bits, using my own meaningful names. */enum IntrStatusBits {	PCIErr = 0x8000,	PCSTimeout = 0x4000,	RxFIFOOver = 0x40,	RxUnderrun = 0x20,	RxOverflow = 0x10,	TxErr = 0x08,	TxOK = 0x04,	RxErr = 0x02,	RxOK = 0x01,	RxAckBits = RxFIFOOver | RxOverflow | RxOK,};enum TxStatusBits {	TxHostOwns = 0x2000,	TxUnderrun = 0x4000,	TxStatOK = 0x8000,	TxOutOfWindow = 0x20000000,	TxAborted = 0x40000000,	TxCarrierLost = 0x80000000,};enum RxStatusBits {	RxMulticast = 0x8000,	RxPhysical = 0x4000,	RxBroadcast = 0x2000,	RxBadSymbol = 0x0020,	RxRunt = 0x0010,	RxTooLong = 0x0008,	RxCRCErr = 0x0004,	RxBadAlign = 0x0002,	RxStatusOK = 0x0001,};/* Bits in RxConfig. */enum rx_mode_bits {	AcceptErr = 0x20,	AcceptRunt = 0x10,	AcceptBroadcast = 0x08,	AcceptMulticast = 0x04,	AcceptMyPhys = 0x02,	AcceptAllPhys = 0x01,};/* Bits in TxConfig. */enum tx_config_bits {        /* Interframe Gap Time. Only TxIFG96 doesn't violate IEEE 802.3 */        TxIFGShift = 24,        TxIFG84 = (0 << TxIFGShift),    /* 8.4us / 840ns (10 / 100Mbps) */        TxIFG88 = (1 << TxIFGShift),    /* 8.8us / 880ns (10 / 100Mbps) */        TxIFG92 = (2 << TxIFGShift),    /* 9.2us / 920ns (10 / 100Mbps) */        TxIFG96 = (3 << TxIFGShift),    /* 9.6us / 960ns (10 / 100Mbps) */	TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */	TxCRC = (1 << 16),	/* DISABLE appending CRC to end of Tx packets */	TxClearAbt = (1 << 0),	/* Clear abort (WO) */	TxDMAShift = 8,		/* DMA burst value (0-7) is shifted this many bits */	TxRetryShift = 4,	/* TXRR value (0-15) is shifted this many bits */	TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */};/* Bits in Config1 */enum Config1Bits {	Cfg1_PM_Enable = 0x01,	Cfg1_VPD_Enable = 0x02,	Cfg1_PIO = 0x04,	Cfg1_MMIO = 0x08,	LWAKE = 0x10,		/* not on 8139, 8139A */	Cfg1_Driver_Load = 0x20,	Cfg1_LED0 = 0x40,	Cfg1_LED1 = 0x80,	SLEEP = (1 << 1),	/* only on 8139, 8139A */	PWRDN = (1 << 0),	/* only on 8139, 8139A */};/* Bits in Config3 */enum Config3Bits {	Cfg3_FBtBEn    = (1 << 0), /* 1 = Fast Back to Back */	Cfg3_FuncRegEn = (1 << 1), /* 1 = enable CardBus Function registers */	Cfg3_CLKRUN_En = (1 << 2), /* 1 = enable CLKRUN */	Cfg3_CardB_En  = (1 << 3), /* 1 = enable CardBus registers */	Cfg3_LinkUp    = (1 << 4), /* 1 = wake up on link up */	Cfg3_Magic     = (1 << 5), /* 1 = wake up on Magic Packet (tm) */	Cfg3_PARM_En   = (1 << 6), /* 0 = software can set twister parameters */	Cfg3_GNTSel    = (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */};/* Bits in Config4 */enum Config4Bits {	LWPTN = (1 << 2),	/* not on 8139, 8139A */};/* Bits in Config5 */enum Config5Bits {	Cfg5_PME_STS     = (1 << 0), /* 1 = PCI reset resets PME_Status */	Cfg5_LANWake     = (1 << 1), /* 1 = enable LANWake signal */	Cfg5_LDPS        = (1 << 2), /* 0 = save power when link is down */	Cfg5_FIFOAddrPtr = (1 << 3), /* Realtek internal SRAM testing */	Cfg5_UWF         = (1 << 4), /* 1 = accept unicast wakeup frame */	Cfg5_MWF         = (1 << 5), /* 1 = accept multicast wakeup frame */	Cfg5_BWF         = (1 << 6), /* 1 = accept broadcast wakeup frame */};enum RxConfigBits {	/* rx fifo threshold */	RxCfgFIFOShift = 13,	RxCfgFIFONone = (7 << RxCfgFIFOShift),	/* Max DMA burst */	RxCfgDMAShift = 8,	RxCfgDMAUnlimited = (7 << RxCfgDMAShift),	/* rx ring buffer length */	RxCfgRcv8K = 0,	RxCfgRcv16K = (1 << 11),	RxCfgRcv32K = (1 << 12),	RxCfgRcv64K = (1 << 11) | (1 << 12),	/* Disable packet wrap at end of Rx buffer. (not possible with 64k) */	RxNoWrap = (1 << 7),};/* Twister tuning parameters from RealTek.   Completely undocumented, but required to tune bad links on some boards. */enum CSCRBits {	CSCR_LinkOKBit = 0x0400,	CSCR_LinkChangeBit = 0x0800,	CSCR_LinkStatusBits = 0x0f000,	CSCR_LinkDownOffCmd = 0x003c0,	CSCR_LinkDownCmd = 0x0f3c0,};enum Cfg9346Bits {	Cfg9346_Lock = 0x00,	Cfg9346_Unlock = 0xC0,};typedef enum {	CH_8139 = 0,

⌨️ 快捷键说明

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