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>
34 #define RTL8169_VERSION "2.3LK-NAPI"
35 #define MODULENAME "r8169"
36 #define PFX MODULENAME ": "
38 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
40 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
42 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
43 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
44 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
45 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
46 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
47 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
48 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
49 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
50 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
51 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
52 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
55 #define assert(expr) \
57 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
58 #expr,__FILE__,__func__,__LINE__); \
60 #define dprintk(fmt, args...) \
61 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
63 #define assert(expr) do {} while (0)
64 #define dprintk(fmt, args...) do {} while (0)
65 #endif /* RTL8169_DEBUG */
67 #define R8169_MSG_DEFAULT \
68 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
70 #define TX_SLOTS_AVAIL(tp) \
71 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
73 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
74 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
75 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
77 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
78 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
79 static const int multicast_filter_limit = 32;
81 #define MAX_READ_REQUEST_SHIFT 12
82 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
83 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
85 #define R8169_REGS_SIZE 256
86 #define R8169_NAPI_WEIGHT 64
87 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
88 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
89 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
90 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
92 #define RTL8169_TX_TIMEOUT (6*HZ)
93 #define RTL8169_PHY_TIMEOUT (10*HZ)
95 /* write/read MMIO register */
96 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
97 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
98 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
99 #define RTL_R8(reg) readb (ioaddr + (reg))
100 #define RTL_R16(reg) readw (ioaddr + (reg))
101 #define RTL_R32(reg) readl (ioaddr + (reg))
104 RTL_GIGA_MAC_VER_01 = 0,
148 RTL_GIGA_MAC_NONE = 0xff,
151 enum rtl_tx_desc_version {
156 #define JUMBO_1K ETH_DATA_LEN
157 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
158 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
159 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
160 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
162 #define _R(NAME,TD,FW,SZ,B) { \
170 static const struct {
172 enum rtl_tx_desc_version txd_version;
176 } rtl_chip_infos[] = {
178 [RTL_GIGA_MAC_VER_01] =
179 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
180 [RTL_GIGA_MAC_VER_02] =
181 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
182 [RTL_GIGA_MAC_VER_03] =
183 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
184 [RTL_GIGA_MAC_VER_04] =
185 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
186 [RTL_GIGA_MAC_VER_05] =
187 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
188 [RTL_GIGA_MAC_VER_06] =
189 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
191 [RTL_GIGA_MAC_VER_07] =
192 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
193 [RTL_GIGA_MAC_VER_08] =
194 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
195 [RTL_GIGA_MAC_VER_09] =
196 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
197 [RTL_GIGA_MAC_VER_10] =
198 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
199 [RTL_GIGA_MAC_VER_11] =
200 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
201 [RTL_GIGA_MAC_VER_12] =
202 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
203 [RTL_GIGA_MAC_VER_13] =
204 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
205 [RTL_GIGA_MAC_VER_14] =
206 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
207 [RTL_GIGA_MAC_VER_15] =
208 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
209 [RTL_GIGA_MAC_VER_16] =
210 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
211 [RTL_GIGA_MAC_VER_17] =
212 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
213 [RTL_GIGA_MAC_VER_18] =
214 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
215 [RTL_GIGA_MAC_VER_19] =
216 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
217 [RTL_GIGA_MAC_VER_20] =
218 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
219 [RTL_GIGA_MAC_VER_21] =
220 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
221 [RTL_GIGA_MAC_VER_22] =
222 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
223 [RTL_GIGA_MAC_VER_23] =
224 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
225 [RTL_GIGA_MAC_VER_24] =
226 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
227 [RTL_GIGA_MAC_VER_25] =
228 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
230 [RTL_GIGA_MAC_VER_26] =
231 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
233 [RTL_GIGA_MAC_VER_27] =
234 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
235 [RTL_GIGA_MAC_VER_28] =
236 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
237 [RTL_GIGA_MAC_VER_29] =
238 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
240 [RTL_GIGA_MAC_VER_30] =
241 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
243 [RTL_GIGA_MAC_VER_31] =
244 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
245 [RTL_GIGA_MAC_VER_32] =
246 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
248 [RTL_GIGA_MAC_VER_33] =
249 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
251 [RTL_GIGA_MAC_VER_34] =
252 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
254 [RTL_GIGA_MAC_VER_35] =
255 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
257 [RTL_GIGA_MAC_VER_36] =
258 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
260 [RTL_GIGA_MAC_VER_37] =
261 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
263 [RTL_GIGA_MAC_VER_38] =
264 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
266 [RTL_GIGA_MAC_VER_39] =
267 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
269 [RTL_GIGA_MAC_VER_40] =
270 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
272 [RTL_GIGA_MAC_VER_41] =
273 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
274 [RTL_GIGA_MAC_VER_42] =
275 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
277 [RTL_GIGA_MAC_VER_43] =
278 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
280 [RTL_GIGA_MAC_VER_44] =
281 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
292 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
293 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
294 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
295 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
296 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
297 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
298 { PCI_VENDOR_ID_DLINK, 0x4300,
299 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
300 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
301 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
302 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
303 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
304 { PCI_VENDOR_ID_LINKSYS, 0x1032,
305 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
307 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
311 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
313 static int rx_buf_sz = 16383;
320 MAC0 = 0, /* Ethernet hardware address. */
322 MAR0 = 8, /* Multicast filter. */
323 CounterAddrLow = 0x10,
324 CounterAddrHigh = 0x14,
325 TxDescStartAddrLow = 0x20,
326 TxDescStartAddrHigh = 0x24,
327 TxHDescStartAddrLow = 0x28,
328 TxHDescStartAddrHigh = 0x2c,
337 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
338 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
341 #define RX128_INT_EN (1 << 15) /* 8111c and later */
342 #define RX_MULTI_EN (1 << 14) /* 8111c only */
343 #define RXCFG_FIFO_SHIFT 13
344 /* No threshold before first PCI xfer */
345 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
346 #define RX_EARLY_OFF (1 << 11)
347 #define RXCFG_DMA_SHIFT 8
348 /* Unlimited maximum PCI burst. */
349 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
356 #define PME_SIGNAL (1 << 5) /* 8168c and later */
367 RxDescAddrLow = 0xe4,
368 RxDescAddrHigh = 0xe8,
369 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
371 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
373 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
375 #define TxPacketMax (8064 >> 7)
376 #define EarlySize 0x27
379 FuncEventMask = 0xf4,
380 FuncPresetState = 0xf8,
381 FuncForceEvent = 0xfc,
384 enum rtl8110_registers {
390 enum rtl8168_8101_registers {
393 #define CSIAR_FLAG 0x80000000
394 #define CSIAR_WRITE_CMD 0x80000000
395 #define CSIAR_BYTE_ENABLE 0x0f
396 #define CSIAR_BYTE_ENABLE_SHIFT 12
397 #define CSIAR_ADDR_MASK 0x0fff
398 #define CSIAR_FUNC_CARD 0x00000000
399 #define CSIAR_FUNC_SDIO 0x00010000
400 #define CSIAR_FUNC_NIC 0x00020000
401 #define CSIAR_FUNC_NIC2 0x00010000
404 #define EPHYAR_FLAG 0x80000000
405 #define EPHYAR_WRITE_CMD 0x80000000
406 #define EPHYAR_REG_MASK 0x1f
407 #define EPHYAR_REG_SHIFT 16
408 #define EPHYAR_DATA_MASK 0xffff
410 #define PFM_EN (1 << 6)
412 #define FIX_NAK_1 (1 << 4)
413 #define FIX_NAK_2 (1 << 3)
416 #define NOW_IS_OOB (1 << 7)
417 #define TX_EMPTY (1 << 5)
418 #define RX_EMPTY (1 << 4)
419 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
420 #define EN_NDP (1 << 3)
421 #define EN_OOB_RESET (1 << 2)
422 #define LINK_LIST_RDY (1 << 1)
424 #define EFUSEAR_FLAG 0x80000000
425 #define EFUSEAR_WRITE_CMD 0x80000000
426 #define EFUSEAR_READ_CMD 0x00000000
427 #define EFUSEAR_REG_MASK 0x03ff
428 #define EFUSEAR_REG_SHIFT 8
429 #define EFUSEAR_DATA_MASK 0xff
432 enum rtl8168_registers {
437 #define ERIAR_FLAG 0x80000000
438 #define ERIAR_WRITE_CMD 0x80000000
439 #define ERIAR_READ_CMD 0x00000000
440 #define ERIAR_ADDR_BYTE_ALIGN 4
441 #define ERIAR_TYPE_SHIFT 16
442 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
443 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
444 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
445 #define ERIAR_MASK_SHIFT 12
446 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
447 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
448 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
449 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
450 EPHY_RXER_NUM = 0x7c,
451 OCPDR = 0xb0, /* OCP GPHY access */
452 #define OCPDR_WRITE_CMD 0x80000000
453 #define OCPDR_READ_CMD 0x00000000
454 #define OCPDR_REG_MASK 0x7f
455 #define OCPDR_GPHY_REG_SHIFT 16
456 #define OCPDR_DATA_MASK 0xffff
458 #define OCPAR_FLAG 0x80000000
459 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
460 #define OCPAR_GPHY_READ_CMD 0x0000f060
462 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
463 MISC = 0xf0, /* 8168e only. */
464 #define TXPLA_RST (1 << 29)
465 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
466 #define PWM_EN (1 << 22)
467 #define RXDV_GATED_EN (1 << 19)
468 #define EARLY_TALLY_EN (1 << 16)
471 enum rtl_register_content {
472 /* InterruptStatusBits */
476 TxDescUnavail = 0x0080,
500 /* TXPoll register p.5 */
501 HPQ = 0x80, /* Poll cmd on the high prio queue */
502 NPQ = 0x40, /* Poll cmd on the low prio queue */
503 FSWInt = 0x01, /* Forced software interrupt */
507 Cfg9346_Unlock = 0xc0,
512 AcceptBroadcast = 0x08,
513 AcceptMulticast = 0x04,
515 AcceptAllPhys = 0x01,
516 #define RX_CONFIG_ACCEPT_MASK 0x3f
519 TxInterFrameGapShift = 24,
520 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
522 /* Config1 register p.24 */
525 Speed_down = (1 << 4),
529 PMEnable = (1 << 0), /* Power Management Enable */
531 /* Config2 register p. 25 */
532 ClkReqEn = (1 << 7), /* Clock Request Enable */
533 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
534 PCI_Clock_66MHz = 0x01,
535 PCI_Clock_33MHz = 0x00,
537 /* Config3 register p.25 */
538 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
539 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
540 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
541 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
543 /* Config4 register */
544 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
546 /* Config5 register p.27 */
547 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
548 MWF = (1 << 5), /* Accept Multicast wakeup frame */
549 UWF = (1 << 4), /* Accept Unicast wakeup frame */
551 LanWake = (1 << 1), /* LanWake enable/disable */
552 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
553 ASPM_en = (1 << 0), /* ASPM enable */
556 TBIReset = 0x80000000,
557 TBILoopback = 0x40000000,
558 TBINwEnable = 0x20000000,
559 TBINwRestart = 0x10000000,
560 TBILinkOk = 0x02000000,
561 TBINwComplete = 0x01000000,
564 EnableBist = (1 << 15), // 8168 8101
565 Mac_dbgo_oe = (1 << 14), // 8168 8101
566 Normal_mode = (1 << 13), // unused
567 Force_half_dup = (1 << 12), // 8168 8101
568 Force_rxflow_en = (1 << 11), // 8168 8101
569 Force_txflow_en = (1 << 10), // 8168 8101
570 Cxpl_dbg_sel = (1 << 9), // 8168 8101
571 ASF = (1 << 8), // 8168 8101
572 PktCntrDisable = (1 << 7), // 8168 8101
573 Mac_dbgo_sel = 0x001c, // 8168
578 INTT_0 = 0x0000, // 8168
579 INTT_1 = 0x0001, // 8168
580 INTT_2 = 0x0002, // 8168
581 INTT_3 = 0x0003, // 8168
583 /* rtl8169_PHYstatus */
594 TBILinkOK = 0x02000000,
596 /* DumpCounterCommand */
601 /* First doubleword. */
602 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
603 RingEnd = (1 << 30), /* End of descriptor ring */
604 FirstFrag = (1 << 29), /* First segment of a packet */
605 LastFrag = (1 << 28), /* Final segment of a packet */
609 enum rtl_tx_desc_bit {
610 /* First doubleword. */
611 TD_LSO = (1 << 27), /* Large Send Offload */
612 #define TD_MSS_MAX 0x07ffu /* MSS value */
614 /* Second doubleword. */
615 TxVlanTag = (1 << 17), /* Add VLAN tag */
618 /* 8169, 8168b and 810x except 8102e. */
619 enum rtl_tx_desc_bit_0 {
620 /* First doubleword. */
621 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
622 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
623 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
624 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
627 /* 8102e, 8168c and beyond. */
628 enum rtl_tx_desc_bit_1 {
629 /* First doubleword. */
630 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
631 #define GTTCPHO_SHIFT 18
633 /* Second doubleword. */
634 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
635 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
636 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
637 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
640 enum rtl_rx_desc_bit {
642 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
643 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
645 #define RxProtoUDP (PID1)
646 #define RxProtoTCP (PID0)
647 #define RxProtoIP (PID1 | PID0)
648 #define RxProtoMask RxProtoIP
650 IPFail = (1 << 16), /* IP checksum failed */
651 UDPFail = (1 << 15), /* UDP/IP checksum failed */
652 TCPFail = (1 << 14), /* TCP/IP checksum failed */
653 RxVlanTag = (1 << 16), /* VLAN tag available */
656 #define RsvdMask 0x3fffc000
673 u8 __pad[sizeof(void *) - sizeof(u32)];
677 RTL_FEATURE_WOL = (1 << 0),
678 RTL_FEATURE_MSI = (1 << 1),
679 RTL_FEATURE_GMII = (1 << 2),
682 struct rtl8169_counters {
689 __le32 tx_one_collision;
690 __le32 tx_multi_collision;
699 RTL_FLAG_TASK_ENABLED,
700 RTL_FLAG_TASK_SLOW_PENDING,
701 RTL_FLAG_TASK_RESET_PENDING,
702 RTL_FLAG_TASK_PHY_PENDING,
706 struct rtl8169_stats {
709 struct u64_stats_sync syncp;
712 struct rtl8169_private {
713 void __iomem *mmio_addr; /* memory map physical address */
714 struct pci_dev *pci_dev;
715 struct net_device *dev;
716 struct napi_struct napi;
720 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
721 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
723 struct rtl8169_stats rx_stats;
724 struct rtl8169_stats tx_stats;
725 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
726 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
727 dma_addr_t TxPhyAddr;
728 dma_addr_t RxPhyAddr;
729 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
730 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
731 struct timer_list timer;
737 void (*write)(struct rtl8169_private *, int, int);
738 int (*read)(struct rtl8169_private *, int);
741 struct pll_power_ops {
742 void (*down)(struct rtl8169_private *);
743 void (*up)(struct rtl8169_private *);
747 void (*enable)(struct rtl8169_private *);
748 void (*disable)(struct rtl8169_private *);
752 void (*write)(struct rtl8169_private *, int, int);
753 u32 (*read)(struct rtl8169_private *, int);
756 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
757 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
758 void (*phy_reset_enable)(struct rtl8169_private *tp);
759 void (*hw_start)(struct net_device *);
760 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
761 unsigned int (*link_ok)(void __iomem *);
762 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
763 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
766 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
768 struct work_struct work;
773 struct mii_if_info mii;
774 struct rtl8169_counters counters;
779 const struct firmware *fw;
781 #define RTL_VER_SIZE 32
783 char version[RTL_VER_SIZE];
785 struct rtl_fw_phy_action {
790 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
795 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
796 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
797 module_param(use_dac, int, 0);
798 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
799 module_param_named(debug, debug.msg_enable, int, 0);
800 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
801 MODULE_LICENSE("GPL");
802 MODULE_VERSION(RTL8169_VERSION);
803 MODULE_FIRMWARE(FIRMWARE_8168D_1);
804 MODULE_FIRMWARE(FIRMWARE_8168D_2);
805 MODULE_FIRMWARE(FIRMWARE_8168E_1);
806 MODULE_FIRMWARE(FIRMWARE_8168E_2);
807 MODULE_FIRMWARE(FIRMWARE_8168E_3);
808 MODULE_FIRMWARE(FIRMWARE_8105E_1);
809 MODULE_FIRMWARE(FIRMWARE_8168F_1);
810 MODULE_FIRMWARE(FIRMWARE_8168F_2);
811 MODULE_FIRMWARE(FIRMWARE_8402_1);
812 MODULE_FIRMWARE(FIRMWARE_8411_1);
813 MODULE_FIRMWARE(FIRMWARE_8411_2);
814 MODULE_FIRMWARE(FIRMWARE_8106E_1);
815 MODULE_FIRMWARE(FIRMWARE_8106E_2);
816 MODULE_FIRMWARE(FIRMWARE_8168G_2);
817 MODULE_FIRMWARE(FIRMWARE_8168G_3);
819 static void rtl_lock_work(struct rtl8169_private *tp)
821 mutex_lock(&tp->wk.mutex);
824 static void rtl_unlock_work(struct rtl8169_private *tp)
826 mutex_unlock(&tp->wk.mutex);
829 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
831 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
832 PCI_EXP_DEVCTL_READRQ, force);
836 bool (*check)(struct rtl8169_private *);
840 static void rtl_udelay(unsigned int d)
845 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
846 void (*delay)(unsigned int), unsigned int d, int n,
851 for (i = 0; i < n; i++) {
853 if (c->check(tp) == high)
856 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
857 c->msg, !high, n, d);
861 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
862 const struct rtl_cond *c,
863 unsigned int d, int n)
865 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
868 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
869 const struct rtl_cond *c,
870 unsigned int d, int n)
872 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
875 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
876 const struct rtl_cond *c,
877 unsigned int d, int n)
879 return rtl_loop_wait(tp, c, msleep, d, n, true);
882 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
883 const struct rtl_cond *c,
884 unsigned int d, int n)
886 return rtl_loop_wait(tp, c, msleep, d, n, false);
889 #define DECLARE_RTL_COND(name) \
890 static bool name ## _check(struct rtl8169_private *); \
892 static const struct rtl_cond name = { \
893 .check = name ## _check, \
897 static bool name ## _check(struct rtl8169_private *tp)
899 DECLARE_RTL_COND(rtl_ocpar_cond)
901 void __iomem *ioaddr = tp->mmio_addr;
903 return RTL_R32(OCPAR) & OCPAR_FLAG;
906 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
908 void __iomem *ioaddr = tp->mmio_addr;
910 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
912 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
916 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
918 void __iomem *ioaddr = tp->mmio_addr;
920 RTL_W32(OCPDR, data);
921 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
923 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
926 DECLARE_RTL_COND(rtl_eriar_cond)
928 void __iomem *ioaddr = tp->mmio_addr;
930 return RTL_R32(ERIAR) & ERIAR_FLAG;
933 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
935 void __iomem *ioaddr = tp->mmio_addr;
938 RTL_W32(ERIAR, 0x800010e8);
941 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
944 ocp_write(tp, 0x1, 0x30, 0x00000001);
947 #define OOB_CMD_RESET 0x00
948 #define OOB_CMD_DRIVER_START 0x05
949 #define OOB_CMD_DRIVER_STOP 0x06
951 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
953 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
956 DECLARE_RTL_COND(rtl_ocp_read_cond)
960 reg = rtl8168_get_ocp_reg(tp);
962 return ocp_read(tp, 0x0f, reg) & 0x00000800;
965 static void rtl8168_driver_start(struct rtl8169_private *tp)
967 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
969 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
972 static void rtl8168_driver_stop(struct rtl8169_private *tp)
974 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
976 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
979 static int r8168dp_check_dash(struct rtl8169_private *tp)
981 u16 reg = rtl8168_get_ocp_reg(tp);
983 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
986 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
988 if (reg & 0xffff0001) {
989 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
995 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
997 void __iomem *ioaddr = tp->mmio_addr;
999 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1002 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1004 void __iomem *ioaddr = tp->mmio_addr;
1006 if (rtl_ocp_reg_failure(tp, reg))
1009 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1011 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1014 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1016 void __iomem *ioaddr = tp->mmio_addr;
1018 if (rtl_ocp_reg_failure(tp, reg))
1021 RTL_W32(GPHY_OCP, reg << 15);
1023 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1024 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1027 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1029 void __iomem *ioaddr = tp->mmio_addr;
1031 if (rtl_ocp_reg_failure(tp, reg))
1034 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
1037 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1039 void __iomem *ioaddr = tp->mmio_addr;
1041 if (rtl_ocp_reg_failure(tp, reg))
1044 RTL_W32(OCPDR, reg << 15);
1046 return RTL_R32(OCPDR);
1049 #define OCP_STD_PHY_BASE 0xa400
1051 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1054 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1058 if (tp->ocp_base != OCP_STD_PHY_BASE)
1061 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1064 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1066 if (tp->ocp_base != OCP_STD_PHY_BASE)
1069 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1072 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1075 tp->ocp_base = value << 4;
1079 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1082 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1084 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1087 DECLARE_RTL_COND(rtl_phyar_cond)
1089 void __iomem *ioaddr = tp->mmio_addr;
1091 return RTL_R32(PHYAR) & 0x80000000;
1094 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1096 void __iomem *ioaddr = tp->mmio_addr;
1098 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1100 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1102 * According to hardware specs a 20us delay is required after write
1103 * complete indication, but before sending next command.
1108 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1110 void __iomem *ioaddr = tp->mmio_addr;
1113 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
1115 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1116 RTL_R32(PHYAR) & 0xffff : ~0;
1119 * According to hardware specs a 20us delay is required after read
1120 * complete indication, but before sending next command.
1127 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1129 void __iomem *ioaddr = tp->mmio_addr;
1131 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1132 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1133 RTL_W32(EPHY_RXER_NUM, 0);
1135 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1138 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1140 r8168dp_1_mdio_access(tp, reg,
1141 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1144 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1146 void __iomem *ioaddr = tp->mmio_addr;
1148 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1151 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1152 RTL_W32(EPHY_RXER_NUM, 0);
1154 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1155 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
1158 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1160 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1162 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1165 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1167 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1170 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1172 void __iomem *ioaddr = tp->mmio_addr;
1174 r8168dp_2_mdio_start(ioaddr);
1176 r8169_mdio_write(tp, reg, value);
1178 r8168dp_2_mdio_stop(ioaddr);
1181 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1183 void __iomem *ioaddr = tp->mmio_addr;
1186 r8168dp_2_mdio_start(ioaddr);
1188 value = r8169_mdio_read(tp, reg);
1190 r8168dp_2_mdio_stop(ioaddr);
1195 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1197 tp->mdio_ops.write(tp, location, val);
1200 static int rtl_readphy(struct rtl8169_private *tp, int location)
1202 return tp->mdio_ops.read(tp, location);
1205 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1207 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1210 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1214 val = rtl_readphy(tp, reg_addr);
1215 rtl_writephy(tp, reg_addr, (val | p) & ~m);
1218 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1221 struct rtl8169_private *tp = netdev_priv(dev);
1223 rtl_writephy(tp, location, val);
1226 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1228 struct rtl8169_private *tp = netdev_priv(dev);
1230 return rtl_readphy(tp, location);
1233 DECLARE_RTL_COND(rtl_ephyar_cond)
1235 void __iomem *ioaddr = tp->mmio_addr;
1237 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1240 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1242 void __iomem *ioaddr = tp->mmio_addr;
1244 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1245 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1247 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1252 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1254 void __iomem *ioaddr = tp->mmio_addr;
1256 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1258 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1259 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
1262 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1265 void __iomem *ioaddr = tp->mmio_addr;
1267 BUG_ON((addr & 3) || (mask == 0));
1268 RTL_W32(ERIDR, val);
1269 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1271 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1274 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1276 void __iomem *ioaddr = tp->mmio_addr;
1278 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1280 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1281 RTL_R32(ERIDR) : ~0;
1284 static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1289 val = rtl_eri_read(tp, addr, type);
1290 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1299 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1300 const struct exgmac_reg *r, int len)
1303 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1308 DECLARE_RTL_COND(rtl_efusear_cond)
1310 void __iomem *ioaddr = tp->mmio_addr;
1312 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1315 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1317 void __iomem *ioaddr = tp->mmio_addr;
1319 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1321 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1322 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1325 static u16 rtl_get_events(struct rtl8169_private *tp)
1327 void __iomem *ioaddr = tp->mmio_addr;
1329 return RTL_R16(IntrStatus);
1332 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1334 void __iomem *ioaddr = tp->mmio_addr;
1336 RTL_W16(IntrStatus, bits);
1340 static void rtl_irq_disable(struct rtl8169_private *tp)
1342 void __iomem *ioaddr = tp->mmio_addr;
1344 RTL_W16(IntrMask, 0);
1348 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1350 void __iomem *ioaddr = tp->mmio_addr;
1352 RTL_W16(IntrMask, bits);
1355 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1356 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1357 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1359 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1361 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1364 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1366 void __iomem *ioaddr = tp->mmio_addr;
1368 rtl_irq_disable(tp);
1369 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1373 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1375 void __iomem *ioaddr = tp->mmio_addr;
1377 return RTL_R32(TBICSR) & TBIReset;
1380 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1382 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1385 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1387 return RTL_R32(TBICSR) & TBILinkOk;
1390 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1392 return RTL_R8(PHYstatus) & LinkStatus;
1395 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1397 void __iomem *ioaddr = tp->mmio_addr;
1399 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1402 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1406 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1407 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1410 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1412 void __iomem *ioaddr = tp->mmio_addr;
1413 struct net_device *dev = tp->dev;
1415 if (!netif_running(dev))
1418 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1419 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1420 if (RTL_R8(PHYstatus) & _1000bpsF) {
1421 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1423 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1425 } else if (RTL_R8(PHYstatus) & _100bps) {
1426 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1428 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1431 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1433 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1436 /* Reset packet filter */
1437 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1439 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1441 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1442 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1443 if (RTL_R8(PHYstatus) & _1000bpsF) {
1444 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1446 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1449 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1451 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1454 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1455 if (RTL_R8(PHYstatus) & _10bps) {
1456 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1458 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1461 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1467 static void __rtl8169_check_link_status(struct net_device *dev,
1468 struct rtl8169_private *tp,
1469 void __iomem *ioaddr, bool pm)
1471 if (tp->link_ok(ioaddr)) {
1472 rtl_link_chg_patch(tp);
1473 /* This is to cancel a scheduled suspend if there's one. */
1475 pm_request_resume(&tp->pci_dev->dev);
1476 netif_carrier_on(dev);
1477 if (net_ratelimit())
1478 netif_info(tp, ifup, dev, "link up\n");
1480 netif_carrier_off(dev);
1481 netif_info(tp, ifdown, dev, "link down\n");
1483 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1487 static void rtl8169_check_link_status(struct net_device *dev,
1488 struct rtl8169_private *tp,
1489 void __iomem *ioaddr)
1491 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1494 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1496 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1498 void __iomem *ioaddr = tp->mmio_addr;
1502 options = RTL_R8(Config1);
1503 if (!(options & PMEnable))
1506 options = RTL_R8(Config3);
1507 if (options & LinkUp)
1508 wolopts |= WAKE_PHY;
1509 if (options & MagicPacket)
1510 wolopts |= WAKE_MAGIC;
1512 options = RTL_R8(Config5);
1514 wolopts |= WAKE_UCAST;
1516 wolopts |= WAKE_BCAST;
1518 wolopts |= WAKE_MCAST;
1523 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1525 struct rtl8169_private *tp = netdev_priv(dev);
1529 wol->supported = WAKE_ANY;
1530 wol->wolopts = __rtl8169_get_wol(tp);
1532 rtl_unlock_work(tp);
1535 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1537 void __iomem *ioaddr = tp->mmio_addr;
1539 static const struct {
1544 { WAKE_PHY, Config3, LinkUp },
1545 { WAKE_MAGIC, Config3, MagicPacket },
1546 { WAKE_UCAST, Config5, UWF },
1547 { WAKE_BCAST, Config5, BWF },
1548 { WAKE_MCAST, Config5, MWF },
1549 { WAKE_ANY, Config5, LanWake }
1553 RTL_W8(Cfg9346, Cfg9346_Unlock);
1555 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1556 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1557 if (wolopts & cfg[i].opt)
1558 options |= cfg[i].mask;
1559 RTL_W8(cfg[i].reg, options);
1562 switch (tp->mac_version) {
1563 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1564 options = RTL_R8(Config1) & ~PMEnable;
1566 options |= PMEnable;
1567 RTL_W8(Config1, options);
1570 options = RTL_R8(Config2) & ~PME_SIGNAL;
1572 options |= PME_SIGNAL;
1573 RTL_W8(Config2, options);
1577 RTL_W8(Cfg9346, Cfg9346_Lock);
1580 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1582 struct rtl8169_private *tp = netdev_priv(dev);
1587 tp->features |= RTL_FEATURE_WOL;
1589 tp->features &= ~RTL_FEATURE_WOL;
1590 __rtl8169_set_wol(tp, wol->wolopts);
1592 rtl_unlock_work(tp);
1594 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1599 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1601 return rtl_chip_infos[tp->mac_version].fw_name;
1604 static void rtl8169_get_drvinfo(struct net_device *dev,
1605 struct ethtool_drvinfo *info)
1607 struct rtl8169_private *tp = netdev_priv(dev);
1608 struct rtl_fw *rtl_fw = tp->rtl_fw;
1610 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1611 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1612 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1613 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1614 if (!IS_ERR_OR_NULL(rtl_fw))
1615 strlcpy(info->fw_version, rtl_fw->version,
1616 sizeof(info->fw_version));
1619 static int rtl8169_get_regs_len(struct net_device *dev)
1621 return R8169_REGS_SIZE;
1624 static int rtl8169_set_speed_tbi(struct net_device *dev,
1625 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1627 struct rtl8169_private *tp = netdev_priv(dev);
1628 void __iomem *ioaddr = tp->mmio_addr;
1632 reg = RTL_R32(TBICSR);
1633 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1634 (duplex == DUPLEX_FULL)) {
1635 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1636 } else if (autoneg == AUTONEG_ENABLE)
1637 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1639 netif_warn(tp, link, dev,
1640 "incorrect speed setting refused in TBI mode\n");
1647 static int rtl8169_set_speed_xmii(struct net_device *dev,
1648 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1650 struct rtl8169_private *tp = netdev_priv(dev);
1651 int giga_ctrl, bmcr;
1654 rtl_writephy(tp, 0x1f, 0x0000);
1656 if (autoneg == AUTONEG_ENABLE) {
1659 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1660 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1661 ADVERTISE_100HALF | ADVERTISE_100FULL);
1663 if (adv & ADVERTISED_10baseT_Half)
1664 auto_nego |= ADVERTISE_10HALF;
1665 if (adv & ADVERTISED_10baseT_Full)
1666 auto_nego |= ADVERTISE_10FULL;
1667 if (adv & ADVERTISED_100baseT_Half)
1668 auto_nego |= ADVERTISE_100HALF;
1669 if (adv & ADVERTISED_100baseT_Full)
1670 auto_nego |= ADVERTISE_100FULL;
1672 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1674 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1675 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1677 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1678 if (tp->mii.supports_gmii) {
1679 if (adv & ADVERTISED_1000baseT_Half)
1680 giga_ctrl |= ADVERTISE_1000HALF;
1681 if (adv & ADVERTISED_1000baseT_Full)
1682 giga_ctrl |= ADVERTISE_1000FULL;
1683 } else if (adv & (ADVERTISED_1000baseT_Half |
1684 ADVERTISED_1000baseT_Full)) {
1685 netif_info(tp, link, dev,
1686 "PHY does not support 1000Mbps\n");
1690 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1692 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1693 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1697 if (speed == SPEED_10)
1699 else if (speed == SPEED_100)
1700 bmcr = BMCR_SPEED100;
1704 if (duplex == DUPLEX_FULL)
1705 bmcr |= BMCR_FULLDPLX;
1708 rtl_writephy(tp, MII_BMCR, bmcr);
1710 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1711 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1712 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1713 rtl_writephy(tp, 0x17, 0x2138);
1714 rtl_writephy(tp, 0x0e, 0x0260);
1716 rtl_writephy(tp, 0x17, 0x2108);
1717 rtl_writephy(tp, 0x0e, 0x0000);
1726 static int rtl8169_set_speed(struct net_device *dev,
1727 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1729 struct rtl8169_private *tp = netdev_priv(dev);
1732 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1736 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1737 (advertising & ADVERTISED_1000baseT_Full)) {
1738 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1744 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1746 struct rtl8169_private *tp = netdev_priv(dev);
1749 del_timer_sync(&tp->timer);
1752 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1753 cmd->duplex, cmd->advertising);
1754 rtl_unlock_work(tp);
1759 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1760 netdev_features_t features)
1762 struct rtl8169_private *tp = netdev_priv(dev);
1764 if (dev->mtu > TD_MSS_MAX)
1765 features &= ~NETIF_F_ALL_TSO;
1767 if (dev->mtu > JUMBO_1K &&
1768 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1769 features &= ~NETIF_F_IP_CSUM;
1774 static void __rtl8169_set_features(struct net_device *dev,
1775 netdev_features_t features)
1777 struct rtl8169_private *tp = netdev_priv(dev);
1778 netdev_features_t changed = features ^ dev->features;
1779 void __iomem *ioaddr = tp->mmio_addr;
1781 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM |
1782 NETIF_F_HW_VLAN_CTAG_RX)))
1785 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX)) {
1786 if (features & NETIF_F_RXCSUM)
1787 tp->cp_cmd |= RxChkSum;
1789 tp->cp_cmd &= ~RxChkSum;
1791 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
1792 tp->cp_cmd |= RxVlan;
1794 tp->cp_cmd &= ~RxVlan;
1796 RTL_W16(CPlusCmd, tp->cp_cmd);
1799 if (changed & NETIF_F_RXALL) {
1800 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1801 if (features & NETIF_F_RXALL)
1802 tmp |= (AcceptErr | AcceptRunt);
1803 RTL_W32(RxConfig, tmp);
1807 static int rtl8169_set_features(struct net_device *dev,
1808 netdev_features_t features)
1810 struct rtl8169_private *tp = netdev_priv(dev);
1813 __rtl8169_set_features(dev, features);
1814 rtl_unlock_work(tp);
1820 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1822 return (vlan_tx_tag_present(skb)) ?
1823 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1826 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1828 u32 opts2 = le32_to_cpu(desc->opts2);
1830 if (opts2 & RxVlanTag)
1831 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1834 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1836 struct rtl8169_private *tp = netdev_priv(dev);
1837 void __iomem *ioaddr = tp->mmio_addr;
1841 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1842 cmd->port = PORT_FIBRE;
1843 cmd->transceiver = XCVR_INTERNAL;
1845 status = RTL_R32(TBICSR);
1846 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1847 cmd->autoneg = !!(status & TBINwEnable);
1849 ethtool_cmd_speed_set(cmd, SPEED_1000);
1850 cmd->duplex = DUPLEX_FULL; /* Always set */
1855 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1857 struct rtl8169_private *tp = netdev_priv(dev);
1859 return mii_ethtool_gset(&tp->mii, cmd);
1862 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1864 struct rtl8169_private *tp = netdev_priv(dev);
1868 rc = tp->get_settings(dev, cmd);
1869 rtl_unlock_work(tp);
1874 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1877 struct rtl8169_private *tp = netdev_priv(dev);
1878 u32 __iomem *data = tp->mmio_addr;
1883 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1884 memcpy_fromio(dw++, data++, 4);
1885 rtl_unlock_work(tp);
1888 static u32 rtl8169_get_msglevel(struct net_device *dev)
1890 struct rtl8169_private *tp = netdev_priv(dev);
1892 return tp->msg_enable;
1895 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1897 struct rtl8169_private *tp = netdev_priv(dev);
1899 tp->msg_enable = value;
1902 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1909 "tx_single_collisions",
1910 "tx_multi_collisions",
1918 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1922 return ARRAY_SIZE(rtl8169_gstrings);
1928 DECLARE_RTL_COND(rtl_counters_cond)
1930 void __iomem *ioaddr = tp->mmio_addr;
1932 return RTL_R32(CounterAddrLow) & CounterDump;
1935 static void rtl8169_update_counters(struct net_device *dev)
1937 struct rtl8169_private *tp = netdev_priv(dev);
1938 void __iomem *ioaddr = tp->mmio_addr;
1939 struct device *d = &tp->pci_dev->dev;
1940 struct rtl8169_counters *counters;
1945 * Some chips are unable to dump tally counters when the receiver
1948 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1951 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1955 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1956 cmd = (u64)paddr & DMA_BIT_MASK(32);
1957 RTL_W32(CounterAddrLow, cmd);
1958 RTL_W32(CounterAddrLow, cmd | CounterDump);
1960 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1961 memcpy(&tp->counters, counters, sizeof(*counters));
1963 RTL_W32(CounterAddrLow, 0);
1964 RTL_W32(CounterAddrHigh, 0);
1966 dma_free_coherent(d, sizeof(*counters), counters, paddr);
1969 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1970 struct ethtool_stats *stats, u64 *data)
1972 struct rtl8169_private *tp = netdev_priv(dev);
1976 rtl8169_update_counters(dev);
1978 data[0] = le64_to_cpu(tp->counters.tx_packets);
1979 data[1] = le64_to_cpu(tp->counters.rx_packets);
1980 data[2] = le64_to_cpu(tp->counters.tx_errors);
1981 data[3] = le32_to_cpu(tp->counters.rx_errors);
1982 data[4] = le16_to_cpu(tp->counters.rx_missed);
1983 data[5] = le16_to_cpu(tp->counters.align_errors);
1984 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1985 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1986 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1987 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1988 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1989 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1990 data[12] = le16_to_cpu(tp->counters.tx_underun);
1993 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1997 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2002 static const struct ethtool_ops rtl8169_ethtool_ops = {
2003 .get_drvinfo = rtl8169_get_drvinfo,
2004 .get_regs_len = rtl8169_get_regs_len,
2005 .get_link = ethtool_op_get_link,
2006 .get_settings = rtl8169_get_settings,
2007 .set_settings = rtl8169_set_settings,
2008 .get_msglevel = rtl8169_get_msglevel,
2009 .set_msglevel = rtl8169_set_msglevel,
2010 .get_regs = rtl8169_get_regs,
2011 .get_wol = rtl8169_get_wol,
2012 .set_wol = rtl8169_set_wol,
2013 .get_strings = rtl8169_get_strings,
2014 .get_sset_count = rtl8169_get_sset_count,
2015 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2016 .get_ts_info = ethtool_op_get_ts_info,
2019 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2020 struct net_device *dev, u8 default_version)
2022 void __iomem *ioaddr = tp->mmio_addr;
2024 * The driver currently handles the 8168Bf and the 8168Be identically
2025 * but they can be identified more specifically through the test below
2028 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2030 * Same thing for the 8101Eb and the 8101Ec:
2032 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2034 static const struct rtl_mac_info {
2040 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2041 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2042 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2043 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2046 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2047 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2048 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2051 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2052 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2053 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2054 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2057 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2058 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2059 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2061 /* 8168DP family. */
2062 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2063 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2064 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2067 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
2068 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2069 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2070 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2071 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2072 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2073 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2074 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
2075 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2078 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2079 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2080 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2081 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2084 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2085 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2086 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2087 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
2088 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2089 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2090 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2091 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2092 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2093 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2094 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2095 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2096 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2097 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2098 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2099 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2100 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2101 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2102 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2103 /* FIXME: where did these entries come from ? -- FR */
2104 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2105 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2108 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2109 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2110 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2111 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2112 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2113 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2116 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2118 const struct rtl_mac_info *p = mac_info;
2121 reg = RTL_R32(TxConfig);
2122 while ((reg & p->mask) != p->val)
2124 tp->mac_version = p->mac_version;
2126 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2127 netif_notice(tp, probe, dev,
2128 "unknown MAC, using family default\n");
2129 tp->mac_version = default_version;
2130 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2131 tp->mac_version = tp->mii.supports_gmii ?
2132 RTL_GIGA_MAC_VER_42 :
2133 RTL_GIGA_MAC_VER_43;
2137 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2139 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2147 static void rtl_writephy_batch(struct rtl8169_private *tp,
2148 const struct phy_reg *regs, int len)
2151 rtl_writephy(tp, regs->reg, regs->val);
2156 #define PHY_READ 0x00000000
2157 #define PHY_DATA_OR 0x10000000
2158 #define PHY_DATA_AND 0x20000000
2159 #define PHY_BJMPN 0x30000000
2160 #define PHY_MDIO_CHG 0x40000000
2161 #define PHY_CLEAR_READCOUNT 0x70000000
2162 #define PHY_WRITE 0x80000000
2163 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2164 #define PHY_COMP_EQ_SKIPN 0xa0000000
2165 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2166 #define PHY_WRITE_PREVIOUS 0xc0000000
2167 #define PHY_SKIPN 0xd0000000
2168 #define PHY_DELAY_MS 0xe0000000
2172 char version[RTL_VER_SIZE];
2178 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2180 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2182 const struct firmware *fw = rtl_fw->fw;
2183 struct fw_info *fw_info = (struct fw_info *)fw->data;
2184 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2185 char *version = rtl_fw->version;
2188 if (fw->size < FW_OPCODE_SIZE)
2191 if (!fw_info->magic) {
2192 size_t i, size, start;
2195 if (fw->size < sizeof(*fw_info))
2198 for (i = 0; i < fw->size; i++)
2199 checksum += fw->data[i];
2203 start = le32_to_cpu(fw_info->fw_start);
2204 if (start > fw->size)
2207 size = le32_to_cpu(fw_info->fw_len);
2208 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2211 memcpy(version, fw_info->version, RTL_VER_SIZE);
2213 pa->code = (__le32 *)(fw->data + start);
2216 if (fw->size % FW_OPCODE_SIZE)
2219 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2221 pa->code = (__le32 *)fw->data;
2222 pa->size = fw->size / FW_OPCODE_SIZE;
2224 version[RTL_VER_SIZE - 1] = 0;
2231 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2232 struct rtl_fw_phy_action *pa)
2237 for (index = 0; index < pa->size; index++) {
2238 u32 action = le32_to_cpu(pa->code[index]);
2239 u32 regno = (action & 0x0fff0000) >> 16;
2241 switch(action & 0xf0000000) {
2246 case PHY_CLEAR_READCOUNT:
2248 case PHY_WRITE_PREVIOUS:
2253 if (regno > index) {
2254 netif_err(tp, ifup, tp->dev,
2255 "Out of range of firmware\n");
2259 case PHY_READCOUNT_EQ_SKIP:
2260 if (index + 2 >= pa->size) {
2261 netif_err(tp, ifup, tp->dev,
2262 "Out of range of firmware\n");
2266 case PHY_COMP_EQ_SKIPN:
2267 case PHY_COMP_NEQ_SKIPN:
2269 if (index + 1 + regno >= pa->size) {
2270 netif_err(tp, ifup, tp->dev,
2271 "Out of range of firmware\n");
2277 netif_err(tp, ifup, tp->dev,
2278 "Invalid action 0x%08x\n", action);
2287 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2289 struct net_device *dev = tp->dev;
2292 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2293 netif_err(tp, ifup, dev, "invalid firwmare\n");
2297 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2303 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2305 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2306 struct mdio_ops org, *ops = &tp->mdio_ops;
2310 predata = count = 0;
2311 org.write = ops->write;
2312 org.read = ops->read;
2314 for (index = 0; index < pa->size; ) {
2315 u32 action = le32_to_cpu(pa->code[index]);
2316 u32 data = action & 0x0000ffff;
2317 u32 regno = (action & 0x0fff0000) >> 16;
2322 switch(action & 0xf0000000) {
2324 predata = rtl_readphy(tp, regno);
2341 ops->write = org.write;
2342 ops->read = org.read;
2343 } else if (data == 1) {
2344 ops->write = mac_mcu_write;
2345 ops->read = mac_mcu_read;
2350 case PHY_CLEAR_READCOUNT:
2355 rtl_writephy(tp, regno, data);
2358 case PHY_READCOUNT_EQ_SKIP:
2359 index += (count == data) ? 2 : 1;
2361 case PHY_COMP_EQ_SKIPN:
2362 if (predata == data)
2366 case PHY_COMP_NEQ_SKIPN:
2367 if (predata != data)
2371 case PHY_WRITE_PREVIOUS:
2372 rtl_writephy(tp, regno, predata);
2388 ops->write = org.write;
2389 ops->read = org.read;
2392 static void rtl_release_firmware(struct rtl8169_private *tp)
2394 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2395 release_firmware(tp->rtl_fw->fw);
2398 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2401 static void rtl_apply_firmware(struct rtl8169_private *tp)
2403 struct rtl_fw *rtl_fw = tp->rtl_fw;
2405 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2406 if (!IS_ERR_OR_NULL(rtl_fw))
2407 rtl_phy_write_fw(tp, rtl_fw);
2410 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2412 if (rtl_readphy(tp, reg) != val)
2413 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2415 rtl_apply_firmware(tp);
2418 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2420 static const struct phy_reg phy_reg_init[] = {
2482 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2485 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2487 static const struct phy_reg phy_reg_init[] = {
2493 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2496 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2498 struct pci_dev *pdev = tp->pci_dev;
2500 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2501 (pdev->subsystem_device != 0xe000))
2504 rtl_writephy(tp, 0x1f, 0x0001);
2505 rtl_writephy(tp, 0x10, 0xf01b);
2506 rtl_writephy(tp, 0x1f, 0x0000);
2509 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2511 static const struct phy_reg phy_reg_init[] = {
2551 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2553 rtl8169scd_hw_phy_config_quirk(tp);
2556 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2558 static const struct phy_reg phy_reg_init[] = {
2606 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2609 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2611 static const struct phy_reg phy_reg_init[] = {
2616 rtl_writephy(tp, 0x1f, 0x0001);
2617 rtl_patchphy(tp, 0x16, 1 << 0);
2619 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2622 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2624 static const struct phy_reg phy_reg_init[] = {
2630 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2633 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2635 static const struct phy_reg phy_reg_init[] = {
2643 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2646 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2648 static const struct phy_reg phy_reg_init[] = {
2654 rtl_writephy(tp, 0x1f, 0x0000);
2655 rtl_patchphy(tp, 0x14, 1 << 5);
2656 rtl_patchphy(tp, 0x0d, 1 << 5);
2658 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2661 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2663 static const struct phy_reg phy_reg_init[] = {
2683 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2685 rtl_patchphy(tp, 0x14, 1 << 5);
2686 rtl_patchphy(tp, 0x0d, 1 << 5);
2687 rtl_writephy(tp, 0x1f, 0x0000);
2690 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2692 static const struct phy_reg phy_reg_init[] = {
2710 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2712 rtl_patchphy(tp, 0x16, 1 << 0);
2713 rtl_patchphy(tp, 0x14, 1 << 5);
2714 rtl_patchphy(tp, 0x0d, 1 << 5);
2715 rtl_writephy(tp, 0x1f, 0x0000);
2718 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2720 static const struct phy_reg phy_reg_init[] = {
2732 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2734 rtl_patchphy(tp, 0x16, 1 << 0);
2735 rtl_patchphy(tp, 0x14, 1 << 5);
2736 rtl_patchphy(tp, 0x0d, 1 << 5);
2737 rtl_writephy(tp, 0x1f, 0x0000);
2740 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2742 rtl8168c_3_hw_phy_config(tp);
2745 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2747 static const struct phy_reg phy_reg_init_0[] = {
2748 /* Channel Estimation */
2769 * Enhance line driver power
2778 * Can not link to 1Gbps with bad cable
2779 * Decrease SNR threshold form 21.07dB to 19.04dB
2788 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2792 * Fine Tune Switching regulator parameter
2794 rtl_writephy(tp, 0x1f, 0x0002);
2795 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2796 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2798 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2799 static const struct phy_reg phy_reg_init[] = {
2809 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2811 val = rtl_readphy(tp, 0x0d);
2813 if ((val & 0x00ff) != 0x006c) {
2814 static const u32 set[] = {
2815 0x0065, 0x0066, 0x0067, 0x0068,
2816 0x0069, 0x006a, 0x006b, 0x006c
2820 rtl_writephy(tp, 0x1f, 0x0002);
2823 for (i = 0; i < ARRAY_SIZE(set); i++)
2824 rtl_writephy(tp, 0x0d, val | set[i]);
2827 static const struct phy_reg phy_reg_init[] = {
2835 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2838 /* RSET couple improve */
2839 rtl_writephy(tp, 0x1f, 0x0002);
2840 rtl_patchphy(tp, 0x0d, 0x0300);
2841 rtl_patchphy(tp, 0x0f, 0x0010);
2843 /* Fine tune PLL performance */
2844 rtl_writephy(tp, 0x1f, 0x0002);
2845 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2846 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2848 rtl_writephy(tp, 0x1f, 0x0005);
2849 rtl_writephy(tp, 0x05, 0x001b);
2851 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2853 rtl_writephy(tp, 0x1f, 0x0000);
2856 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2858 static const struct phy_reg phy_reg_init_0[] = {
2859 /* Channel Estimation */
2880 * Enhance line driver power
2889 * Can not link to 1Gbps with bad cable
2890 * Decrease SNR threshold form 21.07dB to 19.04dB
2899 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2901 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2902 static const struct phy_reg phy_reg_init[] = {
2913 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2915 val = rtl_readphy(tp, 0x0d);
2916 if ((val & 0x00ff) != 0x006c) {
2917 static const u32 set[] = {
2918 0x0065, 0x0066, 0x0067, 0x0068,
2919 0x0069, 0x006a, 0x006b, 0x006c
2923 rtl_writephy(tp, 0x1f, 0x0002);
2926 for (i = 0; i < ARRAY_SIZE(set); i++)
2927 rtl_writephy(tp, 0x0d, val | set[i]);
2930 static const struct phy_reg phy_reg_init[] = {
2938 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2941 /* Fine tune PLL performance */
2942 rtl_writephy(tp, 0x1f, 0x0002);
2943 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2944 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2946 /* Switching regulator Slew rate */
2947 rtl_writephy(tp, 0x1f, 0x0002);
2948 rtl_patchphy(tp, 0x0f, 0x0017);
2950 rtl_writephy(tp, 0x1f, 0x0005);
2951 rtl_writephy(tp, 0x05, 0x001b);
2953 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2955 rtl_writephy(tp, 0x1f, 0x0000);
2958 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2960 static const struct phy_reg phy_reg_init[] = {
3016 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3019 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3021 static const struct phy_reg phy_reg_init[] = {
3031 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3032 rtl_patchphy(tp, 0x0d, 1 << 5);
3035 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3037 static const struct phy_reg phy_reg_init[] = {
3038 /* Enable Delay cap */
3044 /* Channel estimation fine tune */
3053 /* Update PFM & 10M TX idle timer */
3065 rtl_apply_firmware(tp);
3067 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3069 /* DCO enable for 10M IDLE Power */
3070 rtl_writephy(tp, 0x1f, 0x0007);
3071 rtl_writephy(tp, 0x1e, 0x0023);
3072 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3073 rtl_writephy(tp, 0x1f, 0x0000);
3075 /* For impedance matching */
3076 rtl_writephy(tp, 0x1f, 0x0002);
3077 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
3078 rtl_writephy(tp, 0x1f, 0x0000);
3080 /* PHY auto speed down */
3081 rtl_writephy(tp, 0x1f, 0x0007);
3082 rtl_writephy(tp, 0x1e, 0x002d);
3083 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3084 rtl_writephy(tp, 0x1f, 0x0000);
3085 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3087 rtl_writephy(tp, 0x1f, 0x0005);
3088 rtl_writephy(tp, 0x05, 0x8b86);
3089 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3090 rtl_writephy(tp, 0x1f, 0x0000);
3092 rtl_writephy(tp, 0x1f, 0x0005);
3093 rtl_writephy(tp, 0x05, 0x8b85);
3094 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3095 rtl_writephy(tp, 0x1f, 0x0007);
3096 rtl_writephy(tp, 0x1e, 0x0020);
3097 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3098 rtl_writephy(tp, 0x1f, 0x0006);
3099 rtl_writephy(tp, 0x00, 0x5a00);
3100 rtl_writephy(tp, 0x1f, 0x0000);
3101 rtl_writephy(tp, 0x0d, 0x0007);
3102 rtl_writephy(tp, 0x0e, 0x003c);
3103 rtl_writephy(tp, 0x0d, 0x4007);
3104 rtl_writephy(tp, 0x0e, 0x0000);
3105 rtl_writephy(tp, 0x0d, 0x0000);
3108 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3111 addr[0] | (addr[1] << 8),
3112 addr[2] | (addr[3] << 8),
3113 addr[4] | (addr[5] << 8)
3115 const struct exgmac_reg e[] = {
3116 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3117 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3118 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3119 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3122 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3125 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3127 static const struct phy_reg phy_reg_init[] = {
3128 /* Enable Delay cap */
3137 /* Channel estimation fine tune */
3154 rtl_apply_firmware(tp);
3156 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3158 /* For 4-corner performance improve */
3159 rtl_writephy(tp, 0x1f, 0x0005);
3160 rtl_writephy(tp, 0x05, 0x8b80);
3161 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3162 rtl_writephy(tp, 0x1f, 0x0000);
3164 /* PHY auto speed down */
3165 rtl_writephy(tp, 0x1f, 0x0004);
3166 rtl_writephy(tp, 0x1f, 0x0007);
3167 rtl_writephy(tp, 0x1e, 0x002d);
3168 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3169 rtl_writephy(tp, 0x1f, 0x0002);
3170 rtl_writephy(tp, 0x1f, 0x0000);
3171 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3173 /* improve 10M EEE waveform */
3174 rtl_writephy(tp, 0x1f, 0x0005);
3175 rtl_writephy(tp, 0x05, 0x8b86);
3176 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3177 rtl_writephy(tp, 0x1f, 0x0000);
3179 /* Improve 2-pair detection performance */
3180 rtl_writephy(tp, 0x1f, 0x0005);
3181 rtl_writephy(tp, 0x05, 0x8b85);
3182 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3183 rtl_writephy(tp, 0x1f, 0x0000);
3186 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
3187 rtl_writephy(tp, 0x1f, 0x0005);
3188 rtl_writephy(tp, 0x05, 0x8b85);
3189 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3190 rtl_writephy(tp, 0x1f, 0x0004);
3191 rtl_writephy(tp, 0x1f, 0x0007);
3192 rtl_writephy(tp, 0x1e, 0x0020);
3193 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3194 rtl_writephy(tp, 0x1f, 0x0002);
3195 rtl_writephy(tp, 0x1f, 0x0000);
3196 rtl_writephy(tp, 0x0d, 0x0007);
3197 rtl_writephy(tp, 0x0e, 0x003c);
3198 rtl_writephy(tp, 0x0d, 0x4007);
3199 rtl_writephy(tp, 0x0e, 0x0000);
3200 rtl_writephy(tp, 0x0d, 0x0000);
3203 rtl_writephy(tp, 0x1f, 0x0003);
3204 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3205 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3206 rtl_writephy(tp, 0x1f, 0x0000);
3208 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3209 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3212 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3214 /* For 4-corner performance improve */
3215 rtl_writephy(tp, 0x1f, 0x0005);
3216 rtl_writephy(tp, 0x05, 0x8b80);
3217 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3218 rtl_writephy(tp, 0x1f, 0x0000);
3220 /* PHY auto speed down */
3221 rtl_writephy(tp, 0x1f, 0x0007);
3222 rtl_writephy(tp, 0x1e, 0x002d);
3223 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3224 rtl_writephy(tp, 0x1f, 0x0000);
3225 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3227 /* Improve 10M EEE waveform */
3228 rtl_writephy(tp, 0x1f, 0x0005);
3229 rtl_writephy(tp, 0x05, 0x8b86);
3230 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3231 rtl_writephy(tp, 0x1f, 0x0000);
3234 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3236 static const struct phy_reg phy_reg_init[] = {
3237 /* Channel estimation fine tune */
3242 /* Modify green table for giga & fnet */
3259 /* Modify green table for 10M */
3265 /* Disable hiimpedance detection (RTCT) */
3271 rtl_apply_firmware(tp);
3273 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3275 rtl8168f_hw_phy_config(tp);
3277 /* Improve 2-pair detection performance */
3278 rtl_writephy(tp, 0x1f, 0x0005);
3279 rtl_writephy(tp, 0x05, 0x8b85);
3280 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3281 rtl_writephy(tp, 0x1f, 0x0000);
3284 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3286 rtl_apply_firmware(tp);
3288 rtl8168f_hw_phy_config(tp);
3291 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3293 static const struct phy_reg phy_reg_init[] = {
3294 /* Channel estimation fine tune */
3299 /* Modify green table for giga & fnet */
3316 /* Modify green table for 10M */
3322 /* Disable hiimpedance detection (RTCT) */
3329 rtl_apply_firmware(tp);
3331 rtl8168f_hw_phy_config(tp);
3333 /* Improve 2-pair detection performance */
3334 rtl_writephy(tp, 0x1f, 0x0005);
3335 rtl_writephy(tp, 0x05, 0x8b85);
3336 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3337 rtl_writephy(tp, 0x1f, 0x0000);
3339 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3341 /* Modify green table for giga */
3342 rtl_writephy(tp, 0x1f, 0x0005);
3343 rtl_writephy(tp, 0x05, 0x8b54);
3344 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3345 rtl_writephy(tp, 0x05, 0x8b5d);
3346 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3347 rtl_writephy(tp, 0x05, 0x8a7c);
3348 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3349 rtl_writephy(tp, 0x05, 0x8a7f);
3350 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3351 rtl_writephy(tp, 0x05, 0x8a82);
3352 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3353 rtl_writephy(tp, 0x05, 0x8a85);
3354 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3355 rtl_writephy(tp, 0x05, 0x8a88);
3356 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3357 rtl_writephy(tp, 0x1f, 0x0000);
3359 /* uc same-seed solution */
3360 rtl_writephy(tp, 0x1f, 0x0005);
3361 rtl_writephy(tp, 0x05, 0x8b85);
3362 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3363 rtl_writephy(tp, 0x1f, 0x0000);
3366 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3367 rtl_writephy(tp, 0x1f, 0x0005);
3368 rtl_writephy(tp, 0x05, 0x8b85);
3369 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3370 rtl_writephy(tp, 0x1f, 0x0004);
3371 rtl_writephy(tp, 0x1f, 0x0007);
3372 rtl_writephy(tp, 0x1e, 0x0020);
3373 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3374 rtl_writephy(tp, 0x1f, 0x0000);
3375 rtl_writephy(tp, 0x0d, 0x0007);
3376 rtl_writephy(tp, 0x0e, 0x003c);
3377 rtl_writephy(tp, 0x0d, 0x4007);
3378 rtl_writephy(tp, 0x0e, 0x0000);
3379 rtl_writephy(tp, 0x0d, 0x0000);
3382 rtl_writephy(tp, 0x1f, 0x0003);
3383 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3384 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3385 rtl_writephy(tp, 0x1f, 0x0000);
3388 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3390 rtl_apply_firmware(tp);
3392 rtl_writephy(tp, 0x1f, 0x0a46);
3393 if (rtl_readphy(tp, 0x10) & 0x0100) {
3394 rtl_writephy(tp, 0x1f, 0x0bcc);
3395 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3397 rtl_writephy(tp, 0x1f, 0x0bcc);
3398 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3401 rtl_writephy(tp, 0x1f, 0x0a46);
3402 if (rtl_readphy(tp, 0x13) & 0x0100) {
3403 rtl_writephy(tp, 0x1f, 0x0c41);
3404 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3406 rtl_writephy(tp, 0x1f, 0x0c41);
3407 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
3410 /* Enable PHY auto speed down */
3411 rtl_writephy(tp, 0x1f, 0x0a44);
3412 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
3414 rtl_writephy(tp, 0x1f, 0x0bcc);
3415 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3416 rtl_writephy(tp, 0x1f, 0x0a44);
3417 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3418 rtl_writephy(tp, 0x1f, 0x0a43);
3419 rtl_writephy(tp, 0x13, 0x8084);
3420 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3421 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3423 /* EEE auto-fallback function */
3424 rtl_writephy(tp, 0x1f, 0x0a4b);
3425 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
3427 /* Enable UC LPF tune function */
3428 rtl_writephy(tp, 0x1f, 0x0a43);
3429 rtl_writephy(tp, 0x13, 0x8012);
3430 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3432 rtl_writephy(tp, 0x1f, 0x0c42);
3433 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3435 /* Improve SWR Efficiency */
3436 rtl_writephy(tp, 0x1f, 0x0bcd);
3437 rtl_writephy(tp, 0x14, 0x5065);
3438 rtl_writephy(tp, 0x14, 0xd065);
3439 rtl_writephy(tp, 0x1f, 0x0bc8);
3440 rtl_writephy(tp, 0x11, 0x5655);
3441 rtl_writephy(tp, 0x1f, 0x0bcd);
3442 rtl_writephy(tp, 0x14, 0x1065);
3443 rtl_writephy(tp, 0x14, 0x9065);
3444 rtl_writephy(tp, 0x14, 0x1065);
3446 /* Check ALDPS bit, disable it if enabled */
3447 rtl_writephy(tp, 0x1f, 0x0a43);
3448 if (rtl_readphy(tp, 0x10) & 0x0004)
3449 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0004);
3451 rtl_writephy(tp, 0x1f, 0x0000);
3454 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3456 rtl_apply_firmware(tp);
3459 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3461 static const struct phy_reg phy_reg_init[] = {
3468 rtl_writephy(tp, 0x1f, 0x0000);
3469 rtl_patchphy(tp, 0x11, 1 << 12);
3470 rtl_patchphy(tp, 0x19, 1 << 13);
3471 rtl_patchphy(tp, 0x10, 1 << 15);
3473 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3476 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3478 static const struct phy_reg phy_reg_init[] = {
3492 /* Disable ALDPS before ram code */
3493 rtl_writephy(tp, 0x1f, 0x0000);
3494 rtl_writephy(tp, 0x18, 0x0310);
3497 rtl_apply_firmware(tp);
3499 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3502 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3504 /* Disable ALDPS before setting firmware */
3505 rtl_writephy(tp, 0x1f, 0x0000);
3506 rtl_writephy(tp, 0x18, 0x0310);
3509 rtl_apply_firmware(tp);
3512 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3513 rtl_writephy(tp, 0x1f, 0x0004);
3514 rtl_writephy(tp, 0x10, 0x401f);
3515 rtl_writephy(tp, 0x19, 0x7030);
3516 rtl_writephy(tp, 0x1f, 0x0000);
3519 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3521 static const struct phy_reg phy_reg_init[] = {
3528 /* Disable ALDPS before ram code */
3529 rtl_writephy(tp, 0x1f, 0x0000);
3530 rtl_writephy(tp, 0x18, 0x0310);
3533 rtl_apply_firmware(tp);
3535 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3536 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3538 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3541 static void rtl_hw_phy_config(struct net_device *dev)
3543 struct rtl8169_private *tp = netdev_priv(dev);
3545 rtl8169_print_mac_version(tp);
3547 switch (tp->mac_version) {
3548 case RTL_GIGA_MAC_VER_01:
3550 case RTL_GIGA_MAC_VER_02:
3551 case RTL_GIGA_MAC_VER_03:
3552 rtl8169s_hw_phy_config(tp);
3554 case RTL_GIGA_MAC_VER_04:
3555 rtl8169sb_hw_phy_config(tp);
3557 case RTL_GIGA_MAC_VER_05:
3558 rtl8169scd_hw_phy_config(tp);
3560 case RTL_GIGA_MAC_VER_06:
3561 rtl8169sce_hw_phy_config(tp);
3563 case RTL_GIGA_MAC_VER_07:
3564 case RTL_GIGA_MAC_VER_08:
3565 case RTL_GIGA_MAC_VER_09:
3566 rtl8102e_hw_phy_config(tp);
3568 case RTL_GIGA_MAC_VER_11:
3569 rtl8168bb_hw_phy_config(tp);
3571 case RTL_GIGA_MAC_VER_12:
3572 rtl8168bef_hw_phy_config(tp);
3574 case RTL_GIGA_MAC_VER_17:
3575 rtl8168bef_hw_phy_config(tp);
3577 case RTL_GIGA_MAC_VER_18:
3578 rtl8168cp_1_hw_phy_config(tp);
3580 case RTL_GIGA_MAC_VER_19:
3581 rtl8168c_1_hw_phy_config(tp);
3583 case RTL_GIGA_MAC_VER_20:
3584 rtl8168c_2_hw_phy_config(tp);
3586 case RTL_GIGA_MAC_VER_21:
3587 rtl8168c_3_hw_phy_config(tp);
3589 case RTL_GIGA_MAC_VER_22:
3590 rtl8168c_4_hw_phy_config(tp);
3592 case RTL_GIGA_MAC_VER_23:
3593 case RTL_GIGA_MAC_VER_24:
3594 rtl8168cp_2_hw_phy_config(tp);
3596 case RTL_GIGA_MAC_VER_25:
3597 rtl8168d_1_hw_phy_config(tp);
3599 case RTL_GIGA_MAC_VER_26:
3600 rtl8168d_2_hw_phy_config(tp);
3602 case RTL_GIGA_MAC_VER_27:
3603 rtl8168d_3_hw_phy_config(tp);
3605 case RTL_GIGA_MAC_VER_28:
3606 rtl8168d_4_hw_phy_config(tp);
3608 case RTL_GIGA_MAC_VER_29:
3609 case RTL_GIGA_MAC_VER_30:
3610 rtl8105e_hw_phy_config(tp);
3612 case RTL_GIGA_MAC_VER_31:
3615 case RTL_GIGA_MAC_VER_32:
3616 case RTL_GIGA_MAC_VER_33:
3617 rtl8168e_1_hw_phy_config(tp);
3619 case RTL_GIGA_MAC_VER_34:
3620 rtl8168e_2_hw_phy_config(tp);
3622 case RTL_GIGA_MAC_VER_35:
3623 rtl8168f_1_hw_phy_config(tp);
3625 case RTL_GIGA_MAC_VER_36:
3626 rtl8168f_2_hw_phy_config(tp);
3629 case RTL_GIGA_MAC_VER_37:
3630 rtl8402_hw_phy_config(tp);
3633 case RTL_GIGA_MAC_VER_38:
3634 rtl8411_hw_phy_config(tp);
3637 case RTL_GIGA_MAC_VER_39:
3638 rtl8106e_hw_phy_config(tp);
3641 case RTL_GIGA_MAC_VER_40:
3642 rtl8168g_1_hw_phy_config(tp);
3644 case RTL_GIGA_MAC_VER_42:
3645 case RTL_GIGA_MAC_VER_43:
3646 case RTL_GIGA_MAC_VER_44:
3647 rtl8168g_2_hw_phy_config(tp);
3650 case RTL_GIGA_MAC_VER_41:
3656 static void rtl_phy_work(struct rtl8169_private *tp)
3658 struct timer_list *timer = &tp->timer;
3659 void __iomem *ioaddr = tp->mmio_addr;
3660 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3662 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
3664 if (tp->phy_reset_pending(tp)) {
3666 * A busy loop could burn quite a few cycles on nowadays CPU.
3667 * Let's delay the execution of the timer for a few ticks.
3673 if (tp->link_ok(ioaddr))
3676 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
3678 tp->phy_reset_enable(tp);
3681 mod_timer(timer, jiffies + timeout);
3684 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3686 if (!test_and_set_bit(flag, tp->wk.flags))
3687 schedule_work(&tp->wk.work);
3690 static void rtl8169_phy_timer(unsigned long __opaque)
3692 struct net_device *dev = (struct net_device *)__opaque;
3693 struct rtl8169_private *tp = netdev_priv(dev);
3695 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
3698 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3699 void __iomem *ioaddr)
3702 pci_release_regions(pdev);
3703 pci_clear_mwi(pdev);
3704 pci_disable_device(pdev);
3708 DECLARE_RTL_COND(rtl_phy_reset_cond)
3710 return tp->phy_reset_pending(tp);
3713 static void rtl8169_phy_reset(struct net_device *dev,
3714 struct rtl8169_private *tp)
3716 tp->phy_reset_enable(tp);
3717 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
3720 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3722 void __iomem *ioaddr = tp->mmio_addr;
3724 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3725 (RTL_R8(PHYstatus) & TBI_Enable);
3728 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
3730 void __iomem *ioaddr = tp->mmio_addr;
3732 rtl_hw_phy_config(dev);
3734 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3735 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3739 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3741 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3742 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
3744 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
3745 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3747 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
3748 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
3751 rtl8169_phy_reset(dev, tp);
3753 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3754 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3755 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3756 (tp->mii.supports_gmii ?
3757 ADVERTISED_1000baseT_Half |
3758 ADVERTISED_1000baseT_Full : 0));
3760 if (rtl_tbi_enabled(tp))
3761 netif_info(tp, link, dev, "TBI auto-negotiating\n");
3764 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3766 void __iomem *ioaddr = tp->mmio_addr;
3770 RTL_W8(Cfg9346, Cfg9346_Unlock);
3772 RTL_W32(MAC4, addr[4] | addr[5] << 8);
3775 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3778 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3779 rtl_rar_exgmac_set(tp, addr);
3781 RTL_W8(Cfg9346, Cfg9346_Lock);
3783 rtl_unlock_work(tp);
3786 static int rtl_set_mac_address(struct net_device *dev, void *p)
3788 struct rtl8169_private *tp = netdev_priv(dev);
3789 struct sockaddr *addr = p;
3791 if (!is_valid_ether_addr(addr->sa_data))
3792 return -EADDRNOTAVAIL;
3794 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3796 rtl_rar_set(tp, dev->dev_addr);
3801 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3803 struct rtl8169_private *tp = netdev_priv(dev);
3804 struct mii_ioctl_data *data = if_mii(ifr);
3806 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3809 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3810 struct mii_ioctl_data *data, int cmd)
3814 data->phy_id = 32; /* Internal PHY */
3818 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
3822 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
3828 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3833 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3835 if (tp->features & RTL_FEATURE_MSI) {
3836 pci_disable_msi(pdev);
3837 tp->features &= ~RTL_FEATURE_MSI;
3841 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
3843 struct mdio_ops *ops = &tp->mdio_ops;
3845 switch (tp->mac_version) {
3846 case RTL_GIGA_MAC_VER_27:
3847 ops->write = r8168dp_1_mdio_write;
3848 ops->read = r8168dp_1_mdio_read;
3850 case RTL_GIGA_MAC_VER_28:
3851 case RTL_GIGA_MAC_VER_31:
3852 ops->write = r8168dp_2_mdio_write;
3853 ops->read = r8168dp_2_mdio_read;
3855 case RTL_GIGA_MAC_VER_40:
3856 case RTL_GIGA_MAC_VER_41:
3857 case RTL_GIGA_MAC_VER_42:
3858 case RTL_GIGA_MAC_VER_43:
3859 case RTL_GIGA_MAC_VER_44:
3860 ops->write = r8168g_mdio_write;
3861 ops->read = r8168g_mdio_read;
3864 ops->write = r8169_mdio_write;
3865 ops->read = r8169_mdio_read;
3870 static void rtl_speed_down(struct rtl8169_private *tp)
3875 rtl_writephy(tp, 0x1f, 0x0000);
3876 lpa = rtl_readphy(tp, MII_LPA);
3878 if (lpa & (LPA_10HALF | LPA_10FULL))
3879 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
3880 else if (lpa & (LPA_100HALF | LPA_100FULL))
3881 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3882 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
3884 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3885 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3886 (tp->mii.supports_gmii ?
3887 ADVERTISED_1000baseT_Half |
3888 ADVERTISED_1000baseT_Full : 0);
3890 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3894 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3896 void __iomem *ioaddr = tp->mmio_addr;
3898 switch (tp->mac_version) {
3899 case RTL_GIGA_MAC_VER_25:
3900 case RTL_GIGA_MAC_VER_26:
3901 case RTL_GIGA_MAC_VER_29:
3902 case RTL_GIGA_MAC_VER_30:
3903 case RTL_GIGA_MAC_VER_32:
3904 case RTL_GIGA_MAC_VER_33:
3905 case RTL_GIGA_MAC_VER_34:
3906 case RTL_GIGA_MAC_VER_37:
3907 case RTL_GIGA_MAC_VER_38:
3908 case RTL_GIGA_MAC_VER_39:
3909 case RTL_GIGA_MAC_VER_40:
3910 case RTL_GIGA_MAC_VER_41:
3911 case RTL_GIGA_MAC_VER_42:
3912 case RTL_GIGA_MAC_VER_43:
3913 case RTL_GIGA_MAC_VER_44:
3914 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3915 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3922 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3924 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3928 rtl_wol_suspend_quirk(tp);
3933 static void r810x_phy_power_down(struct rtl8169_private *tp)
3935 rtl_writephy(tp, 0x1f, 0x0000);
3936 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3939 static void r810x_phy_power_up(struct rtl8169_private *tp)
3941 rtl_writephy(tp, 0x1f, 0x0000);
3942 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3945 static void r810x_pll_power_down(struct rtl8169_private *tp)
3947 void __iomem *ioaddr = tp->mmio_addr;
3949 if (rtl_wol_pll_power_down(tp))
3952 r810x_phy_power_down(tp);
3954 switch (tp->mac_version) {
3955 case RTL_GIGA_MAC_VER_07:
3956 case RTL_GIGA_MAC_VER_08:
3957 case RTL_GIGA_MAC_VER_09:
3958 case RTL_GIGA_MAC_VER_10:
3959 case RTL_GIGA_MAC_VER_13:
3960 case RTL_GIGA_MAC_VER_16:
3963 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3968 static void r810x_pll_power_up(struct rtl8169_private *tp)
3970 void __iomem *ioaddr = tp->mmio_addr;
3972 r810x_phy_power_up(tp);
3974 switch (tp->mac_version) {
3975 case RTL_GIGA_MAC_VER_07:
3976 case RTL_GIGA_MAC_VER_08:
3977 case RTL_GIGA_MAC_VER_09:
3978 case RTL_GIGA_MAC_VER_10:
3979 case RTL_GIGA_MAC_VER_13:
3980 case RTL_GIGA_MAC_VER_16:
3983 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3988 static void r8168_phy_power_up(struct rtl8169_private *tp)
3990 rtl_writephy(tp, 0x1f, 0x0000);
3991 switch (tp->mac_version) {
3992 case RTL_GIGA_MAC_VER_11:
3993 case RTL_GIGA_MAC_VER_12:
3994 case RTL_GIGA_MAC_VER_17:
3995 case RTL_GIGA_MAC_VER_18:
3996 case RTL_GIGA_MAC_VER_19:
3997 case RTL_GIGA_MAC_VER_20:
3998 case RTL_GIGA_MAC_VER_21:
3999 case RTL_GIGA_MAC_VER_22:
4000 case RTL_GIGA_MAC_VER_23:
4001 case RTL_GIGA_MAC_VER_24:
4002 case RTL_GIGA_MAC_VER_25:
4003 case RTL_GIGA_MAC_VER_26:
4004 case RTL_GIGA_MAC_VER_27:
4005 case RTL_GIGA_MAC_VER_28:
4006 case RTL_GIGA_MAC_VER_31:
4007 rtl_writephy(tp, 0x0e, 0x0000);
4012 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4015 static void r8168_phy_power_down(struct rtl8169_private *tp)
4017 rtl_writephy(tp, 0x1f, 0x0000);
4018 switch (tp->mac_version) {
4019 case RTL_GIGA_MAC_VER_32:
4020 case RTL_GIGA_MAC_VER_33:
4021 case RTL_GIGA_MAC_VER_40:
4022 case RTL_GIGA_MAC_VER_41:
4023 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4026 case RTL_GIGA_MAC_VER_11:
4027 case RTL_GIGA_MAC_VER_12:
4028 case RTL_GIGA_MAC_VER_17:
4029 case RTL_GIGA_MAC_VER_18:
4030 case RTL_GIGA_MAC_VER_19:
4031 case RTL_GIGA_MAC_VER_20:
4032 case RTL_GIGA_MAC_VER_21:
4033 case RTL_GIGA_MAC_VER_22:
4034 case RTL_GIGA_MAC_VER_23:
4035 case RTL_GIGA_MAC_VER_24:
4036 case RTL_GIGA_MAC_VER_25:
4037 case RTL_GIGA_MAC_VER_26:
4038 case RTL_GIGA_MAC_VER_27:
4039 case RTL_GIGA_MAC_VER_28:
4040 case RTL_GIGA_MAC_VER_31:
4041 rtl_writephy(tp, 0x0e, 0x0200);
4043 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4048 static void r8168_pll_power_down(struct rtl8169_private *tp)
4050 void __iomem *ioaddr = tp->mmio_addr;
4052 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4053 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4054 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
4055 r8168dp_check_dash(tp)) {
4059 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4060 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
4061 (RTL_R16(CPlusCmd) & ASF)) {
4065 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4066 tp->mac_version == RTL_GIGA_MAC_VER_33)
4067 rtl_ephy_write(tp, 0x19, 0xff64);
4069 if (rtl_wol_pll_power_down(tp))
4072 r8168_phy_power_down(tp);
4074 switch (tp->mac_version) {
4075 case RTL_GIGA_MAC_VER_25:
4076 case RTL_GIGA_MAC_VER_26:
4077 case RTL_GIGA_MAC_VER_27:
4078 case RTL_GIGA_MAC_VER_28:
4079 case RTL_GIGA_MAC_VER_31:
4080 case RTL_GIGA_MAC_VER_32:
4081 case RTL_GIGA_MAC_VER_33:
4082 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4084 case RTL_GIGA_MAC_VER_40:
4085 case RTL_GIGA_MAC_VER_41:
4086 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4087 0xfc000000, ERIAR_EXGMAC);
4092 static void r8168_pll_power_up(struct rtl8169_private *tp)
4094 void __iomem *ioaddr = tp->mmio_addr;
4096 switch (tp->mac_version) {
4097 case RTL_GIGA_MAC_VER_25:
4098 case RTL_GIGA_MAC_VER_26:
4099 case RTL_GIGA_MAC_VER_27:
4100 case RTL_GIGA_MAC_VER_28:
4101 case RTL_GIGA_MAC_VER_31:
4102 case RTL_GIGA_MAC_VER_32:
4103 case RTL_GIGA_MAC_VER_33:
4104 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4106 case RTL_GIGA_MAC_VER_40:
4107 case RTL_GIGA_MAC_VER_41:
4108 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4109 0x00000000, ERIAR_EXGMAC);
4113 r8168_phy_power_up(tp);
4116 static void rtl_generic_op(struct rtl8169_private *tp,
4117 void (*op)(struct rtl8169_private *))
4123 static void rtl_pll_power_down(struct rtl8169_private *tp)
4125 rtl_generic_op(tp, tp->pll_power_ops.down);
4128 static void rtl_pll_power_up(struct rtl8169_private *tp)
4130 rtl_generic_op(tp, tp->pll_power_ops.up);
4133 static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
4135 struct pll_power_ops *ops = &tp->pll_power_ops;
4137 switch (tp->mac_version) {
4138 case RTL_GIGA_MAC_VER_07:
4139 case RTL_GIGA_MAC_VER_08:
4140 case RTL_GIGA_MAC_VER_09:
4141 case RTL_GIGA_MAC_VER_10:
4142 case RTL_GIGA_MAC_VER_16:
4143 case RTL_GIGA_MAC_VER_29:
4144 case RTL_GIGA_MAC_VER_30:
4145 case RTL_GIGA_MAC_VER_37:
4146 case RTL_GIGA_MAC_VER_39:
4147 case RTL_GIGA_MAC_VER_43:
4148 ops->down = r810x_pll_power_down;
4149 ops->up = r810x_pll_power_up;
4152 case RTL_GIGA_MAC_VER_11:
4153 case RTL_GIGA_MAC_VER_12:
4154 case RTL_GIGA_MAC_VER_17:
4155 case RTL_GIGA_MAC_VER_18:
4156 case RTL_GIGA_MAC_VER_19:
4157 case RTL_GIGA_MAC_VER_20:
4158 case RTL_GIGA_MAC_VER_21:
4159 case RTL_GIGA_MAC_VER_22:
4160 case RTL_GIGA_MAC_VER_23:
4161 case RTL_GIGA_MAC_VER_24:
4162 case RTL_GIGA_MAC_VER_25:
4163 case RTL_GIGA_MAC_VER_26:
4164 case RTL_GIGA_MAC_VER_27:
4165 case RTL_GIGA_MAC_VER_28:
4166 case RTL_GIGA_MAC_VER_31:
4167 case RTL_GIGA_MAC_VER_32:
4168 case RTL_GIGA_MAC_VER_33:
4169 case RTL_GIGA_MAC_VER_34:
4170 case RTL_GIGA_MAC_VER_35:
4171 case RTL_GIGA_MAC_VER_36:
4172 case RTL_GIGA_MAC_VER_38:
4173 case RTL_GIGA_MAC_VER_40:
4174 case RTL_GIGA_MAC_VER_41:
4175 case RTL_GIGA_MAC_VER_42:
4176 case RTL_GIGA_MAC_VER_44:
4177 ops->down = r8168_pll_power_down;
4178 ops->up = r8168_pll_power_up;
4188 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4190 void __iomem *ioaddr = tp->mmio_addr;
4192 switch (tp->mac_version) {
4193 case RTL_GIGA_MAC_VER_01:
4194 case RTL_GIGA_MAC_VER_02:
4195 case RTL_GIGA_MAC_VER_03:
4196 case RTL_GIGA_MAC_VER_04:
4197 case RTL_GIGA_MAC_VER_05:
4198 case RTL_GIGA_MAC_VER_06:
4199 case RTL_GIGA_MAC_VER_10:
4200 case RTL_GIGA_MAC_VER_11:
4201 case RTL_GIGA_MAC_VER_12:
4202 case RTL_GIGA_MAC_VER_13:
4203 case RTL_GIGA_MAC_VER_14:
4204 case RTL_GIGA_MAC_VER_15:
4205 case RTL_GIGA_MAC_VER_16:
4206 case RTL_GIGA_MAC_VER_17:
4207 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4209 case RTL_GIGA_MAC_VER_18:
4210 case RTL_GIGA_MAC_VER_19:
4211 case RTL_GIGA_MAC_VER_20:
4212 case RTL_GIGA_MAC_VER_21:
4213 case RTL_GIGA_MAC_VER_22:
4214 case RTL_GIGA_MAC_VER_23:
4215 case RTL_GIGA_MAC_VER_24:
4216 case RTL_GIGA_MAC_VER_34:
4217 case RTL_GIGA_MAC_VER_35:
4218 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4220 case RTL_GIGA_MAC_VER_40:
4221 case RTL_GIGA_MAC_VER_41:
4222 case RTL_GIGA_MAC_VER_42:
4223 case RTL_GIGA_MAC_VER_43:
4224 case RTL_GIGA_MAC_VER_44:
4225 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4228 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4233 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4235 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4238 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4240 void __iomem *ioaddr = tp->mmio_addr;
4242 RTL_W8(Cfg9346, Cfg9346_Unlock);
4243 rtl_generic_op(tp, tp->jumbo_ops.enable);
4244 RTL_W8(Cfg9346, Cfg9346_Lock);
4247 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4249 void __iomem *ioaddr = tp->mmio_addr;
4251 RTL_W8(Cfg9346, Cfg9346_Unlock);
4252 rtl_generic_op(tp, tp->jumbo_ops.disable);
4253 RTL_W8(Cfg9346, Cfg9346_Lock);
4256 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4258 void __iomem *ioaddr = tp->mmio_addr;
4260 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4261 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4262 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4265 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4267 void __iomem *ioaddr = tp->mmio_addr;
4269 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4270 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4271 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4274 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4276 void __iomem *ioaddr = tp->mmio_addr;
4278 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4281 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4283 void __iomem *ioaddr = tp->mmio_addr;
4285 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4288 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4290 void __iomem *ioaddr = tp->mmio_addr;
4292 RTL_W8(MaxTxPacketSize, 0x3f);
4293 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4294 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
4295 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4298 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4300 void __iomem *ioaddr = tp->mmio_addr;
4302 RTL_W8(MaxTxPacketSize, 0x0c);
4303 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4304 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
4305 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4308 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4310 rtl_tx_performance_tweak(tp->pci_dev,
4311 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4314 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4316 rtl_tx_performance_tweak(tp->pci_dev,
4317 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4320 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4322 void __iomem *ioaddr = tp->mmio_addr;
4324 r8168b_0_hw_jumbo_enable(tp);
4326 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4329 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4331 void __iomem *ioaddr = tp->mmio_addr;
4333 r8168b_0_hw_jumbo_disable(tp);
4335 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4338 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4340 struct jumbo_ops *ops = &tp->jumbo_ops;
4342 switch (tp->mac_version) {
4343 case RTL_GIGA_MAC_VER_11:
4344 ops->disable = r8168b_0_hw_jumbo_disable;
4345 ops->enable = r8168b_0_hw_jumbo_enable;
4347 case RTL_GIGA_MAC_VER_12:
4348 case RTL_GIGA_MAC_VER_17:
4349 ops->disable = r8168b_1_hw_jumbo_disable;
4350 ops->enable = r8168b_1_hw_jumbo_enable;
4352 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4353 case RTL_GIGA_MAC_VER_19:
4354 case RTL_GIGA_MAC_VER_20:
4355 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4356 case RTL_GIGA_MAC_VER_22:
4357 case RTL_GIGA_MAC_VER_23:
4358 case RTL_GIGA_MAC_VER_24:
4359 case RTL_GIGA_MAC_VER_25:
4360 case RTL_GIGA_MAC_VER_26:
4361 ops->disable = r8168c_hw_jumbo_disable;
4362 ops->enable = r8168c_hw_jumbo_enable;
4364 case RTL_GIGA_MAC_VER_27:
4365 case RTL_GIGA_MAC_VER_28:
4366 ops->disable = r8168dp_hw_jumbo_disable;
4367 ops->enable = r8168dp_hw_jumbo_enable;
4369 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4370 case RTL_GIGA_MAC_VER_32:
4371 case RTL_GIGA_MAC_VER_33:
4372 case RTL_GIGA_MAC_VER_34:
4373 ops->disable = r8168e_hw_jumbo_disable;
4374 ops->enable = r8168e_hw_jumbo_enable;
4378 * No action needed for jumbo frames with 8169.
4379 * No jumbo for 810x at all.
4381 case RTL_GIGA_MAC_VER_40:
4382 case RTL_GIGA_MAC_VER_41:
4383 case RTL_GIGA_MAC_VER_42:
4384 case RTL_GIGA_MAC_VER_43:
4385 case RTL_GIGA_MAC_VER_44:
4387 ops->disable = NULL;
4393 DECLARE_RTL_COND(rtl_chipcmd_cond)
4395 void __iomem *ioaddr = tp->mmio_addr;
4397 return RTL_R8(ChipCmd) & CmdReset;
4400 static void rtl_hw_reset(struct rtl8169_private *tp)
4402 void __iomem *ioaddr = tp->mmio_addr;
4404 RTL_W8(ChipCmd, CmdReset);
4406 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4409 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4411 struct rtl_fw *rtl_fw;
4415 name = rtl_lookup_firmware_name(tp);
4417 goto out_no_firmware;
4419 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4423 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4427 rc = rtl_check_firmware(tp, rtl_fw);
4429 goto err_release_firmware;
4431 tp->rtl_fw = rtl_fw;
4435 err_release_firmware:
4436 release_firmware(rtl_fw->fw);
4440 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4447 static void rtl_request_firmware(struct rtl8169_private *tp)
4449 if (IS_ERR(tp->rtl_fw))
4450 rtl_request_uncached_firmware(tp);
4453 static void rtl_rx_close(struct rtl8169_private *tp)
4455 void __iomem *ioaddr = tp->mmio_addr;
4457 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4460 DECLARE_RTL_COND(rtl_npq_cond)
4462 void __iomem *ioaddr = tp->mmio_addr;
4464 return RTL_R8(TxPoll) & NPQ;
4467 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4469 void __iomem *ioaddr = tp->mmio_addr;
4471 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4474 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4476 void __iomem *ioaddr = tp->mmio_addr;
4478 /* Disable interrupts */
4479 rtl8169_irq_mask_and_ack(tp);
4483 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4484 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4485 tp->mac_version == RTL_GIGA_MAC_VER_31) {
4486 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4487 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4488 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4489 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
4490 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4491 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4492 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
4493 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
4494 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
4495 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
4496 tp->mac_version == RTL_GIGA_MAC_VER_38) {
4497 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4498 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4500 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4507 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4509 void __iomem *ioaddr = tp->mmio_addr;
4511 /* Set DMA burst size and Interframe Gap Time */
4512 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4513 (InterFrameGap << TxInterFrameGapShift));
4516 static void rtl_hw_start(struct net_device *dev)
4518 struct rtl8169_private *tp = netdev_priv(dev);
4522 rtl_irq_enable_all(tp);
4525 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4526 void __iomem *ioaddr)
4529 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4530 * register to be written before TxDescAddrLow to work.
4531 * Switching from MMIO to I/O access fixes the issue as well.
4533 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4534 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4535 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4536 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4539 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4543 cmd = RTL_R16(CPlusCmd);
4544 RTL_W16(CPlusCmd, cmd);
4548 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
4550 /* Low hurts. Let's disable the filtering. */
4551 RTL_W16(RxMaxSize, rx_buf_sz + 1);
4554 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4556 static const struct rtl_cfg2_info {
4561 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4562 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4563 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4564 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4566 const struct rtl_cfg2_info *p = cfg2_info;
4570 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
4571 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4572 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4573 RTL_W32(0x7c, p->val);
4579 static void rtl_set_rx_mode(struct net_device *dev)
4581 struct rtl8169_private *tp = netdev_priv(dev);
4582 void __iomem *ioaddr = tp->mmio_addr;
4583 u32 mc_filter[2]; /* Multicast hash filter */
4587 if (dev->flags & IFF_PROMISC) {
4588 /* Unconditionally log net taps. */
4589 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4591 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4593 mc_filter[1] = mc_filter[0] = 0xffffffff;
4594 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4595 (dev->flags & IFF_ALLMULTI)) {
4596 /* Too many to filter perfectly -- accept all multicasts. */
4597 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4598 mc_filter[1] = mc_filter[0] = 0xffffffff;
4600 struct netdev_hw_addr *ha;
4602 rx_mode = AcceptBroadcast | AcceptMyPhys;
4603 mc_filter[1] = mc_filter[0] = 0;
4604 netdev_for_each_mc_addr(ha, dev) {
4605 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4606 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4607 rx_mode |= AcceptMulticast;
4611 if (dev->features & NETIF_F_RXALL)
4612 rx_mode |= (AcceptErr | AcceptRunt);
4614 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4616 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4617 u32 data = mc_filter[0];
4619 mc_filter[0] = swab32(mc_filter[1]);
4620 mc_filter[1] = swab32(data);
4623 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4624 mc_filter[1] = mc_filter[0] = 0xffffffff;
4626 RTL_W32(MAR0 + 4, mc_filter[1]);
4627 RTL_W32(MAR0 + 0, mc_filter[0]);
4629 RTL_W32(RxConfig, tmp);
4632 static void rtl_hw_start_8169(struct net_device *dev)
4634 struct rtl8169_private *tp = netdev_priv(dev);
4635 void __iomem *ioaddr = tp->mmio_addr;
4636 struct pci_dev *pdev = tp->pci_dev;
4638 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4639 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4640 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4643 RTL_W8(Cfg9346, Cfg9346_Unlock);
4644 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4645 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4646 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4647 tp->mac_version == RTL_GIGA_MAC_VER_04)
4648 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4652 RTL_W8(EarlyTxThres, NoEarlyTx);
4654 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4656 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4657 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4658 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4659 tp->mac_version == RTL_GIGA_MAC_VER_04)
4660 rtl_set_rx_tx_config_registers(tp);
4662 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4664 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4665 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4666 dprintk("Set MAC Reg C+CR Offset 0xE0. "
4667 "Bit-3 and bit-14 MUST be 1\n");
4668 tp->cp_cmd |= (1 << 14);
4671 RTL_W16(CPlusCmd, tp->cp_cmd);
4673 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4676 * Undocumented corner. Supposedly:
4677 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4679 RTL_W16(IntrMitigate, 0x0000);
4681 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4683 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4684 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4685 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4686 tp->mac_version != RTL_GIGA_MAC_VER_04) {
4687 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4688 rtl_set_rx_tx_config_registers(tp);
4691 RTL_W8(Cfg9346, Cfg9346_Lock);
4693 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4696 RTL_W32(RxMissed, 0);
4698 rtl_set_rx_mode(dev);
4700 /* no early-rx interrupts */
4701 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4704 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4706 if (tp->csi_ops.write)
4707 tp->csi_ops.write(tp, addr, value);
4710 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4712 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
4715 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
4719 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4720 rtl_csi_write(tp, 0x070c, csi | bits);
4723 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
4725 rtl_csi_access_enable(tp, 0x17000000);
4728 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
4730 rtl_csi_access_enable(tp, 0x27000000);
4733 DECLARE_RTL_COND(rtl_csiar_cond)
4735 void __iomem *ioaddr = tp->mmio_addr;
4737 return RTL_R32(CSIAR) & CSIAR_FLAG;
4740 static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
4742 void __iomem *ioaddr = tp->mmio_addr;
4744 RTL_W32(CSIDR, value);
4745 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4746 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4748 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4751 static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
4753 void __iomem *ioaddr = tp->mmio_addr;
4755 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4756 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4758 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4759 RTL_R32(CSIDR) : ~0;
4762 static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
4764 void __iomem *ioaddr = tp->mmio_addr;
4766 RTL_W32(CSIDR, value);
4767 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4768 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4771 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4774 static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
4776 void __iomem *ioaddr = tp->mmio_addr;
4778 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4779 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4781 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4782 RTL_R32(CSIDR) : ~0;
4785 static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
4787 void __iomem *ioaddr = tp->mmio_addr;
4789 RTL_W32(CSIDR, value);
4790 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4791 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4794 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4797 static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
4799 void __iomem *ioaddr = tp->mmio_addr;
4801 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
4802 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4804 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4805 RTL_R32(CSIDR) : ~0;
4808 static void rtl_init_csi_ops(struct rtl8169_private *tp)
4810 struct csi_ops *ops = &tp->csi_ops;
4812 switch (tp->mac_version) {
4813 case RTL_GIGA_MAC_VER_01:
4814 case RTL_GIGA_MAC_VER_02:
4815 case RTL_GIGA_MAC_VER_03:
4816 case RTL_GIGA_MAC_VER_04:
4817 case RTL_GIGA_MAC_VER_05:
4818 case RTL_GIGA_MAC_VER_06:
4819 case RTL_GIGA_MAC_VER_10:
4820 case RTL_GIGA_MAC_VER_11:
4821 case RTL_GIGA_MAC_VER_12:
4822 case RTL_GIGA_MAC_VER_13:
4823 case RTL_GIGA_MAC_VER_14:
4824 case RTL_GIGA_MAC_VER_15:
4825 case RTL_GIGA_MAC_VER_16:
4826 case RTL_GIGA_MAC_VER_17:
4831 case RTL_GIGA_MAC_VER_37:
4832 case RTL_GIGA_MAC_VER_38:
4833 ops->write = r8402_csi_write;
4834 ops->read = r8402_csi_read;
4837 case RTL_GIGA_MAC_VER_44:
4838 ops->write = r8411_csi_write;
4839 ops->read = r8411_csi_read;
4843 ops->write = r8169_csi_write;
4844 ops->read = r8169_csi_read;
4850 unsigned int offset;
4855 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4861 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4862 rtl_ephy_write(tp, e->offset, w);
4867 static void rtl_disable_clock_request(struct pci_dev *pdev)
4869 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4870 PCI_EXP_LNKCTL_CLKREQ_EN);
4873 static void rtl_enable_clock_request(struct pci_dev *pdev)
4875 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4876 PCI_EXP_LNKCTL_CLKREQ_EN);
4879 #define R8168_CPCMD_QUIRK_MASK (\
4890 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4892 void __iomem *ioaddr = tp->mmio_addr;
4893 struct pci_dev *pdev = tp->pci_dev;
4895 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4897 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4899 if (tp->dev->mtu <= ETH_DATA_LEN) {
4900 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
4901 PCI_EXP_DEVCTL_NOSNOOP_EN);
4905 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4907 void __iomem *ioaddr = tp->mmio_addr;
4909 rtl_hw_start_8168bb(tp);
4911 RTL_W8(MaxTxPacketSize, TxPacketMax);
4913 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4916 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4918 void __iomem *ioaddr = tp->mmio_addr;
4919 struct pci_dev *pdev = tp->pci_dev;
4921 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4923 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4925 if (tp->dev->mtu <= ETH_DATA_LEN)
4926 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4928 rtl_disable_clock_request(pdev);
4930 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4933 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4935 static const struct ephy_info e_info_8168cp[] = {
4936 { 0x01, 0, 0x0001 },
4937 { 0x02, 0x0800, 0x1000 },
4938 { 0x03, 0, 0x0042 },
4939 { 0x06, 0x0080, 0x0000 },
4943 rtl_csi_access_enable_2(tp);
4945 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4947 __rtl_hw_start_8168cp(tp);
4950 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4952 void __iomem *ioaddr = tp->mmio_addr;
4953 struct pci_dev *pdev = tp->pci_dev;
4955 rtl_csi_access_enable_2(tp);
4957 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4959 if (tp->dev->mtu <= ETH_DATA_LEN)
4960 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4962 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4965 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4967 void __iomem *ioaddr = tp->mmio_addr;
4968 struct pci_dev *pdev = tp->pci_dev;
4970 rtl_csi_access_enable_2(tp);
4972 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4975 RTL_W8(DBG_REG, 0x20);
4977 RTL_W8(MaxTxPacketSize, TxPacketMax);
4979 if (tp->dev->mtu <= ETH_DATA_LEN)
4980 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4982 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4985 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4987 void __iomem *ioaddr = tp->mmio_addr;
4988 static const struct ephy_info e_info_8168c_1[] = {
4989 { 0x02, 0x0800, 0x1000 },
4990 { 0x03, 0, 0x0002 },
4991 { 0x06, 0x0080, 0x0000 }
4994 rtl_csi_access_enable_2(tp);
4996 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4998 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
5000 __rtl_hw_start_8168cp(tp);
5003 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
5005 static const struct ephy_info e_info_8168c_2[] = {
5006 { 0x01, 0, 0x0001 },
5007 { 0x03, 0x0400, 0x0220 }
5010 rtl_csi_access_enable_2(tp);
5012 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
5014 __rtl_hw_start_8168cp(tp);
5017 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
5019 rtl_hw_start_8168c_2(tp);
5022 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
5024 rtl_csi_access_enable_2(tp);
5026 __rtl_hw_start_8168cp(tp);
5029 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
5031 void __iomem *ioaddr = tp->mmio_addr;
5032 struct pci_dev *pdev = tp->pci_dev;
5034 rtl_csi_access_enable_2(tp);
5036 rtl_disable_clock_request(pdev);
5038 RTL_W8(MaxTxPacketSize, TxPacketMax);
5040 if (tp->dev->mtu <= ETH_DATA_LEN)
5041 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5043 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5046 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
5048 void __iomem *ioaddr = tp->mmio_addr;
5049 struct pci_dev *pdev = tp->pci_dev;
5051 rtl_csi_access_enable_1(tp);
5053 if (tp->dev->mtu <= ETH_DATA_LEN)
5054 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5056 RTL_W8(MaxTxPacketSize, TxPacketMax);
5058 rtl_disable_clock_request(pdev);
5061 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
5063 void __iomem *ioaddr = tp->mmio_addr;
5064 struct pci_dev *pdev = tp->pci_dev;
5065 static const struct ephy_info e_info_8168d_4[] = {
5067 { 0x19, 0x20, 0x50 },
5072 rtl_csi_access_enable_1(tp);
5074 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5076 RTL_W8(MaxTxPacketSize, TxPacketMax);
5078 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
5079 const struct ephy_info *e = e_info_8168d_4 + i;
5082 w = rtl_ephy_read(tp, e->offset);
5083 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
5086 rtl_enable_clock_request(pdev);
5089 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
5091 void __iomem *ioaddr = tp->mmio_addr;
5092 struct pci_dev *pdev = tp->pci_dev;
5093 static const struct ephy_info e_info_8168e_1[] = {
5094 { 0x00, 0x0200, 0x0100 },
5095 { 0x00, 0x0000, 0x0004 },
5096 { 0x06, 0x0002, 0x0001 },
5097 { 0x06, 0x0000, 0x0030 },
5098 { 0x07, 0x0000, 0x2000 },
5099 { 0x00, 0x0000, 0x0020 },
5100 { 0x03, 0x5800, 0x2000 },
5101 { 0x03, 0x0000, 0x0001 },
5102 { 0x01, 0x0800, 0x1000 },
5103 { 0x07, 0x0000, 0x4000 },
5104 { 0x1e, 0x0000, 0x2000 },
5105 { 0x19, 0xffff, 0xfe6c },
5106 { 0x0a, 0x0000, 0x0040 }
5109 rtl_csi_access_enable_2(tp);
5111 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5113 if (tp->dev->mtu <= ETH_DATA_LEN)
5114 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5116 RTL_W8(MaxTxPacketSize, TxPacketMax);
5118 rtl_disable_clock_request(pdev);
5120 /* Reset tx FIFO pointer */
5121 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5122 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
5124 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5127 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5129 void __iomem *ioaddr = tp->mmio_addr;
5130 struct pci_dev *pdev = tp->pci_dev;
5131 static const struct ephy_info e_info_8168e_2[] = {
5132 { 0x09, 0x0000, 0x0080 },
5133 { 0x19, 0x0000, 0x0224 }
5136 rtl_csi_access_enable_1(tp);
5138 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5140 if (tp->dev->mtu <= ETH_DATA_LEN)
5141 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5143 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5144 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5145 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5146 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5147 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5148 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5149 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5150 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5152 RTL_W8(MaxTxPacketSize, EarlySize);
5154 rtl_disable_clock_request(pdev);
5156 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5157 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5159 /* Adjust EEE LED frequency */
5160 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5162 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5163 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5164 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5167 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5169 void __iomem *ioaddr = tp->mmio_addr;
5170 struct pci_dev *pdev = tp->pci_dev;
5172 rtl_csi_access_enable_2(tp);
5174 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5176 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5177 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5178 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5179 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5180 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5181 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5182 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5183 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5184 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5185 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5187 RTL_W8(MaxTxPacketSize, EarlySize);
5189 rtl_disable_clock_request(pdev);
5191 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5192 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5193 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5194 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5195 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5198 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5200 void __iomem *ioaddr = tp->mmio_addr;
5201 static const struct ephy_info e_info_8168f_1[] = {
5202 { 0x06, 0x00c0, 0x0020 },
5203 { 0x08, 0x0001, 0x0002 },
5204 { 0x09, 0x0000, 0x0080 },
5205 { 0x19, 0x0000, 0x0224 }
5208 rtl_hw_start_8168f(tp);
5210 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5212 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5214 /* Adjust EEE LED frequency */
5215 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5218 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5220 static const struct ephy_info e_info_8168f_1[] = {
5221 { 0x06, 0x00c0, 0x0020 },
5222 { 0x0f, 0xffff, 0x5200 },
5223 { 0x1e, 0x0000, 0x4000 },
5224 { 0x19, 0x0000, 0x0224 }
5227 rtl_hw_start_8168f(tp);
5229 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5231 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5234 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5236 void __iomem *ioaddr = tp->mmio_addr;
5237 struct pci_dev *pdev = tp->pci_dev;
5239 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5241 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5242 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5243 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5244 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5246 rtl_csi_access_enable_1(tp);
5248 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5250 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5251 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5252 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
5254 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5255 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
5256 RTL_W8(MaxTxPacketSize, EarlySize);
5258 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5259 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5261 /* Adjust EEE LED frequency */
5262 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5264 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5265 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5268 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5270 void __iomem *ioaddr = tp->mmio_addr;
5271 static const struct ephy_info e_info_8168g_2[] = {
5272 { 0x00, 0x0000, 0x0008 },
5273 { 0x0c, 0x3df0, 0x0200 },
5274 { 0x19, 0xffff, 0xfc00 },
5275 { 0x1e, 0xffff, 0x20eb }
5278 rtl_hw_start_8168g_1(tp);
5280 /* disable aspm and clock request before access ephy */
5281 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5282 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5283 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5286 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5288 void __iomem *ioaddr = tp->mmio_addr;
5289 static const struct ephy_info e_info_8411_2[] = {
5290 { 0x00, 0x0000, 0x0008 },
5291 { 0x0c, 0x3df0, 0x0200 },
5292 { 0x0f, 0xffff, 0x5200 },
5293 { 0x19, 0x0020, 0x0000 },
5294 { 0x1e, 0x0000, 0x2000 }
5297 rtl_hw_start_8168g_1(tp);
5299 /* disable aspm and clock request before access ephy */
5300 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5301 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5302 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5305 static void rtl_hw_start_8168(struct net_device *dev)
5307 struct rtl8169_private *tp = netdev_priv(dev);
5308 void __iomem *ioaddr = tp->mmio_addr;
5310 RTL_W8(Cfg9346, Cfg9346_Unlock);
5312 RTL_W8(MaxTxPacketSize, TxPacketMax);
5314 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5316 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
5318 RTL_W16(CPlusCmd, tp->cp_cmd);
5320 RTL_W16(IntrMitigate, 0x5151);
5322 /* Work around for RxFIFO overflow. */
5323 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5324 tp->event_slow |= RxFIFOOver | PCSTimeout;
5325 tp->event_slow &= ~RxOverflow;
5328 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5330 rtl_set_rx_tx_config_registers(tp);
5334 switch (tp->mac_version) {
5335 case RTL_GIGA_MAC_VER_11:
5336 rtl_hw_start_8168bb(tp);
5339 case RTL_GIGA_MAC_VER_12:
5340 case RTL_GIGA_MAC_VER_17:
5341 rtl_hw_start_8168bef(tp);
5344 case RTL_GIGA_MAC_VER_18:
5345 rtl_hw_start_8168cp_1(tp);
5348 case RTL_GIGA_MAC_VER_19:
5349 rtl_hw_start_8168c_1(tp);
5352 case RTL_GIGA_MAC_VER_20:
5353 rtl_hw_start_8168c_2(tp);
5356 case RTL_GIGA_MAC_VER_21:
5357 rtl_hw_start_8168c_3(tp);
5360 case RTL_GIGA_MAC_VER_22:
5361 rtl_hw_start_8168c_4(tp);
5364 case RTL_GIGA_MAC_VER_23:
5365 rtl_hw_start_8168cp_2(tp);
5368 case RTL_GIGA_MAC_VER_24:
5369 rtl_hw_start_8168cp_3(tp);
5372 case RTL_GIGA_MAC_VER_25:
5373 case RTL_GIGA_MAC_VER_26:
5374 case RTL_GIGA_MAC_VER_27:
5375 rtl_hw_start_8168d(tp);
5378 case RTL_GIGA_MAC_VER_28:
5379 rtl_hw_start_8168d_4(tp);
5382 case RTL_GIGA_MAC_VER_31:
5383 rtl_hw_start_8168dp(tp);
5386 case RTL_GIGA_MAC_VER_32:
5387 case RTL_GIGA_MAC_VER_33:
5388 rtl_hw_start_8168e_1(tp);
5390 case RTL_GIGA_MAC_VER_34:
5391 rtl_hw_start_8168e_2(tp);
5394 case RTL_GIGA_MAC_VER_35:
5395 case RTL_GIGA_MAC_VER_36:
5396 rtl_hw_start_8168f_1(tp);
5399 case RTL_GIGA_MAC_VER_38:
5400 rtl_hw_start_8411(tp);
5403 case RTL_GIGA_MAC_VER_40:
5404 case RTL_GIGA_MAC_VER_41:
5405 rtl_hw_start_8168g_1(tp);
5407 case RTL_GIGA_MAC_VER_42:
5408 rtl_hw_start_8168g_2(tp);
5411 case RTL_GIGA_MAC_VER_44:
5412 rtl_hw_start_8411_2(tp);
5416 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5417 dev->name, tp->mac_version);
5421 RTL_W8(Cfg9346, Cfg9346_Lock);
5423 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5425 rtl_set_rx_mode(dev);
5427 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
5430 #define R810X_CPCMD_QUIRK_MASK (\
5441 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5443 void __iomem *ioaddr = tp->mmio_addr;
5444 struct pci_dev *pdev = tp->pci_dev;
5445 static const struct ephy_info e_info_8102e_1[] = {
5446 { 0x01, 0, 0x6e65 },
5447 { 0x02, 0, 0x091f },
5448 { 0x03, 0, 0xc2f9 },
5449 { 0x06, 0, 0xafb5 },
5450 { 0x07, 0, 0x0e00 },
5451 { 0x19, 0, 0xec80 },
5452 { 0x01, 0, 0x2e65 },
5457 rtl_csi_access_enable_2(tp);
5459 RTL_W8(DBG_REG, FIX_NAK_1);
5461 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5464 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5465 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5467 cfg1 = RTL_R8(Config1);
5468 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5469 RTL_W8(Config1, cfg1 & ~LEDS0);
5471 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5474 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5476 void __iomem *ioaddr = tp->mmio_addr;
5477 struct pci_dev *pdev = tp->pci_dev;
5479 rtl_csi_access_enable_2(tp);
5481 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5483 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5484 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5487 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5489 rtl_hw_start_8102e_2(tp);
5491 rtl_ephy_write(tp, 0x03, 0xc2f9);
5494 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5496 void __iomem *ioaddr = tp->mmio_addr;
5497 static const struct ephy_info e_info_8105e_1[] = {
5498 { 0x07, 0, 0x4000 },
5499 { 0x19, 0, 0x0200 },
5500 { 0x19, 0, 0x0020 },
5501 { 0x1e, 0, 0x2000 },
5502 { 0x03, 0, 0x0001 },
5503 { 0x19, 0, 0x0100 },
5504 { 0x19, 0, 0x0004 },
5508 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5509 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5511 /* Disable Early Tally Counter */
5512 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5514 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5515 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5517 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5520 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5522 rtl_hw_start_8105e_1(tp);
5523 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5526 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5528 void __iomem *ioaddr = tp->mmio_addr;
5529 static const struct ephy_info e_info_8402[] = {
5530 { 0x19, 0xffff, 0xff64 },
5534 rtl_csi_access_enable_2(tp);
5536 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5537 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5539 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5540 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5542 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
5544 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5546 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5547 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5548 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5549 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5550 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5551 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5552 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
5555 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5557 void __iomem *ioaddr = tp->mmio_addr;
5559 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5560 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5562 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5563 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5564 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5567 static void rtl_hw_start_8101(struct net_device *dev)
5569 struct rtl8169_private *tp = netdev_priv(dev);
5570 void __iomem *ioaddr = tp->mmio_addr;
5571 struct pci_dev *pdev = tp->pci_dev;
5573 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5574 tp->event_slow &= ~RxFIFOOver;
5576 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5577 tp->mac_version == RTL_GIGA_MAC_VER_16)
5578 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5579 PCI_EXP_DEVCTL_NOSNOOP_EN);
5581 RTL_W8(Cfg9346, Cfg9346_Unlock);
5583 RTL_W8(MaxTxPacketSize, TxPacketMax);
5585 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5587 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5588 RTL_W16(CPlusCmd, tp->cp_cmd);
5590 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5592 rtl_set_rx_tx_config_registers(tp);
5594 switch (tp->mac_version) {
5595 case RTL_GIGA_MAC_VER_07:
5596 rtl_hw_start_8102e_1(tp);
5599 case RTL_GIGA_MAC_VER_08:
5600 rtl_hw_start_8102e_3(tp);
5603 case RTL_GIGA_MAC_VER_09:
5604 rtl_hw_start_8102e_2(tp);
5607 case RTL_GIGA_MAC_VER_29:
5608 rtl_hw_start_8105e_1(tp);
5610 case RTL_GIGA_MAC_VER_30:
5611 rtl_hw_start_8105e_2(tp);
5614 case RTL_GIGA_MAC_VER_37:
5615 rtl_hw_start_8402(tp);
5618 case RTL_GIGA_MAC_VER_39:
5619 rtl_hw_start_8106(tp);
5621 case RTL_GIGA_MAC_VER_43:
5622 rtl_hw_start_8168g_2(tp);
5626 RTL_W8(Cfg9346, Cfg9346_Lock);
5628 RTL_W16(IntrMitigate, 0x0000);
5630 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5632 rtl_set_rx_mode(dev);
5636 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5639 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5641 struct rtl8169_private *tp = netdev_priv(dev);
5643 if (new_mtu < ETH_ZLEN ||
5644 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
5647 if (new_mtu > ETH_DATA_LEN)
5648 rtl_hw_jumbo_enable(tp);
5650 rtl_hw_jumbo_disable(tp);
5653 netdev_update_features(dev);
5658 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5660 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5661 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5664 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5665 void **data_buff, struct RxDesc *desc)
5667 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
5672 rtl8169_make_unusable_by_asic(desc);
5675 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5677 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5679 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5682 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5685 desc->addr = cpu_to_le64(mapping);
5687 rtl8169_mark_to_asic(desc, rx_buf_sz);
5690 static inline void *rtl8169_align(void *data)
5692 return (void *)ALIGN((long)data, 16);
5695 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5696 struct RxDesc *desc)
5700 struct device *d = &tp->pci_dev->dev;
5701 struct net_device *dev = tp->dev;
5702 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
5704 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5708 if (rtl8169_align(data) != data) {
5710 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5715 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
5717 if (unlikely(dma_mapping_error(d, mapping))) {
5718 if (net_ratelimit())
5719 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5723 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
5731 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5735 for (i = 0; i < NUM_RX_DESC; i++) {
5736 if (tp->Rx_databuff[i]) {
5737 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5738 tp->RxDescArray + i);
5743 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5745 desc->opts1 |= cpu_to_le32(RingEnd);
5748 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5752 for (i = 0; i < NUM_RX_DESC; i++) {
5755 if (tp->Rx_databuff[i])
5758 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5760 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5763 tp->Rx_databuff[i] = data;
5766 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5770 rtl8169_rx_clear(tp);
5774 static int rtl8169_init_ring(struct net_device *dev)
5776 struct rtl8169_private *tp = netdev_priv(dev);
5778 rtl8169_init_ring_indexes(tp);
5780 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
5781 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
5783 return rtl8169_rx_fill(tp);
5786 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5787 struct TxDesc *desc)
5789 unsigned int len = tx_skb->len;
5791 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5799 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5804 for (i = 0; i < n; i++) {
5805 unsigned int entry = (start + i) % NUM_TX_DESC;
5806 struct ring_info *tx_skb = tp->tx_skb + entry;
5807 unsigned int len = tx_skb->len;
5810 struct sk_buff *skb = tx_skb->skb;
5812 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5813 tp->TxDescArray + entry);
5815 tp->dev->stats.tx_dropped++;
5816 dev_kfree_skb_any(skb);
5823 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5825 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5826 tp->cur_tx = tp->dirty_tx = 0;
5829 static void rtl_reset_work(struct rtl8169_private *tp)
5831 struct net_device *dev = tp->dev;
5834 napi_disable(&tp->napi);
5835 netif_stop_queue(dev);
5836 synchronize_sched();
5838 rtl8169_hw_reset(tp);
5840 for (i = 0; i < NUM_RX_DESC; i++)
5841 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5843 rtl8169_tx_clear(tp);
5844 rtl8169_init_ring_indexes(tp);
5846 napi_enable(&tp->napi);
5848 netif_wake_queue(dev);
5849 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
5852 static void rtl8169_tx_timeout(struct net_device *dev)
5854 struct rtl8169_private *tp = netdev_priv(dev);
5856 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5859 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5862 struct skb_shared_info *info = skb_shinfo(skb);
5863 unsigned int cur_frag, entry;
5864 struct TxDesc * uninitialized_var(txd);
5865 struct device *d = &tp->pci_dev->dev;
5868 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5869 const skb_frag_t *frag = info->frags + cur_frag;
5874 entry = (entry + 1) % NUM_TX_DESC;
5876 txd = tp->TxDescArray + entry;
5877 len = skb_frag_size(frag);
5878 addr = skb_frag_address(frag);
5879 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5880 if (unlikely(dma_mapping_error(d, mapping))) {
5881 if (net_ratelimit())
5882 netif_err(tp, drv, tp->dev,
5883 "Failed to map TX fragments DMA!\n");
5887 /* Anti gcc 2.95.3 bugware (sic) */
5888 status = opts[0] | len |
5889 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5891 txd->opts1 = cpu_to_le32(status);
5892 txd->opts2 = cpu_to_le32(opts[1]);
5893 txd->addr = cpu_to_le64(mapping);
5895 tp->tx_skb[entry].len = len;
5899 tp->tx_skb[entry].skb = skb;
5900 txd->opts1 |= cpu_to_le32(LastFrag);
5906 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5910 static bool rtl_skb_pad(struct sk_buff *skb)
5912 if (skb_padto(skb, ETH_ZLEN))
5914 skb_put(skb, ETH_ZLEN - skb->len);
5918 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5920 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5923 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
5924 struct sk_buff *skb, u32 *opts)
5926 u32 mss = skb_shinfo(skb)->gso_size;
5930 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5931 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5932 const struct iphdr *ip = ip_hdr(skb);
5934 if (ip->protocol == IPPROTO_TCP)
5935 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5936 else if (ip->protocol == IPPROTO_UDP)
5937 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5945 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5946 struct sk_buff *skb, u32 *opts)
5948 u32 transport_offset = (u32)skb_transport_offset(skb);
5949 u32 mss = skb_shinfo(skb)->gso_size;
5952 opts[0] |= TD1_GTSENV4;
5953 opts[0] |= transport_offset << GTTCPHO_SHIFT;
5954 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
5955 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5956 const struct iphdr *ip = ip_hdr(skb);
5958 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5959 return skb_checksum_help(skb) == 0 && rtl_skb_pad(skb);
5961 if (ip->protocol == IPPROTO_TCP)
5962 opts[1] |= TD1_IP_CS | TD1_TCP_CS;
5963 else if (ip->protocol == IPPROTO_UDP)
5964 opts[1] |= TD1_IP_CS | TD1_UDP_CS;
5968 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5969 return rtl_skb_pad(skb);
5975 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5976 struct net_device *dev)
5978 struct rtl8169_private *tp = netdev_priv(dev);
5979 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5980 struct TxDesc *txd = tp->TxDescArray + entry;
5981 void __iomem *ioaddr = tp->mmio_addr;
5982 struct device *d = &tp->pci_dev->dev;
5988 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
5989 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5993 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5996 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
5999 if (!tp->tso_csum(tp, skb, opts))
6000 goto err_update_stats;
6002 len = skb_headlen(skb);
6003 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
6004 if (unlikely(dma_mapping_error(d, mapping))) {
6005 if (net_ratelimit())
6006 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
6010 tp->tx_skb[entry].len = len;
6011 txd->addr = cpu_to_le64(mapping);
6013 frags = rtl8169_xmit_frags(tp, skb, opts);
6017 opts[0] |= FirstFrag;
6019 opts[0] |= FirstFrag | LastFrag;
6020 tp->tx_skb[entry].skb = skb;
6023 txd->opts2 = cpu_to_le32(opts[1]);
6025 skb_tx_timestamp(skb);
6029 /* Anti gcc 2.95.3 bugware (sic) */
6030 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
6031 txd->opts1 = cpu_to_le32(status);
6033 tp->cur_tx += frags + 1;
6037 RTL_W8(TxPoll, NPQ);
6041 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6042 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6043 * not miss a ring update when it notices a stopped queue.
6046 netif_stop_queue(dev);
6047 /* Sync with rtl_tx:
6048 * - publish queue status and cur_tx ring index (write barrier)
6049 * - refresh dirty_tx ring index (read barrier).
6050 * May the current thread have a pessimistic view of the ring
6051 * status and forget to wake up queue, a racing rtl_tx thread
6055 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
6056 netif_wake_queue(dev);
6059 return NETDEV_TX_OK;
6062 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
6064 dev_kfree_skb_any(skb);
6066 dev->stats.tx_dropped++;
6067 return NETDEV_TX_OK;
6070 netif_stop_queue(dev);
6071 dev->stats.tx_dropped++;
6072 return NETDEV_TX_BUSY;
6075 static void rtl8169_pcierr_interrupt(struct net_device *dev)
6077 struct rtl8169_private *tp = netdev_priv(dev);
6078 struct pci_dev *pdev = tp->pci_dev;
6079 u16 pci_status, pci_cmd;
6081 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6082 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6084 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6085 pci_cmd, pci_status);
6088 * The recovery sequence below admits a very elaborated explanation:
6089 * - it seems to work;
6090 * - I did not see what else could be done;
6091 * - it makes iop3xx happy.
6093 * Feel free to adjust to your needs.
6095 if (pdev->broken_parity_status)
6096 pci_cmd &= ~PCI_COMMAND_PARITY;
6098 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6100 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
6102 pci_write_config_word(pdev, PCI_STATUS,
6103 pci_status & (PCI_STATUS_DETECTED_PARITY |
6104 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6105 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6107 /* The infamous DAC f*ckup only happens at boot time */
6108 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
6109 void __iomem *ioaddr = tp->mmio_addr;
6111 netif_info(tp, intr, dev, "disabling PCI DAC\n");
6112 tp->cp_cmd &= ~PCIDAC;
6113 RTL_W16(CPlusCmd, tp->cp_cmd);
6114 dev->features &= ~NETIF_F_HIGHDMA;
6117 rtl8169_hw_reset(tp);
6119 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6122 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
6124 unsigned int dirty_tx, tx_left;
6126 dirty_tx = tp->dirty_tx;
6128 tx_left = tp->cur_tx - dirty_tx;
6130 while (tx_left > 0) {
6131 unsigned int entry = dirty_tx % NUM_TX_DESC;
6132 struct ring_info *tx_skb = tp->tx_skb + entry;
6136 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6137 if (status & DescOwn)
6140 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
6141 tp->TxDescArray + entry);
6142 if (status & LastFrag) {
6143 u64_stats_update_begin(&tp->tx_stats.syncp);
6144 tp->tx_stats.packets++;
6145 tp->tx_stats.bytes += tx_skb->skb->len;
6146 u64_stats_update_end(&tp->tx_stats.syncp);
6147 dev_kfree_skb_any(tx_skb->skb);
6154 if (tp->dirty_tx != dirty_tx) {
6155 tp->dirty_tx = dirty_tx;
6156 /* Sync with rtl8169_start_xmit:
6157 * - publish dirty_tx ring index (write barrier)
6158 * - refresh cur_tx ring index and queue status (read barrier)
6159 * May the current thread miss the stopped queue condition,
6160 * a racing xmit thread can only have a right view of the
6164 if (netif_queue_stopped(dev) &&
6165 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6166 netif_wake_queue(dev);
6169 * 8168 hack: TxPoll requests are lost when the Tx packets are
6170 * too close. Let's kick an extra TxPoll request when a burst
6171 * of start_xmit activity is detected (if it is not detected,
6172 * it is slow enough). -- FR
6174 if (tp->cur_tx != dirty_tx) {
6175 void __iomem *ioaddr = tp->mmio_addr;
6177 RTL_W8(TxPoll, NPQ);
6182 static inline int rtl8169_fragmented_frame(u32 status)
6184 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6187 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
6189 u32 status = opts1 & RxProtoMask;
6191 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
6192 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
6193 skb->ip_summed = CHECKSUM_UNNECESSARY;
6195 skb_checksum_none_assert(skb);
6198 static struct sk_buff *rtl8169_try_rx_copy(void *data,
6199 struct rtl8169_private *tp,
6203 struct sk_buff *skb;
6204 struct device *d = &tp->pci_dev->dev;
6206 data = rtl8169_align(data);
6207 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6209 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6211 memcpy(skb->data, data, pkt_size);
6212 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6217 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
6219 unsigned int cur_rx, rx_left;
6222 cur_rx = tp->cur_rx;
6224 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
6225 unsigned int entry = cur_rx % NUM_RX_DESC;
6226 struct RxDesc *desc = tp->RxDescArray + entry;
6230 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
6232 if (status & DescOwn)
6234 if (unlikely(status & RxRES)) {
6235 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6237 dev->stats.rx_errors++;
6238 if (status & (RxRWT | RxRUNT))
6239 dev->stats.rx_length_errors++;
6241 dev->stats.rx_crc_errors++;
6242 if (status & RxFOVF) {
6243 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6244 dev->stats.rx_fifo_errors++;
6246 if ((status & (RxRUNT | RxCRC)) &&
6247 !(status & (RxRWT | RxFOVF)) &&
6248 (dev->features & NETIF_F_RXALL))
6251 struct sk_buff *skb;
6256 addr = le64_to_cpu(desc->addr);
6257 if (likely(!(dev->features & NETIF_F_RXFCS)))
6258 pkt_size = (status & 0x00003fff) - 4;
6260 pkt_size = status & 0x00003fff;
6263 * The driver does not support incoming fragmented
6264 * frames. They are seen as a symptom of over-mtu
6267 if (unlikely(rtl8169_fragmented_frame(status))) {
6268 dev->stats.rx_dropped++;
6269 dev->stats.rx_length_errors++;
6270 goto release_descriptor;
6273 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6274 tp, pkt_size, addr);
6276 dev->stats.rx_dropped++;
6277 goto release_descriptor;
6280 rtl8169_rx_csum(skb, status);
6281 skb_put(skb, pkt_size);
6282 skb->protocol = eth_type_trans(skb, dev);
6284 rtl8169_rx_vlan_tag(desc, skb);
6286 napi_gro_receive(&tp->napi, skb);
6288 u64_stats_update_begin(&tp->rx_stats.syncp);
6289 tp->rx_stats.packets++;
6290 tp->rx_stats.bytes += pkt_size;
6291 u64_stats_update_end(&tp->rx_stats.syncp);
6296 rtl8169_mark_to_asic(desc, rx_buf_sz);
6299 count = cur_rx - tp->cur_rx;
6300 tp->cur_rx = cur_rx;
6305 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6307 struct net_device *dev = dev_instance;
6308 struct rtl8169_private *tp = netdev_priv(dev);
6312 status = rtl_get_events(tp);
6313 if (status && status != 0xffff) {
6314 status &= RTL_EVENT_NAPI | tp->event_slow;
6318 rtl_irq_disable(tp);
6319 napi_schedule(&tp->napi);
6322 return IRQ_RETVAL(handled);
6326 * Workqueue context.
6328 static void rtl_slow_event_work(struct rtl8169_private *tp)
6330 struct net_device *dev = tp->dev;
6333 status = rtl_get_events(tp) & tp->event_slow;
6334 rtl_ack_events(tp, status);
6336 if (unlikely(status & RxFIFOOver)) {
6337 switch (tp->mac_version) {
6338 /* Work around for rx fifo overflow */
6339 case RTL_GIGA_MAC_VER_11:
6340 netif_stop_queue(dev);
6341 /* XXX - Hack alert. See rtl_task(). */
6342 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6348 if (unlikely(status & SYSErr))
6349 rtl8169_pcierr_interrupt(dev);
6351 if (status & LinkChg)
6352 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6354 rtl_irq_enable_all(tp);
6357 static void rtl_task(struct work_struct *work)
6359 static const struct {
6361 void (*action)(struct rtl8169_private *);
6363 /* XXX - keep rtl_slow_event_work() as first element. */
6364 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6365 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6366 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6368 struct rtl8169_private *tp =
6369 container_of(work, struct rtl8169_private, wk.work);
6370 struct net_device *dev = tp->dev;
6375 if (!netif_running(dev) ||
6376 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6379 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6382 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6384 rtl_work[i].action(tp);
6388 rtl_unlock_work(tp);
6391 static int rtl8169_poll(struct napi_struct *napi, int budget)
6393 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6394 struct net_device *dev = tp->dev;
6395 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6399 status = rtl_get_events(tp);
6400 rtl_ack_events(tp, status & ~tp->event_slow);
6402 if (status & RTL_EVENT_NAPI_RX)
6403 work_done = rtl_rx(dev, tp, (u32) budget);
6405 if (status & RTL_EVENT_NAPI_TX)
6408 if (status & tp->event_slow) {
6409 enable_mask &= ~tp->event_slow;
6411 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6414 if (work_done < budget) {
6415 napi_complete(napi);
6417 rtl_irq_enable(tp, enable_mask);
6424 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6426 struct rtl8169_private *tp = netdev_priv(dev);
6428 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6431 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6432 RTL_W32(RxMissed, 0);
6435 static void rtl8169_down(struct net_device *dev)
6437 struct rtl8169_private *tp = netdev_priv(dev);
6438 void __iomem *ioaddr = tp->mmio_addr;
6440 del_timer_sync(&tp->timer);
6442 napi_disable(&tp->napi);
6443 netif_stop_queue(dev);
6445 rtl8169_hw_reset(tp);
6447 * At this point device interrupts can not be enabled in any function,
6448 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6449 * and napi is disabled (rtl8169_poll).
6451 rtl8169_rx_missed(dev, ioaddr);
6453 /* Give a racing hard_start_xmit a few cycles to complete. */
6454 synchronize_sched();
6456 rtl8169_tx_clear(tp);
6458 rtl8169_rx_clear(tp);
6460 rtl_pll_power_down(tp);
6463 static int rtl8169_close(struct net_device *dev)
6465 struct rtl8169_private *tp = netdev_priv(dev);
6466 struct pci_dev *pdev = tp->pci_dev;
6468 pm_runtime_get_sync(&pdev->dev);
6470 /* Update counters before going down */
6471 rtl8169_update_counters(dev);
6474 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6477 rtl_unlock_work(tp);
6479 cancel_work_sync(&tp->wk.work);
6481 free_irq(pdev->irq, dev);
6483 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6485 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6487 tp->TxDescArray = NULL;
6488 tp->RxDescArray = NULL;
6490 pm_runtime_put_sync(&pdev->dev);
6495 #ifdef CONFIG_NET_POLL_CONTROLLER
6496 static void rtl8169_netpoll(struct net_device *dev)
6498 struct rtl8169_private *tp = netdev_priv(dev);
6500 rtl8169_interrupt(tp->pci_dev->irq, dev);
6504 static int rtl_open(struct net_device *dev)
6506 struct rtl8169_private *tp = netdev_priv(dev);
6507 void __iomem *ioaddr = tp->mmio_addr;
6508 struct pci_dev *pdev = tp->pci_dev;
6509 int retval = -ENOMEM;
6511 pm_runtime_get_sync(&pdev->dev);
6514 * Rx and Tx descriptors needs 256 bytes alignment.
6515 * dma_alloc_coherent provides more.
6517 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6518 &tp->TxPhyAddr, GFP_KERNEL);
6519 if (!tp->TxDescArray)
6520 goto err_pm_runtime_put;
6522 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6523 &tp->RxPhyAddr, GFP_KERNEL);
6524 if (!tp->RxDescArray)
6527 retval = rtl8169_init_ring(dev);
6531 INIT_WORK(&tp->wk.work, rtl_task);
6535 rtl_request_firmware(tp);
6537 retval = request_irq(pdev->irq, rtl8169_interrupt,
6538 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6541 goto err_release_fw_2;
6545 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6547 napi_enable(&tp->napi);
6549 rtl8169_init_phy(dev, tp);
6551 __rtl8169_set_features(dev, dev->features);
6553 rtl_pll_power_up(tp);
6557 netif_start_queue(dev);
6559 rtl_unlock_work(tp);
6561 tp->saved_wolopts = 0;
6562 pm_runtime_put_noidle(&pdev->dev);
6564 rtl8169_check_link_status(dev, tp, ioaddr);
6569 rtl_release_firmware(tp);
6570 rtl8169_rx_clear(tp);
6572 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6574 tp->RxDescArray = NULL;
6576 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6578 tp->TxDescArray = NULL;
6580 pm_runtime_put_noidle(&pdev->dev);
6584 static struct rtnl_link_stats64 *
6585 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6587 struct rtl8169_private *tp = netdev_priv(dev);
6588 void __iomem *ioaddr = tp->mmio_addr;
6591 if (netif_running(dev))
6592 rtl8169_rx_missed(dev, ioaddr);
6595 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
6596 stats->rx_packets = tp->rx_stats.packets;
6597 stats->rx_bytes = tp->rx_stats.bytes;
6598 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
6602 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
6603 stats->tx_packets = tp->tx_stats.packets;
6604 stats->tx_bytes = tp->tx_stats.bytes;
6605 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
6607 stats->rx_dropped = dev->stats.rx_dropped;
6608 stats->tx_dropped = dev->stats.tx_dropped;
6609 stats->rx_length_errors = dev->stats.rx_length_errors;
6610 stats->rx_errors = dev->stats.rx_errors;
6611 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6612 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6613 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6618 static void rtl8169_net_suspend(struct net_device *dev)
6620 struct rtl8169_private *tp = netdev_priv(dev);
6622 if (!netif_running(dev))
6625 netif_device_detach(dev);
6626 netif_stop_queue(dev);
6629 napi_disable(&tp->napi);
6630 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6631 rtl_unlock_work(tp);
6633 rtl_pll_power_down(tp);
6638 static int rtl8169_suspend(struct device *device)
6640 struct pci_dev *pdev = to_pci_dev(device);
6641 struct net_device *dev = pci_get_drvdata(pdev);
6643 rtl8169_net_suspend(dev);
6648 static void __rtl8169_resume(struct net_device *dev)
6650 struct rtl8169_private *tp = netdev_priv(dev);
6652 netif_device_attach(dev);
6654 rtl_pll_power_up(tp);
6657 napi_enable(&tp->napi);
6658 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6659 rtl_unlock_work(tp);
6661 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6664 static int rtl8169_resume(struct device *device)
6666 struct pci_dev *pdev = to_pci_dev(device);
6667 struct net_device *dev = pci_get_drvdata(pdev);
6668 struct rtl8169_private *tp = netdev_priv(dev);
6670 rtl8169_init_phy(dev, tp);
6672 if (netif_running(dev))
6673 __rtl8169_resume(dev);
6678 static int rtl8169_runtime_suspend(struct device *device)
6680 struct pci_dev *pdev = to_pci_dev(device);
6681 struct net_device *dev = pci_get_drvdata(pdev);
6682 struct rtl8169_private *tp = netdev_priv(dev);
6684 if (!tp->TxDescArray)
6688 tp->saved_wolopts = __rtl8169_get_wol(tp);
6689 __rtl8169_set_wol(tp, WAKE_ANY);
6690 rtl_unlock_work(tp);
6692 rtl8169_net_suspend(dev);
6697 static int rtl8169_runtime_resume(struct device *device)
6699 struct pci_dev *pdev = to_pci_dev(device);
6700 struct net_device *dev = pci_get_drvdata(pdev);
6701 struct rtl8169_private *tp = netdev_priv(dev);
6703 if (!tp->TxDescArray)
6707 __rtl8169_set_wol(tp, tp->saved_wolopts);
6708 tp->saved_wolopts = 0;
6709 rtl_unlock_work(tp);
6711 rtl8169_init_phy(dev, tp);
6713 __rtl8169_resume(dev);
6718 static int rtl8169_runtime_idle(struct device *device)
6720 struct pci_dev *pdev = to_pci_dev(device);
6721 struct net_device *dev = pci_get_drvdata(pdev);
6722 struct rtl8169_private *tp = netdev_priv(dev);
6724 return tp->TxDescArray ? -EBUSY : 0;
6727 static const struct dev_pm_ops rtl8169_pm_ops = {
6728 .suspend = rtl8169_suspend,
6729 .resume = rtl8169_resume,
6730 .freeze = rtl8169_suspend,
6731 .thaw = rtl8169_resume,
6732 .poweroff = rtl8169_suspend,
6733 .restore = rtl8169_resume,
6734 .runtime_suspend = rtl8169_runtime_suspend,
6735 .runtime_resume = rtl8169_runtime_resume,
6736 .runtime_idle = rtl8169_runtime_idle,
6739 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
6741 #else /* !CONFIG_PM */
6743 #define RTL8169_PM_OPS NULL
6745 #endif /* !CONFIG_PM */
6747 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6749 void __iomem *ioaddr = tp->mmio_addr;
6751 /* WoL fails with 8168b when the receiver is disabled. */
6752 switch (tp->mac_version) {
6753 case RTL_GIGA_MAC_VER_11:
6754 case RTL_GIGA_MAC_VER_12:
6755 case RTL_GIGA_MAC_VER_17:
6756 pci_clear_master(tp->pci_dev);
6758 RTL_W8(ChipCmd, CmdRxEnb);
6767 static void rtl_shutdown(struct pci_dev *pdev)
6769 struct net_device *dev = pci_get_drvdata(pdev);
6770 struct rtl8169_private *tp = netdev_priv(dev);
6771 struct device *d = &pdev->dev;
6773 pm_runtime_get_sync(d);
6775 rtl8169_net_suspend(dev);
6777 /* Restore original MAC address */
6778 rtl_rar_set(tp, dev->perm_addr);
6780 rtl8169_hw_reset(tp);
6782 if (system_state == SYSTEM_POWER_OFF) {
6783 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6784 rtl_wol_suspend_quirk(tp);
6785 rtl_wol_shutdown_quirk(tp);
6788 pci_wake_from_d3(pdev, true);
6789 pci_set_power_state(pdev, PCI_D3hot);
6792 pm_runtime_put_noidle(d);
6795 static void rtl_remove_one(struct pci_dev *pdev)
6797 struct net_device *dev = pci_get_drvdata(pdev);
6798 struct rtl8169_private *tp = netdev_priv(dev);
6800 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6801 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6802 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6803 rtl8168_driver_stop(tp);
6806 netif_napi_del(&tp->napi);
6808 unregister_netdev(dev);
6810 rtl_release_firmware(tp);
6812 if (pci_dev_run_wake(pdev))
6813 pm_runtime_get_noresume(&pdev->dev);
6815 /* restore original MAC address */
6816 rtl_rar_set(tp, dev->perm_addr);
6818 rtl_disable_msi(pdev, tp);
6819 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6822 static const struct net_device_ops rtl_netdev_ops = {
6823 .ndo_open = rtl_open,
6824 .ndo_stop = rtl8169_close,
6825 .ndo_get_stats64 = rtl8169_get_stats64,
6826 .ndo_start_xmit = rtl8169_start_xmit,
6827 .ndo_tx_timeout = rtl8169_tx_timeout,
6828 .ndo_validate_addr = eth_validate_addr,
6829 .ndo_change_mtu = rtl8169_change_mtu,
6830 .ndo_fix_features = rtl8169_fix_features,
6831 .ndo_set_features = rtl8169_set_features,
6832 .ndo_set_mac_address = rtl_set_mac_address,
6833 .ndo_do_ioctl = rtl8169_ioctl,
6834 .ndo_set_rx_mode = rtl_set_rx_mode,
6835 #ifdef CONFIG_NET_POLL_CONTROLLER
6836 .ndo_poll_controller = rtl8169_netpoll,
6841 static const struct rtl_cfg_info {
6842 void (*hw_start)(struct net_device *);
6843 unsigned int region;
6848 } rtl_cfg_infos [] = {
6850 .hw_start = rtl_hw_start_8169,
6853 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6854 .features = RTL_FEATURE_GMII,
6855 .default_ver = RTL_GIGA_MAC_VER_01,
6858 .hw_start = rtl_hw_start_8168,
6861 .event_slow = SYSErr | LinkChg | RxOverflow,
6862 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6863 .default_ver = RTL_GIGA_MAC_VER_11,
6866 .hw_start = rtl_hw_start_8101,
6869 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6871 .features = RTL_FEATURE_MSI,
6872 .default_ver = RTL_GIGA_MAC_VER_13,
6876 /* Cfg9346_Unlock assumed. */
6877 static unsigned rtl_try_msi(struct rtl8169_private *tp,
6878 const struct rtl_cfg_info *cfg)
6880 void __iomem *ioaddr = tp->mmio_addr;
6884 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6885 if (cfg->features & RTL_FEATURE_MSI) {
6886 if (pci_enable_msi(tp->pci_dev)) {
6887 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6890 msi = RTL_FEATURE_MSI;
6893 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6894 RTL_W8(Config2, cfg2);
6898 DECLARE_RTL_COND(rtl_link_list_ready_cond)
6900 void __iomem *ioaddr = tp->mmio_addr;
6902 return RTL_R8(MCU) & LINK_LIST_RDY;
6905 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6907 void __iomem *ioaddr = tp->mmio_addr;
6909 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
6912 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
6914 void __iomem *ioaddr = tp->mmio_addr;
6917 tp->ocp_base = OCP_STD_PHY_BASE;
6919 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
6921 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6924 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6927 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6929 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6931 data = r8168_mac_ocp_read(tp, 0xe8de);
6933 r8168_mac_ocp_write(tp, 0xe8de, data);
6935 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6938 data = r8168_mac_ocp_read(tp, 0xe8de);
6940 r8168_mac_ocp_write(tp, 0xe8de, data);
6942 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6946 static void rtl_hw_initialize(struct rtl8169_private *tp)
6948 switch (tp->mac_version) {
6949 case RTL_GIGA_MAC_VER_40:
6950 case RTL_GIGA_MAC_VER_41:
6951 case RTL_GIGA_MAC_VER_42:
6952 case RTL_GIGA_MAC_VER_43:
6953 case RTL_GIGA_MAC_VER_44:
6954 rtl_hw_init_8168g(tp);
6963 rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6965 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6966 const unsigned int region = cfg->region;
6967 struct rtl8169_private *tp;
6968 struct mii_if_info *mii;
6969 struct net_device *dev;
6970 void __iomem *ioaddr;
6974 if (netif_msg_drv(&debug)) {
6975 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6976 MODULENAME, RTL8169_VERSION);
6979 dev = alloc_etherdev(sizeof (*tp));
6985 SET_NETDEV_DEV(dev, &pdev->dev);
6986 dev->netdev_ops = &rtl_netdev_ops;
6987 tp = netdev_priv(dev);
6990 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6994 mii->mdio_read = rtl_mdio_read;
6995 mii->mdio_write = rtl_mdio_write;
6996 mii->phy_id_mask = 0x1f;
6997 mii->reg_num_mask = 0x1f;
6998 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
7000 /* disable ASPM completely as that cause random device stop working
7001 * problems as well as full system hangs for some PCIe devices users */
7002 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7003 PCIE_LINK_STATE_CLKPM);
7005 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7006 rc = pci_enable_device(pdev);
7008 netif_err(tp, probe, dev, "enable failure\n");
7009 goto err_out_free_dev_1;
7012 if (pci_set_mwi(pdev) < 0)
7013 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
7015 /* make sure PCI base addr 1 is MMIO */
7016 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
7017 netif_err(tp, probe, dev,
7018 "region #%d not an MMIO resource, aborting\n",
7024 /* check for weird/broken PCI region reporting */
7025 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7026 netif_err(tp, probe, dev,
7027 "Invalid PCI region size(s), aborting\n");
7032 rc = pci_request_regions(pdev, MODULENAME);
7034 netif_err(tp, probe, dev, "could not request regions\n");
7038 tp->cp_cmd = RxChkSum;
7040 if ((sizeof(dma_addr_t) > 4) &&
7041 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
7042 tp->cp_cmd |= PCIDAC;
7043 dev->features |= NETIF_F_HIGHDMA;
7045 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7047 netif_err(tp, probe, dev, "DMA configuration failed\n");
7048 goto err_out_free_res_3;
7052 /* ioremap MMIO region */
7053 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
7055 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
7057 goto err_out_free_res_3;
7059 tp->mmio_addr = ioaddr;
7061 if (!pci_is_pcie(pdev))
7062 netif_info(tp, probe, dev, "not PCI Express\n");
7064 /* Identify chip attached to board */
7065 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
7069 rtl_irq_disable(tp);
7071 rtl_hw_initialize(tp);
7075 rtl_ack_events(tp, 0xffff);
7077 pci_set_master(pdev);
7080 * Pretend we are using VLANs; This bypasses a nasty bug where
7081 * Interrupts stop flowing on high load on 8110SCd controllers.
7083 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7084 tp->cp_cmd |= RxVlan;
7086 rtl_init_mdio_ops(tp);
7087 rtl_init_pll_power_ops(tp);
7088 rtl_init_jumbo_ops(tp);
7089 rtl_init_csi_ops(tp);
7091 rtl8169_print_mac_version(tp);
7093 chipset = tp->mac_version;
7094 tp->txd_version = rtl_chip_infos[chipset].txd_version;
7096 RTL_W8(Cfg9346, Cfg9346_Unlock);
7097 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
7098 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
7099 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
7100 tp->features |= RTL_FEATURE_WOL;
7101 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
7102 tp->features |= RTL_FEATURE_WOL;
7103 tp->features |= rtl_try_msi(tp, cfg);
7104 RTL_W8(Cfg9346, Cfg9346_Lock);
7106 if (rtl_tbi_enabled(tp)) {
7107 tp->set_speed = rtl8169_set_speed_tbi;
7108 tp->get_settings = rtl8169_gset_tbi;
7109 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7110 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7111 tp->link_ok = rtl8169_tbi_link_ok;
7112 tp->do_ioctl = rtl_tbi_ioctl;
7114 tp->set_speed = rtl8169_set_speed_xmii;
7115 tp->get_settings = rtl8169_gset_xmii;
7116 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7117 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7118 tp->link_ok = rtl8169_xmii_link_ok;
7119 tp->do_ioctl = rtl_xmii_ioctl;
7122 mutex_init(&tp->wk.mutex);
7123 u64_stats_init(&tp->rx_stats.syncp);
7124 u64_stats_init(&tp->tx_stats.syncp);
7126 /* Get MAC address */
7127 for (i = 0; i < ETH_ALEN; i++)
7128 dev->dev_addr[i] = RTL_R8(MAC0 + i);
7130 dev->ethtool_ops = &rtl8169_ethtool_ops;
7131 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
7133 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
7135 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7136 * properly for all devices */
7137 dev->features |= NETIF_F_RXCSUM |
7138 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
7140 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7141 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7142 NETIF_F_HW_VLAN_CTAG_RX;
7143 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7146 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7147 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
7148 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
7150 if (tp->txd_version == RTL_TD_0)
7151 tp->tso_csum = rtl8169_tso_csum_v1;
7152 else if (tp->txd_version == RTL_TD_1)
7153 tp->tso_csum = rtl8169_tso_csum_v2;
7157 dev->hw_features |= NETIF_F_RXALL;
7158 dev->hw_features |= NETIF_F_RXFCS;
7160 tp->hw_start = cfg->hw_start;
7161 tp->event_slow = cfg->event_slow;
7163 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
7164 ~(RxBOVF | RxFOVF) : ~0;
7166 init_timer(&tp->timer);
7167 tp->timer.data = (unsigned long) dev;
7168 tp->timer.function = rtl8169_phy_timer;
7170 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7172 rc = register_netdev(dev);
7176 pci_set_drvdata(pdev, dev);
7178 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
7179 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
7180 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
7181 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7182 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7183 "tx checksumming: %s]\n",
7184 rtl_chip_infos[chipset].jumbo_max,
7185 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
7188 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7189 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
7190 tp->mac_version == RTL_GIGA_MAC_VER_31) {
7191 rtl8168_driver_start(tp);
7194 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
7196 if (pci_dev_run_wake(pdev))
7197 pm_runtime_put_noidle(&pdev->dev);
7199 netif_carrier_off(dev);
7205 netif_napi_del(&tp->napi);
7206 rtl_disable_msi(pdev, tp);
7209 pci_release_regions(pdev);
7211 pci_clear_mwi(pdev);
7212 pci_disable_device(pdev);
7218 static struct pci_driver rtl8169_pci_driver = {
7220 .id_table = rtl8169_pci_tbl,
7221 .probe = rtl_init_one,
7222 .remove = rtl_remove_one,
7223 .shutdown = rtl_shutdown,
7224 .driver.pm = RTL8169_PM_OPS,
7227 module_pci_driver(rtl8169_pci_driver);