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/dma-mapping.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/firmware.h>
28 #include <linux/pci-aspm.h>
29 #include <linux/prefetch.h>
31 #include <asm/system.h>
35 #define RTL8169_VERSION "2.3LK-NAPI"
36 #define MODULENAME "r8169"
37 #define PFX MODULENAME ": "
39 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
41 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
43 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
46 #define assert(expr) \
48 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
49 #expr,__FILE__,__func__,__LINE__); \
51 #define dprintk(fmt, args...) \
52 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
54 #define assert(expr) do {} while (0)
55 #define dprintk(fmt, args...) do {} while (0)
56 #endif /* RTL8169_DEBUG */
58 #define R8169_MSG_DEFAULT \
59 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
61 #define TX_BUFFS_AVAIL(tp) \
62 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
64 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
65 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
66 static const int multicast_filter_limit = 32;
68 /* MAC address length */
69 #define MAC_ADDR_LEN 6
71 #define MAX_READ_REQUEST_SHIFT 12
72 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
73 #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
74 #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
75 #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
76 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
78 #define R8169_REGS_SIZE 256
79 #define R8169_NAPI_WEIGHT 64
80 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
81 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
82 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
83 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
84 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
86 #define RTL8169_TX_TIMEOUT (6*HZ)
87 #define RTL8169_PHY_TIMEOUT (10*HZ)
89 #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
90 #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
91 #define RTL_EEPROM_SIG_ADDR 0x0000
93 /* write/read MMIO register */
94 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
95 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
96 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
97 #define RTL_R8(reg) readb (ioaddr + (reg))
98 #define RTL_R16(reg) readw (ioaddr + (reg))
99 #define RTL_R32(reg) readl (ioaddr + (reg))
102 RTL_GIGA_MAC_VER_01 = 0,
135 RTL_GIGA_MAC_NONE = 0xff,
138 enum rtl_tx_desc_version {
143 #define JUMBO_1K ETH_DATA_LEN
144 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
145 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
146 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
147 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
149 #define _R(NAME,TD,FW,SZ,B) { \
157 static const struct {
159 enum rtl_tx_desc_version txd_version;
163 } rtl_chip_infos[] = {
165 [RTL_GIGA_MAC_VER_01] =
166 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
167 [RTL_GIGA_MAC_VER_02] =
168 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
169 [RTL_GIGA_MAC_VER_03] =
170 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
171 [RTL_GIGA_MAC_VER_04] =
172 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
173 [RTL_GIGA_MAC_VER_05] =
174 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
175 [RTL_GIGA_MAC_VER_06] =
176 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
178 [RTL_GIGA_MAC_VER_07] =
179 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
180 [RTL_GIGA_MAC_VER_08] =
181 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
182 [RTL_GIGA_MAC_VER_09] =
183 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
184 [RTL_GIGA_MAC_VER_10] =
185 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
186 [RTL_GIGA_MAC_VER_11] =
187 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
188 [RTL_GIGA_MAC_VER_12] =
189 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
190 [RTL_GIGA_MAC_VER_13] =
191 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
192 [RTL_GIGA_MAC_VER_14] =
193 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
194 [RTL_GIGA_MAC_VER_15] =
195 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
196 [RTL_GIGA_MAC_VER_16] =
197 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
198 [RTL_GIGA_MAC_VER_17] =
199 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
200 [RTL_GIGA_MAC_VER_18] =
201 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
202 [RTL_GIGA_MAC_VER_19] =
203 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
204 [RTL_GIGA_MAC_VER_20] =
205 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
206 [RTL_GIGA_MAC_VER_21] =
207 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
208 [RTL_GIGA_MAC_VER_22] =
209 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
210 [RTL_GIGA_MAC_VER_23] =
211 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
212 [RTL_GIGA_MAC_VER_24] =
213 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
214 [RTL_GIGA_MAC_VER_25] =
215 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
217 [RTL_GIGA_MAC_VER_26] =
218 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
220 [RTL_GIGA_MAC_VER_27] =
221 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
222 [RTL_GIGA_MAC_VER_28] =
223 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
224 [RTL_GIGA_MAC_VER_29] =
225 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
227 [RTL_GIGA_MAC_VER_30] =
228 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
230 [RTL_GIGA_MAC_VER_31] =
231 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
232 [RTL_GIGA_MAC_VER_32] =
233 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
235 [RTL_GIGA_MAC_VER_33] =
236 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
247 static void rtl_hw_start_8169(struct net_device *);
248 static void rtl_hw_start_8168(struct net_device *);
249 static void rtl_hw_start_8101(struct net_device *);
251 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
252 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
253 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
254 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
255 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
256 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
257 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
258 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
259 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
260 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
261 { PCI_VENDOR_ID_LINKSYS, 0x1032,
262 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
264 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
268 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
270 static int rx_buf_sz = 16383;
277 MAC0 = 0, /* Ethernet hardware address. */
279 MAR0 = 8, /* Multicast filter. */
280 CounterAddrLow = 0x10,
281 CounterAddrHigh = 0x14,
282 TxDescStartAddrLow = 0x20,
283 TxDescStartAddrHigh = 0x24,
284 TxHDescStartAddrLow = 0x28,
285 TxHDescStartAddrHigh = 0x2c,
295 #define RTL_RX_CONFIG_MASK 0xff7e1880u
311 RxDescAddrLow = 0xe4,
312 RxDescAddrHigh = 0xe8,
313 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
315 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
317 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
319 #define TxPacketMax (8064 >> 7)
322 FuncEventMask = 0xf4,
323 FuncPresetState = 0xf8,
324 FuncForceEvent = 0xfc,
327 enum rtl8110_registers {
333 enum rtl8168_8101_registers {
336 #define CSIAR_FLAG 0x80000000
337 #define CSIAR_WRITE_CMD 0x80000000
338 #define CSIAR_BYTE_ENABLE 0x0f
339 #define CSIAR_BYTE_ENABLE_SHIFT 12
340 #define CSIAR_ADDR_MASK 0x0fff
343 #define EPHYAR_FLAG 0x80000000
344 #define EPHYAR_WRITE_CMD 0x80000000
345 #define EPHYAR_REG_MASK 0x1f
346 #define EPHYAR_REG_SHIFT 16
347 #define EPHYAR_DATA_MASK 0xffff
349 #define PM_SWITCH (1 << 6)
351 #define FIX_NAK_1 (1 << 4)
352 #define FIX_NAK_2 (1 << 3)
355 #define EN_NDP (1 << 3)
356 #define EN_OOB_RESET (1 << 2)
358 #define EFUSEAR_FLAG 0x80000000
359 #define EFUSEAR_WRITE_CMD 0x80000000
360 #define EFUSEAR_READ_CMD 0x00000000
361 #define EFUSEAR_REG_MASK 0x03ff
362 #define EFUSEAR_REG_SHIFT 8
363 #define EFUSEAR_DATA_MASK 0xff
366 enum rtl8168_registers {
369 #define ERIAR_FLAG 0x80000000
370 #define ERIAR_WRITE_CMD 0x80000000
371 #define ERIAR_READ_CMD 0x00000000
372 #define ERIAR_ADDR_BYTE_ALIGN 4
373 #define ERIAR_EXGMAC 0
376 #define ERIAR_TYPE_SHIFT 16
377 #define ERIAR_BYTEEN 0x0f
378 #define ERIAR_BYTEEN_SHIFT 12
379 EPHY_RXER_NUM = 0x7c,
380 OCPDR = 0xb0, /* OCP GPHY access */
381 #define OCPDR_WRITE_CMD 0x80000000
382 #define OCPDR_READ_CMD 0x00000000
383 #define OCPDR_REG_MASK 0x7f
384 #define OCPDR_GPHY_REG_SHIFT 16
385 #define OCPDR_DATA_MASK 0xffff
387 #define OCPAR_FLAG 0x80000000
388 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
389 #define OCPAR_GPHY_READ_CMD 0x0000f060
390 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
391 MISC = 0xf0, /* 8168e only. */
392 #define TXPLA_RST (1 << 29)
395 enum rtl_register_content {
396 /* InterruptStatusBits */
400 TxDescUnavail = 0x0080,
423 /* TXPoll register p.5 */
424 HPQ = 0x80, /* Poll cmd on the high prio queue */
425 NPQ = 0x40, /* Poll cmd on the low prio queue */
426 FSWInt = 0x01, /* Forced software interrupt */
430 Cfg9346_Unlock = 0xc0,
435 AcceptBroadcast = 0x08,
436 AcceptMulticast = 0x04,
438 AcceptAllPhys = 0x01,
445 TxInterFrameGapShift = 24,
446 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
448 /* Config1 register p.24 */
451 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
452 Speed_down = (1 << 4),
456 PMEnable = (1 << 0), /* Power Management Enable */
458 /* Config2 register p. 25 */
459 PCI_Clock_66MHz = 0x01,
460 PCI_Clock_33MHz = 0x00,
462 /* Config3 register p.25 */
463 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
464 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
465 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
466 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
468 /* Config4 register */
469 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
471 /* Config5 register p.27 */
472 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
473 MWF = (1 << 5), /* Accept Multicast wakeup frame */
474 UWF = (1 << 4), /* Accept Unicast wakeup frame */
476 LanWake = (1 << 1), /* LanWake enable/disable */
477 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
480 TBIReset = 0x80000000,
481 TBILoopback = 0x40000000,
482 TBINwEnable = 0x20000000,
483 TBINwRestart = 0x10000000,
484 TBILinkOk = 0x02000000,
485 TBINwComplete = 0x01000000,
488 EnableBist = (1 << 15), // 8168 8101
489 Mac_dbgo_oe = (1 << 14), // 8168 8101
490 Normal_mode = (1 << 13), // unused
491 Force_half_dup = (1 << 12), // 8168 8101
492 Force_rxflow_en = (1 << 11), // 8168 8101
493 Force_txflow_en = (1 << 10), // 8168 8101
494 Cxpl_dbg_sel = (1 << 9), // 8168 8101
495 ASF = (1 << 8), // 8168 8101
496 PktCntrDisable = (1 << 7), // 8168 8101
497 Mac_dbgo_sel = 0x001c, // 8168
502 INTT_0 = 0x0000, // 8168
503 INTT_1 = 0x0001, // 8168
504 INTT_2 = 0x0002, // 8168
505 INTT_3 = 0x0003, // 8168
507 /* rtl8169_PHYstatus */
518 TBILinkOK = 0x02000000,
520 /* DumpCounterCommand */
525 /* First doubleword. */
526 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
527 RingEnd = (1 << 30), /* End of descriptor ring */
528 FirstFrag = (1 << 29), /* First segment of a packet */
529 LastFrag = (1 << 28), /* Final segment of a packet */
533 enum rtl_tx_desc_bit {
534 /* First doubleword. */
535 TD_LSO = (1 << 27), /* Large Send Offload */
536 #define TD_MSS_MAX 0x07ffu /* MSS value */
538 /* Second doubleword. */
539 TxVlanTag = (1 << 17), /* Add VLAN tag */
542 /* 8169, 8168b and 810x except 8102e. */
543 enum rtl_tx_desc_bit_0 {
544 /* First doubleword. */
545 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
546 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
547 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
548 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
551 /* 8102e, 8168c and beyond. */
552 enum rtl_tx_desc_bit_1 {
553 /* Second doubleword. */
554 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
555 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
556 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
557 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
560 static const struct rtl_tx_desc_info {
567 } tx_desc_info [] = {
570 .udp = TD0_IP_CS | TD0_UDP_CS,
571 .tcp = TD0_IP_CS | TD0_TCP_CS
573 .mss_shift = TD0_MSS_SHIFT,
578 .udp = TD1_IP_CS | TD1_UDP_CS,
579 .tcp = TD1_IP_CS | TD1_TCP_CS
581 .mss_shift = TD1_MSS_SHIFT,
586 enum rtl_rx_desc_bit {
588 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
589 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
591 #define RxProtoUDP (PID1)
592 #define RxProtoTCP (PID0)
593 #define RxProtoIP (PID1 | PID0)
594 #define RxProtoMask RxProtoIP
596 IPFail = (1 << 16), /* IP checksum failed */
597 UDPFail = (1 << 15), /* UDP/IP checksum failed */
598 TCPFail = (1 << 14), /* TCP/IP checksum failed */
599 RxVlanTag = (1 << 16), /* VLAN tag available */
602 #define RsvdMask 0x3fffc000
619 u8 __pad[sizeof(void *) - sizeof(u32)];
623 RTL_FEATURE_WOL = (1 << 0),
624 RTL_FEATURE_MSI = (1 << 1),
625 RTL_FEATURE_GMII = (1 << 2),
628 struct rtl8169_counters {
635 __le32 tx_one_collision;
636 __le32 tx_multi_collision;
644 struct rtl8169_private {
645 void __iomem *mmio_addr; /* memory map physical address */
646 struct pci_dev *pci_dev;
647 struct net_device *dev;
648 struct napi_struct napi;
653 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
654 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
657 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
658 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
659 dma_addr_t TxPhyAddr;
660 dma_addr_t RxPhyAddr;
661 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
662 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
663 struct timer_list timer;
670 void (*write)(void __iomem *, int, int);
671 int (*read)(void __iomem *, int);
674 struct pll_power_ops {
675 void (*down)(struct rtl8169_private *);
676 void (*up)(struct rtl8169_private *);
680 void (*enable)(struct rtl8169_private *);
681 void (*disable)(struct rtl8169_private *);
684 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
685 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
686 void (*phy_reset_enable)(struct rtl8169_private *tp);
687 void (*hw_start)(struct net_device *);
688 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
689 unsigned int (*link_ok)(void __iomem *);
690 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
692 struct delayed_work task;
695 struct mii_if_info mii;
696 struct rtl8169_counters counters;
700 const struct firmware *fw;
701 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
704 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
705 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
706 module_param(use_dac, int, 0);
707 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
708 module_param_named(debug, debug.msg_enable, int, 0);
709 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
710 MODULE_LICENSE("GPL");
711 MODULE_VERSION(RTL8169_VERSION);
712 MODULE_FIRMWARE(FIRMWARE_8168D_1);
713 MODULE_FIRMWARE(FIRMWARE_8168D_2);
714 MODULE_FIRMWARE(FIRMWARE_8168E_1);
715 MODULE_FIRMWARE(FIRMWARE_8168E_2);
716 MODULE_FIRMWARE(FIRMWARE_8105E_1);
718 static int rtl8169_open(struct net_device *dev);
719 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
720 struct net_device *dev);
721 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
722 static int rtl8169_init_ring(struct net_device *dev);
723 static void rtl_hw_start(struct net_device *dev);
724 static int rtl8169_close(struct net_device *dev);
725 static void rtl_set_rx_mode(struct net_device *dev);
726 static void rtl8169_tx_timeout(struct net_device *dev);
727 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
728 static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
729 void __iomem *, u32 budget);
730 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
731 static void rtl8169_down(struct net_device *dev);
732 static void rtl8169_rx_clear(struct rtl8169_private *tp);
733 static int rtl8169_poll(struct napi_struct *napi, int budget);
735 static const unsigned int rtl8169_rx_config =
736 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
738 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
740 struct net_device *dev = pci_get_drvdata(pdev);
741 struct rtl8169_private *tp = netdev_priv(dev);
742 int cap = tp->pcie_cap;
747 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
748 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
749 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
753 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
755 void __iomem *ioaddr = tp->mmio_addr;
758 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
759 for (i = 0; i < 20; i++) {
761 if (RTL_R32(OCPAR) & OCPAR_FLAG)
764 return RTL_R32(OCPDR);
767 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
769 void __iomem *ioaddr = tp->mmio_addr;
772 RTL_W32(OCPDR, data);
773 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
774 for (i = 0; i < 20; i++) {
776 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
781 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
783 void __iomem *ioaddr = tp->mmio_addr;
787 RTL_W32(ERIAR, 0x800010e8);
789 for (i = 0; i < 5; i++) {
791 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
795 ocp_write(tp, 0x1, 0x30, 0x00000001);
798 #define OOB_CMD_RESET 0x00
799 #define OOB_CMD_DRIVER_START 0x05
800 #define OOB_CMD_DRIVER_STOP 0x06
802 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
804 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
807 static void rtl8168_driver_start(struct rtl8169_private *tp)
812 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
814 reg = rtl8168_get_ocp_reg(tp);
816 for (i = 0; i < 10; i++) {
818 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
823 static void rtl8168_driver_stop(struct rtl8169_private *tp)
828 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
830 reg = rtl8168_get_ocp_reg(tp);
832 for (i = 0; i < 10; i++) {
834 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
839 static int r8168dp_check_dash(struct rtl8169_private *tp)
841 u16 reg = rtl8168_get_ocp_reg(tp);
843 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
846 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
850 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
852 for (i = 20; i > 0; i--) {
854 * Check if the RTL8169 has completed writing to the specified
857 if (!(RTL_R32(PHYAR) & 0x80000000))
862 * According to hardware specs a 20us delay is required after write
863 * complete indication, but before sending next command.
868 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
872 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
874 for (i = 20; i > 0; i--) {
876 * Check if the RTL8169 has completed retrieving data from
877 * the specified MII register.
879 if (RTL_R32(PHYAR) & 0x80000000) {
880 value = RTL_R32(PHYAR) & 0xffff;
886 * According to hardware specs a 20us delay is required after read
887 * complete indication, but before sending next command.
894 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
898 RTL_W32(OCPDR, data |
899 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
900 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
901 RTL_W32(EPHY_RXER_NUM, 0);
903 for (i = 0; i < 100; i++) {
905 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
910 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
912 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
913 (value & OCPDR_DATA_MASK));
916 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
920 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
923 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
924 RTL_W32(EPHY_RXER_NUM, 0);
926 for (i = 0; i < 100; i++) {
928 if (RTL_R32(OCPAR) & OCPAR_FLAG)
932 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
935 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
937 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
939 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
942 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
944 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
947 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
949 r8168dp_2_mdio_start(ioaddr);
951 r8169_mdio_write(ioaddr, reg_addr, value);
953 r8168dp_2_mdio_stop(ioaddr);
956 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
960 r8168dp_2_mdio_start(ioaddr);
962 value = r8169_mdio_read(ioaddr, reg_addr);
964 r8168dp_2_mdio_stop(ioaddr);
969 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
971 tp->mdio_ops.write(tp->mmio_addr, location, val);
974 static int rtl_readphy(struct rtl8169_private *tp, int location)
976 return tp->mdio_ops.read(tp->mmio_addr, location);
979 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
981 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
984 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
988 val = rtl_readphy(tp, reg_addr);
989 rtl_writephy(tp, reg_addr, (val | p) & ~m);
992 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
995 struct rtl8169_private *tp = netdev_priv(dev);
997 rtl_writephy(tp, location, val);
1000 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1002 struct rtl8169_private *tp = netdev_priv(dev);
1004 return rtl_readphy(tp, location);
1007 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1011 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1012 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1014 for (i = 0; i < 100; i++) {
1015 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1021 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1026 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1028 for (i = 0; i < 100; i++) {
1029 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1030 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1039 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1043 RTL_W32(CSIDR, value);
1044 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1045 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1047 for (i = 0; i < 100; i++) {
1048 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1054 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1059 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1060 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1062 for (i = 0; i < 100; i++) {
1063 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1064 value = RTL_R32(CSIDR);
1073 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1078 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1080 for (i = 0; i < 300; i++) {
1081 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1082 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1091 static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1093 RTL_W16(IntrMask, 0x0000);
1095 RTL_W16(IntrStatus, 0xffff);
1098 static void rtl8169_asic_down(void __iomem *ioaddr)
1100 RTL_W8(ChipCmd, 0x00);
1101 rtl8169_irq_mask_and_ack(ioaddr);
1105 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1107 void __iomem *ioaddr = tp->mmio_addr;
1109 return RTL_R32(TBICSR) & TBIReset;
1112 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1114 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1117 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1119 return RTL_R32(TBICSR) & TBILinkOk;
1122 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1124 return RTL_R8(PHYstatus) & LinkStatus;
1127 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1129 void __iomem *ioaddr = tp->mmio_addr;
1131 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1134 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1138 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1139 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1142 static void __rtl8169_check_link_status(struct net_device *dev,
1143 struct rtl8169_private *tp,
1144 void __iomem *ioaddr, bool pm)
1146 unsigned long flags;
1148 spin_lock_irqsave(&tp->lock, flags);
1149 if (tp->link_ok(ioaddr)) {
1150 /* This is to cancel a scheduled suspend if there's one. */
1152 pm_request_resume(&tp->pci_dev->dev);
1153 netif_carrier_on(dev);
1154 if (net_ratelimit())
1155 netif_info(tp, ifup, dev, "link up\n");
1157 netif_carrier_off(dev);
1158 netif_info(tp, ifdown, dev, "link down\n");
1160 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1162 spin_unlock_irqrestore(&tp->lock, flags);
1165 static void rtl8169_check_link_status(struct net_device *dev,
1166 struct rtl8169_private *tp,
1167 void __iomem *ioaddr)
1169 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1172 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1174 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1176 void __iomem *ioaddr = tp->mmio_addr;
1180 options = RTL_R8(Config1);
1181 if (!(options & PMEnable))
1184 options = RTL_R8(Config3);
1185 if (options & LinkUp)
1186 wolopts |= WAKE_PHY;
1187 if (options & MagicPacket)
1188 wolopts |= WAKE_MAGIC;
1190 options = RTL_R8(Config5);
1192 wolopts |= WAKE_UCAST;
1194 wolopts |= WAKE_BCAST;
1196 wolopts |= WAKE_MCAST;
1201 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1203 struct rtl8169_private *tp = netdev_priv(dev);
1205 spin_lock_irq(&tp->lock);
1207 wol->supported = WAKE_ANY;
1208 wol->wolopts = __rtl8169_get_wol(tp);
1210 spin_unlock_irq(&tp->lock);
1213 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1215 void __iomem *ioaddr = tp->mmio_addr;
1217 static const struct {
1222 { WAKE_ANY, Config1, PMEnable },
1223 { WAKE_PHY, Config3, LinkUp },
1224 { WAKE_MAGIC, Config3, MagicPacket },
1225 { WAKE_UCAST, Config5, UWF },
1226 { WAKE_BCAST, Config5, BWF },
1227 { WAKE_MCAST, Config5, MWF },
1228 { WAKE_ANY, Config5, LanWake }
1231 RTL_W8(Cfg9346, Cfg9346_Unlock);
1233 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1234 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1235 if (wolopts & cfg[i].opt)
1236 options |= cfg[i].mask;
1237 RTL_W8(cfg[i].reg, options);
1240 RTL_W8(Cfg9346, Cfg9346_Lock);
1243 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1245 struct rtl8169_private *tp = netdev_priv(dev);
1247 spin_lock_irq(&tp->lock);
1250 tp->features |= RTL_FEATURE_WOL;
1252 tp->features &= ~RTL_FEATURE_WOL;
1253 __rtl8169_set_wol(tp, wol->wolopts);
1254 spin_unlock_irq(&tp->lock);
1256 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1261 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1263 return rtl_chip_infos[tp->mac_version].fw_name;
1266 static void rtl8169_get_drvinfo(struct net_device *dev,
1267 struct ethtool_drvinfo *info)
1269 struct rtl8169_private *tp = netdev_priv(dev);
1271 strcpy(info->driver, MODULENAME);
1272 strcpy(info->version, RTL8169_VERSION);
1273 strcpy(info->bus_info, pci_name(tp->pci_dev));
1274 strncpy(info->fw_version, IS_ERR_OR_NULL(tp->fw) ? "N/A" :
1275 rtl_lookup_firmware_name(tp), sizeof(info->fw_version) - 1);
1278 static int rtl8169_get_regs_len(struct net_device *dev)
1280 return R8169_REGS_SIZE;
1283 static int rtl8169_set_speed_tbi(struct net_device *dev,
1284 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1286 struct rtl8169_private *tp = netdev_priv(dev);
1287 void __iomem *ioaddr = tp->mmio_addr;
1291 reg = RTL_R32(TBICSR);
1292 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1293 (duplex == DUPLEX_FULL)) {
1294 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1295 } else if (autoneg == AUTONEG_ENABLE)
1296 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1298 netif_warn(tp, link, dev,
1299 "incorrect speed setting refused in TBI mode\n");
1306 static int rtl8169_set_speed_xmii(struct net_device *dev,
1307 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1309 struct rtl8169_private *tp = netdev_priv(dev);
1310 int giga_ctrl, bmcr;
1313 rtl_writephy(tp, 0x1f, 0x0000);
1315 if (autoneg == AUTONEG_ENABLE) {
1318 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1319 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1320 ADVERTISE_100HALF | ADVERTISE_100FULL);
1322 if (adv & ADVERTISED_10baseT_Half)
1323 auto_nego |= ADVERTISE_10HALF;
1324 if (adv & ADVERTISED_10baseT_Full)
1325 auto_nego |= ADVERTISE_10FULL;
1326 if (adv & ADVERTISED_100baseT_Half)
1327 auto_nego |= ADVERTISE_100HALF;
1328 if (adv & ADVERTISED_100baseT_Full)
1329 auto_nego |= ADVERTISE_100FULL;
1331 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1333 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1334 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1336 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1337 if (tp->mii.supports_gmii) {
1338 if (adv & ADVERTISED_1000baseT_Half)
1339 giga_ctrl |= ADVERTISE_1000HALF;
1340 if (adv & ADVERTISED_1000baseT_Full)
1341 giga_ctrl |= ADVERTISE_1000FULL;
1342 } else if (adv & (ADVERTISED_1000baseT_Half |
1343 ADVERTISED_1000baseT_Full)) {
1344 netif_info(tp, link, dev,
1345 "PHY does not support 1000Mbps\n");
1349 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1351 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1352 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1356 if (speed == SPEED_10)
1358 else if (speed == SPEED_100)
1359 bmcr = BMCR_SPEED100;
1363 if (duplex == DUPLEX_FULL)
1364 bmcr |= BMCR_FULLDPLX;
1367 rtl_writephy(tp, MII_BMCR, bmcr);
1369 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1370 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1371 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1372 rtl_writephy(tp, 0x17, 0x2138);
1373 rtl_writephy(tp, 0x0e, 0x0260);
1375 rtl_writephy(tp, 0x17, 0x2108);
1376 rtl_writephy(tp, 0x0e, 0x0000);
1385 static int rtl8169_set_speed(struct net_device *dev,
1386 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1388 struct rtl8169_private *tp = netdev_priv(dev);
1391 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1395 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1396 (advertising & ADVERTISED_1000baseT_Full)) {
1397 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1403 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1405 struct rtl8169_private *tp = netdev_priv(dev);
1406 unsigned long flags;
1409 del_timer_sync(&tp->timer);
1411 spin_lock_irqsave(&tp->lock, flags);
1412 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1413 cmd->duplex, cmd->advertising);
1414 spin_unlock_irqrestore(&tp->lock, flags);
1419 static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
1421 struct rtl8169_private *tp = netdev_priv(dev);
1423 if (dev->mtu > TD_MSS_MAX)
1424 features &= ~NETIF_F_ALL_TSO;
1426 if (dev->mtu > JUMBO_1K &&
1427 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1428 features &= ~NETIF_F_IP_CSUM;
1433 static int rtl8169_set_features(struct net_device *dev, u32 features)
1435 struct rtl8169_private *tp = netdev_priv(dev);
1436 void __iomem *ioaddr = tp->mmio_addr;
1437 unsigned long flags;
1439 spin_lock_irqsave(&tp->lock, flags);
1441 if (features & NETIF_F_RXCSUM)
1442 tp->cp_cmd |= RxChkSum;
1444 tp->cp_cmd &= ~RxChkSum;
1446 if (dev->features & NETIF_F_HW_VLAN_RX)
1447 tp->cp_cmd |= RxVlan;
1449 tp->cp_cmd &= ~RxVlan;
1451 RTL_W16(CPlusCmd, tp->cp_cmd);
1454 spin_unlock_irqrestore(&tp->lock, flags);
1459 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1460 struct sk_buff *skb)
1462 return (vlan_tx_tag_present(skb)) ?
1463 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1466 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1468 u32 opts2 = le32_to_cpu(desc->opts2);
1470 if (opts2 & RxVlanTag)
1471 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1476 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1478 struct rtl8169_private *tp = netdev_priv(dev);
1479 void __iomem *ioaddr = tp->mmio_addr;
1483 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1484 cmd->port = PORT_FIBRE;
1485 cmd->transceiver = XCVR_INTERNAL;
1487 status = RTL_R32(TBICSR);
1488 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1489 cmd->autoneg = !!(status & TBINwEnable);
1491 ethtool_cmd_speed_set(cmd, SPEED_1000);
1492 cmd->duplex = DUPLEX_FULL; /* Always set */
1497 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1499 struct rtl8169_private *tp = netdev_priv(dev);
1501 return mii_ethtool_gset(&tp->mii, cmd);
1504 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1506 struct rtl8169_private *tp = netdev_priv(dev);
1507 unsigned long flags;
1510 spin_lock_irqsave(&tp->lock, flags);
1512 rc = tp->get_settings(dev, cmd);
1514 spin_unlock_irqrestore(&tp->lock, flags);
1518 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1521 struct rtl8169_private *tp = netdev_priv(dev);
1522 unsigned long flags;
1524 if (regs->len > R8169_REGS_SIZE)
1525 regs->len = R8169_REGS_SIZE;
1527 spin_lock_irqsave(&tp->lock, flags);
1528 memcpy_fromio(p, tp->mmio_addr, regs->len);
1529 spin_unlock_irqrestore(&tp->lock, flags);
1532 static u32 rtl8169_get_msglevel(struct net_device *dev)
1534 struct rtl8169_private *tp = netdev_priv(dev);
1536 return tp->msg_enable;
1539 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1541 struct rtl8169_private *tp = netdev_priv(dev);
1543 tp->msg_enable = value;
1546 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1553 "tx_single_collisions",
1554 "tx_multi_collisions",
1562 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1566 return ARRAY_SIZE(rtl8169_gstrings);
1572 static void rtl8169_update_counters(struct net_device *dev)
1574 struct rtl8169_private *tp = netdev_priv(dev);
1575 void __iomem *ioaddr = tp->mmio_addr;
1576 struct device *d = &tp->pci_dev->dev;
1577 struct rtl8169_counters *counters;
1583 * Some chips are unable to dump tally counters when the receiver
1586 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1589 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1593 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1594 cmd = (u64)paddr & DMA_BIT_MASK(32);
1595 RTL_W32(CounterAddrLow, cmd);
1596 RTL_W32(CounterAddrLow, cmd | CounterDump);
1599 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1600 memcpy(&tp->counters, counters, sizeof(*counters));
1606 RTL_W32(CounterAddrLow, 0);
1607 RTL_W32(CounterAddrHigh, 0);
1609 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1612 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1613 struct ethtool_stats *stats, u64 *data)
1615 struct rtl8169_private *tp = netdev_priv(dev);
1619 rtl8169_update_counters(dev);
1621 data[0] = le64_to_cpu(tp->counters.tx_packets);
1622 data[1] = le64_to_cpu(tp->counters.rx_packets);
1623 data[2] = le64_to_cpu(tp->counters.tx_errors);
1624 data[3] = le32_to_cpu(tp->counters.rx_errors);
1625 data[4] = le16_to_cpu(tp->counters.rx_missed);
1626 data[5] = le16_to_cpu(tp->counters.align_errors);
1627 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1628 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1629 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1630 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1631 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1632 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1633 data[12] = le16_to_cpu(tp->counters.tx_underun);
1636 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1640 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1645 static const struct ethtool_ops rtl8169_ethtool_ops = {
1646 .get_drvinfo = rtl8169_get_drvinfo,
1647 .get_regs_len = rtl8169_get_regs_len,
1648 .get_link = ethtool_op_get_link,
1649 .get_settings = rtl8169_get_settings,
1650 .set_settings = rtl8169_set_settings,
1651 .get_msglevel = rtl8169_get_msglevel,
1652 .set_msglevel = rtl8169_set_msglevel,
1653 .get_regs = rtl8169_get_regs,
1654 .get_wol = rtl8169_get_wol,
1655 .set_wol = rtl8169_set_wol,
1656 .get_strings = rtl8169_get_strings,
1657 .get_sset_count = rtl8169_get_sset_count,
1658 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1661 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1662 struct net_device *dev, u8 default_version)
1664 void __iomem *ioaddr = tp->mmio_addr;
1666 * The driver currently handles the 8168Bf and the 8168Be identically
1667 * but they can be identified more specifically through the test below
1670 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1672 * Same thing for the 8101Eb and the 8101Ec:
1674 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1676 static const struct rtl_mac_info {
1682 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1683 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1684 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1687 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1688 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1689 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1691 /* 8168DP family. */
1692 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1693 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1694 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
1697 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1698 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1699 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1700 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1701 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1702 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1703 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1704 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1705 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1708 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1709 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1710 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1711 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1714 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
1715 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1716 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1717 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1718 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1719 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1720 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1721 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1722 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1723 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1724 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1725 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1726 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1727 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1728 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1729 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1730 /* FIXME: where did these entries come from ? -- FR */
1731 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1732 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1735 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1736 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1737 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1738 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1739 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1740 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1743 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1745 const struct rtl_mac_info *p = mac_info;
1748 reg = RTL_R32(TxConfig);
1749 while ((reg & p->mask) != p->val)
1751 tp->mac_version = p->mac_version;
1753 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1754 netif_notice(tp, probe, dev,
1755 "unknown MAC, using family default\n");
1756 tp->mac_version = default_version;
1760 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1762 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1770 static void rtl_writephy_batch(struct rtl8169_private *tp,
1771 const struct phy_reg *regs, int len)
1774 rtl_writephy(tp, regs->reg, regs->val);
1779 #define PHY_READ 0x00000000
1780 #define PHY_DATA_OR 0x10000000
1781 #define PHY_DATA_AND 0x20000000
1782 #define PHY_BJMPN 0x30000000
1783 #define PHY_READ_EFUSE 0x40000000
1784 #define PHY_READ_MAC_BYTE 0x50000000
1785 #define PHY_WRITE_MAC_BYTE 0x60000000
1786 #define PHY_CLEAR_READCOUNT 0x70000000
1787 #define PHY_WRITE 0x80000000
1788 #define PHY_READCOUNT_EQ_SKIP 0x90000000
1789 #define PHY_COMP_EQ_SKIPN 0xa0000000
1790 #define PHY_COMP_NEQ_SKIPN 0xb0000000
1791 #define PHY_WRITE_PREVIOUS 0xc0000000
1792 #define PHY_SKIPN 0xd0000000
1793 #define PHY_DELAY_MS 0xe0000000
1794 #define PHY_WRITE_ERI_WORD 0xf0000000
1797 rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1799 __le32 *phytable = (__le32 *)fw->data;
1800 struct net_device *dev = tp->dev;
1801 size_t index, fw_size = fw->size / sizeof(*phytable);
1804 if (fw->size % sizeof(*phytable)) {
1805 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1809 for (index = 0; index < fw_size; index++) {
1810 u32 action = le32_to_cpu(phytable[index]);
1811 u32 regno = (action & 0x0fff0000) >> 16;
1813 switch(action & 0xf0000000) {
1817 case PHY_READ_EFUSE:
1818 case PHY_CLEAR_READCOUNT:
1820 case PHY_WRITE_PREVIOUS:
1825 if (regno > index) {
1826 netif_err(tp, probe, tp->dev,
1827 "Out of range of firmware\n");
1831 case PHY_READCOUNT_EQ_SKIP:
1832 if (index + 2 >= fw_size) {
1833 netif_err(tp, probe, tp->dev,
1834 "Out of range of firmware\n");
1838 case PHY_COMP_EQ_SKIPN:
1839 case PHY_COMP_NEQ_SKIPN:
1841 if (index + 1 + regno >= fw_size) {
1842 netif_err(tp, probe, tp->dev,
1843 "Out of range of firmware\n");
1848 case PHY_READ_MAC_BYTE:
1849 case PHY_WRITE_MAC_BYTE:
1850 case PHY_WRITE_ERI_WORD:
1852 netif_err(tp, probe, tp->dev,
1853 "Invalid action 0x%08x\n", action);
1861 for (index = 0; index < fw_size; ) {
1862 u32 action = le32_to_cpu(phytable[index]);
1863 u32 data = action & 0x0000ffff;
1864 u32 regno = (action & 0x0fff0000) >> 16;
1869 switch(action & 0xf0000000) {
1871 predata = rtl_readphy(tp, regno);
1886 case PHY_READ_EFUSE:
1887 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1890 case PHY_CLEAR_READCOUNT:
1895 rtl_writephy(tp, regno, data);
1898 case PHY_READCOUNT_EQ_SKIP:
1899 index += (count == data) ? 2 : 1;
1901 case PHY_COMP_EQ_SKIPN:
1902 if (predata == data)
1906 case PHY_COMP_NEQ_SKIPN:
1907 if (predata != data)
1911 case PHY_WRITE_PREVIOUS:
1912 rtl_writephy(tp, regno, predata);
1923 case PHY_READ_MAC_BYTE:
1924 case PHY_WRITE_MAC_BYTE:
1925 case PHY_WRITE_ERI_WORD:
1932 static void rtl_release_firmware(struct rtl8169_private *tp)
1934 if (!IS_ERR_OR_NULL(tp->fw))
1935 release_firmware(tp->fw);
1936 tp->fw = RTL_FIRMWARE_UNKNOWN;
1939 static void rtl_apply_firmware(struct rtl8169_private *tp)
1941 const struct firmware *fw = tp->fw;
1943 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1944 if (!IS_ERR_OR_NULL(fw))
1945 rtl_phy_write_fw(tp, fw);
1948 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1950 if (rtl_readphy(tp, reg) != val)
1951 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1953 rtl_apply_firmware(tp);
1956 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
1958 static const struct phy_reg phy_reg_init[] = {
2020 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2023 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2025 static const struct phy_reg phy_reg_init[] = {
2031 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2034 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2036 struct pci_dev *pdev = tp->pci_dev;
2037 u16 vendor_id, device_id;
2039 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2040 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2042 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2045 rtl_writephy(tp, 0x1f, 0x0001);
2046 rtl_writephy(tp, 0x10, 0xf01b);
2047 rtl_writephy(tp, 0x1f, 0x0000);
2050 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2052 static const struct phy_reg phy_reg_init[] = {
2092 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2094 rtl8169scd_hw_phy_config_quirk(tp);
2097 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2099 static const struct phy_reg phy_reg_init[] = {
2147 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2150 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2152 static const struct phy_reg phy_reg_init[] = {
2157 rtl_writephy(tp, 0x1f, 0x0001);
2158 rtl_patchphy(tp, 0x16, 1 << 0);
2160 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2163 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2165 static const struct phy_reg phy_reg_init[] = {
2171 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2174 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2176 static const struct phy_reg phy_reg_init[] = {
2184 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2187 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2189 static const struct phy_reg phy_reg_init[] = {
2195 rtl_writephy(tp, 0x1f, 0x0000);
2196 rtl_patchphy(tp, 0x14, 1 << 5);
2197 rtl_patchphy(tp, 0x0d, 1 << 5);
2199 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2202 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2204 static const struct phy_reg phy_reg_init[] = {
2224 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2226 rtl_patchphy(tp, 0x14, 1 << 5);
2227 rtl_patchphy(tp, 0x0d, 1 << 5);
2228 rtl_writephy(tp, 0x1f, 0x0000);
2231 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2233 static const struct phy_reg phy_reg_init[] = {
2251 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2253 rtl_patchphy(tp, 0x16, 1 << 0);
2254 rtl_patchphy(tp, 0x14, 1 << 5);
2255 rtl_patchphy(tp, 0x0d, 1 << 5);
2256 rtl_writephy(tp, 0x1f, 0x0000);
2259 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2261 static const struct phy_reg phy_reg_init[] = {
2273 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2275 rtl_patchphy(tp, 0x16, 1 << 0);
2276 rtl_patchphy(tp, 0x14, 1 << 5);
2277 rtl_patchphy(tp, 0x0d, 1 << 5);
2278 rtl_writephy(tp, 0x1f, 0x0000);
2281 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2283 rtl8168c_3_hw_phy_config(tp);
2286 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2288 static const struct phy_reg phy_reg_init_0[] = {
2289 /* Channel Estimation */
2310 * Enhance line driver power
2319 * Can not link to 1Gbps with bad cable
2320 * Decrease SNR threshold form 21.07dB to 19.04dB
2328 void __iomem *ioaddr = tp->mmio_addr;
2330 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2334 * Fine Tune Switching regulator parameter
2336 rtl_writephy(tp, 0x1f, 0x0002);
2337 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2338 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2340 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2341 static const struct phy_reg phy_reg_init[] = {
2351 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2353 val = rtl_readphy(tp, 0x0d);
2355 if ((val & 0x00ff) != 0x006c) {
2356 static const u32 set[] = {
2357 0x0065, 0x0066, 0x0067, 0x0068,
2358 0x0069, 0x006a, 0x006b, 0x006c
2362 rtl_writephy(tp, 0x1f, 0x0002);
2365 for (i = 0; i < ARRAY_SIZE(set); i++)
2366 rtl_writephy(tp, 0x0d, val | set[i]);
2369 static const struct phy_reg phy_reg_init[] = {
2377 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2380 /* RSET couple improve */
2381 rtl_writephy(tp, 0x1f, 0x0002);
2382 rtl_patchphy(tp, 0x0d, 0x0300);
2383 rtl_patchphy(tp, 0x0f, 0x0010);
2385 /* Fine tune PLL performance */
2386 rtl_writephy(tp, 0x1f, 0x0002);
2387 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2388 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2390 rtl_writephy(tp, 0x1f, 0x0005);
2391 rtl_writephy(tp, 0x05, 0x001b);
2393 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2395 rtl_writephy(tp, 0x1f, 0x0000);
2398 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2400 static const struct phy_reg phy_reg_init_0[] = {
2401 /* Channel Estimation */
2422 * Enhance line driver power
2431 * Can not link to 1Gbps with bad cable
2432 * Decrease SNR threshold form 21.07dB to 19.04dB
2440 void __iomem *ioaddr = tp->mmio_addr;
2442 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2444 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2445 static const struct phy_reg phy_reg_init[] = {
2456 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2458 val = rtl_readphy(tp, 0x0d);
2459 if ((val & 0x00ff) != 0x006c) {
2460 static const u32 set[] = {
2461 0x0065, 0x0066, 0x0067, 0x0068,
2462 0x0069, 0x006a, 0x006b, 0x006c
2466 rtl_writephy(tp, 0x1f, 0x0002);
2469 for (i = 0; i < ARRAY_SIZE(set); i++)
2470 rtl_writephy(tp, 0x0d, val | set[i]);
2473 static const struct phy_reg phy_reg_init[] = {
2481 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2484 /* Fine tune PLL performance */
2485 rtl_writephy(tp, 0x1f, 0x0002);
2486 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2487 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2489 /* Switching regulator Slew rate */
2490 rtl_writephy(tp, 0x1f, 0x0002);
2491 rtl_patchphy(tp, 0x0f, 0x0017);
2493 rtl_writephy(tp, 0x1f, 0x0005);
2494 rtl_writephy(tp, 0x05, 0x001b);
2496 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2498 rtl_writephy(tp, 0x1f, 0x0000);
2501 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2503 static const struct phy_reg phy_reg_init[] = {
2559 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2562 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2564 static const struct phy_reg phy_reg_init[] = {
2574 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2575 rtl_patchphy(tp, 0x0d, 1 << 5);
2578 static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2580 static const struct phy_reg phy_reg_init[] = {
2581 /* Enable Delay cap */
2587 /* Channel estimation fine tune */
2596 /* Update PFM & 10M TX idle timer */
2608 rtl_apply_firmware(tp);
2610 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2612 /* DCO enable for 10M IDLE Power */
2613 rtl_writephy(tp, 0x1f, 0x0007);
2614 rtl_writephy(tp, 0x1e, 0x0023);
2615 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2616 rtl_writephy(tp, 0x1f, 0x0000);
2618 /* For impedance matching */
2619 rtl_writephy(tp, 0x1f, 0x0002);
2620 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2621 rtl_writephy(tp, 0x1f, 0x0000);
2623 /* PHY auto speed down */
2624 rtl_writephy(tp, 0x1f, 0x0007);
2625 rtl_writephy(tp, 0x1e, 0x002d);
2626 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2627 rtl_writephy(tp, 0x1f, 0x0000);
2628 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2630 rtl_writephy(tp, 0x1f, 0x0005);
2631 rtl_writephy(tp, 0x05, 0x8b86);
2632 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2633 rtl_writephy(tp, 0x1f, 0x0000);
2635 rtl_writephy(tp, 0x1f, 0x0005);
2636 rtl_writephy(tp, 0x05, 0x8b85);
2637 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2638 rtl_writephy(tp, 0x1f, 0x0007);
2639 rtl_writephy(tp, 0x1e, 0x0020);
2640 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2641 rtl_writephy(tp, 0x1f, 0x0006);
2642 rtl_writephy(tp, 0x00, 0x5a00);
2643 rtl_writephy(tp, 0x1f, 0x0000);
2644 rtl_writephy(tp, 0x0d, 0x0007);
2645 rtl_writephy(tp, 0x0e, 0x003c);
2646 rtl_writephy(tp, 0x0d, 0x4007);
2647 rtl_writephy(tp, 0x0e, 0x0000);
2648 rtl_writephy(tp, 0x0d, 0x0000);
2651 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
2653 static const struct phy_reg phy_reg_init[] = {
2660 rtl_writephy(tp, 0x1f, 0x0000);
2661 rtl_patchphy(tp, 0x11, 1 << 12);
2662 rtl_patchphy(tp, 0x19, 1 << 13);
2663 rtl_patchphy(tp, 0x10, 1 << 15);
2665 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2668 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2670 static const struct phy_reg phy_reg_init[] = {
2684 /* Disable ALDPS before ram code */
2685 rtl_writephy(tp, 0x1f, 0x0000);
2686 rtl_writephy(tp, 0x18, 0x0310);
2689 rtl_apply_firmware(tp);
2691 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2694 static void rtl_hw_phy_config(struct net_device *dev)
2696 struct rtl8169_private *tp = netdev_priv(dev);
2698 rtl8169_print_mac_version(tp);
2700 switch (tp->mac_version) {
2701 case RTL_GIGA_MAC_VER_01:
2703 case RTL_GIGA_MAC_VER_02:
2704 case RTL_GIGA_MAC_VER_03:
2705 rtl8169s_hw_phy_config(tp);
2707 case RTL_GIGA_MAC_VER_04:
2708 rtl8169sb_hw_phy_config(tp);
2710 case RTL_GIGA_MAC_VER_05:
2711 rtl8169scd_hw_phy_config(tp);
2713 case RTL_GIGA_MAC_VER_06:
2714 rtl8169sce_hw_phy_config(tp);
2716 case RTL_GIGA_MAC_VER_07:
2717 case RTL_GIGA_MAC_VER_08:
2718 case RTL_GIGA_MAC_VER_09:
2719 rtl8102e_hw_phy_config(tp);
2721 case RTL_GIGA_MAC_VER_11:
2722 rtl8168bb_hw_phy_config(tp);
2724 case RTL_GIGA_MAC_VER_12:
2725 rtl8168bef_hw_phy_config(tp);
2727 case RTL_GIGA_MAC_VER_17:
2728 rtl8168bef_hw_phy_config(tp);
2730 case RTL_GIGA_MAC_VER_18:
2731 rtl8168cp_1_hw_phy_config(tp);
2733 case RTL_GIGA_MAC_VER_19:
2734 rtl8168c_1_hw_phy_config(tp);
2736 case RTL_GIGA_MAC_VER_20:
2737 rtl8168c_2_hw_phy_config(tp);
2739 case RTL_GIGA_MAC_VER_21:
2740 rtl8168c_3_hw_phy_config(tp);
2742 case RTL_GIGA_MAC_VER_22:
2743 rtl8168c_4_hw_phy_config(tp);
2745 case RTL_GIGA_MAC_VER_23:
2746 case RTL_GIGA_MAC_VER_24:
2747 rtl8168cp_2_hw_phy_config(tp);
2749 case RTL_GIGA_MAC_VER_25:
2750 rtl8168d_1_hw_phy_config(tp);
2752 case RTL_GIGA_MAC_VER_26:
2753 rtl8168d_2_hw_phy_config(tp);
2755 case RTL_GIGA_MAC_VER_27:
2756 rtl8168d_3_hw_phy_config(tp);
2758 case RTL_GIGA_MAC_VER_28:
2759 rtl8168d_4_hw_phy_config(tp);
2761 case RTL_GIGA_MAC_VER_29:
2762 case RTL_GIGA_MAC_VER_30:
2763 rtl8105e_hw_phy_config(tp);
2765 case RTL_GIGA_MAC_VER_31:
2768 case RTL_GIGA_MAC_VER_32:
2769 case RTL_GIGA_MAC_VER_33:
2770 rtl8168e_hw_phy_config(tp);
2778 static void rtl8169_phy_timer(unsigned long __opaque)
2780 struct net_device *dev = (struct net_device *)__opaque;
2781 struct rtl8169_private *tp = netdev_priv(dev);
2782 struct timer_list *timer = &tp->timer;
2783 void __iomem *ioaddr = tp->mmio_addr;
2784 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2786 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
2788 spin_lock_irq(&tp->lock);
2790 if (tp->phy_reset_pending(tp)) {
2792 * A busy loop could burn quite a few cycles on nowadays CPU.
2793 * Let's delay the execution of the timer for a few ticks.
2799 if (tp->link_ok(ioaddr))
2802 netif_warn(tp, link, dev, "PHY reset until link up\n");
2804 tp->phy_reset_enable(tp);
2807 mod_timer(timer, jiffies + timeout);
2809 spin_unlock_irq(&tp->lock);
2812 #ifdef CONFIG_NET_POLL_CONTROLLER
2814 * Polling 'interrupt' - used by things like netconsole to send skbs
2815 * without having to re-enable interrupts. It's not called while
2816 * the interrupt routine is executing.
2818 static void rtl8169_netpoll(struct net_device *dev)
2820 struct rtl8169_private *tp = netdev_priv(dev);
2821 struct pci_dev *pdev = tp->pci_dev;
2823 disable_irq(pdev->irq);
2824 rtl8169_interrupt(pdev->irq, dev);
2825 enable_irq(pdev->irq);
2829 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2830 void __iomem *ioaddr)
2833 pci_release_regions(pdev);
2834 pci_clear_mwi(pdev);
2835 pci_disable_device(pdev);
2839 static void rtl8169_phy_reset(struct net_device *dev,
2840 struct rtl8169_private *tp)
2844 tp->phy_reset_enable(tp);
2845 for (i = 0; i < 100; i++) {
2846 if (!tp->phy_reset_pending(tp))
2850 netif_err(tp, link, dev, "PHY reset failed\n");
2853 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
2855 void __iomem *ioaddr = tp->mmio_addr;
2857 rtl_hw_phy_config(dev);
2859 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2860 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2864 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2866 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2867 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
2869 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
2870 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2872 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
2873 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
2876 rtl8169_phy_reset(dev, tp);
2878 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2879 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2880 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2881 (tp->mii.supports_gmii ?
2882 ADVERTISED_1000baseT_Half |
2883 ADVERTISED_1000baseT_Full : 0));
2885 if (RTL_R8(PHYstatus) & TBI_Enable)
2886 netif_info(tp, link, dev, "TBI auto-negotiating\n");
2889 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2891 void __iomem *ioaddr = tp->mmio_addr;
2895 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2896 high = addr[4] | (addr[5] << 8);
2898 spin_lock_irq(&tp->lock);
2900 RTL_W8(Cfg9346, Cfg9346_Unlock);
2902 RTL_W32(MAC4, high);
2908 RTL_W8(Cfg9346, Cfg9346_Lock);
2910 spin_unlock_irq(&tp->lock);
2913 static int rtl_set_mac_address(struct net_device *dev, void *p)
2915 struct rtl8169_private *tp = netdev_priv(dev);
2916 struct sockaddr *addr = p;
2918 if (!is_valid_ether_addr(addr->sa_data))
2919 return -EADDRNOTAVAIL;
2921 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2923 rtl_rar_set(tp, dev->dev_addr);
2928 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2930 struct rtl8169_private *tp = netdev_priv(dev);
2931 struct mii_ioctl_data *data = if_mii(ifr);
2933 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2936 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2937 struct mii_ioctl_data *data, int cmd)
2941 data->phy_id = 32; /* Internal PHY */
2945 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
2949 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
2955 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2960 static const struct rtl_cfg_info {
2961 void (*hw_start)(struct net_device *);
2962 unsigned int region;
2968 } rtl_cfg_infos [] = {
2970 .hw_start = rtl_hw_start_8169,
2973 .intr_event = SYSErr | LinkChg | RxOverflow |
2974 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
2975 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
2976 .features = RTL_FEATURE_GMII,
2977 .default_ver = RTL_GIGA_MAC_VER_01,
2980 .hw_start = rtl_hw_start_8168,
2983 .intr_event = SYSErr | LinkChg | RxOverflow |
2984 TxErr | TxOK | RxOK | RxErr,
2985 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
2986 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2987 .default_ver = RTL_GIGA_MAC_VER_11,
2990 .hw_start = rtl_hw_start_8101,
2993 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2994 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
2995 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
2996 .features = RTL_FEATURE_MSI,
2997 .default_ver = RTL_GIGA_MAC_VER_13,
3001 /* Cfg9346_Unlock assumed. */
3002 static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3003 const struct rtl_cfg_info *cfg)
3008 cfg2 = RTL_R8(Config2) & ~MSIEnable;
3009 if (cfg->features & RTL_FEATURE_MSI) {
3010 if (pci_enable_msi(pdev)) {
3011 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3014 msi = RTL_FEATURE_MSI;
3017 RTL_W8(Config2, cfg2);
3021 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3023 if (tp->features & RTL_FEATURE_MSI) {
3024 pci_disable_msi(pdev);
3025 tp->features &= ~RTL_FEATURE_MSI;
3029 static const struct net_device_ops rtl8169_netdev_ops = {
3030 .ndo_open = rtl8169_open,
3031 .ndo_stop = rtl8169_close,
3032 .ndo_get_stats = rtl8169_get_stats,
3033 .ndo_start_xmit = rtl8169_start_xmit,
3034 .ndo_tx_timeout = rtl8169_tx_timeout,
3035 .ndo_validate_addr = eth_validate_addr,
3036 .ndo_change_mtu = rtl8169_change_mtu,
3037 .ndo_fix_features = rtl8169_fix_features,
3038 .ndo_set_features = rtl8169_set_features,
3039 .ndo_set_mac_address = rtl_set_mac_address,
3040 .ndo_do_ioctl = rtl8169_ioctl,
3041 .ndo_set_multicast_list = rtl_set_rx_mode,
3042 #ifdef CONFIG_NET_POLL_CONTROLLER
3043 .ndo_poll_controller = rtl8169_netpoll,
3048 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3050 struct mdio_ops *ops = &tp->mdio_ops;
3052 switch (tp->mac_version) {
3053 case RTL_GIGA_MAC_VER_27:
3054 ops->write = r8168dp_1_mdio_write;
3055 ops->read = r8168dp_1_mdio_read;
3057 case RTL_GIGA_MAC_VER_28:
3058 case RTL_GIGA_MAC_VER_31:
3059 ops->write = r8168dp_2_mdio_write;
3060 ops->read = r8168dp_2_mdio_read;
3063 ops->write = r8169_mdio_write;
3064 ops->read = r8169_mdio_read;
3069 static void r810x_phy_power_down(struct rtl8169_private *tp)
3071 rtl_writephy(tp, 0x1f, 0x0000);
3072 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3075 static void r810x_phy_power_up(struct rtl8169_private *tp)
3077 rtl_writephy(tp, 0x1f, 0x0000);
3078 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3081 static void r810x_pll_power_down(struct rtl8169_private *tp)
3083 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3084 rtl_writephy(tp, 0x1f, 0x0000);
3085 rtl_writephy(tp, MII_BMCR, 0x0000);
3089 r810x_phy_power_down(tp);
3092 static void r810x_pll_power_up(struct rtl8169_private *tp)
3094 r810x_phy_power_up(tp);
3097 static void r8168_phy_power_up(struct rtl8169_private *tp)
3099 rtl_writephy(tp, 0x1f, 0x0000);
3100 switch (tp->mac_version) {
3101 case RTL_GIGA_MAC_VER_11:
3102 case RTL_GIGA_MAC_VER_12:
3103 case RTL_GIGA_MAC_VER_17:
3104 case RTL_GIGA_MAC_VER_18:
3105 case RTL_GIGA_MAC_VER_19:
3106 case RTL_GIGA_MAC_VER_20:
3107 case RTL_GIGA_MAC_VER_21:
3108 case RTL_GIGA_MAC_VER_22:
3109 case RTL_GIGA_MAC_VER_23:
3110 case RTL_GIGA_MAC_VER_24:
3111 case RTL_GIGA_MAC_VER_25:
3112 case RTL_GIGA_MAC_VER_26:
3113 case RTL_GIGA_MAC_VER_27:
3114 case RTL_GIGA_MAC_VER_28:
3115 case RTL_GIGA_MAC_VER_31:
3116 rtl_writephy(tp, 0x0e, 0x0000);
3121 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3124 static void r8168_phy_power_down(struct rtl8169_private *tp)
3126 rtl_writephy(tp, 0x1f, 0x0000);
3127 switch (tp->mac_version) {
3128 case RTL_GIGA_MAC_VER_32:
3129 case RTL_GIGA_MAC_VER_33:
3130 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3133 case RTL_GIGA_MAC_VER_11:
3134 case RTL_GIGA_MAC_VER_12:
3135 case RTL_GIGA_MAC_VER_17:
3136 case RTL_GIGA_MAC_VER_18:
3137 case RTL_GIGA_MAC_VER_19:
3138 case RTL_GIGA_MAC_VER_20:
3139 case RTL_GIGA_MAC_VER_21:
3140 case RTL_GIGA_MAC_VER_22:
3141 case RTL_GIGA_MAC_VER_23:
3142 case RTL_GIGA_MAC_VER_24:
3143 case RTL_GIGA_MAC_VER_25:
3144 case RTL_GIGA_MAC_VER_26:
3145 case RTL_GIGA_MAC_VER_27:
3146 case RTL_GIGA_MAC_VER_28:
3147 case RTL_GIGA_MAC_VER_31:
3148 rtl_writephy(tp, 0x0e, 0x0200);
3150 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3155 static void r8168_pll_power_down(struct rtl8169_private *tp)
3157 void __iomem *ioaddr = tp->mmio_addr;
3159 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3160 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3161 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3162 r8168dp_check_dash(tp)) {
3166 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3167 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
3168 (RTL_R16(CPlusCmd) & ASF)) {
3172 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3173 tp->mac_version == RTL_GIGA_MAC_VER_33)
3174 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3176 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3177 rtl_writephy(tp, 0x1f, 0x0000);
3178 rtl_writephy(tp, MII_BMCR, 0x0000);
3180 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3181 tp->mac_version == RTL_GIGA_MAC_VER_33)
3182 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3183 AcceptMulticast | AcceptMyPhys);
3187 r8168_phy_power_down(tp);
3189 switch (tp->mac_version) {
3190 case RTL_GIGA_MAC_VER_25:
3191 case RTL_GIGA_MAC_VER_26:
3192 case RTL_GIGA_MAC_VER_27:
3193 case RTL_GIGA_MAC_VER_28:
3194 case RTL_GIGA_MAC_VER_31:
3195 case RTL_GIGA_MAC_VER_32:
3196 case RTL_GIGA_MAC_VER_33:
3197 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3202 static void r8168_pll_power_up(struct rtl8169_private *tp)
3204 void __iomem *ioaddr = tp->mmio_addr;
3206 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3207 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3208 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3209 r8168dp_check_dash(tp)) {
3213 switch (tp->mac_version) {
3214 case RTL_GIGA_MAC_VER_25:
3215 case RTL_GIGA_MAC_VER_26:
3216 case RTL_GIGA_MAC_VER_27:
3217 case RTL_GIGA_MAC_VER_28:
3218 case RTL_GIGA_MAC_VER_31:
3219 case RTL_GIGA_MAC_VER_32:
3220 case RTL_GIGA_MAC_VER_33:
3221 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3225 r8168_phy_power_up(tp);
3228 static void rtl_generic_op(struct rtl8169_private *tp,
3229 void (*op)(struct rtl8169_private *))
3235 static void rtl_pll_power_down(struct rtl8169_private *tp)
3237 rtl_generic_op(tp, tp->pll_power_ops.down);
3240 static void rtl_pll_power_up(struct rtl8169_private *tp)
3242 rtl_generic_op(tp, tp->pll_power_ops.up);
3245 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3247 struct pll_power_ops *ops = &tp->pll_power_ops;
3249 switch (tp->mac_version) {
3250 case RTL_GIGA_MAC_VER_07:
3251 case RTL_GIGA_MAC_VER_08:
3252 case RTL_GIGA_MAC_VER_09:
3253 case RTL_GIGA_MAC_VER_10:
3254 case RTL_GIGA_MAC_VER_16:
3255 case RTL_GIGA_MAC_VER_29:
3256 case RTL_GIGA_MAC_VER_30:
3257 ops->down = r810x_pll_power_down;
3258 ops->up = r810x_pll_power_up;
3261 case RTL_GIGA_MAC_VER_11:
3262 case RTL_GIGA_MAC_VER_12:
3263 case RTL_GIGA_MAC_VER_17:
3264 case RTL_GIGA_MAC_VER_18:
3265 case RTL_GIGA_MAC_VER_19:
3266 case RTL_GIGA_MAC_VER_20:
3267 case RTL_GIGA_MAC_VER_21:
3268 case RTL_GIGA_MAC_VER_22:
3269 case RTL_GIGA_MAC_VER_23:
3270 case RTL_GIGA_MAC_VER_24:
3271 case RTL_GIGA_MAC_VER_25:
3272 case RTL_GIGA_MAC_VER_26:
3273 case RTL_GIGA_MAC_VER_27:
3274 case RTL_GIGA_MAC_VER_28:
3275 case RTL_GIGA_MAC_VER_31:
3276 case RTL_GIGA_MAC_VER_32:
3277 case RTL_GIGA_MAC_VER_33:
3278 ops->down = r8168_pll_power_down;
3279 ops->up = r8168_pll_power_up;
3289 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3291 rtl_generic_op(tp, tp->jumbo_ops.enable);
3294 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3296 rtl_generic_op(tp, tp->jumbo_ops.disable);
3299 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3301 void __iomem *ioaddr = tp->mmio_addr;
3303 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3304 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3305 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3308 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3310 void __iomem *ioaddr = tp->mmio_addr;
3312 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3313 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3314 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3317 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3319 void __iomem *ioaddr = tp->mmio_addr;
3321 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3324 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3326 void __iomem *ioaddr = tp->mmio_addr;
3328 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3331 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3333 void __iomem *ioaddr = tp->mmio_addr;
3334 struct pci_dev *pdev = tp->pci_dev;
3336 RTL_W8(MaxTxPacketSize, 0x3f);
3337 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3338 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
3339 pci_write_config_byte(pdev, 0x79, 0x20);
3342 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3344 void __iomem *ioaddr = tp->mmio_addr;
3345 struct pci_dev *pdev = tp->pci_dev;
3347 RTL_W8(MaxTxPacketSize, 0x0c);
3348 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3349 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
3350 pci_write_config_byte(pdev, 0x79, 0x50);
3353 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3355 rtl_tx_performance_tweak(tp->pci_dev,
3356 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3359 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3361 rtl_tx_performance_tweak(tp->pci_dev,
3362 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3365 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3367 void __iomem *ioaddr = tp->mmio_addr;
3369 r8168b_0_hw_jumbo_enable(tp);
3371 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3374 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3376 void __iomem *ioaddr = tp->mmio_addr;
3378 r8168b_0_hw_jumbo_disable(tp);
3380 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3383 static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3385 struct jumbo_ops *ops = &tp->jumbo_ops;
3387 switch (tp->mac_version) {
3388 case RTL_GIGA_MAC_VER_11:
3389 ops->disable = r8168b_0_hw_jumbo_disable;
3390 ops->enable = r8168b_0_hw_jumbo_enable;
3392 case RTL_GIGA_MAC_VER_12:
3393 case RTL_GIGA_MAC_VER_17:
3394 ops->disable = r8168b_1_hw_jumbo_disable;
3395 ops->enable = r8168b_1_hw_jumbo_enable;
3397 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3398 case RTL_GIGA_MAC_VER_19:
3399 case RTL_GIGA_MAC_VER_20:
3400 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3401 case RTL_GIGA_MAC_VER_22:
3402 case RTL_GIGA_MAC_VER_23:
3403 case RTL_GIGA_MAC_VER_24:
3404 case RTL_GIGA_MAC_VER_25:
3405 case RTL_GIGA_MAC_VER_26:
3406 ops->disable = r8168c_hw_jumbo_disable;
3407 ops->enable = r8168c_hw_jumbo_enable;
3409 case RTL_GIGA_MAC_VER_27:
3410 case RTL_GIGA_MAC_VER_28:
3411 ops->disable = r8168dp_hw_jumbo_disable;
3412 ops->enable = r8168dp_hw_jumbo_enable;
3414 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3415 case RTL_GIGA_MAC_VER_32:
3416 case RTL_GIGA_MAC_VER_33:
3417 ops->disable = r8168e_hw_jumbo_disable;
3418 ops->enable = r8168e_hw_jumbo_enable;
3422 * No action needed for jumbo frames with 8169.
3423 * No jumbo for 810x at all.
3426 ops->disable = NULL;
3432 static void rtl_hw_reset(struct rtl8169_private *tp)
3434 void __iomem *ioaddr = tp->mmio_addr;
3437 /* Soft reset the chip. */
3438 RTL_W8(ChipCmd, CmdReset);
3440 /* Check that the chip has finished the reset. */
3441 for (i = 0; i < 100; i++) {
3442 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3444 msleep_interruptible(1);
3448 static int __devinit
3449 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3451 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3452 const unsigned int region = cfg->region;
3453 struct rtl8169_private *tp;
3454 struct mii_if_info *mii;
3455 struct net_device *dev;
3456 void __iomem *ioaddr;
3460 if (netif_msg_drv(&debug)) {
3461 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3462 MODULENAME, RTL8169_VERSION);
3465 dev = alloc_etherdev(sizeof (*tp));
3467 if (netif_msg_drv(&debug))
3468 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
3473 SET_NETDEV_DEV(dev, &pdev->dev);
3474 dev->netdev_ops = &rtl8169_netdev_ops;
3475 tp = netdev_priv(dev);
3478 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
3482 mii->mdio_read = rtl_mdio_read;
3483 mii->mdio_write = rtl_mdio_write;
3484 mii->phy_id_mask = 0x1f;
3485 mii->reg_num_mask = 0x1f;
3486 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3488 /* disable ASPM completely as that cause random device stop working
3489 * problems as well as full system hangs for some PCIe devices users */
3490 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3491 PCIE_LINK_STATE_CLKPM);
3493 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3494 rc = pci_enable_device(pdev);
3496 netif_err(tp, probe, dev, "enable failure\n");
3497 goto err_out_free_dev_1;
3500 if (pci_set_mwi(pdev) < 0)
3501 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
3503 /* make sure PCI base addr 1 is MMIO */
3504 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
3505 netif_err(tp, probe, dev,
3506 "region #%d not an MMIO resource, aborting\n",
3512 /* check for weird/broken PCI region reporting */
3513 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
3514 netif_err(tp, probe, dev,
3515 "Invalid PCI region size(s), aborting\n");
3520 rc = pci_request_regions(pdev, MODULENAME);
3522 netif_err(tp, probe, dev, "could not request regions\n");
3526 tp->cp_cmd = RxChkSum;
3528 if ((sizeof(dma_addr_t) > 4) &&
3529 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
3530 tp->cp_cmd |= PCIDAC;
3531 dev->features |= NETIF_F_HIGHDMA;
3533 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3535 netif_err(tp, probe, dev, "DMA configuration failed\n");
3536 goto err_out_free_res_3;
3540 /* ioremap MMIO region */
3541 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
3543 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
3545 goto err_out_free_res_3;
3547 tp->mmio_addr = ioaddr;
3549 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3551 netif_info(tp, probe, dev, "no PCI Express capability\n");
3553 RTL_W16(IntrMask, 0x0000);
3557 RTL_W16(IntrStatus, 0xffff);
3559 pci_set_master(pdev);
3561 /* Identify chip attached to board */
3562 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
3565 * Pretend we are using VLANs; This bypasses a nasty bug where
3566 * Interrupts stop flowing on high load on 8110SCd controllers.
3568 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3569 tp->cp_cmd |= RxVlan;
3571 rtl_init_mdio_ops(tp);
3572 rtl_init_pll_power_ops(tp);
3573 rtl_init_jumbo_ops(tp);
3575 rtl8169_print_mac_version(tp);
3577 chipset = tp->mac_version;
3578 tp->txd_version = rtl_chip_infos[chipset].txd_version;
3580 RTL_W8(Cfg9346, Cfg9346_Unlock);
3581 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3582 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
3583 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3584 tp->features |= RTL_FEATURE_WOL;
3585 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3586 tp->features |= RTL_FEATURE_WOL;
3587 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
3588 RTL_W8(Cfg9346, Cfg9346_Lock);
3590 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3591 (RTL_R8(PHYstatus) & TBI_Enable)) {
3592 tp->set_speed = rtl8169_set_speed_tbi;
3593 tp->get_settings = rtl8169_gset_tbi;
3594 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3595 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3596 tp->link_ok = rtl8169_tbi_link_ok;
3597 tp->do_ioctl = rtl_tbi_ioctl;
3599 tp->set_speed = rtl8169_set_speed_xmii;
3600 tp->get_settings = rtl8169_gset_xmii;
3601 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3602 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3603 tp->link_ok = rtl8169_xmii_link_ok;
3604 tp->do_ioctl = rtl_xmii_ioctl;
3607 spin_lock_init(&tp->lock);
3609 /* Get MAC address */
3610 for (i = 0; i < MAC_ADDR_LEN; i++)
3611 dev->dev_addr[i] = RTL_R8(MAC0 + i);
3612 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
3614 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
3615 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3616 dev->irq = pdev->irq;
3617 dev->base_addr = (unsigned long) ioaddr;
3619 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
3621 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3622 * properly for all devices */
3623 dev->features |= NETIF_F_RXCSUM |
3624 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3626 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3627 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3628 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3631 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3632 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3633 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
3635 tp->intr_mask = 0xffff;
3636 tp->hw_start = cfg->hw_start;
3637 tp->intr_event = cfg->intr_event;
3638 tp->napi_event = cfg->napi_event;
3640 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
3641 ~(RxBOVF | RxFOVF) : ~0;
3643 init_timer(&tp->timer);
3644 tp->timer.data = (unsigned long) dev;
3645 tp->timer.function = rtl8169_phy_timer;
3647 tp->fw = RTL_FIRMWARE_UNKNOWN;
3649 rc = register_netdev(dev);
3653 pci_set_drvdata(pdev, dev);
3655 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3656 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
3657 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
3658 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
3659 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
3660 "tx checksumming: %s]\n",
3661 rtl_chip_infos[chipset].jumbo_max,
3662 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
3665 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3666 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3667 tp->mac_version == RTL_GIGA_MAC_VER_31) {
3668 rtl8168_driver_start(tp);
3671 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
3673 if (pci_dev_run_wake(pdev))
3674 pm_runtime_put_noidle(&pdev->dev);
3676 netif_carrier_off(dev);
3682 rtl_disable_msi(pdev, tp);
3685 pci_release_regions(pdev);
3687 pci_clear_mwi(pdev);
3688 pci_disable_device(pdev);
3694 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
3696 struct net_device *dev = pci_get_drvdata(pdev);
3697 struct rtl8169_private *tp = netdev_priv(dev);
3699 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3700 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3701 tp->mac_version == RTL_GIGA_MAC_VER_31) {
3702 rtl8168_driver_stop(tp);
3705 cancel_delayed_work_sync(&tp->task);
3707 unregister_netdev(dev);
3709 rtl_release_firmware(tp);
3711 if (pci_dev_run_wake(pdev))
3712 pm_runtime_get_noresume(&pdev->dev);
3714 /* restore original MAC address */
3715 rtl_rar_set(tp, dev->perm_addr);
3717 rtl_disable_msi(pdev, tp);
3718 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3719 pci_set_drvdata(pdev, NULL);
3722 static void rtl_request_firmware(struct rtl8169_private *tp)
3724 /* Return early if the firmware is already loaded / cached. */
3725 if (IS_ERR(tp->fw)) {
3728 name = rtl_lookup_firmware_name(tp);
3732 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
3736 netif_warn(tp, ifup, tp->dev, "unable to load "
3737 "firmware patch %s (%d)\n", name, rc);
3743 static int rtl8169_open(struct net_device *dev)
3745 struct rtl8169_private *tp = netdev_priv(dev);
3746 void __iomem *ioaddr = tp->mmio_addr;
3747 struct pci_dev *pdev = tp->pci_dev;
3748 int retval = -ENOMEM;
3750 pm_runtime_get_sync(&pdev->dev);
3753 * Rx and Tx desscriptors needs 256 bytes alignment.
3754 * dma_alloc_coherent provides more.
3756 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3757 &tp->TxPhyAddr, GFP_KERNEL);
3758 if (!tp->TxDescArray)
3759 goto err_pm_runtime_put;
3761 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3762 &tp->RxPhyAddr, GFP_KERNEL);
3763 if (!tp->RxDescArray)
3766 retval = rtl8169_init_ring(dev);
3770 INIT_DELAYED_WORK(&tp->task, NULL);
3774 rtl_request_firmware(tp);
3776 retval = request_irq(dev->irq, rtl8169_interrupt,
3777 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
3780 goto err_release_fw_2;
3782 napi_enable(&tp->napi);
3784 rtl8169_init_phy(dev, tp);
3786 rtl8169_set_features(dev, dev->features);
3788 rtl_pll_power_up(tp);
3792 tp->saved_wolopts = 0;
3793 pm_runtime_put_noidle(&pdev->dev);
3795 rtl8169_check_link_status(dev, tp, ioaddr);
3800 rtl_release_firmware(tp);
3801 rtl8169_rx_clear(tp);
3803 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3805 tp->RxDescArray = NULL;
3807 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3809 tp->TxDescArray = NULL;
3811 pm_runtime_put_noidle(&pdev->dev);
3815 static void rtl8169_hw_reset(struct rtl8169_private *tp)
3817 void __iomem *ioaddr = tp->mmio_addr;
3819 /* Disable interrupts */
3820 rtl8169_irq_mask_and_ack(ioaddr);
3822 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3823 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3824 tp->mac_version == RTL_GIGA_MAC_VER_31) {
3825 while (RTL_R8(TxPoll) & NPQ)
3830 /* Reset the chipset */
3831 RTL_W8(ChipCmd, CmdReset);
3837 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
3839 void __iomem *ioaddr = tp->mmio_addr;
3840 u32 cfg = rtl8169_rx_config;
3842 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
3843 RTL_W32(RxConfig, cfg);
3845 /* Set DMA burst size and Interframe Gap Time */
3846 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3847 (InterFrameGap << TxInterFrameGapShift));
3850 static void rtl_hw_start(struct net_device *dev)
3852 struct rtl8169_private *tp = netdev_priv(dev);
3858 netif_start_queue(dev);
3861 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3862 void __iomem *ioaddr)
3865 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3866 * register to be written before TxDescAddrLow to work.
3867 * Switching from MMIO to I/O access fixes the issue as well.
3869 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
3870 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
3871 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
3872 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
3875 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3879 cmd = RTL_R16(CPlusCmd);
3880 RTL_W16(CPlusCmd, cmd);
3884 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
3886 /* Low hurts. Let's disable the filtering. */
3887 RTL_W16(RxMaxSize, rx_buf_sz + 1);
3890 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3892 static const struct rtl_cfg2_info {
3897 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3898 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3899 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3900 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3902 const struct rtl_cfg2_info *p = cfg2_info;
3906 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
3907 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
3908 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3909 RTL_W32(0x7c, p->val);
3915 static void rtl_hw_start_8169(struct net_device *dev)
3917 struct rtl8169_private *tp = netdev_priv(dev);
3918 void __iomem *ioaddr = tp->mmio_addr;
3919 struct pci_dev *pdev = tp->pci_dev;
3921 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3922 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3923 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3926 RTL_W8(Cfg9346, Cfg9346_Unlock);
3927 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3928 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3929 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3930 tp->mac_version == RTL_GIGA_MAC_VER_04)
3931 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3933 RTL_W8(EarlyTxThres, NoEarlyTx);
3935 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
3937 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3938 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3939 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3940 tp->mac_version == RTL_GIGA_MAC_VER_04)
3941 rtl_set_rx_tx_config_registers(tp);
3943 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
3945 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3946 tp->mac_version == RTL_GIGA_MAC_VER_03) {
3947 dprintk("Set MAC Reg C+CR Offset 0xE0. "
3948 "Bit-3 and bit-14 MUST be 1\n");
3949 tp->cp_cmd |= (1 << 14);
3952 RTL_W16(CPlusCmd, tp->cp_cmd);
3954 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3957 * Undocumented corner. Supposedly:
3958 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3960 RTL_W16(IntrMitigate, 0x0000);
3962 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3964 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3965 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3966 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3967 tp->mac_version != RTL_GIGA_MAC_VER_04) {
3968 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3969 rtl_set_rx_tx_config_registers(tp);
3972 RTL_W8(Cfg9346, Cfg9346_Lock);
3974 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3977 RTL_W32(RxMissed, 0);
3979 rtl_set_rx_mode(dev);
3981 /* no early-rx interrupts */
3982 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
3984 /* Enable all known interrupts by setting the interrupt mask. */
3985 RTL_W16(IntrMask, tp->intr_event);
3988 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
3992 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
3993 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3996 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3998 rtl_csi_access_enable(ioaddr, 0x17000000);
4001 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4003 rtl_csi_access_enable(ioaddr, 0x27000000);
4007 unsigned int offset;
4012 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
4017 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4018 rtl_ephy_write(ioaddr, e->offset, w);
4023 static void rtl_disable_clock_request(struct pci_dev *pdev)
4025 struct net_device *dev = pci_get_drvdata(pdev);
4026 struct rtl8169_private *tp = netdev_priv(dev);
4027 int cap = tp->pcie_cap;
4032 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4033 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4034 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4038 static void rtl_enable_clock_request(struct pci_dev *pdev)
4040 struct net_device *dev = pci_get_drvdata(pdev);
4041 struct rtl8169_private *tp = netdev_priv(dev);
4042 int cap = tp->pcie_cap;
4047 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4048 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4049 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4053 #define R8168_CPCMD_QUIRK_MASK (\
4064 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4066 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4068 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4070 rtl_tx_performance_tweak(pdev,
4071 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4074 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4076 rtl_hw_start_8168bb(ioaddr, pdev);
4078 RTL_W8(MaxTxPacketSize, TxPacketMax);
4080 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4083 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4085 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4087 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4089 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4091 rtl_disable_clock_request(pdev);
4093 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4096 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
4098 static const struct ephy_info e_info_8168cp[] = {
4099 { 0x01, 0, 0x0001 },
4100 { 0x02, 0x0800, 0x1000 },
4101 { 0x03, 0, 0x0042 },
4102 { 0x06, 0x0080, 0x0000 },
4106 rtl_csi_access_enable_2(ioaddr);
4108 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4110 __rtl_hw_start_8168cp(ioaddr, pdev);
4113 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4115 rtl_csi_access_enable_2(ioaddr);
4117 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4119 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4121 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4124 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4126 rtl_csi_access_enable_2(ioaddr);
4128 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4131 RTL_W8(DBG_REG, 0x20);
4133 RTL_W8(MaxTxPacketSize, TxPacketMax);
4135 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4137 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4140 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4142 static const struct ephy_info e_info_8168c_1[] = {
4143 { 0x02, 0x0800, 0x1000 },
4144 { 0x03, 0, 0x0002 },
4145 { 0x06, 0x0080, 0x0000 }
4148 rtl_csi_access_enable_2(ioaddr);
4150 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4152 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4154 __rtl_hw_start_8168cp(ioaddr, pdev);
4157 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4159 static const struct ephy_info e_info_8168c_2[] = {
4160 { 0x01, 0, 0x0001 },
4161 { 0x03, 0x0400, 0x0220 }
4164 rtl_csi_access_enable_2(ioaddr);
4166 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4168 __rtl_hw_start_8168cp(ioaddr, pdev);
4171 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4173 rtl_hw_start_8168c_2(ioaddr, pdev);
4176 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4178 rtl_csi_access_enable_2(ioaddr);
4180 __rtl_hw_start_8168cp(ioaddr, pdev);
4183 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4185 rtl_csi_access_enable_2(ioaddr);
4187 rtl_disable_clock_request(pdev);
4189 RTL_W8(MaxTxPacketSize, TxPacketMax);
4191 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4193 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4196 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4198 rtl_csi_access_enable_1(ioaddr);
4200 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4202 RTL_W8(MaxTxPacketSize, TxPacketMax);
4204 rtl_disable_clock_request(pdev);
4207 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4209 static const struct ephy_info e_info_8168d_4[] = {
4211 { 0x19, 0x20, 0x50 },
4216 rtl_csi_access_enable_1(ioaddr);
4218 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4220 RTL_W8(MaxTxPacketSize, TxPacketMax);
4222 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4223 const struct ephy_info *e = e_info_8168d_4 + i;
4226 w = rtl_ephy_read(ioaddr, e->offset);
4227 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4230 rtl_enable_clock_request(pdev);
4233 static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4235 static const struct ephy_info e_info_8168e[] = {
4236 { 0x00, 0x0200, 0x0100 },
4237 { 0x00, 0x0000, 0x0004 },
4238 { 0x06, 0x0002, 0x0001 },
4239 { 0x06, 0x0000, 0x0030 },
4240 { 0x07, 0x0000, 0x2000 },
4241 { 0x00, 0x0000, 0x0020 },
4242 { 0x03, 0x5800, 0x2000 },
4243 { 0x03, 0x0000, 0x0001 },
4244 { 0x01, 0x0800, 0x1000 },
4245 { 0x07, 0x0000, 0x4000 },
4246 { 0x1e, 0x0000, 0x2000 },
4247 { 0x19, 0xffff, 0xfe6c },
4248 { 0x0a, 0x0000, 0x0040 }
4251 rtl_csi_access_enable_2(ioaddr);
4253 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4255 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4257 RTL_W8(MaxTxPacketSize, TxPacketMax);
4259 rtl_disable_clock_request(pdev);
4261 /* Reset tx FIFO pointer */
4262 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4263 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
4265 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4268 static void rtl_hw_start_8168(struct net_device *dev)
4270 struct rtl8169_private *tp = netdev_priv(dev);
4271 void __iomem *ioaddr = tp->mmio_addr;
4272 struct pci_dev *pdev = tp->pci_dev;
4274 RTL_W8(Cfg9346, Cfg9346_Unlock);
4276 RTL_W8(MaxTxPacketSize, TxPacketMax);
4278 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4280 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
4282 RTL_W16(CPlusCmd, tp->cp_cmd);
4284 RTL_W16(IntrMitigate, 0x5151);
4286 /* Work around for RxFIFO overflow. */
4287 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4288 tp->mac_version == RTL_GIGA_MAC_VER_22) {
4289 tp->intr_event |= RxFIFOOver | PCSTimeout;
4290 tp->intr_event &= ~RxOverflow;
4293 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4295 rtl_set_rx_mode(dev);
4297 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4298 (InterFrameGap << TxInterFrameGapShift));
4302 switch (tp->mac_version) {
4303 case RTL_GIGA_MAC_VER_11:
4304 rtl_hw_start_8168bb(ioaddr, pdev);
4307 case RTL_GIGA_MAC_VER_12:
4308 case RTL_GIGA_MAC_VER_17:
4309 rtl_hw_start_8168bef(ioaddr, pdev);
4312 case RTL_GIGA_MAC_VER_18:
4313 rtl_hw_start_8168cp_1(ioaddr, pdev);
4316 case RTL_GIGA_MAC_VER_19:
4317 rtl_hw_start_8168c_1(ioaddr, pdev);
4320 case RTL_GIGA_MAC_VER_20:
4321 rtl_hw_start_8168c_2(ioaddr, pdev);
4324 case RTL_GIGA_MAC_VER_21:
4325 rtl_hw_start_8168c_3(ioaddr, pdev);
4328 case RTL_GIGA_MAC_VER_22:
4329 rtl_hw_start_8168c_4(ioaddr, pdev);
4332 case RTL_GIGA_MAC_VER_23:
4333 rtl_hw_start_8168cp_2(ioaddr, pdev);
4336 case RTL_GIGA_MAC_VER_24:
4337 rtl_hw_start_8168cp_3(ioaddr, pdev);
4340 case RTL_GIGA_MAC_VER_25:
4341 case RTL_GIGA_MAC_VER_26:
4342 case RTL_GIGA_MAC_VER_27:
4343 rtl_hw_start_8168d(ioaddr, pdev);
4346 case RTL_GIGA_MAC_VER_28:
4347 rtl_hw_start_8168d_4(ioaddr, pdev);
4350 case RTL_GIGA_MAC_VER_31:
4351 rtl_hw_start_8168dp(ioaddr, pdev);
4354 case RTL_GIGA_MAC_VER_32:
4355 case RTL_GIGA_MAC_VER_33:
4356 rtl_hw_start_8168e(ioaddr, pdev);
4360 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4361 dev->name, tp->mac_version);
4365 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4367 RTL_W8(Cfg9346, Cfg9346_Lock);
4369 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4371 RTL_W16(IntrMask, tp->intr_event);
4374 #define R810X_CPCMD_QUIRK_MASK (\
4385 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4387 static const struct ephy_info e_info_8102e_1[] = {
4388 { 0x01, 0, 0x6e65 },
4389 { 0x02, 0, 0x091f },
4390 { 0x03, 0, 0xc2f9 },
4391 { 0x06, 0, 0xafb5 },
4392 { 0x07, 0, 0x0e00 },
4393 { 0x19, 0, 0xec80 },
4394 { 0x01, 0, 0x2e65 },
4399 rtl_csi_access_enable_2(ioaddr);
4401 RTL_W8(DBG_REG, FIX_NAK_1);
4403 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4406 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4407 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4409 cfg1 = RTL_R8(Config1);
4410 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4411 RTL_W8(Config1, cfg1 & ~LEDS0);
4413 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4416 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4418 rtl_csi_access_enable_2(ioaddr);
4420 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4422 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4423 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4426 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4428 rtl_hw_start_8102e_2(ioaddr, pdev);
4430 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4433 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4435 static const struct ephy_info e_info_8105e_1[] = {
4436 { 0x07, 0, 0x4000 },
4437 { 0x19, 0, 0x0200 },
4438 { 0x19, 0, 0x0020 },
4439 { 0x1e, 0, 0x2000 },
4440 { 0x03, 0, 0x0001 },
4441 { 0x19, 0, 0x0100 },
4442 { 0x19, 0, 0x0004 },
4446 /* Force LAN exit from ASPM if Rx/Tx are not idle */
4447 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4449 /* Disable Early Tally Counter */
4450 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4452 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4453 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4455 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4458 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4460 rtl_hw_start_8105e_1(ioaddr, pdev);
4461 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4464 static void rtl_hw_start_8101(struct net_device *dev)
4466 struct rtl8169_private *tp = netdev_priv(dev);
4467 void __iomem *ioaddr = tp->mmio_addr;
4468 struct pci_dev *pdev = tp->pci_dev;
4470 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4471 tp->mac_version == RTL_GIGA_MAC_VER_16) {
4472 int cap = tp->pcie_cap;
4475 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4476 PCI_EXP_DEVCTL_NOSNOOP_EN);
4480 RTL_W8(Cfg9346, Cfg9346_Unlock);
4482 switch (tp->mac_version) {
4483 case RTL_GIGA_MAC_VER_07:
4484 rtl_hw_start_8102e_1(ioaddr, pdev);
4487 case RTL_GIGA_MAC_VER_08:
4488 rtl_hw_start_8102e_3(ioaddr, pdev);
4491 case RTL_GIGA_MAC_VER_09:
4492 rtl_hw_start_8102e_2(ioaddr, pdev);
4495 case RTL_GIGA_MAC_VER_29:
4496 rtl_hw_start_8105e_1(ioaddr, pdev);
4498 case RTL_GIGA_MAC_VER_30:
4499 rtl_hw_start_8105e_2(ioaddr, pdev);
4503 RTL_W8(Cfg9346, Cfg9346_Lock);
4505 RTL_W8(MaxTxPacketSize, TxPacketMax);
4507 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4509 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
4510 RTL_W16(CPlusCmd, tp->cp_cmd);
4512 RTL_W16(IntrMitigate, 0x0000);
4514 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4516 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4517 rtl_set_rx_tx_config_registers(tp);
4521 rtl_set_rx_mode(dev);
4523 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
4525 RTL_W16(IntrMask, tp->intr_event);
4528 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4530 struct rtl8169_private *tp = netdev_priv(dev);
4532 if (new_mtu < ETH_ZLEN ||
4533 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
4536 if (new_mtu > ETH_DATA_LEN)
4537 rtl_hw_jumbo_enable(tp);
4539 rtl_hw_jumbo_disable(tp);
4542 netdev_update_features(dev);
4547 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4549 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
4550 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4553 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4554 void **data_buff, struct RxDesc *desc)
4556 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
4561 rtl8169_make_unusable_by_asic(desc);
4564 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4566 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4568 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4571 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4574 desc->addr = cpu_to_le64(mapping);
4576 rtl8169_mark_to_asic(desc, rx_buf_sz);
4579 static inline void *rtl8169_align(void *data)
4581 return (void *)ALIGN((long)data, 16);
4584 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4585 struct RxDesc *desc)
4589 struct device *d = &tp->pci_dev->dev;
4590 struct net_device *dev = tp->dev;
4591 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
4593 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4597 if (rtl8169_align(data) != data) {
4599 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4604 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
4606 if (unlikely(dma_mapping_error(d, mapping))) {
4607 if (net_ratelimit())
4608 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
4612 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
4620 static void rtl8169_rx_clear(struct rtl8169_private *tp)
4624 for (i = 0; i < NUM_RX_DESC; i++) {
4625 if (tp->Rx_databuff[i]) {
4626 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
4627 tp->RxDescArray + i);
4632 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
4634 desc->opts1 |= cpu_to_le32(RingEnd);
4637 static int rtl8169_rx_fill(struct rtl8169_private *tp)
4641 for (i = 0; i < NUM_RX_DESC; i++) {
4644 if (tp->Rx_databuff[i])
4647 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
4649 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
4652 tp->Rx_databuff[i] = data;
4655 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4659 rtl8169_rx_clear(tp);
4663 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4665 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4668 static int rtl8169_init_ring(struct net_device *dev)
4670 struct rtl8169_private *tp = netdev_priv(dev);
4672 rtl8169_init_ring_indexes(tp);
4674 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
4675 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
4677 return rtl8169_rx_fill(tp);
4680 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
4681 struct TxDesc *desc)
4683 unsigned int len = tx_skb->len;
4685 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4693 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4698 for (i = 0; i < n; i++) {
4699 unsigned int entry = (start + i) % NUM_TX_DESC;
4700 struct ring_info *tx_skb = tp->tx_skb + entry;
4701 unsigned int len = tx_skb->len;
4704 struct sk_buff *skb = tx_skb->skb;
4706 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4707 tp->TxDescArray + entry);
4709 tp->dev->stats.tx_dropped++;
4717 static void rtl8169_tx_clear(struct rtl8169_private *tp)
4719 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
4720 tp->cur_tx = tp->dirty_tx = 0;
4723 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
4725 struct rtl8169_private *tp = netdev_priv(dev);
4727 PREPARE_DELAYED_WORK(&tp->task, task);
4728 schedule_delayed_work(&tp->task, 4);
4731 static void rtl8169_wait_for_quiescence(struct net_device *dev)
4733 struct rtl8169_private *tp = netdev_priv(dev);
4734 void __iomem *ioaddr = tp->mmio_addr;
4736 synchronize_irq(dev->irq);
4738 /* Wait for any pending NAPI task to complete */
4739 napi_disable(&tp->napi);
4741 rtl8169_irq_mask_and_ack(ioaddr);
4743 tp->intr_mask = 0xffff;
4744 RTL_W16(IntrMask, tp->intr_event);
4745 napi_enable(&tp->napi);
4748 static void rtl8169_reinit_task(struct work_struct *work)
4750 struct rtl8169_private *tp =
4751 container_of(work, struct rtl8169_private, task.work);
4752 struct net_device *dev = tp->dev;
4757 if (!netif_running(dev))
4760 rtl8169_wait_for_quiescence(dev);
4763 ret = rtl8169_open(dev);
4764 if (unlikely(ret < 0)) {
4765 if (net_ratelimit())
4766 netif_err(tp, drv, dev,
4767 "reinit failure (status = %d). Rescheduling\n",
4769 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4776 static void rtl8169_reset_task(struct work_struct *work)
4778 struct rtl8169_private *tp =
4779 container_of(work, struct rtl8169_private, task.work);
4780 struct net_device *dev = tp->dev;
4785 if (!netif_running(dev))
4788 rtl8169_wait_for_quiescence(dev);
4790 for (i = 0; i < NUM_RX_DESC; i++)
4791 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4793 rtl8169_tx_clear(tp);
4795 rtl8169_init_ring_indexes(tp);
4797 netif_wake_queue(dev);
4798 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
4804 static void rtl8169_tx_timeout(struct net_device *dev)
4806 struct rtl8169_private *tp = netdev_priv(dev);
4808 rtl8169_hw_reset(tp);
4810 /* Let's wait a bit while any (async) irq lands on */
4811 rtl8169_schedule_work(dev, rtl8169_reset_task);
4814 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4817 struct skb_shared_info *info = skb_shinfo(skb);
4818 unsigned int cur_frag, entry;
4819 struct TxDesc * uninitialized_var(txd);
4820 struct device *d = &tp->pci_dev->dev;
4823 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4824 skb_frag_t *frag = info->frags + cur_frag;
4829 entry = (entry + 1) % NUM_TX_DESC;
4831 txd = tp->TxDescArray + entry;
4833 addr = ((void *) page_address(frag->page)) + frag->page_offset;
4834 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
4835 if (unlikely(dma_mapping_error(d, mapping))) {
4836 if (net_ratelimit())
4837 netif_err(tp, drv, tp->dev,
4838 "Failed to map TX fragments DMA!\n");
4842 /* Anti gcc 2.95.3 bugware (sic) */
4843 status = opts[0] | len |
4844 (RingEnd * !((entry + 1) % NUM_TX_DESC));
4846 txd->opts1 = cpu_to_le32(status);
4847 txd->opts2 = cpu_to_le32(opts[1]);
4848 txd->addr = cpu_to_le64(mapping);
4850 tp->tx_skb[entry].len = len;
4854 tp->tx_skb[entry].skb = skb;
4855 txd->opts1 |= cpu_to_le32(LastFrag);
4861 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4865 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4866 struct sk_buff *skb, u32 *opts)
4868 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
4869 u32 mss = skb_shinfo(skb)->gso_size;
4870 int offset = info->opts_offset;
4874 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4875 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4876 const struct iphdr *ip = ip_hdr(skb);
4878 if (ip->protocol == IPPROTO_TCP)
4879 opts[offset] |= info->checksum.tcp;
4880 else if (ip->protocol == IPPROTO_UDP)
4881 opts[offset] |= info->checksum.udp;
4887 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4888 struct net_device *dev)
4890 struct rtl8169_private *tp = netdev_priv(dev);
4891 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
4892 struct TxDesc *txd = tp->TxDescArray + entry;
4893 void __iomem *ioaddr = tp->mmio_addr;
4894 struct device *d = &tp->pci_dev->dev;
4900 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
4901 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
4905 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
4908 len = skb_headlen(skb);
4909 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
4910 if (unlikely(dma_mapping_error(d, mapping))) {
4911 if (net_ratelimit())
4912 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
4916 tp->tx_skb[entry].len = len;
4917 txd->addr = cpu_to_le64(mapping);
4919 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4922 rtl8169_tso_csum(tp, skb, opts);
4924 frags = rtl8169_xmit_frags(tp, skb, opts);
4928 opts[0] |= FirstFrag;
4930 opts[0] |= FirstFrag | LastFrag;
4931 tp->tx_skb[entry].skb = skb;
4934 txd->opts2 = cpu_to_le32(opts[1]);
4938 /* Anti gcc 2.95.3 bugware (sic) */
4939 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4940 txd->opts1 = cpu_to_le32(status);
4942 tp->cur_tx += frags + 1;
4946 RTL_W8(TxPoll, NPQ);
4948 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4949 netif_stop_queue(dev);
4951 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4952 netif_wake_queue(dev);
4955 return NETDEV_TX_OK;
4958 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
4961 dev->stats.tx_dropped++;
4962 return NETDEV_TX_OK;
4965 netif_stop_queue(dev);
4966 dev->stats.tx_dropped++;
4967 return NETDEV_TX_BUSY;
4970 static void rtl8169_pcierr_interrupt(struct net_device *dev)
4972 struct rtl8169_private *tp = netdev_priv(dev);
4973 struct pci_dev *pdev = tp->pci_dev;
4974 u16 pci_status, pci_cmd;
4976 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4977 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4979 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4980 pci_cmd, pci_status);
4983 * The recovery sequence below admits a very elaborated explanation:
4984 * - it seems to work;
4985 * - I did not see what else could be done;
4986 * - it makes iop3xx happy.
4988 * Feel free to adjust to your needs.
4990 if (pdev->broken_parity_status)
4991 pci_cmd &= ~PCI_COMMAND_PARITY;
4993 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4995 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
4997 pci_write_config_word(pdev, PCI_STATUS,
4998 pci_status & (PCI_STATUS_DETECTED_PARITY |
4999 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5000 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5002 /* The infamous DAC f*ckup only happens at boot time */
5003 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
5004 void __iomem *ioaddr = tp->mmio_addr;
5006 netif_info(tp, intr, dev, "disabling PCI DAC\n");
5007 tp->cp_cmd &= ~PCIDAC;
5008 RTL_W16(CPlusCmd, tp->cp_cmd);
5009 dev->features &= ~NETIF_F_HIGHDMA;
5012 rtl8169_hw_reset(tp);
5014 rtl8169_schedule_work(dev, rtl8169_reinit_task);
5017 static void rtl8169_tx_interrupt(struct net_device *dev,
5018 struct rtl8169_private *tp,
5019 void __iomem *ioaddr)
5021 unsigned int dirty_tx, tx_left;
5023 dirty_tx = tp->dirty_tx;
5025 tx_left = tp->cur_tx - dirty_tx;
5027 while (tx_left > 0) {
5028 unsigned int entry = dirty_tx % NUM_TX_DESC;
5029 struct ring_info *tx_skb = tp->tx_skb + entry;
5033 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5034 if (status & DescOwn)
5037 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5038 tp->TxDescArray + entry);
5039 if (status & LastFrag) {
5040 dev->stats.tx_packets++;
5041 dev->stats.tx_bytes += tx_skb->skb->len;
5042 dev_kfree_skb(tx_skb->skb);
5049 if (tp->dirty_tx != dirty_tx) {
5050 tp->dirty_tx = dirty_tx;
5052 if (netif_queue_stopped(dev) &&
5053 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5054 netif_wake_queue(dev);
5057 * 8168 hack: TxPoll requests are lost when the Tx packets are
5058 * too close. Let's kick an extra TxPoll request when a burst
5059 * of start_xmit activity is detected (if it is not detected,
5060 * it is slow enough). -- FR
5063 if (tp->cur_tx != dirty_tx)
5064 RTL_W8(TxPoll, NPQ);
5068 static inline int rtl8169_fragmented_frame(u32 status)
5070 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5073 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
5075 u32 status = opts1 & RxProtoMask;
5077 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
5078 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5079 skb->ip_summed = CHECKSUM_UNNECESSARY;
5081 skb_checksum_none_assert(skb);
5084 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5085 struct rtl8169_private *tp,
5089 struct sk_buff *skb;
5090 struct device *d = &tp->pci_dev->dev;
5092 data = rtl8169_align(data);
5093 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
5095 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5097 memcpy(skb->data, data, pkt_size);
5098 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5103 static int rtl8169_rx_interrupt(struct net_device *dev,
5104 struct rtl8169_private *tp,
5105 void __iomem *ioaddr, u32 budget)
5107 unsigned int cur_rx, rx_left;
5110 cur_rx = tp->cur_rx;
5111 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
5112 rx_left = min(rx_left, budget);
5114 for (; rx_left > 0; rx_left--, cur_rx++) {
5115 unsigned int entry = cur_rx % NUM_RX_DESC;
5116 struct RxDesc *desc = tp->RxDescArray + entry;
5120 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
5122 if (status & DescOwn)
5124 if (unlikely(status & RxRES)) {
5125 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5127 dev->stats.rx_errors++;
5128 if (status & (RxRWT | RxRUNT))
5129 dev->stats.rx_length_errors++;
5131 dev->stats.rx_crc_errors++;
5132 if (status & RxFOVF) {
5133 rtl8169_schedule_work(dev, rtl8169_reset_task);
5134 dev->stats.rx_fifo_errors++;
5136 rtl8169_mark_to_asic(desc, rx_buf_sz);
5138 struct sk_buff *skb;
5139 dma_addr_t addr = le64_to_cpu(desc->addr);
5140 int pkt_size = (status & 0x00003fff) - 4;
5143 * The driver does not support incoming fragmented
5144 * frames. They are seen as a symptom of over-mtu
5147 if (unlikely(rtl8169_fragmented_frame(status))) {
5148 dev->stats.rx_dropped++;
5149 dev->stats.rx_length_errors++;
5150 rtl8169_mark_to_asic(desc, rx_buf_sz);
5154 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5155 tp, pkt_size, addr);
5156 rtl8169_mark_to_asic(desc, rx_buf_sz);
5158 dev->stats.rx_dropped++;
5162 rtl8169_rx_csum(skb, status);
5163 skb_put(skb, pkt_size);
5164 skb->protocol = eth_type_trans(skb, dev);
5166 rtl8169_rx_vlan_tag(desc, skb);
5168 napi_gro_receive(&tp->napi, skb);
5170 dev->stats.rx_bytes += pkt_size;
5171 dev->stats.rx_packets++;
5174 /* Work around for AMD plateform. */
5175 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
5176 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5182 count = cur_rx - tp->cur_rx;
5183 tp->cur_rx = cur_rx;
5185 tp->dirty_rx += count;
5190 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5192 struct net_device *dev = dev_instance;
5193 struct rtl8169_private *tp = netdev_priv(dev);
5194 void __iomem *ioaddr = tp->mmio_addr;
5198 /* loop handling interrupts until we have no new ones or
5199 * we hit a invalid/hotplug case.
5201 status = RTL_R16(IntrStatus);
5202 while (status && status != 0xffff) {
5205 /* Handle all of the error cases first. These will reset
5206 * the chip, so just exit the loop.
5208 if (unlikely(!netif_running(dev))) {
5209 rtl8169_asic_down(ioaddr);
5213 if (unlikely(status & RxFIFOOver)) {
5214 switch (tp->mac_version) {
5215 /* Work around for rx fifo overflow */
5216 case RTL_GIGA_MAC_VER_11:
5217 case RTL_GIGA_MAC_VER_22:
5218 case RTL_GIGA_MAC_VER_26:
5219 netif_stop_queue(dev);
5220 rtl8169_tx_timeout(dev);
5222 /* Testers needed. */
5223 case RTL_GIGA_MAC_VER_17:
5224 case RTL_GIGA_MAC_VER_19:
5225 case RTL_GIGA_MAC_VER_20:
5226 case RTL_GIGA_MAC_VER_21:
5227 case RTL_GIGA_MAC_VER_23:
5228 case RTL_GIGA_MAC_VER_24:
5229 case RTL_GIGA_MAC_VER_27:
5230 case RTL_GIGA_MAC_VER_28:
5231 case RTL_GIGA_MAC_VER_31:
5232 /* Experimental science. Pktgen proof. */
5233 case RTL_GIGA_MAC_VER_12:
5234 case RTL_GIGA_MAC_VER_25:
5235 if (status == RxFIFOOver)
5243 if (unlikely(status & SYSErr)) {
5244 rtl8169_pcierr_interrupt(dev);
5248 if (status & LinkChg)
5249 __rtl8169_check_link_status(dev, tp, ioaddr, true);
5251 /* We need to see the lastest version of tp->intr_mask to
5252 * avoid ignoring an MSI interrupt and having to wait for
5253 * another event which may never come.
5256 if (status & tp->intr_mask & tp->napi_event) {
5257 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5258 tp->intr_mask = ~tp->napi_event;
5260 if (likely(napi_schedule_prep(&tp->napi)))
5261 __napi_schedule(&tp->napi);
5263 netif_info(tp, intr, dev,
5264 "interrupt %04x in poll\n", status);
5267 /* We only get a new MSI interrupt when all active irq
5268 * sources on the chip have been acknowledged. So, ack
5269 * everything we've seen and check if new sources have become
5270 * active to avoid blocking all interrupts from the chip.
5273 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5274 status = RTL_R16(IntrStatus);
5277 return IRQ_RETVAL(handled);
5280 static int rtl8169_poll(struct napi_struct *napi, int budget)
5282 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5283 struct net_device *dev = tp->dev;
5284 void __iomem *ioaddr = tp->mmio_addr;
5287 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
5288 rtl8169_tx_interrupt(dev, tp, ioaddr);
5290 if (work_done < budget) {
5291 napi_complete(napi);
5293 /* We need for force the visibility of tp->intr_mask
5294 * for other CPUs, as we can loose an MSI interrupt
5295 * and potentially wait for a retransmit timeout if we don't.
5296 * The posted write to IntrMask is safe, as it will
5297 * eventually make it to the chip and we won't loose anything
5300 tp->intr_mask = 0xffff;
5302 RTL_W16(IntrMask, tp->intr_event);
5308 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5310 struct rtl8169_private *tp = netdev_priv(dev);
5312 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5315 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5316 RTL_W32(RxMissed, 0);
5319 static void rtl8169_down(struct net_device *dev)
5321 struct rtl8169_private *tp = netdev_priv(dev);
5322 void __iomem *ioaddr = tp->mmio_addr;
5324 del_timer_sync(&tp->timer);
5326 netif_stop_queue(dev);
5328 napi_disable(&tp->napi);
5330 spin_lock_irq(&tp->lock);
5332 rtl8169_asic_down(ioaddr);
5334 * At this point device interrupts can not be enabled in any function,
5335 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5336 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5338 rtl8169_rx_missed(dev, ioaddr);
5340 spin_unlock_irq(&tp->lock);
5342 synchronize_irq(dev->irq);
5344 /* Give a racing hard_start_xmit a few cycles to complete. */
5345 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
5347 rtl8169_tx_clear(tp);
5349 rtl8169_rx_clear(tp);
5351 rtl_pll_power_down(tp);
5354 static int rtl8169_close(struct net_device *dev)
5356 struct rtl8169_private *tp = netdev_priv(dev);
5357 struct pci_dev *pdev = tp->pci_dev;
5359 pm_runtime_get_sync(&pdev->dev);
5361 /* Update counters before going down */
5362 rtl8169_update_counters(dev);
5366 free_irq(dev->irq, dev);
5368 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5370 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5372 tp->TxDescArray = NULL;
5373 tp->RxDescArray = NULL;
5375 pm_runtime_put_sync(&pdev->dev);
5380 static void rtl_set_rx_mode(struct net_device *dev)
5382 struct rtl8169_private *tp = netdev_priv(dev);
5383 void __iomem *ioaddr = tp->mmio_addr;
5384 unsigned long flags;
5385 u32 mc_filter[2]; /* Multicast hash filter */
5389 if (dev->flags & IFF_PROMISC) {
5390 /* Unconditionally log net taps. */
5391 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5393 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5395 mc_filter[1] = mc_filter[0] = 0xffffffff;
5396 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5397 (dev->flags & IFF_ALLMULTI)) {
5398 /* Too many to filter perfectly -- accept all multicasts. */
5399 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5400 mc_filter[1] = mc_filter[0] = 0xffffffff;
5402 struct netdev_hw_addr *ha;
5404 rx_mode = AcceptBroadcast | AcceptMyPhys;
5405 mc_filter[1] = mc_filter[0] = 0;
5406 netdev_for_each_mc_addr(ha, dev) {
5407 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5408 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5409 rx_mode |= AcceptMulticast;
5413 spin_lock_irqsave(&tp->lock, flags);
5415 tmp = rtl8169_rx_config | rx_mode |
5416 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
5418 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5419 u32 data = mc_filter[0];
5421 mc_filter[0] = swab32(mc_filter[1]);
5422 mc_filter[1] = swab32(data);
5425 RTL_W32(MAR0 + 4, mc_filter[1]);
5426 RTL_W32(MAR0 + 0, mc_filter[0]);
5428 RTL_W32(RxConfig, tmp);
5430 spin_unlock_irqrestore(&tp->lock, flags);
5434 * rtl8169_get_stats - Get rtl8169 read/write statistics
5435 * @dev: The Ethernet Device to get statistics for
5437 * Get TX/RX statistics for rtl8169
5439 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5441 struct rtl8169_private *tp = netdev_priv(dev);
5442 void __iomem *ioaddr = tp->mmio_addr;
5443 unsigned long flags;
5445 if (netif_running(dev)) {
5446 spin_lock_irqsave(&tp->lock, flags);
5447 rtl8169_rx_missed(dev, ioaddr);
5448 spin_unlock_irqrestore(&tp->lock, flags);
5454 static void rtl8169_net_suspend(struct net_device *dev)
5456 struct rtl8169_private *tp = netdev_priv(dev);
5458 if (!netif_running(dev))
5461 rtl_pll_power_down(tp);
5463 netif_device_detach(dev);
5464 netif_stop_queue(dev);
5469 static int rtl8169_suspend(struct device *device)
5471 struct pci_dev *pdev = to_pci_dev(device);
5472 struct net_device *dev = pci_get_drvdata(pdev);
5474 rtl8169_net_suspend(dev);
5479 static void __rtl8169_resume(struct net_device *dev)
5481 struct rtl8169_private *tp = netdev_priv(dev);
5483 netif_device_attach(dev);
5485 rtl_pll_power_up(tp);
5487 rtl8169_schedule_work(dev, rtl8169_reset_task);
5490 static int rtl8169_resume(struct device *device)
5492 struct pci_dev *pdev = to_pci_dev(device);
5493 struct net_device *dev = pci_get_drvdata(pdev);
5494 struct rtl8169_private *tp = netdev_priv(dev);
5496 rtl8169_init_phy(dev, tp);
5498 if (netif_running(dev))
5499 __rtl8169_resume(dev);
5504 static int rtl8169_runtime_suspend(struct device *device)
5506 struct pci_dev *pdev = to_pci_dev(device);
5507 struct net_device *dev = pci_get_drvdata(pdev);
5508 struct rtl8169_private *tp = netdev_priv(dev);
5510 if (!tp->TxDescArray)
5513 spin_lock_irq(&tp->lock);
5514 tp->saved_wolopts = __rtl8169_get_wol(tp);
5515 __rtl8169_set_wol(tp, WAKE_ANY);
5516 spin_unlock_irq(&tp->lock);
5518 rtl8169_net_suspend(dev);
5523 static int rtl8169_runtime_resume(struct device *device)
5525 struct pci_dev *pdev = to_pci_dev(device);
5526 struct net_device *dev = pci_get_drvdata(pdev);
5527 struct rtl8169_private *tp = netdev_priv(dev);
5529 if (!tp->TxDescArray)
5532 spin_lock_irq(&tp->lock);
5533 __rtl8169_set_wol(tp, tp->saved_wolopts);
5534 tp->saved_wolopts = 0;
5535 spin_unlock_irq(&tp->lock);
5537 rtl8169_init_phy(dev, tp);
5539 __rtl8169_resume(dev);
5544 static int rtl8169_runtime_idle(struct device *device)
5546 struct pci_dev *pdev = to_pci_dev(device);
5547 struct net_device *dev = pci_get_drvdata(pdev);
5548 struct rtl8169_private *tp = netdev_priv(dev);
5550 return tp->TxDescArray ? -EBUSY : 0;
5553 static const struct dev_pm_ops rtl8169_pm_ops = {
5554 .suspend = rtl8169_suspend,
5555 .resume = rtl8169_resume,
5556 .freeze = rtl8169_suspend,
5557 .thaw = rtl8169_resume,
5558 .poweroff = rtl8169_suspend,
5559 .restore = rtl8169_resume,
5560 .runtime_suspend = rtl8169_runtime_suspend,
5561 .runtime_resume = rtl8169_runtime_resume,
5562 .runtime_idle = rtl8169_runtime_idle,
5565 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
5567 #else /* !CONFIG_PM */
5569 #define RTL8169_PM_OPS NULL
5571 #endif /* !CONFIG_PM */
5573 static void rtl_shutdown(struct pci_dev *pdev)
5575 struct net_device *dev = pci_get_drvdata(pdev);
5576 struct rtl8169_private *tp = netdev_priv(dev);
5577 void __iomem *ioaddr = tp->mmio_addr;
5579 rtl8169_net_suspend(dev);
5581 /* Restore original MAC address */
5582 rtl_rar_set(tp, dev->perm_addr);
5584 spin_lock_irq(&tp->lock);
5586 rtl8169_asic_down(ioaddr);
5588 spin_unlock_irq(&tp->lock);
5590 if (system_state == SYSTEM_POWER_OFF) {
5591 /* WoL fails with 8168b when the receiver is disabled. */
5592 if ((tp->mac_version == RTL_GIGA_MAC_VER_11 ||
5593 tp->mac_version == RTL_GIGA_MAC_VER_12 ||
5594 tp->mac_version == RTL_GIGA_MAC_VER_17) &&
5595 (tp->features & RTL_FEATURE_WOL)) {
5596 pci_clear_master(pdev);
5598 RTL_W8(ChipCmd, CmdRxEnb);
5603 pci_wake_from_d3(pdev, true);
5604 pci_set_power_state(pdev, PCI_D3hot);
5608 static struct pci_driver rtl8169_pci_driver = {
5610 .id_table = rtl8169_pci_tbl,
5611 .probe = rtl8169_init_one,
5612 .remove = __devexit_p(rtl8169_remove_one),
5613 .shutdown = rtl_shutdown,
5614 .driver.pm = RTL8169_PM_OPS,
5617 static int __init rtl8169_init_module(void)
5619 return pci_register_driver(&rtl8169_pci_driver);
5622 static void __exit rtl8169_cleanup_module(void)
5624 pci_unregister_driver(&rtl8169_pci_driver);
5627 module_init(rtl8169_init_module);
5628 module_exit(rtl8169_cleanup_module);