2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/if_vlan.h>
20 #include <linux/crc32.h>
23 #include <linux/tcp.h>
24 #include <linux/init.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/firmware.h>
28 #include <linux/pci-aspm.h>
30 #include <asm/system.h>
34 #define RTL8169_VERSION "2.3LK-NAPI"
35 #define MODULENAME "r8169"
36 #define PFX MODULENAME ": "
38 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
40 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
42 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
45 #define assert(expr) \
47 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
48 #expr,__FILE__,__func__,__LINE__); \
50 #define dprintk(fmt, args...) \
51 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
53 #define assert(expr) do {} while (0)
54 #define dprintk(fmt, args...) do {} while (0)
55 #endif /* RTL8169_DEBUG */
57 #define R8169_MSG_DEFAULT \
58 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
60 #define TX_BUFFS_AVAIL(tp) \
61 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
63 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
64 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
65 static const int multicast_filter_limit = 32;
67 /* MAC address length */
68 #define MAC_ADDR_LEN 6
70 #define MAX_READ_REQUEST_SHIFT 12
71 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
72 #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
73 #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
74 #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
77 #define R8169_REGS_SIZE 256
78 #define R8169_NAPI_WEIGHT 64
79 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
82 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
85 #define RTL8169_TX_TIMEOUT (6*HZ)
86 #define RTL8169_PHY_TIMEOUT (10*HZ)
88 #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89 #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
90 #define RTL_EEPROM_SIG_ADDR 0x0000
92 /* write/read MMIO register */
93 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96 #define RTL_R8(reg) readb (ioaddr + (reg))
97 #define RTL_R16(reg) readw (ioaddr + (reg))
98 #define RTL_R32(reg) readl (ioaddr + (reg))
101 RTL_GIGA_MAC_NONE = 0x00,
102 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
103 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
104 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
105 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
106 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
107 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
108 RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
109 RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
110 RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
111 RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
112 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
113 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
114 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
115 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
116 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
117 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
118 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
119 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
120 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
121 RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
122 RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
123 RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
124 RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
125 RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
126 RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
127 RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
128 RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
129 RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
130 RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
131 RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
132 RTL_GIGA_MAC_VER_31 = 0x1f, // 8168DP
133 RTL_GIGA_MAC_VER_32 = 0x20, // 8168E
134 RTL_GIGA_MAC_VER_33 = 0x21, // 8168E
137 enum rtl_tx_desc_version {
142 #define _R(NAME,MAC,TD) \
143 { .name = NAME, .mac_version = MAC, .txd_version = TD }
145 static const struct {
148 enum rtl_tx_desc_version txd_version;
149 } rtl_chip_info[] = {
150 _R("RTL8169", RTL_GIGA_MAC_VER_01, RTL_TD_0), // 8169
151 _R("RTL8169s", RTL_GIGA_MAC_VER_02, RTL_TD_0), // 8169S
152 _R("RTL8110s", RTL_GIGA_MAC_VER_03, RTL_TD_0), // 8110S
153 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, RTL_TD_0), // 8169SB
154 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, RTL_TD_0), // 8110SCd
155 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, RTL_TD_0), // 8110SCe
156 _R("RTL8102e", RTL_GIGA_MAC_VER_07, RTL_TD_1), // PCI-E
157 _R("RTL8102e", RTL_GIGA_MAC_VER_08, RTL_TD_1), // PCI-E
158 _R("RTL8102e", RTL_GIGA_MAC_VER_09, RTL_TD_1), // PCI-E
159 _R("RTL8101e", RTL_GIGA_MAC_VER_10, RTL_TD_0), // PCI-E
160 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, RTL_TD_0), // PCI-E
161 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, RTL_TD_0), // PCI-E
162 _R("RTL8101e", RTL_GIGA_MAC_VER_13, RTL_TD_0), // PCI-E 8139
163 _R("RTL8100e", RTL_GIGA_MAC_VER_14, RTL_TD_0), // PCI-E 8139
164 _R("RTL8100e", RTL_GIGA_MAC_VER_15, RTL_TD_0), // PCI-E 8139
165 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, RTL_TD_0), // PCI-E
166 _R("RTL8101e", RTL_GIGA_MAC_VER_16, RTL_TD_0), // PCI-E
167 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, RTL_TD_1), // PCI-E
168 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, RTL_TD_1), // PCI-E
169 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, RTL_TD_1), // PCI-E
170 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, RTL_TD_1), // PCI-E
171 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, RTL_TD_1), // PCI-E
172 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, RTL_TD_1), // PCI-E
173 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, RTL_TD_1), // PCI-E
174 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, RTL_TD_1), // PCI-E
175 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, RTL_TD_1), // PCI-E
176 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, RTL_TD_1), // PCI-E
177 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, RTL_TD_1), // PCI-E
178 _R("RTL8105e", RTL_GIGA_MAC_VER_29, RTL_TD_1), // PCI-E
179 _R("RTL8105e", RTL_GIGA_MAC_VER_30, RTL_TD_1), // PCI-E
180 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_31, RTL_TD_1), // PCI-E
181 _R("RTL8168e/8111e", RTL_GIGA_MAC_VER_32, RTL_TD_1), // PCI-E
182 _R("RTL8168e/8111e", RTL_GIGA_MAC_VER_33, RTL_TD_1) // PCI-E
186 static const struct rtl_firmware_info {
189 } rtl_firmware_infos[] = {
190 { .mac_version = RTL_GIGA_MAC_VER_25, .fw_name = FIRMWARE_8168D_1 },
191 { .mac_version = RTL_GIGA_MAC_VER_26, .fw_name = FIRMWARE_8168D_2 },
192 { .mac_version = RTL_GIGA_MAC_VER_29, .fw_name = FIRMWARE_8105E_1 },
193 { .mac_version = RTL_GIGA_MAC_VER_30, .fw_name = FIRMWARE_8105E_1 },
194 { .mac_version = RTL_GIGA_MAC_VER_30, .fw_name = FIRMWARE_8105E_1 },
195 { .mac_version = RTL_GIGA_MAC_VER_31, .fw_name = FIRMWARE_8168E_1 },
196 { .mac_version = RTL_GIGA_MAC_VER_32, .fw_name = FIRMWARE_8168E_2 }
205 static void rtl_hw_start_8169(struct net_device *);
206 static void rtl_hw_start_8168(struct net_device *);
207 static void rtl_hw_start_8101(struct net_device *);
209 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
210 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
211 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
212 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
213 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
214 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
215 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
216 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
217 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
218 { PCI_VENDOR_ID_LINKSYS, 0x1032,
219 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
221 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
225 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
227 static int rx_buf_sz = 16383;
234 MAC0 = 0, /* Ethernet hardware address. */
236 MAR0 = 8, /* Multicast filter. */
237 CounterAddrLow = 0x10,
238 CounterAddrHigh = 0x14,
239 TxDescStartAddrLow = 0x20,
240 TxDescStartAddrHigh = 0x24,
241 TxHDescStartAddrLow = 0x28,
242 TxHDescStartAddrHigh = 0x2c,
252 #define RTL_RX_CONFIG_MASK 0xff7e1880u
268 RxDescAddrLow = 0xe4,
269 RxDescAddrHigh = 0xe8,
270 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
272 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
274 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
276 #define TxPacketMax (8064 >> 7)
279 FuncEventMask = 0xf4,
280 FuncPresetState = 0xf8,
281 FuncForceEvent = 0xfc,
284 enum rtl8110_registers {
290 enum rtl8168_8101_registers {
293 #define CSIAR_FLAG 0x80000000
294 #define CSIAR_WRITE_CMD 0x80000000
295 #define CSIAR_BYTE_ENABLE 0x0f
296 #define CSIAR_BYTE_ENABLE_SHIFT 12
297 #define CSIAR_ADDR_MASK 0x0fff
300 #define EPHYAR_FLAG 0x80000000
301 #define EPHYAR_WRITE_CMD 0x80000000
302 #define EPHYAR_REG_MASK 0x1f
303 #define EPHYAR_REG_SHIFT 16
304 #define EPHYAR_DATA_MASK 0xffff
306 #define PM_SWITCH (1 << 6)
308 #define FIX_NAK_1 (1 << 4)
309 #define FIX_NAK_2 (1 << 3)
312 #define EN_NDP (1 << 3)
313 #define EN_OOB_RESET (1 << 2)
315 #define EFUSEAR_FLAG 0x80000000
316 #define EFUSEAR_WRITE_CMD 0x80000000
317 #define EFUSEAR_READ_CMD 0x00000000
318 #define EFUSEAR_REG_MASK 0x03ff
319 #define EFUSEAR_REG_SHIFT 8
320 #define EFUSEAR_DATA_MASK 0xff
323 enum rtl8168_registers {
326 #define ERIAR_FLAG 0x80000000
327 #define ERIAR_WRITE_CMD 0x80000000
328 #define ERIAR_READ_CMD 0x00000000
329 #define ERIAR_ADDR_BYTE_ALIGN 4
330 #define ERIAR_EXGMAC 0
333 #define ERIAR_TYPE_SHIFT 16
334 #define ERIAR_BYTEEN 0x0f
335 #define ERIAR_BYTEEN_SHIFT 12
336 EPHY_RXER_NUM = 0x7c,
337 OCPDR = 0xb0, /* OCP GPHY access */
338 #define OCPDR_WRITE_CMD 0x80000000
339 #define OCPDR_READ_CMD 0x00000000
340 #define OCPDR_REG_MASK 0x7f
341 #define OCPDR_GPHY_REG_SHIFT 16
342 #define OCPDR_DATA_MASK 0xffff
344 #define OCPAR_FLAG 0x80000000
345 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
346 #define OCPAR_GPHY_READ_CMD 0x0000f060
347 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
348 MISC = 0xf0, /* 8168e only. */
349 txpla_rst = (1 << 29)
352 enum rtl_register_content {
353 /* InterruptStatusBits */
357 TxDescUnavail = 0x0080,
379 /* TXPoll register p.5 */
380 HPQ = 0x80, /* Poll cmd on the high prio queue */
381 NPQ = 0x40, /* Poll cmd on the low prio queue */
382 FSWInt = 0x01, /* Forced software interrupt */
386 Cfg9346_Unlock = 0xc0,
391 AcceptBroadcast = 0x08,
392 AcceptMulticast = 0x04,
394 AcceptAllPhys = 0x01,
401 TxInterFrameGapShift = 24,
402 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
404 /* Config1 register p.24 */
407 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
408 Speed_down = (1 << 4),
412 PMEnable = (1 << 0), /* Power Management Enable */
414 /* Config2 register p. 25 */
415 PCI_Clock_66MHz = 0x01,
416 PCI_Clock_33MHz = 0x00,
418 /* Config3 register p.25 */
419 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
420 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
421 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
423 /* Config5 register p.27 */
424 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
425 MWF = (1 << 5), /* Accept Multicast wakeup frame */
426 UWF = (1 << 4), /* Accept Unicast wakeup frame */
428 LanWake = (1 << 1), /* LanWake enable/disable */
429 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
432 TBIReset = 0x80000000,
433 TBILoopback = 0x40000000,
434 TBINwEnable = 0x20000000,
435 TBINwRestart = 0x10000000,
436 TBILinkOk = 0x02000000,
437 TBINwComplete = 0x01000000,
440 EnableBist = (1 << 15), // 8168 8101
441 Mac_dbgo_oe = (1 << 14), // 8168 8101
442 Normal_mode = (1 << 13), // unused
443 Force_half_dup = (1 << 12), // 8168 8101
444 Force_rxflow_en = (1 << 11), // 8168 8101
445 Force_txflow_en = (1 << 10), // 8168 8101
446 Cxpl_dbg_sel = (1 << 9), // 8168 8101
447 ASF = (1 << 8), // 8168 8101
448 PktCntrDisable = (1 << 7), // 8168 8101
449 Mac_dbgo_sel = 0x001c, // 8168
454 INTT_0 = 0x0000, // 8168
455 INTT_1 = 0x0001, // 8168
456 INTT_2 = 0x0002, // 8168
457 INTT_3 = 0x0003, // 8168
459 /* rtl8169_PHYstatus */
470 TBILinkOK = 0x02000000,
472 /* DumpCounterCommand */
477 /* First doubleword. */
478 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
479 RingEnd = (1 << 30), /* End of descriptor ring */
480 FirstFrag = (1 << 29), /* First segment of a packet */
481 LastFrag = (1 << 28), /* Final segment of a packet */
485 enum rtl_tx_desc_bit {
486 /* First doubleword. */
487 TD_LSO = (1 << 27), /* Large Send Offload */
488 #define TD_MSS_MAX 0x07ffu /* MSS value */
490 /* Second doubleword. */
491 TxVlanTag = (1 << 17), /* Add VLAN tag */
494 /* 8169, 8168b and 810x except 8102e. */
495 enum rtl_tx_desc_bit_0 {
496 /* First doubleword. */
497 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
498 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
499 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
500 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
503 /* 8102e, 8168c and beyond. */
504 enum rtl_tx_desc_bit_1 {
505 /* Second doubleword. */
506 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
507 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
508 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
509 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
512 static const struct rtl_tx_desc_info {
519 } tx_desc_info [] = {
522 .udp = TD0_IP_CS | TD0_UDP_CS,
523 .tcp = TD0_IP_CS | TD0_TCP_CS
525 .mss_shift = TD0_MSS_SHIFT,
530 .udp = TD1_IP_CS | TD1_UDP_CS,
531 .tcp = TD1_IP_CS | TD1_TCP_CS
533 .mss_shift = TD1_MSS_SHIFT,
538 enum rtl_rx_desc_bit {
540 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
541 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
543 #define RxProtoUDP (PID1)
544 #define RxProtoTCP (PID0)
545 #define RxProtoIP (PID1 | PID0)
546 #define RxProtoMask RxProtoIP
548 IPFail = (1 << 16), /* IP checksum failed */
549 UDPFail = (1 << 15), /* UDP/IP checksum failed */
550 TCPFail = (1 << 14), /* TCP/IP checksum failed */
551 RxVlanTag = (1 << 16), /* VLAN tag available */
554 #define RsvdMask 0x3fffc000
571 u8 __pad[sizeof(void *) - sizeof(u32)];
575 RTL_FEATURE_WOL = (1 << 0),
576 RTL_FEATURE_MSI = (1 << 1),
577 RTL_FEATURE_GMII = (1 << 2),
580 struct rtl8169_counters {
587 __le32 tx_one_collision;
588 __le32 tx_multi_collision;
596 struct rtl8169_private {
597 void __iomem *mmio_addr; /* memory map physical address */
598 struct pci_dev *pci_dev; /* Index of PCI device */
599 struct net_device *dev;
600 struct napi_struct napi;
601 spinlock_t lock; /* spin lock flag */
605 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
606 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
609 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
610 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
611 dma_addr_t TxPhyAddr;
612 dma_addr_t RxPhyAddr;
613 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
614 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
615 struct timer_list timer;
620 int phy_1000_ctrl_reg;
623 void (*write)(void __iomem *, int, int);
624 int (*read)(void __iomem *, int);
627 struct pll_power_ops {
628 void (*down)(struct rtl8169_private *);
629 void (*up)(struct rtl8169_private *);
632 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
633 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
634 void (*phy_reset_enable)(struct rtl8169_private *tp);
635 void (*hw_start)(struct net_device *);
636 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
637 unsigned int (*link_ok)(void __iomem *);
638 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
640 struct delayed_work task;
643 struct mii_if_info mii;
644 struct rtl8169_counters counters;
647 const struct firmware *fw;
648 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
651 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
652 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
653 module_param(use_dac, int, 0);
654 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
655 module_param_named(debug, debug.msg_enable, int, 0);
656 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
657 MODULE_LICENSE("GPL");
658 MODULE_VERSION(RTL8169_VERSION);
659 MODULE_FIRMWARE(FIRMWARE_8168D_1);
660 MODULE_FIRMWARE(FIRMWARE_8168D_2);
661 MODULE_FIRMWARE(FIRMWARE_8168E_1);
662 MODULE_FIRMWARE(FIRMWARE_8168E_2);
663 MODULE_FIRMWARE(FIRMWARE_8105E_1);
665 static int rtl8169_open(struct net_device *dev);
666 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
667 struct net_device *dev);
668 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
669 static int rtl8169_init_ring(struct net_device *dev);
670 static void rtl_hw_start(struct net_device *dev);
671 static int rtl8169_close(struct net_device *dev);
672 static void rtl_set_rx_mode(struct net_device *dev);
673 static void rtl8169_tx_timeout(struct net_device *dev);
674 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
675 static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
676 void __iomem *, u32 budget);
677 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
678 static void rtl8169_down(struct net_device *dev);
679 static void rtl8169_rx_clear(struct rtl8169_private *tp);
680 static int rtl8169_poll(struct napi_struct *napi, int budget);
682 static const unsigned int rtl8169_rx_config =
683 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
685 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
687 void __iomem *ioaddr = tp->mmio_addr;
690 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
691 for (i = 0; i < 20; i++) {
693 if (RTL_R32(OCPAR) & OCPAR_FLAG)
696 return RTL_R32(OCPDR);
699 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
701 void __iomem *ioaddr = tp->mmio_addr;
704 RTL_W32(OCPDR, data);
705 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
706 for (i = 0; i < 20; i++) {
708 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
713 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
715 void __iomem *ioaddr = tp->mmio_addr;
719 RTL_W32(ERIAR, 0x800010e8);
721 for (i = 0; i < 5; i++) {
723 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
727 ocp_write(tp, 0x1, 0x30, 0x00000001);
730 #define OOB_CMD_RESET 0x00
731 #define OOB_CMD_DRIVER_START 0x05
732 #define OOB_CMD_DRIVER_STOP 0x06
734 static void rtl8168_driver_start(struct rtl8169_private *tp)
739 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
741 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
746 for (i = 0; i < 10; i++) {
748 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
753 static void rtl8168_driver_stop(struct rtl8169_private *tp)
758 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
760 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
765 for (i = 0; i < 10; i++) {
767 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
772 static int r8168dp_check_dash(struct rtl8169_private *tp)
776 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
781 if (ocp_read(tp, 0xF, reg) & 0x00008000)
787 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
791 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
793 for (i = 20; i > 0; i--) {
795 * Check if the RTL8169 has completed writing to the specified
798 if (!(RTL_R32(PHYAR) & 0x80000000))
803 * According to hardware specs a 20us delay is required after write
804 * complete indication, but before sending next command.
809 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
813 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
815 for (i = 20; i > 0; i--) {
817 * Check if the RTL8169 has completed retrieving data from
818 * the specified MII register.
820 if (RTL_R32(PHYAR) & 0x80000000) {
821 value = RTL_R32(PHYAR) & 0xffff;
827 * According to hardware specs a 20us delay is required after read
828 * complete indication, but before sending next command.
835 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
839 RTL_W32(OCPDR, data |
840 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
841 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
842 RTL_W32(EPHY_RXER_NUM, 0);
844 for (i = 0; i < 100; i++) {
846 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
851 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
853 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
854 (value & OCPDR_DATA_MASK));
857 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
861 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
864 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
865 RTL_W32(EPHY_RXER_NUM, 0);
867 for (i = 0; i < 100; i++) {
869 if (RTL_R32(OCPAR) & OCPAR_FLAG)
873 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
876 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
878 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
880 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
883 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
885 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
888 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
890 r8168dp_2_mdio_start(ioaddr);
892 r8169_mdio_write(ioaddr, reg_addr, value);
894 r8168dp_2_mdio_stop(ioaddr);
897 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
901 r8168dp_2_mdio_start(ioaddr);
903 value = r8169_mdio_read(ioaddr, reg_addr);
905 r8168dp_2_mdio_stop(ioaddr);
910 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
912 tp->mdio_ops.write(tp->mmio_addr, location, val);
915 static int rtl_readphy(struct rtl8169_private *tp, int location)
917 return tp->mdio_ops.read(tp->mmio_addr, location);
920 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
922 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
925 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
929 val = rtl_readphy(tp, reg_addr);
930 rtl_writephy(tp, reg_addr, (val | p) & ~m);
933 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
936 struct rtl8169_private *tp = netdev_priv(dev);
938 rtl_writephy(tp, location, val);
941 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
943 struct rtl8169_private *tp = netdev_priv(dev);
945 return rtl_readphy(tp, location);
948 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
952 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
953 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
955 for (i = 0; i < 100; i++) {
956 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
962 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
967 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
969 for (i = 0; i < 100; i++) {
970 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
971 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
980 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
984 RTL_W32(CSIDR, value);
985 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
986 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
988 for (i = 0; i < 100; i++) {
989 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
995 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1000 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1001 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1003 for (i = 0; i < 100; i++) {
1004 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1005 value = RTL_R32(CSIDR);
1014 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1019 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1021 for (i = 0; i < 300; i++) {
1022 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1023 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1032 static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1034 RTL_W16(IntrMask, 0x0000);
1036 RTL_W16(IntrStatus, 0xffff);
1039 static void rtl8169_asic_down(void __iomem *ioaddr)
1041 RTL_W8(ChipCmd, 0x00);
1042 rtl8169_irq_mask_and_ack(ioaddr);
1046 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1048 void __iomem *ioaddr = tp->mmio_addr;
1050 return RTL_R32(TBICSR) & TBIReset;
1053 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1055 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1058 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1060 return RTL_R32(TBICSR) & TBILinkOk;
1063 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1065 return RTL_R8(PHYstatus) & LinkStatus;
1068 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1070 void __iomem *ioaddr = tp->mmio_addr;
1072 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1075 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1079 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1080 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1083 static void __rtl8169_check_link_status(struct net_device *dev,
1084 struct rtl8169_private *tp,
1085 void __iomem *ioaddr,
1088 unsigned long flags;
1090 spin_lock_irqsave(&tp->lock, flags);
1091 if (tp->link_ok(ioaddr)) {
1092 /* This is to cancel a scheduled suspend if there's one. */
1094 pm_request_resume(&tp->pci_dev->dev);
1095 netif_carrier_on(dev);
1096 if (net_ratelimit())
1097 netif_info(tp, ifup, dev, "link up\n");
1099 netif_carrier_off(dev);
1100 netif_info(tp, ifdown, dev, "link down\n");
1102 pm_schedule_suspend(&tp->pci_dev->dev, 100);
1104 spin_unlock_irqrestore(&tp->lock, flags);
1107 static void rtl8169_check_link_status(struct net_device *dev,
1108 struct rtl8169_private *tp,
1109 void __iomem *ioaddr)
1111 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1114 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1116 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1118 void __iomem *ioaddr = tp->mmio_addr;
1122 options = RTL_R8(Config1);
1123 if (!(options & PMEnable))
1126 options = RTL_R8(Config3);
1127 if (options & LinkUp)
1128 wolopts |= WAKE_PHY;
1129 if (options & MagicPacket)
1130 wolopts |= WAKE_MAGIC;
1132 options = RTL_R8(Config5);
1134 wolopts |= WAKE_UCAST;
1136 wolopts |= WAKE_BCAST;
1138 wolopts |= WAKE_MCAST;
1143 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1145 struct rtl8169_private *tp = netdev_priv(dev);
1147 spin_lock_irq(&tp->lock);
1149 wol->supported = WAKE_ANY;
1150 wol->wolopts = __rtl8169_get_wol(tp);
1152 spin_unlock_irq(&tp->lock);
1155 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1157 void __iomem *ioaddr = tp->mmio_addr;
1159 static const struct {
1164 { WAKE_ANY, Config1, PMEnable },
1165 { WAKE_PHY, Config3, LinkUp },
1166 { WAKE_MAGIC, Config3, MagicPacket },
1167 { WAKE_UCAST, Config5, UWF },
1168 { WAKE_BCAST, Config5, BWF },
1169 { WAKE_MCAST, Config5, MWF },
1170 { WAKE_ANY, Config5, LanWake }
1173 RTL_W8(Cfg9346, Cfg9346_Unlock);
1175 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1176 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1177 if (wolopts & cfg[i].opt)
1178 options |= cfg[i].mask;
1179 RTL_W8(cfg[i].reg, options);
1182 RTL_W8(Cfg9346, Cfg9346_Lock);
1185 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1187 struct rtl8169_private *tp = netdev_priv(dev);
1189 spin_lock_irq(&tp->lock);
1192 tp->features |= RTL_FEATURE_WOL;
1194 tp->features &= ~RTL_FEATURE_WOL;
1195 __rtl8169_set_wol(tp, wol->wolopts);
1196 spin_unlock_irq(&tp->lock);
1198 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1203 static void rtl8169_get_drvinfo(struct net_device *dev,
1204 struct ethtool_drvinfo *info)
1206 struct rtl8169_private *tp = netdev_priv(dev);
1208 strcpy(info->driver, MODULENAME);
1209 strcpy(info->version, RTL8169_VERSION);
1210 strcpy(info->bus_info, pci_name(tp->pci_dev));
1213 static int rtl8169_get_regs_len(struct net_device *dev)
1215 return R8169_REGS_SIZE;
1218 static int rtl8169_set_speed_tbi(struct net_device *dev,
1219 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1221 struct rtl8169_private *tp = netdev_priv(dev);
1222 void __iomem *ioaddr = tp->mmio_addr;
1226 reg = RTL_R32(TBICSR);
1227 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1228 (duplex == DUPLEX_FULL)) {
1229 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1230 } else if (autoneg == AUTONEG_ENABLE)
1231 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1233 netif_warn(tp, link, dev,
1234 "incorrect speed setting refused in TBI mode\n");
1241 static int rtl8169_set_speed_xmii(struct net_device *dev,
1242 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1244 struct rtl8169_private *tp = netdev_priv(dev);
1245 int giga_ctrl, bmcr;
1248 rtl_writephy(tp, 0x1f, 0x0000);
1250 if (autoneg == AUTONEG_ENABLE) {
1253 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1254 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1255 ADVERTISE_100HALF | ADVERTISE_100FULL);
1257 if (adv & ADVERTISED_10baseT_Half)
1258 auto_nego |= ADVERTISE_10HALF;
1259 if (adv & ADVERTISED_10baseT_Full)
1260 auto_nego |= ADVERTISE_10FULL;
1261 if (adv & ADVERTISED_100baseT_Half)
1262 auto_nego |= ADVERTISE_100HALF;
1263 if (adv & ADVERTISED_100baseT_Full)
1264 auto_nego |= ADVERTISE_100FULL;
1266 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1268 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1269 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1271 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1272 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1273 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1274 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1275 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1276 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1277 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1278 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
1279 (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
1280 (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
1281 (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
1282 if (adv & ADVERTISED_1000baseT_Half)
1283 giga_ctrl |= ADVERTISE_1000HALF;
1284 if (adv & ADVERTISED_1000baseT_Full)
1285 giga_ctrl |= ADVERTISE_1000FULL;
1286 } else if (adv & (ADVERTISED_1000baseT_Half |
1287 ADVERTISED_1000baseT_Full)) {
1288 netif_info(tp, link, dev,
1289 "PHY does not support 1000Mbps\n");
1293 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1295 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1296 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1300 if (speed == SPEED_10)
1302 else if (speed == SPEED_100)
1303 bmcr = BMCR_SPEED100;
1307 if (duplex == DUPLEX_FULL)
1308 bmcr |= BMCR_FULLDPLX;
1311 tp->phy_1000_ctrl_reg = giga_ctrl;
1313 rtl_writephy(tp, MII_BMCR, bmcr);
1315 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
1316 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
1317 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1318 rtl_writephy(tp, 0x17, 0x2138);
1319 rtl_writephy(tp, 0x0e, 0x0260);
1321 rtl_writephy(tp, 0x17, 0x2108);
1322 rtl_writephy(tp, 0x0e, 0x0000);
1331 static int rtl8169_set_speed(struct net_device *dev,
1332 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1334 struct rtl8169_private *tp = netdev_priv(dev);
1337 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1339 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
1340 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1345 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1347 struct rtl8169_private *tp = netdev_priv(dev);
1348 unsigned long flags;
1351 spin_lock_irqsave(&tp->lock, flags);
1352 ret = rtl8169_set_speed(dev,
1353 cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
1354 spin_unlock_irqrestore(&tp->lock, flags);
1359 static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
1361 if (dev->mtu > TD_MSS_MAX)
1362 features &= ~NETIF_F_ALL_TSO;
1367 static int rtl8169_set_features(struct net_device *dev, u32 features)
1369 struct rtl8169_private *tp = netdev_priv(dev);
1370 void __iomem *ioaddr = tp->mmio_addr;
1371 unsigned long flags;
1373 spin_lock_irqsave(&tp->lock, flags);
1375 if (features & NETIF_F_RXCSUM)
1376 tp->cp_cmd |= RxChkSum;
1378 tp->cp_cmd &= ~RxChkSum;
1380 if (dev->features & NETIF_F_HW_VLAN_RX)
1381 tp->cp_cmd |= RxVlan;
1383 tp->cp_cmd &= ~RxVlan;
1385 RTL_W16(CPlusCmd, tp->cp_cmd);
1388 spin_unlock_irqrestore(&tp->lock, flags);
1393 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1394 struct sk_buff *skb)
1396 return (vlan_tx_tag_present(skb)) ?
1397 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1400 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1402 u32 opts2 = le32_to_cpu(desc->opts2);
1404 if (opts2 & RxVlanTag)
1405 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1410 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1412 struct rtl8169_private *tp = netdev_priv(dev);
1413 void __iomem *ioaddr = tp->mmio_addr;
1417 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1418 cmd->port = PORT_FIBRE;
1419 cmd->transceiver = XCVR_INTERNAL;
1421 status = RTL_R32(TBICSR);
1422 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1423 cmd->autoneg = !!(status & TBINwEnable);
1425 cmd->speed = SPEED_1000;
1426 cmd->duplex = DUPLEX_FULL; /* Always set */
1431 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1433 struct rtl8169_private *tp = netdev_priv(dev);
1435 return mii_ethtool_gset(&tp->mii, cmd);
1438 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1440 struct rtl8169_private *tp = netdev_priv(dev);
1441 unsigned long flags;
1444 spin_lock_irqsave(&tp->lock, flags);
1446 rc = tp->get_settings(dev, cmd);
1448 spin_unlock_irqrestore(&tp->lock, flags);
1452 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1455 struct rtl8169_private *tp = netdev_priv(dev);
1456 unsigned long flags;
1458 if (regs->len > R8169_REGS_SIZE)
1459 regs->len = R8169_REGS_SIZE;
1461 spin_lock_irqsave(&tp->lock, flags);
1462 memcpy_fromio(p, tp->mmio_addr, regs->len);
1463 spin_unlock_irqrestore(&tp->lock, flags);
1466 static u32 rtl8169_get_msglevel(struct net_device *dev)
1468 struct rtl8169_private *tp = netdev_priv(dev);
1470 return tp->msg_enable;
1473 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1475 struct rtl8169_private *tp = netdev_priv(dev);
1477 tp->msg_enable = value;
1480 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1487 "tx_single_collisions",
1488 "tx_multi_collisions",
1496 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1500 return ARRAY_SIZE(rtl8169_gstrings);
1506 static void rtl8169_update_counters(struct net_device *dev)
1508 struct rtl8169_private *tp = netdev_priv(dev);
1509 void __iomem *ioaddr = tp->mmio_addr;
1510 struct rtl8169_counters *counters;
1514 struct device *d = &tp->pci_dev->dev;
1517 * Some chips are unable to dump tally counters when the receiver
1520 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1523 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1527 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1528 cmd = (u64)paddr & DMA_BIT_MASK(32);
1529 RTL_W32(CounterAddrLow, cmd);
1530 RTL_W32(CounterAddrLow, cmd | CounterDump);
1533 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1534 /* copy updated counters */
1535 memcpy(&tp->counters, counters, sizeof(*counters));
1541 RTL_W32(CounterAddrLow, 0);
1542 RTL_W32(CounterAddrHigh, 0);
1544 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1547 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1548 struct ethtool_stats *stats, u64 *data)
1550 struct rtl8169_private *tp = netdev_priv(dev);
1554 rtl8169_update_counters(dev);
1556 data[0] = le64_to_cpu(tp->counters.tx_packets);
1557 data[1] = le64_to_cpu(tp->counters.rx_packets);
1558 data[2] = le64_to_cpu(tp->counters.tx_errors);
1559 data[3] = le32_to_cpu(tp->counters.rx_errors);
1560 data[4] = le16_to_cpu(tp->counters.rx_missed);
1561 data[5] = le16_to_cpu(tp->counters.align_errors);
1562 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1563 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1564 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1565 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1566 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1567 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1568 data[12] = le16_to_cpu(tp->counters.tx_underun);
1571 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1575 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1580 static const struct ethtool_ops rtl8169_ethtool_ops = {
1581 .get_drvinfo = rtl8169_get_drvinfo,
1582 .get_regs_len = rtl8169_get_regs_len,
1583 .get_link = ethtool_op_get_link,
1584 .get_settings = rtl8169_get_settings,
1585 .set_settings = rtl8169_set_settings,
1586 .get_msglevel = rtl8169_get_msglevel,
1587 .set_msglevel = rtl8169_set_msglevel,
1588 .get_regs = rtl8169_get_regs,
1589 .get_wol = rtl8169_get_wol,
1590 .set_wol = rtl8169_set_wol,
1591 .get_strings = rtl8169_get_strings,
1592 .get_sset_count = rtl8169_get_sset_count,
1593 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1596 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1597 void __iomem *ioaddr)
1600 * The driver currently handles the 8168Bf and the 8168Be identically
1601 * but they can be identified more specifically through the test below
1604 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1606 * Same thing for the 8101Eb and the 8101Ec:
1608 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1610 static const struct {
1616 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1617 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1618 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1621 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1622 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1623 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1625 /* 8168DP family. */
1626 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1627 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1628 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
1631 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1632 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1633 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1634 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1635 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1636 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1637 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1638 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1639 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1642 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1643 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1644 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1645 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1648 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
1649 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1650 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1651 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1652 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1653 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1654 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1655 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1656 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1657 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1658 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1659 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1660 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1661 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1662 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1663 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1664 /* FIXME: where did these entries come from ? -- FR */
1665 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1666 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1669 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1670 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1671 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1672 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1673 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1674 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1677 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1681 reg = RTL_R32(TxConfig);
1682 while ((reg & p->mask) != p->val)
1684 tp->mac_version = p->mac_version;
1687 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1689 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1697 static void rtl_writephy_batch(struct rtl8169_private *tp,
1698 const struct phy_reg *regs, int len)
1701 rtl_writephy(tp, regs->reg, regs->val);
1706 #define PHY_READ 0x00000000
1707 #define PHY_DATA_OR 0x10000000
1708 #define PHY_DATA_AND 0x20000000
1709 #define PHY_BJMPN 0x30000000
1710 #define PHY_READ_EFUSE 0x40000000
1711 #define PHY_READ_MAC_BYTE 0x50000000
1712 #define PHY_WRITE_MAC_BYTE 0x60000000
1713 #define PHY_CLEAR_READCOUNT 0x70000000
1714 #define PHY_WRITE 0x80000000
1715 #define PHY_READCOUNT_EQ_SKIP 0x90000000
1716 #define PHY_COMP_EQ_SKIPN 0xa0000000
1717 #define PHY_COMP_NEQ_SKIPN 0xb0000000
1718 #define PHY_WRITE_PREVIOUS 0xc0000000
1719 #define PHY_SKIPN 0xd0000000
1720 #define PHY_DELAY_MS 0xe0000000
1721 #define PHY_WRITE_ERI_WORD 0xf0000000
1724 rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1726 __le32 *phytable = (__le32 *)fw->data;
1727 struct net_device *dev = tp->dev;
1728 size_t index, fw_size = fw->size / sizeof(*phytable);
1731 if (fw->size % sizeof(*phytable)) {
1732 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1736 for (index = 0; index < fw_size; index++) {
1737 u32 action = le32_to_cpu(phytable[index]);
1738 u32 regno = (action & 0x0fff0000) >> 16;
1740 switch(action & 0xf0000000) {
1744 case PHY_READ_EFUSE:
1745 case PHY_CLEAR_READCOUNT:
1747 case PHY_WRITE_PREVIOUS:
1752 if (regno > index) {
1753 netif_err(tp, probe, tp->dev,
1754 "Out of range of firmware\n");
1758 case PHY_READCOUNT_EQ_SKIP:
1759 if (index + 2 >= fw_size) {
1760 netif_err(tp, probe, tp->dev,
1761 "Out of range of firmware\n");
1765 case PHY_COMP_EQ_SKIPN:
1766 case PHY_COMP_NEQ_SKIPN:
1768 if (index + 1 + regno >= fw_size) {
1769 netif_err(tp, probe, tp->dev,
1770 "Out of range of firmware\n");
1775 case PHY_READ_MAC_BYTE:
1776 case PHY_WRITE_MAC_BYTE:
1777 case PHY_WRITE_ERI_WORD:
1779 netif_err(tp, probe, tp->dev,
1780 "Invalid action 0x%08x\n", action);
1788 for (index = 0; index < fw_size; ) {
1789 u32 action = le32_to_cpu(phytable[index]);
1790 u32 data = action & 0x0000ffff;
1791 u32 regno = (action & 0x0fff0000) >> 16;
1796 switch(action & 0xf0000000) {
1798 predata = rtl_readphy(tp, regno);
1813 case PHY_READ_EFUSE:
1814 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1817 case PHY_CLEAR_READCOUNT:
1822 rtl_writephy(tp, regno, data);
1825 case PHY_READCOUNT_EQ_SKIP:
1831 case PHY_COMP_EQ_SKIPN:
1832 if (predata == data)
1836 case PHY_COMP_NEQ_SKIPN:
1837 if (predata != data)
1841 case PHY_WRITE_PREVIOUS:
1842 rtl_writephy(tp, regno, predata);
1853 case PHY_READ_MAC_BYTE:
1854 case PHY_WRITE_MAC_BYTE:
1855 case PHY_WRITE_ERI_WORD:
1862 static void rtl_release_firmware(struct rtl8169_private *tp)
1864 if (!IS_ERR_OR_NULL(tp->fw))
1865 release_firmware(tp->fw);
1866 tp->fw = RTL_FIRMWARE_UNKNOWN;
1869 static void rtl_apply_firmware(struct rtl8169_private *tp)
1871 const struct firmware *fw = tp->fw;
1873 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1874 if (!IS_ERR_OR_NULL(fw))
1875 rtl_phy_write_fw(tp, fw);
1878 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1880 if (rtl_readphy(tp, reg) != val)
1881 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1883 rtl_apply_firmware(tp);
1886 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
1888 static const struct phy_reg phy_reg_init[] = {
1950 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1953 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
1955 static const struct phy_reg phy_reg_init[] = {
1961 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1964 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
1966 struct pci_dev *pdev = tp->pci_dev;
1967 u16 vendor_id, device_id;
1969 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1970 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1972 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1975 rtl_writephy(tp, 0x1f, 0x0001);
1976 rtl_writephy(tp, 0x10, 0xf01b);
1977 rtl_writephy(tp, 0x1f, 0x0000);
1980 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
1982 static const struct phy_reg phy_reg_init[] = {
2022 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2024 rtl8169scd_hw_phy_config_quirk(tp);
2027 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2029 static const struct phy_reg phy_reg_init[] = {
2077 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2080 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2082 static const struct phy_reg phy_reg_init[] = {
2087 rtl_writephy(tp, 0x1f, 0x0001);
2088 rtl_patchphy(tp, 0x16, 1 << 0);
2090 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2093 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2095 static const struct phy_reg phy_reg_init[] = {
2101 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2104 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2106 static const struct phy_reg phy_reg_init[] = {
2114 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2117 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2119 static const struct phy_reg phy_reg_init[] = {
2125 rtl_writephy(tp, 0x1f, 0x0000);
2126 rtl_patchphy(tp, 0x14, 1 << 5);
2127 rtl_patchphy(tp, 0x0d, 1 << 5);
2129 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2132 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2134 static const struct phy_reg phy_reg_init[] = {
2154 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2156 rtl_patchphy(tp, 0x14, 1 << 5);
2157 rtl_patchphy(tp, 0x0d, 1 << 5);
2158 rtl_writephy(tp, 0x1f, 0x0000);
2161 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2163 static const struct phy_reg phy_reg_init[] = {
2181 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2183 rtl_patchphy(tp, 0x16, 1 << 0);
2184 rtl_patchphy(tp, 0x14, 1 << 5);
2185 rtl_patchphy(tp, 0x0d, 1 << 5);
2186 rtl_writephy(tp, 0x1f, 0x0000);
2189 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2191 static const struct phy_reg phy_reg_init[] = {
2203 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2205 rtl_patchphy(tp, 0x16, 1 << 0);
2206 rtl_patchphy(tp, 0x14, 1 << 5);
2207 rtl_patchphy(tp, 0x0d, 1 << 5);
2208 rtl_writephy(tp, 0x1f, 0x0000);
2211 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2213 rtl8168c_3_hw_phy_config(tp);
2216 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2218 static const struct phy_reg phy_reg_init_0[] = {
2219 /* Channel Estimation */
2240 * enhance line driver power
2249 * Can not link to 1Gbps with bad cable
2250 * Decrease SNR threshold form 21.07dB to 19.04dB
2258 void __iomem *ioaddr = tp->mmio_addr;
2260 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2264 * Fine Tune Switching regulator parameter
2266 rtl_writephy(tp, 0x1f, 0x0002);
2267 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2268 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2270 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2271 static const struct phy_reg phy_reg_init[] = {
2281 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2283 val = rtl_readphy(tp, 0x0d);
2285 if ((val & 0x00ff) != 0x006c) {
2286 static const u32 set[] = {
2287 0x0065, 0x0066, 0x0067, 0x0068,
2288 0x0069, 0x006a, 0x006b, 0x006c
2292 rtl_writephy(tp, 0x1f, 0x0002);
2295 for (i = 0; i < ARRAY_SIZE(set); i++)
2296 rtl_writephy(tp, 0x0d, val | set[i]);
2299 static const struct phy_reg phy_reg_init[] = {
2307 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2310 /* RSET couple improve */
2311 rtl_writephy(tp, 0x1f, 0x0002);
2312 rtl_patchphy(tp, 0x0d, 0x0300);
2313 rtl_patchphy(tp, 0x0f, 0x0010);
2315 /* Fine tune PLL performance */
2316 rtl_writephy(tp, 0x1f, 0x0002);
2317 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2318 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2320 rtl_writephy(tp, 0x1f, 0x0005);
2321 rtl_writephy(tp, 0x05, 0x001b);
2323 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2325 rtl_writephy(tp, 0x1f, 0x0000);
2328 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2330 static const struct phy_reg phy_reg_init_0[] = {
2331 /* Channel Estimation */
2352 * enhance line driver power
2361 * Can not link to 1Gbps with bad cable
2362 * Decrease SNR threshold form 21.07dB to 19.04dB
2370 void __iomem *ioaddr = tp->mmio_addr;
2372 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2374 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2375 static const struct phy_reg phy_reg_init[] = {
2386 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2388 val = rtl_readphy(tp, 0x0d);
2389 if ((val & 0x00ff) != 0x006c) {
2390 static const u32 set[] = {
2391 0x0065, 0x0066, 0x0067, 0x0068,
2392 0x0069, 0x006a, 0x006b, 0x006c
2396 rtl_writephy(tp, 0x1f, 0x0002);
2399 for (i = 0; i < ARRAY_SIZE(set); i++)
2400 rtl_writephy(tp, 0x0d, val | set[i]);
2403 static const struct phy_reg phy_reg_init[] = {
2411 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2414 /* Fine tune PLL performance */
2415 rtl_writephy(tp, 0x1f, 0x0002);
2416 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2417 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2419 /* Switching regulator Slew rate */
2420 rtl_writephy(tp, 0x1f, 0x0002);
2421 rtl_patchphy(tp, 0x0f, 0x0017);
2423 rtl_writephy(tp, 0x1f, 0x0005);
2424 rtl_writephy(tp, 0x05, 0x001b);
2426 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2428 rtl_writephy(tp, 0x1f, 0x0000);
2431 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2433 static const struct phy_reg phy_reg_init[] = {
2489 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2492 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2494 static const struct phy_reg phy_reg_init[] = {
2504 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2505 rtl_patchphy(tp, 0x0d, 1 << 5);
2508 static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2510 static const struct phy_reg phy_reg_init[] = {
2511 /* Enable Delay cap */
2517 /* Channel estimation fine tune */
2526 /* Update PFM & 10M TX idle timer */
2538 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2540 /* DCO enable for 10M IDLE Power */
2541 rtl_writephy(tp, 0x1f, 0x0007);
2542 rtl_writephy(tp, 0x1e, 0x0023);
2543 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2544 rtl_writephy(tp, 0x1f, 0x0000);
2546 /* For impedance matching */
2547 rtl_writephy(tp, 0x1f, 0x0002);
2548 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2549 rtl_writephy(tp, 0x1F, 0x0000);
2551 /* PHY auto speed down */
2552 rtl_writephy(tp, 0x1f, 0x0007);
2553 rtl_writephy(tp, 0x1e, 0x002d);
2554 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2555 rtl_writephy(tp, 0x1f, 0x0000);
2556 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2558 rtl_writephy(tp, 0x1f, 0x0005);
2559 rtl_writephy(tp, 0x05, 0x8b86);
2560 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2561 rtl_writephy(tp, 0x1f, 0x0000);
2563 rtl_writephy(tp, 0x1f, 0x0005);
2564 rtl_writephy(tp, 0x05, 0x8b85);
2565 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2566 rtl_writephy(tp, 0x1f, 0x0007);
2567 rtl_writephy(tp, 0x1e, 0x0020);
2568 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2569 rtl_writephy(tp, 0x1f, 0x0006);
2570 rtl_writephy(tp, 0x00, 0x5a00);
2571 rtl_writephy(tp, 0x1f, 0x0000);
2572 rtl_writephy(tp, 0x0d, 0x0007);
2573 rtl_writephy(tp, 0x0e, 0x003c);
2574 rtl_writephy(tp, 0x0d, 0x4007);
2575 rtl_writephy(tp, 0x0e, 0x0000);
2576 rtl_writephy(tp, 0x0d, 0x0000);
2579 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
2581 rtl_apply_firmware(tp);
2583 rtl8168e_hw_phy_config(tp);
2586 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2588 rtl_apply_firmware(tp);
2590 rtl8168e_hw_phy_config(tp);
2593 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
2595 static const struct phy_reg phy_reg_init[] = {
2602 rtl_writephy(tp, 0x1f, 0x0000);
2603 rtl_patchphy(tp, 0x11, 1 << 12);
2604 rtl_patchphy(tp, 0x19, 1 << 13);
2605 rtl_patchphy(tp, 0x10, 1 << 15);
2607 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2610 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2612 static const struct phy_reg phy_reg_init[] = {
2626 /* Disable ALDPS before ram code */
2627 rtl_writephy(tp, 0x1f, 0x0000);
2628 rtl_writephy(tp, 0x18, 0x0310);
2631 rtl_apply_firmware(tp);
2633 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2636 static void rtl_hw_phy_config(struct net_device *dev)
2638 struct rtl8169_private *tp = netdev_priv(dev);
2640 rtl8169_print_mac_version(tp);
2642 switch (tp->mac_version) {
2643 case RTL_GIGA_MAC_VER_01:
2645 case RTL_GIGA_MAC_VER_02:
2646 case RTL_GIGA_MAC_VER_03:
2647 rtl8169s_hw_phy_config(tp);
2649 case RTL_GIGA_MAC_VER_04:
2650 rtl8169sb_hw_phy_config(tp);
2652 case RTL_GIGA_MAC_VER_05:
2653 rtl8169scd_hw_phy_config(tp);
2655 case RTL_GIGA_MAC_VER_06:
2656 rtl8169sce_hw_phy_config(tp);
2658 case RTL_GIGA_MAC_VER_07:
2659 case RTL_GIGA_MAC_VER_08:
2660 case RTL_GIGA_MAC_VER_09:
2661 rtl8102e_hw_phy_config(tp);
2663 case RTL_GIGA_MAC_VER_11:
2664 rtl8168bb_hw_phy_config(tp);
2666 case RTL_GIGA_MAC_VER_12:
2667 rtl8168bef_hw_phy_config(tp);
2669 case RTL_GIGA_MAC_VER_17:
2670 rtl8168bef_hw_phy_config(tp);
2672 case RTL_GIGA_MAC_VER_18:
2673 rtl8168cp_1_hw_phy_config(tp);
2675 case RTL_GIGA_MAC_VER_19:
2676 rtl8168c_1_hw_phy_config(tp);
2678 case RTL_GIGA_MAC_VER_20:
2679 rtl8168c_2_hw_phy_config(tp);
2681 case RTL_GIGA_MAC_VER_21:
2682 rtl8168c_3_hw_phy_config(tp);
2684 case RTL_GIGA_MAC_VER_22:
2685 rtl8168c_4_hw_phy_config(tp);
2687 case RTL_GIGA_MAC_VER_23:
2688 case RTL_GIGA_MAC_VER_24:
2689 rtl8168cp_2_hw_phy_config(tp);
2691 case RTL_GIGA_MAC_VER_25:
2692 rtl8168d_1_hw_phy_config(tp);
2694 case RTL_GIGA_MAC_VER_26:
2695 rtl8168d_2_hw_phy_config(tp);
2697 case RTL_GIGA_MAC_VER_27:
2698 rtl8168d_3_hw_phy_config(tp);
2700 case RTL_GIGA_MAC_VER_28:
2701 rtl8168d_4_hw_phy_config(tp);
2703 case RTL_GIGA_MAC_VER_29:
2704 case RTL_GIGA_MAC_VER_30:
2705 rtl8105e_hw_phy_config(tp);
2707 case RTL_GIGA_MAC_VER_32:
2708 rtl8168e_1_hw_phy_config(tp);
2710 case RTL_GIGA_MAC_VER_33:
2711 rtl8168e_2_hw_phy_config(tp);
2719 static void rtl8169_phy_timer(unsigned long __opaque)
2721 struct net_device *dev = (struct net_device *)__opaque;
2722 struct rtl8169_private *tp = netdev_priv(dev);
2723 struct timer_list *timer = &tp->timer;
2724 void __iomem *ioaddr = tp->mmio_addr;
2725 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2727 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
2729 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
2732 spin_lock_irq(&tp->lock);
2734 if (tp->phy_reset_pending(tp)) {
2736 * A busy loop could burn quite a few cycles on nowadays CPU.
2737 * Let's delay the execution of the timer for a few ticks.
2743 if (tp->link_ok(ioaddr))
2746 netif_warn(tp, link, dev, "PHY reset until link up\n");
2748 tp->phy_reset_enable(tp);
2751 mod_timer(timer, jiffies + timeout);
2753 spin_unlock_irq(&tp->lock);
2756 static inline void rtl8169_delete_timer(struct net_device *dev)
2758 struct rtl8169_private *tp = netdev_priv(dev);
2759 struct timer_list *timer = &tp->timer;
2761 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
2764 del_timer_sync(timer);
2767 static inline void rtl8169_request_timer(struct net_device *dev)
2769 struct rtl8169_private *tp = netdev_priv(dev);
2770 struct timer_list *timer = &tp->timer;
2772 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
2775 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
2778 #ifdef CONFIG_NET_POLL_CONTROLLER
2780 * Polling 'interrupt' - used by things like netconsole to send skbs
2781 * without having to re-enable interrupts. It's not called while
2782 * the interrupt routine is executing.
2784 static void rtl8169_netpoll(struct net_device *dev)
2786 struct rtl8169_private *tp = netdev_priv(dev);
2787 struct pci_dev *pdev = tp->pci_dev;
2789 disable_irq(pdev->irq);
2790 rtl8169_interrupt(pdev->irq, dev);
2791 enable_irq(pdev->irq);
2795 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2796 void __iomem *ioaddr)
2799 pci_release_regions(pdev);
2800 pci_clear_mwi(pdev);
2801 pci_disable_device(pdev);
2805 static void rtl8169_phy_reset(struct net_device *dev,
2806 struct rtl8169_private *tp)
2810 tp->phy_reset_enable(tp);
2811 for (i = 0; i < 100; i++) {
2812 if (!tp->phy_reset_pending(tp))
2816 netif_err(tp, link, dev, "PHY reset failed\n");
2819 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
2821 void __iomem *ioaddr = tp->mmio_addr;
2823 rtl_hw_phy_config(dev);
2825 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2826 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2830 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2832 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2833 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
2835 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
2836 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2838 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
2839 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
2842 rtl8169_phy_reset(dev, tp);
2844 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2845 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2846 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2847 (tp->mii.supports_gmii ?
2848 ADVERTISED_1000baseT_Half |
2849 ADVERTISED_1000baseT_Full : 0));
2851 if (RTL_R8(PHYstatus) & TBI_Enable)
2852 netif_info(tp, link, dev, "TBI auto-negotiating\n");
2855 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2857 void __iomem *ioaddr = tp->mmio_addr;
2861 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2862 high = addr[4] | (addr[5] << 8);
2864 spin_lock_irq(&tp->lock);
2866 RTL_W8(Cfg9346, Cfg9346_Unlock);
2868 RTL_W32(MAC4, high);
2874 RTL_W8(Cfg9346, Cfg9346_Lock);
2876 spin_unlock_irq(&tp->lock);
2879 static int rtl_set_mac_address(struct net_device *dev, void *p)
2881 struct rtl8169_private *tp = netdev_priv(dev);
2882 struct sockaddr *addr = p;
2884 if (!is_valid_ether_addr(addr->sa_data))
2885 return -EADDRNOTAVAIL;
2887 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2889 rtl_rar_set(tp, dev->dev_addr);
2894 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2896 struct rtl8169_private *tp = netdev_priv(dev);
2897 struct mii_ioctl_data *data = if_mii(ifr);
2899 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2902 static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2906 data->phy_id = 32; /* Internal PHY */
2910 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
2914 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
2920 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2925 static const struct rtl_cfg_info {
2926 void (*hw_start)(struct net_device *);
2927 unsigned int region;
2933 } rtl_cfg_infos [] = {
2935 .hw_start = rtl_hw_start_8169,
2938 .intr_event = SYSErr | LinkChg | RxOverflow |
2939 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
2940 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
2941 .features = RTL_FEATURE_GMII,
2942 .default_ver = RTL_GIGA_MAC_VER_01,
2945 .hw_start = rtl_hw_start_8168,
2948 .intr_event = SYSErr | LinkChg | RxOverflow |
2949 TxErr | TxOK | RxOK | RxErr,
2950 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
2951 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2952 .default_ver = RTL_GIGA_MAC_VER_11,
2955 .hw_start = rtl_hw_start_8101,
2958 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2959 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
2960 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
2961 .features = RTL_FEATURE_MSI,
2962 .default_ver = RTL_GIGA_MAC_VER_13,
2966 /* Cfg9346_Unlock assumed. */
2967 static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2968 const struct rtl_cfg_info *cfg)
2973 cfg2 = RTL_R8(Config2) & ~MSIEnable;
2974 if (cfg->features & RTL_FEATURE_MSI) {
2975 if (pci_enable_msi(pdev)) {
2976 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2979 msi = RTL_FEATURE_MSI;
2982 RTL_W8(Config2, cfg2);
2986 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2988 if (tp->features & RTL_FEATURE_MSI) {
2989 pci_disable_msi(pdev);
2990 tp->features &= ~RTL_FEATURE_MSI;
2994 static const struct net_device_ops rtl8169_netdev_ops = {
2995 .ndo_open = rtl8169_open,
2996 .ndo_stop = rtl8169_close,
2997 .ndo_get_stats = rtl8169_get_stats,
2998 .ndo_start_xmit = rtl8169_start_xmit,
2999 .ndo_tx_timeout = rtl8169_tx_timeout,
3000 .ndo_validate_addr = eth_validate_addr,
3001 .ndo_change_mtu = rtl8169_change_mtu,
3002 .ndo_fix_features = rtl8169_fix_features,
3003 .ndo_set_features = rtl8169_set_features,
3004 .ndo_set_mac_address = rtl_set_mac_address,
3005 .ndo_do_ioctl = rtl8169_ioctl,
3006 .ndo_set_multicast_list = rtl_set_rx_mode,
3007 #ifdef CONFIG_NET_POLL_CONTROLLER
3008 .ndo_poll_controller = rtl8169_netpoll,
3013 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3015 struct mdio_ops *ops = &tp->mdio_ops;
3017 switch (tp->mac_version) {
3018 case RTL_GIGA_MAC_VER_27:
3019 ops->write = r8168dp_1_mdio_write;
3020 ops->read = r8168dp_1_mdio_read;
3022 case RTL_GIGA_MAC_VER_28:
3023 case RTL_GIGA_MAC_VER_31:
3024 ops->write = r8168dp_2_mdio_write;
3025 ops->read = r8168dp_2_mdio_read;
3028 ops->write = r8169_mdio_write;
3029 ops->read = r8169_mdio_read;
3034 static void r810x_phy_power_down(struct rtl8169_private *tp)
3036 rtl_writephy(tp, 0x1f, 0x0000);
3037 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3040 static void r810x_phy_power_up(struct rtl8169_private *tp)
3042 rtl_writephy(tp, 0x1f, 0x0000);
3043 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3046 static void r810x_pll_power_down(struct rtl8169_private *tp)
3048 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3049 rtl_writephy(tp, 0x1f, 0x0000);
3050 rtl_writephy(tp, MII_BMCR, 0x0000);
3054 r810x_phy_power_down(tp);
3057 static void r810x_pll_power_up(struct rtl8169_private *tp)
3059 r810x_phy_power_up(tp);
3062 static void r8168_phy_power_up(struct rtl8169_private *tp)
3064 rtl_writephy(tp, 0x1f, 0x0000);
3065 switch (tp->mac_version) {
3066 case RTL_GIGA_MAC_VER_11:
3067 case RTL_GIGA_MAC_VER_12:
3068 case RTL_GIGA_MAC_VER_17:
3069 case RTL_GIGA_MAC_VER_18:
3070 case RTL_GIGA_MAC_VER_19:
3071 case RTL_GIGA_MAC_VER_20:
3072 case RTL_GIGA_MAC_VER_21:
3073 case RTL_GIGA_MAC_VER_22:
3074 case RTL_GIGA_MAC_VER_23:
3075 case RTL_GIGA_MAC_VER_24:
3076 case RTL_GIGA_MAC_VER_25:
3077 case RTL_GIGA_MAC_VER_26:
3078 case RTL_GIGA_MAC_VER_27:
3079 case RTL_GIGA_MAC_VER_28:
3080 case RTL_GIGA_MAC_VER_31:
3081 rtl_writephy(tp, 0x0e, 0x0000);
3086 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3089 static void r8168_phy_power_down(struct rtl8169_private *tp)
3091 rtl_writephy(tp, 0x1f, 0x0000);
3092 switch (tp->mac_version) {
3093 case RTL_GIGA_MAC_VER_32:
3094 case RTL_GIGA_MAC_VER_33:
3095 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3098 case RTL_GIGA_MAC_VER_11:
3099 case RTL_GIGA_MAC_VER_12:
3100 case RTL_GIGA_MAC_VER_17:
3101 case RTL_GIGA_MAC_VER_18:
3102 case RTL_GIGA_MAC_VER_19:
3103 case RTL_GIGA_MAC_VER_20:
3104 case RTL_GIGA_MAC_VER_21:
3105 case RTL_GIGA_MAC_VER_22:
3106 case RTL_GIGA_MAC_VER_23:
3107 case RTL_GIGA_MAC_VER_24:
3108 case RTL_GIGA_MAC_VER_25:
3109 case RTL_GIGA_MAC_VER_26:
3110 case RTL_GIGA_MAC_VER_27:
3111 case RTL_GIGA_MAC_VER_28:
3112 case RTL_GIGA_MAC_VER_31:
3113 rtl_writephy(tp, 0x0e, 0x0200);
3115 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3120 static void r8168_pll_power_down(struct rtl8169_private *tp)
3122 void __iomem *ioaddr = tp->mmio_addr;
3124 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3125 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3126 (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
3127 r8168dp_check_dash(tp)) {
3131 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
3132 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
3133 (RTL_R16(CPlusCmd) & ASF)) {
3137 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3138 tp->mac_version == RTL_GIGA_MAC_VER_33)
3139 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3141 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3142 rtl_writephy(tp, 0x1f, 0x0000);
3143 rtl_writephy(tp, MII_BMCR, 0x0000);
3145 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3146 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3150 r8168_phy_power_down(tp);
3152 switch (tp->mac_version) {
3153 case RTL_GIGA_MAC_VER_25:
3154 case RTL_GIGA_MAC_VER_26:
3155 case RTL_GIGA_MAC_VER_27:
3156 case RTL_GIGA_MAC_VER_28:
3157 case RTL_GIGA_MAC_VER_31:
3158 case RTL_GIGA_MAC_VER_32:
3159 case RTL_GIGA_MAC_VER_33:
3160 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3165 static void r8168_pll_power_up(struct rtl8169_private *tp)
3167 void __iomem *ioaddr = tp->mmio_addr;
3169 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3170 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3171 (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
3172 r8168dp_check_dash(tp)) {
3176 switch (tp->mac_version) {
3177 case RTL_GIGA_MAC_VER_25:
3178 case RTL_GIGA_MAC_VER_26:
3179 case RTL_GIGA_MAC_VER_27:
3180 case RTL_GIGA_MAC_VER_28:
3181 case RTL_GIGA_MAC_VER_31:
3182 case RTL_GIGA_MAC_VER_32:
3183 case RTL_GIGA_MAC_VER_33:
3184 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3188 r8168_phy_power_up(tp);
3191 static void rtl_pll_power_op(struct rtl8169_private *tp,
3192 void (*op)(struct rtl8169_private *))
3198 static void rtl_pll_power_down(struct rtl8169_private *tp)
3200 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3203 static void rtl_pll_power_up(struct rtl8169_private *tp)
3205 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3208 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3210 struct pll_power_ops *ops = &tp->pll_power_ops;
3212 switch (tp->mac_version) {
3213 case RTL_GIGA_MAC_VER_07:
3214 case RTL_GIGA_MAC_VER_08:
3215 case RTL_GIGA_MAC_VER_09:
3216 case RTL_GIGA_MAC_VER_10:
3217 case RTL_GIGA_MAC_VER_16:
3218 case RTL_GIGA_MAC_VER_29:
3219 case RTL_GIGA_MAC_VER_30:
3220 ops->down = r810x_pll_power_down;
3221 ops->up = r810x_pll_power_up;
3224 case RTL_GIGA_MAC_VER_11:
3225 case RTL_GIGA_MAC_VER_12:
3226 case RTL_GIGA_MAC_VER_17:
3227 case RTL_GIGA_MAC_VER_18:
3228 case RTL_GIGA_MAC_VER_19:
3229 case RTL_GIGA_MAC_VER_20:
3230 case RTL_GIGA_MAC_VER_21:
3231 case RTL_GIGA_MAC_VER_22:
3232 case RTL_GIGA_MAC_VER_23:
3233 case RTL_GIGA_MAC_VER_24:
3234 case RTL_GIGA_MAC_VER_25:
3235 case RTL_GIGA_MAC_VER_26:
3236 case RTL_GIGA_MAC_VER_27:
3237 case RTL_GIGA_MAC_VER_28:
3238 case RTL_GIGA_MAC_VER_31:
3239 case RTL_GIGA_MAC_VER_32:
3240 case RTL_GIGA_MAC_VER_33:
3241 ops->down = r8168_pll_power_down;
3242 ops->up = r8168_pll_power_up;
3252 static int __devinit
3253 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3255 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3256 const unsigned int region = cfg->region;
3257 struct rtl8169_private *tp;
3258 struct mii_if_info *mii;
3259 struct net_device *dev;
3260 void __iomem *ioaddr;
3264 if (netif_msg_drv(&debug)) {
3265 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3266 MODULENAME, RTL8169_VERSION);
3269 dev = alloc_etherdev(sizeof (*tp));
3271 if (netif_msg_drv(&debug))
3272 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
3277 SET_NETDEV_DEV(dev, &pdev->dev);
3278 dev->netdev_ops = &rtl8169_netdev_ops;
3279 tp = netdev_priv(dev);
3282 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
3286 mii->mdio_read = rtl_mdio_read;
3287 mii->mdio_write = rtl_mdio_write;
3288 mii->phy_id_mask = 0x1f;
3289 mii->reg_num_mask = 0x1f;
3290 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3292 /* disable ASPM completely as that cause random device stop working
3293 * problems as well as full system hangs for some PCIe devices users */
3294 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3295 PCIE_LINK_STATE_CLKPM);
3297 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3298 rc = pci_enable_device(pdev);
3300 netif_err(tp, probe, dev, "enable failure\n");
3301 goto err_out_free_dev_1;
3304 if (pci_set_mwi(pdev) < 0)
3305 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
3307 /* make sure PCI base addr 1 is MMIO */
3308 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
3309 netif_err(tp, probe, dev,
3310 "region #%d not an MMIO resource, aborting\n",
3316 /* check for weird/broken PCI region reporting */
3317 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
3318 netif_err(tp, probe, dev,
3319 "Invalid PCI region size(s), aborting\n");
3324 rc = pci_request_regions(pdev, MODULENAME);
3326 netif_err(tp, probe, dev, "could not request regions\n");
3330 tp->cp_cmd = RxChkSum;
3332 if ((sizeof(dma_addr_t) > 4) &&
3333 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
3334 tp->cp_cmd |= PCIDAC;
3335 dev->features |= NETIF_F_HIGHDMA;
3337 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3339 netif_err(tp, probe, dev, "DMA configuration failed\n");
3340 goto err_out_free_res_3;
3344 /* ioremap MMIO region */
3345 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
3347 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
3349 goto err_out_free_res_3;
3352 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3354 netif_info(tp, probe, dev, "no PCI Express capability\n");
3356 RTL_W16(IntrMask, 0x0000);
3358 /* Soft reset the chip. */
3359 RTL_W8(ChipCmd, CmdReset);
3361 /* Check that the chip has finished the reset. */
3362 for (i = 0; i < 100; i++) {
3363 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3365 msleep_interruptible(1);
3368 RTL_W16(IntrStatus, 0xffff);
3370 pci_set_master(pdev);
3372 /* Identify chip attached to board */
3373 rtl8169_get_mac_version(tp, ioaddr);
3376 * Pretend we are using VLANs; This bypasses a nasty bug where
3377 * Interrupts stop flowing on high load on 8110SCd controllers.
3379 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3380 tp->cp_cmd |= RxVlan;
3382 rtl_init_mdio_ops(tp);
3383 rtl_init_pll_power_ops(tp);
3385 /* Use appropriate default if unknown */
3386 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
3387 netif_notice(tp, probe, dev,
3388 "unknown MAC, using family default\n");
3389 tp->mac_version = cfg->default_ver;
3392 rtl8169_print_mac_version(tp);
3394 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
3395 if (tp->mac_version == rtl_chip_info[i].mac_version)
3398 if (i == ARRAY_SIZE(rtl_chip_info)) {
3400 "driver bug, MAC version not found in rtl_chip_info\n");
3404 tp->txd_version = rtl_chip_info[chipset].txd_version;
3406 RTL_W8(Cfg9346, Cfg9346_Unlock);
3407 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3408 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
3409 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3410 tp->features |= RTL_FEATURE_WOL;
3411 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3412 tp->features |= RTL_FEATURE_WOL;
3413 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
3414 RTL_W8(Cfg9346, Cfg9346_Lock);
3416 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3417 (RTL_R8(PHYstatus) & TBI_Enable)) {
3418 tp->set_speed = rtl8169_set_speed_tbi;
3419 tp->get_settings = rtl8169_gset_tbi;
3420 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3421 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3422 tp->link_ok = rtl8169_tbi_link_ok;
3423 tp->do_ioctl = rtl_tbi_ioctl;
3425 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
3427 tp->set_speed = rtl8169_set_speed_xmii;
3428 tp->get_settings = rtl8169_gset_xmii;
3429 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3430 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3431 tp->link_ok = rtl8169_xmii_link_ok;
3432 tp->do_ioctl = rtl_xmii_ioctl;
3435 spin_lock_init(&tp->lock);
3437 tp->mmio_addr = ioaddr;
3439 /* Get MAC address */
3440 for (i = 0; i < MAC_ADDR_LEN; i++)
3441 dev->dev_addr[i] = RTL_R8(MAC0 + i);
3442 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
3444 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
3445 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3446 dev->irq = pdev->irq;
3447 dev->base_addr = (unsigned long) ioaddr;
3449 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
3451 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3452 * properly for all devices */
3453 dev->features |= NETIF_F_RXCSUM |
3454 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3456 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3457 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3458 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3461 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3462 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3463 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
3465 tp->intr_mask = 0xffff;
3466 tp->hw_start = cfg->hw_start;
3467 tp->intr_event = cfg->intr_event;
3468 tp->napi_event = cfg->napi_event;
3470 init_timer(&tp->timer);
3471 tp->timer.data = (unsigned long) dev;
3472 tp->timer.function = rtl8169_phy_timer;
3474 tp->fw = RTL_FIRMWARE_UNKNOWN;
3476 rc = register_netdev(dev);
3480 pci_set_drvdata(pdev, dev);
3482 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3483 rtl_chip_info[chipset].name, dev->base_addr, dev->dev_addr,
3484 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
3486 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3487 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3488 (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
3489 rtl8168_driver_start(tp);
3492 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
3494 if (pci_dev_run_wake(pdev))
3495 pm_runtime_put_noidle(&pdev->dev);
3497 netif_carrier_off(dev);
3503 rtl_disable_msi(pdev, tp);
3506 pci_release_regions(pdev);
3508 pci_clear_mwi(pdev);
3509 pci_disable_device(pdev);
3515 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
3517 struct net_device *dev = pci_get_drvdata(pdev);
3518 struct rtl8169_private *tp = netdev_priv(dev);
3520 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3521 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3522 (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
3523 rtl8168_driver_stop(tp);
3526 cancel_delayed_work_sync(&tp->task);
3528 unregister_netdev(dev);
3530 rtl_release_firmware(tp);
3532 if (pci_dev_run_wake(pdev))
3533 pm_runtime_get_noresume(&pdev->dev);
3535 /* restore original MAC address */
3536 rtl_rar_set(tp, dev->perm_addr);
3538 rtl_disable_msi(pdev, tp);
3539 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3540 pci_set_drvdata(pdev, NULL);
3543 static void rtl_request_firmware(struct rtl8169_private *tp)
3547 /* Return early if the firmware is already loaded / cached. */
3548 if (!IS_ERR(tp->fw))
3551 for (i = 0; i < ARRAY_SIZE(rtl_firmware_infos); i++) {
3552 const struct rtl_firmware_info *info = rtl_firmware_infos + i;
3554 if (info->mac_version == tp->mac_version) {
3555 const char *name = info->fw_name;
3558 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
3560 netif_warn(tp, ifup, tp->dev, "unable to load "
3561 "firmware patch %s (%d)\n", name, rc);
3562 goto out_disable_request_firmware;
3568 out_disable_request_firmware:
3574 static int rtl8169_open(struct net_device *dev)
3576 struct rtl8169_private *tp = netdev_priv(dev);
3577 void __iomem *ioaddr = tp->mmio_addr;
3578 struct pci_dev *pdev = tp->pci_dev;
3579 int retval = -ENOMEM;
3581 pm_runtime_get_sync(&pdev->dev);
3584 * Rx and Tx desscriptors needs 256 bytes alignment.
3585 * dma_alloc_coherent provides more.
3587 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3588 &tp->TxPhyAddr, GFP_KERNEL);
3589 if (!tp->TxDescArray)
3590 goto err_pm_runtime_put;
3592 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3593 &tp->RxPhyAddr, GFP_KERNEL);
3594 if (!tp->RxDescArray)
3597 retval = rtl8169_init_ring(dev);
3601 INIT_DELAYED_WORK(&tp->task, NULL);
3605 rtl_request_firmware(tp);
3607 retval = request_irq(dev->irq, rtl8169_interrupt,
3608 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
3611 goto err_release_fw_2;
3613 napi_enable(&tp->napi);
3615 rtl8169_init_phy(dev, tp);
3617 rtl8169_set_features(dev, dev->features);
3619 rtl_pll_power_up(tp);
3623 rtl8169_request_timer(dev);
3625 tp->saved_wolopts = 0;
3626 pm_runtime_put_noidle(&pdev->dev);
3628 rtl8169_check_link_status(dev, tp, ioaddr);
3633 rtl_release_firmware(tp);
3634 rtl8169_rx_clear(tp);
3636 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3638 tp->RxDescArray = NULL;
3640 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3642 tp->TxDescArray = NULL;
3644 pm_runtime_put_noidle(&pdev->dev);
3648 static void rtl8169_hw_reset(struct rtl8169_private *tp)
3650 void __iomem *ioaddr = tp->mmio_addr;
3652 /* Disable interrupts */
3653 rtl8169_irq_mask_and_ack(ioaddr);
3655 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3656 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3657 tp->mac_version == RTL_GIGA_MAC_VER_31) {
3658 while (RTL_R8(TxPoll) & NPQ)
3663 /* Reset the chipset */
3664 RTL_W8(ChipCmd, CmdReset);
3670 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
3672 void __iomem *ioaddr = tp->mmio_addr;
3673 u32 cfg = rtl8169_rx_config;
3675 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
3676 RTL_W32(RxConfig, cfg);
3678 /* Set DMA burst size and Interframe Gap Time */
3679 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3680 (InterFrameGap << TxInterFrameGapShift));
3683 static void rtl_hw_start(struct net_device *dev)
3685 struct rtl8169_private *tp = netdev_priv(dev);
3686 void __iomem *ioaddr = tp->mmio_addr;
3689 /* Soft reset the chip. */
3690 RTL_W8(ChipCmd, CmdReset);
3692 /* Check that the chip has finished the reset. */
3693 for (i = 0; i < 100; i++) {
3694 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3696 msleep_interruptible(1);
3701 netif_start_queue(dev);
3705 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3706 void __iomem *ioaddr)
3709 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3710 * register to be written before TxDescAddrLow to work.
3711 * Switching from MMIO to I/O access fixes the issue as well.
3713 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
3714 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
3715 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
3716 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
3719 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3723 cmd = RTL_R16(CPlusCmd);
3724 RTL_W16(CPlusCmd, cmd);
3728 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
3730 /* Low hurts. Let's disable the filtering. */
3731 RTL_W16(RxMaxSize, rx_buf_sz + 1);
3734 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3736 static const struct {
3741 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3742 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3743 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3744 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3749 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
3750 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
3751 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3752 RTL_W32(0x7c, p->val);
3758 static void rtl_hw_start_8169(struct net_device *dev)
3760 struct rtl8169_private *tp = netdev_priv(dev);
3761 void __iomem *ioaddr = tp->mmio_addr;
3762 struct pci_dev *pdev = tp->pci_dev;
3764 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3765 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3766 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3769 RTL_W8(Cfg9346, Cfg9346_Unlock);
3770 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3771 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3772 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3773 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3774 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3776 RTL_W8(EarlyTxThres, NoEarlyTx);
3778 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
3780 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3781 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3782 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3783 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3784 rtl_set_rx_tx_config_registers(tp);
3786 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
3788 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3789 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
3790 dprintk("Set MAC Reg C+CR Offset 0xE0. "
3791 "Bit-3 and bit-14 MUST be 1\n");
3792 tp->cp_cmd |= (1 << 14);
3795 RTL_W16(CPlusCmd, tp->cp_cmd);
3797 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3800 * Undocumented corner. Supposedly:
3801 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3803 RTL_W16(IntrMitigate, 0x0000);
3805 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3807 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3808 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3809 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3810 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3811 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3812 rtl_set_rx_tx_config_registers(tp);
3815 RTL_W8(Cfg9346, Cfg9346_Lock);
3817 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3820 RTL_W32(RxMissed, 0);
3822 rtl_set_rx_mode(dev);
3824 /* no early-rx interrupts */
3825 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
3827 /* Enable all known interrupts by setting the interrupt mask. */
3828 RTL_W16(IntrMask, tp->intr_event);
3831 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
3833 struct net_device *dev = pci_get_drvdata(pdev);
3834 struct rtl8169_private *tp = netdev_priv(dev);
3835 int cap = tp->pcie_cap;
3840 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3841 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3842 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3846 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
3850 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
3851 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3854 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3856 rtl_csi_access_enable(ioaddr, 0x17000000);
3859 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3861 rtl_csi_access_enable(ioaddr, 0x27000000);
3865 unsigned int offset;
3870 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
3875 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3876 rtl_ephy_write(ioaddr, e->offset, w);
3881 static void rtl_disable_clock_request(struct pci_dev *pdev)
3883 struct net_device *dev = pci_get_drvdata(pdev);
3884 struct rtl8169_private *tp = netdev_priv(dev);
3885 int cap = tp->pcie_cap;
3890 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3891 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3892 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3896 static void rtl_enable_clock_request(struct pci_dev *pdev)
3898 struct net_device *dev = pci_get_drvdata(pdev);
3899 struct rtl8169_private *tp = netdev_priv(dev);
3900 int cap = tp->pcie_cap;
3905 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3906 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3907 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3911 #define R8168_CPCMD_QUIRK_MASK (\
3922 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3924 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3926 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3928 rtl_tx_performance_tweak(pdev,
3929 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3932 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3934 rtl_hw_start_8168bb(ioaddr, pdev);
3936 RTL_W8(MaxTxPacketSize, TxPacketMax);
3938 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3941 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3943 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3945 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3947 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3949 rtl_disable_clock_request(pdev);
3951 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3954 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
3956 static const struct ephy_info e_info_8168cp[] = {
3957 { 0x01, 0, 0x0001 },
3958 { 0x02, 0x0800, 0x1000 },
3959 { 0x03, 0, 0x0042 },
3960 { 0x06, 0x0080, 0x0000 },
3964 rtl_csi_access_enable_2(ioaddr);
3966 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3968 __rtl_hw_start_8168cp(ioaddr, pdev);
3971 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3973 rtl_csi_access_enable_2(ioaddr);
3975 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3977 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3979 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3982 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3984 rtl_csi_access_enable_2(ioaddr);
3986 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3989 RTL_W8(DBG_REG, 0x20);
3991 RTL_W8(MaxTxPacketSize, TxPacketMax);
3993 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3995 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3998 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4000 static const struct ephy_info e_info_8168c_1[] = {
4001 { 0x02, 0x0800, 0x1000 },
4002 { 0x03, 0, 0x0002 },
4003 { 0x06, 0x0080, 0x0000 }
4006 rtl_csi_access_enable_2(ioaddr);
4008 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4010 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4012 __rtl_hw_start_8168cp(ioaddr, pdev);
4015 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4017 static const struct ephy_info e_info_8168c_2[] = {
4018 { 0x01, 0, 0x0001 },
4019 { 0x03, 0x0400, 0x0220 }
4022 rtl_csi_access_enable_2(ioaddr);
4024 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4026 __rtl_hw_start_8168cp(ioaddr, pdev);
4029 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4031 rtl_hw_start_8168c_2(ioaddr, pdev);
4034 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4036 rtl_csi_access_enable_2(ioaddr);
4038 __rtl_hw_start_8168cp(ioaddr, pdev);
4041 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4043 rtl_csi_access_enable_2(ioaddr);
4045 rtl_disable_clock_request(pdev);
4047 RTL_W8(MaxTxPacketSize, TxPacketMax);
4049 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4051 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4054 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4056 rtl_csi_access_enable_1(ioaddr);
4058 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4060 RTL_W8(MaxTxPacketSize, TxPacketMax);
4062 rtl_disable_clock_request(pdev);
4065 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4067 static const struct ephy_info e_info_8168d_4[] = {
4069 { 0x19, 0x20, 0x50 },
4074 rtl_csi_access_enable_1(ioaddr);
4076 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4078 RTL_W8(MaxTxPacketSize, TxPacketMax);
4080 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4081 const struct ephy_info *e = e_info_8168d_4 + i;
4084 w = rtl_ephy_read(ioaddr, e->offset);
4085 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4088 rtl_enable_clock_request(pdev);
4091 static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4093 static const struct ephy_info e_info_8168e[] = {
4094 { 0x00, 0x0200, 0x0100 },
4095 { 0x00, 0x0000, 0x0004 },
4096 { 0x06, 0x0002, 0x0001 },
4097 { 0x06, 0x0000, 0x0030 },
4098 { 0x07, 0x0000, 0x2000 },
4099 { 0x00, 0x0000, 0x0020 },
4100 { 0x03, 0x5800, 0x2000 },
4101 { 0x03, 0x0000, 0x0001 },
4102 { 0x01, 0x0800, 0x1000 },
4103 { 0x07, 0x0000, 0x4000 },
4104 { 0x1e, 0x0000, 0x2000 },
4105 { 0x19, 0xffff, 0xfe6c },
4106 { 0x0a, 0x0000, 0x0040 }
4109 rtl_csi_access_enable_2(ioaddr);
4111 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4113 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4115 RTL_W8(MaxTxPacketSize, TxPacketMax);
4117 rtl_disable_clock_request(pdev);
4119 /* Reset tx FIFO pointer */
4120 RTL_W32(MISC, RTL_R32(MISC) | txpla_rst);
4121 RTL_W32(MISC, RTL_R32(MISC) & ~txpla_rst);
4123 RTL_W8(Config5, RTL_R8(Config5) & ~spi_en);
4126 static void rtl_hw_start_8168(struct net_device *dev)
4128 struct rtl8169_private *tp = netdev_priv(dev);
4129 void __iomem *ioaddr = tp->mmio_addr;
4130 struct pci_dev *pdev = tp->pci_dev;
4132 RTL_W8(Cfg9346, Cfg9346_Unlock);
4134 RTL_W8(MaxTxPacketSize, TxPacketMax);
4136 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4138 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
4140 RTL_W16(CPlusCmd, tp->cp_cmd);
4142 RTL_W16(IntrMitigate, 0x5151);
4144 /* Work around for RxFIFO overflow. */
4145 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4146 tp->mac_version == RTL_GIGA_MAC_VER_22) {
4147 tp->intr_event |= RxFIFOOver | PCSTimeout;
4148 tp->intr_event &= ~RxOverflow;
4151 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4153 rtl_set_rx_mode(dev);
4155 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4156 (InterFrameGap << TxInterFrameGapShift));
4160 switch (tp->mac_version) {
4161 case RTL_GIGA_MAC_VER_11:
4162 rtl_hw_start_8168bb(ioaddr, pdev);
4165 case RTL_GIGA_MAC_VER_12:
4166 case RTL_GIGA_MAC_VER_17:
4167 rtl_hw_start_8168bef(ioaddr, pdev);
4170 case RTL_GIGA_MAC_VER_18:
4171 rtl_hw_start_8168cp_1(ioaddr, pdev);
4174 case RTL_GIGA_MAC_VER_19:
4175 rtl_hw_start_8168c_1(ioaddr, pdev);
4178 case RTL_GIGA_MAC_VER_20:
4179 rtl_hw_start_8168c_2(ioaddr, pdev);
4182 case RTL_GIGA_MAC_VER_21:
4183 rtl_hw_start_8168c_3(ioaddr, pdev);
4186 case RTL_GIGA_MAC_VER_22:
4187 rtl_hw_start_8168c_4(ioaddr, pdev);
4190 case RTL_GIGA_MAC_VER_23:
4191 rtl_hw_start_8168cp_2(ioaddr, pdev);
4194 case RTL_GIGA_MAC_VER_24:
4195 rtl_hw_start_8168cp_3(ioaddr, pdev);
4198 case RTL_GIGA_MAC_VER_25:
4199 case RTL_GIGA_MAC_VER_26:
4200 case RTL_GIGA_MAC_VER_27:
4201 rtl_hw_start_8168d(ioaddr, pdev);
4204 case RTL_GIGA_MAC_VER_28:
4205 rtl_hw_start_8168d_4(ioaddr, pdev);
4207 case RTL_GIGA_MAC_VER_31:
4208 rtl_hw_start_8168dp(ioaddr, pdev);
4211 case RTL_GIGA_MAC_VER_32:
4212 case RTL_GIGA_MAC_VER_33:
4213 rtl_hw_start_8168e(ioaddr, pdev);
4217 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4218 dev->name, tp->mac_version);
4222 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4224 RTL_W8(Cfg9346, Cfg9346_Lock);
4226 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4228 RTL_W16(IntrMask, tp->intr_event);
4231 #define R810X_CPCMD_QUIRK_MASK (\
4242 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4244 static const struct ephy_info e_info_8102e_1[] = {
4245 { 0x01, 0, 0x6e65 },
4246 { 0x02, 0, 0x091f },
4247 { 0x03, 0, 0xc2f9 },
4248 { 0x06, 0, 0xafb5 },
4249 { 0x07, 0, 0x0e00 },
4250 { 0x19, 0, 0xec80 },
4251 { 0x01, 0, 0x2e65 },
4256 rtl_csi_access_enable_2(ioaddr);
4258 RTL_W8(DBG_REG, FIX_NAK_1);
4260 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4263 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4264 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4266 cfg1 = RTL_R8(Config1);
4267 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4268 RTL_W8(Config1, cfg1 & ~LEDS0);
4270 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4273 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4275 rtl_csi_access_enable_2(ioaddr);
4277 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4279 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4280 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4283 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4285 rtl_hw_start_8102e_2(ioaddr, pdev);
4287 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4290 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4292 static const struct ephy_info e_info_8105e_1[] = {
4293 { 0x07, 0, 0x4000 },
4294 { 0x19, 0, 0x0200 },
4295 { 0x19, 0, 0x0020 },
4296 { 0x1e, 0, 0x2000 },
4297 { 0x03, 0, 0x0001 },
4298 { 0x19, 0, 0x0100 },
4299 { 0x19, 0, 0x0004 },
4303 /* Force LAN exit from ASPM if Rx/Tx are not idel */
4304 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4306 /* disable Early Tally Counter */
4307 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4309 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4310 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4312 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4315 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4317 rtl_hw_start_8105e_1(ioaddr, pdev);
4318 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4321 static void rtl_hw_start_8101(struct net_device *dev)
4323 struct rtl8169_private *tp = netdev_priv(dev);
4324 void __iomem *ioaddr = tp->mmio_addr;
4325 struct pci_dev *pdev = tp->pci_dev;
4327 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
4328 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
4329 int cap = tp->pcie_cap;
4332 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4333 PCI_EXP_DEVCTL_NOSNOOP_EN);
4337 RTL_W8(Cfg9346, Cfg9346_Unlock);
4339 switch (tp->mac_version) {
4340 case RTL_GIGA_MAC_VER_07:
4341 rtl_hw_start_8102e_1(ioaddr, pdev);
4344 case RTL_GIGA_MAC_VER_08:
4345 rtl_hw_start_8102e_3(ioaddr, pdev);
4348 case RTL_GIGA_MAC_VER_09:
4349 rtl_hw_start_8102e_2(ioaddr, pdev);
4352 case RTL_GIGA_MAC_VER_29:
4353 rtl_hw_start_8105e_1(ioaddr, pdev);
4355 case RTL_GIGA_MAC_VER_30:
4356 rtl_hw_start_8105e_2(ioaddr, pdev);
4360 RTL_W8(Cfg9346, Cfg9346_Lock);
4362 RTL_W8(MaxTxPacketSize, TxPacketMax);
4364 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4366 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
4367 RTL_W16(CPlusCmd, tp->cp_cmd);
4369 RTL_W16(IntrMitigate, 0x0000);
4371 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4373 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4374 rtl_set_rx_tx_config_registers(tp);
4378 rtl_set_rx_mode(dev);
4380 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
4382 RTL_W16(IntrMask, tp->intr_event);
4385 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4387 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4391 netdev_update_features(dev);
4396 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4398 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
4399 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4402 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4403 void **data_buff, struct RxDesc *desc)
4405 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
4410 rtl8169_make_unusable_by_asic(desc);
4413 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4415 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4417 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4420 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4423 desc->addr = cpu_to_le64(mapping);
4425 rtl8169_mark_to_asic(desc, rx_buf_sz);
4428 static inline void *rtl8169_align(void *data)
4430 return (void *)ALIGN((long)data, 16);
4433 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4434 struct RxDesc *desc)
4438 struct device *d = &tp->pci_dev->dev;
4439 struct net_device *dev = tp->dev;
4440 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
4442 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4446 if (rtl8169_align(data) != data) {
4448 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4453 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
4455 if (unlikely(dma_mapping_error(d, mapping))) {
4456 if (net_ratelimit())
4457 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
4461 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
4469 static void rtl8169_rx_clear(struct rtl8169_private *tp)
4473 for (i = 0; i < NUM_RX_DESC; i++) {
4474 if (tp->Rx_databuff[i]) {
4475 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
4476 tp->RxDescArray + i);
4481 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
4483 desc->opts1 |= cpu_to_le32(RingEnd);
4486 static int rtl8169_rx_fill(struct rtl8169_private *tp)
4490 for (i = 0; i < NUM_RX_DESC; i++) {
4493 if (tp->Rx_databuff[i])
4496 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
4498 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
4501 tp->Rx_databuff[i] = data;
4504 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4508 rtl8169_rx_clear(tp);
4512 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4514 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4517 static int rtl8169_init_ring(struct net_device *dev)
4519 struct rtl8169_private *tp = netdev_priv(dev);
4521 rtl8169_init_ring_indexes(tp);
4523 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
4524 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
4526 return rtl8169_rx_fill(tp);
4529 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
4530 struct TxDesc *desc)
4532 unsigned int len = tx_skb->len;
4534 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4542 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4547 for (i = 0; i < n; i++) {
4548 unsigned int entry = (start + i) % NUM_TX_DESC;
4549 struct ring_info *tx_skb = tp->tx_skb + entry;
4550 unsigned int len = tx_skb->len;
4553 struct sk_buff *skb = tx_skb->skb;
4555 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4556 tp->TxDescArray + entry);
4558 tp->dev->stats.tx_dropped++;
4566 static void rtl8169_tx_clear(struct rtl8169_private *tp)
4568 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
4569 tp->cur_tx = tp->dirty_tx = 0;
4572 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
4574 struct rtl8169_private *tp = netdev_priv(dev);
4576 PREPARE_DELAYED_WORK(&tp->task, task);
4577 schedule_delayed_work(&tp->task, 4);
4580 static void rtl8169_wait_for_quiescence(struct net_device *dev)
4582 struct rtl8169_private *tp = netdev_priv(dev);
4583 void __iomem *ioaddr = tp->mmio_addr;
4585 synchronize_irq(dev->irq);
4587 /* Wait for any pending NAPI task to complete */
4588 napi_disable(&tp->napi);
4590 rtl8169_irq_mask_and_ack(ioaddr);
4592 tp->intr_mask = 0xffff;
4593 RTL_W16(IntrMask, tp->intr_event);
4594 napi_enable(&tp->napi);
4597 static void rtl8169_reinit_task(struct work_struct *work)
4599 struct rtl8169_private *tp =
4600 container_of(work, struct rtl8169_private, task.work);
4601 struct net_device *dev = tp->dev;
4606 if (!netif_running(dev))
4609 rtl8169_wait_for_quiescence(dev);
4612 ret = rtl8169_open(dev);
4613 if (unlikely(ret < 0)) {
4614 if (net_ratelimit())
4615 netif_err(tp, drv, dev,
4616 "reinit failure (status = %d). Rescheduling\n",
4618 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4625 static void rtl8169_reset_task(struct work_struct *work)
4627 struct rtl8169_private *tp =
4628 container_of(work, struct rtl8169_private, task.work);
4629 struct net_device *dev = tp->dev;
4633 if (!netif_running(dev))
4636 rtl8169_wait_for_quiescence(dev);
4638 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
4639 rtl8169_tx_clear(tp);
4641 if (tp->dirty_rx == tp->cur_rx) {
4642 rtl8169_init_ring_indexes(tp);
4644 netif_wake_queue(dev);
4645 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
4647 if (net_ratelimit())
4648 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
4649 rtl8169_schedule_work(dev, rtl8169_reset_task);
4656 static void rtl8169_tx_timeout(struct net_device *dev)
4658 struct rtl8169_private *tp = netdev_priv(dev);
4660 rtl8169_hw_reset(tp);
4662 /* Let's wait a bit while any (async) irq lands on */
4663 rtl8169_schedule_work(dev, rtl8169_reset_task);
4666 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4669 struct skb_shared_info *info = skb_shinfo(skb);
4670 unsigned int cur_frag, entry;
4671 struct TxDesc * uninitialized_var(txd);
4672 struct device *d = &tp->pci_dev->dev;
4675 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4676 skb_frag_t *frag = info->frags + cur_frag;
4681 entry = (entry + 1) % NUM_TX_DESC;
4683 txd = tp->TxDescArray + entry;
4685 addr = ((void *) page_address(frag->page)) + frag->page_offset;
4686 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
4687 if (unlikely(dma_mapping_error(d, mapping))) {
4688 if (net_ratelimit())
4689 netif_err(tp, drv, tp->dev,
4690 "Failed to map TX fragments DMA!\n");
4694 /* anti gcc 2.95.3 bugware (sic) */
4695 status = opts[0] | len |
4696 (RingEnd * !((entry + 1) % NUM_TX_DESC));
4698 txd->opts1 = cpu_to_le32(status);
4699 txd->opts2 = cpu_to_le32(opts[1]);
4700 txd->addr = cpu_to_le64(mapping);
4702 tp->tx_skb[entry].len = len;
4706 tp->tx_skb[entry].skb = skb;
4707 txd->opts1 |= cpu_to_le32(LastFrag);
4713 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4717 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4718 struct sk_buff *skb, u32 *opts)
4720 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
4721 u32 mss = skb_shinfo(skb)->gso_size;
4722 int offset = info->opts_offset;
4726 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4727 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4728 const struct iphdr *ip = ip_hdr(skb);
4730 if (ip->protocol == IPPROTO_TCP)
4731 opts[offset] |= info->checksum.tcp;
4732 else if (ip->protocol == IPPROTO_UDP)
4733 opts[offset] |= info->checksum.udp;
4739 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4740 struct net_device *dev)
4742 struct rtl8169_private *tp = netdev_priv(dev);
4743 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
4744 struct TxDesc *txd = tp->TxDescArray + entry;
4745 void __iomem *ioaddr = tp->mmio_addr;
4746 struct device *d = &tp->pci_dev->dev;
4752 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
4753 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
4757 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
4760 len = skb_headlen(skb);
4761 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
4762 if (unlikely(dma_mapping_error(d, mapping))) {
4763 if (net_ratelimit())
4764 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
4768 tp->tx_skb[entry].len = len;
4769 txd->addr = cpu_to_le64(mapping);
4771 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4774 rtl8169_tso_csum(tp, skb, opts);
4776 frags = rtl8169_xmit_frags(tp, skb, opts);
4780 opts[0] |= FirstFrag;
4782 opts[0] |= FirstFrag | LastFrag;
4783 tp->tx_skb[entry].skb = skb;
4786 txd->opts2 = cpu_to_le32(opts[1]);
4790 /* anti gcc 2.95.3 bugware (sic) */
4791 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4792 txd->opts1 = cpu_to_le32(status);
4794 tp->cur_tx += frags + 1;
4798 RTL_W8(TxPoll, NPQ); /* set polling bit */
4800 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4801 netif_stop_queue(dev);
4803 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4804 netif_wake_queue(dev);
4807 return NETDEV_TX_OK;
4810 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
4813 dev->stats.tx_dropped++;
4814 return NETDEV_TX_OK;
4817 netif_stop_queue(dev);
4818 dev->stats.tx_dropped++;
4819 return NETDEV_TX_BUSY;
4822 static void rtl8169_pcierr_interrupt(struct net_device *dev)
4824 struct rtl8169_private *tp = netdev_priv(dev);
4825 struct pci_dev *pdev = tp->pci_dev;
4826 u16 pci_status, pci_cmd;
4828 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4829 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4831 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4832 pci_cmd, pci_status);
4835 * The recovery sequence below admits a very elaborated explanation:
4836 * - it seems to work;
4837 * - I did not see what else could be done;
4838 * - it makes iop3xx happy.
4840 * Feel free to adjust to your needs.
4842 if (pdev->broken_parity_status)
4843 pci_cmd &= ~PCI_COMMAND_PARITY;
4845 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4847 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
4849 pci_write_config_word(pdev, PCI_STATUS,
4850 pci_status & (PCI_STATUS_DETECTED_PARITY |
4851 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4852 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4854 /* The infamous DAC f*ckup only happens at boot time */
4855 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
4856 void __iomem *ioaddr = tp->mmio_addr;
4858 netif_info(tp, intr, dev, "disabling PCI DAC\n");
4859 tp->cp_cmd &= ~PCIDAC;
4860 RTL_W16(CPlusCmd, tp->cp_cmd);
4861 dev->features &= ~NETIF_F_HIGHDMA;
4864 rtl8169_hw_reset(tp);
4866 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4869 static void rtl8169_tx_interrupt(struct net_device *dev,
4870 struct rtl8169_private *tp,
4871 void __iomem *ioaddr)
4873 unsigned int dirty_tx, tx_left;
4875 dirty_tx = tp->dirty_tx;
4877 tx_left = tp->cur_tx - dirty_tx;
4879 while (tx_left > 0) {
4880 unsigned int entry = dirty_tx % NUM_TX_DESC;
4881 struct ring_info *tx_skb = tp->tx_skb + entry;
4885 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4886 if (status & DescOwn)
4889 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4890 tp->TxDescArray + entry);
4891 if (status & LastFrag) {
4892 dev->stats.tx_packets++;
4893 dev->stats.tx_bytes += tx_skb->skb->len;
4894 dev_kfree_skb(tx_skb->skb);
4901 if (tp->dirty_tx != dirty_tx) {
4902 tp->dirty_tx = dirty_tx;
4904 if (netif_queue_stopped(dev) &&
4905 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4906 netif_wake_queue(dev);
4909 * 8168 hack: TxPoll requests are lost when the Tx packets are
4910 * too close. Let's kick an extra TxPoll request when a burst
4911 * of start_xmit activity is detected (if it is not detected,
4912 * it is slow enough). -- FR
4915 if (tp->cur_tx != dirty_tx)
4916 RTL_W8(TxPoll, NPQ);
4920 static inline int rtl8169_fragmented_frame(u32 status)
4922 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4925 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
4927 u32 status = opts1 & RxProtoMask;
4929 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
4930 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
4931 skb->ip_summed = CHECKSUM_UNNECESSARY;
4933 skb_checksum_none_assert(skb);
4936 static struct sk_buff *rtl8169_try_rx_copy(void *data,
4937 struct rtl8169_private *tp,
4941 struct sk_buff *skb;
4942 struct device *d = &tp->pci_dev->dev;
4944 data = rtl8169_align(data);
4945 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
4947 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4949 memcpy(skb->data, data, pkt_size);
4950 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4956 * Warning : rtl8169_rx_interrupt() might be called :
4957 * 1) from NAPI (softirq) context
4958 * (polling = 1 : we should call netif_receive_skb())
4959 * 2) from process context (rtl8169_reset_task())
4960 * (polling = 0 : we must call netif_rx() instead)
4962 static int rtl8169_rx_interrupt(struct net_device *dev,
4963 struct rtl8169_private *tp,
4964 void __iomem *ioaddr, u32 budget)
4966 unsigned int cur_rx, rx_left;
4968 int polling = (budget != ~(u32)0) ? 1 : 0;
4970 cur_rx = tp->cur_rx;
4971 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
4972 rx_left = min(rx_left, budget);
4974 for (; rx_left > 0; rx_left--, cur_rx++) {
4975 unsigned int entry = cur_rx % NUM_RX_DESC;
4976 struct RxDesc *desc = tp->RxDescArray + entry;
4980 status = le32_to_cpu(desc->opts1);
4982 if (status & DescOwn)
4984 if (unlikely(status & RxRES)) {
4985 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4987 dev->stats.rx_errors++;
4988 if (status & (RxRWT | RxRUNT))
4989 dev->stats.rx_length_errors++;
4991 dev->stats.rx_crc_errors++;
4992 if (status & RxFOVF) {
4993 rtl8169_schedule_work(dev, rtl8169_reset_task);
4994 dev->stats.rx_fifo_errors++;
4996 rtl8169_mark_to_asic(desc, rx_buf_sz);
4998 struct sk_buff *skb;
4999 dma_addr_t addr = le64_to_cpu(desc->addr);
5000 int pkt_size = (status & 0x00001FFF) - 4;
5003 * The driver does not support incoming fragmented
5004 * frames. They are seen as a symptom of over-mtu
5007 if (unlikely(rtl8169_fragmented_frame(status))) {
5008 dev->stats.rx_dropped++;
5009 dev->stats.rx_length_errors++;
5010 rtl8169_mark_to_asic(desc, rx_buf_sz);
5014 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5015 tp, pkt_size, addr);
5016 rtl8169_mark_to_asic(desc, rx_buf_sz);
5018 dev->stats.rx_dropped++;
5022 rtl8169_rx_csum(skb, status);
5023 skb_put(skb, pkt_size);
5024 skb->protocol = eth_type_trans(skb, dev);
5026 rtl8169_rx_vlan_tag(desc, skb);
5028 if (likely(polling))
5029 napi_gro_receive(&tp->napi, skb);
5033 dev->stats.rx_bytes += pkt_size;
5034 dev->stats.rx_packets++;
5037 /* Work around for AMD plateform. */
5038 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
5039 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5045 count = cur_rx - tp->cur_rx;
5046 tp->cur_rx = cur_rx;
5048 tp->dirty_rx += count;
5053 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5055 struct net_device *dev = dev_instance;
5056 struct rtl8169_private *tp = netdev_priv(dev);
5057 void __iomem *ioaddr = tp->mmio_addr;
5061 /* loop handling interrupts until we have no new ones or
5062 * we hit a invalid/hotplug case.
5064 status = RTL_R16(IntrStatus);
5065 while (status && status != 0xffff) {
5068 /* Handle all of the error cases first. These will reset
5069 * the chip, so just exit the loop.
5071 if (unlikely(!netif_running(dev))) {
5072 rtl8169_asic_down(ioaddr);
5076 if (unlikely(status & RxFIFOOver)) {
5077 switch (tp->mac_version) {
5078 /* Work around for rx fifo overflow */
5079 case RTL_GIGA_MAC_VER_11:
5080 case RTL_GIGA_MAC_VER_22:
5081 case RTL_GIGA_MAC_VER_26:
5082 netif_stop_queue(dev);
5083 rtl8169_tx_timeout(dev);
5085 /* Testers needed. */
5086 case RTL_GIGA_MAC_VER_17:
5087 case RTL_GIGA_MAC_VER_19:
5088 case RTL_GIGA_MAC_VER_20:
5089 case RTL_GIGA_MAC_VER_21:
5090 case RTL_GIGA_MAC_VER_23:
5091 case RTL_GIGA_MAC_VER_24:
5092 case RTL_GIGA_MAC_VER_27:
5093 case RTL_GIGA_MAC_VER_28:
5094 case RTL_GIGA_MAC_VER_31:
5095 /* Experimental science. Pktgen proof. */
5096 case RTL_GIGA_MAC_VER_12:
5097 case RTL_GIGA_MAC_VER_25:
5098 if (status == RxFIFOOver)
5106 if (unlikely(status & SYSErr)) {
5107 rtl8169_pcierr_interrupt(dev);
5111 if (status & LinkChg)
5112 __rtl8169_check_link_status(dev, tp, ioaddr, true);
5114 /* We need to see the lastest version of tp->intr_mask to
5115 * avoid ignoring an MSI interrupt and having to wait for
5116 * another event which may never come.
5119 if (status & tp->intr_mask & tp->napi_event) {
5120 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5121 tp->intr_mask = ~tp->napi_event;
5123 if (likely(napi_schedule_prep(&tp->napi)))
5124 __napi_schedule(&tp->napi);
5126 netif_info(tp, intr, dev,
5127 "interrupt %04x in poll\n", status);
5130 /* We only get a new MSI interrupt when all active irq
5131 * sources on the chip have been acknowledged. So, ack
5132 * everything we've seen and check if new sources have become
5133 * active to avoid blocking all interrupts from the chip.
5136 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5137 status = RTL_R16(IntrStatus);
5140 return IRQ_RETVAL(handled);
5143 static int rtl8169_poll(struct napi_struct *napi, int budget)
5145 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5146 struct net_device *dev = tp->dev;
5147 void __iomem *ioaddr = tp->mmio_addr;
5150 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
5151 rtl8169_tx_interrupt(dev, tp, ioaddr);
5153 if (work_done < budget) {
5154 napi_complete(napi);
5156 /* We need for force the visibility of tp->intr_mask
5157 * for other CPUs, as we can loose an MSI interrupt
5158 * and potentially wait for a retransmit timeout if we don't.
5159 * The posted write to IntrMask is safe, as it will
5160 * eventually make it to the chip and we won't loose anything
5163 tp->intr_mask = 0xffff;
5165 RTL_W16(IntrMask, tp->intr_event);
5171 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5173 struct rtl8169_private *tp = netdev_priv(dev);
5175 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5178 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5179 RTL_W32(RxMissed, 0);
5182 static void rtl8169_down(struct net_device *dev)
5184 struct rtl8169_private *tp = netdev_priv(dev);
5185 void __iomem *ioaddr = tp->mmio_addr;
5187 rtl8169_delete_timer(dev);
5189 netif_stop_queue(dev);
5191 napi_disable(&tp->napi);
5193 spin_lock_irq(&tp->lock);
5195 rtl8169_asic_down(ioaddr);
5197 * At this point device interrupts can not be enabled in any function,
5198 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5199 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5201 rtl8169_rx_missed(dev, ioaddr);
5203 spin_unlock_irq(&tp->lock);
5205 synchronize_irq(dev->irq);
5207 /* Give a racing hard_start_xmit a few cycles to complete. */
5208 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
5210 rtl8169_tx_clear(tp);
5212 rtl8169_rx_clear(tp);
5214 rtl_pll_power_down(tp);
5217 static int rtl8169_close(struct net_device *dev)
5219 struct rtl8169_private *tp = netdev_priv(dev);
5220 struct pci_dev *pdev = tp->pci_dev;
5222 pm_runtime_get_sync(&pdev->dev);
5224 /* update counters before going down */
5225 rtl8169_update_counters(dev);
5229 free_irq(dev->irq, dev);
5231 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5233 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5235 tp->TxDescArray = NULL;
5236 tp->RxDescArray = NULL;
5238 pm_runtime_put_sync(&pdev->dev);
5243 static void rtl_set_rx_mode(struct net_device *dev)
5245 struct rtl8169_private *tp = netdev_priv(dev);
5246 void __iomem *ioaddr = tp->mmio_addr;
5247 unsigned long flags;
5248 u32 mc_filter[2]; /* Multicast hash filter */
5252 if (dev->flags & IFF_PROMISC) {
5253 /* Unconditionally log net taps. */
5254 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5256 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5258 mc_filter[1] = mc_filter[0] = 0xffffffff;
5259 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5260 (dev->flags & IFF_ALLMULTI)) {
5261 /* Too many to filter perfectly -- accept all multicasts. */
5262 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5263 mc_filter[1] = mc_filter[0] = 0xffffffff;
5265 struct netdev_hw_addr *ha;
5267 rx_mode = AcceptBroadcast | AcceptMyPhys;
5268 mc_filter[1] = mc_filter[0] = 0;
5269 netdev_for_each_mc_addr(ha, dev) {
5270 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5271 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5272 rx_mode |= AcceptMulticast;
5276 spin_lock_irqsave(&tp->lock, flags);
5278 tmp = rtl8169_rx_config | rx_mode |
5279 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
5281 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5282 u32 data = mc_filter[0];
5284 mc_filter[0] = swab32(mc_filter[1]);
5285 mc_filter[1] = swab32(data);
5288 RTL_W32(MAR0 + 4, mc_filter[1]);
5289 RTL_W32(MAR0 + 0, mc_filter[0]);
5291 RTL_W32(RxConfig, tmp);
5293 spin_unlock_irqrestore(&tp->lock, flags);
5297 * rtl8169_get_stats - Get rtl8169 read/write statistics
5298 * @dev: The Ethernet Device to get statistics for
5300 * Get TX/RX statistics for rtl8169
5302 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5304 struct rtl8169_private *tp = netdev_priv(dev);
5305 void __iomem *ioaddr = tp->mmio_addr;
5306 unsigned long flags;
5308 if (netif_running(dev)) {
5309 spin_lock_irqsave(&tp->lock, flags);
5310 rtl8169_rx_missed(dev, ioaddr);
5311 spin_unlock_irqrestore(&tp->lock, flags);
5317 static void rtl8169_net_suspend(struct net_device *dev)
5319 struct rtl8169_private *tp = netdev_priv(dev);
5321 if (!netif_running(dev))
5324 rtl_pll_power_down(tp);
5326 netif_device_detach(dev);
5327 netif_stop_queue(dev);
5332 static int rtl8169_suspend(struct device *device)
5334 struct pci_dev *pdev = to_pci_dev(device);
5335 struct net_device *dev = pci_get_drvdata(pdev);
5337 rtl8169_net_suspend(dev);
5342 static void __rtl8169_resume(struct net_device *dev)
5344 struct rtl8169_private *tp = netdev_priv(dev);
5346 netif_device_attach(dev);
5348 rtl_pll_power_up(tp);
5350 rtl8169_schedule_work(dev, rtl8169_reset_task);
5353 static int rtl8169_resume(struct device *device)
5355 struct pci_dev *pdev = to_pci_dev(device);
5356 struct net_device *dev = pci_get_drvdata(pdev);
5357 struct rtl8169_private *tp = netdev_priv(dev);
5359 rtl8169_init_phy(dev, tp);
5361 if (netif_running(dev))
5362 __rtl8169_resume(dev);
5367 static int rtl8169_runtime_suspend(struct device *device)
5369 struct pci_dev *pdev = to_pci_dev(device);
5370 struct net_device *dev = pci_get_drvdata(pdev);
5371 struct rtl8169_private *tp = netdev_priv(dev);
5373 if (!tp->TxDescArray)
5376 spin_lock_irq(&tp->lock);
5377 tp->saved_wolopts = __rtl8169_get_wol(tp);
5378 __rtl8169_set_wol(tp, WAKE_ANY);
5379 spin_unlock_irq(&tp->lock);
5381 rtl8169_net_suspend(dev);
5386 static int rtl8169_runtime_resume(struct device *device)
5388 struct pci_dev *pdev = to_pci_dev(device);
5389 struct net_device *dev = pci_get_drvdata(pdev);
5390 struct rtl8169_private *tp = netdev_priv(dev);
5392 if (!tp->TxDescArray)
5395 spin_lock_irq(&tp->lock);
5396 __rtl8169_set_wol(tp, tp->saved_wolopts);
5397 tp->saved_wolopts = 0;
5398 spin_unlock_irq(&tp->lock);
5400 rtl8169_init_phy(dev, tp);
5402 __rtl8169_resume(dev);
5407 static int rtl8169_runtime_idle(struct device *device)
5409 struct pci_dev *pdev = to_pci_dev(device);
5410 struct net_device *dev = pci_get_drvdata(pdev);
5411 struct rtl8169_private *tp = netdev_priv(dev);
5413 return tp->TxDescArray ? -EBUSY : 0;
5416 static const struct dev_pm_ops rtl8169_pm_ops = {
5417 .suspend = rtl8169_suspend,
5418 .resume = rtl8169_resume,
5419 .freeze = rtl8169_suspend,
5420 .thaw = rtl8169_resume,
5421 .poweroff = rtl8169_suspend,
5422 .restore = rtl8169_resume,
5423 .runtime_suspend = rtl8169_runtime_suspend,
5424 .runtime_resume = rtl8169_runtime_resume,
5425 .runtime_idle = rtl8169_runtime_idle,
5428 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
5430 #else /* !CONFIG_PM */
5432 #define RTL8169_PM_OPS NULL
5434 #endif /* !CONFIG_PM */
5436 static void rtl_shutdown(struct pci_dev *pdev)
5438 struct net_device *dev = pci_get_drvdata(pdev);
5439 struct rtl8169_private *tp = netdev_priv(dev);
5440 void __iomem *ioaddr = tp->mmio_addr;
5442 rtl8169_net_suspend(dev);
5444 /* restore original MAC address */
5445 rtl_rar_set(tp, dev->perm_addr);
5447 spin_lock_irq(&tp->lock);
5449 rtl8169_asic_down(ioaddr);
5451 spin_unlock_irq(&tp->lock);
5453 if (system_state == SYSTEM_POWER_OFF) {
5454 /* WoL fails with some 8168 when the receiver is disabled. */
5455 if (tp->features & RTL_FEATURE_WOL) {
5456 pci_clear_master(pdev);
5458 RTL_W8(ChipCmd, CmdRxEnb);
5463 pci_wake_from_d3(pdev, true);
5464 pci_set_power_state(pdev, PCI_D3hot);
5468 static struct pci_driver rtl8169_pci_driver = {
5470 .id_table = rtl8169_pci_tbl,
5471 .probe = rtl8169_init_one,
5472 .remove = __devexit_p(rtl8169_remove_one),
5473 .shutdown = rtl_shutdown,
5474 .driver.pm = RTL8169_PM_OPS,
5477 static int __init rtl8169_init_module(void)
5479 return pci_register_driver(&rtl8169_pci_driver);
5482 static void __exit rtl8169_cleanup_module(void)
5484 pci_unregister_driver(&rtl8169_pci_driver);
5487 module_init(rtl8169_init_module);
5488 module_exit(rtl8169_cleanup_module);