2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/if_vlan.h>
20 #include <linux/crc32.h>
23 #include <linux/tcp.h>
24 #include <linux/init.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/firmware.h>
28 #include <linux/pci-aspm.h>
30 #include <asm/system.h>
34 #define RTL8169_VERSION "2.3LK-NAPI"
35 #define MODULENAME "r8169"
36 #define PFX MODULENAME ": "
38 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
40 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
43 #define assert(expr) \
45 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
46 #expr,__FILE__,__func__,__LINE__); \
48 #define dprintk(fmt, args...) \
49 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
51 #define assert(expr) do {} while (0)
52 #define dprintk(fmt, args...) do {} while (0)
53 #endif /* RTL8169_DEBUG */
55 #define R8169_MSG_DEFAULT \
56 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
58 #define TX_BUFFS_AVAIL(tp) \
59 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
61 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
62 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
63 static const int multicast_filter_limit
= 32;
65 /* MAC address length */
66 #define MAC_ADDR_LEN 6
68 #define MAX_READ_REQUEST_SHIFT 12
69 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
70 #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
71 #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
72 #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
73 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
75 #define R8169_REGS_SIZE 256
76 #define R8169_NAPI_WEIGHT 64
77 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
78 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
79 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
80 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
81 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
83 #define RTL8169_TX_TIMEOUT (6*HZ)
84 #define RTL8169_PHY_TIMEOUT (10*HZ)
86 #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
87 #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
88 #define RTL_EEPROM_SIG_ADDR 0x0000
90 /* write/read MMIO register */
91 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
92 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
93 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
94 #define RTL_R8(reg) readb (ioaddr + (reg))
95 #define RTL_R16(reg) readw (ioaddr + (reg))
96 #define RTL_R32(reg) readl (ioaddr + (reg))
99 RTL_GIGA_MAC_NONE
= 0x00,
100 RTL_GIGA_MAC_VER_01
= 0x01, // 8169
101 RTL_GIGA_MAC_VER_02
= 0x02, // 8169S
102 RTL_GIGA_MAC_VER_03
= 0x03, // 8110S
103 RTL_GIGA_MAC_VER_04
= 0x04, // 8169SB
104 RTL_GIGA_MAC_VER_05
= 0x05, // 8110SCd
105 RTL_GIGA_MAC_VER_06
= 0x06, // 8110SCe
106 RTL_GIGA_MAC_VER_07
= 0x07, // 8102e
107 RTL_GIGA_MAC_VER_08
= 0x08, // 8102e
108 RTL_GIGA_MAC_VER_09
= 0x09, // 8102e
109 RTL_GIGA_MAC_VER_10
= 0x0a, // 8101e
110 RTL_GIGA_MAC_VER_11
= 0x0b, // 8168Bb
111 RTL_GIGA_MAC_VER_12
= 0x0c, // 8168Be
112 RTL_GIGA_MAC_VER_13
= 0x0d, // 8101Eb
113 RTL_GIGA_MAC_VER_14
= 0x0e, // 8101 ?
114 RTL_GIGA_MAC_VER_15
= 0x0f, // 8101 ?
115 RTL_GIGA_MAC_VER_16
= 0x11, // 8101Ec
116 RTL_GIGA_MAC_VER_17
= 0x10, // 8168Bf
117 RTL_GIGA_MAC_VER_18
= 0x12, // 8168CP
118 RTL_GIGA_MAC_VER_19
= 0x13, // 8168C
119 RTL_GIGA_MAC_VER_20
= 0x14, // 8168C
120 RTL_GIGA_MAC_VER_21
= 0x15, // 8168C
121 RTL_GIGA_MAC_VER_22
= 0x16, // 8168C
122 RTL_GIGA_MAC_VER_23
= 0x17, // 8168CP
123 RTL_GIGA_MAC_VER_24
= 0x18, // 8168CP
124 RTL_GIGA_MAC_VER_25
= 0x19, // 8168D
125 RTL_GIGA_MAC_VER_26
= 0x1a, // 8168D
126 RTL_GIGA_MAC_VER_27
= 0x1b, // 8168DP
127 RTL_GIGA_MAC_VER_28
= 0x1c, // 8168DP
128 RTL_GIGA_MAC_VER_29
= 0x1d, // 8105E
129 RTL_GIGA_MAC_VER_30
= 0x1e, // 8105E
132 #define _R(NAME,MAC,MASK) \
133 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
135 static const struct {
138 u32 RxConfigMask
; /* Clears the bits supported by this chip */
139 } rtl_chip_info
[] = {
140 _R("RTL8169", RTL_GIGA_MAC_VER_01
, 0xff7e1880), // 8169
141 _R("RTL8169s", RTL_GIGA_MAC_VER_02
, 0xff7e1880), // 8169S
142 _R("RTL8110s", RTL_GIGA_MAC_VER_03
, 0xff7e1880), // 8110S
143 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04
, 0xff7e1880), // 8169SB
144 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05
, 0xff7e1880), // 8110SCd
145 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06
, 0xff7e1880), // 8110SCe
146 _R("RTL8102e", RTL_GIGA_MAC_VER_07
, 0xff7e1880), // PCI-E
147 _R("RTL8102e", RTL_GIGA_MAC_VER_08
, 0xff7e1880), // PCI-E
148 _R("RTL8102e", RTL_GIGA_MAC_VER_09
, 0xff7e1880), // PCI-E
149 _R("RTL8101e", RTL_GIGA_MAC_VER_10
, 0xff7e1880), // PCI-E
150 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11
, 0xff7e1880), // PCI-E
151 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12
, 0xff7e1880), // PCI-E
152 _R("RTL8101e", RTL_GIGA_MAC_VER_13
, 0xff7e1880), // PCI-E 8139
153 _R("RTL8100e", RTL_GIGA_MAC_VER_14
, 0xff7e1880), // PCI-E 8139
154 _R("RTL8100e", RTL_GIGA_MAC_VER_15
, 0xff7e1880), // PCI-E 8139
155 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17
, 0xff7e1880), // PCI-E
156 _R("RTL8101e", RTL_GIGA_MAC_VER_16
, 0xff7e1880), // PCI-E
157 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18
, 0xff7e1880), // PCI-E
158 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19
, 0xff7e1880), // PCI-E
159 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20
, 0xff7e1880), // PCI-E
160 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21
, 0xff7e1880), // PCI-E
161 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22
, 0xff7e1880), // PCI-E
162 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23
, 0xff7e1880), // PCI-E
163 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24
, 0xff7e1880), // PCI-E
164 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25
, 0xff7e1880), // PCI-E
165 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26
, 0xff7e1880), // PCI-E
166 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27
, 0xff7e1880), // PCI-E
167 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28
, 0xff7e1880), // PCI-E
168 _R("RTL8105e", RTL_GIGA_MAC_VER_29
, 0xff7e1880), // PCI-E
169 _R("RTL8105e", RTL_GIGA_MAC_VER_30
, 0xff7e1880) // PCI-E
173 static const struct rtl_firmware_info
{
176 } rtl_firmware_infos
[] = {
177 { .mac_version
= RTL_GIGA_MAC_VER_25
, .fw_name
= FIRMWARE_8168D_1
},
178 { .mac_version
= RTL_GIGA_MAC_VER_26
, .fw_name
= FIRMWARE_8168D_2
},
179 { .mac_version
= RTL_GIGA_MAC_VER_29
, .fw_name
= FIRMWARE_8105E_1
},
180 { .mac_version
= RTL_GIGA_MAC_VER_30
, .fw_name
= FIRMWARE_8105E_1
}
189 static void rtl_hw_start_8169(struct net_device
*);
190 static void rtl_hw_start_8168(struct net_device
*);
191 static void rtl_hw_start_8101(struct net_device
*);
193 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl
) = {
194 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x8129), 0, 0, RTL_CFG_0
},
195 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x8136), 0, 0, RTL_CFG_2
},
196 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x8167), 0, 0, RTL_CFG_0
},
197 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x8168), 0, 0, RTL_CFG_1
},
198 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK
, 0x8169), 0, 0, RTL_CFG_0
},
199 { PCI_DEVICE(PCI_VENDOR_ID_DLINK
, 0x4300), 0, 0, RTL_CFG_0
},
200 { PCI_DEVICE(PCI_VENDOR_ID_AT
, 0xc107), 0, 0, RTL_CFG_0
},
201 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0
},
202 { PCI_VENDOR_ID_LINKSYS
, 0x1032,
203 PCI_ANY_ID
, 0x0024, 0, 0, RTL_CFG_0
},
205 PCI_ANY_ID
, 0x2410, 0, 0, RTL_CFG_2
},
209 MODULE_DEVICE_TABLE(pci
, rtl8169_pci_tbl
);
211 static int rx_buf_sz
= 16383;
218 MAC0
= 0, /* Ethernet hardware address. */
220 MAR0
= 8, /* Multicast filter. */
221 CounterAddrLow
= 0x10,
222 CounterAddrHigh
= 0x14,
223 TxDescStartAddrLow
= 0x20,
224 TxDescStartAddrHigh
= 0x24,
225 TxHDescStartAddrLow
= 0x28,
226 TxHDescStartAddrHigh
= 0x2c,
249 RxDescAddrLow
= 0xe4,
250 RxDescAddrHigh
= 0xe8,
251 EarlyTxThres
= 0xec, /* 8169. Unit of 32 bytes. */
253 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
255 MaxTxPacketSize
= 0xec, /* 8101/8168. Unit of 128 bytes. */
257 #define TxPacketMax (8064 >> 7)
260 FuncEventMask
= 0xf4,
261 FuncPresetState
= 0xf8,
262 FuncForceEvent
= 0xfc,
265 enum rtl8110_registers
{
271 enum rtl8168_8101_registers
{
274 #define CSIAR_FLAG 0x80000000
275 #define CSIAR_WRITE_CMD 0x80000000
276 #define CSIAR_BYTE_ENABLE 0x0f
277 #define CSIAR_BYTE_ENABLE_SHIFT 12
278 #define CSIAR_ADDR_MASK 0x0fff
281 #define EPHYAR_FLAG 0x80000000
282 #define EPHYAR_WRITE_CMD 0x80000000
283 #define EPHYAR_REG_MASK 0x1f
284 #define EPHYAR_REG_SHIFT 16
285 #define EPHYAR_DATA_MASK 0xffff
287 #define PM_SWITCH (1 << 6)
289 #define FIX_NAK_1 (1 << 4)
290 #define FIX_NAK_2 (1 << 3)
293 #define EN_NDP (1 << 3)
294 #define EN_OOB_RESET (1 << 2)
296 #define EFUSEAR_FLAG 0x80000000
297 #define EFUSEAR_WRITE_CMD 0x80000000
298 #define EFUSEAR_READ_CMD 0x00000000
299 #define EFUSEAR_REG_MASK 0x03ff
300 #define EFUSEAR_REG_SHIFT 8
301 #define EFUSEAR_DATA_MASK 0xff
304 enum rtl8168_registers
{
307 #define ERIAR_FLAG 0x80000000
308 #define ERIAR_WRITE_CMD 0x80000000
309 #define ERIAR_READ_CMD 0x00000000
310 #define ERIAR_ADDR_BYTE_ALIGN 4
311 #define ERIAR_EXGMAC 0
314 #define ERIAR_TYPE_SHIFT 16
315 #define ERIAR_BYTEEN 0x0f
316 #define ERIAR_BYTEEN_SHIFT 12
317 EPHY_RXER_NUM
= 0x7c,
318 OCPDR
= 0xb0, /* OCP GPHY access */
319 #define OCPDR_WRITE_CMD 0x80000000
320 #define OCPDR_READ_CMD 0x00000000
321 #define OCPDR_REG_MASK 0x7f
322 #define OCPDR_GPHY_REG_SHIFT 16
323 #define OCPDR_DATA_MASK 0xffff
325 #define OCPAR_FLAG 0x80000000
326 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
327 #define OCPAR_GPHY_READ_CMD 0x0000f060
328 RDSAR1
= 0xd0 /* 8168c only. Undocumented on 8168dp */
331 enum rtl_register_content
{
332 /* InterruptStatusBits */
336 TxDescUnavail
= 0x0080,
358 /* TXPoll register p.5 */
359 HPQ
= 0x80, /* Poll cmd on the high prio queue */
360 NPQ
= 0x40, /* Poll cmd on the low prio queue */
361 FSWInt
= 0x01, /* Forced software interrupt */
365 Cfg9346_Unlock
= 0xc0,
370 AcceptBroadcast
= 0x08,
371 AcceptMulticast
= 0x04,
373 AcceptAllPhys
= 0x01,
380 TxInterFrameGapShift
= 24,
381 TxDMAShift
= 8, /* DMA burst value (0-7) is shift this many bits */
383 /* Config1 register p.24 */
386 MSIEnable
= (1 << 5), /* Enable Message Signaled Interrupt */
387 Speed_down
= (1 << 4),
391 PMEnable
= (1 << 0), /* Power Management Enable */
393 /* Config2 register p. 25 */
394 PCI_Clock_66MHz
= 0x01,
395 PCI_Clock_33MHz
= 0x00,
397 /* Config3 register p.25 */
398 MagicPacket
= (1 << 5), /* Wake up when receives a Magic Packet */
399 LinkUp
= (1 << 4), /* Wake up when the cable connection is re-established */
400 Beacon_en
= (1 << 0), /* 8168 only. Reserved in the 8168b */
402 /* Config5 register p.27 */
403 BWF
= (1 << 6), /* Accept Broadcast wakeup frame */
404 MWF
= (1 << 5), /* Accept Multicast wakeup frame */
405 UWF
= (1 << 4), /* Accept Unicast wakeup frame */
406 LanWake
= (1 << 1), /* LanWake enable/disable */
407 PMEStatus
= (1 << 0), /* PME status can be reset by PCI RST# */
410 TBIReset
= 0x80000000,
411 TBILoopback
= 0x40000000,
412 TBINwEnable
= 0x20000000,
413 TBINwRestart
= 0x10000000,
414 TBILinkOk
= 0x02000000,
415 TBINwComplete
= 0x01000000,
418 EnableBist
= (1 << 15), // 8168 8101
419 Mac_dbgo_oe
= (1 << 14), // 8168 8101
420 Normal_mode
= (1 << 13), // unused
421 Force_half_dup
= (1 << 12), // 8168 8101
422 Force_rxflow_en
= (1 << 11), // 8168 8101
423 Force_txflow_en
= (1 << 10), // 8168 8101
424 Cxpl_dbg_sel
= (1 << 9), // 8168 8101
425 ASF
= (1 << 8), // 8168 8101
426 PktCntrDisable
= (1 << 7), // 8168 8101
427 Mac_dbgo_sel
= 0x001c, // 8168
432 INTT_0
= 0x0000, // 8168
433 INTT_1
= 0x0001, // 8168
434 INTT_2
= 0x0002, // 8168
435 INTT_3
= 0x0003, // 8168
437 /* rtl8169_PHYstatus */
448 TBILinkOK
= 0x02000000,
450 /* DumpCounterCommand */
454 enum desc_status_bit
{
455 DescOwn
= (1 << 31), /* Descriptor is owned by NIC */
456 RingEnd
= (1 << 30), /* End of descriptor ring */
457 FirstFrag
= (1 << 29), /* First segment of a packet */
458 LastFrag
= (1 << 28), /* Final segment of a packet */
461 LargeSend
= (1 << 27), /* TCP Large Send Offload (TSO) */
462 MSSShift
= 16, /* MSS value position */
463 MSSMask
= 0xfff, /* MSS value + LargeSend bit: 12 bits */
464 IPCS
= (1 << 18), /* Calculate IP checksum */
465 UDPCS
= (1 << 17), /* Calculate UDP/IP checksum */
466 TCPCS
= (1 << 16), /* Calculate TCP/IP checksum */
467 TxVlanTag
= (1 << 17), /* Add VLAN tag */
470 PID1
= (1 << 18), /* Protocol ID bit 1/2 */
471 PID0
= (1 << 17), /* Protocol ID bit 2/2 */
473 #define RxProtoUDP (PID1)
474 #define RxProtoTCP (PID0)
475 #define RxProtoIP (PID1 | PID0)
476 #define RxProtoMask RxProtoIP
478 IPFail
= (1 << 16), /* IP checksum failed */
479 UDPFail
= (1 << 15), /* UDP/IP checksum failed */
480 TCPFail
= (1 << 14), /* TCP/IP checksum failed */
481 RxVlanTag
= (1 << 16), /* VLAN tag available */
484 #define RsvdMask 0x3fffc000
501 u8 __pad
[sizeof(void *) - sizeof(u32
)];
505 RTL_FEATURE_WOL
= (1 << 0),
506 RTL_FEATURE_MSI
= (1 << 1),
507 RTL_FEATURE_GMII
= (1 << 2),
510 struct rtl8169_counters
{
517 __le32 tx_one_collision
;
518 __le32 tx_multi_collision
;
526 struct rtl8169_private
{
527 void __iomem
*mmio_addr
; /* memory map physical address */
528 struct pci_dev
*pci_dev
; /* Index of PCI device */
529 struct net_device
*dev
;
530 struct napi_struct napi
;
531 spinlock_t lock
; /* spin lock flag */
535 u32 cur_rx
; /* Index into the Rx descriptor buffer of next Rx pkt. */
536 u32 cur_tx
; /* Index into the Tx descriptor buffer of next Rx pkt. */
539 struct TxDesc
*TxDescArray
; /* 256-aligned Tx descriptor ring */
540 struct RxDesc
*RxDescArray
; /* 256-aligned Rx descriptor ring */
541 dma_addr_t TxPhyAddr
;
542 dma_addr_t RxPhyAddr
;
543 void *Rx_databuff
[NUM_RX_DESC
]; /* Rx data buffers */
544 struct ring_info tx_skb
[NUM_TX_DESC
]; /* Tx data buffers */
545 struct timer_list timer
;
550 int phy_1000_ctrl_reg
;
553 void (*write
)(void __iomem
*, int, int);
554 int (*read
)(void __iomem
*, int);
557 struct pll_power_ops
{
558 void (*down
)(struct rtl8169_private
*);
559 void (*up
)(struct rtl8169_private
*);
562 int (*set_speed
)(struct net_device
*, u8 aneg
, u16 sp
, u8 dpx
, u32 adv
);
563 int (*get_settings
)(struct net_device
*, struct ethtool_cmd
*);
564 void (*phy_reset_enable
)(struct rtl8169_private
*tp
);
565 void (*hw_start
)(struct net_device
*);
566 unsigned int (*phy_reset_pending
)(struct rtl8169_private
*tp
);
567 unsigned int (*link_ok
)(void __iomem
*);
568 int (*do_ioctl
)(struct rtl8169_private
*tp
, struct mii_ioctl_data
*data
, int cmd
);
570 struct delayed_work task
;
573 struct mii_if_info mii
;
574 struct rtl8169_counters counters
;
577 const struct firmware
*fw
;
578 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
581 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
582 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
583 module_param(use_dac
, int, 0);
584 MODULE_PARM_DESC(use_dac
, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
585 module_param_named(debug
, debug
.msg_enable
, int, 0);
586 MODULE_PARM_DESC(debug
, "Debug verbosity level (0=none, ..., 16=all)");
587 MODULE_LICENSE("GPL");
588 MODULE_VERSION(RTL8169_VERSION
);
589 MODULE_FIRMWARE(FIRMWARE_8168D_1
);
590 MODULE_FIRMWARE(FIRMWARE_8168D_2
);
591 MODULE_FIRMWARE(FIRMWARE_8105E_1
);
593 static int rtl8169_open(struct net_device
*dev
);
594 static netdev_tx_t
rtl8169_start_xmit(struct sk_buff
*skb
,
595 struct net_device
*dev
);
596 static irqreturn_t
rtl8169_interrupt(int irq
, void *dev_instance
);
597 static int rtl8169_init_ring(struct net_device
*dev
);
598 static void rtl_hw_start(struct net_device
*dev
);
599 static int rtl8169_close(struct net_device
*dev
);
600 static void rtl_set_rx_mode(struct net_device
*dev
);
601 static void rtl8169_tx_timeout(struct net_device
*dev
);
602 static struct net_device_stats
*rtl8169_get_stats(struct net_device
*dev
);
603 static int rtl8169_rx_interrupt(struct net_device
*, struct rtl8169_private
*,
604 void __iomem
*, u32 budget
);
605 static int rtl8169_change_mtu(struct net_device
*dev
, int new_mtu
);
606 static void rtl8169_down(struct net_device
*dev
);
607 static void rtl8169_rx_clear(struct rtl8169_private
*tp
);
608 static int rtl8169_poll(struct napi_struct
*napi
, int budget
);
610 static const unsigned int rtl8169_rx_config
=
611 (RX_FIFO_THRESH
<< RxCfgFIFOShift
) | (RX_DMA_BURST
<< RxCfgDMAShift
);
613 static u32
ocp_read(struct rtl8169_private
*tp
, u8 mask
, u16 reg
)
615 void __iomem
*ioaddr
= tp
->mmio_addr
;
618 RTL_W32(OCPAR
, ((u32
)mask
& 0x0f) << 12 | (reg
& 0x0fff));
619 for (i
= 0; i
< 20; i
++) {
621 if (RTL_R32(OCPAR
) & OCPAR_FLAG
)
624 return RTL_R32(OCPDR
);
627 static void ocp_write(struct rtl8169_private
*tp
, u8 mask
, u16 reg
, u32 data
)
629 void __iomem
*ioaddr
= tp
->mmio_addr
;
632 RTL_W32(OCPDR
, data
);
633 RTL_W32(OCPAR
, OCPAR_FLAG
| ((u32
)mask
& 0x0f) << 12 | (reg
& 0x0fff));
634 for (i
= 0; i
< 20; i
++) {
636 if ((RTL_R32(OCPAR
) & OCPAR_FLAG
) == 0)
641 static void rtl8168_oob_notify(struct rtl8169_private
*tp
, u8 cmd
)
643 void __iomem
*ioaddr
= tp
->mmio_addr
;
647 RTL_W32(ERIAR
, 0x800010e8);
649 for (i
= 0; i
< 5; i
++) {
651 if (!(RTL_R32(ERIDR
) & ERIAR_FLAG
))
655 ocp_write(tp
, 0x1, 0x30, 0x00000001);
658 #define OOB_CMD_RESET 0x00
659 #define OOB_CMD_DRIVER_START 0x05
660 #define OOB_CMD_DRIVER_STOP 0x06
662 static void rtl8168_driver_start(struct rtl8169_private
*tp
)
666 rtl8168_oob_notify(tp
, OOB_CMD_DRIVER_START
);
668 for (i
= 0; i
< 10; i
++) {
670 if (ocp_read(tp
, 0x0f, 0x0010) & 0x00000800)
675 static void rtl8168_driver_stop(struct rtl8169_private
*tp
)
679 rtl8168_oob_notify(tp
, OOB_CMD_DRIVER_STOP
);
681 for (i
= 0; i
< 10; i
++) {
683 if ((ocp_read(tp
, 0x0f, 0x0010) & 0x00000800) == 0)
689 static void r8169_mdio_write(void __iomem
*ioaddr
, int reg_addr
, int value
)
693 RTL_W32(PHYAR
, 0x80000000 | (reg_addr
& 0x1f) << 16 | (value
& 0xffff));
695 for (i
= 20; i
> 0; i
--) {
697 * Check if the RTL8169 has completed writing to the specified
700 if (!(RTL_R32(PHYAR
) & 0x80000000))
705 * According to hardware specs a 20us delay is required after write
706 * complete indication, but before sending next command.
711 static int r8169_mdio_read(void __iomem
*ioaddr
, int reg_addr
)
715 RTL_W32(PHYAR
, 0x0 | (reg_addr
& 0x1f) << 16);
717 for (i
= 20; i
> 0; i
--) {
719 * Check if the RTL8169 has completed retrieving data from
720 * the specified MII register.
722 if (RTL_R32(PHYAR
) & 0x80000000) {
723 value
= RTL_R32(PHYAR
) & 0xffff;
729 * According to hardware specs a 20us delay is required after read
730 * complete indication, but before sending next command.
737 static void r8168dp_1_mdio_access(void __iomem
*ioaddr
, int reg_addr
, u32 data
)
741 RTL_W32(OCPDR
, data
|
742 ((reg_addr
& OCPDR_REG_MASK
) << OCPDR_GPHY_REG_SHIFT
));
743 RTL_W32(OCPAR
, OCPAR_GPHY_WRITE_CMD
);
744 RTL_W32(EPHY_RXER_NUM
, 0);
746 for (i
= 0; i
< 100; i
++) {
748 if (!(RTL_R32(OCPAR
) & OCPAR_FLAG
))
753 static void r8168dp_1_mdio_write(void __iomem
*ioaddr
, int reg_addr
, int value
)
755 r8168dp_1_mdio_access(ioaddr
, reg_addr
, OCPDR_WRITE_CMD
|
756 (value
& OCPDR_DATA_MASK
));
759 static int r8168dp_1_mdio_read(void __iomem
*ioaddr
, int reg_addr
)
763 r8168dp_1_mdio_access(ioaddr
, reg_addr
, OCPDR_READ_CMD
);
766 RTL_W32(OCPAR
, OCPAR_GPHY_READ_CMD
);
767 RTL_W32(EPHY_RXER_NUM
, 0);
769 for (i
= 0; i
< 100; i
++) {
771 if (RTL_R32(OCPAR
) & OCPAR_FLAG
)
775 return RTL_R32(OCPDR
) & OCPDR_DATA_MASK
;
778 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
780 static void r8168dp_2_mdio_start(void __iomem
*ioaddr
)
782 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT
);
785 static void r8168dp_2_mdio_stop(void __iomem
*ioaddr
)
787 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT
);
790 static void r8168dp_2_mdio_write(void __iomem
*ioaddr
, int reg_addr
, int value
)
792 r8168dp_2_mdio_start(ioaddr
);
794 r8169_mdio_write(ioaddr
, reg_addr
, value
);
796 r8168dp_2_mdio_stop(ioaddr
);
799 static int r8168dp_2_mdio_read(void __iomem
*ioaddr
, int reg_addr
)
803 r8168dp_2_mdio_start(ioaddr
);
805 value
= r8169_mdio_read(ioaddr
, reg_addr
);
807 r8168dp_2_mdio_stop(ioaddr
);
812 static void rtl_writephy(struct rtl8169_private
*tp
, int location
, u32 val
)
814 tp
->mdio_ops
.write(tp
->mmio_addr
, location
, val
);
817 static int rtl_readphy(struct rtl8169_private
*tp
, int location
)
819 return tp
->mdio_ops
.read(tp
->mmio_addr
, location
);
822 static void rtl_patchphy(struct rtl8169_private
*tp
, int reg_addr
, int value
)
824 rtl_writephy(tp
, reg_addr
, rtl_readphy(tp
, reg_addr
) | value
);
827 static void rtl_w1w0_phy(struct rtl8169_private
*tp
, int reg_addr
, int p
, int m
)
831 val
= rtl_readphy(tp
, reg_addr
);
832 rtl_writephy(tp
, reg_addr
, (val
| p
) & ~m
);
835 static void rtl_mdio_write(struct net_device
*dev
, int phy_id
, int location
,
838 struct rtl8169_private
*tp
= netdev_priv(dev
);
840 rtl_writephy(tp
, location
, val
);
843 static int rtl_mdio_read(struct net_device
*dev
, int phy_id
, int location
)
845 struct rtl8169_private
*tp
= netdev_priv(dev
);
847 return rtl_readphy(tp
, location
);
850 static void rtl_ephy_write(void __iomem
*ioaddr
, int reg_addr
, int value
)
854 RTL_W32(EPHYAR
, EPHYAR_WRITE_CMD
| (value
& EPHYAR_DATA_MASK
) |
855 (reg_addr
& EPHYAR_REG_MASK
) << EPHYAR_REG_SHIFT
);
857 for (i
= 0; i
< 100; i
++) {
858 if (!(RTL_R32(EPHYAR
) & EPHYAR_FLAG
))
864 static u16
rtl_ephy_read(void __iomem
*ioaddr
, int reg_addr
)
869 RTL_W32(EPHYAR
, (reg_addr
& EPHYAR_REG_MASK
) << EPHYAR_REG_SHIFT
);
871 for (i
= 0; i
< 100; i
++) {
872 if (RTL_R32(EPHYAR
) & EPHYAR_FLAG
) {
873 value
= RTL_R32(EPHYAR
) & EPHYAR_DATA_MASK
;
882 static void rtl_csi_write(void __iomem
*ioaddr
, int addr
, int value
)
886 RTL_W32(CSIDR
, value
);
887 RTL_W32(CSIAR
, CSIAR_WRITE_CMD
| (addr
& CSIAR_ADDR_MASK
) |
888 CSIAR_BYTE_ENABLE
<< CSIAR_BYTE_ENABLE_SHIFT
);
890 for (i
= 0; i
< 100; i
++) {
891 if (!(RTL_R32(CSIAR
) & CSIAR_FLAG
))
897 static u32
rtl_csi_read(void __iomem
*ioaddr
, int addr
)
902 RTL_W32(CSIAR
, (addr
& CSIAR_ADDR_MASK
) |
903 CSIAR_BYTE_ENABLE
<< CSIAR_BYTE_ENABLE_SHIFT
);
905 for (i
= 0; i
< 100; i
++) {
906 if (RTL_R32(CSIAR
) & CSIAR_FLAG
) {
907 value
= RTL_R32(CSIDR
);
916 static u8
rtl8168d_efuse_read(void __iomem
*ioaddr
, int reg_addr
)
921 RTL_W32(EFUSEAR
, (reg_addr
& EFUSEAR_REG_MASK
) << EFUSEAR_REG_SHIFT
);
923 for (i
= 0; i
< 300; i
++) {
924 if (RTL_R32(EFUSEAR
) & EFUSEAR_FLAG
) {
925 value
= RTL_R32(EFUSEAR
) & EFUSEAR_DATA_MASK
;
934 static void rtl8169_irq_mask_and_ack(void __iomem
*ioaddr
)
936 RTL_W16(IntrMask
, 0x0000);
938 RTL_W16(IntrStatus
, 0xffff);
941 static void rtl8169_asic_down(void __iomem
*ioaddr
)
943 RTL_W8(ChipCmd
, 0x00);
944 rtl8169_irq_mask_and_ack(ioaddr
);
948 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private
*tp
)
950 void __iomem
*ioaddr
= tp
->mmio_addr
;
952 return RTL_R32(TBICSR
) & TBIReset
;
955 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private
*tp
)
957 return rtl_readphy(tp
, MII_BMCR
) & BMCR_RESET
;
960 static unsigned int rtl8169_tbi_link_ok(void __iomem
*ioaddr
)
962 return RTL_R32(TBICSR
) & TBILinkOk
;
965 static unsigned int rtl8169_xmii_link_ok(void __iomem
*ioaddr
)
967 return RTL_R8(PHYstatus
) & LinkStatus
;
970 static void rtl8169_tbi_reset_enable(struct rtl8169_private
*tp
)
972 void __iomem
*ioaddr
= tp
->mmio_addr
;
974 RTL_W32(TBICSR
, RTL_R32(TBICSR
) | TBIReset
);
977 static void rtl8169_xmii_reset_enable(struct rtl8169_private
*tp
)
981 val
= rtl_readphy(tp
, MII_BMCR
) | BMCR_RESET
;
982 rtl_writephy(tp
, MII_BMCR
, val
& 0xffff);
985 static void __rtl8169_check_link_status(struct net_device
*dev
,
986 struct rtl8169_private
*tp
,
987 void __iomem
*ioaddr
,
992 spin_lock_irqsave(&tp
->lock
, flags
);
993 if (tp
->link_ok(ioaddr
)) {
994 /* This is to cancel a scheduled suspend if there's one. */
996 pm_request_resume(&tp
->pci_dev
->dev
);
997 netif_carrier_on(dev
);
999 netif_info(tp
, ifup
, dev
, "link up\n");
1001 netif_carrier_off(dev
);
1002 netif_info(tp
, ifdown
, dev
, "link down\n");
1004 pm_schedule_suspend(&tp
->pci_dev
->dev
, 100);
1006 spin_unlock_irqrestore(&tp
->lock
, flags
);
1009 static void rtl8169_check_link_status(struct net_device
*dev
,
1010 struct rtl8169_private
*tp
,
1011 void __iomem
*ioaddr
)
1013 __rtl8169_check_link_status(dev
, tp
, ioaddr
, false);
1016 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1018 static u32
__rtl8169_get_wol(struct rtl8169_private
*tp
)
1020 void __iomem
*ioaddr
= tp
->mmio_addr
;
1024 options
= RTL_R8(Config1
);
1025 if (!(options
& PMEnable
))
1028 options
= RTL_R8(Config3
);
1029 if (options
& LinkUp
)
1030 wolopts
|= WAKE_PHY
;
1031 if (options
& MagicPacket
)
1032 wolopts
|= WAKE_MAGIC
;
1034 options
= RTL_R8(Config5
);
1036 wolopts
|= WAKE_UCAST
;
1038 wolopts
|= WAKE_BCAST
;
1040 wolopts
|= WAKE_MCAST
;
1045 static void rtl8169_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
1047 struct rtl8169_private
*tp
= netdev_priv(dev
);
1049 spin_lock_irq(&tp
->lock
);
1051 wol
->supported
= WAKE_ANY
;
1052 wol
->wolopts
= __rtl8169_get_wol(tp
);
1054 spin_unlock_irq(&tp
->lock
);
1057 static void __rtl8169_set_wol(struct rtl8169_private
*tp
, u32 wolopts
)
1059 void __iomem
*ioaddr
= tp
->mmio_addr
;
1061 static const struct {
1066 { WAKE_ANY
, Config1
, PMEnable
},
1067 { WAKE_PHY
, Config3
, LinkUp
},
1068 { WAKE_MAGIC
, Config3
, MagicPacket
},
1069 { WAKE_UCAST
, Config5
, UWF
},
1070 { WAKE_BCAST
, Config5
, BWF
},
1071 { WAKE_MCAST
, Config5
, MWF
},
1072 { WAKE_ANY
, Config5
, LanWake
}
1075 RTL_W8(Cfg9346
, Cfg9346_Unlock
);
1077 for (i
= 0; i
< ARRAY_SIZE(cfg
); i
++) {
1078 u8 options
= RTL_R8(cfg
[i
].reg
) & ~cfg
[i
].mask
;
1079 if (wolopts
& cfg
[i
].opt
)
1080 options
|= cfg
[i
].mask
;
1081 RTL_W8(cfg
[i
].reg
, options
);
1084 RTL_W8(Cfg9346
, Cfg9346_Lock
);
1087 static int rtl8169_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
1089 struct rtl8169_private
*tp
= netdev_priv(dev
);
1091 spin_lock_irq(&tp
->lock
);
1094 tp
->features
|= RTL_FEATURE_WOL
;
1096 tp
->features
&= ~RTL_FEATURE_WOL
;
1097 __rtl8169_set_wol(tp
, wol
->wolopts
);
1098 spin_unlock_irq(&tp
->lock
);
1100 device_set_wakeup_enable(&tp
->pci_dev
->dev
, wol
->wolopts
);
1105 static void rtl8169_get_drvinfo(struct net_device
*dev
,
1106 struct ethtool_drvinfo
*info
)
1108 struct rtl8169_private
*tp
= netdev_priv(dev
);
1110 strcpy(info
->driver
, MODULENAME
);
1111 strcpy(info
->version
, RTL8169_VERSION
);
1112 strcpy(info
->bus_info
, pci_name(tp
->pci_dev
));
1115 static int rtl8169_get_regs_len(struct net_device
*dev
)
1117 return R8169_REGS_SIZE
;
1120 static int rtl8169_set_speed_tbi(struct net_device
*dev
,
1121 u8 autoneg
, u16 speed
, u8 duplex
, u32 ignored
)
1123 struct rtl8169_private
*tp
= netdev_priv(dev
);
1124 void __iomem
*ioaddr
= tp
->mmio_addr
;
1128 reg
= RTL_R32(TBICSR
);
1129 if ((autoneg
== AUTONEG_DISABLE
) && (speed
== SPEED_1000
) &&
1130 (duplex
== DUPLEX_FULL
)) {
1131 RTL_W32(TBICSR
, reg
& ~(TBINwEnable
| TBINwRestart
));
1132 } else if (autoneg
== AUTONEG_ENABLE
)
1133 RTL_W32(TBICSR
, reg
| TBINwEnable
| TBINwRestart
);
1135 netif_warn(tp
, link
, dev
,
1136 "incorrect speed setting refused in TBI mode\n");
1143 static int rtl8169_set_speed_xmii(struct net_device
*dev
,
1144 u8 autoneg
, u16 speed
, u8 duplex
, u32 adv
)
1146 struct rtl8169_private
*tp
= netdev_priv(dev
);
1147 int giga_ctrl
, bmcr
;
1150 rtl_writephy(tp
, 0x1f, 0x0000);
1152 if (autoneg
== AUTONEG_ENABLE
) {
1155 auto_nego
= rtl_readphy(tp
, MII_ADVERTISE
);
1156 auto_nego
&= ~(ADVERTISE_10HALF
| ADVERTISE_10FULL
|
1157 ADVERTISE_100HALF
| ADVERTISE_100FULL
);
1159 if (adv
& ADVERTISED_10baseT_Half
)
1160 auto_nego
|= ADVERTISE_10HALF
;
1161 if (adv
& ADVERTISED_10baseT_Full
)
1162 auto_nego
|= ADVERTISE_10FULL
;
1163 if (adv
& ADVERTISED_100baseT_Half
)
1164 auto_nego
|= ADVERTISE_100HALF
;
1165 if (adv
& ADVERTISED_100baseT_Full
)
1166 auto_nego
|= ADVERTISE_100FULL
;
1168 auto_nego
|= ADVERTISE_PAUSE_CAP
| ADVERTISE_PAUSE_ASYM
;
1170 giga_ctrl
= rtl_readphy(tp
, MII_CTRL1000
);
1171 giga_ctrl
&= ~(ADVERTISE_1000FULL
| ADVERTISE_1000HALF
);
1173 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1174 if ((tp
->mac_version
!= RTL_GIGA_MAC_VER_07
) &&
1175 (tp
->mac_version
!= RTL_GIGA_MAC_VER_08
) &&
1176 (tp
->mac_version
!= RTL_GIGA_MAC_VER_09
) &&
1177 (tp
->mac_version
!= RTL_GIGA_MAC_VER_10
) &&
1178 (tp
->mac_version
!= RTL_GIGA_MAC_VER_13
) &&
1179 (tp
->mac_version
!= RTL_GIGA_MAC_VER_14
) &&
1180 (tp
->mac_version
!= RTL_GIGA_MAC_VER_15
) &&
1181 (tp
->mac_version
!= RTL_GIGA_MAC_VER_16
) &&
1182 (tp
->mac_version
!= RTL_GIGA_MAC_VER_29
) &&
1183 (tp
->mac_version
!= RTL_GIGA_MAC_VER_30
)) {
1184 if (adv
& ADVERTISED_1000baseT_Half
)
1185 giga_ctrl
|= ADVERTISE_1000HALF
;
1186 if (adv
& ADVERTISED_1000baseT_Full
)
1187 giga_ctrl
|= ADVERTISE_1000FULL
;
1188 } else if (adv
& (ADVERTISED_1000baseT_Half
|
1189 ADVERTISED_1000baseT_Full
)) {
1190 netif_info(tp
, link
, dev
,
1191 "PHY does not support 1000Mbps\n");
1195 bmcr
= BMCR_ANENABLE
| BMCR_ANRESTART
;
1197 rtl_writephy(tp
, MII_ADVERTISE
, auto_nego
);
1198 rtl_writephy(tp
, MII_CTRL1000
, giga_ctrl
);
1202 if (speed
== SPEED_10
)
1204 else if (speed
== SPEED_100
)
1205 bmcr
= BMCR_SPEED100
;
1209 if (duplex
== DUPLEX_FULL
)
1210 bmcr
|= BMCR_FULLDPLX
;
1213 tp
->phy_1000_ctrl_reg
= giga_ctrl
;
1215 rtl_writephy(tp
, MII_BMCR
, bmcr
);
1217 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_02
) ||
1218 (tp
->mac_version
== RTL_GIGA_MAC_VER_03
)) {
1219 if ((speed
== SPEED_100
) && (autoneg
!= AUTONEG_ENABLE
)) {
1220 rtl_writephy(tp
, 0x17, 0x2138);
1221 rtl_writephy(tp
, 0x0e, 0x0260);
1223 rtl_writephy(tp
, 0x17, 0x2108);
1224 rtl_writephy(tp
, 0x0e, 0x0000);
1233 static int rtl8169_set_speed(struct net_device
*dev
,
1234 u8 autoneg
, u16 speed
, u8 duplex
, u32 advertising
)
1236 struct rtl8169_private
*tp
= netdev_priv(dev
);
1239 ret
= tp
->set_speed(dev
, autoneg
, speed
, duplex
, advertising
);
1241 if (netif_running(dev
) && (tp
->phy_1000_ctrl_reg
& ADVERTISE_1000FULL
))
1242 mod_timer(&tp
->timer
, jiffies
+ RTL8169_PHY_TIMEOUT
);
1247 static int rtl8169_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1249 struct rtl8169_private
*tp
= netdev_priv(dev
);
1250 unsigned long flags
;
1253 spin_lock_irqsave(&tp
->lock
, flags
);
1254 ret
= rtl8169_set_speed(dev
,
1255 cmd
->autoneg
, cmd
->speed
, cmd
->duplex
, cmd
->advertising
);
1256 spin_unlock_irqrestore(&tp
->lock
, flags
);
1261 static u32
rtl8169_get_rx_csum(struct net_device
*dev
)
1263 struct rtl8169_private
*tp
= netdev_priv(dev
);
1265 return tp
->cp_cmd
& RxChkSum
;
1268 static int rtl8169_set_rx_csum(struct net_device
*dev
, u32 data
)
1270 struct rtl8169_private
*tp
= netdev_priv(dev
);
1271 void __iomem
*ioaddr
= tp
->mmio_addr
;
1272 unsigned long flags
;
1274 spin_lock_irqsave(&tp
->lock
, flags
);
1277 tp
->cp_cmd
|= RxChkSum
;
1279 tp
->cp_cmd
&= ~RxChkSum
;
1281 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
1284 spin_unlock_irqrestore(&tp
->lock
, flags
);
1289 static inline u32
rtl8169_tx_vlan_tag(struct rtl8169_private
*tp
,
1290 struct sk_buff
*skb
)
1292 return (vlan_tx_tag_present(skb
)) ?
1293 TxVlanTag
| swab16(vlan_tx_tag_get(skb
)) : 0x00;
1296 #define NETIF_F_HW_VLAN_TX_RX (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX)
1298 static void rtl8169_vlan_mode(struct net_device
*dev
)
1300 struct rtl8169_private
*tp
= netdev_priv(dev
);
1301 void __iomem
*ioaddr
= tp
->mmio_addr
;
1302 unsigned long flags
;
1304 spin_lock_irqsave(&tp
->lock
, flags
);
1305 if (dev
->features
& NETIF_F_HW_VLAN_RX
)
1306 tp
->cp_cmd
|= RxVlan
;
1308 tp
->cp_cmd
&= ~RxVlan
;
1309 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
1312 spin_unlock_irqrestore(&tp
->lock
, flags
);
1314 dev
->vlan_features
= dev
->features
&~ NETIF_F_HW_VLAN_TX_RX
;
1317 static void rtl8169_rx_vlan_tag(struct RxDesc
*desc
, struct sk_buff
*skb
)
1319 u32 opts2
= le32_to_cpu(desc
->opts2
);
1321 if (opts2
& RxVlanTag
)
1322 __vlan_hwaccel_put_tag(skb
, swab16(opts2
& 0xffff));
1327 static int rtl8169_gset_tbi(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1329 struct rtl8169_private
*tp
= netdev_priv(dev
);
1330 void __iomem
*ioaddr
= tp
->mmio_addr
;
1334 SUPPORTED_1000baseT_Full
| SUPPORTED_Autoneg
| SUPPORTED_FIBRE
;
1335 cmd
->port
= PORT_FIBRE
;
1336 cmd
->transceiver
= XCVR_INTERNAL
;
1338 status
= RTL_R32(TBICSR
);
1339 cmd
->advertising
= (status
& TBINwEnable
) ? ADVERTISED_Autoneg
: 0;
1340 cmd
->autoneg
= !!(status
& TBINwEnable
);
1342 cmd
->speed
= SPEED_1000
;
1343 cmd
->duplex
= DUPLEX_FULL
; /* Always set */
1348 static int rtl8169_gset_xmii(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1350 struct rtl8169_private
*tp
= netdev_priv(dev
);
1352 return mii_ethtool_gset(&tp
->mii
, cmd
);
1355 static int rtl8169_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1357 struct rtl8169_private
*tp
= netdev_priv(dev
);
1358 unsigned long flags
;
1361 spin_lock_irqsave(&tp
->lock
, flags
);
1363 rc
= tp
->get_settings(dev
, cmd
);
1365 spin_unlock_irqrestore(&tp
->lock
, flags
);
1369 static void rtl8169_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
1372 struct rtl8169_private
*tp
= netdev_priv(dev
);
1373 unsigned long flags
;
1375 if (regs
->len
> R8169_REGS_SIZE
)
1376 regs
->len
= R8169_REGS_SIZE
;
1378 spin_lock_irqsave(&tp
->lock
, flags
);
1379 memcpy_fromio(p
, tp
->mmio_addr
, regs
->len
);
1380 spin_unlock_irqrestore(&tp
->lock
, flags
);
1383 static u32
rtl8169_get_msglevel(struct net_device
*dev
)
1385 struct rtl8169_private
*tp
= netdev_priv(dev
);
1387 return tp
->msg_enable
;
1390 static void rtl8169_set_msglevel(struct net_device
*dev
, u32 value
)
1392 struct rtl8169_private
*tp
= netdev_priv(dev
);
1394 tp
->msg_enable
= value
;
1397 static const char rtl8169_gstrings
[][ETH_GSTRING_LEN
] = {
1404 "tx_single_collisions",
1405 "tx_multi_collisions",
1413 static int rtl8169_get_sset_count(struct net_device
*dev
, int sset
)
1417 return ARRAY_SIZE(rtl8169_gstrings
);
1423 static void rtl8169_update_counters(struct net_device
*dev
)
1425 struct rtl8169_private
*tp
= netdev_priv(dev
);
1426 void __iomem
*ioaddr
= tp
->mmio_addr
;
1427 struct rtl8169_counters
*counters
;
1431 struct device
*d
= &tp
->pci_dev
->dev
;
1434 * Some chips are unable to dump tally counters when the receiver
1437 if ((RTL_R8(ChipCmd
) & CmdRxEnb
) == 0)
1440 counters
= dma_alloc_coherent(d
, sizeof(*counters
), &paddr
, GFP_KERNEL
);
1444 RTL_W32(CounterAddrHigh
, (u64
)paddr
>> 32);
1445 cmd
= (u64
)paddr
& DMA_BIT_MASK(32);
1446 RTL_W32(CounterAddrLow
, cmd
);
1447 RTL_W32(CounterAddrLow
, cmd
| CounterDump
);
1450 if ((RTL_R32(CounterAddrLow
) & CounterDump
) == 0) {
1451 /* copy updated counters */
1452 memcpy(&tp
->counters
, counters
, sizeof(*counters
));
1458 RTL_W32(CounterAddrLow
, 0);
1459 RTL_W32(CounterAddrHigh
, 0);
1461 dma_free_coherent(d
, sizeof(*counters
), counters
, paddr
);
1464 static void rtl8169_get_ethtool_stats(struct net_device
*dev
,
1465 struct ethtool_stats
*stats
, u64
*data
)
1467 struct rtl8169_private
*tp
= netdev_priv(dev
);
1471 rtl8169_update_counters(dev
);
1473 data
[0] = le64_to_cpu(tp
->counters
.tx_packets
);
1474 data
[1] = le64_to_cpu(tp
->counters
.rx_packets
);
1475 data
[2] = le64_to_cpu(tp
->counters
.tx_errors
);
1476 data
[3] = le32_to_cpu(tp
->counters
.rx_errors
);
1477 data
[4] = le16_to_cpu(tp
->counters
.rx_missed
);
1478 data
[5] = le16_to_cpu(tp
->counters
.align_errors
);
1479 data
[6] = le32_to_cpu(tp
->counters
.tx_one_collision
);
1480 data
[7] = le32_to_cpu(tp
->counters
.tx_multi_collision
);
1481 data
[8] = le64_to_cpu(tp
->counters
.rx_unicast
);
1482 data
[9] = le64_to_cpu(tp
->counters
.rx_broadcast
);
1483 data
[10] = le32_to_cpu(tp
->counters
.rx_multicast
);
1484 data
[11] = le16_to_cpu(tp
->counters
.tx_aborted
);
1485 data
[12] = le16_to_cpu(tp
->counters
.tx_underun
);
1488 static void rtl8169_get_strings(struct net_device
*dev
, u32 stringset
, u8
*data
)
1492 memcpy(data
, *rtl8169_gstrings
, sizeof(rtl8169_gstrings
));
1497 static int rtl8169_set_flags(struct net_device
*dev
, u32 data
)
1499 struct rtl8169_private
*tp
= netdev_priv(dev
);
1500 unsigned long old_feat
= dev
->features
;
1503 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_05
) &&
1504 !(data
& ETH_FLAG_RXVLAN
)) {
1505 netif_info(tp
, drv
, dev
, "8110SCd requires hardware Rx VLAN\n");
1509 rc
= ethtool_op_set_flags(dev
, data
, ETH_FLAG_TXVLAN
| ETH_FLAG_RXVLAN
);
1513 if ((old_feat
^ dev
->features
) & NETIF_F_HW_VLAN_RX
)
1514 rtl8169_vlan_mode(dev
);
1519 static const struct ethtool_ops rtl8169_ethtool_ops
= {
1520 .get_drvinfo
= rtl8169_get_drvinfo
,
1521 .get_regs_len
= rtl8169_get_regs_len
,
1522 .get_link
= ethtool_op_get_link
,
1523 .get_settings
= rtl8169_get_settings
,
1524 .set_settings
= rtl8169_set_settings
,
1525 .get_msglevel
= rtl8169_get_msglevel
,
1526 .set_msglevel
= rtl8169_set_msglevel
,
1527 .get_rx_csum
= rtl8169_get_rx_csum
,
1528 .set_rx_csum
= rtl8169_set_rx_csum
,
1529 .set_tx_csum
= ethtool_op_set_tx_csum
,
1530 .set_sg
= ethtool_op_set_sg
,
1531 .set_tso
= ethtool_op_set_tso
,
1532 .get_regs
= rtl8169_get_regs
,
1533 .get_wol
= rtl8169_get_wol
,
1534 .set_wol
= rtl8169_set_wol
,
1535 .get_strings
= rtl8169_get_strings
,
1536 .get_sset_count
= rtl8169_get_sset_count
,
1537 .get_ethtool_stats
= rtl8169_get_ethtool_stats
,
1538 .set_flags
= rtl8169_set_flags
,
1539 .get_flags
= ethtool_op_get_flags
,
1542 static void rtl8169_get_mac_version(struct rtl8169_private
*tp
,
1543 void __iomem
*ioaddr
)
1546 * The driver currently handles the 8168Bf and the 8168Be identically
1547 * but they can be identified more specifically through the test below
1550 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1552 * Same thing for the 8101Eb and the 8101Ec:
1554 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1556 static const struct {
1562 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26
},
1563 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25
},
1564 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26
},
1566 /* 8168DP family. */
1567 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27
},
1568 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28
},
1571 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24
},
1572 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23
},
1573 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18
},
1574 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24
},
1575 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19
},
1576 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20
},
1577 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21
},
1578 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22
},
1579 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22
},
1582 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12
},
1583 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17
},
1584 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17
},
1585 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11
},
1588 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30
},
1589 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29
},
1590 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30
},
1591 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09
},
1592 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09
},
1593 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08
},
1594 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08
},
1595 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07
},
1596 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07
},
1597 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13
},
1598 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10
},
1599 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16
},
1600 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09
},
1601 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09
},
1602 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16
},
1603 /* FIXME: where did these entries come from ? -- FR */
1604 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15
},
1605 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14
},
1608 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06
},
1609 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05
},
1610 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04
},
1611 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03
},
1612 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02
},
1613 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01
},
1616 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE
}
1620 reg
= RTL_R32(TxConfig
);
1621 while ((reg
& p
->mask
) != p
->val
)
1623 tp
->mac_version
= p
->mac_version
;
1626 static void rtl8169_print_mac_version(struct rtl8169_private
*tp
)
1628 dprintk("mac_version = 0x%02x\n", tp
->mac_version
);
1636 static void rtl_writephy_batch(struct rtl8169_private
*tp
,
1637 const struct phy_reg
*regs
, int len
)
1640 rtl_writephy(tp
, regs
->reg
, regs
->val
);
1645 #define PHY_READ 0x00000000
1646 #define PHY_DATA_OR 0x10000000
1647 #define PHY_DATA_AND 0x20000000
1648 #define PHY_BJMPN 0x30000000
1649 #define PHY_READ_EFUSE 0x40000000
1650 #define PHY_READ_MAC_BYTE 0x50000000
1651 #define PHY_WRITE_MAC_BYTE 0x60000000
1652 #define PHY_CLEAR_READCOUNT 0x70000000
1653 #define PHY_WRITE 0x80000000
1654 #define PHY_READCOUNT_EQ_SKIP 0x90000000
1655 #define PHY_COMP_EQ_SKIPN 0xa0000000
1656 #define PHY_COMP_NEQ_SKIPN 0xb0000000
1657 #define PHY_WRITE_PREVIOUS 0xc0000000
1658 #define PHY_SKIPN 0xd0000000
1659 #define PHY_DELAY_MS 0xe0000000
1660 #define PHY_WRITE_ERI_WORD 0xf0000000
1663 rtl_phy_write_fw(struct rtl8169_private
*tp
, const struct firmware
*fw
)
1665 __le32
*phytable
= (__le32
*)fw
->data
;
1666 struct net_device
*dev
= tp
->dev
;
1667 size_t index
, fw_size
= fw
->size
/ sizeof(*phytable
);
1670 if (fw
->size
% sizeof(*phytable
)) {
1671 netif_err(tp
, probe
, dev
, "odd sized firmware %zd\n", fw
->size
);
1675 for (index
= 0; index
< fw_size
; index
++) {
1676 u32 action
= le32_to_cpu(phytable
[index
]);
1677 u32 regno
= (action
& 0x0fff0000) >> 16;
1679 switch(action
& 0xf0000000) {
1683 case PHY_READ_EFUSE
:
1684 case PHY_CLEAR_READCOUNT
:
1686 case PHY_WRITE_PREVIOUS
:
1691 if (regno
> index
) {
1692 netif_err(tp
, probe
, tp
->dev
,
1693 "Out of range of firmware\n");
1697 case PHY_READCOUNT_EQ_SKIP
:
1698 if (index
+ 2 >= fw_size
) {
1699 netif_err(tp
, probe
, tp
->dev
,
1700 "Out of range of firmware\n");
1704 case PHY_COMP_EQ_SKIPN
:
1705 case PHY_COMP_NEQ_SKIPN
:
1707 if (index
+ 1 + regno
>= fw_size
) {
1708 netif_err(tp
, probe
, tp
->dev
,
1709 "Out of range of firmware\n");
1714 case PHY_READ_MAC_BYTE
:
1715 case PHY_WRITE_MAC_BYTE
:
1716 case PHY_WRITE_ERI_WORD
:
1718 netif_err(tp
, probe
, tp
->dev
,
1719 "Invalid action 0x%08x\n", action
);
1727 for (index
= 0; index
< fw_size
; ) {
1728 u32 action
= le32_to_cpu(phytable
[index
]);
1729 u32 data
= action
& 0x0000ffff;
1730 u32 regno
= (action
& 0x0fff0000) >> 16;
1735 switch(action
& 0xf0000000) {
1737 predata
= rtl_readphy(tp
, regno
);
1752 case PHY_READ_EFUSE
:
1753 predata
= rtl8168d_efuse_read(tp
->mmio_addr
, regno
);
1756 case PHY_CLEAR_READCOUNT
:
1761 rtl_writephy(tp
, regno
, data
);
1764 case PHY_READCOUNT_EQ_SKIP
:
1770 case PHY_COMP_EQ_SKIPN
:
1771 if (predata
== data
)
1775 case PHY_COMP_NEQ_SKIPN
:
1776 if (predata
!= data
)
1780 case PHY_WRITE_PREVIOUS
:
1781 rtl_writephy(tp
, regno
, predata
);
1792 case PHY_READ_MAC_BYTE
:
1793 case PHY_WRITE_MAC_BYTE
:
1794 case PHY_WRITE_ERI_WORD
:
1801 static void rtl_release_firmware(struct rtl8169_private
*tp
)
1803 if (!IS_ERR_OR_NULL(tp
->fw
))
1804 release_firmware(tp
->fw
);
1805 tp
->fw
= RTL_FIRMWARE_UNKNOWN
;
1808 static void rtl_apply_firmware(struct rtl8169_private
*tp
)
1810 const struct firmware
*fw
= tp
->fw
;
1812 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1813 if (!IS_ERR_OR_NULL(fw
))
1814 rtl_phy_write_fw(tp
, fw
);
1817 static void rtl_apply_firmware_cond(struct rtl8169_private
*tp
, u8 reg
, u16 val
)
1819 if (rtl_readphy(tp
, reg
) != val
)
1820 netif_warn(tp
, hw
, tp
->dev
, "chipset not ready for firmware\n");
1822 rtl_apply_firmware(tp
);
1825 static void rtl8169s_hw_phy_config(struct rtl8169_private
*tp
)
1827 static const struct phy_reg phy_reg_init
[] = {
1889 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
1892 static void rtl8169sb_hw_phy_config(struct rtl8169_private
*tp
)
1894 static const struct phy_reg phy_reg_init
[] = {
1900 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
1903 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private
*tp
)
1905 struct pci_dev
*pdev
= tp
->pci_dev
;
1906 u16 vendor_id
, device_id
;
1908 pci_read_config_word(pdev
, PCI_SUBSYSTEM_VENDOR_ID
, &vendor_id
);
1909 pci_read_config_word(pdev
, PCI_SUBSYSTEM_ID
, &device_id
);
1911 if ((vendor_id
!= PCI_VENDOR_ID_GIGABYTE
) || (device_id
!= 0xe000))
1914 rtl_writephy(tp
, 0x1f, 0x0001);
1915 rtl_writephy(tp
, 0x10, 0xf01b);
1916 rtl_writephy(tp
, 0x1f, 0x0000);
1919 static void rtl8169scd_hw_phy_config(struct rtl8169_private
*tp
)
1921 static const struct phy_reg phy_reg_init
[] = {
1961 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
1963 rtl8169scd_hw_phy_config_quirk(tp
);
1966 static void rtl8169sce_hw_phy_config(struct rtl8169_private
*tp
)
1968 static const struct phy_reg phy_reg_init
[] = {
2016 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2019 static void rtl8168bb_hw_phy_config(struct rtl8169_private
*tp
)
2021 static const struct phy_reg phy_reg_init
[] = {
2026 rtl_writephy(tp
, 0x1f, 0x0001);
2027 rtl_patchphy(tp
, 0x16, 1 << 0);
2029 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2032 static void rtl8168bef_hw_phy_config(struct rtl8169_private
*tp
)
2034 static const struct phy_reg phy_reg_init
[] = {
2040 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2043 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private
*tp
)
2045 static const struct phy_reg phy_reg_init
[] = {
2053 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2056 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private
*tp
)
2058 static const struct phy_reg phy_reg_init
[] = {
2064 rtl_writephy(tp
, 0x1f, 0x0000);
2065 rtl_patchphy(tp
, 0x14, 1 << 5);
2066 rtl_patchphy(tp
, 0x0d, 1 << 5);
2068 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2071 static void rtl8168c_1_hw_phy_config(struct rtl8169_private
*tp
)
2073 static const struct phy_reg phy_reg_init
[] = {
2093 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2095 rtl_patchphy(tp
, 0x14, 1 << 5);
2096 rtl_patchphy(tp
, 0x0d, 1 << 5);
2097 rtl_writephy(tp
, 0x1f, 0x0000);
2100 static void rtl8168c_2_hw_phy_config(struct rtl8169_private
*tp
)
2102 static const struct phy_reg phy_reg_init
[] = {
2120 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2122 rtl_patchphy(tp
, 0x16, 1 << 0);
2123 rtl_patchphy(tp
, 0x14, 1 << 5);
2124 rtl_patchphy(tp
, 0x0d, 1 << 5);
2125 rtl_writephy(tp
, 0x1f, 0x0000);
2128 static void rtl8168c_3_hw_phy_config(struct rtl8169_private
*tp
)
2130 static const struct phy_reg phy_reg_init
[] = {
2142 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2144 rtl_patchphy(tp
, 0x16, 1 << 0);
2145 rtl_patchphy(tp
, 0x14, 1 << 5);
2146 rtl_patchphy(tp
, 0x0d, 1 << 5);
2147 rtl_writephy(tp
, 0x1f, 0x0000);
2150 static void rtl8168c_4_hw_phy_config(struct rtl8169_private
*tp
)
2152 rtl8168c_3_hw_phy_config(tp
);
2155 static void rtl8168d_1_hw_phy_config(struct rtl8169_private
*tp
)
2157 static const struct phy_reg phy_reg_init_0
[] = {
2158 /* Channel Estimation */
2179 * enhance line driver power
2188 * Can not link to 1Gbps with bad cable
2189 * Decrease SNR threshold form 21.07dB to 19.04dB
2197 void __iomem
*ioaddr
= tp
->mmio_addr
;
2199 rtl_writephy_batch(tp
, phy_reg_init_0
, ARRAY_SIZE(phy_reg_init_0
));
2203 * Fine Tune Switching regulator parameter
2205 rtl_writephy(tp
, 0x1f, 0x0002);
2206 rtl_w1w0_phy(tp
, 0x0b, 0x0010, 0x00ef);
2207 rtl_w1w0_phy(tp
, 0x0c, 0xa200, 0x5d00);
2209 if (rtl8168d_efuse_read(ioaddr
, 0x01) == 0xb1) {
2210 static const struct phy_reg phy_reg_init
[] = {
2220 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2222 val
= rtl_readphy(tp
, 0x0d);
2224 if ((val
& 0x00ff) != 0x006c) {
2225 static const u32 set
[] = {
2226 0x0065, 0x0066, 0x0067, 0x0068,
2227 0x0069, 0x006a, 0x006b, 0x006c
2231 rtl_writephy(tp
, 0x1f, 0x0002);
2234 for (i
= 0; i
< ARRAY_SIZE(set
); i
++)
2235 rtl_writephy(tp
, 0x0d, val
| set
[i
]);
2238 static const struct phy_reg phy_reg_init
[] = {
2246 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2249 /* RSET couple improve */
2250 rtl_writephy(tp
, 0x1f, 0x0002);
2251 rtl_patchphy(tp
, 0x0d, 0x0300);
2252 rtl_patchphy(tp
, 0x0f, 0x0010);
2254 /* Fine tune PLL performance */
2255 rtl_writephy(tp
, 0x1f, 0x0002);
2256 rtl_w1w0_phy(tp
, 0x02, 0x0100, 0x0600);
2257 rtl_w1w0_phy(tp
, 0x03, 0x0000, 0xe000);
2259 rtl_writephy(tp
, 0x1f, 0x0005);
2260 rtl_writephy(tp
, 0x05, 0x001b);
2262 rtl_apply_firmware_cond(tp
, MII_EXPANSION
, 0xbf00);
2264 rtl_writephy(tp
, 0x1f, 0x0000);
2267 static void rtl8168d_2_hw_phy_config(struct rtl8169_private
*tp
)
2269 static const struct phy_reg phy_reg_init_0
[] = {
2270 /* Channel Estimation */
2291 * enhance line driver power
2300 * Can not link to 1Gbps with bad cable
2301 * Decrease SNR threshold form 21.07dB to 19.04dB
2309 void __iomem
*ioaddr
= tp
->mmio_addr
;
2311 rtl_writephy_batch(tp
, phy_reg_init_0
, ARRAY_SIZE(phy_reg_init_0
));
2313 if (rtl8168d_efuse_read(ioaddr
, 0x01) == 0xb1) {
2314 static const struct phy_reg phy_reg_init
[] = {
2325 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2327 val
= rtl_readphy(tp
, 0x0d);
2328 if ((val
& 0x00ff) != 0x006c) {
2329 static const u32 set
[] = {
2330 0x0065, 0x0066, 0x0067, 0x0068,
2331 0x0069, 0x006a, 0x006b, 0x006c
2335 rtl_writephy(tp
, 0x1f, 0x0002);
2338 for (i
= 0; i
< ARRAY_SIZE(set
); i
++)
2339 rtl_writephy(tp
, 0x0d, val
| set
[i
]);
2342 static const struct phy_reg phy_reg_init
[] = {
2350 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2353 /* Fine tune PLL performance */
2354 rtl_writephy(tp
, 0x1f, 0x0002);
2355 rtl_w1w0_phy(tp
, 0x02, 0x0100, 0x0600);
2356 rtl_w1w0_phy(tp
, 0x03, 0x0000, 0xe000);
2358 /* Switching regulator Slew rate */
2359 rtl_writephy(tp
, 0x1f, 0x0002);
2360 rtl_patchphy(tp
, 0x0f, 0x0017);
2362 rtl_writephy(tp
, 0x1f, 0x0005);
2363 rtl_writephy(tp
, 0x05, 0x001b);
2365 rtl_apply_firmware_cond(tp
, MII_EXPANSION
, 0xb300);
2367 rtl_writephy(tp
, 0x1f, 0x0000);
2370 static void rtl8168d_3_hw_phy_config(struct rtl8169_private
*tp
)
2372 static const struct phy_reg phy_reg_init
[] = {
2428 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2431 static void rtl8168d_4_hw_phy_config(struct rtl8169_private
*tp
)
2433 static const struct phy_reg phy_reg_init
[] = {
2443 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2444 rtl_patchphy(tp
, 0x0d, 1 << 5);
2447 static void rtl8102e_hw_phy_config(struct rtl8169_private
*tp
)
2449 static const struct phy_reg phy_reg_init
[] = {
2456 rtl_writephy(tp
, 0x1f, 0x0000);
2457 rtl_patchphy(tp
, 0x11, 1 << 12);
2458 rtl_patchphy(tp
, 0x19, 1 << 13);
2459 rtl_patchphy(tp
, 0x10, 1 << 15);
2461 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2464 static void rtl8105e_hw_phy_config(struct rtl8169_private
*tp
)
2466 static const struct phy_reg phy_reg_init
[] = {
2480 /* Disable ALDPS before ram code */
2481 rtl_writephy(tp
, 0x1f, 0x0000);
2482 rtl_writephy(tp
, 0x18, 0x0310);
2485 rtl_apply_firmware(tp
);
2487 rtl_writephy_batch(tp
, phy_reg_init
, ARRAY_SIZE(phy_reg_init
));
2490 static void rtl_hw_phy_config(struct net_device
*dev
)
2492 struct rtl8169_private
*tp
= netdev_priv(dev
);
2494 rtl8169_print_mac_version(tp
);
2496 switch (tp
->mac_version
) {
2497 case RTL_GIGA_MAC_VER_01
:
2499 case RTL_GIGA_MAC_VER_02
:
2500 case RTL_GIGA_MAC_VER_03
:
2501 rtl8169s_hw_phy_config(tp
);
2503 case RTL_GIGA_MAC_VER_04
:
2504 rtl8169sb_hw_phy_config(tp
);
2506 case RTL_GIGA_MAC_VER_05
:
2507 rtl8169scd_hw_phy_config(tp
);
2509 case RTL_GIGA_MAC_VER_06
:
2510 rtl8169sce_hw_phy_config(tp
);
2512 case RTL_GIGA_MAC_VER_07
:
2513 case RTL_GIGA_MAC_VER_08
:
2514 case RTL_GIGA_MAC_VER_09
:
2515 rtl8102e_hw_phy_config(tp
);
2517 case RTL_GIGA_MAC_VER_11
:
2518 rtl8168bb_hw_phy_config(tp
);
2520 case RTL_GIGA_MAC_VER_12
:
2521 rtl8168bef_hw_phy_config(tp
);
2523 case RTL_GIGA_MAC_VER_17
:
2524 rtl8168bef_hw_phy_config(tp
);
2526 case RTL_GIGA_MAC_VER_18
:
2527 rtl8168cp_1_hw_phy_config(tp
);
2529 case RTL_GIGA_MAC_VER_19
:
2530 rtl8168c_1_hw_phy_config(tp
);
2532 case RTL_GIGA_MAC_VER_20
:
2533 rtl8168c_2_hw_phy_config(tp
);
2535 case RTL_GIGA_MAC_VER_21
:
2536 rtl8168c_3_hw_phy_config(tp
);
2538 case RTL_GIGA_MAC_VER_22
:
2539 rtl8168c_4_hw_phy_config(tp
);
2541 case RTL_GIGA_MAC_VER_23
:
2542 case RTL_GIGA_MAC_VER_24
:
2543 rtl8168cp_2_hw_phy_config(tp
);
2545 case RTL_GIGA_MAC_VER_25
:
2546 rtl8168d_1_hw_phy_config(tp
);
2548 case RTL_GIGA_MAC_VER_26
:
2549 rtl8168d_2_hw_phy_config(tp
);
2551 case RTL_GIGA_MAC_VER_27
:
2552 rtl8168d_3_hw_phy_config(tp
);
2554 case RTL_GIGA_MAC_VER_28
:
2555 rtl8168d_4_hw_phy_config(tp
);
2557 case RTL_GIGA_MAC_VER_29
:
2558 case RTL_GIGA_MAC_VER_30
:
2559 rtl8105e_hw_phy_config(tp
);
2567 static void rtl8169_phy_timer(unsigned long __opaque
)
2569 struct net_device
*dev
= (struct net_device
*)__opaque
;
2570 struct rtl8169_private
*tp
= netdev_priv(dev
);
2571 struct timer_list
*timer
= &tp
->timer
;
2572 void __iomem
*ioaddr
= tp
->mmio_addr
;
2573 unsigned long timeout
= RTL8169_PHY_TIMEOUT
;
2575 assert(tp
->mac_version
> RTL_GIGA_MAC_VER_01
);
2577 if (!(tp
->phy_1000_ctrl_reg
& ADVERTISE_1000FULL
))
2580 spin_lock_irq(&tp
->lock
);
2582 if (tp
->phy_reset_pending(tp
)) {
2584 * A busy loop could burn quite a few cycles on nowadays CPU.
2585 * Let's delay the execution of the timer for a few ticks.
2591 if (tp
->link_ok(ioaddr
))
2594 netif_warn(tp
, link
, dev
, "PHY reset until link up\n");
2596 tp
->phy_reset_enable(tp
);
2599 mod_timer(timer
, jiffies
+ timeout
);
2601 spin_unlock_irq(&tp
->lock
);
2604 static inline void rtl8169_delete_timer(struct net_device
*dev
)
2606 struct rtl8169_private
*tp
= netdev_priv(dev
);
2607 struct timer_list
*timer
= &tp
->timer
;
2609 if (tp
->mac_version
<= RTL_GIGA_MAC_VER_01
)
2612 del_timer_sync(timer
);
2615 static inline void rtl8169_request_timer(struct net_device
*dev
)
2617 struct rtl8169_private
*tp
= netdev_priv(dev
);
2618 struct timer_list
*timer
= &tp
->timer
;
2620 if (tp
->mac_version
<= RTL_GIGA_MAC_VER_01
)
2623 mod_timer(timer
, jiffies
+ RTL8169_PHY_TIMEOUT
);
2626 #ifdef CONFIG_NET_POLL_CONTROLLER
2628 * Polling 'interrupt' - used by things like netconsole to send skbs
2629 * without having to re-enable interrupts. It's not called while
2630 * the interrupt routine is executing.
2632 static void rtl8169_netpoll(struct net_device
*dev
)
2634 struct rtl8169_private
*tp
= netdev_priv(dev
);
2635 struct pci_dev
*pdev
= tp
->pci_dev
;
2637 disable_irq(pdev
->irq
);
2638 rtl8169_interrupt(pdev
->irq
, dev
);
2639 enable_irq(pdev
->irq
);
2643 static void rtl8169_release_board(struct pci_dev
*pdev
, struct net_device
*dev
,
2644 void __iomem
*ioaddr
)
2647 pci_release_regions(pdev
);
2648 pci_clear_mwi(pdev
);
2649 pci_disable_device(pdev
);
2653 static void rtl8169_phy_reset(struct net_device
*dev
,
2654 struct rtl8169_private
*tp
)
2658 tp
->phy_reset_enable(tp
);
2659 for (i
= 0; i
< 100; i
++) {
2660 if (!tp
->phy_reset_pending(tp
))
2664 netif_err(tp
, link
, dev
, "PHY reset failed\n");
2667 static void rtl8169_init_phy(struct net_device
*dev
, struct rtl8169_private
*tp
)
2669 void __iomem
*ioaddr
= tp
->mmio_addr
;
2671 rtl_hw_phy_config(dev
);
2673 if (tp
->mac_version
<= RTL_GIGA_MAC_VER_06
) {
2674 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2678 pci_write_config_byte(tp
->pci_dev
, PCI_LATENCY_TIMER
, 0x40);
2680 if (tp
->mac_version
<= RTL_GIGA_MAC_VER_06
)
2681 pci_write_config_byte(tp
->pci_dev
, PCI_CACHE_LINE_SIZE
, 0x08);
2683 if (tp
->mac_version
== RTL_GIGA_MAC_VER_02
) {
2684 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2686 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
2687 rtl_writephy(tp
, 0x0b, 0x0000); //w 0x0b 15 0 0
2690 rtl8169_phy_reset(dev
, tp
);
2692 rtl8169_set_speed(dev
, AUTONEG_ENABLE
, SPEED_1000
, DUPLEX_FULL
,
2693 ADVERTISED_10baseT_Half
| ADVERTISED_10baseT_Full
|
2694 ADVERTISED_100baseT_Half
| ADVERTISED_100baseT_Full
|
2695 (tp
->mii
.supports_gmii
?
2696 ADVERTISED_1000baseT_Half
|
2697 ADVERTISED_1000baseT_Full
: 0));
2699 if (RTL_R8(PHYstatus
) & TBI_Enable
)
2700 netif_info(tp
, link
, dev
, "TBI auto-negotiating\n");
2703 static void rtl_rar_set(struct rtl8169_private
*tp
, u8
*addr
)
2705 void __iomem
*ioaddr
= tp
->mmio_addr
;
2709 low
= addr
[0] | (addr
[1] << 8) | (addr
[2] << 16) | (addr
[3] << 24);
2710 high
= addr
[4] | (addr
[5] << 8);
2712 spin_lock_irq(&tp
->lock
);
2714 RTL_W8(Cfg9346
, Cfg9346_Unlock
);
2716 RTL_W32(MAC4
, high
);
2722 RTL_W8(Cfg9346
, Cfg9346_Lock
);
2724 spin_unlock_irq(&tp
->lock
);
2727 static int rtl_set_mac_address(struct net_device
*dev
, void *p
)
2729 struct rtl8169_private
*tp
= netdev_priv(dev
);
2730 struct sockaddr
*addr
= p
;
2732 if (!is_valid_ether_addr(addr
->sa_data
))
2733 return -EADDRNOTAVAIL
;
2735 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
2737 rtl_rar_set(tp
, dev
->dev_addr
);
2742 static int rtl8169_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
2744 struct rtl8169_private
*tp
= netdev_priv(dev
);
2745 struct mii_ioctl_data
*data
= if_mii(ifr
);
2747 return netif_running(dev
) ? tp
->do_ioctl(tp
, data
, cmd
) : -ENODEV
;
2750 static int rtl_xmii_ioctl(struct rtl8169_private
*tp
, struct mii_ioctl_data
*data
, int cmd
)
2754 data
->phy_id
= 32; /* Internal PHY */
2758 data
->val_out
= rtl_readphy(tp
, data
->reg_num
& 0x1f);
2762 rtl_writephy(tp
, data
->reg_num
& 0x1f, data
->val_in
);
2768 static int rtl_tbi_ioctl(struct rtl8169_private
*tp
, struct mii_ioctl_data
*data
, int cmd
)
2773 static const struct rtl_cfg_info
{
2774 void (*hw_start
)(struct net_device
*);
2775 unsigned int region
;
2781 } rtl_cfg_infos
[] = {
2783 .hw_start
= rtl_hw_start_8169
,
2786 .intr_event
= SYSErr
| LinkChg
| RxOverflow
|
2787 RxFIFOOver
| TxErr
| TxOK
| RxOK
| RxErr
,
2788 .napi_event
= RxFIFOOver
| TxErr
| TxOK
| RxOK
| RxOverflow
,
2789 .features
= RTL_FEATURE_GMII
,
2790 .default_ver
= RTL_GIGA_MAC_VER_01
,
2793 .hw_start
= rtl_hw_start_8168
,
2796 .intr_event
= SYSErr
| LinkChg
| RxOverflow
|
2797 TxErr
| TxOK
| RxOK
| RxErr
,
2798 .napi_event
= TxErr
| TxOK
| RxOK
| RxOverflow
,
2799 .features
= RTL_FEATURE_GMII
| RTL_FEATURE_MSI
,
2800 .default_ver
= RTL_GIGA_MAC_VER_11
,
2803 .hw_start
= rtl_hw_start_8101
,
2806 .intr_event
= SYSErr
| LinkChg
| RxOverflow
| PCSTimeout
|
2807 RxFIFOOver
| TxErr
| TxOK
| RxOK
| RxErr
,
2808 .napi_event
= RxFIFOOver
| TxErr
| TxOK
| RxOK
| RxOverflow
,
2809 .features
= RTL_FEATURE_MSI
,
2810 .default_ver
= RTL_GIGA_MAC_VER_13
,
2814 /* Cfg9346_Unlock assumed. */
2815 static unsigned rtl_try_msi(struct pci_dev
*pdev
, void __iomem
*ioaddr
,
2816 const struct rtl_cfg_info
*cfg
)
2821 cfg2
= RTL_R8(Config2
) & ~MSIEnable
;
2822 if (cfg
->features
& RTL_FEATURE_MSI
) {
2823 if (pci_enable_msi(pdev
)) {
2824 dev_info(&pdev
->dev
, "no MSI. Back to INTx.\n");
2827 msi
= RTL_FEATURE_MSI
;
2830 RTL_W8(Config2
, cfg2
);
2834 static void rtl_disable_msi(struct pci_dev
*pdev
, struct rtl8169_private
*tp
)
2836 if (tp
->features
& RTL_FEATURE_MSI
) {
2837 pci_disable_msi(pdev
);
2838 tp
->features
&= ~RTL_FEATURE_MSI
;
2842 static const struct net_device_ops rtl8169_netdev_ops
= {
2843 .ndo_open
= rtl8169_open
,
2844 .ndo_stop
= rtl8169_close
,
2845 .ndo_get_stats
= rtl8169_get_stats
,
2846 .ndo_start_xmit
= rtl8169_start_xmit
,
2847 .ndo_tx_timeout
= rtl8169_tx_timeout
,
2848 .ndo_validate_addr
= eth_validate_addr
,
2849 .ndo_change_mtu
= rtl8169_change_mtu
,
2850 .ndo_set_mac_address
= rtl_set_mac_address
,
2851 .ndo_do_ioctl
= rtl8169_ioctl
,
2852 .ndo_set_multicast_list
= rtl_set_rx_mode
,
2853 #ifdef CONFIG_NET_POLL_CONTROLLER
2854 .ndo_poll_controller
= rtl8169_netpoll
,
2859 static void __devinit
rtl_init_mdio_ops(struct rtl8169_private
*tp
)
2861 struct mdio_ops
*ops
= &tp
->mdio_ops
;
2863 switch (tp
->mac_version
) {
2864 case RTL_GIGA_MAC_VER_27
:
2865 ops
->write
= r8168dp_1_mdio_write
;
2866 ops
->read
= r8168dp_1_mdio_read
;
2868 case RTL_GIGA_MAC_VER_28
:
2869 ops
->write
= r8168dp_2_mdio_write
;
2870 ops
->read
= r8168dp_2_mdio_read
;
2873 ops
->write
= r8169_mdio_write
;
2874 ops
->read
= r8169_mdio_read
;
2879 static void r810x_phy_power_down(struct rtl8169_private
*tp
)
2881 rtl_writephy(tp
, 0x1f, 0x0000);
2882 rtl_writephy(tp
, MII_BMCR
, BMCR_PDOWN
);
2885 static void r810x_phy_power_up(struct rtl8169_private
*tp
)
2887 rtl_writephy(tp
, 0x1f, 0x0000);
2888 rtl_writephy(tp
, MII_BMCR
, BMCR_ANENABLE
);
2891 static void r810x_pll_power_down(struct rtl8169_private
*tp
)
2893 if (__rtl8169_get_wol(tp
) & WAKE_ANY
) {
2894 rtl_writephy(tp
, 0x1f, 0x0000);
2895 rtl_writephy(tp
, MII_BMCR
, 0x0000);
2899 r810x_phy_power_down(tp
);
2902 static void r810x_pll_power_up(struct rtl8169_private
*tp
)
2904 r810x_phy_power_up(tp
);
2907 static void r8168_phy_power_up(struct rtl8169_private
*tp
)
2909 rtl_writephy(tp
, 0x1f, 0x0000);
2910 rtl_writephy(tp
, 0x0e, 0x0000);
2911 rtl_writephy(tp
, MII_BMCR
, BMCR_ANENABLE
);
2914 static void r8168_phy_power_down(struct rtl8169_private
*tp
)
2916 rtl_writephy(tp
, 0x1f, 0x0000);
2917 rtl_writephy(tp
, 0x0e, 0x0200);
2918 rtl_writephy(tp
, MII_BMCR
, BMCR_PDOWN
);
2921 static void r8168_pll_power_down(struct rtl8169_private
*tp
)
2923 void __iomem
*ioaddr
= tp
->mmio_addr
;
2925 if (((tp
->mac_version
== RTL_GIGA_MAC_VER_27
) ||
2926 (tp
->mac_version
== RTL_GIGA_MAC_VER_28
)) &&
2927 (ocp_read(tp
, 0x0f, 0x0010) & 0x00008000)) {
2931 if (((tp
->mac_version
== RTL_GIGA_MAC_VER_23
) ||
2932 (tp
->mac_version
== RTL_GIGA_MAC_VER_24
)) &&
2933 (RTL_R16(CPlusCmd
) & ASF
)) {
2937 if (__rtl8169_get_wol(tp
) & WAKE_ANY
) {
2938 rtl_writephy(tp
, 0x1f, 0x0000);
2939 rtl_writephy(tp
, MII_BMCR
, 0x0000);
2941 RTL_W32(RxConfig
, RTL_R32(RxConfig
) |
2942 AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
);
2946 r8168_phy_power_down(tp
);
2948 switch (tp
->mac_version
) {
2949 case RTL_GIGA_MAC_VER_25
:
2950 case RTL_GIGA_MAC_VER_26
:
2951 case RTL_GIGA_MAC_VER_27
:
2952 case RTL_GIGA_MAC_VER_28
:
2953 RTL_W8(PMCH
, RTL_R8(PMCH
) & ~0x80);
2958 static void r8168_pll_power_up(struct rtl8169_private
*tp
)
2960 void __iomem
*ioaddr
= tp
->mmio_addr
;
2962 if (((tp
->mac_version
== RTL_GIGA_MAC_VER_27
) ||
2963 (tp
->mac_version
== RTL_GIGA_MAC_VER_28
)) &&
2964 (ocp_read(tp
, 0x0f, 0x0010) & 0x00008000)) {
2968 switch (tp
->mac_version
) {
2969 case RTL_GIGA_MAC_VER_25
:
2970 case RTL_GIGA_MAC_VER_26
:
2971 case RTL_GIGA_MAC_VER_27
:
2972 case RTL_GIGA_MAC_VER_28
:
2973 RTL_W8(PMCH
, RTL_R8(PMCH
) | 0x80);
2977 r8168_phy_power_up(tp
);
2980 static void rtl_pll_power_op(struct rtl8169_private
*tp
,
2981 void (*op
)(struct rtl8169_private
*))
2987 static void rtl_pll_power_down(struct rtl8169_private
*tp
)
2989 rtl_pll_power_op(tp
, tp
->pll_power_ops
.down
);
2992 static void rtl_pll_power_up(struct rtl8169_private
*tp
)
2994 rtl_pll_power_op(tp
, tp
->pll_power_ops
.up
);
2997 static void __devinit
rtl_init_pll_power_ops(struct rtl8169_private
*tp
)
2999 struct pll_power_ops
*ops
= &tp
->pll_power_ops
;
3001 switch (tp
->mac_version
) {
3002 case RTL_GIGA_MAC_VER_07
:
3003 case RTL_GIGA_MAC_VER_08
:
3004 case RTL_GIGA_MAC_VER_09
:
3005 case RTL_GIGA_MAC_VER_10
:
3006 case RTL_GIGA_MAC_VER_16
:
3007 case RTL_GIGA_MAC_VER_29
:
3008 case RTL_GIGA_MAC_VER_30
:
3009 ops
->down
= r810x_pll_power_down
;
3010 ops
->up
= r810x_pll_power_up
;
3013 case RTL_GIGA_MAC_VER_11
:
3014 case RTL_GIGA_MAC_VER_12
:
3015 case RTL_GIGA_MAC_VER_17
:
3016 case RTL_GIGA_MAC_VER_18
:
3017 case RTL_GIGA_MAC_VER_19
:
3018 case RTL_GIGA_MAC_VER_20
:
3019 case RTL_GIGA_MAC_VER_21
:
3020 case RTL_GIGA_MAC_VER_22
:
3021 case RTL_GIGA_MAC_VER_23
:
3022 case RTL_GIGA_MAC_VER_24
:
3023 case RTL_GIGA_MAC_VER_25
:
3024 case RTL_GIGA_MAC_VER_26
:
3025 case RTL_GIGA_MAC_VER_27
:
3026 case RTL_GIGA_MAC_VER_28
:
3027 ops
->down
= r8168_pll_power_down
;
3028 ops
->up
= r8168_pll_power_up
;
3038 static int __devinit
3039 rtl8169_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
3041 const struct rtl_cfg_info
*cfg
= rtl_cfg_infos
+ ent
->driver_data
;
3042 const unsigned int region
= cfg
->region
;
3043 struct rtl8169_private
*tp
;
3044 struct mii_if_info
*mii
;
3045 struct net_device
*dev
;
3046 void __iomem
*ioaddr
;
3050 if (netif_msg_drv(&debug
)) {
3051 printk(KERN_INFO
"%s Gigabit Ethernet driver %s loaded\n",
3052 MODULENAME
, RTL8169_VERSION
);
3055 dev
= alloc_etherdev(sizeof (*tp
));
3057 if (netif_msg_drv(&debug
))
3058 dev_err(&pdev
->dev
, "unable to alloc new ethernet\n");
3063 SET_NETDEV_DEV(dev
, &pdev
->dev
);
3064 dev
->netdev_ops
= &rtl8169_netdev_ops
;
3065 tp
= netdev_priv(dev
);
3068 tp
->msg_enable
= netif_msg_init(debug
.msg_enable
, R8169_MSG_DEFAULT
);
3072 mii
->mdio_read
= rtl_mdio_read
;
3073 mii
->mdio_write
= rtl_mdio_write
;
3074 mii
->phy_id_mask
= 0x1f;
3075 mii
->reg_num_mask
= 0x1f;
3076 mii
->supports_gmii
= !!(cfg
->features
& RTL_FEATURE_GMII
);
3078 /* disable ASPM completely as that cause random device stop working
3079 * problems as well as full system hangs for some PCIe devices users */
3080 pci_disable_link_state(pdev
, PCIE_LINK_STATE_L0S
| PCIE_LINK_STATE_L1
|
3081 PCIE_LINK_STATE_CLKPM
);
3083 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3084 rc
= pci_enable_device(pdev
);
3086 netif_err(tp
, probe
, dev
, "enable failure\n");
3087 goto err_out_free_dev_1
;
3090 if (pci_set_mwi(pdev
) < 0)
3091 netif_info(tp
, probe
, dev
, "Mem-Wr-Inval unavailable\n");
3093 /* make sure PCI base addr 1 is MMIO */
3094 if (!(pci_resource_flags(pdev
, region
) & IORESOURCE_MEM
)) {
3095 netif_err(tp
, probe
, dev
,
3096 "region #%d not an MMIO resource, aborting\n",
3102 /* check for weird/broken PCI region reporting */
3103 if (pci_resource_len(pdev
, region
) < R8169_REGS_SIZE
) {
3104 netif_err(tp
, probe
, dev
,
3105 "Invalid PCI region size(s), aborting\n");
3110 rc
= pci_request_regions(pdev
, MODULENAME
);
3112 netif_err(tp
, probe
, dev
, "could not request regions\n");
3116 tp
->cp_cmd
= RxChkSum
;
3118 if ((sizeof(dma_addr_t
) > 4) &&
3119 !pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) && use_dac
) {
3120 tp
->cp_cmd
|= PCIDAC
;
3121 dev
->features
|= NETIF_F_HIGHDMA
;
3123 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
3125 netif_err(tp
, probe
, dev
, "DMA configuration failed\n");
3126 goto err_out_free_res_3
;
3130 /* ioremap MMIO region */
3131 ioaddr
= ioremap(pci_resource_start(pdev
, region
), R8169_REGS_SIZE
);
3133 netif_err(tp
, probe
, dev
, "cannot remap MMIO, aborting\n");
3135 goto err_out_free_res_3
;
3138 tp
->pcie_cap
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
3140 netif_info(tp
, probe
, dev
, "no PCI Express capability\n");
3142 RTL_W16(IntrMask
, 0x0000);
3144 /* Soft reset the chip. */
3145 RTL_W8(ChipCmd
, CmdReset
);
3147 /* Check that the chip has finished the reset. */
3148 for (i
= 0; i
< 100; i
++) {
3149 if ((RTL_R8(ChipCmd
) & CmdReset
) == 0)
3151 msleep_interruptible(1);
3154 RTL_W16(IntrStatus
, 0xffff);
3156 pci_set_master(pdev
);
3158 /* Identify chip attached to board */
3159 rtl8169_get_mac_version(tp
, ioaddr
);
3162 * Pretend we are using VLANs; This bypasses a nasty bug where
3163 * Interrupts stop flowing on high load on 8110SCd controllers.
3165 if (tp
->mac_version
== RTL_GIGA_MAC_VER_05
)
3166 tp
->cp_cmd
|= RxVlan
;
3168 rtl_init_mdio_ops(tp
);
3169 rtl_init_pll_power_ops(tp
);
3171 /* Use appropriate default if unknown */
3172 if (tp
->mac_version
== RTL_GIGA_MAC_NONE
) {
3173 netif_notice(tp
, probe
, dev
,
3174 "unknown MAC, using family default\n");
3175 tp
->mac_version
= cfg
->default_ver
;
3178 rtl8169_print_mac_version(tp
);
3180 for (i
= 0; i
< ARRAY_SIZE(rtl_chip_info
); i
++) {
3181 if (tp
->mac_version
== rtl_chip_info
[i
].mac_version
)
3184 if (i
== ARRAY_SIZE(rtl_chip_info
)) {
3186 "driver bug, MAC version not found in rtl_chip_info\n");
3191 RTL_W8(Cfg9346
, Cfg9346_Unlock
);
3192 RTL_W8(Config1
, RTL_R8(Config1
) | PMEnable
);
3193 RTL_W8(Config5
, RTL_R8(Config5
) & PMEStatus
);
3194 if ((RTL_R8(Config3
) & (LinkUp
| MagicPacket
)) != 0)
3195 tp
->features
|= RTL_FEATURE_WOL
;
3196 if ((RTL_R8(Config5
) & (UWF
| BWF
| MWF
)) != 0)
3197 tp
->features
|= RTL_FEATURE_WOL
;
3198 tp
->features
|= rtl_try_msi(pdev
, ioaddr
, cfg
);
3199 RTL_W8(Cfg9346
, Cfg9346_Lock
);
3201 if ((tp
->mac_version
<= RTL_GIGA_MAC_VER_06
) &&
3202 (RTL_R8(PHYstatus
) & TBI_Enable
)) {
3203 tp
->set_speed
= rtl8169_set_speed_tbi
;
3204 tp
->get_settings
= rtl8169_gset_tbi
;
3205 tp
->phy_reset_enable
= rtl8169_tbi_reset_enable
;
3206 tp
->phy_reset_pending
= rtl8169_tbi_reset_pending
;
3207 tp
->link_ok
= rtl8169_tbi_link_ok
;
3208 tp
->do_ioctl
= rtl_tbi_ioctl
;
3210 tp
->phy_1000_ctrl_reg
= ADVERTISE_1000FULL
; /* Implied by TBI */
3212 tp
->set_speed
= rtl8169_set_speed_xmii
;
3213 tp
->get_settings
= rtl8169_gset_xmii
;
3214 tp
->phy_reset_enable
= rtl8169_xmii_reset_enable
;
3215 tp
->phy_reset_pending
= rtl8169_xmii_reset_pending
;
3216 tp
->link_ok
= rtl8169_xmii_link_ok
;
3217 tp
->do_ioctl
= rtl_xmii_ioctl
;
3220 spin_lock_init(&tp
->lock
);
3222 tp
->mmio_addr
= ioaddr
;
3224 /* Get MAC address */
3225 for (i
= 0; i
< MAC_ADDR_LEN
; i
++)
3226 dev
->dev_addr
[i
] = RTL_R8(MAC0
+ i
);
3227 memcpy(dev
->perm_addr
, dev
->dev_addr
, dev
->addr_len
);
3229 SET_ETHTOOL_OPS(dev
, &rtl8169_ethtool_ops
);
3230 dev
->watchdog_timeo
= RTL8169_TX_TIMEOUT
;
3231 dev
->irq
= pdev
->irq
;
3232 dev
->base_addr
= (unsigned long) ioaddr
;
3234 netif_napi_add(dev
, &tp
->napi
, rtl8169_poll
, R8169_NAPI_WEIGHT
);
3236 dev
->features
|= NETIF_F_HW_VLAN_TX_RX
| NETIF_F_GRO
;
3238 tp
->intr_mask
= 0xffff;
3239 tp
->hw_start
= cfg
->hw_start
;
3240 tp
->intr_event
= cfg
->intr_event
;
3241 tp
->napi_event
= cfg
->napi_event
;
3243 init_timer(&tp
->timer
);
3244 tp
->timer
.data
= (unsigned long) dev
;
3245 tp
->timer
.function
= rtl8169_phy_timer
;
3247 tp
->fw
= RTL_FIRMWARE_UNKNOWN
;
3249 rc
= register_netdev(dev
);
3253 pci_set_drvdata(pdev
, dev
);
3255 netif_info(tp
, probe
, dev
, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3256 rtl_chip_info
[tp
->chipset
].name
,
3257 dev
->base_addr
, dev
->dev_addr
,
3258 (u32
)(RTL_R32(TxConfig
) & 0x9cf0f8ff), dev
->irq
);
3260 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_27
) ||
3261 (tp
->mac_version
== RTL_GIGA_MAC_VER_28
)) {
3262 rtl8168_driver_start(tp
);
3265 device_set_wakeup_enable(&pdev
->dev
, tp
->features
& RTL_FEATURE_WOL
);
3267 if (pci_dev_run_wake(pdev
))
3268 pm_runtime_put_noidle(&pdev
->dev
);
3270 netif_carrier_off(dev
);
3276 rtl_disable_msi(pdev
, tp
);
3279 pci_release_regions(pdev
);
3281 pci_clear_mwi(pdev
);
3282 pci_disable_device(pdev
);
3288 static void __devexit
rtl8169_remove_one(struct pci_dev
*pdev
)
3290 struct net_device
*dev
= pci_get_drvdata(pdev
);
3291 struct rtl8169_private
*tp
= netdev_priv(dev
);
3293 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_27
) ||
3294 (tp
->mac_version
== RTL_GIGA_MAC_VER_28
)) {
3295 rtl8168_driver_stop(tp
);
3298 cancel_delayed_work_sync(&tp
->task
);
3300 unregister_netdev(dev
);
3302 rtl_release_firmware(tp
);
3304 if (pci_dev_run_wake(pdev
))
3305 pm_runtime_get_noresume(&pdev
->dev
);
3307 /* restore original MAC address */
3308 rtl_rar_set(tp
, dev
->perm_addr
);
3310 rtl_disable_msi(pdev
, tp
);
3311 rtl8169_release_board(pdev
, dev
, tp
->mmio_addr
);
3312 pci_set_drvdata(pdev
, NULL
);
3315 static void rtl_request_firmware(struct rtl8169_private
*tp
)
3319 /* Return early if the firmware is already loaded / cached. */
3320 if (!IS_ERR(tp
->fw
))
3323 for (i
= 0; i
< ARRAY_SIZE(rtl_firmware_infos
); i
++) {
3324 const struct rtl_firmware_info
*info
= rtl_firmware_infos
+ i
;
3326 if (info
->mac_version
== tp
->mac_version
) {
3327 const char *name
= info
->fw_name
;
3330 rc
= request_firmware(&tp
->fw
, name
, &tp
->pci_dev
->dev
);
3332 netif_warn(tp
, ifup
, tp
->dev
, "unable to load "
3333 "firmware patch %s (%d)\n", name
, rc
);
3334 goto out_disable_request_firmware
;
3340 out_disable_request_firmware
:
3346 static int rtl8169_open(struct net_device
*dev
)
3348 struct rtl8169_private
*tp
= netdev_priv(dev
);
3349 void __iomem
*ioaddr
= tp
->mmio_addr
;
3350 struct pci_dev
*pdev
= tp
->pci_dev
;
3351 int retval
= -ENOMEM
;
3353 pm_runtime_get_sync(&pdev
->dev
);
3356 * Rx and Tx desscriptors needs 256 bytes alignment.
3357 * dma_alloc_coherent provides more.
3359 tp
->TxDescArray
= dma_alloc_coherent(&pdev
->dev
, R8169_TX_RING_BYTES
,
3360 &tp
->TxPhyAddr
, GFP_KERNEL
);
3361 if (!tp
->TxDescArray
)
3362 goto err_pm_runtime_put
;
3364 tp
->RxDescArray
= dma_alloc_coherent(&pdev
->dev
, R8169_RX_RING_BYTES
,
3365 &tp
->RxPhyAddr
, GFP_KERNEL
);
3366 if (!tp
->RxDescArray
)
3369 retval
= rtl8169_init_ring(dev
);
3373 INIT_DELAYED_WORK(&tp
->task
, NULL
);
3377 rtl_request_firmware(tp
);
3379 retval
= request_irq(dev
->irq
, rtl8169_interrupt
,
3380 (tp
->features
& RTL_FEATURE_MSI
) ? 0 : IRQF_SHARED
,
3383 goto err_release_fw_2
;
3385 napi_enable(&tp
->napi
);
3387 rtl8169_init_phy(dev
, tp
);
3389 rtl8169_vlan_mode(dev
);
3391 rtl_pll_power_up(tp
);
3395 rtl8169_request_timer(dev
);
3397 tp
->saved_wolopts
= 0;
3398 pm_runtime_put_noidle(&pdev
->dev
);
3400 rtl8169_check_link_status(dev
, tp
, ioaddr
);
3405 rtl_release_firmware(tp
);
3406 rtl8169_rx_clear(tp
);
3408 dma_free_coherent(&pdev
->dev
, R8169_RX_RING_BYTES
, tp
->RxDescArray
,
3410 tp
->RxDescArray
= NULL
;
3412 dma_free_coherent(&pdev
->dev
, R8169_TX_RING_BYTES
, tp
->TxDescArray
,
3414 tp
->TxDescArray
= NULL
;
3416 pm_runtime_put_noidle(&pdev
->dev
);
3420 static void rtl8169_hw_reset(struct rtl8169_private
*tp
)
3422 void __iomem
*ioaddr
= tp
->mmio_addr
;
3424 /* Disable interrupts */
3425 rtl8169_irq_mask_and_ack(ioaddr
);
3427 if (tp
->mac_version
== RTL_GIGA_MAC_VER_27
||
3428 tp
->mac_version
== RTL_GIGA_MAC_VER_28
) {
3429 while (RTL_R8(TxPoll
) & NPQ
)
3434 /* Reset the chipset */
3435 RTL_W8(ChipCmd
, CmdReset
);
3441 static void rtl_set_rx_tx_config_registers(struct rtl8169_private
*tp
)
3443 void __iomem
*ioaddr
= tp
->mmio_addr
;
3444 u32 cfg
= rtl8169_rx_config
;
3446 cfg
|= (RTL_R32(RxConfig
) & rtl_chip_info
[tp
->chipset
].RxConfigMask
);
3447 RTL_W32(RxConfig
, cfg
);
3449 /* Set DMA burst size and Interframe Gap Time */
3450 RTL_W32(TxConfig
, (TX_DMA_BURST
<< TxDMAShift
) |
3451 (InterFrameGap
<< TxInterFrameGapShift
));
3454 static void rtl_hw_start(struct net_device
*dev
)
3456 struct rtl8169_private
*tp
= netdev_priv(dev
);
3457 void __iomem
*ioaddr
= tp
->mmio_addr
;
3460 /* Soft reset the chip. */
3461 RTL_W8(ChipCmd
, CmdReset
);
3463 /* Check that the chip has finished the reset. */
3464 for (i
= 0; i
< 100; i
++) {
3465 if ((RTL_R8(ChipCmd
) & CmdReset
) == 0)
3467 msleep_interruptible(1);
3472 netif_start_queue(dev
);
3476 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private
*tp
,
3477 void __iomem
*ioaddr
)
3480 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3481 * register to be written before TxDescAddrLow to work.
3482 * Switching from MMIO to I/O access fixes the issue as well.
3484 RTL_W32(TxDescStartAddrHigh
, ((u64
) tp
->TxPhyAddr
) >> 32);
3485 RTL_W32(TxDescStartAddrLow
, ((u64
) tp
->TxPhyAddr
) & DMA_BIT_MASK(32));
3486 RTL_W32(RxDescAddrHigh
, ((u64
) tp
->RxPhyAddr
) >> 32);
3487 RTL_W32(RxDescAddrLow
, ((u64
) tp
->RxPhyAddr
) & DMA_BIT_MASK(32));
3490 static u16
rtl_rw_cpluscmd(void __iomem
*ioaddr
)
3494 cmd
= RTL_R16(CPlusCmd
);
3495 RTL_W16(CPlusCmd
, cmd
);
3499 static void rtl_set_rx_max_size(void __iomem
*ioaddr
, unsigned int rx_buf_sz
)
3501 /* Low hurts. Let's disable the filtering. */
3502 RTL_W16(RxMaxSize
, rx_buf_sz
+ 1);
3505 static void rtl8169_set_magic_reg(void __iomem
*ioaddr
, unsigned mac_version
)
3507 static const struct {
3512 { RTL_GIGA_MAC_VER_05
, PCI_Clock_33MHz
, 0x000fff00 }, // 8110SCd
3513 { RTL_GIGA_MAC_VER_05
, PCI_Clock_66MHz
, 0x000fffff },
3514 { RTL_GIGA_MAC_VER_06
, PCI_Clock_33MHz
, 0x00ffff00 }, // 8110SCe
3515 { RTL_GIGA_MAC_VER_06
, PCI_Clock_66MHz
, 0x00ffffff }
3520 clk
= RTL_R8(Config2
) & PCI_Clock_66MHz
;
3521 for (i
= 0; i
< ARRAY_SIZE(cfg2_info
); i
++, p
++) {
3522 if ((p
->mac_version
== mac_version
) && (p
->clk
== clk
)) {
3523 RTL_W32(0x7c, p
->val
);
3529 static void rtl_hw_start_8169(struct net_device
*dev
)
3531 struct rtl8169_private
*tp
= netdev_priv(dev
);
3532 void __iomem
*ioaddr
= tp
->mmio_addr
;
3533 struct pci_dev
*pdev
= tp
->pci_dev
;
3535 if (tp
->mac_version
== RTL_GIGA_MAC_VER_05
) {
3536 RTL_W16(CPlusCmd
, RTL_R16(CPlusCmd
) | PCIMulRW
);
3537 pci_write_config_byte(pdev
, PCI_CACHE_LINE_SIZE
, 0x08);
3540 RTL_W8(Cfg9346
, Cfg9346_Unlock
);
3541 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_01
) ||
3542 (tp
->mac_version
== RTL_GIGA_MAC_VER_02
) ||
3543 (tp
->mac_version
== RTL_GIGA_MAC_VER_03
) ||
3544 (tp
->mac_version
== RTL_GIGA_MAC_VER_04
))
3545 RTL_W8(ChipCmd
, CmdTxEnb
| CmdRxEnb
);
3547 RTL_W8(EarlyTxThres
, NoEarlyTx
);
3549 rtl_set_rx_max_size(ioaddr
, rx_buf_sz
);
3551 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_01
) ||
3552 (tp
->mac_version
== RTL_GIGA_MAC_VER_02
) ||
3553 (tp
->mac_version
== RTL_GIGA_MAC_VER_03
) ||
3554 (tp
->mac_version
== RTL_GIGA_MAC_VER_04
))
3555 rtl_set_rx_tx_config_registers(tp
);
3557 tp
->cp_cmd
|= rtl_rw_cpluscmd(ioaddr
) | PCIMulRW
;
3559 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_02
) ||
3560 (tp
->mac_version
== RTL_GIGA_MAC_VER_03
)) {
3561 dprintk("Set MAC Reg C+CR Offset 0xE0. "
3562 "Bit-3 and bit-14 MUST be 1\n");
3563 tp
->cp_cmd
|= (1 << 14);
3566 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
3568 rtl8169_set_magic_reg(ioaddr
, tp
->mac_version
);
3571 * Undocumented corner. Supposedly:
3572 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3574 RTL_W16(IntrMitigate
, 0x0000);
3576 rtl_set_rx_tx_desc_registers(tp
, ioaddr
);
3578 if ((tp
->mac_version
!= RTL_GIGA_MAC_VER_01
) &&
3579 (tp
->mac_version
!= RTL_GIGA_MAC_VER_02
) &&
3580 (tp
->mac_version
!= RTL_GIGA_MAC_VER_03
) &&
3581 (tp
->mac_version
!= RTL_GIGA_MAC_VER_04
)) {
3582 RTL_W8(ChipCmd
, CmdTxEnb
| CmdRxEnb
);
3583 rtl_set_rx_tx_config_registers(tp
);
3586 RTL_W8(Cfg9346
, Cfg9346_Lock
);
3588 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3591 RTL_W32(RxMissed
, 0);
3593 rtl_set_rx_mode(dev
);
3595 /* no early-rx interrupts */
3596 RTL_W16(MultiIntr
, RTL_R16(MultiIntr
) & 0xF000);
3598 /* Enable all known interrupts by setting the interrupt mask. */
3599 RTL_W16(IntrMask
, tp
->intr_event
);
3602 static void rtl_tx_performance_tweak(struct pci_dev
*pdev
, u16 force
)
3604 struct net_device
*dev
= pci_get_drvdata(pdev
);
3605 struct rtl8169_private
*tp
= netdev_priv(dev
);
3606 int cap
= tp
->pcie_cap
;
3611 pci_read_config_word(pdev
, cap
+ PCI_EXP_DEVCTL
, &ctl
);
3612 ctl
= (ctl
& ~PCI_EXP_DEVCTL_READRQ
) | force
;
3613 pci_write_config_word(pdev
, cap
+ PCI_EXP_DEVCTL
, ctl
);
3617 static void rtl_csi_access_enable(void __iomem
*ioaddr
, u32 bits
)
3621 csi
= rtl_csi_read(ioaddr
, 0x070c) & 0x00ffffff;
3622 rtl_csi_write(ioaddr
, 0x070c, csi
| bits
);
3625 static void rtl_csi_access_enable_1(void __iomem
*ioaddr
)
3627 rtl_csi_access_enable(ioaddr
, 0x17000000);
3630 static void rtl_csi_access_enable_2(void __iomem
*ioaddr
)
3632 rtl_csi_access_enable(ioaddr
, 0x27000000);
3636 unsigned int offset
;
3641 static void rtl_ephy_init(void __iomem
*ioaddr
, const struct ephy_info
*e
, int len
)
3646 w
= (rtl_ephy_read(ioaddr
, e
->offset
) & ~e
->mask
) | e
->bits
;
3647 rtl_ephy_write(ioaddr
, e
->offset
, w
);
3652 static void rtl_disable_clock_request(struct pci_dev
*pdev
)
3654 struct net_device
*dev
= pci_get_drvdata(pdev
);
3655 struct rtl8169_private
*tp
= netdev_priv(dev
);
3656 int cap
= tp
->pcie_cap
;
3661 pci_read_config_word(pdev
, cap
+ PCI_EXP_LNKCTL
, &ctl
);
3662 ctl
&= ~PCI_EXP_LNKCTL_CLKREQ_EN
;
3663 pci_write_config_word(pdev
, cap
+ PCI_EXP_LNKCTL
, ctl
);
3667 static void rtl_enable_clock_request(struct pci_dev
*pdev
)
3669 struct net_device
*dev
= pci_get_drvdata(pdev
);
3670 struct rtl8169_private
*tp
= netdev_priv(dev
);
3671 int cap
= tp
->pcie_cap
;
3676 pci_read_config_word(pdev
, cap
+ PCI_EXP_LNKCTL
, &ctl
);
3677 ctl
|= PCI_EXP_LNKCTL_CLKREQ_EN
;
3678 pci_write_config_word(pdev
, cap
+ PCI_EXP_LNKCTL
, ctl
);
3682 #define R8168_CPCMD_QUIRK_MASK (\
3693 static void rtl_hw_start_8168bb(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3695 RTL_W8(Config3
, RTL_R8(Config3
) & ~Beacon_en
);
3697 RTL_W16(CPlusCmd
, RTL_R16(CPlusCmd
) & ~R8168_CPCMD_QUIRK_MASK
);
3699 rtl_tx_performance_tweak(pdev
,
3700 (0x5 << MAX_READ_REQUEST_SHIFT
) | PCI_EXP_DEVCTL_NOSNOOP_EN
);
3703 static void rtl_hw_start_8168bef(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3705 rtl_hw_start_8168bb(ioaddr
, pdev
);
3707 RTL_W8(MaxTxPacketSize
, TxPacketMax
);
3709 RTL_W8(Config4
, RTL_R8(Config4
) & ~(1 << 0));
3712 static void __rtl_hw_start_8168cp(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3714 RTL_W8(Config1
, RTL_R8(Config1
) | Speed_down
);
3716 RTL_W8(Config3
, RTL_R8(Config3
) & ~Beacon_en
);
3718 rtl_tx_performance_tweak(pdev
, 0x5 << MAX_READ_REQUEST_SHIFT
);
3720 rtl_disable_clock_request(pdev
);
3722 RTL_W16(CPlusCmd
, RTL_R16(CPlusCmd
) & ~R8168_CPCMD_QUIRK_MASK
);
3725 static void rtl_hw_start_8168cp_1(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3727 static const struct ephy_info e_info_8168cp
[] = {
3728 { 0x01, 0, 0x0001 },
3729 { 0x02, 0x0800, 0x1000 },
3730 { 0x03, 0, 0x0042 },
3731 { 0x06, 0x0080, 0x0000 },
3735 rtl_csi_access_enable_2(ioaddr
);
3737 rtl_ephy_init(ioaddr
, e_info_8168cp
, ARRAY_SIZE(e_info_8168cp
));
3739 __rtl_hw_start_8168cp(ioaddr
, pdev
);
3742 static void rtl_hw_start_8168cp_2(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3744 rtl_csi_access_enable_2(ioaddr
);
3746 RTL_W8(Config3
, RTL_R8(Config3
) & ~Beacon_en
);
3748 rtl_tx_performance_tweak(pdev
, 0x5 << MAX_READ_REQUEST_SHIFT
);
3750 RTL_W16(CPlusCmd
, RTL_R16(CPlusCmd
) & ~R8168_CPCMD_QUIRK_MASK
);
3753 static void rtl_hw_start_8168cp_3(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3755 rtl_csi_access_enable_2(ioaddr
);
3757 RTL_W8(Config3
, RTL_R8(Config3
) & ~Beacon_en
);
3760 RTL_W8(DBG_REG
, 0x20);
3762 RTL_W8(MaxTxPacketSize
, TxPacketMax
);
3764 rtl_tx_performance_tweak(pdev
, 0x5 << MAX_READ_REQUEST_SHIFT
);
3766 RTL_W16(CPlusCmd
, RTL_R16(CPlusCmd
) & ~R8168_CPCMD_QUIRK_MASK
);
3769 static void rtl_hw_start_8168c_1(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3771 static const struct ephy_info e_info_8168c_1
[] = {
3772 { 0x02, 0x0800, 0x1000 },
3773 { 0x03, 0, 0x0002 },
3774 { 0x06, 0x0080, 0x0000 }
3777 rtl_csi_access_enable_2(ioaddr
);
3779 RTL_W8(DBG_REG
, 0x06 | FIX_NAK_1
| FIX_NAK_2
);
3781 rtl_ephy_init(ioaddr
, e_info_8168c_1
, ARRAY_SIZE(e_info_8168c_1
));
3783 __rtl_hw_start_8168cp(ioaddr
, pdev
);
3786 static void rtl_hw_start_8168c_2(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3788 static const struct ephy_info e_info_8168c_2
[] = {
3789 { 0x01, 0, 0x0001 },
3790 { 0x03, 0x0400, 0x0220 }
3793 rtl_csi_access_enable_2(ioaddr
);
3795 rtl_ephy_init(ioaddr
, e_info_8168c_2
, ARRAY_SIZE(e_info_8168c_2
));
3797 __rtl_hw_start_8168cp(ioaddr
, pdev
);
3800 static void rtl_hw_start_8168c_3(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3802 rtl_hw_start_8168c_2(ioaddr
, pdev
);
3805 static void rtl_hw_start_8168c_4(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3807 rtl_csi_access_enable_2(ioaddr
);
3809 __rtl_hw_start_8168cp(ioaddr
, pdev
);
3812 static void rtl_hw_start_8168d(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3814 rtl_csi_access_enable_2(ioaddr
);
3816 rtl_disable_clock_request(pdev
);
3818 RTL_W8(MaxTxPacketSize
, TxPacketMax
);
3820 rtl_tx_performance_tweak(pdev
, 0x5 << MAX_READ_REQUEST_SHIFT
);
3822 RTL_W16(CPlusCmd
, RTL_R16(CPlusCmd
) & ~R8168_CPCMD_QUIRK_MASK
);
3825 static void rtl_hw_start_8168d_4(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3827 static const struct ephy_info e_info_8168d_4
[] = {
3829 { 0x19, 0x20, 0x50 },
3834 rtl_csi_access_enable_1(ioaddr
);
3836 rtl_tx_performance_tweak(pdev
, 0x5 << MAX_READ_REQUEST_SHIFT
);
3838 RTL_W8(MaxTxPacketSize
, TxPacketMax
);
3840 for (i
= 0; i
< ARRAY_SIZE(e_info_8168d_4
); i
++) {
3841 const struct ephy_info
*e
= e_info_8168d_4
+ i
;
3844 w
= rtl_ephy_read(ioaddr
, e
->offset
);
3845 rtl_ephy_write(ioaddr
, 0x03, (w
& e
->mask
) | e
->bits
);
3848 rtl_enable_clock_request(pdev
);
3851 static void rtl_hw_start_8168(struct net_device
*dev
)
3853 struct rtl8169_private
*tp
= netdev_priv(dev
);
3854 void __iomem
*ioaddr
= tp
->mmio_addr
;
3855 struct pci_dev
*pdev
= tp
->pci_dev
;
3857 RTL_W8(Cfg9346
, Cfg9346_Unlock
);
3859 RTL_W8(MaxTxPacketSize
, TxPacketMax
);
3861 rtl_set_rx_max_size(ioaddr
, rx_buf_sz
);
3863 tp
->cp_cmd
|= RTL_R16(CPlusCmd
) | PktCntrDisable
| INTT_1
;
3865 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
3867 RTL_W16(IntrMitigate
, 0x5151);
3869 /* Work around for RxFIFO overflow. */
3870 if (tp
->mac_version
== RTL_GIGA_MAC_VER_11
||
3871 tp
->mac_version
== RTL_GIGA_MAC_VER_22
) {
3872 tp
->intr_event
|= RxFIFOOver
| PCSTimeout
;
3873 tp
->intr_event
&= ~RxOverflow
;
3876 rtl_set_rx_tx_desc_registers(tp
, ioaddr
);
3878 rtl_set_rx_mode(dev
);
3880 RTL_W32(TxConfig
, (TX_DMA_BURST
<< TxDMAShift
) |
3881 (InterFrameGap
<< TxInterFrameGapShift
));
3885 switch (tp
->mac_version
) {
3886 case RTL_GIGA_MAC_VER_11
:
3887 rtl_hw_start_8168bb(ioaddr
, pdev
);
3890 case RTL_GIGA_MAC_VER_12
:
3891 case RTL_GIGA_MAC_VER_17
:
3892 rtl_hw_start_8168bef(ioaddr
, pdev
);
3895 case RTL_GIGA_MAC_VER_18
:
3896 rtl_hw_start_8168cp_1(ioaddr
, pdev
);
3899 case RTL_GIGA_MAC_VER_19
:
3900 rtl_hw_start_8168c_1(ioaddr
, pdev
);
3903 case RTL_GIGA_MAC_VER_20
:
3904 rtl_hw_start_8168c_2(ioaddr
, pdev
);
3907 case RTL_GIGA_MAC_VER_21
:
3908 rtl_hw_start_8168c_3(ioaddr
, pdev
);
3911 case RTL_GIGA_MAC_VER_22
:
3912 rtl_hw_start_8168c_4(ioaddr
, pdev
);
3915 case RTL_GIGA_MAC_VER_23
:
3916 rtl_hw_start_8168cp_2(ioaddr
, pdev
);
3919 case RTL_GIGA_MAC_VER_24
:
3920 rtl_hw_start_8168cp_3(ioaddr
, pdev
);
3923 case RTL_GIGA_MAC_VER_25
:
3924 case RTL_GIGA_MAC_VER_26
:
3925 case RTL_GIGA_MAC_VER_27
:
3926 rtl_hw_start_8168d(ioaddr
, pdev
);
3929 case RTL_GIGA_MAC_VER_28
:
3930 rtl_hw_start_8168d_4(ioaddr
, pdev
);
3934 printk(KERN_ERR PFX
"%s: unknown chipset (mac_version = %d).\n",
3935 dev
->name
, tp
->mac_version
);
3939 RTL_W8(ChipCmd
, CmdTxEnb
| CmdRxEnb
);
3941 RTL_W8(Cfg9346
, Cfg9346_Lock
);
3943 RTL_W16(MultiIntr
, RTL_R16(MultiIntr
) & 0xF000);
3945 RTL_W16(IntrMask
, tp
->intr_event
);
3948 #define R810X_CPCMD_QUIRK_MASK (\
3959 static void rtl_hw_start_8102e_1(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3961 static const struct ephy_info e_info_8102e_1
[] = {
3962 { 0x01, 0, 0x6e65 },
3963 { 0x02, 0, 0x091f },
3964 { 0x03, 0, 0xc2f9 },
3965 { 0x06, 0, 0xafb5 },
3966 { 0x07, 0, 0x0e00 },
3967 { 0x19, 0, 0xec80 },
3968 { 0x01, 0, 0x2e65 },
3973 rtl_csi_access_enable_2(ioaddr
);
3975 RTL_W8(DBG_REG
, FIX_NAK_1
);
3977 rtl_tx_performance_tweak(pdev
, 0x5 << MAX_READ_REQUEST_SHIFT
);
3980 LEDS1
| LEDS0
| Speed_down
| MEMMAP
| IOMAP
| VPD
| PMEnable
);
3981 RTL_W8(Config3
, RTL_R8(Config3
) & ~Beacon_en
);
3983 cfg1
= RTL_R8(Config1
);
3984 if ((cfg1
& LEDS0
) && (cfg1
& LEDS1
))
3985 RTL_W8(Config1
, cfg1
& ~LEDS0
);
3987 rtl_ephy_init(ioaddr
, e_info_8102e_1
, ARRAY_SIZE(e_info_8102e_1
));
3990 static void rtl_hw_start_8102e_2(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
3992 rtl_csi_access_enable_2(ioaddr
);
3994 rtl_tx_performance_tweak(pdev
, 0x5 << MAX_READ_REQUEST_SHIFT
);
3996 RTL_W8(Config1
, MEMMAP
| IOMAP
| VPD
| PMEnable
);
3997 RTL_W8(Config3
, RTL_R8(Config3
) & ~Beacon_en
);
4000 static void rtl_hw_start_8102e_3(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
4002 rtl_hw_start_8102e_2(ioaddr
, pdev
);
4004 rtl_ephy_write(ioaddr
, 0x03, 0xc2f9);
4007 static void rtl_hw_start_8105e_1(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
4009 static const struct ephy_info e_info_8105e_1
[] = {
4010 { 0x07, 0, 0x4000 },
4011 { 0x19, 0, 0x0200 },
4012 { 0x19, 0, 0x0020 },
4013 { 0x1e, 0, 0x2000 },
4014 { 0x03, 0, 0x0001 },
4015 { 0x19, 0, 0x0100 },
4016 { 0x19, 0, 0x0004 },
4020 /* Force LAN exit from ASPM if Rx/Tx are not idel */
4021 RTL_W32(FuncEvent
, RTL_R32(FuncEvent
) | 0x002800);
4023 /* disable Early Tally Counter */
4024 RTL_W32(FuncEvent
, RTL_R32(FuncEvent
) & ~0x010000);
4026 RTL_W8(MCU
, RTL_R8(MCU
) | EN_NDP
| EN_OOB_RESET
);
4027 RTL_W8(DLLPR
, RTL_R8(DLLPR
) | PM_SWITCH
);
4029 rtl_ephy_init(ioaddr
, e_info_8105e_1
, ARRAY_SIZE(e_info_8105e_1
));
4032 static void rtl_hw_start_8105e_2(void __iomem
*ioaddr
, struct pci_dev
*pdev
)
4034 rtl_hw_start_8105e_1(ioaddr
, pdev
);
4035 rtl_ephy_write(ioaddr
, 0x1e, rtl_ephy_read(ioaddr
, 0x1e) | 0x8000);
4038 static void rtl_hw_start_8101(struct net_device
*dev
)
4040 struct rtl8169_private
*tp
= netdev_priv(dev
);
4041 void __iomem
*ioaddr
= tp
->mmio_addr
;
4042 struct pci_dev
*pdev
= tp
->pci_dev
;
4044 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_13
) ||
4045 (tp
->mac_version
== RTL_GIGA_MAC_VER_16
)) {
4046 int cap
= tp
->pcie_cap
;
4049 pci_write_config_word(pdev
, cap
+ PCI_EXP_DEVCTL
,
4050 PCI_EXP_DEVCTL_NOSNOOP_EN
);
4054 RTL_W8(Cfg9346
, Cfg9346_Unlock
);
4056 switch (tp
->mac_version
) {
4057 case RTL_GIGA_MAC_VER_07
:
4058 rtl_hw_start_8102e_1(ioaddr
, pdev
);
4061 case RTL_GIGA_MAC_VER_08
:
4062 rtl_hw_start_8102e_3(ioaddr
, pdev
);
4065 case RTL_GIGA_MAC_VER_09
:
4066 rtl_hw_start_8102e_2(ioaddr
, pdev
);
4069 case RTL_GIGA_MAC_VER_29
:
4070 rtl_hw_start_8105e_1(ioaddr
, pdev
);
4072 case RTL_GIGA_MAC_VER_30
:
4073 rtl_hw_start_8105e_2(ioaddr
, pdev
);
4077 RTL_W8(Cfg9346
, Cfg9346_Lock
);
4079 RTL_W8(MaxTxPacketSize
, TxPacketMax
);
4081 rtl_set_rx_max_size(ioaddr
, rx_buf_sz
);
4083 tp
->cp_cmd
&= ~R810X_CPCMD_QUIRK_MASK
;
4084 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
4086 RTL_W16(IntrMitigate
, 0x0000);
4088 rtl_set_rx_tx_desc_registers(tp
, ioaddr
);
4090 RTL_W8(ChipCmd
, CmdTxEnb
| CmdRxEnb
);
4091 rtl_set_rx_tx_config_registers(tp
);
4095 rtl_set_rx_mode(dev
);
4097 RTL_W16(MultiIntr
, RTL_R16(MultiIntr
) & 0xf000);
4099 RTL_W16(IntrMask
, tp
->intr_event
);
4102 static int rtl8169_change_mtu(struct net_device
*dev
, int new_mtu
)
4104 if (new_mtu
< ETH_ZLEN
|| new_mtu
> SafeMtu
)
4111 static inline void rtl8169_make_unusable_by_asic(struct RxDesc
*desc
)
4113 desc
->addr
= cpu_to_le64(0x0badbadbadbadbadull
);
4114 desc
->opts1
&= ~cpu_to_le32(DescOwn
| RsvdMask
);
4117 static void rtl8169_free_rx_databuff(struct rtl8169_private
*tp
,
4118 void **data_buff
, struct RxDesc
*desc
)
4120 dma_unmap_single(&tp
->pci_dev
->dev
, le64_to_cpu(desc
->addr
), rx_buf_sz
,
4125 rtl8169_make_unusable_by_asic(desc
);
4128 static inline void rtl8169_mark_to_asic(struct RxDesc
*desc
, u32 rx_buf_sz
)
4130 u32 eor
= le32_to_cpu(desc
->opts1
) & RingEnd
;
4132 desc
->opts1
= cpu_to_le32(DescOwn
| eor
| rx_buf_sz
);
4135 static inline void rtl8169_map_to_asic(struct RxDesc
*desc
, dma_addr_t mapping
,
4138 desc
->addr
= cpu_to_le64(mapping
);
4140 rtl8169_mark_to_asic(desc
, rx_buf_sz
);
4143 static inline void *rtl8169_align(void *data
)
4145 return (void *)ALIGN((long)data
, 16);
4148 static struct sk_buff
*rtl8169_alloc_rx_data(struct rtl8169_private
*tp
,
4149 struct RxDesc
*desc
)
4153 struct device
*d
= &tp
->pci_dev
->dev
;
4154 struct net_device
*dev
= tp
->dev
;
4155 int node
= dev
->dev
.parent
? dev_to_node(dev
->dev
.parent
) : -1;
4157 data
= kmalloc_node(rx_buf_sz
, GFP_KERNEL
, node
);
4161 if (rtl8169_align(data
) != data
) {
4163 data
= kmalloc_node(rx_buf_sz
+ 15, GFP_KERNEL
, node
);
4168 mapping
= dma_map_single(d
, rtl8169_align(data
), rx_buf_sz
,
4170 if (unlikely(dma_mapping_error(d
, mapping
))) {
4171 if (net_ratelimit())
4172 netif_err(tp
, drv
, tp
->dev
, "Failed to map RX DMA!\n");
4176 rtl8169_map_to_asic(desc
, mapping
, rx_buf_sz
);
4184 static void rtl8169_rx_clear(struct rtl8169_private
*tp
)
4188 for (i
= 0; i
< NUM_RX_DESC
; i
++) {
4189 if (tp
->Rx_databuff
[i
]) {
4190 rtl8169_free_rx_databuff(tp
, tp
->Rx_databuff
+ i
,
4191 tp
->RxDescArray
+ i
);
4196 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc
*desc
)
4198 desc
->opts1
|= cpu_to_le32(RingEnd
);
4201 static int rtl8169_rx_fill(struct rtl8169_private
*tp
)
4205 for (i
= 0; i
< NUM_RX_DESC
; i
++) {
4208 if (tp
->Rx_databuff
[i
])
4211 data
= rtl8169_alloc_rx_data(tp
, tp
->RxDescArray
+ i
);
4213 rtl8169_make_unusable_by_asic(tp
->RxDescArray
+ i
);
4216 tp
->Rx_databuff
[i
] = data
;
4219 rtl8169_mark_as_last_descriptor(tp
->RxDescArray
+ NUM_RX_DESC
- 1);
4223 rtl8169_rx_clear(tp
);
4227 static void rtl8169_init_ring_indexes(struct rtl8169_private
*tp
)
4229 tp
->dirty_tx
= tp
->dirty_rx
= tp
->cur_tx
= tp
->cur_rx
= 0;
4232 static int rtl8169_init_ring(struct net_device
*dev
)
4234 struct rtl8169_private
*tp
= netdev_priv(dev
);
4236 rtl8169_init_ring_indexes(tp
);
4238 memset(tp
->tx_skb
, 0x0, NUM_TX_DESC
* sizeof(struct ring_info
));
4239 memset(tp
->Rx_databuff
, 0x0, NUM_RX_DESC
* sizeof(void *));
4241 return rtl8169_rx_fill(tp
);
4244 static void rtl8169_unmap_tx_skb(struct device
*d
, struct ring_info
*tx_skb
,
4245 struct TxDesc
*desc
)
4247 unsigned int len
= tx_skb
->len
;
4249 dma_unmap_single(d
, le64_to_cpu(desc
->addr
), len
, DMA_TO_DEVICE
);
4257 static void rtl8169_tx_clear_range(struct rtl8169_private
*tp
, u32 start
,
4262 for (i
= 0; i
< n
; i
++) {
4263 unsigned int entry
= (start
+ i
) % NUM_TX_DESC
;
4264 struct ring_info
*tx_skb
= tp
->tx_skb
+ entry
;
4265 unsigned int len
= tx_skb
->len
;
4268 struct sk_buff
*skb
= tx_skb
->skb
;
4270 rtl8169_unmap_tx_skb(&tp
->pci_dev
->dev
, tx_skb
,
4271 tp
->TxDescArray
+ entry
);
4273 tp
->dev
->stats
.tx_dropped
++;
4281 static void rtl8169_tx_clear(struct rtl8169_private
*tp
)
4283 rtl8169_tx_clear_range(tp
, tp
->dirty_tx
, NUM_TX_DESC
);
4284 tp
->cur_tx
= tp
->dirty_tx
= 0;
4287 static void rtl8169_schedule_work(struct net_device
*dev
, work_func_t task
)
4289 struct rtl8169_private
*tp
= netdev_priv(dev
);
4291 PREPARE_DELAYED_WORK(&tp
->task
, task
);
4292 schedule_delayed_work(&tp
->task
, 4);
4295 static void rtl8169_wait_for_quiescence(struct net_device
*dev
)
4297 struct rtl8169_private
*tp
= netdev_priv(dev
);
4298 void __iomem
*ioaddr
= tp
->mmio_addr
;
4300 synchronize_irq(dev
->irq
);
4302 /* Wait for any pending NAPI task to complete */
4303 napi_disable(&tp
->napi
);
4305 rtl8169_irq_mask_and_ack(ioaddr
);
4307 tp
->intr_mask
= 0xffff;
4308 RTL_W16(IntrMask
, tp
->intr_event
);
4309 napi_enable(&tp
->napi
);
4312 static void rtl8169_reinit_task(struct work_struct
*work
)
4314 struct rtl8169_private
*tp
=
4315 container_of(work
, struct rtl8169_private
, task
.work
);
4316 struct net_device
*dev
= tp
->dev
;
4321 if (!netif_running(dev
))
4324 rtl8169_wait_for_quiescence(dev
);
4327 ret
= rtl8169_open(dev
);
4328 if (unlikely(ret
< 0)) {
4329 if (net_ratelimit())
4330 netif_err(tp
, drv
, dev
,
4331 "reinit failure (status = %d). Rescheduling\n",
4333 rtl8169_schedule_work(dev
, rtl8169_reinit_task
);
4340 static void rtl8169_reset_task(struct work_struct
*work
)
4342 struct rtl8169_private
*tp
=
4343 container_of(work
, struct rtl8169_private
, task
.work
);
4344 struct net_device
*dev
= tp
->dev
;
4348 if (!netif_running(dev
))
4351 rtl8169_wait_for_quiescence(dev
);
4353 rtl8169_rx_interrupt(dev
, tp
, tp
->mmio_addr
, ~(u32
)0);
4354 rtl8169_tx_clear(tp
);
4356 if (tp
->dirty_rx
== tp
->cur_rx
) {
4357 rtl8169_init_ring_indexes(tp
);
4359 netif_wake_queue(dev
);
4360 rtl8169_check_link_status(dev
, tp
, tp
->mmio_addr
);
4362 if (net_ratelimit())
4363 netif_emerg(tp
, intr
, dev
, "Rx buffers shortage\n");
4364 rtl8169_schedule_work(dev
, rtl8169_reset_task
);
4371 static void rtl8169_tx_timeout(struct net_device
*dev
)
4373 struct rtl8169_private
*tp
= netdev_priv(dev
);
4375 rtl8169_hw_reset(tp
);
4377 /* Let's wait a bit while any (async) irq lands on */
4378 rtl8169_schedule_work(dev
, rtl8169_reset_task
);
4381 static int rtl8169_xmit_frags(struct rtl8169_private
*tp
, struct sk_buff
*skb
,
4384 struct skb_shared_info
*info
= skb_shinfo(skb
);
4385 unsigned int cur_frag
, entry
;
4386 struct TxDesc
* uninitialized_var(txd
);
4387 struct device
*d
= &tp
->pci_dev
->dev
;
4390 for (cur_frag
= 0; cur_frag
< info
->nr_frags
; cur_frag
++) {
4391 skb_frag_t
*frag
= info
->frags
+ cur_frag
;
4396 entry
= (entry
+ 1) % NUM_TX_DESC
;
4398 txd
= tp
->TxDescArray
+ entry
;
4400 addr
= ((void *) page_address(frag
->page
)) + frag
->page_offset
;
4401 mapping
= dma_map_single(d
, addr
, len
, DMA_TO_DEVICE
);
4402 if (unlikely(dma_mapping_error(d
, mapping
))) {
4403 if (net_ratelimit())
4404 netif_err(tp
, drv
, tp
->dev
,
4405 "Failed to map TX fragments DMA!\n");
4409 /* anti gcc 2.95.3 bugware (sic) */
4410 status
= opts1
| len
| (RingEnd
* !((entry
+ 1) % NUM_TX_DESC
));
4412 txd
->opts1
= cpu_to_le32(status
);
4413 txd
->addr
= cpu_to_le64(mapping
);
4415 tp
->tx_skb
[entry
].len
= len
;
4419 tp
->tx_skb
[entry
].skb
= skb
;
4420 txd
->opts1
|= cpu_to_le32(LastFrag
);
4426 rtl8169_tx_clear_range(tp
, tp
->cur_tx
+ 1, cur_frag
);
4430 static inline u32
rtl8169_tso_csum(struct sk_buff
*skb
, struct net_device
*dev
)
4432 if (dev
->features
& NETIF_F_TSO
) {
4433 u32 mss
= skb_shinfo(skb
)->gso_size
;
4436 return LargeSend
| ((mss
& MSSMask
) << MSSShift
);
4438 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
4439 const struct iphdr
*ip
= ip_hdr(skb
);
4441 if (ip
->protocol
== IPPROTO_TCP
)
4442 return IPCS
| TCPCS
;
4443 else if (ip
->protocol
== IPPROTO_UDP
)
4444 return IPCS
| UDPCS
;
4445 WARN_ON(1); /* we need a WARN() */
4450 static netdev_tx_t
rtl8169_start_xmit(struct sk_buff
*skb
,
4451 struct net_device
*dev
)
4453 struct rtl8169_private
*tp
= netdev_priv(dev
);
4454 unsigned int entry
= tp
->cur_tx
% NUM_TX_DESC
;
4455 struct TxDesc
*txd
= tp
->TxDescArray
+ entry
;
4456 void __iomem
*ioaddr
= tp
->mmio_addr
;
4457 struct device
*d
= &tp
->pci_dev
->dev
;
4463 if (unlikely(TX_BUFFS_AVAIL(tp
) < skb_shinfo(skb
)->nr_frags
)) {
4464 netif_err(tp
, drv
, dev
, "BUG! Tx Ring full when queue awake!\n");
4468 if (unlikely(le32_to_cpu(txd
->opts1
) & DescOwn
))
4471 len
= skb_headlen(skb
);
4472 mapping
= dma_map_single(d
, skb
->data
, len
, DMA_TO_DEVICE
);
4473 if (unlikely(dma_mapping_error(d
, mapping
))) {
4474 if (net_ratelimit())
4475 netif_err(tp
, drv
, dev
, "Failed to map TX DMA!\n");
4479 tp
->tx_skb
[entry
].len
= len
;
4480 txd
->addr
= cpu_to_le64(mapping
);
4481 txd
->opts2
= cpu_to_le32(rtl8169_tx_vlan_tag(tp
, skb
));
4483 opts1
= DescOwn
| rtl8169_tso_csum(skb
, dev
);
4485 frags
= rtl8169_xmit_frags(tp
, skb
, opts1
);
4491 opts1
|= FirstFrag
| LastFrag
;
4492 tp
->tx_skb
[entry
].skb
= skb
;
4497 /* anti gcc 2.95.3 bugware (sic) */
4498 status
= opts1
| len
| (RingEnd
* !((entry
+ 1) % NUM_TX_DESC
));
4499 txd
->opts1
= cpu_to_le32(status
);
4501 tp
->cur_tx
+= frags
+ 1;
4505 RTL_W8(TxPoll
, NPQ
); /* set polling bit */
4507 if (TX_BUFFS_AVAIL(tp
) < MAX_SKB_FRAGS
) {
4508 netif_stop_queue(dev
);
4510 if (TX_BUFFS_AVAIL(tp
) >= MAX_SKB_FRAGS
)
4511 netif_wake_queue(dev
);
4514 return NETDEV_TX_OK
;
4517 rtl8169_unmap_tx_skb(d
, tp
->tx_skb
+ entry
, txd
);
4520 dev
->stats
.tx_dropped
++;
4521 return NETDEV_TX_OK
;
4524 netif_stop_queue(dev
);
4525 dev
->stats
.tx_dropped
++;
4526 return NETDEV_TX_BUSY
;
4529 static void rtl8169_pcierr_interrupt(struct net_device
*dev
)
4531 struct rtl8169_private
*tp
= netdev_priv(dev
);
4532 struct pci_dev
*pdev
= tp
->pci_dev
;
4533 u16 pci_status
, pci_cmd
;
4535 pci_read_config_word(pdev
, PCI_COMMAND
, &pci_cmd
);
4536 pci_read_config_word(pdev
, PCI_STATUS
, &pci_status
);
4538 netif_err(tp
, intr
, dev
, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4539 pci_cmd
, pci_status
);
4542 * The recovery sequence below admits a very elaborated explanation:
4543 * - it seems to work;
4544 * - I did not see what else could be done;
4545 * - it makes iop3xx happy.
4547 * Feel free to adjust to your needs.
4549 if (pdev
->broken_parity_status
)
4550 pci_cmd
&= ~PCI_COMMAND_PARITY
;
4552 pci_cmd
|= PCI_COMMAND_SERR
| PCI_COMMAND_PARITY
;
4554 pci_write_config_word(pdev
, PCI_COMMAND
, pci_cmd
);
4556 pci_write_config_word(pdev
, PCI_STATUS
,
4557 pci_status
& (PCI_STATUS_DETECTED_PARITY
|
4558 PCI_STATUS_SIG_SYSTEM_ERROR
| PCI_STATUS_REC_MASTER_ABORT
|
4559 PCI_STATUS_REC_TARGET_ABORT
| PCI_STATUS_SIG_TARGET_ABORT
));
4561 /* The infamous DAC f*ckup only happens at boot time */
4562 if ((tp
->cp_cmd
& PCIDAC
) && !tp
->dirty_rx
&& !tp
->cur_rx
) {
4563 void __iomem
*ioaddr
= tp
->mmio_addr
;
4565 netif_info(tp
, intr
, dev
, "disabling PCI DAC\n");
4566 tp
->cp_cmd
&= ~PCIDAC
;
4567 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
4568 dev
->features
&= ~NETIF_F_HIGHDMA
;
4571 rtl8169_hw_reset(tp
);
4573 rtl8169_schedule_work(dev
, rtl8169_reinit_task
);
4576 static void rtl8169_tx_interrupt(struct net_device
*dev
,
4577 struct rtl8169_private
*tp
,
4578 void __iomem
*ioaddr
)
4580 unsigned int dirty_tx
, tx_left
;
4582 dirty_tx
= tp
->dirty_tx
;
4584 tx_left
= tp
->cur_tx
- dirty_tx
;
4586 while (tx_left
> 0) {
4587 unsigned int entry
= dirty_tx
% NUM_TX_DESC
;
4588 struct ring_info
*tx_skb
= tp
->tx_skb
+ entry
;
4592 status
= le32_to_cpu(tp
->TxDescArray
[entry
].opts1
);
4593 if (status
& DescOwn
)
4596 rtl8169_unmap_tx_skb(&tp
->pci_dev
->dev
, tx_skb
,
4597 tp
->TxDescArray
+ entry
);
4598 if (status
& LastFrag
) {
4599 dev
->stats
.tx_packets
++;
4600 dev
->stats
.tx_bytes
+= tx_skb
->skb
->len
;
4601 dev_kfree_skb(tx_skb
->skb
);
4608 if (tp
->dirty_tx
!= dirty_tx
) {
4609 tp
->dirty_tx
= dirty_tx
;
4611 if (netif_queue_stopped(dev
) &&
4612 (TX_BUFFS_AVAIL(tp
) >= MAX_SKB_FRAGS
)) {
4613 netif_wake_queue(dev
);
4616 * 8168 hack: TxPoll requests are lost when the Tx packets are
4617 * too close. Let's kick an extra TxPoll request when a burst
4618 * of start_xmit activity is detected (if it is not detected,
4619 * it is slow enough). -- FR
4622 if (tp
->cur_tx
!= dirty_tx
)
4623 RTL_W8(TxPoll
, NPQ
);
4627 static inline int rtl8169_fragmented_frame(u32 status
)
4629 return (status
& (FirstFrag
| LastFrag
)) != (FirstFrag
| LastFrag
);
4632 static inline void rtl8169_rx_csum(struct sk_buff
*skb
, u32 opts1
)
4634 u32 status
= opts1
& RxProtoMask
;
4636 if (((status
== RxProtoTCP
) && !(opts1
& TCPFail
)) ||
4637 ((status
== RxProtoUDP
) && !(opts1
& UDPFail
)))
4638 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
4640 skb_checksum_none_assert(skb
);
4643 static struct sk_buff
*rtl8169_try_rx_copy(void *data
,
4644 struct rtl8169_private
*tp
,
4648 struct sk_buff
*skb
;
4649 struct device
*d
= &tp
->pci_dev
->dev
;
4651 data
= rtl8169_align(data
);
4652 dma_sync_single_for_cpu(d
, addr
, pkt_size
, DMA_FROM_DEVICE
);
4654 skb
= netdev_alloc_skb_ip_align(tp
->dev
, pkt_size
);
4656 memcpy(skb
->data
, data
, pkt_size
);
4657 dma_sync_single_for_device(d
, addr
, pkt_size
, DMA_FROM_DEVICE
);
4663 * Warning : rtl8169_rx_interrupt() might be called :
4664 * 1) from NAPI (softirq) context
4665 * (polling = 1 : we should call netif_receive_skb())
4666 * 2) from process context (rtl8169_reset_task())
4667 * (polling = 0 : we must call netif_rx() instead)
4669 static int rtl8169_rx_interrupt(struct net_device
*dev
,
4670 struct rtl8169_private
*tp
,
4671 void __iomem
*ioaddr
, u32 budget
)
4673 unsigned int cur_rx
, rx_left
;
4675 int polling
= (budget
!= ~(u32
)0) ? 1 : 0;
4677 cur_rx
= tp
->cur_rx
;
4678 rx_left
= NUM_RX_DESC
+ tp
->dirty_rx
- cur_rx
;
4679 rx_left
= min(rx_left
, budget
);
4681 for (; rx_left
> 0; rx_left
--, cur_rx
++) {
4682 unsigned int entry
= cur_rx
% NUM_RX_DESC
;
4683 struct RxDesc
*desc
= tp
->RxDescArray
+ entry
;
4687 status
= le32_to_cpu(desc
->opts1
);
4689 if (status
& DescOwn
)
4691 if (unlikely(status
& RxRES
)) {
4692 netif_info(tp
, rx_err
, dev
, "Rx ERROR. status = %08x\n",
4694 dev
->stats
.rx_errors
++;
4695 if (status
& (RxRWT
| RxRUNT
))
4696 dev
->stats
.rx_length_errors
++;
4698 dev
->stats
.rx_crc_errors
++;
4699 if (status
& RxFOVF
) {
4700 rtl8169_schedule_work(dev
, rtl8169_reset_task
);
4701 dev
->stats
.rx_fifo_errors
++;
4703 rtl8169_mark_to_asic(desc
, rx_buf_sz
);
4705 struct sk_buff
*skb
;
4706 dma_addr_t addr
= le64_to_cpu(desc
->addr
);
4707 int pkt_size
= (status
& 0x00001FFF) - 4;
4710 * The driver does not support incoming fragmented
4711 * frames. They are seen as a symptom of over-mtu
4714 if (unlikely(rtl8169_fragmented_frame(status
))) {
4715 dev
->stats
.rx_dropped
++;
4716 dev
->stats
.rx_length_errors
++;
4717 rtl8169_mark_to_asic(desc
, rx_buf_sz
);
4721 skb
= rtl8169_try_rx_copy(tp
->Rx_databuff
[entry
],
4722 tp
, pkt_size
, addr
);
4723 rtl8169_mark_to_asic(desc
, rx_buf_sz
);
4725 dev
->stats
.rx_dropped
++;
4729 rtl8169_rx_csum(skb
, status
);
4730 skb_put(skb
, pkt_size
);
4731 skb
->protocol
= eth_type_trans(skb
, dev
);
4733 rtl8169_rx_vlan_tag(desc
, skb
);
4735 if (likely(polling
))
4736 napi_gro_receive(&tp
->napi
, skb
);
4740 dev
->stats
.rx_bytes
+= pkt_size
;
4741 dev
->stats
.rx_packets
++;
4744 /* Work around for AMD plateform. */
4745 if ((desc
->opts2
& cpu_to_le32(0xfffe000)) &&
4746 (tp
->mac_version
== RTL_GIGA_MAC_VER_05
)) {
4752 count
= cur_rx
- tp
->cur_rx
;
4753 tp
->cur_rx
= cur_rx
;
4755 tp
->dirty_rx
+= count
;
4760 static irqreturn_t
rtl8169_interrupt(int irq
, void *dev_instance
)
4762 struct net_device
*dev
= dev_instance
;
4763 struct rtl8169_private
*tp
= netdev_priv(dev
);
4764 void __iomem
*ioaddr
= tp
->mmio_addr
;
4768 /* loop handling interrupts until we have no new ones or
4769 * we hit a invalid/hotplug case.
4771 status
= RTL_R16(IntrStatus
);
4772 while (status
&& status
!= 0xffff) {
4775 /* Handle all of the error cases first. These will reset
4776 * the chip, so just exit the loop.
4778 if (unlikely(!netif_running(dev
))) {
4779 rtl8169_asic_down(ioaddr
);
4783 if (unlikely(status
& RxFIFOOver
)) {
4784 switch (tp
->mac_version
) {
4785 /* Work around for rx fifo overflow */
4786 case RTL_GIGA_MAC_VER_11
:
4787 case RTL_GIGA_MAC_VER_22
:
4788 case RTL_GIGA_MAC_VER_26
:
4789 netif_stop_queue(dev
);
4790 rtl8169_tx_timeout(dev
);
4792 /* Testers needed. */
4793 case RTL_GIGA_MAC_VER_17
:
4794 case RTL_GIGA_MAC_VER_19
:
4795 case RTL_GIGA_MAC_VER_20
:
4796 case RTL_GIGA_MAC_VER_21
:
4797 case RTL_GIGA_MAC_VER_23
:
4798 case RTL_GIGA_MAC_VER_24
:
4799 case RTL_GIGA_MAC_VER_27
:
4800 case RTL_GIGA_MAC_VER_28
:
4801 /* Experimental science. Pktgen proof. */
4802 case RTL_GIGA_MAC_VER_12
:
4803 case RTL_GIGA_MAC_VER_25
:
4804 if (status
== RxFIFOOver
)
4812 if (unlikely(status
& SYSErr
)) {
4813 rtl8169_pcierr_interrupt(dev
);
4817 if (status
& LinkChg
)
4818 __rtl8169_check_link_status(dev
, tp
, ioaddr
, true);
4820 /* We need to see the lastest version of tp->intr_mask to
4821 * avoid ignoring an MSI interrupt and having to wait for
4822 * another event which may never come.
4825 if (status
& tp
->intr_mask
& tp
->napi_event
) {
4826 RTL_W16(IntrMask
, tp
->intr_event
& ~tp
->napi_event
);
4827 tp
->intr_mask
= ~tp
->napi_event
;
4829 if (likely(napi_schedule_prep(&tp
->napi
)))
4830 __napi_schedule(&tp
->napi
);
4832 netif_info(tp
, intr
, dev
,
4833 "interrupt %04x in poll\n", status
);
4836 /* We only get a new MSI interrupt when all active irq
4837 * sources on the chip have been acknowledged. So, ack
4838 * everything we've seen and check if new sources have become
4839 * active to avoid blocking all interrupts from the chip.
4842 (status
& RxFIFOOver
) ? (status
| RxOverflow
) : status
);
4843 status
= RTL_R16(IntrStatus
);
4846 return IRQ_RETVAL(handled
);
4849 static int rtl8169_poll(struct napi_struct
*napi
, int budget
)
4851 struct rtl8169_private
*tp
= container_of(napi
, struct rtl8169_private
, napi
);
4852 struct net_device
*dev
= tp
->dev
;
4853 void __iomem
*ioaddr
= tp
->mmio_addr
;
4856 work_done
= rtl8169_rx_interrupt(dev
, tp
, ioaddr
, (u32
) budget
);
4857 rtl8169_tx_interrupt(dev
, tp
, ioaddr
);
4859 if (work_done
< budget
) {
4860 napi_complete(napi
);
4862 /* We need for force the visibility of tp->intr_mask
4863 * for other CPUs, as we can loose an MSI interrupt
4864 * and potentially wait for a retransmit timeout if we don't.
4865 * The posted write to IntrMask is safe, as it will
4866 * eventually make it to the chip and we won't loose anything
4869 tp
->intr_mask
= 0xffff;
4871 RTL_W16(IntrMask
, tp
->intr_event
);
4877 static void rtl8169_rx_missed(struct net_device
*dev
, void __iomem
*ioaddr
)
4879 struct rtl8169_private
*tp
= netdev_priv(dev
);
4881 if (tp
->mac_version
> RTL_GIGA_MAC_VER_06
)
4884 dev
->stats
.rx_missed_errors
+= (RTL_R32(RxMissed
) & 0xffffff);
4885 RTL_W32(RxMissed
, 0);
4888 static void rtl8169_down(struct net_device
*dev
)
4890 struct rtl8169_private
*tp
= netdev_priv(dev
);
4891 void __iomem
*ioaddr
= tp
->mmio_addr
;
4893 rtl8169_delete_timer(dev
);
4895 netif_stop_queue(dev
);
4897 napi_disable(&tp
->napi
);
4899 spin_lock_irq(&tp
->lock
);
4901 rtl8169_asic_down(ioaddr
);
4903 * At this point device interrupts can not be enabled in any function,
4904 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
4905 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
4907 rtl8169_rx_missed(dev
, ioaddr
);
4909 spin_unlock_irq(&tp
->lock
);
4911 synchronize_irq(dev
->irq
);
4913 /* Give a racing hard_start_xmit a few cycles to complete. */
4914 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
4916 rtl8169_tx_clear(tp
);
4918 rtl8169_rx_clear(tp
);
4920 rtl_pll_power_down(tp
);
4923 static int rtl8169_close(struct net_device
*dev
)
4925 struct rtl8169_private
*tp
= netdev_priv(dev
);
4926 struct pci_dev
*pdev
= tp
->pci_dev
;
4928 pm_runtime_get_sync(&pdev
->dev
);
4930 /* update counters before going down */
4931 rtl8169_update_counters(dev
);
4935 free_irq(dev
->irq
, dev
);
4937 dma_free_coherent(&pdev
->dev
, R8169_RX_RING_BYTES
, tp
->RxDescArray
,
4939 dma_free_coherent(&pdev
->dev
, R8169_TX_RING_BYTES
, tp
->TxDescArray
,
4941 tp
->TxDescArray
= NULL
;
4942 tp
->RxDescArray
= NULL
;
4944 pm_runtime_put_sync(&pdev
->dev
);
4949 static void rtl_set_rx_mode(struct net_device
*dev
)
4951 struct rtl8169_private
*tp
= netdev_priv(dev
);
4952 void __iomem
*ioaddr
= tp
->mmio_addr
;
4953 unsigned long flags
;
4954 u32 mc_filter
[2]; /* Multicast hash filter */
4958 if (dev
->flags
& IFF_PROMISC
) {
4959 /* Unconditionally log net taps. */
4960 netif_notice(tp
, link
, dev
, "Promiscuous mode enabled\n");
4962 AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
|
4964 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
4965 } else if ((netdev_mc_count(dev
) > multicast_filter_limit
) ||
4966 (dev
->flags
& IFF_ALLMULTI
)) {
4967 /* Too many to filter perfectly -- accept all multicasts. */
4968 rx_mode
= AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
;
4969 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
4971 struct netdev_hw_addr
*ha
;
4973 rx_mode
= AcceptBroadcast
| AcceptMyPhys
;
4974 mc_filter
[1] = mc_filter
[0] = 0;
4975 netdev_for_each_mc_addr(ha
, dev
) {
4976 int bit_nr
= ether_crc(ETH_ALEN
, ha
->addr
) >> 26;
4977 mc_filter
[bit_nr
>> 5] |= 1 << (bit_nr
& 31);
4978 rx_mode
|= AcceptMulticast
;
4982 spin_lock_irqsave(&tp
->lock
, flags
);
4984 tmp
= rtl8169_rx_config
| rx_mode
|
4985 (RTL_R32(RxConfig
) & rtl_chip_info
[tp
->chipset
].RxConfigMask
);
4987 if (tp
->mac_version
> RTL_GIGA_MAC_VER_06
) {
4988 u32 data
= mc_filter
[0];
4990 mc_filter
[0] = swab32(mc_filter
[1]);
4991 mc_filter
[1] = swab32(data
);
4994 RTL_W32(MAR0
+ 4, mc_filter
[1]);
4995 RTL_W32(MAR0
+ 0, mc_filter
[0]);
4997 RTL_W32(RxConfig
, tmp
);
4999 spin_unlock_irqrestore(&tp
->lock
, flags
);
5003 * rtl8169_get_stats - Get rtl8169 read/write statistics
5004 * @dev: The Ethernet Device to get statistics for
5006 * Get TX/RX statistics for rtl8169
5008 static struct net_device_stats
*rtl8169_get_stats(struct net_device
*dev
)
5010 struct rtl8169_private
*tp
= netdev_priv(dev
);
5011 void __iomem
*ioaddr
= tp
->mmio_addr
;
5012 unsigned long flags
;
5014 if (netif_running(dev
)) {
5015 spin_lock_irqsave(&tp
->lock
, flags
);
5016 rtl8169_rx_missed(dev
, ioaddr
);
5017 spin_unlock_irqrestore(&tp
->lock
, flags
);
5023 static void rtl8169_net_suspend(struct net_device
*dev
)
5025 struct rtl8169_private
*tp
= netdev_priv(dev
);
5027 if (!netif_running(dev
))
5030 rtl_pll_power_down(tp
);
5032 netif_device_detach(dev
);
5033 netif_stop_queue(dev
);
5038 static int rtl8169_suspend(struct device
*device
)
5040 struct pci_dev
*pdev
= to_pci_dev(device
);
5041 struct net_device
*dev
= pci_get_drvdata(pdev
);
5043 rtl8169_net_suspend(dev
);
5048 static void __rtl8169_resume(struct net_device
*dev
)
5050 struct rtl8169_private
*tp
= netdev_priv(dev
);
5052 netif_device_attach(dev
);
5054 rtl_pll_power_up(tp
);
5056 rtl8169_schedule_work(dev
, rtl8169_reset_task
);
5059 static int rtl8169_resume(struct device
*device
)
5061 struct pci_dev
*pdev
= to_pci_dev(device
);
5062 struct net_device
*dev
= pci_get_drvdata(pdev
);
5063 struct rtl8169_private
*tp
= netdev_priv(dev
);
5065 rtl8169_init_phy(dev
, tp
);
5067 if (netif_running(dev
))
5068 __rtl8169_resume(dev
);
5073 static int rtl8169_runtime_suspend(struct device
*device
)
5075 struct pci_dev
*pdev
= to_pci_dev(device
);
5076 struct net_device
*dev
= pci_get_drvdata(pdev
);
5077 struct rtl8169_private
*tp
= netdev_priv(dev
);
5079 if (!tp
->TxDescArray
)
5082 spin_lock_irq(&tp
->lock
);
5083 tp
->saved_wolopts
= __rtl8169_get_wol(tp
);
5084 __rtl8169_set_wol(tp
, WAKE_ANY
);
5085 spin_unlock_irq(&tp
->lock
);
5087 rtl8169_net_suspend(dev
);
5092 static int rtl8169_runtime_resume(struct device
*device
)
5094 struct pci_dev
*pdev
= to_pci_dev(device
);
5095 struct net_device
*dev
= pci_get_drvdata(pdev
);
5096 struct rtl8169_private
*tp
= netdev_priv(dev
);
5098 if (!tp
->TxDescArray
)
5101 spin_lock_irq(&tp
->lock
);
5102 __rtl8169_set_wol(tp
, tp
->saved_wolopts
);
5103 tp
->saved_wolopts
= 0;
5104 spin_unlock_irq(&tp
->lock
);
5106 rtl8169_init_phy(dev
, tp
);
5108 __rtl8169_resume(dev
);
5113 static int rtl8169_runtime_idle(struct device
*device
)
5115 struct pci_dev
*pdev
= to_pci_dev(device
);
5116 struct net_device
*dev
= pci_get_drvdata(pdev
);
5117 struct rtl8169_private
*tp
= netdev_priv(dev
);
5119 return tp
->TxDescArray
? -EBUSY
: 0;
5122 static const struct dev_pm_ops rtl8169_pm_ops
= {
5123 .suspend
= rtl8169_suspend
,
5124 .resume
= rtl8169_resume
,
5125 .freeze
= rtl8169_suspend
,
5126 .thaw
= rtl8169_resume
,
5127 .poweroff
= rtl8169_suspend
,
5128 .restore
= rtl8169_resume
,
5129 .runtime_suspend
= rtl8169_runtime_suspend
,
5130 .runtime_resume
= rtl8169_runtime_resume
,
5131 .runtime_idle
= rtl8169_runtime_idle
,
5134 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
5136 #else /* !CONFIG_PM */
5138 #define RTL8169_PM_OPS NULL
5140 #endif /* !CONFIG_PM */
5142 static void rtl_shutdown(struct pci_dev
*pdev
)
5144 struct net_device
*dev
= pci_get_drvdata(pdev
);
5145 struct rtl8169_private
*tp
= netdev_priv(dev
);
5146 void __iomem
*ioaddr
= tp
->mmio_addr
;
5148 rtl8169_net_suspend(dev
);
5150 /* restore original MAC address */
5151 rtl_rar_set(tp
, dev
->perm_addr
);
5153 spin_lock_irq(&tp
->lock
);
5155 rtl8169_asic_down(ioaddr
);
5157 spin_unlock_irq(&tp
->lock
);
5159 if (system_state
== SYSTEM_POWER_OFF
) {
5160 /* WoL fails with some 8168 when the receiver is disabled. */
5161 if (tp
->features
& RTL_FEATURE_WOL
) {
5162 pci_clear_master(pdev
);
5164 RTL_W8(ChipCmd
, CmdRxEnb
);
5169 pci_wake_from_d3(pdev
, true);
5170 pci_set_power_state(pdev
, PCI_D3hot
);
5174 static struct pci_driver rtl8169_pci_driver
= {
5176 .id_table
= rtl8169_pci_tbl
,
5177 .probe
= rtl8169_init_one
,
5178 .remove
= __devexit_p(rtl8169_remove_one
),
5179 .shutdown
= rtl_shutdown
,
5180 .driver
.pm
= RTL8169_PM_OPS
,
5183 static int __init
rtl8169_init_module(void)
5185 return pci_register_driver(&rtl8169_pci_driver
);
5188 static void __exit
rtl8169_cleanup_module(void)
5190 pci_unregister_driver(&rtl8169_pci_driver
);
5193 module_init(rtl8169_init_module
);
5194 module_exit(rtl8169_cleanup_module
);