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