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_8105E_1 "rtl_nic/rtl8105e-1.fw"
47 #define assert(expr) \
49 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
50 #expr,__FILE__,__func__,__LINE__); \
52 #define dprintk(fmt, args...) \
53 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
55 #define assert(expr) do {} while (0)
56 #define dprintk(fmt, args...) do {} while (0)
57 #endif /* RTL8169_DEBUG */
59 #define R8169_MSG_DEFAULT \
60 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
62 #define TX_BUFFS_AVAIL(tp) \
63 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
65 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
66 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
67 static const int multicast_filter_limit = 32;
69 /* MAC address length */
70 #define MAC_ADDR_LEN 6
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,
134 RTL_GIGA_MAC_NONE = 0xff,
137 enum rtl_tx_desc_version {
142 #define _R(NAME,TD,FW) \
143 { .name = NAME, .txd_version = TD, .fw_name = FW }
145 static const struct {
147 enum rtl_tx_desc_version txd_version;
149 } rtl_chip_infos[] = {
151 [RTL_GIGA_MAC_VER_01] =
152 _R("RTL8169", RTL_TD_0, NULL),
153 [RTL_GIGA_MAC_VER_02] =
154 _R("RTL8169s", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_03] =
156 _R("RTL8110s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_04] =
158 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_05] =
160 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_06] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
164 [RTL_GIGA_MAC_VER_07] =
165 _R("RTL8102e", RTL_TD_1, NULL),
166 [RTL_GIGA_MAC_VER_08] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_09] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_10] =
171 _R("RTL8101e", RTL_TD_0, NULL),
172 [RTL_GIGA_MAC_VER_11] =
173 _R("RTL8168b/8111b", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_12] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_13] =
177 _R("RTL8101e", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_14] =
179 _R("RTL8100e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_15] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_16] =
183 _R("RTL8101e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_17] =
185 _R("RTL8168b/8111b", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_18] =
187 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
188 [RTL_GIGA_MAC_VER_19] =
189 _R("RTL8168c/8111c", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_20] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_21] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_22] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_23] =
197 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_24] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_25] =
201 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
202 [RTL_GIGA_MAC_VER_26] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
204 [RTL_GIGA_MAC_VER_27] =
205 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
206 [RTL_GIGA_MAC_VER_28] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_29] =
209 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
210 [RTL_GIGA_MAC_VER_30] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_31] =
213 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
214 [RTL_GIGA_MAC_VER_32] =
215 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
216 [RTL_GIGA_MAC_VER_33] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2)
227 static void rtl_hw_start_8169(struct net_device *);
228 static void rtl_hw_start_8168(struct net_device *);
229 static void rtl_hw_start_8101(struct net_device *);
231 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
232 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
233 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
234 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
235 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
237 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
238 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
239 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
240 { PCI_VENDOR_ID_LINKSYS, 0x1032,
241 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
243 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
247 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
249 static int rx_buf_sz = 16383;
256 MAC0 = 0, /* Ethernet hardware address. */
258 MAR0 = 8, /* Multicast filter. */
259 CounterAddrLow = 0x10,
260 CounterAddrHigh = 0x14,
261 TxDescStartAddrLow = 0x20,
262 TxDescStartAddrHigh = 0x24,
263 TxHDescStartAddrLow = 0x28,
264 TxHDescStartAddrHigh = 0x2c,
273 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
274 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
277 #define RX128_INT_EN (1 << 15) /* 8111c and later */
278 #define RX_MULTI_EN (1 << 14) /* 8111c only */
279 #define RXCFG_FIFO_SHIFT 13
280 /* No threshold before first PCI xfer */
281 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
282 #define RXCFG_DMA_SHIFT 8
283 /* Unlimited maximum PCI burst. */
284 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
285 #define RTL_RX_CONFIG_MASK 0xff7e1880u
301 RxDescAddrLow = 0xe4,
302 RxDescAddrHigh = 0xe8,
303 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
305 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
307 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
309 #define TxPacketMax (8064 >> 7)
312 FuncEventMask = 0xf4,
313 FuncPresetState = 0xf8,
314 FuncForceEvent = 0xfc,
317 enum rtl8110_registers {
323 enum rtl8168_8101_registers {
326 #define CSIAR_FLAG 0x80000000
327 #define CSIAR_WRITE_CMD 0x80000000
328 #define CSIAR_BYTE_ENABLE 0x0f
329 #define CSIAR_BYTE_ENABLE_SHIFT 12
330 #define CSIAR_ADDR_MASK 0x0fff
333 #define EPHYAR_FLAG 0x80000000
334 #define EPHYAR_WRITE_CMD 0x80000000
335 #define EPHYAR_REG_MASK 0x1f
336 #define EPHYAR_REG_SHIFT 16
337 #define EPHYAR_DATA_MASK 0xffff
339 #define PFM_EN (1 << 6)
341 #define FIX_NAK_1 (1 << 4)
342 #define FIX_NAK_2 (1 << 3)
345 #define NOW_IS_OOB (1 << 7)
346 #define EN_NDP (1 << 3)
347 #define EN_OOB_RESET (1 << 2)
349 #define EFUSEAR_FLAG 0x80000000
350 #define EFUSEAR_WRITE_CMD 0x80000000
351 #define EFUSEAR_READ_CMD 0x00000000
352 #define EFUSEAR_REG_MASK 0x03ff
353 #define EFUSEAR_REG_SHIFT 8
354 #define EFUSEAR_DATA_MASK 0xff
357 enum rtl8168_registers {
362 #define ERIAR_FLAG 0x80000000
363 #define ERIAR_WRITE_CMD 0x80000000
364 #define ERIAR_READ_CMD 0x00000000
365 #define ERIAR_ADDR_BYTE_ALIGN 4
366 #define ERIAR_TYPE_SHIFT 16
367 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
368 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
369 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
370 #define ERIAR_MASK_SHIFT 12
371 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
372 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
373 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
374 EPHY_RXER_NUM = 0x7c,
375 OCPDR = 0xb0, /* OCP GPHY access */
376 #define OCPDR_WRITE_CMD 0x80000000
377 #define OCPDR_READ_CMD 0x00000000
378 #define OCPDR_REG_MASK 0x7f
379 #define OCPDR_GPHY_REG_SHIFT 16
380 #define OCPDR_DATA_MASK 0xffff
382 #define OCPAR_FLAG 0x80000000
383 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
384 #define OCPAR_GPHY_READ_CMD 0x0000f060
385 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
386 MISC = 0xf0, /* 8168e only. */
387 #define TXPLA_RST (1 << 29)
388 #define PWM_EN (1 << 22)
391 enum rtl_register_content {
392 /* InterruptStatusBits */
396 TxDescUnavail = 0x0080,
419 /* TXPoll register p.5 */
420 HPQ = 0x80, /* Poll cmd on the high prio queue */
421 NPQ = 0x40, /* Poll cmd on the low prio queue */
422 FSWInt = 0x01, /* Forced software interrupt */
426 Cfg9346_Unlock = 0xc0,
431 AcceptBroadcast = 0x08,
432 AcceptMulticast = 0x04,
434 AcceptAllPhys = 0x01,
437 TxInterFrameGapShift = 24,
438 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
440 /* Config1 register p.24 */
443 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
444 Speed_down = (1 << 4),
448 PMEnable = (1 << 0), /* Power Management Enable */
450 /* Config2 register p. 25 */
451 PCI_Clock_66MHz = 0x01,
452 PCI_Clock_33MHz = 0x00,
454 /* Config3 register p.25 */
455 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
456 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
457 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
459 /* Config5 register p.27 */
460 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
461 MWF = (1 << 5), /* Accept Multicast wakeup frame */
462 UWF = (1 << 4), /* Accept Unicast wakeup frame */
464 LanWake = (1 << 1), /* LanWake enable/disable */
465 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
468 TBIReset = 0x80000000,
469 TBILoopback = 0x40000000,
470 TBINwEnable = 0x20000000,
471 TBINwRestart = 0x10000000,
472 TBILinkOk = 0x02000000,
473 TBINwComplete = 0x01000000,
476 EnableBist = (1 << 15), // 8168 8101
477 Mac_dbgo_oe = (1 << 14), // 8168 8101
478 Normal_mode = (1 << 13), // unused
479 Force_half_dup = (1 << 12), // 8168 8101
480 Force_rxflow_en = (1 << 11), // 8168 8101
481 Force_txflow_en = (1 << 10), // 8168 8101
482 Cxpl_dbg_sel = (1 << 9), // 8168 8101
483 ASF = (1 << 8), // 8168 8101
484 PktCntrDisable = (1 << 7), // 8168 8101
485 Mac_dbgo_sel = 0x001c, // 8168
490 INTT_0 = 0x0000, // 8168
491 INTT_1 = 0x0001, // 8168
492 INTT_2 = 0x0002, // 8168
493 INTT_3 = 0x0003, // 8168
495 /* rtl8169_PHYstatus */
506 TBILinkOK = 0x02000000,
508 /* DumpCounterCommand */
513 /* First doubleword. */
514 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
515 RingEnd = (1 << 30), /* End of descriptor ring */
516 FirstFrag = (1 << 29), /* First segment of a packet */
517 LastFrag = (1 << 28), /* Final segment of a packet */
521 enum rtl_tx_desc_bit {
522 /* First doubleword. */
523 TD_LSO = (1 << 27), /* Large Send Offload */
524 #define TD_MSS_MAX 0x07ffu /* MSS value */
526 /* Second doubleword. */
527 TxVlanTag = (1 << 17), /* Add VLAN tag */
530 /* 8169, 8168b and 810x except 8102e. */
531 enum rtl_tx_desc_bit_0 {
532 /* First doubleword. */
533 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
534 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
535 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
536 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
539 /* 8102e, 8168c and beyond. */
540 enum rtl_tx_desc_bit_1 {
541 /* Second doubleword. */
542 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
543 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
544 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
545 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
548 static const struct rtl_tx_desc_info {
555 } tx_desc_info [] = {
558 .udp = TD0_IP_CS | TD0_UDP_CS,
559 .tcp = TD0_IP_CS | TD0_TCP_CS
561 .mss_shift = TD0_MSS_SHIFT,
566 .udp = TD1_IP_CS | TD1_UDP_CS,
567 .tcp = TD1_IP_CS | TD1_TCP_CS
569 .mss_shift = TD1_MSS_SHIFT,
574 enum rtl_rx_desc_bit {
576 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
577 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
579 #define RxProtoUDP (PID1)
580 #define RxProtoTCP (PID0)
581 #define RxProtoIP (PID1 | PID0)
582 #define RxProtoMask RxProtoIP
584 IPFail = (1 << 16), /* IP checksum failed */
585 UDPFail = (1 << 15), /* UDP/IP checksum failed */
586 TCPFail = (1 << 14), /* TCP/IP checksum failed */
587 RxVlanTag = (1 << 16), /* VLAN tag available */
590 #define RsvdMask 0x3fffc000
607 u8 __pad[sizeof(void *) - sizeof(u32)];
611 RTL_FEATURE_WOL = (1 << 0),
612 RTL_FEATURE_MSI = (1 << 1),
613 RTL_FEATURE_GMII = (1 << 2),
616 struct rtl8169_counters {
623 __le32 tx_one_collision;
624 __le32 tx_multi_collision;
632 struct rtl8169_private {
633 void __iomem *mmio_addr; /* memory map physical address */
634 struct pci_dev *pci_dev;
635 struct net_device *dev;
636 struct napi_struct napi;
641 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
642 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
645 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
646 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
647 dma_addr_t TxPhyAddr;
648 dma_addr_t RxPhyAddr;
649 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
650 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
651 struct timer_list timer;
658 void (*write)(void __iomem *, int, int);
659 int (*read)(void __iomem *, int);
662 struct pll_power_ops {
663 void (*down)(struct rtl8169_private *);
664 void (*up)(struct rtl8169_private *);
667 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
668 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
669 void (*phy_reset_enable)(struct rtl8169_private *tp);
670 void (*hw_start)(struct net_device *);
671 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
672 unsigned int (*link_ok)(void __iomem *);
673 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
674 struct delayed_work task;
677 struct mii_if_info mii;
678 struct rtl8169_counters counters;
682 const struct firmware *fw;
684 #define RTL_VER_SIZE 32
686 char version[RTL_VER_SIZE];
688 struct rtl_fw_phy_action {
693 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
696 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
697 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
698 module_param(use_dac, int, 0);
699 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
700 module_param_named(debug, debug.msg_enable, int, 0);
701 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
702 MODULE_LICENSE("GPL");
703 MODULE_VERSION(RTL8169_VERSION);
704 MODULE_FIRMWARE(FIRMWARE_8168D_1);
705 MODULE_FIRMWARE(FIRMWARE_8168D_2);
706 MODULE_FIRMWARE(FIRMWARE_8168E_1);
707 MODULE_FIRMWARE(FIRMWARE_8168E_2);
708 MODULE_FIRMWARE(FIRMWARE_8105E_1);
710 static int rtl8169_open(struct net_device *dev);
711 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
712 struct net_device *dev);
713 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
714 static int rtl8169_init_ring(struct net_device *dev);
715 static void rtl_hw_start(struct net_device *dev);
716 static int rtl8169_close(struct net_device *dev);
717 static void rtl_set_rx_mode(struct net_device *dev);
718 static void rtl8169_tx_timeout(struct net_device *dev);
719 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
720 static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
721 void __iomem *, u32 budget);
722 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
723 static void rtl8169_down(struct net_device *dev);
724 static void rtl8169_rx_clear(struct rtl8169_private *tp);
725 static int rtl8169_poll(struct napi_struct *napi, int budget);
727 static const unsigned int rtl8169_rx_config = RX_FIFO_THRESH | RX_DMA_BURST;
729 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
731 void __iomem *ioaddr = tp->mmio_addr;
734 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
735 for (i = 0; i < 20; i++) {
737 if (RTL_R32(OCPAR) & OCPAR_FLAG)
740 return RTL_R32(OCPDR);
743 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
745 void __iomem *ioaddr = tp->mmio_addr;
748 RTL_W32(OCPDR, data);
749 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
750 for (i = 0; i < 20; i++) {
752 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
757 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
759 void __iomem *ioaddr = tp->mmio_addr;
763 RTL_W32(ERIAR, 0x800010e8);
765 for (i = 0; i < 5; i++) {
767 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
771 ocp_write(tp, 0x1, 0x30, 0x00000001);
774 #define OOB_CMD_RESET 0x00
775 #define OOB_CMD_DRIVER_START 0x05
776 #define OOB_CMD_DRIVER_STOP 0x06
778 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
780 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
783 static void rtl8168_driver_start(struct rtl8169_private *tp)
788 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
790 reg = rtl8168_get_ocp_reg(tp);
792 for (i = 0; i < 10; i++) {
794 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
799 static void rtl8168_driver_stop(struct rtl8169_private *tp)
804 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
806 reg = rtl8168_get_ocp_reg(tp);
808 for (i = 0; i < 10; i++) {
810 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
815 static int r8168dp_check_dash(struct rtl8169_private *tp)
817 u16 reg = rtl8168_get_ocp_reg(tp);
819 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
822 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
826 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
828 for (i = 20; i > 0; i--) {
830 * Check if the RTL8169 has completed writing to the specified
833 if (!(RTL_R32(PHYAR) & 0x80000000))
838 * According to hardware specs a 20us delay is required after write
839 * complete indication, but before sending next command.
844 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
848 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
850 for (i = 20; i > 0; i--) {
852 * Check if the RTL8169 has completed retrieving data from
853 * the specified MII register.
855 if (RTL_R32(PHYAR) & 0x80000000) {
856 value = RTL_R32(PHYAR) & 0xffff;
862 * According to hardware specs a 20us delay is required after read
863 * complete indication, but before sending next command.
870 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
874 RTL_W32(OCPDR, data |
875 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
876 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
877 RTL_W32(EPHY_RXER_NUM, 0);
879 for (i = 0; i < 100; i++) {
881 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
886 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
888 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
889 (value & OCPDR_DATA_MASK));
892 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
896 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
899 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
900 RTL_W32(EPHY_RXER_NUM, 0);
902 for (i = 0; i < 100; i++) {
904 if (RTL_R32(OCPAR) & OCPAR_FLAG)
908 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
911 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
913 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
915 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
918 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
920 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
923 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
925 r8168dp_2_mdio_start(ioaddr);
927 r8169_mdio_write(ioaddr, reg_addr, value);
929 r8168dp_2_mdio_stop(ioaddr);
932 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
936 r8168dp_2_mdio_start(ioaddr);
938 value = r8169_mdio_read(ioaddr, reg_addr);
940 r8168dp_2_mdio_stop(ioaddr);
945 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
947 tp->mdio_ops.write(tp->mmio_addr, location, val);
950 static int rtl_readphy(struct rtl8169_private *tp, int location)
952 return tp->mdio_ops.read(tp->mmio_addr, location);
955 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
957 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
960 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
964 val = rtl_readphy(tp, reg_addr);
965 rtl_writephy(tp, reg_addr, (val | p) & ~m);
968 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
971 struct rtl8169_private *tp = netdev_priv(dev);
973 rtl_writephy(tp, location, val);
976 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
978 struct rtl8169_private *tp = netdev_priv(dev);
980 return rtl_readphy(tp, location);
983 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
987 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
988 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
990 for (i = 0; i < 100; i++) {
991 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
997 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1002 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1004 for (i = 0; i < 100; i++) {
1005 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1006 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1015 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1019 RTL_W32(CSIDR, value);
1020 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1021 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1023 for (i = 0; i < 100; i++) {
1024 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1030 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1035 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1036 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1038 for (i = 0; i < 100; i++) {
1039 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1040 value = RTL_R32(CSIDR);
1049 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1054 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1056 for (i = 0; i < 300; i++) {
1057 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1058 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1067 static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1069 RTL_W16(IntrMask, 0x0000);
1071 RTL_W16(IntrStatus, 0xffff);
1074 static void rtl8169_asic_down(void __iomem *ioaddr)
1076 RTL_W8(ChipCmd, 0x00);
1077 rtl8169_irq_mask_and_ack(ioaddr);
1081 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1083 void __iomem *ioaddr = tp->mmio_addr;
1085 return RTL_R32(TBICSR) & TBIReset;
1088 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1090 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1093 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1095 return RTL_R32(TBICSR) & TBILinkOk;
1098 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1100 return RTL_R8(PHYstatus) & LinkStatus;
1103 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1105 void __iomem *ioaddr = tp->mmio_addr;
1107 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1110 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1114 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1115 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1118 static void __rtl8169_check_link_status(struct net_device *dev,
1119 struct rtl8169_private *tp,
1120 void __iomem *ioaddr, bool pm)
1122 unsigned long flags;
1124 spin_lock_irqsave(&tp->lock, flags);
1125 if (tp->link_ok(ioaddr)) {
1126 /* This is to cancel a scheduled suspend if there's one. */
1128 pm_request_resume(&tp->pci_dev->dev);
1129 netif_carrier_on(dev);
1130 if (net_ratelimit())
1131 netif_info(tp, ifup, dev, "link up\n");
1133 netif_carrier_off(dev);
1134 netif_info(tp, ifdown, dev, "link down\n");
1136 pm_schedule_suspend(&tp->pci_dev->dev, 100);
1138 spin_unlock_irqrestore(&tp->lock, flags);
1141 static void rtl8169_check_link_status(struct net_device *dev,
1142 struct rtl8169_private *tp,
1143 void __iomem *ioaddr)
1145 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1148 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1150 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1152 void __iomem *ioaddr = tp->mmio_addr;
1156 options = RTL_R8(Config1);
1157 if (!(options & PMEnable))
1160 options = RTL_R8(Config3);
1161 if (options & LinkUp)
1162 wolopts |= WAKE_PHY;
1163 if (options & MagicPacket)
1164 wolopts |= WAKE_MAGIC;
1166 options = RTL_R8(Config5);
1168 wolopts |= WAKE_UCAST;
1170 wolopts |= WAKE_BCAST;
1172 wolopts |= WAKE_MCAST;
1177 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1179 struct rtl8169_private *tp = netdev_priv(dev);
1181 spin_lock_irq(&tp->lock);
1183 wol->supported = WAKE_ANY;
1184 wol->wolopts = __rtl8169_get_wol(tp);
1186 spin_unlock_irq(&tp->lock);
1189 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1191 void __iomem *ioaddr = tp->mmio_addr;
1193 static const struct {
1198 { WAKE_ANY, Config1, PMEnable },
1199 { WAKE_PHY, Config3, LinkUp },
1200 { WAKE_MAGIC, Config3, MagicPacket },
1201 { WAKE_UCAST, Config5, UWF },
1202 { WAKE_BCAST, Config5, BWF },
1203 { WAKE_MCAST, Config5, MWF },
1204 { WAKE_ANY, Config5, LanWake }
1207 RTL_W8(Cfg9346, Cfg9346_Unlock);
1209 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1210 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1211 if (wolopts & cfg[i].opt)
1212 options |= cfg[i].mask;
1213 RTL_W8(cfg[i].reg, options);
1216 RTL_W8(Cfg9346, Cfg9346_Lock);
1219 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1221 struct rtl8169_private *tp = netdev_priv(dev);
1223 spin_lock_irq(&tp->lock);
1226 tp->features |= RTL_FEATURE_WOL;
1228 tp->features &= ~RTL_FEATURE_WOL;
1229 __rtl8169_set_wol(tp, wol->wolopts);
1230 spin_unlock_irq(&tp->lock);
1232 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1237 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1239 return rtl_chip_infos[tp->mac_version].fw_name;
1242 static void rtl8169_get_drvinfo(struct net_device *dev,
1243 struct ethtool_drvinfo *info)
1245 struct rtl8169_private *tp = netdev_priv(dev);
1246 struct rtl_fw *rtl_fw = tp->rtl_fw;
1248 strcpy(info->driver, MODULENAME);
1249 strcpy(info->version, RTL8169_VERSION);
1250 strcpy(info->bus_info, pci_name(tp->pci_dev));
1251 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1252 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1256 static int rtl8169_get_regs_len(struct net_device *dev)
1258 return R8169_REGS_SIZE;
1261 static int rtl8169_set_speed_tbi(struct net_device *dev,
1262 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1264 struct rtl8169_private *tp = netdev_priv(dev);
1265 void __iomem *ioaddr = tp->mmio_addr;
1269 reg = RTL_R32(TBICSR);
1270 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1271 (duplex == DUPLEX_FULL)) {
1272 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1273 } else if (autoneg == AUTONEG_ENABLE)
1274 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1276 netif_warn(tp, link, dev,
1277 "incorrect speed setting refused in TBI mode\n");
1284 static int rtl8169_set_speed_xmii(struct net_device *dev,
1285 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1287 struct rtl8169_private *tp = netdev_priv(dev);
1288 int giga_ctrl, bmcr;
1291 rtl_writephy(tp, 0x1f, 0x0000);
1293 if (autoneg == AUTONEG_ENABLE) {
1296 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1297 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1298 ADVERTISE_100HALF | ADVERTISE_100FULL);
1300 if (adv & ADVERTISED_10baseT_Half)
1301 auto_nego |= ADVERTISE_10HALF;
1302 if (adv & ADVERTISED_10baseT_Full)
1303 auto_nego |= ADVERTISE_10FULL;
1304 if (adv & ADVERTISED_100baseT_Half)
1305 auto_nego |= ADVERTISE_100HALF;
1306 if (adv & ADVERTISED_100baseT_Full)
1307 auto_nego |= ADVERTISE_100FULL;
1309 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1311 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1312 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1314 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1315 if (tp->mii.supports_gmii) {
1316 if (adv & ADVERTISED_1000baseT_Half)
1317 giga_ctrl |= ADVERTISE_1000HALF;
1318 if (adv & ADVERTISED_1000baseT_Full)
1319 giga_ctrl |= ADVERTISE_1000FULL;
1320 } else if (adv & (ADVERTISED_1000baseT_Half |
1321 ADVERTISED_1000baseT_Full)) {
1322 netif_info(tp, link, dev,
1323 "PHY does not support 1000Mbps\n");
1327 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1329 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1330 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1334 if (speed == SPEED_10)
1336 else if (speed == SPEED_100)
1337 bmcr = BMCR_SPEED100;
1341 if (duplex == DUPLEX_FULL)
1342 bmcr |= BMCR_FULLDPLX;
1345 rtl_writephy(tp, MII_BMCR, bmcr);
1347 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1348 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1349 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1350 rtl_writephy(tp, 0x17, 0x2138);
1351 rtl_writephy(tp, 0x0e, 0x0260);
1353 rtl_writephy(tp, 0x17, 0x2108);
1354 rtl_writephy(tp, 0x0e, 0x0000);
1363 static int rtl8169_set_speed(struct net_device *dev,
1364 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1366 struct rtl8169_private *tp = netdev_priv(dev);
1369 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1373 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1374 (advertising & ADVERTISED_1000baseT_Full)) {
1375 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1381 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1383 struct rtl8169_private *tp = netdev_priv(dev);
1384 unsigned long flags;
1387 del_timer_sync(&tp->timer);
1389 spin_lock_irqsave(&tp->lock, flags);
1390 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1391 cmd->duplex, cmd->advertising);
1392 spin_unlock_irqrestore(&tp->lock, flags);
1397 static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
1399 if (dev->mtu > TD_MSS_MAX)
1400 features &= ~NETIF_F_ALL_TSO;
1405 static int rtl8169_set_features(struct net_device *dev, u32 features)
1407 struct rtl8169_private *tp = netdev_priv(dev);
1408 void __iomem *ioaddr = tp->mmio_addr;
1409 unsigned long flags;
1411 spin_lock_irqsave(&tp->lock, flags);
1413 if (features & NETIF_F_RXCSUM)
1414 tp->cp_cmd |= RxChkSum;
1416 tp->cp_cmd &= ~RxChkSum;
1418 if (dev->features & NETIF_F_HW_VLAN_RX)
1419 tp->cp_cmd |= RxVlan;
1421 tp->cp_cmd &= ~RxVlan;
1423 RTL_W16(CPlusCmd, tp->cp_cmd);
1426 spin_unlock_irqrestore(&tp->lock, flags);
1431 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1432 struct sk_buff *skb)
1434 return (vlan_tx_tag_present(skb)) ?
1435 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1438 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1440 u32 opts2 = le32_to_cpu(desc->opts2);
1442 if (opts2 & RxVlanTag)
1443 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1448 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1450 struct rtl8169_private *tp = netdev_priv(dev);
1451 void __iomem *ioaddr = tp->mmio_addr;
1455 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1456 cmd->port = PORT_FIBRE;
1457 cmd->transceiver = XCVR_INTERNAL;
1459 status = RTL_R32(TBICSR);
1460 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1461 cmd->autoneg = !!(status & TBINwEnable);
1463 ethtool_cmd_speed_set(cmd, SPEED_1000);
1464 cmd->duplex = DUPLEX_FULL; /* Always set */
1469 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1471 struct rtl8169_private *tp = netdev_priv(dev);
1473 return mii_ethtool_gset(&tp->mii, cmd);
1476 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1478 struct rtl8169_private *tp = netdev_priv(dev);
1479 unsigned long flags;
1482 spin_lock_irqsave(&tp->lock, flags);
1484 rc = tp->get_settings(dev, cmd);
1486 spin_unlock_irqrestore(&tp->lock, flags);
1490 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1493 struct rtl8169_private *tp = netdev_priv(dev);
1494 unsigned long flags;
1496 if (regs->len > R8169_REGS_SIZE)
1497 regs->len = R8169_REGS_SIZE;
1499 spin_lock_irqsave(&tp->lock, flags);
1500 memcpy_fromio(p, tp->mmio_addr, regs->len);
1501 spin_unlock_irqrestore(&tp->lock, flags);
1504 static u32 rtl8169_get_msglevel(struct net_device *dev)
1506 struct rtl8169_private *tp = netdev_priv(dev);
1508 return tp->msg_enable;
1511 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1513 struct rtl8169_private *tp = netdev_priv(dev);
1515 tp->msg_enable = value;
1518 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1525 "tx_single_collisions",
1526 "tx_multi_collisions",
1534 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1538 return ARRAY_SIZE(rtl8169_gstrings);
1544 static void rtl8169_update_counters(struct net_device *dev)
1546 struct rtl8169_private *tp = netdev_priv(dev);
1547 void __iomem *ioaddr = tp->mmio_addr;
1548 struct device *d = &tp->pci_dev->dev;
1549 struct rtl8169_counters *counters;
1555 * Some chips are unable to dump tally counters when the receiver
1558 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1561 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1565 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1566 cmd = (u64)paddr & DMA_BIT_MASK(32);
1567 RTL_W32(CounterAddrLow, cmd);
1568 RTL_W32(CounterAddrLow, cmd | CounterDump);
1571 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1572 memcpy(&tp->counters, counters, sizeof(*counters));
1578 RTL_W32(CounterAddrLow, 0);
1579 RTL_W32(CounterAddrHigh, 0);
1581 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1584 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1585 struct ethtool_stats *stats, u64 *data)
1587 struct rtl8169_private *tp = netdev_priv(dev);
1591 rtl8169_update_counters(dev);
1593 data[0] = le64_to_cpu(tp->counters.tx_packets);
1594 data[1] = le64_to_cpu(tp->counters.rx_packets);
1595 data[2] = le64_to_cpu(tp->counters.tx_errors);
1596 data[3] = le32_to_cpu(tp->counters.rx_errors);
1597 data[4] = le16_to_cpu(tp->counters.rx_missed);
1598 data[5] = le16_to_cpu(tp->counters.align_errors);
1599 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1600 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1601 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1602 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1603 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1604 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1605 data[12] = le16_to_cpu(tp->counters.tx_underun);
1608 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1612 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1617 static const struct ethtool_ops rtl8169_ethtool_ops = {
1618 .get_drvinfo = rtl8169_get_drvinfo,
1619 .get_regs_len = rtl8169_get_regs_len,
1620 .get_link = ethtool_op_get_link,
1621 .get_settings = rtl8169_get_settings,
1622 .set_settings = rtl8169_set_settings,
1623 .get_msglevel = rtl8169_get_msglevel,
1624 .set_msglevel = rtl8169_set_msglevel,
1625 .get_regs = rtl8169_get_regs,
1626 .get_wol = rtl8169_get_wol,
1627 .set_wol = rtl8169_set_wol,
1628 .get_strings = rtl8169_get_strings,
1629 .get_sset_count = rtl8169_get_sset_count,
1630 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1633 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1634 struct net_device *dev, u8 default_version)
1636 void __iomem *ioaddr = tp->mmio_addr;
1638 * The driver currently handles the 8168Bf and the 8168Be identically
1639 * but they can be identified more specifically through the test below
1642 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1644 * Same thing for the 8101Eb and the 8101Ec:
1646 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1648 static const struct rtl_mac_info {
1654 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1655 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1656 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1659 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1660 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1661 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1663 /* 8168DP family. */
1664 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1665 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1666 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
1669 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1670 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1671 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1672 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1673 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1674 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1675 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1676 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1677 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1680 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1681 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1682 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1683 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1686 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
1687 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1688 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1689 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1690 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1691 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1692 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1693 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1694 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1695 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1696 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1697 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1698 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1699 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1700 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1701 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1702 /* FIXME: where did these entries come from ? -- FR */
1703 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1704 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1707 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1708 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1709 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1710 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1711 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1712 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1715 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1717 const struct rtl_mac_info *p = mac_info;
1720 reg = RTL_R32(TxConfig);
1721 while ((reg & p->mask) != p->val)
1723 tp->mac_version = p->mac_version;
1725 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1726 netif_notice(tp, probe, dev,
1727 "unknown MAC, using family default\n");
1728 tp->mac_version = default_version;
1732 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1734 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1742 static void rtl_writephy_batch(struct rtl8169_private *tp,
1743 const struct phy_reg *regs, int len)
1746 rtl_writephy(tp, regs->reg, regs->val);
1751 #define PHY_READ 0x00000000
1752 #define PHY_DATA_OR 0x10000000
1753 #define PHY_DATA_AND 0x20000000
1754 #define PHY_BJMPN 0x30000000
1755 #define PHY_READ_EFUSE 0x40000000
1756 #define PHY_READ_MAC_BYTE 0x50000000
1757 #define PHY_WRITE_MAC_BYTE 0x60000000
1758 #define PHY_CLEAR_READCOUNT 0x70000000
1759 #define PHY_WRITE 0x80000000
1760 #define PHY_READCOUNT_EQ_SKIP 0x90000000
1761 #define PHY_COMP_EQ_SKIPN 0xa0000000
1762 #define PHY_COMP_NEQ_SKIPN 0xb0000000
1763 #define PHY_WRITE_PREVIOUS 0xc0000000
1764 #define PHY_SKIPN 0xd0000000
1765 #define PHY_DELAY_MS 0xe0000000
1766 #define PHY_WRITE_ERI_WORD 0xf0000000
1770 char version[RTL_VER_SIZE];
1776 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1778 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1780 const struct firmware *fw = rtl_fw->fw;
1781 struct fw_info *fw_info = (struct fw_info *)fw->data;
1782 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1783 char *version = rtl_fw->version;
1786 if (fw->size < FW_OPCODE_SIZE)
1789 if (!fw_info->magic) {
1790 size_t i, size, start;
1793 if (fw->size < sizeof(*fw_info))
1796 for (i = 0; i < fw->size; i++)
1797 checksum += fw->data[i];
1801 start = le32_to_cpu(fw_info->fw_start);
1802 if (start > fw->size)
1805 size = le32_to_cpu(fw_info->fw_len);
1806 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1809 memcpy(version, fw_info->version, RTL_VER_SIZE);
1811 pa->code = (__le32 *)(fw->data + start);
1814 if (fw->size % FW_OPCODE_SIZE)
1817 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1819 pa->code = (__le32 *)fw->data;
1820 pa->size = fw->size / FW_OPCODE_SIZE;
1822 version[RTL_VER_SIZE - 1] = 0;
1829 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1830 struct rtl_fw_phy_action *pa)
1835 for (index = 0; index < pa->size; index++) {
1836 u32 action = le32_to_cpu(pa->code[index]);
1837 u32 regno = (action & 0x0fff0000) >> 16;
1839 switch(action & 0xf0000000) {
1843 case PHY_READ_EFUSE:
1844 case PHY_CLEAR_READCOUNT:
1846 case PHY_WRITE_PREVIOUS:
1851 if (regno > index) {
1852 netif_err(tp, ifup, tp->dev,
1853 "Out of range of firmware\n");
1857 case PHY_READCOUNT_EQ_SKIP:
1858 if (index + 2 >= pa->size) {
1859 netif_err(tp, ifup, tp->dev,
1860 "Out of range of firmware\n");
1864 case PHY_COMP_EQ_SKIPN:
1865 case PHY_COMP_NEQ_SKIPN:
1867 if (index + 1 + regno >= pa->size) {
1868 netif_err(tp, ifup, tp->dev,
1869 "Out of range of firmware\n");
1874 case PHY_READ_MAC_BYTE:
1875 case PHY_WRITE_MAC_BYTE:
1876 case PHY_WRITE_ERI_WORD:
1878 netif_err(tp, ifup, tp->dev,
1879 "Invalid action 0x%08x\n", action);
1888 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1890 struct net_device *dev = tp->dev;
1893 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1894 netif_err(tp, ifup, dev, "invalid firwmare\n");
1898 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1904 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1906 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1910 predata = count = 0;
1912 for (index = 0; index < pa->size; ) {
1913 u32 action = le32_to_cpu(pa->code[index]);
1914 u32 data = action & 0x0000ffff;
1915 u32 regno = (action & 0x0fff0000) >> 16;
1920 switch(action & 0xf0000000) {
1922 predata = rtl_readphy(tp, regno);
1937 case PHY_READ_EFUSE:
1938 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1941 case PHY_CLEAR_READCOUNT:
1946 rtl_writephy(tp, regno, data);
1949 case PHY_READCOUNT_EQ_SKIP:
1950 index += (count == data) ? 2 : 1;
1952 case PHY_COMP_EQ_SKIPN:
1953 if (predata == data)
1957 case PHY_COMP_NEQ_SKIPN:
1958 if (predata != data)
1962 case PHY_WRITE_PREVIOUS:
1963 rtl_writephy(tp, regno, predata);
1974 case PHY_READ_MAC_BYTE:
1975 case PHY_WRITE_MAC_BYTE:
1976 case PHY_WRITE_ERI_WORD:
1983 static void rtl_release_firmware(struct rtl8169_private *tp)
1985 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
1986 release_firmware(tp->rtl_fw->fw);
1989 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
1992 static void rtl_apply_firmware(struct rtl8169_private *tp)
1994 struct rtl_fw *rtl_fw = tp->rtl_fw;
1996 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1997 if (!IS_ERR_OR_NULL(rtl_fw))
1998 rtl_phy_write_fw(tp, rtl_fw);
2001 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2003 if (rtl_readphy(tp, reg) != val)
2004 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2006 rtl_apply_firmware(tp);
2009 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2011 static const struct phy_reg phy_reg_init[] = {
2073 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2076 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2078 static const struct phy_reg phy_reg_init[] = {
2084 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2087 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2089 struct pci_dev *pdev = tp->pci_dev;
2090 u16 vendor_id, device_id;
2092 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2093 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2095 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2098 rtl_writephy(tp, 0x1f, 0x0001);
2099 rtl_writephy(tp, 0x10, 0xf01b);
2100 rtl_writephy(tp, 0x1f, 0x0000);
2103 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2105 static const struct phy_reg phy_reg_init[] = {
2145 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2147 rtl8169scd_hw_phy_config_quirk(tp);
2150 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2152 static const struct phy_reg phy_reg_init[] = {
2200 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2203 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2205 static const struct phy_reg phy_reg_init[] = {
2210 rtl_writephy(tp, 0x1f, 0x0001);
2211 rtl_patchphy(tp, 0x16, 1 << 0);
2213 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2216 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2218 static const struct phy_reg phy_reg_init[] = {
2224 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2227 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2229 static const struct phy_reg phy_reg_init[] = {
2237 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2240 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2242 static const struct phy_reg phy_reg_init[] = {
2248 rtl_writephy(tp, 0x1f, 0x0000);
2249 rtl_patchphy(tp, 0x14, 1 << 5);
2250 rtl_patchphy(tp, 0x0d, 1 << 5);
2252 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2255 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2257 static const struct phy_reg phy_reg_init[] = {
2277 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2279 rtl_patchphy(tp, 0x14, 1 << 5);
2280 rtl_patchphy(tp, 0x0d, 1 << 5);
2281 rtl_writephy(tp, 0x1f, 0x0000);
2284 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2286 static const struct phy_reg phy_reg_init[] = {
2304 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2306 rtl_patchphy(tp, 0x16, 1 << 0);
2307 rtl_patchphy(tp, 0x14, 1 << 5);
2308 rtl_patchphy(tp, 0x0d, 1 << 5);
2309 rtl_writephy(tp, 0x1f, 0x0000);
2312 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2314 static const struct phy_reg phy_reg_init[] = {
2326 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2328 rtl_patchphy(tp, 0x16, 1 << 0);
2329 rtl_patchphy(tp, 0x14, 1 << 5);
2330 rtl_patchphy(tp, 0x0d, 1 << 5);
2331 rtl_writephy(tp, 0x1f, 0x0000);
2334 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2336 rtl8168c_3_hw_phy_config(tp);
2339 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2341 static const struct phy_reg phy_reg_init_0[] = {
2342 /* Channel Estimation */
2363 * Enhance line driver power
2372 * Can not link to 1Gbps with bad cable
2373 * Decrease SNR threshold form 21.07dB to 19.04dB
2381 void __iomem *ioaddr = tp->mmio_addr;
2383 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2387 * Fine Tune Switching regulator parameter
2389 rtl_writephy(tp, 0x1f, 0x0002);
2390 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2391 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2393 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2394 static const struct phy_reg phy_reg_init[] = {
2404 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2406 val = rtl_readphy(tp, 0x0d);
2408 if ((val & 0x00ff) != 0x006c) {
2409 static const u32 set[] = {
2410 0x0065, 0x0066, 0x0067, 0x0068,
2411 0x0069, 0x006a, 0x006b, 0x006c
2415 rtl_writephy(tp, 0x1f, 0x0002);
2418 for (i = 0; i < ARRAY_SIZE(set); i++)
2419 rtl_writephy(tp, 0x0d, val | set[i]);
2422 static const struct phy_reg phy_reg_init[] = {
2430 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2433 /* RSET couple improve */
2434 rtl_writephy(tp, 0x1f, 0x0002);
2435 rtl_patchphy(tp, 0x0d, 0x0300);
2436 rtl_patchphy(tp, 0x0f, 0x0010);
2438 /* Fine tune PLL performance */
2439 rtl_writephy(tp, 0x1f, 0x0002);
2440 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2441 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2443 rtl_writephy(tp, 0x1f, 0x0005);
2444 rtl_writephy(tp, 0x05, 0x001b);
2446 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2448 rtl_writephy(tp, 0x1f, 0x0000);
2451 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2453 static const struct phy_reg phy_reg_init_0[] = {
2454 /* Channel Estimation */
2475 * Enhance line driver power
2484 * Can not link to 1Gbps with bad cable
2485 * Decrease SNR threshold form 21.07dB to 19.04dB
2493 void __iomem *ioaddr = tp->mmio_addr;
2495 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2497 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2498 static const struct phy_reg phy_reg_init[] = {
2509 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2511 val = rtl_readphy(tp, 0x0d);
2512 if ((val & 0x00ff) != 0x006c) {
2513 static const u32 set[] = {
2514 0x0065, 0x0066, 0x0067, 0x0068,
2515 0x0069, 0x006a, 0x006b, 0x006c
2519 rtl_writephy(tp, 0x1f, 0x0002);
2522 for (i = 0; i < ARRAY_SIZE(set); i++)
2523 rtl_writephy(tp, 0x0d, val | set[i]);
2526 static const struct phy_reg phy_reg_init[] = {
2534 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2537 /* Fine tune PLL performance */
2538 rtl_writephy(tp, 0x1f, 0x0002);
2539 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2540 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2542 /* Switching regulator Slew rate */
2543 rtl_writephy(tp, 0x1f, 0x0002);
2544 rtl_patchphy(tp, 0x0f, 0x0017);
2546 rtl_writephy(tp, 0x1f, 0x0005);
2547 rtl_writephy(tp, 0x05, 0x001b);
2549 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2551 rtl_writephy(tp, 0x1f, 0x0000);
2554 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2556 static const struct phy_reg phy_reg_init[] = {
2612 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2615 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2617 static const struct phy_reg phy_reg_init[] = {
2627 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2628 rtl_patchphy(tp, 0x0d, 1 << 5);
2631 static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2633 static const struct phy_reg phy_reg_init[] = {
2634 /* Enable Delay cap */
2640 /* Channel estimation fine tune */
2649 /* Update PFM & 10M TX idle timer */
2661 rtl_apply_firmware(tp);
2663 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2665 /* DCO enable for 10M IDLE Power */
2666 rtl_writephy(tp, 0x1f, 0x0007);
2667 rtl_writephy(tp, 0x1e, 0x0023);
2668 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2669 rtl_writephy(tp, 0x1f, 0x0000);
2671 /* For impedance matching */
2672 rtl_writephy(tp, 0x1f, 0x0002);
2673 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2674 rtl_writephy(tp, 0x1f, 0x0000);
2676 /* PHY auto speed down */
2677 rtl_writephy(tp, 0x1f, 0x0007);
2678 rtl_writephy(tp, 0x1e, 0x002d);
2679 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2680 rtl_writephy(tp, 0x1f, 0x0000);
2681 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2683 rtl_writephy(tp, 0x1f, 0x0005);
2684 rtl_writephy(tp, 0x05, 0x8b86);
2685 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2686 rtl_writephy(tp, 0x1f, 0x0000);
2688 rtl_writephy(tp, 0x1f, 0x0005);
2689 rtl_writephy(tp, 0x05, 0x8b85);
2690 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2691 rtl_writephy(tp, 0x1f, 0x0007);
2692 rtl_writephy(tp, 0x1e, 0x0020);
2693 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2694 rtl_writephy(tp, 0x1f, 0x0006);
2695 rtl_writephy(tp, 0x00, 0x5a00);
2696 rtl_writephy(tp, 0x1f, 0x0000);
2697 rtl_writephy(tp, 0x0d, 0x0007);
2698 rtl_writephy(tp, 0x0e, 0x003c);
2699 rtl_writephy(tp, 0x0d, 0x4007);
2700 rtl_writephy(tp, 0x0e, 0x0000);
2701 rtl_writephy(tp, 0x0d, 0x0000);
2704 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
2706 static const struct phy_reg phy_reg_init[] = {
2713 rtl_writephy(tp, 0x1f, 0x0000);
2714 rtl_patchphy(tp, 0x11, 1 << 12);
2715 rtl_patchphy(tp, 0x19, 1 << 13);
2716 rtl_patchphy(tp, 0x10, 1 << 15);
2718 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2721 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2723 static const struct phy_reg phy_reg_init[] = {
2737 /* Disable ALDPS before ram code */
2738 rtl_writephy(tp, 0x1f, 0x0000);
2739 rtl_writephy(tp, 0x18, 0x0310);
2742 rtl_apply_firmware(tp);
2744 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2747 static void rtl_hw_phy_config(struct net_device *dev)
2749 struct rtl8169_private *tp = netdev_priv(dev);
2751 rtl8169_print_mac_version(tp);
2753 switch (tp->mac_version) {
2754 case RTL_GIGA_MAC_VER_01:
2756 case RTL_GIGA_MAC_VER_02:
2757 case RTL_GIGA_MAC_VER_03:
2758 rtl8169s_hw_phy_config(tp);
2760 case RTL_GIGA_MAC_VER_04:
2761 rtl8169sb_hw_phy_config(tp);
2763 case RTL_GIGA_MAC_VER_05:
2764 rtl8169scd_hw_phy_config(tp);
2766 case RTL_GIGA_MAC_VER_06:
2767 rtl8169sce_hw_phy_config(tp);
2769 case RTL_GIGA_MAC_VER_07:
2770 case RTL_GIGA_MAC_VER_08:
2771 case RTL_GIGA_MAC_VER_09:
2772 rtl8102e_hw_phy_config(tp);
2774 case RTL_GIGA_MAC_VER_11:
2775 rtl8168bb_hw_phy_config(tp);
2777 case RTL_GIGA_MAC_VER_12:
2778 rtl8168bef_hw_phy_config(tp);
2780 case RTL_GIGA_MAC_VER_17:
2781 rtl8168bef_hw_phy_config(tp);
2783 case RTL_GIGA_MAC_VER_18:
2784 rtl8168cp_1_hw_phy_config(tp);
2786 case RTL_GIGA_MAC_VER_19:
2787 rtl8168c_1_hw_phy_config(tp);
2789 case RTL_GIGA_MAC_VER_20:
2790 rtl8168c_2_hw_phy_config(tp);
2792 case RTL_GIGA_MAC_VER_21:
2793 rtl8168c_3_hw_phy_config(tp);
2795 case RTL_GIGA_MAC_VER_22:
2796 rtl8168c_4_hw_phy_config(tp);
2798 case RTL_GIGA_MAC_VER_23:
2799 case RTL_GIGA_MAC_VER_24:
2800 rtl8168cp_2_hw_phy_config(tp);
2802 case RTL_GIGA_MAC_VER_25:
2803 rtl8168d_1_hw_phy_config(tp);
2805 case RTL_GIGA_MAC_VER_26:
2806 rtl8168d_2_hw_phy_config(tp);
2808 case RTL_GIGA_MAC_VER_27:
2809 rtl8168d_3_hw_phy_config(tp);
2811 case RTL_GIGA_MAC_VER_28:
2812 rtl8168d_4_hw_phy_config(tp);
2814 case RTL_GIGA_MAC_VER_29:
2815 case RTL_GIGA_MAC_VER_30:
2816 rtl8105e_hw_phy_config(tp);
2818 case RTL_GIGA_MAC_VER_31:
2821 case RTL_GIGA_MAC_VER_32:
2822 case RTL_GIGA_MAC_VER_33:
2823 rtl8168e_hw_phy_config(tp);
2831 static void rtl8169_phy_timer(unsigned long __opaque)
2833 struct net_device *dev = (struct net_device *)__opaque;
2834 struct rtl8169_private *tp = netdev_priv(dev);
2835 struct timer_list *timer = &tp->timer;
2836 void __iomem *ioaddr = tp->mmio_addr;
2837 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2839 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
2841 spin_lock_irq(&tp->lock);
2843 if (tp->phy_reset_pending(tp)) {
2845 * A busy loop could burn quite a few cycles on nowadays CPU.
2846 * Let's delay the execution of the timer for a few ticks.
2852 if (tp->link_ok(ioaddr))
2855 netif_warn(tp, link, dev, "PHY reset until link up\n");
2857 tp->phy_reset_enable(tp);
2860 mod_timer(timer, jiffies + timeout);
2862 spin_unlock_irq(&tp->lock);
2865 #ifdef CONFIG_NET_POLL_CONTROLLER
2867 * Polling 'interrupt' - used by things like netconsole to send skbs
2868 * without having to re-enable interrupts. It's not called while
2869 * the interrupt routine is executing.
2871 static void rtl8169_netpoll(struct net_device *dev)
2873 struct rtl8169_private *tp = netdev_priv(dev);
2874 struct pci_dev *pdev = tp->pci_dev;
2876 disable_irq(pdev->irq);
2877 rtl8169_interrupt(pdev->irq, dev);
2878 enable_irq(pdev->irq);
2882 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2883 void __iomem *ioaddr)
2886 pci_release_regions(pdev);
2887 pci_clear_mwi(pdev);
2888 pci_disable_device(pdev);
2892 static void rtl8169_phy_reset(struct net_device *dev,
2893 struct rtl8169_private *tp)
2897 tp->phy_reset_enable(tp);
2898 for (i = 0; i < 100; i++) {
2899 if (!tp->phy_reset_pending(tp))
2903 netif_err(tp, link, dev, "PHY reset failed\n");
2906 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
2908 void __iomem *ioaddr = tp->mmio_addr;
2910 rtl_hw_phy_config(dev);
2912 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2913 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2917 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2919 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2920 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
2922 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
2923 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2925 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
2926 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
2929 rtl8169_phy_reset(dev, tp);
2931 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2932 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2933 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2934 (tp->mii.supports_gmii ?
2935 ADVERTISED_1000baseT_Half |
2936 ADVERTISED_1000baseT_Full : 0));
2938 if (RTL_R8(PHYstatus) & TBI_Enable)
2939 netif_info(tp, link, dev, "TBI auto-negotiating\n");
2942 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2944 void __iomem *ioaddr = tp->mmio_addr;
2948 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2949 high = addr[4] | (addr[5] << 8);
2951 spin_lock_irq(&tp->lock);
2953 RTL_W8(Cfg9346, Cfg9346_Unlock);
2955 RTL_W32(MAC4, high);
2961 RTL_W8(Cfg9346, Cfg9346_Lock);
2963 spin_unlock_irq(&tp->lock);
2966 static int rtl_set_mac_address(struct net_device *dev, void *p)
2968 struct rtl8169_private *tp = netdev_priv(dev);
2969 struct sockaddr *addr = p;
2971 if (!is_valid_ether_addr(addr->sa_data))
2972 return -EADDRNOTAVAIL;
2974 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2976 rtl_rar_set(tp, dev->dev_addr);
2981 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2983 struct rtl8169_private *tp = netdev_priv(dev);
2984 struct mii_ioctl_data *data = if_mii(ifr);
2986 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2989 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2990 struct mii_ioctl_data *data, int cmd)
2994 data->phy_id = 32; /* Internal PHY */
2998 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3002 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3008 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3013 static const struct rtl_cfg_info {
3014 void (*hw_start)(struct net_device *);
3015 unsigned int region;
3021 } rtl_cfg_infos [] = {
3023 .hw_start = rtl_hw_start_8169,
3026 .intr_event = SYSErr | LinkChg | RxOverflow |
3027 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
3028 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
3029 .features = RTL_FEATURE_GMII,
3030 .default_ver = RTL_GIGA_MAC_VER_01,
3033 .hw_start = rtl_hw_start_8168,
3036 .intr_event = SYSErr | LinkChg | RxOverflow |
3037 TxErr | TxOK | RxOK | RxErr,
3038 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
3039 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3040 .default_ver = RTL_GIGA_MAC_VER_11,
3043 .hw_start = rtl_hw_start_8101,
3046 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3047 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
3048 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
3049 .features = RTL_FEATURE_MSI,
3050 .default_ver = RTL_GIGA_MAC_VER_13,
3054 /* Cfg9346_Unlock assumed. */
3055 static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3056 const struct rtl_cfg_info *cfg)
3061 cfg2 = RTL_R8(Config2) & ~MSIEnable;
3062 if (cfg->features & RTL_FEATURE_MSI) {
3063 if (pci_enable_msi(pdev)) {
3064 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3067 msi = RTL_FEATURE_MSI;
3070 RTL_W8(Config2, cfg2);
3074 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3076 if (tp->features & RTL_FEATURE_MSI) {
3077 pci_disable_msi(pdev);
3078 tp->features &= ~RTL_FEATURE_MSI;
3082 static const struct net_device_ops rtl8169_netdev_ops = {
3083 .ndo_open = rtl8169_open,
3084 .ndo_stop = rtl8169_close,
3085 .ndo_get_stats = rtl8169_get_stats,
3086 .ndo_start_xmit = rtl8169_start_xmit,
3087 .ndo_tx_timeout = rtl8169_tx_timeout,
3088 .ndo_validate_addr = eth_validate_addr,
3089 .ndo_change_mtu = rtl8169_change_mtu,
3090 .ndo_fix_features = rtl8169_fix_features,
3091 .ndo_set_features = rtl8169_set_features,
3092 .ndo_set_mac_address = rtl_set_mac_address,
3093 .ndo_do_ioctl = rtl8169_ioctl,
3094 .ndo_set_multicast_list = rtl_set_rx_mode,
3095 #ifdef CONFIG_NET_POLL_CONTROLLER
3096 .ndo_poll_controller = rtl8169_netpoll,
3101 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3103 struct mdio_ops *ops = &tp->mdio_ops;
3105 switch (tp->mac_version) {
3106 case RTL_GIGA_MAC_VER_27:
3107 ops->write = r8168dp_1_mdio_write;
3108 ops->read = r8168dp_1_mdio_read;
3110 case RTL_GIGA_MAC_VER_28:
3111 case RTL_GIGA_MAC_VER_31:
3112 ops->write = r8168dp_2_mdio_write;
3113 ops->read = r8168dp_2_mdio_read;
3116 ops->write = r8169_mdio_write;
3117 ops->read = r8169_mdio_read;
3122 static void r810x_phy_power_down(struct rtl8169_private *tp)
3124 rtl_writephy(tp, 0x1f, 0x0000);
3125 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3128 static void r810x_phy_power_up(struct rtl8169_private *tp)
3130 rtl_writephy(tp, 0x1f, 0x0000);
3131 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3134 static void r810x_pll_power_down(struct rtl8169_private *tp)
3136 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3137 rtl_writephy(tp, 0x1f, 0x0000);
3138 rtl_writephy(tp, MII_BMCR, 0x0000);
3142 r810x_phy_power_down(tp);
3145 static void r810x_pll_power_up(struct rtl8169_private *tp)
3147 r810x_phy_power_up(tp);
3150 static void r8168_phy_power_up(struct rtl8169_private *tp)
3152 rtl_writephy(tp, 0x1f, 0x0000);
3153 switch (tp->mac_version) {
3154 case RTL_GIGA_MAC_VER_11:
3155 case RTL_GIGA_MAC_VER_12:
3156 case RTL_GIGA_MAC_VER_17:
3157 case RTL_GIGA_MAC_VER_18:
3158 case RTL_GIGA_MAC_VER_19:
3159 case RTL_GIGA_MAC_VER_20:
3160 case RTL_GIGA_MAC_VER_21:
3161 case RTL_GIGA_MAC_VER_22:
3162 case RTL_GIGA_MAC_VER_23:
3163 case RTL_GIGA_MAC_VER_24:
3164 case RTL_GIGA_MAC_VER_25:
3165 case RTL_GIGA_MAC_VER_26:
3166 case RTL_GIGA_MAC_VER_27:
3167 case RTL_GIGA_MAC_VER_28:
3168 case RTL_GIGA_MAC_VER_31:
3169 rtl_writephy(tp, 0x0e, 0x0000);
3174 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3177 static void r8168_phy_power_down(struct rtl8169_private *tp)
3179 rtl_writephy(tp, 0x1f, 0x0000);
3180 switch (tp->mac_version) {
3181 case RTL_GIGA_MAC_VER_32:
3182 case RTL_GIGA_MAC_VER_33:
3183 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3186 case RTL_GIGA_MAC_VER_11:
3187 case RTL_GIGA_MAC_VER_12:
3188 case RTL_GIGA_MAC_VER_17:
3189 case RTL_GIGA_MAC_VER_18:
3190 case RTL_GIGA_MAC_VER_19:
3191 case RTL_GIGA_MAC_VER_20:
3192 case RTL_GIGA_MAC_VER_21:
3193 case RTL_GIGA_MAC_VER_22:
3194 case RTL_GIGA_MAC_VER_23:
3195 case RTL_GIGA_MAC_VER_24:
3196 case RTL_GIGA_MAC_VER_25:
3197 case RTL_GIGA_MAC_VER_26:
3198 case RTL_GIGA_MAC_VER_27:
3199 case RTL_GIGA_MAC_VER_28:
3200 case RTL_GIGA_MAC_VER_31:
3201 rtl_writephy(tp, 0x0e, 0x0200);
3203 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3208 static void r8168_pll_power_down(struct rtl8169_private *tp)
3210 void __iomem *ioaddr = tp->mmio_addr;
3212 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3213 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3214 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3215 r8168dp_check_dash(tp)) {
3219 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3220 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
3221 (RTL_R16(CPlusCmd) & ASF)) {
3225 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3226 tp->mac_version == RTL_GIGA_MAC_VER_33)
3227 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3229 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3230 rtl_writephy(tp, 0x1f, 0x0000);
3231 rtl_writephy(tp, MII_BMCR, 0x0000);
3233 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3234 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3238 r8168_phy_power_down(tp);
3240 switch (tp->mac_version) {
3241 case RTL_GIGA_MAC_VER_25:
3242 case RTL_GIGA_MAC_VER_26:
3243 case RTL_GIGA_MAC_VER_27:
3244 case RTL_GIGA_MAC_VER_28:
3245 case RTL_GIGA_MAC_VER_31:
3246 case RTL_GIGA_MAC_VER_32:
3247 case RTL_GIGA_MAC_VER_33:
3248 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3253 static void r8168_pll_power_up(struct rtl8169_private *tp)
3255 void __iomem *ioaddr = tp->mmio_addr;
3257 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3258 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3259 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3260 r8168dp_check_dash(tp)) {
3264 switch (tp->mac_version) {
3265 case RTL_GIGA_MAC_VER_25:
3266 case RTL_GIGA_MAC_VER_26:
3267 case RTL_GIGA_MAC_VER_27:
3268 case RTL_GIGA_MAC_VER_28:
3269 case RTL_GIGA_MAC_VER_31:
3270 case RTL_GIGA_MAC_VER_32:
3271 case RTL_GIGA_MAC_VER_33:
3272 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3276 r8168_phy_power_up(tp);
3279 static void rtl_pll_power_op(struct rtl8169_private *tp,
3280 void (*op)(struct rtl8169_private *))
3286 static void rtl_pll_power_down(struct rtl8169_private *tp)
3288 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3291 static void rtl_pll_power_up(struct rtl8169_private *tp)
3293 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3296 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3298 struct pll_power_ops *ops = &tp->pll_power_ops;
3300 switch (tp->mac_version) {
3301 case RTL_GIGA_MAC_VER_07:
3302 case RTL_GIGA_MAC_VER_08:
3303 case RTL_GIGA_MAC_VER_09:
3304 case RTL_GIGA_MAC_VER_10:
3305 case RTL_GIGA_MAC_VER_16:
3306 case RTL_GIGA_MAC_VER_29:
3307 case RTL_GIGA_MAC_VER_30:
3308 ops->down = r810x_pll_power_down;
3309 ops->up = r810x_pll_power_up;
3312 case RTL_GIGA_MAC_VER_11:
3313 case RTL_GIGA_MAC_VER_12:
3314 case RTL_GIGA_MAC_VER_17:
3315 case RTL_GIGA_MAC_VER_18:
3316 case RTL_GIGA_MAC_VER_19:
3317 case RTL_GIGA_MAC_VER_20:
3318 case RTL_GIGA_MAC_VER_21:
3319 case RTL_GIGA_MAC_VER_22:
3320 case RTL_GIGA_MAC_VER_23:
3321 case RTL_GIGA_MAC_VER_24:
3322 case RTL_GIGA_MAC_VER_25:
3323 case RTL_GIGA_MAC_VER_26:
3324 case RTL_GIGA_MAC_VER_27:
3325 case RTL_GIGA_MAC_VER_28:
3326 case RTL_GIGA_MAC_VER_31:
3327 case RTL_GIGA_MAC_VER_32:
3328 case RTL_GIGA_MAC_VER_33:
3329 ops->down = r8168_pll_power_down;
3330 ops->up = r8168_pll_power_up;
3340 static void rtl_hw_reset(struct rtl8169_private *tp)
3342 void __iomem *ioaddr = tp->mmio_addr;
3345 /* Soft reset the chip. */
3346 RTL_W8(ChipCmd, CmdReset);
3348 /* Check that the chip has finished the reset. */
3349 for (i = 0; i < 100; i++) {
3350 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3352 msleep_interruptible(1);
3356 static int __devinit
3357 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3359 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3360 const unsigned int region = cfg->region;
3361 struct rtl8169_private *tp;
3362 struct mii_if_info *mii;
3363 struct net_device *dev;
3364 void __iomem *ioaddr;
3368 if (netif_msg_drv(&debug)) {
3369 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3370 MODULENAME, RTL8169_VERSION);
3373 dev = alloc_etherdev(sizeof (*tp));
3375 if (netif_msg_drv(&debug))
3376 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
3381 SET_NETDEV_DEV(dev, &pdev->dev);
3382 dev->netdev_ops = &rtl8169_netdev_ops;
3383 tp = netdev_priv(dev);
3386 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
3390 mii->mdio_read = rtl_mdio_read;
3391 mii->mdio_write = rtl_mdio_write;
3392 mii->phy_id_mask = 0x1f;
3393 mii->reg_num_mask = 0x1f;
3394 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3396 /* disable ASPM completely as that cause random device stop working
3397 * problems as well as full system hangs for some PCIe devices users */
3398 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3399 PCIE_LINK_STATE_CLKPM);
3401 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3402 rc = pci_enable_device(pdev);
3404 netif_err(tp, probe, dev, "enable failure\n");
3405 goto err_out_free_dev_1;
3408 if (pci_set_mwi(pdev) < 0)
3409 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
3411 /* make sure PCI base addr 1 is MMIO */
3412 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
3413 netif_err(tp, probe, dev,
3414 "region #%d not an MMIO resource, aborting\n",
3420 /* check for weird/broken PCI region reporting */
3421 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
3422 netif_err(tp, probe, dev,
3423 "Invalid PCI region size(s), aborting\n");
3428 rc = pci_request_regions(pdev, MODULENAME);
3430 netif_err(tp, probe, dev, "could not request regions\n");
3434 tp->cp_cmd = RxChkSum;
3436 if ((sizeof(dma_addr_t) > 4) &&
3437 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
3438 tp->cp_cmd |= PCIDAC;
3439 dev->features |= NETIF_F_HIGHDMA;
3441 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3443 netif_err(tp, probe, dev, "DMA configuration failed\n");
3444 goto err_out_free_res_3;
3448 /* ioremap MMIO region */
3449 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
3451 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
3453 goto err_out_free_res_3;
3455 tp->mmio_addr = ioaddr;
3457 if (!pci_is_pcie(pdev))
3458 netif_info(tp, probe, dev, "not PCI Express\n");
3460 RTL_W16(IntrMask, 0x0000);
3464 RTL_W16(IntrStatus, 0xffff);
3466 pci_set_master(pdev);
3468 /* Identify chip attached to board */
3469 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
3472 * Pretend we are using VLANs; This bypasses a nasty bug where
3473 * Interrupts stop flowing on high load on 8110SCd controllers.
3475 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3476 tp->cp_cmd |= RxVlan;
3478 rtl_init_mdio_ops(tp);
3479 rtl_init_pll_power_ops(tp);
3481 rtl8169_print_mac_version(tp);
3483 chipset = tp->mac_version;
3484 tp->txd_version = rtl_chip_infos[chipset].txd_version;
3486 RTL_W8(Cfg9346, Cfg9346_Unlock);
3487 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3488 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
3489 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3490 tp->features |= RTL_FEATURE_WOL;
3491 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3492 tp->features |= RTL_FEATURE_WOL;
3493 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
3494 RTL_W8(Cfg9346, Cfg9346_Lock);
3496 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3497 (RTL_R8(PHYstatus) & TBI_Enable)) {
3498 tp->set_speed = rtl8169_set_speed_tbi;
3499 tp->get_settings = rtl8169_gset_tbi;
3500 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3501 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3502 tp->link_ok = rtl8169_tbi_link_ok;
3503 tp->do_ioctl = rtl_tbi_ioctl;
3505 tp->set_speed = rtl8169_set_speed_xmii;
3506 tp->get_settings = rtl8169_gset_xmii;
3507 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3508 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3509 tp->link_ok = rtl8169_xmii_link_ok;
3510 tp->do_ioctl = rtl_xmii_ioctl;
3513 spin_lock_init(&tp->lock);
3515 /* Get MAC address */
3516 for (i = 0; i < MAC_ADDR_LEN; i++)
3517 dev->dev_addr[i] = RTL_R8(MAC0 + i);
3518 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
3520 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
3521 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3522 dev->irq = pdev->irq;
3523 dev->base_addr = (unsigned long) ioaddr;
3525 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
3527 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3528 * properly for all devices */
3529 dev->features |= NETIF_F_RXCSUM |
3530 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3532 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3533 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3534 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3537 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3538 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3539 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
3541 tp->intr_mask = 0xffff;
3542 tp->hw_start = cfg->hw_start;
3543 tp->intr_event = cfg->intr_event;
3544 tp->napi_event = cfg->napi_event;
3546 init_timer(&tp->timer);
3547 tp->timer.data = (unsigned long) dev;
3548 tp->timer.function = rtl8169_phy_timer;
3550 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
3552 rc = register_netdev(dev);
3556 pci_set_drvdata(pdev, dev);
3558 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3559 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
3560 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
3562 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3563 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3564 tp->mac_version == RTL_GIGA_MAC_VER_31) {
3565 rtl8168_driver_start(tp);
3568 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
3570 if (pci_dev_run_wake(pdev))
3571 pm_runtime_put_noidle(&pdev->dev);
3573 netif_carrier_off(dev);
3579 rtl_disable_msi(pdev, tp);
3582 pci_release_regions(pdev);
3584 pci_clear_mwi(pdev);
3585 pci_disable_device(pdev);
3591 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
3593 struct net_device *dev = pci_get_drvdata(pdev);
3594 struct rtl8169_private *tp = netdev_priv(dev);
3596 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3597 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3598 tp->mac_version == RTL_GIGA_MAC_VER_31) {
3599 rtl8168_driver_stop(tp);
3602 cancel_delayed_work_sync(&tp->task);
3604 unregister_netdev(dev);
3606 rtl_release_firmware(tp);
3608 if (pci_dev_run_wake(pdev))
3609 pm_runtime_get_noresume(&pdev->dev);
3611 /* restore original MAC address */
3612 rtl_rar_set(tp, dev->perm_addr);
3614 rtl_disable_msi(pdev, tp);
3615 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3616 pci_set_drvdata(pdev, NULL);
3619 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3621 struct rtl_fw *rtl_fw;
3625 name = rtl_lookup_firmware_name(tp);
3627 goto out_no_firmware;
3629 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3633 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3637 rc = rtl_check_firmware(tp, rtl_fw);
3639 goto err_release_firmware;
3641 tp->rtl_fw = rtl_fw;
3645 err_release_firmware:
3646 release_firmware(rtl_fw->fw);
3650 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3657 static void rtl_request_firmware(struct rtl8169_private *tp)
3659 if (IS_ERR(tp->rtl_fw))
3660 rtl_request_uncached_firmware(tp);
3663 static int rtl8169_open(struct net_device *dev)
3665 struct rtl8169_private *tp = netdev_priv(dev);
3666 void __iomem *ioaddr = tp->mmio_addr;
3667 struct pci_dev *pdev = tp->pci_dev;
3668 int retval = -ENOMEM;
3670 pm_runtime_get_sync(&pdev->dev);
3673 * Rx and Tx desscriptors needs 256 bytes alignment.
3674 * dma_alloc_coherent provides more.
3676 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3677 &tp->TxPhyAddr, GFP_KERNEL);
3678 if (!tp->TxDescArray)
3679 goto err_pm_runtime_put;
3681 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3682 &tp->RxPhyAddr, GFP_KERNEL);
3683 if (!tp->RxDescArray)
3686 retval = rtl8169_init_ring(dev);
3690 INIT_DELAYED_WORK(&tp->task, NULL);
3694 rtl_request_firmware(tp);
3696 retval = request_irq(dev->irq, rtl8169_interrupt,
3697 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
3700 goto err_release_fw_2;
3702 napi_enable(&tp->napi);
3704 rtl8169_init_phy(dev, tp);
3706 rtl8169_set_features(dev, dev->features);
3708 rtl_pll_power_up(tp);
3712 tp->saved_wolopts = 0;
3713 pm_runtime_put_noidle(&pdev->dev);
3715 rtl8169_check_link_status(dev, tp, ioaddr);
3720 rtl_release_firmware(tp);
3721 rtl8169_rx_clear(tp);
3723 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3725 tp->RxDescArray = NULL;
3727 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3729 tp->TxDescArray = NULL;
3731 pm_runtime_put_noidle(&pdev->dev);
3735 static void rtl8169_hw_reset(struct rtl8169_private *tp)
3737 void __iomem *ioaddr = tp->mmio_addr;
3739 /* Disable interrupts */
3740 rtl8169_irq_mask_and_ack(ioaddr);
3742 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3743 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3744 tp->mac_version == RTL_GIGA_MAC_VER_31) {
3745 while (RTL_R8(TxPoll) & NPQ)
3750 /* Reset the chipset */
3751 RTL_W8(ChipCmd, CmdReset);
3757 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
3759 void __iomem *ioaddr = tp->mmio_addr;
3760 u32 cfg = rtl8169_rx_config;
3762 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
3763 RTL_W32(RxConfig, cfg);
3765 /* Set DMA burst size and Interframe Gap Time */
3766 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3767 (InterFrameGap << TxInterFrameGapShift));
3770 static void rtl_hw_start(struct net_device *dev)
3772 struct rtl8169_private *tp = netdev_priv(dev);
3778 netif_start_queue(dev);
3781 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3782 void __iomem *ioaddr)
3785 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3786 * register to be written before TxDescAddrLow to work.
3787 * Switching from MMIO to I/O access fixes the issue as well.
3789 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
3790 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
3791 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
3792 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
3795 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3799 cmd = RTL_R16(CPlusCmd);
3800 RTL_W16(CPlusCmd, cmd);
3804 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
3806 /* Low hurts. Let's disable the filtering. */
3807 RTL_W16(RxMaxSize, rx_buf_sz + 1);
3810 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3812 static const struct rtl_cfg2_info {
3817 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3818 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3819 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3820 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3822 const struct rtl_cfg2_info *p = cfg2_info;
3826 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
3827 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
3828 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3829 RTL_W32(0x7c, p->val);
3835 static void rtl_hw_start_8169(struct net_device *dev)
3837 struct rtl8169_private *tp = netdev_priv(dev);
3838 void __iomem *ioaddr = tp->mmio_addr;
3839 struct pci_dev *pdev = tp->pci_dev;
3841 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3842 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3843 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3846 RTL_W8(Cfg9346, Cfg9346_Unlock);
3847 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3848 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3849 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3850 tp->mac_version == RTL_GIGA_MAC_VER_04)
3851 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3853 RTL_W8(EarlyTxThres, NoEarlyTx);
3855 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
3857 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3858 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3859 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3860 tp->mac_version == RTL_GIGA_MAC_VER_04)
3861 rtl_set_rx_tx_config_registers(tp);
3863 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
3865 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3866 tp->mac_version == RTL_GIGA_MAC_VER_03) {
3867 dprintk("Set MAC Reg C+CR Offset 0xE0. "
3868 "Bit-3 and bit-14 MUST be 1\n");
3869 tp->cp_cmd |= (1 << 14);
3872 RTL_W16(CPlusCmd, tp->cp_cmd);
3874 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3877 * Undocumented corner. Supposedly:
3878 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3880 RTL_W16(IntrMitigate, 0x0000);
3882 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3884 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3885 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3886 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3887 tp->mac_version != RTL_GIGA_MAC_VER_04) {
3888 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3889 rtl_set_rx_tx_config_registers(tp);
3892 RTL_W8(Cfg9346, Cfg9346_Lock);
3894 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3897 RTL_W32(RxMissed, 0);
3899 rtl_set_rx_mode(dev);
3901 /* no early-rx interrupts */
3902 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
3904 /* Enable all known interrupts by setting the interrupt mask. */
3905 RTL_W16(IntrMask, tp->intr_event);
3908 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
3910 int cap = pci_pcie_cap(pdev);
3915 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3916 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3917 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3921 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
3925 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
3926 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3929 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3931 rtl_csi_access_enable(ioaddr, 0x17000000);
3934 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3936 rtl_csi_access_enable(ioaddr, 0x27000000);
3940 unsigned int offset;
3945 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
3950 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3951 rtl_ephy_write(ioaddr, e->offset, w);
3956 static void rtl_disable_clock_request(struct pci_dev *pdev)
3958 int cap = pci_pcie_cap(pdev);
3963 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3964 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3965 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3969 static void rtl_enable_clock_request(struct pci_dev *pdev)
3971 int cap = pci_pcie_cap(pdev);
3976 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3977 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3978 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3982 #define R8168_CPCMD_QUIRK_MASK (\
3993 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3995 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3997 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3999 rtl_tx_performance_tweak(pdev,
4000 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4003 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4005 rtl_hw_start_8168bb(ioaddr, pdev);
4007 RTL_W8(MaxTxPacketSize, TxPacketMax);
4009 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4012 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4014 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4016 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4018 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4020 rtl_disable_clock_request(pdev);
4022 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4025 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
4027 static const struct ephy_info e_info_8168cp[] = {
4028 { 0x01, 0, 0x0001 },
4029 { 0x02, 0x0800, 0x1000 },
4030 { 0x03, 0, 0x0042 },
4031 { 0x06, 0x0080, 0x0000 },
4035 rtl_csi_access_enable_2(ioaddr);
4037 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4039 __rtl_hw_start_8168cp(ioaddr, pdev);
4042 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4044 rtl_csi_access_enable_2(ioaddr);
4046 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4048 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4050 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4053 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4055 rtl_csi_access_enable_2(ioaddr);
4057 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4060 RTL_W8(DBG_REG, 0x20);
4062 RTL_W8(MaxTxPacketSize, TxPacketMax);
4064 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4066 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4069 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4071 static const struct ephy_info e_info_8168c_1[] = {
4072 { 0x02, 0x0800, 0x1000 },
4073 { 0x03, 0, 0x0002 },
4074 { 0x06, 0x0080, 0x0000 }
4077 rtl_csi_access_enable_2(ioaddr);
4079 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4081 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4083 __rtl_hw_start_8168cp(ioaddr, pdev);
4086 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4088 static const struct ephy_info e_info_8168c_2[] = {
4089 { 0x01, 0, 0x0001 },
4090 { 0x03, 0x0400, 0x0220 }
4093 rtl_csi_access_enable_2(ioaddr);
4095 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4097 __rtl_hw_start_8168cp(ioaddr, pdev);
4100 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4102 rtl_hw_start_8168c_2(ioaddr, pdev);
4105 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4107 rtl_csi_access_enable_2(ioaddr);
4109 __rtl_hw_start_8168cp(ioaddr, pdev);
4112 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4114 rtl_csi_access_enable_2(ioaddr);
4116 rtl_disable_clock_request(pdev);
4118 RTL_W8(MaxTxPacketSize, TxPacketMax);
4120 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4122 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4125 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4127 rtl_csi_access_enable_1(ioaddr);
4129 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4131 RTL_W8(MaxTxPacketSize, TxPacketMax);
4133 rtl_disable_clock_request(pdev);
4136 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4138 static const struct ephy_info e_info_8168d_4[] = {
4140 { 0x19, 0x20, 0x50 },
4145 rtl_csi_access_enable_1(ioaddr);
4147 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4149 RTL_W8(MaxTxPacketSize, TxPacketMax);
4151 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4152 const struct ephy_info *e = e_info_8168d_4 + i;
4155 w = rtl_ephy_read(ioaddr, e->offset);
4156 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4159 rtl_enable_clock_request(pdev);
4162 static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4164 static const struct ephy_info e_info_8168e[] = {
4165 { 0x00, 0x0200, 0x0100 },
4166 { 0x00, 0x0000, 0x0004 },
4167 { 0x06, 0x0002, 0x0001 },
4168 { 0x06, 0x0000, 0x0030 },
4169 { 0x07, 0x0000, 0x2000 },
4170 { 0x00, 0x0000, 0x0020 },
4171 { 0x03, 0x5800, 0x2000 },
4172 { 0x03, 0x0000, 0x0001 },
4173 { 0x01, 0x0800, 0x1000 },
4174 { 0x07, 0x0000, 0x4000 },
4175 { 0x1e, 0x0000, 0x2000 },
4176 { 0x19, 0xffff, 0xfe6c },
4177 { 0x0a, 0x0000, 0x0040 }
4180 rtl_csi_access_enable_2(ioaddr);
4182 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4184 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4186 RTL_W8(MaxTxPacketSize, TxPacketMax);
4188 rtl_disable_clock_request(pdev);
4190 /* Reset tx FIFO pointer */
4191 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4192 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
4194 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4197 static void rtl_hw_start_8168(struct net_device *dev)
4199 struct rtl8169_private *tp = netdev_priv(dev);
4200 void __iomem *ioaddr = tp->mmio_addr;
4201 struct pci_dev *pdev = tp->pci_dev;
4203 RTL_W8(Cfg9346, Cfg9346_Unlock);
4205 RTL_W8(MaxTxPacketSize, TxPacketMax);
4207 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4209 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
4211 RTL_W16(CPlusCmd, tp->cp_cmd);
4213 RTL_W16(IntrMitigate, 0x5151);
4215 /* Work around for RxFIFO overflow. */
4216 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4217 tp->mac_version == RTL_GIGA_MAC_VER_22) {
4218 tp->intr_event |= RxFIFOOver | PCSTimeout;
4219 tp->intr_event &= ~RxOverflow;
4222 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4224 rtl_set_rx_mode(dev);
4226 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4227 (InterFrameGap << TxInterFrameGapShift));
4231 switch (tp->mac_version) {
4232 case RTL_GIGA_MAC_VER_11:
4233 rtl_hw_start_8168bb(ioaddr, pdev);
4236 case RTL_GIGA_MAC_VER_12:
4237 case RTL_GIGA_MAC_VER_17:
4238 rtl_hw_start_8168bef(ioaddr, pdev);
4241 case RTL_GIGA_MAC_VER_18:
4242 rtl_hw_start_8168cp_1(ioaddr, pdev);
4245 case RTL_GIGA_MAC_VER_19:
4246 rtl_hw_start_8168c_1(ioaddr, pdev);
4249 case RTL_GIGA_MAC_VER_20:
4250 rtl_hw_start_8168c_2(ioaddr, pdev);
4253 case RTL_GIGA_MAC_VER_21:
4254 rtl_hw_start_8168c_3(ioaddr, pdev);
4257 case RTL_GIGA_MAC_VER_22:
4258 rtl_hw_start_8168c_4(ioaddr, pdev);
4261 case RTL_GIGA_MAC_VER_23:
4262 rtl_hw_start_8168cp_2(ioaddr, pdev);
4265 case RTL_GIGA_MAC_VER_24:
4266 rtl_hw_start_8168cp_3(ioaddr, pdev);
4269 case RTL_GIGA_MAC_VER_25:
4270 case RTL_GIGA_MAC_VER_26:
4271 case RTL_GIGA_MAC_VER_27:
4272 rtl_hw_start_8168d(ioaddr, pdev);
4275 case RTL_GIGA_MAC_VER_28:
4276 rtl_hw_start_8168d_4(ioaddr, pdev);
4279 case RTL_GIGA_MAC_VER_31:
4280 rtl_hw_start_8168dp(ioaddr, pdev);
4283 case RTL_GIGA_MAC_VER_32:
4284 case RTL_GIGA_MAC_VER_33:
4285 rtl_hw_start_8168e(ioaddr, pdev);
4289 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4290 dev->name, tp->mac_version);
4294 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4296 RTL_W8(Cfg9346, Cfg9346_Lock);
4298 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4300 RTL_W16(IntrMask, tp->intr_event);
4303 #define R810X_CPCMD_QUIRK_MASK (\
4314 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4316 static const struct ephy_info e_info_8102e_1[] = {
4317 { 0x01, 0, 0x6e65 },
4318 { 0x02, 0, 0x091f },
4319 { 0x03, 0, 0xc2f9 },
4320 { 0x06, 0, 0xafb5 },
4321 { 0x07, 0, 0x0e00 },
4322 { 0x19, 0, 0xec80 },
4323 { 0x01, 0, 0x2e65 },
4328 rtl_csi_access_enable_2(ioaddr);
4330 RTL_W8(DBG_REG, FIX_NAK_1);
4332 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4335 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4336 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4338 cfg1 = RTL_R8(Config1);
4339 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4340 RTL_W8(Config1, cfg1 & ~LEDS0);
4342 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4345 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4347 rtl_csi_access_enable_2(ioaddr);
4349 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4351 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4352 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4355 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4357 rtl_hw_start_8102e_2(ioaddr, pdev);
4359 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4362 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4364 static const struct ephy_info e_info_8105e_1[] = {
4365 { 0x07, 0, 0x4000 },
4366 { 0x19, 0, 0x0200 },
4367 { 0x19, 0, 0x0020 },
4368 { 0x1e, 0, 0x2000 },
4369 { 0x03, 0, 0x0001 },
4370 { 0x19, 0, 0x0100 },
4371 { 0x19, 0, 0x0004 },
4375 /* Force LAN exit from ASPM if Rx/Tx are not idle */
4376 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4378 /* Disable Early Tally Counter */
4379 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4381 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4382 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4384 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4387 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4389 rtl_hw_start_8105e_1(ioaddr, pdev);
4390 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4393 static void rtl_hw_start_8101(struct net_device *dev)
4395 struct rtl8169_private *tp = netdev_priv(dev);
4396 void __iomem *ioaddr = tp->mmio_addr;
4397 struct pci_dev *pdev = tp->pci_dev;
4399 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4400 tp->mac_version == RTL_GIGA_MAC_VER_16) {
4401 int cap = pci_pcie_cap(pdev);
4404 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4405 PCI_EXP_DEVCTL_NOSNOOP_EN);
4409 RTL_W8(Cfg9346, Cfg9346_Unlock);
4411 switch (tp->mac_version) {
4412 case RTL_GIGA_MAC_VER_07:
4413 rtl_hw_start_8102e_1(ioaddr, pdev);
4416 case RTL_GIGA_MAC_VER_08:
4417 rtl_hw_start_8102e_3(ioaddr, pdev);
4420 case RTL_GIGA_MAC_VER_09:
4421 rtl_hw_start_8102e_2(ioaddr, pdev);
4424 case RTL_GIGA_MAC_VER_29:
4425 rtl_hw_start_8105e_1(ioaddr, pdev);
4427 case RTL_GIGA_MAC_VER_30:
4428 rtl_hw_start_8105e_2(ioaddr, pdev);
4432 RTL_W8(Cfg9346, Cfg9346_Lock);
4434 RTL_W8(MaxTxPacketSize, TxPacketMax);
4436 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4438 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
4439 RTL_W16(CPlusCmd, tp->cp_cmd);
4441 RTL_W16(IntrMitigate, 0x0000);
4443 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4445 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4446 rtl_set_rx_tx_config_registers(tp);
4450 rtl_set_rx_mode(dev);
4452 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
4454 RTL_W16(IntrMask, tp->intr_event);
4457 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4459 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4463 netdev_update_features(dev);
4468 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4470 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
4471 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4474 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4475 void **data_buff, struct RxDesc *desc)
4477 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
4482 rtl8169_make_unusable_by_asic(desc);
4485 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4487 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4489 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4492 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4495 desc->addr = cpu_to_le64(mapping);
4497 rtl8169_mark_to_asic(desc, rx_buf_sz);
4500 static inline void *rtl8169_align(void *data)
4502 return (void *)ALIGN((long)data, 16);
4505 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4506 struct RxDesc *desc)
4510 struct device *d = &tp->pci_dev->dev;
4511 struct net_device *dev = tp->dev;
4512 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
4514 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4518 if (rtl8169_align(data) != data) {
4520 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4525 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
4527 if (unlikely(dma_mapping_error(d, mapping))) {
4528 if (net_ratelimit())
4529 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
4533 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
4541 static void rtl8169_rx_clear(struct rtl8169_private *tp)
4545 for (i = 0; i < NUM_RX_DESC; i++) {
4546 if (tp->Rx_databuff[i]) {
4547 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
4548 tp->RxDescArray + i);
4553 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
4555 desc->opts1 |= cpu_to_le32(RingEnd);
4558 static int rtl8169_rx_fill(struct rtl8169_private *tp)
4562 for (i = 0; i < NUM_RX_DESC; i++) {
4565 if (tp->Rx_databuff[i])
4568 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
4570 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
4573 tp->Rx_databuff[i] = data;
4576 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4580 rtl8169_rx_clear(tp);
4584 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4586 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4589 static int rtl8169_init_ring(struct net_device *dev)
4591 struct rtl8169_private *tp = netdev_priv(dev);
4593 rtl8169_init_ring_indexes(tp);
4595 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
4596 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
4598 return rtl8169_rx_fill(tp);
4601 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
4602 struct TxDesc *desc)
4604 unsigned int len = tx_skb->len;
4606 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4614 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4619 for (i = 0; i < n; i++) {
4620 unsigned int entry = (start + i) % NUM_TX_DESC;
4621 struct ring_info *tx_skb = tp->tx_skb + entry;
4622 unsigned int len = tx_skb->len;
4625 struct sk_buff *skb = tx_skb->skb;
4627 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4628 tp->TxDescArray + entry);
4630 tp->dev->stats.tx_dropped++;
4638 static void rtl8169_tx_clear(struct rtl8169_private *tp)
4640 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
4641 tp->cur_tx = tp->dirty_tx = 0;
4644 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
4646 struct rtl8169_private *tp = netdev_priv(dev);
4648 PREPARE_DELAYED_WORK(&tp->task, task);
4649 schedule_delayed_work(&tp->task, 4);
4652 static void rtl8169_wait_for_quiescence(struct net_device *dev)
4654 struct rtl8169_private *tp = netdev_priv(dev);
4655 void __iomem *ioaddr = tp->mmio_addr;
4657 synchronize_irq(dev->irq);
4659 /* Wait for any pending NAPI task to complete */
4660 napi_disable(&tp->napi);
4662 rtl8169_irq_mask_and_ack(ioaddr);
4664 tp->intr_mask = 0xffff;
4665 RTL_W16(IntrMask, tp->intr_event);
4666 napi_enable(&tp->napi);
4669 static void rtl8169_reinit_task(struct work_struct *work)
4671 struct rtl8169_private *tp =
4672 container_of(work, struct rtl8169_private, task.work);
4673 struct net_device *dev = tp->dev;
4678 if (!netif_running(dev))
4681 rtl8169_wait_for_quiescence(dev);
4684 ret = rtl8169_open(dev);
4685 if (unlikely(ret < 0)) {
4686 if (net_ratelimit())
4687 netif_err(tp, drv, dev,
4688 "reinit failure (status = %d). Rescheduling\n",
4690 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4697 static void rtl8169_reset_task(struct work_struct *work)
4699 struct rtl8169_private *tp =
4700 container_of(work, struct rtl8169_private, task.work);
4701 struct net_device *dev = tp->dev;
4706 if (!netif_running(dev))
4709 rtl8169_wait_for_quiescence(dev);
4711 for (i = 0; i < NUM_RX_DESC; i++)
4712 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4714 rtl8169_tx_clear(tp);
4716 rtl8169_init_ring_indexes(tp);
4718 netif_wake_queue(dev);
4719 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
4725 static void rtl8169_tx_timeout(struct net_device *dev)
4727 struct rtl8169_private *tp = netdev_priv(dev);
4729 rtl8169_hw_reset(tp);
4731 /* Let's wait a bit while any (async) irq lands on */
4732 rtl8169_schedule_work(dev, rtl8169_reset_task);
4735 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4738 struct skb_shared_info *info = skb_shinfo(skb);
4739 unsigned int cur_frag, entry;
4740 struct TxDesc * uninitialized_var(txd);
4741 struct device *d = &tp->pci_dev->dev;
4744 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4745 skb_frag_t *frag = info->frags + cur_frag;
4750 entry = (entry + 1) % NUM_TX_DESC;
4752 txd = tp->TxDescArray + entry;
4754 addr = ((void *) page_address(frag->page)) + frag->page_offset;
4755 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
4756 if (unlikely(dma_mapping_error(d, mapping))) {
4757 if (net_ratelimit())
4758 netif_err(tp, drv, tp->dev,
4759 "Failed to map TX fragments DMA!\n");
4763 /* Anti gcc 2.95.3 bugware (sic) */
4764 status = opts[0] | len |
4765 (RingEnd * !((entry + 1) % NUM_TX_DESC));
4767 txd->opts1 = cpu_to_le32(status);
4768 txd->opts2 = cpu_to_le32(opts[1]);
4769 txd->addr = cpu_to_le64(mapping);
4771 tp->tx_skb[entry].len = len;
4775 tp->tx_skb[entry].skb = skb;
4776 txd->opts1 |= cpu_to_le32(LastFrag);
4782 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4786 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4787 struct sk_buff *skb, u32 *opts)
4789 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
4790 u32 mss = skb_shinfo(skb)->gso_size;
4791 int offset = info->opts_offset;
4795 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4796 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4797 const struct iphdr *ip = ip_hdr(skb);
4799 if (ip->protocol == IPPROTO_TCP)
4800 opts[offset] |= info->checksum.tcp;
4801 else if (ip->protocol == IPPROTO_UDP)
4802 opts[offset] |= info->checksum.udp;
4808 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4809 struct net_device *dev)
4811 struct rtl8169_private *tp = netdev_priv(dev);
4812 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
4813 struct TxDesc *txd = tp->TxDescArray + entry;
4814 void __iomem *ioaddr = tp->mmio_addr;
4815 struct device *d = &tp->pci_dev->dev;
4821 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
4822 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
4826 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
4829 len = skb_headlen(skb);
4830 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
4831 if (unlikely(dma_mapping_error(d, mapping))) {
4832 if (net_ratelimit())
4833 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
4837 tp->tx_skb[entry].len = len;
4838 txd->addr = cpu_to_le64(mapping);
4840 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4843 rtl8169_tso_csum(tp, skb, opts);
4845 frags = rtl8169_xmit_frags(tp, skb, opts);
4849 opts[0] |= FirstFrag;
4851 opts[0] |= FirstFrag | LastFrag;
4852 tp->tx_skb[entry].skb = skb;
4855 txd->opts2 = cpu_to_le32(opts[1]);
4859 /* Anti gcc 2.95.3 bugware (sic) */
4860 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4861 txd->opts1 = cpu_to_le32(status);
4863 tp->cur_tx += frags + 1;
4867 RTL_W8(TxPoll, NPQ);
4869 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4870 netif_stop_queue(dev);
4872 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4873 netif_wake_queue(dev);
4876 return NETDEV_TX_OK;
4879 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
4882 dev->stats.tx_dropped++;
4883 return NETDEV_TX_OK;
4886 netif_stop_queue(dev);
4887 dev->stats.tx_dropped++;
4888 return NETDEV_TX_BUSY;
4891 static void rtl8169_pcierr_interrupt(struct net_device *dev)
4893 struct rtl8169_private *tp = netdev_priv(dev);
4894 struct pci_dev *pdev = tp->pci_dev;
4895 u16 pci_status, pci_cmd;
4897 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4898 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4900 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4901 pci_cmd, pci_status);
4904 * The recovery sequence below admits a very elaborated explanation:
4905 * - it seems to work;
4906 * - I did not see what else could be done;
4907 * - it makes iop3xx happy.
4909 * Feel free to adjust to your needs.
4911 if (pdev->broken_parity_status)
4912 pci_cmd &= ~PCI_COMMAND_PARITY;
4914 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4916 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
4918 pci_write_config_word(pdev, PCI_STATUS,
4919 pci_status & (PCI_STATUS_DETECTED_PARITY |
4920 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4921 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4923 /* The infamous DAC f*ckup only happens at boot time */
4924 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
4925 void __iomem *ioaddr = tp->mmio_addr;
4927 netif_info(tp, intr, dev, "disabling PCI DAC\n");
4928 tp->cp_cmd &= ~PCIDAC;
4929 RTL_W16(CPlusCmd, tp->cp_cmd);
4930 dev->features &= ~NETIF_F_HIGHDMA;
4933 rtl8169_hw_reset(tp);
4935 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4938 static void rtl8169_tx_interrupt(struct net_device *dev,
4939 struct rtl8169_private *tp,
4940 void __iomem *ioaddr)
4942 unsigned int dirty_tx, tx_left;
4944 dirty_tx = tp->dirty_tx;
4946 tx_left = tp->cur_tx - dirty_tx;
4948 while (tx_left > 0) {
4949 unsigned int entry = dirty_tx % NUM_TX_DESC;
4950 struct ring_info *tx_skb = tp->tx_skb + entry;
4954 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4955 if (status & DescOwn)
4958 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4959 tp->TxDescArray + entry);
4960 if (status & LastFrag) {
4961 dev->stats.tx_packets++;
4962 dev->stats.tx_bytes += tx_skb->skb->len;
4963 dev_kfree_skb(tx_skb->skb);
4970 if (tp->dirty_tx != dirty_tx) {
4971 tp->dirty_tx = dirty_tx;
4973 if (netif_queue_stopped(dev) &&
4974 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4975 netif_wake_queue(dev);
4978 * 8168 hack: TxPoll requests are lost when the Tx packets are
4979 * too close. Let's kick an extra TxPoll request when a burst
4980 * of start_xmit activity is detected (if it is not detected,
4981 * it is slow enough). -- FR
4984 if (tp->cur_tx != dirty_tx)
4985 RTL_W8(TxPoll, NPQ);
4989 static inline int rtl8169_fragmented_frame(u32 status)
4991 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4994 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
4996 u32 status = opts1 & RxProtoMask;
4998 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
4999 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5000 skb->ip_summed = CHECKSUM_UNNECESSARY;
5002 skb_checksum_none_assert(skb);
5005 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5006 struct rtl8169_private *tp,
5010 struct sk_buff *skb;
5011 struct device *d = &tp->pci_dev->dev;
5013 data = rtl8169_align(data);
5014 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
5016 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5018 memcpy(skb->data, data, pkt_size);
5019 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5024 static int rtl8169_rx_interrupt(struct net_device *dev,
5025 struct rtl8169_private *tp,
5026 void __iomem *ioaddr, u32 budget)
5028 unsigned int cur_rx, rx_left;
5031 cur_rx = tp->cur_rx;
5032 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
5033 rx_left = min(rx_left, budget);
5035 for (; rx_left > 0; rx_left--, cur_rx++) {
5036 unsigned int entry = cur_rx % NUM_RX_DESC;
5037 struct RxDesc *desc = tp->RxDescArray + entry;
5041 status = le32_to_cpu(desc->opts1);
5043 if (status & DescOwn)
5045 if (unlikely(status & RxRES)) {
5046 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5048 dev->stats.rx_errors++;
5049 if (status & (RxRWT | RxRUNT))
5050 dev->stats.rx_length_errors++;
5052 dev->stats.rx_crc_errors++;
5053 if (status & RxFOVF) {
5054 rtl8169_schedule_work(dev, rtl8169_reset_task);
5055 dev->stats.rx_fifo_errors++;
5057 rtl8169_mark_to_asic(desc, rx_buf_sz);
5059 struct sk_buff *skb;
5060 dma_addr_t addr = le64_to_cpu(desc->addr);
5061 int pkt_size = (status & 0x00001FFF) - 4;
5064 * The driver does not support incoming fragmented
5065 * frames. They are seen as a symptom of over-mtu
5068 if (unlikely(rtl8169_fragmented_frame(status))) {
5069 dev->stats.rx_dropped++;
5070 dev->stats.rx_length_errors++;
5071 rtl8169_mark_to_asic(desc, rx_buf_sz);
5075 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5076 tp, pkt_size, addr);
5077 rtl8169_mark_to_asic(desc, rx_buf_sz);
5079 dev->stats.rx_dropped++;
5083 rtl8169_rx_csum(skb, status);
5084 skb_put(skb, pkt_size);
5085 skb->protocol = eth_type_trans(skb, dev);
5087 rtl8169_rx_vlan_tag(desc, skb);
5089 napi_gro_receive(&tp->napi, skb);
5091 dev->stats.rx_bytes += pkt_size;
5092 dev->stats.rx_packets++;
5095 /* Work around for AMD plateform. */
5096 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
5097 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5103 count = cur_rx - tp->cur_rx;
5104 tp->cur_rx = cur_rx;
5106 tp->dirty_rx += count;
5111 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5113 struct net_device *dev = dev_instance;
5114 struct rtl8169_private *tp = netdev_priv(dev);
5115 void __iomem *ioaddr = tp->mmio_addr;
5119 /* loop handling interrupts until we have no new ones or
5120 * we hit a invalid/hotplug case.
5122 status = RTL_R16(IntrStatus);
5123 while (status && status != 0xffff) {
5126 /* Handle all of the error cases first. These will reset
5127 * the chip, so just exit the loop.
5129 if (unlikely(!netif_running(dev))) {
5130 rtl8169_asic_down(ioaddr);
5134 if (unlikely(status & RxFIFOOver)) {
5135 switch (tp->mac_version) {
5136 /* Work around for rx fifo overflow */
5137 case RTL_GIGA_MAC_VER_11:
5138 case RTL_GIGA_MAC_VER_22:
5139 case RTL_GIGA_MAC_VER_26:
5140 netif_stop_queue(dev);
5141 rtl8169_tx_timeout(dev);
5143 /* Testers needed. */
5144 case RTL_GIGA_MAC_VER_17:
5145 case RTL_GIGA_MAC_VER_19:
5146 case RTL_GIGA_MAC_VER_20:
5147 case RTL_GIGA_MAC_VER_21:
5148 case RTL_GIGA_MAC_VER_23:
5149 case RTL_GIGA_MAC_VER_24:
5150 case RTL_GIGA_MAC_VER_27:
5151 case RTL_GIGA_MAC_VER_28:
5152 case RTL_GIGA_MAC_VER_31:
5153 /* Experimental science. Pktgen proof. */
5154 case RTL_GIGA_MAC_VER_12:
5155 case RTL_GIGA_MAC_VER_25:
5156 if (status == RxFIFOOver)
5164 if (unlikely(status & SYSErr)) {
5165 rtl8169_pcierr_interrupt(dev);
5169 if (status & LinkChg)
5170 __rtl8169_check_link_status(dev, tp, ioaddr, true);
5172 /* We need to see the lastest version of tp->intr_mask to
5173 * avoid ignoring an MSI interrupt and having to wait for
5174 * another event which may never come.
5177 if (status & tp->intr_mask & tp->napi_event) {
5178 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5179 tp->intr_mask = ~tp->napi_event;
5181 if (likely(napi_schedule_prep(&tp->napi)))
5182 __napi_schedule(&tp->napi);
5184 netif_info(tp, intr, dev,
5185 "interrupt %04x in poll\n", status);
5188 /* We only get a new MSI interrupt when all active irq
5189 * sources on the chip have been acknowledged. So, ack
5190 * everything we've seen and check if new sources have become
5191 * active to avoid blocking all interrupts from the chip.
5194 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5195 status = RTL_R16(IntrStatus);
5198 return IRQ_RETVAL(handled);
5201 static int rtl8169_poll(struct napi_struct *napi, int budget)
5203 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5204 struct net_device *dev = tp->dev;
5205 void __iomem *ioaddr = tp->mmio_addr;
5208 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
5209 rtl8169_tx_interrupt(dev, tp, ioaddr);
5211 if (work_done < budget) {
5212 napi_complete(napi);
5214 /* We need for force the visibility of tp->intr_mask
5215 * for other CPUs, as we can loose an MSI interrupt
5216 * and potentially wait for a retransmit timeout if we don't.
5217 * The posted write to IntrMask is safe, as it will
5218 * eventually make it to the chip and we won't loose anything
5221 tp->intr_mask = 0xffff;
5223 RTL_W16(IntrMask, tp->intr_event);
5229 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5231 struct rtl8169_private *tp = netdev_priv(dev);
5233 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5236 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5237 RTL_W32(RxMissed, 0);
5240 static void rtl8169_down(struct net_device *dev)
5242 struct rtl8169_private *tp = netdev_priv(dev);
5243 void __iomem *ioaddr = tp->mmio_addr;
5245 del_timer_sync(&tp->timer);
5247 netif_stop_queue(dev);
5249 napi_disable(&tp->napi);
5251 spin_lock_irq(&tp->lock);
5253 rtl8169_asic_down(ioaddr);
5255 * At this point device interrupts can not be enabled in any function,
5256 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5257 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5259 rtl8169_rx_missed(dev, ioaddr);
5261 spin_unlock_irq(&tp->lock);
5263 synchronize_irq(dev->irq);
5265 /* Give a racing hard_start_xmit a few cycles to complete. */
5266 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
5268 rtl8169_tx_clear(tp);
5270 rtl8169_rx_clear(tp);
5272 rtl_pll_power_down(tp);
5275 static int rtl8169_close(struct net_device *dev)
5277 struct rtl8169_private *tp = netdev_priv(dev);
5278 struct pci_dev *pdev = tp->pci_dev;
5280 pm_runtime_get_sync(&pdev->dev);
5282 /* Update counters before going down */
5283 rtl8169_update_counters(dev);
5287 free_irq(dev->irq, dev);
5289 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5291 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5293 tp->TxDescArray = NULL;
5294 tp->RxDescArray = NULL;
5296 pm_runtime_put_sync(&pdev->dev);
5301 static void rtl_set_rx_mode(struct net_device *dev)
5303 struct rtl8169_private *tp = netdev_priv(dev);
5304 void __iomem *ioaddr = tp->mmio_addr;
5305 unsigned long flags;
5306 u32 mc_filter[2]; /* Multicast hash filter */
5310 if (dev->flags & IFF_PROMISC) {
5311 /* Unconditionally log net taps. */
5312 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5314 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5316 mc_filter[1] = mc_filter[0] = 0xffffffff;
5317 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5318 (dev->flags & IFF_ALLMULTI)) {
5319 /* Too many to filter perfectly -- accept all multicasts. */
5320 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5321 mc_filter[1] = mc_filter[0] = 0xffffffff;
5323 struct netdev_hw_addr *ha;
5325 rx_mode = AcceptBroadcast | AcceptMyPhys;
5326 mc_filter[1] = mc_filter[0] = 0;
5327 netdev_for_each_mc_addr(ha, dev) {
5328 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5329 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5330 rx_mode |= AcceptMulticast;
5334 spin_lock_irqsave(&tp->lock, flags);
5336 tmp = rtl8169_rx_config | rx_mode |
5337 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
5339 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5340 u32 data = mc_filter[0];
5342 mc_filter[0] = swab32(mc_filter[1]);
5343 mc_filter[1] = swab32(data);
5346 RTL_W32(MAR0 + 4, mc_filter[1]);
5347 RTL_W32(MAR0 + 0, mc_filter[0]);
5349 RTL_W32(RxConfig, tmp);
5351 spin_unlock_irqrestore(&tp->lock, flags);
5355 * rtl8169_get_stats - Get rtl8169 read/write statistics
5356 * @dev: The Ethernet Device to get statistics for
5358 * Get TX/RX statistics for rtl8169
5360 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5362 struct rtl8169_private *tp = netdev_priv(dev);
5363 void __iomem *ioaddr = tp->mmio_addr;
5364 unsigned long flags;
5366 if (netif_running(dev)) {
5367 spin_lock_irqsave(&tp->lock, flags);
5368 rtl8169_rx_missed(dev, ioaddr);
5369 spin_unlock_irqrestore(&tp->lock, flags);
5375 static void rtl8169_net_suspend(struct net_device *dev)
5377 struct rtl8169_private *tp = netdev_priv(dev);
5379 if (!netif_running(dev))
5382 rtl_pll_power_down(tp);
5384 netif_device_detach(dev);
5385 netif_stop_queue(dev);
5390 static int rtl8169_suspend(struct device *device)
5392 struct pci_dev *pdev = to_pci_dev(device);
5393 struct net_device *dev = pci_get_drvdata(pdev);
5395 rtl8169_net_suspend(dev);
5400 static void __rtl8169_resume(struct net_device *dev)
5402 struct rtl8169_private *tp = netdev_priv(dev);
5404 netif_device_attach(dev);
5406 rtl_pll_power_up(tp);
5408 rtl8169_schedule_work(dev, rtl8169_reset_task);
5411 static int rtl8169_resume(struct device *device)
5413 struct pci_dev *pdev = to_pci_dev(device);
5414 struct net_device *dev = pci_get_drvdata(pdev);
5415 struct rtl8169_private *tp = netdev_priv(dev);
5417 rtl8169_init_phy(dev, tp);
5419 if (netif_running(dev))
5420 __rtl8169_resume(dev);
5425 static int rtl8169_runtime_suspend(struct device *device)
5427 struct pci_dev *pdev = to_pci_dev(device);
5428 struct net_device *dev = pci_get_drvdata(pdev);
5429 struct rtl8169_private *tp = netdev_priv(dev);
5431 if (!tp->TxDescArray)
5434 spin_lock_irq(&tp->lock);
5435 tp->saved_wolopts = __rtl8169_get_wol(tp);
5436 __rtl8169_set_wol(tp, WAKE_ANY);
5437 spin_unlock_irq(&tp->lock);
5439 rtl8169_net_suspend(dev);
5444 static int rtl8169_runtime_resume(struct device *device)
5446 struct pci_dev *pdev = to_pci_dev(device);
5447 struct net_device *dev = pci_get_drvdata(pdev);
5448 struct rtl8169_private *tp = netdev_priv(dev);
5450 if (!tp->TxDescArray)
5453 spin_lock_irq(&tp->lock);
5454 __rtl8169_set_wol(tp, tp->saved_wolopts);
5455 tp->saved_wolopts = 0;
5456 spin_unlock_irq(&tp->lock);
5458 rtl8169_init_phy(dev, tp);
5460 __rtl8169_resume(dev);
5465 static int rtl8169_runtime_idle(struct device *device)
5467 struct pci_dev *pdev = to_pci_dev(device);
5468 struct net_device *dev = pci_get_drvdata(pdev);
5469 struct rtl8169_private *tp = netdev_priv(dev);
5471 return tp->TxDescArray ? -EBUSY : 0;
5474 static const struct dev_pm_ops rtl8169_pm_ops = {
5475 .suspend = rtl8169_suspend,
5476 .resume = rtl8169_resume,
5477 .freeze = rtl8169_suspend,
5478 .thaw = rtl8169_resume,
5479 .poweroff = rtl8169_suspend,
5480 .restore = rtl8169_resume,
5481 .runtime_suspend = rtl8169_runtime_suspend,
5482 .runtime_resume = rtl8169_runtime_resume,
5483 .runtime_idle = rtl8169_runtime_idle,
5486 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
5488 #else /* !CONFIG_PM */
5490 #define RTL8169_PM_OPS NULL
5492 #endif /* !CONFIG_PM */
5494 static void rtl_shutdown(struct pci_dev *pdev)
5496 struct net_device *dev = pci_get_drvdata(pdev);
5497 struct rtl8169_private *tp = netdev_priv(dev);
5498 void __iomem *ioaddr = tp->mmio_addr;
5500 rtl8169_net_suspend(dev);
5502 /* Restore original MAC address */
5503 rtl_rar_set(tp, dev->perm_addr);
5505 spin_lock_irq(&tp->lock);
5507 rtl8169_asic_down(ioaddr);
5509 spin_unlock_irq(&tp->lock);
5511 if (system_state == SYSTEM_POWER_OFF) {
5512 /* WoL fails with some 8168 when the receiver is disabled. */
5513 if (tp->features & RTL_FEATURE_WOL) {
5514 pci_clear_master(pdev);
5516 RTL_W8(ChipCmd, CmdRxEnb);
5521 pci_wake_from_d3(pdev, true);
5522 pci_set_power_state(pdev, PCI_D3hot);
5526 static struct pci_driver rtl8169_pci_driver = {
5528 .id_table = rtl8169_pci_tbl,
5529 .probe = rtl8169_init_one,
5530 .remove = __devexit_p(rtl8169_remove_one),
5531 .shutdown = rtl_shutdown,
5532 .driver.pm = RTL8169_PM_OPS,
5535 static int __init rtl8169_init_module(void)
5537 return pci_register_driver(&rtl8169_pci_driver);
5540 static void __exit rtl8169_cleanup_module(void)
5542 pci_unregister_driver(&rtl8169_pci_driver);
5545 module_init(rtl8169_init_module);
5546 module_exit(rtl8169_cleanup_module);