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