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>
30 #include <asm/system.h>
34 #define RTL8169_VERSION "2.3LK-NAPI"
35 #define MODULENAME "r8169"
36 #define PFX MODULENAME ": "
38 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
40 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
43 #define assert(expr) \
45 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
46 #expr,__FILE__,__func__,__LINE__); \
48 #define dprintk(fmt, args...) \
49 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
51 #define assert(expr) do {} while (0)
52 #define dprintk(fmt, args...) do {} while (0)
53 #endif /* RTL8169_DEBUG */
55 #define R8169_MSG_DEFAULT \
56 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
58 #define TX_BUFFS_AVAIL(tp) \
59 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
61 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
62 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
63 static const int multicast_filter_limit = 32;
65 /* MAC address length */
66 #define MAC_ADDR_LEN 6
68 #define MAX_READ_REQUEST_SHIFT 12
69 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
70 #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
71 #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
72 #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
73 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
75 #define R8169_REGS_SIZE 256
76 #define R8169_NAPI_WEIGHT 64
77 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
78 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
79 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
80 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
81 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
83 #define RTL8169_TX_TIMEOUT (6*HZ)
84 #define RTL8169_PHY_TIMEOUT (10*HZ)
86 #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
87 #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
88 #define RTL_EEPROM_SIG_ADDR 0x0000
90 /* write/read MMIO register */
91 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
92 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
93 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
94 #define RTL_R8(reg) readb (ioaddr + (reg))
95 #define RTL_R16(reg) readw (ioaddr + (reg))
96 #define RTL_R32(reg) readl (ioaddr + (reg))
99 RTL_GIGA_MAC_NONE = 0x00,
100 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
101 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
102 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
103 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
104 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
105 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
106 RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
107 RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
108 RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
109 RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
110 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
111 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
112 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
113 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
114 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
115 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
116 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
117 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
118 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
119 RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
120 RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
121 RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
122 RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
123 RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
124 RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
125 RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
126 RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
127 RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
128 RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
129 RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
130 RTL_GIGA_MAC_VER_31 = 0x1f, // 8168DP
133 #define _R(NAME,MAC,MASK) \
134 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
136 static const struct {
139 u32 RxConfigMask; /* Clears the bits supported by this chip */
140 } rtl_chip_info[] = {
141 _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
142 _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
143 _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
144 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
145 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
146 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
147 _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
148 _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
149 _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
150 _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
151 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
152 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
153 _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
154 _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
155 _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
156 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
157 _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
158 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
159 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
160 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
161 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
162 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
163 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
164 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
165 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880), // PCI-E
166 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, 0xff7e1880), // PCI-E
167 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, 0xff7e1880), // PCI-E
168 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, 0xff7e1880), // PCI-E
169 _R("RTL8105e", RTL_GIGA_MAC_VER_29, 0xff7e1880), // PCI-E
170 _R("RTL8105e", RTL_GIGA_MAC_VER_30, 0xff7e1880), // PCI-E
171 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_31, 0xff7e1880) // PCI-E
181 static void rtl_hw_start_8169(struct net_device *);
182 static void rtl_hw_start_8168(struct net_device *);
183 static void rtl_hw_start_8101(struct net_device *);
185 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
186 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
187 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
188 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
189 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
190 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
191 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
192 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
193 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
194 { PCI_VENDOR_ID_LINKSYS, 0x1032,
195 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
197 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
201 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
203 static int rx_buf_sz = 16383;
210 MAC0 = 0, /* Ethernet hardware address. */
212 MAR0 = 8, /* Multicast filter. */
213 CounterAddrLow = 0x10,
214 CounterAddrHigh = 0x14,
215 TxDescStartAddrLow = 0x20,
216 TxDescStartAddrHigh = 0x24,
217 TxHDescStartAddrLow = 0x28,
218 TxHDescStartAddrHigh = 0x2c,
241 RxDescAddrLow = 0xe4,
242 RxDescAddrHigh = 0xe8,
243 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
245 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
247 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
249 #define TxPacketMax (8064 >> 7)
252 FuncEventMask = 0xf4,
253 FuncPresetState = 0xf8,
254 FuncForceEvent = 0xfc,
257 enum rtl8110_registers {
263 enum rtl8168_8101_registers {
266 #define CSIAR_FLAG 0x80000000
267 #define CSIAR_WRITE_CMD 0x80000000
268 #define CSIAR_BYTE_ENABLE 0x0f
269 #define CSIAR_BYTE_ENABLE_SHIFT 12
270 #define CSIAR_ADDR_MASK 0x0fff
273 #define EPHYAR_FLAG 0x80000000
274 #define EPHYAR_WRITE_CMD 0x80000000
275 #define EPHYAR_REG_MASK 0x1f
276 #define EPHYAR_REG_SHIFT 16
277 #define EPHYAR_DATA_MASK 0xffff
279 #define PM_SWITCH (1 << 6)
281 #define FIX_NAK_1 (1 << 4)
282 #define FIX_NAK_2 (1 << 3)
285 #define EN_NDP (1 << 3)
286 #define EN_OOB_RESET (1 << 2)
288 #define EFUSEAR_FLAG 0x80000000
289 #define EFUSEAR_WRITE_CMD 0x80000000
290 #define EFUSEAR_READ_CMD 0x00000000
291 #define EFUSEAR_REG_MASK 0x03ff
292 #define EFUSEAR_REG_SHIFT 8
293 #define EFUSEAR_DATA_MASK 0xff
296 enum rtl8168_registers {
299 #define ERIAR_FLAG 0x80000000
300 #define ERIAR_WRITE_CMD 0x80000000
301 #define ERIAR_READ_CMD 0x00000000
302 #define ERIAR_ADDR_BYTE_ALIGN 4
303 #define ERIAR_EXGMAC 0
306 #define ERIAR_TYPE_SHIFT 16
307 #define ERIAR_BYTEEN 0x0f
308 #define ERIAR_BYTEEN_SHIFT 12
309 EPHY_RXER_NUM = 0x7c,
310 OCPDR = 0xb0, /* OCP GPHY access */
311 #define OCPDR_WRITE_CMD 0x80000000
312 #define OCPDR_READ_CMD 0x00000000
313 #define OCPDR_REG_MASK 0x7f
314 #define OCPDR_GPHY_REG_SHIFT 16
315 #define OCPDR_DATA_MASK 0xffff
317 #define OCPAR_FLAG 0x80000000
318 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
319 #define OCPAR_GPHY_READ_CMD 0x0000f060
320 RDSAR1 = 0xd0 /* 8168c only. Undocumented on 8168dp */
323 enum rtl_register_content {
324 /* InterruptStatusBits */
328 TxDescUnavail = 0x0080,
350 /* TXPoll register p.5 */
351 HPQ = 0x80, /* Poll cmd on the high prio queue */
352 NPQ = 0x40, /* Poll cmd on the low prio queue */
353 FSWInt = 0x01, /* Forced software interrupt */
357 Cfg9346_Unlock = 0xc0,
362 AcceptBroadcast = 0x08,
363 AcceptMulticast = 0x04,
365 AcceptAllPhys = 0x01,
372 TxInterFrameGapShift = 24,
373 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
375 /* Config1 register p.24 */
378 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
379 Speed_down = (1 << 4),
383 PMEnable = (1 << 0), /* Power Management Enable */
385 /* Config2 register p. 25 */
386 PCI_Clock_66MHz = 0x01,
387 PCI_Clock_33MHz = 0x00,
389 /* Config3 register p.25 */
390 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
391 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
392 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
394 /* Config5 register p.27 */
395 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
396 MWF = (1 << 5), /* Accept Multicast wakeup frame */
397 UWF = (1 << 4), /* Accept Unicast wakeup frame */
398 LanWake = (1 << 1), /* LanWake enable/disable */
399 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
402 TBIReset = 0x80000000,
403 TBILoopback = 0x40000000,
404 TBINwEnable = 0x20000000,
405 TBINwRestart = 0x10000000,
406 TBILinkOk = 0x02000000,
407 TBINwComplete = 0x01000000,
410 EnableBist = (1 << 15), // 8168 8101
411 Mac_dbgo_oe = (1 << 14), // 8168 8101
412 Normal_mode = (1 << 13), // unused
413 Force_half_dup = (1 << 12), // 8168 8101
414 Force_rxflow_en = (1 << 11), // 8168 8101
415 Force_txflow_en = (1 << 10), // 8168 8101
416 Cxpl_dbg_sel = (1 << 9), // 8168 8101
417 ASF = (1 << 8), // 8168 8101
418 PktCntrDisable = (1 << 7), // 8168 8101
419 Mac_dbgo_sel = 0x001c, // 8168
424 INTT_0 = 0x0000, // 8168
425 INTT_1 = 0x0001, // 8168
426 INTT_2 = 0x0002, // 8168
427 INTT_3 = 0x0003, // 8168
429 /* rtl8169_PHYstatus */
440 TBILinkOK = 0x02000000,
442 /* DumpCounterCommand */
446 enum desc_status_bit {
447 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
448 RingEnd = (1 << 30), /* End of descriptor ring */
449 FirstFrag = (1 << 29), /* First segment of a packet */
450 LastFrag = (1 << 28), /* Final segment of a packet */
453 LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
454 MSSShift = 16, /* MSS value position */
455 MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
456 IPCS = (1 << 18), /* Calculate IP checksum */
457 UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
458 TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
459 TxVlanTag = (1 << 17), /* Add VLAN tag */
462 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
463 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
465 #define RxProtoUDP (PID1)
466 #define RxProtoTCP (PID0)
467 #define RxProtoIP (PID1 | PID0)
468 #define RxProtoMask RxProtoIP
470 IPFail = (1 << 16), /* IP checksum failed */
471 UDPFail = (1 << 15), /* UDP/IP checksum failed */
472 TCPFail = (1 << 14), /* TCP/IP checksum failed */
473 RxVlanTag = (1 << 16), /* VLAN tag available */
476 #define RsvdMask 0x3fffc000
493 u8 __pad[sizeof(void *) - sizeof(u32)];
497 RTL_FEATURE_WOL = (1 << 0),
498 RTL_FEATURE_MSI = (1 << 1),
499 RTL_FEATURE_GMII = (1 << 2),
502 struct rtl8169_counters {
509 __le32 tx_one_collision;
510 __le32 tx_multi_collision;
518 struct rtl8169_private {
519 void __iomem *mmio_addr; /* memory map physical address */
520 struct pci_dev *pci_dev; /* Index of PCI device */
521 struct net_device *dev;
522 struct napi_struct napi;
523 spinlock_t lock; /* spin lock flag */
527 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
528 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
531 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
532 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
533 dma_addr_t TxPhyAddr;
534 dma_addr_t RxPhyAddr;
535 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
536 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
537 struct timer_list timer;
542 int phy_1000_ctrl_reg;
545 void (*write)(void __iomem *, int, int);
546 int (*read)(void __iomem *, int);
549 struct pll_power_ops {
550 void (*down)(struct rtl8169_private *);
551 void (*up)(struct rtl8169_private *);
554 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
555 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
556 void (*phy_reset_enable)(struct rtl8169_private *tp);
557 void (*hw_start)(struct net_device *);
558 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
559 unsigned int (*link_ok)(void __iomem *);
560 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
562 struct delayed_work task;
565 struct mii_if_info mii;
566 struct rtl8169_counters counters;
569 const struct firmware *fw;
572 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
573 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
574 module_param(use_dac, int, 0);
575 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
576 module_param_named(debug, debug.msg_enable, int, 0);
577 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
578 MODULE_LICENSE("GPL");
579 MODULE_VERSION(RTL8169_VERSION);
580 MODULE_FIRMWARE(FIRMWARE_8168D_1);
581 MODULE_FIRMWARE(FIRMWARE_8168D_2);
582 MODULE_FIRMWARE(FIRMWARE_8105E_1);
584 static int rtl8169_open(struct net_device *dev);
585 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
586 struct net_device *dev);
587 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
588 static int rtl8169_init_ring(struct net_device *dev);
589 static void rtl_hw_start(struct net_device *dev);
590 static int rtl8169_close(struct net_device *dev);
591 static void rtl_set_rx_mode(struct net_device *dev);
592 static void rtl8169_tx_timeout(struct net_device *dev);
593 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
594 static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
595 void __iomem *, u32 budget);
596 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
597 static void rtl8169_down(struct net_device *dev);
598 static void rtl8169_rx_clear(struct rtl8169_private *tp);
599 static int rtl8169_poll(struct napi_struct *napi, int budget);
601 static const unsigned int rtl8169_rx_config =
602 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
604 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
606 void __iomem *ioaddr = tp->mmio_addr;
609 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
610 for (i = 0; i < 20; i++) {
612 if (RTL_R32(OCPAR) & OCPAR_FLAG)
615 return RTL_R32(OCPDR);
618 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
620 void __iomem *ioaddr = tp->mmio_addr;
623 RTL_W32(OCPDR, data);
624 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
625 for (i = 0; i < 20; i++) {
627 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
632 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
634 void __iomem *ioaddr = tp->mmio_addr;
638 RTL_W32(ERIAR, 0x800010e8);
640 for (i = 0; i < 5; i++) {
642 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
646 ocp_write(tp, 0x1, 0x30, 0x00000001);
649 #define OOB_CMD_RESET 0x00
650 #define OOB_CMD_DRIVER_START 0x05
651 #define OOB_CMD_DRIVER_STOP 0x06
653 static void rtl8168_driver_start(struct rtl8169_private *tp)
658 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
660 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
665 for (i = 0; i < 10; i++) {
667 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
672 static void rtl8168_driver_stop(struct rtl8169_private *tp)
677 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
679 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
684 for (i = 0; i < 10; i++) {
686 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
691 static int r8168dp_check_dash(struct rtl8169_private *tp)
695 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
700 if (ocp_read(tp, 0xF, reg) & 0x00008000)
706 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
710 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
712 for (i = 20; i > 0; i--) {
714 * Check if the RTL8169 has completed writing to the specified
717 if (!(RTL_R32(PHYAR) & 0x80000000))
722 * According to hardware specs a 20us delay is required after write
723 * complete indication, but before sending next command.
728 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
732 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
734 for (i = 20; i > 0; i--) {
736 * Check if the RTL8169 has completed retrieving data from
737 * the specified MII register.
739 if (RTL_R32(PHYAR) & 0x80000000) {
740 value = RTL_R32(PHYAR) & 0xffff;
746 * According to hardware specs a 20us delay is required after read
747 * complete indication, but before sending next command.
754 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
758 RTL_W32(OCPDR, data |
759 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
760 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
761 RTL_W32(EPHY_RXER_NUM, 0);
763 for (i = 0; i < 100; i++) {
765 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
770 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
772 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
773 (value & OCPDR_DATA_MASK));
776 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
780 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
783 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
784 RTL_W32(EPHY_RXER_NUM, 0);
786 for (i = 0; i < 100; i++) {
788 if (RTL_R32(OCPAR) & OCPAR_FLAG)
792 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
795 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
797 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
799 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
802 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
804 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
807 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
809 r8168dp_2_mdio_start(ioaddr);
811 r8169_mdio_write(ioaddr, reg_addr, value);
813 r8168dp_2_mdio_stop(ioaddr);
816 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
820 r8168dp_2_mdio_start(ioaddr);
822 value = r8169_mdio_read(ioaddr, reg_addr);
824 r8168dp_2_mdio_stop(ioaddr);
829 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
831 tp->mdio_ops.write(tp->mmio_addr, location, val);
834 static int rtl_readphy(struct rtl8169_private *tp, int location)
836 return tp->mdio_ops.read(tp->mmio_addr, location);
839 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
841 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
844 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
848 val = rtl_readphy(tp, reg_addr);
849 rtl_writephy(tp, reg_addr, (val | p) & ~m);
852 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
855 struct rtl8169_private *tp = netdev_priv(dev);
857 rtl_writephy(tp, location, val);
860 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
862 struct rtl8169_private *tp = netdev_priv(dev);
864 return rtl_readphy(tp, location);
867 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
871 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
872 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
874 for (i = 0; i < 100; i++) {
875 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
881 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
886 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
888 for (i = 0; i < 100; i++) {
889 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
890 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
899 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
903 RTL_W32(CSIDR, value);
904 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
905 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
907 for (i = 0; i < 100; i++) {
908 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
914 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
919 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
920 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
922 for (i = 0; i < 100; i++) {
923 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
924 value = RTL_R32(CSIDR);
933 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
938 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
940 for (i = 0; i < 300; i++) {
941 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
942 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
951 static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
953 RTL_W16(IntrMask, 0x0000);
955 RTL_W16(IntrStatus, 0xffff);
958 static void rtl8169_asic_down(void __iomem *ioaddr)
960 RTL_W8(ChipCmd, 0x00);
961 rtl8169_irq_mask_and_ack(ioaddr);
965 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
967 void __iomem *ioaddr = tp->mmio_addr;
969 return RTL_R32(TBICSR) & TBIReset;
972 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
974 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
977 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
979 return RTL_R32(TBICSR) & TBILinkOk;
982 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
984 return RTL_R8(PHYstatus) & LinkStatus;
987 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
989 void __iomem *ioaddr = tp->mmio_addr;
991 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
994 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
998 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
999 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1002 static void __rtl8169_check_link_status(struct net_device *dev,
1003 struct rtl8169_private *tp,
1004 void __iomem *ioaddr,
1007 unsigned long flags;
1009 spin_lock_irqsave(&tp->lock, flags);
1010 if (tp->link_ok(ioaddr)) {
1011 /* This is to cancel a scheduled suspend if there's one. */
1013 pm_request_resume(&tp->pci_dev->dev);
1014 netif_carrier_on(dev);
1015 if (net_ratelimit())
1016 netif_info(tp, ifup, dev, "link up\n");
1018 netif_carrier_off(dev);
1019 netif_info(tp, ifdown, dev, "link down\n");
1021 pm_schedule_suspend(&tp->pci_dev->dev, 100);
1023 spin_unlock_irqrestore(&tp->lock, flags);
1026 static void rtl8169_check_link_status(struct net_device *dev,
1027 struct rtl8169_private *tp,
1028 void __iomem *ioaddr)
1030 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1033 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1035 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1037 void __iomem *ioaddr = tp->mmio_addr;
1041 options = RTL_R8(Config1);
1042 if (!(options & PMEnable))
1045 options = RTL_R8(Config3);
1046 if (options & LinkUp)
1047 wolopts |= WAKE_PHY;
1048 if (options & MagicPacket)
1049 wolopts |= WAKE_MAGIC;
1051 options = RTL_R8(Config5);
1053 wolopts |= WAKE_UCAST;
1055 wolopts |= WAKE_BCAST;
1057 wolopts |= WAKE_MCAST;
1062 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1064 struct rtl8169_private *tp = netdev_priv(dev);
1066 spin_lock_irq(&tp->lock);
1068 wol->supported = WAKE_ANY;
1069 wol->wolopts = __rtl8169_get_wol(tp);
1071 spin_unlock_irq(&tp->lock);
1074 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1076 void __iomem *ioaddr = tp->mmio_addr;
1078 static const struct {
1083 { WAKE_ANY, Config1, PMEnable },
1084 { WAKE_PHY, Config3, LinkUp },
1085 { WAKE_MAGIC, Config3, MagicPacket },
1086 { WAKE_UCAST, Config5, UWF },
1087 { WAKE_BCAST, Config5, BWF },
1088 { WAKE_MCAST, Config5, MWF },
1089 { WAKE_ANY, Config5, LanWake }
1092 RTL_W8(Cfg9346, Cfg9346_Unlock);
1094 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1095 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1096 if (wolopts & cfg[i].opt)
1097 options |= cfg[i].mask;
1098 RTL_W8(cfg[i].reg, options);
1101 RTL_W8(Cfg9346, Cfg9346_Lock);
1104 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1106 struct rtl8169_private *tp = netdev_priv(dev);
1108 spin_lock_irq(&tp->lock);
1111 tp->features |= RTL_FEATURE_WOL;
1113 tp->features &= ~RTL_FEATURE_WOL;
1114 __rtl8169_set_wol(tp, wol->wolopts);
1115 spin_unlock_irq(&tp->lock);
1117 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1122 static void rtl8169_get_drvinfo(struct net_device *dev,
1123 struct ethtool_drvinfo *info)
1125 struct rtl8169_private *tp = netdev_priv(dev);
1127 strcpy(info->driver, MODULENAME);
1128 strcpy(info->version, RTL8169_VERSION);
1129 strcpy(info->bus_info, pci_name(tp->pci_dev));
1132 static int rtl8169_get_regs_len(struct net_device *dev)
1134 return R8169_REGS_SIZE;
1137 static int rtl8169_set_speed_tbi(struct net_device *dev,
1138 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1140 struct rtl8169_private *tp = netdev_priv(dev);
1141 void __iomem *ioaddr = tp->mmio_addr;
1145 reg = RTL_R32(TBICSR);
1146 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1147 (duplex == DUPLEX_FULL)) {
1148 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1149 } else if (autoneg == AUTONEG_ENABLE)
1150 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1152 netif_warn(tp, link, dev,
1153 "incorrect speed setting refused in TBI mode\n");
1160 static int rtl8169_set_speed_xmii(struct net_device *dev,
1161 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1163 struct rtl8169_private *tp = netdev_priv(dev);
1164 int giga_ctrl, bmcr;
1167 rtl_writephy(tp, 0x1f, 0x0000);
1169 if (autoneg == AUTONEG_ENABLE) {
1172 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1173 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1174 ADVERTISE_100HALF | ADVERTISE_100FULL);
1176 if (adv & ADVERTISED_10baseT_Half)
1177 auto_nego |= ADVERTISE_10HALF;
1178 if (adv & ADVERTISED_10baseT_Full)
1179 auto_nego |= ADVERTISE_10FULL;
1180 if (adv & ADVERTISED_100baseT_Half)
1181 auto_nego |= ADVERTISE_100HALF;
1182 if (adv & ADVERTISED_100baseT_Full)
1183 auto_nego |= ADVERTISE_100FULL;
1185 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1187 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1188 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1190 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1191 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1192 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1193 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1194 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1195 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1196 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1197 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
1198 (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
1199 (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
1200 (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
1201 if (adv & ADVERTISED_1000baseT_Half)
1202 giga_ctrl |= ADVERTISE_1000HALF;
1203 if (adv & ADVERTISED_1000baseT_Full)
1204 giga_ctrl |= ADVERTISE_1000FULL;
1205 } else if (adv & (ADVERTISED_1000baseT_Half |
1206 ADVERTISED_1000baseT_Full)) {
1207 netif_info(tp, link, dev,
1208 "PHY does not support 1000Mbps\n");
1212 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1214 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1215 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1219 if (speed == SPEED_10)
1221 else if (speed == SPEED_100)
1222 bmcr = BMCR_SPEED100;
1226 if (duplex == DUPLEX_FULL)
1227 bmcr |= BMCR_FULLDPLX;
1230 tp->phy_1000_ctrl_reg = giga_ctrl;
1232 rtl_writephy(tp, MII_BMCR, bmcr);
1234 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
1235 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
1236 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1237 rtl_writephy(tp, 0x17, 0x2138);
1238 rtl_writephy(tp, 0x0e, 0x0260);
1240 rtl_writephy(tp, 0x17, 0x2108);
1241 rtl_writephy(tp, 0x0e, 0x0000);
1250 static int rtl8169_set_speed(struct net_device *dev,
1251 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1253 struct rtl8169_private *tp = netdev_priv(dev);
1256 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1258 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
1259 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1264 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1266 struct rtl8169_private *tp = netdev_priv(dev);
1267 unsigned long flags;
1270 spin_lock_irqsave(&tp->lock, flags);
1271 ret = rtl8169_set_speed(dev,
1272 cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
1273 spin_unlock_irqrestore(&tp->lock, flags);
1278 static u32 rtl8169_get_rx_csum(struct net_device *dev)
1280 struct rtl8169_private *tp = netdev_priv(dev);
1282 return tp->cp_cmd & RxChkSum;
1285 static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
1287 struct rtl8169_private *tp = netdev_priv(dev);
1288 void __iomem *ioaddr = tp->mmio_addr;
1289 unsigned long flags;
1291 spin_lock_irqsave(&tp->lock, flags);
1294 tp->cp_cmd |= RxChkSum;
1296 tp->cp_cmd &= ~RxChkSum;
1298 RTL_W16(CPlusCmd, tp->cp_cmd);
1301 spin_unlock_irqrestore(&tp->lock, flags);
1306 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1307 struct sk_buff *skb)
1309 return (vlan_tx_tag_present(skb)) ?
1310 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1313 #define NETIF_F_HW_VLAN_TX_RX (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX)
1315 static void rtl8169_vlan_mode(struct net_device *dev)
1317 struct rtl8169_private *tp = netdev_priv(dev);
1318 void __iomem *ioaddr = tp->mmio_addr;
1319 unsigned long flags;
1321 spin_lock_irqsave(&tp->lock, flags);
1322 if (dev->features & NETIF_F_HW_VLAN_RX)
1323 tp->cp_cmd |= RxVlan;
1325 tp->cp_cmd &= ~RxVlan;
1326 RTL_W16(CPlusCmd, tp->cp_cmd);
1329 spin_unlock_irqrestore(&tp->lock, flags);
1331 dev->vlan_features = dev->features &~ NETIF_F_HW_VLAN_TX_RX;
1334 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1336 u32 opts2 = le32_to_cpu(desc->opts2);
1338 if (opts2 & RxVlanTag)
1339 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1344 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1346 struct rtl8169_private *tp = netdev_priv(dev);
1347 void __iomem *ioaddr = tp->mmio_addr;
1351 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1352 cmd->port = PORT_FIBRE;
1353 cmd->transceiver = XCVR_INTERNAL;
1355 status = RTL_R32(TBICSR);
1356 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1357 cmd->autoneg = !!(status & TBINwEnable);
1359 cmd->speed = SPEED_1000;
1360 cmd->duplex = DUPLEX_FULL; /* Always set */
1365 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1367 struct rtl8169_private *tp = netdev_priv(dev);
1369 return mii_ethtool_gset(&tp->mii, cmd);
1372 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1374 struct rtl8169_private *tp = netdev_priv(dev);
1375 unsigned long flags;
1378 spin_lock_irqsave(&tp->lock, flags);
1380 rc = tp->get_settings(dev, cmd);
1382 spin_unlock_irqrestore(&tp->lock, flags);
1386 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1389 struct rtl8169_private *tp = netdev_priv(dev);
1390 unsigned long flags;
1392 if (regs->len > R8169_REGS_SIZE)
1393 regs->len = R8169_REGS_SIZE;
1395 spin_lock_irqsave(&tp->lock, flags);
1396 memcpy_fromio(p, tp->mmio_addr, regs->len);
1397 spin_unlock_irqrestore(&tp->lock, flags);
1400 static u32 rtl8169_get_msglevel(struct net_device *dev)
1402 struct rtl8169_private *tp = netdev_priv(dev);
1404 return tp->msg_enable;
1407 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1409 struct rtl8169_private *tp = netdev_priv(dev);
1411 tp->msg_enable = value;
1414 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1421 "tx_single_collisions",
1422 "tx_multi_collisions",
1430 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1434 return ARRAY_SIZE(rtl8169_gstrings);
1440 static void rtl8169_update_counters(struct net_device *dev)
1442 struct rtl8169_private *tp = netdev_priv(dev);
1443 void __iomem *ioaddr = tp->mmio_addr;
1444 struct rtl8169_counters *counters;
1448 struct device *d = &tp->pci_dev->dev;
1451 * Some chips are unable to dump tally counters when the receiver
1454 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1457 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1461 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1462 cmd = (u64)paddr & DMA_BIT_MASK(32);
1463 RTL_W32(CounterAddrLow, cmd);
1464 RTL_W32(CounterAddrLow, cmd | CounterDump);
1467 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1468 /* copy updated counters */
1469 memcpy(&tp->counters, counters, sizeof(*counters));
1475 RTL_W32(CounterAddrLow, 0);
1476 RTL_W32(CounterAddrHigh, 0);
1478 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1481 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1482 struct ethtool_stats *stats, u64 *data)
1484 struct rtl8169_private *tp = netdev_priv(dev);
1488 rtl8169_update_counters(dev);
1490 data[0] = le64_to_cpu(tp->counters.tx_packets);
1491 data[1] = le64_to_cpu(tp->counters.rx_packets);
1492 data[2] = le64_to_cpu(tp->counters.tx_errors);
1493 data[3] = le32_to_cpu(tp->counters.rx_errors);
1494 data[4] = le16_to_cpu(tp->counters.rx_missed);
1495 data[5] = le16_to_cpu(tp->counters.align_errors);
1496 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1497 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1498 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1499 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1500 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1501 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1502 data[12] = le16_to_cpu(tp->counters.tx_underun);
1505 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1509 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1514 static int rtl8169_set_flags(struct net_device *dev, u32 data)
1516 struct rtl8169_private *tp = netdev_priv(dev);
1517 unsigned long old_feat = dev->features;
1520 if ((tp->mac_version == RTL_GIGA_MAC_VER_05) &&
1521 !(data & ETH_FLAG_RXVLAN)) {
1522 netif_info(tp, drv, dev, "8110SCd requires hardware Rx VLAN\n");
1526 rc = ethtool_op_set_flags(dev, data, ETH_FLAG_TXVLAN | ETH_FLAG_RXVLAN);
1530 if ((old_feat ^ dev->features) & NETIF_F_HW_VLAN_RX)
1531 rtl8169_vlan_mode(dev);
1536 static const struct ethtool_ops rtl8169_ethtool_ops = {
1537 .get_drvinfo = rtl8169_get_drvinfo,
1538 .get_regs_len = rtl8169_get_regs_len,
1539 .get_link = ethtool_op_get_link,
1540 .get_settings = rtl8169_get_settings,
1541 .set_settings = rtl8169_set_settings,
1542 .get_msglevel = rtl8169_get_msglevel,
1543 .set_msglevel = rtl8169_set_msglevel,
1544 .get_rx_csum = rtl8169_get_rx_csum,
1545 .set_rx_csum = rtl8169_set_rx_csum,
1546 .set_tx_csum = ethtool_op_set_tx_csum,
1547 .set_sg = ethtool_op_set_sg,
1548 .set_tso = ethtool_op_set_tso,
1549 .get_regs = rtl8169_get_regs,
1550 .get_wol = rtl8169_get_wol,
1551 .set_wol = rtl8169_set_wol,
1552 .get_strings = rtl8169_get_strings,
1553 .get_sset_count = rtl8169_get_sset_count,
1554 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1555 .set_flags = rtl8169_set_flags,
1556 .get_flags = ethtool_op_get_flags,
1559 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1560 void __iomem *ioaddr)
1563 * The driver currently handles the 8168Bf and the 8168Be identically
1564 * but they can be identified more specifically through the test below
1567 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1569 * Same thing for the 8101Eb and the 8101Ec:
1571 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1573 static const struct {
1579 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1580 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1581 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1583 /* 8168DP family. */
1584 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1585 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1586 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
1589 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1590 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1591 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1592 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1593 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1594 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1595 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1596 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1597 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1600 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1601 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1602 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1603 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1606 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
1607 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1608 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1609 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1610 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1611 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1612 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1613 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1614 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1615 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1616 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1617 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1618 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1619 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1620 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1621 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1622 /* FIXME: where did these entries come from ? -- FR */
1623 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1624 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1627 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1628 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1629 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1630 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1631 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1632 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1635 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1639 reg = RTL_R32(TxConfig);
1640 while ((reg & p->mask) != p->val)
1642 tp->mac_version = p->mac_version;
1645 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1647 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1655 static void rtl_writephy_batch(struct rtl8169_private *tp,
1656 const struct phy_reg *regs, int len)
1659 rtl_writephy(tp, regs->reg, regs->val);
1664 #define PHY_READ 0x00000000
1665 #define PHY_DATA_OR 0x10000000
1666 #define PHY_DATA_AND 0x20000000
1667 #define PHY_BJMPN 0x30000000
1668 #define PHY_READ_EFUSE 0x40000000
1669 #define PHY_READ_MAC_BYTE 0x50000000
1670 #define PHY_WRITE_MAC_BYTE 0x60000000
1671 #define PHY_CLEAR_READCOUNT 0x70000000
1672 #define PHY_WRITE 0x80000000
1673 #define PHY_READCOUNT_EQ_SKIP 0x90000000
1674 #define PHY_COMP_EQ_SKIPN 0xa0000000
1675 #define PHY_COMP_NEQ_SKIPN 0xb0000000
1676 #define PHY_WRITE_PREVIOUS 0xc0000000
1677 #define PHY_SKIPN 0xd0000000
1678 #define PHY_DELAY_MS 0xe0000000
1679 #define PHY_WRITE_ERI_WORD 0xf0000000
1682 rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1684 __le32 *phytable = (__le32 *)fw->data;
1685 struct net_device *dev = tp->dev;
1686 size_t index, fw_size = fw->size / sizeof(*phytable);
1689 if (fw->size % sizeof(*phytable)) {
1690 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1694 for (index = 0; index < fw_size; index++) {
1695 u32 action = le32_to_cpu(phytable[index]);
1696 u32 regno = (action & 0x0fff0000) >> 16;
1698 switch(action & 0xf0000000) {
1702 case PHY_READ_EFUSE:
1703 case PHY_CLEAR_READCOUNT:
1705 case PHY_WRITE_PREVIOUS:
1710 if (regno > index) {
1711 netif_err(tp, probe, tp->dev,
1712 "Out of range of firmware\n");
1716 case PHY_READCOUNT_EQ_SKIP:
1717 if (index + 2 >= fw_size) {
1718 netif_err(tp, probe, tp->dev,
1719 "Out of range of firmware\n");
1723 case PHY_COMP_EQ_SKIPN:
1724 case PHY_COMP_NEQ_SKIPN:
1726 if (index + 1 + regno >= fw_size) {
1727 netif_err(tp, probe, tp->dev,
1728 "Out of range of firmware\n");
1733 case PHY_READ_MAC_BYTE:
1734 case PHY_WRITE_MAC_BYTE:
1735 case PHY_WRITE_ERI_WORD:
1737 netif_err(tp, probe, tp->dev,
1738 "Invalid action 0x%08x\n", action);
1746 for (index = 0; index < fw_size; ) {
1747 u32 action = le32_to_cpu(phytable[index]);
1748 u32 data = action & 0x0000ffff;
1749 u32 regno = (action & 0x0fff0000) >> 16;
1754 switch(action & 0xf0000000) {
1756 predata = rtl_readphy(tp, regno);
1771 case PHY_READ_EFUSE:
1772 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1775 case PHY_CLEAR_READCOUNT:
1780 rtl_writephy(tp, regno, data);
1783 case PHY_READCOUNT_EQ_SKIP:
1789 case PHY_COMP_EQ_SKIPN:
1790 if (predata == data)
1794 case PHY_COMP_NEQ_SKIPN:
1795 if (predata != data)
1799 case PHY_WRITE_PREVIOUS:
1800 rtl_writephy(tp, regno, predata);
1811 case PHY_READ_MAC_BYTE:
1812 case PHY_WRITE_MAC_BYTE:
1813 case PHY_WRITE_ERI_WORD:
1820 static void rtl_release_firmware(struct rtl8169_private *tp)
1822 release_firmware(tp->fw);
1826 static int rtl_apply_firmware(struct rtl8169_private *tp, const char *fw_name)
1828 const struct firmware **fw = &tp->fw;
1832 rc = request_firmware(fw, fw_name, &tp->pci_dev->dev);
1837 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1838 rtl_phy_write_fw(tp, *fw);
1843 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
1845 static const struct phy_reg phy_reg_init[] = {
1907 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1910 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
1912 static const struct phy_reg phy_reg_init[] = {
1918 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1921 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
1923 struct pci_dev *pdev = tp->pci_dev;
1924 u16 vendor_id, device_id;
1926 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1927 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1929 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1932 rtl_writephy(tp, 0x1f, 0x0001);
1933 rtl_writephy(tp, 0x10, 0xf01b);
1934 rtl_writephy(tp, 0x1f, 0x0000);
1937 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
1939 static const struct phy_reg phy_reg_init[] = {
1979 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1981 rtl8169scd_hw_phy_config_quirk(tp);
1984 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
1986 static const struct phy_reg phy_reg_init[] = {
2034 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2037 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2039 static const struct phy_reg phy_reg_init[] = {
2044 rtl_writephy(tp, 0x1f, 0x0001);
2045 rtl_patchphy(tp, 0x16, 1 << 0);
2047 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2050 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2052 static const struct phy_reg phy_reg_init[] = {
2058 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2061 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2063 static const struct phy_reg phy_reg_init[] = {
2071 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2074 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2076 static const struct phy_reg phy_reg_init[] = {
2082 rtl_writephy(tp, 0x1f, 0x0000);
2083 rtl_patchphy(tp, 0x14, 1 << 5);
2084 rtl_patchphy(tp, 0x0d, 1 << 5);
2086 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2089 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2091 static const struct phy_reg phy_reg_init[] = {
2111 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2113 rtl_patchphy(tp, 0x14, 1 << 5);
2114 rtl_patchphy(tp, 0x0d, 1 << 5);
2115 rtl_writephy(tp, 0x1f, 0x0000);
2118 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2120 static const struct phy_reg phy_reg_init[] = {
2138 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2140 rtl_patchphy(tp, 0x16, 1 << 0);
2141 rtl_patchphy(tp, 0x14, 1 << 5);
2142 rtl_patchphy(tp, 0x0d, 1 << 5);
2143 rtl_writephy(tp, 0x1f, 0x0000);
2146 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2148 static const struct phy_reg phy_reg_init[] = {
2160 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2162 rtl_patchphy(tp, 0x16, 1 << 0);
2163 rtl_patchphy(tp, 0x14, 1 << 5);
2164 rtl_patchphy(tp, 0x0d, 1 << 5);
2165 rtl_writephy(tp, 0x1f, 0x0000);
2168 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2170 rtl8168c_3_hw_phy_config(tp);
2173 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2175 static const struct phy_reg phy_reg_init_0[] = {
2176 /* Channel Estimation */
2197 * enhance line driver power
2206 * Can not link to 1Gbps with bad cable
2207 * Decrease SNR threshold form 21.07dB to 19.04dB
2215 void __iomem *ioaddr = tp->mmio_addr;
2217 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2221 * Fine Tune Switching regulator parameter
2223 rtl_writephy(tp, 0x1f, 0x0002);
2224 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2225 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2227 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2228 static const struct phy_reg phy_reg_init[] = {
2238 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2240 val = rtl_readphy(tp, 0x0d);
2242 if ((val & 0x00ff) != 0x006c) {
2243 static const u32 set[] = {
2244 0x0065, 0x0066, 0x0067, 0x0068,
2245 0x0069, 0x006a, 0x006b, 0x006c
2249 rtl_writephy(tp, 0x1f, 0x0002);
2252 for (i = 0; i < ARRAY_SIZE(set); i++)
2253 rtl_writephy(tp, 0x0d, val | set[i]);
2256 static const struct phy_reg phy_reg_init[] = {
2264 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2267 /* RSET couple improve */
2268 rtl_writephy(tp, 0x1f, 0x0002);
2269 rtl_patchphy(tp, 0x0d, 0x0300);
2270 rtl_patchphy(tp, 0x0f, 0x0010);
2272 /* Fine tune PLL performance */
2273 rtl_writephy(tp, 0x1f, 0x0002);
2274 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2275 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2277 rtl_writephy(tp, 0x1f, 0x0005);
2278 rtl_writephy(tp, 0x05, 0x001b);
2279 if ((rtl_readphy(tp, 0x06) != 0xbf00) ||
2280 (rtl_apply_firmware(tp, FIRMWARE_8168D_1) < 0)) {
2281 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2284 rtl_writephy(tp, 0x1f, 0x0000);
2287 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2289 static const struct phy_reg phy_reg_init_0[] = {
2290 /* Channel Estimation */
2311 * enhance line driver power
2320 * Can not link to 1Gbps with bad cable
2321 * Decrease SNR threshold form 21.07dB to 19.04dB
2329 void __iomem *ioaddr = tp->mmio_addr;
2331 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2333 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2334 static const struct phy_reg phy_reg_init[] = {
2345 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2347 val = rtl_readphy(tp, 0x0d);
2348 if ((val & 0x00ff) != 0x006c) {
2349 static const u32 set[] = {
2350 0x0065, 0x0066, 0x0067, 0x0068,
2351 0x0069, 0x006a, 0x006b, 0x006c
2355 rtl_writephy(tp, 0x1f, 0x0002);
2358 for (i = 0; i < ARRAY_SIZE(set); i++)
2359 rtl_writephy(tp, 0x0d, val | set[i]);
2362 static const struct phy_reg phy_reg_init[] = {
2370 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2373 /* Fine tune PLL performance */
2374 rtl_writephy(tp, 0x1f, 0x0002);
2375 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2376 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2378 /* Switching regulator Slew rate */
2379 rtl_writephy(tp, 0x1f, 0x0002);
2380 rtl_patchphy(tp, 0x0f, 0x0017);
2382 rtl_writephy(tp, 0x1f, 0x0005);
2383 rtl_writephy(tp, 0x05, 0x001b);
2384 if ((rtl_readphy(tp, 0x06) != 0xb300) ||
2385 (rtl_apply_firmware(tp, FIRMWARE_8168D_2) < 0)) {
2386 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2389 rtl_writephy(tp, 0x1f, 0x0000);
2392 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2394 static const struct phy_reg phy_reg_init[] = {
2450 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2453 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2455 static const struct phy_reg phy_reg_init[] = {
2465 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2466 rtl_patchphy(tp, 0x0d, 1 << 5);
2469 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
2471 static const struct phy_reg phy_reg_init[] = {
2478 rtl_writephy(tp, 0x1f, 0x0000);
2479 rtl_patchphy(tp, 0x11, 1 << 12);
2480 rtl_patchphy(tp, 0x19, 1 << 13);
2481 rtl_patchphy(tp, 0x10, 1 << 15);
2483 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2486 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2488 static const struct phy_reg phy_reg_init[] = {
2502 /* Disable ALDPS before ram code */
2503 rtl_writephy(tp, 0x1f, 0x0000);
2504 rtl_writephy(tp, 0x18, 0x0310);
2507 if (rtl_apply_firmware(tp, FIRMWARE_8105E_1) < 0)
2508 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2510 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2513 static void rtl_hw_phy_config(struct net_device *dev)
2515 struct rtl8169_private *tp = netdev_priv(dev);
2517 rtl8169_print_mac_version(tp);
2519 switch (tp->mac_version) {
2520 case RTL_GIGA_MAC_VER_01:
2522 case RTL_GIGA_MAC_VER_02:
2523 case RTL_GIGA_MAC_VER_03:
2524 rtl8169s_hw_phy_config(tp);
2526 case RTL_GIGA_MAC_VER_04:
2527 rtl8169sb_hw_phy_config(tp);
2529 case RTL_GIGA_MAC_VER_05:
2530 rtl8169scd_hw_phy_config(tp);
2532 case RTL_GIGA_MAC_VER_06:
2533 rtl8169sce_hw_phy_config(tp);
2535 case RTL_GIGA_MAC_VER_07:
2536 case RTL_GIGA_MAC_VER_08:
2537 case RTL_GIGA_MAC_VER_09:
2538 rtl8102e_hw_phy_config(tp);
2540 case RTL_GIGA_MAC_VER_11:
2541 rtl8168bb_hw_phy_config(tp);
2543 case RTL_GIGA_MAC_VER_12:
2544 rtl8168bef_hw_phy_config(tp);
2546 case RTL_GIGA_MAC_VER_17:
2547 rtl8168bef_hw_phy_config(tp);
2549 case RTL_GIGA_MAC_VER_18:
2550 rtl8168cp_1_hw_phy_config(tp);
2552 case RTL_GIGA_MAC_VER_19:
2553 rtl8168c_1_hw_phy_config(tp);
2555 case RTL_GIGA_MAC_VER_20:
2556 rtl8168c_2_hw_phy_config(tp);
2558 case RTL_GIGA_MAC_VER_21:
2559 rtl8168c_3_hw_phy_config(tp);
2561 case RTL_GIGA_MAC_VER_22:
2562 rtl8168c_4_hw_phy_config(tp);
2564 case RTL_GIGA_MAC_VER_23:
2565 case RTL_GIGA_MAC_VER_24:
2566 rtl8168cp_2_hw_phy_config(tp);
2568 case RTL_GIGA_MAC_VER_25:
2569 rtl8168d_1_hw_phy_config(tp);
2571 case RTL_GIGA_MAC_VER_26:
2572 rtl8168d_2_hw_phy_config(tp);
2574 case RTL_GIGA_MAC_VER_27:
2575 rtl8168d_3_hw_phy_config(tp);
2577 case RTL_GIGA_MAC_VER_28:
2578 rtl8168d_4_hw_phy_config(tp);
2580 case RTL_GIGA_MAC_VER_29:
2581 case RTL_GIGA_MAC_VER_30:
2582 rtl8105e_hw_phy_config(tp);
2590 static void rtl8169_phy_timer(unsigned long __opaque)
2592 struct net_device *dev = (struct net_device *)__opaque;
2593 struct rtl8169_private *tp = netdev_priv(dev);
2594 struct timer_list *timer = &tp->timer;
2595 void __iomem *ioaddr = tp->mmio_addr;
2596 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2598 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
2600 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
2603 spin_lock_irq(&tp->lock);
2605 if (tp->phy_reset_pending(tp)) {
2607 * A busy loop could burn quite a few cycles on nowadays CPU.
2608 * Let's delay the execution of the timer for a few ticks.
2614 if (tp->link_ok(ioaddr))
2617 netif_warn(tp, link, dev, "PHY reset until link up\n");
2619 tp->phy_reset_enable(tp);
2622 mod_timer(timer, jiffies + timeout);
2624 spin_unlock_irq(&tp->lock);
2627 static inline void rtl8169_delete_timer(struct net_device *dev)
2629 struct rtl8169_private *tp = netdev_priv(dev);
2630 struct timer_list *timer = &tp->timer;
2632 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
2635 del_timer_sync(timer);
2638 static inline void rtl8169_request_timer(struct net_device *dev)
2640 struct rtl8169_private *tp = netdev_priv(dev);
2641 struct timer_list *timer = &tp->timer;
2643 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
2646 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
2649 #ifdef CONFIG_NET_POLL_CONTROLLER
2651 * Polling 'interrupt' - used by things like netconsole to send skbs
2652 * without having to re-enable interrupts. It's not called while
2653 * the interrupt routine is executing.
2655 static void rtl8169_netpoll(struct net_device *dev)
2657 struct rtl8169_private *tp = netdev_priv(dev);
2658 struct pci_dev *pdev = tp->pci_dev;
2660 disable_irq(pdev->irq);
2661 rtl8169_interrupt(pdev->irq, dev);
2662 enable_irq(pdev->irq);
2666 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2667 void __iomem *ioaddr)
2670 pci_release_regions(pdev);
2671 pci_clear_mwi(pdev);
2672 pci_disable_device(pdev);
2676 static void rtl8169_phy_reset(struct net_device *dev,
2677 struct rtl8169_private *tp)
2681 tp->phy_reset_enable(tp);
2682 for (i = 0; i < 100; i++) {
2683 if (!tp->phy_reset_pending(tp))
2687 netif_err(tp, link, dev, "PHY reset failed\n");
2690 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
2692 void __iomem *ioaddr = tp->mmio_addr;
2694 rtl_hw_phy_config(dev);
2696 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2697 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2701 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2703 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2704 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
2706 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
2707 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2709 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
2710 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
2713 rtl8169_phy_reset(dev, tp);
2715 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2716 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2717 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2718 (tp->mii.supports_gmii ?
2719 ADVERTISED_1000baseT_Half |
2720 ADVERTISED_1000baseT_Full : 0));
2722 if (RTL_R8(PHYstatus) & TBI_Enable)
2723 netif_info(tp, link, dev, "TBI auto-negotiating\n");
2726 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2728 void __iomem *ioaddr = tp->mmio_addr;
2732 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2733 high = addr[4] | (addr[5] << 8);
2735 spin_lock_irq(&tp->lock);
2737 RTL_W8(Cfg9346, Cfg9346_Unlock);
2739 RTL_W32(MAC4, high);
2745 RTL_W8(Cfg9346, Cfg9346_Lock);
2747 spin_unlock_irq(&tp->lock);
2750 static int rtl_set_mac_address(struct net_device *dev, void *p)
2752 struct rtl8169_private *tp = netdev_priv(dev);
2753 struct sockaddr *addr = p;
2755 if (!is_valid_ether_addr(addr->sa_data))
2756 return -EADDRNOTAVAIL;
2758 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2760 rtl_rar_set(tp, dev->dev_addr);
2765 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2767 struct rtl8169_private *tp = netdev_priv(dev);
2768 struct mii_ioctl_data *data = if_mii(ifr);
2770 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2773 static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2777 data->phy_id = 32; /* Internal PHY */
2781 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
2785 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
2791 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2796 static const struct rtl_cfg_info {
2797 void (*hw_start)(struct net_device *);
2798 unsigned int region;
2804 } rtl_cfg_infos [] = {
2806 .hw_start = rtl_hw_start_8169,
2809 .intr_event = SYSErr | LinkChg | RxOverflow |
2810 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
2811 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
2812 .features = RTL_FEATURE_GMII,
2813 .default_ver = RTL_GIGA_MAC_VER_01,
2816 .hw_start = rtl_hw_start_8168,
2819 .intr_event = SYSErr | LinkChg | RxOverflow |
2820 TxErr | TxOK | RxOK | RxErr,
2821 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
2822 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2823 .default_ver = RTL_GIGA_MAC_VER_11,
2826 .hw_start = rtl_hw_start_8101,
2829 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2830 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
2831 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
2832 .features = RTL_FEATURE_MSI,
2833 .default_ver = RTL_GIGA_MAC_VER_13,
2837 /* Cfg9346_Unlock assumed. */
2838 static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2839 const struct rtl_cfg_info *cfg)
2844 cfg2 = RTL_R8(Config2) & ~MSIEnable;
2845 if (cfg->features & RTL_FEATURE_MSI) {
2846 if (pci_enable_msi(pdev)) {
2847 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2850 msi = RTL_FEATURE_MSI;
2853 RTL_W8(Config2, cfg2);
2857 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2859 if (tp->features & RTL_FEATURE_MSI) {
2860 pci_disable_msi(pdev);
2861 tp->features &= ~RTL_FEATURE_MSI;
2865 static const struct net_device_ops rtl8169_netdev_ops = {
2866 .ndo_open = rtl8169_open,
2867 .ndo_stop = rtl8169_close,
2868 .ndo_get_stats = rtl8169_get_stats,
2869 .ndo_start_xmit = rtl8169_start_xmit,
2870 .ndo_tx_timeout = rtl8169_tx_timeout,
2871 .ndo_validate_addr = eth_validate_addr,
2872 .ndo_change_mtu = rtl8169_change_mtu,
2873 .ndo_set_mac_address = rtl_set_mac_address,
2874 .ndo_do_ioctl = rtl8169_ioctl,
2875 .ndo_set_multicast_list = rtl_set_rx_mode,
2876 #ifdef CONFIG_NET_POLL_CONTROLLER
2877 .ndo_poll_controller = rtl8169_netpoll,
2882 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2884 struct mdio_ops *ops = &tp->mdio_ops;
2886 switch (tp->mac_version) {
2887 case RTL_GIGA_MAC_VER_27:
2888 ops->write = r8168dp_1_mdio_write;
2889 ops->read = r8168dp_1_mdio_read;
2891 case RTL_GIGA_MAC_VER_28:
2892 case RTL_GIGA_MAC_VER_31:
2893 ops->write = r8168dp_2_mdio_write;
2894 ops->read = r8168dp_2_mdio_read;
2897 ops->write = r8169_mdio_write;
2898 ops->read = r8169_mdio_read;
2903 static void r810x_phy_power_down(struct rtl8169_private *tp)
2905 rtl_writephy(tp, 0x1f, 0x0000);
2906 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2909 static void r810x_phy_power_up(struct rtl8169_private *tp)
2911 rtl_writephy(tp, 0x1f, 0x0000);
2912 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2915 static void r810x_pll_power_down(struct rtl8169_private *tp)
2917 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2918 rtl_writephy(tp, 0x1f, 0x0000);
2919 rtl_writephy(tp, MII_BMCR, 0x0000);
2923 r810x_phy_power_down(tp);
2926 static void r810x_pll_power_up(struct rtl8169_private *tp)
2928 r810x_phy_power_up(tp);
2931 static void r8168_phy_power_up(struct rtl8169_private *tp)
2933 rtl_writephy(tp, 0x1f, 0x0000);
2934 rtl_writephy(tp, 0x0e, 0x0000);
2935 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2938 static void r8168_phy_power_down(struct rtl8169_private *tp)
2940 rtl_writephy(tp, 0x1f, 0x0000);
2941 rtl_writephy(tp, 0x0e, 0x0200);
2942 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2945 static void r8168_pll_power_down(struct rtl8169_private *tp)
2947 void __iomem *ioaddr = tp->mmio_addr;
2949 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
2950 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
2951 (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
2952 r8168dp_check_dash(tp)) {
2956 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
2957 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
2958 (RTL_R16(CPlusCmd) & ASF)) {
2962 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2963 rtl_writephy(tp, 0x1f, 0x0000);
2964 rtl_writephy(tp, MII_BMCR, 0x0000);
2966 RTL_W32(RxConfig, RTL_R32(RxConfig) |
2967 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
2971 r8168_phy_power_down(tp);
2973 switch (tp->mac_version) {
2974 case RTL_GIGA_MAC_VER_25:
2975 case RTL_GIGA_MAC_VER_26:
2976 case RTL_GIGA_MAC_VER_27:
2977 case RTL_GIGA_MAC_VER_28:
2978 case RTL_GIGA_MAC_VER_31:
2979 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
2984 static void r8168_pll_power_up(struct rtl8169_private *tp)
2986 void __iomem *ioaddr = tp->mmio_addr;
2988 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
2989 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
2990 (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
2991 r8168dp_check_dash(tp)) {
2995 switch (tp->mac_version) {
2996 case RTL_GIGA_MAC_VER_25:
2997 case RTL_GIGA_MAC_VER_26:
2998 case RTL_GIGA_MAC_VER_27:
2999 case RTL_GIGA_MAC_VER_28:
3000 case RTL_GIGA_MAC_VER_31:
3001 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3005 r8168_phy_power_up(tp);
3008 static void rtl_pll_power_op(struct rtl8169_private *tp,
3009 void (*op)(struct rtl8169_private *))
3015 static void rtl_pll_power_down(struct rtl8169_private *tp)
3017 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3020 static void rtl_pll_power_up(struct rtl8169_private *tp)
3022 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3025 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3027 struct pll_power_ops *ops = &tp->pll_power_ops;
3029 switch (tp->mac_version) {
3030 case RTL_GIGA_MAC_VER_07:
3031 case RTL_GIGA_MAC_VER_08:
3032 case RTL_GIGA_MAC_VER_09:
3033 case RTL_GIGA_MAC_VER_10:
3034 case RTL_GIGA_MAC_VER_16:
3035 case RTL_GIGA_MAC_VER_29:
3036 case RTL_GIGA_MAC_VER_30:
3037 ops->down = r810x_pll_power_down;
3038 ops->up = r810x_pll_power_up;
3041 case RTL_GIGA_MAC_VER_11:
3042 case RTL_GIGA_MAC_VER_12:
3043 case RTL_GIGA_MAC_VER_17:
3044 case RTL_GIGA_MAC_VER_18:
3045 case RTL_GIGA_MAC_VER_19:
3046 case RTL_GIGA_MAC_VER_20:
3047 case RTL_GIGA_MAC_VER_21:
3048 case RTL_GIGA_MAC_VER_22:
3049 case RTL_GIGA_MAC_VER_23:
3050 case RTL_GIGA_MAC_VER_24:
3051 case RTL_GIGA_MAC_VER_25:
3052 case RTL_GIGA_MAC_VER_26:
3053 case RTL_GIGA_MAC_VER_27:
3054 case RTL_GIGA_MAC_VER_28:
3055 case RTL_GIGA_MAC_VER_31:
3056 ops->down = r8168_pll_power_down;
3057 ops->up = r8168_pll_power_up;
3067 static int __devinit
3068 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3070 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3071 const unsigned int region = cfg->region;
3072 struct rtl8169_private *tp;
3073 struct mii_if_info *mii;
3074 struct net_device *dev;
3075 void __iomem *ioaddr;
3079 if (netif_msg_drv(&debug)) {
3080 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3081 MODULENAME, RTL8169_VERSION);
3084 dev = alloc_etherdev(sizeof (*tp));
3086 if (netif_msg_drv(&debug))
3087 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
3092 SET_NETDEV_DEV(dev, &pdev->dev);
3093 dev->netdev_ops = &rtl8169_netdev_ops;
3094 tp = netdev_priv(dev);
3097 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
3101 mii->mdio_read = rtl_mdio_read;
3102 mii->mdio_write = rtl_mdio_write;
3103 mii->phy_id_mask = 0x1f;
3104 mii->reg_num_mask = 0x1f;
3105 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3107 /* disable ASPM completely as that cause random device stop working
3108 * problems as well as full system hangs for some PCIe devices users */
3109 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3110 PCIE_LINK_STATE_CLKPM);
3112 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3113 rc = pci_enable_device(pdev);
3115 netif_err(tp, probe, dev, "enable failure\n");
3116 goto err_out_free_dev_1;
3119 if (pci_set_mwi(pdev) < 0)
3120 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
3122 /* make sure PCI base addr 1 is MMIO */
3123 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
3124 netif_err(tp, probe, dev,
3125 "region #%d not an MMIO resource, aborting\n",
3131 /* check for weird/broken PCI region reporting */
3132 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
3133 netif_err(tp, probe, dev,
3134 "Invalid PCI region size(s), aborting\n");
3139 rc = pci_request_regions(pdev, MODULENAME);
3141 netif_err(tp, probe, dev, "could not request regions\n");
3145 tp->cp_cmd = RxChkSum;
3147 if ((sizeof(dma_addr_t) > 4) &&
3148 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
3149 tp->cp_cmd |= PCIDAC;
3150 dev->features |= NETIF_F_HIGHDMA;
3152 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3154 netif_err(tp, probe, dev, "DMA configuration failed\n");
3155 goto err_out_free_res_3;
3159 /* ioremap MMIO region */
3160 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
3162 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
3164 goto err_out_free_res_3;
3167 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3169 netif_info(tp, probe, dev, "no PCI Express capability\n");
3171 RTL_W16(IntrMask, 0x0000);
3173 /* Soft reset the chip. */
3174 RTL_W8(ChipCmd, CmdReset);
3176 /* Check that the chip has finished the reset. */
3177 for (i = 0; i < 100; i++) {
3178 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3180 msleep_interruptible(1);
3183 RTL_W16(IntrStatus, 0xffff);
3185 pci_set_master(pdev);
3187 /* Identify chip attached to board */
3188 rtl8169_get_mac_version(tp, ioaddr);
3191 * Pretend we are using VLANs; This bypasses a nasty bug where
3192 * Interrupts stop flowing on high load on 8110SCd controllers.
3194 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3195 tp->cp_cmd |= RxVlan;
3197 rtl_init_mdio_ops(tp);
3198 rtl_init_pll_power_ops(tp);
3200 /* Use appropriate default if unknown */
3201 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
3202 netif_notice(tp, probe, dev,
3203 "unknown MAC, using family default\n");
3204 tp->mac_version = cfg->default_ver;
3207 rtl8169_print_mac_version(tp);
3209 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
3210 if (tp->mac_version == rtl_chip_info[i].mac_version)
3213 if (i == ARRAY_SIZE(rtl_chip_info)) {
3215 "driver bug, MAC version not found in rtl_chip_info\n");
3220 RTL_W8(Cfg9346, Cfg9346_Unlock);
3221 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3222 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
3223 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3224 tp->features |= RTL_FEATURE_WOL;
3225 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3226 tp->features |= RTL_FEATURE_WOL;
3227 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
3228 RTL_W8(Cfg9346, Cfg9346_Lock);
3230 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3231 (RTL_R8(PHYstatus) & TBI_Enable)) {
3232 tp->set_speed = rtl8169_set_speed_tbi;
3233 tp->get_settings = rtl8169_gset_tbi;
3234 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3235 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3236 tp->link_ok = rtl8169_tbi_link_ok;
3237 tp->do_ioctl = rtl_tbi_ioctl;
3239 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
3241 tp->set_speed = rtl8169_set_speed_xmii;
3242 tp->get_settings = rtl8169_gset_xmii;
3243 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3244 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3245 tp->link_ok = rtl8169_xmii_link_ok;
3246 tp->do_ioctl = rtl_xmii_ioctl;
3249 spin_lock_init(&tp->lock);
3251 tp->mmio_addr = ioaddr;
3253 /* Get MAC address */
3254 for (i = 0; i < MAC_ADDR_LEN; i++)
3255 dev->dev_addr[i] = RTL_R8(MAC0 + i);
3256 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
3258 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
3259 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3260 dev->irq = pdev->irq;
3261 dev->base_addr = (unsigned long) ioaddr;
3263 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
3265 dev->features |= NETIF_F_HW_VLAN_TX_RX | NETIF_F_GRO;
3267 tp->intr_mask = 0xffff;
3268 tp->hw_start = cfg->hw_start;
3269 tp->intr_event = cfg->intr_event;
3270 tp->napi_event = cfg->napi_event;
3272 init_timer(&tp->timer);
3273 tp->timer.data = (unsigned long) dev;
3274 tp->timer.function = rtl8169_phy_timer;
3276 rc = register_netdev(dev);
3280 pci_set_drvdata(pdev, dev);
3282 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3283 rtl_chip_info[tp->chipset].name,
3284 dev->base_addr, dev->dev_addr,
3285 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
3287 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3288 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3289 (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
3290 rtl8168_driver_start(tp);
3293 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
3295 if (pci_dev_run_wake(pdev))
3296 pm_runtime_put_noidle(&pdev->dev);
3298 netif_carrier_off(dev);
3304 rtl_disable_msi(pdev, tp);
3307 pci_release_regions(pdev);
3309 pci_clear_mwi(pdev);
3310 pci_disable_device(pdev);
3316 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
3318 struct net_device *dev = pci_get_drvdata(pdev);
3319 struct rtl8169_private *tp = netdev_priv(dev);
3321 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3322 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3323 (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
3324 rtl8168_driver_stop(tp);
3327 cancel_delayed_work_sync(&tp->task);
3329 rtl_release_firmware(tp);
3331 unregister_netdev(dev);
3333 if (pci_dev_run_wake(pdev))
3334 pm_runtime_get_noresume(&pdev->dev);
3336 /* restore original MAC address */
3337 rtl_rar_set(tp, dev->perm_addr);
3339 rtl_disable_msi(pdev, tp);
3340 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3341 pci_set_drvdata(pdev, NULL);
3344 static int rtl8169_open(struct net_device *dev)
3346 struct rtl8169_private *tp = netdev_priv(dev);
3347 void __iomem *ioaddr = tp->mmio_addr;
3348 struct pci_dev *pdev = tp->pci_dev;
3349 int retval = -ENOMEM;
3351 pm_runtime_get_sync(&pdev->dev);
3354 * Rx and Tx desscriptors needs 256 bytes alignment.
3355 * dma_alloc_coherent provides more.
3357 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3358 &tp->TxPhyAddr, GFP_KERNEL);
3359 if (!tp->TxDescArray)
3360 goto err_pm_runtime_put;
3362 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3363 &tp->RxPhyAddr, GFP_KERNEL);
3364 if (!tp->RxDescArray)
3367 retval = rtl8169_init_ring(dev);
3371 INIT_DELAYED_WORK(&tp->task, NULL);
3375 retval = request_irq(dev->irq, rtl8169_interrupt,
3376 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
3379 goto err_release_ring_2;
3381 napi_enable(&tp->napi);
3383 rtl8169_init_phy(dev, tp);
3385 rtl8169_vlan_mode(dev);
3387 rtl_pll_power_up(tp);
3391 rtl8169_request_timer(dev);
3393 tp->saved_wolopts = 0;
3394 pm_runtime_put_noidle(&pdev->dev);
3396 rtl8169_check_link_status(dev, tp, ioaddr);
3401 rtl8169_rx_clear(tp);
3403 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3405 tp->RxDescArray = NULL;
3407 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3409 tp->TxDescArray = NULL;
3411 pm_runtime_put_noidle(&pdev->dev);
3415 static void rtl8169_hw_reset(struct rtl8169_private *tp)
3417 void __iomem *ioaddr = tp->mmio_addr;
3419 /* Disable interrupts */
3420 rtl8169_irq_mask_and_ack(ioaddr);
3422 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3423 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3424 tp->mac_version == RTL_GIGA_MAC_VER_31) {
3425 while (RTL_R8(TxPoll) & NPQ)
3430 /* Reset the chipset */
3431 RTL_W8(ChipCmd, CmdReset);
3437 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
3439 void __iomem *ioaddr = tp->mmio_addr;
3440 u32 cfg = rtl8169_rx_config;
3442 cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
3443 RTL_W32(RxConfig, cfg);
3445 /* Set DMA burst size and Interframe Gap Time */
3446 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3447 (InterFrameGap << TxInterFrameGapShift));
3450 static void rtl_hw_start(struct net_device *dev)
3452 struct rtl8169_private *tp = netdev_priv(dev);
3453 void __iomem *ioaddr = tp->mmio_addr;
3456 /* Soft reset the chip. */
3457 RTL_W8(ChipCmd, CmdReset);
3459 /* Check that the chip has finished the reset. */
3460 for (i = 0; i < 100; i++) {
3461 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3463 msleep_interruptible(1);
3468 netif_start_queue(dev);
3472 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3473 void __iomem *ioaddr)
3476 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3477 * register to be written before TxDescAddrLow to work.
3478 * Switching from MMIO to I/O access fixes the issue as well.
3480 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
3481 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
3482 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
3483 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
3486 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3490 cmd = RTL_R16(CPlusCmd);
3491 RTL_W16(CPlusCmd, cmd);
3495 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
3497 /* Low hurts. Let's disable the filtering. */
3498 RTL_W16(RxMaxSize, rx_buf_sz + 1);
3501 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3503 static const struct {
3508 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3509 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3510 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3511 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3516 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
3517 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
3518 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3519 RTL_W32(0x7c, p->val);
3525 static void rtl_hw_start_8169(struct net_device *dev)
3527 struct rtl8169_private *tp = netdev_priv(dev);
3528 void __iomem *ioaddr = tp->mmio_addr;
3529 struct pci_dev *pdev = tp->pci_dev;
3531 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3532 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3533 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3536 RTL_W8(Cfg9346, Cfg9346_Unlock);
3537 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3538 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3539 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3540 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3541 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3543 RTL_W8(EarlyTxThres, NoEarlyTx);
3545 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
3547 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3548 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3549 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3550 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3551 rtl_set_rx_tx_config_registers(tp);
3553 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
3555 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3556 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
3557 dprintk("Set MAC Reg C+CR Offset 0xE0. "
3558 "Bit-3 and bit-14 MUST be 1\n");
3559 tp->cp_cmd |= (1 << 14);
3562 RTL_W16(CPlusCmd, tp->cp_cmd);
3564 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3567 * Undocumented corner. Supposedly:
3568 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3570 RTL_W16(IntrMitigate, 0x0000);
3572 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3574 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3575 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3576 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3577 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3578 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3579 rtl_set_rx_tx_config_registers(tp);
3582 RTL_W8(Cfg9346, Cfg9346_Lock);
3584 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3587 RTL_W32(RxMissed, 0);
3589 rtl_set_rx_mode(dev);
3591 /* no early-rx interrupts */
3592 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
3594 /* Enable all known interrupts by setting the interrupt mask. */
3595 RTL_W16(IntrMask, tp->intr_event);
3598 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
3600 struct net_device *dev = pci_get_drvdata(pdev);
3601 struct rtl8169_private *tp = netdev_priv(dev);
3602 int cap = tp->pcie_cap;
3607 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3608 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3609 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3613 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
3617 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
3618 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3621 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3623 rtl_csi_access_enable(ioaddr, 0x17000000);
3626 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3628 rtl_csi_access_enable(ioaddr, 0x27000000);
3632 unsigned int offset;
3637 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
3642 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3643 rtl_ephy_write(ioaddr, e->offset, w);
3648 static void rtl_disable_clock_request(struct pci_dev *pdev)
3650 struct net_device *dev = pci_get_drvdata(pdev);
3651 struct rtl8169_private *tp = netdev_priv(dev);
3652 int cap = tp->pcie_cap;
3657 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3658 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3659 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3663 static void rtl_enable_clock_request(struct pci_dev *pdev)
3665 struct net_device *dev = pci_get_drvdata(pdev);
3666 struct rtl8169_private *tp = netdev_priv(dev);
3667 int cap = tp->pcie_cap;
3672 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3673 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3674 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3678 #define R8168_CPCMD_QUIRK_MASK (\
3689 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3691 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3693 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3695 rtl_tx_performance_tweak(pdev,
3696 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3699 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3701 rtl_hw_start_8168bb(ioaddr, pdev);
3703 RTL_W8(MaxTxPacketSize, TxPacketMax);
3705 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3708 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3710 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3712 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3714 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3716 rtl_disable_clock_request(pdev);
3718 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3721 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
3723 static const struct ephy_info e_info_8168cp[] = {
3724 { 0x01, 0, 0x0001 },
3725 { 0x02, 0x0800, 0x1000 },
3726 { 0x03, 0, 0x0042 },
3727 { 0x06, 0x0080, 0x0000 },
3731 rtl_csi_access_enable_2(ioaddr);
3733 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3735 __rtl_hw_start_8168cp(ioaddr, pdev);
3738 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3740 rtl_csi_access_enable_2(ioaddr);
3742 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3744 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3746 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3749 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3751 rtl_csi_access_enable_2(ioaddr);
3753 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3756 RTL_W8(DBG_REG, 0x20);
3758 RTL_W8(MaxTxPacketSize, TxPacketMax);
3760 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3762 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3765 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3767 static const struct ephy_info e_info_8168c_1[] = {
3768 { 0x02, 0x0800, 0x1000 },
3769 { 0x03, 0, 0x0002 },
3770 { 0x06, 0x0080, 0x0000 }
3773 rtl_csi_access_enable_2(ioaddr);
3775 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3777 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3779 __rtl_hw_start_8168cp(ioaddr, pdev);
3782 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3784 static const struct ephy_info e_info_8168c_2[] = {
3785 { 0x01, 0, 0x0001 },
3786 { 0x03, 0x0400, 0x0220 }
3789 rtl_csi_access_enable_2(ioaddr);
3791 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3793 __rtl_hw_start_8168cp(ioaddr, pdev);
3796 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3798 rtl_hw_start_8168c_2(ioaddr, pdev);
3801 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3803 rtl_csi_access_enable_2(ioaddr);
3805 __rtl_hw_start_8168cp(ioaddr, pdev);
3808 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3810 rtl_csi_access_enable_2(ioaddr);
3812 rtl_disable_clock_request(pdev);
3814 RTL_W8(MaxTxPacketSize, TxPacketMax);
3816 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3818 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3821 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
3823 rtl_csi_access_enable_1(ioaddr);
3825 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3827 RTL_W8(MaxTxPacketSize, TxPacketMax);
3829 rtl_disable_clock_request(pdev);
3832 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
3834 static const struct ephy_info e_info_8168d_4[] = {
3836 { 0x19, 0x20, 0x50 },
3841 rtl_csi_access_enable_1(ioaddr);
3843 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3845 RTL_W8(MaxTxPacketSize, TxPacketMax);
3847 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
3848 const struct ephy_info *e = e_info_8168d_4 + i;
3851 w = rtl_ephy_read(ioaddr, e->offset);
3852 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
3855 rtl_enable_clock_request(pdev);
3858 static void rtl_hw_start_8168(struct net_device *dev)
3860 struct rtl8169_private *tp = netdev_priv(dev);
3861 void __iomem *ioaddr = tp->mmio_addr;
3862 struct pci_dev *pdev = tp->pci_dev;
3864 RTL_W8(Cfg9346, Cfg9346_Unlock);
3866 RTL_W8(MaxTxPacketSize, TxPacketMax);
3868 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
3870 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
3872 RTL_W16(CPlusCmd, tp->cp_cmd);
3874 RTL_W16(IntrMitigate, 0x5151);
3876 /* Work around for RxFIFO overflow. */
3877 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
3878 tp->mac_version == RTL_GIGA_MAC_VER_22) {
3879 tp->intr_event |= RxFIFOOver | PCSTimeout;
3880 tp->intr_event &= ~RxOverflow;
3883 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3885 rtl_set_rx_mode(dev);
3887 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3888 (InterFrameGap << TxInterFrameGapShift));
3892 switch (tp->mac_version) {
3893 case RTL_GIGA_MAC_VER_11:
3894 rtl_hw_start_8168bb(ioaddr, pdev);
3897 case RTL_GIGA_MAC_VER_12:
3898 case RTL_GIGA_MAC_VER_17:
3899 rtl_hw_start_8168bef(ioaddr, pdev);
3902 case RTL_GIGA_MAC_VER_18:
3903 rtl_hw_start_8168cp_1(ioaddr, pdev);
3906 case RTL_GIGA_MAC_VER_19:
3907 rtl_hw_start_8168c_1(ioaddr, pdev);
3910 case RTL_GIGA_MAC_VER_20:
3911 rtl_hw_start_8168c_2(ioaddr, pdev);
3914 case RTL_GIGA_MAC_VER_21:
3915 rtl_hw_start_8168c_3(ioaddr, pdev);
3918 case RTL_GIGA_MAC_VER_22:
3919 rtl_hw_start_8168c_4(ioaddr, pdev);
3922 case RTL_GIGA_MAC_VER_23:
3923 rtl_hw_start_8168cp_2(ioaddr, pdev);
3926 case RTL_GIGA_MAC_VER_24:
3927 rtl_hw_start_8168cp_3(ioaddr, pdev);
3930 case RTL_GIGA_MAC_VER_25:
3931 case RTL_GIGA_MAC_VER_26:
3932 case RTL_GIGA_MAC_VER_27:
3933 rtl_hw_start_8168d(ioaddr, pdev);
3936 case RTL_GIGA_MAC_VER_28:
3937 rtl_hw_start_8168d_4(ioaddr, pdev);
3939 case RTL_GIGA_MAC_VER_31:
3940 rtl_hw_start_8168dp(ioaddr, pdev);
3945 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
3946 dev->name, tp->mac_version);
3950 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3952 RTL_W8(Cfg9346, Cfg9346_Lock);
3954 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
3956 RTL_W16(IntrMask, tp->intr_event);
3959 #define R810X_CPCMD_QUIRK_MASK (\
3970 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3972 static const struct ephy_info e_info_8102e_1[] = {
3973 { 0x01, 0, 0x6e65 },
3974 { 0x02, 0, 0x091f },
3975 { 0x03, 0, 0xc2f9 },
3976 { 0x06, 0, 0xafb5 },
3977 { 0x07, 0, 0x0e00 },
3978 { 0x19, 0, 0xec80 },
3979 { 0x01, 0, 0x2e65 },
3984 rtl_csi_access_enable_2(ioaddr);
3986 RTL_W8(DBG_REG, FIX_NAK_1);
3988 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3991 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
3992 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3994 cfg1 = RTL_R8(Config1);
3995 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
3996 RTL_W8(Config1, cfg1 & ~LEDS0);
3998 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4001 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4003 rtl_csi_access_enable_2(ioaddr);
4005 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4007 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4008 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4011 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4013 rtl_hw_start_8102e_2(ioaddr, pdev);
4015 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4018 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4020 static const struct ephy_info e_info_8105e_1[] = {
4021 { 0x07, 0, 0x4000 },
4022 { 0x19, 0, 0x0200 },
4023 { 0x19, 0, 0x0020 },
4024 { 0x1e, 0, 0x2000 },
4025 { 0x03, 0, 0x0001 },
4026 { 0x19, 0, 0x0100 },
4027 { 0x19, 0, 0x0004 },
4031 /* Force LAN exit from ASPM if Rx/Tx are not idel */
4032 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4034 /* disable Early Tally Counter */
4035 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4037 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4038 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4040 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4043 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4045 rtl_hw_start_8105e_1(ioaddr, pdev);
4046 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4049 static void rtl_hw_start_8101(struct net_device *dev)
4051 struct rtl8169_private *tp = netdev_priv(dev);
4052 void __iomem *ioaddr = tp->mmio_addr;
4053 struct pci_dev *pdev = tp->pci_dev;
4055 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
4056 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
4057 int cap = tp->pcie_cap;
4060 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4061 PCI_EXP_DEVCTL_NOSNOOP_EN);
4065 RTL_W8(Cfg9346, Cfg9346_Unlock);
4067 switch (tp->mac_version) {
4068 case RTL_GIGA_MAC_VER_07:
4069 rtl_hw_start_8102e_1(ioaddr, pdev);
4072 case RTL_GIGA_MAC_VER_08:
4073 rtl_hw_start_8102e_3(ioaddr, pdev);
4076 case RTL_GIGA_MAC_VER_09:
4077 rtl_hw_start_8102e_2(ioaddr, pdev);
4080 case RTL_GIGA_MAC_VER_29:
4081 rtl_hw_start_8105e_1(ioaddr, pdev);
4083 case RTL_GIGA_MAC_VER_30:
4084 rtl_hw_start_8105e_2(ioaddr, pdev);
4088 RTL_W8(Cfg9346, Cfg9346_Lock);
4090 RTL_W8(MaxTxPacketSize, TxPacketMax);
4092 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4094 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
4095 RTL_W16(CPlusCmd, tp->cp_cmd);
4097 RTL_W16(IntrMitigate, 0x0000);
4099 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4101 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4102 rtl_set_rx_tx_config_registers(tp);
4106 rtl_set_rx_mode(dev);
4108 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
4110 RTL_W16(IntrMask, tp->intr_event);
4113 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4115 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4122 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4124 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
4125 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4128 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4129 void **data_buff, struct RxDesc *desc)
4131 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
4136 rtl8169_make_unusable_by_asic(desc);
4139 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4141 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4143 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4146 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4149 desc->addr = cpu_to_le64(mapping);
4151 rtl8169_mark_to_asic(desc, rx_buf_sz);
4154 static inline void *rtl8169_align(void *data)
4156 return (void *)ALIGN((long)data, 16);
4159 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4160 struct RxDesc *desc)
4164 struct device *d = &tp->pci_dev->dev;
4165 struct net_device *dev = tp->dev;
4166 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
4168 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4172 if (rtl8169_align(data) != data) {
4174 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4179 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
4181 if (unlikely(dma_mapping_error(d, mapping))) {
4182 if (net_ratelimit())
4183 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
4187 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
4195 static void rtl8169_rx_clear(struct rtl8169_private *tp)
4199 for (i = 0; i < NUM_RX_DESC; i++) {
4200 if (tp->Rx_databuff[i]) {
4201 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
4202 tp->RxDescArray + i);
4207 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
4209 desc->opts1 |= cpu_to_le32(RingEnd);
4212 static int rtl8169_rx_fill(struct rtl8169_private *tp)
4216 for (i = 0; i < NUM_RX_DESC; i++) {
4219 if (tp->Rx_databuff[i])
4222 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
4224 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
4227 tp->Rx_databuff[i] = data;
4230 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4234 rtl8169_rx_clear(tp);
4238 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4240 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4243 static int rtl8169_init_ring(struct net_device *dev)
4245 struct rtl8169_private *tp = netdev_priv(dev);
4247 rtl8169_init_ring_indexes(tp);
4249 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
4250 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
4252 return rtl8169_rx_fill(tp);
4255 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
4256 struct TxDesc *desc)
4258 unsigned int len = tx_skb->len;
4260 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4268 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4273 for (i = 0; i < n; i++) {
4274 unsigned int entry = (start + i) % NUM_TX_DESC;
4275 struct ring_info *tx_skb = tp->tx_skb + entry;
4276 unsigned int len = tx_skb->len;
4279 struct sk_buff *skb = tx_skb->skb;
4281 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4282 tp->TxDescArray + entry);
4284 tp->dev->stats.tx_dropped++;
4292 static void rtl8169_tx_clear(struct rtl8169_private *tp)
4294 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
4295 tp->cur_tx = tp->dirty_tx = 0;
4298 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
4300 struct rtl8169_private *tp = netdev_priv(dev);
4302 PREPARE_DELAYED_WORK(&tp->task, task);
4303 schedule_delayed_work(&tp->task, 4);
4306 static void rtl8169_wait_for_quiescence(struct net_device *dev)
4308 struct rtl8169_private *tp = netdev_priv(dev);
4309 void __iomem *ioaddr = tp->mmio_addr;
4311 synchronize_irq(dev->irq);
4313 /* Wait for any pending NAPI task to complete */
4314 napi_disable(&tp->napi);
4316 rtl8169_irq_mask_and_ack(ioaddr);
4318 tp->intr_mask = 0xffff;
4319 RTL_W16(IntrMask, tp->intr_event);
4320 napi_enable(&tp->napi);
4323 static void rtl8169_reinit_task(struct work_struct *work)
4325 struct rtl8169_private *tp =
4326 container_of(work, struct rtl8169_private, task.work);
4327 struct net_device *dev = tp->dev;
4332 if (!netif_running(dev))
4335 rtl8169_wait_for_quiescence(dev);
4338 ret = rtl8169_open(dev);
4339 if (unlikely(ret < 0)) {
4340 if (net_ratelimit())
4341 netif_err(tp, drv, dev,
4342 "reinit failure (status = %d). Rescheduling\n",
4344 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4351 static void rtl8169_reset_task(struct work_struct *work)
4353 struct rtl8169_private *tp =
4354 container_of(work, struct rtl8169_private, task.work);
4355 struct net_device *dev = tp->dev;
4359 if (!netif_running(dev))
4362 rtl8169_wait_for_quiescence(dev);
4364 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
4365 rtl8169_tx_clear(tp);
4367 if (tp->dirty_rx == tp->cur_rx) {
4368 rtl8169_init_ring_indexes(tp);
4370 netif_wake_queue(dev);
4371 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
4373 if (net_ratelimit())
4374 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
4375 rtl8169_schedule_work(dev, rtl8169_reset_task);
4382 static void rtl8169_tx_timeout(struct net_device *dev)
4384 struct rtl8169_private *tp = netdev_priv(dev);
4386 rtl8169_hw_reset(tp);
4388 /* Let's wait a bit while any (async) irq lands on */
4389 rtl8169_schedule_work(dev, rtl8169_reset_task);
4392 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4395 struct skb_shared_info *info = skb_shinfo(skb);
4396 unsigned int cur_frag, entry;
4397 struct TxDesc * uninitialized_var(txd);
4398 struct device *d = &tp->pci_dev->dev;
4401 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4402 skb_frag_t *frag = info->frags + cur_frag;
4407 entry = (entry + 1) % NUM_TX_DESC;
4409 txd = tp->TxDescArray + entry;
4411 addr = ((void *) page_address(frag->page)) + frag->page_offset;
4412 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
4413 if (unlikely(dma_mapping_error(d, mapping))) {
4414 if (net_ratelimit())
4415 netif_err(tp, drv, tp->dev,
4416 "Failed to map TX fragments DMA!\n");
4420 /* anti gcc 2.95.3 bugware (sic) */
4421 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4423 txd->opts1 = cpu_to_le32(status);
4424 txd->addr = cpu_to_le64(mapping);
4426 tp->tx_skb[entry].len = len;
4430 tp->tx_skb[entry].skb = skb;
4431 txd->opts1 |= cpu_to_le32(LastFrag);
4437 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4441 static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
4443 if (dev->features & NETIF_F_TSO) {
4444 u32 mss = skb_shinfo(skb)->gso_size;
4447 return LargeSend | ((mss & MSSMask) << MSSShift);
4449 if (skb->ip_summed == CHECKSUM_PARTIAL) {
4450 const struct iphdr *ip = ip_hdr(skb);
4452 if (ip->protocol == IPPROTO_TCP)
4453 return IPCS | TCPCS;
4454 else if (ip->protocol == IPPROTO_UDP)
4455 return IPCS | UDPCS;
4456 WARN_ON(1); /* we need a WARN() */
4461 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4462 struct net_device *dev)
4464 struct rtl8169_private *tp = netdev_priv(dev);
4465 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
4466 struct TxDesc *txd = tp->TxDescArray + entry;
4467 void __iomem *ioaddr = tp->mmio_addr;
4468 struct device *d = &tp->pci_dev->dev;
4474 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
4475 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
4479 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
4482 len = skb_headlen(skb);
4483 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
4484 if (unlikely(dma_mapping_error(d, mapping))) {
4485 if (net_ratelimit())
4486 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
4490 tp->tx_skb[entry].len = len;
4491 txd->addr = cpu_to_le64(mapping);
4492 txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4494 opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
4496 frags = rtl8169_xmit_frags(tp, skb, opts1);
4502 opts1 |= FirstFrag | LastFrag;
4503 tp->tx_skb[entry].skb = skb;
4508 /* anti gcc 2.95.3 bugware (sic) */
4509 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4510 txd->opts1 = cpu_to_le32(status);
4512 tp->cur_tx += frags + 1;
4516 RTL_W8(TxPoll, NPQ); /* set polling bit */
4518 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4519 netif_stop_queue(dev);
4521 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4522 netif_wake_queue(dev);
4525 return NETDEV_TX_OK;
4528 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
4531 dev->stats.tx_dropped++;
4532 return NETDEV_TX_OK;
4535 netif_stop_queue(dev);
4536 dev->stats.tx_dropped++;
4537 return NETDEV_TX_BUSY;
4540 static void rtl8169_pcierr_interrupt(struct net_device *dev)
4542 struct rtl8169_private *tp = netdev_priv(dev);
4543 struct pci_dev *pdev = tp->pci_dev;
4544 u16 pci_status, pci_cmd;
4546 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4547 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4549 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4550 pci_cmd, pci_status);
4553 * The recovery sequence below admits a very elaborated explanation:
4554 * - it seems to work;
4555 * - I did not see what else could be done;
4556 * - it makes iop3xx happy.
4558 * Feel free to adjust to your needs.
4560 if (pdev->broken_parity_status)
4561 pci_cmd &= ~PCI_COMMAND_PARITY;
4563 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4565 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
4567 pci_write_config_word(pdev, PCI_STATUS,
4568 pci_status & (PCI_STATUS_DETECTED_PARITY |
4569 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4570 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4572 /* The infamous DAC f*ckup only happens at boot time */
4573 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
4574 void __iomem *ioaddr = tp->mmio_addr;
4576 netif_info(tp, intr, dev, "disabling PCI DAC\n");
4577 tp->cp_cmd &= ~PCIDAC;
4578 RTL_W16(CPlusCmd, tp->cp_cmd);
4579 dev->features &= ~NETIF_F_HIGHDMA;
4582 rtl8169_hw_reset(tp);
4584 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4587 static void rtl8169_tx_interrupt(struct net_device *dev,
4588 struct rtl8169_private *tp,
4589 void __iomem *ioaddr)
4591 unsigned int dirty_tx, tx_left;
4593 dirty_tx = tp->dirty_tx;
4595 tx_left = tp->cur_tx - dirty_tx;
4597 while (tx_left > 0) {
4598 unsigned int entry = dirty_tx % NUM_TX_DESC;
4599 struct ring_info *tx_skb = tp->tx_skb + entry;
4603 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4604 if (status & DescOwn)
4607 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4608 tp->TxDescArray + entry);
4609 if (status & LastFrag) {
4610 dev->stats.tx_packets++;
4611 dev->stats.tx_bytes += tx_skb->skb->len;
4612 dev_kfree_skb(tx_skb->skb);
4619 if (tp->dirty_tx != dirty_tx) {
4620 tp->dirty_tx = dirty_tx;
4622 if (netif_queue_stopped(dev) &&
4623 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4624 netif_wake_queue(dev);
4627 * 8168 hack: TxPoll requests are lost when the Tx packets are
4628 * too close. Let's kick an extra TxPoll request when a burst
4629 * of start_xmit activity is detected (if it is not detected,
4630 * it is slow enough). -- FR
4633 if (tp->cur_tx != dirty_tx)
4634 RTL_W8(TxPoll, NPQ);
4638 static inline int rtl8169_fragmented_frame(u32 status)
4640 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4643 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
4645 u32 status = opts1 & RxProtoMask;
4647 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
4648 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
4649 skb->ip_summed = CHECKSUM_UNNECESSARY;
4651 skb_checksum_none_assert(skb);
4654 static struct sk_buff *rtl8169_try_rx_copy(void *data,
4655 struct rtl8169_private *tp,
4659 struct sk_buff *skb;
4660 struct device *d = &tp->pci_dev->dev;
4662 data = rtl8169_align(data);
4663 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
4665 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4667 memcpy(skb->data, data, pkt_size);
4668 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4674 * Warning : rtl8169_rx_interrupt() might be called :
4675 * 1) from NAPI (softirq) context
4676 * (polling = 1 : we should call netif_receive_skb())
4677 * 2) from process context (rtl8169_reset_task())
4678 * (polling = 0 : we must call netif_rx() instead)
4680 static int rtl8169_rx_interrupt(struct net_device *dev,
4681 struct rtl8169_private *tp,
4682 void __iomem *ioaddr, u32 budget)
4684 unsigned int cur_rx, rx_left;
4686 int polling = (budget != ~(u32)0) ? 1 : 0;
4688 cur_rx = tp->cur_rx;
4689 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
4690 rx_left = min(rx_left, budget);
4692 for (; rx_left > 0; rx_left--, cur_rx++) {
4693 unsigned int entry = cur_rx % NUM_RX_DESC;
4694 struct RxDesc *desc = tp->RxDescArray + entry;
4698 status = le32_to_cpu(desc->opts1);
4700 if (status & DescOwn)
4702 if (unlikely(status & RxRES)) {
4703 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4705 dev->stats.rx_errors++;
4706 if (status & (RxRWT | RxRUNT))
4707 dev->stats.rx_length_errors++;
4709 dev->stats.rx_crc_errors++;
4710 if (status & RxFOVF) {
4711 rtl8169_schedule_work(dev, rtl8169_reset_task);
4712 dev->stats.rx_fifo_errors++;
4714 rtl8169_mark_to_asic(desc, rx_buf_sz);
4716 struct sk_buff *skb;
4717 dma_addr_t addr = le64_to_cpu(desc->addr);
4718 int pkt_size = (status & 0x00001FFF) - 4;
4721 * The driver does not support incoming fragmented
4722 * frames. They are seen as a symptom of over-mtu
4725 if (unlikely(rtl8169_fragmented_frame(status))) {
4726 dev->stats.rx_dropped++;
4727 dev->stats.rx_length_errors++;
4728 rtl8169_mark_to_asic(desc, rx_buf_sz);
4732 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4733 tp, pkt_size, addr);
4734 rtl8169_mark_to_asic(desc, rx_buf_sz);
4736 dev->stats.rx_dropped++;
4740 rtl8169_rx_csum(skb, status);
4741 skb_put(skb, pkt_size);
4742 skb->protocol = eth_type_trans(skb, dev);
4744 rtl8169_rx_vlan_tag(desc, skb);
4746 if (likely(polling))
4747 napi_gro_receive(&tp->napi, skb);
4751 dev->stats.rx_bytes += pkt_size;
4752 dev->stats.rx_packets++;
4755 /* Work around for AMD plateform. */
4756 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
4757 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4763 count = cur_rx - tp->cur_rx;
4764 tp->cur_rx = cur_rx;
4766 tp->dirty_rx += count;
4771 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
4773 struct net_device *dev = dev_instance;
4774 struct rtl8169_private *tp = netdev_priv(dev);
4775 void __iomem *ioaddr = tp->mmio_addr;
4779 /* loop handling interrupts until we have no new ones or
4780 * we hit a invalid/hotplug case.
4782 status = RTL_R16(IntrStatus);
4783 while (status && status != 0xffff) {
4786 /* Handle all of the error cases first. These will reset
4787 * the chip, so just exit the loop.
4789 if (unlikely(!netif_running(dev))) {
4790 rtl8169_asic_down(ioaddr);
4794 if (unlikely(status & RxFIFOOver)) {
4795 switch (tp->mac_version) {
4796 /* Work around for rx fifo overflow */
4797 case RTL_GIGA_MAC_VER_11:
4798 case RTL_GIGA_MAC_VER_22:
4799 case RTL_GIGA_MAC_VER_26:
4800 netif_stop_queue(dev);
4801 rtl8169_tx_timeout(dev);
4803 /* Testers needed. */
4804 case RTL_GIGA_MAC_VER_17:
4805 case RTL_GIGA_MAC_VER_19:
4806 case RTL_GIGA_MAC_VER_20:
4807 case RTL_GIGA_MAC_VER_21:
4808 case RTL_GIGA_MAC_VER_23:
4809 case RTL_GIGA_MAC_VER_24:
4810 case RTL_GIGA_MAC_VER_27:
4811 case RTL_GIGA_MAC_VER_28:
4812 case RTL_GIGA_MAC_VER_31:
4813 /* Experimental science. Pktgen proof. */
4814 case RTL_GIGA_MAC_VER_12:
4815 case RTL_GIGA_MAC_VER_25:
4816 if (status == RxFIFOOver)
4824 if (unlikely(status & SYSErr)) {
4825 rtl8169_pcierr_interrupt(dev);
4829 if (status & LinkChg)
4830 __rtl8169_check_link_status(dev, tp, ioaddr, true);
4832 /* We need to see the lastest version of tp->intr_mask to
4833 * avoid ignoring an MSI interrupt and having to wait for
4834 * another event which may never come.
4837 if (status & tp->intr_mask & tp->napi_event) {
4838 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
4839 tp->intr_mask = ~tp->napi_event;
4841 if (likely(napi_schedule_prep(&tp->napi)))
4842 __napi_schedule(&tp->napi);
4844 netif_info(tp, intr, dev,
4845 "interrupt %04x in poll\n", status);
4848 /* We only get a new MSI interrupt when all active irq
4849 * sources on the chip have been acknowledged. So, ack
4850 * everything we've seen and check if new sources have become
4851 * active to avoid blocking all interrupts from the chip.
4854 (status & RxFIFOOver) ? (status | RxOverflow) : status);
4855 status = RTL_R16(IntrStatus);
4858 return IRQ_RETVAL(handled);
4861 static int rtl8169_poll(struct napi_struct *napi, int budget)
4863 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
4864 struct net_device *dev = tp->dev;
4865 void __iomem *ioaddr = tp->mmio_addr;
4868 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
4869 rtl8169_tx_interrupt(dev, tp, ioaddr);
4871 if (work_done < budget) {
4872 napi_complete(napi);
4874 /* We need for force the visibility of tp->intr_mask
4875 * for other CPUs, as we can loose an MSI interrupt
4876 * and potentially wait for a retransmit timeout if we don't.
4877 * The posted write to IntrMask is safe, as it will
4878 * eventually make it to the chip and we won't loose anything
4881 tp->intr_mask = 0xffff;
4883 RTL_W16(IntrMask, tp->intr_event);
4889 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
4891 struct rtl8169_private *tp = netdev_priv(dev);
4893 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
4896 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
4897 RTL_W32(RxMissed, 0);
4900 static void rtl8169_down(struct net_device *dev)
4902 struct rtl8169_private *tp = netdev_priv(dev);
4903 void __iomem *ioaddr = tp->mmio_addr;
4905 rtl8169_delete_timer(dev);
4907 netif_stop_queue(dev);
4909 napi_disable(&tp->napi);
4911 spin_lock_irq(&tp->lock);
4913 rtl8169_asic_down(ioaddr);
4915 * At this point device interrupts can not be enabled in any function,
4916 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
4917 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
4919 rtl8169_rx_missed(dev, ioaddr);
4921 spin_unlock_irq(&tp->lock);
4923 synchronize_irq(dev->irq);
4925 /* Give a racing hard_start_xmit a few cycles to complete. */
4926 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
4928 rtl8169_tx_clear(tp);
4930 rtl8169_rx_clear(tp);
4932 rtl_pll_power_down(tp);
4935 static int rtl8169_close(struct net_device *dev)
4937 struct rtl8169_private *tp = netdev_priv(dev);
4938 struct pci_dev *pdev = tp->pci_dev;
4940 pm_runtime_get_sync(&pdev->dev);
4942 /* update counters before going down */
4943 rtl8169_update_counters(dev);
4947 free_irq(dev->irq, dev);
4949 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4951 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4953 tp->TxDescArray = NULL;
4954 tp->RxDescArray = NULL;
4956 pm_runtime_put_sync(&pdev->dev);
4961 static void rtl_set_rx_mode(struct net_device *dev)
4963 struct rtl8169_private *tp = netdev_priv(dev);
4964 void __iomem *ioaddr = tp->mmio_addr;
4965 unsigned long flags;
4966 u32 mc_filter[2]; /* Multicast hash filter */
4970 if (dev->flags & IFF_PROMISC) {
4971 /* Unconditionally log net taps. */
4972 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4974 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4976 mc_filter[1] = mc_filter[0] = 0xffffffff;
4977 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4978 (dev->flags & IFF_ALLMULTI)) {
4979 /* Too many to filter perfectly -- accept all multicasts. */
4980 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4981 mc_filter[1] = mc_filter[0] = 0xffffffff;
4983 struct netdev_hw_addr *ha;
4985 rx_mode = AcceptBroadcast | AcceptMyPhys;
4986 mc_filter[1] = mc_filter[0] = 0;
4987 netdev_for_each_mc_addr(ha, dev) {
4988 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4989 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4990 rx_mode |= AcceptMulticast;
4994 spin_lock_irqsave(&tp->lock, flags);
4996 tmp = rtl8169_rx_config | rx_mode |
4997 (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
4999 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5000 u32 data = mc_filter[0];
5002 mc_filter[0] = swab32(mc_filter[1]);
5003 mc_filter[1] = swab32(data);
5006 RTL_W32(MAR0 + 4, mc_filter[1]);
5007 RTL_W32(MAR0 + 0, mc_filter[0]);
5009 RTL_W32(RxConfig, tmp);
5011 spin_unlock_irqrestore(&tp->lock, flags);
5015 * rtl8169_get_stats - Get rtl8169 read/write statistics
5016 * @dev: The Ethernet Device to get statistics for
5018 * Get TX/RX statistics for rtl8169
5020 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5022 struct rtl8169_private *tp = netdev_priv(dev);
5023 void __iomem *ioaddr = tp->mmio_addr;
5024 unsigned long flags;
5026 if (netif_running(dev)) {
5027 spin_lock_irqsave(&tp->lock, flags);
5028 rtl8169_rx_missed(dev, ioaddr);
5029 spin_unlock_irqrestore(&tp->lock, flags);
5035 static void rtl8169_net_suspend(struct net_device *dev)
5037 struct rtl8169_private *tp = netdev_priv(dev);
5039 if (!netif_running(dev))
5042 rtl_pll_power_down(tp);
5044 netif_device_detach(dev);
5045 netif_stop_queue(dev);
5050 static int rtl8169_suspend(struct device *device)
5052 struct pci_dev *pdev = to_pci_dev(device);
5053 struct net_device *dev = pci_get_drvdata(pdev);
5055 rtl8169_net_suspend(dev);
5060 static void __rtl8169_resume(struct net_device *dev)
5062 struct rtl8169_private *tp = netdev_priv(dev);
5064 netif_device_attach(dev);
5066 rtl_pll_power_up(tp);
5068 rtl8169_schedule_work(dev, rtl8169_reset_task);
5071 static int rtl8169_resume(struct device *device)
5073 struct pci_dev *pdev = to_pci_dev(device);
5074 struct net_device *dev = pci_get_drvdata(pdev);
5075 struct rtl8169_private *tp = netdev_priv(dev);
5077 rtl8169_init_phy(dev, tp);
5079 if (netif_running(dev))
5080 __rtl8169_resume(dev);
5085 static int rtl8169_runtime_suspend(struct device *device)
5087 struct pci_dev *pdev = to_pci_dev(device);
5088 struct net_device *dev = pci_get_drvdata(pdev);
5089 struct rtl8169_private *tp = netdev_priv(dev);
5091 if (!tp->TxDescArray)
5094 spin_lock_irq(&tp->lock);
5095 tp->saved_wolopts = __rtl8169_get_wol(tp);
5096 __rtl8169_set_wol(tp, WAKE_ANY);
5097 spin_unlock_irq(&tp->lock);
5099 rtl8169_net_suspend(dev);
5104 static int rtl8169_runtime_resume(struct device *device)
5106 struct pci_dev *pdev = to_pci_dev(device);
5107 struct net_device *dev = pci_get_drvdata(pdev);
5108 struct rtl8169_private *tp = netdev_priv(dev);
5110 if (!tp->TxDescArray)
5113 spin_lock_irq(&tp->lock);
5114 __rtl8169_set_wol(tp, tp->saved_wolopts);
5115 tp->saved_wolopts = 0;
5116 spin_unlock_irq(&tp->lock);
5118 rtl8169_init_phy(dev, tp);
5120 __rtl8169_resume(dev);
5125 static int rtl8169_runtime_idle(struct device *device)
5127 struct pci_dev *pdev = to_pci_dev(device);
5128 struct net_device *dev = pci_get_drvdata(pdev);
5129 struct rtl8169_private *tp = netdev_priv(dev);
5131 return tp->TxDescArray ? -EBUSY : 0;
5134 static const struct dev_pm_ops rtl8169_pm_ops = {
5135 .suspend = rtl8169_suspend,
5136 .resume = rtl8169_resume,
5137 .freeze = rtl8169_suspend,
5138 .thaw = rtl8169_resume,
5139 .poweroff = rtl8169_suspend,
5140 .restore = rtl8169_resume,
5141 .runtime_suspend = rtl8169_runtime_suspend,
5142 .runtime_resume = rtl8169_runtime_resume,
5143 .runtime_idle = rtl8169_runtime_idle,
5146 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
5148 #else /* !CONFIG_PM */
5150 #define RTL8169_PM_OPS NULL
5152 #endif /* !CONFIG_PM */
5154 static void rtl_shutdown(struct pci_dev *pdev)
5156 struct net_device *dev = pci_get_drvdata(pdev);
5157 struct rtl8169_private *tp = netdev_priv(dev);
5158 void __iomem *ioaddr = tp->mmio_addr;
5160 rtl8169_net_suspend(dev);
5162 /* restore original MAC address */
5163 rtl_rar_set(tp, dev->perm_addr);
5165 spin_lock_irq(&tp->lock);
5167 rtl8169_asic_down(ioaddr);
5169 spin_unlock_irq(&tp->lock);
5171 if (system_state == SYSTEM_POWER_OFF) {
5172 /* WoL fails with some 8168 when the receiver is disabled. */
5173 if (tp->features & RTL_FEATURE_WOL) {
5174 pci_clear_master(pdev);
5176 RTL_W8(ChipCmd, CmdRxEnb);
5181 pci_wake_from_d3(pdev, true);
5182 pci_set_power_state(pdev, PCI_D3hot);
5186 static struct pci_driver rtl8169_pci_driver = {
5188 .id_table = rtl8169_pci_tbl,
5189 .probe = rtl8169_init_one,
5190 .remove = __devexit_p(rtl8169_remove_one),
5191 .shutdown = rtl_shutdown,
5192 .driver.pm = RTL8169_PM_OPS,
5195 static int __init rtl8169_init_module(void)
5197 return pci_register_driver(&rtl8169_pci_driver);
5200 static void __exit rtl8169_cleanup_module(void)
5202 pci_unregister_driver(&rtl8169_pci_driver);
5205 module_init(rtl8169_init_module);
5206 module_exit(rtl8169_cleanup_module);