xref: /linux/drivers/net/ethernet/realtek/r8169_main.c (revision 2696e451dfb07f92d0e995ef456bd9110a48806a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4  *
5  * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
6  * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
7  * Copyright (c) a lot of people too. Please respect their work.
8  *
9  * See MAINTAINERS file for support contact information.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/ethtool.h>
19 #include <linux/hwmon.h>
20 #include <linux/phy.h>
21 #include <linux/if_vlan.h>
22 #include <linux/in.h>
23 #include <linux/io.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/bitfield.h>
30 #include <linux/prefetch.h>
31 #include <linux/ipv6.h>
32 #include <linux/unaligned.h>
33 #include <net/ip6_checksum.h>
34 #include <net/netdev_queues.h>
35 
36 #include "r8169.h"
37 #include "r8169_firmware.h"
38 
39 #define FIRMWARE_8168D_1	"rtl_nic/rtl8168d-1.fw"
40 #define FIRMWARE_8168D_2	"rtl_nic/rtl8168d-2.fw"
41 #define FIRMWARE_8168E_1	"rtl_nic/rtl8168e-1.fw"
42 #define FIRMWARE_8168E_2	"rtl_nic/rtl8168e-2.fw"
43 #define FIRMWARE_8168E_3	"rtl_nic/rtl8168e-3.fw"
44 #define FIRMWARE_8168F_1	"rtl_nic/rtl8168f-1.fw"
45 #define FIRMWARE_8168F_2	"rtl_nic/rtl8168f-2.fw"
46 #define FIRMWARE_8105E_1	"rtl_nic/rtl8105e-1.fw"
47 #define FIRMWARE_8402_1		"rtl_nic/rtl8402-1.fw"
48 #define FIRMWARE_8411_1		"rtl_nic/rtl8411-1.fw"
49 #define FIRMWARE_8411_2		"rtl_nic/rtl8411-2.fw"
50 #define FIRMWARE_8106E_1	"rtl_nic/rtl8106e-1.fw"
51 #define FIRMWARE_8106E_2	"rtl_nic/rtl8106e-2.fw"
52 #define FIRMWARE_8168G_2	"rtl_nic/rtl8168g-2.fw"
53 #define FIRMWARE_8168G_3	"rtl_nic/rtl8168g-3.fw"
54 #define FIRMWARE_8168H_2	"rtl_nic/rtl8168h-2.fw"
55 #define FIRMWARE_8168FP_3	"rtl_nic/rtl8168fp-3.fw"
56 #define FIRMWARE_8107E_2	"rtl_nic/rtl8107e-2.fw"
57 #define FIRMWARE_8125A_3	"rtl_nic/rtl8125a-3.fw"
58 #define FIRMWARE_8125B_2	"rtl_nic/rtl8125b-2.fw"
59 #define FIRMWARE_8125D_1	"rtl_nic/rtl8125d-1.fw"
60 #define FIRMWARE_8126A_2	"rtl_nic/rtl8126a-2.fw"
61 #define FIRMWARE_8126A_3	"rtl_nic/rtl8126a-3.fw"
62 
63 #define TX_DMA_BURST	7	/* Maximum PCI burst, '7' is unlimited */
64 #define InterFrameGap	0x03	/* 3 means InterFrameGap = the shortest one */
65 
66 #define R8169_REGS_SIZE		256
67 #define R8169_RX_BUF_SIZE	(SZ_16K - 1)
68 #define NUM_TX_DESC	256	/* Number of Tx descriptor registers */
69 #define NUM_RX_DESC	256	/* Number of Rx descriptor registers */
70 #define R8169_TX_RING_BYTES	(NUM_TX_DESC * sizeof(struct TxDesc))
71 #define R8169_RX_RING_BYTES	(NUM_RX_DESC * sizeof(struct RxDesc))
72 #define R8169_TX_STOP_THRS	(MAX_SKB_FRAGS + 1)
73 #define R8169_TX_START_THRS	(2 * R8169_TX_STOP_THRS)
74 
75 #define OCP_STD_PHY_BASE	0xa400
76 
77 #define RTL_CFG_NO_GBIT	1
78 
79 /* write/read MMIO register */
80 #define RTL_W8(tp, reg, val8)	writeb((val8), tp->mmio_addr + (reg))
81 #define RTL_W16(tp, reg, val16)	writew((val16), tp->mmio_addr + (reg))
82 #define RTL_W32(tp, reg, val32)	writel((val32), tp->mmio_addr + (reg))
83 #define RTL_R8(tp, reg)		readb(tp->mmio_addr + (reg))
84 #define RTL_R16(tp, reg)		readw(tp->mmio_addr + (reg))
85 #define RTL_R32(tp, reg)		readl(tp->mmio_addr + (reg))
86 
87 #define JUMBO_4K	(4 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
88 #define JUMBO_6K	(6 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
89 #define JUMBO_7K	(7 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
90 #define JUMBO_9K	(9 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
91 
92 static const struct {
93 	const char *name;
94 	const char *fw_name;
95 } rtl_chip_infos[] = {
96 	/* PCI devices. */
97 	[RTL_GIGA_MAC_VER_02] = {"RTL8169s"				},
98 	[RTL_GIGA_MAC_VER_03] = {"RTL8110s"				},
99 	[RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb"			},
100 	[RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc"			},
101 	[RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc"			},
102 	/* PCI-E devices. */
103 	[RTL_GIGA_MAC_VER_07] = {"RTL8102e"				},
104 	[RTL_GIGA_MAC_VER_08] = {"RTL8102e"				},
105 	[RTL_GIGA_MAC_VER_09] = {"RTL8102e/RTL8103e"			},
106 	[RTL_GIGA_MAC_VER_10] = {"RTL8101e/RTL8100e"			},
107 	[RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b"			},
108 	[RTL_GIGA_MAC_VER_14] = {"RTL8401"				},
109 	[RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b"			},
110 	[RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp"			},
111 	[RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c"			},
112 	[RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c"			},
113 	[RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c"			},
114 	[RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c"			},
115 	[RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp"			},
116 	[RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp"			},
117 	[RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d",	FIRMWARE_8168D_1},
118 	[RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d",	FIRMWARE_8168D_2},
119 	[RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp"			},
120 	[RTL_GIGA_MAC_VER_29] = {"RTL8105e",		FIRMWARE_8105E_1},
121 	[RTL_GIGA_MAC_VER_30] = {"RTL8105e",		FIRMWARE_8105E_1},
122 	[RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp"			},
123 	[RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e",	FIRMWARE_8168E_1},
124 	[RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e",	FIRMWARE_8168E_2},
125 	[RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl",	FIRMWARE_8168E_3},
126 	[RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f",	FIRMWARE_8168F_1},
127 	[RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f",	FIRMWARE_8168F_2},
128 	[RTL_GIGA_MAC_VER_37] = {"RTL8402",		FIRMWARE_8402_1 },
129 	[RTL_GIGA_MAC_VER_38] = {"RTL8411",		FIRMWARE_8411_1 },
130 	[RTL_GIGA_MAC_VER_39] = {"RTL8106e",		FIRMWARE_8106E_1},
131 	[RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g",	FIRMWARE_8168G_2},
132 	[RTL_GIGA_MAC_VER_42] = {"RTL8168gu/8111gu",	FIRMWARE_8168G_3},
133 	[RTL_GIGA_MAC_VER_43] = {"RTL8106eus",		FIRMWARE_8106E_2},
134 	[RTL_GIGA_MAC_VER_44] = {"RTL8411b",		FIRMWARE_8411_2 },
135 	[RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h",	FIRMWARE_8168H_2},
136 	[RTL_GIGA_MAC_VER_48] = {"RTL8107e",		FIRMWARE_8107E_2},
137 	[RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep"			},
138 	[RTL_GIGA_MAC_VER_52] = {"RTL8168fp/RTL8117",  FIRMWARE_8168FP_3},
139 	[RTL_GIGA_MAC_VER_53] = {"RTL8168fp/RTL8117",			},
140 	[RTL_GIGA_MAC_VER_61] = {"RTL8125A",		FIRMWARE_8125A_3},
141 	/* reserve 62 for CFG_METHOD_4 in the vendor driver */
142 	[RTL_GIGA_MAC_VER_63] = {"RTL8125B",		FIRMWARE_8125B_2},
143 	[RTL_GIGA_MAC_VER_64] = {"RTL8125D",		FIRMWARE_8125D_1},
144 	[RTL_GIGA_MAC_VER_65] = {"RTL8126A",		FIRMWARE_8126A_2},
145 	[RTL_GIGA_MAC_VER_66] = {"RTL8126A",		FIRMWARE_8126A_3},
146 };
147 
148 static const struct pci_device_id rtl8169_pci_tbl[] = {
149 	{ PCI_VDEVICE(REALTEK,	0x2502) },
150 	{ PCI_VDEVICE(REALTEK,	0x2600) },
151 	{ PCI_VDEVICE(REALTEK,	0x8129) },
152 	{ PCI_VDEVICE(REALTEK,	0x8136), RTL_CFG_NO_GBIT },
153 	{ PCI_VDEVICE(REALTEK,	0x8161) },
154 	{ PCI_VDEVICE(REALTEK,	0x8162) },
155 	{ PCI_VDEVICE(REALTEK,	0x8167) },
156 	{ PCI_VDEVICE(REALTEK,	0x8168) },
157 	{ PCI_VDEVICE(NCUBE,	0x8168) },
158 	{ PCI_VDEVICE(REALTEK,	0x8169) },
159 	{ PCI_VENDOR_ID_DLINK,	0x4300,
160 		PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0 },
161 	{ PCI_VDEVICE(DLINK,	0x4300) },
162 	{ PCI_VDEVICE(DLINK,	0x4302) },
163 	{ PCI_VDEVICE(AT,	0xc107) },
164 	{ PCI_VDEVICE(USR,	0x0116) },
165 	{ PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0024 },
166 	{ 0x0001, 0x8168, PCI_ANY_ID, 0x2410 },
167 	{ PCI_VDEVICE(REALTEK,	0x8125) },
168 	{ PCI_VDEVICE(REALTEK,	0x8126) },
169 	{ PCI_VDEVICE(REALTEK,	0x3000) },
170 	{}
171 };
172 
173 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
174 
175 enum rtl_registers {
176 	MAC0		= 0,	/* Ethernet hardware address. */
177 	MAC4		= 4,
178 	MAR0		= 8,	/* Multicast filter. */
179 	CounterAddrLow		= 0x10,
180 	CounterAddrHigh		= 0x14,
181 	TxDescStartAddrLow	= 0x20,
182 	TxDescStartAddrHigh	= 0x24,
183 	TxHDescStartAddrLow	= 0x28,
184 	TxHDescStartAddrHigh	= 0x2c,
185 	FLASH		= 0x30,
186 	ERSR		= 0x36,
187 	ChipCmd		= 0x37,
188 	TxPoll		= 0x38,
189 	IntrMask	= 0x3c,
190 	IntrStatus	= 0x3e,
191 
192 	TxConfig	= 0x40,
193 #define	TXCFG_AUTO_FIFO			(1 << 7)	/* 8111e-vl */
194 #define	TXCFG_EMPTY			(1 << 11)	/* 8111e-vl */
195 
196 	RxConfig	= 0x44,
197 #define	RX128_INT_EN			(1 << 15)	/* 8111c and later */
198 #define	RX_MULTI_EN			(1 << 14)	/* 8111c only */
199 #define	RXCFG_FIFO_SHIFT		13
200 					/* No threshold before first PCI xfer */
201 #define	RX_FIFO_THRESH			(7 << RXCFG_FIFO_SHIFT)
202 #define	RX_EARLY_OFF			(1 << 11)
203 #define	RX_PAUSE_SLOT_ON		(1 << 11)	/* 8125b and later */
204 #define	RXCFG_DMA_SHIFT			8
205 					/* Unlimited maximum PCI burst. */
206 #define	RX_DMA_BURST			(7 << RXCFG_DMA_SHIFT)
207 
208 	Cfg9346		= 0x50,
209 	Config0		= 0x51,
210 	Config1		= 0x52,
211 	Config2		= 0x53,
212 #define PME_SIGNAL			(1 << 5)	/* 8168c and later */
213 
214 	Config3		= 0x54,
215 	Config4		= 0x55,
216 	Config5		= 0x56,
217 	PHYAR		= 0x60,
218 	PHYstatus	= 0x6c,
219 	RxMaxSize	= 0xda,
220 	CPlusCmd	= 0xe0,
221 	IntrMitigate	= 0xe2,
222 
223 #define RTL_COALESCE_TX_USECS	GENMASK(15, 12)
224 #define RTL_COALESCE_TX_FRAMES	GENMASK(11, 8)
225 #define RTL_COALESCE_RX_USECS	GENMASK(7, 4)
226 #define RTL_COALESCE_RX_FRAMES	GENMASK(3, 0)
227 
228 #define RTL_COALESCE_T_MAX	0x0fU
229 #define RTL_COALESCE_FRAME_MAX	(RTL_COALESCE_T_MAX * 4)
230 
231 	RxDescAddrLow	= 0xe4,
232 	RxDescAddrHigh	= 0xe8,
233 	EarlyTxThres	= 0xec,	/* 8169. Unit of 32 bytes. */
234 
235 #define NoEarlyTx	0x3f	/* Max value : no early transmit. */
236 
237 	MaxTxPacketSize	= 0xec,	/* 8101/8168. Unit of 128 bytes. */
238 
239 #define TxPacketMax	(8064 >> 7)
240 #define EarlySize	0x27
241 
242 	FuncEvent	= 0xf0,
243 	FuncEventMask	= 0xf4,
244 	FuncPresetState	= 0xf8,
245 	IBCR0           = 0xf8,
246 	IBCR2           = 0xf9,
247 	IBIMR0          = 0xfa,
248 	IBISR0          = 0xfb,
249 	FuncForceEvent	= 0xfc,
250 };
251 
252 enum rtl8168_8101_registers {
253 	CSIDR			= 0x64,
254 	CSIAR			= 0x68,
255 #define	CSIAR_FLAG			0x80000000
256 #define	CSIAR_WRITE_CMD			0x80000000
257 #define	CSIAR_BYTE_ENABLE		0x0000f000
258 #define	CSIAR_ADDR_MASK			0x00000fff
259 	PMCH			= 0x6f,
260 #define D3COLD_NO_PLL_DOWN		BIT(7)
261 #define D3HOT_NO_PLL_DOWN		BIT(6)
262 #define D3_NO_PLL_DOWN			(BIT(7) | BIT(6))
263 	EPHYAR			= 0x80,
264 #define	EPHYAR_FLAG			0x80000000
265 #define	EPHYAR_WRITE_CMD		0x80000000
266 #define	EPHYAR_REG_MASK			0x1f
267 #define	EPHYAR_REG_SHIFT		16
268 #define	EPHYAR_DATA_MASK		0xffff
269 	DLLPR			= 0xd0,
270 #define	PFM_EN				(1 << 6)
271 #define	TX_10M_PS_EN			(1 << 7)
272 	DBG_REG			= 0xd1,
273 #define	FIX_NAK_1			(1 << 4)
274 #define	FIX_NAK_2			(1 << 3)
275 	TWSI			= 0xd2,
276 	MCU			= 0xd3,
277 #define	NOW_IS_OOB			(1 << 7)
278 #define	TX_EMPTY			(1 << 5)
279 #define	RX_EMPTY			(1 << 4)
280 #define	RXTX_EMPTY			(TX_EMPTY | RX_EMPTY)
281 #define	EN_NDP				(1 << 3)
282 #define	EN_OOB_RESET			(1 << 2)
283 #define	LINK_LIST_RDY			(1 << 1)
284 	EFUSEAR			= 0xdc,
285 #define	EFUSEAR_FLAG			0x80000000
286 #define	EFUSEAR_WRITE_CMD		0x80000000
287 #define	EFUSEAR_READ_CMD		0x00000000
288 #define	EFUSEAR_REG_MASK		0x03ff
289 #define	EFUSEAR_REG_SHIFT		8
290 #define	EFUSEAR_DATA_MASK		0xff
291 	MISC_1			= 0xf2,
292 #define	PFM_D3COLD_EN			(1 << 6)
293 };
294 
295 enum rtl8168_registers {
296 	LED_CTRL		= 0x18,
297 	LED_FREQ		= 0x1a,
298 	EEE_LED			= 0x1b,
299 	ERIDR			= 0x70,
300 	ERIAR			= 0x74,
301 #define ERIAR_FLAG			0x80000000
302 #define ERIAR_WRITE_CMD			0x80000000
303 #define ERIAR_READ_CMD			0x00000000
304 #define ERIAR_ADDR_BYTE_ALIGN		4
305 #define ERIAR_TYPE_SHIFT		16
306 #define ERIAR_EXGMAC			(0x00 << ERIAR_TYPE_SHIFT)
307 #define ERIAR_MSIX			(0x01 << ERIAR_TYPE_SHIFT)
308 #define ERIAR_ASF			(0x02 << ERIAR_TYPE_SHIFT)
309 #define ERIAR_OOB			(0x02 << ERIAR_TYPE_SHIFT)
310 #define ERIAR_MASK_SHIFT		12
311 #define ERIAR_MASK_0001			(0x1 << ERIAR_MASK_SHIFT)
312 #define ERIAR_MASK_0011			(0x3 << ERIAR_MASK_SHIFT)
313 #define ERIAR_MASK_0100			(0x4 << ERIAR_MASK_SHIFT)
314 #define ERIAR_MASK_0101			(0x5 << ERIAR_MASK_SHIFT)
315 #define ERIAR_MASK_1111			(0xf << ERIAR_MASK_SHIFT)
316 	EPHY_RXER_NUM		= 0x7c,
317 	OCPDR			= 0xb0,	/* OCP GPHY access */
318 #define OCPDR_WRITE_CMD			0x80000000
319 #define OCPDR_READ_CMD			0x00000000
320 #define OCPDR_REG_MASK			0x7f
321 #define OCPDR_GPHY_REG_SHIFT		16
322 #define OCPDR_DATA_MASK			0xffff
323 	OCPAR			= 0xb4,
324 #define OCPAR_FLAG			0x80000000
325 #define OCPAR_GPHY_WRITE_CMD		0x8000f060
326 #define OCPAR_GPHY_READ_CMD		0x0000f060
327 	GPHY_OCP		= 0xb8,
328 	RDSAR1			= 0xd0,	/* 8168c only. Undocumented on 8168dp */
329 	MISC			= 0xf0,	/* 8168e only. */
330 #define TXPLA_RST			(1 << 29)
331 #define DISABLE_LAN_EN			(1 << 23) /* Enable GPIO pin */
332 #define PWM_EN				(1 << 22)
333 #define RXDV_GATED_EN			(1 << 19)
334 #define EARLY_TALLY_EN			(1 << 16)
335 };
336 
337 enum rtl8125_registers {
338 	LEDSEL0			= 0x18,
339 	INT_CFG0_8125		= 0x34,
340 #define INT_CFG0_ENABLE_8125		BIT(0)
341 #define INT_CFG0_CLKREQEN		BIT(3)
342 	IntrMask_8125		= 0x38,
343 	IntrStatus_8125		= 0x3c,
344 	INT_CFG1_8125		= 0x7a,
345 	LEDSEL2			= 0x84,
346 	LEDSEL1			= 0x86,
347 	TxPoll_8125		= 0x90,
348 	LEDSEL3			= 0x96,
349 	MAC0_BKP		= 0x19e0,
350 	RSS_CTRL_8125		= 0x4500,
351 	Q_NUM_CTRL_8125		= 0x4800,
352 	EEE_TXIDLE_TIMER_8125	= 0x6048,
353 };
354 
355 #define LEDSEL_MASK_8125	0x23f
356 
357 #define RX_VLAN_INNER_8125	BIT(22)
358 #define RX_VLAN_OUTER_8125	BIT(23)
359 #define RX_VLAN_8125		(RX_VLAN_INNER_8125 | RX_VLAN_OUTER_8125)
360 
361 #define RX_FETCH_DFLT_8125	(8 << 27)
362 
363 enum rtl_register_content {
364 	/* InterruptStatusBits */
365 	SYSErr		= 0x8000,
366 	PCSTimeout	= 0x4000,
367 	SWInt		= 0x0100,
368 	TxDescUnavail	= 0x0080,
369 	RxFIFOOver	= 0x0040,
370 	LinkChg		= 0x0020,
371 	RxOverflow	= 0x0010,
372 	TxErr		= 0x0008,
373 	TxOK		= 0x0004,
374 	RxErr		= 0x0002,
375 	RxOK		= 0x0001,
376 
377 	/* RxStatusDesc */
378 	RxRWT	= (1 << 22),
379 	RxRES	= (1 << 21),
380 	RxRUNT	= (1 << 20),
381 	RxCRC	= (1 << 19),
382 
383 	/* ChipCmdBits */
384 	StopReq		= 0x80,
385 	CmdReset	= 0x10,
386 	CmdRxEnb	= 0x08,
387 	CmdTxEnb	= 0x04,
388 	RxBufEmpty	= 0x01,
389 
390 	/* TXPoll register p.5 */
391 	HPQ		= 0x80,		/* Poll cmd on the high prio queue */
392 	NPQ		= 0x40,		/* Poll cmd on the low prio queue */
393 	FSWInt		= 0x01,		/* Forced software interrupt */
394 
395 	/* Cfg9346Bits */
396 	Cfg9346_Lock	= 0x00,
397 	Cfg9346_Unlock	= 0xc0,
398 
399 	/* rx_mode_bits */
400 	AcceptErr	= 0x20,
401 	AcceptRunt	= 0x10,
402 #define RX_CONFIG_ACCEPT_ERR_MASK	0x30
403 	AcceptBroadcast	= 0x08,
404 	AcceptMulticast	= 0x04,
405 	AcceptMyPhys	= 0x02,
406 	AcceptAllPhys	= 0x01,
407 #define RX_CONFIG_ACCEPT_OK_MASK	0x0f
408 #define RX_CONFIG_ACCEPT_MASK		0x3f
409 
410 	/* TxConfigBits */
411 	TxInterFrameGapShift = 24,
412 	TxDMAShift = 8,	/* DMA burst value (0-7) is shift this many bits */
413 
414 	/* Config1 register p.24 */
415 	LEDS1		= (1 << 7),
416 	LEDS0		= (1 << 6),
417 	Speed_down	= (1 << 4),
418 	MEMMAP		= (1 << 3),
419 	IOMAP		= (1 << 2),
420 	VPD		= (1 << 1),
421 	PMEnable	= (1 << 0),	/* Power Management Enable */
422 
423 	/* Config2 register p. 25 */
424 	ClkReqEn	= (1 << 7),	/* Clock Request Enable */
425 	MSIEnable	= (1 << 5),	/* 8169 only. Reserved in the 8168. */
426 	PCI_Clock_66MHz = 0x01,
427 	PCI_Clock_33MHz = 0x00,
428 
429 	/* Config3 register p.25 */
430 	MagicPacket	= (1 << 5),	/* Wake up when receives a Magic Packet */
431 	LinkUp		= (1 << 4),	/* Wake up when the cable connection is re-established */
432 	Jumbo_En0	= (1 << 2),	/* 8168 only. Reserved in the 8168b */
433 	Rdy_to_L23	= (1 << 1),	/* L23 Enable */
434 	Beacon_en	= (1 << 0),	/* 8168 only. Reserved in the 8168b */
435 
436 	/* Config4 register */
437 	Jumbo_En1	= (1 << 1),	/* 8168 only. Reserved in the 8168b */
438 
439 	/* Config5 register p.27 */
440 	BWF		= (1 << 6),	/* Accept Broadcast wakeup frame */
441 	MWF		= (1 << 5),	/* Accept Multicast wakeup frame */
442 	UWF		= (1 << 4),	/* Accept Unicast wakeup frame */
443 	Spi_en		= (1 << 3),
444 	LanWake		= (1 << 1),	/* LanWake enable/disable */
445 	PMEStatus	= (1 << 0),	/* PME status can be reset by PCI RST# */
446 	ASPM_en		= (1 << 0),	/* ASPM enable */
447 
448 	/* CPlusCmd p.31 */
449 	EnableBist	= (1 << 15),	// 8168 8101
450 	Mac_dbgo_oe	= (1 << 14),	// 8168 8101
451 	EnAnaPLL	= (1 << 14),	// 8169
452 	Normal_mode	= (1 << 13),	// unused
453 	Force_half_dup	= (1 << 12),	// 8168 8101
454 	Force_rxflow_en	= (1 << 11),	// 8168 8101
455 	Force_txflow_en	= (1 << 10),	// 8168 8101
456 	Cxpl_dbg_sel	= (1 << 9),	// 8168 8101
457 	ASF		= (1 << 8),	// 8168 8101
458 	PktCntrDisable	= (1 << 7),	// 8168 8101
459 	Mac_dbgo_sel	= 0x001c,	// 8168
460 	RxVlan		= (1 << 6),
461 	RxChkSum	= (1 << 5),
462 	PCIDAC		= (1 << 4),
463 	PCIMulRW	= (1 << 3),
464 #define INTT_MASK	GENMASK(1, 0)
465 #define CPCMD_MASK	(Normal_mode | RxVlan | RxChkSum | INTT_MASK)
466 
467 	/* rtl8169_PHYstatus */
468 	TBI_Enable	= 0x80,
469 	TxFlowCtrl	= 0x40,
470 	RxFlowCtrl	= 0x20,
471 	_1000bpsF	= 0x10,
472 	_100bps		= 0x08,
473 	_10bps		= 0x04,
474 	LinkStatus	= 0x02,
475 	FullDup		= 0x01,
476 
477 	/* ResetCounterCommand */
478 	CounterReset	= 0x1,
479 
480 	/* DumpCounterCommand */
481 	CounterDump	= 0x8,
482 
483 	/* magic enable v2 */
484 	MagicPacket_v2	= (1 << 16),	/* Wake up when receives a Magic Packet */
485 };
486 
487 enum rtl_desc_bit {
488 	/* First doubleword. */
489 	DescOwn		= (1 << 31), /* Descriptor is owned by NIC */
490 	RingEnd		= (1 << 30), /* End of descriptor ring */
491 	FirstFrag	= (1 << 29), /* First segment of a packet */
492 	LastFrag	= (1 << 28), /* Final segment of a packet */
493 };
494 
495 /* Generic case. */
496 enum rtl_tx_desc_bit {
497 	/* First doubleword. */
498 	TD_LSO		= (1 << 27),		/* Large Send Offload */
499 #define TD_MSS_MAX			0x07ffu	/* MSS value */
500 
501 	/* Second doubleword. */
502 	TxVlanTag	= (1 << 17),		/* Add VLAN tag */
503 };
504 
505 /* 8169, 8168b and 810x except 8102e. */
506 enum rtl_tx_desc_bit_0 {
507 	/* First doubleword. */
508 #define TD0_MSS_SHIFT			16	/* MSS position (11 bits) */
509 	TD0_TCP_CS	= (1 << 16),		/* Calculate TCP/IP checksum */
510 	TD0_UDP_CS	= (1 << 17),		/* Calculate UDP/IP checksum */
511 	TD0_IP_CS	= (1 << 18),		/* Calculate IP checksum */
512 };
513 
514 /* 8102e, 8168c and beyond. */
515 enum rtl_tx_desc_bit_1 {
516 	/* First doubleword. */
517 	TD1_GTSENV4	= (1 << 26),		/* Giant Send for IPv4 */
518 	TD1_GTSENV6	= (1 << 25),		/* Giant Send for IPv6 */
519 #define GTTCPHO_SHIFT			18
520 #define GTTCPHO_MAX			0x7f
521 
522 	/* Second doubleword. */
523 #define TCPHO_SHIFT			18
524 #define TCPHO_MAX			0x3ff
525 #define TD1_MSS_SHIFT			18	/* MSS position (11 bits) */
526 	TD1_IPv6_CS	= (1 << 28),		/* Calculate IPv6 checksum */
527 	TD1_IPv4_CS	= (1 << 29),		/* Calculate IPv4 checksum */
528 	TD1_TCP_CS	= (1 << 30),		/* Calculate TCP/IP checksum */
529 	TD1_UDP_CS	= (1 << 31),		/* Calculate UDP/IP checksum */
530 };
531 
532 enum rtl_rx_desc_bit {
533 	/* Rx private */
534 	PID1		= (1 << 18), /* Protocol ID bit 1/2 */
535 	PID0		= (1 << 17), /* Protocol ID bit 0/2 */
536 
537 #define RxProtoUDP	(PID1)
538 #define RxProtoTCP	(PID0)
539 #define RxProtoIP	(PID1 | PID0)
540 #define RxProtoMask	RxProtoIP
541 
542 	IPFail		= (1 << 16), /* IP checksum failed */
543 	UDPFail		= (1 << 15), /* UDP/IP checksum failed */
544 	TCPFail		= (1 << 14), /* TCP/IP checksum failed */
545 
546 #define RxCSFailMask	(IPFail | UDPFail | TCPFail)
547 
548 	RxVlanTag	= (1 << 16), /* VLAN tag available */
549 };
550 
551 #define RTL_GSO_MAX_SIZE_V1	32000
552 #define RTL_GSO_MAX_SEGS_V1	24
553 #define RTL_GSO_MAX_SIZE_V2	64000
554 #define RTL_GSO_MAX_SEGS_V2	64
555 
556 struct TxDesc {
557 	__le32 opts1;
558 	__le32 opts2;
559 	__le64 addr;
560 };
561 
562 struct RxDesc {
563 	__le32 opts1;
564 	__le32 opts2;
565 	__le64 addr;
566 };
567 
568 struct ring_info {
569 	struct sk_buff	*skb;
570 	u32		len;
571 };
572 
573 struct rtl8169_counters {
574 	__le64	tx_packets;
575 	__le64	rx_packets;
576 	__le64	tx_errors;
577 	__le32	rx_errors;
578 	__le16	rx_missed;
579 	__le16	align_errors;
580 	__le32	tx_one_collision;
581 	__le32	tx_multi_collision;
582 	__le64	rx_unicast;
583 	__le64	rx_broadcast;
584 	__le32	rx_multicast;
585 	__le16	tx_aborted;
586 	__le16	tx_underrun;
587 	/* new since RTL8125 */
588 	__le64 tx_octets;
589 	__le64 rx_octets;
590 	__le64 rx_multicast64;
591 	__le64 tx_unicast64;
592 	__le64 tx_broadcast64;
593 	__le64 tx_multicast64;
594 	__le32 tx_pause_on;
595 	__le32 tx_pause_off;
596 	__le32 tx_pause_all;
597 	__le32 tx_deferred;
598 	__le32 tx_late_collision;
599 	__le32 tx_all_collision;
600 	__le32 tx_aborted32;
601 	__le32 align_errors32;
602 	__le32 rx_frame_too_long;
603 	__le32 rx_runt;
604 	__le32 rx_pause_on;
605 	__le32 rx_pause_off;
606 	__le32 rx_pause_all;
607 	__le32 rx_unknown_opcode;
608 	__le32 rx_mac_error;
609 	__le32 tx_underrun32;
610 	__le32 rx_mac_missed;
611 	__le32 rx_tcam_dropped;
612 	__le32 tdu;
613 	__le32 rdu;
614 };
615 
616 struct rtl8169_tc_offsets {
617 	bool	inited;
618 	__le64	tx_errors;
619 	__le32	tx_multi_collision;
620 	__le16	tx_aborted;
621 	__le16	rx_missed;
622 };
623 
624 enum rtl_flag {
625 	RTL_FLAG_TASK_RESET_PENDING,
626 	RTL_FLAG_TASK_RESET_NO_QUEUE_WAKE,
627 	RTL_FLAG_TASK_TX_TIMEOUT,
628 	RTL_FLAG_MAX
629 };
630 
631 enum rtl_dash_type {
632 	RTL_DASH_NONE,
633 	RTL_DASH_DP,
634 	RTL_DASH_EP,
635 };
636 
637 struct rtl8169_private {
638 	void __iomem *mmio_addr;	/* memory map physical address */
639 	struct pci_dev *pci_dev;
640 	struct net_device *dev;
641 	struct phy_device *phydev;
642 	struct napi_struct napi;
643 	enum mac_version mac_version;
644 	enum rtl_dash_type dash_type;
645 	u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
646 	u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
647 	u32 dirty_tx;
648 	struct TxDesc *TxDescArray;	/* 256-aligned Tx descriptor ring */
649 	struct RxDesc *RxDescArray;	/* 256-aligned Rx descriptor ring */
650 	dma_addr_t TxPhyAddr;
651 	dma_addr_t RxPhyAddr;
652 	struct page *Rx_databuff[NUM_RX_DESC];	/* Rx data buffers */
653 	struct ring_info tx_skb[NUM_TX_DESC];	/* Tx data buffers */
654 	u16 cp_cmd;
655 	u16 tx_lpi_timer;
656 	u32 irq_mask;
657 	int irq;
658 	struct clk *clk;
659 
660 	struct {
661 		DECLARE_BITMAP(flags, RTL_FLAG_MAX);
662 		struct work_struct work;
663 	} wk;
664 
665 	raw_spinlock_t mac_ocp_lock;
666 	struct mutex led_lock;	/* serialize LED ctrl RMW access */
667 
668 	unsigned supports_gmii:1;
669 	unsigned aspm_manageable:1;
670 	unsigned dash_enabled:1;
671 	dma_addr_t counters_phys_addr;
672 	struct rtl8169_counters *counters;
673 	struct rtl8169_tc_offsets tc_offset;
674 	u32 saved_wolopts;
675 
676 	const char *fw_name;
677 	struct rtl_fw *rtl_fw;
678 
679 	struct r8169_led_classdev *leds;
680 
681 	u32 ocp_base;
682 };
683 
684 typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
685 
686 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
687 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
688 MODULE_SOFTDEP("pre: realtek");
689 MODULE_LICENSE("GPL");
690 MODULE_FIRMWARE(FIRMWARE_8168D_1);
691 MODULE_FIRMWARE(FIRMWARE_8168D_2);
692 MODULE_FIRMWARE(FIRMWARE_8168E_1);
693 MODULE_FIRMWARE(FIRMWARE_8168E_2);
694 MODULE_FIRMWARE(FIRMWARE_8168E_3);
695 MODULE_FIRMWARE(FIRMWARE_8105E_1);
696 MODULE_FIRMWARE(FIRMWARE_8168F_1);
697 MODULE_FIRMWARE(FIRMWARE_8168F_2);
698 MODULE_FIRMWARE(FIRMWARE_8402_1);
699 MODULE_FIRMWARE(FIRMWARE_8411_1);
700 MODULE_FIRMWARE(FIRMWARE_8411_2);
701 MODULE_FIRMWARE(FIRMWARE_8106E_1);
702 MODULE_FIRMWARE(FIRMWARE_8106E_2);
703 MODULE_FIRMWARE(FIRMWARE_8168G_2);
704 MODULE_FIRMWARE(FIRMWARE_8168G_3);
705 MODULE_FIRMWARE(FIRMWARE_8168H_2);
706 MODULE_FIRMWARE(FIRMWARE_8168FP_3);
707 MODULE_FIRMWARE(FIRMWARE_8107E_2);
708 MODULE_FIRMWARE(FIRMWARE_8125A_3);
709 MODULE_FIRMWARE(FIRMWARE_8125B_2);
710 MODULE_FIRMWARE(FIRMWARE_8125D_1);
711 MODULE_FIRMWARE(FIRMWARE_8126A_2);
712 MODULE_FIRMWARE(FIRMWARE_8126A_3);
713 
714 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
715 {
716 	return &tp->pci_dev->dev;
717 }
718 
719 static void rtl_lock_config_regs(struct rtl8169_private *tp)
720 {
721 	RTL_W8(tp, Cfg9346, Cfg9346_Lock);
722 }
723 
724 static void rtl_unlock_config_regs(struct rtl8169_private *tp)
725 {
726 	RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
727 }
728 
729 static void rtl_pci_commit(struct rtl8169_private *tp)
730 {
731 	/* Read an arbitrary register to commit a preceding PCI write */
732 	RTL_R8(tp, ChipCmd);
733 }
734 
735 static void rtl_mod_config2(struct rtl8169_private *tp, u8 clear, u8 set)
736 {
737 	u8 val;
738 
739 	val = RTL_R8(tp, Config2);
740 	RTL_W8(tp, Config2, (val & ~clear) | set);
741 }
742 
743 static void rtl_mod_config5(struct rtl8169_private *tp, u8 clear, u8 set)
744 {
745 	u8 val;
746 
747 	val = RTL_R8(tp, Config5);
748 	RTL_W8(tp, Config5, (val & ~clear) | set);
749 }
750 
751 static bool rtl_is_8125(struct rtl8169_private *tp)
752 {
753 	return tp->mac_version >= RTL_GIGA_MAC_VER_61;
754 }
755 
756 static bool rtl_is_8168evl_up(struct rtl8169_private *tp)
757 {
758 	return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
759 	       tp->mac_version != RTL_GIGA_MAC_VER_39 &&
760 	       tp->mac_version <= RTL_GIGA_MAC_VER_53;
761 }
762 
763 static bool rtl_supports_eee(struct rtl8169_private *tp)
764 {
765 	return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
766 	       tp->mac_version != RTL_GIGA_MAC_VER_37 &&
767 	       tp->mac_version != RTL_GIGA_MAC_VER_39;
768 }
769 
770 static void rtl_read_mac_from_reg(struct rtl8169_private *tp, u8 *mac, int reg)
771 {
772 	int i;
773 
774 	for (i = 0; i < ETH_ALEN; i++)
775 		mac[i] = RTL_R8(tp, reg + i);
776 }
777 
778 struct rtl_cond {
779 	bool (*check)(struct rtl8169_private *);
780 	const char *msg;
781 };
782 
783 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
784 			  unsigned long usecs, int n, bool high)
785 {
786 	int i;
787 
788 	for (i = 0; i < n; i++) {
789 		if (c->check(tp) == high)
790 			return true;
791 		fsleep(usecs);
792 	}
793 
794 	if (net_ratelimit())
795 		netdev_err(tp->dev, "%s == %d (loop: %d, delay: %lu).\n",
796 			   c->msg, !high, n, usecs);
797 	return false;
798 }
799 
800 static bool rtl_loop_wait_high(struct rtl8169_private *tp,
801 			       const struct rtl_cond *c,
802 			       unsigned long d, int n)
803 {
804 	return rtl_loop_wait(tp, c, d, n, true);
805 }
806 
807 static bool rtl_loop_wait_low(struct rtl8169_private *tp,
808 			      const struct rtl_cond *c,
809 			      unsigned long d, int n)
810 {
811 	return rtl_loop_wait(tp, c, d, n, false);
812 }
813 
814 #define DECLARE_RTL_COND(name)				\
815 static bool name ## _check(struct rtl8169_private *);	\
816 							\
817 static const struct rtl_cond name = {			\
818 	.check	= name ## _check,			\
819 	.msg	= #name					\
820 };							\
821 							\
822 static bool name ## _check(struct rtl8169_private *tp)
823 
824 int rtl8168_led_mod_ctrl(struct rtl8169_private *tp, u16 mask, u16 val)
825 {
826 	struct device *dev = tp_to_dev(tp);
827 	int ret;
828 
829 	ret = pm_runtime_resume_and_get(dev);
830 	if (ret < 0)
831 		return ret;
832 
833 	mutex_lock(&tp->led_lock);
834 	RTL_W16(tp, LED_CTRL, (RTL_R16(tp, LED_CTRL) & ~mask) | val);
835 	mutex_unlock(&tp->led_lock);
836 
837 	pm_runtime_put_sync(dev);
838 
839 	return 0;
840 }
841 
842 int rtl8168_get_led_mode(struct rtl8169_private *tp)
843 {
844 	struct device *dev = tp_to_dev(tp);
845 	int ret;
846 
847 	ret = pm_runtime_resume_and_get(dev);
848 	if (ret < 0)
849 		return ret;
850 
851 	ret = RTL_R16(tp, LED_CTRL);
852 
853 	pm_runtime_put_sync(dev);
854 
855 	return ret;
856 }
857 
858 static int rtl8125_get_led_reg(int index)
859 {
860 	static const int led_regs[] = { LEDSEL0, LEDSEL1, LEDSEL2, LEDSEL3 };
861 
862 	return led_regs[index];
863 }
864 
865 int rtl8125_set_led_mode(struct rtl8169_private *tp, int index, u16 mode)
866 {
867 	int reg = rtl8125_get_led_reg(index);
868 	struct device *dev = tp_to_dev(tp);
869 	int ret;
870 	u16 val;
871 
872 	ret = pm_runtime_resume_and_get(dev);
873 	if (ret < 0)
874 		return ret;
875 
876 	mutex_lock(&tp->led_lock);
877 	val = RTL_R16(tp, reg) & ~LEDSEL_MASK_8125;
878 	RTL_W16(tp, reg, val | mode);
879 	mutex_unlock(&tp->led_lock);
880 
881 	pm_runtime_put_sync(dev);
882 
883 	return 0;
884 }
885 
886 int rtl8125_get_led_mode(struct rtl8169_private *tp, int index)
887 {
888 	int reg = rtl8125_get_led_reg(index);
889 	struct device *dev = tp_to_dev(tp);
890 	int ret;
891 
892 	ret = pm_runtime_resume_and_get(dev);
893 	if (ret < 0)
894 		return ret;
895 
896 	ret = RTL_R16(tp, reg);
897 
898 	pm_runtime_put_sync(dev);
899 
900 	return ret;
901 }
902 
903 void r8169_get_led_name(struct rtl8169_private *tp, int idx,
904 			char *buf, int buf_len)
905 {
906 	struct pci_dev *pdev = tp->pci_dev;
907 	char pdom[8], pfun[8];
908 	int domain;
909 
910 	domain = pci_domain_nr(pdev->bus);
911 	if (domain)
912 		snprintf(pdom, sizeof(pdom), "P%d", domain);
913 	else
914 		pdom[0] = '\0';
915 
916 	if (pdev->multifunction)
917 		snprintf(pfun, sizeof(pfun), "f%d", PCI_FUNC(pdev->devfn));
918 	else
919 		pfun[0] = '\0';
920 
921 	snprintf(buf, buf_len, "en%sp%ds%d%s-%d::lan", pdom, pdev->bus->number,
922 		 PCI_SLOT(pdev->devfn), pfun, idx);
923 }
924 
925 static void r8168fp_adjust_ocp_cmd(struct rtl8169_private *tp, u32 *cmd, int type)
926 {
927 	/* based on RTL8168FP_OOBMAC_BASE in vendor driver */
928 	if (type == ERIAR_OOB &&
929 	    (tp->mac_version == RTL_GIGA_MAC_VER_52 ||
930 	     tp->mac_version == RTL_GIGA_MAC_VER_53))
931 		*cmd |= 0xf70 << 18;
932 }
933 
934 DECLARE_RTL_COND(rtl_eriar_cond)
935 {
936 	return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
937 }
938 
939 static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
940 			   u32 val, int type)
941 {
942 	u32 cmd = ERIAR_WRITE_CMD | type | mask | addr;
943 
944 	if (WARN(addr & 3 || !mask, "addr: 0x%x, mask: 0x%08x\n", addr, mask))
945 		return;
946 
947 	RTL_W32(tp, ERIDR, val);
948 	r8168fp_adjust_ocp_cmd(tp, &cmd, type);
949 	RTL_W32(tp, ERIAR, cmd);
950 
951 	rtl_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
952 }
953 
954 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
955 			  u32 val)
956 {
957 	_rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
958 }
959 
960 static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
961 {
962 	u32 cmd = ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr;
963 
964 	r8168fp_adjust_ocp_cmd(tp, &cmd, type);
965 	RTL_W32(tp, ERIAR, cmd);
966 
967 	return rtl_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
968 		RTL_R32(tp, ERIDR) : ~0;
969 }
970 
971 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
972 {
973 	return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
974 }
975 
976 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 p, u32 m)
977 {
978 	u32 val = rtl_eri_read(tp, addr);
979 
980 	rtl_eri_write(tp, addr, ERIAR_MASK_1111, (val & ~m) | p);
981 }
982 
983 static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 p)
984 {
985 	rtl_w0w1_eri(tp, addr, p, 0);
986 }
987 
988 static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 m)
989 {
990 	rtl_w0w1_eri(tp, addr, 0, m);
991 }
992 
993 static bool rtl_ocp_reg_failure(u32 reg)
994 {
995 	return WARN_ONCE(reg & 0xffff0001, "Invalid ocp reg %x!\n", reg);
996 }
997 
998 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
999 {
1000 	return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
1001 }
1002 
1003 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1004 {
1005 	if (rtl_ocp_reg_failure(reg))
1006 		return;
1007 
1008 	RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1009 
1010 	rtl_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1011 }
1012 
1013 static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1014 {
1015 	if (rtl_ocp_reg_failure(reg))
1016 		return 0;
1017 
1018 	RTL_W32(tp, GPHY_OCP, reg << 15);
1019 
1020 	return rtl_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1021 		(RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
1022 }
1023 
1024 static void __r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1025 {
1026 	if (rtl_ocp_reg_failure(reg))
1027 		return;
1028 
1029 	RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
1030 }
1031 
1032 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1033 {
1034 	unsigned long flags;
1035 
1036 	raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags);
1037 	__r8168_mac_ocp_write(tp, reg, data);
1038 	raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags);
1039 }
1040 
1041 static u16 __r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1042 {
1043 	if (rtl_ocp_reg_failure(reg))
1044 		return 0;
1045 
1046 	RTL_W32(tp, OCPDR, reg << 15);
1047 
1048 	return RTL_R32(tp, OCPDR);
1049 }
1050 
1051 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1052 {
1053 	unsigned long flags;
1054 	u16 val;
1055 
1056 	raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags);
1057 	val = __r8168_mac_ocp_read(tp, reg);
1058 	raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags);
1059 
1060 	return val;
1061 }
1062 
1063 static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask,
1064 				 u16 set)
1065 {
1066 	unsigned long flags;
1067 	u16 data;
1068 
1069 	raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags);
1070 	data = __r8168_mac_ocp_read(tp, reg);
1071 	__r8168_mac_ocp_write(tp, reg, (data & ~mask) | set);
1072 	raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags);
1073 }
1074 
1075 /* Work around a hw issue with RTL8168g PHY, the quirk disables
1076  * PHY MCU interrupts before PHY power-down.
1077  */
1078 static void rtl8168g_phy_suspend_quirk(struct rtl8169_private *tp, int value)
1079 {
1080 	switch (tp->mac_version) {
1081 	case RTL_GIGA_MAC_VER_40:
1082 		if (value & BMCR_RESET || !(value & BMCR_PDOWN))
1083 			rtl_eri_set_bits(tp, 0x1a8, 0xfc000000);
1084 		else
1085 			rtl_eri_clear_bits(tp, 0x1a8, 0xfc000000);
1086 		break;
1087 	default:
1088 		break;
1089 	}
1090 };
1091 
1092 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1093 {
1094 	if (reg == 0x1f) {
1095 		tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1096 		return;
1097 	}
1098 
1099 	if (tp->ocp_base != OCP_STD_PHY_BASE)
1100 		reg -= 0x10;
1101 
1102 	if (tp->ocp_base == OCP_STD_PHY_BASE && reg == MII_BMCR)
1103 		rtl8168g_phy_suspend_quirk(tp, value);
1104 
1105 	r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1106 }
1107 
1108 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1109 {
1110 	if (reg == 0x1f)
1111 		return tp->ocp_base == OCP_STD_PHY_BASE ? 0 : tp->ocp_base >> 4;
1112 
1113 	if (tp->ocp_base != OCP_STD_PHY_BASE)
1114 		reg -= 0x10;
1115 
1116 	return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1117 }
1118 
1119 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1120 {
1121 	if (reg == 0x1f) {
1122 		tp->ocp_base = value << 4;
1123 		return;
1124 	}
1125 
1126 	r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1127 }
1128 
1129 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1130 {
1131 	return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1132 }
1133 
1134 DECLARE_RTL_COND(rtl_phyar_cond)
1135 {
1136 	return RTL_R32(tp, PHYAR) & 0x80000000;
1137 }
1138 
1139 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1140 {
1141 	RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1142 
1143 	rtl_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1144 	/*
1145 	 * According to hardware specs a 20us delay is required after write
1146 	 * complete indication, but before sending next command.
1147 	 */
1148 	udelay(20);
1149 }
1150 
1151 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1152 {
1153 	int value;
1154 
1155 	RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
1156 
1157 	value = rtl_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1158 		RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
1159 
1160 	/*
1161 	 * According to hardware specs a 20us delay is required after read
1162 	 * complete indication, but before sending next command.
1163 	 */
1164 	udelay(20);
1165 
1166 	return value;
1167 }
1168 
1169 DECLARE_RTL_COND(rtl_ocpar_cond)
1170 {
1171 	return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
1172 }
1173 
1174 #define R8168DP_1_MDIO_ACCESS_BIT	0x00020000
1175 
1176 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
1177 {
1178 	RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1179 }
1180 
1181 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
1182 {
1183 	RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1184 }
1185 
1186 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1187 {
1188 	r8168dp_2_mdio_start(tp);
1189 
1190 	r8169_mdio_write(tp, reg, value);
1191 
1192 	r8168dp_2_mdio_stop(tp);
1193 }
1194 
1195 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1196 {
1197 	int value;
1198 
1199 	/* Work around issue with chip reporting wrong PHY ID */
1200 	if (reg == MII_PHYSID2)
1201 		return 0xc912;
1202 
1203 	r8168dp_2_mdio_start(tp);
1204 
1205 	value = r8169_mdio_read(tp, reg);
1206 
1207 	r8168dp_2_mdio_stop(tp);
1208 
1209 	return value;
1210 }
1211 
1212 static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
1213 {
1214 	switch (tp->mac_version) {
1215 	case RTL_GIGA_MAC_VER_28:
1216 	case RTL_GIGA_MAC_VER_31:
1217 		r8168dp_2_mdio_write(tp, location, val);
1218 		break;
1219 	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_66:
1220 		r8168g_mdio_write(tp, location, val);
1221 		break;
1222 	default:
1223 		r8169_mdio_write(tp, location, val);
1224 		break;
1225 	}
1226 }
1227 
1228 static int rtl_readphy(struct rtl8169_private *tp, int location)
1229 {
1230 	switch (tp->mac_version) {
1231 	case RTL_GIGA_MAC_VER_28:
1232 	case RTL_GIGA_MAC_VER_31:
1233 		return r8168dp_2_mdio_read(tp, location);
1234 	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_66:
1235 		return r8168g_mdio_read(tp, location);
1236 	default:
1237 		return r8169_mdio_read(tp, location);
1238 	}
1239 }
1240 
1241 DECLARE_RTL_COND(rtl_ephyar_cond)
1242 {
1243 	return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1244 }
1245 
1246 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1247 {
1248 	RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1249 		(reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1250 
1251 	rtl_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1252 
1253 	udelay(10);
1254 }
1255 
1256 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1257 {
1258 	RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1259 
1260 	return rtl_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1261 		RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1262 }
1263 
1264 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u16 reg)
1265 {
1266 	RTL_W32(tp, OCPAR, 0x0fu << 12 | (reg & 0x0fff));
1267 	return rtl_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1268 		RTL_R32(tp, OCPDR) : ~0;
1269 }
1270 
1271 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u16 reg)
1272 {
1273 	return _rtl_eri_read(tp, reg, ERIAR_OOB);
1274 }
1275 
1276 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1277 			      u32 data)
1278 {
1279 	RTL_W32(tp, OCPDR, data);
1280 	RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1281 	rtl_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1282 }
1283 
1284 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1285 			      u32 data)
1286 {
1287 	_rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1288 		       data, ERIAR_OOB);
1289 }
1290 
1291 static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
1292 {
1293 	rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
1294 
1295 	r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
1296 }
1297 
1298 #define OOB_CMD_RESET		0x00
1299 #define OOB_CMD_DRIVER_START	0x05
1300 #define OOB_CMD_DRIVER_STOP	0x06
1301 
1302 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1303 {
1304 	return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1305 }
1306 
1307 DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
1308 {
1309 	u16 reg;
1310 
1311 	reg = rtl8168_get_ocp_reg(tp);
1312 
1313 	return r8168dp_ocp_read(tp, reg) & 0x00000800;
1314 }
1315 
1316 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1317 {
1318 	return r8168ep_ocp_read(tp, 0x124) & 0x00000001;
1319 }
1320 
1321 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1322 {
1323 	return RTL_R8(tp, IBISR0) & 0x20;
1324 }
1325 
1326 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1327 {
1328 	RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1329 	rtl_loop_wait_high(tp, &rtl_ocp_tx_cond, 50000, 2000);
1330 	RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1331 	RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1332 }
1333 
1334 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1335 {
1336 	r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1337 	if (tp->dash_enabled)
1338 		rtl_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10000, 10);
1339 }
1340 
1341 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1342 {
1343 	r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1344 	r8168ep_ocp_write(tp, 0x01, 0x30, r8168ep_ocp_read(tp, 0x30) | 0x01);
1345 	if (tp->dash_enabled)
1346 		rtl_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10000, 30);
1347 }
1348 
1349 static void rtl8168_driver_start(struct rtl8169_private *tp)
1350 {
1351 	if (tp->dash_type == RTL_DASH_DP)
1352 		rtl8168dp_driver_start(tp);
1353 	else
1354 		rtl8168ep_driver_start(tp);
1355 }
1356 
1357 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1358 {
1359 	r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1360 	if (tp->dash_enabled)
1361 		rtl_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10000, 10);
1362 }
1363 
1364 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1365 {
1366 	rtl8168ep_stop_cmac(tp);
1367 	r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1368 	r8168ep_ocp_write(tp, 0x01, 0x30, r8168ep_ocp_read(tp, 0x30) | 0x01);
1369 	if (tp->dash_enabled)
1370 		rtl_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10000, 10);
1371 }
1372 
1373 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1374 {
1375 	if (tp->dash_type == RTL_DASH_DP)
1376 		rtl8168dp_driver_stop(tp);
1377 	else
1378 		rtl8168ep_driver_stop(tp);
1379 }
1380 
1381 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1382 {
1383 	u16 reg = rtl8168_get_ocp_reg(tp);
1384 
1385 	return r8168dp_ocp_read(tp, reg) & BIT(15);
1386 }
1387 
1388 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1389 {
1390 	return r8168ep_ocp_read(tp, 0x128) & BIT(0);
1391 }
1392 
1393 static bool rtl_dash_is_enabled(struct rtl8169_private *tp)
1394 {
1395 	switch (tp->dash_type) {
1396 	case RTL_DASH_DP:
1397 		return r8168dp_check_dash(tp);
1398 	case RTL_DASH_EP:
1399 		return r8168ep_check_dash(tp);
1400 	default:
1401 		return false;
1402 	}
1403 }
1404 
1405 static enum rtl_dash_type rtl_get_dash_type(struct rtl8169_private *tp)
1406 {
1407 	switch (tp->mac_version) {
1408 	case RTL_GIGA_MAC_VER_28:
1409 	case RTL_GIGA_MAC_VER_31:
1410 		return RTL_DASH_DP;
1411 	case RTL_GIGA_MAC_VER_51 ... RTL_GIGA_MAC_VER_53:
1412 		return RTL_DASH_EP;
1413 	default:
1414 		return RTL_DASH_NONE;
1415 	}
1416 }
1417 
1418 static void rtl_set_d3_pll_down(struct rtl8169_private *tp, bool enable)
1419 {
1420 	switch (tp->mac_version) {
1421 	case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_26:
1422 	case RTL_GIGA_MAC_VER_29 ... RTL_GIGA_MAC_VER_30:
1423 	case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_37:
1424 	case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_66:
1425 		if (enable)
1426 			RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~D3_NO_PLL_DOWN);
1427 		else
1428 			RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | D3_NO_PLL_DOWN);
1429 		break;
1430 	default:
1431 		break;
1432 	}
1433 }
1434 
1435 static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1436 {
1437 	rtl_eri_clear_bits(tp, 0xdc, BIT(0));
1438 	rtl_eri_set_bits(tp, 0xdc, BIT(0));
1439 }
1440 
1441 DECLARE_RTL_COND(rtl_efusear_cond)
1442 {
1443 	return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1444 }
1445 
1446 u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1447 {
1448 	RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1449 
1450 	return rtl_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1451 		RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1452 }
1453 
1454 static u32 rtl_get_events(struct rtl8169_private *tp)
1455 {
1456 	if (rtl_is_8125(tp))
1457 		return RTL_R32(tp, IntrStatus_8125);
1458 	else
1459 		return RTL_R16(tp, IntrStatus);
1460 }
1461 
1462 static void rtl_ack_events(struct rtl8169_private *tp, u32 bits)
1463 {
1464 	if (rtl_is_8125(tp))
1465 		RTL_W32(tp, IntrStatus_8125, bits);
1466 	else
1467 		RTL_W16(tp, IntrStatus, bits);
1468 }
1469 
1470 static void rtl_irq_disable(struct rtl8169_private *tp)
1471 {
1472 	if (rtl_is_8125(tp))
1473 		RTL_W32(tp, IntrMask_8125, 0);
1474 	else
1475 		RTL_W16(tp, IntrMask, 0);
1476 }
1477 
1478 static void rtl_irq_enable(struct rtl8169_private *tp)
1479 {
1480 	if (rtl_is_8125(tp))
1481 		RTL_W32(tp, IntrMask_8125, tp->irq_mask);
1482 	else
1483 		RTL_W16(tp, IntrMask, tp->irq_mask);
1484 }
1485 
1486 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1487 {
1488 	rtl_irq_disable(tp);
1489 	rtl_ack_events(tp, 0xffffffff);
1490 	rtl_pci_commit(tp);
1491 }
1492 
1493 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1494 {
1495 	struct phy_device *phydev = tp->phydev;
1496 
1497 	if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1498 	    tp->mac_version == RTL_GIGA_MAC_VER_38) {
1499 		if (phydev->speed == SPEED_1000) {
1500 			rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1501 			rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1502 		} else if (phydev->speed == SPEED_100) {
1503 			rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1504 			rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1505 		} else {
1506 			rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1507 			rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
1508 		}
1509 		rtl_reset_packet_filter(tp);
1510 	} else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1511 		   tp->mac_version == RTL_GIGA_MAC_VER_36) {
1512 		if (phydev->speed == SPEED_1000) {
1513 			rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1514 			rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1515 		} else {
1516 			rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1517 			rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
1518 		}
1519 	} else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1520 		if (phydev->speed == SPEED_10) {
1521 			rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1522 			rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
1523 		} else {
1524 			rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
1525 		}
1526 	}
1527 }
1528 
1529 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1530 
1531 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1532 {
1533 	struct rtl8169_private *tp = netdev_priv(dev);
1534 
1535 	wol->supported = WAKE_ANY;
1536 	wol->wolopts = tp->saved_wolopts;
1537 }
1538 
1539 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1540 {
1541 	static const struct {
1542 		u32 opt;
1543 		u16 reg;
1544 		u8  mask;
1545 	} cfg[] = {
1546 		{ WAKE_PHY,   Config3, LinkUp },
1547 		{ WAKE_UCAST, Config5, UWF },
1548 		{ WAKE_BCAST, Config5, BWF },
1549 		{ WAKE_MCAST, Config5, MWF },
1550 		{ WAKE_ANY,   Config5, LanWake },
1551 		{ WAKE_MAGIC, Config3, MagicPacket }
1552 	};
1553 	unsigned int i, tmp = ARRAY_SIZE(cfg);
1554 	u8 options;
1555 
1556 	rtl_unlock_config_regs(tp);
1557 
1558 	if (rtl_is_8168evl_up(tp)) {
1559 		tmp--;
1560 		if (wolopts & WAKE_MAGIC)
1561 			rtl_eri_set_bits(tp, 0x0dc, MagicPacket_v2);
1562 		else
1563 			rtl_eri_clear_bits(tp, 0x0dc, MagicPacket_v2);
1564 	} else if (rtl_is_8125(tp)) {
1565 		tmp--;
1566 		if (wolopts & WAKE_MAGIC)
1567 			r8168_mac_ocp_modify(tp, 0xc0b6, 0, BIT(0));
1568 		else
1569 			r8168_mac_ocp_modify(tp, 0xc0b6, BIT(0), 0);
1570 	}
1571 
1572 	for (i = 0; i < tmp; i++) {
1573 		options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1574 		if (wolopts & cfg[i].opt)
1575 			options |= cfg[i].mask;
1576 		RTL_W8(tp, cfg[i].reg, options);
1577 	}
1578 
1579 	switch (tp->mac_version) {
1580 	case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
1581 		options = RTL_R8(tp, Config1) & ~PMEnable;
1582 		if (wolopts)
1583 			options |= PMEnable;
1584 		RTL_W8(tp, Config1, options);
1585 		break;
1586 	case RTL_GIGA_MAC_VER_34:
1587 	case RTL_GIGA_MAC_VER_37:
1588 	case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_66:
1589 		if (wolopts)
1590 			rtl_mod_config2(tp, 0, PME_SIGNAL);
1591 		else
1592 			rtl_mod_config2(tp, PME_SIGNAL, 0);
1593 		break;
1594 	default:
1595 		break;
1596 	}
1597 
1598 	rtl_lock_config_regs(tp);
1599 
1600 	device_set_wakeup_enable(tp_to_dev(tp), wolopts);
1601 
1602 	if (!tp->dash_enabled) {
1603 		rtl_set_d3_pll_down(tp, !wolopts);
1604 		tp->dev->ethtool->wol_enabled = wolopts ? 1 : 0;
1605 	}
1606 }
1607 
1608 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1609 {
1610 	struct rtl8169_private *tp = netdev_priv(dev);
1611 
1612 	if (wol->wolopts & ~WAKE_ANY)
1613 		return -EINVAL;
1614 
1615 	tp->saved_wolopts = wol->wolopts;
1616 	__rtl8169_set_wol(tp, tp->saved_wolopts);
1617 
1618 	return 0;
1619 }
1620 
1621 static void rtl8169_get_drvinfo(struct net_device *dev,
1622 				struct ethtool_drvinfo *info)
1623 {
1624 	struct rtl8169_private *tp = netdev_priv(dev);
1625 	struct rtl_fw *rtl_fw = tp->rtl_fw;
1626 
1627 	strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1628 	strscpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1629 	BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1630 	if (rtl_fw)
1631 		strscpy(info->fw_version, rtl_fw->version,
1632 			sizeof(info->fw_version));
1633 }
1634 
1635 static int rtl8169_get_regs_len(struct net_device *dev)
1636 {
1637 	return R8169_REGS_SIZE;
1638 }
1639 
1640 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1641 	netdev_features_t features)
1642 {
1643 	struct rtl8169_private *tp = netdev_priv(dev);
1644 
1645 	if (dev->mtu > TD_MSS_MAX)
1646 		features &= ~NETIF_F_ALL_TSO;
1647 
1648 	if (dev->mtu > ETH_DATA_LEN &&
1649 	    tp->mac_version > RTL_GIGA_MAC_VER_06)
1650 		features &= ~(NETIF_F_CSUM_MASK | NETIF_F_ALL_TSO);
1651 
1652 	return features;
1653 }
1654 
1655 static void rtl_set_rx_config_features(struct rtl8169_private *tp,
1656 				       netdev_features_t features)
1657 {
1658 	u32 rx_config = RTL_R32(tp, RxConfig);
1659 
1660 	if (features & NETIF_F_RXALL)
1661 		rx_config |= RX_CONFIG_ACCEPT_ERR_MASK;
1662 	else
1663 		rx_config &= ~RX_CONFIG_ACCEPT_ERR_MASK;
1664 
1665 	if (rtl_is_8125(tp)) {
1666 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
1667 			rx_config |= RX_VLAN_8125;
1668 		else
1669 			rx_config &= ~RX_VLAN_8125;
1670 	}
1671 
1672 	RTL_W32(tp, RxConfig, rx_config);
1673 }
1674 
1675 static int rtl8169_set_features(struct net_device *dev,
1676 				netdev_features_t features)
1677 {
1678 	struct rtl8169_private *tp = netdev_priv(dev);
1679 
1680 	rtl_set_rx_config_features(tp, features);
1681 
1682 	if (features & NETIF_F_RXCSUM)
1683 		tp->cp_cmd |= RxChkSum;
1684 	else
1685 		tp->cp_cmd &= ~RxChkSum;
1686 
1687 	if (!rtl_is_8125(tp)) {
1688 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
1689 			tp->cp_cmd |= RxVlan;
1690 		else
1691 			tp->cp_cmd &= ~RxVlan;
1692 	}
1693 
1694 	RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1695 	rtl_pci_commit(tp);
1696 
1697 	return 0;
1698 }
1699 
1700 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1701 {
1702 	return (skb_vlan_tag_present(skb)) ?
1703 		TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1704 }
1705 
1706 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1707 {
1708 	u32 opts2 = le32_to_cpu(desc->opts2);
1709 
1710 	if (opts2 & RxVlanTag)
1711 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1712 }
1713 
1714 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1715 			     void *p)
1716 {
1717 	struct rtl8169_private *tp = netdev_priv(dev);
1718 	u32 __iomem *data = tp->mmio_addr;
1719 	u32 *dw = p;
1720 	int i;
1721 
1722 	for (i = 0; i < R8169_REGS_SIZE; i += 4)
1723 		memcpy_fromio(dw++, data++, 4);
1724 }
1725 
1726 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1727 	"tx_packets",
1728 	"rx_packets",
1729 	"tx_errors",
1730 	"rx_errors",
1731 	"rx_missed",
1732 	"align_errors",
1733 	"tx_single_collisions",
1734 	"tx_multi_collisions",
1735 	"unicast",
1736 	"broadcast",
1737 	"multicast",
1738 	"tx_aborted",
1739 	"tx_underrun",
1740 };
1741 
1742 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1743 {
1744 	switch (sset) {
1745 	case ETH_SS_STATS:
1746 		return ARRAY_SIZE(rtl8169_gstrings);
1747 	default:
1748 		return -EOPNOTSUPP;
1749 	}
1750 }
1751 
1752 DECLARE_RTL_COND(rtl_counters_cond)
1753 {
1754 	return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
1755 }
1756 
1757 static void rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
1758 {
1759 	u32 cmd = lower_32_bits(tp->counters_phys_addr);
1760 
1761 	RTL_W32(tp, CounterAddrHigh, upper_32_bits(tp->counters_phys_addr));
1762 	rtl_pci_commit(tp);
1763 	RTL_W32(tp, CounterAddrLow, cmd);
1764 	RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
1765 
1766 	rtl_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
1767 }
1768 
1769 static void rtl8169_update_counters(struct rtl8169_private *tp)
1770 {
1771 	u8 val = RTL_R8(tp, ChipCmd);
1772 
1773 	/*
1774 	 * Some chips are unable to dump tally counters when the receiver
1775 	 * is disabled. If 0xff chip may be in a PCI power-save state.
1776 	 */
1777 	if (val & CmdRxEnb && val != 0xff)
1778 		rtl8169_do_counters(tp, CounterDump);
1779 }
1780 
1781 static void rtl8169_init_counter_offsets(struct rtl8169_private *tp)
1782 {
1783 	struct rtl8169_counters *counters = tp->counters;
1784 
1785 	/*
1786 	 * rtl8169_init_counter_offsets is called from rtl_open.  On chip
1787 	 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1788 	 * reset by a power cycle, while the counter values collected by the
1789 	 * driver are reset at every driver unload/load cycle.
1790 	 *
1791 	 * To make sure the HW values returned by @get_stats64 match the SW
1792 	 * values, we collect the initial values at first open(*) and use them
1793 	 * as offsets to normalize the values returned by @get_stats64.
1794 	 *
1795 	 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1796 	 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1797 	 * set at open time by rtl_hw_start.
1798 	 */
1799 
1800 	if (tp->tc_offset.inited)
1801 		return;
1802 
1803 	if (tp->mac_version >= RTL_GIGA_MAC_VER_19) {
1804 		rtl8169_do_counters(tp, CounterReset);
1805 	} else {
1806 		rtl8169_update_counters(tp);
1807 		tp->tc_offset.tx_errors = counters->tx_errors;
1808 		tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1809 		tp->tc_offset.tx_aborted = counters->tx_aborted;
1810 		tp->tc_offset.rx_missed = counters->rx_missed;
1811 	}
1812 
1813 	tp->tc_offset.inited = true;
1814 }
1815 
1816 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1817 				      struct ethtool_stats *stats, u64 *data)
1818 {
1819 	struct rtl8169_private *tp = netdev_priv(dev);
1820 	struct rtl8169_counters *counters;
1821 
1822 	counters = tp->counters;
1823 	rtl8169_update_counters(tp);
1824 
1825 	data[0] = le64_to_cpu(counters->tx_packets);
1826 	data[1] = le64_to_cpu(counters->rx_packets);
1827 	data[2] = le64_to_cpu(counters->tx_errors);
1828 	data[3] = le32_to_cpu(counters->rx_errors);
1829 	data[4] = le16_to_cpu(counters->rx_missed);
1830 	data[5] = le16_to_cpu(counters->align_errors);
1831 	data[6] = le32_to_cpu(counters->tx_one_collision);
1832 	data[7] = le32_to_cpu(counters->tx_multi_collision);
1833 	data[8] = le64_to_cpu(counters->rx_unicast);
1834 	data[9] = le64_to_cpu(counters->rx_broadcast);
1835 	data[10] = le32_to_cpu(counters->rx_multicast);
1836 	data[11] = le16_to_cpu(counters->tx_aborted);
1837 	data[12] = le16_to_cpu(counters->tx_underrun);
1838 }
1839 
1840 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1841 {
1842 	switch(stringset) {
1843 	case ETH_SS_STATS:
1844 		memcpy(data, rtl8169_gstrings, sizeof(rtl8169_gstrings));
1845 		break;
1846 	}
1847 }
1848 
1849 /*
1850  * Interrupt coalescing
1851  *
1852  * > 1 - the availability of the IntrMitigate (0xe2) register through the
1853  * >     8169, 8168 and 810x line of chipsets
1854  *
1855  * 8169, 8168, and 8136(810x) serial chipsets support it.
1856  *
1857  * > 2 - the Tx timer unit at gigabit speed
1858  *
1859  * The unit of the timer depends on both the speed and the setting of CPlusCmd
1860  * (0xe0) bit 1 and bit 0.
1861  *
1862  * For 8169
1863  * bit[1:0] \ speed        1000M           100M            10M
1864  * 0 0                     320ns           2.56us          40.96us
1865  * 0 1                     2.56us          20.48us         327.7us
1866  * 1 0                     5.12us          40.96us         655.4us
1867  * 1 1                     10.24us         81.92us         1.31ms
1868  *
1869  * For the other
1870  * bit[1:0] \ speed        1000M           100M            10M
1871  * 0 0                     5us             2.56us          40.96us
1872  * 0 1                     40us            20.48us         327.7us
1873  * 1 0                     80us            40.96us         655.4us
1874  * 1 1                     160us           81.92us         1.31ms
1875  */
1876 
1877 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
1878 struct rtl_coalesce_info {
1879 	u32 speed;
1880 	u32 scale_nsecs[4];
1881 };
1882 
1883 /* produce array with base delay *1, *8, *8*2, *8*2*2 */
1884 #define COALESCE_DELAY(d) { (d), 8 * (d), 16 * (d), 32 * (d) }
1885 
1886 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1887 	{ SPEED_1000,	COALESCE_DELAY(320) },
1888 	{ SPEED_100,	COALESCE_DELAY(2560) },
1889 	{ SPEED_10,	COALESCE_DELAY(40960) },
1890 	{ 0 },
1891 };
1892 
1893 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1894 	{ SPEED_1000,	COALESCE_DELAY(5000) },
1895 	{ SPEED_100,	COALESCE_DELAY(2560) },
1896 	{ SPEED_10,	COALESCE_DELAY(40960) },
1897 	{ 0 },
1898 };
1899 #undef COALESCE_DELAY
1900 
1901 /* get rx/tx scale vector corresponding to current speed */
1902 static const struct rtl_coalesce_info *
1903 rtl_coalesce_info(struct rtl8169_private *tp)
1904 {
1905 	const struct rtl_coalesce_info *ci;
1906 
1907 	if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1908 		ci = rtl_coalesce_info_8169;
1909 	else
1910 		ci = rtl_coalesce_info_8168_8136;
1911 
1912 	/* if speed is unknown assume highest one */
1913 	if (tp->phydev->speed == SPEED_UNKNOWN)
1914 		return ci;
1915 
1916 	for (; ci->speed; ci++) {
1917 		if (tp->phydev->speed == ci->speed)
1918 			return ci;
1919 	}
1920 
1921 	return ERR_PTR(-ELNRNG);
1922 }
1923 
1924 static int rtl_get_coalesce(struct net_device *dev,
1925 			    struct ethtool_coalesce *ec,
1926 			    struct kernel_ethtool_coalesce *kernel_coal,
1927 			    struct netlink_ext_ack *extack)
1928 {
1929 	struct rtl8169_private *tp = netdev_priv(dev);
1930 	const struct rtl_coalesce_info *ci;
1931 	u32 scale, c_us, c_fr;
1932 	u16 intrmit;
1933 
1934 	if (rtl_is_8125(tp))
1935 		return -EOPNOTSUPP;
1936 
1937 	memset(ec, 0, sizeof(*ec));
1938 
1939 	/* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1940 	ci = rtl_coalesce_info(tp);
1941 	if (IS_ERR(ci))
1942 		return PTR_ERR(ci);
1943 
1944 	scale = ci->scale_nsecs[tp->cp_cmd & INTT_MASK];
1945 
1946 	intrmit = RTL_R16(tp, IntrMitigate);
1947 
1948 	c_us = FIELD_GET(RTL_COALESCE_TX_USECS, intrmit);
1949 	ec->tx_coalesce_usecs = DIV_ROUND_UP(c_us * scale, 1000);
1950 
1951 	c_fr = FIELD_GET(RTL_COALESCE_TX_FRAMES, intrmit);
1952 	/* ethtool_coalesce states usecs and max_frames must not both be 0 */
1953 	ec->tx_max_coalesced_frames = (c_us || c_fr) ? c_fr * 4 : 1;
1954 
1955 	c_us = FIELD_GET(RTL_COALESCE_RX_USECS, intrmit);
1956 	ec->rx_coalesce_usecs = DIV_ROUND_UP(c_us * scale, 1000);
1957 
1958 	c_fr = FIELD_GET(RTL_COALESCE_RX_FRAMES, intrmit);
1959 	ec->rx_max_coalesced_frames = (c_us || c_fr) ? c_fr * 4 : 1;
1960 
1961 	return 0;
1962 }
1963 
1964 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, usec) */
1965 static int rtl_coalesce_choose_scale(struct rtl8169_private *tp, u32 usec,
1966 				     u16 *cp01)
1967 {
1968 	const struct rtl_coalesce_info *ci;
1969 	u16 i;
1970 
1971 	ci = rtl_coalesce_info(tp);
1972 	if (IS_ERR(ci))
1973 		return PTR_ERR(ci);
1974 
1975 	for (i = 0; i < 4; i++) {
1976 		if (usec <= ci->scale_nsecs[i] * RTL_COALESCE_T_MAX / 1000U) {
1977 			*cp01 = i;
1978 			return ci->scale_nsecs[i];
1979 		}
1980 	}
1981 
1982 	return -ERANGE;
1983 }
1984 
1985 static int rtl_set_coalesce(struct net_device *dev,
1986 			    struct ethtool_coalesce *ec,
1987 			    struct kernel_ethtool_coalesce *kernel_coal,
1988 			    struct netlink_ext_ack *extack)
1989 {
1990 	struct rtl8169_private *tp = netdev_priv(dev);
1991 	u32 tx_fr = ec->tx_max_coalesced_frames;
1992 	u32 rx_fr = ec->rx_max_coalesced_frames;
1993 	u32 coal_usec_max, units;
1994 	u16 w = 0, cp01 = 0;
1995 	int scale;
1996 
1997 	if (rtl_is_8125(tp))
1998 		return -EOPNOTSUPP;
1999 
2000 	if (rx_fr > RTL_COALESCE_FRAME_MAX || tx_fr > RTL_COALESCE_FRAME_MAX)
2001 		return -ERANGE;
2002 
2003 	coal_usec_max = max(ec->rx_coalesce_usecs, ec->tx_coalesce_usecs);
2004 	scale = rtl_coalesce_choose_scale(tp, coal_usec_max, &cp01);
2005 	if (scale < 0)
2006 		return scale;
2007 
2008 	/* Accept max_frames=1 we returned in rtl_get_coalesce. Accept it
2009 	 * not only when usecs=0 because of e.g. the following scenario:
2010 	 *
2011 	 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2012 	 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2013 	 * - then user does `ethtool -C eth0 rx-usecs 100`
2014 	 *
2015 	 * Since ethtool sends to kernel whole ethtool_coalesce settings,
2016 	 * if we want to ignore rx_frames then it has to be set to 0.
2017 	 */
2018 	if (rx_fr == 1)
2019 		rx_fr = 0;
2020 	if (tx_fr == 1)
2021 		tx_fr = 0;
2022 
2023 	/* HW requires time limit to be set if frame limit is set */
2024 	if ((tx_fr && !ec->tx_coalesce_usecs) ||
2025 	    (rx_fr && !ec->rx_coalesce_usecs))
2026 		return -EINVAL;
2027 
2028 	w |= FIELD_PREP(RTL_COALESCE_TX_FRAMES, DIV_ROUND_UP(tx_fr, 4));
2029 	w |= FIELD_PREP(RTL_COALESCE_RX_FRAMES, DIV_ROUND_UP(rx_fr, 4));
2030 
2031 	units = DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000U, scale);
2032 	w |= FIELD_PREP(RTL_COALESCE_TX_USECS, units);
2033 	units = DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000U, scale);
2034 	w |= FIELD_PREP(RTL_COALESCE_RX_USECS, units);
2035 
2036 	RTL_W16(tp, IntrMitigate, w);
2037 
2038 	/* Meaning of PktCntrDisable bit changed from RTL8168e-vl */
2039 	if (rtl_is_8168evl_up(tp)) {
2040 		if (!rx_fr && !tx_fr)
2041 			/* disable packet counter */
2042 			tp->cp_cmd |= PktCntrDisable;
2043 		else
2044 			tp->cp_cmd &= ~PktCntrDisable;
2045 	}
2046 
2047 	tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
2048 	RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2049 	rtl_pci_commit(tp);
2050 
2051 	return 0;
2052 }
2053 
2054 static void rtl_set_eee_txidle_timer(struct rtl8169_private *tp)
2055 {
2056 	unsigned int timer_val = READ_ONCE(tp->dev->mtu) + ETH_HLEN + 0x20;
2057 
2058 	switch (tp->mac_version) {
2059 	case RTL_GIGA_MAC_VER_46:
2060 	case RTL_GIGA_MAC_VER_48:
2061 		tp->tx_lpi_timer = timer_val;
2062 		r8168_mac_ocp_write(tp, 0xe048, timer_val);
2063 		break;
2064 	case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_66:
2065 		tp->tx_lpi_timer = timer_val;
2066 		RTL_W16(tp, EEE_TXIDLE_TIMER_8125, timer_val);
2067 		break;
2068 	default:
2069 		break;
2070 	}
2071 }
2072 
2073 static unsigned int r8169_get_tx_lpi_timer_us(struct rtl8169_private *tp)
2074 {
2075 	unsigned int speed = tp->phydev->speed;
2076 	unsigned int timer = tp->tx_lpi_timer;
2077 
2078 	if (!timer || speed == SPEED_UNKNOWN)
2079 		return 0;
2080 
2081 	/* tx_lpi_timer value is in bytes */
2082 	return DIV_ROUND_CLOSEST(timer * BITS_PER_BYTE, speed);
2083 }
2084 
2085 static int rtl8169_get_eee(struct net_device *dev, struct ethtool_keee *data)
2086 {
2087 	struct rtl8169_private *tp = netdev_priv(dev);
2088 	int ret;
2089 
2090 	if (!rtl_supports_eee(tp))
2091 		return -EOPNOTSUPP;
2092 
2093 	ret = phy_ethtool_get_eee(tp->phydev, data);
2094 	if (ret)
2095 		return ret;
2096 
2097 	data->tx_lpi_timer = r8169_get_tx_lpi_timer_us(tp);
2098 
2099 	return 0;
2100 }
2101 
2102 static int rtl8169_set_eee(struct net_device *dev, struct ethtool_keee *data)
2103 {
2104 	struct rtl8169_private *tp = netdev_priv(dev);
2105 
2106 	if (!rtl_supports_eee(tp))
2107 		return -EOPNOTSUPP;
2108 
2109 	return phy_ethtool_set_eee(tp->phydev, data);
2110 }
2111 
2112 static void rtl8169_get_ringparam(struct net_device *dev,
2113 				  struct ethtool_ringparam *data,
2114 				  struct kernel_ethtool_ringparam *kernel_data,
2115 				  struct netlink_ext_ack *extack)
2116 {
2117 	data->rx_max_pending = NUM_RX_DESC;
2118 	data->rx_pending = NUM_RX_DESC;
2119 	data->tx_max_pending = NUM_TX_DESC;
2120 	data->tx_pending = NUM_TX_DESC;
2121 }
2122 
2123 static void rtl8169_get_pause_stats(struct net_device *dev,
2124 				    struct ethtool_pause_stats *pause_stats)
2125 {
2126 	struct rtl8169_private *tp = netdev_priv(dev);
2127 
2128 	if (!rtl_is_8125(tp))
2129 		return;
2130 
2131 	rtl8169_update_counters(tp);
2132 	pause_stats->tx_pause_frames = le32_to_cpu(tp->counters->tx_pause_on);
2133 	pause_stats->rx_pause_frames = le32_to_cpu(tp->counters->rx_pause_on);
2134 }
2135 
2136 static void rtl8169_get_pauseparam(struct net_device *dev,
2137 				   struct ethtool_pauseparam *data)
2138 {
2139 	struct rtl8169_private *tp = netdev_priv(dev);
2140 	bool tx_pause, rx_pause;
2141 
2142 	phy_get_pause(tp->phydev, &tx_pause, &rx_pause);
2143 
2144 	data->autoneg = tp->phydev->autoneg;
2145 	data->tx_pause = tx_pause ? 1 : 0;
2146 	data->rx_pause = rx_pause ? 1 : 0;
2147 }
2148 
2149 static int rtl8169_set_pauseparam(struct net_device *dev,
2150 				  struct ethtool_pauseparam *data)
2151 {
2152 	struct rtl8169_private *tp = netdev_priv(dev);
2153 
2154 	if (dev->mtu > ETH_DATA_LEN)
2155 		return -EOPNOTSUPP;
2156 
2157 	phy_set_asym_pause(tp->phydev, data->rx_pause, data->tx_pause);
2158 
2159 	return 0;
2160 }
2161 
2162 static void rtl8169_get_eth_mac_stats(struct net_device *dev,
2163 				      struct ethtool_eth_mac_stats *mac_stats)
2164 {
2165 	struct rtl8169_private *tp = netdev_priv(dev);
2166 
2167 	rtl8169_update_counters(tp);
2168 
2169 	mac_stats->FramesTransmittedOK =
2170 		le64_to_cpu(tp->counters->tx_packets);
2171 	mac_stats->SingleCollisionFrames =
2172 		le32_to_cpu(tp->counters->tx_one_collision);
2173 	mac_stats->MultipleCollisionFrames =
2174 		le32_to_cpu(tp->counters->tx_multi_collision);
2175 	mac_stats->FramesReceivedOK =
2176 		le64_to_cpu(tp->counters->rx_packets);
2177 	mac_stats->AlignmentErrors =
2178 		le16_to_cpu(tp->counters->align_errors);
2179 	mac_stats->FramesLostDueToIntMACXmitError =
2180 		le64_to_cpu(tp->counters->tx_errors);
2181 	mac_stats->BroadcastFramesReceivedOK =
2182 		le64_to_cpu(tp->counters->rx_broadcast);
2183 	mac_stats->MulticastFramesReceivedOK =
2184 		le32_to_cpu(tp->counters->rx_multicast);
2185 
2186 	if (!rtl_is_8125(tp))
2187 		return;
2188 
2189 	mac_stats->AlignmentErrors =
2190 		le32_to_cpu(tp->counters->align_errors32);
2191 	mac_stats->OctetsTransmittedOK =
2192 		le64_to_cpu(tp->counters->tx_octets);
2193 	mac_stats->LateCollisions =
2194 		le32_to_cpu(tp->counters->tx_late_collision);
2195 	mac_stats->FramesAbortedDueToXSColls =
2196 		le32_to_cpu(tp->counters->tx_aborted32);
2197 	mac_stats->OctetsReceivedOK =
2198 		le64_to_cpu(tp->counters->rx_octets);
2199 	mac_stats->FramesLostDueToIntMACRcvError =
2200 		le32_to_cpu(tp->counters->rx_mac_error);
2201 	mac_stats->MulticastFramesXmittedOK =
2202 		le64_to_cpu(tp->counters->tx_multicast64);
2203 	mac_stats->BroadcastFramesXmittedOK =
2204 		le64_to_cpu(tp->counters->tx_broadcast64);
2205 	mac_stats->MulticastFramesReceivedOK =
2206 		le64_to_cpu(tp->counters->rx_multicast64);
2207 	mac_stats->FrameTooLongErrors =
2208 		le32_to_cpu(tp->counters->rx_frame_too_long);
2209 }
2210 
2211 static void rtl8169_get_eth_ctrl_stats(struct net_device *dev,
2212 				       struct ethtool_eth_ctrl_stats *ctrl_stats)
2213 {
2214 	struct rtl8169_private *tp = netdev_priv(dev);
2215 
2216 	if (!rtl_is_8125(tp))
2217 		return;
2218 
2219 	rtl8169_update_counters(tp);
2220 
2221 	ctrl_stats->UnsupportedOpcodesReceived =
2222 		le32_to_cpu(tp->counters->rx_unknown_opcode);
2223 }
2224 
2225 static const struct ethtool_ops rtl8169_ethtool_ops = {
2226 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2227 				     ETHTOOL_COALESCE_MAX_FRAMES,
2228 	.get_drvinfo		= rtl8169_get_drvinfo,
2229 	.get_regs_len		= rtl8169_get_regs_len,
2230 	.get_link		= ethtool_op_get_link,
2231 	.get_coalesce		= rtl_get_coalesce,
2232 	.set_coalesce		= rtl_set_coalesce,
2233 	.get_regs		= rtl8169_get_regs,
2234 	.get_wol		= rtl8169_get_wol,
2235 	.set_wol		= rtl8169_set_wol,
2236 	.get_strings		= rtl8169_get_strings,
2237 	.get_sset_count		= rtl8169_get_sset_count,
2238 	.get_ethtool_stats	= rtl8169_get_ethtool_stats,
2239 	.get_ts_info		= ethtool_op_get_ts_info,
2240 	.nway_reset		= phy_ethtool_nway_reset,
2241 	.get_eee		= rtl8169_get_eee,
2242 	.set_eee		= rtl8169_set_eee,
2243 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
2244 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
2245 	.get_ringparam		= rtl8169_get_ringparam,
2246 	.get_pause_stats	= rtl8169_get_pause_stats,
2247 	.get_pauseparam		= rtl8169_get_pauseparam,
2248 	.set_pauseparam		= rtl8169_set_pauseparam,
2249 	.get_eth_mac_stats	= rtl8169_get_eth_mac_stats,
2250 	.get_eth_ctrl_stats	= rtl8169_get_eth_ctrl_stats,
2251 };
2252 
2253 static enum mac_version rtl8169_get_mac_version(u16 xid, bool gmii)
2254 {
2255 	/*
2256 	 * The driver currently handles the 8168Bf and the 8168Be identically
2257 	 * but they can be identified more specifically through the test below
2258 	 * if needed:
2259 	 *
2260 	 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2261 	 *
2262 	 * Same thing for the 8101Eb and the 8101Ec:
2263 	 *
2264 	 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2265 	 */
2266 	static const struct rtl_mac_info {
2267 		u16 mask;
2268 		u16 val;
2269 		enum mac_version ver;
2270 	} mac_info[] = {
2271 		/* 8126A family. */
2272 		{ 0x7cf, 0x64a,	RTL_GIGA_MAC_VER_66 },
2273 		{ 0x7cf, 0x649,	RTL_GIGA_MAC_VER_65 },
2274 
2275 		/* 8125D family. */
2276 		{ 0x7cf, 0x688,	RTL_GIGA_MAC_VER_64 },
2277 
2278 		/* 8125B family. */
2279 		{ 0x7cf, 0x641,	RTL_GIGA_MAC_VER_63 },
2280 
2281 		/* 8125A family. */
2282 		{ 0x7cf, 0x609,	RTL_GIGA_MAC_VER_61 },
2283 		/* It seems only XID 609 made it to the mass market.
2284 		 * { 0x7cf, 0x608,	RTL_GIGA_MAC_VER_60 },
2285 		 * { 0x7c8, 0x608,	RTL_GIGA_MAC_VER_61 },
2286 		 */
2287 
2288 		/* RTL8117 */
2289 		{ 0x7cf, 0x54b,	RTL_GIGA_MAC_VER_53 },
2290 		{ 0x7cf, 0x54a,	RTL_GIGA_MAC_VER_52 },
2291 
2292 		/* 8168EP family. */
2293 		{ 0x7cf, 0x502,	RTL_GIGA_MAC_VER_51 },
2294 		/* It seems this chip version never made it to
2295 		 * the wild. Let's disable detection.
2296 		 * { 0x7cf, 0x501,      RTL_GIGA_MAC_VER_50 },
2297 		 * { 0x7cf, 0x500,      RTL_GIGA_MAC_VER_49 },
2298 		 */
2299 
2300 		/* 8168H family. */
2301 		{ 0x7cf, 0x541,	RTL_GIGA_MAC_VER_46 },
2302 		/* It seems this chip version never made it to
2303 		 * the wild. Let's disable detection.
2304 		 * { 0x7cf, 0x540,	RTL_GIGA_MAC_VER_45 },
2305 		 */
2306 		/* Realtek calls it RTL8168M, but it's handled like RTL8168H */
2307 		{ 0x7cf, 0x6c0,	RTL_GIGA_MAC_VER_46 },
2308 
2309 		/* 8168G family. */
2310 		{ 0x7cf, 0x5c8,	RTL_GIGA_MAC_VER_44 },
2311 		{ 0x7cf, 0x509,	RTL_GIGA_MAC_VER_42 },
2312 		/* It seems this chip version never made it to
2313 		 * the wild. Let's disable detection.
2314 		 * { 0x7cf, 0x4c1,	RTL_GIGA_MAC_VER_41 },
2315 		 */
2316 		{ 0x7cf, 0x4c0,	RTL_GIGA_MAC_VER_40 },
2317 
2318 		/* 8168F family. */
2319 		{ 0x7c8, 0x488,	RTL_GIGA_MAC_VER_38 },
2320 		{ 0x7cf, 0x481,	RTL_GIGA_MAC_VER_36 },
2321 		{ 0x7cf, 0x480,	RTL_GIGA_MAC_VER_35 },
2322 
2323 		/* 8168E family. */
2324 		{ 0x7c8, 0x2c8,	RTL_GIGA_MAC_VER_34 },
2325 		{ 0x7cf, 0x2c1,	RTL_GIGA_MAC_VER_32 },
2326 		{ 0x7c8, 0x2c0,	RTL_GIGA_MAC_VER_33 },
2327 
2328 		/* 8168D family. */
2329 		{ 0x7cf, 0x281,	RTL_GIGA_MAC_VER_25 },
2330 		{ 0x7c8, 0x280,	RTL_GIGA_MAC_VER_26 },
2331 
2332 		/* 8168DP family. */
2333 		/* It seems this early RTL8168dp version never made it to
2334 		 * the wild. Support has been removed.
2335 		 * { 0x7cf, 0x288,      RTL_GIGA_MAC_VER_27 },
2336 		 */
2337 		{ 0x7cf, 0x28a,	RTL_GIGA_MAC_VER_28 },
2338 		{ 0x7cf, 0x28b,	RTL_GIGA_MAC_VER_31 },
2339 
2340 		/* 8168C family. */
2341 		{ 0x7cf, 0x3c9,	RTL_GIGA_MAC_VER_23 },
2342 		{ 0x7cf, 0x3c8,	RTL_GIGA_MAC_VER_18 },
2343 		{ 0x7c8, 0x3c8,	RTL_GIGA_MAC_VER_24 },
2344 		{ 0x7cf, 0x3c0,	RTL_GIGA_MAC_VER_19 },
2345 		{ 0x7cf, 0x3c2,	RTL_GIGA_MAC_VER_20 },
2346 		{ 0x7cf, 0x3c3,	RTL_GIGA_MAC_VER_21 },
2347 		{ 0x7c8, 0x3c0,	RTL_GIGA_MAC_VER_22 },
2348 
2349 		/* 8168B family. */
2350 		{ 0x7c8, 0x380,	RTL_GIGA_MAC_VER_17 },
2351 		/* This one is very old and rare, let's see if anybody complains.
2352 		 * { 0x7c8, 0x300,	RTL_GIGA_MAC_VER_11 },
2353 		 */
2354 
2355 		/* 8101 family. */
2356 		{ 0x7c8, 0x448,	RTL_GIGA_MAC_VER_39 },
2357 		{ 0x7c8, 0x440,	RTL_GIGA_MAC_VER_37 },
2358 		{ 0x7cf, 0x409,	RTL_GIGA_MAC_VER_29 },
2359 		{ 0x7c8, 0x408,	RTL_GIGA_MAC_VER_30 },
2360 		{ 0x7cf, 0x349,	RTL_GIGA_MAC_VER_08 },
2361 		{ 0x7cf, 0x249,	RTL_GIGA_MAC_VER_08 },
2362 		{ 0x7cf, 0x348,	RTL_GIGA_MAC_VER_07 },
2363 		{ 0x7cf, 0x248,	RTL_GIGA_MAC_VER_07 },
2364 		{ 0x7cf, 0x240,	RTL_GIGA_MAC_VER_14 },
2365 		{ 0x7c8, 0x348,	RTL_GIGA_MAC_VER_09 },
2366 		{ 0x7c8, 0x248,	RTL_GIGA_MAC_VER_09 },
2367 		{ 0x7c8, 0x340,	RTL_GIGA_MAC_VER_10 },
2368 
2369 		/* 8110 family. */
2370 		{ 0xfc8, 0x980,	RTL_GIGA_MAC_VER_06 },
2371 		{ 0xfc8, 0x180,	RTL_GIGA_MAC_VER_05 },
2372 		{ 0xfc8, 0x100,	RTL_GIGA_MAC_VER_04 },
2373 		{ 0xfc8, 0x040,	RTL_GIGA_MAC_VER_03 },
2374 		{ 0xfc8, 0x008,	RTL_GIGA_MAC_VER_02 },
2375 
2376 		/* Catch-all */
2377 		{ 0x000, 0x000,	RTL_GIGA_MAC_NONE   }
2378 	};
2379 	const struct rtl_mac_info *p = mac_info;
2380 	enum mac_version ver;
2381 
2382 	while ((xid & p->mask) != p->val)
2383 		p++;
2384 	ver = p->ver;
2385 
2386 	if (ver != RTL_GIGA_MAC_NONE && !gmii) {
2387 		if (ver == RTL_GIGA_MAC_VER_42)
2388 			ver = RTL_GIGA_MAC_VER_43;
2389 		else if (ver == RTL_GIGA_MAC_VER_46)
2390 			ver = RTL_GIGA_MAC_VER_48;
2391 	}
2392 
2393 	return ver;
2394 }
2395 
2396 static void rtl_release_firmware(struct rtl8169_private *tp)
2397 {
2398 	if (tp->rtl_fw) {
2399 		rtl_fw_release_firmware(tp->rtl_fw);
2400 		kfree(tp->rtl_fw);
2401 		tp->rtl_fw = NULL;
2402 	}
2403 }
2404 
2405 void r8169_apply_firmware(struct rtl8169_private *tp)
2406 {
2407 	int val;
2408 
2409 	/* TODO: release firmware if rtl_fw_write_firmware signals failure. */
2410 	if (tp->rtl_fw) {
2411 		rtl_fw_write_firmware(tp, tp->rtl_fw);
2412 		/* At least one firmware doesn't reset tp->ocp_base. */
2413 		tp->ocp_base = OCP_STD_PHY_BASE;
2414 
2415 		/* PHY soft reset may still be in progress */
2416 		phy_read_poll_timeout(tp->phydev, MII_BMCR, val,
2417 				      !(val & BMCR_RESET),
2418 				      50000, 600000, true);
2419 	}
2420 }
2421 
2422 static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2423 {
2424 	/* Adjust EEE LED frequency */
2425 	if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2426 		RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2427 
2428 	rtl_eri_set_bits(tp, 0x1b0, 0x0003);
2429 }
2430 
2431 static void rtl8125a_config_eee_mac(struct rtl8169_private *tp)
2432 {
2433 	r8168_mac_ocp_modify(tp, 0xe040, 0, BIT(1) | BIT(0));
2434 	r8168_mac_ocp_modify(tp, 0xeb62, 0, BIT(2) | BIT(1));
2435 }
2436 
2437 static void rtl8125b_config_eee_mac(struct rtl8169_private *tp)
2438 {
2439 	r8168_mac_ocp_modify(tp, 0xe040, 0, BIT(1) | BIT(0));
2440 }
2441 
2442 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, const u8 *addr)
2443 {
2444 	rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, get_unaligned_le32(addr));
2445 	rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, get_unaligned_le16(addr + 4));
2446 	rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, get_unaligned_le16(addr) << 16);
2447 	rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, get_unaligned_le32(addr + 2));
2448 }
2449 
2450 u16 rtl8168h_2_get_adc_bias_ioffset(struct rtl8169_private *tp)
2451 {
2452 	u16 data1, data2, ioffset;
2453 
2454 	r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
2455 	data1 = r8168_mac_ocp_read(tp, 0xdd02);
2456 	data2 = r8168_mac_ocp_read(tp, 0xdd00);
2457 
2458 	ioffset = (data2 >> 1) & 0x7ff8;
2459 	ioffset |= data2 & 0x0007;
2460 	if (data1 & BIT(7))
2461 		ioffset |= BIT(15);
2462 
2463 	return ioffset;
2464 }
2465 
2466 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
2467 {
2468 	set_bit(flag, tp->wk.flags);
2469 	if (!schedule_work(&tp->wk.work))
2470 		clear_bit(flag, tp->wk.flags);
2471 }
2472 
2473 static void rtl8169_init_phy(struct rtl8169_private *tp)
2474 {
2475 	r8169_hw_phy_config(tp, tp->phydev, tp->mac_version);
2476 
2477 	if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2478 		pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2479 		pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
2480 		/* set undocumented MAC Reg C+CR Offset 0x82h */
2481 		RTL_W8(tp, 0x82, 0x01);
2482 	}
2483 
2484 	if (tp->mac_version == RTL_GIGA_MAC_VER_05 &&
2485 	    tp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_GIGABYTE &&
2486 	    tp->pci_dev->subsystem_device == 0xe000)
2487 		phy_write_paged(tp->phydev, 0x0001, 0x10, 0xf01b);
2488 
2489 	/* We may have called phy_speed_down before */
2490 	phy_speed_up(tp->phydev);
2491 
2492 	genphy_soft_reset(tp->phydev);
2493 }
2494 
2495 static void rtl_rar_set(struct rtl8169_private *tp, const u8 *addr)
2496 {
2497 	rtl_unlock_config_regs(tp);
2498 
2499 	RTL_W32(tp, MAC4, get_unaligned_le16(addr + 4));
2500 	rtl_pci_commit(tp);
2501 
2502 	RTL_W32(tp, MAC0, get_unaligned_le32(addr));
2503 	rtl_pci_commit(tp);
2504 
2505 	if (tp->mac_version == RTL_GIGA_MAC_VER_34)
2506 		rtl_rar_exgmac_set(tp, addr);
2507 
2508 	rtl_lock_config_regs(tp);
2509 }
2510 
2511 static int rtl_set_mac_address(struct net_device *dev, void *p)
2512 {
2513 	struct rtl8169_private *tp = netdev_priv(dev);
2514 	int ret;
2515 
2516 	ret = eth_mac_addr(dev, p);
2517 	if (ret)
2518 		return ret;
2519 
2520 	rtl_rar_set(tp, dev->dev_addr);
2521 
2522 	return 0;
2523 }
2524 
2525 static void rtl_init_rxcfg(struct rtl8169_private *tp)
2526 {
2527 	switch (tp->mac_version) {
2528 	case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
2529 	case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
2530 		RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
2531 		break;
2532 	case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
2533 	case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
2534 	case RTL_GIGA_MAC_VER_38:
2535 		RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
2536 		break;
2537 	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_53:
2538 		RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
2539 		break;
2540 	case RTL_GIGA_MAC_VER_61:
2541 		RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_DMA_BURST);
2542 		break;
2543 	case RTL_GIGA_MAC_VER_63 ... RTL_GIGA_MAC_VER_66:
2544 		RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_DMA_BURST |
2545 			RX_PAUSE_SLOT_ON);
2546 		break;
2547 	default:
2548 		RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
2549 		break;
2550 	}
2551 }
2552 
2553 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
2554 {
2555 	tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
2556 }
2557 
2558 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
2559 {
2560 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
2561 	RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
2562 }
2563 
2564 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
2565 {
2566 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
2567 	RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
2568 }
2569 
2570 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
2571 {
2572 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
2573 }
2574 
2575 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
2576 {
2577 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
2578 }
2579 
2580 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
2581 {
2582 	RTL_W8(tp, MaxTxPacketSize, 0x24);
2583 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
2584 	RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
2585 }
2586 
2587 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
2588 {
2589 	RTL_W8(tp, MaxTxPacketSize, 0x3f);
2590 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
2591 	RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
2592 }
2593 
2594 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
2595 {
2596 	RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
2597 }
2598 
2599 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
2600 {
2601 	RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
2602 }
2603 
2604 static void rtl_jumbo_config(struct rtl8169_private *tp)
2605 {
2606 	bool jumbo = tp->dev->mtu > ETH_DATA_LEN;
2607 	int readrq = 4096;
2608 
2609 	rtl_unlock_config_regs(tp);
2610 	switch (tp->mac_version) {
2611 	case RTL_GIGA_MAC_VER_17:
2612 		if (jumbo) {
2613 			readrq = 512;
2614 			r8168b_1_hw_jumbo_enable(tp);
2615 		} else {
2616 			r8168b_1_hw_jumbo_disable(tp);
2617 		}
2618 		break;
2619 	case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
2620 		if (jumbo) {
2621 			readrq = 512;
2622 			r8168c_hw_jumbo_enable(tp);
2623 		} else {
2624 			r8168c_hw_jumbo_disable(tp);
2625 		}
2626 		break;
2627 	case RTL_GIGA_MAC_VER_28:
2628 		if (jumbo)
2629 			r8168dp_hw_jumbo_enable(tp);
2630 		else
2631 			r8168dp_hw_jumbo_disable(tp);
2632 		break;
2633 	case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_33:
2634 		if (jumbo)
2635 			r8168e_hw_jumbo_enable(tp);
2636 		else
2637 			r8168e_hw_jumbo_disable(tp);
2638 		break;
2639 	default:
2640 		break;
2641 	}
2642 	rtl_lock_config_regs(tp);
2643 
2644 	if (pci_is_pcie(tp->pci_dev) && tp->supports_gmii)
2645 		pcie_set_readrq(tp->pci_dev, readrq);
2646 
2647 	/* Chip doesn't support pause in jumbo mode */
2648 	if (jumbo) {
2649 		linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2650 				   tp->phydev->advertising);
2651 		linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2652 				   tp->phydev->advertising);
2653 		phy_start_aneg(tp->phydev);
2654 	}
2655 }
2656 
2657 DECLARE_RTL_COND(rtl_chipcmd_cond)
2658 {
2659 	return RTL_R8(tp, ChipCmd) & CmdReset;
2660 }
2661 
2662 static void rtl_hw_reset(struct rtl8169_private *tp)
2663 {
2664 	RTL_W8(tp, ChipCmd, CmdReset);
2665 
2666 	rtl_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
2667 }
2668 
2669 static void rtl_request_firmware(struct rtl8169_private *tp)
2670 {
2671 	struct rtl_fw *rtl_fw;
2672 
2673 	/* firmware loaded already or no firmware available */
2674 	if (tp->rtl_fw || !tp->fw_name)
2675 		return;
2676 
2677 	rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
2678 	if (!rtl_fw)
2679 		return;
2680 
2681 	rtl_fw->phy_write = rtl_writephy;
2682 	rtl_fw->phy_read = rtl_readphy;
2683 	rtl_fw->mac_mcu_write = mac_mcu_write;
2684 	rtl_fw->mac_mcu_read = mac_mcu_read;
2685 	rtl_fw->fw_name = tp->fw_name;
2686 	rtl_fw->dev = tp_to_dev(tp);
2687 
2688 	if (rtl_fw_request_firmware(rtl_fw))
2689 		kfree(rtl_fw);
2690 	else
2691 		tp->rtl_fw = rtl_fw;
2692 }
2693 
2694 static void rtl_rx_close(struct rtl8169_private *tp)
2695 {
2696 	RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
2697 }
2698 
2699 DECLARE_RTL_COND(rtl_npq_cond)
2700 {
2701 	return RTL_R8(tp, TxPoll) & NPQ;
2702 }
2703 
2704 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
2705 {
2706 	return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
2707 }
2708 
2709 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
2710 {
2711 	return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
2712 }
2713 
2714 DECLARE_RTL_COND(rtl_rxtx_empty_cond_2)
2715 {
2716 	/* IntrMitigate has new functionality on RTL8125 */
2717 	return (RTL_R16(tp, IntrMitigate) & 0x0103) == 0x0103;
2718 }
2719 
2720 static void rtl_wait_txrx_fifo_empty(struct rtl8169_private *tp)
2721 {
2722 	switch (tp->mac_version) {
2723 	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_53:
2724 		rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42);
2725 		rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
2726 		break;
2727 	case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_61:
2728 		rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
2729 		break;
2730 	case RTL_GIGA_MAC_VER_63 ... RTL_GIGA_MAC_VER_66:
2731 		RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
2732 		rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
2733 		rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond_2, 100, 42);
2734 		break;
2735 	default:
2736 		break;
2737 	}
2738 }
2739 
2740 static void rtl_disable_rxdvgate(struct rtl8169_private *tp)
2741 {
2742 	RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
2743 }
2744 
2745 static void rtl_enable_rxdvgate(struct rtl8169_private *tp)
2746 {
2747 	RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
2748 	fsleep(2000);
2749 	rtl_wait_txrx_fifo_empty(tp);
2750 }
2751 
2752 static void rtl_wol_enable_rx(struct rtl8169_private *tp)
2753 {
2754 	if (tp->mac_version >= RTL_GIGA_MAC_VER_25)
2755 		RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
2756 			AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
2757 
2758 	if (tp->mac_version >= RTL_GIGA_MAC_VER_40)
2759 		rtl_disable_rxdvgate(tp);
2760 }
2761 
2762 static void rtl_prepare_power_down(struct rtl8169_private *tp)
2763 {
2764 	if (tp->dash_enabled)
2765 		return;
2766 
2767 	if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
2768 	    tp->mac_version == RTL_GIGA_MAC_VER_33)
2769 		rtl_ephy_write(tp, 0x19, 0xff64);
2770 
2771 	if (device_may_wakeup(tp_to_dev(tp))) {
2772 		phy_speed_down(tp->phydev, false);
2773 		rtl_wol_enable_rx(tp);
2774 	}
2775 }
2776 
2777 static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
2778 {
2779 	u32 val = TX_DMA_BURST << TxDMAShift |
2780 		  InterFrameGap << TxInterFrameGapShift;
2781 
2782 	if (rtl_is_8168evl_up(tp))
2783 		val |= TXCFG_AUTO_FIFO;
2784 
2785 	RTL_W32(tp, TxConfig, val);
2786 }
2787 
2788 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
2789 {
2790 	/* Low hurts. Let's disable the filtering. */
2791 	RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
2792 }
2793 
2794 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
2795 {
2796 	/*
2797 	 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
2798 	 * register to be written before TxDescAddrLow to work.
2799 	 * Switching from MMIO to I/O access fixes the issue as well.
2800 	 */
2801 	RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
2802 	RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
2803 	RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
2804 	RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
2805 }
2806 
2807 static void rtl8169_set_magic_reg(struct rtl8169_private *tp)
2808 {
2809 	u32 val;
2810 
2811 	if (tp->mac_version == RTL_GIGA_MAC_VER_05)
2812 		val = 0x000fff00;
2813 	else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
2814 		val = 0x00ffff00;
2815 	else
2816 		return;
2817 
2818 	if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
2819 		val |= 0xff;
2820 
2821 	RTL_W32(tp, 0x7c, val);
2822 }
2823 
2824 static void rtl_set_rx_mode(struct net_device *dev)
2825 {
2826 	u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
2827 	/* Multicast hash filter */
2828 	u32 mc_filter[2] = { 0xffffffff, 0xffffffff };
2829 	struct rtl8169_private *tp = netdev_priv(dev);
2830 	u32 tmp;
2831 
2832 	if (dev->flags & IFF_PROMISC) {
2833 		rx_mode |= AcceptAllPhys;
2834 	} else if (!(dev->flags & IFF_MULTICAST)) {
2835 		rx_mode &= ~AcceptMulticast;
2836 	} else if (dev->flags & IFF_ALLMULTI ||
2837 		   tp->mac_version == RTL_GIGA_MAC_VER_35) {
2838 		/* accept all multicasts */
2839 	} else if (netdev_mc_empty(dev)) {
2840 		rx_mode &= ~AcceptMulticast;
2841 	} else {
2842 		struct netdev_hw_addr *ha;
2843 
2844 		mc_filter[1] = mc_filter[0] = 0;
2845 		netdev_for_each_mc_addr(ha, dev) {
2846 			u32 bit_nr = eth_hw_addr_crc(ha) >> 26;
2847 			mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31);
2848 		}
2849 
2850 		if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
2851 			tmp = mc_filter[0];
2852 			mc_filter[0] = swab32(mc_filter[1]);
2853 			mc_filter[1] = swab32(tmp);
2854 		}
2855 	}
2856 
2857 	RTL_W32(tp, MAR0 + 4, mc_filter[1]);
2858 	RTL_W32(tp, MAR0 + 0, mc_filter[0]);
2859 
2860 	tmp = RTL_R32(tp, RxConfig);
2861 	RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_OK_MASK) | rx_mode);
2862 }
2863 
2864 DECLARE_RTL_COND(rtl_csiar_cond)
2865 {
2866 	return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
2867 }
2868 
2869 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
2870 {
2871 	u32 func = PCI_FUNC(tp->pci_dev->devfn);
2872 
2873 	RTL_W32(tp, CSIDR, value);
2874 	RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
2875 		CSIAR_BYTE_ENABLE | func << 16);
2876 
2877 	rtl_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
2878 }
2879 
2880 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
2881 {
2882 	u32 func = PCI_FUNC(tp->pci_dev->devfn);
2883 
2884 	RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
2885 		CSIAR_BYTE_ENABLE);
2886 
2887 	return rtl_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
2888 		RTL_R32(tp, CSIDR) : ~0;
2889 }
2890 
2891 static void rtl_set_aspm_entry_latency(struct rtl8169_private *tp, u8 val)
2892 {
2893 	struct pci_dev *pdev = tp->pci_dev;
2894 	u32 csi;
2895 
2896 	/* According to Realtek the value at config space address 0x070f
2897 	 * controls the L0s/L1 entrance latency. We try standard ECAM access
2898 	 * first and if it fails fall back to CSI.
2899 	 * bit 0..2: L0: 0 = 1us, 1 = 2us .. 6 = 7us, 7 = 7us (no typo)
2900 	 * bit 3..5: L1: 0 = 1us, 1 = 2us .. 6 = 64us, 7 = 64us
2901 	 */
2902 	if (pdev->cfg_size > 0x070f &&
2903 	    pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
2904 		return;
2905 
2906 	netdev_notice_once(tp->dev,
2907 		"No native access to PCI extended config space, falling back to CSI\n");
2908 	csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
2909 	rtl_csi_write(tp, 0x070c, csi | val << 24);
2910 }
2911 
2912 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
2913 {
2914 	/* L0 7us, L1 16us */
2915 	rtl_set_aspm_entry_latency(tp, 0x27);
2916 }
2917 
2918 struct ephy_info {
2919 	unsigned int offset;
2920 	u16 mask;
2921 	u16 bits;
2922 };
2923 
2924 static void __rtl_ephy_init(struct rtl8169_private *tp,
2925 			    const struct ephy_info *e, int len)
2926 {
2927 	u16 w;
2928 
2929 	while (len-- > 0) {
2930 		w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
2931 		rtl_ephy_write(tp, e->offset, w);
2932 		e++;
2933 	}
2934 }
2935 
2936 #define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
2937 
2938 static void rtl_disable_clock_request(struct rtl8169_private *tp)
2939 {
2940 	pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
2941 				   PCI_EXP_LNKCTL_CLKREQ_EN);
2942 }
2943 
2944 static void rtl_enable_clock_request(struct rtl8169_private *tp)
2945 {
2946 	pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
2947 				 PCI_EXP_LNKCTL_CLKREQ_EN);
2948 }
2949 
2950 static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
2951 {
2952 	/* work around an issue when PCI reset occurs during L2/L3 state */
2953 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
2954 }
2955 
2956 static void rtl_enable_exit_l1(struct rtl8169_private *tp)
2957 {
2958 	/* Bits control which events trigger ASPM L1 exit:
2959 	 * Bit 12: rxdv
2960 	 * Bit 11: ltr_msg
2961 	 * Bit 10: txdma_poll
2962 	 * Bit  9: xadm
2963 	 * Bit  8: pktavi
2964 	 * Bit  7: txpla
2965 	 */
2966 	switch (tp->mac_version) {
2967 	case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
2968 		rtl_eri_set_bits(tp, 0xd4, 0x1f00);
2969 		break;
2970 	case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_38:
2971 		rtl_eri_set_bits(tp, 0xd4, 0x0c00);
2972 		break;
2973 	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_66:
2974 		r8168_mac_ocp_modify(tp, 0xc0ac, 0, 0x1f80);
2975 		break;
2976 	default:
2977 		break;
2978 	}
2979 }
2980 
2981 static void rtl_disable_exit_l1(struct rtl8169_private *tp)
2982 {
2983 	switch (tp->mac_version) {
2984 	case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
2985 		rtl_eri_clear_bits(tp, 0xd4, 0x1f00);
2986 		break;
2987 	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_66:
2988 		r8168_mac_ocp_modify(tp, 0xc0ac, 0x1f80, 0);
2989 		break;
2990 	default:
2991 		break;
2992 	}
2993 }
2994 
2995 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
2996 {
2997 	u8 val8;
2998 
2999 	if (tp->mac_version < RTL_GIGA_MAC_VER_32)
3000 		return;
3001 
3002 	/* Don't enable ASPM in the chip if OS can't control ASPM */
3003 	if (enable && tp->aspm_manageable) {
3004 		/* On these chip versions ASPM can even harm
3005 		 * bus communication of other PCI devices.
3006 		 */
3007 		if (tp->mac_version == RTL_GIGA_MAC_VER_42 ||
3008 		    tp->mac_version == RTL_GIGA_MAC_VER_43)
3009 			return;
3010 
3011 		rtl_mod_config5(tp, 0, ASPM_en);
3012 		switch (tp->mac_version) {
3013 		case RTL_GIGA_MAC_VER_65:
3014 		case RTL_GIGA_MAC_VER_66:
3015 			val8 = RTL_R8(tp, INT_CFG0_8125) | INT_CFG0_CLKREQEN;
3016 			RTL_W8(tp, INT_CFG0_8125, val8);
3017 			break;
3018 		default:
3019 			rtl_mod_config2(tp, 0, ClkReqEn);
3020 			break;
3021 		}
3022 
3023 		switch (tp->mac_version) {
3024 		case RTL_GIGA_MAC_VER_46 ... RTL_GIGA_MAC_VER_48:
3025 		case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_66:
3026 			/* reset ephy tx/rx disable timer */
3027 			r8168_mac_ocp_modify(tp, 0xe094, 0xff00, 0);
3028 			/* chip can trigger L1.2 */
3029 			r8168_mac_ocp_modify(tp, 0xe092, 0x00ff, BIT(2));
3030 			break;
3031 		default:
3032 			break;
3033 		}
3034 	} else {
3035 		switch (tp->mac_version) {
3036 		case RTL_GIGA_MAC_VER_46 ... RTL_GIGA_MAC_VER_48:
3037 		case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_66:
3038 			r8168_mac_ocp_modify(tp, 0xe092, 0x00ff, 0);
3039 			break;
3040 		default:
3041 			break;
3042 		}
3043 
3044 		switch (tp->mac_version) {
3045 		case RTL_GIGA_MAC_VER_65:
3046 		case RTL_GIGA_MAC_VER_66:
3047 			val8 = RTL_R8(tp, INT_CFG0_8125) & ~INT_CFG0_CLKREQEN;
3048 			RTL_W8(tp, INT_CFG0_8125, val8);
3049 			break;
3050 		default:
3051 			rtl_mod_config2(tp, ClkReqEn, 0);
3052 			break;
3053 		}
3054 		rtl_mod_config5(tp, ASPM_en, 0);
3055 	}
3056 }
3057 
3058 static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
3059 			      u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
3060 {
3061 	/* Usage of dynamic vs. static FIFO is controlled by bit
3062 	 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
3063 	 */
3064 	rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
3065 	rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
3066 }
3067 
3068 static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
3069 					  u8 low, u8 high)
3070 {
3071 	/* FIFO thresholds for pause flow control */
3072 	rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
3073 	rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
3074 }
3075 
3076 static void rtl_hw_start_8168b(struct rtl8169_private *tp)
3077 {
3078 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3079 }
3080 
3081 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
3082 {
3083 	RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
3084 
3085 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3086 
3087 	rtl_disable_clock_request(tp);
3088 }
3089 
3090 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
3091 {
3092 	static const struct ephy_info e_info_8168cp[] = {
3093 		{ 0x01, 0,	0x0001 },
3094 		{ 0x02, 0x0800,	0x1000 },
3095 		{ 0x03, 0,	0x0042 },
3096 		{ 0x06, 0x0080,	0x0000 },
3097 		{ 0x07, 0,	0x2000 }
3098 	};
3099 
3100 	rtl_set_def_aspm_entry_latency(tp);
3101 
3102 	rtl_ephy_init(tp, e_info_8168cp);
3103 
3104 	__rtl_hw_start_8168cp(tp);
3105 }
3106 
3107 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
3108 {
3109 	rtl_set_def_aspm_entry_latency(tp);
3110 
3111 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3112 }
3113 
3114 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
3115 {
3116 	rtl_set_def_aspm_entry_latency(tp);
3117 
3118 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3119 
3120 	/* Magic. */
3121 	RTL_W8(tp, DBG_REG, 0x20);
3122 }
3123 
3124 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
3125 {
3126 	static const struct ephy_info e_info_8168c_1[] = {
3127 		{ 0x02, 0x0800,	0x1000 },
3128 		{ 0x03, 0,	0x0002 },
3129 		{ 0x06, 0x0080,	0x0000 }
3130 	};
3131 
3132 	rtl_set_def_aspm_entry_latency(tp);
3133 
3134 	RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3135 
3136 	rtl_ephy_init(tp, e_info_8168c_1);
3137 
3138 	__rtl_hw_start_8168cp(tp);
3139 }
3140 
3141 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
3142 {
3143 	static const struct ephy_info e_info_8168c_2[] = {
3144 		{ 0x01, 0,	0x0001 },
3145 		{ 0x03, 0x0400,	0x0020 }
3146 	};
3147 
3148 	rtl_set_def_aspm_entry_latency(tp);
3149 
3150 	rtl_ephy_init(tp, e_info_8168c_2);
3151 
3152 	__rtl_hw_start_8168cp(tp);
3153 }
3154 
3155 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
3156 {
3157 	rtl_set_def_aspm_entry_latency(tp);
3158 
3159 	__rtl_hw_start_8168cp(tp);
3160 }
3161 
3162 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
3163 {
3164 	rtl_set_def_aspm_entry_latency(tp);
3165 
3166 	rtl_disable_clock_request(tp);
3167 }
3168 
3169 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
3170 {
3171 	static const struct ephy_info e_info_8168d_4[] = {
3172 		{ 0x0b, 0x0000,	0x0048 },
3173 		{ 0x19, 0x0020,	0x0050 },
3174 		{ 0x0c, 0x0100,	0x0020 },
3175 		{ 0x10, 0x0004,	0x0000 },
3176 	};
3177 
3178 	rtl_set_def_aspm_entry_latency(tp);
3179 
3180 	rtl_ephy_init(tp, e_info_8168d_4);
3181 
3182 	rtl_enable_clock_request(tp);
3183 }
3184 
3185 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
3186 {
3187 	static const struct ephy_info e_info_8168e_1[] = {
3188 		{ 0x00, 0x0200,	0x0100 },
3189 		{ 0x00, 0x0000,	0x0004 },
3190 		{ 0x06, 0x0002,	0x0001 },
3191 		{ 0x06, 0x0000,	0x0030 },
3192 		{ 0x07, 0x0000,	0x2000 },
3193 		{ 0x00, 0x0000,	0x0020 },
3194 		{ 0x03, 0x5800,	0x2000 },
3195 		{ 0x03, 0x0000,	0x0001 },
3196 		{ 0x01, 0x0800,	0x1000 },
3197 		{ 0x07, 0x0000,	0x4000 },
3198 		{ 0x1e, 0x0000,	0x2000 },
3199 		{ 0x19, 0xffff,	0xfe6c },
3200 		{ 0x0a, 0x0000,	0x0040 }
3201 	};
3202 
3203 	rtl_set_def_aspm_entry_latency(tp);
3204 
3205 	rtl_ephy_init(tp, e_info_8168e_1);
3206 
3207 	rtl_disable_clock_request(tp);
3208 
3209 	/* Reset tx FIFO pointer */
3210 	RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
3211 	RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
3212 
3213 	rtl_mod_config5(tp, Spi_en, 0);
3214 }
3215 
3216 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
3217 {
3218 	static const struct ephy_info e_info_8168e_2[] = {
3219 		{ 0x09, 0x0000,	0x0080 },
3220 		{ 0x19, 0x0000,	0x0224 },
3221 		{ 0x00, 0x0000,	0x0004 },
3222 		{ 0x0c, 0x3df0,	0x0200 },
3223 	};
3224 
3225 	rtl_set_def_aspm_entry_latency(tp);
3226 
3227 	rtl_ephy_init(tp, e_info_8168e_2);
3228 
3229 	rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3230 	rtl_eri_write(tp, 0xb8, ERIAR_MASK_1111, 0x0000);
3231 	rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
3232 	rtl_eri_set_bits(tp, 0x1d0, BIT(1));
3233 	rtl_reset_packet_filter(tp);
3234 	rtl_eri_set_bits(tp, 0x1b0, BIT(4));
3235 	rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
3236 	rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
3237 
3238 	rtl_disable_clock_request(tp);
3239 
3240 	RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
3241 
3242 	rtl8168_config_eee_mac(tp);
3243 
3244 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
3245 	RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
3246 	rtl_mod_config5(tp, Spi_en, 0);
3247 }
3248 
3249 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
3250 {
3251 	rtl_set_def_aspm_entry_latency(tp);
3252 
3253 	rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3254 	rtl_eri_write(tp, 0xb8, ERIAR_MASK_1111, 0x0000);
3255 	rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
3256 	rtl_reset_packet_filter(tp);
3257 	rtl_eri_set_bits(tp, 0x1b0, BIT(4));
3258 	rtl_eri_set_bits(tp, 0x1d0, BIT(4) | BIT(1));
3259 	rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
3260 	rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
3261 
3262 	rtl_disable_clock_request(tp);
3263 
3264 	RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
3265 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
3266 	RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
3267 	rtl_mod_config5(tp, Spi_en, 0);
3268 
3269 	rtl8168_config_eee_mac(tp);
3270 }
3271 
3272 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
3273 {
3274 	static const struct ephy_info e_info_8168f_1[] = {
3275 		{ 0x06, 0x00c0,	0x0020 },
3276 		{ 0x08, 0x0001,	0x0002 },
3277 		{ 0x09, 0x0000,	0x0080 },
3278 		{ 0x19, 0x0000,	0x0224 },
3279 		{ 0x00, 0x0000,	0x0008 },
3280 		{ 0x0c, 0x3df0,	0x0200 },
3281 	};
3282 
3283 	rtl_hw_start_8168f(tp);
3284 
3285 	rtl_ephy_init(tp, e_info_8168f_1);
3286 }
3287 
3288 static void rtl_hw_start_8411(struct rtl8169_private *tp)
3289 {
3290 	static const struct ephy_info e_info_8168f_1[] = {
3291 		{ 0x06, 0x00c0,	0x0020 },
3292 		{ 0x0f, 0xffff,	0x5200 },
3293 		{ 0x19, 0x0000,	0x0224 },
3294 		{ 0x00, 0x0000,	0x0008 },
3295 		{ 0x0c, 0x3df0,	0x0200 },
3296 	};
3297 
3298 	rtl_hw_start_8168f(tp);
3299 	rtl_pcie_state_l2l3_disable(tp);
3300 
3301 	rtl_ephy_init(tp, e_info_8168f_1);
3302 }
3303 
3304 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
3305 {
3306 	rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
3307 	rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
3308 
3309 	rtl_set_def_aspm_entry_latency(tp);
3310 
3311 	rtl_reset_packet_filter(tp);
3312 	rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
3313 
3314 	rtl_disable_rxdvgate(tp);
3315 
3316 	rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3317 	rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3318 
3319 	rtl8168_config_eee_mac(tp);
3320 
3321 	rtl_w0w1_eri(tp, 0x2fc, 0x01, 0x06);
3322 	rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
3323 
3324 	rtl_pcie_state_l2l3_disable(tp);
3325 }
3326 
3327 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
3328 {
3329 	static const struct ephy_info e_info_8168g_1[] = {
3330 		{ 0x00, 0x0008,	0x0000 },
3331 		{ 0x0c, 0x3ff0,	0x0820 },
3332 		{ 0x1e, 0x0000,	0x0001 },
3333 		{ 0x19, 0x8000,	0x0000 }
3334 	};
3335 
3336 	rtl_hw_start_8168g(tp);
3337 	rtl_ephy_init(tp, e_info_8168g_1);
3338 }
3339 
3340 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
3341 {
3342 	static const struct ephy_info e_info_8168g_2[] = {
3343 		{ 0x00, 0x0008,	0x0000 },
3344 		{ 0x0c, 0x3ff0,	0x0820 },
3345 		{ 0x19, 0xffff,	0x7c00 },
3346 		{ 0x1e, 0xffff,	0x20eb },
3347 		{ 0x0d, 0xffff,	0x1666 },
3348 		{ 0x00, 0xffff,	0x10a3 },
3349 		{ 0x06, 0xffff,	0xf050 },
3350 		{ 0x04, 0x0000,	0x0010 },
3351 		{ 0x1d, 0x4000,	0x0000 },
3352 	};
3353 
3354 	rtl_hw_start_8168g(tp);
3355 	rtl_ephy_init(tp, e_info_8168g_2);
3356 }
3357 
3358 static void rtl8411b_fix_phy_down(struct rtl8169_private *tp)
3359 {
3360 	static const u16 fix_data[] = {
3361 /* 0xf800 */ 0xe008, 0xe00a, 0xe00c, 0xe00e, 0xe027, 0xe04f, 0xe05e, 0xe065,
3362 /* 0xf810 */ 0xc602, 0xbe00, 0x0000, 0xc502, 0xbd00, 0x074c, 0xc302, 0xbb00,
3363 /* 0xf820 */ 0x080a, 0x6420, 0x48c2, 0x8c20, 0xc516, 0x64a4, 0x49c0, 0xf009,
3364 /* 0xf830 */ 0x74a2, 0x8ca5, 0x74a0, 0xc50e, 0x9ca2, 0x1c11, 0x9ca0, 0xe006,
3365 /* 0xf840 */ 0x74f8, 0x48c4, 0x8cf8, 0xc404, 0xbc00, 0xc403, 0xbc00, 0x0bf2,
3366 /* 0xf850 */ 0x0c0a, 0xe434, 0xd3c0, 0x49d9, 0xf01f, 0xc526, 0x64a5, 0x1400,
3367 /* 0xf860 */ 0xf007, 0x0c01, 0x8ca5, 0x1c15, 0xc51b, 0x9ca0, 0xe013, 0xc519,
3368 /* 0xf870 */ 0x74a0, 0x48c4, 0x8ca0, 0xc516, 0x74a4, 0x48c8, 0x48ca, 0x9ca4,
3369 /* 0xf880 */ 0xc512, 0x1b00, 0x9ba0, 0x1b1c, 0x483f, 0x9ba2, 0x1b04, 0xc508,
3370 /* 0xf890 */ 0x9ba0, 0xc505, 0xbd00, 0xc502, 0xbd00, 0x0300, 0x051e, 0xe434,
3371 /* 0xf8a0 */ 0xe018, 0xe092, 0xde20, 0xd3c0, 0xc50f, 0x76a4, 0x49e3, 0xf007,
3372 /* 0xf8b0 */ 0x49c0, 0xf103, 0xc607, 0xbe00, 0xc606, 0xbe00, 0xc602, 0xbe00,
3373 /* 0xf8c0 */ 0x0c4c, 0x0c28, 0x0c2c, 0xdc00, 0xc707, 0x1d00, 0x8de2, 0x48c1,
3374 /* 0xf8d0 */ 0xc502, 0xbd00, 0x00aa, 0xe0c0, 0xc502, 0xbd00, 0x0132
3375 	};
3376 	unsigned long flags;
3377 	int i;
3378 
3379 	raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags);
3380 	for (i = 0; i < ARRAY_SIZE(fix_data); i++)
3381 		__r8168_mac_ocp_write(tp, 0xf800 + 2 * i, fix_data[i]);
3382 	raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags);
3383 }
3384 
3385 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
3386 {
3387 	static const struct ephy_info e_info_8411_2[] = {
3388 		{ 0x00, 0x0008,	0x0000 },
3389 		{ 0x0c, 0x37d0,	0x0820 },
3390 		{ 0x1e, 0x0000,	0x0001 },
3391 		{ 0x19, 0x8021,	0x0000 },
3392 		{ 0x1e, 0x0000,	0x2000 },
3393 		{ 0x0d, 0x0100,	0x0200 },
3394 		{ 0x00, 0x0000,	0x0080 },
3395 		{ 0x06, 0x0000,	0x0010 },
3396 		{ 0x04, 0x0000,	0x0010 },
3397 		{ 0x1d, 0x0000,	0x4000 },
3398 	};
3399 
3400 	rtl_hw_start_8168g(tp);
3401 
3402 	rtl_ephy_init(tp, e_info_8411_2);
3403 
3404 	/* The following Realtek-provided magic fixes an issue with the RX unit
3405 	 * getting confused after the PHY having been powered-down.
3406 	 */
3407 	r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
3408 	r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
3409 	r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
3410 	r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
3411 	r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
3412 	r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
3413 	r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
3414 	r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
3415 	mdelay(3);
3416 	r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
3417 
3418 	rtl8411b_fix_phy_down(tp);
3419 
3420 	r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
3421 
3422 	r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
3423 	r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
3424 	r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
3425 	r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
3426 	r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
3427 	r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
3428 	r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
3429 }
3430 
3431 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
3432 {
3433 	static const struct ephy_info e_info_8168h_1[] = {
3434 		{ 0x1e, 0x0800,	0x0001 },
3435 		{ 0x1d, 0x0000,	0x0800 },
3436 		{ 0x05, 0xffff,	0x2089 },
3437 		{ 0x06, 0xffff,	0x5881 },
3438 		{ 0x04, 0xffff,	0x854a },
3439 		{ 0x01, 0xffff,	0x068b }
3440 	};
3441 	int rg_saw_cnt;
3442 
3443 	rtl_ephy_init(tp, e_info_8168h_1);
3444 
3445 	rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
3446 	rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
3447 
3448 	rtl_set_def_aspm_entry_latency(tp);
3449 
3450 	rtl_reset_packet_filter(tp);
3451 
3452 	rtl_eri_set_bits(tp, 0xdc, 0x001c);
3453 
3454 	rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
3455 
3456 	rtl_disable_rxdvgate(tp);
3457 
3458 	rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3459 	rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3460 
3461 	rtl8168_config_eee_mac(tp);
3462 
3463 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
3464 	RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
3465 
3466 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
3467 
3468 	rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
3469 
3470 	rtl_pcie_state_l2l3_disable(tp);
3471 
3472 	rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
3473 	if (rg_saw_cnt > 0) {
3474 		u16 sw_cnt_1ms_ini;
3475 
3476 		sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
3477 		sw_cnt_1ms_ini &= 0x0fff;
3478 		r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
3479 	}
3480 
3481 	r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
3482 	r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008);
3483 	r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f);
3484 	r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
3485 
3486 	r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
3487 	r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
3488 	r8168_mac_ocp_write(tp, 0xc094, 0x0000);
3489 	r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
3490 }
3491 
3492 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
3493 {
3494 	rtl8168ep_stop_cmac(tp);
3495 
3496 	rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
3497 	rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
3498 
3499 	rtl_set_def_aspm_entry_latency(tp);
3500 
3501 	rtl_reset_packet_filter(tp);
3502 
3503 	rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
3504 
3505 	rtl_disable_rxdvgate(tp);
3506 
3507 	rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3508 	rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3509 
3510 	rtl8168_config_eee_mac(tp);
3511 
3512 	rtl_w0w1_eri(tp, 0x2fc, 0x01, 0x06);
3513 
3514 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
3515 
3516 	rtl_pcie_state_l2l3_disable(tp);
3517 }
3518 
3519 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
3520 {
3521 	static const struct ephy_info e_info_8168ep_3[] = {
3522 		{ 0x00, 0x0000,	0x0080 },
3523 		{ 0x0d, 0x0100,	0x0200 },
3524 		{ 0x19, 0x8021,	0x0000 },
3525 		{ 0x1e, 0x0000,	0x2000 },
3526 	};
3527 
3528 	rtl_ephy_init(tp, e_info_8168ep_3);
3529 
3530 	rtl_hw_start_8168ep(tp);
3531 
3532 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
3533 	RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
3534 
3535 	r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271);
3536 	r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
3537 	r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
3538 }
3539 
3540 static void rtl_hw_start_8117(struct rtl8169_private *tp)
3541 {
3542 	static const struct ephy_info e_info_8117[] = {
3543 		{ 0x19, 0x0040,	0x1100 },
3544 		{ 0x59, 0x0040,	0x1100 },
3545 	};
3546 	int rg_saw_cnt;
3547 
3548 	rtl8168ep_stop_cmac(tp);
3549 	rtl_ephy_init(tp, e_info_8117);
3550 
3551 	rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
3552 	rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
3553 
3554 	rtl_set_def_aspm_entry_latency(tp);
3555 
3556 	rtl_reset_packet_filter(tp);
3557 
3558 	rtl_eri_set_bits(tp, 0xd4, 0x0010);
3559 
3560 	rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
3561 
3562 	rtl_disable_rxdvgate(tp);
3563 
3564 	rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3565 	rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3566 
3567 	rtl8168_config_eee_mac(tp);
3568 
3569 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
3570 	RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
3571 
3572 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
3573 
3574 	rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
3575 
3576 	rtl_pcie_state_l2l3_disable(tp);
3577 
3578 	rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
3579 	if (rg_saw_cnt > 0) {
3580 		u16 sw_cnt_1ms_ini;
3581 
3582 		sw_cnt_1ms_ini = (16000000 / rg_saw_cnt) & 0x0fff;
3583 		r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
3584 	}
3585 
3586 	r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
3587 	r8168_mac_ocp_write(tp, 0xea80, 0x0003);
3588 	r8168_mac_ocp_modify(tp, 0xe052, 0x0000, 0x0009);
3589 	r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
3590 
3591 	r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
3592 	r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
3593 	r8168_mac_ocp_write(tp, 0xc094, 0x0000);
3594 	r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
3595 
3596 	/* firmware is for MAC only */
3597 	r8169_apply_firmware(tp);
3598 }
3599 
3600 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
3601 {
3602 	static const struct ephy_info e_info_8102e_1[] = {
3603 		{ 0x01,	0, 0x6e65 },
3604 		{ 0x02,	0, 0x091f },
3605 		{ 0x03,	0, 0xc2f9 },
3606 		{ 0x06,	0, 0xafb5 },
3607 		{ 0x07,	0, 0x0e00 },
3608 		{ 0x19,	0, 0xec80 },
3609 		{ 0x01,	0, 0x2e65 },
3610 		{ 0x01,	0, 0x6e65 }
3611 	};
3612 	u8 cfg1;
3613 
3614 	rtl_set_def_aspm_entry_latency(tp);
3615 
3616 	RTL_W8(tp, DBG_REG, FIX_NAK_1);
3617 
3618 	RTL_W8(tp, Config1,
3619 	       LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
3620 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3621 
3622 	cfg1 = RTL_R8(tp, Config1);
3623 	if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
3624 		RTL_W8(tp, Config1, cfg1 & ~LEDS0);
3625 
3626 	rtl_ephy_init(tp, e_info_8102e_1);
3627 }
3628 
3629 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
3630 {
3631 	rtl_set_def_aspm_entry_latency(tp);
3632 
3633 	RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
3634 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3635 }
3636 
3637 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
3638 {
3639 	rtl_hw_start_8102e_2(tp);
3640 
3641 	rtl_ephy_write(tp, 0x03, 0xc2f9);
3642 }
3643 
3644 static void rtl_hw_start_8401(struct rtl8169_private *tp)
3645 {
3646 	static const struct ephy_info e_info_8401[] = {
3647 		{ 0x01,	0xffff, 0x6fe5 },
3648 		{ 0x03,	0xffff, 0x0599 },
3649 		{ 0x06,	0xffff, 0xaf25 },
3650 		{ 0x07,	0xffff, 0x8e68 },
3651 	};
3652 
3653 	rtl_ephy_init(tp, e_info_8401);
3654 	RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3655 }
3656 
3657 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
3658 {
3659 	static const struct ephy_info e_info_8105e_1[] = {
3660 		{ 0x07,	0, 0x4000 },
3661 		{ 0x19,	0, 0x0200 },
3662 		{ 0x19,	0, 0x0020 },
3663 		{ 0x1e,	0, 0x2000 },
3664 		{ 0x03,	0, 0x0001 },
3665 		{ 0x19,	0, 0x0100 },
3666 		{ 0x19,	0, 0x0004 },
3667 		{ 0x0a,	0, 0x0020 }
3668 	};
3669 
3670 	/* Force LAN exit from ASPM if Rx/Tx are not idle */
3671 	RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
3672 
3673 	/* Disable Early Tally Counter */
3674 	RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
3675 
3676 	RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
3677 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
3678 
3679 	rtl_ephy_init(tp, e_info_8105e_1);
3680 
3681 	rtl_pcie_state_l2l3_disable(tp);
3682 }
3683 
3684 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
3685 {
3686 	rtl_hw_start_8105e_1(tp);
3687 	rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
3688 }
3689 
3690 static void rtl_hw_start_8402(struct rtl8169_private *tp)
3691 {
3692 	static const struct ephy_info e_info_8402[] = {
3693 		{ 0x19,	0xffff, 0xff64 },
3694 		{ 0x1e,	0, 0x4000 }
3695 	};
3696 
3697 	rtl_set_def_aspm_entry_latency(tp);
3698 
3699 	/* Force LAN exit from ASPM if Rx/Tx are not idle */
3700 	RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
3701 
3702 	RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
3703 
3704 	rtl_ephy_init(tp, e_info_8402);
3705 
3706 	rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
3707 	rtl_reset_packet_filter(tp);
3708 	rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3709 	rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3710 	rtl_w0w1_eri(tp, 0x0d4, 0x0e00, 0xff00);
3711 
3712 	/* disable EEE */
3713 	rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
3714 
3715 	rtl_pcie_state_l2l3_disable(tp);
3716 }
3717 
3718 static void rtl_hw_start_8106(struct rtl8169_private *tp)
3719 {
3720 	/* Force LAN exit from ASPM if Rx/Tx are not idle */
3721 	RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
3722 
3723 	RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
3724 	RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
3725 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
3726 
3727 	/* L0 7us, L1 32us - needed to avoid issues with link-up detection */
3728 	rtl_set_aspm_entry_latency(tp, 0x2f);
3729 
3730 	rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
3731 
3732 	/* disable EEE */
3733 	rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
3734 
3735 	rtl_pcie_state_l2l3_disable(tp);
3736 }
3737 
3738 DECLARE_RTL_COND(rtl_mac_ocp_e00e_cond)
3739 {
3740 	return r8168_mac_ocp_read(tp, 0xe00e) & BIT(13);
3741 }
3742 
3743 static void rtl_hw_start_8125_common(struct rtl8169_private *tp)
3744 {
3745 	rtl_pcie_state_l2l3_disable(tp);
3746 
3747 	RTL_W16(tp, 0x382, 0x221b);
3748 	RTL_W32(tp, RSS_CTRL_8125, 0);
3749 	RTL_W16(tp, Q_NUM_CTRL_8125, 0);
3750 
3751 	/* disable UPS */
3752 	r8168_mac_ocp_modify(tp, 0xd40a, 0x0010, 0x0000);
3753 
3754 	RTL_W8(tp, Config1, RTL_R8(tp, Config1) & ~0x10);
3755 
3756 	r8168_mac_ocp_write(tp, 0xc140, 0xffff);
3757 	r8168_mac_ocp_write(tp, 0xc142, 0xffff);
3758 
3759 	r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x03a9);
3760 	r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
3761 	r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
3762 
3763 	/* disable new tx descriptor format */
3764 	r8168_mac_ocp_modify(tp, 0xeb58, 0x0001, 0x0000);
3765 
3766 	if (tp->mac_version == RTL_GIGA_MAC_VER_65 ||
3767 	    tp->mac_version == RTL_GIGA_MAC_VER_66)
3768 		RTL_W8(tp, 0xD8, RTL_R8(tp, 0xD8) & ~0x02);
3769 
3770 	if (tp->mac_version == RTL_GIGA_MAC_VER_65 ||
3771 	    tp->mac_version == RTL_GIGA_MAC_VER_66)
3772 		r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0400);
3773 	else if (tp->mac_version == RTL_GIGA_MAC_VER_63)
3774 		r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0200);
3775 	else
3776 		r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0300);
3777 
3778 	if (tp->mac_version == RTL_GIGA_MAC_VER_63)
3779 		r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0000);
3780 	else
3781 		r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0020);
3782 
3783 	r8168_mac_ocp_modify(tp, 0xc0b4, 0x0000, 0x000c);
3784 	r8168_mac_ocp_modify(tp, 0xeb6a, 0x00ff, 0x0033);
3785 	r8168_mac_ocp_modify(tp, 0xeb50, 0x03e0, 0x0040);
3786 	r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0030);
3787 	r8168_mac_ocp_modify(tp, 0xe040, 0x1000, 0x0000);
3788 	r8168_mac_ocp_modify(tp, 0xea1c, 0x0003, 0x0001);
3789 	if (tp->mac_version == RTL_GIGA_MAC_VER_65 ||
3790 	    tp->mac_version == RTL_GIGA_MAC_VER_66)
3791 		r8168_mac_ocp_modify(tp, 0xea1c, 0x0300, 0x0000);
3792 	else
3793 		r8168_mac_ocp_modify(tp, 0xea1c, 0x0004, 0x0000);
3794 	r8168_mac_ocp_modify(tp, 0xe0c0, 0x4f0f, 0x4403);
3795 	r8168_mac_ocp_modify(tp, 0xe052, 0x0080, 0x0068);
3796 	r8168_mac_ocp_modify(tp, 0xd430, 0x0fff, 0x047f);
3797 
3798 	r8168_mac_ocp_modify(tp, 0xea1c, 0x0004, 0x0000);
3799 	r8168_mac_ocp_modify(tp, 0xeb54, 0x0000, 0x0001);
3800 	udelay(1);
3801 	r8168_mac_ocp_modify(tp, 0xeb54, 0x0001, 0x0000);
3802 	RTL_W16(tp, 0x1880, RTL_R16(tp, 0x1880) & ~0x0030);
3803 
3804 	r8168_mac_ocp_write(tp, 0xe098, 0xc302);
3805 
3806 	rtl_loop_wait_low(tp, &rtl_mac_ocp_e00e_cond, 1000, 10);
3807 
3808 	if (tp->mac_version == RTL_GIGA_MAC_VER_61)
3809 		rtl8125a_config_eee_mac(tp);
3810 	else
3811 		rtl8125b_config_eee_mac(tp);
3812 
3813 	rtl_disable_rxdvgate(tp);
3814 }
3815 
3816 static void rtl_hw_start_8125a_2(struct rtl8169_private *tp)
3817 {
3818 	static const struct ephy_info e_info_8125a_2[] = {
3819 		{ 0x04, 0xffff, 0xd000 },
3820 		{ 0x0a, 0xffff, 0x8653 },
3821 		{ 0x23, 0xffff, 0xab66 },
3822 		{ 0x20, 0xffff, 0x9455 },
3823 		{ 0x21, 0xffff, 0x99ff },
3824 		{ 0x29, 0xffff, 0xfe04 },
3825 
3826 		{ 0x44, 0xffff, 0xd000 },
3827 		{ 0x4a, 0xffff, 0x8653 },
3828 		{ 0x63, 0xffff, 0xab66 },
3829 		{ 0x60, 0xffff, 0x9455 },
3830 		{ 0x61, 0xffff, 0x99ff },
3831 		{ 0x69, 0xffff, 0xfe04 },
3832 	};
3833 
3834 	rtl_set_def_aspm_entry_latency(tp);
3835 	rtl_ephy_init(tp, e_info_8125a_2);
3836 	rtl_hw_start_8125_common(tp);
3837 }
3838 
3839 static void rtl_hw_start_8125b(struct rtl8169_private *tp)
3840 {
3841 	static const struct ephy_info e_info_8125b[] = {
3842 		{ 0x0b, 0xffff, 0xa908 },
3843 		{ 0x1e, 0xffff, 0x20eb },
3844 		{ 0x4b, 0xffff, 0xa908 },
3845 		{ 0x5e, 0xffff, 0x20eb },
3846 		{ 0x22, 0x0030, 0x0020 },
3847 		{ 0x62, 0x0030, 0x0020 },
3848 	};
3849 
3850 	rtl_set_def_aspm_entry_latency(tp);
3851 	rtl_ephy_init(tp, e_info_8125b);
3852 	rtl_hw_start_8125_common(tp);
3853 }
3854 
3855 static void rtl_hw_start_8125d(struct rtl8169_private *tp)
3856 {
3857 	rtl_set_def_aspm_entry_latency(tp);
3858 	rtl_hw_start_8125_common(tp);
3859 }
3860 
3861 static void rtl_hw_start_8126a(struct rtl8169_private *tp)
3862 {
3863 	rtl_set_def_aspm_entry_latency(tp);
3864 	rtl_hw_start_8125_common(tp);
3865 }
3866 
3867 static void rtl_hw_config(struct rtl8169_private *tp)
3868 {
3869 	static const rtl_generic_fct hw_configs[] = {
3870 		[RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
3871 		[RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
3872 		[RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
3873 		[RTL_GIGA_MAC_VER_10] = NULL,
3874 		[RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168b,
3875 		[RTL_GIGA_MAC_VER_14] = rtl_hw_start_8401,
3876 		[RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168b,
3877 		[RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
3878 		[RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
3879 		[RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
3880 		[RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_2,
3881 		[RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
3882 		[RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
3883 		[RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
3884 		[RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
3885 		[RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
3886 		[RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
3887 		[RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
3888 		[RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
3889 		[RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168d,
3890 		[RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
3891 		[RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
3892 		[RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
3893 		[RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
3894 		[RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
3895 		[RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
3896 		[RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
3897 		[RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
3898 		[RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
3899 		[RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
3900 		[RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
3901 		[RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
3902 		[RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
3903 		[RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
3904 		[RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
3905 		[RTL_GIGA_MAC_VER_52] = rtl_hw_start_8117,
3906 		[RTL_GIGA_MAC_VER_53] = rtl_hw_start_8117,
3907 		[RTL_GIGA_MAC_VER_61] = rtl_hw_start_8125a_2,
3908 		[RTL_GIGA_MAC_VER_63] = rtl_hw_start_8125b,
3909 		[RTL_GIGA_MAC_VER_64] = rtl_hw_start_8125d,
3910 		[RTL_GIGA_MAC_VER_65] = rtl_hw_start_8126a,
3911 		[RTL_GIGA_MAC_VER_66] = rtl_hw_start_8126a,
3912 	};
3913 
3914 	if (hw_configs[tp->mac_version])
3915 		hw_configs[tp->mac_version](tp);
3916 }
3917 
3918 static void rtl_hw_start_8125(struct rtl8169_private *tp)
3919 {
3920 	int i;
3921 
3922 	RTL_W8(tp, INT_CFG0_8125, 0x00);
3923 
3924 	/* disable interrupt coalescing */
3925 	switch (tp->mac_version) {
3926 	case RTL_GIGA_MAC_VER_61:
3927 	case RTL_GIGA_MAC_VER_64:
3928 		for (i = 0xa00; i < 0xb00; i += 4)
3929 			RTL_W32(tp, i, 0);
3930 		break;
3931 	case RTL_GIGA_MAC_VER_63:
3932 	case RTL_GIGA_MAC_VER_65:
3933 	case RTL_GIGA_MAC_VER_66:
3934 		for (i = 0xa00; i < 0xa80; i += 4)
3935 			RTL_W32(tp, i, 0);
3936 		RTL_W16(tp, INT_CFG1_8125, 0x0000);
3937 		break;
3938 	default:
3939 		break;
3940 	}
3941 
3942 	/* enable extended tally counter */
3943 	r8168_mac_ocp_modify(tp, 0xea84, 0, BIT(1) | BIT(0));
3944 
3945 	rtl_hw_config(tp);
3946 }
3947 
3948 static void rtl_hw_start_8168(struct rtl8169_private *tp)
3949 {
3950 	if (rtl_is_8168evl_up(tp))
3951 		RTL_W8(tp, MaxTxPacketSize, EarlySize);
3952 	else
3953 		RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
3954 
3955 	rtl_hw_config(tp);
3956 
3957 	/* disable interrupt coalescing */
3958 	RTL_W16(tp, IntrMitigate, 0x0000);
3959 }
3960 
3961 static void rtl_hw_start_8169(struct rtl8169_private *tp)
3962 {
3963 	RTL_W8(tp, EarlyTxThres, NoEarlyTx);
3964 
3965 	tp->cp_cmd |= PCIMulRW;
3966 
3967 	if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3968 	    tp->mac_version == RTL_GIGA_MAC_VER_03)
3969 		tp->cp_cmd |= EnAnaPLL;
3970 
3971 	RTL_W16(tp, CPlusCmd, tp->cp_cmd);
3972 
3973 	rtl8169_set_magic_reg(tp);
3974 
3975 	/* disable interrupt coalescing */
3976 	RTL_W16(tp, IntrMitigate, 0x0000);
3977 }
3978 
3979 static void rtl_hw_start(struct  rtl8169_private *tp)
3980 {
3981 	rtl_unlock_config_regs(tp);
3982 	/* disable aspm and clock request before ephy access */
3983 	rtl_hw_aspm_clkreq_enable(tp, false);
3984 	RTL_W16(tp, CPlusCmd, tp->cp_cmd);
3985 
3986 	rtl_set_eee_txidle_timer(tp);
3987 
3988 	if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3989 		rtl_hw_start_8169(tp);
3990 	else if (rtl_is_8125(tp))
3991 		rtl_hw_start_8125(tp);
3992 	else
3993 		rtl_hw_start_8168(tp);
3994 
3995 	rtl_enable_exit_l1(tp);
3996 	rtl_hw_aspm_clkreq_enable(tp, true);
3997 	rtl_set_rx_max_size(tp);
3998 	rtl_set_rx_tx_desc_registers(tp);
3999 	rtl_lock_config_regs(tp);
4000 
4001 	rtl_jumbo_config(tp);
4002 
4003 	/* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4004 	rtl_pci_commit(tp);
4005 
4006 	RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
4007 	rtl_init_rxcfg(tp);
4008 	rtl_set_tx_config_registers(tp);
4009 	rtl_set_rx_config_features(tp, tp->dev->features);
4010 	rtl_set_rx_mode(tp->dev);
4011 	rtl_irq_enable(tp);
4012 }
4013 
4014 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4015 {
4016 	struct rtl8169_private *tp = netdev_priv(dev);
4017 
4018 	WRITE_ONCE(dev->mtu, new_mtu);
4019 	netdev_update_features(dev);
4020 	rtl_jumbo_config(tp);
4021 	rtl_set_eee_txidle_timer(tp);
4022 
4023 	return 0;
4024 }
4025 
4026 static void rtl8169_mark_to_asic(struct RxDesc *desc)
4027 {
4028 	u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4029 
4030 	desc->opts2 = 0;
4031 	/* Force memory writes to complete before releasing descriptor */
4032 	dma_wmb();
4033 	WRITE_ONCE(desc->opts1, cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE));
4034 }
4035 
4036 static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4037 					  struct RxDesc *desc)
4038 {
4039 	struct device *d = tp_to_dev(tp);
4040 	int node = dev_to_node(d);
4041 	dma_addr_t mapping;
4042 	struct page *data;
4043 
4044 	data = alloc_pages_node(node, GFP_KERNEL, get_order(R8169_RX_BUF_SIZE));
4045 	if (!data)
4046 		return NULL;
4047 
4048 	mapping = dma_map_page(d, data, 0, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
4049 	if (unlikely(dma_mapping_error(d, mapping))) {
4050 		netdev_err(tp->dev, "Failed to map RX DMA!\n");
4051 		__free_pages(data, get_order(R8169_RX_BUF_SIZE));
4052 		return NULL;
4053 	}
4054 
4055 	desc->addr = cpu_to_le64(mapping);
4056 	rtl8169_mark_to_asic(desc);
4057 
4058 	return data;
4059 }
4060 
4061 static void rtl8169_rx_clear(struct rtl8169_private *tp)
4062 {
4063 	int i;
4064 
4065 	for (i = 0; i < NUM_RX_DESC && tp->Rx_databuff[i]; i++) {
4066 		dma_unmap_page(tp_to_dev(tp),
4067 			       le64_to_cpu(tp->RxDescArray[i].addr),
4068 			       R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
4069 		__free_pages(tp->Rx_databuff[i], get_order(R8169_RX_BUF_SIZE));
4070 		tp->Rx_databuff[i] = NULL;
4071 		tp->RxDescArray[i].addr = 0;
4072 		tp->RxDescArray[i].opts1 = 0;
4073 	}
4074 }
4075 
4076 static int rtl8169_rx_fill(struct rtl8169_private *tp)
4077 {
4078 	int i;
4079 
4080 	for (i = 0; i < NUM_RX_DESC; i++) {
4081 		struct page *data;
4082 
4083 		data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
4084 		if (!data) {
4085 			rtl8169_rx_clear(tp);
4086 			return -ENOMEM;
4087 		}
4088 		tp->Rx_databuff[i] = data;
4089 	}
4090 
4091 	/* mark as last descriptor in the ring */
4092 	tp->RxDescArray[NUM_RX_DESC - 1].opts1 |= cpu_to_le32(RingEnd);
4093 
4094 	return 0;
4095 }
4096 
4097 static int rtl8169_init_ring(struct rtl8169_private *tp)
4098 {
4099 	rtl8169_init_ring_indexes(tp);
4100 
4101 	memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
4102 	memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
4103 
4104 	return rtl8169_rx_fill(tp);
4105 }
4106 
4107 static void rtl8169_unmap_tx_skb(struct rtl8169_private *tp, unsigned int entry)
4108 {
4109 	struct ring_info *tx_skb = tp->tx_skb + entry;
4110 	struct TxDesc *desc = tp->TxDescArray + entry;
4111 
4112 	dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr), tx_skb->len,
4113 			 DMA_TO_DEVICE);
4114 	memset(desc, 0, sizeof(*desc));
4115 	memset(tx_skb, 0, sizeof(*tx_skb));
4116 }
4117 
4118 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4119 				   unsigned int n)
4120 {
4121 	unsigned int i;
4122 
4123 	for (i = 0; i < n; i++) {
4124 		unsigned int entry = (start + i) % NUM_TX_DESC;
4125 		struct ring_info *tx_skb = tp->tx_skb + entry;
4126 		unsigned int len = tx_skb->len;
4127 
4128 		if (len) {
4129 			struct sk_buff *skb = tx_skb->skb;
4130 
4131 			rtl8169_unmap_tx_skb(tp, entry);
4132 			if (skb)
4133 				dev_consume_skb_any(skb);
4134 		}
4135 	}
4136 }
4137 
4138 static void rtl8169_tx_clear(struct rtl8169_private *tp)
4139 {
4140 	rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
4141 	netdev_reset_queue(tp->dev);
4142 }
4143 
4144 static void rtl8169_cleanup(struct rtl8169_private *tp)
4145 {
4146 	napi_disable(&tp->napi);
4147 
4148 	/* Give a racing hard_start_xmit a few cycles to complete. */
4149 	synchronize_net();
4150 
4151 	/* Disable interrupts */
4152 	rtl8169_irq_mask_and_ack(tp);
4153 
4154 	rtl_rx_close(tp);
4155 
4156 	switch (tp->mac_version) {
4157 	case RTL_GIGA_MAC_VER_28:
4158 	case RTL_GIGA_MAC_VER_31:
4159 		rtl_loop_wait_low(tp, &rtl_npq_cond, 20, 2000);
4160 		break;
4161 	case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4162 		RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4163 		rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4164 		break;
4165 	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_66:
4166 		rtl_enable_rxdvgate(tp);
4167 		fsleep(2000);
4168 		break;
4169 	default:
4170 		RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4171 		fsleep(100);
4172 		break;
4173 	}
4174 
4175 	rtl_hw_reset(tp);
4176 
4177 	rtl8169_tx_clear(tp);
4178 	rtl8169_init_ring_indexes(tp);
4179 }
4180 
4181 static void rtl_reset_work(struct rtl8169_private *tp)
4182 {
4183 	int i;
4184 
4185 	netif_stop_queue(tp->dev);
4186 
4187 	rtl8169_cleanup(tp);
4188 
4189 	for (i = 0; i < NUM_RX_DESC; i++)
4190 		rtl8169_mark_to_asic(tp->RxDescArray + i);
4191 
4192 	napi_enable(&tp->napi);
4193 	rtl_hw_start(tp);
4194 }
4195 
4196 static void rtl8169_tx_timeout(struct net_device *dev, unsigned int txqueue)
4197 {
4198 	struct rtl8169_private *tp = netdev_priv(dev);
4199 
4200 	rtl_schedule_task(tp, RTL_FLAG_TASK_TX_TIMEOUT);
4201 }
4202 
4203 static int rtl8169_tx_map(struct rtl8169_private *tp, const u32 *opts, u32 len,
4204 			  void *addr, unsigned int entry, bool desc_own)
4205 {
4206 	struct TxDesc *txd = tp->TxDescArray + entry;
4207 	struct device *d = tp_to_dev(tp);
4208 	dma_addr_t mapping;
4209 	u32 opts1;
4210 	int ret;
4211 
4212 	mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
4213 	ret = dma_mapping_error(d, mapping);
4214 	if (unlikely(ret)) {
4215 		if (net_ratelimit())
4216 			netdev_err(tp->dev, "Failed to map TX data!\n");
4217 		return ret;
4218 	}
4219 
4220 	txd->addr = cpu_to_le64(mapping);
4221 	txd->opts2 = cpu_to_le32(opts[1]);
4222 
4223 	opts1 = opts[0] | len;
4224 	if (entry == NUM_TX_DESC - 1)
4225 		opts1 |= RingEnd;
4226 	if (desc_own)
4227 		opts1 |= DescOwn;
4228 	txd->opts1 = cpu_to_le32(opts1);
4229 
4230 	tp->tx_skb[entry].len = len;
4231 
4232 	return 0;
4233 }
4234 
4235 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4236 			      const u32 *opts, unsigned int entry)
4237 {
4238 	struct skb_shared_info *info = skb_shinfo(skb);
4239 	unsigned int cur_frag;
4240 
4241 	for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4242 		const skb_frag_t *frag = info->frags + cur_frag;
4243 		void *addr = skb_frag_address(frag);
4244 		u32 len = skb_frag_size(frag);
4245 
4246 		entry = (entry + 1) % NUM_TX_DESC;
4247 
4248 		if (unlikely(rtl8169_tx_map(tp, opts, len, addr, entry, true)))
4249 			goto err_out;
4250 	}
4251 
4252 	return 0;
4253 
4254 err_out:
4255 	rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4256 	return -EIO;
4257 }
4258 
4259 static bool rtl_skb_is_udp(struct sk_buff *skb)
4260 {
4261 	int no = skb_network_offset(skb);
4262 	struct ipv6hdr *i6h, _i6h;
4263 	struct iphdr *ih, _ih;
4264 
4265 	switch (vlan_get_protocol(skb)) {
4266 	case htons(ETH_P_IP):
4267 		ih = skb_header_pointer(skb, no, sizeof(_ih), &_ih);
4268 		return ih && ih->protocol == IPPROTO_UDP;
4269 	case htons(ETH_P_IPV6):
4270 		i6h = skb_header_pointer(skb, no, sizeof(_i6h), &_i6h);
4271 		return i6h && i6h->nexthdr == IPPROTO_UDP;
4272 	default:
4273 		return false;
4274 	}
4275 }
4276 
4277 #define RTL_MIN_PATCH_LEN	47
4278 
4279 /* see rtl8125_get_patch_pad_len() in r8125 vendor driver */
4280 static unsigned int rtl8125_quirk_udp_padto(struct rtl8169_private *tp,
4281 					    struct sk_buff *skb)
4282 {
4283 	unsigned int padto = 0, len = skb->len;
4284 
4285 	if (len < 128 + RTL_MIN_PATCH_LEN && rtl_skb_is_udp(skb) &&
4286 	    skb_transport_header_was_set(skb)) {
4287 		unsigned int trans_data_len = skb_tail_pointer(skb) -
4288 					      skb_transport_header(skb);
4289 
4290 		if (trans_data_len >= offsetof(struct udphdr, len) &&
4291 		    trans_data_len < RTL_MIN_PATCH_LEN) {
4292 			u16 dest = ntohs(udp_hdr(skb)->dest);
4293 
4294 			/* dest is a standard PTP port */
4295 			if (dest == 319 || dest == 320)
4296 				padto = len + RTL_MIN_PATCH_LEN - trans_data_len;
4297 		}
4298 
4299 		if (trans_data_len < sizeof(struct udphdr))
4300 			padto = max_t(unsigned int, padto,
4301 				      len + sizeof(struct udphdr) - trans_data_len);
4302 	}
4303 
4304 	return padto;
4305 }
4306 
4307 static unsigned int rtl_quirk_packet_padto(struct rtl8169_private *tp,
4308 					   struct sk_buff *skb)
4309 {
4310 	unsigned int padto = 0;
4311 
4312 	switch (tp->mac_version) {
4313 	case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_63:
4314 		padto = rtl8125_quirk_udp_padto(tp, skb);
4315 		break;
4316 	default:
4317 		break;
4318 	}
4319 
4320 	switch (tp->mac_version) {
4321 	case RTL_GIGA_MAC_VER_34:
4322 	case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_66:
4323 		padto = max_t(unsigned int, padto, ETH_ZLEN);
4324 		break;
4325 	default:
4326 		break;
4327 	}
4328 
4329 	return padto;
4330 }
4331 
4332 static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
4333 {
4334 	u32 mss = skb_shinfo(skb)->gso_size;
4335 
4336 	if (mss) {
4337 		opts[0] |= TD_LSO;
4338 		opts[0] |= mss << TD0_MSS_SHIFT;
4339 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4340 		const struct iphdr *ip = ip_hdr(skb);
4341 
4342 		if (ip->protocol == IPPROTO_TCP)
4343 			opts[0] |= TD0_IP_CS | TD0_TCP_CS;
4344 		else if (ip->protocol == IPPROTO_UDP)
4345 			opts[0] |= TD0_IP_CS | TD0_UDP_CS;
4346 		else
4347 			WARN_ON_ONCE(1);
4348 	}
4349 }
4350 
4351 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
4352 				struct sk_buff *skb, u32 *opts)
4353 {
4354 	struct skb_shared_info *shinfo = skb_shinfo(skb);
4355 	u32 mss = shinfo->gso_size;
4356 
4357 	if (mss) {
4358 		if (shinfo->gso_type & SKB_GSO_TCPV4) {
4359 			opts[0] |= TD1_GTSENV4;
4360 		} else if (shinfo->gso_type & SKB_GSO_TCPV6) {
4361 			if (skb_cow_head(skb, 0))
4362 				return false;
4363 
4364 			tcp_v6_gso_csum_prep(skb);
4365 			opts[0] |= TD1_GTSENV6;
4366 		} else {
4367 			WARN_ON_ONCE(1);
4368 		}
4369 
4370 		opts[0] |= skb_transport_offset(skb) << GTTCPHO_SHIFT;
4371 		opts[1] |= mss << TD1_MSS_SHIFT;
4372 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4373 		u8 ip_protocol;
4374 
4375 		switch (vlan_get_protocol(skb)) {
4376 		case htons(ETH_P_IP):
4377 			opts[1] |= TD1_IPv4_CS;
4378 			ip_protocol = ip_hdr(skb)->protocol;
4379 			break;
4380 
4381 		case htons(ETH_P_IPV6):
4382 			opts[1] |= TD1_IPv6_CS;
4383 			ip_protocol = ipv6_hdr(skb)->nexthdr;
4384 			break;
4385 
4386 		default:
4387 			ip_protocol = IPPROTO_RAW;
4388 			break;
4389 		}
4390 
4391 		if (ip_protocol == IPPROTO_TCP)
4392 			opts[1] |= TD1_TCP_CS;
4393 		else if (ip_protocol == IPPROTO_UDP)
4394 			opts[1] |= TD1_UDP_CS;
4395 		else
4396 			WARN_ON_ONCE(1);
4397 
4398 		opts[1] |= skb_transport_offset(skb) << TCPHO_SHIFT;
4399 	} else {
4400 		unsigned int padto = rtl_quirk_packet_padto(tp, skb);
4401 
4402 		/* skb_padto would free the skb on error */
4403 		return !__skb_put_padto(skb, padto, false);
4404 	}
4405 
4406 	return true;
4407 }
4408 
4409 static unsigned int rtl_tx_slots_avail(struct rtl8169_private *tp)
4410 {
4411 	return READ_ONCE(tp->dirty_tx) + NUM_TX_DESC - READ_ONCE(tp->cur_tx);
4412 }
4413 
4414 /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
4415 static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
4416 {
4417 	switch (tp->mac_version) {
4418 	case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
4419 	case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
4420 		return false;
4421 	default:
4422 		return true;
4423 	}
4424 }
4425 
4426 static void rtl8169_doorbell(struct rtl8169_private *tp)
4427 {
4428 	if (rtl_is_8125(tp))
4429 		RTL_W16(tp, TxPoll_8125, BIT(0));
4430 	else
4431 		RTL_W8(tp, TxPoll, NPQ);
4432 }
4433 
4434 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4435 				      struct net_device *dev)
4436 {
4437 	struct rtl8169_private *tp = netdev_priv(dev);
4438 	unsigned int entry = tp->cur_tx % NUM_TX_DESC;
4439 	struct TxDesc *txd_first, *txd_last;
4440 	bool stop_queue, door_bell;
4441 	unsigned int frags;
4442 	u32 opts[2];
4443 
4444 	if (unlikely(!rtl_tx_slots_avail(tp))) {
4445 		if (net_ratelimit())
4446 			netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
4447 		netif_stop_queue(dev);
4448 		return NETDEV_TX_BUSY;
4449 	}
4450 
4451 	opts[1] = rtl8169_tx_vlan_tag(skb);
4452 	opts[0] = 0;
4453 
4454 	if (!rtl_chip_supports_csum_v2(tp))
4455 		rtl8169_tso_csum_v1(skb, opts);
4456 	else if (!rtl8169_tso_csum_v2(tp, skb, opts))
4457 		goto err_dma_0;
4458 
4459 	if (unlikely(rtl8169_tx_map(tp, opts, skb_headlen(skb), skb->data,
4460 				    entry, false)))
4461 		goto err_dma_0;
4462 
4463 	txd_first = tp->TxDescArray + entry;
4464 
4465 	frags = skb_shinfo(skb)->nr_frags;
4466 	if (frags) {
4467 		if (rtl8169_xmit_frags(tp, skb, opts, entry))
4468 			goto err_dma_1;
4469 		entry = (entry + frags) % NUM_TX_DESC;
4470 	}
4471 
4472 	txd_last = tp->TxDescArray + entry;
4473 	txd_last->opts1 |= cpu_to_le32(LastFrag);
4474 	tp->tx_skb[entry].skb = skb;
4475 
4476 	skb_tx_timestamp(skb);
4477 
4478 	/* Force memory writes to complete before releasing descriptor */
4479 	dma_wmb();
4480 
4481 	door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more());
4482 
4483 	txd_first->opts1 |= cpu_to_le32(DescOwn | FirstFrag);
4484 
4485 	/* rtl_tx needs to see descriptor changes before updated tp->cur_tx */
4486 	smp_wmb();
4487 
4488 	WRITE_ONCE(tp->cur_tx, tp->cur_tx + frags + 1);
4489 
4490 	stop_queue = !netif_subqueue_maybe_stop(dev, 0, rtl_tx_slots_avail(tp),
4491 						R8169_TX_STOP_THRS,
4492 						R8169_TX_START_THRS);
4493 	if (door_bell || stop_queue)
4494 		rtl8169_doorbell(tp);
4495 
4496 	return NETDEV_TX_OK;
4497 
4498 err_dma_1:
4499 	rtl8169_unmap_tx_skb(tp, entry);
4500 err_dma_0:
4501 	dev_kfree_skb_any(skb);
4502 	dev->stats.tx_dropped++;
4503 	return NETDEV_TX_OK;
4504 }
4505 
4506 static unsigned int rtl_last_frag_len(struct sk_buff *skb)
4507 {
4508 	struct skb_shared_info *info = skb_shinfo(skb);
4509 	unsigned int nr_frags = info->nr_frags;
4510 
4511 	if (!nr_frags)
4512 		return UINT_MAX;
4513 
4514 	return skb_frag_size(info->frags + nr_frags - 1);
4515 }
4516 
4517 /* Workaround for hw issues with TSO on RTL8168evl */
4518 static netdev_features_t rtl8168evl_fix_tso(struct sk_buff *skb,
4519 					    netdev_features_t features)
4520 {
4521 	/* IPv4 header has options field */
4522 	if (vlan_get_protocol(skb) == htons(ETH_P_IP) &&
4523 	    ip_hdrlen(skb) > sizeof(struct iphdr))
4524 		features &= ~NETIF_F_ALL_TSO;
4525 
4526 	/* IPv4 TCP header has options field */
4527 	else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 &&
4528 		 tcp_hdrlen(skb) > sizeof(struct tcphdr))
4529 		features &= ~NETIF_F_ALL_TSO;
4530 
4531 	else if (rtl_last_frag_len(skb) <= 6)
4532 		features &= ~NETIF_F_ALL_TSO;
4533 
4534 	return features;
4535 }
4536 
4537 static netdev_features_t rtl8169_features_check(struct sk_buff *skb,
4538 						struct net_device *dev,
4539 						netdev_features_t features)
4540 {
4541 	struct rtl8169_private *tp = netdev_priv(dev);
4542 
4543 	if (skb_is_gso(skb)) {
4544 		if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4545 			features = rtl8168evl_fix_tso(skb, features);
4546 
4547 		if (skb_transport_offset(skb) > GTTCPHO_MAX &&
4548 		    rtl_chip_supports_csum_v2(tp))
4549 			features &= ~NETIF_F_ALL_TSO;
4550 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4551 		/* work around hw bug on some chip versions */
4552 		if (skb->len < ETH_ZLEN)
4553 			features &= ~NETIF_F_CSUM_MASK;
4554 
4555 		if (rtl_quirk_packet_padto(tp, skb))
4556 			features &= ~NETIF_F_CSUM_MASK;
4557 
4558 		if (skb_transport_offset(skb) > TCPHO_MAX &&
4559 		    rtl_chip_supports_csum_v2(tp))
4560 			features &= ~NETIF_F_CSUM_MASK;
4561 	}
4562 
4563 	return vlan_features_check(skb, features);
4564 }
4565 
4566 static void rtl8169_pcierr_interrupt(struct net_device *dev)
4567 {
4568 	struct rtl8169_private *tp = netdev_priv(dev);
4569 	struct pci_dev *pdev = tp->pci_dev;
4570 	int pci_status_errs;
4571 	u16 pci_cmd;
4572 
4573 	pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4574 
4575 	pci_status_errs = pci_status_get_and_clear_errors(pdev);
4576 
4577 	if (net_ratelimit())
4578 		netdev_err(dev, "PCI error (cmd = 0x%04x, status_errs = 0x%04x)\n",
4579 			   pci_cmd, pci_status_errs);
4580 
4581 	rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
4582 }
4583 
4584 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
4585 		   int budget)
4586 {
4587 	unsigned int dirty_tx, bytes_compl = 0, pkts_compl = 0;
4588 	struct sk_buff *skb;
4589 
4590 	dirty_tx = tp->dirty_tx;
4591 
4592 	while (READ_ONCE(tp->cur_tx) != dirty_tx) {
4593 		unsigned int entry = dirty_tx % NUM_TX_DESC;
4594 		u32 status;
4595 
4596 		status = le32_to_cpu(READ_ONCE(tp->TxDescArray[entry].opts1));
4597 		if (status & DescOwn)
4598 			break;
4599 
4600 		skb = tp->tx_skb[entry].skb;
4601 		rtl8169_unmap_tx_skb(tp, entry);
4602 
4603 		if (skb) {
4604 			pkts_compl++;
4605 			bytes_compl += skb->len;
4606 			napi_consume_skb(skb, budget);
4607 		}
4608 		dirty_tx++;
4609 	}
4610 
4611 	if (tp->dirty_tx != dirty_tx) {
4612 		dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl);
4613 		WRITE_ONCE(tp->dirty_tx, dirty_tx);
4614 
4615 		netif_subqueue_completed_wake(dev, 0, pkts_compl, bytes_compl,
4616 					      rtl_tx_slots_avail(tp),
4617 					      R8169_TX_START_THRS);
4618 		/*
4619 		 * 8168 hack: TxPoll requests are lost when the Tx packets are
4620 		 * too close. Let's kick an extra TxPoll request when a burst
4621 		 * of start_xmit activity is detected (if it is not detected,
4622 		 * it is slow enough). -- FR
4623 		 * If skb is NULL then we come here again once a tx irq is
4624 		 * triggered after the last fragment is marked transmitted.
4625 		 */
4626 		if (READ_ONCE(tp->cur_tx) != dirty_tx && skb)
4627 			rtl8169_doorbell(tp);
4628 	}
4629 }
4630 
4631 static inline int rtl8169_fragmented_frame(u32 status)
4632 {
4633 	return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4634 }
4635 
4636 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
4637 {
4638 	u32 status = opts1 & (RxProtoMask | RxCSFailMask);
4639 
4640 	if (status == RxProtoTCP || status == RxProtoUDP)
4641 		skb->ip_summed = CHECKSUM_UNNECESSARY;
4642 	else
4643 		skb_checksum_none_assert(skb);
4644 }
4645 
4646 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget)
4647 {
4648 	struct device *d = tp_to_dev(tp);
4649 	int count;
4650 
4651 	for (count = 0; count < budget; count++, tp->cur_rx++) {
4652 		unsigned int pkt_size, entry = tp->cur_rx % NUM_RX_DESC;
4653 		struct RxDesc *desc = tp->RxDescArray + entry;
4654 		struct sk_buff *skb;
4655 		const void *rx_buf;
4656 		dma_addr_t addr;
4657 		u32 status;
4658 
4659 		status = le32_to_cpu(READ_ONCE(desc->opts1));
4660 		if (status & DescOwn)
4661 			break;
4662 
4663 		/* This barrier is needed to keep us from reading
4664 		 * any other fields out of the Rx descriptor until
4665 		 * we know the status of DescOwn
4666 		 */
4667 		dma_rmb();
4668 
4669 		if (unlikely(status & RxRES)) {
4670 			if (net_ratelimit())
4671 				netdev_warn(dev, "Rx ERROR. status = %08x\n",
4672 					    status);
4673 			dev->stats.rx_errors++;
4674 			if (status & (RxRWT | RxRUNT))
4675 				dev->stats.rx_length_errors++;
4676 			if (status & RxCRC)
4677 				dev->stats.rx_crc_errors++;
4678 
4679 			if (!(dev->features & NETIF_F_RXALL))
4680 				goto release_descriptor;
4681 			else if (status & RxRWT || !(status & (RxRUNT | RxCRC)))
4682 				goto release_descriptor;
4683 		}
4684 
4685 		pkt_size = status & GENMASK(13, 0);
4686 		if (likely(!(dev->features & NETIF_F_RXFCS)))
4687 			pkt_size -= ETH_FCS_LEN;
4688 
4689 		/* The driver does not support incoming fragmented frames.
4690 		 * They are seen as a symptom of over-mtu sized frames.
4691 		 */
4692 		if (unlikely(rtl8169_fragmented_frame(status))) {
4693 			dev->stats.rx_dropped++;
4694 			dev->stats.rx_length_errors++;
4695 			goto release_descriptor;
4696 		}
4697 
4698 		skb = napi_alloc_skb(&tp->napi, pkt_size);
4699 		if (unlikely(!skb)) {
4700 			dev->stats.rx_dropped++;
4701 			goto release_descriptor;
4702 		}
4703 
4704 		addr = le64_to_cpu(desc->addr);
4705 		rx_buf = page_address(tp->Rx_databuff[entry]);
4706 
4707 		dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
4708 		prefetch(rx_buf);
4709 		skb_copy_to_linear_data(skb, rx_buf, pkt_size);
4710 		skb->tail += pkt_size;
4711 		skb->len = pkt_size;
4712 		dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4713 
4714 		rtl8169_rx_csum(skb, status);
4715 		skb->protocol = eth_type_trans(skb, dev);
4716 
4717 		rtl8169_rx_vlan_tag(desc, skb);
4718 
4719 		if (skb->pkt_type == PACKET_MULTICAST)
4720 			dev->stats.multicast++;
4721 
4722 		napi_gro_receive(&tp->napi, skb);
4723 
4724 		dev_sw_netstats_rx_add(dev, pkt_size);
4725 release_descriptor:
4726 		rtl8169_mark_to_asic(desc);
4727 	}
4728 
4729 	return count;
4730 }
4731 
4732 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
4733 {
4734 	struct rtl8169_private *tp = dev_instance;
4735 	u32 status = rtl_get_events(tp);
4736 
4737 	if ((status & 0xffff) == 0xffff || !(status & tp->irq_mask))
4738 		return IRQ_NONE;
4739 
4740 	/* At least RTL8168fp may unexpectedly set the SYSErr bit */
4741 	if (unlikely(status & SYSErr &&
4742 	    tp->mac_version <= RTL_GIGA_MAC_VER_06)) {
4743 		rtl8169_pcierr_interrupt(tp->dev);
4744 		goto out;
4745 	}
4746 
4747 	if (status & LinkChg)
4748 		phy_mac_interrupt(tp->phydev);
4749 
4750 	if (unlikely(status & RxFIFOOver &&
4751 	    tp->mac_version == RTL_GIGA_MAC_VER_11)) {
4752 		netif_stop_queue(tp->dev);
4753 		rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
4754 	}
4755 
4756 	rtl_irq_disable(tp);
4757 	napi_schedule(&tp->napi);
4758 out:
4759 	rtl_ack_events(tp, status);
4760 
4761 	return IRQ_HANDLED;
4762 }
4763 
4764 static void rtl_task(struct work_struct *work)
4765 {
4766 	struct rtl8169_private *tp =
4767 		container_of(work, struct rtl8169_private, wk.work);
4768 	int ret;
4769 
4770 	if (test_and_clear_bit(RTL_FLAG_TASK_TX_TIMEOUT, tp->wk.flags)) {
4771 		/* if chip isn't accessible, reset bus to revive it */
4772 		if (RTL_R32(tp, TxConfig) == ~0) {
4773 			ret = pci_reset_bus(tp->pci_dev);
4774 			if (ret < 0) {
4775 				netdev_err(tp->dev, "Can't reset secondary PCI bus, detach NIC\n");
4776 				netif_device_detach(tp->dev);
4777 				return;
4778 			}
4779 		}
4780 
4781 		/* ASPM compatibility issues are a typical reason for tx timeouts */
4782 		ret = pci_disable_link_state(tp->pci_dev, PCIE_LINK_STATE_L1 |
4783 							  PCIE_LINK_STATE_L0S);
4784 		if (!ret)
4785 			netdev_warn_once(tp->dev, "ASPM disabled on Tx timeout\n");
4786 		goto reset;
4787 	}
4788 
4789 	if (test_and_clear_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags)) {
4790 reset:
4791 		rtl_reset_work(tp);
4792 		netif_wake_queue(tp->dev);
4793 	} else if (test_and_clear_bit(RTL_FLAG_TASK_RESET_NO_QUEUE_WAKE, tp->wk.flags)) {
4794 		rtl_reset_work(tp);
4795 	}
4796 }
4797 
4798 static int rtl8169_poll(struct napi_struct *napi, int budget)
4799 {
4800 	struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
4801 	struct net_device *dev = tp->dev;
4802 	int work_done;
4803 
4804 	rtl_tx(dev, tp, budget);
4805 
4806 	work_done = rtl_rx(dev, tp, budget);
4807 
4808 	if (work_done < budget && napi_complete_done(napi, work_done))
4809 		rtl_irq_enable(tp);
4810 
4811 	return work_done;
4812 }
4813 
4814 static void r8169_phylink_handler(struct net_device *ndev)
4815 {
4816 	struct rtl8169_private *tp = netdev_priv(ndev);
4817 	struct device *d = tp_to_dev(tp);
4818 
4819 	if (netif_carrier_ok(ndev)) {
4820 		rtl_link_chg_patch(tp);
4821 		pm_request_resume(d);
4822 	} else {
4823 		pm_runtime_idle(d);
4824 	}
4825 
4826 	phy_print_status(tp->phydev);
4827 }
4828 
4829 static int r8169_phy_connect(struct rtl8169_private *tp)
4830 {
4831 	struct phy_device *phydev = tp->phydev;
4832 	phy_interface_t phy_mode;
4833 	int ret;
4834 
4835 	phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
4836 		   PHY_INTERFACE_MODE_MII;
4837 
4838 	ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
4839 				 phy_mode);
4840 	if (ret)
4841 		return ret;
4842 
4843 	if (!tp->supports_gmii)
4844 		phy_set_max_speed(phydev, SPEED_100);
4845 
4846 	phy_attached_info(phydev);
4847 
4848 	return 0;
4849 }
4850 
4851 static void rtl8169_down(struct rtl8169_private *tp)
4852 {
4853 	disable_work_sync(&tp->wk.work);
4854 	/* Clear all task flags */
4855 	bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
4856 
4857 	phy_stop(tp->phydev);
4858 
4859 	rtl8169_update_counters(tp);
4860 
4861 	pci_clear_master(tp->pci_dev);
4862 	rtl_pci_commit(tp);
4863 
4864 	rtl8169_cleanup(tp);
4865 	rtl_disable_exit_l1(tp);
4866 	rtl_prepare_power_down(tp);
4867 
4868 	if (tp->dash_type != RTL_DASH_NONE)
4869 		rtl8168_driver_stop(tp);
4870 }
4871 
4872 static void rtl8169_up(struct rtl8169_private *tp)
4873 {
4874 	if (tp->dash_type != RTL_DASH_NONE)
4875 		rtl8168_driver_start(tp);
4876 
4877 	pci_set_master(tp->pci_dev);
4878 	phy_init_hw(tp->phydev);
4879 	phy_resume(tp->phydev);
4880 	rtl8169_init_phy(tp);
4881 	napi_enable(&tp->napi);
4882 	enable_work(&tp->wk.work);
4883 	rtl_reset_work(tp);
4884 
4885 	phy_start(tp->phydev);
4886 }
4887 
4888 static int rtl8169_close(struct net_device *dev)
4889 {
4890 	struct rtl8169_private *tp = netdev_priv(dev);
4891 	struct pci_dev *pdev = tp->pci_dev;
4892 
4893 	pm_runtime_get_sync(&pdev->dev);
4894 
4895 	netif_stop_queue(dev);
4896 	rtl8169_down(tp);
4897 	rtl8169_rx_clear(tp);
4898 
4899 	free_irq(tp->irq, tp);
4900 
4901 	phy_disconnect(tp->phydev);
4902 
4903 	dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4904 			  tp->RxPhyAddr);
4905 	dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4906 			  tp->TxPhyAddr);
4907 	tp->TxDescArray = NULL;
4908 	tp->RxDescArray = NULL;
4909 
4910 	pm_runtime_put_sync(&pdev->dev);
4911 
4912 	return 0;
4913 }
4914 
4915 #ifdef CONFIG_NET_POLL_CONTROLLER
4916 static void rtl8169_netpoll(struct net_device *dev)
4917 {
4918 	struct rtl8169_private *tp = netdev_priv(dev);
4919 
4920 	rtl8169_interrupt(tp->irq, tp);
4921 }
4922 #endif
4923 
4924 static int rtl_open(struct net_device *dev)
4925 {
4926 	struct rtl8169_private *tp = netdev_priv(dev);
4927 	struct pci_dev *pdev = tp->pci_dev;
4928 	unsigned long irqflags;
4929 	int retval = -ENOMEM;
4930 
4931 	pm_runtime_get_sync(&pdev->dev);
4932 
4933 	/*
4934 	 * Rx and Tx descriptors needs 256 bytes alignment.
4935 	 * dma_alloc_coherent provides more.
4936 	 */
4937 	tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4938 					     &tp->TxPhyAddr, GFP_KERNEL);
4939 	if (!tp->TxDescArray)
4940 		goto out;
4941 
4942 	tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4943 					     &tp->RxPhyAddr, GFP_KERNEL);
4944 	if (!tp->RxDescArray)
4945 		goto err_free_tx_0;
4946 
4947 	retval = rtl8169_init_ring(tp);
4948 	if (retval < 0)
4949 		goto err_free_rx_1;
4950 
4951 	rtl_request_firmware(tp);
4952 
4953 	irqflags = pci_dev_msi_enabled(pdev) ? IRQF_NO_THREAD : IRQF_SHARED;
4954 	retval = request_irq(tp->irq, rtl8169_interrupt, irqflags, dev->name, tp);
4955 	if (retval < 0)
4956 		goto err_release_fw_2;
4957 
4958 	retval = r8169_phy_connect(tp);
4959 	if (retval)
4960 		goto err_free_irq;
4961 
4962 	rtl8169_up(tp);
4963 	rtl8169_init_counter_offsets(tp);
4964 	netif_start_queue(dev);
4965 out:
4966 	pm_runtime_put_sync(&pdev->dev);
4967 
4968 	return retval;
4969 
4970 err_free_irq:
4971 	free_irq(tp->irq, tp);
4972 err_release_fw_2:
4973 	rtl_release_firmware(tp);
4974 	rtl8169_rx_clear(tp);
4975 err_free_rx_1:
4976 	dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4977 			  tp->RxPhyAddr);
4978 	tp->RxDescArray = NULL;
4979 err_free_tx_0:
4980 	dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4981 			  tp->TxPhyAddr);
4982 	tp->TxDescArray = NULL;
4983 	goto out;
4984 }
4985 
4986 static void
4987 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
4988 {
4989 	struct rtl8169_private *tp = netdev_priv(dev);
4990 	struct pci_dev *pdev = tp->pci_dev;
4991 	struct rtl8169_counters *counters = tp->counters;
4992 
4993 	pm_runtime_get_noresume(&pdev->dev);
4994 
4995 	netdev_stats_to_stats64(stats, &dev->stats);
4996 	dev_fetch_sw_netstats(stats, dev->tstats);
4997 
4998 	/*
4999 	 * Fetch additional counter values missing in stats collected by driver
5000 	 * from tally counters.
5001 	 */
5002 	if (pm_runtime_active(&pdev->dev))
5003 		rtl8169_update_counters(tp);
5004 
5005 	/*
5006 	 * Subtract values fetched during initalization.
5007 	 * See rtl8169_init_counter_offsets for a description why we do that.
5008 	 */
5009 	stats->tx_errors = le64_to_cpu(counters->tx_errors) -
5010 		le64_to_cpu(tp->tc_offset.tx_errors);
5011 	stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
5012 		le32_to_cpu(tp->tc_offset.tx_multi_collision);
5013 	stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
5014 		le16_to_cpu(tp->tc_offset.tx_aborted);
5015 	stats->rx_missed_errors = le16_to_cpu(counters->rx_missed) -
5016 		le16_to_cpu(tp->tc_offset.rx_missed);
5017 
5018 	pm_runtime_put_noidle(&pdev->dev);
5019 }
5020 
5021 static void rtl8169_net_suspend(struct rtl8169_private *tp)
5022 {
5023 	netif_device_detach(tp->dev);
5024 
5025 	if (netif_running(tp->dev))
5026 		rtl8169_down(tp);
5027 }
5028 
5029 static int rtl8169_runtime_resume(struct device *dev)
5030 {
5031 	struct rtl8169_private *tp = dev_get_drvdata(dev);
5032 
5033 	rtl_rar_set(tp, tp->dev->dev_addr);
5034 	__rtl8169_set_wol(tp, tp->saved_wolopts);
5035 
5036 	if (tp->TxDescArray)
5037 		rtl8169_up(tp);
5038 
5039 	netif_device_attach(tp->dev);
5040 
5041 	return 0;
5042 }
5043 
5044 static int rtl8169_suspend(struct device *device)
5045 {
5046 	struct rtl8169_private *tp = dev_get_drvdata(device);
5047 
5048 	rtnl_lock();
5049 	rtl8169_net_suspend(tp);
5050 	if (!device_may_wakeup(tp_to_dev(tp)))
5051 		clk_disable_unprepare(tp->clk);
5052 	rtnl_unlock();
5053 
5054 	return 0;
5055 }
5056 
5057 static int rtl8169_resume(struct device *device)
5058 {
5059 	struct rtl8169_private *tp = dev_get_drvdata(device);
5060 
5061 	if (!device_may_wakeup(tp_to_dev(tp)))
5062 		clk_prepare_enable(tp->clk);
5063 
5064 	/* Reportedly at least Asus X453MA truncates packets otherwise */
5065 	if (tp->mac_version == RTL_GIGA_MAC_VER_37)
5066 		rtl_init_rxcfg(tp);
5067 
5068 	return rtl8169_runtime_resume(device);
5069 }
5070 
5071 static int rtl8169_runtime_suspend(struct device *device)
5072 {
5073 	struct rtl8169_private *tp = dev_get_drvdata(device);
5074 
5075 	if (!tp->TxDescArray) {
5076 		netif_device_detach(tp->dev);
5077 		return 0;
5078 	}
5079 
5080 	rtnl_lock();
5081 	__rtl8169_set_wol(tp, WAKE_PHY);
5082 	rtl8169_net_suspend(tp);
5083 	rtnl_unlock();
5084 
5085 	return 0;
5086 }
5087 
5088 static int rtl8169_runtime_idle(struct device *device)
5089 {
5090 	struct rtl8169_private *tp = dev_get_drvdata(device);
5091 
5092 	if (tp->dash_enabled)
5093 		return -EBUSY;
5094 
5095 	if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev))
5096 		pm_schedule_suspend(device, 10000);
5097 
5098 	return -EBUSY;
5099 }
5100 
5101 static const struct dev_pm_ops rtl8169_pm_ops = {
5102 	SYSTEM_SLEEP_PM_OPS(rtl8169_suspend, rtl8169_resume)
5103 	RUNTIME_PM_OPS(rtl8169_runtime_suspend, rtl8169_runtime_resume,
5104 		       rtl8169_runtime_idle)
5105 };
5106 
5107 static void rtl_shutdown(struct pci_dev *pdev)
5108 {
5109 	struct rtl8169_private *tp = pci_get_drvdata(pdev);
5110 
5111 	rtnl_lock();
5112 	rtl8169_net_suspend(tp);
5113 	rtnl_unlock();
5114 
5115 	/* Restore original MAC address */
5116 	rtl_rar_set(tp, tp->dev->perm_addr);
5117 
5118 	if (system_state == SYSTEM_POWER_OFF && !tp->dash_enabled) {
5119 		pci_wake_from_d3(pdev, tp->saved_wolopts);
5120 		pci_set_power_state(pdev, PCI_D3hot);
5121 	}
5122 }
5123 
5124 static void rtl_remove_one(struct pci_dev *pdev)
5125 {
5126 	struct rtl8169_private *tp = pci_get_drvdata(pdev);
5127 
5128 	if (pci_dev_run_wake(pdev))
5129 		pm_runtime_get_noresume(&pdev->dev);
5130 
5131 	disable_work_sync(&tp->wk.work);
5132 
5133 	if (IS_ENABLED(CONFIG_R8169_LEDS))
5134 		r8169_remove_leds(tp->leds);
5135 
5136 	unregister_netdev(tp->dev);
5137 
5138 	if (tp->dash_type != RTL_DASH_NONE)
5139 		rtl8168_driver_stop(tp);
5140 
5141 	rtl_release_firmware(tp);
5142 
5143 	/* restore original MAC address */
5144 	rtl_rar_set(tp, tp->dev->perm_addr);
5145 }
5146 
5147 static const struct net_device_ops rtl_netdev_ops = {
5148 	.ndo_open		= rtl_open,
5149 	.ndo_stop		= rtl8169_close,
5150 	.ndo_get_stats64	= rtl8169_get_stats64,
5151 	.ndo_start_xmit		= rtl8169_start_xmit,
5152 	.ndo_features_check	= rtl8169_features_check,
5153 	.ndo_tx_timeout		= rtl8169_tx_timeout,
5154 	.ndo_validate_addr	= eth_validate_addr,
5155 	.ndo_change_mtu		= rtl8169_change_mtu,
5156 	.ndo_fix_features	= rtl8169_fix_features,
5157 	.ndo_set_features	= rtl8169_set_features,
5158 	.ndo_set_mac_address	= rtl_set_mac_address,
5159 	.ndo_eth_ioctl		= phy_do_ioctl_running,
5160 	.ndo_set_rx_mode	= rtl_set_rx_mode,
5161 #ifdef CONFIG_NET_POLL_CONTROLLER
5162 	.ndo_poll_controller	= rtl8169_netpoll,
5163 #endif
5164 
5165 };
5166 
5167 static void rtl_set_irq_mask(struct rtl8169_private *tp)
5168 {
5169 	tp->irq_mask = RxOK | RxErr | TxOK | TxErr | LinkChg;
5170 
5171 	if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
5172 		tp->irq_mask |= SYSErr | RxFIFOOver;
5173 	else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
5174 		/* special workaround needed */
5175 		tp->irq_mask |= RxFIFOOver;
5176 }
5177 
5178 static int rtl_alloc_irq(struct rtl8169_private *tp)
5179 {
5180 	unsigned int flags;
5181 
5182 	switch (tp->mac_version) {
5183 	case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5184 		rtl_unlock_config_regs(tp);
5185 		RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
5186 		rtl_lock_config_regs(tp);
5187 		fallthrough;
5188 	case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_17:
5189 		flags = PCI_IRQ_INTX;
5190 		break;
5191 	default:
5192 		flags = PCI_IRQ_ALL_TYPES;
5193 		break;
5194 	}
5195 
5196 	return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
5197 }
5198 
5199 static void rtl_read_mac_address(struct rtl8169_private *tp,
5200 				 u8 mac_addr[ETH_ALEN])
5201 {
5202 	/* Get MAC address */
5203 	if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) {
5204 		u32 value;
5205 
5206 		value = rtl_eri_read(tp, 0xe0);
5207 		put_unaligned_le32(value, mac_addr);
5208 		value = rtl_eri_read(tp, 0xe4);
5209 		put_unaligned_le16(value, mac_addr + 4);
5210 	} else if (rtl_is_8125(tp)) {
5211 		rtl_read_mac_from_reg(tp, mac_addr, MAC0_BKP);
5212 	}
5213 }
5214 
5215 DECLARE_RTL_COND(rtl_link_list_ready_cond)
5216 {
5217 	return RTL_R8(tp, MCU) & LINK_LIST_RDY;
5218 }
5219 
5220 static void r8168g_wait_ll_share_fifo_ready(struct rtl8169_private *tp)
5221 {
5222 	rtl_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
5223 }
5224 
5225 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
5226 {
5227 	struct rtl8169_private *tp = mii_bus->priv;
5228 
5229 	if (phyaddr > 0)
5230 		return -ENODEV;
5231 
5232 	return rtl_readphy(tp, phyreg);
5233 }
5234 
5235 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
5236 				int phyreg, u16 val)
5237 {
5238 	struct rtl8169_private *tp = mii_bus->priv;
5239 
5240 	if (phyaddr > 0)
5241 		return -ENODEV;
5242 
5243 	rtl_writephy(tp, phyreg, val);
5244 
5245 	return 0;
5246 }
5247 
5248 static int r8169_mdio_register(struct rtl8169_private *tp)
5249 {
5250 	struct pci_dev *pdev = tp->pci_dev;
5251 	struct mii_bus *new_bus;
5252 	int ret;
5253 
5254 	/* On some boards with this chip version the BIOS is buggy and misses
5255 	 * to reset the PHY page selector. This results in the PHY ID read
5256 	 * accessing registers on a different page, returning a more or
5257 	 * less random value. Fix this by resetting the page selector first.
5258 	 */
5259 	if (tp->mac_version == RTL_GIGA_MAC_VER_25 ||
5260 	    tp->mac_version == RTL_GIGA_MAC_VER_26)
5261 		r8169_mdio_write(tp, 0x1f, 0);
5262 
5263 	new_bus = devm_mdiobus_alloc(&pdev->dev);
5264 	if (!new_bus)
5265 		return -ENOMEM;
5266 
5267 	new_bus->name = "r8169";
5268 	new_bus->priv = tp;
5269 	new_bus->parent = &pdev->dev;
5270 	new_bus->irq[0] = PHY_MAC_INTERRUPT;
5271 	snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x-%x",
5272 		 pci_domain_nr(pdev->bus), pci_dev_id(pdev));
5273 
5274 	new_bus->read = r8169_mdio_read_reg;
5275 	new_bus->write = r8169_mdio_write_reg;
5276 
5277 	ret = devm_mdiobus_register(&pdev->dev, new_bus);
5278 	if (ret)
5279 		return ret;
5280 
5281 	tp->phydev = mdiobus_get_phy(new_bus, 0);
5282 	if (!tp->phydev) {
5283 		return -ENODEV;
5284 	} else if (!tp->phydev->drv) {
5285 		/* Most chip versions fail with the genphy driver.
5286 		 * Therefore ensure that the dedicated PHY driver is loaded.
5287 		 */
5288 		dev_err(&pdev->dev, "no dedicated PHY driver found for PHY ID 0x%08x, maybe realtek.ko needs to be added to initramfs?\n",
5289 			tp->phydev->phy_id);
5290 		return -EUNATCH;
5291 	}
5292 
5293 	tp->phydev->mac_managed_pm = true;
5294 	if (rtl_supports_eee(tp))
5295 		phy_support_eee(tp->phydev);
5296 	phy_support_asym_pause(tp->phydev);
5297 
5298 	/* PHY will be woken up in rtl_open() */
5299 	phy_suspend(tp->phydev);
5300 
5301 	return 0;
5302 }
5303 
5304 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
5305 {
5306 	rtl_enable_rxdvgate(tp);
5307 
5308 	RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
5309 	msleep(1);
5310 	RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5311 
5312 	r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
5313 	r8168g_wait_ll_share_fifo_ready(tp);
5314 
5315 	r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15));
5316 	r8168g_wait_ll_share_fifo_ready(tp);
5317 }
5318 
5319 static void rtl_hw_init_8125(struct rtl8169_private *tp)
5320 {
5321 	rtl_enable_rxdvgate(tp);
5322 
5323 	RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
5324 	msleep(1);
5325 	RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5326 
5327 	r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
5328 	r8168g_wait_ll_share_fifo_ready(tp);
5329 
5330 	r8168_mac_ocp_write(tp, 0xc0aa, 0x07d0);
5331 	r8168_mac_ocp_write(tp, 0xc0a6, 0x0150);
5332 	r8168_mac_ocp_write(tp, 0xc01e, 0x5555);
5333 	r8168g_wait_ll_share_fifo_ready(tp);
5334 }
5335 
5336 static void rtl_hw_initialize(struct rtl8169_private *tp)
5337 {
5338 	switch (tp->mac_version) {
5339 	case RTL_GIGA_MAC_VER_51 ... RTL_GIGA_MAC_VER_53:
5340 		rtl8168ep_stop_cmac(tp);
5341 		fallthrough;
5342 	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
5343 		rtl_hw_init_8168g(tp);
5344 		break;
5345 	case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_66:
5346 		rtl_hw_init_8125(tp);
5347 		break;
5348 	default:
5349 		break;
5350 	}
5351 }
5352 
5353 static int rtl_jumbo_max(struct rtl8169_private *tp)
5354 {
5355 	/* Non-GBit versions don't support jumbo frames */
5356 	if (!tp->supports_gmii)
5357 		return 0;
5358 
5359 	switch (tp->mac_version) {
5360 	/* RTL8169 */
5361 	case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5362 		return JUMBO_7K;
5363 	/* RTL8168b */
5364 	case RTL_GIGA_MAC_VER_11:
5365 	case RTL_GIGA_MAC_VER_17:
5366 		return JUMBO_4K;
5367 	/* RTL8168c */
5368 	case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
5369 		return JUMBO_6K;
5370 	default:
5371 		return JUMBO_9K;
5372 	}
5373 }
5374 
5375 static void rtl_init_mac_address(struct rtl8169_private *tp)
5376 {
5377 	u8 mac_addr[ETH_ALEN] __aligned(2) = {};
5378 	struct net_device *dev = tp->dev;
5379 	int rc;
5380 
5381 	rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
5382 	if (!rc)
5383 		goto done;
5384 
5385 	rtl_read_mac_address(tp, mac_addr);
5386 	if (is_valid_ether_addr(mac_addr))
5387 		goto done;
5388 
5389 	rtl_read_mac_from_reg(tp, mac_addr, MAC0);
5390 	if (is_valid_ether_addr(mac_addr))
5391 		goto done;
5392 
5393 	eth_random_addr(mac_addr);
5394 	dev->addr_assign_type = NET_ADDR_RANDOM;
5395 	dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
5396 done:
5397 	eth_hw_addr_set(dev, mac_addr);
5398 	rtl_rar_set(tp, mac_addr);
5399 }
5400 
5401 /* register is set if system vendor successfully tested ASPM 1.2 */
5402 static bool rtl_aspm_is_safe(struct rtl8169_private *tp)
5403 {
5404 	if (tp->mac_version >= RTL_GIGA_MAC_VER_61 &&
5405 	    r8168_mac_ocp_read(tp, 0xc0b2) & 0xf)
5406 		return true;
5407 
5408 	return false;
5409 }
5410 
5411 static umode_t r8169_hwmon_is_visible(const void *drvdata,
5412 				      enum hwmon_sensor_types type,
5413 				      u32 attr, int channel)
5414 {
5415 	return 0444;
5416 }
5417 
5418 static int r8169_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
5419 			    u32 attr, int channel, long *val)
5420 {
5421 	struct rtl8169_private *tp = dev_get_drvdata(dev);
5422 	int val_raw;
5423 
5424 	val_raw = phy_read_paged(tp->phydev, 0xbd8, 0x12) & 0x3ff;
5425 	if (val_raw >= 512)
5426 		val_raw -= 1024;
5427 
5428 	*val = 1000 * val_raw / 2;
5429 
5430 	return 0;
5431 }
5432 
5433 static const struct hwmon_ops r8169_hwmon_ops = {
5434 	.is_visible =  r8169_hwmon_is_visible,
5435 	.read = r8169_hwmon_read,
5436 };
5437 
5438 static const struct hwmon_channel_info * const r8169_hwmon_info[] = {
5439 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
5440 	NULL
5441 };
5442 
5443 static const struct hwmon_chip_info r8169_hwmon_chip_info = {
5444 	.ops = &r8169_hwmon_ops,
5445 	.info = r8169_hwmon_info,
5446 };
5447 
5448 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
5449 {
5450 	struct rtl8169_private *tp;
5451 	int jumbo_max, region, rc;
5452 	enum mac_version chipset;
5453 	struct net_device *dev;
5454 	u32 txconfig;
5455 	u16 xid;
5456 
5457 	dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
5458 	if (!dev)
5459 		return -ENOMEM;
5460 
5461 	SET_NETDEV_DEV(dev, &pdev->dev);
5462 	dev->netdev_ops = &rtl_netdev_ops;
5463 	tp = netdev_priv(dev);
5464 	tp->dev = dev;
5465 	tp->pci_dev = pdev;
5466 	tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
5467 	tp->ocp_base = OCP_STD_PHY_BASE;
5468 
5469 	raw_spin_lock_init(&tp->mac_ocp_lock);
5470 	mutex_init(&tp->led_lock);
5471 
5472 	/* Get the *optional* external "ether_clk" used on some boards */
5473 	tp->clk = devm_clk_get_optional_enabled(&pdev->dev, "ether_clk");
5474 	if (IS_ERR(tp->clk))
5475 		return dev_err_probe(&pdev->dev, PTR_ERR(tp->clk), "failed to get ether_clk\n");
5476 
5477 	/* enable device (incl. PCI PM wakeup and hotplug setup) */
5478 	rc = pcim_enable_device(pdev);
5479 	if (rc < 0)
5480 		return dev_err_probe(&pdev->dev, rc, "enable failure\n");
5481 
5482 	if (pcim_set_mwi(pdev) < 0)
5483 		dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
5484 
5485 	/* use first MMIO region */
5486 	region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
5487 	if (region < 0)
5488 		return dev_err_probe(&pdev->dev, -ENODEV, "no MMIO resource found\n");
5489 
5490 	rc = pcim_iomap_regions(pdev, BIT(region), KBUILD_MODNAME);
5491 	if (rc < 0)
5492 		return dev_err_probe(&pdev->dev, rc, "cannot remap MMIO, aborting\n");
5493 
5494 	tp->mmio_addr = pcim_iomap_table(pdev)[region];
5495 
5496 	txconfig = RTL_R32(tp, TxConfig);
5497 	if (txconfig == ~0U)
5498 		return dev_err_probe(&pdev->dev, -EIO, "PCI read failed\n");
5499 
5500 	xid = (txconfig >> 20) & 0xfcf;
5501 
5502 	/* Identify chip attached to board */
5503 	chipset = rtl8169_get_mac_version(xid, tp->supports_gmii);
5504 	if (chipset == RTL_GIGA_MAC_NONE)
5505 		return dev_err_probe(&pdev->dev, -ENODEV,
5506 				     "unknown chip XID %03x, contact r8169 maintainers (see MAINTAINERS file)\n",
5507 				     xid);
5508 	tp->mac_version = chipset;
5509 
5510 	/* Disable ASPM L1 as that cause random device stop working
5511 	 * problems as well as full system hangs for some PCIe devices users.
5512 	 */
5513 	if (rtl_aspm_is_safe(tp))
5514 		rc = 0;
5515 	else
5516 		rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L1);
5517 	tp->aspm_manageable = !rc;
5518 
5519 	tp->dash_type = rtl_get_dash_type(tp);
5520 	tp->dash_enabled = rtl_dash_is_enabled(tp);
5521 
5522 	tp->cp_cmd = RTL_R16(tp, CPlusCmd) & CPCMD_MASK;
5523 
5524 	if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
5525 	    !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
5526 		dev->features |= NETIF_F_HIGHDMA;
5527 
5528 	rtl_init_rxcfg(tp);
5529 
5530 	rtl8169_irq_mask_and_ack(tp);
5531 
5532 	rtl_hw_initialize(tp);
5533 
5534 	rtl_hw_reset(tp);
5535 
5536 	rc = rtl_alloc_irq(tp);
5537 	if (rc < 0)
5538 		return dev_err_probe(&pdev->dev, rc, "Can't allocate interrupt\n");
5539 
5540 	tp->irq = pci_irq_vector(pdev, 0);
5541 
5542 	INIT_WORK(&tp->wk.work, rtl_task);
5543 	disable_work(&tp->wk.work);
5544 
5545 	rtl_init_mac_address(tp);
5546 
5547 	dev->ethtool_ops = &rtl8169_ethtool_ops;
5548 
5549 	netif_napi_add(dev, &tp->napi, rtl8169_poll);
5550 
5551 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
5552 			   NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
5553 	dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
5554 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
5555 
5556 	/*
5557 	 * Pretend we are using VLANs; This bypasses a nasty bug where
5558 	 * Interrupts stop flowing on high load on 8110SCd controllers.
5559 	 */
5560 	if (tp->mac_version == RTL_GIGA_MAC_VER_05)
5561 		/* Disallow toggling */
5562 		dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
5563 
5564 	if (rtl_chip_supports_csum_v2(tp))
5565 		dev->hw_features |= NETIF_F_IPV6_CSUM;
5566 
5567 	dev->features |= dev->hw_features;
5568 
5569 	if (rtl_chip_supports_csum_v2(tp)) {
5570 		dev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6;
5571 		netif_set_tso_max_size(dev, RTL_GSO_MAX_SIZE_V2);
5572 		netif_set_tso_max_segs(dev, RTL_GSO_MAX_SEGS_V2);
5573 	} else {
5574 		dev->hw_features |= NETIF_F_SG | NETIF_F_TSO;
5575 		netif_set_tso_max_size(dev, RTL_GSO_MAX_SIZE_V1);
5576 		netif_set_tso_max_segs(dev, RTL_GSO_MAX_SEGS_V1);
5577 	}
5578 
5579 	/* There has been a number of reports that using SG/TSO results in
5580 	 * tx timeouts. However for a lot of people SG/TSO works fine.
5581 	 * It's not fully clear which chip versions are affected. Vendor
5582 	 * drivers enable SG/TSO for certain chip versions per default,
5583 	 * let's mimic this here. On other chip versions users can
5584 	 * use ethtool to enable SG/TSO, use at own risk!
5585 	 */
5586 	if (tp->mac_version >= RTL_GIGA_MAC_VER_46 &&
5587 	    tp->mac_version != RTL_GIGA_MAC_VER_61)
5588 		dev->features |= dev->hw_features;
5589 
5590 	dev->hw_features |= NETIF_F_RXALL;
5591 	dev->hw_features |= NETIF_F_RXFCS;
5592 
5593 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
5594 
5595 	netdev_sw_irq_coalesce_default_on(dev);
5596 
5597 	/* configure chip for default features */
5598 	rtl8169_set_features(dev, dev->features);
5599 
5600 	if (!tp->dash_enabled) {
5601 		rtl_set_d3_pll_down(tp, true);
5602 	} else {
5603 		rtl_set_d3_pll_down(tp, false);
5604 		dev->ethtool->wol_enabled = 1;
5605 	}
5606 
5607 	jumbo_max = rtl_jumbo_max(tp);
5608 	if (jumbo_max)
5609 		dev->max_mtu = jumbo_max;
5610 
5611 	rtl_set_irq_mask(tp);
5612 
5613 	tp->fw_name = rtl_chip_infos[chipset].fw_name;
5614 
5615 	tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
5616 					    &tp->counters_phys_addr,
5617 					    GFP_KERNEL);
5618 	if (!tp->counters)
5619 		return -ENOMEM;
5620 
5621 	pci_set_drvdata(pdev, tp);
5622 
5623 	rc = r8169_mdio_register(tp);
5624 	if (rc)
5625 		return rc;
5626 
5627 	/* The temperature sensor is available from RTl8125B */
5628 	if (IS_REACHABLE(CONFIG_HWMON) && tp->mac_version >= RTL_GIGA_MAC_VER_63)
5629 		/* ignore errors */
5630 		devm_hwmon_device_register_with_info(&pdev->dev, "nic_temp", tp,
5631 						     &r8169_hwmon_chip_info,
5632 						     NULL);
5633 	rc = register_netdev(dev);
5634 	if (rc)
5635 		return rc;
5636 
5637 	if (IS_ENABLED(CONFIG_R8169_LEDS)) {
5638 		if (rtl_is_8125(tp))
5639 			tp->leds = rtl8125_init_leds(dev);
5640 		else if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5641 			tp->leds = rtl8168_init_leds(dev);
5642 	}
5643 
5644 	netdev_info(dev, "%s, %pM, XID %03x, IRQ %d\n",
5645 		    rtl_chip_infos[chipset].name, dev->dev_addr, xid, tp->irq);
5646 
5647 	if (jumbo_max)
5648 		netdev_info(dev, "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
5649 			    jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
5650 			    "ok" : "ko");
5651 
5652 	if (tp->dash_type != RTL_DASH_NONE) {
5653 		netdev_info(dev, "DASH %s\n",
5654 			    tp->dash_enabled ? "enabled" : "disabled");
5655 		rtl8168_driver_start(tp);
5656 	}
5657 
5658 	if (pci_dev_run_wake(pdev))
5659 		pm_runtime_put_sync(&pdev->dev);
5660 
5661 	return 0;
5662 }
5663 
5664 static struct pci_driver rtl8169_pci_driver = {
5665 	.name		= KBUILD_MODNAME,
5666 	.id_table	= rtl8169_pci_tbl,
5667 	.probe		= rtl_init_one,
5668 	.remove		= rtl_remove_one,
5669 	.shutdown	= rtl_shutdown,
5670 	.driver.pm	= pm_ptr(&rtl8169_pm_ops),
5671 };
5672 
5673 module_pci_driver(rtl8169_pci_driver);
5674