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/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/if_vlan.h>
20 #include <linux/crc32.h>
23 #include <linux/tcp.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/firmware.h>
29 #include <linux/pci-aspm.h>
30 #include <linux/prefetch.h>
35 #define RTL8169_VERSION "2.3LK-NAPI"
36 #define MODULENAME "r8169"
37 #define PFX MODULENAME ": "
39 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
41 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
43 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
44 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
46 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
47 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
48 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
49 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
50 #define FIRMWARE_8168G_1 "rtl_nic/rtl8168g-1.fw"
53 #define assert(expr) \
55 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
56 #expr,__FILE__,__func__,__LINE__); \
58 #define dprintk(fmt, args...) \
59 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
61 #define assert(expr) do {} while (0)
62 #define dprintk(fmt, args...) do {} while (0)
63 #endif /* RTL8169_DEBUG */
65 #define R8169_MSG_DEFAULT \
66 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
68 #define TX_SLOTS_AVAIL(tp) \
69 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
71 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
72 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
73 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
75 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
76 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
77 static const int multicast_filter_limit = 32;
79 #define MAX_READ_REQUEST_SHIFT 12
80 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
81 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
83 #define R8169_REGS_SIZE 256
84 #define R8169_NAPI_WEIGHT 64
85 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
86 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
87 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
88 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
90 #define RTL8169_TX_TIMEOUT (6*HZ)
91 #define RTL8169_PHY_TIMEOUT (10*HZ)
93 /* write/read MMIO register */
94 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
95 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
96 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
97 #define RTL_R8(reg) readb (ioaddr + (reg))
98 #define RTL_R16(reg) readw (ioaddr + (reg))
99 #define RTL_R32(reg) readl (ioaddr + (reg))
102 RTL_GIGA_MAC_VER_01 = 0,
143 RTL_GIGA_MAC_NONE = 0xff,
146 enum rtl_tx_desc_version {
151 #define JUMBO_1K ETH_DATA_LEN
152 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
153 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
154 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
155 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
157 #define _R(NAME,TD,FW,SZ,B) { \
165 static const struct {
167 enum rtl_tx_desc_version txd_version;
171 } rtl_chip_infos[] = {
173 [RTL_GIGA_MAC_VER_01] =
174 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
175 [RTL_GIGA_MAC_VER_02] =
176 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
177 [RTL_GIGA_MAC_VER_03] =
178 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
179 [RTL_GIGA_MAC_VER_04] =
180 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
181 [RTL_GIGA_MAC_VER_05] =
182 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
183 [RTL_GIGA_MAC_VER_06] =
184 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
186 [RTL_GIGA_MAC_VER_07] =
187 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
188 [RTL_GIGA_MAC_VER_08] =
189 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
190 [RTL_GIGA_MAC_VER_09] =
191 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
192 [RTL_GIGA_MAC_VER_10] =
193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
194 [RTL_GIGA_MAC_VER_11] =
195 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
196 [RTL_GIGA_MAC_VER_12] =
197 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
198 [RTL_GIGA_MAC_VER_13] =
199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
200 [RTL_GIGA_MAC_VER_14] =
201 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
202 [RTL_GIGA_MAC_VER_15] =
203 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
204 [RTL_GIGA_MAC_VER_16] =
205 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
206 [RTL_GIGA_MAC_VER_17] =
207 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
208 [RTL_GIGA_MAC_VER_18] =
209 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
210 [RTL_GIGA_MAC_VER_19] =
211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
212 [RTL_GIGA_MAC_VER_20] =
213 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
214 [RTL_GIGA_MAC_VER_21] =
215 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
216 [RTL_GIGA_MAC_VER_22] =
217 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
218 [RTL_GIGA_MAC_VER_23] =
219 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
220 [RTL_GIGA_MAC_VER_24] =
221 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
222 [RTL_GIGA_MAC_VER_25] =
223 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
225 [RTL_GIGA_MAC_VER_26] =
226 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
228 [RTL_GIGA_MAC_VER_27] =
229 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
230 [RTL_GIGA_MAC_VER_28] =
231 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
232 [RTL_GIGA_MAC_VER_29] =
233 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
235 [RTL_GIGA_MAC_VER_30] =
236 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
238 [RTL_GIGA_MAC_VER_31] =
239 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
240 [RTL_GIGA_MAC_VER_32] =
241 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
243 [RTL_GIGA_MAC_VER_33] =
244 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
246 [RTL_GIGA_MAC_VER_34] =
247 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
249 [RTL_GIGA_MAC_VER_35] =
250 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
252 [RTL_GIGA_MAC_VER_36] =
253 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
255 [RTL_GIGA_MAC_VER_37] =
256 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
258 [RTL_GIGA_MAC_VER_38] =
259 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
261 [RTL_GIGA_MAC_VER_39] =
262 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
264 [RTL_GIGA_MAC_VER_40] =
265 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_1,
267 [RTL_GIGA_MAC_VER_41] =
268 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
278 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
279 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
280 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
281 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
282 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
283 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
284 { PCI_VENDOR_ID_DLINK, 0x4300,
285 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
286 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
287 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
288 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
289 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
290 { PCI_VENDOR_ID_LINKSYS, 0x1032,
291 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
293 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
297 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
299 static int rx_buf_sz = 16383;
306 MAC0 = 0, /* Ethernet hardware address. */
308 MAR0 = 8, /* Multicast filter. */
309 CounterAddrLow = 0x10,
310 CounterAddrHigh = 0x14,
311 TxDescStartAddrLow = 0x20,
312 TxDescStartAddrHigh = 0x24,
313 TxHDescStartAddrLow = 0x28,
314 TxHDescStartAddrHigh = 0x2c,
323 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
324 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
327 #define RX128_INT_EN (1 << 15) /* 8111c and later */
328 #define RX_MULTI_EN (1 << 14) /* 8111c only */
329 #define RXCFG_FIFO_SHIFT 13
330 /* No threshold before first PCI xfer */
331 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
332 #define RXCFG_DMA_SHIFT 8
333 /* Unlimited maximum PCI burst. */
334 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
341 #define PME_SIGNAL (1 << 5) /* 8168c and later */
352 RxDescAddrLow = 0xe4,
353 RxDescAddrHigh = 0xe8,
354 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
356 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
358 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
360 #define TxPacketMax (8064 >> 7)
361 #define EarlySize 0x27
364 FuncEventMask = 0xf4,
365 FuncPresetState = 0xf8,
366 FuncForceEvent = 0xfc,
369 enum rtl8110_registers {
375 enum rtl8168_8101_registers {
378 #define CSIAR_FLAG 0x80000000
379 #define CSIAR_WRITE_CMD 0x80000000
380 #define CSIAR_BYTE_ENABLE 0x0f
381 #define CSIAR_BYTE_ENABLE_SHIFT 12
382 #define CSIAR_ADDR_MASK 0x0fff
383 #define CSIAR_FUNC_CARD 0x00000000
384 #define CSIAR_FUNC_SDIO 0x00010000
385 #define CSIAR_FUNC_NIC 0x00020000
388 #define EPHYAR_FLAG 0x80000000
389 #define EPHYAR_WRITE_CMD 0x80000000
390 #define EPHYAR_REG_MASK 0x1f
391 #define EPHYAR_REG_SHIFT 16
392 #define EPHYAR_DATA_MASK 0xffff
394 #define PFM_EN (1 << 6)
396 #define FIX_NAK_1 (1 << 4)
397 #define FIX_NAK_2 (1 << 3)
400 #define NOW_IS_OOB (1 << 7)
401 #define TX_EMPTY (1 << 5)
402 #define RX_EMPTY (1 << 4)
403 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
404 #define EN_NDP (1 << 3)
405 #define EN_OOB_RESET (1 << 2)
406 #define LINK_LIST_RDY (1 << 1)
408 #define EFUSEAR_FLAG 0x80000000
409 #define EFUSEAR_WRITE_CMD 0x80000000
410 #define EFUSEAR_READ_CMD 0x00000000
411 #define EFUSEAR_REG_MASK 0x03ff
412 #define EFUSEAR_REG_SHIFT 8
413 #define EFUSEAR_DATA_MASK 0xff
416 enum rtl8168_registers {
421 #define ERIAR_FLAG 0x80000000
422 #define ERIAR_WRITE_CMD 0x80000000
423 #define ERIAR_READ_CMD 0x00000000
424 #define ERIAR_ADDR_BYTE_ALIGN 4
425 #define ERIAR_TYPE_SHIFT 16
426 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
427 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
428 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
429 #define ERIAR_MASK_SHIFT 12
430 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
431 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
432 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
433 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
434 EPHY_RXER_NUM = 0x7c,
435 OCPDR = 0xb0, /* OCP GPHY access */
436 #define OCPDR_WRITE_CMD 0x80000000
437 #define OCPDR_READ_CMD 0x00000000
438 #define OCPDR_REG_MASK 0x7f
439 #define OCPDR_GPHY_REG_SHIFT 16
440 #define OCPDR_DATA_MASK 0xffff
442 #define OCPAR_FLAG 0x80000000
443 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
444 #define OCPAR_GPHY_READ_CMD 0x0000f060
446 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
447 MISC = 0xf0, /* 8168e only. */
448 #define TXPLA_RST (1 << 29)
449 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
450 #define PWM_EN (1 << 22)
451 #define RXDV_GATED_EN (1 << 19)
452 #define EARLY_TALLY_EN (1 << 16)
455 enum rtl_register_content {
456 /* InterruptStatusBits */
460 TxDescUnavail = 0x0080,
484 /* TXPoll register p.5 */
485 HPQ = 0x80, /* Poll cmd on the high prio queue */
486 NPQ = 0x40, /* Poll cmd on the low prio queue */
487 FSWInt = 0x01, /* Forced software interrupt */
491 Cfg9346_Unlock = 0xc0,
496 AcceptBroadcast = 0x08,
497 AcceptMulticast = 0x04,
499 AcceptAllPhys = 0x01,
500 #define RX_CONFIG_ACCEPT_MASK 0x3f
503 TxInterFrameGapShift = 24,
504 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
506 /* Config1 register p.24 */
509 Speed_down = (1 << 4),
513 PMEnable = (1 << 0), /* Power Management Enable */
515 /* Config2 register p. 25 */
516 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
517 PCI_Clock_66MHz = 0x01,
518 PCI_Clock_33MHz = 0x00,
520 /* Config3 register p.25 */
521 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
522 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
523 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
524 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
526 /* Config4 register */
527 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
529 /* Config5 register p.27 */
530 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
531 MWF = (1 << 5), /* Accept Multicast wakeup frame */
532 UWF = (1 << 4), /* Accept Unicast wakeup frame */
534 LanWake = (1 << 1), /* LanWake enable/disable */
535 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
538 TBIReset = 0x80000000,
539 TBILoopback = 0x40000000,
540 TBINwEnable = 0x20000000,
541 TBINwRestart = 0x10000000,
542 TBILinkOk = 0x02000000,
543 TBINwComplete = 0x01000000,
546 EnableBist = (1 << 15), // 8168 8101
547 Mac_dbgo_oe = (1 << 14), // 8168 8101
548 Normal_mode = (1 << 13), // unused
549 Force_half_dup = (1 << 12), // 8168 8101
550 Force_rxflow_en = (1 << 11), // 8168 8101
551 Force_txflow_en = (1 << 10), // 8168 8101
552 Cxpl_dbg_sel = (1 << 9), // 8168 8101
553 ASF = (1 << 8), // 8168 8101
554 PktCntrDisable = (1 << 7), // 8168 8101
555 Mac_dbgo_sel = 0x001c, // 8168
560 INTT_0 = 0x0000, // 8168
561 INTT_1 = 0x0001, // 8168
562 INTT_2 = 0x0002, // 8168
563 INTT_3 = 0x0003, // 8168
565 /* rtl8169_PHYstatus */
576 TBILinkOK = 0x02000000,
578 /* DumpCounterCommand */
583 /* First doubleword. */
584 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
585 RingEnd = (1 << 30), /* End of descriptor ring */
586 FirstFrag = (1 << 29), /* First segment of a packet */
587 LastFrag = (1 << 28), /* Final segment of a packet */
591 enum rtl_tx_desc_bit {
592 /* First doubleword. */
593 TD_LSO = (1 << 27), /* Large Send Offload */
594 #define TD_MSS_MAX 0x07ffu /* MSS value */
596 /* Second doubleword. */
597 TxVlanTag = (1 << 17), /* Add VLAN tag */
600 /* 8169, 8168b and 810x except 8102e. */
601 enum rtl_tx_desc_bit_0 {
602 /* First doubleword. */
603 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
604 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
605 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
606 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
609 /* 8102e, 8168c and beyond. */
610 enum rtl_tx_desc_bit_1 {
611 /* Second doubleword. */
612 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
613 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
614 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
615 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
618 static const struct rtl_tx_desc_info {
625 } tx_desc_info [] = {
628 .udp = TD0_IP_CS | TD0_UDP_CS,
629 .tcp = TD0_IP_CS | TD0_TCP_CS
631 .mss_shift = TD0_MSS_SHIFT,
636 .udp = TD1_IP_CS | TD1_UDP_CS,
637 .tcp = TD1_IP_CS | TD1_TCP_CS
639 .mss_shift = TD1_MSS_SHIFT,
644 enum rtl_rx_desc_bit {
646 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
647 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
649 #define RxProtoUDP (PID1)
650 #define RxProtoTCP (PID0)
651 #define RxProtoIP (PID1 | PID0)
652 #define RxProtoMask RxProtoIP
654 IPFail = (1 << 16), /* IP checksum failed */
655 UDPFail = (1 << 15), /* UDP/IP checksum failed */
656 TCPFail = (1 << 14), /* TCP/IP checksum failed */
657 RxVlanTag = (1 << 16), /* VLAN tag available */
660 #define RsvdMask 0x3fffc000
677 u8 __pad[sizeof(void *) - sizeof(u32)];
681 RTL_FEATURE_WOL = (1 << 0),
682 RTL_FEATURE_MSI = (1 << 1),
683 RTL_FEATURE_GMII = (1 << 2),
686 struct rtl8169_counters {
693 __le32 tx_one_collision;
694 __le32 tx_multi_collision;
703 RTL_FLAG_TASK_ENABLED,
704 RTL_FLAG_TASK_SLOW_PENDING,
705 RTL_FLAG_TASK_RESET_PENDING,
706 RTL_FLAG_TASK_PHY_PENDING,
710 struct rtl8169_stats {
713 struct u64_stats_sync syncp;
716 struct rtl8169_private {
717 void __iomem *mmio_addr; /* memory map physical address */
718 struct pci_dev *pci_dev;
719 struct net_device *dev;
720 struct napi_struct napi;
724 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
725 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
727 struct rtl8169_stats rx_stats;
728 struct rtl8169_stats tx_stats;
729 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
730 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
731 dma_addr_t TxPhyAddr;
732 dma_addr_t RxPhyAddr;
733 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
734 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
735 struct timer_list timer;
741 void (*write)(struct rtl8169_private *, int, int);
742 int (*read)(struct rtl8169_private *, int);
745 struct pll_power_ops {
746 void (*down)(struct rtl8169_private *);
747 void (*up)(struct rtl8169_private *);
751 void (*enable)(struct rtl8169_private *);
752 void (*disable)(struct rtl8169_private *);
756 void (*write)(struct rtl8169_private *, int, int);
757 u32 (*read)(struct rtl8169_private *, int);
760 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
761 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
762 void (*phy_reset_enable)(struct rtl8169_private *tp);
763 void (*hw_start)(struct net_device *);
764 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
765 unsigned int (*link_ok)(void __iomem *);
766 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
769 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
771 struct work_struct work;
776 struct mii_if_info mii;
777 struct rtl8169_counters counters;
782 const struct firmware *fw;
784 #define RTL_VER_SIZE 32
786 char version[RTL_VER_SIZE];
788 struct rtl_fw_phy_action {
793 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
798 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
799 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
800 module_param(use_dac, int, 0);
801 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
802 module_param_named(debug, debug.msg_enable, int, 0);
803 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
804 MODULE_LICENSE("GPL");
805 MODULE_VERSION(RTL8169_VERSION);
806 MODULE_FIRMWARE(FIRMWARE_8168D_1);
807 MODULE_FIRMWARE(FIRMWARE_8168D_2);
808 MODULE_FIRMWARE(FIRMWARE_8168E_1);
809 MODULE_FIRMWARE(FIRMWARE_8168E_2);
810 MODULE_FIRMWARE(FIRMWARE_8168E_3);
811 MODULE_FIRMWARE(FIRMWARE_8105E_1);
812 MODULE_FIRMWARE(FIRMWARE_8168F_1);
813 MODULE_FIRMWARE(FIRMWARE_8168F_2);
814 MODULE_FIRMWARE(FIRMWARE_8402_1);
815 MODULE_FIRMWARE(FIRMWARE_8411_1);
816 MODULE_FIRMWARE(FIRMWARE_8106E_1);
817 MODULE_FIRMWARE(FIRMWARE_8168G_1);
819 static void rtl_lock_work(struct rtl8169_private *tp)
821 mutex_lock(&tp->wk.mutex);
824 static void rtl_unlock_work(struct rtl8169_private *tp)
826 mutex_unlock(&tp->wk.mutex);
829 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
831 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
832 PCI_EXP_DEVCTL_READRQ, force);
836 bool (*check)(struct rtl8169_private *);
840 static void rtl_udelay(unsigned int d)
845 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
846 void (*delay)(unsigned int), unsigned int d, int n,
851 for (i = 0; i < n; i++) {
853 if (c->check(tp) == high)
856 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
857 c->msg, !high, n, d);
861 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
862 const struct rtl_cond *c,
863 unsigned int d, int n)
865 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
868 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
869 const struct rtl_cond *c,
870 unsigned int d, int n)
872 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
875 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
876 const struct rtl_cond *c,
877 unsigned int d, int n)
879 return rtl_loop_wait(tp, c, msleep, d, n, true);
882 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
883 const struct rtl_cond *c,
884 unsigned int d, int n)
886 return rtl_loop_wait(tp, c, msleep, d, n, false);
889 #define DECLARE_RTL_COND(name) \
890 static bool name ## _check(struct rtl8169_private *); \
892 static const struct rtl_cond name = { \
893 .check = name ## _check, \
897 static bool name ## _check(struct rtl8169_private *tp)
899 DECLARE_RTL_COND(rtl_ocpar_cond)
901 void __iomem *ioaddr = tp->mmio_addr;
903 return RTL_R32(OCPAR) & OCPAR_FLAG;
906 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
908 void __iomem *ioaddr = tp->mmio_addr;
910 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
912 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
916 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
918 void __iomem *ioaddr = tp->mmio_addr;
920 RTL_W32(OCPDR, data);
921 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
923 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
926 DECLARE_RTL_COND(rtl_eriar_cond)
928 void __iomem *ioaddr = tp->mmio_addr;
930 return RTL_R32(ERIAR) & ERIAR_FLAG;
933 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
935 void __iomem *ioaddr = tp->mmio_addr;
938 RTL_W32(ERIAR, 0x800010e8);
941 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
944 ocp_write(tp, 0x1, 0x30, 0x00000001);
947 #define OOB_CMD_RESET 0x00
948 #define OOB_CMD_DRIVER_START 0x05
949 #define OOB_CMD_DRIVER_STOP 0x06
951 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
953 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
956 DECLARE_RTL_COND(rtl_ocp_read_cond)
960 reg = rtl8168_get_ocp_reg(tp);
962 return ocp_read(tp, 0x0f, reg) & 0x00000800;
965 static void rtl8168_driver_start(struct rtl8169_private *tp)
967 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
969 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
972 static void rtl8168_driver_stop(struct rtl8169_private *tp)
974 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
976 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
979 static int r8168dp_check_dash(struct rtl8169_private *tp)
981 u16 reg = rtl8168_get_ocp_reg(tp);
983 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
986 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
988 if (reg & 0xffff0001) {
989 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
995 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
997 void __iomem *ioaddr = tp->mmio_addr;
999 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1002 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1004 void __iomem *ioaddr = tp->mmio_addr;
1006 if (rtl_ocp_reg_failure(tp, reg))
1009 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1011 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1014 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1016 void __iomem *ioaddr = tp->mmio_addr;
1018 if (rtl_ocp_reg_failure(tp, reg))
1021 RTL_W32(GPHY_OCP, reg << 15);
1023 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1024 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1027 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1029 void __iomem *ioaddr = tp->mmio_addr;
1031 if (rtl_ocp_reg_failure(tp, reg))
1034 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
1037 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1039 void __iomem *ioaddr = tp->mmio_addr;
1041 if (rtl_ocp_reg_failure(tp, reg))
1044 RTL_W32(OCPDR, reg << 15);
1046 return RTL_R32(OCPDR);
1049 #define OCP_STD_PHY_BASE 0xa400
1051 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1054 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1058 if (tp->ocp_base != OCP_STD_PHY_BASE)
1061 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1064 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1066 if (tp->ocp_base != OCP_STD_PHY_BASE)
1069 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1072 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1075 tp->ocp_base = value << 4;
1079 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1082 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1084 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1087 DECLARE_RTL_COND(rtl_phyar_cond)
1089 void __iomem *ioaddr = tp->mmio_addr;
1091 return RTL_R32(PHYAR) & 0x80000000;
1094 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1096 void __iomem *ioaddr = tp->mmio_addr;
1098 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1100 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1102 * According to hardware specs a 20us delay is required after write
1103 * complete indication, but before sending next command.
1108 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1110 void __iomem *ioaddr = tp->mmio_addr;
1113 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
1115 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1116 RTL_R32(PHYAR) & 0xffff : ~0;
1119 * According to hardware specs a 20us delay is required after read
1120 * complete indication, but before sending next command.
1127 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1129 void __iomem *ioaddr = tp->mmio_addr;
1131 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1132 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1133 RTL_W32(EPHY_RXER_NUM, 0);
1135 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1138 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1140 r8168dp_1_mdio_access(tp, reg,
1141 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1144 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1146 void __iomem *ioaddr = tp->mmio_addr;
1148 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1151 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1152 RTL_W32(EPHY_RXER_NUM, 0);
1154 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1155 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
1158 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1160 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1162 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1165 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1167 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1170 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1172 void __iomem *ioaddr = tp->mmio_addr;
1174 r8168dp_2_mdio_start(ioaddr);
1176 r8169_mdio_write(tp, reg, value);
1178 r8168dp_2_mdio_stop(ioaddr);
1181 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1183 void __iomem *ioaddr = tp->mmio_addr;
1186 r8168dp_2_mdio_start(ioaddr);
1188 value = r8169_mdio_read(tp, reg);
1190 r8168dp_2_mdio_stop(ioaddr);
1195 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1197 tp->mdio_ops.write(tp, location, val);
1200 static int rtl_readphy(struct rtl8169_private *tp, int location)
1202 return tp->mdio_ops.read(tp, location);
1205 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1207 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1210 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1214 val = rtl_readphy(tp, reg_addr);
1215 rtl_writephy(tp, reg_addr, (val | p) & ~m);
1218 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1221 struct rtl8169_private *tp = netdev_priv(dev);
1223 rtl_writephy(tp, location, val);
1226 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1228 struct rtl8169_private *tp = netdev_priv(dev);
1230 return rtl_readphy(tp, location);
1233 DECLARE_RTL_COND(rtl_ephyar_cond)
1235 void __iomem *ioaddr = tp->mmio_addr;
1237 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1240 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1242 void __iomem *ioaddr = tp->mmio_addr;
1244 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1245 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1247 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1252 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1254 void __iomem *ioaddr = tp->mmio_addr;
1256 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1258 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1259 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
1262 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1265 void __iomem *ioaddr = tp->mmio_addr;
1267 BUG_ON((addr & 3) || (mask == 0));
1268 RTL_W32(ERIDR, val);
1269 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1271 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1274 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1276 void __iomem *ioaddr = tp->mmio_addr;
1278 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1280 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1281 RTL_R32(ERIDR) : ~0;
1284 static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1289 val = rtl_eri_read(tp, addr, type);
1290 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1299 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1300 const struct exgmac_reg *r, int len)
1303 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1308 DECLARE_RTL_COND(rtl_efusear_cond)
1310 void __iomem *ioaddr = tp->mmio_addr;
1312 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1315 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1317 void __iomem *ioaddr = tp->mmio_addr;
1319 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1321 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1322 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1325 static u16 rtl_get_events(struct rtl8169_private *tp)
1327 void __iomem *ioaddr = tp->mmio_addr;
1329 return RTL_R16(IntrStatus);
1332 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1334 void __iomem *ioaddr = tp->mmio_addr;
1336 RTL_W16(IntrStatus, bits);
1340 static void rtl_irq_disable(struct rtl8169_private *tp)
1342 void __iomem *ioaddr = tp->mmio_addr;
1344 RTL_W16(IntrMask, 0);
1348 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1350 void __iomem *ioaddr = tp->mmio_addr;
1352 RTL_W16(IntrMask, bits);
1355 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1356 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1357 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1359 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1361 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1364 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1366 void __iomem *ioaddr = tp->mmio_addr;
1368 rtl_irq_disable(tp);
1369 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1373 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1375 void __iomem *ioaddr = tp->mmio_addr;
1377 return RTL_R32(TBICSR) & TBIReset;
1380 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1382 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1385 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1387 return RTL_R32(TBICSR) & TBILinkOk;
1390 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1392 return RTL_R8(PHYstatus) & LinkStatus;
1395 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1397 void __iomem *ioaddr = tp->mmio_addr;
1399 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1402 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1406 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1407 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1410 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1412 void __iomem *ioaddr = tp->mmio_addr;
1413 struct net_device *dev = tp->dev;
1415 if (!netif_running(dev))
1418 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1419 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1420 if (RTL_R8(PHYstatus) & _1000bpsF) {
1421 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1423 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1425 } else if (RTL_R8(PHYstatus) & _100bps) {
1426 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1428 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1431 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1433 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1436 /* Reset packet filter */
1437 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1439 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1441 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1442 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1443 if (RTL_R8(PHYstatus) & _1000bpsF) {
1444 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1446 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1449 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1451 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1454 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1455 if (RTL_R8(PHYstatus) & _10bps) {
1456 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1458 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1461 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1467 static void __rtl8169_check_link_status(struct net_device *dev,
1468 struct rtl8169_private *tp,
1469 void __iomem *ioaddr, bool pm)
1471 if (tp->link_ok(ioaddr)) {
1472 rtl_link_chg_patch(tp);
1473 /* This is to cancel a scheduled suspend if there's one. */
1475 pm_request_resume(&tp->pci_dev->dev);
1476 netif_carrier_on(dev);
1477 if (net_ratelimit())
1478 netif_info(tp, ifup, dev, "link up\n");
1480 netif_carrier_off(dev);
1481 netif_info(tp, ifdown, dev, "link down\n");
1483 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1487 static void rtl8169_check_link_status(struct net_device *dev,
1488 struct rtl8169_private *tp,
1489 void __iomem *ioaddr)
1491 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1494 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1496 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1498 void __iomem *ioaddr = tp->mmio_addr;
1502 options = RTL_R8(Config1);
1503 if (!(options & PMEnable))
1506 options = RTL_R8(Config3);
1507 if (options & LinkUp)
1508 wolopts |= WAKE_PHY;
1509 if (options & MagicPacket)
1510 wolopts |= WAKE_MAGIC;
1512 options = RTL_R8(Config5);
1514 wolopts |= WAKE_UCAST;
1516 wolopts |= WAKE_BCAST;
1518 wolopts |= WAKE_MCAST;
1523 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1525 struct rtl8169_private *tp = netdev_priv(dev);
1529 wol->supported = WAKE_ANY;
1530 wol->wolopts = __rtl8169_get_wol(tp);
1532 rtl_unlock_work(tp);
1535 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1537 void __iomem *ioaddr = tp->mmio_addr;
1539 static const struct {
1544 { WAKE_PHY, Config3, LinkUp },
1545 { WAKE_MAGIC, Config3, MagicPacket },
1546 { WAKE_UCAST, Config5, UWF },
1547 { WAKE_BCAST, Config5, BWF },
1548 { WAKE_MCAST, Config5, MWF },
1549 { WAKE_ANY, Config5, LanWake }
1553 RTL_W8(Cfg9346, Cfg9346_Unlock);
1555 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1556 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1557 if (wolopts & cfg[i].opt)
1558 options |= cfg[i].mask;
1559 RTL_W8(cfg[i].reg, options);
1562 switch (tp->mac_version) {
1563 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1564 options = RTL_R8(Config1) & ~PMEnable;
1566 options |= PMEnable;
1567 RTL_W8(Config1, options);
1570 options = RTL_R8(Config2) & ~PME_SIGNAL;
1572 options |= PME_SIGNAL;
1573 RTL_W8(Config2, options);
1577 RTL_W8(Cfg9346, Cfg9346_Lock);
1580 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1582 struct rtl8169_private *tp = netdev_priv(dev);
1587 tp->features |= RTL_FEATURE_WOL;
1589 tp->features &= ~RTL_FEATURE_WOL;
1590 __rtl8169_set_wol(tp, wol->wolopts);
1592 rtl_unlock_work(tp);
1594 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1599 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1601 return rtl_chip_infos[tp->mac_version].fw_name;
1604 static void rtl8169_get_drvinfo(struct net_device *dev,
1605 struct ethtool_drvinfo *info)
1607 struct rtl8169_private *tp = netdev_priv(dev);
1608 struct rtl_fw *rtl_fw = tp->rtl_fw;
1610 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1611 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1612 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1613 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1614 if (!IS_ERR_OR_NULL(rtl_fw))
1615 strlcpy(info->fw_version, rtl_fw->version,
1616 sizeof(info->fw_version));
1619 static int rtl8169_get_regs_len(struct net_device *dev)
1621 return R8169_REGS_SIZE;
1624 static int rtl8169_set_speed_tbi(struct net_device *dev,
1625 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1627 struct rtl8169_private *tp = netdev_priv(dev);
1628 void __iomem *ioaddr = tp->mmio_addr;
1632 reg = RTL_R32(TBICSR);
1633 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1634 (duplex == DUPLEX_FULL)) {
1635 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1636 } else if (autoneg == AUTONEG_ENABLE)
1637 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1639 netif_warn(tp, link, dev,
1640 "incorrect speed setting refused in TBI mode\n");
1647 static int rtl8169_set_speed_xmii(struct net_device *dev,
1648 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1650 struct rtl8169_private *tp = netdev_priv(dev);
1651 int giga_ctrl, bmcr;
1654 rtl_writephy(tp, 0x1f, 0x0000);
1656 if (autoneg == AUTONEG_ENABLE) {
1659 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1660 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1661 ADVERTISE_100HALF | ADVERTISE_100FULL);
1663 if (adv & ADVERTISED_10baseT_Half)
1664 auto_nego |= ADVERTISE_10HALF;
1665 if (adv & ADVERTISED_10baseT_Full)
1666 auto_nego |= ADVERTISE_10FULL;
1667 if (adv & ADVERTISED_100baseT_Half)
1668 auto_nego |= ADVERTISE_100HALF;
1669 if (adv & ADVERTISED_100baseT_Full)
1670 auto_nego |= ADVERTISE_100FULL;
1672 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1674 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1675 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1677 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1678 if (tp->mii.supports_gmii) {
1679 if (adv & ADVERTISED_1000baseT_Half)
1680 giga_ctrl |= ADVERTISE_1000HALF;
1681 if (adv & ADVERTISED_1000baseT_Full)
1682 giga_ctrl |= ADVERTISE_1000FULL;
1683 } else if (adv & (ADVERTISED_1000baseT_Half |
1684 ADVERTISED_1000baseT_Full)) {
1685 netif_info(tp, link, dev,
1686 "PHY does not support 1000Mbps\n");
1690 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1692 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1693 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1697 if (speed == SPEED_10)
1699 else if (speed == SPEED_100)
1700 bmcr = BMCR_SPEED100;
1704 if (duplex == DUPLEX_FULL)
1705 bmcr |= BMCR_FULLDPLX;
1708 rtl_writephy(tp, MII_BMCR, bmcr);
1710 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1711 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1712 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1713 rtl_writephy(tp, 0x17, 0x2138);
1714 rtl_writephy(tp, 0x0e, 0x0260);
1716 rtl_writephy(tp, 0x17, 0x2108);
1717 rtl_writephy(tp, 0x0e, 0x0000);
1726 static int rtl8169_set_speed(struct net_device *dev,
1727 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1729 struct rtl8169_private *tp = netdev_priv(dev);
1732 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1736 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1737 (advertising & ADVERTISED_1000baseT_Full)) {
1738 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1744 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1746 struct rtl8169_private *tp = netdev_priv(dev);
1749 del_timer_sync(&tp->timer);
1752 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1753 cmd->duplex, cmd->advertising);
1754 rtl_unlock_work(tp);
1759 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1760 netdev_features_t features)
1762 struct rtl8169_private *tp = netdev_priv(dev);
1764 if (dev->mtu > TD_MSS_MAX)
1765 features &= ~NETIF_F_ALL_TSO;
1767 if (dev->mtu > JUMBO_1K &&
1768 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1769 features &= ~NETIF_F_IP_CSUM;
1774 static void __rtl8169_set_features(struct net_device *dev,
1775 netdev_features_t features)
1777 struct rtl8169_private *tp = netdev_priv(dev);
1778 netdev_features_t changed = features ^ dev->features;
1779 void __iomem *ioaddr = tp->mmio_addr;
1781 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1784 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1785 if (features & NETIF_F_RXCSUM)
1786 tp->cp_cmd |= RxChkSum;
1788 tp->cp_cmd &= ~RxChkSum;
1790 if (dev->features & NETIF_F_HW_VLAN_RX)
1791 tp->cp_cmd |= RxVlan;
1793 tp->cp_cmd &= ~RxVlan;
1795 RTL_W16(CPlusCmd, tp->cp_cmd);
1798 if (changed & NETIF_F_RXALL) {
1799 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1800 if (features & NETIF_F_RXALL)
1801 tmp |= (AcceptErr | AcceptRunt);
1802 RTL_W32(RxConfig, tmp);
1806 static int rtl8169_set_features(struct net_device *dev,
1807 netdev_features_t features)
1809 struct rtl8169_private *tp = netdev_priv(dev);
1812 __rtl8169_set_features(dev, features);
1813 rtl_unlock_work(tp);
1819 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1821 return (vlan_tx_tag_present(skb)) ?
1822 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1825 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1827 u32 opts2 = le32_to_cpu(desc->opts2);
1829 if (opts2 & RxVlanTag)
1830 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1833 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1835 struct rtl8169_private *tp = netdev_priv(dev);
1836 void __iomem *ioaddr = tp->mmio_addr;
1840 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1841 cmd->port = PORT_FIBRE;
1842 cmd->transceiver = XCVR_INTERNAL;
1844 status = RTL_R32(TBICSR);
1845 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1846 cmd->autoneg = !!(status & TBINwEnable);
1848 ethtool_cmd_speed_set(cmd, SPEED_1000);
1849 cmd->duplex = DUPLEX_FULL; /* Always set */
1854 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1856 struct rtl8169_private *tp = netdev_priv(dev);
1858 return mii_ethtool_gset(&tp->mii, cmd);
1861 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1863 struct rtl8169_private *tp = netdev_priv(dev);
1867 rc = tp->get_settings(dev, cmd);
1868 rtl_unlock_work(tp);
1873 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1876 struct rtl8169_private *tp = netdev_priv(dev);
1878 if (regs->len > R8169_REGS_SIZE)
1879 regs->len = R8169_REGS_SIZE;
1882 memcpy_fromio(p, tp->mmio_addr, regs->len);
1883 rtl_unlock_work(tp);
1886 static u32 rtl8169_get_msglevel(struct net_device *dev)
1888 struct rtl8169_private *tp = netdev_priv(dev);
1890 return tp->msg_enable;
1893 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1895 struct rtl8169_private *tp = netdev_priv(dev);
1897 tp->msg_enable = value;
1900 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1907 "tx_single_collisions",
1908 "tx_multi_collisions",
1916 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1920 return ARRAY_SIZE(rtl8169_gstrings);
1926 DECLARE_RTL_COND(rtl_counters_cond)
1928 void __iomem *ioaddr = tp->mmio_addr;
1930 return RTL_R32(CounterAddrLow) & CounterDump;
1933 static void rtl8169_update_counters(struct net_device *dev)
1935 struct rtl8169_private *tp = netdev_priv(dev);
1936 void __iomem *ioaddr = tp->mmio_addr;
1937 struct device *d = &tp->pci_dev->dev;
1938 struct rtl8169_counters *counters;
1943 * Some chips are unable to dump tally counters when the receiver
1946 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1949 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1953 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1954 cmd = (u64)paddr & DMA_BIT_MASK(32);
1955 RTL_W32(CounterAddrLow, cmd);
1956 RTL_W32(CounterAddrLow, cmd | CounterDump);
1958 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1959 memcpy(&tp->counters, counters, sizeof(*counters));
1961 RTL_W32(CounterAddrLow, 0);
1962 RTL_W32(CounterAddrHigh, 0);
1964 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1967 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1968 struct ethtool_stats *stats, u64 *data)
1970 struct rtl8169_private *tp = netdev_priv(dev);
1974 rtl8169_update_counters(dev);
1976 data[0] = le64_to_cpu(tp->counters.tx_packets);
1977 data[1] = le64_to_cpu(tp->counters.rx_packets);
1978 data[2] = le64_to_cpu(tp->counters.tx_errors);
1979 data[3] = le32_to_cpu(tp->counters.rx_errors);
1980 data[4] = le16_to_cpu(tp->counters.rx_missed);
1981 data[5] = le16_to_cpu(tp->counters.align_errors);
1982 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1983 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1984 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1985 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1986 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1987 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1988 data[12] = le16_to_cpu(tp->counters.tx_underun);
1991 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1995 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2000 static const struct ethtool_ops rtl8169_ethtool_ops = {
2001 .get_drvinfo = rtl8169_get_drvinfo,
2002 .get_regs_len = rtl8169_get_regs_len,
2003 .get_link = ethtool_op_get_link,
2004 .get_settings = rtl8169_get_settings,
2005 .set_settings = rtl8169_set_settings,
2006 .get_msglevel = rtl8169_get_msglevel,
2007 .set_msglevel = rtl8169_set_msglevel,
2008 .get_regs = rtl8169_get_regs,
2009 .get_wol = rtl8169_get_wol,
2010 .set_wol = rtl8169_set_wol,
2011 .get_strings = rtl8169_get_strings,
2012 .get_sset_count = rtl8169_get_sset_count,
2013 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2014 .get_ts_info = ethtool_op_get_ts_info,
2017 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2018 struct net_device *dev, u8 default_version)
2020 void __iomem *ioaddr = tp->mmio_addr;
2022 * The driver currently handles the 8168Bf and the 8168Be identically
2023 * but they can be identified more specifically through the test below
2026 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2028 * Same thing for the 8101Eb and the 8101Ec:
2030 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2032 static const struct rtl_mac_info {
2038 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2039 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2042 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2043 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2044 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2047 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2048 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2049 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2050 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2053 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2054 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2055 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2057 /* 8168DP family. */
2058 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2059 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2060 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2063 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
2064 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2065 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2066 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2067 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2068 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2069 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2070 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
2071 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2074 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2075 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2076 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2077 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2080 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2081 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2082 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2083 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
2084 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2085 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2086 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2087 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2088 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2089 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2090 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2091 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2092 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2093 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2094 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2095 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2096 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2097 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2098 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2099 /* FIXME: where did these entries come from ? -- FR */
2100 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2101 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2104 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2105 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2106 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2107 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2108 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2109 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2112 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2114 const struct rtl_mac_info *p = mac_info;
2117 reg = RTL_R32(TxConfig);
2118 while ((reg & p->mask) != p->val)
2120 tp->mac_version = p->mac_version;
2122 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2123 netif_notice(tp, probe, dev,
2124 "unknown MAC, using family default\n");
2125 tp->mac_version = default_version;
2129 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2131 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2139 static void rtl_writephy_batch(struct rtl8169_private *tp,
2140 const struct phy_reg *regs, int len)
2143 rtl_writephy(tp, regs->reg, regs->val);
2148 #define PHY_READ 0x00000000
2149 #define PHY_DATA_OR 0x10000000
2150 #define PHY_DATA_AND 0x20000000
2151 #define PHY_BJMPN 0x30000000
2152 #define PHY_MDIO_CHG 0x40000000
2153 #define PHY_CLEAR_READCOUNT 0x70000000
2154 #define PHY_WRITE 0x80000000
2155 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2156 #define PHY_COMP_EQ_SKIPN 0xa0000000
2157 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2158 #define PHY_WRITE_PREVIOUS 0xc0000000
2159 #define PHY_SKIPN 0xd0000000
2160 #define PHY_DELAY_MS 0xe0000000
2164 char version[RTL_VER_SIZE];
2170 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2172 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2174 const struct firmware *fw = rtl_fw->fw;
2175 struct fw_info *fw_info = (struct fw_info *)fw->data;
2176 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2177 char *version = rtl_fw->version;
2180 if (fw->size < FW_OPCODE_SIZE)
2183 if (!fw_info->magic) {
2184 size_t i, size, start;
2187 if (fw->size < sizeof(*fw_info))
2190 for (i = 0; i < fw->size; i++)
2191 checksum += fw->data[i];
2195 start = le32_to_cpu(fw_info->fw_start);
2196 if (start > fw->size)
2199 size = le32_to_cpu(fw_info->fw_len);
2200 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2203 memcpy(version, fw_info->version, RTL_VER_SIZE);
2205 pa->code = (__le32 *)(fw->data + start);
2208 if (fw->size % FW_OPCODE_SIZE)
2211 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2213 pa->code = (__le32 *)fw->data;
2214 pa->size = fw->size / FW_OPCODE_SIZE;
2216 version[RTL_VER_SIZE - 1] = 0;
2223 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2224 struct rtl_fw_phy_action *pa)
2229 for (index = 0; index < pa->size; index++) {
2230 u32 action = le32_to_cpu(pa->code[index]);
2231 u32 regno = (action & 0x0fff0000) >> 16;
2233 switch(action & 0xf0000000) {
2238 case PHY_CLEAR_READCOUNT:
2240 case PHY_WRITE_PREVIOUS:
2245 if (regno > index) {
2246 netif_err(tp, ifup, tp->dev,
2247 "Out of range of firmware\n");
2251 case PHY_READCOUNT_EQ_SKIP:
2252 if (index + 2 >= pa->size) {
2253 netif_err(tp, ifup, tp->dev,
2254 "Out of range of firmware\n");
2258 case PHY_COMP_EQ_SKIPN:
2259 case PHY_COMP_NEQ_SKIPN:
2261 if (index + 1 + regno >= pa->size) {
2262 netif_err(tp, ifup, tp->dev,
2263 "Out of range of firmware\n");
2269 netif_err(tp, ifup, tp->dev,
2270 "Invalid action 0x%08x\n", action);
2279 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2281 struct net_device *dev = tp->dev;
2284 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2285 netif_err(tp, ifup, dev, "invalid firwmare\n");
2289 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2295 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2297 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2298 struct mdio_ops org, *ops = &tp->mdio_ops;
2302 predata = count = 0;
2303 org.write = ops->write;
2304 org.read = ops->read;
2306 for (index = 0; index < pa->size; ) {
2307 u32 action = le32_to_cpu(pa->code[index]);
2308 u32 data = action & 0x0000ffff;
2309 u32 regno = (action & 0x0fff0000) >> 16;
2314 switch(action & 0xf0000000) {
2316 predata = rtl_readphy(tp, regno);
2333 ops->write = org.write;
2334 ops->read = org.read;
2335 } else if (data == 1) {
2336 ops->write = mac_mcu_write;
2337 ops->read = mac_mcu_read;
2342 case PHY_CLEAR_READCOUNT:
2347 rtl_writephy(tp, regno, data);
2350 case PHY_READCOUNT_EQ_SKIP:
2351 index += (count == data) ? 2 : 1;
2353 case PHY_COMP_EQ_SKIPN:
2354 if (predata == data)
2358 case PHY_COMP_NEQ_SKIPN:
2359 if (predata != data)
2363 case PHY_WRITE_PREVIOUS:
2364 rtl_writephy(tp, regno, predata);
2380 ops->write = org.write;
2381 ops->read = org.read;
2384 static void rtl_release_firmware(struct rtl8169_private *tp)
2386 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2387 release_firmware(tp->rtl_fw->fw);
2390 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2393 static void rtl_apply_firmware(struct rtl8169_private *tp)
2395 struct rtl_fw *rtl_fw = tp->rtl_fw;
2397 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2398 if (!IS_ERR_OR_NULL(rtl_fw))
2399 rtl_phy_write_fw(tp, rtl_fw);
2402 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2404 if (rtl_readphy(tp, reg) != val)
2405 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2407 rtl_apply_firmware(tp);
2410 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2412 static const struct phy_reg phy_reg_init[] = {
2474 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2477 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2479 static const struct phy_reg phy_reg_init[] = {
2485 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2488 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2490 struct pci_dev *pdev = tp->pci_dev;
2492 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2493 (pdev->subsystem_device != 0xe000))
2496 rtl_writephy(tp, 0x1f, 0x0001);
2497 rtl_writephy(tp, 0x10, 0xf01b);
2498 rtl_writephy(tp, 0x1f, 0x0000);
2501 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2503 static const struct phy_reg phy_reg_init[] = {
2543 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2545 rtl8169scd_hw_phy_config_quirk(tp);
2548 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2550 static const struct phy_reg phy_reg_init[] = {
2598 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2601 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2603 static const struct phy_reg phy_reg_init[] = {
2608 rtl_writephy(tp, 0x1f, 0x0001);
2609 rtl_patchphy(tp, 0x16, 1 << 0);
2611 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2614 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2616 static const struct phy_reg phy_reg_init[] = {
2622 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2625 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2627 static const struct phy_reg phy_reg_init[] = {
2635 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2638 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2640 static const struct phy_reg phy_reg_init[] = {
2646 rtl_writephy(tp, 0x1f, 0x0000);
2647 rtl_patchphy(tp, 0x14, 1 << 5);
2648 rtl_patchphy(tp, 0x0d, 1 << 5);
2650 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2653 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2655 static const struct phy_reg phy_reg_init[] = {
2675 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2677 rtl_patchphy(tp, 0x14, 1 << 5);
2678 rtl_patchphy(tp, 0x0d, 1 << 5);
2679 rtl_writephy(tp, 0x1f, 0x0000);
2682 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2684 static const struct phy_reg phy_reg_init[] = {
2702 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2704 rtl_patchphy(tp, 0x16, 1 << 0);
2705 rtl_patchphy(tp, 0x14, 1 << 5);
2706 rtl_patchphy(tp, 0x0d, 1 << 5);
2707 rtl_writephy(tp, 0x1f, 0x0000);
2710 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2712 static const struct phy_reg phy_reg_init[] = {
2724 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2726 rtl_patchphy(tp, 0x16, 1 << 0);
2727 rtl_patchphy(tp, 0x14, 1 << 5);
2728 rtl_patchphy(tp, 0x0d, 1 << 5);
2729 rtl_writephy(tp, 0x1f, 0x0000);
2732 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2734 rtl8168c_3_hw_phy_config(tp);
2737 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2739 static const struct phy_reg phy_reg_init_0[] = {
2740 /* Channel Estimation */
2761 * Enhance line driver power
2770 * Can not link to 1Gbps with bad cable
2771 * Decrease SNR threshold form 21.07dB to 19.04dB
2780 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2784 * Fine Tune Switching regulator parameter
2786 rtl_writephy(tp, 0x1f, 0x0002);
2787 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2788 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2790 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2791 static const struct phy_reg phy_reg_init[] = {
2801 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2803 val = rtl_readphy(tp, 0x0d);
2805 if ((val & 0x00ff) != 0x006c) {
2806 static const u32 set[] = {
2807 0x0065, 0x0066, 0x0067, 0x0068,
2808 0x0069, 0x006a, 0x006b, 0x006c
2812 rtl_writephy(tp, 0x1f, 0x0002);
2815 for (i = 0; i < ARRAY_SIZE(set); i++)
2816 rtl_writephy(tp, 0x0d, val | set[i]);
2819 static const struct phy_reg phy_reg_init[] = {
2827 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2830 /* RSET couple improve */
2831 rtl_writephy(tp, 0x1f, 0x0002);
2832 rtl_patchphy(tp, 0x0d, 0x0300);
2833 rtl_patchphy(tp, 0x0f, 0x0010);
2835 /* Fine tune PLL performance */
2836 rtl_writephy(tp, 0x1f, 0x0002);
2837 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2838 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2840 rtl_writephy(tp, 0x1f, 0x0005);
2841 rtl_writephy(tp, 0x05, 0x001b);
2843 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2845 rtl_writephy(tp, 0x1f, 0x0000);
2848 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2850 static const struct phy_reg phy_reg_init_0[] = {
2851 /* Channel Estimation */
2872 * Enhance line driver power
2881 * Can not link to 1Gbps with bad cable
2882 * Decrease SNR threshold form 21.07dB to 19.04dB
2891 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2893 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2894 static const struct phy_reg phy_reg_init[] = {
2905 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2907 val = rtl_readphy(tp, 0x0d);
2908 if ((val & 0x00ff) != 0x006c) {
2909 static const u32 set[] = {
2910 0x0065, 0x0066, 0x0067, 0x0068,
2911 0x0069, 0x006a, 0x006b, 0x006c
2915 rtl_writephy(tp, 0x1f, 0x0002);
2918 for (i = 0; i < ARRAY_SIZE(set); i++)
2919 rtl_writephy(tp, 0x0d, val | set[i]);
2922 static const struct phy_reg phy_reg_init[] = {
2930 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2933 /* Fine tune PLL performance */
2934 rtl_writephy(tp, 0x1f, 0x0002);
2935 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2936 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2938 /* Switching regulator Slew rate */
2939 rtl_writephy(tp, 0x1f, 0x0002);
2940 rtl_patchphy(tp, 0x0f, 0x0017);
2942 rtl_writephy(tp, 0x1f, 0x0005);
2943 rtl_writephy(tp, 0x05, 0x001b);
2945 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2947 rtl_writephy(tp, 0x1f, 0x0000);
2950 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2952 static const struct phy_reg phy_reg_init[] = {
3008 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3011 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3013 static const struct phy_reg phy_reg_init[] = {
3023 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3024 rtl_patchphy(tp, 0x0d, 1 << 5);
3027 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3029 static const struct phy_reg phy_reg_init[] = {
3030 /* Enable Delay cap */
3036 /* Channel estimation fine tune */
3045 /* Update PFM & 10M TX idle timer */
3057 rtl_apply_firmware(tp);
3059 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3061 /* DCO enable for 10M IDLE Power */
3062 rtl_writephy(tp, 0x1f, 0x0007);
3063 rtl_writephy(tp, 0x1e, 0x0023);
3064 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3065 rtl_writephy(tp, 0x1f, 0x0000);
3067 /* For impedance matching */
3068 rtl_writephy(tp, 0x1f, 0x0002);
3069 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
3070 rtl_writephy(tp, 0x1f, 0x0000);
3072 /* PHY auto speed down */
3073 rtl_writephy(tp, 0x1f, 0x0007);
3074 rtl_writephy(tp, 0x1e, 0x002d);
3075 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3076 rtl_writephy(tp, 0x1f, 0x0000);
3077 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3079 rtl_writephy(tp, 0x1f, 0x0005);
3080 rtl_writephy(tp, 0x05, 0x8b86);
3081 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3084 rtl_writephy(tp, 0x1f, 0x0005);
3085 rtl_writephy(tp, 0x05, 0x8b85);
3086 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3087 rtl_writephy(tp, 0x1f, 0x0007);
3088 rtl_writephy(tp, 0x1e, 0x0020);
3089 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3090 rtl_writephy(tp, 0x1f, 0x0006);
3091 rtl_writephy(tp, 0x00, 0x5a00);
3092 rtl_writephy(tp, 0x1f, 0x0000);
3093 rtl_writephy(tp, 0x0d, 0x0007);
3094 rtl_writephy(tp, 0x0e, 0x003c);
3095 rtl_writephy(tp, 0x0d, 0x4007);
3096 rtl_writephy(tp, 0x0e, 0x0000);
3097 rtl_writephy(tp, 0x0d, 0x0000);
3100 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3103 addr[0] | (addr[1] << 8),
3104 addr[2] | (addr[3] << 8),
3105 addr[4] | (addr[5] << 8)
3107 const struct exgmac_reg e[] = {
3108 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3109 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3110 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3111 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3114 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3117 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3119 static const struct phy_reg phy_reg_init[] = {
3120 /* Enable Delay cap */
3129 /* Channel estimation fine tune */
3146 rtl_apply_firmware(tp);
3148 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3150 /* For 4-corner performance improve */
3151 rtl_writephy(tp, 0x1f, 0x0005);
3152 rtl_writephy(tp, 0x05, 0x8b80);
3153 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3154 rtl_writephy(tp, 0x1f, 0x0000);
3156 /* PHY auto speed down */
3157 rtl_writephy(tp, 0x1f, 0x0004);
3158 rtl_writephy(tp, 0x1f, 0x0007);
3159 rtl_writephy(tp, 0x1e, 0x002d);
3160 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3161 rtl_writephy(tp, 0x1f, 0x0002);
3162 rtl_writephy(tp, 0x1f, 0x0000);
3163 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3165 /* improve 10M EEE waveform */
3166 rtl_writephy(tp, 0x1f, 0x0005);
3167 rtl_writephy(tp, 0x05, 0x8b86);
3168 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3169 rtl_writephy(tp, 0x1f, 0x0000);
3171 /* Improve 2-pair detection performance */
3172 rtl_writephy(tp, 0x1f, 0x0005);
3173 rtl_writephy(tp, 0x05, 0x8b85);
3174 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3175 rtl_writephy(tp, 0x1f, 0x0000);
3178 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
3179 rtl_writephy(tp, 0x1f, 0x0005);
3180 rtl_writephy(tp, 0x05, 0x8b85);
3181 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3182 rtl_writephy(tp, 0x1f, 0x0004);
3183 rtl_writephy(tp, 0x1f, 0x0007);
3184 rtl_writephy(tp, 0x1e, 0x0020);
3185 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3186 rtl_writephy(tp, 0x1f, 0x0002);
3187 rtl_writephy(tp, 0x1f, 0x0000);
3188 rtl_writephy(tp, 0x0d, 0x0007);
3189 rtl_writephy(tp, 0x0e, 0x003c);
3190 rtl_writephy(tp, 0x0d, 0x4007);
3191 rtl_writephy(tp, 0x0e, 0x0000);
3192 rtl_writephy(tp, 0x0d, 0x0000);
3195 rtl_writephy(tp, 0x1f, 0x0003);
3196 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3197 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3198 rtl_writephy(tp, 0x1f, 0x0000);
3200 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3201 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3204 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3206 /* For 4-corner performance improve */
3207 rtl_writephy(tp, 0x1f, 0x0005);
3208 rtl_writephy(tp, 0x05, 0x8b80);
3209 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3210 rtl_writephy(tp, 0x1f, 0x0000);
3212 /* PHY auto speed down */
3213 rtl_writephy(tp, 0x1f, 0x0007);
3214 rtl_writephy(tp, 0x1e, 0x002d);
3215 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3216 rtl_writephy(tp, 0x1f, 0x0000);
3217 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3219 /* Improve 10M EEE waveform */
3220 rtl_writephy(tp, 0x1f, 0x0005);
3221 rtl_writephy(tp, 0x05, 0x8b86);
3222 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3223 rtl_writephy(tp, 0x1f, 0x0000);
3226 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3228 static const struct phy_reg phy_reg_init[] = {
3229 /* Channel estimation fine tune */
3234 /* Modify green table for giga & fnet */
3251 /* Modify green table for 10M */
3257 /* Disable hiimpedance detection (RTCT) */
3263 rtl_apply_firmware(tp);
3265 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3267 rtl8168f_hw_phy_config(tp);
3269 /* Improve 2-pair detection performance */
3270 rtl_writephy(tp, 0x1f, 0x0005);
3271 rtl_writephy(tp, 0x05, 0x8b85);
3272 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3273 rtl_writephy(tp, 0x1f, 0x0000);
3276 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3278 rtl_apply_firmware(tp);
3280 rtl8168f_hw_phy_config(tp);
3283 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3285 static const struct phy_reg phy_reg_init[] = {
3286 /* Channel estimation fine tune */
3291 /* Modify green table for giga & fnet */
3308 /* Modify green table for 10M */
3314 /* Disable hiimpedance detection (RTCT) */
3321 rtl_apply_firmware(tp);
3323 rtl8168f_hw_phy_config(tp);
3325 /* Improve 2-pair detection performance */
3326 rtl_writephy(tp, 0x1f, 0x0005);
3327 rtl_writephy(tp, 0x05, 0x8b85);
3328 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3329 rtl_writephy(tp, 0x1f, 0x0000);
3331 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3333 /* Modify green table for giga */
3334 rtl_writephy(tp, 0x1f, 0x0005);
3335 rtl_writephy(tp, 0x05, 0x8b54);
3336 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3337 rtl_writephy(tp, 0x05, 0x8b5d);
3338 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3339 rtl_writephy(tp, 0x05, 0x8a7c);
3340 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3341 rtl_writephy(tp, 0x05, 0x8a7f);
3342 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3343 rtl_writephy(tp, 0x05, 0x8a82);
3344 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3345 rtl_writephy(tp, 0x05, 0x8a85);
3346 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3347 rtl_writephy(tp, 0x05, 0x8a88);
3348 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3349 rtl_writephy(tp, 0x1f, 0x0000);
3351 /* uc same-seed solution */
3352 rtl_writephy(tp, 0x1f, 0x0005);
3353 rtl_writephy(tp, 0x05, 0x8b85);
3354 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3355 rtl_writephy(tp, 0x1f, 0x0000);
3358 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3359 rtl_writephy(tp, 0x1f, 0x0005);
3360 rtl_writephy(tp, 0x05, 0x8b85);
3361 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3362 rtl_writephy(tp, 0x1f, 0x0004);
3363 rtl_writephy(tp, 0x1f, 0x0007);
3364 rtl_writephy(tp, 0x1e, 0x0020);
3365 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3366 rtl_writephy(tp, 0x1f, 0x0000);
3367 rtl_writephy(tp, 0x0d, 0x0007);
3368 rtl_writephy(tp, 0x0e, 0x003c);
3369 rtl_writephy(tp, 0x0d, 0x4007);
3370 rtl_writephy(tp, 0x0e, 0x0000);
3371 rtl_writephy(tp, 0x0d, 0x0000);
3374 rtl_writephy(tp, 0x1f, 0x0003);
3375 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3376 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3377 rtl_writephy(tp, 0x1f, 0x0000);
3380 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3382 rtl_apply_firmware(tp);
3384 rtl_writephy(tp, 0x1f, 0x0a46);
3385 if (rtl_readphy(tp, 0x10) & 0x0100) {
3386 rtl_writephy(tp, 0x1f, 0x0bcc);
3387 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3389 rtl_writephy(tp, 0x1f, 0x0bcc);
3390 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3393 rtl_writephy(tp, 0x1f, 0x0a46);
3394 if (rtl_readphy(tp, 0x13) & 0x0100) {
3395 rtl_writephy(tp, 0x1f, 0x0c41);
3396 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3398 rtl_writephy(tp, 0x1f, 0x0c41);
3399 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
3402 /* Enable PHY auto speed down */
3403 rtl_writephy(tp, 0x1f, 0x0a44);
3404 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
3406 rtl_writephy(tp, 0x1f, 0x0bcc);
3407 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3408 rtl_writephy(tp, 0x1f, 0x0a44);
3409 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3410 rtl_writephy(tp, 0x1f, 0x0a43);
3411 rtl_writephy(tp, 0x13, 0x8084);
3412 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3413 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3415 /* EEE auto-fallback function */
3416 rtl_writephy(tp, 0x1f, 0x0a4b);
3417 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
3419 /* Enable UC LPF tune function */
3420 rtl_writephy(tp, 0x1f, 0x0a43);
3421 rtl_writephy(tp, 0x13, 0x8012);
3422 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3424 rtl_writephy(tp, 0x1f, 0x0c42);
3425 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3427 /* Improve SWR Efficiency */
3428 rtl_writephy(tp, 0x1f, 0x0bcd);
3429 rtl_writephy(tp, 0x14, 0x5065);
3430 rtl_writephy(tp, 0x14, 0xd065);
3431 rtl_writephy(tp, 0x1f, 0x0bc8);
3432 rtl_writephy(tp, 0x11, 0x5655);
3433 rtl_writephy(tp, 0x1f, 0x0bcd);
3434 rtl_writephy(tp, 0x14, 0x1065);
3435 rtl_writephy(tp, 0x14, 0x9065);
3436 rtl_writephy(tp, 0x14, 0x1065);
3438 rtl_writephy(tp, 0x1f, 0x0000);
3441 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3443 static const struct phy_reg phy_reg_init[] = {
3450 rtl_writephy(tp, 0x1f, 0x0000);
3451 rtl_patchphy(tp, 0x11, 1 << 12);
3452 rtl_patchphy(tp, 0x19, 1 << 13);
3453 rtl_patchphy(tp, 0x10, 1 << 15);
3455 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3458 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3460 static const struct phy_reg phy_reg_init[] = {
3474 /* Disable ALDPS before ram code */
3475 rtl_writephy(tp, 0x1f, 0x0000);
3476 rtl_writephy(tp, 0x18, 0x0310);
3479 rtl_apply_firmware(tp);
3481 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3484 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3486 /* Disable ALDPS before setting firmware */
3487 rtl_writephy(tp, 0x1f, 0x0000);
3488 rtl_writephy(tp, 0x18, 0x0310);
3491 rtl_apply_firmware(tp);
3494 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3495 rtl_writephy(tp, 0x1f, 0x0004);
3496 rtl_writephy(tp, 0x10, 0x401f);
3497 rtl_writephy(tp, 0x19, 0x7030);
3498 rtl_writephy(tp, 0x1f, 0x0000);
3501 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3503 static const struct phy_reg phy_reg_init[] = {
3510 /* Disable ALDPS before ram code */
3511 rtl_writephy(tp, 0x1f, 0x0000);
3512 rtl_writephy(tp, 0x18, 0x0310);
3515 rtl_apply_firmware(tp);
3517 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3518 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3520 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3523 static void rtl_hw_phy_config(struct net_device *dev)
3525 struct rtl8169_private *tp = netdev_priv(dev);
3527 rtl8169_print_mac_version(tp);
3529 switch (tp->mac_version) {
3530 case RTL_GIGA_MAC_VER_01:
3532 case RTL_GIGA_MAC_VER_02:
3533 case RTL_GIGA_MAC_VER_03:
3534 rtl8169s_hw_phy_config(tp);
3536 case RTL_GIGA_MAC_VER_04:
3537 rtl8169sb_hw_phy_config(tp);
3539 case RTL_GIGA_MAC_VER_05:
3540 rtl8169scd_hw_phy_config(tp);
3542 case RTL_GIGA_MAC_VER_06:
3543 rtl8169sce_hw_phy_config(tp);
3545 case RTL_GIGA_MAC_VER_07:
3546 case RTL_GIGA_MAC_VER_08:
3547 case RTL_GIGA_MAC_VER_09:
3548 rtl8102e_hw_phy_config(tp);
3550 case RTL_GIGA_MAC_VER_11:
3551 rtl8168bb_hw_phy_config(tp);
3553 case RTL_GIGA_MAC_VER_12:
3554 rtl8168bef_hw_phy_config(tp);
3556 case RTL_GIGA_MAC_VER_17:
3557 rtl8168bef_hw_phy_config(tp);
3559 case RTL_GIGA_MAC_VER_18:
3560 rtl8168cp_1_hw_phy_config(tp);
3562 case RTL_GIGA_MAC_VER_19:
3563 rtl8168c_1_hw_phy_config(tp);
3565 case RTL_GIGA_MAC_VER_20:
3566 rtl8168c_2_hw_phy_config(tp);
3568 case RTL_GIGA_MAC_VER_21:
3569 rtl8168c_3_hw_phy_config(tp);
3571 case RTL_GIGA_MAC_VER_22:
3572 rtl8168c_4_hw_phy_config(tp);
3574 case RTL_GIGA_MAC_VER_23:
3575 case RTL_GIGA_MAC_VER_24:
3576 rtl8168cp_2_hw_phy_config(tp);
3578 case RTL_GIGA_MAC_VER_25:
3579 rtl8168d_1_hw_phy_config(tp);
3581 case RTL_GIGA_MAC_VER_26:
3582 rtl8168d_2_hw_phy_config(tp);
3584 case RTL_GIGA_MAC_VER_27:
3585 rtl8168d_3_hw_phy_config(tp);
3587 case RTL_GIGA_MAC_VER_28:
3588 rtl8168d_4_hw_phy_config(tp);
3590 case RTL_GIGA_MAC_VER_29:
3591 case RTL_GIGA_MAC_VER_30:
3592 rtl8105e_hw_phy_config(tp);
3594 case RTL_GIGA_MAC_VER_31:
3597 case RTL_GIGA_MAC_VER_32:
3598 case RTL_GIGA_MAC_VER_33:
3599 rtl8168e_1_hw_phy_config(tp);
3601 case RTL_GIGA_MAC_VER_34:
3602 rtl8168e_2_hw_phy_config(tp);
3604 case RTL_GIGA_MAC_VER_35:
3605 rtl8168f_1_hw_phy_config(tp);
3607 case RTL_GIGA_MAC_VER_36:
3608 rtl8168f_2_hw_phy_config(tp);
3611 case RTL_GIGA_MAC_VER_37:
3612 rtl8402_hw_phy_config(tp);
3615 case RTL_GIGA_MAC_VER_38:
3616 rtl8411_hw_phy_config(tp);
3619 case RTL_GIGA_MAC_VER_39:
3620 rtl8106e_hw_phy_config(tp);
3623 case RTL_GIGA_MAC_VER_40:
3624 rtl8168g_1_hw_phy_config(tp);
3627 case RTL_GIGA_MAC_VER_41:
3633 static void rtl_phy_work(struct rtl8169_private *tp)
3635 struct timer_list *timer = &tp->timer;
3636 void __iomem *ioaddr = tp->mmio_addr;
3637 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3639 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
3641 if (tp->phy_reset_pending(tp)) {
3643 * A busy loop could burn quite a few cycles on nowadays CPU.
3644 * Let's delay the execution of the timer for a few ticks.
3650 if (tp->link_ok(ioaddr))
3653 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
3655 tp->phy_reset_enable(tp);
3658 mod_timer(timer, jiffies + timeout);
3661 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3663 if (!test_and_set_bit(flag, tp->wk.flags))
3664 schedule_work(&tp->wk.work);
3667 static void rtl8169_phy_timer(unsigned long __opaque)
3669 struct net_device *dev = (struct net_device *)__opaque;
3670 struct rtl8169_private *tp = netdev_priv(dev);
3672 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
3675 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3676 void __iomem *ioaddr)
3679 pci_release_regions(pdev);
3680 pci_clear_mwi(pdev);
3681 pci_disable_device(pdev);
3685 DECLARE_RTL_COND(rtl_phy_reset_cond)
3687 return tp->phy_reset_pending(tp);
3690 static void rtl8169_phy_reset(struct net_device *dev,
3691 struct rtl8169_private *tp)
3693 tp->phy_reset_enable(tp);
3694 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
3697 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3699 void __iomem *ioaddr = tp->mmio_addr;
3701 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3702 (RTL_R8(PHYstatus) & TBI_Enable);
3705 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3707 void __iomem *ioaddr = tp->mmio_addr;
3709 rtl_hw_phy_config(dev);
3711 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3712 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3716 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3718 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3719 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3721 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
3722 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3724 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
3725 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
3728 rtl8169_phy_reset(dev, tp);
3730 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3731 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3732 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3733 (tp->mii.supports_gmii ?
3734 ADVERTISED_1000baseT_Half |
3735 ADVERTISED_1000baseT_Full : 0));
3737 if (rtl_tbi_enabled(tp))
3738 netif_info(tp, link, dev, "TBI auto-negotiating\n");
3741 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3743 void __iomem *ioaddr = tp->mmio_addr;
3747 RTL_W8(Cfg9346, Cfg9346_Unlock);
3749 RTL_W32(MAC4, addr[4] | addr[5] << 8);
3752 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3755 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3756 rtl_rar_exgmac_set(tp, addr);
3758 RTL_W8(Cfg9346, Cfg9346_Lock);
3760 rtl_unlock_work(tp);
3763 static int rtl_set_mac_address(struct net_device *dev, void *p)
3765 struct rtl8169_private *tp = netdev_priv(dev);
3766 struct sockaddr *addr = p;
3768 if (!is_valid_ether_addr(addr->sa_data))
3769 return -EADDRNOTAVAIL;
3771 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3773 rtl_rar_set(tp, dev->dev_addr);
3778 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3780 struct rtl8169_private *tp = netdev_priv(dev);
3781 struct mii_ioctl_data *data = if_mii(ifr);
3783 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3786 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3787 struct mii_ioctl_data *data, int cmd)
3791 data->phy_id = 32; /* Internal PHY */
3795 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3799 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3805 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3810 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3812 if (tp->features & RTL_FEATURE_MSI) {
3813 pci_disable_msi(pdev);
3814 tp->features &= ~RTL_FEATURE_MSI;
3818 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
3820 struct mdio_ops *ops = &tp->mdio_ops;
3822 switch (tp->mac_version) {
3823 case RTL_GIGA_MAC_VER_27:
3824 ops->write = r8168dp_1_mdio_write;
3825 ops->read = r8168dp_1_mdio_read;
3827 case RTL_GIGA_MAC_VER_28:
3828 case RTL_GIGA_MAC_VER_31:
3829 ops->write = r8168dp_2_mdio_write;
3830 ops->read = r8168dp_2_mdio_read;
3832 case RTL_GIGA_MAC_VER_40:
3833 case RTL_GIGA_MAC_VER_41:
3834 ops->write = r8168g_mdio_write;
3835 ops->read = r8168g_mdio_read;
3838 ops->write = r8169_mdio_write;
3839 ops->read = r8169_mdio_read;
3844 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3846 void __iomem *ioaddr = tp->mmio_addr;
3848 switch (tp->mac_version) {
3849 case RTL_GIGA_MAC_VER_25:
3850 case RTL_GIGA_MAC_VER_26:
3851 case RTL_GIGA_MAC_VER_29:
3852 case RTL_GIGA_MAC_VER_30:
3853 case RTL_GIGA_MAC_VER_32:
3854 case RTL_GIGA_MAC_VER_33:
3855 case RTL_GIGA_MAC_VER_34:
3856 case RTL_GIGA_MAC_VER_37:
3857 case RTL_GIGA_MAC_VER_38:
3858 case RTL_GIGA_MAC_VER_39:
3859 case RTL_GIGA_MAC_VER_40:
3860 case RTL_GIGA_MAC_VER_41:
3861 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3862 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3869 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3871 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3874 rtl_writephy(tp, 0x1f, 0x0000);
3875 rtl_writephy(tp, MII_BMCR, 0x0000);
3877 rtl_wol_suspend_quirk(tp);
3882 static void r810x_phy_power_down(struct rtl8169_private *tp)
3884 rtl_writephy(tp, 0x1f, 0x0000);
3885 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3888 static void r810x_phy_power_up(struct rtl8169_private *tp)
3890 rtl_writephy(tp, 0x1f, 0x0000);
3891 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3894 static void r810x_pll_power_down(struct rtl8169_private *tp)
3896 void __iomem *ioaddr = tp->mmio_addr;
3898 if (rtl_wol_pll_power_down(tp))
3901 r810x_phy_power_down(tp);
3903 switch (tp->mac_version) {
3904 case RTL_GIGA_MAC_VER_07:
3905 case RTL_GIGA_MAC_VER_08:
3906 case RTL_GIGA_MAC_VER_09:
3907 case RTL_GIGA_MAC_VER_10:
3908 case RTL_GIGA_MAC_VER_13:
3909 case RTL_GIGA_MAC_VER_16:
3912 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3917 static void r810x_pll_power_up(struct rtl8169_private *tp)
3919 void __iomem *ioaddr = tp->mmio_addr;
3921 r810x_phy_power_up(tp);
3923 switch (tp->mac_version) {
3924 case RTL_GIGA_MAC_VER_07:
3925 case RTL_GIGA_MAC_VER_08:
3926 case RTL_GIGA_MAC_VER_09:
3927 case RTL_GIGA_MAC_VER_10:
3928 case RTL_GIGA_MAC_VER_13:
3929 case RTL_GIGA_MAC_VER_16:
3932 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3937 static void r8168_phy_power_up(struct rtl8169_private *tp)
3939 rtl_writephy(tp, 0x1f, 0x0000);
3940 switch (tp->mac_version) {
3941 case RTL_GIGA_MAC_VER_11:
3942 case RTL_GIGA_MAC_VER_12:
3943 case RTL_GIGA_MAC_VER_17:
3944 case RTL_GIGA_MAC_VER_18:
3945 case RTL_GIGA_MAC_VER_19:
3946 case RTL_GIGA_MAC_VER_20:
3947 case RTL_GIGA_MAC_VER_21:
3948 case RTL_GIGA_MAC_VER_22:
3949 case RTL_GIGA_MAC_VER_23:
3950 case RTL_GIGA_MAC_VER_24:
3951 case RTL_GIGA_MAC_VER_25:
3952 case RTL_GIGA_MAC_VER_26:
3953 case RTL_GIGA_MAC_VER_27:
3954 case RTL_GIGA_MAC_VER_28:
3955 case RTL_GIGA_MAC_VER_31:
3956 rtl_writephy(tp, 0x0e, 0x0000);
3961 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3964 static void r8168_phy_power_down(struct rtl8169_private *tp)
3966 rtl_writephy(tp, 0x1f, 0x0000);
3967 switch (tp->mac_version) {
3968 case RTL_GIGA_MAC_VER_32:
3969 case RTL_GIGA_MAC_VER_33:
3970 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3973 case RTL_GIGA_MAC_VER_11:
3974 case RTL_GIGA_MAC_VER_12:
3975 case RTL_GIGA_MAC_VER_17:
3976 case RTL_GIGA_MAC_VER_18:
3977 case RTL_GIGA_MAC_VER_19:
3978 case RTL_GIGA_MAC_VER_20:
3979 case RTL_GIGA_MAC_VER_21:
3980 case RTL_GIGA_MAC_VER_22:
3981 case RTL_GIGA_MAC_VER_23:
3982 case RTL_GIGA_MAC_VER_24:
3983 case RTL_GIGA_MAC_VER_25:
3984 case RTL_GIGA_MAC_VER_26:
3985 case RTL_GIGA_MAC_VER_27:
3986 case RTL_GIGA_MAC_VER_28:
3987 case RTL_GIGA_MAC_VER_31:
3988 rtl_writephy(tp, 0x0e, 0x0200);
3990 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3995 static void r8168_pll_power_down(struct rtl8169_private *tp)
3997 void __iomem *ioaddr = tp->mmio_addr;
3999 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4000 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4001 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
4002 r8168dp_check_dash(tp)) {
4006 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4007 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
4008 (RTL_R16(CPlusCmd) & ASF)) {
4012 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4013 tp->mac_version == RTL_GIGA_MAC_VER_33)
4014 rtl_ephy_write(tp, 0x19, 0xff64);
4016 if (rtl_wol_pll_power_down(tp))
4019 r8168_phy_power_down(tp);
4021 switch (tp->mac_version) {
4022 case RTL_GIGA_MAC_VER_25:
4023 case RTL_GIGA_MAC_VER_26:
4024 case RTL_GIGA_MAC_VER_27:
4025 case RTL_GIGA_MAC_VER_28:
4026 case RTL_GIGA_MAC_VER_31:
4027 case RTL_GIGA_MAC_VER_32:
4028 case RTL_GIGA_MAC_VER_33:
4029 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4034 static void r8168_pll_power_up(struct rtl8169_private *tp)
4036 void __iomem *ioaddr = tp->mmio_addr;
4038 switch (tp->mac_version) {
4039 case RTL_GIGA_MAC_VER_25:
4040 case RTL_GIGA_MAC_VER_26:
4041 case RTL_GIGA_MAC_VER_27:
4042 case RTL_GIGA_MAC_VER_28:
4043 case RTL_GIGA_MAC_VER_31:
4044 case RTL_GIGA_MAC_VER_32:
4045 case RTL_GIGA_MAC_VER_33:
4046 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4050 r8168_phy_power_up(tp);
4053 static void rtl_generic_op(struct rtl8169_private *tp,
4054 void (*op)(struct rtl8169_private *))
4060 static void rtl_pll_power_down(struct rtl8169_private *tp)
4062 rtl_generic_op(tp, tp->pll_power_ops.down);
4065 static void rtl_pll_power_up(struct rtl8169_private *tp)
4067 rtl_generic_op(tp, tp->pll_power_ops.up);
4070 static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
4072 struct pll_power_ops *ops = &tp->pll_power_ops;
4074 switch (tp->mac_version) {
4075 case RTL_GIGA_MAC_VER_07:
4076 case RTL_GIGA_MAC_VER_08:
4077 case RTL_GIGA_MAC_VER_09:
4078 case RTL_GIGA_MAC_VER_10:
4079 case RTL_GIGA_MAC_VER_16:
4080 case RTL_GIGA_MAC_VER_29:
4081 case RTL_GIGA_MAC_VER_30:
4082 case RTL_GIGA_MAC_VER_37:
4083 case RTL_GIGA_MAC_VER_39:
4084 ops->down = r810x_pll_power_down;
4085 ops->up = r810x_pll_power_up;
4088 case RTL_GIGA_MAC_VER_11:
4089 case RTL_GIGA_MAC_VER_12:
4090 case RTL_GIGA_MAC_VER_17:
4091 case RTL_GIGA_MAC_VER_18:
4092 case RTL_GIGA_MAC_VER_19:
4093 case RTL_GIGA_MAC_VER_20:
4094 case RTL_GIGA_MAC_VER_21:
4095 case RTL_GIGA_MAC_VER_22:
4096 case RTL_GIGA_MAC_VER_23:
4097 case RTL_GIGA_MAC_VER_24:
4098 case RTL_GIGA_MAC_VER_25:
4099 case RTL_GIGA_MAC_VER_26:
4100 case RTL_GIGA_MAC_VER_27:
4101 case RTL_GIGA_MAC_VER_28:
4102 case RTL_GIGA_MAC_VER_31:
4103 case RTL_GIGA_MAC_VER_32:
4104 case RTL_GIGA_MAC_VER_33:
4105 case RTL_GIGA_MAC_VER_34:
4106 case RTL_GIGA_MAC_VER_35:
4107 case RTL_GIGA_MAC_VER_36:
4108 case RTL_GIGA_MAC_VER_38:
4109 case RTL_GIGA_MAC_VER_40:
4110 case RTL_GIGA_MAC_VER_41:
4111 ops->down = r8168_pll_power_down;
4112 ops->up = r8168_pll_power_up;
4122 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4124 void __iomem *ioaddr = tp->mmio_addr;
4126 switch (tp->mac_version) {
4127 case RTL_GIGA_MAC_VER_01:
4128 case RTL_GIGA_MAC_VER_02:
4129 case RTL_GIGA_MAC_VER_03:
4130 case RTL_GIGA_MAC_VER_04:
4131 case RTL_GIGA_MAC_VER_05:
4132 case RTL_GIGA_MAC_VER_06:
4133 case RTL_GIGA_MAC_VER_10:
4134 case RTL_GIGA_MAC_VER_11:
4135 case RTL_GIGA_MAC_VER_12:
4136 case RTL_GIGA_MAC_VER_13:
4137 case RTL_GIGA_MAC_VER_14:
4138 case RTL_GIGA_MAC_VER_15:
4139 case RTL_GIGA_MAC_VER_16:
4140 case RTL_GIGA_MAC_VER_17:
4141 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4143 case RTL_GIGA_MAC_VER_18:
4144 case RTL_GIGA_MAC_VER_19:
4145 case RTL_GIGA_MAC_VER_20:
4146 case RTL_GIGA_MAC_VER_21:
4147 case RTL_GIGA_MAC_VER_22:
4148 case RTL_GIGA_MAC_VER_23:
4149 case RTL_GIGA_MAC_VER_24:
4150 case RTL_GIGA_MAC_VER_34:
4151 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4154 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4159 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4161 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4164 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4166 void __iomem *ioaddr = tp->mmio_addr;
4168 RTL_W8(Cfg9346, Cfg9346_Unlock);
4169 rtl_generic_op(tp, tp->jumbo_ops.enable);
4170 RTL_W8(Cfg9346, Cfg9346_Lock);
4173 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4175 void __iomem *ioaddr = tp->mmio_addr;
4177 RTL_W8(Cfg9346, Cfg9346_Unlock);
4178 rtl_generic_op(tp, tp->jumbo_ops.disable);
4179 RTL_W8(Cfg9346, Cfg9346_Lock);
4182 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4184 void __iomem *ioaddr = tp->mmio_addr;
4186 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4187 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4188 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4191 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4193 void __iomem *ioaddr = tp->mmio_addr;
4195 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4196 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4197 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4200 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4202 void __iomem *ioaddr = tp->mmio_addr;
4204 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4207 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4209 void __iomem *ioaddr = tp->mmio_addr;
4211 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4214 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4216 void __iomem *ioaddr = tp->mmio_addr;
4218 RTL_W8(MaxTxPacketSize, 0x3f);
4219 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4220 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
4221 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4224 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4226 void __iomem *ioaddr = tp->mmio_addr;
4228 RTL_W8(MaxTxPacketSize, 0x0c);
4229 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4230 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
4231 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4234 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4236 rtl_tx_performance_tweak(tp->pci_dev,
4237 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4240 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4242 rtl_tx_performance_tweak(tp->pci_dev,
4243 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4246 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4248 void __iomem *ioaddr = tp->mmio_addr;
4250 r8168b_0_hw_jumbo_enable(tp);
4252 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4255 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4257 void __iomem *ioaddr = tp->mmio_addr;
4259 r8168b_0_hw_jumbo_disable(tp);
4261 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4264 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4266 struct jumbo_ops *ops = &tp->jumbo_ops;
4268 switch (tp->mac_version) {
4269 case RTL_GIGA_MAC_VER_11:
4270 ops->disable = r8168b_0_hw_jumbo_disable;
4271 ops->enable = r8168b_0_hw_jumbo_enable;
4273 case RTL_GIGA_MAC_VER_12:
4274 case RTL_GIGA_MAC_VER_17:
4275 ops->disable = r8168b_1_hw_jumbo_disable;
4276 ops->enable = r8168b_1_hw_jumbo_enable;
4278 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4279 case RTL_GIGA_MAC_VER_19:
4280 case RTL_GIGA_MAC_VER_20:
4281 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4282 case RTL_GIGA_MAC_VER_22:
4283 case RTL_GIGA_MAC_VER_23:
4284 case RTL_GIGA_MAC_VER_24:
4285 case RTL_GIGA_MAC_VER_25:
4286 case RTL_GIGA_MAC_VER_26:
4287 ops->disable = r8168c_hw_jumbo_disable;
4288 ops->enable = r8168c_hw_jumbo_enable;
4290 case RTL_GIGA_MAC_VER_27:
4291 case RTL_GIGA_MAC_VER_28:
4292 ops->disable = r8168dp_hw_jumbo_disable;
4293 ops->enable = r8168dp_hw_jumbo_enable;
4295 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4296 case RTL_GIGA_MAC_VER_32:
4297 case RTL_GIGA_MAC_VER_33:
4298 case RTL_GIGA_MAC_VER_34:
4299 ops->disable = r8168e_hw_jumbo_disable;
4300 ops->enable = r8168e_hw_jumbo_enable;
4304 * No action needed for jumbo frames with 8169.
4305 * No jumbo for 810x at all.
4307 case RTL_GIGA_MAC_VER_40:
4308 case RTL_GIGA_MAC_VER_41:
4310 ops->disable = NULL;
4316 DECLARE_RTL_COND(rtl_chipcmd_cond)
4318 void __iomem *ioaddr = tp->mmio_addr;
4320 return RTL_R8(ChipCmd) & CmdReset;
4323 static void rtl_hw_reset(struct rtl8169_private *tp)
4325 void __iomem *ioaddr = tp->mmio_addr;
4327 RTL_W8(ChipCmd, CmdReset);
4329 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4332 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4334 struct rtl_fw *rtl_fw;
4338 name = rtl_lookup_firmware_name(tp);
4340 goto out_no_firmware;
4342 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4346 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4350 rc = rtl_check_firmware(tp, rtl_fw);
4352 goto err_release_firmware;
4354 tp->rtl_fw = rtl_fw;
4358 err_release_firmware:
4359 release_firmware(rtl_fw->fw);
4363 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4370 static void rtl_request_firmware(struct rtl8169_private *tp)
4372 if (IS_ERR(tp->rtl_fw))
4373 rtl_request_uncached_firmware(tp);
4376 static void rtl_rx_close(struct rtl8169_private *tp)
4378 void __iomem *ioaddr = tp->mmio_addr;
4380 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4383 DECLARE_RTL_COND(rtl_npq_cond)
4385 void __iomem *ioaddr = tp->mmio_addr;
4387 return RTL_R8(TxPoll) & NPQ;
4390 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4392 void __iomem *ioaddr = tp->mmio_addr;
4394 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4397 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4399 void __iomem *ioaddr = tp->mmio_addr;
4401 /* Disable interrupts */
4402 rtl8169_irq_mask_and_ack(tp);
4406 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4407 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4408 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4409 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4410 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4411 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4412 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
4413 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4414 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4415 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
4416 tp->mac_version == RTL_GIGA_MAC_VER_38) {
4417 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4418 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4420 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4427 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4429 void __iomem *ioaddr = tp->mmio_addr;
4431 /* Set DMA burst size and Interframe Gap Time */
4432 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4433 (InterFrameGap << TxInterFrameGapShift));
4436 static void rtl_hw_start(struct net_device *dev)
4438 struct rtl8169_private *tp = netdev_priv(dev);
4442 rtl_irq_enable_all(tp);
4445 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4446 void __iomem *ioaddr)
4449 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4450 * register to be written before TxDescAddrLow to work.
4451 * Switching from MMIO to I/O access fixes the issue as well.
4453 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4454 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4455 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4456 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4459 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4463 cmd = RTL_R16(CPlusCmd);
4464 RTL_W16(CPlusCmd, cmd);
4468 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
4470 /* Low hurts. Let's disable the filtering. */
4471 RTL_W16(RxMaxSize, rx_buf_sz + 1);
4474 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4476 static const struct rtl_cfg2_info {
4481 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4482 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4483 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4484 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4486 const struct rtl_cfg2_info *p = cfg2_info;
4490 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
4491 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4492 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4493 RTL_W32(0x7c, p->val);
4499 static void rtl_set_rx_mode(struct net_device *dev)
4501 struct rtl8169_private *tp = netdev_priv(dev);
4502 void __iomem *ioaddr = tp->mmio_addr;
4503 u32 mc_filter[2]; /* Multicast hash filter */
4507 if (dev->flags & IFF_PROMISC) {
4508 /* Unconditionally log net taps. */
4509 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4511 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4513 mc_filter[1] = mc_filter[0] = 0xffffffff;
4514 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4515 (dev->flags & IFF_ALLMULTI)) {
4516 /* Too many to filter perfectly -- accept all multicasts. */
4517 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4518 mc_filter[1] = mc_filter[0] = 0xffffffff;
4520 struct netdev_hw_addr *ha;
4522 rx_mode = AcceptBroadcast | AcceptMyPhys;
4523 mc_filter[1] = mc_filter[0] = 0;
4524 netdev_for_each_mc_addr(ha, dev) {
4525 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4526 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4527 rx_mode |= AcceptMulticast;
4531 if (dev->features & NETIF_F_RXALL)
4532 rx_mode |= (AcceptErr | AcceptRunt);
4534 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4536 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4537 u32 data = mc_filter[0];
4539 mc_filter[0] = swab32(mc_filter[1]);
4540 mc_filter[1] = swab32(data);
4543 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4544 mc_filter[1] = mc_filter[0] = 0xffffffff;
4546 RTL_W32(MAR0 + 4, mc_filter[1]);
4547 RTL_W32(MAR0 + 0, mc_filter[0]);
4549 RTL_W32(RxConfig, tmp);
4552 static void rtl_hw_start_8169(struct net_device *dev)
4554 struct rtl8169_private *tp = netdev_priv(dev);
4555 void __iomem *ioaddr = tp->mmio_addr;
4556 struct pci_dev *pdev = tp->pci_dev;
4558 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4559 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4560 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4563 RTL_W8(Cfg9346, Cfg9346_Unlock);
4564 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4565 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4566 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4567 tp->mac_version == RTL_GIGA_MAC_VER_04)
4568 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4572 RTL_W8(EarlyTxThres, NoEarlyTx);
4574 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4576 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4577 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4578 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4579 tp->mac_version == RTL_GIGA_MAC_VER_04)
4580 rtl_set_rx_tx_config_registers(tp);
4582 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4584 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4585 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4586 dprintk("Set MAC Reg C+CR Offset 0xE0. "
4587 "Bit-3 and bit-14 MUST be 1\n");
4588 tp->cp_cmd |= (1 << 14);
4591 RTL_W16(CPlusCmd, tp->cp_cmd);
4593 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4596 * Undocumented corner. Supposedly:
4597 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4599 RTL_W16(IntrMitigate, 0x0000);
4601 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4603 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4604 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4605 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4606 tp->mac_version != RTL_GIGA_MAC_VER_04) {
4607 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4608 rtl_set_rx_tx_config_registers(tp);
4611 RTL_W8(Cfg9346, Cfg9346_Lock);
4613 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4616 RTL_W32(RxMissed, 0);
4618 rtl_set_rx_mode(dev);
4620 /* no early-rx interrupts */
4621 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4624 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4626 if (tp->csi_ops.write)
4627 tp->csi_ops.write(tp, addr, value);
4630 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4632 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
4635 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
4639 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4640 rtl_csi_write(tp, 0x070c, csi | bits);
4643 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
4645 rtl_csi_access_enable(tp, 0x17000000);
4648 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
4650 rtl_csi_access_enable(tp, 0x27000000);
4653 DECLARE_RTL_COND(rtl_csiar_cond)
4655 void __iomem *ioaddr = tp->mmio_addr;
4657 return RTL_R32(CSIAR) & CSIAR_FLAG;
4660 static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
4662 void __iomem *ioaddr = tp->mmio_addr;
4664 RTL_W32(CSIDR, value);
4665 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4666 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4668 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4671 static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
4673 void __iomem *ioaddr = tp->mmio_addr;
4675 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4676 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4678 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4679 RTL_R32(CSIDR) : ~0;
4682 static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
4684 void __iomem *ioaddr = tp->mmio_addr;
4686 RTL_W32(CSIDR, value);
4687 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4688 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4691 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4694 static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
4696 void __iomem *ioaddr = tp->mmio_addr;
4698 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4699 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4701 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4702 RTL_R32(CSIDR) : ~0;
4705 static void rtl_init_csi_ops(struct rtl8169_private *tp)
4707 struct csi_ops *ops = &tp->csi_ops;
4709 switch (tp->mac_version) {
4710 case RTL_GIGA_MAC_VER_01:
4711 case RTL_GIGA_MAC_VER_02:
4712 case RTL_GIGA_MAC_VER_03:
4713 case RTL_GIGA_MAC_VER_04:
4714 case RTL_GIGA_MAC_VER_05:
4715 case RTL_GIGA_MAC_VER_06:
4716 case RTL_GIGA_MAC_VER_10:
4717 case RTL_GIGA_MAC_VER_11:
4718 case RTL_GIGA_MAC_VER_12:
4719 case RTL_GIGA_MAC_VER_13:
4720 case RTL_GIGA_MAC_VER_14:
4721 case RTL_GIGA_MAC_VER_15:
4722 case RTL_GIGA_MAC_VER_16:
4723 case RTL_GIGA_MAC_VER_17:
4728 case RTL_GIGA_MAC_VER_37:
4729 case RTL_GIGA_MAC_VER_38:
4730 ops->write = r8402_csi_write;
4731 ops->read = r8402_csi_read;
4735 ops->write = r8169_csi_write;
4736 ops->read = r8169_csi_read;
4742 unsigned int offset;
4747 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4753 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4754 rtl_ephy_write(tp, e->offset, w);
4759 static void rtl_disable_clock_request(struct pci_dev *pdev)
4761 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4762 PCI_EXP_LNKCTL_CLKREQ_EN);
4765 static void rtl_enable_clock_request(struct pci_dev *pdev)
4767 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4768 PCI_EXP_LNKCTL_CLKREQ_EN);
4771 #define R8168_CPCMD_QUIRK_MASK (\
4782 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4784 void __iomem *ioaddr = tp->mmio_addr;
4785 struct pci_dev *pdev = tp->pci_dev;
4787 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4789 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4791 if (tp->dev->mtu <= ETH_DATA_LEN) {
4792 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
4793 PCI_EXP_DEVCTL_NOSNOOP_EN);
4797 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4799 void __iomem *ioaddr = tp->mmio_addr;
4801 rtl_hw_start_8168bb(tp);
4803 RTL_W8(MaxTxPacketSize, TxPacketMax);
4805 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4808 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4810 void __iomem *ioaddr = tp->mmio_addr;
4811 struct pci_dev *pdev = tp->pci_dev;
4813 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4815 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4817 if (tp->dev->mtu <= ETH_DATA_LEN)
4818 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4820 rtl_disable_clock_request(pdev);
4822 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4825 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4827 static const struct ephy_info e_info_8168cp[] = {
4828 { 0x01, 0, 0x0001 },
4829 { 0x02, 0x0800, 0x1000 },
4830 { 0x03, 0, 0x0042 },
4831 { 0x06, 0x0080, 0x0000 },
4835 rtl_csi_access_enable_2(tp);
4837 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4839 __rtl_hw_start_8168cp(tp);
4842 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4844 void __iomem *ioaddr = tp->mmio_addr;
4845 struct pci_dev *pdev = tp->pci_dev;
4847 rtl_csi_access_enable_2(tp);
4849 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4851 if (tp->dev->mtu <= ETH_DATA_LEN)
4852 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4854 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4857 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4859 void __iomem *ioaddr = tp->mmio_addr;
4860 struct pci_dev *pdev = tp->pci_dev;
4862 rtl_csi_access_enable_2(tp);
4864 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4867 RTL_W8(DBG_REG, 0x20);
4869 RTL_W8(MaxTxPacketSize, TxPacketMax);
4871 if (tp->dev->mtu <= ETH_DATA_LEN)
4872 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4874 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4877 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4879 void __iomem *ioaddr = tp->mmio_addr;
4880 static const struct ephy_info e_info_8168c_1[] = {
4881 { 0x02, 0x0800, 0x1000 },
4882 { 0x03, 0, 0x0002 },
4883 { 0x06, 0x0080, 0x0000 }
4886 rtl_csi_access_enable_2(tp);
4888 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4890 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4892 __rtl_hw_start_8168cp(tp);
4895 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4897 static const struct ephy_info e_info_8168c_2[] = {
4898 { 0x01, 0, 0x0001 },
4899 { 0x03, 0x0400, 0x0220 }
4902 rtl_csi_access_enable_2(tp);
4904 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4906 __rtl_hw_start_8168cp(tp);
4909 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4911 rtl_hw_start_8168c_2(tp);
4914 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4916 rtl_csi_access_enable_2(tp);
4918 __rtl_hw_start_8168cp(tp);
4921 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4923 void __iomem *ioaddr = tp->mmio_addr;
4924 struct pci_dev *pdev = tp->pci_dev;
4926 rtl_csi_access_enable_2(tp);
4928 rtl_disable_clock_request(pdev);
4930 RTL_W8(MaxTxPacketSize, TxPacketMax);
4932 if (tp->dev->mtu <= ETH_DATA_LEN)
4933 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4935 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4938 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
4940 void __iomem *ioaddr = tp->mmio_addr;
4941 struct pci_dev *pdev = tp->pci_dev;
4943 rtl_csi_access_enable_1(tp);
4945 if (tp->dev->mtu <= ETH_DATA_LEN)
4946 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4948 RTL_W8(MaxTxPacketSize, TxPacketMax);
4950 rtl_disable_clock_request(pdev);
4953 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
4955 void __iomem *ioaddr = tp->mmio_addr;
4956 struct pci_dev *pdev = tp->pci_dev;
4957 static const struct ephy_info e_info_8168d_4[] = {
4959 { 0x19, 0x20, 0x50 },
4964 rtl_csi_access_enable_1(tp);
4966 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4968 RTL_W8(MaxTxPacketSize, TxPacketMax);
4970 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4971 const struct ephy_info *e = e_info_8168d_4 + i;
4974 w = rtl_ephy_read(tp, e->offset);
4975 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
4978 rtl_enable_clock_request(pdev);
4981 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
4983 void __iomem *ioaddr = tp->mmio_addr;
4984 struct pci_dev *pdev = tp->pci_dev;
4985 static const struct ephy_info e_info_8168e_1[] = {
4986 { 0x00, 0x0200, 0x0100 },
4987 { 0x00, 0x0000, 0x0004 },
4988 { 0x06, 0x0002, 0x0001 },
4989 { 0x06, 0x0000, 0x0030 },
4990 { 0x07, 0x0000, 0x2000 },
4991 { 0x00, 0x0000, 0x0020 },
4992 { 0x03, 0x5800, 0x2000 },
4993 { 0x03, 0x0000, 0x0001 },
4994 { 0x01, 0x0800, 0x1000 },
4995 { 0x07, 0x0000, 0x4000 },
4996 { 0x1e, 0x0000, 0x2000 },
4997 { 0x19, 0xffff, 0xfe6c },
4998 { 0x0a, 0x0000, 0x0040 }
5001 rtl_csi_access_enable_2(tp);
5003 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5005 if (tp->dev->mtu <= ETH_DATA_LEN)
5006 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5008 RTL_W8(MaxTxPacketSize, TxPacketMax);
5010 rtl_disable_clock_request(pdev);
5012 /* Reset tx FIFO pointer */
5013 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5014 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
5016 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5019 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5021 void __iomem *ioaddr = tp->mmio_addr;
5022 struct pci_dev *pdev = tp->pci_dev;
5023 static const struct ephy_info e_info_8168e_2[] = {
5024 { 0x09, 0x0000, 0x0080 },
5025 { 0x19, 0x0000, 0x0224 }
5028 rtl_csi_access_enable_1(tp);
5030 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5032 if (tp->dev->mtu <= ETH_DATA_LEN)
5033 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5035 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5036 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5037 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5038 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5039 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5040 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5041 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5042 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5044 RTL_W8(MaxTxPacketSize, EarlySize);
5046 rtl_disable_clock_request(pdev);
5048 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5049 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5051 /* Adjust EEE LED frequency */
5052 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5054 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5055 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5056 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5059 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5061 void __iomem *ioaddr = tp->mmio_addr;
5062 struct pci_dev *pdev = tp->pci_dev;
5064 rtl_csi_access_enable_2(tp);
5066 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5068 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5069 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5070 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5071 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5072 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5073 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5074 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5075 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5076 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5077 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5079 RTL_W8(MaxTxPacketSize, EarlySize);
5081 rtl_disable_clock_request(pdev);
5083 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5084 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5085 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5086 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5087 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5090 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5092 void __iomem *ioaddr = tp->mmio_addr;
5093 static const struct ephy_info e_info_8168f_1[] = {
5094 { 0x06, 0x00c0, 0x0020 },
5095 { 0x08, 0x0001, 0x0002 },
5096 { 0x09, 0x0000, 0x0080 },
5097 { 0x19, 0x0000, 0x0224 }
5100 rtl_hw_start_8168f(tp);
5102 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5104 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5106 /* Adjust EEE LED frequency */
5107 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5110 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5112 static const struct ephy_info e_info_8168f_1[] = {
5113 { 0x06, 0x00c0, 0x0020 },
5114 { 0x0f, 0xffff, 0x5200 },
5115 { 0x1e, 0x0000, 0x4000 },
5116 { 0x19, 0x0000, 0x0224 }
5119 rtl_hw_start_8168f(tp);
5121 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5123 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5126 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5128 void __iomem *ioaddr = tp->mmio_addr;
5129 struct pci_dev *pdev = tp->pci_dev;
5131 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5132 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5133 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5134 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5136 rtl_csi_access_enable_1(tp);
5138 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5140 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5141 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5143 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5144 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
5145 RTL_W8(MaxTxPacketSize, EarlySize);
5147 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5148 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5150 /* Adjust EEE LED frequency */
5151 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5153 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x02, ERIAR_EXGMAC);
5156 static void rtl_hw_start_8168(struct net_device *dev)
5158 struct rtl8169_private *tp = netdev_priv(dev);
5159 void __iomem *ioaddr = tp->mmio_addr;
5161 RTL_W8(Cfg9346, Cfg9346_Unlock);
5163 RTL_W8(MaxTxPacketSize, TxPacketMax);
5165 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5167 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
5169 RTL_W16(CPlusCmd, tp->cp_cmd);
5171 RTL_W16(IntrMitigate, 0x5151);
5173 /* Work around for RxFIFO overflow. */
5174 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5175 tp->event_slow |= RxFIFOOver | PCSTimeout;
5176 tp->event_slow &= ~RxOverflow;
5179 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5181 rtl_set_rx_mode(dev);
5183 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5184 (InterFrameGap << TxInterFrameGapShift));
5188 switch (tp->mac_version) {
5189 case RTL_GIGA_MAC_VER_11:
5190 rtl_hw_start_8168bb(tp);
5193 case RTL_GIGA_MAC_VER_12:
5194 case RTL_GIGA_MAC_VER_17:
5195 rtl_hw_start_8168bef(tp);
5198 case RTL_GIGA_MAC_VER_18:
5199 rtl_hw_start_8168cp_1(tp);
5202 case RTL_GIGA_MAC_VER_19:
5203 rtl_hw_start_8168c_1(tp);
5206 case RTL_GIGA_MAC_VER_20:
5207 rtl_hw_start_8168c_2(tp);
5210 case RTL_GIGA_MAC_VER_21:
5211 rtl_hw_start_8168c_3(tp);
5214 case RTL_GIGA_MAC_VER_22:
5215 rtl_hw_start_8168c_4(tp);
5218 case RTL_GIGA_MAC_VER_23:
5219 rtl_hw_start_8168cp_2(tp);
5222 case RTL_GIGA_MAC_VER_24:
5223 rtl_hw_start_8168cp_3(tp);
5226 case RTL_GIGA_MAC_VER_25:
5227 case RTL_GIGA_MAC_VER_26:
5228 case RTL_GIGA_MAC_VER_27:
5229 rtl_hw_start_8168d(tp);
5232 case RTL_GIGA_MAC_VER_28:
5233 rtl_hw_start_8168d_4(tp);
5236 case RTL_GIGA_MAC_VER_31:
5237 rtl_hw_start_8168dp(tp);
5240 case RTL_GIGA_MAC_VER_32:
5241 case RTL_GIGA_MAC_VER_33:
5242 rtl_hw_start_8168e_1(tp);
5244 case RTL_GIGA_MAC_VER_34:
5245 rtl_hw_start_8168e_2(tp);
5248 case RTL_GIGA_MAC_VER_35:
5249 case RTL_GIGA_MAC_VER_36:
5250 rtl_hw_start_8168f_1(tp);
5253 case RTL_GIGA_MAC_VER_38:
5254 rtl_hw_start_8411(tp);
5257 case RTL_GIGA_MAC_VER_40:
5258 case RTL_GIGA_MAC_VER_41:
5259 rtl_hw_start_8168g_1(tp);
5263 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5264 dev->name, tp->mac_version);
5268 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5270 RTL_W8(Cfg9346, Cfg9346_Lock);
5272 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
5275 #define R810X_CPCMD_QUIRK_MASK (\
5286 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5288 void __iomem *ioaddr = tp->mmio_addr;
5289 struct pci_dev *pdev = tp->pci_dev;
5290 static const struct ephy_info e_info_8102e_1[] = {
5291 { 0x01, 0, 0x6e65 },
5292 { 0x02, 0, 0x091f },
5293 { 0x03, 0, 0xc2f9 },
5294 { 0x06, 0, 0xafb5 },
5295 { 0x07, 0, 0x0e00 },
5296 { 0x19, 0, 0xec80 },
5297 { 0x01, 0, 0x2e65 },
5302 rtl_csi_access_enable_2(tp);
5304 RTL_W8(DBG_REG, FIX_NAK_1);
5306 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5309 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5310 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5312 cfg1 = RTL_R8(Config1);
5313 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5314 RTL_W8(Config1, cfg1 & ~LEDS0);
5316 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5319 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5321 void __iomem *ioaddr = tp->mmio_addr;
5322 struct pci_dev *pdev = tp->pci_dev;
5324 rtl_csi_access_enable_2(tp);
5326 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5328 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5329 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5332 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5334 rtl_hw_start_8102e_2(tp);
5336 rtl_ephy_write(tp, 0x03, 0xc2f9);
5339 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5341 void __iomem *ioaddr = tp->mmio_addr;
5342 static const struct ephy_info e_info_8105e_1[] = {
5343 { 0x07, 0, 0x4000 },
5344 { 0x19, 0, 0x0200 },
5345 { 0x19, 0, 0x0020 },
5346 { 0x1e, 0, 0x2000 },
5347 { 0x03, 0, 0x0001 },
5348 { 0x19, 0, 0x0100 },
5349 { 0x19, 0, 0x0004 },
5353 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5354 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5356 /* Disable Early Tally Counter */
5357 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5359 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5360 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5362 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5365 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5367 rtl_hw_start_8105e_1(tp);
5368 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5371 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5373 void __iomem *ioaddr = tp->mmio_addr;
5374 static const struct ephy_info e_info_8402[] = {
5375 { 0x19, 0xffff, 0xff64 },
5379 rtl_csi_access_enable_2(tp);
5381 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5382 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5384 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5385 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5387 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
5389 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5391 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5392 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5393 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5394 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5395 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5396 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5397 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
5400 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5402 void __iomem *ioaddr = tp->mmio_addr;
5404 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5405 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5407 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5408 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5409 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5412 static void rtl_hw_start_8101(struct net_device *dev)
5414 struct rtl8169_private *tp = netdev_priv(dev);
5415 void __iomem *ioaddr = tp->mmio_addr;
5416 struct pci_dev *pdev = tp->pci_dev;
5418 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5419 tp->event_slow &= ~RxFIFOOver;
5421 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5422 tp->mac_version == RTL_GIGA_MAC_VER_16)
5423 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5424 PCI_EXP_DEVCTL_NOSNOOP_EN);
5426 RTL_W8(Cfg9346, Cfg9346_Unlock);
5428 switch (tp->mac_version) {
5429 case RTL_GIGA_MAC_VER_07:
5430 rtl_hw_start_8102e_1(tp);
5433 case RTL_GIGA_MAC_VER_08:
5434 rtl_hw_start_8102e_3(tp);
5437 case RTL_GIGA_MAC_VER_09:
5438 rtl_hw_start_8102e_2(tp);
5441 case RTL_GIGA_MAC_VER_29:
5442 rtl_hw_start_8105e_1(tp);
5444 case RTL_GIGA_MAC_VER_30:
5445 rtl_hw_start_8105e_2(tp);
5448 case RTL_GIGA_MAC_VER_37:
5449 rtl_hw_start_8402(tp);
5452 case RTL_GIGA_MAC_VER_39:
5453 rtl_hw_start_8106(tp);
5457 RTL_W8(Cfg9346, Cfg9346_Lock);
5459 RTL_W8(MaxTxPacketSize, TxPacketMax);
5461 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5463 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5464 RTL_W16(CPlusCmd, tp->cp_cmd);
5466 RTL_W16(IntrMitigate, 0x0000);
5468 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5470 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5471 rtl_set_rx_tx_config_registers(tp);
5475 rtl_set_rx_mode(dev);
5477 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5480 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5482 struct rtl8169_private *tp = netdev_priv(dev);
5484 if (new_mtu < ETH_ZLEN ||
5485 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
5488 if (new_mtu > ETH_DATA_LEN)
5489 rtl_hw_jumbo_enable(tp);
5491 rtl_hw_jumbo_disable(tp);
5494 netdev_update_features(dev);
5499 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5501 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5502 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5505 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5506 void **data_buff, struct RxDesc *desc)
5508 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
5513 rtl8169_make_unusable_by_asic(desc);
5516 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5518 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5520 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5523 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5526 desc->addr = cpu_to_le64(mapping);
5528 rtl8169_mark_to_asic(desc, rx_buf_sz);
5531 static inline void *rtl8169_align(void *data)
5533 return (void *)ALIGN((long)data, 16);
5536 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5537 struct RxDesc *desc)
5541 struct device *d = &tp->pci_dev->dev;
5542 struct net_device *dev = tp->dev;
5543 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
5545 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5549 if (rtl8169_align(data) != data) {
5551 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5556 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
5558 if (unlikely(dma_mapping_error(d, mapping))) {
5559 if (net_ratelimit())
5560 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5564 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
5572 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5576 for (i = 0; i < NUM_RX_DESC; i++) {
5577 if (tp->Rx_databuff[i]) {
5578 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5579 tp->RxDescArray + i);
5584 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5586 desc->opts1 |= cpu_to_le32(RingEnd);
5589 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5593 for (i = 0; i < NUM_RX_DESC; i++) {
5596 if (tp->Rx_databuff[i])
5599 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5601 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5604 tp->Rx_databuff[i] = data;
5607 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5611 rtl8169_rx_clear(tp);
5615 static int rtl8169_init_ring(struct net_device *dev)
5617 struct rtl8169_private *tp = netdev_priv(dev);
5619 rtl8169_init_ring_indexes(tp);
5621 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
5622 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
5624 return rtl8169_rx_fill(tp);
5627 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5628 struct TxDesc *desc)
5630 unsigned int len = tx_skb->len;
5632 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5640 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5645 for (i = 0; i < n; i++) {
5646 unsigned int entry = (start + i) % NUM_TX_DESC;
5647 struct ring_info *tx_skb = tp->tx_skb + entry;
5648 unsigned int len = tx_skb->len;
5651 struct sk_buff *skb = tx_skb->skb;
5653 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5654 tp->TxDescArray + entry);
5656 tp->dev->stats.tx_dropped++;
5664 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5666 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5667 tp->cur_tx = tp->dirty_tx = 0;
5670 static void rtl_reset_work(struct rtl8169_private *tp)
5672 struct net_device *dev = tp->dev;
5675 napi_disable(&tp->napi);
5676 netif_stop_queue(dev);
5677 synchronize_sched();
5679 rtl8169_hw_reset(tp);
5681 for (i = 0; i < NUM_RX_DESC; i++)
5682 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5684 rtl8169_tx_clear(tp);
5685 rtl8169_init_ring_indexes(tp);
5687 napi_enable(&tp->napi);
5689 netif_wake_queue(dev);
5690 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
5693 static void rtl8169_tx_timeout(struct net_device *dev)
5695 struct rtl8169_private *tp = netdev_priv(dev);
5697 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5700 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5703 struct skb_shared_info *info = skb_shinfo(skb);
5704 unsigned int cur_frag, entry;
5705 struct TxDesc * uninitialized_var(txd);
5706 struct device *d = &tp->pci_dev->dev;
5709 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5710 const skb_frag_t *frag = info->frags + cur_frag;
5715 entry = (entry + 1) % NUM_TX_DESC;
5717 txd = tp->TxDescArray + entry;
5718 len = skb_frag_size(frag);
5719 addr = skb_frag_address(frag);
5720 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5721 if (unlikely(dma_mapping_error(d, mapping))) {
5722 if (net_ratelimit())
5723 netif_err(tp, drv, tp->dev,
5724 "Failed to map TX fragments DMA!\n");
5728 /* Anti gcc 2.95.3 bugware (sic) */
5729 status = opts[0] | len |
5730 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5732 txd->opts1 = cpu_to_le32(status);
5733 txd->opts2 = cpu_to_le32(opts[1]);
5734 txd->addr = cpu_to_le64(mapping);
5736 tp->tx_skb[entry].len = len;
5740 tp->tx_skb[entry].skb = skb;
5741 txd->opts1 |= cpu_to_le32(LastFrag);
5747 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5751 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5752 struct sk_buff *skb, u32 *opts)
5754 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
5755 u32 mss = skb_shinfo(skb)->gso_size;
5756 int offset = info->opts_offset;
5760 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5761 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5762 const struct iphdr *ip = ip_hdr(skb);
5764 if (ip->protocol == IPPROTO_TCP)
5765 opts[offset] |= info->checksum.tcp;
5766 else if (ip->protocol == IPPROTO_UDP)
5767 opts[offset] |= info->checksum.udp;
5773 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5774 struct net_device *dev)
5776 struct rtl8169_private *tp = netdev_priv(dev);
5777 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5778 struct TxDesc *txd = tp->TxDescArray + entry;
5779 void __iomem *ioaddr = tp->mmio_addr;
5780 struct device *d = &tp->pci_dev->dev;
5786 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
5787 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5791 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5794 len = skb_headlen(skb);
5795 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
5796 if (unlikely(dma_mapping_error(d, mapping))) {
5797 if (net_ratelimit())
5798 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
5802 tp->tx_skb[entry].len = len;
5803 txd->addr = cpu_to_le64(mapping);
5805 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
5808 rtl8169_tso_csum(tp, skb, opts);
5810 frags = rtl8169_xmit_frags(tp, skb, opts);
5814 opts[0] |= FirstFrag;
5816 opts[0] |= FirstFrag | LastFrag;
5817 tp->tx_skb[entry].skb = skb;
5820 txd->opts2 = cpu_to_le32(opts[1]);
5822 skb_tx_timestamp(skb);
5826 /* Anti gcc 2.95.3 bugware (sic) */
5827 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
5828 txd->opts1 = cpu_to_le32(status);
5830 tp->cur_tx += frags + 1;
5834 RTL_W8(TxPoll, NPQ);
5838 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5839 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5840 * not miss a ring update when it notices a stopped queue.
5843 netif_stop_queue(dev);
5844 /* Sync with rtl_tx:
5845 * - publish queue status and cur_tx ring index (write barrier)
5846 * - refresh dirty_tx ring index (read barrier).
5847 * May the current thread have a pessimistic view of the ring
5848 * status and forget to wake up queue, a racing rtl_tx thread
5852 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
5853 netif_wake_queue(dev);
5856 return NETDEV_TX_OK;
5859 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
5862 dev->stats.tx_dropped++;
5863 return NETDEV_TX_OK;
5866 netif_stop_queue(dev);
5867 dev->stats.tx_dropped++;
5868 return NETDEV_TX_BUSY;
5871 static void rtl8169_pcierr_interrupt(struct net_device *dev)
5873 struct rtl8169_private *tp = netdev_priv(dev);
5874 struct pci_dev *pdev = tp->pci_dev;
5875 u16 pci_status, pci_cmd;
5877 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5878 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5880 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5881 pci_cmd, pci_status);
5884 * The recovery sequence below admits a very elaborated explanation:
5885 * - it seems to work;
5886 * - I did not see what else could be done;
5887 * - it makes iop3xx happy.
5889 * Feel free to adjust to your needs.
5891 if (pdev->broken_parity_status)
5892 pci_cmd &= ~PCI_COMMAND_PARITY;
5894 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5896 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
5898 pci_write_config_word(pdev, PCI_STATUS,
5899 pci_status & (PCI_STATUS_DETECTED_PARITY |
5900 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5901 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5903 /* The infamous DAC f*ckup only happens at boot time */
5904 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
5905 void __iomem *ioaddr = tp->mmio_addr;
5907 netif_info(tp, intr, dev, "disabling PCI DAC\n");
5908 tp->cp_cmd &= ~PCIDAC;
5909 RTL_W16(CPlusCmd, tp->cp_cmd);
5910 dev->features &= ~NETIF_F_HIGHDMA;
5913 rtl8169_hw_reset(tp);
5915 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5918 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
5920 unsigned int dirty_tx, tx_left;
5922 dirty_tx = tp->dirty_tx;
5924 tx_left = tp->cur_tx - dirty_tx;
5926 while (tx_left > 0) {
5927 unsigned int entry = dirty_tx % NUM_TX_DESC;
5928 struct ring_info *tx_skb = tp->tx_skb + entry;
5932 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5933 if (status & DescOwn)
5936 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5937 tp->TxDescArray + entry);
5938 if (status & LastFrag) {
5939 u64_stats_update_begin(&tp->tx_stats.syncp);
5940 tp->tx_stats.packets++;
5941 tp->tx_stats.bytes += tx_skb->skb->len;
5942 u64_stats_update_end(&tp->tx_stats.syncp);
5943 dev_kfree_skb(tx_skb->skb);
5950 if (tp->dirty_tx != dirty_tx) {
5951 tp->dirty_tx = dirty_tx;
5952 /* Sync with rtl8169_start_xmit:
5953 * - publish dirty_tx ring index (write barrier)
5954 * - refresh cur_tx ring index and queue status (read barrier)
5955 * May the current thread miss the stopped queue condition,
5956 * a racing xmit thread can only have a right view of the
5960 if (netif_queue_stopped(dev) &&
5961 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5962 netif_wake_queue(dev);
5965 * 8168 hack: TxPoll requests are lost when the Tx packets are
5966 * too close. Let's kick an extra TxPoll request when a burst
5967 * of start_xmit activity is detected (if it is not detected,
5968 * it is slow enough). -- FR
5970 if (tp->cur_tx != dirty_tx) {
5971 void __iomem *ioaddr = tp->mmio_addr;
5973 RTL_W8(TxPoll, NPQ);
5978 static inline int rtl8169_fragmented_frame(u32 status)
5980 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5983 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
5985 u32 status = opts1 & RxProtoMask;
5987 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
5988 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5989 skb->ip_summed = CHECKSUM_UNNECESSARY;
5991 skb_checksum_none_assert(skb);
5994 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5995 struct rtl8169_private *tp,
5999 struct sk_buff *skb;
6000 struct device *d = &tp->pci_dev->dev;
6002 data = rtl8169_align(data);
6003 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6005 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6007 memcpy(skb->data, data, pkt_size);
6008 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6013 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
6015 unsigned int cur_rx, rx_left;
6018 cur_rx = tp->cur_rx;
6020 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
6021 unsigned int entry = cur_rx % NUM_RX_DESC;
6022 struct RxDesc *desc = tp->RxDescArray + entry;
6026 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
6028 if (status & DescOwn)
6030 if (unlikely(status & RxRES)) {
6031 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6033 dev->stats.rx_errors++;
6034 if (status & (RxRWT | RxRUNT))
6035 dev->stats.rx_length_errors++;
6037 dev->stats.rx_crc_errors++;
6038 if (status & RxFOVF) {
6039 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6040 dev->stats.rx_fifo_errors++;
6042 if ((status & (RxRUNT | RxCRC)) &&
6043 !(status & (RxRWT | RxFOVF)) &&
6044 (dev->features & NETIF_F_RXALL))
6047 struct sk_buff *skb;
6052 addr = le64_to_cpu(desc->addr);
6053 if (likely(!(dev->features & NETIF_F_RXFCS)))
6054 pkt_size = (status & 0x00003fff) - 4;
6056 pkt_size = status & 0x00003fff;
6059 * The driver does not support incoming fragmented
6060 * frames. They are seen as a symptom of over-mtu
6063 if (unlikely(rtl8169_fragmented_frame(status))) {
6064 dev->stats.rx_dropped++;
6065 dev->stats.rx_length_errors++;
6066 goto release_descriptor;
6069 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6070 tp, pkt_size, addr);
6072 dev->stats.rx_dropped++;
6073 goto release_descriptor;
6076 rtl8169_rx_csum(skb, status);
6077 skb_put(skb, pkt_size);
6078 skb->protocol = eth_type_trans(skb, dev);
6080 rtl8169_rx_vlan_tag(desc, skb);
6082 napi_gro_receive(&tp->napi, skb);
6084 u64_stats_update_begin(&tp->rx_stats.syncp);
6085 tp->rx_stats.packets++;
6086 tp->rx_stats.bytes += pkt_size;
6087 u64_stats_update_end(&tp->rx_stats.syncp);
6092 rtl8169_mark_to_asic(desc, rx_buf_sz);
6095 count = cur_rx - tp->cur_rx;
6096 tp->cur_rx = cur_rx;
6101 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6103 struct net_device *dev = dev_instance;
6104 struct rtl8169_private *tp = netdev_priv(dev);
6108 status = rtl_get_events(tp);
6109 if (status && status != 0xffff) {
6110 status &= RTL_EVENT_NAPI | tp->event_slow;
6114 rtl_irq_disable(tp);
6115 napi_schedule(&tp->napi);
6118 return IRQ_RETVAL(handled);
6122 * Workqueue context.
6124 static void rtl_slow_event_work(struct rtl8169_private *tp)
6126 struct net_device *dev = tp->dev;
6129 status = rtl_get_events(tp) & tp->event_slow;
6130 rtl_ack_events(tp, status);
6132 if (unlikely(status & RxFIFOOver)) {
6133 switch (tp->mac_version) {
6134 /* Work around for rx fifo overflow */
6135 case RTL_GIGA_MAC_VER_11:
6136 netif_stop_queue(dev);
6137 /* XXX - Hack alert. See rtl_task(). */
6138 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6144 if (unlikely(status & SYSErr))
6145 rtl8169_pcierr_interrupt(dev);
6147 if (status & LinkChg)
6148 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6150 rtl_irq_enable_all(tp);
6153 static void rtl_task(struct work_struct *work)
6155 static const struct {
6157 void (*action)(struct rtl8169_private *);
6159 /* XXX - keep rtl_slow_event_work() as first element. */
6160 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6161 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6162 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6164 struct rtl8169_private *tp =
6165 container_of(work, struct rtl8169_private, wk.work);
6166 struct net_device *dev = tp->dev;
6171 if (!netif_running(dev) ||
6172 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6175 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6178 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6180 rtl_work[i].action(tp);
6184 rtl_unlock_work(tp);
6187 static int rtl8169_poll(struct napi_struct *napi, int budget)
6189 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6190 struct net_device *dev = tp->dev;
6191 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6195 status = rtl_get_events(tp);
6196 rtl_ack_events(tp, status & ~tp->event_slow);
6198 if (status & RTL_EVENT_NAPI_RX)
6199 work_done = rtl_rx(dev, tp, (u32) budget);
6201 if (status & RTL_EVENT_NAPI_TX)
6204 if (status & tp->event_slow) {
6205 enable_mask &= ~tp->event_slow;
6207 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6210 if (work_done < budget) {
6211 napi_complete(napi);
6213 rtl_irq_enable(tp, enable_mask);
6220 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6222 struct rtl8169_private *tp = netdev_priv(dev);
6224 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6227 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6228 RTL_W32(RxMissed, 0);
6231 static void rtl8169_down(struct net_device *dev)
6233 struct rtl8169_private *tp = netdev_priv(dev);
6234 void __iomem *ioaddr = tp->mmio_addr;
6236 del_timer_sync(&tp->timer);
6238 napi_disable(&tp->napi);
6239 netif_stop_queue(dev);
6241 rtl8169_hw_reset(tp);
6243 * At this point device interrupts can not be enabled in any function,
6244 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6245 * and napi is disabled (rtl8169_poll).
6247 rtl8169_rx_missed(dev, ioaddr);
6249 /* Give a racing hard_start_xmit a few cycles to complete. */
6250 synchronize_sched();
6252 rtl8169_tx_clear(tp);
6254 rtl8169_rx_clear(tp);
6256 rtl_pll_power_down(tp);
6259 static int rtl8169_close(struct net_device *dev)
6261 struct rtl8169_private *tp = netdev_priv(dev);
6262 struct pci_dev *pdev = tp->pci_dev;
6264 pm_runtime_get_sync(&pdev->dev);
6266 /* Update counters before going down */
6267 rtl8169_update_counters(dev);
6270 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6273 rtl_unlock_work(tp);
6275 free_irq(pdev->irq, dev);
6277 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6279 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6281 tp->TxDescArray = NULL;
6282 tp->RxDescArray = NULL;
6284 pm_runtime_put_sync(&pdev->dev);
6289 #ifdef CONFIG_NET_POLL_CONTROLLER
6290 static void rtl8169_netpoll(struct net_device *dev)
6292 struct rtl8169_private *tp = netdev_priv(dev);
6294 rtl8169_interrupt(tp->pci_dev->irq, dev);
6298 static int rtl_open(struct net_device *dev)
6300 struct rtl8169_private *tp = netdev_priv(dev);
6301 void __iomem *ioaddr = tp->mmio_addr;
6302 struct pci_dev *pdev = tp->pci_dev;
6303 int retval = -ENOMEM;
6305 pm_runtime_get_sync(&pdev->dev);
6308 * Rx and Tx descriptors needs 256 bytes alignment.
6309 * dma_alloc_coherent provides more.
6311 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6312 &tp->TxPhyAddr, GFP_KERNEL);
6313 if (!tp->TxDescArray)
6314 goto err_pm_runtime_put;
6316 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6317 &tp->RxPhyAddr, GFP_KERNEL);
6318 if (!tp->RxDescArray)
6321 retval = rtl8169_init_ring(dev);
6325 INIT_WORK(&tp->wk.work, rtl_task);
6329 rtl_request_firmware(tp);
6331 retval = request_irq(pdev->irq, rtl8169_interrupt,
6332 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6335 goto err_release_fw_2;
6339 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6341 napi_enable(&tp->napi);
6343 rtl8169_init_phy(dev, tp);
6345 __rtl8169_set_features(dev, dev->features);
6347 rtl_pll_power_up(tp);
6351 netif_start_queue(dev);
6353 rtl_unlock_work(tp);
6355 tp->saved_wolopts = 0;
6356 pm_runtime_put_noidle(&pdev->dev);
6358 rtl8169_check_link_status(dev, tp, ioaddr);
6363 rtl_release_firmware(tp);
6364 rtl8169_rx_clear(tp);
6366 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6368 tp->RxDescArray = NULL;
6370 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6372 tp->TxDescArray = NULL;
6374 pm_runtime_put_noidle(&pdev->dev);
6378 static struct rtnl_link_stats64 *
6379 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6381 struct rtl8169_private *tp = netdev_priv(dev);
6382 void __iomem *ioaddr = tp->mmio_addr;
6385 if (netif_running(dev))
6386 rtl8169_rx_missed(dev, ioaddr);
6389 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6390 stats->rx_packets = tp->rx_stats.packets;
6391 stats->rx_bytes = tp->rx_stats.bytes;
6392 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6396 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6397 stats->tx_packets = tp->tx_stats.packets;
6398 stats->tx_bytes = tp->tx_stats.bytes;
6399 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6401 stats->rx_dropped = dev->stats.rx_dropped;
6402 stats->tx_dropped = dev->stats.tx_dropped;
6403 stats->rx_length_errors = dev->stats.rx_length_errors;
6404 stats->rx_errors = dev->stats.rx_errors;
6405 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6406 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6407 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6412 static void rtl8169_net_suspend(struct net_device *dev)
6414 struct rtl8169_private *tp = netdev_priv(dev);
6416 if (!netif_running(dev))
6419 netif_device_detach(dev);
6420 netif_stop_queue(dev);
6423 napi_disable(&tp->napi);
6424 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6425 rtl_unlock_work(tp);
6427 rtl_pll_power_down(tp);
6432 static int rtl8169_suspend(struct device *device)
6434 struct pci_dev *pdev = to_pci_dev(device);
6435 struct net_device *dev = pci_get_drvdata(pdev);
6437 rtl8169_net_suspend(dev);
6442 static void __rtl8169_resume(struct net_device *dev)
6444 struct rtl8169_private *tp = netdev_priv(dev);
6446 netif_device_attach(dev);
6448 rtl_pll_power_up(tp);
6451 napi_enable(&tp->napi);
6452 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6453 rtl_unlock_work(tp);
6455 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6458 static int rtl8169_resume(struct device *device)
6460 struct pci_dev *pdev = to_pci_dev(device);
6461 struct net_device *dev = pci_get_drvdata(pdev);
6462 struct rtl8169_private *tp = netdev_priv(dev);
6464 rtl8169_init_phy(dev, tp);
6466 if (netif_running(dev))
6467 __rtl8169_resume(dev);
6472 static int rtl8169_runtime_suspend(struct device *device)
6474 struct pci_dev *pdev = to_pci_dev(device);
6475 struct net_device *dev = pci_get_drvdata(pdev);
6476 struct rtl8169_private *tp = netdev_priv(dev);
6478 if (!tp->TxDescArray)
6482 tp->saved_wolopts = __rtl8169_get_wol(tp);
6483 __rtl8169_set_wol(tp, WAKE_ANY);
6484 rtl_unlock_work(tp);
6486 rtl8169_net_suspend(dev);
6491 static int rtl8169_runtime_resume(struct device *device)
6493 struct pci_dev *pdev = to_pci_dev(device);
6494 struct net_device *dev = pci_get_drvdata(pdev);
6495 struct rtl8169_private *tp = netdev_priv(dev);
6497 if (!tp->TxDescArray)
6501 __rtl8169_set_wol(tp, tp->saved_wolopts);
6502 tp->saved_wolopts = 0;
6503 rtl_unlock_work(tp);
6505 rtl8169_init_phy(dev, tp);
6507 __rtl8169_resume(dev);
6512 static int rtl8169_runtime_idle(struct device *device)
6514 struct pci_dev *pdev = to_pci_dev(device);
6515 struct net_device *dev = pci_get_drvdata(pdev);
6516 struct rtl8169_private *tp = netdev_priv(dev);
6518 return tp->TxDescArray ? -EBUSY : 0;
6521 static const struct dev_pm_ops rtl8169_pm_ops = {
6522 .suspend = rtl8169_suspend,
6523 .resume = rtl8169_resume,
6524 .freeze = rtl8169_suspend,
6525 .thaw = rtl8169_resume,
6526 .poweroff = rtl8169_suspend,
6527 .restore = rtl8169_resume,
6528 .runtime_suspend = rtl8169_runtime_suspend,
6529 .runtime_resume = rtl8169_runtime_resume,
6530 .runtime_idle = rtl8169_runtime_idle,
6533 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6535 #else /* !CONFIG_PM */
6537 #define RTL8169_PM_OPS NULL
6539 #endif /* !CONFIG_PM */
6541 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6543 void __iomem *ioaddr = tp->mmio_addr;
6545 /* WoL fails with 8168b when the receiver is disabled. */
6546 switch (tp->mac_version) {
6547 case RTL_GIGA_MAC_VER_11:
6548 case RTL_GIGA_MAC_VER_12:
6549 case RTL_GIGA_MAC_VER_17:
6550 pci_clear_master(tp->pci_dev);
6552 RTL_W8(ChipCmd, CmdRxEnb);
6561 static void rtl_shutdown(struct pci_dev *pdev)
6563 struct net_device *dev = pci_get_drvdata(pdev);
6564 struct rtl8169_private *tp = netdev_priv(dev);
6565 struct device *d = &pdev->dev;
6567 pm_runtime_get_sync(d);
6569 rtl8169_net_suspend(dev);
6571 /* Restore original MAC address */
6572 rtl_rar_set(tp, dev->perm_addr);
6574 rtl8169_hw_reset(tp);
6576 if (system_state == SYSTEM_POWER_OFF) {
6577 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6578 rtl_wol_suspend_quirk(tp);
6579 rtl_wol_shutdown_quirk(tp);
6582 pci_wake_from_d3(pdev, true);
6583 pci_set_power_state(pdev, PCI_D3hot);
6586 pm_runtime_put_noidle(d);
6589 static void rtl_remove_one(struct pci_dev *pdev)
6591 struct net_device *dev = pci_get_drvdata(pdev);
6592 struct rtl8169_private *tp = netdev_priv(dev);
6594 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6595 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6596 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6597 rtl8168_driver_stop(tp);
6600 cancel_work_sync(&tp->wk.work);
6602 netif_napi_del(&tp->napi);
6604 unregister_netdev(dev);
6606 rtl_release_firmware(tp);
6608 if (pci_dev_run_wake(pdev))
6609 pm_runtime_get_noresume(&pdev->dev);
6611 /* restore original MAC address */
6612 rtl_rar_set(tp, dev->perm_addr);
6614 rtl_disable_msi(pdev, tp);
6615 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6616 pci_set_drvdata(pdev, NULL);
6619 static const struct net_device_ops rtl_netdev_ops = {
6620 .ndo_open = rtl_open,
6621 .ndo_stop = rtl8169_close,
6622 .ndo_get_stats64 = rtl8169_get_stats64,
6623 .ndo_start_xmit = rtl8169_start_xmit,
6624 .ndo_tx_timeout = rtl8169_tx_timeout,
6625 .ndo_validate_addr = eth_validate_addr,
6626 .ndo_change_mtu = rtl8169_change_mtu,
6627 .ndo_fix_features = rtl8169_fix_features,
6628 .ndo_set_features = rtl8169_set_features,
6629 .ndo_set_mac_address = rtl_set_mac_address,
6630 .ndo_do_ioctl = rtl8169_ioctl,
6631 .ndo_set_rx_mode = rtl_set_rx_mode,
6632 #ifdef CONFIG_NET_POLL_CONTROLLER
6633 .ndo_poll_controller = rtl8169_netpoll,
6638 static const struct rtl_cfg_info {
6639 void (*hw_start)(struct net_device *);
6640 unsigned int region;
6645 } rtl_cfg_infos [] = {
6647 .hw_start = rtl_hw_start_8169,
6650 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6651 .features = RTL_FEATURE_GMII,
6652 .default_ver = RTL_GIGA_MAC_VER_01,
6655 .hw_start = rtl_hw_start_8168,
6658 .event_slow = SYSErr | LinkChg | RxOverflow,
6659 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6660 .default_ver = RTL_GIGA_MAC_VER_11,
6663 .hw_start = rtl_hw_start_8101,
6666 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6668 .features = RTL_FEATURE_MSI,
6669 .default_ver = RTL_GIGA_MAC_VER_13,
6673 /* Cfg9346_Unlock assumed. */
6674 static unsigned rtl_try_msi(struct rtl8169_private *tp,
6675 const struct rtl_cfg_info *cfg)
6677 void __iomem *ioaddr = tp->mmio_addr;
6681 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6682 if (cfg->features & RTL_FEATURE_MSI) {
6683 if (pci_enable_msi(tp->pci_dev)) {
6684 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6687 msi = RTL_FEATURE_MSI;
6690 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6691 RTL_W8(Config2, cfg2);
6695 DECLARE_RTL_COND(rtl_link_list_ready_cond)
6697 void __iomem *ioaddr = tp->mmio_addr;
6699 return RTL_R8(MCU) & LINK_LIST_RDY;
6702 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6704 void __iomem *ioaddr = tp->mmio_addr;
6706 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
6709 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
6711 void __iomem *ioaddr = tp->mmio_addr;
6714 tp->ocp_base = OCP_STD_PHY_BASE;
6716 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
6718 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6721 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6724 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6726 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6728 data = r8168_mac_ocp_read(tp, 0xe8de);
6730 r8168_mac_ocp_write(tp, 0xe8de, data);
6732 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6735 data = r8168_mac_ocp_read(tp, 0xe8de);
6737 r8168_mac_ocp_write(tp, 0xe8de, data);
6739 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6743 static void rtl_hw_initialize(struct rtl8169_private *tp)
6745 switch (tp->mac_version) {
6746 case RTL_GIGA_MAC_VER_40:
6747 case RTL_GIGA_MAC_VER_41:
6748 rtl_hw_init_8168g(tp);
6757 rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6759 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6760 const unsigned int region = cfg->region;
6761 struct rtl8169_private *tp;
6762 struct mii_if_info *mii;
6763 struct net_device *dev;
6764 void __iomem *ioaddr;
6768 if (netif_msg_drv(&debug)) {
6769 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6770 MODULENAME, RTL8169_VERSION);
6773 dev = alloc_etherdev(sizeof (*tp));
6779 SET_NETDEV_DEV(dev, &pdev->dev);
6780 dev->netdev_ops = &rtl_netdev_ops;
6781 tp = netdev_priv(dev);
6784 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6788 mii->mdio_read = rtl_mdio_read;
6789 mii->mdio_write = rtl_mdio_write;
6790 mii->phy_id_mask = 0x1f;
6791 mii->reg_num_mask = 0x1f;
6792 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6794 /* disable ASPM completely as that cause random device stop working
6795 * problems as well as full system hangs for some PCIe devices users */
6796 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6797 PCIE_LINK_STATE_CLKPM);
6799 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6800 rc = pci_enable_device(pdev);
6802 netif_err(tp, probe, dev, "enable failure\n");
6803 goto err_out_free_dev_1;
6806 if (pci_set_mwi(pdev) < 0)
6807 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6809 /* make sure PCI base addr 1 is MMIO */
6810 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6811 netif_err(tp, probe, dev,
6812 "region #%d not an MMIO resource, aborting\n",
6818 /* check for weird/broken PCI region reporting */
6819 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6820 netif_err(tp, probe, dev,
6821 "Invalid PCI region size(s), aborting\n");
6826 rc = pci_request_regions(pdev, MODULENAME);
6828 netif_err(tp, probe, dev, "could not request regions\n");
6832 tp->cp_cmd = RxChkSum;
6834 if ((sizeof(dma_addr_t) > 4) &&
6835 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6836 tp->cp_cmd |= PCIDAC;
6837 dev->features |= NETIF_F_HIGHDMA;
6839 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6841 netif_err(tp, probe, dev, "DMA configuration failed\n");
6842 goto err_out_free_res_3;
6846 /* ioremap MMIO region */
6847 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6849 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6851 goto err_out_free_res_3;
6853 tp->mmio_addr = ioaddr;
6855 if (!pci_is_pcie(pdev))
6856 netif_info(tp, probe, dev, "not PCI Express\n");
6858 /* Identify chip attached to board */
6859 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6863 rtl_irq_disable(tp);
6865 rtl_hw_initialize(tp);
6869 rtl_ack_events(tp, 0xffff);
6871 pci_set_master(pdev);
6874 * Pretend we are using VLANs; This bypasses a nasty bug where
6875 * Interrupts stop flowing on high load on 8110SCd controllers.
6877 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6878 tp->cp_cmd |= RxVlan;
6880 rtl_init_mdio_ops(tp);
6881 rtl_init_pll_power_ops(tp);
6882 rtl_init_jumbo_ops(tp);
6883 rtl_init_csi_ops(tp);
6885 rtl8169_print_mac_version(tp);
6887 chipset = tp->mac_version;
6888 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6890 RTL_W8(Cfg9346, Cfg9346_Unlock);
6891 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6892 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6893 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6894 tp->features |= RTL_FEATURE_WOL;
6895 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6896 tp->features |= RTL_FEATURE_WOL;
6897 tp->features |= rtl_try_msi(tp, cfg);
6898 RTL_W8(Cfg9346, Cfg9346_Lock);
6900 if (rtl_tbi_enabled(tp)) {
6901 tp->set_speed = rtl8169_set_speed_tbi;
6902 tp->get_settings = rtl8169_gset_tbi;
6903 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6904 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6905 tp->link_ok = rtl8169_tbi_link_ok;
6906 tp->do_ioctl = rtl_tbi_ioctl;
6908 tp->set_speed = rtl8169_set_speed_xmii;
6909 tp->get_settings = rtl8169_gset_xmii;
6910 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6911 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6912 tp->link_ok = rtl8169_xmii_link_ok;
6913 tp->do_ioctl = rtl_xmii_ioctl;
6916 mutex_init(&tp->wk.mutex);
6918 /* Get MAC address */
6919 for (i = 0; i < ETH_ALEN; i++)
6920 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6922 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6923 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
6925 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6927 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6928 * properly for all devices */
6929 dev->features |= NETIF_F_RXCSUM |
6930 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6932 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6933 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6934 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6937 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6938 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6939 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6941 dev->hw_features |= NETIF_F_RXALL;
6942 dev->hw_features |= NETIF_F_RXFCS;
6944 tp->hw_start = cfg->hw_start;
6945 tp->event_slow = cfg->event_slow;
6947 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6948 ~(RxBOVF | RxFOVF) : ~0;
6950 init_timer(&tp->timer);
6951 tp->timer.data = (unsigned long) dev;
6952 tp->timer.function = rtl8169_phy_timer;
6954 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6956 rc = register_netdev(dev);
6960 pci_set_drvdata(pdev, dev);
6962 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6963 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6964 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
6965 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6966 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6967 "tx checksumming: %s]\n",
6968 rtl_chip_infos[chipset].jumbo_max,
6969 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6972 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6973 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6974 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6975 rtl8168_driver_start(tp);
6978 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6980 if (pci_dev_run_wake(pdev))
6981 pm_runtime_put_noidle(&pdev->dev);
6983 netif_carrier_off(dev);
6989 netif_napi_del(&tp->napi);
6990 rtl_disable_msi(pdev, tp);
6993 pci_release_regions(pdev);
6995 pci_clear_mwi(pdev);
6996 pci_disable_device(pdev);
7002 static struct pci_driver rtl8169_pci_driver = {
7004 .id_table = rtl8169_pci_tbl,
7005 .probe = rtl_init_one,
7006 .remove = rtl_remove_one,
7007 .shutdown = rtl_shutdown,
7008 .driver.pm = RTL8169_PM_OPS,
7011 module_pci_driver(rtl8169_pci_driver);