2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/if_vlan.h>
20 #include <linux/crc32.h>
23 #include <linux/tcp.h>
24 #include <linux/interrupt.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/firmware.h>
28 #include <linux/pci-aspm.h>
29 #include <linux/prefetch.h>
30 #include <linux/ipv6.h>
31 #include <net/ip6_checksum.h>
36 #define RTL8169_VERSION "2.3LK-NAPI"
37 #define MODULENAME "r8169"
38 #define PFX MODULENAME ": "
40 #define FIRMWARE_8168D_1 "/*(DEBLOBBED)*/"
41 #define FIRMWARE_8168D_2 "/*(DEBLOBBED)*/"
42 #define FIRMWARE_8168E_1 "/*(DEBLOBBED)*/"
43 #define FIRMWARE_8168E_2 "/*(DEBLOBBED)*/"
44 #define FIRMWARE_8168E_3 "/*(DEBLOBBED)*/"
45 #define FIRMWARE_8168F_1 "/*(DEBLOBBED)*/"
46 #define FIRMWARE_8168F_2 "/*(DEBLOBBED)*/"
47 #define FIRMWARE_8105E_1 "/*(DEBLOBBED)*/"
48 #define FIRMWARE_8402_1 "/*(DEBLOBBED)*/"
49 #define FIRMWARE_8411_1 "/*(DEBLOBBED)*/"
50 #define FIRMWARE_8411_2 "/*(DEBLOBBED)*/"
51 #define FIRMWARE_8106E_1 "/*(DEBLOBBED)*/"
52 #define FIRMWARE_8106E_2 "/*(DEBLOBBED)*/"
53 #define FIRMWARE_8168G_2 "/*(DEBLOBBED)*/"
54 #define FIRMWARE_8168G_3 "/*(DEBLOBBED)*/"
55 #define FIRMWARE_8168H_1 "/*(DEBLOBBED)*/"
56 #define FIRMWARE_8168H_2 "/*(DEBLOBBED)*/"
57 #define FIRMWARE_8107E_1 "/*(DEBLOBBED)*/"
58 #define FIRMWARE_8107E_2 "/*(DEBLOBBED)*/"
61 #define assert(expr) \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
64 #expr,__FILE__,__func__,__LINE__); \
66 #define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
69 #define assert(expr) do {} while (0)
70 #define dprintk(fmt, args...) do {} while (0)
71 #endif /* RTL8169_DEBUG */
73 #define R8169_MSG_DEFAULT \
74 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
76 #define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
79 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
83 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
85 static const int multicast_filter_limit = 32;
87 #define MAX_READ_REQUEST_SHIFT 12
88 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
89 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
91 #define R8169_REGS_SIZE 256
92 #define R8169_NAPI_WEIGHT 64
93 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
94 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
95 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
98 #define RTL8169_TX_TIMEOUT (6*HZ)
99 #define RTL8169_PHY_TIMEOUT (10*HZ)
101 /* write/read MMIO register */
102 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105 #define RTL_R8(reg) readb (ioaddr + (reg))
106 #define RTL_R16(reg) readw (ioaddr + (reg))
107 #define RTL_R32(reg) readl (ioaddr + (reg))
110 RTL_GIGA_MAC_VER_01 = 0,
161 RTL_GIGA_MAC_NONE = 0xff,
164 enum rtl_tx_desc_version {
169 #define JUMBO_1K ETH_DATA_LEN
170 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
175 #define _R(NAME,TD,FW,SZ,B) { \
183 static const struct {
185 enum rtl_tx_desc_version txd_version;
189 } rtl_chip_infos[] = {
191 [RTL_GIGA_MAC_VER_01] =
192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
193 [RTL_GIGA_MAC_VER_02] =
194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
195 [RTL_GIGA_MAC_VER_03] =
196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
197 [RTL_GIGA_MAC_VER_04] =
198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
199 [RTL_GIGA_MAC_VER_05] =
200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
201 [RTL_GIGA_MAC_VER_06] =
202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
204 [RTL_GIGA_MAC_VER_07] =
205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
206 [RTL_GIGA_MAC_VER_08] =
207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
208 [RTL_GIGA_MAC_VER_09] =
209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
210 [RTL_GIGA_MAC_VER_10] =
211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
212 [RTL_GIGA_MAC_VER_11] =
213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
214 [RTL_GIGA_MAC_VER_12] =
215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
216 [RTL_GIGA_MAC_VER_13] =
217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
218 [RTL_GIGA_MAC_VER_14] =
219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
220 [RTL_GIGA_MAC_VER_15] =
221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
222 [RTL_GIGA_MAC_VER_16] =
223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
224 [RTL_GIGA_MAC_VER_17] =
225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
226 [RTL_GIGA_MAC_VER_18] =
227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
228 [RTL_GIGA_MAC_VER_19] =
229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
230 [RTL_GIGA_MAC_VER_20] =
231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
232 [RTL_GIGA_MAC_VER_21] =
233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
234 [RTL_GIGA_MAC_VER_22] =
235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
236 [RTL_GIGA_MAC_VER_23] =
237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
238 [RTL_GIGA_MAC_VER_24] =
239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
240 [RTL_GIGA_MAC_VER_25] =
241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
243 [RTL_GIGA_MAC_VER_26] =
244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
246 [RTL_GIGA_MAC_VER_27] =
247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
248 [RTL_GIGA_MAC_VER_28] =
249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
250 [RTL_GIGA_MAC_VER_29] =
251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
253 [RTL_GIGA_MAC_VER_30] =
254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
256 [RTL_GIGA_MAC_VER_31] =
257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
258 [RTL_GIGA_MAC_VER_32] =
259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
261 [RTL_GIGA_MAC_VER_33] =
262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
264 [RTL_GIGA_MAC_VER_34] =
265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
267 [RTL_GIGA_MAC_VER_35] =
268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
270 [RTL_GIGA_MAC_VER_36] =
271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
282 [RTL_GIGA_MAC_VER_40] =
283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
326 static const struct pci_device_id rtl8169_pci_tbl[] = {
327 { PCI_VDEVICE(REALTEK, 0x2502), RTL_CFG_1 },
328 { PCI_VDEVICE(REALTEK, 0x2600), RTL_CFG_1 },
329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
332 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
333 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
334 { PCI_DEVICE(PCI_VENDOR_ID_NCUBE, 0x8168), 0, 0, RTL_CFG_1 },
335 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
336 { PCI_VENDOR_ID_DLINK, 0x4300,
337 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
338 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
339 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
340 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
341 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
342 { PCI_VENDOR_ID_LINKSYS, 0x1032,
343 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
345 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
349 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
351 static int rx_buf_sz = 16383;
352 static int use_dac = -1;
358 MAC0 = 0, /* Ethernet hardware address. */
360 MAR0 = 8, /* Multicast filter. */
361 CounterAddrLow = 0x10,
362 CounterAddrHigh = 0x14,
363 TxDescStartAddrLow = 0x20,
364 TxDescStartAddrHigh = 0x24,
365 TxHDescStartAddrLow = 0x28,
366 TxHDescStartAddrHigh = 0x2c,
375 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
376 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
379 #define RX128_INT_EN (1 << 15) /* 8111c and later */
380 #define RX_MULTI_EN (1 << 14) /* 8111c only */
381 #define RXCFG_FIFO_SHIFT 13
382 /* No threshold before first PCI xfer */
383 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
384 #define RX_EARLY_OFF (1 << 11)
385 #define RXCFG_DMA_SHIFT 8
386 /* Unlimited maximum PCI burst. */
387 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
394 #define PME_SIGNAL (1 << 5) /* 8168c and later */
405 RxDescAddrLow = 0xe4,
406 RxDescAddrHigh = 0xe8,
407 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
409 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
411 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
413 #define TxPacketMax (8064 >> 7)
414 #define EarlySize 0x27
417 FuncEventMask = 0xf4,
418 FuncPresetState = 0xf8,
423 FuncForceEvent = 0xfc,
426 enum rtl8110_registers {
432 enum rtl8168_8101_registers {
435 #define CSIAR_FLAG 0x80000000
436 #define CSIAR_WRITE_CMD 0x80000000
437 #define CSIAR_BYTE_ENABLE 0x0f
438 #define CSIAR_BYTE_ENABLE_SHIFT 12
439 #define CSIAR_ADDR_MASK 0x0fff
440 #define CSIAR_FUNC_CARD 0x00000000
441 #define CSIAR_FUNC_SDIO 0x00010000
442 #define CSIAR_FUNC_NIC 0x00020000
443 #define CSIAR_FUNC_NIC2 0x00010000
446 #define EPHYAR_FLAG 0x80000000
447 #define EPHYAR_WRITE_CMD 0x80000000
448 #define EPHYAR_REG_MASK 0x1f
449 #define EPHYAR_REG_SHIFT 16
450 #define EPHYAR_DATA_MASK 0xffff
452 #define PFM_EN (1 << 6)
453 #define TX_10M_PS_EN (1 << 7)
455 #define FIX_NAK_1 (1 << 4)
456 #define FIX_NAK_2 (1 << 3)
459 #define NOW_IS_OOB (1 << 7)
460 #define TX_EMPTY (1 << 5)
461 #define RX_EMPTY (1 << 4)
462 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
463 #define EN_NDP (1 << 3)
464 #define EN_OOB_RESET (1 << 2)
465 #define LINK_LIST_RDY (1 << 1)
467 #define EFUSEAR_FLAG 0x80000000
468 #define EFUSEAR_WRITE_CMD 0x80000000
469 #define EFUSEAR_READ_CMD 0x00000000
470 #define EFUSEAR_REG_MASK 0x03ff
471 #define EFUSEAR_REG_SHIFT 8
472 #define EFUSEAR_DATA_MASK 0xff
474 #define PFM_D3COLD_EN (1 << 6)
477 enum rtl8168_registers {
482 #define ERIAR_FLAG 0x80000000
483 #define ERIAR_WRITE_CMD 0x80000000
484 #define ERIAR_READ_CMD 0x00000000
485 #define ERIAR_ADDR_BYTE_ALIGN 4
486 #define ERIAR_TYPE_SHIFT 16
487 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
488 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
489 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
490 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
491 #define ERIAR_MASK_SHIFT 12
492 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
493 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
494 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
495 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
496 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
497 EPHY_RXER_NUM = 0x7c,
498 OCPDR = 0xb0, /* OCP GPHY access */
499 #define OCPDR_WRITE_CMD 0x80000000
500 #define OCPDR_READ_CMD 0x00000000
501 #define OCPDR_REG_MASK 0x7f
502 #define OCPDR_GPHY_REG_SHIFT 16
503 #define OCPDR_DATA_MASK 0xffff
505 #define OCPAR_FLAG 0x80000000
506 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
507 #define OCPAR_GPHY_READ_CMD 0x0000f060
509 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
510 MISC = 0xf0, /* 8168e only. */
511 #define TXPLA_RST (1 << 29)
512 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
513 #define PWM_EN (1 << 22)
514 #define RXDV_GATED_EN (1 << 19)
515 #define EARLY_TALLY_EN (1 << 16)
518 enum rtl_register_content {
519 /* InterruptStatusBits */
523 TxDescUnavail = 0x0080,
547 /* TXPoll register p.5 */
548 HPQ = 0x80, /* Poll cmd on the high prio queue */
549 NPQ = 0x40, /* Poll cmd on the low prio queue */
550 FSWInt = 0x01, /* Forced software interrupt */
554 Cfg9346_Unlock = 0xc0,
559 AcceptBroadcast = 0x08,
560 AcceptMulticast = 0x04,
562 AcceptAllPhys = 0x01,
563 #define RX_CONFIG_ACCEPT_MASK 0x3f
566 TxInterFrameGapShift = 24,
567 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
569 /* Config1 register p.24 */
572 Speed_down = (1 << 4),
576 PMEnable = (1 << 0), /* Power Management Enable */
578 /* Config2 register p. 25 */
579 ClkReqEn = (1 << 7), /* Clock Request Enable */
580 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
581 PCI_Clock_66MHz = 0x01,
582 PCI_Clock_33MHz = 0x00,
584 /* Config3 register p.25 */
585 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
586 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
587 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
588 Rdy_to_L23 = (1 << 1), /* L23 Enable */
589 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
591 /* Config4 register */
592 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
594 /* Config5 register p.27 */
595 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
596 MWF = (1 << 5), /* Accept Multicast wakeup frame */
597 UWF = (1 << 4), /* Accept Unicast wakeup frame */
599 LanWake = (1 << 1), /* LanWake enable/disable */
600 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
601 ASPM_en = (1 << 0), /* ASPM enable */
604 TBIReset = 0x80000000,
605 TBILoopback = 0x40000000,
606 TBINwEnable = 0x20000000,
607 TBINwRestart = 0x10000000,
608 TBILinkOk = 0x02000000,
609 TBINwComplete = 0x01000000,
612 EnableBist = (1 << 15), // 8168 8101
613 Mac_dbgo_oe = (1 << 14), // 8168 8101
614 Normal_mode = (1 << 13), // unused
615 Force_half_dup = (1 << 12), // 8168 8101
616 Force_rxflow_en = (1 << 11), // 8168 8101
617 Force_txflow_en = (1 << 10), // 8168 8101
618 Cxpl_dbg_sel = (1 << 9), // 8168 8101
619 ASF = (1 << 8), // 8168 8101
620 PktCntrDisable = (1 << 7), // 8168 8101
621 Mac_dbgo_sel = 0x001c, // 8168
626 INTT_0 = 0x0000, // 8168
627 INTT_1 = 0x0001, // 8168
628 INTT_2 = 0x0002, // 8168
629 INTT_3 = 0x0003, // 8168
631 /* rtl8169_PHYstatus */
642 TBILinkOK = 0x02000000,
644 /* ResetCounterCommand */
647 /* DumpCounterCommand */
650 /* magic enable v2 */
651 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
655 /* First doubleword. */
656 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
657 RingEnd = (1 << 30), /* End of descriptor ring */
658 FirstFrag = (1 << 29), /* First segment of a packet */
659 LastFrag = (1 << 28), /* Final segment of a packet */
663 enum rtl_tx_desc_bit {
664 /* First doubleword. */
665 TD_LSO = (1 << 27), /* Large Send Offload */
666 #define TD_MSS_MAX 0x07ffu /* MSS value */
668 /* Second doubleword. */
669 TxVlanTag = (1 << 17), /* Add VLAN tag */
672 /* 8169, 8168b and 810x except 8102e. */
673 enum rtl_tx_desc_bit_0 {
674 /* First doubleword. */
675 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
676 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
677 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
678 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
681 /* 8102e, 8168c and beyond. */
682 enum rtl_tx_desc_bit_1 {
683 /* First doubleword. */
684 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
685 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
686 #define GTTCPHO_SHIFT 18
687 #define GTTCPHO_MAX 0x7fU
689 /* Second doubleword. */
690 #define TCPHO_SHIFT 18
691 #define TCPHO_MAX 0x3ffU
692 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
693 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
694 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
695 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
696 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
699 enum rtl_rx_desc_bit {
701 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
702 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
704 #define RxProtoUDP (PID1)
705 #define RxProtoTCP (PID0)
706 #define RxProtoIP (PID1 | PID0)
707 #define RxProtoMask RxProtoIP
709 IPFail = (1 << 16), /* IP checksum failed */
710 UDPFail = (1 << 15), /* UDP/IP checksum failed */
711 TCPFail = (1 << 14), /* TCP/IP checksum failed */
712 RxVlanTag = (1 << 16), /* VLAN tag available */
715 #define RsvdMask 0x3fffc000
732 u8 __pad[sizeof(void *) - sizeof(u32)];
736 RTL_FEATURE_WOL = (1 << 0),
737 RTL_FEATURE_MSI = (1 << 1),
738 RTL_FEATURE_GMII = (1 << 2),
741 struct rtl8169_counters {
748 __le32 tx_one_collision;
749 __le32 tx_multi_collision;
757 struct rtl8169_tc_offsets {
760 __le32 tx_multi_collision;
765 RTL_FLAG_TASK_ENABLED = 0,
766 RTL_FLAG_TASK_SLOW_PENDING,
767 RTL_FLAG_TASK_RESET_PENDING,
768 RTL_FLAG_TASK_PHY_PENDING,
772 struct rtl8169_stats {
775 struct u64_stats_sync syncp;
778 struct rtl8169_private {
779 void __iomem *mmio_addr; /* memory map physical address */
780 struct pci_dev *pci_dev;
781 struct net_device *dev;
782 struct napi_struct napi;
786 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
787 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
789 struct rtl8169_stats rx_stats;
790 struct rtl8169_stats tx_stats;
791 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
792 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
793 dma_addr_t TxPhyAddr;
794 dma_addr_t RxPhyAddr;
795 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
796 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
797 struct timer_list timer;
803 void (*write)(struct rtl8169_private *, int, int);
804 int (*read)(struct rtl8169_private *, int);
807 struct pll_power_ops {
808 void (*down)(struct rtl8169_private *);
809 void (*up)(struct rtl8169_private *);
813 void (*enable)(struct rtl8169_private *);
814 void (*disable)(struct rtl8169_private *);
818 void (*write)(struct rtl8169_private *, int, int);
819 u32 (*read)(struct rtl8169_private *, int);
822 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
823 int (*get_link_ksettings)(struct net_device *,
824 struct ethtool_link_ksettings *);
825 void (*phy_reset_enable)(struct rtl8169_private *tp);
826 void (*hw_start)(struct net_device *);
827 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
828 unsigned int (*link_ok)(void __iomem *);
829 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
830 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
833 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
835 struct work_struct work;
840 struct mii_if_info mii;
841 dma_addr_t counters_phys_addr;
842 struct rtl8169_counters *counters;
843 struct rtl8169_tc_offsets tc_offset;
848 const struct firmware *fw;
850 #define RTL_VER_SIZE 32
852 char version[RTL_VER_SIZE];
854 struct rtl_fw_phy_action {
859 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
864 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
865 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
866 module_param(use_dac, int, 0);
867 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
868 module_param_named(debug, debug.msg_enable, int, 0);
869 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
870 MODULE_LICENSE("GPL");
871 MODULE_VERSION(RTL8169_VERSION);
874 static void rtl_lock_work(struct rtl8169_private *tp)
876 mutex_lock(&tp->wk.mutex);
879 static void rtl_unlock_work(struct rtl8169_private *tp)
881 mutex_unlock(&tp->wk.mutex);
884 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
886 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
887 PCI_EXP_DEVCTL_READRQ, force);
891 bool (*check)(struct rtl8169_private *);
895 static void rtl_udelay(unsigned int d)
900 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
901 void (*delay)(unsigned int), unsigned int d, int n,
906 for (i = 0; i < n; i++) {
908 if (c->check(tp) == high)
911 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
912 c->msg, !high, n, d);
916 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
917 const struct rtl_cond *c,
918 unsigned int d, int n)
920 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
923 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
924 const struct rtl_cond *c,
925 unsigned int d, int n)
927 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
930 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
931 const struct rtl_cond *c,
932 unsigned int d, int n)
934 return rtl_loop_wait(tp, c, msleep, d, n, true);
937 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
938 const struct rtl_cond *c,
939 unsigned int d, int n)
941 return rtl_loop_wait(tp, c, msleep, d, n, false);
944 #define DECLARE_RTL_COND(name) \
945 static bool name ## _check(struct rtl8169_private *); \
947 static const struct rtl_cond name = { \
948 .check = name ## _check, \
952 static bool name ## _check(struct rtl8169_private *tp)
954 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
956 if (reg & 0xffff0001) {
957 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
963 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
965 void __iomem *ioaddr = tp->mmio_addr;
967 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
970 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
972 void __iomem *ioaddr = tp->mmio_addr;
974 if (rtl_ocp_reg_failure(tp, reg))
977 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
979 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
982 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
984 void __iomem *ioaddr = tp->mmio_addr;
986 if (rtl_ocp_reg_failure(tp, reg))
989 RTL_W32(GPHY_OCP, reg << 15);
991 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
992 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
995 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
997 void __iomem *ioaddr = tp->mmio_addr;
999 if (rtl_ocp_reg_failure(tp, reg))
1002 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
1005 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1007 void __iomem *ioaddr = tp->mmio_addr;
1009 if (rtl_ocp_reg_failure(tp, reg))
1012 RTL_W32(OCPDR, reg << 15);
1014 return RTL_R32(OCPDR);
1017 #define OCP_STD_PHY_BASE 0xa400
1019 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1022 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1026 if (tp->ocp_base != OCP_STD_PHY_BASE)
1029 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1032 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1034 if (tp->ocp_base != OCP_STD_PHY_BASE)
1037 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1040 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1043 tp->ocp_base = value << 4;
1047 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1050 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1052 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1055 DECLARE_RTL_COND(rtl_phyar_cond)
1057 void __iomem *ioaddr = tp->mmio_addr;
1059 return RTL_R32(PHYAR) & 0x80000000;
1062 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1064 void __iomem *ioaddr = tp->mmio_addr;
1066 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1068 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1070 * According to hardware specs a 20us delay is required after write
1071 * complete indication, but before sending next command.
1076 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1078 void __iomem *ioaddr = tp->mmio_addr;
1081 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
1083 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1084 RTL_R32(PHYAR) & 0xffff : ~0;
1087 * According to hardware specs a 20us delay is required after read
1088 * complete indication, but before sending next command.
1095 DECLARE_RTL_COND(rtl_ocpar_cond)
1097 void __iomem *ioaddr = tp->mmio_addr;
1099 return RTL_R32(OCPAR) & OCPAR_FLAG;
1102 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1104 void __iomem *ioaddr = tp->mmio_addr;
1106 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1107 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1108 RTL_W32(EPHY_RXER_NUM, 0);
1110 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1113 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1115 r8168dp_1_mdio_access(tp, reg,
1116 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1119 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1121 void __iomem *ioaddr = tp->mmio_addr;
1123 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1126 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1127 RTL_W32(EPHY_RXER_NUM, 0);
1129 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1130 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
1133 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1135 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1137 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1140 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1142 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1145 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1147 void __iomem *ioaddr = tp->mmio_addr;
1149 r8168dp_2_mdio_start(ioaddr);
1151 r8169_mdio_write(tp, reg, value);
1153 r8168dp_2_mdio_stop(ioaddr);
1156 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1158 void __iomem *ioaddr = tp->mmio_addr;
1161 r8168dp_2_mdio_start(ioaddr);
1163 value = r8169_mdio_read(tp, reg);
1165 r8168dp_2_mdio_stop(ioaddr);
1170 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1172 tp->mdio_ops.write(tp, location, val);
1175 static int rtl_readphy(struct rtl8169_private *tp, int location)
1177 return tp->mdio_ops.read(tp, location);
1180 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1182 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1185 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1189 val = rtl_readphy(tp, reg_addr);
1190 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1193 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1196 struct rtl8169_private *tp = netdev_priv(dev);
1198 rtl_writephy(tp, location, val);
1201 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1203 struct rtl8169_private *tp = netdev_priv(dev);
1205 return rtl_readphy(tp, location);
1208 DECLARE_RTL_COND(rtl_ephyar_cond)
1210 void __iomem *ioaddr = tp->mmio_addr;
1212 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1215 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1217 void __iomem *ioaddr = tp->mmio_addr;
1219 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1220 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1222 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1227 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1229 void __iomem *ioaddr = tp->mmio_addr;
1231 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1233 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1234 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
1237 DECLARE_RTL_COND(rtl_eriar_cond)
1239 void __iomem *ioaddr = tp->mmio_addr;
1241 return RTL_R32(ERIAR) & ERIAR_FLAG;
1244 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1247 void __iomem *ioaddr = tp->mmio_addr;
1249 BUG_ON((addr & 3) || (mask == 0));
1250 RTL_W32(ERIDR, val);
1251 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1253 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1256 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1258 void __iomem *ioaddr = tp->mmio_addr;
1260 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1262 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1263 RTL_R32(ERIDR) : ~0;
1266 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1271 val = rtl_eri_read(tp, addr, type);
1272 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1275 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1277 void __iomem *ioaddr = tp->mmio_addr;
1279 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1280 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1281 RTL_R32(OCPDR) : ~0;
1284 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1286 return rtl_eri_read(tp, reg, ERIAR_OOB);
1289 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1291 switch (tp->mac_version) {
1292 case RTL_GIGA_MAC_VER_27:
1293 case RTL_GIGA_MAC_VER_28:
1294 case RTL_GIGA_MAC_VER_31:
1295 return r8168dp_ocp_read(tp, mask, reg);
1296 case RTL_GIGA_MAC_VER_49:
1297 case RTL_GIGA_MAC_VER_50:
1298 case RTL_GIGA_MAC_VER_51:
1299 return r8168ep_ocp_read(tp, mask, reg);
1306 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1309 void __iomem *ioaddr = tp->mmio_addr;
1311 RTL_W32(OCPDR, data);
1312 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1313 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1316 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1319 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1323 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1325 switch (tp->mac_version) {
1326 case RTL_GIGA_MAC_VER_27:
1327 case RTL_GIGA_MAC_VER_28:
1328 case RTL_GIGA_MAC_VER_31:
1329 r8168dp_ocp_write(tp, mask, reg, data);
1331 case RTL_GIGA_MAC_VER_49:
1332 case RTL_GIGA_MAC_VER_50:
1333 case RTL_GIGA_MAC_VER_51:
1334 r8168ep_ocp_write(tp, mask, reg, data);
1342 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1344 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1346 ocp_write(tp, 0x1, 0x30, 0x00000001);
1349 #define OOB_CMD_RESET 0x00
1350 #define OOB_CMD_DRIVER_START 0x05
1351 #define OOB_CMD_DRIVER_STOP 0x06
1353 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1355 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1358 DECLARE_RTL_COND(rtl_ocp_read_cond)
1362 reg = rtl8168_get_ocp_reg(tp);
1364 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1367 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1369 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1372 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1374 void __iomem *ioaddr = tp->mmio_addr;
1376 return RTL_R8(IBISR0) & 0x20;
1379 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1381 void __iomem *ioaddr = tp->mmio_addr;
1383 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
1384 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1385 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1386 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1389 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1391 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1392 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1395 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1397 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1398 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1399 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1402 static void rtl8168_driver_start(struct rtl8169_private *tp)
1404 switch (tp->mac_version) {
1405 case RTL_GIGA_MAC_VER_27:
1406 case RTL_GIGA_MAC_VER_28:
1407 case RTL_GIGA_MAC_VER_31:
1408 rtl8168dp_driver_start(tp);
1410 case RTL_GIGA_MAC_VER_49:
1411 case RTL_GIGA_MAC_VER_50:
1412 case RTL_GIGA_MAC_VER_51:
1413 rtl8168ep_driver_start(tp);
1421 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1423 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1424 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1427 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1429 rtl8168ep_stop_cmac(tp);
1430 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1431 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1432 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1435 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1437 switch (tp->mac_version) {
1438 case RTL_GIGA_MAC_VER_27:
1439 case RTL_GIGA_MAC_VER_28:
1440 case RTL_GIGA_MAC_VER_31:
1441 rtl8168dp_driver_stop(tp);
1443 case RTL_GIGA_MAC_VER_49:
1444 case RTL_GIGA_MAC_VER_50:
1445 case RTL_GIGA_MAC_VER_51:
1446 rtl8168ep_driver_stop(tp);
1454 static int r8168dp_check_dash(struct rtl8169_private *tp)
1456 u16 reg = rtl8168_get_ocp_reg(tp);
1458 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1461 static int r8168ep_check_dash(struct rtl8169_private *tp)
1463 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1466 static int r8168_check_dash(struct rtl8169_private *tp)
1468 switch (tp->mac_version) {
1469 case RTL_GIGA_MAC_VER_27:
1470 case RTL_GIGA_MAC_VER_28:
1471 case RTL_GIGA_MAC_VER_31:
1472 return r8168dp_check_dash(tp);
1473 case RTL_GIGA_MAC_VER_49:
1474 case RTL_GIGA_MAC_VER_50:
1475 case RTL_GIGA_MAC_VER_51:
1476 return r8168ep_check_dash(tp);
1488 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1489 const struct exgmac_reg *r, int len)
1492 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1497 DECLARE_RTL_COND(rtl_efusear_cond)
1499 void __iomem *ioaddr = tp->mmio_addr;
1501 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1504 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1506 void __iomem *ioaddr = tp->mmio_addr;
1508 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1510 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1511 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1514 static u16 rtl_get_events(struct rtl8169_private *tp)
1516 void __iomem *ioaddr = tp->mmio_addr;
1518 return RTL_R16(IntrStatus);
1521 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1523 void __iomem *ioaddr = tp->mmio_addr;
1525 RTL_W16(IntrStatus, bits);
1529 static void rtl_irq_disable(struct rtl8169_private *tp)
1531 void __iomem *ioaddr = tp->mmio_addr;
1533 RTL_W16(IntrMask, 0);
1537 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1539 void __iomem *ioaddr = tp->mmio_addr;
1541 RTL_W16(IntrMask, bits);
1544 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1545 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1546 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1548 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1550 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1553 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1555 void __iomem *ioaddr = tp->mmio_addr;
1557 rtl_irq_disable(tp);
1558 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1562 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1564 void __iomem *ioaddr = tp->mmio_addr;
1566 return RTL_R32(TBICSR) & TBIReset;
1569 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1571 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1574 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1576 return RTL_R32(TBICSR) & TBILinkOk;
1579 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1581 return RTL_R8(PHYstatus) & LinkStatus;
1584 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1586 void __iomem *ioaddr = tp->mmio_addr;
1588 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1591 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1595 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1596 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1599 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1601 void __iomem *ioaddr = tp->mmio_addr;
1602 struct net_device *dev = tp->dev;
1604 if (!netif_running(dev))
1607 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1608 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1609 if (RTL_R8(PHYstatus) & _1000bpsF) {
1610 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1612 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1614 } else if (RTL_R8(PHYstatus) & _100bps) {
1615 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1617 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1620 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1622 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1625 /* Reset packet filter */
1626 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1628 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1630 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1631 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1632 if (RTL_R8(PHYstatus) & _1000bpsF) {
1633 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1635 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1638 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1640 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1643 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1644 if (RTL_R8(PHYstatus) & _10bps) {
1645 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1647 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1650 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1656 static void __rtl8169_check_link_status(struct net_device *dev,
1657 struct rtl8169_private *tp,
1658 void __iomem *ioaddr, bool pm)
1660 if (tp->link_ok(ioaddr)) {
1661 rtl_link_chg_patch(tp);
1662 /* This is to cancel a scheduled suspend if there's one. */
1664 pm_request_resume(&tp->pci_dev->dev);
1665 netif_carrier_on(dev);
1666 if (net_ratelimit())
1667 netif_info(tp, ifup, dev, "link up\n");
1669 netif_carrier_off(dev);
1670 netif_info(tp, ifdown, dev, "link down\n");
1672 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1676 static void rtl8169_check_link_status(struct net_device *dev,
1677 struct rtl8169_private *tp,
1678 void __iomem *ioaddr)
1680 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1683 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1685 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1687 void __iomem *ioaddr = tp->mmio_addr;
1691 options = RTL_R8(Config1);
1692 if (!(options & PMEnable))
1695 options = RTL_R8(Config3);
1696 if (options & LinkUp)
1697 wolopts |= WAKE_PHY;
1698 switch (tp->mac_version) {
1699 case RTL_GIGA_MAC_VER_34:
1700 case RTL_GIGA_MAC_VER_35:
1701 case RTL_GIGA_MAC_VER_36:
1702 case RTL_GIGA_MAC_VER_37:
1703 case RTL_GIGA_MAC_VER_38:
1704 case RTL_GIGA_MAC_VER_40:
1705 case RTL_GIGA_MAC_VER_41:
1706 case RTL_GIGA_MAC_VER_42:
1707 case RTL_GIGA_MAC_VER_43:
1708 case RTL_GIGA_MAC_VER_44:
1709 case RTL_GIGA_MAC_VER_45:
1710 case RTL_GIGA_MAC_VER_46:
1711 case RTL_GIGA_MAC_VER_47:
1712 case RTL_GIGA_MAC_VER_48:
1713 case RTL_GIGA_MAC_VER_49:
1714 case RTL_GIGA_MAC_VER_50:
1715 case RTL_GIGA_MAC_VER_51:
1716 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1717 wolopts |= WAKE_MAGIC;
1720 if (options & MagicPacket)
1721 wolopts |= WAKE_MAGIC;
1725 options = RTL_R8(Config5);
1727 wolopts |= WAKE_UCAST;
1729 wolopts |= WAKE_BCAST;
1731 wolopts |= WAKE_MCAST;
1736 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1738 struct rtl8169_private *tp = netdev_priv(dev);
1739 struct device *d = &tp->pci_dev->dev;
1741 pm_runtime_get_noresume(d);
1745 wol->supported = WAKE_ANY;
1746 if (pm_runtime_active(d))
1747 wol->wolopts = __rtl8169_get_wol(tp);
1749 wol->wolopts = tp->saved_wolopts;
1751 rtl_unlock_work(tp);
1753 pm_runtime_put_noidle(d);
1756 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1758 void __iomem *ioaddr = tp->mmio_addr;
1759 unsigned int i, tmp;
1760 static const struct {
1765 { WAKE_PHY, Config3, LinkUp },
1766 { WAKE_UCAST, Config5, UWF },
1767 { WAKE_BCAST, Config5, BWF },
1768 { WAKE_MCAST, Config5, MWF },
1769 { WAKE_ANY, Config5, LanWake },
1770 { WAKE_MAGIC, Config3, MagicPacket }
1774 RTL_W8(Cfg9346, Cfg9346_Unlock);
1776 switch (tp->mac_version) {
1777 case RTL_GIGA_MAC_VER_34:
1778 case RTL_GIGA_MAC_VER_35:
1779 case RTL_GIGA_MAC_VER_36:
1780 case RTL_GIGA_MAC_VER_37:
1781 case RTL_GIGA_MAC_VER_38:
1782 case RTL_GIGA_MAC_VER_40:
1783 case RTL_GIGA_MAC_VER_41:
1784 case RTL_GIGA_MAC_VER_42:
1785 case RTL_GIGA_MAC_VER_43:
1786 case RTL_GIGA_MAC_VER_44:
1787 case RTL_GIGA_MAC_VER_45:
1788 case RTL_GIGA_MAC_VER_46:
1789 case RTL_GIGA_MAC_VER_47:
1790 case RTL_GIGA_MAC_VER_48:
1791 case RTL_GIGA_MAC_VER_49:
1792 case RTL_GIGA_MAC_VER_50:
1793 case RTL_GIGA_MAC_VER_51:
1794 tmp = ARRAY_SIZE(cfg) - 1;
1795 if (wolopts & WAKE_MAGIC)
1811 tmp = ARRAY_SIZE(cfg);
1815 for (i = 0; i < tmp; i++) {
1816 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1817 if (wolopts & cfg[i].opt)
1818 options |= cfg[i].mask;
1819 RTL_W8(cfg[i].reg, options);
1822 switch (tp->mac_version) {
1823 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1824 options = RTL_R8(Config1) & ~PMEnable;
1826 options |= PMEnable;
1827 RTL_W8(Config1, options);
1830 options = RTL_R8(Config2) & ~PME_SIGNAL;
1832 options |= PME_SIGNAL;
1833 RTL_W8(Config2, options);
1837 RTL_W8(Cfg9346, Cfg9346_Lock);
1840 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1842 struct rtl8169_private *tp = netdev_priv(dev);
1843 struct device *d = &tp->pci_dev->dev;
1845 pm_runtime_get_noresume(d);
1850 tp->features |= RTL_FEATURE_WOL;
1852 tp->features &= ~RTL_FEATURE_WOL;
1853 if (pm_runtime_active(d))
1854 __rtl8169_set_wol(tp, wol->wolopts);
1856 tp->saved_wolopts = wol->wolopts;
1858 rtl_unlock_work(tp);
1860 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1862 pm_runtime_put_noidle(d);
1867 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1869 return rtl_chip_infos[tp->mac_version].fw_name;
1872 static void rtl8169_get_drvinfo(struct net_device *dev,
1873 struct ethtool_drvinfo *info)
1875 struct rtl8169_private *tp = netdev_priv(dev);
1876 struct rtl_fw *rtl_fw = tp->rtl_fw;
1878 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1879 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1880 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1881 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1882 if (!IS_ERR_OR_NULL(rtl_fw))
1883 strlcpy(info->fw_version, rtl_fw->version,
1884 sizeof(info->fw_version));
1887 static int rtl8169_get_regs_len(struct net_device *dev)
1889 return R8169_REGS_SIZE;
1892 static int rtl8169_set_speed_tbi(struct net_device *dev,
1893 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1895 struct rtl8169_private *tp = netdev_priv(dev);
1896 void __iomem *ioaddr = tp->mmio_addr;
1900 reg = RTL_R32(TBICSR);
1901 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1902 (duplex == DUPLEX_FULL)) {
1903 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1904 } else if (autoneg == AUTONEG_ENABLE)
1905 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1907 netif_warn(tp, link, dev,
1908 "incorrect speed setting refused in TBI mode\n");
1915 static int rtl8169_set_speed_xmii(struct net_device *dev,
1916 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1918 struct rtl8169_private *tp = netdev_priv(dev);
1919 int giga_ctrl, bmcr;
1922 rtl_writephy(tp, 0x1f, 0x0000);
1924 if (autoneg == AUTONEG_ENABLE) {
1927 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1928 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1929 ADVERTISE_100HALF | ADVERTISE_100FULL);
1931 if (adv & ADVERTISED_10baseT_Half)
1932 auto_nego |= ADVERTISE_10HALF;
1933 if (adv & ADVERTISED_10baseT_Full)
1934 auto_nego |= ADVERTISE_10FULL;
1935 if (adv & ADVERTISED_100baseT_Half)
1936 auto_nego |= ADVERTISE_100HALF;
1937 if (adv & ADVERTISED_100baseT_Full)
1938 auto_nego |= ADVERTISE_100FULL;
1940 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1942 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1943 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1945 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1946 if (tp->mii.supports_gmii) {
1947 if (adv & ADVERTISED_1000baseT_Half)
1948 giga_ctrl |= ADVERTISE_1000HALF;
1949 if (adv & ADVERTISED_1000baseT_Full)
1950 giga_ctrl |= ADVERTISE_1000FULL;
1951 } else if (adv & (ADVERTISED_1000baseT_Half |
1952 ADVERTISED_1000baseT_Full)) {
1953 netif_info(tp, link, dev,
1954 "PHY does not support 1000Mbps\n");
1958 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1960 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1961 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1965 if (speed == SPEED_10)
1967 else if (speed == SPEED_100)
1968 bmcr = BMCR_SPEED100;
1972 if (duplex == DUPLEX_FULL)
1973 bmcr |= BMCR_FULLDPLX;
1976 rtl_writephy(tp, MII_BMCR, bmcr);
1978 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1979 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1980 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1981 rtl_writephy(tp, 0x17, 0x2138);
1982 rtl_writephy(tp, 0x0e, 0x0260);
1984 rtl_writephy(tp, 0x17, 0x2108);
1985 rtl_writephy(tp, 0x0e, 0x0000);
1994 static int rtl8169_set_speed(struct net_device *dev,
1995 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1997 struct rtl8169_private *tp = netdev_priv(dev);
2000 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
2004 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
2005 (advertising & ADVERTISED_1000baseT_Full) &&
2006 !pci_is_pcie(tp->pci_dev)) {
2007 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
2013 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2014 netdev_features_t features)
2016 struct rtl8169_private *tp = netdev_priv(dev);
2018 if (dev->mtu > TD_MSS_MAX)
2019 features &= ~NETIF_F_ALL_TSO;
2021 if (dev->mtu > JUMBO_1K &&
2022 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2023 features &= ~NETIF_F_IP_CSUM;
2028 static void __rtl8169_set_features(struct net_device *dev,
2029 netdev_features_t features)
2031 struct rtl8169_private *tp = netdev_priv(dev);
2032 void __iomem *ioaddr = tp->mmio_addr;
2035 rx_config = RTL_R32(RxConfig);
2036 if (features & NETIF_F_RXALL)
2037 rx_config |= (AcceptErr | AcceptRunt);
2039 rx_config &= ~(AcceptErr | AcceptRunt);
2041 RTL_W32(RxConfig, rx_config);
2043 if (features & NETIF_F_RXCSUM)
2044 tp->cp_cmd |= RxChkSum;
2046 tp->cp_cmd &= ~RxChkSum;
2048 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2049 tp->cp_cmd |= RxVlan;
2051 tp->cp_cmd &= ~RxVlan;
2053 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2055 RTL_W16(CPlusCmd, tp->cp_cmd);
2059 static int rtl8169_set_features(struct net_device *dev,
2060 netdev_features_t features)
2062 struct rtl8169_private *tp = netdev_priv(dev);
2064 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2067 if (features ^ dev->features)
2068 __rtl8169_set_features(dev, features);
2069 rtl_unlock_work(tp);
2075 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
2077 return (skb_vlan_tag_present(skb)) ?
2078 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
2081 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
2083 u32 opts2 = le32_to_cpu(desc->opts2);
2085 if (opts2 & RxVlanTag)
2086 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
2089 static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2090 struct ethtool_link_ksettings *cmd)
2092 struct rtl8169_private *tp = netdev_priv(dev);
2093 void __iomem *ioaddr = tp->mmio_addr;
2095 u32 supported, advertising;
2098 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
2099 cmd->base.port = PORT_FIBRE;
2101 status = RTL_R32(TBICSR);
2102 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2103 cmd->base.autoneg = !!(status & TBINwEnable);
2105 cmd->base.speed = SPEED_1000;
2106 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2108 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2110 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2116 static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2117 struct ethtool_link_ksettings *cmd)
2119 struct rtl8169_private *tp = netdev_priv(dev);
2121 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2126 static int rtl8169_get_link_ksettings(struct net_device *dev,
2127 struct ethtool_link_ksettings *cmd)
2129 struct rtl8169_private *tp = netdev_priv(dev);
2133 rc = tp->get_link_ksettings(dev, cmd);
2134 rtl_unlock_work(tp);
2139 static int rtl8169_set_link_ksettings(struct net_device *dev,
2140 const struct ethtool_link_ksettings *cmd)
2142 struct rtl8169_private *tp = netdev_priv(dev);
2146 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2147 cmd->link_modes.advertising))
2150 del_timer_sync(&tp->timer);
2153 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2154 cmd->base.duplex, advertising);
2155 rtl_unlock_work(tp);
2160 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2163 struct rtl8169_private *tp = netdev_priv(dev);
2164 u32 __iomem *data = tp->mmio_addr;
2169 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2170 memcpy_fromio(dw++, data++, 4);
2171 rtl_unlock_work(tp);
2174 static u32 rtl8169_get_msglevel(struct net_device *dev)
2176 struct rtl8169_private *tp = netdev_priv(dev);
2178 return tp->msg_enable;
2181 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2183 struct rtl8169_private *tp = netdev_priv(dev);
2185 tp->msg_enable = value;
2188 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2195 "tx_single_collisions",
2196 "tx_multi_collisions",
2204 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
2208 return ARRAY_SIZE(rtl8169_gstrings);
2214 DECLARE_RTL_COND(rtl_counters_cond)
2216 void __iomem *ioaddr = tp->mmio_addr;
2218 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
2221 static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
2223 struct rtl8169_private *tp = netdev_priv(dev);
2224 void __iomem *ioaddr = tp->mmio_addr;
2225 dma_addr_t paddr = tp->counters_phys_addr;
2228 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
2229 RTL_R32(CounterAddrHigh);
2230 cmd = (u64)paddr & DMA_BIT_MASK(32);
2231 RTL_W32(CounterAddrLow, cmd);
2232 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2234 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
2237 static bool rtl8169_reset_counters(struct net_device *dev)
2239 struct rtl8169_private *tp = netdev_priv(dev);
2242 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2245 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2248 return rtl8169_do_counters(dev, CounterReset);
2251 static bool rtl8169_update_counters(struct net_device *dev)
2253 struct rtl8169_private *tp = netdev_priv(dev);
2254 void __iomem *ioaddr = tp->mmio_addr;
2257 * Some chips are unable to dump tally counters when the receiver
2260 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
2263 return rtl8169_do_counters(dev, CounterDump);
2266 static bool rtl8169_init_counter_offsets(struct net_device *dev)
2268 struct rtl8169_private *tp = netdev_priv(dev);
2269 struct rtl8169_counters *counters = tp->counters;
2273 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2274 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2275 * reset by a power cycle, while the counter values collected by the
2276 * driver are reset at every driver unload/load cycle.
2278 * To make sure the HW values returned by @get_stats64 match the SW
2279 * values, we collect the initial values at first open(*) and use them
2280 * as offsets to normalize the values returned by @get_stats64.
2282 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2283 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2284 * set at open time by rtl_hw_start.
2287 if (tp->tc_offset.inited)
2290 /* If both, reset and update fail, propagate to caller. */
2291 if (rtl8169_reset_counters(dev))
2294 if (rtl8169_update_counters(dev))
2297 tp->tc_offset.tx_errors = counters->tx_errors;
2298 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2299 tp->tc_offset.tx_aborted = counters->tx_aborted;
2300 tp->tc_offset.inited = true;
2305 static void rtl8169_get_ethtool_stats(struct net_device *dev,
2306 struct ethtool_stats *stats, u64 *data)
2308 struct rtl8169_private *tp = netdev_priv(dev);
2309 struct device *d = &tp->pci_dev->dev;
2310 struct rtl8169_counters *counters = tp->counters;
2314 pm_runtime_get_noresume(d);
2316 if (pm_runtime_active(d))
2317 rtl8169_update_counters(dev);
2319 pm_runtime_put_noidle(d);
2321 data[0] = le64_to_cpu(counters->tx_packets);
2322 data[1] = le64_to_cpu(counters->rx_packets);
2323 data[2] = le64_to_cpu(counters->tx_errors);
2324 data[3] = le32_to_cpu(counters->rx_errors);
2325 data[4] = le16_to_cpu(counters->rx_missed);
2326 data[5] = le16_to_cpu(counters->align_errors);
2327 data[6] = le32_to_cpu(counters->tx_one_collision);
2328 data[7] = le32_to_cpu(counters->tx_multi_collision);
2329 data[8] = le64_to_cpu(counters->rx_unicast);
2330 data[9] = le64_to_cpu(counters->rx_broadcast);
2331 data[10] = le32_to_cpu(counters->rx_multicast);
2332 data[11] = le16_to_cpu(counters->tx_aborted);
2333 data[12] = le16_to_cpu(counters->tx_underun);
2336 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2340 memcpy(data, rtl8169_gstrings, sizeof(rtl8169_gstrings));
2345 static int rtl8169_nway_reset(struct net_device *dev)
2347 struct rtl8169_private *tp = netdev_priv(dev);
2349 return mii_nway_restart(&tp->mii);
2352 static const struct ethtool_ops rtl8169_ethtool_ops = {
2353 .get_drvinfo = rtl8169_get_drvinfo,
2354 .get_regs_len = rtl8169_get_regs_len,
2355 .get_link = ethtool_op_get_link,
2356 .get_msglevel = rtl8169_get_msglevel,
2357 .set_msglevel = rtl8169_set_msglevel,
2358 .get_regs = rtl8169_get_regs,
2359 .get_wol = rtl8169_get_wol,
2360 .set_wol = rtl8169_set_wol,
2361 .get_strings = rtl8169_get_strings,
2362 .get_sset_count = rtl8169_get_sset_count,
2363 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2364 .get_ts_info = ethtool_op_get_ts_info,
2365 .nway_reset = rtl8169_nway_reset,
2366 .get_link_ksettings = rtl8169_get_link_ksettings,
2367 .set_link_ksettings = rtl8169_set_link_ksettings,
2370 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2371 struct net_device *dev, u8 default_version)
2373 void __iomem *ioaddr = tp->mmio_addr;
2375 * The driver currently handles the 8168Bf and the 8168Be identically
2376 * but they can be identified more specifically through the test below
2379 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2381 * Same thing for the 8101Eb and the 8101Ec:
2383 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2385 static const struct rtl_mac_info {
2390 /* 8168EP family. */
2391 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2392 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2393 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2396 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2397 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2400 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2401 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2402 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2403 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2406 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2407 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2408 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2411 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2412 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2413 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2414 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2417 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2418 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2419 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2421 /* 8168DP family. */
2422 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2423 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2424 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2427 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
2428 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2429 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2430 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2431 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2432 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2433 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2434 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
2435 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2438 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2439 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2440 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2441 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2444 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2445 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2446 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2447 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
2448 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2449 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2450 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2451 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2452 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2453 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2454 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2455 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2456 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2457 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2458 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2459 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2460 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2461 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2462 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2463 /* FIXME: where did these entries come from ? -- FR */
2464 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2465 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2468 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2469 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2470 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2471 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2472 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2473 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2476 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2478 const struct rtl_mac_info *p = mac_info;
2481 reg = RTL_R32(TxConfig);
2482 while ((reg & p->mask) != p->val)
2484 tp->mac_version = p->mac_version;
2486 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2487 netif_notice(tp, probe, dev,
2488 "unknown MAC, using family default\n");
2489 tp->mac_version = default_version;
2490 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2491 tp->mac_version = tp->mii.supports_gmii ?
2492 RTL_GIGA_MAC_VER_42 :
2493 RTL_GIGA_MAC_VER_43;
2494 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2495 tp->mac_version = tp->mii.supports_gmii ?
2496 RTL_GIGA_MAC_VER_45 :
2497 RTL_GIGA_MAC_VER_47;
2498 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2499 tp->mac_version = tp->mii.supports_gmii ?
2500 RTL_GIGA_MAC_VER_46 :
2501 RTL_GIGA_MAC_VER_48;
2505 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2507 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2515 static void rtl_writephy_batch(struct rtl8169_private *tp,
2516 const struct phy_reg *regs, int len)
2519 rtl_writephy(tp, regs->reg, regs->val);
2524 #define PHY_READ 0x00000000
2525 #define PHY_DATA_OR 0x10000000
2526 #define PHY_DATA_AND 0x20000000
2527 #define PHY_BJMPN 0x30000000
2528 #define PHY_MDIO_CHG 0x40000000
2529 #define PHY_CLEAR_READCOUNT 0x70000000
2530 #define PHY_WRITE 0x80000000
2531 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2532 #define PHY_COMP_EQ_SKIPN 0xa0000000
2533 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2534 #define PHY_WRITE_PREVIOUS 0xc0000000
2535 #define PHY_SKIPN 0xd0000000
2536 #define PHY_DELAY_MS 0xe0000000
2540 char version[RTL_VER_SIZE];
2546 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2548 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2550 const struct firmware *fw = rtl_fw->fw;
2551 struct fw_info *fw_info = (struct fw_info *)fw->data;
2552 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2553 char *version = rtl_fw->version;
2556 if (fw->size < FW_OPCODE_SIZE)
2559 if (!fw_info->magic) {
2560 size_t i, size, start;
2563 if (fw->size < sizeof(*fw_info))
2566 for (i = 0; i < fw->size; i++)
2567 checksum += fw->data[i];
2571 start = le32_to_cpu(fw_info->fw_start);
2572 if (start > fw->size)
2575 size = le32_to_cpu(fw_info->fw_len);
2576 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2579 memcpy(version, fw_info->version, RTL_VER_SIZE);
2581 pa->code = (__le32 *)(fw->data + start);
2584 if (fw->size % FW_OPCODE_SIZE)
2587 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2589 pa->code = (__le32 *)fw->data;
2590 pa->size = fw->size / FW_OPCODE_SIZE;
2592 version[RTL_VER_SIZE - 1] = 0;
2599 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2600 struct rtl_fw_phy_action *pa)
2605 for (index = 0; index < pa->size; index++) {
2606 u32 action = le32_to_cpu(pa->code[index]);
2607 u32 regno = (action & 0x0fff0000) >> 16;
2609 switch(action & 0xf0000000) {
2614 case PHY_CLEAR_READCOUNT:
2616 case PHY_WRITE_PREVIOUS:
2621 if (regno > index) {
2622 netif_err(tp, ifup, tp->dev,
2623 "Out of range of firmware\n");
2627 case PHY_READCOUNT_EQ_SKIP:
2628 if (index + 2 >= pa->size) {
2629 netif_err(tp, ifup, tp->dev,
2630 "Out of range of firmware\n");
2634 case PHY_COMP_EQ_SKIPN:
2635 case PHY_COMP_NEQ_SKIPN:
2637 if (index + 1 + regno >= pa->size) {
2638 netif_err(tp, ifup, tp->dev,
2639 "Out of range of firmware\n");
2645 netif_err(tp, ifup, tp->dev,
2646 "Invalid action 0x%08x\n", action);
2655 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2657 struct net_device *dev = tp->dev;
2660 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2661 netif_err(tp, ifup, dev, "invalid firmware\n");
2665 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2671 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2673 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2674 struct mdio_ops org, *ops = &tp->mdio_ops;
2678 predata = count = 0;
2679 org.write = ops->write;
2680 org.read = ops->read;
2682 for (index = 0; index < pa->size; ) {
2683 u32 action = le32_to_cpu(pa->code[index]);
2684 u32 data = action & 0x0000ffff;
2685 u32 regno = (action & 0x0fff0000) >> 16;
2690 switch(action & 0xf0000000) {
2692 predata = rtl_readphy(tp, regno);
2709 ops->write = org.write;
2710 ops->read = org.read;
2711 } else if (data == 1) {
2712 ops->write = mac_mcu_write;
2713 ops->read = mac_mcu_read;
2718 case PHY_CLEAR_READCOUNT:
2723 rtl_writephy(tp, regno, data);
2726 case PHY_READCOUNT_EQ_SKIP:
2727 index += (count == data) ? 2 : 1;
2729 case PHY_COMP_EQ_SKIPN:
2730 if (predata == data)
2734 case PHY_COMP_NEQ_SKIPN:
2735 if (predata != data)
2739 case PHY_WRITE_PREVIOUS:
2740 rtl_writephy(tp, regno, predata);
2756 ops->write = org.write;
2757 ops->read = org.read;
2760 static void rtl_release_firmware(struct rtl8169_private *tp)
2762 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2763 release_firmware(tp->rtl_fw->fw);
2766 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2769 static void rtl_apply_firmware(struct rtl8169_private *tp)
2771 struct rtl_fw *rtl_fw = tp->rtl_fw;
2773 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2774 if (!IS_ERR_OR_NULL(rtl_fw))
2775 rtl_phy_write_fw(tp, rtl_fw);
2778 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2780 if (rtl_readphy(tp, reg) != val)
2781 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2783 rtl_apply_firmware(tp);
2786 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2788 static const struct phy_reg phy_reg_init[] = {
2850 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2853 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2855 static const struct phy_reg phy_reg_init[] = {
2861 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2864 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2866 struct pci_dev *pdev = tp->pci_dev;
2868 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2869 (pdev->subsystem_device != 0xe000))
2872 rtl_writephy(tp, 0x1f, 0x0001);
2873 rtl_writephy(tp, 0x10, 0xf01b);
2874 rtl_writephy(tp, 0x1f, 0x0000);
2877 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2879 static const struct phy_reg phy_reg_init[] = {
2919 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2921 rtl8169scd_hw_phy_config_quirk(tp);
2924 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2926 static const struct phy_reg phy_reg_init[] = {
2974 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2977 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2979 static const struct phy_reg phy_reg_init[] = {
2984 rtl_writephy(tp, 0x1f, 0x0001);
2985 rtl_patchphy(tp, 0x16, 1 << 0);
2987 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2990 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2992 static const struct phy_reg phy_reg_init[] = {
2998 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3001 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
3003 static const struct phy_reg phy_reg_init[] = {
3011 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3014 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
3016 static const struct phy_reg phy_reg_init[] = {
3022 rtl_writephy(tp, 0x1f, 0x0000);
3023 rtl_patchphy(tp, 0x14, 1 << 5);
3024 rtl_patchphy(tp, 0x0d, 1 << 5);
3026 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3029 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
3031 static const struct phy_reg phy_reg_init[] = {
3051 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3053 rtl_patchphy(tp, 0x14, 1 << 5);
3054 rtl_patchphy(tp, 0x0d, 1 << 5);
3055 rtl_writephy(tp, 0x1f, 0x0000);
3058 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
3060 static const struct phy_reg phy_reg_init[] = {
3078 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3080 rtl_patchphy(tp, 0x16, 1 << 0);
3081 rtl_patchphy(tp, 0x14, 1 << 5);
3082 rtl_patchphy(tp, 0x0d, 1 << 5);
3083 rtl_writephy(tp, 0x1f, 0x0000);
3086 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
3088 static const struct phy_reg phy_reg_init[] = {
3100 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3102 rtl_patchphy(tp, 0x16, 1 << 0);
3103 rtl_patchphy(tp, 0x14, 1 << 5);
3104 rtl_patchphy(tp, 0x0d, 1 << 5);
3105 rtl_writephy(tp, 0x1f, 0x0000);
3108 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
3110 rtl8168c_3_hw_phy_config(tp);
3113 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
3115 static const struct phy_reg phy_reg_init_0[] = {
3116 /* Channel Estimation */
3137 * Enhance line driver power
3146 * Can not link to 1Gbps with bad cable
3147 * Decrease SNR threshold form 21.07dB to 19.04dB
3156 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3160 * Fine Tune Switching regulator parameter
3162 rtl_writephy(tp, 0x1f, 0x0002);
3163 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3164 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
3166 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3167 static const struct phy_reg phy_reg_init[] = {
3177 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3179 val = rtl_readphy(tp, 0x0d);
3181 if ((val & 0x00ff) != 0x006c) {
3182 static const u32 set[] = {
3183 0x0065, 0x0066, 0x0067, 0x0068,
3184 0x0069, 0x006a, 0x006b, 0x006c
3188 rtl_writephy(tp, 0x1f, 0x0002);
3191 for (i = 0; i < ARRAY_SIZE(set); i++)
3192 rtl_writephy(tp, 0x0d, val | set[i]);
3195 static const struct phy_reg phy_reg_init[] = {
3203 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3206 /* RSET couple improve */
3207 rtl_writephy(tp, 0x1f, 0x0002);
3208 rtl_patchphy(tp, 0x0d, 0x0300);
3209 rtl_patchphy(tp, 0x0f, 0x0010);
3211 /* Fine tune PLL performance */
3212 rtl_writephy(tp, 0x1f, 0x0002);
3213 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3214 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3216 rtl_writephy(tp, 0x1f, 0x0005);
3217 rtl_writephy(tp, 0x05, 0x001b);
3219 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
3221 rtl_writephy(tp, 0x1f, 0x0000);
3224 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
3226 static const struct phy_reg phy_reg_init_0[] = {
3227 /* Channel Estimation */
3248 * Enhance line driver power
3257 * Can not link to 1Gbps with bad cable
3258 * Decrease SNR threshold form 21.07dB to 19.04dB
3267 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3269 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3270 static const struct phy_reg phy_reg_init[] = {
3281 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3283 val = rtl_readphy(tp, 0x0d);
3284 if ((val & 0x00ff) != 0x006c) {
3285 static const u32 set[] = {
3286 0x0065, 0x0066, 0x0067, 0x0068,
3287 0x0069, 0x006a, 0x006b, 0x006c
3291 rtl_writephy(tp, 0x1f, 0x0002);
3294 for (i = 0; i < ARRAY_SIZE(set); i++)
3295 rtl_writephy(tp, 0x0d, val | set[i]);
3298 static const struct phy_reg phy_reg_init[] = {
3306 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3309 /* Fine tune PLL performance */
3310 rtl_writephy(tp, 0x1f, 0x0002);
3311 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3312 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3314 /* Switching regulator Slew rate */
3315 rtl_writephy(tp, 0x1f, 0x0002);
3316 rtl_patchphy(tp, 0x0f, 0x0017);
3318 rtl_writephy(tp, 0x1f, 0x0005);
3319 rtl_writephy(tp, 0x05, 0x001b);
3321 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
3323 rtl_writephy(tp, 0x1f, 0x0000);
3326 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
3328 static const struct phy_reg phy_reg_init[] = {
3384 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3387 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3389 static const struct phy_reg phy_reg_init[] = {
3399 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3400 rtl_patchphy(tp, 0x0d, 1 << 5);
3403 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3405 static const struct phy_reg phy_reg_init[] = {
3406 /* Enable Delay cap */
3412 /* Channel estimation fine tune */
3421 /* Update PFM & 10M TX idle timer */
3433 rtl_apply_firmware(tp);
3435 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3437 /* DCO enable for 10M IDLE Power */
3438 rtl_writephy(tp, 0x1f, 0x0007);
3439 rtl_writephy(tp, 0x1e, 0x0023);
3440 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3441 rtl_writephy(tp, 0x1f, 0x0000);
3443 /* For impedance matching */
3444 rtl_writephy(tp, 0x1f, 0x0002);
3445 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3446 rtl_writephy(tp, 0x1f, 0x0000);
3448 /* PHY auto speed down */
3449 rtl_writephy(tp, 0x1f, 0x0007);
3450 rtl_writephy(tp, 0x1e, 0x002d);
3451 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3452 rtl_writephy(tp, 0x1f, 0x0000);
3453 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3455 rtl_writephy(tp, 0x1f, 0x0005);
3456 rtl_writephy(tp, 0x05, 0x8b86);
3457 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3458 rtl_writephy(tp, 0x1f, 0x0000);
3460 rtl_writephy(tp, 0x1f, 0x0005);
3461 rtl_writephy(tp, 0x05, 0x8b85);
3462 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3463 rtl_writephy(tp, 0x1f, 0x0007);
3464 rtl_writephy(tp, 0x1e, 0x0020);
3465 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3466 rtl_writephy(tp, 0x1f, 0x0006);
3467 rtl_writephy(tp, 0x00, 0x5a00);
3468 rtl_writephy(tp, 0x1f, 0x0000);
3469 rtl_writephy(tp, 0x0d, 0x0007);
3470 rtl_writephy(tp, 0x0e, 0x003c);
3471 rtl_writephy(tp, 0x0d, 0x4007);
3472 rtl_writephy(tp, 0x0e, 0x0000);
3473 rtl_writephy(tp, 0x0d, 0x0000);
3476 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3479 addr[0] | (addr[1] << 8),
3480 addr[2] | (addr[3] << 8),
3481 addr[4] | (addr[5] << 8)
3483 const struct exgmac_reg e[] = {
3484 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3485 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3486 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3487 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3490 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3493 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3495 static const struct phy_reg phy_reg_init[] = {
3496 /* Enable Delay cap */
3505 /* Channel estimation fine tune */
3522 rtl_apply_firmware(tp);
3524 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3526 /* For 4-corner performance improve */
3527 rtl_writephy(tp, 0x1f, 0x0005);
3528 rtl_writephy(tp, 0x05, 0x8b80);
3529 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3530 rtl_writephy(tp, 0x1f, 0x0000);
3532 /* PHY auto speed down */
3533 rtl_writephy(tp, 0x1f, 0x0004);
3534 rtl_writephy(tp, 0x1f, 0x0007);
3535 rtl_writephy(tp, 0x1e, 0x002d);
3536 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3537 rtl_writephy(tp, 0x1f, 0x0002);
3538 rtl_writephy(tp, 0x1f, 0x0000);
3539 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3541 /* improve 10M EEE waveform */
3542 rtl_writephy(tp, 0x1f, 0x0005);
3543 rtl_writephy(tp, 0x05, 0x8b86);
3544 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3545 rtl_writephy(tp, 0x1f, 0x0000);
3547 /* Improve 2-pair detection performance */
3548 rtl_writephy(tp, 0x1f, 0x0005);
3549 rtl_writephy(tp, 0x05, 0x8b85);
3550 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3551 rtl_writephy(tp, 0x1f, 0x0000);
3554 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
3555 rtl_writephy(tp, 0x1f, 0x0005);
3556 rtl_writephy(tp, 0x05, 0x8b85);
3557 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3558 rtl_writephy(tp, 0x1f, 0x0004);
3559 rtl_writephy(tp, 0x1f, 0x0007);
3560 rtl_writephy(tp, 0x1e, 0x0020);
3561 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3562 rtl_writephy(tp, 0x1f, 0x0002);
3563 rtl_writephy(tp, 0x1f, 0x0000);
3564 rtl_writephy(tp, 0x0d, 0x0007);
3565 rtl_writephy(tp, 0x0e, 0x003c);
3566 rtl_writephy(tp, 0x0d, 0x4007);
3567 rtl_writephy(tp, 0x0e, 0x0000);
3568 rtl_writephy(tp, 0x0d, 0x0000);
3571 rtl_writephy(tp, 0x1f, 0x0003);
3572 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3573 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3574 rtl_writephy(tp, 0x1f, 0x0000);
3576 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3577 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3580 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3582 /* For 4-corner performance improve */
3583 rtl_writephy(tp, 0x1f, 0x0005);
3584 rtl_writephy(tp, 0x05, 0x8b80);
3585 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3586 rtl_writephy(tp, 0x1f, 0x0000);
3588 /* PHY auto speed down */
3589 rtl_writephy(tp, 0x1f, 0x0007);
3590 rtl_writephy(tp, 0x1e, 0x002d);
3591 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3592 rtl_writephy(tp, 0x1f, 0x0000);
3593 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3595 /* Improve 10M EEE waveform */
3596 rtl_writephy(tp, 0x1f, 0x0005);
3597 rtl_writephy(tp, 0x05, 0x8b86);
3598 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3599 rtl_writephy(tp, 0x1f, 0x0000);
3602 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3604 static const struct phy_reg phy_reg_init[] = {
3605 /* Channel estimation fine tune */
3610 /* Modify green table for giga & fnet */
3627 /* Modify green table for 10M */
3633 /* Disable hiimpedance detection (RTCT) */
3639 rtl_apply_firmware(tp);
3641 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3643 rtl8168f_hw_phy_config(tp);
3645 /* Improve 2-pair detection performance */
3646 rtl_writephy(tp, 0x1f, 0x0005);
3647 rtl_writephy(tp, 0x05, 0x8b85);
3648 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3649 rtl_writephy(tp, 0x1f, 0x0000);
3652 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3654 rtl_apply_firmware(tp);
3656 rtl8168f_hw_phy_config(tp);
3659 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3661 static const struct phy_reg phy_reg_init[] = {
3662 /* Channel estimation fine tune */
3667 /* Modify green table for giga & fnet */
3684 /* Modify green table for 10M */
3690 /* Disable hiimpedance detection (RTCT) */
3697 rtl_apply_firmware(tp);
3699 rtl8168f_hw_phy_config(tp);
3701 /* Improve 2-pair detection performance */
3702 rtl_writephy(tp, 0x1f, 0x0005);
3703 rtl_writephy(tp, 0x05, 0x8b85);
3704 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3705 rtl_writephy(tp, 0x1f, 0x0000);
3707 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3709 /* Modify green table for giga */
3710 rtl_writephy(tp, 0x1f, 0x0005);
3711 rtl_writephy(tp, 0x05, 0x8b54);
3712 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3713 rtl_writephy(tp, 0x05, 0x8b5d);
3714 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3715 rtl_writephy(tp, 0x05, 0x8a7c);
3716 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3717 rtl_writephy(tp, 0x05, 0x8a7f);
3718 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3719 rtl_writephy(tp, 0x05, 0x8a82);
3720 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3721 rtl_writephy(tp, 0x05, 0x8a85);
3722 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3723 rtl_writephy(tp, 0x05, 0x8a88);
3724 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3725 rtl_writephy(tp, 0x1f, 0x0000);
3727 /* uc same-seed solution */
3728 rtl_writephy(tp, 0x1f, 0x0005);
3729 rtl_writephy(tp, 0x05, 0x8b85);
3730 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3731 rtl_writephy(tp, 0x1f, 0x0000);
3734 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3735 rtl_writephy(tp, 0x1f, 0x0005);
3736 rtl_writephy(tp, 0x05, 0x8b85);
3737 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3738 rtl_writephy(tp, 0x1f, 0x0004);
3739 rtl_writephy(tp, 0x1f, 0x0007);
3740 rtl_writephy(tp, 0x1e, 0x0020);
3741 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3742 rtl_writephy(tp, 0x1f, 0x0000);
3743 rtl_writephy(tp, 0x0d, 0x0007);
3744 rtl_writephy(tp, 0x0e, 0x003c);
3745 rtl_writephy(tp, 0x0d, 0x4007);
3746 rtl_writephy(tp, 0x0e, 0x0000);
3747 rtl_writephy(tp, 0x0d, 0x0000);
3750 rtl_writephy(tp, 0x1f, 0x0003);
3751 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3752 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3753 rtl_writephy(tp, 0x1f, 0x0000);
3756 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3758 rtl_apply_firmware(tp);
3760 rtl_writephy(tp, 0x1f, 0x0a46);
3761 if (rtl_readphy(tp, 0x10) & 0x0100) {
3762 rtl_writephy(tp, 0x1f, 0x0bcc);
3763 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3765 rtl_writephy(tp, 0x1f, 0x0bcc);
3766 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3769 rtl_writephy(tp, 0x1f, 0x0a46);
3770 if (rtl_readphy(tp, 0x13) & 0x0100) {
3771 rtl_writephy(tp, 0x1f, 0x0c41);
3772 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3774 rtl_writephy(tp, 0x1f, 0x0c41);
3775 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3778 /* Enable PHY auto speed down */
3779 rtl_writephy(tp, 0x1f, 0x0a44);
3780 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3782 rtl_writephy(tp, 0x1f, 0x0bcc);
3783 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
3784 rtl_writephy(tp, 0x1f, 0x0a44);
3785 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3786 rtl_writephy(tp, 0x1f, 0x0a43);
3787 rtl_writephy(tp, 0x13, 0x8084);
3788 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3789 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3791 /* EEE auto-fallback function */
3792 rtl_writephy(tp, 0x1f, 0x0a4b);
3793 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3795 /* Enable UC LPF tune function */
3796 rtl_writephy(tp, 0x1f, 0x0a43);
3797 rtl_writephy(tp, 0x13, 0x8012);
3798 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3800 rtl_writephy(tp, 0x1f, 0x0c42);
3801 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3803 /* Improve SWR Efficiency */
3804 rtl_writephy(tp, 0x1f, 0x0bcd);
3805 rtl_writephy(tp, 0x14, 0x5065);
3806 rtl_writephy(tp, 0x14, 0xd065);
3807 rtl_writephy(tp, 0x1f, 0x0bc8);
3808 rtl_writephy(tp, 0x11, 0x5655);
3809 rtl_writephy(tp, 0x1f, 0x0bcd);
3810 rtl_writephy(tp, 0x14, 0x1065);
3811 rtl_writephy(tp, 0x14, 0x9065);
3812 rtl_writephy(tp, 0x14, 0x1065);
3814 /* Check ALDPS bit, disable it if enabled */
3815 rtl_writephy(tp, 0x1f, 0x0a43);
3816 if (rtl_readphy(tp, 0x10) & 0x0004)
3817 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3819 rtl_writephy(tp, 0x1f, 0x0000);
3822 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3824 rtl_apply_firmware(tp);
3827 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3832 rtl_apply_firmware(tp);
3834 /* CHN EST parameters adjust - giga master */
3835 rtl_writephy(tp, 0x1f, 0x0a43);
3836 rtl_writephy(tp, 0x13, 0x809b);
3837 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3838 rtl_writephy(tp, 0x13, 0x80a2);
3839 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3840 rtl_writephy(tp, 0x13, 0x80a4);
3841 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3842 rtl_writephy(tp, 0x13, 0x809c);
3843 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3844 rtl_writephy(tp, 0x1f, 0x0000);
3846 /* CHN EST parameters adjust - giga slave */
3847 rtl_writephy(tp, 0x1f, 0x0a43);
3848 rtl_writephy(tp, 0x13, 0x80ad);
3849 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
3850 rtl_writephy(tp, 0x13, 0x80b4);
3851 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
3852 rtl_writephy(tp, 0x13, 0x80ac);
3853 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
3854 rtl_writephy(tp, 0x1f, 0x0000);
3856 /* CHN EST parameters adjust - fnet */
3857 rtl_writephy(tp, 0x1f, 0x0a43);
3858 rtl_writephy(tp, 0x13, 0x808e);
3859 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
3860 rtl_writephy(tp, 0x13, 0x8090);
3861 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
3862 rtl_writephy(tp, 0x13, 0x8092);
3863 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
3864 rtl_writephy(tp, 0x1f, 0x0000);
3866 /* enable R-tune & PGA-retune function */
3868 rtl_writephy(tp, 0x1f, 0x0a46);
3869 data = rtl_readphy(tp, 0x13);
3872 dout_tapbin |= data;
3873 data = rtl_readphy(tp, 0x12);
3876 dout_tapbin |= data;
3877 dout_tapbin = ~(dout_tapbin^0x08);
3879 dout_tapbin &= 0xf000;
3880 rtl_writephy(tp, 0x1f, 0x0a43);
3881 rtl_writephy(tp, 0x13, 0x827a);
3882 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3883 rtl_writephy(tp, 0x13, 0x827b);
3884 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3885 rtl_writephy(tp, 0x13, 0x827c);
3886 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3887 rtl_writephy(tp, 0x13, 0x827d);
3888 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3890 rtl_writephy(tp, 0x1f, 0x0a43);
3891 rtl_writephy(tp, 0x13, 0x0811);
3892 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3893 rtl_writephy(tp, 0x1f, 0x0a42);
3894 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3895 rtl_writephy(tp, 0x1f, 0x0000);
3897 /* enable GPHY 10M */
3898 rtl_writephy(tp, 0x1f, 0x0a44);
3899 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3900 rtl_writephy(tp, 0x1f, 0x0000);
3902 /* SAR ADC performance */
3903 rtl_writephy(tp, 0x1f, 0x0bca);
3904 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
3905 rtl_writephy(tp, 0x1f, 0x0000);
3907 rtl_writephy(tp, 0x1f, 0x0a43);
3908 rtl_writephy(tp, 0x13, 0x803f);
3909 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3910 rtl_writephy(tp, 0x13, 0x8047);
3911 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3912 rtl_writephy(tp, 0x13, 0x804f);
3913 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3914 rtl_writephy(tp, 0x13, 0x8057);
3915 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3916 rtl_writephy(tp, 0x13, 0x805f);
3917 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3918 rtl_writephy(tp, 0x13, 0x8067);
3919 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3920 rtl_writephy(tp, 0x13, 0x806f);
3921 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3922 rtl_writephy(tp, 0x1f, 0x0000);
3924 /* disable phy pfm mode */
3925 rtl_writephy(tp, 0x1f, 0x0a44);
3926 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3927 rtl_writephy(tp, 0x1f, 0x0000);
3929 /* Check ALDPS bit, disable it if enabled */
3930 rtl_writephy(tp, 0x1f, 0x0a43);
3931 if (rtl_readphy(tp, 0x10) & 0x0004)
3932 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3934 rtl_writephy(tp, 0x1f, 0x0000);
3937 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3939 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3943 rtl_apply_firmware(tp);
3945 /* CHIN EST parameter update */
3946 rtl_writephy(tp, 0x1f, 0x0a43);
3947 rtl_writephy(tp, 0x13, 0x808a);
3948 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
3949 rtl_writephy(tp, 0x1f, 0x0000);
3951 /* enable R-tune & PGA-retune function */
3952 rtl_writephy(tp, 0x1f, 0x0a43);
3953 rtl_writephy(tp, 0x13, 0x0811);
3954 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3955 rtl_writephy(tp, 0x1f, 0x0a42);
3956 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3957 rtl_writephy(tp, 0x1f, 0x0000);
3959 /* enable GPHY 10M */
3960 rtl_writephy(tp, 0x1f, 0x0a44);
3961 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3962 rtl_writephy(tp, 0x1f, 0x0000);
3964 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3965 data = r8168_mac_ocp_read(tp, 0xdd02);
3966 ioffset_p3 = ((data & 0x80)>>7);
3969 data = r8168_mac_ocp_read(tp, 0xdd00);
3970 ioffset_p3 |= ((data & (0xe000))>>13);
3971 ioffset_p2 = ((data & (0x1e00))>>9);
3972 ioffset_p1 = ((data & (0x01e0))>>5);
3973 ioffset_p0 = ((data & 0x0010)>>4);
3975 ioffset_p0 |= (data & (0x07));
3976 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3978 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3979 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
3980 rtl_writephy(tp, 0x1f, 0x0bcf);
3981 rtl_writephy(tp, 0x16, data);
3982 rtl_writephy(tp, 0x1f, 0x0000);
3985 /* Modify rlen (TX LPF corner frequency) level */
3986 rtl_writephy(tp, 0x1f, 0x0bcd);
3987 data = rtl_readphy(tp, 0x16);
3992 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3993 rtl_writephy(tp, 0x17, data);
3994 rtl_writephy(tp, 0x1f, 0x0bcd);
3995 rtl_writephy(tp, 0x1f, 0x0000);
3997 /* disable phy pfm mode */
3998 rtl_writephy(tp, 0x1f, 0x0a44);
3999 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
4000 rtl_writephy(tp, 0x1f, 0x0000);
4002 /* Check ALDPS bit, disable it if enabled */
4003 rtl_writephy(tp, 0x1f, 0x0a43);
4004 if (rtl_readphy(tp, 0x10) & 0x0004)
4005 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4007 rtl_writephy(tp, 0x1f, 0x0000);
4010 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4012 /* Enable PHY auto speed down */
4013 rtl_writephy(tp, 0x1f, 0x0a44);
4014 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4015 rtl_writephy(tp, 0x1f, 0x0000);
4017 /* patch 10M & ALDPS */
4018 rtl_writephy(tp, 0x1f, 0x0bcc);
4019 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4020 rtl_writephy(tp, 0x1f, 0x0a44);
4021 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4022 rtl_writephy(tp, 0x1f, 0x0a43);
4023 rtl_writephy(tp, 0x13, 0x8084);
4024 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4025 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4026 rtl_writephy(tp, 0x1f, 0x0000);
4028 /* Enable EEE auto-fallback function */
4029 rtl_writephy(tp, 0x1f, 0x0a4b);
4030 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4031 rtl_writephy(tp, 0x1f, 0x0000);
4033 /* Enable UC LPF tune function */
4034 rtl_writephy(tp, 0x1f, 0x0a43);
4035 rtl_writephy(tp, 0x13, 0x8012);
4036 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4037 rtl_writephy(tp, 0x1f, 0x0000);
4039 /* set rg_sel_sdm_rate */
4040 rtl_writephy(tp, 0x1f, 0x0c42);
4041 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4042 rtl_writephy(tp, 0x1f, 0x0000);
4044 /* Check ALDPS bit, disable it if enabled */
4045 rtl_writephy(tp, 0x1f, 0x0a43);
4046 if (rtl_readphy(tp, 0x10) & 0x0004)
4047 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4049 rtl_writephy(tp, 0x1f, 0x0000);
4052 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4054 /* patch 10M & ALDPS */
4055 rtl_writephy(tp, 0x1f, 0x0bcc);
4056 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4057 rtl_writephy(tp, 0x1f, 0x0a44);
4058 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4059 rtl_writephy(tp, 0x1f, 0x0a43);
4060 rtl_writephy(tp, 0x13, 0x8084);
4061 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4062 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4063 rtl_writephy(tp, 0x1f, 0x0000);
4065 /* Enable UC LPF tune function */
4066 rtl_writephy(tp, 0x1f, 0x0a43);
4067 rtl_writephy(tp, 0x13, 0x8012);
4068 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4069 rtl_writephy(tp, 0x1f, 0x0000);
4071 /* Set rg_sel_sdm_rate */
4072 rtl_writephy(tp, 0x1f, 0x0c42);
4073 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4074 rtl_writephy(tp, 0x1f, 0x0000);
4076 /* Channel estimation parameters */
4077 rtl_writephy(tp, 0x1f, 0x0a43);
4078 rtl_writephy(tp, 0x13, 0x80f3);
4079 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4080 rtl_writephy(tp, 0x13, 0x80f0);
4081 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4082 rtl_writephy(tp, 0x13, 0x80ef);
4083 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4084 rtl_writephy(tp, 0x13, 0x80f6);
4085 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4086 rtl_writephy(tp, 0x13, 0x80ec);
4087 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4088 rtl_writephy(tp, 0x13, 0x80ed);
4089 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4090 rtl_writephy(tp, 0x13, 0x80f2);
4091 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4092 rtl_writephy(tp, 0x13, 0x80f4);
4093 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4094 rtl_writephy(tp, 0x1f, 0x0a43);
4095 rtl_writephy(tp, 0x13, 0x8110);
4096 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4097 rtl_writephy(tp, 0x13, 0x810f);
4098 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4099 rtl_writephy(tp, 0x13, 0x8111);
4100 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4101 rtl_writephy(tp, 0x13, 0x8113);
4102 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4103 rtl_writephy(tp, 0x13, 0x8115);
4104 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4105 rtl_writephy(tp, 0x13, 0x810e);
4106 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4107 rtl_writephy(tp, 0x13, 0x810c);
4108 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4109 rtl_writephy(tp, 0x13, 0x810b);
4110 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4111 rtl_writephy(tp, 0x1f, 0x0a43);
4112 rtl_writephy(tp, 0x13, 0x80d1);
4113 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4114 rtl_writephy(tp, 0x13, 0x80cd);
4115 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4116 rtl_writephy(tp, 0x13, 0x80d3);
4117 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4118 rtl_writephy(tp, 0x13, 0x80d5);
4119 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4120 rtl_writephy(tp, 0x13, 0x80d7);
4121 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4123 /* Force PWM-mode */
4124 rtl_writephy(tp, 0x1f, 0x0bcd);
4125 rtl_writephy(tp, 0x14, 0x5065);
4126 rtl_writephy(tp, 0x14, 0xd065);
4127 rtl_writephy(tp, 0x1f, 0x0bc8);
4128 rtl_writephy(tp, 0x12, 0x00ed);
4129 rtl_writephy(tp, 0x1f, 0x0bcd);
4130 rtl_writephy(tp, 0x14, 0x1065);
4131 rtl_writephy(tp, 0x14, 0x9065);
4132 rtl_writephy(tp, 0x14, 0x1065);
4133 rtl_writephy(tp, 0x1f, 0x0000);
4135 /* Check ALDPS bit, disable it if enabled */
4136 rtl_writephy(tp, 0x1f, 0x0a43);
4137 if (rtl_readphy(tp, 0x10) & 0x0004)
4138 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4140 rtl_writephy(tp, 0x1f, 0x0000);
4143 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
4145 static const struct phy_reg phy_reg_init[] = {
4152 rtl_writephy(tp, 0x1f, 0x0000);
4153 rtl_patchphy(tp, 0x11, 1 << 12);
4154 rtl_patchphy(tp, 0x19, 1 << 13);
4155 rtl_patchphy(tp, 0x10, 1 << 15);
4157 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4160 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4162 static const struct phy_reg phy_reg_init[] = {
4176 /* Disable ALDPS before ram code */
4177 rtl_writephy(tp, 0x1f, 0x0000);
4178 rtl_writephy(tp, 0x18, 0x0310);
4181 rtl_apply_firmware(tp);
4183 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4186 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4188 /* Disable ALDPS before setting firmware */
4189 rtl_writephy(tp, 0x1f, 0x0000);
4190 rtl_writephy(tp, 0x18, 0x0310);
4193 rtl_apply_firmware(tp);
4196 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4197 rtl_writephy(tp, 0x1f, 0x0004);
4198 rtl_writephy(tp, 0x10, 0x401f);
4199 rtl_writephy(tp, 0x19, 0x7030);
4200 rtl_writephy(tp, 0x1f, 0x0000);
4203 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4205 static const struct phy_reg phy_reg_init[] = {
4212 /* Disable ALDPS before ram code */
4213 rtl_writephy(tp, 0x1f, 0x0000);
4214 rtl_writephy(tp, 0x18, 0x0310);
4217 rtl_apply_firmware(tp);
4219 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4220 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4222 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4225 static void rtl_hw_phy_config(struct net_device *dev)
4227 struct rtl8169_private *tp = netdev_priv(dev);
4229 rtl8169_print_mac_version(tp);
4231 switch (tp->mac_version) {
4232 case RTL_GIGA_MAC_VER_01:
4234 case RTL_GIGA_MAC_VER_02:
4235 case RTL_GIGA_MAC_VER_03:
4236 rtl8169s_hw_phy_config(tp);
4238 case RTL_GIGA_MAC_VER_04:
4239 rtl8169sb_hw_phy_config(tp);
4241 case RTL_GIGA_MAC_VER_05:
4242 rtl8169scd_hw_phy_config(tp);
4244 case RTL_GIGA_MAC_VER_06:
4245 rtl8169sce_hw_phy_config(tp);
4247 case RTL_GIGA_MAC_VER_07:
4248 case RTL_GIGA_MAC_VER_08:
4249 case RTL_GIGA_MAC_VER_09:
4250 rtl8102e_hw_phy_config(tp);
4252 case RTL_GIGA_MAC_VER_11:
4253 rtl8168bb_hw_phy_config(tp);
4255 case RTL_GIGA_MAC_VER_12:
4256 rtl8168bef_hw_phy_config(tp);
4258 case RTL_GIGA_MAC_VER_17:
4259 rtl8168bef_hw_phy_config(tp);
4261 case RTL_GIGA_MAC_VER_18:
4262 rtl8168cp_1_hw_phy_config(tp);
4264 case RTL_GIGA_MAC_VER_19:
4265 rtl8168c_1_hw_phy_config(tp);
4267 case RTL_GIGA_MAC_VER_20:
4268 rtl8168c_2_hw_phy_config(tp);
4270 case RTL_GIGA_MAC_VER_21:
4271 rtl8168c_3_hw_phy_config(tp);
4273 case RTL_GIGA_MAC_VER_22:
4274 rtl8168c_4_hw_phy_config(tp);
4276 case RTL_GIGA_MAC_VER_23:
4277 case RTL_GIGA_MAC_VER_24:
4278 rtl8168cp_2_hw_phy_config(tp);
4280 case RTL_GIGA_MAC_VER_25:
4281 rtl8168d_1_hw_phy_config(tp);
4283 case RTL_GIGA_MAC_VER_26:
4284 rtl8168d_2_hw_phy_config(tp);
4286 case RTL_GIGA_MAC_VER_27:
4287 rtl8168d_3_hw_phy_config(tp);
4289 case RTL_GIGA_MAC_VER_28:
4290 rtl8168d_4_hw_phy_config(tp);
4292 case RTL_GIGA_MAC_VER_29:
4293 case RTL_GIGA_MAC_VER_30:
4294 rtl8105e_hw_phy_config(tp);
4296 case RTL_GIGA_MAC_VER_31:
4299 case RTL_GIGA_MAC_VER_32:
4300 case RTL_GIGA_MAC_VER_33:
4301 rtl8168e_1_hw_phy_config(tp);
4303 case RTL_GIGA_MAC_VER_34:
4304 rtl8168e_2_hw_phy_config(tp);
4306 case RTL_GIGA_MAC_VER_35:
4307 rtl8168f_1_hw_phy_config(tp);
4309 case RTL_GIGA_MAC_VER_36:
4310 rtl8168f_2_hw_phy_config(tp);
4313 case RTL_GIGA_MAC_VER_37:
4314 rtl8402_hw_phy_config(tp);
4317 case RTL_GIGA_MAC_VER_38:
4318 rtl8411_hw_phy_config(tp);
4321 case RTL_GIGA_MAC_VER_39:
4322 rtl8106e_hw_phy_config(tp);
4325 case RTL_GIGA_MAC_VER_40:
4326 rtl8168g_1_hw_phy_config(tp);
4328 case RTL_GIGA_MAC_VER_42:
4329 case RTL_GIGA_MAC_VER_43:
4330 case RTL_GIGA_MAC_VER_44:
4331 rtl8168g_2_hw_phy_config(tp);
4333 case RTL_GIGA_MAC_VER_45:
4334 case RTL_GIGA_MAC_VER_47:
4335 rtl8168h_1_hw_phy_config(tp);
4337 case RTL_GIGA_MAC_VER_46:
4338 case RTL_GIGA_MAC_VER_48:
4339 rtl8168h_2_hw_phy_config(tp);
4342 case RTL_GIGA_MAC_VER_49:
4343 rtl8168ep_1_hw_phy_config(tp);
4345 case RTL_GIGA_MAC_VER_50:
4346 case RTL_GIGA_MAC_VER_51:
4347 rtl8168ep_2_hw_phy_config(tp);
4350 case RTL_GIGA_MAC_VER_41:
4356 static void rtl_phy_work(struct rtl8169_private *tp)
4358 struct timer_list *timer = &tp->timer;
4359 void __iomem *ioaddr = tp->mmio_addr;
4360 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4362 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
4364 if (tp->phy_reset_pending(tp)) {
4366 * A busy loop could burn quite a few cycles on nowadays CPU.
4367 * Let's delay the execution of the timer for a few ticks.
4373 if (tp->link_ok(ioaddr))
4376 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
4378 tp->phy_reset_enable(tp);
4381 mod_timer(timer, jiffies + timeout);
4384 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4386 if (!test_and_set_bit(flag, tp->wk.flags))
4387 schedule_work(&tp->wk.work);
4390 static void rtl8169_phy_timer(unsigned long __opaque)
4392 struct net_device *dev = (struct net_device *)__opaque;
4393 struct rtl8169_private *tp = netdev_priv(dev);
4395 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
4398 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
4399 void __iomem *ioaddr)
4402 pci_release_regions(pdev);
4403 pci_clear_mwi(pdev);
4404 pci_disable_device(pdev);
4408 DECLARE_RTL_COND(rtl_phy_reset_cond)
4410 return tp->phy_reset_pending(tp);
4413 static void rtl8169_phy_reset(struct net_device *dev,
4414 struct rtl8169_private *tp)
4416 tp->phy_reset_enable(tp);
4417 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
4420 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4422 void __iomem *ioaddr = tp->mmio_addr;
4424 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4425 (RTL_R8(PHYstatus) & TBI_Enable);
4428 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4430 void __iomem *ioaddr = tp->mmio_addr;
4432 rtl_hw_phy_config(dev);
4434 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4435 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4439 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4441 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4442 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4444 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
4445 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4447 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
4448 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
4451 rtl8169_phy_reset(dev, tp);
4453 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4454 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4455 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4456 (tp->mii.supports_gmii ?
4457 ADVERTISED_1000baseT_Half |
4458 ADVERTISED_1000baseT_Full : 0));
4460 if (rtl_tbi_enabled(tp))
4461 netif_info(tp, link, dev, "TBI auto-negotiating\n");
4464 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4466 void __iomem *ioaddr = tp->mmio_addr;
4470 RTL_W8(Cfg9346, Cfg9346_Unlock);
4472 RTL_W32(MAC4, addr[4] | addr[5] << 8);
4475 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4478 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4479 rtl_rar_exgmac_set(tp, addr);
4481 RTL_W8(Cfg9346, Cfg9346_Lock);
4483 rtl_unlock_work(tp);
4486 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4488 void __iomem *ioaddr = tp->mmio_addr;
4490 switch (tp->mac_version) {
4491 case RTL_GIGA_MAC_VER_01:
4492 case RTL_GIGA_MAC_VER_02:
4493 case RTL_GIGA_MAC_VER_03:
4494 case RTL_GIGA_MAC_VER_04:
4495 case RTL_GIGA_MAC_VER_05:
4496 case RTL_GIGA_MAC_VER_06:
4497 case RTL_GIGA_MAC_VER_10:
4498 case RTL_GIGA_MAC_VER_11:
4499 case RTL_GIGA_MAC_VER_12:
4500 case RTL_GIGA_MAC_VER_13:
4501 case RTL_GIGA_MAC_VER_14:
4502 case RTL_GIGA_MAC_VER_15:
4503 case RTL_GIGA_MAC_VER_16:
4504 case RTL_GIGA_MAC_VER_17:
4505 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4507 case RTL_GIGA_MAC_VER_18:
4508 case RTL_GIGA_MAC_VER_19:
4509 case RTL_GIGA_MAC_VER_20:
4510 case RTL_GIGA_MAC_VER_21:
4511 case RTL_GIGA_MAC_VER_22:
4512 case RTL_GIGA_MAC_VER_23:
4513 case RTL_GIGA_MAC_VER_24:
4514 case RTL_GIGA_MAC_VER_34:
4515 case RTL_GIGA_MAC_VER_35:
4516 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4518 case RTL_GIGA_MAC_VER_40:
4519 case RTL_GIGA_MAC_VER_41:
4520 case RTL_GIGA_MAC_VER_42:
4521 case RTL_GIGA_MAC_VER_43:
4522 case RTL_GIGA_MAC_VER_44:
4523 case RTL_GIGA_MAC_VER_45:
4524 case RTL_GIGA_MAC_VER_46:
4525 case RTL_GIGA_MAC_VER_47:
4526 case RTL_GIGA_MAC_VER_48:
4527 case RTL_GIGA_MAC_VER_49:
4528 case RTL_GIGA_MAC_VER_50:
4529 case RTL_GIGA_MAC_VER_51:
4530 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4533 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4538 static int rtl_set_mac_address(struct net_device *dev, void *p)
4540 struct rtl8169_private *tp = netdev_priv(dev);
4541 struct device *d = &tp->pci_dev->dev;
4542 struct sockaddr *addr = p;
4544 if (!is_valid_ether_addr(addr->sa_data))
4545 return -EADDRNOTAVAIL;
4547 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4549 pm_runtime_get_noresume(d);
4551 if (pm_runtime_active(d))
4552 rtl_rar_set(tp, dev->dev_addr);
4554 pm_runtime_put_noidle(d);
4556 /* Reportedly at least Asus X453MA truncates packets otherwise */
4557 if (tp->mac_version == RTL_GIGA_MAC_VER_37)
4563 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4565 struct rtl8169_private *tp = netdev_priv(dev);
4566 struct mii_ioctl_data *data = if_mii(ifr);
4568 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4571 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4572 struct mii_ioctl_data *data, int cmd)
4576 data->phy_id = 32; /* Internal PHY */
4580 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
4584 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
4590 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4595 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
4597 if (tp->features & RTL_FEATURE_MSI) {
4598 pci_disable_msi(pdev);
4599 tp->features &= ~RTL_FEATURE_MSI;
4603 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4605 struct mdio_ops *ops = &tp->mdio_ops;
4607 switch (tp->mac_version) {
4608 case RTL_GIGA_MAC_VER_27:
4609 ops->write = r8168dp_1_mdio_write;
4610 ops->read = r8168dp_1_mdio_read;
4612 case RTL_GIGA_MAC_VER_28:
4613 case RTL_GIGA_MAC_VER_31:
4614 ops->write = r8168dp_2_mdio_write;
4615 ops->read = r8168dp_2_mdio_read;
4617 case RTL_GIGA_MAC_VER_40:
4618 case RTL_GIGA_MAC_VER_41:
4619 case RTL_GIGA_MAC_VER_42:
4620 case RTL_GIGA_MAC_VER_43:
4621 case RTL_GIGA_MAC_VER_44:
4622 case RTL_GIGA_MAC_VER_45:
4623 case RTL_GIGA_MAC_VER_46:
4624 case RTL_GIGA_MAC_VER_47:
4625 case RTL_GIGA_MAC_VER_48:
4626 case RTL_GIGA_MAC_VER_49:
4627 case RTL_GIGA_MAC_VER_50:
4628 case RTL_GIGA_MAC_VER_51:
4629 ops->write = r8168g_mdio_write;
4630 ops->read = r8168g_mdio_read;
4633 ops->write = r8169_mdio_write;
4634 ops->read = r8169_mdio_read;
4639 static void rtl_speed_down(struct rtl8169_private *tp)
4644 rtl_writephy(tp, 0x1f, 0x0000);
4645 lpa = rtl_readphy(tp, MII_LPA);
4647 if (lpa & (LPA_10HALF | LPA_10FULL))
4648 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4649 else if (lpa & (LPA_100HALF | LPA_100FULL))
4650 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4651 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4653 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4654 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4655 (tp->mii.supports_gmii ?
4656 ADVERTISED_1000baseT_Half |
4657 ADVERTISED_1000baseT_Full : 0);
4659 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4663 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4665 void __iomem *ioaddr = tp->mmio_addr;
4667 switch (tp->mac_version) {
4668 case RTL_GIGA_MAC_VER_25:
4669 case RTL_GIGA_MAC_VER_26:
4670 case RTL_GIGA_MAC_VER_29:
4671 case RTL_GIGA_MAC_VER_30:
4672 case RTL_GIGA_MAC_VER_32:
4673 case RTL_GIGA_MAC_VER_33:
4674 case RTL_GIGA_MAC_VER_34:
4675 case RTL_GIGA_MAC_VER_37:
4676 case RTL_GIGA_MAC_VER_38:
4677 case RTL_GIGA_MAC_VER_39:
4678 case RTL_GIGA_MAC_VER_40:
4679 case RTL_GIGA_MAC_VER_41:
4680 case RTL_GIGA_MAC_VER_42:
4681 case RTL_GIGA_MAC_VER_43:
4682 case RTL_GIGA_MAC_VER_44:
4683 case RTL_GIGA_MAC_VER_45:
4684 case RTL_GIGA_MAC_VER_46:
4685 case RTL_GIGA_MAC_VER_47:
4686 case RTL_GIGA_MAC_VER_48:
4687 case RTL_GIGA_MAC_VER_49:
4688 case RTL_GIGA_MAC_VER_50:
4689 case RTL_GIGA_MAC_VER_51:
4690 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4691 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4698 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4700 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4704 rtl_wol_suspend_quirk(tp);
4709 static void r810x_phy_power_down(struct rtl8169_private *tp)
4711 rtl_writephy(tp, 0x1f, 0x0000);
4712 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4715 static void r810x_phy_power_up(struct rtl8169_private *tp)
4717 rtl_writephy(tp, 0x1f, 0x0000);
4718 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4721 static void r810x_pll_power_down(struct rtl8169_private *tp)
4723 void __iomem *ioaddr = tp->mmio_addr;
4725 if (rtl_wol_pll_power_down(tp))
4728 r810x_phy_power_down(tp);
4730 switch (tp->mac_version) {
4731 case RTL_GIGA_MAC_VER_07:
4732 case RTL_GIGA_MAC_VER_08:
4733 case RTL_GIGA_MAC_VER_09:
4734 case RTL_GIGA_MAC_VER_10:
4735 case RTL_GIGA_MAC_VER_13:
4736 case RTL_GIGA_MAC_VER_16:
4739 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4744 static void r810x_pll_power_up(struct rtl8169_private *tp)
4746 void __iomem *ioaddr = tp->mmio_addr;
4748 r810x_phy_power_up(tp);
4750 switch (tp->mac_version) {
4751 case RTL_GIGA_MAC_VER_07:
4752 case RTL_GIGA_MAC_VER_08:
4753 case RTL_GIGA_MAC_VER_09:
4754 case RTL_GIGA_MAC_VER_10:
4755 case RTL_GIGA_MAC_VER_13:
4756 case RTL_GIGA_MAC_VER_16:
4758 case RTL_GIGA_MAC_VER_47:
4759 case RTL_GIGA_MAC_VER_48:
4760 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
4763 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4768 static void r8168_phy_power_up(struct rtl8169_private *tp)
4770 rtl_writephy(tp, 0x1f, 0x0000);
4771 switch (tp->mac_version) {
4772 case RTL_GIGA_MAC_VER_11:
4773 case RTL_GIGA_MAC_VER_12:
4774 case RTL_GIGA_MAC_VER_17:
4775 case RTL_GIGA_MAC_VER_18:
4776 case RTL_GIGA_MAC_VER_19:
4777 case RTL_GIGA_MAC_VER_20:
4778 case RTL_GIGA_MAC_VER_21:
4779 case RTL_GIGA_MAC_VER_22:
4780 case RTL_GIGA_MAC_VER_23:
4781 case RTL_GIGA_MAC_VER_24:
4782 case RTL_GIGA_MAC_VER_25:
4783 case RTL_GIGA_MAC_VER_26:
4784 case RTL_GIGA_MAC_VER_27:
4785 case RTL_GIGA_MAC_VER_28:
4786 case RTL_GIGA_MAC_VER_31:
4787 rtl_writephy(tp, 0x0e, 0x0000);
4792 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4795 static void r8168_phy_power_down(struct rtl8169_private *tp)
4797 rtl_writephy(tp, 0x1f, 0x0000);
4798 switch (tp->mac_version) {
4799 case RTL_GIGA_MAC_VER_32:
4800 case RTL_GIGA_MAC_VER_33:
4801 case RTL_GIGA_MAC_VER_40:
4802 case RTL_GIGA_MAC_VER_41:
4803 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4806 case RTL_GIGA_MAC_VER_11:
4807 case RTL_GIGA_MAC_VER_12:
4808 case RTL_GIGA_MAC_VER_17:
4809 case RTL_GIGA_MAC_VER_18:
4810 case RTL_GIGA_MAC_VER_19:
4811 case RTL_GIGA_MAC_VER_20:
4812 case RTL_GIGA_MAC_VER_21:
4813 case RTL_GIGA_MAC_VER_22:
4814 case RTL_GIGA_MAC_VER_23:
4815 case RTL_GIGA_MAC_VER_24:
4816 case RTL_GIGA_MAC_VER_25:
4817 case RTL_GIGA_MAC_VER_26:
4818 case RTL_GIGA_MAC_VER_27:
4819 case RTL_GIGA_MAC_VER_28:
4820 case RTL_GIGA_MAC_VER_31:
4821 rtl_writephy(tp, 0x0e, 0x0200);
4823 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4828 static void r8168_pll_power_down(struct rtl8169_private *tp)
4830 void __iomem *ioaddr = tp->mmio_addr;
4832 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4833 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4834 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
4835 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
4836 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
4837 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
4838 r8168_check_dash(tp)) {
4842 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4843 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
4844 (RTL_R16(CPlusCmd) & ASF)) {
4848 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4849 tp->mac_version == RTL_GIGA_MAC_VER_33)
4850 rtl_ephy_write(tp, 0x19, 0xff64);
4852 if (rtl_wol_pll_power_down(tp))
4855 r8168_phy_power_down(tp);
4857 switch (tp->mac_version) {
4858 case RTL_GIGA_MAC_VER_25:
4859 case RTL_GIGA_MAC_VER_26:
4860 case RTL_GIGA_MAC_VER_27:
4861 case RTL_GIGA_MAC_VER_28:
4862 case RTL_GIGA_MAC_VER_31:
4863 case RTL_GIGA_MAC_VER_32:
4864 case RTL_GIGA_MAC_VER_33:
4865 case RTL_GIGA_MAC_VER_44:
4866 case RTL_GIGA_MAC_VER_45:
4867 case RTL_GIGA_MAC_VER_46:
4868 case RTL_GIGA_MAC_VER_50:
4869 case RTL_GIGA_MAC_VER_51:
4870 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4872 case RTL_GIGA_MAC_VER_40:
4873 case RTL_GIGA_MAC_VER_41:
4874 case RTL_GIGA_MAC_VER_49:
4875 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4876 0xfc000000, ERIAR_EXGMAC);
4877 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4882 static void r8168_pll_power_up(struct rtl8169_private *tp)
4884 void __iomem *ioaddr = tp->mmio_addr;
4886 switch (tp->mac_version) {
4887 case RTL_GIGA_MAC_VER_25:
4888 case RTL_GIGA_MAC_VER_26:
4889 case RTL_GIGA_MAC_VER_27:
4890 case RTL_GIGA_MAC_VER_28:
4891 case RTL_GIGA_MAC_VER_31:
4892 case RTL_GIGA_MAC_VER_32:
4893 case RTL_GIGA_MAC_VER_33:
4894 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4896 case RTL_GIGA_MAC_VER_44:
4897 case RTL_GIGA_MAC_VER_45:
4898 case RTL_GIGA_MAC_VER_46:
4899 case RTL_GIGA_MAC_VER_50:
4900 case RTL_GIGA_MAC_VER_51:
4901 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
4903 case RTL_GIGA_MAC_VER_40:
4904 case RTL_GIGA_MAC_VER_41:
4905 case RTL_GIGA_MAC_VER_49:
4906 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
4907 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4908 0x00000000, ERIAR_EXGMAC);
4912 r8168_phy_power_up(tp);
4915 static void rtl_generic_op(struct rtl8169_private *tp,
4916 void (*op)(struct rtl8169_private *))
4922 static void rtl_pll_power_down(struct rtl8169_private *tp)
4924 rtl_generic_op(tp, tp->pll_power_ops.down);
4927 static void rtl_pll_power_up(struct rtl8169_private *tp)
4929 rtl_generic_op(tp, tp->pll_power_ops.up);
4931 /* give MAC/PHY some time to resume */
4935 static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
4937 struct pll_power_ops *ops = &tp->pll_power_ops;
4939 switch (tp->mac_version) {
4940 case RTL_GIGA_MAC_VER_07:
4941 case RTL_GIGA_MAC_VER_08:
4942 case RTL_GIGA_MAC_VER_09:
4943 case RTL_GIGA_MAC_VER_10:
4944 case RTL_GIGA_MAC_VER_16:
4945 case RTL_GIGA_MAC_VER_29:
4946 case RTL_GIGA_MAC_VER_30:
4947 case RTL_GIGA_MAC_VER_37:
4948 case RTL_GIGA_MAC_VER_39:
4949 case RTL_GIGA_MAC_VER_43:
4950 case RTL_GIGA_MAC_VER_47:
4951 case RTL_GIGA_MAC_VER_48:
4952 ops->down = r810x_pll_power_down;
4953 ops->up = r810x_pll_power_up;
4956 case RTL_GIGA_MAC_VER_11:
4957 case RTL_GIGA_MAC_VER_12:
4958 case RTL_GIGA_MAC_VER_17:
4959 case RTL_GIGA_MAC_VER_18:
4960 case RTL_GIGA_MAC_VER_19:
4961 case RTL_GIGA_MAC_VER_20:
4962 case RTL_GIGA_MAC_VER_21:
4963 case RTL_GIGA_MAC_VER_22:
4964 case RTL_GIGA_MAC_VER_23:
4965 case RTL_GIGA_MAC_VER_24:
4966 case RTL_GIGA_MAC_VER_25:
4967 case RTL_GIGA_MAC_VER_26:
4968 case RTL_GIGA_MAC_VER_27:
4969 case RTL_GIGA_MAC_VER_28:
4970 case RTL_GIGA_MAC_VER_31:
4971 case RTL_GIGA_MAC_VER_32:
4972 case RTL_GIGA_MAC_VER_33:
4973 case RTL_GIGA_MAC_VER_34:
4974 case RTL_GIGA_MAC_VER_35:
4975 case RTL_GIGA_MAC_VER_36:
4976 case RTL_GIGA_MAC_VER_38:
4977 case RTL_GIGA_MAC_VER_40:
4978 case RTL_GIGA_MAC_VER_41:
4979 case RTL_GIGA_MAC_VER_42:
4980 case RTL_GIGA_MAC_VER_44:
4981 case RTL_GIGA_MAC_VER_45:
4982 case RTL_GIGA_MAC_VER_46:
4983 case RTL_GIGA_MAC_VER_49:
4984 case RTL_GIGA_MAC_VER_50:
4985 case RTL_GIGA_MAC_VER_51:
4986 ops->down = r8168_pll_power_down;
4987 ops->up = r8168_pll_power_up;
4997 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4999 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
5002 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5004 void __iomem *ioaddr = tp->mmio_addr;
5006 RTL_W8(Cfg9346, Cfg9346_Unlock);
5007 rtl_generic_op(tp, tp->jumbo_ops.enable);
5008 RTL_W8(Cfg9346, Cfg9346_Lock);
5011 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5013 void __iomem *ioaddr = tp->mmio_addr;
5015 RTL_W8(Cfg9346, Cfg9346_Unlock);
5016 rtl_generic_op(tp, tp->jumbo_ops.disable);
5017 RTL_W8(Cfg9346, Cfg9346_Lock);
5020 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5022 void __iomem *ioaddr = tp->mmio_addr;
5024 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5025 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
5026 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
5029 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5031 void __iomem *ioaddr = tp->mmio_addr;
5033 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5034 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5035 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5038 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5040 void __iomem *ioaddr = tp->mmio_addr;
5042 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5045 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5047 void __iomem *ioaddr = tp->mmio_addr;
5049 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5052 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5054 void __iomem *ioaddr = tp->mmio_addr;
5056 RTL_W8(MaxTxPacketSize, 0x3f);
5057 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5058 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
5059 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
5062 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5064 void __iomem *ioaddr = tp->mmio_addr;
5066 RTL_W8(MaxTxPacketSize, 0x0c);
5067 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5068 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
5069 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5072 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5074 rtl_tx_performance_tweak(tp->pci_dev,
5075 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
5078 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5080 rtl_tx_performance_tweak(tp->pci_dev,
5081 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5084 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5086 void __iomem *ioaddr = tp->mmio_addr;
5088 r8168b_0_hw_jumbo_enable(tp);
5090 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5093 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5095 void __iomem *ioaddr = tp->mmio_addr;
5097 r8168b_0_hw_jumbo_disable(tp);
5099 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5102 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
5104 struct jumbo_ops *ops = &tp->jumbo_ops;
5106 switch (tp->mac_version) {
5107 case RTL_GIGA_MAC_VER_11:
5108 ops->disable = r8168b_0_hw_jumbo_disable;
5109 ops->enable = r8168b_0_hw_jumbo_enable;
5111 case RTL_GIGA_MAC_VER_12:
5112 case RTL_GIGA_MAC_VER_17:
5113 ops->disable = r8168b_1_hw_jumbo_disable;
5114 ops->enable = r8168b_1_hw_jumbo_enable;
5116 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5117 case RTL_GIGA_MAC_VER_19:
5118 case RTL_GIGA_MAC_VER_20:
5119 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5120 case RTL_GIGA_MAC_VER_22:
5121 case RTL_GIGA_MAC_VER_23:
5122 case RTL_GIGA_MAC_VER_24:
5123 case RTL_GIGA_MAC_VER_25:
5124 case RTL_GIGA_MAC_VER_26:
5125 ops->disable = r8168c_hw_jumbo_disable;
5126 ops->enable = r8168c_hw_jumbo_enable;
5128 case RTL_GIGA_MAC_VER_27:
5129 case RTL_GIGA_MAC_VER_28:
5130 ops->disable = r8168dp_hw_jumbo_disable;
5131 ops->enable = r8168dp_hw_jumbo_enable;
5133 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5134 case RTL_GIGA_MAC_VER_32:
5135 case RTL_GIGA_MAC_VER_33:
5136 case RTL_GIGA_MAC_VER_34:
5137 ops->disable = r8168e_hw_jumbo_disable;
5138 ops->enable = r8168e_hw_jumbo_enable;
5142 * No action needed for jumbo frames with 8169.
5143 * No jumbo for 810x at all.
5145 case RTL_GIGA_MAC_VER_40:
5146 case RTL_GIGA_MAC_VER_41:
5147 case RTL_GIGA_MAC_VER_42:
5148 case RTL_GIGA_MAC_VER_43:
5149 case RTL_GIGA_MAC_VER_44:
5150 case RTL_GIGA_MAC_VER_45:
5151 case RTL_GIGA_MAC_VER_46:
5152 case RTL_GIGA_MAC_VER_47:
5153 case RTL_GIGA_MAC_VER_48:
5154 case RTL_GIGA_MAC_VER_49:
5155 case RTL_GIGA_MAC_VER_50:
5156 case RTL_GIGA_MAC_VER_51:
5158 ops->disable = NULL;
5164 DECLARE_RTL_COND(rtl_chipcmd_cond)
5166 void __iomem *ioaddr = tp->mmio_addr;
5168 return RTL_R8(ChipCmd) & CmdReset;
5171 static void rtl_hw_reset(struct rtl8169_private *tp)
5173 void __iomem *ioaddr = tp->mmio_addr;
5175 RTL_W8(ChipCmd, CmdReset);
5177 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
5180 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5182 struct rtl_fw *rtl_fw;
5186 name = rtl_lookup_firmware_name(tp);
5188 goto out_no_firmware;
5190 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5194 rc = reject_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5198 rc = rtl_check_firmware(tp, rtl_fw);
5200 goto err_release_firmware;
5202 tp->rtl_fw = rtl_fw;
5206 err_release_firmware:
5207 release_firmware(rtl_fw->fw);
5211 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5218 static void rtl_request_firmware(struct rtl8169_private *tp)
5220 if (IS_ERR(tp->rtl_fw))
5221 rtl_request_uncached_firmware(tp);
5224 static void rtl_rx_close(struct rtl8169_private *tp)
5226 void __iomem *ioaddr = tp->mmio_addr;
5228 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
5231 DECLARE_RTL_COND(rtl_npq_cond)
5233 void __iomem *ioaddr = tp->mmio_addr;
5235 return RTL_R8(TxPoll) & NPQ;
5238 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5240 void __iomem *ioaddr = tp->mmio_addr;
5242 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5245 static void rtl8169_hw_reset(struct rtl8169_private *tp)
5247 void __iomem *ioaddr = tp->mmio_addr;
5249 /* Disable interrupts */
5250 rtl8169_irq_mask_and_ack(tp);
5254 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5255 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5256 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5257 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
5258 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
5259 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5260 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5261 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5262 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5263 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5264 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5265 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5266 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5267 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5268 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5269 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5270 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
5271 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5272 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5273 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5274 tp->mac_version == RTL_GIGA_MAC_VER_51) {
5275 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5276 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
5278 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5285 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
5287 void __iomem *ioaddr = tp->mmio_addr;
5289 /* Set DMA burst size and Interframe Gap Time */
5290 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5291 (InterFrameGap << TxInterFrameGapShift));
5294 static void rtl_hw_start(struct net_device *dev)
5296 struct rtl8169_private *tp = netdev_priv(dev);
5300 rtl_irq_enable_all(tp);
5303 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5304 void __iomem *ioaddr)
5307 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5308 * register to be written before TxDescAddrLow to work.
5309 * Switching from MMIO to I/O access fixes the issue as well.
5311 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5312 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5313 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5314 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
5317 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5321 cmd = RTL_R16(CPlusCmd);
5322 RTL_W16(CPlusCmd, cmd);
5326 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
5328 /* Low hurts. Let's disable the filtering. */
5329 RTL_W16(RxMaxSize, rx_buf_sz + 1);
5332 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5334 static const struct rtl_cfg2_info {
5339 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5340 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5341 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5342 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
5344 const struct rtl_cfg2_info *p = cfg2_info;
5348 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
5349 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
5350 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5351 RTL_W32(0x7c, p->val);
5357 static void rtl_set_rx_mode(struct net_device *dev)
5359 struct rtl8169_private *tp = netdev_priv(dev);
5360 void __iomem *ioaddr = tp->mmio_addr;
5361 u32 mc_filter[2]; /* Multicast hash filter */
5365 if (dev->flags & IFF_PROMISC) {
5366 /* Unconditionally log net taps. */
5367 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5369 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5371 mc_filter[1] = mc_filter[0] = 0xffffffff;
5372 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5373 (dev->flags & IFF_ALLMULTI)) {
5374 /* Too many to filter perfectly -- accept all multicasts. */
5375 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5376 mc_filter[1] = mc_filter[0] = 0xffffffff;
5378 struct netdev_hw_addr *ha;
5380 rx_mode = AcceptBroadcast | AcceptMyPhys;
5381 mc_filter[1] = mc_filter[0] = 0;
5382 netdev_for_each_mc_addr(ha, dev) {
5383 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5384 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5385 rx_mode |= AcceptMulticast;
5389 if (dev->features & NETIF_F_RXALL)
5390 rx_mode |= (AcceptErr | AcceptRunt);
5392 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5394 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5395 u32 data = mc_filter[0];
5397 mc_filter[0] = swab32(mc_filter[1]);
5398 mc_filter[1] = swab32(data);
5401 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5402 mc_filter[1] = mc_filter[0] = 0xffffffff;
5404 RTL_W32(MAR0 + 4, mc_filter[1]);
5405 RTL_W32(MAR0 + 0, mc_filter[0]);
5407 RTL_W32(RxConfig, tmp);
5410 static void rtl_hw_start_8169(struct net_device *dev)
5412 struct rtl8169_private *tp = netdev_priv(dev);
5413 void __iomem *ioaddr = tp->mmio_addr;
5414 struct pci_dev *pdev = tp->pci_dev;
5416 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5417 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5418 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5421 RTL_W8(Cfg9346, Cfg9346_Unlock);
5422 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5423 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5424 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5425 tp->mac_version == RTL_GIGA_MAC_VER_04)
5426 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5430 RTL_W8(EarlyTxThres, NoEarlyTx);
5432 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5434 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5435 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5436 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5437 tp->mac_version == RTL_GIGA_MAC_VER_04)
5438 rtl_set_rx_tx_config_registers(tp);
5440 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
5442 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5443 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5444 dprintk("Set MAC Reg C+CR Offset 0xe0. "
5445 "Bit-3 and bit-14 MUST be 1\n");
5446 tp->cp_cmd |= (1 << 14);
5449 RTL_W16(CPlusCmd, tp->cp_cmd);
5451 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5454 * Undocumented corner. Supposedly:
5455 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5457 RTL_W16(IntrMitigate, 0x0000);
5459 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5461 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5462 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5463 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5464 tp->mac_version != RTL_GIGA_MAC_VER_04) {
5465 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5466 rtl_set_rx_tx_config_registers(tp);
5469 RTL_W8(Cfg9346, Cfg9346_Lock);
5471 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5474 RTL_W32(RxMissed, 0);
5476 rtl_set_rx_mode(dev);
5478 /* no early-rx interrupts */
5479 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5482 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5484 if (tp->csi_ops.write)
5485 tp->csi_ops.write(tp, addr, value);
5488 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5490 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
5493 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
5497 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5498 rtl_csi_write(tp, 0x070c, csi | bits);
5501 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
5503 rtl_csi_access_enable(tp, 0x17000000);
5506 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
5508 rtl_csi_access_enable(tp, 0x27000000);
5511 DECLARE_RTL_COND(rtl_csiar_cond)
5513 void __iomem *ioaddr = tp->mmio_addr;
5515 return RTL_R32(CSIAR) & CSIAR_FLAG;
5518 static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
5520 void __iomem *ioaddr = tp->mmio_addr;
5522 RTL_W32(CSIDR, value);
5523 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5524 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5526 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5529 static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
5531 void __iomem *ioaddr = tp->mmio_addr;
5533 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5534 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5536 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5537 RTL_R32(CSIDR) : ~0;
5540 static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
5542 void __iomem *ioaddr = tp->mmio_addr;
5544 RTL_W32(CSIDR, value);
5545 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5546 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5549 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5552 static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
5554 void __iomem *ioaddr = tp->mmio_addr;
5556 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5557 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5559 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5560 RTL_R32(CSIDR) : ~0;
5563 static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5565 void __iomem *ioaddr = tp->mmio_addr;
5567 RTL_W32(CSIDR, value);
5568 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5569 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5572 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5575 static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5577 void __iomem *ioaddr = tp->mmio_addr;
5579 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5580 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5582 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5583 RTL_R32(CSIDR) : ~0;
5586 static void rtl_init_csi_ops(struct rtl8169_private *tp)
5588 struct csi_ops *ops = &tp->csi_ops;
5590 switch (tp->mac_version) {
5591 case RTL_GIGA_MAC_VER_01:
5592 case RTL_GIGA_MAC_VER_02:
5593 case RTL_GIGA_MAC_VER_03:
5594 case RTL_GIGA_MAC_VER_04:
5595 case RTL_GIGA_MAC_VER_05:
5596 case RTL_GIGA_MAC_VER_06:
5597 case RTL_GIGA_MAC_VER_10:
5598 case RTL_GIGA_MAC_VER_11:
5599 case RTL_GIGA_MAC_VER_12:
5600 case RTL_GIGA_MAC_VER_13:
5601 case RTL_GIGA_MAC_VER_14:
5602 case RTL_GIGA_MAC_VER_15:
5603 case RTL_GIGA_MAC_VER_16:
5604 case RTL_GIGA_MAC_VER_17:
5609 case RTL_GIGA_MAC_VER_37:
5610 case RTL_GIGA_MAC_VER_38:
5611 ops->write = r8402_csi_write;
5612 ops->read = r8402_csi_read;
5615 case RTL_GIGA_MAC_VER_44:
5616 ops->write = r8411_csi_write;
5617 ops->read = r8411_csi_read;
5621 ops->write = r8169_csi_write;
5622 ops->read = r8169_csi_read;
5628 unsigned int offset;
5633 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5639 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5640 rtl_ephy_write(tp, e->offset, w);
5645 static void rtl_disable_clock_request(struct pci_dev *pdev)
5647 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5648 PCI_EXP_LNKCTL_CLKREQ_EN);
5651 static void rtl_enable_clock_request(struct pci_dev *pdev)
5653 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5654 PCI_EXP_LNKCTL_CLKREQ_EN);
5657 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5659 void __iomem *ioaddr = tp->mmio_addr;
5662 data = RTL_R8(Config3);
5667 data &= ~Rdy_to_L23;
5669 RTL_W8(Config3, data);
5672 #define R8168_CPCMD_QUIRK_MASK (\
5683 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
5685 void __iomem *ioaddr = tp->mmio_addr;
5686 struct pci_dev *pdev = tp->pci_dev;
5688 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5690 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5692 if (tp->dev->mtu <= ETH_DATA_LEN) {
5693 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5694 PCI_EXP_DEVCTL_NOSNOOP_EN);
5698 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
5700 void __iomem *ioaddr = tp->mmio_addr;
5702 rtl_hw_start_8168bb(tp);
5704 RTL_W8(MaxTxPacketSize, TxPacketMax);
5706 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5709 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
5711 void __iomem *ioaddr = tp->mmio_addr;
5712 struct pci_dev *pdev = tp->pci_dev;
5714 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5716 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5718 if (tp->dev->mtu <= ETH_DATA_LEN)
5719 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5721 rtl_disable_clock_request(pdev);
5723 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5726 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
5728 static const struct ephy_info e_info_8168cp[] = {
5729 { 0x01, 0, 0x0001 },
5730 { 0x02, 0x0800, 0x1000 },
5731 { 0x03, 0, 0x0042 },
5732 { 0x06, 0x0080, 0x0000 },
5736 rtl_csi_access_enable_2(tp);
5738 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
5740 __rtl_hw_start_8168cp(tp);
5743 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
5745 void __iomem *ioaddr = tp->mmio_addr;
5746 struct pci_dev *pdev = tp->pci_dev;
5748 rtl_csi_access_enable_2(tp);
5750 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5752 if (tp->dev->mtu <= ETH_DATA_LEN)
5753 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5755 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5758 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
5760 void __iomem *ioaddr = tp->mmio_addr;
5761 struct pci_dev *pdev = tp->pci_dev;
5763 rtl_csi_access_enable_2(tp);
5765 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5768 RTL_W8(DBG_REG, 0x20);
5770 RTL_W8(MaxTxPacketSize, TxPacketMax);
5772 if (tp->dev->mtu <= ETH_DATA_LEN)
5773 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5775 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5778 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
5780 void __iomem *ioaddr = tp->mmio_addr;
5781 static const struct ephy_info e_info_8168c_1[] = {
5782 { 0x02, 0x0800, 0x1000 },
5783 { 0x03, 0, 0x0002 },
5784 { 0x06, 0x0080, 0x0000 }
5787 rtl_csi_access_enable_2(tp);
5789 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5791 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
5793 __rtl_hw_start_8168cp(tp);
5796 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
5798 static const struct ephy_info e_info_8168c_2[] = {
5799 { 0x01, 0, 0x0001 },
5800 { 0x03, 0x0400, 0x0220 }
5803 rtl_csi_access_enable_2(tp);
5805 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
5807 __rtl_hw_start_8168cp(tp);
5810 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
5812 rtl_hw_start_8168c_2(tp);
5815 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
5817 rtl_csi_access_enable_2(tp);
5819 __rtl_hw_start_8168cp(tp);
5822 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
5824 void __iomem *ioaddr = tp->mmio_addr;
5825 struct pci_dev *pdev = tp->pci_dev;
5827 rtl_csi_access_enable_2(tp);
5829 rtl_disable_clock_request(pdev);
5831 RTL_W8(MaxTxPacketSize, TxPacketMax);
5833 if (tp->dev->mtu <= ETH_DATA_LEN)
5834 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5836 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5839 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
5841 void __iomem *ioaddr = tp->mmio_addr;
5842 struct pci_dev *pdev = tp->pci_dev;
5844 rtl_csi_access_enable_1(tp);
5846 if (tp->dev->mtu <= ETH_DATA_LEN)
5847 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5849 RTL_W8(MaxTxPacketSize, TxPacketMax);
5851 rtl_disable_clock_request(pdev);
5854 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
5856 void __iomem *ioaddr = tp->mmio_addr;
5857 struct pci_dev *pdev = tp->pci_dev;
5858 static const struct ephy_info e_info_8168d_4[] = {
5859 { 0x0b, 0x0000, 0x0048 },
5860 { 0x19, 0x0020, 0x0050 },
5861 { 0x0c, 0x0100, 0x0020 }
5864 rtl_csi_access_enable_1(tp);
5866 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5868 RTL_W8(MaxTxPacketSize, TxPacketMax);
5870 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
5872 rtl_enable_clock_request(pdev);
5875 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
5877 void __iomem *ioaddr = tp->mmio_addr;
5878 struct pci_dev *pdev = tp->pci_dev;
5879 static const struct ephy_info e_info_8168e_1[] = {
5880 { 0x00, 0x0200, 0x0100 },
5881 { 0x00, 0x0000, 0x0004 },
5882 { 0x06, 0x0002, 0x0001 },
5883 { 0x06, 0x0000, 0x0030 },
5884 { 0x07, 0x0000, 0x2000 },
5885 { 0x00, 0x0000, 0x0020 },
5886 { 0x03, 0x5800, 0x2000 },
5887 { 0x03, 0x0000, 0x0001 },
5888 { 0x01, 0x0800, 0x1000 },
5889 { 0x07, 0x0000, 0x4000 },
5890 { 0x1e, 0x0000, 0x2000 },
5891 { 0x19, 0xffff, 0xfe6c },
5892 { 0x0a, 0x0000, 0x0040 }
5895 rtl_csi_access_enable_2(tp);
5897 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5899 if (tp->dev->mtu <= ETH_DATA_LEN)
5900 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5902 RTL_W8(MaxTxPacketSize, TxPacketMax);
5904 rtl_disable_clock_request(pdev);
5906 /* Reset tx FIFO pointer */
5907 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5908 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
5910 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5913 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5915 void __iomem *ioaddr = tp->mmio_addr;
5916 struct pci_dev *pdev = tp->pci_dev;
5917 static const struct ephy_info e_info_8168e_2[] = {
5918 { 0x09, 0x0000, 0x0080 },
5919 { 0x19, 0x0000, 0x0224 }
5922 rtl_csi_access_enable_1(tp);
5924 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5926 if (tp->dev->mtu <= ETH_DATA_LEN)
5927 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5929 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5930 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5931 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5932 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5933 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5934 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5935 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5936 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5938 RTL_W8(MaxTxPacketSize, EarlySize);
5940 rtl_disable_clock_request(pdev);
5942 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5943 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5945 /* Adjust EEE LED frequency */
5946 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5948 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5949 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5950 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5953 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5955 void __iomem *ioaddr = tp->mmio_addr;
5956 struct pci_dev *pdev = tp->pci_dev;
5958 rtl_csi_access_enable_2(tp);
5960 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5962 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5963 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5964 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5965 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5966 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5967 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5968 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5969 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5970 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5971 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5973 RTL_W8(MaxTxPacketSize, EarlySize);
5975 rtl_disable_clock_request(pdev);
5977 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5978 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5979 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5980 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5981 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5984 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5986 void __iomem *ioaddr = tp->mmio_addr;
5987 static const struct ephy_info e_info_8168f_1[] = {
5988 { 0x06, 0x00c0, 0x0020 },
5989 { 0x08, 0x0001, 0x0002 },
5990 { 0x09, 0x0000, 0x0080 },
5991 { 0x19, 0x0000, 0x0224 }
5994 rtl_hw_start_8168f(tp);
5996 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5998 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
6000 /* Adjust EEE LED frequency */
6001 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6004 static void rtl_hw_start_8411(struct rtl8169_private *tp)
6006 static const struct ephy_info e_info_8168f_1[] = {
6007 { 0x06, 0x00c0, 0x0020 },
6008 { 0x0f, 0xffff, 0x5200 },
6009 { 0x1e, 0x0000, 0x4000 },
6010 { 0x19, 0x0000, 0x0224 }
6013 rtl_hw_start_8168f(tp);
6014 rtl_pcie_state_l2l3_enable(tp, false);
6016 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
6018 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
6021 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
6023 void __iomem *ioaddr = tp->mmio_addr;
6024 struct pci_dev *pdev = tp->pci_dev;
6026 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6028 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6029 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6030 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6031 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6033 rtl_csi_access_enable_1(tp);
6035 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6037 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6038 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6039 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
6041 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6042 RTL_W8(MaxTxPacketSize, EarlySize);
6044 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6045 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6047 /* Adjust EEE LED frequency */
6048 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6050 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6051 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6053 rtl_pcie_state_l2l3_enable(tp, false);
6056 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6058 void __iomem *ioaddr = tp->mmio_addr;
6059 static const struct ephy_info e_info_8168g_1[] = {
6060 { 0x00, 0x0000, 0x0008 },
6061 { 0x0c, 0x37d0, 0x0820 },
6062 { 0x1e, 0x0000, 0x0001 },
6063 { 0x19, 0x8000, 0x0000 }
6066 rtl_hw_start_8168g(tp);
6068 /* disable aspm and clock request before access ephy */
6069 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6070 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6071 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6074 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6076 void __iomem *ioaddr = tp->mmio_addr;
6077 static const struct ephy_info e_info_8168g_2[] = {
6078 { 0x00, 0x0000, 0x0008 },
6079 { 0x0c, 0x3df0, 0x0200 },
6080 { 0x19, 0xffff, 0xfc00 },
6081 { 0x1e, 0xffff, 0x20eb }
6084 rtl_hw_start_8168g(tp);
6086 /* disable aspm and clock request before access ephy */
6087 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6088 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6089 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6092 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6094 void __iomem *ioaddr = tp->mmio_addr;
6095 static const struct ephy_info e_info_8411_2[] = {
6096 { 0x00, 0x0000, 0x0008 },
6097 { 0x0c, 0x3df0, 0x0200 },
6098 { 0x0f, 0xffff, 0x5200 },
6099 { 0x19, 0x0020, 0x0000 },
6100 { 0x1e, 0x0000, 0x2000 }
6103 rtl_hw_start_8168g(tp);
6105 /* disable aspm and clock request before access ephy */
6106 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6107 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6108 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6111 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6113 void __iomem *ioaddr = tp->mmio_addr;
6114 struct pci_dev *pdev = tp->pci_dev;
6117 static const struct ephy_info e_info_8168h_1[] = {
6118 { 0x1e, 0x0800, 0x0001 },
6119 { 0x1d, 0x0000, 0x0800 },
6120 { 0x05, 0xffff, 0x2089 },
6121 { 0x06, 0xffff, 0x5881 },
6122 { 0x04, 0xffff, 0x154a },
6123 { 0x01, 0xffff, 0x068b }
6126 /* disable aspm and clock request before access ephy */
6127 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6128 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6129 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6131 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6133 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6134 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6135 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6136 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6138 rtl_csi_access_enable_1(tp);
6140 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6142 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6143 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6145 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
6147 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
6149 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6151 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6152 RTL_W8(MaxTxPacketSize, EarlySize);
6154 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6155 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6157 /* Adjust EEE LED frequency */
6158 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6160 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6161 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6163 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6165 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6167 rtl_pcie_state_l2l3_enable(tp, false);
6169 rtl_writephy(tp, 0x1f, 0x0c42);
6170 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
6171 rtl_writephy(tp, 0x1f, 0x0000);
6172 if (rg_saw_cnt > 0) {
6175 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6176 sw_cnt_1ms_ini &= 0x0fff;
6177 data = r8168_mac_ocp_read(tp, 0xd412);
6179 data |= sw_cnt_1ms_ini;
6180 r8168_mac_ocp_write(tp, 0xd412, data);
6183 data = r8168_mac_ocp_read(tp, 0xe056);
6186 r8168_mac_ocp_write(tp, 0xe056, data);
6188 data = r8168_mac_ocp_read(tp, 0xe052);
6191 r8168_mac_ocp_write(tp, 0xe052, data);
6193 data = r8168_mac_ocp_read(tp, 0xe0d6);
6196 r8168_mac_ocp_write(tp, 0xe0d6, data);
6198 data = r8168_mac_ocp_read(tp, 0xd420);
6201 r8168_mac_ocp_write(tp, 0xd420, data);
6203 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6204 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6205 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6206 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6209 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6211 void __iomem *ioaddr = tp->mmio_addr;
6212 struct pci_dev *pdev = tp->pci_dev;
6214 rtl8168ep_stop_cmac(tp);
6216 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6218 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6219 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6220 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6221 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6223 rtl_csi_access_enable_1(tp);
6225 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6227 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6228 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6230 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6232 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6234 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6235 RTL_W8(MaxTxPacketSize, EarlySize);
6237 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6238 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6240 /* Adjust EEE LED frequency */
6241 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6243 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6245 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6247 rtl_pcie_state_l2l3_enable(tp, false);
6250 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6252 void __iomem *ioaddr = tp->mmio_addr;
6253 static const struct ephy_info e_info_8168ep_1[] = {
6254 { 0x00, 0xffff, 0x10ab },
6255 { 0x06, 0xffff, 0xf030 },
6256 { 0x08, 0xffff, 0x2006 },
6257 { 0x0d, 0xffff, 0x1666 },
6258 { 0x0c, 0x3ff0, 0x0000 }
6261 /* disable aspm and clock request before access ephy */
6262 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6263 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6264 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6266 rtl_hw_start_8168ep(tp);
6269 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6271 void __iomem *ioaddr = tp->mmio_addr;
6272 static const struct ephy_info e_info_8168ep_2[] = {
6273 { 0x00, 0xffff, 0x10a3 },
6274 { 0x19, 0xffff, 0xfc00 },
6275 { 0x1e, 0xffff, 0x20ea }
6278 /* disable aspm and clock request before access ephy */
6279 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6280 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6281 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6283 rtl_hw_start_8168ep(tp);
6285 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6286 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6289 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6291 void __iomem *ioaddr = tp->mmio_addr;
6293 static const struct ephy_info e_info_8168ep_3[] = {
6294 { 0x00, 0xffff, 0x10a3 },
6295 { 0x19, 0xffff, 0x7c00 },
6296 { 0x1e, 0xffff, 0x20eb },
6297 { 0x0d, 0xffff, 0x1666 }
6300 /* disable aspm and clock request before access ephy */
6301 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6302 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6303 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6305 rtl_hw_start_8168ep(tp);
6307 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6308 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6310 data = r8168_mac_ocp_read(tp, 0xd3e2);
6313 r8168_mac_ocp_write(tp, 0xd3e2, data);
6315 data = r8168_mac_ocp_read(tp, 0xd3e4);
6317 r8168_mac_ocp_write(tp, 0xd3e4, data);
6319 data = r8168_mac_ocp_read(tp, 0xe860);
6321 r8168_mac_ocp_write(tp, 0xe860, data);
6324 static void rtl_hw_start_8168(struct net_device *dev)
6326 struct rtl8169_private *tp = netdev_priv(dev);
6327 void __iomem *ioaddr = tp->mmio_addr;
6329 RTL_W8(Cfg9346, Cfg9346_Unlock);
6331 RTL_W8(MaxTxPacketSize, TxPacketMax);
6333 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6335 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
6337 RTL_W16(CPlusCmd, tp->cp_cmd);
6339 RTL_W16(IntrMitigate, 0x5151);
6341 /* Work around for RxFIFO overflow. */
6342 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
6343 tp->event_slow |= RxFIFOOver | PCSTimeout;
6344 tp->event_slow &= ~RxOverflow;
6347 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6349 rtl_set_rx_tx_config_registers(tp);
6353 switch (tp->mac_version) {
6354 case RTL_GIGA_MAC_VER_11:
6355 rtl_hw_start_8168bb(tp);
6358 case RTL_GIGA_MAC_VER_12:
6359 case RTL_GIGA_MAC_VER_17:
6360 rtl_hw_start_8168bef(tp);
6363 case RTL_GIGA_MAC_VER_18:
6364 rtl_hw_start_8168cp_1(tp);
6367 case RTL_GIGA_MAC_VER_19:
6368 rtl_hw_start_8168c_1(tp);
6371 case RTL_GIGA_MAC_VER_20:
6372 rtl_hw_start_8168c_2(tp);
6375 case RTL_GIGA_MAC_VER_21:
6376 rtl_hw_start_8168c_3(tp);
6379 case RTL_GIGA_MAC_VER_22:
6380 rtl_hw_start_8168c_4(tp);
6383 case RTL_GIGA_MAC_VER_23:
6384 rtl_hw_start_8168cp_2(tp);
6387 case RTL_GIGA_MAC_VER_24:
6388 rtl_hw_start_8168cp_3(tp);
6391 case RTL_GIGA_MAC_VER_25:
6392 case RTL_GIGA_MAC_VER_26:
6393 case RTL_GIGA_MAC_VER_27:
6394 rtl_hw_start_8168d(tp);
6397 case RTL_GIGA_MAC_VER_28:
6398 rtl_hw_start_8168d_4(tp);
6401 case RTL_GIGA_MAC_VER_31:
6402 rtl_hw_start_8168dp(tp);
6405 case RTL_GIGA_MAC_VER_32:
6406 case RTL_GIGA_MAC_VER_33:
6407 rtl_hw_start_8168e_1(tp);
6409 case RTL_GIGA_MAC_VER_34:
6410 rtl_hw_start_8168e_2(tp);
6413 case RTL_GIGA_MAC_VER_35:
6414 case RTL_GIGA_MAC_VER_36:
6415 rtl_hw_start_8168f_1(tp);
6418 case RTL_GIGA_MAC_VER_38:
6419 rtl_hw_start_8411(tp);
6422 case RTL_GIGA_MAC_VER_40:
6423 case RTL_GIGA_MAC_VER_41:
6424 rtl_hw_start_8168g_1(tp);
6426 case RTL_GIGA_MAC_VER_42:
6427 rtl_hw_start_8168g_2(tp);
6430 case RTL_GIGA_MAC_VER_44:
6431 rtl_hw_start_8411_2(tp);
6434 case RTL_GIGA_MAC_VER_45:
6435 case RTL_GIGA_MAC_VER_46:
6436 rtl_hw_start_8168h_1(tp);
6439 case RTL_GIGA_MAC_VER_49:
6440 rtl_hw_start_8168ep_1(tp);
6443 case RTL_GIGA_MAC_VER_50:
6444 rtl_hw_start_8168ep_2(tp);
6447 case RTL_GIGA_MAC_VER_51:
6448 rtl_hw_start_8168ep_3(tp);
6452 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6453 dev->name, tp->mac_version);
6457 RTL_W8(Cfg9346, Cfg9346_Lock);
6459 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6461 rtl_set_rx_mode(dev);
6463 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
6466 #define R810X_CPCMD_QUIRK_MASK (\
6477 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
6479 void __iomem *ioaddr = tp->mmio_addr;
6480 struct pci_dev *pdev = tp->pci_dev;
6481 static const struct ephy_info e_info_8102e_1[] = {
6482 { 0x01, 0, 0x6e65 },
6483 { 0x02, 0, 0x091f },
6484 { 0x03, 0, 0xc2f9 },
6485 { 0x06, 0, 0xafb5 },
6486 { 0x07, 0, 0x0e00 },
6487 { 0x19, 0, 0xec80 },
6488 { 0x01, 0, 0x2e65 },
6493 rtl_csi_access_enable_2(tp);
6495 RTL_W8(DBG_REG, FIX_NAK_1);
6497 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6500 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6501 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6503 cfg1 = RTL_R8(Config1);
6504 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6505 RTL_W8(Config1, cfg1 & ~LEDS0);
6507 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
6510 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
6512 void __iomem *ioaddr = tp->mmio_addr;
6513 struct pci_dev *pdev = tp->pci_dev;
6515 rtl_csi_access_enable_2(tp);
6517 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6519 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6520 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6523 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
6525 rtl_hw_start_8102e_2(tp);
6527 rtl_ephy_write(tp, 0x03, 0xc2f9);
6530 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
6532 void __iomem *ioaddr = tp->mmio_addr;
6533 static const struct ephy_info e_info_8105e_1[] = {
6534 { 0x07, 0, 0x4000 },
6535 { 0x19, 0, 0x0200 },
6536 { 0x19, 0, 0x0020 },
6537 { 0x1e, 0, 0x2000 },
6538 { 0x03, 0, 0x0001 },
6539 { 0x19, 0, 0x0100 },
6540 { 0x19, 0, 0x0004 },
6544 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6545 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6547 /* Disable Early Tally Counter */
6548 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6550 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6551 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6553 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
6555 rtl_pcie_state_l2l3_enable(tp, false);
6558 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
6560 rtl_hw_start_8105e_1(tp);
6561 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
6564 static void rtl_hw_start_8402(struct rtl8169_private *tp)
6566 void __iomem *ioaddr = tp->mmio_addr;
6567 static const struct ephy_info e_info_8402[] = {
6568 { 0x19, 0xffff, 0xff64 },
6572 rtl_csi_access_enable_2(tp);
6574 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6575 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6577 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6578 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6580 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
6582 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6584 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6585 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
6586 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6587 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6588 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6589 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6590 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
6592 rtl_pcie_state_l2l3_enable(tp, false);
6595 static void rtl_hw_start_8106(struct rtl8169_private *tp)
6597 void __iomem *ioaddr = tp->mmio_addr;
6599 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6600 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6602 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6603 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6604 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6606 rtl_pcie_state_l2l3_enable(tp, false);
6609 static void rtl_hw_start_8101(struct net_device *dev)
6611 struct rtl8169_private *tp = netdev_priv(dev);
6612 void __iomem *ioaddr = tp->mmio_addr;
6613 struct pci_dev *pdev = tp->pci_dev;
6615 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6616 tp->event_slow &= ~RxFIFOOver;
6618 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
6619 tp->mac_version == RTL_GIGA_MAC_VER_16)
6620 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6621 PCI_EXP_DEVCTL_NOSNOOP_EN);
6623 RTL_W8(Cfg9346, Cfg9346_Unlock);
6625 RTL_W8(MaxTxPacketSize, TxPacketMax);
6627 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6629 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6630 RTL_W16(CPlusCmd, tp->cp_cmd);
6632 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6634 rtl_set_rx_tx_config_registers(tp);
6636 switch (tp->mac_version) {
6637 case RTL_GIGA_MAC_VER_07:
6638 rtl_hw_start_8102e_1(tp);
6641 case RTL_GIGA_MAC_VER_08:
6642 rtl_hw_start_8102e_3(tp);
6645 case RTL_GIGA_MAC_VER_09:
6646 rtl_hw_start_8102e_2(tp);
6649 case RTL_GIGA_MAC_VER_29:
6650 rtl_hw_start_8105e_1(tp);
6652 case RTL_GIGA_MAC_VER_30:
6653 rtl_hw_start_8105e_2(tp);
6656 case RTL_GIGA_MAC_VER_37:
6657 rtl_hw_start_8402(tp);
6660 case RTL_GIGA_MAC_VER_39:
6661 rtl_hw_start_8106(tp);
6663 case RTL_GIGA_MAC_VER_43:
6664 rtl_hw_start_8168g_2(tp);
6666 case RTL_GIGA_MAC_VER_47:
6667 case RTL_GIGA_MAC_VER_48:
6668 rtl_hw_start_8168h_1(tp);
6672 RTL_W8(Cfg9346, Cfg9346_Lock);
6674 RTL_W16(IntrMitigate, 0x0000);
6676 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6678 rtl_set_rx_mode(dev);
6682 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
6685 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6687 struct rtl8169_private *tp = netdev_priv(dev);
6689 if (new_mtu > ETH_DATA_LEN)
6690 rtl_hw_jumbo_enable(tp);
6692 rtl_hw_jumbo_disable(tp);
6695 netdev_update_features(dev);
6700 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6702 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
6703 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6706 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6707 void **data_buff, struct RxDesc *desc)
6709 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
6714 rtl8169_make_unusable_by_asic(desc);
6717 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6719 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6721 /* Force memory writes to complete before releasing descriptor */
6724 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6727 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6730 desc->addr = cpu_to_le64(mapping);
6731 rtl8169_mark_to_asic(desc, rx_buf_sz);
6734 static inline void *rtl8169_align(void *data)
6736 return (void *)ALIGN((long)data, 16);
6739 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6740 struct RxDesc *desc)
6744 struct device *d = &tp->pci_dev->dev;
6745 struct net_device *dev = tp->dev;
6746 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
6748 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6752 if (rtl8169_align(data) != data) {
6754 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6759 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
6761 if (unlikely(dma_mapping_error(d, mapping))) {
6762 if (net_ratelimit())
6763 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
6767 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
6775 static void rtl8169_rx_clear(struct rtl8169_private *tp)
6779 for (i = 0; i < NUM_RX_DESC; i++) {
6780 if (tp->Rx_databuff[i]) {
6781 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
6782 tp->RxDescArray + i);
6787 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
6789 desc->opts1 |= cpu_to_le32(RingEnd);
6792 static int rtl8169_rx_fill(struct rtl8169_private *tp)
6796 for (i = 0; i < NUM_RX_DESC; i++) {
6799 if (tp->Rx_databuff[i])
6802 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
6804 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
6807 tp->Rx_databuff[i] = data;
6810 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6814 rtl8169_rx_clear(tp);
6818 static int rtl8169_init_ring(struct net_device *dev)
6820 struct rtl8169_private *tp = netdev_priv(dev);
6822 rtl8169_init_ring_indexes(tp);
6824 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
6825 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
6827 return rtl8169_rx_fill(tp);
6830 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
6831 struct TxDesc *desc)
6833 unsigned int len = tx_skb->len;
6835 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6843 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6848 for (i = 0; i < n; i++) {
6849 unsigned int entry = (start + i) % NUM_TX_DESC;
6850 struct ring_info *tx_skb = tp->tx_skb + entry;
6851 unsigned int len = tx_skb->len;
6854 struct sk_buff *skb = tx_skb->skb;
6856 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
6857 tp->TxDescArray + entry);
6859 dev_consume_skb_any(skb);
6866 static void rtl8169_tx_clear(struct rtl8169_private *tp)
6868 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
6869 tp->cur_tx = tp->dirty_tx = 0;
6872 static void rtl_reset_work(struct rtl8169_private *tp)
6874 struct net_device *dev = tp->dev;
6877 napi_disable(&tp->napi);
6878 netif_stop_queue(dev);
6879 synchronize_sched();
6881 rtl8169_hw_reset(tp);
6883 for (i = 0; i < NUM_RX_DESC; i++)
6884 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6886 rtl8169_tx_clear(tp);
6887 rtl8169_init_ring_indexes(tp);
6889 napi_enable(&tp->napi);
6891 netif_wake_queue(dev);
6892 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
6895 static void rtl8169_tx_timeout(struct net_device *dev)
6897 struct rtl8169_private *tp = netdev_priv(dev);
6899 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6902 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
6905 struct skb_shared_info *info = skb_shinfo(skb);
6906 unsigned int cur_frag, entry;
6907 struct TxDesc *uninitialized_var(txd);
6908 struct device *d = &tp->pci_dev->dev;
6911 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
6912 const skb_frag_t *frag = info->frags + cur_frag;
6917 entry = (entry + 1) % NUM_TX_DESC;
6919 txd = tp->TxDescArray + entry;
6920 len = skb_frag_size(frag);
6921 addr = skb_frag_address(frag);
6922 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
6923 if (unlikely(dma_mapping_error(d, mapping))) {
6924 if (net_ratelimit())
6925 netif_err(tp, drv, tp->dev,
6926 "Failed to map TX fragments DMA!\n");
6930 /* Anti gcc 2.95.3 bugware (sic) */
6931 status = opts[0] | len |
6932 (RingEnd * !((entry + 1) % NUM_TX_DESC));
6934 txd->opts1 = cpu_to_le32(status);
6935 txd->opts2 = cpu_to_le32(opts[1]);
6936 txd->addr = cpu_to_le64(mapping);
6938 tp->tx_skb[entry].len = len;
6942 tp->tx_skb[entry].skb = skb;
6943 txd->opts1 |= cpu_to_le32(LastFrag);
6949 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6953 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6955 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6958 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6959 struct net_device *dev);
6960 /* r8169_csum_workaround()
6961 * The hw limites the value the transport offset. When the offset is out of the
6962 * range, calculate the checksum by sw.
6964 static void r8169_csum_workaround(struct rtl8169_private *tp,
6965 struct sk_buff *skb)
6967 if (skb_shinfo(skb)->gso_size) {
6968 netdev_features_t features = tp->dev->features;
6969 struct sk_buff *segs, *nskb;
6971 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6972 segs = skb_gso_segment(skb, features);
6973 if (IS_ERR(segs) || !segs)
6980 rtl8169_start_xmit(nskb, tp->dev);
6983 dev_consume_skb_any(skb);
6984 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6985 if (skb_checksum_help(skb) < 0)
6988 rtl8169_start_xmit(skb, tp->dev);
6990 struct net_device_stats *stats;
6993 stats = &tp->dev->stats;
6994 stats->tx_dropped++;
6995 dev_kfree_skb_any(skb);
6999 /* msdn_giant_send_check()
7000 * According to the document of microsoft, the TCP Pseudo Header excludes the
7001 * packet length for IPv6 TCP large packets.
7003 static int msdn_giant_send_check(struct sk_buff *skb)
7005 const struct ipv6hdr *ipv6h;
7009 ret = skb_cow_head(skb, 0);
7013 ipv6h = ipv6_hdr(skb);
7017 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7022 static inline __be16 get_protocol(struct sk_buff *skb)
7026 if (skb->protocol == htons(ETH_P_8021Q))
7027 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7029 protocol = skb->protocol;
7034 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7035 struct sk_buff *skb, u32 *opts)
7037 u32 mss = skb_shinfo(skb)->gso_size;
7041 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7042 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7043 const struct iphdr *ip = ip_hdr(skb);
7045 if (ip->protocol == IPPROTO_TCP)
7046 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7047 else if (ip->protocol == IPPROTO_UDP)
7048 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7056 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7057 struct sk_buff *skb, u32 *opts)
7059 u32 transport_offset = (u32)skb_transport_offset(skb);
7060 u32 mss = skb_shinfo(skb)->gso_size;
7063 if (transport_offset > GTTCPHO_MAX) {
7064 netif_warn(tp, tx_err, tp->dev,
7065 "Invalid transport offset 0x%x for TSO\n",
7070 switch (get_protocol(skb)) {
7071 case htons(ETH_P_IP):
7072 opts[0] |= TD1_GTSENV4;
7075 case htons(ETH_P_IPV6):
7076 if (msdn_giant_send_check(skb))
7079 opts[0] |= TD1_GTSENV6;
7087 opts[0] |= transport_offset << GTTCPHO_SHIFT;
7088 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
7089 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7092 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7093 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
7095 if (transport_offset > TCPHO_MAX) {
7096 netif_warn(tp, tx_err, tp->dev,
7097 "Invalid transport offset 0x%x\n",
7102 switch (get_protocol(skb)) {
7103 case htons(ETH_P_IP):
7104 opts[1] |= TD1_IPv4_CS;
7105 ip_protocol = ip_hdr(skb)->protocol;
7108 case htons(ETH_P_IPV6):
7109 opts[1] |= TD1_IPv6_CS;
7110 ip_protocol = ipv6_hdr(skb)->nexthdr;
7114 ip_protocol = IPPROTO_RAW;
7118 if (ip_protocol == IPPROTO_TCP)
7119 opts[1] |= TD1_TCP_CS;
7120 else if (ip_protocol == IPPROTO_UDP)
7121 opts[1] |= TD1_UDP_CS;
7125 opts[1] |= transport_offset << TCPHO_SHIFT;
7127 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7128 /* eth_skb_pad would free the skb on error */
7129 return !__skb_put_padto(skb, ETH_ZLEN, false);
7135 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7136 struct net_device *dev)
7138 struct rtl8169_private *tp = netdev_priv(dev);
7139 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
7140 struct TxDesc *txd = tp->TxDescArray + entry;
7141 void __iomem *ioaddr = tp->mmio_addr;
7142 struct device *d = &tp->pci_dev->dev;
7148 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
7149 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
7153 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
7156 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7159 if (!tp->tso_csum(tp, skb, opts)) {
7160 r8169_csum_workaround(tp, skb);
7161 return NETDEV_TX_OK;
7164 len = skb_headlen(skb);
7165 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
7166 if (unlikely(dma_mapping_error(d, mapping))) {
7167 if (net_ratelimit())
7168 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
7172 tp->tx_skb[entry].len = len;
7173 txd->addr = cpu_to_le64(mapping);
7175 frags = rtl8169_xmit_frags(tp, skb, opts);
7179 opts[0] |= FirstFrag;
7181 opts[0] |= FirstFrag | LastFrag;
7182 tp->tx_skb[entry].skb = skb;
7185 txd->opts2 = cpu_to_le32(opts[1]);
7187 skb_tx_timestamp(skb);
7189 /* Force memory writes to complete before releasing descriptor */
7192 /* Anti gcc 2.95.3 bugware (sic) */
7193 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
7194 txd->opts1 = cpu_to_le32(status);
7196 /* Force all memory writes to complete before notifying device */
7199 tp->cur_tx += frags + 1;
7201 RTL_W8(TxPoll, NPQ);
7205 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
7206 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7207 * not miss a ring update when it notices a stopped queue.
7210 netif_stop_queue(dev);
7211 /* Sync with rtl_tx:
7212 * - publish queue status and cur_tx ring index (write barrier)
7213 * - refresh dirty_tx ring index (read barrier).
7214 * May the current thread have a pessimistic view of the ring
7215 * status and forget to wake up queue, a racing rtl_tx thread
7219 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
7220 netif_wake_queue(dev);
7223 return NETDEV_TX_OK;
7226 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
7228 dev_kfree_skb_any(skb);
7229 dev->stats.tx_dropped++;
7230 return NETDEV_TX_OK;
7233 netif_stop_queue(dev);
7234 dev->stats.tx_dropped++;
7235 return NETDEV_TX_BUSY;
7238 static void rtl8169_pcierr_interrupt(struct net_device *dev)
7240 struct rtl8169_private *tp = netdev_priv(dev);
7241 struct pci_dev *pdev = tp->pci_dev;
7242 u16 pci_status, pci_cmd;
7244 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7245 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7247 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7248 pci_cmd, pci_status);
7251 * The recovery sequence below admits a very elaborated explanation:
7252 * - it seems to work;
7253 * - I did not see what else could be done;
7254 * - it makes iop3xx happy.
7256 * Feel free to adjust to your needs.
7258 if (pdev->broken_parity_status)
7259 pci_cmd &= ~PCI_COMMAND_PARITY;
7261 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7263 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
7265 pci_write_config_word(pdev, PCI_STATUS,
7266 pci_status & (PCI_STATUS_DETECTED_PARITY |
7267 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7268 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7270 /* The infamous DAC f*ckup only happens at boot time */
7271 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
7272 void __iomem *ioaddr = tp->mmio_addr;
7274 netif_info(tp, intr, dev, "disabling PCI DAC\n");
7275 tp->cp_cmd &= ~PCIDAC;
7276 RTL_W16(CPlusCmd, tp->cp_cmd);
7277 dev->features &= ~NETIF_F_HIGHDMA;
7280 rtl8169_hw_reset(tp);
7282 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7285 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
7287 unsigned int dirty_tx, tx_left;
7289 dirty_tx = tp->dirty_tx;
7291 tx_left = tp->cur_tx - dirty_tx;
7293 while (tx_left > 0) {
7294 unsigned int entry = dirty_tx % NUM_TX_DESC;
7295 struct ring_info *tx_skb = tp->tx_skb + entry;
7298 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7299 if (status & DescOwn)
7302 /* This barrier is needed to keep us from reading
7303 * any other fields out of the Tx descriptor until
7304 * we know the status of DescOwn
7308 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7309 tp->TxDescArray + entry);
7310 if (status & LastFrag) {
7311 u64_stats_update_begin(&tp->tx_stats.syncp);
7312 tp->tx_stats.packets++;
7313 tp->tx_stats.bytes += tx_skb->skb->len;
7314 u64_stats_update_end(&tp->tx_stats.syncp);
7315 dev_consume_skb_any(tx_skb->skb);
7322 if (tp->dirty_tx != dirty_tx) {
7323 tp->dirty_tx = dirty_tx;
7324 /* Sync with rtl8169_start_xmit:
7325 * - publish dirty_tx ring index (write barrier)
7326 * - refresh cur_tx ring index and queue status (read barrier)
7327 * May the current thread miss the stopped queue condition,
7328 * a racing xmit thread can only have a right view of the
7332 if (netif_queue_stopped(dev) &&
7333 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
7334 netif_wake_queue(dev);
7337 * 8168 hack: TxPoll requests are lost when the Tx packets are
7338 * too close. Let's kick an extra TxPoll request when a burst
7339 * of start_xmit activity is detected (if it is not detected,
7340 * it is slow enough). -- FR
7342 if (tp->cur_tx != dirty_tx) {
7343 void __iomem *ioaddr = tp->mmio_addr;
7345 RTL_W8(TxPoll, NPQ);
7350 static inline int rtl8169_fragmented_frame(u32 status)
7352 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7355 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
7357 u32 status = opts1 & RxProtoMask;
7359 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
7360 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
7361 skb->ip_summed = CHECKSUM_UNNECESSARY;
7363 skb_checksum_none_assert(skb);
7366 static struct sk_buff *rtl8169_try_rx_copy(void *data,
7367 struct rtl8169_private *tp,
7371 struct sk_buff *skb;
7372 struct device *d = &tp->pci_dev->dev;
7374 data = rtl8169_align(data);
7375 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
7377 skb = napi_alloc_skb(&tp->napi, pkt_size);
7379 memcpy(skb->data, data, pkt_size);
7380 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7385 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
7387 unsigned int cur_rx, rx_left;
7390 cur_rx = tp->cur_rx;
7392 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
7393 unsigned int entry = cur_rx % NUM_RX_DESC;
7394 struct RxDesc *desc = tp->RxDescArray + entry;
7397 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
7398 if (status & DescOwn)
7401 /* This barrier is needed to keep us from reading
7402 * any other fields out of the Rx descriptor until
7403 * we know the status of DescOwn
7407 if (unlikely(status & RxRES)) {
7408 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7410 dev->stats.rx_errors++;
7411 if (status & (RxRWT | RxRUNT))
7412 dev->stats.rx_length_errors++;
7414 dev->stats.rx_crc_errors++;
7415 if (status & RxFOVF) {
7416 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7417 dev->stats.rx_fifo_errors++;
7419 if ((status & (RxRUNT | RxCRC)) &&
7420 !(status & (RxRWT | RxFOVF)) &&
7421 (dev->features & NETIF_F_RXALL))
7424 struct sk_buff *skb;
7429 addr = le64_to_cpu(desc->addr);
7430 if (likely(!(dev->features & NETIF_F_RXFCS)))
7431 pkt_size = (status & 0x00003fff) - 4;
7433 pkt_size = status & 0x00003fff;
7436 * The driver does not support incoming fragmented
7437 * frames. They are seen as a symptom of over-mtu
7440 if (unlikely(rtl8169_fragmented_frame(status))) {
7441 dev->stats.rx_dropped++;
7442 dev->stats.rx_length_errors++;
7443 goto release_descriptor;
7446 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7447 tp, pkt_size, addr);
7449 dev->stats.rx_dropped++;
7450 goto release_descriptor;
7453 rtl8169_rx_csum(skb, status);
7454 skb_put(skb, pkt_size);
7455 skb->protocol = eth_type_trans(skb, dev);
7457 rtl8169_rx_vlan_tag(desc, skb);
7459 if (skb->pkt_type == PACKET_MULTICAST)
7460 dev->stats.multicast++;
7462 napi_gro_receive(&tp->napi, skb);
7464 u64_stats_update_begin(&tp->rx_stats.syncp);
7465 tp->rx_stats.packets++;
7466 tp->rx_stats.bytes += pkt_size;
7467 u64_stats_update_end(&tp->rx_stats.syncp);
7471 rtl8169_mark_to_asic(desc, rx_buf_sz);
7474 count = cur_rx - tp->cur_rx;
7475 tp->cur_rx = cur_rx;
7480 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
7482 struct net_device *dev = dev_instance;
7483 struct rtl8169_private *tp = netdev_priv(dev);
7487 status = rtl_get_events(tp);
7488 if (status && status != 0xffff) {
7489 status &= RTL_EVENT_NAPI | tp->event_slow;
7493 rtl_irq_disable(tp);
7494 napi_schedule(&tp->napi);
7497 return IRQ_RETVAL(handled);
7501 * Workqueue context.
7503 static void rtl_slow_event_work(struct rtl8169_private *tp)
7505 struct net_device *dev = tp->dev;
7508 status = rtl_get_events(tp) & tp->event_slow;
7509 rtl_ack_events(tp, status);
7511 if (unlikely(status & RxFIFOOver)) {
7512 switch (tp->mac_version) {
7513 /* Work around for rx fifo overflow */
7514 case RTL_GIGA_MAC_VER_11:
7515 netif_stop_queue(dev);
7516 /* XXX - Hack alert. See rtl_task(). */
7517 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
7523 if (unlikely(status & SYSErr))
7524 rtl8169_pcierr_interrupt(dev);
7526 if (status & LinkChg)
7527 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
7529 rtl_irq_enable_all(tp);
7532 static void rtl_task(struct work_struct *work)
7534 static const struct {
7536 void (*action)(struct rtl8169_private *);
7538 /* XXX - keep rtl_slow_event_work() as first element. */
7539 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7540 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7541 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7543 struct rtl8169_private *tp =
7544 container_of(work, struct rtl8169_private, wk.work);
7545 struct net_device *dev = tp->dev;
7550 if (!netif_running(dev) ||
7551 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
7554 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7557 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
7559 rtl_work[i].action(tp);
7563 rtl_unlock_work(tp);
7566 static int rtl8169_poll(struct napi_struct *napi, int budget)
7568 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7569 struct net_device *dev = tp->dev;
7570 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7574 status = rtl_get_events(tp);
7575 rtl_ack_events(tp, status & ~tp->event_slow);
7577 work_done = rtl_rx(dev, tp, (u32) budget);
7581 if (status & tp->event_slow) {
7582 enable_mask &= ~tp->event_slow;
7584 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7587 if (work_done < budget) {
7588 napi_complete_done(napi, work_done);
7590 rtl_irq_enable(tp, enable_mask);
7597 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7599 struct rtl8169_private *tp = netdev_priv(dev);
7601 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7604 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7605 RTL_W32(RxMissed, 0);
7608 static void rtl8169_down(struct net_device *dev)
7610 struct rtl8169_private *tp = netdev_priv(dev);
7611 void __iomem *ioaddr = tp->mmio_addr;
7613 del_timer_sync(&tp->timer);
7615 napi_disable(&tp->napi);
7616 netif_stop_queue(dev);
7618 rtl8169_hw_reset(tp);
7620 * At this point device interrupts can not be enabled in any function,
7621 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7622 * and napi is disabled (rtl8169_poll).
7624 rtl8169_rx_missed(dev, ioaddr);
7626 /* Give a racing hard_start_xmit a few cycles to complete. */
7627 synchronize_sched();
7629 rtl8169_tx_clear(tp);
7631 rtl8169_rx_clear(tp);
7633 rtl_pll_power_down(tp);
7636 static int rtl8169_close(struct net_device *dev)
7638 struct rtl8169_private *tp = netdev_priv(dev);
7639 struct pci_dev *pdev = tp->pci_dev;
7641 pm_runtime_get_sync(&pdev->dev);
7643 /* Update counters before going down */
7644 rtl8169_update_counters(dev);
7647 /* Clear all task flags */
7648 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
7651 rtl_unlock_work(tp);
7653 cancel_work_sync(&tp->wk.work);
7655 free_irq(pdev->irq, dev);
7657 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7659 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7661 tp->TxDescArray = NULL;
7662 tp->RxDescArray = NULL;
7664 pm_runtime_put_sync(&pdev->dev);
7669 #ifdef CONFIG_NET_POLL_CONTROLLER
7670 static void rtl8169_netpoll(struct net_device *dev)
7672 struct rtl8169_private *tp = netdev_priv(dev);
7674 rtl8169_interrupt(tp->pci_dev->irq, dev);
7678 static int rtl_open(struct net_device *dev)
7680 struct rtl8169_private *tp = netdev_priv(dev);
7681 void __iomem *ioaddr = tp->mmio_addr;
7682 struct pci_dev *pdev = tp->pci_dev;
7683 int retval = -ENOMEM;
7685 pm_runtime_get_sync(&pdev->dev);
7688 * Rx and Tx descriptors needs 256 bytes alignment.
7689 * dma_alloc_coherent provides more.
7691 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7692 &tp->TxPhyAddr, GFP_KERNEL);
7693 if (!tp->TxDescArray)
7694 goto err_pm_runtime_put;
7696 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7697 &tp->RxPhyAddr, GFP_KERNEL);
7698 if (!tp->RxDescArray)
7701 retval = rtl8169_init_ring(dev);
7705 INIT_WORK(&tp->wk.work, rtl_task);
7709 rtl_request_firmware(tp);
7711 retval = request_irq(pdev->irq, rtl8169_interrupt,
7712 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7715 goto err_release_fw_2;
7719 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7721 napi_enable(&tp->napi);
7723 rtl8169_init_phy(dev, tp);
7725 __rtl8169_set_features(dev, dev->features);
7727 rtl_pll_power_up(tp);
7731 if (!rtl8169_init_counter_offsets(dev))
7732 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7734 netif_start_queue(dev);
7736 rtl_unlock_work(tp);
7738 tp->saved_wolopts = 0;
7739 pm_runtime_put_noidle(&pdev->dev);
7741 rtl8169_check_link_status(dev, tp, ioaddr);
7746 rtl_release_firmware(tp);
7747 rtl8169_rx_clear(tp);
7749 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7751 tp->RxDescArray = NULL;
7753 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7755 tp->TxDescArray = NULL;
7757 pm_runtime_put_noidle(&pdev->dev);
7762 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7764 struct rtl8169_private *tp = netdev_priv(dev);
7765 void __iomem *ioaddr = tp->mmio_addr;
7766 struct pci_dev *pdev = tp->pci_dev;
7767 struct rtl8169_counters *counters = tp->counters;
7770 pm_runtime_get_noresume(&pdev->dev);
7772 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
7773 rtl8169_rx_missed(dev, ioaddr);
7776 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
7777 stats->rx_packets = tp->rx_stats.packets;
7778 stats->rx_bytes = tp->rx_stats.bytes;
7779 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
7782 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
7783 stats->tx_packets = tp->tx_stats.packets;
7784 stats->tx_bytes = tp->tx_stats.bytes;
7785 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
7787 stats->rx_dropped = dev->stats.rx_dropped;
7788 stats->tx_dropped = dev->stats.tx_dropped;
7789 stats->rx_length_errors = dev->stats.rx_length_errors;
7790 stats->rx_errors = dev->stats.rx_errors;
7791 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7792 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7793 stats->rx_missed_errors = dev->stats.rx_missed_errors;
7794 stats->multicast = dev->stats.multicast;
7797 * Fetch additonal counter values missing in stats collected by driver
7798 * from tally counters.
7800 if (pm_runtime_active(&pdev->dev))
7801 rtl8169_update_counters(dev);
7804 * Subtract values fetched during initalization.
7805 * See rtl8169_init_counter_offsets for a description why we do that.
7807 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
7808 le64_to_cpu(tp->tc_offset.tx_errors);
7809 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
7810 le32_to_cpu(tp->tc_offset.tx_multi_collision);
7811 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
7812 le16_to_cpu(tp->tc_offset.tx_aborted);
7814 pm_runtime_put_noidle(&pdev->dev);
7817 static void rtl8169_net_suspend(struct net_device *dev)
7819 struct rtl8169_private *tp = netdev_priv(dev);
7821 if (!netif_running(dev))
7824 netif_device_detach(dev);
7825 netif_stop_queue(dev);
7828 napi_disable(&tp->napi);
7829 /* Clear all task flags */
7830 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
7832 rtl_unlock_work(tp);
7834 rtl_pll_power_down(tp);
7839 static int rtl8169_suspend(struct device *device)
7841 struct pci_dev *pdev = to_pci_dev(device);
7842 struct net_device *dev = pci_get_drvdata(pdev);
7844 rtl8169_net_suspend(dev);
7849 static void __rtl8169_resume(struct net_device *dev)
7851 struct rtl8169_private *tp = netdev_priv(dev);
7853 netif_device_attach(dev);
7855 rtl_pll_power_up(tp);
7858 napi_enable(&tp->napi);
7859 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7860 rtl_unlock_work(tp);
7862 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7865 static int rtl8169_resume(struct device *device)
7867 struct pci_dev *pdev = to_pci_dev(device);
7868 struct net_device *dev = pci_get_drvdata(pdev);
7869 struct rtl8169_private *tp = netdev_priv(dev);
7871 rtl8169_init_phy(dev, tp);
7873 if (netif_running(dev))
7874 __rtl8169_resume(dev);
7879 static int rtl8169_runtime_suspend(struct device *device)
7881 struct pci_dev *pdev = to_pci_dev(device);
7882 struct net_device *dev = pci_get_drvdata(pdev);
7883 struct rtl8169_private *tp = netdev_priv(dev);
7885 if (!tp->TxDescArray)
7889 tp->saved_wolopts = __rtl8169_get_wol(tp);
7890 __rtl8169_set_wol(tp, WAKE_ANY);
7891 rtl_unlock_work(tp);
7893 rtl8169_net_suspend(dev);
7895 /* Update counters before going runtime suspend */
7896 rtl8169_rx_missed(dev, tp->mmio_addr);
7897 rtl8169_update_counters(dev);
7902 static int rtl8169_runtime_resume(struct device *device)
7904 struct pci_dev *pdev = to_pci_dev(device);
7905 struct net_device *dev = pci_get_drvdata(pdev);
7906 struct rtl8169_private *tp = netdev_priv(dev);
7907 rtl_rar_set(tp, dev->dev_addr);
7909 if (!tp->TxDescArray)
7913 __rtl8169_set_wol(tp, tp->saved_wolopts);
7914 tp->saved_wolopts = 0;
7915 rtl_unlock_work(tp);
7917 rtl8169_init_phy(dev, tp);
7919 __rtl8169_resume(dev);
7924 static int rtl8169_runtime_idle(struct device *device)
7926 struct pci_dev *pdev = to_pci_dev(device);
7927 struct net_device *dev = pci_get_drvdata(pdev);
7928 struct rtl8169_private *tp = netdev_priv(dev);
7930 return tp->TxDescArray ? -EBUSY : 0;
7933 static const struct dev_pm_ops rtl8169_pm_ops = {
7934 .suspend = rtl8169_suspend,
7935 .resume = rtl8169_resume,
7936 .freeze = rtl8169_suspend,
7937 .thaw = rtl8169_resume,
7938 .poweroff = rtl8169_suspend,
7939 .restore = rtl8169_resume,
7940 .runtime_suspend = rtl8169_runtime_suspend,
7941 .runtime_resume = rtl8169_runtime_resume,
7942 .runtime_idle = rtl8169_runtime_idle,
7945 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
7947 #else /* !CONFIG_PM */
7949 #define RTL8169_PM_OPS NULL
7951 #endif /* !CONFIG_PM */
7953 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7955 void __iomem *ioaddr = tp->mmio_addr;
7957 /* WoL fails with 8168b when the receiver is disabled. */
7958 switch (tp->mac_version) {
7959 case RTL_GIGA_MAC_VER_11:
7960 case RTL_GIGA_MAC_VER_12:
7961 case RTL_GIGA_MAC_VER_17:
7962 pci_clear_master(tp->pci_dev);
7964 RTL_W8(ChipCmd, CmdRxEnb);
7973 static void rtl_shutdown(struct pci_dev *pdev)
7975 struct net_device *dev = pci_get_drvdata(pdev);
7976 struct rtl8169_private *tp = netdev_priv(dev);
7977 struct device *d = &pdev->dev;
7979 pm_runtime_get_sync(d);
7981 rtl8169_net_suspend(dev);
7983 /* Restore original MAC address */
7984 rtl_rar_set(tp, dev->perm_addr);
7986 rtl8169_hw_reset(tp);
7988 if (system_state == SYSTEM_POWER_OFF) {
7989 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7990 rtl_wol_suspend_quirk(tp);
7991 rtl_wol_shutdown_quirk(tp);
7994 pci_wake_from_d3(pdev, true);
7995 pci_set_power_state(pdev, PCI_D3hot);
7998 pm_runtime_put_noidle(d);
8001 static void rtl_remove_one(struct pci_dev *pdev)
8003 struct net_device *dev = pci_get_drvdata(pdev);
8004 struct rtl8169_private *tp = netdev_priv(dev);
8006 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8007 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
8008 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8009 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8010 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8011 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
8012 r8168_check_dash(tp)) {
8013 rtl8168_driver_stop(tp);
8016 netif_napi_del(&tp->napi);
8018 unregister_netdev(dev);
8020 dma_free_coherent(&tp->pci_dev->dev, sizeof(*tp->counters),
8021 tp->counters, tp->counters_phys_addr);
8023 rtl_release_firmware(tp);
8025 if (pci_dev_run_wake(pdev))
8026 pm_runtime_get_noresume(&pdev->dev);
8028 /* restore original MAC address */
8029 rtl_rar_set(tp, dev->perm_addr);
8031 rtl_disable_msi(pdev, tp);
8032 rtl8169_release_board(pdev, dev, tp->mmio_addr);
8035 static const struct net_device_ops rtl_netdev_ops = {
8036 .ndo_open = rtl_open,
8037 .ndo_stop = rtl8169_close,
8038 .ndo_get_stats64 = rtl8169_get_stats64,
8039 .ndo_start_xmit = rtl8169_start_xmit,
8040 .ndo_tx_timeout = rtl8169_tx_timeout,
8041 .ndo_validate_addr = eth_validate_addr,
8042 .ndo_change_mtu = rtl8169_change_mtu,
8043 .ndo_fix_features = rtl8169_fix_features,
8044 .ndo_set_features = rtl8169_set_features,
8045 .ndo_set_mac_address = rtl_set_mac_address,
8046 .ndo_do_ioctl = rtl8169_ioctl,
8047 .ndo_set_rx_mode = rtl_set_rx_mode,
8048 #ifdef CONFIG_NET_POLL_CONTROLLER
8049 .ndo_poll_controller = rtl8169_netpoll,
8054 static const struct rtl_cfg_info {
8055 void (*hw_start)(struct net_device *);
8056 unsigned int region;
8061 } rtl_cfg_infos [] = {
8063 .hw_start = rtl_hw_start_8169,
8066 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8067 .features = RTL_FEATURE_GMII,
8068 .default_ver = RTL_GIGA_MAC_VER_01,
8071 .hw_start = rtl_hw_start_8168,
8074 .event_slow = SYSErr | LinkChg | RxOverflow,
8075 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
8076 .default_ver = RTL_GIGA_MAC_VER_11,
8079 .hw_start = rtl_hw_start_8101,
8082 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8084 .features = RTL_FEATURE_MSI,
8085 .default_ver = RTL_GIGA_MAC_VER_13,
8089 /* Cfg9346_Unlock assumed. */
8090 static unsigned rtl_try_msi(struct rtl8169_private *tp,
8091 const struct rtl_cfg_info *cfg)
8093 void __iomem *ioaddr = tp->mmio_addr;
8097 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8098 if (cfg->features & RTL_FEATURE_MSI) {
8099 if (pci_enable_msi(tp->pci_dev)) {
8100 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8103 msi = RTL_FEATURE_MSI;
8106 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8107 RTL_W8(Config2, cfg2);
8111 DECLARE_RTL_COND(rtl_link_list_ready_cond)
8113 void __iomem *ioaddr = tp->mmio_addr;
8115 return RTL_R8(MCU) & LINK_LIST_RDY;
8118 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8120 void __iomem *ioaddr = tp->mmio_addr;
8122 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8125 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
8127 void __iomem *ioaddr = tp->mmio_addr;
8130 tp->ocp_base = OCP_STD_PHY_BASE;
8132 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8134 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8137 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8140 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8142 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8144 data = r8168_mac_ocp_read(tp, 0xe8de);
8146 r8168_mac_ocp_write(tp, 0xe8de, data);
8148 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8151 data = r8168_mac_ocp_read(tp, 0xe8de);
8153 r8168_mac_ocp_write(tp, 0xe8de, data);
8155 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8159 static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8161 rtl8168ep_stop_cmac(tp);
8162 rtl_hw_init_8168g(tp);
8165 static void rtl_hw_initialize(struct rtl8169_private *tp)
8167 switch (tp->mac_version) {
8168 case RTL_GIGA_MAC_VER_40:
8169 case RTL_GIGA_MAC_VER_41:
8170 case RTL_GIGA_MAC_VER_42:
8171 case RTL_GIGA_MAC_VER_43:
8172 case RTL_GIGA_MAC_VER_44:
8173 case RTL_GIGA_MAC_VER_45:
8174 case RTL_GIGA_MAC_VER_46:
8175 case RTL_GIGA_MAC_VER_47:
8176 case RTL_GIGA_MAC_VER_48:
8177 rtl_hw_init_8168g(tp);
8179 case RTL_GIGA_MAC_VER_49:
8180 case RTL_GIGA_MAC_VER_50:
8181 case RTL_GIGA_MAC_VER_51:
8182 rtl_hw_init_8168ep(tp);
8189 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8191 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8192 const unsigned int region = cfg->region;
8193 struct rtl8169_private *tp;
8194 struct mii_if_info *mii;
8195 struct net_device *dev;
8196 void __iomem *ioaddr;
8200 if (netif_msg_drv(&debug)) {
8201 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8202 MODULENAME, RTL8169_VERSION);
8205 dev = alloc_etherdev(sizeof (*tp));
8211 SET_NETDEV_DEV(dev, &pdev->dev);
8212 dev->netdev_ops = &rtl_netdev_ops;
8213 tp = netdev_priv(dev);
8216 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8220 mii->mdio_read = rtl_mdio_read;
8221 mii->mdio_write = rtl_mdio_write;
8222 mii->phy_id_mask = 0x1f;
8223 mii->reg_num_mask = 0x1f;
8224 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8226 /* disable ASPM completely as that cause random device stop working
8227 * problems as well as full system hangs for some PCIe devices users */
8228 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8229 PCIE_LINK_STATE_CLKPM);
8231 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8232 rc = pci_enable_device(pdev);
8234 netif_err(tp, probe, dev, "enable failure\n");
8235 goto err_out_free_dev_1;
8238 if (pci_set_mwi(pdev) < 0)
8239 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8241 /* make sure PCI base addr 1 is MMIO */
8242 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8243 netif_err(tp, probe, dev,
8244 "region #%d not an MMIO resource, aborting\n",
8250 /* check for weird/broken PCI region reporting */
8251 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8252 netif_err(tp, probe, dev,
8253 "Invalid PCI region size(s), aborting\n");
8258 rc = pci_request_regions(pdev, MODULENAME);
8260 netif_err(tp, probe, dev, "could not request regions\n");
8264 /* ioremap MMIO region */
8265 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
8267 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8269 goto err_out_free_res_3;
8271 tp->mmio_addr = ioaddr;
8273 if (!pci_is_pcie(pdev))
8274 netif_info(tp, probe, dev, "not PCI Express\n");
8276 /* Identify chip attached to board */
8277 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8281 if ((sizeof(dma_addr_t) > 4) &&
8282 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8283 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
8284 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8285 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
8287 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8288 if (!pci_is_pcie(pdev))
8289 tp->cp_cmd |= PCIDAC;
8290 dev->features |= NETIF_F_HIGHDMA;
8292 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8294 netif_err(tp, probe, dev, "DMA configuration failed\n");
8295 goto err_out_unmap_4;
8301 rtl_irq_disable(tp);
8303 rtl_hw_initialize(tp);
8307 rtl_ack_events(tp, 0xffff);
8309 pci_set_master(pdev);
8311 rtl_init_mdio_ops(tp);
8312 rtl_init_pll_power_ops(tp);
8313 rtl_init_jumbo_ops(tp);
8314 rtl_init_csi_ops(tp);
8316 rtl8169_print_mac_version(tp);
8318 chipset = tp->mac_version;
8319 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8321 RTL_W8(Cfg9346, Cfg9346_Unlock);
8322 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
8323 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
8324 switch (tp->mac_version) {
8325 case RTL_GIGA_MAC_VER_34:
8326 case RTL_GIGA_MAC_VER_35:
8327 case RTL_GIGA_MAC_VER_36:
8328 case RTL_GIGA_MAC_VER_37:
8329 case RTL_GIGA_MAC_VER_38:
8330 case RTL_GIGA_MAC_VER_40:
8331 case RTL_GIGA_MAC_VER_41:
8332 case RTL_GIGA_MAC_VER_42:
8333 case RTL_GIGA_MAC_VER_43:
8334 case RTL_GIGA_MAC_VER_44:
8335 case RTL_GIGA_MAC_VER_45:
8336 case RTL_GIGA_MAC_VER_46:
8337 case RTL_GIGA_MAC_VER_47:
8338 case RTL_GIGA_MAC_VER_48:
8339 case RTL_GIGA_MAC_VER_49:
8340 case RTL_GIGA_MAC_VER_50:
8341 case RTL_GIGA_MAC_VER_51:
8342 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
8343 tp->features |= RTL_FEATURE_WOL;
8344 if ((RTL_R8(Config3) & LinkUp) != 0)
8345 tp->features |= RTL_FEATURE_WOL;
8348 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
8349 tp->features |= RTL_FEATURE_WOL;
8352 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
8353 tp->features |= RTL_FEATURE_WOL;
8354 tp->features |= rtl_try_msi(tp, cfg);
8355 RTL_W8(Cfg9346, Cfg9346_Lock);
8357 if (rtl_tbi_enabled(tp)) {
8358 tp->set_speed = rtl8169_set_speed_tbi;
8359 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
8360 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8361 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8362 tp->link_ok = rtl8169_tbi_link_ok;
8363 tp->do_ioctl = rtl_tbi_ioctl;
8365 tp->set_speed = rtl8169_set_speed_xmii;
8366 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
8367 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8368 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8369 tp->link_ok = rtl8169_xmii_link_ok;
8370 tp->do_ioctl = rtl_xmii_ioctl;
8373 mutex_init(&tp->wk.mutex);
8374 u64_stats_init(&tp->rx_stats.syncp);
8375 u64_stats_init(&tp->tx_stats.syncp);
8377 /* Get MAC address */
8378 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8379 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8380 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8381 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8382 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8383 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8384 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8385 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8386 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8387 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
8388 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8389 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
8390 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8391 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8392 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8393 tp->mac_version == RTL_GIGA_MAC_VER_51) {
8396 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8397 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
8399 if (is_valid_ether_addr((u8 *)mac_addr))
8400 rtl_rar_set(tp, (u8 *)mac_addr);
8402 for (i = 0; i < ETH_ALEN; i++)
8403 dev->dev_addr[i] = RTL_R8(MAC0 + i);
8405 dev->ethtool_ops = &rtl8169_ethtool_ops;
8406 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
8408 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8410 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8411 * properly for all devices */
8412 dev->features |= NETIF_F_RXCSUM |
8413 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
8415 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8416 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8417 NETIF_F_HW_VLAN_CTAG_RX;
8418 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8421 tp->cp_cmd |= RxChkSum | RxVlan;
8424 * Pretend we are using VLANs; This bypasses a nasty bug where
8425 * Interrupts stop flowing on high load on 8110SCd controllers.
8427 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
8428 /* Disallow toggling */
8429 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
8431 if (tp->txd_version == RTL_TD_0)
8432 tp->tso_csum = rtl8169_tso_csum_v1;
8433 else if (tp->txd_version == RTL_TD_1) {
8434 tp->tso_csum = rtl8169_tso_csum_v2;
8435 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8439 dev->hw_features |= NETIF_F_RXALL;
8440 dev->hw_features |= NETIF_F_RXFCS;
8442 /* MTU range: 60 - hw-specific max */
8443 dev->min_mtu = ETH_ZLEN;
8444 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8446 tp->hw_start = cfg->hw_start;
8447 tp->event_slow = cfg->event_slow;
8449 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8450 ~(RxBOVF | RxFOVF) : ~0;
8452 setup_timer(&tp->timer, rtl8169_phy_timer, (unsigned long)dev);
8454 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8456 tp->counters = dma_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8457 &tp->counters_phys_addr, GFP_KERNEL);
8458 if (!tp->counters) {
8463 pci_set_drvdata(pdev, dev);
8465 rc = register_netdev(dev);
8469 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8470 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8471 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
8472 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8473 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8474 "tx checksumming: %s]\n",
8475 rtl_chip_infos[chipset].jumbo_max,
8476 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8479 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8480 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
8481 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8482 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8483 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8484 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
8485 r8168_check_dash(tp)) {
8486 rtl8168_driver_start(tp);
8489 if (pci_dev_run_wake(pdev))
8490 pm_runtime_put_noidle(&pdev->dev);
8492 netif_carrier_off(dev);
8498 dma_free_coherent(&pdev->dev, sizeof(*tp->counters), tp->counters,
8499 tp->counters_phys_addr);
8501 netif_napi_del(&tp->napi);
8502 rtl_disable_msi(pdev, tp);
8506 pci_release_regions(pdev);
8508 pci_clear_mwi(pdev);
8509 pci_disable_device(pdev);
8515 static struct pci_driver rtl8169_pci_driver = {
8517 .id_table = rtl8169_pci_tbl,
8518 .probe = rtl_init_one,
8519 .remove = rtl_remove_one,
8520 .shutdown = rtl_shutdown,
8521 .driver.pm = RTL8169_PM_OPS,
8524 module_pci_driver(rtl8169_pci_driver);