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 6 /* Maximum PCI burst, '6' is 1024 */
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)
339 RxDescAddrLow = 0xe4,
340 RxDescAddrHigh = 0xe8,
341 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
343 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
345 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
347 #define TxPacketMax (8064 >> 7)
348 #define EarlySize 0x27
351 FuncEventMask = 0xf4,
352 FuncPresetState = 0xf8,
353 FuncForceEvent = 0xfc,
356 enum rtl8110_registers {
362 enum rtl8168_8101_registers {
365 #define CSIAR_FLAG 0x80000000
366 #define CSIAR_WRITE_CMD 0x80000000
367 #define CSIAR_BYTE_ENABLE 0x0f
368 #define CSIAR_BYTE_ENABLE_SHIFT 12
369 #define CSIAR_ADDR_MASK 0x0fff
372 #define EPHYAR_FLAG 0x80000000
373 #define EPHYAR_WRITE_CMD 0x80000000
374 #define EPHYAR_REG_MASK 0x1f
375 #define EPHYAR_REG_SHIFT 16
376 #define EPHYAR_DATA_MASK 0xffff
378 #define PFM_EN (1 << 6)
380 #define FIX_NAK_1 (1 << 4)
381 #define FIX_NAK_2 (1 << 3)
384 #define NOW_IS_OOB (1 << 7)
385 #define EN_NDP (1 << 3)
386 #define EN_OOB_RESET (1 << 2)
388 #define EFUSEAR_FLAG 0x80000000
389 #define EFUSEAR_WRITE_CMD 0x80000000
390 #define EFUSEAR_READ_CMD 0x00000000
391 #define EFUSEAR_REG_MASK 0x03ff
392 #define EFUSEAR_REG_SHIFT 8
393 #define EFUSEAR_DATA_MASK 0xff
396 enum rtl8168_registers {
401 #define ERIAR_FLAG 0x80000000
402 #define ERIAR_WRITE_CMD 0x80000000
403 #define ERIAR_READ_CMD 0x00000000
404 #define ERIAR_ADDR_BYTE_ALIGN 4
405 #define ERIAR_TYPE_SHIFT 16
406 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
407 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
408 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
409 #define ERIAR_MASK_SHIFT 12
410 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
411 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
412 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
413 EPHY_RXER_NUM = 0x7c,
414 OCPDR = 0xb0, /* OCP GPHY access */
415 #define OCPDR_WRITE_CMD 0x80000000
416 #define OCPDR_READ_CMD 0x00000000
417 #define OCPDR_REG_MASK 0x7f
418 #define OCPDR_GPHY_REG_SHIFT 16
419 #define OCPDR_DATA_MASK 0xffff
421 #define OCPAR_FLAG 0x80000000
422 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
423 #define OCPAR_GPHY_READ_CMD 0x0000f060
424 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
425 MISC = 0xf0, /* 8168e only. */
426 #define TXPLA_RST (1 << 29)
427 #define PWM_EN (1 << 22)
430 enum rtl_register_content {
431 /* InterruptStatusBits */
435 TxDescUnavail = 0x0080,
459 /* TXPoll register p.5 */
460 HPQ = 0x80, /* Poll cmd on the high prio queue */
461 NPQ = 0x40, /* Poll cmd on the low prio queue */
462 FSWInt = 0x01, /* Forced software interrupt */
466 Cfg9346_Unlock = 0xc0,
471 AcceptBroadcast = 0x08,
472 AcceptMulticast = 0x04,
474 AcceptAllPhys = 0x01,
475 #define RX_CONFIG_ACCEPT_MASK 0x3f
478 TxInterFrameGapShift = 24,
479 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
481 /* Config1 register p.24 */
484 Speed_down = (1 << 4),
488 PMEnable = (1 << 0), /* Power Management Enable */
490 /* Config2 register p. 25 */
491 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
492 PCI_Clock_66MHz = 0x01,
493 PCI_Clock_33MHz = 0x00,
495 /* Config3 register p.25 */
496 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
497 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
498 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
499 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
501 /* Config4 register */
502 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
504 /* Config5 register p.27 */
505 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
506 MWF = (1 << 5), /* Accept Multicast wakeup frame */
507 UWF = (1 << 4), /* Accept Unicast wakeup frame */
509 LanWake = (1 << 1), /* LanWake enable/disable */
510 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
513 TBIReset = 0x80000000,
514 TBILoopback = 0x40000000,
515 TBINwEnable = 0x20000000,
516 TBINwRestart = 0x10000000,
517 TBILinkOk = 0x02000000,
518 TBINwComplete = 0x01000000,
521 EnableBist = (1 << 15), // 8168 8101
522 Mac_dbgo_oe = (1 << 14), // 8168 8101
523 Normal_mode = (1 << 13), // unused
524 Force_half_dup = (1 << 12), // 8168 8101
525 Force_rxflow_en = (1 << 11), // 8168 8101
526 Force_txflow_en = (1 << 10), // 8168 8101
527 Cxpl_dbg_sel = (1 << 9), // 8168 8101
528 ASF = (1 << 8), // 8168 8101
529 PktCntrDisable = (1 << 7), // 8168 8101
530 Mac_dbgo_sel = 0x001c, // 8168
535 INTT_0 = 0x0000, // 8168
536 INTT_1 = 0x0001, // 8168
537 INTT_2 = 0x0002, // 8168
538 INTT_3 = 0x0003, // 8168
540 /* rtl8169_PHYstatus */
551 TBILinkOK = 0x02000000,
553 /* DumpCounterCommand */
558 /* First doubleword. */
559 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
560 RingEnd = (1 << 30), /* End of descriptor ring */
561 FirstFrag = (1 << 29), /* First segment of a packet */
562 LastFrag = (1 << 28), /* Final segment of a packet */
566 enum rtl_tx_desc_bit {
567 /* First doubleword. */
568 TD_LSO = (1 << 27), /* Large Send Offload */
569 #define TD_MSS_MAX 0x07ffu /* MSS value */
571 /* Second doubleword. */
572 TxVlanTag = (1 << 17), /* Add VLAN tag */
575 /* 8169, 8168b and 810x except 8102e. */
576 enum rtl_tx_desc_bit_0 {
577 /* First doubleword. */
578 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
579 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
580 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
581 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
584 /* 8102e, 8168c and beyond. */
585 enum rtl_tx_desc_bit_1 {
586 /* Second doubleword. */
587 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
588 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
589 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
590 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
593 static const struct rtl_tx_desc_info {
600 } tx_desc_info [] = {
603 .udp = TD0_IP_CS | TD0_UDP_CS,
604 .tcp = TD0_IP_CS | TD0_TCP_CS
606 .mss_shift = TD0_MSS_SHIFT,
611 .udp = TD1_IP_CS | TD1_UDP_CS,
612 .tcp = TD1_IP_CS | TD1_TCP_CS
614 .mss_shift = TD1_MSS_SHIFT,
619 enum rtl_rx_desc_bit {
621 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
622 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
624 #define RxProtoUDP (PID1)
625 #define RxProtoTCP (PID0)
626 #define RxProtoIP (PID1 | PID0)
627 #define RxProtoMask RxProtoIP
629 IPFail = (1 << 16), /* IP checksum failed */
630 UDPFail = (1 << 15), /* UDP/IP checksum failed */
631 TCPFail = (1 << 14), /* TCP/IP checksum failed */
632 RxVlanTag = (1 << 16), /* VLAN tag available */
635 #define RsvdMask 0x3fffc000
652 u8 __pad[sizeof(void *) - sizeof(u32)];
656 RTL_FEATURE_WOL = (1 << 0),
657 RTL_FEATURE_MSI = (1 << 1),
658 RTL_FEATURE_GMII = (1 << 2),
661 struct rtl8169_counters {
668 __le32 tx_one_collision;
669 __le32 tx_multi_collision;
677 struct rtl8169_private {
678 void __iomem *mmio_addr; /* memory map physical address */
679 struct pci_dev *pci_dev;
680 struct net_device *dev;
681 struct napi_struct napi;
686 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
687 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
690 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
691 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
692 dma_addr_t TxPhyAddr;
693 dma_addr_t RxPhyAddr;
694 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
695 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
696 struct timer_list timer;
703 void (*write)(void __iomem *, int, int);
704 int (*read)(void __iomem *, int);
707 struct pll_power_ops {
708 void (*down)(struct rtl8169_private *);
709 void (*up)(struct rtl8169_private *);
713 void (*enable)(struct rtl8169_private *);
714 void (*disable)(struct rtl8169_private *);
717 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
718 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
719 void (*phy_reset_enable)(struct rtl8169_private *tp);
720 void (*hw_start)(struct net_device *);
721 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
722 unsigned int (*link_ok)(void __iomem *);
723 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
724 struct delayed_work task;
727 struct mii_if_info mii;
728 struct rtl8169_counters counters;
733 const struct firmware *fw;
735 #define RTL_VER_SIZE 32
737 char version[RTL_VER_SIZE];
739 struct rtl_fw_phy_action {
744 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
747 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
748 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
749 module_param(use_dac, int, 0);
750 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
751 module_param_named(debug, debug.msg_enable, int, 0);
752 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
753 MODULE_LICENSE("GPL");
754 MODULE_VERSION(RTL8169_VERSION);
755 MODULE_FIRMWARE(FIRMWARE_8168D_1);
756 MODULE_FIRMWARE(FIRMWARE_8168D_2);
757 MODULE_FIRMWARE(FIRMWARE_8168E_1);
758 MODULE_FIRMWARE(FIRMWARE_8168E_2);
759 MODULE_FIRMWARE(FIRMWARE_8168E_3);
760 MODULE_FIRMWARE(FIRMWARE_8105E_1);
761 MODULE_FIRMWARE(FIRMWARE_8168F_1);
762 MODULE_FIRMWARE(FIRMWARE_8168F_2);
764 static int rtl8169_open(struct net_device *dev);
765 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
766 struct net_device *dev);
767 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
768 static int rtl8169_init_ring(struct net_device *dev);
769 static void rtl_hw_start(struct net_device *dev);
770 static int rtl8169_close(struct net_device *dev);
771 static void rtl_set_rx_mode(struct net_device *dev);
772 static void rtl8169_tx_timeout(struct net_device *dev);
773 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
774 static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
775 void __iomem *, u32 budget);
776 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
777 static void rtl8169_down(struct net_device *dev);
778 static void rtl8169_rx_clear(struct rtl8169_private *tp);
779 static int rtl8169_poll(struct napi_struct *napi, int budget);
781 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
783 int cap = pci_pcie_cap(pdev);
788 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
789 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
790 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
794 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
796 void __iomem *ioaddr = tp->mmio_addr;
799 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
800 for (i = 0; i < 20; i++) {
802 if (RTL_R32(OCPAR) & OCPAR_FLAG)
805 return RTL_R32(OCPDR);
808 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
810 void __iomem *ioaddr = tp->mmio_addr;
813 RTL_W32(OCPDR, data);
814 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
815 for (i = 0; i < 20; i++) {
817 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
822 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
824 void __iomem *ioaddr = tp->mmio_addr;
828 RTL_W32(ERIAR, 0x800010e8);
830 for (i = 0; i < 5; i++) {
832 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
836 ocp_write(tp, 0x1, 0x30, 0x00000001);
839 #define OOB_CMD_RESET 0x00
840 #define OOB_CMD_DRIVER_START 0x05
841 #define OOB_CMD_DRIVER_STOP 0x06
843 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
845 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
848 static void rtl8168_driver_start(struct rtl8169_private *tp)
853 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
855 reg = rtl8168_get_ocp_reg(tp);
857 for (i = 0; i < 10; i++) {
859 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
864 static void rtl8168_driver_stop(struct rtl8169_private *tp)
869 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
871 reg = rtl8168_get_ocp_reg(tp);
873 for (i = 0; i < 10; i++) {
875 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
880 static int r8168dp_check_dash(struct rtl8169_private *tp)
882 u16 reg = rtl8168_get_ocp_reg(tp);
884 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
887 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
891 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
893 for (i = 20; i > 0; i--) {
895 * Check if the RTL8169 has completed writing to the specified
898 if (!(RTL_R32(PHYAR) & 0x80000000))
903 * According to hardware specs a 20us delay is required after write
904 * complete indication, but before sending next command.
909 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
913 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
915 for (i = 20; i > 0; i--) {
917 * Check if the RTL8169 has completed retrieving data from
918 * the specified MII register.
920 if (RTL_R32(PHYAR) & 0x80000000) {
921 value = RTL_R32(PHYAR) & 0xffff;
927 * According to hardware specs a 20us delay is required after read
928 * complete indication, but before sending next command.
935 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
939 RTL_W32(OCPDR, data |
940 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
941 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
942 RTL_W32(EPHY_RXER_NUM, 0);
944 for (i = 0; i < 100; i++) {
946 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
951 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
953 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
954 (value & OCPDR_DATA_MASK));
957 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
961 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
964 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
965 RTL_W32(EPHY_RXER_NUM, 0);
967 for (i = 0; i < 100; i++) {
969 if (RTL_R32(OCPAR) & OCPAR_FLAG)
973 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
976 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
978 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
980 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
983 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
985 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
988 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
990 r8168dp_2_mdio_start(ioaddr);
992 r8169_mdio_write(ioaddr, reg_addr, value);
994 r8168dp_2_mdio_stop(ioaddr);
997 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1001 r8168dp_2_mdio_start(ioaddr);
1003 value = r8169_mdio_read(ioaddr, reg_addr);
1005 r8168dp_2_mdio_stop(ioaddr);
1010 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1012 tp->mdio_ops.write(tp->mmio_addr, location, val);
1015 static int rtl_readphy(struct rtl8169_private *tp, int location)
1017 return tp->mdio_ops.read(tp->mmio_addr, location);
1020 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1022 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1025 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1029 val = rtl_readphy(tp, reg_addr);
1030 rtl_writephy(tp, reg_addr, (val | p) & ~m);
1033 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1036 struct rtl8169_private *tp = netdev_priv(dev);
1038 rtl_writephy(tp, location, val);
1041 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1043 struct rtl8169_private *tp = netdev_priv(dev);
1045 return rtl_readphy(tp, location);
1048 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1052 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1053 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1055 for (i = 0; i < 100; i++) {
1056 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1062 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1067 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1069 for (i = 0; i < 100; i++) {
1070 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1071 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1080 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1084 RTL_W32(CSIDR, value);
1085 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1086 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1088 for (i = 0; i < 100; i++) {
1089 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1095 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1100 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1101 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1103 for (i = 0; i < 100; i++) {
1104 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1105 value = RTL_R32(CSIDR);
1115 void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1119 BUG_ON((addr & 3) || (mask == 0));
1120 RTL_W32(ERIDR, val);
1121 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1123 for (i = 0; i < 100; i++) {
1124 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1130 static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1135 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1137 for (i = 0; i < 100; i++) {
1138 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1139 value = RTL_R32(ERIDR);
1149 rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1153 val = rtl_eri_read(ioaddr, addr, type);
1154 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1163 static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1164 const struct exgmac_reg *r, int len)
1167 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1172 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1177 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1179 for (i = 0; i < 300; i++) {
1180 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1181 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1190 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1192 void __iomem *ioaddr = tp->mmio_addr;
1194 RTL_W16(IntrMask, 0x0000);
1195 RTL_W16(IntrStatus, tp->intr_event);
1199 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1201 void __iomem *ioaddr = tp->mmio_addr;
1203 return RTL_R32(TBICSR) & TBIReset;
1206 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1208 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1211 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1213 return RTL_R32(TBICSR) & TBILinkOk;
1216 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1218 return RTL_R8(PHYstatus) & LinkStatus;
1221 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1223 void __iomem *ioaddr = tp->mmio_addr;
1225 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1228 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1232 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1233 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1236 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1238 void __iomem *ioaddr = tp->mmio_addr;
1239 struct net_device *dev = tp->dev;
1241 if (!netif_running(dev))
1244 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1245 if (RTL_R8(PHYstatus) & _1000bpsF) {
1246 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1247 0x00000011, ERIAR_EXGMAC);
1248 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1249 0x00000005, ERIAR_EXGMAC);
1250 } else if (RTL_R8(PHYstatus) & _100bps) {
1251 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1252 0x0000001f, ERIAR_EXGMAC);
1253 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1254 0x00000005, ERIAR_EXGMAC);
1256 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1257 0x0000001f, ERIAR_EXGMAC);
1258 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1259 0x0000003f, ERIAR_EXGMAC);
1261 /* Reset packet filter */
1262 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1264 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1266 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1267 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1268 if (RTL_R8(PHYstatus) & _1000bpsF) {
1269 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1270 0x00000011, ERIAR_EXGMAC);
1271 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1272 0x00000005, ERIAR_EXGMAC);
1274 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1275 0x0000001f, ERIAR_EXGMAC);
1276 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1277 0x0000003f, ERIAR_EXGMAC);
1282 static void __rtl8169_check_link_status(struct net_device *dev,
1283 struct rtl8169_private *tp,
1284 void __iomem *ioaddr, bool pm)
1286 unsigned long flags;
1288 spin_lock_irqsave(&tp->lock, flags);
1289 if (tp->link_ok(ioaddr)) {
1290 rtl_link_chg_patch(tp);
1291 /* This is to cancel a scheduled suspend if there's one. */
1293 pm_request_resume(&tp->pci_dev->dev);
1294 netif_carrier_on(dev);
1295 if (net_ratelimit())
1296 netif_info(tp, ifup, dev, "link up\n");
1298 netif_carrier_off(dev);
1299 netif_info(tp, ifdown, dev, "link down\n");
1301 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1303 spin_unlock_irqrestore(&tp->lock, flags);
1306 static void rtl8169_check_link_status(struct net_device *dev,
1307 struct rtl8169_private *tp,
1308 void __iomem *ioaddr)
1310 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1313 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1315 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1317 void __iomem *ioaddr = tp->mmio_addr;
1321 options = RTL_R8(Config1);
1322 if (!(options & PMEnable))
1325 options = RTL_R8(Config3);
1326 if (options & LinkUp)
1327 wolopts |= WAKE_PHY;
1328 if (options & MagicPacket)
1329 wolopts |= WAKE_MAGIC;
1331 options = RTL_R8(Config5);
1333 wolopts |= WAKE_UCAST;
1335 wolopts |= WAKE_BCAST;
1337 wolopts |= WAKE_MCAST;
1342 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1344 struct rtl8169_private *tp = netdev_priv(dev);
1346 spin_lock_irq(&tp->lock);
1348 wol->supported = WAKE_ANY;
1349 wol->wolopts = __rtl8169_get_wol(tp);
1351 spin_unlock_irq(&tp->lock);
1354 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1356 void __iomem *ioaddr = tp->mmio_addr;
1358 static const struct {
1363 { WAKE_ANY, Config1, PMEnable },
1364 { WAKE_PHY, Config3, LinkUp },
1365 { WAKE_MAGIC, Config3, MagicPacket },
1366 { WAKE_UCAST, Config5, UWF },
1367 { WAKE_BCAST, Config5, BWF },
1368 { WAKE_MCAST, Config5, MWF },
1369 { WAKE_ANY, Config5, LanWake }
1372 RTL_W8(Cfg9346, Cfg9346_Unlock);
1374 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1375 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1376 if (wolopts & cfg[i].opt)
1377 options |= cfg[i].mask;
1378 RTL_W8(cfg[i].reg, options);
1381 RTL_W8(Cfg9346, Cfg9346_Lock);
1384 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1386 struct rtl8169_private *tp = netdev_priv(dev);
1388 spin_lock_irq(&tp->lock);
1391 tp->features |= RTL_FEATURE_WOL;
1393 tp->features &= ~RTL_FEATURE_WOL;
1394 __rtl8169_set_wol(tp, wol->wolopts);
1395 spin_unlock_irq(&tp->lock);
1397 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1402 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1404 return rtl_chip_infos[tp->mac_version].fw_name;
1407 static void rtl8169_get_drvinfo(struct net_device *dev,
1408 struct ethtool_drvinfo *info)
1410 struct rtl8169_private *tp = netdev_priv(dev);
1411 struct rtl_fw *rtl_fw = tp->rtl_fw;
1413 strcpy(info->driver, MODULENAME);
1414 strcpy(info->version, RTL8169_VERSION);
1415 strcpy(info->bus_info, pci_name(tp->pci_dev));
1416 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1417 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1421 static int rtl8169_get_regs_len(struct net_device *dev)
1423 return R8169_REGS_SIZE;
1426 static int rtl8169_set_speed_tbi(struct net_device *dev,
1427 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1429 struct rtl8169_private *tp = netdev_priv(dev);
1430 void __iomem *ioaddr = tp->mmio_addr;
1434 reg = RTL_R32(TBICSR);
1435 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1436 (duplex == DUPLEX_FULL)) {
1437 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1438 } else if (autoneg == AUTONEG_ENABLE)
1439 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1441 netif_warn(tp, link, dev,
1442 "incorrect speed setting refused in TBI mode\n");
1449 static int rtl8169_set_speed_xmii(struct net_device *dev,
1450 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1452 struct rtl8169_private *tp = netdev_priv(dev);
1453 int giga_ctrl, bmcr;
1456 rtl_writephy(tp, 0x1f, 0x0000);
1458 if (autoneg == AUTONEG_ENABLE) {
1461 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1462 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1463 ADVERTISE_100HALF | ADVERTISE_100FULL);
1465 if (adv & ADVERTISED_10baseT_Half)
1466 auto_nego |= ADVERTISE_10HALF;
1467 if (adv & ADVERTISED_10baseT_Full)
1468 auto_nego |= ADVERTISE_10FULL;
1469 if (adv & ADVERTISED_100baseT_Half)
1470 auto_nego |= ADVERTISE_100HALF;
1471 if (adv & ADVERTISED_100baseT_Full)
1472 auto_nego |= ADVERTISE_100FULL;
1474 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1476 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1477 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1479 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1480 if (tp->mii.supports_gmii) {
1481 if (adv & ADVERTISED_1000baseT_Half)
1482 giga_ctrl |= ADVERTISE_1000HALF;
1483 if (adv & ADVERTISED_1000baseT_Full)
1484 giga_ctrl |= ADVERTISE_1000FULL;
1485 } else if (adv & (ADVERTISED_1000baseT_Half |
1486 ADVERTISED_1000baseT_Full)) {
1487 netif_info(tp, link, dev,
1488 "PHY does not support 1000Mbps\n");
1492 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1494 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1495 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1499 if (speed == SPEED_10)
1501 else if (speed == SPEED_100)
1502 bmcr = BMCR_SPEED100;
1506 if (duplex == DUPLEX_FULL)
1507 bmcr |= BMCR_FULLDPLX;
1510 rtl_writephy(tp, MII_BMCR, bmcr);
1512 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1513 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1514 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1515 rtl_writephy(tp, 0x17, 0x2138);
1516 rtl_writephy(tp, 0x0e, 0x0260);
1518 rtl_writephy(tp, 0x17, 0x2108);
1519 rtl_writephy(tp, 0x0e, 0x0000);
1528 static int rtl8169_set_speed(struct net_device *dev,
1529 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1531 struct rtl8169_private *tp = netdev_priv(dev);
1534 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1538 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1539 (advertising & ADVERTISED_1000baseT_Full)) {
1540 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1546 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1548 struct rtl8169_private *tp = netdev_priv(dev);
1549 unsigned long flags;
1552 del_timer_sync(&tp->timer);
1554 spin_lock_irqsave(&tp->lock, flags);
1555 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1556 cmd->duplex, cmd->advertising);
1557 spin_unlock_irqrestore(&tp->lock, flags);
1562 static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
1564 struct rtl8169_private *tp = netdev_priv(dev);
1566 if (dev->mtu > TD_MSS_MAX)
1567 features &= ~NETIF_F_ALL_TSO;
1569 if (dev->mtu > JUMBO_1K &&
1570 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1571 features &= ~NETIF_F_IP_CSUM;
1576 static int rtl8169_set_features(struct net_device *dev, u32 features)
1578 struct rtl8169_private *tp = netdev_priv(dev);
1579 void __iomem *ioaddr = tp->mmio_addr;
1580 unsigned long flags;
1582 spin_lock_irqsave(&tp->lock, flags);
1584 if (features & NETIF_F_RXCSUM)
1585 tp->cp_cmd |= RxChkSum;
1587 tp->cp_cmd &= ~RxChkSum;
1589 if (dev->features & NETIF_F_HW_VLAN_RX)
1590 tp->cp_cmd |= RxVlan;
1592 tp->cp_cmd &= ~RxVlan;
1594 RTL_W16(CPlusCmd, tp->cp_cmd);
1597 spin_unlock_irqrestore(&tp->lock, flags);
1602 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1603 struct sk_buff *skb)
1605 return (vlan_tx_tag_present(skb)) ?
1606 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1609 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1611 u32 opts2 = le32_to_cpu(desc->opts2);
1613 if (opts2 & RxVlanTag)
1614 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1619 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1621 struct rtl8169_private *tp = netdev_priv(dev);
1622 void __iomem *ioaddr = tp->mmio_addr;
1626 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1627 cmd->port = PORT_FIBRE;
1628 cmd->transceiver = XCVR_INTERNAL;
1630 status = RTL_R32(TBICSR);
1631 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1632 cmd->autoneg = !!(status & TBINwEnable);
1634 ethtool_cmd_speed_set(cmd, SPEED_1000);
1635 cmd->duplex = DUPLEX_FULL; /* Always set */
1640 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1642 struct rtl8169_private *tp = netdev_priv(dev);
1644 return mii_ethtool_gset(&tp->mii, cmd);
1647 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1649 struct rtl8169_private *tp = netdev_priv(dev);
1650 unsigned long flags;
1653 spin_lock_irqsave(&tp->lock, flags);
1655 rc = tp->get_settings(dev, cmd);
1657 spin_unlock_irqrestore(&tp->lock, flags);
1661 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1664 struct rtl8169_private *tp = netdev_priv(dev);
1665 unsigned long flags;
1667 if (regs->len > R8169_REGS_SIZE)
1668 regs->len = R8169_REGS_SIZE;
1670 spin_lock_irqsave(&tp->lock, flags);
1671 memcpy_fromio(p, tp->mmio_addr, regs->len);
1672 spin_unlock_irqrestore(&tp->lock, flags);
1675 static u32 rtl8169_get_msglevel(struct net_device *dev)
1677 struct rtl8169_private *tp = netdev_priv(dev);
1679 return tp->msg_enable;
1682 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1684 struct rtl8169_private *tp = netdev_priv(dev);
1686 tp->msg_enable = value;
1689 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1696 "tx_single_collisions",
1697 "tx_multi_collisions",
1705 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1709 return ARRAY_SIZE(rtl8169_gstrings);
1715 static void rtl8169_update_counters(struct net_device *dev)
1717 struct rtl8169_private *tp = netdev_priv(dev);
1718 void __iomem *ioaddr = tp->mmio_addr;
1719 struct device *d = &tp->pci_dev->dev;
1720 struct rtl8169_counters *counters;
1726 * Some chips are unable to dump tally counters when the receiver
1729 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1732 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1736 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1737 cmd = (u64)paddr & DMA_BIT_MASK(32);
1738 RTL_W32(CounterAddrLow, cmd);
1739 RTL_W32(CounterAddrLow, cmd | CounterDump);
1742 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1743 memcpy(&tp->counters, counters, sizeof(*counters));
1749 RTL_W32(CounterAddrLow, 0);
1750 RTL_W32(CounterAddrHigh, 0);
1752 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1755 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1756 struct ethtool_stats *stats, u64 *data)
1758 struct rtl8169_private *tp = netdev_priv(dev);
1762 rtl8169_update_counters(dev);
1764 data[0] = le64_to_cpu(tp->counters.tx_packets);
1765 data[1] = le64_to_cpu(tp->counters.rx_packets);
1766 data[2] = le64_to_cpu(tp->counters.tx_errors);
1767 data[3] = le32_to_cpu(tp->counters.rx_errors);
1768 data[4] = le16_to_cpu(tp->counters.rx_missed);
1769 data[5] = le16_to_cpu(tp->counters.align_errors);
1770 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1771 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1772 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1773 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1774 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1775 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1776 data[12] = le16_to_cpu(tp->counters.tx_underun);
1779 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1783 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1788 static const struct ethtool_ops rtl8169_ethtool_ops = {
1789 .get_drvinfo = rtl8169_get_drvinfo,
1790 .get_regs_len = rtl8169_get_regs_len,
1791 .get_link = ethtool_op_get_link,
1792 .get_settings = rtl8169_get_settings,
1793 .set_settings = rtl8169_set_settings,
1794 .get_msglevel = rtl8169_get_msglevel,
1795 .set_msglevel = rtl8169_set_msglevel,
1796 .get_regs = rtl8169_get_regs,
1797 .get_wol = rtl8169_get_wol,
1798 .set_wol = rtl8169_set_wol,
1799 .get_strings = rtl8169_get_strings,
1800 .get_sset_count = rtl8169_get_sset_count,
1801 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1804 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1805 struct net_device *dev, u8 default_version)
1807 void __iomem *ioaddr = tp->mmio_addr;
1809 * The driver currently handles the 8168Bf and the 8168Be identically
1810 * but they can be identified more specifically through the test below
1813 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1815 * Same thing for the 8101Eb and the 8101Ec:
1817 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1819 static const struct rtl_mac_info {
1825 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1826 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1829 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
1830 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1831 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1832 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1835 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1836 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1837 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1839 /* 8168DP family. */
1840 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1841 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1842 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
1845 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1846 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1847 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1848 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1849 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1850 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1851 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1852 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1853 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1856 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1857 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1858 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1859 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1862 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
1863 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1864 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1865 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1866 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1867 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1868 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1869 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1870 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1871 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1872 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1873 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1874 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1875 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1876 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1877 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1878 /* FIXME: where did these entries come from ? -- FR */
1879 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1880 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1883 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1884 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1885 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1886 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1887 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1888 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1891 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1893 const struct rtl_mac_info *p = mac_info;
1896 reg = RTL_R32(TxConfig);
1897 while ((reg & p->mask) != p->val)
1899 tp->mac_version = p->mac_version;
1901 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1902 netif_notice(tp, probe, dev,
1903 "unknown MAC, using family default\n");
1904 tp->mac_version = default_version;
1908 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1910 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1918 static void rtl_writephy_batch(struct rtl8169_private *tp,
1919 const struct phy_reg *regs, int len)
1922 rtl_writephy(tp, regs->reg, regs->val);
1927 #define PHY_READ 0x00000000
1928 #define PHY_DATA_OR 0x10000000
1929 #define PHY_DATA_AND 0x20000000
1930 #define PHY_BJMPN 0x30000000
1931 #define PHY_READ_EFUSE 0x40000000
1932 #define PHY_READ_MAC_BYTE 0x50000000
1933 #define PHY_WRITE_MAC_BYTE 0x60000000
1934 #define PHY_CLEAR_READCOUNT 0x70000000
1935 #define PHY_WRITE 0x80000000
1936 #define PHY_READCOUNT_EQ_SKIP 0x90000000
1937 #define PHY_COMP_EQ_SKIPN 0xa0000000
1938 #define PHY_COMP_NEQ_SKIPN 0xb0000000
1939 #define PHY_WRITE_PREVIOUS 0xc0000000
1940 #define PHY_SKIPN 0xd0000000
1941 #define PHY_DELAY_MS 0xe0000000
1942 #define PHY_WRITE_ERI_WORD 0xf0000000
1946 char version[RTL_VER_SIZE];
1952 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1954 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1956 const struct firmware *fw = rtl_fw->fw;
1957 struct fw_info *fw_info = (struct fw_info *)fw->data;
1958 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1959 char *version = rtl_fw->version;
1962 if (fw->size < FW_OPCODE_SIZE)
1965 if (!fw_info->magic) {
1966 size_t i, size, start;
1969 if (fw->size < sizeof(*fw_info))
1972 for (i = 0; i < fw->size; i++)
1973 checksum += fw->data[i];
1977 start = le32_to_cpu(fw_info->fw_start);
1978 if (start > fw->size)
1981 size = le32_to_cpu(fw_info->fw_len);
1982 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1985 memcpy(version, fw_info->version, RTL_VER_SIZE);
1987 pa->code = (__le32 *)(fw->data + start);
1990 if (fw->size % FW_OPCODE_SIZE)
1993 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1995 pa->code = (__le32 *)fw->data;
1996 pa->size = fw->size / FW_OPCODE_SIZE;
1998 version[RTL_VER_SIZE - 1] = 0;
2005 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2006 struct rtl_fw_phy_action *pa)
2011 for (index = 0; index < pa->size; index++) {
2012 u32 action = le32_to_cpu(pa->code[index]);
2013 u32 regno = (action & 0x0fff0000) >> 16;
2015 switch(action & 0xf0000000) {
2019 case PHY_READ_EFUSE:
2020 case PHY_CLEAR_READCOUNT:
2022 case PHY_WRITE_PREVIOUS:
2027 if (regno > index) {
2028 netif_err(tp, ifup, tp->dev,
2029 "Out of range of firmware\n");
2033 case PHY_READCOUNT_EQ_SKIP:
2034 if (index + 2 >= pa->size) {
2035 netif_err(tp, ifup, tp->dev,
2036 "Out of range of firmware\n");
2040 case PHY_COMP_EQ_SKIPN:
2041 case PHY_COMP_NEQ_SKIPN:
2043 if (index + 1 + regno >= pa->size) {
2044 netif_err(tp, ifup, tp->dev,
2045 "Out of range of firmware\n");
2050 case PHY_READ_MAC_BYTE:
2051 case PHY_WRITE_MAC_BYTE:
2052 case PHY_WRITE_ERI_WORD:
2054 netif_err(tp, ifup, tp->dev,
2055 "Invalid action 0x%08x\n", action);
2064 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2066 struct net_device *dev = tp->dev;
2069 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2070 netif_err(tp, ifup, dev, "invalid firwmare\n");
2074 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2080 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2082 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2086 predata = count = 0;
2088 for (index = 0; index < pa->size; ) {
2089 u32 action = le32_to_cpu(pa->code[index]);
2090 u32 data = action & 0x0000ffff;
2091 u32 regno = (action & 0x0fff0000) >> 16;
2096 switch(action & 0xf0000000) {
2098 predata = rtl_readphy(tp, regno);
2113 case PHY_READ_EFUSE:
2114 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2117 case PHY_CLEAR_READCOUNT:
2122 rtl_writephy(tp, regno, data);
2125 case PHY_READCOUNT_EQ_SKIP:
2126 index += (count == data) ? 2 : 1;
2128 case PHY_COMP_EQ_SKIPN:
2129 if (predata == data)
2133 case PHY_COMP_NEQ_SKIPN:
2134 if (predata != data)
2138 case PHY_WRITE_PREVIOUS:
2139 rtl_writephy(tp, regno, predata);
2150 case PHY_READ_MAC_BYTE:
2151 case PHY_WRITE_MAC_BYTE:
2152 case PHY_WRITE_ERI_WORD:
2159 static void rtl_release_firmware(struct rtl8169_private *tp)
2161 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2162 release_firmware(tp->rtl_fw->fw);
2165 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2168 static void rtl_apply_firmware(struct rtl8169_private *tp)
2170 struct rtl_fw *rtl_fw = tp->rtl_fw;
2172 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2173 if (!IS_ERR_OR_NULL(rtl_fw))
2174 rtl_phy_write_fw(tp, rtl_fw);
2177 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2179 if (rtl_readphy(tp, reg) != val)
2180 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2182 rtl_apply_firmware(tp);
2185 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2187 static const struct phy_reg phy_reg_init[] = {
2249 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2252 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2254 static const struct phy_reg phy_reg_init[] = {
2260 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2263 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2265 struct pci_dev *pdev = tp->pci_dev;
2267 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2268 (pdev->subsystem_device != 0xe000))
2271 rtl_writephy(tp, 0x1f, 0x0001);
2272 rtl_writephy(tp, 0x10, 0xf01b);
2273 rtl_writephy(tp, 0x1f, 0x0000);
2276 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2278 static const struct phy_reg phy_reg_init[] = {
2318 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2320 rtl8169scd_hw_phy_config_quirk(tp);
2323 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2325 static const struct phy_reg phy_reg_init[] = {
2373 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2376 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2378 static const struct phy_reg phy_reg_init[] = {
2383 rtl_writephy(tp, 0x1f, 0x0001);
2384 rtl_patchphy(tp, 0x16, 1 << 0);
2386 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2389 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2391 static const struct phy_reg phy_reg_init[] = {
2397 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2400 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2402 static const struct phy_reg phy_reg_init[] = {
2410 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2413 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2415 static const struct phy_reg phy_reg_init[] = {
2421 rtl_writephy(tp, 0x1f, 0x0000);
2422 rtl_patchphy(tp, 0x14, 1 << 5);
2423 rtl_patchphy(tp, 0x0d, 1 << 5);
2425 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2428 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2430 static const struct phy_reg phy_reg_init[] = {
2450 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2452 rtl_patchphy(tp, 0x14, 1 << 5);
2453 rtl_patchphy(tp, 0x0d, 1 << 5);
2454 rtl_writephy(tp, 0x1f, 0x0000);
2457 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2459 static const struct phy_reg phy_reg_init[] = {
2477 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2479 rtl_patchphy(tp, 0x16, 1 << 0);
2480 rtl_patchphy(tp, 0x14, 1 << 5);
2481 rtl_patchphy(tp, 0x0d, 1 << 5);
2482 rtl_writephy(tp, 0x1f, 0x0000);
2485 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2487 static const struct phy_reg phy_reg_init[] = {
2499 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2501 rtl_patchphy(tp, 0x16, 1 << 0);
2502 rtl_patchphy(tp, 0x14, 1 << 5);
2503 rtl_patchphy(tp, 0x0d, 1 << 5);
2504 rtl_writephy(tp, 0x1f, 0x0000);
2507 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2509 rtl8168c_3_hw_phy_config(tp);
2512 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2514 static const struct phy_reg phy_reg_init_0[] = {
2515 /* Channel Estimation */
2536 * Enhance line driver power
2545 * Can not link to 1Gbps with bad cable
2546 * Decrease SNR threshold form 21.07dB to 19.04dB
2554 void __iomem *ioaddr = tp->mmio_addr;
2556 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2560 * Fine Tune Switching regulator parameter
2562 rtl_writephy(tp, 0x1f, 0x0002);
2563 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2564 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2566 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2567 static const struct phy_reg phy_reg_init[] = {
2577 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2579 val = rtl_readphy(tp, 0x0d);
2581 if ((val & 0x00ff) != 0x006c) {
2582 static const u32 set[] = {
2583 0x0065, 0x0066, 0x0067, 0x0068,
2584 0x0069, 0x006a, 0x006b, 0x006c
2588 rtl_writephy(tp, 0x1f, 0x0002);
2591 for (i = 0; i < ARRAY_SIZE(set); i++)
2592 rtl_writephy(tp, 0x0d, val | set[i]);
2595 static const struct phy_reg phy_reg_init[] = {
2603 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2606 /* RSET couple improve */
2607 rtl_writephy(tp, 0x1f, 0x0002);
2608 rtl_patchphy(tp, 0x0d, 0x0300);
2609 rtl_patchphy(tp, 0x0f, 0x0010);
2611 /* Fine tune PLL performance */
2612 rtl_writephy(tp, 0x1f, 0x0002);
2613 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2614 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2616 rtl_writephy(tp, 0x1f, 0x0005);
2617 rtl_writephy(tp, 0x05, 0x001b);
2619 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2621 rtl_writephy(tp, 0x1f, 0x0000);
2624 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2626 static const struct phy_reg phy_reg_init_0[] = {
2627 /* Channel Estimation */
2648 * Enhance line driver power
2657 * Can not link to 1Gbps with bad cable
2658 * Decrease SNR threshold form 21.07dB to 19.04dB
2666 void __iomem *ioaddr = tp->mmio_addr;
2668 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2670 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2671 static const struct phy_reg phy_reg_init[] = {
2682 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2684 val = rtl_readphy(tp, 0x0d);
2685 if ((val & 0x00ff) != 0x006c) {
2686 static const u32 set[] = {
2687 0x0065, 0x0066, 0x0067, 0x0068,
2688 0x0069, 0x006a, 0x006b, 0x006c
2692 rtl_writephy(tp, 0x1f, 0x0002);
2695 for (i = 0; i < ARRAY_SIZE(set); i++)
2696 rtl_writephy(tp, 0x0d, val | set[i]);
2699 static const struct phy_reg phy_reg_init[] = {
2707 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2710 /* Fine tune PLL performance */
2711 rtl_writephy(tp, 0x1f, 0x0002);
2712 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2713 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2715 /* Switching regulator Slew rate */
2716 rtl_writephy(tp, 0x1f, 0x0002);
2717 rtl_patchphy(tp, 0x0f, 0x0017);
2719 rtl_writephy(tp, 0x1f, 0x0005);
2720 rtl_writephy(tp, 0x05, 0x001b);
2722 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2724 rtl_writephy(tp, 0x1f, 0x0000);
2727 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2729 static const struct phy_reg phy_reg_init[] = {
2785 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2788 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2790 static const struct phy_reg phy_reg_init[] = {
2800 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2801 rtl_patchphy(tp, 0x0d, 1 << 5);
2804 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
2806 static const struct phy_reg phy_reg_init[] = {
2807 /* Enable Delay cap */
2813 /* Channel estimation fine tune */
2822 /* Update PFM & 10M TX idle timer */
2834 rtl_apply_firmware(tp);
2836 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2838 /* DCO enable for 10M IDLE Power */
2839 rtl_writephy(tp, 0x1f, 0x0007);
2840 rtl_writephy(tp, 0x1e, 0x0023);
2841 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2842 rtl_writephy(tp, 0x1f, 0x0000);
2844 /* For impedance matching */
2845 rtl_writephy(tp, 0x1f, 0x0002);
2846 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2847 rtl_writephy(tp, 0x1f, 0x0000);
2849 /* PHY auto speed down */
2850 rtl_writephy(tp, 0x1f, 0x0007);
2851 rtl_writephy(tp, 0x1e, 0x002d);
2852 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2853 rtl_writephy(tp, 0x1f, 0x0000);
2854 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2856 rtl_writephy(tp, 0x1f, 0x0005);
2857 rtl_writephy(tp, 0x05, 0x8b86);
2858 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2859 rtl_writephy(tp, 0x1f, 0x0000);
2861 rtl_writephy(tp, 0x1f, 0x0005);
2862 rtl_writephy(tp, 0x05, 0x8b85);
2863 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2864 rtl_writephy(tp, 0x1f, 0x0007);
2865 rtl_writephy(tp, 0x1e, 0x0020);
2866 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2867 rtl_writephy(tp, 0x1f, 0x0006);
2868 rtl_writephy(tp, 0x00, 0x5a00);
2869 rtl_writephy(tp, 0x1f, 0x0000);
2870 rtl_writephy(tp, 0x0d, 0x0007);
2871 rtl_writephy(tp, 0x0e, 0x003c);
2872 rtl_writephy(tp, 0x0d, 0x4007);
2873 rtl_writephy(tp, 0x0e, 0x0000);
2874 rtl_writephy(tp, 0x0d, 0x0000);
2877 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2879 static const struct phy_reg phy_reg_init[] = {
2880 /* Enable Delay cap */
2889 /* Channel estimation fine tune */
2906 rtl_apply_firmware(tp);
2908 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2910 /* For 4-corner performance improve */
2911 rtl_writephy(tp, 0x1f, 0x0005);
2912 rtl_writephy(tp, 0x05, 0x8b80);
2913 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2914 rtl_writephy(tp, 0x1f, 0x0000);
2916 /* PHY auto speed down */
2917 rtl_writephy(tp, 0x1f, 0x0004);
2918 rtl_writephy(tp, 0x1f, 0x0007);
2919 rtl_writephy(tp, 0x1e, 0x002d);
2920 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2921 rtl_writephy(tp, 0x1f, 0x0002);
2922 rtl_writephy(tp, 0x1f, 0x0000);
2923 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2925 /* improve 10M EEE waveform */
2926 rtl_writephy(tp, 0x1f, 0x0005);
2927 rtl_writephy(tp, 0x05, 0x8b86);
2928 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2929 rtl_writephy(tp, 0x1f, 0x0000);
2931 /* Improve 2-pair detection performance */
2932 rtl_writephy(tp, 0x1f, 0x0005);
2933 rtl_writephy(tp, 0x05, 0x8b85);
2934 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2935 rtl_writephy(tp, 0x1f, 0x0000);
2938 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2940 rtl_writephy(tp, 0x1f, 0x0005);
2941 rtl_writephy(tp, 0x05, 0x8b85);
2942 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2943 rtl_writephy(tp, 0x1f, 0x0004);
2944 rtl_writephy(tp, 0x1f, 0x0007);
2945 rtl_writephy(tp, 0x1e, 0x0020);
2946 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
2947 rtl_writephy(tp, 0x1f, 0x0002);
2948 rtl_writephy(tp, 0x1f, 0x0000);
2949 rtl_writephy(tp, 0x0d, 0x0007);
2950 rtl_writephy(tp, 0x0e, 0x003c);
2951 rtl_writephy(tp, 0x0d, 0x4007);
2952 rtl_writephy(tp, 0x0e, 0x0000);
2953 rtl_writephy(tp, 0x0d, 0x0000);
2956 rtl_writephy(tp, 0x1f, 0x0003);
2957 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2958 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2959 rtl_writephy(tp, 0x1f, 0x0000);
2962 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2964 static const struct phy_reg phy_reg_init[] = {
2965 /* Channel estimation fine tune */
2970 /* Modify green table for giga & fnet */
2987 /* Modify green table for 10M */
2993 /* Disable hiimpedance detection (RTCT) */
2999 rtl_apply_firmware(tp);
3001 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3003 /* For 4-corner performance improve */
3004 rtl_writephy(tp, 0x1f, 0x0005);
3005 rtl_writephy(tp, 0x05, 0x8b80);
3006 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3007 rtl_writephy(tp, 0x1f, 0x0000);
3009 /* PHY auto speed down */
3010 rtl_writephy(tp, 0x1f, 0x0007);
3011 rtl_writephy(tp, 0x1e, 0x002d);
3012 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3013 rtl_writephy(tp, 0x1f, 0x0000);
3014 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3016 /* Improve 10M EEE waveform */
3017 rtl_writephy(tp, 0x1f, 0x0005);
3018 rtl_writephy(tp, 0x05, 0x8b86);
3019 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3022 /* Improve 2-pair detection performance */
3023 rtl_writephy(tp, 0x1f, 0x0005);
3024 rtl_writephy(tp, 0x05, 0x8b85);
3025 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3026 rtl_writephy(tp, 0x1f, 0x0000);
3029 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3031 rtl_apply_firmware(tp);
3033 /* For 4-corner performance improve */
3034 rtl_writephy(tp, 0x1f, 0x0005);
3035 rtl_writephy(tp, 0x05, 0x8b80);
3036 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3037 rtl_writephy(tp, 0x1f, 0x0000);
3039 /* PHY auto speed down */
3040 rtl_writephy(tp, 0x1f, 0x0007);
3041 rtl_writephy(tp, 0x1e, 0x002d);
3042 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3043 rtl_writephy(tp, 0x1f, 0x0000);
3044 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3046 /* Improve 10M EEE waveform */
3047 rtl_writephy(tp, 0x1f, 0x0005);
3048 rtl_writephy(tp, 0x05, 0x8b86);
3049 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3050 rtl_writephy(tp, 0x1f, 0x0000);
3053 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3055 static const struct phy_reg phy_reg_init[] = {
3062 rtl_writephy(tp, 0x1f, 0x0000);
3063 rtl_patchphy(tp, 0x11, 1 << 12);
3064 rtl_patchphy(tp, 0x19, 1 << 13);
3065 rtl_patchphy(tp, 0x10, 1 << 15);
3067 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3070 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3072 static const struct phy_reg phy_reg_init[] = {
3086 /* Disable ALDPS before ram code */
3087 rtl_writephy(tp, 0x1f, 0x0000);
3088 rtl_writephy(tp, 0x18, 0x0310);
3091 rtl_apply_firmware(tp);
3093 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3096 static void rtl_hw_phy_config(struct net_device *dev)
3098 struct rtl8169_private *tp = netdev_priv(dev);
3100 rtl8169_print_mac_version(tp);
3102 switch (tp->mac_version) {
3103 case RTL_GIGA_MAC_VER_01:
3105 case RTL_GIGA_MAC_VER_02:
3106 case RTL_GIGA_MAC_VER_03:
3107 rtl8169s_hw_phy_config(tp);
3109 case RTL_GIGA_MAC_VER_04:
3110 rtl8169sb_hw_phy_config(tp);
3112 case RTL_GIGA_MAC_VER_05:
3113 rtl8169scd_hw_phy_config(tp);
3115 case RTL_GIGA_MAC_VER_06:
3116 rtl8169sce_hw_phy_config(tp);
3118 case RTL_GIGA_MAC_VER_07:
3119 case RTL_GIGA_MAC_VER_08:
3120 case RTL_GIGA_MAC_VER_09:
3121 rtl8102e_hw_phy_config(tp);
3123 case RTL_GIGA_MAC_VER_11:
3124 rtl8168bb_hw_phy_config(tp);
3126 case RTL_GIGA_MAC_VER_12:
3127 rtl8168bef_hw_phy_config(tp);
3129 case RTL_GIGA_MAC_VER_17:
3130 rtl8168bef_hw_phy_config(tp);
3132 case RTL_GIGA_MAC_VER_18:
3133 rtl8168cp_1_hw_phy_config(tp);
3135 case RTL_GIGA_MAC_VER_19:
3136 rtl8168c_1_hw_phy_config(tp);
3138 case RTL_GIGA_MAC_VER_20:
3139 rtl8168c_2_hw_phy_config(tp);
3141 case RTL_GIGA_MAC_VER_21:
3142 rtl8168c_3_hw_phy_config(tp);
3144 case RTL_GIGA_MAC_VER_22:
3145 rtl8168c_4_hw_phy_config(tp);
3147 case RTL_GIGA_MAC_VER_23:
3148 case RTL_GIGA_MAC_VER_24:
3149 rtl8168cp_2_hw_phy_config(tp);
3151 case RTL_GIGA_MAC_VER_25:
3152 rtl8168d_1_hw_phy_config(tp);
3154 case RTL_GIGA_MAC_VER_26:
3155 rtl8168d_2_hw_phy_config(tp);
3157 case RTL_GIGA_MAC_VER_27:
3158 rtl8168d_3_hw_phy_config(tp);
3160 case RTL_GIGA_MAC_VER_28:
3161 rtl8168d_4_hw_phy_config(tp);
3163 case RTL_GIGA_MAC_VER_29:
3164 case RTL_GIGA_MAC_VER_30:
3165 rtl8105e_hw_phy_config(tp);
3167 case RTL_GIGA_MAC_VER_31:
3170 case RTL_GIGA_MAC_VER_32:
3171 case RTL_GIGA_MAC_VER_33:
3172 rtl8168e_1_hw_phy_config(tp);
3174 case RTL_GIGA_MAC_VER_34:
3175 rtl8168e_2_hw_phy_config(tp);
3177 case RTL_GIGA_MAC_VER_35:
3178 rtl8168f_1_hw_phy_config(tp);
3180 case RTL_GIGA_MAC_VER_36:
3181 rtl8168f_2_hw_phy_config(tp);
3189 static void rtl8169_phy_timer(unsigned long __opaque)
3191 struct net_device *dev = (struct net_device *)__opaque;
3192 struct rtl8169_private *tp = netdev_priv(dev);
3193 struct timer_list *timer = &tp->timer;
3194 void __iomem *ioaddr = tp->mmio_addr;
3195 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3197 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
3199 spin_lock_irq(&tp->lock);
3201 if (tp->phy_reset_pending(tp)) {
3203 * A busy loop could burn quite a few cycles on nowadays CPU.
3204 * Let's delay the execution of the timer for a few ticks.
3210 if (tp->link_ok(ioaddr))
3213 netif_warn(tp, link, dev, "PHY reset until link up\n");
3215 tp->phy_reset_enable(tp);
3218 mod_timer(timer, jiffies + timeout);
3220 spin_unlock_irq(&tp->lock);
3223 #ifdef CONFIG_NET_POLL_CONTROLLER
3225 * Polling 'interrupt' - used by things like netconsole to send skbs
3226 * without having to re-enable interrupts. It's not called while
3227 * the interrupt routine is executing.
3229 static void rtl8169_netpoll(struct net_device *dev)
3231 struct rtl8169_private *tp = netdev_priv(dev);
3232 struct pci_dev *pdev = tp->pci_dev;
3234 disable_irq(pdev->irq);
3235 rtl8169_interrupt(pdev->irq, dev);
3236 enable_irq(pdev->irq);
3240 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3241 void __iomem *ioaddr)
3244 pci_release_regions(pdev);
3245 pci_clear_mwi(pdev);
3246 pci_disable_device(pdev);
3250 static void rtl8169_phy_reset(struct net_device *dev,
3251 struct rtl8169_private *tp)
3255 tp->phy_reset_enable(tp);
3256 for (i = 0; i < 100; i++) {
3257 if (!tp->phy_reset_pending(tp))
3261 netif_err(tp, link, dev, "PHY reset failed\n");
3264 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3266 void __iomem *ioaddr = tp->mmio_addr;
3268 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3269 (RTL_R8(PHYstatus) & TBI_Enable);
3272 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3274 void __iomem *ioaddr = tp->mmio_addr;
3276 rtl_hw_phy_config(dev);
3278 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3279 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3283 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3285 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3286 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3288 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
3289 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3291 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
3292 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
3295 rtl8169_phy_reset(dev, tp);
3297 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3298 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3299 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3300 (tp->mii.supports_gmii ?
3301 ADVERTISED_1000baseT_Half |
3302 ADVERTISED_1000baseT_Full : 0));
3304 if (rtl_tbi_enabled(tp))
3305 netif_info(tp, link, dev, "TBI auto-negotiating\n");
3308 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3310 void __iomem *ioaddr = tp->mmio_addr;
3314 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3315 high = addr[4] | (addr[5] << 8);
3317 spin_lock_irq(&tp->lock);
3319 RTL_W8(Cfg9346, Cfg9346_Unlock);
3321 RTL_W32(MAC4, high);
3327 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3328 const struct exgmac_reg e[] = {
3329 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3330 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3331 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3332 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3336 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3339 RTL_W8(Cfg9346, Cfg9346_Lock);
3341 spin_unlock_irq(&tp->lock);
3344 static int rtl_set_mac_address(struct net_device *dev, void *p)
3346 struct rtl8169_private *tp = netdev_priv(dev);
3347 struct sockaddr *addr = p;
3349 if (!is_valid_ether_addr(addr->sa_data))
3350 return -EADDRNOTAVAIL;
3352 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3354 rtl_rar_set(tp, dev->dev_addr);
3359 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3361 struct rtl8169_private *tp = netdev_priv(dev);
3362 struct mii_ioctl_data *data = if_mii(ifr);
3364 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3367 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3368 struct mii_ioctl_data *data, int cmd)
3372 data->phy_id = 32; /* Internal PHY */
3376 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3380 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3386 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3391 static const struct rtl_cfg_info {
3392 void (*hw_start)(struct net_device *);
3393 unsigned int region;
3399 } rtl_cfg_infos [] = {
3401 .hw_start = rtl_hw_start_8169,
3404 .intr_event = SYSErr | LinkChg | RxOverflow |
3405 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
3406 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
3407 .features = RTL_FEATURE_GMII,
3408 .default_ver = RTL_GIGA_MAC_VER_01,
3411 .hw_start = rtl_hw_start_8168,
3414 .intr_event = SYSErr | LinkChg | RxOverflow |
3415 TxErr | TxOK | RxOK | RxErr,
3416 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
3417 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3418 .default_ver = RTL_GIGA_MAC_VER_11,
3421 .hw_start = rtl_hw_start_8101,
3424 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3425 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
3426 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
3427 .features = RTL_FEATURE_MSI,
3428 .default_ver = RTL_GIGA_MAC_VER_13,
3432 /* Cfg9346_Unlock assumed. */
3433 static unsigned rtl_try_msi(struct rtl8169_private *tp,
3434 const struct rtl_cfg_info *cfg)
3436 void __iomem *ioaddr = tp->mmio_addr;
3440 cfg2 = RTL_R8(Config2) & ~MSIEnable;
3441 if (cfg->features & RTL_FEATURE_MSI) {
3442 if (pci_enable_msi(tp->pci_dev)) {
3443 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
3446 msi = RTL_FEATURE_MSI;
3449 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3450 RTL_W8(Config2, cfg2);
3454 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3456 if (tp->features & RTL_FEATURE_MSI) {
3457 pci_disable_msi(pdev);
3458 tp->features &= ~RTL_FEATURE_MSI;
3462 static const struct net_device_ops rtl8169_netdev_ops = {
3463 .ndo_open = rtl8169_open,
3464 .ndo_stop = rtl8169_close,
3465 .ndo_get_stats = rtl8169_get_stats,
3466 .ndo_start_xmit = rtl8169_start_xmit,
3467 .ndo_tx_timeout = rtl8169_tx_timeout,
3468 .ndo_validate_addr = eth_validate_addr,
3469 .ndo_change_mtu = rtl8169_change_mtu,
3470 .ndo_fix_features = rtl8169_fix_features,
3471 .ndo_set_features = rtl8169_set_features,
3472 .ndo_set_mac_address = rtl_set_mac_address,
3473 .ndo_do_ioctl = rtl8169_ioctl,
3474 .ndo_set_rx_mode = rtl_set_rx_mode,
3475 #ifdef CONFIG_NET_POLL_CONTROLLER
3476 .ndo_poll_controller = rtl8169_netpoll,
3481 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3483 struct mdio_ops *ops = &tp->mdio_ops;
3485 switch (tp->mac_version) {
3486 case RTL_GIGA_MAC_VER_27:
3487 ops->write = r8168dp_1_mdio_write;
3488 ops->read = r8168dp_1_mdio_read;
3490 case RTL_GIGA_MAC_VER_28:
3491 case RTL_GIGA_MAC_VER_31:
3492 ops->write = r8168dp_2_mdio_write;
3493 ops->read = r8168dp_2_mdio_read;
3496 ops->write = r8169_mdio_write;
3497 ops->read = r8169_mdio_read;
3502 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3504 void __iomem *ioaddr = tp->mmio_addr;
3506 switch (tp->mac_version) {
3507 case RTL_GIGA_MAC_VER_29:
3508 case RTL_GIGA_MAC_VER_30:
3509 case RTL_GIGA_MAC_VER_32:
3510 case RTL_GIGA_MAC_VER_33:
3511 case RTL_GIGA_MAC_VER_34:
3512 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3513 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3520 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3522 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3525 rtl_writephy(tp, 0x1f, 0x0000);
3526 rtl_writephy(tp, MII_BMCR, 0x0000);
3528 rtl_wol_suspend_quirk(tp);
3533 static void r810x_phy_power_down(struct rtl8169_private *tp)
3535 rtl_writephy(tp, 0x1f, 0x0000);
3536 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3539 static void r810x_phy_power_up(struct rtl8169_private *tp)
3541 rtl_writephy(tp, 0x1f, 0x0000);
3542 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3545 static void r810x_pll_power_down(struct rtl8169_private *tp)
3547 if (rtl_wol_pll_power_down(tp))
3550 r810x_phy_power_down(tp);
3553 static void r810x_pll_power_up(struct rtl8169_private *tp)
3555 r810x_phy_power_up(tp);
3558 static void r8168_phy_power_up(struct rtl8169_private *tp)
3560 rtl_writephy(tp, 0x1f, 0x0000);
3561 switch (tp->mac_version) {
3562 case RTL_GIGA_MAC_VER_11:
3563 case RTL_GIGA_MAC_VER_12:
3564 case RTL_GIGA_MAC_VER_17:
3565 case RTL_GIGA_MAC_VER_18:
3566 case RTL_GIGA_MAC_VER_19:
3567 case RTL_GIGA_MAC_VER_20:
3568 case RTL_GIGA_MAC_VER_21:
3569 case RTL_GIGA_MAC_VER_22:
3570 case RTL_GIGA_MAC_VER_23:
3571 case RTL_GIGA_MAC_VER_24:
3572 case RTL_GIGA_MAC_VER_25:
3573 case RTL_GIGA_MAC_VER_26:
3574 case RTL_GIGA_MAC_VER_27:
3575 case RTL_GIGA_MAC_VER_28:
3576 case RTL_GIGA_MAC_VER_31:
3577 rtl_writephy(tp, 0x0e, 0x0000);
3582 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3585 static void r8168_phy_power_down(struct rtl8169_private *tp)
3587 rtl_writephy(tp, 0x1f, 0x0000);
3588 switch (tp->mac_version) {
3589 case RTL_GIGA_MAC_VER_32:
3590 case RTL_GIGA_MAC_VER_33:
3591 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3594 case RTL_GIGA_MAC_VER_11:
3595 case RTL_GIGA_MAC_VER_12:
3596 case RTL_GIGA_MAC_VER_17:
3597 case RTL_GIGA_MAC_VER_18:
3598 case RTL_GIGA_MAC_VER_19:
3599 case RTL_GIGA_MAC_VER_20:
3600 case RTL_GIGA_MAC_VER_21:
3601 case RTL_GIGA_MAC_VER_22:
3602 case RTL_GIGA_MAC_VER_23:
3603 case RTL_GIGA_MAC_VER_24:
3604 case RTL_GIGA_MAC_VER_25:
3605 case RTL_GIGA_MAC_VER_26:
3606 case RTL_GIGA_MAC_VER_27:
3607 case RTL_GIGA_MAC_VER_28:
3608 case RTL_GIGA_MAC_VER_31:
3609 rtl_writephy(tp, 0x0e, 0x0200);
3611 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3616 static void r8168_pll_power_down(struct rtl8169_private *tp)
3618 void __iomem *ioaddr = tp->mmio_addr;
3620 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3621 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3622 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3623 r8168dp_check_dash(tp)) {
3627 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3628 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
3629 (RTL_R16(CPlusCmd) & ASF)) {
3633 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3634 tp->mac_version == RTL_GIGA_MAC_VER_33)
3635 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3637 if (rtl_wol_pll_power_down(tp))
3640 r8168_phy_power_down(tp);
3642 switch (tp->mac_version) {
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 case RTL_GIGA_MAC_VER_32:
3649 case RTL_GIGA_MAC_VER_33:
3650 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3655 static void r8168_pll_power_up(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 switch (tp->mac_version) {
3667 case RTL_GIGA_MAC_VER_25:
3668 case RTL_GIGA_MAC_VER_26:
3669 case RTL_GIGA_MAC_VER_27:
3670 case RTL_GIGA_MAC_VER_28:
3671 case RTL_GIGA_MAC_VER_31:
3672 case RTL_GIGA_MAC_VER_32:
3673 case RTL_GIGA_MAC_VER_33:
3674 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3678 r8168_phy_power_up(tp);
3681 static void rtl_generic_op(struct rtl8169_private *tp,
3682 void (*op)(struct rtl8169_private *))
3688 static void rtl_pll_power_down(struct rtl8169_private *tp)
3690 rtl_generic_op(tp, tp->pll_power_ops.down);
3693 static void rtl_pll_power_up(struct rtl8169_private *tp)
3695 rtl_generic_op(tp, tp->pll_power_ops.up);
3698 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3700 struct pll_power_ops *ops = &tp->pll_power_ops;
3702 switch (tp->mac_version) {
3703 case RTL_GIGA_MAC_VER_07:
3704 case RTL_GIGA_MAC_VER_08:
3705 case RTL_GIGA_MAC_VER_09:
3706 case RTL_GIGA_MAC_VER_10:
3707 case RTL_GIGA_MAC_VER_16:
3708 case RTL_GIGA_MAC_VER_29:
3709 case RTL_GIGA_MAC_VER_30:
3710 ops->down = r810x_pll_power_down;
3711 ops->up = r810x_pll_power_up;
3714 case RTL_GIGA_MAC_VER_11:
3715 case RTL_GIGA_MAC_VER_12:
3716 case RTL_GIGA_MAC_VER_17:
3717 case RTL_GIGA_MAC_VER_18:
3718 case RTL_GIGA_MAC_VER_19:
3719 case RTL_GIGA_MAC_VER_20:
3720 case RTL_GIGA_MAC_VER_21:
3721 case RTL_GIGA_MAC_VER_22:
3722 case RTL_GIGA_MAC_VER_23:
3723 case RTL_GIGA_MAC_VER_24:
3724 case RTL_GIGA_MAC_VER_25:
3725 case RTL_GIGA_MAC_VER_26:
3726 case RTL_GIGA_MAC_VER_27:
3727 case RTL_GIGA_MAC_VER_28:
3728 case RTL_GIGA_MAC_VER_31:
3729 case RTL_GIGA_MAC_VER_32:
3730 case RTL_GIGA_MAC_VER_33:
3731 case RTL_GIGA_MAC_VER_34:
3732 case RTL_GIGA_MAC_VER_35:
3733 case RTL_GIGA_MAC_VER_36:
3734 ops->down = r8168_pll_power_down;
3735 ops->up = r8168_pll_power_up;
3745 static void rtl_init_rxcfg(struct rtl8169_private *tp)
3747 void __iomem *ioaddr = tp->mmio_addr;
3749 switch (tp->mac_version) {
3750 case RTL_GIGA_MAC_VER_01:
3751 case RTL_GIGA_MAC_VER_02:
3752 case RTL_GIGA_MAC_VER_03:
3753 case RTL_GIGA_MAC_VER_04:
3754 case RTL_GIGA_MAC_VER_05:
3755 case RTL_GIGA_MAC_VER_06:
3756 case RTL_GIGA_MAC_VER_10:
3757 case RTL_GIGA_MAC_VER_11:
3758 case RTL_GIGA_MAC_VER_12:
3759 case RTL_GIGA_MAC_VER_13:
3760 case RTL_GIGA_MAC_VER_14:
3761 case RTL_GIGA_MAC_VER_15:
3762 case RTL_GIGA_MAC_VER_16:
3763 case RTL_GIGA_MAC_VER_17:
3764 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3766 case RTL_GIGA_MAC_VER_18:
3767 case RTL_GIGA_MAC_VER_19:
3768 case RTL_GIGA_MAC_VER_20:
3769 case RTL_GIGA_MAC_VER_21:
3770 case RTL_GIGA_MAC_VER_22:
3771 case RTL_GIGA_MAC_VER_23:
3772 case RTL_GIGA_MAC_VER_24:
3773 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3776 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3781 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3783 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3786 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3788 void __iomem *ioaddr = tp->mmio_addr;
3790 RTL_W8(Cfg9346, Cfg9346_Unlock);
3791 rtl_generic_op(tp, tp->jumbo_ops.enable);
3792 RTL_W8(Cfg9346, Cfg9346_Lock);
3795 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3797 void __iomem *ioaddr = tp->mmio_addr;
3799 RTL_W8(Cfg9346, Cfg9346_Unlock);
3800 rtl_generic_op(tp, tp->jumbo_ops.disable);
3801 RTL_W8(Cfg9346, Cfg9346_Lock);
3804 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3806 void __iomem *ioaddr = tp->mmio_addr;
3808 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3809 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3810 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3813 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3815 void __iomem *ioaddr = tp->mmio_addr;
3817 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3818 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3819 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3822 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3824 void __iomem *ioaddr = tp->mmio_addr;
3826 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3829 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3831 void __iomem *ioaddr = tp->mmio_addr;
3833 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3836 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3838 void __iomem *ioaddr = tp->mmio_addr;
3839 struct pci_dev *pdev = tp->pci_dev;
3841 RTL_W8(MaxTxPacketSize, 0x3f);
3842 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3843 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
3844 pci_write_config_byte(pdev, 0x79, 0x20);
3847 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3849 void __iomem *ioaddr = tp->mmio_addr;
3850 struct pci_dev *pdev = tp->pci_dev;
3852 RTL_W8(MaxTxPacketSize, 0x0c);
3853 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3854 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
3855 pci_write_config_byte(pdev, 0x79, 0x50);
3858 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3860 rtl_tx_performance_tweak(tp->pci_dev,
3861 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3864 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3866 rtl_tx_performance_tweak(tp->pci_dev,
3867 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3870 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3872 void __iomem *ioaddr = tp->mmio_addr;
3874 r8168b_0_hw_jumbo_enable(tp);
3876 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3879 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3881 void __iomem *ioaddr = tp->mmio_addr;
3883 r8168b_0_hw_jumbo_disable(tp);
3885 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3888 static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3890 struct jumbo_ops *ops = &tp->jumbo_ops;
3892 switch (tp->mac_version) {
3893 case RTL_GIGA_MAC_VER_11:
3894 ops->disable = r8168b_0_hw_jumbo_disable;
3895 ops->enable = r8168b_0_hw_jumbo_enable;
3897 case RTL_GIGA_MAC_VER_12:
3898 case RTL_GIGA_MAC_VER_17:
3899 ops->disable = r8168b_1_hw_jumbo_disable;
3900 ops->enable = r8168b_1_hw_jumbo_enable;
3902 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3903 case RTL_GIGA_MAC_VER_19:
3904 case RTL_GIGA_MAC_VER_20:
3905 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3906 case RTL_GIGA_MAC_VER_22:
3907 case RTL_GIGA_MAC_VER_23:
3908 case RTL_GIGA_MAC_VER_24:
3909 case RTL_GIGA_MAC_VER_25:
3910 case RTL_GIGA_MAC_VER_26:
3911 ops->disable = r8168c_hw_jumbo_disable;
3912 ops->enable = r8168c_hw_jumbo_enable;
3914 case RTL_GIGA_MAC_VER_27:
3915 case RTL_GIGA_MAC_VER_28:
3916 ops->disable = r8168dp_hw_jumbo_disable;
3917 ops->enable = r8168dp_hw_jumbo_enable;
3919 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3920 case RTL_GIGA_MAC_VER_32:
3921 case RTL_GIGA_MAC_VER_33:
3922 case RTL_GIGA_MAC_VER_34:
3923 ops->disable = r8168e_hw_jumbo_disable;
3924 ops->enable = r8168e_hw_jumbo_enable;
3928 * No action needed for jumbo frames with 8169.
3929 * No jumbo for 810x at all.
3932 ops->disable = NULL;
3938 static void rtl_hw_reset(struct rtl8169_private *tp)
3940 void __iomem *ioaddr = tp->mmio_addr;
3943 /* Soft reset the chip. */
3944 RTL_W8(ChipCmd, CmdReset);
3946 /* Check that the chip has finished the reset. */
3947 for (i = 0; i < 100; i++) {
3948 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3954 static int __devinit
3955 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3957 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3958 const unsigned int region = cfg->region;
3959 struct rtl8169_private *tp;
3960 struct mii_if_info *mii;
3961 struct net_device *dev;
3962 void __iomem *ioaddr;
3966 if (netif_msg_drv(&debug)) {
3967 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3968 MODULENAME, RTL8169_VERSION);
3971 dev = alloc_etherdev(sizeof (*tp));
3973 if (netif_msg_drv(&debug))
3974 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
3979 SET_NETDEV_DEV(dev, &pdev->dev);
3980 dev->netdev_ops = &rtl8169_netdev_ops;
3981 tp = netdev_priv(dev);
3984 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
3988 mii->mdio_read = rtl_mdio_read;
3989 mii->mdio_write = rtl_mdio_write;
3990 mii->phy_id_mask = 0x1f;
3991 mii->reg_num_mask = 0x1f;
3992 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3994 /* disable ASPM completely as that cause random device stop working
3995 * problems as well as full system hangs for some PCIe devices users */
3996 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3997 PCIE_LINK_STATE_CLKPM);
3999 /* enable device (incl. PCI PM wakeup and hotplug setup) */
4000 rc = pci_enable_device(pdev);
4002 netif_err(tp, probe, dev, "enable failure\n");
4003 goto err_out_free_dev_1;
4006 if (pci_set_mwi(pdev) < 0)
4007 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
4009 /* make sure PCI base addr 1 is MMIO */
4010 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
4011 netif_err(tp, probe, dev,
4012 "region #%d not an MMIO resource, aborting\n",
4018 /* check for weird/broken PCI region reporting */
4019 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
4020 netif_err(tp, probe, dev,
4021 "Invalid PCI region size(s), aborting\n");
4026 rc = pci_request_regions(pdev, MODULENAME);
4028 netif_err(tp, probe, dev, "could not request regions\n");
4032 tp->cp_cmd = RxChkSum;
4034 if ((sizeof(dma_addr_t) > 4) &&
4035 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
4036 tp->cp_cmd |= PCIDAC;
4037 dev->features |= NETIF_F_HIGHDMA;
4039 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4041 netif_err(tp, probe, dev, "DMA configuration failed\n");
4042 goto err_out_free_res_3;
4046 /* ioremap MMIO region */
4047 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
4049 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
4051 goto err_out_free_res_3;
4053 tp->mmio_addr = ioaddr;
4055 if (!pci_is_pcie(pdev))
4056 netif_info(tp, probe, dev, "not PCI Express\n");
4058 /* Identify chip attached to board */
4059 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4063 RTL_W16(IntrMask, 0x0000);
4067 RTL_W16(IntrStatus, 0xffff);
4069 pci_set_master(pdev);
4072 * Pretend we are using VLANs; This bypasses a nasty bug where
4073 * Interrupts stop flowing on high load on 8110SCd controllers.
4075 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4076 tp->cp_cmd |= RxVlan;
4078 rtl_init_mdio_ops(tp);
4079 rtl_init_pll_power_ops(tp);
4080 rtl_init_jumbo_ops(tp);
4082 rtl8169_print_mac_version(tp);
4084 chipset = tp->mac_version;
4085 tp->txd_version = rtl_chip_infos[chipset].txd_version;
4087 RTL_W8(Cfg9346, Cfg9346_Unlock);
4088 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4089 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
4090 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4091 tp->features |= RTL_FEATURE_WOL;
4092 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4093 tp->features |= RTL_FEATURE_WOL;
4094 tp->features |= rtl_try_msi(tp, cfg);
4095 RTL_W8(Cfg9346, Cfg9346_Lock);
4097 if (rtl_tbi_enabled(tp)) {
4098 tp->set_speed = rtl8169_set_speed_tbi;
4099 tp->get_settings = rtl8169_gset_tbi;
4100 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4101 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4102 tp->link_ok = rtl8169_tbi_link_ok;
4103 tp->do_ioctl = rtl_tbi_ioctl;
4105 tp->set_speed = rtl8169_set_speed_xmii;
4106 tp->get_settings = rtl8169_gset_xmii;
4107 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4108 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4109 tp->link_ok = rtl8169_xmii_link_ok;
4110 tp->do_ioctl = rtl_xmii_ioctl;
4113 spin_lock_init(&tp->lock);
4115 /* Get MAC address */
4116 for (i = 0; i < MAC_ADDR_LEN; i++)
4117 dev->dev_addr[i] = RTL_R8(MAC0 + i);
4118 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
4120 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
4121 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4122 dev->irq = pdev->irq;
4123 dev->base_addr = (unsigned long) ioaddr;
4125 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
4127 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4128 * properly for all devices */
4129 dev->features |= NETIF_F_RXCSUM |
4130 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4132 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4133 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4134 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4137 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4138 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4139 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
4141 tp->intr_mask = 0xffff;
4142 tp->hw_start = cfg->hw_start;
4143 tp->intr_event = cfg->intr_event;
4144 tp->napi_event = cfg->napi_event;
4146 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4147 ~(RxBOVF | RxFOVF) : ~0;
4149 init_timer(&tp->timer);
4150 tp->timer.data = (unsigned long) dev;
4151 tp->timer.function = rtl8169_phy_timer;
4153 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
4155 rc = register_netdev(dev);
4159 pci_set_drvdata(pdev, dev);
4161 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
4162 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
4163 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
4164 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4165 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4166 "tx checksumming: %s]\n",
4167 rtl_chip_infos[chipset].jumbo_max,
4168 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4171 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4172 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4173 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4174 rtl8168_driver_start(tp);
4177 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
4179 if (pci_dev_run_wake(pdev))
4180 pm_runtime_put_noidle(&pdev->dev);
4182 netif_carrier_off(dev);
4188 rtl_disable_msi(pdev, tp);
4191 pci_release_regions(pdev);
4193 pci_clear_mwi(pdev);
4194 pci_disable_device(pdev);
4200 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
4202 struct net_device *dev = pci_get_drvdata(pdev);
4203 struct rtl8169_private *tp = netdev_priv(dev);
4205 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4206 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4207 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4208 rtl8168_driver_stop(tp);
4211 cancel_delayed_work_sync(&tp->task);
4213 unregister_netdev(dev);
4215 rtl_release_firmware(tp);
4217 if (pci_dev_run_wake(pdev))
4218 pm_runtime_get_noresume(&pdev->dev);
4220 /* restore original MAC address */
4221 rtl_rar_set(tp, dev->perm_addr);
4223 rtl_disable_msi(pdev, tp);
4224 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4225 pci_set_drvdata(pdev, NULL);
4228 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4230 struct rtl_fw *rtl_fw;
4234 name = rtl_lookup_firmware_name(tp);
4236 goto out_no_firmware;
4238 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4242 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4246 rc = rtl_check_firmware(tp, rtl_fw);
4248 goto err_release_firmware;
4250 tp->rtl_fw = rtl_fw;
4254 err_release_firmware:
4255 release_firmware(rtl_fw->fw);
4259 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4266 static void rtl_request_firmware(struct rtl8169_private *tp)
4268 if (IS_ERR(tp->rtl_fw))
4269 rtl_request_uncached_firmware(tp);
4272 static int rtl8169_open(struct net_device *dev)
4274 struct rtl8169_private *tp = netdev_priv(dev);
4275 void __iomem *ioaddr = tp->mmio_addr;
4276 struct pci_dev *pdev = tp->pci_dev;
4277 int retval = -ENOMEM;
4279 pm_runtime_get_sync(&pdev->dev);
4282 * Rx and Tx desscriptors needs 256 bytes alignment.
4283 * dma_alloc_coherent provides more.
4285 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4286 &tp->TxPhyAddr, GFP_KERNEL);
4287 if (!tp->TxDescArray)
4288 goto err_pm_runtime_put;
4290 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4291 &tp->RxPhyAddr, GFP_KERNEL);
4292 if (!tp->RxDescArray)
4295 retval = rtl8169_init_ring(dev);
4299 INIT_DELAYED_WORK(&tp->task, NULL);
4303 rtl_request_firmware(tp);
4305 retval = request_irq(dev->irq, rtl8169_interrupt,
4306 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
4309 goto err_release_fw_2;
4311 napi_enable(&tp->napi);
4313 rtl8169_init_phy(dev, tp);
4315 rtl8169_set_features(dev, dev->features);
4317 rtl_pll_power_up(tp);
4321 tp->saved_wolopts = 0;
4322 pm_runtime_put_noidle(&pdev->dev);
4324 rtl8169_check_link_status(dev, tp, ioaddr);
4329 rtl_release_firmware(tp);
4330 rtl8169_rx_clear(tp);
4332 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4334 tp->RxDescArray = NULL;
4336 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4338 tp->TxDescArray = NULL;
4340 pm_runtime_put_noidle(&pdev->dev);
4344 static void rtl_rx_close(struct rtl8169_private *tp)
4346 void __iomem *ioaddr = tp->mmio_addr;
4348 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4351 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4353 void __iomem *ioaddr = tp->mmio_addr;
4355 /* Disable interrupts */
4356 rtl8169_irq_mask_and_ack(tp);
4360 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4361 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4362 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4363 while (RTL_R8(TxPoll) & NPQ)
4365 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4366 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4367 tp->mac_version == RTL_GIGA_MAC_VER_36) {
4368 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4369 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4372 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4379 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4381 void __iomem *ioaddr = tp->mmio_addr;
4383 /* Set DMA burst size and Interframe Gap Time */
4384 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4385 (InterFrameGap << TxInterFrameGapShift));
4388 static void rtl_hw_start(struct net_device *dev)
4390 struct rtl8169_private *tp = netdev_priv(dev);
4394 netif_start_queue(dev);
4397 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4398 void __iomem *ioaddr)
4401 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4402 * register to be written before TxDescAddrLow to work.
4403 * Switching from MMIO to I/O access fixes the issue as well.
4405 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4406 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4407 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4408 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4411 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4415 cmd = RTL_R16(CPlusCmd);
4416 RTL_W16(CPlusCmd, cmd);
4420 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
4422 /* Low hurts. Let's disable the filtering. */
4423 RTL_W16(RxMaxSize, rx_buf_sz + 1);
4426 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4428 static const struct rtl_cfg2_info {
4433 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4434 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4435 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4436 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4438 const struct rtl_cfg2_info *p = cfg2_info;
4442 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
4443 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4444 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4445 RTL_W32(0x7c, p->val);
4451 static void rtl_hw_start_8169(struct net_device *dev)
4453 struct rtl8169_private *tp = netdev_priv(dev);
4454 void __iomem *ioaddr = tp->mmio_addr;
4455 struct pci_dev *pdev = tp->pci_dev;
4457 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4458 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4459 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4462 RTL_W8(Cfg9346, Cfg9346_Unlock);
4463 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4464 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4465 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4466 tp->mac_version == RTL_GIGA_MAC_VER_04)
4467 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4471 RTL_W8(EarlyTxThres, NoEarlyTx);
4473 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4475 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4476 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4477 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4478 tp->mac_version == RTL_GIGA_MAC_VER_04)
4479 rtl_set_rx_tx_config_registers(tp);
4481 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4483 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4484 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4485 dprintk("Set MAC Reg C+CR Offset 0xE0. "
4486 "Bit-3 and bit-14 MUST be 1\n");
4487 tp->cp_cmd |= (1 << 14);
4490 RTL_W16(CPlusCmd, tp->cp_cmd);
4492 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4495 * Undocumented corner. Supposedly:
4496 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4498 RTL_W16(IntrMitigate, 0x0000);
4500 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4502 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4503 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4504 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4505 tp->mac_version != RTL_GIGA_MAC_VER_04) {
4506 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4507 rtl_set_rx_tx_config_registers(tp);
4510 RTL_W8(Cfg9346, Cfg9346_Lock);
4512 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4515 RTL_W32(RxMissed, 0);
4517 rtl_set_rx_mode(dev);
4519 /* no early-rx interrupts */
4520 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4522 /* Enable all known interrupts by setting the interrupt mask. */
4523 RTL_W16(IntrMask, tp->intr_event);
4526 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
4530 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
4531 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4534 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4536 rtl_csi_access_enable(ioaddr, 0x17000000);
4539 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4541 rtl_csi_access_enable(ioaddr, 0x27000000);
4545 unsigned int offset;
4550 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
4555 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4556 rtl_ephy_write(ioaddr, e->offset, w);
4561 static void rtl_disable_clock_request(struct pci_dev *pdev)
4563 int cap = pci_pcie_cap(pdev);
4568 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4569 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4570 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4574 static void rtl_enable_clock_request(struct pci_dev *pdev)
4576 int cap = pci_pcie_cap(pdev);
4581 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4582 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4583 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4587 #define R8168_CPCMD_QUIRK_MASK (\
4598 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4600 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4602 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4604 rtl_tx_performance_tweak(pdev,
4605 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4608 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4610 rtl_hw_start_8168bb(ioaddr, pdev);
4612 RTL_W8(MaxTxPacketSize, TxPacketMax);
4614 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4617 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4619 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4621 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4623 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4625 rtl_disable_clock_request(pdev);
4627 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4630 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
4632 static const struct ephy_info e_info_8168cp[] = {
4633 { 0x01, 0, 0x0001 },
4634 { 0x02, 0x0800, 0x1000 },
4635 { 0x03, 0, 0x0042 },
4636 { 0x06, 0x0080, 0x0000 },
4640 rtl_csi_access_enable_2(ioaddr);
4642 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4644 __rtl_hw_start_8168cp(ioaddr, pdev);
4647 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4649 rtl_csi_access_enable_2(ioaddr);
4651 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4653 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4655 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4658 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4660 rtl_csi_access_enable_2(ioaddr);
4662 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4665 RTL_W8(DBG_REG, 0x20);
4667 RTL_W8(MaxTxPacketSize, TxPacketMax);
4669 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4671 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4674 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4676 static const struct ephy_info e_info_8168c_1[] = {
4677 { 0x02, 0x0800, 0x1000 },
4678 { 0x03, 0, 0x0002 },
4679 { 0x06, 0x0080, 0x0000 }
4682 rtl_csi_access_enable_2(ioaddr);
4684 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4686 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4688 __rtl_hw_start_8168cp(ioaddr, pdev);
4691 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4693 static const struct ephy_info e_info_8168c_2[] = {
4694 { 0x01, 0, 0x0001 },
4695 { 0x03, 0x0400, 0x0220 }
4698 rtl_csi_access_enable_2(ioaddr);
4700 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4702 __rtl_hw_start_8168cp(ioaddr, pdev);
4705 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4707 rtl_hw_start_8168c_2(ioaddr, pdev);
4710 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4712 rtl_csi_access_enable_2(ioaddr);
4714 __rtl_hw_start_8168cp(ioaddr, pdev);
4717 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4719 rtl_csi_access_enable_2(ioaddr);
4721 rtl_disable_clock_request(pdev);
4723 RTL_W8(MaxTxPacketSize, TxPacketMax);
4725 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4727 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4730 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4732 rtl_csi_access_enable_1(ioaddr);
4734 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4736 RTL_W8(MaxTxPacketSize, TxPacketMax);
4738 rtl_disable_clock_request(pdev);
4741 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4743 static const struct ephy_info e_info_8168d_4[] = {
4745 { 0x19, 0x20, 0x50 },
4750 rtl_csi_access_enable_1(ioaddr);
4752 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4754 RTL_W8(MaxTxPacketSize, TxPacketMax);
4756 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4757 const struct ephy_info *e = e_info_8168d_4 + i;
4760 w = rtl_ephy_read(ioaddr, e->offset);
4761 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4764 rtl_enable_clock_request(pdev);
4767 static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4769 static const struct ephy_info e_info_8168e_1[] = {
4770 { 0x00, 0x0200, 0x0100 },
4771 { 0x00, 0x0000, 0x0004 },
4772 { 0x06, 0x0002, 0x0001 },
4773 { 0x06, 0x0000, 0x0030 },
4774 { 0x07, 0x0000, 0x2000 },
4775 { 0x00, 0x0000, 0x0020 },
4776 { 0x03, 0x5800, 0x2000 },
4777 { 0x03, 0x0000, 0x0001 },
4778 { 0x01, 0x0800, 0x1000 },
4779 { 0x07, 0x0000, 0x4000 },
4780 { 0x1e, 0x0000, 0x2000 },
4781 { 0x19, 0xffff, 0xfe6c },
4782 { 0x0a, 0x0000, 0x0040 }
4785 rtl_csi_access_enable_2(ioaddr);
4787 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
4789 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4791 RTL_W8(MaxTxPacketSize, TxPacketMax);
4793 rtl_disable_clock_request(pdev);
4795 /* Reset tx FIFO pointer */
4796 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4797 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
4799 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4802 static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4804 static const struct ephy_info e_info_8168e_2[] = {
4805 { 0x09, 0x0000, 0x0080 },
4806 { 0x19, 0x0000, 0x0224 }
4809 rtl_csi_access_enable_1(ioaddr);
4811 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4813 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4815 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4816 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4817 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4818 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4819 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4820 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4821 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4822 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4825 RTL_W8(MaxTxPacketSize, EarlySize);
4827 rtl_disable_clock_request(pdev);
4829 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4830 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4832 /* Adjust EEE LED frequency */
4833 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4835 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4836 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4837 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4840 static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4842 static const struct ephy_info e_info_8168f_1[] = {
4843 { 0x06, 0x00c0, 0x0020 },
4844 { 0x08, 0x0001, 0x0002 },
4845 { 0x09, 0x0000, 0x0080 },
4846 { 0x19, 0x0000, 0x0224 }
4849 rtl_csi_access_enable_1(ioaddr);
4851 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4853 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4855 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4856 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4857 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4858 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4859 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4860 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4861 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4862 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4863 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4864 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, 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_8168(struct net_device *dev)
4885 struct rtl8169_private *tp = netdev_priv(dev);
4886 void __iomem *ioaddr = tp->mmio_addr;
4887 struct pci_dev *pdev = tp->pci_dev;
4889 RTL_W8(Cfg9346, Cfg9346_Unlock);
4891 RTL_W8(MaxTxPacketSize, TxPacketMax);
4893 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4895 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
4897 RTL_W16(CPlusCmd, tp->cp_cmd);
4899 RTL_W16(IntrMitigate, 0x5151);
4901 /* Work around for RxFIFO overflow. */
4902 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
4903 tp->intr_event |= RxFIFOOver | PCSTimeout;
4904 tp->intr_event &= ~RxOverflow;
4907 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4909 rtl_set_rx_mode(dev);
4911 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4912 (InterFrameGap << TxInterFrameGapShift));
4916 switch (tp->mac_version) {
4917 case RTL_GIGA_MAC_VER_11:
4918 rtl_hw_start_8168bb(ioaddr, pdev);
4921 case RTL_GIGA_MAC_VER_12:
4922 case RTL_GIGA_MAC_VER_17:
4923 rtl_hw_start_8168bef(ioaddr, pdev);
4926 case RTL_GIGA_MAC_VER_18:
4927 rtl_hw_start_8168cp_1(ioaddr, pdev);
4930 case RTL_GIGA_MAC_VER_19:
4931 rtl_hw_start_8168c_1(ioaddr, pdev);
4934 case RTL_GIGA_MAC_VER_20:
4935 rtl_hw_start_8168c_2(ioaddr, pdev);
4938 case RTL_GIGA_MAC_VER_21:
4939 rtl_hw_start_8168c_3(ioaddr, pdev);
4942 case RTL_GIGA_MAC_VER_22:
4943 rtl_hw_start_8168c_4(ioaddr, pdev);
4946 case RTL_GIGA_MAC_VER_23:
4947 rtl_hw_start_8168cp_2(ioaddr, pdev);
4950 case RTL_GIGA_MAC_VER_24:
4951 rtl_hw_start_8168cp_3(ioaddr, pdev);
4954 case RTL_GIGA_MAC_VER_25:
4955 case RTL_GIGA_MAC_VER_26:
4956 case RTL_GIGA_MAC_VER_27:
4957 rtl_hw_start_8168d(ioaddr, pdev);
4960 case RTL_GIGA_MAC_VER_28:
4961 rtl_hw_start_8168d_4(ioaddr, pdev);
4964 case RTL_GIGA_MAC_VER_31:
4965 rtl_hw_start_8168dp(ioaddr, pdev);
4968 case RTL_GIGA_MAC_VER_32:
4969 case RTL_GIGA_MAC_VER_33:
4970 rtl_hw_start_8168e_1(ioaddr, pdev);
4972 case RTL_GIGA_MAC_VER_34:
4973 rtl_hw_start_8168e_2(ioaddr, pdev);
4976 case RTL_GIGA_MAC_VER_35:
4977 case RTL_GIGA_MAC_VER_36:
4978 rtl_hw_start_8168f_1(ioaddr, pdev);
4982 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4983 dev->name, tp->mac_version);
4987 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4989 RTL_W8(Cfg9346, Cfg9346_Lock);
4991 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4993 RTL_W16(IntrMask, tp->intr_event);
4996 #define R810X_CPCMD_QUIRK_MASK (\
5007 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5009 static const struct ephy_info e_info_8102e_1[] = {
5010 { 0x01, 0, 0x6e65 },
5011 { 0x02, 0, 0x091f },
5012 { 0x03, 0, 0xc2f9 },
5013 { 0x06, 0, 0xafb5 },
5014 { 0x07, 0, 0x0e00 },
5015 { 0x19, 0, 0xec80 },
5016 { 0x01, 0, 0x2e65 },
5021 rtl_csi_access_enable_2(ioaddr);
5023 RTL_W8(DBG_REG, FIX_NAK_1);
5025 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5028 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5029 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5031 cfg1 = RTL_R8(Config1);
5032 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5033 RTL_W8(Config1, cfg1 & ~LEDS0);
5035 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5038 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5040 rtl_csi_access_enable_2(ioaddr);
5042 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5044 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5045 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5048 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5050 rtl_hw_start_8102e_2(ioaddr, pdev);
5052 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5055 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5057 static const struct ephy_info e_info_8105e_1[] = {
5058 { 0x07, 0, 0x4000 },
5059 { 0x19, 0, 0x0200 },
5060 { 0x19, 0, 0x0020 },
5061 { 0x1e, 0, 0x2000 },
5062 { 0x03, 0, 0x0001 },
5063 { 0x19, 0, 0x0100 },
5064 { 0x19, 0, 0x0004 },
5068 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5069 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5071 /* Disable Early Tally Counter */
5072 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5074 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5075 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5077 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5080 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5082 rtl_hw_start_8105e_1(ioaddr, pdev);
5083 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5086 static void rtl_hw_start_8101(struct net_device *dev)
5088 struct rtl8169_private *tp = netdev_priv(dev);
5089 void __iomem *ioaddr = tp->mmio_addr;
5090 struct pci_dev *pdev = tp->pci_dev;
5092 if (tp->mac_version >= RTL_GIGA_MAC_VER_30) {
5093 tp->intr_event &= ~RxFIFOOver;
5094 tp->napi_event &= ~RxFIFOOver;
5097 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5098 tp->mac_version == RTL_GIGA_MAC_VER_16) {
5099 int cap = pci_pcie_cap(pdev);
5102 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5103 PCI_EXP_DEVCTL_NOSNOOP_EN);
5107 RTL_W8(Cfg9346, Cfg9346_Unlock);
5109 switch (tp->mac_version) {
5110 case RTL_GIGA_MAC_VER_07:
5111 rtl_hw_start_8102e_1(ioaddr, pdev);
5114 case RTL_GIGA_MAC_VER_08:
5115 rtl_hw_start_8102e_3(ioaddr, pdev);
5118 case RTL_GIGA_MAC_VER_09:
5119 rtl_hw_start_8102e_2(ioaddr, pdev);
5122 case RTL_GIGA_MAC_VER_29:
5123 rtl_hw_start_8105e_1(ioaddr, pdev);
5125 case RTL_GIGA_MAC_VER_30:
5126 rtl_hw_start_8105e_2(ioaddr, pdev);
5130 RTL_W8(Cfg9346, Cfg9346_Lock);
5132 RTL_W8(MaxTxPacketSize, TxPacketMax);
5134 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5136 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5137 RTL_W16(CPlusCmd, tp->cp_cmd);
5139 RTL_W16(IntrMitigate, 0x0000);
5141 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5143 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5144 rtl_set_rx_tx_config_registers(tp);
5148 rtl_set_rx_mode(dev);
5150 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5152 RTL_W16(IntrMask, tp->intr_event);
5155 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5157 struct rtl8169_private *tp = netdev_priv(dev);
5159 if (new_mtu < ETH_ZLEN ||
5160 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
5163 if (new_mtu > ETH_DATA_LEN)
5164 rtl_hw_jumbo_enable(tp);
5166 rtl_hw_jumbo_disable(tp);
5169 netdev_update_features(dev);
5174 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5176 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5177 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5180 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5181 void **data_buff, struct RxDesc *desc)
5183 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
5188 rtl8169_make_unusable_by_asic(desc);
5191 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5193 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5195 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5198 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5201 desc->addr = cpu_to_le64(mapping);
5203 rtl8169_mark_to_asic(desc, rx_buf_sz);
5206 static inline void *rtl8169_align(void *data)
5208 return (void *)ALIGN((long)data, 16);
5211 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5212 struct RxDesc *desc)
5216 struct device *d = &tp->pci_dev->dev;
5217 struct net_device *dev = tp->dev;
5218 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
5220 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5224 if (rtl8169_align(data) != data) {
5226 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5231 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
5233 if (unlikely(dma_mapping_error(d, mapping))) {
5234 if (net_ratelimit())
5235 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5239 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
5247 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5251 for (i = 0; i < NUM_RX_DESC; i++) {
5252 if (tp->Rx_databuff[i]) {
5253 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5254 tp->RxDescArray + i);
5259 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5261 desc->opts1 |= cpu_to_le32(RingEnd);
5264 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5268 for (i = 0; i < NUM_RX_DESC; i++) {
5271 if (tp->Rx_databuff[i])
5274 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5276 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5279 tp->Rx_databuff[i] = data;
5282 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5286 rtl8169_rx_clear(tp);
5290 static int rtl8169_init_ring(struct net_device *dev)
5292 struct rtl8169_private *tp = netdev_priv(dev);
5294 rtl8169_init_ring_indexes(tp);
5296 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
5297 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
5299 return rtl8169_rx_fill(tp);
5302 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5303 struct TxDesc *desc)
5305 unsigned int len = tx_skb->len;
5307 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5315 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5320 for (i = 0; i < n; i++) {
5321 unsigned int entry = (start + i) % NUM_TX_DESC;
5322 struct ring_info *tx_skb = tp->tx_skb + entry;
5323 unsigned int len = tx_skb->len;
5326 struct sk_buff *skb = tx_skb->skb;
5328 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5329 tp->TxDescArray + entry);
5331 tp->dev->stats.tx_dropped++;
5339 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5341 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5342 tp->cur_tx = tp->dirty_tx = 0;
5345 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
5347 struct rtl8169_private *tp = netdev_priv(dev);
5349 PREPARE_DELAYED_WORK(&tp->task, task);
5350 schedule_delayed_work(&tp->task, 4);
5353 static void rtl8169_wait_for_quiescence(struct net_device *dev)
5355 struct rtl8169_private *tp = netdev_priv(dev);
5356 void __iomem *ioaddr = tp->mmio_addr;
5358 synchronize_irq(dev->irq);
5360 /* Wait for any pending NAPI task to complete */
5361 napi_disable(&tp->napi);
5363 rtl8169_irq_mask_and_ack(tp);
5365 tp->intr_mask = 0xffff;
5366 RTL_W16(IntrMask, tp->intr_event);
5367 napi_enable(&tp->napi);
5370 static void rtl8169_reinit_task(struct work_struct *work)
5372 struct rtl8169_private *tp =
5373 container_of(work, struct rtl8169_private, task.work);
5374 struct net_device *dev = tp->dev;
5379 if (!netif_running(dev))
5382 rtl8169_wait_for_quiescence(dev);
5385 ret = rtl8169_open(dev);
5386 if (unlikely(ret < 0)) {
5387 if (net_ratelimit())
5388 netif_err(tp, drv, dev,
5389 "reinit failure (status = %d). Rescheduling\n",
5391 rtl8169_schedule_work(dev, rtl8169_reinit_task);
5398 static void rtl8169_reset_task(struct work_struct *work)
5400 struct rtl8169_private *tp =
5401 container_of(work, struct rtl8169_private, task.work);
5402 struct net_device *dev = tp->dev;
5407 if (!netif_running(dev))
5410 rtl8169_hw_reset(tp);
5412 rtl8169_wait_for_quiescence(dev);
5414 for (i = 0; i < NUM_RX_DESC; i++)
5415 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5417 rtl8169_tx_clear(tp);
5418 rtl8169_init_ring_indexes(tp);
5421 netif_wake_queue(dev);
5422 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
5428 static void rtl8169_tx_timeout(struct net_device *dev)
5430 rtl8169_schedule_work(dev, rtl8169_reset_task);
5433 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5436 struct skb_shared_info *info = skb_shinfo(skb);
5437 unsigned int cur_frag, entry;
5438 struct TxDesc * uninitialized_var(txd);
5439 struct device *d = &tp->pci_dev->dev;
5442 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5443 const skb_frag_t *frag = info->frags + cur_frag;
5448 entry = (entry + 1) % NUM_TX_DESC;
5450 txd = tp->TxDescArray + entry;
5451 len = skb_frag_size(frag);
5452 addr = skb_frag_address(frag);
5453 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5454 if (unlikely(dma_mapping_error(d, mapping))) {
5455 if (net_ratelimit())
5456 netif_err(tp, drv, tp->dev,
5457 "Failed to map TX fragments DMA!\n");
5461 /* Anti gcc 2.95.3 bugware (sic) */
5462 status = opts[0] | len |
5463 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5465 txd->opts1 = cpu_to_le32(status);
5466 txd->opts2 = cpu_to_le32(opts[1]);
5467 txd->addr = cpu_to_le64(mapping);
5469 tp->tx_skb[entry].len = len;
5473 tp->tx_skb[entry].skb = skb;
5474 txd->opts1 |= cpu_to_le32(LastFrag);
5480 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5484 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5485 struct sk_buff *skb, u32 *opts)
5487 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
5488 u32 mss = skb_shinfo(skb)->gso_size;
5489 int offset = info->opts_offset;
5493 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5494 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5495 const struct iphdr *ip = ip_hdr(skb);
5497 if (ip->protocol == IPPROTO_TCP)
5498 opts[offset] |= info->checksum.tcp;
5499 else if (ip->protocol == IPPROTO_UDP)
5500 opts[offset] |= info->checksum.udp;
5506 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5507 struct net_device *dev)
5509 struct rtl8169_private *tp = netdev_priv(dev);
5510 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5511 struct TxDesc *txd = tp->TxDescArray + entry;
5512 void __iomem *ioaddr = tp->mmio_addr;
5513 struct device *d = &tp->pci_dev->dev;
5519 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
5520 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5524 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5527 len = skb_headlen(skb);
5528 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
5529 if (unlikely(dma_mapping_error(d, mapping))) {
5530 if (net_ratelimit())
5531 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
5535 tp->tx_skb[entry].len = len;
5536 txd->addr = cpu_to_le64(mapping);
5538 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5541 rtl8169_tso_csum(tp, skb, opts);
5543 frags = rtl8169_xmit_frags(tp, skb, opts);
5547 opts[0] |= FirstFrag;
5549 opts[0] |= FirstFrag | LastFrag;
5550 tp->tx_skb[entry].skb = skb;
5553 txd->opts2 = cpu_to_le32(opts[1]);
5557 /* Anti gcc 2.95.3 bugware (sic) */
5558 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
5559 txd->opts1 = cpu_to_le32(status);
5561 tp->cur_tx += frags + 1;
5565 RTL_W8(TxPoll, NPQ);
5567 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5568 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5569 * not miss a ring update when it notices a stopped queue.
5572 netif_stop_queue(dev);
5573 /* Sync with rtl_tx:
5574 * - publish queue status and cur_tx ring index (write barrier)
5575 * - refresh dirty_tx ring index (read barrier).
5576 * May the current thread have a pessimistic view of the ring
5577 * status and forget to wake up queue, a racing rtl_tx thread
5581 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
5582 netif_wake_queue(dev);
5585 return NETDEV_TX_OK;
5588 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
5591 dev->stats.tx_dropped++;
5592 return NETDEV_TX_OK;
5595 netif_stop_queue(dev);
5596 dev->stats.tx_dropped++;
5597 return NETDEV_TX_BUSY;
5600 static void rtl8169_pcierr_interrupt(struct net_device *dev)
5602 struct rtl8169_private *tp = netdev_priv(dev);
5603 struct pci_dev *pdev = tp->pci_dev;
5604 u16 pci_status, pci_cmd;
5606 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5607 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5609 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5610 pci_cmd, pci_status);
5613 * The recovery sequence below admits a very elaborated explanation:
5614 * - it seems to work;
5615 * - I did not see what else could be done;
5616 * - it makes iop3xx happy.
5618 * Feel free to adjust to your needs.
5620 if (pdev->broken_parity_status)
5621 pci_cmd &= ~PCI_COMMAND_PARITY;
5623 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5625 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
5627 pci_write_config_word(pdev, PCI_STATUS,
5628 pci_status & (PCI_STATUS_DETECTED_PARITY |
5629 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5630 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5632 /* The infamous DAC f*ckup only happens at boot time */
5633 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
5634 void __iomem *ioaddr = tp->mmio_addr;
5636 netif_info(tp, intr, dev, "disabling PCI DAC\n");
5637 tp->cp_cmd &= ~PCIDAC;
5638 RTL_W16(CPlusCmd, tp->cp_cmd);
5639 dev->features &= ~NETIF_F_HIGHDMA;
5642 rtl8169_hw_reset(tp);
5644 rtl8169_schedule_work(dev, rtl8169_reinit_task);
5647 static void rtl8169_tx_interrupt(struct net_device *dev,
5648 struct rtl8169_private *tp,
5649 void __iomem *ioaddr)
5651 unsigned int dirty_tx, tx_left;
5653 dirty_tx = tp->dirty_tx;
5655 tx_left = tp->cur_tx - dirty_tx;
5657 while (tx_left > 0) {
5658 unsigned int entry = dirty_tx % NUM_TX_DESC;
5659 struct ring_info *tx_skb = tp->tx_skb + entry;
5663 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5664 if (status & DescOwn)
5667 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5668 tp->TxDescArray + entry);
5669 if (status & LastFrag) {
5670 dev->stats.tx_packets++;
5671 dev->stats.tx_bytes += tx_skb->skb->len;
5672 dev_kfree_skb(tx_skb->skb);
5679 if (tp->dirty_tx != dirty_tx) {
5680 tp->dirty_tx = dirty_tx;
5681 /* Sync with rtl8169_start_xmit:
5682 * - publish dirty_tx ring index (write barrier)
5683 * - refresh cur_tx ring index and queue status (read barrier)
5684 * May the current thread miss the stopped queue condition,
5685 * a racing xmit thread can only have a right view of the
5689 if (netif_queue_stopped(dev) &&
5690 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5691 netif_wake_queue(dev);
5694 * 8168 hack: TxPoll requests are lost when the Tx packets are
5695 * too close. Let's kick an extra TxPoll request when a burst
5696 * of start_xmit activity is detected (if it is not detected,
5697 * it is slow enough). -- FR
5699 if (tp->cur_tx != dirty_tx)
5700 RTL_W8(TxPoll, NPQ);
5704 static inline int rtl8169_fragmented_frame(u32 status)
5706 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5709 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
5711 u32 status = opts1 & RxProtoMask;
5713 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
5714 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5715 skb->ip_summed = CHECKSUM_UNNECESSARY;
5717 skb_checksum_none_assert(skb);
5720 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5721 struct rtl8169_private *tp,
5725 struct sk_buff *skb;
5726 struct device *d = &tp->pci_dev->dev;
5728 data = rtl8169_align(data);
5729 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
5731 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5733 memcpy(skb->data, data, pkt_size);
5734 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5739 static int rtl8169_rx_interrupt(struct net_device *dev,
5740 struct rtl8169_private *tp,
5741 void __iomem *ioaddr, u32 budget)
5743 unsigned int cur_rx, rx_left;
5746 cur_rx = tp->cur_rx;
5747 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
5748 rx_left = min(rx_left, budget);
5750 for (; rx_left > 0; rx_left--, cur_rx++) {
5751 unsigned int entry = cur_rx % NUM_RX_DESC;
5752 struct RxDesc *desc = tp->RxDescArray + entry;
5756 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
5758 if (status & DescOwn)
5760 if (unlikely(status & RxRES)) {
5761 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5763 dev->stats.rx_errors++;
5764 if (status & (RxRWT | RxRUNT))
5765 dev->stats.rx_length_errors++;
5767 dev->stats.rx_crc_errors++;
5768 if (status & RxFOVF) {
5769 rtl8169_schedule_work(dev, rtl8169_reset_task);
5770 dev->stats.rx_fifo_errors++;
5772 rtl8169_mark_to_asic(desc, rx_buf_sz);
5774 struct sk_buff *skb;
5775 dma_addr_t addr = le64_to_cpu(desc->addr);
5776 int pkt_size = (status & 0x00003fff) - 4;
5779 * The driver does not support incoming fragmented
5780 * frames. They are seen as a symptom of over-mtu
5783 if (unlikely(rtl8169_fragmented_frame(status))) {
5784 dev->stats.rx_dropped++;
5785 dev->stats.rx_length_errors++;
5786 rtl8169_mark_to_asic(desc, rx_buf_sz);
5790 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5791 tp, pkt_size, addr);
5792 rtl8169_mark_to_asic(desc, rx_buf_sz);
5794 dev->stats.rx_dropped++;
5798 rtl8169_rx_csum(skb, status);
5799 skb_put(skb, pkt_size);
5800 skb->protocol = eth_type_trans(skb, dev);
5802 rtl8169_rx_vlan_tag(desc, skb);
5804 napi_gro_receive(&tp->napi, skb);
5806 dev->stats.rx_bytes += pkt_size;
5807 dev->stats.rx_packets++;
5810 /* Work around for AMD plateform. */
5811 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
5812 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5818 count = cur_rx - tp->cur_rx;
5819 tp->cur_rx = cur_rx;
5821 tp->dirty_rx += count;
5826 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5828 struct net_device *dev = dev_instance;
5829 struct rtl8169_private *tp = netdev_priv(dev);
5830 void __iomem *ioaddr = tp->mmio_addr;
5834 /* loop handling interrupts until we have no new ones or
5835 * we hit a invalid/hotplug case.
5837 status = RTL_R16(IntrStatus);
5838 while (status && status != 0xffff) {
5839 status &= tp->intr_event;
5845 /* Handle all of the error cases first. These will reset
5846 * the chip, so just exit the loop.
5848 if (unlikely(!netif_running(dev))) {
5849 rtl8169_hw_reset(tp);
5853 if (unlikely(status & RxFIFOOver)) {
5854 switch (tp->mac_version) {
5855 /* Work around for rx fifo overflow */
5856 case RTL_GIGA_MAC_VER_11:
5857 netif_stop_queue(dev);
5858 rtl8169_tx_timeout(dev);
5865 if (unlikely(status & SYSErr)) {
5866 rtl8169_pcierr_interrupt(dev);
5870 if (status & LinkChg)
5871 __rtl8169_check_link_status(dev, tp, ioaddr, true);
5873 /* We need to see the lastest version of tp->intr_mask to
5874 * avoid ignoring an MSI interrupt and having to wait for
5875 * another event which may never come.
5878 if (status & tp->intr_mask & tp->napi_event) {
5879 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5880 tp->intr_mask = ~tp->napi_event;
5882 if (likely(napi_schedule_prep(&tp->napi)))
5883 __napi_schedule(&tp->napi);
5885 netif_info(tp, intr, dev,
5886 "interrupt %04x in poll\n", status);
5889 /* We only get a new MSI interrupt when all active irq
5890 * sources on the chip have been acknowledged. So, ack
5891 * everything we've seen and check if new sources have become
5892 * active to avoid blocking all interrupts from the chip.
5895 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5896 status = RTL_R16(IntrStatus);
5899 return IRQ_RETVAL(handled);
5902 static int rtl8169_poll(struct napi_struct *napi, int budget)
5904 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5905 struct net_device *dev = tp->dev;
5906 void __iomem *ioaddr = tp->mmio_addr;
5909 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
5910 rtl8169_tx_interrupt(dev, tp, ioaddr);
5912 if (work_done < budget) {
5913 napi_complete(napi);
5915 /* We need for force the visibility of tp->intr_mask
5916 * for other CPUs, as we can loose an MSI interrupt
5917 * and potentially wait for a retransmit timeout if we don't.
5918 * The posted write to IntrMask is safe, as it will
5919 * eventually make it to the chip and we won't loose anything
5922 tp->intr_mask = 0xffff;
5924 RTL_W16(IntrMask, tp->intr_event);
5930 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5932 struct rtl8169_private *tp = netdev_priv(dev);
5934 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5937 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5938 RTL_W32(RxMissed, 0);
5941 static void rtl8169_down(struct net_device *dev)
5943 struct rtl8169_private *tp = netdev_priv(dev);
5944 void __iomem *ioaddr = tp->mmio_addr;
5946 del_timer_sync(&tp->timer);
5948 netif_stop_queue(dev);
5950 napi_disable(&tp->napi);
5952 spin_lock_irq(&tp->lock);
5954 rtl8169_hw_reset(tp);
5956 * At this point device interrupts can not be enabled in any function,
5957 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5958 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5960 rtl8169_rx_missed(dev, ioaddr);
5962 spin_unlock_irq(&tp->lock);
5964 synchronize_irq(dev->irq);
5966 /* Give a racing hard_start_xmit a few cycles to complete. */
5967 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
5969 rtl8169_tx_clear(tp);
5971 rtl8169_rx_clear(tp);
5973 rtl_pll_power_down(tp);
5976 static int rtl8169_close(struct net_device *dev)
5978 struct rtl8169_private *tp = netdev_priv(dev);
5979 struct pci_dev *pdev = tp->pci_dev;
5981 pm_runtime_get_sync(&pdev->dev);
5983 /* Update counters before going down */
5984 rtl8169_update_counters(dev);
5988 free_irq(dev->irq, dev);
5990 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5992 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5994 tp->TxDescArray = NULL;
5995 tp->RxDescArray = NULL;
5997 pm_runtime_put_sync(&pdev->dev);
6002 static void rtl_set_rx_mode(struct net_device *dev)
6004 struct rtl8169_private *tp = netdev_priv(dev);
6005 void __iomem *ioaddr = tp->mmio_addr;
6006 unsigned long flags;
6007 u32 mc_filter[2]; /* Multicast hash filter */
6011 if (dev->flags & IFF_PROMISC) {
6012 /* Unconditionally log net taps. */
6013 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
6015 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
6017 mc_filter[1] = mc_filter[0] = 0xffffffff;
6018 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
6019 (dev->flags & IFF_ALLMULTI)) {
6020 /* Too many to filter perfectly -- accept all multicasts. */
6021 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
6022 mc_filter[1] = mc_filter[0] = 0xffffffff;
6024 struct netdev_hw_addr *ha;
6026 rx_mode = AcceptBroadcast | AcceptMyPhys;
6027 mc_filter[1] = mc_filter[0] = 0;
6028 netdev_for_each_mc_addr(ha, dev) {
6029 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
6030 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6031 rx_mode |= AcceptMulticast;
6035 spin_lock_irqsave(&tp->lock, flags);
6037 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
6039 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
6040 u32 data = mc_filter[0];
6042 mc_filter[0] = swab32(mc_filter[1]);
6043 mc_filter[1] = swab32(data);
6046 RTL_W32(MAR0 + 4, mc_filter[1]);
6047 RTL_W32(MAR0 + 0, mc_filter[0]);
6049 RTL_W32(RxConfig, tmp);
6051 spin_unlock_irqrestore(&tp->lock, flags);
6055 * rtl8169_get_stats - Get rtl8169 read/write statistics
6056 * @dev: The Ethernet Device to get statistics for
6058 * Get TX/RX statistics for rtl8169
6060 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6062 struct rtl8169_private *tp = netdev_priv(dev);
6063 void __iomem *ioaddr = tp->mmio_addr;
6064 unsigned long flags;
6066 if (netif_running(dev)) {
6067 spin_lock_irqsave(&tp->lock, flags);
6068 rtl8169_rx_missed(dev, ioaddr);
6069 spin_unlock_irqrestore(&tp->lock, flags);
6075 static void rtl8169_net_suspend(struct net_device *dev)
6077 struct rtl8169_private *tp = netdev_priv(dev);
6079 if (!netif_running(dev))
6082 rtl_pll_power_down(tp);
6084 netif_device_detach(dev);
6085 netif_stop_queue(dev);
6090 static int rtl8169_suspend(struct device *device)
6092 struct pci_dev *pdev = to_pci_dev(device);
6093 struct net_device *dev = pci_get_drvdata(pdev);
6095 rtl8169_net_suspend(dev);
6100 static void __rtl8169_resume(struct net_device *dev)
6102 struct rtl8169_private *tp = netdev_priv(dev);
6104 netif_device_attach(dev);
6106 rtl_pll_power_up(tp);
6108 rtl8169_schedule_work(dev, rtl8169_reset_task);
6111 static int rtl8169_resume(struct device *device)
6113 struct pci_dev *pdev = to_pci_dev(device);
6114 struct net_device *dev = pci_get_drvdata(pdev);
6115 struct rtl8169_private *tp = netdev_priv(dev);
6117 rtl8169_init_phy(dev, tp);
6119 if (netif_running(dev))
6120 __rtl8169_resume(dev);
6125 static int rtl8169_runtime_suspend(struct device *device)
6127 struct pci_dev *pdev = to_pci_dev(device);
6128 struct net_device *dev = pci_get_drvdata(pdev);
6129 struct rtl8169_private *tp = netdev_priv(dev);
6131 if (!tp->TxDescArray)
6134 spin_lock_irq(&tp->lock);
6135 tp->saved_wolopts = __rtl8169_get_wol(tp);
6136 __rtl8169_set_wol(tp, WAKE_ANY);
6137 spin_unlock_irq(&tp->lock);
6139 rtl8169_net_suspend(dev);
6144 static int rtl8169_runtime_resume(struct device *device)
6146 struct pci_dev *pdev = to_pci_dev(device);
6147 struct net_device *dev = pci_get_drvdata(pdev);
6148 struct rtl8169_private *tp = netdev_priv(dev);
6150 if (!tp->TxDescArray)
6153 spin_lock_irq(&tp->lock);
6154 __rtl8169_set_wol(tp, tp->saved_wolopts);
6155 tp->saved_wolopts = 0;
6156 spin_unlock_irq(&tp->lock);
6158 rtl8169_init_phy(dev, tp);
6160 __rtl8169_resume(dev);
6165 static int rtl8169_runtime_idle(struct device *device)
6167 struct pci_dev *pdev = to_pci_dev(device);
6168 struct net_device *dev = pci_get_drvdata(pdev);
6169 struct rtl8169_private *tp = netdev_priv(dev);
6171 return tp->TxDescArray ? -EBUSY : 0;
6174 static const struct dev_pm_ops rtl8169_pm_ops = {
6175 .suspend = rtl8169_suspend,
6176 .resume = rtl8169_resume,
6177 .freeze = rtl8169_suspend,
6178 .thaw = rtl8169_resume,
6179 .poweroff = rtl8169_suspend,
6180 .restore = rtl8169_resume,
6181 .runtime_suspend = rtl8169_runtime_suspend,
6182 .runtime_resume = rtl8169_runtime_resume,
6183 .runtime_idle = rtl8169_runtime_idle,
6186 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6188 #else /* !CONFIG_PM */
6190 #define RTL8169_PM_OPS NULL
6192 #endif /* !CONFIG_PM */
6194 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6196 void __iomem *ioaddr = tp->mmio_addr;
6198 /* WoL fails with 8168b when the receiver is disabled. */
6199 switch (tp->mac_version) {
6200 case RTL_GIGA_MAC_VER_11:
6201 case RTL_GIGA_MAC_VER_12:
6202 case RTL_GIGA_MAC_VER_17:
6203 pci_clear_master(tp->pci_dev);
6205 RTL_W8(ChipCmd, CmdRxEnb);
6214 static void rtl_shutdown(struct pci_dev *pdev)
6216 struct net_device *dev = pci_get_drvdata(pdev);
6217 struct rtl8169_private *tp = netdev_priv(dev);
6218 struct device *d = &pdev->dev;
6220 pm_runtime_get_sync(d);
6222 rtl8169_net_suspend(dev);
6224 /* Restore original MAC address */
6225 rtl_rar_set(tp, dev->perm_addr);
6227 spin_lock_irq(&tp->lock);
6229 rtl8169_hw_reset(tp);
6231 spin_unlock_irq(&tp->lock);
6233 if (system_state == SYSTEM_POWER_OFF) {
6234 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6235 rtl_wol_suspend_quirk(tp);
6236 rtl_wol_shutdown_quirk(tp);
6239 pci_wake_from_d3(pdev, true);
6240 pci_set_power_state(pdev, PCI_D3hot);
6243 pm_runtime_put_noidle(d);
6246 static struct pci_driver rtl8169_pci_driver = {
6248 .id_table = rtl8169_pci_tbl,
6249 .probe = rtl8169_init_one,
6250 .remove = __devexit_p(rtl8169_remove_one),
6251 .shutdown = rtl_shutdown,
6252 .driver.pm = RTL8169_PM_OPS,
6255 static int __init rtl8169_init_module(void)
6257 return pci_register_driver(&rtl8169_pci_driver);
6260 static void __exit rtl8169_cleanup_module(void)
6262 pci_unregister_driver(&rtl8169_pci_driver);
6265 module_init(rtl8169_init_module);
6266 module_exit(rtl8169_cleanup_module);