2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/if_vlan.h>
20 #include <linux/crc32.h>
23 #include <linux/tcp.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/firmware.h>
29 #include <linux/pci-aspm.h>
30 #include <linux/prefetch.h>
32 #include <asm/system.h>
36 #define RTL8169_VERSION "2.3LK-NAPI"
37 #define MODULENAME "r8169"
38 #define PFX MODULENAME ": "
40 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
42 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
44 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
45 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
47 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
50 #define assert(expr) \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
53 #expr,__FILE__,__func__,__LINE__); \
55 #define dprintk(fmt, args...) \
56 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
58 #define assert(expr) do {} while (0)
59 #define dprintk(fmt, args...) do {} while (0)
60 #endif /* RTL8169_DEBUG */
62 #define R8169_MSG_DEFAULT \
63 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
65 #define TX_BUFFS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
68 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
70 static const int multicast_filter_limit = 32;
72 #define MAX_READ_REQUEST_SHIFT 12
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_VER_01 = 0,
137 RTL_GIGA_MAC_NONE = 0xff,
140 enum rtl_tx_desc_version {
145 #define JUMBO_1K ETH_DATA_LEN
146 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
147 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
148 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
149 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
151 #define _R(NAME,TD,FW,SZ,B) { \
159 static const struct {
161 enum rtl_tx_desc_version txd_version;
165 } rtl_chip_infos[] = {
167 [RTL_GIGA_MAC_VER_01] =
168 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
169 [RTL_GIGA_MAC_VER_02] =
170 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
171 [RTL_GIGA_MAC_VER_03] =
172 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
173 [RTL_GIGA_MAC_VER_04] =
174 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
175 [RTL_GIGA_MAC_VER_05] =
176 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
177 [RTL_GIGA_MAC_VER_06] =
178 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
180 [RTL_GIGA_MAC_VER_07] =
181 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
182 [RTL_GIGA_MAC_VER_08] =
183 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
184 [RTL_GIGA_MAC_VER_09] =
185 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
186 [RTL_GIGA_MAC_VER_10] =
187 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
188 [RTL_GIGA_MAC_VER_11] =
189 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
190 [RTL_GIGA_MAC_VER_12] =
191 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
192 [RTL_GIGA_MAC_VER_13] =
193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
194 [RTL_GIGA_MAC_VER_14] =
195 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
196 [RTL_GIGA_MAC_VER_15] =
197 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
198 [RTL_GIGA_MAC_VER_16] =
199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
200 [RTL_GIGA_MAC_VER_17] =
201 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
202 [RTL_GIGA_MAC_VER_18] =
203 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
204 [RTL_GIGA_MAC_VER_19] =
205 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
206 [RTL_GIGA_MAC_VER_20] =
207 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
208 [RTL_GIGA_MAC_VER_21] =
209 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
210 [RTL_GIGA_MAC_VER_22] =
211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
212 [RTL_GIGA_MAC_VER_23] =
213 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
214 [RTL_GIGA_MAC_VER_24] =
215 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
216 [RTL_GIGA_MAC_VER_25] =
217 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
219 [RTL_GIGA_MAC_VER_26] =
220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
222 [RTL_GIGA_MAC_VER_27] =
223 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
224 [RTL_GIGA_MAC_VER_28] =
225 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
226 [RTL_GIGA_MAC_VER_29] =
227 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
229 [RTL_GIGA_MAC_VER_30] =
230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
232 [RTL_GIGA_MAC_VER_31] =
233 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
234 [RTL_GIGA_MAC_VER_32] =
235 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
237 [RTL_GIGA_MAC_VER_33] =
238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
240 [RTL_GIGA_MAC_VER_34] =
241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
243 [RTL_GIGA_MAC_VER_35] =
244 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
246 [RTL_GIGA_MAC_VER_36] =
247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
258 static void rtl_hw_start_8169(struct net_device *);
259 static void rtl_hw_start_8168(struct net_device *);
260 static void rtl_hw_start_8101(struct net_device *);
262 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
263 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
264 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
265 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
266 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
267 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
268 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
269 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
270 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
271 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
272 { PCI_VENDOR_ID_LINKSYS, 0x1032,
273 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
275 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
279 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
281 static int rx_buf_sz = 16383;
288 MAC0 = 0, /* Ethernet hardware address. */
290 MAR0 = 8, /* Multicast filter. */
291 CounterAddrLow = 0x10,
292 CounterAddrHigh = 0x14,
293 TxDescStartAddrLow = 0x20,
294 TxDescStartAddrHigh = 0x24,
295 TxHDescStartAddrLow = 0x28,
296 TxHDescStartAddrHigh = 0x2c,
305 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
306 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
309 #define RX128_INT_EN (1 << 15) /* 8111c and later */
310 #define RX_MULTI_EN (1 << 14) /* 8111c only */
311 #define RXCFG_FIFO_SHIFT 13
312 /* No threshold before first PCI xfer */
313 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
314 #define RXCFG_DMA_SHIFT 8
315 /* Unlimited maximum PCI burst. */
316 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
332 RxDescAddrLow = 0xe4,
333 RxDescAddrHigh = 0xe8,
334 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
336 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
338 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
340 #define TxPacketMax (8064 >> 7)
341 #define EarlySize 0x27
344 FuncEventMask = 0xf4,
345 FuncPresetState = 0xf8,
346 FuncForceEvent = 0xfc,
349 enum rtl8110_registers {
355 enum rtl8168_8101_registers {
358 #define CSIAR_FLAG 0x80000000
359 #define CSIAR_WRITE_CMD 0x80000000
360 #define CSIAR_BYTE_ENABLE 0x0f
361 #define CSIAR_BYTE_ENABLE_SHIFT 12
362 #define CSIAR_ADDR_MASK 0x0fff
365 #define EPHYAR_FLAG 0x80000000
366 #define EPHYAR_WRITE_CMD 0x80000000
367 #define EPHYAR_REG_MASK 0x1f
368 #define EPHYAR_REG_SHIFT 16
369 #define EPHYAR_DATA_MASK 0xffff
371 #define PFM_EN (1 << 6)
373 #define FIX_NAK_1 (1 << 4)
374 #define FIX_NAK_2 (1 << 3)
377 #define NOW_IS_OOB (1 << 7)
378 #define EN_NDP (1 << 3)
379 #define EN_OOB_RESET (1 << 2)
381 #define EFUSEAR_FLAG 0x80000000
382 #define EFUSEAR_WRITE_CMD 0x80000000
383 #define EFUSEAR_READ_CMD 0x00000000
384 #define EFUSEAR_REG_MASK 0x03ff
385 #define EFUSEAR_REG_SHIFT 8
386 #define EFUSEAR_DATA_MASK 0xff
389 enum rtl8168_registers {
394 #define ERIAR_FLAG 0x80000000
395 #define ERIAR_WRITE_CMD 0x80000000
396 #define ERIAR_READ_CMD 0x00000000
397 #define ERIAR_ADDR_BYTE_ALIGN 4
398 #define ERIAR_TYPE_SHIFT 16
399 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
400 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
401 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
402 #define ERIAR_MASK_SHIFT 12
403 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
404 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
405 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
406 EPHY_RXER_NUM = 0x7c,
407 OCPDR = 0xb0, /* OCP GPHY access */
408 #define OCPDR_WRITE_CMD 0x80000000
409 #define OCPDR_READ_CMD 0x00000000
410 #define OCPDR_REG_MASK 0x7f
411 #define OCPDR_GPHY_REG_SHIFT 16
412 #define OCPDR_DATA_MASK 0xffff
414 #define OCPAR_FLAG 0x80000000
415 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
416 #define OCPAR_GPHY_READ_CMD 0x0000f060
417 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
418 MISC = 0xf0, /* 8168e only. */
419 #define TXPLA_RST (1 << 29)
420 #define PWM_EN (1 << 22)
423 enum rtl_register_content {
424 /* InterruptStatusBits */
428 TxDescUnavail = 0x0080,
452 /* TXPoll register p.5 */
453 HPQ = 0x80, /* Poll cmd on the high prio queue */
454 NPQ = 0x40, /* Poll cmd on the low prio queue */
455 FSWInt = 0x01, /* Forced software interrupt */
459 Cfg9346_Unlock = 0xc0,
464 AcceptBroadcast = 0x08,
465 AcceptMulticast = 0x04,
467 AcceptAllPhys = 0x01,
468 #define RX_CONFIG_ACCEPT_MASK 0x3f
471 TxInterFrameGapShift = 24,
472 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
474 /* Config1 register p.24 */
477 Speed_down = (1 << 4),
481 PMEnable = (1 << 0), /* Power Management Enable */
483 /* Config2 register p. 25 */
484 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
485 PCI_Clock_66MHz = 0x01,
486 PCI_Clock_33MHz = 0x00,
488 /* Config3 register p.25 */
489 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
490 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
491 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
492 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
494 /* Config4 register */
495 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
497 /* Config5 register p.27 */
498 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
499 MWF = (1 << 5), /* Accept Multicast wakeup frame */
500 UWF = (1 << 4), /* Accept Unicast wakeup frame */
502 LanWake = (1 << 1), /* LanWake enable/disable */
503 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
506 TBIReset = 0x80000000,
507 TBILoopback = 0x40000000,
508 TBINwEnable = 0x20000000,
509 TBINwRestart = 0x10000000,
510 TBILinkOk = 0x02000000,
511 TBINwComplete = 0x01000000,
514 EnableBist = (1 << 15), // 8168 8101
515 Mac_dbgo_oe = (1 << 14), // 8168 8101
516 Normal_mode = (1 << 13), // unused
517 Force_half_dup = (1 << 12), // 8168 8101
518 Force_rxflow_en = (1 << 11), // 8168 8101
519 Force_txflow_en = (1 << 10), // 8168 8101
520 Cxpl_dbg_sel = (1 << 9), // 8168 8101
521 ASF = (1 << 8), // 8168 8101
522 PktCntrDisable = (1 << 7), // 8168 8101
523 Mac_dbgo_sel = 0x001c, // 8168
528 INTT_0 = 0x0000, // 8168
529 INTT_1 = 0x0001, // 8168
530 INTT_2 = 0x0002, // 8168
531 INTT_3 = 0x0003, // 8168
533 /* rtl8169_PHYstatus */
544 TBILinkOK = 0x02000000,
546 /* DumpCounterCommand */
551 /* First doubleword. */
552 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
553 RingEnd = (1 << 30), /* End of descriptor ring */
554 FirstFrag = (1 << 29), /* First segment of a packet */
555 LastFrag = (1 << 28), /* Final segment of a packet */
559 enum rtl_tx_desc_bit {
560 /* First doubleword. */
561 TD_LSO = (1 << 27), /* Large Send Offload */
562 #define TD_MSS_MAX 0x07ffu /* MSS value */
564 /* Second doubleword. */
565 TxVlanTag = (1 << 17), /* Add VLAN tag */
568 /* 8169, 8168b and 810x except 8102e. */
569 enum rtl_tx_desc_bit_0 {
570 /* First doubleword. */
571 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
572 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
573 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
574 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
577 /* 8102e, 8168c and beyond. */
578 enum rtl_tx_desc_bit_1 {
579 /* Second doubleword. */
580 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
581 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
582 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
583 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
586 static const struct rtl_tx_desc_info {
593 } tx_desc_info [] = {
596 .udp = TD0_IP_CS | TD0_UDP_CS,
597 .tcp = TD0_IP_CS | TD0_TCP_CS
599 .mss_shift = TD0_MSS_SHIFT,
604 .udp = TD1_IP_CS | TD1_UDP_CS,
605 .tcp = TD1_IP_CS | TD1_TCP_CS
607 .mss_shift = TD1_MSS_SHIFT,
612 enum rtl_rx_desc_bit {
614 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
615 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
617 #define RxProtoUDP (PID1)
618 #define RxProtoTCP (PID0)
619 #define RxProtoIP (PID1 | PID0)
620 #define RxProtoMask RxProtoIP
622 IPFail = (1 << 16), /* IP checksum failed */
623 UDPFail = (1 << 15), /* UDP/IP checksum failed */
624 TCPFail = (1 << 14), /* TCP/IP checksum failed */
625 RxVlanTag = (1 << 16), /* VLAN tag available */
628 #define RsvdMask 0x3fffc000
645 u8 __pad[sizeof(void *) - sizeof(u32)];
649 RTL_FEATURE_WOL = (1 << 0),
650 RTL_FEATURE_MSI = (1 << 1),
651 RTL_FEATURE_GMII = (1 << 2),
654 struct rtl8169_counters {
661 __le32 tx_one_collision;
662 __le32 tx_multi_collision;
671 RTL_FLAG_TASK_ENABLED,
672 RTL_FLAG_TASK_SLOW_PENDING,
673 RTL_FLAG_TASK_RESET_PENDING,
674 RTL_FLAG_TASK_PHY_PENDING,
678 struct rtl8169_stats {
681 struct u64_stats_sync syncp;
684 struct rtl8169_private {
685 void __iomem *mmio_addr; /* memory map physical address */
686 struct pci_dev *pci_dev;
687 struct net_device *dev;
688 struct napi_struct napi;
692 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
693 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
696 struct rtl8169_stats rx_stats;
697 struct rtl8169_stats tx_stats;
698 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
699 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
700 dma_addr_t TxPhyAddr;
701 dma_addr_t RxPhyAddr;
702 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
703 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
704 struct timer_list timer;
710 void (*write)(void __iomem *, int, int);
711 int (*read)(void __iomem *, int);
714 struct pll_power_ops {
715 void (*down)(struct rtl8169_private *);
716 void (*up)(struct rtl8169_private *);
720 void (*enable)(struct rtl8169_private *);
721 void (*disable)(struct rtl8169_private *);
724 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
725 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
726 void (*phy_reset_enable)(struct rtl8169_private *tp);
727 void (*hw_start)(struct net_device *);
728 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
729 unsigned int (*link_ok)(void __iomem *);
730 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
733 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
735 struct work_struct work;
740 struct mii_if_info mii;
741 struct rtl8169_counters counters;
746 const struct firmware *fw;
748 #define RTL_VER_SIZE 32
750 char version[RTL_VER_SIZE];
752 struct rtl_fw_phy_action {
757 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
760 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
761 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
762 module_param(use_dac, int, 0);
763 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
764 module_param_named(debug, debug.msg_enable, int, 0);
765 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
766 MODULE_LICENSE("GPL");
767 MODULE_VERSION(RTL8169_VERSION);
768 MODULE_FIRMWARE(FIRMWARE_8168D_1);
769 MODULE_FIRMWARE(FIRMWARE_8168D_2);
770 MODULE_FIRMWARE(FIRMWARE_8168E_1);
771 MODULE_FIRMWARE(FIRMWARE_8168E_2);
772 MODULE_FIRMWARE(FIRMWARE_8168E_3);
773 MODULE_FIRMWARE(FIRMWARE_8105E_1);
774 MODULE_FIRMWARE(FIRMWARE_8168F_1);
775 MODULE_FIRMWARE(FIRMWARE_8168F_2);
777 static int rtl8169_open(struct net_device *dev);
778 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
779 struct net_device *dev);
780 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
781 static int rtl8169_init_ring(struct net_device *dev);
782 static void rtl_hw_start(struct net_device *dev);
783 static int rtl8169_close(struct net_device *dev);
784 static void rtl_set_rx_mode(struct net_device *dev);
785 static void rtl8169_tx_timeout(struct net_device *dev);
786 static struct rtnl_link_stats64 *rtl8169_get_stats64(struct net_device *dev,
787 struct rtnl_link_stats64
789 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
790 static void rtl8169_rx_clear(struct rtl8169_private *tp);
791 static int rtl8169_poll(struct napi_struct *napi, int budget);
793 static void rtl_lock_work(struct rtl8169_private *tp)
795 mutex_lock(&tp->wk.mutex);
798 static void rtl_unlock_work(struct rtl8169_private *tp)
800 mutex_unlock(&tp->wk.mutex);
803 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
805 int cap = pci_pcie_cap(pdev);
810 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
811 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
812 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
816 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
818 void __iomem *ioaddr = tp->mmio_addr;
821 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
822 for (i = 0; i < 20; i++) {
824 if (RTL_R32(OCPAR) & OCPAR_FLAG)
827 return RTL_R32(OCPDR);
830 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
832 void __iomem *ioaddr = tp->mmio_addr;
835 RTL_W32(OCPDR, data);
836 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
837 for (i = 0; i < 20; i++) {
839 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
844 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
846 void __iomem *ioaddr = tp->mmio_addr;
850 RTL_W32(ERIAR, 0x800010e8);
852 for (i = 0; i < 5; i++) {
854 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
858 ocp_write(tp, 0x1, 0x30, 0x00000001);
861 #define OOB_CMD_RESET 0x00
862 #define OOB_CMD_DRIVER_START 0x05
863 #define OOB_CMD_DRIVER_STOP 0x06
865 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
867 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
870 static void rtl8168_driver_start(struct rtl8169_private *tp)
875 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
877 reg = rtl8168_get_ocp_reg(tp);
879 for (i = 0; i < 10; i++) {
881 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
886 static void rtl8168_driver_stop(struct rtl8169_private *tp)
891 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
893 reg = rtl8168_get_ocp_reg(tp);
895 for (i = 0; i < 10; i++) {
897 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
902 static int r8168dp_check_dash(struct rtl8169_private *tp)
904 u16 reg = rtl8168_get_ocp_reg(tp);
906 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
909 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
913 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
915 for (i = 20; i > 0; i--) {
917 * Check if the RTL8169 has completed writing to the specified
920 if (!(RTL_R32(PHYAR) & 0x80000000))
925 * According to hardware specs a 20us delay is required after write
926 * complete indication, but before sending next command.
931 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
935 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
937 for (i = 20; i > 0; i--) {
939 * Check if the RTL8169 has completed retrieving data from
940 * the specified MII register.
942 if (RTL_R32(PHYAR) & 0x80000000) {
943 value = RTL_R32(PHYAR) & 0xffff;
949 * According to hardware specs a 20us delay is required after read
950 * complete indication, but before sending next command.
957 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
961 RTL_W32(OCPDR, data |
962 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
963 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
964 RTL_W32(EPHY_RXER_NUM, 0);
966 for (i = 0; i < 100; i++) {
968 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
973 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
975 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
976 (value & OCPDR_DATA_MASK));
979 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
983 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
986 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
987 RTL_W32(EPHY_RXER_NUM, 0);
989 for (i = 0; i < 100; i++) {
991 if (RTL_R32(OCPAR) & OCPAR_FLAG)
995 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
998 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1000 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1002 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1005 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1007 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1010 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1012 r8168dp_2_mdio_start(ioaddr);
1014 r8169_mdio_write(ioaddr, reg_addr, value);
1016 r8168dp_2_mdio_stop(ioaddr);
1019 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1023 r8168dp_2_mdio_start(ioaddr);
1025 value = r8169_mdio_read(ioaddr, reg_addr);
1027 r8168dp_2_mdio_stop(ioaddr);
1032 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1034 tp->mdio_ops.write(tp->mmio_addr, location, val);
1037 static int rtl_readphy(struct rtl8169_private *tp, int location)
1039 return tp->mdio_ops.read(tp->mmio_addr, location);
1042 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1044 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1047 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1051 val = rtl_readphy(tp, reg_addr);
1052 rtl_writephy(tp, reg_addr, (val | p) & ~m);
1055 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1058 struct rtl8169_private *tp = netdev_priv(dev);
1060 rtl_writephy(tp, location, val);
1063 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1065 struct rtl8169_private *tp = netdev_priv(dev);
1067 return rtl_readphy(tp, location);
1070 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1074 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1075 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1077 for (i = 0; i < 100; i++) {
1078 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1084 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1089 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1091 for (i = 0; i < 100; i++) {
1092 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1093 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1102 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1106 RTL_W32(CSIDR, value);
1107 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1108 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1110 for (i = 0; i < 100; i++) {
1111 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1117 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1122 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1123 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1125 for (i = 0; i < 100; i++) {
1126 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1127 value = RTL_R32(CSIDR);
1137 void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1141 BUG_ON((addr & 3) || (mask == 0));
1142 RTL_W32(ERIDR, val);
1143 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1145 for (i = 0; i < 100; i++) {
1146 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1152 static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1157 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1159 for (i = 0; i < 100; i++) {
1160 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1161 value = RTL_R32(ERIDR);
1171 rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1175 val = rtl_eri_read(ioaddr, addr, type);
1176 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1185 static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1186 const struct exgmac_reg *r, int len)
1189 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1194 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1199 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1201 for (i = 0; i < 300; i++) {
1202 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1203 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1212 static u16 rtl_get_events(struct rtl8169_private *tp)
1214 void __iomem *ioaddr = tp->mmio_addr;
1216 return RTL_R16(IntrStatus);
1219 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1221 void __iomem *ioaddr = tp->mmio_addr;
1223 RTL_W16(IntrStatus, bits);
1227 static void rtl_irq_disable(struct rtl8169_private *tp)
1229 void __iomem *ioaddr = tp->mmio_addr;
1231 RTL_W16(IntrMask, 0);
1235 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1237 void __iomem *ioaddr = tp->mmio_addr;
1239 RTL_W16(IntrMask, bits);
1242 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1243 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1244 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1246 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1248 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1251 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1253 void __iomem *ioaddr = tp->mmio_addr;
1255 rtl_irq_disable(tp);
1256 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1260 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1262 void __iomem *ioaddr = tp->mmio_addr;
1264 return RTL_R32(TBICSR) & TBIReset;
1267 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1269 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1272 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1274 return RTL_R32(TBICSR) & TBILinkOk;
1277 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1279 return RTL_R8(PHYstatus) & LinkStatus;
1282 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1284 void __iomem *ioaddr = tp->mmio_addr;
1286 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1289 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1293 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1294 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1297 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1299 void __iomem *ioaddr = tp->mmio_addr;
1300 struct net_device *dev = tp->dev;
1302 if (!netif_running(dev))
1305 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1306 if (RTL_R8(PHYstatus) & _1000bpsF) {
1307 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1308 0x00000011, ERIAR_EXGMAC);
1309 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1310 0x00000005, ERIAR_EXGMAC);
1311 } else if (RTL_R8(PHYstatus) & _100bps) {
1312 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1313 0x0000001f, ERIAR_EXGMAC);
1314 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1315 0x00000005, ERIAR_EXGMAC);
1317 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1318 0x0000001f, ERIAR_EXGMAC);
1319 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1320 0x0000003f, ERIAR_EXGMAC);
1322 /* Reset packet filter */
1323 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1325 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1327 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1328 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1329 if (RTL_R8(PHYstatus) & _1000bpsF) {
1330 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1331 0x00000011, ERIAR_EXGMAC);
1332 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1333 0x00000005, ERIAR_EXGMAC);
1335 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1336 0x0000001f, ERIAR_EXGMAC);
1337 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1338 0x0000003f, ERIAR_EXGMAC);
1343 static void __rtl8169_check_link_status(struct net_device *dev,
1344 struct rtl8169_private *tp,
1345 void __iomem *ioaddr, bool pm)
1347 if (tp->link_ok(ioaddr)) {
1348 rtl_link_chg_patch(tp);
1349 /* This is to cancel a scheduled suspend if there's one. */
1351 pm_request_resume(&tp->pci_dev->dev);
1352 netif_carrier_on(dev);
1353 if (net_ratelimit())
1354 netif_info(tp, ifup, dev, "link up\n");
1356 netif_carrier_off(dev);
1357 netif_info(tp, ifdown, dev, "link down\n");
1359 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1363 static void rtl8169_check_link_status(struct net_device *dev,
1364 struct rtl8169_private *tp,
1365 void __iomem *ioaddr)
1367 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1370 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1372 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1374 void __iomem *ioaddr = tp->mmio_addr;
1378 options = RTL_R8(Config1);
1379 if (!(options & PMEnable))
1382 options = RTL_R8(Config3);
1383 if (options & LinkUp)
1384 wolopts |= WAKE_PHY;
1385 if (options & MagicPacket)
1386 wolopts |= WAKE_MAGIC;
1388 options = RTL_R8(Config5);
1390 wolopts |= WAKE_UCAST;
1392 wolopts |= WAKE_BCAST;
1394 wolopts |= WAKE_MCAST;
1399 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1401 struct rtl8169_private *tp = netdev_priv(dev);
1405 wol->supported = WAKE_ANY;
1406 wol->wolopts = __rtl8169_get_wol(tp);
1408 rtl_unlock_work(tp);
1411 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1413 void __iomem *ioaddr = tp->mmio_addr;
1415 static const struct {
1420 { WAKE_ANY, Config1, PMEnable },
1421 { WAKE_PHY, Config3, LinkUp },
1422 { WAKE_MAGIC, Config3, MagicPacket },
1423 { WAKE_UCAST, Config5, UWF },
1424 { WAKE_BCAST, Config5, BWF },
1425 { WAKE_MCAST, Config5, MWF },
1426 { WAKE_ANY, Config5, LanWake }
1429 RTL_W8(Cfg9346, Cfg9346_Unlock);
1431 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1432 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1433 if (wolopts & cfg[i].opt)
1434 options |= cfg[i].mask;
1435 RTL_W8(cfg[i].reg, options);
1438 RTL_W8(Cfg9346, Cfg9346_Lock);
1441 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1443 struct rtl8169_private *tp = netdev_priv(dev);
1448 tp->features |= RTL_FEATURE_WOL;
1450 tp->features &= ~RTL_FEATURE_WOL;
1451 __rtl8169_set_wol(tp, wol->wolopts);
1453 rtl_unlock_work(tp);
1455 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1460 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1462 return rtl_chip_infos[tp->mac_version].fw_name;
1465 static void rtl8169_get_drvinfo(struct net_device *dev,
1466 struct ethtool_drvinfo *info)
1468 struct rtl8169_private *tp = netdev_priv(dev);
1469 struct rtl_fw *rtl_fw = tp->rtl_fw;
1471 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1472 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1473 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1474 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1475 if (!IS_ERR_OR_NULL(rtl_fw))
1476 strlcpy(info->fw_version, rtl_fw->version,
1477 sizeof(info->fw_version));
1480 static int rtl8169_get_regs_len(struct net_device *dev)
1482 return R8169_REGS_SIZE;
1485 static int rtl8169_set_speed_tbi(struct net_device *dev,
1486 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1488 struct rtl8169_private *tp = netdev_priv(dev);
1489 void __iomem *ioaddr = tp->mmio_addr;
1493 reg = RTL_R32(TBICSR);
1494 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1495 (duplex == DUPLEX_FULL)) {
1496 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1497 } else if (autoneg == AUTONEG_ENABLE)
1498 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1500 netif_warn(tp, link, dev,
1501 "incorrect speed setting refused in TBI mode\n");
1508 static int rtl8169_set_speed_xmii(struct net_device *dev,
1509 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1511 struct rtl8169_private *tp = netdev_priv(dev);
1512 int giga_ctrl, bmcr;
1515 rtl_writephy(tp, 0x1f, 0x0000);
1517 if (autoneg == AUTONEG_ENABLE) {
1520 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1521 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1522 ADVERTISE_100HALF | ADVERTISE_100FULL);
1524 if (adv & ADVERTISED_10baseT_Half)
1525 auto_nego |= ADVERTISE_10HALF;
1526 if (adv & ADVERTISED_10baseT_Full)
1527 auto_nego |= ADVERTISE_10FULL;
1528 if (adv & ADVERTISED_100baseT_Half)
1529 auto_nego |= ADVERTISE_100HALF;
1530 if (adv & ADVERTISED_100baseT_Full)
1531 auto_nego |= ADVERTISE_100FULL;
1533 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1535 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1536 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1538 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1539 if (tp->mii.supports_gmii) {
1540 if (adv & ADVERTISED_1000baseT_Half)
1541 giga_ctrl |= ADVERTISE_1000HALF;
1542 if (adv & ADVERTISED_1000baseT_Full)
1543 giga_ctrl |= ADVERTISE_1000FULL;
1544 } else if (adv & (ADVERTISED_1000baseT_Half |
1545 ADVERTISED_1000baseT_Full)) {
1546 netif_info(tp, link, dev,
1547 "PHY does not support 1000Mbps\n");
1551 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1553 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1554 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1558 if (speed == SPEED_10)
1560 else if (speed == SPEED_100)
1561 bmcr = BMCR_SPEED100;
1565 if (duplex == DUPLEX_FULL)
1566 bmcr |= BMCR_FULLDPLX;
1569 rtl_writephy(tp, MII_BMCR, bmcr);
1571 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1572 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1573 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1574 rtl_writephy(tp, 0x17, 0x2138);
1575 rtl_writephy(tp, 0x0e, 0x0260);
1577 rtl_writephy(tp, 0x17, 0x2108);
1578 rtl_writephy(tp, 0x0e, 0x0000);
1587 static int rtl8169_set_speed(struct net_device *dev,
1588 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1590 struct rtl8169_private *tp = netdev_priv(dev);
1593 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1597 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1598 (advertising & ADVERTISED_1000baseT_Full)) {
1599 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1605 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1607 struct rtl8169_private *tp = netdev_priv(dev);
1610 del_timer_sync(&tp->timer);
1613 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1614 cmd->duplex, cmd->advertising);
1615 rtl_unlock_work(tp);
1620 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1621 netdev_features_t features)
1623 struct rtl8169_private *tp = netdev_priv(dev);
1625 if (dev->mtu > TD_MSS_MAX)
1626 features &= ~NETIF_F_ALL_TSO;
1628 if (dev->mtu > JUMBO_1K &&
1629 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1630 features &= ~NETIF_F_IP_CSUM;
1635 static void __rtl8169_set_features(struct net_device *dev,
1636 netdev_features_t features)
1638 struct rtl8169_private *tp = netdev_priv(dev);
1639 netdev_features_t changed = features ^ dev->features;
1640 void __iomem *ioaddr = tp->mmio_addr;
1642 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1645 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1646 if (features & NETIF_F_RXCSUM)
1647 tp->cp_cmd |= RxChkSum;
1649 tp->cp_cmd &= ~RxChkSum;
1651 if (dev->features & NETIF_F_HW_VLAN_RX)
1652 tp->cp_cmd |= RxVlan;
1654 tp->cp_cmd &= ~RxVlan;
1656 RTL_W16(CPlusCmd, tp->cp_cmd);
1659 if (changed & NETIF_F_RXALL) {
1660 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1661 if (features & NETIF_F_RXALL)
1662 tmp |= (AcceptErr | AcceptRunt);
1663 RTL_W32(RxConfig, tmp);
1667 static int rtl8169_set_features(struct net_device *dev,
1668 netdev_features_t features)
1670 struct rtl8169_private *tp = netdev_priv(dev);
1673 __rtl8169_set_features(dev, features);
1674 rtl_unlock_work(tp);
1680 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1681 struct sk_buff *skb)
1683 return (vlan_tx_tag_present(skb)) ?
1684 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1687 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1689 u32 opts2 = le32_to_cpu(desc->opts2);
1691 if (opts2 & RxVlanTag)
1692 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1697 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1699 struct rtl8169_private *tp = netdev_priv(dev);
1700 void __iomem *ioaddr = tp->mmio_addr;
1704 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1705 cmd->port = PORT_FIBRE;
1706 cmd->transceiver = XCVR_INTERNAL;
1708 status = RTL_R32(TBICSR);
1709 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1710 cmd->autoneg = !!(status & TBINwEnable);
1712 ethtool_cmd_speed_set(cmd, SPEED_1000);
1713 cmd->duplex = DUPLEX_FULL; /* Always set */
1718 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1720 struct rtl8169_private *tp = netdev_priv(dev);
1722 return mii_ethtool_gset(&tp->mii, cmd);
1725 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1727 struct rtl8169_private *tp = netdev_priv(dev);
1731 rc = tp->get_settings(dev, cmd);
1732 rtl_unlock_work(tp);
1737 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1740 struct rtl8169_private *tp = netdev_priv(dev);
1742 if (regs->len > R8169_REGS_SIZE)
1743 regs->len = R8169_REGS_SIZE;
1746 memcpy_fromio(p, tp->mmio_addr, regs->len);
1747 rtl_unlock_work(tp);
1750 static u32 rtl8169_get_msglevel(struct net_device *dev)
1752 struct rtl8169_private *tp = netdev_priv(dev);
1754 return tp->msg_enable;
1757 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1759 struct rtl8169_private *tp = netdev_priv(dev);
1761 tp->msg_enable = value;
1764 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1771 "tx_single_collisions",
1772 "tx_multi_collisions",
1780 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1784 return ARRAY_SIZE(rtl8169_gstrings);
1790 static void rtl8169_update_counters(struct net_device *dev)
1792 struct rtl8169_private *tp = netdev_priv(dev);
1793 void __iomem *ioaddr = tp->mmio_addr;
1794 struct device *d = &tp->pci_dev->dev;
1795 struct rtl8169_counters *counters;
1801 * Some chips are unable to dump tally counters when the receiver
1804 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1807 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1811 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1812 cmd = (u64)paddr & DMA_BIT_MASK(32);
1813 RTL_W32(CounterAddrLow, cmd);
1814 RTL_W32(CounterAddrLow, cmd | CounterDump);
1817 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1818 memcpy(&tp->counters, counters, sizeof(*counters));
1824 RTL_W32(CounterAddrLow, 0);
1825 RTL_W32(CounterAddrHigh, 0);
1827 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1830 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1831 struct ethtool_stats *stats, u64 *data)
1833 struct rtl8169_private *tp = netdev_priv(dev);
1837 rtl8169_update_counters(dev);
1839 data[0] = le64_to_cpu(tp->counters.tx_packets);
1840 data[1] = le64_to_cpu(tp->counters.rx_packets);
1841 data[2] = le64_to_cpu(tp->counters.tx_errors);
1842 data[3] = le32_to_cpu(tp->counters.rx_errors);
1843 data[4] = le16_to_cpu(tp->counters.rx_missed);
1844 data[5] = le16_to_cpu(tp->counters.align_errors);
1845 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1846 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1847 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1848 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1849 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1850 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1851 data[12] = le16_to_cpu(tp->counters.tx_underun);
1854 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1858 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1863 static const struct ethtool_ops rtl8169_ethtool_ops = {
1864 .get_drvinfo = rtl8169_get_drvinfo,
1865 .get_regs_len = rtl8169_get_regs_len,
1866 .get_link = ethtool_op_get_link,
1867 .get_settings = rtl8169_get_settings,
1868 .set_settings = rtl8169_set_settings,
1869 .get_msglevel = rtl8169_get_msglevel,
1870 .set_msglevel = rtl8169_set_msglevel,
1871 .get_regs = rtl8169_get_regs,
1872 .get_wol = rtl8169_get_wol,
1873 .set_wol = rtl8169_set_wol,
1874 .get_strings = rtl8169_get_strings,
1875 .get_sset_count = rtl8169_get_sset_count,
1876 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1879 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1880 struct net_device *dev, u8 default_version)
1882 void __iomem *ioaddr = tp->mmio_addr;
1884 * The driver currently handles the 8168Bf and the 8168Be identically
1885 * but they can be identified more specifically through the test below
1888 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1890 * Same thing for the 8101Eb and the 8101Ec:
1892 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1894 static const struct rtl_mac_info {
1900 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1901 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1904 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
1905 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1906 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1907 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1910 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1911 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1912 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1914 /* 8168DP family. */
1915 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1916 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1917 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
1920 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1921 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1922 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1923 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1924 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1925 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1926 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1927 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1928 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1931 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1932 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1933 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1934 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1937 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
1938 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1939 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1940 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1941 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1942 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1943 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1944 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1945 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1946 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1947 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1948 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1949 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1950 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1951 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1952 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1953 /* FIXME: where did these entries come from ? -- FR */
1954 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1955 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1958 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1959 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1960 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1961 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1962 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1963 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1966 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1968 const struct rtl_mac_info *p = mac_info;
1971 reg = RTL_R32(TxConfig);
1972 while ((reg & p->mask) != p->val)
1974 tp->mac_version = p->mac_version;
1976 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1977 netif_notice(tp, probe, dev,
1978 "unknown MAC, using family default\n");
1979 tp->mac_version = default_version;
1983 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1985 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1993 static void rtl_writephy_batch(struct rtl8169_private *tp,
1994 const struct phy_reg *regs, int len)
1997 rtl_writephy(tp, regs->reg, regs->val);
2002 #define PHY_READ 0x00000000
2003 #define PHY_DATA_OR 0x10000000
2004 #define PHY_DATA_AND 0x20000000
2005 #define PHY_BJMPN 0x30000000
2006 #define PHY_READ_EFUSE 0x40000000
2007 #define PHY_READ_MAC_BYTE 0x50000000
2008 #define PHY_WRITE_MAC_BYTE 0x60000000
2009 #define PHY_CLEAR_READCOUNT 0x70000000
2010 #define PHY_WRITE 0x80000000
2011 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2012 #define PHY_COMP_EQ_SKIPN 0xa0000000
2013 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2014 #define PHY_WRITE_PREVIOUS 0xc0000000
2015 #define PHY_SKIPN 0xd0000000
2016 #define PHY_DELAY_MS 0xe0000000
2017 #define PHY_WRITE_ERI_WORD 0xf0000000
2021 char version[RTL_VER_SIZE];
2027 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2029 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2031 const struct firmware *fw = rtl_fw->fw;
2032 struct fw_info *fw_info = (struct fw_info *)fw->data;
2033 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2034 char *version = rtl_fw->version;
2037 if (fw->size < FW_OPCODE_SIZE)
2040 if (!fw_info->magic) {
2041 size_t i, size, start;
2044 if (fw->size < sizeof(*fw_info))
2047 for (i = 0; i < fw->size; i++)
2048 checksum += fw->data[i];
2052 start = le32_to_cpu(fw_info->fw_start);
2053 if (start > fw->size)
2056 size = le32_to_cpu(fw_info->fw_len);
2057 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2060 memcpy(version, fw_info->version, RTL_VER_SIZE);
2062 pa->code = (__le32 *)(fw->data + start);
2065 if (fw->size % FW_OPCODE_SIZE)
2068 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2070 pa->code = (__le32 *)fw->data;
2071 pa->size = fw->size / FW_OPCODE_SIZE;
2073 version[RTL_VER_SIZE - 1] = 0;
2080 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2081 struct rtl_fw_phy_action *pa)
2086 for (index = 0; index < pa->size; index++) {
2087 u32 action = le32_to_cpu(pa->code[index]);
2088 u32 regno = (action & 0x0fff0000) >> 16;
2090 switch(action & 0xf0000000) {
2094 case PHY_READ_EFUSE:
2095 case PHY_CLEAR_READCOUNT:
2097 case PHY_WRITE_PREVIOUS:
2102 if (regno > index) {
2103 netif_err(tp, ifup, tp->dev,
2104 "Out of range of firmware\n");
2108 case PHY_READCOUNT_EQ_SKIP:
2109 if (index + 2 >= pa->size) {
2110 netif_err(tp, ifup, tp->dev,
2111 "Out of range of firmware\n");
2115 case PHY_COMP_EQ_SKIPN:
2116 case PHY_COMP_NEQ_SKIPN:
2118 if (index + 1 + regno >= pa->size) {
2119 netif_err(tp, ifup, tp->dev,
2120 "Out of range of firmware\n");
2125 case PHY_READ_MAC_BYTE:
2126 case PHY_WRITE_MAC_BYTE:
2127 case PHY_WRITE_ERI_WORD:
2129 netif_err(tp, ifup, tp->dev,
2130 "Invalid action 0x%08x\n", action);
2139 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2141 struct net_device *dev = tp->dev;
2144 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2145 netif_err(tp, ifup, dev, "invalid firwmare\n");
2149 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2155 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2157 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2161 predata = count = 0;
2163 for (index = 0; index < pa->size; ) {
2164 u32 action = le32_to_cpu(pa->code[index]);
2165 u32 data = action & 0x0000ffff;
2166 u32 regno = (action & 0x0fff0000) >> 16;
2171 switch(action & 0xf0000000) {
2173 predata = rtl_readphy(tp, regno);
2188 case PHY_READ_EFUSE:
2189 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2192 case PHY_CLEAR_READCOUNT:
2197 rtl_writephy(tp, regno, data);
2200 case PHY_READCOUNT_EQ_SKIP:
2201 index += (count == data) ? 2 : 1;
2203 case PHY_COMP_EQ_SKIPN:
2204 if (predata == data)
2208 case PHY_COMP_NEQ_SKIPN:
2209 if (predata != data)
2213 case PHY_WRITE_PREVIOUS:
2214 rtl_writephy(tp, regno, predata);
2225 case PHY_READ_MAC_BYTE:
2226 case PHY_WRITE_MAC_BYTE:
2227 case PHY_WRITE_ERI_WORD:
2234 static void rtl_release_firmware(struct rtl8169_private *tp)
2236 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2237 release_firmware(tp->rtl_fw->fw);
2240 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2243 static void rtl_apply_firmware(struct rtl8169_private *tp)
2245 struct rtl_fw *rtl_fw = tp->rtl_fw;
2247 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2248 if (!IS_ERR_OR_NULL(rtl_fw))
2249 rtl_phy_write_fw(tp, rtl_fw);
2252 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2254 if (rtl_readphy(tp, reg) != val)
2255 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2257 rtl_apply_firmware(tp);
2260 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2262 static const struct phy_reg phy_reg_init[] = {
2324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2327 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2329 static const struct phy_reg phy_reg_init[] = {
2335 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2338 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2340 struct pci_dev *pdev = tp->pci_dev;
2342 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2343 (pdev->subsystem_device != 0xe000))
2346 rtl_writephy(tp, 0x1f, 0x0001);
2347 rtl_writephy(tp, 0x10, 0xf01b);
2348 rtl_writephy(tp, 0x1f, 0x0000);
2351 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2353 static const struct phy_reg phy_reg_init[] = {
2393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2395 rtl8169scd_hw_phy_config_quirk(tp);
2398 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2400 static const struct phy_reg phy_reg_init[] = {
2448 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2451 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2453 static const struct phy_reg phy_reg_init[] = {
2458 rtl_writephy(tp, 0x1f, 0x0001);
2459 rtl_patchphy(tp, 0x16, 1 << 0);
2461 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2464 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2466 static const struct phy_reg phy_reg_init[] = {
2472 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2475 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2477 static const struct phy_reg phy_reg_init[] = {
2485 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2488 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2490 static const struct phy_reg phy_reg_init[] = {
2496 rtl_writephy(tp, 0x1f, 0x0000);
2497 rtl_patchphy(tp, 0x14, 1 << 5);
2498 rtl_patchphy(tp, 0x0d, 1 << 5);
2500 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2503 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2505 static const struct phy_reg phy_reg_init[] = {
2525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2527 rtl_patchphy(tp, 0x14, 1 << 5);
2528 rtl_patchphy(tp, 0x0d, 1 << 5);
2529 rtl_writephy(tp, 0x1f, 0x0000);
2532 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2534 static const struct phy_reg phy_reg_init[] = {
2552 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2554 rtl_patchphy(tp, 0x16, 1 << 0);
2555 rtl_patchphy(tp, 0x14, 1 << 5);
2556 rtl_patchphy(tp, 0x0d, 1 << 5);
2557 rtl_writephy(tp, 0x1f, 0x0000);
2560 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2562 static const struct phy_reg phy_reg_init[] = {
2574 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2576 rtl_patchphy(tp, 0x16, 1 << 0);
2577 rtl_patchphy(tp, 0x14, 1 << 5);
2578 rtl_patchphy(tp, 0x0d, 1 << 5);
2579 rtl_writephy(tp, 0x1f, 0x0000);
2582 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2584 rtl8168c_3_hw_phy_config(tp);
2587 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2589 static const struct phy_reg phy_reg_init_0[] = {
2590 /* Channel Estimation */
2611 * Enhance line driver power
2620 * Can not link to 1Gbps with bad cable
2621 * Decrease SNR threshold form 21.07dB to 19.04dB
2629 void __iomem *ioaddr = tp->mmio_addr;
2631 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2635 * Fine Tune Switching regulator parameter
2637 rtl_writephy(tp, 0x1f, 0x0002);
2638 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2639 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2641 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2642 static const struct phy_reg phy_reg_init[] = {
2652 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2654 val = rtl_readphy(tp, 0x0d);
2656 if ((val & 0x00ff) != 0x006c) {
2657 static const u32 set[] = {
2658 0x0065, 0x0066, 0x0067, 0x0068,
2659 0x0069, 0x006a, 0x006b, 0x006c
2663 rtl_writephy(tp, 0x1f, 0x0002);
2666 for (i = 0; i < ARRAY_SIZE(set); i++)
2667 rtl_writephy(tp, 0x0d, val | set[i]);
2670 static const struct phy_reg phy_reg_init[] = {
2678 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2681 /* RSET couple improve */
2682 rtl_writephy(tp, 0x1f, 0x0002);
2683 rtl_patchphy(tp, 0x0d, 0x0300);
2684 rtl_patchphy(tp, 0x0f, 0x0010);
2686 /* Fine tune PLL performance */
2687 rtl_writephy(tp, 0x1f, 0x0002);
2688 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2689 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2691 rtl_writephy(tp, 0x1f, 0x0005);
2692 rtl_writephy(tp, 0x05, 0x001b);
2694 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2696 rtl_writephy(tp, 0x1f, 0x0000);
2699 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2701 static const struct phy_reg phy_reg_init_0[] = {
2702 /* Channel Estimation */
2723 * Enhance line driver power
2732 * Can not link to 1Gbps with bad cable
2733 * Decrease SNR threshold form 21.07dB to 19.04dB
2741 void __iomem *ioaddr = tp->mmio_addr;
2743 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2745 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2746 static const struct phy_reg phy_reg_init[] = {
2757 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2759 val = rtl_readphy(tp, 0x0d);
2760 if ((val & 0x00ff) != 0x006c) {
2761 static const u32 set[] = {
2762 0x0065, 0x0066, 0x0067, 0x0068,
2763 0x0069, 0x006a, 0x006b, 0x006c
2767 rtl_writephy(tp, 0x1f, 0x0002);
2770 for (i = 0; i < ARRAY_SIZE(set); i++)
2771 rtl_writephy(tp, 0x0d, val | set[i]);
2774 static const struct phy_reg phy_reg_init[] = {
2782 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2785 /* Fine tune PLL performance */
2786 rtl_writephy(tp, 0x1f, 0x0002);
2787 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2788 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2790 /* Switching regulator Slew rate */
2791 rtl_writephy(tp, 0x1f, 0x0002);
2792 rtl_patchphy(tp, 0x0f, 0x0017);
2794 rtl_writephy(tp, 0x1f, 0x0005);
2795 rtl_writephy(tp, 0x05, 0x001b);
2797 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2799 rtl_writephy(tp, 0x1f, 0x0000);
2802 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2804 static const struct phy_reg phy_reg_init[] = {
2860 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2863 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2865 static const struct phy_reg phy_reg_init[] = {
2875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2876 rtl_patchphy(tp, 0x0d, 1 << 5);
2879 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
2881 static const struct phy_reg phy_reg_init[] = {
2882 /* Enable Delay cap */
2888 /* Channel estimation fine tune */
2897 /* Update PFM & 10M TX idle timer */
2909 rtl_apply_firmware(tp);
2911 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2913 /* DCO enable for 10M IDLE Power */
2914 rtl_writephy(tp, 0x1f, 0x0007);
2915 rtl_writephy(tp, 0x1e, 0x0023);
2916 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2917 rtl_writephy(tp, 0x1f, 0x0000);
2919 /* For impedance matching */
2920 rtl_writephy(tp, 0x1f, 0x0002);
2921 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2922 rtl_writephy(tp, 0x1f, 0x0000);
2924 /* PHY auto speed down */
2925 rtl_writephy(tp, 0x1f, 0x0007);
2926 rtl_writephy(tp, 0x1e, 0x002d);
2927 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2928 rtl_writephy(tp, 0x1f, 0x0000);
2929 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2931 rtl_writephy(tp, 0x1f, 0x0005);
2932 rtl_writephy(tp, 0x05, 0x8b86);
2933 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2934 rtl_writephy(tp, 0x1f, 0x0000);
2936 rtl_writephy(tp, 0x1f, 0x0005);
2937 rtl_writephy(tp, 0x05, 0x8b85);
2938 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2939 rtl_writephy(tp, 0x1f, 0x0007);
2940 rtl_writephy(tp, 0x1e, 0x0020);
2941 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2942 rtl_writephy(tp, 0x1f, 0x0006);
2943 rtl_writephy(tp, 0x00, 0x5a00);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945 rtl_writephy(tp, 0x0d, 0x0007);
2946 rtl_writephy(tp, 0x0e, 0x003c);
2947 rtl_writephy(tp, 0x0d, 0x4007);
2948 rtl_writephy(tp, 0x0e, 0x0000);
2949 rtl_writephy(tp, 0x0d, 0x0000);
2952 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2954 static const struct phy_reg phy_reg_init[] = {
2955 /* Enable Delay cap */
2964 /* Channel estimation fine tune */
2981 rtl_apply_firmware(tp);
2983 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2985 /* For 4-corner performance improve */
2986 rtl_writephy(tp, 0x1f, 0x0005);
2987 rtl_writephy(tp, 0x05, 0x8b80);
2988 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2989 rtl_writephy(tp, 0x1f, 0x0000);
2991 /* PHY auto speed down */
2992 rtl_writephy(tp, 0x1f, 0x0004);
2993 rtl_writephy(tp, 0x1f, 0x0007);
2994 rtl_writephy(tp, 0x1e, 0x002d);
2995 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2996 rtl_writephy(tp, 0x1f, 0x0002);
2997 rtl_writephy(tp, 0x1f, 0x0000);
2998 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3000 /* improve 10M EEE waveform */
3001 rtl_writephy(tp, 0x1f, 0x0005);
3002 rtl_writephy(tp, 0x05, 0x8b86);
3003 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3004 rtl_writephy(tp, 0x1f, 0x0000);
3006 /* Improve 2-pair detection performance */
3007 rtl_writephy(tp, 0x1f, 0x0005);
3008 rtl_writephy(tp, 0x05, 0x8b85);
3009 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3010 rtl_writephy(tp, 0x1f, 0x0000);
3013 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3015 rtl_writephy(tp, 0x1f, 0x0005);
3016 rtl_writephy(tp, 0x05, 0x8b85);
3017 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3018 rtl_writephy(tp, 0x1f, 0x0004);
3019 rtl_writephy(tp, 0x1f, 0x0007);
3020 rtl_writephy(tp, 0x1e, 0x0020);
3021 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3022 rtl_writephy(tp, 0x1f, 0x0002);
3023 rtl_writephy(tp, 0x1f, 0x0000);
3024 rtl_writephy(tp, 0x0d, 0x0007);
3025 rtl_writephy(tp, 0x0e, 0x003c);
3026 rtl_writephy(tp, 0x0d, 0x4007);
3027 rtl_writephy(tp, 0x0e, 0x0000);
3028 rtl_writephy(tp, 0x0d, 0x0000);
3031 rtl_writephy(tp, 0x1f, 0x0003);
3032 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3033 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3034 rtl_writephy(tp, 0x1f, 0x0000);
3037 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3039 static const struct phy_reg phy_reg_init[] = {
3040 /* Channel estimation fine tune */
3045 /* Modify green table for giga & fnet */
3062 /* Modify green table for 10M */
3068 /* Disable hiimpedance detection (RTCT) */
3074 rtl_apply_firmware(tp);
3076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3078 /* For 4-corner performance improve */
3079 rtl_writephy(tp, 0x1f, 0x0005);
3080 rtl_writephy(tp, 0x05, 0x8b80);
3081 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3084 /* PHY auto speed down */
3085 rtl_writephy(tp, 0x1f, 0x0007);
3086 rtl_writephy(tp, 0x1e, 0x002d);
3087 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3088 rtl_writephy(tp, 0x1f, 0x0000);
3089 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3091 /* Improve 10M EEE waveform */
3092 rtl_writephy(tp, 0x1f, 0x0005);
3093 rtl_writephy(tp, 0x05, 0x8b86);
3094 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3095 rtl_writephy(tp, 0x1f, 0x0000);
3097 /* Improve 2-pair detection performance */
3098 rtl_writephy(tp, 0x1f, 0x0005);
3099 rtl_writephy(tp, 0x05, 0x8b85);
3100 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3101 rtl_writephy(tp, 0x1f, 0x0000);
3104 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3106 rtl_apply_firmware(tp);
3108 /* For 4-corner performance improve */
3109 rtl_writephy(tp, 0x1f, 0x0005);
3110 rtl_writephy(tp, 0x05, 0x8b80);
3111 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3112 rtl_writephy(tp, 0x1f, 0x0000);
3114 /* PHY auto speed down */
3115 rtl_writephy(tp, 0x1f, 0x0007);
3116 rtl_writephy(tp, 0x1e, 0x002d);
3117 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3118 rtl_writephy(tp, 0x1f, 0x0000);
3119 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3121 /* Improve 10M EEE waveform */
3122 rtl_writephy(tp, 0x1f, 0x0005);
3123 rtl_writephy(tp, 0x05, 0x8b86);
3124 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3125 rtl_writephy(tp, 0x1f, 0x0000);
3128 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3130 static const struct phy_reg phy_reg_init[] = {
3137 rtl_writephy(tp, 0x1f, 0x0000);
3138 rtl_patchphy(tp, 0x11, 1 << 12);
3139 rtl_patchphy(tp, 0x19, 1 << 13);
3140 rtl_patchphy(tp, 0x10, 1 << 15);
3142 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3145 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3147 static const struct phy_reg phy_reg_init[] = {
3161 /* Disable ALDPS before ram code */
3162 rtl_writephy(tp, 0x1f, 0x0000);
3163 rtl_writephy(tp, 0x18, 0x0310);
3166 rtl_apply_firmware(tp);
3168 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3171 static void rtl_hw_phy_config(struct net_device *dev)
3173 struct rtl8169_private *tp = netdev_priv(dev);
3175 rtl8169_print_mac_version(tp);
3177 switch (tp->mac_version) {
3178 case RTL_GIGA_MAC_VER_01:
3180 case RTL_GIGA_MAC_VER_02:
3181 case RTL_GIGA_MAC_VER_03:
3182 rtl8169s_hw_phy_config(tp);
3184 case RTL_GIGA_MAC_VER_04:
3185 rtl8169sb_hw_phy_config(tp);
3187 case RTL_GIGA_MAC_VER_05:
3188 rtl8169scd_hw_phy_config(tp);
3190 case RTL_GIGA_MAC_VER_06:
3191 rtl8169sce_hw_phy_config(tp);
3193 case RTL_GIGA_MAC_VER_07:
3194 case RTL_GIGA_MAC_VER_08:
3195 case RTL_GIGA_MAC_VER_09:
3196 rtl8102e_hw_phy_config(tp);
3198 case RTL_GIGA_MAC_VER_11:
3199 rtl8168bb_hw_phy_config(tp);
3201 case RTL_GIGA_MAC_VER_12:
3202 rtl8168bef_hw_phy_config(tp);
3204 case RTL_GIGA_MAC_VER_17:
3205 rtl8168bef_hw_phy_config(tp);
3207 case RTL_GIGA_MAC_VER_18:
3208 rtl8168cp_1_hw_phy_config(tp);
3210 case RTL_GIGA_MAC_VER_19:
3211 rtl8168c_1_hw_phy_config(tp);
3213 case RTL_GIGA_MAC_VER_20:
3214 rtl8168c_2_hw_phy_config(tp);
3216 case RTL_GIGA_MAC_VER_21:
3217 rtl8168c_3_hw_phy_config(tp);
3219 case RTL_GIGA_MAC_VER_22:
3220 rtl8168c_4_hw_phy_config(tp);
3222 case RTL_GIGA_MAC_VER_23:
3223 case RTL_GIGA_MAC_VER_24:
3224 rtl8168cp_2_hw_phy_config(tp);
3226 case RTL_GIGA_MAC_VER_25:
3227 rtl8168d_1_hw_phy_config(tp);
3229 case RTL_GIGA_MAC_VER_26:
3230 rtl8168d_2_hw_phy_config(tp);
3232 case RTL_GIGA_MAC_VER_27:
3233 rtl8168d_3_hw_phy_config(tp);
3235 case RTL_GIGA_MAC_VER_28:
3236 rtl8168d_4_hw_phy_config(tp);
3238 case RTL_GIGA_MAC_VER_29:
3239 case RTL_GIGA_MAC_VER_30:
3240 rtl8105e_hw_phy_config(tp);
3242 case RTL_GIGA_MAC_VER_31:
3245 case RTL_GIGA_MAC_VER_32:
3246 case RTL_GIGA_MAC_VER_33:
3247 rtl8168e_1_hw_phy_config(tp);
3249 case RTL_GIGA_MAC_VER_34:
3250 rtl8168e_2_hw_phy_config(tp);
3252 case RTL_GIGA_MAC_VER_35:
3253 rtl8168f_1_hw_phy_config(tp);
3255 case RTL_GIGA_MAC_VER_36:
3256 rtl8168f_2_hw_phy_config(tp);
3264 static void rtl_phy_work(struct rtl8169_private *tp)
3266 struct timer_list *timer = &tp->timer;
3267 void __iomem *ioaddr = tp->mmio_addr;
3268 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3270 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
3272 if (tp->phy_reset_pending(tp)) {
3274 * A busy loop could burn quite a few cycles on nowadays CPU.
3275 * Let's delay the execution of the timer for a few ticks.
3281 if (tp->link_ok(ioaddr))
3284 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
3286 tp->phy_reset_enable(tp);
3289 mod_timer(timer, jiffies + timeout);
3292 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3294 if (!test_and_set_bit(flag, tp->wk.flags))
3295 schedule_work(&tp->wk.work);
3298 static void rtl8169_phy_timer(unsigned long __opaque)
3300 struct net_device *dev = (struct net_device *)__opaque;
3301 struct rtl8169_private *tp = netdev_priv(dev);
3303 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
3306 #ifdef CONFIG_NET_POLL_CONTROLLER
3307 static void rtl8169_netpoll(struct net_device *dev)
3309 struct rtl8169_private *tp = netdev_priv(dev);
3311 rtl8169_interrupt(tp->pci_dev->irq, dev);
3315 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3316 void __iomem *ioaddr)
3319 pci_release_regions(pdev);
3320 pci_clear_mwi(pdev);
3321 pci_disable_device(pdev);
3325 static void rtl8169_phy_reset(struct net_device *dev,
3326 struct rtl8169_private *tp)
3330 tp->phy_reset_enable(tp);
3331 for (i = 0; i < 100; i++) {
3332 if (!tp->phy_reset_pending(tp))
3336 netif_err(tp, link, dev, "PHY reset failed\n");
3339 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3341 void __iomem *ioaddr = tp->mmio_addr;
3343 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3344 (RTL_R8(PHYstatus) & TBI_Enable);
3347 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3349 void __iomem *ioaddr = tp->mmio_addr;
3351 rtl_hw_phy_config(dev);
3353 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3354 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3358 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3360 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3361 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3363 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
3364 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3366 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
3367 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
3370 rtl8169_phy_reset(dev, tp);
3372 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3373 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3374 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3375 (tp->mii.supports_gmii ?
3376 ADVERTISED_1000baseT_Half |
3377 ADVERTISED_1000baseT_Full : 0));
3379 if (rtl_tbi_enabled(tp))
3380 netif_info(tp, link, dev, "TBI auto-negotiating\n");
3383 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3385 void __iomem *ioaddr = tp->mmio_addr;
3389 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3390 high = addr[4] | (addr[5] << 8);
3394 RTL_W8(Cfg9346, Cfg9346_Unlock);
3396 RTL_W32(MAC4, high);
3402 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3403 const struct exgmac_reg e[] = {
3404 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3405 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3406 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3407 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3411 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3414 RTL_W8(Cfg9346, Cfg9346_Lock);
3416 rtl_unlock_work(tp);
3419 static int rtl_set_mac_address(struct net_device *dev, void *p)
3421 struct rtl8169_private *tp = netdev_priv(dev);
3422 struct sockaddr *addr = p;
3424 if (!is_valid_ether_addr(addr->sa_data))
3425 return -EADDRNOTAVAIL;
3427 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3429 rtl_rar_set(tp, dev->dev_addr);
3434 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3436 struct rtl8169_private *tp = netdev_priv(dev);
3437 struct mii_ioctl_data *data = if_mii(ifr);
3439 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3442 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3443 struct mii_ioctl_data *data, int cmd)
3447 data->phy_id = 32; /* Internal PHY */
3451 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3455 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3461 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3466 static const struct rtl_cfg_info {
3467 void (*hw_start)(struct net_device *);
3468 unsigned int region;
3473 } rtl_cfg_infos [] = {
3475 .hw_start = rtl_hw_start_8169,
3478 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
3479 .features = RTL_FEATURE_GMII,
3480 .default_ver = RTL_GIGA_MAC_VER_01,
3483 .hw_start = rtl_hw_start_8168,
3486 .event_slow = SYSErr | LinkChg | RxOverflow,
3487 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3488 .default_ver = RTL_GIGA_MAC_VER_11,
3491 .hw_start = rtl_hw_start_8101,
3494 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
3496 .features = RTL_FEATURE_MSI,
3497 .default_ver = RTL_GIGA_MAC_VER_13,
3501 /* Cfg9346_Unlock assumed. */
3502 static unsigned rtl_try_msi(struct rtl8169_private *tp,
3503 const struct rtl_cfg_info *cfg)
3505 void __iomem *ioaddr = tp->mmio_addr;
3509 cfg2 = RTL_R8(Config2) & ~MSIEnable;
3510 if (cfg->features & RTL_FEATURE_MSI) {
3511 if (pci_enable_msi(tp->pci_dev)) {
3512 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
3515 msi = RTL_FEATURE_MSI;
3518 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3519 RTL_W8(Config2, cfg2);
3523 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3525 if (tp->features & RTL_FEATURE_MSI) {
3526 pci_disable_msi(pdev);
3527 tp->features &= ~RTL_FEATURE_MSI;
3531 static const struct net_device_ops rtl8169_netdev_ops = {
3532 .ndo_open = rtl8169_open,
3533 .ndo_stop = rtl8169_close,
3534 .ndo_get_stats64 = rtl8169_get_stats64,
3535 .ndo_start_xmit = rtl8169_start_xmit,
3536 .ndo_tx_timeout = rtl8169_tx_timeout,
3537 .ndo_validate_addr = eth_validate_addr,
3538 .ndo_change_mtu = rtl8169_change_mtu,
3539 .ndo_fix_features = rtl8169_fix_features,
3540 .ndo_set_features = rtl8169_set_features,
3541 .ndo_set_mac_address = rtl_set_mac_address,
3542 .ndo_do_ioctl = rtl8169_ioctl,
3543 .ndo_set_rx_mode = rtl_set_rx_mode,
3544 #ifdef CONFIG_NET_POLL_CONTROLLER
3545 .ndo_poll_controller = rtl8169_netpoll,
3550 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3552 struct mdio_ops *ops = &tp->mdio_ops;
3554 switch (tp->mac_version) {
3555 case RTL_GIGA_MAC_VER_27:
3556 ops->write = r8168dp_1_mdio_write;
3557 ops->read = r8168dp_1_mdio_read;
3559 case RTL_GIGA_MAC_VER_28:
3560 case RTL_GIGA_MAC_VER_31:
3561 ops->write = r8168dp_2_mdio_write;
3562 ops->read = r8168dp_2_mdio_read;
3565 ops->write = r8169_mdio_write;
3566 ops->read = r8169_mdio_read;
3571 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3573 void __iomem *ioaddr = tp->mmio_addr;
3575 switch (tp->mac_version) {
3576 case RTL_GIGA_MAC_VER_29:
3577 case RTL_GIGA_MAC_VER_30:
3578 case RTL_GIGA_MAC_VER_32:
3579 case RTL_GIGA_MAC_VER_33:
3580 case RTL_GIGA_MAC_VER_34:
3581 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3582 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3589 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3591 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3594 rtl_writephy(tp, 0x1f, 0x0000);
3595 rtl_writephy(tp, MII_BMCR, 0x0000);
3597 rtl_wol_suspend_quirk(tp);
3602 static void r810x_phy_power_down(struct rtl8169_private *tp)
3604 rtl_writephy(tp, 0x1f, 0x0000);
3605 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3608 static void r810x_phy_power_up(struct rtl8169_private *tp)
3610 rtl_writephy(tp, 0x1f, 0x0000);
3611 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3614 static void r810x_pll_power_down(struct rtl8169_private *tp)
3616 if (rtl_wol_pll_power_down(tp))
3619 r810x_phy_power_down(tp);
3622 static void r810x_pll_power_up(struct rtl8169_private *tp)
3624 r810x_phy_power_up(tp);
3627 static void r8168_phy_power_up(struct rtl8169_private *tp)
3629 rtl_writephy(tp, 0x1f, 0x0000);
3630 switch (tp->mac_version) {
3631 case RTL_GIGA_MAC_VER_11:
3632 case RTL_GIGA_MAC_VER_12:
3633 case RTL_GIGA_MAC_VER_17:
3634 case RTL_GIGA_MAC_VER_18:
3635 case RTL_GIGA_MAC_VER_19:
3636 case RTL_GIGA_MAC_VER_20:
3637 case RTL_GIGA_MAC_VER_21:
3638 case RTL_GIGA_MAC_VER_22:
3639 case RTL_GIGA_MAC_VER_23:
3640 case RTL_GIGA_MAC_VER_24:
3641 case RTL_GIGA_MAC_VER_25:
3642 case RTL_GIGA_MAC_VER_26:
3643 case RTL_GIGA_MAC_VER_27:
3644 case RTL_GIGA_MAC_VER_28:
3645 case RTL_GIGA_MAC_VER_31:
3646 rtl_writephy(tp, 0x0e, 0x0000);
3651 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3654 static void r8168_phy_power_down(struct rtl8169_private *tp)
3656 rtl_writephy(tp, 0x1f, 0x0000);
3657 switch (tp->mac_version) {
3658 case RTL_GIGA_MAC_VER_32:
3659 case RTL_GIGA_MAC_VER_33:
3660 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3663 case RTL_GIGA_MAC_VER_11:
3664 case RTL_GIGA_MAC_VER_12:
3665 case RTL_GIGA_MAC_VER_17:
3666 case RTL_GIGA_MAC_VER_18:
3667 case RTL_GIGA_MAC_VER_19:
3668 case RTL_GIGA_MAC_VER_20:
3669 case RTL_GIGA_MAC_VER_21:
3670 case RTL_GIGA_MAC_VER_22:
3671 case RTL_GIGA_MAC_VER_23:
3672 case RTL_GIGA_MAC_VER_24:
3673 case RTL_GIGA_MAC_VER_25:
3674 case RTL_GIGA_MAC_VER_26:
3675 case RTL_GIGA_MAC_VER_27:
3676 case RTL_GIGA_MAC_VER_28:
3677 case RTL_GIGA_MAC_VER_31:
3678 rtl_writephy(tp, 0x0e, 0x0200);
3680 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3685 static void r8168_pll_power_down(struct rtl8169_private *tp)
3687 void __iomem *ioaddr = tp->mmio_addr;
3689 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3690 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3691 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3692 r8168dp_check_dash(tp)) {
3696 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3697 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
3698 (RTL_R16(CPlusCmd) & ASF)) {
3702 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3703 tp->mac_version == RTL_GIGA_MAC_VER_33)
3704 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3706 if (rtl_wol_pll_power_down(tp))
3709 r8168_phy_power_down(tp);
3711 switch (tp->mac_version) {
3712 case RTL_GIGA_MAC_VER_25:
3713 case RTL_GIGA_MAC_VER_26:
3714 case RTL_GIGA_MAC_VER_27:
3715 case RTL_GIGA_MAC_VER_28:
3716 case RTL_GIGA_MAC_VER_31:
3717 case RTL_GIGA_MAC_VER_32:
3718 case RTL_GIGA_MAC_VER_33:
3719 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3724 static void r8168_pll_power_up(struct rtl8169_private *tp)
3726 void __iomem *ioaddr = tp->mmio_addr;
3728 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3729 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3730 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3731 r8168dp_check_dash(tp)) {
3735 switch (tp->mac_version) {
3736 case RTL_GIGA_MAC_VER_25:
3737 case RTL_GIGA_MAC_VER_26:
3738 case RTL_GIGA_MAC_VER_27:
3739 case RTL_GIGA_MAC_VER_28:
3740 case RTL_GIGA_MAC_VER_31:
3741 case RTL_GIGA_MAC_VER_32:
3742 case RTL_GIGA_MAC_VER_33:
3743 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3747 r8168_phy_power_up(tp);
3750 static void rtl_generic_op(struct rtl8169_private *tp,
3751 void (*op)(struct rtl8169_private *))
3757 static void rtl_pll_power_down(struct rtl8169_private *tp)
3759 rtl_generic_op(tp, tp->pll_power_ops.down);
3762 static void rtl_pll_power_up(struct rtl8169_private *tp)
3764 rtl_generic_op(tp, tp->pll_power_ops.up);
3767 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3769 struct pll_power_ops *ops = &tp->pll_power_ops;
3771 switch (tp->mac_version) {
3772 case RTL_GIGA_MAC_VER_07:
3773 case RTL_GIGA_MAC_VER_08:
3774 case RTL_GIGA_MAC_VER_09:
3775 case RTL_GIGA_MAC_VER_10:
3776 case RTL_GIGA_MAC_VER_16:
3777 case RTL_GIGA_MAC_VER_29:
3778 case RTL_GIGA_MAC_VER_30:
3779 ops->down = r810x_pll_power_down;
3780 ops->up = r810x_pll_power_up;
3783 case RTL_GIGA_MAC_VER_11:
3784 case RTL_GIGA_MAC_VER_12:
3785 case RTL_GIGA_MAC_VER_17:
3786 case RTL_GIGA_MAC_VER_18:
3787 case RTL_GIGA_MAC_VER_19:
3788 case RTL_GIGA_MAC_VER_20:
3789 case RTL_GIGA_MAC_VER_21:
3790 case RTL_GIGA_MAC_VER_22:
3791 case RTL_GIGA_MAC_VER_23:
3792 case RTL_GIGA_MAC_VER_24:
3793 case RTL_GIGA_MAC_VER_25:
3794 case RTL_GIGA_MAC_VER_26:
3795 case RTL_GIGA_MAC_VER_27:
3796 case RTL_GIGA_MAC_VER_28:
3797 case RTL_GIGA_MAC_VER_31:
3798 case RTL_GIGA_MAC_VER_32:
3799 case RTL_GIGA_MAC_VER_33:
3800 case RTL_GIGA_MAC_VER_34:
3801 case RTL_GIGA_MAC_VER_35:
3802 case RTL_GIGA_MAC_VER_36:
3803 ops->down = r8168_pll_power_down;
3804 ops->up = r8168_pll_power_up;
3814 static void rtl_init_rxcfg(struct rtl8169_private *tp)
3816 void __iomem *ioaddr = tp->mmio_addr;
3818 switch (tp->mac_version) {
3819 case RTL_GIGA_MAC_VER_01:
3820 case RTL_GIGA_MAC_VER_02:
3821 case RTL_GIGA_MAC_VER_03:
3822 case RTL_GIGA_MAC_VER_04:
3823 case RTL_GIGA_MAC_VER_05:
3824 case RTL_GIGA_MAC_VER_06:
3825 case RTL_GIGA_MAC_VER_10:
3826 case RTL_GIGA_MAC_VER_11:
3827 case RTL_GIGA_MAC_VER_12:
3828 case RTL_GIGA_MAC_VER_13:
3829 case RTL_GIGA_MAC_VER_14:
3830 case RTL_GIGA_MAC_VER_15:
3831 case RTL_GIGA_MAC_VER_16:
3832 case RTL_GIGA_MAC_VER_17:
3833 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3835 case RTL_GIGA_MAC_VER_18:
3836 case RTL_GIGA_MAC_VER_19:
3837 case RTL_GIGA_MAC_VER_20:
3838 case RTL_GIGA_MAC_VER_21:
3839 case RTL_GIGA_MAC_VER_22:
3840 case RTL_GIGA_MAC_VER_23:
3841 case RTL_GIGA_MAC_VER_24:
3842 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3845 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3850 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3852 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3855 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3857 void __iomem *ioaddr = tp->mmio_addr;
3859 RTL_W8(Cfg9346, Cfg9346_Unlock);
3860 rtl_generic_op(tp, tp->jumbo_ops.enable);
3861 RTL_W8(Cfg9346, Cfg9346_Lock);
3864 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3866 void __iomem *ioaddr = tp->mmio_addr;
3868 RTL_W8(Cfg9346, Cfg9346_Unlock);
3869 rtl_generic_op(tp, tp->jumbo_ops.disable);
3870 RTL_W8(Cfg9346, Cfg9346_Lock);
3873 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3875 void __iomem *ioaddr = tp->mmio_addr;
3877 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3878 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3879 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3882 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3884 void __iomem *ioaddr = tp->mmio_addr;
3886 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3887 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3888 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3891 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3893 void __iomem *ioaddr = tp->mmio_addr;
3895 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3898 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3900 void __iomem *ioaddr = tp->mmio_addr;
3902 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3905 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3907 void __iomem *ioaddr = tp->mmio_addr;
3909 RTL_W8(MaxTxPacketSize, 0x3f);
3910 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3911 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
3912 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3915 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3917 void __iomem *ioaddr = tp->mmio_addr;
3919 RTL_W8(MaxTxPacketSize, 0x0c);
3920 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3921 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
3922 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3925 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3927 rtl_tx_performance_tweak(tp->pci_dev,
3928 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3931 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3933 rtl_tx_performance_tweak(tp->pci_dev,
3934 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3937 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3939 void __iomem *ioaddr = tp->mmio_addr;
3941 r8168b_0_hw_jumbo_enable(tp);
3943 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3946 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3948 void __iomem *ioaddr = tp->mmio_addr;
3950 r8168b_0_hw_jumbo_disable(tp);
3952 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3955 static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3957 struct jumbo_ops *ops = &tp->jumbo_ops;
3959 switch (tp->mac_version) {
3960 case RTL_GIGA_MAC_VER_11:
3961 ops->disable = r8168b_0_hw_jumbo_disable;
3962 ops->enable = r8168b_0_hw_jumbo_enable;
3964 case RTL_GIGA_MAC_VER_12:
3965 case RTL_GIGA_MAC_VER_17:
3966 ops->disable = r8168b_1_hw_jumbo_disable;
3967 ops->enable = r8168b_1_hw_jumbo_enable;
3969 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3970 case RTL_GIGA_MAC_VER_19:
3971 case RTL_GIGA_MAC_VER_20:
3972 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3973 case RTL_GIGA_MAC_VER_22:
3974 case RTL_GIGA_MAC_VER_23:
3975 case RTL_GIGA_MAC_VER_24:
3976 case RTL_GIGA_MAC_VER_25:
3977 case RTL_GIGA_MAC_VER_26:
3978 ops->disable = r8168c_hw_jumbo_disable;
3979 ops->enable = r8168c_hw_jumbo_enable;
3981 case RTL_GIGA_MAC_VER_27:
3982 case RTL_GIGA_MAC_VER_28:
3983 ops->disable = r8168dp_hw_jumbo_disable;
3984 ops->enable = r8168dp_hw_jumbo_enable;
3986 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3987 case RTL_GIGA_MAC_VER_32:
3988 case RTL_GIGA_MAC_VER_33:
3989 case RTL_GIGA_MAC_VER_34:
3990 ops->disable = r8168e_hw_jumbo_disable;
3991 ops->enable = r8168e_hw_jumbo_enable;
3995 * No action needed for jumbo frames with 8169.
3996 * No jumbo for 810x at all.
3999 ops->disable = NULL;
4005 static void rtl_hw_reset(struct rtl8169_private *tp)
4007 void __iomem *ioaddr = tp->mmio_addr;
4010 /* Soft reset the chip. */
4011 RTL_W8(ChipCmd, CmdReset);
4013 /* Check that the chip has finished the reset. */
4014 for (i = 0; i < 100; i++) {
4015 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4021 static int __devinit
4022 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4024 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
4025 const unsigned int region = cfg->region;
4026 struct rtl8169_private *tp;
4027 struct mii_if_info *mii;
4028 struct net_device *dev;
4029 void __iomem *ioaddr;
4033 if (netif_msg_drv(&debug)) {
4034 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
4035 MODULENAME, RTL8169_VERSION);
4038 dev = alloc_etherdev(sizeof (*tp));
4044 SET_NETDEV_DEV(dev, &pdev->dev);
4045 dev->netdev_ops = &rtl8169_netdev_ops;
4046 tp = netdev_priv(dev);
4049 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
4053 mii->mdio_read = rtl_mdio_read;
4054 mii->mdio_write = rtl_mdio_write;
4055 mii->phy_id_mask = 0x1f;
4056 mii->reg_num_mask = 0x1f;
4057 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
4059 /* disable ASPM completely as that cause random device stop working
4060 * problems as well as full system hangs for some PCIe devices users */
4061 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4062 PCIE_LINK_STATE_CLKPM);
4064 /* enable device (incl. PCI PM wakeup and hotplug setup) */
4065 rc = pci_enable_device(pdev);
4067 netif_err(tp, probe, dev, "enable failure\n");
4068 goto err_out_free_dev_1;
4071 if (pci_set_mwi(pdev) < 0)
4072 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
4074 /* make sure PCI base addr 1 is MMIO */
4075 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
4076 netif_err(tp, probe, dev,
4077 "region #%d not an MMIO resource, aborting\n",
4083 /* check for weird/broken PCI region reporting */
4084 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
4085 netif_err(tp, probe, dev,
4086 "Invalid PCI region size(s), aborting\n");
4091 rc = pci_request_regions(pdev, MODULENAME);
4093 netif_err(tp, probe, dev, "could not request regions\n");
4097 tp->cp_cmd = RxChkSum;
4099 if ((sizeof(dma_addr_t) > 4) &&
4100 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
4101 tp->cp_cmd |= PCIDAC;
4102 dev->features |= NETIF_F_HIGHDMA;
4104 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4106 netif_err(tp, probe, dev, "DMA configuration failed\n");
4107 goto err_out_free_res_3;
4111 /* ioremap MMIO region */
4112 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
4114 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
4116 goto err_out_free_res_3;
4118 tp->mmio_addr = ioaddr;
4120 if (!pci_is_pcie(pdev))
4121 netif_info(tp, probe, dev, "not PCI Express\n");
4123 /* Identify chip attached to board */
4124 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4128 rtl_irq_disable(tp);
4132 rtl_ack_events(tp, 0xffff);
4134 pci_set_master(pdev);
4137 * Pretend we are using VLANs; This bypasses a nasty bug where
4138 * Interrupts stop flowing on high load on 8110SCd controllers.
4140 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4141 tp->cp_cmd |= RxVlan;
4143 rtl_init_mdio_ops(tp);
4144 rtl_init_pll_power_ops(tp);
4145 rtl_init_jumbo_ops(tp);
4147 rtl8169_print_mac_version(tp);
4149 chipset = tp->mac_version;
4150 tp->txd_version = rtl_chip_infos[chipset].txd_version;
4152 RTL_W8(Cfg9346, Cfg9346_Unlock);
4153 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4154 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
4155 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4156 tp->features |= RTL_FEATURE_WOL;
4157 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4158 tp->features |= RTL_FEATURE_WOL;
4159 tp->features |= rtl_try_msi(tp, cfg);
4160 RTL_W8(Cfg9346, Cfg9346_Lock);
4162 if (rtl_tbi_enabled(tp)) {
4163 tp->set_speed = rtl8169_set_speed_tbi;
4164 tp->get_settings = rtl8169_gset_tbi;
4165 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4166 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4167 tp->link_ok = rtl8169_tbi_link_ok;
4168 tp->do_ioctl = rtl_tbi_ioctl;
4170 tp->set_speed = rtl8169_set_speed_xmii;
4171 tp->get_settings = rtl8169_gset_xmii;
4172 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4173 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4174 tp->link_ok = rtl8169_xmii_link_ok;
4175 tp->do_ioctl = rtl_xmii_ioctl;
4178 mutex_init(&tp->wk.mutex);
4180 /* Get MAC address */
4181 for (i = 0; i < ETH_ALEN; i++)
4182 dev->dev_addr[i] = RTL_R8(MAC0 + i);
4183 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
4185 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
4186 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4187 dev->irq = pdev->irq;
4188 dev->base_addr = (unsigned long) ioaddr;
4190 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
4192 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4193 * properly for all devices */
4194 dev->features |= NETIF_F_RXCSUM |
4195 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4197 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4198 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4199 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4202 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4203 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4204 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
4206 dev->hw_features |= NETIF_F_RXALL;
4207 dev->hw_features |= NETIF_F_RXFCS;
4209 tp->hw_start = cfg->hw_start;
4210 tp->event_slow = cfg->event_slow;
4212 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4213 ~(RxBOVF | RxFOVF) : ~0;
4215 init_timer(&tp->timer);
4216 tp->timer.data = (unsigned long) dev;
4217 tp->timer.function = rtl8169_phy_timer;
4219 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
4221 rc = register_netdev(dev);
4225 pci_set_drvdata(pdev, dev);
4227 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
4228 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
4229 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
4230 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4231 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4232 "tx checksumming: %s]\n",
4233 rtl_chip_infos[chipset].jumbo_max,
4234 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4237 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4238 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4239 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4240 rtl8168_driver_start(tp);
4243 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
4245 if (pci_dev_run_wake(pdev))
4246 pm_runtime_put_noidle(&pdev->dev);
4248 netif_carrier_off(dev);
4254 rtl_disable_msi(pdev, tp);
4257 pci_release_regions(pdev);
4259 pci_clear_mwi(pdev);
4260 pci_disable_device(pdev);
4266 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
4268 struct net_device *dev = pci_get_drvdata(pdev);
4269 struct rtl8169_private *tp = netdev_priv(dev);
4271 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4272 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4273 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4274 rtl8168_driver_stop(tp);
4277 cancel_work_sync(&tp->wk.work);
4279 unregister_netdev(dev);
4281 rtl_release_firmware(tp);
4283 if (pci_dev_run_wake(pdev))
4284 pm_runtime_get_noresume(&pdev->dev);
4286 /* restore original MAC address */
4287 rtl_rar_set(tp, dev->perm_addr);
4289 rtl_disable_msi(pdev, tp);
4290 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4291 pci_set_drvdata(pdev, NULL);
4294 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4296 struct rtl_fw *rtl_fw;
4300 name = rtl_lookup_firmware_name(tp);
4302 goto out_no_firmware;
4304 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4308 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4312 rc = rtl_check_firmware(tp, rtl_fw);
4314 goto err_release_firmware;
4316 tp->rtl_fw = rtl_fw;
4320 err_release_firmware:
4321 release_firmware(rtl_fw->fw);
4325 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4332 static void rtl_request_firmware(struct rtl8169_private *tp)
4334 if (IS_ERR(tp->rtl_fw))
4335 rtl_request_uncached_firmware(tp);
4338 static void rtl_task(struct work_struct *);
4340 static int rtl8169_open(struct net_device *dev)
4342 struct rtl8169_private *tp = netdev_priv(dev);
4343 void __iomem *ioaddr = tp->mmio_addr;
4344 struct pci_dev *pdev = tp->pci_dev;
4345 int retval = -ENOMEM;
4347 pm_runtime_get_sync(&pdev->dev);
4350 * Rx and Tx desscriptors needs 256 bytes alignment.
4351 * dma_alloc_coherent provides more.
4353 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4354 &tp->TxPhyAddr, GFP_KERNEL);
4355 if (!tp->TxDescArray)
4356 goto err_pm_runtime_put;
4358 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4359 &tp->RxPhyAddr, GFP_KERNEL);
4360 if (!tp->RxDescArray)
4363 retval = rtl8169_init_ring(dev);
4367 INIT_WORK(&tp->wk.work, rtl_task);
4371 rtl_request_firmware(tp);
4373 retval = request_irq(dev->irq, rtl8169_interrupt,
4374 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
4377 goto err_release_fw_2;
4381 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
4383 napi_enable(&tp->napi);
4385 rtl8169_init_phy(dev, tp);
4387 __rtl8169_set_features(dev, dev->features);
4389 rtl_pll_power_up(tp);
4393 netif_start_queue(dev);
4395 rtl_unlock_work(tp);
4397 tp->saved_wolopts = 0;
4398 pm_runtime_put_noidle(&pdev->dev);
4400 rtl8169_check_link_status(dev, tp, ioaddr);
4405 rtl_release_firmware(tp);
4406 rtl8169_rx_clear(tp);
4408 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4410 tp->RxDescArray = NULL;
4412 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4414 tp->TxDescArray = NULL;
4416 pm_runtime_put_noidle(&pdev->dev);
4420 static void rtl_rx_close(struct rtl8169_private *tp)
4422 void __iomem *ioaddr = tp->mmio_addr;
4424 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4427 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4429 void __iomem *ioaddr = tp->mmio_addr;
4431 /* Disable interrupts */
4432 rtl8169_irq_mask_and_ack(tp);
4436 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4437 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4438 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4439 while (RTL_R8(TxPoll) & NPQ)
4441 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4442 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4443 tp->mac_version == RTL_GIGA_MAC_VER_36) {
4444 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4445 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4448 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4455 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4457 void __iomem *ioaddr = tp->mmio_addr;
4459 /* Set DMA burst size and Interframe Gap Time */
4460 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4461 (InterFrameGap << TxInterFrameGapShift));
4464 static void rtl_hw_start(struct net_device *dev)
4466 struct rtl8169_private *tp = netdev_priv(dev);
4470 rtl_irq_enable_all(tp);
4473 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4474 void __iomem *ioaddr)
4477 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4478 * register to be written before TxDescAddrLow to work.
4479 * Switching from MMIO to I/O access fixes the issue as well.
4481 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4482 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4483 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4484 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4487 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4491 cmd = RTL_R16(CPlusCmd);
4492 RTL_W16(CPlusCmd, cmd);
4496 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
4498 /* Low hurts. Let's disable the filtering. */
4499 RTL_W16(RxMaxSize, rx_buf_sz + 1);
4502 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4504 static const struct rtl_cfg2_info {
4509 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4510 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4511 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4512 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4514 const struct rtl_cfg2_info *p = cfg2_info;
4518 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
4519 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4520 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4521 RTL_W32(0x7c, p->val);
4527 static void rtl_hw_start_8169(struct net_device *dev)
4529 struct rtl8169_private *tp = netdev_priv(dev);
4530 void __iomem *ioaddr = tp->mmio_addr;
4531 struct pci_dev *pdev = tp->pci_dev;
4533 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4534 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4535 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4538 RTL_W8(Cfg9346, Cfg9346_Unlock);
4539 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4540 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4541 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4542 tp->mac_version == RTL_GIGA_MAC_VER_04)
4543 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4547 RTL_W8(EarlyTxThres, NoEarlyTx);
4549 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4551 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4552 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4553 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4554 tp->mac_version == RTL_GIGA_MAC_VER_04)
4555 rtl_set_rx_tx_config_registers(tp);
4557 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4559 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4560 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4561 dprintk("Set MAC Reg C+CR Offset 0xE0. "
4562 "Bit-3 and bit-14 MUST be 1\n");
4563 tp->cp_cmd |= (1 << 14);
4566 RTL_W16(CPlusCmd, tp->cp_cmd);
4568 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4571 * Undocumented corner. Supposedly:
4572 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4574 RTL_W16(IntrMitigate, 0x0000);
4576 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4578 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4579 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4580 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4581 tp->mac_version != RTL_GIGA_MAC_VER_04) {
4582 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4583 rtl_set_rx_tx_config_registers(tp);
4586 RTL_W8(Cfg9346, Cfg9346_Lock);
4588 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4591 RTL_W32(RxMissed, 0);
4593 rtl_set_rx_mode(dev);
4595 /* no early-rx interrupts */
4596 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4599 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
4603 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
4604 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4607 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4609 rtl_csi_access_enable(ioaddr, 0x17000000);
4612 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4614 rtl_csi_access_enable(ioaddr, 0x27000000);
4618 unsigned int offset;
4623 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
4628 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4629 rtl_ephy_write(ioaddr, e->offset, w);
4634 static void rtl_disable_clock_request(struct pci_dev *pdev)
4636 int cap = pci_pcie_cap(pdev);
4641 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4642 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4643 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4647 static void rtl_enable_clock_request(struct pci_dev *pdev)
4649 int cap = pci_pcie_cap(pdev);
4654 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4655 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4656 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4660 #define R8168_CPCMD_QUIRK_MASK (\
4671 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4673 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4675 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4677 rtl_tx_performance_tweak(pdev,
4678 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4681 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4683 rtl_hw_start_8168bb(ioaddr, pdev);
4685 RTL_W8(MaxTxPacketSize, TxPacketMax);
4687 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4690 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4692 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4694 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4696 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4698 rtl_disable_clock_request(pdev);
4700 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4703 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
4705 static const struct ephy_info e_info_8168cp[] = {
4706 { 0x01, 0, 0x0001 },
4707 { 0x02, 0x0800, 0x1000 },
4708 { 0x03, 0, 0x0042 },
4709 { 0x06, 0x0080, 0x0000 },
4713 rtl_csi_access_enable_2(ioaddr);
4715 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4717 __rtl_hw_start_8168cp(ioaddr, pdev);
4720 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4722 rtl_csi_access_enable_2(ioaddr);
4724 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4726 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4728 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4731 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4733 rtl_csi_access_enable_2(ioaddr);
4735 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4738 RTL_W8(DBG_REG, 0x20);
4740 RTL_W8(MaxTxPacketSize, TxPacketMax);
4742 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4744 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4747 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4749 static const struct ephy_info e_info_8168c_1[] = {
4750 { 0x02, 0x0800, 0x1000 },
4751 { 0x03, 0, 0x0002 },
4752 { 0x06, 0x0080, 0x0000 }
4755 rtl_csi_access_enable_2(ioaddr);
4757 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4759 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4761 __rtl_hw_start_8168cp(ioaddr, pdev);
4764 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4766 static const struct ephy_info e_info_8168c_2[] = {
4767 { 0x01, 0, 0x0001 },
4768 { 0x03, 0x0400, 0x0220 }
4771 rtl_csi_access_enable_2(ioaddr);
4773 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4775 __rtl_hw_start_8168cp(ioaddr, pdev);
4778 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4780 rtl_hw_start_8168c_2(ioaddr, pdev);
4783 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4785 rtl_csi_access_enable_2(ioaddr);
4787 __rtl_hw_start_8168cp(ioaddr, pdev);
4790 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4792 rtl_csi_access_enable_2(ioaddr);
4794 rtl_disable_clock_request(pdev);
4796 RTL_W8(MaxTxPacketSize, TxPacketMax);
4798 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4800 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4803 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4805 rtl_csi_access_enable_1(ioaddr);
4807 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4809 RTL_W8(MaxTxPacketSize, TxPacketMax);
4811 rtl_disable_clock_request(pdev);
4814 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4816 static const struct ephy_info e_info_8168d_4[] = {
4818 { 0x19, 0x20, 0x50 },
4823 rtl_csi_access_enable_1(ioaddr);
4825 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4827 RTL_W8(MaxTxPacketSize, TxPacketMax);
4829 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4830 const struct ephy_info *e = e_info_8168d_4 + i;
4833 w = rtl_ephy_read(ioaddr, e->offset);
4834 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4837 rtl_enable_clock_request(pdev);
4840 static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4842 static const struct ephy_info e_info_8168e_1[] = {
4843 { 0x00, 0x0200, 0x0100 },
4844 { 0x00, 0x0000, 0x0004 },
4845 { 0x06, 0x0002, 0x0001 },
4846 { 0x06, 0x0000, 0x0030 },
4847 { 0x07, 0x0000, 0x2000 },
4848 { 0x00, 0x0000, 0x0020 },
4849 { 0x03, 0x5800, 0x2000 },
4850 { 0x03, 0x0000, 0x0001 },
4851 { 0x01, 0x0800, 0x1000 },
4852 { 0x07, 0x0000, 0x4000 },
4853 { 0x1e, 0x0000, 0x2000 },
4854 { 0x19, 0xffff, 0xfe6c },
4855 { 0x0a, 0x0000, 0x0040 }
4858 rtl_csi_access_enable_2(ioaddr);
4860 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
4862 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4864 RTL_W8(MaxTxPacketSize, TxPacketMax);
4866 rtl_disable_clock_request(pdev);
4868 /* Reset tx FIFO pointer */
4869 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4870 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
4872 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4875 static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4877 static const struct ephy_info e_info_8168e_2[] = {
4878 { 0x09, 0x0000, 0x0080 },
4879 { 0x19, 0x0000, 0x0224 }
4882 rtl_csi_access_enable_1(ioaddr);
4884 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4886 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4888 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4889 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4890 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4891 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4892 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4893 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4894 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4895 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4898 RTL_W8(MaxTxPacketSize, EarlySize);
4900 rtl_disable_clock_request(pdev);
4902 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4903 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4905 /* Adjust EEE LED frequency */
4906 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4908 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4909 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4910 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4913 static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4915 static const struct ephy_info e_info_8168f_1[] = {
4916 { 0x06, 0x00c0, 0x0020 },
4917 { 0x08, 0x0001, 0x0002 },
4918 { 0x09, 0x0000, 0x0080 },
4919 { 0x19, 0x0000, 0x0224 }
4922 rtl_csi_access_enable_1(ioaddr);
4924 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4926 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4928 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4929 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4930 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4931 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4932 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4933 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4934 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4935 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4936 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4937 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4938 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4941 RTL_W8(MaxTxPacketSize, EarlySize);
4943 rtl_disable_clock_request(pdev);
4945 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4946 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4948 /* Adjust EEE LED frequency */
4949 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4951 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4952 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4953 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4956 static void rtl_hw_start_8168(struct net_device *dev)
4958 struct rtl8169_private *tp = netdev_priv(dev);
4959 void __iomem *ioaddr = tp->mmio_addr;
4960 struct pci_dev *pdev = tp->pci_dev;
4962 RTL_W8(Cfg9346, Cfg9346_Unlock);
4964 RTL_W8(MaxTxPacketSize, TxPacketMax);
4966 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4968 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
4970 RTL_W16(CPlusCmd, tp->cp_cmd);
4972 RTL_W16(IntrMitigate, 0x5151);
4974 /* Work around for RxFIFO overflow. */
4975 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
4976 tp->event_slow |= RxFIFOOver | PCSTimeout;
4977 tp->event_slow &= ~RxOverflow;
4980 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4982 rtl_set_rx_mode(dev);
4984 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4985 (InterFrameGap << TxInterFrameGapShift));
4989 switch (tp->mac_version) {
4990 case RTL_GIGA_MAC_VER_11:
4991 rtl_hw_start_8168bb(ioaddr, pdev);
4994 case RTL_GIGA_MAC_VER_12:
4995 case RTL_GIGA_MAC_VER_17:
4996 rtl_hw_start_8168bef(ioaddr, pdev);
4999 case RTL_GIGA_MAC_VER_18:
5000 rtl_hw_start_8168cp_1(ioaddr, pdev);
5003 case RTL_GIGA_MAC_VER_19:
5004 rtl_hw_start_8168c_1(ioaddr, pdev);
5007 case RTL_GIGA_MAC_VER_20:
5008 rtl_hw_start_8168c_2(ioaddr, pdev);
5011 case RTL_GIGA_MAC_VER_21:
5012 rtl_hw_start_8168c_3(ioaddr, pdev);
5015 case RTL_GIGA_MAC_VER_22:
5016 rtl_hw_start_8168c_4(ioaddr, pdev);
5019 case RTL_GIGA_MAC_VER_23:
5020 rtl_hw_start_8168cp_2(ioaddr, pdev);
5023 case RTL_GIGA_MAC_VER_24:
5024 rtl_hw_start_8168cp_3(ioaddr, pdev);
5027 case RTL_GIGA_MAC_VER_25:
5028 case RTL_GIGA_MAC_VER_26:
5029 case RTL_GIGA_MAC_VER_27:
5030 rtl_hw_start_8168d(ioaddr, pdev);
5033 case RTL_GIGA_MAC_VER_28:
5034 rtl_hw_start_8168d_4(ioaddr, pdev);
5037 case RTL_GIGA_MAC_VER_31:
5038 rtl_hw_start_8168dp(ioaddr, pdev);
5041 case RTL_GIGA_MAC_VER_32:
5042 case RTL_GIGA_MAC_VER_33:
5043 rtl_hw_start_8168e_1(ioaddr, pdev);
5045 case RTL_GIGA_MAC_VER_34:
5046 rtl_hw_start_8168e_2(ioaddr, pdev);
5049 case RTL_GIGA_MAC_VER_35:
5050 case RTL_GIGA_MAC_VER_36:
5051 rtl_hw_start_8168f_1(ioaddr, pdev);
5055 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5056 dev->name, tp->mac_version);
5060 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5062 RTL_W8(Cfg9346, Cfg9346_Lock);
5064 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
5067 #define R810X_CPCMD_QUIRK_MASK (\
5078 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5080 static const struct ephy_info e_info_8102e_1[] = {
5081 { 0x01, 0, 0x6e65 },
5082 { 0x02, 0, 0x091f },
5083 { 0x03, 0, 0xc2f9 },
5084 { 0x06, 0, 0xafb5 },
5085 { 0x07, 0, 0x0e00 },
5086 { 0x19, 0, 0xec80 },
5087 { 0x01, 0, 0x2e65 },
5092 rtl_csi_access_enable_2(ioaddr);
5094 RTL_W8(DBG_REG, FIX_NAK_1);
5096 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5099 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5100 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5102 cfg1 = RTL_R8(Config1);
5103 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5104 RTL_W8(Config1, cfg1 & ~LEDS0);
5106 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5109 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5111 rtl_csi_access_enable_2(ioaddr);
5113 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5115 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5116 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5119 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5121 rtl_hw_start_8102e_2(ioaddr, pdev);
5123 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5126 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5128 static const struct ephy_info e_info_8105e_1[] = {
5129 { 0x07, 0, 0x4000 },
5130 { 0x19, 0, 0x0200 },
5131 { 0x19, 0, 0x0020 },
5132 { 0x1e, 0, 0x2000 },
5133 { 0x03, 0, 0x0001 },
5134 { 0x19, 0, 0x0100 },
5135 { 0x19, 0, 0x0004 },
5139 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5140 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5142 /* Disable Early Tally Counter */
5143 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5145 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5146 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5148 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5151 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5153 rtl_hw_start_8105e_1(ioaddr, pdev);
5154 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5157 static void rtl_hw_start_8101(struct net_device *dev)
5159 struct rtl8169_private *tp = netdev_priv(dev);
5160 void __iomem *ioaddr = tp->mmio_addr;
5161 struct pci_dev *pdev = tp->pci_dev;
5163 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5164 tp->event_slow &= ~RxFIFOOver;
5166 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5167 tp->mac_version == RTL_GIGA_MAC_VER_16) {
5168 int cap = pci_pcie_cap(pdev);
5171 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5172 PCI_EXP_DEVCTL_NOSNOOP_EN);
5176 RTL_W8(Cfg9346, Cfg9346_Unlock);
5178 switch (tp->mac_version) {
5179 case RTL_GIGA_MAC_VER_07:
5180 rtl_hw_start_8102e_1(ioaddr, pdev);
5183 case RTL_GIGA_MAC_VER_08:
5184 rtl_hw_start_8102e_3(ioaddr, pdev);
5187 case RTL_GIGA_MAC_VER_09:
5188 rtl_hw_start_8102e_2(ioaddr, pdev);
5191 case RTL_GIGA_MAC_VER_29:
5192 rtl_hw_start_8105e_1(ioaddr, pdev);
5194 case RTL_GIGA_MAC_VER_30:
5195 rtl_hw_start_8105e_2(ioaddr, pdev);
5199 RTL_W8(Cfg9346, Cfg9346_Lock);
5201 RTL_W8(MaxTxPacketSize, TxPacketMax);
5203 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5205 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5206 RTL_W16(CPlusCmd, tp->cp_cmd);
5208 RTL_W16(IntrMitigate, 0x0000);
5210 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5212 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5213 rtl_set_rx_tx_config_registers(tp);
5217 rtl_set_rx_mode(dev);
5219 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5222 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5224 struct rtl8169_private *tp = netdev_priv(dev);
5226 if (new_mtu < ETH_ZLEN ||
5227 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
5230 if (new_mtu > ETH_DATA_LEN)
5231 rtl_hw_jumbo_enable(tp);
5233 rtl_hw_jumbo_disable(tp);
5236 netdev_update_features(dev);
5241 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5243 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5244 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5247 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5248 void **data_buff, struct RxDesc *desc)
5250 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
5255 rtl8169_make_unusable_by_asic(desc);
5258 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5260 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5262 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5265 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5268 desc->addr = cpu_to_le64(mapping);
5270 rtl8169_mark_to_asic(desc, rx_buf_sz);
5273 static inline void *rtl8169_align(void *data)
5275 return (void *)ALIGN((long)data, 16);
5278 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5279 struct RxDesc *desc)
5283 struct device *d = &tp->pci_dev->dev;
5284 struct net_device *dev = tp->dev;
5285 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
5287 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5291 if (rtl8169_align(data) != data) {
5293 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5298 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
5300 if (unlikely(dma_mapping_error(d, mapping))) {
5301 if (net_ratelimit())
5302 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5306 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
5314 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5318 for (i = 0; i < NUM_RX_DESC; i++) {
5319 if (tp->Rx_databuff[i]) {
5320 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5321 tp->RxDescArray + i);
5326 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5328 desc->opts1 |= cpu_to_le32(RingEnd);
5331 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5335 for (i = 0; i < NUM_RX_DESC; i++) {
5338 if (tp->Rx_databuff[i])
5341 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5343 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5346 tp->Rx_databuff[i] = data;
5349 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5353 rtl8169_rx_clear(tp);
5357 static int rtl8169_init_ring(struct net_device *dev)
5359 struct rtl8169_private *tp = netdev_priv(dev);
5361 rtl8169_init_ring_indexes(tp);
5363 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
5364 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
5366 return rtl8169_rx_fill(tp);
5369 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5370 struct TxDesc *desc)
5372 unsigned int len = tx_skb->len;
5374 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5382 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5387 for (i = 0; i < n; i++) {
5388 unsigned int entry = (start + i) % NUM_TX_DESC;
5389 struct ring_info *tx_skb = tp->tx_skb + entry;
5390 unsigned int len = tx_skb->len;
5393 struct sk_buff *skb = tx_skb->skb;
5395 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5396 tp->TxDescArray + entry);
5398 tp->dev->stats.tx_dropped++;
5406 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5408 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5409 tp->cur_tx = tp->dirty_tx = 0;
5410 netdev_reset_queue(tp->dev);
5413 static void rtl_reset_work(struct rtl8169_private *tp)
5415 struct net_device *dev = tp->dev;
5418 napi_disable(&tp->napi);
5419 netif_stop_queue(dev);
5420 synchronize_sched();
5422 rtl8169_hw_reset(tp);
5424 for (i = 0; i < NUM_RX_DESC; i++)
5425 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5427 rtl8169_tx_clear(tp);
5428 rtl8169_init_ring_indexes(tp);
5430 napi_enable(&tp->napi);
5432 netif_wake_queue(dev);
5433 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
5436 static void rtl8169_tx_timeout(struct net_device *dev)
5438 struct rtl8169_private *tp = netdev_priv(dev);
5440 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5443 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5446 struct skb_shared_info *info = skb_shinfo(skb);
5447 unsigned int cur_frag, entry;
5448 struct TxDesc * uninitialized_var(txd);
5449 struct device *d = &tp->pci_dev->dev;
5452 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5453 const skb_frag_t *frag = info->frags + cur_frag;
5458 entry = (entry + 1) % NUM_TX_DESC;
5460 txd = tp->TxDescArray + entry;
5461 len = skb_frag_size(frag);
5462 addr = skb_frag_address(frag);
5463 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5464 if (unlikely(dma_mapping_error(d, mapping))) {
5465 if (net_ratelimit())
5466 netif_err(tp, drv, tp->dev,
5467 "Failed to map TX fragments DMA!\n");
5471 /* Anti gcc 2.95.3 bugware (sic) */
5472 status = opts[0] | len |
5473 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5475 txd->opts1 = cpu_to_le32(status);
5476 txd->opts2 = cpu_to_le32(opts[1]);
5477 txd->addr = cpu_to_le64(mapping);
5479 tp->tx_skb[entry].len = len;
5483 tp->tx_skb[entry].skb = skb;
5484 txd->opts1 |= cpu_to_le32(LastFrag);
5490 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5494 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5495 struct sk_buff *skb, u32 *opts)
5497 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
5498 u32 mss = skb_shinfo(skb)->gso_size;
5499 int offset = info->opts_offset;
5503 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5504 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5505 const struct iphdr *ip = ip_hdr(skb);
5507 if (ip->protocol == IPPROTO_TCP)
5508 opts[offset] |= info->checksum.tcp;
5509 else if (ip->protocol == IPPROTO_UDP)
5510 opts[offset] |= info->checksum.udp;
5516 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5517 struct net_device *dev)
5519 struct rtl8169_private *tp = netdev_priv(dev);
5520 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5521 struct TxDesc *txd = tp->TxDescArray + entry;
5522 void __iomem *ioaddr = tp->mmio_addr;
5523 struct device *d = &tp->pci_dev->dev;
5529 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
5530 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5534 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5537 len = skb_headlen(skb);
5538 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
5539 if (unlikely(dma_mapping_error(d, mapping))) {
5540 if (net_ratelimit())
5541 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
5545 tp->tx_skb[entry].len = len;
5546 txd->addr = cpu_to_le64(mapping);
5548 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5551 rtl8169_tso_csum(tp, skb, opts);
5553 frags = rtl8169_xmit_frags(tp, skb, opts);
5557 opts[0] |= FirstFrag;
5559 opts[0] |= FirstFrag | LastFrag;
5560 tp->tx_skb[entry].skb = skb;
5563 txd->opts2 = cpu_to_le32(opts[1]);
5565 netdev_sent_queue(dev, skb->len);
5569 /* Anti gcc 2.95.3 bugware (sic) */
5570 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
5571 txd->opts1 = cpu_to_le32(status);
5573 tp->cur_tx += frags + 1;
5577 RTL_W8(TxPoll, NPQ);
5581 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5582 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5583 * not miss a ring update when it notices a stopped queue.
5586 netif_stop_queue(dev);
5587 /* Sync with rtl_tx:
5588 * - publish queue status and cur_tx ring index (write barrier)
5589 * - refresh dirty_tx ring index (read barrier).
5590 * May the current thread have a pessimistic view of the ring
5591 * status and forget to wake up queue, a racing rtl_tx thread
5595 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5596 netif_wake_queue(dev);
5599 return NETDEV_TX_OK;
5602 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
5605 dev->stats.tx_dropped++;
5606 return NETDEV_TX_OK;
5609 netif_stop_queue(dev);
5610 dev->stats.tx_dropped++;
5611 return NETDEV_TX_BUSY;
5614 static void rtl8169_pcierr_interrupt(struct net_device *dev)
5616 struct rtl8169_private *tp = netdev_priv(dev);
5617 struct pci_dev *pdev = tp->pci_dev;
5618 u16 pci_status, pci_cmd;
5620 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5621 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5623 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5624 pci_cmd, pci_status);
5627 * The recovery sequence below admits a very elaborated explanation:
5628 * - it seems to work;
5629 * - I did not see what else could be done;
5630 * - it makes iop3xx happy.
5632 * Feel free to adjust to your needs.
5634 if (pdev->broken_parity_status)
5635 pci_cmd &= ~PCI_COMMAND_PARITY;
5637 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5639 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
5641 pci_write_config_word(pdev, PCI_STATUS,
5642 pci_status & (PCI_STATUS_DETECTED_PARITY |
5643 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5644 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5646 /* The infamous DAC f*ckup only happens at boot time */
5647 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
5648 void __iomem *ioaddr = tp->mmio_addr;
5650 netif_info(tp, intr, dev, "disabling PCI DAC\n");
5651 tp->cp_cmd &= ~PCIDAC;
5652 RTL_W16(CPlusCmd, tp->cp_cmd);
5653 dev->features &= ~NETIF_F_HIGHDMA;
5656 rtl8169_hw_reset(tp);
5658 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5666 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
5668 struct rtl8169_stats *tx_stats = &tp->tx_stats;
5669 unsigned int dirty_tx, tx_left;
5670 struct rtl_txc txc = { 0, 0 };
5672 dirty_tx = tp->dirty_tx;
5674 tx_left = tp->cur_tx - dirty_tx;
5676 while (tx_left > 0) {
5677 unsigned int entry = dirty_tx % NUM_TX_DESC;
5678 struct ring_info *tx_skb = tp->tx_skb + entry;
5682 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5683 if (status & DescOwn)
5686 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5687 tp->TxDescArray + entry);
5688 if (status & LastFrag) {
5689 struct sk_buff *skb = tx_skb->skb;
5692 txc.bytes += skb->len;
5700 u64_stats_update_begin(&tx_stats->syncp);
5701 tx_stats->packets += txc.packets;
5702 tx_stats->bytes += txc.bytes;
5703 u64_stats_update_end(&tx_stats->syncp);
5705 netdev_completed_queue(dev, txc.packets, txc.bytes);
5707 if (tp->dirty_tx != dirty_tx) {
5708 tp->dirty_tx = dirty_tx;
5709 /* Sync with rtl8169_start_xmit:
5710 * - publish dirty_tx ring index (write barrier)
5711 * - refresh cur_tx ring index and queue status (read barrier)
5712 * May the current thread miss the stopped queue condition,
5713 * a racing xmit thread can only have a right view of the
5717 if (netif_queue_stopped(dev) &&
5718 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5719 netif_wake_queue(dev);
5722 * 8168 hack: TxPoll requests are lost when the Tx packets are
5723 * too close. Let's kick an extra TxPoll request when a burst
5724 * of start_xmit activity is detected (if it is not detected,
5725 * it is slow enough). -- FR
5727 if (tp->cur_tx != dirty_tx) {
5728 void __iomem *ioaddr = tp->mmio_addr;
5730 RTL_W8(TxPoll, NPQ);
5735 static inline int rtl8169_fragmented_frame(u32 status)
5737 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5740 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
5742 u32 status = opts1 & RxProtoMask;
5744 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
5745 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5746 skb->ip_summed = CHECKSUM_UNNECESSARY;
5748 skb_checksum_none_assert(skb);
5751 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5752 struct rtl8169_private *tp,
5756 struct sk_buff *skb;
5757 struct device *d = &tp->pci_dev->dev;
5759 data = rtl8169_align(data);
5760 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
5762 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5764 memcpy(skb->data, data, pkt_size);
5765 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5770 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
5772 unsigned int cur_rx, rx_left;
5775 cur_rx = tp->cur_rx;
5776 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
5777 rx_left = min(rx_left, budget);
5779 for (; rx_left > 0; rx_left--, cur_rx++) {
5780 unsigned int entry = cur_rx % NUM_RX_DESC;
5781 struct RxDesc *desc = tp->RxDescArray + entry;
5785 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
5787 if (status & DescOwn)
5789 if (unlikely(status & RxRES)) {
5790 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5792 dev->stats.rx_errors++;
5793 if (status & (RxRWT | RxRUNT))
5794 dev->stats.rx_length_errors++;
5796 dev->stats.rx_crc_errors++;
5797 if (status & RxFOVF) {
5798 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5799 dev->stats.rx_fifo_errors++;
5801 if ((status & (RxRUNT | RxCRC)) &&
5802 !(status & (RxRWT | RxFOVF)) &&
5803 (dev->features & NETIF_F_RXALL))
5806 rtl8169_mark_to_asic(desc, rx_buf_sz);
5808 struct sk_buff *skb;
5813 addr = le64_to_cpu(desc->addr);
5814 if (likely(!(dev->features & NETIF_F_RXFCS)))
5815 pkt_size = (status & 0x00003fff) - 4;
5817 pkt_size = status & 0x00003fff;
5820 * The driver does not support incoming fragmented
5821 * frames. They are seen as a symptom of over-mtu
5824 if (unlikely(rtl8169_fragmented_frame(status))) {
5825 dev->stats.rx_dropped++;
5826 dev->stats.rx_length_errors++;
5827 rtl8169_mark_to_asic(desc, rx_buf_sz);
5831 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5832 tp, pkt_size, addr);
5833 rtl8169_mark_to_asic(desc, rx_buf_sz);
5835 dev->stats.rx_dropped++;
5839 rtl8169_rx_csum(skb, status);
5840 skb_put(skb, pkt_size);
5841 skb->protocol = eth_type_trans(skb, dev);
5843 rtl8169_rx_vlan_tag(desc, skb);
5845 napi_gro_receive(&tp->napi, skb);
5847 u64_stats_update_begin(&tp->rx_stats.syncp);
5848 tp->rx_stats.packets++;
5849 tp->rx_stats.bytes += pkt_size;
5850 u64_stats_update_end(&tp->rx_stats.syncp);
5853 /* Work around for AMD plateform. */
5854 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
5855 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5861 count = cur_rx - tp->cur_rx;
5862 tp->cur_rx = cur_rx;
5864 tp->dirty_rx += count;
5869 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5871 struct net_device *dev = dev_instance;
5872 struct rtl8169_private *tp = netdev_priv(dev);
5876 status = rtl_get_events(tp);
5877 if (status && status != 0xffff) {
5878 status &= RTL_EVENT_NAPI | tp->event_slow;
5882 rtl_irq_disable(tp);
5883 napi_schedule(&tp->napi);
5886 return IRQ_RETVAL(handled);
5890 * Workqueue context.
5892 static void rtl_slow_event_work(struct rtl8169_private *tp)
5894 struct net_device *dev = tp->dev;
5897 status = rtl_get_events(tp) & tp->event_slow;
5898 rtl_ack_events(tp, status);
5900 if (unlikely(status & RxFIFOOver)) {
5901 switch (tp->mac_version) {
5902 /* Work around for rx fifo overflow */
5903 case RTL_GIGA_MAC_VER_11:
5904 netif_stop_queue(dev);
5905 /* XXX - Hack alert. See rtl_task(). */
5906 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
5912 if (unlikely(status & SYSErr))
5913 rtl8169_pcierr_interrupt(dev);
5915 if (status & LinkChg)
5916 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5918 napi_disable(&tp->napi);
5919 rtl_irq_disable(tp);
5921 napi_enable(&tp->napi);
5922 napi_schedule(&tp->napi);
5925 static void rtl_task(struct work_struct *work)
5927 static const struct {
5929 void (*action)(struct rtl8169_private *);
5931 /* XXX - keep rtl_slow_event_work() as first element. */
5932 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5933 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5934 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5936 struct rtl8169_private *tp =
5937 container_of(work, struct rtl8169_private, wk.work);
5938 struct net_device *dev = tp->dev;
5943 if (!netif_running(dev) ||
5944 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
5947 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5950 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
5952 rtl_work[i].action(tp);
5956 rtl_unlock_work(tp);
5959 static int rtl8169_poll(struct napi_struct *napi, int budget)
5961 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5962 struct net_device *dev = tp->dev;
5963 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5967 status = rtl_get_events(tp);
5968 rtl_ack_events(tp, status & ~tp->event_slow);
5970 if (status & RTL_EVENT_NAPI_RX)
5971 work_done = rtl_rx(dev, tp, (u32) budget);
5973 if (status & RTL_EVENT_NAPI_TX)
5976 if (status & tp->event_slow) {
5977 enable_mask &= ~tp->event_slow;
5979 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5982 if (work_done < budget) {
5983 napi_complete(napi);
5985 rtl_irq_enable(tp, enable_mask);
5992 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5994 struct rtl8169_private *tp = netdev_priv(dev);
5996 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5999 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6000 RTL_W32(RxMissed, 0);
6003 static void rtl8169_down(struct net_device *dev)
6005 struct rtl8169_private *tp = netdev_priv(dev);
6006 void __iomem *ioaddr = tp->mmio_addr;
6008 del_timer_sync(&tp->timer);
6010 napi_disable(&tp->napi);
6011 netif_stop_queue(dev);
6013 rtl8169_hw_reset(tp);
6015 * At this point device interrupts can not be enabled in any function,
6016 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6017 * and napi is disabled (rtl8169_poll).
6019 rtl8169_rx_missed(dev, ioaddr);
6021 /* Give a racing hard_start_xmit a few cycles to complete. */
6022 synchronize_sched();
6024 rtl8169_tx_clear(tp);
6026 rtl8169_rx_clear(tp);
6028 rtl_pll_power_down(tp);
6031 static int rtl8169_close(struct net_device *dev)
6033 struct rtl8169_private *tp = netdev_priv(dev);
6034 struct pci_dev *pdev = tp->pci_dev;
6036 pm_runtime_get_sync(&pdev->dev);
6038 /* Update counters before going down */
6039 rtl8169_update_counters(dev);
6042 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6045 rtl_unlock_work(tp);
6047 free_irq(dev->irq, dev);
6049 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6051 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6053 tp->TxDescArray = NULL;
6054 tp->RxDescArray = NULL;
6056 pm_runtime_put_sync(&pdev->dev);
6061 static void rtl_set_rx_mode(struct net_device *dev)
6063 struct rtl8169_private *tp = netdev_priv(dev);
6064 void __iomem *ioaddr = tp->mmio_addr;
6065 u32 mc_filter[2]; /* Multicast hash filter */
6069 if (dev->flags & IFF_PROMISC) {
6070 /* Unconditionally log net taps. */
6071 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
6073 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
6075 mc_filter[1] = mc_filter[0] = 0xffffffff;
6076 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
6077 (dev->flags & IFF_ALLMULTI)) {
6078 /* Too many to filter perfectly -- accept all multicasts. */
6079 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
6080 mc_filter[1] = mc_filter[0] = 0xffffffff;
6082 struct netdev_hw_addr *ha;
6084 rx_mode = AcceptBroadcast | AcceptMyPhys;
6085 mc_filter[1] = mc_filter[0] = 0;
6086 netdev_for_each_mc_addr(ha, dev) {
6087 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
6088 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6089 rx_mode |= AcceptMulticast;
6093 if (dev->features & NETIF_F_RXALL)
6094 rx_mode |= (AcceptErr | AcceptRunt);
6096 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
6098 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
6099 u32 data = mc_filter[0];
6101 mc_filter[0] = swab32(mc_filter[1]);
6102 mc_filter[1] = swab32(data);
6105 RTL_W32(MAR0 + 4, mc_filter[1]);
6106 RTL_W32(MAR0 + 0, mc_filter[0]);
6108 RTL_W32(RxConfig, tmp);
6111 static struct rtnl_link_stats64 *
6112 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6114 struct rtl8169_private *tp = netdev_priv(dev);
6115 void __iomem *ioaddr = tp->mmio_addr;
6118 if (netif_running(dev))
6119 rtl8169_rx_missed(dev, ioaddr);
6122 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6123 stats->rx_packets = tp->rx_stats.packets;
6124 stats->rx_bytes = tp->rx_stats.bytes;
6125 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6129 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6130 stats->tx_packets = tp->tx_stats.packets;
6131 stats->tx_bytes = tp->tx_stats.bytes;
6132 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6134 stats->rx_dropped = dev->stats.rx_dropped;
6135 stats->tx_dropped = dev->stats.tx_dropped;
6136 stats->rx_length_errors = dev->stats.rx_length_errors;
6137 stats->rx_errors = dev->stats.rx_errors;
6138 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6139 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6140 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6145 static void rtl8169_net_suspend(struct net_device *dev)
6147 struct rtl8169_private *tp = netdev_priv(dev);
6149 if (!netif_running(dev))
6152 netif_device_detach(dev);
6153 netif_stop_queue(dev);
6156 napi_disable(&tp->napi);
6157 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6158 rtl_unlock_work(tp);
6160 rtl_pll_power_down(tp);
6165 static int rtl8169_suspend(struct device *device)
6167 struct pci_dev *pdev = to_pci_dev(device);
6168 struct net_device *dev = pci_get_drvdata(pdev);
6170 rtl8169_net_suspend(dev);
6175 static void __rtl8169_resume(struct net_device *dev)
6177 struct rtl8169_private *tp = netdev_priv(dev);
6179 netif_device_attach(dev);
6181 rtl_pll_power_up(tp);
6183 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6185 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6188 static int rtl8169_resume(struct device *device)
6190 struct pci_dev *pdev = to_pci_dev(device);
6191 struct net_device *dev = pci_get_drvdata(pdev);
6192 struct rtl8169_private *tp = netdev_priv(dev);
6194 rtl8169_init_phy(dev, tp);
6196 if (netif_running(dev))
6197 __rtl8169_resume(dev);
6202 static int rtl8169_runtime_suspend(struct device *device)
6204 struct pci_dev *pdev = to_pci_dev(device);
6205 struct net_device *dev = pci_get_drvdata(pdev);
6206 struct rtl8169_private *tp = netdev_priv(dev);
6208 if (!tp->TxDescArray)
6212 tp->saved_wolopts = __rtl8169_get_wol(tp);
6213 __rtl8169_set_wol(tp, WAKE_ANY);
6214 rtl_unlock_work(tp);
6216 rtl8169_net_suspend(dev);
6221 static int rtl8169_runtime_resume(struct device *device)
6223 struct pci_dev *pdev = to_pci_dev(device);
6224 struct net_device *dev = pci_get_drvdata(pdev);
6225 struct rtl8169_private *tp = netdev_priv(dev);
6227 if (!tp->TxDescArray)
6231 __rtl8169_set_wol(tp, tp->saved_wolopts);
6232 tp->saved_wolopts = 0;
6233 rtl_unlock_work(tp);
6235 rtl8169_init_phy(dev, tp);
6237 __rtl8169_resume(dev);
6242 static int rtl8169_runtime_idle(struct device *device)
6244 struct pci_dev *pdev = to_pci_dev(device);
6245 struct net_device *dev = pci_get_drvdata(pdev);
6246 struct rtl8169_private *tp = netdev_priv(dev);
6248 return tp->TxDescArray ? -EBUSY : 0;
6251 static const struct dev_pm_ops rtl8169_pm_ops = {
6252 .suspend = rtl8169_suspend,
6253 .resume = rtl8169_resume,
6254 .freeze = rtl8169_suspend,
6255 .thaw = rtl8169_resume,
6256 .poweroff = rtl8169_suspend,
6257 .restore = rtl8169_resume,
6258 .runtime_suspend = rtl8169_runtime_suspend,
6259 .runtime_resume = rtl8169_runtime_resume,
6260 .runtime_idle = rtl8169_runtime_idle,
6263 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6265 #else /* !CONFIG_PM */
6267 #define RTL8169_PM_OPS NULL
6269 #endif /* !CONFIG_PM */
6271 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6273 void __iomem *ioaddr = tp->mmio_addr;
6275 /* WoL fails with 8168b when the receiver is disabled. */
6276 switch (tp->mac_version) {
6277 case RTL_GIGA_MAC_VER_11:
6278 case RTL_GIGA_MAC_VER_12:
6279 case RTL_GIGA_MAC_VER_17:
6280 pci_clear_master(tp->pci_dev);
6282 RTL_W8(ChipCmd, CmdRxEnb);
6291 static void rtl_shutdown(struct pci_dev *pdev)
6293 struct net_device *dev = pci_get_drvdata(pdev);
6294 struct rtl8169_private *tp = netdev_priv(dev);
6295 struct device *d = &pdev->dev;
6297 pm_runtime_get_sync(d);
6299 rtl8169_net_suspend(dev);
6301 /* Restore original MAC address */
6302 rtl_rar_set(tp, dev->perm_addr);
6304 rtl8169_hw_reset(tp);
6306 if (system_state == SYSTEM_POWER_OFF) {
6307 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6308 rtl_wol_suspend_quirk(tp);
6309 rtl_wol_shutdown_quirk(tp);
6312 pci_wake_from_d3(pdev, true);
6313 pci_set_power_state(pdev, PCI_D3hot);
6316 pm_runtime_put_noidle(d);
6319 static struct pci_driver rtl8169_pci_driver = {
6321 .id_table = rtl8169_pci_tbl,
6322 .probe = rtl8169_init_one,
6323 .remove = __devexit_p(rtl8169_remove_one),
6324 .shutdown = rtl_shutdown,
6325 .driver.pm = RTL8169_PM_OPS,
6328 static int __init rtl8169_init_module(void)
6330 return pci_register_driver(&rtl8169_pci_driver);
6333 static void __exit rtl8169_cleanup_module(void)
6335 pci_unregister_driver(&rtl8169_pci_driver);
6338 module_init(rtl8169_init_module);
6339 module_exit(rtl8169_cleanup_module);