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/moduleparam.h>
14 #include <linux/pci.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/ethtool.h>
20 #include <linux/phy.h>
21 #include <linux/if_vlan.h>
22 #include <linux/crc32.h>
26 #include <linux/tcp.h>
27 #include <linux/interrupt.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/prefetch.h>
31 #include <linux/ipv6.h>
32 #include <net/ip6_checksum.h>
34 #include "r8169_firmware.h"
36 #define MODULENAME "r8169"
38 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
40 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
42 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
43 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
44 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
45 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
46 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
47 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
48 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
49 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
50 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
51 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
52 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
53 #define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
54 #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
55 #define FIRMWARE_8168FP_3 "rtl_nic/rtl8168fp-3.fw"
56 #define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
57 #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
58 #define FIRMWARE_8125A_3 "rtl_nic/rtl8125a-3.fw"
60 #define R8169_MSG_DEFAULT \
61 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
63 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
64 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
65 #define MC_FILTER_LIMIT 32
67 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
68 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
70 #define R8169_REGS_SIZE 256
71 #define R8169_RX_BUF_SIZE (SZ_16K - 1)
72 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
73 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
74 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
75 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
77 #define RTL_CFG_NO_GBIT 1
79 /* write/read MMIO register */
80 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
81 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
82 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
83 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
84 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
85 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
88 /* support for ancient RTL_GIGA_MAC_VER_01 has been removed */
145 #define JUMBO_1K ETH_DATA_LEN
146 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
147 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
148 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
149 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
151 static const struct {
154 } rtl_chip_infos[] = {
156 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
157 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
158 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
159 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
160 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
162 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
163 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
164 [RTL_GIGA_MAC_VER_09] = {"RTL8102e/RTL8103e" },
165 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
166 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
167 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
168 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
169 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
170 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
171 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
172 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
173 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
174 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
175 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
176 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
177 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
178 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
179 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
180 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
181 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
182 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
183 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
184 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
185 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
186 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
187 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
188 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
189 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
190 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
191 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
192 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
193 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
194 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
195 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
196 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
197 [RTL_GIGA_MAC_VER_42] = {"RTL8168gu/8111gu", FIRMWARE_8168G_3},
198 [RTL_GIGA_MAC_VER_43] = {"RTL8106eus", FIRMWARE_8106E_2},
199 [RTL_GIGA_MAC_VER_44] = {"RTL8411b", FIRMWARE_8411_2 },
200 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
201 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
202 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
203 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
204 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
205 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
206 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
207 [RTL_GIGA_MAC_VER_52] = {"RTL8168fp/RTL8117", FIRMWARE_8168FP_3},
208 [RTL_GIGA_MAC_VER_60] = {"RTL8125" },
209 [RTL_GIGA_MAC_VER_61] = {"RTL8125", FIRMWARE_8125A_3},
212 static const struct pci_device_id rtl8169_pci_tbl[] = {
213 { PCI_VDEVICE(REALTEK, 0x2502) },
214 { PCI_VDEVICE(REALTEK, 0x2600) },
215 { PCI_VDEVICE(REALTEK, 0x8129) },
216 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_NO_GBIT },
217 { PCI_VDEVICE(REALTEK, 0x8161) },
218 { PCI_VDEVICE(REALTEK, 0x8167) },
219 { PCI_VDEVICE(REALTEK, 0x8168) },
220 { PCI_VDEVICE(NCUBE, 0x8168) },
221 { PCI_VDEVICE(REALTEK, 0x8169) },
222 { PCI_VENDOR_ID_DLINK, 0x4300,
223 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0 },
224 { PCI_VDEVICE(DLINK, 0x4300) },
225 { PCI_VDEVICE(DLINK, 0x4302) },
226 { PCI_VDEVICE(AT, 0xc107) },
227 { PCI_VDEVICE(USR, 0x0116) },
228 { PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0024 },
229 { 0x0001, 0x8168, PCI_ANY_ID, 0x2410 },
230 { PCI_VDEVICE(REALTEK, 0x8125) },
231 { PCI_VDEVICE(REALTEK, 0x3000) },
235 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
242 MAC0 = 0, /* Ethernet hardware address. */
244 MAR0 = 8, /* Multicast filter. */
245 CounterAddrLow = 0x10,
246 CounterAddrHigh = 0x14,
247 TxDescStartAddrLow = 0x20,
248 TxDescStartAddrHigh = 0x24,
249 TxHDescStartAddrLow = 0x28,
250 TxHDescStartAddrHigh = 0x2c,
259 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
260 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
263 #define RX128_INT_EN (1 << 15) /* 8111c and later */
264 #define RX_MULTI_EN (1 << 14) /* 8111c only */
265 #define RXCFG_FIFO_SHIFT 13
266 /* No threshold before first PCI xfer */
267 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
268 #define RX_EARLY_OFF (1 << 11)
269 #define RXCFG_DMA_SHIFT 8
270 /* Unlimited maximum PCI burst. */
271 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
278 #define PME_SIGNAL (1 << 5) /* 8168c and later */
289 #define RTL_COALESCE_MASK 0x0f
290 #define RTL_COALESCE_SHIFT 4
291 #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
292 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
294 RxDescAddrLow = 0xe4,
295 RxDescAddrHigh = 0xe8,
296 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
298 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
300 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
302 #define TxPacketMax (8064 >> 7)
303 #define EarlySize 0x27
306 FuncEventMask = 0xf4,
307 FuncPresetState = 0xf8,
312 FuncForceEvent = 0xfc,
315 enum rtl8168_8101_registers {
318 #define CSIAR_FLAG 0x80000000
319 #define CSIAR_WRITE_CMD 0x80000000
320 #define CSIAR_BYTE_ENABLE 0x0000f000
321 #define CSIAR_ADDR_MASK 0x00000fff
324 #define EPHYAR_FLAG 0x80000000
325 #define EPHYAR_WRITE_CMD 0x80000000
326 #define EPHYAR_REG_MASK 0x1f
327 #define EPHYAR_REG_SHIFT 16
328 #define EPHYAR_DATA_MASK 0xffff
330 #define PFM_EN (1 << 6)
331 #define TX_10M_PS_EN (1 << 7)
333 #define FIX_NAK_1 (1 << 4)
334 #define FIX_NAK_2 (1 << 3)
337 #define NOW_IS_OOB (1 << 7)
338 #define TX_EMPTY (1 << 5)
339 #define RX_EMPTY (1 << 4)
340 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
341 #define EN_NDP (1 << 3)
342 #define EN_OOB_RESET (1 << 2)
343 #define LINK_LIST_RDY (1 << 1)
345 #define EFUSEAR_FLAG 0x80000000
346 #define EFUSEAR_WRITE_CMD 0x80000000
347 #define EFUSEAR_READ_CMD 0x00000000
348 #define EFUSEAR_REG_MASK 0x03ff
349 #define EFUSEAR_REG_SHIFT 8
350 #define EFUSEAR_DATA_MASK 0xff
352 #define PFM_D3COLD_EN (1 << 6)
355 enum rtl8168_registers {
360 #define ERIAR_FLAG 0x80000000
361 #define ERIAR_WRITE_CMD 0x80000000
362 #define ERIAR_READ_CMD 0x00000000
363 #define ERIAR_ADDR_BYTE_ALIGN 4
364 #define ERIAR_TYPE_SHIFT 16
365 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
366 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
367 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
368 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
369 #define ERIAR_MASK_SHIFT 12
370 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
371 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
372 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
373 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
374 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
375 EPHY_RXER_NUM = 0x7c,
376 OCPDR = 0xb0, /* OCP GPHY access */
377 #define OCPDR_WRITE_CMD 0x80000000
378 #define OCPDR_READ_CMD 0x00000000
379 #define OCPDR_REG_MASK 0x7f
380 #define OCPDR_GPHY_REG_SHIFT 16
381 #define OCPDR_DATA_MASK 0xffff
383 #define OCPAR_FLAG 0x80000000
384 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
385 #define OCPAR_GPHY_READ_CMD 0x0000f060
387 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
388 MISC = 0xf0, /* 8168e only. */
389 #define TXPLA_RST (1 << 29)
390 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
391 #define PWM_EN (1 << 22)
392 #define RXDV_GATED_EN (1 << 19)
393 #define EARLY_TALLY_EN (1 << 16)
396 enum rtl8125_registers {
397 IntrMask_8125 = 0x38,
398 IntrStatus_8125 = 0x3c,
403 #define RX_VLAN_INNER_8125 BIT(22)
404 #define RX_VLAN_OUTER_8125 BIT(23)
405 #define RX_VLAN_8125 (RX_VLAN_INNER_8125 | RX_VLAN_OUTER_8125)
407 #define RX_FETCH_DFLT_8125 (8 << 27)
409 enum rtl_register_content {
410 /* InterruptStatusBits */
414 TxDescUnavail = 0x0080,
436 /* TXPoll register p.5 */
437 HPQ = 0x80, /* Poll cmd on the high prio queue */
438 NPQ = 0x40, /* Poll cmd on the low prio queue */
439 FSWInt = 0x01, /* Forced software interrupt */
443 Cfg9346_Unlock = 0xc0,
448 AcceptBroadcast = 0x08,
449 AcceptMulticast = 0x04,
451 AcceptAllPhys = 0x01,
452 #define RX_CONFIG_ACCEPT_MASK 0x3f
455 TxInterFrameGapShift = 24,
456 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
458 /* Config1 register p.24 */
461 Speed_down = (1 << 4),
465 PMEnable = (1 << 0), /* Power Management Enable */
467 /* Config2 register p. 25 */
468 ClkReqEn = (1 << 7), /* Clock Request Enable */
469 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
470 PCI_Clock_66MHz = 0x01,
471 PCI_Clock_33MHz = 0x00,
473 /* Config3 register p.25 */
474 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
475 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
476 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
477 Rdy_to_L23 = (1 << 1), /* L23 Enable */
478 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
480 /* Config4 register */
481 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
483 /* Config5 register p.27 */
484 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
485 MWF = (1 << 5), /* Accept Multicast wakeup frame */
486 UWF = (1 << 4), /* Accept Unicast wakeup frame */
488 LanWake = (1 << 1), /* LanWake enable/disable */
489 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
490 ASPM_en = (1 << 0), /* ASPM enable */
493 EnableBist = (1 << 15), // 8168 8101
494 Mac_dbgo_oe = (1 << 14), // 8168 8101
495 Normal_mode = (1 << 13), // unused
496 Force_half_dup = (1 << 12), // 8168 8101
497 Force_rxflow_en = (1 << 11), // 8168 8101
498 Force_txflow_en = (1 << 10), // 8168 8101
499 Cxpl_dbg_sel = (1 << 9), // 8168 8101
500 ASF = (1 << 8), // 8168 8101
501 PktCntrDisable = (1 << 7), // 8168 8101
502 Mac_dbgo_sel = 0x001c, // 8168
507 #define INTT_MASK GENMASK(1, 0)
508 #define CPCMD_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
510 /* rtl8169_PHYstatus */
520 /* ResetCounterCommand */
523 /* DumpCounterCommand */
526 /* magic enable v2 */
527 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
531 /* First doubleword. */
532 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
533 RingEnd = (1 << 30), /* End of descriptor ring */
534 FirstFrag = (1 << 29), /* First segment of a packet */
535 LastFrag = (1 << 28), /* Final segment of a packet */
539 enum rtl_tx_desc_bit {
540 /* First doubleword. */
541 TD_LSO = (1 << 27), /* Large Send Offload */
542 #define TD_MSS_MAX 0x07ffu /* MSS value */
544 /* Second doubleword. */
545 TxVlanTag = (1 << 17), /* Add VLAN tag */
548 /* 8169, 8168b and 810x except 8102e. */
549 enum rtl_tx_desc_bit_0 {
550 /* First doubleword. */
551 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
552 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
553 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
554 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
557 /* 8102e, 8168c and beyond. */
558 enum rtl_tx_desc_bit_1 {
559 /* First doubleword. */
560 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
561 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
562 #define GTTCPHO_SHIFT 18
563 #define GTTCPHO_MAX 0x7f
565 /* Second doubleword. */
566 #define TCPHO_SHIFT 18
567 #define TCPHO_MAX 0x3ff
568 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
569 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
570 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
571 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
572 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
575 enum rtl_rx_desc_bit {
577 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
578 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
580 #define RxProtoUDP (PID1)
581 #define RxProtoTCP (PID0)
582 #define RxProtoIP (PID1 | PID0)
583 #define RxProtoMask RxProtoIP
585 IPFail = (1 << 16), /* IP checksum failed */
586 UDPFail = (1 << 15), /* UDP/IP checksum failed */
587 TCPFail = (1 << 14), /* TCP/IP checksum failed */
588 RxVlanTag = (1 << 16), /* VLAN tag available */
591 #define RsvdMask 0x3fffc000
593 #define RTL_GSO_MAX_SIZE_V1 32000
594 #define RTL_GSO_MAX_SEGS_V1 24
595 #define RTL_GSO_MAX_SIZE_V2 64000
596 #define RTL_GSO_MAX_SEGS_V2 64
615 struct rtl8169_counters {
622 __le32 tx_one_collision;
623 __le32 tx_multi_collision;
631 struct rtl8169_tc_offsets {
634 __le32 tx_multi_collision;
639 RTL_FLAG_TASK_ENABLED = 0,
640 RTL_FLAG_TASK_RESET_PENDING,
644 struct rtl8169_stats {
647 struct u64_stats_sync syncp;
650 struct rtl8169_private {
651 void __iomem *mmio_addr; /* memory map physical address */
652 struct pci_dev *pci_dev;
653 struct net_device *dev;
654 struct phy_device *phydev;
655 struct napi_struct napi;
657 enum mac_version mac_version;
658 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
659 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
661 struct rtl8169_stats rx_stats;
662 struct rtl8169_stats tx_stats;
663 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
664 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
665 dma_addr_t TxPhyAddr;
666 dma_addr_t RxPhyAddr;
667 struct page *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
668 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
674 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
676 struct work_struct work;
679 unsigned irq_enabled:1;
680 unsigned supports_gmii:1;
681 unsigned aspm_manageable:1;
682 dma_addr_t counters_phys_addr;
683 struct rtl8169_counters *counters;
684 struct rtl8169_tc_offsets tc_offset;
689 struct rtl_fw *rtl_fw;
694 typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
696 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
697 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
698 module_param_named(debug, debug.msg_enable, int, 0);
699 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
700 MODULE_SOFTDEP("pre: realtek");
701 MODULE_LICENSE("GPL");
702 MODULE_FIRMWARE(FIRMWARE_8168D_1);
703 MODULE_FIRMWARE(FIRMWARE_8168D_2);
704 MODULE_FIRMWARE(FIRMWARE_8168E_1);
705 MODULE_FIRMWARE(FIRMWARE_8168E_2);
706 MODULE_FIRMWARE(FIRMWARE_8168E_3);
707 MODULE_FIRMWARE(FIRMWARE_8105E_1);
708 MODULE_FIRMWARE(FIRMWARE_8168F_1);
709 MODULE_FIRMWARE(FIRMWARE_8168F_2);
710 MODULE_FIRMWARE(FIRMWARE_8402_1);
711 MODULE_FIRMWARE(FIRMWARE_8411_1);
712 MODULE_FIRMWARE(FIRMWARE_8411_2);
713 MODULE_FIRMWARE(FIRMWARE_8106E_1);
714 MODULE_FIRMWARE(FIRMWARE_8106E_2);
715 MODULE_FIRMWARE(FIRMWARE_8168G_2);
716 MODULE_FIRMWARE(FIRMWARE_8168G_3);
717 MODULE_FIRMWARE(FIRMWARE_8168H_1);
718 MODULE_FIRMWARE(FIRMWARE_8168H_2);
719 MODULE_FIRMWARE(FIRMWARE_8168FP_3);
720 MODULE_FIRMWARE(FIRMWARE_8107E_1);
721 MODULE_FIRMWARE(FIRMWARE_8107E_2);
722 MODULE_FIRMWARE(FIRMWARE_8125A_3);
724 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
726 return &tp->pci_dev->dev;
729 static void rtl_lock_work(struct rtl8169_private *tp)
731 mutex_lock(&tp->wk.mutex);
734 static void rtl_unlock_work(struct rtl8169_private *tp)
736 mutex_unlock(&tp->wk.mutex);
739 static void rtl_lock_config_regs(struct rtl8169_private *tp)
741 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
744 static void rtl_unlock_config_regs(struct rtl8169_private *tp)
746 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
749 static bool rtl_is_8125(struct rtl8169_private *tp)
751 return tp->mac_version >= RTL_GIGA_MAC_VER_60;
754 static bool rtl_is_8168evl_up(struct rtl8169_private *tp)
756 return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
757 tp->mac_version != RTL_GIGA_MAC_VER_39 &&
758 tp->mac_version <= RTL_GIGA_MAC_VER_52;
761 static bool rtl_supports_eee(struct rtl8169_private *tp)
763 return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
764 tp->mac_version != RTL_GIGA_MAC_VER_37 &&
765 tp->mac_version != RTL_GIGA_MAC_VER_39;
768 static void rtl_read_mac_from_reg(struct rtl8169_private *tp, u8 *mac, int reg)
772 for (i = 0; i < ETH_ALEN; i++)
773 mac[i] = RTL_R8(tp, reg + i);
777 bool (*check)(struct rtl8169_private *);
781 static void rtl_udelay(unsigned int d)
786 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
787 void (*delay)(unsigned int), unsigned int d, int n,
792 for (i = 0; i < n; i++) {
793 if (c->check(tp) == high)
797 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
798 c->msg, !high, n, d);
802 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
803 const struct rtl_cond *c,
804 unsigned int d, int n)
806 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
809 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
810 const struct rtl_cond *c,
811 unsigned int d, int n)
813 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
816 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
817 const struct rtl_cond *c,
818 unsigned int d, int n)
820 return rtl_loop_wait(tp, c, msleep, d, n, true);
823 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
824 const struct rtl_cond *c,
825 unsigned int d, int n)
827 return rtl_loop_wait(tp, c, msleep, d, n, false);
830 #define DECLARE_RTL_COND(name) \
831 static bool name ## _check(struct rtl8169_private *); \
833 static const struct rtl_cond name = { \
834 .check = name ## _check, \
838 static bool name ## _check(struct rtl8169_private *tp)
840 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
842 if (reg & 0xffff0001) {
843 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
849 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
851 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
854 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
856 if (rtl_ocp_reg_failure(tp, reg))
859 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
861 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
864 static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
866 if (rtl_ocp_reg_failure(tp, reg))
869 RTL_W32(tp, GPHY_OCP, reg << 15);
871 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
872 (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
875 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
877 if (rtl_ocp_reg_failure(tp, reg))
880 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
883 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
885 if (rtl_ocp_reg_failure(tp, reg))
888 RTL_W32(tp, OCPDR, reg << 15);
890 return RTL_R32(tp, OCPDR);
893 static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask,
896 u16 data = r8168_mac_ocp_read(tp, reg);
898 r8168_mac_ocp_write(tp, reg, (data & ~mask) | set);
901 #define OCP_STD_PHY_BASE 0xa400
903 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
906 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
910 if (tp->ocp_base != OCP_STD_PHY_BASE)
913 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
916 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
919 return tp->ocp_base == OCP_STD_PHY_BASE ? 0 : tp->ocp_base >> 4;
921 if (tp->ocp_base != OCP_STD_PHY_BASE)
924 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
927 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
930 tp->ocp_base = value << 4;
934 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
937 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
939 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
942 DECLARE_RTL_COND(rtl_phyar_cond)
944 return RTL_R32(tp, PHYAR) & 0x80000000;
947 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
949 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
951 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
953 * According to hardware specs a 20us delay is required after write
954 * complete indication, but before sending next command.
959 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
963 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
965 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
966 RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
969 * According to hardware specs a 20us delay is required after read
970 * complete indication, but before sending next command.
977 DECLARE_RTL_COND(rtl_ocpar_cond)
979 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
982 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
984 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
985 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
986 RTL_W32(tp, EPHY_RXER_NUM, 0);
988 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
991 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
993 r8168dp_1_mdio_access(tp, reg,
994 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
997 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
999 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1002 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1003 RTL_W32(tp, EPHY_RXER_NUM, 0);
1005 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1006 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : -ETIMEDOUT;
1009 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1011 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
1013 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1016 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
1018 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1021 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1023 r8168dp_2_mdio_start(tp);
1025 r8169_mdio_write(tp, reg, value);
1027 r8168dp_2_mdio_stop(tp);
1030 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1034 /* Work around issue with chip reporting wrong PHY ID */
1035 if (reg == MII_PHYSID2)
1038 r8168dp_2_mdio_start(tp);
1040 value = r8169_mdio_read(tp, reg);
1042 r8168dp_2_mdio_stop(tp);
1047 static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
1049 switch (tp->mac_version) {
1050 case RTL_GIGA_MAC_VER_27:
1051 r8168dp_1_mdio_write(tp, location, val);
1053 case RTL_GIGA_MAC_VER_28:
1054 case RTL_GIGA_MAC_VER_31:
1055 r8168dp_2_mdio_write(tp, location, val);
1057 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_61:
1058 r8168g_mdio_write(tp, location, val);
1061 r8169_mdio_write(tp, location, val);
1066 static int rtl_readphy(struct rtl8169_private *tp, int location)
1068 switch (tp->mac_version) {
1069 case RTL_GIGA_MAC_VER_27:
1070 return r8168dp_1_mdio_read(tp, location);
1071 case RTL_GIGA_MAC_VER_28:
1072 case RTL_GIGA_MAC_VER_31:
1073 return r8168dp_2_mdio_read(tp, location);
1074 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_61:
1075 return r8168g_mdio_read(tp, location);
1077 return r8169_mdio_read(tp, location);
1081 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1083 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1086 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1090 val = rtl_readphy(tp, reg_addr);
1091 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1094 static void r8168d_modify_extpage(struct phy_device *phydev, int extpage,
1095 int reg, u16 mask, u16 val)
1097 int oldpage = phy_select_page(phydev, 0x0007);
1099 __phy_write(phydev, 0x1e, extpage);
1100 __phy_modify(phydev, reg, mask, val);
1102 phy_restore_page(phydev, oldpage, 0);
1105 static void r8168d_phy_param(struct phy_device *phydev, u16 parm,
1108 int oldpage = phy_select_page(phydev, 0x0005);
1110 __phy_write(phydev, 0x05, parm);
1111 __phy_modify(phydev, 0x06, mask, val);
1113 phy_restore_page(phydev, oldpage, 0);
1116 static void r8168g_phy_param(struct phy_device *phydev, u16 parm,
1119 int oldpage = phy_select_page(phydev, 0x0a43);
1121 __phy_write(phydev, 0x13, parm);
1122 __phy_modify(phydev, 0x14, mask, val);
1124 phy_restore_page(phydev, oldpage, 0);
1127 DECLARE_RTL_COND(rtl_ephyar_cond)
1129 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1132 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1134 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1135 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1137 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1142 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1144 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1146 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1147 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1150 DECLARE_RTL_COND(rtl_eriar_cond)
1152 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
1155 static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1158 BUG_ON((addr & 3) || (mask == 0));
1159 RTL_W32(tp, ERIDR, val);
1160 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1162 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1165 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1168 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
1171 static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1173 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1175 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1176 RTL_R32(tp, ERIDR) : ~0;
1179 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
1181 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
1184 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1189 val = rtl_eri_read(tp, addr);
1190 rtl_eri_write(tp, addr, mask, (val & ~m) | p);
1193 static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 mask,
1196 rtl_w0w1_eri(tp, addr, mask, p, 0);
1199 static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 mask,
1202 rtl_w0w1_eri(tp, addr, mask, 0, m);
1205 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1207 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1208 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1209 RTL_R32(tp, OCPDR) : ~0;
1212 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1214 return _rtl_eri_read(tp, reg, ERIAR_OOB);
1217 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1220 RTL_W32(tp, OCPDR, data);
1221 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1222 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1225 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1228 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1232 static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
1234 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
1236 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
1239 #define OOB_CMD_RESET 0x00
1240 #define OOB_CMD_DRIVER_START 0x05
1241 #define OOB_CMD_DRIVER_STOP 0x06
1243 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1245 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1248 DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
1252 reg = rtl8168_get_ocp_reg(tp);
1254 return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
1257 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1259 return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1262 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1264 return RTL_R8(tp, IBISR0) & 0x20;
1267 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1269 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1270 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1271 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1272 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1275 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1277 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1278 rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
1281 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1283 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1284 r8168ep_ocp_write(tp, 0x01, 0x30,
1285 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
1286 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1289 static void rtl8168_driver_start(struct rtl8169_private *tp)
1291 switch (tp->mac_version) {
1292 case RTL_GIGA_MAC_VER_27:
1293 case RTL_GIGA_MAC_VER_28:
1294 case RTL_GIGA_MAC_VER_31:
1295 rtl8168dp_driver_start(tp);
1297 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_52:
1298 rtl8168ep_driver_start(tp);
1306 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1308 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1309 rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
1312 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1314 rtl8168ep_stop_cmac(tp);
1315 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1316 r8168ep_ocp_write(tp, 0x01, 0x30,
1317 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
1318 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1321 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1323 switch (tp->mac_version) {
1324 case RTL_GIGA_MAC_VER_27:
1325 case RTL_GIGA_MAC_VER_28:
1326 case RTL_GIGA_MAC_VER_31:
1327 rtl8168dp_driver_stop(tp);
1329 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_52:
1330 rtl8168ep_driver_stop(tp);
1338 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1340 u16 reg = rtl8168_get_ocp_reg(tp);
1342 return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
1345 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1347 return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
1350 static bool r8168_check_dash(struct rtl8169_private *tp)
1352 switch (tp->mac_version) {
1353 case RTL_GIGA_MAC_VER_27:
1354 case RTL_GIGA_MAC_VER_28:
1355 case RTL_GIGA_MAC_VER_31:
1356 return r8168dp_check_dash(tp);
1357 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_52:
1358 return r8168ep_check_dash(tp);
1364 static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1366 rtl_eri_clear_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1367 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1370 DECLARE_RTL_COND(rtl_efusear_cond)
1372 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1375 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1377 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1379 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1380 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1383 static u32 rtl_get_events(struct rtl8169_private *tp)
1385 if (rtl_is_8125(tp))
1386 return RTL_R32(tp, IntrStatus_8125);
1388 return RTL_R16(tp, IntrStatus);
1391 static void rtl_ack_events(struct rtl8169_private *tp, u32 bits)
1393 if (rtl_is_8125(tp))
1394 RTL_W32(tp, IntrStatus_8125, bits);
1396 RTL_W16(tp, IntrStatus, bits);
1399 static void rtl_irq_disable(struct rtl8169_private *tp)
1401 if (rtl_is_8125(tp))
1402 RTL_W32(tp, IntrMask_8125, 0);
1404 RTL_W16(tp, IntrMask, 0);
1405 tp->irq_enabled = 0;
1408 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1409 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1410 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1412 static void rtl_irq_enable(struct rtl8169_private *tp)
1414 tp->irq_enabled = 1;
1415 if (rtl_is_8125(tp))
1416 RTL_W32(tp, IntrMask_8125, tp->irq_mask);
1418 RTL_W16(tp, IntrMask, tp->irq_mask);
1421 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1423 rtl_irq_disable(tp);
1424 rtl_ack_events(tp, 0xffffffff);
1426 RTL_R8(tp, ChipCmd);
1429 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1431 struct net_device *dev = tp->dev;
1432 struct phy_device *phydev = tp->phydev;
1434 if (!netif_running(dev))
1437 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1438 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1439 if (phydev->speed == SPEED_1000) {
1440 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1441 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1442 } else if (phydev->speed == SPEED_100) {
1443 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1444 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1446 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1447 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
1449 rtl_reset_packet_filter(tp);
1450 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1451 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1452 if (phydev->speed == SPEED_1000) {
1453 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1454 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1456 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1457 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
1459 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1460 if (phydev->speed == SPEED_10) {
1461 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1462 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
1464 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
1469 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1471 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1473 struct rtl8169_private *tp = netdev_priv(dev);
1476 wol->supported = WAKE_ANY;
1477 wol->wolopts = tp->saved_wolopts;
1478 rtl_unlock_work(tp);
1481 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1483 static const struct {
1488 { WAKE_PHY, Config3, LinkUp },
1489 { WAKE_UCAST, Config5, UWF },
1490 { WAKE_BCAST, Config5, BWF },
1491 { WAKE_MCAST, Config5, MWF },
1492 { WAKE_ANY, Config5, LanWake },
1493 { WAKE_MAGIC, Config3, MagicPacket }
1495 unsigned int i, tmp = ARRAY_SIZE(cfg);
1498 rtl_unlock_config_regs(tp);
1500 if (rtl_is_8168evl_up(tp)) {
1502 if (wolopts & WAKE_MAGIC)
1503 rtl_eri_set_bits(tp, 0x0dc, ERIAR_MASK_0100,
1506 rtl_eri_clear_bits(tp, 0x0dc, ERIAR_MASK_0100,
1508 } else if (rtl_is_8125(tp)) {
1510 if (wolopts & WAKE_MAGIC)
1511 r8168_mac_ocp_modify(tp, 0xc0b6, 0, BIT(0));
1513 r8168_mac_ocp_modify(tp, 0xc0b6, BIT(0), 0);
1516 for (i = 0; i < tmp; i++) {
1517 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1518 if (wolopts & cfg[i].opt)
1519 options |= cfg[i].mask;
1520 RTL_W8(tp, cfg[i].reg, options);
1523 switch (tp->mac_version) {
1524 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
1525 options = RTL_R8(tp, Config1) & ~PMEnable;
1527 options |= PMEnable;
1528 RTL_W8(tp, Config1, options);
1530 case RTL_GIGA_MAC_VER_34:
1531 case RTL_GIGA_MAC_VER_37:
1532 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_52:
1533 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
1535 options |= PME_SIGNAL;
1536 RTL_W8(tp, Config2, options);
1542 rtl_lock_config_regs(tp);
1544 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
1547 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1549 struct rtl8169_private *tp = netdev_priv(dev);
1550 struct device *d = tp_to_dev(tp);
1552 if (wol->wolopts & ~WAKE_ANY)
1555 pm_runtime_get_noresume(d);
1559 tp->saved_wolopts = wol->wolopts;
1561 if (pm_runtime_active(d))
1562 __rtl8169_set_wol(tp, tp->saved_wolopts);
1564 rtl_unlock_work(tp);
1566 pm_runtime_put_noidle(d);
1571 static void rtl8169_get_drvinfo(struct net_device *dev,
1572 struct ethtool_drvinfo *info)
1574 struct rtl8169_private *tp = netdev_priv(dev);
1575 struct rtl_fw *rtl_fw = tp->rtl_fw;
1577 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1578 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1579 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1581 strlcpy(info->fw_version, rtl_fw->version,
1582 sizeof(info->fw_version));
1585 static int rtl8169_get_regs_len(struct net_device *dev)
1587 return R8169_REGS_SIZE;
1590 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1591 netdev_features_t features)
1593 struct rtl8169_private *tp = netdev_priv(dev);
1595 if (dev->mtu > TD_MSS_MAX)
1596 features &= ~NETIF_F_ALL_TSO;
1598 if (dev->mtu > JUMBO_1K &&
1599 tp->mac_version > RTL_GIGA_MAC_VER_06)
1600 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_ALL_TSO);
1605 static int rtl8169_set_features(struct net_device *dev,
1606 netdev_features_t features)
1608 struct rtl8169_private *tp = netdev_priv(dev);
1613 rx_config = RTL_R32(tp, RxConfig);
1614 if (features & NETIF_F_RXALL)
1615 rx_config |= (AcceptErr | AcceptRunt);
1617 rx_config &= ~(AcceptErr | AcceptRunt);
1619 if (rtl_is_8125(tp)) {
1620 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1621 rx_config |= RX_VLAN_8125;
1623 rx_config &= ~RX_VLAN_8125;
1626 RTL_W32(tp, RxConfig, rx_config);
1628 if (features & NETIF_F_RXCSUM)
1629 tp->cp_cmd |= RxChkSum;
1631 tp->cp_cmd &= ~RxChkSum;
1633 if (!rtl_is_8125(tp)) {
1634 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1635 tp->cp_cmd |= RxVlan;
1637 tp->cp_cmd &= ~RxVlan;
1640 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1641 RTL_R16(tp, CPlusCmd);
1643 rtl_unlock_work(tp);
1648 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1650 return (skb_vlan_tag_present(skb)) ?
1651 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1654 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1656 u32 opts2 = le32_to_cpu(desc->opts2);
1658 if (opts2 & RxVlanTag)
1659 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1662 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1665 struct rtl8169_private *tp = netdev_priv(dev);
1666 u32 __iomem *data = tp->mmio_addr;
1671 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1672 memcpy_fromio(dw++, data++, 4);
1673 rtl_unlock_work(tp);
1676 static u32 rtl8169_get_msglevel(struct net_device *dev)
1678 struct rtl8169_private *tp = netdev_priv(dev);
1680 return tp->msg_enable;
1683 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1685 struct rtl8169_private *tp = netdev_priv(dev);
1687 tp->msg_enable = value;
1690 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1697 "tx_single_collisions",
1698 "tx_multi_collisions",
1706 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1710 return ARRAY_SIZE(rtl8169_gstrings);
1716 DECLARE_RTL_COND(rtl_counters_cond)
1718 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
1721 static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
1723 dma_addr_t paddr = tp->counters_phys_addr;
1726 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1727 RTL_R32(tp, CounterAddrHigh);
1728 cmd = (u64)paddr & DMA_BIT_MASK(32);
1729 RTL_W32(tp, CounterAddrLow, cmd);
1730 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
1732 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
1735 static bool rtl8169_reset_counters(struct rtl8169_private *tp)
1738 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1741 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1744 return rtl8169_do_counters(tp, CounterReset);
1747 static bool rtl8169_update_counters(struct rtl8169_private *tp)
1749 u8 val = RTL_R8(tp, ChipCmd);
1752 * Some chips are unable to dump tally counters when the receiver
1753 * is disabled. If 0xff chip may be in a PCI power-save state.
1755 if (!(val & CmdRxEnb) || val == 0xff)
1758 return rtl8169_do_counters(tp, CounterDump);
1761 static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
1763 struct rtl8169_counters *counters = tp->counters;
1767 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1768 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1769 * reset by a power cycle, while the counter values collected by the
1770 * driver are reset at every driver unload/load cycle.
1772 * To make sure the HW values returned by @get_stats64 match the SW
1773 * values, we collect the initial values at first open(*) and use them
1774 * as offsets to normalize the values returned by @get_stats64.
1776 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1777 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1778 * set at open time by rtl_hw_start.
1781 if (tp->tc_offset.inited)
1784 /* If both, reset and update fail, propagate to caller. */
1785 if (rtl8169_reset_counters(tp))
1788 if (rtl8169_update_counters(tp))
1791 tp->tc_offset.tx_errors = counters->tx_errors;
1792 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1793 tp->tc_offset.tx_aborted = counters->tx_aborted;
1794 tp->tc_offset.inited = true;
1799 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1800 struct ethtool_stats *stats, u64 *data)
1802 struct rtl8169_private *tp = netdev_priv(dev);
1803 struct device *d = tp_to_dev(tp);
1804 struct rtl8169_counters *counters = tp->counters;
1808 pm_runtime_get_noresume(d);
1810 if (pm_runtime_active(d))
1811 rtl8169_update_counters(tp);
1813 pm_runtime_put_noidle(d);
1815 data[0] = le64_to_cpu(counters->tx_packets);
1816 data[1] = le64_to_cpu(counters->rx_packets);
1817 data[2] = le64_to_cpu(counters->tx_errors);
1818 data[3] = le32_to_cpu(counters->rx_errors);
1819 data[4] = le16_to_cpu(counters->rx_missed);
1820 data[5] = le16_to_cpu(counters->align_errors);
1821 data[6] = le32_to_cpu(counters->tx_one_collision);
1822 data[7] = le32_to_cpu(counters->tx_multi_collision);
1823 data[8] = le64_to_cpu(counters->rx_unicast);
1824 data[9] = le64_to_cpu(counters->rx_broadcast);
1825 data[10] = le32_to_cpu(counters->rx_multicast);
1826 data[11] = le16_to_cpu(counters->tx_aborted);
1827 data[12] = le16_to_cpu(counters->tx_underun);
1830 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1834 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1840 * Interrupt coalescing
1842 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1843 * > 8169, 8168 and 810x line of chipsets
1845 * 8169, 8168, and 8136(810x) serial chipsets support it.
1847 * > 2 - the Tx timer unit at gigabit speed
1849 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1850 * (0xe0) bit 1 and bit 0.
1853 * bit[1:0] \ speed 1000M 100M 10M
1854 * 0 0 320ns 2.56us 40.96us
1855 * 0 1 2.56us 20.48us 327.7us
1856 * 1 0 5.12us 40.96us 655.4us
1857 * 1 1 10.24us 81.92us 1.31ms
1860 * bit[1:0] \ speed 1000M 100M 10M
1861 * 0 0 5us 2.56us 40.96us
1862 * 0 1 40us 20.48us 327.7us
1863 * 1 0 80us 40.96us 655.4us
1864 * 1 1 160us 81.92us 1.31ms
1867 /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1868 struct rtl_coalesce_scale {
1873 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
1874 struct rtl_coalesce_info {
1876 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1879 /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1880 #define rxtx_x1822(r, t) { \
1883 {{(r)*8*2, (t)*8*2}}, \
1884 {{(r)*8*2*2, (t)*8*2*2}}, \
1886 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1887 /* speed delays: rx00 tx00 */
1888 { SPEED_10, rxtx_x1822(40960, 40960) },
1889 { SPEED_100, rxtx_x1822( 2560, 2560) },
1890 { SPEED_1000, rxtx_x1822( 320, 320) },
1894 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1895 /* speed delays: rx00 tx00 */
1896 { SPEED_10, rxtx_x1822(40960, 40960) },
1897 { SPEED_100, rxtx_x1822( 2560, 2560) },
1898 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1903 /* get rx/tx scale vector corresponding to current speed */
1904 static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1906 struct rtl8169_private *tp = netdev_priv(dev);
1907 const struct rtl_coalesce_info *ci;
1909 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1910 ci = rtl_coalesce_info_8169;
1912 ci = rtl_coalesce_info_8168_8136;
1914 for (; ci->speed; ci++) {
1915 if (tp->phydev->speed == ci->speed)
1919 return ERR_PTR(-ELNRNG);
1922 static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1924 struct rtl8169_private *tp = netdev_priv(dev);
1925 const struct rtl_coalesce_info *ci;
1926 const struct rtl_coalesce_scale *scale;
1930 } coal_settings [] = {
1931 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1932 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1933 }, *p = coal_settings;
1937 if (rtl_is_8125(tp))
1940 memset(ec, 0, sizeof(*ec));
1942 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1943 ci = rtl_coalesce_info(dev);
1947 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
1949 /* read IntrMitigate and adjust according to scale */
1950 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
1951 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1952 w >>= RTL_COALESCE_SHIFT;
1953 *p->usecs = w & RTL_COALESCE_MASK;
1956 for (i = 0; i < 2; i++) {
1957 p = coal_settings + i;
1958 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1961 * ethtool_coalesce says it is illegal to set both usecs and
1964 if (!*p->usecs && !*p->max_frames)
1971 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1972 static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1973 struct net_device *dev, u32 nsec, u16 *cp01)
1975 const struct rtl_coalesce_info *ci;
1978 ci = rtl_coalesce_info(dev);
1980 return ERR_CAST(ci);
1982 for (i = 0; i < 4; i++) {
1983 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1984 ci->scalev[i].nsecs[1]);
1985 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1987 return &ci->scalev[i];
1991 return ERR_PTR(-EINVAL);
1994 static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1996 struct rtl8169_private *tp = netdev_priv(dev);
1997 const struct rtl_coalesce_scale *scale;
2001 } coal_settings [] = {
2002 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2003 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2004 }, *p = coal_settings;
2008 if (rtl_is_8125(tp))
2011 scale = rtl_coalesce_choose_scale(dev,
2012 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2014 return PTR_ERR(scale);
2016 for (i = 0; i < 2; i++, p++) {
2020 * accept max_frames=1 we returned in rtl_get_coalesce.
2021 * accept it not only when usecs=0 because of e.g. the following scenario:
2023 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2024 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2025 * - then user does `ethtool -C eth0 rx-usecs 100`
2027 * since ethtool sends to kernel whole ethtool_coalesce
2028 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2029 * we'll reject it below in `frames % 4 != 0`.
2031 if (p->frames == 1) {
2035 units = p->usecs * 1000 / scale->nsecs[i];
2036 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2039 w <<= RTL_COALESCE_SHIFT;
2041 w <<= RTL_COALESCE_SHIFT;
2042 w |= p->frames >> 2;
2047 RTL_W16(tp, IntrMitigate, swab16(w));
2049 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
2050 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2051 RTL_R16(tp, CPlusCmd);
2053 rtl_unlock_work(tp);
2058 static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
2060 struct rtl8169_private *tp = netdev_priv(dev);
2061 struct device *d = tp_to_dev(tp);
2064 if (!rtl_supports_eee(tp))
2067 pm_runtime_get_noresume(d);
2069 if (!pm_runtime_active(d)) {
2072 ret = phy_ethtool_get_eee(tp->phydev, data);
2075 pm_runtime_put_noidle(d);
2080 static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
2082 struct rtl8169_private *tp = netdev_priv(dev);
2083 struct device *d = tp_to_dev(tp);
2086 if (!rtl_supports_eee(tp))
2089 pm_runtime_get_noresume(d);
2091 if (!pm_runtime_active(d)) {
2096 if (dev->phydev->autoneg == AUTONEG_DISABLE ||
2097 dev->phydev->duplex != DUPLEX_FULL) {
2098 ret = -EPROTONOSUPPORT;
2102 ret = phy_ethtool_set_eee(tp->phydev, data);
2105 tp->eee_adv = phy_read_mmd(dev->phydev, MDIO_MMD_AN,
2108 pm_runtime_put_noidle(d);
2112 static const struct ethtool_ops rtl8169_ethtool_ops = {
2113 .get_drvinfo = rtl8169_get_drvinfo,
2114 .get_regs_len = rtl8169_get_regs_len,
2115 .get_link = ethtool_op_get_link,
2116 .get_coalesce = rtl_get_coalesce,
2117 .set_coalesce = rtl_set_coalesce,
2118 .get_msglevel = rtl8169_get_msglevel,
2119 .set_msglevel = rtl8169_set_msglevel,
2120 .get_regs = rtl8169_get_regs,
2121 .get_wol = rtl8169_get_wol,
2122 .set_wol = rtl8169_set_wol,
2123 .get_strings = rtl8169_get_strings,
2124 .get_sset_count = rtl8169_get_sset_count,
2125 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2126 .get_ts_info = ethtool_op_get_ts_info,
2127 .nway_reset = phy_ethtool_nway_reset,
2128 .get_eee = rtl8169_get_eee,
2129 .set_eee = rtl8169_set_eee,
2130 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2131 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2134 static void rtl_enable_eee(struct rtl8169_private *tp)
2136 struct phy_device *phydev = tp->phydev;
2139 /* respect EEE advertisement the user may have set */
2140 if (tp->eee_adv >= 0)
2143 adv = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
2146 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
2149 static void rtl8169_get_mac_version(struct rtl8169_private *tp)
2152 * The driver currently handles the 8168Bf and the 8168Be identically
2153 * but they can be identified more specifically through the test below
2156 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2158 * Same thing for the 8101Eb and the 8101Ec:
2160 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2162 static const struct rtl_mac_info {
2168 { 0x7cf, 0x608, RTL_GIGA_MAC_VER_60 },
2169 { 0x7c8, 0x608, RTL_GIGA_MAC_VER_61 },
2172 { 0x7cf, 0x54a, RTL_GIGA_MAC_VER_52 },
2174 /* 8168EP family. */
2175 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2176 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2177 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
2180 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2181 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
2184 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2185 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2186 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2187 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
2190 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2191 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2192 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
2195 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2196 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2197 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
2200 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2201 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
2203 /* 8168DP family. */
2204 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2205 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2206 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
2209 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2210 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2211 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2212 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2213 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2214 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2215 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
2218 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2219 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2220 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
2223 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2224 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2225 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2226 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2227 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2228 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2229 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2230 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2231 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2232 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2233 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2234 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2235 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2236 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
2237 /* FIXME: where did these entries come from ? -- FR */
2238 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2239 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
2242 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2243 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2244 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2245 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2246 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
2249 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
2251 const struct rtl_mac_info *p = mac_info;
2252 u16 reg = RTL_R32(tp, TxConfig) >> 20;
2254 while ((reg & p->mask) != p->val)
2256 tp->mac_version = p->mac_version;
2258 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2259 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
2260 } else if (!tp->supports_gmii) {
2261 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2262 tp->mac_version = RTL_GIGA_MAC_VER_43;
2263 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2264 tp->mac_version = RTL_GIGA_MAC_VER_47;
2265 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2266 tp->mac_version = RTL_GIGA_MAC_VER_48;
2275 static void __rtl_writephy_batch(struct rtl8169_private *tp,
2276 const struct phy_reg *regs, int len)
2279 rtl_writephy(tp, regs->reg, regs->val);
2284 #define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
2286 static void rtl_release_firmware(struct rtl8169_private *tp)
2289 rtl_fw_release_firmware(tp->rtl_fw);
2295 static void rtl_apply_firmware(struct rtl8169_private *tp)
2297 /* TODO: release firmware if rtl_fw_write_firmware signals failure. */
2299 rtl_fw_write_firmware(tp, tp->rtl_fw);
2302 static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2304 /* Adjust EEE LED frequency */
2305 if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2306 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2308 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_1111, 0x0003);
2311 static void rtl8125_config_eee_mac(struct rtl8169_private *tp)
2313 r8168_mac_ocp_modify(tp, 0xe040, 0, BIT(1) | BIT(0));
2314 r8168_mac_ocp_modify(tp, 0xeb62, 0, BIT(2) | BIT(1));
2317 static void rtl8168f_config_eee_phy(struct rtl8169_private *tp)
2319 struct phy_device *phydev = tp->phydev;
2321 r8168d_modify_extpage(phydev, 0x0020, 0x15, 0, BIT(8));
2322 r8168d_phy_param(phydev, 0x8b85, 0, BIT(13));
2325 static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
2327 phy_modify_paged(tp->phydev, 0x0a43, 0x11, 0, BIT(4));
2330 static void rtl8168h_config_eee_phy(struct rtl8169_private *tp)
2332 struct phy_device *phydev = tp->phydev;
2334 rtl8168g_config_eee_phy(tp);
2336 phy_modify_paged(phydev, 0xa4a, 0x11, 0x0000, 0x0200);
2337 phy_modify_paged(phydev, 0xa42, 0x14, 0x0000, 0x0080);
2340 static void rtl8125_config_eee_phy(struct rtl8169_private *tp)
2342 struct phy_device *phydev = tp->phydev;
2344 rtl8168h_config_eee_phy(tp);
2346 phy_modify_paged(phydev, 0xa6d, 0x12, 0x0001, 0x0000);
2347 phy_modify_paged(phydev, 0xa6d, 0x14, 0x0010, 0x0000);
2350 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2352 static const struct phy_reg phy_reg_init[] = {
2414 rtl_writephy_batch(tp, phy_reg_init);
2417 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2419 phy_write_paged(tp->phydev, 0x0002, 0x01, 0x90d0);
2422 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2424 struct pci_dev *pdev = tp->pci_dev;
2426 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2427 (pdev->subsystem_device != 0xe000))
2430 phy_write_paged(tp->phydev, 0x0001, 0x10, 0xf01b);
2433 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2435 static const struct phy_reg phy_reg_init[] = {
2475 rtl_writephy_batch(tp, phy_reg_init);
2477 rtl8169scd_hw_phy_config_quirk(tp);
2480 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2482 static const struct phy_reg phy_reg_init[] = {
2530 rtl_writephy_batch(tp, phy_reg_init);
2533 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2535 rtl_writephy(tp, 0x1f, 0x0001);
2536 rtl_patchphy(tp, 0x16, 1 << 0);
2537 rtl_writephy(tp, 0x10, 0xf41b);
2538 rtl_writephy(tp, 0x1f, 0x0000);
2541 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2543 phy_write_paged(tp->phydev, 0x0001, 0x10, 0xf41b);
2546 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2548 phy_write(tp->phydev, 0x1d, 0x0f00);
2549 phy_write_paged(tp->phydev, 0x0002, 0x0c, 0x1ec8);
2552 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2554 phy_set_bits(tp->phydev, 0x14, BIT(5));
2555 phy_set_bits(tp->phydev, 0x0d, BIT(5));
2556 phy_write_paged(tp->phydev, 0x0001, 0x1d, 0x3d98);
2559 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2561 static const struct phy_reg phy_reg_init[] = {
2581 rtl_writephy_batch(tp, phy_reg_init);
2583 rtl_patchphy(tp, 0x14, 1 << 5);
2584 rtl_patchphy(tp, 0x0d, 1 << 5);
2585 rtl_writephy(tp, 0x1f, 0x0000);
2588 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2590 static const struct phy_reg phy_reg_init[] = {
2608 rtl_writephy_batch(tp, phy_reg_init);
2610 rtl_patchphy(tp, 0x16, 1 << 0);
2611 rtl_patchphy(tp, 0x14, 1 << 5);
2612 rtl_patchphy(tp, 0x0d, 1 << 5);
2613 rtl_writephy(tp, 0x1f, 0x0000);
2616 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2618 static const struct phy_reg phy_reg_init[] = {
2630 rtl_writephy_batch(tp, phy_reg_init);
2632 rtl_patchphy(tp, 0x16, 1 << 0);
2633 rtl_patchphy(tp, 0x14, 1 << 5);
2634 rtl_patchphy(tp, 0x0d, 1 << 5);
2635 rtl_writephy(tp, 0x1f, 0x0000);
2638 static const struct phy_reg rtl8168d_1_phy_reg_init_0[] = {
2639 /* Channel Estimation */
2660 * Enhance line driver power
2669 * Can not link to 1Gbps with bad cable
2670 * Decrease SNR threshold form 21.07dB to 19.04dB
2679 static const struct phy_reg rtl8168d_1_phy_reg_init_1[] = {
2688 static void rtl8168d_apply_firmware_cond(struct rtl8169_private *tp, u16 val)
2692 rtl_writephy(tp, 0x1f, 0x0005);
2693 rtl_writephy(tp, 0x05, 0x001b);
2694 reg_val = rtl_readphy(tp, 0x06);
2695 rtl_writephy(tp, 0x1f, 0x0000);
2698 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2700 rtl_apply_firmware(tp);
2703 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2705 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
2709 * Fine Tune Switching regulator parameter
2711 rtl_writephy(tp, 0x1f, 0x0002);
2712 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2713 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
2715 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2718 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
2720 val = rtl_readphy(tp, 0x0d);
2722 if ((val & 0x00ff) != 0x006c) {
2723 static const u32 set[] = {
2724 0x0065, 0x0066, 0x0067, 0x0068,
2725 0x0069, 0x006a, 0x006b, 0x006c
2729 rtl_writephy(tp, 0x1f, 0x0002);
2732 for (i = 0; i < ARRAY_SIZE(set); i++)
2733 rtl_writephy(tp, 0x0d, val | set[i]);
2736 phy_write_paged(tp->phydev, 0x0002, 0x05, 0x6662);
2737 r8168d_phy_param(tp->phydev, 0x8330, 0xffff, 0x6662);
2740 /* RSET couple improve */
2741 rtl_writephy(tp, 0x1f, 0x0002);
2742 rtl_patchphy(tp, 0x0d, 0x0300);
2743 rtl_patchphy(tp, 0x0f, 0x0010);
2745 /* Fine tune PLL performance */
2746 rtl_writephy(tp, 0x1f, 0x0002);
2747 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2748 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
2749 rtl_writephy(tp, 0x1f, 0x0000);
2751 rtl8168d_apply_firmware_cond(tp, 0xbf00);
2754 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2756 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
2758 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2761 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
2763 val = rtl_readphy(tp, 0x0d);
2764 if ((val & 0x00ff) != 0x006c) {
2765 static const u32 set[] = {
2766 0x0065, 0x0066, 0x0067, 0x0068,
2767 0x0069, 0x006a, 0x006b, 0x006c
2771 rtl_writephy(tp, 0x1f, 0x0002);
2774 for (i = 0; i < ARRAY_SIZE(set); i++)
2775 rtl_writephy(tp, 0x0d, val | set[i]);
2778 phy_write_paged(tp->phydev, 0x0002, 0x05, 0x2642);
2779 r8168d_phy_param(tp->phydev, 0x8330, 0xffff, 0x2642);
2782 /* Fine tune PLL performance */
2783 rtl_writephy(tp, 0x1f, 0x0002);
2784 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2785 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
2787 /* Switching regulator Slew rate */
2788 rtl_writephy(tp, 0x1f, 0x0002);
2789 rtl_patchphy(tp, 0x0f, 0x0017);
2790 rtl_writephy(tp, 0x1f, 0x0000);
2792 rtl8168d_apply_firmware_cond(tp, 0xb300);
2795 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2797 static const struct phy_reg phy_reg_init[] = {
2848 rtl_writephy_batch(tp, phy_reg_init);
2850 r8168d_modify_extpage(tp->phydev, 0x0023, 0x16, 0xffff, 0x0000);
2853 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2855 phy_write_paged(tp->phydev, 0x0001, 0x17, 0x0cc0);
2856 r8168d_modify_extpage(tp->phydev, 0x002d, 0x18, 0xffff, 0x0040);
2857 phy_set_bits(tp->phydev, 0x0d, BIT(5));
2860 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
2862 static const struct phy_reg phy_reg_init[] = {
2863 /* Channel estimation fine tune */
2872 struct phy_device *phydev = tp->phydev;
2874 rtl_apply_firmware(tp);
2876 /* Enable Delay cap */
2877 r8168d_phy_param(phydev, 0x8b80, 0xffff, 0xc896);
2879 rtl_writephy_batch(tp, phy_reg_init);
2881 /* Update PFM & 10M TX idle timer */
2882 r8168d_modify_extpage(phydev, 0x002f, 0x15, 0xffff, 0x1919);
2884 r8168d_modify_extpage(phydev, 0x00ac, 0x18, 0xffff, 0x0006);
2886 /* DCO enable for 10M IDLE Power */
2887 r8168d_modify_extpage(phydev, 0x0023, 0x17, 0x0000, 0x0006);
2889 /* For impedance matching */
2890 phy_modify_paged(phydev, 0x0002, 0x08, 0x7f00, 0x8000);
2892 /* PHY auto speed down */
2893 r8168d_modify_extpage(phydev, 0x002d, 0x18, 0x0000, 0x0050);
2894 phy_set_bits(phydev, 0x14, BIT(15));
2896 r8168d_phy_param(phydev, 0x8b86, 0x0000, 0x0001);
2897 r8168d_phy_param(phydev, 0x8b85, 0x2000, 0x0000);
2899 r8168d_modify_extpage(phydev, 0x0020, 0x15, 0x1100, 0x0000);
2900 phy_write_paged(phydev, 0x0006, 0x00, 0x5a00);
2902 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0000);
2905 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
2908 addr[0] | (addr[1] << 8),
2909 addr[2] | (addr[3] << 8),
2910 addr[4] | (addr[5] << 8)
2913 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, w[0] | (w[1] << 16));
2914 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, w[2]);
2915 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, w[0] << 16);
2916 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, w[1] | (w[2] << 16));
2919 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2921 struct phy_device *phydev = tp->phydev;
2923 rtl_apply_firmware(tp);
2925 /* Enable Delay cap */
2926 r8168d_modify_extpage(phydev, 0x00ac, 0x18, 0xffff, 0x0006);
2928 /* Channel estimation fine tune */
2929 phy_write_paged(phydev, 0x0003, 0x09, 0xa20f);
2932 r8168d_phy_param(phydev, 0x8b5b, 0xffff, 0x9222);
2933 r8168d_phy_param(phydev, 0x8b6d, 0xffff, 0x8000);
2934 r8168d_phy_param(phydev, 0x8b76, 0xffff, 0x8000);
2936 /* For 4-corner performance improve */
2937 rtl_writephy(tp, 0x1f, 0x0005);
2938 rtl_writephy(tp, 0x05, 0x8b80);
2939 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
2940 rtl_writephy(tp, 0x1f, 0x0000);
2942 /* PHY auto speed down */
2943 r8168d_modify_extpage(phydev, 0x002d, 0x18, 0x0000, 0x0010);
2944 phy_set_bits(phydev, 0x14, BIT(15));
2946 /* improve 10M EEE waveform */
2947 r8168d_phy_param(phydev, 0x8b86, 0x0000, 0x0001);
2949 /* Improve 2-pair detection performance */
2950 r8168d_phy_param(phydev, 0x8b85, 0x0000, 0x4000);
2952 rtl8168f_config_eee_phy(tp);
2956 rtl_writephy(tp, 0x1f, 0x0003);
2957 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
2958 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
2959 rtl_writephy(tp, 0x1f, 0x0000);
2960 rtl_writephy(tp, 0x1f, 0x0005);
2961 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
2962 rtl_writephy(tp, 0x1f, 0x0000);
2964 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
2965 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
2968 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
2970 struct phy_device *phydev = tp->phydev;
2972 /* For 4-corner performance improve */
2973 r8168d_phy_param(phydev, 0x8b80, 0x0000, 0x0006);
2975 /* PHY auto speed down */
2976 r8168d_modify_extpage(phydev, 0x002d, 0x18, 0x0000, 0x0010);
2977 phy_set_bits(phydev, 0x14, BIT(15));
2979 /* Improve 10M EEE waveform */
2980 r8168d_phy_param(phydev, 0x8b86, 0x0000, 0x0001);
2982 rtl8168f_config_eee_phy(tp);
2986 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2988 struct phy_device *phydev = tp->phydev;
2990 rtl_apply_firmware(tp);
2992 /* Channel estimation fine tune */
2993 phy_write_paged(phydev, 0x0003, 0x09, 0xa20f);
2995 /* Modify green table for giga & fnet */
2996 r8168d_phy_param(phydev, 0x8b55, 0xffff, 0x0000);
2997 r8168d_phy_param(phydev, 0x8b5e, 0xffff, 0x0000);
2998 r8168d_phy_param(phydev, 0x8b67, 0xffff, 0x0000);
2999 r8168d_phy_param(phydev, 0x8b70, 0xffff, 0x0000);
3000 r8168d_modify_extpage(phydev, 0x0078, 0x17, 0xffff, 0x0000);
3001 r8168d_modify_extpage(phydev, 0x0078, 0x19, 0xffff, 0x00fb);
3003 /* Modify green table for 10M */
3004 r8168d_phy_param(phydev, 0x8b79, 0xffff, 0xaa00);
3006 /* Disable hiimpedance detection (RTCT) */
3007 phy_write_paged(phydev, 0x0003, 0x01, 0x328a);
3009 rtl8168f_hw_phy_config(tp);
3011 /* Improve 2-pair detection performance */
3012 r8168d_phy_param(phydev, 0x8b85, 0x0000, 0x4000);
3015 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3017 rtl_apply_firmware(tp);
3019 rtl8168f_hw_phy_config(tp);
3022 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3024 struct phy_device *phydev = tp->phydev;
3026 rtl_apply_firmware(tp);
3028 rtl8168f_hw_phy_config(tp);
3030 /* Improve 2-pair detection performance */
3031 r8168d_phy_param(phydev, 0x8b85, 0x0000, 0x4000);
3033 /* Channel estimation fine tune */
3034 phy_write_paged(phydev, 0x0003, 0x09, 0xa20f);
3036 /* Modify green table for giga & fnet */
3037 r8168d_phy_param(phydev, 0x8b55, 0xffff, 0x0000);
3038 r8168d_phy_param(phydev, 0x8b5e, 0xffff, 0x0000);
3039 r8168d_phy_param(phydev, 0x8b67, 0xffff, 0x0000);
3040 r8168d_phy_param(phydev, 0x8b70, 0xffff, 0x0000);
3041 r8168d_modify_extpage(phydev, 0x0078, 0x17, 0xffff, 0x0000);
3042 r8168d_modify_extpage(phydev, 0x0078, 0x19, 0xffff, 0x00aa);
3044 /* Modify green table for 10M */
3045 r8168d_phy_param(phydev, 0x8b79, 0xffff, 0xaa00);
3047 /* Disable hiimpedance detection (RTCT) */
3048 phy_write_paged(phydev, 0x0003, 0x01, 0x328a);
3050 /* Modify green table for giga */
3051 r8168d_phy_param(phydev, 0x8b54, 0x0800, 0x0000);
3052 r8168d_phy_param(phydev, 0x8b5d, 0x0800, 0x0000);
3053 r8168d_phy_param(phydev, 0x8a7c, 0x0100, 0x0000);
3054 r8168d_phy_param(phydev, 0x8a7f, 0x0000, 0x0100);
3055 r8168d_phy_param(phydev, 0x8a82, 0x0100, 0x0000);
3056 r8168d_phy_param(phydev, 0x8a85, 0x0100, 0x0000);
3057 r8168d_phy_param(phydev, 0x8a88, 0x0100, 0x0000);
3059 /* uc same-seed solution */
3060 r8168d_phy_param(phydev, 0x8b85, 0x0000, 0x8000);
3063 rtl_writephy(tp, 0x1f, 0x0003);
3064 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3065 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3066 rtl_writephy(tp, 0x1f, 0x0000);
3069 static void rtl8168g_disable_aldps(struct rtl8169_private *tp)
3071 phy_modify_paged(tp->phydev, 0x0a43, 0x10, BIT(2), 0);
3074 static void rtl8168g_phy_adjust_10m_aldps(struct rtl8169_private *tp)
3076 struct phy_device *phydev = tp->phydev;
3078 phy_modify_paged(phydev, 0x0bcc, 0x14, BIT(8), 0);
3079 phy_modify_paged(phydev, 0x0a44, 0x11, 0, BIT(7) | BIT(6));
3080 r8168g_phy_param(phydev, 0x8084, 0x6000, 0x0000);
3081 phy_modify_paged(phydev, 0x0a43, 0x10, 0x0000, 0x1003);
3084 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3088 rtl_apply_firmware(tp);
3090 ret = phy_read_paged(tp->phydev, 0x0a46, 0x10);
3092 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, BIT(15), 0);
3094 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, 0, BIT(15));
3096 ret = phy_read_paged(tp->phydev, 0x0a46, 0x13);
3098 phy_modify_paged(tp->phydev, 0x0c41, 0x15, 0, BIT(1));
3100 phy_modify_paged(tp->phydev, 0x0c41, 0x15, BIT(1), 0);
3102 /* Enable PHY auto speed down */
3103 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
3105 rtl8168g_phy_adjust_10m_aldps(tp);
3107 /* EEE auto-fallback function */
3108 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
3110 /* Enable UC LPF tune function */
3111 r8168g_phy_param(tp->phydev, 0x8012, 0x0000, 0x8000);
3113 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
3115 /* Improve SWR Efficiency */
3116 rtl_writephy(tp, 0x1f, 0x0bcd);
3117 rtl_writephy(tp, 0x14, 0x5065);
3118 rtl_writephy(tp, 0x14, 0xd065);
3119 rtl_writephy(tp, 0x1f, 0x0bc8);
3120 rtl_writephy(tp, 0x11, 0x5655);
3121 rtl_writephy(tp, 0x1f, 0x0bcd);
3122 rtl_writephy(tp, 0x14, 0x1065);
3123 rtl_writephy(tp, 0x14, 0x9065);
3124 rtl_writephy(tp, 0x14, 0x1065);
3125 rtl_writephy(tp, 0x1f, 0x0000);
3127 rtl8168g_disable_aldps(tp);
3128 rtl8168g_config_eee_phy(tp);
3132 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3134 rtl_apply_firmware(tp);
3135 rtl8168g_config_eee_phy(tp);
3139 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3141 struct phy_device *phydev = tp->phydev;
3145 rtl_apply_firmware(tp);
3147 /* CHN EST parameters adjust - giga master */
3148 r8168g_phy_param(phydev, 0x809b, 0xf800, 0x8000);
3149 r8168g_phy_param(phydev, 0x80a2, 0xff00, 0x8000);
3150 r8168g_phy_param(phydev, 0x80a4, 0xff00, 0x8500);
3151 r8168g_phy_param(phydev, 0x809c, 0xff00, 0xbd00);
3153 /* CHN EST parameters adjust - giga slave */
3154 r8168g_phy_param(phydev, 0x80ad, 0xf800, 0x7000);
3155 r8168g_phy_param(phydev, 0x80b4, 0xff00, 0x5000);
3156 r8168g_phy_param(phydev, 0x80ac, 0xff00, 0x4000);
3158 /* CHN EST parameters adjust - fnet */
3159 r8168g_phy_param(phydev, 0x808e, 0xff00, 0x1200);
3160 r8168g_phy_param(phydev, 0x8090, 0xff00, 0xe500);
3161 r8168g_phy_param(phydev, 0x8092, 0xff00, 0x9f00);
3163 /* enable R-tune & PGA-retune function */
3165 data = phy_read_paged(phydev, 0x0a46, 0x13);
3168 dout_tapbin |= data;
3169 data = phy_read_paged(phydev, 0x0a46, 0x12);
3172 dout_tapbin |= data;
3173 dout_tapbin = ~(dout_tapbin^0x08);
3175 dout_tapbin &= 0xf000;
3177 r8168g_phy_param(phydev, 0x827a, 0xf000, dout_tapbin);
3178 r8168g_phy_param(phydev, 0x827b, 0xf000, dout_tapbin);
3179 r8168g_phy_param(phydev, 0x827c, 0xf000, dout_tapbin);
3180 r8168g_phy_param(phydev, 0x827d, 0xf000, dout_tapbin);
3181 r8168g_phy_param(phydev, 0x0811, 0x0000, 0x0800);
3182 phy_modify_paged(phydev, 0x0a42, 0x16, 0x0000, 0x0002);
3184 /* enable GPHY 10M */
3185 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
3187 /* SAR ADC performance */
3188 phy_modify_paged(tp->phydev, 0x0bca, 0x17, BIT(12) | BIT(13), BIT(14));
3190 r8168g_phy_param(phydev, 0x803f, 0x3000, 0x0000);
3191 r8168g_phy_param(phydev, 0x8047, 0x3000, 0x0000);
3192 r8168g_phy_param(phydev, 0x804f, 0x3000, 0x0000);
3193 r8168g_phy_param(phydev, 0x8057, 0x3000, 0x0000);
3194 r8168g_phy_param(phydev, 0x805f, 0x3000, 0x0000);
3195 r8168g_phy_param(phydev, 0x8067, 0x3000, 0x0000);
3196 r8168g_phy_param(phydev, 0x806f, 0x3000, 0x0000);
3198 /* disable phy pfm mode */
3199 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
3201 rtl8168g_disable_aldps(tp);
3202 rtl8168h_config_eee_phy(tp);
3206 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3208 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3209 struct phy_device *phydev = tp->phydev;
3213 rtl_apply_firmware(tp);
3215 /* CHIN EST parameter update */
3216 r8168g_phy_param(phydev, 0x808a, 0x003f, 0x000a);
3218 /* enable R-tune & PGA-retune function */
3219 r8168g_phy_param(phydev, 0x0811, 0x0000, 0x0800);
3220 phy_modify_paged(phydev, 0x0a42, 0x16, 0x0000, 0x0002);
3222 /* enable GPHY 10M */
3223 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
3225 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3226 data = r8168_mac_ocp_read(tp, 0xdd02);
3227 ioffset_p3 = ((data & 0x80)>>7);
3230 data = r8168_mac_ocp_read(tp, 0xdd00);
3231 ioffset_p3 |= ((data & (0xe000))>>13);
3232 ioffset_p2 = ((data & (0x1e00))>>9);
3233 ioffset_p1 = ((data & (0x01e0))>>5);
3234 ioffset_p0 = ((data & 0x0010)>>4);
3236 ioffset_p0 |= (data & (0x07));
3237 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3239 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3240 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f))
3241 phy_write_paged(phydev, 0x0bcf, 0x16, data);
3243 /* Modify rlen (TX LPF corner frequency) level */
3244 data = phy_read_paged(phydev, 0x0bcd, 0x16);
3249 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3250 phy_write_paged(phydev, 0x0bcd, 0x17, data);
3252 /* disable phy pfm mode */
3253 phy_modify_paged(phydev, 0x0a44, 0x11, BIT(7), 0);
3255 rtl8168g_disable_aldps(tp);
3256 rtl8168g_config_eee_phy(tp);
3260 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3262 struct phy_device *phydev = tp->phydev;
3264 /* Enable PHY auto speed down */
3265 phy_modify_paged(phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
3267 rtl8168g_phy_adjust_10m_aldps(tp);
3269 /* Enable EEE auto-fallback function */
3270 phy_modify_paged(phydev, 0x0a4b, 0x11, 0, BIT(2));
3272 /* Enable UC LPF tune function */
3273 r8168g_phy_param(phydev, 0x8012, 0x0000, 0x8000);
3275 /* set rg_sel_sdm_rate */
3276 phy_modify_paged(phydev, 0x0c42, 0x11, BIT(13), BIT(14));
3278 rtl8168g_disable_aldps(tp);
3279 rtl8168g_config_eee_phy(tp);
3283 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3285 struct phy_device *phydev = tp->phydev;
3287 rtl8168g_phy_adjust_10m_aldps(tp);
3289 /* Enable UC LPF tune function */
3290 r8168g_phy_param(phydev, 0x8012, 0x0000, 0x8000);
3292 /* Set rg_sel_sdm_rate */
3293 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
3295 /* Channel estimation parameters */
3296 r8168g_phy_param(phydev, 0x80f3, 0xff00, 0x8b00);
3297 r8168g_phy_param(phydev, 0x80f0, 0xff00, 0x3a00);
3298 r8168g_phy_param(phydev, 0x80ef, 0xff00, 0x0500);
3299 r8168g_phy_param(phydev, 0x80f6, 0xff00, 0x6e00);
3300 r8168g_phy_param(phydev, 0x80ec, 0xff00, 0x6800);
3301 r8168g_phy_param(phydev, 0x80ed, 0xff00, 0x7c00);
3302 r8168g_phy_param(phydev, 0x80f2, 0xff00, 0xf400);
3303 r8168g_phy_param(phydev, 0x80f4, 0xff00, 0x8500);
3304 r8168g_phy_param(phydev, 0x8110, 0xff00, 0xa800);
3305 r8168g_phy_param(phydev, 0x810f, 0xff00, 0x1d00);
3306 r8168g_phy_param(phydev, 0x8111, 0xff00, 0xf500);
3307 r8168g_phy_param(phydev, 0x8113, 0xff00, 0x6100);
3308 r8168g_phy_param(phydev, 0x8115, 0xff00, 0x9200);
3309 r8168g_phy_param(phydev, 0x810e, 0xff00, 0x0400);
3310 r8168g_phy_param(phydev, 0x810c, 0xff00, 0x7c00);
3311 r8168g_phy_param(phydev, 0x810b, 0xff00, 0x5a00);
3312 r8168g_phy_param(phydev, 0x80d1, 0xff00, 0xff00);
3313 r8168g_phy_param(phydev, 0x80cd, 0xff00, 0x9e00);
3314 r8168g_phy_param(phydev, 0x80d3, 0xff00, 0x0e00);
3315 r8168g_phy_param(phydev, 0x80d5, 0xff00, 0xca00);
3316 r8168g_phy_param(phydev, 0x80d7, 0xff00, 0x8400);
3318 /* Force PWM-mode */
3319 rtl_writephy(tp, 0x1f, 0x0bcd);
3320 rtl_writephy(tp, 0x14, 0x5065);
3321 rtl_writephy(tp, 0x14, 0xd065);
3322 rtl_writephy(tp, 0x1f, 0x0bc8);
3323 rtl_writephy(tp, 0x12, 0x00ed);
3324 rtl_writephy(tp, 0x1f, 0x0bcd);
3325 rtl_writephy(tp, 0x14, 0x1065);
3326 rtl_writephy(tp, 0x14, 0x9065);
3327 rtl_writephy(tp, 0x14, 0x1065);
3328 rtl_writephy(tp, 0x1f, 0x0000);
3330 rtl8168g_disable_aldps(tp);
3331 rtl8168g_config_eee_phy(tp);
3335 static void rtl8117_hw_phy_config(struct rtl8169_private *tp)
3337 struct phy_device *phydev = tp->phydev;
3339 /* CHN EST parameters adjust - fnet */
3340 r8168g_phy_param(phydev, 0x808e, 0xff00, 0x4800);
3341 r8168g_phy_param(phydev, 0x8090, 0xff00, 0xcc00);
3342 r8168g_phy_param(phydev, 0x8092, 0xff00, 0xb000);
3344 r8168g_phy_param(phydev, 0x8088, 0xff00, 0x6000);
3345 r8168g_phy_param(phydev, 0x808b, 0x3f00, 0x0b00);
3346 r8168g_phy_param(phydev, 0x808d, 0x1f00, 0x0600);
3347 r8168g_phy_param(phydev, 0x808c, 0xff00, 0xb000);
3348 r8168g_phy_param(phydev, 0x80a0, 0xff00, 0x2800);
3349 r8168g_phy_param(phydev, 0x80a2, 0xff00, 0x5000);
3350 r8168g_phy_param(phydev, 0x809b, 0xf800, 0xb000);
3351 r8168g_phy_param(phydev, 0x809a, 0xff00, 0x4b00);
3352 r8168g_phy_param(phydev, 0x809d, 0x3f00, 0x0800);
3353 r8168g_phy_param(phydev, 0x80a1, 0xff00, 0x7000);
3354 r8168g_phy_param(phydev, 0x809f, 0x1f00, 0x0300);
3355 r8168g_phy_param(phydev, 0x809e, 0xff00, 0x8800);
3356 r8168g_phy_param(phydev, 0x80b2, 0xff00, 0x2200);
3357 r8168g_phy_param(phydev, 0x80ad, 0xf800, 0x9800);
3358 r8168g_phy_param(phydev, 0x80af, 0x3f00, 0x0800);
3359 r8168g_phy_param(phydev, 0x80b3, 0xff00, 0x6f00);
3360 r8168g_phy_param(phydev, 0x80b1, 0x1f00, 0x0300);
3361 r8168g_phy_param(phydev, 0x80b0, 0xff00, 0x9300);
3363 r8168g_phy_param(phydev, 0x8011, 0x0000, 0x0800);
3365 /* enable GPHY 10M */
3366 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
3368 r8168g_phy_param(phydev, 0x8016, 0x0000, 0x0400);
3370 rtl8168g_disable_aldps(tp);
3371 rtl8168h_config_eee_phy(tp);
3375 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3377 static const struct phy_reg phy_reg_init[] = {
3384 rtl_writephy(tp, 0x1f, 0x0000);
3385 rtl_patchphy(tp, 0x11, 1 << 12);
3386 rtl_patchphy(tp, 0x19, 1 << 13);
3387 rtl_patchphy(tp, 0x10, 1 << 15);
3389 rtl_writephy_batch(tp, phy_reg_init);
3392 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3394 /* Disable ALDPS before ram code */
3395 phy_write(tp->phydev, 0x18, 0x0310);
3398 rtl_apply_firmware(tp);
3400 phy_write_paged(tp->phydev, 0x0005, 0x1a, 0x0000);
3401 phy_write_paged(tp->phydev, 0x0004, 0x1c, 0x0000);
3402 phy_write_paged(tp->phydev, 0x0001, 0x15, 0x7701);
3405 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3407 /* Disable ALDPS before setting firmware */
3408 phy_write(tp->phydev, 0x18, 0x0310);
3411 rtl_apply_firmware(tp);
3414 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
3415 rtl_writephy(tp, 0x1f, 0x0004);
3416 rtl_writephy(tp, 0x10, 0x401f);
3417 rtl_writephy(tp, 0x19, 0x7030);
3418 rtl_writephy(tp, 0x1f, 0x0000);
3421 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3423 static const struct phy_reg phy_reg_init[] = {
3430 /* Disable ALDPS before ram code */
3431 phy_write(tp->phydev, 0x18, 0x0310);
3434 rtl_apply_firmware(tp);
3436 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
3437 rtl_writephy_batch(tp, phy_reg_init);
3439 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
3442 static void rtl8125_1_hw_phy_config(struct rtl8169_private *tp)
3444 struct phy_device *phydev = tp->phydev;
3446 phy_modify_paged(phydev, 0xad4, 0x10, 0x03ff, 0x0084);
3447 phy_modify_paged(phydev, 0xad4, 0x17, 0x0000, 0x0010);
3448 phy_modify_paged(phydev, 0xad1, 0x13, 0x03ff, 0x0006);
3449 phy_modify_paged(phydev, 0xad3, 0x11, 0x003f, 0x0006);
3450 phy_modify_paged(phydev, 0xac0, 0x14, 0x0000, 0x1100);
3451 phy_modify_paged(phydev, 0xac8, 0x15, 0xf000, 0x7000);
3452 phy_modify_paged(phydev, 0xad1, 0x14, 0x0000, 0x0400);
3453 phy_modify_paged(phydev, 0xad1, 0x15, 0x0000, 0x03ff);
3454 phy_modify_paged(phydev, 0xad1, 0x16, 0x0000, 0x03ff);
3456 r8168g_phy_param(phydev, 0x80ea, 0xff00, 0xc400);
3457 r8168g_phy_param(phydev, 0x80eb, 0x0700, 0x0300);
3458 r8168g_phy_param(phydev, 0x80f8, 0xff00, 0x1c00);
3459 r8168g_phy_param(phydev, 0x80f1, 0xff00, 0x3000);
3460 r8168g_phy_param(phydev, 0x80fe, 0xff00, 0xa500);
3461 r8168g_phy_param(phydev, 0x8102, 0xff00, 0x5000);
3462 r8168g_phy_param(phydev, 0x8105, 0xff00, 0x3300);
3463 r8168g_phy_param(phydev, 0x8100, 0xff00, 0x7000);
3464 r8168g_phy_param(phydev, 0x8104, 0xff00, 0xf000);
3465 r8168g_phy_param(phydev, 0x8106, 0xff00, 0x6500);
3466 r8168g_phy_param(phydev, 0x80dc, 0xff00, 0xed00);
3467 r8168g_phy_param(phydev, 0x80df, 0x0000, 0x0100);
3468 r8168g_phy_param(phydev, 0x80e1, 0x0100, 0x0000);
3470 phy_modify_paged(phydev, 0xbf0, 0x13, 0x003f, 0x0038);
3471 r8168g_phy_param(phydev, 0x819f, 0xffff, 0xd0b6);
3473 phy_write_paged(phydev, 0xbc3, 0x12, 0x5555);
3474 phy_modify_paged(phydev, 0xbf0, 0x15, 0x0e00, 0x0a00);
3475 phy_modify_paged(phydev, 0xa5c, 0x10, 0x0400, 0x0000);
3476 phy_modify_paged(phydev, 0xa44, 0x11, 0x0000, 0x0800);
3478 rtl8125_config_eee_phy(tp);
3482 static void rtl8125_2_hw_phy_config(struct rtl8169_private *tp)
3484 struct phy_device *phydev = tp->phydev;
3487 phy_modify_paged(phydev, 0xad4, 0x17, 0x0000, 0x0010);
3488 phy_modify_paged(phydev, 0xad1, 0x13, 0x03ff, 0x03ff);
3489 phy_modify_paged(phydev, 0xad3, 0x11, 0x003f, 0x0006);
3490 phy_modify_paged(phydev, 0xac0, 0x14, 0x1100, 0x0000);
3491 phy_modify_paged(phydev, 0xacc, 0x10, 0x0003, 0x0002);
3492 phy_modify_paged(phydev, 0xad4, 0x10, 0x00e7, 0x0044);
3493 phy_modify_paged(phydev, 0xac1, 0x12, 0x0080, 0x0000);
3494 phy_modify_paged(phydev, 0xac8, 0x10, 0x0300, 0x0000);
3495 phy_modify_paged(phydev, 0xac5, 0x17, 0x0007, 0x0002);
3496 phy_write_paged(phydev, 0xad4, 0x16, 0x00a8);
3497 phy_write_paged(phydev, 0xac5, 0x16, 0x01ff);
3498 phy_modify_paged(phydev, 0xac8, 0x15, 0x00f0, 0x0030);
3500 phy_write(phydev, 0x1f, 0x0b87);
3501 phy_write(phydev, 0x16, 0x80a2);
3502 phy_write(phydev, 0x17, 0x0153);
3503 phy_write(phydev, 0x16, 0x809c);
3504 phy_write(phydev, 0x17, 0x0153);
3505 phy_write(phydev, 0x1f, 0x0000);
3507 phy_write(phydev, 0x1f, 0x0a43);
3508 phy_write(phydev, 0x13, 0x81B3);
3509 phy_write(phydev, 0x14, 0x0043);
3510 phy_write(phydev, 0x14, 0x00A7);
3511 phy_write(phydev, 0x14, 0x00D6);
3512 phy_write(phydev, 0x14, 0x00EC);
3513 phy_write(phydev, 0x14, 0x00F6);
3514 phy_write(phydev, 0x14, 0x00FB);
3515 phy_write(phydev, 0x14, 0x00FD);
3516 phy_write(phydev, 0x14, 0x00FF);
3517 phy_write(phydev, 0x14, 0x00BB);
3518 phy_write(phydev, 0x14, 0x0058);
3519 phy_write(phydev, 0x14, 0x0029);
3520 phy_write(phydev, 0x14, 0x0013);
3521 phy_write(phydev, 0x14, 0x0009);
3522 phy_write(phydev, 0x14, 0x0004);
3523 phy_write(phydev, 0x14, 0x0002);
3524 for (i = 0; i < 25; i++)
3525 phy_write(phydev, 0x14, 0x0000);
3526 phy_write(phydev, 0x1f, 0x0000);
3528 r8168g_phy_param(phydev, 0x8257, 0xffff, 0x020F);
3529 r8168g_phy_param(phydev, 0x80ea, 0xffff, 0x7843);
3531 rtl_apply_firmware(tp);
3533 phy_modify_paged(phydev, 0xd06, 0x14, 0x0000, 0x2000);
3535 r8168g_phy_param(phydev, 0x81a2, 0x0000, 0x0100);
3537 phy_modify_paged(phydev, 0xb54, 0x16, 0xff00, 0xdb00);
3538 phy_modify_paged(phydev, 0xa45, 0x12, 0x0001, 0x0000);
3539 phy_modify_paged(phydev, 0xa5d, 0x12, 0x0000, 0x0020);
3540 phy_modify_paged(phydev, 0xad4, 0x17, 0x0010, 0x0000);
3541 phy_modify_paged(phydev, 0xa86, 0x15, 0x0001, 0x0000);
3542 phy_modify_paged(phydev, 0xa44, 0x11, 0x0000, 0x0800);
3544 rtl8125_config_eee_phy(tp);
3548 static void rtl_hw_phy_config(struct net_device *dev)
3550 static const rtl_generic_fct phy_configs[] = {
3552 [RTL_GIGA_MAC_VER_02] = rtl8169s_hw_phy_config,
3553 [RTL_GIGA_MAC_VER_03] = rtl8169s_hw_phy_config,
3554 [RTL_GIGA_MAC_VER_04] = rtl8169sb_hw_phy_config,
3555 [RTL_GIGA_MAC_VER_05] = rtl8169scd_hw_phy_config,
3556 [RTL_GIGA_MAC_VER_06] = rtl8169sce_hw_phy_config,
3557 /* PCI-E devices. */
3558 [RTL_GIGA_MAC_VER_07] = rtl8102e_hw_phy_config,
3559 [RTL_GIGA_MAC_VER_08] = rtl8102e_hw_phy_config,
3560 [RTL_GIGA_MAC_VER_09] = rtl8102e_hw_phy_config,
3561 [RTL_GIGA_MAC_VER_10] = NULL,
3562 [RTL_GIGA_MAC_VER_11] = rtl8168bb_hw_phy_config,
3563 [RTL_GIGA_MAC_VER_12] = rtl8168bef_hw_phy_config,
3564 [RTL_GIGA_MAC_VER_13] = NULL,
3565 [RTL_GIGA_MAC_VER_14] = NULL,
3566 [RTL_GIGA_MAC_VER_15] = NULL,
3567 [RTL_GIGA_MAC_VER_16] = NULL,
3568 [RTL_GIGA_MAC_VER_17] = rtl8168bef_hw_phy_config,
3569 [RTL_GIGA_MAC_VER_18] = rtl8168cp_1_hw_phy_config,
3570 [RTL_GIGA_MAC_VER_19] = rtl8168c_1_hw_phy_config,
3571 [RTL_GIGA_MAC_VER_20] = rtl8168c_2_hw_phy_config,
3572 [RTL_GIGA_MAC_VER_21] = rtl8168c_3_hw_phy_config,
3573 [RTL_GIGA_MAC_VER_22] = rtl8168c_3_hw_phy_config,
3574 [RTL_GIGA_MAC_VER_23] = rtl8168cp_2_hw_phy_config,
3575 [RTL_GIGA_MAC_VER_24] = rtl8168cp_2_hw_phy_config,
3576 [RTL_GIGA_MAC_VER_25] = rtl8168d_1_hw_phy_config,
3577 [RTL_GIGA_MAC_VER_26] = rtl8168d_2_hw_phy_config,
3578 [RTL_GIGA_MAC_VER_27] = rtl8168d_3_hw_phy_config,
3579 [RTL_GIGA_MAC_VER_28] = rtl8168d_4_hw_phy_config,
3580 [RTL_GIGA_MAC_VER_29] = rtl8105e_hw_phy_config,
3581 [RTL_GIGA_MAC_VER_30] = rtl8105e_hw_phy_config,
3582 [RTL_GIGA_MAC_VER_31] = NULL,
3583 [RTL_GIGA_MAC_VER_32] = rtl8168e_1_hw_phy_config,
3584 [RTL_GIGA_MAC_VER_33] = rtl8168e_1_hw_phy_config,
3585 [RTL_GIGA_MAC_VER_34] = rtl8168e_2_hw_phy_config,
3586 [RTL_GIGA_MAC_VER_35] = rtl8168f_1_hw_phy_config,
3587 [RTL_GIGA_MAC_VER_36] = rtl8168f_2_hw_phy_config,
3588 [RTL_GIGA_MAC_VER_37] = rtl8402_hw_phy_config,
3589 [RTL_GIGA_MAC_VER_38] = rtl8411_hw_phy_config,
3590 [RTL_GIGA_MAC_VER_39] = rtl8106e_hw_phy_config,
3591 [RTL_GIGA_MAC_VER_40] = rtl8168g_1_hw_phy_config,
3592 [RTL_GIGA_MAC_VER_41] = NULL,
3593 [RTL_GIGA_MAC_VER_42] = rtl8168g_2_hw_phy_config,
3594 [RTL_GIGA_MAC_VER_43] = rtl8168g_2_hw_phy_config,
3595 [RTL_GIGA_MAC_VER_44] = rtl8168g_2_hw_phy_config,
3596 [RTL_GIGA_MAC_VER_45] = rtl8168h_1_hw_phy_config,
3597 [RTL_GIGA_MAC_VER_46] = rtl8168h_2_hw_phy_config,
3598 [RTL_GIGA_MAC_VER_47] = rtl8168h_1_hw_phy_config,
3599 [RTL_GIGA_MAC_VER_48] = rtl8168h_2_hw_phy_config,
3600 [RTL_GIGA_MAC_VER_49] = rtl8168ep_1_hw_phy_config,
3601 [RTL_GIGA_MAC_VER_50] = rtl8168ep_2_hw_phy_config,
3602 [RTL_GIGA_MAC_VER_51] = rtl8168ep_2_hw_phy_config,
3603 [RTL_GIGA_MAC_VER_52] = rtl8117_hw_phy_config,
3604 [RTL_GIGA_MAC_VER_60] = rtl8125_1_hw_phy_config,
3605 [RTL_GIGA_MAC_VER_61] = rtl8125_2_hw_phy_config,
3607 struct rtl8169_private *tp = netdev_priv(dev);
3609 if (phy_configs[tp->mac_version])
3610 phy_configs[tp->mac_version](tp);
3613 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3615 if (!test_and_set_bit(flag, tp->wk.flags))
3616 schedule_work(&tp->wk.work);
3619 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3621 rtl_hw_phy_config(dev);
3623 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3624 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3625 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3626 netif_dbg(tp, drv, dev,
3627 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3628 RTL_W8(tp, 0x82, 0x01);
3631 /* We may have called phy_speed_down before */
3632 phy_speed_up(tp->phydev);
3634 genphy_soft_reset(tp->phydev);
3637 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3641 rtl_unlock_config_regs(tp);
3643 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
3646 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3649 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3650 rtl_rar_exgmac_set(tp, addr);
3652 rtl_lock_config_regs(tp);
3654 rtl_unlock_work(tp);
3657 static int rtl_set_mac_address(struct net_device *dev, void *p)
3659 struct rtl8169_private *tp = netdev_priv(dev);
3660 struct device *d = tp_to_dev(tp);
3663 ret = eth_mac_addr(dev, p);
3667 pm_runtime_get_noresume(d);
3669 if (pm_runtime_active(d))
3670 rtl_rar_set(tp, dev->dev_addr);
3672 pm_runtime_put_noidle(d);
3677 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3679 struct rtl8169_private *tp = netdev_priv(dev);
3681 if (!netif_running(dev))
3684 return phy_mii_ioctl(tp->phydev, ifr, cmd);
3687 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3689 switch (tp->mac_version) {
3690 case RTL_GIGA_MAC_VER_25:
3691 case RTL_GIGA_MAC_VER_26:
3692 case RTL_GIGA_MAC_VER_29:
3693 case RTL_GIGA_MAC_VER_30:
3694 case RTL_GIGA_MAC_VER_32:
3695 case RTL_GIGA_MAC_VER_33:
3696 case RTL_GIGA_MAC_VER_34:
3697 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_52:
3698 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
3699 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3706 static void rtl_pll_power_down(struct rtl8169_private *tp)
3708 if (r8168_check_dash(tp))
3711 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3712 tp->mac_version == RTL_GIGA_MAC_VER_33)
3713 rtl_ephy_write(tp, 0x19, 0xff64);
3715 if (device_may_wakeup(tp_to_dev(tp))) {
3716 phy_speed_down(tp->phydev, false);
3717 rtl_wol_suspend_quirk(tp);
3721 switch (tp->mac_version) {
3722 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
3723 case RTL_GIGA_MAC_VER_37:
3724 case RTL_GIGA_MAC_VER_39:
3725 case RTL_GIGA_MAC_VER_43:
3726 case RTL_GIGA_MAC_VER_44:
3727 case RTL_GIGA_MAC_VER_45:
3728 case RTL_GIGA_MAC_VER_46:
3729 case RTL_GIGA_MAC_VER_47:
3730 case RTL_GIGA_MAC_VER_48:
3731 case RTL_GIGA_MAC_VER_50:
3732 case RTL_GIGA_MAC_VER_51:
3733 case RTL_GIGA_MAC_VER_52:
3734 case RTL_GIGA_MAC_VER_60:
3735 case RTL_GIGA_MAC_VER_61:
3736 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
3738 case RTL_GIGA_MAC_VER_40:
3739 case RTL_GIGA_MAC_VER_41:
3740 case RTL_GIGA_MAC_VER_49:
3741 rtl_eri_clear_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
3742 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
3749 static void rtl_pll_power_up(struct rtl8169_private *tp)
3751 switch (tp->mac_version) {
3752 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
3753 case RTL_GIGA_MAC_VER_37:
3754 case RTL_GIGA_MAC_VER_39:
3755 case RTL_GIGA_MAC_VER_43:
3756 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
3758 case RTL_GIGA_MAC_VER_44:
3759 case RTL_GIGA_MAC_VER_45:
3760 case RTL_GIGA_MAC_VER_46:
3761 case RTL_GIGA_MAC_VER_47:
3762 case RTL_GIGA_MAC_VER_48:
3763 case RTL_GIGA_MAC_VER_50:
3764 case RTL_GIGA_MAC_VER_51:
3765 case RTL_GIGA_MAC_VER_52:
3766 case RTL_GIGA_MAC_VER_60:
3767 case RTL_GIGA_MAC_VER_61:
3768 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
3770 case RTL_GIGA_MAC_VER_40:
3771 case RTL_GIGA_MAC_VER_41:
3772 case RTL_GIGA_MAC_VER_49:
3773 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
3774 rtl_eri_set_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
3780 phy_resume(tp->phydev);
3781 /* give MAC/PHY some time to resume */
3785 static void rtl_init_rxcfg(struct rtl8169_private *tp)
3787 switch (tp->mac_version) {
3788 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
3789 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
3790 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3792 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
3793 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
3794 case RTL_GIGA_MAC_VER_38:
3795 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3797 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_52:
3798 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
3800 case RTL_GIGA_MAC_VER_60 ... RTL_GIGA_MAC_VER_61:
3801 RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_VLAN_8125 |
3805 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
3810 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3812 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
3815 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3817 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3818 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
3821 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3823 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3824 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
3827 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3829 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3832 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3834 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3837 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3839 RTL_W8(tp, MaxTxPacketSize, 0x3f);
3840 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3841 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
3844 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3846 RTL_W8(tp, MaxTxPacketSize, 0x0c);
3847 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3848 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
3851 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3853 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
3856 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3858 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
3861 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3863 rtl_unlock_config_regs(tp);
3864 switch (tp->mac_version) {
3865 case RTL_GIGA_MAC_VER_12:
3866 case RTL_GIGA_MAC_VER_17:
3867 r8168b_1_hw_jumbo_enable(tp);
3869 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3870 r8168c_hw_jumbo_enable(tp);
3872 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3873 r8168dp_hw_jumbo_enable(tp);
3875 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3876 r8168e_hw_jumbo_enable(tp);
3881 rtl_lock_config_regs(tp);
3884 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3886 rtl_unlock_config_regs(tp);
3887 switch (tp->mac_version) {
3888 case RTL_GIGA_MAC_VER_12:
3889 case RTL_GIGA_MAC_VER_17:
3890 r8168b_1_hw_jumbo_disable(tp);
3892 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3893 r8168c_hw_jumbo_disable(tp);
3895 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3896 r8168dp_hw_jumbo_disable(tp);
3898 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3899 r8168e_hw_jumbo_disable(tp);
3904 rtl_lock_config_regs(tp);
3907 static void rtl_jumbo_config(struct rtl8169_private *tp, int mtu)
3909 if (mtu > ETH_DATA_LEN)
3910 rtl_hw_jumbo_enable(tp);
3912 rtl_hw_jumbo_disable(tp);
3915 DECLARE_RTL_COND(rtl_chipcmd_cond)
3917 return RTL_R8(tp, ChipCmd) & CmdReset;
3920 static void rtl_hw_reset(struct rtl8169_private *tp)
3922 RTL_W8(tp, ChipCmd, CmdReset);
3924 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
3927 static void rtl_request_firmware(struct rtl8169_private *tp)
3929 struct rtl_fw *rtl_fw;
3931 /* firmware loaded already or no firmware available */
3932 if (tp->rtl_fw || !tp->fw_name)
3935 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3937 netif_warn(tp, ifup, tp->dev, "Unable to load firmware, out of memory\n");
3941 rtl_fw->phy_write = rtl_writephy;
3942 rtl_fw->phy_read = rtl_readphy;
3943 rtl_fw->mac_mcu_write = mac_mcu_write;
3944 rtl_fw->mac_mcu_read = mac_mcu_read;
3945 rtl_fw->fw_name = tp->fw_name;
3946 rtl_fw->dev = tp_to_dev(tp);
3948 if (rtl_fw_request_firmware(rtl_fw))
3951 tp->rtl_fw = rtl_fw;
3954 static void rtl_rx_close(struct rtl8169_private *tp)
3956 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
3959 DECLARE_RTL_COND(rtl_npq_cond)
3961 return RTL_R8(tp, TxPoll) & NPQ;
3964 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
3966 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
3969 static void rtl8169_hw_reset(struct rtl8169_private *tp)
3971 /* Disable interrupts */
3972 rtl8169_irq_mask_and_ack(tp);
3976 switch (tp->mac_version) {
3977 case RTL_GIGA_MAC_VER_27:
3978 case RTL_GIGA_MAC_VER_28:
3979 case RTL_GIGA_MAC_VER_31:
3980 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
3982 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
3983 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_52:
3984 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
3985 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
3988 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
3996 static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
3998 u32 val = TX_DMA_BURST << TxDMAShift |
3999 InterFrameGap << TxInterFrameGapShift;
4001 if (rtl_is_8168evl_up(tp))
4002 val |= TXCFG_AUTO_FIFO;
4004 RTL_W32(tp, TxConfig, val);
4007 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
4009 /* Low hurts. Let's disable the filtering. */
4010 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
4013 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
4016 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4017 * register to be written before TxDescAddrLow to work.
4018 * Switching from MMIO to I/O access fixes the issue as well.
4020 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4021 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4022 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4023 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4026 static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
4030 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4032 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4037 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4040 RTL_W32(tp, 0x7c, val);
4043 static void rtl_set_rx_mode(struct net_device *dev)
4045 u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
4046 /* Multicast hash filter */
4047 u32 mc_filter[2] = { 0xffffffff, 0xffffffff };
4048 struct rtl8169_private *tp = netdev_priv(dev);
4051 if (dev->flags & IFF_PROMISC) {
4052 /* Unconditionally log net taps. */
4053 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4054 rx_mode |= AcceptAllPhys;
4055 } else if (netdev_mc_count(dev) > MC_FILTER_LIMIT ||
4056 dev->flags & IFF_ALLMULTI ||
4057 tp->mac_version == RTL_GIGA_MAC_VER_35) {
4058 /* accept all multicasts */
4059 } else if (netdev_mc_empty(dev)) {
4060 rx_mode &= ~AcceptMulticast;
4062 struct netdev_hw_addr *ha;
4064 mc_filter[1] = mc_filter[0] = 0;
4065 netdev_for_each_mc_addr(ha, dev) {
4066 u32 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4067 mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31);
4070 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4072 mc_filter[0] = swab32(mc_filter[1]);
4073 mc_filter[1] = swab32(tmp);
4077 if (dev->features & NETIF_F_RXALL)
4078 rx_mode |= (AcceptErr | AcceptRunt);
4080 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4081 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
4083 tmp = RTL_R32(tp, RxConfig);
4084 RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_MASK) | rx_mode);
4087 DECLARE_RTL_COND(rtl_csiar_cond)
4089 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
4092 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4094 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4096 RTL_W32(tp, CSIDR, value);
4097 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4098 CSIAR_BYTE_ENABLE | func << 16);
4100 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4103 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4105 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4107 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4110 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4111 RTL_R32(tp, CSIDR) : ~0;
4114 static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
4116 struct pci_dev *pdev = tp->pci_dev;
4119 /* According to Realtek the value at config space address 0x070f
4120 * controls the L0s/L1 entrance latency. We try standard ECAM access
4121 * first and if it fails fall back to CSI.
4123 if (pdev->cfg_size > 0x070f &&
4124 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4127 netdev_notice_once(tp->dev,
4128 "No native access to PCI extended config space, falling back to CSI\n");
4129 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4130 rtl_csi_write(tp, 0x070c, csi | val << 24);
4133 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
4135 rtl_csi_access_enable(tp, 0x27);
4139 unsigned int offset;
4144 static void __rtl_ephy_init(struct rtl8169_private *tp,
4145 const struct ephy_info *e, int len)
4150 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4151 rtl_ephy_write(tp, e->offset, w);
4156 #define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
4158 static void rtl_disable_clock_request(struct rtl8169_private *tp)
4160 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
4161 PCI_EXP_LNKCTL_CLKREQ_EN);
4164 static void rtl_enable_clock_request(struct rtl8169_private *tp)
4166 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
4167 PCI_EXP_LNKCTL_CLKREQ_EN);
4170 static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
4172 /* work around an issue when PCI reset occurs during L2/L3 state */
4173 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
4176 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4178 /* Don't enable ASPM in the chip if OS can't control ASPM */
4179 if (enable && tp->aspm_manageable) {
4180 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
4181 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
4183 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4184 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4190 static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
4191 u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
4193 /* Usage of dynamic vs. static FIFO is controlled by bit
4194 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
4196 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
4197 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
4200 static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
4203 /* FIFO thresholds for pause flow control */
4204 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
4205 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
4208 static void rtl_hw_start_8168b(struct rtl8169_private *tp)
4210 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4213 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4215 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
4217 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4219 rtl_disable_clock_request(tp);
4222 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4224 static const struct ephy_info e_info_8168cp[] = {
4225 { 0x01, 0, 0x0001 },
4226 { 0x02, 0x0800, 0x1000 },
4227 { 0x03, 0, 0x0042 },
4228 { 0x06, 0x0080, 0x0000 },
4232 rtl_set_def_aspm_entry_latency(tp);
4234 rtl_ephy_init(tp, e_info_8168cp);
4236 __rtl_hw_start_8168cp(tp);
4239 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4241 rtl_set_def_aspm_entry_latency(tp);
4243 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4246 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4248 rtl_set_def_aspm_entry_latency(tp);
4250 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4253 RTL_W8(tp, DBG_REG, 0x20);
4256 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4258 static const struct ephy_info e_info_8168c_1[] = {
4259 { 0x02, 0x0800, 0x1000 },
4260 { 0x03, 0, 0x0002 },
4261 { 0x06, 0x0080, 0x0000 }
4264 rtl_set_def_aspm_entry_latency(tp);
4266 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4268 rtl_ephy_init(tp, e_info_8168c_1);
4270 __rtl_hw_start_8168cp(tp);
4273 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4275 static const struct ephy_info e_info_8168c_2[] = {
4276 { 0x01, 0, 0x0001 },
4277 { 0x03, 0x0400, 0x0020 }
4280 rtl_set_def_aspm_entry_latency(tp);
4282 rtl_ephy_init(tp, e_info_8168c_2);
4284 __rtl_hw_start_8168cp(tp);
4287 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4289 rtl_hw_start_8168c_2(tp);
4292 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4294 rtl_set_def_aspm_entry_latency(tp);
4296 __rtl_hw_start_8168cp(tp);
4299 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4301 rtl_set_def_aspm_entry_latency(tp);
4303 rtl_disable_clock_request(tp);
4306 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
4308 static const struct ephy_info e_info_8168d_4[] = {
4309 { 0x0b, 0x0000, 0x0048 },
4310 { 0x19, 0x0020, 0x0050 },
4311 { 0x0c, 0x0100, 0x0020 },
4312 { 0x10, 0x0004, 0x0000 },
4315 rtl_set_def_aspm_entry_latency(tp);
4317 rtl_ephy_init(tp, e_info_8168d_4);
4319 rtl_enable_clock_request(tp);
4322 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
4324 static const struct ephy_info e_info_8168e_1[] = {
4325 { 0x00, 0x0200, 0x0100 },
4326 { 0x00, 0x0000, 0x0004 },
4327 { 0x06, 0x0002, 0x0001 },
4328 { 0x06, 0x0000, 0x0030 },
4329 { 0x07, 0x0000, 0x2000 },
4330 { 0x00, 0x0000, 0x0020 },
4331 { 0x03, 0x5800, 0x2000 },
4332 { 0x03, 0x0000, 0x0001 },
4333 { 0x01, 0x0800, 0x1000 },
4334 { 0x07, 0x0000, 0x4000 },
4335 { 0x1e, 0x0000, 0x2000 },
4336 { 0x19, 0xffff, 0xfe6c },
4337 { 0x0a, 0x0000, 0x0040 }
4340 rtl_set_def_aspm_entry_latency(tp);
4342 rtl_ephy_init(tp, e_info_8168e_1);
4344 rtl_disable_clock_request(tp);
4346 /* Reset tx FIFO pointer */
4347 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4348 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
4350 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
4353 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
4355 static const struct ephy_info e_info_8168e_2[] = {
4356 { 0x09, 0x0000, 0x0080 },
4357 { 0x19, 0x0000, 0x0224 },
4358 { 0x00, 0x0000, 0x0004 },
4359 { 0x0c, 0x3df0, 0x0200 },
4362 rtl_set_def_aspm_entry_latency(tp);
4364 rtl_ephy_init(tp, e_info_8168e_2);
4366 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4367 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4368 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
4369 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4370 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
4371 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4372 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
4374 rtl_disable_clock_request(tp);
4376 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4378 rtl8168_config_eee_mac(tp);
4380 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4381 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4382 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
4384 rtl_hw_aspm_clkreq_enable(tp, true);
4387 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
4389 rtl_set_def_aspm_entry_latency(tp);
4391 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4392 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4393 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
4394 rtl_reset_packet_filter(tp);
4395 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4396 rtl_eri_set_bits(tp, 0x1d0, ERIAR_MASK_0001, BIT(4));
4397 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4398 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
4400 rtl_disable_clock_request(tp);
4402 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4403 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4404 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4405 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
4407 rtl8168_config_eee_mac(tp);
4410 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4412 static const struct ephy_info e_info_8168f_1[] = {
4413 { 0x06, 0x00c0, 0x0020 },
4414 { 0x08, 0x0001, 0x0002 },
4415 { 0x09, 0x0000, 0x0080 },
4416 { 0x19, 0x0000, 0x0224 },
4417 { 0x00, 0x0000, 0x0004 },
4418 { 0x0c, 0x3df0, 0x0200 },
4421 rtl_hw_start_8168f(tp);
4423 rtl_ephy_init(tp, e_info_8168f_1);
4425 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
4428 static void rtl_hw_start_8411(struct rtl8169_private *tp)
4430 static const struct ephy_info e_info_8168f_1[] = {
4431 { 0x06, 0x00c0, 0x0020 },
4432 { 0x0f, 0xffff, 0x5200 },
4433 { 0x19, 0x0000, 0x0224 },
4434 { 0x00, 0x0000, 0x0004 },
4435 { 0x0c, 0x3df0, 0x0200 },
4438 rtl_hw_start_8168f(tp);
4439 rtl_pcie_state_l2l3_disable(tp);
4441 rtl_ephy_init(tp, e_info_8168f_1);
4443 rtl_eri_set_bits(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00);
4446 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
4448 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
4449 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
4451 rtl_set_def_aspm_entry_latency(tp);
4453 rtl_reset_packet_filter(tp);
4454 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
4456 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
4458 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4459 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4461 rtl8168_config_eee_mac(tp);
4463 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
4464 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
4466 rtl_pcie_state_l2l3_disable(tp);
4469 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
4471 static const struct ephy_info e_info_8168g_1[] = {
4472 { 0x00, 0x0008, 0x0000 },
4473 { 0x0c, 0x3ff0, 0x0820 },
4474 { 0x1e, 0x0000, 0x0001 },
4475 { 0x19, 0x8000, 0x0000 }
4478 rtl_hw_start_8168g(tp);
4480 /* disable aspm and clock request before access ephy */
4481 rtl_hw_aspm_clkreq_enable(tp, false);
4482 rtl_ephy_init(tp, e_info_8168g_1);
4483 rtl_hw_aspm_clkreq_enable(tp, true);
4486 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
4488 static const struct ephy_info e_info_8168g_2[] = {
4489 { 0x00, 0x0008, 0x0000 },
4490 { 0x0c, 0x3ff0, 0x0820 },
4491 { 0x19, 0xffff, 0x7c00 },
4492 { 0x1e, 0xffff, 0x20eb },
4493 { 0x0d, 0xffff, 0x1666 },
4494 { 0x00, 0xffff, 0x10a3 },
4495 { 0x06, 0xffff, 0xf050 },
4496 { 0x04, 0x0000, 0x0010 },
4497 { 0x1d, 0x4000, 0x0000 },
4500 rtl_hw_start_8168g(tp);
4502 /* disable aspm and clock request before access ephy */
4503 rtl_hw_aspm_clkreq_enable(tp, false);
4504 rtl_ephy_init(tp, e_info_8168g_2);
4507 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
4509 static const struct ephy_info e_info_8411_2[] = {
4510 { 0x00, 0x0008, 0x0000 },
4511 { 0x0c, 0x37d0, 0x0820 },
4512 { 0x1e, 0x0000, 0x0001 },
4513 { 0x19, 0x8021, 0x0000 },
4514 { 0x1e, 0x0000, 0x2000 },
4515 { 0x0d, 0x0100, 0x0200 },
4516 { 0x00, 0x0000, 0x0080 },
4517 { 0x06, 0x0000, 0x0010 },
4518 { 0x04, 0x0000, 0x0010 },
4519 { 0x1d, 0x0000, 0x4000 },
4522 rtl_hw_start_8168g(tp);
4524 /* disable aspm and clock request before access ephy */
4525 rtl_hw_aspm_clkreq_enable(tp, false);
4526 rtl_ephy_init(tp, e_info_8411_2);
4528 /* The following Realtek-provided magic fixes an issue with the RX unit
4529 * getting confused after the PHY having been powered-down.
4531 r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
4532 r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
4533 r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
4534 r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
4535 r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
4536 r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
4537 r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
4538 r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
4540 r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
4542 r8168_mac_ocp_write(tp, 0xF800, 0xE008);
4543 r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
4544 r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
4545 r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
4546 r8168_mac_ocp_write(tp, 0xF808, 0xE027);
4547 r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
4548 r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
4549 r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
4550 r8168_mac_ocp_write(tp, 0xF810, 0xC602);
4551 r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
4552 r8168_mac_ocp_write(tp, 0xF814, 0x0000);
4553 r8168_mac_ocp_write(tp, 0xF816, 0xC502);
4554 r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
4555 r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
4556 r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
4557 r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
4558 r8168_mac_ocp_write(tp, 0xF820, 0x080A);
4559 r8168_mac_ocp_write(tp, 0xF822, 0x6420);
4560 r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
4561 r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
4562 r8168_mac_ocp_write(tp, 0xF828, 0xC516);
4563 r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
4564 r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
4565 r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
4566 r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
4567 r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
4568 r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
4569 r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
4570 r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
4571 r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
4572 r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
4573 r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
4574 r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
4575 r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
4576 r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
4577 r8168_mac_ocp_write(tp, 0xF846, 0xC404);
4578 r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
4579 r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
4580 r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
4581 r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
4582 r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
4583 r8168_mac_ocp_write(tp, 0xF852, 0xE434);
4584 r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
4585 r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
4586 r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
4587 r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
4588 r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
4589 r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
4590 r8168_mac_ocp_write(tp, 0xF860, 0xF007);
4591 r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
4592 r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
4593 r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
4594 r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
4595 r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
4596 r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
4597 r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
4598 r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
4599 r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
4600 r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
4601 r8168_mac_ocp_write(tp, 0xF876, 0xC516);
4602 r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
4603 r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
4604 r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
4605 r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
4606 r8168_mac_ocp_write(tp, 0xF880, 0xC512);
4607 r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
4608 r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
4609 r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
4610 r8168_mac_ocp_write(tp, 0xF888, 0x483F);
4611 r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
4612 r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
4613 r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
4614 r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
4615 r8168_mac_ocp_write(tp, 0xF892, 0xC505);
4616 r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
4617 r8168_mac_ocp_write(tp, 0xF896, 0xC502);
4618 r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
4619 r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
4620 r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
4621 r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
4622 r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
4623 r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
4624 r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
4625 r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
4626 r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
4627 r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
4628 r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
4629 r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
4630 r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
4631 r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
4632 r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
4633 r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
4634 r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
4635 r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
4636 r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
4637 r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
4638 r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
4639 r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
4640 r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
4641 r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
4642 r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
4643 r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
4644 r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
4645 r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
4646 r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
4647 r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
4648 r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
4649 r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
4650 r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
4651 r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
4652 r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
4654 r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
4656 r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
4657 r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
4658 r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
4659 r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
4660 r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
4661 r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
4662 r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
4664 rtl_hw_aspm_clkreq_enable(tp, true);
4667 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
4669 static const struct ephy_info e_info_8168h_1[] = {
4670 { 0x1e, 0x0800, 0x0001 },
4671 { 0x1d, 0x0000, 0x0800 },
4672 { 0x05, 0xffff, 0x2089 },
4673 { 0x06, 0xffff, 0x5881 },
4674 { 0x04, 0xffff, 0x854a },
4675 { 0x01, 0xffff, 0x068b }
4679 /* disable aspm and clock request before access ephy */
4680 rtl_hw_aspm_clkreq_enable(tp, false);
4681 rtl_ephy_init(tp, e_info_8168h_1);
4683 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
4684 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
4686 rtl_set_def_aspm_entry_latency(tp);
4688 rtl_reset_packet_filter(tp);
4690 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_1111, BIT(4));
4692 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f00);
4694 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
4696 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
4698 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4699 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4701 rtl8168_config_eee_mac(tp);
4703 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4704 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
4706 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
4708 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
4710 rtl_pcie_state_l2l3_disable(tp);
4712 rtl_writephy(tp, 0x1f, 0x0c42);
4713 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
4714 rtl_writephy(tp, 0x1f, 0x0000);
4715 if (rg_saw_cnt > 0) {
4718 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
4719 sw_cnt_1ms_ini &= 0x0fff;
4720 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
4723 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
4724 r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008);
4725 r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f);
4726 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
4728 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
4729 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
4730 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
4731 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
4733 rtl_hw_aspm_clkreq_enable(tp, true);
4736 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
4738 rtl8168ep_stop_cmac(tp);
4740 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
4741 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
4743 rtl_set_def_aspm_entry_latency(tp);
4745 rtl_reset_packet_filter(tp);
4747 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f80);
4749 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
4751 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
4753 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4754 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4756 rtl8168_config_eee_mac(tp);
4758 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
4760 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
4762 rtl_pcie_state_l2l3_disable(tp);
4765 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
4767 static const struct ephy_info e_info_8168ep_1[] = {
4768 { 0x00, 0xffff, 0x10ab },
4769 { 0x06, 0xffff, 0xf030 },
4770 { 0x08, 0xffff, 0x2006 },
4771 { 0x0d, 0xffff, 0x1666 },
4772 { 0x0c, 0x3ff0, 0x0000 }
4775 /* disable aspm and clock request before access ephy */
4776 rtl_hw_aspm_clkreq_enable(tp, false);
4777 rtl_ephy_init(tp, e_info_8168ep_1);
4779 rtl_hw_start_8168ep(tp);
4781 rtl_hw_aspm_clkreq_enable(tp, true);
4784 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
4786 static const struct ephy_info e_info_8168ep_2[] = {
4787 { 0x00, 0xffff, 0x10a3 },
4788 { 0x19, 0xffff, 0xfc00 },
4789 { 0x1e, 0xffff, 0x20ea }
4792 /* disable aspm and clock request before access ephy */
4793 rtl_hw_aspm_clkreq_enable(tp, false);
4794 rtl_ephy_init(tp, e_info_8168ep_2);
4796 rtl_hw_start_8168ep(tp);
4798 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4799 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
4801 rtl_hw_aspm_clkreq_enable(tp, true);
4804 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
4806 static const struct ephy_info e_info_8168ep_3[] = {
4807 { 0x00, 0x0000, 0x0080 },
4808 { 0x0d, 0x0100, 0x0200 },
4809 { 0x19, 0x8021, 0x0000 },
4810 { 0x1e, 0x0000, 0x2000 },
4813 /* disable aspm and clock request before access ephy */
4814 rtl_hw_aspm_clkreq_enable(tp, false);
4815 rtl_ephy_init(tp, e_info_8168ep_3);
4817 rtl_hw_start_8168ep(tp);
4819 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4820 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
4822 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271);
4823 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
4824 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
4826 rtl_hw_aspm_clkreq_enable(tp, true);
4829 static void rtl_hw_start_8117(struct rtl8169_private *tp)
4831 static const struct ephy_info e_info_8117[] = {
4832 { 0x19, 0x0040, 0x1100 },
4833 { 0x59, 0x0040, 0x1100 },
4837 rtl8168ep_stop_cmac(tp);
4839 /* disable aspm and clock request before access ephy */
4840 rtl_hw_aspm_clkreq_enable(tp, false);
4841 rtl_ephy_init(tp, e_info_8117);
4843 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
4844 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
4846 rtl_set_def_aspm_entry_latency(tp);
4848 rtl_reset_packet_filter(tp);
4850 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f90);
4852 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
4854 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
4856 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4857 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4859 rtl8168_config_eee_mac(tp);
4861 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4862 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
4864 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
4866 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
4868 rtl_pcie_state_l2l3_disable(tp);
4870 rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
4871 if (rg_saw_cnt > 0) {
4874 sw_cnt_1ms_ini = (16000000 / rg_saw_cnt) & 0x0fff;
4875 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
4878 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
4879 r8168_mac_ocp_write(tp, 0xea80, 0x0003);
4880 r8168_mac_ocp_modify(tp, 0xe052, 0x0000, 0x0009);
4881 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
4883 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
4884 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
4885 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
4886 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
4888 /* firmware is for MAC only */
4889 rtl_apply_firmware(tp);
4891 rtl_hw_aspm_clkreq_enable(tp, true);
4894 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
4896 static const struct ephy_info e_info_8102e_1[] = {
4897 { 0x01, 0, 0x6e65 },
4898 { 0x02, 0, 0x091f },
4899 { 0x03, 0, 0xc2f9 },
4900 { 0x06, 0, 0xafb5 },
4901 { 0x07, 0, 0x0e00 },
4902 { 0x19, 0, 0xec80 },
4903 { 0x01, 0, 0x2e65 },
4908 rtl_set_def_aspm_entry_latency(tp);
4910 RTL_W8(tp, DBG_REG, FIX_NAK_1);
4913 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4914 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4916 cfg1 = RTL_R8(tp, Config1);
4917 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4918 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
4920 rtl_ephy_init(tp, e_info_8102e_1);
4923 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
4925 rtl_set_def_aspm_entry_latency(tp);
4927 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
4928 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4931 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
4933 rtl_hw_start_8102e_2(tp);
4935 rtl_ephy_write(tp, 0x03, 0xc2f9);
4938 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
4940 static const struct ephy_info e_info_8105e_1[] = {
4941 { 0x07, 0, 0x4000 },
4942 { 0x19, 0, 0x0200 },
4943 { 0x19, 0, 0x0020 },
4944 { 0x1e, 0, 0x2000 },
4945 { 0x03, 0, 0x0001 },
4946 { 0x19, 0, 0x0100 },
4947 { 0x19, 0, 0x0004 },
4951 /* Force LAN exit from ASPM if Rx/Tx are not idle */
4952 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
4954 /* Disable Early Tally Counter */
4955 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
4957 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
4958 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4960 rtl_ephy_init(tp, e_info_8105e_1);
4962 rtl_pcie_state_l2l3_disable(tp);
4965 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
4967 rtl_hw_start_8105e_1(tp);
4968 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
4971 static void rtl_hw_start_8402(struct rtl8169_private *tp)
4973 static const struct ephy_info e_info_8402[] = {
4974 { 0x19, 0xffff, 0xff64 },
4978 rtl_set_def_aspm_entry_latency(tp);
4980 /* Force LAN exit from ASPM if Rx/Tx are not idle */
4981 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
4983 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4985 rtl_ephy_init(tp, e_info_8402);
4987 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
4988 rtl_reset_packet_filter(tp);
4989 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4990 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4991 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00);
4993 rtl_pcie_state_l2l3_disable(tp);
4996 static void rtl_hw_start_8106(struct rtl8169_private *tp)
4998 rtl_hw_aspm_clkreq_enable(tp, false);
5000 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5001 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5003 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5004 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5005 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5007 rtl_pcie_state_l2l3_disable(tp);
5008 rtl_hw_aspm_clkreq_enable(tp, true);
5011 DECLARE_RTL_COND(rtl_mac_ocp_e00e_cond)
5013 return r8168_mac_ocp_read(tp, 0xe00e) & BIT(13);
5016 static void rtl_hw_start_8125_common(struct rtl8169_private *tp)
5018 rtl_pcie_state_l2l3_disable(tp);
5020 RTL_W16(tp, 0x382, 0x221b);
5021 RTL_W8(tp, 0x4500, 0);
5022 RTL_W16(tp, 0x4800, 0);
5025 r8168_mac_ocp_modify(tp, 0xd40a, 0x0010, 0x0000);
5027 RTL_W8(tp, Config1, RTL_R8(tp, Config1) & ~0x10);
5029 r8168_mac_ocp_write(tp, 0xc140, 0xffff);
5030 r8168_mac_ocp_write(tp, 0xc142, 0xffff);
5032 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x03a9);
5033 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
5034 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
5036 /* disable new tx descriptor format */
5037 r8168_mac_ocp_modify(tp, 0xeb58, 0x0001, 0x0000);
5039 r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0400);
5040 r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0020);
5041 r8168_mac_ocp_modify(tp, 0xc0b4, 0x0000, 0x000c);
5042 r8168_mac_ocp_modify(tp, 0xeb6a, 0x00ff, 0x0033);
5043 r8168_mac_ocp_modify(tp, 0xeb50, 0x03e0, 0x0040);
5044 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0030);
5045 r8168_mac_ocp_modify(tp, 0xe040, 0x1000, 0x0000);
5046 r8168_mac_ocp_modify(tp, 0xe0c0, 0x4f0f, 0x4403);
5047 r8168_mac_ocp_modify(tp, 0xe052, 0x0080, 0x0067);
5048 r8168_mac_ocp_modify(tp, 0xc0ac, 0x0080, 0x1f00);
5049 r8168_mac_ocp_modify(tp, 0xd430, 0x0fff, 0x047f);
5050 r8168_mac_ocp_modify(tp, 0xe84c, 0x0000, 0x00c0);
5051 r8168_mac_ocp_modify(tp, 0xea1c, 0x0004, 0x0000);
5052 r8168_mac_ocp_modify(tp, 0xeb54, 0x0000, 0x0001);
5054 r8168_mac_ocp_modify(tp, 0xeb54, 0x0001, 0x0000);
5055 RTL_W16(tp, 0x1880, RTL_R16(tp, 0x1880) & ~0x0030);
5057 r8168_mac_ocp_write(tp, 0xe098, 0xc302);
5059 rtl_udelay_loop_wait_low(tp, &rtl_mac_ocp_e00e_cond, 1000, 10);
5061 rtl8125_config_eee_mac(tp);
5063 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5067 static void rtl_hw_start_8125_1(struct rtl8169_private *tp)
5069 static const struct ephy_info e_info_8125_1[] = {
5070 { 0x01, 0xffff, 0xa812 },
5071 { 0x09, 0xffff, 0x520c },
5072 { 0x04, 0xffff, 0xd000 },
5073 { 0x0d, 0xffff, 0xf702 },
5074 { 0x0a, 0xffff, 0x8653 },
5075 { 0x06, 0xffff, 0x001e },
5076 { 0x08, 0xffff, 0x3595 },
5077 { 0x20, 0xffff, 0x9455 },
5078 { 0x21, 0xffff, 0x99ff },
5079 { 0x02, 0xffff, 0x6046 },
5080 { 0x29, 0xffff, 0xfe00 },
5081 { 0x23, 0xffff, 0xab62 },
5083 { 0x41, 0xffff, 0xa80c },
5084 { 0x49, 0xffff, 0x520c },
5085 { 0x44, 0xffff, 0xd000 },
5086 { 0x4d, 0xffff, 0xf702 },
5087 { 0x4a, 0xffff, 0x8653 },
5088 { 0x46, 0xffff, 0x001e },
5089 { 0x48, 0xffff, 0x3595 },
5090 { 0x60, 0xffff, 0x9455 },
5091 { 0x61, 0xffff, 0x99ff },
5092 { 0x42, 0xffff, 0x6046 },
5093 { 0x69, 0xffff, 0xfe00 },
5094 { 0x63, 0xffff, 0xab62 },
5097 rtl_set_def_aspm_entry_latency(tp);
5099 /* disable aspm and clock request before access ephy */
5100 rtl_hw_aspm_clkreq_enable(tp, false);
5101 rtl_ephy_init(tp, e_info_8125_1);
5103 rtl_hw_start_8125_common(tp);
5106 static void rtl_hw_start_8125_2(struct rtl8169_private *tp)
5108 static const struct ephy_info e_info_8125_2[] = {
5109 { 0x04, 0xffff, 0xd000 },
5110 { 0x0a, 0xffff, 0x8653 },
5111 { 0x23, 0xffff, 0xab66 },
5112 { 0x20, 0xffff, 0x9455 },
5113 { 0x21, 0xffff, 0x99ff },
5114 { 0x29, 0xffff, 0xfe04 },
5116 { 0x44, 0xffff, 0xd000 },
5117 { 0x4a, 0xffff, 0x8653 },
5118 { 0x63, 0xffff, 0xab66 },
5119 { 0x60, 0xffff, 0x9455 },
5120 { 0x61, 0xffff, 0x99ff },
5121 { 0x69, 0xffff, 0xfe04 },
5124 rtl_set_def_aspm_entry_latency(tp);
5126 /* disable aspm and clock request before access ephy */
5127 rtl_hw_aspm_clkreq_enable(tp, false);
5128 rtl_ephy_init(tp, e_info_8125_2);
5130 rtl_hw_start_8125_common(tp);
5133 static void rtl_hw_config(struct rtl8169_private *tp)
5135 static const rtl_generic_fct hw_configs[] = {
5136 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
5137 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
5138 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
5139 [RTL_GIGA_MAC_VER_10] = NULL,
5140 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168b,
5141 [RTL_GIGA_MAC_VER_12] = rtl_hw_start_8168b,
5142 [RTL_GIGA_MAC_VER_13] = NULL,
5143 [RTL_GIGA_MAC_VER_14] = NULL,
5144 [RTL_GIGA_MAC_VER_15] = NULL,
5145 [RTL_GIGA_MAC_VER_16] = NULL,
5146 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168b,
5147 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
5148 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
5149 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
5150 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_3,
5151 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
5152 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
5153 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
5154 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
5155 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
5156 [RTL_GIGA_MAC_VER_27] = rtl_hw_start_8168d,
5157 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
5158 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
5159 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
5160 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168d,
5161 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
5162 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
5163 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
5164 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
5165 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
5166 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
5167 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
5168 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
5169 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
5170 [RTL_GIGA_MAC_VER_41] = rtl_hw_start_8168g_1,
5171 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
5172 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
5173 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
5174 [RTL_GIGA_MAC_VER_45] = rtl_hw_start_8168h_1,
5175 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
5176 [RTL_GIGA_MAC_VER_47] = rtl_hw_start_8168h_1,
5177 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
5178 [RTL_GIGA_MAC_VER_49] = rtl_hw_start_8168ep_1,
5179 [RTL_GIGA_MAC_VER_50] = rtl_hw_start_8168ep_2,
5180 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
5181 [RTL_GIGA_MAC_VER_52] = rtl_hw_start_8117,
5182 [RTL_GIGA_MAC_VER_60] = rtl_hw_start_8125_1,
5183 [RTL_GIGA_MAC_VER_61] = rtl_hw_start_8125_2,
5186 if (hw_configs[tp->mac_version])
5187 hw_configs[tp->mac_version](tp);
5190 static void rtl_hw_start_8125(struct rtl8169_private *tp)
5194 /* disable interrupt coalescing */
5195 for (i = 0xa00; i < 0xb00; i += 4)
5201 static void rtl_hw_start_8168(struct rtl8169_private *tp)
5203 if (rtl_is_8168evl_up(tp))
5204 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5206 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5210 /* disable interrupt coalescing */
5211 RTL_W16(tp, IntrMitigate, 0x0000);
5214 static void rtl_hw_start_8169(struct rtl8169_private *tp)
5216 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
5217 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
5219 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
5221 tp->cp_cmd |= PCIMulRW;
5223 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5224 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5225 netif_dbg(tp, drv, tp->dev,
5226 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
5227 tp->cp_cmd |= (1 << 14);
5230 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5232 rtl8169_set_magic_reg(tp, tp->mac_version);
5234 RTL_W32(tp, RxMissed, 0);
5236 /* disable interrupt coalescing */
5237 RTL_W16(tp, IntrMitigate, 0x0000);
5240 static void rtl_hw_start(struct rtl8169_private *tp)
5242 rtl_unlock_config_regs(tp);
5244 tp->cp_cmd &= CPCMD_MASK;
5245 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5247 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
5248 rtl_hw_start_8169(tp);
5249 else if (rtl_is_8125(tp))
5250 rtl_hw_start_8125(tp);
5252 rtl_hw_start_8168(tp);
5254 rtl_set_rx_max_size(tp);
5255 rtl_set_rx_tx_desc_registers(tp);
5256 rtl_lock_config_regs(tp);
5258 rtl_jumbo_config(tp, tp->dev->mtu);
5260 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5261 RTL_R16(tp, CPlusCmd);
5262 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5264 rtl_set_tx_config_registers(tp);
5265 rtl_set_rx_mode(tp->dev);
5269 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5271 struct rtl8169_private *tp = netdev_priv(dev);
5273 rtl_jumbo_config(tp, new_mtu);
5276 netdev_update_features(dev);
5281 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5283 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5284 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5287 static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
5289 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5291 /* Force memory writes to complete before releasing descriptor */
5294 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
5297 static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5298 struct RxDesc *desc)
5300 struct device *d = tp_to_dev(tp);
5301 int node = dev_to_node(d);
5305 data = alloc_pages_node(node, GFP_KERNEL, get_order(R8169_RX_BUF_SIZE));
5309 mapping = dma_map_page(d, data, 0, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5310 if (unlikely(dma_mapping_error(d, mapping))) {
5311 if (net_ratelimit())
5312 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5313 __free_pages(data, get_order(R8169_RX_BUF_SIZE));
5317 desc->addr = cpu_to_le64(mapping);
5318 rtl8169_mark_to_asic(desc);
5323 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5327 for (i = 0; i < NUM_RX_DESC && tp->Rx_databuff[i]; i++) {
5328 dma_unmap_page(tp_to_dev(tp),
5329 le64_to_cpu(tp->RxDescArray[i].addr),
5330 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5331 __free_pages(tp->Rx_databuff[i], get_order(R8169_RX_BUF_SIZE));
5332 tp->Rx_databuff[i] = NULL;
5333 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5337 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5339 desc->opts1 |= cpu_to_le32(RingEnd);
5342 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5346 for (i = 0; i < NUM_RX_DESC; i++) {
5349 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5351 rtl8169_rx_clear(tp);
5354 tp->Rx_databuff[i] = data;
5357 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5362 static int rtl8169_init_ring(struct rtl8169_private *tp)
5364 rtl8169_init_ring_indexes(tp);
5366 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5367 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
5369 return rtl8169_rx_fill(tp);
5372 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5373 struct TxDesc *desc)
5375 unsigned int len = tx_skb->len;
5377 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5385 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5390 for (i = 0; i < n; i++) {
5391 unsigned int entry = (start + i) % NUM_TX_DESC;
5392 struct ring_info *tx_skb = tp->tx_skb + entry;
5393 unsigned int len = tx_skb->len;
5396 struct sk_buff *skb = tx_skb->skb;
5398 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
5399 tp->TxDescArray + entry);
5401 dev_consume_skb_any(skb);
5408 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5410 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5411 tp->cur_tx = tp->dirty_tx = 0;
5412 netdev_reset_queue(tp->dev);
5415 static void rtl_reset_work(struct rtl8169_private *tp)
5417 struct net_device *dev = tp->dev;
5420 napi_disable(&tp->napi);
5421 netif_stop_queue(dev);
5424 rtl8169_hw_reset(tp);
5426 for (i = 0; i < NUM_RX_DESC; i++)
5427 rtl8169_mark_to_asic(tp->RxDescArray + i);
5429 rtl8169_tx_clear(tp);
5430 rtl8169_init_ring_indexes(tp);
5432 napi_enable(&tp->napi);
5434 netif_wake_queue(dev);
5437 static void rtl8169_tx_timeout(struct net_device *dev)
5439 struct rtl8169_private *tp = netdev_priv(dev);
5441 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5444 static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5446 u32 status = opts0 | len;
5448 if (entry == NUM_TX_DESC - 1)
5451 return cpu_to_le32(status);
5454 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5457 struct skb_shared_info *info = skb_shinfo(skb);
5458 unsigned int cur_frag, entry;
5459 struct TxDesc *uninitialized_var(txd);
5460 struct device *d = tp_to_dev(tp);
5463 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5464 const skb_frag_t *frag = info->frags + cur_frag;
5469 entry = (entry + 1) % NUM_TX_DESC;
5471 txd = tp->TxDescArray + entry;
5472 len = skb_frag_size(frag);
5473 addr = skb_frag_address(frag);
5474 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5475 if (unlikely(dma_mapping_error(d, mapping))) {
5476 if (net_ratelimit())
5477 netif_err(tp, drv, tp->dev,
5478 "Failed to map TX fragments DMA!\n");
5482 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
5483 txd->opts2 = cpu_to_le32(opts[1]);
5484 txd->addr = cpu_to_le64(mapping);
5486 tp->tx_skb[entry].len = len;
5490 tp->tx_skb[entry].skb = skb;
5491 txd->opts1 |= cpu_to_le32(LastFrag);
5497 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5501 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5503 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5506 /* msdn_giant_send_check()
5507 * According to the document of microsoft, the TCP Pseudo Header excludes the
5508 * packet length for IPv6 TCP large packets.
5510 static int msdn_giant_send_check(struct sk_buff *skb)
5512 const struct ipv6hdr *ipv6h;
5516 ret = skb_cow_head(skb, 0);
5520 ipv6h = ipv6_hdr(skb);
5524 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5529 static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
5531 u32 mss = skb_shinfo(skb)->gso_size;
5535 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5536 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5537 const struct iphdr *ip = ip_hdr(skb);
5539 if (ip->protocol == IPPROTO_TCP)
5540 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5541 else if (ip->protocol == IPPROTO_UDP)
5542 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5548 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5549 struct sk_buff *skb, u32 *opts)
5551 u32 transport_offset = (u32)skb_transport_offset(skb);
5552 u32 mss = skb_shinfo(skb)->gso_size;
5555 switch (vlan_get_protocol(skb)) {
5556 case htons(ETH_P_IP):
5557 opts[0] |= TD1_GTSENV4;
5560 case htons(ETH_P_IPV6):
5561 if (msdn_giant_send_check(skb))
5564 opts[0] |= TD1_GTSENV6;
5572 opts[0] |= transport_offset << GTTCPHO_SHIFT;
5573 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
5574 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5577 switch (vlan_get_protocol(skb)) {
5578 case htons(ETH_P_IP):
5579 opts[1] |= TD1_IPv4_CS;
5580 ip_protocol = ip_hdr(skb)->protocol;
5583 case htons(ETH_P_IPV6):
5584 opts[1] |= TD1_IPv6_CS;
5585 ip_protocol = ipv6_hdr(skb)->nexthdr;
5589 ip_protocol = IPPROTO_RAW;
5593 if (ip_protocol == IPPROTO_TCP)
5594 opts[1] |= TD1_TCP_CS;
5595 else if (ip_protocol == IPPROTO_UDP)
5596 opts[1] |= TD1_UDP_CS;
5600 opts[1] |= transport_offset << TCPHO_SHIFT;
5602 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5603 return !eth_skb_pad(skb);
5609 static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
5610 unsigned int nr_frags)
5612 unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
5614 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
5615 return slots_avail > nr_frags;
5618 /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
5619 static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
5621 switch (tp->mac_version) {
5622 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5623 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
5630 static void rtl8169_doorbell(struct rtl8169_private *tp)
5632 if (rtl_is_8125(tp))
5633 RTL_W16(tp, TxPoll_8125, BIT(0));
5635 RTL_W8(tp, TxPoll, NPQ);
5638 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5639 struct net_device *dev)
5641 struct rtl8169_private *tp = netdev_priv(dev);
5642 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5643 struct TxDesc *txd = tp->TxDescArray + entry;
5644 struct device *d = tp_to_dev(tp);
5651 if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
5652 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5656 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5659 opts[1] = rtl8169_tx_vlan_tag(skb);
5662 if (rtl_chip_supports_csum_v2(tp)) {
5663 if (!rtl8169_tso_csum_v2(tp, skb, opts))
5666 rtl8169_tso_csum_v1(skb, opts);
5669 len = skb_headlen(skb);
5670 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
5671 if (unlikely(dma_mapping_error(d, mapping))) {
5672 if (net_ratelimit())
5673 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
5677 tp->tx_skb[entry].len = len;
5678 txd->addr = cpu_to_le64(mapping);
5680 frags = rtl8169_xmit_frags(tp, skb, opts);
5684 opts[0] |= FirstFrag;
5686 opts[0] |= FirstFrag | LastFrag;
5687 tp->tx_skb[entry].skb = skb;
5690 txd->opts2 = cpu_to_le32(opts[1]);
5692 skb_tx_timestamp(skb);
5694 /* Force memory writes to complete before releasing descriptor */
5697 door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more());
5699 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
5701 /* Force all memory writes to complete before notifying device */
5704 tp->cur_tx += frags + 1;
5706 stop_queue = !rtl_tx_slots_avail(tp, MAX_SKB_FRAGS);
5707 if (unlikely(stop_queue)) {
5708 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5709 * not miss a ring update when it notices a stopped queue.
5712 netif_stop_queue(dev);
5717 rtl8169_doorbell(tp);
5719 if (unlikely(stop_queue)) {
5720 /* Sync with rtl_tx:
5721 * - publish queue status and cur_tx ring index (write barrier)
5722 * - refresh dirty_tx ring index (read barrier).
5723 * May the current thread have a pessimistic view of the ring
5724 * status and forget to wake up queue, a racing rtl_tx thread
5728 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
5729 netif_start_queue(dev);
5732 return NETDEV_TX_OK;
5735 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
5737 dev_kfree_skb_any(skb);
5738 dev->stats.tx_dropped++;
5739 return NETDEV_TX_OK;
5742 netif_stop_queue(dev);
5743 dev->stats.tx_dropped++;
5744 return NETDEV_TX_BUSY;
5747 static netdev_features_t rtl8169_features_check(struct sk_buff *skb,
5748 struct net_device *dev,
5749 netdev_features_t features)
5751 int transport_offset = skb_transport_offset(skb);
5752 struct rtl8169_private *tp = netdev_priv(dev);
5754 if (skb_is_gso(skb)) {
5755 if (transport_offset > GTTCPHO_MAX &&
5756 rtl_chip_supports_csum_v2(tp))
5757 features &= ~NETIF_F_ALL_TSO;
5758 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5759 if (skb->len < ETH_ZLEN) {
5760 switch (tp->mac_version) {
5761 case RTL_GIGA_MAC_VER_11:
5762 case RTL_GIGA_MAC_VER_12:
5763 case RTL_GIGA_MAC_VER_17:
5764 case RTL_GIGA_MAC_VER_34:
5765 features &= ~NETIF_F_CSUM_MASK;
5772 if (transport_offset > TCPHO_MAX &&
5773 rtl_chip_supports_csum_v2(tp))
5774 features &= ~NETIF_F_CSUM_MASK;
5777 return vlan_features_check(skb, features);
5780 static void rtl8169_pcierr_interrupt(struct net_device *dev)
5782 struct rtl8169_private *tp = netdev_priv(dev);
5783 struct pci_dev *pdev = tp->pci_dev;
5784 u16 pci_status, pci_cmd;
5786 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5787 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5789 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5790 pci_cmd, pci_status);
5793 * The recovery sequence below admits a very elaborated explanation:
5794 * - it seems to work;
5795 * - I did not see what else could be done;
5796 * - it makes iop3xx happy.
5798 * Feel free to adjust to your needs.
5800 if (pdev->broken_parity_status)
5801 pci_cmd &= ~PCI_COMMAND_PARITY;
5803 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5805 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
5807 pci_write_config_word(pdev, PCI_STATUS,
5808 pci_status & (PCI_STATUS_DETECTED_PARITY |
5809 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5810 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5812 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5815 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
5818 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
5820 dirty_tx = tp->dirty_tx;
5822 tx_left = tp->cur_tx - dirty_tx;
5824 while (tx_left > 0) {
5825 unsigned int entry = dirty_tx % NUM_TX_DESC;
5826 struct ring_info *tx_skb = tp->tx_skb + entry;
5829 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5830 if (status & DescOwn)
5833 /* This barrier is needed to keep us from reading
5834 * any other fields out of the Tx descriptor until
5835 * we know the status of DescOwn
5839 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
5840 tp->TxDescArray + entry);
5843 bytes_compl += tx_skb->skb->len;
5844 napi_consume_skb(tx_skb->skb, budget);
5851 if (tp->dirty_tx != dirty_tx) {
5852 netdev_completed_queue(dev, pkts_compl, bytes_compl);
5854 u64_stats_update_begin(&tp->tx_stats.syncp);
5855 tp->tx_stats.packets += pkts_compl;
5856 tp->tx_stats.bytes += bytes_compl;
5857 u64_stats_update_end(&tp->tx_stats.syncp);
5859 tp->dirty_tx = dirty_tx;
5860 /* Sync with rtl8169_start_xmit:
5861 * - publish dirty_tx ring index (write barrier)
5862 * - refresh cur_tx ring index and queue status (read barrier)
5863 * May the current thread miss the stopped queue condition,
5864 * a racing xmit thread can only have a right view of the
5868 if (netif_queue_stopped(dev) &&
5869 rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
5870 netif_wake_queue(dev);
5873 * 8168 hack: TxPoll requests are lost when the Tx packets are
5874 * too close. Let's kick an extra TxPoll request when a burst
5875 * of start_xmit activity is detected (if it is not detected,
5876 * it is slow enough). -- FR
5878 if (tp->cur_tx != dirty_tx)
5879 rtl8169_doorbell(tp);
5883 static inline int rtl8169_fragmented_frame(u32 status)
5885 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5888 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
5890 u32 status = opts1 & RxProtoMask;
5892 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
5893 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5894 skb->ip_summed = CHECKSUM_UNNECESSARY;
5896 skb_checksum_none_assert(skb);
5899 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
5901 unsigned int cur_rx, rx_left;
5904 cur_rx = tp->cur_rx;
5906 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
5907 unsigned int entry = cur_rx % NUM_RX_DESC;
5908 const void *rx_buf = page_address(tp->Rx_databuff[entry]);
5909 struct RxDesc *desc = tp->RxDescArray + entry;
5912 status = le32_to_cpu(desc->opts1);
5913 if (status & DescOwn)
5916 /* This barrier is needed to keep us from reading
5917 * any other fields out of the Rx descriptor until
5918 * we know the status of DescOwn
5922 if (unlikely(status & RxRES)) {
5923 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5925 dev->stats.rx_errors++;
5926 if (status & (RxRWT | RxRUNT))
5927 dev->stats.rx_length_errors++;
5929 dev->stats.rx_crc_errors++;
5930 if (status & (RxRUNT | RxCRC) && !(status & RxRWT) &&
5931 dev->features & NETIF_F_RXALL) {
5935 unsigned int pkt_size;
5936 struct sk_buff *skb;
5939 pkt_size = status & GENMASK(13, 0);
5940 if (likely(!(dev->features & NETIF_F_RXFCS)))
5941 pkt_size -= ETH_FCS_LEN;
5943 * The driver does not support incoming fragmented
5944 * frames. They are seen as a symptom of over-mtu
5947 if (unlikely(rtl8169_fragmented_frame(status))) {
5948 dev->stats.rx_dropped++;
5949 dev->stats.rx_length_errors++;
5950 goto release_descriptor;
5953 skb = napi_alloc_skb(&tp->napi, pkt_size);
5954 if (unlikely(!skb)) {
5955 dev->stats.rx_dropped++;
5956 goto release_descriptor;
5959 dma_sync_single_for_cpu(tp_to_dev(tp),
5960 le64_to_cpu(desc->addr),
5961 pkt_size, DMA_FROM_DEVICE);
5963 skb_copy_to_linear_data(skb, rx_buf, pkt_size);
5964 skb->tail += pkt_size;
5965 skb->len = pkt_size;
5967 dma_sync_single_for_device(tp_to_dev(tp),
5968 le64_to_cpu(desc->addr),
5969 pkt_size, DMA_FROM_DEVICE);
5971 rtl8169_rx_csum(skb, status);
5972 skb->protocol = eth_type_trans(skb, dev);
5974 rtl8169_rx_vlan_tag(desc, skb);
5976 if (skb->pkt_type == PACKET_MULTICAST)
5977 dev->stats.multicast++;
5979 napi_gro_receive(&tp->napi, skb);
5981 u64_stats_update_begin(&tp->rx_stats.syncp);
5982 tp->rx_stats.packets++;
5983 tp->rx_stats.bytes += pkt_size;
5984 u64_stats_update_end(&tp->rx_stats.syncp);
5988 rtl8169_mark_to_asic(desc);
5991 count = cur_rx - tp->cur_rx;
5992 tp->cur_rx = cur_rx;
5997 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5999 struct rtl8169_private *tp = dev_instance;
6000 u32 status = rtl_get_events(tp);
6002 if (!tp->irq_enabled || (status & 0xffff) == 0xffff ||
6003 !(status & tp->irq_mask))
6006 if (unlikely(status & SYSErr)) {
6007 rtl8169_pcierr_interrupt(tp->dev);
6011 if (status & LinkChg)
6012 phy_mac_interrupt(tp->phydev);
6014 if (unlikely(status & RxFIFOOver &&
6015 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
6016 netif_stop_queue(tp->dev);
6017 /* XXX - Hack alert. See rtl_task(). */
6018 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6021 rtl_irq_disable(tp);
6022 napi_schedule_irqoff(&tp->napi);
6024 rtl_ack_events(tp, status);
6029 static void rtl_task(struct work_struct *work)
6031 static const struct {
6033 void (*action)(struct rtl8169_private *);
6035 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6037 struct rtl8169_private *tp =
6038 container_of(work, struct rtl8169_private, wk.work);
6039 struct net_device *dev = tp->dev;
6044 if (!netif_running(dev) ||
6045 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6048 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6051 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6053 rtl_work[i].action(tp);
6057 rtl_unlock_work(tp);
6060 static int rtl8169_poll(struct napi_struct *napi, int budget)
6062 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6063 struct net_device *dev = tp->dev;
6066 work_done = rtl_rx(dev, tp, (u32) budget);
6068 rtl_tx(dev, tp, budget);
6070 if (work_done < budget) {
6071 napi_complete_done(napi, work_done);
6078 static void rtl8169_rx_missed(struct net_device *dev)
6080 struct rtl8169_private *tp = netdev_priv(dev);
6082 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6085 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6086 RTL_W32(tp, RxMissed, 0);
6089 static void r8169_phylink_handler(struct net_device *ndev)
6091 struct rtl8169_private *tp = netdev_priv(ndev);
6093 if (netif_carrier_ok(ndev)) {
6094 rtl_link_chg_patch(tp);
6095 pm_request_resume(&tp->pci_dev->dev);
6097 pm_runtime_idle(&tp->pci_dev->dev);
6100 if (net_ratelimit())
6101 phy_print_status(tp->phydev);
6104 static int r8169_phy_connect(struct rtl8169_private *tp)
6106 struct phy_device *phydev = tp->phydev;
6107 phy_interface_t phy_mode;
6110 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
6111 PHY_INTERFACE_MODE_MII;
6113 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6118 if (!tp->supports_gmii)
6119 phy_set_max_speed(phydev, SPEED_100);
6121 phy_support_asym_pause(phydev);
6123 phy_attached_info(phydev);
6128 static void rtl8169_down(struct net_device *dev)
6130 struct rtl8169_private *tp = netdev_priv(dev);
6132 phy_stop(tp->phydev);
6134 napi_disable(&tp->napi);
6135 netif_stop_queue(dev);
6137 rtl8169_hw_reset(tp);
6139 * At this point device interrupts can not be enabled in any function,
6140 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6141 * and napi is disabled (rtl8169_poll).
6143 rtl8169_rx_missed(dev);
6145 /* Give a racing hard_start_xmit a few cycles to complete. */
6148 rtl8169_tx_clear(tp);
6150 rtl8169_rx_clear(tp);
6152 rtl_pll_power_down(tp);
6155 static int rtl8169_close(struct net_device *dev)
6157 struct rtl8169_private *tp = netdev_priv(dev);
6158 struct pci_dev *pdev = tp->pci_dev;
6160 pm_runtime_get_sync(&pdev->dev);
6162 /* Update counters before going down */
6163 rtl8169_update_counters(tp);
6166 /* Clear all task flags */
6167 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6170 rtl_unlock_work(tp);
6172 cancel_work_sync(&tp->wk.work);
6174 phy_disconnect(tp->phydev);
6176 pci_free_irq(pdev, 0, tp);
6178 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6180 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6182 tp->TxDescArray = NULL;
6183 tp->RxDescArray = NULL;
6185 pm_runtime_put_sync(&pdev->dev);
6190 #ifdef CONFIG_NET_POLL_CONTROLLER
6191 static void rtl8169_netpoll(struct net_device *dev)
6193 struct rtl8169_private *tp = netdev_priv(dev);
6195 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
6199 static int rtl_open(struct net_device *dev)
6201 struct rtl8169_private *tp = netdev_priv(dev);
6202 struct pci_dev *pdev = tp->pci_dev;
6203 int retval = -ENOMEM;
6205 pm_runtime_get_sync(&pdev->dev);
6208 * Rx and Tx descriptors needs 256 bytes alignment.
6209 * dma_alloc_coherent provides more.
6211 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6212 &tp->TxPhyAddr, GFP_KERNEL);
6213 if (!tp->TxDescArray)
6214 goto err_pm_runtime_put;
6216 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6217 &tp->RxPhyAddr, GFP_KERNEL);
6218 if (!tp->RxDescArray)
6221 retval = rtl8169_init_ring(tp);
6225 rtl_request_firmware(tp);
6227 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
6230 goto err_release_fw_2;
6232 retval = r8169_phy_connect(tp);
6238 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6240 napi_enable(&tp->napi);
6242 rtl8169_init_phy(dev, tp);
6244 rtl_pll_power_up(tp);
6248 if (!rtl8169_init_counter_offsets(tp))
6249 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6251 phy_start(tp->phydev);
6252 netif_start_queue(dev);
6254 rtl_unlock_work(tp);
6256 pm_runtime_put_sync(&pdev->dev);
6261 pci_free_irq(pdev, 0, tp);
6263 rtl_release_firmware(tp);
6264 rtl8169_rx_clear(tp);
6266 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6268 tp->RxDescArray = NULL;
6270 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6272 tp->TxDescArray = NULL;
6274 pm_runtime_put_noidle(&pdev->dev);
6279 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6281 struct rtl8169_private *tp = netdev_priv(dev);
6282 struct pci_dev *pdev = tp->pci_dev;
6283 struct rtl8169_counters *counters = tp->counters;
6286 pm_runtime_get_noresume(&pdev->dev);
6288 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
6289 rtl8169_rx_missed(dev);
6292 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
6293 stats->rx_packets = tp->rx_stats.packets;
6294 stats->rx_bytes = tp->rx_stats.bytes;
6295 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
6298 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
6299 stats->tx_packets = tp->tx_stats.packets;
6300 stats->tx_bytes = tp->tx_stats.bytes;
6301 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
6303 stats->rx_dropped = dev->stats.rx_dropped;
6304 stats->tx_dropped = dev->stats.tx_dropped;
6305 stats->rx_length_errors = dev->stats.rx_length_errors;
6306 stats->rx_errors = dev->stats.rx_errors;
6307 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6308 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6309 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6310 stats->multicast = dev->stats.multicast;
6313 * Fetch additional counter values missing in stats collected by driver
6314 * from tally counters.
6316 if (pm_runtime_active(&pdev->dev))
6317 rtl8169_update_counters(tp);
6320 * Subtract values fetched during initalization.
6321 * See rtl8169_init_counter_offsets for a description why we do that.
6323 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
6324 le64_to_cpu(tp->tc_offset.tx_errors);
6325 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
6326 le32_to_cpu(tp->tc_offset.tx_multi_collision);
6327 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
6328 le16_to_cpu(tp->tc_offset.tx_aborted);
6330 pm_runtime_put_noidle(&pdev->dev);
6333 static void rtl8169_net_suspend(struct net_device *dev)
6335 struct rtl8169_private *tp = netdev_priv(dev);
6337 if (!netif_running(dev))
6340 phy_stop(tp->phydev);
6341 netif_device_detach(dev);
6344 napi_disable(&tp->napi);
6345 /* Clear all task flags */
6346 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6348 rtl_unlock_work(tp);
6350 rtl_pll_power_down(tp);
6355 static int rtl8169_suspend(struct device *device)
6357 struct net_device *dev = dev_get_drvdata(device);
6358 struct rtl8169_private *tp = netdev_priv(dev);
6360 rtl8169_net_suspend(dev);
6361 clk_disable_unprepare(tp->clk);
6366 static void __rtl8169_resume(struct net_device *dev)
6368 struct rtl8169_private *tp = netdev_priv(dev);
6370 netif_device_attach(dev);
6372 rtl_pll_power_up(tp);
6373 rtl8169_init_phy(dev, tp);
6375 phy_start(tp->phydev);
6378 napi_enable(&tp->napi);
6379 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6381 rtl_unlock_work(tp);
6384 static int rtl8169_resume(struct device *device)
6386 struct net_device *dev = dev_get_drvdata(device);
6387 struct rtl8169_private *tp = netdev_priv(dev);
6389 rtl_rar_set(tp, dev->dev_addr);
6391 clk_prepare_enable(tp->clk);
6393 if (netif_running(dev))
6394 __rtl8169_resume(dev);
6399 static int rtl8169_runtime_suspend(struct device *device)
6401 struct net_device *dev = dev_get_drvdata(device);
6402 struct rtl8169_private *tp = netdev_priv(dev);
6404 if (!tp->TxDescArray)
6408 __rtl8169_set_wol(tp, WAKE_ANY);
6409 rtl_unlock_work(tp);
6411 rtl8169_net_suspend(dev);
6413 /* Update counters before going runtime suspend */
6414 rtl8169_rx_missed(dev);
6415 rtl8169_update_counters(tp);
6420 static int rtl8169_runtime_resume(struct device *device)
6422 struct net_device *dev = dev_get_drvdata(device);
6423 struct rtl8169_private *tp = netdev_priv(dev);
6425 rtl_rar_set(tp, dev->dev_addr);
6427 if (!tp->TxDescArray)
6431 __rtl8169_set_wol(tp, tp->saved_wolopts);
6432 rtl_unlock_work(tp);
6434 __rtl8169_resume(dev);
6439 static int rtl8169_runtime_idle(struct device *device)
6441 struct net_device *dev = dev_get_drvdata(device);
6443 if (!netif_running(dev) || !netif_carrier_ok(dev))
6444 pm_schedule_suspend(device, 10000);
6449 static const struct dev_pm_ops rtl8169_pm_ops = {
6450 .suspend = rtl8169_suspend,
6451 .resume = rtl8169_resume,
6452 .freeze = rtl8169_suspend,
6453 .thaw = rtl8169_resume,
6454 .poweroff = rtl8169_suspend,
6455 .restore = rtl8169_resume,
6456 .runtime_suspend = rtl8169_runtime_suspend,
6457 .runtime_resume = rtl8169_runtime_resume,
6458 .runtime_idle = rtl8169_runtime_idle,
6461 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6463 #else /* !CONFIG_PM */
6465 #define RTL8169_PM_OPS NULL
6467 #endif /* !CONFIG_PM */
6469 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6471 /* WoL fails with 8168b when the receiver is disabled. */
6472 switch (tp->mac_version) {
6473 case RTL_GIGA_MAC_VER_11:
6474 case RTL_GIGA_MAC_VER_12:
6475 case RTL_GIGA_MAC_VER_17:
6476 pci_clear_master(tp->pci_dev);
6478 RTL_W8(tp, ChipCmd, CmdRxEnb);
6480 RTL_R8(tp, ChipCmd);
6487 static void rtl_shutdown(struct pci_dev *pdev)
6489 struct net_device *dev = pci_get_drvdata(pdev);
6490 struct rtl8169_private *tp = netdev_priv(dev);
6492 rtl8169_net_suspend(dev);
6494 /* Restore original MAC address */
6495 rtl_rar_set(tp, dev->perm_addr);
6497 rtl8169_hw_reset(tp);
6499 if (system_state == SYSTEM_POWER_OFF) {
6500 if (tp->saved_wolopts) {
6501 rtl_wol_suspend_quirk(tp);
6502 rtl_wol_shutdown_quirk(tp);
6505 pci_wake_from_d3(pdev, true);
6506 pci_set_power_state(pdev, PCI_D3hot);
6510 static void rtl_remove_one(struct pci_dev *pdev)
6512 struct net_device *dev = pci_get_drvdata(pdev);
6513 struct rtl8169_private *tp = netdev_priv(dev);
6515 if (r8168_check_dash(tp))
6516 rtl8168_driver_stop(tp);
6518 netif_napi_del(&tp->napi);
6520 unregister_netdev(dev);
6521 mdiobus_unregister(tp->phydev->mdio.bus);
6523 rtl_release_firmware(tp);
6525 if (pci_dev_run_wake(pdev))
6526 pm_runtime_get_noresume(&pdev->dev);
6528 /* restore original MAC address */
6529 rtl_rar_set(tp, dev->perm_addr);
6532 static const struct net_device_ops rtl_netdev_ops = {
6533 .ndo_open = rtl_open,
6534 .ndo_stop = rtl8169_close,
6535 .ndo_get_stats64 = rtl8169_get_stats64,
6536 .ndo_start_xmit = rtl8169_start_xmit,
6537 .ndo_features_check = rtl8169_features_check,
6538 .ndo_tx_timeout = rtl8169_tx_timeout,
6539 .ndo_validate_addr = eth_validate_addr,
6540 .ndo_change_mtu = rtl8169_change_mtu,
6541 .ndo_fix_features = rtl8169_fix_features,
6542 .ndo_set_features = rtl8169_set_features,
6543 .ndo_set_mac_address = rtl_set_mac_address,
6544 .ndo_do_ioctl = rtl8169_ioctl,
6545 .ndo_set_rx_mode = rtl_set_rx_mode,
6546 #ifdef CONFIG_NET_POLL_CONTROLLER
6547 .ndo_poll_controller = rtl8169_netpoll,
6552 static void rtl_set_irq_mask(struct rtl8169_private *tp)
6554 tp->irq_mask = RTL_EVENT_NAPI | LinkChg;
6556 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6557 tp->irq_mask |= SYSErr | RxOverflow | RxFIFOOver;
6558 else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
6559 /* special workaround needed */
6560 tp->irq_mask |= RxFIFOOver;
6562 tp->irq_mask |= RxOverflow;
6565 static int rtl_alloc_irq(struct rtl8169_private *tp)
6569 switch (tp->mac_version) {
6570 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
6571 rtl_unlock_config_regs(tp);
6572 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
6573 rtl_lock_config_regs(tp);
6575 case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_24:
6576 flags = PCI_IRQ_LEGACY;
6579 flags = PCI_IRQ_ALL_TYPES;
6583 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
6586 static void rtl_read_mac_address(struct rtl8169_private *tp,
6587 u8 mac_addr[ETH_ALEN])
6589 /* Get MAC address */
6590 if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) {
6591 u32 value = rtl_eri_read(tp, 0xe0);
6593 mac_addr[0] = (value >> 0) & 0xff;
6594 mac_addr[1] = (value >> 8) & 0xff;
6595 mac_addr[2] = (value >> 16) & 0xff;
6596 mac_addr[3] = (value >> 24) & 0xff;
6598 value = rtl_eri_read(tp, 0xe4);
6599 mac_addr[4] = (value >> 0) & 0xff;
6600 mac_addr[5] = (value >> 8) & 0xff;
6601 } else if (rtl_is_8125(tp)) {
6602 rtl_read_mac_from_reg(tp, mac_addr, MAC0_BKP);
6606 DECLARE_RTL_COND(rtl_link_list_ready_cond)
6608 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
6611 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6613 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
6616 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
6618 struct rtl8169_private *tp = mii_bus->priv;
6623 return rtl_readphy(tp, phyreg);
6626 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
6627 int phyreg, u16 val)
6629 struct rtl8169_private *tp = mii_bus->priv;
6634 rtl_writephy(tp, phyreg, val);
6639 static int r8169_mdio_register(struct rtl8169_private *tp)
6641 struct pci_dev *pdev = tp->pci_dev;
6642 struct mii_bus *new_bus;
6645 new_bus = devm_mdiobus_alloc(&pdev->dev);
6649 new_bus->name = "r8169";
6651 new_bus->parent = &pdev->dev;
6652 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
6653 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x", pci_dev_id(pdev));
6655 new_bus->read = r8169_mdio_read_reg;
6656 new_bus->write = r8169_mdio_write_reg;
6658 ret = mdiobus_register(new_bus);
6662 tp->phydev = mdiobus_get_phy(new_bus, 0);
6664 mdiobus_unregister(new_bus);
6668 /* PHY will be woken up in rtl_open() */
6669 phy_suspend(tp->phydev);
6674 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
6676 tp->ocp_base = OCP_STD_PHY_BASE;
6678 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
6680 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6683 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6686 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6688 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
6690 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
6692 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6695 r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15));
6697 rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
6700 static void rtl_hw_init_8125(struct rtl8169_private *tp)
6702 tp->ocp_base = OCP_STD_PHY_BASE;
6704 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
6706 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6709 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6711 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
6713 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
6715 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6718 r8168_mac_ocp_write(tp, 0xc0aa, 0x07d0);
6719 r8168_mac_ocp_write(tp, 0xc0a6, 0x0150);
6720 r8168_mac_ocp_write(tp, 0xc01e, 0x5555);
6722 rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
6725 static void rtl_hw_initialize(struct rtl8169_private *tp)
6727 switch (tp->mac_version) {
6728 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_52:
6729 rtl8168ep_stop_cmac(tp);
6731 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
6732 rtl_hw_init_8168g(tp);
6734 case RTL_GIGA_MAC_VER_60 ... RTL_GIGA_MAC_VER_61:
6735 rtl_hw_init_8125(tp);
6742 static int rtl_jumbo_max(struct rtl8169_private *tp)
6744 /* Non-GBit versions don't support jumbo frames */
6745 if (!tp->supports_gmii)
6748 switch (tp->mac_version) {
6750 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
6753 case RTL_GIGA_MAC_VER_11:
6754 case RTL_GIGA_MAC_VER_12:
6755 case RTL_GIGA_MAC_VER_17:
6758 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
6765 static void rtl_disable_clk(void *data)
6767 clk_disable_unprepare(data);
6770 static int rtl_get_ether_clk(struct rtl8169_private *tp)
6772 struct device *d = tp_to_dev(tp);
6776 clk = devm_clk_get(d, "ether_clk");
6780 /* clk-core allows NULL (for suspend / resume) */
6782 else if (rc != -EPROBE_DEFER)
6783 dev_err(d, "failed to get clk: %d\n", rc);
6786 rc = clk_prepare_enable(clk);
6788 dev_err(d, "failed to enable clk: %d\n", rc);
6790 rc = devm_add_action_or_reset(d, rtl_disable_clk, clk);
6796 static void rtl_init_mac_address(struct rtl8169_private *tp)
6798 struct net_device *dev = tp->dev;
6799 u8 *mac_addr = dev->dev_addr;
6802 rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
6806 rtl_read_mac_address(tp, mac_addr);
6807 if (is_valid_ether_addr(mac_addr))
6810 rtl_read_mac_from_reg(tp, mac_addr, MAC0);
6811 if (is_valid_ether_addr(mac_addr))
6814 eth_hw_addr_random(dev);
6815 dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
6817 rtl_rar_set(tp, mac_addr);
6820 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6822 struct rtl8169_private *tp;
6823 struct net_device *dev;
6824 int chipset, region;
6827 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
6831 SET_NETDEV_DEV(dev, &pdev->dev);
6832 dev->netdev_ops = &rtl_netdev_ops;
6833 tp = netdev_priv(dev);
6836 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6837 tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
6840 /* Get the *optional* external "ether_clk" used on some boards */
6841 rc = rtl_get_ether_clk(tp);
6845 /* Disable ASPM completely as that cause random device stop working
6846 * problems as well as full system hangs for some PCIe devices users.
6848 rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
6849 PCIE_LINK_STATE_L1);
6850 tp->aspm_manageable = !rc;
6852 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6853 rc = pcim_enable_device(pdev);
6855 dev_err(&pdev->dev, "enable failure\n");
6859 if (pcim_set_mwi(pdev) < 0)
6860 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
6862 /* use first MMIO region */
6863 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
6865 dev_err(&pdev->dev, "no MMIO resource found\n");
6869 /* check for weird/broken PCI region reporting */
6870 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6871 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
6875 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
6877 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
6881 tp->mmio_addr = pcim_iomap_table(pdev)[region];
6883 /* Identify chip attached to board */
6884 rtl8169_get_mac_version(tp);
6885 if (tp->mac_version == RTL_GIGA_MAC_NONE)
6888 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
6890 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
6891 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
6892 dev->features |= NETIF_F_HIGHDMA;
6896 rtl8169_irq_mask_and_ack(tp);
6898 rtl_hw_initialize(tp);
6902 pci_set_master(pdev);
6904 chipset = tp->mac_version;
6906 rc = rtl_alloc_irq(tp);
6908 dev_err(&pdev->dev, "Can't allocate interrupt\n");
6912 mutex_init(&tp->wk.mutex);
6913 INIT_WORK(&tp->wk.work, rtl_task);
6914 u64_stats_init(&tp->rx_stats.syncp);
6915 u64_stats_init(&tp->tx_stats.syncp);
6917 rtl_init_mac_address(tp);
6919 dev->ethtool_ops = &rtl8169_ethtool_ops;
6921 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
6923 dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6924 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6925 NETIF_F_HW_VLAN_CTAG_RX;
6926 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6927 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6928 NETIF_F_HW_VLAN_CTAG_RX;
6929 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6931 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
6933 tp->cp_cmd |= RxChkSum;
6934 /* RTL8125 uses register RxConfig for VLAN offloading config */
6935 if (!rtl_is_8125(tp))
6936 tp->cp_cmd |= RxVlan;
6938 * Pretend we are using VLANs; This bypasses a nasty bug where
6939 * Interrupts stop flowing on high load on 8110SCd controllers.
6941 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6942 /* Disallow toggling */
6943 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
6945 if (rtl_chip_supports_csum_v2(tp)) {
6946 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
6947 dev->features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
6948 dev->gso_max_size = RTL_GSO_MAX_SIZE_V2;
6949 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V2;
6951 dev->gso_max_size = RTL_GSO_MAX_SIZE_V1;
6952 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V1;
6955 /* RTL8168e-vl and one RTL8168c variant are known to have a
6956 * HW issue with TSO.
6958 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
6959 tp->mac_version == RTL_GIGA_MAC_VER_22) {
6960 dev->vlan_features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6961 dev->hw_features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6962 dev->features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6965 dev->hw_features |= NETIF_F_RXALL;
6966 dev->hw_features |= NETIF_F_RXFCS;
6968 /* MTU range: 60 - hw-specific max */
6969 dev->min_mtu = ETH_ZLEN;
6970 jumbo_max = rtl_jumbo_max(tp);
6971 dev->max_mtu = jumbo_max;
6973 rtl_set_irq_mask(tp);
6975 tp->fw_name = rtl_chip_infos[chipset].fw_name;
6977 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
6978 &tp->counters_phys_addr,
6983 pci_set_drvdata(pdev, dev);
6985 rc = r8169_mdio_register(tp);
6989 /* chip gets powered up in rtl_open() */
6990 rtl_pll_power_down(tp);
6992 rc = register_netdev(dev);
6994 goto err_mdio_unregister;
6996 netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
6997 rtl_chip_infos[chipset].name, dev->dev_addr,
6998 (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
6999 pci_irq_vector(pdev, 0));
7001 if (jumbo_max > JUMBO_1K)
7002 netif_info(tp, probe, dev,
7003 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7004 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
7007 if (r8168_check_dash(tp))
7008 rtl8168_driver_start(tp);
7010 if (pci_dev_run_wake(pdev))
7011 pm_runtime_put_sync(&pdev->dev);
7015 err_mdio_unregister:
7016 mdiobus_unregister(tp->phydev->mdio.bus);
7020 static struct pci_driver rtl8169_pci_driver = {
7022 .id_table = rtl8169_pci_tbl,
7023 .probe = rtl_init_one,
7024 .remove = rtl_remove_one,
7025 .shutdown = rtl_shutdown,
7026 .driver.pm = RTL8169_PM_OPS,
7029 module_pci_driver(rtl8169_pci_driver);