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>
29 #include <asm/system.h>
33 #define RTL8169_VERSION "2.3LK-NAPI"
34 #define MODULENAME "r8169"
35 #define PFX MODULENAME ": "
37 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
38 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
39 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
42 #define assert(expr) \
44 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
45 #expr,__FILE__,__func__,__LINE__); \
47 #define dprintk(fmt, args...) \
48 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
50 #define assert(expr) do {} while (0)
51 #define dprintk(fmt, args...) do {} while (0)
52 #endif /* RTL8169_DEBUG */
54 #define R8169_MSG_DEFAULT \
55 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
57 #define TX_BUFFS_AVAIL(tp) \
58 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
60 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
61 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
62 static const int multicast_filter_limit = 32;
64 /* MAC address length */
65 #define MAC_ADDR_LEN 6
67 #define MAX_READ_REQUEST_SHIFT 12
68 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
69 #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
70 #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
71 #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
72 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
74 #define R8169_REGS_SIZE 256
75 #define R8169_NAPI_WEIGHT 64
76 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
77 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
78 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
79 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
80 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
82 #define RTL8169_TX_TIMEOUT (6*HZ)
83 #define RTL8169_PHY_TIMEOUT (10*HZ)
85 #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
86 #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
87 #define RTL_EEPROM_SIG_ADDR 0x0000
89 /* write/read MMIO register */
90 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
91 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
92 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
93 #define RTL_R8(reg) readb (ioaddr + (reg))
94 #define RTL_R16(reg) readw (ioaddr + (reg))
95 #define RTL_R32(reg) readl (ioaddr + (reg))
98 RTL_GIGA_MAC_NONE = 0x00,
99 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
100 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
101 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
102 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
103 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
104 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
105 RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
106 RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
107 RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
108 RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
109 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
110 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
111 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
112 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
113 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
114 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
115 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
116 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
117 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
118 RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
119 RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
120 RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
121 RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
122 RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
123 RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
124 RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
125 RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
126 RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
127 RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
128 RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
131 #define _R(NAME,MAC,MASK) \
132 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
134 static const struct {
137 u32 RxConfigMask; /* Clears the bits supported by this chip */
138 } rtl_chip_info[] = {
139 _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
140 _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
141 _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
142 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
143 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
144 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
145 _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
146 _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
147 _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
148 _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
149 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
150 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
151 _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
152 _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
153 _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
154 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
155 _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
156 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
157 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
158 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
159 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
160 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
161 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
162 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
163 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880), // PCI-E
164 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, 0xff7e1880), // PCI-E
165 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, 0xff7e1880), // PCI-E
166 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, 0xff7e1880), // PCI-E
167 _R("RTL8105e", RTL_GIGA_MAC_VER_29, 0xff7e1880), // PCI-E
168 _R("RTL8105e", RTL_GIGA_MAC_VER_30, 0xff7e1880) // PCI-E
178 static void rtl_hw_start_8169(struct net_device *);
179 static void rtl_hw_start_8168(struct net_device *);
180 static void rtl_hw_start_8101(struct net_device *);
182 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
183 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
184 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
185 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
186 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
187 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
188 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
189 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
190 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
191 { PCI_VENDOR_ID_LINKSYS, 0x1032,
192 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
194 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
198 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
200 static int rx_buf_sz = 16383;
207 MAC0 = 0, /* Ethernet hardware address. */
209 MAR0 = 8, /* Multicast filter. */
210 CounterAddrLow = 0x10,
211 CounterAddrHigh = 0x14,
212 TxDescStartAddrLow = 0x20,
213 TxDescStartAddrHigh = 0x24,
214 TxHDescStartAddrLow = 0x28,
215 TxHDescStartAddrHigh = 0x2c,
238 RxDescAddrLow = 0xe4,
239 RxDescAddrHigh = 0xe8,
240 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
242 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
244 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
246 #define TxPacketMax (8064 >> 7)
249 FuncEventMask = 0xf4,
250 FuncPresetState = 0xf8,
251 FuncForceEvent = 0xfc,
254 enum rtl8110_registers {
260 enum rtl8168_8101_registers {
263 #define CSIAR_FLAG 0x80000000
264 #define CSIAR_WRITE_CMD 0x80000000
265 #define CSIAR_BYTE_ENABLE 0x0f
266 #define CSIAR_BYTE_ENABLE_SHIFT 12
267 #define CSIAR_ADDR_MASK 0x0fff
270 #define EPHYAR_FLAG 0x80000000
271 #define EPHYAR_WRITE_CMD 0x80000000
272 #define EPHYAR_REG_MASK 0x1f
273 #define EPHYAR_REG_SHIFT 16
274 #define EPHYAR_DATA_MASK 0xffff
276 #define PM_SWITCH (1 << 6)
278 #define FIX_NAK_1 (1 << 4)
279 #define FIX_NAK_2 (1 << 3)
282 #define EN_NDP (1 << 3)
283 #define EN_OOB_RESET (1 << 2)
285 #define EFUSEAR_FLAG 0x80000000
286 #define EFUSEAR_WRITE_CMD 0x80000000
287 #define EFUSEAR_READ_CMD 0x00000000
288 #define EFUSEAR_REG_MASK 0x03ff
289 #define EFUSEAR_REG_SHIFT 8
290 #define EFUSEAR_DATA_MASK 0xff
293 enum rtl8168_registers {
296 #define ERIAR_FLAG 0x80000000
297 #define ERIAR_WRITE_CMD 0x80000000
298 #define ERIAR_READ_CMD 0x00000000
299 #define ERIAR_ADDR_BYTE_ALIGN 4
300 #define ERIAR_EXGMAC 0
303 #define ERIAR_TYPE_SHIFT 16
304 #define ERIAR_BYTEEN 0x0f
305 #define ERIAR_BYTEEN_SHIFT 12
306 EPHY_RXER_NUM = 0x7c,
307 OCPDR = 0xb0, /* OCP GPHY access */
308 #define OCPDR_WRITE_CMD 0x80000000
309 #define OCPDR_READ_CMD 0x00000000
310 #define OCPDR_REG_MASK 0x7f
311 #define OCPDR_GPHY_REG_SHIFT 16
312 #define OCPDR_DATA_MASK 0xffff
314 #define OCPAR_FLAG 0x80000000
315 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
316 #define OCPAR_GPHY_READ_CMD 0x0000f060
317 RDSAR1 = 0xd0 /* 8168c only. Undocumented on 8168dp */
320 enum rtl_register_content {
321 /* InterruptStatusBits */
325 TxDescUnavail = 0x0080,
347 /* TXPoll register p.5 */
348 HPQ = 0x80, /* Poll cmd on the high prio queue */
349 NPQ = 0x40, /* Poll cmd on the low prio queue */
350 FSWInt = 0x01, /* Forced software interrupt */
354 Cfg9346_Unlock = 0xc0,
359 AcceptBroadcast = 0x08,
360 AcceptMulticast = 0x04,
362 AcceptAllPhys = 0x01,
369 TxInterFrameGapShift = 24,
370 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
372 /* Config1 register p.24 */
375 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
376 Speed_down = (1 << 4),
380 PMEnable = (1 << 0), /* Power Management Enable */
382 /* Config2 register p. 25 */
383 PCI_Clock_66MHz = 0x01,
384 PCI_Clock_33MHz = 0x00,
386 /* Config3 register p.25 */
387 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
388 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
389 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
391 /* Config5 register p.27 */
392 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
393 MWF = (1 << 5), /* Accept Multicast wakeup frame */
394 UWF = (1 << 4), /* Accept Unicast wakeup frame */
395 LanWake = (1 << 1), /* LanWake enable/disable */
396 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
399 TBIReset = 0x80000000,
400 TBILoopback = 0x40000000,
401 TBINwEnable = 0x20000000,
402 TBINwRestart = 0x10000000,
403 TBILinkOk = 0x02000000,
404 TBINwComplete = 0x01000000,
407 EnableBist = (1 << 15), // 8168 8101
408 Mac_dbgo_oe = (1 << 14), // 8168 8101
409 Normal_mode = (1 << 13), // unused
410 Force_half_dup = (1 << 12), // 8168 8101
411 Force_rxflow_en = (1 << 11), // 8168 8101
412 Force_txflow_en = (1 << 10), // 8168 8101
413 Cxpl_dbg_sel = (1 << 9), // 8168 8101
414 ASF = (1 << 8), // 8168 8101
415 PktCntrDisable = (1 << 7), // 8168 8101
416 Mac_dbgo_sel = 0x001c, // 8168
421 INTT_0 = 0x0000, // 8168
422 INTT_1 = 0x0001, // 8168
423 INTT_2 = 0x0002, // 8168
424 INTT_3 = 0x0003, // 8168
426 /* rtl8169_PHYstatus */
437 TBILinkOK = 0x02000000,
439 /* DumpCounterCommand */
443 enum desc_status_bit {
444 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
445 RingEnd = (1 << 30), /* End of descriptor ring */
446 FirstFrag = (1 << 29), /* First segment of a packet */
447 LastFrag = (1 << 28), /* Final segment of a packet */
450 LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
451 MSSShift = 16, /* MSS value position */
452 MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
453 IPCS = (1 << 18), /* Calculate IP checksum */
454 UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
455 TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
456 TxVlanTag = (1 << 17), /* Add VLAN tag */
459 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
460 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
462 #define RxProtoUDP (PID1)
463 #define RxProtoTCP (PID0)
464 #define RxProtoIP (PID1 | PID0)
465 #define RxProtoMask RxProtoIP
467 IPFail = (1 << 16), /* IP checksum failed */
468 UDPFail = (1 << 15), /* UDP/IP checksum failed */
469 TCPFail = (1 << 14), /* TCP/IP checksum failed */
470 RxVlanTag = (1 << 16), /* VLAN tag available */
473 #define RsvdMask 0x3fffc000
490 u8 __pad[sizeof(void *) - sizeof(u32)];
494 RTL_FEATURE_WOL = (1 << 0),
495 RTL_FEATURE_MSI = (1 << 1),
496 RTL_FEATURE_GMII = (1 << 2),
499 struct rtl8169_counters {
506 __le32 tx_one_collision;
507 __le32 tx_multi_collision;
515 struct rtl8169_private {
516 void __iomem *mmio_addr; /* memory map physical address */
517 struct pci_dev *pci_dev; /* Index of PCI device */
518 struct net_device *dev;
519 struct napi_struct napi;
520 spinlock_t lock; /* spin lock flag */
524 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
525 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
528 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
529 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
530 dma_addr_t TxPhyAddr;
531 dma_addr_t RxPhyAddr;
532 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
533 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
534 struct timer_list timer;
539 int phy_1000_ctrl_reg;
540 #ifdef CONFIG_R8169_VLAN
541 struct vlan_group *vlgrp;
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 autoneg, u16 speed, u8 duplex);
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(void __iomem *ioaddr, u8 cmd)
637 RTL_W32(ERIAR, 0x800010e8);
639 for (i = 0; i < 5; i++) {
641 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
645 ocp_write(ioaddr, 0x1, 0x30, 0x00000001);
648 #define OOB_CMD_RESET 0x00
649 #define OOB_CMD_DRIVER_START 0x05
650 #define OOB_CMD_DRIVER_STOP 0x06
652 static void rtl8168_driver_start(struct rtl8169_private *tp)
656 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
658 for (i = 0; i < 10; i++) {
660 if (ocp_read(tp, 0x0f, 0x0010) & 0x00000800)
665 static void rtl8168_driver_stop(struct rtl8169_private *tp)
669 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
671 for (i = 0; i < 10; i++) {
673 if ((ocp_read(tp, 0x0f, 0x0010) & 0x00000800) == 0)
679 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
683 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
685 for (i = 20; i > 0; i--) {
687 * Check if the RTL8169 has completed writing to the specified
690 if (!(RTL_R32(PHYAR) & 0x80000000))
695 * According to hardware specs a 20us delay is required after write
696 * complete indication, but before sending next command.
701 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
705 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
707 for (i = 20; i > 0; i--) {
709 * Check if the RTL8169 has completed retrieving data from
710 * the specified MII register.
712 if (RTL_R32(PHYAR) & 0x80000000) {
713 value = RTL_R32(PHYAR) & 0xffff;
719 * According to hardware specs a 20us delay is required after read
720 * complete indication, but before sending next command.
727 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
731 RTL_W32(OCPDR, data |
732 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
733 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
734 RTL_W32(EPHY_RXER_NUM, 0);
736 for (i = 0; i < 100; i++) {
738 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
743 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
745 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
746 (value & OCPDR_DATA_MASK));
749 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
753 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
756 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
757 RTL_W32(EPHY_RXER_NUM, 0);
759 for (i = 0; i < 100; i++) {
761 if (RTL_R32(OCPAR) & OCPAR_FLAG)
765 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
768 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
770 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
772 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
775 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
777 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
780 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
782 r8168dp_2_mdio_start(ioaddr);
784 r8169_mdio_write(ioaddr, reg_addr, value);
786 r8168dp_2_mdio_stop(ioaddr);
789 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
793 r8168dp_2_mdio_start(ioaddr);
795 value = r8169_mdio_read(ioaddr, reg_addr);
797 r8168dp_2_mdio_stop(ioaddr);
802 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
804 tp->mdio_ops.write(tp->mmio_addr, location, val);
807 static int rtl_readphy(struct rtl8169_private *tp, int location)
809 return tp->mdio_ops.read(tp->mmio_addr, location);
812 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
814 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
817 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
821 val = rtl_readphy(tp, reg_addr);
822 rtl_writephy(tp, reg_addr, (val | p) & ~m);
825 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
828 struct rtl8169_private *tp = netdev_priv(dev);
830 rtl_writephy(tp, location, val);
833 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
835 struct rtl8169_private *tp = netdev_priv(dev);
837 return rtl_readphy(tp, location);
840 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
844 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
845 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
847 for (i = 0; i < 100; i++) {
848 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
854 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
859 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
861 for (i = 0; i < 100; i++) {
862 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
863 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
872 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
876 RTL_W32(CSIDR, value);
877 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
878 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
880 for (i = 0; i < 100; i++) {
881 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
887 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
892 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
893 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
895 for (i = 0; i < 100; i++) {
896 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
897 value = RTL_R32(CSIDR);
906 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
911 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
913 for (i = 0; i < 300; i++) {
914 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
915 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
924 static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
926 RTL_W16(IntrMask, 0x0000);
928 RTL_W16(IntrStatus, 0xffff);
931 static void rtl8169_asic_down(void __iomem *ioaddr)
933 RTL_W8(ChipCmd, 0x00);
934 rtl8169_irq_mask_and_ack(ioaddr);
938 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
940 void __iomem *ioaddr = tp->mmio_addr;
942 return RTL_R32(TBICSR) & TBIReset;
945 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
947 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
950 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
952 return RTL_R32(TBICSR) & TBILinkOk;
955 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
957 return RTL_R8(PHYstatus) & LinkStatus;
960 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
962 void __iomem *ioaddr = tp->mmio_addr;
964 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
967 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
971 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
972 rtl_writephy(tp, MII_BMCR, val & 0xffff);
975 static void __rtl8169_check_link_status(struct net_device *dev,
976 struct rtl8169_private *tp,
977 void __iomem *ioaddr,
982 spin_lock_irqsave(&tp->lock, flags);
983 if (tp->link_ok(ioaddr)) {
984 /* This is to cancel a scheduled suspend if there's one. */
986 pm_request_resume(&tp->pci_dev->dev);
987 netif_carrier_on(dev);
989 netif_info(tp, ifup, dev, "link up\n");
991 netif_carrier_off(dev);
992 netif_info(tp, ifdown, dev, "link down\n");
994 pm_schedule_suspend(&tp->pci_dev->dev, 100);
996 spin_unlock_irqrestore(&tp->lock, flags);
999 static void rtl8169_check_link_status(struct net_device *dev,
1000 struct rtl8169_private *tp,
1001 void __iomem *ioaddr)
1003 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1006 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1008 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1010 void __iomem *ioaddr = tp->mmio_addr;
1014 options = RTL_R8(Config1);
1015 if (!(options & PMEnable))
1018 options = RTL_R8(Config3);
1019 if (options & LinkUp)
1020 wolopts |= WAKE_PHY;
1021 if (options & MagicPacket)
1022 wolopts |= WAKE_MAGIC;
1024 options = RTL_R8(Config5);
1026 wolopts |= WAKE_UCAST;
1028 wolopts |= WAKE_BCAST;
1030 wolopts |= WAKE_MCAST;
1035 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1037 struct rtl8169_private *tp = netdev_priv(dev);
1039 spin_lock_irq(&tp->lock);
1041 wol->supported = WAKE_ANY;
1042 wol->wolopts = __rtl8169_get_wol(tp);
1044 spin_unlock_irq(&tp->lock);
1047 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1049 void __iomem *ioaddr = tp->mmio_addr;
1051 static const struct {
1056 { WAKE_ANY, Config1, PMEnable },
1057 { WAKE_PHY, Config3, LinkUp },
1058 { WAKE_MAGIC, Config3, MagicPacket },
1059 { WAKE_UCAST, Config5, UWF },
1060 { WAKE_BCAST, Config5, BWF },
1061 { WAKE_MCAST, Config5, MWF },
1062 { WAKE_ANY, Config5, LanWake }
1065 RTL_W8(Cfg9346, Cfg9346_Unlock);
1067 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1068 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1069 if (wolopts & cfg[i].opt)
1070 options |= cfg[i].mask;
1071 RTL_W8(cfg[i].reg, options);
1074 RTL_W8(Cfg9346, Cfg9346_Lock);
1077 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1079 struct rtl8169_private *tp = netdev_priv(dev);
1081 spin_lock_irq(&tp->lock);
1084 tp->features |= RTL_FEATURE_WOL;
1086 tp->features &= ~RTL_FEATURE_WOL;
1087 __rtl8169_set_wol(tp, wol->wolopts);
1088 spin_unlock_irq(&tp->lock);
1090 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1095 static void rtl8169_get_drvinfo(struct net_device *dev,
1096 struct ethtool_drvinfo *info)
1098 struct rtl8169_private *tp = netdev_priv(dev);
1100 strcpy(info->driver, MODULENAME);
1101 strcpy(info->version, RTL8169_VERSION);
1102 strcpy(info->bus_info, pci_name(tp->pci_dev));
1105 static int rtl8169_get_regs_len(struct net_device *dev)
1107 return R8169_REGS_SIZE;
1110 static int rtl8169_set_speed_tbi(struct net_device *dev,
1111 u8 autoneg, u16 speed, u8 duplex)
1113 struct rtl8169_private *tp = netdev_priv(dev);
1114 void __iomem *ioaddr = tp->mmio_addr;
1118 reg = RTL_R32(TBICSR);
1119 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1120 (duplex == DUPLEX_FULL)) {
1121 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1122 } else if (autoneg == AUTONEG_ENABLE)
1123 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1125 netif_warn(tp, link, dev,
1126 "incorrect speed setting refused in TBI mode\n");
1133 static int rtl8169_set_speed_xmii(struct net_device *dev,
1134 u8 autoneg, u16 speed, u8 duplex)
1136 struct rtl8169_private *tp = netdev_priv(dev);
1137 int giga_ctrl, bmcr;
1139 rtl_writephy(tp, 0x1f, 0x0000);
1141 if (autoneg == AUTONEG_ENABLE) {
1144 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1145 auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
1146 ADVERTISE_100HALF | ADVERTISE_100FULL);
1147 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1149 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1150 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1152 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1153 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1154 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1155 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1156 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1157 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1158 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1159 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
1160 (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
1161 (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
1162 (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
1163 giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
1165 netif_info(tp, link, dev,
1166 "PHY does not support 1000Mbps\n");
1169 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1171 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1172 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1176 if (speed == SPEED_10)
1178 else if (speed == SPEED_100)
1179 bmcr = BMCR_SPEED100;
1183 if (duplex == DUPLEX_FULL)
1184 bmcr |= BMCR_FULLDPLX;
1187 tp->phy_1000_ctrl_reg = giga_ctrl;
1189 rtl_writephy(tp, MII_BMCR, bmcr);
1191 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
1192 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
1193 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1194 rtl_writephy(tp, 0x17, 0x2138);
1195 rtl_writephy(tp, 0x0e, 0x0260);
1197 rtl_writephy(tp, 0x17, 0x2108);
1198 rtl_writephy(tp, 0x0e, 0x0000);
1205 static int rtl8169_set_speed(struct net_device *dev,
1206 u8 autoneg, u16 speed, u8 duplex)
1208 struct rtl8169_private *tp = netdev_priv(dev);
1211 ret = tp->set_speed(dev, autoneg, speed, duplex);
1213 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
1214 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1219 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1221 struct rtl8169_private *tp = netdev_priv(dev);
1222 unsigned long flags;
1225 spin_lock_irqsave(&tp->lock, flags);
1226 ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
1227 spin_unlock_irqrestore(&tp->lock, flags);
1232 static u32 rtl8169_get_rx_csum(struct net_device *dev)
1234 struct rtl8169_private *tp = netdev_priv(dev);
1236 return tp->cp_cmd & RxChkSum;
1239 static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
1241 struct rtl8169_private *tp = netdev_priv(dev);
1242 void __iomem *ioaddr = tp->mmio_addr;
1243 unsigned long flags;
1245 spin_lock_irqsave(&tp->lock, flags);
1248 tp->cp_cmd |= RxChkSum;
1250 tp->cp_cmd &= ~RxChkSum;
1252 RTL_W16(CPlusCmd, tp->cp_cmd);
1255 spin_unlock_irqrestore(&tp->lock, flags);
1260 #ifdef CONFIG_R8169_VLAN
1262 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1263 struct sk_buff *skb)
1265 return (vlan_tx_tag_present(skb)) ?
1266 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1269 static void rtl8169_vlan_rx_register(struct net_device *dev,
1270 struct vlan_group *grp)
1272 struct rtl8169_private *tp = netdev_priv(dev);
1273 void __iomem *ioaddr = tp->mmio_addr;
1274 unsigned long flags;
1276 spin_lock_irqsave(&tp->lock, flags);
1279 * Do not disable RxVlan on 8110SCd.
1281 if (tp->vlgrp || (tp->mac_version == RTL_GIGA_MAC_VER_05))
1282 tp->cp_cmd |= RxVlan;
1284 tp->cp_cmd &= ~RxVlan;
1285 RTL_W16(CPlusCmd, tp->cp_cmd);
1287 spin_unlock_irqrestore(&tp->lock, flags);
1290 static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
1291 struct sk_buff *skb, int polling)
1293 u32 opts2 = le32_to_cpu(desc->opts2);
1294 struct vlan_group *vlgrp = tp->vlgrp;
1297 if (vlgrp && (opts2 & RxVlanTag)) {
1298 u16 vtag = swab16(opts2 & 0xffff);
1300 if (likely(polling))
1301 vlan_gro_receive(&tp->napi, vlgrp, vtag, skb);
1303 __vlan_hwaccel_rx(skb, vlgrp, vtag, polling);
1311 #else /* !CONFIG_R8169_VLAN */
1313 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1314 struct sk_buff *skb)
1319 static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
1320 struct sk_buff *skb, int polling)
1327 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1329 struct rtl8169_private *tp = netdev_priv(dev);
1330 void __iomem *ioaddr = tp->mmio_addr;
1334 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1335 cmd->port = PORT_FIBRE;
1336 cmd->transceiver = XCVR_INTERNAL;
1338 status = RTL_R32(TBICSR);
1339 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1340 cmd->autoneg = !!(status & TBINwEnable);
1342 cmd->speed = SPEED_1000;
1343 cmd->duplex = DUPLEX_FULL; /* Always set */
1348 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1350 struct rtl8169_private *tp = netdev_priv(dev);
1352 return mii_ethtool_gset(&tp->mii, cmd);
1355 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1357 struct rtl8169_private *tp = netdev_priv(dev);
1358 unsigned long flags;
1361 spin_lock_irqsave(&tp->lock, flags);
1363 rc = tp->get_settings(dev, cmd);
1365 spin_unlock_irqrestore(&tp->lock, flags);
1369 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1372 struct rtl8169_private *tp = netdev_priv(dev);
1373 unsigned long flags;
1375 if (regs->len > R8169_REGS_SIZE)
1376 regs->len = R8169_REGS_SIZE;
1378 spin_lock_irqsave(&tp->lock, flags);
1379 memcpy_fromio(p, tp->mmio_addr, regs->len);
1380 spin_unlock_irqrestore(&tp->lock, flags);
1383 static u32 rtl8169_get_msglevel(struct net_device *dev)
1385 struct rtl8169_private *tp = netdev_priv(dev);
1387 return tp->msg_enable;
1390 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1392 struct rtl8169_private *tp = netdev_priv(dev);
1394 tp->msg_enable = value;
1397 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1404 "tx_single_collisions",
1405 "tx_multi_collisions",
1413 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1417 return ARRAY_SIZE(rtl8169_gstrings);
1423 static void rtl8169_update_counters(struct net_device *dev)
1425 struct rtl8169_private *tp = netdev_priv(dev);
1426 void __iomem *ioaddr = tp->mmio_addr;
1427 struct rtl8169_counters *counters;
1431 struct device *d = &tp->pci_dev->dev;
1434 * Some chips are unable to dump tally counters when the receiver
1437 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1440 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1444 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1445 cmd = (u64)paddr & DMA_BIT_MASK(32);
1446 RTL_W32(CounterAddrLow, cmd);
1447 RTL_W32(CounterAddrLow, cmd | CounterDump);
1450 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1451 /* copy updated counters */
1452 memcpy(&tp->counters, counters, sizeof(*counters));
1458 RTL_W32(CounterAddrLow, 0);
1459 RTL_W32(CounterAddrHigh, 0);
1461 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1464 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1465 struct ethtool_stats *stats, u64 *data)
1467 struct rtl8169_private *tp = netdev_priv(dev);
1471 rtl8169_update_counters(dev);
1473 data[0] = le64_to_cpu(tp->counters.tx_packets);
1474 data[1] = le64_to_cpu(tp->counters.rx_packets);
1475 data[2] = le64_to_cpu(tp->counters.tx_errors);
1476 data[3] = le32_to_cpu(tp->counters.rx_errors);
1477 data[4] = le16_to_cpu(tp->counters.rx_missed);
1478 data[5] = le16_to_cpu(tp->counters.align_errors);
1479 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1480 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1481 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1482 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1483 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1484 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1485 data[12] = le16_to_cpu(tp->counters.tx_underun);
1488 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1492 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1497 static const struct ethtool_ops rtl8169_ethtool_ops = {
1498 .get_drvinfo = rtl8169_get_drvinfo,
1499 .get_regs_len = rtl8169_get_regs_len,
1500 .get_link = ethtool_op_get_link,
1501 .get_settings = rtl8169_get_settings,
1502 .set_settings = rtl8169_set_settings,
1503 .get_msglevel = rtl8169_get_msglevel,
1504 .set_msglevel = rtl8169_set_msglevel,
1505 .get_rx_csum = rtl8169_get_rx_csum,
1506 .set_rx_csum = rtl8169_set_rx_csum,
1507 .set_tx_csum = ethtool_op_set_tx_csum,
1508 .set_sg = ethtool_op_set_sg,
1509 .set_tso = ethtool_op_set_tso,
1510 .get_regs = rtl8169_get_regs,
1511 .get_wol = rtl8169_get_wol,
1512 .set_wol = rtl8169_set_wol,
1513 .get_strings = rtl8169_get_strings,
1514 .get_sset_count = rtl8169_get_sset_count,
1515 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1518 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1519 void __iomem *ioaddr)
1522 * The driver currently handles the 8168Bf and the 8168Be identically
1523 * but they can be identified more specifically through the test below
1526 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1528 * Same thing for the 8101Eb and the 8101Ec:
1530 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1532 static const struct {
1538 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1539 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1540 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1542 /* 8168DP family. */
1543 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1544 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1547 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1548 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1549 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1550 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1551 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1552 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1553 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1554 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1555 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1558 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1559 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1560 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1561 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1564 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1565 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1566 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1567 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1568 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1569 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1570 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1571 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1572 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1573 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1574 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1575 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1576 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1577 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1578 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1579 /* FIXME: where did these entries come from ? -- FR */
1580 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1581 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1584 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1585 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1586 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1587 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1588 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1589 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1592 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1596 reg = RTL_R32(TxConfig);
1597 while ((reg & p->mask) != p->val)
1599 tp->mac_version = p->mac_version;
1602 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1604 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1612 static void rtl_writephy_batch(struct rtl8169_private *tp,
1613 const struct phy_reg *regs, int len)
1616 rtl_writephy(tp, regs->reg, regs->val);
1621 #define PHY_READ 0x00000000
1622 #define PHY_DATA_OR 0x10000000
1623 #define PHY_DATA_AND 0x20000000
1624 #define PHY_BJMPN 0x30000000
1625 #define PHY_READ_EFUSE 0x40000000
1626 #define PHY_READ_MAC_BYTE 0x50000000
1627 #define PHY_WRITE_MAC_BYTE 0x60000000
1628 #define PHY_CLEAR_READCOUNT 0x70000000
1629 #define PHY_WRITE 0x80000000
1630 #define PHY_READCOUNT_EQ_SKIP 0x90000000
1631 #define PHY_COMP_EQ_SKIPN 0xa0000000
1632 #define PHY_COMP_NEQ_SKIPN 0xb0000000
1633 #define PHY_WRITE_PREVIOUS 0xc0000000
1634 #define PHY_SKIPN 0xd0000000
1635 #define PHY_DELAY_MS 0xe0000000
1636 #define PHY_WRITE_ERI_WORD 0xf0000000
1639 rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1641 __le32 *phytable = (__le32 *)fw->data;
1642 struct net_device *dev = tp->dev;
1643 size_t index, fw_size = fw->size / sizeof(*phytable);
1646 if (fw->size % sizeof(*phytable)) {
1647 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1651 for (index = 0; index < fw_size; index++) {
1652 u32 action = le32_to_cpu(phytable[index]);
1653 u32 regno = (action & 0x0fff0000) >> 16;
1655 switch(action & 0xf0000000) {
1659 case PHY_READ_EFUSE:
1660 case PHY_CLEAR_READCOUNT:
1662 case PHY_WRITE_PREVIOUS:
1667 if (regno > index) {
1668 netif_err(tp, probe, tp->dev,
1669 "Out of range of firmware\n");
1673 case PHY_READCOUNT_EQ_SKIP:
1674 if (index + 2 >= fw_size) {
1675 netif_err(tp, probe, tp->dev,
1676 "Out of range of firmware\n");
1680 case PHY_COMP_EQ_SKIPN:
1681 case PHY_COMP_NEQ_SKIPN:
1683 if (index + 1 + regno >= fw_size) {
1684 netif_err(tp, probe, tp->dev,
1685 "Out of range of firmware\n");
1690 case PHY_READ_MAC_BYTE:
1691 case PHY_WRITE_MAC_BYTE:
1692 case PHY_WRITE_ERI_WORD:
1694 netif_err(tp, probe, tp->dev,
1695 "Invalid action 0x%08x\n", action);
1703 for (index = 0; index < fw_size; ) {
1704 u32 action = le32_to_cpu(phytable[index]);
1705 u32 data = action & 0x0000ffff;
1706 u32 regno = (action & 0x0fff0000) >> 16;
1711 switch(action & 0xf0000000) {
1713 predata = rtl_readphy(tp, regno);
1728 case PHY_READ_EFUSE:
1729 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1732 case PHY_CLEAR_READCOUNT:
1737 rtl_writephy(tp, regno, data);
1740 case PHY_READCOUNT_EQ_SKIP:
1746 case PHY_COMP_EQ_SKIPN:
1747 if (predata == data)
1751 case PHY_COMP_NEQ_SKIPN:
1752 if (predata != data)
1756 case PHY_WRITE_PREVIOUS:
1757 rtl_writephy(tp, regno, predata);
1768 case PHY_READ_MAC_BYTE:
1769 case PHY_WRITE_MAC_BYTE:
1770 case PHY_WRITE_ERI_WORD:
1777 static void rtl_release_firmware(struct rtl8169_private *tp)
1779 release_firmware(tp->fw);
1783 static int rtl_apply_firmware(struct rtl8169_private *tp, const char *fw_name)
1785 const struct firmware **fw = &tp->fw;
1789 rc = request_firmware(fw, fw_name, &tp->pci_dev->dev);
1794 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1795 rtl_phy_write_fw(tp, *fw);
1800 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
1802 static const struct phy_reg phy_reg_init[] = {
1864 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1867 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
1869 static const struct phy_reg phy_reg_init[] = {
1875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1878 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
1880 struct pci_dev *pdev = tp->pci_dev;
1881 u16 vendor_id, device_id;
1883 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1884 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1886 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1889 rtl_writephy(tp, 0x1f, 0x0001);
1890 rtl_writephy(tp, 0x10, 0xf01b);
1891 rtl_writephy(tp, 0x1f, 0x0000);
1894 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
1896 static const struct phy_reg phy_reg_init[] = {
1936 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1938 rtl8169scd_hw_phy_config_quirk(tp);
1941 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
1943 static const struct phy_reg phy_reg_init[] = {
1991 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1994 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
1996 static const struct phy_reg phy_reg_init[] = {
2001 rtl_writephy(tp, 0x1f, 0x0001);
2002 rtl_patchphy(tp, 0x16, 1 << 0);
2004 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2007 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2009 static const struct phy_reg phy_reg_init[] = {
2015 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2018 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2020 static const struct phy_reg phy_reg_init[] = {
2028 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2031 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2033 static const struct phy_reg phy_reg_init[] = {
2039 rtl_writephy(tp, 0x1f, 0x0000);
2040 rtl_patchphy(tp, 0x14, 1 << 5);
2041 rtl_patchphy(tp, 0x0d, 1 << 5);
2043 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2046 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2048 static const struct phy_reg phy_reg_init[] = {
2068 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2070 rtl_patchphy(tp, 0x14, 1 << 5);
2071 rtl_patchphy(tp, 0x0d, 1 << 5);
2072 rtl_writephy(tp, 0x1f, 0x0000);
2075 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2077 static const struct phy_reg phy_reg_init[] = {
2095 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2097 rtl_patchphy(tp, 0x16, 1 << 0);
2098 rtl_patchphy(tp, 0x14, 1 << 5);
2099 rtl_patchphy(tp, 0x0d, 1 << 5);
2100 rtl_writephy(tp, 0x1f, 0x0000);
2103 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2105 static const struct phy_reg phy_reg_init[] = {
2117 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2119 rtl_patchphy(tp, 0x16, 1 << 0);
2120 rtl_patchphy(tp, 0x14, 1 << 5);
2121 rtl_patchphy(tp, 0x0d, 1 << 5);
2122 rtl_writephy(tp, 0x1f, 0x0000);
2125 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2127 rtl8168c_3_hw_phy_config(tp);
2130 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2132 static const struct phy_reg phy_reg_init_0[] = {
2133 /* Channel Estimation */
2154 * enhance line driver power
2163 * Can not link to 1Gbps with bad cable
2164 * Decrease SNR threshold form 21.07dB to 19.04dB
2172 void __iomem *ioaddr = tp->mmio_addr;
2174 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2178 * Fine Tune Switching regulator parameter
2180 rtl_writephy(tp, 0x1f, 0x0002);
2181 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2182 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2184 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2185 static const struct phy_reg phy_reg_init[] = {
2195 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2197 val = rtl_readphy(tp, 0x0d);
2199 if ((val & 0x00ff) != 0x006c) {
2200 static const u32 set[] = {
2201 0x0065, 0x0066, 0x0067, 0x0068,
2202 0x0069, 0x006a, 0x006b, 0x006c
2206 rtl_writephy(tp, 0x1f, 0x0002);
2209 for (i = 0; i < ARRAY_SIZE(set); i++)
2210 rtl_writephy(tp, 0x0d, val | set[i]);
2213 static const struct phy_reg phy_reg_init[] = {
2221 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2224 /* RSET couple improve */
2225 rtl_writephy(tp, 0x1f, 0x0002);
2226 rtl_patchphy(tp, 0x0d, 0x0300);
2227 rtl_patchphy(tp, 0x0f, 0x0010);
2229 /* Fine tune PLL performance */
2230 rtl_writephy(tp, 0x1f, 0x0002);
2231 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2232 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2234 rtl_writephy(tp, 0x1f, 0x0005);
2235 rtl_writephy(tp, 0x05, 0x001b);
2236 if ((rtl_readphy(tp, 0x06) != 0xbf00) ||
2237 (rtl_apply_firmware(tp, FIRMWARE_8168D_1) < 0)) {
2238 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2241 rtl_writephy(tp, 0x1f, 0x0000);
2244 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2246 static const struct phy_reg phy_reg_init_0[] = {
2247 /* Channel Estimation */
2268 * enhance line driver power
2277 * Can not link to 1Gbps with bad cable
2278 * Decrease SNR threshold form 21.07dB to 19.04dB
2286 void __iomem *ioaddr = tp->mmio_addr;
2288 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2290 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2291 static const struct phy_reg phy_reg_init[] = {
2302 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2304 val = rtl_readphy(tp, 0x0d);
2305 if ((val & 0x00ff) != 0x006c) {
2306 static const u32 set[] = {
2307 0x0065, 0x0066, 0x0067, 0x0068,
2308 0x0069, 0x006a, 0x006b, 0x006c
2312 rtl_writephy(tp, 0x1f, 0x0002);
2315 for (i = 0; i < ARRAY_SIZE(set); i++)
2316 rtl_writephy(tp, 0x0d, val | set[i]);
2319 static const struct phy_reg phy_reg_init[] = {
2327 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2330 /* Fine tune PLL performance */
2331 rtl_writephy(tp, 0x1f, 0x0002);
2332 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2333 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2335 /* Switching regulator Slew rate */
2336 rtl_writephy(tp, 0x1f, 0x0002);
2337 rtl_patchphy(tp, 0x0f, 0x0017);
2339 rtl_writephy(tp, 0x1f, 0x0005);
2340 rtl_writephy(tp, 0x05, 0x001b);
2341 if ((rtl_readphy(tp, 0x06) != 0xb300) ||
2342 (rtl_apply_firmware(tp, FIRMWARE_8168D_2) < 0)) {
2343 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2346 rtl_writephy(tp, 0x1f, 0x0000);
2349 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2351 static const struct phy_reg phy_reg_init[] = {
2407 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2410 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2412 static const struct phy_reg phy_reg_init[] = {
2422 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2423 rtl_patchphy(tp, 0x0d, 1 << 5);
2426 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
2428 static const struct phy_reg phy_reg_init[] = {
2435 rtl_writephy(tp, 0x1f, 0x0000);
2436 rtl_patchphy(tp, 0x11, 1 << 12);
2437 rtl_patchphy(tp, 0x19, 1 << 13);
2438 rtl_patchphy(tp, 0x10, 1 << 15);
2440 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2443 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2445 static const struct phy_reg phy_reg_init[] = {
2459 /* Disable ALDPS before ram code */
2460 rtl_writephy(tp, 0x1f, 0x0000);
2461 rtl_writephy(tp, 0x18, 0x0310);
2464 if (rtl_apply_firmware(tp, FIRMWARE_8105E_1) < 0)
2465 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2467 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2470 static void rtl_hw_phy_config(struct net_device *dev)
2472 struct rtl8169_private *tp = netdev_priv(dev);
2474 rtl8169_print_mac_version(tp);
2476 switch (tp->mac_version) {
2477 case RTL_GIGA_MAC_VER_01:
2479 case RTL_GIGA_MAC_VER_02:
2480 case RTL_GIGA_MAC_VER_03:
2481 rtl8169s_hw_phy_config(tp);
2483 case RTL_GIGA_MAC_VER_04:
2484 rtl8169sb_hw_phy_config(tp);
2486 case RTL_GIGA_MAC_VER_05:
2487 rtl8169scd_hw_phy_config(tp);
2489 case RTL_GIGA_MAC_VER_06:
2490 rtl8169sce_hw_phy_config(tp);
2492 case RTL_GIGA_MAC_VER_07:
2493 case RTL_GIGA_MAC_VER_08:
2494 case RTL_GIGA_MAC_VER_09:
2495 rtl8102e_hw_phy_config(tp);
2497 case RTL_GIGA_MAC_VER_11:
2498 rtl8168bb_hw_phy_config(tp);
2500 case RTL_GIGA_MAC_VER_12:
2501 rtl8168bef_hw_phy_config(tp);
2503 case RTL_GIGA_MAC_VER_17:
2504 rtl8168bef_hw_phy_config(tp);
2506 case RTL_GIGA_MAC_VER_18:
2507 rtl8168cp_1_hw_phy_config(tp);
2509 case RTL_GIGA_MAC_VER_19:
2510 rtl8168c_1_hw_phy_config(tp);
2512 case RTL_GIGA_MAC_VER_20:
2513 rtl8168c_2_hw_phy_config(tp);
2515 case RTL_GIGA_MAC_VER_21:
2516 rtl8168c_3_hw_phy_config(tp);
2518 case RTL_GIGA_MAC_VER_22:
2519 rtl8168c_4_hw_phy_config(tp);
2521 case RTL_GIGA_MAC_VER_23:
2522 case RTL_GIGA_MAC_VER_24:
2523 rtl8168cp_2_hw_phy_config(tp);
2525 case RTL_GIGA_MAC_VER_25:
2526 rtl8168d_1_hw_phy_config(tp);
2528 case RTL_GIGA_MAC_VER_26:
2529 rtl8168d_2_hw_phy_config(tp);
2531 case RTL_GIGA_MAC_VER_27:
2532 rtl8168d_3_hw_phy_config(tp);
2534 case RTL_GIGA_MAC_VER_28:
2535 rtl8168d_4_hw_phy_config(tp);
2537 case RTL_GIGA_MAC_VER_29:
2538 case RTL_GIGA_MAC_VER_30:
2539 rtl8105e_hw_phy_config(tp);
2547 static void rtl8169_phy_timer(unsigned long __opaque)
2549 struct net_device *dev = (struct net_device *)__opaque;
2550 struct rtl8169_private *tp = netdev_priv(dev);
2551 struct timer_list *timer = &tp->timer;
2552 void __iomem *ioaddr = tp->mmio_addr;
2553 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2555 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
2557 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
2560 spin_lock_irq(&tp->lock);
2562 if (tp->phy_reset_pending(tp)) {
2564 * A busy loop could burn quite a few cycles on nowadays CPU.
2565 * Let's delay the execution of the timer for a few ticks.
2571 if (tp->link_ok(ioaddr))
2574 netif_warn(tp, link, dev, "PHY reset until link up\n");
2576 tp->phy_reset_enable(tp);
2579 mod_timer(timer, jiffies + timeout);
2581 spin_unlock_irq(&tp->lock);
2584 static inline void rtl8169_delete_timer(struct net_device *dev)
2586 struct rtl8169_private *tp = netdev_priv(dev);
2587 struct timer_list *timer = &tp->timer;
2589 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
2592 del_timer_sync(timer);
2595 static inline void rtl8169_request_timer(struct net_device *dev)
2597 struct rtl8169_private *tp = netdev_priv(dev);
2598 struct timer_list *timer = &tp->timer;
2600 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
2603 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
2606 #ifdef CONFIG_NET_POLL_CONTROLLER
2608 * Polling 'interrupt' - used by things like netconsole to send skbs
2609 * without having to re-enable interrupts. It's not called while
2610 * the interrupt routine is executing.
2612 static void rtl8169_netpoll(struct net_device *dev)
2614 struct rtl8169_private *tp = netdev_priv(dev);
2615 struct pci_dev *pdev = tp->pci_dev;
2617 disable_irq(pdev->irq);
2618 rtl8169_interrupt(pdev->irq, dev);
2619 enable_irq(pdev->irq);
2623 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2624 void __iomem *ioaddr)
2627 pci_release_regions(pdev);
2628 pci_clear_mwi(pdev);
2629 pci_disable_device(pdev);
2633 static void rtl8169_phy_reset(struct net_device *dev,
2634 struct rtl8169_private *tp)
2638 tp->phy_reset_enable(tp);
2639 for (i = 0; i < 100; i++) {
2640 if (!tp->phy_reset_pending(tp))
2644 netif_err(tp, link, dev, "PHY reset failed\n");
2647 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
2649 void __iomem *ioaddr = tp->mmio_addr;
2651 rtl_hw_phy_config(dev);
2653 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2654 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2658 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2660 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2661 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
2663 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
2664 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2666 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
2667 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
2670 rtl8169_phy_reset(dev, tp);
2673 * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
2674 * only 8101. Don't panic.
2676 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
2678 if (RTL_R8(PHYstatus) & TBI_Enable)
2679 netif_info(tp, link, dev, "TBI auto-negotiating\n");
2682 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2684 void __iomem *ioaddr = tp->mmio_addr;
2688 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2689 high = addr[4] | (addr[5] << 8);
2691 spin_lock_irq(&tp->lock);
2693 RTL_W8(Cfg9346, Cfg9346_Unlock);
2695 RTL_W32(MAC4, high);
2701 RTL_W8(Cfg9346, Cfg9346_Lock);
2703 spin_unlock_irq(&tp->lock);
2706 static int rtl_set_mac_address(struct net_device *dev, void *p)
2708 struct rtl8169_private *tp = netdev_priv(dev);
2709 struct sockaddr *addr = p;
2711 if (!is_valid_ether_addr(addr->sa_data))
2712 return -EADDRNOTAVAIL;
2714 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2716 rtl_rar_set(tp, dev->dev_addr);
2721 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2723 struct rtl8169_private *tp = netdev_priv(dev);
2724 struct mii_ioctl_data *data = if_mii(ifr);
2726 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2729 static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2733 data->phy_id = 32; /* Internal PHY */
2737 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
2741 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
2747 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2752 static const struct rtl_cfg_info {
2753 void (*hw_start)(struct net_device *);
2754 unsigned int region;
2760 } rtl_cfg_infos [] = {
2762 .hw_start = rtl_hw_start_8169,
2765 .intr_event = SYSErr | LinkChg | RxOverflow |
2766 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
2767 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
2768 .features = RTL_FEATURE_GMII,
2769 .default_ver = RTL_GIGA_MAC_VER_01,
2772 .hw_start = rtl_hw_start_8168,
2775 .intr_event = SYSErr | LinkChg | RxOverflow |
2776 TxErr | TxOK | RxOK | RxErr,
2777 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
2778 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2779 .default_ver = RTL_GIGA_MAC_VER_11,
2782 .hw_start = rtl_hw_start_8101,
2785 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2786 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
2787 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
2788 .features = RTL_FEATURE_MSI,
2789 .default_ver = RTL_GIGA_MAC_VER_13,
2793 /* Cfg9346_Unlock assumed. */
2794 static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2795 const struct rtl_cfg_info *cfg)
2800 cfg2 = RTL_R8(Config2) & ~MSIEnable;
2801 if (cfg->features & RTL_FEATURE_MSI) {
2802 if (pci_enable_msi(pdev)) {
2803 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2806 msi = RTL_FEATURE_MSI;
2809 RTL_W8(Config2, cfg2);
2813 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2815 if (tp->features & RTL_FEATURE_MSI) {
2816 pci_disable_msi(pdev);
2817 tp->features &= ~RTL_FEATURE_MSI;
2821 static const struct net_device_ops rtl8169_netdev_ops = {
2822 .ndo_open = rtl8169_open,
2823 .ndo_stop = rtl8169_close,
2824 .ndo_get_stats = rtl8169_get_stats,
2825 .ndo_start_xmit = rtl8169_start_xmit,
2826 .ndo_tx_timeout = rtl8169_tx_timeout,
2827 .ndo_validate_addr = eth_validate_addr,
2828 .ndo_change_mtu = rtl8169_change_mtu,
2829 .ndo_set_mac_address = rtl_set_mac_address,
2830 .ndo_do_ioctl = rtl8169_ioctl,
2831 .ndo_set_multicast_list = rtl_set_rx_mode,
2832 #ifdef CONFIG_R8169_VLAN
2833 .ndo_vlan_rx_register = rtl8169_vlan_rx_register,
2835 #ifdef CONFIG_NET_POLL_CONTROLLER
2836 .ndo_poll_controller = rtl8169_netpoll,
2841 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2843 struct mdio_ops *ops = &tp->mdio_ops;
2845 switch (tp->mac_version) {
2846 case RTL_GIGA_MAC_VER_27:
2847 ops->write = r8168dp_1_mdio_write;
2848 ops->read = r8168dp_1_mdio_read;
2850 case RTL_GIGA_MAC_VER_28:
2851 ops->write = r8168dp_2_mdio_write;
2852 ops->read = r8168dp_2_mdio_read;
2855 ops->write = r8169_mdio_write;
2856 ops->read = r8169_mdio_read;
2861 static void r810x_phy_power_down(struct rtl8169_private *tp)
2863 rtl_writephy(tp, 0x1f, 0x0000);
2864 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2867 static void r810x_phy_power_up(struct rtl8169_private *tp)
2869 rtl_writephy(tp, 0x1f, 0x0000);
2870 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2873 static void r810x_pll_power_down(struct rtl8169_private *tp)
2875 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2876 rtl_writephy(tp, 0x1f, 0x0000);
2877 rtl_writephy(tp, MII_BMCR, 0x0000);
2881 r810x_phy_power_down(tp);
2884 static void r810x_pll_power_up(struct rtl8169_private *tp)
2886 r810x_phy_power_up(tp);
2889 static void r8168_phy_power_up(struct rtl8169_private *tp)
2891 rtl_writephy(tp, 0x1f, 0x0000);
2892 rtl_writephy(tp, 0x0e, 0x0000);
2893 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2896 static void r8168_phy_power_down(struct rtl8169_private *tp)
2898 rtl_writephy(tp, 0x1f, 0x0000);
2899 rtl_writephy(tp, 0x0e, 0x0200);
2900 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2903 static void r8168_pll_power_down(struct rtl8169_private *tp)
2905 void __iomem *ioaddr = tp->mmio_addr;
2907 if (tp->mac_version == RTL_GIGA_MAC_VER_27)
2910 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
2911 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
2912 (RTL_R16(CPlusCmd) & ASF)) {
2916 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2917 rtl_writephy(tp, 0x1f, 0x0000);
2918 rtl_writephy(tp, MII_BMCR, 0x0000);
2920 RTL_W32(RxConfig, RTL_R32(RxConfig) |
2921 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
2925 r8168_phy_power_down(tp);
2927 switch (tp->mac_version) {
2928 case RTL_GIGA_MAC_VER_25:
2929 case RTL_GIGA_MAC_VER_26:
2930 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
2935 static void r8168_pll_power_up(struct rtl8169_private *tp)
2937 void __iomem *ioaddr = tp->mmio_addr;
2939 if (tp->mac_version == RTL_GIGA_MAC_VER_27)
2942 switch (tp->mac_version) {
2943 case RTL_GIGA_MAC_VER_25:
2944 case RTL_GIGA_MAC_VER_26:
2945 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
2949 r8168_phy_power_up(tp);
2952 static void rtl_pll_power_op(struct rtl8169_private *tp,
2953 void (*op)(struct rtl8169_private *))
2959 static void rtl_pll_power_down(struct rtl8169_private *tp)
2961 rtl_pll_power_op(tp, tp->pll_power_ops.down);
2964 static void rtl_pll_power_up(struct rtl8169_private *tp)
2966 rtl_pll_power_op(tp, tp->pll_power_ops.up);
2969 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
2971 struct pll_power_ops *ops = &tp->pll_power_ops;
2973 switch (tp->mac_version) {
2974 case RTL_GIGA_MAC_VER_07:
2975 case RTL_GIGA_MAC_VER_08:
2976 case RTL_GIGA_MAC_VER_09:
2977 case RTL_GIGA_MAC_VER_10:
2978 case RTL_GIGA_MAC_VER_16:
2979 case RTL_GIGA_MAC_VER_29:
2980 case RTL_GIGA_MAC_VER_30:
2981 ops->down = r810x_pll_power_down;
2982 ops->up = r810x_pll_power_up;
2985 case RTL_GIGA_MAC_VER_11:
2986 case RTL_GIGA_MAC_VER_12:
2987 case RTL_GIGA_MAC_VER_17:
2988 case RTL_GIGA_MAC_VER_18:
2989 case RTL_GIGA_MAC_VER_19:
2990 case RTL_GIGA_MAC_VER_20:
2991 case RTL_GIGA_MAC_VER_21:
2992 case RTL_GIGA_MAC_VER_22:
2993 case RTL_GIGA_MAC_VER_23:
2994 case RTL_GIGA_MAC_VER_24:
2995 case RTL_GIGA_MAC_VER_25:
2996 case RTL_GIGA_MAC_VER_26:
2997 case RTL_GIGA_MAC_VER_27:
2998 case RTL_GIGA_MAC_VER_28:
2999 ops->down = r8168_pll_power_down;
3000 ops->up = r8168_pll_power_up;
3010 static int __devinit
3011 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3013 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3014 const unsigned int region = cfg->region;
3015 struct rtl8169_private *tp;
3016 struct mii_if_info *mii;
3017 struct net_device *dev;
3018 void __iomem *ioaddr;
3022 if (netif_msg_drv(&debug)) {
3023 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3024 MODULENAME, RTL8169_VERSION);
3027 dev = alloc_etherdev(sizeof (*tp));
3029 if (netif_msg_drv(&debug))
3030 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
3035 SET_NETDEV_DEV(dev, &pdev->dev);
3036 dev->netdev_ops = &rtl8169_netdev_ops;
3037 tp = netdev_priv(dev);
3040 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
3044 mii->mdio_read = rtl_mdio_read;
3045 mii->mdio_write = rtl_mdio_write;
3046 mii->phy_id_mask = 0x1f;
3047 mii->reg_num_mask = 0x1f;
3048 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3050 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3051 rc = pci_enable_device(pdev);
3053 netif_err(tp, probe, dev, "enable failure\n");
3054 goto err_out_free_dev_1;
3057 if (pci_set_mwi(pdev) < 0)
3058 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
3060 /* make sure PCI base addr 1 is MMIO */
3061 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
3062 netif_err(tp, probe, dev,
3063 "region #%d not an MMIO resource, aborting\n",
3069 /* check for weird/broken PCI region reporting */
3070 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
3071 netif_err(tp, probe, dev,
3072 "Invalid PCI region size(s), aborting\n");
3077 rc = pci_request_regions(pdev, MODULENAME);
3079 netif_err(tp, probe, dev, "could not request regions\n");
3083 tp->cp_cmd = PCIMulRW | RxChkSum;
3085 if ((sizeof(dma_addr_t) > 4) &&
3086 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
3087 tp->cp_cmd |= PCIDAC;
3088 dev->features |= NETIF_F_HIGHDMA;
3090 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3092 netif_err(tp, probe, dev, "DMA configuration failed\n");
3093 goto err_out_free_res_3;
3097 /* ioremap MMIO region */
3098 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
3100 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
3102 goto err_out_free_res_3;
3105 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3107 netif_info(tp, probe, dev, "no PCI Express capability\n");
3109 RTL_W16(IntrMask, 0x0000);
3111 /* Soft reset the chip. */
3112 RTL_W8(ChipCmd, CmdReset);
3114 /* Check that the chip has finished the reset. */
3115 for (i = 0; i < 100; i++) {
3116 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3118 msleep_interruptible(1);
3121 RTL_W16(IntrStatus, 0xffff);
3123 pci_set_master(pdev);
3125 /* Identify chip attached to board */
3126 rtl8169_get_mac_version(tp, ioaddr);
3128 rtl_init_mdio_ops(tp);
3129 rtl_init_pll_power_ops(tp);
3131 /* Use appropriate default if unknown */
3132 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
3133 netif_notice(tp, probe, dev,
3134 "unknown MAC, using family default\n");
3135 tp->mac_version = cfg->default_ver;
3138 rtl8169_print_mac_version(tp);
3140 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
3141 if (tp->mac_version == rtl_chip_info[i].mac_version)
3144 if (i == ARRAY_SIZE(rtl_chip_info)) {
3146 "driver bug, MAC version not found in rtl_chip_info\n");
3151 RTL_W8(Cfg9346, Cfg9346_Unlock);
3152 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3153 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
3154 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3155 tp->features |= RTL_FEATURE_WOL;
3156 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3157 tp->features |= RTL_FEATURE_WOL;
3158 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
3159 RTL_W8(Cfg9346, Cfg9346_Lock);
3161 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3162 (RTL_R8(PHYstatus) & TBI_Enable)) {
3163 tp->set_speed = rtl8169_set_speed_tbi;
3164 tp->get_settings = rtl8169_gset_tbi;
3165 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3166 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3167 tp->link_ok = rtl8169_tbi_link_ok;
3168 tp->do_ioctl = rtl_tbi_ioctl;
3170 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
3172 tp->set_speed = rtl8169_set_speed_xmii;
3173 tp->get_settings = rtl8169_gset_xmii;
3174 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3175 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3176 tp->link_ok = rtl8169_xmii_link_ok;
3177 tp->do_ioctl = rtl_xmii_ioctl;
3180 spin_lock_init(&tp->lock);
3182 tp->mmio_addr = ioaddr;
3184 /* Get MAC address */
3185 for (i = 0; i < MAC_ADDR_LEN; i++)
3186 dev->dev_addr[i] = RTL_R8(MAC0 + i);
3187 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
3189 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
3190 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3191 dev->irq = pdev->irq;
3192 dev->base_addr = (unsigned long) ioaddr;
3194 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
3196 #ifdef CONFIG_R8169_VLAN
3197 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3199 dev->features |= NETIF_F_GRO;
3201 tp->intr_mask = 0xffff;
3202 tp->hw_start = cfg->hw_start;
3203 tp->intr_event = cfg->intr_event;
3204 tp->napi_event = cfg->napi_event;
3206 init_timer(&tp->timer);
3207 tp->timer.data = (unsigned long) dev;
3208 tp->timer.function = rtl8169_phy_timer;
3210 rc = register_netdev(dev);
3214 pci_set_drvdata(pdev, dev);
3216 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3217 rtl_chip_info[tp->chipset].name,
3218 dev->base_addr, dev->dev_addr,
3219 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
3221 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3222 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
3223 rtl8168_driver_start(tp);
3226 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
3228 if (pci_dev_run_wake(pdev))
3229 pm_runtime_put_noidle(&pdev->dev);
3231 netif_carrier_off(dev);
3237 rtl_disable_msi(pdev, tp);
3240 pci_release_regions(pdev);
3242 pci_clear_mwi(pdev);
3243 pci_disable_device(pdev);
3249 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
3251 struct net_device *dev = pci_get_drvdata(pdev);
3252 struct rtl8169_private *tp = netdev_priv(dev);
3254 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3255 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
3256 rtl8168_driver_stop(tp);
3259 cancel_delayed_work_sync(&tp->task);
3261 rtl_release_firmware(tp);
3263 unregister_netdev(dev);
3265 if (pci_dev_run_wake(pdev))
3266 pm_runtime_get_noresume(&pdev->dev);
3268 /* restore original MAC address */
3269 rtl_rar_set(tp, dev->perm_addr);
3271 rtl_disable_msi(pdev, tp);
3272 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3273 pci_set_drvdata(pdev, NULL);
3276 static int rtl8169_open(struct net_device *dev)
3278 struct rtl8169_private *tp = netdev_priv(dev);
3279 void __iomem *ioaddr = tp->mmio_addr;
3280 struct pci_dev *pdev = tp->pci_dev;
3281 int retval = -ENOMEM;
3283 pm_runtime_get_sync(&pdev->dev);
3286 * Rx and Tx desscriptors needs 256 bytes alignment.
3287 * dma_alloc_coherent provides more.
3289 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3290 &tp->TxPhyAddr, GFP_KERNEL);
3291 if (!tp->TxDescArray)
3292 goto err_pm_runtime_put;
3294 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3295 &tp->RxPhyAddr, GFP_KERNEL);
3296 if (!tp->RxDescArray)
3299 retval = rtl8169_init_ring(dev);
3303 INIT_DELAYED_WORK(&tp->task, NULL);
3307 retval = request_irq(dev->irq, rtl8169_interrupt,
3308 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
3311 goto err_release_ring_2;
3313 napi_enable(&tp->napi);
3315 rtl8169_init_phy(dev, tp);
3318 * Pretend we are using VLANs; This bypasses a nasty bug where
3319 * Interrupts stop flowing on high load on 8110SCd controllers.
3321 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3322 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | RxVlan);
3324 rtl_pll_power_up(tp);
3328 rtl8169_request_timer(dev);
3330 tp->saved_wolopts = 0;
3331 pm_runtime_put_noidle(&pdev->dev);
3333 rtl8169_check_link_status(dev, tp, ioaddr);
3338 rtl8169_rx_clear(tp);
3340 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3342 tp->RxDescArray = NULL;
3344 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3346 tp->TxDescArray = NULL;
3348 pm_runtime_put_noidle(&pdev->dev);
3352 static void rtl8169_hw_reset(struct rtl8169_private *tp)
3354 void __iomem *ioaddr = tp->mmio_addr;
3356 /* Disable interrupts */
3357 rtl8169_irq_mask_and_ack(ioaddr);
3359 if (tp->mac_version == RTL_GIGA_MAC_VER_28) {
3360 while (RTL_R8(TxPoll) & NPQ)
3365 /* Reset the chipset */
3366 RTL_W8(ChipCmd, CmdReset);
3372 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
3374 void __iomem *ioaddr = tp->mmio_addr;
3375 u32 cfg = rtl8169_rx_config;
3377 cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
3378 RTL_W32(RxConfig, cfg);
3380 /* Set DMA burst size and Interframe Gap Time */
3381 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3382 (InterFrameGap << TxInterFrameGapShift));
3385 static void rtl_hw_start(struct net_device *dev)
3387 struct rtl8169_private *tp = netdev_priv(dev);
3388 void __iomem *ioaddr = tp->mmio_addr;
3391 /* Soft reset the chip. */
3392 RTL_W8(ChipCmd, CmdReset);
3394 /* Check that the chip has finished the reset. */
3395 for (i = 0; i < 100; i++) {
3396 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3398 msleep_interruptible(1);
3403 netif_start_queue(dev);
3407 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3408 void __iomem *ioaddr)
3411 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3412 * register to be written before TxDescAddrLow to work.
3413 * Switching from MMIO to I/O access fixes the issue as well.
3415 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
3416 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
3417 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
3418 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
3421 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3425 cmd = RTL_R16(CPlusCmd);
3426 RTL_W16(CPlusCmd, cmd);
3430 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
3432 /* Low hurts. Let's disable the filtering. */
3433 RTL_W16(RxMaxSize, rx_buf_sz + 1);
3436 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3438 static const struct {
3443 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3444 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3445 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3446 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3451 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
3452 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
3453 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3454 RTL_W32(0x7c, p->val);
3460 static void rtl_hw_start_8169(struct net_device *dev)
3462 struct rtl8169_private *tp = netdev_priv(dev);
3463 void __iomem *ioaddr = tp->mmio_addr;
3464 struct pci_dev *pdev = tp->pci_dev;
3466 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3467 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3468 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3471 RTL_W8(Cfg9346, Cfg9346_Unlock);
3472 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3473 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3474 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3475 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3476 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3478 RTL_W8(EarlyTxThres, NoEarlyTx);
3480 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
3482 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3483 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3484 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3485 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3486 rtl_set_rx_tx_config_registers(tp);
3488 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
3490 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3491 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
3492 dprintk("Set MAC Reg C+CR Offset 0xE0. "
3493 "Bit-3 and bit-14 MUST be 1\n");
3494 tp->cp_cmd |= (1 << 14);
3497 RTL_W16(CPlusCmd, tp->cp_cmd);
3499 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3502 * Undocumented corner. Supposedly:
3503 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3505 RTL_W16(IntrMitigate, 0x0000);
3507 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3509 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3510 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3511 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3512 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3513 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3514 rtl_set_rx_tx_config_registers(tp);
3517 RTL_W8(Cfg9346, Cfg9346_Lock);
3519 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3522 RTL_W32(RxMissed, 0);
3524 rtl_set_rx_mode(dev);
3526 /* no early-rx interrupts */
3527 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
3529 /* Enable all known interrupts by setting the interrupt mask. */
3530 RTL_W16(IntrMask, tp->intr_event);
3533 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
3535 struct net_device *dev = pci_get_drvdata(pdev);
3536 struct rtl8169_private *tp = netdev_priv(dev);
3537 int cap = tp->pcie_cap;
3542 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3543 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3544 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3548 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
3552 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
3553 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3556 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3558 rtl_csi_access_enable(ioaddr, 0x17000000);
3561 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3563 rtl_csi_access_enable(ioaddr, 0x27000000);
3567 unsigned int offset;
3572 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
3577 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3578 rtl_ephy_write(ioaddr, e->offset, w);
3583 static void rtl_disable_clock_request(struct pci_dev *pdev)
3585 struct net_device *dev = pci_get_drvdata(pdev);
3586 struct rtl8169_private *tp = netdev_priv(dev);
3587 int cap = tp->pcie_cap;
3592 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3593 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3594 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3598 static void rtl_enable_clock_request(struct pci_dev *pdev)
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_LNKCTL, &ctl);
3608 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3609 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3613 #define R8168_CPCMD_QUIRK_MASK (\
3624 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3626 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3628 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3630 rtl_tx_performance_tweak(pdev,
3631 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3634 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3636 rtl_hw_start_8168bb(ioaddr, pdev);
3638 RTL_W8(MaxTxPacketSize, TxPacketMax);
3640 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3643 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3645 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3647 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3649 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3651 rtl_disable_clock_request(pdev);
3653 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3656 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
3658 static const struct ephy_info e_info_8168cp[] = {
3659 { 0x01, 0, 0x0001 },
3660 { 0x02, 0x0800, 0x1000 },
3661 { 0x03, 0, 0x0042 },
3662 { 0x06, 0x0080, 0x0000 },
3666 rtl_csi_access_enable_2(ioaddr);
3668 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3670 __rtl_hw_start_8168cp(ioaddr, pdev);
3673 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3675 rtl_csi_access_enable_2(ioaddr);
3677 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3679 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3681 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3684 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3686 rtl_csi_access_enable_2(ioaddr);
3688 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3691 RTL_W8(DBG_REG, 0x20);
3693 RTL_W8(MaxTxPacketSize, TxPacketMax);
3695 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3697 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3700 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3702 static const struct ephy_info e_info_8168c_1[] = {
3703 { 0x02, 0x0800, 0x1000 },
3704 { 0x03, 0, 0x0002 },
3705 { 0x06, 0x0080, 0x0000 }
3708 rtl_csi_access_enable_2(ioaddr);
3710 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3712 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3714 __rtl_hw_start_8168cp(ioaddr, pdev);
3717 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3719 static const struct ephy_info e_info_8168c_2[] = {
3720 { 0x01, 0, 0x0001 },
3721 { 0x03, 0x0400, 0x0220 }
3724 rtl_csi_access_enable_2(ioaddr);
3726 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3728 __rtl_hw_start_8168cp(ioaddr, pdev);
3731 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3733 rtl_hw_start_8168c_2(ioaddr, pdev);
3736 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3738 rtl_csi_access_enable_2(ioaddr);
3740 __rtl_hw_start_8168cp(ioaddr, pdev);
3743 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3745 rtl_csi_access_enable_2(ioaddr);
3747 rtl_disable_clock_request(pdev);
3749 RTL_W8(MaxTxPacketSize, TxPacketMax);
3751 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3753 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3756 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
3758 static const struct ephy_info e_info_8168d_4[] = {
3760 { 0x19, 0x20, 0x50 },
3765 rtl_csi_access_enable_1(ioaddr);
3767 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3769 RTL_W8(MaxTxPacketSize, TxPacketMax);
3771 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
3772 const struct ephy_info *e = e_info_8168d_4 + i;
3775 w = rtl_ephy_read(ioaddr, e->offset);
3776 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
3779 rtl_enable_clock_request(pdev);
3782 static void rtl_hw_start_8168(struct net_device *dev)
3784 struct rtl8169_private *tp = netdev_priv(dev);
3785 void __iomem *ioaddr = tp->mmio_addr;
3786 struct pci_dev *pdev = tp->pci_dev;
3788 RTL_W8(Cfg9346, Cfg9346_Unlock);
3790 RTL_W8(MaxTxPacketSize, TxPacketMax);
3792 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
3794 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
3796 RTL_W16(CPlusCmd, tp->cp_cmd);
3798 RTL_W16(IntrMitigate, 0x5151);
3800 /* Work around for RxFIFO overflow. */
3801 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
3802 tp->mac_version == RTL_GIGA_MAC_VER_22) {
3803 tp->intr_event |= RxFIFOOver | PCSTimeout;
3804 tp->intr_event &= ~RxOverflow;
3807 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3809 rtl_set_rx_mode(dev);
3811 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3812 (InterFrameGap << TxInterFrameGapShift));
3816 switch (tp->mac_version) {
3817 case RTL_GIGA_MAC_VER_11:
3818 rtl_hw_start_8168bb(ioaddr, pdev);
3821 case RTL_GIGA_MAC_VER_12:
3822 case RTL_GIGA_MAC_VER_17:
3823 rtl_hw_start_8168bef(ioaddr, pdev);
3826 case RTL_GIGA_MAC_VER_18:
3827 rtl_hw_start_8168cp_1(ioaddr, pdev);
3830 case RTL_GIGA_MAC_VER_19:
3831 rtl_hw_start_8168c_1(ioaddr, pdev);
3834 case RTL_GIGA_MAC_VER_20:
3835 rtl_hw_start_8168c_2(ioaddr, pdev);
3838 case RTL_GIGA_MAC_VER_21:
3839 rtl_hw_start_8168c_3(ioaddr, pdev);
3842 case RTL_GIGA_MAC_VER_22:
3843 rtl_hw_start_8168c_4(ioaddr, pdev);
3846 case RTL_GIGA_MAC_VER_23:
3847 rtl_hw_start_8168cp_2(ioaddr, pdev);
3850 case RTL_GIGA_MAC_VER_24:
3851 rtl_hw_start_8168cp_3(ioaddr, pdev);
3854 case RTL_GIGA_MAC_VER_25:
3855 case RTL_GIGA_MAC_VER_26:
3856 case RTL_GIGA_MAC_VER_27:
3857 rtl_hw_start_8168d(ioaddr, pdev);
3860 case RTL_GIGA_MAC_VER_28:
3861 rtl_hw_start_8168d_4(ioaddr, pdev);
3865 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
3866 dev->name, tp->mac_version);
3870 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3872 RTL_W8(Cfg9346, Cfg9346_Lock);
3874 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
3876 RTL_W16(IntrMask, tp->intr_event);
3879 #define R810X_CPCMD_QUIRK_MASK (\
3891 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3893 static const struct ephy_info e_info_8102e_1[] = {
3894 { 0x01, 0, 0x6e65 },
3895 { 0x02, 0, 0x091f },
3896 { 0x03, 0, 0xc2f9 },
3897 { 0x06, 0, 0xafb5 },
3898 { 0x07, 0, 0x0e00 },
3899 { 0x19, 0, 0xec80 },
3900 { 0x01, 0, 0x2e65 },
3905 rtl_csi_access_enable_2(ioaddr);
3907 RTL_W8(DBG_REG, FIX_NAK_1);
3909 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3912 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
3913 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3915 cfg1 = RTL_R8(Config1);
3916 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
3917 RTL_W8(Config1, cfg1 & ~LEDS0);
3919 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
3921 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
3924 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
3926 rtl_csi_access_enable_2(ioaddr);
3928 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3930 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
3931 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3933 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
3936 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
3938 rtl_hw_start_8102e_2(ioaddr, pdev);
3940 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
3943 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3945 static const struct ephy_info e_info_8105e_1[] = {
3946 { 0x07, 0, 0x4000 },
3947 { 0x19, 0, 0x0200 },
3948 { 0x19, 0, 0x0020 },
3949 { 0x1e, 0, 0x2000 },
3950 { 0x03, 0, 0x0001 },
3951 { 0x19, 0, 0x0100 },
3952 { 0x19, 0, 0x0004 },
3956 /* Force LAN exit from ASPM if Rx/Tx are not idel */
3957 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
3959 /* disable Early Tally Counter */
3960 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
3962 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
3963 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
3965 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
3968 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
3970 rtl_hw_start_8105e_1(ioaddr, pdev);
3971 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
3974 static void rtl_hw_start_8101(struct net_device *dev)
3976 struct rtl8169_private *tp = netdev_priv(dev);
3977 void __iomem *ioaddr = tp->mmio_addr;
3978 struct pci_dev *pdev = tp->pci_dev;
3980 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
3981 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
3982 int cap = tp->pcie_cap;
3985 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
3986 PCI_EXP_DEVCTL_NOSNOOP_EN);
3990 switch (tp->mac_version) {
3991 case RTL_GIGA_MAC_VER_07:
3992 rtl_hw_start_8102e_1(ioaddr, pdev);
3995 case RTL_GIGA_MAC_VER_08:
3996 rtl_hw_start_8102e_3(ioaddr, pdev);
3999 case RTL_GIGA_MAC_VER_09:
4000 rtl_hw_start_8102e_2(ioaddr, pdev);
4003 case RTL_GIGA_MAC_VER_29:
4004 rtl_hw_start_8105e_1(ioaddr, pdev);
4006 case RTL_GIGA_MAC_VER_30:
4007 rtl_hw_start_8105e_2(ioaddr, pdev);
4011 RTL_W8(Cfg9346, Cfg9346_Unlock);
4013 RTL_W8(MaxTxPacketSize, TxPacketMax);
4015 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4017 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4019 RTL_W16(CPlusCmd, tp->cp_cmd);
4021 RTL_W16(IntrMitigate, 0x0000);
4023 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4025 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4026 rtl_set_rx_tx_config_registers(tp);
4028 RTL_W8(Cfg9346, Cfg9346_Lock);
4032 rtl_set_rx_mode(dev);
4034 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4036 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
4038 RTL_W16(IntrMask, tp->intr_event);
4041 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4043 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4050 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4052 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
4053 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4056 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4057 void **data_buff, struct RxDesc *desc)
4059 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
4064 rtl8169_make_unusable_by_asic(desc);
4067 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4069 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4071 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4074 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4077 desc->addr = cpu_to_le64(mapping);
4079 rtl8169_mark_to_asic(desc, rx_buf_sz);
4082 static inline void *rtl8169_align(void *data)
4084 return (void *)ALIGN((long)data, 16);
4087 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4088 struct RxDesc *desc)
4092 struct device *d = &tp->pci_dev->dev;
4093 struct net_device *dev = tp->dev;
4094 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
4096 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4100 if (rtl8169_align(data) != data) {
4102 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4107 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
4109 if (unlikely(dma_mapping_error(d, mapping))) {
4110 if (net_ratelimit())
4111 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
4115 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
4123 static void rtl8169_rx_clear(struct rtl8169_private *tp)
4127 for (i = 0; i < NUM_RX_DESC; i++) {
4128 if (tp->Rx_databuff[i]) {
4129 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
4130 tp->RxDescArray + i);
4135 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
4137 desc->opts1 |= cpu_to_le32(RingEnd);
4140 static int rtl8169_rx_fill(struct rtl8169_private *tp)
4144 for (i = 0; i < NUM_RX_DESC; i++) {
4147 if (tp->Rx_databuff[i])
4150 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
4152 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
4155 tp->Rx_databuff[i] = data;
4158 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4162 rtl8169_rx_clear(tp);
4166 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4168 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4171 static int rtl8169_init_ring(struct net_device *dev)
4173 struct rtl8169_private *tp = netdev_priv(dev);
4175 rtl8169_init_ring_indexes(tp);
4177 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
4178 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
4180 return rtl8169_rx_fill(tp);
4183 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
4184 struct TxDesc *desc)
4186 unsigned int len = tx_skb->len;
4188 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4196 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4201 for (i = 0; i < n; i++) {
4202 unsigned int entry = (start + i) % NUM_TX_DESC;
4203 struct ring_info *tx_skb = tp->tx_skb + entry;
4204 unsigned int len = tx_skb->len;
4207 struct sk_buff *skb = tx_skb->skb;
4209 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4210 tp->TxDescArray + entry);
4212 tp->dev->stats.tx_dropped++;
4220 static void rtl8169_tx_clear(struct rtl8169_private *tp)
4222 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
4223 tp->cur_tx = tp->dirty_tx = 0;
4226 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
4228 struct rtl8169_private *tp = netdev_priv(dev);
4230 PREPARE_DELAYED_WORK(&tp->task, task);
4231 schedule_delayed_work(&tp->task, 4);
4234 static void rtl8169_wait_for_quiescence(struct net_device *dev)
4236 struct rtl8169_private *tp = netdev_priv(dev);
4237 void __iomem *ioaddr = tp->mmio_addr;
4239 synchronize_irq(dev->irq);
4241 /* Wait for any pending NAPI task to complete */
4242 napi_disable(&tp->napi);
4244 rtl8169_irq_mask_and_ack(ioaddr);
4246 tp->intr_mask = 0xffff;
4247 RTL_W16(IntrMask, tp->intr_event);
4248 napi_enable(&tp->napi);
4251 static void rtl8169_reinit_task(struct work_struct *work)
4253 struct rtl8169_private *tp =
4254 container_of(work, struct rtl8169_private, task.work);
4255 struct net_device *dev = tp->dev;
4260 if (!netif_running(dev))
4263 rtl8169_wait_for_quiescence(dev);
4266 ret = rtl8169_open(dev);
4267 if (unlikely(ret < 0)) {
4268 if (net_ratelimit())
4269 netif_err(tp, drv, dev,
4270 "reinit failure (status = %d). Rescheduling\n",
4272 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4279 static void rtl8169_reset_task(struct work_struct *work)
4281 struct rtl8169_private *tp =
4282 container_of(work, struct rtl8169_private, task.work);
4283 struct net_device *dev = tp->dev;
4287 if (!netif_running(dev))
4290 rtl8169_wait_for_quiescence(dev);
4292 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
4293 rtl8169_tx_clear(tp);
4295 if (tp->dirty_rx == tp->cur_rx) {
4296 rtl8169_init_ring_indexes(tp);
4298 netif_wake_queue(dev);
4299 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
4301 if (net_ratelimit())
4302 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
4303 rtl8169_schedule_work(dev, rtl8169_reset_task);
4310 static void rtl8169_tx_timeout(struct net_device *dev)
4312 struct rtl8169_private *tp = netdev_priv(dev);
4314 rtl8169_hw_reset(tp);
4316 /* Let's wait a bit while any (async) irq lands on */
4317 rtl8169_schedule_work(dev, rtl8169_reset_task);
4320 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4323 struct skb_shared_info *info = skb_shinfo(skb);
4324 unsigned int cur_frag, entry;
4325 struct TxDesc * uninitialized_var(txd);
4326 struct device *d = &tp->pci_dev->dev;
4329 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4330 skb_frag_t *frag = info->frags + cur_frag;
4335 entry = (entry + 1) % NUM_TX_DESC;
4337 txd = tp->TxDescArray + entry;
4339 addr = ((void *) page_address(frag->page)) + frag->page_offset;
4340 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
4341 if (unlikely(dma_mapping_error(d, mapping))) {
4342 if (net_ratelimit())
4343 netif_err(tp, drv, tp->dev,
4344 "Failed to map TX fragments DMA!\n");
4348 /* anti gcc 2.95.3 bugware (sic) */
4349 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4351 txd->opts1 = cpu_to_le32(status);
4352 txd->addr = cpu_to_le64(mapping);
4354 tp->tx_skb[entry].len = len;
4358 tp->tx_skb[entry].skb = skb;
4359 txd->opts1 |= cpu_to_le32(LastFrag);
4365 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4369 static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
4371 if (dev->features & NETIF_F_TSO) {
4372 u32 mss = skb_shinfo(skb)->gso_size;
4375 return LargeSend | ((mss & MSSMask) << MSSShift);
4377 if (skb->ip_summed == CHECKSUM_PARTIAL) {
4378 const struct iphdr *ip = ip_hdr(skb);
4380 if (ip->protocol == IPPROTO_TCP)
4381 return IPCS | TCPCS;
4382 else if (ip->protocol == IPPROTO_UDP)
4383 return IPCS | UDPCS;
4384 WARN_ON(1); /* we need a WARN() */
4389 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4390 struct net_device *dev)
4392 struct rtl8169_private *tp = netdev_priv(dev);
4393 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
4394 struct TxDesc *txd = tp->TxDescArray + entry;
4395 void __iomem *ioaddr = tp->mmio_addr;
4396 struct device *d = &tp->pci_dev->dev;
4402 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
4403 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
4407 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
4410 len = skb_headlen(skb);
4411 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
4412 if (unlikely(dma_mapping_error(d, mapping))) {
4413 if (net_ratelimit())
4414 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
4418 tp->tx_skb[entry].len = len;
4419 txd->addr = cpu_to_le64(mapping);
4420 txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4422 opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
4424 frags = rtl8169_xmit_frags(tp, skb, opts1);
4430 opts1 |= FirstFrag | LastFrag;
4431 tp->tx_skb[entry].skb = skb;
4436 /* anti gcc 2.95.3 bugware (sic) */
4437 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4438 txd->opts1 = cpu_to_le32(status);
4440 tp->cur_tx += frags + 1;
4444 RTL_W8(TxPoll, NPQ); /* set polling bit */
4446 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4447 netif_stop_queue(dev);
4449 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4450 netif_wake_queue(dev);
4453 return NETDEV_TX_OK;
4456 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
4459 dev->stats.tx_dropped++;
4460 return NETDEV_TX_OK;
4463 netif_stop_queue(dev);
4464 dev->stats.tx_dropped++;
4465 return NETDEV_TX_BUSY;
4468 static void rtl8169_pcierr_interrupt(struct net_device *dev)
4470 struct rtl8169_private *tp = netdev_priv(dev);
4471 struct pci_dev *pdev = tp->pci_dev;
4472 u16 pci_status, pci_cmd;
4474 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4475 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4477 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4478 pci_cmd, pci_status);
4481 * The recovery sequence below admits a very elaborated explanation:
4482 * - it seems to work;
4483 * - I did not see what else could be done;
4484 * - it makes iop3xx happy.
4486 * Feel free to adjust to your needs.
4488 if (pdev->broken_parity_status)
4489 pci_cmd &= ~PCI_COMMAND_PARITY;
4491 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4493 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
4495 pci_write_config_word(pdev, PCI_STATUS,
4496 pci_status & (PCI_STATUS_DETECTED_PARITY |
4497 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4498 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4500 /* The infamous DAC f*ckup only happens at boot time */
4501 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
4502 void __iomem *ioaddr = tp->mmio_addr;
4504 netif_info(tp, intr, dev, "disabling PCI DAC\n");
4505 tp->cp_cmd &= ~PCIDAC;
4506 RTL_W16(CPlusCmd, tp->cp_cmd);
4507 dev->features &= ~NETIF_F_HIGHDMA;
4510 rtl8169_hw_reset(tp);
4512 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4515 static void rtl8169_tx_interrupt(struct net_device *dev,
4516 struct rtl8169_private *tp,
4517 void __iomem *ioaddr)
4519 unsigned int dirty_tx, tx_left;
4521 dirty_tx = tp->dirty_tx;
4523 tx_left = tp->cur_tx - dirty_tx;
4525 while (tx_left > 0) {
4526 unsigned int entry = dirty_tx % NUM_TX_DESC;
4527 struct ring_info *tx_skb = tp->tx_skb + entry;
4531 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4532 if (status & DescOwn)
4535 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4536 tp->TxDescArray + entry);
4537 if (status & LastFrag) {
4538 dev->stats.tx_packets++;
4539 dev->stats.tx_bytes += tx_skb->skb->len;
4540 dev_kfree_skb(tx_skb->skb);
4547 if (tp->dirty_tx != dirty_tx) {
4548 tp->dirty_tx = dirty_tx;
4550 if (netif_queue_stopped(dev) &&
4551 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4552 netif_wake_queue(dev);
4555 * 8168 hack: TxPoll requests are lost when the Tx packets are
4556 * too close. Let's kick an extra TxPoll request when a burst
4557 * of start_xmit activity is detected (if it is not detected,
4558 * it is slow enough). -- FR
4561 if (tp->cur_tx != dirty_tx)
4562 RTL_W8(TxPoll, NPQ);
4566 static inline int rtl8169_fragmented_frame(u32 status)
4568 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4571 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
4573 u32 status = opts1 & RxProtoMask;
4575 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
4576 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
4577 skb->ip_summed = CHECKSUM_UNNECESSARY;
4579 skb_checksum_none_assert(skb);
4582 static struct sk_buff *rtl8169_try_rx_copy(void *data,
4583 struct rtl8169_private *tp,
4587 struct sk_buff *skb;
4588 struct device *d = &tp->pci_dev->dev;
4590 data = rtl8169_align(data);
4591 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
4593 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4595 memcpy(skb->data, data, pkt_size);
4596 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4602 * Warning : rtl8169_rx_interrupt() might be called :
4603 * 1) from NAPI (softirq) context
4604 * (polling = 1 : we should call netif_receive_skb())
4605 * 2) from process context (rtl8169_reset_task())
4606 * (polling = 0 : we must call netif_rx() instead)
4608 static int rtl8169_rx_interrupt(struct net_device *dev,
4609 struct rtl8169_private *tp,
4610 void __iomem *ioaddr, u32 budget)
4612 unsigned int cur_rx, rx_left;
4614 int polling = (budget != ~(u32)0) ? 1 : 0;
4616 cur_rx = tp->cur_rx;
4617 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
4618 rx_left = min(rx_left, budget);
4620 for (; rx_left > 0; rx_left--, cur_rx++) {
4621 unsigned int entry = cur_rx % NUM_RX_DESC;
4622 struct RxDesc *desc = tp->RxDescArray + entry;
4626 status = le32_to_cpu(desc->opts1);
4628 if (status & DescOwn)
4630 if (unlikely(status & RxRES)) {
4631 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4633 dev->stats.rx_errors++;
4634 if (status & (RxRWT | RxRUNT))
4635 dev->stats.rx_length_errors++;
4637 dev->stats.rx_crc_errors++;
4638 if (status & RxFOVF) {
4639 rtl8169_schedule_work(dev, rtl8169_reset_task);
4640 dev->stats.rx_fifo_errors++;
4642 rtl8169_mark_to_asic(desc, rx_buf_sz);
4644 struct sk_buff *skb;
4645 dma_addr_t addr = le64_to_cpu(desc->addr);
4646 int pkt_size = (status & 0x00001FFF) - 4;
4649 * The driver does not support incoming fragmented
4650 * frames. They are seen as a symptom of over-mtu
4653 if (unlikely(rtl8169_fragmented_frame(status))) {
4654 dev->stats.rx_dropped++;
4655 dev->stats.rx_length_errors++;
4656 rtl8169_mark_to_asic(desc, rx_buf_sz);
4660 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4661 tp, pkt_size, addr);
4662 rtl8169_mark_to_asic(desc, rx_buf_sz);
4664 dev->stats.rx_dropped++;
4668 rtl8169_rx_csum(skb, status);
4669 skb_put(skb, pkt_size);
4670 skb->protocol = eth_type_trans(skb, dev);
4672 if (rtl8169_rx_vlan_skb(tp, desc, skb, polling) < 0) {
4673 if (likely(polling))
4674 napi_gro_receive(&tp->napi, skb);
4679 dev->stats.rx_bytes += pkt_size;
4680 dev->stats.rx_packets++;
4683 /* Work around for AMD plateform. */
4684 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
4685 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4691 count = cur_rx - tp->cur_rx;
4692 tp->cur_rx = cur_rx;
4694 tp->dirty_rx += count;
4699 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
4701 struct net_device *dev = dev_instance;
4702 struct rtl8169_private *tp = netdev_priv(dev);
4703 void __iomem *ioaddr = tp->mmio_addr;
4707 /* loop handling interrupts until we have no new ones or
4708 * we hit a invalid/hotplug case.
4710 status = RTL_R16(IntrStatus);
4711 while (status && status != 0xffff) {
4714 /* Handle all of the error cases first. These will reset
4715 * the chip, so just exit the loop.
4717 if (unlikely(!netif_running(dev))) {
4718 rtl8169_asic_down(ioaddr);
4722 if (unlikely(status & RxFIFOOver)) {
4723 switch (tp->mac_version) {
4724 /* Work around for rx fifo overflow */
4725 case RTL_GIGA_MAC_VER_11:
4726 case RTL_GIGA_MAC_VER_22:
4727 case RTL_GIGA_MAC_VER_26:
4728 netif_stop_queue(dev);
4729 rtl8169_tx_timeout(dev);
4731 /* Testers needed. */
4732 case RTL_GIGA_MAC_VER_17:
4733 case RTL_GIGA_MAC_VER_19:
4734 case RTL_GIGA_MAC_VER_20:
4735 case RTL_GIGA_MAC_VER_21:
4736 case RTL_GIGA_MAC_VER_23:
4737 case RTL_GIGA_MAC_VER_24:
4738 case RTL_GIGA_MAC_VER_27:
4739 case RTL_GIGA_MAC_VER_28:
4740 /* Experimental science. Pktgen proof. */
4741 case RTL_GIGA_MAC_VER_12:
4742 case RTL_GIGA_MAC_VER_25:
4743 if (status == RxFIFOOver)
4751 if (unlikely(status & SYSErr)) {
4752 rtl8169_pcierr_interrupt(dev);
4756 if (status & LinkChg)
4757 __rtl8169_check_link_status(dev, tp, ioaddr, true);
4759 /* We need to see the lastest version of tp->intr_mask to
4760 * avoid ignoring an MSI interrupt and having to wait for
4761 * another event which may never come.
4764 if (status & tp->intr_mask & tp->napi_event) {
4765 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
4766 tp->intr_mask = ~tp->napi_event;
4768 if (likely(napi_schedule_prep(&tp->napi)))
4769 __napi_schedule(&tp->napi);
4771 netif_info(tp, intr, dev,
4772 "interrupt %04x in poll\n", status);
4775 /* We only get a new MSI interrupt when all active irq
4776 * sources on the chip have been acknowledged. So, ack
4777 * everything we've seen and check if new sources have become
4778 * active to avoid blocking all interrupts from the chip.
4781 (status & RxFIFOOver) ? (status | RxOverflow) : status);
4782 status = RTL_R16(IntrStatus);
4785 return IRQ_RETVAL(handled);
4788 static int rtl8169_poll(struct napi_struct *napi, int budget)
4790 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
4791 struct net_device *dev = tp->dev;
4792 void __iomem *ioaddr = tp->mmio_addr;
4795 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
4796 rtl8169_tx_interrupt(dev, tp, ioaddr);
4798 if (work_done < budget) {
4799 napi_complete(napi);
4801 /* We need for force the visibility of tp->intr_mask
4802 * for other CPUs, as we can loose an MSI interrupt
4803 * and potentially wait for a retransmit timeout if we don't.
4804 * The posted write to IntrMask is safe, as it will
4805 * eventually make it to the chip and we won't loose anything
4808 tp->intr_mask = 0xffff;
4810 RTL_W16(IntrMask, tp->intr_event);
4816 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
4818 struct rtl8169_private *tp = netdev_priv(dev);
4820 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
4823 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
4824 RTL_W32(RxMissed, 0);
4827 static void rtl8169_down(struct net_device *dev)
4829 struct rtl8169_private *tp = netdev_priv(dev);
4830 void __iomem *ioaddr = tp->mmio_addr;
4832 rtl8169_delete_timer(dev);
4834 netif_stop_queue(dev);
4836 napi_disable(&tp->napi);
4838 spin_lock_irq(&tp->lock);
4840 rtl8169_asic_down(ioaddr);
4842 * At this point device interrupts can not be enabled in any function,
4843 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
4844 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
4846 rtl8169_rx_missed(dev, ioaddr);
4848 spin_unlock_irq(&tp->lock);
4850 synchronize_irq(dev->irq);
4852 /* Give a racing hard_start_xmit a few cycles to complete. */
4853 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
4855 rtl8169_tx_clear(tp);
4857 rtl8169_rx_clear(tp);
4859 rtl_pll_power_down(tp);
4862 static int rtl8169_close(struct net_device *dev)
4864 struct rtl8169_private *tp = netdev_priv(dev);
4865 struct pci_dev *pdev = tp->pci_dev;
4867 pm_runtime_get_sync(&pdev->dev);
4869 /* update counters before going down */
4870 rtl8169_update_counters(dev);
4874 free_irq(dev->irq, dev);
4876 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4878 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4880 tp->TxDescArray = NULL;
4881 tp->RxDescArray = NULL;
4883 pm_runtime_put_sync(&pdev->dev);
4888 static void rtl_set_rx_mode(struct net_device *dev)
4890 struct rtl8169_private *tp = netdev_priv(dev);
4891 void __iomem *ioaddr = tp->mmio_addr;
4892 unsigned long flags;
4893 u32 mc_filter[2]; /* Multicast hash filter */
4897 if (dev->flags & IFF_PROMISC) {
4898 /* Unconditionally log net taps. */
4899 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4901 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4903 mc_filter[1] = mc_filter[0] = 0xffffffff;
4904 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4905 (dev->flags & IFF_ALLMULTI)) {
4906 /* Too many to filter perfectly -- accept all multicasts. */
4907 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4908 mc_filter[1] = mc_filter[0] = 0xffffffff;
4910 struct netdev_hw_addr *ha;
4912 rx_mode = AcceptBroadcast | AcceptMyPhys;
4913 mc_filter[1] = mc_filter[0] = 0;
4914 netdev_for_each_mc_addr(ha, dev) {
4915 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4916 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4917 rx_mode |= AcceptMulticast;
4921 spin_lock_irqsave(&tp->lock, flags);
4923 tmp = rtl8169_rx_config | rx_mode |
4924 (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
4926 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4927 u32 data = mc_filter[0];
4929 mc_filter[0] = swab32(mc_filter[1]);
4930 mc_filter[1] = swab32(data);
4933 RTL_W32(MAR0 + 4, mc_filter[1]);
4934 RTL_W32(MAR0 + 0, mc_filter[0]);
4936 RTL_W32(RxConfig, tmp);
4938 spin_unlock_irqrestore(&tp->lock, flags);
4942 * rtl8169_get_stats - Get rtl8169 read/write statistics
4943 * @dev: The Ethernet Device to get statistics for
4945 * Get TX/RX statistics for rtl8169
4947 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
4949 struct rtl8169_private *tp = netdev_priv(dev);
4950 void __iomem *ioaddr = tp->mmio_addr;
4951 unsigned long flags;
4953 if (netif_running(dev)) {
4954 spin_lock_irqsave(&tp->lock, flags);
4955 rtl8169_rx_missed(dev, ioaddr);
4956 spin_unlock_irqrestore(&tp->lock, flags);
4962 static void rtl8169_net_suspend(struct net_device *dev)
4964 struct rtl8169_private *tp = netdev_priv(dev);
4966 if (!netif_running(dev))
4969 rtl_pll_power_down(tp);
4971 netif_device_detach(dev);
4972 netif_stop_queue(dev);
4977 static int rtl8169_suspend(struct device *device)
4979 struct pci_dev *pdev = to_pci_dev(device);
4980 struct net_device *dev = pci_get_drvdata(pdev);
4982 rtl8169_net_suspend(dev);
4987 static void __rtl8169_resume(struct net_device *dev)
4989 struct rtl8169_private *tp = netdev_priv(dev);
4991 netif_device_attach(dev);
4993 rtl_pll_power_up(tp);
4995 rtl8169_schedule_work(dev, rtl8169_reset_task);
4998 static int rtl8169_resume(struct device *device)
5000 struct pci_dev *pdev = to_pci_dev(device);
5001 struct net_device *dev = pci_get_drvdata(pdev);
5002 struct rtl8169_private *tp = netdev_priv(dev);
5004 rtl8169_init_phy(dev, tp);
5006 if (netif_running(dev))
5007 __rtl8169_resume(dev);
5012 static int rtl8169_runtime_suspend(struct device *device)
5014 struct pci_dev *pdev = to_pci_dev(device);
5015 struct net_device *dev = pci_get_drvdata(pdev);
5016 struct rtl8169_private *tp = netdev_priv(dev);
5018 if (!tp->TxDescArray)
5021 spin_lock_irq(&tp->lock);
5022 tp->saved_wolopts = __rtl8169_get_wol(tp);
5023 __rtl8169_set_wol(tp, WAKE_ANY);
5024 spin_unlock_irq(&tp->lock);
5026 rtl8169_net_suspend(dev);
5031 static int rtl8169_runtime_resume(struct device *device)
5033 struct pci_dev *pdev = to_pci_dev(device);
5034 struct net_device *dev = pci_get_drvdata(pdev);
5035 struct rtl8169_private *tp = netdev_priv(dev);
5037 if (!tp->TxDescArray)
5040 spin_lock_irq(&tp->lock);
5041 __rtl8169_set_wol(tp, tp->saved_wolopts);
5042 tp->saved_wolopts = 0;
5043 spin_unlock_irq(&tp->lock);
5045 rtl8169_init_phy(dev, tp);
5047 __rtl8169_resume(dev);
5052 static int rtl8169_runtime_idle(struct device *device)
5054 struct pci_dev *pdev = to_pci_dev(device);
5055 struct net_device *dev = pci_get_drvdata(pdev);
5056 struct rtl8169_private *tp = netdev_priv(dev);
5058 return tp->TxDescArray ? -EBUSY : 0;
5061 static const struct dev_pm_ops rtl8169_pm_ops = {
5062 .suspend = rtl8169_suspend,
5063 .resume = rtl8169_resume,
5064 .freeze = rtl8169_suspend,
5065 .thaw = rtl8169_resume,
5066 .poweroff = rtl8169_suspend,
5067 .restore = rtl8169_resume,
5068 .runtime_suspend = rtl8169_runtime_suspend,
5069 .runtime_resume = rtl8169_runtime_resume,
5070 .runtime_idle = rtl8169_runtime_idle,
5073 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
5075 #else /* !CONFIG_PM */
5077 #define RTL8169_PM_OPS NULL
5079 #endif /* !CONFIG_PM */
5081 static void rtl_shutdown(struct pci_dev *pdev)
5083 struct net_device *dev = pci_get_drvdata(pdev);
5084 struct rtl8169_private *tp = netdev_priv(dev);
5085 void __iomem *ioaddr = tp->mmio_addr;
5087 rtl8169_net_suspend(dev);
5089 /* restore original MAC address */
5090 rtl_rar_set(tp, dev->perm_addr);
5092 spin_lock_irq(&tp->lock);
5094 rtl8169_asic_down(ioaddr);
5096 spin_unlock_irq(&tp->lock);
5098 if (system_state == SYSTEM_POWER_OFF) {
5099 /* WoL fails with some 8168 when the receiver is disabled. */
5100 if (tp->features & RTL_FEATURE_WOL) {
5101 pci_clear_master(pdev);
5103 RTL_W8(ChipCmd, CmdRxEnb);
5108 pci_wake_from_d3(pdev, true);
5109 pci_set_power_state(pdev, PCI_D3hot);
5113 static struct pci_driver rtl8169_pci_driver = {
5115 .id_table = rtl8169_pci_tbl,
5116 .probe = rtl8169_init_one,
5117 .remove = __devexit_p(rtl8169_remove_one),
5118 .shutdown = rtl_shutdown,
5119 .driver.pm = RTL8169_PM_OPS,
5122 static int __init rtl8169_init_module(void)
5124 return pci_register_driver(&rtl8169_pci_driver);
5127 static void __exit rtl8169_cleanup_module(void)
5129 pci_unregister_driver(&rtl8169_pci_driver);
5132 module_init(rtl8169_init_module);
5133 module_exit(rtl8169_cleanup_module);