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 "rtl_nic/rtl8168d-1.fw"
38 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
39 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
40 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
41 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
42 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
43 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
44 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
45 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
46 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
47 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
48 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
49 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
50 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
51 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
52 #define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
53 #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
54 #define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
55 #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
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");
725 MODULE_FIRMWARE(FIRMWARE_8168D_1
);
726 MODULE_FIRMWARE(FIRMWARE_8168D_2
);
727 MODULE_FIRMWARE(FIRMWARE_8168E_1
);
728 MODULE_FIRMWARE(FIRMWARE_8168E_2
);
729 MODULE_FIRMWARE(FIRMWARE_8168E_3
);
730 MODULE_FIRMWARE(FIRMWARE_8105E_1
);
731 MODULE_FIRMWARE(FIRMWARE_8168F_1
);
732 MODULE_FIRMWARE(FIRMWARE_8168F_2
);
733 MODULE_FIRMWARE(FIRMWARE_8402_1
);
734 MODULE_FIRMWARE(FIRMWARE_8411_1
);
735 MODULE_FIRMWARE(FIRMWARE_8411_2
);
736 MODULE_FIRMWARE(FIRMWARE_8106E_1
);
737 MODULE_FIRMWARE(FIRMWARE_8106E_2
);
738 MODULE_FIRMWARE(FIRMWARE_8168G_2
);
739 MODULE_FIRMWARE(FIRMWARE_8168G_3
);
740 MODULE_FIRMWARE(FIRMWARE_8168H_1
);
741 MODULE_FIRMWARE(FIRMWARE_8168H_2
);
742 MODULE_FIRMWARE(FIRMWARE_8107E_1
);
743 MODULE_FIRMWARE(FIRMWARE_8107E_2
);
745 static inline struct device
*tp_to_dev(struct rtl8169_private
*tp
)
747 return &tp
->pci_dev
->dev
;
750 static void rtl_lock_work(struct rtl8169_private
*tp
)
752 mutex_lock(&tp
->wk
.mutex
);
755 static void rtl_unlock_work(struct rtl8169_private
*tp
)
757 mutex_unlock(&tp
->wk
.mutex
);
760 static void rtl_tx_performance_tweak(struct rtl8169_private
*tp
, u16 force
)
762 pcie_capability_clear_and_set_word(tp
->pci_dev
, PCI_EXP_DEVCTL
,
763 PCI_EXP_DEVCTL_READRQ
, force
);
767 bool (*check
)(struct rtl8169_private
*);
771 static void rtl_udelay(unsigned int d
)
776 static bool rtl_loop_wait(struct rtl8169_private
*tp
, const struct rtl_cond
*c
,
777 void (*delay
)(unsigned int), unsigned int d
, int n
,
782 for (i
= 0; i
< n
; i
++) {
784 if (c
->check(tp
) == high
)
787 netif_err(tp
, drv
, tp
->dev
, "%s == %d (loop: %d, delay: %d).\n",
788 c
->msg
, !high
, n
, d
);
792 static bool rtl_udelay_loop_wait_high(struct rtl8169_private
*tp
,
793 const struct rtl_cond
*c
,
794 unsigned int d
, int n
)
796 return rtl_loop_wait(tp
, c
, rtl_udelay
, d
, n
, true);
799 static bool rtl_udelay_loop_wait_low(struct rtl8169_private
*tp
,
800 const struct rtl_cond
*c
,
801 unsigned int d
, int n
)
803 return rtl_loop_wait(tp
, c
, rtl_udelay
, d
, n
, false);
806 static bool rtl_msleep_loop_wait_high(struct rtl8169_private
*tp
,
807 const struct rtl_cond
*c
,
808 unsigned int d
, int n
)
810 return rtl_loop_wait(tp
, c
, msleep
, d
, n
, true);
813 static bool rtl_msleep_loop_wait_low(struct rtl8169_private
*tp
,
814 const struct rtl_cond
*c
,
815 unsigned int d
, int n
)
817 return rtl_loop_wait(tp
, c
, msleep
, d
, n
, false);
820 #define DECLARE_RTL_COND(name) \
821 static bool name ## _check(struct rtl8169_private *); \
823 static const struct rtl_cond name = { \
824 .check = name ## _check, \
828 static bool name ## _check(struct rtl8169_private *tp)
830 static bool rtl_ocp_reg_failure(struct rtl8169_private
*tp
, u32 reg
)
832 if (reg
& 0xffff0001) {
833 netif_err(tp
, drv
, tp
->dev
, "Invalid ocp reg %x!\n", reg
);
839 DECLARE_RTL_COND(rtl_ocp_gphy_cond
)
841 return RTL_R32(tp
, GPHY_OCP
) & OCPAR_FLAG
;
844 static void r8168_phy_ocp_write(struct rtl8169_private
*tp
, u32 reg
, u32 data
)
846 if (rtl_ocp_reg_failure(tp
, reg
))
849 RTL_W32(tp
, GPHY_OCP
, OCPAR_FLAG
| (reg
<< 15) | data
);
851 rtl_udelay_loop_wait_low(tp
, &rtl_ocp_gphy_cond
, 25, 10);
854 static u16
r8168_phy_ocp_read(struct rtl8169_private
*tp
, u32 reg
)
856 if (rtl_ocp_reg_failure(tp
, reg
))
859 RTL_W32(tp
, GPHY_OCP
, reg
<< 15);
861 return rtl_udelay_loop_wait_high(tp
, &rtl_ocp_gphy_cond
, 25, 10) ?
862 (RTL_R32(tp
, GPHY_OCP
) & 0xffff) : ~0;
865 static void r8168_mac_ocp_write(struct rtl8169_private
*tp
, u32 reg
, u32 data
)
867 if (rtl_ocp_reg_failure(tp
, reg
))
870 RTL_W32(tp
, OCPDR
, OCPAR_FLAG
| (reg
<< 15) | data
);
873 static u16
r8168_mac_ocp_read(struct rtl8169_private
*tp
, u32 reg
)
875 if (rtl_ocp_reg_failure(tp
, reg
))
878 RTL_W32(tp
, OCPDR
, reg
<< 15);
880 return RTL_R32(tp
, OCPDR
);
883 #define OCP_STD_PHY_BASE 0xa400
885 static void r8168g_mdio_write(struct rtl8169_private
*tp
, int reg
, int value
)
888 tp
->ocp_base
= value
? value
<< 4 : OCP_STD_PHY_BASE
;
892 if (tp
->ocp_base
!= OCP_STD_PHY_BASE
)
895 r8168_phy_ocp_write(tp
, tp
->ocp_base
+ reg
* 2, value
);
898 static int r8168g_mdio_read(struct rtl8169_private
*tp
, int reg
)
900 if (tp
->ocp_base
!= OCP_STD_PHY_BASE
)
903 return r8168_phy_ocp_read(tp
, tp
->ocp_base
+ reg
* 2);
906 static void mac_mcu_write(struct rtl8169_private
*tp
, int reg
, int value
)
909 tp
->ocp_base
= value
<< 4;
913 r8168_mac_ocp_write(tp
, tp
->ocp_base
+ reg
, value
);
916 static int mac_mcu_read(struct rtl8169_private
*tp
, int reg
)
918 return r8168_mac_ocp_read(tp
, tp
->ocp_base
+ reg
);
921 DECLARE_RTL_COND(rtl_phyar_cond
)
923 return RTL_R32(tp
, PHYAR
) & 0x80000000;
926 static void r8169_mdio_write(struct rtl8169_private
*tp
, int reg
, int value
)
928 RTL_W32(tp
, PHYAR
, 0x80000000 | (reg
& 0x1f) << 16 | (value
& 0xffff));
930 rtl_udelay_loop_wait_low(tp
, &rtl_phyar_cond
, 25, 20);
932 * According to hardware specs a 20us delay is required after write
933 * complete indication, but before sending next command.
938 static int r8169_mdio_read(struct rtl8169_private
*tp
, int reg
)
942 RTL_W32(tp
, PHYAR
, 0x0 | (reg
& 0x1f) << 16);
944 value
= rtl_udelay_loop_wait_high(tp
, &rtl_phyar_cond
, 25, 20) ?
945 RTL_R32(tp
, PHYAR
) & 0xffff : ~0;
948 * According to hardware specs a 20us delay is required after read
949 * complete indication, but before sending next command.
956 DECLARE_RTL_COND(rtl_ocpar_cond
)
958 return RTL_R32(tp
, OCPAR
) & OCPAR_FLAG
;
961 static void r8168dp_1_mdio_access(struct rtl8169_private
*tp
, int reg
, u32 data
)
963 RTL_W32(tp
, OCPDR
, data
| ((reg
& OCPDR_REG_MASK
) << OCPDR_GPHY_REG_SHIFT
));
964 RTL_W32(tp
, OCPAR
, OCPAR_GPHY_WRITE_CMD
);
965 RTL_W32(tp
, EPHY_RXER_NUM
, 0);
967 rtl_udelay_loop_wait_low(tp
, &rtl_ocpar_cond
, 1000, 100);
970 static void r8168dp_1_mdio_write(struct rtl8169_private
*tp
, int reg
, int value
)
972 r8168dp_1_mdio_access(tp
, reg
,
973 OCPDR_WRITE_CMD
| (value
& OCPDR_DATA_MASK
));
976 static int r8168dp_1_mdio_read(struct rtl8169_private
*tp
, int reg
)
978 r8168dp_1_mdio_access(tp
, reg
, OCPDR_READ_CMD
);
981 RTL_W32(tp
, OCPAR
, OCPAR_GPHY_READ_CMD
);
982 RTL_W32(tp
, EPHY_RXER_NUM
, 0);
984 return rtl_udelay_loop_wait_high(tp
, &rtl_ocpar_cond
, 1000, 100) ?
985 RTL_R32(tp
, OCPDR
) & OCPDR_DATA_MASK
: ~0;
988 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
990 static void r8168dp_2_mdio_start(struct rtl8169_private
*tp
)
992 RTL_W32(tp
, 0xd0, RTL_R32(tp
, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT
);
995 static void r8168dp_2_mdio_stop(struct rtl8169_private
*tp
)
997 RTL_W32(tp
, 0xd0, RTL_R32(tp
, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT
);
1000 static void r8168dp_2_mdio_write(struct rtl8169_private
*tp
, int reg
, int value
)
1002 r8168dp_2_mdio_start(tp
);
1004 r8169_mdio_write(tp
, reg
, value
);
1006 r8168dp_2_mdio_stop(tp
);
1009 static int r8168dp_2_mdio_read(struct rtl8169_private
*tp
, int reg
)
1013 /* Work around issue with chip reporting wrong PHY ID */
1014 if (reg
== MII_PHYSID2
)
1017 r8168dp_2_mdio_start(tp
);
1019 value
= r8169_mdio_read(tp
, reg
);
1021 r8168dp_2_mdio_stop(tp
);
1026 static void rtl_writephy(struct rtl8169_private
*tp
, int location
, u32 val
)
1028 tp
->mdio_ops
.write(tp
, location
, val
);
1031 static int rtl_readphy(struct rtl8169_private
*tp
, int location
)
1033 return tp
->mdio_ops
.read(tp
, location
);
1036 static void rtl_patchphy(struct rtl8169_private
*tp
, int reg_addr
, int value
)
1038 rtl_writephy(tp
, reg_addr
, rtl_readphy(tp
, reg_addr
) | value
);
1041 static void rtl_w0w1_phy(struct rtl8169_private
*tp
, int reg_addr
, int p
, int m
)
1045 val
= rtl_readphy(tp
, reg_addr
);
1046 rtl_writephy(tp
, reg_addr
, (val
& ~m
) | p
);
1049 DECLARE_RTL_COND(rtl_ephyar_cond
)
1051 return RTL_R32(tp
, EPHYAR
) & EPHYAR_FLAG
;
1054 static void rtl_ephy_write(struct rtl8169_private
*tp
, int reg_addr
, int value
)
1056 RTL_W32(tp
, EPHYAR
, EPHYAR_WRITE_CMD
| (value
& EPHYAR_DATA_MASK
) |
1057 (reg_addr
& EPHYAR_REG_MASK
) << EPHYAR_REG_SHIFT
);
1059 rtl_udelay_loop_wait_low(tp
, &rtl_ephyar_cond
, 10, 100);
1064 static u16
rtl_ephy_read(struct rtl8169_private
*tp
, int reg_addr
)
1066 RTL_W32(tp
, EPHYAR
, (reg_addr
& EPHYAR_REG_MASK
) << EPHYAR_REG_SHIFT
);
1068 return rtl_udelay_loop_wait_high(tp
, &rtl_ephyar_cond
, 10, 100) ?
1069 RTL_R32(tp
, EPHYAR
) & EPHYAR_DATA_MASK
: ~0;
1072 DECLARE_RTL_COND(rtl_eriar_cond
)
1074 return RTL_R32(tp
, ERIAR
) & ERIAR_FLAG
;
1077 static void rtl_eri_write(struct rtl8169_private
*tp
, int addr
, u32 mask
,
1080 BUG_ON((addr
& 3) || (mask
== 0));
1081 RTL_W32(tp
, ERIDR
, val
);
1082 RTL_W32(tp
, ERIAR
, ERIAR_WRITE_CMD
| type
| mask
| addr
);
1084 rtl_udelay_loop_wait_low(tp
, &rtl_eriar_cond
, 100, 100);
1087 static u32
rtl_eri_read(struct rtl8169_private
*tp
, int addr
, int type
)
1089 RTL_W32(tp
, ERIAR
, ERIAR_READ_CMD
| type
| ERIAR_MASK_1111
| addr
);
1091 return rtl_udelay_loop_wait_high(tp
, &rtl_eriar_cond
, 100, 100) ?
1092 RTL_R32(tp
, ERIDR
) : ~0;
1095 static void rtl_w0w1_eri(struct rtl8169_private
*tp
, int addr
, u32 mask
, u32 p
,
1100 val
= rtl_eri_read(tp
, addr
, type
);
1101 rtl_eri_write(tp
, addr
, mask
, (val
& ~m
) | p
, type
);
1104 static u32
r8168dp_ocp_read(struct rtl8169_private
*tp
, u8 mask
, u16 reg
)
1106 RTL_W32(tp
, OCPAR
, ((u32
)mask
& 0x0f) << 12 | (reg
& 0x0fff));
1107 return rtl_udelay_loop_wait_high(tp
, &rtl_ocpar_cond
, 100, 20) ?
1108 RTL_R32(tp
, OCPDR
) : ~0;
1111 static u32
r8168ep_ocp_read(struct rtl8169_private
*tp
, u8 mask
, u16 reg
)
1113 return rtl_eri_read(tp
, reg
, ERIAR_OOB
);
1116 static u32
ocp_read(struct rtl8169_private
*tp
, u8 mask
, u16 reg
)
1118 switch (tp
->mac_version
) {
1119 case RTL_GIGA_MAC_VER_27
:
1120 case RTL_GIGA_MAC_VER_28
:
1121 case RTL_GIGA_MAC_VER_31
:
1122 return r8168dp_ocp_read(tp
, mask
, reg
);
1123 case RTL_GIGA_MAC_VER_49
:
1124 case RTL_GIGA_MAC_VER_50
:
1125 case RTL_GIGA_MAC_VER_51
:
1126 return r8168ep_ocp_read(tp
, mask
, reg
);
1133 static void r8168dp_ocp_write(struct rtl8169_private
*tp
, u8 mask
, u16 reg
,
1136 RTL_W32(tp
, OCPDR
, data
);
1137 RTL_W32(tp
, OCPAR
, OCPAR_FLAG
| ((u32
)mask
& 0x0f) << 12 | (reg
& 0x0fff));
1138 rtl_udelay_loop_wait_low(tp
, &rtl_ocpar_cond
, 100, 20);
1141 static void r8168ep_ocp_write(struct rtl8169_private
*tp
, u8 mask
, u16 reg
,
1144 rtl_eri_write(tp
, reg
, ((u32
)mask
& 0x0f) << ERIAR_MASK_SHIFT
,
1148 static void ocp_write(struct rtl8169_private
*tp
, u8 mask
, u16 reg
, u32 data
)
1150 switch (tp
->mac_version
) {
1151 case RTL_GIGA_MAC_VER_27
:
1152 case RTL_GIGA_MAC_VER_28
:
1153 case RTL_GIGA_MAC_VER_31
:
1154 r8168dp_ocp_write(tp
, mask
, reg
, data
);
1156 case RTL_GIGA_MAC_VER_49
:
1157 case RTL_GIGA_MAC_VER_50
:
1158 case RTL_GIGA_MAC_VER_51
:
1159 r8168ep_ocp_write(tp
, mask
, reg
, data
);
1167 static void rtl8168_oob_notify(struct rtl8169_private
*tp
, u8 cmd
)
1169 rtl_eri_write(tp
, 0xe8, ERIAR_MASK_0001
, cmd
, ERIAR_EXGMAC
);
1171 ocp_write(tp
, 0x1, 0x30, 0x00000001);
1174 #define OOB_CMD_RESET 0x00
1175 #define OOB_CMD_DRIVER_START 0x05
1176 #define OOB_CMD_DRIVER_STOP 0x06
1178 static u16
rtl8168_get_ocp_reg(struct rtl8169_private
*tp
)
1180 return (tp
->mac_version
== RTL_GIGA_MAC_VER_31
) ? 0xb8 : 0x10;
1183 DECLARE_RTL_COND(rtl_ocp_read_cond
)
1187 reg
= rtl8168_get_ocp_reg(tp
);
1189 return ocp_read(tp
, 0x0f, reg
) & 0x00000800;
1192 DECLARE_RTL_COND(rtl_ep_ocp_read_cond
)
1194 return ocp_read(tp
, 0x0f, 0x124) & 0x00000001;
1197 DECLARE_RTL_COND(rtl_ocp_tx_cond
)
1199 return RTL_R8(tp
, IBISR0
) & 0x20;
1202 static void rtl8168ep_stop_cmac(struct rtl8169_private
*tp
)
1204 RTL_W8(tp
, IBCR2
, RTL_R8(tp
, IBCR2
) & ~0x01);
1205 rtl_msleep_loop_wait_high(tp
, &rtl_ocp_tx_cond
, 50, 2000);
1206 RTL_W8(tp
, IBISR0
, RTL_R8(tp
, IBISR0
) | 0x20);
1207 RTL_W8(tp
, IBCR0
, RTL_R8(tp
, IBCR0
) & ~0x01);
1210 static void rtl8168dp_driver_start(struct rtl8169_private
*tp
)
1212 rtl8168_oob_notify(tp
, OOB_CMD_DRIVER_START
);
1213 rtl_msleep_loop_wait_high(tp
, &rtl_ocp_read_cond
, 10, 10);
1216 static void rtl8168ep_driver_start(struct rtl8169_private
*tp
)
1218 ocp_write(tp
, 0x01, 0x180, OOB_CMD_DRIVER_START
);
1219 ocp_write(tp
, 0x01, 0x30, ocp_read(tp
, 0x01, 0x30) | 0x01);
1220 rtl_msleep_loop_wait_high(tp
, &rtl_ep_ocp_read_cond
, 10, 10);
1223 static void rtl8168_driver_start(struct rtl8169_private
*tp
)
1225 switch (tp
->mac_version
) {
1226 case RTL_GIGA_MAC_VER_27
:
1227 case RTL_GIGA_MAC_VER_28
:
1228 case RTL_GIGA_MAC_VER_31
:
1229 rtl8168dp_driver_start(tp
);
1231 case RTL_GIGA_MAC_VER_49
:
1232 case RTL_GIGA_MAC_VER_50
:
1233 case RTL_GIGA_MAC_VER_51
:
1234 rtl8168ep_driver_start(tp
);
1242 static void rtl8168dp_driver_stop(struct rtl8169_private
*tp
)
1244 rtl8168_oob_notify(tp
, OOB_CMD_DRIVER_STOP
);
1245 rtl_msleep_loop_wait_low(tp
, &rtl_ocp_read_cond
, 10, 10);
1248 static void rtl8168ep_driver_stop(struct rtl8169_private
*tp
)
1250 rtl8168ep_stop_cmac(tp
);
1251 ocp_write(tp
, 0x01, 0x180, OOB_CMD_DRIVER_STOP
);
1252 ocp_write(tp
, 0x01, 0x30, ocp_read(tp
, 0x01, 0x30) | 0x01);
1253 rtl_msleep_loop_wait_low(tp
, &rtl_ep_ocp_read_cond
, 10, 10);
1256 static void rtl8168_driver_stop(struct rtl8169_private
*tp
)
1258 switch (tp
->mac_version
) {
1259 case RTL_GIGA_MAC_VER_27
:
1260 case RTL_GIGA_MAC_VER_28
:
1261 case RTL_GIGA_MAC_VER_31
:
1262 rtl8168dp_driver_stop(tp
);
1264 case RTL_GIGA_MAC_VER_49
:
1265 case RTL_GIGA_MAC_VER_50
:
1266 case RTL_GIGA_MAC_VER_51
:
1267 rtl8168ep_driver_stop(tp
);
1275 static bool r8168dp_check_dash(struct rtl8169_private
*tp
)
1277 u16 reg
= rtl8168_get_ocp_reg(tp
);
1279 return !!(ocp_read(tp
, 0x0f, reg
) & 0x00008000);
1282 static bool r8168ep_check_dash(struct rtl8169_private
*tp
)
1284 return !!(ocp_read(tp
, 0x0f, 0x128) & 0x00000001);
1287 static bool r8168_check_dash(struct rtl8169_private
*tp
)
1289 switch (tp
->mac_version
) {
1290 case RTL_GIGA_MAC_VER_27
:
1291 case RTL_GIGA_MAC_VER_28
:
1292 case RTL_GIGA_MAC_VER_31
:
1293 return r8168dp_check_dash(tp
);
1294 case RTL_GIGA_MAC_VER_49
:
1295 case RTL_GIGA_MAC_VER_50
:
1296 case RTL_GIGA_MAC_VER_51
:
1297 return r8168ep_check_dash(tp
);
1309 static void rtl_write_exgmac_batch(struct rtl8169_private
*tp
,
1310 const struct exgmac_reg
*r
, int len
)
1313 rtl_eri_write(tp
, r
->addr
, r
->mask
, r
->val
, ERIAR_EXGMAC
);
1318 DECLARE_RTL_COND(rtl_efusear_cond
)
1320 return RTL_R32(tp
, EFUSEAR
) & EFUSEAR_FLAG
;
1323 static u8
rtl8168d_efuse_read(struct rtl8169_private
*tp
, int reg_addr
)
1325 RTL_W32(tp
, EFUSEAR
, (reg_addr
& EFUSEAR_REG_MASK
) << EFUSEAR_REG_SHIFT
);
1327 return rtl_udelay_loop_wait_high(tp
, &rtl_efusear_cond
, 100, 300) ?
1328 RTL_R32(tp
, EFUSEAR
) & EFUSEAR_DATA_MASK
: ~0;
1331 static u16
rtl_get_events(struct rtl8169_private
*tp
)
1333 return RTL_R16(tp
, IntrStatus
);
1336 static void rtl_ack_events(struct rtl8169_private
*tp
, u16 bits
)
1338 RTL_W16(tp
, IntrStatus
, bits
);
1342 static void rtl_irq_disable(struct rtl8169_private
*tp
)
1344 RTL_W16(tp
, IntrMask
, 0);
1348 static void rtl_irq_enable(struct rtl8169_private
*tp
, u16 bits
)
1350 RTL_W16(tp
, IntrMask
, bits
);
1353 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1354 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1355 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1357 static void rtl_irq_enable_all(struct rtl8169_private
*tp
)
1359 rtl_irq_enable(tp
, RTL_EVENT_NAPI
| tp
->event_slow
);
1362 static void rtl8169_irq_mask_and_ack(struct rtl8169_private
*tp
)
1364 rtl_irq_disable(tp
);
1365 rtl_ack_events(tp
, RTL_EVENT_NAPI
| tp
->event_slow
);
1366 RTL_R8(tp
, ChipCmd
);
1369 static void rtl_link_chg_patch(struct rtl8169_private
*tp
)
1371 struct net_device
*dev
= tp
->dev
;
1372 struct phy_device
*phydev
= dev
->phydev
;
1374 if (!netif_running(dev
))
1377 if (tp
->mac_version
== RTL_GIGA_MAC_VER_34
||
1378 tp
->mac_version
== RTL_GIGA_MAC_VER_38
) {
1379 if (phydev
->speed
== SPEED_1000
) {
1380 rtl_eri_write(tp
, 0x1bc, ERIAR_MASK_1111
, 0x00000011,
1382 rtl_eri_write(tp
, 0x1dc, ERIAR_MASK_1111
, 0x00000005,
1384 } else if (phydev
->speed
== SPEED_100
) {
1385 rtl_eri_write(tp
, 0x1bc, ERIAR_MASK_1111
, 0x0000001f,
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 /* Reset packet filter */
1396 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x00, 0x01,
1398 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x01, 0x00,
1400 } else if (tp
->mac_version
== RTL_GIGA_MAC_VER_35
||
1401 tp
->mac_version
== RTL_GIGA_MAC_VER_36
) {
1402 if (phydev
->speed
== SPEED_1000
) {
1403 rtl_eri_write(tp
, 0x1bc, ERIAR_MASK_1111
, 0x00000011,
1405 rtl_eri_write(tp
, 0x1dc, ERIAR_MASK_1111
, 0x00000005,
1408 rtl_eri_write(tp
, 0x1bc, ERIAR_MASK_1111
, 0x0000001f,
1410 rtl_eri_write(tp
, 0x1dc, ERIAR_MASK_1111
, 0x0000003f,
1413 } else if (tp
->mac_version
== RTL_GIGA_MAC_VER_37
) {
1414 if (phydev
->speed
== SPEED_10
) {
1415 rtl_eri_write(tp
, 0x1d0, ERIAR_MASK_0011
, 0x4d02,
1417 rtl_eri_write(tp
, 0x1dc, ERIAR_MASK_0011
, 0x0060,
1420 rtl_eri_write(tp
, 0x1d0, ERIAR_MASK_0011
, 0x0000,
1426 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1428 static u32
__rtl8169_get_wol(struct rtl8169_private
*tp
)
1433 options
= RTL_R8(tp
, Config1
);
1434 if (!(options
& PMEnable
))
1437 options
= RTL_R8(tp
, Config3
);
1438 if (options
& LinkUp
)
1439 wolopts
|= WAKE_PHY
;
1440 switch (tp
->mac_version
) {
1441 case RTL_GIGA_MAC_VER_34
... RTL_GIGA_MAC_VER_38
:
1442 case RTL_GIGA_MAC_VER_40
... RTL_GIGA_MAC_VER_51
:
1443 if (rtl_eri_read(tp
, 0xdc, ERIAR_EXGMAC
) & MagicPacket_v2
)
1444 wolopts
|= WAKE_MAGIC
;
1447 if (options
& MagicPacket
)
1448 wolopts
|= WAKE_MAGIC
;
1452 options
= RTL_R8(tp
, Config5
);
1454 wolopts
|= WAKE_UCAST
;
1456 wolopts
|= WAKE_BCAST
;
1458 wolopts
|= WAKE_MCAST
;
1463 static void rtl8169_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
1465 struct rtl8169_private
*tp
= netdev_priv(dev
);
1468 wol
->supported
= WAKE_ANY
;
1469 wol
->wolopts
= tp
->saved_wolopts
;
1470 rtl_unlock_work(tp
);
1473 static void __rtl8169_set_wol(struct rtl8169_private
*tp
, u32 wolopts
)
1475 unsigned int i
, tmp
;
1476 static const struct {
1481 { WAKE_PHY
, Config3
, LinkUp
},
1482 { WAKE_UCAST
, Config5
, UWF
},
1483 { WAKE_BCAST
, Config5
, BWF
},
1484 { WAKE_MCAST
, Config5
, MWF
},
1485 { WAKE_ANY
, Config5
, LanWake
},
1486 { WAKE_MAGIC
, Config3
, MagicPacket
}
1490 RTL_W8(tp
, Cfg9346
, Cfg9346_Unlock
);
1492 switch (tp
->mac_version
) {
1493 case RTL_GIGA_MAC_VER_34
... RTL_GIGA_MAC_VER_38
:
1494 case RTL_GIGA_MAC_VER_40
... RTL_GIGA_MAC_VER_51
:
1495 tmp
= ARRAY_SIZE(cfg
) - 1;
1496 if (wolopts
& WAKE_MAGIC
)
1512 tmp
= ARRAY_SIZE(cfg
);
1516 for (i
= 0; i
< tmp
; i
++) {
1517 options
= RTL_R8(tp
, cfg
[i
].reg
) & ~cfg
[i
].mask
;
1518 if (wolopts
& cfg
[i
].opt
)
1519 options
|= cfg
[i
].mask
;
1520 RTL_W8(tp
, cfg
[i
].reg
, options
);
1523 switch (tp
->mac_version
) {
1524 case RTL_GIGA_MAC_VER_01
... RTL_GIGA_MAC_VER_17
:
1525 options
= RTL_R8(tp
, Config1
) & ~PMEnable
;
1527 options
|= PMEnable
;
1528 RTL_W8(tp
, Config1
, options
);
1531 options
= RTL_R8(tp
, Config2
) & ~PME_SIGNAL
;
1533 options
|= PME_SIGNAL
;
1534 RTL_W8(tp
, Config2
, options
);
1538 RTL_W8(tp
, Cfg9346
, Cfg9346_Lock
);
1540 device_set_wakeup_enable(tp_to_dev(tp
), wolopts
);
1543 static int rtl8169_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
1545 struct rtl8169_private
*tp
= netdev_priv(dev
);
1546 struct device
*d
= tp_to_dev(tp
);
1548 if (wol
->wolopts
& ~WAKE_ANY
)
1551 pm_runtime_get_noresume(d
);
1555 tp
->saved_wolopts
= wol
->wolopts
;
1557 if (pm_runtime_active(d
))
1558 __rtl8169_set_wol(tp
, tp
->saved_wolopts
);
1560 rtl_unlock_work(tp
);
1562 pm_runtime_put_noidle(d
);
1567 static const char *rtl_lookup_firmware_name(struct rtl8169_private
*tp
)
1569 return rtl_chip_infos
[tp
->mac_version
].fw_name
;
1572 static void rtl8169_get_drvinfo(struct net_device
*dev
,
1573 struct ethtool_drvinfo
*info
)
1575 struct rtl8169_private
*tp
= netdev_priv(dev
);
1576 struct rtl_fw
*rtl_fw
= tp
->rtl_fw
;
1578 strlcpy(info
->driver
, MODULENAME
, sizeof(info
->driver
));
1579 strlcpy(info
->bus_info
, pci_name(tp
->pci_dev
), sizeof(info
->bus_info
));
1580 BUILD_BUG_ON(sizeof(info
->fw_version
) < sizeof(rtl_fw
->version
));
1581 if (!IS_ERR_OR_NULL(rtl_fw
))
1582 strlcpy(info
->fw_version
, rtl_fw
->version
,
1583 sizeof(info
->fw_version
));
1586 static int rtl8169_get_regs_len(struct net_device
*dev
)
1588 return R8169_REGS_SIZE
;
1591 static netdev_features_t
rtl8169_fix_features(struct net_device
*dev
,
1592 netdev_features_t features
)
1594 struct rtl8169_private
*tp
= netdev_priv(dev
);
1596 if (dev
->mtu
> TD_MSS_MAX
)
1597 features
&= ~NETIF_F_ALL_TSO
;
1599 if (dev
->mtu
> JUMBO_1K
&&
1600 tp
->mac_version
> RTL_GIGA_MAC_VER_06
)
1601 features
&= ~NETIF_F_IP_CSUM
;
1606 static int rtl8169_set_features(struct net_device
*dev
,
1607 netdev_features_t features
)
1609 struct rtl8169_private
*tp
= netdev_priv(dev
);
1614 rx_config
= RTL_R32(tp
, RxConfig
);
1615 if (features
& NETIF_F_RXALL
)
1616 rx_config
|= (AcceptErr
| AcceptRunt
);
1618 rx_config
&= ~(AcceptErr
| AcceptRunt
);
1620 RTL_W32(tp
, RxConfig
, rx_config
);
1622 if (features
& NETIF_F_RXCSUM
)
1623 tp
->cp_cmd
|= RxChkSum
;
1625 tp
->cp_cmd
&= ~RxChkSum
;
1627 if (features
& NETIF_F_HW_VLAN_CTAG_RX
)
1628 tp
->cp_cmd
|= RxVlan
;
1630 tp
->cp_cmd
&= ~RxVlan
;
1632 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
1633 RTL_R16(tp
, CPlusCmd
);
1635 rtl_unlock_work(tp
);
1640 static inline u32
rtl8169_tx_vlan_tag(struct sk_buff
*skb
)
1642 return (skb_vlan_tag_present(skb
)) ?
1643 TxVlanTag
| swab16(skb_vlan_tag_get(skb
)) : 0x00;
1646 static void rtl8169_rx_vlan_tag(struct RxDesc
*desc
, struct sk_buff
*skb
)
1648 u32 opts2
= le32_to_cpu(desc
->opts2
);
1650 if (opts2
& RxVlanTag
)
1651 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), swab16(opts2
& 0xffff));
1654 static void rtl8169_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
1657 struct rtl8169_private
*tp
= netdev_priv(dev
);
1658 u32 __iomem
*data
= tp
->mmio_addr
;
1663 for (i
= 0; i
< R8169_REGS_SIZE
; i
+= 4)
1664 memcpy_fromio(dw
++, data
++, 4);
1665 rtl_unlock_work(tp
);
1668 static u32
rtl8169_get_msglevel(struct net_device
*dev
)
1670 struct rtl8169_private
*tp
= netdev_priv(dev
);
1672 return tp
->msg_enable
;
1675 static void rtl8169_set_msglevel(struct net_device
*dev
, u32 value
)
1677 struct rtl8169_private
*tp
= netdev_priv(dev
);
1679 tp
->msg_enable
= value
;
1682 static const char rtl8169_gstrings
[][ETH_GSTRING_LEN
] = {
1689 "tx_single_collisions",
1690 "tx_multi_collisions",
1698 static int rtl8169_get_sset_count(struct net_device
*dev
, int sset
)
1702 return ARRAY_SIZE(rtl8169_gstrings
);
1708 DECLARE_RTL_COND(rtl_counters_cond
)
1710 return RTL_R32(tp
, CounterAddrLow
) & (CounterReset
| CounterDump
);
1713 static bool rtl8169_do_counters(struct rtl8169_private
*tp
, u32 counter_cmd
)
1715 dma_addr_t paddr
= tp
->counters_phys_addr
;
1718 RTL_W32(tp
, CounterAddrHigh
, (u64
)paddr
>> 32);
1719 RTL_R32(tp
, CounterAddrHigh
);
1720 cmd
= (u64
)paddr
& DMA_BIT_MASK(32);
1721 RTL_W32(tp
, CounterAddrLow
, cmd
);
1722 RTL_W32(tp
, CounterAddrLow
, cmd
| counter_cmd
);
1724 return rtl_udelay_loop_wait_low(tp
, &rtl_counters_cond
, 10, 1000);
1727 static bool rtl8169_reset_counters(struct rtl8169_private
*tp
)
1730 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1733 if (tp
->mac_version
< RTL_GIGA_MAC_VER_19
)
1736 return rtl8169_do_counters(tp
, CounterReset
);
1739 static bool rtl8169_update_counters(struct rtl8169_private
*tp
)
1741 u8 val
= RTL_R8(tp
, ChipCmd
);
1744 * Some chips are unable to dump tally counters when the receiver
1745 * is disabled. If 0xff chip may be in a PCI power-save state.
1747 if (!(val
& CmdRxEnb
) || val
== 0xff)
1750 return rtl8169_do_counters(tp
, CounterDump
);
1753 static bool rtl8169_init_counter_offsets(struct rtl8169_private
*tp
)
1755 struct rtl8169_counters
*counters
= tp
->counters
;
1759 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1760 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1761 * reset by a power cycle, while the counter values collected by the
1762 * driver are reset at every driver unload/load cycle.
1764 * To make sure the HW values returned by @get_stats64 match the SW
1765 * values, we collect the initial values at first open(*) and use them
1766 * as offsets to normalize the values returned by @get_stats64.
1768 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1769 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1770 * set at open time by rtl_hw_start.
1773 if (tp
->tc_offset
.inited
)
1776 /* If both, reset and update fail, propagate to caller. */
1777 if (rtl8169_reset_counters(tp
))
1780 if (rtl8169_update_counters(tp
))
1783 tp
->tc_offset
.tx_errors
= counters
->tx_errors
;
1784 tp
->tc_offset
.tx_multi_collision
= counters
->tx_multi_collision
;
1785 tp
->tc_offset
.tx_aborted
= counters
->tx_aborted
;
1786 tp
->tc_offset
.inited
= true;
1791 static void rtl8169_get_ethtool_stats(struct net_device
*dev
,
1792 struct ethtool_stats
*stats
, u64
*data
)
1794 struct rtl8169_private
*tp
= netdev_priv(dev
);
1795 struct device
*d
= tp_to_dev(tp
);
1796 struct rtl8169_counters
*counters
= tp
->counters
;
1800 pm_runtime_get_noresume(d
);
1802 if (pm_runtime_active(d
))
1803 rtl8169_update_counters(tp
);
1805 pm_runtime_put_noidle(d
);
1807 data
[0] = le64_to_cpu(counters
->tx_packets
);
1808 data
[1] = le64_to_cpu(counters
->rx_packets
);
1809 data
[2] = le64_to_cpu(counters
->tx_errors
);
1810 data
[3] = le32_to_cpu(counters
->rx_errors
);
1811 data
[4] = le16_to_cpu(counters
->rx_missed
);
1812 data
[5] = le16_to_cpu(counters
->align_errors
);
1813 data
[6] = le32_to_cpu(counters
->tx_one_collision
);
1814 data
[7] = le32_to_cpu(counters
->tx_multi_collision
);
1815 data
[8] = le64_to_cpu(counters
->rx_unicast
);
1816 data
[9] = le64_to_cpu(counters
->rx_broadcast
);
1817 data
[10] = le32_to_cpu(counters
->rx_multicast
);
1818 data
[11] = le16_to_cpu(counters
->tx_aborted
);
1819 data
[12] = le16_to_cpu(counters
->tx_underun
);
1822 static void rtl8169_get_strings(struct net_device
*dev
, u32 stringset
, u8
*data
)
1826 memcpy(data
, *rtl8169_gstrings
, sizeof(rtl8169_gstrings
));
1832 * Interrupt coalescing
1834 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1835 * > 8169, 8168 and 810x line of chipsets
1837 * 8169, 8168, and 8136(810x) serial chipsets support it.
1839 * > 2 - the Tx timer unit at gigabit speed
1841 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1842 * (0xe0) bit 1 and bit 0.
1845 * bit[1:0] \ speed 1000M 100M 10M
1846 * 0 0 320ns 2.56us 40.96us
1847 * 0 1 2.56us 20.48us 327.7us
1848 * 1 0 5.12us 40.96us 655.4us
1849 * 1 1 10.24us 81.92us 1.31ms
1852 * bit[1:0] \ speed 1000M 100M 10M
1853 * 0 0 5us 2.56us 40.96us
1854 * 0 1 40us 20.48us 327.7us
1855 * 1 0 80us 40.96us 655.4us
1856 * 1 1 160us 81.92us 1.31ms
1859 /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1860 struct rtl_coalesce_scale
{
1865 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
1866 struct rtl_coalesce_info
{
1868 struct rtl_coalesce_scale scalev
[4]; /* each CPlusCmd[0:1] case */
1871 /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1872 #define rxtx_x1822(r, t) { \
1875 {{(r)*8*2, (t)*8*2}}, \
1876 {{(r)*8*2*2, (t)*8*2*2}}, \
1878 static const struct rtl_coalesce_info rtl_coalesce_info_8169
[] = {
1879 /* speed delays: rx00 tx00 */
1880 { SPEED_10
, rxtx_x1822(40960, 40960) },
1881 { SPEED_100
, rxtx_x1822( 2560, 2560) },
1882 { SPEED_1000
, rxtx_x1822( 320, 320) },
1886 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136
[] = {
1887 /* speed delays: rx00 tx00 */
1888 { SPEED_10
, rxtx_x1822(40960, 40960) },
1889 { SPEED_100
, rxtx_x1822( 2560, 2560) },
1890 { SPEED_1000
, rxtx_x1822( 5000, 5000) },
1895 /* get rx/tx scale vector corresponding to current speed */
1896 static const struct rtl_coalesce_info
*rtl_coalesce_info(struct net_device
*dev
)
1898 struct rtl8169_private
*tp
= netdev_priv(dev
);
1899 struct ethtool_link_ksettings ecmd
;
1900 const struct rtl_coalesce_info
*ci
;
1903 rc
= phy_ethtool_get_link_ksettings(dev
, &ecmd
);
1907 for (ci
= tp
->coalesce_info
; ci
->speed
!= 0; ci
++) {
1908 if (ecmd
.base
.speed
== ci
->speed
) {
1913 return ERR_PTR(-ELNRNG
);
1916 static int rtl_get_coalesce(struct net_device
*dev
, struct ethtool_coalesce
*ec
)
1918 struct rtl8169_private
*tp
= netdev_priv(dev
);
1919 const struct rtl_coalesce_info
*ci
;
1920 const struct rtl_coalesce_scale
*scale
;
1924 } coal_settings
[] = {
1925 { &ec
->rx_max_coalesced_frames
, &ec
->rx_coalesce_usecs
},
1926 { &ec
->tx_max_coalesced_frames
, &ec
->tx_coalesce_usecs
}
1927 }, *p
= coal_settings
;
1931 memset(ec
, 0, sizeof(*ec
));
1933 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1934 ci
= rtl_coalesce_info(dev
);
1938 scale
= &ci
->scalev
[tp
->cp_cmd
& INTT_MASK
];
1940 /* read IntrMitigate and adjust according to scale */
1941 for (w
= RTL_R16(tp
, IntrMitigate
); w
; w
>>= RTL_COALESCE_SHIFT
, p
++) {
1942 *p
->max_frames
= (w
& RTL_COALESCE_MASK
) << 2;
1943 w
>>= RTL_COALESCE_SHIFT
;
1944 *p
->usecs
= w
& RTL_COALESCE_MASK
;
1947 for (i
= 0; i
< 2; i
++) {
1948 p
= coal_settings
+ i
;
1949 *p
->usecs
= (*p
->usecs
* scale
->nsecs
[i
]) / 1000;
1952 * ethtool_coalesce says it is illegal to set both usecs and
1955 if (!*p
->usecs
&& !*p
->max_frames
)
1962 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1963 static const struct rtl_coalesce_scale
*rtl_coalesce_choose_scale(
1964 struct net_device
*dev
, u32 nsec
, u16
*cp01
)
1966 const struct rtl_coalesce_info
*ci
;
1969 ci
= rtl_coalesce_info(dev
);
1971 return ERR_CAST(ci
);
1973 for (i
= 0; i
< 4; i
++) {
1974 u32 rxtx_maxscale
= max(ci
->scalev
[i
].nsecs
[0],
1975 ci
->scalev
[i
].nsecs
[1]);
1976 if (nsec
<= rxtx_maxscale
* RTL_COALESCE_T_MAX
) {
1978 return &ci
->scalev
[i
];
1982 return ERR_PTR(-EINVAL
);
1985 static int rtl_set_coalesce(struct net_device
*dev
, struct ethtool_coalesce
*ec
)
1987 struct rtl8169_private
*tp
= netdev_priv(dev
);
1988 const struct rtl_coalesce_scale
*scale
;
1992 } coal_settings
[] = {
1993 { ec
->rx_max_coalesced_frames
, ec
->rx_coalesce_usecs
},
1994 { ec
->tx_max_coalesced_frames
, ec
->tx_coalesce_usecs
}
1995 }, *p
= coal_settings
;
1999 scale
= rtl_coalesce_choose_scale(dev
,
2000 max(p
[0].usecs
, p
[1].usecs
) * 1000, &cp01
);
2002 return PTR_ERR(scale
);
2004 for (i
= 0; i
< 2; i
++, p
++) {
2008 * accept max_frames=1 we returned in rtl_get_coalesce.
2009 * accept it not only when usecs=0 because of e.g. the following scenario:
2011 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2012 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2013 * - then user does `ethtool -C eth0 rx-usecs 100`
2015 * since ethtool sends to kernel whole ethtool_coalesce
2016 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2017 * we'll reject it below in `frames % 4 != 0`.
2019 if (p
->frames
== 1) {
2023 units
= p
->usecs
* 1000 / scale
->nsecs
[i
];
2024 if (p
->frames
> RTL_COALESCE_FRAME_MAX
|| p
->frames
% 4)
2027 w
<<= RTL_COALESCE_SHIFT
;
2029 w
<<= RTL_COALESCE_SHIFT
;
2030 w
|= p
->frames
>> 2;
2035 RTL_W16(tp
, IntrMitigate
, swab16(w
));
2037 tp
->cp_cmd
= (tp
->cp_cmd
& ~INTT_MASK
) | cp01
;
2038 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
2039 RTL_R16(tp
, CPlusCmd
);
2041 rtl_unlock_work(tp
);
2046 static const struct ethtool_ops rtl8169_ethtool_ops
= {
2047 .get_drvinfo
= rtl8169_get_drvinfo
,
2048 .get_regs_len
= rtl8169_get_regs_len
,
2049 .get_link
= ethtool_op_get_link
,
2050 .get_coalesce
= rtl_get_coalesce
,
2051 .set_coalesce
= rtl_set_coalesce
,
2052 .get_msglevel
= rtl8169_get_msglevel
,
2053 .set_msglevel
= rtl8169_set_msglevel
,
2054 .get_regs
= rtl8169_get_regs
,
2055 .get_wol
= rtl8169_get_wol
,
2056 .set_wol
= rtl8169_set_wol
,
2057 .get_strings
= rtl8169_get_strings
,
2058 .get_sset_count
= rtl8169_get_sset_count
,
2059 .get_ethtool_stats
= rtl8169_get_ethtool_stats
,
2060 .get_ts_info
= ethtool_op_get_ts_info
,
2061 .nway_reset
= phy_ethtool_nway_reset
,
2062 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
2063 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
2066 static void rtl8169_get_mac_version(struct rtl8169_private
*tp
,
2070 * The driver currently handles the 8168Bf and the 8168Be identically
2071 * but they can be identified more specifically through the test below
2074 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2076 * Same thing for the 8101Eb and the 8101Ec:
2078 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2080 static const struct rtl_mac_info
{
2085 /* 8168EP family. */
2086 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51
},
2087 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50
},
2088 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49
},
2091 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46
},
2092 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45
},
2095 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44
},
2096 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42
},
2097 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41
},
2098 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40
},
2101 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38
},
2102 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36
},
2103 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35
},
2106 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34
},
2107 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32
},
2108 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33
},
2111 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25
},
2112 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26
},
2114 /* 8168DP family. */
2115 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27
},
2116 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28
},
2117 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31
},
2120 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23
},
2121 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18
},
2122 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24
},
2123 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19
},
2124 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20
},
2125 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21
},
2126 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22
},
2129 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12
},
2130 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17
},
2131 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11
},
2134 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39
},
2135 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37
},
2136 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29
},
2137 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30
},
2138 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08
},
2139 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08
},
2140 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07
},
2141 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07
},
2142 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13
},
2143 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10
},
2144 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16
},
2145 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09
},
2146 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09
},
2147 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16
},
2148 /* FIXME: where did these entries come from ? -- FR */
2149 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15
},
2150 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14
},
2153 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06
},
2154 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05
},
2155 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04
},
2156 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03
},
2157 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02
},
2158 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01
},
2161 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE
}
2163 const struct rtl_mac_info
*p
= mac_info
;
2166 reg
= RTL_R32(tp
, TxConfig
);
2167 while ((reg
& p
->mask
) != p
->val
)
2169 tp
->mac_version
= p
->mac_version
;
2171 if (tp
->mac_version
== RTL_GIGA_MAC_NONE
) {
2172 dev_notice(tp_to_dev(tp
),
2173 "unknown MAC, using family default\n");
2174 tp
->mac_version
= default_version
;
2175 } else if (tp
->mac_version
== RTL_GIGA_MAC_VER_42
) {
2176 tp
->mac_version
= tp
->supports_gmii
?
2177 RTL_GIGA_MAC_VER_42
:
2178 RTL_GIGA_MAC_VER_43
;
2179 } else if (tp
->mac_version
== RTL_GIGA_MAC_VER_45
) {
2180 tp
->mac_version
= tp
->supports_gmii
?
2181 RTL_GIGA_MAC_VER_45
:
2182 RTL_GIGA_MAC_VER_47
;
2183 } else if (tp
->mac_version
== RTL_GIGA_MAC_VER_46
) {
2184 tp
->mac_version
= tp
->supports_gmii
?
2185 RTL_GIGA_MAC_VER_46
:
2186 RTL_GIGA_MAC_VER_48
;
2190 static void rtl8169_print_mac_version(struct rtl8169_private
*tp
)
2192 netif_dbg(tp
, drv
, tp
->dev
, "mac_version = 0x%02x\n", tp
->mac_version
);
2200 static void rtl_writephy_batch(struct rtl8169_private
*tp
,
2201 const struct phy_reg
*regs
, int len
)
2204 rtl_writephy(tp
, regs
->reg
, regs
->val
);
2209 #define PHY_READ 0x00000000
2210 #define PHY_DATA_OR 0x10000000
2211 #define PHY_DATA_AND 0x20000000
2212 #define PHY_BJMPN 0x30000000
2213 #define PHY_MDIO_CHG 0x40000000
2214 #define PHY_CLEAR_READCOUNT 0x70000000
2215 #define PHY_WRITE 0x80000000
2216 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2217 #define PHY_COMP_EQ_SKIPN 0xa0000000
2218 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2219 #define PHY_WRITE_PREVIOUS 0xc0000000
2220 #define PHY_SKIPN 0xd0000000
2221 #define PHY_DELAY_MS 0xe0000000
2225 char version
[RTL_VER_SIZE
];
2231 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2233 static bool rtl_fw_format_ok(struct rtl8169_private
*tp
, struct rtl_fw
*rtl_fw
)
2235 const struct firmware
*fw
= rtl_fw
->fw
;
2236 struct fw_info
*fw_info
= (struct fw_info
*)fw
->data
;
2237 struct rtl_fw_phy_action
*pa
= &rtl_fw
->phy_action
;
2238 char *version
= rtl_fw
->version
;
2241 if (fw
->size
< FW_OPCODE_SIZE
)
2244 if (!fw_info
->magic
) {
2245 size_t i
, size
, start
;
2248 if (fw
->size
< sizeof(*fw_info
))
2251 for (i
= 0; i
< fw
->size
; i
++)
2252 checksum
+= fw
->data
[i
];
2256 start
= le32_to_cpu(fw_info
->fw_start
);
2257 if (start
> fw
->size
)
2260 size
= le32_to_cpu(fw_info
->fw_len
);
2261 if (size
> (fw
->size
- start
) / FW_OPCODE_SIZE
)
2264 memcpy(version
, fw_info
->version
, RTL_VER_SIZE
);
2266 pa
->code
= (__le32
*)(fw
->data
+ start
);
2269 if (fw
->size
% FW_OPCODE_SIZE
)
2272 strlcpy(version
, rtl_lookup_firmware_name(tp
), RTL_VER_SIZE
);
2274 pa
->code
= (__le32
*)fw
->data
;
2275 pa
->size
= fw
->size
/ FW_OPCODE_SIZE
;
2277 version
[RTL_VER_SIZE
- 1] = 0;
2284 static bool rtl_fw_data_ok(struct rtl8169_private
*tp
, struct net_device
*dev
,
2285 struct rtl_fw_phy_action
*pa
)
2290 for (index
= 0; index
< pa
->size
; index
++) {
2291 u32 action
= le32_to_cpu(pa
->code
[index
]);
2292 u32 regno
= (action
& 0x0fff0000) >> 16;
2294 switch(action
& 0xf0000000) {
2299 case PHY_CLEAR_READCOUNT
:
2301 case PHY_WRITE_PREVIOUS
:
2306 if (regno
> index
) {
2307 netif_err(tp
, ifup
, tp
->dev
,
2308 "Out of range of firmware\n");
2312 case PHY_READCOUNT_EQ_SKIP
:
2313 if (index
+ 2 >= pa
->size
) {
2314 netif_err(tp
, ifup
, tp
->dev
,
2315 "Out of range of firmware\n");
2319 case PHY_COMP_EQ_SKIPN
:
2320 case PHY_COMP_NEQ_SKIPN
:
2322 if (index
+ 1 + regno
>= pa
->size
) {
2323 netif_err(tp
, ifup
, tp
->dev
,
2324 "Out of range of firmware\n");
2330 netif_err(tp
, ifup
, tp
->dev
,
2331 "Invalid action 0x%08x\n", action
);
2340 static int rtl_check_firmware(struct rtl8169_private
*tp
, struct rtl_fw
*rtl_fw
)
2342 struct net_device
*dev
= tp
->dev
;
2345 if (!rtl_fw_format_ok(tp
, rtl_fw
)) {
2346 netif_err(tp
, ifup
, dev
, "invalid firmware\n");
2350 if (rtl_fw_data_ok(tp
, dev
, &rtl_fw
->phy_action
))
2356 static void rtl_phy_write_fw(struct rtl8169_private
*tp
, struct rtl_fw
*rtl_fw
)
2358 struct rtl_fw_phy_action
*pa
= &rtl_fw
->phy_action
;
2359 struct mdio_ops org
, *ops
= &tp
->mdio_ops
;
2363 predata
= count
= 0;
2364 org
.write
= ops
->write
;
2365 org
.read
= ops
->read
;
2367 for (index
= 0; index
< pa
->size
; ) {
2368 u32 action
= le32_to_cpu(pa
->code
[index
]);
2369 u32 data
= action
& 0x0000ffff;
2370 u32 regno
= (action
& 0x0fff0000) >> 16;
2375 switch(action
& 0xf0000000) {
2377 predata
= rtl_readphy(tp
, regno
);
2394 ops
->write
= org
.write
;
2395 ops
->read
= org
.read
;
2396 } else if (data
== 1) {
2397 ops
->write
= mac_mcu_write
;
2398 ops
->read
= mac_mcu_read
;
2403 case PHY_CLEAR_READCOUNT
:
2408 rtl_writephy(tp
, regno
, data
);
2411 case PHY_READCOUNT_EQ_SKIP
:
2412 index
+= (count
== data
) ? 2 : 1;
2414 case PHY_COMP_EQ_SKIPN
:
2415 if (predata
== data
)
2419 case PHY_COMP_NEQ_SKIPN
:
2420 if (predata
!= data
)
2424 case PHY_WRITE_PREVIOUS
:
2425 rtl_writephy(tp
, regno
, predata
);
2441 ops
->write
= org
.write
;
2442 ops
->read
= org
.read
;
2445 static void rtl_release_firmware(struct rtl8169_private
*tp
)
2447 if (!IS_ERR_OR_NULL(tp
->rtl_fw
)) {
2448 release_firmware(tp
->rtl_fw
->fw
);
2451 tp
->rtl_fw
= RTL_FIRMWARE_UNKNOWN
;
2454 static void rtl_apply_firmware(struct rtl8169_private
*tp
)
2456 struct rtl_fw
*rtl_fw
= tp
->rtl_fw
;
2458 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2459 if (!IS_ERR_OR_NULL(rtl_fw
))
2460 rtl_phy_write_fw(tp
, rtl_fw
);
2463 static void rtl_apply_firmware_cond(struct rtl8169_private
*tp
, u8 reg
, u16 val
)
2465 if (rtl_readphy(tp
, reg
) != val
)
2466 netif_warn(tp
, hw
, tp
->dev
, "chipset not ready for firmware\n");
2468 rtl_apply_firmware(tp
);
2471 static void rtl8169s_hw_phy_config(struct rtl8169_private
*tp
)
2473 static const struct phy_reg phy_reg_init
[] = {
2535 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2538 static void rtl8169sb_hw_phy_config(struct rtl8169_private
*tp
)
2540 static const struct phy_reg phy_reg_init
[] = {
2546 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2549 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private
*tp
)
2551 struct pci_dev
*pdev
= tp
->pci_dev
;
2553 if ((pdev
->subsystem_vendor
!= PCI_VENDOR_ID_GIGABYTE
) ||
2554 (pdev
->subsystem_device
!= 0xe000))
2557 rtl_writephy(tp
, 0x1f, 0x0001);
2558 rtl_writephy(tp
, 0x10, 0xf01b);
2559 rtl_writephy(tp
, 0x1f, 0x0000);
2562 static void rtl8169scd_hw_phy_config(struct rtl8169_private
*tp
)
2564 static const struct phy_reg phy_reg_init
[] = {
2604 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2606 rtl8169scd_hw_phy_config_quirk(tp
);
2609 static void rtl8169sce_hw_phy_config(struct rtl8169_private
*tp
)
2611 static const struct phy_reg phy_reg_init
[] = {
2659 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2662 static void rtl8168bb_hw_phy_config(struct rtl8169_private
*tp
)
2664 static const struct phy_reg phy_reg_init
[] = {
2669 rtl_writephy(tp
, 0x1f, 0x0001);
2670 rtl_patchphy(tp
, 0x16, 1 << 0);
2672 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2675 static void rtl8168bef_hw_phy_config(struct rtl8169_private
*tp
)
2677 static const struct phy_reg phy_reg_init
[] = {
2683 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2686 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private
*tp
)
2688 static const struct phy_reg phy_reg_init
[] = {
2696 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2699 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private
*tp
)
2701 static const struct phy_reg phy_reg_init
[] = {
2707 rtl_writephy(tp
, 0x1f, 0x0000);
2708 rtl_patchphy(tp
, 0x14, 1 << 5);
2709 rtl_patchphy(tp
, 0x0d, 1 << 5);
2711 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2714 static void rtl8168c_1_hw_phy_config(struct rtl8169_private
*tp
)
2716 static const struct phy_reg phy_reg_init
[] = {
2736 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2738 rtl_patchphy(tp
, 0x14, 1 << 5);
2739 rtl_patchphy(tp
, 0x0d, 1 << 5);
2740 rtl_writephy(tp
, 0x1f, 0x0000);
2743 static void rtl8168c_2_hw_phy_config(struct rtl8169_private
*tp
)
2745 static const struct phy_reg phy_reg_init
[] = {
2763 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2765 rtl_patchphy(tp
, 0x16, 1 << 0);
2766 rtl_patchphy(tp
, 0x14, 1 << 5);
2767 rtl_patchphy(tp
, 0x0d, 1 << 5);
2768 rtl_writephy(tp
, 0x1f, 0x0000);
2771 static void rtl8168c_3_hw_phy_config(struct rtl8169_private
*tp
)
2773 static const struct phy_reg phy_reg_init
[] = {
2785 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2787 rtl_patchphy(tp
, 0x16, 1 << 0);
2788 rtl_patchphy(tp
, 0x14, 1 << 5);
2789 rtl_patchphy(tp
, 0x0d, 1 << 5);
2790 rtl_writephy(tp
, 0x1f, 0x0000);
2793 static void rtl8168c_4_hw_phy_config(struct rtl8169_private
*tp
)
2795 rtl8168c_3_hw_phy_config(tp
);
2798 static void rtl8168d_1_hw_phy_config(struct rtl8169_private
*tp
)
2800 static const struct phy_reg phy_reg_init_0
[] = {
2801 /* Channel Estimation */
2822 * Enhance line driver power
2831 * Can not link to 1Gbps with bad cable
2832 * Decrease SNR threshold form 21.07dB to 19.04dB
2841 rtl_writephy_batch(tp
, phy_reg_init_0
, ARRAY_SIZE(phy_reg_init_0
));
2845 * Fine Tune Switching regulator parameter
2847 rtl_writephy(tp
, 0x1f, 0x0002);
2848 rtl_w0w1_phy(tp
, 0x0b, 0x0010, 0x00ef);
2849 rtl_w0w1_phy(tp
, 0x0c, 0xa200, 0x5d00);
2851 if (rtl8168d_efuse_read(tp
, 0x01) == 0xb1) {
2852 static const struct phy_reg phy_reg_init
[] = {
2862 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2864 val
= rtl_readphy(tp
, 0x0d);
2866 if ((val
& 0x00ff) != 0x006c) {
2867 static const u32 set
[] = {
2868 0x0065, 0x0066, 0x0067, 0x0068,
2869 0x0069, 0x006a, 0x006b, 0x006c
2873 rtl_writephy(tp
, 0x1f, 0x0002);
2876 for (i
= 0; i
< ARRAY_SIZE(set
); i
++)
2877 rtl_writephy(tp
, 0x0d, val
| set
[i
]);
2880 static const struct phy_reg phy_reg_init
[] = {
2888 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2891 /* RSET couple improve */
2892 rtl_writephy(tp
, 0x1f, 0x0002);
2893 rtl_patchphy(tp
, 0x0d, 0x0300);
2894 rtl_patchphy(tp
, 0x0f, 0x0010);
2896 /* Fine tune PLL performance */
2897 rtl_writephy(tp
, 0x1f, 0x0002);
2898 rtl_w0w1_phy(tp
, 0x02, 0x0100, 0x0600);
2899 rtl_w0w1_phy(tp
, 0x03, 0x0000, 0xe000);
2901 rtl_writephy(tp
, 0x1f, 0x0005);
2902 rtl_writephy(tp
, 0x05, 0x001b);
2904 rtl_apply_firmware_cond(tp
, MII_EXPANSION
, 0xbf00);
2906 rtl_writephy(tp
, 0x1f, 0x0000);
2909 static void rtl8168d_2_hw_phy_config(struct rtl8169_private
*tp
)
2911 static const struct phy_reg phy_reg_init_0
[] = {
2912 /* Channel Estimation */
2933 * Enhance line driver power
2942 * Can not link to 1Gbps with bad cable
2943 * Decrease SNR threshold form 21.07dB to 19.04dB
2952 rtl_writephy_batch(tp
, phy_reg_init_0
, ARRAY_SIZE(phy_reg_init_0
));
2954 if (rtl8168d_efuse_read(tp
, 0x01) == 0xb1) {
2955 static const struct phy_reg phy_reg_init
[] = {
2966 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2968 val
= rtl_readphy(tp
, 0x0d);
2969 if ((val
& 0x00ff) != 0x006c) {
2970 static const u32 set
[] = {
2971 0x0065, 0x0066, 0x0067, 0x0068,
2972 0x0069, 0x006a, 0x006b, 0x006c
2976 rtl_writephy(tp
, 0x1f, 0x0002);
2979 for (i
= 0; i
< ARRAY_SIZE(set
); i
++)
2980 rtl_writephy(tp
, 0x0d, val
| set
[i
]);
2983 static const struct phy_reg phy_reg_init
[] = {
2991 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2994 /* Fine tune PLL performance */
2995 rtl_writephy(tp
, 0x1f, 0x0002);
2996 rtl_w0w1_phy(tp
, 0x02, 0x0100, 0x0600);
2997 rtl_w0w1_phy(tp
, 0x03, 0x0000, 0xe000);
2999 /* Switching regulator Slew rate */
3000 rtl_writephy(tp
, 0x1f, 0x0002);
3001 rtl_patchphy(tp
, 0x0f, 0x0017);
3003 rtl_writephy(tp
, 0x1f, 0x0005);
3004 rtl_writephy(tp
, 0x05, 0x001b);
3006 rtl_apply_firmware_cond(tp
, MII_EXPANSION
, 0xb300);
3008 rtl_writephy(tp
, 0x1f, 0x0000);
3011 static void rtl8168d_3_hw_phy_config(struct rtl8169_private
*tp
)
3013 static const struct phy_reg phy_reg_init
[] = {
3069 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3072 static void rtl8168d_4_hw_phy_config(struct rtl8169_private
*tp
)
3074 static const struct phy_reg phy_reg_init
[] = {
3084 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3085 rtl_patchphy(tp
, 0x0d, 1 << 5);
3088 static void rtl8168e_1_hw_phy_config(struct rtl8169_private
*tp
)
3090 static const struct phy_reg phy_reg_init
[] = {
3091 /* Enable Delay cap */
3097 /* Channel estimation fine tune */
3106 /* Update PFM & 10M TX idle timer */
3118 rtl_apply_firmware(tp
);
3120 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3122 /* DCO enable for 10M IDLE Power */
3123 rtl_writephy(tp
, 0x1f, 0x0007);
3124 rtl_writephy(tp
, 0x1e, 0x0023);
3125 rtl_w0w1_phy(tp
, 0x17, 0x0006, 0x0000);
3126 rtl_writephy(tp
, 0x1f, 0x0000);
3128 /* For impedance matching */
3129 rtl_writephy(tp
, 0x1f, 0x0002);
3130 rtl_w0w1_phy(tp
, 0x08, 0x8000, 0x7f00);
3131 rtl_writephy(tp
, 0x1f, 0x0000);
3133 /* PHY auto speed down */
3134 rtl_writephy(tp
, 0x1f, 0x0007);
3135 rtl_writephy(tp
, 0x1e, 0x002d);
3136 rtl_w0w1_phy(tp
, 0x18, 0x0050, 0x0000);
3137 rtl_writephy(tp
, 0x1f, 0x0000);
3138 rtl_w0w1_phy(tp
, 0x14, 0x8000, 0x0000);
3140 rtl_writephy(tp
, 0x1f, 0x0005);
3141 rtl_writephy(tp
, 0x05, 0x8b86);
3142 rtl_w0w1_phy(tp
, 0x06, 0x0001, 0x0000);
3143 rtl_writephy(tp
, 0x1f, 0x0000);
3145 rtl_writephy(tp
, 0x1f, 0x0005);
3146 rtl_writephy(tp
, 0x05, 0x8b85);
3147 rtl_w0w1_phy(tp
, 0x06, 0x0000, 0x2000);
3148 rtl_writephy(tp
, 0x1f, 0x0007);
3149 rtl_writephy(tp
, 0x1e, 0x0020);
3150 rtl_w0w1_phy(tp
, 0x15, 0x0000, 0x1100);
3151 rtl_writephy(tp
, 0x1f, 0x0006);
3152 rtl_writephy(tp
, 0x00, 0x5a00);
3153 rtl_writephy(tp
, 0x1f, 0x0000);
3154 rtl_writephy(tp
, 0x0d, 0x0007);
3155 rtl_writephy(tp
, 0x0e, 0x003c);
3156 rtl_writephy(tp
, 0x0d, 0x4007);
3157 rtl_writephy(tp
, 0x0e, 0x0000);
3158 rtl_writephy(tp
, 0x0d, 0x0000);
3161 static void rtl_rar_exgmac_set(struct rtl8169_private
*tp
, u8
*addr
)
3164 addr
[0] | (addr
[1] << 8),
3165 addr
[2] | (addr
[3] << 8),
3166 addr
[4] | (addr
[5] << 8)
3168 const struct exgmac_reg e
[] = {
3169 { .addr
= 0xe0, ERIAR_MASK_1111
, .val
= w
[0] | (w
[1] << 16) },
3170 { .addr
= 0xe4, ERIAR_MASK_1111
, .val
= w
[2] },
3171 { .addr
= 0xf0, ERIAR_MASK_1111
, .val
= w
[0] << 16 },
3172 { .addr
= 0xf4, ERIAR_MASK_1111
, .val
= w
[1] | (w
[2] << 16) }
3175 rtl_write_exgmac_batch(tp
, e
, ARRAY_SIZE(e
));
3178 static void rtl8168e_2_hw_phy_config(struct rtl8169_private
*tp
)
3180 static const struct phy_reg phy_reg_init
[] = {
3181 /* Enable Delay cap */
3190 /* Channel estimation fine tune */
3207 rtl_apply_firmware(tp
);
3209 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3211 /* For 4-corner performance improve */
3212 rtl_writephy(tp
, 0x1f, 0x0005);
3213 rtl_writephy(tp
, 0x05, 0x8b80);
3214 rtl_w0w1_phy(tp
, 0x17, 0x0006, 0x0000);
3215 rtl_writephy(tp
, 0x1f, 0x0000);
3217 /* PHY auto speed down */
3218 rtl_writephy(tp
, 0x1f, 0x0004);
3219 rtl_writephy(tp
, 0x1f, 0x0007);
3220 rtl_writephy(tp
, 0x1e, 0x002d);
3221 rtl_w0w1_phy(tp
, 0x18, 0x0010, 0x0000);
3222 rtl_writephy(tp
, 0x1f, 0x0002);
3223 rtl_writephy(tp
, 0x1f, 0x0000);
3224 rtl_w0w1_phy(tp
, 0x14, 0x8000, 0x0000);
3226 /* improve 10M EEE waveform */
3227 rtl_writephy(tp
, 0x1f, 0x0005);
3228 rtl_writephy(tp
, 0x05, 0x8b86);
3229 rtl_w0w1_phy(tp
, 0x06, 0x0001, 0x0000);
3230 rtl_writephy(tp
, 0x1f, 0x0000);
3232 /* Improve 2-pair detection performance */
3233 rtl_writephy(tp
, 0x1f, 0x0005);
3234 rtl_writephy(tp
, 0x05, 0x8b85);
3235 rtl_w0w1_phy(tp
, 0x06, 0x4000, 0x0000);
3236 rtl_writephy(tp
, 0x1f, 0x0000);
3239 rtl_w0w1_eri(tp
, 0x1b0, ERIAR_MASK_1111
, 0x0003, 0x0000, ERIAR_EXGMAC
);
3240 rtl_writephy(tp
, 0x1f, 0x0005);
3241 rtl_writephy(tp
, 0x05, 0x8b85);
3242 rtl_w0w1_phy(tp
, 0x06, 0x2000, 0x0000);
3243 rtl_writephy(tp
, 0x1f, 0x0004);
3244 rtl_writephy(tp
, 0x1f, 0x0007);
3245 rtl_writephy(tp
, 0x1e, 0x0020);
3246 rtl_w0w1_phy(tp
, 0x15, 0x0100, 0x0000);
3247 rtl_writephy(tp
, 0x1f, 0x0002);
3248 rtl_writephy(tp
, 0x1f, 0x0000);
3249 rtl_writephy(tp
, 0x0d, 0x0007);
3250 rtl_writephy(tp
, 0x0e, 0x003c);
3251 rtl_writephy(tp
, 0x0d, 0x4007);
3252 rtl_writephy(tp
, 0x0e, 0x0006);
3253 rtl_writephy(tp
, 0x0d, 0x0000);
3256 rtl_writephy(tp
, 0x1f, 0x0003);
3257 rtl_w0w1_phy(tp
, 0x19, 0x0001, 0x0000);
3258 rtl_w0w1_phy(tp
, 0x10, 0x0400, 0x0000);
3259 rtl_writephy(tp
, 0x1f, 0x0000);
3260 rtl_writephy(tp
, 0x1f, 0x0005);
3261 rtl_w0w1_phy(tp
, 0x01, 0x0100, 0x0000);
3262 rtl_writephy(tp
, 0x1f, 0x0000);
3264 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3265 rtl_rar_exgmac_set(tp
, tp
->dev
->dev_addr
);
3268 static void rtl8168f_hw_phy_config(struct rtl8169_private
*tp
)
3270 /* For 4-corner performance improve */
3271 rtl_writephy(tp
, 0x1f, 0x0005);
3272 rtl_writephy(tp
, 0x05, 0x8b80);
3273 rtl_w0w1_phy(tp
, 0x06, 0x0006, 0x0000);
3274 rtl_writephy(tp
, 0x1f, 0x0000);
3276 /* PHY auto speed down */
3277 rtl_writephy(tp
, 0x1f, 0x0007);
3278 rtl_writephy(tp
, 0x1e, 0x002d);
3279 rtl_w0w1_phy(tp
, 0x18, 0x0010, 0x0000);
3280 rtl_writephy(tp
, 0x1f, 0x0000);
3281 rtl_w0w1_phy(tp
, 0x14, 0x8000, 0x0000);
3283 /* Improve 10M EEE waveform */
3284 rtl_writephy(tp
, 0x1f, 0x0005);
3285 rtl_writephy(tp
, 0x05, 0x8b86);
3286 rtl_w0w1_phy(tp
, 0x06, 0x0001, 0x0000);
3287 rtl_writephy(tp
, 0x1f, 0x0000);
3290 static void rtl8168f_1_hw_phy_config(struct rtl8169_private
*tp
)
3292 static const struct phy_reg phy_reg_init
[] = {
3293 /* Channel estimation fine tune */
3298 /* Modify green table for giga & fnet */
3315 /* Modify green table for 10M */
3321 /* Disable hiimpedance detection (RTCT) */
3327 rtl_apply_firmware(tp
);
3329 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3331 rtl8168f_hw_phy_config(tp
);
3333 /* Improve 2-pair detection performance */
3334 rtl_writephy(tp
, 0x1f, 0x0005);
3335 rtl_writephy(tp
, 0x05, 0x8b85);
3336 rtl_w0w1_phy(tp
, 0x06, 0x4000, 0x0000);
3337 rtl_writephy(tp
, 0x1f, 0x0000);
3340 static void rtl8168f_2_hw_phy_config(struct rtl8169_private
*tp
)
3342 rtl_apply_firmware(tp
);
3344 rtl8168f_hw_phy_config(tp
);
3347 static void rtl8411_hw_phy_config(struct rtl8169_private
*tp
)
3349 static const struct phy_reg phy_reg_init
[] = {
3350 /* Channel estimation fine tune */
3355 /* Modify green table for giga & fnet */
3372 /* Modify green table for 10M */
3378 /* Disable hiimpedance detection (RTCT) */
3385 rtl_apply_firmware(tp
);
3387 rtl8168f_hw_phy_config(tp
);
3389 /* Improve 2-pair detection performance */
3390 rtl_writephy(tp
, 0x1f, 0x0005);
3391 rtl_writephy(tp
, 0x05, 0x8b85);
3392 rtl_w0w1_phy(tp
, 0x06, 0x4000, 0x0000);
3393 rtl_writephy(tp
, 0x1f, 0x0000);
3395 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3397 /* Modify green table for giga */
3398 rtl_writephy(tp
, 0x1f, 0x0005);
3399 rtl_writephy(tp
, 0x05, 0x8b54);
3400 rtl_w0w1_phy(tp
, 0x06, 0x0000, 0x0800);
3401 rtl_writephy(tp
, 0x05, 0x8b5d);
3402 rtl_w0w1_phy(tp
, 0x06, 0x0000, 0x0800);
3403 rtl_writephy(tp
, 0x05, 0x8a7c);
3404 rtl_w0w1_phy(tp
, 0x06, 0x0000, 0x0100);
3405 rtl_writephy(tp
, 0x05, 0x8a7f);
3406 rtl_w0w1_phy(tp
, 0x06, 0x0100, 0x0000);
3407 rtl_writephy(tp
, 0x05, 0x8a82);
3408 rtl_w0w1_phy(tp
, 0x06, 0x0000, 0x0100);
3409 rtl_writephy(tp
, 0x05, 0x8a85);
3410 rtl_w0w1_phy(tp
, 0x06, 0x0000, 0x0100);
3411 rtl_writephy(tp
, 0x05, 0x8a88);
3412 rtl_w0w1_phy(tp
, 0x06, 0x0000, 0x0100);
3413 rtl_writephy(tp
, 0x1f, 0x0000);
3415 /* uc same-seed solution */
3416 rtl_writephy(tp
, 0x1f, 0x0005);
3417 rtl_writephy(tp
, 0x05, 0x8b85);
3418 rtl_w0w1_phy(tp
, 0x06, 0x8000, 0x0000);
3419 rtl_writephy(tp
, 0x1f, 0x0000);
3422 rtl_w0w1_eri(tp
, 0x1b0, ERIAR_MASK_0001
, 0x00, 0x03, ERIAR_EXGMAC
);
3423 rtl_writephy(tp
, 0x1f, 0x0005);
3424 rtl_writephy(tp
, 0x05, 0x8b85);
3425 rtl_w0w1_phy(tp
, 0x06, 0x0000, 0x2000);
3426 rtl_writephy(tp
, 0x1f, 0x0004);
3427 rtl_writephy(tp
, 0x1f, 0x0007);
3428 rtl_writephy(tp
, 0x1e, 0x0020);
3429 rtl_w0w1_phy(tp
, 0x15, 0x0000, 0x0100);
3430 rtl_writephy(tp
, 0x1f, 0x0000);
3431 rtl_writephy(tp
, 0x0d, 0x0007);
3432 rtl_writephy(tp
, 0x0e, 0x003c);
3433 rtl_writephy(tp
, 0x0d, 0x4007);
3434 rtl_writephy(tp
, 0x0e, 0x0000);
3435 rtl_writephy(tp
, 0x0d, 0x0000);
3438 rtl_writephy(tp
, 0x1f, 0x0003);
3439 rtl_w0w1_phy(tp
, 0x19, 0x0000, 0x0001);
3440 rtl_w0w1_phy(tp
, 0x10, 0x0000, 0x0400);
3441 rtl_writephy(tp
, 0x1f, 0x0000);
3444 static void rtl8168g_1_hw_phy_config(struct rtl8169_private
*tp
)
3446 rtl_apply_firmware(tp
);
3448 rtl_writephy(tp
, 0x1f, 0x0a46);
3449 if (rtl_readphy(tp
, 0x10) & 0x0100) {
3450 rtl_writephy(tp
, 0x1f, 0x0bcc);
3451 rtl_w0w1_phy(tp
, 0x12, 0x0000, 0x8000);
3453 rtl_writephy(tp
, 0x1f, 0x0bcc);
3454 rtl_w0w1_phy(tp
, 0x12, 0x8000, 0x0000);
3457 rtl_writephy(tp
, 0x1f, 0x0a46);
3458 if (rtl_readphy(tp
, 0x13) & 0x0100) {
3459 rtl_writephy(tp
, 0x1f, 0x0c41);
3460 rtl_w0w1_phy(tp
, 0x15, 0x0002, 0x0000);
3462 rtl_writephy(tp
, 0x1f, 0x0c41);
3463 rtl_w0w1_phy(tp
, 0x15, 0x0000, 0x0002);
3466 /* Enable PHY auto speed down */
3467 rtl_writephy(tp
, 0x1f, 0x0a44);
3468 rtl_w0w1_phy(tp
, 0x11, 0x000c, 0x0000);
3470 rtl_writephy(tp
, 0x1f, 0x0bcc);
3471 rtl_w0w1_phy(tp
, 0x14, 0x0100, 0x0000);
3472 rtl_writephy(tp
, 0x1f, 0x0a44);
3473 rtl_w0w1_phy(tp
, 0x11, 0x00c0, 0x0000);
3474 rtl_writephy(tp
, 0x1f, 0x0a43);
3475 rtl_writephy(tp
, 0x13, 0x8084);
3476 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x6000);
3477 rtl_w0w1_phy(tp
, 0x10, 0x1003, 0x0000);
3479 /* EEE auto-fallback function */
3480 rtl_writephy(tp
, 0x1f, 0x0a4b);
3481 rtl_w0w1_phy(tp
, 0x11, 0x0004, 0x0000);
3483 /* Enable UC LPF tune function */
3484 rtl_writephy(tp
, 0x1f, 0x0a43);
3485 rtl_writephy(tp
, 0x13, 0x8012);
3486 rtl_w0w1_phy(tp
, 0x14, 0x8000, 0x0000);
3488 rtl_writephy(tp
, 0x1f, 0x0c42);
3489 rtl_w0w1_phy(tp
, 0x11, 0x4000, 0x2000);
3491 /* Improve SWR Efficiency */
3492 rtl_writephy(tp
, 0x1f, 0x0bcd);
3493 rtl_writephy(tp
, 0x14, 0x5065);
3494 rtl_writephy(tp
, 0x14, 0xd065);
3495 rtl_writephy(tp
, 0x1f, 0x0bc8);
3496 rtl_writephy(tp
, 0x11, 0x5655);
3497 rtl_writephy(tp
, 0x1f, 0x0bcd);
3498 rtl_writephy(tp
, 0x14, 0x1065);
3499 rtl_writephy(tp
, 0x14, 0x9065);
3500 rtl_writephy(tp
, 0x14, 0x1065);
3502 /* Check ALDPS bit, disable it if enabled */
3503 rtl_writephy(tp
, 0x1f, 0x0a43);
3504 if (rtl_readphy(tp
, 0x10) & 0x0004)
3505 rtl_w0w1_phy(tp
, 0x10, 0x0000, 0x0004);
3507 rtl_writephy(tp
, 0x1f, 0x0000);
3510 static void rtl8168g_2_hw_phy_config(struct rtl8169_private
*tp
)
3512 rtl_apply_firmware(tp
);
3515 static void rtl8168h_1_hw_phy_config(struct rtl8169_private
*tp
)
3520 rtl_apply_firmware(tp
);
3522 /* CHN EST parameters adjust - giga master */
3523 rtl_writephy(tp
, 0x1f, 0x0a43);
3524 rtl_writephy(tp
, 0x13, 0x809b);
3525 rtl_w0w1_phy(tp
, 0x14, 0x8000, 0xf800);
3526 rtl_writephy(tp
, 0x13, 0x80a2);
3527 rtl_w0w1_phy(tp
, 0x14, 0x8000, 0xff00);
3528 rtl_writephy(tp
, 0x13, 0x80a4);
3529 rtl_w0w1_phy(tp
, 0x14, 0x8500, 0xff00);
3530 rtl_writephy(tp
, 0x13, 0x809c);
3531 rtl_w0w1_phy(tp
, 0x14, 0xbd00, 0xff00);
3532 rtl_writephy(tp
, 0x1f, 0x0000);
3534 /* CHN EST parameters adjust - giga slave */
3535 rtl_writephy(tp
, 0x1f, 0x0a43);
3536 rtl_writephy(tp
, 0x13, 0x80ad);
3537 rtl_w0w1_phy(tp
, 0x14, 0x7000, 0xf800);
3538 rtl_writephy(tp
, 0x13, 0x80b4);
3539 rtl_w0w1_phy(tp
, 0x14, 0x5000, 0xff00);
3540 rtl_writephy(tp
, 0x13, 0x80ac);
3541 rtl_w0w1_phy(tp
, 0x14, 0x4000, 0xff00);
3542 rtl_writephy(tp
, 0x1f, 0x0000);
3544 /* CHN EST parameters adjust - fnet */
3545 rtl_writephy(tp
, 0x1f, 0x0a43);
3546 rtl_writephy(tp
, 0x13, 0x808e);
3547 rtl_w0w1_phy(tp
, 0x14, 0x1200, 0xff00);
3548 rtl_writephy(tp
, 0x13, 0x8090);
3549 rtl_w0w1_phy(tp
, 0x14, 0xe500, 0xff00);
3550 rtl_writephy(tp
, 0x13, 0x8092);
3551 rtl_w0w1_phy(tp
, 0x14, 0x9f00, 0xff00);
3552 rtl_writephy(tp
, 0x1f, 0x0000);
3554 /* enable R-tune & PGA-retune function */
3556 rtl_writephy(tp
, 0x1f, 0x0a46);
3557 data
= rtl_readphy(tp
, 0x13);
3560 dout_tapbin
|= data
;
3561 data
= rtl_readphy(tp
, 0x12);
3564 dout_tapbin
|= data
;
3565 dout_tapbin
= ~(dout_tapbin
^0x08);
3567 dout_tapbin
&= 0xf000;
3568 rtl_writephy(tp
, 0x1f, 0x0a43);
3569 rtl_writephy(tp
, 0x13, 0x827a);
3570 rtl_w0w1_phy(tp
, 0x14, dout_tapbin
, 0xf000);
3571 rtl_writephy(tp
, 0x13, 0x827b);
3572 rtl_w0w1_phy(tp
, 0x14, dout_tapbin
, 0xf000);
3573 rtl_writephy(tp
, 0x13, 0x827c);
3574 rtl_w0w1_phy(tp
, 0x14, dout_tapbin
, 0xf000);
3575 rtl_writephy(tp
, 0x13, 0x827d);
3576 rtl_w0w1_phy(tp
, 0x14, dout_tapbin
, 0xf000);
3578 rtl_writephy(tp
, 0x1f, 0x0a43);
3579 rtl_writephy(tp
, 0x13, 0x0811);
3580 rtl_w0w1_phy(tp
, 0x14, 0x0800, 0x0000);
3581 rtl_writephy(tp
, 0x1f, 0x0a42);
3582 rtl_w0w1_phy(tp
, 0x16, 0x0002, 0x0000);
3583 rtl_writephy(tp
, 0x1f, 0x0000);
3585 /* enable GPHY 10M */
3586 rtl_writephy(tp
, 0x1f, 0x0a44);
3587 rtl_w0w1_phy(tp
, 0x11, 0x0800, 0x0000);
3588 rtl_writephy(tp
, 0x1f, 0x0000);
3590 /* SAR ADC performance */
3591 rtl_writephy(tp
, 0x1f, 0x0bca);
3592 rtl_w0w1_phy(tp
, 0x17, 0x4000, 0x3000);
3593 rtl_writephy(tp
, 0x1f, 0x0000);
3595 rtl_writephy(tp
, 0x1f, 0x0a43);
3596 rtl_writephy(tp
, 0x13, 0x803f);
3597 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x3000);
3598 rtl_writephy(tp
, 0x13, 0x8047);
3599 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x3000);
3600 rtl_writephy(tp
, 0x13, 0x804f);
3601 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x3000);
3602 rtl_writephy(tp
, 0x13, 0x8057);
3603 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x3000);
3604 rtl_writephy(tp
, 0x13, 0x805f);
3605 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x3000);
3606 rtl_writephy(tp
, 0x13, 0x8067);
3607 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x3000);
3608 rtl_writephy(tp
, 0x13, 0x806f);
3609 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x3000);
3610 rtl_writephy(tp
, 0x1f, 0x0000);
3612 /* disable phy pfm mode */
3613 rtl_writephy(tp
, 0x1f, 0x0a44);
3614 rtl_w0w1_phy(tp
, 0x11, 0x0000, 0x0080);
3615 rtl_writephy(tp
, 0x1f, 0x0000);
3617 /* Check ALDPS bit, disable it if enabled */
3618 rtl_writephy(tp
, 0x1f, 0x0a43);
3619 if (rtl_readphy(tp
, 0x10) & 0x0004)
3620 rtl_w0w1_phy(tp
, 0x10, 0x0000, 0x0004);
3622 rtl_writephy(tp
, 0x1f, 0x0000);
3625 static void rtl8168h_2_hw_phy_config(struct rtl8169_private
*tp
)
3627 u16 ioffset_p3
, ioffset_p2
, ioffset_p1
, ioffset_p0
;
3631 rtl_apply_firmware(tp
);
3633 /* CHIN EST parameter update */
3634 rtl_writephy(tp
, 0x1f, 0x0a43);
3635 rtl_writephy(tp
, 0x13, 0x808a);
3636 rtl_w0w1_phy(tp
, 0x14, 0x000a, 0x003f);
3637 rtl_writephy(tp
, 0x1f, 0x0000);
3639 /* enable R-tune & PGA-retune function */
3640 rtl_writephy(tp
, 0x1f, 0x0a43);
3641 rtl_writephy(tp
, 0x13, 0x0811);
3642 rtl_w0w1_phy(tp
, 0x14, 0x0800, 0x0000);
3643 rtl_writephy(tp
, 0x1f, 0x0a42);
3644 rtl_w0w1_phy(tp
, 0x16, 0x0002, 0x0000);
3645 rtl_writephy(tp
, 0x1f, 0x0000);
3647 /* enable GPHY 10M */
3648 rtl_writephy(tp
, 0x1f, 0x0a44);
3649 rtl_w0w1_phy(tp
, 0x11, 0x0800, 0x0000);
3650 rtl_writephy(tp
, 0x1f, 0x0000);
3652 r8168_mac_ocp_write(tp
, 0xdd02, 0x807d);
3653 data
= r8168_mac_ocp_read(tp
, 0xdd02);
3654 ioffset_p3
= ((data
& 0x80)>>7);
3657 data
= r8168_mac_ocp_read(tp
, 0xdd00);
3658 ioffset_p3
|= ((data
& (0xe000))>>13);
3659 ioffset_p2
= ((data
& (0x1e00))>>9);
3660 ioffset_p1
= ((data
& (0x01e0))>>5);
3661 ioffset_p0
= ((data
& 0x0010)>>4);
3663 ioffset_p0
|= (data
& (0x07));
3664 data
= (ioffset_p3
<<12)|(ioffset_p2
<<8)|(ioffset_p1
<<4)|(ioffset_p0
);
3666 if ((ioffset_p3
!= 0x0f) || (ioffset_p2
!= 0x0f) ||
3667 (ioffset_p1
!= 0x0f) || (ioffset_p0
!= 0x0f)) {
3668 rtl_writephy(tp
, 0x1f, 0x0bcf);
3669 rtl_writephy(tp
, 0x16, data
);
3670 rtl_writephy(tp
, 0x1f, 0x0000);
3673 /* Modify rlen (TX LPF corner frequency) level */
3674 rtl_writephy(tp
, 0x1f, 0x0bcd);
3675 data
= rtl_readphy(tp
, 0x16);
3680 data
= rlen
| (rlen
<<4) | (rlen
<<8) | (rlen
<<12);
3681 rtl_writephy(tp
, 0x17, data
);
3682 rtl_writephy(tp
, 0x1f, 0x0bcd);
3683 rtl_writephy(tp
, 0x1f, 0x0000);
3685 /* disable phy pfm mode */
3686 rtl_writephy(tp
, 0x1f, 0x0a44);
3687 rtl_w0w1_phy(tp
, 0x11, 0x0000, 0x0080);
3688 rtl_writephy(tp
, 0x1f, 0x0000);
3690 /* Check ALDPS bit, disable it if enabled */
3691 rtl_writephy(tp
, 0x1f, 0x0a43);
3692 if (rtl_readphy(tp
, 0x10) & 0x0004)
3693 rtl_w0w1_phy(tp
, 0x10, 0x0000, 0x0004);
3695 rtl_writephy(tp
, 0x1f, 0x0000);
3698 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private
*tp
)
3700 /* Enable PHY auto speed down */
3701 rtl_writephy(tp
, 0x1f, 0x0a44);
3702 rtl_w0w1_phy(tp
, 0x11, 0x000c, 0x0000);
3703 rtl_writephy(tp
, 0x1f, 0x0000);
3705 /* patch 10M & ALDPS */
3706 rtl_writephy(tp
, 0x1f, 0x0bcc);
3707 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x0100);
3708 rtl_writephy(tp
, 0x1f, 0x0a44);
3709 rtl_w0w1_phy(tp
, 0x11, 0x00c0, 0x0000);
3710 rtl_writephy(tp
, 0x1f, 0x0a43);
3711 rtl_writephy(tp
, 0x13, 0x8084);
3712 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x6000);
3713 rtl_w0w1_phy(tp
, 0x10, 0x1003, 0x0000);
3714 rtl_writephy(tp
, 0x1f, 0x0000);
3716 /* Enable EEE auto-fallback function */
3717 rtl_writephy(tp
, 0x1f, 0x0a4b);
3718 rtl_w0w1_phy(tp
, 0x11, 0x0004, 0x0000);
3719 rtl_writephy(tp
, 0x1f, 0x0000);
3721 /* Enable UC LPF tune function */
3722 rtl_writephy(tp
, 0x1f, 0x0a43);
3723 rtl_writephy(tp
, 0x13, 0x8012);
3724 rtl_w0w1_phy(tp
, 0x14, 0x8000, 0x0000);
3725 rtl_writephy(tp
, 0x1f, 0x0000);
3727 /* set rg_sel_sdm_rate */
3728 rtl_writephy(tp
, 0x1f, 0x0c42);
3729 rtl_w0w1_phy(tp
, 0x11, 0x4000, 0x2000);
3730 rtl_writephy(tp
, 0x1f, 0x0000);
3732 /* Check ALDPS bit, disable it if enabled */
3733 rtl_writephy(tp
, 0x1f, 0x0a43);
3734 if (rtl_readphy(tp
, 0x10) & 0x0004)
3735 rtl_w0w1_phy(tp
, 0x10, 0x0000, 0x0004);
3737 rtl_writephy(tp
, 0x1f, 0x0000);
3740 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private
*tp
)
3742 /* patch 10M & ALDPS */
3743 rtl_writephy(tp
, 0x1f, 0x0bcc);
3744 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x0100);
3745 rtl_writephy(tp
, 0x1f, 0x0a44);
3746 rtl_w0w1_phy(tp
, 0x11, 0x00c0, 0x0000);
3747 rtl_writephy(tp
, 0x1f, 0x0a43);
3748 rtl_writephy(tp
, 0x13, 0x8084);
3749 rtl_w0w1_phy(tp
, 0x14, 0x0000, 0x6000);
3750 rtl_w0w1_phy(tp
, 0x10, 0x1003, 0x0000);
3751 rtl_writephy(tp
, 0x1f, 0x0000);
3753 /* Enable UC LPF tune function */
3754 rtl_writephy(tp
, 0x1f, 0x0a43);
3755 rtl_writephy(tp
, 0x13, 0x8012);
3756 rtl_w0w1_phy(tp
, 0x14, 0x8000, 0x0000);
3757 rtl_writephy(tp
, 0x1f, 0x0000);
3759 /* Set rg_sel_sdm_rate */
3760 rtl_writephy(tp
, 0x1f, 0x0c42);
3761 rtl_w0w1_phy(tp
, 0x11, 0x4000, 0x2000);
3762 rtl_writephy(tp
, 0x1f, 0x0000);
3764 /* Channel estimation parameters */
3765 rtl_writephy(tp
, 0x1f, 0x0a43);
3766 rtl_writephy(tp
, 0x13, 0x80f3);
3767 rtl_w0w1_phy(tp
, 0x14, 0x8b00, ~0x8bff);
3768 rtl_writephy(tp
, 0x13, 0x80f0);
3769 rtl_w0w1_phy(tp
, 0x14, 0x3a00, ~0x3aff);
3770 rtl_writephy(tp
, 0x13, 0x80ef);
3771 rtl_w0w1_phy(tp
, 0x14, 0x0500, ~0x05ff);
3772 rtl_writephy(tp
, 0x13, 0x80f6);
3773 rtl_w0w1_phy(tp
, 0x14, 0x6e00, ~0x6eff);
3774 rtl_writephy(tp
, 0x13, 0x80ec);
3775 rtl_w0w1_phy(tp
, 0x14, 0x6800, ~0x68ff);
3776 rtl_writephy(tp
, 0x13, 0x80ed);
3777 rtl_w0w1_phy(tp
, 0x14, 0x7c00, ~0x7cff);
3778 rtl_writephy(tp
, 0x13, 0x80f2);
3779 rtl_w0w1_phy(tp
, 0x14, 0xf400, ~0xf4ff);
3780 rtl_writephy(tp
, 0x13, 0x80f4);
3781 rtl_w0w1_phy(tp
, 0x14, 0x8500, ~0x85ff);
3782 rtl_writephy(tp
, 0x1f, 0x0a43);
3783 rtl_writephy(tp
, 0x13, 0x8110);
3784 rtl_w0w1_phy(tp
, 0x14, 0xa800, ~0xa8ff);
3785 rtl_writephy(tp
, 0x13, 0x810f);
3786 rtl_w0w1_phy(tp
, 0x14, 0x1d00, ~0x1dff);
3787 rtl_writephy(tp
, 0x13, 0x8111);
3788 rtl_w0w1_phy(tp
, 0x14, 0xf500, ~0xf5ff);
3789 rtl_writephy(tp
, 0x13, 0x8113);
3790 rtl_w0w1_phy(tp
, 0x14, 0x6100, ~0x61ff);
3791 rtl_writephy(tp
, 0x13, 0x8115);
3792 rtl_w0w1_phy(tp
, 0x14, 0x9200, ~0x92ff);
3793 rtl_writephy(tp
, 0x13, 0x810e);
3794 rtl_w0w1_phy(tp
, 0x14, 0x0400, ~0x04ff);
3795 rtl_writephy(tp
, 0x13, 0x810c);
3796 rtl_w0w1_phy(tp
, 0x14, 0x7c00, ~0x7cff);
3797 rtl_writephy(tp
, 0x13, 0x810b);
3798 rtl_w0w1_phy(tp
, 0x14, 0x5a00, ~0x5aff);
3799 rtl_writephy(tp
, 0x1f, 0x0a43);
3800 rtl_writephy(tp
, 0x13, 0x80d1);
3801 rtl_w0w1_phy(tp
, 0x14, 0xff00, ~0xffff);
3802 rtl_writephy(tp
, 0x13, 0x80cd);
3803 rtl_w0w1_phy(tp
, 0x14, 0x9e00, ~0x9eff);
3804 rtl_writephy(tp
, 0x13, 0x80d3);
3805 rtl_w0w1_phy(tp
, 0x14, 0x0e00, ~0x0eff);
3806 rtl_writephy(tp
, 0x13, 0x80d5);
3807 rtl_w0w1_phy(tp
, 0x14, 0xca00, ~0xcaff);
3808 rtl_writephy(tp
, 0x13, 0x80d7);
3809 rtl_w0w1_phy(tp
, 0x14, 0x8400, ~0x84ff);
3811 /* Force PWM-mode */
3812 rtl_writephy(tp
, 0x1f, 0x0bcd);
3813 rtl_writephy(tp
, 0x14, 0x5065);
3814 rtl_writephy(tp
, 0x14, 0xd065);
3815 rtl_writephy(tp
, 0x1f, 0x0bc8);
3816 rtl_writephy(tp
, 0x12, 0x00ed);
3817 rtl_writephy(tp
, 0x1f, 0x0bcd);
3818 rtl_writephy(tp
, 0x14, 0x1065);
3819 rtl_writephy(tp
, 0x14, 0x9065);
3820 rtl_writephy(tp
, 0x14, 0x1065);
3821 rtl_writephy(tp
, 0x1f, 0x0000);
3823 /* Check ALDPS bit, disable it if enabled */
3824 rtl_writephy(tp
, 0x1f, 0x0a43);
3825 if (rtl_readphy(tp
, 0x10) & 0x0004)
3826 rtl_w0w1_phy(tp
, 0x10, 0x0000, 0x0004);
3828 rtl_writephy(tp
, 0x1f, 0x0000);
3831 static void rtl8102e_hw_phy_config(struct rtl8169_private
*tp
)
3833 static const struct phy_reg phy_reg_init
[] = {
3840 rtl_writephy(tp
, 0x1f, 0x0000);
3841 rtl_patchphy(tp
, 0x11, 1 << 12);
3842 rtl_patchphy(tp
, 0x19, 1 << 13);
3843 rtl_patchphy(tp
, 0x10, 1 << 15);
3845 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3848 static void rtl8105e_hw_phy_config(struct rtl8169_private
*tp
)
3850 static const struct phy_reg phy_reg_init
[] = {
3864 /* Disable ALDPS before ram code */
3865 rtl_writephy(tp
, 0x1f, 0x0000);
3866 rtl_writephy(tp
, 0x18, 0x0310);
3869 rtl_apply_firmware(tp
);
3871 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3874 static void rtl8402_hw_phy_config(struct rtl8169_private
*tp
)
3876 /* Disable ALDPS before setting firmware */
3877 rtl_writephy(tp
, 0x1f, 0x0000);
3878 rtl_writephy(tp
, 0x18, 0x0310);
3881 rtl_apply_firmware(tp
);
3884 rtl_eri_write(tp
, 0x1b0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
3885 rtl_writephy(tp
, 0x1f, 0x0004);
3886 rtl_writephy(tp
, 0x10, 0x401f);
3887 rtl_writephy(tp
, 0x19, 0x7030);
3888 rtl_writephy(tp
, 0x1f, 0x0000);
3891 static void rtl8106e_hw_phy_config(struct rtl8169_private
*tp
)
3893 static const struct phy_reg phy_reg_init
[] = {
3900 /* Disable ALDPS before ram code */
3901 rtl_writephy(tp
, 0x1f, 0x0000);
3902 rtl_writephy(tp
, 0x18, 0x0310);
3905 rtl_apply_firmware(tp
);
3907 rtl_eri_write(tp
, 0x1b0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
3908 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
3910 rtl_eri_write(tp
, 0x1d0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
3913 static void rtl_hw_phy_config(struct net_device
*dev
)
3915 struct rtl8169_private
*tp
= netdev_priv(dev
);
3917 rtl8169_print_mac_version(tp
);
3919 switch (tp
->mac_version
) {
3920 case RTL_GIGA_MAC_VER_01
:
3922 case RTL_GIGA_MAC_VER_02
:
3923 case RTL_GIGA_MAC_VER_03
:
3924 rtl8169s_hw_phy_config(tp
);
3926 case RTL_GIGA_MAC_VER_04
:
3927 rtl8169sb_hw_phy_config(tp
);
3929 case RTL_GIGA_MAC_VER_05
:
3930 rtl8169scd_hw_phy_config(tp
);
3932 case RTL_GIGA_MAC_VER_06
:
3933 rtl8169sce_hw_phy_config(tp
);
3935 case RTL_GIGA_MAC_VER_07
:
3936 case RTL_GIGA_MAC_VER_08
:
3937 case RTL_GIGA_MAC_VER_09
:
3938 rtl8102e_hw_phy_config(tp
);
3940 case RTL_GIGA_MAC_VER_11
:
3941 rtl8168bb_hw_phy_config(tp
);
3943 case RTL_GIGA_MAC_VER_12
:
3944 rtl8168bef_hw_phy_config(tp
);
3946 case RTL_GIGA_MAC_VER_17
:
3947 rtl8168bef_hw_phy_config(tp
);
3949 case RTL_GIGA_MAC_VER_18
:
3950 rtl8168cp_1_hw_phy_config(tp
);
3952 case RTL_GIGA_MAC_VER_19
:
3953 rtl8168c_1_hw_phy_config(tp
);
3955 case RTL_GIGA_MAC_VER_20
:
3956 rtl8168c_2_hw_phy_config(tp
);
3958 case RTL_GIGA_MAC_VER_21
:
3959 rtl8168c_3_hw_phy_config(tp
);
3961 case RTL_GIGA_MAC_VER_22
:
3962 rtl8168c_4_hw_phy_config(tp
);
3964 case RTL_GIGA_MAC_VER_23
:
3965 case RTL_GIGA_MAC_VER_24
:
3966 rtl8168cp_2_hw_phy_config(tp
);
3968 case RTL_GIGA_MAC_VER_25
:
3969 rtl8168d_1_hw_phy_config(tp
);
3971 case RTL_GIGA_MAC_VER_26
:
3972 rtl8168d_2_hw_phy_config(tp
);
3974 case RTL_GIGA_MAC_VER_27
:
3975 rtl8168d_3_hw_phy_config(tp
);
3977 case RTL_GIGA_MAC_VER_28
:
3978 rtl8168d_4_hw_phy_config(tp
);
3980 case RTL_GIGA_MAC_VER_29
:
3981 case RTL_GIGA_MAC_VER_30
:
3982 rtl8105e_hw_phy_config(tp
);
3984 case RTL_GIGA_MAC_VER_31
:
3987 case RTL_GIGA_MAC_VER_32
:
3988 case RTL_GIGA_MAC_VER_33
:
3989 rtl8168e_1_hw_phy_config(tp
);
3991 case RTL_GIGA_MAC_VER_34
:
3992 rtl8168e_2_hw_phy_config(tp
);
3994 case RTL_GIGA_MAC_VER_35
:
3995 rtl8168f_1_hw_phy_config(tp
);
3997 case RTL_GIGA_MAC_VER_36
:
3998 rtl8168f_2_hw_phy_config(tp
);
4001 case RTL_GIGA_MAC_VER_37
:
4002 rtl8402_hw_phy_config(tp
);
4005 case RTL_GIGA_MAC_VER_38
:
4006 rtl8411_hw_phy_config(tp
);
4009 case RTL_GIGA_MAC_VER_39
:
4010 rtl8106e_hw_phy_config(tp
);
4013 case RTL_GIGA_MAC_VER_40
:
4014 rtl8168g_1_hw_phy_config(tp
);
4016 case RTL_GIGA_MAC_VER_42
:
4017 case RTL_GIGA_MAC_VER_43
:
4018 case RTL_GIGA_MAC_VER_44
:
4019 rtl8168g_2_hw_phy_config(tp
);
4021 case RTL_GIGA_MAC_VER_45
:
4022 case RTL_GIGA_MAC_VER_47
:
4023 rtl8168h_1_hw_phy_config(tp
);
4025 case RTL_GIGA_MAC_VER_46
:
4026 case RTL_GIGA_MAC_VER_48
:
4027 rtl8168h_2_hw_phy_config(tp
);
4030 case RTL_GIGA_MAC_VER_49
:
4031 rtl8168ep_1_hw_phy_config(tp
);
4033 case RTL_GIGA_MAC_VER_50
:
4034 case RTL_GIGA_MAC_VER_51
:
4035 rtl8168ep_2_hw_phy_config(tp
);
4038 case RTL_GIGA_MAC_VER_41
:
4044 static void rtl_schedule_task(struct rtl8169_private
*tp
, enum rtl_flag flag
)
4046 if (!test_and_set_bit(flag
, tp
->wk
.flags
))
4047 schedule_work(&tp
->wk
.work
);
4050 static bool rtl_tbi_enabled(struct rtl8169_private
*tp
)
4052 return (tp
->mac_version
== RTL_GIGA_MAC_VER_01
) &&
4053 (RTL_R8(tp
, PHYstatus
) & TBI_Enable
);
4056 static void rtl8169_init_phy(struct net_device
*dev
, struct rtl8169_private
*tp
)
4058 rtl_hw_phy_config(dev
);
4060 if (tp
->mac_version
<= RTL_GIGA_MAC_VER_06
) {
4061 netif_dbg(tp
, drv
, dev
,
4062 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4063 RTL_W8(tp
, 0x82, 0x01);
4066 pci_write_config_byte(tp
->pci_dev
, PCI_LATENCY_TIMER
, 0x40);
4068 if (tp
->mac_version
<= RTL_GIGA_MAC_VER_06
)
4069 pci_write_config_byte(tp
->pci_dev
, PCI_CACHE_LINE_SIZE
, 0x08);
4071 if (tp
->mac_version
== RTL_GIGA_MAC_VER_02
) {
4072 netif_dbg(tp
, drv
, dev
,
4073 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4074 RTL_W8(tp
, 0x82, 0x01);
4075 netif_dbg(tp
, drv
, dev
,
4076 "Set PHY Reg 0x0bh = 0x00h\n");
4077 rtl_writephy(tp
, 0x0b, 0x0000); //w 0x0b 15 0 0
4080 /* We may have called phy_speed_down before */
4081 phy_speed_up(dev
->phydev
);
4083 genphy_soft_reset(dev
->phydev
);
4085 /* It was reported that several chips end up with 10MBit/Half on a
4086 * 1GBit link after resuming from S3. For whatever reason the PHY on
4087 * these chips doesn't properly start a renegotiation when soft-reset.
4088 * Explicitly requesting a renegotiation fixes this.
4090 if (dev
->phydev
->autoneg
== AUTONEG_ENABLE
)
4091 phy_restart_aneg(dev
->phydev
);
4094 static void rtl_rar_set(struct rtl8169_private
*tp
, u8
*addr
)
4098 RTL_W8(tp
, Cfg9346
, Cfg9346_Unlock
);
4100 RTL_W32(tp
, MAC4
, addr
[4] | addr
[5] << 8);
4103 RTL_W32(tp
, MAC0
, addr
[0] | addr
[1] << 8 | addr
[2] << 16 | addr
[3] << 24);
4106 if (tp
->mac_version
== RTL_GIGA_MAC_VER_34
)
4107 rtl_rar_exgmac_set(tp
, addr
);
4109 RTL_W8(tp
, Cfg9346
, Cfg9346_Lock
);
4111 rtl_unlock_work(tp
);
4114 static int rtl_set_mac_address(struct net_device
*dev
, void *p
)
4116 struct rtl8169_private
*tp
= netdev_priv(dev
);
4117 struct device
*d
= tp_to_dev(tp
);
4120 ret
= eth_mac_addr(dev
, p
);
4124 pm_runtime_get_noresume(d
);
4126 if (pm_runtime_active(d
))
4127 rtl_rar_set(tp
, dev
->dev_addr
);
4129 pm_runtime_put_noidle(d
);
4134 static int rtl8169_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
4136 if (!netif_running(dev
))
4139 return phy_mii_ioctl(dev
->phydev
, ifr
, cmd
);
4142 static void rtl_init_mdio_ops(struct rtl8169_private
*tp
)
4144 struct mdio_ops
*ops
= &tp
->mdio_ops
;
4146 switch (tp
->mac_version
) {
4147 case RTL_GIGA_MAC_VER_27
:
4148 ops
->write
= r8168dp_1_mdio_write
;
4149 ops
->read
= r8168dp_1_mdio_read
;
4151 case RTL_GIGA_MAC_VER_28
:
4152 case RTL_GIGA_MAC_VER_31
:
4153 ops
->write
= r8168dp_2_mdio_write
;
4154 ops
->read
= r8168dp_2_mdio_read
;
4156 case RTL_GIGA_MAC_VER_40
... RTL_GIGA_MAC_VER_51
:
4157 ops
->write
= r8168g_mdio_write
;
4158 ops
->read
= r8168g_mdio_read
;
4161 ops
->write
= r8169_mdio_write
;
4162 ops
->read
= r8169_mdio_read
;
4167 static void rtl_wol_suspend_quirk(struct rtl8169_private
*tp
)
4169 switch (tp
->mac_version
) {
4170 case RTL_GIGA_MAC_VER_25
:
4171 case RTL_GIGA_MAC_VER_26
:
4172 case RTL_GIGA_MAC_VER_29
:
4173 case RTL_GIGA_MAC_VER_30
:
4174 case RTL_GIGA_MAC_VER_32
:
4175 case RTL_GIGA_MAC_VER_33
:
4176 case RTL_GIGA_MAC_VER_34
:
4177 case RTL_GIGA_MAC_VER_37
... RTL_GIGA_MAC_VER_51
:
4178 RTL_W32(tp
, RxConfig
, RTL_R32(tp
, RxConfig
) |
4179 AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
);
4186 static bool rtl_wol_pll_power_down(struct rtl8169_private
*tp
)
4188 struct phy_device
*phydev
;
4190 if (!__rtl8169_get_wol(tp
))
4193 /* phydev may not be attached to netdevice */
4194 phydev
= mdiobus_get_phy(tp
->mii_bus
, 0);
4196 phy_speed_down(phydev
, false);
4197 rtl_wol_suspend_quirk(tp
);
4202 static void r8168_pll_power_down(struct rtl8169_private
*tp
)
4204 if (r8168_check_dash(tp
))
4207 if (tp
->mac_version
== RTL_GIGA_MAC_VER_32
||
4208 tp
->mac_version
== RTL_GIGA_MAC_VER_33
)
4209 rtl_ephy_write(tp
, 0x19, 0xff64);
4211 if (rtl_wol_pll_power_down(tp
))
4214 switch (tp
->mac_version
) {
4215 case RTL_GIGA_MAC_VER_25
... RTL_GIGA_MAC_VER_33
:
4216 case RTL_GIGA_MAC_VER_37
:
4217 case RTL_GIGA_MAC_VER_39
:
4218 case RTL_GIGA_MAC_VER_43
:
4219 case RTL_GIGA_MAC_VER_44
:
4220 case RTL_GIGA_MAC_VER_45
:
4221 case RTL_GIGA_MAC_VER_46
:
4222 case RTL_GIGA_MAC_VER_47
:
4223 case RTL_GIGA_MAC_VER_48
:
4224 case RTL_GIGA_MAC_VER_50
:
4225 case RTL_GIGA_MAC_VER_51
:
4226 RTL_W8(tp
, PMCH
, RTL_R8(tp
, PMCH
) & ~0x80);
4228 case RTL_GIGA_MAC_VER_40
:
4229 case RTL_GIGA_MAC_VER_41
:
4230 case RTL_GIGA_MAC_VER_49
:
4231 rtl_w0w1_eri(tp
, 0x1a8, ERIAR_MASK_1111
, 0x00000000,
4232 0xfc000000, ERIAR_EXGMAC
);
4233 RTL_W8(tp
, PMCH
, RTL_R8(tp
, PMCH
) & ~0x80);
4238 static void r8168_pll_power_up(struct rtl8169_private
*tp
)
4240 switch (tp
->mac_version
) {
4241 case RTL_GIGA_MAC_VER_25
... RTL_GIGA_MAC_VER_33
:
4242 case RTL_GIGA_MAC_VER_37
:
4243 case RTL_GIGA_MAC_VER_39
:
4244 case RTL_GIGA_MAC_VER_43
:
4245 RTL_W8(tp
, PMCH
, RTL_R8(tp
, PMCH
) | 0x80);
4247 case RTL_GIGA_MAC_VER_44
:
4248 case RTL_GIGA_MAC_VER_45
:
4249 case RTL_GIGA_MAC_VER_46
:
4250 case RTL_GIGA_MAC_VER_47
:
4251 case RTL_GIGA_MAC_VER_48
:
4252 case RTL_GIGA_MAC_VER_50
:
4253 case RTL_GIGA_MAC_VER_51
:
4254 RTL_W8(tp
, PMCH
, RTL_R8(tp
, PMCH
) | 0xc0);
4256 case RTL_GIGA_MAC_VER_40
:
4257 case RTL_GIGA_MAC_VER_41
:
4258 case RTL_GIGA_MAC_VER_49
:
4259 RTL_W8(tp
, PMCH
, RTL_R8(tp
, PMCH
) | 0xc0);
4260 rtl_w0w1_eri(tp
, 0x1a8, ERIAR_MASK_1111
, 0xfc000000,
4261 0x00000000, ERIAR_EXGMAC
);
4265 phy_resume(tp
->dev
->phydev
);
4266 /* give MAC/PHY some time to resume */
4270 static void rtl_pll_power_down(struct rtl8169_private
*tp
)
4272 switch (tp
->mac_version
) {
4273 case RTL_GIGA_MAC_VER_01
... RTL_GIGA_MAC_VER_06
:
4274 case RTL_GIGA_MAC_VER_13
... RTL_GIGA_MAC_VER_15
:
4277 r8168_pll_power_down(tp
);
4281 static void rtl_pll_power_up(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_up(tp
);
4292 static void rtl_init_rxcfg(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_10
... RTL_GIGA_MAC_VER_17
:
4297 RTL_W32(tp
, RxConfig
, RX_FIFO_THRESH
| RX_DMA_BURST
);
4299 case RTL_GIGA_MAC_VER_18
... RTL_GIGA_MAC_VER_24
:
4300 case RTL_GIGA_MAC_VER_34
... RTL_GIGA_MAC_VER_36
:
4301 case RTL_GIGA_MAC_VER_38
:
4302 RTL_W32(tp
, RxConfig
, RX128_INT_EN
| RX_MULTI_EN
| RX_DMA_BURST
);
4304 case RTL_GIGA_MAC_VER_40
... RTL_GIGA_MAC_VER_51
:
4305 RTL_W32(tp
, RxConfig
, RX128_INT_EN
| RX_MULTI_EN
| RX_DMA_BURST
| RX_EARLY_OFF
);
4308 RTL_W32(tp
, RxConfig
, RX128_INT_EN
| RX_DMA_BURST
);
4313 static void rtl8169_init_ring_indexes(struct rtl8169_private
*tp
)
4315 tp
->dirty_tx
= tp
->cur_tx
= tp
->cur_rx
= 0;
4318 static void rtl_hw_jumbo_enable(struct rtl8169_private
*tp
)
4320 if (tp
->jumbo_ops
.enable
) {
4321 RTL_W8(tp
, Cfg9346
, Cfg9346_Unlock
);
4322 tp
->jumbo_ops
.enable(tp
);
4323 RTL_W8(tp
, Cfg9346
, Cfg9346_Lock
);
4327 static void rtl_hw_jumbo_disable(struct rtl8169_private
*tp
)
4329 if (tp
->jumbo_ops
.disable
) {
4330 RTL_W8(tp
, Cfg9346
, Cfg9346_Unlock
);
4331 tp
->jumbo_ops
.disable(tp
);
4332 RTL_W8(tp
, Cfg9346
, Cfg9346_Lock
);
4336 static void r8168c_hw_jumbo_enable(struct rtl8169_private
*tp
)
4338 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) | Jumbo_En0
);
4339 RTL_W8(tp
, Config4
, RTL_R8(tp
, Config4
) | Jumbo_En1
);
4340 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_512B
);
4343 static void r8168c_hw_jumbo_disable(struct rtl8169_private
*tp
)
4345 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Jumbo_En0
);
4346 RTL_W8(tp
, Config4
, RTL_R8(tp
, Config4
) & ~Jumbo_En1
);
4347 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
4350 static void r8168dp_hw_jumbo_enable(struct rtl8169_private
*tp
)
4352 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) | Jumbo_En0
);
4355 static void r8168dp_hw_jumbo_disable(struct rtl8169_private
*tp
)
4357 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Jumbo_En0
);
4360 static void r8168e_hw_jumbo_enable(struct rtl8169_private
*tp
)
4362 RTL_W8(tp
, MaxTxPacketSize
, 0x3f);
4363 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) | Jumbo_En0
);
4364 RTL_W8(tp
, Config4
, RTL_R8(tp
, Config4
) | 0x01);
4365 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_512B
);
4368 static void r8168e_hw_jumbo_disable(struct rtl8169_private
*tp
)
4370 RTL_W8(tp
, MaxTxPacketSize
, 0x0c);
4371 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Jumbo_En0
);
4372 RTL_W8(tp
, Config4
, RTL_R8(tp
, Config4
) & ~0x01);
4373 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
4376 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private
*tp
)
4378 rtl_tx_performance_tweak(tp
,
4379 PCI_EXP_DEVCTL_READRQ_512B
| PCI_EXP_DEVCTL_NOSNOOP_EN
);
4382 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private
*tp
)
4384 rtl_tx_performance_tweak(tp
,
4385 PCI_EXP_DEVCTL_READRQ_4096B
| PCI_EXP_DEVCTL_NOSNOOP_EN
);
4388 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private
*tp
)
4390 r8168b_0_hw_jumbo_enable(tp
);
4392 RTL_W8(tp
, Config4
, RTL_R8(tp
, Config4
) | (1 << 0));
4395 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private
*tp
)
4397 r8168b_0_hw_jumbo_disable(tp
);
4399 RTL_W8(tp
, Config4
, RTL_R8(tp
, Config4
) & ~(1 << 0));
4402 static void rtl_init_jumbo_ops(struct rtl8169_private
*tp
)
4404 struct jumbo_ops
*ops
= &tp
->jumbo_ops
;
4406 switch (tp
->mac_version
) {
4407 case RTL_GIGA_MAC_VER_11
:
4408 ops
->disable
= r8168b_0_hw_jumbo_disable
;
4409 ops
->enable
= r8168b_0_hw_jumbo_enable
;
4411 case RTL_GIGA_MAC_VER_12
:
4412 case RTL_GIGA_MAC_VER_17
:
4413 ops
->disable
= r8168b_1_hw_jumbo_disable
;
4414 ops
->enable
= r8168b_1_hw_jumbo_enable
;
4416 case RTL_GIGA_MAC_VER_18
: /* Wild guess. Needs info from Realtek. */
4417 case RTL_GIGA_MAC_VER_19
:
4418 case RTL_GIGA_MAC_VER_20
:
4419 case RTL_GIGA_MAC_VER_21
: /* Wild guess. Needs info from Realtek. */
4420 case RTL_GIGA_MAC_VER_22
:
4421 case RTL_GIGA_MAC_VER_23
:
4422 case RTL_GIGA_MAC_VER_24
:
4423 case RTL_GIGA_MAC_VER_25
:
4424 case RTL_GIGA_MAC_VER_26
:
4425 ops
->disable
= r8168c_hw_jumbo_disable
;
4426 ops
->enable
= r8168c_hw_jumbo_enable
;
4428 case RTL_GIGA_MAC_VER_27
:
4429 case RTL_GIGA_MAC_VER_28
:
4430 ops
->disable
= r8168dp_hw_jumbo_disable
;
4431 ops
->enable
= r8168dp_hw_jumbo_enable
;
4433 case RTL_GIGA_MAC_VER_31
: /* Wild guess. Needs info from Realtek. */
4434 case RTL_GIGA_MAC_VER_32
:
4435 case RTL_GIGA_MAC_VER_33
:
4436 case RTL_GIGA_MAC_VER_34
:
4437 ops
->disable
= r8168e_hw_jumbo_disable
;
4438 ops
->enable
= r8168e_hw_jumbo_enable
;
4442 * No action needed for jumbo frames with 8169.
4443 * No jumbo for 810x at all.
4445 case RTL_GIGA_MAC_VER_40
... RTL_GIGA_MAC_VER_51
:
4447 ops
->disable
= NULL
;
4453 DECLARE_RTL_COND(rtl_chipcmd_cond
)
4455 return RTL_R8(tp
, ChipCmd
) & CmdReset
;
4458 static void rtl_hw_reset(struct rtl8169_private
*tp
)
4460 RTL_W8(tp
, ChipCmd
, CmdReset
);
4462 rtl_udelay_loop_wait_low(tp
, &rtl_chipcmd_cond
, 100, 100);
4465 static void rtl_request_uncached_firmware(struct rtl8169_private
*tp
)
4467 struct rtl_fw
*rtl_fw
;
4471 name
= rtl_lookup_firmware_name(tp
);
4473 goto out_no_firmware
;
4475 rtl_fw
= kzalloc(sizeof(*rtl_fw
), GFP_KERNEL
);
4479 rc
= request_firmware(&rtl_fw
->fw
, name
, tp_to_dev(tp
));
4483 rc
= rtl_check_firmware(tp
, rtl_fw
);
4485 goto err_release_firmware
;
4487 tp
->rtl_fw
= rtl_fw
;
4491 err_release_firmware
:
4492 release_firmware(rtl_fw
->fw
);
4496 netif_warn(tp
, ifup
, tp
->dev
, "unable to load firmware patch %s (%d)\n",
4503 static void rtl_request_firmware(struct rtl8169_private
*tp
)
4505 if (IS_ERR(tp
->rtl_fw
))
4506 rtl_request_uncached_firmware(tp
);
4509 static void rtl_rx_close(struct rtl8169_private
*tp
)
4511 RTL_W32(tp
, RxConfig
, RTL_R32(tp
, RxConfig
) & ~RX_CONFIG_ACCEPT_MASK
);
4514 DECLARE_RTL_COND(rtl_npq_cond
)
4516 return RTL_R8(tp
, TxPoll
) & NPQ
;
4519 DECLARE_RTL_COND(rtl_txcfg_empty_cond
)
4521 return RTL_R32(tp
, TxConfig
) & TXCFG_EMPTY
;
4524 static void rtl8169_hw_reset(struct rtl8169_private
*tp
)
4526 /* Disable interrupts */
4527 rtl8169_irq_mask_and_ack(tp
);
4531 switch (tp
->mac_version
) {
4532 case RTL_GIGA_MAC_VER_27
:
4533 case RTL_GIGA_MAC_VER_28
:
4534 case RTL_GIGA_MAC_VER_31
:
4535 rtl_udelay_loop_wait_low(tp
, &rtl_npq_cond
, 20, 42*42);
4537 case RTL_GIGA_MAC_VER_34
... RTL_GIGA_MAC_VER_38
:
4538 case RTL_GIGA_MAC_VER_40
... RTL_GIGA_MAC_VER_51
:
4539 RTL_W8(tp
, ChipCmd
, RTL_R8(tp
, ChipCmd
) | StopReq
);
4540 rtl_udelay_loop_wait_high(tp
, &rtl_txcfg_empty_cond
, 100, 666);
4543 RTL_W8(tp
, ChipCmd
, RTL_R8(tp
, ChipCmd
) | StopReq
);
4551 static void rtl_set_tx_config_registers(struct rtl8169_private
*tp
)
4553 u32 val
= TX_DMA_BURST
<< TxDMAShift
|
4554 InterFrameGap
<< TxInterFrameGapShift
;
4556 if (tp
->mac_version
>= RTL_GIGA_MAC_VER_34
&&
4557 tp
->mac_version
!= RTL_GIGA_MAC_VER_39
)
4558 val
|= TXCFG_AUTO_FIFO
;
4560 RTL_W32(tp
, TxConfig
, val
);
4563 static void rtl_set_rx_max_size(struct rtl8169_private
*tp
)
4565 /* Low hurts. Let's disable the filtering. */
4566 RTL_W16(tp
, RxMaxSize
, R8169_RX_BUF_SIZE
+ 1);
4569 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private
*tp
)
4572 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4573 * register to be written before TxDescAddrLow to work.
4574 * Switching from MMIO to I/O access fixes the issue as well.
4576 RTL_W32(tp
, TxDescStartAddrHigh
, ((u64
) tp
->TxPhyAddr
) >> 32);
4577 RTL_W32(tp
, TxDescStartAddrLow
, ((u64
) tp
->TxPhyAddr
) & DMA_BIT_MASK(32));
4578 RTL_W32(tp
, RxDescAddrHigh
, ((u64
) tp
->RxPhyAddr
) >> 32);
4579 RTL_W32(tp
, RxDescAddrLow
, ((u64
) tp
->RxPhyAddr
) & DMA_BIT_MASK(32));
4582 static void rtl8169_set_magic_reg(struct rtl8169_private
*tp
, unsigned mac_version
)
4584 static const struct rtl_cfg2_info
{
4589 { RTL_GIGA_MAC_VER_05
, PCI_Clock_33MHz
, 0x000fff00 }, // 8110SCd
4590 { RTL_GIGA_MAC_VER_05
, PCI_Clock_66MHz
, 0x000fffff },
4591 { RTL_GIGA_MAC_VER_06
, PCI_Clock_33MHz
, 0x00ffff00 }, // 8110SCe
4592 { RTL_GIGA_MAC_VER_06
, PCI_Clock_66MHz
, 0x00ffffff }
4594 const struct rtl_cfg2_info
*p
= cfg2_info
;
4598 clk
= RTL_R8(tp
, Config2
) & PCI_Clock_66MHz
;
4599 for (i
= 0; i
< ARRAY_SIZE(cfg2_info
); i
++, p
++) {
4600 if ((p
->mac_version
== mac_version
) && (p
->clk
== clk
)) {
4601 RTL_W32(tp
, 0x7c, p
->val
);
4607 static void rtl_set_rx_mode(struct net_device
*dev
)
4609 struct rtl8169_private
*tp
= netdev_priv(dev
);
4610 u32 mc_filter
[2]; /* Multicast hash filter */
4614 if (dev
->flags
& IFF_PROMISC
) {
4615 /* Unconditionally log net taps. */
4616 netif_notice(tp
, link
, dev
, "Promiscuous mode enabled\n");
4618 AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
|
4620 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
4621 } else if ((netdev_mc_count(dev
) > multicast_filter_limit
) ||
4622 (dev
->flags
& IFF_ALLMULTI
)) {
4623 /* Too many to filter perfectly -- accept all multicasts. */
4624 rx_mode
= AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
;
4625 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
4627 struct netdev_hw_addr
*ha
;
4629 rx_mode
= AcceptBroadcast
| AcceptMyPhys
;
4630 mc_filter
[1] = mc_filter
[0] = 0;
4631 netdev_for_each_mc_addr(ha
, dev
) {
4632 int bit_nr
= ether_crc(ETH_ALEN
, ha
->addr
) >> 26;
4633 mc_filter
[bit_nr
>> 5] |= 1 << (bit_nr
& 31);
4634 rx_mode
|= AcceptMulticast
;
4638 if (dev
->features
& NETIF_F_RXALL
)
4639 rx_mode
|= (AcceptErr
| AcceptRunt
);
4641 tmp
= (RTL_R32(tp
, RxConfig
) & ~RX_CONFIG_ACCEPT_MASK
) | rx_mode
;
4643 if (tp
->mac_version
> RTL_GIGA_MAC_VER_06
) {
4644 u32 data
= mc_filter
[0];
4646 mc_filter
[0] = swab32(mc_filter
[1]);
4647 mc_filter
[1] = swab32(data
);
4650 if (tp
->mac_version
== RTL_GIGA_MAC_VER_35
)
4651 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
4653 RTL_W32(tp
, MAR0
+ 4, mc_filter
[1]);
4654 RTL_W32(tp
, MAR0
+ 0, mc_filter
[0]);
4656 RTL_W32(tp
, RxConfig
, tmp
);
4659 static void rtl_hw_start(struct rtl8169_private
*tp
)
4661 RTL_W8(tp
, Cfg9346
, Cfg9346_Unlock
);
4665 rtl_set_rx_max_size(tp
);
4666 rtl_set_rx_tx_desc_registers(tp
);
4667 RTL_W8(tp
, Cfg9346
, Cfg9346_Lock
);
4669 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4670 RTL_R8(tp
, IntrMask
);
4671 RTL_W8(tp
, ChipCmd
, CmdTxEnb
| CmdRxEnb
);
4673 rtl_set_tx_config_registers(tp
);
4675 rtl_set_rx_mode(tp
->dev
);
4676 /* no early-rx interrupts */
4677 RTL_W16(tp
, MultiIntr
, RTL_R16(tp
, MultiIntr
) & 0xf000);
4678 rtl_irq_enable_all(tp
);
4681 static void rtl_hw_start_8169(struct rtl8169_private
*tp
)
4683 if (tp
->mac_version
== RTL_GIGA_MAC_VER_05
)
4684 pci_write_config_byte(tp
->pci_dev
, PCI_CACHE_LINE_SIZE
, 0x08);
4686 RTL_W8(tp
, EarlyTxThres
, NoEarlyTx
);
4688 tp
->cp_cmd
|= PCIMulRW
;
4690 if (tp
->mac_version
== RTL_GIGA_MAC_VER_02
||
4691 tp
->mac_version
== RTL_GIGA_MAC_VER_03
) {
4692 netif_dbg(tp
, drv
, tp
->dev
,
4693 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
4694 tp
->cp_cmd
|= (1 << 14);
4697 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
4699 rtl8169_set_magic_reg(tp
, tp
->mac_version
);
4702 * Undocumented corner. Supposedly:
4703 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4705 RTL_W16(tp
, IntrMitigate
, 0x0000);
4707 RTL_W32(tp
, RxMissed
, 0);
4710 DECLARE_RTL_COND(rtl_csiar_cond
)
4712 return RTL_R32(tp
, CSIAR
) & CSIAR_FLAG
;
4715 static void rtl_csi_write(struct rtl8169_private
*tp
, int addr
, int value
)
4717 u32 func
= PCI_FUNC(tp
->pci_dev
->devfn
);
4719 RTL_W32(tp
, CSIDR
, value
);
4720 RTL_W32(tp
, CSIAR
, CSIAR_WRITE_CMD
| (addr
& CSIAR_ADDR_MASK
) |
4721 CSIAR_BYTE_ENABLE
| func
<< 16);
4723 rtl_udelay_loop_wait_low(tp
, &rtl_csiar_cond
, 10, 100);
4726 static u32
rtl_csi_read(struct rtl8169_private
*tp
, int addr
)
4728 u32 func
= PCI_FUNC(tp
->pci_dev
->devfn
);
4730 RTL_W32(tp
, CSIAR
, (addr
& CSIAR_ADDR_MASK
) | func
<< 16 |
4733 return rtl_udelay_loop_wait_high(tp
, &rtl_csiar_cond
, 10, 100) ?
4734 RTL_R32(tp
, CSIDR
) : ~0;
4737 static void rtl_csi_access_enable(struct rtl8169_private
*tp
, u8 val
)
4739 struct pci_dev
*pdev
= tp
->pci_dev
;
4742 /* According to Realtek the value at config space address 0x070f
4743 * controls the L0s/L1 entrance latency. We try standard ECAM access
4744 * first and if it fails fall back to CSI.
4746 if (pdev
->cfg_size
> 0x070f &&
4747 pci_write_config_byte(pdev
, 0x070f, val
) == PCIBIOS_SUCCESSFUL
)
4750 netdev_notice_once(tp
->dev
,
4751 "No native access to PCI extended config space, falling back to CSI\n");
4752 csi
= rtl_csi_read(tp
, 0x070c) & 0x00ffffff;
4753 rtl_csi_write(tp
, 0x070c, csi
| val
<< 24);
4756 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private
*tp
)
4758 rtl_csi_access_enable(tp
, 0x27);
4762 unsigned int offset
;
4767 static void rtl_ephy_init(struct rtl8169_private
*tp
, const struct ephy_info
*e
,
4773 w
= (rtl_ephy_read(tp
, e
->offset
) & ~e
->mask
) | e
->bits
;
4774 rtl_ephy_write(tp
, e
->offset
, w
);
4779 static void rtl_disable_clock_request(struct rtl8169_private
*tp
)
4781 pcie_capability_clear_word(tp
->pci_dev
, PCI_EXP_LNKCTL
,
4782 PCI_EXP_LNKCTL_CLKREQ_EN
);
4785 static void rtl_enable_clock_request(struct rtl8169_private
*tp
)
4787 pcie_capability_set_word(tp
->pci_dev
, PCI_EXP_LNKCTL
,
4788 PCI_EXP_LNKCTL_CLKREQ_EN
);
4791 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private
*tp
, bool enable
)
4795 data
= RTL_R8(tp
, Config3
);
4800 data
&= ~Rdy_to_L23
;
4802 RTL_W8(tp
, Config3
, data
);
4805 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private
*tp
, bool enable
)
4808 RTL_W8(tp
, Config5
, RTL_R8(tp
, Config5
) | ASPM_en
);
4809 RTL_W8(tp
, Config2
, RTL_R8(tp
, Config2
) | ClkReqEn
);
4811 RTL_W8(tp
, Config2
, RTL_R8(tp
, Config2
) & ~ClkReqEn
);
4812 RTL_W8(tp
, Config5
, RTL_R8(tp
, Config5
) & ~ASPM_en
);
4818 static void rtl_hw_start_8168bb(struct rtl8169_private
*tp
)
4820 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Beacon_en
);
4822 tp
->cp_cmd
&= CPCMD_QUIRK_MASK
;
4823 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
4825 if (tp
->dev
->mtu
<= ETH_DATA_LEN
) {
4826 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
|
4827 PCI_EXP_DEVCTL_NOSNOOP_EN
);
4831 static void rtl_hw_start_8168bef(struct rtl8169_private
*tp
)
4833 rtl_hw_start_8168bb(tp
);
4835 RTL_W8(tp
, MaxTxPacketSize
, TxPacketMax
);
4837 RTL_W8(tp
, Config4
, RTL_R8(tp
, Config4
) & ~(1 << 0));
4840 static void __rtl_hw_start_8168cp(struct rtl8169_private
*tp
)
4842 RTL_W8(tp
, Config1
, RTL_R8(tp
, Config1
) | Speed_down
);
4844 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Beacon_en
);
4846 if (tp
->dev
->mtu
<= ETH_DATA_LEN
)
4847 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
4849 rtl_disable_clock_request(tp
);
4851 tp
->cp_cmd
&= CPCMD_QUIRK_MASK
;
4852 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
4855 static void rtl_hw_start_8168cp_1(struct rtl8169_private
*tp
)
4857 static const struct ephy_info e_info_8168cp
[] = {
4858 { 0x01, 0, 0x0001 },
4859 { 0x02, 0x0800, 0x1000 },
4860 { 0x03, 0, 0x0042 },
4861 { 0x06, 0x0080, 0x0000 },
4865 rtl_set_def_aspm_entry_latency(tp
);
4867 rtl_ephy_init(tp
, e_info_8168cp
, ARRAY_SIZE(e_info_8168cp
));
4869 __rtl_hw_start_8168cp(tp
);
4872 static void rtl_hw_start_8168cp_2(struct rtl8169_private
*tp
)
4874 rtl_set_def_aspm_entry_latency(tp
);
4876 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Beacon_en
);
4878 if (tp
->dev
->mtu
<= ETH_DATA_LEN
)
4879 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
4881 tp
->cp_cmd
&= CPCMD_QUIRK_MASK
;
4882 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
4885 static void rtl_hw_start_8168cp_3(struct rtl8169_private
*tp
)
4887 rtl_set_def_aspm_entry_latency(tp
);
4889 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Beacon_en
);
4892 RTL_W8(tp
, DBG_REG
, 0x20);
4894 RTL_W8(tp
, MaxTxPacketSize
, TxPacketMax
);
4896 if (tp
->dev
->mtu
<= ETH_DATA_LEN
)
4897 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
4899 tp
->cp_cmd
&= CPCMD_QUIRK_MASK
;
4900 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
4903 static void rtl_hw_start_8168c_1(struct rtl8169_private
*tp
)
4905 static const struct ephy_info e_info_8168c_1
[] = {
4906 { 0x02, 0x0800, 0x1000 },
4907 { 0x03, 0, 0x0002 },
4908 { 0x06, 0x0080, 0x0000 }
4911 rtl_set_def_aspm_entry_latency(tp
);
4913 RTL_W8(tp
, DBG_REG
, 0x06 | FIX_NAK_1
| FIX_NAK_2
);
4915 rtl_ephy_init(tp
, e_info_8168c_1
, ARRAY_SIZE(e_info_8168c_1
));
4917 __rtl_hw_start_8168cp(tp
);
4920 static void rtl_hw_start_8168c_2(struct rtl8169_private
*tp
)
4922 static const struct ephy_info e_info_8168c_2
[] = {
4923 { 0x01, 0, 0x0001 },
4924 { 0x03, 0x0400, 0x0220 }
4927 rtl_set_def_aspm_entry_latency(tp
);
4929 rtl_ephy_init(tp
, e_info_8168c_2
, ARRAY_SIZE(e_info_8168c_2
));
4931 __rtl_hw_start_8168cp(tp
);
4934 static void rtl_hw_start_8168c_3(struct rtl8169_private
*tp
)
4936 rtl_hw_start_8168c_2(tp
);
4939 static void rtl_hw_start_8168c_4(struct rtl8169_private
*tp
)
4941 rtl_set_def_aspm_entry_latency(tp
);
4943 __rtl_hw_start_8168cp(tp
);
4946 static void rtl_hw_start_8168d(struct rtl8169_private
*tp
)
4948 rtl_set_def_aspm_entry_latency(tp
);
4950 rtl_disable_clock_request(tp
);
4952 RTL_W8(tp
, MaxTxPacketSize
, TxPacketMax
);
4954 if (tp
->dev
->mtu
<= ETH_DATA_LEN
)
4955 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
4957 tp
->cp_cmd
&= CPCMD_QUIRK_MASK
;
4958 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
4961 static void rtl_hw_start_8168dp(struct rtl8169_private
*tp
)
4963 rtl_set_def_aspm_entry_latency(tp
);
4965 if (tp
->dev
->mtu
<= ETH_DATA_LEN
)
4966 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
4968 RTL_W8(tp
, MaxTxPacketSize
, TxPacketMax
);
4970 rtl_disable_clock_request(tp
);
4973 static void rtl_hw_start_8168d_4(struct rtl8169_private
*tp
)
4975 static const struct ephy_info e_info_8168d_4
[] = {
4976 { 0x0b, 0x0000, 0x0048 },
4977 { 0x19, 0x0020, 0x0050 },
4978 { 0x0c, 0x0100, 0x0020 }
4981 rtl_set_def_aspm_entry_latency(tp
);
4983 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
4985 RTL_W8(tp
, MaxTxPacketSize
, TxPacketMax
);
4987 rtl_ephy_init(tp
, e_info_8168d_4
, ARRAY_SIZE(e_info_8168d_4
));
4989 rtl_enable_clock_request(tp
);
4992 static void rtl_hw_start_8168e_1(struct rtl8169_private
*tp
)
4994 static const struct ephy_info e_info_8168e_1
[] = {
4995 { 0x00, 0x0200, 0x0100 },
4996 { 0x00, 0x0000, 0x0004 },
4997 { 0x06, 0x0002, 0x0001 },
4998 { 0x06, 0x0000, 0x0030 },
4999 { 0x07, 0x0000, 0x2000 },
5000 { 0x00, 0x0000, 0x0020 },
5001 { 0x03, 0x5800, 0x2000 },
5002 { 0x03, 0x0000, 0x0001 },
5003 { 0x01, 0x0800, 0x1000 },
5004 { 0x07, 0x0000, 0x4000 },
5005 { 0x1e, 0x0000, 0x2000 },
5006 { 0x19, 0xffff, 0xfe6c },
5007 { 0x0a, 0x0000, 0x0040 }
5010 rtl_set_def_aspm_entry_latency(tp
);
5012 rtl_ephy_init(tp
, e_info_8168e_1
, ARRAY_SIZE(e_info_8168e_1
));
5014 if (tp
->dev
->mtu
<= ETH_DATA_LEN
)
5015 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5017 RTL_W8(tp
, MaxTxPacketSize
, TxPacketMax
);
5019 rtl_disable_clock_request(tp
);
5021 /* Reset tx FIFO pointer */
5022 RTL_W32(tp
, MISC
, RTL_R32(tp
, MISC
) | TXPLA_RST
);
5023 RTL_W32(tp
, MISC
, RTL_R32(tp
, MISC
) & ~TXPLA_RST
);
5025 RTL_W8(tp
, Config5
, RTL_R8(tp
, Config5
) & ~Spi_en
);
5028 static void rtl_hw_start_8168e_2(struct rtl8169_private
*tp
)
5030 static const struct ephy_info e_info_8168e_2
[] = {
5031 { 0x09, 0x0000, 0x0080 },
5032 { 0x19, 0x0000, 0x0224 }
5035 rtl_set_def_aspm_entry_latency(tp
);
5037 rtl_ephy_init(tp
, e_info_8168e_2
, ARRAY_SIZE(e_info_8168e_2
));
5039 if (tp
->dev
->mtu
<= ETH_DATA_LEN
)
5040 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5042 rtl_eri_write(tp
, 0xc0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5043 rtl_eri_write(tp
, 0xb8, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5044 rtl_eri_write(tp
, 0xc8, ERIAR_MASK_1111
, 0x00100002, ERIAR_EXGMAC
);
5045 rtl_eri_write(tp
, 0xe8, ERIAR_MASK_1111
, 0x00100006, ERIAR_EXGMAC
);
5046 rtl_eri_write(tp
, 0xcc, ERIAR_MASK_1111
, 0x00000050, ERIAR_EXGMAC
);
5047 rtl_eri_write(tp
, 0xd0, ERIAR_MASK_1111
, 0x07ff0060, ERIAR_EXGMAC
);
5048 rtl_w0w1_eri(tp
, 0x1b0, ERIAR_MASK_0001
, 0x10, 0x00, ERIAR_EXGMAC
);
5049 rtl_w0w1_eri(tp
, 0x0d4, ERIAR_MASK_0011
, 0x0c00, 0xff00, ERIAR_EXGMAC
);
5051 RTL_W8(tp
, MaxTxPacketSize
, EarlySize
);
5053 rtl_disable_clock_request(tp
);
5055 RTL_W8(tp
, MCU
, RTL_R8(tp
, MCU
) & ~NOW_IS_OOB
);
5057 /* Adjust EEE LED frequency */
5058 RTL_W8(tp
, EEE_LED
, RTL_R8(tp
, EEE_LED
) & ~0x07);
5060 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) | PFM_EN
);
5061 RTL_W32(tp
, MISC
, RTL_R32(tp
, MISC
) | PWM_EN
);
5062 RTL_W8(tp
, Config5
, RTL_R8(tp
, Config5
) & ~Spi_en
);
5064 rtl_hw_aspm_clkreq_enable(tp
, true);
5067 static void rtl_hw_start_8168f(struct rtl8169_private
*tp
)
5069 rtl_set_def_aspm_entry_latency(tp
);
5071 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5073 rtl_eri_write(tp
, 0xc0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5074 rtl_eri_write(tp
, 0xb8, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5075 rtl_eri_write(tp
, 0xc8, ERIAR_MASK_1111
, 0x00100002, ERIAR_EXGMAC
);
5076 rtl_eri_write(tp
, 0xe8, ERIAR_MASK_1111
, 0x00100006, ERIAR_EXGMAC
);
5077 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x00, 0x01, ERIAR_EXGMAC
);
5078 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x01, 0x00, ERIAR_EXGMAC
);
5079 rtl_w0w1_eri(tp
, 0x1b0, ERIAR_MASK_0001
, 0x10, 0x00, ERIAR_EXGMAC
);
5080 rtl_w0w1_eri(tp
, 0x1d0, ERIAR_MASK_0001
, 0x10, 0x00, ERIAR_EXGMAC
);
5081 rtl_eri_write(tp
, 0xcc, ERIAR_MASK_1111
, 0x00000050, ERIAR_EXGMAC
);
5082 rtl_eri_write(tp
, 0xd0, ERIAR_MASK_1111
, 0x00000060, ERIAR_EXGMAC
);
5084 RTL_W8(tp
, MaxTxPacketSize
, EarlySize
);
5086 rtl_disable_clock_request(tp
);
5088 RTL_W8(tp
, MCU
, RTL_R8(tp
, MCU
) & ~NOW_IS_OOB
);
5089 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) | PFM_EN
);
5090 RTL_W32(tp
, MISC
, RTL_R32(tp
, MISC
) | PWM_EN
);
5091 RTL_W8(tp
, Config5
, RTL_R8(tp
, Config5
) & ~Spi_en
);
5094 static void rtl_hw_start_8168f_1(struct rtl8169_private
*tp
)
5096 static const struct ephy_info e_info_8168f_1
[] = {
5097 { 0x06, 0x00c0, 0x0020 },
5098 { 0x08, 0x0001, 0x0002 },
5099 { 0x09, 0x0000, 0x0080 },
5100 { 0x19, 0x0000, 0x0224 }
5103 rtl_hw_start_8168f(tp
);
5105 rtl_ephy_init(tp
, e_info_8168f_1
, ARRAY_SIZE(e_info_8168f_1
));
5107 rtl_w0w1_eri(tp
, 0x0d4, ERIAR_MASK_0011
, 0x0c00, 0xff00, ERIAR_EXGMAC
);
5109 /* Adjust EEE LED frequency */
5110 RTL_W8(tp
, EEE_LED
, RTL_R8(tp
, EEE_LED
) & ~0x07);
5113 static void rtl_hw_start_8411(struct rtl8169_private
*tp
)
5115 static const struct ephy_info e_info_8168f_1
[] = {
5116 { 0x06, 0x00c0, 0x0020 },
5117 { 0x0f, 0xffff, 0x5200 },
5118 { 0x1e, 0x0000, 0x4000 },
5119 { 0x19, 0x0000, 0x0224 }
5122 rtl_hw_start_8168f(tp
);
5123 rtl_pcie_state_l2l3_enable(tp
, false);
5125 rtl_ephy_init(tp
, e_info_8168f_1
, ARRAY_SIZE(e_info_8168f_1
));
5127 rtl_w0w1_eri(tp
, 0x0d4, ERIAR_MASK_0011
, 0x0c00, 0x0000, ERIAR_EXGMAC
);
5130 static void rtl_hw_start_8168g(struct rtl8169_private
*tp
)
5132 rtl_eri_write(tp
, 0xc8, ERIAR_MASK_0101
, 0x080002, ERIAR_EXGMAC
);
5133 rtl_eri_write(tp
, 0xcc, ERIAR_MASK_0001
, 0x38, ERIAR_EXGMAC
);
5134 rtl_eri_write(tp
, 0xd0, ERIAR_MASK_0001
, 0x48, ERIAR_EXGMAC
);
5135 rtl_eri_write(tp
, 0xe8, ERIAR_MASK_1111
, 0x00100006, ERIAR_EXGMAC
);
5137 rtl_set_def_aspm_entry_latency(tp
);
5139 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5141 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x00, 0x01, ERIAR_EXGMAC
);
5142 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x01, 0x00, ERIAR_EXGMAC
);
5143 rtl_eri_write(tp
, 0x2f8, ERIAR_MASK_0011
, 0x1d8f, ERIAR_EXGMAC
);
5145 RTL_W32(tp
, MISC
, RTL_R32(tp
, MISC
) & ~RXDV_GATED_EN
);
5146 RTL_W8(tp
, MaxTxPacketSize
, EarlySize
);
5148 rtl_eri_write(tp
, 0xc0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5149 rtl_eri_write(tp
, 0xb8, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5151 /* Adjust EEE LED frequency */
5152 RTL_W8(tp
, EEE_LED
, RTL_R8(tp
, EEE_LED
) & ~0x07);
5154 rtl_w0w1_eri(tp
, 0x2fc, ERIAR_MASK_0001
, 0x01, 0x06, ERIAR_EXGMAC
);
5155 rtl_w0w1_eri(tp
, 0x1b0, ERIAR_MASK_0011
, 0x0000, 0x1000, ERIAR_EXGMAC
);
5157 rtl_pcie_state_l2l3_enable(tp
, false);
5160 static void rtl_hw_start_8168g_1(struct rtl8169_private
*tp
)
5162 static const struct ephy_info e_info_8168g_1
[] = {
5163 { 0x00, 0x0000, 0x0008 },
5164 { 0x0c, 0x37d0, 0x0820 },
5165 { 0x1e, 0x0000, 0x0001 },
5166 { 0x19, 0x8000, 0x0000 }
5169 rtl_hw_start_8168g(tp
);
5171 /* disable aspm and clock request before access ephy */
5172 rtl_hw_aspm_clkreq_enable(tp
, false);
5173 rtl_ephy_init(tp
, e_info_8168g_1
, ARRAY_SIZE(e_info_8168g_1
));
5174 rtl_hw_aspm_clkreq_enable(tp
, true);
5177 static void rtl_hw_start_8168g_2(struct rtl8169_private
*tp
)
5179 static const struct ephy_info e_info_8168g_2
[] = {
5180 { 0x00, 0x0000, 0x0008 },
5181 { 0x0c, 0x3df0, 0x0200 },
5182 { 0x19, 0xffff, 0xfc00 },
5183 { 0x1e, 0xffff, 0x20eb }
5186 rtl_hw_start_8168g(tp
);
5188 /* disable aspm and clock request before access ephy */
5189 RTL_W8(tp
, Config2
, RTL_R8(tp
, Config2
) & ~ClkReqEn
);
5190 RTL_W8(tp
, Config5
, RTL_R8(tp
, Config5
) & ~ASPM_en
);
5191 rtl_ephy_init(tp
, e_info_8168g_2
, ARRAY_SIZE(e_info_8168g_2
));
5194 static void rtl_hw_start_8411_2(struct rtl8169_private
*tp
)
5196 static const struct ephy_info e_info_8411_2
[] = {
5197 { 0x00, 0x0000, 0x0008 },
5198 { 0x0c, 0x3df0, 0x0200 },
5199 { 0x0f, 0xffff, 0x5200 },
5200 { 0x19, 0x0020, 0x0000 },
5201 { 0x1e, 0x0000, 0x2000 }
5204 rtl_hw_start_8168g(tp
);
5206 /* disable aspm and clock request before access ephy */
5207 rtl_hw_aspm_clkreq_enable(tp
, false);
5208 rtl_ephy_init(tp
, e_info_8411_2
, ARRAY_SIZE(e_info_8411_2
));
5210 /* The following Realtek-provided magic fixes an issue with the RX unit
5211 * getting confused after the PHY having been powered-down.
5213 r8168_mac_ocp_write(tp
, 0xFC28, 0x0000);
5214 r8168_mac_ocp_write(tp
, 0xFC2A, 0x0000);
5215 r8168_mac_ocp_write(tp
, 0xFC2C, 0x0000);
5216 r8168_mac_ocp_write(tp
, 0xFC2E, 0x0000);
5217 r8168_mac_ocp_write(tp
, 0xFC30, 0x0000);
5218 r8168_mac_ocp_write(tp
, 0xFC32, 0x0000);
5219 r8168_mac_ocp_write(tp
, 0xFC34, 0x0000);
5220 r8168_mac_ocp_write(tp
, 0xFC36, 0x0000);
5222 r8168_mac_ocp_write(tp
, 0xFC26, 0x0000);
5224 r8168_mac_ocp_write(tp
, 0xF800, 0xE008);
5225 r8168_mac_ocp_write(tp
, 0xF802, 0xE00A);
5226 r8168_mac_ocp_write(tp
, 0xF804, 0xE00C);
5227 r8168_mac_ocp_write(tp
, 0xF806, 0xE00E);
5228 r8168_mac_ocp_write(tp
, 0xF808, 0xE027);
5229 r8168_mac_ocp_write(tp
, 0xF80A, 0xE04F);
5230 r8168_mac_ocp_write(tp
, 0xF80C, 0xE05E);
5231 r8168_mac_ocp_write(tp
, 0xF80E, 0xE065);
5232 r8168_mac_ocp_write(tp
, 0xF810, 0xC602);
5233 r8168_mac_ocp_write(tp
, 0xF812, 0xBE00);
5234 r8168_mac_ocp_write(tp
, 0xF814, 0x0000);
5235 r8168_mac_ocp_write(tp
, 0xF816, 0xC502);
5236 r8168_mac_ocp_write(tp
, 0xF818, 0xBD00);
5237 r8168_mac_ocp_write(tp
, 0xF81A, 0x074C);
5238 r8168_mac_ocp_write(tp
, 0xF81C, 0xC302);
5239 r8168_mac_ocp_write(tp
, 0xF81E, 0xBB00);
5240 r8168_mac_ocp_write(tp
, 0xF820, 0x080A);
5241 r8168_mac_ocp_write(tp
, 0xF822, 0x6420);
5242 r8168_mac_ocp_write(tp
, 0xF824, 0x48C2);
5243 r8168_mac_ocp_write(tp
, 0xF826, 0x8C20);
5244 r8168_mac_ocp_write(tp
, 0xF828, 0xC516);
5245 r8168_mac_ocp_write(tp
, 0xF82A, 0x64A4);
5246 r8168_mac_ocp_write(tp
, 0xF82C, 0x49C0);
5247 r8168_mac_ocp_write(tp
, 0xF82E, 0xF009);
5248 r8168_mac_ocp_write(tp
, 0xF830, 0x74A2);
5249 r8168_mac_ocp_write(tp
, 0xF832, 0x8CA5);
5250 r8168_mac_ocp_write(tp
, 0xF834, 0x74A0);
5251 r8168_mac_ocp_write(tp
, 0xF836, 0xC50E);
5252 r8168_mac_ocp_write(tp
, 0xF838, 0x9CA2);
5253 r8168_mac_ocp_write(tp
, 0xF83A, 0x1C11);
5254 r8168_mac_ocp_write(tp
, 0xF83C, 0x9CA0);
5255 r8168_mac_ocp_write(tp
, 0xF83E, 0xE006);
5256 r8168_mac_ocp_write(tp
, 0xF840, 0x74F8);
5257 r8168_mac_ocp_write(tp
, 0xF842, 0x48C4);
5258 r8168_mac_ocp_write(tp
, 0xF844, 0x8CF8);
5259 r8168_mac_ocp_write(tp
, 0xF846, 0xC404);
5260 r8168_mac_ocp_write(tp
, 0xF848, 0xBC00);
5261 r8168_mac_ocp_write(tp
, 0xF84A, 0xC403);
5262 r8168_mac_ocp_write(tp
, 0xF84C, 0xBC00);
5263 r8168_mac_ocp_write(tp
, 0xF84E, 0x0BF2);
5264 r8168_mac_ocp_write(tp
, 0xF850, 0x0C0A);
5265 r8168_mac_ocp_write(tp
, 0xF852, 0xE434);
5266 r8168_mac_ocp_write(tp
, 0xF854, 0xD3C0);
5267 r8168_mac_ocp_write(tp
, 0xF856, 0x49D9);
5268 r8168_mac_ocp_write(tp
, 0xF858, 0xF01F);
5269 r8168_mac_ocp_write(tp
, 0xF85A, 0xC526);
5270 r8168_mac_ocp_write(tp
, 0xF85C, 0x64A5);
5271 r8168_mac_ocp_write(tp
, 0xF85E, 0x1400);
5272 r8168_mac_ocp_write(tp
, 0xF860, 0xF007);
5273 r8168_mac_ocp_write(tp
, 0xF862, 0x0C01);
5274 r8168_mac_ocp_write(tp
, 0xF864, 0x8CA5);
5275 r8168_mac_ocp_write(tp
, 0xF866, 0x1C15);
5276 r8168_mac_ocp_write(tp
, 0xF868, 0xC51B);
5277 r8168_mac_ocp_write(tp
, 0xF86A, 0x9CA0);
5278 r8168_mac_ocp_write(tp
, 0xF86C, 0xE013);
5279 r8168_mac_ocp_write(tp
, 0xF86E, 0xC519);
5280 r8168_mac_ocp_write(tp
, 0xF870, 0x74A0);
5281 r8168_mac_ocp_write(tp
, 0xF872, 0x48C4);
5282 r8168_mac_ocp_write(tp
, 0xF874, 0x8CA0);
5283 r8168_mac_ocp_write(tp
, 0xF876, 0xC516);
5284 r8168_mac_ocp_write(tp
, 0xF878, 0x74A4);
5285 r8168_mac_ocp_write(tp
, 0xF87A, 0x48C8);
5286 r8168_mac_ocp_write(tp
, 0xF87C, 0x48CA);
5287 r8168_mac_ocp_write(tp
, 0xF87E, 0x9CA4);
5288 r8168_mac_ocp_write(tp
, 0xF880, 0xC512);
5289 r8168_mac_ocp_write(tp
, 0xF882, 0x1B00);
5290 r8168_mac_ocp_write(tp
, 0xF884, 0x9BA0);
5291 r8168_mac_ocp_write(tp
, 0xF886, 0x1B1C);
5292 r8168_mac_ocp_write(tp
, 0xF888, 0x483F);
5293 r8168_mac_ocp_write(tp
, 0xF88A, 0x9BA2);
5294 r8168_mac_ocp_write(tp
, 0xF88C, 0x1B04);
5295 r8168_mac_ocp_write(tp
, 0xF88E, 0xC508);
5296 r8168_mac_ocp_write(tp
, 0xF890, 0x9BA0);
5297 r8168_mac_ocp_write(tp
, 0xF892, 0xC505);
5298 r8168_mac_ocp_write(tp
, 0xF894, 0xBD00);
5299 r8168_mac_ocp_write(tp
, 0xF896, 0xC502);
5300 r8168_mac_ocp_write(tp
, 0xF898, 0xBD00);
5301 r8168_mac_ocp_write(tp
, 0xF89A, 0x0300);
5302 r8168_mac_ocp_write(tp
, 0xF89C, 0x051E);
5303 r8168_mac_ocp_write(tp
, 0xF89E, 0xE434);
5304 r8168_mac_ocp_write(tp
, 0xF8A0, 0xE018);
5305 r8168_mac_ocp_write(tp
, 0xF8A2, 0xE092);
5306 r8168_mac_ocp_write(tp
, 0xF8A4, 0xDE20);
5307 r8168_mac_ocp_write(tp
, 0xF8A6, 0xD3C0);
5308 r8168_mac_ocp_write(tp
, 0xF8A8, 0xC50F);
5309 r8168_mac_ocp_write(tp
, 0xF8AA, 0x76A4);
5310 r8168_mac_ocp_write(tp
, 0xF8AC, 0x49E3);
5311 r8168_mac_ocp_write(tp
, 0xF8AE, 0xF007);
5312 r8168_mac_ocp_write(tp
, 0xF8B0, 0x49C0);
5313 r8168_mac_ocp_write(tp
, 0xF8B2, 0xF103);
5314 r8168_mac_ocp_write(tp
, 0xF8B4, 0xC607);
5315 r8168_mac_ocp_write(tp
, 0xF8B6, 0xBE00);
5316 r8168_mac_ocp_write(tp
, 0xF8B8, 0xC606);
5317 r8168_mac_ocp_write(tp
, 0xF8BA, 0xBE00);
5318 r8168_mac_ocp_write(tp
, 0xF8BC, 0xC602);
5319 r8168_mac_ocp_write(tp
, 0xF8BE, 0xBE00);
5320 r8168_mac_ocp_write(tp
, 0xF8C0, 0x0C4C);
5321 r8168_mac_ocp_write(tp
, 0xF8C2, 0x0C28);
5322 r8168_mac_ocp_write(tp
, 0xF8C4, 0x0C2C);
5323 r8168_mac_ocp_write(tp
, 0xF8C6, 0xDC00);
5324 r8168_mac_ocp_write(tp
, 0xF8C8, 0xC707);
5325 r8168_mac_ocp_write(tp
, 0xF8CA, 0x1D00);
5326 r8168_mac_ocp_write(tp
, 0xF8CC, 0x8DE2);
5327 r8168_mac_ocp_write(tp
, 0xF8CE, 0x48C1);
5328 r8168_mac_ocp_write(tp
, 0xF8D0, 0xC502);
5329 r8168_mac_ocp_write(tp
, 0xF8D2, 0xBD00);
5330 r8168_mac_ocp_write(tp
, 0xF8D4, 0x00AA);
5331 r8168_mac_ocp_write(tp
, 0xF8D6, 0xE0C0);
5332 r8168_mac_ocp_write(tp
, 0xF8D8, 0xC502);
5333 r8168_mac_ocp_write(tp
, 0xF8DA, 0xBD00);
5334 r8168_mac_ocp_write(tp
, 0xF8DC, 0x0132);
5336 r8168_mac_ocp_write(tp
, 0xFC26, 0x8000);
5338 r8168_mac_ocp_write(tp
, 0xFC2A, 0x0743);
5339 r8168_mac_ocp_write(tp
, 0xFC2C, 0x0801);
5340 r8168_mac_ocp_write(tp
, 0xFC2E, 0x0BE9);
5341 r8168_mac_ocp_write(tp
, 0xFC30, 0x02FD);
5342 r8168_mac_ocp_write(tp
, 0xFC32, 0x0C25);
5343 r8168_mac_ocp_write(tp
, 0xFC34, 0x00A9);
5344 r8168_mac_ocp_write(tp
, 0xFC36, 0x012D);
5346 rtl_hw_aspm_clkreq_enable(tp
, true);
5349 static void rtl_hw_start_8168h_1(struct rtl8169_private
*tp
)
5353 static const struct ephy_info e_info_8168h_1
[] = {
5354 { 0x1e, 0x0800, 0x0001 },
5355 { 0x1d, 0x0000, 0x0800 },
5356 { 0x05, 0xffff, 0x2089 },
5357 { 0x06, 0xffff, 0x5881 },
5358 { 0x04, 0xffff, 0x154a },
5359 { 0x01, 0xffff, 0x068b }
5362 /* disable aspm and clock request before access ephy */
5363 rtl_hw_aspm_clkreq_enable(tp
, false);
5364 rtl_ephy_init(tp
, e_info_8168h_1
, ARRAY_SIZE(e_info_8168h_1
));
5366 rtl_eri_write(tp
, 0xc8, ERIAR_MASK_0101
, 0x00080002, ERIAR_EXGMAC
);
5367 rtl_eri_write(tp
, 0xcc, ERIAR_MASK_0001
, 0x38, ERIAR_EXGMAC
);
5368 rtl_eri_write(tp
, 0xd0, ERIAR_MASK_0001
, 0x48, ERIAR_EXGMAC
);
5369 rtl_eri_write(tp
, 0xe8, ERIAR_MASK_1111
, 0x00100006, ERIAR_EXGMAC
);
5371 rtl_set_def_aspm_entry_latency(tp
);
5373 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5375 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x00, 0x01, ERIAR_EXGMAC
);
5376 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x01, 0x00, ERIAR_EXGMAC
);
5378 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_1111
, 0x0010, 0x00, ERIAR_EXGMAC
);
5380 rtl_w0w1_eri(tp
, 0xd4, ERIAR_MASK_1111
, 0x1f00, 0x00, ERIAR_EXGMAC
);
5382 rtl_eri_write(tp
, 0x5f0, ERIAR_MASK_0011
, 0x4f87, ERIAR_EXGMAC
);
5384 RTL_W32(tp
, MISC
, RTL_R32(tp
, MISC
) & ~RXDV_GATED_EN
);
5385 RTL_W8(tp
, MaxTxPacketSize
, EarlySize
);
5387 rtl_eri_write(tp
, 0xc0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5388 rtl_eri_write(tp
, 0xb8, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5390 /* Adjust EEE LED frequency */
5391 RTL_W8(tp
, EEE_LED
, RTL_R8(tp
, EEE_LED
) & ~0x07);
5393 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) & ~PFM_EN
);
5394 RTL_W8(tp
, MISC_1
, RTL_R8(tp
, MISC_1
) & ~PFM_D3COLD_EN
);
5396 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) & ~TX_10M_PS_EN
);
5398 rtl_w0w1_eri(tp
, 0x1b0, ERIAR_MASK_0011
, 0x0000, 0x1000, ERIAR_EXGMAC
);
5400 rtl_pcie_state_l2l3_enable(tp
, false);
5402 rtl_writephy(tp
, 0x1f, 0x0c42);
5403 rg_saw_cnt
= (rtl_readphy(tp
, 0x13) & 0x3fff);
5404 rtl_writephy(tp
, 0x1f, 0x0000);
5405 if (rg_saw_cnt
> 0) {
5408 sw_cnt_1ms_ini
= 16000000/rg_saw_cnt
;
5409 sw_cnt_1ms_ini
&= 0x0fff;
5410 data
= r8168_mac_ocp_read(tp
, 0xd412);
5412 data
|= sw_cnt_1ms_ini
;
5413 r8168_mac_ocp_write(tp
, 0xd412, data
);
5416 data
= r8168_mac_ocp_read(tp
, 0xe056);
5419 r8168_mac_ocp_write(tp
, 0xe056, data
);
5421 data
= r8168_mac_ocp_read(tp
, 0xe052);
5424 r8168_mac_ocp_write(tp
, 0xe052, data
);
5426 data
= r8168_mac_ocp_read(tp
, 0xe0d6);
5429 r8168_mac_ocp_write(tp
, 0xe0d6, data
);
5431 data
= r8168_mac_ocp_read(tp
, 0xd420);
5434 r8168_mac_ocp_write(tp
, 0xd420, data
);
5436 r8168_mac_ocp_write(tp
, 0xe63e, 0x0001);
5437 r8168_mac_ocp_write(tp
, 0xe63e, 0x0000);
5438 r8168_mac_ocp_write(tp
, 0xc094, 0x0000);
5439 r8168_mac_ocp_write(tp
, 0xc09e, 0x0000);
5441 rtl_hw_aspm_clkreq_enable(tp
, true);
5444 static void rtl_hw_start_8168ep(struct rtl8169_private
*tp
)
5446 rtl8168ep_stop_cmac(tp
);
5448 rtl_eri_write(tp
, 0xc8, ERIAR_MASK_0101
, 0x00080002, ERIAR_EXGMAC
);
5449 rtl_eri_write(tp
, 0xcc, ERIAR_MASK_0001
, 0x2f, ERIAR_EXGMAC
);
5450 rtl_eri_write(tp
, 0xd0, ERIAR_MASK_0001
, 0x5f, ERIAR_EXGMAC
);
5451 rtl_eri_write(tp
, 0xe8, ERIAR_MASK_1111
, 0x00100006, ERIAR_EXGMAC
);
5453 rtl_set_def_aspm_entry_latency(tp
);
5455 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5457 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x00, 0x01, ERIAR_EXGMAC
);
5458 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x01, 0x00, ERIAR_EXGMAC
);
5460 rtl_w0w1_eri(tp
, 0xd4, ERIAR_MASK_1111
, 0x1f80, 0x00, ERIAR_EXGMAC
);
5462 rtl_eri_write(tp
, 0x5f0, ERIAR_MASK_0011
, 0x4f87, ERIAR_EXGMAC
);
5464 RTL_W32(tp
, MISC
, RTL_R32(tp
, MISC
) & ~RXDV_GATED_EN
);
5465 RTL_W8(tp
, MaxTxPacketSize
, EarlySize
);
5467 rtl_eri_write(tp
, 0xc0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5468 rtl_eri_write(tp
, 0xb8, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5470 /* Adjust EEE LED frequency */
5471 RTL_W8(tp
, EEE_LED
, RTL_R8(tp
, EEE_LED
) & ~0x07);
5473 rtl_w0w1_eri(tp
, 0x2fc, ERIAR_MASK_0001
, 0x01, 0x06, ERIAR_EXGMAC
);
5475 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) & ~TX_10M_PS_EN
);
5477 rtl_pcie_state_l2l3_enable(tp
, false);
5480 static void rtl_hw_start_8168ep_1(struct rtl8169_private
*tp
)
5482 static const struct ephy_info e_info_8168ep_1
[] = {
5483 { 0x00, 0xffff, 0x10ab },
5484 { 0x06, 0xffff, 0xf030 },
5485 { 0x08, 0xffff, 0x2006 },
5486 { 0x0d, 0xffff, 0x1666 },
5487 { 0x0c, 0x3ff0, 0x0000 }
5490 /* disable aspm and clock request before access ephy */
5491 rtl_hw_aspm_clkreq_enable(tp
, false);
5492 rtl_ephy_init(tp
, e_info_8168ep_1
, ARRAY_SIZE(e_info_8168ep_1
));
5494 rtl_hw_start_8168ep(tp
);
5496 rtl_hw_aspm_clkreq_enable(tp
, true);
5499 static void rtl_hw_start_8168ep_2(struct rtl8169_private
*tp
)
5501 static const struct ephy_info e_info_8168ep_2
[] = {
5502 { 0x00, 0xffff, 0x10a3 },
5503 { 0x19, 0xffff, 0xfc00 },
5504 { 0x1e, 0xffff, 0x20ea }
5507 /* disable aspm and clock request before access ephy */
5508 rtl_hw_aspm_clkreq_enable(tp
, false);
5509 rtl_ephy_init(tp
, e_info_8168ep_2
, ARRAY_SIZE(e_info_8168ep_2
));
5511 rtl_hw_start_8168ep(tp
);
5513 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) & ~PFM_EN
);
5514 RTL_W8(tp
, MISC_1
, RTL_R8(tp
, MISC_1
) & ~PFM_D3COLD_EN
);
5516 rtl_hw_aspm_clkreq_enable(tp
, true);
5519 static void rtl_hw_start_8168ep_3(struct rtl8169_private
*tp
)
5522 static const struct ephy_info e_info_8168ep_3
[] = {
5523 { 0x00, 0xffff, 0x10a3 },
5524 { 0x19, 0xffff, 0x7c00 },
5525 { 0x1e, 0xffff, 0x20eb },
5526 { 0x0d, 0xffff, 0x1666 }
5529 /* disable aspm and clock request before access ephy */
5530 rtl_hw_aspm_clkreq_enable(tp
, false);
5531 rtl_ephy_init(tp
, e_info_8168ep_3
, ARRAY_SIZE(e_info_8168ep_3
));
5533 rtl_hw_start_8168ep(tp
);
5535 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) & ~PFM_EN
);
5536 RTL_W8(tp
, MISC_1
, RTL_R8(tp
, MISC_1
) & ~PFM_D3COLD_EN
);
5538 data
= r8168_mac_ocp_read(tp
, 0xd3e2);
5541 r8168_mac_ocp_write(tp
, 0xd3e2, data
);
5543 data
= r8168_mac_ocp_read(tp
, 0xd3e4);
5545 r8168_mac_ocp_write(tp
, 0xd3e4, data
);
5547 data
= r8168_mac_ocp_read(tp
, 0xe860);
5549 r8168_mac_ocp_write(tp
, 0xe860, data
);
5551 rtl_hw_aspm_clkreq_enable(tp
, true);
5554 static void rtl_hw_start_8168(struct rtl8169_private
*tp
)
5556 RTL_W8(tp
, MaxTxPacketSize
, TxPacketMax
);
5558 tp
->cp_cmd
&= ~INTT_MASK
;
5559 tp
->cp_cmd
|= PktCntrDisable
| INTT_1
;
5560 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
5562 RTL_W16(tp
, IntrMitigate
, 0x5100);
5564 /* Work around for RxFIFO overflow. */
5565 if (tp
->mac_version
== RTL_GIGA_MAC_VER_11
) {
5566 tp
->event_slow
|= RxFIFOOver
| PCSTimeout
;
5567 tp
->event_slow
&= ~RxOverflow
;
5570 switch (tp
->mac_version
) {
5571 case RTL_GIGA_MAC_VER_11
:
5572 rtl_hw_start_8168bb(tp
);
5575 case RTL_GIGA_MAC_VER_12
:
5576 case RTL_GIGA_MAC_VER_17
:
5577 rtl_hw_start_8168bef(tp
);
5580 case RTL_GIGA_MAC_VER_18
:
5581 rtl_hw_start_8168cp_1(tp
);
5584 case RTL_GIGA_MAC_VER_19
:
5585 rtl_hw_start_8168c_1(tp
);
5588 case RTL_GIGA_MAC_VER_20
:
5589 rtl_hw_start_8168c_2(tp
);
5592 case RTL_GIGA_MAC_VER_21
:
5593 rtl_hw_start_8168c_3(tp
);
5596 case RTL_GIGA_MAC_VER_22
:
5597 rtl_hw_start_8168c_4(tp
);
5600 case RTL_GIGA_MAC_VER_23
:
5601 rtl_hw_start_8168cp_2(tp
);
5604 case RTL_GIGA_MAC_VER_24
:
5605 rtl_hw_start_8168cp_3(tp
);
5608 case RTL_GIGA_MAC_VER_25
:
5609 case RTL_GIGA_MAC_VER_26
:
5610 case RTL_GIGA_MAC_VER_27
:
5611 rtl_hw_start_8168d(tp
);
5614 case RTL_GIGA_MAC_VER_28
:
5615 rtl_hw_start_8168d_4(tp
);
5618 case RTL_GIGA_MAC_VER_31
:
5619 rtl_hw_start_8168dp(tp
);
5622 case RTL_GIGA_MAC_VER_32
:
5623 case RTL_GIGA_MAC_VER_33
:
5624 rtl_hw_start_8168e_1(tp
);
5626 case RTL_GIGA_MAC_VER_34
:
5627 rtl_hw_start_8168e_2(tp
);
5630 case RTL_GIGA_MAC_VER_35
:
5631 case RTL_GIGA_MAC_VER_36
:
5632 rtl_hw_start_8168f_1(tp
);
5635 case RTL_GIGA_MAC_VER_38
:
5636 rtl_hw_start_8411(tp
);
5639 case RTL_GIGA_MAC_VER_40
:
5640 case RTL_GIGA_MAC_VER_41
:
5641 rtl_hw_start_8168g_1(tp
);
5643 case RTL_GIGA_MAC_VER_42
:
5644 rtl_hw_start_8168g_2(tp
);
5647 case RTL_GIGA_MAC_VER_44
:
5648 rtl_hw_start_8411_2(tp
);
5651 case RTL_GIGA_MAC_VER_45
:
5652 case RTL_GIGA_MAC_VER_46
:
5653 rtl_hw_start_8168h_1(tp
);
5656 case RTL_GIGA_MAC_VER_49
:
5657 rtl_hw_start_8168ep_1(tp
);
5660 case RTL_GIGA_MAC_VER_50
:
5661 rtl_hw_start_8168ep_2(tp
);
5664 case RTL_GIGA_MAC_VER_51
:
5665 rtl_hw_start_8168ep_3(tp
);
5669 netif_err(tp
, drv
, tp
->dev
,
5670 "unknown chipset (mac_version = %d)\n",
5676 static void rtl_hw_start_8102e_1(struct rtl8169_private
*tp
)
5678 static const struct ephy_info e_info_8102e_1
[] = {
5679 { 0x01, 0, 0x6e65 },
5680 { 0x02, 0, 0x091f },
5681 { 0x03, 0, 0xc2f9 },
5682 { 0x06, 0, 0xafb5 },
5683 { 0x07, 0, 0x0e00 },
5684 { 0x19, 0, 0xec80 },
5685 { 0x01, 0, 0x2e65 },
5690 rtl_set_def_aspm_entry_latency(tp
);
5692 RTL_W8(tp
, DBG_REG
, FIX_NAK_1
);
5694 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5697 LEDS1
| LEDS0
| Speed_down
| MEMMAP
| IOMAP
| VPD
| PMEnable
);
5698 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Beacon_en
);
5700 cfg1
= RTL_R8(tp
, Config1
);
5701 if ((cfg1
& LEDS0
) && (cfg1
& LEDS1
))
5702 RTL_W8(tp
, Config1
, cfg1
& ~LEDS0
);
5704 rtl_ephy_init(tp
, e_info_8102e_1
, ARRAY_SIZE(e_info_8102e_1
));
5707 static void rtl_hw_start_8102e_2(struct rtl8169_private
*tp
)
5709 rtl_set_def_aspm_entry_latency(tp
);
5711 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5713 RTL_W8(tp
, Config1
, MEMMAP
| IOMAP
| VPD
| PMEnable
);
5714 RTL_W8(tp
, Config3
, RTL_R8(tp
, Config3
) & ~Beacon_en
);
5717 static void rtl_hw_start_8102e_3(struct rtl8169_private
*tp
)
5719 rtl_hw_start_8102e_2(tp
);
5721 rtl_ephy_write(tp
, 0x03, 0xc2f9);
5724 static void rtl_hw_start_8105e_1(struct rtl8169_private
*tp
)
5726 static const struct ephy_info e_info_8105e_1
[] = {
5727 { 0x07, 0, 0x4000 },
5728 { 0x19, 0, 0x0200 },
5729 { 0x19, 0, 0x0020 },
5730 { 0x1e, 0, 0x2000 },
5731 { 0x03, 0, 0x0001 },
5732 { 0x19, 0, 0x0100 },
5733 { 0x19, 0, 0x0004 },
5737 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5738 RTL_W32(tp
, FuncEvent
, RTL_R32(tp
, FuncEvent
) | 0x002800);
5740 /* Disable Early Tally Counter */
5741 RTL_W32(tp
, FuncEvent
, RTL_R32(tp
, FuncEvent
) & ~0x010000);
5743 RTL_W8(tp
, MCU
, RTL_R8(tp
, MCU
) | EN_NDP
| EN_OOB_RESET
);
5744 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) | PFM_EN
);
5746 rtl_ephy_init(tp
, e_info_8105e_1
, ARRAY_SIZE(e_info_8105e_1
));
5748 rtl_pcie_state_l2l3_enable(tp
, false);
5751 static void rtl_hw_start_8105e_2(struct rtl8169_private
*tp
)
5753 rtl_hw_start_8105e_1(tp
);
5754 rtl_ephy_write(tp
, 0x1e, rtl_ephy_read(tp
, 0x1e) | 0x8000);
5757 static void rtl_hw_start_8402(struct rtl8169_private
*tp
)
5759 static const struct ephy_info e_info_8402
[] = {
5760 { 0x19, 0xffff, 0xff64 },
5764 rtl_set_def_aspm_entry_latency(tp
);
5766 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5767 RTL_W32(tp
, FuncEvent
, RTL_R32(tp
, FuncEvent
) | 0x002800);
5769 RTL_W8(tp
, MCU
, RTL_R8(tp
, MCU
) & ~NOW_IS_OOB
);
5771 rtl_ephy_init(tp
, e_info_8402
, ARRAY_SIZE(e_info_8402
));
5773 rtl_tx_performance_tweak(tp
, PCI_EXP_DEVCTL_READRQ_4096B
);
5775 rtl_eri_write(tp
, 0xc8, ERIAR_MASK_1111
, 0x00000002, ERIAR_EXGMAC
);
5776 rtl_eri_write(tp
, 0xe8, ERIAR_MASK_1111
, 0x00000006, ERIAR_EXGMAC
);
5777 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x00, 0x01, ERIAR_EXGMAC
);
5778 rtl_w0w1_eri(tp
, 0xdc, ERIAR_MASK_0001
, 0x01, 0x00, ERIAR_EXGMAC
);
5779 rtl_eri_write(tp
, 0xc0, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5780 rtl_eri_write(tp
, 0xb8, ERIAR_MASK_0011
, 0x0000, ERIAR_EXGMAC
);
5781 rtl_w0w1_eri(tp
, 0x0d4, ERIAR_MASK_0011
, 0x0e00, 0xff00, ERIAR_EXGMAC
);
5783 rtl_pcie_state_l2l3_enable(tp
, false);
5786 static void rtl_hw_start_8106(struct rtl8169_private
*tp
)
5788 rtl_hw_aspm_clkreq_enable(tp
, false);
5790 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5791 RTL_W32(tp
, FuncEvent
, RTL_R32(tp
, FuncEvent
) | 0x002800);
5793 RTL_W32(tp
, MISC
, (RTL_R32(tp
, MISC
) | DISABLE_LAN_EN
) & ~EARLY_TALLY_EN
);
5794 RTL_W8(tp
, MCU
, RTL_R8(tp
, MCU
) | EN_NDP
| EN_OOB_RESET
);
5795 RTL_W8(tp
, DLLPR
, RTL_R8(tp
, DLLPR
) & ~PFM_EN
);
5797 rtl_pcie_state_l2l3_enable(tp
, false);
5798 rtl_hw_aspm_clkreq_enable(tp
, true);
5801 static void rtl_hw_start_8101(struct rtl8169_private
*tp
)
5803 if (tp
->mac_version
>= RTL_GIGA_MAC_VER_30
)
5804 tp
->event_slow
&= ~RxFIFOOver
;
5806 if (tp
->mac_version
== RTL_GIGA_MAC_VER_13
||
5807 tp
->mac_version
== RTL_GIGA_MAC_VER_16
)
5808 pcie_capability_set_word(tp
->pci_dev
, PCI_EXP_DEVCTL
,
5809 PCI_EXP_DEVCTL_NOSNOOP_EN
);
5811 RTL_W8(tp
, MaxTxPacketSize
, TxPacketMax
);
5813 tp
->cp_cmd
&= CPCMD_QUIRK_MASK
;
5814 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
5816 switch (tp
->mac_version
) {
5817 case RTL_GIGA_MAC_VER_07
:
5818 rtl_hw_start_8102e_1(tp
);
5821 case RTL_GIGA_MAC_VER_08
:
5822 rtl_hw_start_8102e_3(tp
);
5825 case RTL_GIGA_MAC_VER_09
:
5826 rtl_hw_start_8102e_2(tp
);
5829 case RTL_GIGA_MAC_VER_29
:
5830 rtl_hw_start_8105e_1(tp
);
5832 case RTL_GIGA_MAC_VER_30
:
5833 rtl_hw_start_8105e_2(tp
);
5836 case RTL_GIGA_MAC_VER_37
:
5837 rtl_hw_start_8402(tp
);
5840 case RTL_GIGA_MAC_VER_39
:
5841 rtl_hw_start_8106(tp
);
5843 case RTL_GIGA_MAC_VER_43
:
5844 rtl_hw_start_8168g_2(tp
);
5846 case RTL_GIGA_MAC_VER_47
:
5847 case RTL_GIGA_MAC_VER_48
:
5848 rtl_hw_start_8168h_1(tp
);
5852 RTL_W16(tp
, IntrMitigate
, 0x0000);
5855 static int rtl8169_change_mtu(struct net_device
*dev
, int new_mtu
)
5857 struct rtl8169_private
*tp
= netdev_priv(dev
);
5859 if (new_mtu
> ETH_DATA_LEN
)
5860 rtl_hw_jumbo_enable(tp
);
5862 rtl_hw_jumbo_disable(tp
);
5865 netdev_update_features(dev
);
5870 static inline void rtl8169_make_unusable_by_asic(struct RxDesc
*desc
)
5872 desc
->addr
= cpu_to_le64(0x0badbadbadbadbadull
);
5873 desc
->opts1
&= ~cpu_to_le32(DescOwn
| RsvdMask
);
5876 static void rtl8169_free_rx_databuff(struct rtl8169_private
*tp
,
5877 void **data_buff
, struct RxDesc
*desc
)
5879 dma_unmap_single(tp_to_dev(tp
), le64_to_cpu(desc
->addr
),
5880 R8169_RX_BUF_SIZE
, DMA_FROM_DEVICE
);
5884 rtl8169_make_unusable_by_asic(desc
);
5887 static inline void rtl8169_mark_to_asic(struct RxDesc
*desc
)
5889 u32 eor
= le32_to_cpu(desc
->opts1
) & RingEnd
;
5891 /* Force memory writes to complete before releasing descriptor */
5894 desc
->opts1
= cpu_to_le32(DescOwn
| eor
| R8169_RX_BUF_SIZE
);
5897 static inline void *rtl8169_align(void *data
)
5899 return (void *)ALIGN((long)data
, 16);
5902 static struct sk_buff
*rtl8169_alloc_rx_data(struct rtl8169_private
*tp
,
5903 struct RxDesc
*desc
)
5907 struct device
*d
= tp_to_dev(tp
);
5908 int node
= dev_to_node(d
);
5910 data
= kmalloc_node(R8169_RX_BUF_SIZE
, GFP_KERNEL
, node
);
5914 if (rtl8169_align(data
) != data
) {
5916 data
= kmalloc_node(R8169_RX_BUF_SIZE
+ 15, GFP_KERNEL
, node
);
5921 mapping
= dma_map_single(d
, rtl8169_align(data
), R8169_RX_BUF_SIZE
,
5923 if (unlikely(dma_mapping_error(d
, mapping
))) {
5924 if (net_ratelimit())
5925 netif_err(tp
, drv
, tp
->dev
, "Failed to map RX DMA!\n");
5929 desc
->addr
= cpu_to_le64(mapping
);
5930 rtl8169_mark_to_asic(desc
);
5938 static void rtl8169_rx_clear(struct rtl8169_private
*tp
)
5942 for (i
= 0; i
< NUM_RX_DESC
; i
++) {
5943 if (tp
->Rx_databuff
[i
]) {
5944 rtl8169_free_rx_databuff(tp
, tp
->Rx_databuff
+ i
,
5945 tp
->RxDescArray
+ i
);
5950 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc
*desc
)
5952 desc
->opts1
|= cpu_to_le32(RingEnd
);
5955 static int rtl8169_rx_fill(struct rtl8169_private
*tp
)
5959 for (i
= 0; i
< NUM_RX_DESC
; i
++) {
5962 data
= rtl8169_alloc_rx_data(tp
, tp
->RxDescArray
+ i
);
5964 rtl8169_make_unusable_by_asic(tp
->RxDescArray
+ i
);
5967 tp
->Rx_databuff
[i
] = data
;
5970 rtl8169_mark_as_last_descriptor(tp
->RxDescArray
+ NUM_RX_DESC
- 1);
5974 rtl8169_rx_clear(tp
);
5978 static int rtl8169_init_ring(struct rtl8169_private
*tp
)
5980 rtl8169_init_ring_indexes(tp
);
5982 memset(tp
->tx_skb
, 0, sizeof(tp
->tx_skb
));
5983 memset(tp
->Rx_databuff
, 0, sizeof(tp
->Rx_databuff
));
5985 return rtl8169_rx_fill(tp
);
5988 static void rtl8169_unmap_tx_skb(struct device
*d
, struct ring_info
*tx_skb
,
5989 struct TxDesc
*desc
)
5991 unsigned int len
= tx_skb
->len
;
5993 dma_unmap_single(d
, le64_to_cpu(desc
->addr
), len
, DMA_TO_DEVICE
);
6001 static void rtl8169_tx_clear_range(struct rtl8169_private
*tp
, u32 start
,
6006 for (i
= 0; i
< n
; i
++) {
6007 unsigned int entry
= (start
+ i
) % NUM_TX_DESC
;
6008 struct ring_info
*tx_skb
= tp
->tx_skb
+ entry
;
6009 unsigned int len
= tx_skb
->len
;
6012 struct sk_buff
*skb
= tx_skb
->skb
;
6014 rtl8169_unmap_tx_skb(tp_to_dev(tp
), tx_skb
,
6015 tp
->TxDescArray
+ entry
);
6017 dev_consume_skb_any(skb
);
6024 static void rtl8169_tx_clear(struct rtl8169_private
*tp
)
6026 rtl8169_tx_clear_range(tp
, tp
->dirty_tx
, NUM_TX_DESC
);
6027 tp
->cur_tx
= tp
->dirty_tx
= 0;
6030 static void rtl_reset_work(struct rtl8169_private
*tp
)
6032 struct net_device
*dev
= tp
->dev
;
6035 napi_disable(&tp
->napi
);
6036 netif_stop_queue(dev
);
6037 synchronize_sched();
6039 rtl8169_hw_reset(tp
);
6041 for (i
= 0; i
< NUM_RX_DESC
; i
++)
6042 rtl8169_mark_to_asic(tp
->RxDescArray
+ i
);
6044 rtl8169_tx_clear(tp
);
6045 rtl8169_init_ring_indexes(tp
);
6047 napi_enable(&tp
->napi
);
6049 netif_wake_queue(dev
);
6052 static void rtl8169_tx_timeout(struct net_device
*dev
)
6054 struct rtl8169_private
*tp
= netdev_priv(dev
);
6056 rtl_schedule_task(tp
, RTL_FLAG_TASK_RESET_PENDING
);
6059 static int rtl8169_xmit_frags(struct rtl8169_private
*tp
, struct sk_buff
*skb
,
6062 struct skb_shared_info
*info
= skb_shinfo(skb
);
6063 unsigned int cur_frag
, entry
;
6064 struct TxDesc
*uninitialized_var(txd
);
6065 struct device
*d
= tp_to_dev(tp
);
6068 for (cur_frag
= 0; cur_frag
< info
->nr_frags
; cur_frag
++) {
6069 const skb_frag_t
*frag
= info
->frags
+ cur_frag
;
6074 entry
= (entry
+ 1) % NUM_TX_DESC
;
6076 txd
= tp
->TxDescArray
+ entry
;
6077 len
= skb_frag_size(frag
);
6078 addr
= skb_frag_address(frag
);
6079 mapping
= dma_map_single(d
, addr
, len
, DMA_TO_DEVICE
);
6080 if (unlikely(dma_mapping_error(d
, mapping
))) {
6081 if (net_ratelimit())
6082 netif_err(tp
, drv
, tp
->dev
,
6083 "Failed to map TX fragments DMA!\n");
6087 /* Anti gcc 2.95.3 bugware (sic) */
6088 status
= opts
[0] | len
|
6089 (RingEnd
* !((entry
+ 1) % NUM_TX_DESC
));
6091 txd
->opts1
= cpu_to_le32(status
);
6092 txd
->opts2
= cpu_to_le32(opts
[1]);
6093 txd
->addr
= cpu_to_le64(mapping
);
6095 tp
->tx_skb
[entry
].len
= len
;
6099 tp
->tx_skb
[entry
].skb
= skb
;
6100 txd
->opts1
|= cpu_to_le32(LastFrag
);
6106 rtl8169_tx_clear_range(tp
, tp
->cur_tx
+ 1, cur_frag
);
6110 static bool rtl_test_hw_pad_bug(struct rtl8169_private
*tp
, struct sk_buff
*skb
)
6112 return skb
->len
< ETH_ZLEN
&& tp
->mac_version
== RTL_GIGA_MAC_VER_34
;
6115 static netdev_tx_t
rtl8169_start_xmit(struct sk_buff
*skb
,
6116 struct net_device
*dev
);
6117 /* r8169_csum_workaround()
6118 * The hw limites the value the transport offset. When the offset is out of the
6119 * range, calculate the checksum by sw.
6121 static void r8169_csum_workaround(struct rtl8169_private
*tp
,
6122 struct sk_buff
*skb
)
6124 if (skb_shinfo(skb
)->gso_size
) {
6125 netdev_features_t features
= tp
->dev
->features
;
6126 struct sk_buff
*segs
, *nskb
;
6128 features
&= ~(NETIF_F_SG
| NETIF_F_IPV6_CSUM
| NETIF_F_TSO6
);
6129 segs
= skb_gso_segment(skb
, features
);
6130 if (IS_ERR(segs
) || !segs
)
6137 rtl8169_start_xmit(nskb
, tp
->dev
);
6140 dev_consume_skb_any(skb
);
6141 } else if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
6142 if (skb_checksum_help(skb
) < 0)
6145 rtl8169_start_xmit(skb
, tp
->dev
);
6147 struct net_device_stats
*stats
;
6150 stats
= &tp
->dev
->stats
;
6151 stats
->tx_dropped
++;
6152 dev_kfree_skb_any(skb
);
6156 /* msdn_giant_send_check()
6157 * According to the document of microsoft, the TCP Pseudo Header excludes the
6158 * packet length for IPv6 TCP large packets.
6160 static int msdn_giant_send_check(struct sk_buff
*skb
)
6162 const struct ipv6hdr
*ipv6h
;
6166 ret
= skb_cow_head(skb
, 0);
6170 ipv6h
= ipv6_hdr(skb
);
6174 th
->check
= ~tcp_v6_check(0, &ipv6h
->saddr
, &ipv6h
->daddr
, 0);
6179 static bool rtl8169_tso_csum_v1(struct rtl8169_private
*tp
,
6180 struct sk_buff
*skb
, u32
*opts
)
6182 u32 mss
= skb_shinfo(skb
)->gso_size
;
6186 opts
[0] |= min(mss
, TD_MSS_MAX
) << TD0_MSS_SHIFT
;
6187 } else if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
6188 const struct iphdr
*ip
= ip_hdr(skb
);
6190 if (ip
->protocol
== IPPROTO_TCP
)
6191 opts
[0] |= TD0_IP_CS
| TD0_TCP_CS
;
6192 else if (ip
->protocol
== IPPROTO_UDP
)
6193 opts
[0] |= TD0_IP_CS
| TD0_UDP_CS
;
6201 static bool rtl8169_tso_csum_v2(struct rtl8169_private
*tp
,
6202 struct sk_buff
*skb
, u32
*opts
)
6204 u32 transport_offset
= (u32
)skb_transport_offset(skb
);
6205 u32 mss
= skb_shinfo(skb
)->gso_size
;
6208 if (transport_offset
> GTTCPHO_MAX
) {
6209 netif_warn(tp
, tx_err
, tp
->dev
,
6210 "Invalid transport offset 0x%x for TSO\n",
6215 switch (vlan_get_protocol(skb
)) {
6216 case htons(ETH_P_IP
):
6217 opts
[0] |= TD1_GTSENV4
;
6220 case htons(ETH_P_IPV6
):
6221 if (msdn_giant_send_check(skb
))
6224 opts
[0] |= TD1_GTSENV6
;
6232 opts
[0] |= transport_offset
<< GTTCPHO_SHIFT
;
6233 opts
[1] |= min(mss
, TD_MSS_MAX
) << TD1_MSS_SHIFT
;
6234 } else if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
6237 if (unlikely(rtl_test_hw_pad_bug(tp
, skb
)))
6238 return !(skb_checksum_help(skb
) || eth_skb_pad(skb
));
6240 if (transport_offset
> TCPHO_MAX
) {
6241 netif_warn(tp
, tx_err
, tp
->dev
,
6242 "Invalid transport offset 0x%x\n",
6247 switch (vlan_get_protocol(skb
)) {
6248 case htons(ETH_P_IP
):
6249 opts
[1] |= TD1_IPv4_CS
;
6250 ip_protocol
= ip_hdr(skb
)->protocol
;
6253 case htons(ETH_P_IPV6
):
6254 opts
[1] |= TD1_IPv6_CS
;
6255 ip_protocol
= ipv6_hdr(skb
)->nexthdr
;
6259 ip_protocol
= IPPROTO_RAW
;
6263 if (ip_protocol
== IPPROTO_TCP
)
6264 opts
[1] |= TD1_TCP_CS
;
6265 else if (ip_protocol
== IPPROTO_UDP
)
6266 opts
[1] |= TD1_UDP_CS
;
6270 opts
[1] |= transport_offset
<< TCPHO_SHIFT
;
6272 if (unlikely(rtl_test_hw_pad_bug(tp
, skb
)))
6273 return !eth_skb_pad(skb
);
6279 static netdev_tx_t
rtl8169_start_xmit(struct sk_buff
*skb
,
6280 struct net_device
*dev
)
6282 struct rtl8169_private
*tp
= netdev_priv(dev
);
6283 unsigned int entry
= tp
->cur_tx
% NUM_TX_DESC
;
6284 struct TxDesc
*txd
= tp
->TxDescArray
+ entry
;
6285 struct device
*d
= tp_to_dev(tp
);
6291 if (unlikely(!TX_FRAGS_READY_FOR(tp
, skb_shinfo(skb
)->nr_frags
))) {
6292 netif_err(tp
, drv
, dev
, "BUG! Tx Ring full when queue awake!\n");
6296 if (unlikely(le32_to_cpu(txd
->opts1
) & DescOwn
))
6299 opts
[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb
));
6302 if (!tp
->tso_csum(tp
, skb
, opts
)) {
6303 r8169_csum_workaround(tp
, skb
);
6304 return NETDEV_TX_OK
;
6307 len
= skb_headlen(skb
);
6308 mapping
= dma_map_single(d
, skb
->data
, len
, DMA_TO_DEVICE
);
6309 if (unlikely(dma_mapping_error(d
, mapping
))) {
6310 if (net_ratelimit())
6311 netif_err(tp
, drv
, dev
, "Failed to map TX DMA!\n");
6315 tp
->tx_skb
[entry
].len
= len
;
6316 txd
->addr
= cpu_to_le64(mapping
);
6318 frags
= rtl8169_xmit_frags(tp
, skb
, opts
);
6322 opts
[0] |= FirstFrag
;
6324 opts
[0] |= FirstFrag
| LastFrag
;
6325 tp
->tx_skb
[entry
].skb
= skb
;
6328 txd
->opts2
= cpu_to_le32(opts
[1]);
6330 skb_tx_timestamp(skb
);
6332 /* Force memory writes to complete before releasing descriptor */
6335 /* Anti gcc 2.95.3 bugware (sic) */
6336 status
= opts
[0] | len
| (RingEnd
* !((entry
+ 1) % NUM_TX_DESC
));
6337 txd
->opts1
= cpu_to_le32(status
);
6339 /* Force all memory writes to complete before notifying device */
6342 tp
->cur_tx
+= frags
+ 1;
6344 RTL_W8(tp
, TxPoll
, NPQ
);
6348 if (!TX_FRAGS_READY_FOR(tp
, MAX_SKB_FRAGS
)) {
6349 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6350 * not miss a ring update when it notices a stopped queue.
6353 netif_stop_queue(dev
);
6354 /* Sync with rtl_tx:
6355 * - publish queue status and cur_tx ring index (write barrier)
6356 * - refresh dirty_tx ring index (read barrier).
6357 * May the current thread have a pessimistic view of the ring
6358 * status and forget to wake up queue, a racing rtl_tx thread
6362 if (TX_FRAGS_READY_FOR(tp
, MAX_SKB_FRAGS
))
6363 netif_wake_queue(dev
);
6366 return NETDEV_TX_OK
;
6369 rtl8169_unmap_tx_skb(d
, tp
->tx_skb
+ entry
, txd
);
6371 dev_kfree_skb_any(skb
);
6372 dev
->stats
.tx_dropped
++;
6373 return NETDEV_TX_OK
;
6376 netif_stop_queue(dev
);
6377 dev
->stats
.tx_dropped
++;
6378 return NETDEV_TX_BUSY
;
6381 static void rtl8169_pcierr_interrupt(struct net_device
*dev
)
6383 struct rtl8169_private
*tp
= netdev_priv(dev
);
6384 struct pci_dev
*pdev
= tp
->pci_dev
;
6385 u16 pci_status
, pci_cmd
;
6387 pci_read_config_word(pdev
, PCI_COMMAND
, &pci_cmd
);
6388 pci_read_config_word(pdev
, PCI_STATUS
, &pci_status
);
6390 netif_err(tp
, intr
, dev
, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6391 pci_cmd
, pci_status
);
6394 * The recovery sequence below admits a very elaborated explanation:
6395 * - it seems to work;
6396 * - I did not see what else could be done;
6397 * - it makes iop3xx happy.
6399 * Feel free to adjust to your needs.
6401 if (pdev
->broken_parity_status
)
6402 pci_cmd
&= ~PCI_COMMAND_PARITY
;
6404 pci_cmd
|= PCI_COMMAND_SERR
| PCI_COMMAND_PARITY
;
6406 pci_write_config_word(pdev
, PCI_COMMAND
, pci_cmd
);
6408 pci_write_config_word(pdev
, PCI_STATUS
,
6409 pci_status
& (PCI_STATUS_DETECTED_PARITY
|
6410 PCI_STATUS_SIG_SYSTEM_ERROR
| PCI_STATUS_REC_MASTER_ABORT
|
6411 PCI_STATUS_REC_TARGET_ABORT
| PCI_STATUS_SIG_TARGET_ABORT
));
6413 /* The infamous DAC f*ckup only happens at boot time */
6414 if ((tp
->cp_cmd
& PCIDAC
) && !tp
->cur_rx
) {
6415 netif_info(tp
, intr
, dev
, "disabling PCI DAC\n");
6416 tp
->cp_cmd
&= ~PCIDAC
;
6417 RTL_W16(tp
, CPlusCmd
, tp
->cp_cmd
);
6418 dev
->features
&= ~NETIF_F_HIGHDMA
;
6421 rtl8169_hw_reset(tp
);
6423 rtl_schedule_task(tp
, RTL_FLAG_TASK_RESET_PENDING
);
6426 static void rtl_tx(struct net_device
*dev
, struct rtl8169_private
*tp
)
6428 unsigned int dirty_tx
, tx_left
;
6430 dirty_tx
= tp
->dirty_tx
;
6432 tx_left
= tp
->cur_tx
- dirty_tx
;
6434 while (tx_left
> 0) {
6435 unsigned int entry
= dirty_tx
% NUM_TX_DESC
;
6436 struct ring_info
*tx_skb
= tp
->tx_skb
+ entry
;
6439 status
= le32_to_cpu(tp
->TxDescArray
[entry
].opts1
);
6440 if (status
& DescOwn
)
6443 /* This barrier is needed to keep us from reading
6444 * any other fields out of the Tx descriptor until
6445 * we know the status of DescOwn
6449 rtl8169_unmap_tx_skb(tp_to_dev(tp
), tx_skb
,
6450 tp
->TxDescArray
+ entry
);
6451 if (status
& LastFrag
) {
6452 u64_stats_update_begin(&tp
->tx_stats
.syncp
);
6453 tp
->tx_stats
.packets
++;
6454 tp
->tx_stats
.bytes
+= tx_skb
->skb
->len
;
6455 u64_stats_update_end(&tp
->tx_stats
.syncp
);
6456 dev_consume_skb_any(tx_skb
->skb
);
6463 if (tp
->dirty_tx
!= dirty_tx
) {
6464 tp
->dirty_tx
= dirty_tx
;
6465 /* Sync with rtl8169_start_xmit:
6466 * - publish dirty_tx ring index (write barrier)
6467 * - refresh cur_tx ring index and queue status (read barrier)
6468 * May the current thread miss the stopped queue condition,
6469 * a racing xmit thread can only have a right view of the
6473 if (netif_queue_stopped(dev
) &&
6474 TX_FRAGS_READY_FOR(tp
, MAX_SKB_FRAGS
)) {
6475 netif_wake_queue(dev
);
6478 * 8168 hack: TxPoll requests are lost when the Tx packets are
6479 * too close. Let's kick an extra TxPoll request when a burst
6480 * of start_xmit activity is detected (if it is not detected,
6481 * it is slow enough). -- FR
6483 if (tp
->cur_tx
!= dirty_tx
)
6484 RTL_W8(tp
, TxPoll
, NPQ
);
6488 static inline int rtl8169_fragmented_frame(u32 status
)
6490 return (status
& (FirstFrag
| LastFrag
)) != (FirstFrag
| LastFrag
);
6493 static inline void rtl8169_rx_csum(struct sk_buff
*skb
, u32 opts1
)
6495 u32 status
= opts1
& RxProtoMask
;
6497 if (((status
== RxProtoTCP
) && !(opts1
& TCPFail
)) ||
6498 ((status
== RxProtoUDP
) && !(opts1
& UDPFail
)))
6499 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
6501 skb_checksum_none_assert(skb
);
6504 static struct sk_buff
*rtl8169_try_rx_copy(void *data
,
6505 struct rtl8169_private
*tp
,
6509 struct sk_buff
*skb
;
6510 struct device
*d
= tp_to_dev(tp
);
6512 data
= rtl8169_align(data
);
6513 dma_sync_single_for_cpu(d
, addr
, pkt_size
, DMA_FROM_DEVICE
);
6515 skb
= napi_alloc_skb(&tp
->napi
, pkt_size
);
6517 skb_copy_to_linear_data(skb
, data
, pkt_size
);
6518 dma_sync_single_for_device(d
, addr
, pkt_size
, DMA_FROM_DEVICE
);
6523 static int rtl_rx(struct net_device
*dev
, struct rtl8169_private
*tp
, u32 budget
)
6525 unsigned int cur_rx
, rx_left
;
6528 cur_rx
= tp
->cur_rx
;
6530 for (rx_left
= min(budget
, NUM_RX_DESC
); rx_left
> 0; rx_left
--, cur_rx
++) {
6531 unsigned int entry
= cur_rx
% NUM_RX_DESC
;
6532 struct RxDesc
*desc
= tp
->RxDescArray
+ entry
;
6535 status
= le32_to_cpu(desc
->opts1
);
6536 if (status
& DescOwn
)
6539 /* This barrier is needed to keep us from reading
6540 * any other fields out of the Rx descriptor until
6541 * we know the status of DescOwn
6545 if (unlikely(status
& RxRES
)) {
6546 netif_info(tp
, rx_err
, dev
, "Rx ERROR. status = %08x\n",
6548 dev
->stats
.rx_errors
++;
6549 if (status
& (RxRWT
| RxRUNT
))
6550 dev
->stats
.rx_length_errors
++;
6552 dev
->stats
.rx_crc_errors
++;
6553 /* RxFOVF is a reserved bit on later chip versions */
6554 if (tp
->mac_version
== RTL_GIGA_MAC_VER_01
&&
6556 rtl_schedule_task(tp
, RTL_FLAG_TASK_RESET_PENDING
);
6557 dev
->stats
.rx_fifo_errors
++;
6558 } else if (status
& (RxRUNT
| RxCRC
) &&
6559 !(status
& RxRWT
) &&
6560 dev
->features
& NETIF_F_RXALL
) {
6564 struct sk_buff
*skb
;
6569 addr
= le64_to_cpu(desc
->addr
);
6570 if (likely(!(dev
->features
& NETIF_F_RXFCS
)))
6571 pkt_size
= (status
& 0x00003fff) - 4;
6573 pkt_size
= status
& 0x00003fff;
6576 * The driver does not support incoming fragmented
6577 * frames. They are seen as a symptom of over-mtu
6580 if (unlikely(rtl8169_fragmented_frame(status
))) {
6581 dev
->stats
.rx_dropped
++;
6582 dev
->stats
.rx_length_errors
++;
6583 goto release_descriptor
;
6586 skb
= rtl8169_try_rx_copy(tp
->Rx_databuff
[entry
],
6587 tp
, pkt_size
, addr
);
6589 dev
->stats
.rx_dropped
++;
6590 goto release_descriptor
;
6593 rtl8169_rx_csum(skb
, status
);
6594 skb_put(skb
, pkt_size
);
6595 skb
->protocol
= eth_type_trans(skb
, dev
);
6597 rtl8169_rx_vlan_tag(desc
, skb
);
6599 if (skb
->pkt_type
== PACKET_MULTICAST
)
6600 dev
->stats
.multicast
++;
6602 napi_gro_receive(&tp
->napi
, skb
);
6604 u64_stats_update_begin(&tp
->rx_stats
.syncp
);
6605 tp
->rx_stats
.packets
++;
6606 tp
->rx_stats
.bytes
+= pkt_size
;
6607 u64_stats_update_end(&tp
->rx_stats
.syncp
);
6611 rtl8169_mark_to_asic(desc
);
6614 count
= cur_rx
- tp
->cur_rx
;
6615 tp
->cur_rx
= cur_rx
;
6620 static irqreturn_t
rtl8169_interrupt(int irq
, void *dev_instance
)
6622 struct rtl8169_private
*tp
= dev_instance
;
6623 u16 status
= rtl_get_events(tp
);
6625 if (status
== 0xffff || !(status
& (RTL_EVENT_NAPI
| tp
->event_slow
)))
6628 rtl_irq_disable(tp
);
6629 napi_schedule_irqoff(&tp
->napi
);
6635 * Workqueue context.
6637 static void rtl_slow_event_work(struct rtl8169_private
*tp
)
6639 struct net_device
*dev
= tp
->dev
;
6642 status
= rtl_get_events(tp
) & tp
->event_slow
;
6643 rtl_ack_events(tp
, status
);
6645 if (unlikely(status
& RxFIFOOver
)) {
6646 switch (tp
->mac_version
) {
6647 /* Work around for rx fifo overflow */
6648 case RTL_GIGA_MAC_VER_11
:
6649 netif_stop_queue(dev
);
6650 /* XXX - Hack alert. See rtl_task(). */
6651 set_bit(RTL_FLAG_TASK_RESET_PENDING
, tp
->wk
.flags
);
6657 if (unlikely(status
& SYSErr
))
6658 rtl8169_pcierr_interrupt(dev
);
6660 if (status
& LinkChg
)
6661 phy_mac_interrupt(dev
->phydev
);
6663 rtl_irq_enable_all(tp
);
6666 static void rtl_task(struct work_struct
*work
)
6668 static const struct {
6670 void (*action
)(struct rtl8169_private
*);
6672 /* XXX - keep rtl_slow_event_work() as first element. */
6673 { RTL_FLAG_TASK_SLOW_PENDING
, rtl_slow_event_work
},
6674 { RTL_FLAG_TASK_RESET_PENDING
, rtl_reset_work
},
6676 struct rtl8169_private
*tp
=
6677 container_of(work
, struct rtl8169_private
, wk
.work
);
6678 struct net_device
*dev
= tp
->dev
;
6683 if (!netif_running(dev
) ||
6684 !test_bit(RTL_FLAG_TASK_ENABLED
, tp
->wk
.flags
))
6687 for (i
= 0; i
< ARRAY_SIZE(rtl_work
); i
++) {
6690 pending
= test_and_clear_bit(rtl_work
[i
].bitnr
, tp
->wk
.flags
);
6692 rtl_work
[i
].action(tp
);
6696 rtl_unlock_work(tp
);
6699 static int rtl8169_poll(struct napi_struct
*napi
, int budget
)
6701 struct rtl8169_private
*tp
= container_of(napi
, struct rtl8169_private
, napi
);
6702 struct net_device
*dev
= tp
->dev
;
6703 u16 enable_mask
= RTL_EVENT_NAPI
| tp
->event_slow
;
6707 status
= rtl_get_events(tp
);
6708 rtl_ack_events(tp
, status
& ~tp
->event_slow
);
6710 work_done
= rtl_rx(dev
, tp
, (u32
) budget
);
6714 if (status
& tp
->event_slow
) {
6715 enable_mask
&= ~tp
->event_slow
;
6717 rtl_schedule_task(tp
, RTL_FLAG_TASK_SLOW_PENDING
);
6720 if (work_done
< budget
) {
6721 napi_complete_done(napi
, work_done
);
6723 rtl_irq_enable(tp
, enable_mask
);
6730 static void rtl8169_rx_missed(struct net_device
*dev
)
6732 struct rtl8169_private
*tp
= netdev_priv(dev
);
6734 if (tp
->mac_version
> RTL_GIGA_MAC_VER_06
)
6737 dev
->stats
.rx_missed_errors
+= RTL_R32(tp
, RxMissed
) & 0xffffff;
6738 RTL_W32(tp
, RxMissed
, 0);
6741 static void r8169_phylink_handler(struct net_device
*ndev
)
6743 struct rtl8169_private
*tp
= netdev_priv(ndev
);
6745 if (netif_carrier_ok(ndev
)) {
6746 rtl_link_chg_patch(tp
);
6747 pm_request_resume(&tp
->pci_dev
->dev
);
6749 pm_runtime_idle(&tp
->pci_dev
->dev
);
6752 if (net_ratelimit())
6753 phy_print_status(ndev
->phydev
);
6756 static int r8169_phy_connect(struct rtl8169_private
*tp
)
6758 struct phy_device
*phydev
= mdiobus_get_phy(tp
->mii_bus
, 0);
6759 phy_interface_t phy_mode
;
6762 phy_mode
= tp
->supports_gmii
? PHY_INTERFACE_MODE_GMII
:
6763 PHY_INTERFACE_MODE_MII
;
6765 ret
= phy_connect_direct(tp
->dev
, phydev
, r8169_phylink_handler
,
6770 if (!tp
->supports_gmii
)
6771 phy_set_max_speed(phydev
, SPEED_100
);
6773 /* Ensure to advertise everything, incl. pause */
6774 phydev
->advertising
= phydev
->supported
;
6776 phy_attached_info(phydev
);
6781 static void rtl8169_down(struct net_device
*dev
)
6783 struct rtl8169_private
*tp
= netdev_priv(dev
);
6785 phy_stop(dev
->phydev
);
6787 napi_disable(&tp
->napi
);
6788 netif_stop_queue(dev
);
6790 rtl8169_hw_reset(tp
);
6792 * At this point device interrupts can not be enabled in any function,
6793 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6794 * and napi is disabled (rtl8169_poll).
6796 rtl8169_rx_missed(dev
);
6798 /* Give a racing hard_start_xmit a few cycles to complete. */
6799 synchronize_sched();
6801 rtl8169_tx_clear(tp
);
6803 rtl8169_rx_clear(tp
);
6805 rtl_pll_power_down(tp
);
6808 static int rtl8169_close(struct net_device
*dev
)
6810 struct rtl8169_private
*tp
= netdev_priv(dev
);
6811 struct pci_dev
*pdev
= tp
->pci_dev
;
6813 pm_runtime_get_sync(&pdev
->dev
);
6815 /* Update counters before going down */
6816 rtl8169_update_counters(tp
);
6819 /* Clear all task flags */
6820 bitmap_zero(tp
->wk
.flags
, RTL_FLAG_MAX
);
6823 rtl_unlock_work(tp
);
6825 cancel_work_sync(&tp
->wk
.work
);
6827 phy_disconnect(dev
->phydev
);
6829 pci_free_irq(pdev
, 0, tp
);
6831 dma_free_coherent(&pdev
->dev
, R8169_RX_RING_BYTES
, tp
->RxDescArray
,
6833 dma_free_coherent(&pdev
->dev
, R8169_TX_RING_BYTES
, tp
->TxDescArray
,
6835 tp
->TxDescArray
= NULL
;
6836 tp
->RxDescArray
= NULL
;
6838 pm_runtime_put_sync(&pdev
->dev
);
6843 #ifdef CONFIG_NET_POLL_CONTROLLER
6844 static void rtl8169_netpoll(struct net_device
*dev
)
6846 struct rtl8169_private
*tp
= netdev_priv(dev
);
6848 rtl8169_interrupt(pci_irq_vector(tp
->pci_dev
, 0), tp
);
6852 static int rtl_open(struct net_device
*dev
)
6854 struct rtl8169_private
*tp
= netdev_priv(dev
);
6855 struct pci_dev
*pdev
= tp
->pci_dev
;
6856 int retval
= -ENOMEM
;
6858 pm_runtime_get_sync(&pdev
->dev
);
6861 * Rx and Tx descriptors needs 256 bytes alignment.
6862 * dma_alloc_coherent provides more.
6864 tp
->TxDescArray
= dma_alloc_coherent(&pdev
->dev
, R8169_TX_RING_BYTES
,
6865 &tp
->TxPhyAddr
, GFP_KERNEL
);
6866 if (!tp
->TxDescArray
)
6867 goto err_pm_runtime_put
;
6869 tp
->RxDescArray
= dma_alloc_coherent(&pdev
->dev
, R8169_RX_RING_BYTES
,
6870 &tp
->RxPhyAddr
, GFP_KERNEL
);
6871 if (!tp
->RxDescArray
)
6874 retval
= rtl8169_init_ring(tp
);
6878 INIT_WORK(&tp
->wk
.work
, rtl_task
);
6882 rtl_request_firmware(tp
);
6884 retval
= pci_request_irq(pdev
, 0, rtl8169_interrupt
, NULL
, tp
,
6887 goto err_release_fw_2
;
6889 retval
= r8169_phy_connect(tp
);
6895 set_bit(RTL_FLAG_TASK_ENABLED
, tp
->wk
.flags
);
6897 napi_enable(&tp
->napi
);
6899 rtl8169_init_phy(dev
, tp
);
6901 rtl_pll_power_up(tp
);
6905 if (!rtl8169_init_counter_offsets(tp
))
6906 netif_warn(tp
, hw
, dev
, "counter reset/update failed\n");
6908 phy_start(dev
->phydev
);
6909 netif_start_queue(dev
);
6911 rtl_unlock_work(tp
);
6913 pm_runtime_put_sync(&pdev
->dev
);
6918 pci_free_irq(pdev
, 0, tp
);
6920 rtl_release_firmware(tp
);
6921 rtl8169_rx_clear(tp
);
6923 dma_free_coherent(&pdev
->dev
, R8169_RX_RING_BYTES
, tp
->RxDescArray
,
6925 tp
->RxDescArray
= NULL
;
6927 dma_free_coherent(&pdev
->dev
, R8169_TX_RING_BYTES
, tp
->TxDescArray
,
6929 tp
->TxDescArray
= NULL
;
6931 pm_runtime_put_noidle(&pdev
->dev
);
6936 rtl8169_get_stats64(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
6938 struct rtl8169_private
*tp
= netdev_priv(dev
);
6939 struct pci_dev
*pdev
= tp
->pci_dev
;
6940 struct rtl8169_counters
*counters
= tp
->counters
;
6943 pm_runtime_get_noresume(&pdev
->dev
);
6945 if (netif_running(dev
) && pm_runtime_active(&pdev
->dev
))
6946 rtl8169_rx_missed(dev
);
6949 start
= u64_stats_fetch_begin_irq(&tp
->rx_stats
.syncp
);
6950 stats
->rx_packets
= tp
->rx_stats
.packets
;
6951 stats
->rx_bytes
= tp
->rx_stats
.bytes
;
6952 } while (u64_stats_fetch_retry_irq(&tp
->rx_stats
.syncp
, start
));
6955 start
= u64_stats_fetch_begin_irq(&tp
->tx_stats
.syncp
);
6956 stats
->tx_packets
= tp
->tx_stats
.packets
;
6957 stats
->tx_bytes
= tp
->tx_stats
.bytes
;
6958 } while (u64_stats_fetch_retry_irq(&tp
->tx_stats
.syncp
, start
));
6960 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
6961 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
6962 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
6963 stats
->rx_errors
= dev
->stats
.rx_errors
;
6964 stats
->rx_crc_errors
= dev
->stats
.rx_crc_errors
;
6965 stats
->rx_fifo_errors
= dev
->stats
.rx_fifo_errors
;
6966 stats
->rx_missed_errors
= dev
->stats
.rx_missed_errors
;
6967 stats
->multicast
= dev
->stats
.multicast
;
6970 * Fetch additonal counter values missing in stats collected by driver
6971 * from tally counters.
6973 if (pm_runtime_active(&pdev
->dev
))
6974 rtl8169_update_counters(tp
);
6977 * Subtract values fetched during initalization.
6978 * See rtl8169_init_counter_offsets for a description why we do that.
6980 stats
->tx_errors
= le64_to_cpu(counters
->tx_errors
) -
6981 le64_to_cpu(tp
->tc_offset
.tx_errors
);
6982 stats
->collisions
= le32_to_cpu(counters
->tx_multi_collision
) -
6983 le32_to_cpu(tp
->tc_offset
.tx_multi_collision
);
6984 stats
->tx_aborted_errors
= le16_to_cpu(counters
->tx_aborted
) -
6985 le16_to_cpu(tp
->tc_offset
.tx_aborted
);
6987 pm_runtime_put_noidle(&pdev
->dev
);
6990 static void rtl8169_net_suspend(struct net_device
*dev
)
6992 struct rtl8169_private
*tp
= netdev_priv(dev
);
6994 if (!netif_running(dev
))
6997 phy_stop(dev
->phydev
);
6998 netif_device_detach(dev
);
6999 netif_stop_queue(dev
);
7002 napi_disable(&tp
->napi
);
7003 /* Clear all task flags */
7004 bitmap_zero(tp
->wk
.flags
, RTL_FLAG_MAX
);
7006 rtl_unlock_work(tp
);
7008 rtl_pll_power_down(tp
);
7013 static int rtl8169_suspend(struct device
*device
)
7015 struct pci_dev
*pdev
= to_pci_dev(device
);
7016 struct net_device
*dev
= pci_get_drvdata(pdev
);
7017 struct rtl8169_private
*tp
= netdev_priv(dev
);
7019 rtl8169_net_suspend(dev
);
7020 clk_disable_unprepare(tp
->clk
);
7025 static void __rtl8169_resume(struct net_device
*dev
)
7027 struct rtl8169_private
*tp
= netdev_priv(dev
);
7029 netif_device_attach(dev
);
7031 rtl_pll_power_up(tp
);
7032 rtl8169_init_phy(dev
, tp
);
7034 phy_start(tp
->dev
->phydev
);
7037 napi_enable(&tp
->napi
);
7038 set_bit(RTL_FLAG_TASK_ENABLED
, tp
->wk
.flags
);
7039 rtl_unlock_work(tp
);
7041 rtl_schedule_task(tp
, RTL_FLAG_TASK_RESET_PENDING
);
7044 static int rtl8169_resume(struct device
*device
)
7046 struct pci_dev
*pdev
= to_pci_dev(device
);
7047 struct net_device
*dev
= pci_get_drvdata(pdev
);
7048 struct rtl8169_private
*tp
= netdev_priv(dev
);
7050 clk_prepare_enable(tp
->clk
);
7052 if (netif_running(dev
))
7053 __rtl8169_resume(dev
);
7058 static int rtl8169_runtime_suspend(struct device
*device
)
7060 struct pci_dev
*pdev
= to_pci_dev(device
);
7061 struct net_device
*dev
= pci_get_drvdata(pdev
);
7062 struct rtl8169_private
*tp
= netdev_priv(dev
);
7064 if (!tp
->TxDescArray
)
7068 __rtl8169_set_wol(tp
, WAKE_ANY
);
7069 rtl_unlock_work(tp
);
7071 rtl8169_net_suspend(dev
);
7073 /* Update counters before going runtime suspend */
7074 rtl8169_rx_missed(dev
);
7075 rtl8169_update_counters(tp
);
7080 static int rtl8169_runtime_resume(struct device
*device
)
7082 struct pci_dev
*pdev
= to_pci_dev(device
);
7083 struct net_device
*dev
= pci_get_drvdata(pdev
);
7084 struct rtl8169_private
*tp
= netdev_priv(dev
);
7085 rtl_rar_set(tp
, dev
->dev_addr
);
7087 if (!tp
->TxDescArray
)
7091 __rtl8169_set_wol(tp
, tp
->saved_wolopts
);
7092 rtl_unlock_work(tp
);
7094 __rtl8169_resume(dev
);
7099 static int rtl8169_runtime_idle(struct device
*device
)
7101 struct pci_dev
*pdev
= to_pci_dev(device
);
7102 struct net_device
*dev
= pci_get_drvdata(pdev
);
7104 if (!netif_running(dev
) || !netif_carrier_ok(dev
))
7105 pm_schedule_suspend(device
, 10000);
7110 static const struct dev_pm_ops rtl8169_pm_ops
= {
7111 .suspend
= rtl8169_suspend
,
7112 .resume
= rtl8169_resume
,
7113 .freeze
= rtl8169_suspend
,
7114 .thaw
= rtl8169_resume
,
7115 .poweroff
= rtl8169_suspend
,
7116 .restore
= rtl8169_resume
,
7117 .runtime_suspend
= rtl8169_runtime_suspend
,
7118 .runtime_resume
= rtl8169_runtime_resume
,
7119 .runtime_idle
= rtl8169_runtime_idle
,
7122 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
7124 #else /* !CONFIG_PM */
7126 #define RTL8169_PM_OPS NULL
7128 #endif /* !CONFIG_PM */
7130 static void rtl_wol_shutdown_quirk(struct rtl8169_private
*tp
)
7132 /* WoL fails with 8168b when the receiver is disabled. */
7133 switch (tp
->mac_version
) {
7134 case RTL_GIGA_MAC_VER_11
:
7135 case RTL_GIGA_MAC_VER_12
:
7136 case RTL_GIGA_MAC_VER_17
:
7137 pci_clear_master(tp
->pci_dev
);
7139 RTL_W8(tp
, ChipCmd
, CmdRxEnb
);
7141 RTL_R8(tp
, ChipCmd
);
7148 static void rtl_shutdown(struct pci_dev
*pdev
)
7150 struct net_device
*dev
= pci_get_drvdata(pdev
);
7151 struct rtl8169_private
*tp
= netdev_priv(dev
);
7153 rtl8169_net_suspend(dev
);
7155 /* Restore original MAC address */
7156 rtl_rar_set(tp
, dev
->perm_addr
);
7158 rtl8169_hw_reset(tp
);
7160 if (system_state
== SYSTEM_POWER_OFF
) {
7161 if (tp
->saved_wolopts
) {
7162 rtl_wol_suspend_quirk(tp
);
7163 rtl_wol_shutdown_quirk(tp
);
7166 pci_wake_from_d3(pdev
, true);
7167 pci_set_power_state(pdev
, PCI_D3hot
);
7171 static void rtl_remove_one(struct pci_dev
*pdev
)
7173 struct net_device
*dev
= pci_get_drvdata(pdev
);
7174 struct rtl8169_private
*tp
= netdev_priv(dev
);
7176 if (r8168_check_dash(tp
))
7177 rtl8168_driver_stop(tp
);
7179 netif_napi_del(&tp
->napi
);
7181 unregister_netdev(dev
);
7182 mdiobus_unregister(tp
->mii_bus
);
7184 rtl_release_firmware(tp
);
7186 if (pci_dev_run_wake(pdev
))
7187 pm_runtime_get_noresume(&pdev
->dev
);
7189 /* restore original MAC address */
7190 rtl_rar_set(tp
, dev
->perm_addr
);
7193 static const struct net_device_ops rtl_netdev_ops
= {
7194 .ndo_open
= rtl_open
,
7195 .ndo_stop
= rtl8169_close
,
7196 .ndo_get_stats64
= rtl8169_get_stats64
,
7197 .ndo_start_xmit
= rtl8169_start_xmit
,
7198 .ndo_tx_timeout
= rtl8169_tx_timeout
,
7199 .ndo_validate_addr
= eth_validate_addr
,
7200 .ndo_change_mtu
= rtl8169_change_mtu
,
7201 .ndo_fix_features
= rtl8169_fix_features
,
7202 .ndo_set_features
= rtl8169_set_features
,
7203 .ndo_set_mac_address
= rtl_set_mac_address
,
7204 .ndo_do_ioctl
= rtl8169_ioctl
,
7205 .ndo_set_rx_mode
= rtl_set_rx_mode
,
7206 #ifdef CONFIG_NET_POLL_CONTROLLER
7207 .ndo_poll_controller
= rtl8169_netpoll
,
7212 static const struct rtl_cfg_info
{
7213 void (*hw_start
)(struct rtl8169_private
*tp
);
7215 unsigned int has_gmii
:1;
7216 const struct rtl_coalesce_info
*coalesce_info
;
7218 } rtl_cfg_infos
[] = {
7220 .hw_start
= rtl_hw_start_8169
,
7221 .event_slow
= SYSErr
| LinkChg
| RxOverflow
| RxFIFOOver
,
7223 .coalesce_info
= rtl_coalesce_info_8169
,
7224 .default_ver
= RTL_GIGA_MAC_VER_01
,
7227 .hw_start
= rtl_hw_start_8168
,
7228 .event_slow
= SYSErr
| LinkChg
| RxOverflow
,
7230 .coalesce_info
= rtl_coalesce_info_8168_8136
,
7231 .default_ver
= RTL_GIGA_MAC_VER_11
,
7234 .hw_start
= rtl_hw_start_8101
,
7235 .event_slow
= SYSErr
| LinkChg
| RxOverflow
| RxFIFOOver
|
7237 .coalesce_info
= rtl_coalesce_info_8168_8136
,
7238 .default_ver
= RTL_GIGA_MAC_VER_13
,
7242 static int rtl_alloc_irq(struct rtl8169_private
*tp
)
7246 switch (tp
->mac_version
) {
7247 case RTL_GIGA_MAC_VER_02
... RTL_GIGA_MAC_VER_06
:
7248 RTL_W8(tp
, Cfg9346
, Cfg9346_Unlock
);
7249 RTL_W8(tp
, Config2
, RTL_R8(tp
, Config2
) & ~MSIEnable
);
7250 RTL_W8(tp
, Cfg9346
, Cfg9346_Lock
);
7252 case RTL_GIGA_MAC_VER_07
... RTL_GIGA_MAC_VER_17
:
7253 flags
= PCI_IRQ_LEGACY
;
7256 flags
= PCI_IRQ_ALL_TYPES
;
7260 return pci_alloc_irq_vectors(tp
->pci_dev
, 1, 1, flags
);
7263 DECLARE_RTL_COND(rtl_link_list_ready_cond
)
7265 return RTL_R8(tp
, MCU
) & LINK_LIST_RDY
;
7268 DECLARE_RTL_COND(rtl_rxtx_empty_cond
)
7270 return (RTL_R8(tp
, MCU
) & RXTX_EMPTY
) == RXTX_EMPTY
;
7273 static int r8169_mdio_read_reg(struct mii_bus
*mii_bus
, int phyaddr
, int phyreg
)
7275 struct rtl8169_private
*tp
= mii_bus
->priv
;
7280 return rtl_readphy(tp
, phyreg
);
7283 static int r8169_mdio_write_reg(struct mii_bus
*mii_bus
, int phyaddr
,
7284 int phyreg
, u16 val
)
7286 struct rtl8169_private
*tp
= mii_bus
->priv
;
7291 rtl_writephy(tp
, phyreg
, val
);
7296 static int r8169_mdio_register(struct rtl8169_private
*tp
)
7298 struct pci_dev
*pdev
= tp
->pci_dev
;
7299 struct phy_device
*phydev
;
7300 struct mii_bus
*new_bus
;
7303 new_bus
= devm_mdiobus_alloc(&pdev
->dev
);
7307 new_bus
->name
= "r8169";
7309 new_bus
->parent
= &pdev
->dev
;
7310 new_bus
->irq
[0] = PHY_IGNORE_INTERRUPT
;
7311 snprintf(new_bus
->id
, MII_BUS_ID_SIZE
, "r8169-%x",
7312 PCI_DEVID(pdev
->bus
->number
, pdev
->devfn
));
7314 new_bus
->read
= r8169_mdio_read_reg
;
7315 new_bus
->write
= r8169_mdio_write_reg
;
7317 ret
= mdiobus_register(new_bus
);
7321 phydev
= mdiobus_get_phy(new_bus
, 0);
7323 mdiobus_unregister(new_bus
);
7327 /* PHY will be woken up in rtl_open() */
7328 phy_suspend(phydev
);
7330 tp
->mii_bus
= new_bus
;
7335 static void rtl_hw_init_8168g(struct rtl8169_private
*tp
)
7339 tp
->ocp_base
= OCP_STD_PHY_BASE
;
7341 RTL_W32(tp
, MISC
, RTL_R32(tp
, MISC
) | RXDV_GATED_EN
);
7343 if (!rtl_udelay_loop_wait_high(tp
, &rtl_txcfg_empty_cond
, 100, 42))
7346 if (!rtl_udelay_loop_wait_high(tp
, &rtl_rxtx_empty_cond
, 100, 42))
7349 RTL_W8(tp
, ChipCmd
, RTL_R8(tp
, ChipCmd
) & ~(CmdTxEnb
| CmdRxEnb
));
7351 RTL_W8(tp
, MCU
, RTL_R8(tp
, MCU
) & ~NOW_IS_OOB
);
7353 data
= r8168_mac_ocp_read(tp
, 0xe8de);
7355 r8168_mac_ocp_write(tp
, 0xe8de, data
);
7357 if (!rtl_udelay_loop_wait_high(tp
, &rtl_link_list_ready_cond
, 100, 42))
7360 data
= r8168_mac_ocp_read(tp
, 0xe8de);
7362 r8168_mac_ocp_write(tp
, 0xe8de, data
);
7364 if (!rtl_udelay_loop_wait_high(tp
, &rtl_link_list_ready_cond
, 100, 42))
7368 static void rtl_hw_init_8168ep(struct rtl8169_private
*tp
)
7370 rtl8168ep_stop_cmac(tp
);
7371 rtl_hw_init_8168g(tp
);
7374 static void rtl_hw_initialize(struct rtl8169_private
*tp
)
7376 switch (tp
->mac_version
) {
7377 case RTL_GIGA_MAC_VER_40
... RTL_GIGA_MAC_VER_48
:
7378 rtl_hw_init_8168g(tp
);
7380 case RTL_GIGA_MAC_VER_49
... RTL_GIGA_MAC_VER_51
:
7381 rtl_hw_init_8168ep(tp
);
7388 /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
7389 static bool rtl_chip_supports_csum_v2(struct rtl8169_private
*tp
)
7391 switch (tp
->mac_version
) {
7392 case RTL_GIGA_MAC_VER_01
... RTL_GIGA_MAC_VER_06
:
7393 case RTL_GIGA_MAC_VER_10
... RTL_GIGA_MAC_VER_17
:
7400 static int rtl_jumbo_max(struct rtl8169_private
*tp
)
7402 /* Non-GBit versions don't support jumbo frames */
7403 if (!tp
->supports_gmii
)
7406 switch (tp
->mac_version
) {
7408 case RTL_GIGA_MAC_VER_01
... RTL_GIGA_MAC_VER_06
:
7411 case RTL_GIGA_MAC_VER_11
:
7412 case RTL_GIGA_MAC_VER_12
:
7413 case RTL_GIGA_MAC_VER_17
:
7416 case RTL_GIGA_MAC_VER_18
... RTL_GIGA_MAC_VER_24
:
7423 static void rtl_disable_clk(void *data
)
7425 clk_disable_unprepare(data
);
7428 static int rtl_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
7430 const struct rtl_cfg_info
*cfg
= rtl_cfg_infos
+ ent
->driver_data
;
7431 struct rtl8169_private
*tp
;
7432 struct net_device
*dev
;
7433 int chipset
, region
, i
;
7436 dev
= devm_alloc_etherdev(&pdev
->dev
, sizeof (*tp
));
7440 SET_NETDEV_DEV(dev
, &pdev
->dev
);
7441 dev
->netdev_ops
= &rtl_netdev_ops
;
7442 tp
= netdev_priv(dev
);
7445 tp
->msg_enable
= netif_msg_init(debug
.msg_enable
, R8169_MSG_DEFAULT
);
7446 tp
->supports_gmii
= cfg
->has_gmii
;
7448 /* Get the *optional* external "ether_clk" used on some boards */
7449 tp
->clk
= devm_clk_get(&pdev
->dev
, "ether_clk");
7450 if (IS_ERR(tp
->clk
)) {
7451 rc
= PTR_ERR(tp
->clk
);
7452 if (rc
== -ENOENT
) {
7453 /* clk-core allows NULL (for suspend / resume) */
7455 } else if (rc
== -EPROBE_DEFER
) {
7458 dev_err(&pdev
->dev
, "failed to get clk: %d\n", rc
);
7462 rc
= clk_prepare_enable(tp
->clk
);
7464 dev_err(&pdev
->dev
, "failed to enable clk: %d\n", rc
);
7468 rc
= devm_add_action_or_reset(&pdev
->dev
, rtl_disable_clk
,
7474 /* Disable ASPM completely as that cause random device stop working
7475 * problems as well as full system hangs for some PCIe devices users.
7477 pci_disable_link_state(pdev
, PCIE_LINK_STATE_L0S
| PCIE_LINK_STATE_L1
);
7479 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7480 rc
= pcim_enable_device(pdev
);
7482 dev_err(&pdev
->dev
, "enable failure\n");
7486 if (pcim_set_mwi(pdev
) < 0)
7487 dev_info(&pdev
->dev
, "Mem-Wr-Inval unavailable\n");
7489 /* use first MMIO region */
7490 region
= ffs(pci_select_bars(pdev
, IORESOURCE_MEM
)) - 1;
7492 dev_err(&pdev
->dev
, "no MMIO resource found\n");
7496 /* check for weird/broken PCI region reporting */
7497 if (pci_resource_len(pdev
, region
) < R8169_REGS_SIZE
) {
7498 dev_err(&pdev
->dev
, "Invalid PCI region size(s), aborting\n");
7502 rc
= pcim_iomap_regions(pdev
, BIT(region
), MODULENAME
);
7504 dev_err(&pdev
->dev
, "cannot remap MMIO, aborting\n");
7508 tp
->mmio_addr
= pcim_iomap_table(pdev
)[region
];
7510 if (!pci_is_pcie(pdev
))
7511 dev_info(&pdev
->dev
, "not PCI Express\n");
7513 /* Identify chip attached to board */
7514 rtl8169_get_mac_version(tp
, cfg
->default_ver
);
7516 if (rtl_tbi_enabled(tp
)) {
7517 dev_err(&pdev
->dev
, "TBI fiber mode not supported\n");
7521 tp
->cp_cmd
= RTL_R16(tp
, CPlusCmd
);
7523 if ((sizeof(dma_addr_t
) > 4) &&
7524 (use_dac
== 1 || (use_dac
== -1 && pci_is_pcie(pdev
) &&
7525 tp
->mac_version
>= RTL_GIGA_MAC_VER_18
)) &&
7526 !pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) &&
7527 !pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64))) {
7529 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7530 if (!pci_is_pcie(pdev
))
7531 tp
->cp_cmd
|= PCIDAC
;
7532 dev
->features
|= NETIF_F_HIGHDMA
;
7534 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
7536 dev_err(&pdev
->dev
, "DMA configuration failed\n");
7543 rtl_irq_disable(tp
);
7545 rtl_hw_initialize(tp
);
7549 rtl_ack_events(tp
, 0xffff);
7551 pci_set_master(pdev
);
7553 rtl_init_mdio_ops(tp
);
7554 rtl_init_jumbo_ops(tp
);
7556 rtl8169_print_mac_version(tp
);
7558 chipset
= tp
->mac_version
;
7560 rc
= rtl_alloc_irq(tp
);
7562 dev_err(&pdev
->dev
, "Can't allocate interrupt\n");
7566 tp
->saved_wolopts
= __rtl8169_get_wol(tp
);
7568 mutex_init(&tp
->wk
.mutex
);
7569 u64_stats_init(&tp
->rx_stats
.syncp
);
7570 u64_stats_init(&tp
->tx_stats
.syncp
);
7572 /* Get MAC address */
7573 switch (tp
->mac_version
) {
7574 u8 mac_addr
[ETH_ALEN
] __aligned(4);
7575 case RTL_GIGA_MAC_VER_35
... RTL_GIGA_MAC_VER_38
:
7576 case RTL_GIGA_MAC_VER_40
... RTL_GIGA_MAC_VER_51
:
7577 *(u32
*)&mac_addr
[0] = rtl_eri_read(tp
, 0xe0, ERIAR_EXGMAC
);
7578 *(u16
*)&mac_addr
[4] = rtl_eri_read(tp
, 0xe4, ERIAR_EXGMAC
);
7580 if (is_valid_ether_addr(mac_addr
))
7581 rtl_rar_set(tp
, mac_addr
);
7586 for (i
= 0; i
< ETH_ALEN
; i
++)
7587 dev
->dev_addr
[i
] = RTL_R8(tp
, MAC0
+ i
);
7589 dev
->ethtool_ops
= &rtl8169_ethtool_ops
;
7590 dev
->watchdog_timeo
= RTL8169_TX_TIMEOUT
;
7592 netif_napi_add(dev
, &tp
->napi
, rtl8169_poll
, NAPI_POLL_WEIGHT
);
7594 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7595 * properly for all devices */
7596 dev
->features
|= NETIF_F_RXCSUM
|
7597 NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_RX
;
7599 dev
->hw_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_TSO
|
7600 NETIF_F_RXCSUM
| NETIF_F_HW_VLAN_CTAG_TX
|
7601 NETIF_F_HW_VLAN_CTAG_RX
;
7602 dev
->vlan_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_TSO
|
7604 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
7606 tp
->cp_cmd
|= RxChkSum
| RxVlan
;
7609 * Pretend we are using VLANs; This bypasses a nasty bug where
7610 * Interrupts stop flowing on high load on 8110SCd controllers.
7612 if (tp
->mac_version
== RTL_GIGA_MAC_VER_05
)
7613 /* Disallow toggling */
7614 dev
->hw_features
&= ~NETIF_F_HW_VLAN_CTAG_RX
;
7616 if (rtl_chip_supports_csum_v2(tp
)) {
7617 tp
->tso_csum
= rtl8169_tso_csum_v2
;
7618 dev
->hw_features
|= NETIF_F_IPV6_CSUM
| NETIF_F_TSO6
;
7620 tp
->tso_csum
= rtl8169_tso_csum_v1
;
7623 dev
->hw_features
|= NETIF_F_RXALL
;
7624 dev
->hw_features
|= NETIF_F_RXFCS
;
7626 /* MTU range: 60 - hw-specific max */
7627 dev
->min_mtu
= ETH_ZLEN
;
7628 jumbo_max
= rtl_jumbo_max(tp
);
7629 dev
->max_mtu
= jumbo_max
;
7631 tp
->hw_start
= cfg
->hw_start
;
7632 tp
->event_slow
= cfg
->event_slow
;
7633 tp
->coalesce_info
= cfg
->coalesce_info
;
7635 tp
->rtl_fw
= RTL_FIRMWARE_UNKNOWN
;
7637 tp
->counters
= dmam_alloc_coherent (&pdev
->dev
, sizeof(*tp
->counters
),
7638 &tp
->counters_phys_addr
,
7643 pci_set_drvdata(pdev
, dev
);
7645 rc
= r8169_mdio_register(tp
);
7649 /* chip gets powered up in rtl_open() */
7650 rtl_pll_power_down(tp
);
7652 rc
= register_netdev(dev
);
7654 goto err_mdio_unregister
;
7656 netif_info(tp
, probe
, dev
, "%s, %pM, XID %08x, IRQ %d\n",
7657 rtl_chip_infos
[chipset
].name
, dev
->dev_addr
,
7658 (u32
)(RTL_R32(tp
, TxConfig
) & 0xfcf0f8ff),
7659 pci_irq_vector(pdev
, 0));
7661 if (jumbo_max
> JUMBO_1K
)
7662 netif_info(tp
, probe
, dev
,
7663 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7664 jumbo_max
, tp
->mac_version
<= RTL_GIGA_MAC_VER_06
?
7667 if (r8168_check_dash(tp
))
7668 rtl8168_driver_start(tp
);
7670 if (pci_dev_run_wake(pdev
))
7671 pm_runtime_put_sync(&pdev
->dev
);
7675 err_mdio_unregister
:
7676 mdiobus_unregister(tp
->mii_bus
);
7680 static struct pci_driver rtl8169_pci_driver
= {
7682 .id_table
= rtl8169_pci_tbl
,
7683 .probe
= rtl_init_one
,
7684 .remove
= rtl_remove_one
,
7685 .shutdown
= rtl_shutdown
,
7686 .driver
.pm
= RTL8169_PM_OPS
,
7689 module_pci_driver(rtl8169_pci_driver
);