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>
32 #include <asm/system.h>
36 #define RTL8169_VERSION "2.3LK-NAPI"
37 #define MODULENAME "r8169"
38 #define PFX MODULENAME ": "
40 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
42 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
44 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
45 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
47 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
50 #define assert(expr) \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
53 #expr,__FILE__,__func__,__LINE__); \
55 #define dprintk(fmt, args...) \
56 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
58 #define assert(expr) do {} while (0)
59 #define dprintk(fmt, args...) do {} while (0)
60 #endif /* RTL8169_DEBUG */
62 #define R8169_MSG_DEFAULT \
63 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
65 #define TX_SLOTS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
68 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
69 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
70 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
72 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
73 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
74 static const int multicast_filter_limit = 32;
76 /* MAC address length */
77 #define MAC_ADDR_LEN 6
79 #define MAX_READ_REQUEST_SHIFT 12
80 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
81 #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
82 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
84 #define R8169_REGS_SIZE 256
85 #define R8169_NAPI_WEIGHT 64
86 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
87 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
88 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
89 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
90 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
92 #define RTL8169_TX_TIMEOUT (6*HZ)
93 #define RTL8169_PHY_TIMEOUT (10*HZ)
95 #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
96 #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
97 #define RTL_EEPROM_SIG_ADDR 0x0000
99 /* write/read MMIO register */
100 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
101 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
102 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
103 #define RTL_R8(reg) readb (ioaddr + (reg))
104 #define RTL_R16(reg) readw (ioaddr + (reg))
105 #define RTL_R32(reg) readl (ioaddr + (reg))
108 RTL_GIGA_MAC_VER_01 = 0,
144 RTL_GIGA_MAC_NONE = 0xff,
147 enum rtl_tx_desc_version {
152 #define JUMBO_1K ETH_DATA_LEN
153 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
154 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
155 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
156 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
158 #define _R(NAME,TD,FW,SZ,B) { \
166 static const struct {
168 enum rtl_tx_desc_version txd_version;
172 } rtl_chip_infos[] = {
174 [RTL_GIGA_MAC_VER_01] =
175 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
176 [RTL_GIGA_MAC_VER_02] =
177 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
178 [RTL_GIGA_MAC_VER_03] =
179 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
180 [RTL_GIGA_MAC_VER_04] =
181 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
182 [RTL_GIGA_MAC_VER_05] =
183 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
184 [RTL_GIGA_MAC_VER_06] =
185 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
187 [RTL_GIGA_MAC_VER_07] =
188 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
189 [RTL_GIGA_MAC_VER_08] =
190 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
191 [RTL_GIGA_MAC_VER_09] =
192 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
193 [RTL_GIGA_MAC_VER_10] =
194 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
195 [RTL_GIGA_MAC_VER_11] =
196 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
197 [RTL_GIGA_MAC_VER_12] =
198 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
199 [RTL_GIGA_MAC_VER_13] =
200 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
201 [RTL_GIGA_MAC_VER_14] =
202 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
203 [RTL_GIGA_MAC_VER_15] =
204 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
205 [RTL_GIGA_MAC_VER_16] =
206 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
207 [RTL_GIGA_MAC_VER_17] =
208 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
209 [RTL_GIGA_MAC_VER_18] =
210 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
211 [RTL_GIGA_MAC_VER_19] =
212 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
213 [RTL_GIGA_MAC_VER_20] =
214 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
215 [RTL_GIGA_MAC_VER_21] =
216 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
217 [RTL_GIGA_MAC_VER_22] =
218 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
219 [RTL_GIGA_MAC_VER_23] =
220 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
221 [RTL_GIGA_MAC_VER_24] =
222 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
223 [RTL_GIGA_MAC_VER_25] =
224 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
226 [RTL_GIGA_MAC_VER_26] =
227 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
229 [RTL_GIGA_MAC_VER_27] =
230 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
231 [RTL_GIGA_MAC_VER_28] =
232 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
233 [RTL_GIGA_MAC_VER_29] =
234 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
236 [RTL_GIGA_MAC_VER_30] =
237 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
239 [RTL_GIGA_MAC_VER_31] =
240 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
241 [RTL_GIGA_MAC_VER_32] =
242 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
244 [RTL_GIGA_MAC_VER_33] =
245 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
247 [RTL_GIGA_MAC_VER_34] =
248 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
250 [RTL_GIGA_MAC_VER_35] =
251 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
253 [RTL_GIGA_MAC_VER_36] =
254 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
265 static void rtl_hw_start_8169(struct net_device *);
266 static void rtl_hw_start_8168(struct net_device *);
267 static void rtl_hw_start_8101(struct net_device *);
269 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
270 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
271 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
272 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
273 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
274 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
275 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
276 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
277 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
278 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
279 { PCI_VENDOR_ID_LINKSYS, 0x1032,
280 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
282 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
286 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
288 static int rx_buf_sz = 16383;
295 MAC0 = 0, /* Ethernet hardware address. */
297 MAR0 = 8, /* Multicast filter. */
298 CounterAddrLow = 0x10,
299 CounterAddrHigh = 0x14,
300 TxDescStartAddrLow = 0x20,
301 TxDescStartAddrHigh = 0x24,
302 TxHDescStartAddrLow = 0x28,
303 TxHDescStartAddrHigh = 0x2c,
312 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
313 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
316 #define RX128_INT_EN (1 << 15) /* 8111c and later */
317 #define RX_MULTI_EN (1 << 14) /* 8111c only */
318 #define RXCFG_FIFO_SHIFT 13
319 /* No threshold before first PCI xfer */
320 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
321 #define RXCFG_DMA_SHIFT 8
322 /* Unlimited maximum PCI burst. */
323 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
330 #define PME_SIGNAL (1 << 5) /* 8168c and later */
341 RxDescAddrLow = 0xe4,
342 RxDescAddrHigh = 0xe8,
343 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
345 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
347 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
349 #define TxPacketMax (8064 >> 7)
350 #define EarlySize 0x27
353 FuncEventMask = 0xf4,
354 FuncPresetState = 0xf8,
355 FuncForceEvent = 0xfc,
358 enum rtl8110_registers {
364 enum rtl8168_8101_registers {
367 #define CSIAR_FLAG 0x80000000
368 #define CSIAR_WRITE_CMD 0x80000000
369 #define CSIAR_BYTE_ENABLE 0x0f
370 #define CSIAR_BYTE_ENABLE_SHIFT 12
371 #define CSIAR_ADDR_MASK 0x0fff
374 #define EPHYAR_FLAG 0x80000000
375 #define EPHYAR_WRITE_CMD 0x80000000
376 #define EPHYAR_REG_MASK 0x1f
377 #define EPHYAR_REG_SHIFT 16
378 #define EPHYAR_DATA_MASK 0xffff
380 #define PFM_EN (1 << 6)
382 #define FIX_NAK_1 (1 << 4)
383 #define FIX_NAK_2 (1 << 3)
386 #define NOW_IS_OOB (1 << 7)
387 #define EN_NDP (1 << 3)
388 #define EN_OOB_RESET (1 << 2)
390 #define EFUSEAR_FLAG 0x80000000
391 #define EFUSEAR_WRITE_CMD 0x80000000
392 #define EFUSEAR_READ_CMD 0x00000000
393 #define EFUSEAR_REG_MASK 0x03ff
394 #define EFUSEAR_REG_SHIFT 8
395 #define EFUSEAR_DATA_MASK 0xff
398 enum rtl8168_registers {
403 #define ERIAR_FLAG 0x80000000
404 #define ERIAR_WRITE_CMD 0x80000000
405 #define ERIAR_READ_CMD 0x00000000
406 #define ERIAR_ADDR_BYTE_ALIGN 4
407 #define ERIAR_TYPE_SHIFT 16
408 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
409 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
410 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
411 #define ERIAR_MASK_SHIFT 12
412 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
413 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
414 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
415 EPHY_RXER_NUM = 0x7c,
416 OCPDR = 0xb0, /* OCP GPHY access */
417 #define OCPDR_WRITE_CMD 0x80000000
418 #define OCPDR_READ_CMD 0x00000000
419 #define OCPDR_REG_MASK 0x7f
420 #define OCPDR_GPHY_REG_SHIFT 16
421 #define OCPDR_DATA_MASK 0xffff
423 #define OCPAR_FLAG 0x80000000
424 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
425 #define OCPAR_GPHY_READ_CMD 0x0000f060
426 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
427 MISC = 0xf0, /* 8168e only. */
428 #define TXPLA_RST (1 << 29)
429 #define PWM_EN (1 << 22)
432 enum rtl_register_content {
433 /* InterruptStatusBits */
437 TxDescUnavail = 0x0080,
461 /* TXPoll register p.5 */
462 HPQ = 0x80, /* Poll cmd on the high prio queue */
463 NPQ = 0x40, /* Poll cmd on the low prio queue */
464 FSWInt = 0x01, /* Forced software interrupt */
468 Cfg9346_Unlock = 0xc0,
473 AcceptBroadcast = 0x08,
474 AcceptMulticast = 0x04,
476 AcceptAllPhys = 0x01,
477 #define RX_CONFIG_ACCEPT_MASK 0x3f
480 TxInterFrameGapShift = 24,
481 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
483 /* Config1 register p.24 */
486 Speed_down = (1 << 4),
490 PMEnable = (1 << 0), /* Power Management Enable */
492 /* Config2 register p. 25 */
493 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
494 PCI_Clock_66MHz = 0x01,
495 PCI_Clock_33MHz = 0x00,
497 /* Config3 register p.25 */
498 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
499 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
500 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
501 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
503 /* Config4 register */
504 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
506 /* Config5 register p.27 */
507 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
508 MWF = (1 << 5), /* Accept Multicast wakeup frame */
509 UWF = (1 << 4), /* Accept Unicast wakeup frame */
511 LanWake = (1 << 1), /* LanWake enable/disable */
512 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
515 TBIReset = 0x80000000,
516 TBILoopback = 0x40000000,
517 TBINwEnable = 0x20000000,
518 TBINwRestart = 0x10000000,
519 TBILinkOk = 0x02000000,
520 TBINwComplete = 0x01000000,
523 EnableBist = (1 << 15), // 8168 8101
524 Mac_dbgo_oe = (1 << 14), // 8168 8101
525 Normal_mode = (1 << 13), // unused
526 Force_half_dup = (1 << 12), // 8168 8101
527 Force_rxflow_en = (1 << 11), // 8168 8101
528 Force_txflow_en = (1 << 10), // 8168 8101
529 Cxpl_dbg_sel = (1 << 9), // 8168 8101
530 ASF = (1 << 8), // 8168 8101
531 PktCntrDisable = (1 << 7), // 8168 8101
532 Mac_dbgo_sel = 0x001c, // 8168
537 INTT_0 = 0x0000, // 8168
538 INTT_1 = 0x0001, // 8168
539 INTT_2 = 0x0002, // 8168
540 INTT_3 = 0x0003, // 8168
542 /* rtl8169_PHYstatus */
553 TBILinkOK = 0x02000000,
555 /* DumpCounterCommand */
560 /* First doubleword. */
561 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
562 RingEnd = (1 << 30), /* End of descriptor ring */
563 FirstFrag = (1 << 29), /* First segment of a packet */
564 LastFrag = (1 << 28), /* Final segment of a packet */
568 enum rtl_tx_desc_bit {
569 /* First doubleword. */
570 TD_LSO = (1 << 27), /* Large Send Offload */
571 #define TD_MSS_MAX 0x07ffu /* MSS value */
573 /* Second doubleword. */
574 TxVlanTag = (1 << 17), /* Add VLAN tag */
577 /* 8169, 8168b and 810x except 8102e. */
578 enum rtl_tx_desc_bit_0 {
579 /* First doubleword. */
580 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
581 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
582 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
583 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
586 /* 8102e, 8168c and beyond. */
587 enum rtl_tx_desc_bit_1 {
588 /* Second doubleword. */
589 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
590 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
591 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
592 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
595 static const struct rtl_tx_desc_info {
602 } tx_desc_info [] = {
605 .udp = TD0_IP_CS | TD0_UDP_CS,
606 .tcp = TD0_IP_CS | TD0_TCP_CS
608 .mss_shift = TD0_MSS_SHIFT,
613 .udp = TD1_IP_CS | TD1_UDP_CS,
614 .tcp = TD1_IP_CS | TD1_TCP_CS
616 .mss_shift = TD1_MSS_SHIFT,
621 enum rtl_rx_desc_bit {
623 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
624 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
626 #define RxProtoUDP (PID1)
627 #define RxProtoTCP (PID0)
628 #define RxProtoIP (PID1 | PID0)
629 #define RxProtoMask RxProtoIP
631 IPFail = (1 << 16), /* IP checksum failed */
632 UDPFail = (1 << 15), /* UDP/IP checksum failed */
633 TCPFail = (1 << 14), /* TCP/IP checksum failed */
634 RxVlanTag = (1 << 16), /* VLAN tag available */
637 #define RsvdMask 0x3fffc000
654 u8 __pad[sizeof(void *) - sizeof(u32)];
658 RTL_FEATURE_WOL = (1 << 0),
659 RTL_FEATURE_MSI = (1 << 1),
660 RTL_FEATURE_GMII = (1 << 2),
663 struct rtl8169_counters {
670 __le32 tx_one_collision;
671 __le32 tx_multi_collision;
679 struct rtl8169_private {
680 void __iomem *mmio_addr; /* memory map physical address */
681 struct pci_dev *pci_dev;
682 struct net_device *dev;
683 struct napi_struct napi;
688 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
689 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
692 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
693 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
694 dma_addr_t TxPhyAddr;
695 dma_addr_t RxPhyAddr;
696 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
697 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
698 struct timer_list timer;
705 void (*write)(void __iomem *, int, int);
706 int (*read)(void __iomem *, int);
709 struct pll_power_ops {
710 void (*down)(struct rtl8169_private *);
711 void (*up)(struct rtl8169_private *);
715 void (*enable)(struct rtl8169_private *);
716 void (*disable)(struct rtl8169_private *);
719 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
720 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
721 void (*phy_reset_enable)(struct rtl8169_private *tp);
722 void (*hw_start)(struct net_device *);
723 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
724 unsigned int (*link_ok)(void __iomem *);
725 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
726 struct delayed_work task;
729 struct mii_if_info mii;
730 struct rtl8169_counters counters;
735 const struct firmware *fw;
737 #define RTL_VER_SIZE 32
739 char version[RTL_VER_SIZE];
741 struct rtl_fw_phy_action {
746 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
749 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
750 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
751 module_param(use_dac, int, 0);
752 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
753 module_param_named(debug, debug.msg_enable, int, 0);
754 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
755 MODULE_LICENSE("GPL");
756 MODULE_VERSION(RTL8169_VERSION);
757 MODULE_FIRMWARE(FIRMWARE_8168D_1);
758 MODULE_FIRMWARE(FIRMWARE_8168D_2);
759 MODULE_FIRMWARE(FIRMWARE_8168E_1);
760 MODULE_FIRMWARE(FIRMWARE_8168E_2);
761 MODULE_FIRMWARE(FIRMWARE_8168E_3);
762 MODULE_FIRMWARE(FIRMWARE_8105E_1);
763 MODULE_FIRMWARE(FIRMWARE_8168F_1);
764 MODULE_FIRMWARE(FIRMWARE_8168F_2);
766 static int rtl8169_open(struct net_device *dev);
767 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
768 struct net_device *dev);
769 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
770 static int rtl8169_init_ring(struct net_device *dev);
771 static void rtl_hw_start(struct net_device *dev);
772 static int rtl8169_close(struct net_device *dev);
773 static void rtl_set_rx_mode(struct net_device *dev);
774 static void rtl8169_tx_timeout(struct net_device *dev);
775 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
776 static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
777 void __iomem *, u32 budget);
778 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
779 static void rtl8169_down(struct net_device *dev);
780 static void rtl8169_rx_clear(struct rtl8169_private *tp);
781 static int rtl8169_poll(struct napi_struct *napi, int budget);
783 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
785 int cap = pci_pcie_cap(pdev);
790 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
791 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
792 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
796 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
798 void __iomem *ioaddr = tp->mmio_addr;
801 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
802 for (i = 0; i < 20; i++) {
804 if (RTL_R32(OCPAR) & OCPAR_FLAG)
807 return RTL_R32(OCPDR);
810 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
812 void __iomem *ioaddr = tp->mmio_addr;
815 RTL_W32(OCPDR, data);
816 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
817 for (i = 0; i < 20; i++) {
819 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
824 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
826 void __iomem *ioaddr = tp->mmio_addr;
830 RTL_W32(ERIAR, 0x800010e8);
832 for (i = 0; i < 5; i++) {
834 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
838 ocp_write(tp, 0x1, 0x30, 0x00000001);
841 #define OOB_CMD_RESET 0x00
842 #define OOB_CMD_DRIVER_START 0x05
843 #define OOB_CMD_DRIVER_STOP 0x06
845 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
847 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
850 static void rtl8168_driver_start(struct rtl8169_private *tp)
855 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
857 reg = rtl8168_get_ocp_reg(tp);
859 for (i = 0; i < 10; i++) {
861 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
866 static void rtl8168_driver_stop(struct rtl8169_private *tp)
871 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
873 reg = rtl8168_get_ocp_reg(tp);
875 for (i = 0; i < 10; i++) {
877 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
882 static int r8168dp_check_dash(struct rtl8169_private *tp)
884 u16 reg = rtl8168_get_ocp_reg(tp);
886 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
889 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
893 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
895 for (i = 20; i > 0; i--) {
897 * Check if the RTL8169 has completed writing to the specified
900 if (!(RTL_R32(PHYAR) & 0x80000000))
905 * According to hardware specs a 20us delay is required after write
906 * complete indication, but before sending next command.
911 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
915 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
917 for (i = 20; i > 0; i--) {
919 * Check if the RTL8169 has completed retrieving data from
920 * the specified MII register.
922 if (RTL_R32(PHYAR) & 0x80000000) {
923 value = RTL_R32(PHYAR) & 0xffff;
929 * According to hardware specs a 20us delay is required after read
930 * complete indication, but before sending next command.
937 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
941 RTL_W32(OCPDR, data |
942 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
943 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
944 RTL_W32(EPHY_RXER_NUM, 0);
946 for (i = 0; i < 100; i++) {
948 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
953 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
955 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
956 (value & OCPDR_DATA_MASK));
959 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
963 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
966 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
967 RTL_W32(EPHY_RXER_NUM, 0);
969 for (i = 0; i < 100; i++) {
971 if (RTL_R32(OCPAR) & OCPAR_FLAG)
975 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
978 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
980 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
982 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
985 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
987 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
990 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
992 r8168dp_2_mdio_start(ioaddr);
994 r8169_mdio_write(ioaddr, reg_addr, value);
996 r8168dp_2_mdio_stop(ioaddr);
999 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1003 r8168dp_2_mdio_start(ioaddr);
1005 value = r8169_mdio_read(ioaddr, reg_addr);
1007 r8168dp_2_mdio_stop(ioaddr);
1012 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1014 tp->mdio_ops.write(tp->mmio_addr, location, val);
1017 static int rtl_readphy(struct rtl8169_private *tp, int location)
1019 return tp->mdio_ops.read(tp->mmio_addr, location);
1022 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1024 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1027 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1031 val = rtl_readphy(tp, reg_addr);
1032 rtl_writephy(tp, reg_addr, (val | p) & ~m);
1035 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1038 struct rtl8169_private *tp = netdev_priv(dev);
1040 rtl_writephy(tp, location, val);
1043 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1045 struct rtl8169_private *tp = netdev_priv(dev);
1047 return rtl_readphy(tp, location);
1050 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1054 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1055 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1057 for (i = 0; i < 100; i++) {
1058 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1064 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1069 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1071 for (i = 0; i < 100; i++) {
1072 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1073 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1082 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1086 RTL_W32(CSIDR, value);
1087 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1088 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1090 for (i = 0; i < 100; i++) {
1091 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1097 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1102 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1103 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1105 for (i = 0; i < 100; i++) {
1106 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1107 value = RTL_R32(CSIDR);
1117 void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1121 BUG_ON((addr & 3) || (mask == 0));
1122 RTL_W32(ERIDR, val);
1123 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1125 for (i = 0; i < 100; i++) {
1126 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1132 static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1137 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1139 for (i = 0; i < 100; i++) {
1140 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1141 value = RTL_R32(ERIDR);
1151 rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1155 val = rtl_eri_read(ioaddr, addr, type);
1156 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1165 static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1166 const struct exgmac_reg *r, int len)
1169 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1174 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1179 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1181 for (i = 0; i < 300; i++) {
1182 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1183 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1192 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1194 void __iomem *ioaddr = tp->mmio_addr;
1196 RTL_W16(IntrMask, 0x0000);
1197 RTL_W16(IntrStatus, tp->intr_event);
1201 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1203 void __iomem *ioaddr = tp->mmio_addr;
1205 return RTL_R32(TBICSR) & TBIReset;
1208 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1210 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1213 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1215 return RTL_R32(TBICSR) & TBILinkOk;
1218 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1220 return RTL_R8(PHYstatus) & LinkStatus;
1223 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1225 void __iomem *ioaddr = tp->mmio_addr;
1227 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1230 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1234 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1235 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1238 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1240 void __iomem *ioaddr = tp->mmio_addr;
1241 struct net_device *dev = tp->dev;
1243 if (!netif_running(dev))
1246 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1247 if (RTL_R8(PHYstatus) & _1000bpsF) {
1248 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1249 0x00000011, ERIAR_EXGMAC);
1250 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1251 0x00000005, ERIAR_EXGMAC);
1252 } else if (RTL_R8(PHYstatus) & _100bps) {
1253 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1254 0x0000001f, ERIAR_EXGMAC);
1255 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1256 0x00000005, ERIAR_EXGMAC);
1258 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1259 0x0000001f, ERIAR_EXGMAC);
1260 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1261 0x0000003f, ERIAR_EXGMAC);
1263 /* Reset packet filter */
1264 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1266 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1268 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1269 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1270 if (RTL_R8(PHYstatus) & _1000bpsF) {
1271 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1272 0x00000011, ERIAR_EXGMAC);
1273 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1274 0x00000005, ERIAR_EXGMAC);
1276 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1277 0x0000001f, ERIAR_EXGMAC);
1278 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1279 0x0000003f, ERIAR_EXGMAC);
1284 static void __rtl8169_check_link_status(struct net_device *dev,
1285 struct rtl8169_private *tp,
1286 void __iomem *ioaddr, bool pm)
1288 unsigned long flags;
1290 spin_lock_irqsave(&tp->lock, flags);
1291 if (tp->link_ok(ioaddr)) {
1292 rtl_link_chg_patch(tp);
1293 /* This is to cancel a scheduled suspend if there's one. */
1295 pm_request_resume(&tp->pci_dev->dev);
1296 netif_carrier_on(dev);
1297 if (net_ratelimit())
1298 netif_info(tp, ifup, dev, "link up\n");
1300 netif_carrier_off(dev);
1301 netif_info(tp, ifdown, dev, "link down\n");
1303 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1305 spin_unlock_irqrestore(&tp->lock, flags);
1308 static void rtl8169_check_link_status(struct net_device *dev,
1309 struct rtl8169_private *tp,
1310 void __iomem *ioaddr)
1312 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1315 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1317 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1319 void __iomem *ioaddr = tp->mmio_addr;
1323 options = RTL_R8(Config1);
1324 if (!(options & PMEnable))
1327 options = RTL_R8(Config3);
1328 if (options & LinkUp)
1329 wolopts |= WAKE_PHY;
1330 if (options & MagicPacket)
1331 wolopts |= WAKE_MAGIC;
1333 options = RTL_R8(Config5);
1335 wolopts |= WAKE_UCAST;
1337 wolopts |= WAKE_BCAST;
1339 wolopts |= WAKE_MCAST;
1344 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1346 struct rtl8169_private *tp = netdev_priv(dev);
1348 spin_lock_irq(&tp->lock);
1350 wol->supported = WAKE_ANY;
1351 wol->wolopts = __rtl8169_get_wol(tp);
1353 spin_unlock_irq(&tp->lock);
1356 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1358 void __iomem *ioaddr = tp->mmio_addr;
1360 static const struct {
1365 { WAKE_PHY, Config3, LinkUp },
1366 { WAKE_MAGIC, Config3, MagicPacket },
1367 { WAKE_UCAST, Config5, UWF },
1368 { WAKE_BCAST, Config5, BWF },
1369 { WAKE_MCAST, Config5, MWF },
1370 { WAKE_ANY, Config5, LanWake }
1374 RTL_W8(Cfg9346, Cfg9346_Unlock);
1376 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1377 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1378 if (wolopts & cfg[i].opt)
1379 options |= cfg[i].mask;
1380 RTL_W8(cfg[i].reg, options);
1383 switch (tp->mac_version) {
1384 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1385 options = RTL_R8(Config1) & ~PMEnable;
1387 options |= PMEnable;
1388 RTL_W8(Config1, options);
1391 options = RTL_R8(Config2) & ~PME_SIGNAL;
1393 options |= PME_SIGNAL;
1394 RTL_W8(Config2, options);
1398 RTL_W8(Cfg9346, Cfg9346_Lock);
1401 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1403 struct rtl8169_private *tp = netdev_priv(dev);
1405 spin_lock_irq(&tp->lock);
1408 tp->features |= RTL_FEATURE_WOL;
1410 tp->features &= ~RTL_FEATURE_WOL;
1411 __rtl8169_set_wol(tp, wol->wolopts);
1412 spin_unlock_irq(&tp->lock);
1414 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1419 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1421 return rtl_chip_infos[tp->mac_version].fw_name;
1424 static void rtl8169_get_drvinfo(struct net_device *dev,
1425 struct ethtool_drvinfo *info)
1427 struct rtl8169_private *tp = netdev_priv(dev);
1428 struct rtl_fw *rtl_fw = tp->rtl_fw;
1430 strcpy(info->driver, MODULENAME);
1431 strcpy(info->version, RTL8169_VERSION);
1432 strcpy(info->bus_info, pci_name(tp->pci_dev));
1433 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1434 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1438 static int rtl8169_get_regs_len(struct net_device *dev)
1440 return R8169_REGS_SIZE;
1443 static int rtl8169_set_speed_tbi(struct net_device *dev,
1444 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1446 struct rtl8169_private *tp = netdev_priv(dev);
1447 void __iomem *ioaddr = tp->mmio_addr;
1451 reg = RTL_R32(TBICSR);
1452 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1453 (duplex == DUPLEX_FULL)) {
1454 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1455 } else if (autoneg == AUTONEG_ENABLE)
1456 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1458 netif_warn(tp, link, dev,
1459 "incorrect speed setting refused in TBI mode\n");
1466 static int rtl8169_set_speed_xmii(struct net_device *dev,
1467 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1469 struct rtl8169_private *tp = netdev_priv(dev);
1470 int giga_ctrl, bmcr;
1473 rtl_writephy(tp, 0x1f, 0x0000);
1475 if (autoneg == AUTONEG_ENABLE) {
1478 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1479 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1480 ADVERTISE_100HALF | ADVERTISE_100FULL);
1482 if (adv & ADVERTISED_10baseT_Half)
1483 auto_nego |= ADVERTISE_10HALF;
1484 if (adv & ADVERTISED_10baseT_Full)
1485 auto_nego |= ADVERTISE_10FULL;
1486 if (adv & ADVERTISED_100baseT_Half)
1487 auto_nego |= ADVERTISE_100HALF;
1488 if (adv & ADVERTISED_100baseT_Full)
1489 auto_nego |= ADVERTISE_100FULL;
1491 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1493 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1494 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1496 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1497 if (tp->mii.supports_gmii) {
1498 if (adv & ADVERTISED_1000baseT_Half)
1499 giga_ctrl |= ADVERTISE_1000HALF;
1500 if (adv & ADVERTISED_1000baseT_Full)
1501 giga_ctrl |= ADVERTISE_1000FULL;
1502 } else if (adv & (ADVERTISED_1000baseT_Half |
1503 ADVERTISED_1000baseT_Full)) {
1504 netif_info(tp, link, dev,
1505 "PHY does not support 1000Mbps\n");
1509 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1511 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1512 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1516 if (speed == SPEED_10)
1518 else if (speed == SPEED_100)
1519 bmcr = BMCR_SPEED100;
1523 if (duplex == DUPLEX_FULL)
1524 bmcr |= BMCR_FULLDPLX;
1527 rtl_writephy(tp, MII_BMCR, bmcr);
1529 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1530 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1531 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1532 rtl_writephy(tp, 0x17, 0x2138);
1533 rtl_writephy(tp, 0x0e, 0x0260);
1535 rtl_writephy(tp, 0x17, 0x2108);
1536 rtl_writephy(tp, 0x0e, 0x0000);
1545 static int rtl8169_set_speed(struct net_device *dev,
1546 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1548 struct rtl8169_private *tp = netdev_priv(dev);
1551 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1555 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1556 (advertising & ADVERTISED_1000baseT_Full)) {
1557 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1563 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1565 struct rtl8169_private *tp = netdev_priv(dev);
1566 unsigned long flags;
1569 del_timer_sync(&tp->timer);
1571 spin_lock_irqsave(&tp->lock, flags);
1572 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1573 cmd->duplex, cmd->advertising);
1574 spin_unlock_irqrestore(&tp->lock, flags);
1579 static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
1581 struct rtl8169_private *tp = netdev_priv(dev);
1583 if (dev->mtu > TD_MSS_MAX)
1584 features &= ~NETIF_F_ALL_TSO;
1586 if (dev->mtu > JUMBO_1K &&
1587 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1588 features &= ~NETIF_F_IP_CSUM;
1593 static int rtl8169_set_features(struct net_device *dev, u32 features)
1595 struct rtl8169_private *tp = netdev_priv(dev);
1596 void __iomem *ioaddr = tp->mmio_addr;
1597 unsigned long flags;
1599 spin_lock_irqsave(&tp->lock, flags);
1601 if (features & NETIF_F_RXCSUM)
1602 tp->cp_cmd |= RxChkSum;
1604 tp->cp_cmd &= ~RxChkSum;
1606 if (dev->features & NETIF_F_HW_VLAN_RX)
1607 tp->cp_cmd |= RxVlan;
1609 tp->cp_cmd &= ~RxVlan;
1611 RTL_W16(CPlusCmd, tp->cp_cmd);
1614 spin_unlock_irqrestore(&tp->lock, flags);
1619 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1620 struct sk_buff *skb)
1622 return (vlan_tx_tag_present(skb)) ?
1623 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1626 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1628 u32 opts2 = le32_to_cpu(desc->opts2);
1630 if (opts2 & RxVlanTag)
1631 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1634 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1636 struct rtl8169_private *tp = netdev_priv(dev);
1637 void __iomem *ioaddr = tp->mmio_addr;
1641 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1642 cmd->port = PORT_FIBRE;
1643 cmd->transceiver = XCVR_INTERNAL;
1645 status = RTL_R32(TBICSR);
1646 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1647 cmd->autoneg = !!(status & TBINwEnable);
1649 ethtool_cmd_speed_set(cmd, SPEED_1000);
1650 cmd->duplex = DUPLEX_FULL; /* Always set */
1655 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1657 struct rtl8169_private *tp = netdev_priv(dev);
1659 return mii_ethtool_gset(&tp->mii, cmd);
1662 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1664 struct rtl8169_private *tp = netdev_priv(dev);
1665 unsigned long flags;
1668 spin_lock_irqsave(&tp->lock, flags);
1670 rc = tp->get_settings(dev, cmd);
1672 spin_unlock_irqrestore(&tp->lock, flags);
1676 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1679 struct rtl8169_private *tp = netdev_priv(dev);
1680 unsigned long flags;
1682 if (regs->len > R8169_REGS_SIZE)
1683 regs->len = R8169_REGS_SIZE;
1685 spin_lock_irqsave(&tp->lock, flags);
1686 memcpy_fromio(p, tp->mmio_addr, regs->len);
1687 spin_unlock_irqrestore(&tp->lock, flags);
1690 static u32 rtl8169_get_msglevel(struct net_device *dev)
1692 struct rtl8169_private *tp = netdev_priv(dev);
1694 return tp->msg_enable;
1697 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1699 struct rtl8169_private *tp = netdev_priv(dev);
1701 tp->msg_enable = value;
1704 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1711 "tx_single_collisions",
1712 "tx_multi_collisions",
1720 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1724 return ARRAY_SIZE(rtl8169_gstrings);
1730 static void rtl8169_update_counters(struct net_device *dev)
1732 struct rtl8169_private *tp = netdev_priv(dev);
1733 void __iomem *ioaddr = tp->mmio_addr;
1734 struct device *d = &tp->pci_dev->dev;
1735 struct rtl8169_counters *counters;
1741 * Some chips are unable to dump tally counters when the receiver
1744 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1747 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1751 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1752 cmd = (u64)paddr & DMA_BIT_MASK(32);
1753 RTL_W32(CounterAddrLow, cmd);
1754 RTL_W32(CounterAddrLow, cmd | CounterDump);
1757 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1758 memcpy(&tp->counters, counters, sizeof(*counters));
1764 RTL_W32(CounterAddrLow, 0);
1765 RTL_W32(CounterAddrHigh, 0);
1767 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1770 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1771 struct ethtool_stats *stats, u64 *data)
1773 struct rtl8169_private *tp = netdev_priv(dev);
1777 rtl8169_update_counters(dev);
1779 data[0] = le64_to_cpu(tp->counters.tx_packets);
1780 data[1] = le64_to_cpu(tp->counters.rx_packets);
1781 data[2] = le64_to_cpu(tp->counters.tx_errors);
1782 data[3] = le32_to_cpu(tp->counters.rx_errors);
1783 data[4] = le16_to_cpu(tp->counters.rx_missed);
1784 data[5] = le16_to_cpu(tp->counters.align_errors);
1785 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1786 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1787 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1788 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1789 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1790 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1791 data[12] = le16_to_cpu(tp->counters.tx_underun);
1794 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1798 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1803 static const struct ethtool_ops rtl8169_ethtool_ops = {
1804 .get_drvinfo = rtl8169_get_drvinfo,
1805 .get_regs_len = rtl8169_get_regs_len,
1806 .get_link = ethtool_op_get_link,
1807 .get_settings = rtl8169_get_settings,
1808 .set_settings = rtl8169_set_settings,
1809 .get_msglevel = rtl8169_get_msglevel,
1810 .set_msglevel = rtl8169_set_msglevel,
1811 .get_regs = rtl8169_get_regs,
1812 .get_wol = rtl8169_get_wol,
1813 .set_wol = rtl8169_set_wol,
1814 .get_strings = rtl8169_get_strings,
1815 .get_sset_count = rtl8169_get_sset_count,
1816 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1819 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1820 struct net_device *dev, u8 default_version)
1822 void __iomem *ioaddr = tp->mmio_addr;
1824 * The driver currently handles the 8168Bf and the 8168Be identically
1825 * but they can be identified more specifically through the test below
1828 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1830 * Same thing for the 8101Eb and the 8101Ec:
1832 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1834 static const struct rtl_mac_info {
1840 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1841 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1844 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
1845 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1846 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1847 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1850 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1851 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1852 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1854 /* 8168DP family. */
1855 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1856 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1857 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
1860 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1861 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1862 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1863 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1864 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1865 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1866 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1867 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1868 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1871 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1872 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1873 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1874 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1877 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
1878 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1879 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1880 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1881 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1882 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1883 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1884 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1885 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1886 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1887 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1888 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1889 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1890 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1891 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1892 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1893 /* FIXME: where did these entries come from ? -- FR */
1894 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1895 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1898 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1899 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1900 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1901 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1902 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1903 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1906 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1908 const struct rtl_mac_info *p = mac_info;
1911 reg = RTL_R32(TxConfig);
1912 while ((reg & p->mask) != p->val)
1914 tp->mac_version = p->mac_version;
1916 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1917 netif_notice(tp, probe, dev,
1918 "unknown MAC, using family default\n");
1919 tp->mac_version = default_version;
1923 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1925 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1933 static void rtl_writephy_batch(struct rtl8169_private *tp,
1934 const struct phy_reg *regs, int len)
1937 rtl_writephy(tp, regs->reg, regs->val);
1942 #define PHY_READ 0x00000000
1943 #define PHY_DATA_OR 0x10000000
1944 #define PHY_DATA_AND 0x20000000
1945 #define PHY_BJMPN 0x30000000
1946 #define PHY_READ_EFUSE 0x40000000
1947 #define PHY_READ_MAC_BYTE 0x50000000
1948 #define PHY_WRITE_MAC_BYTE 0x60000000
1949 #define PHY_CLEAR_READCOUNT 0x70000000
1950 #define PHY_WRITE 0x80000000
1951 #define PHY_READCOUNT_EQ_SKIP 0x90000000
1952 #define PHY_COMP_EQ_SKIPN 0xa0000000
1953 #define PHY_COMP_NEQ_SKIPN 0xb0000000
1954 #define PHY_WRITE_PREVIOUS 0xc0000000
1955 #define PHY_SKIPN 0xd0000000
1956 #define PHY_DELAY_MS 0xe0000000
1957 #define PHY_WRITE_ERI_WORD 0xf0000000
1961 char version[RTL_VER_SIZE];
1967 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1969 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1971 const struct firmware *fw = rtl_fw->fw;
1972 struct fw_info *fw_info = (struct fw_info *)fw->data;
1973 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1974 char *version = rtl_fw->version;
1977 if (fw->size < FW_OPCODE_SIZE)
1980 if (!fw_info->magic) {
1981 size_t i, size, start;
1984 if (fw->size < sizeof(*fw_info))
1987 for (i = 0; i < fw->size; i++)
1988 checksum += fw->data[i];
1992 start = le32_to_cpu(fw_info->fw_start);
1993 if (start > fw->size)
1996 size = le32_to_cpu(fw_info->fw_len);
1997 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2000 memcpy(version, fw_info->version, RTL_VER_SIZE);
2002 pa->code = (__le32 *)(fw->data + start);
2005 if (fw->size % FW_OPCODE_SIZE)
2008 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2010 pa->code = (__le32 *)fw->data;
2011 pa->size = fw->size / FW_OPCODE_SIZE;
2013 version[RTL_VER_SIZE - 1] = 0;
2020 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2021 struct rtl_fw_phy_action *pa)
2026 for (index = 0; index < pa->size; index++) {
2027 u32 action = le32_to_cpu(pa->code[index]);
2028 u32 regno = (action & 0x0fff0000) >> 16;
2030 switch(action & 0xf0000000) {
2034 case PHY_READ_EFUSE:
2035 case PHY_CLEAR_READCOUNT:
2037 case PHY_WRITE_PREVIOUS:
2042 if (regno > index) {
2043 netif_err(tp, ifup, tp->dev,
2044 "Out of range of firmware\n");
2048 case PHY_READCOUNT_EQ_SKIP:
2049 if (index + 2 >= pa->size) {
2050 netif_err(tp, ifup, tp->dev,
2051 "Out of range of firmware\n");
2055 case PHY_COMP_EQ_SKIPN:
2056 case PHY_COMP_NEQ_SKIPN:
2058 if (index + 1 + regno >= pa->size) {
2059 netif_err(tp, ifup, tp->dev,
2060 "Out of range of firmware\n");
2065 case PHY_READ_MAC_BYTE:
2066 case PHY_WRITE_MAC_BYTE:
2067 case PHY_WRITE_ERI_WORD:
2069 netif_err(tp, ifup, tp->dev,
2070 "Invalid action 0x%08x\n", action);
2079 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2081 struct net_device *dev = tp->dev;
2084 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2085 netif_err(tp, ifup, dev, "invalid firwmare\n");
2089 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2095 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2097 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2101 predata = count = 0;
2103 for (index = 0; index < pa->size; ) {
2104 u32 action = le32_to_cpu(pa->code[index]);
2105 u32 data = action & 0x0000ffff;
2106 u32 regno = (action & 0x0fff0000) >> 16;
2111 switch(action & 0xf0000000) {
2113 predata = rtl_readphy(tp, regno);
2128 case PHY_READ_EFUSE:
2129 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2132 case PHY_CLEAR_READCOUNT:
2137 rtl_writephy(tp, regno, data);
2140 case PHY_READCOUNT_EQ_SKIP:
2141 index += (count == data) ? 2 : 1;
2143 case PHY_COMP_EQ_SKIPN:
2144 if (predata == data)
2148 case PHY_COMP_NEQ_SKIPN:
2149 if (predata != data)
2153 case PHY_WRITE_PREVIOUS:
2154 rtl_writephy(tp, regno, predata);
2165 case PHY_READ_MAC_BYTE:
2166 case PHY_WRITE_MAC_BYTE:
2167 case PHY_WRITE_ERI_WORD:
2174 static void rtl_release_firmware(struct rtl8169_private *tp)
2176 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2177 release_firmware(tp->rtl_fw->fw);
2180 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2183 static void rtl_apply_firmware(struct rtl8169_private *tp)
2185 struct rtl_fw *rtl_fw = tp->rtl_fw;
2187 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2188 if (!IS_ERR_OR_NULL(rtl_fw))
2189 rtl_phy_write_fw(tp, rtl_fw);
2192 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2194 if (rtl_readphy(tp, reg) != val)
2195 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2197 rtl_apply_firmware(tp);
2200 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2202 static const struct phy_reg phy_reg_init[] = {
2264 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2267 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2269 static const struct phy_reg phy_reg_init[] = {
2275 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2278 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2280 struct pci_dev *pdev = tp->pci_dev;
2282 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2283 (pdev->subsystem_device != 0xe000))
2286 rtl_writephy(tp, 0x1f, 0x0001);
2287 rtl_writephy(tp, 0x10, 0xf01b);
2288 rtl_writephy(tp, 0x1f, 0x0000);
2291 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2293 static const struct phy_reg phy_reg_init[] = {
2333 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2335 rtl8169scd_hw_phy_config_quirk(tp);
2338 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2340 static const struct phy_reg phy_reg_init[] = {
2388 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2391 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2393 static const struct phy_reg phy_reg_init[] = {
2398 rtl_writephy(tp, 0x1f, 0x0001);
2399 rtl_patchphy(tp, 0x16, 1 << 0);
2401 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2404 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2406 static const struct phy_reg phy_reg_init[] = {
2412 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2415 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2417 static const struct phy_reg phy_reg_init[] = {
2425 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2428 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2430 static const struct phy_reg phy_reg_init[] = {
2436 rtl_writephy(tp, 0x1f, 0x0000);
2437 rtl_patchphy(tp, 0x14, 1 << 5);
2438 rtl_patchphy(tp, 0x0d, 1 << 5);
2440 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2443 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2445 static const struct phy_reg phy_reg_init[] = {
2465 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2467 rtl_patchphy(tp, 0x14, 1 << 5);
2468 rtl_patchphy(tp, 0x0d, 1 << 5);
2469 rtl_writephy(tp, 0x1f, 0x0000);
2472 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2474 static const struct phy_reg phy_reg_init[] = {
2492 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2494 rtl_patchphy(tp, 0x16, 1 << 0);
2495 rtl_patchphy(tp, 0x14, 1 << 5);
2496 rtl_patchphy(tp, 0x0d, 1 << 5);
2497 rtl_writephy(tp, 0x1f, 0x0000);
2500 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2502 static const struct phy_reg phy_reg_init[] = {
2514 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2516 rtl_patchphy(tp, 0x16, 1 << 0);
2517 rtl_patchphy(tp, 0x14, 1 << 5);
2518 rtl_patchphy(tp, 0x0d, 1 << 5);
2519 rtl_writephy(tp, 0x1f, 0x0000);
2522 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2524 rtl8168c_3_hw_phy_config(tp);
2527 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2529 static const struct phy_reg phy_reg_init_0[] = {
2530 /* Channel Estimation */
2551 * Enhance line driver power
2560 * Can not link to 1Gbps with bad cable
2561 * Decrease SNR threshold form 21.07dB to 19.04dB
2569 void __iomem *ioaddr = tp->mmio_addr;
2571 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2575 * Fine Tune Switching regulator parameter
2577 rtl_writephy(tp, 0x1f, 0x0002);
2578 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2579 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2581 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2582 static const struct phy_reg phy_reg_init[] = {
2592 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2594 val = rtl_readphy(tp, 0x0d);
2596 if ((val & 0x00ff) != 0x006c) {
2597 static const u32 set[] = {
2598 0x0065, 0x0066, 0x0067, 0x0068,
2599 0x0069, 0x006a, 0x006b, 0x006c
2603 rtl_writephy(tp, 0x1f, 0x0002);
2606 for (i = 0; i < ARRAY_SIZE(set); i++)
2607 rtl_writephy(tp, 0x0d, val | set[i]);
2610 static const struct phy_reg phy_reg_init[] = {
2618 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2621 /* RSET couple improve */
2622 rtl_writephy(tp, 0x1f, 0x0002);
2623 rtl_patchphy(tp, 0x0d, 0x0300);
2624 rtl_patchphy(tp, 0x0f, 0x0010);
2626 /* Fine tune PLL performance */
2627 rtl_writephy(tp, 0x1f, 0x0002);
2628 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2629 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2631 rtl_writephy(tp, 0x1f, 0x0005);
2632 rtl_writephy(tp, 0x05, 0x001b);
2634 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2636 rtl_writephy(tp, 0x1f, 0x0000);
2639 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2641 static const struct phy_reg phy_reg_init_0[] = {
2642 /* Channel Estimation */
2663 * Enhance line driver power
2672 * Can not link to 1Gbps with bad cable
2673 * Decrease SNR threshold form 21.07dB to 19.04dB
2681 void __iomem *ioaddr = tp->mmio_addr;
2683 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2685 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2686 static const struct phy_reg phy_reg_init[] = {
2697 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2699 val = rtl_readphy(tp, 0x0d);
2700 if ((val & 0x00ff) != 0x006c) {
2701 static const u32 set[] = {
2702 0x0065, 0x0066, 0x0067, 0x0068,
2703 0x0069, 0x006a, 0x006b, 0x006c
2707 rtl_writephy(tp, 0x1f, 0x0002);
2710 for (i = 0; i < ARRAY_SIZE(set); i++)
2711 rtl_writephy(tp, 0x0d, val | set[i]);
2714 static const struct phy_reg phy_reg_init[] = {
2722 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2725 /* Fine tune PLL performance */
2726 rtl_writephy(tp, 0x1f, 0x0002);
2727 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2728 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2730 /* Switching regulator Slew rate */
2731 rtl_writephy(tp, 0x1f, 0x0002);
2732 rtl_patchphy(tp, 0x0f, 0x0017);
2734 rtl_writephy(tp, 0x1f, 0x0005);
2735 rtl_writephy(tp, 0x05, 0x001b);
2737 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2739 rtl_writephy(tp, 0x1f, 0x0000);
2742 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2744 static const struct phy_reg phy_reg_init[] = {
2800 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2803 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2805 static const struct phy_reg phy_reg_init[] = {
2815 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2816 rtl_patchphy(tp, 0x0d, 1 << 5);
2819 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
2821 static const struct phy_reg phy_reg_init[] = {
2822 /* Enable Delay cap */
2828 /* Channel estimation fine tune */
2837 /* Update PFM & 10M TX idle timer */
2849 rtl_apply_firmware(tp);
2851 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2853 /* DCO enable for 10M IDLE Power */
2854 rtl_writephy(tp, 0x1f, 0x0007);
2855 rtl_writephy(tp, 0x1e, 0x0023);
2856 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2857 rtl_writephy(tp, 0x1f, 0x0000);
2859 /* For impedance matching */
2860 rtl_writephy(tp, 0x1f, 0x0002);
2861 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2862 rtl_writephy(tp, 0x1f, 0x0000);
2864 /* PHY auto speed down */
2865 rtl_writephy(tp, 0x1f, 0x0007);
2866 rtl_writephy(tp, 0x1e, 0x002d);
2867 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2868 rtl_writephy(tp, 0x1f, 0x0000);
2869 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2871 rtl_writephy(tp, 0x1f, 0x0005);
2872 rtl_writephy(tp, 0x05, 0x8b86);
2873 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2874 rtl_writephy(tp, 0x1f, 0x0000);
2876 rtl_writephy(tp, 0x1f, 0x0005);
2877 rtl_writephy(tp, 0x05, 0x8b85);
2878 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2879 rtl_writephy(tp, 0x1f, 0x0007);
2880 rtl_writephy(tp, 0x1e, 0x0020);
2881 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2882 rtl_writephy(tp, 0x1f, 0x0006);
2883 rtl_writephy(tp, 0x00, 0x5a00);
2884 rtl_writephy(tp, 0x1f, 0x0000);
2885 rtl_writephy(tp, 0x0d, 0x0007);
2886 rtl_writephy(tp, 0x0e, 0x003c);
2887 rtl_writephy(tp, 0x0d, 0x4007);
2888 rtl_writephy(tp, 0x0e, 0x0000);
2889 rtl_writephy(tp, 0x0d, 0x0000);
2892 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2894 static const struct phy_reg phy_reg_init[] = {
2895 /* Enable Delay cap */
2904 /* Channel estimation fine tune */
2921 rtl_apply_firmware(tp);
2923 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2925 /* For 4-corner performance improve */
2926 rtl_writephy(tp, 0x1f, 0x0005);
2927 rtl_writephy(tp, 0x05, 0x8b80);
2928 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2929 rtl_writephy(tp, 0x1f, 0x0000);
2931 /* PHY auto speed down */
2932 rtl_writephy(tp, 0x1f, 0x0004);
2933 rtl_writephy(tp, 0x1f, 0x0007);
2934 rtl_writephy(tp, 0x1e, 0x002d);
2935 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2936 rtl_writephy(tp, 0x1f, 0x0002);
2937 rtl_writephy(tp, 0x1f, 0x0000);
2938 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2940 /* improve 10M EEE waveform */
2941 rtl_writephy(tp, 0x1f, 0x0005);
2942 rtl_writephy(tp, 0x05, 0x8b86);
2943 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2946 /* Improve 2-pair detection performance */
2947 rtl_writephy(tp, 0x1f, 0x0005);
2948 rtl_writephy(tp, 0x05, 0x8b85);
2949 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2950 rtl_writephy(tp, 0x1f, 0x0000);
2953 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2955 rtl_writephy(tp, 0x1f, 0x0005);
2956 rtl_writephy(tp, 0x05, 0x8b85);
2957 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2958 rtl_writephy(tp, 0x1f, 0x0004);
2959 rtl_writephy(tp, 0x1f, 0x0007);
2960 rtl_writephy(tp, 0x1e, 0x0020);
2961 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
2962 rtl_writephy(tp, 0x1f, 0x0002);
2963 rtl_writephy(tp, 0x1f, 0x0000);
2964 rtl_writephy(tp, 0x0d, 0x0007);
2965 rtl_writephy(tp, 0x0e, 0x003c);
2966 rtl_writephy(tp, 0x0d, 0x4007);
2967 rtl_writephy(tp, 0x0e, 0x0000);
2968 rtl_writephy(tp, 0x0d, 0x0000);
2971 rtl_writephy(tp, 0x1f, 0x0003);
2972 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2973 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2974 rtl_writephy(tp, 0x1f, 0x0000);
2977 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2979 static const struct phy_reg phy_reg_init[] = {
2980 /* Channel estimation fine tune */
2985 /* Modify green table for giga & fnet */
3002 /* Modify green table for 10M */
3008 /* Disable hiimpedance detection (RTCT) */
3014 rtl_apply_firmware(tp);
3016 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3018 /* For 4-corner performance improve */
3019 rtl_writephy(tp, 0x1f, 0x0005);
3020 rtl_writephy(tp, 0x05, 0x8b80);
3021 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3022 rtl_writephy(tp, 0x1f, 0x0000);
3024 /* PHY auto speed down */
3025 rtl_writephy(tp, 0x1f, 0x0007);
3026 rtl_writephy(tp, 0x1e, 0x002d);
3027 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3028 rtl_writephy(tp, 0x1f, 0x0000);
3029 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3031 /* Improve 10M EEE waveform */
3032 rtl_writephy(tp, 0x1f, 0x0005);
3033 rtl_writephy(tp, 0x05, 0x8b86);
3034 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3035 rtl_writephy(tp, 0x1f, 0x0000);
3037 /* Improve 2-pair detection performance */
3038 rtl_writephy(tp, 0x1f, 0x0005);
3039 rtl_writephy(tp, 0x05, 0x8b85);
3040 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3041 rtl_writephy(tp, 0x1f, 0x0000);
3044 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3046 rtl_apply_firmware(tp);
3048 /* For 4-corner performance improve */
3049 rtl_writephy(tp, 0x1f, 0x0005);
3050 rtl_writephy(tp, 0x05, 0x8b80);
3051 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3052 rtl_writephy(tp, 0x1f, 0x0000);
3054 /* PHY auto speed down */
3055 rtl_writephy(tp, 0x1f, 0x0007);
3056 rtl_writephy(tp, 0x1e, 0x002d);
3057 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3058 rtl_writephy(tp, 0x1f, 0x0000);
3059 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3061 /* Improve 10M EEE waveform */
3062 rtl_writephy(tp, 0x1f, 0x0005);
3063 rtl_writephy(tp, 0x05, 0x8b86);
3064 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3065 rtl_writephy(tp, 0x1f, 0x0000);
3068 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3070 static const struct phy_reg phy_reg_init[] = {
3077 rtl_writephy(tp, 0x1f, 0x0000);
3078 rtl_patchphy(tp, 0x11, 1 << 12);
3079 rtl_patchphy(tp, 0x19, 1 << 13);
3080 rtl_patchphy(tp, 0x10, 1 << 15);
3082 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3085 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3087 static const struct phy_reg phy_reg_init[] = {
3101 /* Disable ALDPS before ram code */
3102 rtl_writephy(tp, 0x1f, 0x0000);
3103 rtl_writephy(tp, 0x18, 0x0310);
3106 rtl_apply_firmware(tp);
3108 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3111 static void rtl_hw_phy_config(struct net_device *dev)
3113 struct rtl8169_private *tp = netdev_priv(dev);
3115 rtl8169_print_mac_version(tp);
3117 switch (tp->mac_version) {
3118 case RTL_GIGA_MAC_VER_01:
3120 case RTL_GIGA_MAC_VER_02:
3121 case RTL_GIGA_MAC_VER_03:
3122 rtl8169s_hw_phy_config(tp);
3124 case RTL_GIGA_MAC_VER_04:
3125 rtl8169sb_hw_phy_config(tp);
3127 case RTL_GIGA_MAC_VER_05:
3128 rtl8169scd_hw_phy_config(tp);
3130 case RTL_GIGA_MAC_VER_06:
3131 rtl8169sce_hw_phy_config(tp);
3133 case RTL_GIGA_MAC_VER_07:
3134 case RTL_GIGA_MAC_VER_08:
3135 case RTL_GIGA_MAC_VER_09:
3136 rtl8102e_hw_phy_config(tp);
3138 case RTL_GIGA_MAC_VER_11:
3139 rtl8168bb_hw_phy_config(tp);
3141 case RTL_GIGA_MAC_VER_12:
3142 rtl8168bef_hw_phy_config(tp);
3144 case RTL_GIGA_MAC_VER_17:
3145 rtl8168bef_hw_phy_config(tp);
3147 case RTL_GIGA_MAC_VER_18:
3148 rtl8168cp_1_hw_phy_config(tp);
3150 case RTL_GIGA_MAC_VER_19:
3151 rtl8168c_1_hw_phy_config(tp);
3153 case RTL_GIGA_MAC_VER_20:
3154 rtl8168c_2_hw_phy_config(tp);
3156 case RTL_GIGA_MAC_VER_21:
3157 rtl8168c_3_hw_phy_config(tp);
3159 case RTL_GIGA_MAC_VER_22:
3160 rtl8168c_4_hw_phy_config(tp);
3162 case RTL_GIGA_MAC_VER_23:
3163 case RTL_GIGA_MAC_VER_24:
3164 rtl8168cp_2_hw_phy_config(tp);
3166 case RTL_GIGA_MAC_VER_25:
3167 rtl8168d_1_hw_phy_config(tp);
3169 case RTL_GIGA_MAC_VER_26:
3170 rtl8168d_2_hw_phy_config(tp);
3172 case RTL_GIGA_MAC_VER_27:
3173 rtl8168d_3_hw_phy_config(tp);
3175 case RTL_GIGA_MAC_VER_28:
3176 rtl8168d_4_hw_phy_config(tp);
3178 case RTL_GIGA_MAC_VER_29:
3179 case RTL_GIGA_MAC_VER_30:
3180 rtl8105e_hw_phy_config(tp);
3182 case RTL_GIGA_MAC_VER_31:
3185 case RTL_GIGA_MAC_VER_32:
3186 case RTL_GIGA_MAC_VER_33:
3187 rtl8168e_1_hw_phy_config(tp);
3189 case RTL_GIGA_MAC_VER_34:
3190 rtl8168e_2_hw_phy_config(tp);
3192 case RTL_GIGA_MAC_VER_35:
3193 rtl8168f_1_hw_phy_config(tp);
3195 case RTL_GIGA_MAC_VER_36:
3196 rtl8168f_2_hw_phy_config(tp);
3204 static void rtl8169_phy_timer(unsigned long __opaque)
3206 struct net_device *dev = (struct net_device *)__opaque;
3207 struct rtl8169_private *tp = netdev_priv(dev);
3208 struct timer_list *timer = &tp->timer;
3209 void __iomem *ioaddr = tp->mmio_addr;
3210 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3212 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
3214 spin_lock_irq(&tp->lock);
3216 if (tp->phy_reset_pending(tp)) {
3218 * A busy loop could burn quite a few cycles on nowadays CPU.
3219 * Let's delay the execution of the timer for a few ticks.
3225 if (tp->link_ok(ioaddr))
3228 netif_warn(tp, link, dev, "PHY reset until link up\n");
3230 tp->phy_reset_enable(tp);
3233 mod_timer(timer, jiffies + timeout);
3235 spin_unlock_irq(&tp->lock);
3238 #ifdef CONFIG_NET_POLL_CONTROLLER
3240 * Polling 'interrupt' - used by things like netconsole to send skbs
3241 * without having to re-enable interrupts. It's not called while
3242 * the interrupt routine is executing.
3244 static void rtl8169_netpoll(struct net_device *dev)
3246 struct rtl8169_private *tp = netdev_priv(dev);
3247 struct pci_dev *pdev = tp->pci_dev;
3249 disable_irq(pdev->irq);
3250 rtl8169_interrupt(pdev->irq, dev);
3251 enable_irq(pdev->irq);
3255 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3256 void __iomem *ioaddr)
3259 pci_release_regions(pdev);
3260 pci_clear_mwi(pdev);
3261 pci_disable_device(pdev);
3265 static void rtl8169_phy_reset(struct net_device *dev,
3266 struct rtl8169_private *tp)
3270 tp->phy_reset_enable(tp);
3271 for (i = 0; i < 100; i++) {
3272 if (!tp->phy_reset_pending(tp))
3276 netif_err(tp, link, dev, "PHY reset failed\n");
3279 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3281 void __iomem *ioaddr = tp->mmio_addr;
3283 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3284 (RTL_R8(PHYstatus) & TBI_Enable);
3287 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3289 void __iomem *ioaddr = tp->mmio_addr;
3291 rtl_hw_phy_config(dev);
3293 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3294 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3298 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3300 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3301 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3303 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
3304 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3306 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
3307 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
3310 rtl8169_phy_reset(dev, tp);
3312 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3313 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3314 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3315 (tp->mii.supports_gmii ?
3316 ADVERTISED_1000baseT_Half |
3317 ADVERTISED_1000baseT_Full : 0));
3319 if (rtl_tbi_enabled(tp))
3320 netif_info(tp, link, dev, "TBI auto-negotiating\n");
3323 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3325 void __iomem *ioaddr = tp->mmio_addr;
3329 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3330 high = addr[4] | (addr[5] << 8);
3332 spin_lock_irq(&tp->lock);
3334 RTL_W8(Cfg9346, Cfg9346_Unlock);
3336 RTL_W32(MAC4, high);
3342 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3343 const struct exgmac_reg e[] = {
3344 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3345 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3346 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3347 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3351 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3354 RTL_W8(Cfg9346, Cfg9346_Lock);
3356 spin_unlock_irq(&tp->lock);
3359 static int rtl_set_mac_address(struct net_device *dev, void *p)
3361 struct rtl8169_private *tp = netdev_priv(dev);
3362 struct sockaddr *addr = p;
3364 if (!is_valid_ether_addr(addr->sa_data))
3365 return -EADDRNOTAVAIL;
3367 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3369 rtl_rar_set(tp, dev->dev_addr);
3374 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3376 struct rtl8169_private *tp = netdev_priv(dev);
3377 struct mii_ioctl_data *data = if_mii(ifr);
3379 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3382 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3383 struct mii_ioctl_data *data, int cmd)
3387 data->phy_id = 32; /* Internal PHY */
3391 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3395 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3401 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3406 static const struct rtl_cfg_info {
3407 void (*hw_start)(struct net_device *);
3408 unsigned int region;
3414 } rtl_cfg_infos [] = {
3416 .hw_start = rtl_hw_start_8169,
3419 .intr_event = SYSErr | LinkChg | RxOverflow |
3420 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
3421 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
3422 .features = RTL_FEATURE_GMII,
3423 .default_ver = RTL_GIGA_MAC_VER_01,
3426 .hw_start = rtl_hw_start_8168,
3429 .intr_event = SYSErr | LinkChg | RxOverflow |
3430 TxErr | TxOK | RxOK | RxErr,
3431 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
3432 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3433 .default_ver = RTL_GIGA_MAC_VER_11,
3436 .hw_start = rtl_hw_start_8101,
3439 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3440 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
3441 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
3442 .features = RTL_FEATURE_MSI,
3443 .default_ver = RTL_GIGA_MAC_VER_13,
3447 /* Cfg9346_Unlock assumed. */
3448 static unsigned rtl_try_msi(struct rtl8169_private *tp,
3449 const struct rtl_cfg_info *cfg)
3451 void __iomem *ioaddr = tp->mmio_addr;
3455 cfg2 = RTL_R8(Config2) & ~MSIEnable;
3456 if (cfg->features & RTL_FEATURE_MSI) {
3457 if (pci_enable_msi(tp->pci_dev)) {
3458 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
3461 msi = RTL_FEATURE_MSI;
3464 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3465 RTL_W8(Config2, cfg2);
3469 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3471 if (tp->features & RTL_FEATURE_MSI) {
3472 pci_disable_msi(pdev);
3473 tp->features &= ~RTL_FEATURE_MSI;
3477 static const struct net_device_ops rtl8169_netdev_ops = {
3478 .ndo_open = rtl8169_open,
3479 .ndo_stop = rtl8169_close,
3480 .ndo_get_stats = rtl8169_get_stats,
3481 .ndo_start_xmit = rtl8169_start_xmit,
3482 .ndo_tx_timeout = rtl8169_tx_timeout,
3483 .ndo_validate_addr = eth_validate_addr,
3484 .ndo_change_mtu = rtl8169_change_mtu,
3485 .ndo_fix_features = rtl8169_fix_features,
3486 .ndo_set_features = rtl8169_set_features,
3487 .ndo_set_mac_address = rtl_set_mac_address,
3488 .ndo_do_ioctl = rtl8169_ioctl,
3489 .ndo_set_rx_mode = rtl_set_rx_mode,
3490 #ifdef CONFIG_NET_POLL_CONTROLLER
3491 .ndo_poll_controller = rtl8169_netpoll,
3496 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3498 struct mdio_ops *ops = &tp->mdio_ops;
3500 switch (tp->mac_version) {
3501 case RTL_GIGA_MAC_VER_27:
3502 ops->write = r8168dp_1_mdio_write;
3503 ops->read = r8168dp_1_mdio_read;
3505 case RTL_GIGA_MAC_VER_28:
3506 case RTL_GIGA_MAC_VER_31:
3507 ops->write = r8168dp_2_mdio_write;
3508 ops->read = r8168dp_2_mdio_read;
3511 ops->write = r8169_mdio_write;
3512 ops->read = r8169_mdio_read;
3517 static void rtl_speed_down(struct rtl8169_private *tp)
3522 rtl_writephy(tp, 0x1f, 0x0000);
3523 lpa = rtl_readphy(tp, MII_LPA);
3525 if (lpa & (LPA_10HALF | LPA_10FULL))
3526 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
3527 else if (lpa & (LPA_100HALF | LPA_100FULL))
3528 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3529 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
3531 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3532 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3533 (tp->mii.supports_gmii ?
3534 ADVERTISED_1000baseT_Half |
3535 ADVERTISED_1000baseT_Full : 0);
3537 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3541 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3543 void __iomem *ioaddr = tp->mmio_addr;
3545 switch (tp->mac_version) {
3546 case RTL_GIGA_MAC_VER_25:
3547 case RTL_GIGA_MAC_VER_26:
3548 case RTL_GIGA_MAC_VER_29:
3549 case RTL_GIGA_MAC_VER_30:
3550 case RTL_GIGA_MAC_VER_32:
3551 case RTL_GIGA_MAC_VER_33:
3552 case RTL_GIGA_MAC_VER_34:
3553 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3554 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3561 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3563 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3567 rtl_wol_suspend_quirk(tp);
3572 static void r810x_phy_power_down(struct rtl8169_private *tp)
3574 rtl_writephy(tp, 0x1f, 0x0000);
3575 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3578 static void r810x_phy_power_up(struct rtl8169_private *tp)
3580 rtl_writephy(tp, 0x1f, 0x0000);
3581 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3584 static void r810x_pll_power_down(struct rtl8169_private *tp)
3586 if (rtl_wol_pll_power_down(tp))
3589 r810x_phy_power_down(tp);
3592 static void r810x_pll_power_up(struct rtl8169_private *tp)
3594 r810x_phy_power_up(tp);
3597 static void r8168_phy_power_up(struct rtl8169_private *tp)
3599 rtl_writephy(tp, 0x1f, 0x0000);
3600 switch (tp->mac_version) {
3601 case RTL_GIGA_MAC_VER_11:
3602 case RTL_GIGA_MAC_VER_12:
3603 case RTL_GIGA_MAC_VER_17:
3604 case RTL_GIGA_MAC_VER_18:
3605 case RTL_GIGA_MAC_VER_19:
3606 case RTL_GIGA_MAC_VER_20:
3607 case RTL_GIGA_MAC_VER_21:
3608 case RTL_GIGA_MAC_VER_22:
3609 case RTL_GIGA_MAC_VER_23:
3610 case RTL_GIGA_MAC_VER_24:
3611 case RTL_GIGA_MAC_VER_25:
3612 case RTL_GIGA_MAC_VER_26:
3613 case RTL_GIGA_MAC_VER_27:
3614 case RTL_GIGA_MAC_VER_28:
3615 case RTL_GIGA_MAC_VER_31:
3616 rtl_writephy(tp, 0x0e, 0x0000);
3621 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3624 static void r8168_phy_power_down(struct rtl8169_private *tp)
3626 rtl_writephy(tp, 0x1f, 0x0000);
3627 switch (tp->mac_version) {
3628 case RTL_GIGA_MAC_VER_32:
3629 case RTL_GIGA_MAC_VER_33:
3630 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3633 case RTL_GIGA_MAC_VER_11:
3634 case RTL_GIGA_MAC_VER_12:
3635 case RTL_GIGA_MAC_VER_17:
3636 case RTL_GIGA_MAC_VER_18:
3637 case RTL_GIGA_MAC_VER_19:
3638 case RTL_GIGA_MAC_VER_20:
3639 case RTL_GIGA_MAC_VER_21:
3640 case RTL_GIGA_MAC_VER_22:
3641 case RTL_GIGA_MAC_VER_23:
3642 case RTL_GIGA_MAC_VER_24:
3643 case RTL_GIGA_MAC_VER_25:
3644 case RTL_GIGA_MAC_VER_26:
3645 case RTL_GIGA_MAC_VER_27:
3646 case RTL_GIGA_MAC_VER_28:
3647 case RTL_GIGA_MAC_VER_31:
3648 rtl_writephy(tp, 0x0e, 0x0200);
3650 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3655 static void r8168_pll_power_down(struct rtl8169_private *tp)
3657 void __iomem *ioaddr = tp->mmio_addr;
3659 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3660 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3661 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3662 r8168dp_check_dash(tp)) {
3666 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3667 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
3668 (RTL_R16(CPlusCmd) & ASF)) {
3672 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3673 tp->mac_version == RTL_GIGA_MAC_VER_33)
3674 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3676 if (rtl_wol_pll_power_down(tp))
3679 r8168_phy_power_down(tp);
3681 switch (tp->mac_version) {
3682 case RTL_GIGA_MAC_VER_25:
3683 case RTL_GIGA_MAC_VER_26:
3684 case RTL_GIGA_MAC_VER_27:
3685 case RTL_GIGA_MAC_VER_28:
3686 case RTL_GIGA_MAC_VER_31:
3687 case RTL_GIGA_MAC_VER_32:
3688 case RTL_GIGA_MAC_VER_33:
3689 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3694 static void r8168_pll_power_up(struct rtl8169_private *tp)
3696 void __iomem *ioaddr = tp->mmio_addr;
3698 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3699 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3700 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3701 r8168dp_check_dash(tp)) {
3705 switch (tp->mac_version) {
3706 case RTL_GIGA_MAC_VER_25:
3707 case RTL_GIGA_MAC_VER_26:
3708 case RTL_GIGA_MAC_VER_27:
3709 case RTL_GIGA_MAC_VER_28:
3710 case RTL_GIGA_MAC_VER_31:
3711 case RTL_GIGA_MAC_VER_32:
3712 case RTL_GIGA_MAC_VER_33:
3713 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3717 r8168_phy_power_up(tp);
3720 static void rtl_generic_op(struct rtl8169_private *tp,
3721 void (*op)(struct rtl8169_private *))
3727 static void rtl_pll_power_down(struct rtl8169_private *tp)
3729 rtl_generic_op(tp, tp->pll_power_ops.down);
3732 static void rtl_pll_power_up(struct rtl8169_private *tp)
3734 rtl_generic_op(tp, tp->pll_power_ops.up);
3737 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3739 struct pll_power_ops *ops = &tp->pll_power_ops;
3741 switch (tp->mac_version) {
3742 case RTL_GIGA_MAC_VER_07:
3743 case RTL_GIGA_MAC_VER_08:
3744 case RTL_GIGA_MAC_VER_09:
3745 case RTL_GIGA_MAC_VER_10:
3746 case RTL_GIGA_MAC_VER_16:
3747 case RTL_GIGA_MAC_VER_29:
3748 case RTL_GIGA_MAC_VER_30:
3749 ops->down = r810x_pll_power_down;
3750 ops->up = r810x_pll_power_up;
3753 case RTL_GIGA_MAC_VER_11:
3754 case RTL_GIGA_MAC_VER_12:
3755 case RTL_GIGA_MAC_VER_17:
3756 case RTL_GIGA_MAC_VER_18:
3757 case RTL_GIGA_MAC_VER_19:
3758 case RTL_GIGA_MAC_VER_20:
3759 case RTL_GIGA_MAC_VER_21:
3760 case RTL_GIGA_MAC_VER_22:
3761 case RTL_GIGA_MAC_VER_23:
3762 case RTL_GIGA_MAC_VER_24:
3763 case RTL_GIGA_MAC_VER_25:
3764 case RTL_GIGA_MAC_VER_26:
3765 case RTL_GIGA_MAC_VER_27:
3766 case RTL_GIGA_MAC_VER_28:
3767 case RTL_GIGA_MAC_VER_31:
3768 case RTL_GIGA_MAC_VER_32:
3769 case RTL_GIGA_MAC_VER_33:
3770 case RTL_GIGA_MAC_VER_34:
3771 case RTL_GIGA_MAC_VER_35:
3772 case RTL_GIGA_MAC_VER_36:
3773 ops->down = r8168_pll_power_down;
3774 ops->up = r8168_pll_power_up;
3784 static void rtl_init_rxcfg(struct rtl8169_private *tp)
3786 void __iomem *ioaddr = tp->mmio_addr;
3788 switch (tp->mac_version) {
3789 case RTL_GIGA_MAC_VER_01:
3790 case RTL_GIGA_MAC_VER_02:
3791 case RTL_GIGA_MAC_VER_03:
3792 case RTL_GIGA_MAC_VER_04:
3793 case RTL_GIGA_MAC_VER_05:
3794 case RTL_GIGA_MAC_VER_06:
3795 case RTL_GIGA_MAC_VER_10:
3796 case RTL_GIGA_MAC_VER_11:
3797 case RTL_GIGA_MAC_VER_12:
3798 case RTL_GIGA_MAC_VER_13:
3799 case RTL_GIGA_MAC_VER_14:
3800 case RTL_GIGA_MAC_VER_15:
3801 case RTL_GIGA_MAC_VER_16:
3802 case RTL_GIGA_MAC_VER_17:
3803 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3805 case RTL_GIGA_MAC_VER_18:
3806 case RTL_GIGA_MAC_VER_19:
3807 case RTL_GIGA_MAC_VER_20:
3808 case RTL_GIGA_MAC_VER_21:
3809 case RTL_GIGA_MAC_VER_22:
3810 case RTL_GIGA_MAC_VER_23:
3811 case RTL_GIGA_MAC_VER_24:
3812 case RTL_GIGA_MAC_VER_34:
3813 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3816 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3821 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3823 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3826 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3828 void __iomem *ioaddr = tp->mmio_addr;
3830 RTL_W8(Cfg9346, Cfg9346_Unlock);
3831 rtl_generic_op(tp, tp->jumbo_ops.enable);
3832 RTL_W8(Cfg9346, Cfg9346_Lock);
3835 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3837 void __iomem *ioaddr = tp->mmio_addr;
3839 RTL_W8(Cfg9346, Cfg9346_Unlock);
3840 rtl_generic_op(tp, tp->jumbo_ops.disable);
3841 RTL_W8(Cfg9346, Cfg9346_Lock);
3844 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3846 void __iomem *ioaddr = tp->mmio_addr;
3848 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3849 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3850 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3853 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3855 void __iomem *ioaddr = tp->mmio_addr;
3857 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3858 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3859 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3862 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3864 void __iomem *ioaddr = tp->mmio_addr;
3866 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3869 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3871 void __iomem *ioaddr = tp->mmio_addr;
3873 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3876 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3878 void __iomem *ioaddr = tp->mmio_addr;
3879 struct pci_dev *pdev = tp->pci_dev;
3881 RTL_W8(MaxTxPacketSize, 0x3f);
3882 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3883 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
3884 pci_write_config_byte(pdev, 0x79, 0x20);
3887 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3889 void __iomem *ioaddr = tp->mmio_addr;
3890 struct pci_dev *pdev = tp->pci_dev;
3892 RTL_W8(MaxTxPacketSize, 0x0c);
3893 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3894 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
3895 pci_write_config_byte(pdev, 0x79, 0x50);
3898 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3900 rtl_tx_performance_tweak(tp->pci_dev,
3901 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3904 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3906 rtl_tx_performance_tweak(tp->pci_dev,
3907 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3910 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3912 void __iomem *ioaddr = tp->mmio_addr;
3914 r8168b_0_hw_jumbo_enable(tp);
3916 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3919 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3921 void __iomem *ioaddr = tp->mmio_addr;
3923 r8168b_0_hw_jumbo_disable(tp);
3925 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3928 static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3930 struct jumbo_ops *ops = &tp->jumbo_ops;
3932 switch (tp->mac_version) {
3933 case RTL_GIGA_MAC_VER_11:
3934 ops->disable = r8168b_0_hw_jumbo_disable;
3935 ops->enable = r8168b_0_hw_jumbo_enable;
3937 case RTL_GIGA_MAC_VER_12:
3938 case RTL_GIGA_MAC_VER_17:
3939 ops->disable = r8168b_1_hw_jumbo_disable;
3940 ops->enable = r8168b_1_hw_jumbo_enable;
3942 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3943 case RTL_GIGA_MAC_VER_19:
3944 case RTL_GIGA_MAC_VER_20:
3945 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3946 case RTL_GIGA_MAC_VER_22:
3947 case RTL_GIGA_MAC_VER_23:
3948 case RTL_GIGA_MAC_VER_24:
3949 case RTL_GIGA_MAC_VER_25:
3950 case RTL_GIGA_MAC_VER_26:
3951 ops->disable = r8168c_hw_jumbo_disable;
3952 ops->enable = r8168c_hw_jumbo_enable;
3954 case RTL_GIGA_MAC_VER_27:
3955 case RTL_GIGA_MAC_VER_28:
3956 ops->disable = r8168dp_hw_jumbo_disable;
3957 ops->enable = r8168dp_hw_jumbo_enable;
3959 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3960 case RTL_GIGA_MAC_VER_32:
3961 case RTL_GIGA_MAC_VER_33:
3962 case RTL_GIGA_MAC_VER_34:
3963 ops->disable = r8168e_hw_jumbo_disable;
3964 ops->enable = r8168e_hw_jumbo_enable;
3968 * No action needed for jumbo frames with 8169.
3969 * No jumbo for 810x at all.
3972 ops->disable = NULL;
3978 static void rtl_hw_reset(struct rtl8169_private *tp)
3980 void __iomem *ioaddr = tp->mmio_addr;
3983 /* Soft reset the chip. */
3984 RTL_W8(ChipCmd, CmdReset);
3986 /* Check that the chip has finished the reset. */
3987 for (i = 0; i < 100; i++) {
3988 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3994 static int __devinit
3995 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3997 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3998 const unsigned int region = cfg->region;
3999 struct rtl8169_private *tp;
4000 struct mii_if_info *mii;
4001 struct net_device *dev;
4002 void __iomem *ioaddr;
4006 if (netif_msg_drv(&debug)) {
4007 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
4008 MODULENAME, RTL8169_VERSION);
4011 dev = alloc_etherdev(sizeof (*tp));
4013 if (netif_msg_drv(&debug))
4014 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
4019 SET_NETDEV_DEV(dev, &pdev->dev);
4020 dev->netdev_ops = &rtl8169_netdev_ops;
4021 tp = netdev_priv(dev);
4024 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
4028 mii->mdio_read = rtl_mdio_read;
4029 mii->mdio_write = rtl_mdio_write;
4030 mii->phy_id_mask = 0x1f;
4031 mii->reg_num_mask = 0x1f;
4032 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
4034 /* disable ASPM completely as that cause random device stop working
4035 * problems as well as full system hangs for some PCIe devices users */
4036 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4037 PCIE_LINK_STATE_CLKPM);
4039 /* enable device (incl. PCI PM wakeup and hotplug setup) */
4040 rc = pci_enable_device(pdev);
4042 netif_err(tp, probe, dev, "enable failure\n");
4043 goto err_out_free_dev_1;
4046 if (pci_set_mwi(pdev) < 0)
4047 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
4049 /* make sure PCI base addr 1 is MMIO */
4050 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
4051 netif_err(tp, probe, dev,
4052 "region #%d not an MMIO resource, aborting\n",
4058 /* check for weird/broken PCI region reporting */
4059 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
4060 netif_err(tp, probe, dev,
4061 "Invalid PCI region size(s), aborting\n");
4066 rc = pci_request_regions(pdev, MODULENAME);
4068 netif_err(tp, probe, dev, "could not request regions\n");
4072 tp->cp_cmd = RxChkSum;
4074 if ((sizeof(dma_addr_t) > 4) &&
4075 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
4076 tp->cp_cmd |= PCIDAC;
4077 dev->features |= NETIF_F_HIGHDMA;
4079 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4081 netif_err(tp, probe, dev, "DMA configuration failed\n");
4082 goto err_out_free_res_3;
4086 /* ioremap MMIO region */
4087 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
4089 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
4091 goto err_out_free_res_3;
4093 tp->mmio_addr = ioaddr;
4095 if (!pci_is_pcie(pdev))
4096 netif_info(tp, probe, dev, "not PCI Express\n");
4098 /* Identify chip attached to board */
4099 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4103 RTL_W16(IntrMask, 0x0000);
4107 RTL_W16(IntrStatus, 0xffff);
4109 pci_set_master(pdev);
4112 * Pretend we are using VLANs; This bypasses a nasty bug where
4113 * Interrupts stop flowing on high load on 8110SCd controllers.
4115 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4116 tp->cp_cmd |= RxVlan;
4118 rtl_init_mdio_ops(tp);
4119 rtl_init_pll_power_ops(tp);
4120 rtl_init_jumbo_ops(tp);
4122 rtl8169_print_mac_version(tp);
4124 chipset = tp->mac_version;
4125 tp->txd_version = rtl_chip_infos[chipset].txd_version;
4127 RTL_W8(Cfg9346, Cfg9346_Unlock);
4128 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4129 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
4130 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4131 tp->features |= RTL_FEATURE_WOL;
4132 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4133 tp->features |= RTL_FEATURE_WOL;
4134 tp->features |= rtl_try_msi(tp, cfg);
4135 RTL_W8(Cfg9346, Cfg9346_Lock);
4137 if (rtl_tbi_enabled(tp)) {
4138 tp->set_speed = rtl8169_set_speed_tbi;
4139 tp->get_settings = rtl8169_gset_tbi;
4140 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4141 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4142 tp->link_ok = rtl8169_tbi_link_ok;
4143 tp->do_ioctl = rtl_tbi_ioctl;
4145 tp->set_speed = rtl8169_set_speed_xmii;
4146 tp->get_settings = rtl8169_gset_xmii;
4147 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4148 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4149 tp->link_ok = rtl8169_xmii_link_ok;
4150 tp->do_ioctl = rtl_xmii_ioctl;
4153 spin_lock_init(&tp->lock);
4155 /* Get MAC address */
4156 for (i = 0; i < MAC_ADDR_LEN; i++)
4157 dev->dev_addr[i] = RTL_R8(MAC0 + i);
4158 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
4160 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
4161 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4162 dev->irq = pdev->irq;
4163 dev->base_addr = (unsigned long) ioaddr;
4165 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
4167 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4168 * properly for all devices */
4169 dev->features |= NETIF_F_RXCSUM |
4170 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4172 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4173 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4174 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4177 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4178 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4179 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
4181 tp->intr_mask = 0xffff;
4182 tp->hw_start = cfg->hw_start;
4183 tp->intr_event = cfg->intr_event;
4184 tp->napi_event = cfg->napi_event;
4186 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4187 ~(RxBOVF | RxFOVF) : ~0;
4189 init_timer(&tp->timer);
4190 tp->timer.data = (unsigned long) dev;
4191 tp->timer.function = rtl8169_phy_timer;
4193 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
4195 rc = register_netdev(dev);
4199 pci_set_drvdata(pdev, dev);
4201 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
4202 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
4203 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
4204 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4205 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4206 "tx checksumming: %s]\n",
4207 rtl_chip_infos[chipset].jumbo_max,
4208 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4211 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4212 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4213 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4214 rtl8168_driver_start(tp);
4217 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
4219 if (pci_dev_run_wake(pdev))
4220 pm_runtime_put_noidle(&pdev->dev);
4222 netif_carrier_off(dev);
4228 netif_napi_del(&tp->napi);
4229 rtl_disable_msi(pdev, tp);
4232 pci_release_regions(pdev);
4234 pci_clear_mwi(pdev);
4235 pci_disable_device(pdev);
4241 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
4243 struct net_device *dev = pci_get_drvdata(pdev);
4244 struct rtl8169_private *tp = netdev_priv(dev);
4246 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4247 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4248 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4249 rtl8168_driver_stop(tp);
4252 cancel_delayed_work_sync(&tp->task);
4254 netif_napi_del(&tp->napi);
4256 unregister_netdev(dev);
4258 rtl_release_firmware(tp);
4260 if (pci_dev_run_wake(pdev))
4261 pm_runtime_get_noresume(&pdev->dev);
4263 /* restore original MAC address */
4264 rtl_rar_set(tp, dev->perm_addr);
4266 rtl_disable_msi(pdev, tp);
4267 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4268 pci_set_drvdata(pdev, NULL);
4271 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4273 struct rtl_fw *rtl_fw;
4277 name = rtl_lookup_firmware_name(tp);
4279 goto out_no_firmware;
4281 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4285 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4289 rc = rtl_check_firmware(tp, rtl_fw);
4291 goto err_release_firmware;
4293 tp->rtl_fw = rtl_fw;
4297 err_release_firmware:
4298 release_firmware(rtl_fw->fw);
4302 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4309 static void rtl_request_firmware(struct rtl8169_private *tp)
4311 if (IS_ERR(tp->rtl_fw))
4312 rtl_request_uncached_firmware(tp);
4315 static int rtl8169_open(struct net_device *dev)
4317 struct rtl8169_private *tp = netdev_priv(dev);
4318 void __iomem *ioaddr = tp->mmio_addr;
4319 struct pci_dev *pdev = tp->pci_dev;
4320 int retval = -ENOMEM;
4322 pm_runtime_get_sync(&pdev->dev);
4325 * Rx and Tx desscriptors needs 256 bytes alignment.
4326 * dma_alloc_coherent provides more.
4328 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4329 &tp->TxPhyAddr, GFP_KERNEL);
4330 if (!tp->TxDescArray)
4331 goto err_pm_runtime_put;
4333 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4334 &tp->RxPhyAddr, GFP_KERNEL);
4335 if (!tp->RxDescArray)
4338 retval = rtl8169_init_ring(dev);
4342 INIT_DELAYED_WORK(&tp->task, NULL);
4346 rtl_request_firmware(tp);
4348 retval = request_irq(dev->irq, rtl8169_interrupt,
4349 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
4352 goto err_release_fw_2;
4354 napi_enable(&tp->napi);
4356 rtl8169_init_phy(dev, tp);
4358 rtl8169_set_features(dev, dev->features);
4360 rtl_pll_power_up(tp);
4364 tp->saved_wolopts = 0;
4365 pm_runtime_put_noidle(&pdev->dev);
4367 rtl8169_check_link_status(dev, tp, ioaddr);
4372 rtl_release_firmware(tp);
4373 rtl8169_rx_clear(tp);
4375 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4377 tp->RxDescArray = NULL;
4379 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4381 tp->TxDescArray = NULL;
4383 pm_runtime_put_noidle(&pdev->dev);
4387 static void rtl_rx_close(struct rtl8169_private *tp)
4389 void __iomem *ioaddr = tp->mmio_addr;
4391 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4394 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4396 void __iomem *ioaddr = tp->mmio_addr;
4398 /* Disable interrupts */
4399 rtl8169_irq_mask_and_ack(tp);
4403 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4404 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4405 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4406 while (RTL_R8(TxPoll) & NPQ)
4408 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4409 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4410 tp->mac_version == RTL_GIGA_MAC_VER_36) {
4411 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4412 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4415 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4422 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4424 void __iomem *ioaddr = tp->mmio_addr;
4426 /* Set DMA burst size and Interframe Gap Time */
4427 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4428 (InterFrameGap << TxInterFrameGapShift));
4431 static void rtl_hw_start(struct net_device *dev)
4433 struct rtl8169_private *tp = netdev_priv(dev);
4437 netif_start_queue(dev);
4440 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4441 void __iomem *ioaddr)
4444 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4445 * register to be written before TxDescAddrLow to work.
4446 * Switching from MMIO to I/O access fixes the issue as well.
4448 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4449 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4450 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4451 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4454 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4458 cmd = RTL_R16(CPlusCmd);
4459 RTL_W16(CPlusCmd, cmd);
4463 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
4465 /* Low hurts. Let's disable the filtering. */
4466 RTL_W16(RxMaxSize, rx_buf_sz + 1);
4469 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4471 static const struct rtl_cfg2_info {
4476 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4477 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4478 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4479 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4481 const struct rtl_cfg2_info *p = cfg2_info;
4485 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
4486 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4487 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4488 RTL_W32(0x7c, p->val);
4494 static void rtl_hw_start_8169(struct net_device *dev)
4496 struct rtl8169_private *tp = netdev_priv(dev);
4497 void __iomem *ioaddr = tp->mmio_addr;
4498 struct pci_dev *pdev = tp->pci_dev;
4500 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4501 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4502 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4505 RTL_W8(Cfg9346, Cfg9346_Unlock);
4506 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4507 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4508 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4509 tp->mac_version == RTL_GIGA_MAC_VER_04)
4510 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4514 RTL_W8(EarlyTxThres, NoEarlyTx);
4516 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4518 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4519 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4520 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4521 tp->mac_version == RTL_GIGA_MAC_VER_04)
4522 rtl_set_rx_tx_config_registers(tp);
4524 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4526 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4527 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4528 dprintk("Set MAC Reg C+CR Offset 0xE0. "
4529 "Bit-3 and bit-14 MUST be 1\n");
4530 tp->cp_cmd |= (1 << 14);
4533 RTL_W16(CPlusCmd, tp->cp_cmd);
4535 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4538 * Undocumented corner. Supposedly:
4539 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4541 RTL_W16(IntrMitigate, 0x0000);
4543 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4545 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4546 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4547 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4548 tp->mac_version != RTL_GIGA_MAC_VER_04) {
4549 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4550 rtl_set_rx_tx_config_registers(tp);
4553 RTL_W8(Cfg9346, Cfg9346_Lock);
4555 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4558 RTL_W32(RxMissed, 0);
4560 rtl_set_rx_mode(dev);
4562 /* no early-rx interrupts */
4563 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4565 /* Enable all known interrupts by setting the interrupt mask. */
4566 RTL_W16(IntrMask, tp->intr_event);
4569 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
4573 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
4574 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4577 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4579 rtl_csi_access_enable(ioaddr, 0x17000000);
4582 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4584 rtl_csi_access_enable(ioaddr, 0x27000000);
4588 unsigned int offset;
4593 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
4598 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4599 rtl_ephy_write(ioaddr, e->offset, w);
4604 static void rtl_disable_clock_request(struct pci_dev *pdev)
4606 int cap = pci_pcie_cap(pdev);
4611 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4612 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4613 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4617 static void rtl_enable_clock_request(struct pci_dev *pdev)
4619 int cap = pci_pcie_cap(pdev);
4624 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4625 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4626 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4630 #define R8168_CPCMD_QUIRK_MASK (\
4641 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4643 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4645 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4647 rtl_tx_performance_tweak(pdev,
4648 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4651 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4653 rtl_hw_start_8168bb(ioaddr, pdev);
4655 RTL_W8(MaxTxPacketSize, TxPacketMax);
4657 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4660 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4662 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4664 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4666 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4668 rtl_disable_clock_request(pdev);
4670 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4673 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
4675 static const struct ephy_info e_info_8168cp[] = {
4676 { 0x01, 0, 0x0001 },
4677 { 0x02, 0x0800, 0x1000 },
4678 { 0x03, 0, 0x0042 },
4679 { 0x06, 0x0080, 0x0000 },
4683 rtl_csi_access_enable_2(ioaddr);
4685 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4687 __rtl_hw_start_8168cp(ioaddr, pdev);
4690 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4692 rtl_csi_access_enable_2(ioaddr);
4694 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4696 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4698 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4701 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4703 rtl_csi_access_enable_2(ioaddr);
4705 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4708 RTL_W8(DBG_REG, 0x20);
4710 RTL_W8(MaxTxPacketSize, TxPacketMax);
4712 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4714 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4717 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4719 static const struct ephy_info e_info_8168c_1[] = {
4720 { 0x02, 0x0800, 0x1000 },
4721 { 0x03, 0, 0x0002 },
4722 { 0x06, 0x0080, 0x0000 }
4725 rtl_csi_access_enable_2(ioaddr);
4727 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4729 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4731 __rtl_hw_start_8168cp(ioaddr, pdev);
4734 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4736 static const struct ephy_info e_info_8168c_2[] = {
4737 { 0x01, 0, 0x0001 },
4738 { 0x03, 0x0400, 0x0220 }
4741 rtl_csi_access_enable_2(ioaddr);
4743 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4745 __rtl_hw_start_8168cp(ioaddr, pdev);
4748 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4750 rtl_hw_start_8168c_2(ioaddr, pdev);
4753 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4755 rtl_csi_access_enable_2(ioaddr);
4757 __rtl_hw_start_8168cp(ioaddr, pdev);
4760 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4762 rtl_csi_access_enable_2(ioaddr);
4764 rtl_disable_clock_request(pdev);
4766 RTL_W8(MaxTxPacketSize, TxPacketMax);
4768 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4770 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4773 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4775 rtl_csi_access_enable_1(ioaddr);
4777 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4779 RTL_W8(MaxTxPacketSize, TxPacketMax);
4781 rtl_disable_clock_request(pdev);
4784 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4786 static const struct ephy_info e_info_8168d_4[] = {
4788 { 0x19, 0x20, 0x50 },
4793 rtl_csi_access_enable_1(ioaddr);
4795 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4797 RTL_W8(MaxTxPacketSize, TxPacketMax);
4799 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4800 const struct ephy_info *e = e_info_8168d_4 + i;
4803 w = rtl_ephy_read(ioaddr, e->offset);
4804 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4807 rtl_enable_clock_request(pdev);
4810 static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4812 static const struct ephy_info e_info_8168e_1[] = {
4813 { 0x00, 0x0200, 0x0100 },
4814 { 0x00, 0x0000, 0x0004 },
4815 { 0x06, 0x0002, 0x0001 },
4816 { 0x06, 0x0000, 0x0030 },
4817 { 0x07, 0x0000, 0x2000 },
4818 { 0x00, 0x0000, 0x0020 },
4819 { 0x03, 0x5800, 0x2000 },
4820 { 0x03, 0x0000, 0x0001 },
4821 { 0x01, 0x0800, 0x1000 },
4822 { 0x07, 0x0000, 0x4000 },
4823 { 0x1e, 0x0000, 0x2000 },
4824 { 0x19, 0xffff, 0xfe6c },
4825 { 0x0a, 0x0000, 0x0040 }
4828 rtl_csi_access_enable_2(ioaddr);
4830 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
4832 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4834 RTL_W8(MaxTxPacketSize, TxPacketMax);
4836 rtl_disable_clock_request(pdev);
4838 /* Reset tx FIFO pointer */
4839 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4840 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
4842 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4845 static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4847 static const struct ephy_info e_info_8168e_2[] = {
4848 { 0x09, 0x0000, 0x0080 },
4849 { 0x19, 0x0000, 0x0224 }
4852 rtl_csi_access_enable_1(ioaddr);
4854 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4856 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4858 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4859 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4860 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4861 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4862 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4863 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4864 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4865 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4868 RTL_W8(MaxTxPacketSize, EarlySize);
4870 rtl_disable_clock_request(pdev);
4872 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4873 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4875 /* Adjust EEE LED frequency */
4876 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4878 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4879 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4880 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4883 static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4885 static const struct ephy_info e_info_8168f_1[] = {
4886 { 0x06, 0x00c0, 0x0020 },
4887 { 0x08, 0x0001, 0x0002 },
4888 { 0x09, 0x0000, 0x0080 },
4889 { 0x19, 0x0000, 0x0224 }
4892 rtl_csi_access_enable_1(ioaddr);
4894 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4896 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4898 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4899 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4900 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4901 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4902 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4903 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4904 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4905 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4906 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4907 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4908 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4911 RTL_W8(MaxTxPacketSize, EarlySize);
4913 rtl_disable_clock_request(pdev);
4915 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4916 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4918 /* Adjust EEE LED frequency */
4919 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4921 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4922 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4923 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4926 static void rtl_hw_start_8168(struct net_device *dev)
4928 struct rtl8169_private *tp = netdev_priv(dev);
4929 void __iomem *ioaddr = tp->mmio_addr;
4930 struct pci_dev *pdev = tp->pci_dev;
4932 RTL_W8(Cfg9346, Cfg9346_Unlock);
4934 RTL_W8(MaxTxPacketSize, TxPacketMax);
4936 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4938 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
4940 RTL_W16(CPlusCmd, tp->cp_cmd);
4942 RTL_W16(IntrMitigate, 0x5151);
4944 /* Work around for RxFIFO overflow. */
4945 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
4946 tp->intr_event |= RxFIFOOver | PCSTimeout;
4947 tp->intr_event &= ~RxOverflow;
4950 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4952 rtl_set_rx_mode(dev);
4954 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4955 (InterFrameGap << TxInterFrameGapShift));
4959 switch (tp->mac_version) {
4960 case RTL_GIGA_MAC_VER_11:
4961 rtl_hw_start_8168bb(ioaddr, pdev);
4964 case RTL_GIGA_MAC_VER_12:
4965 case RTL_GIGA_MAC_VER_17:
4966 rtl_hw_start_8168bef(ioaddr, pdev);
4969 case RTL_GIGA_MAC_VER_18:
4970 rtl_hw_start_8168cp_1(ioaddr, pdev);
4973 case RTL_GIGA_MAC_VER_19:
4974 rtl_hw_start_8168c_1(ioaddr, pdev);
4977 case RTL_GIGA_MAC_VER_20:
4978 rtl_hw_start_8168c_2(ioaddr, pdev);
4981 case RTL_GIGA_MAC_VER_21:
4982 rtl_hw_start_8168c_3(ioaddr, pdev);
4985 case RTL_GIGA_MAC_VER_22:
4986 rtl_hw_start_8168c_4(ioaddr, pdev);
4989 case RTL_GIGA_MAC_VER_23:
4990 rtl_hw_start_8168cp_2(ioaddr, pdev);
4993 case RTL_GIGA_MAC_VER_24:
4994 rtl_hw_start_8168cp_3(ioaddr, pdev);
4997 case RTL_GIGA_MAC_VER_25:
4998 case RTL_GIGA_MAC_VER_26:
4999 case RTL_GIGA_MAC_VER_27:
5000 rtl_hw_start_8168d(ioaddr, pdev);
5003 case RTL_GIGA_MAC_VER_28:
5004 rtl_hw_start_8168d_4(ioaddr, pdev);
5007 case RTL_GIGA_MAC_VER_31:
5008 rtl_hw_start_8168dp(ioaddr, pdev);
5011 case RTL_GIGA_MAC_VER_32:
5012 case RTL_GIGA_MAC_VER_33:
5013 rtl_hw_start_8168e_1(ioaddr, pdev);
5015 case RTL_GIGA_MAC_VER_34:
5016 rtl_hw_start_8168e_2(ioaddr, pdev);
5019 case RTL_GIGA_MAC_VER_35:
5020 case RTL_GIGA_MAC_VER_36:
5021 rtl_hw_start_8168f_1(ioaddr, pdev);
5025 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5026 dev->name, tp->mac_version);
5030 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5032 RTL_W8(Cfg9346, Cfg9346_Lock);
5034 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
5036 RTL_W16(IntrMask, tp->intr_event);
5039 #define R810X_CPCMD_QUIRK_MASK (\
5050 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5052 static const struct ephy_info e_info_8102e_1[] = {
5053 { 0x01, 0, 0x6e65 },
5054 { 0x02, 0, 0x091f },
5055 { 0x03, 0, 0xc2f9 },
5056 { 0x06, 0, 0xafb5 },
5057 { 0x07, 0, 0x0e00 },
5058 { 0x19, 0, 0xec80 },
5059 { 0x01, 0, 0x2e65 },
5064 rtl_csi_access_enable_2(ioaddr);
5066 RTL_W8(DBG_REG, FIX_NAK_1);
5068 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5071 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5072 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5074 cfg1 = RTL_R8(Config1);
5075 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5076 RTL_W8(Config1, cfg1 & ~LEDS0);
5078 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5081 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5083 rtl_csi_access_enable_2(ioaddr);
5085 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5087 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5088 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5091 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5093 rtl_hw_start_8102e_2(ioaddr, pdev);
5095 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5098 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5100 static const struct ephy_info e_info_8105e_1[] = {
5101 { 0x07, 0, 0x4000 },
5102 { 0x19, 0, 0x0200 },
5103 { 0x19, 0, 0x0020 },
5104 { 0x1e, 0, 0x2000 },
5105 { 0x03, 0, 0x0001 },
5106 { 0x19, 0, 0x0100 },
5107 { 0x19, 0, 0x0004 },
5111 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5112 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5114 /* Disable Early Tally Counter */
5115 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5117 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5118 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5120 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5123 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5125 rtl_hw_start_8105e_1(ioaddr, pdev);
5126 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5129 static void rtl_hw_start_8101(struct net_device *dev)
5131 struct rtl8169_private *tp = netdev_priv(dev);
5132 void __iomem *ioaddr = tp->mmio_addr;
5133 struct pci_dev *pdev = tp->pci_dev;
5135 if (tp->mac_version >= RTL_GIGA_MAC_VER_30) {
5136 tp->intr_event &= ~RxFIFOOver;
5137 tp->napi_event &= ~RxFIFOOver;
5140 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5141 tp->mac_version == RTL_GIGA_MAC_VER_16) {
5142 int cap = pci_pcie_cap(pdev);
5145 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5146 PCI_EXP_DEVCTL_NOSNOOP_EN);
5150 RTL_W8(Cfg9346, Cfg9346_Unlock);
5152 switch (tp->mac_version) {
5153 case RTL_GIGA_MAC_VER_07:
5154 rtl_hw_start_8102e_1(ioaddr, pdev);
5157 case RTL_GIGA_MAC_VER_08:
5158 rtl_hw_start_8102e_3(ioaddr, pdev);
5161 case RTL_GIGA_MAC_VER_09:
5162 rtl_hw_start_8102e_2(ioaddr, pdev);
5165 case RTL_GIGA_MAC_VER_29:
5166 rtl_hw_start_8105e_1(ioaddr, pdev);
5168 case RTL_GIGA_MAC_VER_30:
5169 rtl_hw_start_8105e_2(ioaddr, pdev);
5173 RTL_W8(Cfg9346, Cfg9346_Lock);
5175 RTL_W8(MaxTxPacketSize, TxPacketMax);
5177 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5179 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5180 RTL_W16(CPlusCmd, tp->cp_cmd);
5182 RTL_W16(IntrMitigate, 0x0000);
5184 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5186 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5187 rtl_set_rx_tx_config_registers(tp);
5191 rtl_set_rx_mode(dev);
5193 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5195 RTL_W16(IntrMask, tp->intr_event);
5198 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5200 struct rtl8169_private *tp = netdev_priv(dev);
5202 if (new_mtu < ETH_ZLEN ||
5203 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
5206 if (new_mtu > ETH_DATA_LEN)
5207 rtl_hw_jumbo_enable(tp);
5209 rtl_hw_jumbo_disable(tp);
5212 netdev_update_features(dev);
5217 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5219 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5220 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5223 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5224 void **data_buff, struct RxDesc *desc)
5226 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
5231 rtl8169_make_unusable_by_asic(desc);
5234 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5236 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5238 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5241 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5244 desc->addr = cpu_to_le64(mapping);
5246 rtl8169_mark_to_asic(desc, rx_buf_sz);
5249 static inline void *rtl8169_align(void *data)
5251 return (void *)ALIGN((long)data, 16);
5254 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5255 struct RxDesc *desc)
5259 struct device *d = &tp->pci_dev->dev;
5260 struct net_device *dev = tp->dev;
5261 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
5263 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5267 if (rtl8169_align(data) != data) {
5269 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5274 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
5276 if (unlikely(dma_mapping_error(d, mapping))) {
5277 if (net_ratelimit())
5278 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5282 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
5290 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5294 for (i = 0; i < NUM_RX_DESC; i++) {
5295 if (tp->Rx_databuff[i]) {
5296 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5297 tp->RxDescArray + i);
5302 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5304 desc->opts1 |= cpu_to_le32(RingEnd);
5307 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5311 for (i = 0; i < NUM_RX_DESC; i++) {
5314 if (tp->Rx_databuff[i])
5317 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5319 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5322 tp->Rx_databuff[i] = data;
5325 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5329 rtl8169_rx_clear(tp);
5333 static int rtl8169_init_ring(struct net_device *dev)
5335 struct rtl8169_private *tp = netdev_priv(dev);
5337 rtl8169_init_ring_indexes(tp);
5339 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
5340 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
5342 return rtl8169_rx_fill(tp);
5345 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5346 struct TxDesc *desc)
5348 unsigned int len = tx_skb->len;
5350 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5358 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5363 for (i = 0; i < n; i++) {
5364 unsigned int entry = (start + i) % NUM_TX_DESC;
5365 struct ring_info *tx_skb = tp->tx_skb + entry;
5366 unsigned int len = tx_skb->len;
5369 struct sk_buff *skb = tx_skb->skb;
5371 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5372 tp->TxDescArray + entry);
5374 tp->dev->stats.tx_dropped++;
5382 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5384 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5385 tp->cur_tx = tp->dirty_tx = 0;
5388 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
5390 struct rtl8169_private *tp = netdev_priv(dev);
5392 PREPARE_DELAYED_WORK(&tp->task, task);
5393 schedule_delayed_work(&tp->task, 4);
5396 static void rtl8169_wait_for_quiescence(struct net_device *dev)
5398 struct rtl8169_private *tp = netdev_priv(dev);
5399 void __iomem *ioaddr = tp->mmio_addr;
5401 synchronize_irq(dev->irq);
5403 /* Wait for any pending NAPI task to complete */
5404 napi_disable(&tp->napi);
5406 rtl8169_irq_mask_and_ack(tp);
5408 tp->intr_mask = 0xffff;
5409 RTL_W16(IntrMask, tp->intr_event);
5410 napi_enable(&tp->napi);
5413 static void rtl8169_reinit_task(struct work_struct *work)
5415 struct rtl8169_private *tp =
5416 container_of(work, struct rtl8169_private, task.work);
5417 struct net_device *dev = tp->dev;
5422 if (!netif_running(dev))
5425 rtl8169_wait_for_quiescence(dev);
5428 ret = rtl8169_open(dev);
5429 if (unlikely(ret < 0)) {
5430 if (net_ratelimit())
5431 netif_err(tp, drv, dev,
5432 "reinit failure (status = %d). Rescheduling\n",
5434 rtl8169_schedule_work(dev, rtl8169_reinit_task);
5441 static void rtl8169_reset_task(struct work_struct *work)
5443 struct rtl8169_private *tp =
5444 container_of(work, struct rtl8169_private, task.work);
5445 struct net_device *dev = tp->dev;
5450 if (!netif_running(dev))
5453 rtl8169_hw_reset(tp);
5455 rtl8169_wait_for_quiescence(dev);
5457 for (i = 0; i < NUM_RX_DESC; i++)
5458 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5460 rtl8169_tx_clear(tp);
5461 rtl8169_init_ring_indexes(tp);
5464 netif_wake_queue(dev);
5465 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
5471 static void rtl8169_tx_timeout(struct net_device *dev)
5473 rtl8169_schedule_work(dev, rtl8169_reset_task);
5476 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5479 struct skb_shared_info *info = skb_shinfo(skb);
5480 unsigned int cur_frag, entry;
5481 struct TxDesc * uninitialized_var(txd);
5482 struct device *d = &tp->pci_dev->dev;
5485 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5486 const skb_frag_t *frag = info->frags + cur_frag;
5491 entry = (entry + 1) % NUM_TX_DESC;
5493 txd = tp->TxDescArray + entry;
5494 len = skb_frag_size(frag);
5495 addr = skb_frag_address(frag);
5496 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5497 if (unlikely(dma_mapping_error(d, mapping))) {
5498 if (net_ratelimit())
5499 netif_err(tp, drv, tp->dev,
5500 "Failed to map TX fragments DMA!\n");
5504 /* Anti gcc 2.95.3 bugware (sic) */
5505 status = opts[0] | len |
5506 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5508 txd->opts1 = cpu_to_le32(status);
5509 txd->opts2 = cpu_to_le32(opts[1]);
5510 txd->addr = cpu_to_le64(mapping);
5512 tp->tx_skb[entry].len = len;
5516 tp->tx_skb[entry].skb = skb;
5517 txd->opts1 |= cpu_to_le32(LastFrag);
5523 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5527 static bool rtl_skb_pad(struct sk_buff *skb)
5529 if (skb_padto(skb, ETH_ZLEN))
5531 skb_put(skb, ETH_ZLEN - skb->len);
5535 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5537 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5540 static inline bool rtl8169_tso_csum(struct rtl8169_private *tp,
5541 struct sk_buff *skb, u32 *opts)
5543 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
5544 u32 mss = skb_shinfo(skb)->gso_size;
5545 int offset = info->opts_offset;
5549 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5550 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5551 const struct iphdr *ip = ip_hdr(skb);
5553 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5554 return skb_checksum_help(skb) == 0 && rtl_skb_pad(skb);
5556 if (ip->protocol == IPPROTO_TCP)
5557 opts[offset] |= info->checksum.tcp;
5558 else if (ip->protocol == IPPROTO_UDP)
5559 opts[offset] |= info->checksum.udp;
5563 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5564 return rtl_skb_pad(skb);
5569 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5570 struct net_device *dev)
5572 struct rtl8169_private *tp = netdev_priv(dev);
5573 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5574 struct TxDesc *txd = tp->TxDescArray + entry;
5575 void __iomem *ioaddr = tp->mmio_addr;
5576 struct device *d = &tp->pci_dev->dev;
5582 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
5583 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5587 /* 8168evl does not automatically pad to minimum length. */
5588 if (unlikely(tp->mac_version == RTL_GIGA_MAC_VER_34 &&
5589 skb->len < ETH_ZLEN)) {
5590 if (skb_padto(skb, ETH_ZLEN))
5591 goto err_update_stats;
5592 skb_put(skb, ETH_ZLEN - skb->len);
5595 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5598 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5601 if (!rtl8169_tso_csum(tp, skb, opts))
5602 goto err_update_stats;
5604 len = skb_headlen(skb);
5605 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
5606 if (unlikely(dma_mapping_error(d, mapping))) {
5607 if (net_ratelimit())
5608 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
5612 tp->tx_skb[entry].len = len;
5613 txd->addr = cpu_to_le64(mapping);
5615 frags = rtl8169_xmit_frags(tp, skb, opts);
5619 opts[0] |= FirstFrag;
5621 opts[0] |= FirstFrag | LastFrag;
5622 tp->tx_skb[entry].skb = skb;
5625 txd->opts2 = cpu_to_le32(opts[1]);
5629 /* Anti gcc 2.95.3 bugware (sic) */
5630 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
5631 txd->opts1 = cpu_to_le32(status);
5633 tp->cur_tx += frags + 1;
5637 RTL_W8(TxPoll, NPQ);
5639 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5640 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5641 * not miss a ring update when it notices a stopped queue.
5644 netif_stop_queue(dev);
5645 /* Sync with rtl_tx:
5646 * - publish queue status and cur_tx ring index (write barrier)
5647 * - refresh dirty_tx ring index (read barrier).
5648 * May the current thread have a pessimistic view of the ring
5649 * status and forget to wake up queue, a racing rtl_tx thread
5653 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
5654 netif_wake_queue(dev);
5657 return NETDEV_TX_OK;
5660 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
5664 dev->stats.tx_dropped++;
5665 return NETDEV_TX_OK;
5668 netif_stop_queue(dev);
5669 dev->stats.tx_dropped++;
5670 return NETDEV_TX_BUSY;
5673 static void rtl8169_pcierr_interrupt(struct net_device *dev)
5675 struct rtl8169_private *tp = netdev_priv(dev);
5676 struct pci_dev *pdev = tp->pci_dev;
5677 u16 pci_status, pci_cmd;
5679 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5680 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5682 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5683 pci_cmd, pci_status);
5686 * The recovery sequence below admits a very elaborated explanation:
5687 * - it seems to work;
5688 * - I did not see what else could be done;
5689 * - it makes iop3xx happy.
5691 * Feel free to adjust to your needs.
5693 if (pdev->broken_parity_status)
5694 pci_cmd &= ~PCI_COMMAND_PARITY;
5696 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5698 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
5700 pci_write_config_word(pdev, PCI_STATUS,
5701 pci_status & (PCI_STATUS_DETECTED_PARITY |
5702 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5703 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5705 /* The infamous DAC f*ckup only happens at boot time */
5706 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
5707 void __iomem *ioaddr = tp->mmio_addr;
5709 netif_info(tp, intr, dev, "disabling PCI DAC\n");
5710 tp->cp_cmd &= ~PCIDAC;
5711 RTL_W16(CPlusCmd, tp->cp_cmd);
5712 dev->features &= ~NETIF_F_HIGHDMA;
5715 rtl8169_hw_reset(tp);
5717 rtl8169_schedule_work(dev, rtl8169_reinit_task);
5720 static void rtl8169_tx_interrupt(struct net_device *dev,
5721 struct rtl8169_private *tp,
5722 void __iomem *ioaddr)
5724 unsigned int dirty_tx, tx_left;
5726 dirty_tx = tp->dirty_tx;
5728 tx_left = tp->cur_tx - dirty_tx;
5730 while (tx_left > 0) {
5731 unsigned int entry = dirty_tx % NUM_TX_DESC;
5732 struct ring_info *tx_skb = tp->tx_skb + entry;
5736 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5737 if (status & DescOwn)
5740 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5741 tp->TxDescArray + entry);
5742 if (status & LastFrag) {
5743 dev->stats.tx_packets++;
5744 dev->stats.tx_bytes += tx_skb->skb->len;
5745 dev_kfree_skb(tx_skb->skb);
5752 if (tp->dirty_tx != dirty_tx) {
5753 tp->dirty_tx = dirty_tx;
5754 /* Sync with rtl8169_start_xmit:
5755 * - publish dirty_tx ring index (write barrier)
5756 * - refresh cur_tx ring index and queue status (read barrier)
5757 * May the current thread miss the stopped queue condition,
5758 * a racing xmit thread can only have a right view of the
5762 if (netif_queue_stopped(dev) &&
5763 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5764 netif_wake_queue(dev);
5767 * 8168 hack: TxPoll requests are lost when the Tx packets are
5768 * too close. Let's kick an extra TxPoll request when a burst
5769 * of start_xmit activity is detected (if it is not detected,
5770 * it is slow enough). -- FR
5772 if (tp->cur_tx != dirty_tx)
5773 RTL_W8(TxPoll, NPQ);
5777 static inline int rtl8169_fragmented_frame(u32 status)
5779 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5782 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
5784 u32 status = opts1 & RxProtoMask;
5786 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
5787 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5788 skb->ip_summed = CHECKSUM_UNNECESSARY;
5790 skb_checksum_none_assert(skb);
5793 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5794 struct rtl8169_private *tp,
5798 struct sk_buff *skb;
5799 struct device *d = &tp->pci_dev->dev;
5801 data = rtl8169_align(data);
5802 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
5804 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5806 memcpy(skb->data, data, pkt_size);
5807 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5812 static int rtl8169_rx_interrupt(struct net_device *dev,
5813 struct rtl8169_private *tp,
5814 void __iomem *ioaddr, u32 budget)
5816 unsigned int cur_rx, rx_left;
5819 cur_rx = tp->cur_rx;
5820 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
5821 rx_left = min(rx_left, budget);
5823 for (; rx_left > 0; rx_left--, cur_rx++) {
5824 unsigned int entry = cur_rx % NUM_RX_DESC;
5825 struct RxDesc *desc = tp->RxDescArray + entry;
5829 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
5831 if (status & DescOwn)
5833 if (unlikely(status & RxRES)) {
5834 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5836 dev->stats.rx_errors++;
5837 if (status & (RxRWT | RxRUNT))
5838 dev->stats.rx_length_errors++;
5840 dev->stats.rx_crc_errors++;
5841 if (status & RxFOVF) {
5842 rtl8169_schedule_work(dev, rtl8169_reset_task);
5843 dev->stats.rx_fifo_errors++;
5846 struct sk_buff *skb;
5847 dma_addr_t addr = le64_to_cpu(desc->addr);
5848 int pkt_size = (status & 0x00003fff) - 4;
5851 * The driver does not support incoming fragmented
5852 * frames. They are seen as a symptom of over-mtu
5855 if (unlikely(rtl8169_fragmented_frame(status))) {
5856 dev->stats.rx_dropped++;
5857 dev->stats.rx_length_errors++;
5858 goto release_descriptor;
5861 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5862 tp, pkt_size, addr);
5864 dev->stats.rx_dropped++;
5865 goto release_descriptor;
5868 rtl8169_rx_csum(skb, status);
5869 skb_put(skb, pkt_size);
5870 skb->protocol = eth_type_trans(skb, dev);
5872 rtl8169_rx_vlan_tag(desc, skb);
5874 napi_gro_receive(&tp->napi, skb);
5876 dev->stats.rx_bytes += pkt_size;
5877 dev->stats.rx_packets++;
5882 rtl8169_mark_to_asic(desc, rx_buf_sz);
5885 count = cur_rx - tp->cur_rx;
5886 tp->cur_rx = cur_rx;
5888 tp->dirty_rx += count;
5893 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5895 struct net_device *dev = dev_instance;
5896 struct rtl8169_private *tp = netdev_priv(dev);
5897 void __iomem *ioaddr = tp->mmio_addr;
5901 /* loop handling interrupts until we have no new ones or
5902 * we hit a invalid/hotplug case.
5904 status = RTL_R16(IntrStatus);
5905 while (status && status != 0xffff) {
5906 status &= tp->intr_event;
5912 /* Handle all of the error cases first. These will reset
5913 * the chip, so just exit the loop.
5915 if (unlikely(!netif_running(dev))) {
5916 rtl8169_hw_reset(tp);
5920 if (unlikely(status & RxFIFOOver)) {
5921 switch (tp->mac_version) {
5922 /* Work around for rx fifo overflow */
5923 case RTL_GIGA_MAC_VER_11:
5924 netif_stop_queue(dev);
5925 rtl8169_tx_timeout(dev);
5932 if (unlikely(status & SYSErr)) {
5933 rtl8169_pcierr_interrupt(dev);
5937 if (status & LinkChg)
5938 __rtl8169_check_link_status(dev, tp, ioaddr, true);
5940 /* We need to see the lastest version of tp->intr_mask to
5941 * avoid ignoring an MSI interrupt and having to wait for
5942 * another event which may never come.
5945 if (status & tp->intr_mask & tp->napi_event) {
5946 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5947 tp->intr_mask = ~tp->napi_event;
5949 if (likely(napi_schedule_prep(&tp->napi)))
5950 __napi_schedule(&tp->napi);
5952 netif_info(tp, intr, dev,
5953 "interrupt %04x in poll\n", status);
5956 /* We only get a new MSI interrupt when all active irq
5957 * sources on the chip have been acknowledged. So, ack
5958 * everything we've seen and check if new sources have become
5959 * active to avoid blocking all interrupts from the chip.
5962 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5963 status = RTL_R16(IntrStatus);
5966 return IRQ_RETVAL(handled);
5969 static int rtl8169_poll(struct napi_struct *napi, int budget)
5971 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5972 struct net_device *dev = tp->dev;
5973 void __iomem *ioaddr = tp->mmio_addr;
5976 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
5977 rtl8169_tx_interrupt(dev, tp, ioaddr);
5979 if (work_done < budget) {
5980 napi_complete(napi);
5982 /* We need for force the visibility of tp->intr_mask
5983 * for other CPUs, as we can loose an MSI interrupt
5984 * and potentially wait for a retransmit timeout if we don't.
5985 * The posted write to IntrMask is safe, as it will
5986 * eventually make it to the chip and we won't loose anything
5989 tp->intr_mask = 0xffff;
5991 RTL_W16(IntrMask, tp->intr_event);
5997 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5999 struct rtl8169_private *tp = netdev_priv(dev);
6001 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6004 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6005 RTL_W32(RxMissed, 0);
6008 static void rtl8169_down(struct net_device *dev)
6010 struct rtl8169_private *tp = netdev_priv(dev);
6011 void __iomem *ioaddr = tp->mmio_addr;
6013 del_timer_sync(&tp->timer);
6015 netif_stop_queue(dev);
6017 napi_disable(&tp->napi);
6019 spin_lock_irq(&tp->lock);
6021 rtl8169_hw_reset(tp);
6023 * At this point device interrupts can not be enabled in any function,
6024 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
6025 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
6027 rtl8169_rx_missed(dev, ioaddr);
6029 spin_unlock_irq(&tp->lock);
6031 synchronize_irq(dev->irq);
6033 /* Give a racing hard_start_xmit a few cycles to complete. */
6034 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
6036 rtl8169_tx_clear(tp);
6038 rtl8169_rx_clear(tp);
6040 rtl_pll_power_down(tp);
6043 static int rtl8169_close(struct net_device *dev)
6045 struct rtl8169_private *tp = netdev_priv(dev);
6046 struct pci_dev *pdev = tp->pci_dev;
6048 pm_runtime_get_sync(&pdev->dev);
6050 /* Update counters before going down */
6051 rtl8169_update_counters(dev);
6055 free_irq(dev->irq, dev);
6057 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6059 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6061 tp->TxDescArray = NULL;
6062 tp->RxDescArray = NULL;
6064 pm_runtime_put_sync(&pdev->dev);
6069 static void rtl_set_rx_mode(struct net_device *dev)
6071 struct rtl8169_private *tp = netdev_priv(dev);
6072 void __iomem *ioaddr = tp->mmio_addr;
6073 unsigned long flags;
6074 u32 mc_filter[2]; /* Multicast hash filter */
6078 if (dev->flags & IFF_PROMISC) {
6079 /* Unconditionally log net taps. */
6080 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
6082 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
6084 mc_filter[1] = mc_filter[0] = 0xffffffff;
6085 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
6086 (dev->flags & IFF_ALLMULTI)) {
6087 /* Too many to filter perfectly -- accept all multicasts. */
6088 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
6089 mc_filter[1] = mc_filter[0] = 0xffffffff;
6091 struct netdev_hw_addr *ha;
6093 rx_mode = AcceptBroadcast | AcceptMyPhys;
6094 mc_filter[1] = mc_filter[0] = 0;
6095 netdev_for_each_mc_addr(ha, dev) {
6096 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
6097 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6098 rx_mode |= AcceptMulticast;
6102 spin_lock_irqsave(&tp->lock, flags);
6104 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
6106 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
6107 u32 data = mc_filter[0];
6109 mc_filter[0] = swab32(mc_filter[1]);
6110 mc_filter[1] = swab32(data);
6113 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
6114 mc_filter[1] = mc_filter[0] = 0xffffffff;
6116 RTL_W32(MAR0 + 4, mc_filter[1]);
6117 RTL_W32(MAR0 + 0, mc_filter[0]);
6119 RTL_W32(RxConfig, tmp);
6121 spin_unlock_irqrestore(&tp->lock, flags);
6125 * rtl8169_get_stats - Get rtl8169 read/write statistics
6126 * @dev: The Ethernet Device to get statistics for
6128 * Get TX/RX statistics for rtl8169
6130 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6132 struct rtl8169_private *tp = netdev_priv(dev);
6133 void __iomem *ioaddr = tp->mmio_addr;
6134 unsigned long flags;
6136 if (netif_running(dev)) {
6137 spin_lock_irqsave(&tp->lock, flags);
6138 rtl8169_rx_missed(dev, ioaddr);
6139 spin_unlock_irqrestore(&tp->lock, flags);
6145 static void rtl8169_net_suspend(struct net_device *dev)
6147 struct rtl8169_private *tp = netdev_priv(dev);
6149 if (!netif_running(dev))
6152 rtl_pll_power_down(tp);
6154 netif_device_detach(dev);
6155 netif_stop_queue(dev);
6160 static int rtl8169_suspend(struct device *device)
6162 struct pci_dev *pdev = to_pci_dev(device);
6163 struct net_device *dev = pci_get_drvdata(pdev);
6165 rtl8169_net_suspend(dev);
6170 static void __rtl8169_resume(struct net_device *dev)
6172 struct rtl8169_private *tp = netdev_priv(dev);
6174 netif_device_attach(dev);
6176 rtl_pll_power_up(tp);
6178 rtl8169_schedule_work(dev, rtl8169_reset_task);
6181 static int rtl8169_resume(struct device *device)
6183 struct pci_dev *pdev = to_pci_dev(device);
6184 struct net_device *dev = pci_get_drvdata(pdev);
6185 struct rtl8169_private *tp = netdev_priv(dev);
6187 rtl8169_init_phy(dev, tp);
6189 if (netif_running(dev))
6190 __rtl8169_resume(dev);
6195 static int rtl8169_runtime_suspend(struct device *device)
6197 struct pci_dev *pdev = to_pci_dev(device);
6198 struct net_device *dev = pci_get_drvdata(pdev);
6199 struct rtl8169_private *tp = netdev_priv(dev);
6201 if (!tp->TxDescArray)
6204 spin_lock_irq(&tp->lock);
6205 tp->saved_wolopts = __rtl8169_get_wol(tp);
6206 __rtl8169_set_wol(tp, WAKE_ANY);
6207 spin_unlock_irq(&tp->lock);
6209 rtl8169_net_suspend(dev);
6214 static int rtl8169_runtime_resume(struct device *device)
6216 struct pci_dev *pdev = to_pci_dev(device);
6217 struct net_device *dev = pci_get_drvdata(pdev);
6218 struct rtl8169_private *tp = netdev_priv(dev);
6220 if (!tp->TxDescArray)
6223 spin_lock_irq(&tp->lock);
6224 __rtl8169_set_wol(tp, tp->saved_wolopts);
6225 tp->saved_wolopts = 0;
6226 spin_unlock_irq(&tp->lock);
6228 rtl8169_init_phy(dev, tp);
6230 __rtl8169_resume(dev);
6235 static int rtl8169_runtime_idle(struct device *device)
6237 struct pci_dev *pdev = to_pci_dev(device);
6238 struct net_device *dev = pci_get_drvdata(pdev);
6239 struct rtl8169_private *tp = netdev_priv(dev);
6241 return tp->TxDescArray ? -EBUSY : 0;
6244 static const struct dev_pm_ops rtl8169_pm_ops = {
6245 .suspend = rtl8169_suspend,
6246 .resume = rtl8169_resume,
6247 .freeze = rtl8169_suspend,
6248 .thaw = rtl8169_resume,
6249 .poweroff = rtl8169_suspend,
6250 .restore = rtl8169_resume,
6251 .runtime_suspend = rtl8169_runtime_suspend,
6252 .runtime_resume = rtl8169_runtime_resume,
6253 .runtime_idle = rtl8169_runtime_idle,
6256 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6258 #else /* !CONFIG_PM */
6260 #define RTL8169_PM_OPS NULL
6262 #endif /* !CONFIG_PM */
6264 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6266 void __iomem *ioaddr = tp->mmio_addr;
6268 /* WoL fails with 8168b when the receiver is disabled. */
6269 switch (tp->mac_version) {
6270 case RTL_GIGA_MAC_VER_11:
6271 case RTL_GIGA_MAC_VER_12:
6272 case RTL_GIGA_MAC_VER_17:
6273 pci_clear_master(tp->pci_dev);
6275 RTL_W8(ChipCmd, CmdRxEnb);
6284 static void rtl_shutdown(struct pci_dev *pdev)
6286 struct net_device *dev = pci_get_drvdata(pdev);
6287 struct rtl8169_private *tp = netdev_priv(dev);
6288 struct device *d = &pdev->dev;
6290 pm_runtime_get_sync(d);
6292 rtl8169_net_suspend(dev);
6294 /* Restore original MAC address */
6295 rtl_rar_set(tp, dev->perm_addr);
6297 spin_lock_irq(&tp->lock);
6299 rtl8169_hw_reset(tp);
6301 spin_unlock_irq(&tp->lock);
6303 if (system_state == SYSTEM_POWER_OFF) {
6304 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6305 rtl_wol_suspend_quirk(tp);
6306 rtl_wol_shutdown_quirk(tp);
6309 pci_wake_from_d3(pdev, true);
6310 pci_set_power_state(pdev, PCI_D3hot);
6313 pm_runtime_put_noidle(d);
6316 static struct pci_driver rtl8169_pci_driver = {
6318 .id_table = rtl8169_pci_tbl,
6319 .probe = rtl8169_init_one,
6320 .remove = __devexit_p(rtl8169_remove_one),
6321 .shutdown = rtl_shutdown,
6322 .driver.pm = RTL8169_PM_OPS,
6325 static int __init rtl8169_init_module(void)
6327 return pci_register_driver(&rtl8169_pci_driver);
6330 static void __exit rtl8169_cleanup_module(void)
6332 pci_unregister_driver(&rtl8169_pci_driver);
6335 module_init(rtl8169_init_module);
6336 module_exit(rtl8169_cleanup_module);