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/pci-aspm.h>
32 #include <linux/ipv6.h>
33 #include <net/ip6_checksum.h>
35 #define MODULENAME "r8169"
37 #define FIRMWARE_8168D_1 "/*(DEBLOBBED)*/"
38 #define FIRMWARE_8168D_2 "/*(DEBLOBBED)*/"
39 #define FIRMWARE_8168E_1 "/*(DEBLOBBED)*/"
40 #define FIRMWARE_8168E_2 "/*(DEBLOBBED)*/"
41 #define FIRMWARE_8168E_3 "/*(DEBLOBBED)*/"
42 #define FIRMWARE_8168F_1 "/*(DEBLOBBED)*/"
43 #define FIRMWARE_8168F_2 "/*(DEBLOBBED)*/"
44 #define FIRMWARE_8105E_1 "/*(DEBLOBBED)*/"
45 #define FIRMWARE_8402_1 "/*(DEBLOBBED)*/"
46 #define FIRMWARE_8411_1 "/*(DEBLOBBED)*/"
47 #define FIRMWARE_8411_2 "/*(DEBLOBBED)*/"
48 #define FIRMWARE_8106E_1 "/*(DEBLOBBED)*/"
49 #define FIRMWARE_8106E_2 "/*(DEBLOBBED)*/"
50 #define FIRMWARE_8168G_2 "/*(DEBLOBBED)*/"
51 #define FIRMWARE_8168G_3 "/*(DEBLOBBED)*/"
52 #define FIRMWARE_8168H_1 "/*(DEBLOBBED)*/"
53 #define FIRMWARE_8168H_2 "/*(DEBLOBBED)*/"
54 #define FIRMWARE_8107E_1 "/*(DEBLOBBED)*/"
55 #define FIRMWARE_8107E_2 "/*(DEBLOBBED)*/"
57 #define R8169_MSG_DEFAULT \
58 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
60 #define TX_SLOTS_AVAIL(tp) \
61 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
63 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
64 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
65 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
67 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
68 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
69 static const int multicast_filter_limit = 32;
71 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
72 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
74 #define R8169_REGS_SIZE 256
75 #define R8169_RX_BUF_SIZE (SZ_16K - 1)
76 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
77 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
78 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
79 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
81 #define RTL8169_TX_TIMEOUT (6*HZ)
83 /* write/read MMIO register */
84 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
85 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
86 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
87 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
88 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
89 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
92 RTL_GIGA_MAC_VER_01 = 0,
143 RTL_GIGA_MAC_NONE = 0xff,
146 #define JUMBO_1K ETH_DATA_LEN
147 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
148 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
149 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
150 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
152 static const struct {
155 } rtl_chip_infos[] = {
157 [RTL_GIGA_MAC_VER_01] = {"RTL8169" },
158 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
159 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
160 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
161 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
162 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
164 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
165 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
166 [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
167 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
168 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
169 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
170 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
171 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
172 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
173 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
174 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
175 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
176 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
177 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
178 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
179 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
180 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
181 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
182 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
183 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
184 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
185 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
186 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
187 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
188 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
189 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
190 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
191 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
192 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
193 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
194 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
195 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
196 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
197 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
198 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
199 [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
200 [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
201 [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
202 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
203 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
204 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
205 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
206 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
207 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
208 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
217 static const struct pci_device_id rtl8169_pci_tbl[] = {
218 { PCI_VDEVICE(REALTEK, 0x2502), RTL_CFG_1 },
219 { PCI_VDEVICE(REALTEK, 0x2600), RTL_CFG_1 },
220 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
221 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
222 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
223 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
224 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
225 { PCI_DEVICE(PCI_VENDOR_ID_NCUBE, 0x8168), 0, 0, RTL_CFG_1 },
226 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
227 { PCI_VENDOR_ID_DLINK, 0x4300,
228 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
229 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
230 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
231 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
232 { PCI_DEVICE(PCI_VENDOR_ID_USR, 0x0116), 0, 0, RTL_CFG_0 },
233 { PCI_VENDOR_ID_LINKSYS, 0x1032,
234 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
236 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
240 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
242 static int use_dac = -1;
248 MAC0 = 0, /* Ethernet hardware address. */
250 MAR0 = 8, /* Multicast filter. */
251 CounterAddrLow = 0x10,
252 CounterAddrHigh = 0x14,
253 TxDescStartAddrLow = 0x20,
254 TxDescStartAddrHigh = 0x24,
255 TxHDescStartAddrLow = 0x28,
256 TxHDescStartAddrHigh = 0x2c,
265 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
266 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
269 #define RX128_INT_EN (1 << 15) /* 8111c and later */
270 #define RX_MULTI_EN (1 << 14) /* 8111c only */
271 #define RXCFG_FIFO_SHIFT 13
272 /* No threshold before first PCI xfer */
273 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
274 #define RX_EARLY_OFF (1 << 11)
275 #define RXCFG_DMA_SHIFT 8
276 /* Unlimited maximum PCI burst. */
277 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
284 #define PME_SIGNAL (1 << 5) /* 8168c and later */
296 #define RTL_COALESCE_MASK 0x0f
297 #define RTL_COALESCE_SHIFT 4
298 #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
299 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
301 RxDescAddrLow = 0xe4,
302 RxDescAddrHigh = 0xe8,
303 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
305 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
307 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
309 #define TxPacketMax (8064 >> 7)
310 #define EarlySize 0x27
313 FuncEventMask = 0xf4,
314 FuncPresetState = 0xf8,
319 FuncForceEvent = 0xfc,
322 enum rtl8168_8101_registers {
325 #define CSIAR_FLAG 0x80000000
326 #define CSIAR_WRITE_CMD 0x80000000
327 #define CSIAR_BYTE_ENABLE 0x0000f000
328 #define CSIAR_ADDR_MASK 0x00000fff
331 #define EPHYAR_FLAG 0x80000000
332 #define EPHYAR_WRITE_CMD 0x80000000
333 #define EPHYAR_REG_MASK 0x1f
334 #define EPHYAR_REG_SHIFT 16
335 #define EPHYAR_DATA_MASK 0xffff
337 #define PFM_EN (1 << 6)
338 #define TX_10M_PS_EN (1 << 7)
340 #define FIX_NAK_1 (1 << 4)
341 #define FIX_NAK_2 (1 << 3)
344 #define NOW_IS_OOB (1 << 7)
345 #define TX_EMPTY (1 << 5)
346 #define RX_EMPTY (1 << 4)
347 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
348 #define EN_NDP (1 << 3)
349 #define EN_OOB_RESET (1 << 2)
350 #define LINK_LIST_RDY (1 << 1)
352 #define EFUSEAR_FLAG 0x80000000
353 #define EFUSEAR_WRITE_CMD 0x80000000
354 #define EFUSEAR_READ_CMD 0x00000000
355 #define EFUSEAR_REG_MASK 0x03ff
356 #define EFUSEAR_REG_SHIFT 8
357 #define EFUSEAR_DATA_MASK 0xff
359 #define PFM_D3COLD_EN (1 << 6)
362 enum rtl8168_registers {
367 #define ERIAR_FLAG 0x80000000
368 #define ERIAR_WRITE_CMD 0x80000000
369 #define ERIAR_READ_CMD 0x00000000
370 #define ERIAR_ADDR_BYTE_ALIGN 4
371 #define ERIAR_TYPE_SHIFT 16
372 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
373 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
374 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
375 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
376 #define ERIAR_MASK_SHIFT 12
377 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
378 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
379 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
380 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
381 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
382 EPHY_RXER_NUM = 0x7c,
383 OCPDR = 0xb0, /* OCP GPHY access */
384 #define OCPDR_WRITE_CMD 0x80000000
385 #define OCPDR_READ_CMD 0x00000000
386 #define OCPDR_REG_MASK 0x7f
387 #define OCPDR_GPHY_REG_SHIFT 16
388 #define OCPDR_DATA_MASK 0xffff
390 #define OCPAR_FLAG 0x80000000
391 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
392 #define OCPAR_GPHY_READ_CMD 0x0000f060
394 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
395 MISC = 0xf0, /* 8168e only. */
396 #define TXPLA_RST (1 << 29)
397 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
398 #define PWM_EN (1 << 22)
399 #define RXDV_GATED_EN (1 << 19)
400 #define EARLY_TALLY_EN (1 << 16)
403 enum rtl_register_content {
404 /* InterruptStatusBits */
408 TxDescUnavail = 0x0080,
432 /* TXPoll register p.5 */
433 HPQ = 0x80, /* Poll cmd on the high prio queue */
434 NPQ = 0x40, /* Poll cmd on the low prio queue */
435 FSWInt = 0x01, /* Forced software interrupt */
439 Cfg9346_Unlock = 0xc0,
444 AcceptBroadcast = 0x08,
445 AcceptMulticast = 0x04,
447 AcceptAllPhys = 0x01,
448 #define RX_CONFIG_ACCEPT_MASK 0x3f
451 TxInterFrameGapShift = 24,
452 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
454 /* Config1 register p.24 */
457 Speed_down = (1 << 4),
461 PMEnable = (1 << 0), /* Power Management Enable */
463 /* Config2 register p. 25 */
464 ClkReqEn = (1 << 7), /* Clock Request Enable */
465 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
466 PCI_Clock_66MHz = 0x01,
467 PCI_Clock_33MHz = 0x00,
469 /* Config3 register p.25 */
470 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
471 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
472 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
473 Rdy_to_L23 = (1 << 1), /* L23 Enable */
474 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
476 /* Config4 register */
477 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
479 /* Config5 register p.27 */
480 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
481 MWF = (1 << 5), /* Accept Multicast wakeup frame */
482 UWF = (1 << 4), /* Accept Unicast wakeup frame */
484 LanWake = (1 << 1), /* LanWake enable/disable */
485 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
486 ASPM_en = (1 << 0), /* ASPM enable */
489 EnableBist = (1 << 15), // 8168 8101
490 Mac_dbgo_oe = (1 << 14), // 8168 8101
491 Normal_mode = (1 << 13), // unused
492 Force_half_dup = (1 << 12), // 8168 8101
493 Force_rxflow_en = (1 << 11), // 8168 8101
494 Force_txflow_en = (1 << 10), // 8168 8101
495 Cxpl_dbg_sel = (1 << 9), // 8168 8101
496 ASF = (1 << 8), // 8168 8101
497 PktCntrDisable = (1 << 7), // 8168 8101
498 Mac_dbgo_sel = 0x001c, // 8168
503 #define INTT_MASK GENMASK(1, 0)
504 INTT_0 = 0x0000, // 8168
505 INTT_1 = 0x0001, // 8168
506 INTT_2 = 0x0002, // 8168
507 INTT_3 = 0x0003, // 8168
509 /* rtl8169_PHYstatus */
520 TBILinkOK = 0x02000000,
522 /* ResetCounterCommand */
525 /* DumpCounterCommand */
528 /* magic enable v2 */
529 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
533 /* First doubleword. */
534 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
535 RingEnd = (1 << 30), /* End of descriptor ring */
536 FirstFrag = (1 << 29), /* First segment of a packet */
537 LastFrag = (1 << 28), /* Final segment of a packet */
541 enum rtl_tx_desc_bit {
542 /* First doubleword. */
543 TD_LSO = (1 << 27), /* Large Send Offload */
544 #define TD_MSS_MAX 0x07ffu /* MSS value */
546 /* Second doubleword. */
547 TxVlanTag = (1 << 17), /* Add VLAN tag */
550 /* 8169, 8168b and 810x except 8102e. */
551 enum rtl_tx_desc_bit_0 {
552 /* First doubleword. */
553 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
554 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
555 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
556 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
559 /* 8102e, 8168c and beyond. */
560 enum rtl_tx_desc_bit_1 {
561 /* First doubleword. */
562 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
563 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
564 #define GTTCPHO_SHIFT 18
565 #define GTTCPHO_MAX 0x7fU
567 /* Second doubleword. */
568 #define TCPHO_SHIFT 18
569 #define TCPHO_MAX 0x3ffU
570 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
571 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
572 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
573 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
574 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
577 enum rtl_rx_desc_bit {
579 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
580 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
582 #define RxProtoUDP (PID1)
583 #define RxProtoTCP (PID0)
584 #define RxProtoIP (PID1 | PID0)
585 #define RxProtoMask RxProtoIP
587 IPFail = (1 << 16), /* IP checksum failed */
588 UDPFail = (1 << 15), /* UDP/IP checksum failed */
589 TCPFail = (1 << 14), /* TCP/IP checksum failed */
590 RxVlanTag = (1 << 16), /* VLAN tag available */
593 #define RsvdMask 0x3fffc000
594 #define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
611 u8 __pad[sizeof(void *) - sizeof(u32)];
614 struct rtl8169_counters {
621 __le32 tx_one_collision;
622 __le32 tx_multi_collision;
630 struct rtl8169_tc_offsets {
633 __le32 tx_multi_collision;
638 RTL_FLAG_TASK_ENABLED = 0,
639 RTL_FLAG_TASK_SLOW_PENDING,
640 RTL_FLAG_TASK_RESET_PENDING,
644 struct rtl8169_stats {
647 struct u64_stats_sync syncp;
650 struct rtl8169_private {
651 void __iomem *mmio_addr; /* memory map physical address */
652 struct pci_dev *pci_dev;
653 struct net_device *dev;
654 struct napi_struct napi;
657 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
658 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
660 struct rtl8169_stats rx_stats;
661 struct rtl8169_stats tx_stats;
662 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
663 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
664 dma_addr_t TxPhyAddr;
665 dma_addr_t RxPhyAddr;
666 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
667 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
671 const struct rtl_coalesce_info *coalesce_info;
675 void (*write)(struct rtl8169_private *, int, int);
676 int (*read)(struct rtl8169_private *, int);
680 void (*enable)(struct rtl8169_private *);
681 void (*disable)(struct rtl8169_private *);
684 void (*hw_start)(struct rtl8169_private *tp);
685 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
688 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
690 struct work_struct work;
693 unsigned supports_gmii:1;
694 struct mii_bus *mii_bus;
695 dma_addr_t counters_phys_addr;
696 struct rtl8169_counters *counters;
697 struct rtl8169_tc_offsets tc_offset;
701 const struct firmware *fw;
703 #define RTL_VER_SIZE 32
705 char version[RTL_VER_SIZE];
707 struct rtl_fw_phy_action {
712 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
717 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
718 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
719 module_param(use_dac, int, 0);
720 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
721 module_param_named(debug, debug.msg_enable, int, 0);
722 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
723 MODULE_SOFTDEP("pre: realtek");
724 MODULE_LICENSE("GPL");
727 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
729 return &tp->pci_dev->dev;
732 static void rtl_lock_work(struct rtl8169_private *tp)
734 mutex_lock(&tp->wk.mutex);
737 static void rtl_unlock_work(struct rtl8169_private *tp)
739 mutex_unlock(&tp->wk.mutex);
742 static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
744 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
745 PCI_EXP_DEVCTL_READRQ, force);
749 bool (*check)(struct rtl8169_private *);
753 static void rtl_udelay(unsigned int d)
758 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
759 void (*delay)(unsigned int), unsigned int d, int n,
764 for (i = 0; i < n; i++) {
766 if (c->check(tp) == high)
769 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
770 c->msg, !high, n, d);
774 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
775 const struct rtl_cond *c,
776 unsigned int d, int n)
778 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
781 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
782 const struct rtl_cond *c,
783 unsigned int d, int n)
785 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
788 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
789 const struct rtl_cond *c,
790 unsigned int d, int n)
792 return rtl_loop_wait(tp, c, msleep, d, n, true);
795 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
796 const struct rtl_cond *c,
797 unsigned int d, int n)
799 return rtl_loop_wait(tp, c, msleep, d, n, false);
802 #define DECLARE_RTL_COND(name) \
803 static bool name ## _check(struct rtl8169_private *); \
805 static const struct rtl_cond name = { \
806 .check = name ## _check, \
810 static bool name ## _check(struct rtl8169_private *tp)
812 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
814 if (reg & 0xffff0001) {
815 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
821 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
823 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
826 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
828 if (rtl_ocp_reg_failure(tp, reg))
831 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
833 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
836 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
838 if (rtl_ocp_reg_failure(tp, reg))
841 RTL_W32(tp, GPHY_OCP, reg << 15);
843 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
844 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
847 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
849 if (rtl_ocp_reg_failure(tp, reg))
852 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
855 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
857 if (rtl_ocp_reg_failure(tp, reg))
860 RTL_W32(tp, OCPDR, reg << 15);
862 return RTL_R32(tp, OCPDR);
865 #define OCP_STD_PHY_BASE 0xa400
867 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
870 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
874 if (tp->ocp_base != OCP_STD_PHY_BASE)
877 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
880 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
882 if (tp->ocp_base != OCP_STD_PHY_BASE)
885 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
888 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
891 tp->ocp_base = value << 4;
895 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
898 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
900 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
903 DECLARE_RTL_COND(rtl_phyar_cond)
905 return RTL_R32(tp, PHYAR) & 0x80000000;
908 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
910 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
912 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
914 * According to hardware specs a 20us delay is required after write
915 * complete indication, but before sending next command.
920 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
924 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
926 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
927 RTL_R32(tp, PHYAR) & 0xffff : ~0;
930 * According to hardware specs a 20us delay is required after read
931 * complete indication, but before sending next command.
938 DECLARE_RTL_COND(rtl_ocpar_cond)
940 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
943 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
945 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
946 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
947 RTL_W32(tp, EPHY_RXER_NUM, 0);
949 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
952 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
954 r8168dp_1_mdio_access(tp, reg,
955 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
958 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
960 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
963 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
964 RTL_W32(tp, EPHY_RXER_NUM, 0);
966 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
967 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
970 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
972 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
974 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
977 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
979 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
982 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
984 r8168dp_2_mdio_start(tp);
986 r8169_mdio_write(tp, reg, value);
988 r8168dp_2_mdio_stop(tp);
991 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
995 /* Work around issue with chip reporting wrong PHY ID */
996 if (reg == MII_PHYSID2)
999 r8168dp_2_mdio_start(tp);
1001 value = r8169_mdio_read(tp, reg);
1003 r8168dp_2_mdio_stop(tp);
1008 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1010 tp->mdio_ops.write(tp, location, val);
1013 static int rtl_readphy(struct rtl8169_private *tp, int location)
1015 return tp->mdio_ops.read(tp, location);
1018 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1020 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1023 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1027 val = rtl_readphy(tp, reg_addr);
1028 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1031 DECLARE_RTL_COND(rtl_ephyar_cond)
1033 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1036 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1038 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1039 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1041 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1046 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1048 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1050 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1051 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1054 DECLARE_RTL_COND(rtl_eriar_cond)
1056 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
1059 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1062 BUG_ON((addr & 3) || (mask == 0));
1063 RTL_W32(tp, ERIDR, val);
1064 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1066 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1069 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1071 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1073 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1074 RTL_R32(tp, ERIDR) : ~0;
1077 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1082 val = rtl_eri_read(tp, addr, type);
1083 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1086 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1088 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1089 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1090 RTL_R32(tp, OCPDR) : ~0;
1093 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1095 return rtl_eri_read(tp, reg, ERIAR_OOB);
1098 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1100 switch (tp->mac_version) {
1101 case RTL_GIGA_MAC_VER_27:
1102 case RTL_GIGA_MAC_VER_28:
1103 case RTL_GIGA_MAC_VER_31:
1104 return r8168dp_ocp_read(tp, mask, reg);
1105 case RTL_GIGA_MAC_VER_49:
1106 case RTL_GIGA_MAC_VER_50:
1107 case RTL_GIGA_MAC_VER_51:
1108 return r8168ep_ocp_read(tp, mask, reg);
1115 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1118 RTL_W32(tp, OCPDR, data);
1119 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1120 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1123 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1126 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1130 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1132 switch (tp->mac_version) {
1133 case RTL_GIGA_MAC_VER_27:
1134 case RTL_GIGA_MAC_VER_28:
1135 case RTL_GIGA_MAC_VER_31:
1136 r8168dp_ocp_write(tp, mask, reg, data);
1138 case RTL_GIGA_MAC_VER_49:
1139 case RTL_GIGA_MAC_VER_50:
1140 case RTL_GIGA_MAC_VER_51:
1141 r8168ep_ocp_write(tp, mask, reg, data);
1149 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1151 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1153 ocp_write(tp, 0x1, 0x30, 0x00000001);
1156 #define OOB_CMD_RESET 0x00
1157 #define OOB_CMD_DRIVER_START 0x05
1158 #define OOB_CMD_DRIVER_STOP 0x06
1160 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1162 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1165 DECLARE_RTL_COND(rtl_ocp_read_cond)
1169 reg = rtl8168_get_ocp_reg(tp);
1171 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1174 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1176 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1179 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1181 return RTL_R8(tp, IBISR0) & 0x20;
1184 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1186 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1187 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1188 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1189 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1192 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1194 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1195 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1198 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1200 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1201 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1202 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1205 static void rtl8168_driver_start(struct rtl8169_private *tp)
1207 switch (tp->mac_version) {
1208 case RTL_GIGA_MAC_VER_27:
1209 case RTL_GIGA_MAC_VER_28:
1210 case RTL_GIGA_MAC_VER_31:
1211 rtl8168dp_driver_start(tp);
1213 case RTL_GIGA_MAC_VER_49:
1214 case RTL_GIGA_MAC_VER_50:
1215 case RTL_GIGA_MAC_VER_51:
1216 rtl8168ep_driver_start(tp);
1224 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1226 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1227 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1230 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1232 rtl8168ep_stop_cmac(tp);
1233 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1234 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1235 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1238 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1240 switch (tp->mac_version) {
1241 case RTL_GIGA_MAC_VER_27:
1242 case RTL_GIGA_MAC_VER_28:
1243 case RTL_GIGA_MAC_VER_31:
1244 rtl8168dp_driver_stop(tp);
1246 case RTL_GIGA_MAC_VER_49:
1247 case RTL_GIGA_MAC_VER_50:
1248 case RTL_GIGA_MAC_VER_51:
1249 rtl8168ep_driver_stop(tp);
1257 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1259 u16 reg = rtl8168_get_ocp_reg(tp);
1261 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
1264 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1266 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
1269 static bool r8168_check_dash(struct rtl8169_private *tp)
1271 switch (tp->mac_version) {
1272 case RTL_GIGA_MAC_VER_27:
1273 case RTL_GIGA_MAC_VER_28:
1274 case RTL_GIGA_MAC_VER_31:
1275 return r8168dp_check_dash(tp);
1276 case RTL_GIGA_MAC_VER_49:
1277 case RTL_GIGA_MAC_VER_50:
1278 case RTL_GIGA_MAC_VER_51:
1279 return r8168ep_check_dash(tp);
1291 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1292 const struct exgmac_reg *r, int len)
1295 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1300 DECLARE_RTL_COND(rtl_efusear_cond)
1302 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1305 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1307 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1309 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1310 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1313 static u16 rtl_get_events(struct rtl8169_private *tp)
1315 return RTL_R16(tp, IntrStatus);
1318 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1320 RTL_W16(tp, IntrStatus, bits);
1324 static void rtl_irq_disable(struct rtl8169_private *tp)
1326 RTL_W16(tp, IntrMask, 0);
1330 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1332 RTL_W16(tp, IntrMask, bits);
1335 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1336 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1337 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1339 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1341 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1344 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1346 rtl_irq_disable(tp);
1347 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1348 RTL_R8(tp, ChipCmd);
1351 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1353 struct net_device *dev = tp->dev;
1354 struct phy_device *phydev = dev->phydev;
1356 if (!netif_running(dev))
1359 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1360 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1361 if (phydev->speed == SPEED_1000) {
1362 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1364 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1366 } else if (phydev->speed == SPEED_100) {
1367 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1369 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1372 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1374 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1377 /* Reset packet filter */
1378 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1380 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1382 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1383 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1384 if (phydev->speed == SPEED_1000) {
1385 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1387 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1390 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1392 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1395 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1396 if (phydev->speed == SPEED_10) {
1397 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1399 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1402 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1408 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1410 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1415 options = RTL_R8(tp, Config1);
1416 if (!(options & PMEnable))
1419 options = RTL_R8(tp, Config3);
1420 if (options & LinkUp)
1421 wolopts |= WAKE_PHY;
1422 switch (tp->mac_version) {
1423 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1424 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1425 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1426 wolopts |= WAKE_MAGIC;
1429 if (options & MagicPacket)
1430 wolopts |= WAKE_MAGIC;
1434 options = RTL_R8(tp, Config5);
1436 wolopts |= WAKE_UCAST;
1438 wolopts |= WAKE_BCAST;
1440 wolopts |= WAKE_MCAST;
1445 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1447 struct rtl8169_private *tp = netdev_priv(dev);
1450 wol->supported = WAKE_ANY;
1451 wol->wolopts = tp->saved_wolopts;
1452 rtl_unlock_work(tp);
1455 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1457 unsigned int i, tmp;
1458 static const struct {
1463 { WAKE_PHY, Config3, LinkUp },
1464 { WAKE_UCAST, Config5, UWF },
1465 { WAKE_BCAST, Config5, BWF },
1466 { WAKE_MCAST, Config5, MWF },
1467 { WAKE_ANY, Config5, LanWake },
1468 { WAKE_MAGIC, Config3, MagicPacket }
1472 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
1474 switch (tp->mac_version) {
1475 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1476 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1477 tmp = ARRAY_SIZE(cfg) - 1;
1478 if (wolopts & WAKE_MAGIC)
1494 tmp = ARRAY_SIZE(cfg);
1498 for (i = 0; i < tmp; i++) {
1499 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1500 if (wolopts & cfg[i].opt)
1501 options |= cfg[i].mask;
1502 RTL_W8(tp, cfg[i].reg, options);
1505 switch (tp->mac_version) {
1506 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1507 options = RTL_R8(tp, Config1) & ~PMEnable;
1509 options |= PMEnable;
1510 RTL_W8(tp, Config1, options);
1513 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
1515 options |= PME_SIGNAL;
1516 RTL_W8(tp, Config2, options);
1520 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
1522 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
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 pm_runtime_put_noidle(d);
1549 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1551 return rtl_chip_infos[tp->mac_version].fw_name;
1554 static void rtl8169_get_drvinfo(struct net_device *dev,
1555 struct ethtool_drvinfo *info)
1557 struct rtl8169_private *tp = netdev_priv(dev);
1558 struct rtl_fw *rtl_fw = tp->rtl_fw;
1560 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1561 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1562 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1563 if (!IS_ERR_OR_NULL(rtl_fw))
1564 strlcpy(info->fw_version, rtl_fw->version,
1565 sizeof(info->fw_version));
1568 static int rtl8169_get_regs_len(struct net_device *dev)
1570 return R8169_REGS_SIZE;
1573 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1574 netdev_features_t features)
1576 struct rtl8169_private *tp = netdev_priv(dev);
1578 if (dev->mtu > TD_MSS_MAX)
1579 features &= ~NETIF_F_ALL_TSO;
1581 if (dev->mtu > JUMBO_1K &&
1582 tp->mac_version > RTL_GIGA_MAC_VER_06)
1583 features &= ~NETIF_F_IP_CSUM;
1588 static int rtl8169_set_features(struct net_device *dev,
1589 netdev_features_t features)
1591 struct rtl8169_private *tp = netdev_priv(dev);
1596 rx_config = RTL_R32(tp, RxConfig);
1597 if (features & NETIF_F_RXALL)
1598 rx_config |= (AcceptErr | AcceptRunt);
1600 rx_config &= ~(AcceptErr | AcceptRunt);
1602 RTL_W32(tp, RxConfig, rx_config);
1604 if (features & NETIF_F_RXCSUM)
1605 tp->cp_cmd |= RxChkSum;
1607 tp->cp_cmd &= ~RxChkSum;
1609 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1610 tp->cp_cmd |= RxVlan;
1612 tp->cp_cmd &= ~RxVlan;
1614 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1615 RTL_R16(tp, CPlusCmd);
1617 rtl_unlock_work(tp);
1622 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1624 return (skb_vlan_tag_present(skb)) ?
1625 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1628 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1630 u32 opts2 = le32_to_cpu(desc->opts2);
1632 if (opts2 & RxVlanTag)
1633 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1636 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1639 struct rtl8169_private *tp = netdev_priv(dev);
1640 u32 __iomem *data = tp->mmio_addr;
1645 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1646 memcpy_fromio(dw++, data++, 4);
1647 rtl_unlock_work(tp);
1650 static u32 rtl8169_get_msglevel(struct net_device *dev)
1652 struct rtl8169_private *tp = netdev_priv(dev);
1654 return tp->msg_enable;
1657 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1659 struct rtl8169_private *tp = netdev_priv(dev);
1661 tp->msg_enable = value;
1664 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1671 "tx_single_collisions",
1672 "tx_multi_collisions",
1680 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1684 return ARRAY_SIZE(rtl8169_gstrings);
1690 DECLARE_RTL_COND(rtl_counters_cond)
1692 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
1695 static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
1697 dma_addr_t paddr = tp->counters_phys_addr;
1700 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1701 RTL_R32(tp, CounterAddrHigh);
1702 cmd = (u64)paddr & DMA_BIT_MASK(32);
1703 RTL_W32(tp, CounterAddrLow, cmd);
1704 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
1706 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
1709 static bool rtl8169_reset_counters(struct rtl8169_private *tp)
1712 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1715 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1718 return rtl8169_do_counters(tp, CounterReset);
1721 static bool rtl8169_update_counters(struct rtl8169_private *tp)
1723 u8 val = RTL_R8(tp, ChipCmd);
1726 * Some chips are unable to dump tally counters when the receiver
1727 * is disabled. If 0xff chip may be in a PCI power-save state.
1729 if (!(val & CmdRxEnb) || val == 0xff)
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;
2172 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2174 netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
2182 static void rtl_writephy_batch(struct rtl8169_private *tp,
2183 const struct phy_reg *regs, int len)
2186 rtl_writephy(tp, regs->reg, regs->val);
2191 #define PHY_READ 0x00000000
2192 #define PHY_DATA_OR 0x10000000
2193 #define PHY_DATA_AND 0x20000000
2194 #define PHY_BJMPN 0x30000000
2195 #define PHY_MDIO_CHG 0x40000000
2196 #define PHY_CLEAR_READCOUNT 0x70000000
2197 #define PHY_WRITE 0x80000000
2198 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2199 #define PHY_COMP_EQ_SKIPN 0xa0000000
2200 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2201 #define PHY_WRITE_PREVIOUS 0xc0000000
2202 #define PHY_SKIPN 0xd0000000
2203 #define PHY_DELAY_MS 0xe0000000
2207 char version[RTL_VER_SIZE];
2213 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2215 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2217 const struct firmware *fw = rtl_fw->fw;
2218 struct fw_info *fw_info = (struct fw_info *)fw->data;
2219 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2220 char *version = rtl_fw->version;
2223 if (fw->size < FW_OPCODE_SIZE)
2226 if (!fw_info->magic) {
2227 size_t i, size, start;
2230 if (fw->size < sizeof(*fw_info))
2233 for (i = 0; i < fw->size; i++)
2234 checksum += fw->data[i];
2238 start = le32_to_cpu(fw_info->fw_start);
2239 if (start > fw->size)
2242 size = le32_to_cpu(fw_info->fw_len);
2243 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2246 memcpy(version, fw_info->version, RTL_VER_SIZE);
2248 pa->code = (__le32 *)(fw->data + start);
2251 if (fw->size % FW_OPCODE_SIZE)
2254 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2256 pa->code = (__le32 *)fw->data;
2257 pa->size = fw->size / FW_OPCODE_SIZE;
2259 version[RTL_VER_SIZE - 1] = 0;
2266 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2267 struct rtl_fw_phy_action *pa)
2272 for (index = 0; index < pa->size; index++) {
2273 u32 action = le32_to_cpu(pa->code[index]);
2274 u32 regno = (action & 0x0fff0000) >> 16;
2276 switch(action & 0xf0000000) {
2281 case PHY_CLEAR_READCOUNT:
2283 case PHY_WRITE_PREVIOUS:
2288 if (regno > index) {
2289 netif_err(tp, ifup, tp->dev,
2290 "Out of range of firmware\n");
2294 case PHY_READCOUNT_EQ_SKIP:
2295 if (index + 2 >= pa->size) {
2296 netif_err(tp, ifup, tp->dev,
2297 "Out of range of firmware\n");
2301 case PHY_COMP_EQ_SKIPN:
2302 case PHY_COMP_NEQ_SKIPN:
2304 if (index + 1 + regno >= pa->size) {
2305 netif_err(tp, ifup, tp->dev,
2306 "Out of range of firmware\n");
2312 netif_err(tp, ifup, tp->dev,
2313 "Invalid action 0x%08x\n", action);
2322 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2324 struct net_device *dev = tp->dev;
2327 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2328 netif_err(tp, ifup, dev, "invalid firmware\n");
2332 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2338 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2340 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2341 struct mdio_ops org, *ops = &tp->mdio_ops;
2345 predata = count = 0;
2346 org.write = ops->write;
2347 org.read = ops->read;
2349 for (index = 0; index < pa->size; ) {
2350 u32 action = le32_to_cpu(pa->code[index]);
2351 u32 data = action & 0x0000ffff;
2352 u32 regno = (action & 0x0fff0000) >> 16;
2357 switch(action & 0xf0000000) {
2359 predata = rtl_readphy(tp, regno);
2376 ops->write = org.write;
2377 ops->read = org.read;
2378 } else if (data == 1) {
2379 ops->write = mac_mcu_write;
2380 ops->read = mac_mcu_read;
2385 case PHY_CLEAR_READCOUNT:
2390 rtl_writephy(tp, regno, data);
2393 case PHY_READCOUNT_EQ_SKIP:
2394 index += (count == data) ? 2 : 1;
2396 case PHY_COMP_EQ_SKIPN:
2397 if (predata == data)
2401 case PHY_COMP_NEQ_SKIPN:
2402 if (predata != data)
2406 case PHY_WRITE_PREVIOUS:
2407 rtl_writephy(tp, regno, predata);
2423 ops->write = org.write;
2424 ops->read = org.read;
2427 static void rtl_release_firmware(struct rtl8169_private *tp)
2429 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2430 release_firmware(tp->rtl_fw->fw);
2433 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2436 static void rtl_apply_firmware(struct rtl8169_private *tp)
2438 struct rtl_fw *rtl_fw = tp->rtl_fw;
2440 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2441 if (!IS_ERR_OR_NULL(rtl_fw))
2442 rtl_phy_write_fw(tp, rtl_fw);
2445 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2447 if (rtl_readphy(tp, reg) != val)
2448 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2450 rtl_apply_firmware(tp);
2453 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2455 static const struct phy_reg phy_reg_init[] = {
2517 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2520 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2522 static const struct phy_reg phy_reg_init[] = {
2528 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2531 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2533 struct pci_dev *pdev = tp->pci_dev;
2535 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2536 (pdev->subsystem_device != 0xe000))
2539 rtl_writephy(tp, 0x1f, 0x0001);
2540 rtl_writephy(tp, 0x10, 0xf01b);
2541 rtl_writephy(tp, 0x1f, 0x0000);
2544 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2546 static const struct phy_reg phy_reg_init[] = {
2586 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2588 rtl8169scd_hw_phy_config_quirk(tp);
2591 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2593 static const struct phy_reg phy_reg_init[] = {
2641 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2644 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2646 static const struct phy_reg phy_reg_init[] = {
2651 rtl_writephy(tp, 0x1f, 0x0001);
2652 rtl_patchphy(tp, 0x16, 1 << 0);
2654 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2657 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2659 static const struct phy_reg phy_reg_init[] = {
2665 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2668 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2670 static const struct phy_reg phy_reg_init[] = {
2678 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2681 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2683 static const struct phy_reg phy_reg_init[] = {
2689 rtl_writephy(tp, 0x1f, 0x0000);
2690 rtl_patchphy(tp, 0x14, 1 << 5);
2691 rtl_patchphy(tp, 0x0d, 1 << 5);
2693 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2696 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2698 static const struct phy_reg phy_reg_init[] = {
2718 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2720 rtl_patchphy(tp, 0x14, 1 << 5);
2721 rtl_patchphy(tp, 0x0d, 1 << 5);
2722 rtl_writephy(tp, 0x1f, 0x0000);
2725 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2727 static const struct phy_reg phy_reg_init[] = {
2745 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2747 rtl_patchphy(tp, 0x16, 1 << 0);
2748 rtl_patchphy(tp, 0x14, 1 << 5);
2749 rtl_patchphy(tp, 0x0d, 1 << 5);
2750 rtl_writephy(tp, 0x1f, 0x0000);
2753 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2755 static const struct phy_reg phy_reg_init[] = {
2767 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2769 rtl_patchphy(tp, 0x16, 1 << 0);
2770 rtl_patchphy(tp, 0x14, 1 << 5);
2771 rtl_patchphy(tp, 0x0d, 1 << 5);
2772 rtl_writephy(tp, 0x1f, 0x0000);
2775 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2777 rtl8168c_3_hw_phy_config(tp);
2780 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2782 static const struct phy_reg phy_reg_init_0[] = {
2783 /* Channel Estimation */
2804 * Enhance line driver power
2813 * Can not link to 1Gbps with bad cable
2814 * Decrease SNR threshold form 21.07dB to 19.04dB
2823 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2827 * Fine Tune Switching regulator parameter
2829 rtl_writephy(tp, 0x1f, 0x0002);
2830 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2831 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
2833 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2834 static const struct phy_reg phy_reg_init[] = {
2844 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2846 val = rtl_readphy(tp, 0x0d);
2848 if ((val & 0x00ff) != 0x006c) {
2849 static const u32 set[] = {
2850 0x0065, 0x0066, 0x0067, 0x0068,
2851 0x0069, 0x006a, 0x006b, 0x006c
2855 rtl_writephy(tp, 0x1f, 0x0002);
2858 for (i = 0; i < ARRAY_SIZE(set); i++)
2859 rtl_writephy(tp, 0x0d, val | set[i]);
2862 static const struct phy_reg phy_reg_init[] = {
2870 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2873 /* RSET couple improve */
2874 rtl_writephy(tp, 0x1f, 0x0002);
2875 rtl_patchphy(tp, 0x0d, 0x0300);
2876 rtl_patchphy(tp, 0x0f, 0x0010);
2878 /* Fine tune PLL performance */
2879 rtl_writephy(tp, 0x1f, 0x0002);
2880 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2881 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
2883 rtl_writephy(tp, 0x1f, 0x0005);
2884 rtl_writephy(tp, 0x05, 0x001b);
2886 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2888 rtl_writephy(tp, 0x1f, 0x0000);
2891 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2893 static const struct phy_reg phy_reg_init_0[] = {
2894 /* Channel Estimation */
2915 * Enhance line driver power
2924 * Can not link to 1Gbps with bad cable
2925 * Decrease SNR threshold form 21.07dB to 19.04dB
2934 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2936 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2937 static const struct phy_reg phy_reg_init[] = {
2948 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2950 val = rtl_readphy(tp, 0x0d);
2951 if ((val & 0x00ff) != 0x006c) {
2952 static const u32 set[] = {
2953 0x0065, 0x0066, 0x0067, 0x0068,
2954 0x0069, 0x006a, 0x006b, 0x006c
2958 rtl_writephy(tp, 0x1f, 0x0002);
2961 for (i = 0; i < ARRAY_SIZE(set); i++)
2962 rtl_writephy(tp, 0x0d, val | set[i]);
2965 static const struct phy_reg phy_reg_init[] = {
2973 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2976 /* Fine tune PLL performance */
2977 rtl_writephy(tp, 0x1f, 0x0002);
2978 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2979 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
2981 /* Switching regulator Slew rate */
2982 rtl_writephy(tp, 0x1f, 0x0002);
2983 rtl_patchphy(tp, 0x0f, 0x0017);
2985 rtl_writephy(tp, 0x1f, 0x0005);
2986 rtl_writephy(tp, 0x05, 0x001b);
2988 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2990 rtl_writephy(tp, 0x1f, 0x0000);
2993 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2995 static const struct phy_reg phy_reg_init[] = {
3051 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3054 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3056 static const struct phy_reg phy_reg_init[] = {
3066 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3067 rtl_patchphy(tp, 0x0d, 1 << 5);
3070 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3072 static const struct phy_reg phy_reg_init[] = {
3073 /* Enable Delay cap */
3079 /* Channel estimation fine tune */
3088 /* Update PFM & 10M TX idle timer */
3100 rtl_apply_firmware(tp);
3102 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3104 /* DCO enable for 10M IDLE Power */
3105 rtl_writephy(tp, 0x1f, 0x0007);
3106 rtl_writephy(tp, 0x1e, 0x0023);
3107 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3108 rtl_writephy(tp, 0x1f, 0x0000);
3110 /* For impedance matching */
3111 rtl_writephy(tp, 0x1f, 0x0002);
3112 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3113 rtl_writephy(tp, 0x1f, 0x0000);
3115 /* PHY auto speed down */
3116 rtl_writephy(tp, 0x1f, 0x0007);
3117 rtl_writephy(tp, 0x1e, 0x002d);
3118 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3119 rtl_writephy(tp, 0x1f, 0x0000);
3120 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3122 rtl_writephy(tp, 0x1f, 0x0005);
3123 rtl_writephy(tp, 0x05, 0x8b86);
3124 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3125 rtl_writephy(tp, 0x1f, 0x0000);
3127 rtl_writephy(tp, 0x1f, 0x0005);
3128 rtl_writephy(tp, 0x05, 0x8b85);
3129 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3130 rtl_writephy(tp, 0x1f, 0x0007);
3131 rtl_writephy(tp, 0x1e, 0x0020);
3132 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3133 rtl_writephy(tp, 0x1f, 0x0006);
3134 rtl_writephy(tp, 0x00, 0x5a00);
3135 rtl_writephy(tp, 0x1f, 0x0000);
3136 rtl_writephy(tp, 0x0d, 0x0007);
3137 rtl_writephy(tp, 0x0e, 0x003c);
3138 rtl_writephy(tp, 0x0d, 0x4007);
3139 rtl_writephy(tp, 0x0e, 0x0000);
3140 rtl_writephy(tp, 0x0d, 0x0000);
3143 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3146 addr[0] | (addr[1] << 8),
3147 addr[2] | (addr[3] << 8),
3148 addr[4] | (addr[5] << 8)
3150 const struct exgmac_reg e[] = {
3151 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3152 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3153 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3154 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3157 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3160 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3162 static const struct phy_reg phy_reg_init[] = {
3163 /* Enable Delay cap */
3172 /* Channel estimation fine tune */
3189 rtl_apply_firmware(tp);
3191 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3193 /* For 4-corner performance improve */
3194 rtl_writephy(tp, 0x1f, 0x0005);
3195 rtl_writephy(tp, 0x05, 0x8b80);
3196 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3197 rtl_writephy(tp, 0x1f, 0x0000);
3199 /* PHY auto speed down */
3200 rtl_writephy(tp, 0x1f, 0x0004);
3201 rtl_writephy(tp, 0x1f, 0x0007);
3202 rtl_writephy(tp, 0x1e, 0x002d);
3203 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3204 rtl_writephy(tp, 0x1f, 0x0002);
3205 rtl_writephy(tp, 0x1f, 0x0000);
3206 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3208 /* improve 10M EEE waveform */
3209 rtl_writephy(tp, 0x1f, 0x0005);
3210 rtl_writephy(tp, 0x05, 0x8b86);
3211 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3212 rtl_writephy(tp, 0x1f, 0x0000);
3214 /* Improve 2-pair detection performance */
3215 rtl_writephy(tp, 0x1f, 0x0005);
3216 rtl_writephy(tp, 0x05, 0x8b85);
3217 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3218 rtl_writephy(tp, 0x1f, 0x0000);
3221 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
3222 rtl_writephy(tp, 0x1f, 0x0005);
3223 rtl_writephy(tp, 0x05, 0x8b85);
3224 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
3225 rtl_writephy(tp, 0x1f, 0x0004);
3226 rtl_writephy(tp, 0x1f, 0x0007);
3227 rtl_writephy(tp, 0x1e, 0x0020);
3228 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
3229 rtl_writephy(tp, 0x1f, 0x0002);
3230 rtl_writephy(tp, 0x1f, 0x0000);
3231 rtl_writephy(tp, 0x0d, 0x0007);
3232 rtl_writephy(tp, 0x0e, 0x003c);
3233 rtl_writephy(tp, 0x0d, 0x4007);
3234 rtl_writephy(tp, 0x0e, 0x0006);
3235 rtl_writephy(tp, 0x0d, 0x0000);
3238 rtl_writephy(tp, 0x1f, 0x0003);
3239 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3240 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
3241 rtl_writephy(tp, 0x1f, 0x0000);
3242 rtl_writephy(tp, 0x1f, 0x0005);
3243 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3244 rtl_writephy(tp, 0x1f, 0x0000);
3246 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3247 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3250 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3252 /* For 4-corner performance improve */
3253 rtl_writephy(tp, 0x1f, 0x0005);
3254 rtl_writephy(tp, 0x05, 0x8b80);
3255 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3256 rtl_writephy(tp, 0x1f, 0x0000);
3258 /* PHY auto speed down */
3259 rtl_writephy(tp, 0x1f, 0x0007);
3260 rtl_writephy(tp, 0x1e, 0x002d);
3261 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3262 rtl_writephy(tp, 0x1f, 0x0000);
3263 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3265 /* Improve 10M EEE waveform */
3266 rtl_writephy(tp, 0x1f, 0x0005);
3267 rtl_writephy(tp, 0x05, 0x8b86);
3268 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3269 rtl_writephy(tp, 0x1f, 0x0000);
3272 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3274 static const struct phy_reg phy_reg_init[] = {
3275 /* Channel estimation fine tune */
3280 /* Modify green table for giga & fnet */
3297 /* Modify green table for 10M */
3303 /* Disable hiimpedance detection (RTCT) */
3309 rtl_apply_firmware(tp);
3311 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3313 rtl8168f_hw_phy_config(tp);
3315 /* Improve 2-pair detection performance */
3316 rtl_writephy(tp, 0x1f, 0x0005);
3317 rtl_writephy(tp, 0x05, 0x8b85);
3318 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3319 rtl_writephy(tp, 0x1f, 0x0000);
3322 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3324 rtl_apply_firmware(tp);
3326 rtl8168f_hw_phy_config(tp);
3329 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3331 static const struct phy_reg phy_reg_init[] = {
3332 /* Channel estimation fine tune */
3337 /* Modify green table for giga & fnet */
3354 /* Modify green table for 10M */
3360 /* Disable hiimpedance detection (RTCT) */
3367 rtl_apply_firmware(tp);
3369 rtl8168f_hw_phy_config(tp);
3371 /* Improve 2-pair detection performance */
3372 rtl_writephy(tp, 0x1f, 0x0005);
3373 rtl_writephy(tp, 0x05, 0x8b85);
3374 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3375 rtl_writephy(tp, 0x1f, 0x0000);
3377 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3379 /* Modify green table for giga */
3380 rtl_writephy(tp, 0x1f, 0x0005);
3381 rtl_writephy(tp, 0x05, 0x8b54);
3382 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3383 rtl_writephy(tp, 0x05, 0x8b5d);
3384 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3385 rtl_writephy(tp, 0x05, 0x8a7c);
3386 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3387 rtl_writephy(tp, 0x05, 0x8a7f);
3388 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3389 rtl_writephy(tp, 0x05, 0x8a82);
3390 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3391 rtl_writephy(tp, 0x05, 0x8a85);
3392 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3393 rtl_writephy(tp, 0x05, 0x8a88);
3394 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3395 rtl_writephy(tp, 0x1f, 0x0000);
3397 /* uc same-seed solution */
3398 rtl_writephy(tp, 0x1f, 0x0005);
3399 rtl_writephy(tp, 0x05, 0x8b85);
3400 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3401 rtl_writephy(tp, 0x1f, 0x0000);
3404 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3405 rtl_writephy(tp, 0x1f, 0x0005);
3406 rtl_writephy(tp, 0x05, 0x8b85);
3407 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3408 rtl_writephy(tp, 0x1f, 0x0004);
3409 rtl_writephy(tp, 0x1f, 0x0007);
3410 rtl_writephy(tp, 0x1e, 0x0020);
3411 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3412 rtl_writephy(tp, 0x1f, 0x0000);
3413 rtl_writephy(tp, 0x0d, 0x0007);
3414 rtl_writephy(tp, 0x0e, 0x003c);
3415 rtl_writephy(tp, 0x0d, 0x4007);
3416 rtl_writephy(tp, 0x0e, 0x0000);
3417 rtl_writephy(tp, 0x0d, 0x0000);
3420 rtl_writephy(tp, 0x1f, 0x0003);
3421 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3422 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3423 rtl_writephy(tp, 0x1f, 0x0000);
3426 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3428 rtl_apply_firmware(tp);
3430 rtl_writephy(tp, 0x1f, 0x0a46);
3431 if (rtl_readphy(tp, 0x10) & 0x0100) {
3432 rtl_writephy(tp, 0x1f, 0x0bcc);
3433 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3435 rtl_writephy(tp, 0x1f, 0x0bcc);
3436 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3439 rtl_writephy(tp, 0x1f, 0x0a46);
3440 if (rtl_readphy(tp, 0x13) & 0x0100) {
3441 rtl_writephy(tp, 0x1f, 0x0c41);
3442 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3444 rtl_writephy(tp, 0x1f, 0x0c41);
3445 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3448 /* Enable PHY auto speed down */
3449 rtl_writephy(tp, 0x1f, 0x0a44);
3450 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3452 rtl_writephy(tp, 0x1f, 0x0bcc);
3453 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
3454 rtl_writephy(tp, 0x1f, 0x0a44);
3455 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3456 rtl_writephy(tp, 0x1f, 0x0a43);
3457 rtl_writephy(tp, 0x13, 0x8084);
3458 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3459 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3461 /* EEE auto-fallback function */
3462 rtl_writephy(tp, 0x1f, 0x0a4b);
3463 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3465 /* Enable UC LPF tune function */
3466 rtl_writephy(tp, 0x1f, 0x0a43);
3467 rtl_writephy(tp, 0x13, 0x8012);
3468 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3470 rtl_writephy(tp, 0x1f, 0x0c42);
3471 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3473 /* Improve SWR Efficiency */
3474 rtl_writephy(tp, 0x1f, 0x0bcd);
3475 rtl_writephy(tp, 0x14, 0x5065);
3476 rtl_writephy(tp, 0x14, 0xd065);
3477 rtl_writephy(tp, 0x1f, 0x0bc8);
3478 rtl_writephy(tp, 0x11, 0x5655);
3479 rtl_writephy(tp, 0x1f, 0x0bcd);
3480 rtl_writephy(tp, 0x14, 0x1065);
3481 rtl_writephy(tp, 0x14, 0x9065);
3482 rtl_writephy(tp, 0x14, 0x1065);
3484 /* Check ALDPS bit, disable it if enabled */
3485 rtl_writephy(tp, 0x1f, 0x0a43);
3486 if (rtl_readphy(tp, 0x10) & 0x0004)
3487 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3489 rtl_writephy(tp, 0x1f, 0x0000);
3492 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3494 rtl_apply_firmware(tp);
3497 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3502 rtl_apply_firmware(tp);
3504 /* CHN EST parameters adjust - giga master */
3505 rtl_writephy(tp, 0x1f, 0x0a43);
3506 rtl_writephy(tp, 0x13, 0x809b);
3507 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3508 rtl_writephy(tp, 0x13, 0x80a2);
3509 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3510 rtl_writephy(tp, 0x13, 0x80a4);
3511 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3512 rtl_writephy(tp, 0x13, 0x809c);
3513 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3514 rtl_writephy(tp, 0x1f, 0x0000);
3516 /* CHN EST parameters adjust - giga slave */
3517 rtl_writephy(tp, 0x1f, 0x0a43);
3518 rtl_writephy(tp, 0x13, 0x80ad);
3519 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
3520 rtl_writephy(tp, 0x13, 0x80b4);
3521 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
3522 rtl_writephy(tp, 0x13, 0x80ac);
3523 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
3524 rtl_writephy(tp, 0x1f, 0x0000);
3526 /* CHN EST parameters adjust - fnet */
3527 rtl_writephy(tp, 0x1f, 0x0a43);
3528 rtl_writephy(tp, 0x13, 0x808e);
3529 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
3530 rtl_writephy(tp, 0x13, 0x8090);
3531 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
3532 rtl_writephy(tp, 0x13, 0x8092);
3533 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
3534 rtl_writephy(tp, 0x1f, 0x0000);
3536 /* enable R-tune & PGA-retune function */
3538 rtl_writephy(tp, 0x1f, 0x0a46);
3539 data = rtl_readphy(tp, 0x13);
3542 dout_tapbin |= data;
3543 data = rtl_readphy(tp, 0x12);
3546 dout_tapbin |= data;
3547 dout_tapbin = ~(dout_tapbin^0x08);
3549 dout_tapbin &= 0xf000;
3550 rtl_writephy(tp, 0x1f, 0x0a43);
3551 rtl_writephy(tp, 0x13, 0x827a);
3552 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3553 rtl_writephy(tp, 0x13, 0x827b);
3554 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3555 rtl_writephy(tp, 0x13, 0x827c);
3556 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3557 rtl_writephy(tp, 0x13, 0x827d);
3558 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3560 rtl_writephy(tp, 0x1f, 0x0a43);
3561 rtl_writephy(tp, 0x13, 0x0811);
3562 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3563 rtl_writephy(tp, 0x1f, 0x0a42);
3564 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3565 rtl_writephy(tp, 0x1f, 0x0000);
3567 /* enable GPHY 10M */
3568 rtl_writephy(tp, 0x1f, 0x0a44);
3569 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3570 rtl_writephy(tp, 0x1f, 0x0000);
3572 /* SAR ADC performance */
3573 rtl_writephy(tp, 0x1f, 0x0bca);
3574 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
3575 rtl_writephy(tp, 0x1f, 0x0000);
3577 rtl_writephy(tp, 0x1f, 0x0a43);
3578 rtl_writephy(tp, 0x13, 0x803f);
3579 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3580 rtl_writephy(tp, 0x13, 0x8047);
3581 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3582 rtl_writephy(tp, 0x13, 0x804f);
3583 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3584 rtl_writephy(tp, 0x13, 0x8057);
3585 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3586 rtl_writephy(tp, 0x13, 0x805f);
3587 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3588 rtl_writephy(tp, 0x13, 0x8067);
3589 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3590 rtl_writephy(tp, 0x13, 0x806f);
3591 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3592 rtl_writephy(tp, 0x1f, 0x0000);
3594 /* disable phy pfm mode */
3595 rtl_writephy(tp, 0x1f, 0x0a44);
3596 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3597 rtl_writephy(tp, 0x1f, 0x0000);
3599 /* Check ALDPS bit, disable it if enabled */
3600 rtl_writephy(tp, 0x1f, 0x0a43);
3601 if (rtl_readphy(tp, 0x10) & 0x0004)
3602 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3604 rtl_writephy(tp, 0x1f, 0x0000);
3607 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3609 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3613 rtl_apply_firmware(tp);
3615 /* CHIN EST parameter update */
3616 rtl_writephy(tp, 0x1f, 0x0a43);
3617 rtl_writephy(tp, 0x13, 0x808a);
3618 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
3619 rtl_writephy(tp, 0x1f, 0x0000);
3621 /* enable R-tune & PGA-retune function */
3622 rtl_writephy(tp, 0x1f, 0x0a43);
3623 rtl_writephy(tp, 0x13, 0x0811);
3624 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3625 rtl_writephy(tp, 0x1f, 0x0a42);
3626 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3627 rtl_writephy(tp, 0x1f, 0x0000);
3629 /* enable GPHY 10M */
3630 rtl_writephy(tp, 0x1f, 0x0a44);
3631 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3632 rtl_writephy(tp, 0x1f, 0x0000);
3634 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3635 data = r8168_mac_ocp_read(tp, 0xdd02);
3636 ioffset_p3 = ((data & 0x80)>>7);
3639 data = r8168_mac_ocp_read(tp, 0xdd00);
3640 ioffset_p3 |= ((data & (0xe000))>>13);
3641 ioffset_p2 = ((data & (0x1e00))>>9);
3642 ioffset_p1 = ((data & (0x01e0))>>5);
3643 ioffset_p0 = ((data & 0x0010)>>4);
3645 ioffset_p0 |= (data & (0x07));
3646 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3648 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3649 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
3650 rtl_writephy(tp, 0x1f, 0x0bcf);
3651 rtl_writephy(tp, 0x16, data);
3652 rtl_writephy(tp, 0x1f, 0x0000);
3655 /* Modify rlen (TX LPF corner frequency) level */
3656 rtl_writephy(tp, 0x1f, 0x0bcd);
3657 data = rtl_readphy(tp, 0x16);
3662 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3663 rtl_writephy(tp, 0x17, data);
3664 rtl_writephy(tp, 0x1f, 0x0bcd);
3665 rtl_writephy(tp, 0x1f, 0x0000);
3667 /* disable phy pfm mode */
3668 rtl_writephy(tp, 0x1f, 0x0a44);
3669 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3670 rtl_writephy(tp, 0x1f, 0x0000);
3672 /* Check ALDPS bit, disable it if enabled */
3673 rtl_writephy(tp, 0x1f, 0x0a43);
3674 if (rtl_readphy(tp, 0x10) & 0x0004)
3675 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3677 rtl_writephy(tp, 0x1f, 0x0000);
3680 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3682 /* Enable PHY auto speed down */
3683 rtl_writephy(tp, 0x1f, 0x0a44);
3684 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3685 rtl_writephy(tp, 0x1f, 0x0000);
3687 /* patch 10M & ALDPS */
3688 rtl_writephy(tp, 0x1f, 0x0bcc);
3689 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3690 rtl_writephy(tp, 0x1f, 0x0a44);
3691 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3692 rtl_writephy(tp, 0x1f, 0x0a43);
3693 rtl_writephy(tp, 0x13, 0x8084);
3694 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3695 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3696 rtl_writephy(tp, 0x1f, 0x0000);
3698 /* Enable EEE auto-fallback function */
3699 rtl_writephy(tp, 0x1f, 0x0a4b);
3700 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3701 rtl_writephy(tp, 0x1f, 0x0000);
3703 /* Enable UC LPF tune function */
3704 rtl_writephy(tp, 0x1f, 0x0a43);
3705 rtl_writephy(tp, 0x13, 0x8012);
3706 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3707 rtl_writephy(tp, 0x1f, 0x0000);
3709 /* set rg_sel_sdm_rate */
3710 rtl_writephy(tp, 0x1f, 0x0c42);
3711 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3712 rtl_writephy(tp, 0x1f, 0x0000);
3714 /* Check ALDPS bit, disable it if enabled */
3715 rtl_writephy(tp, 0x1f, 0x0a43);
3716 if (rtl_readphy(tp, 0x10) & 0x0004)
3717 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3719 rtl_writephy(tp, 0x1f, 0x0000);
3722 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3724 /* patch 10M & ALDPS */
3725 rtl_writephy(tp, 0x1f, 0x0bcc);
3726 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3727 rtl_writephy(tp, 0x1f, 0x0a44);
3728 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3729 rtl_writephy(tp, 0x1f, 0x0a43);
3730 rtl_writephy(tp, 0x13, 0x8084);
3731 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3732 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3733 rtl_writephy(tp, 0x1f, 0x0000);
3735 /* Enable UC LPF tune function */
3736 rtl_writephy(tp, 0x1f, 0x0a43);
3737 rtl_writephy(tp, 0x13, 0x8012);
3738 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3739 rtl_writephy(tp, 0x1f, 0x0000);
3741 /* Set rg_sel_sdm_rate */
3742 rtl_writephy(tp, 0x1f, 0x0c42);
3743 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3744 rtl_writephy(tp, 0x1f, 0x0000);
3746 /* Channel estimation parameters */
3747 rtl_writephy(tp, 0x1f, 0x0a43);
3748 rtl_writephy(tp, 0x13, 0x80f3);
3749 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3750 rtl_writephy(tp, 0x13, 0x80f0);
3751 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3752 rtl_writephy(tp, 0x13, 0x80ef);
3753 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3754 rtl_writephy(tp, 0x13, 0x80f6);
3755 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3756 rtl_writephy(tp, 0x13, 0x80ec);
3757 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3758 rtl_writephy(tp, 0x13, 0x80ed);
3759 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3760 rtl_writephy(tp, 0x13, 0x80f2);
3761 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3762 rtl_writephy(tp, 0x13, 0x80f4);
3763 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3764 rtl_writephy(tp, 0x1f, 0x0a43);
3765 rtl_writephy(tp, 0x13, 0x8110);
3766 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3767 rtl_writephy(tp, 0x13, 0x810f);
3768 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3769 rtl_writephy(tp, 0x13, 0x8111);
3770 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3771 rtl_writephy(tp, 0x13, 0x8113);
3772 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3773 rtl_writephy(tp, 0x13, 0x8115);
3774 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3775 rtl_writephy(tp, 0x13, 0x810e);
3776 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3777 rtl_writephy(tp, 0x13, 0x810c);
3778 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3779 rtl_writephy(tp, 0x13, 0x810b);
3780 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3781 rtl_writephy(tp, 0x1f, 0x0a43);
3782 rtl_writephy(tp, 0x13, 0x80d1);
3783 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3784 rtl_writephy(tp, 0x13, 0x80cd);
3785 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3786 rtl_writephy(tp, 0x13, 0x80d3);
3787 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3788 rtl_writephy(tp, 0x13, 0x80d5);
3789 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3790 rtl_writephy(tp, 0x13, 0x80d7);
3791 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3793 /* Force PWM-mode */
3794 rtl_writephy(tp, 0x1f, 0x0bcd);
3795 rtl_writephy(tp, 0x14, 0x5065);
3796 rtl_writephy(tp, 0x14, 0xd065);
3797 rtl_writephy(tp, 0x1f, 0x0bc8);
3798 rtl_writephy(tp, 0x12, 0x00ed);
3799 rtl_writephy(tp, 0x1f, 0x0bcd);
3800 rtl_writephy(tp, 0x14, 0x1065);
3801 rtl_writephy(tp, 0x14, 0x9065);
3802 rtl_writephy(tp, 0x14, 0x1065);
3803 rtl_writephy(tp, 0x1f, 0x0000);
3805 /* Check ALDPS bit, disable it if enabled */
3806 rtl_writephy(tp, 0x1f, 0x0a43);
3807 if (rtl_readphy(tp, 0x10) & 0x0004)
3808 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3810 rtl_writephy(tp, 0x1f, 0x0000);
3813 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3815 static const struct phy_reg phy_reg_init[] = {
3822 rtl_writephy(tp, 0x1f, 0x0000);
3823 rtl_patchphy(tp, 0x11, 1 << 12);
3824 rtl_patchphy(tp, 0x19, 1 << 13);
3825 rtl_patchphy(tp, 0x10, 1 << 15);
3827 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3830 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3832 static const struct phy_reg phy_reg_init[] = {
3846 /* Disable ALDPS before ram code */
3847 rtl_writephy(tp, 0x1f, 0x0000);
3848 rtl_writephy(tp, 0x18, 0x0310);
3851 rtl_apply_firmware(tp);
3853 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3856 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3858 /* Disable ALDPS before setting firmware */
3859 rtl_writephy(tp, 0x1f, 0x0000);
3860 rtl_writephy(tp, 0x18, 0x0310);
3863 rtl_apply_firmware(tp);
3866 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3867 rtl_writephy(tp, 0x1f, 0x0004);
3868 rtl_writephy(tp, 0x10, 0x401f);
3869 rtl_writephy(tp, 0x19, 0x7030);
3870 rtl_writephy(tp, 0x1f, 0x0000);
3873 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3875 static const struct phy_reg phy_reg_init[] = {
3882 /* Disable ALDPS before ram code */
3883 rtl_writephy(tp, 0x1f, 0x0000);
3884 rtl_writephy(tp, 0x18, 0x0310);
3887 rtl_apply_firmware(tp);
3889 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3890 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3892 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3895 static void rtl_hw_phy_config(struct net_device *dev)
3897 struct rtl8169_private *tp = netdev_priv(dev);
3899 rtl8169_print_mac_version(tp);
3901 switch (tp->mac_version) {
3902 case RTL_GIGA_MAC_VER_01:
3904 case RTL_GIGA_MAC_VER_02:
3905 case RTL_GIGA_MAC_VER_03:
3906 rtl8169s_hw_phy_config(tp);
3908 case RTL_GIGA_MAC_VER_04:
3909 rtl8169sb_hw_phy_config(tp);
3911 case RTL_GIGA_MAC_VER_05:
3912 rtl8169scd_hw_phy_config(tp);
3914 case RTL_GIGA_MAC_VER_06:
3915 rtl8169sce_hw_phy_config(tp);
3917 case RTL_GIGA_MAC_VER_07:
3918 case RTL_GIGA_MAC_VER_08:
3919 case RTL_GIGA_MAC_VER_09:
3920 rtl8102e_hw_phy_config(tp);
3922 case RTL_GIGA_MAC_VER_11:
3923 rtl8168bb_hw_phy_config(tp);
3925 case RTL_GIGA_MAC_VER_12:
3926 rtl8168bef_hw_phy_config(tp);
3928 case RTL_GIGA_MAC_VER_17:
3929 rtl8168bef_hw_phy_config(tp);
3931 case RTL_GIGA_MAC_VER_18:
3932 rtl8168cp_1_hw_phy_config(tp);
3934 case RTL_GIGA_MAC_VER_19:
3935 rtl8168c_1_hw_phy_config(tp);
3937 case RTL_GIGA_MAC_VER_20:
3938 rtl8168c_2_hw_phy_config(tp);
3940 case RTL_GIGA_MAC_VER_21:
3941 rtl8168c_3_hw_phy_config(tp);
3943 case RTL_GIGA_MAC_VER_22:
3944 rtl8168c_4_hw_phy_config(tp);
3946 case RTL_GIGA_MAC_VER_23:
3947 case RTL_GIGA_MAC_VER_24:
3948 rtl8168cp_2_hw_phy_config(tp);
3950 case RTL_GIGA_MAC_VER_25:
3951 rtl8168d_1_hw_phy_config(tp);
3953 case RTL_GIGA_MAC_VER_26:
3954 rtl8168d_2_hw_phy_config(tp);
3956 case RTL_GIGA_MAC_VER_27:
3957 rtl8168d_3_hw_phy_config(tp);
3959 case RTL_GIGA_MAC_VER_28:
3960 rtl8168d_4_hw_phy_config(tp);
3962 case RTL_GIGA_MAC_VER_29:
3963 case RTL_GIGA_MAC_VER_30:
3964 rtl8105e_hw_phy_config(tp);
3966 case RTL_GIGA_MAC_VER_31:
3969 case RTL_GIGA_MAC_VER_32:
3970 case RTL_GIGA_MAC_VER_33:
3971 rtl8168e_1_hw_phy_config(tp);
3973 case RTL_GIGA_MAC_VER_34:
3974 rtl8168e_2_hw_phy_config(tp);
3976 case RTL_GIGA_MAC_VER_35:
3977 rtl8168f_1_hw_phy_config(tp);
3979 case RTL_GIGA_MAC_VER_36:
3980 rtl8168f_2_hw_phy_config(tp);
3983 case RTL_GIGA_MAC_VER_37:
3984 rtl8402_hw_phy_config(tp);
3987 case RTL_GIGA_MAC_VER_38:
3988 rtl8411_hw_phy_config(tp);
3991 case RTL_GIGA_MAC_VER_39:
3992 rtl8106e_hw_phy_config(tp);
3995 case RTL_GIGA_MAC_VER_40:
3996 rtl8168g_1_hw_phy_config(tp);
3998 case RTL_GIGA_MAC_VER_42:
3999 case RTL_GIGA_MAC_VER_43:
4000 case RTL_GIGA_MAC_VER_44:
4001 rtl8168g_2_hw_phy_config(tp);
4003 case RTL_GIGA_MAC_VER_45:
4004 case RTL_GIGA_MAC_VER_47:
4005 rtl8168h_1_hw_phy_config(tp);
4007 case RTL_GIGA_MAC_VER_46:
4008 case RTL_GIGA_MAC_VER_48:
4009 rtl8168h_2_hw_phy_config(tp);
4012 case RTL_GIGA_MAC_VER_49:
4013 rtl8168ep_1_hw_phy_config(tp);
4015 case RTL_GIGA_MAC_VER_50:
4016 case RTL_GIGA_MAC_VER_51:
4017 rtl8168ep_2_hw_phy_config(tp);
4020 case RTL_GIGA_MAC_VER_41:
4026 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4028 if (!test_and_set_bit(flag, tp->wk.flags))
4029 schedule_work(&tp->wk.work);
4032 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4034 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4035 (RTL_R8(tp, PHYstatus) & TBI_Enable);
4038 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4040 rtl_hw_phy_config(dev);
4042 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4043 netif_dbg(tp, drv, dev,
4044 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4045 RTL_W8(tp, 0x82, 0x01);
4048 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4050 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4051 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4053 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
4054 netif_dbg(tp, drv, dev,
4055 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4056 RTL_W8(tp, 0x82, 0x01);
4057 netif_dbg(tp, drv, dev,
4058 "Set PHY Reg 0x0bh = 0x00h\n");
4059 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
4062 /* We may have called phy_speed_down before */
4063 phy_speed_up(dev->phydev);
4065 genphy_soft_reset(dev->phydev);
4067 /* It was reported that several chips end up with 10MBit/Half on a
4068 * 1GBit link after resuming from S3. For whatever reason the PHY on
4069 * these chips doesn't properly start a renegotiation when soft-reset.
4070 * Explicitly requesting a renegotiation fixes this.
4072 if (dev->phydev->autoneg == AUTONEG_ENABLE)
4073 phy_restart_aneg(dev->phydev);
4076 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4080 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4082 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4085 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4088 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4089 rtl_rar_exgmac_set(tp, addr);
4091 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4093 rtl_unlock_work(tp);
4096 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4098 switch (tp->mac_version) {
4099 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4100 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
4101 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4103 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
4104 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
4105 case RTL_GIGA_MAC_VER_38:
4106 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4108 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4109 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4112 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
4117 static int rtl_set_mac_address(struct net_device *dev, void *p)
4119 struct rtl8169_private *tp = netdev_priv(dev);
4120 struct device *d = tp_to_dev(tp);
4123 ret = eth_mac_addr(dev, p);
4127 pm_runtime_get_noresume(d);
4129 if (pm_runtime_active(d))
4130 rtl_rar_set(tp, dev->dev_addr);
4132 pm_runtime_put_noidle(d);
4134 /* Reportedly at least Asus X453MA truncates packets otherwise */
4135 if (tp->mac_version == RTL_GIGA_MAC_VER_37)
4141 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4143 if (!netif_running(dev))
4146 return phy_mii_ioctl(dev->phydev, ifr, cmd);
4149 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4151 struct mdio_ops *ops = &tp->mdio_ops;
4153 switch (tp->mac_version) {
4154 case RTL_GIGA_MAC_VER_27:
4155 ops->write = r8168dp_1_mdio_write;
4156 ops->read = r8168dp_1_mdio_read;
4158 case RTL_GIGA_MAC_VER_28:
4159 case RTL_GIGA_MAC_VER_31:
4160 ops->write = r8168dp_2_mdio_write;
4161 ops->read = r8168dp_2_mdio_read;
4163 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4164 ops->write = r8168g_mdio_write;
4165 ops->read = r8168g_mdio_read;
4168 ops->write = r8169_mdio_write;
4169 ops->read = r8169_mdio_read;
4174 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4176 switch (tp->mac_version) {
4177 case RTL_GIGA_MAC_VER_25:
4178 case RTL_GIGA_MAC_VER_26:
4179 case RTL_GIGA_MAC_VER_29:
4180 case RTL_GIGA_MAC_VER_30:
4181 case RTL_GIGA_MAC_VER_32:
4182 case RTL_GIGA_MAC_VER_33:
4183 case RTL_GIGA_MAC_VER_34:
4184 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
4185 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
4186 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4193 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4195 struct phy_device *phydev;
4197 if (!__rtl8169_get_wol(tp))
4200 /* phydev may not be attached to netdevice */
4201 phydev = mdiobus_get_phy(tp->mii_bus, 0);
4203 phy_speed_down(phydev, false);
4204 rtl_wol_suspend_quirk(tp);
4209 static void r8168_pll_power_down(struct rtl8169_private *tp)
4211 if (r8168_check_dash(tp))
4214 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4215 tp->mac_version == RTL_GIGA_MAC_VER_33)
4216 rtl_ephy_write(tp, 0x19, 0xff64);
4218 if (rtl_wol_pll_power_down(tp))
4221 switch (tp->mac_version) {
4222 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_26:
4223 case RTL_GIGA_MAC_VER_29 ... RTL_GIGA_MAC_VER_30:
4224 case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_33:
4225 case RTL_GIGA_MAC_VER_37:
4226 case RTL_GIGA_MAC_VER_39:
4227 case RTL_GIGA_MAC_VER_43:
4228 case RTL_GIGA_MAC_VER_44:
4229 case RTL_GIGA_MAC_VER_45:
4230 case RTL_GIGA_MAC_VER_46:
4231 case RTL_GIGA_MAC_VER_47:
4232 case RTL_GIGA_MAC_VER_48:
4233 case RTL_GIGA_MAC_VER_50:
4234 case RTL_GIGA_MAC_VER_51:
4235 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4237 case RTL_GIGA_MAC_VER_40:
4238 case RTL_GIGA_MAC_VER_41:
4239 case RTL_GIGA_MAC_VER_49:
4240 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4241 0xfc000000, ERIAR_EXGMAC);
4242 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4247 static void r8168_pll_power_up(struct rtl8169_private *tp)
4249 switch (tp->mac_version) {
4250 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_26:
4251 case RTL_GIGA_MAC_VER_29 ... RTL_GIGA_MAC_VER_30:
4252 case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_33:
4253 case RTL_GIGA_MAC_VER_37:
4254 case RTL_GIGA_MAC_VER_39:
4255 case RTL_GIGA_MAC_VER_43:
4256 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4258 case RTL_GIGA_MAC_VER_44:
4259 case RTL_GIGA_MAC_VER_45:
4260 case RTL_GIGA_MAC_VER_46:
4261 case RTL_GIGA_MAC_VER_47:
4262 case RTL_GIGA_MAC_VER_48:
4263 case RTL_GIGA_MAC_VER_50:
4264 case RTL_GIGA_MAC_VER_51:
4265 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4267 case RTL_GIGA_MAC_VER_40:
4268 case RTL_GIGA_MAC_VER_41:
4269 case RTL_GIGA_MAC_VER_49:
4270 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4271 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4272 0x00000000, ERIAR_EXGMAC);
4276 phy_resume(tp->dev->phydev);
4277 /* give MAC/PHY some time to resume */
4281 static void rtl_pll_power_down(struct rtl8169_private *tp)
4283 switch (tp->mac_version) {
4284 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4285 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4288 r8168_pll_power_down(tp);
4292 static void rtl_pll_power_up(struct rtl8169_private *tp)
4294 switch (tp->mac_version) {
4295 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4296 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4299 r8168_pll_power_up(tp);
4303 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4305 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4308 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4310 if (tp->jumbo_ops.enable) {
4311 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4312 tp->jumbo_ops.enable(tp);
4313 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4317 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4319 if (tp->jumbo_ops.disable) {
4320 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4321 tp->jumbo_ops.disable(tp);
4322 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4326 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4328 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4329 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
4330 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4333 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4335 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4336 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
4337 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4340 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4342 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4345 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4347 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4350 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4352 RTL_W8(tp, MaxTxPacketSize, 0x24);
4353 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4354 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
4355 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4358 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4360 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4361 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4362 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
4363 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4366 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4368 rtl_tx_performance_tweak(tp,
4369 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4372 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4374 rtl_tx_performance_tweak(tp,
4375 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4378 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4380 r8168b_0_hw_jumbo_enable(tp);
4382 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
4385 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4387 r8168b_0_hw_jumbo_disable(tp);
4389 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4392 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4394 struct jumbo_ops *ops = &tp->jumbo_ops;
4396 switch (tp->mac_version) {
4397 case RTL_GIGA_MAC_VER_11:
4398 ops->disable = r8168b_0_hw_jumbo_disable;
4399 ops->enable = r8168b_0_hw_jumbo_enable;
4401 case RTL_GIGA_MAC_VER_12:
4402 case RTL_GIGA_MAC_VER_17:
4403 ops->disable = r8168b_1_hw_jumbo_disable;
4404 ops->enable = r8168b_1_hw_jumbo_enable;
4406 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4407 case RTL_GIGA_MAC_VER_19:
4408 case RTL_GIGA_MAC_VER_20:
4409 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4410 case RTL_GIGA_MAC_VER_22:
4411 case RTL_GIGA_MAC_VER_23:
4412 case RTL_GIGA_MAC_VER_24:
4413 case RTL_GIGA_MAC_VER_25:
4414 case RTL_GIGA_MAC_VER_26:
4415 ops->disable = r8168c_hw_jumbo_disable;
4416 ops->enable = r8168c_hw_jumbo_enable;
4418 case RTL_GIGA_MAC_VER_27:
4419 case RTL_GIGA_MAC_VER_28:
4420 ops->disable = r8168dp_hw_jumbo_disable;
4421 ops->enable = r8168dp_hw_jumbo_enable;
4423 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4424 case RTL_GIGA_MAC_VER_32:
4425 case RTL_GIGA_MAC_VER_33:
4426 case RTL_GIGA_MAC_VER_34:
4427 ops->disable = r8168e_hw_jumbo_disable;
4428 ops->enable = r8168e_hw_jumbo_enable;
4432 * No action needed for jumbo frames with 8169.
4433 * No jumbo for 810x at all.
4435 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4437 ops->disable = NULL;
4443 DECLARE_RTL_COND(rtl_chipcmd_cond)
4445 return RTL_R8(tp, ChipCmd) & CmdReset;
4448 static void rtl_hw_reset(struct rtl8169_private *tp)
4450 RTL_W8(tp, ChipCmd, CmdReset);
4452 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4455 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4457 struct rtl_fw *rtl_fw;
4461 name = rtl_lookup_firmware_name(tp);
4463 goto out_no_firmware;
4465 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4469 rc = reject_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
4473 rc = rtl_check_firmware(tp, rtl_fw);
4475 goto err_release_firmware;
4477 tp->rtl_fw = rtl_fw;
4481 err_release_firmware:
4482 release_firmware(rtl_fw->fw);
4486 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4493 static void rtl_request_firmware(struct rtl8169_private *tp)
4495 if (IS_ERR(tp->rtl_fw))
4496 rtl_request_uncached_firmware(tp);
4499 static void rtl_rx_close(struct rtl8169_private *tp)
4501 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4504 DECLARE_RTL_COND(rtl_npq_cond)
4506 return RTL_R8(tp, TxPoll) & NPQ;
4509 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4511 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
4514 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4516 /* Disable interrupts */
4517 rtl8169_irq_mask_and_ack(tp);
4521 switch (tp->mac_version) {
4522 case RTL_GIGA_MAC_VER_27:
4523 case RTL_GIGA_MAC_VER_28:
4524 case RTL_GIGA_MAC_VER_31:
4525 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4527 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4528 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4529 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4530 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4533 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4541 static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
4543 u32 val = TX_DMA_BURST << TxDMAShift |
4544 InterFrameGap << TxInterFrameGapShift;
4546 if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
4547 tp->mac_version != RTL_GIGA_MAC_VER_39)
4548 val |= TXCFG_AUTO_FIFO;
4550 RTL_W32(tp, TxConfig, val);
4553 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
4555 /* Low hurts. Let's disable the filtering. */
4556 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
4559 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
4562 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4563 * register to be written before TxDescAddrLow to work.
4564 * Switching from MMIO to I/O access fixes the issue as well.
4566 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4567 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4568 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4569 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4572 static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
4574 static const struct rtl_cfg2_info {
4579 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4580 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4581 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4582 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4584 const struct rtl_cfg2_info *p = cfg2_info;
4588 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
4589 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4590 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4591 RTL_W32(tp, 0x7c, p->val);
4597 static void rtl_set_rx_mode(struct net_device *dev)
4599 struct rtl8169_private *tp = netdev_priv(dev);
4600 u32 mc_filter[2]; /* Multicast hash filter */
4604 if (dev->flags & IFF_PROMISC) {
4605 /* Unconditionally log net taps. */
4606 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4608 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4610 mc_filter[1] = mc_filter[0] = 0xffffffff;
4611 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4612 (dev->flags & IFF_ALLMULTI)) {
4613 /* Too many to filter perfectly -- accept all multicasts. */
4614 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4615 mc_filter[1] = mc_filter[0] = 0xffffffff;
4617 struct netdev_hw_addr *ha;
4619 rx_mode = AcceptBroadcast | AcceptMyPhys;
4620 mc_filter[1] = mc_filter[0] = 0;
4621 netdev_for_each_mc_addr(ha, dev) {
4622 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4623 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4624 rx_mode |= AcceptMulticast;
4628 if (dev->features & NETIF_F_RXALL)
4629 rx_mode |= (AcceptErr | AcceptRunt);
4631 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4633 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4634 u32 data = mc_filter[0];
4636 mc_filter[0] = swab32(mc_filter[1]);
4637 mc_filter[1] = swab32(data);
4640 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4641 mc_filter[1] = mc_filter[0] = 0xffffffff;
4643 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4644 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
4646 RTL_W32(tp, RxConfig, tmp);
4649 static void rtl_hw_start(struct rtl8169_private *tp)
4651 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4655 rtl_set_rx_max_size(tp);
4656 rtl_set_rx_tx_desc_registers(tp);
4657 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4659 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4660 RTL_R8(tp, IntrMask);
4661 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
4663 rtl_set_tx_config_registers(tp);
4665 rtl_set_rx_mode(tp->dev);
4666 /* no early-rx interrupts */
4667 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4668 rtl_irq_enable_all(tp);
4671 static void rtl_hw_start_8169(struct rtl8169_private *tp)
4673 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4674 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4676 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
4678 tp->cp_cmd |= PCIMulRW;
4680 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4681 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4682 netif_dbg(tp, drv, tp->dev,
4683 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
4684 tp->cp_cmd |= (1 << 14);
4687 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4689 rtl8169_set_magic_reg(tp, tp->mac_version);
4692 * Undocumented corner. Supposedly:
4693 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4695 RTL_W16(tp, IntrMitigate, 0x0000);
4697 RTL_W32(tp, RxMissed, 0);
4700 DECLARE_RTL_COND(rtl_csiar_cond)
4702 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
4705 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4707 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4709 RTL_W32(tp, CSIDR, value);
4710 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4711 CSIAR_BYTE_ENABLE | func << 16);
4713 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4716 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4718 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4720 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4723 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4724 RTL_R32(tp, CSIDR) : ~0;
4727 static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
4729 struct pci_dev *pdev = tp->pci_dev;
4732 /* According to Realtek the value at config space address 0x070f
4733 * controls the L0s/L1 entrance latency. We try standard ECAM access
4734 * first and if it fails fall back to CSI.
4736 if (pdev->cfg_size > 0x070f &&
4737 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4740 netdev_notice_once(tp->dev,
4741 "No native access to PCI extended config space, falling back to CSI\n");
4742 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4743 rtl_csi_write(tp, 0x070c, csi | val << 24);
4746 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
4748 rtl_csi_access_enable(tp, 0x27);
4752 unsigned int offset;
4757 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4763 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4764 rtl_ephy_write(tp, e->offset, w);
4769 static void rtl_disable_clock_request(struct rtl8169_private *tp)
4771 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
4772 PCI_EXP_LNKCTL_CLKREQ_EN);
4775 static void rtl_enable_clock_request(struct rtl8169_private *tp)
4777 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
4778 PCI_EXP_LNKCTL_CLKREQ_EN);
4781 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4785 data = RTL_R8(tp, Config3);
4790 data &= ~Rdy_to_L23;
4792 RTL_W8(tp, Config3, data);
4795 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4798 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
4799 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
4801 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4802 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4808 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4810 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4812 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4813 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4815 if (tp->dev->mtu <= ETH_DATA_LEN) {
4816 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
4817 PCI_EXP_DEVCTL_NOSNOOP_EN);
4821 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4823 rtl_hw_start_8168bb(tp);
4825 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4827 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4830 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4832 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
4834 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4836 if (tp->dev->mtu <= ETH_DATA_LEN)
4837 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4839 rtl_disable_clock_request(tp);
4841 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4842 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4845 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4847 static const struct ephy_info e_info_8168cp[] = {
4848 { 0x01, 0, 0x0001 },
4849 { 0x02, 0x0800, 0x1000 },
4850 { 0x03, 0, 0x0042 },
4851 { 0x06, 0x0080, 0x0000 },
4855 rtl_set_def_aspm_entry_latency(tp);
4857 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4859 __rtl_hw_start_8168cp(tp);
4862 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4864 rtl_set_def_aspm_entry_latency(tp);
4866 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4868 if (tp->dev->mtu <= ETH_DATA_LEN)
4869 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4871 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4872 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4875 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4877 rtl_set_def_aspm_entry_latency(tp);
4879 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4882 RTL_W8(tp, DBG_REG, 0x20);
4884 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4886 if (tp->dev->mtu <= ETH_DATA_LEN)
4887 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4889 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4890 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4893 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4895 static const struct ephy_info e_info_8168c_1[] = {
4896 { 0x02, 0x0800, 0x1000 },
4897 { 0x03, 0, 0x0002 },
4898 { 0x06, 0x0080, 0x0000 }
4901 rtl_set_def_aspm_entry_latency(tp);
4903 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4905 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4907 __rtl_hw_start_8168cp(tp);
4910 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4912 static const struct ephy_info e_info_8168c_2[] = {
4913 { 0x01, 0, 0x0001 },
4914 { 0x03, 0x0400, 0x0220 }
4917 rtl_set_def_aspm_entry_latency(tp);
4919 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4921 __rtl_hw_start_8168cp(tp);
4924 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4926 rtl_hw_start_8168c_2(tp);
4929 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4931 rtl_set_def_aspm_entry_latency(tp);
4933 __rtl_hw_start_8168cp(tp);
4936 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4938 rtl_set_def_aspm_entry_latency(tp);
4940 rtl_disable_clock_request(tp);
4942 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4944 if (tp->dev->mtu <= ETH_DATA_LEN)
4945 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4947 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4948 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4951 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
4953 rtl_set_def_aspm_entry_latency(tp);
4955 if (tp->dev->mtu <= ETH_DATA_LEN)
4956 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4958 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4960 rtl_disable_clock_request(tp);
4963 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
4965 static const struct ephy_info e_info_8168d_4[] = {
4966 { 0x0b, 0x0000, 0x0048 },
4967 { 0x19, 0x0020, 0x0050 },
4968 { 0x0c, 0x0100, 0x0020 }
4971 rtl_set_def_aspm_entry_latency(tp);
4973 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4975 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4977 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
4979 rtl_enable_clock_request(tp);
4982 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
4984 static const struct ephy_info e_info_8168e_1[] = {
4985 { 0x00, 0x0200, 0x0100 },
4986 { 0x00, 0x0000, 0x0004 },
4987 { 0x06, 0x0002, 0x0001 },
4988 { 0x06, 0x0000, 0x0030 },
4989 { 0x07, 0x0000, 0x2000 },
4990 { 0x00, 0x0000, 0x0020 },
4991 { 0x03, 0x5800, 0x2000 },
4992 { 0x03, 0x0000, 0x0001 },
4993 { 0x01, 0x0800, 0x1000 },
4994 { 0x07, 0x0000, 0x4000 },
4995 { 0x1e, 0x0000, 0x2000 },
4996 { 0x19, 0xffff, 0xfe6c },
4997 { 0x0a, 0x0000, 0x0040 }
5000 rtl_set_def_aspm_entry_latency(tp);
5002 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5004 if (tp->dev->mtu <= ETH_DATA_LEN)
5005 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5007 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5009 rtl_disable_clock_request(tp);
5011 /* Reset tx FIFO pointer */
5012 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5013 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
5015 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5018 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5020 static const struct ephy_info e_info_8168e_2[] = {
5021 { 0x09, 0x0000, 0x0080 },
5022 { 0x19, 0x0000, 0x0224 }
5025 rtl_set_def_aspm_entry_latency(tp);
5027 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5029 if (tp->dev->mtu <= ETH_DATA_LEN)
5030 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5032 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5033 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5034 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5035 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5036 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5037 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5038 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5039 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5041 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5043 rtl_disable_clock_request(tp);
5045 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5047 /* Adjust EEE LED frequency */
5048 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5050 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5051 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5052 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5054 rtl_hw_aspm_clkreq_enable(tp, true);
5057 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5059 rtl_set_def_aspm_entry_latency(tp);
5061 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5063 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5064 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5065 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5066 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5067 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5068 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5069 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5070 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5071 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5072 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5074 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5076 rtl_disable_clock_request(tp);
5078 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5079 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5080 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5081 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5084 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5086 static const struct ephy_info e_info_8168f_1[] = {
5087 { 0x06, 0x00c0, 0x0020 },
5088 { 0x08, 0x0001, 0x0002 },
5089 { 0x09, 0x0000, 0x0080 },
5090 { 0x19, 0x0000, 0x0224 }
5093 rtl_hw_start_8168f(tp);
5095 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5097 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5099 /* Adjust EEE LED frequency */
5100 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5103 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5105 static const struct ephy_info e_info_8168f_1[] = {
5106 { 0x06, 0x00c0, 0x0020 },
5107 { 0x0f, 0xffff, 0x5200 },
5108 { 0x1e, 0x0000, 0x4000 },
5109 { 0x19, 0x0000, 0x0224 }
5112 rtl_hw_start_8168f(tp);
5113 rtl_pcie_state_l2l3_enable(tp, false);
5115 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5117 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5120 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
5122 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5123 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5124 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5125 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5127 rtl_set_def_aspm_entry_latency(tp);
5129 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5131 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5132 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5133 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
5135 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5136 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5138 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5139 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5141 /* Adjust EEE LED frequency */
5142 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5144 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5145 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5147 rtl_pcie_state_l2l3_enable(tp, false);
5150 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5152 static const struct ephy_info e_info_8168g_1[] = {
5153 { 0x00, 0x0000, 0x0008 },
5154 { 0x0c, 0x37d0, 0x0820 },
5155 { 0x1e, 0x0000, 0x0001 },
5156 { 0x19, 0x8000, 0x0000 }
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_8168g_1, ARRAY_SIZE(e_info_8168g_1));
5164 rtl_hw_aspm_clkreq_enable(tp, true);
5167 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5169 static const struct ephy_info e_info_8168g_2[] = {
5170 { 0x00, 0x0000, 0x0008 },
5171 { 0x0c, 0x3df0, 0x0200 },
5172 { 0x19, 0xffff, 0xfc00 },
5173 { 0x1e, 0xffff, 0x20eb }
5176 rtl_hw_start_8168g(tp);
5178 /* disable aspm and clock request before access ephy */
5179 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5180 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5181 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5184 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5186 static const struct ephy_info e_info_8411_2[] = {
5187 { 0x00, 0x0000, 0x0008 },
5188 { 0x0c, 0x3df0, 0x0200 },
5189 { 0x0f, 0xffff, 0x5200 },
5190 { 0x19, 0x0020, 0x0000 },
5191 { 0x1e, 0x0000, 0x2000 }
5194 rtl_hw_start_8168g(tp);
5196 /* disable aspm and clock request before access ephy */
5197 rtl_hw_aspm_clkreq_enable(tp, false);
5198 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5200 /* The following Realtek-provided magic fixes an issue with the RX unit
5201 * getting confused after the PHY having been powered-down.
5203 r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
5204 r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
5205 r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
5206 r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
5207 r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
5208 r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
5209 r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
5210 r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
5212 r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
5214 r8168_mac_ocp_write(tp, 0xF800, 0xE008);
5215 r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
5216 r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
5217 r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
5218 r8168_mac_ocp_write(tp, 0xF808, 0xE027);
5219 r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
5220 r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
5221 r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
5222 r8168_mac_ocp_write(tp, 0xF810, 0xC602);
5223 r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
5224 r8168_mac_ocp_write(tp, 0xF814, 0x0000);
5225 r8168_mac_ocp_write(tp, 0xF816, 0xC502);
5226 r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
5227 r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
5228 r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
5229 r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
5230 r8168_mac_ocp_write(tp, 0xF820, 0x080A);
5231 r8168_mac_ocp_write(tp, 0xF822, 0x6420);
5232 r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
5233 r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
5234 r8168_mac_ocp_write(tp, 0xF828, 0xC516);
5235 r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
5236 r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
5237 r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
5238 r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
5239 r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
5240 r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
5241 r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
5242 r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
5243 r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
5244 r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
5245 r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
5246 r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
5247 r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
5248 r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
5249 r8168_mac_ocp_write(tp, 0xF846, 0xC404);
5250 r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
5251 r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
5252 r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
5253 r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
5254 r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
5255 r8168_mac_ocp_write(tp, 0xF852, 0xE434);
5256 r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
5257 r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
5258 r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
5259 r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
5260 r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
5261 r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
5262 r8168_mac_ocp_write(tp, 0xF860, 0xF007);
5263 r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
5264 r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
5265 r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
5266 r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
5267 r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
5268 r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
5269 r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
5270 r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
5271 r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
5272 r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
5273 r8168_mac_ocp_write(tp, 0xF876, 0xC516);
5274 r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
5275 r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
5276 r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
5277 r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
5278 r8168_mac_ocp_write(tp, 0xF880, 0xC512);
5279 r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
5280 r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
5281 r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
5282 r8168_mac_ocp_write(tp, 0xF888, 0x483F);
5283 r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
5284 r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
5285 r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
5286 r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
5287 r8168_mac_ocp_write(tp, 0xF892, 0xC505);
5288 r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
5289 r8168_mac_ocp_write(tp, 0xF896, 0xC502);
5290 r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
5291 r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
5292 r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
5293 r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
5294 r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
5295 r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
5296 r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
5297 r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
5298 r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
5299 r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
5300 r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
5301 r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
5302 r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
5303 r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
5304 r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
5305 r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
5306 r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
5307 r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
5308 r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
5309 r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
5310 r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
5311 r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
5312 r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
5313 r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
5314 r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
5315 r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
5316 r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
5317 r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
5318 r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
5319 r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
5320 r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
5321 r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
5322 r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
5323 r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
5324 r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
5326 r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
5328 r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
5329 r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
5330 r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
5331 r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
5332 r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
5333 r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
5334 r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
5336 rtl_hw_aspm_clkreq_enable(tp, true);
5339 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5343 static const struct ephy_info e_info_8168h_1[] = {
5344 { 0x1e, 0x0800, 0x0001 },
5345 { 0x1d, 0x0000, 0x0800 },
5346 { 0x05, 0xffff, 0x2089 },
5347 { 0x06, 0xffff, 0x5881 },
5348 { 0x04, 0xffff, 0x154a },
5349 { 0x01, 0xffff, 0x068b }
5352 /* disable aspm and clock request before access ephy */
5353 rtl_hw_aspm_clkreq_enable(tp, false);
5354 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5356 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5357 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5358 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5359 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5361 rtl_set_def_aspm_entry_latency(tp);
5363 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5365 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5366 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5368 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
5370 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
5372 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5374 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5375 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5377 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5378 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5380 /* Adjust EEE LED frequency */
5381 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5383 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5384 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5386 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5388 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5390 rtl_pcie_state_l2l3_enable(tp, false);
5392 rtl_writephy(tp, 0x1f, 0x0c42);
5393 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
5394 rtl_writephy(tp, 0x1f, 0x0000);
5395 if (rg_saw_cnt > 0) {
5398 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5399 sw_cnt_1ms_ini &= 0x0fff;
5400 data = r8168_mac_ocp_read(tp, 0xd412);
5402 data |= sw_cnt_1ms_ini;
5403 r8168_mac_ocp_write(tp, 0xd412, data);
5406 data = r8168_mac_ocp_read(tp, 0xe056);
5409 r8168_mac_ocp_write(tp, 0xe056, data);
5411 data = r8168_mac_ocp_read(tp, 0xe052);
5414 r8168_mac_ocp_write(tp, 0xe052, data);
5416 data = r8168_mac_ocp_read(tp, 0xe0d6);
5419 r8168_mac_ocp_write(tp, 0xe0d6, data);
5421 data = r8168_mac_ocp_read(tp, 0xd420);
5424 r8168_mac_ocp_write(tp, 0xd420, data);
5426 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5427 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5428 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5429 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
5431 rtl_hw_aspm_clkreq_enable(tp, true);
5434 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5436 rtl8168ep_stop_cmac(tp);
5438 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5439 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5440 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5441 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5443 rtl_set_def_aspm_entry_latency(tp);
5445 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5447 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5448 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5450 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5452 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5454 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5455 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5457 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5458 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5460 /* Adjust EEE LED frequency */
5461 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5463 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5465 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5467 rtl_pcie_state_l2l3_enable(tp, false);
5470 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5472 static const struct ephy_info e_info_8168ep_1[] = {
5473 { 0x00, 0xffff, 0x10ab },
5474 { 0x06, 0xffff, 0xf030 },
5475 { 0x08, 0xffff, 0x2006 },
5476 { 0x0d, 0xffff, 0x1666 },
5477 { 0x0c, 0x3ff0, 0x0000 }
5480 /* disable aspm and clock request before access ephy */
5481 rtl_hw_aspm_clkreq_enable(tp, false);
5482 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5484 rtl_hw_start_8168ep(tp);
5486 rtl_hw_aspm_clkreq_enable(tp, true);
5489 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5491 static const struct ephy_info e_info_8168ep_2[] = {
5492 { 0x00, 0xffff, 0x10a3 },
5493 { 0x19, 0xffff, 0xfc00 },
5494 { 0x1e, 0xffff, 0x20ea }
5497 /* disable aspm and clock request before access ephy */
5498 rtl_hw_aspm_clkreq_enable(tp, false);
5499 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5501 rtl_hw_start_8168ep(tp);
5503 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5504 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5506 rtl_hw_aspm_clkreq_enable(tp, true);
5509 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5512 static const struct ephy_info e_info_8168ep_3[] = {
5513 { 0x00, 0xffff, 0x10a3 },
5514 { 0x19, 0xffff, 0x7c00 },
5515 { 0x1e, 0xffff, 0x20eb },
5516 { 0x0d, 0xffff, 0x1666 }
5519 /* disable aspm and clock request before access ephy */
5520 rtl_hw_aspm_clkreq_enable(tp, false);
5521 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5523 rtl_hw_start_8168ep(tp);
5525 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5526 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5528 data = r8168_mac_ocp_read(tp, 0xd3e2);
5531 r8168_mac_ocp_write(tp, 0xd3e2, data);
5533 data = r8168_mac_ocp_read(tp, 0xd3e4);
5535 r8168_mac_ocp_write(tp, 0xd3e4, data);
5537 data = r8168_mac_ocp_read(tp, 0xe860);
5539 r8168_mac_ocp_write(tp, 0xe860, data);
5541 rtl_hw_aspm_clkreq_enable(tp, true);
5544 static void rtl_hw_start_8168(struct rtl8169_private *tp)
5546 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5548 tp->cp_cmd &= ~INTT_MASK;
5549 tp->cp_cmd |= PktCntrDisable | INTT_1;
5550 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5552 RTL_W16(tp, IntrMitigate, 0x5100);
5554 /* Work around for RxFIFO overflow. */
5555 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5556 tp->event_slow |= RxFIFOOver | PCSTimeout;
5557 tp->event_slow &= ~RxOverflow;
5560 switch (tp->mac_version) {
5561 case RTL_GIGA_MAC_VER_11:
5562 rtl_hw_start_8168bb(tp);
5565 case RTL_GIGA_MAC_VER_12:
5566 case RTL_GIGA_MAC_VER_17:
5567 rtl_hw_start_8168bef(tp);
5570 case RTL_GIGA_MAC_VER_18:
5571 rtl_hw_start_8168cp_1(tp);
5574 case RTL_GIGA_MAC_VER_19:
5575 rtl_hw_start_8168c_1(tp);
5578 case RTL_GIGA_MAC_VER_20:
5579 rtl_hw_start_8168c_2(tp);
5582 case RTL_GIGA_MAC_VER_21:
5583 rtl_hw_start_8168c_3(tp);
5586 case RTL_GIGA_MAC_VER_22:
5587 rtl_hw_start_8168c_4(tp);
5590 case RTL_GIGA_MAC_VER_23:
5591 rtl_hw_start_8168cp_2(tp);
5594 case RTL_GIGA_MAC_VER_24:
5595 rtl_hw_start_8168cp_3(tp);
5598 case RTL_GIGA_MAC_VER_25:
5599 case RTL_GIGA_MAC_VER_26:
5600 case RTL_GIGA_MAC_VER_27:
5601 rtl_hw_start_8168d(tp);
5604 case RTL_GIGA_MAC_VER_28:
5605 rtl_hw_start_8168d_4(tp);
5608 case RTL_GIGA_MAC_VER_31:
5609 rtl_hw_start_8168dp(tp);
5612 case RTL_GIGA_MAC_VER_32:
5613 case RTL_GIGA_MAC_VER_33:
5614 rtl_hw_start_8168e_1(tp);
5616 case RTL_GIGA_MAC_VER_34:
5617 rtl_hw_start_8168e_2(tp);
5620 case RTL_GIGA_MAC_VER_35:
5621 case RTL_GIGA_MAC_VER_36:
5622 rtl_hw_start_8168f_1(tp);
5625 case RTL_GIGA_MAC_VER_38:
5626 rtl_hw_start_8411(tp);
5629 case RTL_GIGA_MAC_VER_40:
5630 case RTL_GIGA_MAC_VER_41:
5631 rtl_hw_start_8168g_1(tp);
5633 case RTL_GIGA_MAC_VER_42:
5634 rtl_hw_start_8168g_2(tp);
5637 case RTL_GIGA_MAC_VER_44:
5638 rtl_hw_start_8411_2(tp);
5641 case RTL_GIGA_MAC_VER_45:
5642 case RTL_GIGA_MAC_VER_46:
5643 rtl_hw_start_8168h_1(tp);
5646 case RTL_GIGA_MAC_VER_49:
5647 rtl_hw_start_8168ep_1(tp);
5650 case RTL_GIGA_MAC_VER_50:
5651 rtl_hw_start_8168ep_2(tp);
5654 case RTL_GIGA_MAC_VER_51:
5655 rtl_hw_start_8168ep_3(tp);
5659 netif_err(tp, drv, tp->dev,
5660 "unknown chipset (mac_version = %d)\n",
5666 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5668 static const struct ephy_info e_info_8102e_1[] = {
5669 { 0x01, 0, 0x6e65 },
5670 { 0x02, 0, 0x091f },
5671 { 0x03, 0, 0xc2f9 },
5672 { 0x06, 0, 0xafb5 },
5673 { 0x07, 0, 0x0e00 },
5674 { 0x19, 0, 0xec80 },
5675 { 0x01, 0, 0x2e65 },
5680 rtl_set_def_aspm_entry_latency(tp);
5682 RTL_W8(tp, DBG_REG, FIX_NAK_1);
5684 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5687 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5688 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5690 cfg1 = RTL_R8(tp, Config1);
5691 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5692 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
5694 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5697 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5699 rtl_set_def_aspm_entry_latency(tp);
5701 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5703 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5704 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5707 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5709 rtl_hw_start_8102e_2(tp);
5711 rtl_ephy_write(tp, 0x03, 0xc2f9);
5714 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5716 static const struct ephy_info e_info_8105e_1[] = {
5717 { 0x07, 0, 0x4000 },
5718 { 0x19, 0, 0x0200 },
5719 { 0x19, 0, 0x0020 },
5720 { 0x1e, 0, 0x2000 },
5721 { 0x03, 0, 0x0001 },
5722 { 0x19, 0, 0x0100 },
5723 { 0x19, 0, 0x0004 },
5727 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5728 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5730 /* Disable Early Tally Counter */
5731 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
5733 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5734 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5736 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5738 rtl_pcie_state_l2l3_enable(tp, false);
5741 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5743 rtl_hw_start_8105e_1(tp);
5744 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5747 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5749 static const struct ephy_info e_info_8402[] = {
5750 { 0x19, 0xffff, 0xff64 },
5754 rtl_set_def_aspm_entry_latency(tp);
5756 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5757 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5759 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5761 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
5763 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5765 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5766 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5767 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5768 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5769 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5770 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5771 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
5773 rtl_pcie_state_l2l3_enable(tp, false);
5776 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5778 rtl_hw_aspm_clkreq_enable(tp, false);
5780 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5781 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5783 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5784 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5785 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5787 rtl_pcie_state_l2l3_enable(tp, false);
5788 rtl_hw_aspm_clkreq_enable(tp, true);
5791 static void rtl_hw_start_8101(struct rtl8169_private *tp)
5793 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5794 tp->event_slow &= ~RxFIFOOver;
5796 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5797 tp->mac_version == RTL_GIGA_MAC_VER_16)
5798 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
5799 PCI_EXP_DEVCTL_NOSNOOP_EN);
5801 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5803 tp->cp_cmd &= CPCMD_QUIRK_MASK;
5804 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5806 switch (tp->mac_version) {
5807 case RTL_GIGA_MAC_VER_07:
5808 rtl_hw_start_8102e_1(tp);
5811 case RTL_GIGA_MAC_VER_08:
5812 rtl_hw_start_8102e_3(tp);
5815 case RTL_GIGA_MAC_VER_09:
5816 rtl_hw_start_8102e_2(tp);
5819 case RTL_GIGA_MAC_VER_29:
5820 rtl_hw_start_8105e_1(tp);
5822 case RTL_GIGA_MAC_VER_30:
5823 rtl_hw_start_8105e_2(tp);
5826 case RTL_GIGA_MAC_VER_37:
5827 rtl_hw_start_8402(tp);
5830 case RTL_GIGA_MAC_VER_39:
5831 rtl_hw_start_8106(tp);
5833 case RTL_GIGA_MAC_VER_43:
5834 rtl_hw_start_8168g_2(tp);
5836 case RTL_GIGA_MAC_VER_47:
5837 case RTL_GIGA_MAC_VER_48:
5838 rtl_hw_start_8168h_1(tp);
5842 RTL_W16(tp, IntrMitigate, 0x0000);
5845 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5847 struct rtl8169_private *tp = netdev_priv(dev);
5849 if (new_mtu > ETH_DATA_LEN)
5850 rtl_hw_jumbo_enable(tp);
5852 rtl_hw_jumbo_disable(tp);
5855 netdev_update_features(dev);
5860 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5862 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5863 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5866 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5867 void **data_buff, struct RxDesc *desc)
5869 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5870 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5874 rtl8169_make_unusable_by_asic(desc);
5877 static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
5879 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5881 /* Force memory writes to complete before releasing descriptor */
5884 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
5887 static inline void *rtl8169_align(void *data)
5889 return (void *)ALIGN((long)data, 16);
5892 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5893 struct RxDesc *desc)
5897 struct device *d = tp_to_dev(tp);
5898 int node = dev_to_node(d);
5900 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
5904 if (rtl8169_align(data) != data) {
5906 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
5911 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
5913 if (unlikely(dma_mapping_error(d, mapping))) {
5914 if (net_ratelimit())
5915 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5919 desc->addr = cpu_to_le64(mapping);
5920 rtl8169_mark_to_asic(desc);
5928 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5932 for (i = 0; i < NUM_RX_DESC; i++) {
5933 if (tp->Rx_databuff[i]) {
5934 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5935 tp->RxDescArray + i);
5940 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5942 desc->opts1 |= cpu_to_le32(RingEnd);
5945 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5949 for (i = 0; i < NUM_RX_DESC; i++) {
5952 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5954 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5957 tp->Rx_databuff[i] = data;
5960 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5964 rtl8169_rx_clear(tp);
5968 static int rtl8169_init_ring(struct rtl8169_private *tp)
5970 rtl8169_init_ring_indexes(tp);
5972 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5973 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
5975 return rtl8169_rx_fill(tp);
5978 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5979 struct TxDesc *desc)
5981 unsigned int len = tx_skb->len;
5983 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5991 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5996 for (i = 0; i < n; i++) {
5997 unsigned int entry = (start + i) % NUM_TX_DESC;
5998 struct ring_info *tx_skb = tp->tx_skb + entry;
5999 unsigned int len = tx_skb->len;
6002 struct sk_buff *skb = tx_skb->skb;
6004 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
6005 tp->TxDescArray + entry);
6007 dev_consume_skb_any(skb);
6014 static void rtl8169_tx_clear(struct rtl8169_private *tp)
6016 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
6017 tp->cur_tx = tp->dirty_tx = 0;
6020 static void rtl_reset_work(struct rtl8169_private *tp)
6022 struct net_device *dev = tp->dev;
6025 napi_disable(&tp->napi);
6026 netif_stop_queue(dev);
6027 synchronize_sched();
6029 rtl8169_hw_reset(tp);
6031 for (i = 0; i < NUM_RX_DESC; i++)
6032 rtl8169_mark_to_asic(tp->RxDescArray + i);
6034 rtl8169_tx_clear(tp);
6035 rtl8169_init_ring_indexes(tp);
6037 napi_enable(&tp->napi);
6039 netif_wake_queue(dev);
6042 static void rtl8169_tx_timeout(struct net_device *dev)
6044 struct rtl8169_private *tp = netdev_priv(dev);
6046 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6049 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
6052 struct skb_shared_info *info = skb_shinfo(skb);
6053 unsigned int cur_frag, entry;
6054 struct TxDesc *uninitialized_var(txd);
6055 struct device *d = tp_to_dev(tp);
6058 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
6059 const skb_frag_t *frag = info->frags + cur_frag;
6064 entry = (entry + 1) % NUM_TX_DESC;
6066 txd = tp->TxDescArray + entry;
6067 len = skb_frag_size(frag);
6068 addr = skb_frag_address(frag);
6069 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
6070 if (unlikely(dma_mapping_error(d, mapping))) {
6071 if (net_ratelimit())
6072 netif_err(tp, drv, tp->dev,
6073 "Failed to map TX fragments DMA!\n");
6077 /* Anti gcc 2.95.3 bugware (sic) */
6078 status = opts[0] | len |
6079 (RingEnd * !((entry + 1) % NUM_TX_DESC));
6081 txd->opts1 = cpu_to_le32(status);
6082 txd->opts2 = cpu_to_le32(opts[1]);
6083 txd->addr = cpu_to_le64(mapping);
6085 tp->tx_skb[entry].len = len;
6089 tp->tx_skb[entry].skb = skb;
6090 txd->opts1 |= cpu_to_le32(LastFrag);
6096 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6100 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6102 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6105 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6106 struct net_device *dev);
6107 /* r8169_csum_workaround()
6108 * The hw limites the value the transport offset. When the offset is out of the
6109 * range, calculate the checksum by sw.
6111 static void r8169_csum_workaround(struct rtl8169_private *tp,
6112 struct sk_buff *skb)
6114 if (skb_shinfo(skb)->gso_size) {
6115 netdev_features_t features = tp->dev->features;
6116 struct sk_buff *segs, *nskb;
6118 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6119 segs = skb_gso_segment(skb, features);
6120 if (IS_ERR(segs) || !segs)
6127 rtl8169_start_xmit(nskb, tp->dev);
6130 dev_consume_skb_any(skb);
6131 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6132 if (skb_checksum_help(skb) < 0)
6135 rtl8169_start_xmit(skb, tp->dev);
6137 struct net_device_stats *stats;
6140 stats = &tp->dev->stats;
6141 stats->tx_dropped++;
6142 dev_kfree_skb_any(skb);
6146 /* msdn_giant_send_check()
6147 * According to the document of microsoft, the TCP Pseudo Header excludes the
6148 * packet length for IPv6 TCP large packets.
6150 static int msdn_giant_send_check(struct sk_buff *skb)
6152 const struct ipv6hdr *ipv6h;
6156 ret = skb_cow_head(skb, 0);
6160 ipv6h = ipv6_hdr(skb);
6164 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6169 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6170 struct sk_buff *skb, u32 *opts)
6172 u32 mss = skb_shinfo(skb)->gso_size;
6176 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6177 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6178 const struct iphdr *ip = ip_hdr(skb);
6180 if (ip->protocol == IPPROTO_TCP)
6181 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6182 else if (ip->protocol == IPPROTO_UDP)
6183 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6191 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6192 struct sk_buff *skb, u32 *opts)
6194 u32 transport_offset = (u32)skb_transport_offset(skb);
6195 u32 mss = skb_shinfo(skb)->gso_size;
6198 if (transport_offset > GTTCPHO_MAX) {
6199 netif_warn(tp, tx_err, tp->dev,
6200 "Invalid transport offset 0x%x for TSO\n",
6205 switch (vlan_get_protocol(skb)) {
6206 case htons(ETH_P_IP):
6207 opts[0] |= TD1_GTSENV4;
6210 case htons(ETH_P_IPV6):
6211 if (msdn_giant_send_check(skb))
6214 opts[0] |= TD1_GTSENV6;
6222 opts[0] |= transport_offset << GTTCPHO_SHIFT;
6223 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
6224 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6227 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6228 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
6230 if (transport_offset > TCPHO_MAX) {
6231 netif_warn(tp, tx_err, tp->dev,
6232 "Invalid transport offset 0x%x\n",
6237 switch (vlan_get_protocol(skb)) {
6238 case htons(ETH_P_IP):
6239 opts[1] |= TD1_IPv4_CS;
6240 ip_protocol = ip_hdr(skb)->protocol;
6243 case htons(ETH_P_IPV6):
6244 opts[1] |= TD1_IPv6_CS;
6245 ip_protocol = ipv6_hdr(skb)->nexthdr;
6249 ip_protocol = IPPROTO_RAW;
6253 if (ip_protocol == IPPROTO_TCP)
6254 opts[1] |= TD1_TCP_CS;
6255 else if (ip_protocol == IPPROTO_UDP)
6256 opts[1] |= TD1_UDP_CS;
6260 opts[1] |= transport_offset << TCPHO_SHIFT;
6262 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6263 /* eth_skb_pad would free the skb on error */
6264 return !__skb_put_padto(skb, ETH_ZLEN, false);
6270 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6271 struct net_device *dev)
6273 struct rtl8169_private *tp = netdev_priv(dev);
6274 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
6275 struct TxDesc *txd = tp->TxDescArray + entry;
6276 struct device *d = tp_to_dev(tp);
6282 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
6283 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
6287 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
6290 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6293 if (!tp->tso_csum(tp, skb, opts)) {
6294 r8169_csum_workaround(tp, skb);
6295 return NETDEV_TX_OK;
6298 len = skb_headlen(skb);
6299 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
6300 if (unlikely(dma_mapping_error(d, mapping))) {
6301 if (net_ratelimit())
6302 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
6306 tp->tx_skb[entry].len = len;
6307 txd->addr = cpu_to_le64(mapping);
6309 frags = rtl8169_xmit_frags(tp, skb, opts);
6313 opts[0] |= FirstFrag;
6315 opts[0] |= FirstFrag | LastFrag;
6316 tp->tx_skb[entry].skb = skb;
6319 txd->opts2 = cpu_to_le32(opts[1]);
6321 skb_tx_timestamp(skb);
6323 /* Force memory writes to complete before releasing descriptor */
6326 /* Anti gcc 2.95.3 bugware (sic) */
6327 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
6328 txd->opts1 = cpu_to_le32(status);
6330 /* Force all memory writes to complete before notifying device */
6333 tp->cur_tx += frags + 1;
6335 RTL_W8(tp, TxPoll, NPQ);
6339 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6340 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6341 * not miss a ring update when it notices a stopped queue.
6344 netif_stop_queue(dev);
6345 /* Sync with rtl_tx:
6346 * - publish queue status and cur_tx ring index (write barrier)
6347 * - refresh dirty_tx ring index (read barrier).
6348 * May the current thread have a pessimistic view of the ring
6349 * status and forget to wake up queue, a racing rtl_tx thread
6353 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
6354 netif_wake_queue(dev);
6357 return NETDEV_TX_OK;
6360 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
6362 dev_kfree_skb_any(skb);
6363 dev->stats.tx_dropped++;
6364 return NETDEV_TX_OK;
6367 netif_stop_queue(dev);
6368 dev->stats.tx_dropped++;
6369 return NETDEV_TX_BUSY;
6372 static void rtl8169_pcierr_interrupt(struct net_device *dev)
6374 struct rtl8169_private *tp = netdev_priv(dev);
6375 struct pci_dev *pdev = tp->pci_dev;
6376 u16 pci_status, pci_cmd;
6378 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6379 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6381 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6382 pci_cmd, pci_status);
6385 * The recovery sequence below admits a very elaborated explanation:
6386 * - it seems to work;
6387 * - I did not see what else could be done;
6388 * - it makes iop3xx happy.
6390 * Feel free to adjust to your needs.
6392 if (pdev->broken_parity_status)
6393 pci_cmd &= ~PCI_COMMAND_PARITY;
6395 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6397 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
6399 pci_write_config_word(pdev, PCI_STATUS,
6400 pci_status & (PCI_STATUS_DETECTED_PARITY |
6401 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6402 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6404 /* The infamous DAC f*ckup only happens at boot time */
6405 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
6406 netif_info(tp, intr, dev, "disabling PCI DAC\n");
6407 tp->cp_cmd &= ~PCIDAC;
6408 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
6409 dev->features &= ~NETIF_F_HIGHDMA;
6412 rtl8169_hw_reset(tp);
6414 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6417 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
6419 unsigned int dirty_tx, tx_left;
6421 dirty_tx = tp->dirty_tx;
6423 tx_left = tp->cur_tx - dirty_tx;
6425 while (tx_left > 0) {
6426 unsigned int entry = dirty_tx % NUM_TX_DESC;
6427 struct ring_info *tx_skb = tp->tx_skb + entry;
6430 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6431 if (status & DescOwn)
6434 /* This barrier is needed to keep us from reading
6435 * any other fields out of the Tx descriptor until
6436 * we know the status of DescOwn
6440 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
6441 tp->TxDescArray + entry);
6442 if (status & LastFrag) {
6443 u64_stats_update_begin(&tp->tx_stats.syncp);
6444 tp->tx_stats.packets++;
6445 tp->tx_stats.bytes += tx_skb->skb->len;
6446 u64_stats_update_end(&tp->tx_stats.syncp);
6447 dev_consume_skb_any(tx_skb->skb);
6454 if (tp->dirty_tx != dirty_tx) {
6455 tp->dirty_tx = dirty_tx;
6456 /* Sync with rtl8169_start_xmit:
6457 * - publish dirty_tx ring index (write barrier)
6458 * - refresh cur_tx ring index and queue status (read barrier)
6459 * May the current thread miss the stopped queue condition,
6460 * a racing xmit thread can only have a right view of the
6464 if (netif_queue_stopped(dev) &&
6465 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6466 netif_wake_queue(dev);
6469 * 8168 hack: TxPoll requests are lost when the Tx packets are
6470 * too close. Let's kick an extra TxPoll request when a burst
6471 * of start_xmit activity is detected (if it is not detected,
6472 * it is slow enough). -- FR
6474 if (tp->cur_tx != dirty_tx)
6475 RTL_W8(tp, TxPoll, NPQ);
6479 static inline int rtl8169_fragmented_frame(u32 status)
6481 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6484 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
6486 u32 status = opts1 & RxProtoMask;
6488 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
6489 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
6490 skb->ip_summed = CHECKSUM_UNNECESSARY;
6492 skb_checksum_none_assert(skb);
6495 static struct sk_buff *rtl8169_try_rx_copy(void *data,
6496 struct rtl8169_private *tp,
6500 struct sk_buff *skb;
6501 struct device *d = tp_to_dev(tp);
6503 data = rtl8169_align(data);
6504 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6506 skb = napi_alloc_skb(&tp->napi, pkt_size);
6508 skb_copy_to_linear_data(skb, data, pkt_size);
6509 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6514 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
6516 unsigned int cur_rx, rx_left;
6519 cur_rx = tp->cur_rx;
6521 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
6522 unsigned int entry = cur_rx % NUM_RX_DESC;
6523 struct RxDesc *desc = tp->RxDescArray + entry;
6526 status = le32_to_cpu(desc->opts1);
6527 if (status & DescOwn)
6530 /* This barrier is needed to keep us from reading
6531 * any other fields out of the Rx descriptor until
6532 * we know the status of DescOwn
6536 if (unlikely(status & RxRES)) {
6537 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6539 dev->stats.rx_errors++;
6540 if (status & (RxRWT | RxRUNT))
6541 dev->stats.rx_length_errors++;
6543 dev->stats.rx_crc_errors++;
6544 /* RxFOVF is a reserved bit on later chip versions */
6545 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6547 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6548 dev->stats.rx_fifo_errors++;
6549 } else if (status & (RxRUNT | RxCRC) &&
6550 !(status & RxRWT) &&
6551 dev->features & NETIF_F_RXALL) {
6555 struct sk_buff *skb;
6560 addr = le64_to_cpu(desc->addr);
6561 if (likely(!(dev->features & NETIF_F_RXFCS)))
6562 pkt_size = (status & 0x00003fff) - 4;
6564 pkt_size = status & 0x00003fff;
6567 * The driver does not support incoming fragmented
6568 * frames. They are seen as a symptom of over-mtu
6571 if (unlikely(rtl8169_fragmented_frame(status))) {
6572 dev->stats.rx_dropped++;
6573 dev->stats.rx_length_errors++;
6574 goto release_descriptor;
6577 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6578 tp, pkt_size, addr);
6580 dev->stats.rx_dropped++;
6581 goto release_descriptor;
6584 rtl8169_rx_csum(skb, status);
6585 skb_put(skb, pkt_size);
6586 skb->protocol = eth_type_trans(skb, dev);
6588 rtl8169_rx_vlan_tag(desc, skb);
6590 if (skb->pkt_type == PACKET_MULTICAST)
6591 dev->stats.multicast++;
6593 napi_gro_receive(&tp->napi, skb);
6595 u64_stats_update_begin(&tp->rx_stats.syncp);
6596 tp->rx_stats.packets++;
6597 tp->rx_stats.bytes += pkt_size;
6598 u64_stats_update_end(&tp->rx_stats.syncp);
6602 rtl8169_mark_to_asic(desc);
6605 count = cur_rx - tp->cur_rx;
6606 tp->cur_rx = cur_rx;
6611 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6613 struct rtl8169_private *tp = dev_instance;
6614 u16 status = rtl_get_events(tp);
6616 if (status == 0xffff || !(status & (RTL_EVENT_NAPI | tp->event_slow)))
6619 rtl_irq_disable(tp);
6620 napi_schedule(&tp->napi);
6626 * Workqueue context.
6628 static void rtl_slow_event_work(struct rtl8169_private *tp)
6630 struct net_device *dev = tp->dev;
6633 status = rtl_get_events(tp) & tp->event_slow;
6634 rtl_ack_events(tp, status);
6636 if (unlikely(status & RxFIFOOver)) {
6637 switch (tp->mac_version) {
6638 /* Work around for rx fifo overflow */
6639 case RTL_GIGA_MAC_VER_11:
6640 netif_stop_queue(dev);
6641 /* XXX - Hack alert. See rtl_task(). */
6642 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6648 if (unlikely(status & SYSErr))
6649 rtl8169_pcierr_interrupt(dev);
6651 if (status & LinkChg)
6652 phy_mac_interrupt(dev->phydev);
6654 rtl_irq_enable_all(tp);
6657 static void rtl_task(struct work_struct *work)
6659 static const struct {
6661 void (*action)(struct rtl8169_private *);
6663 /* XXX - keep rtl_slow_event_work() as first element. */
6664 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6665 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6667 struct rtl8169_private *tp =
6668 container_of(work, struct rtl8169_private, wk.work);
6669 struct net_device *dev = tp->dev;
6674 if (!netif_running(dev) ||
6675 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6678 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6681 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6683 rtl_work[i].action(tp);
6687 rtl_unlock_work(tp);
6690 static int rtl8169_poll(struct napi_struct *napi, int budget)
6692 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6693 struct net_device *dev = tp->dev;
6694 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6698 status = rtl_get_events(tp);
6699 rtl_ack_events(tp, status & ~tp->event_slow);
6701 work_done = rtl_rx(dev, tp, (u32) budget);
6705 if (status & tp->event_slow) {
6706 enable_mask &= ~tp->event_slow;
6708 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6711 if (work_done < budget) {
6712 napi_complete_done(napi, work_done);
6714 rtl_irq_enable(tp, enable_mask);
6721 static void rtl8169_rx_missed(struct net_device *dev)
6723 struct rtl8169_private *tp = netdev_priv(dev);
6725 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6728 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6729 RTL_W32(tp, RxMissed, 0);
6732 static void r8169_phylink_handler(struct net_device *ndev)
6734 struct rtl8169_private *tp = netdev_priv(ndev);
6736 if (netif_carrier_ok(ndev)) {
6737 rtl_link_chg_patch(tp);
6738 pm_request_resume(&tp->pci_dev->dev);
6740 pm_runtime_idle(&tp->pci_dev->dev);
6743 if (net_ratelimit())
6744 phy_print_status(ndev->phydev);
6747 static int r8169_phy_connect(struct rtl8169_private *tp)
6749 struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6750 phy_interface_t phy_mode;
6753 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
6754 PHY_INTERFACE_MODE_MII;
6756 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6761 if (!tp->supports_gmii)
6762 phy_set_max_speed(phydev, SPEED_100);
6764 /* Ensure to advertise everything, incl. pause */
6765 phydev->advertising = phydev->supported;
6767 phy_attached_info(phydev);
6772 static void rtl8169_down(struct net_device *dev)
6774 struct rtl8169_private *tp = netdev_priv(dev);
6776 phy_stop(dev->phydev);
6778 napi_disable(&tp->napi);
6779 netif_stop_queue(dev);
6781 rtl8169_hw_reset(tp);
6783 * At this point device interrupts can not be enabled in any function,
6784 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6785 * and napi is disabled (rtl8169_poll).
6787 rtl8169_rx_missed(dev);
6789 /* Give a racing hard_start_xmit a few cycles to complete. */
6790 synchronize_sched();
6792 rtl8169_tx_clear(tp);
6794 rtl8169_rx_clear(tp);
6796 rtl_pll_power_down(tp);
6799 static int rtl8169_close(struct net_device *dev)
6801 struct rtl8169_private *tp = netdev_priv(dev);
6802 struct pci_dev *pdev = tp->pci_dev;
6804 pm_runtime_get_sync(&pdev->dev);
6806 /* Update counters before going down */
6807 rtl8169_update_counters(tp);
6810 /* Clear all task flags */
6811 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6814 rtl_unlock_work(tp);
6816 cancel_work_sync(&tp->wk.work);
6818 phy_disconnect(dev->phydev);
6820 free_irq(pci_irq_vector(pdev, 0), tp);
6822 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6824 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6826 tp->TxDescArray = NULL;
6827 tp->RxDescArray = NULL;
6829 pm_runtime_put_sync(&pdev->dev);
6834 #ifdef CONFIG_NET_POLL_CONTROLLER
6835 static void rtl8169_netpoll(struct net_device *dev)
6837 struct rtl8169_private *tp = netdev_priv(dev);
6839 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
6843 static int rtl_open(struct net_device *dev)
6845 struct rtl8169_private *tp = netdev_priv(dev);
6846 struct pci_dev *pdev = tp->pci_dev;
6847 int retval = -ENOMEM;
6849 pm_runtime_get_sync(&pdev->dev);
6852 * Rx and Tx descriptors needs 256 bytes alignment.
6853 * dma_alloc_coherent provides more.
6855 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6856 &tp->TxPhyAddr, GFP_KERNEL);
6857 if (!tp->TxDescArray)
6858 goto err_pm_runtime_put;
6860 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6861 &tp->RxPhyAddr, GFP_KERNEL);
6862 if (!tp->RxDescArray)
6865 retval = rtl8169_init_ring(tp);
6869 INIT_WORK(&tp->wk.work, rtl_task);
6873 rtl_request_firmware(tp);
6875 retval = request_irq(pci_irq_vector(pdev, 0), rtl8169_interrupt,
6876 IRQF_SHARED, dev->name, tp);
6878 goto err_release_fw_2;
6880 retval = r8169_phy_connect(tp);
6886 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6888 napi_enable(&tp->napi);
6890 rtl8169_init_phy(dev, tp);
6892 rtl_pll_power_up(tp);
6896 if (!rtl8169_init_counter_offsets(tp))
6897 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6899 phy_start(dev->phydev);
6900 netif_start_queue(dev);
6902 rtl_unlock_work(tp);
6904 pm_runtime_put_sync(&pdev->dev);
6909 free_irq(pci_irq_vector(pdev, 0), tp);
6911 rtl_release_firmware(tp);
6912 rtl8169_rx_clear(tp);
6914 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6916 tp->RxDescArray = NULL;
6918 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6920 tp->TxDescArray = NULL;
6922 pm_runtime_put_noidle(&pdev->dev);
6927 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6929 struct rtl8169_private *tp = netdev_priv(dev);
6930 struct pci_dev *pdev = tp->pci_dev;
6931 struct rtl8169_counters *counters = tp->counters;
6934 pm_runtime_get_noresume(&pdev->dev);
6936 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
6937 rtl8169_rx_missed(dev);
6940 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
6941 stats->rx_packets = tp->rx_stats.packets;
6942 stats->rx_bytes = tp->rx_stats.bytes;
6943 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
6946 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
6947 stats->tx_packets = tp->tx_stats.packets;
6948 stats->tx_bytes = tp->tx_stats.bytes;
6949 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
6951 stats->rx_dropped = dev->stats.rx_dropped;
6952 stats->tx_dropped = dev->stats.tx_dropped;
6953 stats->rx_length_errors = dev->stats.rx_length_errors;
6954 stats->rx_errors = dev->stats.rx_errors;
6955 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6956 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6957 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6958 stats->multicast = dev->stats.multicast;
6961 * Fetch additonal counter values missing in stats collected by driver
6962 * from tally counters.
6964 if (pm_runtime_active(&pdev->dev))
6965 rtl8169_update_counters(tp);
6968 * Subtract values fetched during initalization.
6969 * See rtl8169_init_counter_offsets for a description why we do that.
6971 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
6972 le64_to_cpu(tp->tc_offset.tx_errors);
6973 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
6974 le32_to_cpu(tp->tc_offset.tx_multi_collision);
6975 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
6976 le16_to_cpu(tp->tc_offset.tx_aborted);
6978 pm_runtime_put_noidle(&pdev->dev);
6981 static void rtl8169_net_suspend(struct net_device *dev)
6983 struct rtl8169_private *tp = netdev_priv(dev);
6985 if (!netif_running(dev))
6988 phy_stop(dev->phydev);
6989 netif_device_detach(dev);
6990 netif_stop_queue(dev);
6993 napi_disable(&tp->napi);
6994 /* Clear all task flags */
6995 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6997 rtl_unlock_work(tp);
6999 rtl_pll_power_down(tp);
7004 static int rtl8169_suspend(struct device *device)
7006 struct pci_dev *pdev = to_pci_dev(device);
7007 struct net_device *dev = pci_get_drvdata(pdev);
7008 struct rtl8169_private *tp = netdev_priv(dev);
7010 rtl8169_net_suspend(dev);
7011 clk_disable_unprepare(tp->clk);
7016 static void __rtl8169_resume(struct net_device *dev)
7018 struct rtl8169_private *tp = netdev_priv(dev);
7020 netif_device_attach(dev);
7022 rtl_pll_power_up(tp);
7023 rtl8169_init_phy(dev, tp);
7025 phy_start(tp->dev->phydev);
7028 napi_enable(&tp->napi);
7029 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7030 rtl_unlock_work(tp);
7032 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7035 static int rtl8169_resume(struct device *device)
7037 struct pci_dev *pdev = to_pci_dev(device);
7038 struct net_device *dev = pci_get_drvdata(pdev);
7039 struct rtl8169_private *tp = netdev_priv(dev);
7041 clk_prepare_enable(tp->clk);
7043 if (netif_running(dev))
7044 __rtl8169_resume(dev);
7049 static int rtl8169_runtime_suspend(struct device *device)
7051 struct pci_dev *pdev = to_pci_dev(device);
7052 struct net_device *dev = pci_get_drvdata(pdev);
7053 struct rtl8169_private *tp = netdev_priv(dev);
7055 if (!tp->TxDescArray)
7059 __rtl8169_set_wol(tp, WAKE_ANY);
7060 rtl_unlock_work(tp);
7062 rtl8169_net_suspend(dev);
7064 /* Update counters before going runtime suspend */
7065 rtl8169_rx_missed(dev);
7066 rtl8169_update_counters(tp);
7071 static int rtl8169_runtime_resume(struct device *device)
7073 struct pci_dev *pdev = to_pci_dev(device);
7074 struct net_device *dev = pci_get_drvdata(pdev);
7075 struct rtl8169_private *tp = netdev_priv(dev);
7076 rtl_rar_set(tp, dev->dev_addr);
7078 if (!tp->TxDescArray)
7082 __rtl8169_set_wol(tp, tp->saved_wolopts);
7083 rtl_unlock_work(tp);
7085 __rtl8169_resume(dev);
7090 static int rtl8169_runtime_idle(struct device *device)
7092 struct pci_dev *pdev = to_pci_dev(device);
7093 struct net_device *dev = pci_get_drvdata(pdev);
7095 if (!netif_running(dev) || !netif_carrier_ok(dev))
7096 pm_schedule_suspend(device, 10000);
7101 static const struct dev_pm_ops rtl8169_pm_ops = {
7102 .suspend = rtl8169_suspend,
7103 .resume = rtl8169_resume,
7104 .freeze = rtl8169_suspend,
7105 .thaw = rtl8169_resume,
7106 .poweroff = rtl8169_suspend,
7107 .restore = rtl8169_resume,
7108 .runtime_suspend = rtl8169_runtime_suspend,
7109 .runtime_resume = rtl8169_runtime_resume,
7110 .runtime_idle = rtl8169_runtime_idle,
7113 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
7115 #else /* !CONFIG_PM */
7117 #define RTL8169_PM_OPS NULL
7119 #endif /* !CONFIG_PM */
7121 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7123 /* WoL fails with 8168b when the receiver is disabled. */
7124 switch (tp->mac_version) {
7125 case RTL_GIGA_MAC_VER_11:
7126 case RTL_GIGA_MAC_VER_12:
7127 case RTL_GIGA_MAC_VER_17:
7128 pci_clear_master(tp->pci_dev);
7130 RTL_W8(tp, ChipCmd, CmdRxEnb);
7132 RTL_R8(tp, ChipCmd);
7139 static void rtl_shutdown(struct pci_dev *pdev)
7141 struct net_device *dev = pci_get_drvdata(pdev);
7142 struct rtl8169_private *tp = netdev_priv(dev);
7144 rtl8169_net_suspend(dev);
7146 /* Restore original MAC address */
7147 rtl_rar_set(tp, dev->perm_addr);
7149 rtl8169_hw_reset(tp);
7151 if (system_state == SYSTEM_POWER_OFF) {
7152 if (tp->saved_wolopts) {
7153 rtl_wol_suspend_quirk(tp);
7154 rtl_wol_shutdown_quirk(tp);
7157 pci_wake_from_d3(pdev, true);
7158 pci_set_power_state(pdev, PCI_D3hot);
7162 static void rtl_remove_one(struct pci_dev *pdev)
7164 struct net_device *dev = pci_get_drvdata(pdev);
7165 struct rtl8169_private *tp = netdev_priv(dev);
7167 if (r8168_check_dash(tp))
7168 rtl8168_driver_stop(tp);
7170 netif_napi_del(&tp->napi);
7172 unregister_netdev(dev);
7173 mdiobus_unregister(tp->mii_bus);
7175 rtl_release_firmware(tp);
7177 if (pci_dev_run_wake(pdev))
7178 pm_runtime_get_noresume(&pdev->dev);
7180 /* restore original MAC address */
7181 rtl_rar_set(tp, dev->perm_addr);
7184 static const struct net_device_ops rtl_netdev_ops = {
7185 .ndo_open = rtl_open,
7186 .ndo_stop = rtl8169_close,
7187 .ndo_get_stats64 = rtl8169_get_stats64,
7188 .ndo_start_xmit = rtl8169_start_xmit,
7189 .ndo_tx_timeout = rtl8169_tx_timeout,
7190 .ndo_validate_addr = eth_validate_addr,
7191 .ndo_change_mtu = rtl8169_change_mtu,
7192 .ndo_fix_features = rtl8169_fix_features,
7193 .ndo_set_features = rtl8169_set_features,
7194 .ndo_set_mac_address = rtl_set_mac_address,
7195 .ndo_do_ioctl = rtl8169_ioctl,
7196 .ndo_set_rx_mode = rtl_set_rx_mode,
7197 #ifdef CONFIG_NET_POLL_CONTROLLER
7198 .ndo_poll_controller = rtl8169_netpoll,
7203 static const struct rtl_cfg_info {
7204 void (*hw_start)(struct rtl8169_private *tp);
7206 unsigned int has_gmii:1;
7207 const struct rtl_coalesce_info *coalesce_info;
7209 } rtl_cfg_infos [] = {
7211 .hw_start = rtl_hw_start_8169,
7212 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7214 .coalesce_info = rtl_coalesce_info_8169,
7215 .default_ver = RTL_GIGA_MAC_VER_01,
7218 .hw_start = rtl_hw_start_8168,
7219 .event_slow = SYSErr | LinkChg | RxOverflow,
7221 .coalesce_info = rtl_coalesce_info_8168_8136,
7222 .default_ver = RTL_GIGA_MAC_VER_11,
7225 .hw_start = rtl_hw_start_8101,
7226 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7228 .coalesce_info = rtl_coalesce_info_8168_8136,
7229 .default_ver = RTL_GIGA_MAC_VER_13,
7233 static int rtl_alloc_irq(struct rtl8169_private *tp)
7237 switch (tp->mac_version) {
7238 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
7239 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7240 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7241 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
7243 case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_17:
7244 flags = PCI_IRQ_LEGACY;
7247 flags = PCI_IRQ_ALL_TYPES;
7251 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
7254 DECLARE_RTL_COND(rtl_link_list_ready_cond)
7256 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
7259 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7261 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7264 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7266 struct rtl8169_private *tp = mii_bus->priv;
7271 return rtl_readphy(tp, phyreg);
7274 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7275 int phyreg, u16 val)
7277 struct rtl8169_private *tp = mii_bus->priv;
7282 rtl_writephy(tp, phyreg, val);
7287 static int r8169_mdio_register(struct rtl8169_private *tp)
7289 struct pci_dev *pdev = tp->pci_dev;
7290 struct phy_device *phydev;
7291 struct mii_bus *new_bus;
7294 new_bus = devm_mdiobus_alloc(&pdev->dev);
7298 new_bus->name = "r8169";
7300 new_bus->parent = &pdev->dev;
7301 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7302 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7303 PCI_DEVID(pdev->bus->number, pdev->devfn));
7305 new_bus->read = r8169_mdio_read_reg;
7306 new_bus->write = r8169_mdio_write_reg;
7308 ret = mdiobus_register(new_bus);
7312 phydev = mdiobus_get_phy(new_bus, 0);
7314 mdiobus_unregister(new_bus);
7318 /* PHY will be woken up in rtl_open() */
7319 phy_suspend(phydev);
7321 tp->mii_bus = new_bus;
7326 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
7330 tp->ocp_base = OCP_STD_PHY_BASE;
7332 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
7334 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7337 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7340 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7342 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
7344 data = r8168_mac_ocp_read(tp, 0xe8de);
7346 r8168_mac_ocp_write(tp, 0xe8de, data);
7348 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7351 data = r8168_mac_ocp_read(tp, 0xe8de);
7353 r8168_mac_ocp_write(tp, 0xe8de, data);
7355 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7359 static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7361 rtl8168ep_stop_cmac(tp);
7362 rtl_hw_init_8168g(tp);
7365 static void rtl_hw_initialize(struct rtl8169_private *tp)
7367 switch (tp->mac_version) {
7368 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
7369 rtl_hw_init_8168g(tp);
7371 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
7372 rtl_hw_init_8168ep(tp);
7379 /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
7380 static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
7382 switch (tp->mac_version) {
7383 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7384 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
7391 static int rtl_jumbo_max(struct rtl8169_private *tp)
7393 /* Non-GBit versions don't support jumbo frames */
7394 if (!tp->supports_gmii)
7397 switch (tp->mac_version) {
7399 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7402 case RTL_GIGA_MAC_VER_11:
7403 case RTL_GIGA_MAC_VER_12:
7404 case RTL_GIGA_MAC_VER_17:
7407 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
7414 static void rtl_disable_clk(void *data)
7416 clk_disable_unprepare(data);
7419 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7421 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7422 struct rtl8169_private *tp;
7423 struct net_device *dev;
7424 int chipset, region, i;
7427 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7431 SET_NETDEV_DEV(dev, &pdev->dev);
7432 dev->netdev_ops = &rtl_netdev_ops;
7433 tp = netdev_priv(dev);
7436 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7437 tp->supports_gmii = cfg->has_gmii;
7439 /* Get the *optional* external "ether_clk" used on some boards */
7440 tp->clk = devm_clk_get(&pdev->dev, "ether_clk");
7441 if (IS_ERR(tp->clk)) {
7442 rc = PTR_ERR(tp->clk);
7443 if (rc == -ENOENT) {
7444 /* clk-core allows NULL (for suspend / resume) */
7446 } else if (rc == -EPROBE_DEFER) {
7449 dev_err(&pdev->dev, "failed to get clk: %d\n", rc);
7453 rc = clk_prepare_enable(tp->clk);
7455 dev_err(&pdev->dev, "failed to enable clk: %d\n", rc);
7459 rc = devm_add_action_or_reset(&pdev->dev, rtl_disable_clk,
7465 /* Disable ASPM completely as that cause random device stop working
7466 * problems as well as full system hangs for some PCIe devices users.
7468 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
7470 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7471 rc = pcim_enable_device(pdev);
7473 dev_err(&pdev->dev, "enable failure\n");
7477 if (pcim_set_mwi(pdev) < 0)
7478 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
7480 /* use first MMIO region */
7481 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7483 dev_err(&pdev->dev, "no MMIO resource found\n");
7487 /* check for weird/broken PCI region reporting */
7488 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7489 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
7493 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
7495 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
7499 tp->mmio_addr = pcim_iomap_table(pdev)[region];
7501 if (!pci_is_pcie(pdev))
7502 dev_info(&pdev->dev, "not PCI Express\n");
7504 /* Identify chip attached to board */
7505 rtl8169_get_mac_version(tp, cfg->default_ver);
7507 if (rtl_tbi_enabled(tp)) {
7508 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7512 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
7514 if ((sizeof(dma_addr_t) > 4) &&
7515 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7516 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
7517 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7518 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
7520 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7521 if (!pci_is_pcie(pdev))
7522 tp->cp_cmd |= PCIDAC;
7523 dev->features |= NETIF_F_HIGHDMA;
7525 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7527 dev_err(&pdev->dev, "DMA configuration failed\n");
7534 rtl_irq_disable(tp);
7536 rtl_hw_initialize(tp);
7540 rtl_ack_events(tp, 0xffff);
7542 pci_set_master(pdev);
7544 rtl_init_mdio_ops(tp);
7545 rtl_init_jumbo_ops(tp);
7547 rtl8169_print_mac_version(tp);
7549 chipset = tp->mac_version;
7551 rc = rtl_alloc_irq(tp);
7553 dev_err(&pdev->dev, "Can't allocate interrupt\n");
7557 tp->saved_wolopts = __rtl8169_get_wol(tp);
7559 mutex_init(&tp->wk.mutex);
7560 u64_stats_init(&tp->rx_stats.syncp);
7561 u64_stats_init(&tp->tx_stats.syncp);
7563 /* Get MAC address */
7564 switch (tp->mac_version) {
7565 u8 mac_addr[ETH_ALEN] __aligned(4);
7566 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7567 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
7568 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
7569 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
7571 if (is_valid_ether_addr(mac_addr))
7572 rtl_rar_set(tp, mac_addr);
7577 for (i = 0; i < ETH_ALEN; i++)
7578 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
7580 dev->ethtool_ops = &rtl8169_ethtool_ops;
7581 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
7583 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
7585 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7586 * properly for all devices */
7587 dev->features |= NETIF_F_RXCSUM |
7588 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
7590 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7591 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7592 NETIF_F_HW_VLAN_CTAG_RX;
7593 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7595 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
7597 tp->cp_cmd |= RxChkSum | RxVlan;
7600 * Pretend we are using VLANs; This bypasses a nasty bug where
7601 * Interrupts stop flowing on high load on 8110SCd controllers.
7603 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7604 /* Disallow toggling */
7605 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
7607 if (rtl_chip_supports_csum_v2(tp)) {
7608 tp->tso_csum = rtl8169_tso_csum_v2;
7609 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7611 tp->tso_csum = rtl8169_tso_csum_v1;
7614 dev->hw_features |= NETIF_F_RXALL;
7615 dev->hw_features |= NETIF_F_RXFCS;
7617 /* MTU range: 60 - hw-specific max */
7618 dev->min_mtu = ETH_ZLEN;
7619 jumbo_max = rtl_jumbo_max(tp);
7620 dev->max_mtu = jumbo_max;
7622 tp->hw_start = cfg->hw_start;
7623 tp->event_slow = cfg->event_slow;
7624 tp->coalesce_info = cfg->coalesce_info;
7626 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7628 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7629 &tp->counters_phys_addr,
7634 pci_set_drvdata(pdev, dev);
7636 rc = r8169_mdio_register(tp);
7640 /* chip gets powered up in rtl_open() */
7641 rtl_pll_power_down(tp);
7643 rc = register_netdev(dev);
7645 goto err_mdio_unregister;
7647 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7648 rtl_chip_infos[chipset].name, dev->dev_addr,
7649 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
7650 pci_irq_vector(pdev, 0));
7652 if (jumbo_max > JUMBO_1K)
7653 netif_info(tp, probe, dev,
7654 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7655 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
7658 if (r8168_check_dash(tp))
7659 rtl8168_driver_start(tp);
7661 if (pci_dev_run_wake(pdev))
7662 pm_runtime_put_sync(&pdev->dev);
7666 err_mdio_unregister:
7667 mdiobus_unregister(tp->mii_bus);
7671 static struct pci_driver rtl8169_pci_driver = {
7673 .id_table = rtl8169_pci_tbl,
7674 .probe = rtl_init_one,
7675 .remove = rtl_remove_one,
7676 .shutdown = rtl_shutdown,
7677 .driver.pm = RTL8169_PM_OPS,
7680 module_pci_driver(rtl8169_pci_driver);