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/interrupt.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/firmware.h>
28 #include <linux/pci-aspm.h>
29 #include <linux/prefetch.h>
30 #include <linux/ipv6.h>
31 #include <net/ip6_checksum.h>
36 #define RTL8169_VERSION "2.3LK-NAPI"
37 #define MODULENAME "r8169"
38 #define PFX MODULENAME ": "
40 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
42 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
44 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
45 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
47 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
48 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
49 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
50 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
51 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
52 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
53 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
54 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
57 #define assert(expr) \
59 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
60 #expr,__FILE__,__func__,__LINE__); \
62 #define dprintk(fmt, args...) \
63 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
65 #define assert(expr) do {} while (0)
66 #define dprintk(fmt, args...) do {} while (0)
67 #endif /* RTL8169_DEBUG */
69 #define R8169_MSG_DEFAULT \
70 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
72 #define TX_SLOTS_AVAIL(tp) \
73 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
75 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
76 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
77 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
79 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
80 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
81 static const int multicast_filter_limit = 32;
83 #define MAX_READ_REQUEST_SHIFT 12
84 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
85 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
87 #define R8169_REGS_SIZE 256
88 #define R8169_NAPI_WEIGHT 64
89 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
90 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
91 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
92 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
94 #define RTL8169_TX_TIMEOUT (6*HZ)
95 #define RTL8169_PHY_TIMEOUT (10*HZ)
97 /* write/read MMIO register */
98 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
99 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
100 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
101 #define RTL_R8(reg) readb (ioaddr + (reg))
102 #define RTL_R16(reg) readw (ioaddr + (reg))
103 #define RTL_R32(reg) readl (ioaddr + (reg))
106 RTL_GIGA_MAC_VER_01 = 0,
150 RTL_GIGA_MAC_NONE = 0xff,
153 enum rtl_tx_desc_version {
158 #define JUMBO_1K ETH_DATA_LEN
159 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
160 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
161 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
162 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
164 #define _R(NAME,TD,FW,SZ,B) { \
172 static const struct {
174 enum rtl_tx_desc_version txd_version;
178 } rtl_chip_infos[] = {
180 [RTL_GIGA_MAC_VER_01] =
181 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
182 [RTL_GIGA_MAC_VER_02] =
183 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
184 [RTL_GIGA_MAC_VER_03] =
185 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
186 [RTL_GIGA_MAC_VER_04] =
187 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
188 [RTL_GIGA_MAC_VER_05] =
189 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
190 [RTL_GIGA_MAC_VER_06] =
191 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
193 [RTL_GIGA_MAC_VER_07] =
194 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
195 [RTL_GIGA_MAC_VER_08] =
196 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
197 [RTL_GIGA_MAC_VER_09] =
198 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
199 [RTL_GIGA_MAC_VER_10] =
200 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
201 [RTL_GIGA_MAC_VER_11] =
202 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
203 [RTL_GIGA_MAC_VER_12] =
204 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
205 [RTL_GIGA_MAC_VER_13] =
206 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
207 [RTL_GIGA_MAC_VER_14] =
208 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
209 [RTL_GIGA_MAC_VER_15] =
210 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
211 [RTL_GIGA_MAC_VER_16] =
212 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
213 [RTL_GIGA_MAC_VER_17] =
214 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
215 [RTL_GIGA_MAC_VER_18] =
216 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
217 [RTL_GIGA_MAC_VER_19] =
218 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
219 [RTL_GIGA_MAC_VER_20] =
220 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
221 [RTL_GIGA_MAC_VER_21] =
222 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
223 [RTL_GIGA_MAC_VER_22] =
224 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
225 [RTL_GIGA_MAC_VER_23] =
226 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
227 [RTL_GIGA_MAC_VER_24] =
228 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
229 [RTL_GIGA_MAC_VER_25] =
230 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
232 [RTL_GIGA_MAC_VER_26] =
233 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
235 [RTL_GIGA_MAC_VER_27] =
236 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
237 [RTL_GIGA_MAC_VER_28] =
238 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
239 [RTL_GIGA_MAC_VER_29] =
240 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
242 [RTL_GIGA_MAC_VER_30] =
243 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
245 [RTL_GIGA_MAC_VER_31] =
246 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
247 [RTL_GIGA_MAC_VER_32] =
248 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
250 [RTL_GIGA_MAC_VER_33] =
251 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
253 [RTL_GIGA_MAC_VER_34] =
254 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
256 [RTL_GIGA_MAC_VER_35] =
257 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
259 [RTL_GIGA_MAC_VER_36] =
260 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
262 [RTL_GIGA_MAC_VER_37] =
263 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
265 [RTL_GIGA_MAC_VER_38] =
266 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
268 [RTL_GIGA_MAC_VER_39] =
269 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
271 [RTL_GIGA_MAC_VER_40] =
272 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
274 [RTL_GIGA_MAC_VER_41] =
275 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
276 [RTL_GIGA_MAC_VER_42] =
277 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
279 [RTL_GIGA_MAC_VER_43] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
282 [RTL_GIGA_MAC_VER_44] =
283 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
294 static const struct pci_device_id rtl8169_pci_tbl[] = {
295 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
296 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
297 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
298 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
299 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
300 { PCI_VENDOR_ID_DLINK, 0x4300,
301 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
302 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
303 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
304 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
305 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
306 { PCI_VENDOR_ID_LINKSYS, 0x1032,
307 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
309 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
313 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
315 static int rx_buf_sz = 16383;
322 MAC0 = 0, /* Ethernet hardware address. */
324 MAR0 = 8, /* Multicast filter. */
325 CounterAddrLow = 0x10,
326 CounterAddrHigh = 0x14,
327 TxDescStartAddrLow = 0x20,
328 TxDescStartAddrHigh = 0x24,
329 TxHDescStartAddrLow = 0x28,
330 TxHDescStartAddrHigh = 0x2c,
339 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
340 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
343 #define RX128_INT_EN (1 << 15) /* 8111c and later */
344 #define RX_MULTI_EN (1 << 14) /* 8111c only */
345 #define RXCFG_FIFO_SHIFT 13
346 /* No threshold before first PCI xfer */
347 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
348 #define RX_EARLY_OFF (1 << 11)
349 #define RXCFG_DMA_SHIFT 8
350 /* Unlimited maximum PCI burst. */
351 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
358 #define PME_SIGNAL (1 << 5) /* 8168c and later */
369 RxDescAddrLow = 0xe4,
370 RxDescAddrHigh = 0xe8,
371 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
373 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
375 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
377 #define TxPacketMax (8064 >> 7)
378 #define EarlySize 0x27
381 FuncEventMask = 0xf4,
382 FuncPresetState = 0xf8,
383 FuncForceEvent = 0xfc,
386 enum rtl8110_registers {
392 enum rtl8168_8101_registers {
395 #define CSIAR_FLAG 0x80000000
396 #define CSIAR_WRITE_CMD 0x80000000
397 #define CSIAR_BYTE_ENABLE 0x0f
398 #define CSIAR_BYTE_ENABLE_SHIFT 12
399 #define CSIAR_ADDR_MASK 0x0fff
400 #define CSIAR_FUNC_CARD 0x00000000
401 #define CSIAR_FUNC_SDIO 0x00010000
402 #define CSIAR_FUNC_NIC 0x00020000
403 #define CSIAR_FUNC_NIC2 0x00010000
406 #define EPHYAR_FLAG 0x80000000
407 #define EPHYAR_WRITE_CMD 0x80000000
408 #define EPHYAR_REG_MASK 0x1f
409 #define EPHYAR_REG_SHIFT 16
410 #define EPHYAR_DATA_MASK 0xffff
412 #define PFM_EN (1 << 6)
414 #define FIX_NAK_1 (1 << 4)
415 #define FIX_NAK_2 (1 << 3)
418 #define NOW_IS_OOB (1 << 7)
419 #define TX_EMPTY (1 << 5)
420 #define RX_EMPTY (1 << 4)
421 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
422 #define EN_NDP (1 << 3)
423 #define EN_OOB_RESET (1 << 2)
424 #define LINK_LIST_RDY (1 << 1)
426 #define EFUSEAR_FLAG 0x80000000
427 #define EFUSEAR_WRITE_CMD 0x80000000
428 #define EFUSEAR_READ_CMD 0x00000000
429 #define EFUSEAR_REG_MASK 0x03ff
430 #define EFUSEAR_REG_SHIFT 8
431 #define EFUSEAR_DATA_MASK 0xff
434 enum rtl8168_registers {
439 #define ERIAR_FLAG 0x80000000
440 #define ERIAR_WRITE_CMD 0x80000000
441 #define ERIAR_READ_CMD 0x00000000
442 #define ERIAR_ADDR_BYTE_ALIGN 4
443 #define ERIAR_TYPE_SHIFT 16
444 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
445 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
446 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
447 #define ERIAR_MASK_SHIFT 12
448 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
449 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
450 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
451 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
452 EPHY_RXER_NUM = 0x7c,
453 OCPDR = 0xb0, /* OCP GPHY access */
454 #define OCPDR_WRITE_CMD 0x80000000
455 #define OCPDR_READ_CMD 0x00000000
456 #define OCPDR_REG_MASK 0x7f
457 #define OCPDR_GPHY_REG_SHIFT 16
458 #define OCPDR_DATA_MASK 0xffff
460 #define OCPAR_FLAG 0x80000000
461 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
462 #define OCPAR_GPHY_READ_CMD 0x0000f060
464 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
465 MISC = 0xf0, /* 8168e only. */
466 #define TXPLA_RST (1 << 29)
467 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
468 #define PWM_EN (1 << 22)
469 #define RXDV_GATED_EN (1 << 19)
470 #define EARLY_TALLY_EN (1 << 16)
473 enum rtl_register_content {
474 /* InterruptStatusBits */
478 TxDescUnavail = 0x0080,
502 /* TXPoll register p.5 */
503 HPQ = 0x80, /* Poll cmd on the high prio queue */
504 NPQ = 0x40, /* Poll cmd on the low prio queue */
505 FSWInt = 0x01, /* Forced software interrupt */
509 Cfg9346_Unlock = 0xc0,
514 AcceptBroadcast = 0x08,
515 AcceptMulticast = 0x04,
517 AcceptAllPhys = 0x01,
518 #define RX_CONFIG_ACCEPT_MASK 0x3f
521 TxInterFrameGapShift = 24,
522 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
524 /* Config1 register p.24 */
527 Speed_down = (1 << 4),
531 PMEnable = (1 << 0), /* Power Management Enable */
533 /* Config2 register p. 25 */
534 ClkReqEn = (1 << 7), /* Clock Request Enable */
535 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
536 PCI_Clock_66MHz = 0x01,
537 PCI_Clock_33MHz = 0x00,
539 /* Config3 register p.25 */
540 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
541 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
542 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
543 Rdy_to_L23 = (1 << 1), /* L23 Enable */
544 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
546 /* Config4 register */
547 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
549 /* Config5 register p.27 */
550 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
551 MWF = (1 << 5), /* Accept Multicast wakeup frame */
552 UWF = (1 << 4), /* Accept Unicast wakeup frame */
554 LanWake = (1 << 1), /* LanWake enable/disable */
555 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
556 ASPM_en = (1 << 0), /* ASPM enable */
559 TBIReset = 0x80000000,
560 TBILoopback = 0x40000000,
561 TBINwEnable = 0x20000000,
562 TBINwRestart = 0x10000000,
563 TBILinkOk = 0x02000000,
564 TBINwComplete = 0x01000000,
567 EnableBist = (1 << 15), // 8168 8101
568 Mac_dbgo_oe = (1 << 14), // 8168 8101
569 Normal_mode = (1 << 13), // unused
570 Force_half_dup = (1 << 12), // 8168 8101
571 Force_rxflow_en = (1 << 11), // 8168 8101
572 Force_txflow_en = (1 << 10), // 8168 8101
573 Cxpl_dbg_sel = (1 << 9), // 8168 8101
574 ASF = (1 << 8), // 8168 8101
575 PktCntrDisable = (1 << 7), // 8168 8101
576 Mac_dbgo_sel = 0x001c, // 8168
581 INTT_0 = 0x0000, // 8168
582 INTT_1 = 0x0001, // 8168
583 INTT_2 = 0x0002, // 8168
584 INTT_3 = 0x0003, // 8168
586 /* rtl8169_PHYstatus */
597 TBILinkOK = 0x02000000,
599 /* DumpCounterCommand */
604 /* First doubleword. */
605 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
606 RingEnd = (1 << 30), /* End of descriptor ring */
607 FirstFrag = (1 << 29), /* First segment of a packet */
608 LastFrag = (1 << 28), /* Final segment of a packet */
612 enum rtl_tx_desc_bit {
613 /* First doubleword. */
614 TD_LSO = (1 << 27), /* Large Send Offload */
615 #define TD_MSS_MAX 0x07ffu /* MSS value */
617 /* Second doubleword. */
618 TxVlanTag = (1 << 17), /* Add VLAN tag */
621 /* 8169, 8168b and 810x except 8102e. */
622 enum rtl_tx_desc_bit_0 {
623 /* First doubleword. */
624 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
625 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
626 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
627 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
630 /* 8102e, 8168c and beyond. */
631 enum rtl_tx_desc_bit_1 {
632 /* First doubleword. */
633 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
634 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
635 #define GTTCPHO_SHIFT 18
636 #define GTTCPHO_MAX 0x7fU
638 /* Second doubleword. */
639 #define TCPHO_SHIFT 18
640 #define TCPHO_MAX 0x3ffU
641 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
642 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
643 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
644 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
645 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
648 enum rtl_rx_desc_bit {
650 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
651 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
653 #define RxProtoUDP (PID1)
654 #define RxProtoTCP (PID0)
655 #define RxProtoIP (PID1 | PID0)
656 #define RxProtoMask RxProtoIP
658 IPFail = (1 << 16), /* IP checksum failed */
659 UDPFail = (1 << 15), /* UDP/IP checksum failed */
660 TCPFail = (1 << 14), /* TCP/IP checksum failed */
661 RxVlanTag = (1 << 16), /* VLAN tag available */
664 #define RsvdMask 0x3fffc000
681 u8 __pad[sizeof(void *) - sizeof(u32)];
685 RTL_FEATURE_WOL = (1 << 0),
686 RTL_FEATURE_MSI = (1 << 1),
687 RTL_FEATURE_GMII = (1 << 2),
690 struct rtl8169_counters {
697 __le32 tx_one_collision;
698 __le32 tx_multi_collision;
707 RTL_FLAG_TASK_ENABLED,
708 RTL_FLAG_TASK_SLOW_PENDING,
709 RTL_FLAG_TASK_RESET_PENDING,
710 RTL_FLAG_TASK_PHY_PENDING,
714 struct rtl8169_stats {
717 struct u64_stats_sync syncp;
720 struct rtl8169_private {
721 void __iomem *mmio_addr; /* memory map physical address */
722 struct pci_dev *pci_dev;
723 struct net_device *dev;
724 struct napi_struct napi;
728 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
729 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
731 struct rtl8169_stats rx_stats;
732 struct rtl8169_stats tx_stats;
733 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
734 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
735 dma_addr_t TxPhyAddr;
736 dma_addr_t RxPhyAddr;
737 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
738 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
739 struct timer_list timer;
745 void (*write)(struct rtl8169_private *, int, int);
746 int (*read)(struct rtl8169_private *, int);
749 struct pll_power_ops {
750 void (*down)(struct rtl8169_private *);
751 void (*up)(struct rtl8169_private *);
755 void (*enable)(struct rtl8169_private *);
756 void (*disable)(struct rtl8169_private *);
760 void (*write)(struct rtl8169_private *, int, int);
761 u32 (*read)(struct rtl8169_private *, int);
764 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
765 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
766 void (*phy_reset_enable)(struct rtl8169_private *tp);
767 void (*hw_start)(struct net_device *);
768 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
769 unsigned int (*link_ok)(void __iomem *);
770 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
771 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
774 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
776 struct work_struct work;
781 struct mii_if_info mii;
782 struct rtl8169_counters counters;
787 const struct firmware *fw;
789 #define RTL_VER_SIZE 32
791 char version[RTL_VER_SIZE];
793 struct rtl_fw_phy_action {
798 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
803 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
804 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
805 module_param(use_dac, int, 0);
806 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
807 module_param_named(debug, debug.msg_enable, int, 0);
808 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
809 MODULE_LICENSE("GPL");
810 MODULE_VERSION(RTL8169_VERSION);
811 MODULE_FIRMWARE(FIRMWARE_8168D_1);
812 MODULE_FIRMWARE(FIRMWARE_8168D_2);
813 MODULE_FIRMWARE(FIRMWARE_8168E_1);
814 MODULE_FIRMWARE(FIRMWARE_8168E_2);
815 MODULE_FIRMWARE(FIRMWARE_8168E_3);
816 MODULE_FIRMWARE(FIRMWARE_8105E_1);
817 MODULE_FIRMWARE(FIRMWARE_8168F_1);
818 MODULE_FIRMWARE(FIRMWARE_8168F_2);
819 MODULE_FIRMWARE(FIRMWARE_8402_1);
820 MODULE_FIRMWARE(FIRMWARE_8411_1);
821 MODULE_FIRMWARE(FIRMWARE_8411_2);
822 MODULE_FIRMWARE(FIRMWARE_8106E_1);
823 MODULE_FIRMWARE(FIRMWARE_8106E_2);
824 MODULE_FIRMWARE(FIRMWARE_8168G_2);
825 MODULE_FIRMWARE(FIRMWARE_8168G_3);
827 static void rtl_lock_work(struct rtl8169_private *tp)
829 mutex_lock(&tp->wk.mutex);
832 static void rtl_unlock_work(struct rtl8169_private *tp)
834 mutex_unlock(&tp->wk.mutex);
837 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
839 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
840 PCI_EXP_DEVCTL_READRQ, force);
844 bool (*check)(struct rtl8169_private *);
848 static void rtl_udelay(unsigned int d)
853 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
854 void (*delay)(unsigned int), unsigned int d, int n,
859 for (i = 0; i < n; i++) {
861 if (c->check(tp) == high)
864 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
865 c->msg, !high, n, d);
869 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
870 const struct rtl_cond *c,
871 unsigned int d, int n)
873 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
876 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
877 const struct rtl_cond *c,
878 unsigned int d, int n)
880 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
883 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
884 const struct rtl_cond *c,
885 unsigned int d, int n)
887 return rtl_loop_wait(tp, c, msleep, d, n, true);
890 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
891 const struct rtl_cond *c,
892 unsigned int d, int n)
894 return rtl_loop_wait(tp, c, msleep, d, n, false);
897 #define DECLARE_RTL_COND(name) \
898 static bool name ## _check(struct rtl8169_private *); \
900 static const struct rtl_cond name = { \
901 .check = name ## _check, \
905 static bool name ## _check(struct rtl8169_private *tp)
907 DECLARE_RTL_COND(rtl_ocpar_cond)
909 void __iomem *ioaddr = tp->mmio_addr;
911 return RTL_R32(OCPAR) & OCPAR_FLAG;
914 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
916 void __iomem *ioaddr = tp->mmio_addr;
918 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
920 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
924 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
926 void __iomem *ioaddr = tp->mmio_addr;
928 RTL_W32(OCPDR, data);
929 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
931 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
934 DECLARE_RTL_COND(rtl_eriar_cond)
936 void __iomem *ioaddr = tp->mmio_addr;
938 return RTL_R32(ERIAR) & ERIAR_FLAG;
941 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
943 void __iomem *ioaddr = tp->mmio_addr;
946 RTL_W32(ERIAR, 0x800010e8);
949 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
952 ocp_write(tp, 0x1, 0x30, 0x00000001);
955 #define OOB_CMD_RESET 0x00
956 #define OOB_CMD_DRIVER_START 0x05
957 #define OOB_CMD_DRIVER_STOP 0x06
959 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
961 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
964 DECLARE_RTL_COND(rtl_ocp_read_cond)
968 reg = rtl8168_get_ocp_reg(tp);
970 return ocp_read(tp, 0x0f, reg) & 0x00000800;
973 static void rtl8168_driver_start(struct rtl8169_private *tp)
975 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
977 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
980 static void rtl8168_driver_stop(struct rtl8169_private *tp)
982 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
984 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
987 static int r8168dp_check_dash(struct rtl8169_private *tp)
989 u16 reg = rtl8168_get_ocp_reg(tp);
991 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
994 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
996 if (reg & 0xffff0001) {
997 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
1003 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
1005 void __iomem *ioaddr = tp->mmio_addr;
1007 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1010 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1012 void __iomem *ioaddr = tp->mmio_addr;
1014 if (rtl_ocp_reg_failure(tp, reg))
1017 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1019 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1022 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1024 void __iomem *ioaddr = tp->mmio_addr;
1026 if (rtl_ocp_reg_failure(tp, reg))
1029 RTL_W32(GPHY_OCP, reg << 15);
1031 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1032 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1035 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1037 void __iomem *ioaddr = tp->mmio_addr;
1039 if (rtl_ocp_reg_failure(tp, reg))
1042 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
1045 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1047 void __iomem *ioaddr = tp->mmio_addr;
1049 if (rtl_ocp_reg_failure(tp, reg))
1052 RTL_W32(OCPDR, reg << 15);
1054 return RTL_R32(OCPDR);
1057 #define OCP_STD_PHY_BASE 0xa400
1059 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1062 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1066 if (tp->ocp_base != OCP_STD_PHY_BASE)
1069 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1072 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1074 if (tp->ocp_base != OCP_STD_PHY_BASE)
1077 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1080 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1083 tp->ocp_base = value << 4;
1087 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1090 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1092 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1095 DECLARE_RTL_COND(rtl_phyar_cond)
1097 void __iomem *ioaddr = tp->mmio_addr;
1099 return RTL_R32(PHYAR) & 0x80000000;
1102 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1104 void __iomem *ioaddr = tp->mmio_addr;
1106 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1108 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1110 * According to hardware specs a 20us delay is required after write
1111 * complete indication, but before sending next command.
1116 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1118 void __iomem *ioaddr = tp->mmio_addr;
1121 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
1123 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1124 RTL_R32(PHYAR) & 0xffff : ~0;
1127 * According to hardware specs a 20us delay is required after read
1128 * complete indication, but before sending next command.
1135 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1137 void __iomem *ioaddr = tp->mmio_addr;
1139 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1140 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1141 RTL_W32(EPHY_RXER_NUM, 0);
1143 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1146 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1148 r8168dp_1_mdio_access(tp, reg,
1149 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1152 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1154 void __iomem *ioaddr = tp->mmio_addr;
1156 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1159 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1160 RTL_W32(EPHY_RXER_NUM, 0);
1162 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1163 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
1166 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1168 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1170 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1173 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1175 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1178 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1180 void __iomem *ioaddr = tp->mmio_addr;
1182 r8168dp_2_mdio_start(ioaddr);
1184 r8169_mdio_write(tp, reg, value);
1186 r8168dp_2_mdio_stop(ioaddr);
1189 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1191 void __iomem *ioaddr = tp->mmio_addr;
1194 r8168dp_2_mdio_start(ioaddr);
1196 value = r8169_mdio_read(tp, reg);
1198 r8168dp_2_mdio_stop(ioaddr);
1203 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1205 tp->mdio_ops.write(tp, location, val);
1208 static int rtl_readphy(struct rtl8169_private *tp, int location)
1210 return tp->mdio_ops.read(tp, location);
1213 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1215 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1218 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1222 val = rtl_readphy(tp, reg_addr);
1223 rtl_writephy(tp, reg_addr, (val | p) & ~m);
1226 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1229 struct rtl8169_private *tp = netdev_priv(dev);
1231 rtl_writephy(tp, location, val);
1234 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1236 struct rtl8169_private *tp = netdev_priv(dev);
1238 return rtl_readphy(tp, location);
1241 DECLARE_RTL_COND(rtl_ephyar_cond)
1243 void __iomem *ioaddr = tp->mmio_addr;
1245 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1248 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1250 void __iomem *ioaddr = tp->mmio_addr;
1252 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1253 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1255 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1260 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1262 void __iomem *ioaddr = tp->mmio_addr;
1264 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1266 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1267 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
1270 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1273 void __iomem *ioaddr = tp->mmio_addr;
1275 BUG_ON((addr & 3) || (mask == 0));
1276 RTL_W32(ERIDR, val);
1277 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1279 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1282 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1284 void __iomem *ioaddr = tp->mmio_addr;
1286 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1288 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1289 RTL_R32(ERIDR) : ~0;
1292 static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1297 val = rtl_eri_read(tp, addr, type);
1298 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1307 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1308 const struct exgmac_reg *r, int len)
1311 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1316 DECLARE_RTL_COND(rtl_efusear_cond)
1318 void __iomem *ioaddr = tp->mmio_addr;
1320 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1323 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1325 void __iomem *ioaddr = tp->mmio_addr;
1327 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1329 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1330 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1333 static u16 rtl_get_events(struct rtl8169_private *tp)
1335 void __iomem *ioaddr = tp->mmio_addr;
1337 return RTL_R16(IntrStatus);
1340 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1342 void __iomem *ioaddr = tp->mmio_addr;
1344 RTL_W16(IntrStatus, bits);
1348 static void rtl_irq_disable(struct rtl8169_private *tp)
1350 void __iomem *ioaddr = tp->mmio_addr;
1352 RTL_W16(IntrMask, 0);
1356 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1358 void __iomem *ioaddr = tp->mmio_addr;
1360 RTL_W16(IntrMask, bits);
1363 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1364 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1365 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1367 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1369 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1372 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1374 void __iomem *ioaddr = tp->mmio_addr;
1376 rtl_irq_disable(tp);
1377 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1381 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1383 void __iomem *ioaddr = tp->mmio_addr;
1385 return RTL_R32(TBICSR) & TBIReset;
1388 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1390 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1393 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1395 return RTL_R32(TBICSR) & TBILinkOk;
1398 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1400 return RTL_R8(PHYstatus) & LinkStatus;
1403 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1405 void __iomem *ioaddr = tp->mmio_addr;
1407 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1410 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1414 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1415 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1418 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1420 void __iomem *ioaddr = tp->mmio_addr;
1421 struct net_device *dev = tp->dev;
1423 if (!netif_running(dev))
1426 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1427 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1428 if (RTL_R8(PHYstatus) & _1000bpsF) {
1429 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1431 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1433 } else if (RTL_R8(PHYstatus) & _100bps) {
1434 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1436 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1439 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1441 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1444 /* Reset packet filter */
1445 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1447 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1449 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1450 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1451 if (RTL_R8(PHYstatus) & _1000bpsF) {
1452 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1454 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1457 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1459 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1462 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1463 if (RTL_R8(PHYstatus) & _10bps) {
1464 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1466 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1469 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1475 static void __rtl8169_check_link_status(struct net_device *dev,
1476 struct rtl8169_private *tp,
1477 void __iomem *ioaddr, bool pm)
1479 if (tp->link_ok(ioaddr)) {
1480 rtl_link_chg_patch(tp);
1481 /* This is to cancel a scheduled suspend if there's one. */
1483 pm_request_resume(&tp->pci_dev->dev);
1484 netif_carrier_on(dev);
1485 if (net_ratelimit())
1486 netif_info(tp, ifup, dev, "link up\n");
1488 netif_carrier_off(dev);
1489 netif_info(tp, ifdown, dev, "link down\n");
1491 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1495 static void rtl8169_check_link_status(struct net_device *dev,
1496 struct rtl8169_private *tp,
1497 void __iomem *ioaddr)
1499 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1502 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1504 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1506 void __iomem *ioaddr = tp->mmio_addr;
1510 options = RTL_R8(Config1);
1511 if (!(options & PMEnable))
1514 options = RTL_R8(Config3);
1515 if (options & LinkUp)
1516 wolopts |= WAKE_PHY;
1517 if (options & MagicPacket)
1518 wolopts |= WAKE_MAGIC;
1520 options = RTL_R8(Config5);
1522 wolopts |= WAKE_UCAST;
1524 wolopts |= WAKE_BCAST;
1526 wolopts |= WAKE_MCAST;
1531 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1533 struct rtl8169_private *tp = netdev_priv(dev);
1537 wol->supported = WAKE_ANY;
1538 wol->wolopts = __rtl8169_get_wol(tp);
1540 rtl_unlock_work(tp);
1543 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1545 void __iomem *ioaddr = tp->mmio_addr;
1547 static const struct {
1552 { WAKE_PHY, Config3, LinkUp },
1553 { WAKE_MAGIC, Config3, MagicPacket },
1554 { WAKE_UCAST, Config5, UWF },
1555 { WAKE_BCAST, Config5, BWF },
1556 { WAKE_MCAST, Config5, MWF },
1557 { WAKE_ANY, Config5, LanWake }
1561 RTL_W8(Cfg9346, Cfg9346_Unlock);
1563 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1564 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1565 if (wolopts & cfg[i].opt)
1566 options |= cfg[i].mask;
1567 RTL_W8(cfg[i].reg, options);
1570 switch (tp->mac_version) {
1571 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1572 options = RTL_R8(Config1) & ~PMEnable;
1574 options |= PMEnable;
1575 RTL_W8(Config1, options);
1578 options = RTL_R8(Config2) & ~PME_SIGNAL;
1580 options |= PME_SIGNAL;
1581 RTL_W8(Config2, options);
1585 RTL_W8(Cfg9346, Cfg9346_Lock);
1588 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1590 struct rtl8169_private *tp = netdev_priv(dev);
1595 tp->features |= RTL_FEATURE_WOL;
1597 tp->features &= ~RTL_FEATURE_WOL;
1598 __rtl8169_set_wol(tp, wol->wolopts);
1600 rtl_unlock_work(tp);
1602 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1607 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1609 return rtl_chip_infos[tp->mac_version].fw_name;
1612 static void rtl8169_get_drvinfo(struct net_device *dev,
1613 struct ethtool_drvinfo *info)
1615 struct rtl8169_private *tp = netdev_priv(dev);
1616 struct rtl_fw *rtl_fw = tp->rtl_fw;
1618 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1619 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1620 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1621 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1622 if (!IS_ERR_OR_NULL(rtl_fw))
1623 strlcpy(info->fw_version, rtl_fw->version,
1624 sizeof(info->fw_version));
1627 static int rtl8169_get_regs_len(struct net_device *dev)
1629 return R8169_REGS_SIZE;
1632 static int rtl8169_set_speed_tbi(struct net_device *dev,
1633 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1635 struct rtl8169_private *tp = netdev_priv(dev);
1636 void __iomem *ioaddr = tp->mmio_addr;
1640 reg = RTL_R32(TBICSR);
1641 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1642 (duplex == DUPLEX_FULL)) {
1643 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1644 } else if (autoneg == AUTONEG_ENABLE)
1645 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1647 netif_warn(tp, link, dev,
1648 "incorrect speed setting refused in TBI mode\n");
1655 static int rtl8169_set_speed_xmii(struct net_device *dev,
1656 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1658 struct rtl8169_private *tp = netdev_priv(dev);
1659 int giga_ctrl, bmcr;
1662 rtl_writephy(tp, 0x1f, 0x0000);
1664 if (autoneg == AUTONEG_ENABLE) {
1667 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1668 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1669 ADVERTISE_100HALF | ADVERTISE_100FULL);
1671 if (adv & ADVERTISED_10baseT_Half)
1672 auto_nego |= ADVERTISE_10HALF;
1673 if (adv & ADVERTISED_10baseT_Full)
1674 auto_nego |= ADVERTISE_10FULL;
1675 if (adv & ADVERTISED_100baseT_Half)
1676 auto_nego |= ADVERTISE_100HALF;
1677 if (adv & ADVERTISED_100baseT_Full)
1678 auto_nego |= ADVERTISE_100FULL;
1680 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1682 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1683 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1685 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1686 if (tp->mii.supports_gmii) {
1687 if (adv & ADVERTISED_1000baseT_Half)
1688 giga_ctrl |= ADVERTISE_1000HALF;
1689 if (adv & ADVERTISED_1000baseT_Full)
1690 giga_ctrl |= ADVERTISE_1000FULL;
1691 } else if (adv & (ADVERTISED_1000baseT_Half |
1692 ADVERTISED_1000baseT_Full)) {
1693 netif_info(tp, link, dev,
1694 "PHY does not support 1000Mbps\n");
1698 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1700 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1701 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1705 if (speed == SPEED_10)
1707 else if (speed == SPEED_100)
1708 bmcr = BMCR_SPEED100;
1712 if (duplex == DUPLEX_FULL)
1713 bmcr |= BMCR_FULLDPLX;
1716 rtl_writephy(tp, MII_BMCR, bmcr);
1718 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1719 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1720 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1721 rtl_writephy(tp, 0x17, 0x2138);
1722 rtl_writephy(tp, 0x0e, 0x0260);
1724 rtl_writephy(tp, 0x17, 0x2108);
1725 rtl_writephy(tp, 0x0e, 0x0000);
1734 static int rtl8169_set_speed(struct net_device *dev,
1735 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1737 struct rtl8169_private *tp = netdev_priv(dev);
1740 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1744 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1745 (advertising & ADVERTISED_1000baseT_Full)) {
1746 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1752 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1754 struct rtl8169_private *tp = netdev_priv(dev);
1757 del_timer_sync(&tp->timer);
1760 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1761 cmd->duplex, cmd->advertising);
1762 rtl_unlock_work(tp);
1767 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1768 netdev_features_t features)
1770 struct rtl8169_private *tp = netdev_priv(dev);
1772 if (dev->mtu > TD_MSS_MAX)
1773 features &= ~NETIF_F_ALL_TSO;
1775 if (dev->mtu > JUMBO_1K &&
1776 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1777 features &= ~NETIF_F_IP_CSUM;
1782 static void __rtl8169_set_features(struct net_device *dev,
1783 netdev_features_t features)
1785 struct rtl8169_private *tp = netdev_priv(dev);
1786 void __iomem *ioaddr = tp->mmio_addr;
1789 rx_config = RTL_R32(RxConfig);
1790 if (features & NETIF_F_RXALL)
1791 rx_config |= (AcceptErr | AcceptRunt);
1793 rx_config &= ~(AcceptErr | AcceptRunt);
1795 RTL_W32(RxConfig, rx_config);
1797 if (features & NETIF_F_RXCSUM)
1798 tp->cp_cmd |= RxChkSum;
1800 tp->cp_cmd &= ~RxChkSum;
1802 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1803 tp->cp_cmd |= RxVlan;
1805 tp->cp_cmd &= ~RxVlan;
1807 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
1809 RTL_W16(CPlusCmd, tp->cp_cmd);
1813 static int rtl8169_set_features(struct net_device *dev,
1814 netdev_features_t features)
1816 struct rtl8169_private *tp = netdev_priv(dev);
1818 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
1821 if (features ^ dev->features);
1822 __rtl8169_set_features(dev, features);
1823 rtl_unlock_work(tp);
1829 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1831 return (vlan_tx_tag_present(skb)) ?
1832 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1835 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1837 u32 opts2 = le32_to_cpu(desc->opts2);
1839 if (opts2 & RxVlanTag)
1840 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1843 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1845 struct rtl8169_private *tp = netdev_priv(dev);
1846 void __iomem *ioaddr = tp->mmio_addr;
1850 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1851 cmd->port = PORT_FIBRE;
1852 cmd->transceiver = XCVR_INTERNAL;
1854 status = RTL_R32(TBICSR);
1855 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1856 cmd->autoneg = !!(status & TBINwEnable);
1858 ethtool_cmd_speed_set(cmd, SPEED_1000);
1859 cmd->duplex = DUPLEX_FULL; /* Always set */
1864 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1866 struct rtl8169_private *tp = netdev_priv(dev);
1868 return mii_ethtool_gset(&tp->mii, cmd);
1871 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1873 struct rtl8169_private *tp = netdev_priv(dev);
1877 rc = tp->get_settings(dev, cmd);
1878 rtl_unlock_work(tp);
1883 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1886 struct rtl8169_private *tp = netdev_priv(dev);
1887 u32 __iomem *data = tp->mmio_addr;
1892 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1893 memcpy_fromio(dw++, data++, 4);
1894 rtl_unlock_work(tp);
1897 static u32 rtl8169_get_msglevel(struct net_device *dev)
1899 struct rtl8169_private *tp = netdev_priv(dev);
1901 return tp->msg_enable;
1904 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1906 struct rtl8169_private *tp = netdev_priv(dev);
1908 tp->msg_enable = value;
1911 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1918 "tx_single_collisions",
1919 "tx_multi_collisions",
1927 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1931 return ARRAY_SIZE(rtl8169_gstrings);
1937 DECLARE_RTL_COND(rtl_counters_cond)
1939 void __iomem *ioaddr = tp->mmio_addr;
1941 return RTL_R32(CounterAddrLow) & CounterDump;
1944 static void rtl8169_update_counters(struct net_device *dev)
1946 struct rtl8169_private *tp = netdev_priv(dev);
1947 void __iomem *ioaddr = tp->mmio_addr;
1948 struct device *d = &tp->pci_dev->dev;
1949 struct rtl8169_counters *counters;
1954 * Some chips are unable to dump tally counters when the receiver
1957 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1960 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1964 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1965 cmd = (u64)paddr & DMA_BIT_MASK(32);
1966 RTL_W32(CounterAddrLow, cmd);
1967 RTL_W32(CounterAddrLow, cmd | CounterDump);
1969 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1970 memcpy(&tp->counters, counters, sizeof(*counters));
1972 RTL_W32(CounterAddrLow, 0);
1973 RTL_W32(CounterAddrHigh, 0);
1975 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1978 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1979 struct ethtool_stats *stats, u64 *data)
1981 struct rtl8169_private *tp = netdev_priv(dev);
1985 rtl8169_update_counters(dev);
1987 data[0] = le64_to_cpu(tp->counters.tx_packets);
1988 data[1] = le64_to_cpu(tp->counters.rx_packets);
1989 data[2] = le64_to_cpu(tp->counters.tx_errors);
1990 data[3] = le32_to_cpu(tp->counters.rx_errors);
1991 data[4] = le16_to_cpu(tp->counters.rx_missed);
1992 data[5] = le16_to_cpu(tp->counters.align_errors);
1993 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1994 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1995 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1996 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1997 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1998 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1999 data[12] = le16_to_cpu(tp->counters.tx_underun);
2002 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2006 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2011 static const struct ethtool_ops rtl8169_ethtool_ops = {
2012 .get_drvinfo = rtl8169_get_drvinfo,
2013 .get_regs_len = rtl8169_get_regs_len,
2014 .get_link = ethtool_op_get_link,
2015 .get_settings = rtl8169_get_settings,
2016 .set_settings = rtl8169_set_settings,
2017 .get_msglevel = rtl8169_get_msglevel,
2018 .set_msglevel = rtl8169_set_msglevel,
2019 .get_regs = rtl8169_get_regs,
2020 .get_wol = rtl8169_get_wol,
2021 .set_wol = rtl8169_set_wol,
2022 .get_strings = rtl8169_get_strings,
2023 .get_sset_count = rtl8169_get_sset_count,
2024 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2025 .get_ts_info = ethtool_op_get_ts_info,
2028 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2029 struct net_device *dev, u8 default_version)
2031 void __iomem *ioaddr = tp->mmio_addr;
2033 * The driver currently handles the 8168Bf and the 8168Be identically
2034 * but they can be identified more specifically through the test below
2037 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2039 * Same thing for the 8101Eb and the 8101Ec:
2041 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2043 static const struct rtl_mac_info {
2049 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2050 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2051 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2052 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2055 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2056 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2057 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2060 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2061 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2062 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2063 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2066 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2067 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2068 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2070 /* 8168DP family. */
2071 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2072 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2073 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2076 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
2077 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2078 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2079 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2080 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2081 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2082 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2083 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
2084 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2087 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2088 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2089 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2090 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2093 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2094 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2095 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2096 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
2097 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2098 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2099 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2100 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2101 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2102 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2103 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2104 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2105 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2106 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2107 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2108 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2109 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2110 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2111 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2112 /* FIXME: where did these entries come from ? -- FR */
2113 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2114 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2117 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2118 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2119 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2120 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2121 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2122 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2125 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2127 const struct rtl_mac_info *p = mac_info;
2130 reg = RTL_R32(TxConfig);
2131 while ((reg & p->mask) != p->val)
2133 tp->mac_version = p->mac_version;
2135 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2136 netif_notice(tp, probe, dev,
2137 "unknown MAC, using family default\n");
2138 tp->mac_version = default_version;
2139 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2140 tp->mac_version = tp->mii.supports_gmii ?
2141 RTL_GIGA_MAC_VER_42 :
2142 RTL_GIGA_MAC_VER_43;
2146 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2148 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2156 static void rtl_writephy_batch(struct rtl8169_private *tp,
2157 const struct phy_reg *regs, int len)
2160 rtl_writephy(tp, regs->reg, regs->val);
2165 #define PHY_READ 0x00000000
2166 #define PHY_DATA_OR 0x10000000
2167 #define PHY_DATA_AND 0x20000000
2168 #define PHY_BJMPN 0x30000000
2169 #define PHY_MDIO_CHG 0x40000000
2170 #define PHY_CLEAR_READCOUNT 0x70000000
2171 #define PHY_WRITE 0x80000000
2172 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2173 #define PHY_COMP_EQ_SKIPN 0xa0000000
2174 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2175 #define PHY_WRITE_PREVIOUS 0xc0000000
2176 #define PHY_SKIPN 0xd0000000
2177 #define PHY_DELAY_MS 0xe0000000
2181 char version[RTL_VER_SIZE];
2187 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2189 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2191 const struct firmware *fw = rtl_fw->fw;
2192 struct fw_info *fw_info = (struct fw_info *)fw->data;
2193 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2194 char *version = rtl_fw->version;
2197 if (fw->size < FW_OPCODE_SIZE)
2200 if (!fw_info->magic) {
2201 size_t i, size, start;
2204 if (fw->size < sizeof(*fw_info))
2207 for (i = 0; i < fw->size; i++)
2208 checksum += fw->data[i];
2212 start = le32_to_cpu(fw_info->fw_start);
2213 if (start > fw->size)
2216 size = le32_to_cpu(fw_info->fw_len);
2217 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2220 memcpy(version, fw_info->version, RTL_VER_SIZE);
2222 pa->code = (__le32 *)(fw->data + start);
2225 if (fw->size % FW_OPCODE_SIZE)
2228 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2230 pa->code = (__le32 *)fw->data;
2231 pa->size = fw->size / FW_OPCODE_SIZE;
2233 version[RTL_VER_SIZE - 1] = 0;
2240 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2241 struct rtl_fw_phy_action *pa)
2246 for (index = 0; index < pa->size; index++) {
2247 u32 action = le32_to_cpu(pa->code[index]);
2248 u32 regno = (action & 0x0fff0000) >> 16;
2250 switch(action & 0xf0000000) {
2255 case PHY_CLEAR_READCOUNT:
2257 case PHY_WRITE_PREVIOUS:
2262 if (regno > index) {
2263 netif_err(tp, ifup, tp->dev,
2264 "Out of range of firmware\n");
2268 case PHY_READCOUNT_EQ_SKIP:
2269 if (index + 2 >= pa->size) {
2270 netif_err(tp, ifup, tp->dev,
2271 "Out of range of firmware\n");
2275 case PHY_COMP_EQ_SKIPN:
2276 case PHY_COMP_NEQ_SKIPN:
2278 if (index + 1 + regno >= pa->size) {
2279 netif_err(tp, ifup, tp->dev,
2280 "Out of range of firmware\n");
2286 netif_err(tp, ifup, tp->dev,
2287 "Invalid action 0x%08x\n", action);
2296 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2298 struct net_device *dev = tp->dev;
2301 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2302 netif_err(tp, ifup, dev, "invalid firwmare\n");
2306 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2312 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2314 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2315 struct mdio_ops org, *ops = &tp->mdio_ops;
2319 predata = count = 0;
2320 org.write = ops->write;
2321 org.read = ops->read;
2323 for (index = 0; index < pa->size; ) {
2324 u32 action = le32_to_cpu(pa->code[index]);
2325 u32 data = action & 0x0000ffff;
2326 u32 regno = (action & 0x0fff0000) >> 16;
2331 switch(action & 0xf0000000) {
2333 predata = rtl_readphy(tp, regno);
2350 ops->write = org.write;
2351 ops->read = org.read;
2352 } else if (data == 1) {
2353 ops->write = mac_mcu_write;
2354 ops->read = mac_mcu_read;
2359 case PHY_CLEAR_READCOUNT:
2364 rtl_writephy(tp, regno, data);
2367 case PHY_READCOUNT_EQ_SKIP:
2368 index += (count == data) ? 2 : 1;
2370 case PHY_COMP_EQ_SKIPN:
2371 if (predata == data)
2375 case PHY_COMP_NEQ_SKIPN:
2376 if (predata != data)
2380 case PHY_WRITE_PREVIOUS:
2381 rtl_writephy(tp, regno, predata);
2397 ops->write = org.write;
2398 ops->read = org.read;
2401 static void rtl_release_firmware(struct rtl8169_private *tp)
2403 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2404 release_firmware(tp->rtl_fw->fw);
2407 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2410 static void rtl_apply_firmware(struct rtl8169_private *tp)
2412 struct rtl_fw *rtl_fw = tp->rtl_fw;
2414 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2415 if (!IS_ERR_OR_NULL(rtl_fw))
2416 rtl_phy_write_fw(tp, rtl_fw);
2419 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2421 if (rtl_readphy(tp, reg) != val)
2422 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2424 rtl_apply_firmware(tp);
2427 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2429 static const struct phy_reg phy_reg_init[] = {
2491 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2494 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2496 static const struct phy_reg phy_reg_init[] = {
2502 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2505 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2507 struct pci_dev *pdev = tp->pci_dev;
2509 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2510 (pdev->subsystem_device != 0xe000))
2513 rtl_writephy(tp, 0x1f, 0x0001);
2514 rtl_writephy(tp, 0x10, 0xf01b);
2515 rtl_writephy(tp, 0x1f, 0x0000);
2518 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2520 static const struct phy_reg phy_reg_init[] = {
2560 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2562 rtl8169scd_hw_phy_config_quirk(tp);
2565 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2567 static const struct phy_reg phy_reg_init[] = {
2615 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2618 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2620 static const struct phy_reg phy_reg_init[] = {
2625 rtl_writephy(tp, 0x1f, 0x0001);
2626 rtl_patchphy(tp, 0x16, 1 << 0);
2628 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2631 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2633 static const struct phy_reg phy_reg_init[] = {
2639 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2642 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2644 static const struct phy_reg phy_reg_init[] = {
2652 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2655 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2657 static const struct phy_reg phy_reg_init[] = {
2663 rtl_writephy(tp, 0x1f, 0x0000);
2664 rtl_patchphy(tp, 0x14, 1 << 5);
2665 rtl_patchphy(tp, 0x0d, 1 << 5);
2667 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2670 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2672 static const struct phy_reg phy_reg_init[] = {
2692 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2694 rtl_patchphy(tp, 0x14, 1 << 5);
2695 rtl_patchphy(tp, 0x0d, 1 << 5);
2696 rtl_writephy(tp, 0x1f, 0x0000);
2699 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2701 static const struct phy_reg phy_reg_init[] = {
2719 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2721 rtl_patchphy(tp, 0x16, 1 << 0);
2722 rtl_patchphy(tp, 0x14, 1 << 5);
2723 rtl_patchphy(tp, 0x0d, 1 << 5);
2724 rtl_writephy(tp, 0x1f, 0x0000);
2727 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2729 static const struct phy_reg phy_reg_init[] = {
2741 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2743 rtl_patchphy(tp, 0x16, 1 << 0);
2744 rtl_patchphy(tp, 0x14, 1 << 5);
2745 rtl_patchphy(tp, 0x0d, 1 << 5);
2746 rtl_writephy(tp, 0x1f, 0x0000);
2749 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2751 rtl8168c_3_hw_phy_config(tp);
2754 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2756 static const struct phy_reg phy_reg_init_0[] = {
2757 /* Channel Estimation */
2778 * Enhance line driver power
2787 * Can not link to 1Gbps with bad cable
2788 * Decrease SNR threshold form 21.07dB to 19.04dB
2797 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2801 * Fine Tune Switching regulator parameter
2803 rtl_writephy(tp, 0x1f, 0x0002);
2804 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2805 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2807 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2808 static const struct phy_reg phy_reg_init[] = {
2818 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2820 val = rtl_readphy(tp, 0x0d);
2822 if ((val & 0x00ff) != 0x006c) {
2823 static const u32 set[] = {
2824 0x0065, 0x0066, 0x0067, 0x0068,
2825 0x0069, 0x006a, 0x006b, 0x006c
2829 rtl_writephy(tp, 0x1f, 0x0002);
2832 for (i = 0; i < ARRAY_SIZE(set); i++)
2833 rtl_writephy(tp, 0x0d, val | set[i]);
2836 static const struct phy_reg phy_reg_init[] = {
2844 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2847 /* RSET couple improve */
2848 rtl_writephy(tp, 0x1f, 0x0002);
2849 rtl_patchphy(tp, 0x0d, 0x0300);
2850 rtl_patchphy(tp, 0x0f, 0x0010);
2852 /* Fine tune PLL performance */
2853 rtl_writephy(tp, 0x1f, 0x0002);
2854 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2855 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2857 rtl_writephy(tp, 0x1f, 0x0005);
2858 rtl_writephy(tp, 0x05, 0x001b);
2860 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2862 rtl_writephy(tp, 0x1f, 0x0000);
2865 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2867 static const struct phy_reg phy_reg_init_0[] = {
2868 /* Channel Estimation */
2889 * Enhance line driver power
2898 * Can not link to 1Gbps with bad cable
2899 * Decrease SNR threshold form 21.07dB to 19.04dB
2908 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2910 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2911 static const struct phy_reg phy_reg_init[] = {
2922 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2924 val = rtl_readphy(tp, 0x0d);
2925 if ((val & 0x00ff) != 0x006c) {
2926 static const u32 set[] = {
2927 0x0065, 0x0066, 0x0067, 0x0068,
2928 0x0069, 0x006a, 0x006b, 0x006c
2932 rtl_writephy(tp, 0x1f, 0x0002);
2935 for (i = 0; i < ARRAY_SIZE(set); i++)
2936 rtl_writephy(tp, 0x0d, val | set[i]);
2939 static const struct phy_reg phy_reg_init[] = {
2947 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2950 /* Fine tune PLL performance */
2951 rtl_writephy(tp, 0x1f, 0x0002);
2952 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2953 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2955 /* Switching regulator Slew rate */
2956 rtl_writephy(tp, 0x1f, 0x0002);
2957 rtl_patchphy(tp, 0x0f, 0x0017);
2959 rtl_writephy(tp, 0x1f, 0x0005);
2960 rtl_writephy(tp, 0x05, 0x001b);
2962 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2964 rtl_writephy(tp, 0x1f, 0x0000);
2967 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2969 static const struct phy_reg phy_reg_init[] = {
3025 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3028 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3030 static const struct phy_reg phy_reg_init[] = {
3040 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3041 rtl_patchphy(tp, 0x0d, 1 << 5);
3044 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3046 static const struct phy_reg phy_reg_init[] = {
3047 /* Enable Delay cap */
3053 /* Channel estimation fine tune */
3062 /* Update PFM & 10M TX idle timer */
3074 rtl_apply_firmware(tp);
3076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3078 /* DCO enable for 10M IDLE Power */
3079 rtl_writephy(tp, 0x1f, 0x0007);
3080 rtl_writephy(tp, 0x1e, 0x0023);
3081 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3084 /* For impedance matching */
3085 rtl_writephy(tp, 0x1f, 0x0002);
3086 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
3087 rtl_writephy(tp, 0x1f, 0x0000);
3089 /* PHY auto speed down */
3090 rtl_writephy(tp, 0x1f, 0x0007);
3091 rtl_writephy(tp, 0x1e, 0x002d);
3092 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3093 rtl_writephy(tp, 0x1f, 0x0000);
3094 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3096 rtl_writephy(tp, 0x1f, 0x0005);
3097 rtl_writephy(tp, 0x05, 0x8b86);
3098 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3099 rtl_writephy(tp, 0x1f, 0x0000);
3101 rtl_writephy(tp, 0x1f, 0x0005);
3102 rtl_writephy(tp, 0x05, 0x8b85);
3103 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3104 rtl_writephy(tp, 0x1f, 0x0007);
3105 rtl_writephy(tp, 0x1e, 0x0020);
3106 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3107 rtl_writephy(tp, 0x1f, 0x0006);
3108 rtl_writephy(tp, 0x00, 0x5a00);
3109 rtl_writephy(tp, 0x1f, 0x0000);
3110 rtl_writephy(tp, 0x0d, 0x0007);
3111 rtl_writephy(tp, 0x0e, 0x003c);
3112 rtl_writephy(tp, 0x0d, 0x4007);
3113 rtl_writephy(tp, 0x0e, 0x0000);
3114 rtl_writephy(tp, 0x0d, 0x0000);
3117 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3120 addr[0] | (addr[1] << 8),
3121 addr[2] | (addr[3] << 8),
3122 addr[4] | (addr[5] << 8)
3124 const struct exgmac_reg e[] = {
3125 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3126 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3127 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3128 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3131 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3134 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3136 static const struct phy_reg phy_reg_init[] = {
3137 /* Enable Delay cap */
3146 /* Channel estimation fine tune */
3163 rtl_apply_firmware(tp);
3165 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3167 /* For 4-corner performance improve */
3168 rtl_writephy(tp, 0x1f, 0x0005);
3169 rtl_writephy(tp, 0x05, 0x8b80);
3170 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3171 rtl_writephy(tp, 0x1f, 0x0000);
3173 /* PHY auto speed down */
3174 rtl_writephy(tp, 0x1f, 0x0004);
3175 rtl_writephy(tp, 0x1f, 0x0007);
3176 rtl_writephy(tp, 0x1e, 0x002d);
3177 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3178 rtl_writephy(tp, 0x1f, 0x0002);
3179 rtl_writephy(tp, 0x1f, 0x0000);
3180 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3182 /* improve 10M EEE waveform */
3183 rtl_writephy(tp, 0x1f, 0x0005);
3184 rtl_writephy(tp, 0x05, 0x8b86);
3185 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3186 rtl_writephy(tp, 0x1f, 0x0000);
3188 /* Improve 2-pair detection performance */
3189 rtl_writephy(tp, 0x1f, 0x0005);
3190 rtl_writephy(tp, 0x05, 0x8b85);
3191 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3192 rtl_writephy(tp, 0x1f, 0x0000);
3195 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
3196 rtl_writephy(tp, 0x1f, 0x0005);
3197 rtl_writephy(tp, 0x05, 0x8b85);
3198 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3199 rtl_writephy(tp, 0x1f, 0x0004);
3200 rtl_writephy(tp, 0x1f, 0x0007);
3201 rtl_writephy(tp, 0x1e, 0x0020);
3202 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3203 rtl_writephy(tp, 0x1f, 0x0002);
3204 rtl_writephy(tp, 0x1f, 0x0000);
3205 rtl_writephy(tp, 0x0d, 0x0007);
3206 rtl_writephy(tp, 0x0e, 0x003c);
3207 rtl_writephy(tp, 0x0d, 0x4007);
3208 rtl_writephy(tp, 0x0e, 0x0000);
3209 rtl_writephy(tp, 0x0d, 0x0000);
3212 rtl_writephy(tp, 0x1f, 0x0003);
3213 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3214 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3215 rtl_writephy(tp, 0x1f, 0x0000);
3217 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3218 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3221 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3223 /* For 4-corner performance improve */
3224 rtl_writephy(tp, 0x1f, 0x0005);
3225 rtl_writephy(tp, 0x05, 0x8b80);
3226 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3227 rtl_writephy(tp, 0x1f, 0x0000);
3229 /* PHY auto speed down */
3230 rtl_writephy(tp, 0x1f, 0x0007);
3231 rtl_writephy(tp, 0x1e, 0x002d);
3232 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3233 rtl_writephy(tp, 0x1f, 0x0000);
3234 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3236 /* Improve 10M EEE waveform */
3237 rtl_writephy(tp, 0x1f, 0x0005);
3238 rtl_writephy(tp, 0x05, 0x8b86);
3239 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3240 rtl_writephy(tp, 0x1f, 0x0000);
3243 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3245 static const struct phy_reg phy_reg_init[] = {
3246 /* Channel estimation fine tune */
3251 /* Modify green table for giga & fnet */
3268 /* Modify green table for 10M */
3274 /* Disable hiimpedance detection (RTCT) */
3280 rtl_apply_firmware(tp);
3282 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3284 rtl8168f_hw_phy_config(tp);
3286 /* Improve 2-pair detection performance */
3287 rtl_writephy(tp, 0x1f, 0x0005);
3288 rtl_writephy(tp, 0x05, 0x8b85);
3289 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3290 rtl_writephy(tp, 0x1f, 0x0000);
3293 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3295 rtl_apply_firmware(tp);
3297 rtl8168f_hw_phy_config(tp);
3300 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3302 static const struct phy_reg phy_reg_init[] = {
3303 /* Channel estimation fine tune */
3308 /* Modify green table for giga & fnet */
3325 /* Modify green table for 10M */
3331 /* Disable hiimpedance detection (RTCT) */
3338 rtl_apply_firmware(tp);
3340 rtl8168f_hw_phy_config(tp);
3342 /* Improve 2-pair detection performance */
3343 rtl_writephy(tp, 0x1f, 0x0005);
3344 rtl_writephy(tp, 0x05, 0x8b85);
3345 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3346 rtl_writephy(tp, 0x1f, 0x0000);
3348 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3350 /* Modify green table for giga */
3351 rtl_writephy(tp, 0x1f, 0x0005);
3352 rtl_writephy(tp, 0x05, 0x8b54);
3353 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3354 rtl_writephy(tp, 0x05, 0x8b5d);
3355 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3356 rtl_writephy(tp, 0x05, 0x8a7c);
3357 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3358 rtl_writephy(tp, 0x05, 0x8a7f);
3359 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3360 rtl_writephy(tp, 0x05, 0x8a82);
3361 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3362 rtl_writephy(tp, 0x05, 0x8a85);
3363 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3364 rtl_writephy(tp, 0x05, 0x8a88);
3365 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3366 rtl_writephy(tp, 0x1f, 0x0000);
3368 /* uc same-seed solution */
3369 rtl_writephy(tp, 0x1f, 0x0005);
3370 rtl_writephy(tp, 0x05, 0x8b85);
3371 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3372 rtl_writephy(tp, 0x1f, 0x0000);
3375 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3376 rtl_writephy(tp, 0x1f, 0x0005);
3377 rtl_writephy(tp, 0x05, 0x8b85);
3378 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3379 rtl_writephy(tp, 0x1f, 0x0004);
3380 rtl_writephy(tp, 0x1f, 0x0007);
3381 rtl_writephy(tp, 0x1e, 0x0020);
3382 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3383 rtl_writephy(tp, 0x1f, 0x0000);
3384 rtl_writephy(tp, 0x0d, 0x0007);
3385 rtl_writephy(tp, 0x0e, 0x003c);
3386 rtl_writephy(tp, 0x0d, 0x4007);
3387 rtl_writephy(tp, 0x0e, 0x0000);
3388 rtl_writephy(tp, 0x0d, 0x0000);
3391 rtl_writephy(tp, 0x1f, 0x0003);
3392 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3393 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3394 rtl_writephy(tp, 0x1f, 0x0000);
3397 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3399 rtl_apply_firmware(tp);
3401 rtl_writephy(tp, 0x1f, 0x0a46);
3402 if (rtl_readphy(tp, 0x10) & 0x0100) {
3403 rtl_writephy(tp, 0x1f, 0x0bcc);
3404 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3406 rtl_writephy(tp, 0x1f, 0x0bcc);
3407 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3410 rtl_writephy(tp, 0x1f, 0x0a46);
3411 if (rtl_readphy(tp, 0x13) & 0x0100) {
3412 rtl_writephy(tp, 0x1f, 0x0c41);
3413 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3415 rtl_writephy(tp, 0x1f, 0x0c41);
3416 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
3419 /* Enable PHY auto speed down */
3420 rtl_writephy(tp, 0x1f, 0x0a44);
3421 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
3423 rtl_writephy(tp, 0x1f, 0x0bcc);
3424 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3425 rtl_writephy(tp, 0x1f, 0x0a44);
3426 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3427 rtl_writephy(tp, 0x1f, 0x0a43);
3428 rtl_writephy(tp, 0x13, 0x8084);
3429 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3430 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3432 /* EEE auto-fallback function */
3433 rtl_writephy(tp, 0x1f, 0x0a4b);
3434 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
3436 /* Enable UC LPF tune function */
3437 rtl_writephy(tp, 0x1f, 0x0a43);
3438 rtl_writephy(tp, 0x13, 0x8012);
3439 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3441 rtl_writephy(tp, 0x1f, 0x0c42);
3442 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3444 /* Improve SWR Efficiency */
3445 rtl_writephy(tp, 0x1f, 0x0bcd);
3446 rtl_writephy(tp, 0x14, 0x5065);
3447 rtl_writephy(tp, 0x14, 0xd065);
3448 rtl_writephy(tp, 0x1f, 0x0bc8);
3449 rtl_writephy(tp, 0x11, 0x5655);
3450 rtl_writephy(tp, 0x1f, 0x0bcd);
3451 rtl_writephy(tp, 0x14, 0x1065);
3452 rtl_writephy(tp, 0x14, 0x9065);
3453 rtl_writephy(tp, 0x14, 0x1065);
3455 /* Check ALDPS bit, disable it if enabled */
3456 rtl_writephy(tp, 0x1f, 0x0a43);
3457 if (rtl_readphy(tp, 0x10) & 0x0004)
3458 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0004);
3460 rtl_writephy(tp, 0x1f, 0x0000);
3463 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3465 rtl_apply_firmware(tp);
3468 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3470 static const struct phy_reg phy_reg_init[] = {
3477 rtl_writephy(tp, 0x1f, 0x0000);
3478 rtl_patchphy(tp, 0x11, 1 << 12);
3479 rtl_patchphy(tp, 0x19, 1 << 13);
3480 rtl_patchphy(tp, 0x10, 1 << 15);
3482 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3485 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3487 static const struct phy_reg phy_reg_init[] = {
3501 /* Disable ALDPS before ram code */
3502 rtl_writephy(tp, 0x1f, 0x0000);
3503 rtl_writephy(tp, 0x18, 0x0310);
3506 rtl_apply_firmware(tp);
3508 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3511 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3513 /* Disable ALDPS before setting firmware */
3514 rtl_writephy(tp, 0x1f, 0x0000);
3515 rtl_writephy(tp, 0x18, 0x0310);
3518 rtl_apply_firmware(tp);
3521 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3522 rtl_writephy(tp, 0x1f, 0x0004);
3523 rtl_writephy(tp, 0x10, 0x401f);
3524 rtl_writephy(tp, 0x19, 0x7030);
3525 rtl_writephy(tp, 0x1f, 0x0000);
3528 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3530 static const struct phy_reg phy_reg_init[] = {
3537 /* Disable ALDPS before ram code */
3538 rtl_writephy(tp, 0x1f, 0x0000);
3539 rtl_writephy(tp, 0x18, 0x0310);
3542 rtl_apply_firmware(tp);
3544 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3545 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3547 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3550 static void rtl_hw_phy_config(struct net_device *dev)
3552 struct rtl8169_private *tp = netdev_priv(dev);
3554 rtl8169_print_mac_version(tp);
3556 switch (tp->mac_version) {
3557 case RTL_GIGA_MAC_VER_01:
3559 case RTL_GIGA_MAC_VER_02:
3560 case RTL_GIGA_MAC_VER_03:
3561 rtl8169s_hw_phy_config(tp);
3563 case RTL_GIGA_MAC_VER_04:
3564 rtl8169sb_hw_phy_config(tp);
3566 case RTL_GIGA_MAC_VER_05:
3567 rtl8169scd_hw_phy_config(tp);
3569 case RTL_GIGA_MAC_VER_06:
3570 rtl8169sce_hw_phy_config(tp);
3572 case RTL_GIGA_MAC_VER_07:
3573 case RTL_GIGA_MAC_VER_08:
3574 case RTL_GIGA_MAC_VER_09:
3575 rtl8102e_hw_phy_config(tp);
3577 case RTL_GIGA_MAC_VER_11:
3578 rtl8168bb_hw_phy_config(tp);
3580 case RTL_GIGA_MAC_VER_12:
3581 rtl8168bef_hw_phy_config(tp);
3583 case RTL_GIGA_MAC_VER_17:
3584 rtl8168bef_hw_phy_config(tp);
3586 case RTL_GIGA_MAC_VER_18:
3587 rtl8168cp_1_hw_phy_config(tp);
3589 case RTL_GIGA_MAC_VER_19:
3590 rtl8168c_1_hw_phy_config(tp);
3592 case RTL_GIGA_MAC_VER_20:
3593 rtl8168c_2_hw_phy_config(tp);
3595 case RTL_GIGA_MAC_VER_21:
3596 rtl8168c_3_hw_phy_config(tp);
3598 case RTL_GIGA_MAC_VER_22:
3599 rtl8168c_4_hw_phy_config(tp);
3601 case RTL_GIGA_MAC_VER_23:
3602 case RTL_GIGA_MAC_VER_24:
3603 rtl8168cp_2_hw_phy_config(tp);
3605 case RTL_GIGA_MAC_VER_25:
3606 rtl8168d_1_hw_phy_config(tp);
3608 case RTL_GIGA_MAC_VER_26:
3609 rtl8168d_2_hw_phy_config(tp);
3611 case RTL_GIGA_MAC_VER_27:
3612 rtl8168d_3_hw_phy_config(tp);
3614 case RTL_GIGA_MAC_VER_28:
3615 rtl8168d_4_hw_phy_config(tp);
3617 case RTL_GIGA_MAC_VER_29:
3618 case RTL_GIGA_MAC_VER_30:
3619 rtl8105e_hw_phy_config(tp);
3621 case RTL_GIGA_MAC_VER_31:
3624 case RTL_GIGA_MAC_VER_32:
3625 case RTL_GIGA_MAC_VER_33:
3626 rtl8168e_1_hw_phy_config(tp);
3628 case RTL_GIGA_MAC_VER_34:
3629 rtl8168e_2_hw_phy_config(tp);
3631 case RTL_GIGA_MAC_VER_35:
3632 rtl8168f_1_hw_phy_config(tp);
3634 case RTL_GIGA_MAC_VER_36:
3635 rtl8168f_2_hw_phy_config(tp);
3638 case RTL_GIGA_MAC_VER_37:
3639 rtl8402_hw_phy_config(tp);
3642 case RTL_GIGA_MAC_VER_38:
3643 rtl8411_hw_phy_config(tp);
3646 case RTL_GIGA_MAC_VER_39:
3647 rtl8106e_hw_phy_config(tp);
3650 case RTL_GIGA_MAC_VER_40:
3651 rtl8168g_1_hw_phy_config(tp);
3653 case RTL_GIGA_MAC_VER_42:
3654 case RTL_GIGA_MAC_VER_43:
3655 case RTL_GIGA_MAC_VER_44:
3656 rtl8168g_2_hw_phy_config(tp);
3659 case RTL_GIGA_MAC_VER_41:
3665 static void rtl_phy_work(struct rtl8169_private *tp)
3667 struct timer_list *timer = &tp->timer;
3668 void __iomem *ioaddr = tp->mmio_addr;
3669 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3671 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
3673 if (tp->phy_reset_pending(tp)) {
3675 * A busy loop could burn quite a few cycles on nowadays CPU.
3676 * Let's delay the execution of the timer for a few ticks.
3682 if (tp->link_ok(ioaddr))
3685 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
3687 tp->phy_reset_enable(tp);
3690 mod_timer(timer, jiffies + timeout);
3693 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3695 if (!test_and_set_bit(flag, tp->wk.flags))
3696 schedule_work(&tp->wk.work);
3699 static void rtl8169_phy_timer(unsigned long __opaque)
3701 struct net_device *dev = (struct net_device *)__opaque;
3702 struct rtl8169_private *tp = netdev_priv(dev);
3704 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
3707 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3708 void __iomem *ioaddr)
3711 pci_release_regions(pdev);
3712 pci_clear_mwi(pdev);
3713 pci_disable_device(pdev);
3717 DECLARE_RTL_COND(rtl_phy_reset_cond)
3719 return tp->phy_reset_pending(tp);
3722 static void rtl8169_phy_reset(struct net_device *dev,
3723 struct rtl8169_private *tp)
3725 tp->phy_reset_enable(tp);
3726 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
3729 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3731 void __iomem *ioaddr = tp->mmio_addr;
3733 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3734 (RTL_R8(PHYstatus) & TBI_Enable);
3737 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3739 void __iomem *ioaddr = tp->mmio_addr;
3741 rtl_hw_phy_config(dev);
3743 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3744 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3748 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3750 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3751 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3753 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
3754 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3756 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
3757 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
3760 rtl8169_phy_reset(dev, tp);
3762 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3763 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3764 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3765 (tp->mii.supports_gmii ?
3766 ADVERTISED_1000baseT_Half |
3767 ADVERTISED_1000baseT_Full : 0));
3769 if (rtl_tbi_enabled(tp))
3770 netif_info(tp, link, dev, "TBI auto-negotiating\n");
3773 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3775 void __iomem *ioaddr = tp->mmio_addr;
3779 RTL_W8(Cfg9346, Cfg9346_Unlock);
3781 RTL_W32(MAC4, addr[4] | addr[5] << 8);
3784 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3787 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3788 rtl_rar_exgmac_set(tp, addr);
3790 RTL_W8(Cfg9346, Cfg9346_Lock);
3792 rtl_unlock_work(tp);
3795 static int rtl_set_mac_address(struct net_device *dev, void *p)
3797 struct rtl8169_private *tp = netdev_priv(dev);
3798 struct sockaddr *addr = p;
3800 if (!is_valid_ether_addr(addr->sa_data))
3801 return -EADDRNOTAVAIL;
3803 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3805 rtl_rar_set(tp, dev->dev_addr);
3810 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3812 struct rtl8169_private *tp = netdev_priv(dev);
3813 struct mii_ioctl_data *data = if_mii(ifr);
3815 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3818 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3819 struct mii_ioctl_data *data, int cmd)
3823 data->phy_id = 32; /* Internal PHY */
3827 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3831 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3837 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3842 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3844 if (tp->features & RTL_FEATURE_MSI) {
3845 pci_disable_msi(pdev);
3846 tp->features &= ~RTL_FEATURE_MSI;
3850 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
3852 struct mdio_ops *ops = &tp->mdio_ops;
3854 switch (tp->mac_version) {
3855 case RTL_GIGA_MAC_VER_27:
3856 ops->write = r8168dp_1_mdio_write;
3857 ops->read = r8168dp_1_mdio_read;
3859 case RTL_GIGA_MAC_VER_28:
3860 case RTL_GIGA_MAC_VER_31:
3861 ops->write = r8168dp_2_mdio_write;
3862 ops->read = r8168dp_2_mdio_read;
3864 case RTL_GIGA_MAC_VER_40:
3865 case RTL_GIGA_MAC_VER_41:
3866 case RTL_GIGA_MAC_VER_42:
3867 case RTL_GIGA_MAC_VER_43:
3868 case RTL_GIGA_MAC_VER_44:
3869 ops->write = r8168g_mdio_write;
3870 ops->read = r8168g_mdio_read;
3873 ops->write = r8169_mdio_write;
3874 ops->read = r8169_mdio_read;
3879 static void rtl_speed_down(struct rtl8169_private *tp)
3884 rtl_writephy(tp, 0x1f, 0x0000);
3885 lpa = rtl_readphy(tp, MII_LPA);
3887 if (lpa & (LPA_10HALF | LPA_10FULL))
3888 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
3889 else if (lpa & (LPA_100HALF | LPA_100FULL))
3890 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3891 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
3893 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3894 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3895 (tp->mii.supports_gmii ?
3896 ADVERTISED_1000baseT_Half |
3897 ADVERTISED_1000baseT_Full : 0);
3899 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3903 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3905 void __iomem *ioaddr = tp->mmio_addr;
3907 switch (tp->mac_version) {
3908 case RTL_GIGA_MAC_VER_25:
3909 case RTL_GIGA_MAC_VER_26:
3910 case RTL_GIGA_MAC_VER_29:
3911 case RTL_GIGA_MAC_VER_30:
3912 case RTL_GIGA_MAC_VER_32:
3913 case RTL_GIGA_MAC_VER_33:
3914 case RTL_GIGA_MAC_VER_34:
3915 case RTL_GIGA_MAC_VER_37:
3916 case RTL_GIGA_MAC_VER_38:
3917 case RTL_GIGA_MAC_VER_39:
3918 case RTL_GIGA_MAC_VER_40:
3919 case RTL_GIGA_MAC_VER_41:
3920 case RTL_GIGA_MAC_VER_42:
3921 case RTL_GIGA_MAC_VER_43:
3922 case RTL_GIGA_MAC_VER_44:
3923 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3924 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3931 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3933 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3937 rtl_wol_suspend_quirk(tp);
3942 static void r810x_phy_power_down(struct rtl8169_private *tp)
3944 rtl_writephy(tp, 0x1f, 0x0000);
3945 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3948 static void r810x_phy_power_up(struct rtl8169_private *tp)
3950 rtl_writephy(tp, 0x1f, 0x0000);
3951 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3954 static void r810x_pll_power_down(struct rtl8169_private *tp)
3956 void __iomem *ioaddr = tp->mmio_addr;
3958 if (rtl_wol_pll_power_down(tp))
3961 r810x_phy_power_down(tp);
3963 switch (tp->mac_version) {
3964 case RTL_GIGA_MAC_VER_07:
3965 case RTL_GIGA_MAC_VER_08:
3966 case RTL_GIGA_MAC_VER_09:
3967 case RTL_GIGA_MAC_VER_10:
3968 case RTL_GIGA_MAC_VER_13:
3969 case RTL_GIGA_MAC_VER_16:
3972 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3977 static void r810x_pll_power_up(struct rtl8169_private *tp)
3979 void __iomem *ioaddr = tp->mmio_addr;
3981 r810x_phy_power_up(tp);
3983 switch (tp->mac_version) {
3984 case RTL_GIGA_MAC_VER_07:
3985 case RTL_GIGA_MAC_VER_08:
3986 case RTL_GIGA_MAC_VER_09:
3987 case RTL_GIGA_MAC_VER_10:
3988 case RTL_GIGA_MAC_VER_13:
3989 case RTL_GIGA_MAC_VER_16:
3992 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3997 static void r8168_phy_power_up(struct rtl8169_private *tp)
3999 rtl_writephy(tp, 0x1f, 0x0000);
4000 switch (tp->mac_version) {
4001 case RTL_GIGA_MAC_VER_11:
4002 case RTL_GIGA_MAC_VER_12:
4003 case RTL_GIGA_MAC_VER_17:
4004 case RTL_GIGA_MAC_VER_18:
4005 case RTL_GIGA_MAC_VER_19:
4006 case RTL_GIGA_MAC_VER_20:
4007 case RTL_GIGA_MAC_VER_21:
4008 case RTL_GIGA_MAC_VER_22:
4009 case RTL_GIGA_MAC_VER_23:
4010 case RTL_GIGA_MAC_VER_24:
4011 case RTL_GIGA_MAC_VER_25:
4012 case RTL_GIGA_MAC_VER_26:
4013 case RTL_GIGA_MAC_VER_27:
4014 case RTL_GIGA_MAC_VER_28:
4015 case RTL_GIGA_MAC_VER_31:
4016 rtl_writephy(tp, 0x0e, 0x0000);
4021 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4024 static void r8168_phy_power_down(struct rtl8169_private *tp)
4026 rtl_writephy(tp, 0x1f, 0x0000);
4027 switch (tp->mac_version) {
4028 case RTL_GIGA_MAC_VER_32:
4029 case RTL_GIGA_MAC_VER_33:
4030 case RTL_GIGA_MAC_VER_40:
4031 case RTL_GIGA_MAC_VER_41:
4032 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4035 case RTL_GIGA_MAC_VER_11:
4036 case RTL_GIGA_MAC_VER_12:
4037 case RTL_GIGA_MAC_VER_17:
4038 case RTL_GIGA_MAC_VER_18:
4039 case RTL_GIGA_MAC_VER_19:
4040 case RTL_GIGA_MAC_VER_20:
4041 case RTL_GIGA_MAC_VER_21:
4042 case RTL_GIGA_MAC_VER_22:
4043 case RTL_GIGA_MAC_VER_23:
4044 case RTL_GIGA_MAC_VER_24:
4045 case RTL_GIGA_MAC_VER_25:
4046 case RTL_GIGA_MAC_VER_26:
4047 case RTL_GIGA_MAC_VER_27:
4048 case RTL_GIGA_MAC_VER_28:
4049 case RTL_GIGA_MAC_VER_31:
4050 rtl_writephy(tp, 0x0e, 0x0200);
4052 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4057 static void r8168_pll_power_down(struct rtl8169_private *tp)
4059 void __iomem *ioaddr = tp->mmio_addr;
4061 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4062 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4063 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
4064 r8168dp_check_dash(tp)) {
4068 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4069 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
4070 (RTL_R16(CPlusCmd) & ASF)) {
4074 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4075 tp->mac_version == RTL_GIGA_MAC_VER_33)
4076 rtl_ephy_write(tp, 0x19, 0xff64);
4078 if (rtl_wol_pll_power_down(tp))
4081 r8168_phy_power_down(tp);
4083 switch (tp->mac_version) {
4084 case RTL_GIGA_MAC_VER_25:
4085 case RTL_GIGA_MAC_VER_26:
4086 case RTL_GIGA_MAC_VER_27:
4087 case RTL_GIGA_MAC_VER_28:
4088 case RTL_GIGA_MAC_VER_31:
4089 case RTL_GIGA_MAC_VER_32:
4090 case RTL_GIGA_MAC_VER_33:
4091 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4093 case RTL_GIGA_MAC_VER_40:
4094 case RTL_GIGA_MAC_VER_41:
4095 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4096 0xfc000000, ERIAR_EXGMAC);
4101 static void r8168_pll_power_up(struct rtl8169_private *tp)
4103 void __iomem *ioaddr = tp->mmio_addr;
4105 switch (tp->mac_version) {
4106 case RTL_GIGA_MAC_VER_25:
4107 case RTL_GIGA_MAC_VER_26:
4108 case RTL_GIGA_MAC_VER_27:
4109 case RTL_GIGA_MAC_VER_28:
4110 case RTL_GIGA_MAC_VER_31:
4111 case RTL_GIGA_MAC_VER_32:
4112 case RTL_GIGA_MAC_VER_33:
4113 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4115 case RTL_GIGA_MAC_VER_40:
4116 case RTL_GIGA_MAC_VER_41:
4117 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4118 0x00000000, ERIAR_EXGMAC);
4122 r8168_phy_power_up(tp);
4125 static void rtl_generic_op(struct rtl8169_private *tp,
4126 void (*op)(struct rtl8169_private *))
4132 static void rtl_pll_power_down(struct rtl8169_private *tp)
4134 rtl_generic_op(tp, tp->pll_power_ops.down);
4137 static void rtl_pll_power_up(struct rtl8169_private *tp)
4139 rtl_generic_op(tp, tp->pll_power_ops.up);
4142 static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
4144 struct pll_power_ops *ops = &tp->pll_power_ops;
4146 switch (tp->mac_version) {
4147 case RTL_GIGA_MAC_VER_07:
4148 case RTL_GIGA_MAC_VER_08:
4149 case RTL_GIGA_MAC_VER_09:
4150 case RTL_GIGA_MAC_VER_10:
4151 case RTL_GIGA_MAC_VER_16:
4152 case RTL_GIGA_MAC_VER_29:
4153 case RTL_GIGA_MAC_VER_30:
4154 case RTL_GIGA_MAC_VER_37:
4155 case RTL_GIGA_MAC_VER_39:
4156 case RTL_GIGA_MAC_VER_43:
4157 ops->down = r810x_pll_power_down;
4158 ops->up = r810x_pll_power_up;
4161 case RTL_GIGA_MAC_VER_11:
4162 case RTL_GIGA_MAC_VER_12:
4163 case RTL_GIGA_MAC_VER_17:
4164 case RTL_GIGA_MAC_VER_18:
4165 case RTL_GIGA_MAC_VER_19:
4166 case RTL_GIGA_MAC_VER_20:
4167 case RTL_GIGA_MAC_VER_21:
4168 case RTL_GIGA_MAC_VER_22:
4169 case RTL_GIGA_MAC_VER_23:
4170 case RTL_GIGA_MAC_VER_24:
4171 case RTL_GIGA_MAC_VER_25:
4172 case RTL_GIGA_MAC_VER_26:
4173 case RTL_GIGA_MAC_VER_27:
4174 case RTL_GIGA_MAC_VER_28:
4175 case RTL_GIGA_MAC_VER_31:
4176 case RTL_GIGA_MAC_VER_32:
4177 case RTL_GIGA_MAC_VER_33:
4178 case RTL_GIGA_MAC_VER_34:
4179 case RTL_GIGA_MAC_VER_35:
4180 case RTL_GIGA_MAC_VER_36:
4181 case RTL_GIGA_MAC_VER_38:
4182 case RTL_GIGA_MAC_VER_40:
4183 case RTL_GIGA_MAC_VER_41:
4184 case RTL_GIGA_MAC_VER_42:
4185 case RTL_GIGA_MAC_VER_44:
4186 ops->down = r8168_pll_power_down;
4187 ops->up = r8168_pll_power_up;
4197 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4199 void __iomem *ioaddr = tp->mmio_addr;
4201 switch (tp->mac_version) {
4202 case RTL_GIGA_MAC_VER_01:
4203 case RTL_GIGA_MAC_VER_02:
4204 case RTL_GIGA_MAC_VER_03:
4205 case RTL_GIGA_MAC_VER_04:
4206 case RTL_GIGA_MAC_VER_05:
4207 case RTL_GIGA_MAC_VER_06:
4208 case RTL_GIGA_MAC_VER_10:
4209 case RTL_GIGA_MAC_VER_11:
4210 case RTL_GIGA_MAC_VER_12:
4211 case RTL_GIGA_MAC_VER_13:
4212 case RTL_GIGA_MAC_VER_14:
4213 case RTL_GIGA_MAC_VER_15:
4214 case RTL_GIGA_MAC_VER_16:
4215 case RTL_GIGA_MAC_VER_17:
4216 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4218 case RTL_GIGA_MAC_VER_18:
4219 case RTL_GIGA_MAC_VER_19:
4220 case RTL_GIGA_MAC_VER_20:
4221 case RTL_GIGA_MAC_VER_21:
4222 case RTL_GIGA_MAC_VER_22:
4223 case RTL_GIGA_MAC_VER_23:
4224 case RTL_GIGA_MAC_VER_24:
4225 case RTL_GIGA_MAC_VER_34:
4226 case RTL_GIGA_MAC_VER_35:
4227 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4229 case RTL_GIGA_MAC_VER_40:
4230 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4232 case RTL_GIGA_MAC_VER_41:
4233 case RTL_GIGA_MAC_VER_42:
4234 case RTL_GIGA_MAC_VER_43:
4235 case RTL_GIGA_MAC_VER_44:
4236 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4239 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4244 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4246 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4249 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4251 void __iomem *ioaddr = tp->mmio_addr;
4253 RTL_W8(Cfg9346, Cfg9346_Unlock);
4254 rtl_generic_op(tp, tp->jumbo_ops.enable);
4255 RTL_W8(Cfg9346, Cfg9346_Lock);
4258 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4260 void __iomem *ioaddr = tp->mmio_addr;
4262 RTL_W8(Cfg9346, Cfg9346_Unlock);
4263 rtl_generic_op(tp, tp->jumbo_ops.disable);
4264 RTL_W8(Cfg9346, Cfg9346_Lock);
4267 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4269 void __iomem *ioaddr = tp->mmio_addr;
4271 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4272 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4273 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4276 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4278 void __iomem *ioaddr = tp->mmio_addr;
4280 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4281 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4282 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4285 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4287 void __iomem *ioaddr = tp->mmio_addr;
4289 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4292 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4294 void __iomem *ioaddr = tp->mmio_addr;
4296 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4299 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4301 void __iomem *ioaddr = tp->mmio_addr;
4303 RTL_W8(MaxTxPacketSize, 0x3f);
4304 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4305 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
4306 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4309 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4311 void __iomem *ioaddr = tp->mmio_addr;
4313 RTL_W8(MaxTxPacketSize, 0x0c);
4314 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4315 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
4316 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4319 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4321 rtl_tx_performance_tweak(tp->pci_dev,
4322 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4325 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4327 rtl_tx_performance_tweak(tp->pci_dev,
4328 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4331 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4333 void __iomem *ioaddr = tp->mmio_addr;
4335 r8168b_0_hw_jumbo_enable(tp);
4337 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4340 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4342 void __iomem *ioaddr = tp->mmio_addr;
4344 r8168b_0_hw_jumbo_disable(tp);
4346 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4349 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4351 struct jumbo_ops *ops = &tp->jumbo_ops;
4353 switch (tp->mac_version) {
4354 case RTL_GIGA_MAC_VER_11:
4355 ops->disable = r8168b_0_hw_jumbo_disable;
4356 ops->enable = r8168b_0_hw_jumbo_enable;
4358 case RTL_GIGA_MAC_VER_12:
4359 case RTL_GIGA_MAC_VER_17:
4360 ops->disable = r8168b_1_hw_jumbo_disable;
4361 ops->enable = r8168b_1_hw_jumbo_enable;
4363 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4364 case RTL_GIGA_MAC_VER_19:
4365 case RTL_GIGA_MAC_VER_20:
4366 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4367 case RTL_GIGA_MAC_VER_22:
4368 case RTL_GIGA_MAC_VER_23:
4369 case RTL_GIGA_MAC_VER_24:
4370 case RTL_GIGA_MAC_VER_25:
4371 case RTL_GIGA_MAC_VER_26:
4372 ops->disable = r8168c_hw_jumbo_disable;
4373 ops->enable = r8168c_hw_jumbo_enable;
4375 case RTL_GIGA_MAC_VER_27:
4376 case RTL_GIGA_MAC_VER_28:
4377 ops->disable = r8168dp_hw_jumbo_disable;
4378 ops->enable = r8168dp_hw_jumbo_enable;
4380 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4381 case RTL_GIGA_MAC_VER_32:
4382 case RTL_GIGA_MAC_VER_33:
4383 case RTL_GIGA_MAC_VER_34:
4384 ops->disable = r8168e_hw_jumbo_disable;
4385 ops->enable = r8168e_hw_jumbo_enable;
4389 * No action needed for jumbo frames with 8169.
4390 * No jumbo for 810x at all.
4392 case RTL_GIGA_MAC_VER_40:
4393 case RTL_GIGA_MAC_VER_41:
4394 case RTL_GIGA_MAC_VER_42:
4395 case RTL_GIGA_MAC_VER_43:
4396 case RTL_GIGA_MAC_VER_44:
4398 ops->disable = NULL;
4404 DECLARE_RTL_COND(rtl_chipcmd_cond)
4406 void __iomem *ioaddr = tp->mmio_addr;
4408 return RTL_R8(ChipCmd) & CmdReset;
4411 static void rtl_hw_reset(struct rtl8169_private *tp)
4413 void __iomem *ioaddr = tp->mmio_addr;
4415 RTL_W8(ChipCmd, CmdReset);
4417 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4420 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4422 struct rtl_fw *rtl_fw;
4426 name = rtl_lookup_firmware_name(tp);
4428 goto out_no_firmware;
4430 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4434 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4438 rc = rtl_check_firmware(tp, rtl_fw);
4440 goto err_release_firmware;
4442 tp->rtl_fw = rtl_fw;
4446 err_release_firmware:
4447 release_firmware(rtl_fw->fw);
4451 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4458 static void rtl_request_firmware(struct rtl8169_private *tp)
4460 if (IS_ERR(tp->rtl_fw))
4461 rtl_request_uncached_firmware(tp);
4464 static void rtl_rx_close(struct rtl8169_private *tp)
4466 void __iomem *ioaddr = tp->mmio_addr;
4468 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4471 DECLARE_RTL_COND(rtl_npq_cond)
4473 void __iomem *ioaddr = tp->mmio_addr;
4475 return RTL_R8(TxPoll) & NPQ;
4478 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4480 void __iomem *ioaddr = tp->mmio_addr;
4482 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4485 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4487 void __iomem *ioaddr = tp->mmio_addr;
4489 /* Disable interrupts */
4490 rtl8169_irq_mask_and_ack(tp);
4494 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4495 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4496 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4497 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4498 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4499 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4500 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
4501 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4502 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4503 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
4504 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
4505 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
4506 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
4507 tp->mac_version == RTL_GIGA_MAC_VER_38) {
4508 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4509 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4511 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4518 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4520 void __iomem *ioaddr = tp->mmio_addr;
4522 /* Set DMA burst size and Interframe Gap Time */
4523 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4524 (InterFrameGap << TxInterFrameGapShift));
4527 static void rtl_hw_start(struct net_device *dev)
4529 struct rtl8169_private *tp = netdev_priv(dev);
4533 rtl_irq_enable_all(tp);
4536 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4537 void __iomem *ioaddr)
4540 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4541 * register to be written before TxDescAddrLow to work.
4542 * Switching from MMIO to I/O access fixes the issue as well.
4544 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4545 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4546 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4547 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4550 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4554 cmd = RTL_R16(CPlusCmd);
4555 RTL_W16(CPlusCmd, cmd);
4559 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
4561 /* Low hurts. Let's disable the filtering. */
4562 RTL_W16(RxMaxSize, rx_buf_sz + 1);
4565 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4567 static const struct rtl_cfg2_info {
4572 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4573 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4574 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4575 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4577 const struct rtl_cfg2_info *p = cfg2_info;
4581 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
4582 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4583 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4584 RTL_W32(0x7c, p->val);
4590 static void rtl_set_rx_mode(struct net_device *dev)
4592 struct rtl8169_private *tp = netdev_priv(dev);
4593 void __iomem *ioaddr = tp->mmio_addr;
4594 u32 mc_filter[2]; /* Multicast hash filter */
4598 if (dev->flags & IFF_PROMISC) {
4599 /* Unconditionally log net taps. */
4600 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4602 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4604 mc_filter[1] = mc_filter[0] = 0xffffffff;
4605 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4606 (dev->flags & IFF_ALLMULTI)) {
4607 /* Too many to filter perfectly -- accept all multicasts. */
4608 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4609 mc_filter[1] = mc_filter[0] = 0xffffffff;
4611 struct netdev_hw_addr *ha;
4613 rx_mode = AcceptBroadcast | AcceptMyPhys;
4614 mc_filter[1] = mc_filter[0] = 0;
4615 netdev_for_each_mc_addr(ha, dev) {
4616 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4617 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4618 rx_mode |= AcceptMulticast;
4622 if (dev->features & NETIF_F_RXALL)
4623 rx_mode |= (AcceptErr | AcceptRunt);
4625 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4627 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4628 u32 data = mc_filter[0];
4630 mc_filter[0] = swab32(mc_filter[1]);
4631 mc_filter[1] = swab32(data);
4634 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4635 mc_filter[1] = mc_filter[0] = 0xffffffff;
4637 RTL_W32(MAR0 + 4, mc_filter[1]);
4638 RTL_W32(MAR0 + 0, mc_filter[0]);
4640 RTL_W32(RxConfig, tmp);
4643 static void rtl_hw_start_8169(struct net_device *dev)
4645 struct rtl8169_private *tp = netdev_priv(dev);
4646 void __iomem *ioaddr = tp->mmio_addr;
4647 struct pci_dev *pdev = tp->pci_dev;
4649 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4650 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4651 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4654 RTL_W8(Cfg9346, Cfg9346_Unlock);
4655 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4656 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4657 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4658 tp->mac_version == RTL_GIGA_MAC_VER_04)
4659 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4663 RTL_W8(EarlyTxThres, NoEarlyTx);
4665 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4667 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4668 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4669 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4670 tp->mac_version == RTL_GIGA_MAC_VER_04)
4671 rtl_set_rx_tx_config_registers(tp);
4673 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4675 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4676 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4677 dprintk("Set MAC Reg C+CR Offset 0xE0. "
4678 "Bit-3 and bit-14 MUST be 1\n");
4679 tp->cp_cmd |= (1 << 14);
4682 RTL_W16(CPlusCmd, tp->cp_cmd);
4684 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4687 * Undocumented corner. Supposedly:
4688 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4690 RTL_W16(IntrMitigate, 0x0000);
4692 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4694 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4695 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4696 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4697 tp->mac_version != RTL_GIGA_MAC_VER_04) {
4698 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4699 rtl_set_rx_tx_config_registers(tp);
4702 RTL_W8(Cfg9346, Cfg9346_Lock);
4704 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4707 RTL_W32(RxMissed, 0);
4709 rtl_set_rx_mode(dev);
4711 /* no early-rx interrupts */
4712 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4715 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4717 if (tp->csi_ops.write)
4718 tp->csi_ops.write(tp, addr, value);
4721 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4723 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
4726 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
4730 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4731 rtl_csi_write(tp, 0x070c, csi | bits);
4734 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
4736 rtl_csi_access_enable(tp, 0x17000000);
4739 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
4741 rtl_csi_access_enable(tp, 0x27000000);
4744 DECLARE_RTL_COND(rtl_csiar_cond)
4746 void __iomem *ioaddr = tp->mmio_addr;
4748 return RTL_R32(CSIAR) & CSIAR_FLAG;
4751 static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
4753 void __iomem *ioaddr = tp->mmio_addr;
4755 RTL_W32(CSIDR, value);
4756 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4757 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4759 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4762 static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
4764 void __iomem *ioaddr = tp->mmio_addr;
4766 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4767 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4769 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4770 RTL_R32(CSIDR) : ~0;
4773 static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
4775 void __iomem *ioaddr = tp->mmio_addr;
4777 RTL_W32(CSIDR, value);
4778 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4779 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4782 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4785 static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
4787 void __iomem *ioaddr = tp->mmio_addr;
4789 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4790 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4792 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4793 RTL_R32(CSIDR) : ~0;
4796 static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
4798 void __iomem *ioaddr = tp->mmio_addr;
4800 RTL_W32(CSIDR, value);
4801 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4802 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4805 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4808 static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
4810 void __iomem *ioaddr = tp->mmio_addr;
4812 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
4813 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4815 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4816 RTL_R32(CSIDR) : ~0;
4819 static void rtl_init_csi_ops(struct rtl8169_private *tp)
4821 struct csi_ops *ops = &tp->csi_ops;
4823 switch (tp->mac_version) {
4824 case RTL_GIGA_MAC_VER_01:
4825 case RTL_GIGA_MAC_VER_02:
4826 case RTL_GIGA_MAC_VER_03:
4827 case RTL_GIGA_MAC_VER_04:
4828 case RTL_GIGA_MAC_VER_05:
4829 case RTL_GIGA_MAC_VER_06:
4830 case RTL_GIGA_MAC_VER_10:
4831 case RTL_GIGA_MAC_VER_11:
4832 case RTL_GIGA_MAC_VER_12:
4833 case RTL_GIGA_MAC_VER_13:
4834 case RTL_GIGA_MAC_VER_14:
4835 case RTL_GIGA_MAC_VER_15:
4836 case RTL_GIGA_MAC_VER_16:
4837 case RTL_GIGA_MAC_VER_17:
4842 case RTL_GIGA_MAC_VER_37:
4843 case RTL_GIGA_MAC_VER_38:
4844 ops->write = r8402_csi_write;
4845 ops->read = r8402_csi_read;
4848 case RTL_GIGA_MAC_VER_44:
4849 ops->write = r8411_csi_write;
4850 ops->read = r8411_csi_read;
4854 ops->write = r8169_csi_write;
4855 ops->read = r8169_csi_read;
4861 unsigned int offset;
4866 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4872 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4873 rtl_ephy_write(tp, e->offset, w);
4878 static void rtl_disable_clock_request(struct pci_dev *pdev)
4880 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4881 PCI_EXP_LNKCTL_CLKREQ_EN);
4884 static void rtl_enable_clock_request(struct pci_dev *pdev)
4886 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4887 PCI_EXP_LNKCTL_CLKREQ_EN);
4890 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4892 void __iomem *ioaddr = tp->mmio_addr;
4895 data = RTL_R8(Config3);
4900 data &= ~Rdy_to_L23;
4902 RTL_W8(Config3, data);
4905 #define R8168_CPCMD_QUIRK_MASK (\
4916 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4918 void __iomem *ioaddr = tp->mmio_addr;
4919 struct pci_dev *pdev = tp->pci_dev;
4921 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4923 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4925 if (tp->dev->mtu <= ETH_DATA_LEN) {
4926 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
4927 PCI_EXP_DEVCTL_NOSNOOP_EN);
4931 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4933 void __iomem *ioaddr = tp->mmio_addr;
4935 rtl_hw_start_8168bb(tp);
4937 RTL_W8(MaxTxPacketSize, TxPacketMax);
4939 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4942 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4944 void __iomem *ioaddr = tp->mmio_addr;
4945 struct pci_dev *pdev = tp->pci_dev;
4947 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4949 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4951 if (tp->dev->mtu <= ETH_DATA_LEN)
4952 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4954 rtl_disable_clock_request(pdev);
4956 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4959 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4961 static const struct ephy_info e_info_8168cp[] = {
4962 { 0x01, 0, 0x0001 },
4963 { 0x02, 0x0800, 0x1000 },
4964 { 0x03, 0, 0x0042 },
4965 { 0x06, 0x0080, 0x0000 },
4969 rtl_csi_access_enable_2(tp);
4971 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4973 __rtl_hw_start_8168cp(tp);
4976 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4978 void __iomem *ioaddr = tp->mmio_addr;
4979 struct pci_dev *pdev = tp->pci_dev;
4981 rtl_csi_access_enable_2(tp);
4983 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4985 if (tp->dev->mtu <= ETH_DATA_LEN)
4986 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4988 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4991 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4993 void __iomem *ioaddr = tp->mmio_addr;
4994 struct pci_dev *pdev = tp->pci_dev;
4996 rtl_csi_access_enable_2(tp);
4998 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5001 RTL_W8(DBG_REG, 0x20);
5003 RTL_W8(MaxTxPacketSize, TxPacketMax);
5005 if (tp->dev->mtu <= ETH_DATA_LEN)
5006 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5008 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5011 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
5013 void __iomem *ioaddr = tp->mmio_addr;
5014 static const struct ephy_info e_info_8168c_1[] = {
5015 { 0x02, 0x0800, 0x1000 },
5016 { 0x03, 0, 0x0002 },
5017 { 0x06, 0x0080, 0x0000 }
5020 rtl_csi_access_enable_2(tp);
5022 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5024 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
5026 __rtl_hw_start_8168cp(tp);
5029 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
5031 static const struct ephy_info e_info_8168c_2[] = {
5032 { 0x01, 0, 0x0001 },
5033 { 0x03, 0x0400, 0x0220 }
5036 rtl_csi_access_enable_2(tp);
5038 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
5040 __rtl_hw_start_8168cp(tp);
5043 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
5045 rtl_hw_start_8168c_2(tp);
5048 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
5050 rtl_csi_access_enable_2(tp);
5052 __rtl_hw_start_8168cp(tp);
5055 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
5057 void __iomem *ioaddr = tp->mmio_addr;
5058 struct pci_dev *pdev = tp->pci_dev;
5060 rtl_csi_access_enable_2(tp);
5062 rtl_disable_clock_request(pdev);
5064 RTL_W8(MaxTxPacketSize, TxPacketMax);
5066 if (tp->dev->mtu <= ETH_DATA_LEN)
5067 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5069 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5072 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
5074 void __iomem *ioaddr = tp->mmio_addr;
5075 struct pci_dev *pdev = tp->pci_dev;
5077 rtl_csi_access_enable_1(tp);
5079 if (tp->dev->mtu <= ETH_DATA_LEN)
5080 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5082 RTL_W8(MaxTxPacketSize, TxPacketMax);
5084 rtl_disable_clock_request(pdev);
5087 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
5089 void __iomem *ioaddr = tp->mmio_addr;
5090 struct pci_dev *pdev = tp->pci_dev;
5091 static const struct ephy_info e_info_8168d_4[] = {
5093 { 0x19, 0x20, 0x50 },
5098 rtl_csi_access_enable_1(tp);
5100 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5102 RTL_W8(MaxTxPacketSize, TxPacketMax);
5104 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
5105 const struct ephy_info *e = e_info_8168d_4 + i;
5108 w = rtl_ephy_read(tp, e->offset);
5109 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
5112 rtl_enable_clock_request(pdev);
5115 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
5117 void __iomem *ioaddr = tp->mmio_addr;
5118 struct pci_dev *pdev = tp->pci_dev;
5119 static const struct ephy_info e_info_8168e_1[] = {
5120 { 0x00, 0x0200, 0x0100 },
5121 { 0x00, 0x0000, 0x0004 },
5122 { 0x06, 0x0002, 0x0001 },
5123 { 0x06, 0x0000, 0x0030 },
5124 { 0x07, 0x0000, 0x2000 },
5125 { 0x00, 0x0000, 0x0020 },
5126 { 0x03, 0x5800, 0x2000 },
5127 { 0x03, 0x0000, 0x0001 },
5128 { 0x01, 0x0800, 0x1000 },
5129 { 0x07, 0x0000, 0x4000 },
5130 { 0x1e, 0x0000, 0x2000 },
5131 { 0x19, 0xffff, 0xfe6c },
5132 { 0x0a, 0x0000, 0x0040 }
5135 rtl_csi_access_enable_2(tp);
5137 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5139 if (tp->dev->mtu <= ETH_DATA_LEN)
5140 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5142 RTL_W8(MaxTxPacketSize, TxPacketMax);
5144 rtl_disable_clock_request(pdev);
5146 /* Reset tx FIFO pointer */
5147 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5148 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
5150 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5153 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5155 void __iomem *ioaddr = tp->mmio_addr;
5156 struct pci_dev *pdev = tp->pci_dev;
5157 static const struct ephy_info e_info_8168e_2[] = {
5158 { 0x09, 0x0000, 0x0080 },
5159 { 0x19, 0x0000, 0x0224 }
5162 rtl_csi_access_enable_1(tp);
5164 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5166 if (tp->dev->mtu <= ETH_DATA_LEN)
5167 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5169 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5170 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5171 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5172 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5173 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5174 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5175 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5176 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5178 RTL_W8(MaxTxPacketSize, EarlySize);
5180 rtl_disable_clock_request(pdev);
5182 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5183 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5185 /* Adjust EEE LED frequency */
5186 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5188 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5189 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5190 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5193 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5195 void __iomem *ioaddr = tp->mmio_addr;
5196 struct pci_dev *pdev = tp->pci_dev;
5198 rtl_csi_access_enable_2(tp);
5200 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5202 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5203 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5204 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5205 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5206 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5207 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5208 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5209 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5210 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5211 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5213 RTL_W8(MaxTxPacketSize, EarlySize);
5215 rtl_disable_clock_request(pdev);
5217 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5218 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5219 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5220 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5221 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5224 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5226 void __iomem *ioaddr = tp->mmio_addr;
5227 static const struct ephy_info e_info_8168f_1[] = {
5228 { 0x06, 0x00c0, 0x0020 },
5229 { 0x08, 0x0001, 0x0002 },
5230 { 0x09, 0x0000, 0x0080 },
5231 { 0x19, 0x0000, 0x0224 }
5234 rtl_hw_start_8168f(tp);
5236 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5238 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5240 /* Adjust EEE LED frequency */
5241 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5244 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5246 static const struct ephy_info e_info_8168f_1[] = {
5247 { 0x06, 0x00c0, 0x0020 },
5248 { 0x0f, 0xffff, 0x5200 },
5249 { 0x1e, 0x0000, 0x4000 },
5250 { 0x19, 0x0000, 0x0224 }
5253 rtl_hw_start_8168f(tp);
5254 rtl_pcie_state_l2l3_enable(tp, false);
5256 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5258 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5261 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5263 void __iomem *ioaddr = tp->mmio_addr;
5264 struct pci_dev *pdev = tp->pci_dev;
5266 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5268 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5269 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5270 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5271 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5273 rtl_csi_access_enable_1(tp);
5275 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5277 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5278 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5279 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
5281 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5282 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
5283 RTL_W8(MaxTxPacketSize, EarlySize);
5285 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5286 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5288 /* Adjust EEE LED frequency */
5289 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5291 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5292 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5294 rtl_pcie_state_l2l3_enable(tp, false);
5297 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5299 void __iomem *ioaddr = tp->mmio_addr;
5300 static const struct ephy_info e_info_8168g_2[] = {
5301 { 0x00, 0x0000, 0x0008 },
5302 { 0x0c, 0x3df0, 0x0200 },
5303 { 0x19, 0xffff, 0xfc00 },
5304 { 0x1e, 0xffff, 0x20eb }
5307 rtl_hw_start_8168g_1(tp);
5309 /* disable aspm and clock request before access ephy */
5310 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5311 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5312 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5315 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5317 void __iomem *ioaddr = tp->mmio_addr;
5318 static const struct ephy_info e_info_8411_2[] = {
5319 { 0x00, 0x0000, 0x0008 },
5320 { 0x0c, 0x3df0, 0x0200 },
5321 { 0x0f, 0xffff, 0x5200 },
5322 { 0x19, 0x0020, 0x0000 },
5323 { 0x1e, 0x0000, 0x2000 }
5326 rtl_hw_start_8168g_1(tp);
5328 /* disable aspm and clock request before access ephy */
5329 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5330 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5331 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5334 static void rtl_hw_start_8168(struct net_device *dev)
5336 struct rtl8169_private *tp = netdev_priv(dev);
5337 void __iomem *ioaddr = tp->mmio_addr;
5339 RTL_W8(Cfg9346, Cfg9346_Unlock);
5341 RTL_W8(MaxTxPacketSize, TxPacketMax);
5343 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5345 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
5347 RTL_W16(CPlusCmd, tp->cp_cmd);
5349 RTL_W16(IntrMitigate, 0x5151);
5351 /* Work around for RxFIFO overflow. */
5352 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5353 tp->event_slow |= RxFIFOOver | PCSTimeout;
5354 tp->event_slow &= ~RxOverflow;
5357 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5359 rtl_set_rx_tx_config_registers(tp);
5363 switch (tp->mac_version) {
5364 case RTL_GIGA_MAC_VER_11:
5365 rtl_hw_start_8168bb(tp);
5368 case RTL_GIGA_MAC_VER_12:
5369 case RTL_GIGA_MAC_VER_17:
5370 rtl_hw_start_8168bef(tp);
5373 case RTL_GIGA_MAC_VER_18:
5374 rtl_hw_start_8168cp_1(tp);
5377 case RTL_GIGA_MAC_VER_19:
5378 rtl_hw_start_8168c_1(tp);
5381 case RTL_GIGA_MAC_VER_20:
5382 rtl_hw_start_8168c_2(tp);
5385 case RTL_GIGA_MAC_VER_21:
5386 rtl_hw_start_8168c_3(tp);
5389 case RTL_GIGA_MAC_VER_22:
5390 rtl_hw_start_8168c_4(tp);
5393 case RTL_GIGA_MAC_VER_23:
5394 rtl_hw_start_8168cp_2(tp);
5397 case RTL_GIGA_MAC_VER_24:
5398 rtl_hw_start_8168cp_3(tp);
5401 case RTL_GIGA_MAC_VER_25:
5402 case RTL_GIGA_MAC_VER_26:
5403 case RTL_GIGA_MAC_VER_27:
5404 rtl_hw_start_8168d(tp);
5407 case RTL_GIGA_MAC_VER_28:
5408 rtl_hw_start_8168d_4(tp);
5411 case RTL_GIGA_MAC_VER_31:
5412 rtl_hw_start_8168dp(tp);
5415 case RTL_GIGA_MAC_VER_32:
5416 case RTL_GIGA_MAC_VER_33:
5417 rtl_hw_start_8168e_1(tp);
5419 case RTL_GIGA_MAC_VER_34:
5420 rtl_hw_start_8168e_2(tp);
5423 case RTL_GIGA_MAC_VER_35:
5424 case RTL_GIGA_MAC_VER_36:
5425 rtl_hw_start_8168f_1(tp);
5428 case RTL_GIGA_MAC_VER_38:
5429 rtl_hw_start_8411(tp);
5432 case RTL_GIGA_MAC_VER_40:
5433 case RTL_GIGA_MAC_VER_41:
5434 rtl_hw_start_8168g_1(tp);
5436 case RTL_GIGA_MAC_VER_42:
5437 rtl_hw_start_8168g_2(tp);
5440 case RTL_GIGA_MAC_VER_44:
5441 rtl_hw_start_8411_2(tp);
5445 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5446 dev->name, tp->mac_version);
5450 RTL_W8(Cfg9346, Cfg9346_Lock);
5452 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5454 rtl_set_rx_mode(dev);
5456 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
5459 #define R810X_CPCMD_QUIRK_MASK (\
5470 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5472 void __iomem *ioaddr = tp->mmio_addr;
5473 struct pci_dev *pdev = tp->pci_dev;
5474 static const struct ephy_info e_info_8102e_1[] = {
5475 { 0x01, 0, 0x6e65 },
5476 { 0x02, 0, 0x091f },
5477 { 0x03, 0, 0xc2f9 },
5478 { 0x06, 0, 0xafb5 },
5479 { 0x07, 0, 0x0e00 },
5480 { 0x19, 0, 0xec80 },
5481 { 0x01, 0, 0x2e65 },
5486 rtl_csi_access_enable_2(tp);
5488 RTL_W8(DBG_REG, FIX_NAK_1);
5490 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5493 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5494 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5496 cfg1 = RTL_R8(Config1);
5497 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5498 RTL_W8(Config1, cfg1 & ~LEDS0);
5500 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5503 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5505 void __iomem *ioaddr = tp->mmio_addr;
5506 struct pci_dev *pdev = tp->pci_dev;
5508 rtl_csi_access_enable_2(tp);
5510 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5512 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5513 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5516 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5518 rtl_hw_start_8102e_2(tp);
5520 rtl_ephy_write(tp, 0x03, 0xc2f9);
5523 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5525 void __iomem *ioaddr = tp->mmio_addr;
5526 static const struct ephy_info e_info_8105e_1[] = {
5527 { 0x07, 0, 0x4000 },
5528 { 0x19, 0, 0x0200 },
5529 { 0x19, 0, 0x0020 },
5530 { 0x1e, 0, 0x2000 },
5531 { 0x03, 0, 0x0001 },
5532 { 0x19, 0, 0x0100 },
5533 { 0x19, 0, 0x0004 },
5537 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5538 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5540 /* Disable Early Tally Counter */
5541 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5543 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5544 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5546 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5548 rtl_pcie_state_l2l3_enable(tp, false);
5551 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5553 rtl_hw_start_8105e_1(tp);
5554 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5557 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5559 void __iomem *ioaddr = tp->mmio_addr;
5560 static const struct ephy_info e_info_8402[] = {
5561 { 0x19, 0xffff, 0xff64 },
5565 rtl_csi_access_enable_2(tp);
5567 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5568 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5570 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5571 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5573 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
5575 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5577 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5578 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5579 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5580 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5581 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5582 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5583 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
5585 rtl_pcie_state_l2l3_enable(tp, false);
5588 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5590 void __iomem *ioaddr = tp->mmio_addr;
5592 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5593 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5595 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5596 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5597 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5599 rtl_pcie_state_l2l3_enable(tp, false);
5602 static void rtl_hw_start_8101(struct net_device *dev)
5604 struct rtl8169_private *tp = netdev_priv(dev);
5605 void __iomem *ioaddr = tp->mmio_addr;
5606 struct pci_dev *pdev = tp->pci_dev;
5608 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5609 tp->event_slow &= ~RxFIFOOver;
5611 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5612 tp->mac_version == RTL_GIGA_MAC_VER_16)
5613 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5614 PCI_EXP_DEVCTL_NOSNOOP_EN);
5616 RTL_W8(Cfg9346, Cfg9346_Unlock);
5618 RTL_W8(MaxTxPacketSize, TxPacketMax);
5620 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5622 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5623 RTL_W16(CPlusCmd, tp->cp_cmd);
5625 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5627 rtl_set_rx_tx_config_registers(tp);
5629 switch (tp->mac_version) {
5630 case RTL_GIGA_MAC_VER_07:
5631 rtl_hw_start_8102e_1(tp);
5634 case RTL_GIGA_MAC_VER_08:
5635 rtl_hw_start_8102e_3(tp);
5638 case RTL_GIGA_MAC_VER_09:
5639 rtl_hw_start_8102e_2(tp);
5642 case RTL_GIGA_MAC_VER_29:
5643 rtl_hw_start_8105e_1(tp);
5645 case RTL_GIGA_MAC_VER_30:
5646 rtl_hw_start_8105e_2(tp);
5649 case RTL_GIGA_MAC_VER_37:
5650 rtl_hw_start_8402(tp);
5653 case RTL_GIGA_MAC_VER_39:
5654 rtl_hw_start_8106(tp);
5656 case RTL_GIGA_MAC_VER_43:
5657 rtl_hw_start_8168g_2(tp);
5661 RTL_W8(Cfg9346, Cfg9346_Lock);
5663 RTL_W16(IntrMitigate, 0x0000);
5665 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5667 rtl_set_rx_mode(dev);
5671 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5674 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5676 struct rtl8169_private *tp = netdev_priv(dev);
5678 if (new_mtu < ETH_ZLEN ||
5679 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
5682 if (new_mtu > ETH_DATA_LEN)
5683 rtl_hw_jumbo_enable(tp);
5685 rtl_hw_jumbo_disable(tp);
5688 netdev_update_features(dev);
5693 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5695 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5696 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5699 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5700 void **data_buff, struct RxDesc *desc)
5702 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
5707 rtl8169_make_unusable_by_asic(desc);
5710 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5712 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5714 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5717 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5720 desc->addr = cpu_to_le64(mapping);
5722 rtl8169_mark_to_asic(desc, rx_buf_sz);
5725 static inline void *rtl8169_align(void *data)
5727 return (void *)ALIGN((long)data, 16);
5730 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5731 struct RxDesc *desc)
5735 struct device *d = &tp->pci_dev->dev;
5736 struct net_device *dev = tp->dev;
5737 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
5739 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5743 if (rtl8169_align(data) != data) {
5745 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5750 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
5752 if (unlikely(dma_mapping_error(d, mapping))) {
5753 if (net_ratelimit())
5754 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5758 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
5766 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5770 for (i = 0; i < NUM_RX_DESC; i++) {
5771 if (tp->Rx_databuff[i]) {
5772 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5773 tp->RxDescArray + i);
5778 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5780 desc->opts1 |= cpu_to_le32(RingEnd);
5783 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5787 for (i = 0; i < NUM_RX_DESC; i++) {
5790 if (tp->Rx_databuff[i])
5793 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5795 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5798 tp->Rx_databuff[i] = data;
5801 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5805 rtl8169_rx_clear(tp);
5809 static int rtl8169_init_ring(struct net_device *dev)
5811 struct rtl8169_private *tp = netdev_priv(dev);
5813 rtl8169_init_ring_indexes(tp);
5815 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
5816 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
5818 return rtl8169_rx_fill(tp);
5821 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5822 struct TxDesc *desc)
5824 unsigned int len = tx_skb->len;
5826 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5834 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5839 for (i = 0; i < n; i++) {
5840 unsigned int entry = (start + i) % NUM_TX_DESC;
5841 struct ring_info *tx_skb = tp->tx_skb + entry;
5842 unsigned int len = tx_skb->len;
5845 struct sk_buff *skb = tx_skb->skb;
5847 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5848 tp->TxDescArray + entry);
5850 tp->dev->stats.tx_dropped++;
5851 dev_kfree_skb_any(skb);
5858 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5860 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5861 tp->cur_tx = tp->dirty_tx = 0;
5864 static void rtl_reset_work(struct rtl8169_private *tp)
5866 struct net_device *dev = tp->dev;
5869 napi_disable(&tp->napi);
5870 netif_stop_queue(dev);
5871 synchronize_sched();
5873 rtl8169_hw_reset(tp);
5875 for (i = 0; i < NUM_RX_DESC; i++)
5876 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5878 rtl8169_tx_clear(tp);
5879 rtl8169_init_ring_indexes(tp);
5881 napi_enable(&tp->napi);
5883 netif_wake_queue(dev);
5884 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
5887 static void rtl8169_tx_timeout(struct net_device *dev)
5889 struct rtl8169_private *tp = netdev_priv(dev);
5891 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5894 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5897 struct skb_shared_info *info = skb_shinfo(skb);
5898 unsigned int cur_frag, entry;
5899 struct TxDesc * uninitialized_var(txd);
5900 struct device *d = &tp->pci_dev->dev;
5903 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5904 const skb_frag_t *frag = info->frags + cur_frag;
5909 entry = (entry + 1) % NUM_TX_DESC;
5911 txd = tp->TxDescArray + entry;
5912 len = skb_frag_size(frag);
5913 addr = skb_frag_address(frag);
5914 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5915 if (unlikely(dma_mapping_error(d, mapping))) {
5916 if (net_ratelimit())
5917 netif_err(tp, drv, tp->dev,
5918 "Failed to map TX fragments DMA!\n");
5922 /* Anti gcc 2.95.3 bugware (sic) */
5923 status = opts[0] | len |
5924 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5926 txd->opts1 = cpu_to_le32(status);
5927 txd->opts2 = cpu_to_le32(opts[1]);
5928 txd->addr = cpu_to_le64(mapping);
5930 tp->tx_skb[entry].len = len;
5934 tp->tx_skb[entry].skb = skb;
5935 txd->opts1 |= cpu_to_le32(LastFrag);
5941 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5945 static bool rtl_skb_pad(struct sk_buff *skb)
5947 if (skb_padto(skb, ETH_ZLEN))
5949 skb_put(skb, ETH_ZLEN - skb->len);
5953 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5955 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5958 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5959 struct net_device *dev);
5960 /* r8169_csum_workaround()
5961 * The hw limites the value the transport offset. When the offset is out of the
5962 * range, calculate the checksum by sw.
5964 static void r8169_csum_workaround(struct rtl8169_private *tp,
5965 struct sk_buff *skb)
5967 if (skb_shinfo(skb)->gso_size) {
5968 netdev_features_t features = tp->dev->features;
5969 struct sk_buff *segs, *nskb;
5971 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5972 segs = skb_gso_segment(skb, features);
5973 if (IS_ERR(segs) || !segs)
5980 rtl8169_start_xmit(nskb, tp->dev);
5984 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5985 if (skb_checksum_help(skb) < 0)
5988 rtl8169_start_xmit(skb, tp->dev);
5990 struct net_device_stats *stats;
5993 stats = &tp->dev->stats;
5994 stats->tx_dropped++;
5999 /* msdn_giant_send_check()
6000 * According to the document of microsoft, the TCP Pseudo Header excludes the
6001 * packet length for IPv6 TCP large packets.
6003 static int msdn_giant_send_check(struct sk_buff *skb)
6005 const struct ipv6hdr *ipv6h;
6009 ret = skb_cow_head(skb, 0);
6013 ipv6h = ipv6_hdr(skb);
6017 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6022 static inline __be16 get_protocol(struct sk_buff *skb)
6026 if (skb->protocol == htons(ETH_P_8021Q))
6027 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6029 protocol = skb->protocol;
6034 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6035 struct sk_buff *skb, u32 *opts)
6037 u32 mss = skb_shinfo(skb)->gso_size;
6041 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6042 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6043 const struct iphdr *ip = ip_hdr(skb);
6045 if (ip->protocol == IPPROTO_TCP)
6046 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6047 else if (ip->protocol == IPPROTO_UDP)
6048 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6056 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6057 struct sk_buff *skb, u32 *opts)
6059 u32 transport_offset = (u32)skb_transport_offset(skb);
6060 u32 mss = skb_shinfo(skb)->gso_size;
6063 if (transport_offset > GTTCPHO_MAX) {
6064 netif_warn(tp, tx_err, tp->dev,
6065 "Invalid transport offset 0x%x for TSO\n",
6070 switch (get_protocol(skb)) {
6071 case htons(ETH_P_IP):
6072 opts[0] |= TD1_GTSENV4;
6075 case htons(ETH_P_IPV6):
6076 if (msdn_giant_send_check(skb))
6079 opts[0] |= TD1_GTSENV6;
6087 opts[0] |= transport_offset << GTTCPHO_SHIFT;
6088 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
6089 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6092 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6093 return skb_checksum_help(skb) == 0 && rtl_skb_pad(skb);
6095 if (transport_offset > TCPHO_MAX) {
6096 netif_warn(tp, tx_err, tp->dev,
6097 "Invalid transport offset 0x%x\n",
6102 switch (get_protocol(skb)) {
6103 case htons(ETH_P_IP):
6104 opts[1] |= TD1_IPv4_CS;
6105 ip_protocol = ip_hdr(skb)->protocol;
6108 case htons(ETH_P_IPV6):
6109 opts[1] |= TD1_IPv6_CS;
6110 ip_protocol = ipv6_hdr(skb)->nexthdr;
6114 ip_protocol = IPPROTO_RAW;
6118 if (ip_protocol == IPPROTO_TCP)
6119 opts[1] |= TD1_TCP_CS;
6120 else if (ip_protocol == IPPROTO_UDP)
6121 opts[1] |= TD1_UDP_CS;
6125 opts[1] |= transport_offset << TCPHO_SHIFT;
6127 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6128 return rtl_skb_pad(skb);
6134 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6135 struct net_device *dev)
6137 struct rtl8169_private *tp = netdev_priv(dev);
6138 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
6139 struct TxDesc *txd = tp->TxDescArray + entry;
6140 void __iomem *ioaddr = tp->mmio_addr;
6141 struct device *d = &tp->pci_dev->dev;
6147 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
6148 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
6152 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
6155 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6158 if (!tp->tso_csum(tp, skb, opts)) {
6159 r8169_csum_workaround(tp, skb);
6160 return NETDEV_TX_OK;
6163 len = skb_headlen(skb);
6164 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
6165 if (unlikely(dma_mapping_error(d, mapping))) {
6166 if (net_ratelimit())
6167 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
6171 tp->tx_skb[entry].len = len;
6172 txd->addr = cpu_to_le64(mapping);
6174 frags = rtl8169_xmit_frags(tp, skb, opts);
6178 opts[0] |= FirstFrag;
6180 opts[0] |= FirstFrag | LastFrag;
6181 tp->tx_skb[entry].skb = skb;
6184 txd->opts2 = cpu_to_le32(opts[1]);
6186 skb_tx_timestamp(skb);
6190 /* Anti gcc 2.95.3 bugware (sic) */
6191 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
6192 txd->opts1 = cpu_to_le32(status);
6194 tp->cur_tx += frags + 1;
6198 RTL_W8(TxPoll, NPQ);
6202 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6203 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6204 * not miss a ring update when it notices a stopped queue.
6207 netif_stop_queue(dev);
6208 /* Sync with rtl_tx:
6209 * - publish queue status and cur_tx ring index (write barrier)
6210 * - refresh dirty_tx ring index (read barrier).
6211 * May the current thread have a pessimistic view of the ring
6212 * status and forget to wake up queue, a racing rtl_tx thread
6216 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
6217 netif_wake_queue(dev);
6220 return NETDEV_TX_OK;
6223 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
6225 dev_kfree_skb_any(skb);
6226 dev->stats.tx_dropped++;
6227 return NETDEV_TX_OK;
6230 netif_stop_queue(dev);
6231 dev->stats.tx_dropped++;
6232 return NETDEV_TX_BUSY;
6235 static void rtl8169_pcierr_interrupt(struct net_device *dev)
6237 struct rtl8169_private *tp = netdev_priv(dev);
6238 struct pci_dev *pdev = tp->pci_dev;
6239 u16 pci_status, pci_cmd;
6241 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6242 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6244 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6245 pci_cmd, pci_status);
6248 * The recovery sequence below admits a very elaborated explanation:
6249 * - it seems to work;
6250 * - I did not see what else could be done;
6251 * - it makes iop3xx happy.
6253 * Feel free to adjust to your needs.
6255 if (pdev->broken_parity_status)
6256 pci_cmd &= ~PCI_COMMAND_PARITY;
6258 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6260 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
6262 pci_write_config_word(pdev, PCI_STATUS,
6263 pci_status & (PCI_STATUS_DETECTED_PARITY |
6264 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6265 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6267 /* The infamous DAC f*ckup only happens at boot time */
6268 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
6269 void __iomem *ioaddr = tp->mmio_addr;
6271 netif_info(tp, intr, dev, "disabling PCI DAC\n");
6272 tp->cp_cmd &= ~PCIDAC;
6273 RTL_W16(CPlusCmd, tp->cp_cmd);
6274 dev->features &= ~NETIF_F_HIGHDMA;
6277 rtl8169_hw_reset(tp);
6279 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6282 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
6284 unsigned int dirty_tx, tx_left;
6286 dirty_tx = tp->dirty_tx;
6288 tx_left = tp->cur_tx - dirty_tx;
6290 while (tx_left > 0) {
6291 unsigned int entry = dirty_tx % NUM_TX_DESC;
6292 struct ring_info *tx_skb = tp->tx_skb + entry;
6296 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6297 if (status & DescOwn)
6300 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
6301 tp->TxDescArray + entry);
6302 if (status & LastFrag) {
6303 u64_stats_update_begin(&tp->tx_stats.syncp);
6304 tp->tx_stats.packets++;
6305 tp->tx_stats.bytes += tx_skb->skb->len;
6306 u64_stats_update_end(&tp->tx_stats.syncp);
6307 dev_kfree_skb_any(tx_skb->skb);
6314 if (tp->dirty_tx != dirty_tx) {
6315 tp->dirty_tx = dirty_tx;
6316 /* Sync with rtl8169_start_xmit:
6317 * - publish dirty_tx ring index (write barrier)
6318 * - refresh cur_tx ring index and queue status (read barrier)
6319 * May the current thread miss the stopped queue condition,
6320 * a racing xmit thread can only have a right view of the
6324 if (netif_queue_stopped(dev) &&
6325 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6326 netif_wake_queue(dev);
6329 * 8168 hack: TxPoll requests are lost when the Tx packets are
6330 * too close. Let's kick an extra TxPoll request when a burst
6331 * of start_xmit activity is detected (if it is not detected,
6332 * it is slow enough). -- FR
6334 if (tp->cur_tx != dirty_tx) {
6335 void __iomem *ioaddr = tp->mmio_addr;
6337 RTL_W8(TxPoll, NPQ);
6342 static inline int rtl8169_fragmented_frame(u32 status)
6344 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6347 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
6349 u32 status = opts1 & RxProtoMask;
6351 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
6352 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
6353 skb->ip_summed = CHECKSUM_UNNECESSARY;
6355 skb_checksum_none_assert(skb);
6358 static struct sk_buff *rtl8169_try_rx_copy(void *data,
6359 struct rtl8169_private *tp,
6363 struct sk_buff *skb;
6364 struct device *d = &tp->pci_dev->dev;
6366 data = rtl8169_align(data);
6367 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6369 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6371 memcpy(skb->data, data, pkt_size);
6372 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6377 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
6379 unsigned int cur_rx, rx_left;
6382 cur_rx = tp->cur_rx;
6384 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
6385 unsigned int entry = cur_rx % NUM_RX_DESC;
6386 struct RxDesc *desc = tp->RxDescArray + entry;
6390 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
6392 if (status & DescOwn)
6394 if (unlikely(status & RxRES)) {
6395 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6397 dev->stats.rx_errors++;
6398 if (status & (RxRWT | RxRUNT))
6399 dev->stats.rx_length_errors++;
6401 dev->stats.rx_crc_errors++;
6402 if (status & RxFOVF) {
6403 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6404 dev->stats.rx_fifo_errors++;
6406 if ((status & (RxRUNT | RxCRC)) &&
6407 !(status & (RxRWT | RxFOVF)) &&
6408 (dev->features & NETIF_F_RXALL))
6411 struct sk_buff *skb;
6416 addr = le64_to_cpu(desc->addr);
6417 if (likely(!(dev->features & NETIF_F_RXFCS)))
6418 pkt_size = (status & 0x00003fff) - 4;
6420 pkt_size = status & 0x00003fff;
6423 * The driver does not support incoming fragmented
6424 * frames. They are seen as a symptom of over-mtu
6427 if (unlikely(rtl8169_fragmented_frame(status))) {
6428 dev->stats.rx_dropped++;
6429 dev->stats.rx_length_errors++;
6430 goto release_descriptor;
6433 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6434 tp, pkt_size, addr);
6436 dev->stats.rx_dropped++;
6437 goto release_descriptor;
6440 rtl8169_rx_csum(skb, status);
6441 skb_put(skb, pkt_size);
6442 skb->protocol = eth_type_trans(skb, dev);
6444 rtl8169_rx_vlan_tag(desc, skb);
6446 napi_gro_receive(&tp->napi, skb);
6448 u64_stats_update_begin(&tp->rx_stats.syncp);
6449 tp->rx_stats.packets++;
6450 tp->rx_stats.bytes += pkt_size;
6451 u64_stats_update_end(&tp->rx_stats.syncp);
6456 rtl8169_mark_to_asic(desc, rx_buf_sz);
6459 count = cur_rx - tp->cur_rx;
6460 tp->cur_rx = cur_rx;
6465 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6467 struct net_device *dev = dev_instance;
6468 struct rtl8169_private *tp = netdev_priv(dev);
6472 status = rtl_get_events(tp);
6473 if (status && status != 0xffff) {
6474 status &= RTL_EVENT_NAPI | tp->event_slow;
6478 rtl_irq_disable(tp);
6479 napi_schedule(&tp->napi);
6482 return IRQ_RETVAL(handled);
6486 * Workqueue context.
6488 static void rtl_slow_event_work(struct rtl8169_private *tp)
6490 struct net_device *dev = tp->dev;
6493 status = rtl_get_events(tp) & tp->event_slow;
6494 rtl_ack_events(tp, status);
6496 if (unlikely(status & RxFIFOOver)) {
6497 switch (tp->mac_version) {
6498 /* Work around for rx fifo overflow */
6499 case RTL_GIGA_MAC_VER_11:
6500 netif_stop_queue(dev);
6501 /* XXX - Hack alert. See rtl_task(). */
6502 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6508 if (unlikely(status & SYSErr))
6509 rtl8169_pcierr_interrupt(dev);
6511 if (status & LinkChg)
6512 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6514 rtl_irq_enable_all(tp);
6517 static void rtl_task(struct work_struct *work)
6519 static const struct {
6521 void (*action)(struct rtl8169_private *);
6523 /* XXX - keep rtl_slow_event_work() as first element. */
6524 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6525 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6526 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6528 struct rtl8169_private *tp =
6529 container_of(work, struct rtl8169_private, wk.work);
6530 struct net_device *dev = tp->dev;
6535 if (!netif_running(dev) ||
6536 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6539 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6542 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6544 rtl_work[i].action(tp);
6548 rtl_unlock_work(tp);
6551 static int rtl8169_poll(struct napi_struct *napi, int budget)
6553 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6554 struct net_device *dev = tp->dev;
6555 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6559 status = rtl_get_events(tp);
6560 rtl_ack_events(tp, status & ~tp->event_slow);
6562 if (status & RTL_EVENT_NAPI_RX)
6563 work_done = rtl_rx(dev, tp, (u32) budget);
6565 if (status & RTL_EVENT_NAPI_TX)
6568 if (status & tp->event_slow) {
6569 enable_mask &= ~tp->event_slow;
6571 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6574 if (work_done < budget) {
6575 napi_complete(napi);
6577 rtl_irq_enable(tp, enable_mask);
6584 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6586 struct rtl8169_private *tp = netdev_priv(dev);
6588 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6591 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6592 RTL_W32(RxMissed, 0);
6595 static void rtl8169_down(struct net_device *dev)
6597 struct rtl8169_private *tp = netdev_priv(dev);
6598 void __iomem *ioaddr = tp->mmio_addr;
6600 del_timer_sync(&tp->timer);
6602 napi_disable(&tp->napi);
6603 netif_stop_queue(dev);
6605 rtl8169_hw_reset(tp);
6607 * At this point device interrupts can not be enabled in any function,
6608 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6609 * and napi is disabled (rtl8169_poll).
6611 rtl8169_rx_missed(dev, ioaddr);
6613 /* Give a racing hard_start_xmit a few cycles to complete. */
6614 synchronize_sched();
6616 rtl8169_tx_clear(tp);
6618 rtl8169_rx_clear(tp);
6620 rtl_pll_power_down(tp);
6623 static int rtl8169_close(struct net_device *dev)
6625 struct rtl8169_private *tp = netdev_priv(dev);
6626 struct pci_dev *pdev = tp->pci_dev;
6628 pm_runtime_get_sync(&pdev->dev);
6630 /* Update counters before going down */
6631 rtl8169_update_counters(dev);
6634 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6637 rtl_unlock_work(tp);
6639 cancel_work_sync(&tp->wk.work);
6641 free_irq(pdev->irq, dev);
6643 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6645 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6647 tp->TxDescArray = NULL;
6648 tp->RxDescArray = NULL;
6650 pm_runtime_put_sync(&pdev->dev);
6655 #ifdef CONFIG_NET_POLL_CONTROLLER
6656 static void rtl8169_netpoll(struct net_device *dev)
6658 struct rtl8169_private *tp = netdev_priv(dev);
6660 rtl8169_interrupt(tp->pci_dev->irq, dev);
6664 static int rtl_open(struct net_device *dev)
6666 struct rtl8169_private *tp = netdev_priv(dev);
6667 void __iomem *ioaddr = tp->mmio_addr;
6668 struct pci_dev *pdev = tp->pci_dev;
6669 int retval = -ENOMEM;
6671 pm_runtime_get_sync(&pdev->dev);
6674 * Rx and Tx descriptors needs 256 bytes alignment.
6675 * dma_alloc_coherent provides more.
6677 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6678 &tp->TxPhyAddr, GFP_KERNEL);
6679 if (!tp->TxDescArray)
6680 goto err_pm_runtime_put;
6682 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6683 &tp->RxPhyAddr, GFP_KERNEL);
6684 if (!tp->RxDescArray)
6687 retval = rtl8169_init_ring(dev);
6691 INIT_WORK(&tp->wk.work, rtl_task);
6695 rtl_request_firmware(tp);
6697 retval = request_irq(pdev->irq, rtl8169_interrupt,
6698 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6701 goto err_release_fw_2;
6705 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6707 napi_enable(&tp->napi);
6709 rtl8169_init_phy(dev, tp);
6711 __rtl8169_set_features(dev, dev->features);
6713 rtl_pll_power_up(tp);
6717 netif_start_queue(dev);
6719 rtl_unlock_work(tp);
6721 tp->saved_wolopts = 0;
6722 pm_runtime_put_noidle(&pdev->dev);
6724 rtl8169_check_link_status(dev, tp, ioaddr);
6729 rtl_release_firmware(tp);
6730 rtl8169_rx_clear(tp);
6732 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6734 tp->RxDescArray = NULL;
6736 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6738 tp->TxDescArray = NULL;
6740 pm_runtime_put_noidle(&pdev->dev);
6744 static struct rtnl_link_stats64 *
6745 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6747 struct rtl8169_private *tp = netdev_priv(dev);
6748 void __iomem *ioaddr = tp->mmio_addr;
6751 if (netif_running(dev))
6752 rtl8169_rx_missed(dev, ioaddr);
6755 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
6756 stats->rx_packets = tp->rx_stats.packets;
6757 stats->rx_bytes = tp->rx_stats.bytes;
6758 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
6762 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
6763 stats->tx_packets = tp->tx_stats.packets;
6764 stats->tx_bytes = tp->tx_stats.bytes;
6765 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
6767 stats->rx_dropped = dev->stats.rx_dropped;
6768 stats->tx_dropped = dev->stats.tx_dropped;
6769 stats->rx_length_errors = dev->stats.rx_length_errors;
6770 stats->rx_errors = dev->stats.rx_errors;
6771 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6772 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6773 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6778 static void rtl8169_net_suspend(struct net_device *dev)
6780 struct rtl8169_private *tp = netdev_priv(dev);
6782 if (!netif_running(dev))
6785 netif_device_detach(dev);
6786 netif_stop_queue(dev);
6789 napi_disable(&tp->napi);
6790 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6791 rtl_unlock_work(tp);
6793 rtl_pll_power_down(tp);
6798 static int rtl8169_suspend(struct device *device)
6800 struct pci_dev *pdev = to_pci_dev(device);
6801 struct net_device *dev = pci_get_drvdata(pdev);
6803 rtl8169_net_suspend(dev);
6808 static void __rtl8169_resume(struct net_device *dev)
6810 struct rtl8169_private *tp = netdev_priv(dev);
6812 netif_device_attach(dev);
6814 rtl_pll_power_up(tp);
6817 napi_enable(&tp->napi);
6818 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6819 rtl_unlock_work(tp);
6821 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6824 static int rtl8169_resume(struct device *device)
6826 struct pci_dev *pdev = to_pci_dev(device);
6827 struct net_device *dev = pci_get_drvdata(pdev);
6828 struct rtl8169_private *tp = netdev_priv(dev);
6830 rtl8169_init_phy(dev, tp);
6832 if (netif_running(dev))
6833 __rtl8169_resume(dev);
6838 static int rtl8169_runtime_suspend(struct device *device)
6840 struct pci_dev *pdev = to_pci_dev(device);
6841 struct net_device *dev = pci_get_drvdata(pdev);
6842 struct rtl8169_private *tp = netdev_priv(dev);
6844 if (!tp->TxDescArray)
6848 tp->saved_wolopts = __rtl8169_get_wol(tp);
6849 __rtl8169_set_wol(tp, WAKE_ANY);
6850 rtl_unlock_work(tp);
6852 rtl8169_net_suspend(dev);
6857 static int rtl8169_runtime_resume(struct device *device)
6859 struct pci_dev *pdev = to_pci_dev(device);
6860 struct net_device *dev = pci_get_drvdata(pdev);
6861 struct rtl8169_private *tp = netdev_priv(dev);
6863 if (!tp->TxDescArray)
6867 __rtl8169_set_wol(tp, tp->saved_wolopts);
6868 tp->saved_wolopts = 0;
6869 rtl_unlock_work(tp);
6871 rtl8169_init_phy(dev, tp);
6873 __rtl8169_resume(dev);
6878 static int rtl8169_runtime_idle(struct device *device)
6880 struct pci_dev *pdev = to_pci_dev(device);
6881 struct net_device *dev = pci_get_drvdata(pdev);
6882 struct rtl8169_private *tp = netdev_priv(dev);
6884 return tp->TxDescArray ? -EBUSY : 0;
6887 static const struct dev_pm_ops rtl8169_pm_ops = {
6888 .suspend = rtl8169_suspend,
6889 .resume = rtl8169_resume,
6890 .freeze = rtl8169_suspend,
6891 .thaw = rtl8169_resume,
6892 .poweroff = rtl8169_suspend,
6893 .restore = rtl8169_resume,
6894 .runtime_suspend = rtl8169_runtime_suspend,
6895 .runtime_resume = rtl8169_runtime_resume,
6896 .runtime_idle = rtl8169_runtime_idle,
6899 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6901 #else /* !CONFIG_PM */
6903 #define RTL8169_PM_OPS NULL
6905 #endif /* !CONFIG_PM */
6907 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6909 void __iomem *ioaddr = tp->mmio_addr;
6911 /* WoL fails with 8168b when the receiver is disabled. */
6912 switch (tp->mac_version) {
6913 case RTL_GIGA_MAC_VER_11:
6914 case RTL_GIGA_MAC_VER_12:
6915 case RTL_GIGA_MAC_VER_17:
6916 pci_clear_master(tp->pci_dev);
6918 RTL_W8(ChipCmd, CmdRxEnb);
6927 static void rtl_shutdown(struct pci_dev *pdev)
6929 struct net_device *dev = pci_get_drvdata(pdev);
6930 struct rtl8169_private *tp = netdev_priv(dev);
6931 struct device *d = &pdev->dev;
6933 pm_runtime_get_sync(d);
6935 rtl8169_net_suspend(dev);
6937 /* Restore original MAC address */
6938 rtl_rar_set(tp, dev->perm_addr);
6940 rtl8169_hw_reset(tp);
6942 if (system_state == SYSTEM_POWER_OFF) {
6943 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6944 rtl_wol_suspend_quirk(tp);
6945 rtl_wol_shutdown_quirk(tp);
6948 pci_wake_from_d3(pdev, true);
6949 pci_set_power_state(pdev, PCI_D3hot);
6952 pm_runtime_put_noidle(d);
6955 static void rtl_remove_one(struct pci_dev *pdev)
6957 struct net_device *dev = pci_get_drvdata(pdev);
6958 struct rtl8169_private *tp = netdev_priv(dev);
6960 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6961 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6962 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6963 rtl8168_driver_stop(tp);
6966 netif_napi_del(&tp->napi);
6968 unregister_netdev(dev);
6970 rtl_release_firmware(tp);
6972 if (pci_dev_run_wake(pdev))
6973 pm_runtime_get_noresume(&pdev->dev);
6975 /* restore original MAC address */
6976 rtl_rar_set(tp, dev->perm_addr);
6978 rtl_disable_msi(pdev, tp);
6979 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6982 static const struct net_device_ops rtl_netdev_ops = {
6983 .ndo_open = rtl_open,
6984 .ndo_stop = rtl8169_close,
6985 .ndo_get_stats64 = rtl8169_get_stats64,
6986 .ndo_start_xmit = rtl8169_start_xmit,
6987 .ndo_tx_timeout = rtl8169_tx_timeout,
6988 .ndo_validate_addr = eth_validate_addr,
6989 .ndo_change_mtu = rtl8169_change_mtu,
6990 .ndo_fix_features = rtl8169_fix_features,
6991 .ndo_set_features = rtl8169_set_features,
6992 .ndo_set_mac_address = rtl_set_mac_address,
6993 .ndo_do_ioctl = rtl8169_ioctl,
6994 .ndo_set_rx_mode = rtl_set_rx_mode,
6995 #ifdef CONFIG_NET_POLL_CONTROLLER
6996 .ndo_poll_controller = rtl8169_netpoll,
7001 static const struct rtl_cfg_info {
7002 void (*hw_start)(struct net_device *);
7003 unsigned int region;
7008 } rtl_cfg_infos [] = {
7010 .hw_start = rtl_hw_start_8169,
7013 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7014 .features = RTL_FEATURE_GMII,
7015 .default_ver = RTL_GIGA_MAC_VER_01,
7018 .hw_start = rtl_hw_start_8168,
7021 .event_slow = SYSErr | LinkChg | RxOverflow,
7022 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
7023 .default_ver = RTL_GIGA_MAC_VER_11,
7026 .hw_start = rtl_hw_start_8101,
7029 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7031 .features = RTL_FEATURE_MSI,
7032 .default_ver = RTL_GIGA_MAC_VER_13,
7036 /* Cfg9346_Unlock assumed. */
7037 static unsigned rtl_try_msi(struct rtl8169_private *tp,
7038 const struct rtl_cfg_info *cfg)
7040 void __iomem *ioaddr = tp->mmio_addr;
7044 cfg2 = RTL_R8(Config2) & ~MSIEnable;
7045 if (cfg->features & RTL_FEATURE_MSI) {
7046 if (pci_enable_msi(tp->pci_dev)) {
7047 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
7050 msi = RTL_FEATURE_MSI;
7053 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
7054 RTL_W8(Config2, cfg2);
7058 DECLARE_RTL_COND(rtl_link_list_ready_cond)
7060 void __iomem *ioaddr = tp->mmio_addr;
7062 return RTL_R8(MCU) & LINK_LIST_RDY;
7065 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7067 void __iomem *ioaddr = tp->mmio_addr;
7069 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7072 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
7074 void __iomem *ioaddr = tp->mmio_addr;
7077 tp->ocp_base = OCP_STD_PHY_BASE;
7079 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
7081 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7084 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7087 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7089 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
7091 data = r8168_mac_ocp_read(tp, 0xe8de);
7093 r8168_mac_ocp_write(tp, 0xe8de, data);
7095 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7098 data = r8168_mac_ocp_read(tp, 0xe8de);
7100 r8168_mac_ocp_write(tp, 0xe8de, data);
7102 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7106 static void rtl_hw_initialize(struct rtl8169_private *tp)
7108 switch (tp->mac_version) {
7109 case RTL_GIGA_MAC_VER_40:
7110 case RTL_GIGA_MAC_VER_41:
7111 case RTL_GIGA_MAC_VER_42:
7112 case RTL_GIGA_MAC_VER_43:
7113 case RTL_GIGA_MAC_VER_44:
7114 rtl_hw_init_8168g(tp);
7122 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7124 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7125 const unsigned int region = cfg->region;
7126 struct rtl8169_private *tp;
7127 struct mii_if_info *mii;
7128 struct net_device *dev;
7129 void __iomem *ioaddr;
7133 if (netif_msg_drv(&debug)) {
7134 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7135 MODULENAME, RTL8169_VERSION);
7138 dev = alloc_etherdev(sizeof (*tp));
7144 SET_NETDEV_DEV(dev, &pdev->dev);
7145 dev->netdev_ops = &rtl_netdev_ops;
7146 tp = netdev_priv(dev);
7149 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7153 mii->mdio_read = rtl_mdio_read;
7154 mii->mdio_write = rtl_mdio_write;
7155 mii->phy_id_mask = 0x1f;
7156 mii->reg_num_mask = 0x1f;
7157 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
7159 /* disable ASPM completely as that cause random device stop working
7160 * problems as well as full system hangs for some PCIe devices users */
7161 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7162 PCIE_LINK_STATE_CLKPM);
7164 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7165 rc = pci_enable_device(pdev);
7167 netif_err(tp, probe, dev, "enable failure\n");
7168 goto err_out_free_dev_1;
7171 if (pci_set_mwi(pdev) < 0)
7172 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
7174 /* make sure PCI base addr 1 is MMIO */
7175 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
7176 netif_err(tp, probe, dev,
7177 "region #%d not an MMIO resource, aborting\n",
7183 /* check for weird/broken PCI region reporting */
7184 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7185 netif_err(tp, probe, dev,
7186 "Invalid PCI region size(s), aborting\n");
7191 rc = pci_request_regions(pdev, MODULENAME);
7193 netif_err(tp, probe, dev, "could not request regions\n");
7199 if ((sizeof(dma_addr_t) > 4) &&
7200 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
7201 tp->cp_cmd |= PCIDAC;
7202 dev->features |= NETIF_F_HIGHDMA;
7204 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7206 netif_err(tp, probe, dev, "DMA configuration failed\n");
7207 goto err_out_free_res_3;
7211 /* ioremap MMIO region */
7212 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
7214 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
7216 goto err_out_free_res_3;
7218 tp->mmio_addr = ioaddr;
7220 if (!pci_is_pcie(pdev))
7221 netif_info(tp, probe, dev, "not PCI Express\n");
7223 /* Identify chip attached to board */
7224 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
7228 rtl_irq_disable(tp);
7230 rtl_hw_initialize(tp);
7234 rtl_ack_events(tp, 0xffff);
7236 pci_set_master(pdev);
7238 rtl_init_mdio_ops(tp);
7239 rtl_init_pll_power_ops(tp);
7240 rtl_init_jumbo_ops(tp);
7241 rtl_init_csi_ops(tp);
7243 rtl8169_print_mac_version(tp);
7245 chipset = tp->mac_version;
7246 tp->txd_version = rtl_chip_infos[chipset].txd_version;
7248 RTL_W8(Cfg9346, Cfg9346_Unlock);
7249 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
7250 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
7251 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
7252 tp->features |= RTL_FEATURE_WOL;
7253 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
7254 tp->features |= RTL_FEATURE_WOL;
7255 tp->features |= rtl_try_msi(tp, cfg);
7256 RTL_W8(Cfg9346, Cfg9346_Lock);
7258 if (rtl_tbi_enabled(tp)) {
7259 tp->set_speed = rtl8169_set_speed_tbi;
7260 tp->get_settings = rtl8169_gset_tbi;
7261 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7262 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7263 tp->link_ok = rtl8169_tbi_link_ok;
7264 tp->do_ioctl = rtl_tbi_ioctl;
7266 tp->set_speed = rtl8169_set_speed_xmii;
7267 tp->get_settings = rtl8169_gset_xmii;
7268 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7269 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7270 tp->link_ok = rtl8169_xmii_link_ok;
7271 tp->do_ioctl = rtl_xmii_ioctl;
7274 mutex_init(&tp->wk.mutex);
7275 u64_stats_init(&tp->rx_stats.syncp);
7276 u64_stats_init(&tp->tx_stats.syncp);
7278 /* Get MAC address */
7279 for (i = 0; i < ETH_ALEN; i++)
7280 dev->dev_addr[i] = RTL_R8(MAC0 + i);
7282 dev->ethtool_ops = &rtl8169_ethtool_ops;
7283 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
7285 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
7287 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7288 * properly for all devices */
7289 dev->features |= NETIF_F_RXCSUM |
7290 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
7292 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7293 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7294 NETIF_F_HW_VLAN_CTAG_RX;
7295 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7298 tp->cp_cmd |= RxChkSum | RxVlan;
7301 * Pretend we are using VLANs; This bypasses a nasty bug where
7302 * Interrupts stop flowing on high load on 8110SCd controllers.
7304 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7305 /* Disallow toggling */
7306 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
7308 if (tp->txd_version == RTL_TD_0)
7309 tp->tso_csum = rtl8169_tso_csum_v1;
7310 else if (tp->txd_version == RTL_TD_1) {
7311 tp->tso_csum = rtl8169_tso_csum_v2;
7312 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7316 dev->hw_features |= NETIF_F_RXALL;
7317 dev->hw_features |= NETIF_F_RXFCS;
7319 tp->hw_start = cfg->hw_start;
7320 tp->event_slow = cfg->event_slow;
7322 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
7323 ~(RxBOVF | RxFOVF) : ~0;
7325 init_timer(&tp->timer);
7326 tp->timer.data = (unsigned long) dev;
7327 tp->timer.function = rtl8169_phy_timer;
7329 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7331 rc = register_netdev(dev);
7335 pci_set_drvdata(pdev, dev);
7337 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
7338 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
7339 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
7340 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7341 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7342 "tx checksumming: %s]\n",
7343 rtl_chip_infos[chipset].jumbo_max,
7344 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
7347 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7348 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
7349 tp->mac_version == RTL_GIGA_MAC_VER_31) {
7350 rtl8168_driver_start(tp);
7353 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
7355 if (pci_dev_run_wake(pdev))
7356 pm_runtime_put_noidle(&pdev->dev);
7358 netif_carrier_off(dev);
7364 netif_napi_del(&tp->napi);
7365 rtl_disable_msi(pdev, tp);
7368 pci_release_regions(pdev);
7370 pci_clear_mwi(pdev);
7371 pci_disable_device(pdev);
7377 static struct pci_driver rtl8169_pci_driver = {
7379 .id_table = rtl8169_pci_tbl,
7380 .probe = rtl_init_one,
7381 .remove = rtl_remove_one,
7382 .shutdown = rtl_shutdown,
7383 .driver.pm = RTL8169_PM_OPS,
7386 module_pci_driver(rtl8169_pci_driver);