2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/ethtool.h>
19 #include <linux/phy.h>
20 #include <linux/if_vlan.h>
21 #include <linux/crc32.h>
25 #include <linux/tcp.h>
26 #include <linux/interrupt.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/firmware.h>
30 #include <linux/prefetch.h>
31 #include <linux/ipv6.h>
32 #include <net/ip6_checksum.h>
34 #define MODULENAME "r8169"
36 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
37 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
38 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
39 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
40 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
41 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
42 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
43 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
44 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
45 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
46 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
47 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
48 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
49 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
50 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
51 #define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
52 #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
53 #define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
54 #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
56 #define R8169_MSG_DEFAULT \
57 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
59 #define TX_SLOTS_AVAIL(tp) \
60 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
62 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
63 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
64 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
66 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
67 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
68 static const int multicast_filter_limit = 32;
70 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
71 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
73 #define R8169_REGS_SIZE 256
74 #define R8169_RX_BUF_SIZE (SZ_16K - 1)
75 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
76 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
77 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
78 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
80 /* write/read MMIO register */
81 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
82 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
83 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
84 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
85 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
86 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
89 RTL_GIGA_MAC_VER_01 = 0,
140 RTL_GIGA_MAC_NONE = 0xff,
143 #define JUMBO_1K ETH_DATA_LEN
144 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
145 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
146 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
147 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
149 static const struct {
152 } rtl_chip_infos[] = {
154 [RTL_GIGA_MAC_VER_01] = {"RTL8169" },
155 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
156 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
157 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
158 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
159 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
161 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
162 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
163 [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
164 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
165 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
166 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
167 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
168 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
169 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
170 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
171 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
172 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
173 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
174 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
175 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
176 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
177 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
178 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
179 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
180 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
181 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
182 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
183 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
184 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
185 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
186 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
187 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
188 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
189 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
190 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
191 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
192 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
193 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
194 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
195 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
196 [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
197 [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
198 [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
199 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
200 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
201 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
202 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
203 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
204 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
205 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
214 static const struct pci_device_id rtl8169_pci_tbl[] = {
215 { PCI_VDEVICE(REALTEK, 0x8129), RTL_CFG_0 },
216 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_2 },
217 { PCI_VDEVICE(REALTEK, 0x8161), RTL_CFG_1 },
218 { PCI_VDEVICE(REALTEK, 0x8167), RTL_CFG_0 },
219 { PCI_VDEVICE(REALTEK, 0x8168), RTL_CFG_1 },
220 { PCI_VDEVICE(NCUBE, 0x8168), RTL_CFG_1 },
221 { PCI_VDEVICE(REALTEK, 0x8169), RTL_CFG_0 },
222 { PCI_VENDOR_ID_DLINK, 0x4300,
223 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
224 { PCI_VDEVICE(DLINK, 0x4300), RTL_CFG_0 },
225 { PCI_VDEVICE(DLINK, 0x4302), RTL_CFG_0 },
226 { PCI_VDEVICE(AT, 0xc107), RTL_CFG_0 },
227 { PCI_VDEVICE(USR, 0x0116), RTL_CFG_0 },
228 { PCI_VENDOR_ID_LINKSYS, 0x1032,
229 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
231 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
235 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
237 static int use_dac = -1;
243 MAC0 = 0, /* Ethernet hardware address. */
245 MAR0 = 8, /* Multicast filter. */
246 CounterAddrLow = 0x10,
247 CounterAddrHigh = 0x14,
248 TxDescStartAddrLow = 0x20,
249 TxDescStartAddrHigh = 0x24,
250 TxHDescStartAddrLow = 0x28,
251 TxHDescStartAddrHigh = 0x2c,
260 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
261 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
264 #define RX128_INT_EN (1 << 15) /* 8111c and later */
265 #define RX_MULTI_EN (1 << 14) /* 8111c only */
266 #define RXCFG_FIFO_SHIFT 13
267 /* No threshold before first PCI xfer */
268 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
269 #define RX_EARLY_OFF (1 << 11)
270 #define RXCFG_DMA_SHIFT 8
271 /* Unlimited maximum PCI burst. */
272 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
279 #define PME_SIGNAL (1 << 5) /* 8168c and later */
291 #define RTL_COALESCE_MASK 0x0f
292 #define RTL_COALESCE_SHIFT 4
293 #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
294 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
296 RxDescAddrLow = 0xe4,
297 RxDescAddrHigh = 0xe8,
298 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
300 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
302 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
304 #define TxPacketMax (8064 >> 7)
305 #define EarlySize 0x27
308 FuncEventMask = 0xf4,
309 FuncPresetState = 0xf8,
314 FuncForceEvent = 0xfc,
317 enum rtl8168_8101_registers {
320 #define CSIAR_FLAG 0x80000000
321 #define CSIAR_WRITE_CMD 0x80000000
322 #define CSIAR_BYTE_ENABLE 0x0000f000
323 #define CSIAR_ADDR_MASK 0x00000fff
326 #define EPHYAR_FLAG 0x80000000
327 #define EPHYAR_WRITE_CMD 0x80000000
328 #define EPHYAR_REG_MASK 0x1f
329 #define EPHYAR_REG_SHIFT 16
330 #define EPHYAR_DATA_MASK 0xffff
332 #define PFM_EN (1 << 6)
333 #define TX_10M_PS_EN (1 << 7)
335 #define FIX_NAK_1 (1 << 4)
336 #define FIX_NAK_2 (1 << 3)
339 #define NOW_IS_OOB (1 << 7)
340 #define TX_EMPTY (1 << 5)
341 #define RX_EMPTY (1 << 4)
342 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
343 #define EN_NDP (1 << 3)
344 #define EN_OOB_RESET (1 << 2)
345 #define LINK_LIST_RDY (1 << 1)
347 #define EFUSEAR_FLAG 0x80000000
348 #define EFUSEAR_WRITE_CMD 0x80000000
349 #define EFUSEAR_READ_CMD 0x00000000
350 #define EFUSEAR_REG_MASK 0x03ff
351 #define EFUSEAR_REG_SHIFT 8
352 #define EFUSEAR_DATA_MASK 0xff
354 #define PFM_D3COLD_EN (1 << 6)
357 enum rtl8168_registers {
362 #define ERIAR_FLAG 0x80000000
363 #define ERIAR_WRITE_CMD 0x80000000
364 #define ERIAR_READ_CMD 0x00000000
365 #define ERIAR_ADDR_BYTE_ALIGN 4
366 #define ERIAR_TYPE_SHIFT 16
367 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
368 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
369 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
370 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
371 #define ERIAR_MASK_SHIFT 12
372 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
373 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
374 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
375 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
376 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
377 EPHY_RXER_NUM = 0x7c,
378 OCPDR = 0xb0, /* OCP GPHY access */
379 #define OCPDR_WRITE_CMD 0x80000000
380 #define OCPDR_READ_CMD 0x00000000
381 #define OCPDR_REG_MASK 0x7f
382 #define OCPDR_GPHY_REG_SHIFT 16
383 #define OCPDR_DATA_MASK 0xffff
385 #define OCPAR_FLAG 0x80000000
386 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
387 #define OCPAR_GPHY_READ_CMD 0x0000f060
389 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
390 MISC = 0xf0, /* 8168e only. */
391 #define TXPLA_RST (1 << 29)
392 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
393 #define PWM_EN (1 << 22)
394 #define RXDV_GATED_EN (1 << 19)
395 #define EARLY_TALLY_EN (1 << 16)
398 enum rtl_register_content {
399 /* InterruptStatusBits */
403 TxDescUnavail = 0x0080,
427 /* TXPoll register p.5 */
428 HPQ = 0x80, /* Poll cmd on the high prio queue */
429 NPQ = 0x40, /* Poll cmd on the low prio queue */
430 FSWInt = 0x01, /* Forced software interrupt */
434 Cfg9346_Unlock = 0xc0,
439 AcceptBroadcast = 0x08,
440 AcceptMulticast = 0x04,
442 AcceptAllPhys = 0x01,
443 #define RX_CONFIG_ACCEPT_MASK 0x3f
446 TxInterFrameGapShift = 24,
447 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
449 /* Config1 register p.24 */
452 Speed_down = (1 << 4),
456 PMEnable = (1 << 0), /* Power Management Enable */
458 /* Config2 register p. 25 */
459 ClkReqEn = (1 << 7), /* Clock Request Enable */
460 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
461 PCI_Clock_66MHz = 0x01,
462 PCI_Clock_33MHz = 0x00,
464 /* Config3 register p.25 */
465 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
466 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
467 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
468 Rdy_to_L23 = (1 << 1), /* L23 Enable */
469 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
471 /* Config4 register */
472 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
474 /* Config5 register p.27 */
475 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
476 MWF = (1 << 5), /* Accept Multicast wakeup frame */
477 UWF = (1 << 4), /* Accept Unicast wakeup frame */
479 LanWake = (1 << 1), /* LanWake enable/disable */
480 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
481 ASPM_en = (1 << 0), /* ASPM enable */
484 EnableBist = (1 << 15), // 8168 8101
485 Mac_dbgo_oe = (1 << 14), // 8168 8101
486 Normal_mode = (1 << 13), // unused
487 Force_half_dup = (1 << 12), // 8168 8101
488 Force_rxflow_en = (1 << 11), // 8168 8101
489 Force_txflow_en = (1 << 10), // 8168 8101
490 Cxpl_dbg_sel = (1 << 9), // 8168 8101
491 ASF = (1 << 8), // 8168 8101
492 PktCntrDisable = (1 << 7), // 8168 8101
493 Mac_dbgo_sel = 0x001c, // 8168
498 #define INTT_MASK GENMASK(1, 0)
499 INTT_0 = 0x0000, // 8168
500 INTT_1 = 0x0001, // 8168
501 INTT_2 = 0x0002, // 8168
502 INTT_3 = 0x0003, // 8168
504 /* rtl8169_PHYstatus */
515 TBILinkOK = 0x02000000,
517 /* ResetCounterCommand */
520 /* DumpCounterCommand */
523 /* magic enable v2 */
524 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
528 /* First doubleword. */
529 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
530 RingEnd = (1 << 30), /* End of descriptor ring */
531 FirstFrag = (1 << 29), /* First segment of a packet */
532 LastFrag = (1 << 28), /* Final segment of a packet */
536 enum rtl_tx_desc_bit {
537 /* First doubleword. */
538 TD_LSO = (1 << 27), /* Large Send Offload */
539 #define TD_MSS_MAX 0x07ffu /* MSS value */
541 /* Second doubleword. */
542 TxVlanTag = (1 << 17), /* Add VLAN tag */
545 /* 8169, 8168b and 810x except 8102e. */
546 enum rtl_tx_desc_bit_0 {
547 /* First doubleword. */
548 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
549 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
550 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
551 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
554 /* 8102e, 8168c and beyond. */
555 enum rtl_tx_desc_bit_1 {
556 /* First doubleword. */
557 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
558 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
559 #define GTTCPHO_SHIFT 18
560 #define GTTCPHO_MAX 0x7fU
562 /* Second doubleword. */
563 #define TCPHO_SHIFT 18
564 #define TCPHO_MAX 0x3ffU
565 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
566 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
567 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
568 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
569 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
572 enum rtl_rx_desc_bit {
574 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
575 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
577 #define RxProtoUDP (PID1)
578 #define RxProtoTCP (PID0)
579 #define RxProtoIP (PID1 | PID0)
580 #define RxProtoMask RxProtoIP
582 IPFail = (1 << 16), /* IP checksum failed */
583 UDPFail = (1 << 15), /* UDP/IP checksum failed */
584 TCPFail = (1 << 14), /* TCP/IP checksum failed */
585 RxVlanTag = (1 << 16), /* VLAN tag available */
588 #define RsvdMask 0x3fffc000
589 #define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
608 struct rtl8169_counters {
615 __le32 tx_one_collision;
616 __le32 tx_multi_collision;
624 struct rtl8169_tc_offsets {
627 __le32 tx_multi_collision;
632 RTL_FLAG_TASK_ENABLED = 0,
633 RTL_FLAG_TASK_RESET_PENDING,
637 struct rtl8169_stats {
640 struct u64_stats_sync syncp;
643 struct rtl8169_private {
644 void __iomem *mmio_addr; /* memory map physical address */
645 struct pci_dev *pci_dev;
646 struct net_device *dev;
647 struct napi_struct napi;
650 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
651 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
653 struct rtl8169_stats rx_stats;
654 struct rtl8169_stats tx_stats;
655 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
656 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
657 dma_addr_t TxPhyAddr;
658 dma_addr_t RxPhyAddr;
659 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
660 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
664 const struct rtl_coalesce_info *coalesce_info;
668 void (*write)(struct rtl8169_private *, int, int);
669 int (*read)(struct rtl8169_private *, int);
673 void (*enable)(struct rtl8169_private *);
674 void (*disable)(struct rtl8169_private *);
677 void (*hw_start)(struct rtl8169_private *tp);
678 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
681 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
683 struct work_struct work;
686 unsigned supports_gmii:1;
687 struct mii_bus *mii_bus;
688 dma_addr_t counters_phys_addr;
689 struct rtl8169_counters *counters;
690 struct rtl8169_tc_offsets tc_offset;
694 const struct firmware *fw;
696 #define RTL_VER_SIZE 32
698 char version[RTL_VER_SIZE];
700 struct rtl_fw_phy_action {
705 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
710 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
711 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
712 module_param(use_dac, int, 0);
713 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
714 module_param_named(debug, debug.msg_enable, int, 0);
715 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
716 MODULE_LICENSE("GPL");
717 MODULE_FIRMWARE(FIRMWARE_8168D_1);
718 MODULE_FIRMWARE(FIRMWARE_8168D_2);
719 MODULE_FIRMWARE(FIRMWARE_8168E_1);
720 MODULE_FIRMWARE(FIRMWARE_8168E_2);
721 MODULE_FIRMWARE(FIRMWARE_8168E_3);
722 MODULE_FIRMWARE(FIRMWARE_8105E_1);
723 MODULE_FIRMWARE(FIRMWARE_8168F_1);
724 MODULE_FIRMWARE(FIRMWARE_8168F_2);
725 MODULE_FIRMWARE(FIRMWARE_8402_1);
726 MODULE_FIRMWARE(FIRMWARE_8411_1);
727 MODULE_FIRMWARE(FIRMWARE_8411_2);
728 MODULE_FIRMWARE(FIRMWARE_8106E_1);
729 MODULE_FIRMWARE(FIRMWARE_8106E_2);
730 MODULE_FIRMWARE(FIRMWARE_8168G_2);
731 MODULE_FIRMWARE(FIRMWARE_8168G_3);
732 MODULE_FIRMWARE(FIRMWARE_8168H_1);
733 MODULE_FIRMWARE(FIRMWARE_8168H_2);
734 MODULE_FIRMWARE(FIRMWARE_8107E_1);
735 MODULE_FIRMWARE(FIRMWARE_8107E_2);
737 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
739 return &tp->pci_dev->dev;
742 static void rtl_lock_work(struct rtl8169_private *tp)
744 mutex_lock(&tp->wk.mutex);
747 static void rtl_unlock_work(struct rtl8169_private *tp)
749 mutex_unlock(&tp->wk.mutex);
752 static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
754 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
755 PCI_EXP_DEVCTL_READRQ, force);
759 bool (*check)(struct rtl8169_private *);
763 static void rtl_udelay(unsigned int d)
768 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
769 void (*delay)(unsigned int), unsigned int d, int n,
774 for (i = 0; i < n; i++) {
776 if (c->check(tp) == high)
779 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
780 c->msg, !high, n, d);
784 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
785 const struct rtl_cond *c,
786 unsigned int d, int n)
788 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
791 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
792 const struct rtl_cond *c,
793 unsigned int d, int n)
795 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
798 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
799 const struct rtl_cond *c,
800 unsigned int d, int n)
802 return rtl_loop_wait(tp, c, msleep, d, n, true);
805 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
806 const struct rtl_cond *c,
807 unsigned int d, int n)
809 return rtl_loop_wait(tp, c, msleep, d, n, false);
812 #define DECLARE_RTL_COND(name) \
813 static bool name ## _check(struct rtl8169_private *); \
815 static const struct rtl_cond name = { \
816 .check = name ## _check, \
820 static bool name ## _check(struct rtl8169_private *tp)
822 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
824 if (reg & 0xffff0001) {
825 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
831 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
833 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
836 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
838 if (rtl_ocp_reg_failure(tp, reg))
841 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
843 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
846 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
848 if (rtl_ocp_reg_failure(tp, reg))
851 RTL_W32(tp, GPHY_OCP, reg << 15);
853 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
854 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
857 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
859 if (rtl_ocp_reg_failure(tp, reg))
862 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
865 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
867 if (rtl_ocp_reg_failure(tp, reg))
870 RTL_W32(tp, OCPDR, reg << 15);
872 return RTL_R32(tp, OCPDR);
875 #define OCP_STD_PHY_BASE 0xa400
877 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
880 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
884 if (tp->ocp_base != OCP_STD_PHY_BASE)
887 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
890 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
892 if (tp->ocp_base != OCP_STD_PHY_BASE)
895 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
898 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
901 tp->ocp_base = value << 4;
905 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
908 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
910 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
913 DECLARE_RTL_COND(rtl_phyar_cond)
915 return RTL_R32(tp, PHYAR) & 0x80000000;
918 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
920 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
922 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
924 * According to hardware specs a 20us delay is required after write
925 * complete indication, but before sending next command.
930 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
934 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
936 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
937 RTL_R32(tp, PHYAR) & 0xffff : ~0;
940 * According to hardware specs a 20us delay is required after read
941 * complete indication, but before sending next command.
948 DECLARE_RTL_COND(rtl_ocpar_cond)
950 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
953 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
955 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
956 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
957 RTL_W32(tp, EPHY_RXER_NUM, 0);
959 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
962 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
964 r8168dp_1_mdio_access(tp, reg,
965 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
968 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
970 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
973 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
974 RTL_W32(tp, EPHY_RXER_NUM, 0);
976 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
977 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
980 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
982 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
984 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
987 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
989 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
992 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
994 r8168dp_2_mdio_start(tp);
996 r8169_mdio_write(tp, reg, value);
998 r8168dp_2_mdio_stop(tp);
1001 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1005 r8168dp_2_mdio_start(tp);
1007 value = r8169_mdio_read(tp, reg);
1009 r8168dp_2_mdio_stop(tp);
1014 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1016 tp->mdio_ops.write(tp, location, val);
1019 static int rtl_readphy(struct rtl8169_private *tp, int location)
1021 return tp->mdio_ops.read(tp, location);
1024 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1026 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1029 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1033 val = rtl_readphy(tp, reg_addr);
1034 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1037 DECLARE_RTL_COND(rtl_ephyar_cond)
1039 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1042 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1044 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1045 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1047 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1052 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1054 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1056 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1057 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1060 DECLARE_RTL_COND(rtl_eriar_cond)
1062 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
1065 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1068 BUG_ON((addr & 3) || (mask == 0));
1069 RTL_W32(tp, ERIDR, val);
1070 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1072 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1075 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1077 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1079 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1080 RTL_R32(tp, ERIDR) : ~0;
1083 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1088 val = rtl_eri_read(tp, addr, type);
1089 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1092 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1094 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1095 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1096 RTL_R32(tp, OCPDR) : ~0;
1099 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1101 return rtl_eri_read(tp, reg, ERIAR_OOB);
1104 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1106 switch (tp->mac_version) {
1107 case RTL_GIGA_MAC_VER_27:
1108 case RTL_GIGA_MAC_VER_28:
1109 case RTL_GIGA_MAC_VER_31:
1110 return r8168dp_ocp_read(tp, mask, reg);
1111 case RTL_GIGA_MAC_VER_49:
1112 case RTL_GIGA_MAC_VER_50:
1113 case RTL_GIGA_MAC_VER_51:
1114 return r8168ep_ocp_read(tp, mask, reg);
1121 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1124 RTL_W32(tp, OCPDR, data);
1125 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1126 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1129 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1132 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1136 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1138 switch (tp->mac_version) {
1139 case RTL_GIGA_MAC_VER_27:
1140 case RTL_GIGA_MAC_VER_28:
1141 case RTL_GIGA_MAC_VER_31:
1142 r8168dp_ocp_write(tp, mask, reg, data);
1144 case RTL_GIGA_MAC_VER_49:
1145 case RTL_GIGA_MAC_VER_50:
1146 case RTL_GIGA_MAC_VER_51:
1147 r8168ep_ocp_write(tp, mask, reg, data);
1155 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1157 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1159 ocp_write(tp, 0x1, 0x30, 0x00000001);
1162 #define OOB_CMD_RESET 0x00
1163 #define OOB_CMD_DRIVER_START 0x05
1164 #define OOB_CMD_DRIVER_STOP 0x06
1166 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1168 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1171 DECLARE_RTL_COND(rtl_ocp_read_cond)
1175 reg = rtl8168_get_ocp_reg(tp);
1177 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1180 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1182 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1185 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1187 return RTL_R8(tp, IBISR0) & 0x20;
1190 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1192 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1193 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1194 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1195 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1198 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1200 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1201 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1204 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1206 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1207 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1208 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1211 static void rtl8168_driver_start(struct rtl8169_private *tp)
1213 switch (tp->mac_version) {
1214 case RTL_GIGA_MAC_VER_27:
1215 case RTL_GIGA_MAC_VER_28:
1216 case RTL_GIGA_MAC_VER_31:
1217 rtl8168dp_driver_start(tp);
1219 case RTL_GIGA_MAC_VER_49:
1220 case RTL_GIGA_MAC_VER_50:
1221 case RTL_GIGA_MAC_VER_51:
1222 rtl8168ep_driver_start(tp);
1230 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1232 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1233 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1236 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1238 rtl8168ep_stop_cmac(tp);
1239 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1240 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1241 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1244 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1246 switch (tp->mac_version) {
1247 case RTL_GIGA_MAC_VER_27:
1248 case RTL_GIGA_MAC_VER_28:
1249 case RTL_GIGA_MAC_VER_31:
1250 rtl8168dp_driver_stop(tp);
1252 case RTL_GIGA_MAC_VER_49:
1253 case RTL_GIGA_MAC_VER_50:
1254 case RTL_GIGA_MAC_VER_51:
1255 rtl8168ep_driver_stop(tp);
1263 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1265 u16 reg = rtl8168_get_ocp_reg(tp);
1267 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
1270 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1272 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
1275 static bool r8168_check_dash(struct rtl8169_private *tp)
1277 switch (tp->mac_version) {
1278 case RTL_GIGA_MAC_VER_27:
1279 case RTL_GIGA_MAC_VER_28:
1280 case RTL_GIGA_MAC_VER_31:
1281 return r8168dp_check_dash(tp);
1282 case RTL_GIGA_MAC_VER_49:
1283 case RTL_GIGA_MAC_VER_50:
1284 case RTL_GIGA_MAC_VER_51:
1285 return r8168ep_check_dash(tp);
1297 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1298 const struct exgmac_reg *r, int len)
1301 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1306 DECLARE_RTL_COND(rtl_efusear_cond)
1308 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1311 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1313 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1315 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1316 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1319 static u16 rtl_get_events(struct rtl8169_private *tp)
1321 return RTL_R16(tp, IntrStatus);
1324 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1326 RTL_W16(tp, IntrStatus, bits);
1330 static void rtl_irq_disable(struct rtl8169_private *tp)
1332 RTL_W16(tp, IntrMask, 0);
1336 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1337 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1338 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1340 static void rtl_irq_enable(struct rtl8169_private *tp)
1342 RTL_W16(tp, IntrMask, tp->irq_mask);
1345 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1347 rtl_irq_disable(tp);
1348 rtl_ack_events(tp, 0xffff);
1350 RTL_R8(tp, ChipCmd);
1353 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1355 struct net_device *dev = tp->dev;
1356 struct phy_device *phydev = dev->phydev;
1358 if (!netif_running(dev))
1361 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1362 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1363 if (phydev->speed == SPEED_1000) {
1364 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1366 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1368 } else if (phydev->speed == SPEED_100) {
1369 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1371 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1374 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1376 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1379 /* Reset packet filter */
1380 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1382 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1384 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1385 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1386 if (phydev->speed == SPEED_1000) {
1387 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1389 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1392 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1394 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1397 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1398 if (phydev->speed == SPEED_10) {
1399 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1401 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1404 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1410 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1412 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1417 options = RTL_R8(tp, Config1);
1418 if (!(options & PMEnable))
1421 options = RTL_R8(tp, Config3);
1422 if (options & LinkUp)
1423 wolopts |= WAKE_PHY;
1424 switch (tp->mac_version) {
1425 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1426 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1427 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1428 wolopts |= WAKE_MAGIC;
1431 if (options & MagicPacket)
1432 wolopts |= WAKE_MAGIC;
1436 options = RTL_R8(tp, Config5);
1438 wolopts |= WAKE_UCAST;
1440 wolopts |= WAKE_BCAST;
1442 wolopts |= WAKE_MCAST;
1447 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1449 struct rtl8169_private *tp = netdev_priv(dev);
1452 wol->supported = WAKE_ANY;
1453 wol->wolopts = tp->saved_wolopts;
1454 rtl_unlock_work(tp);
1457 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1459 unsigned int i, tmp;
1460 static const struct {
1465 { WAKE_PHY, Config3, LinkUp },
1466 { WAKE_UCAST, Config5, UWF },
1467 { WAKE_BCAST, Config5, BWF },
1468 { WAKE_MCAST, Config5, MWF },
1469 { WAKE_ANY, Config5, LanWake },
1470 { WAKE_MAGIC, Config3, MagicPacket }
1474 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
1476 switch (tp->mac_version) {
1477 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1478 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1479 tmp = ARRAY_SIZE(cfg) - 1;
1480 if (wolopts & WAKE_MAGIC)
1496 tmp = ARRAY_SIZE(cfg);
1500 for (i = 0; i < tmp; i++) {
1501 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1502 if (wolopts & cfg[i].opt)
1503 options |= cfg[i].mask;
1504 RTL_W8(tp, cfg[i].reg, options);
1507 switch (tp->mac_version) {
1508 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1509 options = RTL_R8(tp, Config1) & ~PMEnable;
1511 options |= PMEnable;
1512 RTL_W8(tp, Config1, options);
1515 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
1517 options |= PME_SIGNAL;
1518 RTL_W8(tp, Config2, options);
1522 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
1525 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1527 struct rtl8169_private *tp = netdev_priv(dev);
1528 struct device *d = tp_to_dev(tp);
1530 if (wol->wolopts & ~WAKE_ANY)
1533 pm_runtime_get_noresume(d);
1537 tp->saved_wolopts = wol->wolopts;
1539 if (pm_runtime_active(d))
1540 __rtl8169_set_wol(tp, tp->saved_wolopts);
1542 rtl_unlock_work(tp);
1544 device_set_wakeup_enable(d, tp->saved_wolopts);
1546 pm_runtime_put_noidle(d);
1551 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1553 return rtl_chip_infos[tp->mac_version].fw_name;
1556 static void rtl8169_get_drvinfo(struct net_device *dev,
1557 struct ethtool_drvinfo *info)
1559 struct rtl8169_private *tp = netdev_priv(dev);
1560 struct rtl_fw *rtl_fw = tp->rtl_fw;
1562 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1563 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1564 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1565 if (!IS_ERR_OR_NULL(rtl_fw))
1566 strlcpy(info->fw_version, rtl_fw->version,
1567 sizeof(info->fw_version));
1570 static int rtl8169_get_regs_len(struct net_device *dev)
1572 return R8169_REGS_SIZE;
1575 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1576 netdev_features_t features)
1578 struct rtl8169_private *tp = netdev_priv(dev);
1580 if (dev->mtu > TD_MSS_MAX)
1581 features &= ~NETIF_F_ALL_TSO;
1583 if (dev->mtu > JUMBO_1K &&
1584 tp->mac_version > RTL_GIGA_MAC_VER_06)
1585 features &= ~NETIF_F_IP_CSUM;
1590 static int rtl8169_set_features(struct net_device *dev,
1591 netdev_features_t features)
1593 struct rtl8169_private *tp = netdev_priv(dev);
1598 rx_config = RTL_R32(tp, RxConfig);
1599 if (features & NETIF_F_RXALL)
1600 rx_config |= (AcceptErr | AcceptRunt);
1602 rx_config &= ~(AcceptErr | AcceptRunt);
1604 RTL_W32(tp, RxConfig, rx_config);
1606 if (features & NETIF_F_RXCSUM)
1607 tp->cp_cmd |= RxChkSum;
1609 tp->cp_cmd &= ~RxChkSum;
1611 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1612 tp->cp_cmd |= RxVlan;
1614 tp->cp_cmd &= ~RxVlan;
1616 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1617 RTL_R16(tp, CPlusCmd);
1619 rtl_unlock_work(tp);
1624 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1626 return (skb_vlan_tag_present(skb)) ?
1627 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1630 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1632 u32 opts2 = le32_to_cpu(desc->opts2);
1634 if (opts2 & RxVlanTag)
1635 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1638 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1641 struct rtl8169_private *tp = netdev_priv(dev);
1642 u32 __iomem *data = tp->mmio_addr;
1647 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1648 memcpy_fromio(dw++, data++, 4);
1649 rtl_unlock_work(tp);
1652 static u32 rtl8169_get_msglevel(struct net_device *dev)
1654 struct rtl8169_private *tp = netdev_priv(dev);
1656 return tp->msg_enable;
1659 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1661 struct rtl8169_private *tp = netdev_priv(dev);
1663 tp->msg_enable = value;
1666 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1673 "tx_single_collisions",
1674 "tx_multi_collisions",
1682 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1686 return ARRAY_SIZE(rtl8169_gstrings);
1692 DECLARE_RTL_COND(rtl_counters_cond)
1694 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
1697 static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
1699 dma_addr_t paddr = tp->counters_phys_addr;
1702 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1703 RTL_R32(tp, CounterAddrHigh);
1704 cmd = (u64)paddr & DMA_BIT_MASK(32);
1705 RTL_W32(tp, CounterAddrLow, cmd);
1706 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
1708 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
1711 static bool rtl8169_reset_counters(struct rtl8169_private *tp)
1714 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1717 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1720 return rtl8169_do_counters(tp, CounterReset);
1723 static bool rtl8169_update_counters(struct rtl8169_private *tp)
1726 * Some chips are unable to dump tally counters when the receiver
1729 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
1732 return rtl8169_do_counters(tp, CounterDump);
1735 static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
1737 struct rtl8169_counters *counters = tp->counters;
1741 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1742 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1743 * reset by a power cycle, while the counter values collected by the
1744 * driver are reset at every driver unload/load cycle.
1746 * To make sure the HW values returned by @get_stats64 match the SW
1747 * values, we collect the initial values at first open(*) and use them
1748 * as offsets to normalize the values returned by @get_stats64.
1750 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1751 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1752 * set at open time by rtl_hw_start.
1755 if (tp->tc_offset.inited)
1758 /* If both, reset and update fail, propagate to caller. */
1759 if (rtl8169_reset_counters(tp))
1762 if (rtl8169_update_counters(tp))
1765 tp->tc_offset.tx_errors = counters->tx_errors;
1766 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1767 tp->tc_offset.tx_aborted = counters->tx_aborted;
1768 tp->tc_offset.inited = true;
1773 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1774 struct ethtool_stats *stats, u64 *data)
1776 struct rtl8169_private *tp = netdev_priv(dev);
1777 struct device *d = tp_to_dev(tp);
1778 struct rtl8169_counters *counters = tp->counters;
1782 pm_runtime_get_noresume(d);
1784 if (pm_runtime_active(d))
1785 rtl8169_update_counters(tp);
1787 pm_runtime_put_noidle(d);
1789 data[0] = le64_to_cpu(counters->tx_packets);
1790 data[1] = le64_to_cpu(counters->rx_packets);
1791 data[2] = le64_to_cpu(counters->tx_errors);
1792 data[3] = le32_to_cpu(counters->rx_errors);
1793 data[4] = le16_to_cpu(counters->rx_missed);
1794 data[5] = le16_to_cpu(counters->align_errors);
1795 data[6] = le32_to_cpu(counters->tx_one_collision);
1796 data[7] = le32_to_cpu(counters->tx_multi_collision);
1797 data[8] = le64_to_cpu(counters->rx_unicast);
1798 data[9] = le64_to_cpu(counters->rx_broadcast);
1799 data[10] = le32_to_cpu(counters->rx_multicast);
1800 data[11] = le16_to_cpu(counters->tx_aborted);
1801 data[12] = le16_to_cpu(counters->tx_underun);
1804 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1808 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1814 * Interrupt coalescing
1816 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1817 * > 8169, 8168 and 810x line of chipsets
1819 * 8169, 8168, and 8136(810x) serial chipsets support it.
1821 * > 2 - the Tx timer unit at gigabit speed
1823 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1824 * (0xe0) bit 1 and bit 0.
1827 * bit[1:0] \ speed 1000M 100M 10M
1828 * 0 0 320ns 2.56us 40.96us
1829 * 0 1 2.56us 20.48us 327.7us
1830 * 1 0 5.12us 40.96us 655.4us
1831 * 1 1 10.24us 81.92us 1.31ms
1834 * bit[1:0] \ speed 1000M 100M 10M
1835 * 0 0 5us 2.56us 40.96us
1836 * 0 1 40us 20.48us 327.7us
1837 * 1 0 80us 40.96us 655.4us
1838 * 1 1 160us 81.92us 1.31ms
1841 /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1842 struct rtl_coalesce_scale {
1847 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
1848 struct rtl_coalesce_info {
1850 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1853 /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1854 #define rxtx_x1822(r, t) { \
1857 {{(r)*8*2, (t)*8*2}}, \
1858 {{(r)*8*2*2, (t)*8*2*2}}, \
1860 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1861 /* speed delays: rx00 tx00 */
1862 { SPEED_10, rxtx_x1822(40960, 40960) },
1863 { SPEED_100, rxtx_x1822( 2560, 2560) },
1864 { SPEED_1000, rxtx_x1822( 320, 320) },
1868 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1869 /* speed delays: rx00 tx00 */
1870 { SPEED_10, rxtx_x1822(40960, 40960) },
1871 { SPEED_100, rxtx_x1822( 2560, 2560) },
1872 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1877 /* get rx/tx scale vector corresponding to current speed */
1878 static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1880 struct rtl8169_private *tp = netdev_priv(dev);
1881 struct ethtool_link_ksettings ecmd;
1882 const struct rtl_coalesce_info *ci;
1885 rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
1889 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
1890 if (ecmd.base.speed == ci->speed) {
1895 return ERR_PTR(-ELNRNG);
1898 static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1900 struct rtl8169_private *tp = netdev_priv(dev);
1901 const struct rtl_coalesce_info *ci;
1902 const struct rtl_coalesce_scale *scale;
1906 } coal_settings [] = {
1907 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1908 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1909 }, *p = coal_settings;
1913 memset(ec, 0, sizeof(*ec));
1915 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1916 ci = rtl_coalesce_info(dev);
1920 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
1922 /* read IntrMitigate and adjust according to scale */
1923 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
1924 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1925 w >>= RTL_COALESCE_SHIFT;
1926 *p->usecs = w & RTL_COALESCE_MASK;
1929 for (i = 0; i < 2; i++) {
1930 p = coal_settings + i;
1931 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1934 * ethtool_coalesce says it is illegal to set both usecs and
1937 if (!*p->usecs && !*p->max_frames)
1944 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1945 static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1946 struct net_device *dev, u32 nsec, u16 *cp01)
1948 const struct rtl_coalesce_info *ci;
1951 ci = rtl_coalesce_info(dev);
1953 return ERR_CAST(ci);
1955 for (i = 0; i < 4; i++) {
1956 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1957 ci->scalev[i].nsecs[1]);
1958 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1960 return &ci->scalev[i];
1964 return ERR_PTR(-EINVAL);
1967 static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1969 struct rtl8169_private *tp = netdev_priv(dev);
1970 const struct rtl_coalesce_scale *scale;
1974 } coal_settings [] = {
1975 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1976 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1977 }, *p = coal_settings;
1981 scale = rtl_coalesce_choose_scale(dev,
1982 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1984 return PTR_ERR(scale);
1986 for (i = 0; i < 2; i++, p++) {
1990 * accept max_frames=1 we returned in rtl_get_coalesce.
1991 * accept it not only when usecs=0 because of e.g. the following scenario:
1993 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1994 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1995 * - then user does `ethtool -C eth0 rx-usecs 100`
1997 * since ethtool sends to kernel whole ethtool_coalesce
1998 * settings, if we do not handle rx_usecs=!0, rx_frames=1
1999 * we'll reject it below in `frames % 4 != 0`.
2001 if (p->frames == 1) {
2005 units = p->usecs * 1000 / scale->nsecs[i];
2006 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2009 w <<= RTL_COALESCE_SHIFT;
2011 w <<= RTL_COALESCE_SHIFT;
2012 w |= p->frames >> 2;
2017 RTL_W16(tp, IntrMitigate, swab16(w));
2019 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
2020 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2021 RTL_R16(tp, CPlusCmd);
2023 rtl_unlock_work(tp);
2028 static const struct ethtool_ops rtl8169_ethtool_ops = {
2029 .get_drvinfo = rtl8169_get_drvinfo,
2030 .get_regs_len = rtl8169_get_regs_len,
2031 .get_link = ethtool_op_get_link,
2032 .get_coalesce = rtl_get_coalesce,
2033 .set_coalesce = rtl_set_coalesce,
2034 .get_msglevel = rtl8169_get_msglevel,
2035 .set_msglevel = rtl8169_set_msglevel,
2036 .get_regs = rtl8169_get_regs,
2037 .get_wol = rtl8169_get_wol,
2038 .set_wol = rtl8169_set_wol,
2039 .get_strings = rtl8169_get_strings,
2040 .get_sset_count = rtl8169_get_sset_count,
2041 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2042 .get_ts_info = ethtool_op_get_ts_info,
2043 .nway_reset = phy_ethtool_nway_reset,
2044 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2045 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2048 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2052 * The driver currently handles the 8168Bf and the 8168Be identically
2053 * but they can be identified more specifically through the test below
2056 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2058 * Same thing for the 8101Eb and the 8101Ec:
2060 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2062 static const struct rtl_mac_info {
2067 /* 8168EP family. */
2068 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2069 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2070 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2073 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2074 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2077 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2078 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2079 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2080 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2083 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2084 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2085 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2088 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2089 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2090 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2093 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2094 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2096 /* 8168DP family. */
2097 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2098 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2099 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2102 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2103 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2104 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2105 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2106 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2107 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2108 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2111 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2112 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2113 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2116 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2117 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2118 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2119 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2120 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2121 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2122 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2123 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2124 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2125 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2126 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2127 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2128 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2129 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2130 /* FIXME: where did these entries come from ? -- FR */
2131 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2132 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2135 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2136 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2137 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2138 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2139 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2140 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2143 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2145 const struct rtl_mac_info *p = mac_info;
2148 reg = RTL_R32(tp, TxConfig);
2149 while ((reg & p->mask) != p->val)
2151 tp->mac_version = p->mac_version;
2153 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2154 dev_notice(tp_to_dev(tp),
2155 "unknown MAC, using family default\n");
2156 tp->mac_version = default_version;
2157 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2158 tp->mac_version = tp->supports_gmii ?
2159 RTL_GIGA_MAC_VER_42 :
2160 RTL_GIGA_MAC_VER_43;
2161 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2162 tp->mac_version = tp->supports_gmii ?
2163 RTL_GIGA_MAC_VER_45 :
2164 RTL_GIGA_MAC_VER_47;
2165 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2166 tp->mac_version = tp->supports_gmii ?
2167 RTL_GIGA_MAC_VER_46 :
2168 RTL_GIGA_MAC_VER_48;
2177 static void rtl_writephy_batch(struct rtl8169_private *tp,
2178 const struct phy_reg *regs, int len)
2181 rtl_writephy(tp, regs->reg, regs->val);
2186 #define PHY_READ 0x00000000
2187 #define PHY_DATA_OR 0x10000000
2188 #define PHY_DATA_AND 0x20000000
2189 #define PHY_BJMPN 0x30000000
2190 #define PHY_MDIO_CHG 0x40000000
2191 #define PHY_CLEAR_READCOUNT 0x70000000
2192 #define PHY_WRITE 0x80000000
2193 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2194 #define PHY_COMP_EQ_SKIPN 0xa0000000
2195 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2196 #define PHY_WRITE_PREVIOUS 0xc0000000
2197 #define PHY_SKIPN 0xd0000000
2198 #define PHY_DELAY_MS 0xe0000000
2202 char version[RTL_VER_SIZE];
2208 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2210 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2212 const struct firmware *fw = rtl_fw->fw;
2213 struct fw_info *fw_info = (struct fw_info *)fw->data;
2214 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2215 char *version = rtl_fw->version;
2218 if (fw->size < FW_OPCODE_SIZE)
2221 if (!fw_info->magic) {
2222 size_t i, size, start;
2225 if (fw->size < sizeof(*fw_info))
2228 for (i = 0; i < fw->size; i++)
2229 checksum += fw->data[i];
2233 start = le32_to_cpu(fw_info->fw_start);
2234 if (start > fw->size)
2237 size = le32_to_cpu(fw_info->fw_len);
2238 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2241 memcpy(version, fw_info->version, RTL_VER_SIZE);
2243 pa->code = (__le32 *)(fw->data + start);
2246 if (fw->size % FW_OPCODE_SIZE)
2249 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2251 pa->code = (__le32 *)fw->data;
2252 pa->size = fw->size / FW_OPCODE_SIZE;
2254 version[RTL_VER_SIZE - 1] = 0;
2261 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2262 struct rtl_fw_phy_action *pa)
2267 for (index = 0; index < pa->size; index++) {
2268 u32 action = le32_to_cpu(pa->code[index]);
2269 u32 regno = (action & 0x0fff0000) >> 16;
2271 switch(action & 0xf0000000) {
2276 case PHY_CLEAR_READCOUNT:
2278 case PHY_WRITE_PREVIOUS:
2283 if (regno > index) {
2284 netif_err(tp, ifup, tp->dev,
2285 "Out of range of firmware\n");
2289 case PHY_READCOUNT_EQ_SKIP:
2290 if (index + 2 >= pa->size) {
2291 netif_err(tp, ifup, tp->dev,
2292 "Out of range of firmware\n");
2296 case PHY_COMP_EQ_SKIPN:
2297 case PHY_COMP_NEQ_SKIPN:
2299 if (index + 1 + regno >= pa->size) {
2300 netif_err(tp, ifup, tp->dev,
2301 "Out of range of firmware\n");
2307 netif_err(tp, ifup, tp->dev,
2308 "Invalid action 0x%08x\n", action);
2317 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2319 struct net_device *dev = tp->dev;
2322 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2323 netif_err(tp, ifup, dev, "invalid firmware\n");
2327 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2333 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2335 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2336 struct mdio_ops org, *ops = &tp->mdio_ops;
2340 predata = count = 0;
2341 org.write = ops->write;
2342 org.read = ops->read;
2344 for (index = 0; index < pa->size; ) {
2345 u32 action = le32_to_cpu(pa->code[index]);
2346 u32 data = action & 0x0000ffff;
2347 u32 regno = (action & 0x0fff0000) >> 16;
2352 switch(action & 0xf0000000) {
2354 predata = rtl_readphy(tp, regno);
2371 ops->write = org.write;
2372 ops->read = org.read;
2373 } else if (data == 1) {
2374 ops->write = mac_mcu_write;
2375 ops->read = mac_mcu_read;
2380 case PHY_CLEAR_READCOUNT:
2385 rtl_writephy(tp, regno, data);
2388 case PHY_READCOUNT_EQ_SKIP:
2389 index += (count == data) ? 2 : 1;
2391 case PHY_COMP_EQ_SKIPN:
2392 if (predata == data)
2396 case PHY_COMP_NEQ_SKIPN:
2397 if (predata != data)
2401 case PHY_WRITE_PREVIOUS:
2402 rtl_writephy(tp, regno, predata);
2418 ops->write = org.write;
2419 ops->read = org.read;
2422 static void rtl_release_firmware(struct rtl8169_private *tp)
2424 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2425 release_firmware(tp->rtl_fw->fw);
2428 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2431 static void rtl_apply_firmware(struct rtl8169_private *tp)
2433 struct rtl_fw *rtl_fw = tp->rtl_fw;
2435 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2436 if (!IS_ERR_OR_NULL(rtl_fw))
2437 rtl_phy_write_fw(tp, rtl_fw);
2440 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2442 if (rtl_readphy(tp, reg) != val)
2443 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2445 rtl_apply_firmware(tp);
2448 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2450 static const struct phy_reg phy_reg_init[] = {
2512 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2515 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2517 static const struct phy_reg phy_reg_init[] = {
2523 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2526 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2528 struct pci_dev *pdev = tp->pci_dev;
2530 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2531 (pdev->subsystem_device != 0xe000))
2534 rtl_writephy(tp, 0x1f, 0x0001);
2535 rtl_writephy(tp, 0x10, 0xf01b);
2536 rtl_writephy(tp, 0x1f, 0x0000);
2539 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2541 static const struct phy_reg phy_reg_init[] = {
2581 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2583 rtl8169scd_hw_phy_config_quirk(tp);
2586 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2588 static const struct phy_reg phy_reg_init[] = {
2636 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2639 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2641 static const struct phy_reg phy_reg_init[] = {
2646 rtl_writephy(tp, 0x1f, 0x0001);
2647 rtl_patchphy(tp, 0x16, 1 << 0);
2649 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2652 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2654 static const struct phy_reg phy_reg_init[] = {
2660 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2663 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2665 static const struct phy_reg phy_reg_init[] = {
2673 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2676 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2678 static const struct phy_reg phy_reg_init[] = {
2684 rtl_writephy(tp, 0x1f, 0x0000);
2685 rtl_patchphy(tp, 0x14, 1 << 5);
2686 rtl_patchphy(tp, 0x0d, 1 << 5);
2688 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2691 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2693 static const struct phy_reg phy_reg_init[] = {
2713 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2715 rtl_patchphy(tp, 0x14, 1 << 5);
2716 rtl_patchphy(tp, 0x0d, 1 << 5);
2717 rtl_writephy(tp, 0x1f, 0x0000);
2720 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2722 static const struct phy_reg phy_reg_init[] = {
2740 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2742 rtl_patchphy(tp, 0x16, 1 << 0);
2743 rtl_patchphy(tp, 0x14, 1 << 5);
2744 rtl_patchphy(tp, 0x0d, 1 << 5);
2745 rtl_writephy(tp, 0x1f, 0x0000);
2748 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2750 static const struct phy_reg phy_reg_init[] = {
2762 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2764 rtl_patchphy(tp, 0x16, 1 << 0);
2765 rtl_patchphy(tp, 0x14, 1 << 5);
2766 rtl_patchphy(tp, 0x0d, 1 << 5);
2767 rtl_writephy(tp, 0x1f, 0x0000);
2770 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2772 rtl8168c_3_hw_phy_config(tp);
2775 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2777 static const struct phy_reg phy_reg_init_0[] = {
2778 /* Channel Estimation */
2799 * Enhance line driver power
2808 * Can not link to 1Gbps with bad cable
2809 * Decrease SNR threshold form 21.07dB to 19.04dB
2818 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2822 * Fine Tune Switching regulator parameter
2824 rtl_writephy(tp, 0x1f, 0x0002);
2825 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2826 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
2828 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2829 static const struct phy_reg phy_reg_init[] = {
2839 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2841 val = rtl_readphy(tp, 0x0d);
2843 if ((val & 0x00ff) != 0x006c) {
2844 static const u32 set[] = {
2845 0x0065, 0x0066, 0x0067, 0x0068,
2846 0x0069, 0x006a, 0x006b, 0x006c
2850 rtl_writephy(tp, 0x1f, 0x0002);
2853 for (i = 0; i < ARRAY_SIZE(set); i++)
2854 rtl_writephy(tp, 0x0d, val | set[i]);
2857 static const struct phy_reg phy_reg_init[] = {
2865 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2868 /* RSET couple improve */
2869 rtl_writephy(tp, 0x1f, 0x0002);
2870 rtl_patchphy(tp, 0x0d, 0x0300);
2871 rtl_patchphy(tp, 0x0f, 0x0010);
2873 /* Fine tune PLL performance */
2874 rtl_writephy(tp, 0x1f, 0x0002);
2875 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2876 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
2878 rtl_writephy(tp, 0x1f, 0x0005);
2879 rtl_writephy(tp, 0x05, 0x001b);
2881 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2883 rtl_writephy(tp, 0x1f, 0x0000);
2886 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2888 static const struct phy_reg phy_reg_init_0[] = {
2889 /* Channel Estimation */
2910 * Enhance line driver power
2919 * Can not link to 1Gbps with bad cable
2920 * Decrease SNR threshold form 21.07dB to 19.04dB
2929 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2931 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2932 static const struct phy_reg phy_reg_init[] = {
2943 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2945 val = rtl_readphy(tp, 0x0d);
2946 if ((val & 0x00ff) != 0x006c) {
2947 static const u32 set[] = {
2948 0x0065, 0x0066, 0x0067, 0x0068,
2949 0x0069, 0x006a, 0x006b, 0x006c
2953 rtl_writephy(tp, 0x1f, 0x0002);
2956 for (i = 0; i < ARRAY_SIZE(set); i++)
2957 rtl_writephy(tp, 0x0d, val | set[i]);
2960 static const struct phy_reg phy_reg_init[] = {
2968 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2971 /* Fine tune PLL performance */
2972 rtl_writephy(tp, 0x1f, 0x0002);
2973 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2974 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
2976 /* Switching regulator Slew rate */
2977 rtl_writephy(tp, 0x1f, 0x0002);
2978 rtl_patchphy(tp, 0x0f, 0x0017);
2980 rtl_writephy(tp, 0x1f, 0x0005);
2981 rtl_writephy(tp, 0x05, 0x001b);
2983 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2985 rtl_writephy(tp, 0x1f, 0x0000);
2988 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2990 static const struct phy_reg phy_reg_init[] = {
3046 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3049 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3051 static const struct phy_reg phy_reg_init[] = {
3061 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3062 rtl_patchphy(tp, 0x0d, 1 << 5);
3065 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3067 static const struct phy_reg phy_reg_init[] = {
3068 /* Enable Delay cap */
3074 /* Channel estimation fine tune */
3083 /* Update PFM & 10M TX idle timer */
3095 rtl_apply_firmware(tp);
3097 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3099 /* DCO enable for 10M IDLE Power */
3100 rtl_writephy(tp, 0x1f, 0x0007);
3101 rtl_writephy(tp, 0x1e, 0x0023);
3102 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3103 rtl_writephy(tp, 0x1f, 0x0000);
3105 /* For impedance matching */
3106 rtl_writephy(tp, 0x1f, 0x0002);
3107 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3108 rtl_writephy(tp, 0x1f, 0x0000);
3110 /* PHY auto speed down */
3111 rtl_writephy(tp, 0x1f, 0x0007);
3112 rtl_writephy(tp, 0x1e, 0x002d);
3113 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3114 rtl_writephy(tp, 0x1f, 0x0000);
3115 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3117 rtl_writephy(tp, 0x1f, 0x0005);
3118 rtl_writephy(tp, 0x05, 0x8b86);
3119 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3120 rtl_writephy(tp, 0x1f, 0x0000);
3122 rtl_writephy(tp, 0x1f, 0x0005);
3123 rtl_writephy(tp, 0x05, 0x8b85);
3124 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3125 rtl_writephy(tp, 0x1f, 0x0007);
3126 rtl_writephy(tp, 0x1e, 0x0020);
3127 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3128 rtl_writephy(tp, 0x1f, 0x0006);
3129 rtl_writephy(tp, 0x00, 0x5a00);
3130 rtl_writephy(tp, 0x1f, 0x0000);
3131 rtl_writephy(tp, 0x0d, 0x0007);
3132 rtl_writephy(tp, 0x0e, 0x003c);
3133 rtl_writephy(tp, 0x0d, 0x4007);
3134 rtl_writephy(tp, 0x0e, 0x0000);
3135 rtl_writephy(tp, 0x0d, 0x0000);
3138 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3141 addr[0] | (addr[1] << 8),
3142 addr[2] | (addr[3] << 8),
3143 addr[4] | (addr[5] << 8)
3145 const struct exgmac_reg e[] = {
3146 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3147 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3148 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3149 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3152 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3155 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3157 static const struct phy_reg phy_reg_init[] = {
3158 /* Enable Delay cap */
3167 /* Channel estimation fine tune */
3184 rtl_apply_firmware(tp);
3186 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3188 /* For 4-corner performance improve */
3189 rtl_writephy(tp, 0x1f, 0x0005);
3190 rtl_writephy(tp, 0x05, 0x8b80);
3191 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3192 rtl_writephy(tp, 0x1f, 0x0000);
3194 /* PHY auto speed down */
3195 rtl_writephy(tp, 0x1f, 0x0004);
3196 rtl_writephy(tp, 0x1f, 0x0007);
3197 rtl_writephy(tp, 0x1e, 0x002d);
3198 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3199 rtl_writephy(tp, 0x1f, 0x0002);
3200 rtl_writephy(tp, 0x1f, 0x0000);
3201 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3203 /* improve 10M EEE waveform */
3204 rtl_writephy(tp, 0x1f, 0x0005);
3205 rtl_writephy(tp, 0x05, 0x8b86);
3206 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3207 rtl_writephy(tp, 0x1f, 0x0000);
3209 /* Improve 2-pair detection performance */
3210 rtl_writephy(tp, 0x1f, 0x0005);
3211 rtl_writephy(tp, 0x05, 0x8b85);
3212 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3213 rtl_writephy(tp, 0x1f, 0x0000);
3216 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
3217 rtl_writephy(tp, 0x1f, 0x0005);
3218 rtl_writephy(tp, 0x05, 0x8b85);
3219 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
3220 rtl_writephy(tp, 0x1f, 0x0004);
3221 rtl_writephy(tp, 0x1f, 0x0007);
3222 rtl_writephy(tp, 0x1e, 0x0020);
3223 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
3224 rtl_writephy(tp, 0x1f, 0x0002);
3225 rtl_writephy(tp, 0x1f, 0x0000);
3226 rtl_writephy(tp, 0x0d, 0x0007);
3227 rtl_writephy(tp, 0x0e, 0x003c);
3228 rtl_writephy(tp, 0x0d, 0x4007);
3229 rtl_writephy(tp, 0x0e, 0x0006);
3230 rtl_writephy(tp, 0x0d, 0x0000);
3233 rtl_writephy(tp, 0x1f, 0x0003);
3234 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3235 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
3236 rtl_writephy(tp, 0x1f, 0x0000);
3237 rtl_writephy(tp, 0x1f, 0x0005);
3238 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3239 rtl_writephy(tp, 0x1f, 0x0000);
3241 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3242 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3245 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3247 /* For 4-corner performance improve */
3248 rtl_writephy(tp, 0x1f, 0x0005);
3249 rtl_writephy(tp, 0x05, 0x8b80);
3250 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3251 rtl_writephy(tp, 0x1f, 0x0000);
3253 /* PHY auto speed down */
3254 rtl_writephy(tp, 0x1f, 0x0007);
3255 rtl_writephy(tp, 0x1e, 0x002d);
3256 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3257 rtl_writephy(tp, 0x1f, 0x0000);
3258 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3260 /* Improve 10M EEE waveform */
3261 rtl_writephy(tp, 0x1f, 0x0005);
3262 rtl_writephy(tp, 0x05, 0x8b86);
3263 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3264 rtl_writephy(tp, 0x1f, 0x0000);
3267 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3269 static const struct phy_reg phy_reg_init[] = {
3270 /* Channel estimation fine tune */
3275 /* Modify green table for giga & fnet */
3292 /* Modify green table for 10M */
3298 /* Disable hiimpedance detection (RTCT) */
3304 rtl_apply_firmware(tp);
3306 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3308 rtl8168f_hw_phy_config(tp);
3310 /* Improve 2-pair detection performance */
3311 rtl_writephy(tp, 0x1f, 0x0005);
3312 rtl_writephy(tp, 0x05, 0x8b85);
3313 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3314 rtl_writephy(tp, 0x1f, 0x0000);
3317 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3319 rtl_apply_firmware(tp);
3321 rtl8168f_hw_phy_config(tp);
3324 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3326 static const struct phy_reg phy_reg_init[] = {
3327 /* Channel estimation fine tune */
3332 /* Modify green table for giga & fnet */
3349 /* Modify green table for 10M */
3355 /* Disable hiimpedance detection (RTCT) */
3362 rtl_apply_firmware(tp);
3364 rtl8168f_hw_phy_config(tp);
3366 /* Improve 2-pair detection performance */
3367 rtl_writephy(tp, 0x1f, 0x0005);
3368 rtl_writephy(tp, 0x05, 0x8b85);
3369 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3370 rtl_writephy(tp, 0x1f, 0x0000);
3372 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3374 /* Modify green table for giga */
3375 rtl_writephy(tp, 0x1f, 0x0005);
3376 rtl_writephy(tp, 0x05, 0x8b54);
3377 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3378 rtl_writephy(tp, 0x05, 0x8b5d);
3379 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3380 rtl_writephy(tp, 0x05, 0x8a7c);
3381 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3382 rtl_writephy(tp, 0x05, 0x8a7f);
3383 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3384 rtl_writephy(tp, 0x05, 0x8a82);
3385 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3386 rtl_writephy(tp, 0x05, 0x8a85);
3387 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3388 rtl_writephy(tp, 0x05, 0x8a88);
3389 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3390 rtl_writephy(tp, 0x1f, 0x0000);
3392 /* uc same-seed solution */
3393 rtl_writephy(tp, 0x1f, 0x0005);
3394 rtl_writephy(tp, 0x05, 0x8b85);
3395 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3396 rtl_writephy(tp, 0x1f, 0x0000);
3399 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3400 rtl_writephy(tp, 0x1f, 0x0005);
3401 rtl_writephy(tp, 0x05, 0x8b85);
3402 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3403 rtl_writephy(tp, 0x1f, 0x0004);
3404 rtl_writephy(tp, 0x1f, 0x0007);
3405 rtl_writephy(tp, 0x1e, 0x0020);
3406 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3407 rtl_writephy(tp, 0x1f, 0x0000);
3408 rtl_writephy(tp, 0x0d, 0x0007);
3409 rtl_writephy(tp, 0x0e, 0x003c);
3410 rtl_writephy(tp, 0x0d, 0x4007);
3411 rtl_writephy(tp, 0x0e, 0x0000);
3412 rtl_writephy(tp, 0x0d, 0x0000);
3415 rtl_writephy(tp, 0x1f, 0x0003);
3416 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3417 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3418 rtl_writephy(tp, 0x1f, 0x0000);
3421 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3423 rtl_apply_firmware(tp);
3425 rtl_writephy(tp, 0x1f, 0x0a46);
3426 if (rtl_readphy(tp, 0x10) & 0x0100) {
3427 rtl_writephy(tp, 0x1f, 0x0bcc);
3428 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3430 rtl_writephy(tp, 0x1f, 0x0bcc);
3431 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3434 rtl_writephy(tp, 0x1f, 0x0a46);
3435 if (rtl_readphy(tp, 0x13) & 0x0100) {
3436 rtl_writephy(tp, 0x1f, 0x0c41);
3437 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3439 rtl_writephy(tp, 0x1f, 0x0c41);
3440 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3443 /* Enable PHY auto speed down */
3444 rtl_writephy(tp, 0x1f, 0x0a44);
3445 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3447 rtl_writephy(tp, 0x1f, 0x0bcc);
3448 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
3449 rtl_writephy(tp, 0x1f, 0x0a44);
3450 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3451 rtl_writephy(tp, 0x1f, 0x0a43);
3452 rtl_writephy(tp, 0x13, 0x8084);
3453 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3454 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3456 /* EEE auto-fallback function */
3457 rtl_writephy(tp, 0x1f, 0x0a4b);
3458 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3460 /* Enable UC LPF tune function */
3461 rtl_writephy(tp, 0x1f, 0x0a43);
3462 rtl_writephy(tp, 0x13, 0x8012);
3463 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3465 rtl_writephy(tp, 0x1f, 0x0c42);
3466 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3468 /* Improve SWR Efficiency */
3469 rtl_writephy(tp, 0x1f, 0x0bcd);
3470 rtl_writephy(tp, 0x14, 0x5065);
3471 rtl_writephy(tp, 0x14, 0xd065);
3472 rtl_writephy(tp, 0x1f, 0x0bc8);
3473 rtl_writephy(tp, 0x11, 0x5655);
3474 rtl_writephy(tp, 0x1f, 0x0bcd);
3475 rtl_writephy(tp, 0x14, 0x1065);
3476 rtl_writephy(tp, 0x14, 0x9065);
3477 rtl_writephy(tp, 0x14, 0x1065);
3479 /* Check ALDPS bit, disable it if enabled */
3480 rtl_writephy(tp, 0x1f, 0x0a43);
3481 if (rtl_readphy(tp, 0x10) & 0x0004)
3482 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3484 rtl_writephy(tp, 0x1f, 0x0000);
3487 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3489 rtl_apply_firmware(tp);
3492 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3497 rtl_apply_firmware(tp);
3499 /* CHN EST parameters adjust - giga master */
3500 rtl_writephy(tp, 0x1f, 0x0a43);
3501 rtl_writephy(tp, 0x13, 0x809b);
3502 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3503 rtl_writephy(tp, 0x13, 0x80a2);
3504 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3505 rtl_writephy(tp, 0x13, 0x80a4);
3506 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3507 rtl_writephy(tp, 0x13, 0x809c);
3508 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3509 rtl_writephy(tp, 0x1f, 0x0000);
3511 /* CHN EST parameters adjust - giga slave */
3512 rtl_writephy(tp, 0x1f, 0x0a43);
3513 rtl_writephy(tp, 0x13, 0x80ad);
3514 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
3515 rtl_writephy(tp, 0x13, 0x80b4);
3516 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
3517 rtl_writephy(tp, 0x13, 0x80ac);
3518 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
3519 rtl_writephy(tp, 0x1f, 0x0000);
3521 /* CHN EST parameters adjust - fnet */
3522 rtl_writephy(tp, 0x1f, 0x0a43);
3523 rtl_writephy(tp, 0x13, 0x808e);
3524 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
3525 rtl_writephy(tp, 0x13, 0x8090);
3526 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
3527 rtl_writephy(tp, 0x13, 0x8092);
3528 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
3529 rtl_writephy(tp, 0x1f, 0x0000);
3531 /* enable R-tune & PGA-retune function */
3533 rtl_writephy(tp, 0x1f, 0x0a46);
3534 data = rtl_readphy(tp, 0x13);
3537 dout_tapbin |= data;
3538 data = rtl_readphy(tp, 0x12);
3541 dout_tapbin |= data;
3542 dout_tapbin = ~(dout_tapbin^0x08);
3544 dout_tapbin &= 0xf000;
3545 rtl_writephy(tp, 0x1f, 0x0a43);
3546 rtl_writephy(tp, 0x13, 0x827a);
3547 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3548 rtl_writephy(tp, 0x13, 0x827b);
3549 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3550 rtl_writephy(tp, 0x13, 0x827c);
3551 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3552 rtl_writephy(tp, 0x13, 0x827d);
3553 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3555 rtl_writephy(tp, 0x1f, 0x0a43);
3556 rtl_writephy(tp, 0x13, 0x0811);
3557 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3558 rtl_writephy(tp, 0x1f, 0x0a42);
3559 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3560 rtl_writephy(tp, 0x1f, 0x0000);
3562 /* enable GPHY 10M */
3563 rtl_writephy(tp, 0x1f, 0x0a44);
3564 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3565 rtl_writephy(tp, 0x1f, 0x0000);
3567 /* SAR ADC performance */
3568 rtl_writephy(tp, 0x1f, 0x0bca);
3569 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
3570 rtl_writephy(tp, 0x1f, 0x0000);
3572 rtl_writephy(tp, 0x1f, 0x0a43);
3573 rtl_writephy(tp, 0x13, 0x803f);
3574 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3575 rtl_writephy(tp, 0x13, 0x8047);
3576 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3577 rtl_writephy(tp, 0x13, 0x804f);
3578 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3579 rtl_writephy(tp, 0x13, 0x8057);
3580 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3581 rtl_writephy(tp, 0x13, 0x805f);
3582 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3583 rtl_writephy(tp, 0x13, 0x8067);
3584 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3585 rtl_writephy(tp, 0x13, 0x806f);
3586 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3587 rtl_writephy(tp, 0x1f, 0x0000);
3589 /* disable phy pfm mode */
3590 rtl_writephy(tp, 0x1f, 0x0a44);
3591 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3592 rtl_writephy(tp, 0x1f, 0x0000);
3594 /* Check ALDPS bit, disable it if enabled */
3595 rtl_writephy(tp, 0x1f, 0x0a43);
3596 if (rtl_readphy(tp, 0x10) & 0x0004)
3597 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3599 rtl_writephy(tp, 0x1f, 0x0000);
3602 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3604 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3608 rtl_apply_firmware(tp);
3610 /* CHIN EST parameter update */
3611 rtl_writephy(tp, 0x1f, 0x0a43);
3612 rtl_writephy(tp, 0x13, 0x808a);
3613 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
3614 rtl_writephy(tp, 0x1f, 0x0000);
3616 /* enable R-tune & PGA-retune function */
3617 rtl_writephy(tp, 0x1f, 0x0a43);
3618 rtl_writephy(tp, 0x13, 0x0811);
3619 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3620 rtl_writephy(tp, 0x1f, 0x0a42);
3621 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3622 rtl_writephy(tp, 0x1f, 0x0000);
3624 /* enable GPHY 10M */
3625 rtl_writephy(tp, 0x1f, 0x0a44);
3626 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3627 rtl_writephy(tp, 0x1f, 0x0000);
3629 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3630 data = r8168_mac_ocp_read(tp, 0xdd02);
3631 ioffset_p3 = ((data & 0x80)>>7);
3634 data = r8168_mac_ocp_read(tp, 0xdd00);
3635 ioffset_p3 |= ((data & (0xe000))>>13);
3636 ioffset_p2 = ((data & (0x1e00))>>9);
3637 ioffset_p1 = ((data & (0x01e0))>>5);
3638 ioffset_p0 = ((data & 0x0010)>>4);
3640 ioffset_p0 |= (data & (0x07));
3641 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3643 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3644 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
3645 rtl_writephy(tp, 0x1f, 0x0bcf);
3646 rtl_writephy(tp, 0x16, data);
3647 rtl_writephy(tp, 0x1f, 0x0000);
3650 /* Modify rlen (TX LPF corner frequency) level */
3651 rtl_writephy(tp, 0x1f, 0x0bcd);
3652 data = rtl_readphy(tp, 0x16);
3657 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3658 rtl_writephy(tp, 0x17, data);
3659 rtl_writephy(tp, 0x1f, 0x0bcd);
3660 rtl_writephy(tp, 0x1f, 0x0000);
3662 /* disable phy pfm mode */
3663 rtl_writephy(tp, 0x1f, 0x0a44);
3664 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3665 rtl_writephy(tp, 0x1f, 0x0000);
3667 /* Check ALDPS bit, disable it if enabled */
3668 rtl_writephy(tp, 0x1f, 0x0a43);
3669 if (rtl_readphy(tp, 0x10) & 0x0004)
3670 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3672 rtl_writephy(tp, 0x1f, 0x0000);
3675 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3677 /* Enable PHY auto speed down */
3678 rtl_writephy(tp, 0x1f, 0x0a44);
3679 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3680 rtl_writephy(tp, 0x1f, 0x0000);
3682 /* patch 10M & ALDPS */
3683 rtl_writephy(tp, 0x1f, 0x0bcc);
3684 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3685 rtl_writephy(tp, 0x1f, 0x0a44);
3686 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3687 rtl_writephy(tp, 0x1f, 0x0a43);
3688 rtl_writephy(tp, 0x13, 0x8084);
3689 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3690 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3691 rtl_writephy(tp, 0x1f, 0x0000);
3693 /* Enable EEE auto-fallback function */
3694 rtl_writephy(tp, 0x1f, 0x0a4b);
3695 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3696 rtl_writephy(tp, 0x1f, 0x0000);
3698 /* Enable UC LPF tune function */
3699 rtl_writephy(tp, 0x1f, 0x0a43);
3700 rtl_writephy(tp, 0x13, 0x8012);
3701 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3702 rtl_writephy(tp, 0x1f, 0x0000);
3704 /* set rg_sel_sdm_rate */
3705 rtl_writephy(tp, 0x1f, 0x0c42);
3706 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3707 rtl_writephy(tp, 0x1f, 0x0000);
3709 /* Check ALDPS bit, disable it if enabled */
3710 rtl_writephy(tp, 0x1f, 0x0a43);
3711 if (rtl_readphy(tp, 0x10) & 0x0004)
3712 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3714 rtl_writephy(tp, 0x1f, 0x0000);
3717 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3719 /* patch 10M & ALDPS */
3720 rtl_writephy(tp, 0x1f, 0x0bcc);
3721 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3722 rtl_writephy(tp, 0x1f, 0x0a44);
3723 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3724 rtl_writephy(tp, 0x1f, 0x0a43);
3725 rtl_writephy(tp, 0x13, 0x8084);
3726 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3727 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3728 rtl_writephy(tp, 0x1f, 0x0000);
3730 /* Enable UC LPF tune function */
3731 rtl_writephy(tp, 0x1f, 0x0a43);
3732 rtl_writephy(tp, 0x13, 0x8012);
3733 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3734 rtl_writephy(tp, 0x1f, 0x0000);
3736 /* Set rg_sel_sdm_rate */
3737 rtl_writephy(tp, 0x1f, 0x0c42);
3738 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3739 rtl_writephy(tp, 0x1f, 0x0000);
3741 /* Channel estimation parameters */
3742 rtl_writephy(tp, 0x1f, 0x0a43);
3743 rtl_writephy(tp, 0x13, 0x80f3);
3744 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3745 rtl_writephy(tp, 0x13, 0x80f0);
3746 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3747 rtl_writephy(tp, 0x13, 0x80ef);
3748 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3749 rtl_writephy(tp, 0x13, 0x80f6);
3750 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3751 rtl_writephy(tp, 0x13, 0x80ec);
3752 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3753 rtl_writephy(tp, 0x13, 0x80ed);
3754 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3755 rtl_writephy(tp, 0x13, 0x80f2);
3756 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3757 rtl_writephy(tp, 0x13, 0x80f4);
3758 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3759 rtl_writephy(tp, 0x1f, 0x0a43);
3760 rtl_writephy(tp, 0x13, 0x8110);
3761 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3762 rtl_writephy(tp, 0x13, 0x810f);
3763 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3764 rtl_writephy(tp, 0x13, 0x8111);
3765 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3766 rtl_writephy(tp, 0x13, 0x8113);
3767 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3768 rtl_writephy(tp, 0x13, 0x8115);
3769 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3770 rtl_writephy(tp, 0x13, 0x810e);
3771 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3772 rtl_writephy(tp, 0x13, 0x810c);
3773 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3774 rtl_writephy(tp, 0x13, 0x810b);
3775 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3776 rtl_writephy(tp, 0x1f, 0x0a43);
3777 rtl_writephy(tp, 0x13, 0x80d1);
3778 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3779 rtl_writephy(tp, 0x13, 0x80cd);
3780 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3781 rtl_writephy(tp, 0x13, 0x80d3);
3782 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3783 rtl_writephy(tp, 0x13, 0x80d5);
3784 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3785 rtl_writephy(tp, 0x13, 0x80d7);
3786 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3788 /* Force PWM-mode */
3789 rtl_writephy(tp, 0x1f, 0x0bcd);
3790 rtl_writephy(tp, 0x14, 0x5065);
3791 rtl_writephy(tp, 0x14, 0xd065);
3792 rtl_writephy(tp, 0x1f, 0x0bc8);
3793 rtl_writephy(tp, 0x12, 0x00ed);
3794 rtl_writephy(tp, 0x1f, 0x0bcd);
3795 rtl_writephy(tp, 0x14, 0x1065);
3796 rtl_writephy(tp, 0x14, 0x9065);
3797 rtl_writephy(tp, 0x14, 0x1065);
3798 rtl_writephy(tp, 0x1f, 0x0000);
3800 /* Check ALDPS bit, disable it if enabled */
3801 rtl_writephy(tp, 0x1f, 0x0a43);
3802 if (rtl_readphy(tp, 0x10) & 0x0004)
3803 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3805 rtl_writephy(tp, 0x1f, 0x0000);
3808 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3810 static const struct phy_reg phy_reg_init[] = {
3817 rtl_writephy(tp, 0x1f, 0x0000);
3818 rtl_patchphy(tp, 0x11, 1 << 12);
3819 rtl_patchphy(tp, 0x19, 1 << 13);
3820 rtl_patchphy(tp, 0x10, 1 << 15);
3822 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3825 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3827 static const struct phy_reg phy_reg_init[] = {
3841 /* Disable ALDPS before ram code */
3842 rtl_writephy(tp, 0x1f, 0x0000);
3843 rtl_writephy(tp, 0x18, 0x0310);
3846 rtl_apply_firmware(tp);
3848 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3851 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3853 /* Disable ALDPS before setting firmware */
3854 rtl_writephy(tp, 0x1f, 0x0000);
3855 rtl_writephy(tp, 0x18, 0x0310);
3858 rtl_apply_firmware(tp);
3861 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3862 rtl_writephy(tp, 0x1f, 0x0004);
3863 rtl_writephy(tp, 0x10, 0x401f);
3864 rtl_writephy(tp, 0x19, 0x7030);
3865 rtl_writephy(tp, 0x1f, 0x0000);
3868 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3870 static const struct phy_reg phy_reg_init[] = {
3877 /* Disable ALDPS before ram code */
3878 rtl_writephy(tp, 0x1f, 0x0000);
3879 rtl_writephy(tp, 0x18, 0x0310);
3882 rtl_apply_firmware(tp);
3884 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3885 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3887 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3890 static void rtl_hw_phy_config(struct net_device *dev)
3892 struct rtl8169_private *tp = netdev_priv(dev);
3894 switch (tp->mac_version) {
3895 case RTL_GIGA_MAC_VER_01:
3897 case RTL_GIGA_MAC_VER_02:
3898 case RTL_GIGA_MAC_VER_03:
3899 rtl8169s_hw_phy_config(tp);
3901 case RTL_GIGA_MAC_VER_04:
3902 rtl8169sb_hw_phy_config(tp);
3904 case RTL_GIGA_MAC_VER_05:
3905 rtl8169scd_hw_phy_config(tp);
3907 case RTL_GIGA_MAC_VER_06:
3908 rtl8169sce_hw_phy_config(tp);
3910 case RTL_GIGA_MAC_VER_07:
3911 case RTL_GIGA_MAC_VER_08:
3912 case RTL_GIGA_MAC_VER_09:
3913 rtl8102e_hw_phy_config(tp);
3915 case RTL_GIGA_MAC_VER_11:
3916 rtl8168bb_hw_phy_config(tp);
3918 case RTL_GIGA_MAC_VER_12:
3919 rtl8168bef_hw_phy_config(tp);
3921 case RTL_GIGA_MAC_VER_17:
3922 rtl8168bef_hw_phy_config(tp);
3924 case RTL_GIGA_MAC_VER_18:
3925 rtl8168cp_1_hw_phy_config(tp);
3927 case RTL_GIGA_MAC_VER_19:
3928 rtl8168c_1_hw_phy_config(tp);
3930 case RTL_GIGA_MAC_VER_20:
3931 rtl8168c_2_hw_phy_config(tp);
3933 case RTL_GIGA_MAC_VER_21:
3934 rtl8168c_3_hw_phy_config(tp);
3936 case RTL_GIGA_MAC_VER_22:
3937 rtl8168c_4_hw_phy_config(tp);
3939 case RTL_GIGA_MAC_VER_23:
3940 case RTL_GIGA_MAC_VER_24:
3941 rtl8168cp_2_hw_phy_config(tp);
3943 case RTL_GIGA_MAC_VER_25:
3944 rtl8168d_1_hw_phy_config(tp);
3946 case RTL_GIGA_MAC_VER_26:
3947 rtl8168d_2_hw_phy_config(tp);
3949 case RTL_GIGA_MAC_VER_27:
3950 rtl8168d_3_hw_phy_config(tp);
3952 case RTL_GIGA_MAC_VER_28:
3953 rtl8168d_4_hw_phy_config(tp);
3955 case RTL_GIGA_MAC_VER_29:
3956 case RTL_GIGA_MAC_VER_30:
3957 rtl8105e_hw_phy_config(tp);
3959 case RTL_GIGA_MAC_VER_31:
3962 case RTL_GIGA_MAC_VER_32:
3963 case RTL_GIGA_MAC_VER_33:
3964 rtl8168e_1_hw_phy_config(tp);
3966 case RTL_GIGA_MAC_VER_34:
3967 rtl8168e_2_hw_phy_config(tp);
3969 case RTL_GIGA_MAC_VER_35:
3970 rtl8168f_1_hw_phy_config(tp);
3972 case RTL_GIGA_MAC_VER_36:
3973 rtl8168f_2_hw_phy_config(tp);
3976 case RTL_GIGA_MAC_VER_37:
3977 rtl8402_hw_phy_config(tp);
3980 case RTL_GIGA_MAC_VER_38:
3981 rtl8411_hw_phy_config(tp);
3984 case RTL_GIGA_MAC_VER_39:
3985 rtl8106e_hw_phy_config(tp);
3988 case RTL_GIGA_MAC_VER_40:
3989 rtl8168g_1_hw_phy_config(tp);
3991 case RTL_GIGA_MAC_VER_42:
3992 case RTL_GIGA_MAC_VER_43:
3993 case RTL_GIGA_MAC_VER_44:
3994 rtl8168g_2_hw_phy_config(tp);
3996 case RTL_GIGA_MAC_VER_45:
3997 case RTL_GIGA_MAC_VER_47:
3998 rtl8168h_1_hw_phy_config(tp);
4000 case RTL_GIGA_MAC_VER_46:
4001 case RTL_GIGA_MAC_VER_48:
4002 rtl8168h_2_hw_phy_config(tp);
4005 case RTL_GIGA_MAC_VER_49:
4006 rtl8168ep_1_hw_phy_config(tp);
4008 case RTL_GIGA_MAC_VER_50:
4009 case RTL_GIGA_MAC_VER_51:
4010 rtl8168ep_2_hw_phy_config(tp);
4013 case RTL_GIGA_MAC_VER_41:
4019 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4021 if (!test_and_set_bit(flag, tp->wk.flags))
4022 schedule_work(&tp->wk.work);
4025 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4027 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4028 (RTL_R8(tp, PHYstatus) & TBI_Enable);
4031 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4033 rtl_hw_phy_config(dev);
4035 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4036 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4037 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4038 netif_dbg(tp, drv, dev,
4039 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4040 RTL_W8(tp, 0x82, 0x01);
4043 /* We may have called phy_speed_down before */
4044 phy_speed_up(dev->phydev);
4046 genphy_soft_reset(dev->phydev);
4048 /* It was reported that several chips end up with 10MBit/Half on a
4049 * 1GBit link after resuming from S3. For whatever reason the PHY on
4050 * these chips doesn't properly start a renegotiation when soft-reset.
4051 * Explicitly requesting a renegotiation fixes this.
4053 if (dev->phydev->autoneg == AUTONEG_ENABLE)
4054 phy_restart_aneg(dev->phydev);
4057 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4061 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4063 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4066 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4069 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4070 rtl_rar_exgmac_set(tp, addr);
4072 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4074 rtl_unlock_work(tp);
4077 static int rtl_set_mac_address(struct net_device *dev, void *p)
4079 struct rtl8169_private *tp = netdev_priv(dev);
4080 struct device *d = tp_to_dev(tp);
4083 ret = eth_mac_addr(dev, p);
4087 pm_runtime_get_noresume(d);
4089 if (pm_runtime_active(d))
4090 rtl_rar_set(tp, dev->dev_addr);
4092 pm_runtime_put_noidle(d);
4097 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4099 if (!netif_running(dev))
4102 return phy_mii_ioctl(dev->phydev, ifr, cmd);
4105 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4107 struct mdio_ops *ops = &tp->mdio_ops;
4109 switch (tp->mac_version) {
4110 case RTL_GIGA_MAC_VER_27:
4111 ops->write = r8168dp_1_mdio_write;
4112 ops->read = r8168dp_1_mdio_read;
4114 case RTL_GIGA_MAC_VER_28:
4115 case RTL_GIGA_MAC_VER_31:
4116 ops->write = r8168dp_2_mdio_write;
4117 ops->read = r8168dp_2_mdio_read;
4119 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4120 ops->write = r8168g_mdio_write;
4121 ops->read = r8168g_mdio_read;
4124 ops->write = r8169_mdio_write;
4125 ops->read = r8169_mdio_read;
4130 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4132 switch (tp->mac_version) {
4133 case RTL_GIGA_MAC_VER_25:
4134 case RTL_GIGA_MAC_VER_26:
4135 case RTL_GIGA_MAC_VER_29:
4136 case RTL_GIGA_MAC_VER_30:
4137 case RTL_GIGA_MAC_VER_32:
4138 case RTL_GIGA_MAC_VER_33:
4139 case RTL_GIGA_MAC_VER_34:
4140 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
4141 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
4142 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4149 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4151 struct phy_device *phydev;
4153 if (!__rtl8169_get_wol(tp))
4156 /* phydev may not be attached to netdevice */
4157 phydev = mdiobus_get_phy(tp->mii_bus, 0);
4159 phy_speed_down(phydev, false);
4160 rtl_wol_suspend_quirk(tp);
4165 static void r8168_pll_power_down(struct rtl8169_private *tp)
4167 if (r8168_check_dash(tp))
4170 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4171 tp->mac_version == RTL_GIGA_MAC_VER_33)
4172 rtl_ephy_write(tp, 0x19, 0xff64);
4174 if (rtl_wol_pll_power_down(tp))
4177 switch (tp->mac_version) {
4178 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
4179 case RTL_GIGA_MAC_VER_37:
4180 case RTL_GIGA_MAC_VER_39:
4181 case RTL_GIGA_MAC_VER_43:
4182 case RTL_GIGA_MAC_VER_44:
4183 case RTL_GIGA_MAC_VER_45:
4184 case RTL_GIGA_MAC_VER_46:
4185 case RTL_GIGA_MAC_VER_47:
4186 case RTL_GIGA_MAC_VER_48:
4187 case RTL_GIGA_MAC_VER_50:
4188 case RTL_GIGA_MAC_VER_51:
4189 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4191 case RTL_GIGA_MAC_VER_40:
4192 case RTL_GIGA_MAC_VER_41:
4193 case RTL_GIGA_MAC_VER_49:
4194 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4195 0xfc000000, ERIAR_EXGMAC);
4196 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4201 static void r8168_pll_power_up(struct rtl8169_private *tp)
4203 switch (tp->mac_version) {
4204 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
4205 case RTL_GIGA_MAC_VER_37:
4206 case RTL_GIGA_MAC_VER_39:
4207 case RTL_GIGA_MAC_VER_43:
4208 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4210 case RTL_GIGA_MAC_VER_44:
4211 case RTL_GIGA_MAC_VER_45:
4212 case RTL_GIGA_MAC_VER_46:
4213 case RTL_GIGA_MAC_VER_47:
4214 case RTL_GIGA_MAC_VER_48:
4215 case RTL_GIGA_MAC_VER_50:
4216 case RTL_GIGA_MAC_VER_51:
4217 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4219 case RTL_GIGA_MAC_VER_40:
4220 case RTL_GIGA_MAC_VER_41:
4221 case RTL_GIGA_MAC_VER_49:
4222 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4223 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4224 0x00000000, ERIAR_EXGMAC);
4228 phy_resume(tp->dev->phydev);
4229 /* give MAC/PHY some time to resume */
4233 static void rtl_pll_power_down(struct rtl8169_private *tp)
4235 switch (tp->mac_version) {
4236 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4237 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4240 r8168_pll_power_down(tp);
4244 static void rtl_pll_power_up(struct rtl8169_private *tp)
4246 switch (tp->mac_version) {
4247 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4248 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4251 r8168_pll_power_up(tp);
4255 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4257 switch (tp->mac_version) {
4258 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4259 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
4260 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4262 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
4263 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
4264 case RTL_GIGA_MAC_VER_38:
4265 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4267 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4268 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4271 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
4276 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4278 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4281 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4283 if (tp->jumbo_ops.enable) {
4284 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4285 tp->jumbo_ops.enable(tp);
4286 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4290 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4292 if (tp->jumbo_ops.disable) {
4293 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4294 tp->jumbo_ops.disable(tp);
4295 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4299 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4301 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4302 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
4303 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4306 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4308 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4309 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
4310 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4313 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4315 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4318 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4320 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4323 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4325 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4326 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4327 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
4328 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4331 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4333 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4334 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4335 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
4336 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4339 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4341 rtl_tx_performance_tweak(tp,
4342 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4345 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4347 rtl_tx_performance_tweak(tp,
4348 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4351 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4353 r8168b_0_hw_jumbo_enable(tp);
4355 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
4358 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4360 r8168b_0_hw_jumbo_disable(tp);
4362 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4365 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4367 struct jumbo_ops *ops = &tp->jumbo_ops;
4369 switch (tp->mac_version) {
4370 case RTL_GIGA_MAC_VER_11:
4371 ops->disable = r8168b_0_hw_jumbo_disable;
4372 ops->enable = r8168b_0_hw_jumbo_enable;
4374 case RTL_GIGA_MAC_VER_12:
4375 case RTL_GIGA_MAC_VER_17:
4376 ops->disable = r8168b_1_hw_jumbo_disable;
4377 ops->enable = r8168b_1_hw_jumbo_enable;
4379 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4380 case RTL_GIGA_MAC_VER_19:
4381 case RTL_GIGA_MAC_VER_20:
4382 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4383 case RTL_GIGA_MAC_VER_22:
4384 case RTL_GIGA_MAC_VER_23:
4385 case RTL_GIGA_MAC_VER_24:
4386 case RTL_GIGA_MAC_VER_25:
4387 case RTL_GIGA_MAC_VER_26:
4388 ops->disable = r8168c_hw_jumbo_disable;
4389 ops->enable = r8168c_hw_jumbo_enable;
4391 case RTL_GIGA_MAC_VER_27:
4392 case RTL_GIGA_MAC_VER_28:
4393 ops->disable = r8168dp_hw_jumbo_disable;
4394 ops->enable = r8168dp_hw_jumbo_enable;
4396 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4397 case RTL_GIGA_MAC_VER_32:
4398 case RTL_GIGA_MAC_VER_33:
4399 case RTL_GIGA_MAC_VER_34:
4400 ops->disable = r8168e_hw_jumbo_disable;
4401 ops->enable = r8168e_hw_jumbo_enable;
4405 * No action needed for jumbo frames with 8169.
4406 * No jumbo for 810x at all.
4408 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4410 ops->disable = NULL;
4416 DECLARE_RTL_COND(rtl_chipcmd_cond)
4418 return RTL_R8(tp, ChipCmd) & CmdReset;
4421 static void rtl_hw_reset(struct rtl8169_private *tp)
4423 RTL_W8(tp, ChipCmd, CmdReset);
4425 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4428 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4430 struct rtl_fw *rtl_fw;
4434 name = rtl_lookup_firmware_name(tp);
4436 goto out_no_firmware;
4438 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4442 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
4446 rc = rtl_check_firmware(tp, rtl_fw);
4448 goto err_release_firmware;
4450 tp->rtl_fw = rtl_fw;
4454 err_release_firmware:
4455 release_firmware(rtl_fw->fw);
4459 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4466 static void rtl_request_firmware(struct rtl8169_private *tp)
4468 if (IS_ERR(tp->rtl_fw))
4469 rtl_request_uncached_firmware(tp);
4472 static void rtl_rx_close(struct rtl8169_private *tp)
4474 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4477 DECLARE_RTL_COND(rtl_npq_cond)
4479 return RTL_R8(tp, TxPoll) & NPQ;
4482 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4484 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
4487 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4489 /* Disable interrupts */
4490 rtl8169_irq_mask_and_ack(tp);
4494 switch (tp->mac_version) {
4495 case RTL_GIGA_MAC_VER_27:
4496 case RTL_GIGA_MAC_VER_28:
4497 case RTL_GIGA_MAC_VER_31:
4498 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4500 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4501 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4502 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4503 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4506 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4514 static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
4516 u32 val = TX_DMA_BURST << TxDMAShift |
4517 InterFrameGap << TxInterFrameGapShift;
4519 if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
4520 tp->mac_version != RTL_GIGA_MAC_VER_39)
4521 val |= TXCFG_AUTO_FIFO;
4523 RTL_W32(tp, TxConfig, val);
4526 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
4528 /* Low hurts. Let's disable the filtering. */
4529 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
4532 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
4535 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4536 * register to be written before TxDescAddrLow to work.
4537 * Switching from MMIO to I/O access fixes the issue as well.
4539 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4540 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4541 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4542 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4545 static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
4549 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4551 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4556 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4559 RTL_W32(tp, 0x7c, val);
4562 static void rtl_set_rx_mode(struct net_device *dev)
4564 struct rtl8169_private *tp = netdev_priv(dev);
4565 u32 mc_filter[2]; /* Multicast hash filter */
4569 if (dev->flags & IFF_PROMISC) {
4570 /* Unconditionally log net taps. */
4571 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4573 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4575 mc_filter[1] = mc_filter[0] = 0xffffffff;
4576 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4577 (dev->flags & IFF_ALLMULTI)) {
4578 /* Too many to filter perfectly -- accept all multicasts. */
4579 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4580 mc_filter[1] = mc_filter[0] = 0xffffffff;
4582 struct netdev_hw_addr *ha;
4584 rx_mode = AcceptBroadcast | AcceptMyPhys;
4585 mc_filter[1] = mc_filter[0] = 0;
4586 netdev_for_each_mc_addr(ha, dev) {
4587 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4588 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4589 rx_mode |= AcceptMulticast;
4593 if (dev->features & NETIF_F_RXALL)
4594 rx_mode |= (AcceptErr | AcceptRunt);
4596 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4598 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4599 u32 data = mc_filter[0];
4601 mc_filter[0] = swab32(mc_filter[1]);
4602 mc_filter[1] = swab32(data);
4605 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4606 mc_filter[1] = mc_filter[0] = 0xffffffff;
4608 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4609 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
4611 RTL_W32(tp, RxConfig, tmp);
4614 static void rtl_hw_start(struct rtl8169_private *tp)
4616 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4620 rtl_set_rx_max_size(tp);
4621 rtl_set_rx_tx_desc_registers(tp);
4622 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4624 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4625 RTL_R8(tp, IntrMask);
4626 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
4628 rtl_set_tx_config_registers(tp);
4630 rtl_set_rx_mode(tp->dev);
4631 /* no early-rx interrupts */
4632 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4636 static void rtl_hw_start_8169(struct rtl8169_private *tp)
4638 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4639 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4641 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
4643 tp->cp_cmd |= PCIMulRW;
4645 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4646 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4647 netif_dbg(tp, drv, tp->dev,
4648 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
4649 tp->cp_cmd |= (1 << 14);
4652 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4654 rtl8169_set_magic_reg(tp, tp->mac_version);
4657 * Undocumented corner. Supposedly:
4658 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4660 RTL_W16(tp, IntrMitigate, 0x0000);
4662 RTL_W32(tp, RxMissed, 0);
4665 DECLARE_RTL_COND(rtl_csiar_cond)
4667 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
4670 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4672 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4674 RTL_W32(tp, CSIDR, value);
4675 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4676 CSIAR_BYTE_ENABLE | func << 16);
4678 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4681 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4683 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4685 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4688 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4689 RTL_R32(tp, CSIDR) : ~0;
4692 static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
4694 struct pci_dev *pdev = tp->pci_dev;
4697 /* According to Realtek the value at config space address 0x070f
4698 * controls the L0s/L1 entrance latency. We try standard ECAM access
4699 * first and if it fails fall back to CSI.
4701 if (pdev->cfg_size > 0x070f &&
4702 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4705 netdev_notice_once(tp->dev,
4706 "No native access to PCI extended config space, falling back to CSI\n");
4707 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4708 rtl_csi_write(tp, 0x070c, csi | val << 24);
4711 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
4713 rtl_csi_access_enable(tp, 0x27);
4717 unsigned int offset;
4722 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4728 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4729 rtl_ephy_write(tp, e->offset, w);
4734 static void rtl_disable_clock_request(struct rtl8169_private *tp)
4736 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
4737 PCI_EXP_LNKCTL_CLKREQ_EN);
4740 static void rtl_enable_clock_request(struct rtl8169_private *tp)
4742 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
4743 PCI_EXP_LNKCTL_CLKREQ_EN);
4746 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4750 data = RTL_R8(tp, Config3);
4755 data &= ~Rdy_to_L23;
4757 RTL_W8(tp, Config3, data);
4760 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4763 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
4764 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
4766 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4767 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4773 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4775 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4777 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4778 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4780 if (tp->dev->mtu <= ETH_DATA_LEN) {
4781 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
4782 PCI_EXP_DEVCTL_NOSNOOP_EN);
4786 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4788 rtl_hw_start_8168bb(tp);
4790 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4792 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4795 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4797 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
4799 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4801 if (tp->dev->mtu <= ETH_DATA_LEN)
4802 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4804 rtl_disable_clock_request(tp);
4806 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4807 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4810 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4812 static const struct ephy_info e_info_8168cp[] = {
4813 { 0x01, 0, 0x0001 },
4814 { 0x02, 0x0800, 0x1000 },
4815 { 0x03, 0, 0x0042 },
4816 { 0x06, 0x0080, 0x0000 },
4820 rtl_set_def_aspm_entry_latency(tp);
4822 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4824 __rtl_hw_start_8168cp(tp);
4827 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4829 rtl_set_def_aspm_entry_latency(tp);
4831 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4833 if (tp->dev->mtu <= ETH_DATA_LEN)
4834 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4836 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4837 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4840 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4842 rtl_set_def_aspm_entry_latency(tp);
4844 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4847 RTL_W8(tp, DBG_REG, 0x20);
4849 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4851 if (tp->dev->mtu <= ETH_DATA_LEN)
4852 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4854 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4855 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4858 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4860 static const struct ephy_info e_info_8168c_1[] = {
4861 { 0x02, 0x0800, 0x1000 },
4862 { 0x03, 0, 0x0002 },
4863 { 0x06, 0x0080, 0x0000 }
4866 rtl_set_def_aspm_entry_latency(tp);
4868 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4870 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4872 __rtl_hw_start_8168cp(tp);
4875 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4877 static const struct ephy_info e_info_8168c_2[] = {
4878 { 0x01, 0, 0x0001 },
4879 { 0x03, 0x0400, 0x0220 }
4882 rtl_set_def_aspm_entry_latency(tp);
4884 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4886 __rtl_hw_start_8168cp(tp);
4889 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4891 rtl_hw_start_8168c_2(tp);
4894 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4896 rtl_set_def_aspm_entry_latency(tp);
4898 __rtl_hw_start_8168cp(tp);
4901 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4903 rtl_set_def_aspm_entry_latency(tp);
4905 rtl_disable_clock_request(tp);
4907 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4909 if (tp->dev->mtu <= ETH_DATA_LEN)
4910 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4912 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4913 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4916 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
4918 rtl_set_def_aspm_entry_latency(tp);
4920 if (tp->dev->mtu <= ETH_DATA_LEN)
4921 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4923 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4925 rtl_disable_clock_request(tp);
4928 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
4930 static const struct ephy_info e_info_8168d_4[] = {
4931 { 0x0b, 0x0000, 0x0048 },
4932 { 0x19, 0x0020, 0x0050 },
4933 { 0x0c, 0x0100, 0x0020 }
4936 rtl_set_def_aspm_entry_latency(tp);
4938 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4940 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4942 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
4944 rtl_enable_clock_request(tp);
4947 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
4949 static const struct ephy_info e_info_8168e_1[] = {
4950 { 0x00, 0x0200, 0x0100 },
4951 { 0x00, 0x0000, 0x0004 },
4952 { 0x06, 0x0002, 0x0001 },
4953 { 0x06, 0x0000, 0x0030 },
4954 { 0x07, 0x0000, 0x2000 },
4955 { 0x00, 0x0000, 0x0020 },
4956 { 0x03, 0x5800, 0x2000 },
4957 { 0x03, 0x0000, 0x0001 },
4958 { 0x01, 0x0800, 0x1000 },
4959 { 0x07, 0x0000, 0x4000 },
4960 { 0x1e, 0x0000, 0x2000 },
4961 { 0x19, 0xffff, 0xfe6c },
4962 { 0x0a, 0x0000, 0x0040 }
4965 rtl_set_def_aspm_entry_latency(tp);
4967 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
4969 if (tp->dev->mtu <= ETH_DATA_LEN)
4970 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4972 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4974 rtl_disable_clock_request(tp);
4976 /* Reset tx FIFO pointer */
4977 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4978 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
4980 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
4983 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
4985 static const struct ephy_info e_info_8168e_2[] = {
4986 { 0x09, 0x0000, 0x0080 },
4987 { 0x19, 0x0000, 0x0224 }
4990 rtl_set_def_aspm_entry_latency(tp);
4992 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4994 if (tp->dev->mtu <= ETH_DATA_LEN)
4995 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4997 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4998 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4999 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5000 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5001 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5002 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5003 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5004 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5006 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5008 rtl_disable_clock_request(tp);
5010 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5012 /* Adjust EEE LED frequency */
5013 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5015 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5016 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5017 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5019 rtl_hw_aspm_clkreq_enable(tp, true);
5022 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5024 rtl_set_def_aspm_entry_latency(tp);
5026 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5028 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5029 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5030 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5031 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5032 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5033 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5034 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5035 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5036 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5037 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5039 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5041 rtl_disable_clock_request(tp);
5043 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5044 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5045 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5046 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5049 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5051 static const struct ephy_info e_info_8168f_1[] = {
5052 { 0x06, 0x00c0, 0x0020 },
5053 { 0x08, 0x0001, 0x0002 },
5054 { 0x09, 0x0000, 0x0080 },
5055 { 0x19, 0x0000, 0x0224 }
5058 rtl_hw_start_8168f(tp);
5060 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5062 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5064 /* Adjust EEE LED frequency */
5065 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5068 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5070 static const struct ephy_info e_info_8168f_1[] = {
5071 { 0x06, 0x00c0, 0x0020 },
5072 { 0x0f, 0xffff, 0x5200 },
5073 { 0x1e, 0x0000, 0x4000 },
5074 { 0x19, 0x0000, 0x0224 }
5077 rtl_hw_start_8168f(tp);
5078 rtl_pcie_state_l2l3_enable(tp, false);
5080 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5082 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5085 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
5087 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5088 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5089 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5090 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5092 rtl_set_def_aspm_entry_latency(tp);
5094 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5096 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5097 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5098 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
5100 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5101 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5103 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5104 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5106 /* Adjust EEE LED frequency */
5107 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5109 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5110 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5112 rtl_pcie_state_l2l3_enable(tp, false);
5115 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5117 static const struct ephy_info e_info_8168g_1[] = {
5118 { 0x00, 0x0000, 0x0008 },
5119 { 0x0c, 0x37d0, 0x0820 },
5120 { 0x1e, 0x0000, 0x0001 },
5121 { 0x19, 0x8000, 0x0000 }
5124 rtl_hw_start_8168g(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_8168g_1, ARRAY_SIZE(e_info_8168g_1));
5129 rtl_hw_aspm_clkreq_enable(tp, true);
5132 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5134 static const struct ephy_info e_info_8168g_2[] = {
5135 { 0x00, 0x0000, 0x0008 },
5136 { 0x0c, 0x3df0, 0x0200 },
5137 { 0x19, 0xffff, 0xfc00 },
5138 { 0x1e, 0xffff, 0x20eb }
5141 rtl_hw_start_8168g(tp);
5143 /* disable aspm and clock request before access ephy */
5144 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5145 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5146 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5149 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5151 static const struct ephy_info e_info_8411_2[] = {
5152 { 0x00, 0x0000, 0x0008 },
5153 { 0x0c, 0x3df0, 0x0200 },
5154 { 0x0f, 0xffff, 0x5200 },
5155 { 0x19, 0x0020, 0x0000 },
5156 { 0x1e, 0x0000, 0x2000 }
5159 rtl_hw_start_8168g(tp);
5161 /* disable aspm and clock request before access ephy */
5162 rtl_hw_aspm_clkreq_enable(tp, false);
5163 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5164 rtl_hw_aspm_clkreq_enable(tp, true);
5167 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5171 static const struct ephy_info e_info_8168h_1[] = {
5172 { 0x1e, 0x0800, 0x0001 },
5173 { 0x1d, 0x0000, 0x0800 },
5174 { 0x05, 0xffff, 0x2089 },
5175 { 0x06, 0xffff, 0x5881 },
5176 { 0x04, 0xffff, 0x154a },
5177 { 0x01, 0xffff, 0x068b }
5180 /* disable aspm and clock request before access ephy */
5181 rtl_hw_aspm_clkreq_enable(tp, false);
5182 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5184 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5185 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5186 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5187 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5189 rtl_set_def_aspm_entry_latency(tp);
5191 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5193 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5194 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5196 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
5198 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
5200 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5202 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5203 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5205 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5206 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5208 /* Adjust EEE LED frequency */
5209 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5211 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5212 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5214 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5216 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5218 rtl_pcie_state_l2l3_enable(tp, false);
5220 rtl_writephy(tp, 0x1f, 0x0c42);
5221 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
5222 rtl_writephy(tp, 0x1f, 0x0000);
5223 if (rg_saw_cnt > 0) {
5226 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5227 sw_cnt_1ms_ini &= 0x0fff;
5228 data = r8168_mac_ocp_read(tp, 0xd412);
5230 data |= sw_cnt_1ms_ini;
5231 r8168_mac_ocp_write(tp, 0xd412, data);
5234 data = r8168_mac_ocp_read(tp, 0xe056);
5237 r8168_mac_ocp_write(tp, 0xe056, data);
5239 data = r8168_mac_ocp_read(tp, 0xe052);
5242 r8168_mac_ocp_write(tp, 0xe052, data);
5244 data = r8168_mac_ocp_read(tp, 0xe0d6);
5247 r8168_mac_ocp_write(tp, 0xe0d6, data);
5249 data = r8168_mac_ocp_read(tp, 0xd420);
5252 r8168_mac_ocp_write(tp, 0xd420, data);
5254 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5255 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5256 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5257 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
5259 rtl_hw_aspm_clkreq_enable(tp, true);
5262 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5264 rtl8168ep_stop_cmac(tp);
5266 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5267 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5268 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5269 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5271 rtl_set_def_aspm_entry_latency(tp);
5273 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5275 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5276 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5278 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5280 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5282 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5283 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5285 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5286 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5288 /* Adjust EEE LED frequency */
5289 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5291 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5293 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5295 rtl_pcie_state_l2l3_enable(tp, false);
5298 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5300 static const struct ephy_info e_info_8168ep_1[] = {
5301 { 0x00, 0xffff, 0x10ab },
5302 { 0x06, 0xffff, 0xf030 },
5303 { 0x08, 0xffff, 0x2006 },
5304 { 0x0d, 0xffff, 0x1666 },
5305 { 0x0c, 0x3ff0, 0x0000 }
5308 /* disable aspm and clock request before access ephy */
5309 rtl_hw_aspm_clkreq_enable(tp, false);
5310 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5312 rtl_hw_start_8168ep(tp);
5314 rtl_hw_aspm_clkreq_enable(tp, true);
5317 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5319 static const struct ephy_info e_info_8168ep_2[] = {
5320 { 0x00, 0xffff, 0x10a3 },
5321 { 0x19, 0xffff, 0xfc00 },
5322 { 0x1e, 0xffff, 0x20ea }
5325 /* disable aspm and clock request before access ephy */
5326 rtl_hw_aspm_clkreq_enable(tp, false);
5327 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5329 rtl_hw_start_8168ep(tp);
5331 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5332 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5334 rtl_hw_aspm_clkreq_enable(tp, true);
5337 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5340 static const struct ephy_info e_info_8168ep_3[] = {
5341 { 0x00, 0xffff, 0x10a3 },
5342 { 0x19, 0xffff, 0x7c00 },
5343 { 0x1e, 0xffff, 0x20eb },
5344 { 0x0d, 0xffff, 0x1666 }
5347 /* disable aspm and clock request before access ephy */
5348 rtl_hw_aspm_clkreq_enable(tp, false);
5349 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5351 rtl_hw_start_8168ep(tp);
5353 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5354 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5356 data = r8168_mac_ocp_read(tp, 0xd3e2);
5359 r8168_mac_ocp_write(tp, 0xd3e2, data);
5361 data = r8168_mac_ocp_read(tp, 0xd3e4);
5363 r8168_mac_ocp_write(tp, 0xd3e4, data);
5365 data = r8168_mac_ocp_read(tp, 0xe860);
5367 r8168_mac_ocp_write(tp, 0xe860, data);
5369 rtl_hw_aspm_clkreq_enable(tp, true);
5372 static void rtl_hw_start_8168(struct rtl8169_private *tp)
5374 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5376 tp->cp_cmd &= ~INTT_MASK;
5377 tp->cp_cmd |= PktCntrDisable | INTT_1;
5378 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5380 RTL_W16(tp, IntrMitigate, 0x5151);
5382 /* Work around for RxFIFO overflow. */
5383 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5384 tp->irq_mask |= RxFIFOOver;
5385 tp->irq_mask &= ~RxOverflow;
5388 switch (tp->mac_version) {
5389 case RTL_GIGA_MAC_VER_11:
5390 rtl_hw_start_8168bb(tp);
5393 case RTL_GIGA_MAC_VER_12:
5394 case RTL_GIGA_MAC_VER_17:
5395 rtl_hw_start_8168bef(tp);
5398 case RTL_GIGA_MAC_VER_18:
5399 rtl_hw_start_8168cp_1(tp);
5402 case RTL_GIGA_MAC_VER_19:
5403 rtl_hw_start_8168c_1(tp);
5406 case RTL_GIGA_MAC_VER_20:
5407 rtl_hw_start_8168c_2(tp);
5410 case RTL_GIGA_MAC_VER_21:
5411 rtl_hw_start_8168c_3(tp);
5414 case RTL_GIGA_MAC_VER_22:
5415 rtl_hw_start_8168c_4(tp);
5418 case RTL_GIGA_MAC_VER_23:
5419 rtl_hw_start_8168cp_2(tp);
5422 case RTL_GIGA_MAC_VER_24:
5423 rtl_hw_start_8168cp_3(tp);
5426 case RTL_GIGA_MAC_VER_25:
5427 case RTL_GIGA_MAC_VER_26:
5428 case RTL_GIGA_MAC_VER_27:
5429 rtl_hw_start_8168d(tp);
5432 case RTL_GIGA_MAC_VER_28:
5433 rtl_hw_start_8168d_4(tp);
5436 case RTL_GIGA_MAC_VER_31:
5437 rtl_hw_start_8168dp(tp);
5440 case RTL_GIGA_MAC_VER_32:
5441 case RTL_GIGA_MAC_VER_33:
5442 rtl_hw_start_8168e_1(tp);
5444 case RTL_GIGA_MAC_VER_34:
5445 rtl_hw_start_8168e_2(tp);
5448 case RTL_GIGA_MAC_VER_35:
5449 case RTL_GIGA_MAC_VER_36:
5450 rtl_hw_start_8168f_1(tp);
5453 case RTL_GIGA_MAC_VER_38:
5454 rtl_hw_start_8411(tp);
5457 case RTL_GIGA_MAC_VER_40:
5458 case RTL_GIGA_MAC_VER_41:
5459 rtl_hw_start_8168g_1(tp);
5461 case RTL_GIGA_MAC_VER_42:
5462 rtl_hw_start_8168g_2(tp);
5465 case RTL_GIGA_MAC_VER_44:
5466 rtl_hw_start_8411_2(tp);
5469 case RTL_GIGA_MAC_VER_45:
5470 case RTL_GIGA_MAC_VER_46:
5471 rtl_hw_start_8168h_1(tp);
5474 case RTL_GIGA_MAC_VER_49:
5475 rtl_hw_start_8168ep_1(tp);
5478 case RTL_GIGA_MAC_VER_50:
5479 rtl_hw_start_8168ep_2(tp);
5482 case RTL_GIGA_MAC_VER_51:
5483 rtl_hw_start_8168ep_3(tp);
5487 netif_err(tp, drv, tp->dev,
5488 "unknown chipset (mac_version = %d)\n",
5494 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5496 static const struct ephy_info e_info_8102e_1[] = {
5497 { 0x01, 0, 0x6e65 },
5498 { 0x02, 0, 0x091f },
5499 { 0x03, 0, 0xc2f9 },
5500 { 0x06, 0, 0xafb5 },
5501 { 0x07, 0, 0x0e00 },
5502 { 0x19, 0, 0xec80 },
5503 { 0x01, 0, 0x2e65 },
5508 rtl_set_def_aspm_entry_latency(tp);
5510 RTL_W8(tp, DBG_REG, FIX_NAK_1);
5512 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5515 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5516 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5518 cfg1 = RTL_R8(tp, Config1);
5519 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5520 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
5522 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5525 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5527 rtl_set_def_aspm_entry_latency(tp);
5529 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5531 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5532 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5535 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5537 rtl_hw_start_8102e_2(tp);
5539 rtl_ephy_write(tp, 0x03, 0xc2f9);
5542 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5544 static const struct ephy_info e_info_8105e_1[] = {
5545 { 0x07, 0, 0x4000 },
5546 { 0x19, 0, 0x0200 },
5547 { 0x19, 0, 0x0020 },
5548 { 0x1e, 0, 0x2000 },
5549 { 0x03, 0, 0x0001 },
5550 { 0x19, 0, 0x0100 },
5551 { 0x19, 0, 0x0004 },
5555 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5556 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5558 /* Disable Early Tally Counter */
5559 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
5561 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5562 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5564 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5566 rtl_pcie_state_l2l3_enable(tp, false);
5569 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5571 rtl_hw_start_8105e_1(tp);
5572 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5575 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5577 static const struct ephy_info e_info_8402[] = {
5578 { 0x19, 0xffff, 0xff64 },
5582 rtl_set_def_aspm_entry_latency(tp);
5584 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5585 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5587 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5589 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
5591 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5593 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5594 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5595 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5596 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5597 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5598 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5599 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
5601 rtl_pcie_state_l2l3_enable(tp, false);
5604 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5606 rtl_hw_aspm_clkreq_enable(tp, false);
5608 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5609 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5611 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5612 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5613 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5615 rtl_pcie_state_l2l3_enable(tp, false);
5616 rtl_hw_aspm_clkreq_enable(tp, true);
5619 static void rtl_hw_start_8101(struct rtl8169_private *tp)
5621 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5622 tp->irq_mask &= ~RxFIFOOver;
5624 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5625 tp->mac_version == RTL_GIGA_MAC_VER_16)
5626 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
5627 PCI_EXP_DEVCTL_NOSNOOP_EN);
5629 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5631 tp->cp_cmd &= CPCMD_QUIRK_MASK;
5632 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5634 switch (tp->mac_version) {
5635 case RTL_GIGA_MAC_VER_07:
5636 rtl_hw_start_8102e_1(tp);
5639 case RTL_GIGA_MAC_VER_08:
5640 rtl_hw_start_8102e_3(tp);
5643 case RTL_GIGA_MAC_VER_09:
5644 rtl_hw_start_8102e_2(tp);
5647 case RTL_GIGA_MAC_VER_29:
5648 rtl_hw_start_8105e_1(tp);
5650 case RTL_GIGA_MAC_VER_30:
5651 rtl_hw_start_8105e_2(tp);
5654 case RTL_GIGA_MAC_VER_37:
5655 rtl_hw_start_8402(tp);
5658 case RTL_GIGA_MAC_VER_39:
5659 rtl_hw_start_8106(tp);
5661 case RTL_GIGA_MAC_VER_43:
5662 rtl_hw_start_8168g_2(tp);
5664 case RTL_GIGA_MAC_VER_47:
5665 case RTL_GIGA_MAC_VER_48:
5666 rtl_hw_start_8168h_1(tp);
5670 RTL_W16(tp, IntrMitigate, 0x0000);
5673 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5675 struct rtl8169_private *tp = netdev_priv(dev);
5677 if (new_mtu > ETH_DATA_LEN)
5678 rtl_hw_jumbo_enable(tp);
5680 rtl_hw_jumbo_disable(tp);
5683 netdev_update_features(dev);
5688 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5690 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5691 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5694 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5695 void **data_buff, struct RxDesc *desc)
5697 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5698 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5702 rtl8169_make_unusable_by_asic(desc);
5705 static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
5707 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5709 /* Force memory writes to complete before releasing descriptor */
5712 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
5715 static inline void *rtl8169_align(void *data)
5717 return (void *)ALIGN((long)data, 16);
5720 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5721 struct RxDesc *desc)
5725 struct device *d = tp_to_dev(tp);
5726 int node = dev_to_node(d);
5728 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
5732 if (rtl8169_align(data) != data) {
5734 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
5739 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
5741 if (unlikely(dma_mapping_error(d, mapping))) {
5742 if (net_ratelimit())
5743 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5747 desc->addr = cpu_to_le64(mapping);
5748 rtl8169_mark_to_asic(desc);
5756 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5760 for (i = 0; i < NUM_RX_DESC; i++) {
5761 if (tp->Rx_databuff[i]) {
5762 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5763 tp->RxDescArray + i);
5768 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5770 desc->opts1 |= cpu_to_le32(RingEnd);
5773 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5777 for (i = 0; i < NUM_RX_DESC; i++) {
5780 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5782 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5785 tp->Rx_databuff[i] = data;
5788 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5792 rtl8169_rx_clear(tp);
5796 static int rtl8169_init_ring(struct rtl8169_private *tp)
5798 rtl8169_init_ring_indexes(tp);
5800 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5801 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
5803 return rtl8169_rx_fill(tp);
5806 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5807 struct TxDesc *desc)
5809 unsigned int len = tx_skb->len;
5811 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5819 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5824 for (i = 0; i < n; i++) {
5825 unsigned int entry = (start + i) % NUM_TX_DESC;
5826 struct ring_info *tx_skb = tp->tx_skb + entry;
5827 unsigned int len = tx_skb->len;
5830 struct sk_buff *skb = tx_skb->skb;
5832 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
5833 tp->TxDescArray + entry);
5835 dev_consume_skb_any(skb);
5842 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5844 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5845 tp->cur_tx = tp->dirty_tx = 0;
5846 netdev_reset_queue(tp->dev);
5849 static void rtl_reset_work(struct rtl8169_private *tp)
5851 struct net_device *dev = tp->dev;
5854 napi_disable(&tp->napi);
5855 netif_stop_queue(dev);
5856 synchronize_sched();
5858 rtl8169_hw_reset(tp);
5860 for (i = 0; i < NUM_RX_DESC; i++)
5861 rtl8169_mark_to_asic(tp->RxDescArray + i);
5863 rtl8169_tx_clear(tp);
5864 rtl8169_init_ring_indexes(tp);
5866 napi_enable(&tp->napi);
5868 netif_wake_queue(dev);
5871 static void rtl8169_tx_timeout(struct net_device *dev)
5873 struct rtl8169_private *tp = netdev_priv(dev);
5875 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5878 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5881 struct skb_shared_info *info = skb_shinfo(skb);
5882 unsigned int cur_frag, entry;
5883 struct TxDesc *uninitialized_var(txd);
5884 struct device *d = tp_to_dev(tp);
5887 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5888 const skb_frag_t *frag = info->frags + cur_frag;
5893 entry = (entry + 1) % NUM_TX_DESC;
5895 txd = tp->TxDescArray + entry;
5896 len = skb_frag_size(frag);
5897 addr = skb_frag_address(frag);
5898 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5899 if (unlikely(dma_mapping_error(d, mapping))) {
5900 if (net_ratelimit())
5901 netif_err(tp, drv, tp->dev,
5902 "Failed to map TX fragments DMA!\n");
5906 status = opts[0] | len;
5907 if (entry == NUM_TX_DESC - 1)
5910 txd->opts1 = cpu_to_le32(status);
5911 txd->opts2 = cpu_to_le32(opts[1]);
5912 txd->addr = cpu_to_le64(mapping);
5914 tp->tx_skb[entry].len = len;
5918 tp->tx_skb[entry].skb = skb;
5919 txd->opts1 |= cpu_to_le32(LastFrag);
5925 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5929 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5931 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5934 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5935 struct net_device *dev);
5936 /* r8169_csum_workaround()
5937 * The hw limites the value the transport offset. When the offset is out of the
5938 * range, calculate the checksum by sw.
5940 static void r8169_csum_workaround(struct rtl8169_private *tp,
5941 struct sk_buff *skb)
5943 if (skb_shinfo(skb)->gso_size) {
5944 netdev_features_t features = tp->dev->features;
5945 struct sk_buff *segs, *nskb;
5947 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5948 segs = skb_gso_segment(skb, features);
5949 if (IS_ERR(segs) || !segs)
5956 rtl8169_start_xmit(nskb, tp->dev);
5959 dev_consume_skb_any(skb);
5960 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5961 if (skb_checksum_help(skb) < 0)
5964 rtl8169_start_xmit(skb, tp->dev);
5966 struct net_device_stats *stats;
5969 stats = &tp->dev->stats;
5970 stats->tx_dropped++;
5971 dev_kfree_skb_any(skb);
5975 /* msdn_giant_send_check()
5976 * According to the document of microsoft, the TCP Pseudo Header excludes the
5977 * packet length for IPv6 TCP large packets.
5979 static int msdn_giant_send_check(struct sk_buff *skb)
5981 const struct ipv6hdr *ipv6h;
5985 ret = skb_cow_head(skb, 0);
5989 ipv6h = ipv6_hdr(skb);
5993 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5998 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
5999 struct sk_buff *skb, u32 *opts)
6001 u32 mss = skb_shinfo(skb)->gso_size;
6005 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6006 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6007 const struct iphdr *ip = ip_hdr(skb);
6009 if (ip->protocol == IPPROTO_TCP)
6010 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6011 else if (ip->protocol == IPPROTO_UDP)
6012 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6020 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6021 struct sk_buff *skb, u32 *opts)
6023 u32 transport_offset = (u32)skb_transport_offset(skb);
6024 u32 mss = skb_shinfo(skb)->gso_size;
6027 if (transport_offset > GTTCPHO_MAX) {
6028 netif_warn(tp, tx_err, tp->dev,
6029 "Invalid transport offset 0x%x for TSO\n",
6034 switch (vlan_get_protocol(skb)) {
6035 case htons(ETH_P_IP):
6036 opts[0] |= TD1_GTSENV4;
6039 case htons(ETH_P_IPV6):
6040 if (msdn_giant_send_check(skb))
6043 opts[0] |= TD1_GTSENV6;
6051 opts[0] |= transport_offset << GTTCPHO_SHIFT;
6052 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
6053 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6056 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6057 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
6059 if (transport_offset > TCPHO_MAX) {
6060 netif_warn(tp, tx_err, tp->dev,
6061 "Invalid transport offset 0x%x\n",
6066 switch (vlan_get_protocol(skb)) {
6067 case htons(ETH_P_IP):
6068 opts[1] |= TD1_IPv4_CS;
6069 ip_protocol = ip_hdr(skb)->protocol;
6072 case htons(ETH_P_IPV6):
6073 opts[1] |= TD1_IPv6_CS;
6074 ip_protocol = ipv6_hdr(skb)->nexthdr;
6078 ip_protocol = IPPROTO_RAW;
6082 if (ip_protocol == IPPROTO_TCP)
6083 opts[1] |= TD1_TCP_CS;
6084 else if (ip_protocol == IPPROTO_UDP)
6085 opts[1] |= TD1_UDP_CS;
6089 opts[1] |= transport_offset << TCPHO_SHIFT;
6091 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6092 return !eth_skb_pad(skb);
6098 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6099 struct net_device *dev)
6101 struct rtl8169_private *tp = netdev_priv(dev);
6102 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
6103 struct TxDesc *txd = tp->TxDescArray + entry;
6104 struct device *d = tp_to_dev(tp);
6110 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
6111 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
6115 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
6118 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6121 if (!tp->tso_csum(tp, skb, opts)) {
6122 r8169_csum_workaround(tp, skb);
6123 return NETDEV_TX_OK;
6126 len = skb_headlen(skb);
6127 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
6128 if (unlikely(dma_mapping_error(d, mapping))) {
6129 if (net_ratelimit())
6130 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
6134 tp->tx_skb[entry].len = len;
6135 txd->addr = cpu_to_le64(mapping);
6137 frags = rtl8169_xmit_frags(tp, skb, opts);
6141 opts[0] |= FirstFrag;
6143 opts[0] |= FirstFrag | LastFrag;
6144 tp->tx_skb[entry].skb = skb;
6147 txd->opts2 = cpu_to_le32(opts[1]);
6149 netdev_sent_queue(dev, skb->len);
6151 skb_tx_timestamp(skb);
6153 /* Force memory writes to complete before releasing descriptor */
6156 /* Anti gcc 2.95.3 bugware (sic) */
6157 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
6158 txd->opts1 = cpu_to_le32(status);
6160 /* Force all memory writes to complete before notifying device */
6163 tp->cur_tx += frags + 1;
6165 RTL_W8(tp, TxPoll, NPQ);
6169 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6170 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6171 * not miss a ring update when it notices a stopped queue.
6174 netif_stop_queue(dev);
6175 /* Sync with rtl_tx:
6176 * - publish queue status and cur_tx ring index (write barrier)
6177 * - refresh dirty_tx ring index (read barrier).
6178 * May the current thread have a pessimistic view of the ring
6179 * status and forget to wake up queue, a racing rtl_tx thread
6183 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
6184 netif_wake_queue(dev);
6187 return NETDEV_TX_OK;
6190 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
6192 dev_kfree_skb_any(skb);
6193 dev->stats.tx_dropped++;
6194 return NETDEV_TX_OK;
6197 netif_stop_queue(dev);
6198 dev->stats.tx_dropped++;
6199 return NETDEV_TX_BUSY;
6202 static void rtl8169_pcierr_interrupt(struct net_device *dev)
6204 struct rtl8169_private *tp = netdev_priv(dev);
6205 struct pci_dev *pdev = tp->pci_dev;
6206 u16 pci_status, pci_cmd;
6208 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6209 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6211 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6212 pci_cmd, pci_status);
6215 * The recovery sequence below admits a very elaborated explanation:
6216 * - it seems to work;
6217 * - I did not see what else could be done;
6218 * - it makes iop3xx happy.
6220 * Feel free to adjust to your needs.
6222 if (pdev->broken_parity_status)
6223 pci_cmd &= ~PCI_COMMAND_PARITY;
6225 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6227 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
6229 pci_write_config_word(pdev, PCI_STATUS,
6230 pci_status & (PCI_STATUS_DETECTED_PARITY |
6231 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6232 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6234 /* The infamous DAC f*ckup only happens at boot time */
6235 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
6236 netif_info(tp, intr, dev, "disabling PCI DAC\n");
6237 tp->cp_cmd &= ~PCIDAC;
6238 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
6239 dev->features &= ~NETIF_F_HIGHDMA;
6242 rtl8169_hw_reset(tp);
6244 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6247 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
6249 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
6251 dirty_tx = tp->dirty_tx;
6253 tx_left = tp->cur_tx - dirty_tx;
6255 while (tx_left > 0) {
6256 unsigned int entry = dirty_tx % NUM_TX_DESC;
6257 struct ring_info *tx_skb = tp->tx_skb + entry;
6260 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6261 if (status & DescOwn)
6264 /* This barrier is needed to keep us from reading
6265 * any other fields out of the Tx descriptor until
6266 * we know the status of DescOwn
6270 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
6271 tp->TxDescArray + entry);
6272 if (status & LastFrag) {
6274 bytes_compl += tx_skb->skb->len;
6275 dev_consume_skb_any(tx_skb->skb);
6282 if (tp->dirty_tx != dirty_tx) {
6283 netdev_completed_queue(dev, pkts_compl, bytes_compl);
6285 u64_stats_update_begin(&tp->tx_stats.syncp);
6286 tp->tx_stats.packets += pkts_compl;
6287 tp->tx_stats.bytes += bytes_compl;
6288 u64_stats_update_end(&tp->tx_stats.syncp);
6290 tp->dirty_tx = dirty_tx;
6291 /* Sync with rtl8169_start_xmit:
6292 * - publish dirty_tx ring index (write barrier)
6293 * - refresh cur_tx ring index and queue status (read barrier)
6294 * May the current thread miss the stopped queue condition,
6295 * a racing xmit thread can only have a right view of the
6299 if (netif_queue_stopped(dev) &&
6300 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6301 netif_wake_queue(dev);
6304 * 8168 hack: TxPoll requests are lost when the Tx packets are
6305 * too close. Let's kick an extra TxPoll request when a burst
6306 * of start_xmit activity is detected (if it is not detected,
6307 * it is slow enough). -- FR
6309 if (tp->cur_tx != dirty_tx)
6310 RTL_W8(tp, TxPoll, NPQ);
6314 static inline int rtl8169_fragmented_frame(u32 status)
6316 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6319 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
6321 u32 status = opts1 & RxProtoMask;
6323 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
6324 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
6325 skb->ip_summed = CHECKSUM_UNNECESSARY;
6327 skb_checksum_none_assert(skb);
6330 static struct sk_buff *rtl8169_try_rx_copy(void *data,
6331 struct rtl8169_private *tp,
6335 struct sk_buff *skb;
6336 struct device *d = tp_to_dev(tp);
6338 data = rtl8169_align(data);
6339 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6341 skb = napi_alloc_skb(&tp->napi, pkt_size);
6343 skb_copy_to_linear_data(skb, data, pkt_size);
6344 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6349 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
6351 unsigned int cur_rx, rx_left;
6354 cur_rx = tp->cur_rx;
6356 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
6357 unsigned int entry = cur_rx % NUM_RX_DESC;
6358 struct RxDesc *desc = tp->RxDescArray + entry;
6361 status = le32_to_cpu(desc->opts1);
6362 if (status & DescOwn)
6365 /* This barrier is needed to keep us from reading
6366 * any other fields out of the Rx descriptor until
6367 * we know the status of DescOwn
6371 if (unlikely(status & RxRES)) {
6372 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6374 dev->stats.rx_errors++;
6375 if (status & (RxRWT | RxRUNT))
6376 dev->stats.rx_length_errors++;
6378 dev->stats.rx_crc_errors++;
6379 /* RxFOVF is a reserved bit on later chip versions */
6380 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6382 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6383 dev->stats.rx_fifo_errors++;
6384 } else if (status & (RxRUNT | RxCRC) &&
6385 !(status & RxRWT) &&
6386 dev->features & NETIF_F_RXALL) {
6390 struct sk_buff *skb;
6395 addr = le64_to_cpu(desc->addr);
6396 if (likely(!(dev->features & NETIF_F_RXFCS)))
6397 pkt_size = (status & 0x00003fff) - 4;
6399 pkt_size = status & 0x00003fff;
6402 * The driver does not support incoming fragmented
6403 * frames. They are seen as a symptom of over-mtu
6406 if (unlikely(rtl8169_fragmented_frame(status))) {
6407 dev->stats.rx_dropped++;
6408 dev->stats.rx_length_errors++;
6409 goto release_descriptor;
6412 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6413 tp, pkt_size, addr);
6415 dev->stats.rx_dropped++;
6416 goto release_descriptor;
6419 rtl8169_rx_csum(skb, status);
6420 skb_put(skb, pkt_size);
6421 skb->protocol = eth_type_trans(skb, dev);
6423 rtl8169_rx_vlan_tag(desc, skb);
6425 if (skb->pkt_type == PACKET_MULTICAST)
6426 dev->stats.multicast++;
6428 napi_gro_receive(&tp->napi, skb);
6430 u64_stats_update_begin(&tp->rx_stats.syncp);
6431 tp->rx_stats.packets++;
6432 tp->rx_stats.bytes += pkt_size;
6433 u64_stats_update_end(&tp->rx_stats.syncp);
6437 rtl8169_mark_to_asic(desc);
6440 count = cur_rx - tp->cur_rx;
6441 tp->cur_rx = cur_rx;
6446 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6448 struct rtl8169_private *tp = dev_instance;
6449 u16 status = rtl_get_events(tp);
6451 if (status == 0xffff || !(status & tp->irq_mask))
6454 if (unlikely(status & SYSErr)) {
6455 rtl8169_pcierr_interrupt(tp->dev);
6459 if (status & LinkChg)
6460 phy_mac_interrupt(tp->dev->phydev);
6462 if (unlikely(status & RxFIFOOver &&
6463 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
6464 netif_stop_queue(tp->dev);
6465 /* XXX - Hack alert. See rtl_task(). */
6466 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6469 if (status & RTL_EVENT_NAPI) {
6470 rtl_irq_disable(tp);
6471 napi_schedule_irqoff(&tp->napi);
6474 rtl_ack_events(tp, status);
6479 static void rtl_task(struct work_struct *work)
6481 static const struct {
6483 void (*action)(struct rtl8169_private *);
6485 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6487 struct rtl8169_private *tp =
6488 container_of(work, struct rtl8169_private, wk.work);
6489 struct net_device *dev = tp->dev;
6494 if (!netif_running(dev) ||
6495 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6498 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6501 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6503 rtl_work[i].action(tp);
6507 rtl_unlock_work(tp);
6510 static int rtl8169_poll(struct napi_struct *napi, int budget)
6512 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6513 struct net_device *dev = tp->dev;
6516 work_done = rtl_rx(dev, tp, (u32) budget);
6520 if (work_done < budget) {
6521 napi_complete_done(napi, work_done);
6530 static void rtl8169_rx_missed(struct net_device *dev)
6532 struct rtl8169_private *tp = netdev_priv(dev);
6534 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6537 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6538 RTL_W32(tp, RxMissed, 0);
6541 static void r8169_phylink_handler(struct net_device *ndev)
6543 struct rtl8169_private *tp = netdev_priv(ndev);
6545 if (netif_carrier_ok(ndev)) {
6546 rtl_link_chg_patch(tp);
6547 pm_request_resume(&tp->pci_dev->dev);
6549 pm_runtime_idle(&tp->pci_dev->dev);
6552 if (net_ratelimit())
6553 phy_print_status(ndev->phydev);
6556 static int r8169_phy_connect(struct rtl8169_private *tp)
6558 struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6559 phy_interface_t phy_mode;
6562 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
6563 PHY_INTERFACE_MODE_MII;
6565 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6570 if (!tp->supports_gmii)
6571 phy_set_max_speed(phydev, SPEED_100);
6573 /* Ensure to advertise everything, incl. pause */
6574 linkmode_copy(phydev->advertising, phydev->supported);
6576 phy_attached_info(phydev);
6581 static void rtl8169_down(struct net_device *dev)
6583 struct rtl8169_private *tp = netdev_priv(dev);
6585 phy_stop(dev->phydev);
6587 napi_disable(&tp->napi);
6588 netif_stop_queue(dev);
6590 rtl8169_hw_reset(tp);
6592 * At this point device interrupts can not be enabled in any function,
6593 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6594 * and napi is disabled (rtl8169_poll).
6596 rtl8169_rx_missed(dev);
6598 /* Give a racing hard_start_xmit a few cycles to complete. */
6599 synchronize_sched();
6601 rtl8169_tx_clear(tp);
6603 rtl8169_rx_clear(tp);
6605 rtl_pll_power_down(tp);
6608 static int rtl8169_close(struct net_device *dev)
6610 struct rtl8169_private *tp = netdev_priv(dev);
6611 struct pci_dev *pdev = tp->pci_dev;
6613 pm_runtime_get_sync(&pdev->dev);
6615 /* Update counters before going down */
6616 rtl8169_update_counters(tp);
6619 /* Clear all task flags */
6620 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6623 rtl_unlock_work(tp);
6625 cancel_work_sync(&tp->wk.work);
6627 phy_disconnect(dev->phydev);
6629 pci_free_irq(pdev, 0, tp);
6631 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6633 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6635 tp->TxDescArray = NULL;
6636 tp->RxDescArray = NULL;
6638 pm_runtime_put_sync(&pdev->dev);
6643 #ifdef CONFIG_NET_POLL_CONTROLLER
6644 static void rtl8169_netpoll(struct net_device *dev)
6646 struct rtl8169_private *tp = netdev_priv(dev);
6648 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
6652 static int rtl_open(struct net_device *dev)
6654 struct rtl8169_private *tp = netdev_priv(dev);
6655 struct pci_dev *pdev = tp->pci_dev;
6656 int retval = -ENOMEM;
6658 pm_runtime_get_sync(&pdev->dev);
6661 * Rx and Tx descriptors needs 256 bytes alignment.
6662 * dma_alloc_coherent provides more.
6664 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6665 &tp->TxPhyAddr, GFP_KERNEL);
6666 if (!tp->TxDescArray)
6667 goto err_pm_runtime_put;
6669 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6670 &tp->RxPhyAddr, GFP_KERNEL);
6671 if (!tp->RxDescArray)
6674 retval = rtl8169_init_ring(tp);
6678 INIT_WORK(&tp->wk.work, rtl_task);
6682 rtl_request_firmware(tp);
6684 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
6687 goto err_release_fw_2;
6689 retval = r8169_phy_connect(tp);
6695 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6697 napi_enable(&tp->napi);
6699 rtl8169_init_phy(dev, tp);
6701 rtl_pll_power_up(tp);
6705 if (!rtl8169_init_counter_offsets(tp))
6706 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6708 phy_start(dev->phydev);
6709 netif_start_queue(dev);
6711 rtl_unlock_work(tp);
6713 pm_runtime_put_sync(&pdev->dev);
6718 pci_free_irq(pdev, 0, tp);
6720 rtl_release_firmware(tp);
6721 rtl8169_rx_clear(tp);
6723 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6725 tp->RxDescArray = NULL;
6727 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6729 tp->TxDescArray = NULL;
6731 pm_runtime_put_noidle(&pdev->dev);
6736 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6738 struct rtl8169_private *tp = netdev_priv(dev);
6739 struct pci_dev *pdev = tp->pci_dev;
6740 struct rtl8169_counters *counters = tp->counters;
6743 pm_runtime_get_noresume(&pdev->dev);
6745 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
6746 rtl8169_rx_missed(dev);
6749 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
6750 stats->rx_packets = tp->rx_stats.packets;
6751 stats->rx_bytes = tp->rx_stats.bytes;
6752 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
6755 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
6756 stats->tx_packets = tp->tx_stats.packets;
6757 stats->tx_bytes = tp->tx_stats.bytes;
6758 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
6760 stats->rx_dropped = dev->stats.rx_dropped;
6761 stats->tx_dropped = dev->stats.tx_dropped;
6762 stats->rx_length_errors = dev->stats.rx_length_errors;
6763 stats->rx_errors = dev->stats.rx_errors;
6764 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6765 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6766 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6767 stats->multicast = dev->stats.multicast;
6770 * Fetch additonal counter values missing in stats collected by driver
6771 * from tally counters.
6773 if (pm_runtime_active(&pdev->dev))
6774 rtl8169_update_counters(tp);
6777 * Subtract values fetched during initalization.
6778 * See rtl8169_init_counter_offsets for a description why we do that.
6780 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
6781 le64_to_cpu(tp->tc_offset.tx_errors);
6782 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
6783 le32_to_cpu(tp->tc_offset.tx_multi_collision);
6784 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
6785 le16_to_cpu(tp->tc_offset.tx_aborted);
6787 pm_runtime_put_noidle(&pdev->dev);
6790 static void rtl8169_net_suspend(struct net_device *dev)
6792 struct rtl8169_private *tp = netdev_priv(dev);
6794 if (!netif_running(dev))
6797 phy_stop(dev->phydev);
6798 netif_device_detach(dev);
6801 napi_disable(&tp->napi);
6802 /* Clear all task flags */
6803 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6805 rtl_unlock_work(tp);
6807 rtl_pll_power_down(tp);
6812 static int rtl8169_suspend(struct device *device)
6814 struct net_device *dev = dev_get_drvdata(device);
6815 struct rtl8169_private *tp = netdev_priv(dev);
6817 rtl8169_net_suspend(dev);
6818 clk_disable_unprepare(tp->clk);
6823 static void __rtl8169_resume(struct net_device *dev)
6825 struct rtl8169_private *tp = netdev_priv(dev);
6827 netif_device_attach(dev);
6829 rtl_pll_power_up(tp);
6830 rtl8169_init_phy(dev, tp);
6832 phy_start(tp->dev->phydev);
6835 napi_enable(&tp->napi);
6836 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6837 rtl_unlock_work(tp);
6839 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6842 static int rtl8169_resume(struct device *device)
6844 struct net_device *dev = dev_get_drvdata(device);
6845 struct rtl8169_private *tp = netdev_priv(dev);
6847 clk_prepare_enable(tp->clk);
6849 if (netif_running(dev))
6850 __rtl8169_resume(dev);
6855 static int rtl8169_runtime_suspend(struct device *device)
6857 struct net_device *dev = dev_get_drvdata(device);
6858 struct rtl8169_private *tp = netdev_priv(dev);
6860 if (!tp->TxDescArray)
6864 __rtl8169_set_wol(tp, WAKE_ANY);
6865 rtl_unlock_work(tp);
6867 rtl8169_net_suspend(dev);
6869 /* Update counters before going runtime suspend */
6870 rtl8169_rx_missed(dev);
6871 rtl8169_update_counters(tp);
6876 static int rtl8169_runtime_resume(struct device *device)
6878 struct net_device *dev = dev_get_drvdata(device);
6879 struct rtl8169_private *tp = netdev_priv(dev);
6880 rtl_rar_set(tp, dev->dev_addr);
6882 if (!tp->TxDescArray)
6886 __rtl8169_set_wol(tp, tp->saved_wolopts);
6887 rtl_unlock_work(tp);
6889 __rtl8169_resume(dev);
6894 static int rtl8169_runtime_idle(struct device *device)
6896 struct net_device *dev = dev_get_drvdata(device);
6898 if (!netif_running(dev) || !netif_carrier_ok(dev))
6899 pm_schedule_suspend(device, 10000);
6904 static const struct dev_pm_ops rtl8169_pm_ops = {
6905 .suspend = rtl8169_suspend,
6906 .resume = rtl8169_resume,
6907 .freeze = rtl8169_suspend,
6908 .thaw = rtl8169_resume,
6909 .poweroff = rtl8169_suspend,
6910 .restore = rtl8169_resume,
6911 .runtime_suspend = rtl8169_runtime_suspend,
6912 .runtime_resume = rtl8169_runtime_resume,
6913 .runtime_idle = rtl8169_runtime_idle,
6916 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6918 #else /* !CONFIG_PM */
6920 #define RTL8169_PM_OPS NULL
6922 #endif /* !CONFIG_PM */
6924 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6926 /* WoL fails with 8168b when the receiver is disabled. */
6927 switch (tp->mac_version) {
6928 case RTL_GIGA_MAC_VER_11:
6929 case RTL_GIGA_MAC_VER_12:
6930 case RTL_GIGA_MAC_VER_17:
6931 pci_clear_master(tp->pci_dev);
6933 RTL_W8(tp, ChipCmd, CmdRxEnb);
6935 RTL_R8(tp, ChipCmd);
6942 static void rtl_shutdown(struct pci_dev *pdev)
6944 struct net_device *dev = pci_get_drvdata(pdev);
6945 struct rtl8169_private *tp = netdev_priv(dev);
6947 rtl8169_net_suspend(dev);
6949 /* Restore original MAC address */
6950 rtl_rar_set(tp, dev->perm_addr);
6952 rtl8169_hw_reset(tp);
6954 if (system_state == SYSTEM_POWER_OFF) {
6955 if (tp->saved_wolopts) {
6956 rtl_wol_suspend_quirk(tp);
6957 rtl_wol_shutdown_quirk(tp);
6960 pci_wake_from_d3(pdev, true);
6961 pci_set_power_state(pdev, PCI_D3hot);
6965 static void rtl_remove_one(struct pci_dev *pdev)
6967 struct net_device *dev = pci_get_drvdata(pdev);
6968 struct rtl8169_private *tp = netdev_priv(dev);
6970 if (r8168_check_dash(tp))
6971 rtl8168_driver_stop(tp);
6973 netif_napi_del(&tp->napi);
6975 unregister_netdev(dev);
6976 mdiobus_unregister(tp->mii_bus);
6978 rtl_release_firmware(tp);
6980 if (pci_dev_run_wake(pdev))
6981 pm_runtime_get_noresume(&pdev->dev);
6983 /* restore original MAC address */
6984 rtl_rar_set(tp, dev->perm_addr);
6987 static const struct net_device_ops rtl_netdev_ops = {
6988 .ndo_open = rtl_open,
6989 .ndo_stop = rtl8169_close,
6990 .ndo_get_stats64 = rtl8169_get_stats64,
6991 .ndo_start_xmit = rtl8169_start_xmit,
6992 .ndo_tx_timeout = rtl8169_tx_timeout,
6993 .ndo_validate_addr = eth_validate_addr,
6994 .ndo_change_mtu = rtl8169_change_mtu,
6995 .ndo_fix_features = rtl8169_fix_features,
6996 .ndo_set_features = rtl8169_set_features,
6997 .ndo_set_mac_address = rtl_set_mac_address,
6998 .ndo_do_ioctl = rtl8169_ioctl,
6999 .ndo_set_rx_mode = rtl_set_rx_mode,
7000 #ifdef CONFIG_NET_POLL_CONTROLLER
7001 .ndo_poll_controller = rtl8169_netpoll,
7006 static const struct rtl_cfg_info {
7007 void (*hw_start)(struct rtl8169_private *tp);
7009 unsigned int has_gmii:1;
7010 const struct rtl_coalesce_info *coalesce_info;
7012 } rtl_cfg_infos [] = {
7014 .hw_start = rtl_hw_start_8169,
7015 .irq_mask = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7017 .coalesce_info = rtl_coalesce_info_8169,
7018 .default_ver = RTL_GIGA_MAC_VER_01,
7021 .hw_start = rtl_hw_start_8168,
7022 .irq_mask = LinkChg | RxOverflow,
7024 .coalesce_info = rtl_coalesce_info_8168_8136,
7025 .default_ver = RTL_GIGA_MAC_VER_11,
7028 .hw_start = rtl_hw_start_8101,
7029 .irq_mask = LinkChg | RxOverflow | RxFIFOOver,
7030 .coalesce_info = rtl_coalesce_info_8168_8136,
7031 .default_ver = RTL_GIGA_MAC_VER_13,
7035 static int rtl_alloc_irq(struct rtl8169_private *tp)
7039 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
7040 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7041 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7042 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
7043 flags = PCI_IRQ_LEGACY;
7045 flags = PCI_IRQ_ALL_TYPES;
7048 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
7051 DECLARE_RTL_COND(rtl_link_list_ready_cond)
7053 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
7056 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7058 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7061 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7063 struct rtl8169_private *tp = mii_bus->priv;
7068 return rtl_readphy(tp, phyreg);
7071 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7072 int phyreg, u16 val)
7074 struct rtl8169_private *tp = mii_bus->priv;
7079 rtl_writephy(tp, phyreg, val);
7084 static int r8169_mdio_register(struct rtl8169_private *tp)
7086 struct pci_dev *pdev = tp->pci_dev;
7087 struct phy_device *phydev;
7088 struct mii_bus *new_bus;
7091 new_bus = devm_mdiobus_alloc(&pdev->dev);
7095 new_bus->name = "r8169";
7097 new_bus->parent = &pdev->dev;
7098 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7099 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7100 PCI_DEVID(pdev->bus->number, pdev->devfn));
7102 new_bus->read = r8169_mdio_read_reg;
7103 new_bus->write = r8169_mdio_write_reg;
7105 ret = mdiobus_register(new_bus);
7109 phydev = mdiobus_get_phy(new_bus, 0);
7111 mdiobus_unregister(new_bus);
7115 /* PHY will be woken up in rtl_open() */
7116 phy_suspend(phydev);
7118 tp->mii_bus = new_bus;
7123 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
7127 tp->ocp_base = OCP_STD_PHY_BASE;
7129 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
7131 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7134 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7137 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7139 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
7141 data = r8168_mac_ocp_read(tp, 0xe8de);
7143 r8168_mac_ocp_write(tp, 0xe8de, data);
7145 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7148 data = r8168_mac_ocp_read(tp, 0xe8de);
7150 r8168_mac_ocp_write(tp, 0xe8de, data);
7152 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7156 static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7158 rtl8168ep_stop_cmac(tp);
7159 rtl_hw_init_8168g(tp);
7162 static void rtl_hw_initialize(struct rtl8169_private *tp)
7164 switch (tp->mac_version) {
7165 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
7166 rtl_hw_init_8168g(tp);
7168 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
7169 rtl_hw_init_8168ep(tp);
7176 /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
7177 static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
7179 switch (tp->mac_version) {
7180 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7181 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
7188 static int rtl_jumbo_max(struct rtl8169_private *tp)
7190 /* Non-GBit versions don't support jumbo frames */
7191 if (!tp->supports_gmii)
7194 switch (tp->mac_version) {
7196 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7199 case RTL_GIGA_MAC_VER_11:
7200 case RTL_GIGA_MAC_VER_12:
7201 case RTL_GIGA_MAC_VER_17:
7204 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
7211 static void rtl_disable_clk(void *data)
7213 clk_disable_unprepare(data);
7216 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7218 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7219 struct rtl8169_private *tp;
7220 struct net_device *dev;
7221 int chipset, region, i;
7224 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7228 SET_NETDEV_DEV(dev, &pdev->dev);
7229 dev->netdev_ops = &rtl_netdev_ops;
7230 tp = netdev_priv(dev);
7233 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7234 tp->supports_gmii = cfg->has_gmii;
7236 /* Get the *optional* external "ether_clk" used on some boards */
7237 tp->clk = devm_clk_get(&pdev->dev, "ether_clk");
7238 if (IS_ERR(tp->clk)) {
7239 rc = PTR_ERR(tp->clk);
7240 if (rc == -ENOENT) {
7241 /* clk-core allows NULL (for suspend / resume) */
7243 } else if (rc == -EPROBE_DEFER) {
7246 dev_err(&pdev->dev, "failed to get clk: %d\n", rc);
7250 rc = clk_prepare_enable(tp->clk);
7252 dev_err(&pdev->dev, "failed to enable clk: %d\n", rc);
7256 rc = devm_add_action_or_reset(&pdev->dev, rtl_disable_clk,
7262 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7263 rc = pcim_enable_device(pdev);
7265 dev_err(&pdev->dev, "enable failure\n");
7269 if (pcim_set_mwi(pdev) < 0)
7270 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
7272 /* use first MMIO region */
7273 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7275 dev_err(&pdev->dev, "no MMIO resource found\n");
7279 /* check for weird/broken PCI region reporting */
7280 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7281 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
7285 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
7287 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
7291 tp->mmio_addr = pcim_iomap_table(pdev)[region];
7293 /* Identify chip attached to board */
7294 rtl8169_get_mac_version(tp, cfg->default_ver);
7296 if (rtl_tbi_enabled(tp)) {
7297 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7301 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
7303 if (sizeof(dma_addr_t) > 4 && (use_dac == 1 || (use_dac == -1 &&
7304 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
7305 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
7307 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7308 if (!pci_is_pcie(pdev))
7309 tp->cp_cmd |= PCIDAC;
7310 dev->features |= NETIF_F_HIGHDMA;
7312 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7314 dev_err(&pdev->dev, "DMA configuration failed\n");
7321 rtl8169_irq_mask_and_ack(tp);
7323 rtl_hw_initialize(tp);
7327 pci_set_master(pdev);
7329 rtl_init_mdio_ops(tp);
7330 rtl_init_jumbo_ops(tp);
7332 chipset = tp->mac_version;
7334 rc = rtl_alloc_irq(tp);
7336 dev_err(&pdev->dev, "Can't allocate interrupt\n");
7340 tp->saved_wolopts = __rtl8169_get_wol(tp);
7342 mutex_init(&tp->wk.mutex);
7343 u64_stats_init(&tp->rx_stats.syncp);
7344 u64_stats_init(&tp->tx_stats.syncp);
7346 /* Get MAC address */
7347 switch (tp->mac_version) {
7348 u8 mac_addr[ETH_ALEN] __aligned(4);
7349 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7350 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
7351 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
7352 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
7354 if (is_valid_ether_addr(mac_addr))
7355 rtl_rar_set(tp, mac_addr);
7360 for (i = 0; i < ETH_ALEN; i++)
7361 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
7363 dev->ethtool_ops = &rtl8169_ethtool_ops;
7365 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
7367 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7368 * properly for all devices */
7369 dev->features |= NETIF_F_RXCSUM |
7370 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
7372 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7373 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7374 NETIF_F_HW_VLAN_CTAG_RX;
7375 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7377 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
7379 tp->cp_cmd |= RxChkSum | RxVlan;
7382 * Pretend we are using VLANs; This bypasses a nasty bug where
7383 * Interrupts stop flowing on high load on 8110SCd controllers.
7385 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7386 /* Disallow toggling */
7387 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
7389 if (rtl_chip_supports_csum_v2(tp)) {
7390 tp->tso_csum = rtl8169_tso_csum_v2;
7391 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7393 tp->tso_csum = rtl8169_tso_csum_v1;
7396 dev->hw_features |= NETIF_F_RXALL;
7397 dev->hw_features |= NETIF_F_RXFCS;
7399 /* MTU range: 60 - hw-specific max */
7400 dev->min_mtu = ETH_ZLEN;
7401 jumbo_max = rtl_jumbo_max(tp);
7402 dev->max_mtu = jumbo_max;
7404 tp->hw_start = cfg->hw_start;
7405 tp->irq_mask = RTL_EVENT_NAPI | cfg->irq_mask;
7406 tp->coalesce_info = cfg->coalesce_info;
7408 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7410 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7411 &tp->counters_phys_addr,
7416 pci_set_drvdata(pdev, dev);
7418 rc = r8169_mdio_register(tp);
7422 /* chip gets powered up in rtl_open() */
7423 rtl_pll_power_down(tp);
7425 rc = register_netdev(dev);
7427 goto err_mdio_unregister;
7429 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7430 rtl_chip_infos[chipset].name, dev->dev_addr,
7431 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
7432 pci_irq_vector(pdev, 0));
7434 if (jumbo_max > JUMBO_1K)
7435 netif_info(tp, probe, dev,
7436 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7437 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
7440 if (r8168_check_dash(tp))
7441 rtl8168_driver_start(tp);
7443 if (pci_dev_run_wake(pdev))
7444 pm_runtime_put_sync(&pdev->dev);
7448 err_mdio_unregister:
7449 mdiobus_unregister(tp->mii_bus);
7453 static struct pci_driver rtl8169_pci_driver = {
7455 .id_table = rtl8169_pci_tbl,
7456 .probe = rtl_init_one,
7457 .remove = rtl_remove_one,
7458 .shutdown = rtl_shutdown,
7459 .driver.pm = RTL8169_PM_OPS,
7462 module_pci_driver(rtl8169_pci_driver);