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