2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/if_vlan.h>
20 #include <linux/crc32.h>
23 #include <linux/tcp.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/firmware.h>
29 #include <linux/pci-aspm.h>
30 #include <linux/prefetch.h>
35 #define RTL8169_VERSION "2.3LK-NAPI"
36 #define MODULENAME "r8169"
37 #define PFX MODULENAME ": "
39 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
41 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
43 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
44 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
46 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
47 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
48 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
49 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
52 #define assert(expr) \
54 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
55 #expr,__FILE__,__func__,__LINE__); \
57 #define dprintk(fmt, args...) \
58 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
60 #define assert(expr) do {} while (0)
61 #define dprintk(fmt, args...) do {} while (0)
62 #endif /* RTL8169_DEBUG */
64 #define R8169_MSG_DEFAULT \
65 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
67 #define TX_SLOTS_AVAIL(tp) \
68 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
70 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
71 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
72 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
74 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
75 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
76 static const int multicast_filter_limit = 32;
78 #define MAX_READ_REQUEST_SHIFT 12
79 #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
80 #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
81 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
83 #define R8169_REGS_SIZE 256
84 #define R8169_NAPI_WEIGHT 64
85 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
86 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
87 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
88 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
89 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
91 #define RTL8169_TX_TIMEOUT (6*HZ)
92 #define RTL8169_PHY_TIMEOUT (10*HZ)
94 #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
95 #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
96 #define RTL_EEPROM_SIG_ADDR 0x0000
98 /* write/read MMIO register */
99 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
100 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
101 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
102 #define RTL_R8(reg) readb (ioaddr + (reg))
103 #define RTL_R16(reg) readw (ioaddr + (reg))
104 #define RTL_R32(reg) readl (ioaddr + (reg))
107 RTL_GIGA_MAC_VER_01 = 0,
146 RTL_GIGA_MAC_NONE = 0xff,
149 enum rtl_tx_desc_version {
154 #define JUMBO_1K ETH_DATA_LEN
155 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
156 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
157 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
158 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
160 #define _R(NAME,TD,FW,SZ,B) { \
168 static const struct {
170 enum rtl_tx_desc_version txd_version;
174 } rtl_chip_infos[] = {
176 [RTL_GIGA_MAC_VER_01] =
177 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
178 [RTL_GIGA_MAC_VER_02] =
179 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
180 [RTL_GIGA_MAC_VER_03] =
181 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
182 [RTL_GIGA_MAC_VER_04] =
183 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
184 [RTL_GIGA_MAC_VER_05] =
185 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
186 [RTL_GIGA_MAC_VER_06] =
187 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
189 [RTL_GIGA_MAC_VER_07] =
190 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
191 [RTL_GIGA_MAC_VER_08] =
192 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
193 [RTL_GIGA_MAC_VER_09] =
194 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
195 [RTL_GIGA_MAC_VER_10] =
196 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
197 [RTL_GIGA_MAC_VER_11] =
198 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
199 [RTL_GIGA_MAC_VER_12] =
200 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
201 [RTL_GIGA_MAC_VER_13] =
202 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
203 [RTL_GIGA_MAC_VER_14] =
204 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
205 [RTL_GIGA_MAC_VER_15] =
206 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
207 [RTL_GIGA_MAC_VER_16] =
208 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
209 [RTL_GIGA_MAC_VER_17] =
210 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
211 [RTL_GIGA_MAC_VER_18] =
212 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
213 [RTL_GIGA_MAC_VER_19] =
214 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
215 [RTL_GIGA_MAC_VER_20] =
216 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
217 [RTL_GIGA_MAC_VER_21] =
218 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
219 [RTL_GIGA_MAC_VER_22] =
220 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
221 [RTL_GIGA_MAC_VER_23] =
222 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
223 [RTL_GIGA_MAC_VER_24] =
224 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
225 [RTL_GIGA_MAC_VER_25] =
226 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
228 [RTL_GIGA_MAC_VER_26] =
229 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
231 [RTL_GIGA_MAC_VER_27] =
232 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
233 [RTL_GIGA_MAC_VER_28] =
234 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
235 [RTL_GIGA_MAC_VER_29] =
236 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
238 [RTL_GIGA_MAC_VER_30] =
239 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
241 [RTL_GIGA_MAC_VER_31] =
242 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
243 [RTL_GIGA_MAC_VER_32] =
244 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
246 [RTL_GIGA_MAC_VER_33] =
247 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
249 [RTL_GIGA_MAC_VER_34] =
250 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
252 [RTL_GIGA_MAC_VER_35] =
253 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
255 [RTL_GIGA_MAC_VER_36] =
256 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
258 [RTL_GIGA_MAC_VER_37] =
259 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
261 [RTL_GIGA_MAC_VER_38] =
262 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
264 [RTL_GIGA_MAC_VER_39] =
265 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
276 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
277 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
278 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
279 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
280 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
281 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
282 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
283 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
284 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
285 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
286 { PCI_VENDOR_ID_LINKSYS, 0x1032,
287 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
289 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
293 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
295 static int rx_buf_sz = 16383;
302 MAC0 = 0, /* Ethernet hardware address. */
304 MAR0 = 8, /* Multicast filter. */
305 CounterAddrLow = 0x10,
306 CounterAddrHigh = 0x14,
307 TxDescStartAddrLow = 0x20,
308 TxDescStartAddrHigh = 0x24,
309 TxHDescStartAddrLow = 0x28,
310 TxHDescStartAddrHigh = 0x2c,
319 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
320 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
323 #define RX128_INT_EN (1 << 15) /* 8111c and later */
324 #define RX_MULTI_EN (1 << 14) /* 8111c only */
325 #define RXCFG_FIFO_SHIFT 13
326 /* No threshold before first PCI xfer */
327 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
328 #define RXCFG_DMA_SHIFT 8
329 /* Unlimited maximum PCI burst. */
330 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
337 #define PME_SIGNAL (1 << 5) /* 8168c and later */
348 RxDescAddrLow = 0xe4,
349 RxDescAddrHigh = 0xe8,
350 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
352 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
354 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
356 #define TxPacketMax (8064 >> 7)
357 #define EarlySize 0x27
360 FuncEventMask = 0xf4,
361 FuncPresetState = 0xf8,
362 FuncForceEvent = 0xfc,
365 enum rtl8110_registers {
371 enum rtl8168_8101_registers {
374 #define CSIAR_FLAG 0x80000000
375 #define CSIAR_WRITE_CMD 0x80000000
376 #define CSIAR_BYTE_ENABLE 0x0f
377 #define CSIAR_BYTE_ENABLE_SHIFT 12
378 #define CSIAR_ADDR_MASK 0x0fff
379 #define CSIAR_FUNC_CARD 0x00000000
380 #define CSIAR_FUNC_SDIO 0x00010000
381 #define CSIAR_FUNC_NIC 0x00020000
384 #define EPHYAR_FLAG 0x80000000
385 #define EPHYAR_WRITE_CMD 0x80000000
386 #define EPHYAR_REG_MASK 0x1f
387 #define EPHYAR_REG_SHIFT 16
388 #define EPHYAR_DATA_MASK 0xffff
390 #define PFM_EN (1 << 6)
392 #define FIX_NAK_1 (1 << 4)
393 #define FIX_NAK_2 (1 << 3)
396 #define NOW_IS_OOB (1 << 7)
397 #define EN_NDP (1 << 3)
398 #define EN_OOB_RESET (1 << 2)
400 #define EFUSEAR_FLAG 0x80000000
401 #define EFUSEAR_WRITE_CMD 0x80000000
402 #define EFUSEAR_READ_CMD 0x00000000
403 #define EFUSEAR_REG_MASK 0x03ff
404 #define EFUSEAR_REG_SHIFT 8
405 #define EFUSEAR_DATA_MASK 0xff
408 enum rtl8168_registers {
413 #define ERIAR_FLAG 0x80000000
414 #define ERIAR_WRITE_CMD 0x80000000
415 #define ERIAR_READ_CMD 0x00000000
416 #define ERIAR_ADDR_BYTE_ALIGN 4
417 #define ERIAR_TYPE_SHIFT 16
418 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
419 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
420 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
421 #define ERIAR_MASK_SHIFT 12
422 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
423 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
424 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
425 EPHY_RXER_NUM = 0x7c,
426 OCPDR = 0xb0, /* OCP GPHY access */
427 #define OCPDR_WRITE_CMD 0x80000000
428 #define OCPDR_READ_CMD 0x00000000
429 #define OCPDR_REG_MASK 0x7f
430 #define OCPDR_GPHY_REG_SHIFT 16
431 #define OCPDR_DATA_MASK 0xffff
433 #define OCPAR_FLAG 0x80000000
434 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
435 #define OCPAR_GPHY_READ_CMD 0x0000f060
436 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
437 MISC = 0xf0, /* 8168e only. */
438 #define TXPLA_RST (1 << 29)
439 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
440 #define PWM_EN (1 << 22)
441 #define EARLY_TALLY_EN (1 << 16)
444 enum rtl_register_content {
445 /* InterruptStatusBits */
449 TxDescUnavail = 0x0080,
473 /* TXPoll register p.5 */
474 HPQ = 0x80, /* Poll cmd on the high prio queue */
475 NPQ = 0x40, /* Poll cmd on the low prio queue */
476 FSWInt = 0x01, /* Forced software interrupt */
480 Cfg9346_Unlock = 0xc0,
485 AcceptBroadcast = 0x08,
486 AcceptMulticast = 0x04,
488 AcceptAllPhys = 0x01,
489 #define RX_CONFIG_ACCEPT_MASK 0x3f
492 TxInterFrameGapShift = 24,
493 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
495 /* Config1 register p.24 */
498 Speed_down = (1 << 4),
502 PMEnable = (1 << 0), /* Power Management Enable */
504 /* Config2 register p. 25 */
505 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
506 PCI_Clock_66MHz = 0x01,
507 PCI_Clock_33MHz = 0x00,
509 /* Config3 register p.25 */
510 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
511 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
512 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
513 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
515 /* Config4 register */
516 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
518 /* Config5 register p.27 */
519 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
520 MWF = (1 << 5), /* Accept Multicast wakeup frame */
521 UWF = (1 << 4), /* Accept Unicast wakeup frame */
523 LanWake = (1 << 1), /* LanWake enable/disable */
524 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
527 TBIReset = 0x80000000,
528 TBILoopback = 0x40000000,
529 TBINwEnable = 0x20000000,
530 TBINwRestart = 0x10000000,
531 TBILinkOk = 0x02000000,
532 TBINwComplete = 0x01000000,
535 EnableBist = (1 << 15), // 8168 8101
536 Mac_dbgo_oe = (1 << 14), // 8168 8101
537 Normal_mode = (1 << 13), // unused
538 Force_half_dup = (1 << 12), // 8168 8101
539 Force_rxflow_en = (1 << 11), // 8168 8101
540 Force_txflow_en = (1 << 10), // 8168 8101
541 Cxpl_dbg_sel = (1 << 9), // 8168 8101
542 ASF = (1 << 8), // 8168 8101
543 PktCntrDisable = (1 << 7), // 8168 8101
544 Mac_dbgo_sel = 0x001c, // 8168
549 INTT_0 = 0x0000, // 8168
550 INTT_1 = 0x0001, // 8168
551 INTT_2 = 0x0002, // 8168
552 INTT_3 = 0x0003, // 8168
554 /* rtl8169_PHYstatus */
565 TBILinkOK = 0x02000000,
567 /* DumpCounterCommand */
572 /* First doubleword. */
573 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
574 RingEnd = (1 << 30), /* End of descriptor ring */
575 FirstFrag = (1 << 29), /* First segment of a packet */
576 LastFrag = (1 << 28), /* Final segment of a packet */
580 enum rtl_tx_desc_bit {
581 /* First doubleword. */
582 TD_LSO = (1 << 27), /* Large Send Offload */
583 #define TD_MSS_MAX 0x07ffu /* MSS value */
585 /* Second doubleword. */
586 TxVlanTag = (1 << 17), /* Add VLAN tag */
589 /* 8169, 8168b and 810x except 8102e. */
590 enum rtl_tx_desc_bit_0 {
591 /* First doubleword. */
592 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
593 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
594 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
595 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
598 /* 8102e, 8168c and beyond. */
599 enum rtl_tx_desc_bit_1 {
600 /* Second doubleword. */
601 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
602 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
603 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
604 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
607 static const struct rtl_tx_desc_info {
614 } tx_desc_info [] = {
617 .udp = TD0_IP_CS | TD0_UDP_CS,
618 .tcp = TD0_IP_CS | TD0_TCP_CS
620 .mss_shift = TD0_MSS_SHIFT,
625 .udp = TD1_IP_CS | TD1_UDP_CS,
626 .tcp = TD1_IP_CS | TD1_TCP_CS
628 .mss_shift = TD1_MSS_SHIFT,
633 enum rtl_rx_desc_bit {
635 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
636 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
638 #define RxProtoUDP (PID1)
639 #define RxProtoTCP (PID0)
640 #define RxProtoIP (PID1 | PID0)
641 #define RxProtoMask RxProtoIP
643 IPFail = (1 << 16), /* IP checksum failed */
644 UDPFail = (1 << 15), /* UDP/IP checksum failed */
645 TCPFail = (1 << 14), /* TCP/IP checksum failed */
646 RxVlanTag = (1 << 16), /* VLAN tag available */
649 #define RsvdMask 0x3fffc000
666 u8 __pad[sizeof(void *) - sizeof(u32)];
670 RTL_FEATURE_WOL = (1 << 0),
671 RTL_FEATURE_MSI = (1 << 1),
672 RTL_FEATURE_GMII = (1 << 2),
675 struct rtl8169_counters {
682 __le32 tx_one_collision;
683 __le32 tx_multi_collision;
692 RTL_FLAG_TASK_ENABLED,
693 RTL_FLAG_TASK_SLOW_PENDING,
694 RTL_FLAG_TASK_RESET_PENDING,
695 RTL_FLAG_TASK_PHY_PENDING,
699 struct rtl8169_stats {
702 struct u64_stats_sync syncp;
705 struct rtl8169_private {
706 void __iomem *mmio_addr; /* memory map physical address */
707 struct pci_dev *pci_dev;
708 struct net_device *dev;
709 struct napi_struct napi;
713 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
714 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
717 struct rtl8169_stats rx_stats;
718 struct rtl8169_stats tx_stats;
719 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
720 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
721 dma_addr_t TxPhyAddr;
722 dma_addr_t RxPhyAddr;
723 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
724 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
725 struct timer_list timer;
731 void (*write)(void __iomem *, int, int);
732 int (*read)(void __iomem *, int);
735 struct pll_power_ops {
736 void (*down)(struct rtl8169_private *);
737 void (*up)(struct rtl8169_private *);
741 void (*enable)(struct rtl8169_private *);
742 void (*disable)(struct rtl8169_private *);
746 void (*write)(void __iomem *, int, int);
747 u32 (*read)(void __iomem *, int);
750 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
751 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
752 void (*phy_reset_enable)(struct rtl8169_private *tp);
753 void (*hw_start)(struct net_device *);
754 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
755 unsigned int (*link_ok)(void __iomem *);
756 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
759 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
761 struct work_struct work;
766 struct mii_if_info mii;
767 struct rtl8169_counters counters;
772 const struct firmware *fw;
774 #define RTL_VER_SIZE 32
776 char version[RTL_VER_SIZE];
778 struct rtl_fw_phy_action {
783 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
786 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
787 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
788 module_param(use_dac, int, 0);
789 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
790 module_param_named(debug, debug.msg_enable, int, 0);
791 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
792 MODULE_LICENSE("GPL");
793 MODULE_VERSION(RTL8169_VERSION);
794 MODULE_FIRMWARE(FIRMWARE_8168D_1);
795 MODULE_FIRMWARE(FIRMWARE_8168D_2);
796 MODULE_FIRMWARE(FIRMWARE_8168E_1);
797 MODULE_FIRMWARE(FIRMWARE_8168E_2);
798 MODULE_FIRMWARE(FIRMWARE_8168E_3);
799 MODULE_FIRMWARE(FIRMWARE_8105E_1);
800 MODULE_FIRMWARE(FIRMWARE_8168F_1);
801 MODULE_FIRMWARE(FIRMWARE_8168F_2);
802 MODULE_FIRMWARE(FIRMWARE_8402_1);
803 MODULE_FIRMWARE(FIRMWARE_8411_1);
804 MODULE_FIRMWARE(FIRMWARE_8106E_1);
806 static void rtl_lock_work(struct rtl8169_private *tp)
808 mutex_lock(&tp->wk.mutex);
811 static void rtl_unlock_work(struct rtl8169_private *tp)
813 mutex_unlock(&tp->wk.mutex);
816 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
818 int cap = pci_pcie_cap(pdev);
823 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
824 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
825 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
829 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
831 void __iomem *ioaddr = tp->mmio_addr;
834 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
835 for (i = 0; i < 20; i++) {
837 if (RTL_R32(OCPAR) & OCPAR_FLAG)
840 return RTL_R32(OCPDR);
843 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
845 void __iomem *ioaddr = tp->mmio_addr;
848 RTL_W32(OCPDR, data);
849 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
850 for (i = 0; i < 20; i++) {
852 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
857 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
859 void __iomem *ioaddr = tp->mmio_addr;
863 RTL_W32(ERIAR, 0x800010e8);
865 for (i = 0; i < 5; i++) {
867 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
871 ocp_write(tp, 0x1, 0x30, 0x00000001);
874 #define OOB_CMD_RESET 0x00
875 #define OOB_CMD_DRIVER_START 0x05
876 #define OOB_CMD_DRIVER_STOP 0x06
878 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
880 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
883 static void rtl8168_driver_start(struct rtl8169_private *tp)
888 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
890 reg = rtl8168_get_ocp_reg(tp);
892 for (i = 0; i < 10; i++) {
894 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
899 static void rtl8168_driver_stop(struct rtl8169_private *tp)
904 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
906 reg = rtl8168_get_ocp_reg(tp);
908 for (i = 0; i < 10; i++) {
910 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
915 static int r8168dp_check_dash(struct rtl8169_private *tp)
917 u16 reg = rtl8168_get_ocp_reg(tp);
919 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
922 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
926 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
928 for (i = 20; i > 0; i--) {
930 * Check if the RTL8169 has completed writing to the specified
933 if (!(RTL_R32(PHYAR) & 0x80000000))
938 * According to hardware specs a 20us delay is required after write
939 * complete indication, but before sending next command.
944 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
948 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
950 for (i = 20; i > 0; i--) {
952 * Check if the RTL8169 has completed retrieving data from
953 * the specified MII register.
955 if (RTL_R32(PHYAR) & 0x80000000) {
956 value = RTL_R32(PHYAR) & 0xffff;
962 * According to hardware specs a 20us delay is required after read
963 * complete indication, but before sending next command.
970 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
974 RTL_W32(OCPDR, data |
975 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
976 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
977 RTL_W32(EPHY_RXER_NUM, 0);
979 for (i = 0; i < 100; i++) {
981 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
986 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
988 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
989 (value & OCPDR_DATA_MASK));
992 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
996 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
999 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1000 RTL_W32(EPHY_RXER_NUM, 0);
1002 for (i = 0; i < 100; i++) {
1004 if (RTL_R32(OCPAR) & OCPAR_FLAG)
1008 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
1011 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1013 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1015 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1018 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1020 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1023 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1025 r8168dp_2_mdio_start(ioaddr);
1027 r8169_mdio_write(ioaddr, reg_addr, value);
1029 r8168dp_2_mdio_stop(ioaddr);
1032 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1036 r8168dp_2_mdio_start(ioaddr);
1038 value = r8169_mdio_read(ioaddr, reg_addr);
1040 r8168dp_2_mdio_stop(ioaddr);
1045 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1047 tp->mdio_ops.write(tp->mmio_addr, location, val);
1050 static int rtl_readphy(struct rtl8169_private *tp, int location)
1052 return tp->mdio_ops.read(tp->mmio_addr, location);
1055 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1057 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1060 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1064 val = rtl_readphy(tp, reg_addr);
1065 rtl_writephy(tp, reg_addr, (val | p) & ~m);
1068 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1071 struct rtl8169_private *tp = netdev_priv(dev);
1073 rtl_writephy(tp, location, val);
1076 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1078 struct rtl8169_private *tp = netdev_priv(dev);
1080 return rtl_readphy(tp, location);
1083 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1087 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1088 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1090 for (i = 0; i < 100; i++) {
1091 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1097 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1102 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1104 for (i = 0; i < 100; i++) {
1105 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1106 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1116 void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1120 BUG_ON((addr & 3) || (mask == 0));
1121 RTL_W32(ERIDR, val);
1122 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1124 for (i = 0; i < 100; i++) {
1125 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1131 static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1136 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1138 for (i = 0; i < 100; i++) {
1139 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1140 value = RTL_R32(ERIDR);
1150 rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1154 val = rtl_eri_read(ioaddr, addr, type);
1155 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1164 static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1165 const struct exgmac_reg *r, int len)
1168 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1173 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1178 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1180 for (i = 0; i < 300; i++) {
1181 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1182 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1191 static u16 rtl_get_events(struct rtl8169_private *tp)
1193 void __iomem *ioaddr = tp->mmio_addr;
1195 return RTL_R16(IntrStatus);
1198 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1200 void __iomem *ioaddr = tp->mmio_addr;
1202 RTL_W16(IntrStatus, bits);
1206 static void rtl_irq_disable(struct rtl8169_private *tp)
1208 void __iomem *ioaddr = tp->mmio_addr;
1210 RTL_W16(IntrMask, 0);
1214 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1216 void __iomem *ioaddr = tp->mmio_addr;
1218 RTL_W16(IntrMask, bits);
1221 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1222 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1223 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1225 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1227 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1230 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1232 void __iomem *ioaddr = tp->mmio_addr;
1234 rtl_irq_disable(tp);
1235 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1239 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1241 void __iomem *ioaddr = tp->mmio_addr;
1243 return RTL_R32(TBICSR) & TBIReset;
1246 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1248 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1251 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1253 return RTL_R32(TBICSR) & TBILinkOk;
1256 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1258 return RTL_R8(PHYstatus) & LinkStatus;
1261 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1263 void __iomem *ioaddr = tp->mmio_addr;
1265 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1268 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1272 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1273 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1276 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1278 void __iomem *ioaddr = tp->mmio_addr;
1279 struct net_device *dev = tp->dev;
1281 if (!netif_running(dev))
1284 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1285 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1286 if (RTL_R8(PHYstatus) & _1000bpsF) {
1287 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1288 0x00000011, ERIAR_EXGMAC);
1289 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1290 0x00000005, ERIAR_EXGMAC);
1291 } else if (RTL_R8(PHYstatus) & _100bps) {
1292 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1293 0x0000001f, ERIAR_EXGMAC);
1294 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1295 0x00000005, ERIAR_EXGMAC);
1297 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1298 0x0000001f, ERIAR_EXGMAC);
1299 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1300 0x0000003f, ERIAR_EXGMAC);
1302 /* Reset packet filter */
1303 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1305 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1307 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1308 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1309 if (RTL_R8(PHYstatus) & _1000bpsF) {
1310 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1311 0x00000011, ERIAR_EXGMAC);
1312 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1313 0x00000005, ERIAR_EXGMAC);
1315 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1316 0x0000001f, ERIAR_EXGMAC);
1317 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1318 0x0000003f, ERIAR_EXGMAC);
1320 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1321 if (RTL_R8(PHYstatus) & _10bps) {
1322 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1323 0x4d02, ERIAR_EXGMAC);
1324 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_0011,
1325 0x0060, ERIAR_EXGMAC);
1327 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1328 0x0000, ERIAR_EXGMAC);
1333 static void __rtl8169_check_link_status(struct net_device *dev,
1334 struct rtl8169_private *tp,
1335 void __iomem *ioaddr, bool pm)
1337 if (tp->link_ok(ioaddr)) {
1338 rtl_link_chg_patch(tp);
1339 /* This is to cancel a scheduled suspend if there's one. */
1341 pm_request_resume(&tp->pci_dev->dev);
1342 netif_carrier_on(dev);
1343 if (net_ratelimit())
1344 netif_info(tp, ifup, dev, "link up\n");
1346 netif_carrier_off(dev);
1347 netif_info(tp, ifdown, dev, "link down\n");
1349 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1353 static void rtl8169_check_link_status(struct net_device *dev,
1354 struct rtl8169_private *tp,
1355 void __iomem *ioaddr)
1357 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1360 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1362 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1364 void __iomem *ioaddr = tp->mmio_addr;
1368 options = RTL_R8(Config1);
1369 if (!(options & PMEnable))
1372 options = RTL_R8(Config3);
1373 if (options & LinkUp)
1374 wolopts |= WAKE_PHY;
1375 if (options & MagicPacket)
1376 wolopts |= WAKE_MAGIC;
1378 options = RTL_R8(Config5);
1380 wolopts |= WAKE_UCAST;
1382 wolopts |= WAKE_BCAST;
1384 wolopts |= WAKE_MCAST;
1389 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1391 struct rtl8169_private *tp = netdev_priv(dev);
1395 wol->supported = WAKE_ANY;
1396 wol->wolopts = __rtl8169_get_wol(tp);
1398 rtl_unlock_work(tp);
1401 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1403 void __iomem *ioaddr = tp->mmio_addr;
1405 static const struct {
1410 { WAKE_PHY, Config3, LinkUp },
1411 { WAKE_MAGIC, Config3, MagicPacket },
1412 { WAKE_UCAST, Config5, UWF },
1413 { WAKE_BCAST, Config5, BWF },
1414 { WAKE_MCAST, Config5, MWF },
1415 { WAKE_ANY, Config5, LanWake }
1419 RTL_W8(Cfg9346, Cfg9346_Unlock);
1421 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1422 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1423 if (wolopts & cfg[i].opt)
1424 options |= cfg[i].mask;
1425 RTL_W8(cfg[i].reg, options);
1428 switch (tp->mac_version) {
1429 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1430 options = RTL_R8(Config1) & ~PMEnable;
1432 options |= PMEnable;
1433 RTL_W8(Config1, options);
1436 options = RTL_R8(Config2) & ~PME_SIGNAL;
1438 options |= PME_SIGNAL;
1439 RTL_W8(Config2, options);
1443 RTL_W8(Cfg9346, Cfg9346_Lock);
1446 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1448 struct rtl8169_private *tp = netdev_priv(dev);
1453 tp->features |= RTL_FEATURE_WOL;
1455 tp->features &= ~RTL_FEATURE_WOL;
1456 __rtl8169_set_wol(tp, wol->wolopts);
1458 rtl_unlock_work(tp);
1460 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1465 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1467 return rtl_chip_infos[tp->mac_version].fw_name;
1470 static void rtl8169_get_drvinfo(struct net_device *dev,
1471 struct ethtool_drvinfo *info)
1473 struct rtl8169_private *tp = netdev_priv(dev);
1474 struct rtl_fw *rtl_fw = tp->rtl_fw;
1476 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1477 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1478 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1479 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1480 if (!IS_ERR_OR_NULL(rtl_fw))
1481 strlcpy(info->fw_version, rtl_fw->version,
1482 sizeof(info->fw_version));
1485 static int rtl8169_get_regs_len(struct net_device *dev)
1487 return R8169_REGS_SIZE;
1490 static int rtl8169_set_speed_tbi(struct net_device *dev,
1491 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1493 struct rtl8169_private *tp = netdev_priv(dev);
1494 void __iomem *ioaddr = tp->mmio_addr;
1498 reg = RTL_R32(TBICSR);
1499 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1500 (duplex == DUPLEX_FULL)) {
1501 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1502 } else if (autoneg == AUTONEG_ENABLE)
1503 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1505 netif_warn(tp, link, dev,
1506 "incorrect speed setting refused in TBI mode\n");
1513 static int rtl8169_set_speed_xmii(struct net_device *dev,
1514 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1516 struct rtl8169_private *tp = netdev_priv(dev);
1517 int giga_ctrl, bmcr;
1520 rtl_writephy(tp, 0x1f, 0x0000);
1522 if (autoneg == AUTONEG_ENABLE) {
1525 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1526 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1527 ADVERTISE_100HALF | ADVERTISE_100FULL);
1529 if (adv & ADVERTISED_10baseT_Half)
1530 auto_nego |= ADVERTISE_10HALF;
1531 if (adv & ADVERTISED_10baseT_Full)
1532 auto_nego |= ADVERTISE_10FULL;
1533 if (adv & ADVERTISED_100baseT_Half)
1534 auto_nego |= ADVERTISE_100HALF;
1535 if (adv & ADVERTISED_100baseT_Full)
1536 auto_nego |= ADVERTISE_100FULL;
1538 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1540 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1541 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1543 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1544 if (tp->mii.supports_gmii) {
1545 if (adv & ADVERTISED_1000baseT_Half)
1546 giga_ctrl |= ADVERTISE_1000HALF;
1547 if (adv & ADVERTISED_1000baseT_Full)
1548 giga_ctrl |= ADVERTISE_1000FULL;
1549 } else if (adv & (ADVERTISED_1000baseT_Half |
1550 ADVERTISED_1000baseT_Full)) {
1551 netif_info(tp, link, dev,
1552 "PHY does not support 1000Mbps\n");
1556 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1558 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1559 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1563 if (speed == SPEED_10)
1565 else if (speed == SPEED_100)
1566 bmcr = BMCR_SPEED100;
1570 if (duplex == DUPLEX_FULL)
1571 bmcr |= BMCR_FULLDPLX;
1574 rtl_writephy(tp, MII_BMCR, bmcr);
1576 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1577 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1578 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1579 rtl_writephy(tp, 0x17, 0x2138);
1580 rtl_writephy(tp, 0x0e, 0x0260);
1582 rtl_writephy(tp, 0x17, 0x2108);
1583 rtl_writephy(tp, 0x0e, 0x0000);
1592 static int rtl8169_set_speed(struct net_device *dev,
1593 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1595 struct rtl8169_private *tp = netdev_priv(dev);
1598 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1602 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1603 (advertising & ADVERTISED_1000baseT_Full)) {
1604 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1610 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1612 struct rtl8169_private *tp = netdev_priv(dev);
1615 del_timer_sync(&tp->timer);
1618 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1619 cmd->duplex, cmd->advertising);
1620 rtl_unlock_work(tp);
1625 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1626 netdev_features_t features)
1628 struct rtl8169_private *tp = netdev_priv(dev);
1630 if (dev->mtu > TD_MSS_MAX)
1631 features &= ~NETIF_F_ALL_TSO;
1633 if (dev->mtu > JUMBO_1K &&
1634 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1635 features &= ~NETIF_F_IP_CSUM;
1640 static void __rtl8169_set_features(struct net_device *dev,
1641 netdev_features_t features)
1643 struct rtl8169_private *tp = netdev_priv(dev);
1644 netdev_features_t changed = features ^ dev->features;
1645 void __iomem *ioaddr = tp->mmio_addr;
1647 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1650 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1651 if (features & NETIF_F_RXCSUM)
1652 tp->cp_cmd |= RxChkSum;
1654 tp->cp_cmd &= ~RxChkSum;
1656 if (dev->features & NETIF_F_HW_VLAN_RX)
1657 tp->cp_cmd |= RxVlan;
1659 tp->cp_cmd &= ~RxVlan;
1661 RTL_W16(CPlusCmd, tp->cp_cmd);
1664 if (changed & NETIF_F_RXALL) {
1665 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1666 if (features & NETIF_F_RXALL)
1667 tmp |= (AcceptErr | AcceptRunt);
1668 RTL_W32(RxConfig, tmp);
1672 static int rtl8169_set_features(struct net_device *dev,
1673 netdev_features_t features)
1675 struct rtl8169_private *tp = netdev_priv(dev);
1678 __rtl8169_set_features(dev, features);
1679 rtl_unlock_work(tp);
1685 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1686 struct sk_buff *skb)
1688 return (vlan_tx_tag_present(skb)) ?
1689 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1692 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1694 u32 opts2 = le32_to_cpu(desc->opts2);
1696 if (opts2 & RxVlanTag)
1697 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1702 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1704 struct rtl8169_private *tp = netdev_priv(dev);
1705 void __iomem *ioaddr = tp->mmio_addr;
1709 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1710 cmd->port = PORT_FIBRE;
1711 cmd->transceiver = XCVR_INTERNAL;
1713 status = RTL_R32(TBICSR);
1714 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1715 cmd->autoneg = !!(status & TBINwEnable);
1717 ethtool_cmd_speed_set(cmd, SPEED_1000);
1718 cmd->duplex = DUPLEX_FULL; /* Always set */
1723 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1725 struct rtl8169_private *tp = netdev_priv(dev);
1727 return mii_ethtool_gset(&tp->mii, cmd);
1730 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1732 struct rtl8169_private *tp = netdev_priv(dev);
1736 rc = tp->get_settings(dev, cmd);
1737 rtl_unlock_work(tp);
1742 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1745 struct rtl8169_private *tp = netdev_priv(dev);
1747 if (regs->len > R8169_REGS_SIZE)
1748 regs->len = R8169_REGS_SIZE;
1751 memcpy_fromio(p, tp->mmio_addr, regs->len);
1752 rtl_unlock_work(tp);
1755 static u32 rtl8169_get_msglevel(struct net_device *dev)
1757 struct rtl8169_private *tp = netdev_priv(dev);
1759 return tp->msg_enable;
1762 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1764 struct rtl8169_private *tp = netdev_priv(dev);
1766 tp->msg_enable = value;
1769 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1776 "tx_single_collisions",
1777 "tx_multi_collisions",
1785 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1789 return ARRAY_SIZE(rtl8169_gstrings);
1795 static void rtl8169_update_counters(struct net_device *dev)
1797 struct rtl8169_private *tp = netdev_priv(dev);
1798 void __iomem *ioaddr = tp->mmio_addr;
1799 struct device *d = &tp->pci_dev->dev;
1800 struct rtl8169_counters *counters;
1806 * Some chips are unable to dump tally counters when the receiver
1809 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1812 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1816 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1817 cmd = (u64)paddr & DMA_BIT_MASK(32);
1818 RTL_W32(CounterAddrLow, cmd);
1819 RTL_W32(CounterAddrLow, cmd | CounterDump);
1822 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1823 memcpy(&tp->counters, counters, sizeof(*counters));
1829 RTL_W32(CounterAddrLow, 0);
1830 RTL_W32(CounterAddrHigh, 0);
1832 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1835 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1836 struct ethtool_stats *stats, u64 *data)
1838 struct rtl8169_private *tp = netdev_priv(dev);
1842 rtl8169_update_counters(dev);
1844 data[0] = le64_to_cpu(tp->counters.tx_packets);
1845 data[1] = le64_to_cpu(tp->counters.rx_packets);
1846 data[2] = le64_to_cpu(tp->counters.tx_errors);
1847 data[3] = le32_to_cpu(tp->counters.rx_errors);
1848 data[4] = le16_to_cpu(tp->counters.rx_missed);
1849 data[5] = le16_to_cpu(tp->counters.align_errors);
1850 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1851 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1852 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1853 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1854 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1855 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1856 data[12] = le16_to_cpu(tp->counters.tx_underun);
1859 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1863 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1868 static const struct ethtool_ops rtl8169_ethtool_ops = {
1869 .get_drvinfo = rtl8169_get_drvinfo,
1870 .get_regs_len = rtl8169_get_regs_len,
1871 .get_link = ethtool_op_get_link,
1872 .get_settings = rtl8169_get_settings,
1873 .set_settings = rtl8169_set_settings,
1874 .get_msglevel = rtl8169_get_msglevel,
1875 .set_msglevel = rtl8169_set_msglevel,
1876 .get_regs = rtl8169_get_regs,
1877 .get_wol = rtl8169_get_wol,
1878 .set_wol = rtl8169_set_wol,
1879 .get_strings = rtl8169_get_strings,
1880 .get_sset_count = rtl8169_get_sset_count,
1881 .get_ethtool_stats = rtl8169_get_ethtool_stats,
1882 .get_ts_info = ethtool_op_get_ts_info,
1885 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1886 struct net_device *dev, u8 default_version)
1888 void __iomem *ioaddr = tp->mmio_addr;
1890 * The driver currently handles the 8168Bf and the 8168Be identically
1891 * but they can be identified more specifically through the test below
1894 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1896 * Same thing for the 8101Eb and the 8101Ec:
1898 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1900 static const struct rtl_mac_info {
1906 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
1907 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1908 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1911 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
1912 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1913 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1914 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1917 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1918 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
1919 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
1921 /* 8168DP family. */
1922 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1923 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1924 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
1927 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
1928 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
1929 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1930 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
1931 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1932 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1933 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
1934 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
1935 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
1938 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1939 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1940 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1941 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1944 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
1945 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
1946 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
1947 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
1948 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1949 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1950 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
1951 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1952 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1953 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1954 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1955 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1956 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
1957 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1958 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
1959 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1960 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1961 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
1962 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1963 /* FIXME: where did these entries come from ? -- FR */
1964 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1965 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1968 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1969 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1970 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1971 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1972 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1973 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1976 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
1978 const struct rtl_mac_info *p = mac_info;
1981 reg = RTL_R32(TxConfig);
1982 while ((reg & p->mask) != p->val)
1984 tp->mac_version = p->mac_version;
1986 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1987 netif_notice(tp, probe, dev,
1988 "unknown MAC, using family default\n");
1989 tp->mac_version = default_version;
1993 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1995 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2003 static void rtl_writephy_batch(struct rtl8169_private *tp,
2004 const struct phy_reg *regs, int len)
2007 rtl_writephy(tp, regs->reg, regs->val);
2012 #define PHY_READ 0x00000000
2013 #define PHY_DATA_OR 0x10000000
2014 #define PHY_DATA_AND 0x20000000
2015 #define PHY_BJMPN 0x30000000
2016 #define PHY_READ_EFUSE 0x40000000
2017 #define PHY_READ_MAC_BYTE 0x50000000
2018 #define PHY_WRITE_MAC_BYTE 0x60000000
2019 #define PHY_CLEAR_READCOUNT 0x70000000
2020 #define PHY_WRITE 0x80000000
2021 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2022 #define PHY_COMP_EQ_SKIPN 0xa0000000
2023 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2024 #define PHY_WRITE_PREVIOUS 0xc0000000
2025 #define PHY_SKIPN 0xd0000000
2026 #define PHY_DELAY_MS 0xe0000000
2027 #define PHY_WRITE_ERI_WORD 0xf0000000
2031 char version[RTL_VER_SIZE];
2037 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2039 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2041 const struct firmware *fw = rtl_fw->fw;
2042 struct fw_info *fw_info = (struct fw_info *)fw->data;
2043 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2044 char *version = rtl_fw->version;
2047 if (fw->size < FW_OPCODE_SIZE)
2050 if (!fw_info->magic) {
2051 size_t i, size, start;
2054 if (fw->size < sizeof(*fw_info))
2057 for (i = 0; i < fw->size; i++)
2058 checksum += fw->data[i];
2062 start = le32_to_cpu(fw_info->fw_start);
2063 if (start > fw->size)
2066 size = le32_to_cpu(fw_info->fw_len);
2067 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2070 memcpy(version, fw_info->version, RTL_VER_SIZE);
2072 pa->code = (__le32 *)(fw->data + start);
2075 if (fw->size % FW_OPCODE_SIZE)
2078 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2080 pa->code = (__le32 *)fw->data;
2081 pa->size = fw->size / FW_OPCODE_SIZE;
2083 version[RTL_VER_SIZE - 1] = 0;
2090 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2091 struct rtl_fw_phy_action *pa)
2096 for (index = 0; index < pa->size; index++) {
2097 u32 action = le32_to_cpu(pa->code[index]);
2098 u32 regno = (action & 0x0fff0000) >> 16;
2100 switch(action & 0xf0000000) {
2104 case PHY_READ_EFUSE:
2105 case PHY_CLEAR_READCOUNT:
2107 case PHY_WRITE_PREVIOUS:
2112 if (regno > index) {
2113 netif_err(tp, ifup, tp->dev,
2114 "Out of range of firmware\n");
2118 case PHY_READCOUNT_EQ_SKIP:
2119 if (index + 2 >= pa->size) {
2120 netif_err(tp, ifup, tp->dev,
2121 "Out of range of firmware\n");
2125 case PHY_COMP_EQ_SKIPN:
2126 case PHY_COMP_NEQ_SKIPN:
2128 if (index + 1 + regno >= pa->size) {
2129 netif_err(tp, ifup, tp->dev,
2130 "Out of range of firmware\n");
2135 case PHY_READ_MAC_BYTE:
2136 case PHY_WRITE_MAC_BYTE:
2137 case PHY_WRITE_ERI_WORD:
2139 netif_err(tp, ifup, tp->dev,
2140 "Invalid action 0x%08x\n", action);
2149 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2151 struct net_device *dev = tp->dev;
2154 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2155 netif_err(tp, ifup, dev, "invalid firwmare\n");
2159 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2165 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2167 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2171 predata = count = 0;
2173 for (index = 0; index < pa->size; ) {
2174 u32 action = le32_to_cpu(pa->code[index]);
2175 u32 data = action & 0x0000ffff;
2176 u32 regno = (action & 0x0fff0000) >> 16;
2181 switch(action & 0xf0000000) {
2183 predata = rtl_readphy(tp, regno);
2198 case PHY_READ_EFUSE:
2199 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2202 case PHY_CLEAR_READCOUNT:
2207 rtl_writephy(tp, regno, data);
2210 case PHY_READCOUNT_EQ_SKIP:
2211 index += (count == data) ? 2 : 1;
2213 case PHY_COMP_EQ_SKIPN:
2214 if (predata == data)
2218 case PHY_COMP_NEQ_SKIPN:
2219 if (predata != data)
2223 case PHY_WRITE_PREVIOUS:
2224 rtl_writephy(tp, regno, predata);
2235 case PHY_READ_MAC_BYTE:
2236 case PHY_WRITE_MAC_BYTE:
2237 case PHY_WRITE_ERI_WORD:
2244 static void rtl_release_firmware(struct rtl8169_private *tp)
2246 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2247 release_firmware(tp->rtl_fw->fw);
2250 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2253 static void rtl_apply_firmware(struct rtl8169_private *tp)
2255 struct rtl_fw *rtl_fw = tp->rtl_fw;
2257 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2258 if (!IS_ERR_OR_NULL(rtl_fw))
2259 rtl_phy_write_fw(tp, rtl_fw);
2262 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2264 if (rtl_readphy(tp, reg) != val)
2265 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2267 rtl_apply_firmware(tp);
2270 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2272 static const struct phy_reg phy_reg_init[] = {
2334 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2337 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2339 static const struct phy_reg phy_reg_init[] = {
2345 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2348 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2350 struct pci_dev *pdev = tp->pci_dev;
2352 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2353 (pdev->subsystem_device != 0xe000))
2356 rtl_writephy(tp, 0x1f, 0x0001);
2357 rtl_writephy(tp, 0x10, 0xf01b);
2358 rtl_writephy(tp, 0x1f, 0x0000);
2361 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2363 static const struct phy_reg phy_reg_init[] = {
2403 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2405 rtl8169scd_hw_phy_config_quirk(tp);
2408 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2410 static const struct phy_reg phy_reg_init[] = {
2458 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2461 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2463 static const struct phy_reg phy_reg_init[] = {
2468 rtl_writephy(tp, 0x1f, 0x0001);
2469 rtl_patchphy(tp, 0x16, 1 << 0);
2471 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2474 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2476 static const struct phy_reg phy_reg_init[] = {
2482 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2485 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2487 static const struct phy_reg phy_reg_init[] = {
2495 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2498 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2500 static const struct phy_reg phy_reg_init[] = {
2506 rtl_writephy(tp, 0x1f, 0x0000);
2507 rtl_patchphy(tp, 0x14, 1 << 5);
2508 rtl_patchphy(tp, 0x0d, 1 << 5);
2510 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2513 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2515 static const struct phy_reg phy_reg_init[] = {
2535 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2537 rtl_patchphy(tp, 0x14, 1 << 5);
2538 rtl_patchphy(tp, 0x0d, 1 << 5);
2539 rtl_writephy(tp, 0x1f, 0x0000);
2542 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2544 static const struct phy_reg phy_reg_init[] = {
2562 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2564 rtl_patchphy(tp, 0x16, 1 << 0);
2565 rtl_patchphy(tp, 0x14, 1 << 5);
2566 rtl_patchphy(tp, 0x0d, 1 << 5);
2567 rtl_writephy(tp, 0x1f, 0x0000);
2570 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2572 static const struct phy_reg phy_reg_init[] = {
2584 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2586 rtl_patchphy(tp, 0x16, 1 << 0);
2587 rtl_patchphy(tp, 0x14, 1 << 5);
2588 rtl_patchphy(tp, 0x0d, 1 << 5);
2589 rtl_writephy(tp, 0x1f, 0x0000);
2592 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2594 rtl8168c_3_hw_phy_config(tp);
2597 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2599 static const struct phy_reg phy_reg_init_0[] = {
2600 /* Channel Estimation */
2621 * Enhance line driver power
2630 * Can not link to 1Gbps with bad cable
2631 * Decrease SNR threshold form 21.07dB to 19.04dB
2639 void __iomem *ioaddr = tp->mmio_addr;
2641 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2645 * Fine Tune Switching regulator parameter
2647 rtl_writephy(tp, 0x1f, 0x0002);
2648 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2649 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2651 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2652 static const struct phy_reg phy_reg_init[] = {
2662 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2664 val = rtl_readphy(tp, 0x0d);
2666 if ((val & 0x00ff) != 0x006c) {
2667 static const u32 set[] = {
2668 0x0065, 0x0066, 0x0067, 0x0068,
2669 0x0069, 0x006a, 0x006b, 0x006c
2673 rtl_writephy(tp, 0x1f, 0x0002);
2676 for (i = 0; i < ARRAY_SIZE(set); i++)
2677 rtl_writephy(tp, 0x0d, val | set[i]);
2680 static const struct phy_reg phy_reg_init[] = {
2688 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2691 /* RSET couple improve */
2692 rtl_writephy(tp, 0x1f, 0x0002);
2693 rtl_patchphy(tp, 0x0d, 0x0300);
2694 rtl_patchphy(tp, 0x0f, 0x0010);
2696 /* Fine tune PLL performance */
2697 rtl_writephy(tp, 0x1f, 0x0002);
2698 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2699 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2701 rtl_writephy(tp, 0x1f, 0x0005);
2702 rtl_writephy(tp, 0x05, 0x001b);
2704 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2706 rtl_writephy(tp, 0x1f, 0x0000);
2709 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2711 static const struct phy_reg phy_reg_init_0[] = {
2712 /* Channel Estimation */
2733 * Enhance line driver power
2742 * Can not link to 1Gbps with bad cable
2743 * Decrease SNR threshold form 21.07dB to 19.04dB
2751 void __iomem *ioaddr = tp->mmio_addr;
2753 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2755 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2756 static const struct phy_reg phy_reg_init[] = {
2767 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2769 val = rtl_readphy(tp, 0x0d);
2770 if ((val & 0x00ff) != 0x006c) {
2771 static const u32 set[] = {
2772 0x0065, 0x0066, 0x0067, 0x0068,
2773 0x0069, 0x006a, 0x006b, 0x006c
2777 rtl_writephy(tp, 0x1f, 0x0002);
2780 for (i = 0; i < ARRAY_SIZE(set); i++)
2781 rtl_writephy(tp, 0x0d, val | set[i]);
2784 static const struct phy_reg phy_reg_init[] = {
2792 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2795 /* Fine tune PLL performance */
2796 rtl_writephy(tp, 0x1f, 0x0002);
2797 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2798 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2800 /* Switching regulator Slew rate */
2801 rtl_writephy(tp, 0x1f, 0x0002);
2802 rtl_patchphy(tp, 0x0f, 0x0017);
2804 rtl_writephy(tp, 0x1f, 0x0005);
2805 rtl_writephy(tp, 0x05, 0x001b);
2807 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2809 rtl_writephy(tp, 0x1f, 0x0000);
2812 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2814 static const struct phy_reg phy_reg_init[] = {
2870 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2873 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2875 static const struct phy_reg phy_reg_init[] = {
2885 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2886 rtl_patchphy(tp, 0x0d, 1 << 5);
2889 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
2891 static const struct phy_reg phy_reg_init[] = {
2892 /* Enable Delay cap */
2898 /* Channel estimation fine tune */
2907 /* Update PFM & 10M TX idle timer */
2919 rtl_apply_firmware(tp);
2921 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2923 /* DCO enable for 10M IDLE Power */
2924 rtl_writephy(tp, 0x1f, 0x0007);
2925 rtl_writephy(tp, 0x1e, 0x0023);
2926 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2927 rtl_writephy(tp, 0x1f, 0x0000);
2929 /* For impedance matching */
2930 rtl_writephy(tp, 0x1f, 0x0002);
2931 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2932 rtl_writephy(tp, 0x1f, 0x0000);
2934 /* PHY auto speed down */
2935 rtl_writephy(tp, 0x1f, 0x0007);
2936 rtl_writephy(tp, 0x1e, 0x002d);
2937 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2938 rtl_writephy(tp, 0x1f, 0x0000);
2939 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2941 rtl_writephy(tp, 0x1f, 0x0005);
2942 rtl_writephy(tp, 0x05, 0x8b86);
2943 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2946 rtl_writephy(tp, 0x1f, 0x0005);
2947 rtl_writephy(tp, 0x05, 0x8b85);
2948 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2949 rtl_writephy(tp, 0x1f, 0x0007);
2950 rtl_writephy(tp, 0x1e, 0x0020);
2951 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2952 rtl_writephy(tp, 0x1f, 0x0006);
2953 rtl_writephy(tp, 0x00, 0x5a00);
2954 rtl_writephy(tp, 0x1f, 0x0000);
2955 rtl_writephy(tp, 0x0d, 0x0007);
2956 rtl_writephy(tp, 0x0e, 0x003c);
2957 rtl_writephy(tp, 0x0d, 0x4007);
2958 rtl_writephy(tp, 0x0e, 0x0000);
2959 rtl_writephy(tp, 0x0d, 0x0000);
2962 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2964 static const struct phy_reg phy_reg_init[] = {
2965 /* Enable Delay cap */
2974 /* Channel estimation fine tune */
2991 rtl_apply_firmware(tp);
2993 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2995 /* For 4-corner performance improve */
2996 rtl_writephy(tp, 0x1f, 0x0005);
2997 rtl_writephy(tp, 0x05, 0x8b80);
2998 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2999 rtl_writephy(tp, 0x1f, 0x0000);
3001 /* PHY auto speed down */
3002 rtl_writephy(tp, 0x1f, 0x0004);
3003 rtl_writephy(tp, 0x1f, 0x0007);
3004 rtl_writephy(tp, 0x1e, 0x002d);
3005 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3006 rtl_writephy(tp, 0x1f, 0x0002);
3007 rtl_writephy(tp, 0x1f, 0x0000);
3008 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3010 /* improve 10M EEE waveform */
3011 rtl_writephy(tp, 0x1f, 0x0005);
3012 rtl_writephy(tp, 0x05, 0x8b86);
3013 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3014 rtl_writephy(tp, 0x1f, 0x0000);
3016 /* Improve 2-pair detection performance */
3017 rtl_writephy(tp, 0x1f, 0x0005);
3018 rtl_writephy(tp, 0x05, 0x8b85);
3019 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3023 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3025 rtl_writephy(tp, 0x1f, 0x0005);
3026 rtl_writephy(tp, 0x05, 0x8b85);
3027 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3028 rtl_writephy(tp, 0x1f, 0x0004);
3029 rtl_writephy(tp, 0x1f, 0x0007);
3030 rtl_writephy(tp, 0x1e, 0x0020);
3031 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3032 rtl_writephy(tp, 0x1f, 0x0002);
3033 rtl_writephy(tp, 0x1f, 0x0000);
3034 rtl_writephy(tp, 0x0d, 0x0007);
3035 rtl_writephy(tp, 0x0e, 0x003c);
3036 rtl_writephy(tp, 0x0d, 0x4007);
3037 rtl_writephy(tp, 0x0e, 0x0000);
3038 rtl_writephy(tp, 0x0d, 0x0000);
3041 rtl_writephy(tp, 0x1f, 0x0003);
3042 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3043 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3044 rtl_writephy(tp, 0x1f, 0x0000);
3047 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3049 /* For 4-corner performance improve */
3050 rtl_writephy(tp, 0x1f, 0x0005);
3051 rtl_writephy(tp, 0x05, 0x8b80);
3052 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3053 rtl_writephy(tp, 0x1f, 0x0000);
3055 /* PHY auto speed down */
3056 rtl_writephy(tp, 0x1f, 0x0007);
3057 rtl_writephy(tp, 0x1e, 0x002d);
3058 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3059 rtl_writephy(tp, 0x1f, 0x0000);
3060 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3062 /* Improve 10M EEE waveform */
3063 rtl_writephy(tp, 0x1f, 0x0005);
3064 rtl_writephy(tp, 0x05, 0x8b86);
3065 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3066 rtl_writephy(tp, 0x1f, 0x0000);
3069 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3071 static const struct phy_reg phy_reg_init[] = {
3072 /* Channel estimation fine tune */
3077 /* Modify green table for giga & fnet */
3094 /* Modify green table for 10M */
3100 /* Disable hiimpedance detection (RTCT) */
3106 rtl_apply_firmware(tp);
3108 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3110 rtl8168f_hw_phy_config(tp);
3112 /* Improve 2-pair detection performance */
3113 rtl_writephy(tp, 0x1f, 0x0005);
3114 rtl_writephy(tp, 0x05, 0x8b85);
3115 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3116 rtl_writephy(tp, 0x1f, 0x0000);
3119 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3121 rtl_apply_firmware(tp);
3123 rtl8168f_hw_phy_config(tp);
3126 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3128 void __iomem *ioaddr = tp->mmio_addr;
3129 static const struct phy_reg phy_reg_init[] = {
3130 /* Channel estimation fine tune */
3135 /* Modify green table for giga & fnet */
3152 /* Modify green table for 10M */
3158 /* Disable hiimpedance detection (RTCT) */
3165 rtl_apply_firmware(tp);
3167 rtl8168f_hw_phy_config(tp);
3169 /* Improve 2-pair detection performance */
3170 rtl_writephy(tp, 0x1f, 0x0005);
3171 rtl_writephy(tp, 0x05, 0x8b85);
3172 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3173 rtl_writephy(tp, 0x1f, 0x0000);
3175 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3177 /* Modify green table for giga */
3178 rtl_writephy(tp, 0x1f, 0x0005);
3179 rtl_writephy(tp, 0x05, 0x8b54);
3180 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3181 rtl_writephy(tp, 0x05, 0x8b5d);
3182 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3183 rtl_writephy(tp, 0x05, 0x8a7c);
3184 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3185 rtl_writephy(tp, 0x05, 0x8a7f);
3186 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3187 rtl_writephy(tp, 0x05, 0x8a82);
3188 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3189 rtl_writephy(tp, 0x05, 0x8a85);
3190 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3191 rtl_writephy(tp, 0x05, 0x8a88);
3192 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3193 rtl_writephy(tp, 0x1f, 0x0000);
3195 /* uc same-seed solution */
3196 rtl_writephy(tp, 0x1f, 0x0005);
3197 rtl_writephy(tp, 0x05, 0x8b85);
3198 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3199 rtl_writephy(tp, 0x1f, 0x0000);
3202 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3203 rtl_writephy(tp, 0x1f, 0x0005);
3204 rtl_writephy(tp, 0x05, 0x8b85);
3205 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3206 rtl_writephy(tp, 0x1f, 0x0004);
3207 rtl_writephy(tp, 0x1f, 0x0007);
3208 rtl_writephy(tp, 0x1e, 0x0020);
3209 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3210 rtl_writephy(tp, 0x1f, 0x0000);
3211 rtl_writephy(tp, 0x0d, 0x0007);
3212 rtl_writephy(tp, 0x0e, 0x003c);
3213 rtl_writephy(tp, 0x0d, 0x4007);
3214 rtl_writephy(tp, 0x0e, 0x0000);
3215 rtl_writephy(tp, 0x0d, 0x0000);
3218 rtl_writephy(tp, 0x1f, 0x0003);
3219 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3220 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3221 rtl_writephy(tp, 0x1f, 0x0000);
3224 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3226 static const struct phy_reg phy_reg_init[] = {
3233 rtl_writephy(tp, 0x1f, 0x0000);
3234 rtl_patchphy(tp, 0x11, 1 << 12);
3235 rtl_patchphy(tp, 0x19, 1 << 13);
3236 rtl_patchphy(tp, 0x10, 1 << 15);
3238 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3241 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3243 static const struct phy_reg phy_reg_init[] = {
3257 /* Disable ALDPS before ram code */
3258 rtl_writephy(tp, 0x1f, 0x0000);
3259 rtl_writephy(tp, 0x18, 0x0310);
3262 rtl_apply_firmware(tp);
3264 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3267 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3269 void __iomem *ioaddr = tp->mmio_addr;
3271 /* Disable ALDPS before setting firmware */
3272 rtl_writephy(tp, 0x1f, 0x0000);
3273 rtl_writephy(tp, 0x18, 0x0310);
3276 rtl_apply_firmware(tp);
3279 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3280 rtl_writephy(tp, 0x1f, 0x0004);
3281 rtl_writephy(tp, 0x10, 0x401f);
3282 rtl_writephy(tp, 0x19, 0x7030);
3283 rtl_writephy(tp, 0x1f, 0x0000);
3286 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3288 void __iomem *ioaddr = tp->mmio_addr;
3290 static const struct phy_reg phy_reg_init[] = {
3297 /* Disable ALDPS before ram code */
3298 rtl_writephy(tp, 0x1f, 0x0000);
3299 rtl_writephy(tp, 0x18, 0x0310);
3302 rtl_apply_firmware(tp);
3304 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3305 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3307 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3310 static void rtl_hw_phy_config(struct net_device *dev)
3312 struct rtl8169_private *tp = netdev_priv(dev);
3314 rtl8169_print_mac_version(tp);
3316 switch (tp->mac_version) {
3317 case RTL_GIGA_MAC_VER_01:
3319 case RTL_GIGA_MAC_VER_02:
3320 case RTL_GIGA_MAC_VER_03:
3321 rtl8169s_hw_phy_config(tp);
3323 case RTL_GIGA_MAC_VER_04:
3324 rtl8169sb_hw_phy_config(tp);
3326 case RTL_GIGA_MAC_VER_05:
3327 rtl8169scd_hw_phy_config(tp);
3329 case RTL_GIGA_MAC_VER_06:
3330 rtl8169sce_hw_phy_config(tp);
3332 case RTL_GIGA_MAC_VER_07:
3333 case RTL_GIGA_MAC_VER_08:
3334 case RTL_GIGA_MAC_VER_09:
3335 rtl8102e_hw_phy_config(tp);
3337 case RTL_GIGA_MAC_VER_11:
3338 rtl8168bb_hw_phy_config(tp);
3340 case RTL_GIGA_MAC_VER_12:
3341 rtl8168bef_hw_phy_config(tp);
3343 case RTL_GIGA_MAC_VER_17:
3344 rtl8168bef_hw_phy_config(tp);
3346 case RTL_GIGA_MAC_VER_18:
3347 rtl8168cp_1_hw_phy_config(tp);
3349 case RTL_GIGA_MAC_VER_19:
3350 rtl8168c_1_hw_phy_config(tp);
3352 case RTL_GIGA_MAC_VER_20:
3353 rtl8168c_2_hw_phy_config(tp);
3355 case RTL_GIGA_MAC_VER_21:
3356 rtl8168c_3_hw_phy_config(tp);
3358 case RTL_GIGA_MAC_VER_22:
3359 rtl8168c_4_hw_phy_config(tp);
3361 case RTL_GIGA_MAC_VER_23:
3362 case RTL_GIGA_MAC_VER_24:
3363 rtl8168cp_2_hw_phy_config(tp);
3365 case RTL_GIGA_MAC_VER_25:
3366 rtl8168d_1_hw_phy_config(tp);
3368 case RTL_GIGA_MAC_VER_26:
3369 rtl8168d_2_hw_phy_config(tp);
3371 case RTL_GIGA_MAC_VER_27:
3372 rtl8168d_3_hw_phy_config(tp);
3374 case RTL_GIGA_MAC_VER_28:
3375 rtl8168d_4_hw_phy_config(tp);
3377 case RTL_GIGA_MAC_VER_29:
3378 case RTL_GIGA_MAC_VER_30:
3379 rtl8105e_hw_phy_config(tp);
3381 case RTL_GIGA_MAC_VER_31:
3384 case RTL_GIGA_MAC_VER_32:
3385 case RTL_GIGA_MAC_VER_33:
3386 rtl8168e_1_hw_phy_config(tp);
3388 case RTL_GIGA_MAC_VER_34:
3389 rtl8168e_2_hw_phy_config(tp);
3391 case RTL_GIGA_MAC_VER_35:
3392 rtl8168f_1_hw_phy_config(tp);
3394 case RTL_GIGA_MAC_VER_36:
3395 rtl8168f_2_hw_phy_config(tp);
3398 case RTL_GIGA_MAC_VER_37:
3399 rtl8402_hw_phy_config(tp);
3402 case RTL_GIGA_MAC_VER_38:
3403 rtl8411_hw_phy_config(tp);
3406 case RTL_GIGA_MAC_VER_39:
3407 rtl8106e_hw_phy_config(tp);
3415 static void rtl_phy_work(struct rtl8169_private *tp)
3417 struct timer_list *timer = &tp->timer;
3418 void __iomem *ioaddr = tp->mmio_addr;
3419 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3421 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
3423 if (tp->phy_reset_pending(tp)) {
3425 * A busy loop could burn quite a few cycles on nowadays CPU.
3426 * Let's delay the execution of the timer for a few ticks.
3432 if (tp->link_ok(ioaddr))
3435 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
3437 tp->phy_reset_enable(tp);
3440 mod_timer(timer, jiffies + timeout);
3443 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3445 if (!test_and_set_bit(flag, tp->wk.flags))
3446 schedule_work(&tp->wk.work);
3449 static void rtl8169_phy_timer(unsigned long __opaque)
3451 struct net_device *dev = (struct net_device *)__opaque;
3452 struct rtl8169_private *tp = netdev_priv(dev);
3454 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
3457 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3458 void __iomem *ioaddr)
3461 pci_release_regions(pdev);
3462 pci_clear_mwi(pdev);
3463 pci_disable_device(pdev);
3467 static void rtl8169_phy_reset(struct net_device *dev,
3468 struct rtl8169_private *tp)
3472 tp->phy_reset_enable(tp);
3473 for (i = 0; i < 100; i++) {
3474 if (!tp->phy_reset_pending(tp))
3478 netif_err(tp, link, dev, "PHY reset failed\n");
3481 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3483 void __iomem *ioaddr = tp->mmio_addr;
3485 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3486 (RTL_R8(PHYstatus) & TBI_Enable);
3489 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3491 void __iomem *ioaddr = tp->mmio_addr;
3493 rtl_hw_phy_config(dev);
3495 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3496 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3500 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3502 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3503 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3505 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
3506 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3508 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
3509 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
3512 rtl8169_phy_reset(dev, tp);
3514 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3515 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3516 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3517 (tp->mii.supports_gmii ?
3518 ADVERTISED_1000baseT_Half |
3519 ADVERTISED_1000baseT_Full : 0));
3521 if (rtl_tbi_enabled(tp))
3522 netif_info(tp, link, dev, "TBI auto-negotiating\n");
3525 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3527 void __iomem *ioaddr = tp->mmio_addr;
3531 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3532 high = addr[4] | (addr[5] << 8);
3536 RTL_W8(Cfg9346, Cfg9346_Unlock);
3538 RTL_W32(MAC4, high);
3544 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3545 const struct exgmac_reg e[] = {
3546 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3547 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3548 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3549 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3553 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3556 RTL_W8(Cfg9346, Cfg9346_Lock);
3558 rtl_unlock_work(tp);
3561 static int rtl_set_mac_address(struct net_device *dev, void *p)
3563 struct rtl8169_private *tp = netdev_priv(dev);
3564 struct sockaddr *addr = p;
3566 if (!is_valid_ether_addr(addr->sa_data))
3567 return -EADDRNOTAVAIL;
3569 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3571 rtl_rar_set(tp, dev->dev_addr);
3576 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3578 struct rtl8169_private *tp = netdev_priv(dev);
3579 struct mii_ioctl_data *data = if_mii(ifr);
3581 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3584 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3585 struct mii_ioctl_data *data, int cmd)
3589 data->phy_id = 32; /* Internal PHY */
3593 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3597 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3603 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3608 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3610 if (tp->features & RTL_FEATURE_MSI) {
3611 pci_disable_msi(pdev);
3612 tp->features &= ~RTL_FEATURE_MSI;
3616 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3618 struct mdio_ops *ops = &tp->mdio_ops;
3620 switch (tp->mac_version) {
3621 case RTL_GIGA_MAC_VER_27:
3622 ops->write = r8168dp_1_mdio_write;
3623 ops->read = r8168dp_1_mdio_read;
3625 case RTL_GIGA_MAC_VER_28:
3626 case RTL_GIGA_MAC_VER_31:
3627 ops->write = r8168dp_2_mdio_write;
3628 ops->read = r8168dp_2_mdio_read;
3631 ops->write = r8169_mdio_write;
3632 ops->read = r8169_mdio_read;
3637 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3639 void __iomem *ioaddr = tp->mmio_addr;
3641 switch (tp->mac_version) {
3642 case RTL_GIGA_MAC_VER_29:
3643 case RTL_GIGA_MAC_VER_30:
3644 case RTL_GIGA_MAC_VER_32:
3645 case RTL_GIGA_MAC_VER_33:
3646 case RTL_GIGA_MAC_VER_34:
3647 case RTL_GIGA_MAC_VER_37:
3648 case RTL_GIGA_MAC_VER_38:
3649 case RTL_GIGA_MAC_VER_39:
3650 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3651 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3658 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3660 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3663 rtl_writephy(tp, 0x1f, 0x0000);
3664 rtl_writephy(tp, MII_BMCR, 0x0000);
3666 rtl_wol_suspend_quirk(tp);
3671 static void r810x_phy_power_down(struct rtl8169_private *tp)
3673 rtl_writephy(tp, 0x1f, 0x0000);
3674 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3677 static void r810x_phy_power_up(struct rtl8169_private *tp)
3679 rtl_writephy(tp, 0x1f, 0x0000);
3680 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3683 static void r810x_pll_power_down(struct rtl8169_private *tp)
3685 void __iomem *ioaddr = tp->mmio_addr;
3687 if (rtl_wol_pll_power_down(tp))
3690 r810x_phy_power_down(tp);
3692 switch (tp->mac_version) {
3693 case RTL_GIGA_MAC_VER_07:
3694 case RTL_GIGA_MAC_VER_08:
3695 case RTL_GIGA_MAC_VER_09:
3696 case RTL_GIGA_MAC_VER_10:
3697 case RTL_GIGA_MAC_VER_13:
3698 case RTL_GIGA_MAC_VER_16:
3701 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3706 static void r810x_pll_power_up(struct rtl8169_private *tp)
3708 void __iomem *ioaddr = tp->mmio_addr;
3710 r810x_phy_power_up(tp);
3712 switch (tp->mac_version) {
3713 case RTL_GIGA_MAC_VER_07:
3714 case RTL_GIGA_MAC_VER_08:
3715 case RTL_GIGA_MAC_VER_09:
3716 case RTL_GIGA_MAC_VER_10:
3717 case RTL_GIGA_MAC_VER_13:
3718 case RTL_GIGA_MAC_VER_16:
3721 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3726 static void r8168_phy_power_up(struct rtl8169_private *tp)
3728 rtl_writephy(tp, 0x1f, 0x0000);
3729 switch (tp->mac_version) {
3730 case RTL_GIGA_MAC_VER_11:
3731 case RTL_GIGA_MAC_VER_12:
3732 case RTL_GIGA_MAC_VER_17:
3733 case RTL_GIGA_MAC_VER_18:
3734 case RTL_GIGA_MAC_VER_19:
3735 case RTL_GIGA_MAC_VER_20:
3736 case RTL_GIGA_MAC_VER_21:
3737 case RTL_GIGA_MAC_VER_22:
3738 case RTL_GIGA_MAC_VER_23:
3739 case RTL_GIGA_MAC_VER_24:
3740 case RTL_GIGA_MAC_VER_25:
3741 case RTL_GIGA_MAC_VER_26:
3742 case RTL_GIGA_MAC_VER_27:
3743 case RTL_GIGA_MAC_VER_28:
3744 case RTL_GIGA_MAC_VER_31:
3745 rtl_writephy(tp, 0x0e, 0x0000);
3750 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3753 static void r8168_phy_power_down(struct rtl8169_private *tp)
3755 rtl_writephy(tp, 0x1f, 0x0000);
3756 switch (tp->mac_version) {
3757 case RTL_GIGA_MAC_VER_32:
3758 case RTL_GIGA_MAC_VER_33:
3759 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3762 case RTL_GIGA_MAC_VER_11:
3763 case RTL_GIGA_MAC_VER_12:
3764 case RTL_GIGA_MAC_VER_17:
3765 case RTL_GIGA_MAC_VER_18:
3766 case RTL_GIGA_MAC_VER_19:
3767 case RTL_GIGA_MAC_VER_20:
3768 case RTL_GIGA_MAC_VER_21:
3769 case RTL_GIGA_MAC_VER_22:
3770 case RTL_GIGA_MAC_VER_23:
3771 case RTL_GIGA_MAC_VER_24:
3772 case RTL_GIGA_MAC_VER_25:
3773 case RTL_GIGA_MAC_VER_26:
3774 case RTL_GIGA_MAC_VER_27:
3775 case RTL_GIGA_MAC_VER_28:
3776 case RTL_GIGA_MAC_VER_31:
3777 rtl_writephy(tp, 0x0e, 0x0200);
3779 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3784 static void r8168_pll_power_down(struct rtl8169_private *tp)
3786 void __iomem *ioaddr = tp->mmio_addr;
3788 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3789 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3790 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3791 r8168dp_check_dash(tp)) {
3795 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3796 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
3797 (RTL_R16(CPlusCmd) & ASF)) {
3801 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3802 tp->mac_version == RTL_GIGA_MAC_VER_33)
3803 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3805 if (rtl_wol_pll_power_down(tp))
3808 r8168_phy_power_down(tp);
3810 switch (tp->mac_version) {
3811 case RTL_GIGA_MAC_VER_25:
3812 case RTL_GIGA_MAC_VER_26:
3813 case RTL_GIGA_MAC_VER_27:
3814 case RTL_GIGA_MAC_VER_28:
3815 case RTL_GIGA_MAC_VER_31:
3816 case RTL_GIGA_MAC_VER_32:
3817 case RTL_GIGA_MAC_VER_33:
3818 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3823 static void r8168_pll_power_up(struct rtl8169_private *tp)
3825 void __iomem *ioaddr = tp->mmio_addr;
3827 switch (tp->mac_version) {
3828 case RTL_GIGA_MAC_VER_25:
3829 case RTL_GIGA_MAC_VER_26:
3830 case RTL_GIGA_MAC_VER_27:
3831 case RTL_GIGA_MAC_VER_28:
3832 case RTL_GIGA_MAC_VER_31:
3833 case RTL_GIGA_MAC_VER_32:
3834 case RTL_GIGA_MAC_VER_33:
3835 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3839 r8168_phy_power_up(tp);
3842 static void rtl_generic_op(struct rtl8169_private *tp,
3843 void (*op)(struct rtl8169_private *))
3849 static void rtl_pll_power_down(struct rtl8169_private *tp)
3851 rtl_generic_op(tp, tp->pll_power_ops.down);
3854 static void rtl_pll_power_up(struct rtl8169_private *tp)
3856 rtl_generic_op(tp, tp->pll_power_ops.up);
3859 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3861 struct pll_power_ops *ops = &tp->pll_power_ops;
3863 switch (tp->mac_version) {
3864 case RTL_GIGA_MAC_VER_07:
3865 case RTL_GIGA_MAC_VER_08:
3866 case RTL_GIGA_MAC_VER_09:
3867 case RTL_GIGA_MAC_VER_10:
3868 case RTL_GIGA_MAC_VER_16:
3869 case RTL_GIGA_MAC_VER_29:
3870 case RTL_GIGA_MAC_VER_30:
3871 case RTL_GIGA_MAC_VER_37:
3872 case RTL_GIGA_MAC_VER_39:
3873 ops->down = r810x_pll_power_down;
3874 ops->up = r810x_pll_power_up;
3877 case RTL_GIGA_MAC_VER_11:
3878 case RTL_GIGA_MAC_VER_12:
3879 case RTL_GIGA_MAC_VER_17:
3880 case RTL_GIGA_MAC_VER_18:
3881 case RTL_GIGA_MAC_VER_19:
3882 case RTL_GIGA_MAC_VER_20:
3883 case RTL_GIGA_MAC_VER_21:
3884 case RTL_GIGA_MAC_VER_22:
3885 case RTL_GIGA_MAC_VER_23:
3886 case RTL_GIGA_MAC_VER_24:
3887 case RTL_GIGA_MAC_VER_25:
3888 case RTL_GIGA_MAC_VER_26:
3889 case RTL_GIGA_MAC_VER_27:
3890 case RTL_GIGA_MAC_VER_28:
3891 case RTL_GIGA_MAC_VER_31:
3892 case RTL_GIGA_MAC_VER_32:
3893 case RTL_GIGA_MAC_VER_33:
3894 case RTL_GIGA_MAC_VER_34:
3895 case RTL_GIGA_MAC_VER_35:
3896 case RTL_GIGA_MAC_VER_36:
3897 case RTL_GIGA_MAC_VER_38:
3898 ops->down = r8168_pll_power_down;
3899 ops->up = r8168_pll_power_up;
3909 static void rtl_init_rxcfg(struct rtl8169_private *tp)
3911 void __iomem *ioaddr = tp->mmio_addr;
3913 switch (tp->mac_version) {
3914 case RTL_GIGA_MAC_VER_01:
3915 case RTL_GIGA_MAC_VER_02:
3916 case RTL_GIGA_MAC_VER_03:
3917 case RTL_GIGA_MAC_VER_04:
3918 case RTL_GIGA_MAC_VER_05:
3919 case RTL_GIGA_MAC_VER_06:
3920 case RTL_GIGA_MAC_VER_10:
3921 case RTL_GIGA_MAC_VER_11:
3922 case RTL_GIGA_MAC_VER_12:
3923 case RTL_GIGA_MAC_VER_13:
3924 case RTL_GIGA_MAC_VER_14:
3925 case RTL_GIGA_MAC_VER_15:
3926 case RTL_GIGA_MAC_VER_16:
3927 case RTL_GIGA_MAC_VER_17:
3928 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3930 case RTL_GIGA_MAC_VER_18:
3931 case RTL_GIGA_MAC_VER_19:
3932 case RTL_GIGA_MAC_VER_20:
3933 case RTL_GIGA_MAC_VER_21:
3934 case RTL_GIGA_MAC_VER_22:
3935 case RTL_GIGA_MAC_VER_23:
3936 case RTL_GIGA_MAC_VER_24:
3937 case RTL_GIGA_MAC_VER_34:
3938 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3941 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3946 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3948 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3951 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3953 void __iomem *ioaddr = tp->mmio_addr;
3955 RTL_W8(Cfg9346, Cfg9346_Unlock);
3956 rtl_generic_op(tp, tp->jumbo_ops.enable);
3957 RTL_W8(Cfg9346, Cfg9346_Lock);
3960 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3962 void __iomem *ioaddr = tp->mmio_addr;
3964 RTL_W8(Cfg9346, Cfg9346_Unlock);
3965 rtl_generic_op(tp, tp->jumbo_ops.disable);
3966 RTL_W8(Cfg9346, Cfg9346_Lock);
3969 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3971 void __iomem *ioaddr = tp->mmio_addr;
3973 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3974 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3975 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3978 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3980 void __iomem *ioaddr = tp->mmio_addr;
3982 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3983 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3984 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3987 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3989 void __iomem *ioaddr = tp->mmio_addr;
3991 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3994 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3996 void __iomem *ioaddr = tp->mmio_addr;
3998 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4001 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4003 void __iomem *ioaddr = tp->mmio_addr;
4005 RTL_W8(MaxTxPacketSize, 0x3f);
4006 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4007 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
4008 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4011 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4013 void __iomem *ioaddr = tp->mmio_addr;
4015 RTL_W8(MaxTxPacketSize, 0x0c);
4016 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4017 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
4018 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4021 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4023 rtl_tx_performance_tweak(tp->pci_dev,
4024 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4027 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4029 rtl_tx_performance_tweak(tp->pci_dev,
4030 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4033 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4035 void __iomem *ioaddr = tp->mmio_addr;
4037 r8168b_0_hw_jumbo_enable(tp);
4039 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4042 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4044 void __iomem *ioaddr = tp->mmio_addr;
4046 r8168b_0_hw_jumbo_disable(tp);
4048 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4051 static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
4053 struct jumbo_ops *ops = &tp->jumbo_ops;
4055 switch (tp->mac_version) {
4056 case RTL_GIGA_MAC_VER_11:
4057 ops->disable = r8168b_0_hw_jumbo_disable;
4058 ops->enable = r8168b_0_hw_jumbo_enable;
4060 case RTL_GIGA_MAC_VER_12:
4061 case RTL_GIGA_MAC_VER_17:
4062 ops->disable = r8168b_1_hw_jumbo_disable;
4063 ops->enable = r8168b_1_hw_jumbo_enable;
4065 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4066 case RTL_GIGA_MAC_VER_19:
4067 case RTL_GIGA_MAC_VER_20:
4068 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4069 case RTL_GIGA_MAC_VER_22:
4070 case RTL_GIGA_MAC_VER_23:
4071 case RTL_GIGA_MAC_VER_24:
4072 case RTL_GIGA_MAC_VER_25:
4073 case RTL_GIGA_MAC_VER_26:
4074 ops->disable = r8168c_hw_jumbo_disable;
4075 ops->enable = r8168c_hw_jumbo_enable;
4077 case RTL_GIGA_MAC_VER_27:
4078 case RTL_GIGA_MAC_VER_28:
4079 ops->disable = r8168dp_hw_jumbo_disable;
4080 ops->enable = r8168dp_hw_jumbo_enable;
4082 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4083 case RTL_GIGA_MAC_VER_32:
4084 case RTL_GIGA_MAC_VER_33:
4085 case RTL_GIGA_MAC_VER_34:
4086 ops->disable = r8168e_hw_jumbo_disable;
4087 ops->enable = r8168e_hw_jumbo_enable;
4091 * No action needed for jumbo frames with 8169.
4092 * No jumbo for 810x at all.
4095 ops->disable = NULL;
4101 static void rtl_hw_reset(struct rtl8169_private *tp)
4103 void __iomem *ioaddr = tp->mmio_addr;
4106 /* Soft reset the chip. */
4107 RTL_W8(ChipCmd, CmdReset);
4109 /* Check that the chip has finished the reset. */
4110 for (i = 0; i < 100; i++) {
4111 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4117 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4119 struct rtl_fw *rtl_fw;
4123 name = rtl_lookup_firmware_name(tp);
4125 goto out_no_firmware;
4127 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4131 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4135 rc = rtl_check_firmware(tp, rtl_fw);
4137 goto err_release_firmware;
4139 tp->rtl_fw = rtl_fw;
4143 err_release_firmware:
4144 release_firmware(rtl_fw->fw);
4148 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4155 static void rtl_request_firmware(struct rtl8169_private *tp)
4157 if (IS_ERR(tp->rtl_fw))
4158 rtl_request_uncached_firmware(tp);
4161 static void rtl_rx_close(struct rtl8169_private *tp)
4163 void __iomem *ioaddr = tp->mmio_addr;
4165 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4168 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4170 void __iomem *ioaddr = tp->mmio_addr;
4172 /* Disable interrupts */
4173 rtl8169_irq_mask_and_ack(tp);
4177 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4178 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4179 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4180 while (RTL_R8(TxPoll) & NPQ)
4182 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4183 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4184 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
4185 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4186 tp->mac_version == RTL_GIGA_MAC_VER_38) {
4187 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4188 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4191 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4198 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4200 void __iomem *ioaddr = tp->mmio_addr;
4202 /* Set DMA burst size and Interframe Gap Time */
4203 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4204 (InterFrameGap << TxInterFrameGapShift));
4207 static void rtl_hw_start(struct net_device *dev)
4209 struct rtl8169_private *tp = netdev_priv(dev);
4213 rtl_irq_enable_all(tp);
4216 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4217 void __iomem *ioaddr)
4220 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4221 * register to be written before TxDescAddrLow to work.
4222 * Switching from MMIO to I/O access fixes the issue as well.
4224 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4225 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4226 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4227 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4230 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4234 cmd = RTL_R16(CPlusCmd);
4235 RTL_W16(CPlusCmd, cmd);
4239 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
4241 /* Low hurts. Let's disable the filtering. */
4242 RTL_W16(RxMaxSize, rx_buf_sz + 1);
4245 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4247 static const struct rtl_cfg2_info {
4252 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4253 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4254 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4255 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4257 const struct rtl_cfg2_info *p = cfg2_info;
4261 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
4262 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4263 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4264 RTL_W32(0x7c, p->val);
4270 static void rtl_set_rx_mode(struct net_device *dev)
4272 struct rtl8169_private *tp = netdev_priv(dev);
4273 void __iomem *ioaddr = tp->mmio_addr;
4274 u32 mc_filter[2]; /* Multicast hash filter */
4278 if (dev->flags & IFF_PROMISC) {
4279 /* Unconditionally log net taps. */
4280 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4282 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4284 mc_filter[1] = mc_filter[0] = 0xffffffff;
4285 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4286 (dev->flags & IFF_ALLMULTI)) {
4287 /* Too many to filter perfectly -- accept all multicasts. */
4288 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4289 mc_filter[1] = mc_filter[0] = 0xffffffff;
4291 struct netdev_hw_addr *ha;
4293 rx_mode = AcceptBroadcast | AcceptMyPhys;
4294 mc_filter[1] = mc_filter[0] = 0;
4295 netdev_for_each_mc_addr(ha, dev) {
4296 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4297 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4298 rx_mode |= AcceptMulticast;
4302 if (dev->features & NETIF_F_RXALL)
4303 rx_mode |= (AcceptErr | AcceptRunt);
4305 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4307 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4308 u32 data = mc_filter[0];
4310 mc_filter[0] = swab32(mc_filter[1]);
4311 mc_filter[1] = swab32(data);
4314 RTL_W32(MAR0 + 4, mc_filter[1]);
4315 RTL_W32(MAR0 + 0, mc_filter[0]);
4317 RTL_W32(RxConfig, tmp);
4320 static void rtl_hw_start_8169(struct net_device *dev)
4322 struct rtl8169_private *tp = netdev_priv(dev);
4323 void __iomem *ioaddr = tp->mmio_addr;
4324 struct pci_dev *pdev = tp->pci_dev;
4326 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4327 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4328 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4331 RTL_W8(Cfg9346, Cfg9346_Unlock);
4332 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4333 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4334 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4335 tp->mac_version == RTL_GIGA_MAC_VER_04)
4336 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4340 RTL_W8(EarlyTxThres, NoEarlyTx);
4342 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4344 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4345 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4346 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4347 tp->mac_version == RTL_GIGA_MAC_VER_04)
4348 rtl_set_rx_tx_config_registers(tp);
4350 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4352 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4353 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4354 dprintk("Set MAC Reg C+CR Offset 0xE0. "
4355 "Bit-3 and bit-14 MUST be 1\n");
4356 tp->cp_cmd |= (1 << 14);
4359 RTL_W16(CPlusCmd, tp->cp_cmd);
4361 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4364 * Undocumented corner. Supposedly:
4365 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4367 RTL_W16(IntrMitigate, 0x0000);
4369 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4371 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4372 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4373 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4374 tp->mac_version != RTL_GIGA_MAC_VER_04) {
4375 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4376 rtl_set_rx_tx_config_registers(tp);
4379 RTL_W8(Cfg9346, Cfg9346_Lock);
4381 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4384 RTL_W32(RxMissed, 0);
4386 rtl_set_rx_mode(dev);
4388 /* no early-rx interrupts */
4389 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4392 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4394 if (tp->csi_ops.write)
4395 tp->csi_ops.write(tp->mmio_addr, addr, value);
4398 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4400 if (tp->csi_ops.read)
4401 return tp->csi_ops.read(tp->mmio_addr, addr);
4406 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
4410 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4411 rtl_csi_write(tp, 0x070c, csi | bits);
4414 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
4416 rtl_csi_access_enable(tp, 0x17000000);
4419 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
4421 rtl_csi_access_enable(tp, 0x27000000);
4424 static void r8169_csi_write(void __iomem *ioaddr, int addr, int value)
4428 RTL_W32(CSIDR, value);
4429 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4430 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4432 for (i = 0; i < 100; i++) {
4433 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4439 static u32 r8169_csi_read(void __iomem *ioaddr, int addr)
4444 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4445 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4447 for (i = 0; i < 100; i++) {
4448 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4449 value = RTL_R32(CSIDR);
4458 static void r8402_csi_write(void __iomem *ioaddr, int addr, int value)
4462 RTL_W32(CSIDR, value);
4463 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4464 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4467 for (i = 0; i < 100; i++) {
4468 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4474 static u32 r8402_csi_read(void __iomem *ioaddr, int addr)
4479 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4480 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4482 for (i = 0; i < 100; i++) {
4483 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4484 value = RTL_R32(CSIDR);
4493 static void __devinit rtl_init_csi_ops(struct rtl8169_private *tp)
4495 struct csi_ops *ops = &tp->csi_ops;
4497 switch (tp->mac_version) {
4498 case RTL_GIGA_MAC_VER_01:
4499 case RTL_GIGA_MAC_VER_02:
4500 case RTL_GIGA_MAC_VER_03:
4501 case RTL_GIGA_MAC_VER_04:
4502 case RTL_GIGA_MAC_VER_05:
4503 case RTL_GIGA_MAC_VER_06:
4504 case RTL_GIGA_MAC_VER_10:
4505 case RTL_GIGA_MAC_VER_11:
4506 case RTL_GIGA_MAC_VER_12:
4507 case RTL_GIGA_MAC_VER_13:
4508 case RTL_GIGA_MAC_VER_14:
4509 case RTL_GIGA_MAC_VER_15:
4510 case RTL_GIGA_MAC_VER_16:
4511 case RTL_GIGA_MAC_VER_17:
4516 case RTL_GIGA_MAC_VER_37:
4517 case RTL_GIGA_MAC_VER_38:
4518 ops->write = r8402_csi_write;
4519 ops->read = r8402_csi_read;
4523 ops->write = r8169_csi_write;
4524 ops->read = r8169_csi_read;
4530 unsigned int offset;
4535 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
4540 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4541 rtl_ephy_write(ioaddr, e->offset, w);
4546 static void rtl_disable_clock_request(struct pci_dev *pdev)
4548 int cap = pci_pcie_cap(pdev);
4553 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4554 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4555 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4559 static void rtl_enable_clock_request(struct pci_dev *pdev)
4561 int cap = pci_pcie_cap(pdev);
4566 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4567 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4568 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4572 #define R8168_CPCMD_QUIRK_MASK (\
4583 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4585 void __iomem *ioaddr = tp->mmio_addr;
4586 struct pci_dev *pdev = tp->pci_dev;
4588 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4590 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4592 rtl_tx_performance_tweak(pdev,
4593 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4596 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4598 void __iomem *ioaddr = tp->mmio_addr;
4600 rtl_hw_start_8168bb(tp);
4602 RTL_W8(MaxTxPacketSize, TxPacketMax);
4604 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4607 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4609 void __iomem *ioaddr = tp->mmio_addr;
4610 struct pci_dev *pdev = tp->pci_dev;
4612 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4614 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4616 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4618 rtl_disable_clock_request(pdev);
4620 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4623 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4625 void __iomem *ioaddr = tp->mmio_addr;
4626 static const struct ephy_info e_info_8168cp[] = {
4627 { 0x01, 0, 0x0001 },
4628 { 0x02, 0x0800, 0x1000 },
4629 { 0x03, 0, 0x0042 },
4630 { 0x06, 0x0080, 0x0000 },
4634 rtl_csi_access_enable_2(tp);
4636 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4638 __rtl_hw_start_8168cp(tp);
4641 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4643 void __iomem *ioaddr = tp->mmio_addr;
4644 struct pci_dev *pdev = tp->pci_dev;
4646 rtl_csi_access_enable_2(tp);
4648 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4650 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4652 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4655 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4657 void __iomem *ioaddr = tp->mmio_addr;
4658 struct pci_dev *pdev = tp->pci_dev;
4660 rtl_csi_access_enable_2(tp);
4662 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4665 RTL_W8(DBG_REG, 0x20);
4667 RTL_W8(MaxTxPacketSize, TxPacketMax);
4669 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4671 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4674 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4676 void __iomem *ioaddr = tp->mmio_addr;
4677 static const struct ephy_info e_info_8168c_1[] = {
4678 { 0x02, 0x0800, 0x1000 },
4679 { 0x03, 0, 0x0002 },
4680 { 0x06, 0x0080, 0x0000 }
4683 rtl_csi_access_enable_2(tp);
4685 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4687 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4689 __rtl_hw_start_8168cp(tp);
4692 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4694 void __iomem *ioaddr = tp->mmio_addr;
4695 static const struct ephy_info e_info_8168c_2[] = {
4696 { 0x01, 0, 0x0001 },
4697 { 0x03, 0x0400, 0x0220 }
4700 rtl_csi_access_enable_2(tp);
4702 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4704 __rtl_hw_start_8168cp(tp);
4707 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4709 rtl_hw_start_8168c_2(tp);
4712 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4714 rtl_csi_access_enable_2(tp);
4716 __rtl_hw_start_8168cp(tp);
4719 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4721 void __iomem *ioaddr = tp->mmio_addr;
4722 struct pci_dev *pdev = tp->pci_dev;
4724 rtl_csi_access_enable_2(tp);
4726 rtl_disable_clock_request(pdev);
4728 RTL_W8(MaxTxPacketSize, TxPacketMax);
4730 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4732 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4735 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
4737 void __iomem *ioaddr = tp->mmio_addr;
4738 struct pci_dev *pdev = tp->pci_dev;
4740 rtl_csi_access_enable_1(tp);
4742 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4744 RTL_W8(MaxTxPacketSize, TxPacketMax);
4746 rtl_disable_clock_request(pdev);
4749 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
4751 void __iomem *ioaddr = tp->mmio_addr;
4752 struct pci_dev *pdev = tp->pci_dev;
4753 static const struct ephy_info e_info_8168d_4[] = {
4755 { 0x19, 0x20, 0x50 },
4760 rtl_csi_access_enable_1(tp);
4762 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4764 RTL_W8(MaxTxPacketSize, TxPacketMax);
4766 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4767 const struct ephy_info *e = e_info_8168d_4 + i;
4770 w = rtl_ephy_read(ioaddr, e->offset);
4771 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4774 rtl_enable_clock_request(pdev);
4777 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
4779 void __iomem *ioaddr = tp->mmio_addr;
4780 struct pci_dev *pdev = tp->pci_dev;
4781 static const struct ephy_info e_info_8168e_1[] = {
4782 { 0x00, 0x0200, 0x0100 },
4783 { 0x00, 0x0000, 0x0004 },
4784 { 0x06, 0x0002, 0x0001 },
4785 { 0x06, 0x0000, 0x0030 },
4786 { 0x07, 0x0000, 0x2000 },
4787 { 0x00, 0x0000, 0x0020 },
4788 { 0x03, 0x5800, 0x2000 },
4789 { 0x03, 0x0000, 0x0001 },
4790 { 0x01, 0x0800, 0x1000 },
4791 { 0x07, 0x0000, 0x4000 },
4792 { 0x1e, 0x0000, 0x2000 },
4793 { 0x19, 0xffff, 0xfe6c },
4794 { 0x0a, 0x0000, 0x0040 }
4797 rtl_csi_access_enable_2(tp);
4799 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
4801 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4803 RTL_W8(MaxTxPacketSize, TxPacketMax);
4805 rtl_disable_clock_request(pdev);
4807 /* Reset tx FIFO pointer */
4808 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4809 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
4811 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4814 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
4816 void __iomem *ioaddr = tp->mmio_addr;
4817 struct pci_dev *pdev = tp->pci_dev;
4818 static const struct ephy_info e_info_8168e_2[] = {
4819 { 0x09, 0x0000, 0x0080 },
4820 { 0x19, 0x0000, 0x0224 }
4823 rtl_csi_access_enable_1(tp);
4825 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4827 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4829 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4830 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4831 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4832 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4833 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4834 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4835 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4836 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4839 RTL_W8(MaxTxPacketSize, EarlySize);
4841 rtl_disable_clock_request(pdev);
4843 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4844 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4846 /* Adjust EEE LED frequency */
4847 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4849 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4850 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4851 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4854 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
4856 void __iomem *ioaddr = tp->mmio_addr;
4857 struct pci_dev *pdev = tp->pci_dev;
4859 rtl_csi_access_enable_2(tp);
4861 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4863 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4864 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4865 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4866 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4867 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4868 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4869 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4870 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4871 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4872 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4874 RTL_W8(MaxTxPacketSize, EarlySize);
4876 rtl_disable_clock_request(pdev);
4878 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4879 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4880 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4881 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4882 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4885 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4887 void __iomem *ioaddr = tp->mmio_addr;
4888 static const struct ephy_info e_info_8168f_1[] = {
4889 { 0x06, 0x00c0, 0x0020 },
4890 { 0x08, 0x0001, 0x0002 },
4891 { 0x09, 0x0000, 0x0080 },
4892 { 0x19, 0x0000, 0x0224 }
4895 rtl_hw_start_8168f(tp);
4897 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4899 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4902 /* Adjust EEE LED frequency */
4903 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4906 static void rtl_hw_start_8411(struct rtl8169_private *tp)
4908 void __iomem *ioaddr = tp->mmio_addr;
4909 static const struct ephy_info e_info_8168f_1[] = {
4910 { 0x06, 0x00c0, 0x0020 },
4911 { 0x0f, 0xffff, 0x5200 },
4912 { 0x1e, 0x0000, 0x4000 },
4913 { 0x19, 0x0000, 0x0224 }
4916 rtl_hw_start_8168f(tp);
4918 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4920 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000,
4924 static void rtl_hw_start_8168(struct net_device *dev)
4926 struct rtl8169_private *tp = netdev_priv(dev);
4927 void __iomem *ioaddr = tp->mmio_addr;
4929 RTL_W8(Cfg9346, Cfg9346_Unlock);
4931 RTL_W8(MaxTxPacketSize, TxPacketMax);
4933 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4935 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
4937 RTL_W16(CPlusCmd, tp->cp_cmd);
4939 RTL_W16(IntrMitigate, 0x5151);
4941 /* Work around for RxFIFO overflow. */
4942 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
4943 tp->event_slow |= RxFIFOOver | PCSTimeout;
4944 tp->event_slow &= ~RxOverflow;
4947 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4949 rtl_set_rx_mode(dev);
4951 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4952 (InterFrameGap << TxInterFrameGapShift));
4956 switch (tp->mac_version) {
4957 case RTL_GIGA_MAC_VER_11:
4958 rtl_hw_start_8168bb(tp);
4961 case RTL_GIGA_MAC_VER_12:
4962 case RTL_GIGA_MAC_VER_17:
4963 rtl_hw_start_8168bef(tp);
4966 case RTL_GIGA_MAC_VER_18:
4967 rtl_hw_start_8168cp_1(tp);
4970 case RTL_GIGA_MAC_VER_19:
4971 rtl_hw_start_8168c_1(tp);
4974 case RTL_GIGA_MAC_VER_20:
4975 rtl_hw_start_8168c_2(tp);
4978 case RTL_GIGA_MAC_VER_21:
4979 rtl_hw_start_8168c_3(tp);
4982 case RTL_GIGA_MAC_VER_22:
4983 rtl_hw_start_8168c_4(tp);
4986 case RTL_GIGA_MAC_VER_23:
4987 rtl_hw_start_8168cp_2(tp);
4990 case RTL_GIGA_MAC_VER_24:
4991 rtl_hw_start_8168cp_3(tp);
4994 case RTL_GIGA_MAC_VER_25:
4995 case RTL_GIGA_MAC_VER_26:
4996 case RTL_GIGA_MAC_VER_27:
4997 rtl_hw_start_8168d(tp);
5000 case RTL_GIGA_MAC_VER_28:
5001 rtl_hw_start_8168d_4(tp);
5004 case RTL_GIGA_MAC_VER_31:
5005 rtl_hw_start_8168dp(tp);
5008 case RTL_GIGA_MAC_VER_32:
5009 case RTL_GIGA_MAC_VER_33:
5010 rtl_hw_start_8168e_1(tp);
5012 case RTL_GIGA_MAC_VER_34:
5013 rtl_hw_start_8168e_2(tp);
5016 case RTL_GIGA_MAC_VER_35:
5017 case RTL_GIGA_MAC_VER_36:
5018 rtl_hw_start_8168f_1(tp);
5021 case RTL_GIGA_MAC_VER_38:
5022 rtl_hw_start_8411(tp);
5026 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5027 dev->name, tp->mac_version);
5031 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5033 RTL_W8(Cfg9346, Cfg9346_Lock);
5035 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
5038 #define R810X_CPCMD_QUIRK_MASK (\
5049 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5051 void __iomem *ioaddr = tp->mmio_addr;
5052 struct pci_dev *pdev = tp->pci_dev;
5053 static const struct ephy_info e_info_8102e_1[] = {
5054 { 0x01, 0, 0x6e65 },
5055 { 0x02, 0, 0x091f },
5056 { 0x03, 0, 0xc2f9 },
5057 { 0x06, 0, 0xafb5 },
5058 { 0x07, 0, 0x0e00 },
5059 { 0x19, 0, 0xec80 },
5060 { 0x01, 0, 0x2e65 },
5065 rtl_csi_access_enable_2(tp);
5067 RTL_W8(DBG_REG, FIX_NAK_1);
5069 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5072 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5073 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5075 cfg1 = RTL_R8(Config1);
5076 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5077 RTL_W8(Config1, cfg1 & ~LEDS0);
5079 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5082 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5084 void __iomem *ioaddr = tp->mmio_addr;
5085 struct pci_dev *pdev = tp->pci_dev;
5087 rtl_csi_access_enable_2(tp);
5089 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5091 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5092 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5095 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5097 rtl_hw_start_8102e_2(tp);
5099 rtl_ephy_write(tp->mmio_addr, 0x03, 0xc2f9);
5102 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5104 void __iomem *ioaddr = tp->mmio_addr;
5105 static const struct ephy_info e_info_8105e_1[] = {
5106 { 0x07, 0, 0x4000 },
5107 { 0x19, 0, 0x0200 },
5108 { 0x19, 0, 0x0020 },
5109 { 0x1e, 0, 0x2000 },
5110 { 0x03, 0, 0x0001 },
5111 { 0x19, 0, 0x0100 },
5112 { 0x19, 0, 0x0004 },
5116 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5117 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5119 /* Disable Early Tally Counter */
5120 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5122 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5123 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5125 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5128 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5130 void __iomem *ioaddr = tp->mmio_addr;
5132 rtl_hw_start_8105e_1(tp);
5133 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5136 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5138 void __iomem *ioaddr = tp->mmio_addr;
5139 static const struct ephy_info e_info_8402[] = {
5140 { 0x19, 0xffff, 0xff64 },
5144 rtl_csi_access_enable_2(tp);
5146 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5147 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5149 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5150 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5152 rtl_ephy_init(ioaddr, e_info_8402, ARRAY_SIZE(e_info_8402));
5154 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5156 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5157 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5158 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5159 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5160 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5161 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5162 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00,
5166 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5168 void __iomem *ioaddr = tp->mmio_addr;
5170 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5171 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5173 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5174 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5175 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5178 static void rtl_hw_start_8101(struct net_device *dev)
5180 struct rtl8169_private *tp = netdev_priv(dev);
5181 void __iomem *ioaddr = tp->mmio_addr;
5182 struct pci_dev *pdev = tp->pci_dev;
5184 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5185 tp->event_slow &= ~RxFIFOOver;
5187 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5188 tp->mac_version == RTL_GIGA_MAC_VER_16) {
5189 int cap = pci_pcie_cap(pdev);
5192 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5193 PCI_EXP_DEVCTL_NOSNOOP_EN);
5197 RTL_W8(Cfg9346, Cfg9346_Unlock);
5199 switch (tp->mac_version) {
5200 case RTL_GIGA_MAC_VER_07:
5201 rtl_hw_start_8102e_1(tp);
5204 case RTL_GIGA_MAC_VER_08:
5205 rtl_hw_start_8102e_3(tp);
5208 case RTL_GIGA_MAC_VER_09:
5209 rtl_hw_start_8102e_2(tp);
5212 case RTL_GIGA_MAC_VER_29:
5213 rtl_hw_start_8105e_1(tp);
5215 case RTL_GIGA_MAC_VER_30:
5216 rtl_hw_start_8105e_2(tp);
5219 case RTL_GIGA_MAC_VER_37:
5220 rtl_hw_start_8402(tp);
5223 case RTL_GIGA_MAC_VER_39:
5224 rtl_hw_start_8106(tp);
5228 RTL_W8(Cfg9346, Cfg9346_Lock);
5230 RTL_W8(MaxTxPacketSize, TxPacketMax);
5232 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5234 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5235 RTL_W16(CPlusCmd, tp->cp_cmd);
5237 RTL_W16(IntrMitigate, 0x0000);
5239 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5241 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5242 rtl_set_rx_tx_config_registers(tp);
5246 rtl_set_rx_mode(dev);
5248 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5251 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5253 struct rtl8169_private *tp = netdev_priv(dev);
5255 if (new_mtu < ETH_ZLEN ||
5256 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
5259 if (new_mtu > ETH_DATA_LEN)
5260 rtl_hw_jumbo_enable(tp);
5262 rtl_hw_jumbo_disable(tp);
5265 netdev_update_features(dev);
5270 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5272 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5273 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5276 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5277 void **data_buff, struct RxDesc *desc)
5279 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
5284 rtl8169_make_unusable_by_asic(desc);
5287 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5289 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5291 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5294 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5297 desc->addr = cpu_to_le64(mapping);
5299 rtl8169_mark_to_asic(desc, rx_buf_sz);
5302 static inline void *rtl8169_align(void *data)
5304 return (void *)ALIGN((long)data, 16);
5307 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5308 struct RxDesc *desc)
5312 struct device *d = &tp->pci_dev->dev;
5313 struct net_device *dev = tp->dev;
5314 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
5316 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5320 if (rtl8169_align(data) != data) {
5322 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5327 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
5329 if (unlikely(dma_mapping_error(d, mapping))) {
5330 if (net_ratelimit())
5331 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5335 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
5343 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5347 for (i = 0; i < NUM_RX_DESC; i++) {
5348 if (tp->Rx_databuff[i]) {
5349 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5350 tp->RxDescArray + i);
5355 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5357 desc->opts1 |= cpu_to_le32(RingEnd);
5360 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5364 for (i = 0; i < NUM_RX_DESC; i++) {
5367 if (tp->Rx_databuff[i])
5370 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5372 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5375 tp->Rx_databuff[i] = data;
5378 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5382 rtl8169_rx_clear(tp);
5386 static int rtl8169_init_ring(struct net_device *dev)
5388 struct rtl8169_private *tp = netdev_priv(dev);
5390 rtl8169_init_ring_indexes(tp);
5392 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
5393 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
5395 return rtl8169_rx_fill(tp);
5398 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5399 struct TxDesc *desc)
5401 unsigned int len = tx_skb->len;
5403 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5411 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5416 for (i = 0; i < n; i++) {
5417 unsigned int entry = (start + i) % NUM_TX_DESC;
5418 struct ring_info *tx_skb = tp->tx_skb + entry;
5419 unsigned int len = tx_skb->len;
5422 struct sk_buff *skb = tx_skb->skb;
5424 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5425 tp->TxDescArray + entry);
5427 tp->dev->stats.tx_dropped++;
5435 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5437 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5438 tp->cur_tx = tp->dirty_tx = 0;
5439 netdev_reset_queue(tp->dev);
5442 static void rtl_reset_work(struct rtl8169_private *tp)
5444 struct net_device *dev = tp->dev;
5447 napi_disable(&tp->napi);
5448 netif_stop_queue(dev);
5449 synchronize_sched();
5451 rtl8169_hw_reset(tp);
5453 for (i = 0; i < NUM_RX_DESC; i++)
5454 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5456 rtl8169_tx_clear(tp);
5457 rtl8169_init_ring_indexes(tp);
5459 napi_enable(&tp->napi);
5461 netif_wake_queue(dev);
5462 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
5465 static void rtl8169_tx_timeout(struct net_device *dev)
5467 struct rtl8169_private *tp = netdev_priv(dev);
5469 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5472 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5475 struct skb_shared_info *info = skb_shinfo(skb);
5476 unsigned int cur_frag, entry;
5477 struct TxDesc * uninitialized_var(txd);
5478 struct device *d = &tp->pci_dev->dev;
5481 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5482 const skb_frag_t *frag = info->frags + cur_frag;
5487 entry = (entry + 1) % NUM_TX_DESC;
5489 txd = tp->TxDescArray + entry;
5490 len = skb_frag_size(frag);
5491 addr = skb_frag_address(frag);
5492 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5493 if (unlikely(dma_mapping_error(d, mapping))) {
5494 if (net_ratelimit())
5495 netif_err(tp, drv, tp->dev,
5496 "Failed to map TX fragments DMA!\n");
5500 /* Anti gcc 2.95.3 bugware (sic) */
5501 status = opts[0] | len |
5502 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5504 txd->opts1 = cpu_to_le32(status);
5505 txd->opts2 = cpu_to_le32(opts[1]);
5506 txd->addr = cpu_to_le64(mapping);
5508 tp->tx_skb[entry].len = len;
5512 tp->tx_skb[entry].skb = skb;
5513 txd->opts1 |= cpu_to_le32(LastFrag);
5519 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5523 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5524 struct sk_buff *skb, u32 *opts)
5526 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
5527 u32 mss = skb_shinfo(skb)->gso_size;
5528 int offset = info->opts_offset;
5532 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5533 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5534 const struct iphdr *ip = ip_hdr(skb);
5536 if (ip->protocol == IPPROTO_TCP)
5537 opts[offset] |= info->checksum.tcp;
5538 else if (ip->protocol == IPPROTO_UDP)
5539 opts[offset] |= info->checksum.udp;
5545 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5546 struct net_device *dev)
5548 struct rtl8169_private *tp = netdev_priv(dev);
5549 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5550 struct TxDesc *txd = tp->TxDescArray + entry;
5551 void __iomem *ioaddr = tp->mmio_addr;
5552 struct device *d = &tp->pci_dev->dev;
5558 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
5559 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5563 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5566 len = skb_headlen(skb);
5567 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
5568 if (unlikely(dma_mapping_error(d, mapping))) {
5569 if (net_ratelimit())
5570 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
5574 tp->tx_skb[entry].len = len;
5575 txd->addr = cpu_to_le64(mapping);
5577 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5580 rtl8169_tso_csum(tp, skb, opts);
5582 frags = rtl8169_xmit_frags(tp, skb, opts);
5586 opts[0] |= FirstFrag;
5588 opts[0] |= FirstFrag | LastFrag;
5589 tp->tx_skb[entry].skb = skb;
5592 txd->opts2 = cpu_to_le32(opts[1]);
5594 netdev_sent_queue(dev, skb->len);
5596 skb_tx_timestamp(skb);
5600 /* Anti gcc 2.95.3 bugware (sic) */
5601 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
5602 txd->opts1 = cpu_to_le32(status);
5604 tp->cur_tx += frags + 1;
5608 RTL_W8(TxPoll, NPQ);
5612 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5613 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5614 * not miss a ring update when it notices a stopped queue.
5617 netif_stop_queue(dev);
5618 /* Sync with rtl_tx:
5619 * - publish queue status and cur_tx ring index (write barrier)
5620 * - refresh dirty_tx ring index (read barrier).
5621 * May the current thread have a pessimistic view of the ring
5622 * status and forget to wake up queue, a racing rtl_tx thread
5626 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
5627 netif_wake_queue(dev);
5630 return NETDEV_TX_OK;
5633 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
5636 dev->stats.tx_dropped++;
5637 return NETDEV_TX_OK;
5640 netif_stop_queue(dev);
5641 dev->stats.tx_dropped++;
5642 return NETDEV_TX_BUSY;
5645 static void rtl8169_pcierr_interrupt(struct net_device *dev)
5647 struct rtl8169_private *tp = netdev_priv(dev);
5648 struct pci_dev *pdev = tp->pci_dev;
5649 u16 pci_status, pci_cmd;
5651 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5652 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5654 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5655 pci_cmd, pci_status);
5658 * The recovery sequence below admits a very elaborated explanation:
5659 * - it seems to work;
5660 * - I did not see what else could be done;
5661 * - it makes iop3xx happy.
5663 * Feel free to adjust to your needs.
5665 if (pdev->broken_parity_status)
5666 pci_cmd &= ~PCI_COMMAND_PARITY;
5668 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5670 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
5672 pci_write_config_word(pdev, PCI_STATUS,
5673 pci_status & (PCI_STATUS_DETECTED_PARITY |
5674 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5675 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5677 /* The infamous DAC f*ckup only happens at boot time */
5678 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
5679 void __iomem *ioaddr = tp->mmio_addr;
5681 netif_info(tp, intr, dev, "disabling PCI DAC\n");
5682 tp->cp_cmd &= ~PCIDAC;
5683 RTL_W16(CPlusCmd, tp->cp_cmd);
5684 dev->features &= ~NETIF_F_HIGHDMA;
5687 rtl8169_hw_reset(tp);
5689 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5697 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
5699 struct rtl8169_stats *tx_stats = &tp->tx_stats;
5700 unsigned int dirty_tx, tx_left;
5701 struct rtl_txc txc = { 0, 0 };
5703 dirty_tx = tp->dirty_tx;
5705 tx_left = tp->cur_tx - dirty_tx;
5707 while (tx_left > 0) {
5708 unsigned int entry = dirty_tx % NUM_TX_DESC;
5709 struct ring_info *tx_skb = tp->tx_skb + entry;
5713 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5714 if (status & DescOwn)
5717 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5718 tp->TxDescArray + entry);
5719 if (status & LastFrag) {
5720 struct sk_buff *skb = tx_skb->skb;
5723 txc.bytes += skb->len;
5731 u64_stats_update_begin(&tx_stats->syncp);
5732 tx_stats->packets += txc.packets;
5733 tx_stats->bytes += txc.bytes;
5734 u64_stats_update_end(&tx_stats->syncp);
5736 netdev_completed_queue(dev, txc.packets, txc.bytes);
5738 if (tp->dirty_tx != dirty_tx) {
5739 tp->dirty_tx = dirty_tx;
5740 /* Sync with rtl8169_start_xmit:
5741 * - publish dirty_tx ring index (write barrier)
5742 * - refresh cur_tx ring index and queue status (read barrier)
5743 * May the current thread miss the stopped queue condition,
5744 * a racing xmit thread can only have a right view of the
5748 if (netif_queue_stopped(dev) &&
5749 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
5750 netif_wake_queue(dev);
5753 * 8168 hack: TxPoll requests are lost when the Tx packets are
5754 * too close. Let's kick an extra TxPoll request when a burst
5755 * of start_xmit activity is detected (if it is not detected,
5756 * it is slow enough). -- FR
5758 if (tp->cur_tx != dirty_tx) {
5759 void __iomem *ioaddr = tp->mmio_addr;
5761 RTL_W8(TxPoll, NPQ);
5766 static inline int rtl8169_fragmented_frame(u32 status)
5768 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5771 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
5773 u32 status = opts1 & RxProtoMask;
5775 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
5776 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
5777 skb->ip_summed = CHECKSUM_UNNECESSARY;
5779 skb_checksum_none_assert(skb);
5782 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5783 struct rtl8169_private *tp,
5787 struct sk_buff *skb;
5788 struct device *d = &tp->pci_dev->dev;
5790 data = rtl8169_align(data);
5791 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
5793 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5795 memcpy(skb->data, data, pkt_size);
5796 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5801 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
5803 unsigned int cur_rx, rx_left;
5806 cur_rx = tp->cur_rx;
5807 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
5808 rx_left = min(rx_left, budget);
5810 for (; rx_left > 0; rx_left--, cur_rx++) {
5811 unsigned int entry = cur_rx % NUM_RX_DESC;
5812 struct RxDesc *desc = tp->RxDescArray + entry;
5816 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
5818 if (status & DescOwn)
5820 if (unlikely(status & RxRES)) {
5821 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5823 dev->stats.rx_errors++;
5824 if (status & (RxRWT | RxRUNT))
5825 dev->stats.rx_length_errors++;
5827 dev->stats.rx_crc_errors++;
5828 if (status & RxFOVF) {
5829 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5830 dev->stats.rx_fifo_errors++;
5832 if ((status & (RxRUNT | RxCRC)) &&
5833 !(status & (RxRWT | RxFOVF)) &&
5834 (dev->features & NETIF_F_RXALL))
5837 rtl8169_mark_to_asic(desc, rx_buf_sz);
5839 struct sk_buff *skb;
5844 addr = le64_to_cpu(desc->addr);
5845 if (likely(!(dev->features & NETIF_F_RXFCS)))
5846 pkt_size = (status & 0x00003fff) - 4;
5848 pkt_size = status & 0x00003fff;
5851 * The driver does not support incoming fragmented
5852 * frames. They are seen as a symptom of over-mtu
5855 if (unlikely(rtl8169_fragmented_frame(status))) {
5856 dev->stats.rx_dropped++;
5857 dev->stats.rx_length_errors++;
5858 rtl8169_mark_to_asic(desc, rx_buf_sz);
5862 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5863 tp, pkt_size, addr);
5864 rtl8169_mark_to_asic(desc, rx_buf_sz);
5866 dev->stats.rx_dropped++;
5870 rtl8169_rx_csum(skb, status);
5871 skb_put(skb, pkt_size);
5872 skb->protocol = eth_type_trans(skb, dev);
5874 rtl8169_rx_vlan_tag(desc, skb);
5876 napi_gro_receive(&tp->napi, skb);
5878 u64_stats_update_begin(&tp->rx_stats.syncp);
5879 tp->rx_stats.packets++;
5880 tp->rx_stats.bytes += pkt_size;
5881 u64_stats_update_end(&tp->rx_stats.syncp);
5884 /* Work around for AMD plateform. */
5885 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
5886 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5892 count = cur_rx - tp->cur_rx;
5893 tp->cur_rx = cur_rx;
5895 tp->dirty_rx += count;
5900 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5902 struct net_device *dev = dev_instance;
5903 struct rtl8169_private *tp = netdev_priv(dev);
5907 status = rtl_get_events(tp);
5908 if (status && status != 0xffff) {
5909 status &= RTL_EVENT_NAPI | tp->event_slow;
5913 rtl_irq_disable(tp);
5914 napi_schedule(&tp->napi);
5917 return IRQ_RETVAL(handled);
5921 * Workqueue context.
5923 static void rtl_slow_event_work(struct rtl8169_private *tp)
5925 struct net_device *dev = tp->dev;
5928 status = rtl_get_events(tp) & tp->event_slow;
5929 rtl_ack_events(tp, status);
5931 if (unlikely(status & RxFIFOOver)) {
5932 switch (tp->mac_version) {
5933 /* Work around for rx fifo overflow */
5934 case RTL_GIGA_MAC_VER_11:
5935 netif_stop_queue(dev);
5936 /* XXX - Hack alert. See rtl_task(). */
5937 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
5943 if (unlikely(status & SYSErr))
5944 rtl8169_pcierr_interrupt(dev);
5946 if (status & LinkChg)
5947 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5949 rtl_irq_enable_all(tp);
5952 static void rtl_task(struct work_struct *work)
5954 static const struct {
5956 void (*action)(struct rtl8169_private *);
5958 /* XXX - keep rtl_slow_event_work() as first element. */
5959 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5960 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5961 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5963 struct rtl8169_private *tp =
5964 container_of(work, struct rtl8169_private, wk.work);
5965 struct net_device *dev = tp->dev;
5970 if (!netif_running(dev) ||
5971 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
5974 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5977 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
5979 rtl_work[i].action(tp);
5983 rtl_unlock_work(tp);
5986 static int rtl8169_poll(struct napi_struct *napi, int budget)
5988 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5989 struct net_device *dev = tp->dev;
5990 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5994 status = rtl_get_events(tp);
5995 rtl_ack_events(tp, status & ~tp->event_slow);
5997 if (status & RTL_EVENT_NAPI_RX)
5998 work_done = rtl_rx(dev, tp, (u32) budget);
6000 if (status & RTL_EVENT_NAPI_TX)
6003 if (status & tp->event_slow) {
6004 enable_mask &= ~tp->event_slow;
6006 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6009 if (work_done < budget) {
6010 napi_complete(napi);
6012 rtl_irq_enable(tp, enable_mask);
6019 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6021 struct rtl8169_private *tp = netdev_priv(dev);
6023 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6026 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6027 RTL_W32(RxMissed, 0);
6030 static void rtl8169_down(struct net_device *dev)
6032 struct rtl8169_private *tp = netdev_priv(dev);
6033 void __iomem *ioaddr = tp->mmio_addr;
6035 del_timer_sync(&tp->timer);
6037 napi_disable(&tp->napi);
6038 netif_stop_queue(dev);
6040 rtl8169_hw_reset(tp);
6042 * At this point device interrupts can not be enabled in any function,
6043 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6044 * and napi is disabled (rtl8169_poll).
6046 rtl8169_rx_missed(dev, ioaddr);
6048 /* Give a racing hard_start_xmit a few cycles to complete. */
6049 synchronize_sched();
6051 rtl8169_tx_clear(tp);
6053 rtl8169_rx_clear(tp);
6055 rtl_pll_power_down(tp);
6058 static int rtl8169_close(struct net_device *dev)
6060 struct rtl8169_private *tp = netdev_priv(dev);
6061 struct pci_dev *pdev = tp->pci_dev;
6063 pm_runtime_get_sync(&pdev->dev);
6065 /* Update counters before going down */
6066 rtl8169_update_counters(dev);
6069 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6072 rtl_unlock_work(tp);
6074 free_irq(pdev->irq, dev);
6076 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6078 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6080 tp->TxDescArray = NULL;
6081 tp->RxDescArray = NULL;
6083 pm_runtime_put_sync(&pdev->dev);
6088 #ifdef CONFIG_NET_POLL_CONTROLLER
6089 static void rtl8169_netpoll(struct net_device *dev)
6091 struct rtl8169_private *tp = netdev_priv(dev);
6093 rtl8169_interrupt(tp->pci_dev->irq, dev);
6097 static int rtl_open(struct net_device *dev)
6099 struct rtl8169_private *tp = netdev_priv(dev);
6100 void __iomem *ioaddr = tp->mmio_addr;
6101 struct pci_dev *pdev = tp->pci_dev;
6102 int retval = -ENOMEM;
6104 pm_runtime_get_sync(&pdev->dev);
6107 * Rx and Tx descriptors needs 256 bytes alignment.
6108 * dma_alloc_coherent provides more.
6110 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6111 &tp->TxPhyAddr, GFP_KERNEL);
6112 if (!tp->TxDescArray)
6113 goto err_pm_runtime_put;
6115 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6116 &tp->RxPhyAddr, GFP_KERNEL);
6117 if (!tp->RxDescArray)
6120 retval = rtl8169_init_ring(dev);
6124 INIT_WORK(&tp->wk.work, rtl_task);
6128 rtl_request_firmware(tp);
6130 retval = request_irq(pdev->irq, rtl8169_interrupt,
6131 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6134 goto err_release_fw_2;
6138 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6140 napi_enable(&tp->napi);
6142 rtl8169_init_phy(dev, tp);
6144 __rtl8169_set_features(dev, dev->features);
6146 rtl_pll_power_up(tp);
6150 netif_start_queue(dev);
6152 rtl_unlock_work(tp);
6154 tp->saved_wolopts = 0;
6155 pm_runtime_put_noidle(&pdev->dev);
6157 rtl8169_check_link_status(dev, tp, ioaddr);
6162 rtl_release_firmware(tp);
6163 rtl8169_rx_clear(tp);
6165 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6167 tp->RxDescArray = NULL;
6169 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6171 tp->TxDescArray = NULL;
6173 pm_runtime_put_noidle(&pdev->dev);
6177 static struct rtnl_link_stats64 *
6178 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6180 struct rtl8169_private *tp = netdev_priv(dev);
6181 void __iomem *ioaddr = tp->mmio_addr;
6184 if (netif_running(dev))
6185 rtl8169_rx_missed(dev, ioaddr);
6188 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6189 stats->rx_packets = tp->rx_stats.packets;
6190 stats->rx_bytes = tp->rx_stats.bytes;
6191 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6195 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6196 stats->tx_packets = tp->tx_stats.packets;
6197 stats->tx_bytes = tp->tx_stats.bytes;
6198 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6200 stats->rx_dropped = dev->stats.rx_dropped;
6201 stats->tx_dropped = dev->stats.tx_dropped;
6202 stats->rx_length_errors = dev->stats.rx_length_errors;
6203 stats->rx_errors = dev->stats.rx_errors;
6204 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6205 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6206 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6211 static void rtl8169_net_suspend(struct net_device *dev)
6213 struct rtl8169_private *tp = netdev_priv(dev);
6215 if (!netif_running(dev))
6218 netif_device_detach(dev);
6219 netif_stop_queue(dev);
6222 napi_disable(&tp->napi);
6223 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6224 rtl_unlock_work(tp);
6226 rtl_pll_power_down(tp);
6231 static int rtl8169_suspend(struct device *device)
6233 struct pci_dev *pdev = to_pci_dev(device);
6234 struct net_device *dev = pci_get_drvdata(pdev);
6236 rtl8169_net_suspend(dev);
6241 static void __rtl8169_resume(struct net_device *dev)
6243 struct rtl8169_private *tp = netdev_priv(dev);
6245 netif_device_attach(dev);
6247 rtl_pll_power_up(tp);
6250 napi_enable(&tp->napi);
6251 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6252 rtl_unlock_work(tp);
6254 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6257 static int rtl8169_resume(struct device *device)
6259 struct pci_dev *pdev = to_pci_dev(device);
6260 struct net_device *dev = pci_get_drvdata(pdev);
6261 struct rtl8169_private *tp = netdev_priv(dev);
6263 rtl8169_init_phy(dev, tp);
6265 if (netif_running(dev))
6266 __rtl8169_resume(dev);
6271 static int rtl8169_runtime_suspend(struct device *device)
6273 struct pci_dev *pdev = to_pci_dev(device);
6274 struct net_device *dev = pci_get_drvdata(pdev);
6275 struct rtl8169_private *tp = netdev_priv(dev);
6277 if (!tp->TxDescArray)
6281 tp->saved_wolopts = __rtl8169_get_wol(tp);
6282 __rtl8169_set_wol(tp, WAKE_ANY);
6283 rtl_unlock_work(tp);
6285 rtl8169_net_suspend(dev);
6290 static int rtl8169_runtime_resume(struct device *device)
6292 struct pci_dev *pdev = to_pci_dev(device);
6293 struct net_device *dev = pci_get_drvdata(pdev);
6294 struct rtl8169_private *tp = netdev_priv(dev);
6296 if (!tp->TxDescArray)
6300 __rtl8169_set_wol(tp, tp->saved_wolopts);
6301 tp->saved_wolopts = 0;
6302 rtl_unlock_work(tp);
6304 rtl8169_init_phy(dev, tp);
6306 __rtl8169_resume(dev);
6311 static int rtl8169_runtime_idle(struct device *device)
6313 struct pci_dev *pdev = to_pci_dev(device);
6314 struct net_device *dev = pci_get_drvdata(pdev);
6315 struct rtl8169_private *tp = netdev_priv(dev);
6317 return tp->TxDescArray ? -EBUSY : 0;
6320 static const struct dev_pm_ops rtl8169_pm_ops = {
6321 .suspend = rtl8169_suspend,
6322 .resume = rtl8169_resume,
6323 .freeze = rtl8169_suspend,
6324 .thaw = rtl8169_resume,
6325 .poweroff = rtl8169_suspend,
6326 .restore = rtl8169_resume,
6327 .runtime_suspend = rtl8169_runtime_suspend,
6328 .runtime_resume = rtl8169_runtime_resume,
6329 .runtime_idle = rtl8169_runtime_idle,
6332 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6334 #else /* !CONFIG_PM */
6336 #define RTL8169_PM_OPS NULL
6338 #endif /* !CONFIG_PM */
6340 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6342 void __iomem *ioaddr = tp->mmio_addr;
6344 /* WoL fails with 8168b when the receiver is disabled. */
6345 switch (tp->mac_version) {
6346 case RTL_GIGA_MAC_VER_11:
6347 case RTL_GIGA_MAC_VER_12:
6348 case RTL_GIGA_MAC_VER_17:
6349 pci_clear_master(tp->pci_dev);
6351 RTL_W8(ChipCmd, CmdRxEnb);
6360 static void rtl_shutdown(struct pci_dev *pdev)
6362 struct net_device *dev = pci_get_drvdata(pdev);
6363 struct rtl8169_private *tp = netdev_priv(dev);
6364 struct device *d = &pdev->dev;
6366 pm_runtime_get_sync(d);
6368 rtl8169_net_suspend(dev);
6370 /* Restore original MAC address */
6371 rtl_rar_set(tp, dev->perm_addr);
6373 rtl8169_hw_reset(tp);
6375 if (system_state == SYSTEM_POWER_OFF) {
6376 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6377 rtl_wol_suspend_quirk(tp);
6378 rtl_wol_shutdown_quirk(tp);
6381 pci_wake_from_d3(pdev, true);
6382 pci_set_power_state(pdev, PCI_D3hot);
6385 pm_runtime_put_noidle(d);
6388 static void __devexit rtl_remove_one(struct pci_dev *pdev)
6390 struct net_device *dev = pci_get_drvdata(pdev);
6391 struct rtl8169_private *tp = netdev_priv(dev);
6393 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6394 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6395 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6396 rtl8168_driver_stop(tp);
6399 cancel_work_sync(&tp->wk.work);
6401 netif_napi_del(&tp->napi);
6403 unregister_netdev(dev);
6405 rtl_release_firmware(tp);
6407 if (pci_dev_run_wake(pdev))
6408 pm_runtime_get_noresume(&pdev->dev);
6410 /* restore original MAC address */
6411 rtl_rar_set(tp, dev->perm_addr);
6413 rtl_disable_msi(pdev, tp);
6414 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6415 pci_set_drvdata(pdev, NULL);
6418 static const struct net_device_ops rtl_netdev_ops = {
6419 .ndo_open = rtl_open,
6420 .ndo_stop = rtl8169_close,
6421 .ndo_get_stats64 = rtl8169_get_stats64,
6422 .ndo_start_xmit = rtl8169_start_xmit,
6423 .ndo_tx_timeout = rtl8169_tx_timeout,
6424 .ndo_validate_addr = eth_validate_addr,
6425 .ndo_change_mtu = rtl8169_change_mtu,
6426 .ndo_fix_features = rtl8169_fix_features,
6427 .ndo_set_features = rtl8169_set_features,
6428 .ndo_set_mac_address = rtl_set_mac_address,
6429 .ndo_do_ioctl = rtl8169_ioctl,
6430 .ndo_set_rx_mode = rtl_set_rx_mode,
6431 #ifdef CONFIG_NET_POLL_CONTROLLER
6432 .ndo_poll_controller = rtl8169_netpoll,
6437 static const struct rtl_cfg_info {
6438 void (*hw_start)(struct net_device *);
6439 unsigned int region;
6444 } rtl_cfg_infos [] = {
6446 .hw_start = rtl_hw_start_8169,
6449 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6450 .features = RTL_FEATURE_GMII,
6451 .default_ver = RTL_GIGA_MAC_VER_01,
6454 .hw_start = rtl_hw_start_8168,
6457 .event_slow = SYSErr | LinkChg | RxOverflow,
6458 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6459 .default_ver = RTL_GIGA_MAC_VER_11,
6462 .hw_start = rtl_hw_start_8101,
6465 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6467 .features = RTL_FEATURE_MSI,
6468 .default_ver = RTL_GIGA_MAC_VER_13,
6472 /* Cfg9346_Unlock assumed. */
6473 static unsigned rtl_try_msi(struct rtl8169_private *tp,
6474 const struct rtl_cfg_info *cfg)
6476 void __iomem *ioaddr = tp->mmio_addr;
6480 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6481 if (cfg->features & RTL_FEATURE_MSI) {
6482 if (pci_enable_msi(tp->pci_dev)) {
6483 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6486 msi = RTL_FEATURE_MSI;
6489 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6490 RTL_W8(Config2, cfg2);
6494 static int __devinit
6495 rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6497 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6498 const unsigned int region = cfg->region;
6499 struct rtl8169_private *tp;
6500 struct mii_if_info *mii;
6501 struct net_device *dev;
6502 void __iomem *ioaddr;
6506 if (netif_msg_drv(&debug)) {
6507 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6508 MODULENAME, RTL8169_VERSION);
6511 dev = alloc_etherdev(sizeof (*tp));
6517 SET_NETDEV_DEV(dev, &pdev->dev);
6518 dev->netdev_ops = &rtl_netdev_ops;
6519 tp = netdev_priv(dev);
6522 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6526 mii->mdio_read = rtl_mdio_read;
6527 mii->mdio_write = rtl_mdio_write;
6528 mii->phy_id_mask = 0x1f;
6529 mii->reg_num_mask = 0x1f;
6530 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6532 /* disable ASPM completely as that cause random device stop working
6533 * problems as well as full system hangs for some PCIe devices users */
6534 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6535 PCIE_LINK_STATE_CLKPM);
6537 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6538 rc = pci_enable_device(pdev);
6540 netif_err(tp, probe, dev, "enable failure\n");
6541 goto err_out_free_dev_1;
6544 if (pci_set_mwi(pdev) < 0)
6545 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6547 /* make sure PCI base addr 1 is MMIO */
6548 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6549 netif_err(tp, probe, dev,
6550 "region #%d not an MMIO resource, aborting\n",
6556 /* check for weird/broken PCI region reporting */
6557 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6558 netif_err(tp, probe, dev,
6559 "Invalid PCI region size(s), aborting\n");
6564 rc = pci_request_regions(pdev, MODULENAME);
6566 netif_err(tp, probe, dev, "could not request regions\n");
6570 tp->cp_cmd = RxChkSum;
6572 if ((sizeof(dma_addr_t) > 4) &&
6573 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6574 tp->cp_cmd |= PCIDAC;
6575 dev->features |= NETIF_F_HIGHDMA;
6577 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6579 netif_err(tp, probe, dev, "DMA configuration failed\n");
6580 goto err_out_free_res_3;
6584 /* ioremap MMIO region */
6585 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6587 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6589 goto err_out_free_res_3;
6591 tp->mmio_addr = ioaddr;
6593 if (!pci_is_pcie(pdev))
6594 netif_info(tp, probe, dev, "not PCI Express\n");
6596 /* Identify chip attached to board */
6597 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6601 rtl_irq_disable(tp);
6605 rtl_ack_events(tp, 0xffff);
6607 pci_set_master(pdev);
6610 * Pretend we are using VLANs; This bypasses a nasty bug where
6611 * Interrupts stop flowing on high load on 8110SCd controllers.
6613 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6614 tp->cp_cmd |= RxVlan;
6616 rtl_init_mdio_ops(tp);
6617 rtl_init_pll_power_ops(tp);
6618 rtl_init_jumbo_ops(tp);
6619 rtl_init_csi_ops(tp);
6621 rtl8169_print_mac_version(tp);
6623 chipset = tp->mac_version;
6624 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6626 RTL_W8(Cfg9346, Cfg9346_Unlock);
6627 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6628 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6629 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6630 tp->features |= RTL_FEATURE_WOL;
6631 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6632 tp->features |= RTL_FEATURE_WOL;
6633 tp->features |= rtl_try_msi(tp, cfg);
6634 RTL_W8(Cfg9346, Cfg9346_Lock);
6636 if (rtl_tbi_enabled(tp)) {
6637 tp->set_speed = rtl8169_set_speed_tbi;
6638 tp->get_settings = rtl8169_gset_tbi;
6639 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6640 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6641 tp->link_ok = rtl8169_tbi_link_ok;
6642 tp->do_ioctl = rtl_tbi_ioctl;
6644 tp->set_speed = rtl8169_set_speed_xmii;
6645 tp->get_settings = rtl8169_gset_xmii;
6646 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6647 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6648 tp->link_ok = rtl8169_xmii_link_ok;
6649 tp->do_ioctl = rtl_xmii_ioctl;
6652 mutex_init(&tp->wk.mutex);
6654 /* Get MAC address */
6655 for (i = 0; i < ETH_ALEN; i++)
6656 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6657 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6659 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6660 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
6662 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6664 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6665 * properly for all devices */
6666 dev->features |= NETIF_F_RXCSUM |
6667 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6669 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6670 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6671 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6674 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6675 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6676 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6678 dev->hw_features |= NETIF_F_RXALL;
6679 dev->hw_features |= NETIF_F_RXFCS;
6681 tp->hw_start = cfg->hw_start;
6682 tp->event_slow = cfg->event_slow;
6684 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6685 ~(RxBOVF | RxFOVF) : ~0;
6687 init_timer(&tp->timer);
6688 tp->timer.data = (unsigned long) dev;
6689 tp->timer.function = rtl8169_phy_timer;
6691 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6693 rc = register_netdev(dev);
6697 pci_set_drvdata(pdev, dev);
6699 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6700 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6701 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
6702 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6703 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6704 "tx checksumming: %s]\n",
6705 rtl_chip_infos[chipset].jumbo_max,
6706 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6709 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6710 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6711 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6712 rtl8168_driver_start(tp);
6715 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6717 if (pci_dev_run_wake(pdev))
6718 pm_runtime_put_noidle(&pdev->dev);
6720 netif_carrier_off(dev);
6726 netif_napi_del(&tp->napi);
6727 rtl_disable_msi(pdev, tp);
6730 pci_release_regions(pdev);
6732 pci_clear_mwi(pdev);
6733 pci_disable_device(pdev);
6739 static struct pci_driver rtl8169_pci_driver = {
6741 .id_table = rtl8169_pci_tbl,
6742 .probe = rtl_init_one,
6743 .remove = __devexit_p(rtl_remove_one),
6744 .shutdown = rtl_shutdown,
6745 .driver.pm = RTL8169_PM_OPS,
6748 static int __init rtl8169_init_module(void)
6750 return pci_register_driver(&rtl8169_pci_driver);
6753 static void __exit rtl8169_cleanup_module(void)
6755 pci_unregister_driver(&rtl8169_pci_driver);
6758 module_init(rtl8169_init_module);
6759 module_exit(rtl8169_cleanup_module);