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