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