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