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