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 2/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_settings)(struct net_device *, struct ethtool_cmd *);
824 void (*phy_reset_enable)(struct rtl8169_private *tp);
825 void (*hw_start)(struct net_device *);
826 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
827 unsigned int (*link_ok)(void __iomem *);
828 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
829 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
832 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
834 struct work_struct work;
839 struct mii_if_info mii;
840 dma_addr_t counters_phys_addr;
841 struct rtl8169_counters *counters;
842 struct rtl8169_tc_offsets tc_offset;
847 const struct firmware *fw;
849 #define RTL_VER_SIZE 32
851 char version[RTL_VER_SIZE];
853 struct rtl_fw_phy_action {
858 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
863 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
864 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
865 module_param(use_dac, int, 0);
866 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
867 module_param_named(debug, debug.msg_enable, int, 0);
868 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
869 MODULE_LICENSE("GPL");
870 MODULE_VERSION(RTL8169_VERSION);
873 static void rtl_lock_work(struct rtl8169_private *tp)
875 mutex_lock(&tp->wk.mutex);
878 static void rtl_unlock_work(struct rtl8169_private *tp)
880 mutex_unlock(&tp->wk.mutex);
883 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
885 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
886 PCI_EXP_DEVCTL_READRQ, force);
890 bool (*check)(struct rtl8169_private *);
894 static void rtl_udelay(unsigned int d)
899 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
900 void (*delay)(unsigned int), unsigned int d, int n,
905 for (i = 0; i < n; i++) {
907 if (c->check(tp) == high)
910 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
911 c->msg, !high, n, d);
915 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
916 const struct rtl_cond *c,
917 unsigned int d, int n)
919 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
922 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
923 const struct rtl_cond *c,
924 unsigned int d, int n)
926 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
929 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
930 const struct rtl_cond *c,
931 unsigned int d, int n)
933 return rtl_loop_wait(tp, c, msleep, d, n, true);
936 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
937 const struct rtl_cond *c,
938 unsigned int d, int n)
940 return rtl_loop_wait(tp, c, msleep, d, n, false);
943 #define DECLARE_RTL_COND(name) \
944 static bool name ## _check(struct rtl8169_private *); \
946 static const struct rtl_cond name = { \
947 .check = name ## _check, \
951 static bool name ## _check(struct rtl8169_private *tp)
953 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
955 if (reg & 0xffff0001) {
956 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
962 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
964 void __iomem *ioaddr = tp->mmio_addr;
966 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
969 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
971 void __iomem *ioaddr = tp->mmio_addr;
973 if (rtl_ocp_reg_failure(tp, reg))
976 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
978 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
981 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
983 void __iomem *ioaddr = tp->mmio_addr;
985 if (rtl_ocp_reg_failure(tp, reg))
988 RTL_W32(GPHY_OCP, reg << 15);
990 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
991 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
994 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
996 void __iomem *ioaddr = tp->mmio_addr;
998 if (rtl_ocp_reg_failure(tp, reg))
1001 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
1004 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1006 void __iomem *ioaddr = tp->mmio_addr;
1008 if (rtl_ocp_reg_failure(tp, reg))
1011 RTL_W32(OCPDR, reg << 15);
1013 return RTL_R32(OCPDR);
1016 #define OCP_STD_PHY_BASE 0xa400
1018 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1021 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1025 if (tp->ocp_base != OCP_STD_PHY_BASE)
1028 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1031 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1033 if (tp->ocp_base != OCP_STD_PHY_BASE)
1036 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1039 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1042 tp->ocp_base = value << 4;
1046 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1049 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1051 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1054 DECLARE_RTL_COND(rtl_phyar_cond)
1056 void __iomem *ioaddr = tp->mmio_addr;
1058 return RTL_R32(PHYAR) & 0x80000000;
1061 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1063 void __iomem *ioaddr = tp->mmio_addr;
1065 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1067 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1069 * According to hardware specs a 20us delay is required after write
1070 * complete indication, but before sending next command.
1075 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1077 void __iomem *ioaddr = tp->mmio_addr;
1080 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
1082 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1083 RTL_R32(PHYAR) & 0xffff : ~0;
1086 * According to hardware specs a 20us delay is required after read
1087 * complete indication, but before sending next command.
1094 DECLARE_RTL_COND(rtl_ocpar_cond)
1096 void __iomem *ioaddr = tp->mmio_addr;
1098 return RTL_R32(OCPAR) & OCPAR_FLAG;
1101 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1103 void __iomem *ioaddr = tp->mmio_addr;
1105 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1106 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1107 RTL_W32(EPHY_RXER_NUM, 0);
1109 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1112 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1114 r8168dp_1_mdio_access(tp, reg,
1115 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1118 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1120 void __iomem *ioaddr = tp->mmio_addr;
1122 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1125 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1126 RTL_W32(EPHY_RXER_NUM, 0);
1128 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1129 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
1132 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1134 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1136 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1139 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1141 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1144 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1146 void __iomem *ioaddr = tp->mmio_addr;
1148 r8168dp_2_mdio_start(ioaddr);
1150 r8169_mdio_write(tp, reg, value);
1152 r8168dp_2_mdio_stop(ioaddr);
1155 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1157 void __iomem *ioaddr = tp->mmio_addr;
1160 r8168dp_2_mdio_start(ioaddr);
1162 value = r8169_mdio_read(tp, reg);
1164 r8168dp_2_mdio_stop(ioaddr);
1169 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1171 tp->mdio_ops.write(tp, location, val);
1174 static int rtl_readphy(struct rtl8169_private *tp, int location)
1176 return tp->mdio_ops.read(tp, location);
1179 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1181 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1184 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1188 val = rtl_readphy(tp, reg_addr);
1189 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1192 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1195 struct rtl8169_private *tp = netdev_priv(dev);
1197 rtl_writephy(tp, location, val);
1200 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1202 struct rtl8169_private *tp = netdev_priv(dev);
1204 return rtl_readphy(tp, location);
1207 DECLARE_RTL_COND(rtl_ephyar_cond)
1209 void __iomem *ioaddr = tp->mmio_addr;
1211 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1214 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1216 void __iomem *ioaddr = tp->mmio_addr;
1218 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1219 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1221 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1226 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1228 void __iomem *ioaddr = tp->mmio_addr;
1230 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1232 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1233 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
1236 DECLARE_RTL_COND(rtl_eriar_cond)
1238 void __iomem *ioaddr = tp->mmio_addr;
1240 return RTL_R32(ERIAR) & ERIAR_FLAG;
1243 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1246 void __iomem *ioaddr = tp->mmio_addr;
1248 BUG_ON((addr & 3) || (mask == 0));
1249 RTL_W32(ERIDR, val);
1250 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1252 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1255 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1257 void __iomem *ioaddr = tp->mmio_addr;
1259 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1261 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1262 RTL_R32(ERIDR) : ~0;
1265 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1270 val = rtl_eri_read(tp, addr, type);
1271 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1274 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1276 void __iomem *ioaddr = tp->mmio_addr;
1278 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1279 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1280 RTL_R32(OCPDR) : ~0;
1283 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1285 return rtl_eri_read(tp, reg, ERIAR_OOB);
1288 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1290 switch (tp->mac_version) {
1291 case RTL_GIGA_MAC_VER_27:
1292 case RTL_GIGA_MAC_VER_28:
1293 case RTL_GIGA_MAC_VER_31:
1294 return r8168dp_ocp_read(tp, mask, reg);
1295 case RTL_GIGA_MAC_VER_49:
1296 case RTL_GIGA_MAC_VER_50:
1297 case RTL_GIGA_MAC_VER_51:
1298 return r8168ep_ocp_read(tp, mask, reg);
1305 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1308 void __iomem *ioaddr = tp->mmio_addr;
1310 RTL_W32(OCPDR, data);
1311 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1312 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1315 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1318 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1322 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1324 switch (tp->mac_version) {
1325 case RTL_GIGA_MAC_VER_27:
1326 case RTL_GIGA_MAC_VER_28:
1327 case RTL_GIGA_MAC_VER_31:
1328 r8168dp_ocp_write(tp, mask, reg, data);
1330 case RTL_GIGA_MAC_VER_49:
1331 case RTL_GIGA_MAC_VER_50:
1332 case RTL_GIGA_MAC_VER_51:
1333 r8168ep_ocp_write(tp, mask, reg, data);
1341 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1343 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1345 ocp_write(tp, 0x1, 0x30, 0x00000001);
1348 #define OOB_CMD_RESET 0x00
1349 #define OOB_CMD_DRIVER_START 0x05
1350 #define OOB_CMD_DRIVER_STOP 0x06
1352 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1354 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1357 DECLARE_RTL_COND(rtl_ocp_read_cond)
1361 reg = rtl8168_get_ocp_reg(tp);
1363 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1366 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1368 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1371 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1373 void __iomem *ioaddr = tp->mmio_addr;
1375 return RTL_R8(IBISR0) & 0x20;
1378 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1380 void __iomem *ioaddr = tp->mmio_addr;
1382 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
1383 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1384 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1385 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1388 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1390 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1391 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1394 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1396 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1397 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1398 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1401 static void rtl8168_driver_start(struct rtl8169_private *tp)
1403 switch (tp->mac_version) {
1404 case RTL_GIGA_MAC_VER_27:
1405 case RTL_GIGA_MAC_VER_28:
1406 case RTL_GIGA_MAC_VER_31:
1407 rtl8168dp_driver_start(tp);
1409 case RTL_GIGA_MAC_VER_49:
1410 case RTL_GIGA_MAC_VER_50:
1411 case RTL_GIGA_MAC_VER_51:
1412 rtl8168ep_driver_start(tp);
1420 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1422 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1423 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1426 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1428 rtl8168ep_stop_cmac(tp);
1429 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1430 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1431 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1434 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1436 switch (tp->mac_version) {
1437 case RTL_GIGA_MAC_VER_27:
1438 case RTL_GIGA_MAC_VER_28:
1439 case RTL_GIGA_MAC_VER_31:
1440 rtl8168dp_driver_stop(tp);
1442 case RTL_GIGA_MAC_VER_49:
1443 case RTL_GIGA_MAC_VER_50:
1444 case RTL_GIGA_MAC_VER_51:
1445 rtl8168ep_driver_stop(tp);
1453 static int r8168dp_check_dash(struct rtl8169_private *tp)
1455 u16 reg = rtl8168_get_ocp_reg(tp);
1457 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1460 static int r8168ep_check_dash(struct rtl8169_private *tp)
1462 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1465 static int r8168_check_dash(struct rtl8169_private *tp)
1467 switch (tp->mac_version) {
1468 case RTL_GIGA_MAC_VER_27:
1469 case RTL_GIGA_MAC_VER_28:
1470 case RTL_GIGA_MAC_VER_31:
1471 return r8168dp_check_dash(tp);
1472 case RTL_GIGA_MAC_VER_49:
1473 case RTL_GIGA_MAC_VER_50:
1474 case RTL_GIGA_MAC_VER_51:
1475 return r8168ep_check_dash(tp);
1487 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1488 const struct exgmac_reg *r, int len)
1491 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1496 DECLARE_RTL_COND(rtl_efusear_cond)
1498 void __iomem *ioaddr = tp->mmio_addr;
1500 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1503 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1505 void __iomem *ioaddr = tp->mmio_addr;
1507 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1509 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1510 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1513 static u16 rtl_get_events(struct rtl8169_private *tp)
1515 void __iomem *ioaddr = tp->mmio_addr;
1517 return RTL_R16(IntrStatus);
1520 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1522 void __iomem *ioaddr = tp->mmio_addr;
1524 RTL_W16(IntrStatus, bits);
1528 static void rtl_irq_disable(struct rtl8169_private *tp)
1530 void __iomem *ioaddr = tp->mmio_addr;
1532 RTL_W16(IntrMask, 0);
1536 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1538 void __iomem *ioaddr = tp->mmio_addr;
1540 RTL_W16(IntrMask, bits);
1543 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1544 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1545 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1547 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1549 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1552 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1554 void __iomem *ioaddr = tp->mmio_addr;
1556 rtl_irq_disable(tp);
1557 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1561 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1563 void __iomem *ioaddr = tp->mmio_addr;
1565 return RTL_R32(TBICSR) & TBIReset;
1568 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1570 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1573 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1575 return RTL_R32(TBICSR) & TBILinkOk;
1578 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1580 return RTL_R8(PHYstatus) & LinkStatus;
1583 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1585 void __iomem *ioaddr = tp->mmio_addr;
1587 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1590 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1594 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1595 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1598 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1600 void __iomem *ioaddr = tp->mmio_addr;
1601 struct net_device *dev = tp->dev;
1603 if (!netif_running(dev))
1606 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1607 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1608 if (RTL_R8(PHYstatus) & _1000bpsF) {
1609 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1611 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1613 } else if (RTL_R8(PHYstatus) & _100bps) {
1614 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1616 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1619 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1621 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1624 /* Reset packet filter */
1625 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1627 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1629 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1630 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1631 if (RTL_R8(PHYstatus) & _1000bpsF) {
1632 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1634 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1637 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1639 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1642 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1643 if (RTL_R8(PHYstatus) & _10bps) {
1644 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1646 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1649 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1655 static void __rtl8169_check_link_status(struct net_device *dev,
1656 struct rtl8169_private *tp,
1657 void __iomem *ioaddr, bool pm)
1659 if (tp->link_ok(ioaddr)) {
1660 rtl_link_chg_patch(tp);
1661 /* This is to cancel a scheduled suspend if there's one. */
1663 pm_request_resume(&tp->pci_dev->dev);
1664 netif_carrier_on(dev);
1665 if (net_ratelimit())
1666 netif_info(tp, ifup, dev, "link up\n");
1668 netif_carrier_off(dev);
1669 netif_info(tp, ifdown, dev, "link down\n");
1671 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
1675 static void rtl8169_check_link_status(struct net_device *dev,
1676 struct rtl8169_private *tp,
1677 void __iomem *ioaddr)
1679 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1682 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1684 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1686 void __iomem *ioaddr = tp->mmio_addr;
1690 options = RTL_R8(Config1);
1691 if (!(options & PMEnable))
1694 options = RTL_R8(Config3);
1695 if (options & LinkUp)
1696 wolopts |= WAKE_PHY;
1697 switch (tp->mac_version) {
1698 case RTL_GIGA_MAC_VER_34:
1699 case RTL_GIGA_MAC_VER_35:
1700 case RTL_GIGA_MAC_VER_36:
1701 case RTL_GIGA_MAC_VER_37:
1702 case RTL_GIGA_MAC_VER_38:
1703 case RTL_GIGA_MAC_VER_40:
1704 case RTL_GIGA_MAC_VER_41:
1705 case RTL_GIGA_MAC_VER_42:
1706 case RTL_GIGA_MAC_VER_43:
1707 case RTL_GIGA_MAC_VER_44:
1708 case RTL_GIGA_MAC_VER_45:
1709 case RTL_GIGA_MAC_VER_46:
1710 case RTL_GIGA_MAC_VER_47:
1711 case RTL_GIGA_MAC_VER_48:
1712 case RTL_GIGA_MAC_VER_49:
1713 case RTL_GIGA_MAC_VER_50:
1714 case RTL_GIGA_MAC_VER_51:
1715 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1716 wolopts |= WAKE_MAGIC;
1719 if (options & MagicPacket)
1720 wolopts |= WAKE_MAGIC;
1724 options = RTL_R8(Config5);
1726 wolopts |= WAKE_UCAST;
1728 wolopts |= WAKE_BCAST;
1730 wolopts |= WAKE_MCAST;
1735 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1737 struct rtl8169_private *tp = netdev_priv(dev);
1738 struct device *d = &tp->pci_dev->dev;
1740 pm_runtime_get_noresume(d);
1744 wol->supported = WAKE_ANY;
1745 if (pm_runtime_active(d))
1746 wol->wolopts = __rtl8169_get_wol(tp);
1748 wol->wolopts = tp->saved_wolopts;
1750 rtl_unlock_work(tp);
1752 pm_runtime_put_noidle(d);
1755 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1757 void __iomem *ioaddr = tp->mmio_addr;
1758 unsigned int i, tmp;
1759 static const struct {
1764 { WAKE_PHY, Config3, LinkUp },
1765 { WAKE_UCAST, Config5, UWF },
1766 { WAKE_BCAST, Config5, BWF },
1767 { WAKE_MCAST, Config5, MWF },
1768 { WAKE_ANY, Config5, LanWake },
1769 { WAKE_MAGIC, Config3, MagicPacket }
1773 RTL_W8(Cfg9346, Cfg9346_Unlock);
1775 switch (tp->mac_version) {
1776 case RTL_GIGA_MAC_VER_34:
1777 case RTL_GIGA_MAC_VER_35:
1778 case RTL_GIGA_MAC_VER_36:
1779 case RTL_GIGA_MAC_VER_37:
1780 case RTL_GIGA_MAC_VER_38:
1781 case RTL_GIGA_MAC_VER_40:
1782 case RTL_GIGA_MAC_VER_41:
1783 case RTL_GIGA_MAC_VER_42:
1784 case RTL_GIGA_MAC_VER_43:
1785 case RTL_GIGA_MAC_VER_44:
1786 case RTL_GIGA_MAC_VER_45:
1787 case RTL_GIGA_MAC_VER_46:
1788 case RTL_GIGA_MAC_VER_47:
1789 case RTL_GIGA_MAC_VER_48:
1790 case RTL_GIGA_MAC_VER_49:
1791 case RTL_GIGA_MAC_VER_50:
1792 case RTL_GIGA_MAC_VER_51:
1793 tmp = ARRAY_SIZE(cfg) - 1;
1794 if (wolopts & WAKE_MAGIC)
1810 tmp = ARRAY_SIZE(cfg);
1814 for (i = 0; i < tmp; i++) {
1815 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1816 if (wolopts & cfg[i].opt)
1817 options |= cfg[i].mask;
1818 RTL_W8(cfg[i].reg, options);
1821 switch (tp->mac_version) {
1822 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1823 options = RTL_R8(Config1) & ~PMEnable;
1825 options |= PMEnable;
1826 RTL_W8(Config1, options);
1829 options = RTL_R8(Config2) & ~PME_SIGNAL;
1831 options |= PME_SIGNAL;
1832 RTL_W8(Config2, options);
1836 RTL_W8(Cfg9346, Cfg9346_Lock);
1839 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1841 struct rtl8169_private *tp = netdev_priv(dev);
1842 struct device *d = &tp->pci_dev->dev;
1844 pm_runtime_get_noresume(d);
1849 tp->features |= RTL_FEATURE_WOL;
1851 tp->features &= ~RTL_FEATURE_WOL;
1852 if (pm_runtime_active(d))
1853 __rtl8169_set_wol(tp, wol->wolopts);
1855 tp->saved_wolopts = wol->wolopts;
1857 rtl_unlock_work(tp);
1859 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1861 pm_runtime_put_noidle(d);
1866 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1868 return rtl_chip_infos[tp->mac_version].fw_name;
1871 static void rtl8169_get_drvinfo(struct net_device *dev,
1872 struct ethtool_drvinfo *info)
1874 struct rtl8169_private *tp = netdev_priv(dev);
1875 struct rtl_fw *rtl_fw = tp->rtl_fw;
1877 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1878 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1879 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1880 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1881 if (!IS_ERR_OR_NULL(rtl_fw))
1882 strlcpy(info->fw_version, rtl_fw->version,
1883 sizeof(info->fw_version));
1886 static int rtl8169_get_regs_len(struct net_device *dev)
1888 return R8169_REGS_SIZE;
1891 static int rtl8169_set_speed_tbi(struct net_device *dev,
1892 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1894 struct rtl8169_private *tp = netdev_priv(dev);
1895 void __iomem *ioaddr = tp->mmio_addr;
1899 reg = RTL_R32(TBICSR);
1900 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1901 (duplex == DUPLEX_FULL)) {
1902 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1903 } else if (autoneg == AUTONEG_ENABLE)
1904 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1906 netif_warn(tp, link, dev,
1907 "incorrect speed setting refused in TBI mode\n");
1914 static int rtl8169_set_speed_xmii(struct net_device *dev,
1915 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1917 struct rtl8169_private *tp = netdev_priv(dev);
1918 int giga_ctrl, bmcr;
1921 rtl_writephy(tp, 0x1f, 0x0000);
1923 if (autoneg == AUTONEG_ENABLE) {
1926 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1927 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1928 ADVERTISE_100HALF | ADVERTISE_100FULL);
1930 if (adv & ADVERTISED_10baseT_Half)
1931 auto_nego |= ADVERTISE_10HALF;
1932 if (adv & ADVERTISED_10baseT_Full)
1933 auto_nego |= ADVERTISE_10FULL;
1934 if (adv & ADVERTISED_100baseT_Half)
1935 auto_nego |= ADVERTISE_100HALF;
1936 if (adv & ADVERTISED_100baseT_Full)
1937 auto_nego |= ADVERTISE_100FULL;
1939 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1941 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1942 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1944 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1945 if (tp->mii.supports_gmii) {
1946 if (adv & ADVERTISED_1000baseT_Half)
1947 giga_ctrl |= ADVERTISE_1000HALF;
1948 if (adv & ADVERTISED_1000baseT_Full)
1949 giga_ctrl |= ADVERTISE_1000FULL;
1950 } else if (adv & (ADVERTISED_1000baseT_Half |
1951 ADVERTISED_1000baseT_Full)) {
1952 netif_info(tp, link, dev,
1953 "PHY does not support 1000Mbps\n");
1957 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1959 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1960 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1964 if (speed == SPEED_10)
1966 else if (speed == SPEED_100)
1967 bmcr = BMCR_SPEED100;
1971 if (duplex == DUPLEX_FULL)
1972 bmcr |= BMCR_FULLDPLX;
1975 rtl_writephy(tp, MII_BMCR, bmcr);
1977 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1978 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1979 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1980 rtl_writephy(tp, 0x17, 0x2138);
1981 rtl_writephy(tp, 0x0e, 0x0260);
1983 rtl_writephy(tp, 0x17, 0x2108);
1984 rtl_writephy(tp, 0x0e, 0x0000);
1993 static int rtl8169_set_speed(struct net_device *dev,
1994 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1996 struct rtl8169_private *tp = netdev_priv(dev);
1999 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
2003 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
2004 (advertising & ADVERTISED_1000baseT_Full) &&
2005 !pci_is_pcie(tp->pci_dev)) {
2006 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
2012 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2014 struct rtl8169_private *tp = netdev_priv(dev);
2017 del_timer_sync(&tp->timer);
2020 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
2021 cmd->duplex, cmd->advertising);
2022 rtl_unlock_work(tp);
2027 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2028 netdev_features_t features)
2030 struct rtl8169_private *tp = netdev_priv(dev);
2032 if (dev->mtu > TD_MSS_MAX)
2033 features &= ~NETIF_F_ALL_TSO;
2035 if (dev->mtu > JUMBO_1K &&
2036 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2037 features &= ~NETIF_F_IP_CSUM;
2042 static void __rtl8169_set_features(struct net_device *dev,
2043 netdev_features_t features)
2045 struct rtl8169_private *tp = netdev_priv(dev);
2046 void __iomem *ioaddr = tp->mmio_addr;
2049 rx_config = RTL_R32(RxConfig);
2050 if (features & NETIF_F_RXALL)
2051 rx_config |= (AcceptErr | AcceptRunt);
2053 rx_config &= ~(AcceptErr | AcceptRunt);
2055 RTL_W32(RxConfig, rx_config);
2057 if (features & NETIF_F_RXCSUM)
2058 tp->cp_cmd |= RxChkSum;
2060 tp->cp_cmd &= ~RxChkSum;
2062 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2063 tp->cp_cmd |= RxVlan;
2065 tp->cp_cmd &= ~RxVlan;
2067 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2069 RTL_W16(CPlusCmd, tp->cp_cmd);
2073 static int rtl8169_set_features(struct net_device *dev,
2074 netdev_features_t features)
2076 struct rtl8169_private *tp = netdev_priv(dev);
2078 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2081 if (features ^ dev->features)
2082 __rtl8169_set_features(dev, features);
2083 rtl_unlock_work(tp);
2089 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
2091 return (skb_vlan_tag_present(skb)) ?
2092 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
2095 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
2097 u32 opts2 = le32_to_cpu(desc->opts2);
2099 if (opts2 & RxVlanTag)
2100 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
2103 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
2105 struct rtl8169_private *tp = netdev_priv(dev);
2106 void __iomem *ioaddr = tp->mmio_addr;
2110 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
2111 cmd->port = PORT_FIBRE;
2112 cmd->transceiver = XCVR_INTERNAL;
2114 status = RTL_R32(TBICSR);
2115 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2116 cmd->autoneg = !!(status & TBINwEnable);
2118 ethtool_cmd_speed_set(cmd, SPEED_1000);
2119 cmd->duplex = DUPLEX_FULL; /* Always set */
2124 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
2126 struct rtl8169_private *tp = netdev_priv(dev);
2128 return mii_ethtool_gset(&tp->mii, cmd);
2131 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2133 struct rtl8169_private *tp = netdev_priv(dev);
2137 rc = tp->get_settings(dev, cmd);
2138 rtl_unlock_work(tp);
2143 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2146 struct rtl8169_private *tp = netdev_priv(dev);
2147 u32 __iomem *data = tp->mmio_addr;
2152 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2153 memcpy_fromio(dw++, data++, 4);
2154 rtl_unlock_work(tp);
2157 static u32 rtl8169_get_msglevel(struct net_device *dev)
2159 struct rtl8169_private *tp = netdev_priv(dev);
2161 return tp->msg_enable;
2164 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2166 struct rtl8169_private *tp = netdev_priv(dev);
2168 tp->msg_enable = value;
2171 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2178 "tx_single_collisions",
2179 "tx_multi_collisions",
2187 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
2191 return ARRAY_SIZE(rtl8169_gstrings);
2197 DECLARE_RTL_COND(rtl_counters_cond)
2199 void __iomem *ioaddr = tp->mmio_addr;
2201 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
2204 static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
2206 struct rtl8169_private *tp = netdev_priv(dev);
2207 void __iomem *ioaddr = tp->mmio_addr;
2208 dma_addr_t paddr = tp->counters_phys_addr;
2211 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
2212 RTL_R32(CounterAddrHigh);
2213 cmd = (u64)paddr & DMA_BIT_MASK(32);
2214 RTL_W32(CounterAddrLow, cmd);
2215 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2217 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
2220 static bool rtl8169_reset_counters(struct net_device *dev)
2222 struct rtl8169_private *tp = netdev_priv(dev);
2225 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2228 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2231 return rtl8169_do_counters(dev, CounterReset);
2234 static bool rtl8169_update_counters(struct net_device *dev)
2236 struct rtl8169_private *tp = netdev_priv(dev);
2237 void __iomem *ioaddr = tp->mmio_addr;
2240 * Some chips are unable to dump tally counters when the receiver
2243 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
2246 return rtl8169_do_counters(dev, CounterDump);
2249 static bool rtl8169_init_counter_offsets(struct net_device *dev)
2251 struct rtl8169_private *tp = netdev_priv(dev);
2252 struct rtl8169_counters *counters = tp->counters;
2256 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2257 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2258 * reset by a power cycle, while the counter values collected by the
2259 * driver are reset at every driver unload/load cycle.
2261 * To make sure the HW values returned by @get_stats64 match the SW
2262 * values, we collect the initial values at first open(*) and use them
2263 * as offsets to normalize the values returned by @get_stats64.
2265 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2266 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2267 * set at open time by rtl_hw_start.
2270 if (tp->tc_offset.inited)
2273 /* If both, reset and update fail, propagate to caller. */
2274 if (rtl8169_reset_counters(dev))
2277 if (rtl8169_update_counters(dev))
2280 tp->tc_offset.tx_errors = counters->tx_errors;
2281 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2282 tp->tc_offset.tx_aborted = counters->tx_aborted;
2283 tp->tc_offset.inited = true;
2288 static void rtl8169_get_ethtool_stats(struct net_device *dev,
2289 struct ethtool_stats *stats, u64 *data)
2291 struct rtl8169_private *tp = netdev_priv(dev);
2292 struct device *d = &tp->pci_dev->dev;
2293 struct rtl8169_counters *counters = tp->counters;
2297 pm_runtime_get_noresume(d);
2299 if (pm_runtime_active(d))
2300 rtl8169_update_counters(dev);
2302 pm_runtime_put_noidle(d);
2304 data[0] = le64_to_cpu(counters->tx_packets);
2305 data[1] = le64_to_cpu(counters->rx_packets);
2306 data[2] = le64_to_cpu(counters->tx_errors);
2307 data[3] = le32_to_cpu(counters->rx_errors);
2308 data[4] = le16_to_cpu(counters->rx_missed);
2309 data[5] = le16_to_cpu(counters->align_errors);
2310 data[6] = le32_to_cpu(counters->tx_one_collision);
2311 data[7] = le32_to_cpu(counters->tx_multi_collision);
2312 data[8] = le64_to_cpu(counters->rx_unicast);
2313 data[9] = le64_to_cpu(counters->rx_broadcast);
2314 data[10] = le32_to_cpu(counters->rx_multicast);
2315 data[11] = le16_to_cpu(counters->tx_aborted);
2316 data[12] = le16_to_cpu(counters->tx_underun);
2319 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2323 memcpy(data, rtl8169_gstrings, sizeof(rtl8169_gstrings));
2328 static const struct ethtool_ops rtl8169_ethtool_ops = {
2329 .get_drvinfo = rtl8169_get_drvinfo,
2330 .get_regs_len = rtl8169_get_regs_len,
2331 .get_link = ethtool_op_get_link,
2332 .get_settings = rtl8169_get_settings,
2333 .set_settings = rtl8169_set_settings,
2334 .get_msglevel = rtl8169_get_msglevel,
2335 .set_msglevel = rtl8169_set_msglevel,
2336 .get_regs = rtl8169_get_regs,
2337 .get_wol = rtl8169_get_wol,
2338 .set_wol = rtl8169_set_wol,
2339 .get_strings = rtl8169_get_strings,
2340 .get_sset_count = rtl8169_get_sset_count,
2341 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2342 .get_ts_info = ethtool_op_get_ts_info,
2345 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2346 struct net_device *dev, u8 default_version)
2348 void __iomem *ioaddr = tp->mmio_addr;
2350 * The driver currently handles the 8168Bf and the 8168Be identically
2351 * but they can be identified more specifically through the test below
2354 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2356 * Same thing for the 8101Eb and the 8101Ec:
2358 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2360 static const struct rtl_mac_info {
2365 /* 8168EP family. */
2366 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2367 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2368 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2371 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2372 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2375 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2376 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2377 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2378 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2381 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2382 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2383 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2386 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2387 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2388 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2389 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2392 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2393 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2394 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2396 /* 8168DP family. */
2397 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2398 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2399 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2402 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
2403 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2404 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2405 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2406 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2407 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2408 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2409 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
2410 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2413 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2414 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2415 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2416 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2419 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2420 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2421 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2422 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
2423 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2424 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2425 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2426 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2427 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2428 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2429 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2430 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2431 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2432 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2433 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2434 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2435 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2436 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2437 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2438 /* FIXME: where did these entries come from ? -- FR */
2439 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2440 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2443 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2444 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2445 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2446 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2447 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2448 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2451 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2453 const struct rtl_mac_info *p = mac_info;
2456 reg = RTL_R32(TxConfig);
2457 while ((reg & p->mask) != p->val)
2459 tp->mac_version = p->mac_version;
2461 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2462 netif_notice(tp, probe, dev,
2463 "unknown MAC, using family default\n");
2464 tp->mac_version = default_version;
2465 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2466 tp->mac_version = tp->mii.supports_gmii ?
2467 RTL_GIGA_MAC_VER_42 :
2468 RTL_GIGA_MAC_VER_43;
2469 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2470 tp->mac_version = tp->mii.supports_gmii ?
2471 RTL_GIGA_MAC_VER_45 :
2472 RTL_GIGA_MAC_VER_47;
2473 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2474 tp->mac_version = tp->mii.supports_gmii ?
2475 RTL_GIGA_MAC_VER_46 :
2476 RTL_GIGA_MAC_VER_48;
2480 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2482 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2490 static void rtl_writephy_batch(struct rtl8169_private *tp,
2491 const struct phy_reg *regs, int len)
2494 rtl_writephy(tp, regs->reg, regs->val);
2499 #define PHY_READ 0x00000000
2500 #define PHY_DATA_OR 0x10000000
2501 #define PHY_DATA_AND 0x20000000
2502 #define PHY_BJMPN 0x30000000
2503 #define PHY_MDIO_CHG 0x40000000
2504 #define PHY_CLEAR_READCOUNT 0x70000000
2505 #define PHY_WRITE 0x80000000
2506 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2507 #define PHY_COMP_EQ_SKIPN 0xa0000000
2508 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2509 #define PHY_WRITE_PREVIOUS 0xc0000000
2510 #define PHY_SKIPN 0xd0000000
2511 #define PHY_DELAY_MS 0xe0000000
2515 char version[RTL_VER_SIZE];
2521 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2523 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2525 const struct firmware *fw = rtl_fw->fw;
2526 struct fw_info *fw_info = (struct fw_info *)fw->data;
2527 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2528 char *version = rtl_fw->version;
2531 if (fw->size < FW_OPCODE_SIZE)
2534 if (!fw_info->magic) {
2535 size_t i, size, start;
2538 if (fw->size < sizeof(*fw_info))
2541 for (i = 0; i < fw->size; i++)
2542 checksum += fw->data[i];
2546 start = le32_to_cpu(fw_info->fw_start);
2547 if (start > fw->size)
2550 size = le32_to_cpu(fw_info->fw_len);
2551 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2554 memcpy(version, fw_info->version, RTL_VER_SIZE);
2556 pa->code = (__le32 *)(fw->data + start);
2559 if (fw->size % FW_OPCODE_SIZE)
2562 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2564 pa->code = (__le32 *)fw->data;
2565 pa->size = fw->size / FW_OPCODE_SIZE;
2567 version[RTL_VER_SIZE - 1] = 0;
2574 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2575 struct rtl_fw_phy_action *pa)
2580 for (index = 0; index < pa->size; index++) {
2581 u32 action = le32_to_cpu(pa->code[index]);
2582 u32 regno = (action & 0x0fff0000) >> 16;
2584 switch(action & 0xf0000000) {
2589 case PHY_CLEAR_READCOUNT:
2591 case PHY_WRITE_PREVIOUS:
2596 if (regno > index) {
2597 netif_err(tp, ifup, tp->dev,
2598 "Out of range of firmware\n");
2602 case PHY_READCOUNT_EQ_SKIP:
2603 if (index + 2 >= pa->size) {
2604 netif_err(tp, ifup, tp->dev,
2605 "Out of range of firmware\n");
2609 case PHY_COMP_EQ_SKIPN:
2610 case PHY_COMP_NEQ_SKIPN:
2612 if (index + 1 + regno >= pa->size) {
2613 netif_err(tp, ifup, tp->dev,
2614 "Out of range of firmware\n");
2620 netif_err(tp, ifup, tp->dev,
2621 "Invalid action 0x%08x\n", action);
2630 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2632 struct net_device *dev = tp->dev;
2635 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2636 netif_err(tp, ifup, dev, "invalid firmware\n");
2640 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2646 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2648 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2649 struct mdio_ops org, *ops = &tp->mdio_ops;
2653 predata = count = 0;
2654 org.write = ops->write;
2655 org.read = ops->read;
2657 for (index = 0; index < pa->size; ) {
2658 u32 action = le32_to_cpu(pa->code[index]);
2659 u32 data = action & 0x0000ffff;
2660 u32 regno = (action & 0x0fff0000) >> 16;
2665 switch(action & 0xf0000000) {
2667 predata = rtl_readphy(tp, regno);
2684 ops->write = org.write;
2685 ops->read = org.read;
2686 } else if (data == 1) {
2687 ops->write = mac_mcu_write;
2688 ops->read = mac_mcu_read;
2693 case PHY_CLEAR_READCOUNT:
2698 rtl_writephy(tp, regno, data);
2701 case PHY_READCOUNT_EQ_SKIP:
2702 index += (count == data) ? 2 : 1;
2704 case PHY_COMP_EQ_SKIPN:
2705 if (predata == data)
2709 case PHY_COMP_NEQ_SKIPN:
2710 if (predata != data)
2714 case PHY_WRITE_PREVIOUS:
2715 rtl_writephy(tp, regno, predata);
2731 ops->write = org.write;
2732 ops->read = org.read;
2735 static void rtl_release_firmware(struct rtl8169_private *tp)
2737 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2738 release_firmware(tp->rtl_fw->fw);
2741 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2744 static void rtl_apply_firmware(struct rtl8169_private *tp)
2746 struct rtl_fw *rtl_fw = tp->rtl_fw;
2748 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2749 if (!IS_ERR_OR_NULL(rtl_fw))
2750 rtl_phy_write_fw(tp, rtl_fw);
2753 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2755 if (rtl_readphy(tp, reg) != val)
2756 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2758 rtl_apply_firmware(tp);
2761 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2763 static const struct phy_reg phy_reg_init[] = {
2825 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2828 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2830 static const struct phy_reg phy_reg_init[] = {
2836 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2839 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2841 struct pci_dev *pdev = tp->pci_dev;
2843 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2844 (pdev->subsystem_device != 0xe000))
2847 rtl_writephy(tp, 0x1f, 0x0001);
2848 rtl_writephy(tp, 0x10, 0xf01b);
2849 rtl_writephy(tp, 0x1f, 0x0000);
2852 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2854 static const struct phy_reg phy_reg_init[] = {
2894 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2896 rtl8169scd_hw_phy_config_quirk(tp);
2899 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2901 static const struct phy_reg phy_reg_init[] = {
2949 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2952 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2954 static const struct phy_reg phy_reg_init[] = {
2959 rtl_writephy(tp, 0x1f, 0x0001);
2960 rtl_patchphy(tp, 0x16, 1 << 0);
2962 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2965 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2967 static const struct phy_reg phy_reg_init[] = {
2973 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2976 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2978 static const struct phy_reg phy_reg_init[] = {
2986 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2989 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2991 static const struct phy_reg phy_reg_init[] = {
2997 rtl_writephy(tp, 0x1f, 0x0000);
2998 rtl_patchphy(tp, 0x14, 1 << 5);
2999 rtl_patchphy(tp, 0x0d, 1 << 5);
3001 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3004 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
3006 static const struct phy_reg phy_reg_init[] = {
3026 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3028 rtl_patchphy(tp, 0x14, 1 << 5);
3029 rtl_patchphy(tp, 0x0d, 1 << 5);
3030 rtl_writephy(tp, 0x1f, 0x0000);
3033 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
3035 static const struct phy_reg phy_reg_init[] = {
3053 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3055 rtl_patchphy(tp, 0x16, 1 << 0);
3056 rtl_patchphy(tp, 0x14, 1 << 5);
3057 rtl_patchphy(tp, 0x0d, 1 << 5);
3058 rtl_writephy(tp, 0x1f, 0x0000);
3061 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
3063 static const struct phy_reg phy_reg_init[] = {
3075 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3077 rtl_patchphy(tp, 0x16, 1 << 0);
3078 rtl_patchphy(tp, 0x14, 1 << 5);
3079 rtl_patchphy(tp, 0x0d, 1 << 5);
3080 rtl_writephy(tp, 0x1f, 0x0000);
3083 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
3085 rtl8168c_3_hw_phy_config(tp);
3088 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
3090 static const struct phy_reg phy_reg_init_0[] = {
3091 /* Channel Estimation */
3112 * Enhance line driver power
3121 * Can not link to 1Gbps with bad cable
3122 * Decrease SNR threshold form 21.07dB to 19.04dB
3131 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3135 * Fine Tune Switching regulator parameter
3137 rtl_writephy(tp, 0x1f, 0x0002);
3138 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3139 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
3141 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3142 static const struct phy_reg phy_reg_init[] = {
3152 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3154 val = rtl_readphy(tp, 0x0d);
3156 if ((val & 0x00ff) != 0x006c) {
3157 static const u32 set[] = {
3158 0x0065, 0x0066, 0x0067, 0x0068,
3159 0x0069, 0x006a, 0x006b, 0x006c
3163 rtl_writephy(tp, 0x1f, 0x0002);
3166 for (i = 0; i < ARRAY_SIZE(set); i++)
3167 rtl_writephy(tp, 0x0d, val | set[i]);
3170 static const struct phy_reg phy_reg_init[] = {
3178 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3181 /* RSET couple improve */
3182 rtl_writephy(tp, 0x1f, 0x0002);
3183 rtl_patchphy(tp, 0x0d, 0x0300);
3184 rtl_patchphy(tp, 0x0f, 0x0010);
3186 /* Fine tune PLL performance */
3187 rtl_writephy(tp, 0x1f, 0x0002);
3188 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3189 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3191 rtl_writephy(tp, 0x1f, 0x0005);
3192 rtl_writephy(tp, 0x05, 0x001b);
3194 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
3196 rtl_writephy(tp, 0x1f, 0x0000);
3199 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
3201 static const struct phy_reg phy_reg_init_0[] = {
3202 /* Channel Estimation */
3223 * Enhance line driver power
3232 * Can not link to 1Gbps with bad cable
3233 * Decrease SNR threshold form 21.07dB to 19.04dB
3242 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3244 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3245 static const struct phy_reg phy_reg_init[] = {
3256 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3258 val = rtl_readphy(tp, 0x0d);
3259 if ((val & 0x00ff) != 0x006c) {
3260 static const u32 set[] = {
3261 0x0065, 0x0066, 0x0067, 0x0068,
3262 0x0069, 0x006a, 0x006b, 0x006c
3266 rtl_writephy(tp, 0x1f, 0x0002);
3269 for (i = 0; i < ARRAY_SIZE(set); i++)
3270 rtl_writephy(tp, 0x0d, val | set[i]);
3273 static const struct phy_reg phy_reg_init[] = {
3281 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3284 /* Fine tune PLL performance */
3285 rtl_writephy(tp, 0x1f, 0x0002);
3286 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3287 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3289 /* Switching regulator Slew rate */
3290 rtl_writephy(tp, 0x1f, 0x0002);
3291 rtl_patchphy(tp, 0x0f, 0x0017);
3293 rtl_writephy(tp, 0x1f, 0x0005);
3294 rtl_writephy(tp, 0x05, 0x001b);
3296 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
3298 rtl_writephy(tp, 0x1f, 0x0000);
3301 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
3303 static const struct phy_reg phy_reg_init[] = {
3359 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3362 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3364 static const struct phy_reg phy_reg_init[] = {
3374 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3375 rtl_patchphy(tp, 0x0d, 1 << 5);
3378 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3380 static const struct phy_reg phy_reg_init[] = {
3381 /* Enable Delay cap */
3387 /* Channel estimation fine tune */
3396 /* Update PFM & 10M TX idle timer */
3408 rtl_apply_firmware(tp);
3410 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3412 /* DCO enable for 10M IDLE Power */
3413 rtl_writephy(tp, 0x1f, 0x0007);
3414 rtl_writephy(tp, 0x1e, 0x0023);
3415 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3416 rtl_writephy(tp, 0x1f, 0x0000);
3418 /* For impedance matching */
3419 rtl_writephy(tp, 0x1f, 0x0002);
3420 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3421 rtl_writephy(tp, 0x1f, 0x0000);
3423 /* PHY auto speed down */
3424 rtl_writephy(tp, 0x1f, 0x0007);
3425 rtl_writephy(tp, 0x1e, 0x002d);
3426 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3427 rtl_writephy(tp, 0x1f, 0x0000);
3428 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3430 rtl_writephy(tp, 0x1f, 0x0005);
3431 rtl_writephy(tp, 0x05, 0x8b86);
3432 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3433 rtl_writephy(tp, 0x1f, 0x0000);
3435 rtl_writephy(tp, 0x1f, 0x0005);
3436 rtl_writephy(tp, 0x05, 0x8b85);
3437 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3438 rtl_writephy(tp, 0x1f, 0x0007);
3439 rtl_writephy(tp, 0x1e, 0x0020);
3440 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3441 rtl_writephy(tp, 0x1f, 0x0006);
3442 rtl_writephy(tp, 0x00, 0x5a00);
3443 rtl_writephy(tp, 0x1f, 0x0000);
3444 rtl_writephy(tp, 0x0d, 0x0007);
3445 rtl_writephy(tp, 0x0e, 0x003c);
3446 rtl_writephy(tp, 0x0d, 0x4007);
3447 rtl_writephy(tp, 0x0e, 0x0000);
3448 rtl_writephy(tp, 0x0d, 0x0000);
3451 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3454 addr[0] | (addr[1] << 8),
3455 addr[2] | (addr[3] << 8),
3456 addr[4] | (addr[5] << 8)
3458 const struct exgmac_reg e[] = {
3459 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3460 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3461 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3462 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3465 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3468 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3470 static const struct phy_reg phy_reg_init[] = {
3471 /* Enable Delay cap */
3480 /* Channel estimation fine tune */
3497 rtl_apply_firmware(tp);
3499 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3501 /* For 4-corner performance improve */
3502 rtl_writephy(tp, 0x1f, 0x0005);
3503 rtl_writephy(tp, 0x05, 0x8b80);
3504 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3505 rtl_writephy(tp, 0x1f, 0x0000);
3507 /* PHY auto speed down */
3508 rtl_writephy(tp, 0x1f, 0x0004);
3509 rtl_writephy(tp, 0x1f, 0x0007);
3510 rtl_writephy(tp, 0x1e, 0x002d);
3511 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3512 rtl_writephy(tp, 0x1f, 0x0002);
3513 rtl_writephy(tp, 0x1f, 0x0000);
3514 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3516 /* improve 10M EEE waveform */
3517 rtl_writephy(tp, 0x1f, 0x0005);
3518 rtl_writephy(tp, 0x05, 0x8b86);
3519 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3520 rtl_writephy(tp, 0x1f, 0x0000);
3522 /* Improve 2-pair detection performance */
3523 rtl_writephy(tp, 0x1f, 0x0005);
3524 rtl_writephy(tp, 0x05, 0x8b85);
3525 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3526 rtl_writephy(tp, 0x1f, 0x0000);
3529 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
3530 rtl_writephy(tp, 0x1f, 0x0005);
3531 rtl_writephy(tp, 0x05, 0x8b85);
3532 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3533 rtl_writephy(tp, 0x1f, 0x0004);
3534 rtl_writephy(tp, 0x1f, 0x0007);
3535 rtl_writephy(tp, 0x1e, 0x0020);
3536 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3537 rtl_writephy(tp, 0x1f, 0x0002);
3538 rtl_writephy(tp, 0x1f, 0x0000);
3539 rtl_writephy(tp, 0x0d, 0x0007);
3540 rtl_writephy(tp, 0x0e, 0x003c);
3541 rtl_writephy(tp, 0x0d, 0x4007);
3542 rtl_writephy(tp, 0x0e, 0x0000);
3543 rtl_writephy(tp, 0x0d, 0x0000);
3546 rtl_writephy(tp, 0x1f, 0x0003);
3547 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3548 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3549 rtl_writephy(tp, 0x1f, 0x0000);
3551 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3552 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3555 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3557 /* For 4-corner performance improve */
3558 rtl_writephy(tp, 0x1f, 0x0005);
3559 rtl_writephy(tp, 0x05, 0x8b80);
3560 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3561 rtl_writephy(tp, 0x1f, 0x0000);
3563 /* PHY auto speed down */
3564 rtl_writephy(tp, 0x1f, 0x0007);
3565 rtl_writephy(tp, 0x1e, 0x002d);
3566 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3567 rtl_writephy(tp, 0x1f, 0x0000);
3568 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3570 /* Improve 10M EEE waveform */
3571 rtl_writephy(tp, 0x1f, 0x0005);
3572 rtl_writephy(tp, 0x05, 0x8b86);
3573 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3574 rtl_writephy(tp, 0x1f, 0x0000);
3577 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3579 static const struct phy_reg phy_reg_init[] = {
3580 /* Channel estimation fine tune */
3585 /* Modify green table for giga & fnet */
3602 /* Modify green table for 10M */
3608 /* Disable hiimpedance detection (RTCT) */
3614 rtl_apply_firmware(tp);
3616 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3618 rtl8168f_hw_phy_config(tp);
3620 /* Improve 2-pair detection performance */
3621 rtl_writephy(tp, 0x1f, 0x0005);
3622 rtl_writephy(tp, 0x05, 0x8b85);
3623 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3624 rtl_writephy(tp, 0x1f, 0x0000);
3627 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3629 rtl_apply_firmware(tp);
3631 rtl8168f_hw_phy_config(tp);
3634 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3636 static const struct phy_reg phy_reg_init[] = {
3637 /* Channel estimation fine tune */
3642 /* Modify green table for giga & fnet */
3659 /* Modify green table for 10M */
3665 /* Disable hiimpedance detection (RTCT) */
3672 rtl_apply_firmware(tp);
3674 rtl8168f_hw_phy_config(tp);
3676 /* Improve 2-pair detection performance */
3677 rtl_writephy(tp, 0x1f, 0x0005);
3678 rtl_writephy(tp, 0x05, 0x8b85);
3679 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3680 rtl_writephy(tp, 0x1f, 0x0000);
3682 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3684 /* Modify green table for giga */
3685 rtl_writephy(tp, 0x1f, 0x0005);
3686 rtl_writephy(tp, 0x05, 0x8b54);
3687 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3688 rtl_writephy(tp, 0x05, 0x8b5d);
3689 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3690 rtl_writephy(tp, 0x05, 0x8a7c);
3691 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3692 rtl_writephy(tp, 0x05, 0x8a7f);
3693 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3694 rtl_writephy(tp, 0x05, 0x8a82);
3695 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3696 rtl_writephy(tp, 0x05, 0x8a85);
3697 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3698 rtl_writephy(tp, 0x05, 0x8a88);
3699 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3700 rtl_writephy(tp, 0x1f, 0x0000);
3702 /* uc same-seed solution */
3703 rtl_writephy(tp, 0x1f, 0x0005);
3704 rtl_writephy(tp, 0x05, 0x8b85);
3705 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3706 rtl_writephy(tp, 0x1f, 0x0000);
3709 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3710 rtl_writephy(tp, 0x1f, 0x0005);
3711 rtl_writephy(tp, 0x05, 0x8b85);
3712 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3713 rtl_writephy(tp, 0x1f, 0x0004);
3714 rtl_writephy(tp, 0x1f, 0x0007);
3715 rtl_writephy(tp, 0x1e, 0x0020);
3716 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3717 rtl_writephy(tp, 0x1f, 0x0000);
3718 rtl_writephy(tp, 0x0d, 0x0007);
3719 rtl_writephy(tp, 0x0e, 0x003c);
3720 rtl_writephy(tp, 0x0d, 0x4007);
3721 rtl_writephy(tp, 0x0e, 0x0000);
3722 rtl_writephy(tp, 0x0d, 0x0000);
3725 rtl_writephy(tp, 0x1f, 0x0003);
3726 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3727 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3728 rtl_writephy(tp, 0x1f, 0x0000);
3731 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3733 rtl_apply_firmware(tp);
3735 rtl_writephy(tp, 0x1f, 0x0a46);
3736 if (rtl_readphy(tp, 0x10) & 0x0100) {
3737 rtl_writephy(tp, 0x1f, 0x0bcc);
3738 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3740 rtl_writephy(tp, 0x1f, 0x0bcc);
3741 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3744 rtl_writephy(tp, 0x1f, 0x0a46);
3745 if (rtl_readphy(tp, 0x13) & 0x0100) {
3746 rtl_writephy(tp, 0x1f, 0x0c41);
3747 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3749 rtl_writephy(tp, 0x1f, 0x0c41);
3750 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3753 /* Enable PHY auto speed down */
3754 rtl_writephy(tp, 0x1f, 0x0a44);
3755 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3757 rtl_writephy(tp, 0x1f, 0x0bcc);
3758 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
3759 rtl_writephy(tp, 0x1f, 0x0a44);
3760 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3761 rtl_writephy(tp, 0x1f, 0x0a43);
3762 rtl_writephy(tp, 0x13, 0x8084);
3763 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3764 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3766 /* EEE auto-fallback function */
3767 rtl_writephy(tp, 0x1f, 0x0a4b);
3768 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3770 /* Enable UC LPF tune function */
3771 rtl_writephy(tp, 0x1f, 0x0a43);
3772 rtl_writephy(tp, 0x13, 0x8012);
3773 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3775 rtl_writephy(tp, 0x1f, 0x0c42);
3776 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3778 /* Improve SWR Efficiency */
3779 rtl_writephy(tp, 0x1f, 0x0bcd);
3780 rtl_writephy(tp, 0x14, 0x5065);
3781 rtl_writephy(tp, 0x14, 0xd065);
3782 rtl_writephy(tp, 0x1f, 0x0bc8);
3783 rtl_writephy(tp, 0x11, 0x5655);
3784 rtl_writephy(tp, 0x1f, 0x0bcd);
3785 rtl_writephy(tp, 0x14, 0x1065);
3786 rtl_writephy(tp, 0x14, 0x9065);
3787 rtl_writephy(tp, 0x14, 0x1065);
3789 /* Check ALDPS bit, disable it if enabled */
3790 rtl_writephy(tp, 0x1f, 0x0a43);
3791 if (rtl_readphy(tp, 0x10) & 0x0004)
3792 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3794 rtl_writephy(tp, 0x1f, 0x0000);
3797 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3799 rtl_apply_firmware(tp);
3802 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3807 rtl_apply_firmware(tp);
3809 /* CHN EST parameters adjust - giga master */
3810 rtl_writephy(tp, 0x1f, 0x0a43);
3811 rtl_writephy(tp, 0x13, 0x809b);
3812 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3813 rtl_writephy(tp, 0x13, 0x80a2);
3814 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3815 rtl_writephy(tp, 0x13, 0x80a4);
3816 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3817 rtl_writephy(tp, 0x13, 0x809c);
3818 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3819 rtl_writephy(tp, 0x1f, 0x0000);
3821 /* CHN EST parameters adjust - giga slave */
3822 rtl_writephy(tp, 0x1f, 0x0a43);
3823 rtl_writephy(tp, 0x13, 0x80ad);
3824 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
3825 rtl_writephy(tp, 0x13, 0x80b4);
3826 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
3827 rtl_writephy(tp, 0x13, 0x80ac);
3828 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
3829 rtl_writephy(tp, 0x1f, 0x0000);
3831 /* CHN EST parameters adjust - fnet */
3832 rtl_writephy(tp, 0x1f, 0x0a43);
3833 rtl_writephy(tp, 0x13, 0x808e);
3834 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
3835 rtl_writephy(tp, 0x13, 0x8090);
3836 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
3837 rtl_writephy(tp, 0x13, 0x8092);
3838 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
3839 rtl_writephy(tp, 0x1f, 0x0000);
3841 /* enable R-tune & PGA-retune function */
3843 rtl_writephy(tp, 0x1f, 0x0a46);
3844 data = rtl_readphy(tp, 0x13);
3847 dout_tapbin |= data;
3848 data = rtl_readphy(tp, 0x12);
3851 dout_tapbin |= data;
3852 dout_tapbin = ~(dout_tapbin^0x08);
3854 dout_tapbin &= 0xf000;
3855 rtl_writephy(tp, 0x1f, 0x0a43);
3856 rtl_writephy(tp, 0x13, 0x827a);
3857 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3858 rtl_writephy(tp, 0x13, 0x827b);
3859 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3860 rtl_writephy(tp, 0x13, 0x827c);
3861 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3862 rtl_writephy(tp, 0x13, 0x827d);
3863 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3865 rtl_writephy(tp, 0x1f, 0x0a43);
3866 rtl_writephy(tp, 0x13, 0x0811);
3867 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3868 rtl_writephy(tp, 0x1f, 0x0a42);
3869 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3870 rtl_writephy(tp, 0x1f, 0x0000);
3872 /* enable GPHY 10M */
3873 rtl_writephy(tp, 0x1f, 0x0a44);
3874 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3875 rtl_writephy(tp, 0x1f, 0x0000);
3877 /* SAR ADC performance */
3878 rtl_writephy(tp, 0x1f, 0x0bca);
3879 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
3880 rtl_writephy(tp, 0x1f, 0x0000);
3882 rtl_writephy(tp, 0x1f, 0x0a43);
3883 rtl_writephy(tp, 0x13, 0x803f);
3884 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3885 rtl_writephy(tp, 0x13, 0x8047);
3886 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3887 rtl_writephy(tp, 0x13, 0x804f);
3888 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3889 rtl_writephy(tp, 0x13, 0x8057);
3890 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3891 rtl_writephy(tp, 0x13, 0x805f);
3892 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3893 rtl_writephy(tp, 0x13, 0x8067);
3894 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3895 rtl_writephy(tp, 0x13, 0x806f);
3896 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3897 rtl_writephy(tp, 0x1f, 0x0000);
3899 /* disable phy pfm mode */
3900 rtl_writephy(tp, 0x1f, 0x0a44);
3901 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3902 rtl_writephy(tp, 0x1f, 0x0000);
3904 /* Check ALDPS bit, disable it if enabled */
3905 rtl_writephy(tp, 0x1f, 0x0a43);
3906 if (rtl_readphy(tp, 0x10) & 0x0004)
3907 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3909 rtl_writephy(tp, 0x1f, 0x0000);
3912 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3914 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3918 rtl_apply_firmware(tp);
3920 /* CHIN EST parameter update */
3921 rtl_writephy(tp, 0x1f, 0x0a43);
3922 rtl_writephy(tp, 0x13, 0x808a);
3923 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
3924 rtl_writephy(tp, 0x1f, 0x0000);
3926 /* enable R-tune & PGA-retune function */
3927 rtl_writephy(tp, 0x1f, 0x0a43);
3928 rtl_writephy(tp, 0x13, 0x0811);
3929 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3930 rtl_writephy(tp, 0x1f, 0x0a42);
3931 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3932 rtl_writephy(tp, 0x1f, 0x0000);
3934 /* enable GPHY 10M */
3935 rtl_writephy(tp, 0x1f, 0x0a44);
3936 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3937 rtl_writephy(tp, 0x1f, 0x0000);
3939 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3940 data = r8168_mac_ocp_read(tp, 0xdd02);
3941 ioffset_p3 = ((data & 0x80)>>7);
3944 data = r8168_mac_ocp_read(tp, 0xdd00);
3945 ioffset_p3 |= ((data & (0xe000))>>13);
3946 ioffset_p2 = ((data & (0x1e00))>>9);
3947 ioffset_p1 = ((data & (0x01e0))>>5);
3948 ioffset_p0 = ((data & 0x0010)>>4);
3950 ioffset_p0 |= (data & (0x07));
3951 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3953 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3954 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
3955 rtl_writephy(tp, 0x1f, 0x0bcf);
3956 rtl_writephy(tp, 0x16, data);
3957 rtl_writephy(tp, 0x1f, 0x0000);
3960 /* Modify rlen (TX LPF corner frequency) level */
3961 rtl_writephy(tp, 0x1f, 0x0bcd);
3962 data = rtl_readphy(tp, 0x16);
3967 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3968 rtl_writephy(tp, 0x17, data);
3969 rtl_writephy(tp, 0x1f, 0x0bcd);
3970 rtl_writephy(tp, 0x1f, 0x0000);
3972 /* disable phy pfm mode */
3973 rtl_writephy(tp, 0x1f, 0x0a44);
3974 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3975 rtl_writephy(tp, 0x1f, 0x0000);
3977 /* Check ALDPS bit, disable it if enabled */
3978 rtl_writephy(tp, 0x1f, 0x0a43);
3979 if (rtl_readphy(tp, 0x10) & 0x0004)
3980 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3982 rtl_writephy(tp, 0x1f, 0x0000);
3985 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3987 /* Enable PHY auto speed down */
3988 rtl_writephy(tp, 0x1f, 0x0a44);
3989 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3990 rtl_writephy(tp, 0x1f, 0x0000);
3992 /* patch 10M & ALDPS */
3993 rtl_writephy(tp, 0x1f, 0x0bcc);
3994 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3995 rtl_writephy(tp, 0x1f, 0x0a44);
3996 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3997 rtl_writephy(tp, 0x1f, 0x0a43);
3998 rtl_writephy(tp, 0x13, 0x8084);
3999 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4000 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4001 rtl_writephy(tp, 0x1f, 0x0000);
4003 /* Enable EEE auto-fallback function */
4004 rtl_writephy(tp, 0x1f, 0x0a4b);
4005 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4006 rtl_writephy(tp, 0x1f, 0x0000);
4008 /* Enable UC LPF tune function */
4009 rtl_writephy(tp, 0x1f, 0x0a43);
4010 rtl_writephy(tp, 0x13, 0x8012);
4011 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4012 rtl_writephy(tp, 0x1f, 0x0000);
4014 /* set rg_sel_sdm_rate */
4015 rtl_writephy(tp, 0x1f, 0x0c42);
4016 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4017 rtl_writephy(tp, 0x1f, 0x0000);
4019 /* Check ALDPS bit, disable it if enabled */
4020 rtl_writephy(tp, 0x1f, 0x0a43);
4021 if (rtl_readphy(tp, 0x10) & 0x0004)
4022 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4024 rtl_writephy(tp, 0x1f, 0x0000);
4027 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4029 /* patch 10M & ALDPS */
4030 rtl_writephy(tp, 0x1f, 0x0bcc);
4031 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4032 rtl_writephy(tp, 0x1f, 0x0a44);
4033 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4034 rtl_writephy(tp, 0x1f, 0x0a43);
4035 rtl_writephy(tp, 0x13, 0x8084);
4036 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4037 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4038 rtl_writephy(tp, 0x1f, 0x0000);
4040 /* Enable UC LPF tune function */
4041 rtl_writephy(tp, 0x1f, 0x0a43);
4042 rtl_writephy(tp, 0x13, 0x8012);
4043 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4044 rtl_writephy(tp, 0x1f, 0x0000);
4046 /* Set rg_sel_sdm_rate */
4047 rtl_writephy(tp, 0x1f, 0x0c42);
4048 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4049 rtl_writephy(tp, 0x1f, 0x0000);
4051 /* Channel estimation parameters */
4052 rtl_writephy(tp, 0x1f, 0x0a43);
4053 rtl_writephy(tp, 0x13, 0x80f3);
4054 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4055 rtl_writephy(tp, 0x13, 0x80f0);
4056 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4057 rtl_writephy(tp, 0x13, 0x80ef);
4058 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4059 rtl_writephy(tp, 0x13, 0x80f6);
4060 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4061 rtl_writephy(tp, 0x13, 0x80ec);
4062 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4063 rtl_writephy(tp, 0x13, 0x80ed);
4064 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4065 rtl_writephy(tp, 0x13, 0x80f2);
4066 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4067 rtl_writephy(tp, 0x13, 0x80f4);
4068 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4069 rtl_writephy(tp, 0x1f, 0x0a43);
4070 rtl_writephy(tp, 0x13, 0x8110);
4071 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4072 rtl_writephy(tp, 0x13, 0x810f);
4073 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4074 rtl_writephy(tp, 0x13, 0x8111);
4075 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4076 rtl_writephy(tp, 0x13, 0x8113);
4077 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4078 rtl_writephy(tp, 0x13, 0x8115);
4079 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4080 rtl_writephy(tp, 0x13, 0x810e);
4081 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4082 rtl_writephy(tp, 0x13, 0x810c);
4083 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4084 rtl_writephy(tp, 0x13, 0x810b);
4085 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4086 rtl_writephy(tp, 0x1f, 0x0a43);
4087 rtl_writephy(tp, 0x13, 0x80d1);
4088 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4089 rtl_writephy(tp, 0x13, 0x80cd);
4090 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4091 rtl_writephy(tp, 0x13, 0x80d3);
4092 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4093 rtl_writephy(tp, 0x13, 0x80d5);
4094 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4095 rtl_writephy(tp, 0x13, 0x80d7);
4096 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4098 /* Force PWM-mode */
4099 rtl_writephy(tp, 0x1f, 0x0bcd);
4100 rtl_writephy(tp, 0x14, 0x5065);
4101 rtl_writephy(tp, 0x14, 0xd065);
4102 rtl_writephy(tp, 0x1f, 0x0bc8);
4103 rtl_writephy(tp, 0x12, 0x00ed);
4104 rtl_writephy(tp, 0x1f, 0x0bcd);
4105 rtl_writephy(tp, 0x14, 0x1065);
4106 rtl_writephy(tp, 0x14, 0x9065);
4107 rtl_writephy(tp, 0x14, 0x1065);
4108 rtl_writephy(tp, 0x1f, 0x0000);
4110 /* Check ALDPS bit, disable it if enabled */
4111 rtl_writephy(tp, 0x1f, 0x0a43);
4112 if (rtl_readphy(tp, 0x10) & 0x0004)
4113 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4115 rtl_writephy(tp, 0x1f, 0x0000);
4118 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
4120 static const struct phy_reg phy_reg_init[] = {
4127 rtl_writephy(tp, 0x1f, 0x0000);
4128 rtl_patchphy(tp, 0x11, 1 << 12);
4129 rtl_patchphy(tp, 0x19, 1 << 13);
4130 rtl_patchphy(tp, 0x10, 1 << 15);
4132 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4135 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4137 static const struct phy_reg phy_reg_init[] = {
4151 /* Disable ALDPS before ram code */
4152 rtl_writephy(tp, 0x1f, 0x0000);
4153 rtl_writephy(tp, 0x18, 0x0310);
4156 rtl_apply_firmware(tp);
4158 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4161 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4163 /* Disable ALDPS before setting firmware */
4164 rtl_writephy(tp, 0x1f, 0x0000);
4165 rtl_writephy(tp, 0x18, 0x0310);
4168 rtl_apply_firmware(tp);
4171 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4172 rtl_writephy(tp, 0x1f, 0x0004);
4173 rtl_writephy(tp, 0x10, 0x401f);
4174 rtl_writephy(tp, 0x19, 0x7030);
4175 rtl_writephy(tp, 0x1f, 0x0000);
4178 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4180 static const struct phy_reg phy_reg_init[] = {
4187 /* Disable ALDPS before ram code */
4188 rtl_writephy(tp, 0x1f, 0x0000);
4189 rtl_writephy(tp, 0x18, 0x0310);
4192 rtl_apply_firmware(tp);
4194 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4195 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4197 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4200 static void rtl_hw_phy_config(struct net_device *dev)
4202 struct rtl8169_private *tp = netdev_priv(dev);
4204 rtl8169_print_mac_version(tp);
4206 switch (tp->mac_version) {
4207 case RTL_GIGA_MAC_VER_01:
4209 case RTL_GIGA_MAC_VER_02:
4210 case RTL_GIGA_MAC_VER_03:
4211 rtl8169s_hw_phy_config(tp);
4213 case RTL_GIGA_MAC_VER_04:
4214 rtl8169sb_hw_phy_config(tp);
4216 case RTL_GIGA_MAC_VER_05:
4217 rtl8169scd_hw_phy_config(tp);
4219 case RTL_GIGA_MAC_VER_06:
4220 rtl8169sce_hw_phy_config(tp);
4222 case RTL_GIGA_MAC_VER_07:
4223 case RTL_GIGA_MAC_VER_08:
4224 case RTL_GIGA_MAC_VER_09:
4225 rtl8102e_hw_phy_config(tp);
4227 case RTL_GIGA_MAC_VER_11:
4228 rtl8168bb_hw_phy_config(tp);
4230 case RTL_GIGA_MAC_VER_12:
4231 rtl8168bef_hw_phy_config(tp);
4233 case RTL_GIGA_MAC_VER_17:
4234 rtl8168bef_hw_phy_config(tp);
4236 case RTL_GIGA_MAC_VER_18:
4237 rtl8168cp_1_hw_phy_config(tp);
4239 case RTL_GIGA_MAC_VER_19:
4240 rtl8168c_1_hw_phy_config(tp);
4242 case RTL_GIGA_MAC_VER_20:
4243 rtl8168c_2_hw_phy_config(tp);
4245 case RTL_GIGA_MAC_VER_21:
4246 rtl8168c_3_hw_phy_config(tp);
4248 case RTL_GIGA_MAC_VER_22:
4249 rtl8168c_4_hw_phy_config(tp);
4251 case RTL_GIGA_MAC_VER_23:
4252 case RTL_GIGA_MAC_VER_24:
4253 rtl8168cp_2_hw_phy_config(tp);
4255 case RTL_GIGA_MAC_VER_25:
4256 rtl8168d_1_hw_phy_config(tp);
4258 case RTL_GIGA_MAC_VER_26:
4259 rtl8168d_2_hw_phy_config(tp);
4261 case RTL_GIGA_MAC_VER_27:
4262 rtl8168d_3_hw_phy_config(tp);
4264 case RTL_GIGA_MAC_VER_28:
4265 rtl8168d_4_hw_phy_config(tp);
4267 case RTL_GIGA_MAC_VER_29:
4268 case RTL_GIGA_MAC_VER_30:
4269 rtl8105e_hw_phy_config(tp);
4271 case RTL_GIGA_MAC_VER_31:
4274 case RTL_GIGA_MAC_VER_32:
4275 case RTL_GIGA_MAC_VER_33:
4276 rtl8168e_1_hw_phy_config(tp);
4278 case RTL_GIGA_MAC_VER_34:
4279 rtl8168e_2_hw_phy_config(tp);
4281 case RTL_GIGA_MAC_VER_35:
4282 rtl8168f_1_hw_phy_config(tp);
4284 case RTL_GIGA_MAC_VER_36:
4285 rtl8168f_2_hw_phy_config(tp);
4288 case RTL_GIGA_MAC_VER_37:
4289 rtl8402_hw_phy_config(tp);
4292 case RTL_GIGA_MAC_VER_38:
4293 rtl8411_hw_phy_config(tp);
4296 case RTL_GIGA_MAC_VER_39:
4297 rtl8106e_hw_phy_config(tp);
4300 case RTL_GIGA_MAC_VER_40:
4301 rtl8168g_1_hw_phy_config(tp);
4303 case RTL_GIGA_MAC_VER_42:
4304 case RTL_GIGA_MAC_VER_43:
4305 case RTL_GIGA_MAC_VER_44:
4306 rtl8168g_2_hw_phy_config(tp);
4308 case RTL_GIGA_MAC_VER_45:
4309 case RTL_GIGA_MAC_VER_47:
4310 rtl8168h_1_hw_phy_config(tp);
4312 case RTL_GIGA_MAC_VER_46:
4313 case RTL_GIGA_MAC_VER_48:
4314 rtl8168h_2_hw_phy_config(tp);
4317 case RTL_GIGA_MAC_VER_49:
4318 rtl8168ep_1_hw_phy_config(tp);
4320 case RTL_GIGA_MAC_VER_50:
4321 case RTL_GIGA_MAC_VER_51:
4322 rtl8168ep_2_hw_phy_config(tp);
4325 case RTL_GIGA_MAC_VER_41:
4331 static void rtl_phy_work(struct rtl8169_private *tp)
4333 struct timer_list *timer = &tp->timer;
4334 void __iomem *ioaddr = tp->mmio_addr;
4335 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4337 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
4339 if (tp->phy_reset_pending(tp)) {
4341 * A busy loop could burn quite a few cycles on nowadays CPU.
4342 * Let's delay the execution of the timer for a few ticks.
4348 if (tp->link_ok(ioaddr))
4351 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
4353 tp->phy_reset_enable(tp);
4356 mod_timer(timer, jiffies + timeout);
4359 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4361 if (!test_and_set_bit(flag, tp->wk.flags))
4362 schedule_work(&tp->wk.work);
4365 static void rtl8169_phy_timer(unsigned long __opaque)
4367 struct net_device *dev = (struct net_device *)__opaque;
4368 struct rtl8169_private *tp = netdev_priv(dev);
4370 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
4373 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
4374 void __iomem *ioaddr)
4377 pci_release_regions(pdev);
4378 pci_clear_mwi(pdev);
4379 pci_disable_device(pdev);
4383 DECLARE_RTL_COND(rtl_phy_reset_cond)
4385 return tp->phy_reset_pending(tp);
4388 static void rtl8169_phy_reset(struct net_device *dev,
4389 struct rtl8169_private *tp)
4391 tp->phy_reset_enable(tp);
4392 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
4395 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4397 void __iomem *ioaddr = tp->mmio_addr;
4399 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4400 (RTL_R8(PHYstatus) & TBI_Enable);
4403 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4405 void __iomem *ioaddr = tp->mmio_addr;
4407 rtl_hw_phy_config(dev);
4409 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4410 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4414 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4416 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4417 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4419 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
4420 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4422 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
4423 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
4426 rtl8169_phy_reset(dev, tp);
4428 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4429 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4430 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4431 (tp->mii.supports_gmii ?
4432 ADVERTISED_1000baseT_Half |
4433 ADVERTISED_1000baseT_Full : 0));
4435 if (rtl_tbi_enabled(tp))
4436 netif_info(tp, link, dev, "TBI auto-negotiating\n");
4439 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4441 void __iomem *ioaddr = tp->mmio_addr;
4445 RTL_W8(Cfg9346, Cfg9346_Unlock);
4447 RTL_W32(MAC4, addr[4] | addr[5] << 8);
4450 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4453 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4454 rtl_rar_exgmac_set(tp, addr);
4456 RTL_W8(Cfg9346, Cfg9346_Lock);
4458 rtl_unlock_work(tp);
4461 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4463 void __iomem *ioaddr = tp->mmio_addr;
4465 switch (tp->mac_version) {
4466 case RTL_GIGA_MAC_VER_01:
4467 case RTL_GIGA_MAC_VER_02:
4468 case RTL_GIGA_MAC_VER_03:
4469 case RTL_GIGA_MAC_VER_04:
4470 case RTL_GIGA_MAC_VER_05:
4471 case RTL_GIGA_MAC_VER_06:
4472 case RTL_GIGA_MAC_VER_10:
4473 case RTL_GIGA_MAC_VER_11:
4474 case RTL_GIGA_MAC_VER_12:
4475 case RTL_GIGA_MAC_VER_13:
4476 case RTL_GIGA_MAC_VER_14:
4477 case RTL_GIGA_MAC_VER_15:
4478 case RTL_GIGA_MAC_VER_16:
4479 case RTL_GIGA_MAC_VER_17:
4480 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4482 case RTL_GIGA_MAC_VER_18:
4483 case RTL_GIGA_MAC_VER_19:
4484 case RTL_GIGA_MAC_VER_20:
4485 case RTL_GIGA_MAC_VER_21:
4486 case RTL_GIGA_MAC_VER_22:
4487 case RTL_GIGA_MAC_VER_23:
4488 case RTL_GIGA_MAC_VER_24:
4489 case RTL_GIGA_MAC_VER_34:
4490 case RTL_GIGA_MAC_VER_35:
4491 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4493 case RTL_GIGA_MAC_VER_40:
4494 case RTL_GIGA_MAC_VER_41:
4495 case RTL_GIGA_MAC_VER_42:
4496 case RTL_GIGA_MAC_VER_43:
4497 case RTL_GIGA_MAC_VER_44:
4498 case RTL_GIGA_MAC_VER_45:
4499 case RTL_GIGA_MAC_VER_46:
4500 case RTL_GIGA_MAC_VER_47:
4501 case RTL_GIGA_MAC_VER_48:
4502 case RTL_GIGA_MAC_VER_49:
4503 case RTL_GIGA_MAC_VER_50:
4504 case RTL_GIGA_MAC_VER_51:
4505 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4508 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4513 static int rtl_set_mac_address(struct net_device *dev, void *p)
4515 struct rtl8169_private *tp = netdev_priv(dev);
4516 struct device *d = &tp->pci_dev->dev;
4517 struct sockaddr *addr = p;
4519 if (!is_valid_ether_addr(addr->sa_data))
4520 return -EADDRNOTAVAIL;
4522 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4524 pm_runtime_get_noresume(d);
4526 if (pm_runtime_active(d))
4527 rtl_rar_set(tp, dev->dev_addr);
4529 pm_runtime_put_noidle(d);
4531 /* Reportedly at least Asus X453MA truncates packets otherwise */
4532 if (tp->mac_version == RTL_GIGA_MAC_VER_37)
4538 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4540 struct rtl8169_private *tp = netdev_priv(dev);
4541 struct mii_ioctl_data *data = if_mii(ifr);
4543 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4546 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4547 struct mii_ioctl_data *data, int cmd)
4551 data->phy_id = 32; /* Internal PHY */
4555 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
4559 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
4565 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4570 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
4572 if (tp->features & RTL_FEATURE_MSI) {
4573 pci_disable_msi(pdev);
4574 tp->features &= ~RTL_FEATURE_MSI;
4578 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4580 struct mdio_ops *ops = &tp->mdio_ops;
4582 switch (tp->mac_version) {
4583 case RTL_GIGA_MAC_VER_27:
4584 ops->write = r8168dp_1_mdio_write;
4585 ops->read = r8168dp_1_mdio_read;
4587 case RTL_GIGA_MAC_VER_28:
4588 case RTL_GIGA_MAC_VER_31:
4589 ops->write = r8168dp_2_mdio_write;
4590 ops->read = r8168dp_2_mdio_read;
4592 case RTL_GIGA_MAC_VER_40:
4593 case RTL_GIGA_MAC_VER_41:
4594 case RTL_GIGA_MAC_VER_42:
4595 case RTL_GIGA_MAC_VER_43:
4596 case RTL_GIGA_MAC_VER_44:
4597 case RTL_GIGA_MAC_VER_45:
4598 case RTL_GIGA_MAC_VER_46:
4599 case RTL_GIGA_MAC_VER_47:
4600 case RTL_GIGA_MAC_VER_48:
4601 case RTL_GIGA_MAC_VER_49:
4602 case RTL_GIGA_MAC_VER_50:
4603 case RTL_GIGA_MAC_VER_51:
4604 ops->write = r8168g_mdio_write;
4605 ops->read = r8168g_mdio_read;
4608 ops->write = r8169_mdio_write;
4609 ops->read = r8169_mdio_read;
4614 static void rtl_speed_down(struct rtl8169_private *tp)
4619 rtl_writephy(tp, 0x1f, 0x0000);
4620 lpa = rtl_readphy(tp, MII_LPA);
4622 if (lpa & (LPA_10HALF | LPA_10FULL))
4623 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4624 else if (lpa & (LPA_100HALF | LPA_100FULL))
4625 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4626 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4628 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4629 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4630 (tp->mii.supports_gmii ?
4631 ADVERTISED_1000baseT_Half |
4632 ADVERTISED_1000baseT_Full : 0);
4634 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4638 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4640 void __iomem *ioaddr = tp->mmio_addr;
4642 switch (tp->mac_version) {
4643 case RTL_GIGA_MAC_VER_25:
4644 case RTL_GIGA_MAC_VER_26:
4645 case RTL_GIGA_MAC_VER_29:
4646 case RTL_GIGA_MAC_VER_30:
4647 case RTL_GIGA_MAC_VER_32:
4648 case RTL_GIGA_MAC_VER_33:
4649 case RTL_GIGA_MAC_VER_34:
4650 case RTL_GIGA_MAC_VER_37:
4651 case RTL_GIGA_MAC_VER_38:
4652 case RTL_GIGA_MAC_VER_39:
4653 case RTL_GIGA_MAC_VER_40:
4654 case RTL_GIGA_MAC_VER_41:
4655 case RTL_GIGA_MAC_VER_42:
4656 case RTL_GIGA_MAC_VER_43:
4657 case RTL_GIGA_MAC_VER_44:
4658 case RTL_GIGA_MAC_VER_45:
4659 case RTL_GIGA_MAC_VER_46:
4660 case RTL_GIGA_MAC_VER_47:
4661 case RTL_GIGA_MAC_VER_48:
4662 case RTL_GIGA_MAC_VER_49:
4663 case RTL_GIGA_MAC_VER_50:
4664 case RTL_GIGA_MAC_VER_51:
4665 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4666 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4673 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4675 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4679 rtl_wol_suspend_quirk(tp);
4684 static void r810x_phy_power_down(struct rtl8169_private *tp)
4686 rtl_writephy(tp, 0x1f, 0x0000);
4687 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4690 static void r810x_phy_power_up(struct rtl8169_private *tp)
4692 rtl_writephy(tp, 0x1f, 0x0000);
4693 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4696 static void r810x_pll_power_down(struct rtl8169_private *tp)
4698 void __iomem *ioaddr = tp->mmio_addr;
4700 if (rtl_wol_pll_power_down(tp))
4703 r810x_phy_power_down(tp);
4705 switch (tp->mac_version) {
4706 case RTL_GIGA_MAC_VER_07:
4707 case RTL_GIGA_MAC_VER_08:
4708 case RTL_GIGA_MAC_VER_09:
4709 case RTL_GIGA_MAC_VER_10:
4710 case RTL_GIGA_MAC_VER_13:
4711 case RTL_GIGA_MAC_VER_16:
4714 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4719 static void r810x_pll_power_up(struct rtl8169_private *tp)
4721 void __iomem *ioaddr = tp->mmio_addr;
4723 r810x_phy_power_up(tp);
4725 switch (tp->mac_version) {
4726 case RTL_GIGA_MAC_VER_07:
4727 case RTL_GIGA_MAC_VER_08:
4728 case RTL_GIGA_MAC_VER_09:
4729 case RTL_GIGA_MAC_VER_10:
4730 case RTL_GIGA_MAC_VER_13:
4731 case RTL_GIGA_MAC_VER_16:
4733 case RTL_GIGA_MAC_VER_47:
4734 case RTL_GIGA_MAC_VER_48:
4735 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
4738 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4743 static void r8168_phy_power_up(struct rtl8169_private *tp)
4745 rtl_writephy(tp, 0x1f, 0x0000);
4746 switch (tp->mac_version) {
4747 case RTL_GIGA_MAC_VER_11:
4748 case RTL_GIGA_MAC_VER_12:
4749 case RTL_GIGA_MAC_VER_17:
4750 case RTL_GIGA_MAC_VER_18:
4751 case RTL_GIGA_MAC_VER_19:
4752 case RTL_GIGA_MAC_VER_20:
4753 case RTL_GIGA_MAC_VER_21:
4754 case RTL_GIGA_MAC_VER_22:
4755 case RTL_GIGA_MAC_VER_23:
4756 case RTL_GIGA_MAC_VER_24:
4757 case RTL_GIGA_MAC_VER_25:
4758 case RTL_GIGA_MAC_VER_26:
4759 case RTL_GIGA_MAC_VER_27:
4760 case RTL_GIGA_MAC_VER_28:
4761 case RTL_GIGA_MAC_VER_31:
4762 rtl_writephy(tp, 0x0e, 0x0000);
4767 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4770 static void r8168_phy_power_down(struct rtl8169_private *tp)
4772 rtl_writephy(tp, 0x1f, 0x0000);
4773 switch (tp->mac_version) {
4774 case RTL_GIGA_MAC_VER_32:
4775 case RTL_GIGA_MAC_VER_33:
4776 case RTL_GIGA_MAC_VER_40:
4777 case RTL_GIGA_MAC_VER_41:
4778 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4781 case RTL_GIGA_MAC_VER_11:
4782 case RTL_GIGA_MAC_VER_12:
4783 case RTL_GIGA_MAC_VER_17:
4784 case RTL_GIGA_MAC_VER_18:
4785 case RTL_GIGA_MAC_VER_19:
4786 case RTL_GIGA_MAC_VER_20:
4787 case RTL_GIGA_MAC_VER_21:
4788 case RTL_GIGA_MAC_VER_22:
4789 case RTL_GIGA_MAC_VER_23:
4790 case RTL_GIGA_MAC_VER_24:
4791 case RTL_GIGA_MAC_VER_25:
4792 case RTL_GIGA_MAC_VER_26:
4793 case RTL_GIGA_MAC_VER_27:
4794 case RTL_GIGA_MAC_VER_28:
4795 case RTL_GIGA_MAC_VER_31:
4796 rtl_writephy(tp, 0x0e, 0x0200);
4798 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4803 static void r8168_pll_power_down(struct rtl8169_private *tp)
4805 void __iomem *ioaddr = tp->mmio_addr;
4807 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4808 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4809 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
4810 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
4811 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
4812 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
4813 r8168_check_dash(tp)) {
4817 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4818 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
4819 (RTL_R16(CPlusCmd) & ASF)) {
4823 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4824 tp->mac_version == RTL_GIGA_MAC_VER_33)
4825 rtl_ephy_write(tp, 0x19, 0xff64);
4827 if (rtl_wol_pll_power_down(tp))
4830 r8168_phy_power_down(tp);
4832 switch (tp->mac_version) {
4833 case RTL_GIGA_MAC_VER_25:
4834 case RTL_GIGA_MAC_VER_26:
4835 case RTL_GIGA_MAC_VER_27:
4836 case RTL_GIGA_MAC_VER_28:
4837 case RTL_GIGA_MAC_VER_31:
4838 case RTL_GIGA_MAC_VER_32:
4839 case RTL_GIGA_MAC_VER_33:
4840 case RTL_GIGA_MAC_VER_44:
4841 case RTL_GIGA_MAC_VER_45:
4842 case RTL_GIGA_MAC_VER_46:
4843 case RTL_GIGA_MAC_VER_50:
4844 case RTL_GIGA_MAC_VER_51:
4845 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4847 case RTL_GIGA_MAC_VER_40:
4848 case RTL_GIGA_MAC_VER_41:
4849 case RTL_GIGA_MAC_VER_49:
4850 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4851 0xfc000000, ERIAR_EXGMAC);
4852 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4857 static void r8168_pll_power_up(struct rtl8169_private *tp)
4859 void __iomem *ioaddr = tp->mmio_addr;
4861 switch (tp->mac_version) {
4862 case RTL_GIGA_MAC_VER_25:
4863 case RTL_GIGA_MAC_VER_26:
4864 case RTL_GIGA_MAC_VER_27:
4865 case RTL_GIGA_MAC_VER_28:
4866 case RTL_GIGA_MAC_VER_31:
4867 case RTL_GIGA_MAC_VER_32:
4868 case RTL_GIGA_MAC_VER_33:
4869 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4871 case RTL_GIGA_MAC_VER_44:
4872 case RTL_GIGA_MAC_VER_45:
4873 case RTL_GIGA_MAC_VER_46:
4874 case RTL_GIGA_MAC_VER_50:
4875 case RTL_GIGA_MAC_VER_51:
4876 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
4878 case RTL_GIGA_MAC_VER_40:
4879 case RTL_GIGA_MAC_VER_41:
4880 case RTL_GIGA_MAC_VER_49:
4881 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
4882 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4883 0x00000000, ERIAR_EXGMAC);
4887 r8168_phy_power_up(tp);
4890 static void rtl_generic_op(struct rtl8169_private *tp,
4891 void (*op)(struct rtl8169_private *))
4897 static void rtl_pll_power_down(struct rtl8169_private *tp)
4899 rtl_generic_op(tp, tp->pll_power_ops.down);
4902 static void rtl_pll_power_up(struct rtl8169_private *tp)
4904 rtl_generic_op(tp, tp->pll_power_ops.up);
4906 /* give MAC/PHY some time to resume */
4910 static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
4912 struct pll_power_ops *ops = &tp->pll_power_ops;
4914 switch (tp->mac_version) {
4915 case RTL_GIGA_MAC_VER_07:
4916 case RTL_GIGA_MAC_VER_08:
4917 case RTL_GIGA_MAC_VER_09:
4918 case RTL_GIGA_MAC_VER_10:
4919 case RTL_GIGA_MAC_VER_16:
4920 case RTL_GIGA_MAC_VER_29:
4921 case RTL_GIGA_MAC_VER_30:
4922 case RTL_GIGA_MAC_VER_37:
4923 case RTL_GIGA_MAC_VER_39:
4924 case RTL_GIGA_MAC_VER_43:
4925 case RTL_GIGA_MAC_VER_47:
4926 case RTL_GIGA_MAC_VER_48:
4927 ops->down = r810x_pll_power_down;
4928 ops->up = r810x_pll_power_up;
4931 case RTL_GIGA_MAC_VER_11:
4932 case RTL_GIGA_MAC_VER_12:
4933 case RTL_GIGA_MAC_VER_17:
4934 case RTL_GIGA_MAC_VER_18:
4935 case RTL_GIGA_MAC_VER_19:
4936 case RTL_GIGA_MAC_VER_20:
4937 case RTL_GIGA_MAC_VER_21:
4938 case RTL_GIGA_MAC_VER_22:
4939 case RTL_GIGA_MAC_VER_23:
4940 case RTL_GIGA_MAC_VER_24:
4941 case RTL_GIGA_MAC_VER_25:
4942 case RTL_GIGA_MAC_VER_26:
4943 case RTL_GIGA_MAC_VER_27:
4944 case RTL_GIGA_MAC_VER_28:
4945 case RTL_GIGA_MAC_VER_31:
4946 case RTL_GIGA_MAC_VER_32:
4947 case RTL_GIGA_MAC_VER_33:
4948 case RTL_GIGA_MAC_VER_34:
4949 case RTL_GIGA_MAC_VER_35:
4950 case RTL_GIGA_MAC_VER_36:
4951 case RTL_GIGA_MAC_VER_38:
4952 case RTL_GIGA_MAC_VER_40:
4953 case RTL_GIGA_MAC_VER_41:
4954 case RTL_GIGA_MAC_VER_42:
4955 case RTL_GIGA_MAC_VER_44:
4956 case RTL_GIGA_MAC_VER_45:
4957 case RTL_GIGA_MAC_VER_46:
4958 case RTL_GIGA_MAC_VER_49:
4959 case RTL_GIGA_MAC_VER_50:
4960 case RTL_GIGA_MAC_VER_51:
4961 ops->down = r8168_pll_power_down;
4962 ops->up = r8168_pll_power_up;
4972 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4974 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4977 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4979 void __iomem *ioaddr = tp->mmio_addr;
4981 RTL_W8(Cfg9346, Cfg9346_Unlock);
4982 rtl_generic_op(tp, tp->jumbo_ops.enable);
4983 RTL_W8(Cfg9346, Cfg9346_Lock);
4986 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4988 void __iomem *ioaddr = tp->mmio_addr;
4990 RTL_W8(Cfg9346, Cfg9346_Unlock);
4991 rtl_generic_op(tp, tp->jumbo_ops.disable);
4992 RTL_W8(Cfg9346, Cfg9346_Lock);
4995 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4997 void __iomem *ioaddr = tp->mmio_addr;
4999 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5000 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
5001 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
5004 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5006 void __iomem *ioaddr = tp->mmio_addr;
5008 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5009 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5010 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5013 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5015 void __iomem *ioaddr = tp->mmio_addr;
5017 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5020 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5022 void __iomem *ioaddr = tp->mmio_addr;
5024 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5027 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5029 void __iomem *ioaddr = tp->mmio_addr;
5031 RTL_W8(MaxTxPacketSize, 0x3f);
5032 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5033 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
5034 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
5037 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5039 void __iomem *ioaddr = tp->mmio_addr;
5041 RTL_W8(MaxTxPacketSize, 0x0c);
5042 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5043 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
5044 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5047 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5049 rtl_tx_performance_tweak(tp->pci_dev,
5050 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
5053 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5055 rtl_tx_performance_tweak(tp->pci_dev,
5056 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5059 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5061 void __iomem *ioaddr = tp->mmio_addr;
5063 r8168b_0_hw_jumbo_enable(tp);
5065 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5068 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5070 void __iomem *ioaddr = tp->mmio_addr;
5072 r8168b_0_hw_jumbo_disable(tp);
5074 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5077 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
5079 struct jumbo_ops *ops = &tp->jumbo_ops;
5081 switch (tp->mac_version) {
5082 case RTL_GIGA_MAC_VER_11:
5083 ops->disable = r8168b_0_hw_jumbo_disable;
5084 ops->enable = r8168b_0_hw_jumbo_enable;
5086 case RTL_GIGA_MAC_VER_12:
5087 case RTL_GIGA_MAC_VER_17:
5088 ops->disable = r8168b_1_hw_jumbo_disable;
5089 ops->enable = r8168b_1_hw_jumbo_enable;
5091 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5092 case RTL_GIGA_MAC_VER_19:
5093 case RTL_GIGA_MAC_VER_20:
5094 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5095 case RTL_GIGA_MAC_VER_22:
5096 case RTL_GIGA_MAC_VER_23:
5097 case RTL_GIGA_MAC_VER_24:
5098 case RTL_GIGA_MAC_VER_25:
5099 case RTL_GIGA_MAC_VER_26:
5100 ops->disable = r8168c_hw_jumbo_disable;
5101 ops->enable = r8168c_hw_jumbo_enable;
5103 case RTL_GIGA_MAC_VER_27:
5104 case RTL_GIGA_MAC_VER_28:
5105 ops->disable = r8168dp_hw_jumbo_disable;
5106 ops->enable = r8168dp_hw_jumbo_enable;
5108 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5109 case RTL_GIGA_MAC_VER_32:
5110 case RTL_GIGA_MAC_VER_33:
5111 case RTL_GIGA_MAC_VER_34:
5112 ops->disable = r8168e_hw_jumbo_disable;
5113 ops->enable = r8168e_hw_jumbo_enable;
5117 * No action needed for jumbo frames with 8169.
5118 * No jumbo for 810x at all.
5120 case RTL_GIGA_MAC_VER_40:
5121 case RTL_GIGA_MAC_VER_41:
5122 case RTL_GIGA_MAC_VER_42:
5123 case RTL_GIGA_MAC_VER_43:
5124 case RTL_GIGA_MAC_VER_44:
5125 case RTL_GIGA_MAC_VER_45:
5126 case RTL_GIGA_MAC_VER_46:
5127 case RTL_GIGA_MAC_VER_47:
5128 case RTL_GIGA_MAC_VER_48:
5129 case RTL_GIGA_MAC_VER_49:
5130 case RTL_GIGA_MAC_VER_50:
5131 case RTL_GIGA_MAC_VER_51:
5133 ops->disable = NULL;
5139 DECLARE_RTL_COND(rtl_chipcmd_cond)
5141 void __iomem *ioaddr = tp->mmio_addr;
5143 return RTL_R8(ChipCmd) & CmdReset;
5146 static void rtl_hw_reset(struct rtl8169_private *tp)
5148 void __iomem *ioaddr = tp->mmio_addr;
5150 RTL_W8(ChipCmd, CmdReset);
5152 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
5155 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5157 struct rtl_fw *rtl_fw;
5161 name = rtl_lookup_firmware_name(tp);
5163 goto out_no_firmware;
5165 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5169 rc = reject_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5173 rc = rtl_check_firmware(tp, rtl_fw);
5175 goto err_release_firmware;
5177 tp->rtl_fw = rtl_fw;
5181 err_release_firmware:
5182 release_firmware(rtl_fw->fw);
5186 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5193 static void rtl_request_firmware(struct rtl8169_private *tp)
5195 if (IS_ERR(tp->rtl_fw))
5196 rtl_request_uncached_firmware(tp);
5199 static void rtl_rx_close(struct rtl8169_private *tp)
5201 void __iomem *ioaddr = tp->mmio_addr;
5203 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
5206 DECLARE_RTL_COND(rtl_npq_cond)
5208 void __iomem *ioaddr = tp->mmio_addr;
5210 return RTL_R8(TxPoll) & NPQ;
5213 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5215 void __iomem *ioaddr = tp->mmio_addr;
5217 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5220 static void rtl8169_hw_reset(struct rtl8169_private *tp)
5222 void __iomem *ioaddr = tp->mmio_addr;
5224 /* Disable interrupts */
5225 rtl8169_irq_mask_and_ack(tp);
5229 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5230 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5231 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5232 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
5233 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
5234 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5235 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5236 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5237 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5238 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5239 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5240 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5241 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5242 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5243 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5244 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5245 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
5246 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5247 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5248 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5249 tp->mac_version == RTL_GIGA_MAC_VER_51) {
5250 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5251 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
5253 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5260 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
5262 void __iomem *ioaddr = tp->mmio_addr;
5264 /* Set DMA burst size and Interframe Gap Time */
5265 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5266 (InterFrameGap << TxInterFrameGapShift));
5269 static void rtl_hw_start(struct net_device *dev)
5271 struct rtl8169_private *tp = netdev_priv(dev);
5275 rtl_irq_enable_all(tp);
5278 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5279 void __iomem *ioaddr)
5282 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5283 * register to be written before TxDescAddrLow to work.
5284 * Switching from MMIO to I/O access fixes the issue as well.
5286 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5287 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5288 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5289 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
5292 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5296 cmd = RTL_R16(CPlusCmd);
5297 RTL_W16(CPlusCmd, cmd);
5301 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
5303 /* Low hurts. Let's disable the filtering. */
5304 RTL_W16(RxMaxSize, rx_buf_sz + 1);
5307 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5309 static const struct rtl_cfg2_info {
5314 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5315 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5316 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5317 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
5319 const struct rtl_cfg2_info *p = cfg2_info;
5323 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
5324 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
5325 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5326 RTL_W32(0x7c, p->val);
5332 static void rtl_set_rx_mode(struct net_device *dev)
5334 struct rtl8169_private *tp = netdev_priv(dev);
5335 void __iomem *ioaddr = tp->mmio_addr;
5336 u32 mc_filter[2]; /* Multicast hash filter */
5340 if (dev->flags & IFF_PROMISC) {
5341 /* Unconditionally log net taps. */
5342 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5344 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5346 mc_filter[1] = mc_filter[0] = 0xffffffff;
5347 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5348 (dev->flags & IFF_ALLMULTI)) {
5349 /* Too many to filter perfectly -- accept all multicasts. */
5350 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5351 mc_filter[1] = mc_filter[0] = 0xffffffff;
5353 struct netdev_hw_addr *ha;
5355 rx_mode = AcceptBroadcast | AcceptMyPhys;
5356 mc_filter[1] = mc_filter[0] = 0;
5357 netdev_for_each_mc_addr(ha, dev) {
5358 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5359 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5360 rx_mode |= AcceptMulticast;
5364 if (dev->features & NETIF_F_RXALL)
5365 rx_mode |= (AcceptErr | AcceptRunt);
5367 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5369 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5370 u32 data = mc_filter[0];
5372 mc_filter[0] = swab32(mc_filter[1]);
5373 mc_filter[1] = swab32(data);
5376 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5377 mc_filter[1] = mc_filter[0] = 0xffffffff;
5379 RTL_W32(MAR0 + 4, mc_filter[1]);
5380 RTL_W32(MAR0 + 0, mc_filter[0]);
5382 RTL_W32(RxConfig, tmp);
5385 static void rtl_hw_start_8169(struct net_device *dev)
5387 struct rtl8169_private *tp = netdev_priv(dev);
5388 void __iomem *ioaddr = tp->mmio_addr;
5389 struct pci_dev *pdev = tp->pci_dev;
5391 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5392 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5393 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5396 RTL_W8(Cfg9346, Cfg9346_Unlock);
5397 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5398 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5399 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5400 tp->mac_version == RTL_GIGA_MAC_VER_04)
5401 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5405 RTL_W8(EarlyTxThres, NoEarlyTx);
5407 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5409 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5410 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5411 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5412 tp->mac_version == RTL_GIGA_MAC_VER_04)
5413 rtl_set_rx_tx_config_registers(tp);
5415 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
5417 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5418 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5419 dprintk("Set MAC Reg C+CR Offset 0xe0. "
5420 "Bit-3 and bit-14 MUST be 1\n");
5421 tp->cp_cmd |= (1 << 14);
5424 RTL_W16(CPlusCmd, tp->cp_cmd);
5426 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5429 * Undocumented corner. Supposedly:
5430 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5432 RTL_W16(IntrMitigate, 0x0000);
5434 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5436 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5437 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5438 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5439 tp->mac_version != RTL_GIGA_MAC_VER_04) {
5440 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5441 rtl_set_rx_tx_config_registers(tp);
5444 RTL_W8(Cfg9346, Cfg9346_Lock);
5446 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5449 RTL_W32(RxMissed, 0);
5451 rtl_set_rx_mode(dev);
5453 /* no early-rx interrupts */
5454 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
5457 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5459 if (tp->csi_ops.write)
5460 tp->csi_ops.write(tp, addr, value);
5463 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5465 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
5468 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
5472 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5473 rtl_csi_write(tp, 0x070c, csi | bits);
5476 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
5478 rtl_csi_access_enable(tp, 0x17000000);
5481 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
5483 rtl_csi_access_enable(tp, 0x27000000);
5486 DECLARE_RTL_COND(rtl_csiar_cond)
5488 void __iomem *ioaddr = tp->mmio_addr;
5490 return RTL_R32(CSIAR) & CSIAR_FLAG;
5493 static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
5495 void __iomem *ioaddr = tp->mmio_addr;
5497 RTL_W32(CSIDR, value);
5498 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5499 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5501 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5504 static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
5506 void __iomem *ioaddr = tp->mmio_addr;
5508 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5509 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5511 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5512 RTL_R32(CSIDR) : ~0;
5515 static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
5517 void __iomem *ioaddr = tp->mmio_addr;
5519 RTL_W32(CSIDR, value);
5520 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5521 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5524 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5527 static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
5529 void __iomem *ioaddr = tp->mmio_addr;
5531 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5532 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5534 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5535 RTL_R32(CSIDR) : ~0;
5538 static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5540 void __iomem *ioaddr = tp->mmio_addr;
5542 RTL_W32(CSIDR, value);
5543 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5544 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5547 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5550 static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5552 void __iomem *ioaddr = tp->mmio_addr;
5554 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5555 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5557 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5558 RTL_R32(CSIDR) : ~0;
5561 static void rtl_init_csi_ops(struct rtl8169_private *tp)
5563 struct csi_ops *ops = &tp->csi_ops;
5565 switch (tp->mac_version) {
5566 case RTL_GIGA_MAC_VER_01:
5567 case RTL_GIGA_MAC_VER_02:
5568 case RTL_GIGA_MAC_VER_03:
5569 case RTL_GIGA_MAC_VER_04:
5570 case RTL_GIGA_MAC_VER_05:
5571 case RTL_GIGA_MAC_VER_06:
5572 case RTL_GIGA_MAC_VER_10:
5573 case RTL_GIGA_MAC_VER_11:
5574 case RTL_GIGA_MAC_VER_12:
5575 case RTL_GIGA_MAC_VER_13:
5576 case RTL_GIGA_MAC_VER_14:
5577 case RTL_GIGA_MAC_VER_15:
5578 case RTL_GIGA_MAC_VER_16:
5579 case RTL_GIGA_MAC_VER_17:
5584 case RTL_GIGA_MAC_VER_37:
5585 case RTL_GIGA_MAC_VER_38:
5586 ops->write = r8402_csi_write;
5587 ops->read = r8402_csi_read;
5590 case RTL_GIGA_MAC_VER_44:
5591 ops->write = r8411_csi_write;
5592 ops->read = r8411_csi_read;
5596 ops->write = r8169_csi_write;
5597 ops->read = r8169_csi_read;
5603 unsigned int offset;
5608 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5614 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5615 rtl_ephy_write(tp, e->offset, w);
5620 static void rtl_disable_clock_request(struct pci_dev *pdev)
5622 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5623 PCI_EXP_LNKCTL_CLKREQ_EN);
5626 static void rtl_enable_clock_request(struct pci_dev *pdev)
5628 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5629 PCI_EXP_LNKCTL_CLKREQ_EN);
5632 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5634 void __iomem *ioaddr = tp->mmio_addr;
5637 data = RTL_R8(Config3);
5642 data &= ~Rdy_to_L23;
5644 RTL_W8(Config3, data);
5647 #define R8168_CPCMD_QUIRK_MASK (\
5658 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
5660 void __iomem *ioaddr = tp->mmio_addr;
5661 struct pci_dev *pdev = tp->pci_dev;
5663 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5665 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5667 if (tp->dev->mtu <= ETH_DATA_LEN) {
5668 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5669 PCI_EXP_DEVCTL_NOSNOOP_EN);
5673 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
5675 void __iomem *ioaddr = tp->mmio_addr;
5677 rtl_hw_start_8168bb(tp);
5679 RTL_W8(MaxTxPacketSize, TxPacketMax);
5681 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5684 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
5686 void __iomem *ioaddr = tp->mmio_addr;
5687 struct pci_dev *pdev = tp->pci_dev;
5689 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5691 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5693 if (tp->dev->mtu <= ETH_DATA_LEN)
5694 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5696 rtl_disable_clock_request(pdev);
5698 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5701 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
5703 static const struct ephy_info e_info_8168cp[] = {
5704 { 0x01, 0, 0x0001 },
5705 { 0x02, 0x0800, 0x1000 },
5706 { 0x03, 0, 0x0042 },
5707 { 0x06, 0x0080, 0x0000 },
5711 rtl_csi_access_enable_2(tp);
5713 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
5715 __rtl_hw_start_8168cp(tp);
5718 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
5720 void __iomem *ioaddr = tp->mmio_addr;
5721 struct pci_dev *pdev = tp->pci_dev;
5723 rtl_csi_access_enable_2(tp);
5725 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5727 if (tp->dev->mtu <= ETH_DATA_LEN)
5728 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5730 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5733 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
5735 void __iomem *ioaddr = tp->mmio_addr;
5736 struct pci_dev *pdev = tp->pci_dev;
5738 rtl_csi_access_enable_2(tp);
5740 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5743 RTL_W8(DBG_REG, 0x20);
5745 RTL_W8(MaxTxPacketSize, TxPacketMax);
5747 if (tp->dev->mtu <= ETH_DATA_LEN)
5748 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5750 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5753 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
5755 void __iomem *ioaddr = tp->mmio_addr;
5756 static const struct ephy_info e_info_8168c_1[] = {
5757 { 0x02, 0x0800, 0x1000 },
5758 { 0x03, 0, 0x0002 },
5759 { 0x06, 0x0080, 0x0000 }
5762 rtl_csi_access_enable_2(tp);
5764 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5766 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
5768 __rtl_hw_start_8168cp(tp);
5771 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
5773 static const struct ephy_info e_info_8168c_2[] = {
5774 { 0x01, 0, 0x0001 },
5775 { 0x03, 0x0400, 0x0220 }
5778 rtl_csi_access_enable_2(tp);
5780 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
5782 __rtl_hw_start_8168cp(tp);
5785 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
5787 rtl_hw_start_8168c_2(tp);
5790 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
5792 rtl_csi_access_enable_2(tp);
5794 __rtl_hw_start_8168cp(tp);
5797 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
5799 void __iomem *ioaddr = tp->mmio_addr;
5800 struct pci_dev *pdev = tp->pci_dev;
5802 rtl_csi_access_enable_2(tp);
5804 rtl_disable_clock_request(pdev);
5806 RTL_W8(MaxTxPacketSize, TxPacketMax);
5808 if (tp->dev->mtu <= ETH_DATA_LEN)
5809 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5811 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5814 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
5816 void __iomem *ioaddr = tp->mmio_addr;
5817 struct pci_dev *pdev = tp->pci_dev;
5819 rtl_csi_access_enable_1(tp);
5821 if (tp->dev->mtu <= ETH_DATA_LEN)
5822 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5824 RTL_W8(MaxTxPacketSize, TxPacketMax);
5826 rtl_disable_clock_request(pdev);
5829 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
5831 void __iomem *ioaddr = tp->mmio_addr;
5832 struct pci_dev *pdev = tp->pci_dev;
5833 static const struct ephy_info e_info_8168d_4[] = {
5834 { 0x0b, 0x0000, 0x0048 },
5835 { 0x19, 0x0020, 0x0050 },
5836 { 0x0c, 0x0100, 0x0020 }
5839 rtl_csi_access_enable_1(tp);
5841 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5843 RTL_W8(MaxTxPacketSize, TxPacketMax);
5845 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
5847 rtl_enable_clock_request(pdev);
5850 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
5852 void __iomem *ioaddr = tp->mmio_addr;
5853 struct pci_dev *pdev = tp->pci_dev;
5854 static const struct ephy_info e_info_8168e_1[] = {
5855 { 0x00, 0x0200, 0x0100 },
5856 { 0x00, 0x0000, 0x0004 },
5857 { 0x06, 0x0002, 0x0001 },
5858 { 0x06, 0x0000, 0x0030 },
5859 { 0x07, 0x0000, 0x2000 },
5860 { 0x00, 0x0000, 0x0020 },
5861 { 0x03, 0x5800, 0x2000 },
5862 { 0x03, 0x0000, 0x0001 },
5863 { 0x01, 0x0800, 0x1000 },
5864 { 0x07, 0x0000, 0x4000 },
5865 { 0x1e, 0x0000, 0x2000 },
5866 { 0x19, 0xffff, 0xfe6c },
5867 { 0x0a, 0x0000, 0x0040 }
5870 rtl_csi_access_enable_2(tp);
5872 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5874 if (tp->dev->mtu <= ETH_DATA_LEN)
5875 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5877 RTL_W8(MaxTxPacketSize, TxPacketMax);
5879 rtl_disable_clock_request(pdev);
5881 /* Reset tx FIFO pointer */
5882 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5883 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
5885 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5888 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5890 void __iomem *ioaddr = tp->mmio_addr;
5891 struct pci_dev *pdev = tp->pci_dev;
5892 static const struct ephy_info e_info_8168e_2[] = {
5893 { 0x09, 0x0000, 0x0080 },
5894 { 0x19, 0x0000, 0x0224 }
5897 rtl_csi_access_enable_1(tp);
5899 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5901 if (tp->dev->mtu <= ETH_DATA_LEN)
5902 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5904 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5905 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5906 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5907 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5908 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5909 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5910 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5911 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5913 RTL_W8(MaxTxPacketSize, EarlySize);
5915 rtl_disable_clock_request(pdev);
5917 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5918 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5920 /* Adjust EEE LED frequency */
5921 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5923 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5924 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5925 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5928 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5930 void __iomem *ioaddr = tp->mmio_addr;
5931 struct pci_dev *pdev = tp->pci_dev;
5933 rtl_csi_access_enable_2(tp);
5935 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5937 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5938 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5939 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5940 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5941 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5942 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5943 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5944 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5945 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5946 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5948 RTL_W8(MaxTxPacketSize, EarlySize);
5950 rtl_disable_clock_request(pdev);
5952 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5953 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5954 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5955 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5956 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
5959 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5961 void __iomem *ioaddr = tp->mmio_addr;
5962 static const struct ephy_info e_info_8168f_1[] = {
5963 { 0x06, 0x00c0, 0x0020 },
5964 { 0x08, 0x0001, 0x0002 },
5965 { 0x09, 0x0000, 0x0080 },
5966 { 0x19, 0x0000, 0x0224 }
5969 rtl_hw_start_8168f(tp);
5971 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5973 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5975 /* Adjust EEE LED frequency */
5976 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5979 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5981 static const struct ephy_info e_info_8168f_1[] = {
5982 { 0x06, 0x00c0, 0x0020 },
5983 { 0x0f, 0xffff, 0x5200 },
5984 { 0x1e, 0x0000, 0x4000 },
5985 { 0x19, 0x0000, 0x0224 }
5988 rtl_hw_start_8168f(tp);
5989 rtl_pcie_state_l2l3_enable(tp, false);
5991 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5993 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5996 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
5998 void __iomem *ioaddr = tp->mmio_addr;
5999 struct pci_dev *pdev = tp->pci_dev;
6001 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6003 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6004 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6005 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6006 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6008 rtl_csi_access_enable_1(tp);
6010 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6012 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6013 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6014 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
6016 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6017 RTL_W8(MaxTxPacketSize, EarlySize);
6019 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6020 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6022 /* Adjust EEE LED frequency */
6023 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6025 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6026 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6028 rtl_pcie_state_l2l3_enable(tp, false);
6031 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6033 void __iomem *ioaddr = tp->mmio_addr;
6034 static const struct ephy_info e_info_8168g_1[] = {
6035 { 0x00, 0x0000, 0x0008 },
6036 { 0x0c, 0x37d0, 0x0820 },
6037 { 0x1e, 0x0000, 0x0001 },
6038 { 0x19, 0x8000, 0x0000 }
6041 rtl_hw_start_8168g(tp);
6043 /* disable aspm and clock request before access ephy */
6044 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6045 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6046 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6049 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6051 void __iomem *ioaddr = tp->mmio_addr;
6052 static const struct ephy_info e_info_8168g_2[] = {
6053 { 0x00, 0x0000, 0x0008 },
6054 { 0x0c, 0x3df0, 0x0200 },
6055 { 0x19, 0xffff, 0xfc00 },
6056 { 0x1e, 0xffff, 0x20eb }
6059 rtl_hw_start_8168g(tp);
6061 /* disable aspm and clock request before access ephy */
6062 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6063 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6064 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6067 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6069 void __iomem *ioaddr = tp->mmio_addr;
6070 static const struct ephy_info e_info_8411_2[] = {
6071 { 0x00, 0x0000, 0x0008 },
6072 { 0x0c, 0x3df0, 0x0200 },
6073 { 0x0f, 0xffff, 0x5200 },
6074 { 0x19, 0x0020, 0x0000 },
6075 { 0x1e, 0x0000, 0x2000 }
6078 rtl_hw_start_8168g(tp);
6080 /* disable aspm and clock request before access ephy */
6081 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6082 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6083 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6086 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6088 void __iomem *ioaddr = tp->mmio_addr;
6089 struct pci_dev *pdev = tp->pci_dev;
6092 static const struct ephy_info e_info_8168h_1[] = {
6093 { 0x1e, 0x0800, 0x0001 },
6094 { 0x1d, 0x0000, 0x0800 },
6095 { 0x05, 0xffff, 0x2089 },
6096 { 0x06, 0xffff, 0x5881 },
6097 { 0x04, 0xffff, 0x154a },
6098 { 0x01, 0xffff, 0x068b }
6101 /* disable aspm and clock request before access ephy */
6102 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6103 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6104 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6106 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6108 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6109 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6110 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6111 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6113 rtl_csi_access_enable_1(tp);
6115 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6117 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6118 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6120 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
6122 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
6124 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6126 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6127 RTL_W8(MaxTxPacketSize, EarlySize);
6129 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6130 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6132 /* Adjust EEE LED frequency */
6133 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6135 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6136 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6138 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6140 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6142 rtl_pcie_state_l2l3_enable(tp, false);
6144 rtl_writephy(tp, 0x1f, 0x0c42);
6145 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
6146 rtl_writephy(tp, 0x1f, 0x0000);
6147 if (rg_saw_cnt > 0) {
6150 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6151 sw_cnt_1ms_ini &= 0x0fff;
6152 data = r8168_mac_ocp_read(tp, 0xd412);
6154 data |= sw_cnt_1ms_ini;
6155 r8168_mac_ocp_write(tp, 0xd412, data);
6158 data = r8168_mac_ocp_read(tp, 0xe056);
6161 r8168_mac_ocp_write(tp, 0xe056, data);
6163 data = r8168_mac_ocp_read(tp, 0xe052);
6166 r8168_mac_ocp_write(tp, 0xe052, data);
6168 data = r8168_mac_ocp_read(tp, 0xe0d6);
6171 r8168_mac_ocp_write(tp, 0xe0d6, data);
6173 data = r8168_mac_ocp_read(tp, 0xd420);
6176 r8168_mac_ocp_write(tp, 0xd420, data);
6178 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6179 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6180 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6181 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6184 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6186 void __iomem *ioaddr = tp->mmio_addr;
6187 struct pci_dev *pdev = tp->pci_dev;
6189 rtl8168ep_stop_cmac(tp);
6191 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6193 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6194 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6195 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6196 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6198 rtl_csi_access_enable_1(tp);
6200 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6202 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6203 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6205 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6207 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6209 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6210 RTL_W8(MaxTxPacketSize, EarlySize);
6212 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6213 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6215 /* Adjust EEE LED frequency */
6216 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6218 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6220 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6222 rtl_pcie_state_l2l3_enable(tp, false);
6225 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6227 void __iomem *ioaddr = tp->mmio_addr;
6228 static const struct ephy_info e_info_8168ep_1[] = {
6229 { 0x00, 0xffff, 0x10ab },
6230 { 0x06, 0xffff, 0xf030 },
6231 { 0x08, 0xffff, 0x2006 },
6232 { 0x0d, 0xffff, 0x1666 },
6233 { 0x0c, 0x3ff0, 0x0000 }
6236 /* disable aspm and clock request before access ephy */
6237 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6238 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6239 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6241 rtl_hw_start_8168ep(tp);
6244 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6246 void __iomem *ioaddr = tp->mmio_addr;
6247 static const struct ephy_info e_info_8168ep_2[] = {
6248 { 0x00, 0xffff, 0x10a3 },
6249 { 0x19, 0xffff, 0xfc00 },
6250 { 0x1e, 0xffff, 0x20ea }
6253 /* disable aspm and clock request before access ephy */
6254 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6255 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6256 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6258 rtl_hw_start_8168ep(tp);
6260 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6261 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6264 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6266 void __iomem *ioaddr = tp->mmio_addr;
6268 static const struct ephy_info e_info_8168ep_3[] = {
6269 { 0x00, 0xffff, 0x10a3 },
6270 { 0x19, 0xffff, 0x7c00 },
6271 { 0x1e, 0xffff, 0x20eb },
6272 { 0x0d, 0xffff, 0x1666 }
6275 /* disable aspm and clock request before access ephy */
6276 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6277 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6278 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6280 rtl_hw_start_8168ep(tp);
6282 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6283 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6285 data = r8168_mac_ocp_read(tp, 0xd3e2);
6288 r8168_mac_ocp_write(tp, 0xd3e2, data);
6290 data = r8168_mac_ocp_read(tp, 0xd3e4);
6292 r8168_mac_ocp_write(tp, 0xd3e4, data);
6294 data = r8168_mac_ocp_read(tp, 0xe860);
6296 r8168_mac_ocp_write(tp, 0xe860, data);
6299 static void rtl_hw_start_8168(struct net_device *dev)
6301 struct rtl8169_private *tp = netdev_priv(dev);
6302 void __iomem *ioaddr = tp->mmio_addr;
6304 RTL_W8(Cfg9346, Cfg9346_Unlock);
6306 RTL_W8(MaxTxPacketSize, TxPacketMax);
6308 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6310 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
6312 RTL_W16(CPlusCmd, tp->cp_cmd);
6314 RTL_W16(IntrMitigate, 0x5151);
6316 /* Work around for RxFIFO overflow. */
6317 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
6318 tp->event_slow |= RxFIFOOver | PCSTimeout;
6319 tp->event_slow &= ~RxOverflow;
6322 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6324 rtl_set_rx_tx_config_registers(tp);
6328 switch (tp->mac_version) {
6329 case RTL_GIGA_MAC_VER_11:
6330 rtl_hw_start_8168bb(tp);
6333 case RTL_GIGA_MAC_VER_12:
6334 case RTL_GIGA_MAC_VER_17:
6335 rtl_hw_start_8168bef(tp);
6338 case RTL_GIGA_MAC_VER_18:
6339 rtl_hw_start_8168cp_1(tp);
6342 case RTL_GIGA_MAC_VER_19:
6343 rtl_hw_start_8168c_1(tp);
6346 case RTL_GIGA_MAC_VER_20:
6347 rtl_hw_start_8168c_2(tp);
6350 case RTL_GIGA_MAC_VER_21:
6351 rtl_hw_start_8168c_3(tp);
6354 case RTL_GIGA_MAC_VER_22:
6355 rtl_hw_start_8168c_4(tp);
6358 case RTL_GIGA_MAC_VER_23:
6359 rtl_hw_start_8168cp_2(tp);
6362 case RTL_GIGA_MAC_VER_24:
6363 rtl_hw_start_8168cp_3(tp);
6366 case RTL_GIGA_MAC_VER_25:
6367 case RTL_GIGA_MAC_VER_26:
6368 case RTL_GIGA_MAC_VER_27:
6369 rtl_hw_start_8168d(tp);
6372 case RTL_GIGA_MAC_VER_28:
6373 rtl_hw_start_8168d_4(tp);
6376 case RTL_GIGA_MAC_VER_31:
6377 rtl_hw_start_8168dp(tp);
6380 case RTL_GIGA_MAC_VER_32:
6381 case RTL_GIGA_MAC_VER_33:
6382 rtl_hw_start_8168e_1(tp);
6384 case RTL_GIGA_MAC_VER_34:
6385 rtl_hw_start_8168e_2(tp);
6388 case RTL_GIGA_MAC_VER_35:
6389 case RTL_GIGA_MAC_VER_36:
6390 rtl_hw_start_8168f_1(tp);
6393 case RTL_GIGA_MAC_VER_38:
6394 rtl_hw_start_8411(tp);
6397 case RTL_GIGA_MAC_VER_40:
6398 case RTL_GIGA_MAC_VER_41:
6399 rtl_hw_start_8168g_1(tp);
6401 case RTL_GIGA_MAC_VER_42:
6402 rtl_hw_start_8168g_2(tp);
6405 case RTL_GIGA_MAC_VER_44:
6406 rtl_hw_start_8411_2(tp);
6409 case RTL_GIGA_MAC_VER_45:
6410 case RTL_GIGA_MAC_VER_46:
6411 rtl_hw_start_8168h_1(tp);
6414 case RTL_GIGA_MAC_VER_49:
6415 rtl_hw_start_8168ep_1(tp);
6418 case RTL_GIGA_MAC_VER_50:
6419 rtl_hw_start_8168ep_2(tp);
6422 case RTL_GIGA_MAC_VER_51:
6423 rtl_hw_start_8168ep_3(tp);
6427 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6428 dev->name, tp->mac_version);
6432 RTL_W8(Cfg9346, Cfg9346_Lock);
6434 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6436 rtl_set_rx_mode(dev);
6438 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
6441 #define R810X_CPCMD_QUIRK_MASK (\
6452 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
6454 void __iomem *ioaddr = tp->mmio_addr;
6455 struct pci_dev *pdev = tp->pci_dev;
6456 static const struct ephy_info e_info_8102e_1[] = {
6457 { 0x01, 0, 0x6e65 },
6458 { 0x02, 0, 0x091f },
6459 { 0x03, 0, 0xc2f9 },
6460 { 0x06, 0, 0xafb5 },
6461 { 0x07, 0, 0x0e00 },
6462 { 0x19, 0, 0xec80 },
6463 { 0x01, 0, 0x2e65 },
6468 rtl_csi_access_enable_2(tp);
6470 RTL_W8(DBG_REG, FIX_NAK_1);
6472 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6475 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6476 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6478 cfg1 = RTL_R8(Config1);
6479 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6480 RTL_W8(Config1, cfg1 & ~LEDS0);
6482 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
6485 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
6487 void __iomem *ioaddr = tp->mmio_addr;
6488 struct pci_dev *pdev = tp->pci_dev;
6490 rtl_csi_access_enable_2(tp);
6492 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6494 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6495 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6498 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
6500 rtl_hw_start_8102e_2(tp);
6502 rtl_ephy_write(tp, 0x03, 0xc2f9);
6505 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
6507 void __iomem *ioaddr = tp->mmio_addr;
6508 static const struct ephy_info e_info_8105e_1[] = {
6509 { 0x07, 0, 0x4000 },
6510 { 0x19, 0, 0x0200 },
6511 { 0x19, 0, 0x0020 },
6512 { 0x1e, 0, 0x2000 },
6513 { 0x03, 0, 0x0001 },
6514 { 0x19, 0, 0x0100 },
6515 { 0x19, 0, 0x0004 },
6519 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6520 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6522 /* Disable Early Tally Counter */
6523 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6525 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6526 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6528 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
6530 rtl_pcie_state_l2l3_enable(tp, false);
6533 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
6535 rtl_hw_start_8105e_1(tp);
6536 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
6539 static void rtl_hw_start_8402(struct rtl8169_private *tp)
6541 void __iomem *ioaddr = tp->mmio_addr;
6542 static const struct ephy_info e_info_8402[] = {
6543 { 0x19, 0xffff, 0xff64 },
6547 rtl_csi_access_enable_2(tp);
6549 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6550 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6552 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6553 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6555 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
6557 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6559 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6560 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
6561 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6562 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6563 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6564 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6565 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
6567 rtl_pcie_state_l2l3_enable(tp, false);
6570 static void rtl_hw_start_8106(struct rtl8169_private *tp)
6572 void __iomem *ioaddr = tp->mmio_addr;
6574 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6575 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6577 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6578 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6579 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6581 rtl_pcie_state_l2l3_enable(tp, false);
6584 static void rtl_hw_start_8101(struct net_device *dev)
6586 struct rtl8169_private *tp = netdev_priv(dev);
6587 void __iomem *ioaddr = tp->mmio_addr;
6588 struct pci_dev *pdev = tp->pci_dev;
6590 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6591 tp->event_slow &= ~RxFIFOOver;
6593 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
6594 tp->mac_version == RTL_GIGA_MAC_VER_16)
6595 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6596 PCI_EXP_DEVCTL_NOSNOOP_EN);
6598 RTL_W8(Cfg9346, Cfg9346_Unlock);
6600 RTL_W8(MaxTxPacketSize, TxPacketMax);
6602 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6604 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6605 RTL_W16(CPlusCmd, tp->cp_cmd);
6607 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6609 rtl_set_rx_tx_config_registers(tp);
6611 switch (tp->mac_version) {
6612 case RTL_GIGA_MAC_VER_07:
6613 rtl_hw_start_8102e_1(tp);
6616 case RTL_GIGA_MAC_VER_08:
6617 rtl_hw_start_8102e_3(tp);
6620 case RTL_GIGA_MAC_VER_09:
6621 rtl_hw_start_8102e_2(tp);
6624 case RTL_GIGA_MAC_VER_29:
6625 rtl_hw_start_8105e_1(tp);
6627 case RTL_GIGA_MAC_VER_30:
6628 rtl_hw_start_8105e_2(tp);
6631 case RTL_GIGA_MAC_VER_37:
6632 rtl_hw_start_8402(tp);
6635 case RTL_GIGA_MAC_VER_39:
6636 rtl_hw_start_8106(tp);
6638 case RTL_GIGA_MAC_VER_43:
6639 rtl_hw_start_8168g_2(tp);
6641 case RTL_GIGA_MAC_VER_47:
6642 case RTL_GIGA_MAC_VER_48:
6643 rtl_hw_start_8168h_1(tp);
6647 RTL_W8(Cfg9346, Cfg9346_Lock);
6649 RTL_W16(IntrMitigate, 0x0000);
6651 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6653 rtl_set_rx_mode(dev);
6657 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
6660 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6662 struct rtl8169_private *tp = netdev_priv(dev);
6664 if (new_mtu < ETH_ZLEN ||
6665 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
6668 if (new_mtu > ETH_DATA_LEN)
6669 rtl_hw_jumbo_enable(tp);
6671 rtl_hw_jumbo_disable(tp);
6674 netdev_update_features(dev);
6679 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6681 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
6682 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6685 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6686 void **data_buff, struct RxDesc *desc)
6688 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
6693 rtl8169_make_unusable_by_asic(desc);
6696 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6698 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6700 /* Force memory writes to complete before releasing descriptor */
6703 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6706 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6709 desc->addr = cpu_to_le64(mapping);
6710 rtl8169_mark_to_asic(desc, rx_buf_sz);
6713 static inline void *rtl8169_align(void *data)
6715 return (void *)ALIGN((long)data, 16);
6718 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6719 struct RxDesc *desc)
6723 struct device *d = &tp->pci_dev->dev;
6724 struct net_device *dev = tp->dev;
6725 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
6727 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6731 if (rtl8169_align(data) != data) {
6733 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6738 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
6740 if (unlikely(dma_mapping_error(d, mapping))) {
6741 if (net_ratelimit())
6742 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
6746 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
6754 static void rtl8169_rx_clear(struct rtl8169_private *tp)
6758 for (i = 0; i < NUM_RX_DESC; i++) {
6759 if (tp->Rx_databuff[i]) {
6760 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
6761 tp->RxDescArray + i);
6766 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
6768 desc->opts1 |= cpu_to_le32(RingEnd);
6771 static int rtl8169_rx_fill(struct rtl8169_private *tp)
6775 for (i = 0; i < NUM_RX_DESC; i++) {
6778 if (tp->Rx_databuff[i])
6781 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
6783 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
6786 tp->Rx_databuff[i] = data;
6789 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6793 rtl8169_rx_clear(tp);
6797 static int rtl8169_init_ring(struct net_device *dev)
6799 struct rtl8169_private *tp = netdev_priv(dev);
6801 rtl8169_init_ring_indexes(tp);
6803 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
6804 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
6806 return rtl8169_rx_fill(tp);
6809 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
6810 struct TxDesc *desc)
6812 unsigned int len = tx_skb->len;
6814 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6822 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6827 for (i = 0; i < n; i++) {
6828 unsigned int entry = (start + i) % NUM_TX_DESC;
6829 struct ring_info *tx_skb = tp->tx_skb + entry;
6830 unsigned int len = tx_skb->len;
6833 struct sk_buff *skb = tx_skb->skb;
6835 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
6836 tp->TxDescArray + entry);
6838 tp->dev->stats.tx_dropped++;
6839 dev_kfree_skb_any(skb);
6846 static void rtl8169_tx_clear(struct rtl8169_private *tp)
6848 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
6849 tp->cur_tx = tp->dirty_tx = 0;
6852 static void rtl_reset_work(struct rtl8169_private *tp)
6854 struct net_device *dev = tp->dev;
6857 napi_disable(&tp->napi);
6858 netif_stop_queue(dev);
6859 synchronize_sched();
6861 rtl8169_hw_reset(tp);
6863 for (i = 0; i < NUM_RX_DESC; i++)
6864 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6866 rtl8169_tx_clear(tp);
6867 rtl8169_init_ring_indexes(tp);
6869 napi_enable(&tp->napi);
6871 netif_wake_queue(dev);
6872 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
6875 static void rtl8169_tx_timeout(struct net_device *dev)
6877 struct rtl8169_private *tp = netdev_priv(dev);
6879 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6882 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
6885 struct skb_shared_info *info = skb_shinfo(skb);
6886 unsigned int cur_frag, entry;
6887 struct TxDesc *uninitialized_var(txd);
6888 struct device *d = &tp->pci_dev->dev;
6891 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
6892 const skb_frag_t *frag = info->frags + cur_frag;
6897 entry = (entry + 1) % NUM_TX_DESC;
6899 txd = tp->TxDescArray + entry;
6900 len = skb_frag_size(frag);
6901 addr = skb_frag_address(frag);
6902 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
6903 if (unlikely(dma_mapping_error(d, mapping))) {
6904 if (net_ratelimit())
6905 netif_err(tp, drv, tp->dev,
6906 "Failed to map TX fragments DMA!\n");
6910 /* Anti gcc 2.95.3 bugware (sic) */
6911 status = opts[0] | len |
6912 (RingEnd * !((entry + 1) % NUM_TX_DESC));
6914 txd->opts1 = cpu_to_le32(status);
6915 txd->opts2 = cpu_to_le32(opts[1]);
6916 txd->addr = cpu_to_le64(mapping);
6918 tp->tx_skb[entry].len = len;
6922 tp->tx_skb[entry].skb = skb;
6923 txd->opts1 |= cpu_to_le32(LastFrag);
6929 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6933 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6935 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6938 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6939 struct net_device *dev);
6940 /* r8169_csum_workaround()
6941 * The hw limites the value the transport offset. When the offset is out of the
6942 * range, calculate the checksum by sw.
6944 static void r8169_csum_workaround(struct rtl8169_private *tp,
6945 struct sk_buff *skb)
6947 if (skb_shinfo(skb)->gso_size) {
6948 netdev_features_t features = tp->dev->features;
6949 struct sk_buff *segs, *nskb;
6951 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6952 segs = skb_gso_segment(skb, features);
6953 if (IS_ERR(segs) || !segs)
6960 rtl8169_start_xmit(nskb, tp->dev);
6963 dev_consume_skb_any(skb);
6964 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6965 if (skb_checksum_help(skb) < 0)
6968 rtl8169_start_xmit(skb, tp->dev);
6970 struct net_device_stats *stats;
6973 stats = &tp->dev->stats;
6974 stats->tx_dropped++;
6975 dev_kfree_skb_any(skb);
6979 /* msdn_giant_send_check()
6980 * According to the document of microsoft, the TCP Pseudo Header excludes the
6981 * packet length for IPv6 TCP large packets.
6983 static int msdn_giant_send_check(struct sk_buff *skb)
6985 const struct ipv6hdr *ipv6h;
6989 ret = skb_cow_head(skb, 0);
6993 ipv6h = ipv6_hdr(skb);
6997 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7002 static inline __be16 get_protocol(struct sk_buff *skb)
7006 if (skb->protocol == htons(ETH_P_8021Q))
7007 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7009 protocol = skb->protocol;
7014 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7015 struct sk_buff *skb, u32 *opts)
7017 u32 mss = skb_shinfo(skb)->gso_size;
7021 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7022 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7023 const struct iphdr *ip = ip_hdr(skb);
7025 if (ip->protocol == IPPROTO_TCP)
7026 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7027 else if (ip->protocol == IPPROTO_UDP)
7028 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7036 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7037 struct sk_buff *skb, u32 *opts)
7039 u32 transport_offset = (u32)skb_transport_offset(skb);
7040 u32 mss = skb_shinfo(skb)->gso_size;
7043 if (transport_offset > GTTCPHO_MAX) {
7044 netif_warn(tp, tx_err, tp->dev,
7045 "Invalid transport offset 0x%x for TSO\n",
7050 switch (get_protocol(skb)) {
7051 case htons(ETH_P_IP):
7052 opts[0] |= TD1_GTSENV4;
7055 case htons(ETH_P_IPV6):
7056 if (msdn_giant_send_check(skb))
7059 opts[0] |= TD1_GTSENV6;
7067 opts[0] |= transport_offset << GTTCPHO_SHIFT;
7068 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
7069 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7072 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7073 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
7075 if (transport_offset > TCPHO_MAX) {
7076 netif_warn(tp, tx_err, tp->dev,
7077 "Invalid transport offset 0x%x\n",
7082 switch (get_protocol(skb)) {
7083 case htons(ETH_P_IP):
7084 opts[1] |= TD1_IPv4_CS;
7085 ip_protocol = ip_hdr(skb)->protocol;
7088 case htons(ETH_P_IPV6):
7089 opts[1] |= TD1_IPv6_CS;
7090 ip_protocol = ipv6_hdr(skb)->nexthdr;
7094 ip_protocol = IPPROTO_RAW;
7098 if (ip_protocol == IPPROTO_TCP)
7099 opts[1] |= TD1_TCP_CS;
7100 else if (ip_protocol == IPPROTO_UDP)
7101 opts[1] |= TD1_UDP_CS;
7105 opts[1] |= transport_offset << TCPHO_SHIFT;
7107 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7108 return !eth_skb_pad(skb);
7114 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7115 struct net_device *dev)
7117 struct rtl8169_private *tp = netdev_priv(dev);
7118 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
7119 struct TxDesc *txd = tp->TxDescArray + entry;
7120 void __iomem *ioaddr = tp->mmio_addr;
7121 struct device *d = &tp->pci_dev->dev;
7127 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
7128 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
7132 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
7135 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7138 if (!tp->tso_csum(tp, skb, opts)) {
7139 r8169_csum_workaround(tp, skb);
7140 return NETDEV_TX_OK;
7143 len = skb_headlen(skb);
7144 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
7145 if (unlikely(dma_mapping_error(d, mapping))) {
7146 if (net_ratelimit())
7147 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
7151 tp->tx_skb[entry].len = len;
7152 txd->addr = cpu_to_le64(mapping);
7154 frags = rtl8169_xmit_frags(tp, skb, opts);
7158 opts[0] |= FirstFrag;
7160 opts[0] |= FirstFrag | LastFrag;
7161 tp->tx_skb[entry].skb = skb;
7164 txd->opts2 = cpu_to_le32(opts[1]);
7166 skb_tx_timestamp(skb);
7168 /* Force memory writes to complete before releasing descriptor */
7171 /* Anti gcc 2.95.3 bugware (sic) */
7172 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
7173 txd->opts1 = cpu_to_le32(status);
7175 /* Force all memory writes to complete before notifying device */
7178 tp->cur_tx += frags + 1;
7180 RTL_W8(TxPoll, NPQ);
7184 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
7185 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7186 * not miss a ring update when it notices a stopped queue.
7189 netif_stop_queue(dev);
7190 /* Sync with rtl_tx:
7191 * - publish queue status and cur_tx ring index (write barrier)
7192 * - refresh dirty_tx ring index (read barrier).
7193 * May the current thread have a pessimistic view of the ring
7194 * status and forget to wake up queue, a racing rtl_tx thread
7198 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
7199 netif_wake_queue(dev);
7202 return NETDEV_TX_OK;
7205 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
7207 dev_kfree_skb_any(skb);
7208 dev->stats.tx_dropped++;
7209 return NETDEV_TX_OK;
7212 netif_stop_queue(dev);
7213 dev->stats.tx_dropped++;
7214 return NETDEV_TX_BUSY;
7217 static void rtl8169_pcierr_interrupt(struct net_device *dev)
7219 struct rtl8169_private *tp = netdev_priv(dev);
7220 struct pci_dev *pdev = tp->pci_dev;
7221 u16 pci_status, pci_cmd;
7223 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7224 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7226 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7227 pci_cmd, pci_status);
7230 * The recovery sequence below admits a very elaborated explanation:
7231 * - it seems to work;
7232 * - I did not see what else could be done;
7233 * - it makes iop3xx happy.
7235 * Feel free to adjust to your needs.
7237 if (pdev->broken_parity_status)
7238 pci_cmd &= ~PCI_COMMAND_PARITY;
7240 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7242 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
7244 pci_write_config_word(pdev, PCI_STATUS,
7245 pci_status & (PCI_STATUS_DETECTED_PARITY |
7246 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7247 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7249 /* The infamous DAC f*ckup only happens at boot time */
7250 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
7251 void __iomem *ioaddr = tp->mmio_addr;
7253 netif_info(tp, intr, dev, "disabling PCI DAC\n");
7254 tp->cp_cmd &= ~PCIDAC;
7255 RTL_W16(CPlusCmd, tp->cp_cmd);
7256 dev->features &= ~NETIF_F_HIGHDMA;
7259 rtl8169_hw_reset(tp);
7261 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7264 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
7266 unsigned int dirty_tx, tx_left;
7268 dirty_tx = tp->dirty_tx;
7270 tx_left = tp->cur_tx - dirty_tx;
7272 while (tx_left > 0) {
7273 unsigned int entry = dirty_tx % NUM_TX_DESC;
7274 struct ring_info *tx_skb = tp->tx_skb + entry;
7277 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7278 if (status & DescOwn)
7281 /* This barrier is needed to keep us from reading
7282 * any other fields out of the Tx descriptor until
7283 * we know the status of DescOwn
7287 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7288 tp->TxDescArray + entry);
7289 if (status & LastFrag) {
7290 u64_stats_update_begin(&tp->tx_stats.syncp);
7291 tp->tx_stats.packets++;
7292 tp->tx_stats.bytes += tx_skb->skb->len;
7293 u64_stats_update_end(&tp->tx_stats.syncp);
7294 dev_kfree_skb_any(tx_skb->skb);
7301 if (tp->dirty_tx != dirty_tx) {
7302 tp->dirty_tx = dirty_tx;
7303 /* Sync with rtl8169_start_xmit:
7304 * - publish dirty_tx ring index (write barrier)
7305 * - refresh cur_tx ring index and queue status (read barrier)
7306 * May the current thread miss the stopped queue condition,
7307 * a racing xmit thread can only have a right view of the
7311 if (netif_queue_stopped(dev) &&
7312 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
7313 netif_wake_queue(dev);
7316 * 8168 hack: TxPoll requests are lost when the Tx packets are
7317 * too close. Let's kick an extra TxPoll request when a burst
7318 * of start_xmit activity is detected (if it is not detected,
7319 * it is slow enough). -- FR
7321 if (tp->cur_tx != dirty_tx) {
7322 void __iomem *ioaddr = tp->mmio_addr;
7324 RTL_W8(TxPoll, NPQ);
7329 static inline int rtl8169_fragmented_frame(u32 status)
7331 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7334 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
7336 u32 status = opts1 & RxProtoMask;
7338 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
7339 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
7340 skb->ip_summed = CHECKSUM_UNNECESSARY;
7342 skb_checksum_none_assert(skb);
7345 static struct sk_buff *rtl8169_try_rx_copy(void *data,
7346 struct rtl8169_private *tp,
7350 struct sk_buff *skb;
7351 struct device *d = &tp->pci_dev->dev;
7353 data = rtl8169_align(data);
7354 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
7356 skb = napi_alloc_skb(&tp->napi, pkt_size);
7358 memcpy(skb->data, data, pkt_size);
7359 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7364 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
7366 unsigned int cur_rx, rx_left;
7369 cur_rx = tp->cur_rx;
7371 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
7372 unsigned int entry = cur_rx % NUM_RX_DESC;
7373 struct RxDesc *desc = tp->RxDescArray + entry;
7376 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
7377 if (status & DescOwn)
7380 /* This barrier is needed to keep us from reading
7381 * any other fields out of the Rx descriptor until
7382 * we know the status of DescOwn
7386 if (unlikely(status & RxRES)) {
7387 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7389 dev->stats.rx_errors++;
7390 if (status & (RxRWT | RxRUNT))
7391 dev->stats.rx_length_errors++;
7393 dev->stats.rx_crc_errors++;
7394 if (status & RxFOVF) {
7395 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7396 dev->stats.rx_fifo_errors++;
7398 if ((status & (RxRUNT | RxCRC)) &&
7399 !(status & (RxRWT | RxFOVF)) &&
7400 (dev->features & NETIF_F_RXALL))
7403 struct sk_buff *skb;
7408 addr = le64_to_cpu(desc->addr);
7409 if (likely(!(dev->features & NETIF_F_RXFCS)))
7410 pkt_size = (status & 0x00003fff) - 4;
7412 pkt_size = status & 0x00003fff;
7415 * The driver does not support incoming fragmented
7416 * frames. They are seen as a symptom of over-mtu
7419 if (unlikely(rtl8169_fragmented_frame(status))) {
7420 dev->stats.rx_dropped++;
7421 dev->stats.rx_length_errors++;
7422 goto release_descriptor;
7425 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7426 tp, pkt_size, addr);
7428 dev->stats.rx_dropped++;
7429 goto release_descriptor;
7432 rtl8169_rx_csum(skb, status);
7433 skb_put(skb, pkt_size);
7434 skb->protocol = eth_type_trans(skb, dev);
7436 rtl8169_rx_vlan_tag(desc, skb);
7438 if (skb->pkt_type == PACKET_MULTICAST)
7439 dev->stats.multicast++;
7441 napi_gro_receive(&tp->napi, skb);
7443 u64_stats_update_begin(&tp->rx_stats.syncp);
7444 tp->rx_stats.packets++;
7445 tp->rx_stats.bytes += pkt_size;
7446 u64_stats_update_end(&tp->rx_stats.syncp);
7450 rtl8169_mark_to_asic(desc, rx_buf_sz);
7453 count = cur_rx - tp->cur_rx;
7454 tp->cur_rx = cur_rx;
7459 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
7461 struct net_device *dev = dev_instance;
7462 struct rtl8169_private *tp = netdev_priv(dev);
7466 status = rtl_get_events(tp);
7467 if (status && status != 0xffff) {
7468 status &= RTL_EVENT_NAPI | tp->event_slow;
7472 rtl_irq_disable(tp);
7473 napi_schedule(&tp->napi);
7476 return IRQ_RETVAL(handled);
7480 * Workqueue context.
7482 static void rtl_slow_event_work(struct rtl8169_private *tp)
7484 struct net_device *dev = tp->dev;
7487 status = rtl_get_events(tp) & tp->event_slow;
7488 rtl_ack_events(tp, status);
7490 if (unlikely(status & RxFIFOOver)) {
7491 switch (tp->mac_version) {
7492 /* Work around for rx fifo overflow */
7493 case RTL_GIGA_MAC_VER_11:
7494 netif_stop_queue(dev);
7495 /* XXX - Hack alert. See rtl_task(). */
7496 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
7502 if (unlikely(status & SYSErr))
7503 rtl8169_pcierr_interrupt(dev);
7505 if (status & LinkChg)
7506 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
7508 rtl_irq_enable_all(tp);
7511 static void rtl_task(struct work_struct *work)
7513 static const struct {
7515 void (*action)(struct rtl8169_private *);
7517 /* XXX - keep rtl_slow_event_work() as first element. */
7518 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7519 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7520 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7522 struct rtl8169_private *tp =
7523 container_of(work, struct rtl8169_private, wk.work);
7524 struct net_device *dev = tp->dev;
7529 if (!netif_running(dev) ||
7530 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
7533 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7536 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
7538 rtl_work[i].action(tp);
7542 rtl_unlock_work(tp);
7545 static int rtl8169_poll(struct napi_struct *napi, int budget)
7547 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7548 struct net_device *dev = tp->dev;
7549 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7553 status = rtl_get_events(tp);
7554 rtl_ack_events(tp, status & ~tp->event_slow);
7556 work_done = rtl_rx(dev, tp, (u32) budget);
7560 if (status & tp->event_slow) {
7561 enable_mask &= ~tp->event_slow;
7563 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7566 if (work_done < budget) {
7567 napi_complete(napi);
7569 rtl_irq_enable(tp, enable_mask);
7576 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7578 struct rtl8169_private *tp = netdev_priv(dev);
7580 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7583 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7584 RTL_W32(RxMissed, 0);
7587 static void rtl8169_down(struct net_device *dev)
7589 struct rtl8169_private *tp = netdev_priv(dev);
7590 void __iomem *ioaddr = tp->mmio_addr;
7592 del_timer_sync(&tp->timer);
7594 napi_disable(&tp->napi);
7595 netif_stop_queue(dev);
7597 rtl8169_hw_reset(tp);
7599 * At this point device interrupts can not be enabled in any function,
7600 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7601 * and napi is disabled (rtl8169_poll).
7603 rtl8169_rx_missed(dev, ioaddr);
7605 /* Give a racing hard_start_xmit a few cycles to complete. */
7606 synchronize_sched();
7608 rtl8169_tx_clear(tp);
7610 rtl8169_rx_clear(tp);
7612 rtl_pll_power_down(tp);
7615 static int rtl8169_close(struct net_device *dev)
7617 struct rtl8169_private *tp = netdev_priv(dev);
7618 struct pci_dev *pdev = tp->pci_dev;
7620 pm_runtime_get_sync(&pdev->dev);
7622 /* Update counters before going down */
7623 rtl8169_update_counters(dev);
7626 /* Clear all task flags */
7627 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
7630 rtl_unlock_work(tp);
7632 cancel_work_sync(&tp->wk.work);
7634 free_irq(pdev->irq, dev);
7636 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7638 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7640 tp->TxDescArray = NULL;
7641 tp->RxDescArray = NULL;
7643 pm_runtime_put_sync(&pdev->dev);
7648 #ifdef CONFIG_NET_POLL_CONTROLLER
7649 static void rtl8169_netpoll(struct net_device *dev)
7651 struct rtl8169_private *tp = netdev_priv(dev);
7653 rtl8169_interrupt(tp->pci_dev->irq, dev);
7657 static int rtl_open(struct net_device *dev)
7659 struct rtl8169_private *tp = netdev_priv(dev);
7660 void __iomem *ioaddr = tp->mmio_addr;
7661 struct pci_dev *pdev = tp->pci_dev;
7662 int retval = -ENOMEM;
7664 pm_runtime_get_sync(&pdev->dev);
7667 * Rx and Tx descriptors needs 256 bytes alignment.
7668 * dma_alloc_coherent provides more.
7670 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7671 &tp->TxPhyAddr, GFP_KERNEL);
7672 if (!tp->TxDescArray)
7673 goto err_pm_runtime_put;
7675 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7676 &tp->RxPhyAddr, GFP_KERNEL);
7677 if (!tp->RxDescArray)
7680 retval = rtl8169_init_ring(dev);
7684 INIT_WORK(&tp->wk.work, rtl_task);
7688 rtl_request_firmware(tp);
7690 retval = request_irq(pdev->irq, rtl8169_interrupt,
7691 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7694 goto err_release_fw_2;
7698 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7700 napi_enable(&tp->napi);
7702 rtl8169_init_phy(dev, tp);
7704 __rtl8169_set_features(dev, dev->features);
7706 rtl_pll_power_up(tp);
7710 if (!rtl8169_init_counter_offsets(dev))
7711 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7713 netif_start_queue(dev);
7715 rtl_unlock_work(tp);
7717 tp->saved_wolopts = 0;
7718 pm_runtime_put_noidle(&pdev->dev);
7720 rtl8169_check_link_status(dev, tp, ioaddr);
7725 rtl_release_firmware(tp);
7726 rtl8169_rx_clear(tp);
7728 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7730 tp->RxDescArray = NULL;
7732 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7734 tp->TxDescArray = NULL;
7736 pm_runtime_put_noidle(&pdev->dev);
7740 static struct rtnl_link_stats64 *
7741 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7743 struct rtl8169_private *tp = netdev_priv(dev);
7744 void __iomem *ioaddr = tp->mmio_addr;
7745 struct pci_dev *pdev = tp->pci_dev;
7746 struct rtl8169_counters *counters = tp->counters;
7749 pm_runtime_get_noresume(&pdev->dev);
7751 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
7752 rtl8169_rx_missed(dev, ioaddr);
7755 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
7756 stats->rx_packets = tp->rx_stats.packets;
7757 stats->rx_bytes = tp->rx_stats.bytes;
7758 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
7761 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
7762 stats->tx_packets = tp->tx_stats.packets;
7763 stats->tx_bytes = tp->tx_stats.bytes;
7764 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
7766 stats->rx_dropped = dev->stats.rx_dropped;
7767 stats->tx_dropped = dev->stats.tx_dropped;
7768 stats->rx_length_errors = dev->stats.rx_length_errors;
7769 stats->rx_errors = dev->stats.rx_errors;
7770 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7771 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7772 stats->rx_missed_errors = dev->stats.rx_missed_errors;
7773 stats->multicast = dev->stats.multicast;
7776 * Fetch additonal counter values missing in stats collected by driver
7777 * from tally counters.
7779 if (pm_runtime_active(&pdev->dev))
7780 rtl8169_update_counters(dev);
7783 * Subtract values fetched during initalization.
7784 * See rtl8169_init_counter_offsets for a description why we do that.
7786 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
7787 le64_to_cpu(tp->tc_offset.tx_errors);
7788 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
7789 le32_to_cpu(tp->tc_offset.tx_multi_collision);
7790 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
7791 le16_to_cpu(tp->tc_offset.tx_aborted);
7793 pm_runtime_put_noidle(&pdev->dev);
7798 static void rtl8169_net_suspend(struct net_device *dev)
7800 struct rtl8169_private *tp = netdev_priv(dev);
7802 if (!netif_running(dev))
7805 netif_device_detach(dev);
7806 netif_stop_queue(dev);
7809 napi_disable(&tp->napi);
7810 /* Clear all task flags */
7811 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
7813 rtl_unlock_work(tp);
7815 rtl_pll_power_down(tp);
7820 static int rtl8169_suspend(struct device *device)
7822 struct pci_dev *pdev = to_pci_dev(device);
7823 struct net_device *dev = pci_get_drvdata(pdev);
7825 rtl8169_net_suspend(dev);
7830 static void __rtl8169_resume(struct net_device *dev)
7832 struct rtl8169_private *tp = netdev_priv(dev);
7834 netif_device_attach(dev);
7836 rtl_pll_power_up(tp);
7839 napi_enable(&tp->napi);
7840 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7841 rtl_unlock_work(tp);
7843 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7846 static int rtl8169_resume(struct device *device)
7848 struct pci_dev *pdev = to_pci_dev(device);
7849 struct net_device *dev = pci_get_drvdata(pdev);
7850 struct rtl8169_private *tp = netdev_priv(dev);
7852 rtl8169_init_phy(dev, tp);
7854 if (netif_running(dev))
7855 __rtl8169_resume(dev);
7860 static int rtl8169_runtime_suspend(struct device *device)
7862 struct pci_dev *pdev = to_pci_dev(device);
7863 struct net_device *dev = pci_get_drvdata(pdev);
7864 struct rtl8169_private *tp = netdev_priv(dev);
7866 if (!tp->TxDescArray)
7870 tp->saved_wolopts = __rtl8169_get_wol(tp);
7871 __rtl8169_set_wol(tp, WAKE_ANY);
7872 rtl_unlock_work(tp);
7874 rtl8169_net_suspend(dev);
7876 /* Update counters before going runtime suspend */
7877 rtl8169_rx_missed(dev, tp->mmio_addr);
7878 rtl8169_update_counters(dev);
7883 static int rtl8169_runtime_resume(struct device *device)
7885 struct pci_dev *pdev = to_pci_dev(device);
7886 struct net_device *dev = pci_get_drvdata(pdev);
7887 struct rtl8169_private *tp = netdev_priv(dev);
7888 rtl_rar_set(tp, dev->dev_addr);
7890 if (!tp->TxDescArray)
7894 __rtl8169_set_wol(tp, tp->saved_wolopts);
7895 tp->saved_wolopts = 0;
7896 rtl_unlock_work(tp);
7898 rtl8169_init_phy(dev, tp);
7900 __rtl8169_resume(dev);
7905 static int rtl8169_runtime_idle(struct device *device)
7907 struct pci_dev *pdev = to_pci_dev(device);
7908 struct net_device *dev = pci_get_drvdata(pdev);
7909 struct rtl8169_private *tp = netdev_priv(dev);
7911 return tp->TxDescArray ? -EBUSY : 0;
7914 static const struct dev_pm_ops rtl8169_pm_ops = {
7915 .suspend = rtl8169_suspend,
7916 .resume = rtl8169_resume,
7917 .freeze = rtl8169_suspend,
7918 .thaw = rtl8169_resume,
7919 .poweroff = rtl8169_suspend,
7920 .restore = rtl8169_resume,
7921 .runtime_suspend = rtl8169_runtime_suspend,
7922 .runtime_resume = rtl8169_runtime_resume,
7923 .runtime_idle = rtl8169_runtime_idle,
7926 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
7928 #else /* !CONFIG_PM */
7930 #define RTL8169_PM_OPS NULL
7932 #endif /* !CONFIG_PM */
7934 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7936 void __iomem *ioaddr = tp->mmio_addr;
7938 /* WoL fails with 8168b when the receiver is disabled. */
7939 switch (tp->mac_version) {
7940 case RTL_GIGA_MAC_VER_11:
7941 case RTL_GIGA_MAC_VER_12:
7942 case RTL_GIGA_MAC_VER_17:
7943 pci_clear_master(tp->pci_dev);
7945 RTL_W8(ChipCmd, CmdRxEnb);
7954 static void rtl_shutdown(struct pci_dev *pdev)
7956 struct net_device *dev = pci_get_drvdata(pdev);
7957 struct rtl8169_private *tp = netdev_priv(dev);
7958 struct device *d = &pdev->dev;
7960 pm_runtime_get_sync(d);
7962 rtl8169_net_suspend(dev);
7964 /* Restore original MAC address */
7965 rtl_rar_set(tp, dev->perm_addr);
7967 rtl8169_hw_reset(tp);
7969 if (system_state == SYSTEM_POWER_OFF) {
7970 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7971 rtl_wol_suspend_quirk(tp);
7972 rtl_wol_shutdown_quirk(tp);
7975 pci_wake_from_d3(pdev, true);
7976 pci_set_power_state(pdev, PCI_D3hot);
7979 pm_runtime_put_noidle(d);
7982 static void rtl_remove_one(struct pci_dev *pdev)
7984 struct net_device *dev = pci_get_drvdata(pdev);
7985 struct rtl8169_private *tp = netdev_priv(dev);
7987 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7988 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
7989 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
7990 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
7991 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
7992 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
7993 r8168_check_dash(tp)) {
7994 rtl8168_driver_stop(tp);
7997 netif_napi_del(&tp->napi);
7999 unregister_netdev(dev);
8001 dma_free_coherent(&tp->pci_dev->dev, sizeof(*tp->counters),
8002 tp->counters, tp->counters_phys_addr);
8004 rtl_release_firmware(tp);
8006 if (pci_dev_run_wake(pdev))
8007 pm_runtime_get_noresume(&pdev->dev);
8009 /* restore original MAC address */
8010 rtl_rar_set(tp, dev->perm_addr);
8012 rtl_disable_msi(pdev, tp);
8013 rtl8169_release_board(pdev, dev, tp->mmio_addr);
8016 static const struct net_device_ops rtl_netdev_ops = {
8017 .ndo_open = rtl_open,
8018 .ndo_stop = rtl8169_close,
8019 .ndo_get_stats64 = rtl8169_get_stats64,
8020 .ndo_start_xmit = rtl8169_start_xmit,
8021 .ndo_tx_timeout = rtl8169_tx_timeout,
8022 .ndo_validate_addr = eth_validate_addr,
8023 .ndo_change_mtu = rtl8169_change_mtu,
8024 .ndo_fix_features = rtl8169_fix_features,
8025 .ndo_set_features = rtl8169_set_features,
8026 .ndo_set_mac_address = rtl_set_mac_address,
8027 .ndo_do_ioctl = rtl8169_ioctl,
8028 .ndo_set_rx_mode = rtl_set_rx_mode,
8029 #ifdef CONFIG_NET_POLL_CONTROLLER
8030 .ndo_poll_controller = rtl8169_netpoll,
8035 static const struct rtl_cfg_info {
8036 void (*hw_start)(struct net_device *);
8037 unsigned int region;
8042 } rtl_cfg_infos [] = {
8044 .hw_start = rtl_hw_start_8169,
8047 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8048 .features = RTL_FEATURE_GMII,
8049 .default_ver = RTL_GIGA_MAC_VER_01,
8052 .hw_start = rtl_hw_start_8168,
8055 .event_slow = SYSErr | LinkChg | RxOverflow,
8056 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
8057 .default_ver = RTL_GIGA_MAC_VER_11,
8060 .hw_start = rtl_hw_start_8101,
8063 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8065 .features = RTL_FEATURE_MSI,
8066 .default_ver = RTL_GIGA_MAC_VER_13,
8070 /* Cfg9346_Unlock assumed. */
8071 static unsigned rtl_try_msi(struct rtl8169_private *tp,
8072 const struct rtl_cfg_info *cfg)
8074 void __iomem *ioaddr = tp->mmio_addr;
8078 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8079 if (cfg->features & RTL_FEATURE_MSI) {
8080 if (pci_enable_msi(tp->pci_dev)) {
8081 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8084 msi = RTL_FEATURE_MSI;
8087 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8088 RTL_W8(Config2, cfg2);
8092 DECLARE_RTL_COND(rtl_link_list_ready_cond)
8094 void __iomem *ioaddr = tp->mmio_addr;
8096 return RTL_R8(MCU) & LINK_LIST_RDY;
8099 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8101 void __iomem *ioaddr = tp->mmio_addr;
8103 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8106 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
8108 void __iomem *ioaddr = tp->mmio_addr;
8111 tp->ocp_base = OCP_STD_PHY_BASE;
8113 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8115 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8118 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8121 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8123 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8125 data = r8168_mac_ocp_read(tp, 0xe8de);
8127 r8168_mac_ocp_write(tp, 0xe8de, data);
8129 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8132 data = r8168_mac_ocp_read(tp, 0xe8de);
8134 r8168_mac_ocp_write(tp, 0xe8de, data);
8136 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8140 static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8142 rtl8168ep_stop_cmac(tp);
8143 rtl_hw_init_8168g(tp);
8146 static void rtl_hw_initialize(struct rtl8169_private *tp)
8148 switch (tp->mac_version) {
8149 case RTL_GIGA_MAC_VER_40:
8150 case RTL_GIGA_MAC_VER_41:
8151 case RTL_GIGA_MAC_VER_42:
8152 case RTL_GIGA_MAC_VER_43:
8153 case RTL_GIGA_MAC_VER_44:
8154 case RTL_GIGA_MAC_VER_45:
8155 case RTL_GIGA_MAC_VER_46:
8156 case RTL_GIGA_MAC_VER_47:
8157 case RTL_GIGA_MAC_VER_48:
8158 rtl_hw_init_8168g(tp);
8160 case RTL_GIGA_MAC_VER_49:
8161 case RTL_GIGA_MAC_VER_50:
8162 case RTL_GIGA_MAC_VER_51:
8163 rtl_hw_init_8168ep(tp);
8170 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8172 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8173 const unsigned int region = cfg->region;
8174 struct rtl8169_private *tp;
8175 struct mii_if_info *mii;
8176 struct net_device *dev;
8177 void __iomem *ioaddr;
8181 if (netif_msg_drv(&debug)) {
8182 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8183 MODULENAME, RTL8169_VERSION);
8186 dev = alloc_etherdev(sizeof (*tp));
8192 SET_NETDEV_DEV(dev, &pdev->dev);
8193 dev->netdev_ops = &rtl_netdev_ops;
8194 tp = netdev_priv(dev);
8197 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8201 mii->mdio_read = rtl_mdio_read;
8202 mii->mdio_write = rtl_mdio_write;
8203 mii->phy_id_mask = 0x1f;
8204 mii->reg_num_mask = 0x1f;
8205 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8207 /* disable ASPM completely as that cause random device stop working
8208 * problems as well as full system hangs for some PCIe devices users */
8209 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8210 PCIE_LINK_STATE_CLKPM);
8212 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8213 rc = pci_enable_device(pdev);
8215 netif_err(tp, probe, dev, "enable failure\n");
8216 goto err_out_free_dev_1;
8219 if (pci_set_mwi(pdev) < 0)
8220 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8222 /* make sure PCI base addr 1 is MMIO */
8223 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8224 netif_err(tp, probe, dev,
8225 "region #%d not an MMIO resource, aborting\n",
8231 /* check for weird/broken PCI region reporting */
8232 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8233 netif_err(tp, probe, dev,
8234 "Invalid PCI region size(s), aborting\n");
8239 rc = pci_request_regions(pdev, MODULENAME);
8241 netif_err(tp, probe, dev, "could not request regions\n");
8245 /* ioremap MMIO region */
8246 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
8248 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8250 goto err_out_free_res_3;
8252 tp->mmio_addr = ioaddr;
8254 if (!pci_is_pcie(pdev))
8255 netif_info(tp, probe, dev, "not PCI Express\n");
8257 /* Identify chip attached to board */
8258 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8262 if ((sizeof(dma_addr_t) > 4) &&
8263 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8264 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
8265 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8266 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
8268 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8269 if (!pci_is_pcie(pdev))
8270 tp->cp_cmd |= PCIDAC;
8271 dev->features |= NETIF_F_HIGHDMA;
8273 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8275 netif_err(tp, probe, dev, "DMA configuration failed\n");
8276 goto err_out_unmap_4;
8282 rtl_irq_disable(tp);
8284 rtl_hw_initialize(tp);
8288 rtl_ack_events(tp, 0xffff);
8290 pci_set_master(pdev);
8292 rtl_init_mdio_ops(tp);
8293 rtl_init_pll_power_ops(tp);
8294 rtl_init_jumbo_ops(tp);
8295 rtl_init_csi_ops(tp);
8297 rtl8169_print_mac_version(tp);
8299 chipset = tp->mac_version;
8300 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8302 RTL_W8(Cfg9346, Cfg9346_Unlock);
8303 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
8304 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
8305 switch (tp->mac_version) {
8306 case RTL_GIGA_MAC_VER_34:
8307 case RTL_GIGA_MAC_VER_35:
8308 case RTL_GIGA_MAC_VER_36:
8309 case RTL_GIGA_MAC_VER_37:
8310 case RTL_GIGA_MAC_VER_38:
8311 case RTL_GIGA_MAC_VER_40:
8312 case RTL_GIGA_MAC_VER_41:
8313 case RTL_GIGA_MAC_VER_42:
8314 case RTL_GIGA_MAC_VER_43:
8315 case RTL_GIGA_MAC_VER_44:
8316 case RTL_GIGA_MAC_VER_45:
8317 case RTL_GIGA_MAC_VER_46:
8318 case RTL_GIGA_MAC_VER_47:
8319 case RTL_GIGA_MAC_VER_48:
8320 case RTL_GIGA_MAC_VER_49:
8321 case RTL_GIGA_MAC_VER_50:
8322 case RTL_GIGA_MAC_VER_51:
8323 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
8324 tp->features |= RTL_FEATURE_WOL;
8325 if ((RTL_R8(Config3) & LinkUp) != 0)
8326 tp->features |= RTL_FEATURE_WOL;
8329 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
8330 tp->features |= RTL_FEATURE_WOL;
8333 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
8334 tp->features |= RTL_FEATURE_WOL;
8335 tp->features |= rtl_try_msi(tp, cfg);
8336 RTL_W8(Cfg9346, Cfg9346_Lock);
8338 if (rtl_tbi_enabled(tp)) {
8339 tp->set_speed = rtl8169_set_speed_tbi;
8340 tp->get_settings = rtl8169_gset_tbi;
8341 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8342 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8343 tp->link_ok = rtl8169_tbi_link_ok;
8344 tp->do_ioctl = rtl_tbi_ioctl;
8346 tp->set_speed = rtl8169_set_speed_xmii;
8347 tp->get_settings = rtl8169_gset_xmii;
8348 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8349 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8350 tp->link_ok = rtl8169_xmii_link_ok;
8351 tp->do_ioctl = rtl_xmii_ioctl;
8354 mutex_init(&tp->wk.mutex);
8355 u64_stats_init(&tp->rx_stats.syncp);
8356 u64_stats_init(&tp->tx_stats.syncp);
8358 /* Get MAC address */
8359 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8360 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8361 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8362 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8363 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8364 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8365 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8366 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8367 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8368 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
8369 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8370 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
8371 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8372 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8373 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8374 tp->mac_version == RTL_GIGA_MAC_VER_51) {
8377 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8378 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
8380 if (is_valid_ether_addr((u8 *)mac_addr))
8381 rtl_rar_set(tp, (u8 *)mac_addr);
8383 for (i = 0; i < ETH_ALEN; i++)
8384 dev->dev_addr[i] = RTL_R8(MAC0 + i);
8386 dev->ethtool_ops = &rtl8169_ethtool_ops;
8387 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
8389 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8391 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8392 * properly for all devices */
8393 dev->features |= NETIF_F_RXCSUM |
8394 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
8396 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8397 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8398 NETIF_F_HW_VLAN_CTAG_RX;
8399 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8402 tp->cp_cmd |= RxChkSum | RxVlan;
8405 * Pretend we are using VLANs; This bypasses a nasty bug where
8406 * Interrupts stop flowing on high load on 8110SCd controllers.
8408 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
8409 /* Disallow toggling */
8410 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
8412 if (tp->txd_version == RTL_TD_0)
8413 tp->tso_csum = rtl8169_tso_csum_v1;
8414 else if (tp->txd_version == RTL_TD_1) {
8415 tp->tso_csum = rtl8169_tso_csum_v2;
8416 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8420 dev->hw_features |= NETIF_F_RXALL;
8421 dev->hw_features |= NETIF_F_RXFCS;
8423 tp->hw_start = cfg->hw_start;
8424 tp->event_slow = cfg->event_slow;
8426 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8427 ~(RxBOVF | RxFOVF) : ~0;
8429 init_timer(&tp->timer);
8430 tp->timer.data = (unsigned long) dev;
8431 tp->timer.function = rtl8169_phy_timer;
8433 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8435 tp->counters = dma_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8436 &tp->counters_phys_addr, GFP_KERNEL);
8437 if (!tp->counters) {
8442 pci_set_drvdata(pdev, dev);
8444 rc = register_netdev(dev);
8448 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8449 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8450 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
8451 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8452 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8453 "tx checksumming: %s]\n",
8454 rtl_chip_infos[chipset].jumbo_max,
8455 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8458 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8459 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
8460 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8461 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8462 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8463 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
8464 r8168_check_dash(tp)) {
8465 rtl8168_driver_start(tp);
8468 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
8470 if (pci_dev_run_wake(pdev))
8471 pm_runtime_put_noidle(&pdev->dev);
8473 netif_carrier_off(dev);
8479 dma_free_coherent(&pdev->dev, sizeof(*tp->counters), tp->counters,
8480 tp->counters_phys_addr);
8482 netif_napi_del(&tp->napi);
8483 rtl_disable_msi(pdev, tp);
8487 pci_release_regions(pdev);
8489 pci_clear_mwi(pdev);
8490 pci_disable_device(pdev);
8496 static struct pci_driver rtl8169_pci_driver = {
8498 .id_table = rtl8169_pci_tbl,
8499 .probe = rtl_init_one,
8500 .remove = rtl_remove_one,
8501 .shutdown = rtl_shutdown,
8502 .driver.pm = RTL8169_PM_OPS,
8505 module_pci_driver(rtl8169_pci_driver);