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