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