devices/r8169-3.2-ethercat.c
changeset 2403 5cbd10256e12
child 2582 87e502828b3f
equal deleted inserted replaced
2402:1a450b095f3b 2403:5cbd10256e12
       
     1 /*
       
     2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
       
     3 *
       
     4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
       
     5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
       
     6 * Copyright (c) a lot of people too. Please respect their work.
       
     7  *
       
     8  * See MAINTAINERS file for support contact information.
       
     9  */
       
    10 
       
    11 #include <linux/module.h>
       
    12 #include <linux/moduleparam.h>
       
    13 #include <linux/pci.h>
       
    14 #include <linux/netdevice.h>
       
    15 #include <linux/etherdevice.h>
       
    16 #include <linux/delay.h>
       
    17 #include <linux/ethtool.h>
       
    18 #include <linux/mii.h>
       
    19 #include <linux/if_vlan.h>
       
    20 #include <linux/crc32.h>
       
    21 #include <linux/in.h>
       
    22 #include <linux/ip.h>
       
    23 #include <linux/tcp.h>
       
    24 #include <linux/init.h>
       
    25 #include <linux/interrupt.h>
       
    26 #include <linux/dma-mapping.h>
       
    27 #include <linux/pm_runtime.h>
       
    28 #include <linux/firmware.h>
       
    29 #include <linux/pci-aspm.h>
       
    30 #include <linux/prefetch.h>
       
    31 
       
    32 #include <asm/system.h>
       
    33 #include <asm/io.h>
       
    34 #include <asm/irq.h>
       
    35 #include "../globals.h"
       
    36 #include "ecdev.h"
       
    37 
       
    38 #define RTL8169_VERSION "2.3LK-NAPI"
       
    39 #define MODULENAME "ec_r8169"
       
    40 #define PFX MODULENAME ": "
       
    41 
       
    42 #define FIRMWARE_8168D_1	"rtl_nic/rtl8168d-1.fw"
       
    43 #define FIRMWARE_8168D_2	"rtl_nic/rtl8168d-2.fw"
       
    44 #define FIRMWARE_8168E_1	"rtl_nic/rtl8168e-1.fw"
       
    45 #define FIRMWARE_8168E_2	"rtl_nic/rtl8168e-2.fw"
       
    46 #define FIRMWARE_8168E_3	"rtl_nic/rtl8168e-3.fw"
       
    47 #define FIRMWARE_8168F_1	"rtl_nic/rtl8168f-1.fw"
       
    48 #define FIRMWARE_8168F_2	"rtl_nic/rtl8168f-2.fw"
       
    49 #define FIRMWARE_8105E_1	"rtl_nic/rtl8105e-1.fw"
       
    50 
       
    51 #ifdef RTL8169_DEBUG
       
    52 #define assert(expr) \
       
    53 	if (!(expr)) {					\
       
    54 		printk( "Assertion failed! %s,%s,%s,line=%d\n",	\
       
    55 		#expr,__FILE__,__func__,__LINE__);		\
       
    56 	}
       
    57 #define dprintk(fmt, args...) \
       
    58 	do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
       
    59 #else
       
    60 #define assert(expr) do {} while (0)
       
    61 #define dprintk(fmt, args...)	do {} while (0)
       
    62 #endif /* RTL8169_DEBUG */
       
    63 
       
    64 #define R8169_MSG_DEFAULT \
       
    65 	(NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
       
    66 
       
    67 #define TX_BUFFS_AVAIL(tp) \
       
    68 	(tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
       
    69 
       
    70 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
       
    71    The RTL chips use a 64 element hash table based on the Ethernet CRC. */
       
    72 static const int multicast_filter_limit = 32;
       
    73 
       
    74 /* MAC address length */
       
    75 #define MAC_ADDR_LEN	6
       
    76 
       
    77 #define MAX_READ_REQUEST_SHIFT	12
       
    78 #define TX_DMA_BURST	6	/* Maximum PCI burst, '6' is 1024 */
       
    79 #define SafeMtu		0x1c20	/* ... actually life sucks beyond ~7k */
       
    80 #define InterFrameGap	0x03	/* 3 means InterFrameGap = the shortest one */
       
    81 
       
    82 #define R8169_REGS_SIZE		256
       
    83 #define R8169_NAPI_WEIGHT	64
       
    84 #define NUM_TX_DESC	64	/* Number of Tx descriptor registers */
       
    85 #define NUM_RX_DESC	256	/* Number of Rx descriptor registers */
       
    86 #define RX_BUF_SIZE	1536	/* Rx Buffer size */
       
    87 #define R8169_TX_RING_BYTES	(NUM_TX_DESC * sizeof(struct TxDesc))
       
    88 #define R8169_RX_RING_BYTES	(NUM_RX_DESC * sizeof(struct RxDesc))
       
    89 
       
    90 #define RTL8169_TX_TIMEOUT	(6*HZ)
       
    91 #define RTL8169_PHY_TIMEOUT	(10*HZ)
       
    92 
       
    93 #define RTL_EEPROM_SIG		cpu_to_le32(0x8129)
       
    94 #define RTL_EEPROM_SIG_MASK	cpu_to_le32(0xffff)
       
    95 #define RTL_EEPROM_SIG_ADDR	0x0000
       
    96 
       
    97 /* write/read MMIO register */
       
    98 #define RTL_W8(reg, val8)	writeb ((val8), ioaddr + (reg))
       
    99 #define RTL_W16(reg, val16)	writew ((val16), ioaddr + (reg))
       
   100 #define RTL_W32(reg, val32)	writel ((val32), ioaddr + (reg))
       
   101 #define RTL_R8(reg)		readb (ioaddr + (reg))
       
   102 #define RTL_R16(reg)		readw (ioaddr + (reg))
       
   103 #define RTL_R32(reg)		readl (ioaddr + (reg))
       
   104 
       
   105 enum mac_version {
       
   106 	RTL_GIGA_MAC_VER_01 = 0,
       
   107 	RTL_GIGA_MAC_VER_02,
       
   108 	RTL_GIGA_MAC_VER_03,
       
   109 	RTL_GIGA_MAC_VER_04,
       
   110 	RTL_GIGA_MAC_VER_05,
       
   111 	RTL_GIGA_MAC_VER_06,
       
   112 	RTL_GIGA_MAC_VER_07,
       
   113 	RTL_GIGA_MAC_VER_08,
       
   114 	RTL_GIGA_MAC_VER_09,
       
   115 	RTL_GIGA_MAC_VER_10,
       
   116 	RTL_GIGA_MAC_VER_11,
       
   117 	RTL_GIGA_MAC_VER_12,
       
   118 	RTL_GIGA_MAC_VER_13,
       
   119 	RTL_GIGA_MAC_VER_14,
       
   120 	RTL_GIGA_MAC_VER_15,
       
   121 	RTL_GIGA_MAC_VER_16,
       
   122 	RTL_GIGA_MAC_VER_17,
       
   123 	RTL_GIGA_MAC_VER_18,
       
   124 	RTL_GIGA_MAC_VER_19,
       
   125 	RTL_GIGA_MAC_VER_20,
       
   126 	RTL_GIGA_MAC_VER_21,
       
   127 	RTL_GIGA_MAC_VER_22,
       
   128 	RTL_GIGA_MAC_VER_23,
       
   129 	RTL_GIGA_MAC_VER_24,
       
   130 	RTL_GIGA_MAC_VER_25,
       
   131 	RTL_GIGA_MAC_VER_26,
       
   132 	RTL_GIGA_MAC_VER_27,
       
   133 	RTL_GIGA_MAC_VER_28,
       
   134 	RTL_GIGA_MAC_VER_29,
       
   135 	RTL_GIGA_MAC_VER_30,
       
   136 	RTL_GIGA_MAC_VER_31,
       
   137 	RTL_GIGA_MAC_VER_32,
       
   138 	RTL_GIGA_MAC_VER_33,
       
   139 	RTL_GIGA_MAC_VER_34,
       
   140 	RTL_GIGA_MAC_VER_35,
       
   141 	RTL_GIGA_MAC_VER_36,
       
   142 	RTL_GIGA_MAC_NONE   = 0xff,
       
   143 };
       
   144 
       
   145 enum rtl_tx_desc_version {
       
   146 	RTL_TD_0	= 0,
       
   147 	RTL_TD_1	= 1,
       
   148 };
       
   149 
       
   150 #define JUMBO_1K	ETH_DATA_LEN
       
   151 #define JUMBO_4K	(4*1024 - ETH_HLEN - 2)
       
   152 #define JUMBO_6K	(6*1024 - ETH_HLEN - 2)
       
   153 #define JUMBO_7K	(7*1024 - ETH_HLEN - 2)
       
   154 #define JUMBO_9K	(9*1024 - ETH_HLEN - 2)
       
   155 
       
   156 #define _R(NAME,TD,FW,SZ,B) {	\
       
   157 	.name = NAME,		\
       
   158 	.txd_version = TD,	\
       
   159 	.fw_name = FW,		\
       
   160 	.jumbo_max = SZ,	\
       
   161 	.jumbo_tx_csum = B	\
       
   162 }
       
   163 
       
   164 static const struct {
       
   165 	const char *name;
       
   166 	enum rtl_tx_desc_version txd_version;
       
   167 	const char *fw_name;
       
   168 	u16 jumbo_max;
       
   169 	bool jumbo_tx_csum;
       
   170 } rtl_chip_infos[] = {
       
   171 	/* PCI devices. */
       
   172 	[RTL_GIGA_MAC_VER_01] =
       
   173 		_R("RTL8169",		RTL_TD_0, NULL, JUMBO_7K, true),
       
   174 	[RTL_GIGA_MAC_VER_02] =
       
   175 		_R("RTL8169s",		RTL_TD_0, NULL, JUMBO_7K, true),
       
   176 	[RTL_GIGA_MAC_VER_03] =
       
   177 		_R("RTL8110s",		RTL_TD_0, NULL, JUMBO_7K, true),
       
   178 	[RTL_GIGA_MAC_VER_04] =
       
   179 		_R("RTL8169sb/8110sb",	RTL_TD_0, NULL, JUMBO_7K, true),
       
   180 	[RTL_GIGA_MAC_VER_05] =
       
   181 		_R("RTL8169sc/8110sc",	RTL_TD_0, NULL, JUMBO_7K, true),
       
   182 	[RTL_GIGA_MAC_VER_06] =
       
   183 		_R("RTL8169sc/8110sc",	RTL_TD_0, NULL, JUMBO_7K, true),
       
   184 	/* PCI-E devices. */
       
   185 	[RTL_GIGA_MAC_VER_07] =
       
   186 		_R("RTL8102e",		RTL_TD_1, NULL, JUMBO_1K, true),
       
   187 	[RTL_GIGA_MAC_VER_08] =
       
   188 		_R("RTL8102e",		RTL_TD_1, NULL, JUMBO_1K, true),
       
   189 	[RTL_GIGA_MAC_VER_09] =
       
   190 		_R("RTL8102e",		RTL_TD_1, NULL, JUMBO_1K, true),
       
   191 	[RTL_GIGA_MAC_VER_10] =
       
   192 		_R("RTL8101e",		RTL_TD_0, NULL, JUMBO_1K, true),
       
   193 	[RTL_GIGA_MAC_VER_11] =
       
   194 		_R("RTL8168b/8111b",	RTL_TD_0, NULL, JUMBO_4K, false),
       
   195 	[RTL_GIGA_MAC_VER_12] =
       
   196 		_R("RTL8168b/8111b",	RTL_TD_0, NULL, JUMBO_4K, false),
       
   197 	[RTL_GIGA_MAC_VER_13] =
       
   198 		_R("RTL8101e",		RTL_TD_0, NULL, JUMBO_1K, true),
       
   199 	[RTL_GIGA_MAC_VER_14] =
       
   200 		_R("RTL8100e",		RTL_TD_0, NULL, JUMBO_1K, true),
       
   201 	[RTL_GIGA_MAC_VER_15] =
       
   202 		_R("RTL8100e",		RTL_TD_0, NULL, JUMBO_1K, true),
       
   203 	[RTL_GIGA_MAC_VER_16] =
       
   204 		_R("RTL8101e",		RTL_TD_0, NULL, JUMBO_1K, true),
       
   205 	[RTL_GIGA_MAC_VER_17] =
       
   206 		_R("RTL8168b/8111b",	RTL_TD_1, NULL, JUMBO_4K, false),
       
   207 	[RTL_GIGA_MAC_VER_18] =
       
   208 		_R("RTL8168cp/8111cp",	RTL_TD_1, NULL, JUMBO_6K, false),
       
   209 	[RTL_GIGA_MAC_VER_19] =
       
   210 		_R("RTL8168c/8111c",	RTL_TD_1, NULL, JUMBO_6K, false),
       
   211 	[RTL_GIGA_MAC_VER_20] =
       
   212 		_R("RTL8168c/8111c",	RTL_TD_1, NULL, JUMBO_6K, false),
       
   213 	[RTL_GIGA_MAC_VER_21] =
       
   214 		_R("RTL8168c/8111c",	RTL_TD_1, NULL, JUMBO_6K, false),
       
   215 	[RTL_GIGA_MAC_VER_22] =
       
   216 		_R("RTL8168c/8111c",	RTL_TD_1, NULL, JUMBO_6K, false),
       
   217 	[RTL_GIGA_MAC_VER_23] =
       
   218 		_R("RTL8168cp/8111cp",	RTL_TD_1, NULL, JUMBO_6K, false),
       
   219 	[RTL_GIGA_MAC_VER_24] =
       
   220 		_R("RTL8168cp/8111cp",	RTL_TD_1, NULL, JUMBO_6K, false),
       
   221 	[RTL_GIGA_MAC_VER_25] =
       
   222 		_R("RTL8168d/8111d",	RTL_TD_1, FIRMWARE_8168D_1,
       
   223 							JUMBO_9K, false),
       
   224 	[RTL_GIGA_MAC_VER_26] =
       
   225 		_R("RTL8168d/8111d",	RTL_TD_1, FIRMWARE_8168D_2,
       
   226 							JUMBO_9K, false),
       
   227 	[RTL_GIGA_MAC_VER_27] =
       
   228 		_R("RTL8168dp/8111dp",	RTL_TD_1, NULL, JUMBO_9K, false),
       
   229 	[RTL_GIGA_MAC_VER_28] =
       
   230 		_R("RTL8168dp/8111dp",	RTL_TD_1, NULL, JUMBO_9K, false),
       
   231 	[RTL_GIGA_MAC_VER_29] =
       
   232 		_R("RTL8105e",		RTL_TD_1, FIRMWARE_8105E_1,
       
   233 							JUMBO_1K, true),
       
   234 	[RTL_GIGA_MAC_VER_30] =
       
   235 		_R("RTL8105e",		RTL_TD_1, FIRMWARE_8105E_1,
       
   236 							JUMBO_1K, true),
       
   237 	[RTL_GIGA_MAC_VER_31] =
       
   238 		_R("RTL8168dp/8111dp",	RTL_TD_1, NULL, JUMBO_9K, false),
       
   239 	[RTL_GIGA_MAC_VER_32] =
       
   240 		_R("RTL8168e/8111e",	RTL_TD_1, FIRMWARE_8168E_1,
       
   241 							JUMBO_9K, false),
       
   242 	[RTL_GIGA_MAC_VER_33] =
       
   243 		_R("RTL8168e/8111e",	RTL_TD_1, FIRMWARE_8168E_2,
       
   244 							JUMBO_9K, false),
       
   245 	[RTL_GIGA_MAC_VER_34] =
       
   246 		_R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
       
   247 							JUMBO_9K, false),
       
   248 	[RTL_GIGA_MAC_VER_35] =
       
   249 		_R("RTL8168f/8111f",	RTL_TD_1, FIRMWARE_8168F_1,
       
   250 							JUMBO_9K, false),
       
   251 	[RTL_GIGA_MAC_VER_36] =
       
   252 		_R("RTL8168f/8111f",	RTL_TD_1, FIRMWARE_8168F_2,
       
   253 							JUMBO_9K, false),
       
   254 };
       
   255 #undef _R
       
   256 
       
   257 enum cfg_version {
       
   258 	RTL_CFG_0 = 0x00,
       
   259 	RTL_CFG_1,
       
   260 	RTL_CFG_2
       
   261 };
       
   262 
       
   263 static void rtl_hw_start_8169(struct net_device *);
       
   264 static void rtl_hw_start_8168(struct net_device *);
       
   265 static void rtl_hw_start_8101(struct net_device *);
       
   266 
       
   267 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
       
   268 	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8129), 0, 0, RTL_CFG_0 },
       
   269 	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8136), 0, 0, RTL_CFG_2 },
       
   270 	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8167), 0, 0, RTL_CFG_0 },
       
   271 	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8168), 0, 0, RTL_CFG_1 },
       
   272 	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8169), 0, 0, RTL_CFG_0 },
       
   273 	{ PCI_DEVICE(PCI_VENDOR_ID_DLINK,	0x4300), 0, 0, RTL_CFG_0 },
       
   274 	{ PCI_DEVICE(PCI_VENDOR_ID_DLINK,	0x4302), 0, 0, RTL_CFG_0 },
       
   275 	{ PCI_DEVICE(PCI_VENDOR_ID_AT,		0xc107), 0, 0, RTL_CFG_0 },
       
   276 	{ PCI_DEVICE(0x16ec,			0x0116), 0, 0, RTL_CFG_0 },
       
   277 	{ PCI_VENDOR_ID_LINKSYS,		0x1032,
       
   278 		PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
       
   279 	{ 0x0001,				0x8168,
       
   280 		PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
       
   281 	{0,},
       
   282 };
       
   283 
       
   284 /* prevent driver from being loaded automatically */
       
   285 //MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
       
   286 
       
   287 static int rx_buf_sz = 16383;
       
   288 static int use_dac;
       
   289 static struct {
       
   290 	u32 msg_enable;
       
   291 } debug = { -1 };
       
   292 
       
   293 enum rtl_registers {
       
   294 	MAC0		= 0,	/* Ethernet hardware address. */
       
   295 	MAC4		= 4,
       
   296 	MAR0		= 8,	/* Multicast filter. */
       
   297 	CounterAddrLow		= 0x10,
       
   298 	CounterAddrHigh		= 0x14,
       
   299 	TxDescStartAddrLow	= 0x20,
       
   300 	TxDescStartAddrHigh	= 0x24,
       
   301 	TxHDescStartAddrLow	= 0x28,
       
   302 	TxHDescStartAddrHigh	= 0x2c,
       
   303 	FLASH		= 0x30,
       
   304 	ERSR		= 0x36,
       
   305 	ChipCmd		= 0x37,
       
   306 	TxPoll		= 0x38,
       
   307 	IntrMask	= 0x3c,
       
   308 	IntrStatus	= 0x3e,
       
   309 
       
   310 	TxConfig	= 0x40,
       
   311 #define	TXCFG_AUTO_FIFO			(1 << 7)	/* 8111e-vl */
       
   312 #define	TXCFG_EMPTY			(1 << 11)	/* 8111e-vl */
       
   313 
       
   314 	RxConfig	= 0x44,
       
   315 #define	RX128_INT_EN			(1 << 15)	/* 8111c and later */
       
   316 #define	RX_MULTI_EN			(1 << 14)	/* 8111c only */
       
   317 #define	RXCFG_FIFO_SHIFT		13
       
   318 					/* No threshold before first PCI xfer */
       
   319 #define	RX_FIFO_THRESH			(7 << RXCFG_FIFO_SHIFT)
       
   320 #define	RXCFG_DMA_SHIFT			8
       
   321 					/* Unlimited maximum PCI burst. */
       
   322 #define	RX_DMA_BURST			(7 << RXCFG_DMA_SHIFT)
       
   323 
       
   324 	RxMissed	= 0x4c,
       
   325 	Cfg9346		= 0x50,
       
   326 	Config0		= 0x51,
       
   327 	Config1		= 0x52,
       
   328 	Config2		= 0x53,
       
   329 	Config3		= 0x54,
       
   330 	Config4		= 0x55,
       
   331 	Config5		= 0x56,
       
   332 	MultiIntr	= 0x5c,
       
   333 	PHYAR		= 0x60,
       
   334 	PHYstatus	= 0x6c,
       
   335 	RxMaxSize	= 0xda,
       
   336 	CPlusCmd	= 0xe0,
       
   337 	IntrMitigate	= 0xe2,
       
   338 	RxDescAddrLow	= 0xe4,
       
   339 	RxDescAddrHigh	= 0xe8,
       
   340 	EarlyTxThres	= 0xec,	/* 8169. Unit of 32 bytes. */
       
   341 
       
   342 #define NoEarlyTx	0x3f	/* Max value : no early transmit. */
       
   343 
       
   344 	MaxTxPacketSize	= 0xec,	/* 8101/8168. Unit of 128 bytes. */
       
   345 
       
   346 #define TxPacketMax	(8064 >> 7)
       
   347 #define EarlySize	0x27
       
   348 
       
   349 	FuncEvent	= 0xf0,
       
   350 	FuncEventMask	= 0xf4,
       
   351 	FuncPresetState	= 0xf8,
       
   352 	FuncForceEvent	= 0xfc,
       
   353 };
       
   354 
       
   355 enum rtl8110_registers {
       
   356 	TBICSR			= 0x64,
       
   357 	TBI_ANAR		= 0x68,
       
   358 	TBI_LPAR		= 0x6a,
       
   359 };
       
   360 
       
   361 enum rtl8168_8101_registers {
       
   362 	CSIDR			= 0x64,
       
   363 	CSIAR			= 0x68,
       
   364 #define	CSIAR_FLAG			0x80000000
       
   365 #define	CSIAR_WRITE_CMD			0x80000000
       
   366 #define	CSIAR_BYTE_ENABLE		0x0f
       
   367 #define	CSIAR_BYTE_ENABLE_SHIFT		12
       
   368 #define	CSIAR_ADDR_MASK			0x0fff
       
   369 	PMCH			= 0x6f,
       
   370 	EPHYAR			= 0x80,
       
   371 #define	EPHYAR_FLAG			0x80000000
       
   372 #define	EPHYAR_WRITE_CMD		0x80000000
       
   373 #define	EPHYAR_REG_MASK			0x1f
       
   374 #define	EPHYAR_REG_SHIFT		16
       
   375 #define	EPHYAR_DATA_MASK		0xffff
       
   376 	DLLPR			= 0xd0,
       
   377 #define	PFM_EN				(1 << 6)
       
   378 	DBG_REG			= 0xd1,
       
   379 #define	FIX_NAK_1			(1 << 4)
       
   380 #define	FIX_NAK_2			(1 << 3)
       
   381 	TWSI			= 0xd2,
       
   382 	MCU			= 0xd3,
       
   383 #define	NOW_IS_OOB			(1 << 7)
       
   384 #define	EN_NDP				(1 << 3)
       
   385 #define	EN_OOB_RESET			(1 << 2)
       
   386 	EFUSEAR			= 0xdc,
       
   387 #define	EFUSEAR_FLAG			0x80000000
       
   388 #define	EFUSEAR_WRITE_CMD		0x80000000
       
   389 #define	EFUSEAR_READ_CMD		0x00000000
       
   390 #define	EFUSEAR_REG_MASK		0x03ff
       
   391 #define	EFUSEAR_REG_SHIFT		8
       
   392 #define	EFUSEAR_DATA_MASK		0xff
       
   393 };
       
   394 
       
   395 enum rtl8168_registers {
       
   396 	LED_FREQ		= 0x1a,
       
   397 	EEE_LED			= 0x1b,
       
   398 	ERIDR			= 0x70,
       
   399 	ERIAR			= 0x74,
       
   400 #define ERIAR_FLAG			0x80000000
       
   401 #define ERIAR_WRITE_CMD			0x80000000
       
   402 #define ERIAR_READ_CMD			0x00000000
       
   403 #define ERIAR_ADDR_BYTE_ALIGN		4
       
   404 #define ERIAR_TYPE_SHIFT		16
       
   405 #define ERIAR_EXGMAC			(0x00 << ERIAR_TYPE_SHIFT)
       
   406 #define ERIAR_MSIX			(0x01 << ERIAR_TYPE_SHIFT)
       
   407 #define ERIAR_ASF			(0x02 << ERIAR_TYPE_SHIFT)
       
   408 #define ERIAR_MASK_SHIFT		12
       
   409 #define ERIAR_MASK_0001			(0x1 << ERIAR_MASK_SHIFT)
       
   410 #define ERIAR_MASK_0011			(0x3 << ERIAR_MASK_SHIFT)
       
   411 #define ERIAR_MASK_1111			(0xf << ERIAR_MASK_SHIFT)
       
   412 	EPHY_RXER_NUM		= 0x7c,
       
   413 	OCPDR			= 0xb0,	/* OCP GPHY access */
       
   414 #define OCPDR_WRITE_CMD			0x80000000
       
   415 #define OCPDR_READ_CMD			0x00000000
       
   416 #define OCPDR_REG_MASK			0x7f
       
   417 #define OCPDR_GPHY_REG_SHIFT		16
       
   418 #define OCPDR_DATA_MASK			0xffff
       
   419 	OCPAR			= 0xb4,
       
   420 #define OCPAR_FLAG			0x80000000
       
   421 #define OCPAR_GPHY_WRITE_CMD		0x8000f060
       
   422 #define OCPAR_GPHY_READ_CMD		0x0000f060
       
   423 	RDSAR1			= 0xd0,	/* 8168c only. Undocumented on 8168dp */
       
   424 	MISC			= 0xf0,	/* 8168e only. */
       
   425 #define TXPLA_RST			(1 << 29)
       
   426 #define PWM_EN				(1 << 22)
       
   427 };
       
   428 
       
   429 enum rtl_register_content {
       
   430 	/* InterruptStatusBits */
       
   431 	SYSErr		= 0x8000,
       
   432 	PCSTimeout	= 0x4000,
       
   433 	SWInt		= 0x0100,
       
   434 	TxDescUnavail	= 0x0080,
       
   435 	RxFIFOOver	= 0x0040,
       
   436 	LinkChg		= 0x0020,
       
   437 	RxOverflow	= 0x0010,
       
   438 	TxErr		= 0x0008,
       
   439 	TxOK		= 0x0004,
       
   440 	RxErr		= 0x0002,
       
   441 	RxOK		= 0x0001,
       
   442 
       
   443 	/* RxStatusDesc */
       
   444 	RxBOVF	= (1 << 24),
       
   445 	RxFOVF	= (1 << 23),
       
   446 	RxRWT	= (1 << 22),
       
   447 	RxRES	= (1 << 21),
       
   448 	RxRUNT	= (1 << 20),
       
   449 	RxCRC	= (1 << 19),
       
   450 
       
   451 	/* ChipCmdBits */
       
   452 	StopReq		= 0x80,
       
   453 	CmdReset	= 0x10,
       
   454 	CmdRxEnb	= 0x08,
       
   455 	CmdTxEnb	= 0x04,
       
   456 	RxBufEmpty	= 0x01,
       
   457 
       
   458 	/* TXPoll register p.5 */
       
   459 	HPQ		= 0x80,		/* Poll cmd on the high prio queue */
       
   460 	NPQ		= 0x40,		/* Poll cmd on the low prio queue */
       
   461 	FSWInt		= 0x01,		/* Forced software interrupt */
       
   462 
       
   463 	/* Cfg9346Bits */
       
   464 	Cfg9346_Lock	= 0x00,
       
   465 	Cfg9346_Unlock	= 0xc0,
       
   466 
       
   467 	/* rx_mode_bits */
       
   468 	AcceptErr	= 0x20,
       
   469 	AcceptRunt	= 0x10,
       
   470 	AcceptBroadcast	= 0x08,
       
   471 	AcceptMulticast	= 0x04,
       
   472 	AcceptMyPhys	= 0x02,
       
   473 	AcceptAllPhys	= 0x01,
       
   474 #define RX_CONFIG_ACCEPT_MASK		0x3f
       
   475 
       
   476 	/* TxConfigBits */
       
   477 	TxInterFrameGapShift = 24,
       
   478 	TxDMAShift = 8,	/* DMA burst value (0-7) is shift this many bits */
       
   479 
       
   480 	/* Config1 register p.24 */
       
   481 	LEDS1		= (1 << 7),
       
   482 	LEDS0		= (1 << 6),
       
   483 	Speed_down	= (1 << 4),
       
   484 	MEMMAP		= (1 << 3),
       
   485 	IOMAP		= (1 << 2),
       
   486 	VPD		= (1 << 1),
       
   487 	PMEnable	= (1 << 0),	/* Power Management Enable */
       
   488 
       
   489 	/* Config2 register p. 25 */
       
   490 	MSIEnable	= (1 << 5),	/* 8169 only. Reserved in the 8168. */
       
   491 	PCI_Clock_66MHz = 0x01,
       
   492 	PCI_Clock_33MHz = 0x00,
       
   493 
       
   494 	/* Config3 register p.25 */
       
   495 	MagicPacket	= (1 << 5),	/* Wake up when receives a Magic Packet */
       
   496 	LinkUp		= (1 << 4),	/* Wake up when the cable connection is re-established */
       
   497 	Jumbo_En0	= (1 << 2),	/* 8168 only. Reserved in the 8168b */
       
   498 	Beacon_en	= (1 << 0),	/* 8168 only. Reserved in the 8168b */
       
   499 
       
   500 	/* Config4 register */
       
   501 	Jumbo_En1	= (1 << 1),	/* 8168 only. Reserved in the 8168b */
       
   502 
       
   503 	/* Config5 register p.27 */
       
   504 	BWF		= (1 << 6),	/* Accept Broadcast wakeup frame */
       
   505 	MWF		= (1 << 5),	/* Accept Multicast wakeup frame */
       
   506 	UWF		= (1 << 4),	/* Accept Unicast wakeup frame */
       
   507 	Spi_en		= (1 << 3),
       
   508 	LanWake		= (1 << 1),	/* LanWake enable/disable */
       
   509 	PMEStatus	= (1 << 0),	/* PME status can be reset by PCI RST# */
       
   510 
       
   511 	/* TBICSR p.28 */
       
   512 	TBIReset	= 0x80000000,
       
   513 	TBILoopback	= 0x40000000,
       
   514 	TBINwEnable	= 0x20000000,
       
   515 	TBINwRestart	= 0x10000000,
       
   516 	TBILinkOk	= 0x02000000,
       
   517 	TBINwComplete	= 0x01000000,
       
   518 
       
   519 	/* CPlusCmd p.31 */
       
   520 	EnableBist	= (1 << 15),	// 8168 8101
       
   521 	Mac_dbgo_oe	= (1 << 14),	// 8168 8101
       
   522 	Normal_mode	= (1 << 13),	// unused
       
   523 	Force_half_dup	= (1 << 12),	// 8168 8101
       
   524 	Force_rxflow_en	= (1 << 11),	// 8168 8101
       
   525 	Force_txflow_en	= (1 << 10),	// 8168 8101
       
   526 	Cxpl_dbg_sel	= (1 << 9),	// 8168 8101
       
   527 	ASF		= (1 << 8),	// 8168 8101
       
   528 	PktCntrDisable	= (1 << 7),	// 8168 8101
       
   529 	Mac_dbgo_sel	= 0x001c,	// 8168
       
   530 	RxVlan		= (1 << 6),
       
   531 	RxChkSum	= (1 << 5),
       
   532 	PCIDAC		= (1 << 4),
       
   533 	PCIMulRW	= (1 << 3),
       
   534 	INTT_0		= 0x0000,	// 8168
       
   535 	INTT_1		= 0x0001,	// 8168
       
   536 	INTT_2		= 0x0002,	// 8168
       
   537 	INTT_3		= 0x0003,	// 8168
       
   538 
       
   539 	/* rtl8169_PHYstatus */
       
   540 	TBI_Enable	= 0x80,
       
   541 	TxFlowCtrl	= 0x40,
       
   542 	RxFlowCtrl	= 0x20,
       
   543 	_1000bpsF	= 0x10,
       
   544 	_100bps		= 0x08,
       
   545 	_10bps		= 0x04,
       
   546 	LinkStatus	= 0x02,
       
   547 	FullDup		= 0x01,
       
   548 
       
   549 	/* _TBICSRBit */
       
   550 	TBILinkOK	= 0x02000000,
       
   551 
       
   552 	/* DumpCounterCommand */
       
   553 	CounterDump	= 0x8,
       
   554 };
       
   555 
       
   556 enum rtl_desc_bit {
       
   557 	/* First doubleword. */
       
   558 	DescOwn		= (1 << 31), /* Descriptor is owned by NIC */
       
   559 	RingEnd		= (1 << 30), /* End of descriptor ring */
       
   560 	FirstFrag	= (1 << 29), /* First segment of a packet */
       
   561 	LastFrag	= (1 << 28), /* Final segment of a packet */
       
   562 };
       
   563 
       
   564 /* Generic case. */
       
   565 enum rtl_tx_desc_bit {
       
   566 	/* First doubleword. */
       
   567 	TD_LSO		= (1 << 27),		/* Large Send Offload */
       
   568 #define TD_MSS_MAX			0x07ffu	/* MSS value */
       
   569 
       
   570 	/* Second doubleword. */
       
   571 	TxVlanTag	= (1 << 17),		/* Add VLAN tag */
       
   572 };
       
   573 
       
   574 /* 8169, 8168b and 810x except 8102e. */
       
   575 enum rtl_tx_desc_bit_0 {
       
   576 	/* First doubleword. */
       
   577 #define TD0_MSS_SHIFT			16	/* MSS position (11 bits) */
       
   578 	TD0_TCP_CS	= (1 << 16),		/* Calculate TCP/IP checksum */
       
   579 	TD0_UDP_CS	= (1 << 17),		/* Calculate UDP/IP checksum */
       
   580 	TD0_IP_CS	= (1 << 18),		/* Calculate IP checksum */
       
   581 };
       
   582 
       
   583 /* 8102e, 8168c and beyond. */
       
   584 enum rtl_tx_desc_bit_1 {
       
   585 	/* Second doubleword. */
       
   586 #define TD1_MSS_SHIFT			18	/* MSS position (11 bits) */
       
   587 	TD1_IP_CS	= (1 << 29),		/* Calculate IP checksum */
       
   588 	TD1_TCP_CS	= (1 << 30),		/* Calculate TCP/IP checksum */
       
   589 	TD1_UDP_CS	= (1 << 31),		/* Calculate UDP/IP checksum */
       
   590 };
       
   591 
       
   592 static const struct rtl_tx_desc_info {
       
   593 	struct {
       
   594 		u32 udp;
       
   595 		u32 tcp;
       
   596 	} checksum;
       
   597 	u16 mss_shift;
       
   598 	u16 opts_offset;
       
   599 } tx_desc_info [] = {
       
   600 	[RTL_TD_0] = {
       
   601 		.checksum = {
       
   602 			.udp	= TD0_IP_CS | TD0_UDP_CS,
       
   603 			.tcp	= TD0_IP_CS | TD0_TCP_CS
       
   604 		},
       
   605 		.mss_shift	= TD0_MSS_SHIFT,
       
   606 		.opts_offset	= 0
       
   607 	},
       
   608 	[RTL_TD_1] = {
       
   609 		.checksum = {
       
   610 			.udp	= TD1_IP_CS | TD1_UDP_CS,
       
   611 			.tcp	= TD1_IP_CS | TD1_TCP_CS
       
   612 		},
       
   613 		.mss_shift	= TD1_MSS_SHIFT,
       
   614 		.opts_offset	= 1
       
   615 	}
       
   616 };
       
   617 
       
   618 enum rtl_rx_desc_bit {
       
   619 	/* Rx private */
       
   620 	PID1		= (1 << 18), /* Protocol ID bit 1/2 */
       
   621 	PID0		= (1 << 17), /* Protocol ID bit 2/2 */
       
   622 
       
   623 #define RxProtoUDP	(PID1)
       
   624 #define RxProtoTCP	(PID0)
       
   625 #define RxProtoIP	(PID1 | PID0)
       
   626 #define RxProtoMask	RxProtoIP
       
   627 
       
   628 	IPFail		= (1 << 16), /* IP checksum failed */
       
   629 	UDPFail		= (1 << 15), /* UDP/IP checksum failed */
       
   630 	TCPFail		= (1 << 14), /* TCP/IP checksum failed */
       
   631 	RxVlanTag	= (1 << 16), /* VLAN tag available */
       
   632 };
       
   633 
       
   634 #define RsvdMask	0x3fffc000
       
   635 
       
   636 struct TxDesc {
       
   637 	__le32 opts1;
       
   638 	__le32 opts2;
       
   639 	__le64 addr;
       
   640 };
       
   641 
       
   642 struct RxDesc {
       
   643 	__le32 opts1;
       
   644 	__le32 opts2;
       
   645 	__le64 addr;
       
   646 };
       
   647 
       
   648 struct ring_info {
       
   649 	struct sk_buff	*skb;
       
   650 	u32		len;
       
   651 	u8		__pad[sizeof(void *) - sizeof(u32)];
       
   652 };
       
   653 
       
   654 enum features {
       
   655 	RTL_FEATURE_WOL		= (1 << 0),
       
   656 	RTL_FEATURE_MSI		= (1 << 1),
       
   657 	RTL_FEATURE_GMII	= (1 << 2),
       
   658 };
       
   659 
       
   660 struct rtl8169_counters {
       
   661 	__le64	tx_packets;
       
   662 	__le64	rx_packets;
       
   663 	__le64	tx_errors;
       
   664 	__le32	rx_errors;
       
   665 	__le16	rx_missed;
       
   666 	__le16	align_errors;
       
   667 	__le32	tx_one_collision;
       
   668 	__le32	tx_multi_collision;
       
   669 	__le64	rx_unicast;
       
   670 	__le64	rx_broadcast;
       
   671 	__le32	rx_multicast;
       
   672 	__le16	tx_aborted;
       
   673 	__le16	tx_underun;
       
   674 };
       
   675 
       
   676 struct rtl8169_private {
       
   677 	void __iomem *mmio_addr;	/* memory map physical address */
       
   678 	struct pci_dev *pci_dev;
       
   679 	struct net_device *dev;
       
   680 	struct napi_struct napi;
       
   681 	spinlock_t lock;
       
   682 	u32 msg_enable;
       
   683 	u16 txd_version;
       
   684 	u16 mac_version;
       
   685 	u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
       
   686 	u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
       
   687 	u32 dirty_rx;
       
   688 	u32 dirty_tx;
       
   689 	struct TxDesc *TxDescArray;	/* 256-aligned Tx descriptor ring */
       
   690 	struct RxDesc *RxDescArray;	/* 256-aligned Rx descriptor ring */
       
   691 	dma_addr_t TxPhyAddr;
       
   692 	dma_addr_t RxPhyAddr;
       
   693 	void *Rx_databuff[NUM_RX_DESC];	/* Rx data buffers */
       
   694 	struct ring_info tx_skb[NUM_TX_DESC];	/* Tx data buffers */
       
   695 	struct timer_list timer;
       
   696 	u16 cp_cmd;
       
   697 	u16 intr_event;
       
   698 	u16 napi_event;
       
   699 	u16 intr_mask;
       
   700 
       
   701 	struct mdio_ops {
       
   702 		void (*write)(void __iomem *, int, int);
       
   703 		int (*read)(void __iomem *, int);
       
   704 	} mdio_ops;
       
   705 
       
   706 	struct pll_power_ops {
       
   707 		void (*down)(struct rtl8169_private *);
       
   708 		void (*up)(struct rtl8169_private *);
       
   709 	} pll_power_ops;
       
   710 
       
   711 	struct jumbo_ops {
       
   712 		void (*enable)(struct rtl8169_private *);
       
   713 		void (*disable)(struct rtl8169_private *);
       
   714 	} jumbo_ops;
       
   715 
       
   716 	int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
       
   717 	int (*get_settings)(struct net_device *, struct ethtool_cmd *);
       
   718 	void (*phy_reset_enable)(struct rtl8169_private *tp);
       
   719 	void (*hw_start)(struct net_device *);
       
   720 	unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
       
   721 	unsigned int (*link_ok)(void __iomem *);
       
   722 	int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
       
   723 	struct delayed_work task;
       
   724 	unsigned features;
       
   725 
       
   726 	struct mii_if_info mii;
       
   727 	struct rtl8169_counters counters;
       
   728 	u32 saved_wolopts;
       
   729 
       
   730 	ec_device_t *ecdev;
       
   731 	unsigned long ec_watchdog_jiffies;
       
   732 	u32 opts1_mask;
       
   733 
       
   734 	struct rtl_fw {
       
   735 		const struct firmware *fw;
       
   736 
       
   737 #define RTL_VER_SIZE		32
       
   738 
       
   739 		char version[RTL_VER_SIZE];
       
   740 
       
   741 		struct rtl_fw_phy_action {
       
   742 			__le32 *code;
       
   743 			size_t size;
       
   744 		} phy_action;
       
   745 	} *rtl_fw;
       
   746 #define RTL_FIRMWARE_UNKNOWN	ERR_PTR(-EAGAIN)
       
   747 };
       
   748 
       
   749 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
       
   750 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver (EtherCAT)");
       
   751 module_param(use_dac, int, 0);
       
   752 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
       
   753 module_param_named(debug, debug.msg_enable, int, 0);
       
   754 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
       
   755 MODULE_LICENSE("GPL");
       
   756 MODULE_VERSION(EC_MASTER_VERSION);
       
   757 MODULE_FIRMWARE(FIRMWARE_8168D_1);
       
   758 MODULE_FIRMWARE(FIRMWARE_8168D_2);
       
   759 MODULE_FIRMWARE(FIRMWARE_8168E_1);
       
   760 MODULE_FIRMWARE(FIRMWARE_8168E_2);
       
   761 MODULE_FIRMWARE(FIRMWARE_8168E_3);
       
   762 MODULE_FIRMWARE(FIRMWARE_8105E_1);
       
   763 MODULE_FIRMWARE(FIRMWARE_8168F_1);
       
   764 MODULE_FIRMWARE(FIRMWARE_8168F_2);
       
   765 
       
   766 static int rtl8169_open(struct net_device *dev);
       
   767 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
       
   768 				      struct net_device *dev);
       
   769 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
       
   770 static int rtl8169_init_ring(struct net_device *dev);
       
   771 static void rtl_hw_start(struct net_device *dev);
       
   772 static int rtl8169_close(struct net_device *dev);
       
   773 static void rtl_set_rx_mode(struct net_device *dev);
       
   774 static void rtl8169_tx_timeout(struct net_device *dev);
       
   775 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
       
   776 static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
       
   777 				void __iomem *, u32 budget);
       
   778 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
       
   779 static void rtl8169_down(struct net_device *dev);
       
   780 static void rtl8169_rx_clear(struct rtl8169_private *tp);
       
   781 static void ec_poll(struct net_device *dev);
       
   782 static int rtl8169_poll(struct napi_struct *napi, int budget);
       
   783 
       
   784 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
       
   785 {
       
   786 	int cap = pci_pcie_cap(pdev);
       
   787 
       
   788 	if (cap) {
       
   789 		u16 ctl;
       
   790 
       
   791 		pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
       
   792 		ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
       
   793 		pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
       
   794 	}
       
   795 }
       
   796 
       
   797 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
       
   798 {
       
   799 	void __iomem *ioaddr = tp->mmio_addr;
       
   800 	int i;
       
   801 
       
   802 	RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
       
   803 	for (i = 0; i < 20; i++) {
       
   804 		udelay(100);
       
   805 		if (RTL_R32(OCPAR) & OCPAR_FLAG)
       
   806 			break;
       
   807 	}
       
   808 	return RTL_R32(OCPDR);
       
   809 }
       
   810 
       
   811 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
       
   812 {
       
   813 	void __iomem *ioaddr = tp->mmio_addr;
       
   814 	int i;
       
   815 
       
   816 	RTL_W32(OCPDR, data);
       
   817 	RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
       
   818 	for (i = 0; i < 20; i++) {
       
   819 		udelay(100);
       
   820 		if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
       
   821 			break;
       
   822 	}
       
   823 }
       
   824 
       
   825 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
       
   826 {
       
   827 	void __iomem *ioaddr = tp->mmio_addr;
       
   828 	int i;
       
   829 
       
   830 	RTL_W8(ERIDR, cmd);
       
   831 	RTL_W32(ERIAR, 0x800010e8);
       
   832 	msleep(2);
       
   833 	for (i = 0; i < 5; i++) {
       
   834 		udelay(100);
       
   835 		if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
       
   836 			break;
       
   837 	}
       
   838 
       
   839 	ocp_write(tp, 0x1, 0x30, 0x00000001);
       
   840 }
       
   841 
       
   842 #define OOB_CMD_RESET		0x00
       
   843 #define OOB_CMD_DRIVER_START	0x05
       
   844 #define OOB_CMD_DRIVER_STOP	0x06
       
   845 
       
   846 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
       
   847 {
       
   848 	return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
       
   849 }
       
   850 
       
   851 static void rtl8168_driver_start(struct rtl8169_private *tp)
       
   852 {
       
   853 	u16 reg;
       
   854 	int i;
       
   855 
       
   856 	rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
       
   857 
       
   858 	reg = rtl8168_get_ocp_reg(tp);
       
   859 
       
   860 	for (i = 0; i < 10; i++) {
       
   861 		msleep(10);
       
   862 		if (ocp_read(tp, 0x0f, reg) & 0x00000800)
       
   863 			break;
       
   864 	}
       
   865 }
       
   866 
       
   867 static void rtl8168_driver_stop(struct rtl8169_private *tp)
       
   868 {
       
   869 	u16 reg;
       
   870 	int i;
       
   871 
       
   872 	rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
       
   873 
       
   874 	reg = rtl8168_get_ocp_reg(tp);
       
   875 
       
   876 	for (i = 0; i < 10; i++) {
       
   877 		msleep(10);
       
   878 		if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
       
   879 			break;
       
   880 	}
       
   881 }
       
   882 
       
   883 static int r8168dp_check_dash(struct rtl8169_private *tp)
       
   884 {
       
   885 	u16 reg = rtl8168_get_ocp_reg(tp);
       
   886 
       
   887 	return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
       
   888 }
       
   889 
       
   890 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
       
   891 {
       
   892 	int i;
       
   893 
       
   894 	RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
       
   895 
       
   896 	for (i = 20; i > 0; i--) {
       
   897 		/*
       
   898 		 * Check if the RTL8169 has completed writing to the specified
       
   899 		 * MII register.
       
   900 		 */
       
   901 		if (!(RTL_R32(PHYAR) & 0x80000000))
       
   902 			break;
       
   903 		udelay(25);
       
   904 	}
       
   905 	/*
       
   906 	 * According to hardware specs a 20us delay is required after write
       
   907 	 * complete indication, but before sending next command.
       
   908 	 */
       
   909 	udelay(20);
       
   910 }
       
   911 
       
   912 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
       
   913 {
       
   914 	int i, value = -1;
       
   915 
       
   916 	RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
       
   917 
       
   918 	for (i = 20; i > 0; i--) {
       
   919 		/*
       
   920 		 * Check if the RTL8169 has completed retrieving data from
       
   921 		 * the specified MII register.
       
   922 		 */
       
   923 		if (RTL_R32(PHYAR) & 0x80000000) {
       
   924 			value = RTL_R32(PHYAR) & 0xffff;
       
   925 			break;
       
   926 		}
       
   927 		udelay(25);
       
   928 	}
       
   929 	/*
       
   930 	 * According to hardware specs a 20us delay is required after read
       
   931 	 * complete indication, but before sending next command.
       
   932 	 */
       
   933 	udelay(20);
       
   934 
       
   935 	return value;
       
   936 }
       
   937 
       
   938 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
       
   939 {
       
   940 	int i;
       
   941 
       
   942 	RTL_W32(OCPDR, data |
       
   943 		((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
       
   944 	RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
       
   945 	RTL_W32(EPHY_RXER_NUM, 0);
       
   946 
       
   947 	for (i = 0; i < 100; i++) {
       
   948 		mdelay(1);
       
   949 		if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
       
   950 			break;
       
   951 	}
       
   952 }
       
   953 
       
   954 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
       
   955 {
       
   956 	r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
       
   957 		(value & OCPDR_DATA_MASK));
       
   958 }
       
   959 
       
   960 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
       
   961 {
       
   962 	int i;
       
   963 
       
   964 	r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
       
   965 
       
   966 	mdelay(1);
       
   967 	RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
       
   968 	RTL_W32(EPHY_RXER_NUM, 0);
       
   969 
       
   970 	for (i = 0; i < 100; i++) {
       
   971 		mdelay(1);
       
   972 		if (RTL_R32(OCPAR) & OCPAR_FLAG)
       
   973 			break;
       
   974 	}
       
   975 
       
   976 	return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
       
   977 }
       
   978 
       
   979 #define R8168DP_1_MDIO_ACCESS_BIT	0x00020000
       
   980 
       
   981 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
       
   982 {
       
   983 	RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
       
   984 }
       
   985 
       
   986 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
       
   987 {
       
   988 	RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
       
   989 }
       
   990 
       
   991 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
       
   992 {
       
   993 	r8168dp_2_mdio_start(ioaddr);
       
   994 
       
   995 	r8169_mdio_write(ioaddr, reg_addr, value);
       
   996 
       
   997 	r8168dp_2_mdio_stop(ioaddr);
       
   998 }
       
   999 
       
  1000 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
       
  1001 {
       
  1002 	int value;
       
  1003 
       
  1004 	r8168dp_2_mdio_start(ioaddr);
       
  1005 
       
  1006 	value = r8169_mdio_read(ioaddr, reg_addr);
       
  1007 
       
  1008 	r8168dp_2_mdio_stop(ioaddr);
       
  1009 
       
  1010 	return value;
       
  1011 }
       
  1012 
       
  1013 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
       
  1014 {
       
  1015 	tp->mdio_ops.write(tp->mmio_addr, location, val);
       
  1016 }
       
  1017 
       
  1018 static int rtl_readphy(struct rtl8169_private *tp, int location)
       
  1019 {
       
  1020 	return tp->mdio_ops.read(tp->mmio_addr, location);
       
  1021 }
       
  1022 
       
  1023 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
       
  1024 {
       
  1025 	rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
       
  1026 }
       
  1027 
       
  1028 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
       
  1029 {
       
  1030 	int val;
       
  1031 
       
  1032 	val = rtl_readphy(tp, reg_addr);
       
  1033 	rtl_writephy(tp, reg_addr, (val | p) & ~m);
       
  1034 }
       
  1035 
       
  1036 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
       
  1037 			   int val)
       
  1038 {
       
  1039 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1040 
       
  1041 	rtl_writephy(tp, location, val);
       
  1042 }
       
  1043 
       
  1044 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
       
  1045 {
       
  1046 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1047 
       
  1048 	return rtl_readphy(tp, location);
       
  1049 }
       
  1050 
       
  1051 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
       
  1052 {
       
  1053 	unsigned int i;
       
  1054 
       
  1055 	RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
       
  1056 		(reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
       
  1057 
       
  1058 	for (i = 0; i < 100; i++) {
       
  1059 		if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
       
  1060 			break;
       
  1061 		udelay(10);
       
  1062 	}
       
  1063 }
       
  1064 
       
  1065 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
       
  1066 {
       
  1067 	u16 value = 0xffff;
       
  1068 	unsigned int i;
       
  1069 
       
  1070 	RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
       
  1071 
       
  1072 	for (i = 0; i < 100; i++) {
       
  1073 		if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
       
  1074 			value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
       
  1075 			break;
       
  1076 		}
       
  1077 		udelay(10);
       
  1078 	}
       
  1079 
       
  1080 	return value;
       
  1081 }
       
  1082 
       
  1083 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
       
  1084 {
       
  1085 	unsigned int i;
       
  1086 
       
  1087 	RTL_W32(CSIDR, value);
       
  1088 	RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
       
  1089 		CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
       
  1090 
       
  1091 	for (i = 0; i < 100; i++) {
       
  1092 		if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
       
  1093 			break;
       
  1094 		udelay(10);
       
  1095 	}
       
  1096 }
       
  1097 
       
  1098 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
       
  1099 {
       
  1100 	u32 value = ~0x00;
       
  1101 	unsigned int i;
       
  1102 
       
  1103 	RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
       
  1104 		CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
       
  1105 
       
  1106 	for (i = 0; i < 100; i++) {
       
  1107 		if (RTL_R32(CSIAR) & CSIAR_FLAG) {
       
  1108 			value = RTL_R32(CSIDR);
       
  1109 			break;
       
  1110 		}
       
  1111 		udelay(10);
       
  1112 	}
       
  1113 
       
  1114 	return value;
       
  1115 }
       
  1116 
       
  1117 static
       
  1118 void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
       
  1119 {
       
  1120 	unsigned int i;
       
  1121 
       
  1122 	BUG_ON((addr & 3) || (mask == 0));
       
  1123 	RTL_W32(ERIDR, val);
       
  1124 	RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
       
  1125 
       
  1126 	for (i = 0; i < 100; i++) {
       
  1127 		if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
       
  1128 			break;
       
  1129 		udelay(100);
       
  1130 	}
       
  1131 }
       
  1132 
       
  1133 static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
       
  1134 {
       
  1135 	u32 value = ~0x00;
       
  1136 	unsigned int i;
       
  1137 
       
  1138 	RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
       
  1139 
       
  1140 	for (i = 0; i < 100; i++) {
       
  1141 		if (RTL_R32(ERIAR) & ERIAR_FLAG) {
       
  1142 			value = RTL_R32(ERIDR);
       
  1143 			break;
       
  1144 		}
       
  1145 		udelay(100);
       
  1146 	}
       
  1147 
       
  1148 	return value;
       
  1149 }
       
  1150 
       
  1151 static void
       
  1152 rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
       
  1153 {
       
  1154 	u32 val;
       
  1155 
       
  1156 	val = rtl_eri_read(ioaddr, addr, type);
       
  1157 	rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
       
  1158 }
       
  1159 
       
  1160 struct exgmac_reg {
       
  1161 	u16 addr;
       
  1162 	u16 mask;
       
  1163 	u32 val;
       
  1164 };
       
  1165 
       
  1166 static void rtl_write_exgmac_batch(void __iomem *ioaddr,
       
  1167 				   const struct exgmac_reg *r, int len)
       
  1168 {
       
  1169 	while (len-- > 0) {
       
  1170 		rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
       
  1171 		r++;
       
  1172 	}
       
  1173 }
       
  1174 
       
  1175 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
       
  1176 {
       
  1177 	u8 value = 0xff;
       
  1178 	unsigned int i;
       
  1179 
       
  1180 	RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
       
  1181 
       
  1182 	for (i = 0; i < 300; i++) {
       
  1183 		if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
       
  1184 			value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
       
  1185 			break;
       
  1186 		}
       
  1187 		udelay(100);
       
  1188 	}
       
  1189 
       
  1190 	return value;
       
  1191 }
       
  1192 
       
  1193 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
       
  1194 {
       
  1195 	void __iomem *ioaddr = tp->mmio_addr;
       
  1196 
       
  1197 	RTL_W16(IntrMask, 0x0000);
       
  1198 	RTL_W16(IntrStatus, tp->intr_event);
       
  1199 	RTL_R8(ChipCmd);
       
  1200 }
       
  1201 
       
  1202 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
       
  1203 {
       
  1204 	void __iomem *ioaddr = tp->mmio_addr;
       
  1205 
       
  1206 	return RTL_R32(TBICSR) & TBIReset;
       
  1207 }
       
  1208 
       
  1209 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
       
  1210 {
       
  1211 	return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
       
  1212 }
       
  1213 
       
  1214 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
       
  1215 {
       
  1216 	return RTL_R32(TBICSR) & TBILinkOk;
       
  1217 }
       
  1218 
       
  1219 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
       
  1220 {
       
  1221 	return RTL_R8(PHYstatus) & LinkStatus;
       
  1222 }
       
  1223 
       
  1224 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
       
  1225 {
       
  1226 	void __iomem *ioaddr = tp->mmio_addr;
       
  1227 
       
  1228 	RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
       
  1229 }
       
  1230 
       
  1231 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
       
  1232 {
       
  1233 	unsigned int val;
       
  1234 
       
  1235 	val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
       
  1236 	rtl_writephy(tp, MII_BMCR, val & 0xffff);
       
  1237 }
       
  1238 
       
  1239 static void rtl_link_chg_patch(struct rtl8169_private *tp)
       
  1240 {
       
  1241 	void __iomem *ioaddr = tp->mmio_addr;
       
  1242 	struct net_device *dev = tp->dev;
       
  1243 
       
  1244 	if (!netif_running(dev))
       
  1245 		return;
       
  1246 
       
  1247 	if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
       
  1248 		if (RTL_R8(PHYstatus) & _1000bpsF) {
       
  1249 			rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
       
  1250 				      0x00000011, ERIAR_EXGMAC);
       
  1251 			rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
       
  1252 				      0x00000005, ERIAR_EXGMAC);
       
  1253 		} else if (RTL_R8(PHYstatus) & _100bps) {
       
  1254 			rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
       
  1255 				      0x0000001f, ERIAR_EXGMAC);
       
  1256 			rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
       
  1257 				      0x00000005, ERIAR_EXGMAC);
       
  1258 		} else {
       
  1259 			rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
       
  1260 				      0x0000001f, ERIAR_EXGMAC);
       
  1261 			rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
       
  1262 				      0x0000003f, ERIAR_EXGMAC);
       
  1263 		}
       
  1264 		/* Reset packet filter */
       
  1265 		rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
       
  1266 			     ERIAR_EXGMAC);
       
  1267 		rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
       
  1268 			     ERIAR_EXGMAC);
       
  1269 	} else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
       
  1270 		   tp->mac_version == RTL_GIGA_MAC_VER_36) {
       
  1271 		if (RTL_R8(PHYstatus) & _1000bpsF) {
       
  1272 			rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
       
  1273 				      0x00000011, ERIAR_EXGMAC);
       
  1274 			rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
       
  1275 				      0x00000005, ERIAR_EXGMAC);
       
  1276 		} else {
       
  1277 			rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
       
  1278 				      0x0000001f, ERIAR_EXGMAC);
       
  1279 			rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
       
  1280 				      0x0000003f, ERIAR_EXGMAC);
       
  1281 		}
       
  1282 	}
       
  1283 }
       
  1284 
       
  1285 static void __rtl8169_check_link_status(struct net_device *dev,
       
  1286 					struct rtl8169_private *tp,
       
  1287 					void __iomem *ioaddr, bool pm)
       
  1288 {
       
  1289 	unsigned long flags;
       
  1290 
       
  1291 	if (tp->ecdev) {
       
  1292 		ecdev_set_link(tp->ecdev, tp->link_ok(ioaddr) ? 1 : 0);
       
  1293 		return;
       
  1294 	}
       
  1295 
       
  1296 	spin_lock_irqsave(&tp->lock, flags);
       
  1297 	if (tp->link_ok(ioaddr)) {
       
  1298 		rtl_link_chg_patch(tp);
       
  1299 		/* This is to cancel a scheduled suspend if there's one. */
       
  1300 		if (pm)
       
  1301 			pm_request_resume(&tp->pci_dev->dev);
       
  1302 		netif_carrier_on(dev);
       
  1303 		if (net_ratelimit())
       
  1304 			netif_info(tp, ifup, dev, "link up\n");
       
  1305 	} else {
       
  1306 		netif_carrier_off(dev);
       
  1307 		netif_info(tp, ifdown, dev, "link down\n");
       
  1308 		if (pm)
       
  1309 			pm_schedule_suspend(&tp->pci_dev->dev, 5000);
       
  1310 	}
       
  1311 	spin_unlock_irqrestore(&tp->lock, flags);
       
  1312 }
       
  1313 
       
  1314 static void rtl8169_check_link_status(struct net_device *dev,
       
  1315 				      struct rtl8169_private *tp,
       
  1316 				      void __iomem *ioaddr)
       
  1317 {
       
  1318 	__rtl8169_check_link_status(dev, tp, ioaddr, false);
       
  1319 }
       
  1320 
       
  1321 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
       
  1322 
       
  1323 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
       
  1324 {
       
  1325 	void __iomem *ioaddr = tp->mmio_addr;
       
  1326 	u8 options;
       
  1327 	u32 wolopts = 0;
       
  1328 
       
  1329 	options = RTL_R8(Config1);
       
  1330 	if (!(options & PMEnable))
       
  1331 		return 0;
       
  1332 
       
  1333 	options = RTL_R8(Config3);
       
  1334 	if (options & LinkUp)
       
  1335 		wolopts |= WAKE_PHY;
       
  1336 	if (options & MagicPacket)
       
  1337 		wolopts |= WAKE_MAGIC;
       
  1338 
       
  1339 	options = RTL_R8(Config5);
       
  1340 	if (options & UWF)
       
  1341 		wolopts |= WAKE_UCAST;
       
  1342 	if (options & BWF)
       
  1343 		wolopts |= WAKE_BCAST;
       
  1344 	if (options & MWF)
       
  1345 		wolopts |= WAKE_MCAST;
       
  1346 
       
  1347 	return wolopts;
       
  1348 }
       
  1349 
       
  1350 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
       
  1351 {
       
  1352 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1353 
       
  1354 	spin_lock_irq(&tp->lock);
       
  1355 
       
  1356 	wol->supported = WAKE_ANY;
       
  1357 	wol->wolopts = __rtl8169_get_wol(tp);
       
  1358 
       
  1359 	spin_unlock_irq(&tp->lock);
       
  1360 }
       
  1361 
       
  1362 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
       
  1363 {
       
  1364 	void __iomem *ioaddr = tp->mmio_addr;
       
  1365 	unsigned int i;
       
  1366 	static const struct {
       
  1367 		u32 opt;
       
  1368 		u16 reg;
       
  1369 		u8  mask;
       
  1370 	} cfg[] = {
       
  1371 		{ WAKE_ANY,   Config1, PMEnable },
       
  1372 		{ WAKE_PHY,   Config3, LinkUp },
       
  1373 		{ WAKE_MAGIC, Config3, MagicPacket },
       
  1374 		{ WAKE_UCAST, Config5, UWF },
       
  1375 		{ WAKE_BCAST, Config5, BWF },
       
  1376 		{ WAKE_MCAST, Config5, MWF },
       
  1377 		{ WAKE_ANY,   Config5, LanWake }
       
  1378 	};
       
  1379 
       
  1380 	RTL_W8(Cfg9346, Cfg9346_Unlock);
       
  1381 
       
  1382 	for (i = 0; i < ARRAY_SIZE(cfg); i++) {
       
  1383 		u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
       
  1384 		if (wolopts & cfg[i].opt)
       
  1385 			options |= cfg[i].mask;
       
  1386 		RTL_W8(cfg[i].reg, options);
       
  1387 	}
       
  1388 
       
  1389 	RTL_W8(Cfg9346, Cfg9346_Lock);
       
  1390 }
       
  1391 
       
  1392 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
       
  1393 {
       
  1394 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1395 
       
  1396 	spin_lock_irq(&tp->lock);
       
  1397 
       
  1398 	if (wol->wolopts)
       
  1399 		tp->features |= RTL_FEATURE_WOL;
       
  1400 	else
       
  1401 		tp->features &= ~RTL_FEATURE_WOL;
       
  1402 	__rtl8169_set_wol(tp, wol->wolopts);
       
  1403 	spin_unlock_irq(&tp->lock);
       
  1404 
       
  1405 	device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
       
  1406 
       
  1407 	return 0;
       
  1408 }
       
  1409 
       
  1410 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
       
  1411 {
       
  1412 	return rtl_chip_infos[tp->mac_version].fw_name;
       
  1413 }
       
  1414 
       
  1415 static void rtl8169_get_drvinfo(struct net_device *dev,
       
  1416 				struct ethtool_drvinfo *info)
       
  1417 {
       
  1418 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1419 	struct rtl_fw *rtl_fw = tp->rtl_fw;
       
  1420 
       
  1421 	strlcpy(info->driver, MODULENAME, sizeof(info->driver));
       
  1422 	strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
       
  1423 	strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
       
  1424 	BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
       
  1425 	strlcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
       
  1426 	       rtl_fw->version, sizeof(info->fw_version));
       
  1427 }
       
  1428 
       
  1429 static int rtl8169_get_regs_len(struct net_device *dev)
       
  1430 {
       
  1431 	return R8169_REGS_SIZE;
       
  1432 }
       
  1433 
       
  1434 static int rtl8169_set_speed_tbi(struct net_device *dev,
       
  1435 				 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
       
  1436 {
       
  1437 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1438 	void __iomem *ioaddr = tp->mmio_addr;
       
  1439 	int ret = 0;
       
  1440 	u32 reg;
       
  1441 
       
  1442 	reg = RTL_R32(TBICSR);
       
  1443 	if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
       
  1444 	    (duplex == DUPLEX_FULL)) {
       
  1445 		RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
       
  1446 	} else if (autoneg == AUTONEG_ENABLE)
       
  1447 		RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
       
  1448 	else {
       
  1449 		netif_warn(tp, link, dev,
       
  1450 			   "incorrect speed setting refused in TBI mode\n");
       
  1451 		ret = -EOPNOTSUPP;
       
  1452 	}
       
  1453 
       
  1454 	return ret;
       
  1455 }
       
  1456 
       
  1457 static int rtl8169_set_speed_xmii(struct net_device *dev,
       
  1458 				  u8 autoneg, u16 speed, u8 duplex, u32 adv)
       
  1459 {
       
  1460 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1461 	int giga_ctrl, bmcr;
       
  1462 	int rc = -EINVAL;
       
  1463 
       
  1464 	rtl_writephy(tp, 0x1f, 0x0000);
       
  1465 
       
  1466 	if (autoneg == AUTONEG_ENABLE) {
       
  1467 		int auto_nego;
       
  1468 
       
  1469 		auto_nego = rtl_readphy(tp, MII_ADVERTISE);
       
  1470 		auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
       
  1471 				ADVERTISE_100HALF | ADVERTISE_100FULL);
       
  1472 
       
  1473 		if (adv & ADVERTISED_10baseT_Half)
       
  1474 			auto_nego |= ADVERTISE_10HALF;
       
  1475 		if (adv & ADVERTISED_10baseT_Full)
       
  1476 			auto_nego |= ADVERTISE_10FULL;
       
  1477 		if (adv & ADVERTISED_100baseT_Half)
       
  1478 			auto_nego |= ADVERTISE_100HALF;
       
  1479 		if (adv & ADVERTISED_100baseT_Full)
       
  1480 			auto_nego |= ADVERTISE_100FULL;
       
  1481 
       
  1482 		auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
       
  1483 
       
  1484 		giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
       
  1485 		giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
       
  1486 
       
  1487 		/* The 8100e/8101e/8102e do Fast Ethernet only. */
       
  1488 		if (tp->mii.supports_gmii) {
       
  1489 			if (adv & ADVERTISED_1000baseT_Half)
       
  1490 				giga_ctrl |= ADVERTISE_1000HALF;
       
  1491 			if (adv & ADVERTISED_1000baseT_Full)
       
  1492 				giga_ctrl |= ADVERTISE_1000FULL;
       
  1493 		} else if (adv & (ADVERTISED_1000baseT_Half |
       
  1494 				  ADVERTISED_1000baseT_Full)) {
       
  1495 			netif_info(tp, link, dev,
       
  1496 				   "PHY does not support 1000Mbps\n");
       
  1497 			goto out;
       
  1498 		}
       
  1499 
       
  1500 		bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
       
  1501 
       
  1502 		rtl_writephy(tp, MII_ADVERTISE, auto_nego);
       
  1503 		rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
       
  1504 	} else {
       
  1505 		giga_ctrl = 0;
       
  1506 
       
  1507 		if (speed == SPEED_10)
       
  1508 			bmcr = 0;
       
  1509 		else if (speed == SPEED_100)
       
  1510 			bmcr = BMCR_SPEED100;
       
  1511 		else
       
  1512 			goto out;
       
  1513 
       
  1514 		if (duplex == DUPLEX_FULL)
       
  1515 			bmcr |= BMCR_FULLDPLX;
       
  1516 	}
       
  1517 
       
  1518 	rtl_writephy(tp, MII_BMCR, bmcr);
       
  1519 
       
  1520 	if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
       
  1521 	    tp->mac_version == RTL_GIGA_MAC_VER_03) {
       
  1522 		if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
       
  1523 			rtl_writephy(tp, 0x17, 0x2138);
       
  1524 			rtl_writephy(tp, 0x0e, 0x0260);
       
  1525 		} else {
       
  1526 			rtl_writephy(tp, 0x17, 0x2108);
       
  1527 			rtl_writephy(tp, 0x0e, 0x0000);
       
  1528 		}
       
  1529 	}
       
  1530 
       
  1531 	rc = 0;
       
  1532 out:
       
  1533 	return rc;
       
  1534 }
       
  1535 
       
  1536 static int rtl8169_set_speed(struct net_device *dev,
       
  1537 			     u8 autoneg, u16 speed, u8 duplex, u32 advertising)
       
  1538 {
       
  1539 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1540 	int ret;
       
  1541 
       
  1542 	ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
       
  1543 	if (ret < 0)
       
  1544 		goto out;
       
  1545 
       
  1546 	if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
       
  1547 	    (advertising & ADVERTISED_1000baseT_Full)) {
       
  1548 		mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
       
  1549 	}
       
  1550 out:
       
  1551 	return ret;
       
  1552 }
       
  1553 
       
  1554 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
       
  1555 {
       
  1556 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1557 	unsigned long flags;
       
  1558 	int ret;
       
  1559 
       
  1560 	del_timer_sync(&tp->timer);
       
  1561 
       
  1562 	spin_lock_irqsave(&tp->lock, flags);
       
  1563 	ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
       
  1564 				cmd->duplex, cmd->advertising);
       
  1565 	spin_unlock_irqrestore(&tp->lock, flags);
       
  1566 
       
  1567 	return ret;
       
  1568 }
       
  1569 
       
  1570 static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
       
  1571 {
       
  1572 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1573 
       
  1574 	if (dev->mtu > TD_MSS_MAX)
       
  1575 		features &= ~NETIF_F_ALL_TSO;
       
  1576 
       
  1577 	if (dev->mtu > JUMBO_1K &&
       
  1578 	    !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
       
  1579 		features &= ~NETIF_F_IP_CSUM;
       
  1580 
       
  1581 	return features;
       
  1582 }
       
  1583 
       
  1584 static int rtl8169_set_features(struct net_device *dev, u32 features)
       
  1585 {
       
  1586 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1587 	void __iomem *ioaddr = tp->mmio_addr;
       
  1588 	unsigned long flags;
       
  1589 
       
  1590 	spin_lock_irqsave(&tp->lock, flags);
       
  1591 
       
  1592 	if (features & NETIF_F_RXCSUM)
       
  1593 		tp->cp_cmd |= RxChkSum;
       
  1594 	else
       
  1595 		tp->cp_cmd &= ~RxChkSum;
       
  1596 
       
  1597 	if (dev->features & NETIF_F_HW_VLAN_RX)
       
  1598 		tp->cp_cmd |= RxVlan;
       
  1599 	else
       
  1600 		tp->cp_cmd &= ~RxVlan;
       
  1601 
       
  1602 	RTL_W16(CPlusCmd, tp->cp_cmd);
       
  1603 	RTL_R16(CPlusCmd);
       
  1604 
       
  1605 	spin_unlock_irqrestore(&tp->lock, flags);
       
  1606 
       
  1607 	return 0;
       
  1608 }
       
  1609 
       
  1610 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
       
  1611 				      struct sk_buff *skb)
       
  1612 {
       
  1613 	return (vlan_tx_tag_present(skb)) ?
       
  1614 		TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
       
  1615 }
       
  1616 
       
  1617 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
       
  1618 {
       
  1619 	u32 opts2 = le32_to_cpu(desc->opts2);
       
  1620 
       
  1621 	if (opts2 & RxVlanTag)
       
  1622 		__vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
       
  1623 
       
  1624 	desc->opts2 = 0;
       
  1625 }
       
  1626 
       
  1627 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
       
  1628 {
       
  1629 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1630 	void __iomem *ioaddr = tp->mmio_addr;
       
  1631 	u32 status;
       
  1632 
       
  1633 	cmd->supported =
       
  1634 		SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
       
  1635 	cmd->port = PORT_FIBRE;
       
  1636 	cmd->transceiver = XCVR_INTERNAL;
       
  1637 
       
  1638 	status = RTL_R32(TBICSR);
       
  1639 	cmd->advertising = (status & TBINwEnable) ?  ADVERTISED_Autoneg : 0;
       
  1640 	cmd->autoneg = !!(status & TBINwEnable);
       
  1641 
       
  1642 	ethtool_cmd_speed_set(cmd, SPEED_1000);
       
  1643 	cmd->duplex = DUPLEX_FULL; /* Always set */
       
  1644 
       
  1645 	return 0;
       
  1646 }
       
  1647 
       
  1648 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
       
  1649 {
       
  1650 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1651 
       
  1652 	return mii_ethtool_gset(&tp->mii, cmd);
       
  1653 }
       
  1654 
       
  1655 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
       
  1656 {
       
  1657 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1658 	unsigned long flags;
       
  1659 	int rc;
       
  1660 
       
  1661 	spin_lock_irqsave(&tp->lock, flags);
       
  1662 
       
  1663 	rc = tp->get_settings(dev, cmd);
       
  1664 
       
  1665 	spin_unlock_irqrestore(&tp->lock, flags);
       
  1666 	return rc;
       
  1667 }
       
  1668 
       
  1669 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
       
  1670 			     void *p)
       
  1671 {
       
  1672 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1673 	unsigned long flags;
       
  1674 
       
  1675 	if (regs->len > R8169_REGS_SIZE)
       
  1676 		regs->len = R8169_REGS_SIZE;
       
  1677 
       
  1678 	spin_lock_irqsave(&tp->lock, flags);
       
  1679 	memcpy_fromio(p, tp->mmio_addr, regs->len);
       
  1680 	spin_unlock_irqrestore(&tp->lock, flags);
       
  1681 }
       
  1682 
       
  1683 static u32 rtl8169_get_msglevel(struct net_device *dev)
       
  1684 {
       
  1685 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1686 
       
  1687 	return tp->msg_enable;
       
  1688 }
       
  1689 
       
  1690 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
       
  1691 {
       
  1692 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1693 
       
  1694 	tp->msg_enable = value;
       
  1695 }
       
  1696 
       
  1697 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
       
  1698 	"tx_packets",
       
  1699 	"rx_packets",
       
  1700 	"tx_errors",
       
  1701 	"rx_errors",
       
  1702 	"rx_missed",
       
  1703 	"align_errors",
       
  1704 	"tx_single_collisions",
       
  1705 	"tx_multi_collisions",
       
  1706 	"unicast",
       
  1707 	"broadcast",
       
  1708 	"multicast",
       
  1709 	"tx_aborted",
       
  1710 	"tx_underrun",
       
  1711 };
       
  1712 
       
  1713 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
       
  1714 {
       
  1715 	switch (sset) {
       
  1716 	case ETH_SS_STATS:
       
  1717 		return ARRAY_SIZE(rtl8169_gstrings);
       
  1718 	default:
       
  1719 		return -EOPNOTSUPP;
       
  1720 	}
       
  1721 }
       
  1722 
       
  1723 static void rtl8169_update_counters(struct net_device *dev)
       
  1724 {
       
  1725 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1726 	void __iomem *ioaddr = tp->mmio_addr;
       
  1727 	struct device *d = &tp->pci_dev->dev;
       
  1728 	struct rtl8169_counters *counters;
       
  1729 	dma_addr_t paddr;
       
  1730 	u32 cmd;
       
  1731 	int wait = 1000;
       
  1732 
       
  1733 	/*
       
  1734 	 * Some chips are unable to dump tally counters when the receiver
       
  1735 	 * is disabled.
       
  1736 	 */
       
  1737 	if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
       
  1738 		return;
       
  1739 
       
  1740 	counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
       
  1741 	if (!counters)
       
  1742 		return;
       
  1743 
       
  1744 	RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
       
  1745 	cmd = (u64)paddr & DMA_BIT_MASK(32);
       
  1746 	RTL_W32(CounterAddrLow, cmd);
       
  1747 	RTL_W32(CounterAddrLow, cmd | CounterDump);
       
  1748 
       
  1749 	while (wait--) {
       
  1750 		if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
       
  1751 			memcpy(&tp->counters, counters, sizeof(*counters));
       
  1752 			break;
       
  1753 		}
       
  1754 		udelay(10);
       
  1755 	}
       
  1756 
       
  1757 	RTL_W32(CounterAddrLow, 0);
       
  1758 	RTL_W32(CounterAddrHigh, 0);
       
  1759 
       
  1760 	dma_free_coherent(d, sizeof(*counters), counters, paddr);
       
  1761 }
       
  1762 
       
  1763 static void rtl8169_get_ethtool_stats(struct net_device *dev,
       
  1764 				      struct ethtool_stats *stats, u64 *data)
       
  1765 {
       
  1766 	struct rtl8169_private *tp = netdev_priv(dev);
       
  1767 
       
  1768 	ASSERT_RTNL();
       
  1769 
       
  1770 	rtl8169_update_counters(dev);
       
  1771 
       
  1772 	data[0] = le64_to_cpu(tp->counters.tx_packets);
       
  1773 	data[1] = le64_to_cpu(tp->counters.rx_packets);
       
  1774 	data[2] = le64_to_cpu(tp->counters.tx_errors);
       
  1775 	data[3] = le32_to_cpu(tp->counters.rx_errors);
       
  1776 	data[4] = le16_to_cpu(tp->counters.rx_missed);
       
  1777 	data[5] = le16_to_cpu(tp->counters.align_errors);
       
  1778 	data[6] = le32_to_cpu(tp->counters.tx_one_collision);
       
  1779 	data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
       
  1780 	data[8] = le64_to_cpu(tp->counters.rx_unicast);
       
  1781 	data[9] = le64_to_cpu(tp->counters.rx_broadcast);
       
  1782 	data[10] = le32_to_cpu(tp->counters.rx_multicast);
       
  1783 	data[11] = le16_to_cpu(tp->counters.tx_aborted);
       
  1784 	data[12] = le16_to_cpu(tp->counters.tx_underun);
       
  1785 }
       
  1786 
       
  1787 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
       
  1788 {
       
  1789 	switch(stringset) {
       
  1790 	case ETH_SS_STATS:
       
  1791 		memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
       
  1792 		break;
       
  1793 	}
       
  1794 }
       
  1795 
       
  1796 static const struct ethtool_ops rtl8169_ethtool_ops = {
       
  1797 	.get_drvinfo		= rtl8169_get_drvinfo,
       
  1798 	.get_regs_len		= rtl8169_get_regs_len,
       
  1799 	.get_link		= ethtool_op_get_link,
       
  1800 	.get_settings		= rtl8169_get_settings,
       
  1801 	.set_settings		= rtl8169_set_settings,
       
  1802 	.get_msglevel		= rtl8169_get_msglevel,
       
  1803 	.set_msglevel		= rtl8169_set_msglevel,
       
  1804 	.get_regs		= rtl8169_get_regs,
       
  1805 	.get_wol		= rtl8169_get_wol,
       
  1806 	.set_wol		= rtl8169_set_wol,
       
  1807 	.get_strings		= rtl8169_get_strings,
       
  1808 	.get_sset_count		= rtl8169_get_sset_count,
       
  1809 	.get_ethtool_stats	= rtl8169_get_ethtool_stats,
       
  1810 };
       
  1811 
       
  1812 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
       
  1813 				    struct net_device *dev, u8 default_version)
       
  1814 {
       
  1815 	void __iomem *ioaddr = tp->mmio_addr;
       
  1816 	/*
       
  1817 	 * The driver currently handles the 8168Bf and the 8168Be identically
       
  1818 	 * but they can be identified more specifically through the test below
       
  1819 	 * if needed:
       
  1820 	 *
       
  1821 	 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
       
  1822 	 *
       
  1823 	 * Same thing for the 8101Eb and the 8101Ec:
       
  1824 	 *
       
  1825 	 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
       
  1826 	 */
       
  1827 	static const struct rtl_mac_info {
       
  1828 		u32 mask;
       
  1829 		u32 val;
       
  1830 		int mac_version;
       
  1831 	} mac_info[] = {
       
  1832 		/* 8168F family. */
       
  1833 		{ 0x7cf00000, 0x48100000,	RTL_GIGA_MAC_VER_36 },
       
  1834 		{ 0x7cf00000, 0x48000000,	RTL_GIGA_MAC_VER_35 },
       
  1835 
       
  1836 		/* 8168E family. */
       
  1837 		{ 0x7c800000, 0x2c800000,	RTL_GIGA_MAC_VER_34 },
       
  1838 		{ 0x7cf00000, 0x2c200000,	RTL_GIGA_MAC_VER_33 },
       
  1839 		{ 0x7cf00000, 0x2c100000,	RTL_GIGA_MAC_VER_32 },
       
  1840 		{ 0x7c800000, 0x2c000000,	RTL_GIGA_MAC_VER_33 },
       
  1841 
       
  1842 		/* 8168D family. */
       
  1843 		{ 0x7cf00000, 0x28300000,	RTL_GIGA_MAC_VER_26 },
       
  1844 		{ 0x7cf00000, 0x28100000,	RTL_GIGA_MAC_VER_25 },
       
  1845 		{ 0x7c800000, 0x28000000,	RTL_GIGA_MAC_VER_26 },
       
  1846 
       
  1847 		/* 8168DP family. */
       
  1848 		{ 0x7cf00000, 0x28800000,	RTL_GIGA_MAC_VER_27 },
       
  1849 		{ 0x7cf00000, 0x28a00000,	RTL_GIGA_MAC_VER_28 },
       
  1850 		{ 0x7cf00000, 0x28b00000,	RTL_GIGA_MAC_VER_31 },
       
  1851 
       
  1852 		/* 8168C family. */
       
  1853 		{ 0x7cf00000, 0x3cb00000,	RTL_GIGA_MAC_VER_24 },
       
  1854 		{ 0x7cf00000, 0x3c900000,	RTL_GIGA_MAC_VER_23 },
       
  1855 		{ 0x7cf00000, 0x3c800000,	RTL_GIGA_MAC_VER_18 },
       
  1856 		{ 0x7c800000, 0x3c800000,	RTL_GIGA_MAC_VER_24 },
       
  1857 		{ 0x7cf00000, 0x3c000000,	RTL_GIGA_MAC_VER_19 },
       
  1858 		{ 0x7cf00000, 0x3c200000,	RTL_GIGA_MAC_VER_20 },
       
  1859 		{ 0x7cf00000, 0x3c300000,	RTL_GIGA_MAC_VER_21 },
       
  1860 		{ 0x7cf00000, 0x3c400000,	RTL_GIGA_MAC_VER_22 },
       
  1861 		{ 0x7c800000, 0x3c000000,	RTL_GIGA_MAC_VER_22 },
       
  1862 
       
  1863 		/* 8168B family. */
       
  1864 		{ 0x7cf00000, 0x38000000,	RTL_GIGA_MAC_VER_12 },
       
  1865 		{ 0x7cf00000, 0x38500000,	RTL_GIGA_MAC_VER_17 },
       
  1866 		{ 0x7c800000, 0x38000000,	RTL_GIGA_MAC_VER_17 },
       
  1867 		{ 0x7c800000, 0x30000000,	RTL_GIGA_MAC_VER_11 },
       
  1868 
       
  1869 		/* 8101 family. */
       
  1870 		{ 0x7cf00000, 0x40b00000,	RTL_GIGA_MAC_VER_30 },
       
  1871 		{ 0x7cf00000, 0x40a00000,	RTL_GIGA_MAC_VER_30 },
       
  1872 		{ 0x7cf00000, 0x40900000,	RTL_GIGA_MAC_VER_29 },
       
  1873 		{ 0x7c800000, 0x40800000,	RTL_GIGA_MAC_VER_30 },
       
  1874 		{ 0x7cf00000, 0x34a00000,	RTL_GIGA_MAC_VER_09 },
       
  1875 		{ 0x7cf00000, 0x24a00000,	RTL_GIGA_MAC_VER_09 },
       
  1876 		{ 0x7cf00000, 0x34900000,	RTL_GIGA_MAC_VER_08 },
       
  1877 		{ 0x7cf00000, 0x24900000,	RTL_GIGA_MAC_VER_08 },
       
  1878 		{ 0x7cf00000, 0x34800000,	RTL_GIGA_MAC_VER_07 },
       
  1879 		{ 0x7cf00000, 0x24800000,	RTL_GIGA_MAC_VER_07 },
       
  1880 		{ 0x7cf00000, 0x34000000,	RTL_GIGA_MAC_VER_13 },
       
  1881 		{ 0x7cf00000, 0x34300000,	RTL_GIGA_MAC_VER_10 },
       
  1882 		{ 0x7cf00000, 0x34200000,	RTL_GIGA_MAC_VER_16 },
       
  1883 		{ 0x7c800000, 0x34800000,	RTL_GIGA_MAC_VER_09 },
       
  1884 		{ 0x7c800000, 0x24800000,	RTL_GIGA_MAC_VER_09 },
       
  1885 		{ 0x7c800000, 0x34000000,	RTL_GIGA_MAC_VER_16 },
       
  1886 		/* FIXME: where did these entries come from ? -- FR */
       
  1887 		{ 0xfc800000, 0x38800000,	RTL_GIGA_MAC_VER_15 },
       
  1888 		{ 0xfc800000, 0x30800000,	RTL_GIGA_MAC_VER_14 },
       
  1889 
       
  1890 		/* 8110 family. */
       
  1891 		{ 0xfc800000, 0x98000000,	RTL_GIGA_MAC_VER_06 },
       
  1892 		{ 0xfc800000, 0x18000000,	RTL_GIGA_MAC_VER_05 },
       
  1893 		{ 0xfc800000, 0x10000000,	RTL_GIGA_MAC_VER_04 },
       
  1894 		{ 0xfc800000, 0x04000000,	RTL_GIGA_MAC_VER_03 },
       
  1895 		{ 0xfc800000, 0x00800000,	RTL_GIGA_MAC_VER_02 },
       
  1896 		{ 0xfc800000, 0x00000000,	RTL_GIGA_MAC_VER_01 },
       
  1897 
       
  1898 		/* Catch-all */
       
  1899 		{ 0x00000000, 0x00000000,	RTL_GIGA_MAC_NONE   }
       
  1900 	};
       
  1901 	const struct rtl_mac_info *p = mac_info;
       
  1902 	u32 reg;
       
  1903 
       
  1904 	reg = RTL_R32(TxConfig);
       
  1905 	while ((reg & p->mask) != p->val)
       
  1906 		p++;
       
  1907 	tp->mac_version = p->mac_version;
       
  1908 
       
  1909 	if (tp->mac_version == RTL_GIGA_MAC_NONE) {
       
  1910 		netif_notice(tp, probe, dev,
       
  1911 			     "unknown MAC, using family default\n");
       
  1912 		tp->mac_version = default_version;
       
  1913 	}
       
  1914 }
       
  1915 
       
  1916 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
       
  1917 {
       
  1918 	dprintk("mac_version = 0x%02x\n", tp->mac_version);
       
  1919 }
       
  1920 
       
  1921 struct phy_reg {
       
  1922 	u16 reg;
       
  1923 	u16 val;
       
  1924 };
       
  1925 
       
  1926 static void rtl_writephy_batch(struct rtl8169_private *tp,
       
  1927 			       const struct phy_reg *regs, int len)
       
  1928 {
       
  1929 	while (len-- > 0) {
       
  1930 		rtl_writephy(tp, regs->reg, regs->val);
       
  1931 		regs++;
       
  1932 	}
       
  1933 }
       
  1934 
       
  1935 #define PHY_READ		0x00000000
       
  1936 #define PHY_DATA_OR		0x10000000
       
  1937 #define PHY_DATA_AND		0x20000000
       
  1938 #define PHY_BJMPN		0x30000000
       
  1939 #define PHY_READ_EFUSE		0x40000000
       
  1940 #define PHY_READ_MAC_BYTE	0x50000000
       
  1941 #define PHY_WRITE_MAC_BYTE	0x60000000
       
  1942 #define PHY_CLEAR_READCOUNT	0x70000000
       
  1943 #define PHY_WRITE		0x80000000
       
  1944 #define PHY_READCOUNT_EQ_SKIP	0x90000000
       
  1945 #define PHY_COMP_EQ_SKIPN	0xa0000000
       
  1946 #define PHY_COMP_NEQ_SKIPN	0xb0000000
       
  1947 #define PHY_WRITE_PREVIOUS	0xc0000000
       
  1948 #define PHY_SKIPN		0xd0000000
       
  1949 #define PHY_DELAY_MS		0xe0000000
       
  1950 #define PHY_WRITE_ERI_WORD	0xf0000000
       
  1951 
       
  1952 struct fw_info {
       
  1953 	u32	magic;
       
  1954 	char	version[RTL_VER_SIZE];
       
  1955 	__le32	fw_start;
       
  1956 	__le32	fw_len;
       
  1957 	u8	chksum;
       
  1958 } __packed;
       
  1959 
       
  1960 #define FW_OPCODE_SIZE	sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
       
  1961 
       
  1962 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
       
  1963 {
       
  1964 	const struct firmware *fw = rtl_fw->fw;
       
  1965 	struct fw_info *fw_info = (struct fw_info *)fw->data;
       
  1966 	struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
       
  1967 	char *version = rtl_fw->version;
       
  1968 	bool rc = false;
       
  1969 
       
  1970 	if (fw->size < FW_OPCODE_SIZE)
       
  1971 		goto out;
       
  1972 
       
  1973 	if (!fw_info->magic) {
       
  1974 		size_t i, size, start;
       
  1975 		u8 checksum = 0;
       
  1976 
       
  1977 		if (fw->size < sizeof(*fw_info))
       
  1978 			goto out;
       
  1979 
       
  1980 		for (i = 0; i < fw->size; i++)
       
  1981 			checksum += fw->data[i];
       
  1982 		if (checksum != 0)
       
  1983 			goto out;
       
  1984 
       
  1985 		start = le32_to_cpu(fw_info->fw_start);
       
  1986 		if (start > fw->size)
       
  1987 			goto out;
       
  1988 
       
  1989 		size = le32_to_cpu(fw_info->fw_len);
       
  1990 		if (size > (fw->size - start) / FW_OPCODE_SIZE)
       
  1991 			goto out;
       
  1992 
       
  1993 		memcpy(version, fw_info->version, RTL_VER_SIZE);
       
  1994 
       
  1995 		pa->code = (__le32 *)(fw->data + start);
       
  1996 		pa->size = size;
       
  1997 	} else {
       
  1998 		if (fw->size % FW_OPCODE_SIZE)
       
  1999 			goto out;
       
  2000 
       
  2001 		strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
       
  2002 
       
  2003 		pa->code = (__le32 *)fw->data;
       
  2004 		pa->size = fw->size / FW_OPCODE_SIZE;
       
  2005 	}
       
  2006 	version[RTL_VER_SIZE - 1] = 0;
       
  2007 
       
  2008 	rc = true;
       
  2009 out:
       
  2010 	return rc;
       
  2011 }
       
  2012 
       
  2013 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
       
  2014 			   struct rtl_fw_phy_action *pa)
       
  2015 {
       
  2016 	bool rc = false;
       
  2017 	size_t index;
       
  2018 
       
  2019 	for (index = 0; index < pa->size; index++) {
       
  2020 		u32 action = le32_to_cpu(pa->code[index]);
       
  2021 		u32 regno = (action & 0x0fff0000) >> 16;
       
  2022 
       
  2023 		switch(action & 0xf0000000) {
       
  2024 		case PHY_READ:
       
  2025 		case PHY_DATA_OR:
       
  2026 		case PHY_DATA_AND:
       
  2027 		case PHY_READ_EFUSE:
       
  2028 		case PHY_CLEAR_READCOUNT:
       
  2029 		case PHY_WRITE:
       
  2030 		case PHY_WRITE_PREVIOUS:
       
  2031 		case PHY_DELAY_MS:
       
  2032 			break;
       
  2033 
       
  2034 		case PHY_BJMPN:
       
  2035 			if (regno > index) {
       
  2036 				netif_err(tp, ifup, tp->dev,
       
  2037 					  "Out of range of firmware\n");
       
  2038 				goto out;
       
  2039 			}
       
  2040 			break;
       
  2041 		case PHY_READCOUNT_EQ_SKIP:
       
  2042 			if (index + 2 >= pa->size) {
       
  2043 				netif_err(tp, ifup, tp->dev,
       
  2044 					  "Out of range of firmware\n");
       
  2045 				goto out;
       
  2046 			}
       
  2047 			break;
       
  2048 		case PHY_COMP_EQ_SKIPN:
       
  2049 		case PHY_COMP_NEQ_SKIPN:
       
  2050 		case PHY_SKIPN:
       
  2051 			if (index + 1 + regno >= pa->size) {
       
  2052 				netif_err(tp, ifup, tp->dev,
       
  2053 					  "Out of range of firmware\n");
       
  2054 				goto out;
       
  2055 			}
       
  2056 			break;
       
  2057 
       
  2058 		case PHY_READ_MAC_BYTE:
       
  2059 		case PHY_WRITE_MAC_BYTE:
       
  2060 		case PHY_WRITE_ERI_WORD:
       
  2061 		default:
       
  2062 			netif_err(tp, ifup, tp->dev,
       
  2063 				  "Invalid action 0x%08x\n", action);
       
  2064 			goto out;
       
  2065 		}
       
  2066 	}
       
  2067 	rc = true;
       
  2068 out:
       
  2069 	return rc;
       
  2070 }
       
  2071 
       
  2072 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
       
  2073 {
       
  2074 	struct net_device *dev = tp->dev;
       
  2075 	int rc = -EINVAL;
       
  2076 
       
  2077 	if (!rtl_fw_format_ok(tp, rtl_fw)) {
       
  2078 		netif_err(tp, ifup, dev, "invalid firwmare\n");
       
  2079 		goto out;
       
  2080 	}
       
  2081 
       
  2082 	if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
       
  2083 		rc = 0;
       
  2084 out:
       
  2085 	return rc;
       
  2086 }
       
  2087 
       
  2088 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
       
  2089 {
       
  2090 	struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
       
  2091 	u32 predata, count;
       
  2092 	size_t index;
       
  2093 
       
  2094 	predata = count = 0;
       
  2095 
       
  2096 	for (index = 0; index < pa->size; ) {
       
  2097 		u32 action = le32_to_cpu(pa->code[index]);
       
  2098 		u32 data = action & 0x0000ffff;
       
  2099 		u32 regno = (action & 0x0fff0000) >> 16;
       
  2100 
       
  2101 		if (!action)
       
  2102 			break;
       
  2103 
       
  2104 		switch(action & 0xf0000000) {
       
  2105 		case PHY_READ:
       
  2106 			predata = rtl_readphy(tp, regno);
       
  2107 			count++;
       
  2108 			index++;
       
  2109 			break;
       
  2110 		case PHY_DATA_OR:
       
  2111 			predata |= data;
       
  2112 			index++;
       
  2113 			break;
       
  2114 		case PHY_DATA_AND:
       
  2115 			predata &= data;
       
  2116 			index++;
       
  2117 			break;
       
  2118 		case PHY_BJMPN:
       
  2119 			index -= regno;
       
  2120 			break;
       
  2121 		case PHY_READ_EFUSE:
       
  2122 			predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
       
  2123 			index++;
       
  2124 			break;
       
  2125 		case PHY_CLEAR_READCOUNT:
       
  2126 			count = 0;
       
  2127 			index++;
       
  2128 			break;
       
  2129 		case PHY_WRITE:
       
  2130 			rtl_writephy(tp, regno, data);
       
  2131 			index++;
       
  2132 			break;
       
  2133 		case PHY_READCOUNT_EQ_SKIP:
       
  2134 			index += (count == data) ? 2 : 1;
       
  2135 			break;
       
  2136 		case PHY_COMP_EQ_SKIPN:
       
  2137 			if (predata == data)
       
  2138 				index += regno;
       
  2139 			index++;
       
  2140 			break;
       
  2141 		case PHY_COMP_NEQ_SKIPN:
       
  2142 			if (predata != data)
       
  2143 				index += regno;
       
  2144 			index++;
       
  2145 			break;
       
  2146 		case PHY_WRITE_PREVIOUS:
       
  2147 			rtl_writephy(tp, regno, predata);
       
  2148 			index++;
       
  2149 			break;
       
  2150 		case PHY_SKIPN:
       
  2151 			index += regno + 1;
       
  2152 			break;
       
  2153 		case PHY_DELAY_MS:
       
  2154 			mdelay(data);
       
  2155 			index++;
       
  2156 			break;
       
  2157 
       
  2158 		case PHY_READ_MAC_BYTE:
       
  2159 		case PHY_WRITE_MAC_BYTE:
       
  2160 		case PHY_WRITE_ERI_WORD:
       
  2161 		default:
       
  2162 			BUG();
       
  2163 		}
       
  2164 	}
       
  2165 }
       
  2166 
       
  2167 static void rtl_release_firmware(struct rtl8169_private *tp)
       
  2168 {
       
  2169 	if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
       
  2170 		release_firmware(tp->rtl_fw->fw);
       
  2171 		kfree(tp->rtl_fw);
       
  2172 	}
       
  2173 	tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
       
  2174 }
       
  2175 
       
  2176 static void rtl_apply_firmware(struct rtl8169_private *tp)
       
  2177 {
       
  2178 	struct rtl_fw *rtl_fw = tp->rtl_fw;
       
  2179 
       
  2180 	/* TODO: release firmware once rtl_phy_write_fw signals failures. */
       
  2181 	if (!IS_ERR_OR_NULL(rtl_fw))
       
  2182 		rtl_phy_write_fw(tp, rtl_fw);
       
  2183 }
       
  2184 
       
  2185 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
       
  2186 {
       
  2187 	if (rtl_readphy(tp, reg) != val)
       
  2188 		netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
       
  2189 	else
       
  2190 		rtl_apply_firmware(tp);
       
  2191 }
       
  2192 
       
  2193 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
       
  2194 {
       
  2195 	static const struct phy_reg phy_reg_init[] = {
       
  2196 		{ 0x1f, 0x0001 },
       
  2197 		{ 0x06, 0x006e },
       
  2198 		{ 0x08, 0x0708 },
       
  2199 		{ 0x15, 0x4000 },
       
  2200 		{ 0x18, 0x65c7 },
       
  2201 
       
  2202 		{ 0x1f, 0x0001 },
       
  2203 		{ 0x03, 0x00a1 },
       
  2204 		{ 0x02, 0x0008 },
       
  2205 		{ 0x01, 0x0120 },
       
  2206 		{ 0x00, 0x1000 },
       
  2207 		{ 0x04, 0x0800 },
       
  2208 		{ 0x04, 0x0000 },
       
  2209 
       
  2210 		{ 0x03, 0xff41 },
       
  2211 		{ 0x02, 0xdf60 },
       
  2212 		{ 0x01, 0x0140 },
       
  2213 		{ 0x00, 0x0077 },
       
  2214 		{ 0x04, 0x7800 },
       
  2215 		{ 0x04, 0x7000 },
       
  2216 
       
  2217 		{ 0x03, 0x802f },
       
  2218 		{ 0x02, 0x4f02 },
       
  2219 		{ 0x01, 0x0409 },
       
  2220 		{ 0x00, 0xf0f9 },
       
  2221 		{ 0x04, 0x9800 },
       
  2222 		{ 0x04, 0x9000 },
       
  2223 
       
  2224 		{ 0x03, 0xdf01 },
       
  2225 		{ 0x02, 0xdf20 },
       
  2226 		{ 0x01, 0xff95 },
       
  2227 		{ 0x00, 0xba00 },
       
  2228 		{ 0x04, 0xa800 },
       
  2229 		{ 0x04, 0xa000 },
       
  2230 
       
  2231 		{ 0x03, 0xff41 },
       
  2232 		{ 0x02, 0xdf20 },
       
  2233 		{ 0x01, 0x0140 },
       
  2234 		{ 0x00, 0x00bb },
       
  2235 		{ 0x04, 0xb800 },
       
  2236 		{ 0x04, 0xb000 },
       
  2237 
       
  2238 		{ 0x03, 0xdf41 },
       
  2239 		{ 0x02, 0xdc60 },
       
  2240 		{ 0x01, 0x6340 },
       
  2241 		{ 0x00, 0x007d },
       
  2242 		{ 0x04, 0xd800 },
       
  2243 		{ 0x04, 0xd000 },
       
  2244 
       
  2245 		{ 0x03, 0xdf01 },
       
  2246 		{ 0x02, 0xdf20 },
       
  2247 		{ 0x01, 0x100a },
       
  2248 		{ 0x00, 0xa0ff },
       
  2249 		{ 0x04, 0xf800 },
       
  2250 		{ 0x04, 0xf000 },
       
  2251 
       
  2252 		{ 0x1f, 0x0000 },
       
  2253 		{ 0x0b, 0x0000 },
       
  2254 		{ 0x00, 0x9200 }
       
  2255 	};
       
  2256 
       
  2257 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2258 }
       
  2259 
       
  2260 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
       
  2261 {
       
  2262 	static const struct phy_reg phy_reg_init[] = {
       
  2263 		{ 0x1f, 0x0002 },
       
  2264 		{ 0x01, 0x90d0 },
       
  2265 		{ 0x1f, 0x0000 }
       
  2266 	};
       
  2267 
       
  2268 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2269 }
       
  2270 
       
  2271 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
       
  2272 {
       
  2273 	struct pci_dev *pdev = tp->pci_dev;
       
  2274 
       
  2275 	if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
       
  2276 	    (pdev->subsystem_device != 0xe000))
       
  2277 		return;
       
  2278 
       
  2279 	rtl_writephy(tp, 0x1f, 0x0001);
       
  2280 	rtl_writephy(tp, 0x10, 0xf01b);
       
  2281 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2282 }
       
  2283 
       
  2284 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
       
  2285 {
       
  2286 	static const struct phy_reg phy_reg_init[] = {
       
  2287 		{ 0x1f, 0x0001 },
       
  2288 		{ 0x04, 0x0000 },
       
  2289 		{ 0x03, 0x00a1 },
       
  2290 		{ 0x02, 0x0008 },
       
  2291 		{ 0x01, 0x0120 },
       
  2292 		{ 0x00, 0x1000 },
       
  2293 		{ 0x04, 0x0800 },
       
  2294 		{ 0x04, 0x9000 },
       
  2295 		{ 0x03, 0x802f },
       
  2296 		{ 0x02, 0x4f02 },
       
  2297 		{ 0x01, 0x0409 },
       
  2298 		{ 0x00, 0xf099 },
       
  2299 		{ 0x04, 0x9800 },
       
  2300 		{ 0x04, 0xa000 },
       
  2301 		{ 0x03, 0xdf01 },
       
  2302 		{ 0x02, 0xdf20 },
       
  2303 		{ 0x01, 0xff95 },
       
  2304 		{ 0x00, 0xba00 },
       
  2305 		{ 0x04, 0xa800 },
       
  2306 		{ 0x04, 0xf000 },
       
  2307 		{ 0x03, 0xdf01 },
       
  2308 		{ 0x02, 0xdf20 },
       
  2309 		{ 0x01, 0x101a },
       
  2310 		{ 0x00, 0xa0ff },
       
  2311 		{ 0x04, 0xf800 },
       
  2312 		{ 0x04, 0x0000 },
       
  2313 		{ 0x1f, 0x0000 },
       
  2314 
       
  2315 		{ 0x1f, 0x0001 },
       
  2316 		{ 0x10, 0xf41b },
       
  2317 		{ 0x14, 0xfb54 },
       
  2318 		{ 0x18, 0xf5c7 },
       
  2319 		{ 0x1f, 0x0000 },
       
  2320 
       
  2321 		{ 0x1f, 0x0001 },
       
  2322 		{ 0x17, 0x0cc0 },
       
  2323 		{ 0x1f, 0x0000 }
       
  2324 	};
       
  2325 
       
  2326 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2327 
       
  2328 	rtl8169scd_hw_phy_config_quirk(tp);
       
  2329 }
       
  2330 
       
  2331 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
       
  2332 {
       
  2333 	static const struct phy_reg phy_reg_init[] = {
       
  2334 		{ 0x1f, 0x0001 },
       
  2335 		{ 0x04, 0x0000 },
       
  2336 		{ 0x03, 0x00a1 },
       
  2337 		{ 0x02, 0x0008 },
       
  2338 		{ 0x01, 0x0120 },
       
  2339 		{ 0x00, 0x1000 },
       
  2340 		{ 0x04, 0x0800 },
       
  2341 		{ 0x04, 0x9000 },
       
  2342 		{ 0x03, 0x802f },
       
  2343 		{ 0x02, 0x4f02 },
       
  2344 		{ 0x01, 0x0409 },
       
  2345 		{ 0x00, 0xf099 },
       
  2346 		{ 0x04, 0x9800 },
       
  2347 		{ 0x04, 0xa000 },
       
  2348 		{ 0x03, 0xdf01 },
       
  2349 		{ 0x02, 0xdf20 },
       
  2350 		{ 0x01, 0xff95 },
       
  2351 		{ 0x00, 0xba00 },
       
  2352 		{ 0x04, 0xa800 },
       
  2353 		{ 0x04, 0xf000 },
       
  2354 		{ 0x03, 0xdf01 },
       
  2355 		{ 0x02, 0xdf20 },
       
  2356 		{ 0x01, 0x101a },
       
  2357 		{ 0x00, 0xa0ff },
       
  2358 		{ 0x04, 0xf800 },
       
  2359 		{ 0x04, 0x0000 },
       
  2360 		{ 0x1f, 0x0000 },
       
  2361 
       
  2362 		{ 0x1f, 0x0001 },
       
  2363 		{ 0x0b, 0x8480 },
       
  2364 		{ 0x1f, 0x0000 },
       
  2365 
       
  2366 		{ 0x1f, 0x0001 },
       
  2367 		{ 0x18, 0x67c7 },
       
  2368 		{ 0x04, 0x2000 },
       
  2369 		{ 0x03, 0x002f },
       
  2370 		{ 0x02, 0x4360 },
       
  2371 		{ 0x01, 0x0109 },
       
  2372 		{ 0x00, 0x3022 },
       
  2373 		{ 0x04, 0x2800 },
       
  2374 		{ 0x1f, 0x0000 },
       
  2375 
       
  2376 		{ 0x1f, 0x0001 },
       
  2377 		{ 0x17, 0x0cc0 },
       
  2378 		{ 0x1f, 0x0000 }
       
  2379 	};
       
  2380 
       
  2381 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2382 }
       
  2383 
       
  2384 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
       
  2385 {
       
  2386 	static const struct phy_reg phy_reg_init[] = {
       
  2387 		{ 0x10, 0xf41b },
       
  2388 		{ 0x1f, 0x0000 }
       
  2389 	};
       
  2390 
       
  2391 	rtl_writephy(tp, 0x1f, 0x0001);
       
  2392 	rtl_patchphy(tp, 0x16, 1 << 0);
       
  2393 
       
  2394 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2395 }
       
  2396 
       
  2397 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
       
  2398 {
       
  2399 	static const struct phy_reg phy_reg_init[] = {
       
  2400 		{ 0x1f, 0x0001 },
       
  2401 		{ 0x10, 0xf41b },
       
  2402 		{ 0x1f, 0x0000 }
       
  2403 	};
       
  2404 
       
  2405 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2406 }
       
  2407 
       
  2408 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
       
  2409 {
       
  2410 	static const struct phy_reg phy_reg_init[] = {
       
  2411 		{ 0x1f, 0x0000 },
       
  2412 		{ 0x1d, 0x0f00 },
       
  2413 		{ 0x1f, 0x0002 },
       
  2414 		{ 0x0c, 0x1ec8 },
       
  2415 		{ 0x1f, 0x0000 }
       
  2416 	};
       
  2417 
       
  2418 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2419 }
       
  2420 
       
  2421 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
       
  2422 {
       
  2423 	static const struct phy_reg phy_reg_init[] = {
       
  2424 		{ 0x1f, 0x0001 },
       
  2425 		{ 0x1d, 0x3d98 },
       
  2426 		{ 0x1f, 0x0000 }
       
  2427 	};
       
  2428 
       
  2429 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2430 	rtl_patchphy(tp, 0x14, 1 << 5);
       
  2431 	rtl_patchphy(tp, 0x0d, 1 << 5);
       
  2432 
       
  2433 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2434 }
       
  2435 
       
  2436 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
       
  2437 {
       
  2438 	static const struct phy_reg phy_reg_init[] = {
       
  2439 		{ 0x1f, 0x0001 },
       
  2440 		{ 0x12, 0x2300 },
       
  2441 		{ 0x1f, 0x0002 },
       
  2442 		{ 0x00, 0x88d4 },
       
  2443 		{ 0x01, 0x82b1 },
       
  2444 		{ 0x03, 0x7002 },
       
  2445 		{ 0x08, 0x9e30 },
       
  2446 		{ 0x09, 0x01f0 },
       
  2447 		{ 0x0a, 0x5500 },
       
  2448 		{ 0x0c, 0x00c8 },
       
  2449 		{ 0x1f, 0x0003 },
       
  2450 		{ 0x12, 0xc096 },
       
  2451 		{ 0x16, 0x000a },
       
  2452 		{ 0x1f, 0x0000 },
       
  2453 		{ 0x1f, 0x0000 },
       
  2454 		{ 0x09, 0x2000 },
       
  2455 		{ 0x09, 0x0000 }
       
  2456 	};
       
  2457 
       
  2458 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2459 
       
  2460 	rtl_patchphy(tp, 0x14, 1 << 5);
       
  2461 	rtl_patchphy(tp, 0x0d, 1 << 5);
       
  2462 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2463 }
       
  2464 
       
  2465 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
       
  2466 {
       
  2467 	static const struct phy_reg phy_reg_init[] = {
       
  2468 		{ 0x1f, 0x0001 },
       
  2469 		{ 0x12, 0x2300 },
       
  2470 		{ 0x03, 0x802f },
       
  2471 		{ 0x02, 0x4f02 },
       
  2472 		{ 0x01, 0x0409 },
       
  2473 		{ 0x00, 0xf099 },
       
  2474 		{ 0x04, 0x9800 },
       
  2475 		{ 0x04, 0x9000 },
       
  2476 		{ 0x1d, 0x3d98 },
       
  2477 		{ 0x1f, 0x0002 },
       
  2478 		{ 0x0c, 0x7eb8 },
       
  2479 		{ 0x06, 0x0761 },
       
  2480 		{ 0x1f, 0x0003 },
       
  2481 		{ 0x16, 0x0f0a },
       
  2482 		{ 0x1f, 0x0000 }
       
  2483 	};
       
  2484 
       
  2485 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2486 
       
  2487 	rtl_patchphy(tp, 0x16, 1 << 0);
       
  2488 	rtl_patchphy(tp, 0x14, 1 << 5);
       
  2489 	rtl_patchphy(tp, 0x0d, 1 << 5);
       
  2490 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2491 }
       
  2492 
       
  2493 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
       
  2494 {
       
  2495 	static const struct phy_reg phy_reg_init[] = {
       
  2496 		{ 0x1f, 0x0001 },
       
  2497 		{ 0x12, 0x2300 },
       
  2498 		{ 0x1d, 0x3d98 },
       
  2499 		{ 0x1f, 0x0002 },
       
  2500 		{ 0x0c, 0x7eb8 },
       
  2501 		{ 0x06, 0x5461 },
       
  2502 		{ 0x1f, 0x0003 },
       
  2503 		{ 0x16, 0x0f0a },
       
  2504 		{ 0x1f, 0x0000 }
       
  2505 	};
       
  2506 
       
  2507 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2508 
       
  2509 	rtl_patchphy(tp, 0x16, 1 << 0);
       
  2510 	rtl_patchphy(tp, 0x14, 1 << 5);
       
  2511 	rtl_patchphy(tp, 0x0d, 1 << 5);
       
  2512 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2513 }
       
  2514 
       
  2515 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
       
  2516 {
       
  2517 	rtl8168c_3_hw_phy_config(tp);
       
  2518 }
       
  2519 
       
  2520 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
       
  2521 {
       
  2522 	static const struct phy_reg phy_reg_init_0[] = {
       
  2523 		/* Channel Estimation */
       
  2524 		{ 0x1f, 0x0001 },
       
  2525 		{ 0x06, 0x4064 },
       
  2526 		{ 0x07, 0x2863 },
       
  2527 		{ 0x08, 0x059c },
       
  2528 		{ 0x09, 0x26b4 },
       
  2529 		{ 0x0a, 0x6a19 },
       
  2530 		{ 0x0b, 0xdcc8 },
       
  2531 		{ 0x10, 0xf06d },
       
  2532 		{ 0x14, 0x7f68 },
       
  2533 		{ 0x18, 0x7fd9 },
       
  2534 		{ 0x1c, 0xf0ff },
       
  2535 		{ 0x1d, 0x3d9c },
       
  2536 		{ 0x1f, 0x0003 },
       
  2537 		{ 0x12, 0xf49f },
       
  2538 		{ 0x13, 0x070b },
       
  2539 		{ 0x1a, 0x05ad },
       
  2540 		{ 0x14, 0x94c0 },
       
  2541 
       
  2542 		/*
       
  2543 		 * Tx Error Issue
       
  2544 		 * Enhance line driver power
       
  2545 		 */
       
  2546 		{ 0x1f, 0x0002 },
       
  2547 		{ 0x06, 0x5561 },
       
  2548 		{ 0x1f, 0x0005 },
       
  2549 		{ 0x05, 0x8332 },
       
  2550 		{ 0x06, 0x5561 },
       
  2551 
       
  2552 		/*
       
  2553 		 * Can not link to 1Gbps with bad cable
       
  2554 		 * Decrease SNR threshold form 21.07dB to 19.04dB
       
  2555 		 */
       
  2556 		{ 0x1f, 0x0001 },
       
  2557 		{ 0x17, 0x0cc0 },
       
  2558 
       
  2559 		{ 0x1f, 0x0000 },
       
  2560 		{ 0x0d, 0xf880 }
       
  2561 	};
       
  2562 	void __iomem *ioaddr = tp->mmio_addr;
       
  2563 
       
  2564 	rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
       
  2565 
       
  2566 	/*
       
  2567 	 * Rx Error Issue
       
  2568 	 * Fine Tune Switching regulator parameter
       
  2569 	 */
       
  2570 	rtl_writephy(tp, 0x1f, 0x0002);
       
  2571 	rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
       
  2572 	rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
       
  2573 
       
  2574 	if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
       
  2575 		static const struct phy_reg phy_reg_init[] = {
       
  2576 			{ 0x1f, 0x0002 },
       
  2577 			{ 0x05, 0x669a },
       
  2578 			{ 0x1f, 0x0005 },
       
  2579 			{ 0x05, 0x8330 },
       
  2580 			{ 0x06, 0x669a },
       
  2581 			{ 0x1f, 0x0002 }
       
  2582 		};
       
  2583 		int val;
       
  2584 
       
  2585 		rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2586 
       
  2587 		val = rtl_readphy(tp, 0x0d);
       
  2588 
       
  2589 		if ((val & 0x00ff) != 0x006c) {
       
  2590 			static const u32 set[] = {
       
  2591 				0x0065, 0x0066, 0x0067, 0x0068,
       
  2592 				0x0069, 0x006a, 0x006b, 0x006c
       
  2593 			};
       
  2594 			int i;
       
  2595 
       
  2596 			rtl_writephy(tp, 0x1f, 0x0002);
       
  2597 
       
  2598 			val &= 0xff00;
       
  2599 			for (i = 0; i < ARRAY_SIZE(set); i++)
       
  2600 				rtl_writephy(tp, 0x0d, val | set[i]);
       
  2601 		}
       
  2602 	} else {
       
  2603 		static const struct phy_reg phy_reg_init[] = {
       
  2604 			{ 0x1f, 0x0002 },
       
  2605 			{ 0x05, 0x6662 },
       
  2606 			{ 0x1f, 0x0005 },
       
  2607 			{ 0x05, 0x8330 },
       
  2608 			{ 0x06, 0x6662 }
       
  2609 		};
       
  2610 
       
  2611 		rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2612 	}
       
  2613 
       
  2614 	/* RSET couple improve */
       
  2615 	rtl_writephy(tp, 0x1f, 0x0002);
       
  2616 	rtl_patchphy(tp, 0x0d, 0x0300);
       
  2617 	rtl_patchphy(tp, 0x0f, 0x0010);
       
  2618 
       
  2619 	/* Fine tune PLL performance */
       
  2620 	rtl_writephy(tp, 0x1f, 0x0002);
       
  2621 	rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
       
  2622 	rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
       
  2623 
       
  2624 	rtl_writephy(tp, 0x1f, 0x0005);
       
  2625 	rtl_writephy(tp, 0x05, 0x001b);
       
  2626 
       
  2627 	rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
       
  2628 
       
  2629 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2630 }
       
  2631 
       
  2632 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
       
  2633 {
       
  2634 	static const struct phy_reg phy_reg_init_0[] = {
       
  2635 		/* Channel Estimation */
       
  2636 		{ 0x1f, 0x0001 },
       
  2637 		{ 0x06, 0x4064 },
       
  2638 		{ 0x07, 0x2863 },
       
  2639 		{ 0x08, 0x059c },
       
  2640 		{ 0x09, 0x26b4 },
       
  2641 		{ 0x0a, 0x6a19 },
       
  2642 		{ 0x0b, 0xdcc8 },
       
  2643 		{ 0x10, 0xf06d },
       
  2644 		{ 0x14, 0x7f68 },
       
  2645 		{ 0x18, 0x7fd9 },
       
  2646 		{ 0x1c, 0xf0ff },
       
  2647 		{ 0x1d, 0x3d9c },
       
  2648 		{ 0x1f, 0x0003 },
       
  2649 		{ 0x12, 0xf49f },
       
  2650 		{ 0x13, 0x070b },
       
  2651 		{ 0x1a, 0x05ad },
       
  2652 		{ 0x14, 0x94c0 },
       
  2653 
       
  2654 		/*
       
  2655 		 * Tx Error Issue
       
  2656 		 * Enhance line driver power
       
  2657 		 */
       
  2658 		{ 0x1f, 0x0002 },
       
  2659 		{ 0x06, 0x5561 },
       
  2660 		{ 0x1f, 0x0005 },
       
  2661 		{ 0x05, 0x8332 },
       
  2662 		{ 0x06, 0x5561 },
       
  2663 
       
  2664 		/*
       
  2665 		 * Can not link to 1Gbps with bad cable
       
  2666 		 * Decrease SNR threshold form 21.07dB to 19.04dB
       
  2667 		 */
       
  2668 		{ 0x1f, 0x0001 },
       
  2669 		{ 0x17, 0x0cc0 },
       
  2670 
       
  2671 		{ 0x1f, 0x0000 },
       
  2672 		{ 0x0d, 0xf880 }
       
  2673 	};
       
  2674 	void __iomem *ioaddr = tp->mmio_addr;
       
  2675 
       
  2676 	rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
       
  2677 
       
  2678 	if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
       
  2679 		static const struct phy_reg phy_reg_init[] = {
       
  2680 			{ 0x1f, 0x0002 },
       
  2681 			{ 0x05, 0x669a },
       
  2682 			{ 0x1f, 0x0005 },
       
  2683 			{ 0x05, 0x8330 },
       
  2684 			{ 0x06, 0x669a },
       
  2685 
       
  2686 			{ 0x1f, 0x0002 }
       
  2687 		};
       
  2688 		int val;
       
  2689 
       
  2690 		rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2691 
       
  2692 		val = rtl_readphy(tp, 0x0d);
       
  2693 		if ((val & 0x00ff) != 0x006c) {
       
  2694 			static const u32 set[] = {
       
  2695 				0x0065, 0x0066, 0x0067, 0x0068,
       
  2696 				0x0069, 0x006a, 0x006b, 0x006c
       
  2697 			};
       
  2698 			int i;
       
  2699 
       
  2700 			rtl_writephy(tp, 0x1f, 0x0002);
       
  2701 
       
  2702 			val &= 0xff00;
       
  2703 			for (i = 0; i < ARRAY_SIZE(set); i++)
       
  2704 				rtl_writephy(tp, 0x0d, val | set[i]);
       
  2705 		}
       
  2706 	} else {
       
  2707 		static const struct phy_reg phy_reg_init[] = {
       
  2708 			{ 0x1f, 0x0002 },
       
  2709 			{ 0x05, 0x2642 },
       
  2710 			{ 0x1f, 0x0005 },
       
  2711 			{ 0x05, 0x8330 },
       
  2712 			{ 0x06, 0x2642 }
       
  2713 		};
       
  2714 
       
  2715 		rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2716 	}
       
  2717 
       
  2718 	/* Fine tune PLL performance */
       
  2719 	rtl_writephy(tp, 0x1f, 0x0002);
       
  2720 	rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
       
  2721 	rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
       
  2722 
       
  2723 	/* Switching regulator Slew rate */
       
  2724 	rtl_writephy(tp, 0x1f, 0x0002);
       
  2725 	rtl_patchphy(tp, 0x0f, 0x0017);
       
  2726 
       
  2727 	rtl_writephy(tp, 0x1f, 0x0005);
       
  2728 	rtl_writephy(tp, 0x05, 0x001b);
       
  2729 
       
  2730 	rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
       
  2731 
       
  2732 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2733 }
       
  2734 
       
  2735 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
       
  2736 {
       
  2737 	static const struct phy_reg phy_reg_init[] = {
       
  2738 		{ 0x1f, 0x0002 },
       
  2739 		{ 0x10, 0x0008 },
       
  2740 		{ 0x0d, 0x006c },
       
  2741 
       
  2742 		{ 0x1f, 0x0000 },
       
  2743 		{ 0x0d, 0xf880 },
       
  2744 
       
  2745 		{ 0x1f, 0x0001 },
       
  2746 		{ 0x17, 0x0cc0 },
       
  2747 
       
  2748 		{ 0x1f, 0x0001 },
       
  2749 		{ 0x0b, 0xa4d8 },
       
  2750 		{ 0x09, 0x281c },
       
  2751 		{ 0x07, 0x2883 },
       
  2752 		{ 0x0a, 0x6b35 },
       
  2753 		{ 0x1d, 0x3da4 },
       
  2754 		{ 0x1c, 0xeffd },
       
  2755 		{ 0x14, 0x7f52 },
       
  2756 		{ 0x18, 0x7fc6 },
       
  2757 		{ 0x08, 0x0601 },
       
  2758 		{ 0x06, 0x4063 },
       
  2759 		{ 0x10, 0xf074 },
       
  2760 		{ 0x1f, 0x0003 },
       
  2761 		{ 0x13, 0x0789 },
       
  2762 		{ 0x12, 0xf4bd },
       
  2763 		{ 0x1a, 0x04fd },
       
  2764 		{ 0x14, 0x84b0 },
       
  2765 		{ 0x1f, 0x0000 },
       
  2766 		{ 0x00, 0x9200 },
       
  2767 
       
  2768 		{ 0x1f, 0x0005 },
       
  2769 		{ 0x01, 0x0340 },
       
  2770 		{ 0x1f, 0x0001 },
       
  2771 		{ 0x04, 0x4000 },
       
  2772 		{ 0x03, 0x1d21 },
       
  2773 		{ 0x02, 0x0c32 },
       
  2774 		{ 0x01, 0x0200 },
       
  2775 		{ 0x00, 0x5554 },
       
  2776 		{ 0x04, 0x4800 },
       
  2777 		{ 0x04, 0x4000 },
       
  2778 		{ 0x04, 0xf000 },
       
  2779 		{ 0x03, 0xdf01 },
       
  2780 		{ 0x02, 0xdf20 },
       
  2781 		{ 0x01, 0x101a },
       
  2782 		{ 0x00, 0xa0ff },
       
  2783 		{ 0x04, 0xf800 },
       
  2784 		{ 0x04, 0xf000 },
       
  2785 		{ 0x1f, 0x0000 },
       
  2786 
       
  2787 		{ 0x1f, 0x0007 },
       
  2788 		{ 0x1e, 0x0023 },
       
  2789 		{ 0x16, 0x0000 },
       
  2790 		{ 0x1f, 0x0000 }
       
  2791 	};
       
  2792 
       
  2793 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2794 }
       
  2795 
       
  2796 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
       
  2797 {
       
  2798 	static const struct phy_reg phy_reg_init[] = {
       
  2799 		{ 0x1f, 0x0001 },
       
  2800 		{ 0x17, 0x0cc0 },
       
  2801 
       
  2802 		{ 0x1f, 0x0007 },
       
  2803 		{ 0x1e, 0x002d },
       
  2804 		{ 0x18, 0x0040 },
       
  2805 		{ 0x1f, 0x0000 }
       
  2806 	};
       
  2807 
       
  2808 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2809 	rtl_patchphy(tp, 0x0d, 1 << 5);
       
  2810 }
       
  2811 
       
  2812 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
       
  2813 {
       
  2814 	static const struct phy_reg phy_reg_init[] = {
       
  2815 		/* Enable Delay cap */
       
  2816 		{ 0x1f, 0x0005 },
       
  2817 		{ 0x05, 0x8b80 },
       
  2818 		{ 0x06, 0xc896 },
       
  2819 		{ 0x1f, 0x0000 },
       
  2820 
       
  2821 		/* Channel estimation fine tune */
       
  2822 		{ 0x1f, 0x0001 },
       
  2823 		{ 0x0b, 0x6c20 },
       
  2824 		{ 0x07, 0x2872 },
       
  2825 		{ 0x1c, 0xefff },
       
  2826 		{ 0x1f, 0x0003 },
       
  2827 		{ 0x14, 0x6420 },
       
  2828 		{ 0x1f, 0x0000 },
       
  2829 
       
  2830 		/* Update PFM & 10M TX idle timer */
       
  2831 		{ 0x1f, 0x0007 },
       
  2832 		{ 0x1e, 0x002f },
       
  2833 		{ 0x15, 0x1919 },
       
  2834 		{ 0x1f, 0x0000 },
       
  2835 
       
  2836 		{ 0x1f, 0x0007 },
       
  2837 		{ 0x1e, 0x00ac },
       
  2838 		{ 0x18, 0x0006 },
       
  2839 		{ 0x1f, 0x0000 }
       
  2840 	};
       
  2841 
       
  2842 	rtl_apply_firmware(tp);
       
  2843 
       
  2844 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2845 
       
  2846 	/* DCO enable for 10M IDLE Power */
       
  2847 	rtl_writephy(tp, 0x1f, 0x0007);
       
  2848 	rtl_writephy(tp, 0x1e, 0x0023);
       
  2849 	rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
       
  2850 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2851 
       
  2852 	/* For impedance matching */
       
  2853 	rtl_writephy(tp, 0x1f, 0x0002);
       
  2854 	rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
       
  2855 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2856 
       
  2857 	/* PHY auto speed down */
       
  2858 	rtl_writephy(tp, 0x1f, 0x0007);
       
  2859 	rtl_writephy(tp, 0x1e, 0x002d);
       
  2860 	rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
       
  2861 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2862 	rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
       
  2863 
       
  2864 	rtl_writephy(tp, 0x1f, 0x0005);
       
  2865 	rtl_writephy(tp, 0x05, 0x8b86);
       
  2866 	rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
       
  2867 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2868 
       
  2869 	rtl_writephy(tp, 0x1f, 0x0005);
       
  2870 	rtl_writephy(tp, 0x05, 0x8b85);
       
  2871 	rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
       
  2872 	rtl_writephy(tp, 0x1f, 0x0007);
       
  2873 	rtl_writephy(tp, 0x1e, 0x0020);
       
  2874 	rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
       
  2875 	rtl_writephy(tp, 0x1f, 0x0006);
       
  2876 	rtl_writephy(tp, 0x00, 0x5a00);
       
  2877 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2878 	rtl_writephy(tp, 0x0d, 0x0007);
       
  2879 	rtl_writephy(tp, 0x0e, 0x003c);
       
  2880 	rtl_writephy(tp, 0x0d, 0x4007);
       
  2881 	rtl_writephy(tp, 0x0e, 0x0000);
       
  2882 	rtl_writephy(tp, 0x0d, 0x0000);
       
  2883 }
       
  2884 
       
  2885 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
       
  2886 {
       
  2887 	static const struct phy_reg phy_reg_init[] = {
       
  2888 		/* Enable Delay cap */
       
  2889 		{ 0x1f, 0x0004 },
       
  2890 		{ 0x1f, 0x0007 },
       
  2891 		{ 0x1e, 0x00ac },
       
  2892 		{ 0x18, 0x0006 },
       
  2893 		{ 0x1f, 0x0002 },
       
  2894 		{ 0x1f, 0x0000 },
       
  2895 		{ 0x1f, 0x0000 },
       
  2896 
       
  2897 		/* Channel estimation fine tune */
       
  2898 		{ 0x1f, 0x0003 },
       
  2899 		{ 0x09, 0xa20f },
       
  2900 		{ 0x1f, 0x0000 },
       
  2901 		{ 0x1f, 0x0000 },
       
  2902 
       
  2903 		/* Green Setting */
       
  2904 		{ 0x1f, 0x0005 },
       
  2905 		{ 0x05, 0x8b5b },
       
  2906 		{ 0x06, 0x9222 },
       
  2907 		{ 0x05, 0x8b6d },
       
  2908 		{ 0x06, 0x8000 },
       
  2909 		{ 0x05, 0x8b76 },
       
  2910 		{ 0x06, 0x8000 },
       
  2911 		{ 0x1f, 0x0000 }
       
  2912 	};
       
  2913 
       
  2914 	rtl_apply_firmware(tp);
       
  2915 
       
  2916 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  2917 
       
  2918 	/* For 4-corner performance improve */
       
  2919 	rtl_writephy(tp, 0x1f, 0x0005);
       
  2920 	rtl_writephy(tp, 0x05, 0x8b80);
       
  2921 	rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
       
  2922 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2923 
       
  2924 	/* PHY auto speed down */
       
  2925 	rtl_writephy(tp, 0x1f, 0x0004);
       
  2926 	rtl_writephy(tp, 0x1f, 0x0007);
       
  2927 	rtl_writephy(tp, 0x1e, 0x002d);
       
  2928 	rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
       
  2929 	rtl_writephy(tp, 0x1f, 0x0002);
       
  2930 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2931 	rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
       
  2932 
       
  2933 	/* improve 10M EEE waveform */
       
  2934 	rtl_writephy(tp, 0x1f, 0x0005);
       
  2935 	rtl_writephy(tp, 0x05, 0x8b86);
       
  2936 	rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
       
  2937 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2938 
       
  2939 	/* Improve 2-pair detection performance */
       
  2940 	rtl_writephy(tp, 0x1f, 0x0005);
       
  2941 	rtl_writephy(tp, 0x05, 0x8b85);
       
  2942 	rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
       
  2943 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2944 
       
  2945 	/* EEE setting */
       
  2946 	rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
       
  2947 		     ERIAR_EXGMAC);
       
  2948 	rtl_writephy(tp, 0x1f, 0x0005);
       
  2949 	rtl_writephy(tp, 0x05, 0x8b85);
       
  2950 	rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
       
  2951 	rtl_writephy(tp, 0x1f, 0x0004);
       
  2952 	rtl_writephy(tp, 0x1f, 0x0007);
       
  2953 	rtl_writephy(tp, 0x1e, 0x0020);
       
  2954 	rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
       
  2955 	rtl_writephy(tp, 0x1f, 0x0002);
       
  2956 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2957 	rtl_writephy(tp, 0x0d, 0x0007);
       
  2958 	rtl_writephy(tp, 0x0e, 0x003c);
       
  2959 	rtl_writephy(tp, 0x0d, 0x4007);
       
  2960 	rtl_writephy(tp, 0x0e, 0x0000);
       
  2961 	rtl_writephy(tp, 0x0d, 0x0000);
       
  2962 
       
  2963 	/* Green feature */
       
  2964 	rtl_writephy(tp, 0x1f, 0x0003);
       
  2965 	rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
       
  2966 	rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
       
  2967 	rtl_writephy(tp, 0x1f, 0x0000);
       
  2968 }
       
  2969 
       
  2970 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
       
  2971 {
       
  2972 	static const struct phy_reg phy_reg_init[] = {
       
  2973 		/* Channel estimation fine tune */
       
  2974 		{ 0x1f, 0x0003 },
       
  2975 		{ 0x09, 0xa20f },
       
  2976 		{ 0x1f, 0x0000 },
       
  2977 
       
  2978 		/* Modify green table for giga & fnet */
       
  2979 		{ 0x1f, 0x0005 },
       
  2980 		{ 0x05, 0x8b55 },
       
  2981 		{ 0x06, 0x0000 },
       
  2982 		{ 0x05, 0x8b5e },
       
  2983 		{ 0x06, 0x0000 },
       
  2984 		{ 0x05, 0x8b67 },
       
  2985 		{ 0x06, 0x0000 },
       
  2986 		{ 0x05, 0x8b70 },
       
  2987 		{ 0x06, 0x0000 },
       
  2988 		{ 0x1f, 0x0000 },
       
  2989 		{ 0x1f, 0x0007 },
       
  2990 		{ 0x1e, 0x0078 },
       
  2991 		{ 0x17, 0x0000 },
       
  2992 		{ 0x19, 0x00fb },
       
  2993 		{ 0x1f, 0x0000 },
       
  2994 
       
  2995 		/* Modify green table for 10M */
       
  2996 		{ 0x1f, 0x0005 },
       
  2997 		{ 0x05, 0x8b79 },
       
  2998 		{ 0x06, 0xaa00 },
       
  2999 		{ 0x1f, 0x0000 },
       
  3000 
       
  3001 		/* Disable hiimpedance detection (RTCT) */
       
  3002 		{ 0x1f, 0x0003 },
       
  3003 		{ 0x01, 0x328a },
       
  3004 		{ 0x1f, 0x0000 }
       
  3005 	};
       
  3006 
       
  3007 	rtl_apply_firmware(tp);
       
  3008 
       
  3009 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  3010 
       
  3011 	/* For 4-corner performance improve */
       
  3012 	rtl_writephy(tp, 0x1f, 0x0005);
       
  3013 	rtl_writephy(tp, 0x05, 0x8b80);
       
  3014 	rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
       
  3015 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3016 
       
  3017 	/* PHY auto speed down */
       
  3018 	rtl_writephy(tp, 0x1f, 0x0007);
       
  3019 	rtl_writephy(tp, 0x1e, 0x002d);
       
  3020 	rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
       
  3021 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3022 	rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
       
  3023 
       
  3024 	/* Improve 10M EEE waveform */
       
  3025 	rtl_writephy(tp, 0x1f, 0x0005);
       
  3026 	rtl_writephy(tp, 0x05, 0x8b86);
       
  3027 	rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
       
  3028 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3029 
       
  3030 	/* Improve 2-pair detection performance */
       
  3031 	rtl_writephy(tp, 0x1f, 0x0005);
       
  3032 	rtl_writephy(tp, 0x05, 0x8b85);
       
  3033 	rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
       
  3034 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3035 }
       
  3036 
       
  3037 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
       
  3038 {
       
  3039 	rtl_apply_firmware(tp);
       
  3040 
       
  3041 	/* For 4-corner performance improve */
       
  3042 	rtl_writephy(tp, 0x1f, 0x0005);
       
  3043 	rtl_writephy(tp, 0x05, 0x8b80);
       
  3044 	rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
       
  3045 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3046 
       
  3047 	/* PHY auto speed down */
       
  3048 	rtl_writephy(tp, 0x1f, 0x0007);
       
  3049 	rtl_writephy(tp, 0x1e, 0x002d);
       
  3050 	rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
       
  3051 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3052 	rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
       
  3053 
       
  3054 	/* Improve 10M EEE waveform */
       
  3055 	rtl_writephy(tp, 0x1f, 0x0005);
       
  3056 	rtl_writephy(tp, 0x05, 0x8b86);
       
  3057 	rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
       
  3058 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3059 }
       
  3060 
       
  3061 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
       
  3062 {
       
  3063 	static const struct phy_reg phy_reg_init[] = {
       
  3064 		{ 0x1f, 0x0003 },
       
  3065 		{ 0x08, 0x441d },
       
  3066 		{ 0x01, 0x9100 },
       
  3067 		{ 0x1f, 0x0000 }
       
  3068 	};
       
  3069 
       
  3070 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3071 	rtl_patchphy(tp, 0x11, 1 << 12);
       
  3072 	rtl_patchphy(tp, 0x19, 1 << 13);
       
  3073 	rtl_patchphy(tp, 0x10, 1 << 15);
       
  3074 
       
  3075 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  3076 }
       
  3077 
       
  3078 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
       
  3079 {
       
  3080 	static const struct phy_reg phy_reg_init[] = {
       
  3081 		{ 0x1f, 0x0005 },
       
  3082 		{ 0x1a, 0x0000 },
       
  3083 		{ 0x1f, 0x0000 },
       
  3084 
       
  3085 		{ 0x1f, 0x0004 },
       
  3086 		{ 0x1c, 0x0000 },
       
  3087 		{ 0x1f, 0x0000 },
       
  3088 
       
  3089 		{ 0x1f, 0x0001 },
       
  3090 		{ 0x15, 0x7701 },
       
  3091 		{ 0x1f, 0x0000 }
       
  3092 	};
       
  3093 
       
  3094 	/* Disable ALDPS before ram code */
       
  3095 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3096 	rtl_writephy(tp, 0x18, 0x0310);
       
  3097 	msleep(100);
       
  3098 
       
  3099 	rtl_apply_firmware(tp);
       
  3100 
       
  3101 	rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
       
  3102 }
       
  3103 
       
  3104 static void rtl_hw_phy_config(struct net_device *dev)
       
  3105 {
       
  3106 	struct rtl8169_private *tp = netdev_priv(dev);
       
  3107 
       
  3108 	rtl8169_print_mac_version(tp);
       
  3109 
       
  3110 	switch (tp->mac_version) {
       
  3111 	case RTL_GIGA_MAC_VER_01:
       
  3112 		break;
       
  3113 	case RTL_GIGA_MAC_VER_02:
       
  3114 	case RTL_GIGA_MAC_VER_03:
       
  3115 		rtl8169s_hw_phy_config(tp);
       
  3116 		break;
       
  3117 	case RTL_GIGA_MAC_VER_04:
       
  3118 		rtl8169sb_hw_phy_config(tp);
       
  3119 		break;
       
  3120 	case RTL_GIGA_MAC_VER_05:
       
  3121 		rtl8169scd_hw_phy_config(tp);
       
  3122 		break;
       
  3123 	case RTL_GIGA_MAC_VER_06:
       
  3124 		rtl8169sce_hw_phy_config(tp);
       
  3125 		break;
       
  3126 	case RTL_GIGA_MAC_VER_07:
       
  3127 	case RTL_GIGA_MAC_VER_08:
       
  3128 	case RTL_GIGA_MAC_VER_09:
       
  3129 		rtl8102e_hw_phy_config(tp);
       
  3130 		break;
       
  3131 	case RTL_GIGA_MAC_VER_11:
       
  3132 		rtl8168bb_hw_phy_config(tp);
       
  3133 		break;
       
  3134 	case RTL_GIGA_MAC_VER_12:
       
  3135 		rtl8168bef_hw_phy_config(tp);
       
  3136 		break;
       
  3137 	case RTL_GIGA_MAC_VER_17:
       
  3138 		rtl8168bef_hw_phy_config(tp);
       
  3139 		break;
       
  3140 	case RTL_GIGA_MAC_VER_18:
       
  3141 		rtl8168cp_1_hw_phy_config(tp);
       
  3142 		break;
       
  3143 	case RTL_GIGA_MAC_VER_19:
       
  3144 		rtl8168c_1_hw_phy_config(tp);
       
  3145 		break;
       
  3146 	case RTL_GIGA_MAC_VER_20:
       
  3147 		rtl8168c_2_hw_phy_config(tp);
       
  3148 		break;
       
  3149 	case RTL_GIGA_MAC_VER_21:
       
  3150 		rtl8168c_3_hw_phy_config(tp);
       
  3151 		break;
       
  3152 	case RTL_GIGA_MAC_VER_22:
       
  3153 		rtl8168c_4_hw_phy_config(tp);
       
  3154 		break;
       
  3155 	case RTL_GIGA_MAC_VER_23:
       
  3156 	case RTL_GIGA_MAC_VER_24:
       
  3157 		rtl8168cp_2_hw_phy_config(tp);
       
  3158 		break;
       
  3159 	case RTL_GIGA_MAC_VER_25:
       
  3160 		rtl8168d_1_hw_phy_config(tp);
       
  3161 		break;
       
  3162 	case RTL_GIGA_MAC_VER_26:
       
  3163 		rtl8168d_2_hw_phy_config(tp);
       
  3164 		break;
       
  3165 	case RTL_GIGA_MAC_VER_27:
       
  3166 		rtl8168d_3_hw_phy_config(tp);
       
  3167 		break;
       
  3168 	case RTL_GIGA_MAC_VER_28:
       
  3169 		rtl8168d_4_hw_phy_config(tp);
       
  3170 		break;
       
  3171 	case RTL_GIGA_MAC_VER_29:
       
  3172 	case RTL_GIGA_MAC_VER_30:
       
  3173 		rtl8105e_hw_phy_config(tp);
       
  3174 		break;
       
  3175 	case RTL_GIGA_MAC_VER_31:
       
  3176 		/* None. */
       
  3177 		break;
       
  3178 	case RTL_GIGA_MAC_VER_32:
       
  3179 	case RTL_GIGA_MAC_VER_33:
       
  3180 		rtl8168e_1_hw_phy_config(tp);
       
  3181 		break;
       
  3182 	case RTL_GIGA_MAC_VER_34:
       
  3183 		rtl8168e_2_hw_phy_config(tp);
       
  3184 		break;
       
  3185 	case RTL_GIGA_MAC_VER_35:
       
  3186 		rtl8168f_1_hw_phy_config(tp);
       
  3187 		break;
       
  3188 	case RTL_GIGA_MAC_VER_36:
       
  3189 		rtl8168f_2_hw_phy_config(tp);
       
  3190 		break;
       
  3191 
       
  3192 	default:
       
  3193 		break;
       
  3194 	}
       
  3195 }
       
  3196 
       
  3197 static void rtl8169_phy_timer(unsigned long __opaque)
       
  3198 {
       
  3199 	struct net_device *dev = (struct net_device *)__opaque;
       
  3200 	struct rtl8169_private *tp = netdev_priv(dev);
       
  3201 	struct timer_list *timer = &tp->timer;
       
  3202 	void __iomem *ioaddr = tp->mmio_addr;
       
  3203 	unsigned long timeout = RTL8169_PHY_TIMEOUT;
       
  3204 
       
  3205 	assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
       
  3206 
       
  3207 	if (!tp->ecdev)
       
  3208 		spin_lock_irq(&tp->lock);
       
  3209 
       
  3210 	if (tp->phy_reset_pending(tp)) {
       
  3211 		/*
       
  3212 		 * A busy loop could burn quite a few cycles on nowadays CPU.
       
  3213 		 * Let's delay the execution of the timer for a few ticks.
       
  3214 		 */
       
  3215 		timeout = HZ/10;
       
  3216 		goto out_mod_timer;
       
  3217 	}
       
  3218 
       
  3219 	if (tp->link_ok(ioaddr))
       
  3220 		goto out_unlock;
       
  3221 
       
  3222 	netif_warn(tp, link, dev, "PHY reset until link up\n");
       
  3223 
       
  3224 	tp->phy_reset_enable(tp);
       
  3225 
       
  3226 out_mod_timer:
       
  3227 	if (!tp->ecdev)
       
  3228 		mod_timer(timer, jiffies + timeout);
       
  3229 out_unlock:
       
  3230 	if (!tp->ecdev)
       
  3231 		spin_unlock_irq(&tp->lock);
       
  3232 }
       
  3233 
       
  3234 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  3235 /*
       
  3236  * Polling 'interrupt' - used by things like netconsole to send skbs
       
  3237  * without having to re-enable interrupts. It's not called while
       
  3238  * the interrupt routine is executing.
       
  3239  */
       
  3240 static void rtl8169_netpoll(struct net_device *dev)
       
  3241 {
       
  3242 	struct rtl8169_private *tp = netdev_priv(dev);
       
  3243 	struct pci_dev *pdev = tp->pci_dev;
       
  3244 
       
  3245 	disable_irq(pdev->irq);
       
  3246 	rtl8169_interrupt(pdev->irq, dev);
       
  3247 	enable_irq(pdev->irq);
       
  3248 }
       
  3249 #endif
       
  3250 
       
  3251 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
       
  3252 				  void __iomem *ioaddr)
       
  3253 {
       
  3254 	iounmap(ioaddr);
       
  3255 	pci_release_regions(pdev);
       
  3256 	pci_clear_mwi(pdev);
       
  3257 	pci_disable_device(pdev);
       
  3258 	free_netdev(dev);
       
  3259 }
       
  3260 
       
  3261 static void rtl8169_phy_reset(struct net_device *dev,
       
  3262 			      struct rtl8169_private *tp)
       
  3263 {
       
  3264 	unsigned int i;
       
  3265 
       
  3266 	tp->phy_reset_enable(tp);
       
  3267 	for (i = 0; i < 100; i++) {
       
  3268 		if (!tp->phy_reset_pending(tp))
       
  3269 			return;
       
  3270 		msleep(1);
       
  3271 	}
       
  3272 	netif_err(tp, link, dev, "PHY reset failed\n");
       
  3273 }
       
  3274 
       
  3275 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
       
  3276 {
       
  3277 	void __iomem *ioaddr = tp->mmio_addr;
       
  3278 
       
  3279 	return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
       
  3280 	    (RTL_R8(PHYstatus) & TBI_Enable);
       
  3281 }
       
  3282 
       
  3283 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
       
  3284 {
       
  3285 	void __iomem *ioaddr = tp->mmio_addr;
       
  3286 
       
  3287 	rtl_hw_phy_config(dev);
       
  3288 
       
  3289 	if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
       
  3290 		dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
       
  3291 		RTL_W8(0x82, 0x01);
       
  3292 	}
       
  3293 
       
  3294 	pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
       
  3295 
       
  3296 	if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
       
  3297 		pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
       
  3298 
       
  3299 	if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
       
  3300 		dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
       
  3301 		RTL_W8(0x82, 0x01);
       
  3302 		dprintk("Set PHY Reg 0x0bh = 0x00h\n");
       
  3303 		rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
       
  3304 	}
       
  3305 
       
  3306 	rtl8169_phy_reset(dev, tp);
       
  3307 
       
  3308 	rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
       
  3309 			  ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
       
  3310 			  ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
       
  3311 			  (tp->mii.supports_gmii ?
       
  3312 			   ADVERTISED_1000baseT_Half |
       
  3313 			   ADVERTISED_1000baseT_Full : 0));
       
  3314 
       
  3315 	if (rtl_tbi_enabled(tp))
       
  3316 		netif_info(tp, link, dev, "TBI auto-negotiating\n");
       
  3317 }
       
  3318 
       
  3319 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
       
  3320 {
       
  3321 	void __iomem *ioaddr = tp->mmio_addr;
       
  3322 	u32 high;
       
  3323 	u32 low;
       
  3324 
       
  3325 	low  = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
       
  3326 	high = addr[4] | (addr[5] << 8);
       
  3327 
       
  3328 	spin_lock_irq(&tp->lock);
       
  3329 
       
  3330 	RTL_W8(Cfg9346, Cfg9346_Unlock);
       
  3331 
       
  3332 	RTL_W32(MAC4, high);
       
  3333 	RTL_R32(MAC4);
       
  3334 
       
  3335 	RTL_W32(MAC0, low);
       
  3336 	RTL_R32(MAC0);
       
  3337 
       
  3338 	if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
       
  3339 		const struct exgmac_reg e[] = {
       
  3340 			{ .addr = 0xe0, ERIAR_MASK_1111, .val = low },
       
  3341 			{ .addr = 0xe4, ERIAR_MASK_1111, .val = high },
       
  3342 			{ .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
       
  3343 			{ .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
       
  3344 								low  >> 16 },
       
  3345 		};
       
  3346 
       
  3347 		rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
       
  3348 	}
       
  3349 
       
  3350 	RTL_W8(Cfg9346, Cfg9346_Lock);
       
  3351 
       
  3352 	spin_unlock_irq(&tp->lock);
       
  3353 }
       
  3354 
       
  3355 static int rtl_set_mac_address(struct net_device *dev, void *p)
       
  3356 {
       
  3357 	struct rtl8169_private *tp = netdev_priv(dev);
       
  3358 	struct sockaddr *addr = p;
       
  3359 
       
  3360 	if (!is_valid_ether_addr(addr->sa_data))
       
  3361 		return -EADDRNOTAVAIL;
       
  3362 
       
  3363 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
       
  3364 
       
  3365 	rtl_rar_set(tp, dev->dev_addr);
       
  3366 
       
  3367 	return 0;
       
  3368 }
       
  3369 
       
  3370 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
       
  3371 {
       
  3372 	struct rtl8169_private *tp = netdev_priv(dev);
       
  3373 	struct mii_ioctl_data *data = if_mii(ifr);
       
  3374 
       
  3375 	return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
       
  3376 }
       
  3377 
       
  3378 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
       
  3379 			  struct mii_ioctl_data *data, int cmd)
       
  3380 {
       
  3381 	switch (cmd) {
       
  3382 	case SIOCGMIIPHY:
       
  3383 		data->phy_id = 32; /* Internal PHY */
       
  3384 		return 0;
       
  3385 
       
  3386 	case SIOCGMIIREG:
       
  3387 		data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
       
  3388 		return 0;
       
  3389 
       
  3390 	case SIOCSMIIREG:
       
  3391 		rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
       
  3392 		return 0;
       
  3393 	}
       
  3394 	return -EOPNOTSUPP;
       
  3395 }
       
  3396 
       
  3397 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
       
  3398 {
       
  3399 	return -EOPNOTSUPP;
       
  3400 }
       
  3401 
       
  3402 static const struct rtl_cfg_info {
       
  3403 	void (*hw_start)(struct net_device *);
       
  3404 	unsigned int region;
       
  3405 	unsigned int align;
       
  3406 	u16 intr_event;
       
  3407 	u16 napi_event;
       
  3408 	unsigned features;
       
  3409 	u8 default_ver;
       
  3410 } rtl_cfg_infos [] = {
       
  3411 	[RTL_CFG_0] = {
       
  3412 		.hw_start	= rtl_hw_start_8169,
       
  3413 		.region		= 1,
       
  3414 		.align		= 0,
       
  3415 		.intr_event	= SYSErr | LinkChg | RxOverflow |
       
  3416 				  RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
       
  3417 		.napi_event	= RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
       
  3418 		.features	= RTL_FEATURE_GMII,
       
  3419 		.default_ver	= RTL_GIGA_MAC_VER_01,
       
  3420 	},
       
  3421 	[RTL_CFG_1] = {
       
  3422 		.hw_start	= rtl_hw_start_8168,
       
  3423 		.region		= 2,
       
  3424 		.align		= 8,
       
  3425 		.intr_event	= SYSErr | LinkChg | RxOverflow |
       
  3426 				  TxErr | TxOK | RxOK | RxErr,
       
  3427 		.napi_event	= TxErr | TxOK | RxOK | RxOverflow,
       
  3428 		.features	= RTL_FEATURE_GMII | RTL_FEATURE_MSI,
       
  3429 		.default_ver	= RTL_GIGA_MAC_VER_11,
       
  3430 	},
       
  3431 	[RTL_CFG_2] = {
       
  3432 		.hw_start	= rtl_hw_start_8101,
       
  3433 		.region		= 2,
       
  3434 		.align		= 8,
       
  3435 		.intr_event	= SYSErr | LinkChg | RxOverflow | PCSTimeout |
       
  3436 				  RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
       
  3437 		.napi_event	= RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
       
  3438 		.features	= RTL_FEATURE_MSI,
       
  3439 		.default_ver	= RTL_GIGA_MAC_VER_13,
       
  3440 	}
       
  3441 };
       
  3442 
       
  3443 /* Cfg9346_Unlock assumed. */
       
  3444 static unsigned rtl_try_msi(struct rtl8169_private *tp,
       
  3445 			    const struct rtl_cfg_info *cfg)
       
  3446 {
       
  3447 	void __iomem *ioaddr = tp->mmio_addr;
       
  3448 	unsigned msi = 0;
       
  3449 	u8 cfg2;
       
  3450 
       
  3451 	cfg2 = RTL_R8(Config2) & ~MSIEnable;
       
  3452 	if (cfg->features & RTL_FEATURE_MSI) {
       
  3453 		if (pci_enable_msi(tp->pci_dev)) {
       
  3454 			netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
       
  3455 		} else {
       
  3456 			cfg2 |= MSIEnable;
       
  3457 			msi = RTL_FEATURE_MSI;
       
  3458 		}
       
  3459 	}
       
  3460 	if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
       
  3461 		RTL_W8(Config2, cfg2);
       
  3462 	return msi;
       
  3463 }
       
  3464 
       
  3465 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
       
  3466 {
       
  3467 	if (tp->features & RTL_FEATURE_MSI) {
       
  3468 		pci_disable_msi(pdev);
       
  3469 		tp->features &= ~RTL_FEATURE_MSI;
       
  3470 	}
       
  3471 }
       
  3472 
       
  3473 static const struct net_device_ops rtl8169_netdev_ops = {
       
  3474 	.ndo_open		= rtl8169_open,
       
  3475 	.ndo_stop		= rtl8169_close,
       
  3476 	.ndo_get_stats		= rtl8169_get_stats,
       
  3477 	.ndo_start_xmit		= rtl8169_start_xmit,
       
  3478 	.ndo_tx_timeout		= rtl8169_tx_timeout,
       
  3479 	.ndo_validate_addr	= eth_validate_addr,
       
  3480 	.ndo_change_mtu		= rtl8169_change_mtu,
       
  3481 	.ndo_fix_features	= rtl8169_fix_features,
       
  3482 	.ndo_set_features	= rtl8169_set_features,
       
  3483 	.ndo_set_mac_address	= rtl_set_mac_address,
       
  3484 	.ndo_do_ioctl		= rtl8169_ioctl,
       
  3485 	.ndo_set_rx_mode	= rtl_set_rx_mode,
       
  3486 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  3487 	.ndo_poll_controller	= rtl8169_netpoll,
       
  3488 #endif
       
  3489 
       
  3490 };
       
  3491 
       
  3492 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
       
  3493 {
       
  3494 	struct mdio_ops *ops = &tp->mdio_ops;
       
  3495 
       
  3496 	switch (tp->mac_version) {
       
  3497 	case RTL_GIGA_MAC_VER_27:
       
  3498 		ops->write	= r8168dp_1_mdio_write;
       
  3499 		ops->read	= r8168dp_1_mdio_read;
       
  3500 		break;
       
  3501 	case RTL_GIGA_MAC_VER_28:
       
  3502 	case RTL_GIGA_MAC_VER_31:
       
  3503 		ops->write	= r8168dp_2_mdio_write;
       
  3504 		ops->read	= r8168dp_2_mdio_read;
       
  3505 		break;
       
  3506 	default:
       
  3507 		ops->write	= r8169_mdio_write;
       
  3508 		ops->read	= r8169_mdio_read;
       
  3509 		break;
       
  3510 	}
       
  3511 }
       
  3512 
       
  3513 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
       
  3514 {
       
  3515 	void __iomem *ioaddr = tp->mmio_addr;
       
  3516 
       
  3517 	switch (tp->mac_version) {
       
  3518 	case RTL_GIGA_MAC_VER_29:
       
  3519 	case RTL_GIGA_MAC_VER_30:
       
  3520 	case RTL_GIGA_MAC_VER_32:
       
  3521 	case RTL_GIGA_MAC_VER_33:
       
  3522 	case RTL_GIGA_MAC_VER_34:
       
  3523 		RTL_W32(RxConfig, RTL_R32(RxConfig) |
       
  3524 			AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
       
  3525 		break;
       
  3526 	default:
       
  3527 		break;
       
  3528 	}
       
  3529 }
       
  3530 
       
  3531 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
       
  3532 {
       
  3533 	if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
       
  3534 		return false;
       
  3535 
       
  3536 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3537 	rtl_writephy(tp, MII_BMCR, 0x0000);
       
  3538 
       
  3539 	rtl_wol_suspend_quirk(tp);
       
  3540 
       
  3541 	return true;
       
  3542 }
       
  3543 
       
  3544 static void r810x_phy_power_down(struct rtl8169_private *tp)
       
  3545 {
       
  3546 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3547 	rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
       
  3548 }
       
  3549 
       
  3550 static void r810x_phy_power_up(struct rtl8169_private *tp)
       
  3551 {
       
  3552 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3553 	rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
       
  3554 }
       
  3555 
       
  3556 static void r810x_pll_power_down(struct rtl8169_private *tp)
       
  3557 {
       
  3558 	if (rtl_wol_pll_power_down(tp))
       
  3559 		return;
       
  3560 
       
  3561 	r810x_phy_power_down(tp);
       
  3562 }
       
  3563 
       
  3564 static void r810x_pll_power_up(struct rtl8169_private *tp)
       
  3565 {
       
  3566 	r810x_phy_power_up(tp);
       
  3567 }
       
  3568 
       
  3569 static void r8168_phy_power_up(struct rtl8169_private *tp)
       
  3570 {
       
  3571 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3572 	switch (tp->mac_version) {
       
  3573 	case RTL_GIGA_MAC_VER_11:
       
  3574 	case RTL_GIGA_MAC_VER_12:
       
  3575 	case RTL_GIGA_MAC_VER_17:
       
  3576 	case RTL_GIGA_MAC_VER_18:
       
  3577 	case RTL_GIGA_MAC_VER_19:
       
  3578 	case RTL_GIGA_MAC_VER_20:
       
  3579 	case RTL_GIGA_MAC_VER_21:
       
  3580 	case RTL_GIGA_MAC_VER_22:
       
  3581 	case RTL_GIGA_MAC_VER_23:
       
  3582 	case RTL_GIGA_MAC_VER_24:
       
  3583 	case RTL_GIGA_MAC_VER_25:
       
  3584 	case RTL_GIGA_MAC_VER_26:
       
  3585 	case RTL_GIGA_MAC_VER_27:
       
  3586 	case RTL_GIGA_MAC_VER_28:
       
  3587 	case RTL_GIGA_MAC_VER_31:
       
  3588 		rtl_writephy(tp, 0x0e, 0x0000);
       
  3589 		break;
       
  3590 	default:
       
  3591 		break;
       
  3592 	}
       
  3593 	rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
       
  3594 }
       
  3595 
       
  3596 static void r8168_phy_power_down(struct rtl8169_private *tp)
       
  3597 {
       
  3598 	rtl_writephy(tp, 0x1f, 0x0000);
       
  3599 	switch (tp->mac_version) {
       
  3600 	case RTL_GIGA_MAC_VER_32:
       
  3601 	case RTL_GIGA_MAC_VER_33:
       
  3602 		rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
       
  3603 		break;
       
  3604 
       
  3605 	case RTL_GIGA_MAC_VER_11:
       
  3606 	case RTL_GIGA_MAC_VER_12:
       
  3607 	case RTL_GIGA_MAC_VER_17:
       
  3608 	case RTL_GIGA_MAC_VER_18:
       
  3609 	case RTL_GIGA_MAC_VER_19:
       
  3610 	case RTL_GIGA_MAC_VER_20:
       
  3611 	case RTL_GIGA_MAC_VER_21:
       
  3612 	case RTL_GIGA_MAC_VER_22:
       
  3613 	case RTL_GIGA_MAC_VER_23:
       
  3614 	case RTL_GIGA_MAC_VER_24:
       
  3615 	case RTL_GIGA_MAC_VER_25:
       
  3616 	case RTL_GIGA_MAC_VER_26:
       
  3617 	case RTL_GIGA_MAC_VER_27:
       
  3618 	case RTL_GIGA_MAC_VER_28:
       
  3619 	case RTL_GIGA_MAC_VER_31:
       
  3620 		rtl_writephy(tp, 0x0e, 0x0200);
       
  3621 	default:
       
  3622 		rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
       
  3623 		break;
       
  3624 	}
       
  3625 }
       
  3626 
       
  3627 static void r8168_pll_power_down(struct rtl8169_private *tp)
       
  3628 {
       
  3629 	void __iomem *ioaddr = tp->mmio_addr;
       
  3630 
       
  3631 	if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
       
  3632 	     tp->mac_version == RTL_GIGA_MAC_VER_28 ||
       
  3633 	     tp->mac_version == RTL_GIGA_MAC_VER_31) &&
       
  3634 	    r8168dp_check_dash(tp)) {
       
  3635 		return;
       
  3636 	}
       
  3637 
       
  3638 	if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
       
  3639 	     tp->mac_version == RTL_GIGA_MAC_VER_24) &&
       
  3640 	    (RTL_R16(CPlusCmd) & ASF)) {
       
  3641 		return;
       
  3642 	}
       
  3643 
       
  3644 	if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
       
  3645 	    tp->mac_version == RTL_GIGA_MAC_VER_33)
       
  3646 		rtl_ephy_write(ioaddr, 0x19, 0xff64);
       
  3647 
       
  3648 	if (rtl_wol_pll_power_down(tp))
       
  3649 		return;
       
  3650 
       
  3651 	r8168_phy_power_down(tp);
       
  3652 
       
  3653 	switch (tp->mac_version) {
       
  3654 	case RTL_GIGA_MAC_VER_25:
       
  3655 	case RTL_GIGA_MAC_VER_26:
       
  3656 	case RTL_GIGA_MAC_VER_27:
       
  3657 	case RTL_GIGA_MAC_VER_28:
       
  3658 	case RTL_GIGA_MAC_VER_31:
       
  3659 	case RTL_GIGA_MAC_VER_32:
       
  3660 	case RTL_GIGA_MAC_VER_33:
       
  3661 		RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
       
  3662 		break;
       
  3663 	}
       
  3664 }
       
  3665 
       
  3666 static void r8168_pll_power_up(struct rtl8169_private *tp)
       
  3667 {
       
  3668 	void __iomem *ioaddr = tp->mmio_addr;
       
  3669 
       
  3670 	if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
       
  3671 	     tp->mac_version == RTL_GIGA_MAC_VER_28 ||
       
  3672 	     tp->mac_version == RTL_GIGA_MAC_VER_31) &&
       
  3673 	    r8168dp_check_dash(tp)) {
       
  3674 		return;
       
  3675 	}
       
  3676 
       
  3677 	switch (tp->mac_version) {
       
  3678 	case RTL_GIGA_MAC_VER_25:
       
  3679 	case RTL_GIGA_MAC_VER_26:
       
  3680 	case RTL_GIGA_MAC_VER_27:
       
  3681 	case RTL_GIGA_MAC_VER_28:
       
  3682 	case RTL_GIGA_MAC_VER_31:
       
  3683 	case RTL_GIGA_MAC_VER_32:
       
  3684 	case RTL_GIGA_MAC_VER_33:
       
  3685 		RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
       
  3686 		break;
       
  3687 	}
       
  3688 
       
  3689 	r8168_phy_power_up(tp);
       
  3690 }
       
  3691 
       
  3692 static void rtl_generic_op(struct rtl8169_private *tp,
       
  3693 			   void (*op)(struct rtl8169_private *))
       
  3694 {
       
  3695 	if (op)
       
  3696 		op(tp);
       
  3697 }
       
  3698 
       
  3699 static void rtl_pll_power_down(struct rtl8169_private *tp)
       
  3700 {
       
  3701 	rtl_generic_op(tp, tp->pll_power_ops.down);
       
  3702 }
       
  3703 
       
  3704 static void rtl_pll_power_up(struct rtl8169_private *tp)
       
  3705 {
       
  3706 	rtl_generic_op(tp, tp->pll_power_ops.up);
       
  3707 }
       
  3708 
       
  3709 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
       
  3710 {
       
  3711 	struct pll_power_ops *ops = &tp->pll_power_ops;
       
  3712 
       
  3713 	switch (tp->mac_version) {
       
  3714 	case RTL_GIGA_MAC_VER_07:
       
  3715 	case RTL_GIGA_MAC_VER_08:
       
  3716 	case RTL_GIGA_MAC_VER_09:
       
  3717 	case RTL_GIGA_MAC_VER_10:
       
  3718 	case RTL_GIGA_MAC_VER_16:
       
  3719 	case RTL_GIGA_MAC_VER_29:
       
  3720 	case RTL_GIGA_MAC_VER_30:
       
  3721 		ops->down	= r810x_pll_power_down;
       
  3722 		ops->up		= r810x_pll_power_up;
       
  3723 		break;
       
  3724 
       
  3725 	case RTL_GIGA_MAC_VER_11:
       
  3726 	case RTL_GIGA_MAC_VER_12:
       
  3727 	case RTL_GIGA_MAC_VER_17:
       
  3728 	case RTL_GIGA_MAC_VER_18:
       
  3729 	case RTL_GIGA_MAC_VER_19:
       
  3730 	case RTL_GIGA_MAC_VER_20:
       
  3731 	case RTL_GIGA_MAC_VER_21:
       
  3732 	case RTL_GIGA_MAC_VER_22:
       
  3733 	case RTL_GIGA_MAC_VER_23:
       
  3734 	case RTL_GIGA_MAC_VER_24:
       
  3735 	case RTL_GIGA_MAC_VER_25:
       
  3736 	case RTL_GIGA_MAC_VER_26:
       
  3737 	case RTL_GIGA_MAC_VER_27:
       
  3738 	case RTL_GIGA_MAC_VER_28:
       
  3739 	case RTL_GIGA_MAC_VER_31:
       
  3740 	case RTL_GIGA_MAC_VER_32:
       
  3741 	case RTL_GIGA_MAC_VER_33:
       
  3742 	case RTL_GIGA_MAC_VER_34:
       
  3743 	case RTL_GIGA_MAC_VER_35:
       
  3744 	case RTL_GIGA_MAC_VER_36:
       
  3745 		ops->down	= r8168_pll_power_down;
       
  3746 		ops->up		= r8168_pll_power_up;
       
  3747 		break;
       
  3748 
       
  3749 	default:
       
  3750 		ops->down	= NULL;
       
  3751 		ops->up		= NULL;
       
  3752 		break;
       
  3753 	}
       
  3754 }
       
  3755 
       
  3756 static void rtl_init_rxcfg(struct rtl8169_private *tp)
       
  3757 {
       
  3758 	void __iomem *ioaddr = tp->mmio_addr;
       
  3759 
       
  3760 	switch (tp->mac_version) {
       
  3761 	case RTL_GIGA_MAC_VER_01:
       
  3762 	case RTL_GIGA_MAC_VER_02:
       
  3763 	case RTL_GIGA_MAC_VER_03:
       
  3764 	case RTL_GIGA_MAC_VER_04:
       
  3765 	case RTL_GIGA_MAC_VER_05:
       
  3766 	case RTL_GIGA_MAC_VER_06:
       
  3767 	case RTL_GIGA_MAC_VER_10:
       
  3768 	case RTL_GIGA_MAC_VER_11:
       
  3769 	case RTL_GIGA_MAC_VER_12:
       
  3770 	case RTL_GIGA_MAC_VER_13:
       
  3771 	case RTL_GIGA_MAC_VER_14:
       
  3772 	case RTL_GIGA_MAC_VER_15:
       
  3773 	case RTL_GIGA_MAC_VER_16:
       
  3774 	case RTL_GIGA_MAC_VER_17:
       
  3775 		RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
       
  3776 		break;
       
  3777 	case RTL_GIGA_MAC_VER_18:
       
  3778 	case RTL_GIGA_MAC_VER_19:
       
  3779 	case RTL_GIGA_MAC_VER_20:
       
  3780 	case RTL_GIGA_MAC_VER_21:
       
  3781 	case RTL_GIGA_MAC_VER_22:
       
  3782 	case RTL_GIGA_MAC_VER_23:
       
  3783 	case RTL_GIGA_MAC_VER_24:
       
  3784 		RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
       
  3785 		break;
       
  3786 	default:
       
  3787 		RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
       
  3788 		break;
       
  3789 	}
       
  3790 }
       
  3791 
       
  3792 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
       
  3793 {
       
  3794 	tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
       
  3795 }
       
  3796 
       
  3797 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
       
  3798 {
       
  3799 	void __iomem *ioaddr = tp->mmio_addr;
       
  3800 
       
  3801 	RTL_W8(Cfg9346, Cfg9346_Unlock);
       
  3802 	rtl_generic_op(tp, tp->jumbo_ops.enable);
       
  3803 	RTL_W8(Cfg9346, Cfg9346_Lock);
       
  3804 }
       
  3805 
       
  3806 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
       
  3807 {
       
  3808 	void __iomem *ioaddr = tp->mmio_addr;
       
  3809 
       
  3810 	RTL_W8(Cfg9346, Cfg9346_Unlock);
       
  3811 	rtl_generic_op(tp, tp->jumbo_ops.disable);
       
  3812 	RTL_W8(Cfg9346, Cfg9346_Lock);
       
  3813 }
       
  3814 
       
  3815 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
       
  3816 {
       
  3817 	void __iomem *ioaddr = tp->mmio_addr;
       
  3818 
       
  3819 	RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
       
  3820 	RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
       
  3821 	rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
       
  3822 }
       
  3823 
       
  3824 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
       
  3825 {
       
  3826 	void __iomem *ioaddr = tp->mmio_addr;
       
  3827 
       
  3828 	RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
       
  3829 	RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
       
  3830 	rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  3831 }
       
  3832 
       
  3833 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
       
  3834 {
       
  3835 	void __iomem *ioaddr = tp->mmio_addr;
       
  3836 
       
  3837 	RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
       
  3838 }
       
  3839 
       
  3840 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
       
  3841 {
       
  3842 	void __iomem *ioaddr = tp->mmio_addr;
       
  3843 
       
  3844 	RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
       
  3845 }
       
  3846 
       
  3847 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
       
  3848 {
       
  3849 	void __iomem *ioaddr = tp->mmio_addr;
       
  3850 	struct pci_dev *pdev = tp->pci_dev;
       
  3851 
       
  3852 	RTL_W8(MaxTxPacketSize, 0x3f);
       
  3853 	RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
       
  3854 	RTL_W8(Config4, RTL_R8(Config4) | 0x01);
       
  3855 	pci_write_config_byte(pdev, 0x79, 0x20);
       
  3856 }
       
  3857 
       
  3858 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
       
  3859 {
       
  3860 	void __iomem *ioaddr = tp->mmio_addr;
       
  3861 	struct pci_dev *pdev = tp->pci_dev;
       
  3862 
       
  3863 	RTL_W8(MaxTxPacketSize, 0x0c);
       
  3864 	RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
       
  3865 	RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
       
  3866 	pci_write_config_byte(pdev, 0x79, 0x50);
       
  3867 }
       
  3868 
       
  3869 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
       
  3870 {
       
  3871 	rtl_tx_performance_tweak(tp->pci_dev,
       
  3872 		(0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
       
  3873 }
       
  3874 
       
  3875 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
       
  3876 {
       
  3877 	rtl_tx_performance_tweak(tp->pci_dev,
       
  3878 		(0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
       
  3879 }
       
  3880 
       
  3881 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
       
  3882 {
       
  3883 	void __iomem *ioaddr = tp->mmio_addr;
       
  3884 
       
  3885 	r8168b_0_hw_jumbo_enable(tp);
       
  3886 
       
  3887 	RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
       
  3888 }
       
  3889 
       
  3890 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
       
  3891 {
       
  3892 	void __iomem *ioaddr = tp->mmio_addr;
       
  3893 
       
  3894 	r8168b_0_hw_jumbo_disable(tp);
       
  3895 
       
  3896 	RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
       
  3897 }
       
  3898 
       
  3899 static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
       
  3900 {
       
  3901 	struct jumbo_ops *ops = &tp->jumbo_ops;
       
  3902 
       
  3903 	switch (tp->mac_version) {
       
  3904 	case RTL_GIGA_MAC_VER_11:
       
  3905 		ops->disable	= r8168b_0_hw_jumbo_disable;
       
  3906 		ops->enable	= r8168b_0_hw_jumbo_enable;
       
  3907 		break;
       
  3908 	case RTL_GIGA_MAC_VER_12:
       
  3909 	case RTL_GIGA_MAC_VER_17:
       
  3910 		ops->disable	= r8168b_1_hw_jumbo_disable;
       
  3911 		ops->enable	= r8168b_1_hw_jumbo_enable;
       
  3912 		break;
       
  3913 	case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
       
  3914 	case RTL_GIGA_MAC_VER_19:
       
  3915 	case RTL_GIGA_MAC_VER_20:
       
  3916 	case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
       
  3917 	case RTL_GIGA_MAC_VER_22:
       
  3918 	case RTL_GIGA_MAC_VER_23:
       
  3919 	case RTL_GIGA_MAC_VER_24:
       
  3920 	case RTL_GIGA_MAC_VER_25:
       
  3921 	case RTL_GIGA_MAC_VER_26:
       
  3922 		ops->disable	= r8168c_hw_jumbo_disable;
       
  3923 		ops->enable	= r8168c_hw_jumbo_enable;
       
  3924 		break;
       
  3925 	case RTL_GIGA_MAC_VER_27:
       
  3926 	case RTL_GIGA_MAC_VER_28:
       
  3927 		ops->disable	= r8168dp_hw_jumbo_disable;
       
  3928 		ops->enable	= r8168dp_hw_jumbo_enable;
       
  3929 		break;
       
  3930 	case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
       
  3931 	case RTL_GIGA_MAC_VER_32:
       
  3932 	case RTL_GIGA_MAC_VER_33:
       
  3933 	case RTL_GIGA_MAC_VER_34:
       
  3934 		ops->disable	= r8168e_hw_jumbo_disable;
       
  3935 		ops->enable	= r8168e_hw_jumbo_enable;
       
  3936 		break;
       
  3937 
       
  3938 	/*
       
  3939 	 * No action needed for jumbo frames with 8169.
       
  3940 	 * No jumbo for 810x at all.
       
  3941 	 */
       
  3942 	default:
       
  3943 		ops->disable	= NULL;
       
  3944 		ops->enable	= NULL;
       
  3945 		break;
       
  3946 	}
       
  3947 }
       
  3948 
       
  3949 static void rtl_hw_reset(struct rtl8169_private *tp)
       
  3950 {
       
  3951 	void __iomem *ioaddr = tp->mmio_addr;
       
  3952 	int i;
       
  3953 
       
  3954 	/* Soft reset the chip. */
       
  3955 	RTL_W8(ChipCmd, CmdReset);
       
  3956 
       
  3957 	/* Check that the chip has finished the reset. */
       
  3958 	for (i = 0; i < 100; i++) {
       
  3959 		if ((RTL_R8(ChipCmd) & CmdReset) == 0)
       
  3960 			break;
       
  3961 		udelay(100);
       
  3962 	}
       
  3963 }
       
  3964 
       
  3965 static int __devinit
       
  3966 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
       
  3967 {
       
  3968 	const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
       
  3969 	const unsigned int region = cfg->region;
       
  3970 	struct rtl8169_private *tp;
       
  3971 	struct mii_if_info *mii;
       
  3972 	struct net_device *dev;
       
  3973 	void __iomem *ioaddr;
       
  3974 	int chipset, i;
       
  3975 	int rc;
       
  3976 
       
  3977 	if (netif_msg_drv(&debug)) {
       
  3978 		printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
       
  3979 		       MODULENAME, RTL8169_VERSION);
       
  3980 	}
       
  3981 
       
  3982 	dev = alloc_etherdev(sizeof (*tp));
       
  3983 	if (!dev) {
       
  3984 		if (netif_msg_drv(&debug))
       
  3985 			dev_err(&pdev->dev, "unable to alloc new ethernet\n");
       
  3986 		rc = -ENOMEM;
       
  3987 		goto out;
       
  3988 	}
       
  3989 
       
  3990 	SET_NETDEV_DEV(dev, &pdev->dev);
       
  3991 	dev->netdev_ops = &rtl8169_netdev_ops;
       
  3992 	tp = netdev_priv(dev);
       
  3993 	tp->dev = dev;
       
  3994 	tp->pci_dev = pdev;
       
  3995 	tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
       
  3996 
       
  3997 	mii = &tp->mii;
       
  3998 	mii->dev = dev;
       
  3999 	mii->mdio_read = rtl_mdio_read;
       
  4000 	mii->mdio_write = rtl_mdio_write;
       
  4001 	mii->phy_id_mask = 0x1f;
       
  4002 	mii->reg_num_mask = 0x1f;
       
  4003 	mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
       
  4004 
       
  4005 	/* disable ASPM completely as that cause random device stop working
       
  4006 	 * problems as well as full system hangs for some PCIe devices users */
       
  4007 	pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
       
  4008 				     PCIE_LINK_STATE_CLKPM);
       
  4009 
       
  4010 	/* enable device (incl. PCI PM wakeup and hotplug setup) */
       
  4011 	rc = pci_enable_device(pdev);
       
  4012 	if (rc < 0) {
       
  4013 		netif_err(tp, probe, dev, "enable failure\n");
       
  4014 		goto err_out_free_dev_1;
       
  4015 	}
       
  4016 
       
  4017 	if (pci_set_mwi(pdev) < 0)
       
  4018 		netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
       
  4019 
       
  4020 	/* make sure PCI base addr 1 is MMIO */
       
  4021 	if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
       
  4022 		netif_err(tp, probe, dev,
       
  4023 			  "region #%d not an MMIO resource, aborting\n",
       
  4024 			  region);
       
  4025 		rc = -ENODEV;
       
  4026 		goto err_out_mwi_2;
       
  4027 	}
       
  4028 
       
  4029 	/* check for weird/broken PCI region reporting */
       
  4030 	if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
       
  4031 		netif_err(tp, probe, dev,
       
  4032 			  "Invalid PCI region size(s), aborting\n");
       
  4033 		rc = -ENODEV;
       
  4034 		goto err_out_mwi_2;
       
  4035 	}
       
  4036 
       
  4037 	rc = pci_request_regions(pdev, MODULENAME);
       
  4038 	if (rc < 0) {
       
  4039 		netif_err(tp, probe, dev, "could not request regions\n");
       
  4040 		goto err_out_mwi_2;
       
  4041 	}
       
  4042 
       
  4043 	tp->cp_cmd = RxChkSum;
       
  4044 
       
  4045 	if ((sizeof(dma_addr_t) > 4) &&
       
  4046 	    !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
       
  4047 		tp->cp_cmd |= PCIDAC;
       
  4048 		dev->features |= NETIF_F_HIGHDMA;
       
  4049 	} else {
       
  4050 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
       
  4051 		if (rc < 0) {
       
  4052 			netif_err(tp, probe, dev, "DMA configuration failed\n");
       
  4053 			goto err_out_free_res_3;
       
  4054 		}
       
  4055 	}
       
  4056 
       
  4057 	/* ioremap MMIO region */
       
  4058 	ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
       
  4059 	if (!ioaddr) {
       
  4060 		netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
       
  4061 		rc = -EIO;
       
  4062 		goto err_out_free_res_3;
       
  4063 	}
       
  4064 	tp->mmio_addr = ioaddr;
       
  4065 
       
  4066 	if (!pci_is_pcie(pdev))
       
  4067 		netif_info(tp, probe, dev, "not PCI Express\n");
       
  4068 
       
  4069 	/* Identify chip attached to board */
       
  4070 	rtl8169_get_mac_version(tp, dev, cfg->default_ver);
       
  4071 
       
  4072 	rtl_init_rxcfg(tp);
       
  4073 
       
  4074 	RTL_W16(IntrMask, 0x0000);
       
  4075 
       
  4076 	rtl_hw_reset(tp);
       
  4077 
       
  4078 	RTL_W16(IntrStatus, 0xffff);
       
  4079 
       
  4080 	pci_set_master(pdev);
       
  4081 
       
  4082 	/*
       
  4083 	 * Pretend we are using VLANs; This bypasses a nasty bug where
       
  4084 	 * Interrupts stop flowing on high load on 8110SCd controllers.
       
  4085 	 */
       
  4086 	if (tp->mac_version == RTL_GIGA_MAC_VER_05)
       
  4087 		tp->cp_cmd |= RxVlan;
       
  4088 
       
  4089 	rtl_init_mdio_ops(tp);
       
  4090 	rtl_init_pll_power_ops(tp);
       
  4091 	rtl_init_jumbo_ops(tp);
       
  4092 
       
  4093 	rtl8169_print_mac_version(tp);
       
  4094 
       
  4095 	chipset = tp->mac_version;
       
  4096 	tp->txd_version = rtl_chip_infos[chipset].txd_version;
       
  4097 
       
  4098 	RTL_W8(Cfg9346, Cfg9346_Unlock);
       
  4099 	RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
       
  4100 	RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
       
  4101 	if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
       
  4102 		tp->features |= RTL_FEATURE_WOL;
       
  4103 	if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
       
  4104 		tp->features |= RTL_FEATURE_WOL;
       
  4105 	tp->features |= rtl_try_msi(tp, cfg);
       
  4106 	RTL_W8(Cfg9346, Cfg9346_Lock);
       
  4107 
       
  4108 	if (rtl_tbi_enabled(tp)) {
       
  4109 		tp->set_speed = rtl8169_set_speed_tbi;
       
  4110 		tp->get_settings = rtl8169_gset_tbi;
       
  4111 		tp->phy_reset_enable = rtl8169_tbi_reset_enable;
       
  4112 		tp->phy_reset_pending = rtl8169_tbi_reset_pending;
       
  4113 		tp->link_ok = rtl8169_tbi_link_ok;
       
  4114 		tp->do_ioctl = rtl_tbi_ioctl;
       
  4115 	} else {
       
  4116 		tp->set_speed = rtl8169_set_speed_xmii;
       
  4117 		tp->get_settings = rtl8169_gset_xmii;
       
  4118 		tp->phy_reset_enable = rtl8169_xmii_reset_enable;
       
  4119 		tp->phy_reset_pending = rtl8169_xmii_reset_pending;
       
  4120 		tp->link_ok = rtl8169_xmii_link_ok;
       
  4121 		tp->do_ioctl = rtl_xmii_ioctl;
       
  4122 	}
       
  4123 
       
  4124 	spin_lock_init(&tp->lock);
       
  4125 
       
  4126 	/* Get MAC address */
       
  4127 	for (i = 0; i < MAC_ADDR_LEN; i++)
       
  4128 		dev->dev_addr[i] = RTL_R8(MAC0 + i);
       
  4129 	memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
       
  4130 
       
  4131 	SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
       
  4132 	dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
       
  4133 	dev->irq = pdev->irq;
       
  4134 	dev->base_addr = (unsigned long) ioaddr;
       
  4135 
       
  4136 	netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
       
  4137 
       
  4138 	/* don't enable SG, IP_CSUM and TSO by default - it might not work
       
  4139 	 * properly for all devices */
       
  4140 	dev->features |= NETIF_F_RXCSUM |
       
  4141 		NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
       
  4142 
       
  4143 	dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
       
  4144 		NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
       
  4145 	dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
       
  4146 		NETIF_F_HIGHDMA;
       
  4147 
       
  4148 	if (tp->mac_version == RTL_GIGA_MAC_VER_05)
       
  4149 		/* 8110SCd requires hardware Rx VLAN - disallow toggling */
       
  4150 		dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
       
  4151 
       
  4152 	tp->intr_mask = 0xffff;
       
  4153 	tp->hw_start = cfg->hw_start;
       
  4154 	tp->intr_event = cfg->intr_event;
       
  4155 	tp->napi_event = cfg->napi_event;
       
  4156 
       
  4157 	tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
       
  4158 		~(RxBOVF | RxFOVF) : ~0;
       
  4159 
       
  4160 	init_timer(&tp->timer);
       
  4161 	tp->timer.data = (unsigned long) dev;
       
  4162 	tp->timer.function = rtl8169_phy_timer;
       
  4163 
       
  4164 	tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
       
  4165 
       
  4166 	// offer device to EtherCAT master module
       
  4167 	tp->ecdev = ecdev_offer(dev, ec_poll, THIS_MODULE);
       
  4168 	tp->ec_watchdog_jiffies = jiffies;
       
  4169 
       
  4170 	if (!tp->ecdev) {
       
  4171 		rc = register_netdev(dev);
       
  4172 		if (rc < 0)
       
  4173 			goto err_out_msi_4;
       
  4174 	}
       
  4175 
       
  4176 	pci_set_drvdata(pdev, dev);
       
  4177 
       
  4178 	netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
       
  4179 		   rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
       
  4180 		   (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
       
  4181 	if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
       
  4182 		netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
       
  4183 			   "tx checksumming: %s]\n",
       
  4184 			   rtl_chip_infos[chipset].jumbo_max,
       
  4185 			   rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
       
  4186 	}
       
  4187 
       
  4188 	if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
       
  4189 	    tp->mac_version == RTL_GIGA_MAC_VER_28 ||
       
  4190 	    tp->mac_version == RTL_GIGA_MAC_VER_31) {
       
  4191 		rtl8168_driver_start(tp);
       
  4192 	}
       
  4193 
       
  4194 	device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
       
  4195 
       
  4196 	if (pci_dev_run_wake(pdev))
       
  4197 		pm_runtime_put_noidle(&pdev->dev);
       
  4198 
       
  4199 	netif_carrier_off(dev);
       
  4200 
       
  4201 	if (tp->ecdev && ecdev_open(tp->ecdev)) {
       
  4202 		ecdev_withdraw(tp->ecdev);
       
  4203 		goto err_out_msi_4;
       
  4204 	}
       
  4205 
       
  4206 out:
       
  4207 	return rc;
       
  4208 
       
  4209 err_out_msi_4:
       
  4210 	rtl_disable_msi(pdev, tp);
       
  4211 	iounmap(ioaddr);
       
  4212 err_out_free_res_3:
       
  4213 	pci_release_regions(pdev);
       
  4214 err_out_mwi_2:
       
  4215 	pci_clear_mwi(pdev);
       
  4216 	pci_disable_device(pdev);
       
  4217 err_out_free_dev_1:
       
  4218 	free_netdev(dev);
       
  4219 	goto out;
       
  4220 }
       
  4221 
       
  4222 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
       
  4223 {
       
  4224 	struct net_device *dev = pci_get_drvdata(pdev);
       
  4225 	struct rtl8169_private *tp = netdev_priv(dev);
       
  4226 
       
  4227 	if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
       
  4228 	    tp->mac_version == RTL_GIGA_MAC_VER_28 ||
       
  4229 	    tp->mac_version == RTL_GIGA_MAC_VER_31) {
       
  4230 		rtl8168_driver_stop(tp);
       
  4231 	}
       
  4232 
       
  4233 	cancel_delayed_work_sync(&tp->task);
       
  4234 
       
  4235 	if (tp->ecdev) {
       
  4236 		ecdev_close(tp->ecdev);
       
  4237 		ecdev_withdraw(tp->ecdev);
       
  4238 	} else {
       
  4239 		unregister_netdev(dev);
       
  4240 	}
       
  4241 
       
  4242 	rtl_release_firmware(tp);
       
  4243 
       
  4244 	if (pci_dev_run_wake(pdev))
       
  4245 		pm_runtime_get_noresume(&pdev->dev);
       
  4246 
       
  4247 	/* restore original MAC address */
       
  4248 	rtl_rar_set(tp, dev->perm_addr);
       
  4249 
       
  4250 	rtl_disable_msi(pdev, tp);
       
  4251 	rtl8169_release_board(pdev, dev, tp->mmio_addr);
       
  4252 	pci_set_drvdata(pdev, NULL);
       
  4253 }
       
  4254 
       
  4255 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
       
  4256 {
       
  4257 	struct rtl_fw *rtl_fw;
       
  4258 	const char *name;
       
  4259 	int rc = -ENOMEM;
       
  4260 
       
  4261 	name = rtl_lookup_firmware_name(tp);
       
  4262 	if (!name)
       
  4263 		goto out_no_firmware;
       
  4264 
       
  4265 	rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
       
  4266 	if (!rtl_fw)
       
  4267 		goto err_warn;
       
  4268 
       
  4269 	rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
       
  4270 	if (rc < 0)
       
  4271 		goto err_free;
       
  4272 
       
  4273 	rc = rtl_check_firmware(tp, rtl_fw);
       
  4274 	if (rc < 0)
       
  4275 		goto err_release_firmware;
       
  4276 
       
  4277 	tp->rtl_fw = rtl_fw;
       
  4278 out:
       
  4279 	return;
       
  4280 
       
  4281 err_release_firmware:
       
  4282 	release_firmware(rtl_fw->fw);
       
  4283 err_free:
       
  4284 	kfree(rtl_fw);
       
  4285 err_warn:
       
  4286 	netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
       
  4287 		   name, rc);
       
  4288 out_no_firmware:
       
  4289 	tp->rtl_fw = NULL;
       
  4290 	goto out;
       
  4291 }
       
  4292 
       
  4293 static void rtl_request_firmware(struct rtl8169_private *tp)
       
  4294 {
       
  4295 	if (IS_ERR(tp->rtl_fw))
       
  4296 		rtl_request_uncached_firmware(tp);
       
  4297 }
       
  4298 
       
  4299 static int rtl8169_open(struct net_device *dev)
       
  4300 {
       
  4301 	struct rtl8169_private *tp = netdev_priv(dev);
       
  4302 	void __iomem *ioaddr = tp->mmio_addr;
       
  4303 	struct pci_dev *pdev = tp->pci_dev;
       
  4304 	int retval = -ENOMEM;
       
  4305 
       
  4306 	pm_runtime_get_sync(&pdev->dev);
       
  4307 
       
  4308 	/*
       
  4309 	 * Rx and Tx desscriptors needs 256 bytes alignment.
       
  4310 	 * dma_alloc_coherent provides more.
       
  4311 	 */
       
  4312 	tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
       
  4313 					     &tp->TxPhyAddr, GFP_KERNEL);
       
  4314 	if (!tp->TxDescArray)
       
  4315 		goto err_pm_runtime_put;
       
  4316 
       
  4317 	tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
       
  4318 					     &tp->RxPhyAddr, GFP_KERNEL);
       
  4319 	if (!tp->RxDescArray)
       
  4320 		goto err_free_tx_0;
       
  4321 
       
  4322 	retval = rtl8169_init_ring(dev);
       
  4323 	if (retval < 0)
       
  4324 		goto err_free_rx_1;
       
  4325 
       
  4326 	INIT_DELAYED_WORK(&tp->task, NULL);
       
  4327 
       
  4328 	smp_mb();
       
  4329 
       
  4330 	rtl_request_firmware(tp);
       
  4331 	if (!tp->ecdev) {
       
  4332 
       
  4333 		retval = request_irq(dev->irq, rtl8169_interrupt,
       
  4334 				(tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
       
  4335 				dev->name, dev);
       
  4336 		if (retval < 0)
       
  4337 		goto err_release_fw_2;
       
  4338 
       
  4339 		napi_enable(&tp->napi);
       
  4340 	}
       
  4341 
       
  4342 	rtl8169_init_phy(dev, tp);
       
  4343 
       
  4344 	rtl8169_set_features(dev, dev->features);
       
  4345 
       
  4346 	rtl_pll_power_up(tp);
       
  4347 
       
  4348 	rtl_hw_start(dev);
       
  4349 
       
  4350 	tp->saved_wolopts = 0;
       
  4351 	pm_runtime_put_noidle(&pdev->dev);
       
  4352 
       
  4353 	rtl8169_check_link_status(dev, tp, ioaddr);
       
  4354 out:
       
  4355 	return retval;
       
  4356 
       
  4357 err_release_fw_2:
       
  4358 	rtl_release_firmware(tp);
       
  4359 	rtl8169_rx_clear(tp);
       
  4360 err_free_rx_1:
       
  4361 	dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
       
  4362 			  tp->RxPhyAddr);
       
  4363 	tp->RxDescArray = NULL;
       
  4364 err_free_tx_0:
       
  4365 	dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
       
  4366 			  tp->TxPhyAddr);
       
  4367 	tp->TxDescArray = NULL;
       
  4368 err_pm_runtime_put:
       
  4369 	pm_runtime_put_noidle(&pdev->dev);
       
  4370 	goto out;
       
  4371 }
       
  4372 
       
  4373 static void rtl_rx_close(struct rtl8169_private *tp)
       
  4374 {
       
  4375 	void __iomem *ioaddr = tp->mmio_addr;
       
  4376 
       
  4377 	RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
       
  4378 }
       
  4379 
       
  4380 static void rtl8169_hw_reset(struct rtl8169_private *tp)
       
  4381 {
       
  4382 	void __iomem *ioaddr = tp->mmio_addr;
       
  4383 
       
  4384 	/* Disable interrupts */
       
  4385 	rtl8169_irq_mask_and_ack(tp);
       
  4386 
       
  4387 	rtl_rx_close(tp);
       
  4388 
       
  4389 	if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
       
  4390 	    tp->mac_version == RTL_GIGA_MAC_VER_28 ||
       
  4391 	    tp->mac_version == RTL_GIGA_MAC_VER_31) {
       
  4392 		while (RTL_R8(TxPoll) & NPQ)
       
  4393 			udelay(20);
       
  4394 	} else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
       
  4395 	           tp->mac_version == RTL_GIGA_MAC_VER_35 ||
       
  4396 	           tp->mac_version == RTL_GIGA_MAC_VER_36) {
       
  4397 		RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
       
  4398 		while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
       
  4399 			udelay(100);
       
  4400 	} else {
       
  4401 		RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
       
  4402 		udelay(100);
       
  4403 	}
       
  4404 
       
  4405 	rtl_hw_reset(tp);
       
  4406 }
       
  4407 
       
  4408 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
       
  4409 {
       
  4410 	void __iomem *ioaddr = tp->mmio_addr;
       
  4411 
       
  4412 	/* Set DMA burst size and Interframe Gap Time */
       
  4413 	RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
       
  4414 		(InterFrameGap << TxInterFrameGapShift));
       
  4415 }
       
  4416 
       
  4417 static void rtl_hw_start(struct net_device *dev)
       
  4418 {
       
  4419 	struct rtl8169_private *tp = netdev_priv(dev);
       
  4420 
       
  4421 	tp->hw_start(dev);
       
  4422 
       
  4423 	if (!tp->ecdev)
       
  4424 		netif_start_queue(dev);
       
  4425 }
       
  4426 
       
  4427 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
       
  4428 					 void __iomem *ioaddr)
       
  4429 {
       
  4430 	/*
       
  4431 	 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
       
  4432 	 * register to be written before TxDescAddrLow to work.
       
  4433 	 * Switching from MMIO to I/O access fixes the issue as well.
       
  4434 	 */
       
  4435 	RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
       
  4436 	RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
       
  4437 	RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
       
  4438 	RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
       
  4439 }
       
  4440 
       
  4441 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
       
  4442 {
       
  4443 	u16 cmd;
       
  4444 
       
  4445 	cmd = RTL_R16(CPlusCmd);
       
  4446 	RTL_W16(CPlusCmd, cmd);
       
  4447 	return cmd;
       
  4448 }
       
  4449 
       
  4450 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
       
  4451 {
       
  4452 	/* Low hurts. Let's disable the filtering. */
       
  4453 	RTL_W16(RxMaxSize, rx_buf_sz + 1);
       
  4454 }
       
  4455 
       
  4456 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
       
  4457 {
       
  4458 	static const struct rtl_cfg2_info {
       
  4459 		u32 mac_version;
       
  4460 		u32 clk;
       
  4461 		u32 val;
       
  4462 	} cfg2_info [] = {
       
  4463 		{ RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
       
  4464 		{ RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
       
  4465 		{ RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
       
  4466 		{ RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
       
  4467 	};
       
  4468 	const struct rtl_cfg2_info *p = cfg2_info;
       
  4469 	unsigned int i;
       
  4470 	u32 clk;
       
  4471 
       
  4472 	clk = RTL_R8(Config2) & PCI_Clock_66MHz;
       
  4473 	for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
       
  4474 		if ((p->mac_version == mac_version) && (p->clk == clk)) {
       
  4475 			RTL_W32(0x7c, p->val);
       
  4476 			break;
       
  4477 		}
       
  4478 	}
       
  4479 }
       
  4480 
       
  4481 static void rtl_hw_start_8169(struct net_device *dev)
       
  4482 {
       
  4483 	struct rtl8169_private *tp = netdev_priv(dev);
       
  4484 	void __iomem *ioaddr = tp->mmio_addr;
       
  4485 	struct pci_dev *pdev = tp->pci_dev;
       
  4486 
       
  4487 	if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
       
  4488 		RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
       
  4489 		pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
       
  4490 	}
       
  4491 
       
  4492 	RTL_W8(Cfg9346, Cfg9346_Unlock);
       
  4493 	if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
       
  4494 	    tp->mac_version == RTL_GIGA_MAC_VER_02 ||
       
  4495 	    tp->mac_version == RTL_GIGA_MAC_VER_03 ||
       
  4496 	    tp->mac_version == RTL_GIGA_MAC_VER_04)
       
  4497 		RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
       
  4498 
       
  4499 	rtl_init_rxcfg(tp);
       
  4500 
       
  4501 	RTL_W8(EarlyTxThres, NoEarlyTx);
       
  4502 
       
  4503 	rtl_set_rx_max_size(ioaddr, rx_buf_sz);
       
  4504 
       
  4505 	if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
       
  4506 	    tp->mac_version == RTL_GIGA_MAC_VER_02 ||
       
  4507 	    tp->mac_version == RTL_GIGA_MAC_VER_03 ||
       
  4508 	    tp->mac_version == RTL_GIGA_MAC_VER_04)
       
  4509 		rtl_set_rx_tx_config_registers(tp);
       
  4510 
       
  4511 	tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
       
  4512 
       
  4513 	if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
       
  4514 	    tp->mac_version == RTL_GIGA_MAC_VER_03) {
       
  4515 		dprintk("Set MAC Reg C+CR Offset 0xE0. "
       
  4516 			"Bit-3 and bit-14 MUST be 1\n");
       
  4517 		tp->cp_cmd |= (1 << 14);
       
  4518 	}
       
  4519 
       
  4520 	RTL_W16(CPlusCmd, tp->cp_cmd);
       
  4521 
       
  4522 	rtl8169_set_magic_reg(ioaddr, tp->mac_version);
       
  4523 
       
  4524 	/*
       
  4525 	 * Undocumented corner. Supposedly:
       
  4526 	 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
       
  4527 	 */
       
  4528 	RTL_W16(IntrMitigate, 0x0000);
       
  4529 
       
  4530 	rtl_set_rx_tx_desc_registers(tp, ioaddr);
       
  4531 
       
  4532 	if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
       
  4533 	    tp->mac_version != RTL_GIGA_MAC_VER_02 &&
       
  4534 	    tp->mac_version != RTL_GIGA_MAC_VER_03 &&
       
  4535 	    tp->mac_version != RTL_GIGA_MAC_VER_04) {
       
  4536 		RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
       
  4537 		rtl_set_rx_tx_config_registers(tp);
       
  4538 	}
       
  4539 
       
  4540 	RTL_W8(Cfg9346, Cfg9346_Lock);
       
  4541 
       
  4542 	/* Initially a 10 us delay. Turned it into a PCI commit. - FR */
       
  4543 	RTL_R8(IntrMask);
       
  4544 
       
  4545 	RTL_W32(RxMissed, 0);
       
  4546 
       
  4547 	rtl_set_rx_mode(dev);
       
  4548 
       
  4549 	/* no early-rx interrupts */
       
  4550 	RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
       
  4551 
       
  4552 	/* Enable all known interrupts by setting the interrupt mask. */
       
  4553 	if (!tp->ecdev)
       
  4554 		RTL_W16(IntrMask, tp->intr_event);
       
  4555 }
       
  4556 
       
  4557 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
       
  4558 {
       
  4559 	u32 csi;
       
  4560 
       
  4561 	csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
       
  4562 	rtl_csi_write(ioaddr, 0x070c, csi | bits);
       
  4563 }
       
  4564 
       
  4565 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
       
  4566 {
       
  4567 	rtl_csi_access_enable(ioaddr, 0x17000000);
       
  4568 }
       
  4569 
       
  4570 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
       
  4571 {
       
  4572 	rtl_csi_access_enable(ioaddr, 0x27000000);
       
  4573 }
       
  4574 
       
  4575 struct ephy_info {
       
  4576 	unsigned int offset;
       
  4577 	u16 mask;
       
  4578 	u16 bits;
       
  4579 };
       
  4580 
       
  4581 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
       
  4582 {
       
  4583 	u16 w;
       
  4584 
       
  4585 	while (len-- > 0) {
       
  4586 		w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
       
  4587 		rtl_ephy_write(ioaddr, e->offset, w);
       
  4588 		e++;
       
  4589 	}
       
  4590 }
       
  4591 
       
  4592 static void rtl_disable_clock_request(struct pci_dev *pdev)
       
  4593 {
       
  4594 	int cap = pci_pcie_cap(pdev);
       
  4595 
       
  4596 	if (cap) {
       
  4597 		u16 ctl;
       
  4598 
       
  4599 		pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
       
  4600 		ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
       
  4601 		pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
       
  4602 	}
       
  4603 }
       
  4604 
       
  4605 static void rtl_enable_clock_request(struct pci_dev *pdev)
       
  4606 {
       
  4607 	int cap = pci_pcie_cap(pdev);
       
  4608 
       
  4609 	if (cap) {
       
  4610 		u16 ctl;
       
  4611 
       
  4612 		pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
       
  4613 		ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
       
  4614 		pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
       
  4615 	}
       
  4616 }
       
  4617 
       
  4618 #define R8168_CPCMD_QUIRK_MASK (\
       
  4619 	EnableBist | \
       
  4620 	Mac_dbgo_oe | \
       
  4621 	Force_half_dup | \
       
  4622 	Force_rxflow_en | \
       
  4623 	Force_txflow_en | \
       
  4624 	Cxpl_dbg_sel | \
       
  4625 	ASF | \
       
  4626 	PktCntrDisable | \
       
  4627 	Mac_dbgo_sel)
       
  4628 
       
  4629 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4630 {
       
  4631 	RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
       
  4632 
       
  4633 	RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
       
  4634 
       
  4635 	rtl_tx_performance_tweak(pdev,
       
  4636 		(0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
       
  4637 }
       
  4638 
       
  4639 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4640 {
       
  4641 	rtl_hw_start_8168bb(ioaddr, pdev);
       
  4642 
       
  4643 	RTL_W8(MaxTxPacketSize, TxPacketMax);
       
  4644 
       
  4645 	RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
       
  4646 }
       
  4647 
       
  4648 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4649 {
       
  4650 	RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
       
  4651 
       
  4652 	RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
       
  4653 
       
  4654 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4655 
       
  4656 	rtl_disable_clock_request(pdev);
       
  4657 
       
  4658 	RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
       
  4659 }
       
  4660 
       
  4661 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4662 {
       
  4663 	static const struct ephy_info e_info_8168cp[] = {
       
  4664 		{ 0x01, 0,	0x0001 },
       
  4665 		{ 0x02, 0x0800,	0x1000 },
       
  4666 		{ 0x03, 0,	0x0042 },
       
  4667 		{ 0x06, 0x0080,	0x0000 },
       
  4668 		{ 0x07, 0,	0x2000 }
       
  4669 	};
       
  4670 
       
  4671 	rtl_csi_access_enable_2(ioaddr);
       
  4672 
       
  4673 	rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
       
  4674 
       
  4675 	__rtl_hw_start_8168cp(ioaddr, pdev);
       
  4676 }
       
  4677 
       
  4678 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4679 {
       
  4680 	rtl_csi_access_enable_2(ioaddr);
       
  4681 
       
  4682 	RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
       
  4683 
       
  4684 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4685 
       
  4686 	RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
       
  4687 }
       
  4688 
       
  4689 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4690 {
       
  4691 	rtl_csi_access_enable_2(ioaddr);
       
  4692 
       
  4693 	RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
       
  4694 
       
  4695 	/* Magic. */
       
  4696 	RTL_W8(DBG_REG, 0x20);
       
  4697 
       
  4698 	RTL_W8(MaxTxPacketSize, TxPacketMax);
       
  4699 
       
  4700 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4701 
       
  4702 	RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
       
  4703 }
       
  4704 
       
  4705 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4706 {
       
  4707 	static const struct ephy_info e_info_8168c_1[] = {
       
  4708 		{ 0x02, 0x0800,	0x1000 },
       
  4709 		{ 0x03, 0,	0x0002 },
       
  4710 		{ 0x06, 0x0080,	0x0000 }
       
  4711 	};
       
  4712 
       
  4713 	rtl_csi_access_enable_2(ioaddr);
       
  4714 
       
  4715 	RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
       
  4716 
       
  4717 	rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
       
  4718 
       
  4719 	__rtl_hw_start_8168cp(ioaddr, pdev);
       
  4720 }
       
  4721 
       
  4722 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4723 {
       
  4724 	static const struct ephy_info e_info_8168c_2[] = {
       
  4725 		{ 0x01, 0,	0x0001 },
       
  4726 		{ 0x03, 0x0400,	0x0220 }
       
  4727 	};
       
  4728 
       
  4729 	rtl_csi_access_enable_2(ioaddr);
       
  4730 
       
  4731 	rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
       
  4732 
       
  4733 	__rtl_hw_start_8168cp(ioaddr, pdev);
       
  4734 }
       
  4735 
       
  4736 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4737 {
       
  4738 	rtl_hw_start_8168c_2(ioaddr, pdev);
       
  4739 }
       
  4740 
       
  4741 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4742 {
       
  4743 	rtl_csi_access_enable_2(ioaddr);
       
  4744 
       
  4745 	__rtl_hw_start_8168cp(ioaddr, pdev);
       
  4746 }
       
  4747 
       
  4748 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4749 {
       
  4750 	rtl_csi_access_enable_2(ioaddr);
       
  4751 
       
  4752 	rtl_disable_clock_request(pdev);
       
  4753 
       
  4754 	RTL_W8(MaxTxPacketSize, TxPacketMax);
       
  4755 
       
  4756 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4757 
       
  4758 	RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
       
  4759 }
       
  4760 
       
  4761 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4762 {
       
  4763 	rtl_csi_access_enable_1(ioaddr);
       
  4764 
       
  4765 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4766 
       
  4767 	RTL_W8(MaxTxPacketSize, TxPacketMax);
       
  4768 
       
  4769 	rtl_disable_clock_request(pdev);
       
  4770 }
       
  4771 
       
  4772 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4773 {
       
  4774 	static const struct ephy_info e_info_8168d_4[] = {
       
  4775 		{ 0x0b, ~0,	0x48 },
       
  4776 		{ 0x19, 0x20,	0x50 },
       
  4777 		{ 0x0c, ~0,	0x20 }
       
  4778 	};
       
  4779 	int i;
       
  4780 
       
  4781 	rtl_csi_access_enable_1(ioaddr);
       
  4782 
       
  4783 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4784 
       
  4785 	RTL_W8(MaxTxPacketSize, TxPacketMax);
       
  4786 
       
  4787 	for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
       
  4788 		const struct ephy_info *e = e_info_8168d_4 + i;
       
  4789 		u16 w;
       
  4790 
       
  4791 		w = rtl_ephy_read(ioaddr, e->offset);
       
  4792 		rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
       
  4793 	}
       
  4794 
       
  4795 	rtl_enable_clock_request(pdev);
       
  4796 }
       
  4797 
       
  4798 static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4799 {
       
  4800 	static const struct ephy_info e_info_8168e_1[] = {
       
  4801 		{ 0x00, 0x0200,	0x0100 },
       
  4802 		{ 0x00, 0x0000,	0x0004 },
       
  4803 		{ 0x06, 0x0002,	0x0001 },
       
  4804 		{ 0x06, 0x0000,	0x0030 },
       
  4805 		{ 0x07, 0x0000,	0x2000 },
       
  4806 		{ 0x00, 0x0000,	0x0020 },
       
  4807 		{ 0x03, 0x5800,	0x2000 },
       
  4808 		{ 0x03, 0x0000,	0x0001 },
       
  4809 		{ 0x01, 0x0800,	0x1000 },
       
  4810 		{ 0x07, 0x0000,	0x4000 },
       
  4811 		{ 0x1e, 0x0000,	0x2000 },
       
  4812 		{ 0x19, 0xffff,	0xfe6c },
       
  4813 		{ 0x0a, 0x0000,	0x0040 }
       
  4814 	};
       
  4815 
       
  4816 	rtl_csi_access_enable_2(ioaddr);
       
  4817 
       
  4818 	rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
       
  4819 
       
  4820 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4821 
       
  4822 	RTL_W8(MaxTxPacketSize, TxPacketMax);
       
  4823 
       
  4824 	rtl_disable_clock_request(pdev);
       
  4825 
       
  4826 	/* Reset tx FIFO pointer */
       
  4827 	RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
       
  4828 	RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
       
  4829 
       
  4830 	RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
       
  4831 }
       
  4832 
       
  4833 static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4834 {
       
  4835 	static const struct ephy_info e_info_8168e_2[] = {
       
  4836 		{ 0x09, 0x0000,	0x0080 },
       
  4837 		{ 0x19, 0x0000,	0x0224 }
       
  4838 	};
       
  4839 
       
  4840 	rtl_csi_access_enable_1(ioaddr);
       
  4841 
       
  4842 	rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
       
  4843 
       
  4844 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4845 
       
  4846 	rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
       
  4847 	rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
       
  4848 	rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
       
  4849 	rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
       
  4850 	rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
       
  4851 	rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
       
  4852 	rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
       
  4853 	rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
       
  4854 		     ERIAR_EXGMAC);
       
  4855 
       
  4856 	RTL_W8(MaxTxPacketSize, EarlySize);
       
  4857 
       
  4858 	rtl_disable_clock_request(pdev);
       
  4859 
       
  4860 	RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
       
  4861 	RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
       
  4862 
       
  4863 	/* Adjust EEE LED frequency */
       
  4864 	RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
       
  4865 
       
  4866 	RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
       
  4867 	RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
       
  4868 	RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
       
  4869 }
       
  4870 
       
  4871 static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
       
  4872 {
       
  4873 	static const struct ephy_info e_info_8168f_1[] = {
       
  4874 		{ 0x06, 0x00c0,	0x0020 },
       
  4875 		{ 0x08, 0x0001,	0x0002 },
       
  4876 		{ 0x09, 0x0000,	0x0080 },
       
  4877 		{ 0x19, 0x0000,	0x0224 }
       
  4878 	};
       
  4879 
       
  4880 	rtl_csi_access_enable_1(ioaddr);
       
  4881 
       
  4882 	rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
       
  4883 
       
  4884 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  4885 
       
  4886 	rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
       
  4887 	rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
       
  4888 	rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
       
  4889 	rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
       
  4890 	rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
       
  4891 	rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
       
  4892 	rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
       
  4893 	rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
       
  4894 	rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
       
  4895 	rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
       
  4896 	rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
       
  4897 		     ERIAR_EXGMAC);
       
  4898 
       
  4899 	RTL_W8(MaxTxPacketSize, EarlySize);
       
  4900 
       
  4901 	rtl_disable_clock_request(pdev);
       
  4902 
       
  4903 	RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
       
  4904 	RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
       
  4905 
       
  4906 	/* Adjust EEE LED frequency */
       
  4907 	RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
       
  4908 
       
  4909 	RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
       
  4910 	RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
       
  4911 	RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
       
  4912 }
       
  4913 
       
  4914 static void rtl_hw_start_8168(struct net_device *dev)
       
  4915 {
       
  4916 	struct rtl8169_private *tp = netdev_priv(dev);
       
  4917 	void __iomem *ioaddr = tp->mmio_addr;
       
  4918 	struct pci_dev *pdev = tp->pci_dev;
       
  4919 
       
  4920 	RTL_W8(Cfg9346, Cfg9346_Unlock);
       
  4921 
       
  4922 	RTL_W8(MaxTxPacketSize, TxPacketMax);
       
  4923 
       
  4924 	rtl_set_rx_max_size(ioaddr, rx_buf_sz);
       
  4925 
       
  4926 	tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
       
  4927 
       
  4928 	RTL_W16(CPlusCmd, tp->cp_cmd);
       
  4929 
       
  4930 	RTL_W16(IntrMitigate, 0x5151);
       
  4931 
       
  4932 	/* Work around for RxFIFO overflow. */
       
  4933 	if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
       
  4934 		tp->intr_event |= RxFIFOOver | PCSTimeout;
       
  4935 		tp->intr_event &= ~RxOverflow;
       
  4936 	}
       
  4937 
       
  4938 	rtl_set_rx_tx_desc_registers(tp, ioaddr);
       
  4939 
       
  4940 	rtl_set_rx_mode(dev);
       
  4941 
       
  4942 	RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
       
  4943 		(InterFrameGap << TxInterFrameGapShift));
       
  4944 
       
  4945 	RTL_R8(IntrMask);
       
  4946 
       
  4947 	switch (tp->mac_version) {
       
  4948 	case RTL_GIGA_MAC_VER_11:
       
  4949 		rtl_hw_start_8168bb(ioaddr, pdev);
       
  4950 		break;
       
  4951 
       
  4952 	case RTL_GIGA_MAC_VER_12:
       
  4953 	case RTL_GIGA_MAC_VER_17:
       
  4954 		rtl_hw_start_8168bef(ioaddr, pdev);
       
  4955 		break;
       
  4956 
       
  4957 	case RTL_GIGA_MAC_VER_18:
       
  4958 		rtl_hw_start_8168cp_1(ioaddr, pdev);
       
  4959 		break;
       
  4960 
       
  4961 	case RTL_GIGA_MAC_VER_19:
       
  4962 		rtl_hw_start_8168c_1(ioaddr, pdev);
       
  4963 		break;
       
  4964 
       
  4965 	case RTL_GIGA_MAC_VER_20:
       
  4966 		rtl_hw_start_8168c_2(ioaddr, pdev);
       
  4967 		break;
       
  4968 
       
  4969 	case RTL_GIGA_MAC_VER_21:
       
  4970 		rtl_hw_start_8168c_3(ioaddr, pdev);
       
  4971 		break;
       
  4972 
       
  4973 	case RTL_GIGA_MAC_VER_22:
       
  4974 		rtl_hw_start_8168c_4(ioaddr, pdev);
       
  4975 		break;
       
  4976 
       
  4977 	case RTL_GIGA_MAC_VER_23:
       
  4978 		rtl_hw_start_8168cp_2(ioaddr, pdev);
       
  4979 		break;
       
  4980 
       
  4981 	case RTL_GIGA_MAC_VER_24:
       
  4982 		rtl_hw_start_8168cp_3(ioaddr, pdev);
       
  4983 		break;
       
  4984 
       
  4985 	case RTL_GIGA_MAC_VER_25:
       
  4986 	case RTL_GIGA_MAC_VER_26:
       
  4987 	case RTL_GIGA_MAC_VER_27:
       
  4988 		rtl_hw_start_8168d(ioaddr, pdev);
       
  4989 		break;
       
  4990 
       
  4991 	case RTL_GIGA_MAC_VER_28:
       
  4992 		rtl_hw_start_8168d_4(ioaddr, pdev);
       
  4993 		break;
       
  4994 
       
  4995 	case RTL_GIGA_MAC_VER_31:
       
  4996 		rtl_hw_start_8168dp(ioaddr, pdev);
       
  4997 		break;
       
  4998 
       
  4999 	case RTL_GIGA_MAC_VER_32:
       
  5000 	case RTL_GIGA_MAC_VER_33:
       
  5001 		rtl_hw_start_8168e_1(ioaddr, pdev);
       
  5002 		break;
       
  5003 	case RTL_GIGA_MAC_VER_34:
       
  5004 		rtl_hw_start_8168e_2(ioaddr, pdev);
       
  5005 		break;
       
  5006 
       
  5007 	case RTL_GIGA_MAC_VER_35:
       
  5008 	case RTL_GIGA_MAC_VER_36:
       
  5009 		rtl_hw_start_8168f_1(ioaddr, pdev);
       
  5010 		break;
       
  5011 
       
  5012 	default:
       
  5013 		printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
       
  5014 			dev->name, tp->mac_version);
       
  5015 		break;
       
  5016 	}
       
  5017 
       
  5018 	RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
       
  5019 
       
  5020 	RTL_W8(Cfg9346, Cfg9346_Lock);
       
  5021 
       
  5022 	RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
       
  5023 
       
  5024 	if (!tp->ecdev)
       
  5025 		RTL_W16(IntrMask, tp->intr_event);
       
  5026 }
       
  5027 
       
  5028 #define R810X_CPCMD_QUIRK_MASK (\
       
  5029 	EnableBist | \
       
  5030 	Mac_dbgo_oe | \
       
  5031 	Force_half_dup | \
       
  5032 	Force_rxflow_en | \
       
  5033 	Force_txflow_en | \
       
  5034 	Cxpl_dbg_sel | \
       
  5035 	ASF | \
       
  5036 	PktCntrDisable | \
       
  5037 	Mac_dbgo_sel)
       
  5038 
       
  5039 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
       
  5040 {
       
  5041 	static const struct ephy_info e_info_8102e_1[] = {
       
  5042 		{ 0x01,	0, 0x6e65 },
       
  5043 		{ 0x02,	0, 0x091f },
       
  5044 		{ 0x03,	0, 0xc2f9 },
       
  5045 		{ 0x06,	0, 0xafb5 },
       
  5046 		{ 0x07,	0, 0x0e00 },
       
  5047 		{ 0x19,	0, 0xec80 },
       
  5048 		{ 0x01,	0, 0x2e65 },
       
  5049 		{ 0x01,	0, 0x6e65 }
       
  5050 	};
       
  5051 	u8 cfg1;
       
  5052 
       
  5053 	rtl_csi_access_enable_2(ioaddr);
       
  5054 
       
  5055 	RTL_W8(DBG_REG, FIX_NAK_1);
       
  5056 
       
  5057 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  5058 
       
  5059 	RTL_W8(Config1,
       
  5060 	       LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
       
  5061 	RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
       
  5062 
       
  5063 	cfg1 = RTL_R8(Config1);
       
  5064 	if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
       
  5065 		RTL_W8(Config1, cfg1 & ~LEDS0);
       
  5066 
       
  5067 	rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
       
  5068 }
       
  5069 
       
  5070 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
       
  5071 {
       
  5072 	rtl_csi_access_enable_2(ioaddr);
       
  5073 
       
  5074 	rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
       
  5075 
       
  5076 	RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
       
  5077 	RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
       
  5078 }
       
  5079 
       
  5080 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
       
  5081 {
       
  5082 	rtl_hw_start_8102e_2(ioaddr, pdev);
       
  5083 
       
  5084 	rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
       
  5085 }
       
  5086 
       
  5087 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
       
  5088 {
       
  5089 	static const struct ephy_info e_info_8105e_1[] = {
       
  5090 		{ 0x07,	0, 0x4000 },
       
  5091 		{ 0x19,	0, 0x0200 },
       
  5092 		{ 0x19,	0, 0x0020 },
       
  5093 		{ 0x1e,	0, 0x2000 },
       
  5094 		{ 0x03,	0, 0x0001 },
       
  5095 		{ 0x19,	0, 0x0100 },
       
  5096 		{ 0x19,	0, 0x0004 },
       
  5097 		{ 0x0a,	0, 0x0020 }
       
  5098 	};
       
  5099 
       
  5100 	/* Force LAN exit from ASPM if Rx/Tx are not idle */
       
  5101 	RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
       
  5102 
       
  5103 	/* Disable Early Tally Counter */
       
  5104 	RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
       
  5105 
       
  5106 	RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
       
  5107 	RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
       
  5108 
       
  5109 	rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
       
  5110 }
       
  5111 
       
  5112 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
       
  5113 {
       
  5114 	rtl_hw_start_8105e_1(ioaddr, pdev);
       
  5115 	rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
       
  5116 }
       
  5117 
       
  5118 static void rtl_hw_start_8101(struct net_device *dev)
       
  5119 {
       
  5120 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5121 	void __iomem *ioaddr = tp->mmio_addr;
       
  5122 	struct pci_dev *pdev = tp->pci_dev;
       
  5123 
       
  5124 	if (tp->mac_version >= RTL_GIGA_MAC_VER_30) {
       
  5125 		tp->intr_event &= ~RxFIFOOver;
       
  5126 		tp->napi_event &= ~RxFIFOOver;
       
  5127 	}
       
  5128 
       
  5129 	if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
       
  5130 	    tp->mac_version == RTL_GIGA_MAC_VER_16) {
       
  5131 		int cap = pci_pcie_cap(pdev);
       
  5132 
       
  5133 		if (cap) {
       
  5134 			pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
       
  5135 					      PCI_EXP_DEVCTL_NOSNOOP_EN);
       
  5136 		}
       
  5137 	}
       
  5138 
       
  5139 	RTL_W8(Cfg9346, Cfg9346_Unlock);
       
  5140 
       
  5141 	switch (tp->mac_version) {
       
  5142 	case RTL_GIGA_MAC_VER_07:
       
  5143 		rtl_hw_start_8102e_1(ioaddr, pdev);
       
  5144 		break;
       
  5145 
       
  5146 	case RTL_GIGA_MAC_VER_08:
       
  5147 		rtl_hw_start_8102e_3(ioaddr, pdev);
       
  5148 		break;
       
  5149 
       
  5150 	case RTL_GIGA_MAC_VER_09:
       
  5151 		rtl_hw_start_8102e_2(ioaddr, pdev);
       
  5152 		break;
       
  5153 
       
  5154 	case RTL_GIGA_MAC_VER_29:
       
  5155 		rtl_hw_start_8105e_1(ioaddr, pdev);
       
  5156 		break;
       
  5157 	case RTL_GIGA_MAC_VER_30:
       
  5158 		rtl_hw_start_8105e_2(ioaddr, pdev);
       
  5159 		break;
       
  5160 	}
       
  5161 
       
  5162 	RTL_W8(Cfg9346, Cfg9346_Lock);
       
  5163 
       
  5164 	RTL_W8(MaxTxPacketSize, TxPacketMax);
       
  5165 
       
  5166 	rtl_set_rx_max_size(ioaddr, rx_buf_sz);
       
  5167 
       
  5168 	tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
       
  5169 	RTL_W16(CPlusCmd, tp->cp_cmd);
       
  5170 
       
  5171 	RTL_W16(IntrMitigate, 0x0000);
       
  5172 
       
  5173 	rtl_set_rx_tx_desc_registers(tp, ioaddr);
       
  5174 
       
  5175 	RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
       
  5176 	rtl_set_rx_tx_config_registers(tp);
       
  5177 
       
  5178 	RTL_R8(IntrMask);
       
  5179 
       
  5180 	rtl_set_rx_mode(dev);
       
  5181 
       
  5182 	RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
       
  5183 
       
  5184 	if (!tp->ecdev)
       
  5185 		RTL_W16(IntrMask, tp->intr_event);
       
  5186 }
       
  5187 
       
  5188 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
       
  5189 {
       
  5190 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5191 
       
  5192 	if (new_mtu < ETH_ZLEN ||
       
  5193 	    new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
       
  5194 		return -EINVAL;
       
  5195 
       
  5196 	if (new_mtu > ETH_DATA_LEN)
       
  5197 		rtl_hw_jumbo_enable(tp);
       
  5198 	else
       
  5199 		rtl_hw_jumbo_disable(tp);
       
  5200 
       
  5201 	dev->mtu = new_mtu;
       
  5202 	netdev_update_features(dev);
       
  5203 
       
  5204 	return 0;
       
  5205 }
       
  5206 
       
  5207 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
       
  5208 {
       
  5209 	desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
       
  5210 	desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
       
  5211 }
       
  5212 
       
  5213 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
       
  5214 				     void **data_buff, struct RxDesc *desc)
       
  5215 {
       
  5216 	dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
       
  5217 			 DMA_FROM_DEVICE);
       
  5218 
       
  5219 	kfree(*data_buff);
       
  5220 	*data_buff = NULL;
       
  5221 	rtl8169_make_unusable_by_asic(desc);
       
  5222 }
       
  5223 
       
  5224 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
       
  5225 {
       
  5226 	u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
       
  5227 
       
  5228 	desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
       
  5229 }
       
  5230 
       
  5231 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
       
  5232 				       u32 rx_buf_sz)
       
  5233 {
       
  5234 	desc->addr = cpu_to_le64(mapping);
       
  5235 	wmb();
       
  5236 	rtl8169_mark_to_asic(desc, rx_buf_sz);
       
  5237 }
       
  5238 
       
  5239 static inline void *rtl8169_align(void *data)
       
  5240 {
       
  5241 	return (void *)ALIGN((long)data, 16);
       
  5242 }
       
  5243 
       
  5244 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
       
  5245 					     struct RxDesc *desc)
       
  5246 {
       
  5247 	void *data;
       
  5248 	dma_addr_t mapping;
       
  5249 	struct device *d = &tp->pci_dev->dev;
       
  5250 	struct net_device *dev = tp->dev;
       
  5251 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
       
  5252 
       
  5253 	data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
       
  5254 	if (!data)
       
  5255 		return NULL;
       
  5256 
       
  5257 	if (rtl8169_align(data) != data) {
       
  5258 		kfree(data);
       
  5259 		data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
       
  5260 		if (!data)
       
  5261 			return NULL;
       
  5262 	}
       
  5263 
       
  5264 	mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
       
  5265 				 DMA_FROM_DEVICE);
       
  5266 	if (unlikely(dma_mapping_error(d, mapping))) {
       
  5267 		if (net_ratelimit())
       
  5268 			netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
       
  5269 		goto err_out;
       
  5270 	}
       
  5271 
       
  5272 	rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
       
  5273 	return data;
       
  5274 
       
  5275 err_out:
       
  5276 	kfree(data);
       
  5277 	return NULL;
       
  5278 }
       
  5279 
       
  5280 static void rtl8169_rx_clear(struct rtl8169_private *tp)
       
  5281 {
       
  5282 	unsigned int i;
       
  5283 
       
  5284 	for (i = 0; i < NUM_RX_DESC; i++) {
       
  5285 		if (tp->Rx_databuff[i]) {
       
  5286 			rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
       
  5287 					    tp->RxDescArray + i);
       
  5288 		}
       
  5289 	}
       
  5290 }
       
  5291 
       
  5292 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
       
  5293 {
       
  5294 	desc->opts1 |= cpu_to_le32(RingEnd);
       
  5295 }
       
  5296 
       
  5297 static int rtl8169_rx_fill(struct rtl8169_private *tp)
       
  5298 {
       
  5299 	unsigned int i;
       
  5300 
       
  5301 	for (i = 0; i < NUM_RX_DESC; i++) {
       
  5302 		void *data;
       
  5303 
       
  5304 		if (tp->Rx_databuff[i])
       
  5305 			continue;
       
  5306 
       
  5307 		data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
       
  5308 		if (!data) {
       
  5309 			rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
       
  5310 			goto err_out;
       
  5311 		}
       
  5312 		tp->Rx_databuff[i] = data;
       
  5313 	}
       
  5314 
       
  5315 	rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
       
  5316 	return 0;
       
  5317 
       
  5318 err_out:
       
  5319 	rtl8169_rx_clear(tp);
       
  5320 	return -ENOMEM;
       
  5321 }
       
  5322 
       
  5323 static int rtl8169_init_ring(struct net_device *dev)
       
  5324 {
       
  5325 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5326 
       
  5327 	rtl8169_init_ring_indexes(tp);
       
  5328 
       
  5329 	memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
       
  5330 	memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
       
  5331 
       
  5332 	return rtl8169_rx_fill(tp);
       
  5333 }
       
  5334 
       
  5335 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
       
  5336 				 struct TxDesc *desc)
       
  5337 {
       
  5338 	unsigned int len = tx_skb->len;
       
  5339 
       
  5340 	dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
       
  5341 
       
  5342 	desc->opts1 = 0x00;
       
  5343 	desc->opts2 = 0x00;
       
  5344 	desc->addr = 0x00;
       
  5345 	tx_skb->len = 0;
       
  5346 }
       
  5347 
       
  5348 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
       
  5349 				   unsigned int n)
       
  5350 {
       
  5351 	unsigned int i;
       
  5352 
       
  5353 	for (i = 0; i < n; i++) {
       
  5354 		unsigned int entry = (start + i) % NUM_TX_DESC;
       
  5355 		struct ring_info *tx_skb = tp->tx_skb + entry;
       
  5356 		unsigned int len = tx_skb->len;
       
  5357 
       
  5358 		if (len) {
       
  5359 			struct sk_buff *skb = tx_skb->skb;
       
  5360 
       
  5361 			rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
       
  5362 					     tp->TxDescArray + entry);
       
  5363 			if (skb) {
       
  5364 				tp->dev->stats.tx_dropped++;
       
  5365 				if (!tp->ecdev)
       
  5366 					dev_kfree_skb(skb);
       
  5367 				tx_skb->skb = NULL;
       
  5368 			}
       
  5369 		}
       
  5370 	}
       
  5371 }
       
  5372 
       
  5373 static void rtl8169_tx_clear(struct rtl8169_private *tp)
       
  5374 {
       
  5375 	rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
       
  5376 	tp->cur_tx = tp->dirty_tx = 0;
       
  5377 }
       
  5378 
       
  5379 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
       
  5380 {
       
  5381 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5382 
       
  5383 	PREPARE_DELAYED_WORK(&tp->task, task);
       
  5384 	schedule_delayed_work(&tp->task, 4);
       
  5385 }
       
  5386 
       
  5387 static void rtl8169_wait_for_quiescence(struct net_device *dev)
       
  5388 {
       
  5389 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5390 	void __iomem *ioaddr = tp->mmio_addr;
       
  5391 
       
  5392 	synchronize_irq(dev->irq);
       
  5393 
       
  5394 	/* Wait for any pending NAPI task to complete */
       
  5395 	napi_disable(&tp->napi);
       
  5396 
       
  5397 	rtl8169_irq_mask_and_ack(tp);
       
  5398 
       
  5399 	tp->intr_mask = 0xffff;
       
  5400 	RTL_W16(IntrMask, tp->intr_event);
       
  5401 	napi_enable(&tp->napi);
       
  5402 }
       
  5403 
       
  5404 static void rtl8169_reinit_task(struct work_struct *work)
       
  5405 {
       
  5406 	struct rtl8169_private *tp =
       
  5407 		container_of(work, struct rtl8169_private, task.work);
       
  5408 	struct net_device *dev = tp->dev;
       
  5409 	int ret;
       
  5410 
       
  5411 	rtnl_lock();
       
  5412 
       
  5413 	if (!netif_running(dev))
       
  5414 		goto out_unlock;
       
  5415 
       
  5416 	rtl8169_wait_for_quiescence(dev);
       
  5417 	rtl8169_close(dev);
       
  5418 
       
  5419 	ret = rtl8169_open(dev);
       
  5420 	if (unlikely(ret < 0)) {
       
  5421 		if (net_ratelimit())
       
  5422 			netif_err(tp, drv, dev,
       
  5423 				  "reinit failure (status = %d). Rescheduling\n",
       
  5424 				  ret);
       
  5425 		rtl8169_schedule_work(dev, rtl8169_reinit_task);
       
  5426 	}
       
  5427 
       
  5428 out_unlock:
       
  5429 	rtnl_unlock();
       
  5430 }
       
  5431 
       
  5432 static void rtl8169_reset_task(struct work_struct *work)
       
  5433 {
       
  5434 	struct rtl8169_private *tp =
       
  5435 		container_of(work, struct rtl8169_private, task.work);
       
  5436 	struct net_device *dev = tp->dev;
       
  5437 	int i;
       
  5438 
       
  5439 	rtnl_lock();
       
  5440 
       
  5441 	if (!netif_running(dev))
       
  5442 		goto out_unlock;
       
  5443 
       
  5444 	rtl8169_hw_reset(tp);
       
  5445 
       
  5446 	rtl8169_wait_for_quiescence(dev);
       
  5447 
       
  5448 	for (i = 0; i < NUM_RX_DESC; i++)
       
  5449 		rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
       
  5450 
       
  5451 	rtl8169_tx_clear(tp);
       
  5452 	rtl8169_init_ring_indexes(tp);
       
  5453 
       
  5454 	rtl_hw_start(dev);
       
  5455 	netif_wake_queue(dev);
       
  5456 	rtl8169_check_link_status(dev, tp, tp->mmio_addr);
       
  5457 
       
  5458 out_unlock:
       
  5459 	rtnl_unlock();
       
  5460 }
       
  5461 
       
  5462 static void rtl8169_tx_timeout(struct net_device *dev)
       
  5463 {
       
  5464 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5465 	if (tp->ecdev)
       
  5466 		return;
       
  5467 
       
  5468 	rtl8169_schedule_work(dev, rtl8169_reset_task);
       
  5469 }
       
  5470 
       
  5471 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
       
  5472 			      u32 *opts)
       
  5473 {
       
  5474 	struct skb_shared_info *info = skb_shinfo(skb);
       
  5475 	unsigned int cur_frag, entry;
       
  5476 	struct TxDesc * uninitialized_var(txd);
       
  5477 	struct device *d = &tp->pci_dev->dev;
       
  5478 
       
  5479 	entry = tp->cur_tx;
       
  5480 	for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
       
  5481 		const skb_frag_t *frag = info->frags + cur_frag;
       
  5482 		dma_addr_t mapping;
       
  5483 		u32 status, len;
       
  5484 		void *addr;
       
  5485 
       
  5486 		entry = (entry + 1) % NUM_TX_DESC;
       
  5487 
       
  5488 		txd = tp->TxDescArray + entry;
       
  5489 		len = skb_frag_size(frag);
       
  5490 		addr = skb_frag_address(frag);
       
  5491 		mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
       
  5492 		if (unlikely(dma_mapping_error(d, mapping))) {
       
  5493 			if (net_ratelimit())
       
  5494 				netif_err(tp, drv, tp->dev,
       
  5495 					  "Failed to map TX fragments DMA!\n");
       
  5496 			goto err_out;
       
  5497 		}
       
  5498 
       
  5499 		/* Anti gcc 2.95.3 bugware (sic) */
       
  5500 		status = opts[0] | len |
       
  5501 			(RingEnd * !((entry + 1) % NUM_TX_DESC));
       
  5502 
       
  5503 		txd->opts1 = cpu_to_le32(status);
       
  5504 		txd->opts2 = cpu_to_le32(opts[1]);
       
  5505 		txd->addr = cpu_to_le64(mapping);
       
  5506 
       
  5507 		tp->tx_skb[entry].len = len;
       
  5508 	}
       
  5509 
       
  5510 	if (cur_frag) {
       
  5511 		tp->tx_skb[entry].skb = skb;
       
  5512 		txd->opts1 |= cpu_to_le32(LastFrag);
       
  5513 	}
       
  5514 
       
  5515 	return cur_frag;
       
  5516 
       
  5517 err_out:
       
  5518 	rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
       
  5519 	return -EIO;
       
  5520 }
       
  5521 
       
  5522 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
       
  5523 				    struct sk_buff *skb, u32 *opts)
       
  5524 {
       
  5525 	const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
       
  5526 	u32 mss = skb_shinfo(skb)->gso_size;
       
  5527 	int offset = info->opts_offset;
       
  5528 
       
  5529 	if (mss) {
       
  5530 		opts[0] |= TD_LSO;
       
  5531 		opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
       
  5532 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
       
  5533 		const struct iphdr *ip = ip_hdr(skb);
       
  5534 
       
  5535 		if (ip->protocol == IPPROTO_TCP)
       
  5536 			opts[offset] |= info->checksum.tcp;
       
  5537 		else if (ip->protocol == IPPROTO_UDP)
       
  5538 			opts[offset] |= info->checksum.udp;
       
  5539 		else
       
  5540 			WARN_ON_ONCE(1);
       
  5541 	}
       
  5542 }
       
  5543 
       
  5544 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
       
  5545 				      struct net_device *dev)
       
  5546 {
       
  5547 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5548 	unsigned int entry = tp->cur_tx % NUM_TX_DESC;
       
  5549 	struct TxDesc *txd = tp->TxDescArray + entry;
       
  5550 	void __iomem *ioaddr = tp->mmio_addr;
       
  5551 	struct device *d = &tp->pci_dev->dev;
       
  5552 	dma_addr_t mapping;
       
  5553 	u32 status, len;
       
  5554 	u32 opts[2];
       
  5555 	int frags;
       
  5556 
       
  5557 	if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
       
  5558 		netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
       
  5559 		goto err_stop_0;
       
  5560 	}
       
  5561 
       
  5562 	if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
       
  5563 		goto err_stop_0;
       
  5564 
       
  5565 	len = skb_headlen(skb);
       
  5566 	mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
       
  5567 	if (unlikely(dma_mapping_error(d, mapping))) {
       
  5568 		if (net_ratelimit())
       
  5569 			netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
       
  5570 		goto err_dma_0;
       
  5571 	}
       
  5572 
       
  5573 	tp->tx_skb[entry].len = len;
       
  5574 	txd->addr = cpu_to_le64(mapping);
       
  5575 
       
  5576 	opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
       
  5577 	opts[0] = DescOwn;
       
  5578 
       
  5579 	rtl8169_tso_csum(tp, skb, opts);
       
  5580 
       
  5581 	frags = rtl8169_xmit_frags(tp, skb, opts);
       
  5582 	if (frags < 0)
       
  5583 		goto err_dma_1;
       
  5584 	else if (frags)
       
  5585 		opts[0] |= FirstFrag;
       
  5586 	else {
       
  5587 		opts[0] |= FirstFrag | LastFrag;
       
  5588 		tp->tx_skb[entry].skb = skb;
       
  5589 	}
       
  5590 
       
  5591 	txd->opts2 = cpu_to_le32(opts[1]);
       
  5592 
       
  5593 	wmb();
       
  5594 
       
  5595 	/* Anti gcc 2.95.3 bugware (sic) */
       
  5596 	status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
       
  5597 	txd->opts1 = cpu_to_le32(status);
       
  5598 
       
  5599 	tp->cur_tx += frags + 1;
       
  5600 
       
  5601 	wmb();
       
  5602 
       
  5603 	RTL_W8(TxPoll, NPQ);
       
  5604 
       
  5605 	if (!tp->ecdev && TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
       
  5606 		netif_stop_queue(dev);
       
  5607 		smp_rmb();
       
  5608 		if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
       
  5609 			netif_wake_queue(dev);
       
  5610 	}
       
  5611 
       
  5612 	return NETDEV_TX_OK;
       
  5613 
       
  5614 err_dma_1:
       
  5615 	rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
       
  5616 err_dma_0:
       
  5617 	if (!tp->ecdev)
       
  5618 	  dev_kfree_skb(skb);
       
  5619 	dev->stats.tx_dropped++;
       
  5620 	return NETDEV_TX_OK;
       
  5621 
       
  5622 err_stop_0:
       
  5623 	if (!tp->ecdev)
       
  5624 		netif_stop_queue(dev);
       
  5625 	dev->stats.tx_dropped++;
       
  5626 	return NETDEV_TX_BUSY;
       
  5627 }
       
  5628 
       
  5629 static void rtl8169_pcierr_interrupt(struct net_device *dev)
       
  5630 {
       
  5631 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5632 	struct pci_dev *pdev = tp->pci_dev;
       
  5633 	u16 pci_status, pci_cmd;
       
  5634 
       
  5635 	pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
       
  5636 	pci_read_config_word(pdev, PCI_STATUS, &pci_status);
       
  5637 
       
  5638 	netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
       
  5639 		  pci_cmd, pci_status);
       
  5640 
       
  5641 	/*
       
  5642 	 * The recovery sequence below admits a very elaborated explanation:
       
  5643 	 * - it seems to work;
       
  5644 	 * - I did not see what else could be done;
       
  5645 	 * - it makes iop3xx happy.
       
  5646 	 *
       
  5647 	 * Feel free to adjust to your needs.
       
  5648 	 */
       
  5649 	if (pdev->broken_parity_status)
       
  5650 		pci_cmd &= ~PCI_COMMAND_PARITY;
       
  5651 	else
       
  5652 		pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
       
  5653 
       
  5654 	pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
       
  5655 
       
  5656 	pci_write_config_word(pdev, PCI_STATUS,
       
  5657 		pci_status & (PCI_STATUS_DETECTED_PARITY |
       
  5658 		PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
       
  5659 		PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
       
  5660 
       
  5661 	/* The infamous DAC f*ckup only happens at boot time */
       
  5662 	if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
       
  5663 		void __iomem *ioaddr = tp->mmio_addr;
       
  5664 
       
  5665 		netif_info(tp, intr, dev, "disabling PCI DAC\n");
       
  5666 		tp->cp_cmd &= ~PCIDAC;
       
  5667 		RTL_W16(CPlusCmd, tp->cp_cmd);
       
  5668 		dev->features &= ~NETIF_F_HIGHDMA;
       
  5669 	}
       
  5670 
       
  5671 	rtl8169_hw_reset(tp);
       
  5672 
       
  5673 	rtl8169_schedule_work(dev, rtl8169_reinit_task);
       
  5674 }
       
  5675 
       
  5676 static void rtl8169_tx_interrupt(struct net_device *dev,
       
  5677 				 struct rtl8169_private *tp,
       
  5678 				 void __iomem *ioaddr)
       
  5679 {
       
  5680 	unsigned int dirty_tx, tx_left;
       
  5681 
       
  5682 	dirty_tx = tp->dirty_tx;
       
  5683 	smp_rmb();
       
  5684 	tx_left = tp->cur_tx - dirty_tx;
       
  5685 
       
  5686 	while (tx_left > 0) {
       
  5687 		unsigned int entry = dirty_tx % NUM_TX_DESC;
       
  5688 		struct ring_info *tx_skb = tp->tx_skb + entry;
       
  5689 		u32 status;
       
  5690 
       
  5691 		rmb();
       
  5692 		status = le32_to_cpu(tp->TxDescArray[entry].opts1);
       
  5693 		if (status & DescOwn)
       
  5694 			break;
       
  5695 
       
  5696 		rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
       
  5697 				     tp->TxDescArray + entry);
       
  5698 		if (status & LastFrag) {
       
  5699 			dev->stats.tx_packets++;
       
  5700 			dev->stats.tx_bytes += tx_skb->skb->len;
       
  5701 			if (!tp->ecdev)
       
  5702 				dev_kfree_skb(tx_skb->skb);
       
  5703 			tx_skb->skb = NULL;
       
  5704 		}
       
  5705 		dirty_tx++;
       
  5706 		tx_left--;
       
  5707 	}
       
  5708 
       
  5709 	if (tp->dirty_tx != dirty_tx) {
       
  5710 		tp->dirty_tx = dirty_tx;
       
  5711 		smp_wmb();
       
  5712 		if (!tp->ecdev && netif_queue_stopped(dev) &&
       
  5713 		    (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
       
  5714 			netif_wake_queue(dev);
       
  5715 		}
       
  5716 		/*
       
  5717 		 * 8168 hack: TxPoll requests are lost when the Tx packets are
       
  5718 		 * too close. Let's kick an extra TxPoll request when a burst
       
  5719 		 * of start_xmit activity is detected (if it is not detected,
       
  5720 		 * it is slow enough). -- FR
       
  5721 		 */
       
  5722 		smp_rmb();
       
  5723 		if (tp->cur_tx != dirty_tx)
       
  5724 			RTL_W8(TxPoll, NPQ);
       
  5725 	}
       
  5726 }
       
  5727 
       
  5728 static inline int rtl8169_fragmented_frame(u32 status)
       
  5729 {
       
  5730 	return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
       
  5731 }
       
  5732 
       
  5733 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
       
  5734 {
       
  5735 	u32 status = opts1 & RxProtoMask;
       
  5736 
       
  5737 	if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
       
  5738 	    ((status == RxProtoUDP) && !(opts1 & UDPFail)))
       
  5739 		skb->ip_summed = CHECKSUM_UNNECESSARY;
       
  5740 	else
       
  5741 		skb_checksum_none_assert(skb);
       
  5742 }
       
  5743 
       
  5744 static struct sk_buff *rtl8169_try_rx_copy(void *data,
       
  5745 					   struct rtl8169_private *tp,
       
  5746 					   int pkt_size,
       
  5747 					   dma_addr_t addr)
       
  5748 {
       
  5749 	struct sk_buff *skb;
       
  5750 	struct device *d = &tp->pci_dev->dev;
       
  5751 
       
  5752 	data = rtl8169_align(data);
       
  5753 	dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
       
  5754 	prefetch(data);
       
  5755 	skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
       
  5756 	if (skb)
       
  5757 		memcpy(skb->data, data, pkt_size);
       
  5758 	dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
       
  5759 
       
  5760 	return skb;
       
  5761 }
       
  5762 
       
  5763 static int rtl8169_rx_interrupt(struct net_device *dev,
       
  5764 				struct rtl8169_private *tp,
       
  5765 				void __iomem *ioaddr, u32 budget)
       
  5766 {
       
  5767 	unsigned int cur_rx, rx_left;
       
  5768 	unsigned int count;
       
  5769 
       
  5770 	cur_rx = tp->cur_rx;
       
  5771 	rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
       
  5772 	rx_left = min(rx_left, budget);
       
  5773 
       
  5774 	for (; rx_left > 0; rx_left--, cur_rx++) {
       
  5775 		unsigned int entry = cur_rx % NUM_RX_DESC;
       
  5776 		struct RxDesc *desc = tp->RxDescArray + entry;
       
  5777 		u32 status;
       
  5778 
       
  5779 		rmb();
       
  5780 		status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
       
  5781 
       
  5782 		if (status & DescOwn)
       
  5783 			break;
       
  5784 		if (unlikely(status & RxRES)) {
       
  5785 			netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
       
  5786 				   status);
       
  5787 			dev->stats.rx_errors++;
       
  5788 			if (status & (RxRWT | RxRUNT))
       
  5789 				dev->stats.rx_length_errors++;
       
  5790 			if (status & RxCRC)
       
  5791 				dev->stats.rx_crc_errors++;
       
  5792 			if (status & RxFOVF) {
       
  5793 				if (!tp->ecdev)
       
  5794 					rtl8169_schedule_work(dev, rtl8169_reset_task);
       
  5795 				dev->stats.rx_fifo_errors++;
       
  5796 			}
       
  5797 			rtl8169_mark_to_asic(desc, rx_buf_sz);
       
  5798 		} else {
       
  5799 			struct sk_buff *skb;
       
  5800 			dma_addr_t addr = le64_to_cpu(desc->addr);
       
  5801 			int pkt_size = (status & 0x00003fff) - 4;
       
  5802 
       
  5803 			/*
       
  5804 			 * The driver does not support incoming fragmented
       
  5805 			 * frames. They are seen as a symptom of over-mtu
       
  5806 			 * sized frames.
       
  5807 			 */
       
  5808 			if (unlikely(rtl8169_fragmented_frame(status))) {
       
  5809 				dev->stats.rx_dropped++;
       
  5810 				dev->stats.rx_length_errors++;
       
  5811 				rtl8169_mark_to_asic(desc, rx_buf_sz);
       
  5812 				continue;
       
  5813 			}
       
  5814 
       
  5815 			if (tp->ecdev) {
       
  5816 			    struct device *d = &tp->pci_dev->dev;
       
  5817 
       
  5818 			    /* reusing parts of rtl8169_try_rx_copy() */
       
  5819 			    tp->Rx_databuff[entry] = rtl8169_align(tp->Rx_databuff[entry]);
       
  5820 			    dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
       
  5821 			    prefetch(tp->Rx_databuff[entry]);
       
  5822 
       
  5823 			    ecdev_receive(tp->ecdev, tp->Rx_databuff[entry], pkt_size);
       
  5824 
       
  5825 			    dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
       
  5826 			    rtl8169_mark_to_asic(desc, rx_buf_sz);
       
  5827 
       
  5828 			    rtl8169_rx_csum(tp->Rx_databuff[entry], status);
       
  5829 
       
  5830 			    // No need to detect link status as
       
  5831 			    // long as frames are received: Reset watchdog.
       
  5832 			    tp->ec_watchdog_jiffies = jiffies;
       
  5833 			} else {
       
  5834 			    skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
       
  5835 						      tp, pkt_size, addr);
       
  5836 			    rtl8169_mark_to_asic(desc, rx_buf_sz);
       
  5837 			    if (!skb) {
       
  5838 				    dev->stats.rx_dropped++;
       
  5839 				    continue;
       
  5840 			    }
       
  5841 
       
  5842 			    rtl8169_rx_csum(skb, status);
       
  5843 			    skb_put(skb, pkt_size);
       
  5844 			    skb->protocol = eth_type_trans(skb, dev);
       
  5845 
       
  5846 			    rtl8169_rx_vlan_tag(desc, skb);
       
  5847 
       
  5848 			    napi_gro_receive(&tp->napi, skb);
       
  5849 
       
  5850 			}
       
  5851 
       
  5852 			dev->stats.rx_bytes += pkt_size;
       
  5853 			dev->stats.rx_packets++;
       
  5854 		}
       
  5855 
       
  5856 		/* Work around for AMD plateform. */
       
  5857 		if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
       
  5858 		    (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
       
  5859 			desc->opts2 = 0;
       
  5860 			cur_rx++;
       
  5861 		}
       
  5862 	}
       
  5863 
       
  5864 	count = cur_rx - tp->cur_rx;
       
  5865 	tp->cur_rx = cur_rx;
       
  5866 
       
  5867 	tp->dirty_rx += count;
       
  5868 
       
  5869 	return count;
       
  5870 }
       
  5871 
       
  5872 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
       
  5873 {
       
  5874 	struct net_device *dev = dev_instance;
       
  5875 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5876 	void __iomem *ioaddr = tp->mmio_addr;
       
  5877 	int handled = 0;
       
  5878 	int status;
       
  5879 
       
  5880 	/* loop handling interrupts until we have no new ones or
       
  5881 	 * we hit a invalid/hotplug case.
       
  5882 	 */
       
  5883 	status = RTL_R16(IntrStatus);
       
  5884 	while (status && status != 0xffff) {
       
  5885 		status &= tp->intr_event;
       
  5886 		if (!status)
       
  5887 			break;
       
  5888 
       
  5889 		handled = 1;
       
  5890 
       
  5891 		/* Handle all of the error cases first. These will reset
       
  5892 		 * the chip, so just exit the loop.
       
  5893 		 */
       
  5894 		if (unlikely(!tp->ecdev && !netif_running(dev))) {
       
  5895 			rtl8169_hw_reset(tp);
       
  5896 			break;
       
  5897 		}
       
  5898 
       
  5899 		if (unlikely(!tp->ecdev && (status & RxFIFOOver))) {
       
  5900 			switch (tp->mac_version) {
       
  5901 			/* Work around for rx fifo overflow */
       
  5902 			case RTL_GIGA_MAC_VER_11:
       
  5903 				netif_stop_queue(dev);
       
  5904 				rtl8169_tx_timeout(dev);
       
  5905 				goto done;
       
  5906 			default:
       
  5907 				break;
       
  5908 			}
       
  5909 		}
       
  5910 
       
  5911 		if (unlikely(status & SYSErr)) {
       
  5912 			rtl8169_pcierr_interrupt(dev);
       
  5913 			break;
       
  5914 		}
       
  5915 
       
  5916 		if (status & LinkChg)
       
  5917 			__rtl8169_check_link_status(dev, tp, ioaddr, true);
       
  5918 
       
  5919 		/* We need to see the lastest version of tp->intr_mask to
       
  5920 		 * avoid ignoring an MSI interrupt and having to wait for
       
  5921 		 * another event which may never come.
       
  5922 		 */
       
  5923 		smp_rmb();
       
  5924 		if (status & tp->intr_mask & tp->napi_event) {
       
  5925 			RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
       
  5926 			tp->intr_mask = ~tp->napi_event;
       
  5927 
       
  5928 			if (likely(napi_schedule_prep(&tp->napi)))
       
  5929 				__napi_schedule(&tp->napi);
       
  5930 			else
       
  5931 				netif_info(tp, intr, dev,
       
  5932 					   "interrupt %04x in poll\n", status);
       
  5933 		}
       
  5934 
       
  5935 		/* We only get a new MSI interrupt when all active irq
       
  5936 		 * sources on the chip have been acknowledged. So, ack
       
  5937 		 * everything we've seen and check if new sources have become
       
  5938 		 * active to avoid blocking all interrupts from the chip.
       
  5939 		 */
       
  5940 		RTL_W16(IntrStatus,
       
  5941 			(status & RxFIFOOver) ? (status | RxOverflow) : status);
       
  5942 		status = RTL_R16(IntrStatus);
       
  5943 	}
       
  5944 done:
       
  5945 	return IRQ_RETVAL(handled);
       
  5946 }
       
  5947 
       
  5948 static void ec_poll(struct net_device *dev)
       
  5949 {
       
  5950 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5951 	struct pci_dev *pdev = tp->pci_dev;
       
  5952 
       
  5953 	rtl8169_interrupt(pdev->irq, dev);
       
  5954 	rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, 100); // FIXME
       
  5955 	rtl8169_tx_interrupt(dev, tp, tp->mmio_addr);
       
  5956 
       
  5957 	if (jiffies - tp->ec_watchdog_jiffies >= 2 * HZ) {
       
  5958 		rtl8169_phy_timer((unsigned long) dev);
       
  5959 		tp->ec_watchdog_jiffies = jiffies;
       
  5960 	}
       
  5961 }
       
  5962 
       
  5963 static int rtl8169_poll(struct napi_struct *napi, int budget)
       
  5964 {
       
  5965 	struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
       
  5966 	struct net_device *dev = tp->dev;
       
  5967 	void __iomem *ioaddr = tp->mmio_addr;
       
  5968 	int work_done;
       
  5969 
       
  5970 	work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
       
  5971 	rtl8169_tx_interrupt(dev, tp, ioaddr);
       
  5972 
       
  5973 	if (work_done < budget) {
       
  5974 		napi_complete(napi);
       
  5975 
       
  5976 		/* We need for force the visibility of tp->intr_mask
       
  5977 		 * for other CPUs, as we can loose an MSI interrupt
       
  5978 		 * and potentially wait for a retransmit timeout if we don't.
       
  5979 		 * The posted write to IntrMask is safe, as it will
       
  5980 		 * eventually make it to the chip and we won't loose anything
       
  5981 		 * until it does.
       
  5982 		 */
       
  5983 		tp->intr_mask = 0xffff;
       
  5984 		wmb();
       
  5985 		RTL_W16(IntrMask, tp->intr_event);
       
  5986 	}
       
  5987 
       
  5988 	return work_done;
       
  5989 }
       
  5990 
       
  5991 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
       
  5992 {
       
  5993 	struct rtl8169_private *tp = netdev_priv(dev);
       
  5994 
       
  5995 	if (tp->mac_version > RTL_GIGA_MAC_VER_06)
       
  5996 		return;
       
  5997 
       
  5998 	dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
       
  5999 	RTL_W32(RxMissed, 0);
       
  6000 }
       
  6001 
       
  6002 static void rtl8169_down(struct net_device *dev)
       
  6003 {
       
  6004 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6005 	void __iomem *ioaddr = tp->mmio_addr;
       
  6006 
       
  6007 	if (!tp->ecdev) {
       
  6008 	    del_timer_sync(&tp->timer);
       
  6009 
       
  6010 		netif_stop_queue(dev);
       
  6011 
       
  6012 		napi_disable(&tp->napi);
       
  6013 	}
       
  6014 
       
  6015 	if (!tp->ecdev) {
       
  6016 		spin_lock_irq(&tp->lock);
       
  6017 	}
       
  6018 
       
  6019 	rtl8169_hw_reset(tp);
       
  6020 	/*
       
  6021 	 * At this point device interrupts can not be enabled in any function,
       
  6022 	 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
       
  6023 	 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
       
  6024 	 */
       
  6025 	rtl8169_rx_missed(dev, ioaddr);
       
  6026 
       
  6027 	if (!tp->ecdev) {
       
  6028 		spin_unlock_irq(&tp->lock);
       
  6029 
       
  6030 		synchronize_irq(dev->irq);
       
  6031 	}
       
  6032 
       
  6033 	/* Give a racing hard_start_xmit a few cycles to complete. */
       
  6034 	synchronize_sched();  /* FIXME: should this be synchronize_irq()? */
       
  6035 
       
  6036 	rtl8169_tx_clear(tp);
       
  6037 
       
  6038 	rtl8169_rx_clear(tp);
       
  6039 
       
  6040 	rtl_pll_power_down(tp);
       
  6041 }
       
  6042 
       
  6043 static int rtl8169_close(struct net_device *dev)
       
  6044 {
       
  6045 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6046 	struct pci_dev *pdev = tp->pci_dev;
       
  6047 
       
  6048 	pm_runtime_get_sync(&pdev->dev);
       
  6049 
       
  6050 	/* Update counters before going down */
       
  6051 	rtl8169_update_counters(dev);
       
  6052 
       
  6053 	rtl8169_down(dev);
       
  6054 
       
  6055 	if (!tp->ecdev)
       
  6056 		free_irq(dev->irq, dev);
       
  6057 
       
  6058 	dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
       
  6059 			  tp->RxPhyAddr);
       
  6060 	dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
       
  6061 			  tp->TxPhyAddr);
       
  6062 	tp->TxDescArray = NULL;
       
  6063 	tp->RxDescArray = NULL;
       
  6064 
       
  6065 	pm_runtime_put_sync(&pdev->dev);
       
  6066 
       
  6067 	return 0;
       
  6068 }
       
  6069 
       
  6070 static void rtl_set_rx_mode(struct net_device *dev)
       
  6071 {
       
  6072 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6073 	void __iomem *ioaddr = tp->mmio_addr;
       
  6074 	unsigned long flags;
       
  6075 	u32 mc_filter[2];	/* Multicast hash filter */
       
  6076 	int rx_mode;
       
  6077 	u32 tmp = 0;
       
  6078 
       
  6079 	if (dev->flags & IFF_PROMISC) {
       
  6080 		/* Unconditionally log net taps. */
       
  6081 		netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
       
  6082 		rx_mode =
       
  6083 		    AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
       
  6084 		    AcceptAllPhys;
       
  6085 		mc_filter[1] = mc_filter[0] = 0xffffffff;
       
  6086 	} else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
       
  6087 		   (dev->flags & IFF_ALLMULTI)) {
       
  6088 		/* Too many to filter perfectly -- accept all multicasts. */
       
  6089 		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
       
  6090 		mc_filter[1] = mc_filter[0] = 0xffffffff;
       
  6091 	} else {
       
  6092 		struct netdev_hw_addr *ha;
       
  6093 
       
  6094 		rx_mode = AcceptBroadcast | AcceptMyPhys;
       
  6095 		mc_filter[1] = mc_filter[0] = 0;
       
  6096 		netdev_for_each_mc_addr(ha, dev) {
       
  6097 			int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
       
  6098 			mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
       
  6099 			rx_mode |= AcceptMulticast;
       
  6100 		}
       
  6101 	}
       
  6102 
       
  6103 	spin_lock_irqsave(&tp->lock, flags);
       
  6104 
       
  6105 	tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
       
  6106 
       
  6107 	if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
       
  6108 		u32 data = mc_filter[0];
       
  6109 
       
  6110 		mc_filter[0] = swab32(mc_filter[1]);
       
  6111 		mc_filter[1] = swab32(data);
       
  6112 	}
       
  6113 
       
  6114 	RTL_W32(MAR0 + 4, mc_filter[1]);
       
  6115 	RTL_W32(MAR0 + 0, mc_filter[0]);
       
  6116 
       
  6117 	RTL_W32(RxConfig, tmp);
       
  6118 
       
  6119 	spin_unlock_irqrestore(&tp->lock, flags);
       
  6120 }
       
  6121 
       
  6122 /**
       
  6123  *  rtl8169_get_stats - Get rtl8169 read/write statistics
       
  6124  *  @dev: The Ethernet Device to get statistics for
       
  6125  *
       
  6126  *  Get TX/RX statistics for rtl8169
       
  6127  */
       
  6128 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
       
  6129 {
       
  6130 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6131 	void __iomem *ioaddr = tp->mmio_addr;
       
  6132 	unsigned long flags;
       
  6133 
       
  6134 	if (netif_running(dev)) {
       
  6135 		spin_lock_irqsave(&tp->lock, flags);
       
  6136 		rtl8169_rx_missed(dev, ioaddr);
       
  6137 		spin_unlock_irqrestore(&tp->lock, flags);
       
  6138 	}
       
  6139 
       
  6140 	return &dev->stats;
       
  6141 }
       
  6142 
       
  6143 static void rtl8169_net_suspend(struct net_device *dev)
       
  6144 {
       
  6145 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6146 
       
  6147 	if (!netif_running(dev))
       
  6148 		return;
       
  6149 
       
  6150 	rtl_pll_power_down(tp);
       
  6151 
       
  6152 	netif_device_detach(dev);
       
  6153 	netif_stop_queue(dev);
       
  6154 }
       
  6155 
       
  6156 #ifdef CONFIG_PM
       
  6157 
       
  6158 static int rtl8169_suspend(struct device *device)
       
  6159 {
       
  6160 	struct pci_dev *pdev = to_pci_dev(device);
       
  6161 	struct net_device *dev = pci_get_drvdata(pdev);
       
  6162 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6163 
       
  6164 	if (tp->ecdev)
       
  6165 		return -EBUSY;
       
  6166 
       
  6167 	rtl8169_net_suspend(dev);
       
  6168 
       
  6169 	return 0;
       
  6170 }
       
  6171 
       
  6172 static void __rtl8169_resume(struct net_device *dev)
       
  6173 {
       
  6174 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6175 
       
  6176 	netif_device_attach(dev);
       
  6177 
       
  6178 	rtl_pll_power_up(tp);
       
  6179 
       
  6180 	rtl8169_schedule_work(dev, rtl8169_reset_task);
       
  6181 }
       
  6182 
       
  6183 static int rtl8169_resume(struct device *device)
       
  6184 {
       
  6185 	struct pci_dev *pdev = to_pci_dev(device);
       
  6186 	struct net_device *dev = pci_get_drvdata(pdev);
       
  6187 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6188 
       
  6189 	rtl8169_init_phy(dev, tp);
       
  6190 
       
  6191 	if (tp->ecdev)
       
  6192 		return -EBUSY;
       
  6193 
       
  6194 	if (netif_running(dev))
       
  6195 		__rtl8169_resume(dev);
       
  6196 
       
  6197 	return 0;
       
  6198 }
       
  6199 
       
  6200 static int rtl8169_runtime_suspend(struct device *device)
       
  6201 {
       
  6202 	struct pci_dev *pdev = to_pci_dev(device);
       
  6203 	struct net_device *dev = pci_get_drvdata(pdev);
       
  6204 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6205 
       
  6206 	if (!tp->TxDescArray)
       
  6207 		return 0;
       
  6208 
       
  6209 	spin_lock_irq(&tp->lock);
       
  6210 	tp->saved_wolopts = __rtl8169_get_wol(tp);
       
  6211 	__rtl8169_set_wol(tp, WAKE_ANY);
       
  6212 	spin_unlock_irq(&tp->lock);
       
  6213 
       
  6214 	rtl8169_net_suspend(dev);
       
  6215 
       
  6216 	return 0;
       
  6217 }
       
  6218 
       
  6219 static int rtl8169_runtime_resume(struct device *device)
       
  6220 {
       
  6221 	struct pci_dev *pdev = to_pci_dev(device);
       
  6222 	struct net_device *dev = pci_get_drvdata(pdev);
       
  6223 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6224 
       
  6225 	if (!tp->TxDescArray)
       
  6226 		return 0;
       
  6227 
       
  6228 	spin_lock_irq(&tp->lock);
       
  6229 	__rtl8169_set_wol(tp, tp->saved_wolopts);
       
  6230 	tp->saved_wolopts = 0;
       
  6231 	spin_unlock_irq(&tp->lock);
       
  6232 
       
  6233 	rtl8169_init_phy(dev, tp);
       
  6234 
       
  6235 	__rtl8169_resume(dev);
       
  6236 
       
  6237 	return 0;
       
  6238 }
       
  6239 
       
  6240 static int rtl8169_runtime_idle(struct device *device)
       
  6241 {
       
  6242 	struct pci_dev *pdev = to_pci_dev(device);
       
  6243 	struct net_device *dev = pci_get_drvdata(pdev);
       
  6244 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6245 
       
  6246 	return tp->TxDescArray ? -EBUSY : 0;
       
  6247 }
       
  6248 
       
  6249 static const struct dev_pm_ops rtl8169_pm_ops = {
       
  6250 	.suspend		= rtl8169_suspend,
       
  6251 	.resume			= rtl8169_resume,
       
  6252 	.freeze			= rtl8169_suspend,
       
  6253 	.thaw			= rtl8169_resume,
       
  6254 	.poweroff		= rtl8169_suspend,
       
  6255 	.restore		= rtl8169_resume,
       
  6256 	.runtime_suspend	= rtl8169_runtime_suspend,
       
  6257 	.runtime_resume		= rtl8169_runtime_resume,
       
  6258 	.runtime_idle		= rtl8169_runtime_idle,
       
  6259 };
       
  6260 
       
  6261 #define RTL8169_PM_OPS	(&rtl8169_pm_ops)
       
  6262 
       
  6263 #else /* !CONFIG_PM */
       
  6264 
       
  6265 #define RTL8169_PM_OPS	NULL
       
  6266 
       
  6267 #endif /* !CONFIG_PM */
       
  6268 
       
  6269 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
       
  6270 {
       
  6271 	void __iomem *ioaddr = tp->mmio_addr;
       
  6272 
       
  6273 	/* WoL fails with 8168b when the receiver is disabled. */
       
  6274 	switch (tp->mac_version) {
       
  6275 	case RTL_GIGA_MAC_VER_11:
       
  6276 	case RTL_GIGA_MAC_VER_12:
       
  6277 	case RTL_GIGA_MAC_VER_17:
       
  6278 		pci_clear_master(tp->pci_dev);
       
  6279 
       
  6280 		RTL_W8(ChipCmd, CmdRxEnb);
       
  6281 		/* PCI commit */
       
  6282 		RTL_R8(ChipCmd);
       
  6283 		break;
       
  6284 	default:
       
  6285 		break;
       
  6286 	}
       
  6287 }
       
  6288 
       
  6289 static void rtl_shutdown(struct pci_dev *pdev)
       
  6290 {
       
  6291 	struct net_device *dev = pci_get_drvdata(pdev);
       
  6292 	struct rtl8169_private *tp = netdev_priv(dev);
       
  6293 
       
  6294 	rtl8169_net_suspend(dev);
       
  6295 
       
  6296 	/* Restore original MAC address */
       
  6297 	rtl_rar_set(tp, dev->perm_addr);
       
  6298 
       
  6299 	spin_lock_irq(&tp->lock);
       
  6300 
       
  6301 	rtl8169_hw_reset(tp);
       
  6302 
       
  6303 	spin_unlock_irq(&tp->lock);
       
  6304 
       
  6305 	if (system_state == SYSTEM_POWER_OFF) {
       
  6306 		if (__rtl8169_get_wol(tp) & WAKE_ANY) {
       
  6307 			rtl_wol_suspend_quirk(tp);
       
  6308 			rtl_wol_shutdown_quirk(tp);
       
  6309 		}
       
  6310 
       
  6311 		pci_wake_from_d3(pdev, true);
       
  6312 		pci_set_power_state(pdev, PCI_D3hot);
       
  6313 	}
       
  6314 }
       
  6315 
       
  6316 static struct pci_driver rtl8169_pci_driver = {
       
  6317 	.name		= MODULENAME,
       
  6318 	.id_table	= rtl8169_pci_tbl,
       
  6319 	.probe		= rtl8169_init_one,
       
  6320 	.remove		= __devexit_p(rtl8169_remove_one),
       
  6321 	.shutdown	= rtl_shutdown,
       
  6322 	.driver.pm	= RTL8169_PM_OPS,
       
  6323 };
       
  6324 
       
  6325 static int __init rtl8169_init_module(void)
       
  6326 {
       
  6327 	return pci_register_driver(&rtl8169_pci_driver);
       
  6328 }
       
  6329 
       
  6330 static void __exit rtl8169_cleanup_module(void)
       
  6331 {
       
  6332 	pci_unregister_driver(&rtl8169_pci_driver);
       
  6333 }
       
  6334 
       
  6335 module_init(rtl8169_init_module);
       
  6336 module_exit(rtl8169_cleanup_module);