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