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 DECLARE_RTL_COND(rtl_phyar_cond)
1074 void __iomem *ioaddr = tp->mmio_addr;
1076 return RTL_R32(PHYAR) & 0x80000000;
1079 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1081 void __iomem *ioaddr = tp->mmio_addr;
1083 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1085 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1087 * According to hardware specs a 20us delay is required after write
1088 * complete indication, but before sending next command.
1093 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1095 void __iomem *ioaddr = tp->mmio_addr;
1098 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
1100 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1101 RTL_R32(PHYAR) & 0xffff : ~0;
1104 * According to hardware specs a 20us delay is required after read
1105 * complete indication, but before sending next command.
1112 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1114 void __iomem *ioaddr = tp->mmio_addr;
1116 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1117 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1118 RTL_W32(EPHY_RXER_NUM, 0);
1120 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1123 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1125 r8168dp_1_mdio_access(tp, reg,
1126 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1129 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1131 void __iomem *ioaddr = tp->mmio_addr;
1133 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1136 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1137 RTL_W32(EPHY_RXER_NUM, 0);
1139 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1140 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
1143 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1145 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1147 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1150 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1152 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1155 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1157 void __iomem *ioaddr = tp->mmio_addr;
1159 r8168dp_2_mdio_start(ioaddr);
1161 r8169_mdio_write(tp, reg, value);
1163 r8168dp_2_mdio_stop(ioaddr);
1166 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1168 void __iomem *ioaddr = tp->mmio_addr;
1171 r8168dp_2_mdio_start(ioaddr);
1173 value = r8169_mdio_read(tp, reg);
1175 r8168dp_2_mdio_stop(ioaddr);
1180 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1182 tp->mdio_ops.write(tp, location, val);
1185 static int rtl_readphy(struct rtl8169_private *tp, int location)
1187 return tp->mdio_ops.read(tp, location);
1190 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1192 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1195 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1199 val = rtl_readphy(tp, reg_addr);
1200 rtl_writephy(tp, reg_addr, (val | p) & ~m);
1203 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1206 struct rtl8169_private *tp = netdev_priv(dev);
1208 rtl_writephy(tp, location, val);
1211 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1213 struct rtl8169_private *tp = netdev_priv(dev);
1215 return rtl_readphy(tp, location);
1218 DECLARE_RTL_COND(rtl_ephyar_cond)
1220 void __iomem *ioaddr = tp->mmio_addr;
1222 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1225 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1227 void __iomem *ioaddr = tp->mmio_addr;
1229 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1230 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1232 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1237 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1239 void __iomem *ioaddr = tp->mmio_addr;
1241 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1243 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1244 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
1247 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1250 void __iomem *ioaddr = tp->mmio_addr;
1252 BUG_ON((addr & 3) || (mask == 0));
1253 RTL_W32(ERIDR, val);
1254 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1256 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1259 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1261 void __iomem *ioaddr = tp->mmio_addr;
1263 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1265 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1266 RTL_R32(ERIDR) : ~0;
1269 static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1274 val = rtl_eri_read(tp, addr, type);
1275 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1284 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1285 const struct exgmac_reg *r, int len)
1288 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1293 DECLARE_RTL_COND(rtl_efusear_cond)
1295 void __iomem *ioaddr = tp->mmio_addr;
1297 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1300 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1302 void __iomem *ioaddr = tp->mmio_addr;
1304 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1306 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1307 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1310 static u16 rtl_get_events(struct rtl8169_private *tp)
1312 void __iomem *ioaddr = tp->mmio_addr;
1314 return RTL_R16(IntrStatus);
1317 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1319 void __iomem *ioaddr = tp->mmio_addr;
1321 RTL_W16(IntrStatus, bits);
1325 static void rtl_irq_disable(struct rtl8169_private *tp)
1327 void __iomem *ioaddr = tp->mmio_addr;
1329 RTL_W16(IntrMask, 0);
1333 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1335 void __iomem *ioaddr = tp->mmio_addr;
1337 RTL_W16(IntrMask, bits);
1340 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1341 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1342 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1344 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1346 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1349 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1351 void __iomem *ioaddr = tp->mmio_addr;
1353 rtl_irq_disable(tp);
1354 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1358 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1360 void __iomem *ioaddr = tp->mmio_addr;
1362 return RTL_R32(TBICSR) & TBIReset;
1365 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1367 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1370 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1372 return RTL_R32(TBICSR) & TBILinkOk;
1375 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1377 return RTL_R8(PHYstatus) & LinkStatus;
1380 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1382 void __iomem *ioaddr = tp->mmio_addr;
1384 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1387 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1391 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1392 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1395 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1397 void __iomem *ioaddr = tp->mmio_addr;
1398 struct net_device *dev = tp->dev;
1400 if (!netif_running(dev))
1403 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1404 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1405 if (RTL_R8(PHYstatus) & _1000bpsF) {
1406 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1408 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1410 } else if (RTL_R8(PHYstatus) & _100bps) {
1411 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1413 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1416 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1418 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1421 /* Reset packet filter */
1422 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1424 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1426 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1427 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1428 if (RTL_R8(PHYstatus) & _1000bpsF) {
1429 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1431 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1434 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1436 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1439 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1440 if (RTL_R8(PHYstatus) & _10bps) {
1441 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1443 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1446 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1452 static void __rtl8169_check_link_status(struct net_device *dev,
1453 struct rtl8169_private *tp,
1454 void __iomem *ioaddr, bool pm)
1456 if (tp->link_ok(ioaddr)) {
1457 rtl_link_chg_patch(tp);
1458 /* This is to cancel a scheduled suspend if there's one. */
1460 pm_request_resume(&tp->pci_dev->dev);
1461 netif_carrier_on(dev);
1462 if (net_ratelimit())
1463 netif_info(tp, ifup, dev, "link up\n");
1465 netif_carrier_off(dev);
1466 netif_info(tp, ifdown, dev, "link down\n");
1468 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1472 static void rtl8169_check_link_status(struct net_device *dev,
1473 struct rtl8169_private *tp,
1474 void __iomem *ioaddr)
1476 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1479 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1481 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1483 void __iomem *ioaddr = tp->mmio_addr;
1487 options = RTL_R8(Config1);
1488 if (!(options & PMEnable))
1491 options = RTL_R8(Config3);
1492 if (options & LinkUp)
1493 wolopts |= WAKE_PHY;
1494 if (options & MagicPacket)
1495 wolopts |= WAKE_MAGIC;
1497 options = RTL_R8(Config5);
1499 wolopts |= WAKE_UCAST;
1501 wolopts |= WAKE_BCAST;
1503 wolopts |= WAKE_MCAST;
1508 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1510 struct rtl8169_private *tp = netdev_priv(dev);
1514 wol->supported = WAKE_ANY;
1515 wol->wolopts = __rtl8169_get_wol(tp);
1517 rtl_unlock_work(tp);
1520 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1522 void __iomem *ioaddr = tp->mmio_addr;
1524 static const struct {
1529 { WAKE_PHY, Config3, LinkUp },
1530 { WAKE_MAGIC, Config3, MagicPacket },
1531 { WAKE_UCAST, Config5, UWF },
1532 { WAKE_BCAST, Config5, BWF },
1533 { WAKE_MCAST, Config5, MWF },
1534 { WAKE_ANY, Config5, LanWake }
1538 RTL_W8(Cfg9346, Cfg9346_Unlock);
1540 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1541 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1542 if (wolopts & cfg[i].opt)
1543 options |= cfg[i].mask;
1544 RTL_W8(cfg[i].reg, options);
1547 switch (tp->mac_version) {
1548 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1549 options = RTL_R8(Config1) & ~PMEnable;
1551 options |= PMEnable;
1552 RTL_W8(Config1, options);
1555 options = RTL_R8(Config2) & ~PME_SIGNAL;
1557 options |= PME_SIGNAL;
1558 RTL_W8(Config2, options);
1562 RTL_W8(Cfg9346, Cfg9346_Lock);
1565 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1567 struct rtl8169_private *tp = netdev_priv(dev);
1572 tp->features |= RTL_FEATURE_WOL;
1574 tp->features &= ~RTL_FEATURE_WOL;
1575 __rtl8169_set_wol(tp, wol->wolopts);
1577 rtl_unlock_work(tp);
1579 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1584 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1586 return rtl_chip_infos[tp->mac_version].fw_name;
1589 static void rtl8169_get_drvinfo(struct net_device *dev,
1590 struct ethtool_drvinfo *info)
1592 struct rtl8169_private *tp = netdev_priv(dev);
1593 struct rtl_fw *rtl_fw = tp->rtl_fw;
1595 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1596 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1597 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1598 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1599 if (!IS_ERR_OR_NULL(rtl_fw))
1600 strlcpy(info->fw_version, rtl_fw->version,
1601 sizeof(info->fw_version));
1604 static int rtl8169_get_regs_len(struct net_device *dev)
1606 return R8169_REGS_SIZE;
1609 static int rtl8169_set_speed_tbi(struct net_device *dev,
1610 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1612 struct rtl8169_private *tp = netdev_priv(dev);
1613 void __iomem *ioaddr = tp->mmio_addr;
1617 reg = RTL_R32(TBICSR);
1618 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1619 (duplex == DUPLEX_FULL)) {
1620 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1621 } else if (autoneg == AUTONEG_ENABLE)
1622 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1624 netif_warn(tp, link, dev,
1625 "incorrect speed setting refused in TBI mode\n");
1632 static int rtl8169_set_speed_xmii(struct net_device *dev,
1633 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1635 struct rtl8169_private *tp = netdev_priv(dev);
1636 int giga_ctrl, bmcr;
1639 rtl_writephy(tp, 0x1f, 0x0000);
1641 if (autoneg == AUTONEG_ENABLE) {
1644 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1645 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1646 ADVERTISE_100HALF | ADVERTISE_100FULL);
1648 if (adv & ADVERTISED_10baseT_Half)
1649 auto_nego |= ADVERTISE_10HALF;
1650 if (adv & ADVERTISED_10baseT_Full)
1651 auto_nego |= ADVERTISE_10FULL;
1652 if (adv & ADVERTISED_100baseT_Half)
1653 auto_nego |= ADVERTISE_100HALF;
1654 if (adv & ADVERTISED_100baseT_Full)
1655 auto_nego |= ADVERTISE_100FULL;
1657 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1659 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1660 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1662 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1663 if (tp->mii.supports_gmii) {
1664 if (adv & ADVERTISED_1000baseT_Half)
1665 giga_ctrl |= ADVERTISE_1000HALF;
1666 if (adv & ADVERTISED_1000baseT_Full)
1667 giga_ctrl |= ADVERTISE_1000FULL;
1668 } else if (adv & (ADVERTISED_1000baseT_Half |
1669 ADVERTISED_1000baseT_Full)) {
1670 netif_info(tp, link, dev,
1671 "PHY does not support 1000Mbps\n");
1675 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1677 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1678 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1682 if (speed == SPEED_10)
1684 else if (speed == SPEED_100)
1685 bmcr = BMCR_SPEED100;
1689 if (duplex == DUPLEX_FULL)
1690 bmcr |= BMCR_FULLDPLX;
1693 rtl_writephy(tp, MII_BMCR, bmcr);
1695 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1696 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1697 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1698 rtl_writephy(tp, 0x17, 0x2138);
1699 rtl_writephy(tp, 0x0e, 0x0260);
1701 rtl_writephy(tp, 0x17, 0x2108);
1702 rtl_writephy(tp, 0x0e, 0x0000);
1711 static int rtl8169_set_speed(struct net_device *dev,
1712 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1714 struct rtl8169_private *tp = netdev_priv(dev);
1717 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1721 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1722 (advertising & ADVERTISED_1000baseT_Full)) {
1723 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1729 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1731 struct rtl8169_private *tp = netdev_priv(dev);
1734 del_timer_sync(&tp->timer);
1737 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1738 cmd->duplex, cmd->advertising);
1739 rtl_unlock_work(tp);
1744 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1745 netdev_features_t features)
1747 struct rtl8169_private *tp = netdev_priv(dev);
1749 if (dev->mtu > TD_MSS_MAX)
1750 features &= ~NETIF_F_ALL_TSO;
1752 if (dev->mtu > JUMBO_1K &&
1753 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1754 features &= ~NETIF_F_IP_CSUM;
1759 static void __rtl8169_set_features(struct net_device *dev,
1760 netdev_features_t features)
1762 struct rtl8169_private *tp = netdev_priv(dev);
1763 netdev_features_t changed = features ^ dev->features;
1764 void __iomem *ioaddr = tp->mmio_addr;
1766 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1769 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1770 if (features & NETIF_F_RXCSUM)
1771 tp->cp_cmd |= RxChkSum;
1773 tp->cp_cmd &= ~RxChkSum;
1775 if (dev->features & NETIF_F_HW_VLAN_RX)
1776 tp->cp_cmd |= RxVlan;
1778 tp->cp_cmd &= ~RxVlan;
1780 RTL_W16(CPlusCmd, tp->cp_cmd);
1783 if (changed & NETIF_F_RXALL) {
1784 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1785 if (features & NETIF_F_RXALL)
1786 tmp |= (AcceptErr | AcceptRunt);
1787 RTL_W32(RxConfig, tmp);
1791 static int rtl8169_set_features(struct net_device *dev,
1792 netdev_features_t features)
1794 struct rtl8169_private *tp = netdev_priv(dev);
1797 __rtl8169_set_features(dev, features);
1798 rtl_unlock_work(tp);
1804 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1806 return (vlan_tx_tag_present(skb)) ?
1807 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1810 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1812 u32 opts2 = le32_to_cpu(desc->opts2);
1814 if (opts2 & RxVlanTag)
1815 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1818 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1820 struct rtl8169_private *tp = netdev_priv(dev);
1821 void __iomem *ioaddr = tp->mmio_addr;
1825 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1826 cmd->port = PORT_FIBRE;
1827 cmd->transceiver = XCVR_INTERNAL;
1829 status = RTL_R32(TBICSR);
1830 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1831 cmd->autoneg = !!(status & TBINwEnable);
1833 ethtool_cmd_speed_set(cmd, SPEED_1000);
1834 cmd->duplex = DUPLEX_FULL; /* Always set */
1839 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1841 struct rtl8169_private *tp = netdev_priv(dev);
1843 return mii_ethtool_gset(&tp->mii, cmd);
1846 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1848 struct rtl8169_private *tp = netdev_priv(dev);
1852 rc = tp->get_settings(dev, cmd);
1853 rtl_unlock_work(tp);
1858 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1861 struct rtl8169_private *tp = netdev_priv(dev);
1863 if (regs->len > R8169_REGS_SIZE)
1864 regs->len = R8169_REGS_SIZE;
1867 memcpy_fromio(p, tp->mmio_addr, regs->len);
1868 rtl_unlock_work(tp);
1871 static u32 rtl8169_get_msglevel(struct net_device *dev)
1873 struct rtl8169_private *tp = netdev_priv(dev);
1875 return tp->msg_enable;
1878 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1880 struct rtl8169_private *tp = netdev_priv(dev);
1882 tp->msg_enable = value;
1885 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1892 "tx_single_collisions",
1893 "tx_multi_collisions",
1901 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1905 return ARRAY_SIZE(rtl8169_gstrings);
1911 DECLARE_RTL_COND(rtl_counters_cond)
1913 void __iomem *ioaddr = tp->mmio_addr;
1915 return RTL_R32(CounterAddrLow) & CounterDump;
1918 static void rtl8169_update_counters(struct net_device *dev)
1920 struct rtl8169_private *tp = netdev_priv(dev);
1921 void __iomem *ioaddr = tp->mmio_addr;
1922 struct device *d = &tp->pci_dev->dev;
1923 struct rtl8169_counters *counters;
1928 * Some chips are unable to dump tally counters when the receiver
1931 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1934 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1938 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1939 cmd = (u64)paddr & DMA_BIT_MASK(32);
1940 RTL_W32(CounterAddrLow, cmd);
1941 RTL_W32(CounterAddrLow, cmd | CounterDump);
1943 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1944 memcpy(&tp->counters, counters, sizeof(*counters));
1946 RTL_W32(CounterAddrLow, 0);
1947 RTL_W32(CounterAddrHigh, 0);
1949 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1952 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1953 struct ethtool_stats *stats, u64 *data)
1955 struct rtl8169_private *tp = netdev_priv(dev);
1959 rtl8169_update_counters(dev);
1961 data[0] = le64_to_cpu(tp->counters.tx_packets);
1962 data[1] = le64_to_cpu(tp->counters.rx_packets);
1963 data[2] = le64_to_cpu(tp->counters.tx_errors);
1964 data[3] = le32_to_cpu(tp->counters.rx_errors);
1965 data[4] = le16_to_cpu(tp->counters.rx_missed);
1966 data[5] = le16_to_cpu(tp->counters.align_errors);
1967 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1968 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1969 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1970 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1971 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1972 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1973 data[12] = le16_to_cpu(tp->counters.tx_underun);
1976 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1980 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1985 static const struct ethtool_ops rtl8169_ethtool_ops = {
1986 .get_drvinfo = rtl8169_get_drvinfo,
1987 .get_regs_len = rtl8169_get_regs_len,
1988 .get_link = ethtool_op_get_link,
1989 .get_settings = rtl8169_get_settings,
1990 .set_settings = rtl8169_set_settings,
1991 .get_msglevel = rtl8169_get_msglevel,
1992 .set_msglevel = rtl8169_set_msglevel,
1993 .get_regs = rtl8169_get_regs,
1994 .get_wol = rtl8169_get_wol,
1995 .set_wol = rtl8169_set_wol,
1996 .get_strings = rtl8169_get_strings,
1997 .get_sset_count = rtl8169_get_sset_count,
1998 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1999 .get_ts_info = ethtool_op_get_ts_info,
2002 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2003 struct net_device *dev, u8 default_version)
2005 void __iomem *ioaddr = tp->mmio_addr;
2007 * The driver currently handles the 8168Bf and the 8168Be identically
2008 * but they can be identified more specifically through the test below
2011 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2013 * Same thing for the 8101Eb and the 8101Ec:
2015 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2017 static const struct rtl_mac_info {
2023 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2024 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2027 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2028 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2029 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2032 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2033 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2034 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2035 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2038 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2039 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2040 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2042 /* 8168DP family. */
2043 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2044 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2045 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2048 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
2049 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2050 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2051 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2052 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2053 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2054 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2055 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
2056 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2059 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2060 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2061 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2062 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2065 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2066 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2067 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2068 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
2069 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2070 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2071 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2072 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2073 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2074 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2075 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2076 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2077 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2078 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2079 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2080 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2081 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2082 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2083 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2084 /* FIXME: where did these entries come from ? -- FR */
2085 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2086 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2089 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2090 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2091 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2092 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2093 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2094 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2097 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2099 const struct rtl_mac_info *p = mac_info;
2102 reg = RTL_R32(TxConfig);
2103 while ((reg & p->mask) != p->val)
2105 tp->mac_version = p->mac_version;
2107 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2108 netif_notice(tp, probe, dev,
2109 "unknown MAC, using family default\n");
2110 tp->mac_version = default_version;
2114 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2116 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2124 static void rtl_writephy_batch(struct rtl8169_private *tp,
2125 const struct phy_reg *regs, int len)
2128 rtl_writephy(tp, regs->reg, regs->val);
2133 #define PHY_READ 0x00000000
2134 #define PHY_DATA_OR 0x10000000
2135 #define PHY_DATA_AND 0x20000000
2136 #define PHY_BJMPN 0x30000000
2137 #define PHY_READ_EFUSE 0x40000000
2138 #define PHY_READ_MAC_BYTE 0x50000000
2139 #define PHY_WRITE_MAC_BYTE 0x60000000
2140 #define PHY_CLEAR_READCOUNT 0x70000000
2141 #define PHY_WRITE 0x80000000
2142 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2143 #define PHY_COMP_EQ_SKIPN 0xa0000000
2144 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2145 #define PHY_WRITE_PREVIOUS 0xc0000000
2146 #define PHY_SKIPN 0xd0000000
2147 #define PHY_DELAY_MS 0xe0000000
2148 #define PHY_WRITE_ERI_WORD 0xf0000000
2152 char version[RTL_VER_SIZE];
2158 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2160 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2162 const struct firmware *fw = rtl_fw->fw;
2163 struct fw_info *fw_info = (struct fw_info *)fw->data;
2164 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2165 char *version = rtl_fw->version;
2168 if (fw->size < FW_OPCODE_SIZE)
2171 if (!fw_info->magic) {
2172 size_t i, size, start;
2175 if (fw->size < sizeof(*fw_info))
2178 for (i = 0; i < fw->size; i++)
2179 checksum += fw->data[i];
2183 start = le32_to_cpu(fw_info->fw_start);
2184 if (start > fw->size)
2187 size = le32_to_cpu(fw_info->fw_len);
2188 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2191 memcpy(version, fw_info->version, RTL_VER_SIZE);
2193 pa->code = (__le32 *)(fw->data + start);
2196 if (fw->size % FW_OPCODE_SIZE)
2199 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2201 pa->code = (__le32 *)fw->data;
2202 pa->size = fw->size / FW_OPCODE_SIZE;
2204 version[RTL_VER_SIZE - 1] = 0;
2211 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2212 struct rtl_fw_phy_action *pa)
2217 for (index = 0; index < pa->size; index++) {
2218 u32 action = le32_to_cpu(pa->code[index]);
2219 u32 regno = (action & 0x0fff0000) >> 16;
2221 switch(action & 0xf0000000) {
2225 case PHY_READ_EFUSE:
2226 case PHY_CLEAR_READCOUNT:
2228 case PHY_WRITE_PREVIOUS:
2233 if (regno > index) {
2234 netif_err(tp, ifup, tp->dev,
2235 "Out of range of firmware\n");
2239 case PHY_READCOUNT_EQ_SKIP:
2240 if (index + 2 >= pa->size) {
2241 netif_err(tp, ifup, tp->dev,
2242 "Out of range of firmware\n");
2246 case PHY_COMP_EQ_SKIPN:
2247 case PHY_COMP_NEQ_SKIPN:
2249 if (index + 1 + regno >= pa->size) {
2250 netif_err(tp, ifup, tp->dev,
2251 "Out of range of firmware\n");
2256 case PHY_READ_MAC_BYTE:
2257 case PHY_WRITE_MAC_BYTE:
2258 case PHY_WRITE_ERI_WORD:
2260 netif_err(tp, ifup, tp->dev,
2261 "Invalid action 0x%08x\n", action);
2270 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2272 struct net_device *dev = tp->dev;
2275 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2276 netif_err(tp, ifup, dev, "invalid firwmare\n");
2280 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2286 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2288 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2292 predata = count = 0;
2294 for (index = 0; index < pa->size; ) {
2295 u32 action = le32_to_cpu(pa->code[index]);
2296 u32 data = action & 0x0000ffff;
2297 u32 regno = (action & 0x0fff0000) >> 16;
2302 switch(action & 0xf0000000) {
2304 predata = rtl_readphy(tp, regno);
2319 case PHY_READ_EFUSE:
2320 predata = rtl8168d_efuse_read(tp, regno);
2323 case PHY_CLEAR_READCOUNT:
2328 rtl_writephy(tp, regno, data);
2331 case PHY_READCOUNT_EQ_SKIP:
2332 index += (count == data) ? 2 : 1;
2334 case PHY_COMP_EQ_SKIPN:
2335 if (predata == data)
2339 case PHY_COMP_NEQ_SKIPN:
2340 if (predata != data)
2344 case PHY_WRITE_PREVIOUS:
2345 rtl_writephy(tp, regno, predata);
2356 case PHY_READ_MAC_BYTE:
2357 case PHY_WRITE_MAC_BYTE:
2358 case PHY_WRITE_ERI_WORD:
2365 static void rtl_release_firmware(struct rtl8169_private *tp)
2367 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2368 release_firmware(tp->rtl_fw->fw);
2371 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2374 static void rtl_apply_firmware(struct rtl8169_private *tp)
2376 struct rtl_fw *rtl_fw = tp->rtl_fw;
2378 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2379 if (!IS_ERR_OR_NULL(rtl_fw))
2380 rtl_phy_write_fw(tp, rtl_fw);
2383 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2385 if (rtl_readphy(tp, reg) != val)
2386 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2388 rtl_apply_firmware(tp);
2391 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2393 static const struct phy_reg phy_reg_init[] = {
2455 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2458 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2460 static const struct phy_reg phy_reg_init[] = {
2466 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2469 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2471 struct pci_dev *pdev = tp->pci_dev;
2473 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2474 (pdev->subsystem_device != 0xe000))
2477 rtl_writephy(tp, 0x1f, 0x0001);
2478 rtl_writephy(tp, 0x10, 0xf01b);
2479 rtl_writephy(tp, 0x1f, 0x0000);
2482 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2484 static const struct phy_reg phy_reg_init[] = {
2524 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2526 rtl8169scd_hw_phy_config_quirk(tp);
2529 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2531 static const struct phy_reg phy_reg_init[] = {
2579 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2582 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2584 static const struct phy_reg phy_reg_init[] = {
2589 rtl_writephy(tp, 0x1f, 0x0001);
2590 rtl_patchphy(tp, 0x16, 1 << 0);
2592 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2595 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2597 static const struct phy_reg phy_reg_init[] = {
2603 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2606 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2608 static const struct phy_reg phy_reg_init[] = {
2616 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2619 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2621 static const struct phy_reg phy_reg_init[] = {
2627 rtl_writephy(tp, 0x1f, 0x0000);
2628 rtl_patchphy(tp, 0x14, 1 << 5);
2629 rtl_patchphy(tp, 0x0d, 1 << 5);
2631 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2634 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2636 static const struct phy_reg phy_reg_init[] = {
2656 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2658 rtl_patchphy(tp, 0x14, 1 << 5);
2659 rtl_patchphy(tp, 0x0d, 1 << 5);
2660 rtl_writephy(tp, 0x1f, 0x0000);
2663 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2665 static const struct phy_reg phy_reg_init[] = {
2683 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2685 rtl_patchphy(tp, 0x16, 1 << 0);
2686 rtl_patchphy(tp, 0x14, 1 << 5);
2687 rtl_patchphy(tp, 0x0d, 1 << 5);
2688 rtl_writephy(tp, 0x1f, 0x0000);
2691 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2693 static const struct phy_reg phy_reg_init[] = {
2705 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2707 rtl_patchphy(tp, 0x16, 1 << 0);
2708 rtl_patchphy(tp, 0x14, 1 << 5);
2709 rtl_patchphy(tp, 0x0d, 1 << 5);
2710 rtl_writephy(tp, 0x1f, 0x0000);
2713 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2715 rtl8168c_3_hw_phy_config(tp);
2718 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2720 static const struct phy_reg phy_reg_init_0[] = {
2721 /* Channel Estimation */
2742 * Enhance line driver power
2751 * Can not link to 1Gbps with bad cable
2752 * Decrease SNR threshold form 21.07dB to 19.04dB
2761 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2765 * Fine Tune Switching regulator parameter
2767 rtl_writephy(tp, 0x1f, 0x0002);
2768 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2769 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2771 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2772 static const struct phy_reg phy_reg_init[] = {
2782 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2784 val = rtl_readphy(tp, 0x0d);
2786 if ((val & 0x00ff) != 0x006c) {
2787 static const u32 set[] = {
2788 0x0065, 0x0066, 0x0067, 0x0068,
2789 0x0069, 0x006a, 0x006b, 0x006c
2793 rtl_writephy(tp, 0x1f, 0x0002);
2796 for (i = 0; i < ARRAY_SIZE(set); i++)
2797 rtl_writephy(tp, 0x0d, val | set[i]);
2800 static const struct phy_reg phy_reg_init[] = {
2808 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2811 /* RSET couple improve */
2812 rtl_writephy(tp, 0x1f, 0x0002);
2813 rtl_patchphy(tp, 0x0d, 0x0300);
2814 rtl_patchphy(tp, 0x0f, 0x0010);
2816 /* Fine tune PLL performance */
2817 rtl_writephy(tp, 0x1f, 0x0002);
2818 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2819 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2821 rtl_writephy(tp, 0x1f, 0x0005);
2822 rtl_writephy(tp, 0x05, 0x001b);
2824 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2826 rtl_writephy(tp, 0x1f, 0x0000);
2829 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2831 static const struct phy_reg phy_reg_init_0[] = {
2832 /* Channel Estimation */
2853 * Enhance line driver power
2862 * Can not link to 1Gbps with bad cable
2863 * Decrease SNR threshold form 21.07dB to 19.04dB
2872 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2874 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2875 static const struct phy_reg phy_reg_init[] = {
2886 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2888 val = rtl_readphy(tp, 0x0d);
2889 if ((val & 0x00ff) != 0x006c) {
2890 static const u32 set[] = {
2891 0x0065, 0x0066, 0x0067, 0x0068,
2892 0x0069, 0x006a, 0x006b, 0x006c
2896 rtl_writephy(tp, 0x1f, 0x0002);
2899 for (i = 0; i < ARRAY_SIZE(set); i++)
2900 rtl_writephy(tp, 0x0d, val | set[i]);
2903 static const struct phy_reg phy_reg_init[] = {
2911 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2914 /* Fine tune PLL performance */
2915 rtl_writephy(tp, 0x1f, 0x0002);
2916 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2917 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2919 /* Switching regulator Slew rate */
2920 rtl_writephy(tp, 0x1f, 0x0002);
2921 rtl_patchphy(tp, 0x0f, 0x0017);
2923 rtl_writephy(tp, 0x1f, 0x0005);
2924 rtl_writephy(tp, 0x05, 0x001b);
2926 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2928 rtl_writephy(tp, 0x1f, 0x0000);
2931 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2933 static const struct phy_reg phy_reg_init[] = {
2989 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2992 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2994 static const struct phy_reg phy_reg_init[] = {
3004 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3005 rtl_patchphy(tp, 0x0d, 1 << 5);
3008 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3010 static const struct phy_reg phy_reg_init[] = {
3011 /* Enable Delay cap */
3017 /* Channel estimation fine tune */
3026 /* Update PFM & 10M TX idle timer */
3038 rtl_apply_firmware(tp);
3040 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3042 /* DCO enable for 10M IDLE Power */
3043 rtl_writephy(tp, 0x1f, 0x0007);
3044 rtl_writephy(tp, 0x1e, 0x0023);
3045 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3046 rtl_writephy(tp, 0x1f, 0x0000);
3048 /* For impedance matching */
3049 rtl_writephy(tp, 0x1f, 0x0002);
3050 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
3051 rtl_writephy(tp, 0x1f, 0x0000);
3053 /* PHY auto speed down */
3054 rtl_writephy(tp, 0x1f, 0x0007);
3055 rtl_writephy(tp, 0x1e, 0x002d);
3056 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3057 rtl_writephy(tp, 0x1f, 0x0000);
3058 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3060 rtl_writephy(tp, 0x1f, 0x0005);
3061 rtl_writephy(tp, 0x05, 0x8b86);
3062 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3063 rtl_writephy(tp, 0x1f, 0x0000);
3065 rtl_writephy(tp, 0x1f, 0x0005);
3066 rtl_writephy(tp, 0x05, 0x8b85);
3067 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3068 rtl_writephy(tp, 0x1f, 0x0007);
3069 rtl_writephy(tp, 0x1e, 0x0020);
3070 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3071 rtl_writephy(tp, 0x1f, 0x0006);
3072 rtl_writephy(tp, 0x00, 0x5a00);
3073 rtl_writephy(tp, 0x1f, 0x0000);
3074 rtl_writephy(tp, 0x0d, 0x0007);
3075 rtl_writephy(tp, 0x0e, 0x003c);
3076 rtl_writephy(tp, 0x0d, 0x4007);
3077 rtl_writephy(tp, 0x0e, 0x0000);
3078 rtl_writephy(tp, 0x0d, 0x0000);
3081 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3084 addr[0] | (addr[1] << 8),
3085 addr[2] | (addr[3] << 8),
3086 addr[4] | (addr[5] << 8)
3088 const struct exgmac_reg e[] = {
3089 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3090 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3091 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3092 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3095 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3098 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3100 static const struct phy_reg phy_reg_init[] = {
3101 /* Enable Delay cap */
3110 /* Channel estimation fine tune */
3127 rtl_apply_firmware(tp);
3129 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3131 /* For 4-corner performance improve */
3132 rtl_writephy(tp, 0x1f, 0x0005);
3133 rtl_writephy(tp, 0x05, 0x8b80);
3134 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3135 rtl_writephy(tp, 0x1f, 0x0000);
3137 /* PHY auto speed down */
3138 rtl_writephy(tp, 0x1f, 0x0004);
3139 rtl_writephy(tp, 0x1f, 0x0007);
3140 rtl_writephy(tp, 0x1e, 0x002d);
3141 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3142 rtl_writephy(tp, 0x1f, 0x0002);
3143 rtl_writephy(tp, 0x1f, 0x0000);
3144 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3146 /* improve 10M EEE waveform */
3147 rtl_writephy(tp, 0x1f, 0x0005);
3148 rtl_writephy(tp, 0x05, 0x8b86);
3149 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3150 rtl_writephy(tp, 0x1f, 0x0000);
3152 /* Improve 2-pair detection performance */
3153 rtl_writephy(tp, 0x1f, 0x0005);
3154 rtl_writephy(tp, 0x05, 0x8b85);
3155 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3156 rtl_writephy(tp, 0x1f, 0x0000);
3159 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
3160 rtl_writephy(tp, 0x1f, 0x0005);
3161 rtl_writephy(tp, 0x05, 0x8b85);
3162 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3163 rtl_writephy(tp, 0x1f, 0x0004);
3164 rtl_writephy(tp, 0x1f, 0x0007);
3165 rtl_writephy(tp, 0x1e, 0x0020);
3166 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3167 rtl_writephy(tp, 0x1f, 0x0002);
3168 rtl_writephy(tp, 0x1f, 0x0000);
3169 rtl_writephy(tp, 0x0d, 0x0007);
3170 rtl_writephy(tp, 0x0e, 0x003c);
3171 rtl_writephy(tp, 0x0d, 0x4007);
3172 rtl_writephy(tp, 0x0e, 0x0000);
3173 rtl_writephy(tp, 0x0d, 0x0000);
3176 rtl_writephy(tp, 0x1f, 0x0003);
3177 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3178 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3179 rtl_writephy(tp, 0x1f, 0x0000);
3181 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3182 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3185 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3187 /* For 4-corner performance improve */
3188 rtl_writephy(tp, 0x1f, 0x0005);
3189 rtl_writephy(tp, 0x05, 0x8b80);
3190 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3191 rtl_writephy(tp, 0x1f, 0x0000);
3193 /* PHY auto speed down */
3194 rtl_writephy(tp, 0x1f, 0x0007);
3195 rtl_writephy(tp, 0x1e, 0x002d);
3196 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3197 rtl_writephy(tp, 0x1f, 0x0000);
3198 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3200 /* Improve 10M EEE waveform */
3201 rtl_writephy(tp, 0x1f, 0x0005);
3202 rtl_writephy(tp, 0x05, 0x8b86);
3203 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3204 rtl_writephy(tp, 0x1f, 0x0000);
3207 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3209 static const struct phy_reg phy_reg_init[] = {
3210 /* Channel estimation fine tune */
3215 /* Modify green table for giga & fnet */
3232 /* Modify green table for 10M */
3238 /* Disable hiimpedance detection (RTCT) */
3244 rtl_apply_firmware(tp);
3246 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3248 rtl8168f_hw_phy_config(tp);
3250 /* Improve 2-pair detection performance */
3251 rtl_writephy(tp, 0x1f, 0x0005);
3252 rtl_writephy(tp, 0x05, 0x8b85);
3253 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3254 rtl_writephy(tp, 0x1f, 0x0000);
3257 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3259 rtl_apply_firmware(tp);
3261 rtl8168f_hw_phy_config(tp);
3264 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3266 static const struct phy_reg phy_reg_init[] = {
3267 /* Channel estimation fine tune */
3272 /* Modify green table for giga & fnet */
3289 /* Modify green table for 10M */
3295 /* Disable hiimpedance detection (RTCT) */
3302 rtl_apply_firmware(tp);
3304 rtl8168f_hw_phy_config(tp);
3306 /* Improve 2-pair detection performance */
3307 rtl_writephy(tp, 0x1f, 0x0005);
3308 rtl_writephy(tp, 0x05, 0x8b85);
3309 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3310 rtl_writephy(tp, 0x1f, 0x0000);
3312 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3314 /* Modify green table for giga */
3315 rtl_writephy(tp, 0x1f, 0x0005);
3316 rtl_writephy(tp, 0x05, 0x8b54);
3317 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3318 rtl_writephy(tp, 0x05, 0x8b5d);
3319 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3320 rtl_writephy(tp, 0x05, 0x8a7c);
3321 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3322 rtl_writephy(tp, 0x05, 0x8a7f);
3323 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3324 rtl_writephy(tp, 0x05, 0x8a82);
3325 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3326 rtl_writephy(tp, 0x05, 0x8a85);
3327 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3328 rtl_writephy(tp, 0x05, 0x8a88);
3329 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3330 rtl_writephy(tp, 0x1f, 0x0000);
3332 /* uc same-seed solution */
3333 rtl_writephy(tp, 0x1f, 0x0005);
3334 rtl_writephy(tp, 0x05, 0x8b85);
3335 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3336 rtl_writephy(tp, 0x1f, 0x0000);
3339 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3340 rtl_writephy(tp, 0x1f, 0x0005);
3341 rtl_writephy(tp, 0x05, 0x8b85);
3342 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3343 rtl_writephy(tp, 0x1f, 0x0004);
3344 rtl_writephy(tp, 0x1f, 0x0007);
3345 rtl_writephy(tp, 0x1e, 0x0020);
3346 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3347 rtl_writephy(tp, 0x1f, 0x0000);
3348 rtl_writephy(tp, 0x0d, 0x0007);
3349 rtl_writephy(tp, 0x0e, 0x003c);
3350 rtl_writephy(tp, 0x0d, 0x4007);
3351 rtl_writephy(tp, 0x0e, 0x0000);
3352 rtl_writephy(tp, 0x0d, 0x0000);
3355 rtl_writephy(tp, 0x1f, 0x0003);
3356 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3357 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3358 rtl_writephy(tp, 0x1f, 0x0000);
3361 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3363 rtl_apply_firmware(tp);
3365 rtl_writephy(tp, 0x1f, 0x0a46);
3366 if (rtl_readphy(tp, 0x10) & 0x0100) {
3367 rtl_writephy(tp, 0x1f, 0x0bcc);
3368 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3370 rtl_writephy(tp, 0x1f, 0x0bcc);
3371 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3374 rtl_writephy(tp, 0x1f, 0x0a46);
3375 if (rtl_readphy(tp, 0x13) & 0x0100) {
3376 rtl_writephy(tp, 0x1f, 0x0c41);
3377 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3379 rtl_writephy(tp, 0x1f, 0x0bcc);
3380 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x0002);
3383 /* Enable PHY auto speed down */
3384 rtl_writephy(tp, 0x1f, 0x0a44);
3385 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
3387 /* EEE auto-fallback function */
3388 rtl_writephy(tp, 0x1f, 0x0a4b);
3389 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
3391 /* Enable UC LPF tune function */
3392 rtl_writephy(tp, 0x1f, 0x0a43);
3393 rtl_writephy(tp, 0x13, 0x8012);
3394 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3396 rtl_writephy(tp, 0x1f, 0x0c42);
3397 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3399 rtl_writephy(tp, 0x1f, 0x0000);
3402 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3404 static const struct phy_reg phy_reg_init[] = {
3411 rtl_writephy(tp, 0x1f, 0x0000);
3412 rtl_patchphy(tp, 0x11, 1 << 12);
3413 rtl_patchphy(tp, 0x19, 1 << 13);
3414 rtl_patchphy(tp, 0x10, 1 << 15);
3416 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3419 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3421 static const struct phy_reg phy_reg_init[] = {
3435 /* Disable ALDPS before ram code */
3436 rtl_writephy(tp, 0x1f, 0x0000);
3437 rtl_writephy(tp, 0x18, 0x0310);
3440 rtl_apply_firmware(tp);
3442 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3445 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3447 /* Disable ALDPS before setting firmware */
3448 rtl_writephy(tp, 0x1f, 0x0000);
3449 rtl_writephy(tp, 0x18, 0x0310);
3452 rtl_apply_firmware(tp);
3455 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3456 rtl_writephy(tp, 0x1f, 0x0004);
3457 rtl_writephy(tp, 0x10, 0x401f);
3458 rtl_writephy(tp, 0x19, 0x7030);
3459 rtl_writephy(tp, 0x1f, 0x0000);
3462 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3464 static const struct phy_reg phy_reg_init[] = {
3471 /* Disable ALDPS before ram code */
3472 rtl_writephy(tp, 0x1f, 0x0000);
3473 rtl_writephy(tp, 0x18, 0x0310);
3476 rtl_apply_firmware(tp);
3478 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3479 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3481 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3484 static void rtl_hw_phy_config(struct net_device *dev)
3486 struct rtl8169_private *tp = netdev_priv(dev);
3488 rtl8169_print_mac_version(tp);
3490 switch (tp->mac_version) {
3491 case RTL_GIGA_MAC_VER_01:
3493 case RTL_GIGA_MAC_VER_02:
3494 case RTL_GIGA_MAC_VER_03:
3495 rtl8169s_hw_phy_config(tp);
3497 case RTL_GIGA_MAC_VER_04:
3498 rtl8169sb_hw_phy_config(tp);
3500 case RTL_GIGA_MAC_VER_05:
3501 rtl8169scd_hw_phy_config(tp);
3503 case RTL_GIGA_MAC_VER_06:
3504 rtl8169sce_hw_phy_config(tp);
3506 case RTL_GIGA_MAC_VER_07:
3507 case RTL_GIGA_MAC_VER_08:
3508 case RTL_GIGA_MAC_VER_09:
3509 rtl8102e_hw_phy_config(tp);
3511 case RTL_GIGA_MAC_VER_11:
3512 rtl8168bb_hw_phy_config(tp);
3514 case RTL_GIGA_MAC_VER_12:
3515 rtl8168bef_hw_phy_config(tp);
3517 case RTL_GIGA_MAC_VER_17:
3518 rtl8168bef_hw_phy_config(tp);
3520 case RTL_GIGA_MAC_VER_18:
3521 rtl8168cp_1_hw_phy_config(tp);
3523 case RTL_GIGA_MAC_VER_19:
3524 rtl8168c_1_hw_phy_config(tp);
3526 case RTL_GIGA_MAC_VER_20:
3527 rtl8168c_2_hw_phy_config(tp);
3529 case RTL_GIGA_MAC_VER_21:
3530 rtl8168c_3_hw_phy_config(tp);
3532 case RTL_GIGA_MAC_VER_22:
3533 rtl8168c_4_hw_phy_config(tp);
3535 case RTL_GIGA_MAC_VER_23:
3536 case RTL_GIGA_MAC_VER_24:
3537 rtl8168cp_2_hw_phy_config(tp);
3539 case RTL_GIGA_MAC_VER_25:
3540 rtl8168d_1_hw_phy_config(tp);
3542 case RTL_GIGA_MAC_VER_26:
3543 rtl8168d_2_hw_phy_config(tp);
3545 case RTL_GIGA_MAC_VER_27:
3546 rtl8168d_3_hw_phy_config(tp);
3548 case RTL_GIGA_MAC_VER_28:
3549 rtl8168d_4_hw_phy_config(tp);
3551 case RTL_GIGA_MAC_VER_29:
3552 case RTL_GIGA_MAC_VER_30:
3553 rtl8105e_hw_phy_config(tp);
3555 case RTL_GIGA_MAC_VER_31:
3558 case RTL_GIGA_MAC_VER_32:
3559 case RTL_GIGA_MAC_VER_33:
3560 rtl8168e_1_hw_phy_config(tp);
3562 case RTL_GIGA_MAC_VER_34:
3563 rtl8168e_2_hw_phy_config(tp);
3565 case RTL_GIGA_MAC_VER_35:
3566 rtl8168f_1_hw_phy_config(tp);
3568 case RTL_GIGA_MAC_VER_36:
3569 rtl8168f_2_hw_phy_config(tp);
3572 case RTL_GIGA_MAC_VER_37:
3573 rtl8402_hw_phy_config(tp);
3576 case RTL_GIGA_MAC_VER_38:
3577 rtl8411_hw_phy_config(tp);
3580 case RTL_GIGA_MAC_VER_39:
3581 rtl8106e_hw_phy_config(tp);
3584 case RTL_GIGA_MAC_VER_40:
3585 rtl8168g_1_hw_phy_config(tp);
3588 case RTL_GIGA_MAC_VER_41:
3594 static void rtl_phy_work(struct rtl8169_private *tp)
3596 struct timer_list *timer = &tp->timer;
3597 void __iomem *ioaddr = tp->mmio_addr;
3598 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3600 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
3602 if (tp->phy_reset_pending(tp)) {
3604 * A busy loop could burn quite a few cycles on nowadays CPU.
3605 * Let's delay the execution of the timer for a few ticks.
3611 if (tp->link_ok(ioaddr))
3614 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
3616 tp->phy_reset_enable(tp);
3619 mod_timer(timer, jiffies + timeout);
3622 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3624 if (!test_and_set_bit(flag, tp->wk.flags))
3625 schedule_work(&tp->wk.work);
3628 static void rtl8169_phy_timer(unsigned long __opaque)
3630 struct net_device *dev = (struct net_device *)__opaque;
3631 struct rtl8169_private *tp = netdev_priv(dev);
3633 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
3636 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3637 void __iomem *ioaddr)
3640 pci_release_regions(pdev);
3641 pci_clear_mwi(pdev);
3642 pci_disable_device(pdev);
3646 DECLARE_RTL_COND(rtl_phy_reset_cond)
3648 return tp->phy_reset_pending(tp);
3651 static void rtl8169_phy_reset(struct net_device *dev,
3652 struct rtl8169_private *tp)
3654 tp->phy_reset_enable(tp);
3655 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
3658 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3660 void __iomem *ioaddr = tp->mmio_addr;
3662 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3663 (RTL_R8(PHYstatus) & TBI_Enable);
3666 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3668 void __iomem *ioaddr = tp->mmio_addr;
3670 rtl_hw_phy_config(dev);
3672 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3673 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3677 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3679 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3680 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3682 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
3683 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3685 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
3686 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
3689 rtl8169_phy_reset(dev, tp);
3691 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3692 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3693 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3694 (tp->mii.supports_gmii ?
3695 ADVERTISED_1000baseT_Half |
3696 ADVERTISED_1000baseT_Full : 0));
3698 if (rtl_tbi_enabled(tp))
3699 netif_info(tp, link, dev, "TBI auto-negotiating\n");
3702 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3704 void __iomem *ioaddr = tp->mmio_addr;
3708 RTL_W8(Cfg9346, Cfg9346_Unlock);
3710 RTL_W32(MAC4, addr[4] | addr[5] << 8);
3713 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3716 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3717 rtl_rar_exgmac_set(tp, addr);
3719 RTL_W8(Cfg9346, Cfg9346_Lock);
3721 rtl_unlock_work(tp);
3724 static int rtl_set_mac_address(struct net_device *dev, void *p)
3726 struct rtl8169_private *tp = netdev_priv(dev);
3727 struct sockaddr *addr = p;
3729 if (!is_valid_ether_addr(addr->sa_data))
3730 return -EADDRNOTAVAIL;
3732 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3734 rtl_rar_set(tp, dev->dev_addr);
3739 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3741 struct rtl8169_private *tp = netdev_priv(dev);
3742 struct mii_ioctl_data *data = if_mii(ifr);
3744 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3747 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3748 struct mii_ioctl_data *data, int cmd)
3752 data->phy_id = 32; /* Internal PHY */
3756 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3760 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3766 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3771 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3773 if (tp->features & RTL_FEATURE_MSI) {
3774 pci_disable_msi(pdev);
3775 tp->features &= ~RTL_FEATURE_MSI;
3779 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
3781 struct mdio_ops *ops = &tp->mdio_ops;
3783 switch (tp->mac_version) {
3784 case RTL_GIGA_MAC_VER_27:
3785 ops->write = r8168dp_1_mdio_write;
3786 ops->read = r8168dp_1_mdio_read;
3788 case RTL_GIGA_MAC_VER_28:
3789 case RTL_GIGA_MAC_VER_31:
3790 ops->write = r8168dp_2_mdio_write;
3791 ops->read = r8168dp_2_mdio_read;
3793 case RTL_GIGA_MAC_VER_40:
3794 case RTL_GIGA_MAC_VER_41:
3795 ops->write = r8168g_mdio_write;
3796 ops->read = r8168g_mdio_read;
3799 ops->write = r8169_mdio_write;
3800 ops->read = r8169_mdio_read;
3805 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3807 void __iomem *ioaddr = tp->mmio_addr;
3809 switch (tp->mac_version) {
3810 case RTL_GIGA_MAC_VER_25:
3811 case RTL_GIGA_MAC_VER_26:
3812 case RTL_GIGA_MAC_VER_29:
3813 case RTL_GIGA_MAC_VER_30:
3814 case RTL_GIGA_MAC_VER_32:
3815 case RTL_GIGA_MAC_VER_33:
3816 case RTL_GIGA_MAC_VER_34:
3817 case RTL_GIGA_MAC_VER_37:
3818 case RTL_GIGA_MAC_VER_38:
3819 case RTL_GIGA_MAC_VER_39:
3820 case RTL_GIGA_MAC_VER_40:
3821 case RTL_GIGA_MAC_VER_41:
3822 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3823 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3830 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3832 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3835 rtl_writephy(tp, 0x1f, 0x0000);
3836 rtl_writephy(tp, MII_BMCR, 0x0000);
3838 rtl_wol_suspend_quirk(tp);
3843 static void r810x_phy_power_down(struct rtl8169_private *tp)
3845 rtl_writephy(tp, 0x1f, 0x0000);
3846 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3849 static void r810x_phy_power_up(struct rtl8169_private *tp)
3851 rtl_writephy(tp, 0x1f, 0x0000);
3852 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3855 static void r810x_pll_power_down(struct rtl8169_private *tp)
3857 void __iomem *ioaddr = tp->mmio_addr;
3859 if (rtl_wol_pll_power_down(tp))
3862 r810x_phy_power_down(tp);
3864 switch (tp->mac_version) {
3865 case RTL_GIGA_MAC_VER_07:
3866 case RTL_GIGA_MAC_VER_08:
3867 case RTL_GIGA_MAC_VER_09:
3868 case RTL_GIGA_MAC_VER_10:
3869 case RTL_GIGA_MAC_VER_13:
3870 case RTL_GIGA_MAC_VER_16:
3873 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3878 static void r810x_pll_power_up(struct rtl8169_private *tp)
3880 void __iomem *ioaddr = tp->mmio_addr;
3882 r810x_phy_power_up(tp);
3884 switch (tp->mac_version) {
3885 case RTL_GIGA_MAC_VER_07:
3886 case RTL_GIGA_MAC_VER_08:
3887 case RTL_GIGA_MAC_VER_09:
3888 case RTL_GIGA_MAC_VER_10:
3889 case RTL_GIGA_MAC_VER_13:
3890 case RTL_GIGA_MAC_VER_16:
3893 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3898 static void r8168_phy_power_up(struct rtl8169_private *tp)
3900 rtl_writephy(tp, 0x1f, 0x0000);
3901 switch (tp->mac_version) {
3902 case RTL_GIGA_MAC_VER_11:
3903 case RTL_GIGA_MAC_VER_12:
3904 case RTL_GIGA_MAC_VER_17:
3905 case RTL_GIGA_MAC_VER_18:
3906 case RTL_GIGA_MAC_VER_19:
3907 case RTL_GIGA_MAC_VER_20:
3908 case RTL_GIGA_MAC_VER_21:
3909 case RTL_GIGA_MAC_VER_22:
3910 case RTL_GIGA_MAC_VER_23:
3911 case RTL_GIGA_MAC_VER_24:
3912 case RTL_GIGA_MAC_VER_25:
3913 case RTL_GIGA_MAC_VER_26:
3914 case RTL_GIGA_MAC_VER_27:
3915 case RTL_GIGA_MAC_VER_28:
3916 case RTL_GIGA_MAC_VER_31:
3917 rtl_writephy(tp, 0x0e, 0x0000);
3922 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3925 static void r8168_phy_power_down(struct rtl8169_private *tp)
3927 rtl_writephy(tp, 0x1f, 0x0000);
3928 switch (tp->mac_version) {
3929 case RTL_GIGA_MAC_VER_32:
3930 case RTL_GIGA_MAC_VER_33:
3931 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3934 case RTL_GIGA_MAC_VER_11:
3935 case RTL_GIGA_MAC_VER_12:
3936 case RTL_GIGA_MAC_VER_17:
3937 case RTL_GIGA_MAC_VER_18:
3938 case RTL_GIGA_MAC_VER_19:
3939 case RTL_GIGA_MAC_VER_20:
3940 case RTL_GIGA_MAC_VER_21:
3941 case RTL_GIGA_MAC_VER_22:
3942 case RTL_GIGA_MAC_VER_23:
3943 case RTL_GIGA_MAC_VER_24:
3944 case RTL_GIGA_MAC_VER_25:
3945 case RTL_GIGA_MAC_VER_26:
3946 case RTL_GIGA_MAC_VER_27:
3947 case RTL_GIGA_MAC_VER_28:
3948 case RTL_GIGA_MAC_VER_31:
3949 rtl_writephy(tp, 0x0e, 0x0200);
3951 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3956 static void r8168_pll_power_down(struct rtl8169_private *tp)
3958 void __iomem *ioaddr = tp->mmio_addr;
3960 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3961 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3962 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3963 r8168dp_check_dash(tp)) {
3967 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3968 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
3969 (RTL_R16(CPlusCmd) & ASF)) {
3973 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3974 tp->mac_version == RTL_GIGA_MAC_VER_33)
3975 rtl_ephy_write(tp, 0x19, 0xff64);
3977 if (rtl_wol_pll_power_down(tp))
3980 r8168_phy_power_down(tp);
3982 switch (tp->mac_version) {
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 case RTL_GIGA_MAC_VER_32:
3989 case RTL_GIGA_MAC_VER_33:
3990 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3995 static void r8168_pll_power_up(struct rtl8169_private *tp)
3997 void __iomem *ioaddr = tp->mmio_addr;
3999 switch (tp->mac_version) {
4000 case RTL_GIGA_MAC_VER_25:
4001 case RTL_GIGA_MAC_VER_26:
4002 case RTL_GIGA_MAC_VER_27:
4003 case RTL_GIGA_MAC_VER_28:
4004 case RTL_GIGA_MAC_VER_31:
4005 case RTL_GIGA_MAC_VER_32:
4006 case RTL_GIGA_MAC_VER_33:
4007 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4011 r8168_phy_power_up(tp);
4014 static void rtl_generic_op(struct rtl8169_private *tp,
4015 void (*op)(struct rtl8169_private *))
4021 static void rtl_pll_power_down(struct rtl8169_private *tp)
4023 rtl_generic_op(tp, tp->pll_power_ops.down);
4026 static void rtl_pll_power_up(struct rtl8169_private *tp)
4028 rtl_generic_op(tp, tp->pll_power_ops.up);
4031 static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
4033 struct pll_power_ops *ops = &tp->pll_power_ops;
4035 switch (tp->mac_version) {
4036 case RTL_GIGA_MAC_VER_07:
4037 case RTL_GIGA_MAC_VER_08:
4038 case RTL_GIGA_MAC_VER_09:
4039 case RTL_GIGA_MAC_VER_10:
4040 case RTL_GIGA_MAC_VER_16:
4041 case RTL_GIGA_MAC_VER_29:
4042 case RTL_GIGA_MAC_VER_30:
4043 case RTL_GIGA_MAC_VER_37:
4044 case RTL_GIGA_MAC_VER_39:
4045 ops->down = r810x_pll_power_down;
4046 ops->up = r810x_pll_power_up;
4049 case RTL_GIGA_MAC_VER_11:
4050 case RTL_GIGA_MAC_VER_12:
4051 case RTL_GIGA_MAC_VER_17:
4052 case RTL_GIGA_MAC_VER_18:
4053 case RTL_GIGA_MAC_VER_19:
4054 case RTL_GIGA_MAC_VER_20:
4055 case RTL_GIGA_MAC_VER_21:
4056 case RTL_GIGA_MAC_VER_22:
4057 case RTL_GIGA_MAC_VER_23:
4058 case RTL_GIGA_MAC_VER_24:
4059 case RTL_GIGA_MAC_VER_25:
4060 case RTL_GIGA_MAC_VER_26:
4061 case RTL_GIGA_MAC_VER_27:
4062 case RTL_GIGA_MAC_VER_28:
4063 case RTL_GIGA_MAC_VER_31:
4064 case RTL_GIGA_MAC_VER_32:
4065 case RTL_GIGA_MAC_VER_33:
4066 case RTL_GIGA_MAC_VER_34:
4067 case RTL_GIGA_MAC_VER_35:
4068 case RTL_GIGA_MAC_VER_36:
4069 case RTL_GIGA_MAC_VER_38:
4070 case RTL_GIGA_MAC_VER_40:
4071 case RTL_GIGA_MAC_VER_41:
4072 ops->down = r8168_pll_power_down;
4073 ops->up = r8168_pll_power_up;
4083 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4085 void __iomem *ioaddr = tp->mmio_addr;
4087 switch (tp->mac_version) {
4088 case RTL_GIGA_MAC_VER_01:
4089 case RTL_GIGA_MAC_VER_02:
4090 case RTL_GIGA_MAC_VER_03:
4091 case RTL_GIGA_MAC_VER_04:
4092 case RTL_GIGA_MAC_VER_05:
4093 case RTL_GIGA_MAC_VER_06:
4094 case RTL_GIGA_MAC_VER_10:
4095 case RTL_GIGA_MAC_VER_11:
4096 case RTL_GIGA_MAC_VER_12:
4097 case RTL_GIGA_MAC_VER_13:
4098 case RTL_GIGA_MAC_VER_14:
4099 case RTL_GIGA_MAC_VER_15:
4100 case RTL_GIGA_MAC_VER_16:
4101 case RTL_GIGA_MAC_VER_17:
4102 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4104 case RTL_GIGA_MAC_VER_18:
4105 case RTL_GIGA_MAC_VER_19:
4106 case RTL_GIGA_MAC_VER_20:
4107 case RTL_GIGA_MAC_VER_21:
4108 case RTL_GIGA_MAC_VER_22:
4109 case RTL_GIGA_MAC_VER_23:
4110 case RTL_GIGA_MAC_VER_24:
4111 case RTL_GIGA_MAC_VER_34:
4112 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4115 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4120 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4122 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4125 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4127 void __iomem *ioaddr = tp->mmio_addr;
4129 RTL_W8(Cfg9346, Cfg9346_Unlock);
4130 rtl_generic_op(tp, tp->jumbo_ops.enable);
4131 RTL_W8(Cfg9346, Cfg9346_Lock);
4134 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4136 void __iomem *ioaddr = tp->mmio_addr;
4138 RTL_W8(Cfg9346, Cfg9346_Unlock);
4139 rtl_generic_op(tp, tp->jumbo_ops.disable);
4140 RTL_W8(Cfg9346, Cfg9346_Lock);
4143 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4145 void __iomem *ioaddr = tp->mmio_addr;
4147 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4148 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4149 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4152 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4154 void __iomem *ioaddr = tp->mmio_addr;
4156 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4157 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4158 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4161 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4163 void __iomem *ioaddr = tp->mmio_addr;
4165 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4168 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4170 void __iomem *ioaddr = tp->mmio_addr;
4172 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4175 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4177 void __iomem *ioaddr = tp->mmio_addr;
4179 RTL_W8(MaxTxPacketSize, 0x3f);
4180 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4181 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
4182 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4185 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4187 void __iomem *ioaddr = tp->mmio_addr;
4189 RTL_W8(MaxTxPacketSize, 0x0c);
4190 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4191 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
4192 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4195 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4197 rtl_tx_performance_tweak(tp->pci_dev,
4198 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4201 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4203 rtl_tx_performance_tweak(tp->pci_dev,
4204 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4207 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4209 void __iomem *ioaddr = tp->mmio_addr;
4211 r8168b_0_hw_jumbo_enable(tp);
4213 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4216 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4218 void __iomem *ioaddr = tp->mmio_addr;
4220 r8168b_0_hw_jumbo_disable(tp);
4222 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4225 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4227 struct jumbo_ops *ops = &tp->jumbo_ops;
4229 switch (tp->mac_version) {
4230 case RTL_GIGA_MAC_VER_11:
4231 ops->disable = r8168b_0_hw_jumbo_disable;
4232 ops->enable = r8168b_0_hw_jumbo_enable;
4234 case RTL_GIGA_MAC_VER_12:
4235 case RTL_GIGA_MAC_VER_17:
4236 ops->disable = r8168b_1_hw_jumbo_disable;
4237 ops->enable = r8168b_1_hw_jumbo_enable;
4239 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4240 case RTL_GIGA_MAC_VER_19:
4241 case RTL_GIGA_MAC_VER_20:
4242 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4243 case RTL_GIGA_MAC_VER_22:
4244 case RTL_GIGA_MAC_VER_23:
4245 case RTL_GIGA_MAC_VER_24:
4246 case RTL_GIGA_MAC_VER_25:
4247 case RTL_GIGA_MAC_VER_26:
4248 ops->disable = r8168c_hw_jumbo_disable;
4249 ops->enable = r8168c_hw_jumbo_enable;
4251 case RTL_GIGA_MAC_VER_27:
4252 case RTL_GIGA_MAC_VER_28:
4253 ops->disable = r8168dp_hw_jumbo_disable;
4254 ops->enable = r8168dp_hw_jumbo_enable;
4256 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4257 case RTL_GIGA_MAC_VER_32:
4258 case RTL_GIGA_MAC_VER_33:
4259 case RTL_GIGA_MAC_VER_34:
4260 ops->disable = r8168e_hw_jumbo_disable;
4261 ops->enable = r8168e_hw_jumbo_enable;
4265 * No action needed for jumbo frames with 8169.
4266 * No jumbo for 810x at all.
4268 case RTL_GIGA_MAC_VER_40:
4269 case RTL_GIGA_MAC_VER_41:
4271 ops->disable = NULL;
4277 DECLARE_RTL_COND(rtl_chipcmd_cond)
4279 void __iomem *ioaddr = tp->mmio_addr;
4281 return RTL_R8(ChipCmd) & CmdReset;
4284 static void rtl_hw_reset(struct rtl8169_private *tp)
4286 void __iomem *ioaddr = tp->mmio_addr;
4288 RTL_W8(ChipCmd, CmdReset);
4290 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4293 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4295 struct rtl_fw *rtl_fw;
4299 name = rtl_lookup_firmware_name(tp);
4301 goto out_no_firmware;
4303 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4307 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4311 rc = rtl_check_firmware(tp, rtl_fw);
4313 goto err_release_firmware;
4315 tp->rtl_fw = rtl_fw;
4319 err_release_firmware:
4320 release_firmware(rtl_fw->fw);
4324 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4331 static void rtl_request_firmware(struct rtl8169_private *tp)
4333 if (IS_ERR(tp->rtl_fw))
4334 rtl_request_uncached_firmware(tp);
4337 static void rtl_rx_close(struct rtl8169_private *tp)
4339 void __iomem *ioaddr = tp->mmio_addr;
4341 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4344 DECLARE_RTL_COND(rtl_npq_cond)
4346 void __iomem *ioaddr = tp->mmio_addr;
4348 return RTL_R8(TxPoll) & NPQ;
4351 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4353 void __iomem *ioaddr = tp->mmio_addr;
4355 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4358 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4360 void __iomem *ioaddr = tp->mmio_addr;
4362 /* Disable interrupts */
4363 rtl8169_irq_mask_and_ack(tp);
4367 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4368 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4369 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4370 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4371 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4372 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4373 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
4374 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4375 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4376 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
4377 tp->mac_version == RTL_GIGA_MAC_VER_38) {
4378 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4379 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4381 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4388 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4390 void __iomem *ioaddr = tp->mmio_addr;
4392 /* Set DMA burst size and Interframe Gap Time */
4393 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4394 (InterFrameGap << TxInterFrameGapShift));
4397 static void rtl_hw_start(struct net_device *dev)
4399 struct rtl8169_private *tp = netdev_priv(dev);
4403 rtl_irq_enable_all(tp);
4406 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4407 void __iomem *ioaddr)
4410 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4411 * register to be written before TxDescAddrLow to work.
4412 * Switching from MMIO to I/O access fixes the issue as well.
4414 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4415 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4416 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4417 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4420 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4424 cmd = RTL_R16(CPlusCmd);
4425 RTL_W16(CPlusCmd, cmd);
4429 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
4431 /* Low hurts. Let's disable the filtering. */
4432 RTL_W16(RxMaxSize, rx_buf_sz + 1);
4435 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4437 static const struct rtl_cfg2_info {
4442 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4443 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4444 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4445 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4447 const struct rtl_cfg2_info *p = cfg2_info;
4451 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
4452 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4453 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4454 RTL_W32(0x7c, p->val);
4460 static void rtl_set_rx_mode(struct net_device *dev)
4462 struct rtl8169_private *tp = netdev_priv(dev);
4463 void __iomem *ioaddr = tp->mmio_addr;
4464 u32 mc_filter[2]; /* Multicast hash filter */
4468 if (dev->flags & IFF_PROMISC) {
4469 /* Unconditionally log net taps. */
4470 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4472 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4474 mc_filter[1] = mc_filter[0] = 0xffffffff;
4475 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4476 (dev->flags & IFF_ALLMULTI)) {
4477 /* Too many to filter perfectly -- accept all multicasts. */
4478 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4479 mc_filter[1] = mc_filter[0] = 0xffffffff;
4481 struct netdev_hw_addr *ha;
4483 rx_mode = AcceptBroadcast | AcceptMyPhys;
4484 mc_filter[1] = mc_filter[0] = 0;
4485 netdev_for_each_mc_addr(ha, dev) {
4486 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4487 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4488 rx_mode |= AcceptMulticast;
4492 if (dev->features & NETIF_F_RXALL)
4493 rx_mode |= (AcceptErr | AcceptRunt);
4495 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4497 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4498 u32 data = mc_filter[0];
4500 mc_filter[0] = swab32(mc_filter[1]);
4501 mc_filter[1] = swab32(data);
4504 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4505 mc_filter[1] = mc_filter[0] = 0xffffffff;
4507 RTL_W32(MAR0 + 4, mc_filter[1]);
4508 RTL_W32(MAR0 + 0, mc_filter[0]);
4510 RTL_W32(RxConfig, tmp);
4513 static void rtl_hw_start_8169(struct net_device *dev)
4515 struct rtl8169_private *tp = netdev_priv(dev);
4516 void __iomem *ioaddr = tp->mmio_addr;
4517 struct pci_dev *pdev = tp->pci_dev;
4519 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4520 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4521 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4524 RTL_W8(Cfg9346, Cfg9346_Unlock);
4525 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4526 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4527 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4528 tp->mac_version == RTL_GIGA_MAC_VER_04)
4529 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4533 RTL_W8(EarlyTxThres, NoEarlyTx);
4535 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4537 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4538 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4539 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4540 tp->mac_version == RTL_GIGA_MAC_VER_04)
4541 rtl_set_rx_tx_config_registers(tp);
4543 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4545 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4546 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4547 dprintk("Set MAC Reg C+CR Offset 0xE0. "
4548 "Bit-3 and bit-14 MUST be 1\n");
4549 tp->cp_cmd |= (1 << 14);
4552 RTL_W16(CPlusCmd, tp->cp_cmd);
4554 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4557 * Undocumented corner. Supposedly:
4558 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4560 RTL_W16(IntrMitigate, 0x0000);
4562 rtl_set_rx_tx_desc_registers(tp, ioaddr);
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);
4569 rtl_set_rx_tx_config_registers(tp);
4572 RTL_W8(Cfg9346, Cfg9346_Lock);
4574 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4577 RTL_W32(RxMissed, 0);
4579 rtl_set_rx_mode(dev);
4581 /* no early-rx interrupts */
4582 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4585 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4587 if (tp->csi_ops.write)
4588 tp->csi_ops.write(tp, addr, value);
4591 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4593 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
4596 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
4600 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4601 rtl_csi_write(tp, 0x070c, csi | bits);
4604 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
4606 rtl_csi_access_enable(tp, 0x17000000);
4609 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
4611 rtl_csi_access_enable(tp, 0x27000000);
4614 DECLARE_RTL_COND(rtl_csiar_cond)
4616 void __iomem *ioaddr = tp->mmio_addr;
4618 return RTL_R32(CSIAR) & CSIAR_FLAG;
4621 static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
4623 void __iomem *ioaddr = tp->mmio_addr;
4625 RTL_W32(CSIDR, value);
4626 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4627 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4629 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4632 static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
4634 void __iomem *ioaddr = tp->mmio_addr;
4636 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4637 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4639 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4640 RTL_R32(CSIDR) : ~0;
4643 static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
4645 void __iomem *ioaddr = tp->mmio_addr;
4647 RTL_W32(CSIDR, value);
4648 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4649 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4652 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4655 static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
4657 void __iomem *ioaddr = tp->mmio_addr;
4659 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4660 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4662 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4663 RTL_R32(CSIDR) : ~0;
4666 static void rtl_init_csi_ops(struct rtl8169_private *tp)
4668 struct csi_ops *ops = &tp->csi_ops;
4670 switch (tp->mac_version) {
4671 case RTL_GIGA_MAC_VER_01:
4672 case RTL_GIGA_MAC_VER_02:
4673 case RTL_GIGA_MAC_VER_03:
4674 case RTL_GIGA_MAC_VER_04:
4675 case RTL_GIGA_MAC_VER_05:
4676 case RTL_GIGA_MAC_VER_06:
4677 case RTL_GIGA_MAC_VER_10:
4678 case RTL_GIGA_MAC_VER_11:
4679 case RTL_GIGA_MAC_VER_12:
4680 case RTL_GIGA_MAC_VER_13:
4681 case RTL_GIGA_MAC_VER_14:
4682 case RTL_GIGA_MAC_VER_15:
4683 case RTL_GIGA_MAC_VER_16:
4684 case RTL_GIGA_MAC_VER_17:
4689 case RTL_GIGA_MAC_VER_37:
4690 case RTL_GIGA_MAC_VER_38:
4691 ops->write = r8402_csi_write;
4692 ops->read = r8402_csi_read;
4696 ops->write = r8169_csi_write;
4697 ops->read = r8169_csi_read;
4703 unsigned int offset;
4708 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4714 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4715 rtl_ephy_write(tp, e->offset, w);
4720 static void rtl_disable_clock_request(struct pci_dev *pdev)
4722 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4723 PCI_EXP_LNKCTL_CLKREQ_EN);
4726 static void rtl_enable_clock_request(struct pci_dev *pdev)
4728 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4729 PCI_EXP_LNKCTL_CLKREQ_EN);
4732 #define R8168_CPCMD_QUIRK_MASK (\
4743 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4745 void __iomem *ioaddr = tp->mmio_addr;
4746 struct pci_dev *pdev = tp->pci_dev;
4748 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4750 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4752 if (tp->dev->mtu <= ETH_DATA_LEN) {
4753 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
4754 PCI_EXP_DEVCTL_NOSNOOP_EN);
4758 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4760 void __iomem *ioaddr = tp->mmio_addr;
4762 rtl_hw_start_8168bb(tp);
4764 RTL_W8(MaxTxPacketSize, TxPacketMax);
4766 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4769 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4771 void __iomem *ioaddr = tp->mmio_addr;
4772 struct pci_dev *pdev = tp->pci_dev;
4774 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4776 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4778 if (tp->dev->mtu <= ETH_DATA_LEN)
4779 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4781 rtl_disable_clock_request(pdev);
4783 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4786 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4788 static const struct ephy_info e_info_8168cp[] = {
4789 { 0x01, 0, 0x0001 },
4790 { 0x02, 0x0800, 0x1000 },
4791 { 0x03, 0, 0x0042 },
4792 { 0x06, 0x0080, 0x0000 },
4796 rtl_csi_access_enable_2(tp);
4798 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4800 __rtl_hw_start_8168cp(tp);
4803 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4805 void __iomem *ioaddr = tp->mmio_addr;
4806 struct pci_dev *pdev = tp->pci_dev;
4808 rtl_csi_access_enable_2(tp);
4810 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4812 if (tp->dev->mtu <= ETH_DATA_LEN)
4813 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4815 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4818 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4820 void __iomem *ioaddr = tp->mmio_addr;
4821 struct pci_dev *pdev = tp->pci_dev;
4823 rtl_csi_access_enable_2(tp);
4825 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4828 RTL_W8(DBG_REG, 0x20);
4830 RTL_W8(MaxTxPacketSize, TxPacketMax);
4832 if (tp->dev->mtu <= ETH_DATA_LEN)
4833 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4835 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4838 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4840 void __iomem *ioaddr = tp->mmio_addr;
4841 static const struct ephy_info e_info_8168c_1[] = {
4842 { 0x02, 0x0800, 0x1000 },
4843 { 0x03, 0, 0x0002 },
4844 { 0x06, 0x0080, 0x0000 }
4847 rtl_csi_access_enable_2(tp);
4849 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4851 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4853 __rtl_hw_start_8168cp(tp);
4856 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4858 static const struct ephy_info e_info_8168c_2[] = {
4859 { 0x01, 0, 0x0001 },
4860 { 0x03, 0x0400, 0x0220 }
4863 rtl_csi_access_enable_2(tp);
4865 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4867 __rtl_hw_start_8168cp(tp);
4870 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4872 rtl_hw_start_8168c_2(tp);
4875 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4877 rtl_csi_access_enable_2(tp);
4879 __rtl_hw_start_8168cp(tp);
4882 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4884 void __iomem *ioaddr = tp->mmio_addr;
4885 struct pci_dev *pdev = tp->pci_dev;
4887 rtl_csi_access_enable_2(tp);
4889 rtl_disable_clock_request(pdev);
4891 RTL_W8(MaxTxPacketSize, TxPacketMax);
4893 if (tp->dev->mtu <= ETH_DATA_LEN)
4894 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4896 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4899 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
4901 void __iomem *ioaddr = tp->mmio_addr;
4902 struct pci_dev *pdev = tp->pci_dev;
4904 rtl_csi_access_enable_1(tp);
4906 if (tp->dev->mtu <= ETH_DATA_LEN)
4907 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4909 RTL_W8(MaxTxPacketSize, TxPacketMax);
4911 rtl_disable_clock_request(pdev);
4914 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
4916 void __iomem *ioaddr = tp->mmio_addr;
4917 struct pci_dev *pdev = tp->pci_dev;
4918 static const struct ephy_info e_info_8168d_4[] = {
4920 { 0x19, 0x20, 0x50 },
4925 rtl_csi_access_enable_1(tp);
4927 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4929 RTL_W8(MaxTxPacketSize, TxPacketMax);
4931 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4932 const struct ephy_info *e = e_info_8168d_4 + i;
4935 w = rtl_ephy_read(tp, e->offset);
4936 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
4939 rtl_enable_clock_request(pdev);
4942 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
4944 void __iomem *ioaddr = tp->mmio_addr;
4945 struct pci_dev *pdev = tp->pci_dev;
4946 static const struct ephy_info e_info_8168e_1[] = {
4947 { 0x00, 0x0200, 0x0100 },
4948 { 0x00, 0x0000, 0x0004 },
4949 { 0x06, 0x0002, 0x0001 },
4950 { 0x06, 0x0000, 0x0030 },
4951 { 0x07, 0x0000, 0x2000 },
4952 { 0x00, 0x0000, 0x0020 },
4953 { 0x03, 0x5800, 0x2000 },
4954 { 0x03, 0x0000, 0x0001 },
4955 { 0x01, 0x0800, 0x1000 },
4956 { 0x07, 0x0000, 0x4000 },
4957 { 0x1e, 0x0000, 0x2000 },
4958 { 0x19, 0xffff, 0xfe6c },
4959 { 0x0a, 0x0000, 0x0040 }
4962 rtl_csi_access_enable_2(tp);
4964 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
4966 if (tp->dev->mtu <= ETH_DATA_LEN)
4967 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4969 RTL_W8(MaxTxPacketSize, TxPacketMax);
4971 rtl_disable_clock_request(pdev);
4973 /* Reset tx FIFO pointer */
4974 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4975 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
4977 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4980 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
4982 void __iomem *ioaddr = tp->mmio_addr;
4983 struct pci_dev *pdev = tp->pci_dev;
4984 static const struct ephy_info e_info_8168e_2[] = {
4985 { 0x09, 0x0000, 0x0080 },
4986 { 0x19, 0x0000, 0x0224 }
4989 rtl_csi_access_enable_1(tp);
4991 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4993 if (tp->dev->mtu <= ETH_DATA_LEN)
4994 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4996 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4997 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4998 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4999 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5000 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5001 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5002 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5003 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5005 RTL_W8(MaxTxPacketSize, EarlySize);
5007 rtl_disable_clock_request(pdev);
5009 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5010 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5012 /* Adjust EEE LED frequency */
5013 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5015 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5016 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5017 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5020 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5022 void __iomem *ioaddr = tp->mmio_addr;
5023 struct pci_dev *pdev = tp->pci_dev;
5025 rtl_csi_access_enable_2(tp);
5027 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5029 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5030 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5031 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5032 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5033 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5034 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5035 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5036 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5037 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5038 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5040 RTL_W8(MaxTxPacketSize, EarlySize);
5042 rtl_disable_clock_request(pdev);
5044 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5045 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5046 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5047 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5048 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5051 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5053 void __iomem *ioaddr = tp->mmio_addr;
5054 static const struct ephy_info e_info_8168f_1[] = {
5055 { 0x06, 0x00c0, 0x0020 },
5056 { 0x08, 0x0001, 0x0002 },
5057 { 0x09, 0x0000, 0x0080 },
5058 { 0x19, 0x0000, 0x0224 }
5061 rtl_hw_start_8168f(tp);
5063 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5065 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5067 /* Adjust EEE LED frequency */
5068 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5071 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5073 static const struct ephy_info e_info_8168f_1[] = {
5074 { 0x06, 0x00c0, 0x0020 },
5075 { 0x0f, 0xffff, 0x5200 },
5076 { 0x1e, 0x0000, 0x4000 },
5077 { 0x19, 0x0000, 0x0224 }
5080 rtl_hw_start_8168f(tp);
5082 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5084 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5087 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5089 void __iomem *ioaddr = tp->mmio_addr;
5090 struct pci_dev *pdev = tp->pci_dev;
5092 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5093 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5094 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5095 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5097 rtl_csi_access_enable_1(tp);
5099 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5101 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5102 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5104 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5105 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
5106 RTL_W8(MaxTxPacketSize, EarlySize);
5108 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5109 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5111 /* Adjust EEE LED frequency */
5112 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5114 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x02, ERIAR_EXGMAC);
5117 static void rtl_hw_start_8168(struct net_device *dev)
5119 struct rtl8169_private *tp = netdev_priv(dev);
5120 void __iomem *ioaddr = tp->mmio_addr;
5122 RTL_W8(Cfg9346, Cfg9346_Unlock);
5124 RTL_W8(MaxTxPacketSize, TxPacketMax);
5126 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5128 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
5130 RTL_W16(CPlusCmd, tp->cp_cmd);
5132 RTL_W16(IntrMitigate, 0x5151);
5134 /* Work around for RxFIFO overflow. */
5135 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5136 tp->event_slow |= RxFIFOOver | PCSTimeout;
5137 tp->event_slow &= ~RxOverflow;
5140 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5142 rtl_set_rx_mode(dev);
5144 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5145 (InterFrameGap << TxInterFrameGapShift));
5149 switch (tp->mac_version) {
5150 case RTL_GIGA_MAC_VER_11:
5151 rtl_hw_start_8168bb(tp);
5154 case RTL_GIGA_MAC_VER_12:
5155 case RTL_GIGA_MAC_VER_17:
5156 rtl_hw_start_8168bef(tp);
5159 case RTL_GIGA_MAC_VER_18:
5160 rtl_hw_start_8168cp_1(tp);
5163 case RTL_GIGA_MAC_VER_19:
5164 rtl_hw_start_8168c_1(tp);
5167 case RTL_GIGA_MAC_VER_20:
5168 rtl_hw_start_8168c_2(tp);
5171 case RTL_GIGA_MAC_VER_21:
5172 rtl_hw_start_8168c_3(tp);
5175 case RTL_GIGA_MAC_VER_22:
5176 rtl_hw_start_8168c_4(tp);
5179 case RTL_GIGA_MAC_VER_23:
5180 rtl_hw_start_8168cp_2(tp);
5183 case RTL_GIGA_MAC_VER_24:
5184 rtl_hw_start_8168cp_3(tp);
5187 case RTL_GIGA_MAC_VER_25:
5188 case RTL_GIGA_MAC_VER_26:
5189 case RTL_GIGA_MAC_VER_27:
5190 rtl_hw_start_8168d(tp);
5193 case RTL_GIGA_MAC_VER_28:
5194 rtl_hw_start_8168d_4(tp);
5197 case RTL_GIGA_MAC_VER_31:
5198 rtl_hw_start_8168dp(tp);
5201 case RTL_GIGA_MAC_VER_32:
5202 case RTL_GIGA_MAC_VER_33:
5203 rtl_hw_start_8168e_1(tp);
5205 case RTL_GIGA_MAC_VER_34:
5206 rtl_hw_start_8168e_2(tp);
5209 case RTL_GIGA_MAC_VER_35:
5210 case RTL_GIGA_MAC_VER_36:
5211 rtl_hw_start_8168f_1(tp);
5214 case RTL_GIGA_MAC_VER_38:
5215 rtl_hw_start_8411(tp);
5218 case RTL_GIGA_MAC_VER_40:
5219 case RTL_GIGA_MAC_VER_41:
5220 rtl_hw_start_8168g_1(tp);
5224 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5225 dev->name, tp->mac_version);
5229 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5231 RTL_W8(Cfg9346, Cfg9346_Lock);
5233 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
5236 #define R810X_CPCMD_QUIRK_MASK (\
5247 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5249 void __iomem *ioaddr = tp->mmio_addr;
5250 struct pci_dev *pdev = tp->pci_dev;
5251 static const struct ephy_info e_info_8102e_1[] = {
5252 { 0x01, 0, 0x6e65 },
5253 { 0x02, 0, 0x091f },
5254 { 0x03, 0, 0xc2f9 },
5255 { 0x06, 0, 0xafb5 },
5256 { 0x07, 0, 0x0e00 },
5257 { 0x19, 0, 0xec80 },
5258 { 0x01, 0, 0x2e65 },
5263 rtl_csi_access_enable_2(tp);
5265 RTL_W8(DBG_REG, FIX_NAK_1);
5267 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5270 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5271 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5273 cfg1 = RTL_R8(Config1);
5274 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5275 RTL_W8(Config1, cfg1 & ~LEDS0);
5277 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5280 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5282 void __iomem *ioaddr = tp->mmio_addr;
5283 struct pci_dev *pdev = tp->pci_dev;
5285 rtl_csi_access_enable_2(tp);
5287 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5289 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5290 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5293 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5295 rtl_hw_start_8102e_2(tp);
5297 rtl_ephy_write(tp, 0x03, 0xc2f9);
5300 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5302 void __iomem *ioaddr = tp->mmio_addr;
5303 static const struct ephy_info e_info_8105e_1[] = {
5304 { 0x07, 0, 0x4000 },
5305 { 0x19, 0, 0x0200 },
5306 { 0x19, 0, 0x0020 },
5307 { 0x1e, 0, 0x2000 },
5308 { 0x03, 0, 0x0001 },
5309 { 0x19, 0, 0x0100 },
5310 { 0x19, 0, 0x0004 },
5314 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5315 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5317 /* Disable Early Tally Counter */
5318 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5320 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5321 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5323 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5326 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5328 rtl_hw_start_8105e_1(tp);
5329 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5332 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5334 void __iomem *ioaddr = tp->mmio_addr;
5335 static const struct ephy_info e_info_8402[] = {
5336 { 0x19, 0xffff, 0xff64 },
5340 rtl_csi_access_enable_2(tp);
5342 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5343 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5345 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5346 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5348 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
5350 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5352 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5353 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5354 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5355 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5356 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5357 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5358 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
5361 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5363 void __iomem *ioaddr = tp->mmio_addr;
5365 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5366 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5368 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5369 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5370 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5373 static void rtl_hw_start_8101(struct net_device *dev)
5375 struct rtl8169_private *tp = netdev_priv(dev);
5376 void __iomem *ioaddr = tp->mmio_addr;
5377 struct pci_dev *pdev = tp->pci_dev;
5379 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5380 tp->event_slow &= ~RxFIFOOver;
5382 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5383 tp->mac_version == RTL_GIGA_MAC_VER_16)
5384 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5385 PCI_EXP_DEVCTL_NOSNOOP_EN);
5387 RTL_W8(Cfg9346, Cfg9346_Unlock);
5389 switch (tp->mac_version) {
5390 case RTL_GIGA_MAC_VER_07:
5391 rtl_hw_start_8102e_1(tp);
5394 case RTL_GIGA_MAC_VER_08:
5395 rtl_hw_start_8102e_3(tp);
5398 case RTL_GIGA_MAC_VER_09:
5399 rtl_hw_start_8102e_2(tp);
5402 case RTL_GIGA_MAC_VER_29:
5403 rtl_hw_start_8105e_1(tp);
5405 case RTL_GIGA_MAC_VER_30:
5406 rtl_hw_start_8105e_2(tp);
5409 case RTL_GIGA_MAC_VER_37:
5410 rtl_hw_start_8402(tp);
5413 case RTL_GIGA_MAC_VER_39:
5414 rtl_hw_start_8106(tp);
5418 RTL_W8(Cfg9346, Cfg9346_Lock);
5420 RTL_W8(MaxTxPacketSize, TxPacketMax);
5422 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5424 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5425 RTL_W16(CPlusCmd, tp->cp_cmd);
5427 RTL_W16(IntrMitigate, 0x0000);
5429 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5431 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5432 rtl_set_rx_tx_config_registers(tp);
5436 rtl_set_rx_mode(dev);
5438 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5441 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5443 struct rtl8169_private *tp = netdev_priv(dev);
5445 if (new_mtu < ETH_ZLEN ||
5446 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
5449 if (new_mtu > ETH_DATA_LEN)
5450 rtl_hw_jumbo_enable(tp);
5452 rtl_hw_jumbo_disable(tp);
5455 netdev_update_features(dev);
5460 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5462 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5463 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5466 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5467 void **data_buff, struct RxDesc *desc)
5469 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
5474 rtl8169_make_unusable_by_asic(desc);
5477 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5479 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5481 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5484 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5487 desc->addr = cpu_to_le64(mapping);
5489 rtl8169_mark_to_asic(desc, rx_buf_sz);
5492 static inline void *rtl8169_align(void *data)
5494 return (void *)ALIGN((long)data, 16);
5497 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5498 struct RxDesc *desc)
5502 struct device *d = &tp->pci_dev->dev;
5503 struct net_device *dev = tp->dev;
5504 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
5506 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5510 if (rtl8169_align(data) != data) {
5512 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5517 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
5519 if (unlikely(dma_mapping_error(d, mapping))) {
5520 if (net_ratelimit())
5521 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5525 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
5533 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5537 for (i = 0; i < NUM_RX_DESC; i++) {
5538 if (tp->Rx_databuff[i]) {
5539 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5540 tp->RxDescArray + i);
5545 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5547 desc->opts1 |= cpu_to_le32(RingEnd);
5550 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5554 for (i = 0; i < NUM_RX_DESC; i++) {
5557 if (tp->Rx_databuff[i])
5560 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5562 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5565 tp->Rx_databuff[i] = data;
5568 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5572 rtl8169_rx_clear(tp);
5576 static int rtl8169_init_ring(struct net_device *dev)
5578 struct rtl8169_private *tp = netdev_priv(dev);
5580 rtl8169_init_ring_indexes(tp);
5582 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
5583 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
5585 return rtl8169_rx_fill(tp);
5588 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5589 struct TxDesc *desc)
5591 unsigned int len = tx_skb->len;
5593 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5601 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5606 for (i = 0; i < n; i++) {
5607 unsigned int entry = (start + i) % NUM_TX_DESC;
5608 struct ring_info *tx_skb = tp->tx_skb + entry;
5609 unsigned int len = tx_skb->len;
5612 struct sk_buff *skb = tx_skb->skb;
5614 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5615 tp->TxDescArray + entry);
5617 tp->dev->stats.tx_dropped++;
5625 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5627 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5628 tp->cur_tx = tp->dirty_tx = 0;
5631 static void rtl_reset_work(struct rtl8169_private *tp)
5633 struct net_device *dev = tp->dev;
5636 napi_disable(&tp->napi);
5637 netif_stop_queue(dev);
5638 synchronize_sched();
5640 rtl8169_hw_reset(tp);
5642 for (i = 0; i < NUM_RX_DESC; i++)
5643 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5645 rtl8169_tx_clear(tp);
5646 rtl8169_init_ring_indexes(tp);
5648 napi_enable(&tp->napi);
5650 netif_wake_queue(dev);
5651 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
5654 static void rtl8169_tx_timeout(struct net_device *dev)
5656 struct rtl8169_private *tp = netdev_priv(dev);
5658 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5661 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5664 struct skb_shared_info *info = skb_shinfo(skb);
5665 unsigned int cur_frag, entry;
5666 struct TxDesc * uninitialized_var(txd);
5667 struct device *d = &tp->pci_dev->dev;
5670 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5671 const skb_frag_t *frag = info->frags + cur_frag;
5676 entry = (entry + 1) % NUM_TX_DESC;
5678 txd = tp->TxDescArray + entry;
5679 len = skb_frag_size(frag);
5680 addr = skb_frag_address(frag);
5681 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5682 if (unlikely(dma_mapping_error(d, mapping))) {
5683 if (net_ratelimit())
5684 netif_err(tp, drv, tp->dev,
5685 "Failed to map TX fragments DMA!\n");
5689 /* Anti gcc 2.95.3 bugware (sic) */
5690 status = opts[0] | len |
5691 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5693 txd->opts1 = cpu_to_le32(status);
5694 txd->opts2 = cpu_to_le32(opts[1]);
5695 txd->addr = cpu_to_le64(mapping);
5697 tp->tx_skb[entry].len = len;
5701 tp->tx_skb[entry].skb = skb;
5702 txd->opts1 |= cpu_to_le32(LastFrag);
5708 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5712 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5713 struct sk_buff *skb, u32 *opts)
5715 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
5716 u32 mss = skb_shinfo(skb)->gso_size;
5717 int offset = info->opts_offset;
5721 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5722 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5723 const struct iphdr *ip = ip_hdr(skb);
5725 if (ip->protocol == IPPROTO_TCP)
5726 opts[offset] |= info->checksum.tcp;
5727 else if (ip->protocol == IPPROTO_UDP)
5728 opts[offset] |= info->checksum.udp;
5734 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5735 struct net_device *dev)
5737 struct rtl8169_private *tp = netdev_priv(dev);
5738 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5739 struct TxDesc *txd = tp->TxDescArray + entry;
5740 void __iomem *ioaddr = tp->mmio_addr;
5741 struct device *d = &tp->pci_dev->dev;
5747 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
5748 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5752 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5755 len = skb_headlen(skb);
5756 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
5757 if (unlikely(dma_mapping_error(d, mapping))) {
5758 if (net_ratelimit())
5759 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
5763 tp->tx_skb[entry].len = len;
5764 txd->addr = cpu_to_le64(mapping);
5766 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
5769 rtl8169_tso_csum(tp, skb, opts);
5771 frags = rtl8169_xmit_frags(tp, skb, opts);
5775 opts[0] |= FirstFrag;
5777 opts[0] |= FirstFrag | LastFrag;
5778 tp->tx_skb[entry].skb = skb;
5781 txd->opts2 = cpu_to_le32(opts[1]);
5783 skb_tx_timestamp(skb);
5787 /* Anti gcc 2.95.3 bugware (sic) */
5788 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
5789 txd->opts1 = cpu_to_le32(status);
5791 tp->cur_tx += frags + 1;
5795 RTL_W8(TxPoll, NPQ);
5799 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5800 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5801 * not miss a ring update when it notices a stopped queue.
5804 netif_stop_queue(dev);
5805 /* Sync with rtl_tx:
5806 * - publish queue status and cur_tx ring index (write barrier)
5807 * - refresh dirty_tx ring index (read barrier).
5808 * May the current thread have a pessimistic view of the ring
5809 * status and forget to wake up queue, a racing rtl_tx thread
5813 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
5814 netif_wake_queue(dev);
5817 return NETDEV_TX_OK;
5820 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
5823 dev->stats.tx_dropped++;
5824 return NETDEV_TX_OK;
5827 netif_stop_queue(dev);
5828 dev->stats.tx_dropped++;
5829 return NETDEV_TX_BUSY;
5832 static void rtl8169_pcierr_interrupt(struct net_device *dev)
5834 struct rtl8169_private *tp = netdev_priv(dev);
5835 struct pci_dev *pdev = tp->pci_dev;
5836 u16 pci_status, pci_cmd;
5838 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5839 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5841 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5842 pci_cmd, pci_status);
5845 * The recovery sequence below admits a very elaborated explanation:
5846 * - it seems to work;
5847 * - I did not see what else could be done;
5848 * - it makes iop3xx happy.
5850 * Feel free to adjust to your needs.
5852 if (pdev->broken_parity_status)
5853 pci_cmd &= ~PCI_COMMAND_PARITY;
5855 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5857 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
5859 pci_write_config_word(pdev, PCI_STATUS,
5860 pci_status & (PCI_STATUS_DETECTED_PARITY |
5861 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5862 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5864 /* The infamous DAC f*ckup only happens at boot time */
5865 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
5866 void __iomem *ioaddr = tp->mmio_addr;
5868 netif_info(tp, intr, dev, "disabling PCI DAC\n");
5869 tp->cp_cmd &= ~PCIDAC;
5870 RTL_W16(CPlusCmd, tp->cp_cmd);
5871 dev->features &= ~NETIF_F_HIGHDMA;
5874 rtl8169_hw_reset(tp);
5876 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5879 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
5881 unsigned int dirty_tx, tx_left;
5883 dirty_tx = tp->dirty_tx;
5885 tx_left = tp->cur_tx - dirty_tx;
5887 while (tx_left > 0) {
5888 unsigned int entry = dirty_tx % NUM_TX_DESC;
5889 struct ring_info *tx_skb = tp->tx_skb + entry;
5893 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5894 if (status & DescOwn)
5897 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5898 tp->TxDescArray + entry);
5899 if (status & LastFrag) {
5900 u64_stats_update_begin(&tp->tx_stats.syncp);
5901 tp->tx_stats.packets++;
5902 tp->tx_stats.bytes += tx_skb->skb->len;
5903 u64_stats_update_end(&tp->tx_stats.syncp);
5904 dev_kfree_skb(tx_skb->skb);
5911 if (tp->dirty_tx != dirty_tx) {
5912 tp->dirty_tx = dirty_tx;
5913 /* Sync with rtl8169_start_xmit:
5914 * - publish dirty_tx ring index (write barrier)
5915 * - refresh cur_tx ring index and queue status (read barrier)
5916 * May the current thread miss the stopped queue condition,
5917 * a racing xmit thread can only have a right view of the
5921 if (netif_queue_stopped(dev) &&
5922 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5923 netif_wake_queue(dev);
5926 * 8168 hack: TxPoll requests are lost when the Tx packets are
5927 * too close. Let's kick an extra TxPoll request when a burst
5928 * of start_xmit activity is detected (if it is not detected,
5929 * it is slow enough). -- FR
5931 if (tp->cur_tx != dirty_tx) {
5932 void __iomem *ioaddr = tp->mmio_addr;
5934 RTL_W8(TxPoll, NPQ);
5939 static inline int rtl8169_fragmented_frame(u32 status)
5941 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5944 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
5946 u32 status = opts1 & RxProtoMask;
5948 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
5949 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5950 skb->ip_summed = CHECKSUM_UNNECESSARY;
5952 skb_checksum_none_assert(skb);
5955 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5956 struct rtl8169_private *tp,
5960 struct sk_buff *skb;
5961 struct device *d = &tp->pci_dev->dev;
5963 data = rtl8169_align(data);
5964 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
5966 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5968 memcpy(skb->data, data, pkt_size);
5969 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5974 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
5976 unsigned int cur_rx, rx_left;
5979 cur_rx = tp->cur_rx;
5981 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
5982 unsigned int entry = cur_rx % NUM_RX_DESC;
5983 struct RxDesc *desc = tp->RxDescArray + entry;
5987 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
5989 if (status & DescOwn)
5991 if (unlikely(status & RxRES)) {
5992 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5994 dev->stats.rx_errors++;
5995 if (status & (RxRWT | RxRUNT))
5996 dev->stats.rx_length_errors++;
5998 dev->stats.rx_crc_errors++;
5999 if (status & RxFOVF) {
6000 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6001 dev->stats.rx_fifo_errors++;
6003 if ((status & (RxRUNT | RxCRC)) &&
6004 !(status & (RxRWT | RxFOVF)) &&
6005 (dev->features & NETIF_F_RXALL))
6008 struct sk_buff *skb;
6013 addr = le64_to_cpu(desc->addr);
6014 if (likely(!(dev->features & NETIF_F_RXFCS)))
6015 pkt_size = (status & 0x00003fff) - 4;
6017 pkt_size = status & 0x00003fff;
6020 * The driver does not support incoming fragmented
6021 * frames. They are seen as a symptom of over-mtu
6024 if (unlikely(rtl8169_fragmented_frame(status))) {
6025 dev->stats.rx_dropped++;
6026 dev->stats.rx_length_errors++;
6027 goto release_descriptor;
6030 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6031 tp, pkt_size, addr);
6033 dev->stats.rx_dropped++;
6034 goto release_descriptor;
6037 rtl8169_rx_csum(skb, status);
6038 skb_put(skb, pkt_size);
6039 skb->protocol = eth_type_trans(skb, dev);
6041 rtl8169_rx_vlan_tag(desc, skb);
6043 napi_gro_receive(&tp->napi, skb);
6045 u64_stats_update_begin(&tp->rx_stats.syncp);
6046 tp->rx_stats.packets++;
6047 tp->rx_stats.bytes += pkt_size;
6048 u64_stats_update_end(&tp->rx_stats.syncp);
6053 rtl8169_mark_to_asic(desc, rx_buf_sz);
6056 count = cur_rx - tp->cur_rx;
6057 tp->cur_rx = cur_rx;
6062 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6064 struct net_device *dev = dev_instance;
6065 struct rtl8169_private *tp = netdev_priv(dev);
6069 status = rtl_get_events(tp);
6070 if (status && status != 0xffff) {
6071 status &= RTL_EVENT_NAPI | tp->event_slow;
6075 rtl_irq_disable(tp);
6076 napi_schedule(&tp->napi);
6079 return IRQ_RETVAL(handled);
6083 * Workqueue context.
6085 static void rtl_slow_event_work(struct rtl8169_private *tp)
6087 struct net_device *dev = tp->dev;
6090 status = rtl_get_events(tp) & tp->event_slow;
6091 rtl_ack_events(tp, status);
6093 if (unlikely(status & RxFIFOOver)) {
6094 switch (tp->mac_version) {
6095 /* Work around for rx fifo overflow */
6096 case RTL_GIGA_MAC_VER_11:
6097 netif_stop_queue(dev);
6098 /* XXX - Hack alert. See rtl_task(). */
6099 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6105 if (unlikely(status & SYSErr))
6106 rtl8169_pcierr_interrupt(dev);
6108 if (status & LinkChg)
6109 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6111 rtl_irq_enable_all(tp);
6114 static void rtl_task(struct work_struct *work)
6116 static const struct {
6118 void (*action)(struct rtl8169_private *);
6120 /* XXX - keep rtl_slow_event_work() as first element. */
6121 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6122 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6123 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6125 struct rtl8169_private *tp =
6126 container_of(work, struct rtl8169_private, wk.work);
6127 struct net_device *dev = tp->dev;
6132 if (!netif_running(dev) ||
6133 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6136 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6139 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6141 rtl_work[i].action(tp);
6145 rtl_unlock_work(tp);
6148 static int rtl8169_poll(struct napi_struct *napi, int budget)
6150 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6151 struct net_device *dev = tp->dev;
6152 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6156 status = rtl_get_events(tp);
6157 rtl_ack_events(tp, status & ~tp->event_slow);
6159 if (status & RTL_EVENT_NAPI_RX)
6160 work_done = rtl_rx(dev, tp, (u32) budget);
6162 if (status & RTL_EVENT_NAPI_TX)
6165 if (status & tp->event_slow) {
6166 enable_mask &= ~tp->event_slow;
6168 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6171 if (work_done < budget) {
6172 napi_complete(napi);
6174 rtl_irq_enable(tp, enable_mask);
6181 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6183 struct rtl8169_private *tp = netdev_priv(dev);
6185 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6188 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6189 RTL_W32(RxMissed, 0);
6192 static void rtl8169_down(struct net_device *dev)
6194 struct rtl8169_private *tp = netdev_priv(dev);
6195 void __iomem *ioaddr = tp->mmio_addr;
6197 del_timer_sync(&tp->timer);
6199 napi_disable(&tp->napi);
6200 netif_stop_queue(dev);
6202 rtl8169_hw_reset(tp);
6204 * At this point device interrupts can not be enabled in any function,
6205 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6206 * and napi is disabled (rtl8169_poll).
6208 rtl8169_rx_missed(dev, ioaddr);
6210 /* Give a racing hard_start_xmit a few cycles to complete. */
6211 synchronize_sched();
6213 rtl8169_tx_clear(tp);
6215 rtl8169_rx_clear(tp);
6217 rtl_pll_power_down(tp);
6220 static int rtl8169_close(struct net_device *dev)
6222 struct rtl8169_private *tp = netdev_priv(dev);
6223 struct pci_dev *pdev = tp->pci_dev;
6225 pm_runtime_get_sync(&pdev->dev);
6227 /* Update counters before going down */
6228 rtl8169_update_counters(dev);
6231 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6234 rtl_unlock_work(tp);
6236 free_irq(pdev->irq, dev);
6238 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6240 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6242 tp->TxDescArray = NULL;
6243 tp->RxDescArray = NULL;
6245 pm_runtime_put_sync(&pdev->dev);
6250 #ifdef CONFIG_NET_POLL_CONTROLLER
6251 static void rtl8169_netpoll(struct net_device *dev)
6253 struct rtl8169_private *tp = netdev_priv(dev);
6255 rtl8169_interrupt(tp->pci_dev->irq, dev);
6259 static int rtl_open(struct net_device *dev)
6261 struct rtl8169_private *tp = netdev_priv(dev);
6262 void __iomem *ioaddr = tp->mmio_addr;
6263 struct pci_dev *pdev = tp->pci_dev;
6264 int retval = -ENOMEM;
6266 pm_runtime_get_sync(&pdev->dev);
6269 * Rx and Tx descriptors needs 256 bytes alignment.
6270 * dma_alloc_coherent provides more.
6272 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6273 &tp->TxPhyAddr, GFP_KERNEL);
6274 if (!tp->TxDescArray)
6275 goto err_pm_runtime_put;
6277 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6278 &tp->RxPhyAddr, GFP_KERNEL);
6279 if (!tp->RxDescArray)
6282 retval = rtl8169_init_ring(dev);
6286 INIT_WORK(&tp->wk.work, rtl_task);
6290 rtl_request_firmware(tp);
6292 retval = request_irq(pdev->irq, rtl8169_interrupt,
6293 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6296 goto err_release_fw_2;
6300 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6302 napi_enable(&tp->napi);
6304 rtl8169_init_phy(dev, tp);
6306 __rtl8169_set_features(dev, dev->features);
6308 rtl_pll_power_up(tp);
6312 netif_start_queue(dev);
6314 rtl_unlock_work(tp);
6316 tp->saved_wolopts = 0;
6317 pm_runtime_put_noidle(&pdev->dev);
6319 rtl8169_check_link_status(dev, tp, ioaddr);
6324 rtl_release_firmware(tp);
6325 rtl8169_rx_clear(tp);
6327 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6329 tp->RxDescArray = NULL;
6331 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6333 tp->TxDescArray = NULL;
6335 pm_runtime_put_noidle(&pdev->dev);
6339 static struct rtnl_link_stats64 *
6340 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6342 struct rtl8169_private *tp = netdev_priv(dev);
6343 void __iomem *ioaddr = tp->mmio_addr;
6346 if (netif_running(dev))
6347 rtl8169_rx_missed(dev, ioaddr);
6350 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6351 stats->rx_packets = tp->rx_stats.packets;
6352 stats->rx_bytes = tp->rx_stats.bytes;
6353 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6357 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6358 stats->tx_packets = tp->tx_stats.packets;
6359 stats->tx_bytes = tp->tx_stats.bytes;
6360 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6362 stats->rx_dropped = dev->stats.rx_dropped;
6363 stats->tx_dropped = dev->stats.tx_dropped;
6364 stats->rx_length_errors = dev->stats.rx_length_errors;
6365 stats->rx_errors = dev->stats.rx_errors;
6366 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6367 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6368 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6373 static void rtl8169_net_suspend(struct net_device *dev)
6375 struct rtl8169_private *tp = netdev_priv(dev);
6377 if (!netif_running(dev))
6380 netif_device_detach(dev);
6381 netif_stop_queue(dev);
6384 napi_disable(&tp->napi);
6385 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6386 rtl_unlock_work(tp);
6388 rtl_pll_power_down(tp);
6393 static int rtl8169_suspend(struct device *device)
6395 struct pci_dev *pdev = to_pci_dev(device);
6396 struct net_device *dev = pci_get_drvdata(pdev);
6398 rtl8169_net_suspend(dev);
6403 static void __rtl8169_resume(struct net_device *dev)
6405 struct rtl8169_private *tp = netdev_priv(dev);
6407 netif_device_attach(dev);
6409 rtl_pll_power_up(tp);
6412 napi_enable(&tp->napi);
6413 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6414 rtl_unlock_work(tp);
6416 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6419 static int rtl8169_resume(struct device *device)
6421 struct pci_dev *pdev = to_pci_dev(device);
6422 struct net_device *dev = pci_get_drvdata(pdev);
6423 struct rtl8169_private *tp = netdev_priv(dev);
6425 rtl8169_init_phy(dev, tp);
6427 if (netif_running(dev))
6428 __rtl8169_resume(dev);
6433 static int rtl8169_runtime_suspend(struct device *device)
6435 struct pci_dev *pdev = to_pci_dev(device);
6436 struct net_device *dev = pci_get_drvdata(pdev);
6437 struct rtl8169_private *tp = netdev_priv(dev);
6439 if (!tp->TxDescArray)
6443 tp->saved_wolopts = __rtl8169_get_wol(tp);
6444 __rtl8169_set_wol(tp, WAKE_ANY);
6445 rtl_unlock_work(tp);
6447 rtl8169_net_suspend(dev);
6452 static int rtl8169_runtime_resume(struct device *device)
6454 struct pci_dev *pdev = to_pci_dev(device);
6455 struct net_device *dev = pci_get_drvdata(pdev);
6456 struct rtl8169_private *tp = netdev_priv(dev);
6458 if (!tp->TxDescArray)
6462 __rtl8169_set_wol(tp, tp->saved_wolopts);
6463 tp->saved_wolopts = 0;
6464 rtl_unlock_work(tp);
6466 rtl8169_init_phy(dev, tp);
6468 __rtl8169_resume(dev);
6473 static int rtl8169_runtime_idle(struct device *device)
6475 struct pci_dev *pdev = to_pci_dev(device);
6476 struct net_device *dev = pci_get_drvdata(pdev);
6477 struct rtl8169_private *tp = netdev_priv(dev);
6479 return tp->TxDescArray ? -EBUSY : 0;
6482 static const struct dev_pm_ops rtl8169_pm_ops = {
6483 .suspend = rtl8169_suspend,
6484 .resume = rtl8169_resume,
6485 .freeze = rtl8169_suspend,
6486 .thaw = rtl8169_resume,
6487 .poweroff = rtl8169_suspend,
6488 .restore = rtl8169_resume,
6489 .runtime_suspend = rtl8169_runtime_suspend,
6490 .runtime_resume = rtl8169_runtime_resume,
6491 .runtime_idle = rtl8169_runtime_idle,
6494 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6496 #else /* !CONFIG_PM */
6498 #define RTL8169_PM_OPS NULL
6500 #endif /* !CONFIG_PM */
6502 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6504 void __iomem *ioaddr = tp->mmio_addr;
6506 /* WoL fails with 8168b when the receiver is disabled. */
6507 switch (tp->mac_version) {
6508 case RTL_GIGA_MAC_VER_11:
6509 case RTL_GIGA_MAC_VER_12:
6510 case RTL_GIGA_MAC_VER_17:
6511 pci_clear_master(tp->pci_dev);
6513 RTL_W8(ChipCmd, CmdRxEnb);
6522 static void rtl_shutdown(struct pci_dev *pdev)
6524 struct net_device *dev = pci_get_drvdata(pdev);
6525 struct rtl8169_private *tp = netdev_priv(dev);
6526 struct device *d = &pdev->dev;
6528 pm_runtime_get_sync(d);
6530 rtl8169_net_suspend(dev);
6532 /* Restore original MAC address */
6533 rtl_rar_set(tp, dev->perm_addr);
6535 rtl8169_hw_reset(tp);
6537 if (system_state == SYSTEM_POWER_OFF) {
6538 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6539 rtl_wol_suspend_quirk(tp);
6540 rtl_wol_shutdown_quirk(tp);
6543 pci_wake_from_d3(pdev, true);
6544 pci_set_power_state(pdev, PCI_D3hot);
6547 pm_runtime_put_noidle(d);
6550 static void rtl_remove_one(struct pci_dev *pdev)
6552 struct net_device *dev = pci_get_drvdata(pdev);
6553 struct rtl8169_private *tp = netdev_priv(dev);
6555 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6556 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6557 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6558 rtl8168_driver_stop(tp);
6561 cancel_work_sync(&tp->wk.work);
6563 netif_napi_del(&tp->napi);
6565 unregister_netdev(dev);
6567 rtl_release_firmware(tp);
6569 if (pci_dev_run_wake(pdev))
6570 pm_runtime_get_noresume(&pdev->dev);
6572 /* restore original MAC address */
6573 rtl_rar_set(tp, dev->perm_addr);
6575 rtl_disable_msi(pdev, tp);
6576 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6577 pci_set_drvdata(pdev, NULL);
6580 static const struct net_device_ops rtl_netdev_ops = {
6581 .ndo_open = rtl_open,
6582 .ndo_stop = rtl8169_close,
6583 .ndo_get_stats64 = rtl8169_get_stats64,
6584 .ndo_start_xmit = rtl8169_start_xmit,
6585 .ndo_tx_timeout = rtl8169_tx_timeout,
6586 .ndo_validate_addr = eth_validate_addr,
6587 .ndo_change_mtu = rtl8169_change_mtu,
6588 .ndo_fix_features = rtl8169_fix_features,
6589 .ndo_set_features = rtl8169_set_features,
6590 .ndo_set_mac_address = rtl_set_mac_address,
6591 .ndo_do_ioctl = rtl8169_ioctl,
6592 .ndo_set_rx_mode = rtl_set_rx_mode,
6593 #ifdef CONFIG_NET_POLL_CONTROLLER
6594 .ndo_poll_controller = rtl8169_netpoll,
6599 static const struct rtl_cfg_info {
6600 void (*hw_start)(struct net_device *);
6601 unsigned int region;
6606 } rtl_cfg_infos [] = {
6608 .hw_start = rtl_hw_start_8169,
6611 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6612 .features = RTL_FEATURE_GMII,
6613 .default_ver = RTL_GIGA_MAC_VER_01,
6616 .hw_start = rtl_hw_start_8168,
6619 .event_slow = SYSErr | LinkChg | RxOverflow,
6620 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6621 .default_ver = RTL_GIGA_MAC_VER_11,
6624 .hw_start = rtl_hw_start_8101,
6627 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6629 .features = RTL_FEATURE_MSI,
6630 .default_ver = RTL_GIGA_MAC_VER_13,
6634 /* Cfg9346_Unlock assumed. */
6635 static unsigned rtl_try_msi(struct rtl8169_private *tp,
6636 const struct rtl_cfg_info *cfg)
6638 void __iomem *ioaddr = tp->mmio_addr;
6642 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6643 if (cfg->features & RTL_FEATURE_MSI) {
6644 if (pci_enable_msi(tp->pci_dev)) {
6645 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6648 msi = RTL_FEATURE_MSI;
6651 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6652 RTL_W8(Config2, cfg2);
6656 DECLARE_RTL_COND(rtl_link_list_ready_cond)
6658 void __iomem *ioaddr = tp->mmio_addr;
6660 return RTL_R8(MCU) & LINK_LIST_RDY;
6663 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6665 void __iomem *ioaddr = tp->mmio_addr;
6667 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
6670 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
6672 void __iomem *ioaddr = tp->mmio_addr;
6675 tp->ocp_base = OCP_STD_PHY_BASE;
6677 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
6679 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6682 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6685 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6687 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6689 data = r8168_mac_ocp_read(tp, 0xe8de);
6691 r8168_mac_ocp_write(tp, 0xe8de, data);
6693 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6696 data = r8168_mac_ocp_read(tp, 0xe8de);
6698 r8168_mac_ocp_write(tp, 0xe8de, data);
6700 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6704 static void rtl_hw_initialize(struct rtl8169_private *tp)
6706 switch (tp->mac_version) {
6707 case RTL_GIGA_MAC_VER_40:
6708 case RTL_GIGA_MAC_VER_41:
6709 rtl_hw_init_8168g(tp);
6718 rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6720 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6721 const unsigned int region = cfg->region;
6722 struct rtl8169_private *tp;
6723 struct mii_if_info *mii;
6724 struct net_device *dev;
6725 void __iomem *ioaddr;
6729 if (netif_msg_drv(&debug)) {
6730 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6731 MODULENAME, RTL8169_VERSION);
6734 dev = alloc_etherdev(sizeof (*tp));
6740 SET_NETDEV_DEV(dev, &pdev->dev);
6741 dev->netdev_ops = &rtl_netdev_ops;
6742 tp = netdev_priv(dev);
6745 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6749 mii->mdio_read = rtl_mdio_read;
6750 mii->mdio_write = rtl_mdio_write;
6751 mii->phy_id_mask = 0x1f;
6752 mii->reg_num_mask = 0x1f;
6753 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6755 /* disable ASPM completely as that cause random device stop working
6756 * problems as well as full system hangs for some PCIe devices users */
6757 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6758 PCIE_LINK_STATE_CLKPM);
6760 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6761 rc = pci_enable_device(pdev);
6763 netif_err(tp, probe, dev, "enable failure\n");
6764 goto err_out_free_dev_1;
6767 if (pci_set_mwi(pdev) < 0)
6768 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6770 /* make sure PCI base addr 1 is MMIO */
6771 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6772 netif_err(tp, probe, dev,
6773 "region #%d not an MMIO resource, aborting\n",
6779 /* check for weird/broken PCI region reporting */
6780 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6781 netif_err(tp, probe, dev,
6782 "Invalid PCI region size(s), aborting\n");
6787 rc = pci_request_regions(pdev, MODULENAME);
6789 netif_err(tp, probe, dev, "could not request regions\n");
6793 tp->cp_cmd = RxChkSum;
6795 if ((sizeof(dma_addr_t) > 4) &&
6796 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6797 tp->cp_cmd |= PCIDAC;
6798 dev->features |= NETIF_F_HIGHDMA;
6800 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6802 netif_err(tp, probe, dev, "DMA configuration failed\n");
6803 goto err_out_free_res_3;
6807 /* ioremap MMIO region */
6808 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6810 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6812 goto err_out_free_res_3;
6814 tp->mmio_addr = ioaddr;
6816 if (!pci_is_pcie(pdev))
6817 netif_info(tp, probe, dev, "not PCI Express\n");
6819 /* Identify chip attached to board */
6820 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6824 rtl_irq_disable(tp);
6826 rtl_hw_initialize(tp);
6830 rtl_ack_events(tp, 0xffff);
6832 pci_set_master(pdev);
6835 * Pretend we are using VLANs; This bypasses a nasty bug where
6836 * Interrupts stop flowing on high load on 8110SCd controllers.
6838 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6839 tp->cp_cmd |= RxVlan;
6841 rtl_init_mdio_ops(tp);
6842 rtl_init_pll_power_ops(tp);
6843 rtl_init_jumbo_ops(tp);
6844 rtl_init_csi_ops(tp);
6846 rtl8169_print_mac_version(tp);
6848 chipset = tp->mac_version;
6849 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6851 RTL_W8(Cfg9346, Cfg9346_Unlock);
6852 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6853 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6854 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6855 tp->features |= RTL_FEATURE_WOL;
6856 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6857 tp->features |= RTL_FEATURE_WOL;
6858 tp->features |= rtl_try_msi(tp, cfg);
6859 RTL_W8(Cfg9346, Cfg9346_Lock);
6861 if (rtl_tbi_enabled(tp)) {
6862 tp->set_speed = rtl8169_set_speed_tbi;
6863 tp->get_settings = rtl8169_gset_tbi;
6864 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6865 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6866 tp->link_ok = rtl8169_tbi_link_ok;
6867 tp->do_ioctl = rtl_tbi_ioctl;
6869 tp->set_speed = rtl8169_set_speed_xmii;
6870 tp->get_settings = rtl8169_gset_xmii;
6871 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6872 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6873 tp->link_ok = rtl8169_xmii_link_ok;
6874 tp->do_ioctl = rtl_xmii_ioctl;
6877 mutex_init(&tp->wk.mutex);
6879 /* Get MAC address */
6880 for (i = 0; i < ETH_ALEN; i++)
6881 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6883 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6884 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
6886 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6888 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6889 * properly for all devices */
6890 dev->features |= NETIF_F_RXCSUM |
6891 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6893 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6894 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6895 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6898 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6899 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6900 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6902 dev->hw_features |= NETIF_F_RXALL;
6903 dev->hw_features |= NETIF_F_RXFCS;
6905 tp->hw_start = cfg->hw_start;
6906 tp->event_slow = cfg->event_slow;
6908 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6909 ~(RxBOVF | RxFOVF) : ~0;
6911 init_timer(&tp->timer);
6912 tp->timer.data = (unsigned long) dev;
6913 tp->timer.function = rtl8169_phy_timer;
6915 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6917 rc = register_netdev(dev);
6921 pci_set_drvdata(pdev, dev);
6923 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6924 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6925 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
6926 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6927 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6928 "tx checksumming: %s]\n",
6929 rtl_chip_infos[chipset].jumbo_max,
6930 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6933 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6934 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6935 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6936 rtl8168_driver_start(tp);
6939 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6941 if (pci_dev_run_wake(pdev))
6942 pm_runtime_put_noidle(&pdev->dev);
6944 netif_carrier_off(dev);
6950 netif_napi_del(&tp->napi);
6951 rtl_disable_msi(pdev, tp);
6954 pci_release_regions(pdev);
6956 pci_clear_mwi(pdev);
6957 pci_disable_device(pdev);
6963 static struct pci_driver rtl8169_pci_driver = {
6965 .id_table = rtl8169_pci_tbl,
6966 .probe = rtl_init_one,
6967 .remove = rtl_remove_one,
6968 .shutdown = rtl_shutdown,
6969 .driver.pm = RTL8169_PM_OPS,
6972 module_pci_driver(rtl8169_pci_driver);