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