1 // SPDX-License-Identifier: GPL-2.0-only
3 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
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.
9 * See MAINTAINERS file for support contact information.
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>
24 #include <linux/tcp.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/bitfield.h>
29 #include <linux/prefetch.h>
30 #include <linux/ipv6.h>
31 #include <asm/unaligned.h>
32 #include <net/ip6_checksum.h>
33 #include <net/netdev_queues.h>
36 #include "r8169_firmware.h"
38 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
40 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
42 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
43 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
44 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
45 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
46 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
47 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
48 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
49 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
50 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
51 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
52 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
53 #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
54 #define FIRMWARE_8168FP_3 "rtl_nic/rtl8168fp-3.fw"
55 #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
56 #define FIRMWARE_8125A_3 "rtl_nic/rtl8125a-3.fw"
57 #define FIRMWARE_8125B_2 "rtl_nic/rtl8125b-2.fw"
58 #define FIRMWARE_8126A_2 "rtl_nic/rtl8126a-2.fw"
60 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
61 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
63 #define R8169_REGS_SIZE 256
64 #define R8169_RX_BUF_SIZE (SZ_16K - 1)
65 #define NUM_TX_DESC 256 /* Number of Tx descriptor registers */
66 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
67 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
68 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
69 #define R8169_TX_STOP_THRS (MAX_SKB_FRAGS + 1)
70 #define R8169_TX_START_THRS (2 * R8169_TX_STOP_THRS)
72 #define OCP_STD_PHY_BASE 0xa400
74 #define RTL_CFG_NO_GBIT 1
76 /* write/read MMIO register */
77 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
78 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
79 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
80 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
81 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
82 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
84 #define JUMBO_4K (4 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
85 #define JUMBO_6K (6 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
86 #define JUMBO_7K (7 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
87 #define JUMBO_9K (9 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
92 } rtl_chip_infos[] = {
94 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
95 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
96 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
97 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
98 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
100 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
101 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
102 [RTL_GIGA_MAC_VER_09] = {"RTL8102e/RTL8103e" },
103 [RTL_GIGA_MAC_VER_10] = {"RTL8101e/RTL8100e" },
104 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
105 [RTL_GIGA_MAC_VER_14] = {"RTL8401" },
106 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
107 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
108 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
109 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
110 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
111 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
112 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
113 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
114 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
115 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
116 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
117 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
118 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
119 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
120 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
121 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
122 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
123 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
124 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
125 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
126 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
127 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
128 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
129 [RTL_GIGA_MAC_VER_42] = {"RTL8168gu/8111gu", FIRMWARE_8168G_3},
130 [RTL_GIGA_MAC_VER_43] = {"RTL8106eus", FIRMWARE_8106E_2},
131 [RTL_GIGA_MAC_VER_44] = {"RTL8411b", FIRMWARE_8411_2 },
132 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
133 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
134 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
135 [RTL_GIGA_MAC_VER_52] = {"RTL8168fp/RTL8117", FIRMWARE_8168FP_3},
136 [RTL_GIGA_MAC_VER_53] = {"RTL8168fp/RTL8117", },
137 [RTL_GIGA_MAC_VER_61] = {"RTL8125A", FIRMWARE_8125A_3},
138 /* reserve 62 for CFG_METHOD_4 in the vendor driver */
139 [RTL_GIGA_MAC_VER_63] = {"RTL8125B", FIRMWARE_8125B_2},
140 [RTL_GIGA_MAC_VER_65] = {"RTL8126A", FIRMWARE_8126A_2},
143 static const struct pci_device_id rtl8169_pci_tbl[] = {
144 { PCI_VDEVICE(REALTEK, 0x2502) },
145 { PCI_VDEVICE(REALTEK, 0x2600) },
146 { PCI_VDEVICE(REALTEK, 0x8129) },
147 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_NO_GBIT },
148 { PCI_VDEVICE(REALTEK, 0x8161) },
149 { PCI_VDEVICE(REALTEK, 0x8162) },
150 { PCI_VDEVICE(REALTEK, 0x8167) },
151 { PCI_VDEVICE(REALTEK, 0x8168) },
152 { PCI_VDEVICE(NCUBE, 0x8168) },
153 { PCI_VDEVICE(REALTEK, 0x8169) },
154 { PCI_VENDOR_ID_DLINK, 0x4300,
155 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0 },
156 { PCI_VDEVICE(DLINK, 0x4300) },
157 { PCI_VDEVICE(DLINK, 0x4302) },
158 { PCI_VDEVICE(AT, 0xc107) },
159 { PCI_VDEVICE(USR, 0x0116) },
160 { PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0024 },
161 { 0x0001, 0x8168, PCI_ANY_ID, 0x2410 },
162 { PCI_VDEVICE(REALTEK, 0x8125) },
163 { PCI_VDEVICE(REALTEK, 0x8126) },
164 { PCI_VDEVICE(REALTEK, 0x3000) },
168 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
171 MAC0 = 0, /* Ethernet hardware address. */
173 MAR0 = 8, /* Multicast filter. */
174 CounterAddrLow = 0x10,
175 CounterAddrHigh = 0x14,
176 TxDescStartAddrLow = 0x20,
177 TxDescStartAddrHigh = 0x24,
178 TxHDescStartAddrLow = 0x28,
179 TxHDescStartAddrHigh = 0x2c,
188 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
189 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
192 #define RX128_INT_EN (1 << 15) /* 8111c and later */
193 #define RX_MULTI_EN (1 << 14) /* 8111c only */
194 #define RXCFG_FIFO_SHIFT 13
195 /* No threshold before first PCI xfer */
196 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
197 #define RX_EARLY_OFF (1 << 11)
198 #define RX_PAUSE_SLOT_ON (1 << 11) /* 8125b and later */
199 #define RXCFG_DMA_SHIFT 8
200 /* Unlimited maximum PCI burst. */
201 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
207 #define PME_SIGNAL (1 << 5) /* 8168c and later */
218 #define RTL_COALESCE_TX_USECS GENMASK(15, 12)
219 #define RTL_COALESCE_TX_FRAMES GENMASK(11, 8)
220 #define RTL_COALESCE_RX_USECS GENMASK(7, 4)
221 #define RTL_COALESCE_RX_FRAMES GENMASK(3, 0)
223 #define RTL_COALESCE_T_MAX 0x0fU
224 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_T_MAX * 4)
226 RxDescAddrLow = 0xe4,
227 RxDescAddrHigh = 0xe8,
228 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
230 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
232 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
234 #define TxPacketMax (8064 >> 7)
235 #define EarlySize 0x27
238 FuncEventMask = 0xf4,
239 FuncPresetState = 0xf8,
244 FuncForceEvent = 0xfc,
247 enum rtl8168_8101_registers {
250 #define CSIAR_FLAG 0x80000000
251 #define CSIAR_WRITE_CMD 0x80000000
252 #define CSIAR_BYTE_ENABLE 0x0000f000
253 #define CSIAR_ADDR_MASK 0x00000fff
255 #define D3COLD_NO_PLL_DOWN BIT(7)
256 #define D3HOT_NO_PLL_DOWN BIT(6)
257 #define D3_NO_PLL_DOWN (BIT(7) | BIT(6))
259 #define EPHYAR_FLAG 0x80000000
260 #define EPHYAR_WRITE_CMD 0x80000000
261 #define EPHYAR_REG_MASK 0x1f
262 #define EPHYAR_REG_SHIFT 16
263 #define EPHYAR_DATA_MASK 0xffff
265 #define PFM_EN (1 << 6)
266 #define TX_10M_PS_EN (1 << 7)
268 #define FIX_NAK_1 (1 << 4)
269 #define FIX_NAK_2 (1 << 3)
272 #define NOW_IS_OOB (1 << 7)
273 #define TX_EMPTY (1 << 5)
274 #define RX_EMPTY (1 << 4)
275 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
276 #define EN_NDP (1 << 3)
277 #define EN_OOB_RESET (1 << 2)
278 #define LINK_LIST_RDY (1 << 1)
280 #define EFUSEAR_FLAG 0x80000000
281 #define EFUSEAR_WRITE_CMD 0x80000000
282 #define EFUSEAR_READ_CMD 0x00000000
283 #define EFUSEAR_REG_MASK 0x03ff
284 #define EFUSEAR_REG_SHIFT 8
285 #define EFUSEAR_DATA_MASK 0xff
287 #define PFM_D3COLD_EN (1 << 6)
290 enum rtl8168_registers {
296 #define ERIAR_FLAG 0x80000000
297 #define ERIAR_WRITE_CMD 0x80000000
298 #define ERIAR_READ_CMD 0x00000000
299 #define ERIAR_ADDR_BYTE_ALIGN 4
300 #define ERIAR_TYPE_SHIFT 16
301 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
302 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
303 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
304 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
305 #define ERIAR_MASK_SHIFT 12
306 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
307 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
308 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
309 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
310 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
311 EPHY_RXER_NUM = 0x7c,
312 OCPDR = 0xb0, /* OCP GPHY access */
313 #define OCPDR_WRITE_CMD 0x80000000
314 #define OCPDR_READ_CMD 0x00000000
315 #define OCPDR_REG_MASK 0x7f
316 #define OCPDR_GPHY_REG_SHIFT 16
317 #define OCPDR_DATA_MASK 0xffff
319 #define OCPAR_FLAG 0x80000000
320 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
321 #define OCPAR_GPHY_READ_CMD 0x0000f060
323 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
324 MISC = 0xf0, /* 8168e only. */
325 #define TXPLA_RST (1 << 29)
326 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
327 #define PWM_EN (1 << 22)
328 #define RXDV_GATED_EN (1 << 19)
329 #define EARLY_TALLY_EN (1 << 16)
332 enum rtl8125_registers {
334 INT_CFG0_8125 = 0x34,
335 #define INT_CFG0_ENABLE_8125 BIT(0)
336 #define INT_CFG0_CLKREQEN BIT(3)
337 IntrMask_8125 = 0x38,
338 IntrStatus_8125 = 0x3c,
339 INT_CFG1_8125 = 0x7a,
345 EEE_TXIDLE_TIMER_8125 = 0x6048,
348 #define LEDSEL_MASK_8125 0x23f
350 #define RX_VLAN_INNER_8125 BIT(22)
351 #define RX_VLAN_OUTER_8125 BIT(23)
352 #define RX_VLAN_8125 (RX_VLAN_INNER_8125 | RX_VLAN_OUTER_8125)
354 #define RX_FETCH_DFLT_8125 (8 << 27)
356 enum rtl_register_content {
357 /* InterruptStatusBits */
361 TxDescUnavail = 0x0080,
383 /* TXPoll register p.5 */
384 HPQ = 0x80, /* Poll cmd on the high prio queue */
385 NPQ = 0x40, /* Poll cmd on the low prio queue */
386 FSWInt = 0x01, /* Forced software interrupt */
390 Cfg9346_Unlock = 0xc0,
395 #define RX_CONFIG_ACCEPT_ERR_MASK 0x30
396 AcceptBroadcast = 0x08,
397 AcceptMulticast = 0x04,
399 AcceptAllPhys = 0x01,
400 #define RX_CONFIG_ACCEPT_OK_MASK 0x0f
401 #define RX_CONFIG_ACCEPT_MASK 0x3f
404 TxInterFrameGapShift = 24,
405 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
407 /* Config1 register p.24 */
410 Speed_down = (1 << 4),
414 PMEnable = (1 << 0), /* Power Management Enable */
416 /* Config2 register p. 25 */
417 ClkReqEn = (1 << 7), /* Clock Request Enable */
418 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
419 PCI_Clock_66MHz = 0x01,
420 PCI_Clock_33MHz = 0x00,
422 /* Config3 register p.25 */
423 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
424 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
425 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
426 Rdy_to_L23 = (1 << 1), /* L23 Enable */
427 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
429 /* Config4 register */
430 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
432 /* Config5 register p.27 */
433 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
434 MWF = (1 << 5), /* Accept Multicast wakeup frame */
435 UWF = (1 << 4), /* Accept Unicast wakeup frame */
437 LanWake = (1 << 1), /* LanWake enable/disable */
438 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
439 ASPM_en = (1 << 0), /* ASPM enable */
442 EnableBist = (1 << 15), // 8168 8101
443 Mac_dbgo_oe = (1 << 14), // 8168 8101
444 EnAnaPLL = (1 << 14), // 8169
445 Normal_mode = (1 << 13), // unused
446 Force_half_dup = (1 << 12), // 8168 8101
447 Force_rxflow_en = (1 << 11), // 8168 8101
448 Force_txflow_en = (1 << 10), // 8168 8101
449 Cxpl_dbg_sel = (1 << 9), // 8168 8101
450 ASF = (1 << 8), // 8168 8101
451 PktCntrDisable = (1 << 7), // 8168 8101
452 Mac_dbgo_sel = 0x001c, // 8168
457 #define INTT_MASK GENMASK(1, 0)
458 #define CPCMD_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
460 /* rtl8169_PHYstatus */
470 /* ResetCounterCommand */
473 /* DumpCounterCommand */
476 /* magic enable v2 */
477 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
481 /* First doubleword. */
482 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
483 RingEnd = (1 << 30), /* End of descriptor ring */
484 FirstFrag = (1 << 29), /* First segment of a packet */
485 LastFrag = (1 << 28), /* Final segment of a packet */
489 enum rtl_tx_desc_bit {
490 /* First doubleword. */
491 TD_LSO = (1 << 27), /* Large Send Offload */
492 #define TD_MSS_MAX 0x07ffu /* MSS value */
494 /* Second doubleword. */
495 TxVlanTag = (1 << 17), /* Add VLAN tag */
498 /* 8169, 8168b and 810x except 8102e. */
499 enum rtl_tx_desc_bit_0 {
500 /* First doubleword. */
501 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
502 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
503 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
504 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
507 /* 8102e, 8168c and beyond. */
508 enum rtl_tx_desc_bit_1 {
509 /* First doubleword. */
510 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
511 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
512 #define GTTCPHO_SHIFT 18
513 #define GTTCPHO_MAX 0x7f
515 /* Second doubleword. */
516 #define TCPHO_SHIFT 18
517 #define TCPHO_MAX 0x3ff
518 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
519 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
520 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
521 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
522 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
525 enum rtl_rx_desc_bit {
527 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
528 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
530 #define RxProtoUDP (PID1)
531 #define RxProtoTCP (PID0)
532 #define RxProtoIP (PID1 | PID0)
533 #define RxProtoMask RxProtoIP
535 IPFail = (1 << 16), /* IP checksum failed */
536 UDPFail = (1 << 15), /* UDP/IP checksum failed */
537 TCPFail = (1 << 14), /* TCP/IP checksum failed */
539 #define RxCSFailMask (IPFail | UDPFail | TCPFail)
541 RxVlanTag = (1 << 16), /* VLAN tag available */
544 #define RTL_GSO_MAX_SIZE_V1 32000
545 #define RTL_GSO_MAX_SEGS_V1 24
546 #define RTL_GSO_MAX_SIZE_V2 64000
547 #define RTL_GSO_MAX_SEGS_V2 64
566 struct rtl8169_counters {
573 __le32 tx_one_collision;
574 __le32 tx_multi_collision;
582 struct rtl8169_tc_offsets {
585 __le32 tx_multi_collision;
591 RTL_FLAG_TASK_ENABLED = 0,
592 RTL_FLAG_TASK_RESET_PENDING,
593 RTL_FLAG_TASK_RESET_NO_QUEUE_WAKE,
594 RTL_FLAG_TASK_TX_TIMEOUT,
604 struct rtl8169_private {
605 void __iomem *mmio_addr; /* memory map physical address */
606 struct pci_dev *pci_dev;
607 struct net_device *dev;
608 struct phy_device *phydev;
609 struct napi_struct napi;
610 enum mac_version mac_version;
611 enum rtl_dash_type dash_type;
612 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
613 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
615 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
616 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
617 dma_addr_t TxPhyAddr;
618 dma_addr_t RxPhyAddr;
619 struct page *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
620 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
628 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
629 struct work_struct work;
632 raw_spinlock_t config25_lock;
633 raw_spinlock_t mac_ocp_lock;
634 struct mutex led_lock; /* serialize LED ctrl RMW access */
636 raw_spinlock_t cfg9346_usage_lock;
637 int cfg9346_usage_count;
639 unsigned supports_gmii:1;
640 unsigned aspm_manageable:1;
641 unsigned dash_enabled:1;
642 dma_addr_t counters_phys_addr;
643 struct rtl8169_counters *counters;
644 struct rtl8169_tc_offsets tc_offset;
648 struct rtl_fw *rtl_fw;
650 struct r8169_led_classdev *leds;
655 typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
657 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
658 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
659 MODULE_SOFTDEP("pre: realtek");
660 MODULE_LICENSE("GPL");
661 MODULE_FIRMWARE(FIRMWARE_8168D_1);
662 MODULE_FIRMWARE(FIRMWARE_8168D_2);
663 MODULE_FIRMWARE(FIRMWARE_8168E_1);
664 MODULE_FIRMWARE(FIRMWARE_8168E_2);
665 MODULE_FIRMWARE(FIRMWARE_8168E_3);
666 MODULE_FIRMWARE(FIRMWARE_8105E_1);
667 MODULE_FIRMWARE(FIRMWARE_8168F_1);
668 MODULE_FIRMWARE(FIRMWARE_8168F_2);
669 MODULE_FIRMWARE(FIRMWARE_8402_1);
670 MODULE_FIRMWARE(FIRMWARE_8411_1);
671 MODULE_FIRMWARE(FIRMWARE_8411_2);
672 MODULE_FIRMWARE(FIRMWARE_8106E_1);
673 MODULE_FIRMWARE(FIRMWARE_8106E_2);
674 MODULE_FIRMWARE(FIRMWARE_8168G_2);
675 MODULE_FIRMWARE(FIRMWARE_8168G_3);
676 MODULE_FIRMWARE(FIRMWARE_8168H_2);
677 MODULE_FIRMWARE(FIRMWARE_8168FP_3);
678 MODULE_FIRMWARE(FIRMWARE_8107E_2);
679 MODULE_FIRMWARE(FIRMWARE_8125A_3);
680 MODULE_FIRMWARE(FIRMWARE_8125B_2);
681 MODULE_FIRMWARE(FIRMWARE_8126A_2);
683 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
685 return &tp->pci_dev->dev;
688 static void rtl_lock_config_regs(struct rtl8169_private *tp)
692 raw_spin_lock_irqsave(&tp->cfg9346_usage_lock, flags);
693 if (!--tp->cfg9346_usage_count)
694 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
695 raw_spin_unlock_irqrestore(&tp->cfg9346_usage_lock, flags);
698 static void rtl_unlock_config_regs(struct rtl8169_private *tp)
702 raw_spin_lock_irqsave(&tp->cfg9346_usage_lock, flags);
703 if (!tp->cfg9346_usage_count++)
704 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
705 raw_spin_unlock_irqrestore(&tp->cfg9346_usage_lock, flags);
708 static void rtl_pci_commit(struct rtl8169_private *tp)
710 /* Read an arbitrary register to commit a preceding PCI write */
714 static void rtl_mod_config2(struct rtl8169_private *tp, u8 clear, u8 set)
719 raw_spin_lock_irqsave(&tp->config25_lock, flags);
720 val = RTL_R8(tp, Config2);
721 RTL_W8(tp, Config2, (val & ~clear) | set);
722 raw_spin_unlock_irqrestore(&tp->config25_lock, flags);
725 static void rtl_mod_config5(struct rtl8169_private *tp, u8 clear, u8 set)
730 raw_spin_lock_irqsave(&tp->config25_lock, flags);
731 val = RTL_R8(tp, Config5);
732 RTL_W8(tp, Config5, (val & ~clear) | set);
733 raw_spin_unlock_irqrestore(&tp->config25_lock, flags);
736 static bool rtl_is_8125(struct rtl8169_private *tp)
738 return tp->mac_version >= RTL_GIGA_MAC_VER_61;
741 static bool rtl_is_8168evl_up(struct rtl8169_private *tp)
743 return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
744 tp->mac_version != RTL_GIGA_MAC_VER_39 &&
745 tp->mac_version <= RTL_GIGA_MAC_VER_53;
748 static bool rtl_supports_eee(struct rtl8169_private *tp)
750 return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
751 tp->mac_version != RTL_GIGA_MAC_VER_37 &&
752 tp->mac_version != RTL_GIGA_MAC_VER_39;
755 static void rtl_read_mac_from_reg(struct rtl8169_private *tp, u8 *mac, int reg)
759 for (i = 0; i < ETH_ALEN; i++)
760 mac[i] = RTL_R8(tp, reg + i);
764 bool (*check)(struct rtl8169_private *);
768 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
769 unsigned long usecs, int n, bool high)
773 for (i = 0; i < n; i++) {
774 if (c->check(tp) == high)
780 netdev_err(tp->dev, "%s == %d (loop: %d, delay: %lu).\n",
781 c->msg, !high, n, usecs);
785 static bool rtl_loop_wait_high(struct rtl8169_private *tp,
786 const struct rtl_cond *c,
787 unsigned long d, int n)
789 return rtl_loop_wait(tp, c, d, n, true);
792 static bool rtl_loop_wait_low(struct rtl8169_private *tp,
793 const struct rtl_cond *c,
794 unsigned long d, int n)
796 return rtl_loop_wait(tp, c, d, n, false);
799 #define DECLARE_RTL_COND(name) \
800 static bool name ## _check(struct rtl8169_private *); \
802 static const struct rtl_cond name = { \
803 .check = name ## _check, \
807 static bool name ## _check(struct rtl8169_private *tp)
809 int rtl8168_led_mod_ctrl(struct rtl8169_private *tp, u16 mask, u16 val)
811 struct device *dev = tp_to_dev(tp);
814 ret = pm_runtime_resume_and_get(dev);
818 mutex_lock(&tp->led_lock);
819 RTL_W16(tp, LED_CTRL, (RTL_R16(tp, LED_CTRL) & ~mask) | val);
820 mutex_unlock(&tp->led_lock);
822 pm_runtime_put_sync(dev);
827 int rtl8168_get_led_mode(struct rtl8169_private *tp)
829 struct device *dev = tp_to_dev(tp);
832 ret = pm_runtime_resume_and_get(dev);
836 ret = RTL_R16(tp, LED_CTRL);
838 pm_runtime_put_sync(dev);
843 static int rtl8125_get_led_reg(int index)
845 static const int led_regs[] = { LEDSEL0, LEDSEL1, LEDSEL2, LEDSEL3 };
847 return led_regs[index];
850 int rtl8125_set_led_mode(struct rtl8169_private *tp, int index, u16 mode)
852 int reg = rtl8125_get_led_reg(index);
853 struct device *dev = tp_to_dev(tp);
857 ret = pm_runtime_resume_and_get(dev);
861 mutex_lock(&tp->led_lock);
862 val = RTL_R16(tp, reg) & ~LEDSEL_MASK_8125;
863 RTL_W16(tp, reg, val | mode);
864 mutex_unlock(&tp->led_lock);
866 pm_runtime_put_sync(dev);
871 int rtl8125_get_led_mode(struct rtl8169_private *tp, int index)
873 int reg = rtl8125_get_led_reg(index);
874 struct device *dev = tp_to_dev(tp);
877 ret = pm_runtime_resume_and_get(dev);
881 ret = RTL_R16(tp, reg);
883 pm_runtime_put_sync(dev);
888 void r8169_get_led_name(struct rtl8169_private *tp, int idx,
889 char *buf, int buf_len)
891 struct pci_dev *pdev = tp->pci_dev;
892 char pdom[8], pfun[8];
895 domain = pci_domain_nr(pdev->bus);
897 snprintf(pdom, sizeof(pdom), "P%d", domain);
901 if (pdev->multifunction)
902 snprintf(pfun, sizeof(pfun), "f%d", PCI_FUNC(pdev->devfn));
906 snprintf(buf, buf_len, "en%sp%ds%d%s-%d::lan", pdom, pdev->bus->number,
907 PCI_SLOT(pdev->devfn), pfun, idx);
910 static void r8168fp_adjust_ocp_cmd(struct rtl8169_private *tp, u32 *cmd, int type)
912 /* based on RTL8168FP_OOBMAC_BASE in vendor driver */
913 if (type == ERIAR_OOB &&
914 (tp->mac_version == RTL_GIGA_MAC_VER_52 ||
915 tp->mac_version == RTL_GIGA_MAC_VER_53))
919 DECLARE_RTL_COND(rtl_eriar_cond)
921 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
924 static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
927 u32 cmd = ERIAR_WRITE_CMD | type | mask | addr;
929 if (WARN(addr & 3 || !mask, "addr: 0x%x, mask: 0x%08x\n", addr, mask))
932 RTL_W32(tp, ERIDR, val);
933 r8168fp_adjust_ocp_cmd(tp, &cmd, type);
934 RTL_W32(tp, ERIAR, cmd);
936 rtl_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
939 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
942 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
945 static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
947 u32 cmd = ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr;
949 r8168fp_adjust_ocp_cmd(tp, &cmd, type);
950 RTL_W32(tp, ERIAR, cmd);
952 return rtl_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
953 RTL_R32(tp, ERIDR) : ~0;
956 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
958 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
961 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 p, u32 m)
963 u32 val = rtl_eri_read(tp, addr);
965 rtl_eri_write(tp, addr, ERIAR_MASK_1111, (val & ~m) | p);
968 static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 p)
970 rtl_w0w1_eri(tp, addr, p, 0);
973 static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 m)
975 rtl_w0w1_eri(tp, addr, 0, m);
978 static bool rtl_ocp_reg_failure(u32 reg)
980 return WARN_ONCE(reg & 0xffff0001, "Invalid ocp reg %x!\n", reg);
983 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
985 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
988 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
990 if (rtl_ocp_reg_failure(reg))
993 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
995 rtl_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
998 static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1000 if (rtl_ocp_reg_failure(reg))
1003 RTL_W32(tp, GPHY_OCP, reg << 15);
1005 return rtl_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1006 (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
1009 static void __r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1011 if (rtl_ocp_reg_failure(reg))
1014 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
1017 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1019 unsigned long flags;
1021 raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags);
1022 __r8168_mac_ocp_write(tp, reg, data);
1023 raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags);
1026 static u16 __r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1028 if (rtl_ocp_reg_failure(reg))
1031 RTL_W32(tp, OCPDR, reg << 15);
1033 return RTL_R32(tp, OCPDR);
1036 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1038 unsigned long flags;
1041 raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags);
1042 val = __r8168_mac_ocp_read(tp, reg);
1043 raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags);
1048 static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask,
1051 unsigned long flags;
1054 raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags);
1055 data = __r8168_mac_ocp_read(tp, reg);
1056 __r8168_mac_ocp_write(tp, reg, (data & ~mask) | set);
1057 raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags);
1060 /* Work around a hw issue with RTL8168g PHY, the quirk disables
1061 * PHY MCU interrupts before PHY power-down.
1063 static void rtl8168g_phy_suspend_quirk(struct rtl8169_private *tp, int value)
1065 switch (tp->mac_version) {
1066 case RTL_GIGA_MAC_VER_40:
1067 if (value & BMCR_RESET || !(value & BMCR_PDOWN))
1068 rtl_eri_set_bits(tp, 0x1a8, 0xfc000000);
1070 rtl_eri_clear_bits(tp, 0x1a8, 0xfc000000);
1077 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1080 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1084 if (tp->ocp_base != OCP_STD_PHY_BASE)
1087 if (tp->ocp_base == OCP_STD_PHY_BASE && reg == MII_BMCR)
1088 rtl8168g_phy_suspend_quirk(tp, value);
1090 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1093 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1096 return tp->ocp_base == OCP_STD_PHY_BASE ? 0 : tp->ocp_base >> 4;
1098 if (tp->ocp_base != OCP_STD_PHY_BASE)
1101 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1104 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1107 tp->ocp_base = value << 4;
1111 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1114 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1116 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1119 DECLARE_RTL_COND(rtl_phyar_cond)
1121 return RTL_R32(tp, PHYAR) & 0x80000000;
1124 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1126 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1128 rtl_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1130 * According to hardware specs a 20us delay is required after write
1131 * complete indication, but before sending next command.
1136 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1140 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
1142 value = rtl_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1143 RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
1146 * According to hardware specs a 20us delay is required after read
1147 * complete indication, but before sending next command.
1154 DECLARE_RTL_COND(rtl_ocpar_cond)
1156 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
1159 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1161 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
1163 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1166 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
1168 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1171 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1173 r8168dp_2_mdio_start(tp);
1175 r8169_mdio_write(tp, reg, value);
1177 r8168dp_2_mdio_stop(tp);
1180 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1184 /* Work around issue with chip reporting wrong PHY ID */
1185 if (reg == MII_PHYSID2)
1188 r8168dp_2_mdio_start(tp);
1190 value = r8169_mdio_read(tp, reg);
1192 r8168dp_2_mdio_stop(tp);
1197 static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
1199 switch (tp->mac_version) {
1200 case RTL_GIGA_MAC_VER_28:
1201 case RTL_GIGA_MAC_VER_31:
1202 r8168dp_2_mdio_write(tp, location, val);
1204 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_65:
1205 r8168g_mdio_write(tp, location, val);
1208 r8169_mdio_write(tp, location, val);
1213 static int rtl_readphy(struct rtl8169_private *tp, int location)
1215 switch (tp->mac_version) {
1216 case RTL_GIGA_MAC_VER_28:
1217 case RTL_GIGA_MAC_VER_31:
1218 return r8168dp_2_mdio_read(tp, location);
1219 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_65:
1220 return r8168g_mdio_read(tp, location);
1222 return r8169_mdio_read(tp, location);
1226 DECLARE_RTL_COND(rtl_ephyar_cond)
1228 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1231 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1233 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1234 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1236 rtl_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1241 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1243 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1245 return rtl_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1246 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1249 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u16 reg)
1251 RTL_W32(tp, OCPAR, 0x0fu << 12 | (reg & 0x0fff));
1252 return rtl_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1253 RTL_R32(tp, OCPDR) : ~0;
1256 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u16 reg)
1258 return _rtl_eri_read(tp, reg, ERIAR_OOB);
1261 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1264 RTL_W32(tp, OCPDR, data);
1265 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1266 rtl_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1269 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1272 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1276 static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
1278 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
1280 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
1283 #define OOB_CMD_RESET 0x00
1284 #define OOB_CMD_DRIVER_START 0x05
1285 #define OOB_CMD_DRIVER_STOP 0x06
1287 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1289 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1292 DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
1296 reg = rtl8168_get_ocp_reg(tp);
1298 return r8168dp_ocp_read(tp, reg) & 0x00000800;
1301 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1303 return r8168ep_ocp_read(tp, 0x124) & 0x00000001;
1306 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1308 return RTL_R8(tp, IBISR0) & 0x20;
1311 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1313 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1314 rtl_loop_wait_high(tp, &rtl_ocp_tx_cond, 50000, 2000);
1315 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1316 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1319 static void rtl_dash_loop_wait(struct rtl8169_private *tp,
1320 const struct rtl_cond *c,
1321 unsigned long usecs, int n, bool high)
1323 if (!tp->dash_enabled)
1325 rtl_loop_wait(tp, c, usecs, n, high);
1328 static void rtl_dash_loop_wait_high(struct rtl8169_private *tp,
1329 const struct rtl_cond *c,
1330 unsigned long d, int n)
1332 rtl_dash_loop_wait(tp, c, d, n, true);
1335 static void rtl_dash_loop_wait_low(struct rtl8169_private *tp,
1336 const struct rtl_cond *c,
1337 unsigned long d, int n)
1339 rtl_dash_loop_wait(tp, c, d, n, false);
1342 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1344 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1345 rtl_dash_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10000, 10);
1348 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1350 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1351 r8168ep_ocp_write(tp, 0x01, 0x30, r8168ep_ocp_read(tp, 0x30) | 0x01);
1352 rtl_dash_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10000, 30);
1355 static void rtl8168_driver_start(struct rtl8169_private *tp)
1357 if (tp->dash_type == RTL_DASH_DP)
1358 rtl8168dp_driver_start(tp);
1360 rtl8168ep_driver_start(tp);
1363 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1365 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1366 rtl_dash_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10000, 10);
1369 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1371 rtl8168ep_stop_cmac(tp);
1372 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1373 r8168ep_ocp_write(tp, 0x01, 0x30, r8168ep_ocp_read(tp, 0x30) | 0x01);
1374 rtl_dash_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10000, 10);
1377 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1379 if (tp->dash_type == RTL_DASH_DP)
1380 rtl8168dp_driver_stop(tp);
1382 rtl8168ep_driver_stop(tp);
1385 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1387 u16 reg = rtl8168_get_ocp_reg(tp);
1389 return r8168dp_ocp_read(tp, reg) & BIT(15);
1392 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1394 return r8168ep_ocp_read(tp, 0x128) & BIT(0);
1397 static bool rtl_dash_is_enabled(struct rtl8169_private *tp)
1399 switch (tp->dash_type) {
1401 return r8168dp_check_dash(tp);
1403 return r8168ep_check_dash(tp);
1409 static enum rtl_dash_type rtl_get_dash_type(struct rtl8169_private *tp)
1411 switch (tp->mac_version) {
1412 case RTL_GIGA_MAC_VER_28:
1413 case RTL_GIGA_MAC_VER_31:
1415 case RTL_GIGA_MAC_VER_51 ... RTL_GIGA_MAC_VER_53:
1418 return RTL_DASH_NONE;
1422 static void rtl_set_d3_pll_down(struct rtl8169_private *tp, bool enable)
1424 switch (tp->mac_version) {
1425 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_26:
1426 case RTL_GIGA_MAC_VER_29 ... RTL_GIGA_MAC_VER_30:
1427 case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_37:
1428 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_65:
1430 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~D3_NO_PLL_DOWN);
1432 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | D3_NO_PLL_DOWN);
1439 static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1441 rtl_eri_clear_bits(tp, 0xdc, BIT(0));
1442 rtl_eri_set_bits(tp, 0xdc, BIT(0));
1445 DECLARE_RTL_COND(rtl_efusear_cond)
1447 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1450 u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1452 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1454 return rtl_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1455 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1458 static u32 rtl_get_events(struct rtl8169_private *tp)
1460 if (rtl_is_8125(tp))
1461 return RTL_R32(tp, IntrStatus_8125);
1463 return RTL_R16(tp, IntrStatus);
1466 static void rtl_ack_events(struct rtl8169_private *tp, u32 bits)
1468 if (rtl_is_8125(tp))
1469 RTL_W32(tp, IntrStatus_8125, bits);
1471 RTL_W16(tp, IntrStatus, bits);
1474 static void rtl_irq_disable(struct rtl8169_private *tp)
1476 if (rtl_is_8125(tp))
1477 RTL_W32(tp, IntrMask_8125, 0);
1479 RTL_W16(tp, IntrMask, 0);
1482 static void rtl_irq_enable(struct rtl8169_private *tp)
1484 if (rtl_is_8125(tp))
1485 RTL_W32(tp, IntrMask_8125, tp->irq_mask);
1487 RTL_W16(tp, IntrMask, tp->irq_mask);
1490 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1492 rtl_irq_disable(tp);
1493 rtl_ack_events(tp, 0xffffffff);
1497 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1499 struct phy_device *phydev = tp->phydev;
1501 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1502 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1503 if (phydev->speed == SPEED_1000) {
1504 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1505 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1506 } else if (phydev->speed == SPEED_100) {
1507 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1508 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1510 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1511 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
1513 rtl_reset_packet_filter(tp);
1514 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1515 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1516 if (phydev->speed == SPEED_1000) {
1517 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1518 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1520 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1521 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
1523 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1524 if (phydev->speed == SPEED_10) {
1525 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1526 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
1528 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
1533 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1535 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1537 struct rtl8169_private *tp = netdev_priv(dev);
1539 wol->supported = WAKE_ANY;
1540 wol->wolopts = tp->saved_wolopts;
1543 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1545 static const struct {
1550 { WAKE_PHY, Config3, LinkUp },
1551 { WAKE_UCAST, Config5, UWF },
1552 { WAKE_BCAST, Config5, BWF },
1553 { WAKE_MCAST, Config5, MWF },
1554 { WAKE_ANY, Config5, LanWake },
1555 { WAKE_MAGIC, Config3, MagicPacket }
1557 unsigned int i, tmp = ARRAY_SIZE(cfg);
1558 unsigned long flags;
1561 rtl_unlock_config_regs(tp);
1563 if (rtl_is_8168evl_up(tp)) {
1565 if (wolopts & WAKE_MAGIC)
1566 rtl_eri_set_bits(tp, 0x0dc, MagicPacket_v2);
1568 rtl_eri_clear_bits(tp, 0x0dc, MagicPacket_v2);
1569 } else if (rtl_is_8125(tp)) {
1571 if (wolopts & WAKE_MAGIC)
1572 r8168_mac_ocp_modify(tp, 0xc0b6, 0, BIT(0));
1574 r8168_mac_ocp_modify(tp, 0xc0b6, BIT(0), 0);
1577 raw_spin_lock_irqsave(&tp->config25_lock, flags);
1578 for (i = 0; i < tmp; i++) {
1579 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1580 if (wolopts & cfg[i].opt)
1581 options |= cfg[i].mask;
1582 RTL_W8(tp, cfg[i].reg, options);
1584 raw_spin_unlock_irqrestore(&tp->config25_lock, flags);
1586 switch (tp->mac_version) {
1587 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
1588 options = RTL_R8(tp, Config1) & ~PMEnable;
1590 options |= PMEnable;
1591 RTL_W8(tp, Config1, options);
1593 case RTL_GIGA_MAC_VER_34:
1594 case RTL_GIGA_MAC_VER_37:
1595 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_65:
1597 rtl_mod_config2(tp, 0, PME_SIGNAL);
1599 rtl_mod_config2(tp, PME_SIGNAL, 0);
1605 rtl_lock_config_regs(tp);
1607 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
1609 if (!tp->dash_enabled) {
1610 rtl_set_d3_pll_down(tp, !wolopts);
1611 tp->dev->wol_enabled = wolopts ? 1 : 0;
1615 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1617 struct rtl8169_private *tp = netdev_priv(dev);
1619 if (wol->wolopts & ~WAKE_ANY)
1622 tp->saved_wolopts = wol->wolopts;
1623 __rtl8169_set_wol(tp, tp->saved_wolopts);
1628 static void rtl8169_get_drvinfo(struct net_device *dev,
1629 struct ethtool_drvinfo *info)
1631 struct rtl8169_private *tp = netdev_priv(dev);
1632 struct rtl_fw *rtl_fw = tp->rtl_fw;
1634 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1635 strscpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1636 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1638 strscpy(info->fw_version, rtl_fw->version,
1639 sizeof(info->fw_version));
1642 static int rtl8169_get_regs_len(struct net_device *dev)
1644 return R8169_REGS_SIZE;
1647 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1648 netdev_features_t features)
1650 struct rtl8169_private *tp = netdev_priv(dev);
1652 if (dev->mtu > TD_MSS_MAX)
1653 features &= ~NETIF_F_ALL_TSO;
1655 if (dev->mtu > ETH_DATA_LEN &&
1656 tp->mac_version > RTL_GIGA_MAC_VER_06)
1657 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_ALL_TSO);
1662 static void rtl_set_rx_config_features(struct rtl8169_private *tp,
1663 netdev_features_t features)
1665 u32 rx_config = RTL_R32(tp, RxConfig);
1667 if (features & NETIF_F_RXALL)
1668 rx_config |= RX_CONFIG_ACCEPT_ERR_MASK;
1670 rx_config &= ~RX_CONFIG_ACCEPT_ERR_MASK;
1672 if (rtl_is_8125(tp)) {
1673 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1674 rx_config |= RX_VLAN_8125;
1676 rx_config &= ~RX_VLAN_8125;
1679 RTL_W32(tp, RxConfig, rx_config);
1682 static int rtl8169_set_features(struct net_device *dev,
1683 netdev_features_t features)
1685 struct rtl8169_private *tp = netdev_priv(dev);
1687 rtl_set_rx_config_features(tp, features);
1689 if (features & NETIF_F_RXCSUM)
1690 tp->cp_cmd |= RxChkSum;
1692 tp->cp_cmd &= ~RxChkSum;
1694 if (!rtl_is_8125(tp)) {
1695 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1696 tp->cp_cmd |= RxVlan;
1698 tp->cp_cmd &= ~RxVlan;
1701 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1707 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1709 return (skb_vlan_tag_present(skb)) ?
1710 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1713 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1715 u32 opts2 = le32_to_cpu(desc->opts2);
1717 if (opts2 & RxVlanTag)
1718 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1721 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1724 struct rtl8169_private *tp = netdev_priv(dev);
1725 u32 __iomem *data = tp->mmio_addr;
1729 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1730 memcpy_fromio(dw++, data++, 4);
1733 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1740 "tx_single_collisions",
1741 "tx_multi_collisions",
1749 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1753 return ARRAY_SIZE(rtl8169_gstrings);
1759 DECLARE_RTL_COND(rtl_counters_cond)
1761 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
1764 static void rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
1766 u32 cmd = lower_32_bits(tp->counters_phys_addr);
1768 RTL_W32(tp, CounterAddrHigh, upper_32_bits(tp->counters_phys_addr));
1770 RTL_W32(tp, CounterAddrLow, cmd);
1771 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
1773 rtl_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
1776 static void rtl8169_update_counters(struct rtl8169_private *tp)
1778 u8 val = RTL_R8(tp, ChipCmd);
1781 * Some chips are unable to dump tally counters when the receiver
1782 * is disabled. If 0xff chip may be in a PCI power-save state.
1784 if (val & CmdRxEnb && val != 0xff)
1785 rtl8169_do_counters(tp, CounterDump);
1788 static void rtl8169_init_counter_offsets(struct rtl8169_private *tp)
1790 struct rtl8169_counters *counters = tp->counters;
1793 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1794 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1795 * reset by a power cycle, while the counter values collected by the
1796 * driver are reset at every driver unload/load cycle.
1798 * To make sure the HW values returned by @get_stats64 match the SW
1799 * values, we collect the initial values at first open(*) and use them
1800 * as offsets to normalize the values returned by @get_stats64.
1802 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1803 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1804 * set at open time by rtl_hw_start.
1807 if (tp->tc_offset.inited)
1810 if (tp->mac_version >= RTL_GIGA_MAC_VER_19) {
1811 rtl8169_do_counters(tp, CounterReset);
1813 rtl8169_update_counters(tp);
1814 tp->tc_offset.tx_errors = counters->tx_errors;
1815 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1816 tp->tc_offset.tx_aborted = counters->tx_aborted;
1817 tp->tc_offset.rx_missed = counters->rx_missed;
1820 tp->tc_offset.inited = true;
1823 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1824 struct ethtool_stats *stats, u64 *data)
1826 struct rtl8169_private *tp = netdev_priv(dev);
1827 struct rtl8169_counters *counters;
1829 counters = tp->counters;
1830 rtl8169_update_counters(tp);
1832 data[0] = le64_to_cpu(counters->tx_packets);
1833 data[1] = le64_to_cpu(counters->rx_packets);
1834 data[2] = le64_to_cpu(counters->tx_errors);
1835 data[3] = le32_to_cpu(counters->rx_errors);
1836 data[4] = le16_to_cpu(counters->rx_missed);
1837 data[5] = le16_to_cpu(counters->align_errors);
1838 data[6] = le32_to_cpu(counters->tx_one_collision);
1839 data[7] = le32_to_cpu(counters->tx_multi_collision);
1840 data[8] = le64_to_cpu(counters->rx_unicast);
1841 data[9] = le64_to_cpu(counters->rx_broadcast);
1842 data[10] = le32_to_cpu(counters->rx_multicast);
1843 data[11] = le16_to_cpu(counters->tx_aborted);
1844 data[12] = le16_to_cpu(counters->tx_underun);
1847 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1851 memcpy(data, rtl8169_gstrings, sizeof(rtl8169_gstrings));
1857 * Interrupt coalescing
1859 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1860 * > 8169, 8168 and 810x line of chipsets
1862 * 8169, 8168, and 8136(810x) serial chipsets support it.
1864 * > 2 - the Tx timer unit at gigabit speed
1866 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1867 * (0xe0) bit 1 and bit 0.
1870 * bit[1:0] \ speed 1000M 100M 10M
1871 * 0 0 320ns 2.56us 40.96us
1872 * 0 1 2.56us 20.48us 327.7us
1873 * 1 0 5.12us 40.96us 655.4us
1874 * 1 1 10.24us 81.92us 1.31ms
1877 * bit[1:0] \ speed 1000M 100M 10M
1878 * 0 0 5us 2.56us 40.96us
1879 * 0 1 40us 20.48us 327.7us
1880 * 1 0 80us 40.96us 655.4us
1881 * 1 1 160us 81.92us 1.31ms
1884 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
1885 struct rtl_coalesce_info {
1890 /* produce array with base delay *1, *8, *8*2, *8*2*2 */
1891 #define COALESCE_DELAY(d) { (d), 8 * (d), 16 * (d), 32 * (d) }
1893 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1894 { SPEED_1000, COALESCE_DELAY(320) },
1895 { SPEED_100, COALESCE_DELAY(2560) },
1896 { SPEED_10, COALESCE_DELAY(40960) },
1900 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1901 { SPEED_1000, COALESCE_DELAY(5000) },
1902 { SPEED_100, COALESCE_DELAY(2560) },
1903 { SPEED_10, COALESCE_DELAY(40960) },
1906 #undef COALESCE_DELAY
1908 /* get rx/tx scale vector corresponding to current speed */
1909 static const struct rtl_coalesce_info *
1910 rtl_coalesce_info(struct rtl8169_private *tp)
1912 const struct rtl_coalesce_info *ci;
1914 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1915 ci = rtl_coalesce_info_8169;
1917 ci = rtl_coalesce_info_8168_8136;
1919 /* if speed is unknown assume highest one */
1920 if (tp->phydev->speed == SPEED_UNKNOWN)
1923 for (; ci->speed; ci++) {
1924 if (tp->phydev->speed == ci->speed)
1928 return ERR_PTR(-ELNRNG);
1931 static int rtl_get_coalesce(struct net_device *dev,
1932 struct ethtool_coalesce *ec,
1933 struct kernel_ethtool_coalesce *kernel_coal,
1934 struct netlink_ext_ack *extack)
1936 struct rtl8169_private *tp = netdev_priv(dev);
1937 const struct rtl_coalesce_info *ci;
1938 u32 scale, c_us, c_fr;
1941 if (rtl_is_8125(tp))
1944 memset(ec, 0, sizeof(*ec));
1946 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1947 ci = rtl_coalesce_info(tp);
1951 scale = ci->scale_nsecs[tp->cp_cmd & INTT_MASK];
1953 intrmit = RTL_R16(tp, IntrMitigate);
1955 c_us = FIELD_GET(RTL_COALESCE_TX_USECS, intrmit);
1956 ec->tx_coalesce_usecs = DIV_ROUND_UP(c_us * scale, 1000);
1958 c_fr = FIELD_GET(RTL_COALESCE_TX_FRAMES, intrmit);
1959 /* ethtool_coalesce states usecs and max_frames must not both be 0 */
1960 ec->tx_max_coalesced_frames = (c_us || c_fr) ? c_fr * 4 : 1;
1962 c_us = FIELD_GET(RTL_COALESCE_RX_USECS, intrmit);
1963 ec->rx_coalesce_usecs = DIV_ROUND_UP(c_us * scale, 1000);
1965 c_fr = FIELD_GET(RTL_COALESCE_RX_FRAMES, intrmit);
1966 ec->rx_max_coalesced_frames = (c_us || c_fr) ? c_fr * 4 : 1;
1971 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, usec) */
1972 static int rtl_coalesce_choose_scale(struct rtl8169_private *tp, u32 usec,
1975 const struct rtl_coalesce_info *ci;
1978 ci = rtl_coalesce_info(tp);
1982 for (i = 0; i < 4; i++) {
1983 if (usec <= ci->scale_nsecs[i] * RTL_COALESCE_T_MAX / 1000U) {
1985 return ci->scale_nsecs[i];
1992 static int rtl_set_coalesce(struct net_device *dev,
1993 struct ethtool_coalesce *ec,
1994 struct kernel_ethtool_coalesce *kernel_coal,
1995 struct netlink_ext_ack *extack)
1997 struct rtl8169_private *tp = netdev_priv(dev);
1998 u32 tx_fr = ec->tx_max_coalesced_frames;
1999 u32 rx_fr = ec->rx_max_coalesced_frames;
2000 u32 coal_usec_max, units;
2001 u16 w = 0, cp01 = 0;
2004 if (rtl_is_8125(tp))
2007 if (rx_fr > RTL_COALESCE_FRAME_MAX || tx_fr > RTL_COALESCE_FRAME_MAX)
2010 coal_usec_max = max(ec->rx_coalesce_usecs, ec->tx_coalesce_usecs);
2011 scale = rtl_coalesce_choose_scale(tp, coal_usec_max, &cp01);
2015 /* Accept max_frames=1 we returned in rtl_get_coalesce. Accept it
2016 * not only when usecs=0 because of e.g. the following scenario:
2018 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2019 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2020 * - then user does `ethtool -C eth0 rx-usecs 100`
2022 * Since ethtool sends to kernel whole ethtool_coalesce settings,
2023 * if we want to ignore rx_frames then it has to be set to 0.
2030 /* HW requires time limit to be set if frame limit is set */
2031 if ((tx_fr && !ec->tx_coalesce_usecs) ||
2032 (rx_fr && !ec->rx_coalesce_usecs))
2035 w |= FIELD_PREP(RTL_COALESCE_TX_FRAMES, DIV_ROUND_UP(tx_fr, 4));
2036 w |= FIELD_PREP(RTL_COALESCE_RX_FRAMES, DIV_ROUND_UP(rx_fr, 4));
2038 units = DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000U, scale);
2039 w |= FIELD_PREP(RTL_COALESCE_TX_USECS, units);
2040 units = DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000U, scale);
2041 w |= FIELD_PREP(RTL_COALESCE_RX_USECS, units);
2043 RTL_W16(tp, IntrMitigate, w);
2045 /* Meaning of PktCntrDisable bit changed from RTL8168e-vl */
2046 if (rtl_is_8168evl_up(tp)) {
2047 if (!rx_fr && !tx_fr)
2048 /* disable packet counter */
2049 tp->cp_cmd |= PktCntrDisable;
2051 tp->cp_cmd &= ~PktCntrDisable;
2054 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
2055 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2061 static void rtl_set_eee_txidle_timer(struct rtl8169_private *tp)
2063 unsigned int timer_val = READ_ONCE(tp->dev->mtu) + ETH_HLEN + 0x20;
2065 switch (tp->mac_version) {
2066 case RTL_GIGA_MAC_VER_46:
2067 case RTL_GIGA_MAC_VER_48:
2068 tp->tx_lpi_timer = timer_val;
2069 r8168_mac_ocp_write(tp, 0xe048, timer_val);
2071 case RTL_GIGA_MAC_VER_61:
2072 case RTL_GIGA_MAC_VER_63:
2073 case RTL_GIGA_MAC_VER_65:
2074 tp->tx_lpi_timer = timer_val;
2075 RTL_W16(tp, EEE_TXIDLE_TIMER_8125, timer_val);
2082 static unsigned int r8169_get_tx_lpi_timer_us(struct rtl8169_private *tp)
2084 unsigned int speed = tp->phydev->speed;
2085 unsigned int timer = tp->tx_lpi_timer;
2087 if (!timer || speed == SPEED_UNKNOWN)
2090 /* tx_lpi_timer value is in bytes */
2091 return DIV_ROUND_CLOSEST(timer * BITS_PER_BYTE, speed);
2094 static int rtl8169_get_eee(struct net_device *dev, struct ethtool_keee *data)
2096 struct rtl8169_private *tp = netdev_priv(dev);
2099 if (!rtl_supports_eee(tp))
2102 ret = phy_ethtool_get_eee(tp->phydev, data);
2106 data->tx_lpi_timer = r8169_get_tx_lpi_timer_us(tp);
2111 static int rtl8169_set_eee(struct net_device *dev, struct ethtool_keee *data)
2113 struct rtl8169_private *tp = netdev_priv(dev);
2115 if (!rtl_supports_eee(tp))
2118 return phy_ethtool_set_eee(tp->phydev, data);
2121 static void rtl8169_get_ringparam(struct net_device *dev,
2122 struct ethtool_ringparam *data,
2123 struct kernel_ethtool_ringparam *kernel_data,
2124 struct netlink_ext_ack *extack)
2126 data->rx_max_pending = NUM_RX_DESC;
2127 data->rx_pending = NUM_RX_DESC;
2128 data->tx_max_pending = NUM_TX_DESC;
2129 data->tx_pending = NUM_TX_DESC;
2132 static void rtl8169_get_pauseparam(struct net_device *dev,
2133 struct ethtool_pauseparam *data)
2135 struct rtl8169_private *tp = netdev_priv(dev);
2136 bool tx_pause, rx_pause;
2138 phy_get_pause(tp->phydev, &tx_pause, &rx_pause);
2140 data->autoneg = tp->phydev->autoneg;
2141 data->tx_pause = tx_pause ? 1 : 0;
2142 data->rx_pause = rx_pause ? 1 : 0;
2145 static int rtl8169_set_pauseparam(struct net_device *dev,
2146 struct ethtool_pauseparam *data)
2148 struct rtl8169_private *tp = netdev_priv(dev);
2150 if (dev->mtu > ETH_DATA_LEN)
2153 phy_set_asym_pause(tp->phydev, data->rx_pause, data->tx_pause);
2158 static const struct ethtool_ops rtl8169_ethtool_ops = {
2159 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2160 ETHTOOL_COALESCE_MAX_FRAMES,
2161 .get_drvinfo = rtl8169_get_drvinfo,
2162 .get_regs_len = rtl8169_get_regs_len,
2163 .get_link = ethtool_op_get_link,
2164 .get_coalesce = rtl_get_coalesce,
2165 .set_coalesce = rtl_set_coalesce,
2166 .get_regs = rtl8169_get_regs,
2167 .get_wol = rtl8169_get_wol,
2168 .set_wol = rtl8169_set_wol,
2169 .get_strings = rtl8169_get_strings,
2170 .get_sset_count = rtl8169_get_sset_count,
2171 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2172 .get_ts_info = ethtool_op_get_ts_info,
2173 .nway_reset = phy_ethtool_nway_reset,
2174 .get_eee = rtl8169_get_eee,
2175 .set_eee = rtl8169_set_eee,
2176 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2177 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2178 .get_ringparam = rtl8169_get_ringparam,
2179 .get_pauseparam = rtl8169_get_pauseparam,
2180 .set_pauseparam = rtl8169_set_pauseparam,
2183 static enum mac_version rtl8169_get_mac_version(u16 xid, bool gmii)
2186 * The driver currently handles the 8168Bf and the 8168Be identically
2187 * but they can be identified more specifically through the test below
2190 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2192 * Same thing for the 8101Eb and the 8101Ec:
2194 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2196 static const struct rtl_mac_info {
2199 enum mac_version ver;
2202 { 0x7cf, 0x649, RTL_GIGA_MAC_VER_65 },
2205 { 0x7cf, 0x641, RTL_GIGA_MAC_VER_63 },
2208 { 0x7cf, 0x609, RTL_GIGA_MAC_VER_61 },
2209 /* It seems only XID 609 made it to the mass market.
2210 * { 0x7cf, 0x608, RTL_GIGA_MAC_VER_60 },
2211 * { 0x7c8, 0x608, RTL_GIGA_MAC_VER_61 },
2215 { 0x7cf, 0x54b, RTL_GIGA_MAC_VER_53 },
2216 { 0x7cf, 0x54a, RTL_GIGA_MAC_VER_52 },
2218 /* 8168EP family. */
2219 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2220 /* It seems this chip version never made it to
2221 * the wild. Let's disable detection.
2222 * { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2223 * { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
2227 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2228 /* It seems this chip version never made it to
2229 * the wild. Let's disable detection.
2230 * { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
2232 /* Realtek calls it RTL8168M, but it's handled like RTL8168H */
2233 { 0x7cf, 0x6c0, RTL_GIGA_MAC_VER_46 },
2236 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2237 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2238 /* It seems this chip version never made it to
2239 * the wild. Let's disable detection.
2240 * { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2242 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
2245 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2246 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2247 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
2250 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2251 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2252 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
2255 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2256 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
2258 /* 8168DP family. */
2259 /* It seems this early RTL8168dp version never made it to
2260 * the wild. Support has been removed.
2261 * { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2263 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2264 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
2267 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2268 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2269 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2270 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2271 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2272 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2273 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
2276 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2277 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
2280 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2281 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2282 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2283 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2284 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2285 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2286 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2287 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2288 { 0x7cf, 0x240, RTL_GIGA_MAC_VER_14 },
2289 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2290 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2291 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_10 },
2294 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2295 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2296 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2297 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2298 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
2301 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
2303 const struct rtl_mac_info *p = mac_info;
2304 enum mac_version ver;
2306 while ((xid & p->mask) != p->val)
2310 if (ver != RTL_GIGA_MAC_NONE && !gmii) {
2311 if (ver == RTL_GIGA_MAC_VER_42)
2312 ver = RTL_GIGA_MAC_VER_43;
2313 else if (ver == RTL_GIGA_MAC_VER_46)
2314 ver = RTL_GIGA_MAC_VER_48;
2320 static void rtl_release_firmware(struct rtl8169_private *tp)
2323 rtl_fw_release_firmware(tp->rtl_fw);
2329 void r8169_apply_firmware(struct rtl8169_private *tp)
2333 /* TODO: release firmware if rtl_fw_write_firmware signals failure. */
2335 rtl_fw_write_firmware(tp, tp->rtl_fw);
2336 /* At least one firmware doesn't reset tp->ocp_base. */
2337 tp->ocp_base = OCP_STD_PHY_BASE;
2339 /* PHY soft reset may still be in progress */
2340 phy_read_poll_timeout(tp->phydev, MII_BMCR, val,
2341 !(val & BMCR_RESET),
2342 50000, 600000, true);
2346 static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2348 /* Adjust EEE LED frequency */
2349 if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2350 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2352 rtl_eri_set_bits(tp, 0x1b0, 0x0003);
2355 static void rtl8125a_config_eee_mac(struct rtl8169_private *tp)
2357 r8168_mac_ocp_modify(tp, 0xe040, 0, BIT(1) | BIT(0));
2358 r8168_mac_ocp_modify(tp, 0xeb62, 0, BIT(2) | BIT(1));
2361 static void rtl8125b_config_eee_mac(struct rtl8169_private *tp)
2363 r8168_mac_ocp_modify(tp, 0xe040, 0, BIT(1) | BIT(0));
2366 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, const u8 *addr)
2368 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, get_unaligned_le32(addr));
2369 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, get_unaligned_le16(addr + 4));
2370 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, get_unaligned_le16(addr) << 16);
2371 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, get_unaligned_le32(addr + 2));
2374 u16 rtl8168h_2_get_adc_bias_ioffset(struct rtl8169_private *tp)
2376 u16 data1, data2, ioffset;
2378 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
2379 data1 = r8168_mac_ocp_read(tp, 0xdd02);
2380 data2 = r8168_mac_ocp_read(tp, 0xdd00);
2382 ioffset = (data2 >> 1) & 0x7ff8;
2383 ioffset |= data2 & 0x0007;
2390 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
2392 if (!test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
2395 set_bit(flag, tp->wk.flags);
2396 schedule_work(&tp->wk.work);
2399 static void rtl8169_init_phy(struct rtl8169_private *tp)
2401 r8169_hw_phy_config(tp, tp->phydev, tp->mac_version);
2403 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2404 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2405 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
2406 /* set undocumented MAC Reg C+CR Offset 0x82h */
2407 RTL_W8(tp, 0x82, 0x01);
2410 if (tp->mac_version == RTL_GIGA_MAC_VER_05 &&
2411 tp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_GIGABYTE &&
2412 tp->pci_dev->subsystem_device == 0xe000)
2413 phy_write_paged(tp->phydev, 0x0001, 0x10, 0xf01b);
2415 /* We may have called phy_speed_down before */
2416 phy_speed_up(tp->phydev);
2418 genphy_soft_reset(tp->phydev);
2421 static void rtl_rar_set(struct rtl8169_private *tp, const u8 *addr)
2423 rtl_unlock_config_regs(tp);
2425 RTL_W32(tp, MAC4, get_unaligned_le16(addr + 4));
2428 RTL_W32(tp, MAC0, get_unaligned_le32(addr));
2431 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
2432 rtl_rar_exgmac_set(tp, addr);
2434 rtl_lock_config_regs(tp);
2437 static int rtl_set_mac_address(struct net_device *dev, void *p)
2439 struct rtl8169_private *tp = netdev_priv(dev);
2442 ret = eth_mac_addr(dev, p);
2446 rtl_rar_set(tp, dev->dev_addr);
2451 static void rtl_init_rxcfg(struct rtl8169_private *tp)
2453 switch (tp->mac_version) {
2454 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
2455 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
2456 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
2458 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
2459 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
2460 case RTL_GIGA_MAC_VER_38:
2461 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
2463 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_53:
2464 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
2466 case RTL_GIGA_MAC_VER_61:
2467 RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_DMA_BURST);
2469 case RTL_GIGA_MAC_VER_63:
2470 case RTL_GIGA_MAC_VER_65:
2471 RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_DMA_BURST |
2475 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
2480 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
2482 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
2485 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
2487 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
2488 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
2491 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
2493 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
2494 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
2497 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
2499 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
2502 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
2504 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
2507 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
2509 RTL_W8(tp, MaxTxPacketSize, 0x24);
2510 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
2511 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
2514 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
2516 RTL_W8(tp, MaxTxPacketSize, 0x3f);
2517 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
2518 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
2521 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
2523 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
2526 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
2528 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
2531 static void rtl_jumbo_config(struct rtl8169_private *tp)
2533 bool jumbo = tp->dev->mtu > ETH_DATA_LEN;
2536 rtl_unlock_config_regs(tp);
2537 switch (tp->mac_version) {
2538 case RTL_GIGA_MAC_VER_17:
2541 r8168b_1_hw_jumbo_enable(tp);
2543 r8168b_1_hw_jumbo_disable(tp);
2546 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
2549 r8168c_hw_jumbo_enable(tp);
2551 r8168c_hw_jumbo_disable(tp);
2554 case RTL_GIGA_MAC_VER_28:
2556 r8168dp_hw_jumbo_enable(tp);
2558 r8168dp_hw_jumbo_disable(tp);
2560 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_33:
2562 r8168e_hw_jumbo_enable(tp);
2564 r8168e_hw_jumbo_disable(tp);
2569 rtl_lock_config_regs(tp);
2571 if (pci_is_pcie(tp->pci_dev) && tp->supports_gmii)
2572 pcie_set_readrq(tp->pci_dev, readrq);
2574 /* Chip doesn't support pause in jumbo mode */
2576 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2577 tp->phydev->advertising);
2578 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2579 tp->phydev->advertising);
2580 phy_start_aneg(tp->phydev);
2584 DECLARE_RTL_COND(rtl_chipcmd_cond)
2586 return RTL_R8(tp, ChipCmd) & CmdReset;
2589 static void rtl_hw_reset(struct rtl8169_private *tp)
2591 RTL_W8(tp, ChipCmd, CmdReset);
2593 rtl_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
2596 static void rtl_request_firmware(struct rtl8169_private *tp)
2598 struct rtl_fw *rtl_fw;
2600 /* firmware loaded already or no firmware available */
2601 if (tp->rtl_fw || !tp->fw_name)
2604 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
2608 rtl_fw->phy_write = rtl_writephy;
2609 rtl_fw->phy_read = rtl_readphy;
2610 rtl_fw->mac_mcu_write = mac_mcu_write;
2611 rtl_fw->mac_mcu_read = mac_mcu_read;
2612 rtl_fw->fw_name = tp->fw_name;
2613 rtl_fw->dev = tp_to_dev(tp);
2615 if (rtl_fw_request_firmware(rtl_fw))
2618 tp->rtl_fw = rtl_fw;
2621 static void rtl_rx_close(struct rtl8169_private *tp)
2623 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
2626 DECLARE_RTL_COND(rtl_npq_cond)
2628 return RTL_R8(tp, TxPoll) & NPQ;
2631 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
2633 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
2636 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
2638 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
2641 DECLARE_RTL_COND(rtl_rxtx_empty_cond_2)
2643 /* IntrMitigate has new functionality on RTL8125 */
2644 return (RTL_R16(tp, IntrMitigate) & 0x0103) == 0x0103;
2647 static void rtl_wait_txrx_fifo_empty(struct rtl8169_private *tp)
2649 switch (tp->mac_version) {
2650 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_53:
2651 rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42);
2652 rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
2654 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_61:
2655 rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
2657 case RTL_GIGA_MAC_VER_63 ... RTL_GIGA_MAC_VER_65:
2658 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
2659 rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
2660 rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond_2, 100, 42);
2667 static void rtl_disable_rxdvgate(struct rtl8169_private *tp)
2669 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
2672 static void rtl_enable_rxdvgate(struct rtl8169_private *tp)
2674 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
2676 rtl_wait_txrx_fifo_empty(tp);
2679 static void rtl_wol_enable_rx(struct rtl8169_private *tp)
2681 if (tp->mac_version >= RTL_GIGA_MAC_VER_25)
2682 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
2683 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
2685 if (tp->mac_version >= RTL_GIGA_MAC_VER_40)
2686 rtl_disable_rxdvgate(tp);
2689 static void rtl_prepare_power_down(struct rtl8169_private *tp)
2691 if (tp->dash_enabled)
2694 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
2695 tp->mac_version == RTL_GIGA_MAC_VER_33)
2696 rtl_ephy_write(tp, 0x19, 0xff64);
2698 if (device_may_wakeup(tp_to_dev(tp))) {
2699 phy_speed_down(tp->phydev, false);
2700 rtl_wol_enable_rx(tp);
2704 static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
2706 u32 val = TX_DMA_BURST << TxDMAShift |
2707 InterFrameGap << TxInterFrameGapShift;
2709 if (rtl_is_8168evl_up(tp))
2710 val |= TXCFG_AUTO_FIFO;
2712 RTL_W32(tp, TxConfig, val);
2715 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
2717 /* Low hurts. Let's disable the filtering. */
2718 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
2721 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
2724 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
2725 * register to be written before TxDescAddrLow to work.
2726 * Switching from MMIO to I/O access fixes the issue as well.
2728 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
2729 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
2730 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
2731 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
2734 static void rtl8169_set_magic_reg(struct rtl8169_private *tp)
2738 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
2740 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
2745 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
2748 RTL_W32(tp, 0x7c, val);
2751 static void rtl_set_rx_mode(struct net_device *dev)
2753 u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
2754 /* Multicast hash filter */
2755 u32 mc_filter[2] = { 0xffffffff, 0xffffffff };
2756 struct rtl8169_private *tp = netdev_priv(dev);
2759 if (dev->flags & IFF_PROMISC) {
2760 rx_mode |= AcceptAllPhys;
2761 } else if (!(dev->flags & IFF_MULTICAST)) {
2762 rx_mode &= ~AcceptMulticast;
2763 } else if (dev->flags & IFF_ALLMULTI ||
2764 tp->mac_version == RTL_GIGA_MAC_VER_35) {
2765 /* accept all multicasts */
2766 } else if (netdev_mc_empty(dev)) {
2767 rx_mode &= ~AcceptMulticast;
2769 struct netdev_hw_addr *ha;
2771 mc_filter[1] = mc_filter[0] = 0;
2772 netdev_for_each_mc_addr(ha, dev) {
2773 u32 bit_nr = eth_hw_addr_crc(ha) >> 26;
2774 mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31);
2777 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
2779 mc_filter[0] = swab32(mc_filter[1]);
2780 mc_filter[1] = swab32(tmp);
2784 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
2785 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
2787 tmp = RTL_R32(tp, RxConfig);
2788 RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_OK_MASK) | rx_mode);
2791 DECLARE_RTL_COND(rtl_csiar_cond)
2793 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
2796 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
2798 u32 func = PCI_FUNC(tp->pci_dev->devfn);
2800 RTL_W32(tp, CSIDR, value);
2801 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
2802 CSIAR_BYTE_ENABLE | func << 16);
2804 rtl_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
2807 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
2809 u32 func = PCI_FUNC(tp->pci_dev->devfn);
2811 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
2814 return rtl_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
2815 RTL_R32(tp, CSIDR) : ~0;
2818 static void rtl_set_aspm_entry_latency(struct rtl8169_private *tp, u8 val)
2820 struct pci_dev *pdev = tp->pci_dev;
2823 /* According to Realtek the value at config space address 0x070f
2824 * controls the L0s/L1 entrance latency. We try standard ECAM access
2825 * first and if it fails fall back to CSI.
2826 * bit 0..2: L0: 0 = 1us, 1 = 2us .. 6 = 7us, 7 = 7us (no typo)
2827 * bit 3..5: L1: 0 = 1us, 1 = 2us .. 6 = 64us, 7 = 64us
2829 if (pdev->cfg_size > 0x070f &&
2830 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
2833 netdev_notice_once(tp->dev,
2834 "No native access to PCI extended config space, falling back to CSI\n");
2835 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
2836 rtl_csi_write(tp, 0x070c, csi | val << 24);
2839 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
2841 /* L0 7us, L1 16us */
2842 rtl_set_aspm_entry_latency(tp, 0x27);
2846 unsigned int offset;
2851 static void __rtl_ephy_init(struct rtl8169_private *tp,
2852 const struct ephy_info *e, int len)
2857 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
2858 rtl_ephy_write(tp, e->offset, w);
2863 #define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
2865 static void rtl_disable_clock_request(struct rtl8169_private *tp)
2867 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
2868 PCI_EXP_LNKCTL_CLKREQ_EN);
2871 static void rtl_enable_clock_request(struct rtl8169_private *tp)
2873 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
2874 PCI_EXP_LNKCTL_CLKREQ_EN);
2877 static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
2879 /* work around an issue when PCI reset occurs during L2/L3 state */
2880 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
2883 static void rtl_enable_exit_l1(struct rtl8169_private *tp)
2885 /* Bits control which events trigger ASPM L1 exit:
2888 * Bit 10: txdma_poll
2893 switch (tp->mac_version) {
2894 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
2895 rtl_eri_set_bits(tp, 0xd4, 0x1f00);
2897 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_38:
2898 rtl_eri_set_bits(tp, 0xd4, 0x0c00);
2900 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_65:
2901 r8168_mac_ocp_modify(tp, 0xc0ac, 0, 0x1f80);
2908 static void rtl_disable_exit_l1(struct rtl8169_private *tp)
2910 switch (tp->mac_version) {
2911 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
2912 rtl_eri_clear_bits(tp, 0xd4, 0x1f00);
2914 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_65:
2915 r8168_mac_ocp_modify(tp, 0xc0ac, 0x1f80, 0);
2922 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
2926 if (tp->mac_version < RTL_GIGA_MAC_VER_32)
2929 /* Don't enable ASPM in the chip if OS can't control ASPM */
2930 if (enable && tp->aspm_manageable) {
2931 /* On these chip versions ASPM can even harm
2932 * bus communication of other PCI devices.
2934 if (tp->mac_version == RTL_GIGA_MAC_VER_42 ||
2935 tp->mac_version == RTL_GIGA_MAC_VER_43)
2938 rtl_mod_config5(tp, 0, ASPM_en);
2939 switch (tp->mac_version) {
2940 case RTL_GIGA_MAC_VER_65:
2941 val8 = RTL_R8(tp, INT_CFG0_8125) | INT_CFG0_CLKREQEN;
2942 RTL_W8(tp, INT_CFG0_8125, val8);
2945 rtl_mod_config2(tp, 0, ClkReqEn);
2949 switch (tp->mac_version) {
2950 case RTL_GIGA_MAC_VER_46 ... RTL_GIGA_MAC_VER_48:
2951 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_65:
2952 /* reset ephy tx/rx disable timer */
2953 r8168_mac_ocp_modify(tp, 0xe094, 0xff00, 0);
2954 /* chip can trigger L1.2 */
2955 r8168_mac_ocp_modify(tp, 0xe092, 0x00ff, BIT(2));
2961 switch (tp->mac_version) {
2962 case RTL_GIGA_MAC_VER_46 ... RTL_GIGA_MAC_VER_48:
2963 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_65:
2964 r8168_mac_ocp_modify(tp, 0xe092, 0x00ff, 0);
2970 switch (tp->mac_version) {
2971 case RTL_GIGA_MAC_VER_65:
2972 val8 = RTL_R8(tp, INT_CFG0_8125) & ~INT_CFG0_CLKREQEN;
2973 RTL_W8(tp, INT_CFG0_8125, val8);
2976 rtl_mod_config2(tp, ClkReqEn, 0);
2979 rtl_mod_config5(tp, ASPM_en, 0);
2983 static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
2984 u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
2986 /* Usage of dynamic vs. static FIFO is controlled by bit
2987 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
2989 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
2990 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
2993 static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
2996 /* FIFO thresholds for pause flow control */
2997 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
2998 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
3001 static void rtl_hw_start_8168b(struct rtl8169_private *tp)
3003 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3006 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
3008 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
3010 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3012 rtl_disable_clock_request(tp);
3015 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
3017 static const struct ephy_info e_info_8168cp[] = {
3018 { 0x01, 0, 0x0001 },
3019 { 0x02, 0x0800, 0x1000 },
3020 { 0x03, 0, 0x0042 },
3021 { 0x06, 0x0080, 0x0000 },
3025 rtl_set_def_aspm_entry_latency(tp);
3027 rtl_ephy_init(tp, e_info_8168cp);
3029 __rtl_hw_start_8168cp(tp);
3032 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
3034 rtl_set_def_aspm_entry_latency(tp);
3036 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3039 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
3041 rtl_set_def_aspm_entry_latency(tp);
3043 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3046 RTL_W8(tp, DBG_REG, 0x20);
3049 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
3051 static const struct ephy_info e_info_8168c_1[] = {
3052 { 0x02, 0x0800, 0x1000 },
3053 { 0x03, 0, 0x0002 },
3054 { 0x06, 0x0080, 0x0000 }
3057 rtl_set_def_aspm_entry_latency(tp);
3059 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3061 rtl_ephy_init(tp, e_info_8168c_1);
3063 __rtl_hw_start_8168cp(tp);
3066 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
3068 static const struct ephy_info e_info_8168c_2[] = {
3069 { 0x01, 0, 0x0001 },
3070 { 0x03, 0x0400, 0x0020 }
3073 rtl_set_def_aspm_entry_latency(tp);
3075 rtl_ephy_init(tp, e_info_8168c_2);
3077 __rtl_hw_start_8168cp(tp);
3080 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
3082 rtl_set_def_aspm_entry_latency(tp);
3084 __rtl_hw_start_8168cp(tp);
3087 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
3089 rtl_set_def_aspm_entry_latency(tp);
3091 rtl_disable_clock_request(tp);
3094 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
3096 static const struct ephy_info e_info_8168d_4[] = {
3097 { 0x0b, 0x0000, 0x0048 },
3098 { 0x19, 0x0020, 0x0050 },
3099 { 0x0c, 0x0100, 0x0020 },
3100 { 0x10, 0x0004, 0x0000 },
3103 rtl_set_def_aspm_entry_latency(tp);
3105 rtl_ephy_init(tp, e_info_8168d_4);
3107 rtl_enable_clock_request(tp);
3110 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
3112 static const struct ephy_info e_info_8168e_1[] = {
3113 { 0x00, 0x0200, 0x0100 },
3114 { 0x00, 0x0000, 0x0004 },
3115 { 0x06, 0x0002, 0x0001 },
3116 { 0x06, 0x0000, 0x0030 },
3117 { 0x07, 0x0000, 0x2000 },
3118 { 0x00, 0x0000, 0x0020 },
3119 { 0x03, 0x5800, 0x2000 },
3120 { 0x03, 0x0000, 0x0001 },
3121 { 0x01, 0x0800, 0x1000 },
3122 { 0x07, 0x0000, 0x4000 },
3123 { 0x1e, 0x0000, 0x2000 },
3124 { 0x19, 0xffff, 0xfe6c },
3125 { 0x0a, 0x0000, 0x0040 }
3128 rtl_set_def_aspm_entry_latency(tp);
3130 rtl_ephy_init(tp, e_info_8168e_1);
3132 rtl_disable_clock_request(tp);
3134 /* Reset tx FIFO pointer */
3135 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
3136 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
3138 rtl_mod_config5(tp, Spi_en, 0);
3141 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
3143 static const struct ephy_info e_info_8168e_2[] = {
3144 { 0x09, 0x0000, 0x0080 },
3145 { 0x19, 0x0000, 0x0224 },
3146 { 0x00, 0x0000, 0x0004 },
3147 { 0x0c, 0x3df0, 0x0200 },
3150 rtl_set_def_aspm_entry_latency(tp);
3152 rtl_ephy_init(tp, e_info_8168e_2);
3154 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3155 rtl_eri_write(tp, 0xb8, ERIAR_MASK_1111, 0x0000);
3156 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
3157 rtl_eri_set_bits(tp, 0x1d0, BIT(1));
3158 rtl_reset_packet_filter(tp);
3159 rtl_eri_set_bits(tp, 0x1b0, BIT(4));
3160 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
3161 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
3163 rtl_disable_clock_request(tp);
3165 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
3167 rtl8168_config_eee_mac(tp);
3169 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
3170 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
3171 rtl_mod_config5(tp, Spi_en, 0);
3174 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
3176 rtl_set_def_aspm_entry_latency(tp);
3178 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3179 rtl_eri_write(tp, 0xb8, ERIAR_MASK_1111, 0x0000);
3180 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
3181 rtl_reset_packet_filter(tp);
3182 rtl_eri_set_bits(tp, 0x1b0, BIT(4));
3183 rtl_eri_set_bits(tp, 0x1d0, BIT(4) | BIT(1));
3184 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
3185 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
3187 rtl_disable_clock_request(tp);
3189 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
3190 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
3191 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
3192 rtl_mod_config5(tp, Spi_en, 0);
3194 rtl8168_config_eee_mac(tp);
3197 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
3199 static const struct ephy_info e_info_8168f_1[] = {
3200 { 0x06, 0x00c0, 0x0020 },
3201 { 0x08, 0x0001, 0x0002 },
3202 { 0x09, 0x0000, 0x0080 },
3203 { 0x19, 0x0000, 0x0224 },
3204 { 0x00, 0x0000, 0x0008 },
3205 { 0x0c, 0x3df0, 0x0200 },
3208 rtl_hw_start_8168f(tp);
3210 rtl_ephy_init(tp, e_info_8168f_1);
3213 static void rtl_hw_start_8411(struct rtl8169_private *tp)
3215 static const struct ephy_info e_info_8168f_1[] = {
3216 { 0x06, 0x00c0, 0x0020 },
3217 { 0x0f, 0xffff, 0x5200 },
3218 { 0x19, 0x0000, 0x0224 },
3219 { 0x00, 0x0000, 0x0008 },
3220 { 0x0c, 0x3df0, 0x0200 },
3223 rtl_hw_start_8168f(tp);
3224 rtl_pcie_state_l2l3_disable(tp);
3226 rtl_ephy_init(tp, e_info_8168f_1);
3229 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
3231 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
3232 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
3234 rtl_set_def_aspm_entry_latency(tp);
3236 rtl_reset_packet_filter(tp);
3237 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
3239 rtl_disable_rxdvgate(tp);
3241 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3242 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3244 rtl8168_config_eee_mac(tp);
3246 rtl_w0w1_eri(tp, 0x2fc, 0x01, 0x06);
3247 rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
3249 rtl_pcie_state_l2l3_disable(tp);
3252 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
3254 static const struct ephy_info e_info_8168g_1[] = {
3255 { 0x00, 0x0008, 0x0000 },
3256 { 0x0c, 0x3ff0, 0x0820 },
3257 { 0x1e, 0x0000, 0x0001 },
3258 { 0x19, 0x8000, 0x0000 }
3261 rtl_hw_start_8168g(tp);
3262 rtl_ephy_init(tp, e_info_8168g_1);
3265 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
3267 static const struct ephy_info e_info_8168g_2[] = {
3268 { 0x00, 0x0008, 0x0000 },
3269 { 0x0c, 0x3ff0, 0x0820 },
3270 { 0x19, 0xffff, 0x7c00 },
3271 { 0x1e, 0xffff, 0x20eb },
3272 { 0x0d, 0xffff, 0x1666 },
3273 { 0x00, 0xffff, 0x10a3 },
3274 { 0x06, 0xffff, 0xf050 },
3275 { 0x04, 0x0000, 0x0010 },
3276 { 0x1d, 0x4000, 0x0000 },
3279 rtl_hw_start_8168g(tp);
3280 rtl_ephy_init(tp, e_info_8168g_2);
3283 static void rtl8411b_fix_phy_down(struct rtl8169_private *tp)
3285 static const u16 fix_data[] = {
3286 /* 0xf800 */ 0xe008, 0xe00a, 0xe00c, 0xe00e, 0xe027, 0xe04f, 0xe05e, 0xe065,
3287 /* 0xf810 */ 0xc602, 0xbe00, 0x0000, 0xc502, 0xbd00, 0x074c, 0xc302, 0xbb00,
3288 /* 0xf820 */ 0x080a, 0x6420, 0x48c2, 0x8c20, 0xc516, 0x64a4, 0x49c0, 0xf009,
3289 /* 0xf830 */ 0x74a2, 0x8ca5, 0x74a0, 0xc50e, 0x9ca2, 0x1c11, 0x9ca0, 0xe006,
3290 /* 0xf840 */ 0x74f8, 0x48c4, 0x8cf8, 0xc404, 0xbc00, 0xc403, 0xbc00, 0x0bf2,
3291 /* 0xf850 */ 0x0c0a, 0xe434, 0xd3c0, 0x49d9, 0xf01f, 0xc526, 0x64a5, 0x1400,
3292 /* 0xf860 */ 0xf007, 0x0c01, 0x8ca5, 0x1c15, 0xc51b, 0x9ca0, 0xe013, 0xc519,
3293 /* 0xf870 */ 0x74a0, 0x48c4, 0x8ca0, 0xc516, 0x74a4, 0x48c8, 0x48ca, 0x9ca4,
3294 /* 0xf880 */ 0xc512, 0x1b00, 0x9ba0, 0x1b1c, 0x483f, 0x9ba2, 0x1b04, 0xc508,
3295 /* 0xf890 */ 0x9ba0, 0xc505, 0xbd00, 0xc502, 0xbd00, 0x0300, 0x051e, 0xe434,
3296 /* 0xf8a0 */ 0xe018, 0xe092, 0xde20, 0xd3c0, 0xc50f, 0x76a4, 0x49e3, 0xf007,
3297 /* 0xf8b0 */ 0x49c0, 0xf103, 0xc607, 0xbe00, 0xc606, 0xbe00, 0xc602, 0xbe00,
3298 /* 0xf8c0 */ 0x0c4c, 0x0c28, 0x0c2c, 0xdc00, 0xc707, 0x1d00, 0x8de2, 0x48c1,
3299 /* 0xf8d0 */ 0xc502, 0xbd00, 0x00aa, 0xe0c0, 0xc502, 0xbd00, 0x0132
3301 unsigned long flags;
3304 raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags);
3305 for (i = 0; i < ARRAY_SIZE(fix_data); i++)
3306 __r8168_mac_ocp_write(tp, 0xf800 + 2 * i, fix_data[i]);
3307 raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags);
3310 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
3312 static const struct ephy_info e_info_8411_2[] = {
3313 { 0x00, 0x0008, 0x0000 },
3314 { 0x0c, 0x37d0, 0x0820 },
3315 { 0x1e, 0x0000, 0x0001 },
3316 { 0x19, 0x8021, 0x0000 },
3317 { 0x1e, 0x0000, 0x2000 },
3318 { 0x0d, 0x0100, 0x0200 },
3319 { 0x00, 0x0000, 0x0080 },
3320 { 0x06, 0x0000, 0x0010 },
3321 { 0x04, 0x0000, 0x0010 },
3322 { 0x1d, 0x0000, 0x4000 },
3325 rtl_hw_start_8168g(tp);
3327 rtl_ephy_init(tp, e_info_8411_2);
3329 /* The following Realtek-provided magic fixes an issue with the RX unit
3330 * getting confused after the PHY having been powered-down.
3332 r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
3333 r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
3334 r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
3335 r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
3336 r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
3337 r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
3338 r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
3339 r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
3341 r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
3343 rtl8411b_fix_phy_down(tp);
3345 r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
3347 r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
3348 r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
3349 r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
3350 r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
3351 r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
3352 r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
3353 r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
3356 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
3358 static const struct ephy_info e_info_8168h_1[] = {
3359 { 0x1e, 0x0800, 0x0001 },
3360 { 0x1d, 0x0000, 0x0800 },
3361 { 0x05, 0xffff, 0x2089 },
3362 { 0x06, 0xffff, 0x5881 },
3363 { 0x04, 0xffff, 0x854a },
3364 { 0x01, 0xffff, 0x068b }
3368 rtl_ephy_init(tp, e_info_8168h_1);
3370 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
3371 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
3373 rtl_set_def_aspm_entry_latency(tp);
3375 rtl_reset_packet_filter(tp);
3377 rtl_eri_set_bits(tp, 0xdc, 0x001c);
3379 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
3381 rtl_disable_rxdvgate(tp);
3383 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3384 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3386 rtl8168_config_eee_mac(tp);
3388 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
3389 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
3391 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
3393 rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
3395 rtl_pcie_state_l2l3_disable(tp);
3397 rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
3398 if (rg_saw_cnt > 0) {
3401 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
3402 sw_cnt_1ms_ini &= 0x0fff;
3403 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
3406 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
3407 r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008);
3408 r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f);
3409 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
3411 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
3412 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
3413 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
3414 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
3417 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
3419 rtl8168ep_stop_cmac(tp);
3421 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
3422 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
3424 rtl_set_def_aspm_entry_latency(tp);
3426 rtl_reset_packet_filter(tp);
3428 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
3430 rtl_disable_rxdvgate(tp);
3432 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3433 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3435 rtl8168_config_eee_mac(tp);
3437 rtl_w0w1_eri(tp, 0x2fc, 0x01, 0x06);
3439 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
3441 rtl_pcie_state_l2l3_disable(tp);
3444 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
3446 static const struct ephy_info e_info_8168ep_3[] = {
3447 { 0x00, 0x0000, 0x0080 },
3448 { 0x0d, 0x0100, 0x0200 },
3449 { 0x19, 0x8021, 0x0000 },
3450 { 0x1e, 0x0000, 0x2000 },
3453 rtl_ephy_init(tp, e_info_8168ep_3);
3455 rtl_hw_start_8168ep(tp);
3457 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
3458 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
3460 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271);
3461 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
3462 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
3465 static void rtl_hw_start_8117(struct rtl8169_private *tp)
3467 static const struct ephy_info e_info_8117[] = {
3468 { 0x19, 0x0040, 0x1100 },
3469 { 0x59, 0x0040, 0x1100 },
3473 rtl8168ep_stop_cmac(tp);
3474 rtl_ephy_init(tp, e_info_8117);
3476 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
3477 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
3479 rtl_set_def_aspm_entry_latency(tp);
3481 rtl_reset_packet_filter(tp);
3483 rtl_eri_set_bits(tp, 0xd4, 0x0010);
3485 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
3487 rtl_disable_rxdvgate(tp);
3489 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3490 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3492 rtl8168_config_eee_mac(tp);
3494 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
3495 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
3497 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
3499 rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
3501 rtl_pcie_state_l2l3_disable(tp);
3503 rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
3504 if (rg_saw_cnt > 0) {
3507 sw_cnt_1ms_ini = (16000000 / rg_saw_cnt) & 0x0fff;
3508 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
3511 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
3512 r8168_mac_ocp_write(tp, 0xea80, 0x0003);
3513 r8168_mac_ocp_modify(tp, 0xe052, 0x0000, 0x0009);
3514 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
3516 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
3517 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
3518 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
3519 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
3521 /* firmware is for MAC only */
3522 r8169_apply_firmware(tp);
3525 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
3527 static const struct ephy_info e_info_8102e_1[] = {
3528 { 0x01, 0, 0x6e65 },
3529 { 0x02, 0, 0x091f },
3530 { 0x03, 0, 0xc2f9 },
3531 { 0x06, 0, 0xafb5 },
3532 { 0x07, 0, 0x0e00 },
3533 { 0x19, 0, 0xec80 },
3534 { 0x01, 0, 0x2e65 },
3539 rtl_set_def_aspm_entry_latency(tp);
3541 RTL_W8(tp, DBG_REG, FIX_NAK_1);
3544 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
3545 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3547 cfg1 = RTL_R8(tp, Config1);
3548 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
3549 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
3551 rtl_ephy_init(tp, e_info_8102e_1);
3554 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
3556 rtl_set_def_aspm_entry_latency(tp);
3558 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
3559 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3562 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
3564 rtl_hw_start_8102e_2(tp);
3566 rtl_ephy_write(tp, 0x03, 0xc2f9);
3569 static void rtl_hw_start_8401(struct rtl8169_private *tp)
3571 static const struct ephy_info e_info_8401[] = {
3572 { 0x01, 0xffff, 0x6fe5 },
3573 { 0x03, 0xffff, 0x0599 },
3574 { 0x06, 0xffff, 0xaf25 },
3575 { 0x07, 0xffff, 0x8e68 },
3578 rtl_ephy_init(tp, e_info_8401);
3579 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
3582 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
3584 static const struct ephy_info e_info_8105e_1[] = {
3585 { 0x07, 0, 0x4000 },
3586 { 0x19, 0, 0x0200 },
3587 { 0x19, 0, 0x0020 },
3588 { 0x1e, 0, 0x2000 },
3589 { 0x03, 0, 0x0001 },
3590 { 0x19, 0, 0x0100 },
3591 { 0x19, 0, 0x0004 },
3595 /* Force LAN exit from ASPM if Rx/Tx are not idle */
3596 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
3598 /* Disable Early Tally Counter */
3599 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
3601 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
3602 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
3604 rtl_ephy_init(tp, e_info_8105e_1);
3606 rtl_pcie_state_l2l3_disable(tp);
3609 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
3611 rtl_hw_start_8105e_1(tp);
3612 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
3615 static void rtl_hw_start_8402(struct rtl8169_private *tp)
3617 static const struct ephy_info e_info_8402[] = {
3618 { 0x19, 0xffff, 0xff64 },
3622 rtl_set_def_aspm_entry_latency(tp);
3624 /* Force LAN exit from ASPM if Rx/Tx are not idle */
3625 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
3627 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
3629 rtl_ephy_init(tp, e_info_8402);
3631 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
3632 rtl_reset_packet_filter(tp);
3633 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
3634 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
3635 rtl_w0w1_eri(tp, 0x0d4, 0x0e00, 0xff00);
3638 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
3640 rtl_pcie_state_l2l3_disable(tp);
3643 static void rtl_hw_start_8106(struct rtl8169_private *tp)
3645 /* Force LAN exit from ASPM if Rx/Tx are not idle */
3646 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
3648 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
3649 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
3650 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
3652 /* L0 7us, L1 32us - needed to avoid issues with link-up detection */
3653 rtl_set_aspm_entry_latency(tp, 0x2f);
3655 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
3658 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
3660 rtl_pcie_state_l2l3_disable(tp);
3663 DECLARE_RTL_COND(rtl_mac_ocp_e00e_cond)
3665 return r8168_mac_ocp_read(tp, 0xe00e) & BIT(13);
3668 static void rtl_hw_start_8125_common(struct rtl8169_private *tp)
3670 rtl_pcie_state_l2l3_disable(tp);
3672 RTL_W16(tp, 0x382, 0x221b);
3673 RTL_W8(tp, 0x4500, 0);
3674 RTL_W16(tp, 0x4800, 0);
3677 r8168_mac_ocp_modify(tp, 0xd40a, 0x0010, 0x0000);
3679 RTL_W8(tp, Config1, RTL_R8(tp, Config1) & ~0x10);
3681 r8168_mac_ocp_write(tp, 0xc140, 0xffff);
3682 r8168_mac_ocp_write(tp, 0xc142, 0xffff);
3684 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x03a9);
3685 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
3686 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
3688 /* disable new tx descriptor format */
3689 r8168_mac_ocp_modify(tp, 0xeb58, 0x0001, 0x0000);
3691 if (tp->mac_version == RTL_GIGA_MAC_VER_65)
3692 RTL_W8(tp, 0xD8, RTL_R8(tp, 0xD8) & ~0x02);
3694 if (tp->mac_version == RTL_GIGA_MAC_VER_65)
3695 r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0400);
3696 else if (tp->mac_version == RTL_GIGA_MAC_VER_63)
3697 r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0200);
3699 r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0300);
3701 if (tp->mac_version == RTL_GIGA_MAC_VER_63)
3702 r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0000);
3704 r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0020);
3706 r8168_mac_ocp_modify(tp, 0xc0b4, 0x0000, 0x000c);
3707 r8168_mac_ocp_modify(tp, 0xeb6a, 0x00ff, 0x0033);
3708 r8168_mac_ocp_modify(tp, 0xeb50, 0x03e0, 0x0040);
3709 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0030);
3710 r8168_mac_ocp_modify(tp, 0xe040, 0x1000, 0x0000);
3711 r8168_mac_ocp_modify(tp, 0xea1c, 0x0003, 0x0001);
3712 if (tp->mac_version == RTL_GIGA_MAC_VER_65)
3713 r8168_mac_ocp_modify(tp, 0xea1c, 0x0300, 0x0000);
3715 r8168_mac_ocp_modify(tp, 0xea1c, 0x0004, 0x0000);
3716 r8168_mac_ocp_modify(tp, 0xe0c0, 0x4f0f, 0x4403);
3717 r8168_mac_ocp_modify(tp, 0xe052, 0x0080, 0x0068);
3718 r8168_mac_ocp_modify(tp, 0xd430, 0x0fff, 0x047f);
3720 r8168_mac_ocp_modify(tp, 0xea1c, 0x0004, 0x0000);
3721 r8168_mac_ocp_modify(tp, 0xeb54, 0x0000, 0x0001);
3723 r8168_mac_ocp_modify(tp, 0xeb54, 0x0001, 0x0000);
3724 RTL_W16(tp, 0x1880, RTL_R16(tp, 0x1880) & ~0x0030);
3726 r8168_mac_ocp_write(tp, 0xe098, 0xc302);
3728 rtl_loop_wait_low(tp, &rtl_mac_ocp_e00e_cond, 1000, 10);
3730 if (tp->mac_version == RTL_GIGA_MAC_VER_61)
3731 rtl8125a_config_eee_mac(tp);
3733 rtl8125b_config_eee_mac(tp);
3735 rtl_disable_rxdvgate(tp);
3738 static void rtl_hw_start_8125a_2(struct rtl8169_private *tp)
3740 static const struct ephy_info e_info_8125a_2[] = {
3741 { 0x04, 0xffff, 0xd000 },
3742 { 0x0a, 0xffff, 0x8653 },
3743 { 0x23, 0xffff, 0xab66 },
3744 { 0x20, 0xffff, 0x9455 },
3745 { 0x21, 0xffff, 0x99ff },
3746 { 0x29, 0xffff, 0xfe04 },
3748 { 0x44, 0xffff, 0xd000 },
3749 { 0x4a, 0xffff, 0x8653 },
3750 { 0x63, 0xffff, 0xab66 },
3751 { 0x60, 0xffff, 0x9455 },
3752 { 0x61, 0xffff, 0x99ff },
3753 { 0x69, 0xffff, 0xfe04 },
3756 rtl_set_def_aspm_entry_latency(tp);
3757 rtl_ephy_init(tp, e_info_8125a_2);
3758 rtl_hw_start_8125_common(tp);
3761 static void rtl_hw_start_8125b(struct rtl8169_private *tp)
3763 static const struct ephy_info e_info_8125b[] = {
3764 { 0x0b, 0xffff, 0xa908 },
3765 { 0x1e, 0xffff, 0x20eb },
3766 { 0x4b, 0xffff, 0xa908 },
3767 { 0x5e, 0xffff, 0x20eb },
3768 { 0x22, 0x0030, 0x0020 },
3769 { 0x62, 0x0030, 0x0020 },
3772 rtl_set_def_aspm_entry_latency(tp);
3773 rtl_ephy_init(tp, e_info_8125b);
3774 rtl_hw_start_8125_common(tp);
3777 static void rtl_hw_start_8126a(struct rtl8169_private *tp)
3779 rtl_set_def_aspm_entry_latency(tp);
3780 rtl_hw_start_8125_common(tp);
3783 static void rtl_hw_config(struct rtl8169_private *tp)
3785 static const rtl_generic_fct hw_configs[] = {
3786 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
3787 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
3788 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
3789 [RTL_GIGA_MAC_VER_10] = NULL,
3790 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168b,
3791 [RTL_GIGA_MAC_VER_14] = rtl_hw_start_8401,
3792 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168b,
3793 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
3794 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
3795 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
3796 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_2,
3797 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
3798 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
3799 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
3800 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
3801 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
3802 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
3803 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
3804 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
3805 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168d,
3806 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
3807 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
3808 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
3809 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
3810 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
3811 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
3812 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
3813 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
3814 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
3815 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
3816 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
3817 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
3818 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
3819 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
3820 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
3821 [RTL_GIGA_MAC_VER_52] = rtl_hw_start_8117,
3822 [RTL_GIGA_MAC_VER_53] = rtl_hw_start_8117,
3823 [RTL_GIGA_MAC_VER_61] = rtl_hw_start_8125a_2,
3824 [RTL_GIGA_MAC_VER_63] = rtl_hw_start_8125b,
3825 [RTL_GIGA_MAC_VER_65] = rtl_hw_start_8126a,
3828 if (hw_configs[tp->mac_version])
3829 hw_configs[tp->mac_version](tp);
3832 static void rtl_hw_start_8125(struct rtl8169_private *tp)
3836 RTL_W8(tp, INT_CFG0_8125, 0x00);
3838 /* disable interrupt coalescing */
3839 switch (tp->mac_version) {
3840 case RTL_GIGA_MAC_VER_61:
3841 for (i = 0xa00; i < 0xb00; i += 4)
3844 case RTL_GIGA_MAC_VER_63:
3845 case RTL_GIGA_MAC_VER_65:
3846 for (i = 0xa00; i < 0xa80; i += 4)
3848 RTL_W16(tp, INT_CFG1_8125, 0x0000);
3857 static void rtl_hw_start_8168(struct rtl8169_private *tp)
3859 if (rtl_is_8168evl_up(tp))
3860 RTL_W8(tp, MaxTxPacketSize, EarlySize);
3862 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
3866 /* disable interrupt coalescing */
3867 RTL_W16(tp, IntrMitigate, 0x0000);
3870 static void rtl_hw_start_8169(struct rtl8169_private *tp)
3872 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
3874 tp->cp_cmd |= PCIMulRW;
3876 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3877 tp->mac_version == RTL_GIGA_MAC_VER_03)
3878 tp->cp_cmd |= EnAnaPLL;
3880 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
3882 rtl8169_set_magic_reg(tp);
3884 /* disable interrupt coalescing */
3885 RTL_W16(tp, IntrMitigate, 0x0000);
3888 static void rtl_hw_start(struct rtl8169_private *tp)
3890 rtl_unlock_config_regs(tp);
3891 /* disable aspm and clock request before ephy access */
3892 rtl_hw_aspm_clkreq_enable(tp, false);
3893 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
3895 rtl_set_eee_txidle_timer(tp);
3897 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3898 rtl_hw_start_8169(tp);
3899 else if (rtl_is_8125(tp))
3900 rtl_hw_start_8125(tp);
3902 rtl_hw_start_8168(tp);
3904 rtl_enable_exit_l1(tp);
3905 rtl_hw_aspm_clkreq_enable(tp, true);
3906 rtl_set_rx_max_size(tp);
3907 rtl_set_rx_tx_desc_registers(tp);
3908 rtl_lock_config_regs(tp);
3910 rtl_jumbo_config(tp);
3912 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3915 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
3917 rtl_set_tx_config_registers(tp);
3918 rtl_set_rx_config_features(tp, tp->dev->features);
3919 rtl_set_rx_mode(tp->dev);
3923 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
3925 struct rtl8169_private *tp = netdev_priv(dev);
3927 WRITE_ONCE(dev->mtu, new_mtu);
3928 netdev_update_features(dev);
3929 rtl_jumbo_config(tp);
3930 rtl_set_eee_txidle_timer(tp);
3935 static void rtl8169_mark_to_asic(struct RxDesc *desc)
3937 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
3940 /* Force memory writes to complete before releasing descriptor */
3942 WRITE_ONCE(desc->opts1, cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE));
3945 static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
3946 struct RxDesc *desc)
3948 struct device *d = tp_to_dev(tp);
3949 int node = dev_to_node(d);
3953 data = alloc_pages_node(node, GFP_KERNEL, get_order(R8169_RX_BUF_SIZE));
3957 mapping = dma_map_page(d, data, 0, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
3958 if (unlikely(dma_mapping_error(d, mapping))) {
3959 netdev_err(tp->dev, "Failed to map RX DMA!\n");
3960 __free_pages(data, get_order(R8169_RX_BUF_SIZE));
3964 desc->addr = cpu_to_le64(mapping);
3965 rtl8169_mark_to_asic(desc);
3970 static void rtl8169_rx_clear(struct rtl8169_private *tp)
3974 for (i = 0; i < NUM_RX_DESC && tp->Rx_databuff[i]; i++) {
3975 dma_unmap_page(tp_to_dev(tp),
3976 le64_to_cpu(tp->RxDescArray[i].addr),
3977 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
3978 __free_pages(tp->Rx_databuff[i], get_order(R8169_RX_BUF_SIZE));
3979 tp->Rx_databuff[i] = NULL;
3980 tp->RxDescArray[i].addr = 0;
3981 tp->RxDescArray[i].opts1 = 0;
3985 static int rtl8169_rx_fill(struct rtl8169_private *tp)
3989 for (i = 0; i < NUM_RX_DESC; i++) {
3992 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
3994 rtl8169_rx_clear(tp);
3997 tp->Rx_databuff[i] = data;
4000 /* mark as last descriptor in the ring */
4001 tp->RxDescArray[NUM_RX_DESC - 1].opts1 |= cpu_to_le32(RingEnd);
4006 static int rtl8169_init_ring(struct rtl8169_private *tp)
4008 rtl8169_init_ring_indexes(tp);
4010 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
4011 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
4013 return rtl8169_rx_fill(tp);
4016 static void rtl8169_unmap_tx_skb(struct rtl8169_private *tp, unsigned int entry)
4018 struct ring_info *tx_skb = tp->tx_skb + entry;
4019 struct TxDesc *desc = tp->TxDescArray + entry;
4021 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr), tx_skb->len,
4023 memset(desc, 0, sizeof(*desc));
4024 memset(tx_skb, 0, sizeof(*tx_skb));
4027 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4032 for (i = 0; i < n; i++) {
4033 unsigned int entry = (start + i) % NUM_TX_DESC;
4034 struct ring_info *tx_skb = tp->tx_skb + entry;
4035 unsigned int len = tx_skb->len;
4038 struct sk_buff *skb = tx_skb->skb;
4040 rtl8169_unmap_tx_skb(tp, entry);
4042 dev_consume_skb_any(skb);
4047 static void rtl8169_tx_clear(struct rtl8169_private *tp)
4049 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
4050 netdev_reset_queue(tp->dev);
4053 static void rtl8169_cleanup(struct rtl8169_private *tp)
4055 napi_disable(&tp->napi);
4057 /* Give a racing hard_start_xmit a few cycles to complete. */
4060 /* Disable interrupts */
4061 rtl8169_irq_mask_and_ack(tp);
4065 switch (tp->mac_version) {
4066 case RTL_GIGA_MAC_VER_28:
4067 case RTL_GIGA_MAC_VER_31:
4068 rtl_loop_wait_low(tp, &rtl_npq_cond, 20, 2000);
4070 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4071 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4072 rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4074 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_65:
4075 rtl_enable_rxdvgate(tp);
4079 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4086 rtl8169_tx_clear(tp);
4087 rtl8169_init_ring_indexes(tp);
4090 static void rtl_reset_work(struct rtl8169_private *tp)
4094 netif_stop_queue(tp->dev);
4096 rtl8169_cleanup(tp);
4098 for (i = 0; i < NUM_RX_DESC; i++)
4099 rtl8169_mark_to_asic(tp->RxDescArray + i);
4101 napi_enable(&tp->napi);
4105 static void rtl8169_tx_timeout(struct net_device *dev, unsigned int txqueue)
4107 struct rtl8169_private *tp = netdev_priv(dev);
4109 rtl_schedule_task(tp, RTL_FLAG_TASK_TX_TIMEOUT);
4112 static int rtl8169_tx_map(struct rtl8169_private *tp, const u32 *opts, u32 len,
4113 void *addr, unsigned int entry, bool desc_own)
4115 struct TxDesc *txd = tp->TxDescArray + entry;
4116 struct device *d = tp_to_dev(tp);
4121 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
4122 ret = dma_mapping_error(d, mapping);
4123 if (unlikely(ret)) {
4124 if (net_ratelimit())
4125 netdev_err(tp->dev, "Failed to map TX data!\n");
4129 txd->addr = cpu_to_le64(mapping);
4130 txd->opts2 = cpu_to_le32(opts[1]);
4132 opts1 = opts[0] | len;
4133 if (entry == NUM_TX_DESC - 1)
4137 txd->opts1 = cpu_to_le32(opts1);
4139 tp->tx_skb[entry].len = len;
4144 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4145 const u32 *opts, unsigned int entry)
4147 struct skb_shared_info *info = skb_shinfo(skb);
4148 unsigned int cur_frag;
4150 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4151 const skb_frag_t *frag = info->frags + cur_frag;
4152 void *addr = skb_frag_address(frag);
4153 u32 len = skb_frag_size(frag);
4155 entry = (entry + 1) % NUM_TX_DESC;
4157 if (unlikely(rtl8169_tx_map(tp, opts, len, addr, entry, true)))
4164 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4168 static bool rtl_skb_is_udp(struct sk_buff *skb)
4170 int no = skb_network_offset(skb);
4171 struct ipv6hdr *i6h, _i6h;
4172 struct iphdr *ih, _ih;
4174 switch (vlan_get_protocol(skb)) {
4175 case htons(ETH_P_IP):
4176 ih = skb_header_pointer(skb, no, sizeof(_ih), &_ih);
4177 return ih && ih->protocol == IPPROTO_UDP;
4178 case htons(ETH_P_IPV6):
4179 i6h = skb_header_pointer(skb, no, sizeof(_i6h), &_i6h);
4180 return i6h && i6h->nexthdr == IPPROTO_UDP;
4186 #define RTL_MIN_PATCH_LEN 47
4188 /* see rtl8125_get_patch_pad_len() in r8125 vendor driver */
4189 static unsigned int rtl8125_quirk_udp_padto(struct rtl8169_private *tp,
4190 struct sk_buff *skb)
4192 unsigned int padto = 0, len = skb->len;
4194 if (rtl_is_8125(tp) && len < 128 + RTL_MIN_PATCH_LEN &&
4195 rtl_skb_is_udp(skb) && skb_transport_header_was_set(skb)) {
4196 unsigned int trans_data_len = skb_tail_pointer(skb) -
4197 skb_transport_header(skb);
4199 if (trans_data_len >= offsetof(struct udphdr, len) &&
4200 trans_data_len < RTL_MIN_PATCH_LEN) {
4201 u16 dest = ntohs(udp_hdr(skb)->dest);
4203 /* dest is a standard PTP port */
4204 if (dest == 319 || dest == 320)
4205 padto = len + RTL_MIN_PATCH_LEN - trans_data_len;
4208 if (trans_data_len < sizeof(struct udphdr))
4209 padto = max_t(unsigned int, padto,
4210 len + sizeof(struct udphdr) - trans_data_len);
4216 static unsigned int rtl_quirk_packet_padto(struct rtl8169_private *tp,
4217 struct sk_buff *skb)
4221 padto = rtl8125_quirk_udp_padto(tp, skb);
4223 switch (tp->mac_version) {
4224 case RTL_GIGA_MAC_VER_34:
4225 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_65:
4226 padto = max_t(unsigned int, padto, ETH_ZLEN);
4235 static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
4237 u32 mss = skb_shinfo(skb)->gso_size;
4241 opts[0] |= mss << TD0_MSS_SHIFT;
4242 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4243 const struct iphdr *ip = ip_hdr(skb);
4245 if (ip->protocol == IPPROTO_TCP)
4246 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
4247 else if (ip->protocol == IPPROTO_UDP)
4248 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
4254 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
4255 struct sk_buff *skb, u32 *opts)
4257 struct skb_shared_info *shinfo = skb_shinfo(skb);
4258 u32 mss = shinfo->gso_size;
4261 if (shinfo->gso_type & SKB_GSO_TCPV4) {
4262 opts[0] |= TD1_GTSENV4;
4263 } else if (shinfo->gso_type & SKB_GSO_TCPV6) {
4264 if (skb_cow_head(skb, 0))
4267 tcp_v6_gso_csum_prep(skb);
4268 opts[0] |= TD1_GTSENV6;
4273 opts[0] |= skb_transport_offset(skb) << GTTCPHO_SHIFT;
4274 opts[1] |= mss << TD1_MSS_SHIFT;
4275 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4278 switch (vlan_get_protocol(skb)) {
4279 case htons(ETH_P_IP):
4280 opts[1] |= TD1_IPv4_CS;
4281 ip_protocol = ip_hdr(skb)->protocol;
4284 case htons(ETH_P_IPV6):
4285 opts[1] |= TD1_IPv6_CS;
4286 ip_protocol = ipv6_hdr(skb)->nexthdr;
4290 ip_protocol = IPPROTO_RAW;
4294 if (ip_protocol == IPPROTO_TCP)
4295 opts[1] |= TD1_TCP_CS;
4296 else if (ip_protocol == IPPROTO_UDP)
4297 opts[1] |= TD1_UDP_CS;
4301 opts[1] |= skb_transport_offset(skb) << TCPHO_SHIFT;
4303 unsigned int padto = rtl_quirk_packet_padto(tp, skb);
4305 /* skb_padto would free the skb on error */
4306 return !__skb_put_padto(skb, padto, false);
4312 static unsigned int rtl_tx_slots_avail(struct rtl8169_private *tp)
4314 return READ_ONCE(tp->dirty_tx) + NUM_TX_DESC - READ_ONCE(tp->cur_tx);
4317 /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
4318 static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
4320 switch (tp->mac_version) {
4321 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
4322 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
4329 static void rtl8169_doorbell(struct rtl8169_private *tp)
4331 if (rtl_is_8125(tp))
4332 RTL_W16(tp, TxPoll_8125, BIT(0));
4334 RTL_W8(tp, TxPoll, NPQ);
4337 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4338 struct net_device *dev)
4340 unsigned int frags = skb_shinfo(skb)->nr_frags;
4341 struct rtl8169_private *tp = netdev_priv(dev);
4342 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
4343 struct TxDesc *txd_first, *txd_last;
4344 bool stop_queue, door_bell;
4347 if (unlikely(!rtl_tx_slots_avail(tp))) {
4348 if (net_ratelimit())
4349 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
4353 opts[1] = rtl8169_tx_vlan_tag(skb);
4356 if (!rtl_chip_supports_csum_v2(tp))
4357 rtl8169_tso_csum_v1(skb, opts);
4358 else if (!rtl8169_tso_csum_v2(tp, skb, opts))
4361 if (unlikely(rtl8169_tx_map(tp, opts, skb_headlen(skb), skb->data,
4365 txd_first = tp->TxDescArray + entry;
4368 if (rtl8169_xmit_frags(tp, skb, opts, entry))
4370 entry = (entry + frags) % NUM_TX_DESC;
4373 txd_last = tp->TxDescArray + entry;
4374 txd_last->opts1 |= cpu_to_le32(LastFrag);
4375 tp->tx_skb[entry].skb = skb;
4377 skb_tx_timestamp(skb);
4379 /* Force memory writes to complete before releasing descriptor */
4382 door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more());
4384 txd_first->opts1 |= cpu_to_le32(DescOwn | FirstFrag);
4386 /* rtl_tx needs to see descriptor changes before updated tp->cur_tx */
4389 WRITE_ONCE(tp->cur_tx, tp->cur_tx + frags + 1);
4391 stop_queue = !netif_subqueue_maybe_stop(dev, 0, rtl_tx_slots_avail(tp),
4393 R8169_TX_START_THRS);
4394 if (door_bell || stop_queue)
4395 rtl8169_doorbell(tp);
4397 return NETDEV_TX_OK;
4400 rtl8169_unmap_tx_skb(tp, entry);
4402 dev_kfree_skb_any(skb);
4403 dev->stats.tx_dropped++;
4404 return NETDEV_TX_OK;
4407 netif_stop_queue(dev);
4408 dev->stats.tx_dropped++;
4409 return NETDEV_TX_BUSY;
4412 static unsigned int rtl_last_frag_len(struct sk_buff *skb)
4414 struct skb_shared_info *info = skb_shinfo(skb);
4415 unsigned int nr_frags = info->nr_frags;
4420 return skb_frag_size(info->frags + nr_frags - 1);
4423 /* Workaround for hw issues with TSO on RTL8168evl */
4424 static netdev_features_t rtl8168evl_fix_tso(struct sk_buff *skb,
4425 netdev_features_t features)
4427 /* IPv4 header has options field */
4428 if (vlan_get_protocol(skb) == htons(ETH_P_IP) &&
4429 ip_hdrlen(skb) > sizeof(struct iphdr))
4430 features &= ~NETIF_F_ALL_TSO;
4432 /* IPv4 TCP header has options field */
4433 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 &&
4434 tcp_hdrlen(skb) > sizeof(struct tcphdr))
4435 features &= ~NETIF_F_ALL_TSO;
4437 else if (rtl_last_frag_len(skb) <= 6)
4438 features &= ~NETIF_F_ALL_TSO;
4443 static netdev_features_t rtl8169_features_check(struct sk_buff *skb,
4444 struct net_device *dev,
4445 netdev_features_t features)
4447 struct rtl8169_private *tp = netdev_priv(dev);
4449 if (skb_is_gso(skb)) {
4450 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4451 features = rtl8168evl_fix_tso(skb, features);
4453 if (skb_transport_offset(skb) > GTTCPHO_MAX &&
4454 rtl_chip_supports_csum_v2(tp))
4455 features &= ~NETIF_F_ALL_TSO;
4456 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4457 /* work around hw bug on some chip versions */
4458 if (skb->len < ETH_ZLEN)
4459 features &= ~NETIF_F_CSUM_MASK;
4461 if (rtl_quirk_packet_padto(tp, skb))
4462 features &= ~NETIF_F_CSUM_MASK;
4464 if (skb_transport_offset(skb) > TCPHO_MAX &&
4465 rtl_chip_supports_csum_v2(tp))
4466 features &= ~NETIF_F_CSUM_MASK;
4469 return vlan_features_check(skb, features);
4472 static void rtl8169_pcierr_interrupt(struct net_device *dev)
4474 struct rtl8169_private *tp = netdev_priv(dev);
4475 struct pci_dev *pdev = tp->pci_dev;
4476 int pci_status_errs;
4479 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4481 pci_status_errs = pci_status_get_and_clear_errors(pdev);
4483 if (net_ratelimit())
4484 netdev_err(dev, "PCI error (cmd = 0x%04x, status_errs = 0x%04x)\n",
4485 pci_cmd, pci_status_errs);
4487 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
4490 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
4493 unsigned int dirty_tx, bytes_compl = 0, pkts_compl = 0;
4494 struct sk_buff *skb;
4496 dirty_tx = tp->dirty_tx;
4498 while (READ_ONCE(tp->cur_tx) != dirty_tx) {
4499 unsigned int entry = dirty_tx % NUM_TX_DESC;
4502 status = le32_to_cpu(READ_ONCE(tp->TxDescArray[entry].opts1));
4503 if (status & DescOwn)
4506 skb = tp->tx_skb[entry].skb;
4507 rtl8169_unmap_tx_skb(tp, entry);
4511 bytes_compl += skb->len;
4512 napi_consume_skb(skb, budget);
4517 if (tp->dirty_tx != dirty_tx) {
4518 dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl);
4519 WRITE_ONCE(tp->dirty_tx, dirty_tx);
4521 netif_subqueue_completed_wake(dev, 0, pkts_compl, bytes_compl,
4522 rtl_tx_slots_avail(tp),
4523 R8169_TX_START_THRS);
4525 * 8168 hack: TxPoll requests are lost when the Tx packets are
4526 * too close. Let's kick an extra TxPoll request when a burst
4527 * of start_xmit activity is detected (if it is not detected,
4528 * it is slow enough). -- FR
4529 * If skb is NULL then we come here again once a tx irq is
4530 * triggered after the last fragment is marked transmitted.
4532 if (READ_ONCE(tp->cur_tx) != dirty_tx && skb)
4533 rtl8169_doorbell(tp);
4537 static inline int rtl8169_fragmented_frame(u32 status)
4539 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4542 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
4544 u32 status = opts1 & (RxProtoMask | RxCSFailMask);
4546 if (status == RxProtoTCP || status == RxProtoUDP)
4547 skb->ip_summed = CHECKSUM_UNNECESSARY;
4549 skb_checksum_none_assert(skb);
4552 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget)
4554 struct device *d = tp_to_dev(tp);
4557 for (count = 0; count < budget; count++, tp->cur_rx++) {
4558 unsigned int pkt_size, entry = tp->cur_rx % NUM_RX_DESC;
4559 struct RxDesc *desc = tp->RxDescArray + entry;
4560 struct sk_buff *skb;
4565 status = le32_to_cpu(READ_ONCE(desc->opts1));
4566 if (status & DescOwn)
4569 /* This barrier is needed to keep us from reading
4570 * any other fields out of the Rx descriptor until
4571 * we know the status of DescOwn
4575 if (unlikely(status & RxRES)) {
4576 if (net_ratelimit())
4577 netdev_warn(dev, "Rx ERROR. status = %08x\n",
4579 dev->stats.rx_errors++;
4580 if (status & (RxRWT | RxRUNT))
4581 dev->stats.rx_length_errors++;
4583 dev->stats.rx_crc_errors++;
4585 if (!(dev->features & NETIF_F_RXALL))
4586 goto release_descriptor;
4587 else if (status & RxRWT || !(status & (RxRUNT | RxCRC)))
4588 goto release_descriptor;
4591 pkt_size = status & GENMASK(13, 0);
4592 if (likely(!(dev->features & NETIF_F_RXFCS)))
4593 pkt_size -= ETH_FCS_LEN;
4595 /* The driver does not support incoming fragmented frames.
4596 * They are seen as a symptom of over-mtu sized frames.
4598 if (unlikely(rtl8169_fragmented_frame(status))) {
4599 dev->stats.rx_dropped++;
4600 dev->stats.rx_length_errors++;
4601 goto release_descriptor;
4604 skb = napi_alloc_skb(&tp->napi, pkt_size);
4605 if (unlikely(!skb)) {
4606 dev->stats.rx_dropped++;
4607 goto release_descriptor;
4610 addr = le64_to_cpu(desc->addr);
4611 rx_buf = page_address(tp->Rx_databuff[entry]);
4613 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
4615 skb_copy_to_linear_data(skb, rx_buf, pkt_size);
4616 skb->tail += pkt_size;
4617 skb->len = pkt_size;
4618 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4620 rtl8169_rx_csum(skb, status);
4621 skb->protocol = eth_type_trans(skb, dev);
4623 rtl8169_rx_vlan_tag(desc, skb);
4625 if (skb->pkt_type == PACKET_MULTICAST)
4626 dev->stats.multicast++;
4628 napi_gro_receive(&tp->napi, skb);
4630 dev_sw_netstats_rx_add(dev, pkt_size);
4632 rtl8169_mark_to_asic(desc);
4638 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
4640 struct rtl8169_private *tp = dev_instance;
4641 u32 status = rtl_get_events(tp);
4643 if ((status & 0xffff) == 0xffff || !(status & tp->irq_mask))
4646 if (unlikely(status & SYSErr)) {
4647 rtl8169_pcierr_interrupt(tp->dev);
4651 if (status & LinkChg)
4652 phy_mac_interrupt(tp->phydev);
4654 if (unlikely(status & RxFIFOOver &&
4655 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
4656 netif_stop_queue(tp->dev);
4657 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
4660 if (napi_schedule_prep(&tp->napi)) {
4661 rtl_irq_disable(tp);
4662 __napi_schedule(&tp->napi);
4665 rtl_ack_events(tp, status);
4670 static void rtl_task(struct work_struct *work)
4672 struct rtl8169_private *tp =
4673 container_of(work, struct rtl8169_private, wk.work);
4678 if (!test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
4681 if (test_and_clear_bit(RTL_FLAG_TASK_TX_TIMEOUT, tp->wk.flags)) {
4682 /* if chip isn't accessible, reset bus to revive it */
4683 if (RTL_R32(tp, TxConfig) == ~0) {
4684 ret = pci_reset_bus(tp->pci_dev);
4686 netdev_err(tp->dev, "Can't reset secondary PCI bus, detach NIC\n");
4687 netif_device_detach(tp->dev);
4692 /* ASPM compatibility issues are a typical reason for tx timeouts */
4693 ret = pci_disable_link_state(tp->pci_dev, PCIE_LINK_STATE_L1 |
4694 PCIE_LINK_STATE_L0S);
4696 netdev_warn_once(tp->dev, "ASPM disabled on Tx timeout\n");
4700 if (test_and_clear_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags)) {
4703 netif_wake_queue(tp->dev);
4704 } else if (test_and_clear_bit(RTL_FLAG_TASK_RESET_NO_QUEUE_WAKE, tp->wk.flags)) {
4711 static int rtl8169_poll(struct napi_struct *napi, int budget)
4713 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
4714 struct net_device *dev = tp->dev;
4717 rtl_tx(dev, tp, budget);
4719 work_done = rtl_rx(dev, tp, budget);
4721 if (work_done < budget && napi_complete_done(napi, work_done))
4727 static void r8169_phylink_handler(struct net_device *ndev)
4729 struct rtl8169_private *tp = netdev_priv(ndev);
4730 struct device *d = tp_to_dev(tp);
4732 if (netif_carrier_ok(ndev)) {
4733 rtl_link_chg_patch(tp);
4734 pm_request_resume(d);
4735 netif_wake_queue(tp->dev);
4737 /* In few cases rx is broken after link-down otherwise */
4738 if (rtl_is_8125(tp))
4739 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_NO_QUEUE_WAKE);
4743 phy_print_status(tp->phydev);
4746 static int r8169_phy_connect(struct rtl8169_private *tp)
4748 struct phy_device *phydev = tp->phydev;
4749 phy_interface_t phy_mode;
4752 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
4753 PHY_INTERFACE_MODE_MII;
4755 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
4760 if (!tp->supports_gmii)
4761 phy_set_max_speed(phydev, SPEED_100);
4763 phy_attached_info(phydev);
4768 static void rtl8169_down(struct rtl8169_private *tp)
4770 /* Clear all task flags */
4771 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
4773 phy_stop(tp->phydev);
4775 rtl8169_update_counters(tp);
4777 pci_clear_master(tp->pci_dev);
4780 rtl8169_cleanup(tp);
4781 rtl_disable_exit_l1(tp);
4782 rtl_prepare_power_down(tp);
4784 if (tp->dash_type != RTL_DASH_NONE)
4785 rtl8168_driver_stop(tp);
4788 static void rtl8169_up(struct rtl8169_private *tp)
4790 if (tp->dash_type != RTL_DASH_NONE)
4791 rtl8168_driver_start(tp);
4793 pci_set_master(tp->pci_dev);
4794 phy_init_hw(tp->phydev);
4795 phy_resume(tp->phydev);
4796 rtl8169_init_phy(tp);
4797 napi_enable(&tp->napi);
4798 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
4801 phy_start(tp->phydev);
4804 static int rtl8169_close(struct net_device *dev)
4806 struct rtl8169_private *tp = netdev_priv(dev);
4807 struct pci_dev *pdev = tp->pci_dev;
4809 pm_runtime_get_sync(&pdev->dev);
4811 netif_stop_queue(dev);
4813 rtl8169_rx_clear(tp);
4815 cancel_work(&tp->wk.work);
4817 free_irq(tp->irq, tp);
4819 phy_disconnect(tp->phydev);
4821 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4823 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4825 tp->TxDescArray = NULL;
4826 tp->RxDescArray = NULL;
4828 pm_runtime_put_sync(&pdev->dev);
4833 #ifdef CONFIG_NET_POLL_CONTROLLER
4834 static void rtl8169_netpoll(struct net_device *dev)
4836 struct rtl8169_private *tp = netdev_priv(dev);
4838 rtl8169_interrupt(tp->irq, tp);
4842 static int rtl_open(struct net_device *dev)
4844 struct rtl8169_private *tp = netdev_priv(dev);
4845 struct pci_dev *pdev = tp->pci_dev;
4846 unsigned long irqflags;
4847 int retval = -ENOMEM;
4849 pm_runtime_get_sync(&pdev->dev);
4852 * Rx and Tx descriptors needs 256 bytes alignment.
4853 * dma_alloc_coherent provides more.
4855 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4856 &tp->TxPhyAddr, GFP_KERNEL);
4857 if (!tp->TxDescArray)
4860 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4861 &tp->RxPhyAddr, GFP_KERNEL);
4862 if (!tp->RxDescArray)
4865 retval = rtl8169_init_ring(tp);
4869 rtl_request_firmware(tp);
4871 irqflags = pci_dev_msi_enabled(pdev) ? IRQF_NO_THREAD : IRQF_SHARED;
4872 retval = request_irq(tp->irq, rtl8169_interrupt, irqflags, dev->name, tp);
4874 goto err_release_fw_2;
4876 retval = r8169_phy_connect(tp);
4881 rtl8169_init_counter_offsets(tp);
4882 netif_start_queue(dev);
4884 pm_runtime_put_sync(&pdev->dev);
4889 free_irq(tp->irq, tp);
4891 rtl_release_firmware(tp);
4892 rtl8169_rx_clear(tp);
4894 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4896 tp->RxDescArray = NULL;
4898 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4900 tp->TxDescArray = NULL;
4905 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
4907 struct rtl8169_private *tp = netdev_priv(dev);
4908 struct pci_dev *pdev = tp->pci_dev;
4909 struct rtl8169_counters *counters = tp->counters;
4911 pm_runtime_get_noresume(&pdev->dev);
4913 netdev_stats_to_stats64(stats, &dev->stats);
4914 dev_fetch_sw_netstats(stats, dev->tstats);
4917 * Fetch additional counter values missing in stats collected by driver
4918 * from tally counters.
4920 if (pm_runtime_active(&pdev->dev))
4921 rtl8169_update_counters(tp);
4924 * Subtract values fetched during initalization.
4925 * See rtl8169_init_counter_offsets for a description why we do that.
4927 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
4928 le64_to_cpu(tp->tc_offset.tx_errors);
4929 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
4930 le32_to_cpu(tp->tc_offset.tx_multi_collision);
4931 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
4932 le16_to_cpu(tp->tc_offset.tx_aborted);
4933 stats->rx_missed_errors = le16_to_cpu(counters->rx_missed) -
4934 le16_to_cpu(tp->tc_offset.rx_missed);
4936 pm_runtime_put_noidle(&pdev->dev);
4939 static void rtl8169_net_suspend(struct rtl8169_private *tp)
4941 netif_device_detach(tp->dev);
4943 if (netif_running(tp->dev))
4947 static int rtl8169_runtime_resume(struct device *dev)
4949 struct rtl8169_private *tp = dev_get_drvdata(dev);
4951 rtl_rar_set(tp, tp->dev->dev_addr);
4952 __rtl8169_set_wol(tp, tp->saved_wolopts);
4954 if (tp->TxDescArray)
4957 netif_device_attach(tp->dev);
4962 static int rtl8169_suspend(struct device *device)
4964 struct rtl8169_private *tp = dev_get_drvdata(device);
4967 rtl8169_net_suspend(tp);
4968 if (!device_may_wakeup(tp_to_dev(tp)))
4969 clk_disable_unprepare(tp->clk);
4975 static int rtl8169_resume(struct device *device)
4977 struct rtl8169_private *tp = dev_get_drvdata(device);
4979 if (!device_may_wakeup(tp_to_dev(tp)))
4980 clk_prepare_enable(tp->clk);
4982 /* Reportedly at least Asus X453MA truncates packets otherwise */
4983 if (tp->mac_version == RTL_GIGA_MAC_VER_37)
4986 return rtl8169_runtime_resume(device);
4989 static int rtl8169_runtime_suspend(struct device *device)
4991 struct rtl8169_private *tp = dev_get_drvdata(device);
4993 if (!tp->TxDescArray) {
4994 netif_device_detach(tp->dev);
4999 __rtl8169_set_wol(tp, WAKE_PHY);
5000 rtl8169_net_suspend(tp);
5006 static int rtl8169_runtime_idle(struct device *device)
5008 struct rtl8169_private *tp = dev_get_drvdata(device);
5010 if (tp->dash_enabled)
5013 if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev))
5014 pm_schedule_suspend(device, 10000);
5019 static const struct dev_pm_ops rtl8169_pm_ops = {
5020 SYSTEM_SLEEP_PM_OPS(rtl8169_suspend, rtl8169_resume)
5021 RUNTIME_PM_OPS(rtl8169_runtime_suspend, rtl8169_runtime_resume,
5022 rtl8169_runtime_idle)
5025 static void rtl_shutdown(struct pci_dev *pdev)
5027 struct rtl8169_private *tp = pci_get_drvdata(pdev);
5030 rtl8169_net_suspend(tp);
5033 /* Restore original MAC address */
5034 rtl_rar_set(tp, tp->dev->perm_addr);
5036 if (system_state == SYSTEM_POWER_OFF && !tp->dash_enabled) {
5037 pci_wake_from_d3(pdev, tp->saved_wolopts);
5038 pci_set_power_state(pdev, PCI_D3hot);
5042 static void rtl_remove_one(struct pci_dev *pdev)
5044 struct rtl8169_private *tp = pci_get_drvdata(pdev);
5046 if (pci_dev_run_wake(pdev))
5047 pm_runtime_get_noresume(&pdev->dev);
5049 cancel_work_sync(&tp->wk.work);
5051 if (IS_ENABLED(CONFIG_R8169_LEDS))
5052 r8169_remove_leds(tp->leds);
5054 unregister_netdev(tp->dev);
5056 if (tp->dash_type != RTL_DASH_NONE)
5057 rtl8168_driver_stop(tp);
5059 rtl_release_firmware(tp);
5061 /* restore original MAC address */
5062 rtl_rar_set(tp, tp->dev->perm_addr);
5065 static const struct net_device_ops rtl_netdev_ops = {
5066 .ndo_open = rtl_open,
5067 .ndo_stop = rtl8169_close,
5068 .ndo_get_stats64 = rtl8169_get_stats64,
5069 .ndo_start_xmit = rtl8169_start_xmit,
5070 .ndo_features_check = rtl8169_features_check,
5071 .ndo_tx_timeout = rtl8169_tx_timeout,
5072 .ndo_validate_addr = eth_validate_addr,
5073 .ndo_change_mtu = rtl8169_change_mtu,
5074 .ndo_fix_features = rtl8169_fix_features,
5075 .ndo_set_features = rtl8169_set_features,
5076 .ndo_set_mac_address = rtl_set_mac_address,
5077 .ndo_eth_ioctl = phy_do_ioctl_running,
5078 .ndo_set_rx_mode = rtl_set_rx_mode,
5079 #ifdef CONFIG_NET_POLL_CONTROLLER
5080 .ndo_poll_controller = rtl8169_netpoll,
5085 static void rtl_set_irq_mask(struct rtl8169_private *tp)
5087 tp->irq_mask = RxOK | RxErr | TxOK | TxErr | LinkChg;
5089 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
5090 tp->irq_mask |= SYSErr | RxOverflow | RxFIFOOver;
5091 else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
5092 /* special workaround needed */
5093 tp->irq_mask |= RxFIFOOver;
5095 tp->irq_mask |= RxOverflow;
5098 static int rtl_alloc_irq(struct rtl8169_private *tp)
5102 switch (tp->mac_version) {
5103 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5104 rtl_unlock_config_regs(tp);
5105 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
5106 rtl_lock_config_regs(tp);
5108 case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_17:
5109 flags = PCI_IRQ_INTX;
5112 flags = PCI_IRQ_ALL_TYPES;
5116 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
5119 static void rtl_read_mac_address(struct rtl8169_private *tp,
5120 u8 mac_addr[ETH_ALEN])
5122 /* Get MAC address */
5123 if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) {
5126 value = rtl_eri_read(tp, 0xe0);
5127 put_unaligned_le32(value, mac_addr);
5128 value = rtl_eri_read(tp, 0xe4);
5129 put_unaligned_le16(value, mac_addr + 4);
5130 } else if (rtl_is_8125(tp)) {
5131 rtl_read_mac_from_reg(tp, mac_addr, MAC0_BKP);
5135 DECLARE_RTL_COND(rtl_link_list_ready_cond)
5137 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
5140 static void r8168g_wait_ll_share_fifo_ready(struct rtl8169_private *tp)
5142 rtl_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
5145 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
5147 struct rtl8169_private *tp = mii_bus->priv;
5152 return rtl_readphy(tp, phyreg);
5155 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
5156 int phyreg, u16 val)
5158 struct rtl8169_private *tp = mii_bus->priv;
5163 rtl_writephy(tp, phyreg, val);
5168 static int r8169_mdio_register(struct rtl8169_private *tp)
5170 struct pci_dev *pdev = tp->pci_dev;
5171 struct mii_bus *new_bus;
5174 /* On some boards with this chip version the BIOS is buggy and misses
5175 * to reset the PHY page selector. This results in the PHY ID read
5176 * accessing registers on a different page, returning a more or
5177 * less random value. Fix this by resetting the page selector first.
5179 if (tp->mac_version == RTL_GIGA_MAC_VER_25 ||
5180 tp->mac_version == RTL_GIGA_MAC_VER_26)
5181 r8169_mdio_write(tp, 0x1f, 0);
5183 new_bus = devm_mdiobus_alloc(&pdev->dev);
5187 new_bus->name = "r8169";
5189 new_bus->parent = &pdev->dev;
5190 new_bus->irq[0] = PHY_MAC_INTERRUPT;
5191 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x-%x",
5192 pci_domain_nr(pdev->bus), pci_dev_id(pdev));
5194 new_bus->read = r8169_mdio_read_reg;
5195 new_bus->write = r8169_mdio_write_reg;
5197 ret = devm_mdiobus_register(&pdev->dev, new_bus);
5201 tp->phydev = mdiobus_get_phy(new_bus, 0);
5204 } else if (!tp->phydev->drv) {
5205 /* Most chip versions fail with the genphy driver.
5206 * Therefore ensure that the dedicated PHY driver is loaded.
5208 dev_err(&pdev->dev, "no dedicated PHY driver found for PHY ID 0x%08x, maybe realtek.ko needs to be added to initramfs?\n",
5209 tp->phydev->phy_id);
5213 tp->phydev->mac_managed_pm = true;
5214 if (rtl_supports_eee(tp))
5215 phy_support_eee(tp->phydev);
5216 phy_support_asym_pause(tp->phydev);
5218 /* PHY will be woken up in rtl_open() */
5219 phy_suspend(tp->phydev);
5224 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
5226 rtl_enable_rxdvgate(tp);
5228 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
5230 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5232 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
5233 r8168g_wait_ll_share_fifo_ready(tp);
5235 r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15));
5236 r8168g_wait_ll_share_fifo_ready(tp);
5239 static void rtl_hw_init_8125(struct rtl8169_private *tp)
5241 rtl_enable_rxdvgate(tp);
5243 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
5245 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5247 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
5248 r8168g_wait_ll_share_fifo_ready(tp);
5250 r8168_mac_ocp_write(tp, 0xc0aa, 0x07d0);
5251 r8168_mac_ocp_write(tp, 0xc0a6, 0x0150);
5252 r8168_mac_ocp_write(tp, 0xc01e, 0x5555);
5253 r8168g_wait_ll_share_fifo_ready(tp);
5256 static void rtl_hw_initialize(struct rtl8169_private *tp)
5258 switch (tp->mac_version) {
5259 case RTL_GIGA_MAC_VER_51 ... RTL_GIGA_MAC_VER_53:
5260 rtl8168ep_stop_cmac(tp);
5262 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
5263 rtl_hw_init_8168g(tp);
5265 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_65:
5266 rtl_hw_init_8125(tp);
5273 static int rtl_jumbo_max(struct rtl8169_private *tp)
5275 /* Non-GBit versions don't support jumbo frames */
5276 if (!tp->supports_gmii)
5279 switch (tp->mac_version) {
5281 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5284 case RTL_GIGA_MAC_VER_11:
5285 case RTL_GIGA_MAC_VER_17:
5288 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
5295 static void rtl_init_mac_address(struct rtl8169_private *tp)
5297 u8 mac_addr[ETH_ALEN] __aligned(2) = {};
5298 struct net_device *dev = tp->dev;
5301 rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
5305 rtl_read_mac_address(tp, mac_addr);
5306 if (is_valid_ether_addr(mac_addr))
5309 rtl_read_mac_from_reg(tp, mac_addr, MAC0);
5310 if (is_valid_ether_addr(mac_addr))
5313 eth_random_addr(mac_addr);
5314 dev->addr_assign_type = NET_ADDR_RANDOM;
5315 dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
5317 eth_hw_addr_set(dev, mac_addr);
5318 rtl_rar_set(tp, mac_addr);
5321 /* register is set if system vendor successfully tested ASPM 1.2 */
5322 static bool rtl_aspm_is_safe(struct rtl8169_private *tp)
5324 if (tp->mac_version >= RTL_GIGA_MAC_VER_61 &&
5325 r8168_mac_ocp_read(tp, 0xc0b2) & 0xf)
5331 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
5333 struct rtl8169_private *tp;
5334 int jumbo_max, region, rc;
5335 enum mac_version chipset;
5336 struct net_device *dev;
5340 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
5344 SET_NETDEV_DEV(dev, &pdev->dev);
5345 dev->netdev_ops = &rtl_netdev_ops;
5346 tp = netdev_priv(dev);
5349 tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
5350 tp->ocp_base = OCP_STD_PHY_BASE;
5352 raw_spin_lock_init(&tp->cfg9346_usage_lock);
5353 raw_spin_lock_init(&tp->config25_lock);
5354 raw_spin_lock_init(&tp->mac_ocp_lock);
5355 mutex_init(&tp->led_lock);
5357 /* Get the *optional* external "ether_clk" used on some boards */
5358 tp->clk = devm_clk_get_optional_enabled(&pdev->dev, "ether_clk");
5359 if (IS_ERR(tp->clk))
5360 return dev_err_probe(&pdev->dev, PTR_ERR(tp->clk), "failed to get ether_clk\n");
5362 /* enable device (incl. PCI PM wakeup and hotplug setup) */
5363 rc = pcim_enable_device(pdev);
5365 return dev_err_probe(&pdev->dev, rc, "enable failure\n");
5367 if (pcim_set_mwi(pdev) < 0)
5368 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
5370 /* use first MMIO region */
5371 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
5373 return dev_err_probe(&pdev->dev, -ENODEV, "no MMIO resource found\n");
5375 rc = pcim_iomap_regions(pdev, BIT(region), KBUILD_MODNAME);
5377 return dev_err_probe(&pdev->dev, rc, "cannot remap MMIO, aborting\n");
5379 tp->mmio_addr = pcim_iomap_table(pdev)[region];
5381 txconfig = RTL_R32(tp, TxConfig);
5382 if (txconfig == ~0U)
5383 return dev_err_probe(&pdev->dev, -EIO, "PCI read failed\n");
5385 xid = (txconfig >> 20) & 0xfcf;
5387 /* Identify chip attached to board */
5388 chipset = rtl8169_get_mac_version(xid, tp->supports_gmii);
5389 if (chipset == RTL_GIGA_MAC_NONE)
5390 return dev_err_probe(&pdev->dev, -ENODEV,
5391 "unknown chip XID %03x, contact r8169 maintainers (see MAINTAINERS file)\n",
5393 tp->mac_version = chipset;
5395 /* Disable ASPM L1 as that cause random device stop working
5396 * problems as well as full system hangs for some PCIe devices users.
5398 if (rtl_aspm_is_safe(tp))
5401 rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L1);
5402 tp->aspm_manageable = !rc;
5404 tp->dash_type = rtl_get_dash_type(tp);
5405 tp->dash_enabled = rtl_dash_is_enabled(tp);
5407 tp->cp_cmd = RTL_R16(tp, CPlusCmd) & CPCMD_MASK;
5409 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
5410 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
5411 dev->features |= NETIF_F_HIGHDMA;
5415 rtl8169_irq_mask_and_ack(tp);
5417 rtl_hw_initialize(tp);
5421 rc = rtl_alloc_irq(tp);
5423 return dev_err_probe(&pdev->dev, rc, "Can't allocate interrupt\n");
5425 tp->irq = pci_irq_vector(pdev, 0);
5427 INIT_WORK(&tp->wk.work, rtl_task);
5429 rtl_init_mac_address(tp);
5431 dev->ethtool_ops = &rtl8169_ethtool_ops;
5433 netif_napi_add(dev, &tp->napi, rtl8169_poll);
5435 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
5436 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
5437 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
5438 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
5441 * Pretend we are using VLANs; This bypasses a nasty bug where
5442 * Interrupts stop flowing on high load on 8110SCd controllers.
5444 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
5445 /* Disallow toggling */
5446 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
5448 if (rtl_chip_supports_csum_v2(tp))
5449 dev->hw_features |= NETIF_F_IPV6_CSUM;
5451 dev->features |= dev->hw_features;
5453 /* There has been a number of reports that using SG/TSO results in
5454 * tx timeouts. However for a lot of people SG/TSO works fine.
5455 * Therefore disable both features by default, but allow users to
5456 * enable them. Use at own risk!
5458 if (rtl_chip_supports_csum_v2(tp)) {
5459 dev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6;
5460 netif_set_tso_max_size(dev, RTL_GSO_MAX_SIZE_V2);
5461 netif_set_tso_max_segs(dev, RTL_GSO_MAX_SEGS_V2);
5463 dev->hw_features |= NETIF_F_SG | NETIF_F_TSO;
5464 netif_set_tso_max_size(dev, RTL_GSO_MAX_SIZE_V1);
5465 netif_set_tso_max_segs(dev, RTL_GSO_MAX_SEGS_V1);
5468 dev->hw_features |= NETIF_F_RXALL;
5469 dev->hw_features |= NETIF_F_RXFCS;
5471 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
5473 netdev_sw_irq_coalesce_default_on(dev);
5475 /* configure chip for default features */
5476 rtl8169_set_features(dev, dev->features);
5478 if (!tp->dash_enabled) {
5479 rtl_set_d3_pll_down(tp, true);
5481 rtl_set_d3_pll_down(tp, false);
5482 dev->wol_enabled = 1;
5485 jumbo_max = rtl_jumbo_max(tp);
5487 dev->max_mtu = jumbo_max;
5489 rtl_set_irq_mask(tp);
5491 tp->fw_name = rtl_chip_infos[chipset].fw_name;
5493 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
5494 &tp->counters_phys_addr,
5499 pci_set_drvdata(pdev, tp);
5501 rc = r8169_mdio_register(tp);
5505 rc = register_netdev(dev);
5509 if (IS_ENABLED(CONFIG_R8169_LEDS)) {
5510 if (rtl_is_8125(tp))
5511 tp->leds = rtl8125_init_leds(dev);
5512 else if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5513 tp->leds = rtl8168_init_leds(dev);
5516 netdev_info(dev, "%s, %pM, XID %03x, IRQ %d\n",
5517 rtl_chip_infos[chipset].name, dev->dev_addr, xid, tp->irq);
5520 netdev_info(dev, "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
5521 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
5524 if (tp->dash_type != RTL_DASH_NONE) {
5525 netdev_info(dev, "DASH %s\n",
5526 tp->dash_enabled ? "enabled" : "disabled");
5527 rtl8168_driver_start(tp);
5530 if (pci_dev_run_wake(pdev))
5531 pm_runtime_put_sync(&pdev->dev);
5536 static struct pci_driver rtl8169_pci_driver = {
5537 .name = KBUILD_MODNAME,
5538 .id_table = rtl8169_pci_tbl,
5539 .probe = rtl_init_one,
5540 .remove = rtl_remove_one,
5541 .shutdown = rtl_shutdown,
5542 .driver.pm = pm_ptr(&rtl8169_pm_ops),
5545 module_pci_driver(rtl8169_pci_driver);