2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/if_vlan.h>
20 #include <linux/crc32.h>
23 #include <linux/tcp.h>
24 #include <linux/interrupt.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/firmware.h>
28 #include <linux/pci-aspm.h>
29 #include <linux/prefetch.h>
30 #include <linux/ipv6.h>
31 #include <net/ip6_checksum.h>
36 #define RTL8169_VERSION "2.3LK-NAPI"
37 #define MODULENAME "r8169"
38 #define PFX MODULENAME ": "
40 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
42 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
44 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
45 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
47 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
48 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
49 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
50 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
51 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
52 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
53 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
54 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
55 #define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56 #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57 #define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58 #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
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 TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
88 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
90 #define R8169_REGS_SIZE 256
91 #define R8169_RX_BUF_SIZE (SZ_16K - 1)
92 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
93 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
94 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
95 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
97 #define RTL8169_TX_TIMEOUT (6*HZ)
98 #define RTL8169_PHY_TIMEOUT (10*HZ)
100 /* write/read MMIO register */
101 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
102 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
103 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
104 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
105 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
106 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
109 RTL_GIGA_MAC_VER_01 = 0,
160 RTL_GIGA_MAC_NONE = 0xff,
163 enum rtl_tx_desc_version {
168 #define JUMBO_1K ETH_DATA_LEN
169 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
170 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
171 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
172 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
174 #define _R(NAME,TD,FW,SZ,B) { \
182 static const struct {
184 enum rtl_tx_desc_version txd_version;
188 } rtl_chip_infos[] = {
190 [RTL_GIGA_MAC_VER_01] =
191 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
192 [RTL_GIGA_MAC_VER_02] =
193 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
194 [RTL_GIGA_MAC_VER_03] =
195 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
196 [RTL_GIGA_MAC_VER_04] =
197 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
198 [RTL_GIGA_MAC_VER_05] =
199 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
200 [RTL_GIGA_MAC_VER_06] =
201 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
203 [RTL_GIGA_MAC_VER_07] =
204 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
205 [RTL_GIGA_MAC_VER_08] =
206 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
207 [RTL_GIGA_MAC_VER_09] =
208 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
209 [RTL_GIGA_MAC_VER_10] =
210 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
211 [RTL_GIGA_MAC_VER_11] =
212 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
213 [RTL_GIGA_MAC_VER_12] =
214 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
215 [RTL_GIGA_MAC_VER_13] =
216 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
217 [RTL_GIGA_MAC_VER_14] =
218 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
219 [RTL_GIGA_MAC_VER_15] =
220 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
221 [RTL_GIGA_MAC_VER_16] =
222 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
223 [RTL_GIGA_MAC_VER_17] =
224 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
225 [RTL_GIGA_MAC_VER_18] =
226 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
227 [RTL_GIGA_MAC_VER_19] =
228 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
229 [RTL_GIGA_MAC_VER_20] =
230 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
231 [RTL_GIGA_MAC_VER_21] =
232 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
233 [RTL_GIGA_MAC_VER_22] =
234 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
235 [RTL_GIGA_MAC_VER_23] =
236 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
237 [RTL_GIGA_MAC_VER_24] =
238 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
239 [RTL_GIGA_MAC_VER_25] =
240 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
242 [RTL_GIGA_MAC_VER_26] =
243 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
245 [RTL_GIGA_MAC_VER_27] =
246 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
247 [RTL_GIGA_MAC_VER_28] =
248 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
249 [RTL_GIGA_MAC_VER_29] =
250 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
252 [RTL_GIGA_MAC_VER_30] =
253 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
255 [RTL_GIGA_MAC_VER_31] =
256 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
257 [RTL_GIGA_MAC_VER_32] =
258 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
260 [RTL_GIGA_MAC_VER_33] =
261 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
263 [RTL_GIGA_MAC_VER_34] =
264 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
266 [RTL_GIGA_MAC_VER_35] =
267 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
269 [RTL_GIGA_MAC_VER_36] =
270 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
272 [RTL_GIGA_MAC_VER_37] =
273 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
275 [RTL_GIGA_MAC_VER_38] =
276 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
278 [RTL_GIGA_MAC_VER_39] =
279 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
281 [RTL_GIGA_MAC_VER_40] =
282 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
284 [RTL_GIGA_MAC_VER_41] =
285 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
286 [RTL_GIGA_MAC_VER_42] =
287 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
289 [RTL_GIGA_MAC_VER_43] =
290 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
292 [RTL_GIGA_MAC_VER_44] =
293 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
295 [RTL_GIGA_MAC_VER_45] =
296 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
298 [RTL_GIGA_MAC_VER_46] =
299 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
301 [RTL_GIGA_MAC_VER_47] =
302 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
304 [RTL_GIGA_MAC_VER_48] =
305 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
307 [RTL_GIGA_MAC_VER_49] =
308 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
310 [RTL_GIGA_MAC_VER_50] =
311 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
313 [RTL_GIGA_MAC_VER_51] =
314 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
325 static const struct pci_device_id rtl8169_pci_tbl[] = {
326 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
332 { PCI_VENDOR_ID_DLINK, 0x4300,
333 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
334 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
336 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
337 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
338 { PCI_VENDOR_ID_LINKSYS, 0x1032,
339 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
341 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
345 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
347 static int use_dac = -1;
353 MAC0 = 0, /* Ethernet hardware address. */
355 MAR0 = 8, /* Multicast filter. */
356 CounterAddrLow = 0x10,
357 CounterAddrHigh = 0x14,
358 TxDescStartAddrLow = 0x20,
359 TxDescStartAddrHigh = 0x24,
360 TxHDescStartAddrLow = 0x28,
361 TxHDescStartAddrHigh = 0x2c,
370 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
371 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
374 #define RX128_INT_EN (1 << 15) /* 8111c and later */
375 #define RX_MULTI_EN (1 << 14) /* 8111c only */
376 #define RXCFG_FIFO_SHIFT 13
377 /* No threshold before first PCI xfer */
378 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
379 #define RX_EARLY_OFF (1 << 11)
380 #define RXCFG_DMA_SHIFT 8
381 /* Unlimited maximum PCI burst. */
382 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
389 #define PME_SIGNAL (1 << 5) /* 8168c and later */
401 #define RTL_COALESCE_MASK 0x0f
402 #define RTL_COALESCE_SHIFT 4
403 #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
404 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
406 RxDescAddrLow = 0xe4,
407 RxDescAddrHigh = 0xe8,
408 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
410 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
412 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
414 #define TxPacketMax (8064 >> 7)
415 #define EarlySize 0x27
418 FuncEventMask = 0xf4,
419 FuncPresetState = 0xf8,
424 FuncForceEvent = 0xfc,
427 enum rtl8110_registers {
433 enum rtl8168_8101_registers {
436 #define CSIAR_FLAG 0x80000000
437 #define CSIAR_WRITE_CMD 0x80000000
438 #define CSIAR_BYTE_ENABLE 0x0f
439 #define CSIAR_BYTE_ENABLE_SHIFT 12
440 #define CSIAR_ADDR_MASK 0x0fff
441 #define CSIAR_FUNC_CARD 0x00000000
442 #define CSIAR_FUNC_SDIO 0x00010000
443 #define CSIAR_FUNC_NIC 0x00020000
444 #define CSIAR_FUNC_NIC2 0x00010000
447 #define EPHYAR_FLAG 0x80000000
448 #define EPHYAR_WRITE_CMD 0x80000000
449 #define EPHYAR_REG_MASK 0x1f
450 #define EPHYAR_REG_SHIFT 16
451 #define EPHYAR_DATA_MASK 0xffff
453 #define PFM_EN (1 << 6)
454 #define TX_10M_PS_EN (1 << 7)
456 #define FIX_NAK_1 (1 << 4)
457 #define FIX_NAK_2 (1 << 3)
460 #define NOW_IS_OOB (1 << 7)
461 #define TX_EMPTY (1 << 5)
462 #define RX_EMPTY (1 << 4)
463 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
464 #define EN_NDP (1 << 3)
465 #define EN_OOB_RESET (1 << 2)
466 #define LINK_LIST_RDY (1 << 1)
468 #define EFUSEAR_FLAG 0x80000000
469 #define EFUSEAR_WRITE_CMD 0x80000000
470 #define EFUSEAR_READ_CMD 0x00000000
471 #define EFUSEAR_REG_MASK 0x03ff
472 #define EFUSEAR_REG_SHIFT 8
473 #define EFUSEAR_DATA_MASK 0xff
475 #define PFM_D3COLD_EN (1 << 6)
478 enum rtl8168_registers {
483 #define ERIAR_FLAG 0x80000000
484 #define ERIAR_WRITE_CMD 0x80000000
485 #define ERIAR_READ_CMD 0x00000000
486 #define ERIAR_ADDR_BYTE_ALIGN 4
487 #define ERIAR_TYPE_SHIFT 16
488 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
489 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
490 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
491 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
492 #define ERIAR_MASK_SHIFT 12
493 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
494 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
495 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
496 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
497 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
498 EPHY_RXER_NUM = 0x7c,
499 OCPDR = 0xb0, /* OCP GPHY access */
500 #define OCPDR_WRITE_CMD 0x80000000
501 #define OCPDR_READ_CMD 0x00000000
502 #define OCPDR_REG_MASK 0x7f
503 #define OCPDR_GPHY_REG_SHIFT 16
504 #define OCPDR_DATA_MASK 0xffff
506 #define OCPAR_FLAG 0x80000000
507 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
508 #define OCPAR_GPHY_READ_CMD 0x0000f060
510 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
511 MISC = 0xf0, /* 8168e only. */
512 #define TXPLA_RST (1 << 29)
513 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
514 #define PWM_EN (1 << 22)
515 #define RXDV_GATED_EN (1 << 19)
516 #define EARLY_TALLY_EN (1 << 16)
519 enum rtl_register_content {
520 /* InterruptStatusBits */
524 TxDescUnavail = 0x0080,
548 /* TXPoll register p.5 */
549 HPQ = 0x80, /* Poll cmd on the high prio queue */
550 NPQ = 0x40, /* Poll cmd on the low prio queue */
551 FSWInt = 0x01, /* Forced software interrupt */
555 Cfg9346_Unlock = 0xc0,
560 AcceptBroadcast = 0x08,
561 AcceptMulticast = 0x04,
563 AcceptAllPhys = 0x01,
564 #define RX_CONFIG_ACCEPT_MASK 0x3f
567 TxInterFrameGapShift = 24,
568 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
570 /* Config1 register p.24 */
573 Speed_down = (1 << 4),
577 PMEnable = (1 << 0), /* Power Management Enable */
579 /* Config2 register p. 25 */
580 ClkReqEn = (1 << 7), /* Clock Request Enable */
581 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
582 PCI_Clock_66MHz = 0x01,
583 PCI_Clock_33MHz = 0x00,
585 /* Config3 register p.25 */
586 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
587 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
588 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
589 Rdy_to_L23 = (1 << 1), /* L23 Enable */
590 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
592 /* Config4 register */
593 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
595 /* Config5 register p.27 */
596 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
597 MWF = (1 << 5), /* Accept Multicast wakeup frame */
598 UWF = (1 << 4), /* Accept Unicast wakeup frame */
600 LanWake = (1 << 1), /* LanWake enable/disable */
601 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
602 ASPM_en = (1 << 0), /* ASPM enable */
605 TBIReset = 0x80000000,
606 TBILoopback = 0x40000000,
607 TBINwEnable = 0x20000000,
608 TBINwRestart = 0x10000000,
609 TBILinkOk = 0x02000000,
610 TBINwComplete = 0x01000000,
613 EnableBist = (1 << 15), // 8168 8101
614 Mac_dbgo_oe = (1 << 14), // 8168 8101
615 Normal_mode = (1 << 13), // unused
616 Force_half_dup = (1 << 12), // 8168 8101
617 Force_rxflow_en = (1 << 11), // 8168 8101
618 Force_txflow_en = (1 << 10), // 8168 8101
619 Cxpl_dbg_sel = (1 << 9), // 8168 8101
620 ASF = (1 << 8), // 8168 8101
621 PktCntrDisable = (1 << 7), // 8168 8101
622 Mac_dbgo_sel = 0x001c, // 8168
627 INTT_0 = 0x0000, // 8168
628 INTT_1 = 0x0001, // 8168
629 INTT_2 = 0x0002, // 8168
630 INTT_3 = 0x0003, // 8168
632 /* rtl8169_PHYstatus */
643 TBILinkOK = 0x02000000,
645 /* ResetCounterCommand */
648 /* DumpCounterCommand */
651 /* magic enable v2 */
652 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
656 /* First doubleword. */
657 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
658 RingEnd = (1 << 30), /* End of descriptor ring */
659 FirstFrag = (1 << 29), /* First segment of a packet */
660 LastFrag = (1 << 28), /* Final segment of a packet */
664 enum rtl_tx_desc_bit {
665 /* First doubleword. */
666 TD_LSO = (1 << 27), /* Large Send Offload */
667 #define TD_MSS_MAX 0x07ffu /* MSS value */
669 /* Second doubleword. */
670 TxVlanTag = (1 << 17), /* Add VLAN tag */
673 /* 8169, 8168b and 810x except 8102e. */
674 enum rtl_tx_desc_bit_0 {
675 /* First doubleword. */
676 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
677 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
678 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
679 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
682 /* 8102e, 8168c and beyond. */
683 enum rtl_tx_desc_bit_1 {
684 /* First doubleword. */
685 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
686 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
687 #define GTTCPHO_SHIFT 18
688 #define GTTCPHO_MAX 0x7fU
690 /* Second doubleword. */
691 #define TCPHO_SHIFT 18
692 #define TCPHO_MAX 0x3ffU
693 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
694 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
695 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
696 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
697 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
700 enum rtl_rx_desc_bit {
702 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
703 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
705 #define RxProtoUDP (PID1)
706 #define RxProtoTCP (PID0)
707 #define RxProtoIP (PID1 | PID0)
708 #define RxProtoMask RxProtoIP
710 IPFail = (1 << 16), /* IP checksum failed */
711 UDPFail = (1 << 15), /* UDP/IP checksum failed */
712 TCPFail = (1 << 14), /* TCP/IP checksum failed */
713 RxVlanTag = (1 << 16), /* VLAN tag available */
716 #define RsvdMask 0x3fffc000
733 u8 __pad[sizeof(void *) - sizeof(u32)];
736 struct rtl8169_counters {
743 __le32 tx_one_collision;
744 __le32 tx_multi_collision;
752 struct rtl8169_tc_offsets {
755 __le32 tx_multi_collision;
760 RTL_FLAG_TASK_ENABLED,
761 RTL_FLAG_TASK_SLOW_PENDING,
762 RTL_FLAG_TASK_RESET_PENDING,
763 RTL_FLAG_TASK_PHY_PENDING,
767 struct rtl8169_stats {
770 struct u64_stats_sync syncp;
773 struct rtl8169_private {
774 void __iomem *mmio_addr; /* memory map physical address */
775 struct pci_dev *pci_dev;
776 struct net_device *dev;
777 struct napi_struct napi;
781 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
782 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
784 struct rtl8169_stats rx_stats;
785 struct rtl8169_stats tx_stats;
786 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
787 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
788 dma_addr_t TxPhyAddr;
789 dma_addr_t RxPhyAddr;
790 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
791 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
792 struct timer_list timer;
796 const struct rtl_coalesce_info *coalesce_info;
799 void (*write)(struct rtl8169_private *, int, int);
800 int (*read)(struct rtl8169_private *, int);
803 struct pll_power_ops {
804 void (*down)(struct rtl8169_private *);
805 void (*up)(struct rtl8169_private *);
809 void (*enable)(struct rtl8169_private *);
810 void (*disable)(struct rtl8169_private *);
814 void (*write)(struct rtl8169_private *, int, int);
815 u32 (*read)(struct rtl8169_private *, int);
818 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
819 int (*get_link_ksettings)(struct net_device *,
820 struct ethtool_link_ksettings *);
821 void (*phy_reset_enable)(struct rtl8169_private *tp);
822 void (*hw_start)(struct net_device *);
823 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
824 unsigned int (*link_ok)(struct rtl8169_private *tp);
825 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
826 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
829 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
831 struct work_struct work;
834 struct mii_if_info mii;
835 dma_addr_t counters_phys_addr;
836 struct rtl8169_counters *counters;
837 struct rtl8169_tc_offsets tc_offset;
842 const struct firmware *fw;
844 #define RTL_VER_SIZE 32
846 char version[RTL_VER_SIZE];
848 struct rtl_fw_phy_action {
853 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
858 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
859 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
860 module_param(use_dac, int, 0);
861 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
862 module_param_named(debug, debug.msg_enable, int, 0);
863 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
864 MODULE_LICENSE("GPL");
865 MODULE_VERSION(RTL8169_VERSION);
866 MODULE_FIRMWARE(FIRMWARE_8168D_1);
867 MODULE_FIRMWARE(FIRMWARE_8168D_2);
868 MODULE_FIRMWARE(FIRMWARE_8168E_1);
869 MODULE_FIRMWARE(FIRMWARE_8168E_2);
870 MODULE_FIRMWARE(FIRMWARE_8168E_3);
871 MODULE_FIRMWARE(FIRMWARE_8105E_1);
872 MODULE_FIRMWARE(FIRMWARE_8168F_1);
873 MODULE_FIRMWARE(FIRMWARE_8168F_2);
874 MODULE_FIRMWARE(FIRMWARE_8402_1);
875 MODULE_FIRMWARE(FIRMWARE_8411_1);
876 MODULE_FIRMWARE(FIRMWARE_8411_2);
877 MODULE_FIRMWARE(FIRMWARE_8106E_1);
878 MODULE_FIRMWARE(FIRMWARE_8106E_2);
879 MODULE_FIRMWARE(FIRMWARE_8168G_2);
880 MODULE_FIRMWARE(FIRMWARE_8168G_3);
881 MODULE_FIRMWARE(FIRMWARE_8168H_1);
882 MODULE_FIRMWARE(FIRMWARE_8168H_2);
883 MODULE_FIRMWARE(FIRMWARE_8107E_1);
884 MODULE_FIRMWARE(FIRMWARE_8107E_2);
886 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
888 return &tp->pci_dev->dev;
891 static void rtl_lock_work(struct rtl8169_private *tp)
893 mutex_lock(&tp->wk.mutex);
896 static void rtl_unlock_work(struct rtl8169_private *tp)
898 mutex_unlock(&tp->wk.mutex);
901 static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
903 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
904 PCI_EXP_DEVCTL_READRQ, force);
908 bool (*check)(struct rtl8169_private *);
912 static void rtl_udelay(unsigned int d)
917 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
918 void (*delay)(unsigned int), unsigned int d, int n,
923 for (i = 0; i < n; i++) {
925 if (c->check(tp) == high)
928 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
929 c->msg, !high, n, d);
933 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
934 const struct rtl_cond *c,
935 unsigned int d, int n)
937 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
940 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
941 const struct rtl_cond *c,
942 unsigned int d, int n)
944 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
947 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
948 const struct rtl_cond *c,
949 unsigned int d, int n)
951 return rtl_loop_wait(tp, c, msleep, d, n, true);
954 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
955 const struct rtl_cond *c,
956 unsigned int d, int n)
958 return rtl_loop_wait(tp, c, msleep, d, n, false);
961 #define DECLARE_RTL_COND(name) \
962 static bool name ## _check(struct rtl8169_private *); \
964 static const struct rtl_cond name = { \
965 .check = name ## _check, \
969 static bool name ## _check(struct rtl8169_private *tp)
971 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
973 if (reg & 0xffff0001) {
974 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
980 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
982 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
985 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
987 if (rtl_ocp_reg_failure(tp, reg))
990 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
992 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
995 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
997 if (rtl_ocp_reg_failure(tp, reg))
1000 RTL_W32(tp, GPHY_OCP, reg << 15);
1002 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1003 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
1006 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1008 if (rtl_ocp_reg_failure(tp, reg))
1011 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
1014 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1016 if (rtl_ocp_reg_failure(tp, reg))
1019 RTL_W32(tp, OCPDR, reg << 15);
1021 return RTL_R32(tp, OCPDR);
1024 #define OCP_STD_PHY_BASE 0xa400
1026 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1029 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1033 if (tp->ocp_base != OCP_STD_PHY_BASE)
1036 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1039 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1041 if (tp->ocp_base != OCP_STD_PHY_BASE)
1044 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1047 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1050 tp->ocp_base = value << 4;
1054 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1057 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1059 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1062 DECLARE_RTL_COND(rtl_phyar_cond)
1064 return RTL_R32(tp, PHYAR) & 0x80000000;
1067 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1069 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1071 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1073 * According to hardware specs a 20us delay is required after write
1074 * complete indication, but before sending next command.
1079 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1083 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
1085 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1086 RTL_R32(tp, PHYAR) & 0xffff : ~0;
1089 * According to hardware specs a 20us delay is required after read
1090 * complete indication, but before sending next command.
1097 DECLARE_RTL_COND(rtl_ocpar_cond)
1099 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
1102 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1104 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1105 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1106 RTL_W32(tp, EPHY_RXER_NUM, 0);
1108 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1111 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1113 r8168dp_1_mdio_access(tp, reg,
1114 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1117 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1119 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1122 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1123 RTL_W32(tp, EPHY_RXER_NUM, 0);
1125 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1126 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
1129 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1131 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
1133 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1136 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
1138 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1141 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1143 r8168dp_2_mdio_start(tp);
1145 r8169_mdio_write(tp, reg, value);
1147 r8168dp_2_mdio_stop(tp);
1150 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1154 r8168dp_2_mdio_start(tp);
1156 value = r8169_mdio_read(tp, reg);
1158 r8168dp_2_mdio_stop(tp);
1163 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1165 tp->mdio_ops.write(tp, location, val);
1168 static int rtl_readphy(struct rtl8169_private *tp, int location)
1170 return tp->mdio_ops.read(tp, location);
1173 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1175 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1178 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1182 val = rtl_readphy(tp, reg_addr);
1183 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1186 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1189 struct rtl8169_private *tp = netdev_priv(dev);
1191 rtl_writephy(tp, location, val);
1194 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1196 struct rtl8169_private *tp = netdev_priv(dev);
1198 return rtl_readphy(tp, location);
1201 DECLARE_RTL_COND(rtl_ephyar_cond)
1203 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1206 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1208 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1209 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1211 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1216 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1218 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1220 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1221 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1224 DECLARE_RTL_COND(rtl_eriar_cond)
1226 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
1229 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1232 BUG_ON((addr & 3) || (mask == 0));
1233 RTL_W32(tp, ERIDR, val);
1234 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1236 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1239 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1241 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1243 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1244 RTL_R32(tp, ERIDR) : ~0;
1247 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1252 val = rtl_eri_read(tp, addr, type);
1253 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1256 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1258 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1259 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1260 RTL_R32(tp, OCPDR) : ~0;
1263 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1265 return rtl_eri_read(tp, reg, ERIAR_OOB);
1268 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1270 switch (tp->mac_version) {
1271 case RTL_GIGA_MAC_VER_27:
1272 case RTL_GIGA_MAC_VER_28:
1273 case RTL_GIGA_MAC_VER_31:
1274 return r8168dp_ocp_read(tp, mask, reg);
1275 case RTL_GIGA_MAC_VER_49:
1276 case RTL_GIGA_MAC_VER_50:
1277 case RTL_GIGA_MAC_VER_51:
1278 return r8168ep_ocp_read(tp, mask, reg);
1285 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1288 RTL_W32(tp, OCPDR, data);
1289 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1290 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1293 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1296 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1300 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1302 switch (tp->mac_version) {
1303 case RTL_GIGA_MAC_VER_27:
1304 case RTL_GIGA_MAC_VER_28:
1305 case RTL_GIGA_MAC_VER_31:
1306 r8168dp_ocp_write(tp, mask, reg, data);
1308 case RTL_GIGA_MAC_VER_49:
1309 case RTL_GIGA_MAC_VER_50:
1310 case RTL_GIGA_MAC_VER_51:
1311 r8168ep_ocp_write(tp, mask, reg, data);
1319 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1321 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1323 ocp_write(tp, 0x1, 0x30, 0x00000001);
1326 #define OOB_CMD_RESET 0x00
1327 #define OOB_CMD_DRIVER_START 0x05
1328 #define OOB_CMD_DRIVER_STOP 0x06
1330 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1332 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1335 DECLARE_RTL_COND(rtl_ocp_read_cond)
1339 reg = rtl8168_get_ocp_reg(tp);
1341 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1344 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1346 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1349 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1351 return RTL_R8(tp, IBISR0) & 0x20;
1354 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1356 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1357 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1358 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1359 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1362 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1364 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1365 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1368 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1370 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1371 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1372 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1375 static void rtl8168_driver_start(struct rtl8169_private *tp)
1377 switch (tp->mac_version) {
1378 case RTL_GIGA_MAC_VER_27:
1379 case RTL_GIGA_MAC_VER_28:
1380 case RTL_GIGA_MAC_VER_31:
1381 rtl8168dp_driver_start(tp);
1383 case RTL_GIGA_MAC_VER_49:
1384 case RTL_GIGA_MAC_VER_50:
1385 case RTL_GIGA_MAC_VER_51:
1386 rtl8168ep_driver_start(tp);
1394 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1396 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1397 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1400 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1402 rtl8168ep_stop_cmac(tp);
1403 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1404 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1405 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1408 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1410 switch (tp->mac_version) {
1411 case RTL_GIGA_MAC_VER_27:
1412 case RTL_GIGA_MAC_VER_28:
1413 case RTL_GIGA_MAC_VER_31:
1414 rtl8168dp_driver_stop(tp);
1416 case RTL_GIGA_MAC_VER_49:
1417 case RTL_GIGA_MAC_VER_50:
1418 case RTL_GIGA_MAC_VER_51:
1419 rtl8168ep_driver_stop(tp);
1427 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1429 u16 reg = rtl8168_get_ocp_reg(tp);
1431 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
1434 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1436 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
1439 static bool r8168_check_dash(struct rtl8169_private *tp)
1441 switch (tp->mac_version) {
1442 case RTL_GIGA_MAC_VER_27:
1443 case RTL_GIGA_MAC_VER_28:
1444 case RTL_GIGA_MAC_VER_31:
1445 return r8168dp_check_dash(tp);
1446 case RTL_GIGA_MAC_VER_49:
1447 case RTL_GIGA_MAC_VER_50:
1448 case RTL_GIGA_MAC_VER_51:
1449 return r8168ep_check_dash(tp);
1461 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1462 const struct exgmac_reg *r, int len)
1465 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1470 DECLARE_RTL_COND(rtl_efusear_cond)
1472 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1475 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1477 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1479 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1480 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1483 static u16 rtl_get_events(struct rtl8169_private *tp)
1485 return RTL_R16(tp, IntrStatus);
1488 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1490 RTL_W16(tp, IntrStatus, bits);
1494 static void rtl_irq_disable(struct rtl8169_private *tp)
1496 RTL_W16(tp, IntrMask, 0);
1500 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1502 RTL_W16(tp, IntrMask, bits);
1505 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1506 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1507 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1509 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1511 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1514 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1516 rtl_irq_disable(tp);
1517 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1518 RTL_R8(tp, ChipCmd);
1521 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1523 return RTL_R32(tp, TBICSR) & TBIReset;
1526 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1528 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1531 static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
1533 return RTL_R32(tp, TBICSR) & TBILinkOk;
1536 static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
1538 return RTL_R8(tp, PHYstatus) & LinkStatus;
1541 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1543 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
1546 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1550 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1551 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1554 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1556 struct net_device *dev = tp->dev;
1558 if (!netif_running(dev))
1561 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1562 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1563 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
1564 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1566 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1568 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
1569 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1571 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1574 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1576 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1579 /* Reset packet filter */
1580 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1582 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1584 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1585 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1586 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
1587 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1589 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1592 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1594 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1597 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1598 if (RTL_R8(tp, PHYstatus) & _10bps) {
1599 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1601 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1604 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1610 static void rtl8169_check_link_status(struct net_device *dev,
1611 struct rtl8169_private *tp)
1613 struct device *d = tp_to_dev(tp);
1615 if (tp->link_ok(tp)) {
1616 rtl_link_chg_patch(tp);
1617 /* This is to cancel a scheduled suspend if there's one. */
1618 pm_request_resume(d);
1619 netif_carrier_on(dev);
1620 if (net_ratelimit())
1621 netif_info(tp, ifup, dev, "link up\n");
1623 netif_carrier_off(dev);
1624 netif_info(tp, ifdown, dev, "link down\n");
1629 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1631 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1636 options = RTL_R8(tp, Config1);
1637 if (!(options & PMEnable))
1640 options = RTL_R8(tp, Config3);
1641 if (options & LinkUp)
1642 wolopts |= WAKE_PHY;
1643 switch (tp->mac_version) {
1644 case RTL_GIGA_MAC_VER_34:
1645 case RTL_GIGA_MAC_VER_35:
1646 case RTL_GIGA_MAC_VER_36:
1647 case RTL_GIGA_MAC_VER_37:
1648 case RTL_GIGA_MAC_VER_38:
1649 case RTL_GIGA_MAC_VER_40:
1650 case RTL_GIGA_MAC_VER_41:
1651 case RTL_GIGA_MAC_VER_42:
1652 case RTL_GIGA_MAC_VER_43:
1653 case RTL_GIGA_MAC_VER_44:
1654 case RTL_GIGA_MAC_VER_45:
1655 case RTL_GIGA_MAC_VER_46:
1656 case RTL_GIGA_MAC_VER_47:
1657 case RTL_GIGA_MAC_VER_48:
1658 case RTL_GIGA_MAC_VER_49:
1659 case RTL_GIGA_MAC_VER_50:
1660 case RTL_GIGA_MAC_VER_51:
1661 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1662 wolopts |= WAKE_MAGIC;
1665 if (options & MagicPacket)
1666 wolopts |= WAKE_MAGIC;
1670 options = RTL_R8(tp, Config5);
1672 wolopts |= WAKE_UCAST;
1674 wolopts |= WAKE_BCAST;
1676 wolopts |= WAKE_MCAST;
1681 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1683 struct rtl8169_private *tp = netdev_priv(dev);
1684 struct device *d = tp_to_dev(tp);
1686 pm_runtime_get_noresume(d);
1690 wol->supported = WAKE_ANY;
1691 if (pm_runtime_active(d))
1692 wol->wolopts = __rtl8169_get_wol(tp);
1694 wol->wolopts = tp->saved_wolopts;
1696 rtl_unlock_work(tp);
1698 pm_runtime_put_noidle(d);
1701 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1703 unsigned int i, tmp;
1704 static const struct {
1709 { WAKE_PHY, Config3, LinkUp },
1710 { WAKE_UCAST, Config5, UWF },
1711 { WAKE_BCAST, Config5, BWF },
1712 { WAKE_MCAST, Config5, MWF },
1713 { WAKE_ANY, Config5, LanWake },
1714 { WAKE_MAGIC, Config3, MagicPacket }
1718 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
1720 switch (tp->mac_version) {
1721 case RTL_GIGA_MAC_VER_34:
1722 case RTL_GIGA_MAC_VER_35:
1723 case RTL_GIGA_MAC_VER_36:
1724 case RTL_GIGA_MAC_VER_37:
1725 case RTL_GIGA_MAC_VER_38:
1726 case RTL_GIGA_MAC_VER_40:
1727 case RTL_GIGA_MAC_VER_41:
1728 case RTL_GIGA_MAC_VER_42:
1729 case RTL_GIGA_MAC_VER_43:
1730 case RTL_GIGA_MAC_VER_44:
1731 case RTL_GIGA_MAC_VER_45:
1732 case RTL_GIGA_MAC_VER_46:
1733 case RTL_GIGA_MAC_VER_47:
1734 case RTL_GIGA_MAC_VER_48:
1735 case RTL_GIGA_MAC_VER_49:
1736 case RTL_GIGA_MAC_VER_50:
1737 case RTL_GIGA_MAC_VER_51:
1738 tmp = ARRAY_SIZE(cfg) - 1;
1739 if (wolopts & WAKE_MAGIC)
1755 tmp = ARRAY_SIZE(cfg);
1759 for (i = 0; i < tmp; i++) {
1760 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1761 if (wolopts & cfg[i].opt)
1762 options |= cfg[i].mask;
1763 RTL_W8(tp, cfg[i].reg, options);
1766 switch (tp->mac_version) {
1767 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1768 options = RTL_R8(tp, Config1) & ~PMEnable;
1770 options |= PMEnable;
1771 RTL_W8(tp, Config1, options);
1774 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
1776 options |= PME_SIGNAL;
1777 RTL_W8(tp, Config2, options);
1781 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
1784 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1786 struct rtl8169_private *tp = netdev_priv(dev);
1787 struct device *d = tp_to_dev(tp);
1789 pm_runtime_get_noresume(d);
1793 if (pm_runtime_active(d))
1794 __rtl8169_set_wol(tp, wol->wolopts);
1796 tp->saved_wolopts = wol->wolopts;
1798 rtl_unlock_work(tp);
1800 device_set_wakeup_enable(d, wol->wolopts);
1802 pm_runtime_put_noidle(d);
1807 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1809 return rtl_chip_infos[tp->mac_version].fw_name;
1812 static void rtl8169_get_drvinfo(struct net_device *dev,
1813 struct ethtool_drvinfo *info)
1815 struct rtl8169_private *tp = netdev_priv(dev);
1816 struct rtl_fw *rtl_fw = tp->rtl_fw;
1818 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1819 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1820 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1821 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1822 if (!IS_ERR_OR_NULL(rtl_fw))
1823 strlcpy(info->fw_version, rtl_fw->version,
1824 sizeof(info->fw_version));
1827 static int rtl8169_get_regs_len(struct net_device *dev)
1829 return R8169_REGS_SIZE;
1832 static int rtl8169_set_speed_tbi(struct net_device *dev,
1833 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1835 struct rtl8169_private *tp = netdev_priv(dev);
1839 reg = RTL_R32(tp, TBICSR);
1840 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1841 (duplex == DUPLEX_FULL)) {
1842 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1843 } else if (autoneg == AUTONEG_ENABLE)
1844 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
1846 netif_warn(tp, link, dev,
1847 "incorrect speed setting refused in TBI mode\n");
1854 static int rtl8169_set_speed_xmii(struct net_device *dev,
1855 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1857 struct rtl8169_private *tp = netdev_priv(dev);
1858 int giga_ctrl, bmcr;
1861 rtl_writephy(tp, 0x1f, 0x0000);
1863 if (autoneg == AUTONEG_ENABLE) {
1866 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1867 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1868 ADVERTISE_100HALF | ADVERTISE_100FULL);
1870 if (adv & ADVERTISED_10baseT_Half)
1871 auto_nego |= ADVERTISE_10HALF;
1872 if (adv & ADVERTISED_10baseT_Full)
1873 auto_nego |= ADVERTISE_10FULL;
1874 if (adv & ADVERTISED_100baseT_Half)
1875 auto_nego |= ADVERTISE_100HALF;
1876 if (adv & ADVERTISED_100baseT_Full)
1877 auto_nego |= ADVERTISE_100FULL;
1879 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1881 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1882 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1884 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1885 if (tp->mii.supports_gmii) {
1886 if (adv & ADVERTISED_1000baseT_Half)
1887 giga_ctrl |= ADVERTISE_1000HALF;
1888 if (adv & ADVERTISED_1000baseT_Full)
1889 giga_ctrl |= ADVERTISE_1000FULL;
1890 } else if (adv & (ADVERTISED_1000baseT_Half |
1891 ADVERTISED_1000baseT_Full)) {
1892 netif_info(tp, link, dev,
1893 "PHY does not support 1000Mbps\n");
1897 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1899 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1900 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1902 if (speed == SPEED_10)
1904 else if (speed == SPEED_100)
1905 bmcr = BMCR_SPEED100;
1909 if (duplex == DUPLEX_FULL)
1910 bmcr |= BMCR_FULLDPLX;
1913 rtl_writephy(tp, MII_BMCR, bmcr);
1915 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1916 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1917 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1918 rtl_writephy(tp, 0x17, 0x2138);
1919 rtl_writephy(tp, 0x0e, 0x0260);
1921 rtl_writephy(tp, 0x17, 0x2108);
1922 rtl_writephy(tp, 0x0e, 0x0000);
1931 static int rtl8169_set_speed(struct net_device *dev,
1932 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1934 struct rtl8169_private *tp = netdev_priv(dev);
1937 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1941 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1942 (advertising & ADVERTISED_1000baseT_Full) &&
1943 !pci_is_pcie(tp->pci_dev)) {
1944 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1950 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1951 netdev_features_t features)
1953 struct rtl8169_private *tp = netdev_priv(dev);
1955 if (dev->mtu > TD_MSS_MAX)
1956 features &= ~NETIF_F_ALL_TSO;
1958 if (dev->mtu > JUMBO_1K &&
1959 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1960 features &= ~NETIF_F_IP_CSUM;
1965 static void __rtl8169_set_features(struct net_device *dev,
1966 netdev_features_t features)
1968 struct rtl8169_private *tp = netdev_priv(dev);
1971 rx_config = RTL_R32(tp, RxConfig);
1972 if (features & NETIF_F_RXALL)
1973 rx_config |= (AcceptErr | AcceptRunt);
1975 rx_config &= ~(AcceptErr | AcceptRunt);
1977 RTL_W32(tp, RxConfig, rx_config);
1979 if (features & NETIF_F_RXCSUM)
1980 tp->cp_cmd |= RxChkSum;
1982 tp->cp_cmd &= ~RxChkSum;
1984 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1985 tp->cp_cmd |= RxVlan;
1987 tp->cp_cmd &= ~RxVlan;
1989 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) & ~(RxVlan | RxChkSum);
1991 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1992 RTL_R16(tp, CPlusCmd);
1995 static int rtl8169_set_features(struct net_device *dev,
1996 netdev_features_t features)
1998 struct rtl8169_private *tp = netdev_priv(dev);
2000 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2003 if (features ^ dev->features)
2004 __rtl8169_set_features(dev, features);
2005 rtl_unlock_work(tp);
2011 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
2013 return (skb_vlan_tag_present(skb)) ?
2014 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
2017 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
2019 u32 opts2 = le32_to_cpu(desc->opts2);
2021 if (opts2 & RxVlanTag)
2022 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
2025 static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2026 struct ethtool_link_ksettings *cmd)
2028 struct rtl8169_private *tp = netdev_priv(dev);
2030 u32 supported, advertising;
2033 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
2034 cmd->base.port = PORT_FIBRE;
2036 status = RTL_R32(tp, TBICSR);
2037 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2038 cmd->base.autoneg = !!(status & TBINwEnable);
2040 cmd->base.speed = SPEED_1000;
2041 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2043 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2045 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2051 static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2052 struct ethtool_link_ksettings *cmd)
2054 struct rtl8169_private *tp = netdev_priv(dev);
2056 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2061 static int rtl8169_get_link_ksettings(struct net_device *dev,
2062 struct ethtool_link_ksettings *cmd)
2064 struct rtl8169_private *tp = netdev_priv(dev);
2068 rc = tp->get_link_ksettings(dev, cmd);
2069 rtl_unlock_work(tp);
2074 static int rtl8169_set_link_ksettings(struct net_device *dev,
2075 const struct ethtool_link_ksettings *cmd)
2077 struct rtl8169_private *tp = netdev_priv(dev);
2081 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2082 cmd->link_modes.advertising))
2085 del_timer_sync(&tp->timer);
2088 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2089 cmd->base.duplex, advertising);
2090 rtl_unlock_work(tp);
2095 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2098 struct rtl8169_private *tp = netdev_priv(dev);
2099 u32 __iomem *data = tp->mmio_addr;
2104 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2105 memcpy_fromio(dw++, data++, 4);
2106 rtl_unlock_work(tp);
2109 static u32 rtl8169_get_msglevel(struct net_device *dev)
2111 struct rtl8169_private *tp = netdev_priv(dev);
2113 return tp->msg_enable;
2116 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2118 struct rtl8169_private *tp = netdev_priv(dev);
2120 tp->msg_enable = value;
2123 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2130 "tx_single_collisions",
2131 "tx_multi_collisions",
2139 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
2143 return ARRAY_SIZE(rtl8169_gstrings);
2149 DECLARE_RTL_COND(rtl_counters_cond)
2151 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
2154 static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
2156 struct rtl8169_private *tp = netdev_priv(dev);
2157 dma_addr_t paddr = tp->counters_phys_addr;
2160 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2161 RTL_R32(tp, CounterAddrHigh);
2162 cmd = (u64)paddr & DMA_BIT_MASK(32);
2163 RTL_W32(tp, CounterAddrLow, cmd);
2164 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
2166 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
2169 static bool rtl8169_reset_counters(struct net_device *dev)
2171 struct rtl8169_private *tp = netdev_priv(dev);
2174 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2177 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2180 return rtl8169_do_counters(dev, CounterReset);
2183 static bool rtl8169_update_counters(struct net_device *dev)
2185 struct rtl8169_private *tp = netdev_priv(dev);
2188 * Some chips are unable to dump tally counters when the receiver
2191 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
2194 return rtl8169_do_counters(dev, CounterDump);
2197 static bool rtl8169_init_counter_offsets(struct net_device *dev)
2199 struct rtl8169_private *tp = netdev_priv(dev);
2200 struct rtl8169_counters *counters = tp->counters;
2204 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2205 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2206 * reset by a power cycle, while the counter values collected by the
2207 * driver are reset at every driver unload/load cycle.
2209 * To make sure the HW values returned by @get_stats64 match the SW
2210 * values, we collect the initial values at first open(*) and use them
2211 * as offsets to normalize the values returned by @get_stats64.
2213 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2214 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2215 * set at open time by rtl_hw_start.
2218 if (tp->tc_offset.inited)
2221 /* If both, reset and update fail, propagate to caller. */
2222 if (rtl8169_reset_counters(dev))
2225 if (rtl8169_update_counters(dev))
2228 tp->tc_offset.tx_errors = counters->tx_errors;
2229 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2230 tp->tc_offset.tx_aborted = counters->tx_aborted;
2231 tp->tc_offset.inited = true;
2236 static void rtl8169_get_ethtool_stats(struct net_device *dev,
2237 struct ethtool_stats *stats, u64 *data)
2239 struct rtl8169_private *tp = netdev_priv(dev);
2240 struct device *d = tp_to_dev(tp);
2241 struct rtl8169_counters *counters = tp->counters;
2245 pm_runtime_get_noresume(d);
2247 if (pm_runtime_active(d))
2248 rtl8169_update_counters(dev);
2250 pm_runtime_put_noidle(d);
2252 data[0] = le64_to_cpu(counters->tx_packets);
2253 data[1] = le64_to_cpu(counters->rx_packets);
2254 data[2] = le64_to_cpu(counters->tx_errors);
2255 data[3] = le32_to_cpu(counters->rx_errors);
2256 data[4] = le16_to_cpu(counters->rx_missed);
2257 data[5] = le16_to_cpu(counters->align_errors);
2258 data[6] = le32_to_cpu(counters->tx_one_collision);
2259 data[7] = le32_to_cpu(counters->tx_multi_collision);
2260 data[8] = le64_to_cpu(counters->rx_unicast);
2261 data[9] = le64_to_cpu(counters->rx_broadcast);
2262 data[10] = le32_to_cpu(counters->rx_multicast);
2263 data[11] = le16_to_cpu(counters->tx_aborted);
2264 data[12] = le16_to_cpu(counters->tx_underun);
2267 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2271 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2276 static int rtl8169_nway_reset(struct net_device *dev)
2278 struct rtl8169_private *tp = netdev_priv(dev);
2280 return mii_nway_restart(&tp->mii);
2284 * Interrupt coalescing
2286 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2287 * > 8169, 8168 and 810x line of chipsets
2289 * 8169, 8168, and 8136(810x) serial chipsets support it.
2291 * > 2 - the Tx timer unit at gigabit speed
2293 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2294 * (0xe0) bit 1 and bit 0.
2297 * bit[1:0] \ speed 1000M 100M 10M
2298 * 0 0 320ns 2.56us 40.96us
2299 * 0 1 2.56us 20.48us 327.7us
2300 * 1 0 5.12us 40.96us 655.4us
2301 * 1 1 10.24us 81.92us 1.31ms
2304 * bit[1:0] \ speed 1000M 100M 10M
2305 * 0 0 5us 2.56us 40.96us
2306 * 0 1 40us 20.48us 327.7us
2307 * 1 0 80us 40.96us 655.4us
2308 * 1 1 160us 81.92us 1.31ms
2311 /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2312 struct rtl_coalesce_scale {
2317 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
2318 struct rtl_coalesce_info {
2320 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2323 /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2324 #define rxtx_x1822(r, t) { \
2327 {{(r)*8*2, (t)*8*2}}, \
2328 {{(r)*8*2*2, (t)*8*2*2}}, \
2330 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2331 /* speed delays: rx00 tx00 */
2332 { SPEED_10, rxtx_x1822(40960, 40960) },
2333 { SPEED_100, rxtx_x1822( 2560, 2560) },
2334 { SPEED_1000, rxtx_x1822( 320, 320) },
2338 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2339 /* speed delays: rx00 tx00 */
2340 { SPEED_10, rxtx_x1822(40960, 40960) },
2341 { SPEED_100, rxtx_x1822( 2560, 2560) },
2342 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2347 /* get rx/tx scale vector corresponding to current speed */
2348 static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2350 struct rtl8169_private *tp = netdev_priv(dev);
2351 struct ethtool_link_ksettings ecmd;
2352 const struct rtl_coalesce_info *ci;
2355 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2359 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2360 if (ecmd.base.speed == ci->speed) {
2365 return ERR_PTR(-ELNRNG);
2368 static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2370 struct rtl8169_private *tp = netdev_priv(dev);
2371 const struct rtl_coalesce_info *ci;
2372 const struct rtl_coalesce_scale *scale;
2376 } coal_settings [] = {
2377 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2378 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2379 }, *p = coal_settings;
2383 memset(ec, 0, sizeof(*ec));
2385 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2386 ci = rtl_coalesce_info(dev);
2390 scale = &ci->scalev[RTL_R16(tp, CPlusCmd) & 3];
2392 /* read IntrMitigate and adjust according to scale */
2393 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2394 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2395 w >>= RTL_COALESCE_SHIFT;
2396 *p->usecs = w & RTL_COALESCE_MASK;
2399 for (i = 0; i < 2; i++) {
2400 p = coal_settings + i;
2401 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2404 * ethtool_coalesce says it is illegal to set both usecs and
2407 if (!*p->usecs && !*p->max_frames)
2414 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2415 static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2416 struct net_device *dev, u32 nsec, u16 *cp01)
2418 const struct rtl_coalesce_info *ci;
2421 ci = rtl_coalesce_info(dev);
2423 return ERR_CAST(ci);
2425 for (i = 0; i < 4; i++) {
2426 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2427 ci->scalev[i].nsecs[1]);
2428 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2430 return &ci->scalev[i];
2434 return ERR_PTR(-EINVAL);
2437 static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2439 struct rtl8169_private *tp = netdev_priv(dev);
2440 const struct rtl_coalesce_scale *scale;
2444 } coal_settings [] = {
2445 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2446 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2447 }, *p = coal_settings;
2451 scale = rtl_coalesce_choose_scale(dev,
2452 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2454 return PTR_ERR(scale);
2456 for (i = 0; i < 2; i++, p++) {
2460 * accept max_frames=1 we returned in rtl_get_coalesce.
2461 * accept it not only when usecs=0 because of e.g. the following scenario:
2463 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2464 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2465 * - then user does `ethtool -C eth0 rx-usecs 100`
2467 * since ethtool sends to kernel whole ethtool_coalesce
2468 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2469 * we'll reject it below in `frames % 4 != 0`.
2471 if (p->frames == 1) {
2475 units = p->usecs * 1000 / scale->nsecs[i];
2476 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2479 w <<= RTL_COALESCE_SHIFT;
2481 w <<= RTL_COALESCE_SHIFT;
2482 w |= p->frames >> 2;
2487 RTL_W16(tp, IntrMitigate, swab16(w));
2489 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
2490 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2491 RTL_R16(tp, CPlusCmd);
2493 rtl_unlock_work(tp);
2498 static const struct ethtool_ops rtl8169_ethtool_ops = {
2499 .get_drvinfo = rtl8169_get_drvinfo,
2500 .get_regs_len = rtl8169_get_regs_len,
2501 .get_link = ethtool_op_get_link,
2502 .get_coalesce = rtl_get_coalesce,
2503 .set_coalesce = rtl_set_coalesce,
2504 .get_msglevel = rtl8169_get_msglevel,
2505 .set_msglevel = rtl8169_set_msglevel,
2506 .get_regs = rtl8169_get_regs,
2507 .get_wol = rtl8169_get_wol,
2508 .set_wol = rtl8169_set_wol,
2509 .get_strings = rtl8169_get_strings,
2510 .get_sset_count = rtl8169_get_sset_count,
2511 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2512 .get_ts_info = ethtool_op_get_ts_info,
2513 .nway_reset = rtl8169_nway_reset,
2514 .get_link_ksettings = rtl8169_get_link_ksettings,
2515 .set_link_ksettings = rtl8169_set_link_ksettings,
2518 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2519 struct net_device *dev, u8 default_version)
2522 * The driver currently handles the 8168Bf and the 8168Be identically
2523 * but they can be identified more specifically through the test below
2526 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2528 * Same thing for the 8101Eb and the 8101Ec:
2530 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2532 static const struct rtl_mac_info {
2537 /* 8168EP family. */
2538 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2539 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2540 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2543 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2544 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2547 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2548 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2549 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2550 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2553 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2554 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2555 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2558 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2559 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2560 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2561 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2564 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2565 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2566 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2568 /* 8168DP family. */
2569 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2570 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2571 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2574 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
2575 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2576 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2577 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2578 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2579 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2580 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2581 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
2582 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2585 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2586 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2587 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2588 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2591 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2592 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2593 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2594 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
2595 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2596 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2597 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2598 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2599 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2600 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2601 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2602 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2603 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2604 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2605 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2606 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2607 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2608 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2609 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2610 /* FIXME: where did these entries come from ? -- FR */
2611 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2612 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2615 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2616 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2617 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2618 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2619 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2620 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2623 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2625 const struct rtl_mac_info *p = mac_info;
2628 reg = RTL_R32(tp, TxConfig);
2629 while ((reg & p->mask) != p->val)
2631 tp->mac_version = p->mac_version;
2633 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2634 netif_notice(tp, probe, dev,
2635 "unknown MAC, using family default\n");
2636 tp->mac_version = default_version;
2637 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2638 tp->mac_version = tp->mii.supports_gmii ?
2639 RTL_GIGA_MAC_VER_42 :
2640 RTL_GIGA_MAC_VER_43;
2641 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2642 tp->mac_version = tp->mii.supports_gmii ?
2643 RTL_GIGA_MAC_VER_45 :
2644 RTL_GIGA_MAC_VER_47;
2645 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2646 tp->mac_version = tp->mii.supports_gmii ?
2647 RTL_GIGA_MAC_VER_46 :
2648 RTL_GIGA_MAC_VER_48;
2652 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2654 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2662 static void rtl_writephy_batch(struct rtl8169_private *tp,
2663 const struct phy_reg *regs, int len)
2666 rtl_writephy(tp, regs->reg, regs->val);
2671 #define PHY_READ 0x00000000
2672 #define PHY_DATA_OR 0x10000000
2673 #define PHY_DATA_AND 0x20000000
2674 #define PHY_BJMPN 0x30000000
2675 #define PHY_MDIO_CHG 0x40000000
2676 #define PHY_CLEAR_READCOUNT 0x70000000
2677 #define PHY_WRITE 0x80000000
2678 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2679 #define PHY_COMP_EQ_SKIPN 0xa0000000
2680 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2681 #define PHY_WRITE_PREVIOUS 0xc0000000
2682 #define PHY_SKIPN 0xd0000000
2683 #define PHY_DELAY_MS 0xe0000000
2687 char version[RTL_VER_SIZE];
2693 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2695 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2697 const struct firmware *fw = rtl_fw->fw;
2698 struct fw_info *fw_info = (struct fw_info *)fw->data;
2699 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2700 char *version = rtl_fw->version;
2703 if (fw->size < FW_OPCODE_SIZE)
2706 if (!fw_info->magic) {
2707 size_t i, size, start;
2710 if (fw->size < sizeof(*fw_info))
2713 for (i = 0; i < fw->size; i++)
2714 checksum += fw->data[i];
2718 start = le32_to_cpu(fw_info->fw_start);
2719 if (start > fw->size)
2722 size = le32_to_cpu(fw_info->fw_len);
2723 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2726 memcpy(version, fw_info->version, RTL_VER_SIZE);
2728 pa->code = (__le32 *)(fw->data + start);
2731 if (fw->size % FW_OPCODE_SIZE)
2734 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2736 pa->code = (__le32 *)fw->data;
2737 pa->size = fw->size / FW_OPCODE_SIZE;
2739 version[RTL_VER_SIZE - 1] = 0;
2746 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2747 struct rtl_fw_phy_action *pa)
2752 for (index = 0; index < pa->size; index++) {
2753 u32 action = le32_to_cpu(pa->code[index]);
2754 u32 regno = (action & 0x0fff0000) >> 16;
2756 switch(action & 0xf0000000) {
2761 case PHY_CLEAR_READCOUNT:
2763 case PHY_WRITE_PREVIOUS:
2768 if (regno > index) {
2769 netif_err(tp, ifup, tp->dev,
2770 "Out of range of firmware\n");
2774 case PHY_READCOUNT_EQ_SKIP:
2775 if (index + 2 >= pa->size) {
2776 netif_err(tp, ifup, tp->dev,
2777 "Out of range of firmware\n");
2781 case PHY_COMP_EQ_SKIPN:
2782 case PHY_COMP_NEQ_SKIPN:
2784 if (index + 1 + regno >= pa->size) {
2785 netif_err(tp, ifup, tp->dev,
2786 "Out of range of firmware\n");
2792 netif_err(tp, ifup, tp->dev,
2793 "Invalid action 0x%08x\n", action);
2802 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2804 struct net_device *dev = tp->dev;
2807 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2808 netif_err(tp, ifup, dev, "invalid firmware\n");
2812 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2818 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2820 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2821 struct mdio_ops org, *ops = &tp->mdio_ops;
2825 predata = count = 0;
2826 org.write = ops->write;
2827 org.read = ops->read;
2829 for (index = 0; index < pa->size; ) {
2830 u32 action = le32_to_cpu(pa->code[index]);
2831 u32 data = action & 0x0000ffff;
2832 u32 regno = (action & 0x0fff0000) >> 16;
2837 switch(action & 0xf0000000) {
2839 predata = rtl_readphy(tp, regno);
2856 ops->write = org.write;
2857 ops->read = org.read;
2858 } else if (data == 1) {
2859 ops->write = mac_mcu_write;
2860 ops->read = mac_mcu_read;
2865 case PHY_CLEAR_READCOUNT:
2870 rtl_writephy(tp, regno, data);
2873 case PHY_READCOUNT_EQ_SKIP:
2874 index += (count == data) ? 2 : 1;
2876 case PHY_COMP_EQ_SKIPN:
2877 if (predata == data)
2881 case PHY_COMP_NEQ_SKIPN:
2882 if (predata != data)
2886 case PHY_WRITE_PREVIOUS:
2887 rtl_writephy(tp, regno, predata);
2903 ops->write = org.write;
2904 ops->read = org.read;
2907 static void rtl_release_firmware(struct rtl8169_private *tp)
2909 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2910 release_firmware(tp->rtl_fw->fw);
2913 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2916 static void rtl_apply_firmware(struct rtl8169_private *tp)
2918 struct rtl_fw *rtl_fw = tp->rtl_fw;
2920 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2921 if (!IS_ERR_OR_NULL(rtl_fw))
2922 rtl_phy_write_fw(tp, rtl_fw);
2925 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2927 if (rtl_readphy(tp, reg) != val)
2928 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2930 rtl_apply_firmware(tp);
2933 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2935 static const struct phy_reg phy_reg_init[] = {
2997 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3000 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
3002 static const struct phy_reg phy_reg_init[] = {
3008 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3011 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
3013 struct pci_dev *pdev = tp->pci_dev;
3015 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3016 (pdev->subsystem_device != 0xe000))
3019 rtl_writephy(tp, 0x1f, 0x0001);
3020 rtl_writephy(tp, 0x10, 0xf01b);
3021 rtl_writephy(tp, 0x1f, 0x0000);
3024 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
3026 static const struct phy_reg phy_reg_init[] = {
3066 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3068 rtl8169scd_hw_phy_config_quirk(tp);
3071 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
3073 static const struct phy_reg phy_reg_init[] = {
3121 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3124 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
3126 static const struct phy_reg phy_reg_init[] = {
3131 rtl_writephy(tp, 0x1f, 0x0001);
3132 rtl_patchphy(tp, 0x16, 1 << 0);
3134 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3137 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
3139 static const struct phy_reg phy_reg_init[] = {
3145 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3148 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
3150 static const struct phy_reg phy_reg_init[] = {
3158 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3161 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
3163 static const struct phy_reg phy_reg_init[] = {
3169 rtl_writephy(tp, 0x1f, 0x0000);
3170 rtl_patchphy(tp, 0x14, 1 << 5);
3171 rtl_patchphy(tp, 0x0d, 1 << 5);
3173 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3176 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
3178 static const struct phy_reg phy_reg_init[] = {
3198 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3200 rtl_patchphy(tp, 0x14, 1 << 5);
3201 rtl_patchphy(tp, 0x0d, 1 << 5);
3202 rtl_writephy(tp, 0x1f, 0x0000);
3205 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
3207 static const struct phy_reg phy_reg_init[] = {
3225 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3227 rtl_patchphy(tp, 0x16, 1 << 0);
3228 rtl_patchphy(tp, 0x14, 1 << 5);
3229 rtl_patchphy(tp, 0x0d, 1 << 5);
3230 rtl_writephy(tp, 0x1f, 0x0000);
3233 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
3235 static const struct phy_reg phy_reg_init[] = {
3247 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3249 rtl_patchphy(tp, 0x16, 1 << 0);
3250 rtl_patchphy(tp, 0x14, 1 << 5);
3251 rtl_patchphy(tp, 0x0d, 1 << 5);
3252 rtl_writephy(tp, 0x1f, 0x0000);
3255 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
3257 rtl8168c_3_hw_phy_config(tp);
3260 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
3262 static const struct phy_reg phy_reg_init_0[] = {
3263 /* Channel Estimation */
3284 * Enhance line driver power
3293 * Can not link to 1Gbps with bad cable
3294 * Decrease SNR threshold form 21.07dB to 19.04dB
3303 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3307 * Fine Tune Switching regulator parameter
3309 rtl_writephy(tp, 0x1f, 0x0002);
3310 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3311 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
3313 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3314 static const struct phy_reg phy_reg_init[] = {
3324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3326 val = rtl_readphy(tp, 0x0d);
3328 if ((val & 0x00ff) != 0x006c) {
3329 static const u32 set[] = {
3330 0x0065, 0x0066, 0x0067, 0x0068,
3331 0x0069, 0x006a, 0x006b, 0x006c
3335 rtl_writephy(tp, 0x1f, 0x0002);
3338 for (i = 0; i < ARRAY_SIZE(set); i++)
3339 rtl_writephy(tp, 0x0d, val | set[i]);
3342 static const struct phy_reg phy_reg_init[] = {
3350 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3353 /* RSET couple improve */
3354 rtl_writephy(tp, 0x1f, 0x0002);
3355 rtl_patchphy(tp, 0x0d, 0x0300);
3356 rtl_patchphy(tp, 0x0f, 0x0010);
3358 /* Fine tune PLL performance */
3359 rtl_writephy(tp, 0x1f, 0x0002);
3360 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3361 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3363 rtl_writephy(tp, 0x1f, 0x0005);
3364 rtl_writephy(tp, 0x05, 0x001b);
3366 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
3368 rtl_writephy(tp, 0x1f, 0x0000);
3371 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
3373 static const struct phy_reg phy_reg_init_0[] = {
3374 /* Channel Estimation */
3395 * Enhance line driver power
3404 * Can not link to 1Gbps with bad cable
3405 * Decrease SNR threshold form 21.07dB to 19.04dB
3414 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3416 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3417 static const struct phy_reg phy_reg_init[] = {
3428 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3430 val = rtl_readphy(tp, 0x0d);
3431 if ((val & 0x00ff) != 0x006c) {
3432 static const u32 set[] = {
3433 0x0065, 0x0066, 0x0067, 0x0068,
3434 0x0069, 0x006a, 0x006b, 0x006c
3438 rtl_writephy(tp, 0x1f, 0x0002);
3441 for (i = 0; i < ARRAY_SIZE(set); i++)
3442 rtl_writephy(tp, 0x0d, val | set[i]);
3445 static const struct phy_reg phy_reg_init[] = {
3453 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3456 /* Fine tune PLL performance */
3457 rtl_writephy(tp, 0x1f, 0x0002);
3458 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3459 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3461 /* Switching regulator Slew rate */
3462 rtl_writephy(tp, 0x1f, 0x0002);
3463 rtl_patchphy(tp, 0x0f, 0x0017);
3465 rtl_writephy(tp, 0x1f, 0x0005);
3466 rtl_writephy(tp, 0x05, 0x001b);
3468 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
3470 rtl_writephy(tp, 0x1f, 0x0000);
3473 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
3475 static const struct phy_reg phy_reg_init[] = {
3531 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3534 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3536 static const struct phy_reg phy_reg_init[] = {
3546 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3547 rtl_patchphy(tp, 0x0d, 1 << 5);
3550 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3552 static const struct phy_reg phy_reg_init[] = {
3553 /* Enable Delay cap */
3559 /* Channel estimation fine tune */
3568 /* Update PFM & 10M TX idle timer */
3580 rtl_apply_firmware(tp);
3582 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3584 /* DCO enable for 10M IDLE Power */
3585 rtl_writephy(tp, 0x1f, 0x0007);
3586 rtl_writephy(tp, 0x1e, 0x0023);
3587 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3588 rtl_writephy(tp, 0x1f, 0x0000);
3590 /* For impedance matching */
3591 rtl_writephy(tp, 0x1f, 0x0002);
3592 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3593 rtl_writephy(tp, 0x1f, 0x0000);
3595 /* PHY auto speed down */
3596 rtl_writephy(tp, 0x1f, 0x0007);
3597 rtl_writephy(tp, 0x1e, 0x002d);
3598 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3599 rtl_writephy(tp, 0x1f, 0x0000);
3600 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3602 rtl_writephy(tp, 0x1f, 0x0005);
3603 rtl_writephy(tp, 0x05, 0x8b86);
3604 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3605 rtl_writephy(tp, 0x1f, 0x0000);
3607 rtl_writephy(tp, 0x1f, 0x0005);
3608 rtl_writephy(tp, 0x05, 0x8b85);
3609 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3610 rtl_writephy(tp, 0x1f, 0x0007);
3611 rtl_writephy(tp, 0x1e, 0x0020);
3612 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3613 rtl_writephy(tp, 0x1f, 0x0006);
3614 rtl_writephy(tp, 0x00, 0x5a00);
3615 rtl_writephy(tp, 0x1f, 0x0000);
3616 rtl_writephy(tp, 0x0d, 0x0007);
3617 rtl_writephy(tp, 0x0e, 0x003c);
3618 rtl_writephy(tp, 0x0d, 0x4007);
3619 rtl_writephy(tp, 0x0e, 0x0000);
3620 rtl_writephy(tp, 0x0d, 0x0000);
3623 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3626 addr[0] | (addr[1] << 8),
3627 addr[2] | (addr[3] << 8),
3628 addr[4] | (addr[5] << 8)
3630 const struct exgmac_reg e[] = {
3631 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3632 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3633 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3634 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3637 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3640 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3642 static const struct phy_reg phy_reg_init[] = {
3643 /* Enable Delay cap */
3652 /* Channel estimation fine tune */
3669 rtl_apply_firmware(tp);
3671 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3673 /* For 4-corner performance improve */
3674 rtl_writephy(tp, 0x1f, 0x0005);
3675 rtl_writephy(tp, 0x05, 0x8b80);
3676 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3677 rtl_writephy(tp, 0x1f, 0x0000);
3679 /* PHY auto speed down */
3680 rtl_writephy(tp, 0x1f, 0x0004);
3681 rtl_writephy(tp, 0x1f, 0x0007);
3682 rtl_writephy(tp, 0x1e, 0x002d);
3683 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3684 rtl_writephy(tp, 0x1f, 0x0002);
3685 rtl_writephy(tp, 0x1f, 0x0000);
3686 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3688 /* improve 10M EEE waveform */
3689 rtl_writephy(tp, 0x1f, 0x0005);
3690 rtl_writephy(tp, 0x05, 0x8b86);
3691 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3692 rtl_writephy(tp, 0x1f, 0x0000);
3694 /* Improve 2-pair detection performance */
3695 rtl_writephy(tp, 0x1f, 0x0005);
3696 rtl_writephy(tp, 0x05, 0x8b85);
3697 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3698 rtl_writephy(tp, 0x1f, 0x0000);
3701 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
3702 rtl_writephy(tp, 0x1f, 0x0005);
3703 rtl_writephy(tp, 0x05, 0x8b85);
3704 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
3705 rtl_writephy(tp, 0x1f, 0x0004);
3706 rtl_writephy(tp, 0x1f, 0x0007);
3707 rtl_writephy(tp, 0x1e, 0x0020);
3708 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
3709 rtl_writephy(tp, 0x1f, 0x0002);
3710 rtl_writephy(tp, 0x1f, 0x0000);
3711 rtl_writephy(tp, 0x0d, 0x0007);
3712 rtl_writephy(tp, 0x0e, 0x003c);
3713 rtl_writephy(tp, 0x0d, 0x4007);
3714 rtl_writephy(tp, 0x0e, 0x0006);
3715 rtl_writephy(tp, 0x0d, 0x0000);
3718 rtl_writephy(tp, 0x1f, 0x0003);
3719 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3720 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
3721 rtl_writephy(tp, 0x1f, 0x0000);
3722 rtl_writephy(tp, 0x1f, 0x0005);
3723 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3724 rtl_writephy(tp, 0x1f, 0x0000);
3726 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3727 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3730 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3732 /* For 4-corner performance improve */
3733 rtl_writephy(tp, 0x1f, 0x0005);
3734 rtl_writephy(tp, 0x05, 0x8b80);
3735 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3736 rtl_writephy(tp, 0x1f, 0x0000);
3738 /* PHY auto speed down */
3739 rtl_writephy(tp, 0x1f, 0x0007);
3740 rtl_writephy(tp, 0x1e, 0x002d);
3741 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3742 rtl_writephy(tp, 0x1f, 0x0000);
3743 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3745 /* Improve 10M EEE waveform */
3746 rtl_writephy(tp, 0x1f, 0x0005);
3747 rtl_writephy(tp, 0x05, 0x8b86);
3748 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3749 rtl_writephy(tp, 0x1f, 0x0000);
3752 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3754 static const struct phy_reg phy_reg_init[] = {
3755 /* Channel estimation fine tune */
3760 /* Modify green table for giga & fnet */
3777 /* Modify green table for 10M */
3783 /* Disable hiimpedance detection (RTCT) */
3789 rtl_apply_firmware(tp);
3791 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3793 rtl8168f_hw_phy_config(tp);
3795 /* Improve 2-pair detection performance */
3796 rtl_writephy(tp, 0x1f, 0x0005);
3797 rtl_writephy(tp, 0x05, 0x8b85);
3798 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3799 rtl_writephy(tp, 0x1f, 0x0000);
3802 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3804 rtl_apply_firmware(tp);
3806 rtl8168f_hw_phy_config(tp);
3809 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3811 static const struct phy_reg phy_reg_init[] = {
3812 /* Channel estimation fine tune */
3817 /* Modify green table for giga & fnet */
3834 /* Modify green table for 10M */
3840 /* Disable hiimpedance detection (RTCT) */
3847 rtl_apply_firmware(tp);
3849 rtl8168f_hw_phy_config(tp);
3851 /* Improve 2-pair detection performance */
3852 rtl_writephy(tp, 0x1f, 0x0005);
3853 rtl_writephy(tp, 0x05, 0x8b85);
3854 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3855 rtl_writephy(tp, 0x1f, 0x0000);
3857 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3859 /* Modify green table for giga */
3860 rtl_writephy(tp, 0x1f, 0x0005);
3861 rtl_writephy(tp, 0x05, 0x8b54);
3862 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3863 rtl_writephy(tp, 0x05, 0x8b5d);
3864 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3865 rtl_writephy(tp, 0x05, 0x8a7c);
3866 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3867 rtl_writephy(tp, 0x05, 0x8a7f);
3868 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3869 rtl_writephy(tp, 0x05, 0x8a82);
3870 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3871 rtl_writephy(tp, 0x05, 0x8a85);
3872 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3873 rtl_writephy(tp, 0x05, 0x8a88);
3874 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3875 rtl_writephy(tp, 0x1f, 0x0000);
3877 /* uc same-seed solution */
3878 rtl_writephy(tp, 0x1f, 0x0005);
3879 rtl_writephy(tp, 0x05, 0x8b85);
3880 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3881 rtl_writephy(tp, 0x1f, 0x0000);
3884 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3885 rtl_writephy(tp, 0x1f, 0x0005);
3886 rtl_writephy(tp, 0x05, 0x8b85);
3887 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3888 rtl_writephy(tp, 0x1f, 0x0004);
3889 rtl_writephy(tp, 0x1f, 0x0007);
3890 rtl_writephy(tp, 0x1e, 0x0020);
3891 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3892 rtl_writephy(tp, 0x1f, 0x0000);
3893 rtl_writephy(tp, 0x0d, 0x0007);
3894 rtl_writephy(tp, 0x0e, 0x003c);
3895 rtl_writephy(tp, 0x0d, 0x4007);
3896 rtl_writephy(tp, 0x0e, 0x0000);
3897 rtl_writephy(tp, 0x0d, 0x0000);
3900 rtl_writephy(tp, 0x1f, 0x0003);
3901 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3902 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3903 rtl_writephy(tp, 0x1f, 0x0000);
3906 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3908 rtl_apply_firmware(tp);
3910 rtl_writephy(tp, 0x1f, 0x0a46);
3911 if (rtl_readphy(tp, 0x10) & 0x0100) {
3912 rtl_writephy(tp, 0x1f, 0x0bcc);
3913 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3915 rtl_writephy(tp, 0x1f, 0x0bcc);
3916 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3919 rtl_writephy(tp, 0x1f, 0x0a46);
3920 if (rtl_readphy(tp, 0x13) & 0x0100) {
3921 rtl_writephy(tp, 0x1f, 0x0c41);
3922 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3924 rtl_writephy(tp, 0x1f, 0x0c41);
3925 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3928 /* Enable PHY auto speed down */
3929 rtl_writephy(tp, 0x1f, 0x0a44);
3930 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3932 rtl_writephy(tp, 0x1f, 0x0bcc);
3933 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
3934 rtl_writephy(tp, 0x1f, 0x0a44);
3935 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3936 rtl_writephy(tp, 0x1f, 0x0a43);
3937 rtl_writephy(tp, 0x13, 0x8084);
3938 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3939 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3941 /* EEE auto-fallback function */
3942 rtl_writephy(tp, 0x1f, 0x0a4b);
3943 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3945 /* Enable UC LPF tune function */
3946 rtl_writephy(tp, 0x1f, 0x0a43);
3947 rtl_writephy(tp, 0x13, 0x8012);
3948 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3950 rtl_writephy(tp, 0x1f, 0x0c42);
3951 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3953 /* Improve SWR Efficiency */
3954 rtl_writephy(tp, 0x1f, 0x0bcd);
3955 rtl_writephy(tp, 0x14, 0x5065);
3956 rtl_writephy(tp, 0x14, 0xd065);
3957 rtl_writephy(tp, 0x1f, 0x0bc8);
3958 rtl_writephy(tp, 0x11, 0x5655);
3959 rtl_writephy(tp, 0x1f, 0x0bcd);
3960 rtl_writephy(tp, 0x14, 0x1065);
3961 rtl_writephy(tp, 0x14, 0x9065);
3962 rtl_writephy(tp, 0x14, 0x1065);
3964 /* Check ALDPS bit, disable it if enabled */
3965 rtl_writephy(tp, 0x1f, 0x0a43);
3966 if (rtl_readphy(tp, 0x10) & 0x0004)
3967 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3969 rtl_writephy(tp, 0x1f, 0x0000);
3972 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3974 rtl_apply_firmware(tp);
3977 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3982 rtl_apply_firmware(tp);
3984 /* CHN EST parameters adjust - giga master */
3985 rtl_writephy(tp, 0x1f, 0x0a43);
3986 rtl_writephy(tp, 0x13, 0x809b);
3987 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3988 rtl_writephy(tp, 0x13, 0x80a2);
3989 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3990 rtl_writephy(tp, 0x13, 0x80a4);
3991 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3992 rtl_writephy(tp, 0x13, 0x809c);
3993 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3994 rtl_writephy(tp, 0x1f, 0x0000);
3996 /* CHN EST parameters adjust - giga slave */
3997 rtl_writephy(tp, 0x1f, 0x0a43);
3998 rtl_writephy(tp, 0x13, 0x80ad);
3999 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
4000 rtl_writephy(tp, 0x13, 0x80b4);
4001 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
4002 rtl_writephy(tp, 0x13, 0x80ac);
4003 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
4004 rtl_writephy(tp, 0x1f, 0x0000);
4006 /* CHN EST parameters adjust - fnet */
4007 rtl_writephy(tp, 0x1f, 0x0a43);
4008 rtl_writephy(tp, 0x13, 0x808e);
4009 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
4010 rtl_writephy(tp, 0x13, 0x8090);
4011 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
4012 rtl_writephy(tp, 0x13, 0x8092);
4013 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
4014 rtl_writephy(tp, 0x1f, 0x0000);
4016 /* enable R-tune & PGA-retune function */
4018 rtl_writephy(tp, 0x1f, 0x0a46);
4019 data = rtl_readphy(tp, 0x13);
4022 dout_tapbin |= data;
4023 data = rtl_readphy(tp, 0x12);
4026 dout_tapbin |= data;
4027 dout_tapbin = ~(dout_tapbin^0x08);
4029 dout_tapbin &= 0xf000;
4030 rtl_writephy(tp, 0x1f, 0x0a43);
4031 rtl_writephy(tp, 0x13, 0x827a);
4032 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
4033 rtl_writephy(tp, 0x13, 0x827b);
4034 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
4035 rtl_writephy(tp, 0x13, 0x827c);
4036 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
4037 rtl_writephy(tp, 0x13, 0x827d);
4038 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
4040 rtl_writephy(tp, 0x1f, 0x0a43);
4041 rtl_writephy(tp, 0x13, 0x0811);
4042 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
4043 rtl_writephy(tp, 0x1f, 0x0a42);
4044 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
4045 rtl_writephy(tp, 0x1f, 0x0000);
4047 /* enable GPHY 10M */
4048 rtl_writephy(tp, 0x1f, 0x0a44);
4049 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
4050 rtl_writephy(tp, 0x1f, 0x0000);
4052 /* SAR ADC performance */
4053 rtl_writephy(tp, 0x1f, 0x0bca);
4054 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
4055 rtl_writephy(tp, 0x1f, 0x0000);
4057 rtl_writephy(tp, 0x1f, 0x0a43);
4058 rtl_writephy(tp, 0x13, 0x803f);
4059 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4060 rtl_writephy(tp, 0x13, 0x8047);
4061 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4062 rtl_writephy(tp, 0x13, 0x804f);
4063 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4064 rtl_writephy(tp, 0x13, 0x8057);
4065 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4066 rtl_writephy(tp, 0x13, 0x805f);
4067 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4068 rtl_writephy(tp, 0x13, 0x8067);
4069 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4070 rtl_writephy(tp, 0x13, 0x806f);
4071 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4072 rtl_writephy(tp, 0x1f, 0x0000);
4074 /* disable phy pfm mode */
4075 rtl_writephy(tp, 0x1f, 0x0a44);
4076 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
4077 rtl_writephy(tp, 0x1f, 0x0000);
4079 /* Check ALDPS bit, disable it if enabled */
4080 rtl_writephy(tp, 0x1f, 0x0a43);
4081 if (rtl_readphy(tp, 0x10) & 0x0004)
4082 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4084 rtl_writephy(tp, 0x1f, 0x0000);
4087 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4089 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4093 rtl_apply_firmware(tp);
4095 /* CHIN EST parameter update */
4096 rtl_writephy(tp, 0x1f, 0x0a43);
4097 rtl_writephy(tp, 0x13, 0x808a);
4098 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
4099 rtl_writephy(tp, 0x1f, 0x0000);
4101 /* enable R-tune & PGA-retune function */
4102 rtl_writephy(tp, 0x1f, 0x0a43);
4103 rtl_writephy(tp, 0x13, 0x0811);
4104 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
4105 rtl_writephy(tp, 0x1f, 0x0a42);
4106 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
4107 rtl_writephy(tp, 0x1f, 0x0000);
4109 /* enable GPHY 10M */
4110 rtl_writephy(tp, 0x1f, 0x0a44);
4111 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
4112 rtl_writephy(tp, 0x1f, 0x0000);
4114 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4115 data = r8168_mac_ocp_read(tp, 0xdd02);
4116 ioffset_p3 = ((data & 0x80)>>7);
4119 data = r8168_mac_ocp_read(tp, 0xdd00);
4120 ioffset_p3 |= ((data & (0xe000))>>13);
4121 ioffset_p2 = ((data & (0x1e00))>>9);
4122 ioffset_p1 = ((data & (0x01e0))>>5);
4123 ioffset_p0 = ((data & 0x0010)>>4);
4125 ioffset_p0 |= (data & (0x07));
4126 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4128 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
4129 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
4130 rtl_writephy(tp, 0x1f, 0x0bcf);
4131 rtl_writephy(tp, 0x16, data);
4132 rtl_writephy(tp, 0x1f, 0x0000);
4135 /* Modify rlen (TX LPF corner frequency) level */
4136 rtl_writephy(tp, 0x1f, 0x0bcd);
4137 data = rtl_readphy(tp, 0x16);
4142 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4143 rtl_writephy(tp, 0x17, data);
4144 rtl_writephy(tp, 0x1f, 0x0bcd);
4145 rtl_writephy(tp, 0x1f, 0x0000);
4147 /* disable phy pfm mode */
4148 rtl_writephy(tp, 0x1f, 0x0a44);
4149 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
4150 rtl_writephy(tp, 0x1f, 0x0000);
4152 /* Check ALDPS bit, disable it if enabled */
4153 rtl_writephy(tp, 0x1f, 0x0a43);
4154 if (rtl_readphy(tp, 0x10) & 0x0004)
4155 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4157 rtl_writephy(tp, 0x1f, 0x0000);
4160 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4162 /* Enable PHY auto speed down */
4163 rtl_writephy(tp, 0x1f, 0x0a44);
4164 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4165 rtl_writephy(tp, 0x1f, 0x0000);
4167 /* patch 10M & ALDPS */
4168 rtl_writephy(tp, 0x1f, 0x0bcc);
4169 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4170 rtl_writephy(tp, 0x1f, 0x0a44);
4171 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4172 rtl_writephy(tp, 0x1f, 0x0a43);
4173 rtl_writephy(tp, 0x13, 0x8084);
4174 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4175 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4176 rtl_writephy(tp, 0x1f, 0x0000);
4178 /* Enable EEE auto-fallback function */
4179 rtl_writephy(tp, 0x1f, 0x0a4b);
4180 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4181 rtl_writephy(tp, 0x1f, 0x0000);
4183 /* Enable UC LPF tune function */
4184 rtl_writephy(tp, 0x1f, 0x0a43);
4185 rtl_writephy(tp, 0x13, 0x8012);
4186 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4187 rtl_writephy(tp, 0x1f, 0x0000);
4189 /* set rg_sel_sdm_rate */
4190 rtl_writephy(tp, 0x1f, 0x0c42);
4191 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4192 rtl_writephy(tp, 0x1f, 0x0000);
4194 /* Check ALDPS bit, disable it if enabled */
4195 rtl_writephy(tp, 0x1f, 0x0a43);
4196 if (rtl_readphy(tp, 0x10) & 0x0004)
4197 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4199 rtl_writephy(tp, 0x1f, 0x0000);
4202 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4204 /* patch 10M & ALDPS */
4205 rtl_writephy(tp, 0x1f, 0x0bcc);
4206 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4207 rtl_writephy(tp, 0x1f, 0x0a44);
4208 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4209 rtl_writephy(tp, 0x1f, 0x0a43);
4210 rtl_writephy(tp, 0x13, 0x8084);
4211 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4212 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4213 rtl_writephy(tp, 0x1f, 0x0000);
4215 /* Enable UC LPF tune function */
4216 rtl_writephy(tp, 0x1f, 0x0a43);
4217 rtl_writephy(tp, 0x13, 0x8012);
4218 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4219 rtl_writephy(tp, 0x1f, 0x0000);
4221 /* Set rg_sel_sdm_rate */
4222 rtl_writephy(tp, 0x1f, 0x0c42);
4223 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4224 rtl_writephy(tp, 0x1f, 0x0000);
4226 /* Channel estimation parameters */
4227 rtl_writephy(tp, 0x1f, 0x0a43);
4228 rtl_writephy(tp, 0x13, 0x80f3);
4229 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4230 rtl_writephy(tp, 0x13, 0x80f0);
4231 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4232 rtl_writephy(tp, 0x13, 0x80ef);
4233 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4234 rtl_writephy(tp, 0x13, 0x80f6);
4235 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4236 rtl_writephy(tp, 0x13, 0x80ec);
4237 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4238 rtl_writephy(tp, 0x13, 0x80ed);
4239 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4240 rtl_writephy(tp, 0x13, 0x80f2);
4241 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4242 rtl_writephy(tp, 0x13, 0x80f4);
4243 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4244 rtl_writephy(tp, 0x1f, 0x0a43);
4245 rtl_writephy(tp, 0x13, 0x8110);
4246 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4247 rtl_writephy(tp, 0x13, 0x810f);
4248 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4249 rtl_writephy(tp, 0x13, 0x8111);
4250 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4251 rtl_writephy(tp, 0x13, 0x8113);
4252 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4253 rtl_writephy(tp, 0x13, 0x8115);
4254 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4255 rtl_writephy(tp, 0x13, 0x810e);
4256 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4257 rtl_writephy(tp, 0x13, 0x810c);
4258 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4259 rtl_writephy(tp, 0x13, 0x810b);
4260 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4261 rtl_writephy(tp, 0x1f, 0x0a43);
4262 rtl_writephy(tp, 0x13, 0x80d1);
4263 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4264 rtl_writephy(tp, 0x13, 0x80cd);
4265 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4266 rtl_writephy(tp, 0x13, 0x80d3);
4267 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4268 rtl_writephy(tp, 0x13, 0x80d5);
4269 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4270 rtl_writephy(tp, 0x13, 0x80d7);
4271 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4273 /* Force PWM-mode */
4274 rtl_writephy(tp, 0x1f, 0x0bcd);
4275 rtl_writephy(tp, 0x14, 0x5065);
4276 rtl_writephy(tp, 0x14, 0xd065);
4277 rtl_writephy(tp, 0x1f, 0x0bc8);
4278 rtl_writephy(tp, 0x12, 0x00ed);
4279 rtl_writephy(tp, 0x1f, 0x0bcd);
4280 rtl_writephy(tp, 0x14, 0x1065);
4281 rtl_writephy(tp, 0x14, 0x9065);
4282 rtl_writephy(tp, 0x14, 0x1065);
4283 rtl_writephy(tp, 0x1f, 0x0000);
4285 /* Check ALDPS bit, disable it if enabled */
4286 rtl_writephy(tp, 0x1f, 0x0a43);
4287 if (rtl_readphy(tp, 0x10) & 0x0004)
4288 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4290 rtl_writephy(tp, 0x1f, 0x0000);
4293 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
4295 static const struct phy_reg phy_reg_init[] = {
4302 rtl_writephy(tp, 0x1f, 0x0000);
4303 rtl_patchphy(tp, 0x11, 1 << 12);
4304 rtl_patchphy(tp, 0x19, 1 << 13);
4305 rtl_patchphy(tp, 0x10, 1 << 15);
4307 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4310 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4312 static const struct phy_reg phy_reg_init[] = {
4326 /* Disable ALDPS before ram code */
4327 rtl_writephy(tp, 0x1f, 0x0000);
4328 rtl_writephy(tp, 0x18, 0x0310);
4331 rtl_apply_firmware(tp);
4333 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4336 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4338 /* Disable ALDPS before setting firmware */
4339 rtl_writephy(tp, 0x1f, 0x0000);
4340 rtl_writephy(tp, 0x18, 0x0310);
4343 rtl_apply_firmware(tp);
4346 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4347 rtl_writephy(tp, 0x1f, 0x0004);
4348 rtl_writephy(tp, 0x10, 0x401f);
4349 rtl_writephy(tp, 0x19, 0x7030);
4350 rtl_writephy(tp, 0x1f, 0x0000);
4353 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4355 static const struct phy_reg phy_reg_init[] = {
4362 /* Disable ALDPS before ram code */
4363 rtl_writephy(tp, 0x1f, 0x0000);
4364 rtl_writephy(tp, 0x18, 0x0310);
4367 rtl_apply_firmware(tp);
4369 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4370 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4372 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4375 static void rtl_hw_phy_config(struct net_device *dev)
4377 struct rtl8169_private *tp = netdev_priv(dev);
4379 rtl8169_print_mac_version(tp);
4381 switch (tp->mac_version) {
4382 case RTL_GIGA_MAC_VER_01:
4384 case RTL_GIGA_MAC_VER_02:
4385 case RTL_GIGA_MAC_VER_03:
4386 rtl8169s_hw_phy_config(tp);
4388 case RTL_GIGA_MAC_VER_04:
4389 rtl8169sb_hw_phy_config(tp);
4391 case RTL_GIGA_MAC_VER_05:
4392 rtl8169scd_hw_phy_config(tp);
4394 case RTL_GIGA_MAC_VER_06:
4395 rtl8169sce_hw_phy_config(tp);
4397 case RTL_GIGA_MAC_VER_07:
4398 case RTL_GIGA_MAC_VER_08:
4399 case RTL_GIGA_MAC_VER_09:
4400 rtl8102e_hw_phy_config(tp);
4402 case RTL_GIGA_MAC_VER_11:
4403 rtl8168bb_hw_phy_config(tp);
4405 case RTL_GIGA_MAC_VER_12:
4406 rtl8168bef_hw_phy_config(tp);
4408 case RTL_GIGA_MAC_VER_17:
4409 rtl8168bef_hw_phy_config(tp);
4411 case RTL_GIGA_MAC_VER_18:
4412 rtl8168cp_1_hw_phy_config(tp);
4414 case RTL_GIGA_MAC_VER_19:
4415 rtl8168c_1_hw_phy_config(tp);
4417 case RTL_GIGA_MAC_VER_20:
4418 rtl8168c_2_hw_phy_config(tp);
4420 case RTL_GIGA_MAC_VER_21:
4421 rtl8168c_3_hw_phy_config(tp);
4423 case RTL_GIGA_MAC_VER_22:
4424 rtl8168c_4_hw_phy_config(tp);
4426 case RTL_GIGA_MAC_VER_23:
4427 case RTL_GIGA_MAC_VER_24:
4428 rtl8168cp_2_hw_phy_config(tp);
4430 case RTL_GIGA_MAC_VER_25:
4431 rtl8168d_1_hw_phy_config(tp);
4433 case RTL_GIGA_MAC_VER_26:
4434 rtl8168d_2_hw_phy_config(tp);
4436 case RTL_GIGA_MAC_VER_27:
4437 rtl8168d_3_hw_phy_config(tp);
4439 case RTL_GIGA_MAC_VER_28:
4440 rtl8168d_4_hw_phy_config(tp);
4442 case RTL_GIGA_MAC_VER_29:
4443 case RTL_GIGA_MAC_VER_30:
4444 rtl8105e_hw_phy_config(tp);
4446 case RTL_GIGA_MAC_VER_31:
4449 case RTL_GIGA_MAC_VER_32:
4450 case RTL_GIGA_MAC_VER_33:
4451 rtl8168e_1_hw_phy_config(tp);
4453 case RTL_GIGA_MAC_VER_34:
4454 rtl8168e_2_hw_phy_config(tp);
4456 case RTL_GIGA_MAC_VER_35:
4457 rtl8168f_1_hw_phy_config(tp);
4459 case RTL_GIGA_MAC_VER_36:
4460 rtl8168f_2_hw_phy_config(tp);
4463 case RTL_GIGA_MAC_VER_37:
4464 rtl8402_hw_phy_config(tp);
4467 case RTL_GIGA_MAC_VER_38:
4468 rtl8411_hw_phy_config(tp);
4471 case RTL_GIGA_MAC_VER_39:
4472 rtl8106e_hw_phy_config(tp);
4475 case RTL_GIGA_MAC_VER_40:
4476 rtl8168g_1_hw_phy_config(tp);
4478 case RTL_GIGA_MAC_VER_42:
4479 case RTL_GIGA_MAC_VER_43:
4480 case RTL_GIGA_MAC_VER_44:
4481 rtl8168g_2_hw_phy_config(tp);
4483 case RTL_GIGA_MAC_VER_45:
4484 case RTL_GIGA_MAC_VER_47:
4485 rtl8168h_1_hw_phy_config(tp);
4487 case RTL_GIGA_MAC_VER_46:
4488 case RTL_GIGA_MAC_VER_48:
4489 rtl8168h_2_hw_phy_config(tp);
4492 case RTL_GIGA_MAC_VER_49:
4493 rtl8168ep_1_hw_phy_config(tp);
4495 case RTL_GIGA_MAC_VER_50:
4496 case RTL_GIGA_MAC_VER_51:
4497 rtl8168ep_2_hw_phy_config(tp);
4500 case RTL_GIGA_MAC_VER_41:
4506 static void rtl_phy_work(struct rtl8169_private *tp)
4508 struct timer_list *timer = &tp->timer;
4509 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4511 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
4513 if (tp->phy_reset_pending(tp)) {
4515 * A busy loop could burn quite a few cycles on nowadays CPU.
4516 * Let's delay the execution of the timer for a few ticks.
4522 if (tp->link_ok(tp))
4525 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
4527 tp->phy_reset_enable(tp);
4530 mod_timer(timer, jiffies + timeout);
4533 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4535 if (!test_and_set_bit(flag, tp->wk.flags))
4536 schedule_work(&tp->wk.work);
4539 static void rtl8169_phy_timer(struct timer_list *t)
4541 struct rtl8169_private *tp = from_timer(tp, t, timer);
4543 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
4546 DECLARE_RTL_COND(rtl_phy_reset_cond)
4548 return tp->phy_reset_pending(tp);
4551 static void rtl8169_phy_reset(struct net_device *dev,
4552 struct rtl8169_private *tp)
4554 tp->phy_reset_enable(tp);
4555 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
4558 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4560 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4561 (RTL_R8(tp, PHYstatus) & TBI_Enable);
4564 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4566 rtl_hw_phy_config(dev);
4568 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4569 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4570 RTL_W8(tp, 0x82, 0x01);
4573 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4575 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4576 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4578 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
4579 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4580 RTL_W8(tp, 0x82, 0x01);
4581 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
4582 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
4585 rtl8169_phy_reset(dev, tp);
4587 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4588 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4589 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4590 (tp->mii.supports_gmii ?
4591 ADVERTISED_1000baseT_Half |
4592 ADVERTISED_1000baseT_Full : 0));
4594 if (rtl_tbi_enabled(tp))
4595 netif_info(tp, link, dev, "TBI auto-negotiating\n");
4598 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4602 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4604 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4607 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4610 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4611 rtl_rar_exgmac_set(tp, addr);
4613 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4615 rtl_unlock_work(tp);
4618 static int rtl_set_mac_address(struct net_device *dev, void *p)
4620 struct rtl8169_private *tp = netdev_priv(dev);
4621 struct device *d = tp_to_dev(tp);
4624 ret = eth_mac_addr(dev, p);
4628 pm_runtime_get_noresume(d);
4630 if (pm_runtime_active(d))
4631 rtl_rar_set(tp, dev->dev_addr);
4633 pm_runtime_put_noidle(d);
4638 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4640 struct rtl8169_private *tp = netdev_priv(dev);
4641 struct mii_ioctl_data *data = if_mii(ifr);
4643 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4646 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4647 struct mii_ioctl_data *data, int cmd)
4651 data->phy_id = 32; /* Internal PHY */
4655 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
4659 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
4665 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4670 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4672 struct mdio_ops *ops = &tp->mdio_ops;
4674 switch (tp->mac_version) {
4675 case RTL_GIGA_MAC_VER_27:
4676 ops->write = r8168dp_1_mdio_write;
4677 ops->read = r8168dp_1_mdio_read;
4679 case RTL_GIGA_MAC_VER_28:
4680 case RTL_GIGA_MAC_VER_31:
4681 ops->write = r8168dp_2_mdio_write;
4682 ops->read = r8168dp_2_mdio_read;
4684 case RTL_GIGA_MAC_VER_40:
4685 case RTL_GIGA_MAC_VER_41:
4686 case RTL_GIGA_MAC_VER_42:
4687 case RTL_GIGA_MAC_VER_43:
4688 case RTL_GIGA_MAC_VER_44:
4689 case RTL_GIGA_MAC_VER_45:
4690 case RTL_GIGA_MAC_VER_46:
4691 case RTL_GIGA_MAC_VER_47:
4692 case RTL_GIGA_MAC_VER_48:
4693 case RTL_GIGA_MAC_VER_49:
4694 case RTL_GIGA_MAC_VER_50:
4695 case RTL_GIGA_MAC_VER_51:
4696 ops->write = r8168g_mdio_write;
4697 ops->read = r8168g_mdio_read;
4700 ops->write = r8169_mdio_write;
4701 ops->read = r8169_mdio_read;
4706 static void rtl_speed_down(struct rtl8169_private *tp)
4711 rtl_writephy(tp, 0x1f, 0x0000);
4712 lpa = rtl_readphy(tp, MII_LPA);
4714 if (lpa & (LPA_10HALF | LPA_10FULL))
4715 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4716 else if (lpa & (LPA_100HALF | LPA_100FULL))
4717 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4718 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4720 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4721 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4722 (tp->mii.supports_gmii ?
4723 ADVERTISED_1000baseT_Half |
4724 ADVERTISED_1000baseT_Full : 0);
4726 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4730 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4732 switch (tp->mac_version) {
4733 case RTL_GIGA_MAC_VER_25:
4734 case RTL_GIGA_MAC_VER_26:
4735 case RTL_GIGA_MAC_VER_29:
4736 case RTL_GIGA_MAC_VER_30:
4737 case RTL_GIGA_MAC_VER_32:
4738 case RTL_GIGA_MAC_VER_33:
4739 case RTL_GIGA_MAC_VER_34:
4740 case RTL_GIGA_MAC_VER_37:
4741 case RTL_GIGA_MAC_VER_38:
4742 case RTL_GIGA_MAC_VER_39:
4743 case RTL_GIGA_MAC_VER_40:
4744 case RTL_GIGA_MAC_VER_41:
4745 case RTL_GIGA_MAC_VER_42:
4746 case RTL_GIGA_MAC_VER_43:
4747 case RTL_GIGA_MAC_VER_44:
4748 case RTL_GIGA_MAC_VER_45:
4749 case RTL_GIGA_MAC_VER_46:
4750 case RTL_GIGA_MAC_VER_47:
4751 case RTL_GIGA_MAC_VER_48:
4752 case RTL_GIGA_MAC_VER_49:
4753 case RTL_GIGA_MAC_VER_50:
4754 case RTL_GIGA_MAC_VER_51:
4755 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
4756 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4763 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4765 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4769 rtl_wol_suspend_quirk(tp);
4774 static void r810x_phy_power_down(struct rtl8169_private *tp)
4776 rtl_writephy(tp, 0x1f, 0x0000);
4777 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4780 static void r810x_phy_power_up(struct rtl8169_private *tp)
4782 rtl_writephy(tp, 0x1f, 0x0000);
4783 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4786 static void r810x_pll_power_down(struct rtl8169_private *tp)
4788 if (rtl_wol_pll_power_down(tp))
4791 r810x_phy_power_down(tp);
4793 switch (tp->mac_version) {
4794 case RTL_GIGA_MAC_VER_07:
4795 case RTL_GIGA_MAC_VER_08:
4796 case RTL_GIGA_MAC_VER_09:
4797 case RTL_GIGA_MAC_VER_10:
4798 case RTL_GIGA_MAC_VER_13:
4799 case RTL_GIGA_MAC_VER_16:
4802 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4807 static void r810x_pll_power_up(struct rtl8169_private *tp)
4809 r810x_phy_power_up(tp);
4811 switch (tp->mac_version) {
4812 case RTL_GIGA_MAC_VER_07:
4813 case RTL_GIGA_MAC_VER_08:
4814 case RTL_GIGA_MAC_VER_09:
4815 case RTL_GIGA_MAC_VER_10:
4816 case RTL_GIGA_MAC_VER_13:
4817 case RTL_GIGA_MAC_VER_16:
4819 case RTL_GIGA_MAC_VER_47:
4820 case RTL_GIGA_MAC_VER_48:
4821 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4824 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4829 static void r8168_phy_power_up(struct rtl8169_private *tp)
4831 rtl_writephy(tp, 0x1f, 0x0000);
4832 switch (tp->mac_version) {
4833 case RTL_GIGA_MAC_VER_11:
4834 case RTL_GIGA_MAC_VER_12:
4835 case RTL_GIGA_MAC_VER_17:
4836 case RTL_GIGA_MAC_VER_18:
4837 case RTL_GIGA_MAC_VER_19:
4838 case RTL_GIGA_MAC_VER_20:
4839 case RTL_GIGA_MAC_VER_21:
4840 case RTL_GIGA_MAC_VER_22:
4841 case RTL_GIGA_MAC_VER_23:
4842 case RTL_GIGA_MAC_VER_24:
4843 case RTL_GIGA_MAC_VER_25:
4844 case RTL_GIGA_MAC_VER_26:
4845 case RTL_GIGA_MAC_VER_27:
4846 case RTL_GIGA_MAC_VER_28:
4847 case RTL_GIGA_MAC_VER_31:
4848 rtl_writephy(tp, 0x0e, 0x0000);
4853 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4856 static void r8168_phy_power_down(struct rtl8169_private *tp)
4858 rtl_writephy(tp, 0x1f, 0x0000);
4859 switch (tp->mac_version) {
4860 case RTL_GIGA_MAC_VER_32:
4861 case RTL_GIGA_MAC_VER_33:
4862 case RTL_GIGA_MAC_VER_40:
4863 case RTL_GIGA_MAC_VER_41:
4864 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4867 case RTL_GIGA_MAC_VER_11:
4868 case RTL_GIGA_MAC_VER_12:
4869 case RTL_GIGA_MAC_VER_17:
4870 case RTL_GIGA_MAC_VER_18:
4871 case RTL_GIGA_MAC_VER_19:
4872 case RTL_GIGA_MAC_VER_20:
4873 case RTL_GIGA_MAC_VER_21:
4874 case RTL_GIGA_MAC_VER_22:
4875 case RTL_GIGA_MAC_VER_23:
4876 case RTL_GIGA_MAC_VER_24:
4877 case RTL_GIGA_MAC_VER_25:
4878 case RTL_GIGA_MAC_VER_26:
4879 case RTL_GIGA_MAC_VER_27:
4880 case RTL_GIGA_MAC_VER_28:
4881 case RTL_GIGA_MAC_VER_31:
4882 rtl_writephy(tp, 0x0e, 0x0200);
4884 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4889 static void r8168_pll_power_down(struct rtl8169_private *tp)
4891 if (r8168_check_dash(tp))
4894 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4895 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
4896 (RTL_R16(tp, CPlusCmd) & ASF)) {
4900 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4901 tp->mac_version == RTL_GIGA_MAC_VER_33)
4902 rtl_ephy_write(tp, 0x19, 0xff64);
4904 if (rtl_wol_pll_power_down(tp))
4907 r8168_phy_power_down(tp);
4909 switch (tp->mac_version) {
4910 case RTL_GIGA_MAC_VER_25:
4911 case RTL_GIGA_MAC_VER_26:
4912 case RTL_GIGA_MAC_VER_27:
4913 case RTL_GIGA_MAC_VER_28:
4914 case RTL_GIGA_MAC_VER_31:
4915 case RTL_GIGA_MAC_VER_32:
4916 case RTL_GIGA_MAC_VER_33:
4917 case RTL_GIGA_MAC_VER_44:
4918 case RTL_GIGA_MAC_VER_45:
4919 case RTL_GIGA_MAC_VER_46:
4920 case RTL_GIGA_MAC_VER_50:
4921 case RTL_GIGA_MAC_VER_51:
4922 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4924 case RTL_GIGA_MAC_VER_40:
4925 case RTL_GIGA_MAC_VER_41:
4926 case RTL_GIGA_MAC_VER_49:
4927 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4928 0xfc000000, ERIAR_EXGMAC);
4929 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4934 static void r8168_pll_power_up(struct rtl8169_private *tp)
4936 switch (tp->mac_version) {
4937 case RTL_GIGA_MAC_VER_25:
4938 case RTL_GIGA_MAC_VER_26:
4939 case RTL_GIGA_MAC_VER_27:
4940 case RTL_GIGA_MAC_VER_28:
4941 case RTL_GIGA_MAC_VER_31:
4942 case RTL_GIGA_MAC_VER_32:
4943 case RTL_GIGA_MAC_VER_33:
4944 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4946 case RTL_GIGA_MAC_VER_44:
4947 case RTL_GIGA_MAC_VER_45:
4948 case RTL_GIGA_MAC_VER_46:
4949 case RTL_GIGA_MAC_VER_50:
4950 case RTL_GIGA_MAC_VER_51:
4951 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4953 case RTL_GIGA_MAC_VER_40:
4954 case RTL_GIGA_MAC_VER_41:
4955 case RTL_GIGA_MAC_VER_49:
4956 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4957 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4958 0x00000000, ERIAR_EXGMAC);
4962 r8168_phy_power_up(tp);
4965 static void rtl_generic_op(struct rtl8169_private *tp,
4966 void (*op)(struct rtl8169_private *))
4972 static void rtl_pll_power_down(struct rtl8169_private *tp)
4974 rtl_generic_op(tp, tp->pll_power_ops.down);
4977 static void rtl_pll_power_up(struct rtl8169_private *tp)
4979 rtl_generic_op(tp, tp->pll_power_ops.up);
4982 static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
4984 struct pll_power_ops *ops = &tp->pll_power_ops;
4986 switch (tp->mac_version) {
4987 case RTL_GIGA_MAC_VER_07:
4988 case RTL_GIGA_MAC_VER_08:
4989 case RTL_GIGA_MAC_VER_09:
4990 case RTL_GIGA_MAC_VER_10:
4991 case RTL_GIGA_MAC_VER_16:
4992 case RTL_GIGA_MAC_VER_29:
4993 case RTL_GIGA_MAC_VER_30:
4994 case RTL_GIGA_MAC_VER_37:
4995 case RTL_GIGA_MAC_VER_39:
4996 case RTL_GIGA_MAC_VER_43:
4997 case RTL_GIGA_MAC_VER_47:
4998 case RTL_GIGA_MAC_VER_48:
4999 ops->down = r810x_pll_power_down;
5000 ops->up = r810x_pll_power_up;
5003 case RTL_GIGA_MAC_VER_11:
5004 case RTL_GIGA_MAC_VER_12:
5005 case RTL_GIGA_MAC_VER_17:
5006 case RTL_GIGA_MAC_VER_18:
5007 case RTL_GIGA_MAC_VER_19:
5008 case RTL_GIGA_MAC_VER_20:
5009 case RTL_GIGA_MAC_VER_21:
5010 case RTL_GIGA_MAC_VER_22:
5011 case RTL_GIGA_MAC_VER_23:
5012 case RTL_GIGA_MAC_VER_24:
5013 case RTL_GIGA_MAC_VER_25:
5014 case RTL_GIGA_MAC_VER_26:
5015 case RTL_GIGA_MAC_VER_27:
5016 case RTL_GIGA_MAC_VER_28:
5017 case RTL_GIGA_MAC_VER_31:
5018 case RTL_GIGA_MAC_VER_32:
5019 case RTL_GIGA_MAC_VER_33:
5020 case RTL_GIGA_MAC_VER_34:
5021 case RTL_GIGA_MAC_VER_35:
5022 case RTL_GIGA_MAC_VER_36:
5023 case RTL_GIGA_MAC_VER_38:
5024 case RTL_GIGA_MAC_VER_40:
5025 case RTL_GIGA_MAC_VER_41:
5026 case RTL_GIGA_MAC_VER_42:
5027 case RTL_GIGA_MAC_VER_44:
5028 case RTL_GIGA_MAC_VER_45:
5029 case RTL_GIGA_MAC_VER_46:
5030 case RTL_GIGA_MAC_VER_49:
5031 case RTL_GIGA_MAC_VER_50:
5032 case RTL_GIGA_MAC_VER_51:
5033 ops->down = r8168_pll_power_down;
5034 ops->up = r8168_pll_power_up;
5044 static void rtl_init_rxcfg(struct rtl8169_private *tp)
5046 switch (tp->mac_version) {
5047 case RTL_GIGA_MAC_VER_01:
5048 case RTL_GIGA_MAC_VER_02:
5049 case RTL_GIGA_MAC_VER_03:
5050 case RTL_GIGA_MAC_VER_04:
5051 case RTL_GIGA_MAC_VER_05:
5052 case RTL_GIGA_MAC_VER_06:
5053 case RTL_GIGA_MAC_VER_10:
5054 case RTL_GIGA_MAC_VER_11:
5055 case RTL_GIGA_MAC_VER_12:
5056 case RTL_GIGA_MAC_VER_13:
5057 case RTL_GIGA_MAC_VER_14:
5058 case RTL_GIGA_MAC_VER_15:
5059 case RTL_GIGA_MAC_VER_16:
5060 case RTL_GIGA_MAC_VER_17:
5061 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5063 case RTL_GIGA_MAC_VER_18:
5064 case RTL_GIGA_MAC_VER_19:
5065 case RTL_GIGA_MAC_VER_20:
5066 case RTL_GIGA_MAC_VER_21:
5067 case RTL_GIGA_MAC_VER_22:
5068 case RTL_GIGA_MAC_VER_23:
5069 case RTL_GIGA_MAC_VER_24:
5070 case RTL_GIGA_MAC_VER_34:
5071 case RTL_GIGA_MAC_VER_35:
5072 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5074 case RTL_GIGA_MAC_VER_40:
5075 case RTL_GIGA_MAC_VER_41:
5076 case RTL_GIGA_MAC_VER_42:
5077 case RTL_GIGA_MAC_VER_43:
5078 case RTL_GIGA_MAC_VER_44:
5079 case RTL_GIGA_MAC_VER_45:
5080 case RTL_GIGA_MAC_VER_46:
5081 case RTL_GIGA_MAC_VER_47:
5082 case RTL_GIGA_MAC_VER_48:
5083 case RTL_GIGA_MAC_VER_49:
5084 case RTL_GIGA_MAC_VER_50:
5085 case RTL_GIGA_MAC_VER_51:
5086 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
5089 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
5094 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5096 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
5099 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5101 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5102 rtl_generic_op(tp, tp->jumbo_ops.enable);
5103 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5106 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5108 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5109 rtl_generic_op(tp, tp->jumbo_ops.disable);
5110 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5113 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5115 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5116 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
5117 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
5120 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5122 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5123 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
5124 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5127 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5129 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5132 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5134 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5137 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5139 RTL_W8(tp, MaxTxPacketSize, 0x3f);
5140 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5141 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
5142 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
5145 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5147 RTL_W8(tp, MaxTxPacketSize, 0x0c);
5148 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5149 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
5150 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5153 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5155 rtl_tx_performance_tweak(tp,
5156 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
5159 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5161 rtl_tx_performance_tweak(tp,
5162 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
5165 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5167 r8168b_0_hw_jumbo_enable(tp);
5169 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
5172 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5174 r8168b_0_hw_jumbo_disable(tp);
5176 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
5179 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
5181 struct jumbo_ops *ops = &tp->jumbo_ops;
5183 switch (tp->mac_version) {
5184 case RTL_GIGA_MAC_VER_11:
5185 ops->disable = r8168b_0_hw_jumbo_disable;
5186 ops->enable = r8168b_0_hw_jumbo_enable;
5188 case RTL_GIGA_MAC_VER_12:
5189 case RTL_GIGA_MAC_VER_17:
5190 ops->disable = r8168b_1_hw_jumbo_disable;
5191 ops->enable = r8168b_1_hw_jumbo_enable;
5193 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5194 case RTL_GIGA_MAC_VER_19:
5195 case RTL_GIGA_MAC_VER_20:
5196 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5197 case RTL_GIGA_MAC_VER_22:
5198 case RTL_GIGA_MAC_VER_23:
5199 case RTL_GIGA_MAC_VER_24:
5200 case RTL_GIGA_MAC_VER_25:
5201 case RTL_GIGA_MAC_VER_26:
5202 ops->disable = r8168c_hw_jumbo_disable;
5203 ops->enable = r8168c_hw_jumbo_enable;
5205 case RTL_GIGA_MAC_VER_27:
5206 case RTL_GIGA_MAC_VER_28:
5207 ops->disable = r8168dp_hw_jumbo_disable;
5208 ops->enable = r8168dp_hw_jumbo_enable;
5210 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5211 case RTL_GIGA_MAC_VER_32:
5212 case RTL_GIGA_MAC_VER_33:
5213 case RTL_GIGA_MAC_VER_34:
5214 ops->disable = r8168e_hw_jumbo_disable;
5215 ops->enable = r8168e_hw_jumbo_enable;
5219 * No action needed for jumbo frames with 8169.
5220 * No jumbo for 810x at all.
5222 case RTL_GIGA_MAC_VER_40:
5223 case RTL_GIGA_MAC_VER_41:
5224 case RTL_GIGA_MAC_VER_42:
5225 case RTL_GIGA_MAC_VER_43:
5226 case RTL_GIGA_MAC_VER_44:
5227 case RTL_GIGA_MAC_VER_45:
5228 case RTL_GIGA_MAC_VER_46:
5229 case RTL_GIGA_MAC_VER_47:
5230 case RTL_GIGA_MAC_VER_48:
5231 case RTL_GIGA_MAC_VER_49:
5232 case RTL_GIGA_MAC_VER_50:
5233 case RTL_GIGA_MAC_VER_51:
5235 ops->disable = NULL;
5241 DECLARE_RTL_COND(rtl_chipcmd_cond)
5243 return RTL_R8(tp, ChipCmd) & CmdReset;
5246 static void rtl_hw_reset(struct rtl8169_private *tp)
5248 RTL_W8(tp, ChipCmd, CmdReset);
5250 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
5253 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5255 struct rtl_fw *rtl_fw;
5259 name = rtl_lookup_firmware_name(tp);
5261 goto out_no_firmware;
5263 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5267 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
5271 rc = rtl_check_firmware(tp, rtl_fw);
5273 goto err_release_firmware;
5275 tp->rtl_fw = rtl_fw;
5279 err_release_firmware:
5280 release_firmware(rtl_fw->fw);
5284 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5291 static void rtl_request_firmware(struct rtl8169_private *tp)
5293 if (IS_ERR(tp->rtl_fw))
5294 rtl_request_uncached_firmware(tp);
5297 static void rtl_rx_close(struct rtl8169_private *tp)
5299 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
5302 DECLARE_RTL_COND(rtl_npq_cond)
5304 return RTL_R8(tp, TxPoll) & NPQ;
5307 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5309 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
5312 static void rtl8169_hw_reset(struct rtl8169_private *tp)
5314 /* Disable interrupts */
5315 rtl8169_irq_mask_and_ack(tp);
5319 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5320 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5321 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5322 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
5323 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
5324 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5325 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5326 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5327 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5328 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5329 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5330 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5331 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5332 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5333 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5334 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5335 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
5336 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5337 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5338 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5339 tp->mac_version == RTL_GIGA_MAC_VER_51) {
5340 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
5341 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
5343 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
5350 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
5352 /* Set DMA burst size and Interframe Gap Time */
5353 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
5354 (InterFrameGap << TxInterFrameGapShift));
5357 static void rtl_hw_start(struct net_device *dev)
5359 struct rtl8169_private *tp = netdev_priv(dev);
5363 rtl_irq_enable_all(tp);
5366 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
5369 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5370 * register to be written before TxDescAddrLow to work.
5371 * Switching from MMIO to I/O access fixes the issue as well.
5373 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5374 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5375 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5376 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
5379 static u16 rtl_rw_cpluscmd(struct rtl8169_private *tp)
5383 cmd = RTL_R16(tp, CPlusCmd);
5384 RTL_W16(tp, CPlusCmd, cmd);
5388 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
5390 /* Low hurts. Let's disable the filtering. */
5391 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
5394 static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
5396 static const struct rtl_cfg2_info {
5401 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5402 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5403 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5404 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
5406 const struct rtl_cfg2_info *p = cfg2_info;
5410 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
5411 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
5412 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5413 RTL_W32(tp, 0x7c, p->val);
5419 static void rtl_set_rx_mode(struct net_device *dev)
5421 struct rtl8169_private *tp = netdev_priv(dev);
5422 u32 mc_filter[2]; /* Multicast hash filter */
5426 if (dev->flags & IFF_PROMISC) {
5427 /* Unconditionally log net taps. */
5428 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5430 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5432 mc_filter[1] = mc_filter[0] = 0xffffffff;
5433 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5434 (dev->flags & IFF_ALLMULTI)) {
5435 /* Too many to filter perfectly -- accept all multicasts. */
5436 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5437 mc_filter[1] = mc_filter[0] = 0xffffffff;
5439 struct netdev_hw_addr *ha;
5441 rx_mode = AcceptBroadcast | AcceptMyPhys;
5442 mc_filter[1] = mc_filter[0] = 0;
5443 netdev_for_each_mc_addr(ha, dev) {
5444 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5445 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5446 rx_mode |= AcceptMulticast;
5450 if (dev->features & NETIF_F_RXALL)
5451 rx_mode |= (AcceptErr | AcceptRunt);
5453 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5455 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5456 u32 data = mc_filter[0];
5458 mc_filter[0] = swab32(mc_filter[1]);
5459 mc_filter[1] = swab32(data);
5462 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5463 mc_filter[1] = mc_filter[0] = 0xffffffff;
5465 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5466 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
5468 RTL_W32(tp, RxConfig, tmp);
5471 static void rtl_hw_start_8169(struct net_device *dev)
5473 struct rtl8169_private *tp = netdev_priv(dev);
5474 struct pci_dev *pdev = tp->pci_dev;
5476 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5477 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) | PCIMulRW);
5478 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5481 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5482 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5483 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5484 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5485 tp->mac_version == RTL_GIGA_MAC_VER_04)
5486 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5490 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
5492 rtl_set_rx_max_size(tp);
5494 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5495 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5496 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5497 tp->mac_version == RTL_GIGA_MAC_VER_04)
5498 rtl_set_rx_tx_config_registers(tp);
5500 tp->cp_cmd |= rtl_rw_cpluscmd(tp) | PCIMulRW;
5502 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5503 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5504 dprintk("Set MAC Reg C+CR Offset 0xe0. "
5505 "Bit-3 and bit-14 MUST be 1\n");
5506 tp->cp_cmd |= (1 << 14);
5509 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5511 rtl8169_set_magic_reg(tp, tp->mac_version);
5514 * Undocumented corner. Supposedly:
5515 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5517 RTL_W16(tp, IntrMitigate, 0x0000);
5519 rtl_set_rx_tx_desc_registers(tp);
5521 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5522 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5523 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5524 tp->mac_version != RTL_GIGA_MAC_VER_04) {
5525 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5526 rtl_set_rx_tx_config_registers(tp);
5529 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5531 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5532 RTL_R8(tp, IntrMask);
5534 RTL_W32(tp, RxMissed, 0);
5536 rtl_set_rx_mode(dev);
5538 /* no early-rx interrupts */
5539 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5542 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5544 if (tp->csi_ops.write)
5545 tp->csi_ops.write(tp, addr, value);
5548 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5550 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
5553 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
5557 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5558 rtl_csi_write(tp, 0x070c, csi | bits);
5561 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
5563 rtl_csi_access_enable(tp, 0x17000000);
5566 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
5568 rtl_csi_access_enable(tp, 0x27000000);
5571 DECLARE_RTL_COND(rtl_csiar_cond)
5573 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
5576 static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
5578 RTL_W32(tp, CSIDR, value);
5579 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5580 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5582 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5585 static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
5587 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) |
5588 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5590 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5591 RTL_R32(tp, CSIDR) : ~0;
5594 static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
5596 RTL_W32(tp, CSIDR, value);
5597 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5598 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5601 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5604 static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
5606 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5607 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5609 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5610 RTL_R32(tp, CSIDR) : ~0;
5613 static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5615 RTL_W32(tp, CSIDR, value);
5616 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5617 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5620 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5623 static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5625 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5626 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5628 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5629 RTL_R32(tp, CSIDR) : ~0;
5632 static void rtl_init_csi_ops(struct rtl8169_private *tp)
5634 struct csi_ops *ops = &tp->csi_ops;
5636 switch (tp->mac_version) {
5637 case RTL_GIGA_MAC_VER_01:
5638 case RTL_GIGA_MAC_VER_02:
5639 case RTL_GIGA_MAC_VER_03:
5640 case RTL_GIGA_MAC_VER_04:
5641 case RTL_GIGA_MAC_VER_05:
5642 case RTL_GIGA_MAC_VER_06:
5643 case RTL_GIGA_MAC_VER_10:
5644 case RTL_GIGA_MAC_VER_11:
5645 case RTL_GIGA_MAC_VER_12:
5646 case RTL_GIGA_MAC_VER_13:
5647 case RTL_GIGA_MAC_VER_14:
5648 case RTL_GIGA_MAC_VER_15:
5649 case RTL_GIGA_MAC_VER_16:
5650 case RTL_GIGA_MAC_VER_17:
5655 case RTL_GIGA_MAC_VER_37:
5656 case RTL_GIGA_MAC_VER_38:
5657 ops->write = r8402_csi_write;
5658 ops->read = r8402_csi_read;
5661 case RTL_GIGA_MAC_VER_44:
5662 ops->write = r8411_csi_write;
5663 ops->read = r8411_csi_read;
5667 ops->write = r8169_csi_write;
5668 ops->read = r8169_csi_read;
5674 unsigned int offset;
5679 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5685 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5686 rtl_ephy_write(tp, e->offset, w);
5691 static void rtl_disable_clock_request(struct rtl8169_private *tp)
5693 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
5694 PCI_EXP_LNKCTL_CLKREQ_EN);
5697 static void rtl_enable_clock_request(struct rtl8169_private *tp)
5699 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
5700 PCI_EXP_LNKCTL_CLKREQ_EN);
5703 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5707 data = RTL_R8(tp, Config3);
5712 data &= ~Rdy_to_L23;
5714 RTL_W8(tp, Config3, data);
5717 #define R8168_CPCMD_QUIRK_MASK (\
5728 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
5730 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5732 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5734 if (tp->dev->mtu <= ETH_DATA_LEN) {
5735 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
5736 PCI_EXP_DEVCTL_NOSNOOP_EN);
5740 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
5742 rtl_hw_start_8168bb(tp);
5744 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5746 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
5749 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
5751 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
5753 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5755 if (tp->dev->mtu <= ETH_DATA_LEN)
5756 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5758 rtl_disable_clock_request(tp);
5760 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5763 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
5765 static const struct ephy_info e_info_8168cp[] = {
5766 { 0x01, 0, 0x0001 },
5767 { 0x02, 0x0800, 0x1000 },
5768 { 0x03, 0, 0x0042 },
5769 { 0x06, 0x0080, 0x0000 },
5773 rtl_csi_access_enable_2(tp);
5775 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
5777 __rtl_hw_start_8168cp(tp);
5780 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
5782 rtl_csi_access_enable_2(tp);
5784 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5786 if (tp->dev->mtu <= ETH_DATA_LEN)
5787 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5789 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5792 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
5794 rtl_csi_access_enable_2(tp);
5796 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5799 RTL_W8(tp, DBG_REG, 0x20);
5801 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5803 if (tp->dev->mtu <= ETH_DATA_LEN)
5804 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5806 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5809 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
5811 static const struct ephy_info e_info_8168c_1[] = {
5812 { 0x02, 0x0800, 0x1000 },
5813 { 0x03, 0, 0x0002 },
5814 { 0x06, 0x0080, 0x0000 }
5817 rtl_csi_access_enable_2(tp);
5819 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5821 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
5823 __rtl_hw_start_8168cp(tp);
5826 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
5828 static const struct ephy_info e_info_8168c_2[] = {
5829 { 0x01, 0, 0x0001 },
5830 { 0x03, 0x0400, 0x0220 }
5833 rtl_csi_access_enable_2(tp);
5835 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
5837 __rtl_hw_start_8168cp(tp);
5840 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
5842 rtl_hw_start_8168c_2(tp);
5845 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
5847 rtl_csi_access_enable_2(tp);
5849 __rtl_hw_start_8168cp(tp);
5852 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
5854 rtl_csi_access_enable_2(tp);
5856 rtl_disable_clock_request(tp);
5858 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5860 if (tp->dev->mtu <= ETH_DATA_LEN)
5861 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5863 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5866 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
5868 rtl_csi_access_enable_1(tp);
5870 if (tp->dev->mtu <= ETH_DATA_LEN)
5871 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5873 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5875 rtl_disable_clock_request(tp);
5878 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
5880 static const struct ephy_info e_info_8168d_4[] = {
5881 { 0x0b, 0x0000, 0x0048 },
5882 { 0x19, 0x0020, 0x0050 },
5883 { 0x0c, 0x0100, 0x0020 }
5886 rtl_csi_access_enable_1(tp);
5888 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5890 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5892 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
5894 rtl_enable_clock_request(tp);
5897 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
5899 static const struct ephy_info e_info_8168e_1[] = {
5900 { 0x00, 0x0200, 0x0100 },
5901 { 0x00, 0x0000, 0x0004 },
5902 { 0x06, 0x0002, 0x0001 },
5903 { 0x06, 0x0000, 0x0030 },
5904 { 0x07, 0x0000, 0x2000 },
5905 { 0x00, 0x0000, 0x0020 },
5906 { 0x03, 0x5800, 0x2000 },
5907 { 0x03, 0x0000, 0x0001 },
5908 { 0x01, 0x0800, 0x1000 },
5909 { 0x07, 0x0000, 0x4000 },
5910 { 0x1e, 0x0000, 0x2000 },
5911 { 0x19, 0xffff, 0xfe6c },
5912 { 0x0a, 0x0000, 0x0040 }
5915 rtl_csi_access_enable_2(tp);
5917 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5919 if (tp->dev->mtu <= ETH_DATA_LEN)
5920 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5922 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5924 rtl_disable_clock_request(tp);
5926 /* Reset tx FIFO pointer */
5927 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5928 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
5930 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5933 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5935 static const struct ephy_info e_info_8168e_2[] = {
5936 { 0x09, 0x0000, 0x0080 },
5937 { 0x19, 0x0000, 0x0224 }
5940 rtl_csi_access_enable_1(tp);
5942 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5944 if (tp->dev->mtu <= ETH_DATA_LEN)
5945 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5947 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5948 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5949 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5950 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5951 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5952 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5953 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5954 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5956 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5958 rtl_disable_clock_request(tp);
5960 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5961 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5963 /* Adjust EEE LED frequency */
5964 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5966 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5967 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5968 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5971 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5973 rtl_csi_access_enable_2(tp);
5975 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5977 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5978 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5979 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5980 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5981 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5982 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5983 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5984 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5985 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5986 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5988 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5990 rtl_disable_clock_request(tp);
5992 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5993 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5994 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5995 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5996 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5999 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6001 static const struct ephy_info e_info_8168f_1[] = {
6002 { 0x06, 0x00c0, 0x0020 },
6003 { 0x08, 0x0001, 0x0002 },
6004 { 0x09, 0x0000, 0x0080 },
6005 { 0x19, 0x0000, 0x0224 }
6008 rtl_hw_start_8168f(tp);
6010 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
6012 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
6014 /* Adjust EEE LED frequency */
6015 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
6018 static void rtl_hw_start_8411(struct rtl8169_private *tp)
6020 static const struct ephy_info e_info_8168f_1[] = {
6021 { 0x06, 0x00c0, 0x0020 },
6022 { 0x0f, 0xffff, 0x5200 },
6023 { 0x1e, 0x0000, 0x4000 },
6024 { 0x19, 0x0000, 0x0224 }
6027 rtl_hw_start_8168f(tp);
6028 rtl_pcie_state_l2l3_enable(tp, false);
6030 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
6032 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
6035 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
6037 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6039 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6040 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6041 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6042 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6044 rtl_csi_access_enable_1(tp);
6046 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
6048 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6049 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6050 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
6052 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6053 RTL_W8(tp, MaxTxPacketSize, EarlySize);
6055 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6056 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6058 /* Adjust EEE LED frequency */
6059 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
6061 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6062 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6064 rtl_pcie_state_l2l3_enable(tp, false);
6067 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6069 static const struct ephy_info e_info_8168g_1[] = {
6070 { 0x00, 0x0000, 0x0008 },
6071 { 0x0c, 0x37d0, 0x0820 },
6072 { 0x1e, 0x0000, 0x0001 },
6073 { 0x19, 0x8000, 0x0000 }
6076 rtl_hw_start_8168g(tp);
6078 /* disable aspm and clock request before access ephy */
6079 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6080 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6081 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6084 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6086 static const struct ephy_info e_info_8168g_2[] = {
6087 { 0x00, 0x0000, 0x0008 },
6088 { 0x0c, 0x3df0, 0x0200 },
6089 { 0x19, 0xffff, 0xfc00 },
6090 { 0x1e, 0xffff, 0x20eb }
6093 rtl_hw_start_8168g(tp);
6095 /* disable aspm and clock request before access ephy */
6096 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6097 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6098 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6101 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6103 static const struct ephy_info e_info_8411_2[] = {
6104 { 0x00, 0x0000, 0x0008 },
6105 { 0x0c, 0x3df0, 0x0200 },
6106 { 0x0f, 0xffff, 0x5200 },
6107 { 0x19, 0x0020, 0x0000 },
6108 { 0x1e, 0x0000, 0x2000 }
6111 rtl_hw_start_8168g(tp);
6113 /* disable aspm and clock request before access ephy */
6114 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6115 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6116 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6119 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6123 static const struct ephy_info e_info_8168h_1[] = {
6124 { 0x1e, 0x0800, 0x0001 },
6125 { 0x1d, 0x0000, 0x0800 },
6126 { 0x05, 0xffff, 0x2089 },
6127 { 0x06, 0xffff, 0x5881 },
6128 { 0x04, 0xffff, 0x154a },
6129 { 0x01, 0xffff, 0x068b }
6132 /* disable aspm and clock request before access ephy */
6133 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6134 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6135 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6137 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6139 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6140 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6141 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6142 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6144 rtl_csi_access_enable_1(tp);
6146 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
6148 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6149 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6151 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
6153 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
6155 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6157 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6158 RTL_W8(tp, MaxTxPacketSize, EarlySize);
6160 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6161 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6163 /* Adjust EEE LED frequency */
6164 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
6166 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6167 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
6169 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
6171 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6173 rtl_pcie_state_l2l3_enable(tp, false);
6175 rtl_writephy(tp, 0x1f, 0x0c42);
6176 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
6177 rtl_writephy(tp, 0x1f, 0x0000);
6178 if (rg_saw_cnt > 0) {
6181 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6182 sw_cnt_1ms_ini &= 0x0fff;
6183 data = r8168_mac_ocp_read(tp, 0xd412);
6185 data |= sw_cnt_1ms_ini;
6186 r8168_mac_ocp_write(tp, 0xd412, data);
6189 data = r8168_mac_ocp_read(tp, 0xe056);
6192 r8168_mac_ocp_write(tp, 0xe056, data);
6194 data = r8168_mac_ocp_read(tp, 0xe052);
6197 r8168_mac_ocp_write(tp, 0xe052, data);
6199 data = r8168_mac_ocp_read(tp, 0xe0d6);
6202 r8168_mac_ocp_write(tp, 0xe0d6, data);
6204 data = r8168_mac_ocp_read(tp, 0xd420);
6207 r8168_mac_ocp_write(tp, 0xd420, data);
6209 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6210 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6211 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6212 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6215 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6217 rtl8168ep_stop_cmac(tp);
6219 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6221 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6222 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6223 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6224 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6226 rtl_csi_access_enable_1(tp);
6228 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
6230 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6231 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6233 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6235 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6237 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6238 RTL_W8(tp, MaxTxPacketSize, EarlySize);
6240 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6241 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6243 /* Adjust EEE LED frequency */
6244 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
6246 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6248 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
6250 rtl_pcie_state_l2l3_enable(tp, false);
6253 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6255 static const struct ephy_info e_info_8168ep_1[] = {
6256 { 0x00, 0xffff, 0x10ab },
6257 { 0x06, 0xffff, 0xf030 },
6258 { 0x08, 0xffff, 0x2006 },
6259 { 0x0d, 0xffff, 0x1666 },
6260 { 0x0c, 0x3ff0, 0x0000 }
6263 /* disable aspm and clock request before access ephy */
6264 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6265 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6266 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6268 rtl_hw_start_8168ep(tp);
6271 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6273 static const struct ephy_info e_info_8168ep_2[] = {
6274 { 0x00, 0xffff, 0x10a3 },
6275 { 0x19, 0xffff, 0xfc00 },
6276 { 0x1e, 0xffff, 0x20ea }
6279 /* disable aspm and clock request before access ephy */
6280 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6281 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6282 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6284 rtl_hw_start_8168ep(tp);
6286 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6287 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
6290 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6293 static const struct ephy_info e_info_8168ep_3[] = {
6294 { 0x00, 0xffff, 0x10a3 },
6295 { 0x19, 0xffff, 0x7c00 },
6296 { 0x1e, 0xffff, 0x20eb },
6297 { 0x0d, 0xffff, 0x1666 }
6300 /* disable aspm and clock request before access ephy */
6301 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6302 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6303 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6305 rtl_hw_start_8168ep(tp);
6307 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6308 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
6310 data = r8168_mac_ocp_read(tp, 0xd3e2);
6313 r8168_mac_ocp_write(tp, 0xd3e2, data);
6315 data = r8168_mac_ocp_read(tp, 0xd3e4);
6317 r8168_mac_ocp_write(tp, 0xd3e4, data);
6319 data = r8168_mac_ocp_read(tp, 0xe860);
6321 r8168_mac_ocp_write(tp, 0xe860, data);
6324 static void rtl_hw_start_8168(struct net_device *dev)
6326 struct rtl8169_private *tp = netdev_priv(dev);
6328 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
6330 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
6332 rtl_set_rx_max_size(tp);
6334 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) | PktCntrDisable | INTT_1;
6336 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
6338 RTL_W16(tp, IntrMitigate, 0x5151);
6340 /* Work around for RxFIFO overflow. */
6341 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
6342 tp->event_slow |= RxFIFOOver | PCSTimeout;
6343 tp->event_slow &= ~RxOverflow;
6346 rtl_set_rx_tx_desc_registers(tp);
6348 rtl_set_rx_tx_config_registers(tp);
6350 RTL_R8(tp, IntrMask);
6352 switch (tp->mac_version) {
6353 case RTL_GIGA_MAC_VER_11:
6354 rtl_hw_start_8168bb(tp);
6357 case RTL_GIGA_MAC_VER_12:
6358 case RTL_GIGA_MAC_VER_17:
6359 rtl_hw_start_8168bef(tp);
6362 case RTL_GIGA_MAC_VER_18:
6363 rtl_hw_start_8168cp_1(tp);
6366 case RTL_GIGA_MAC_VER_19:
6367 rtl_hw_start_8168c_1(tp);
6370 case RTL_GIGA_MAC_VER_20:
6371 rtl_hw_start_8168c_2(tp);
6374 case RTL_GIGA_MAC_VER_21:
6375 rtl_hw_start_8168c_3(tp);
6378 case RTL_GIGA_MAC_VER_22:
6379 rtl_hw_start_8168c_4(tp);
6382 case RTL_GIGA_MAC_VER_23:
6383 rtl_hw_start_8168cp_2(tp);
6386 case RTL_GIGA_MAC_VER_24:
6387 rtl_hw_start_8168cp_3(tp);
6390 case RTL_GIGA_MAC_VER_25:
6391 case RTL_GIGA_MAC_VER_26:
6392 case RTL_GIGA_MAC_VER_27:
6393 rtl_hw_start_8168d(tp);
6396 case RTL_GIGA_MAC_VER_28:
6397 rtl_hw_start_8168d_4(tp);
6400 case RTL_GIGA_MAC_VER_31:
6401 rtl_hw_start_8168dp(tp);
6404 case RTL_GIGA_MAC_VER_32:
6405 case RTL_GIGA_MAC_VER_33:
6406 rtl_hw_start_8168e_1(tp);
6408 case RTL_GIGA_MAC_VER_34:
6409 rtl_hw_start_8168e_2(tp);
6412 case RTL_GIGA_MAC_VER_35:
6413 case RTL_GIGA_MAC_VER_36:
6414 rtl_hw_start_8168f_1(tp);
6417 case RTL_GIGA_MAC_VER_38:
6418 rtl_hw_start_8411(tp);
6421 case RTL_GIGA_MAC_VER_40:
6422 case RTL_GIGA_MAC_VER_41:
6423 rtl_hw_start_8168g_1(tp);
6425 case RTL_GIGA_MAC_VER_42:
6426 rtl_hw_start_8168g_2(tp);
6429 case RTL_GIGA_MAC_VER_44:
6430 rtl_hw_start_8411_2(tp);
6433 case RTL_GIGA_MAC_VER_45:
6434 case RTL_GIGA_MAC_VER_46:
6435 rtl_hw_start_8168h_1(tp);
6438 case RTL_GIGA_MAC_VER_49:
6439 rtl_hw_start_8168ep_1(tp);
6442 case RTL_GIGA_MAC_VER_50:
6443 rtl_hw_start_8168ep_2(tp);
6446 case RTL_GIGA_MAC_VER_51:
6447 rtl_hw_start_8168ep_3(tp);
6451 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6452 dev->name, tp->mac_version);
6456 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
6458 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
6460 rtl_set_rx_mode(dev);
6462 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
6465 #define R810X_CPCMD_QUIRK_MASK (\
6476 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
6478 static const struct ephy_info e_info_8102e_1[] = {
6479 { 0x01, 0, 0x6e65 },
6480 { 0x02, 0, 0x091f },
6481 { 0x03, 0, 0xc2f9 },
6482 { 0x06, 0, 0xafb5 },
6483 { 0x07, 0, 0x0e00 },
6484 { 0x19, 0, 0xec80 },
6485 { 0x01, 0, 0x2e65 },
6490 rtl_csi_access_enable_2(tp);
6492 RTL_W8(tp, DBG_REG, FIX_NAK_1);
6494 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
6497 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6498 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
6500 cfg1 = RTL_R8(tp, Config1);
6501 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6502 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
6504 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
6507 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
6509 rtl_csi_access_enable_2(tp);
6511 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
6513 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6514 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
6517 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
6519 rtl_hw_start_8102e_2(tp);
6521 rtl_ephy_write(tp, 0x03, 0xc2f9);
6524 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
6526 static const struct ephy_info e_info_8105e_1[] = {
6527 { 0x07, 0, 0x4000 },
6528 { 0x19, 0, 0x0200 },
6529 { 0x19, 0, 0x0020 },
6530 { 0x1e, 0, 0x2000 },
6531 { 0x03, 0, 0x0001 },
6532 { 0x19, 0, 0x0100 },
6533 { 0x19, 0, 0x0004 },
6537 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6538 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
6540 /* Disable Early Tally Counter */
6541 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
6543 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6544 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
6546 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
6548 rtl_pcie_state_l2l3_enable(tp, false);
6551 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
6553 rtl_hw_start_8105e_1(tp);
6554 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
6557 static void rtl_hw_start_8402(struct rtl8169_private *tp)
6559 static const struct ephy_info e_info_8402[] = {
6560 { 0x19, 0xffff, 0xff64 },
6564 rtl_csi_access_enable_2(tp);
6566 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6567 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
6569 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6570 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
6572 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
6574 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
6576 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6577 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
6578 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6579 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6580 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6581 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6582 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
6584 rtl_pcie_state_l2l3_enable(tp, false);
6587 static void rtl_hw_start_8106(struct rtl8169_private *tp)
6589 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6590 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
6592 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6593 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6594 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6596 rtl_pcie_state_l2l3_enable(tp, false);
6599 static void rtl_hw_start_8101(struct net_device *dev)
6601 struct rtl8169_private *tp = netdev_priv(dev);
6602 struct pci_dev *pdev = tp->pci_dev;
6604 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6605 tp->event_slow &= ~RxFIFOOver;
6607 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
6608 tp->mac_version == RTL_GIGA_MAC_VER_16)
6609 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6610 PCI_EXP_DEVCTL_NOSNOOP_EN);
6612 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
6614 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
6616 rtl_set_rx_max_size(tp);
6618 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6619 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
6621 rtl_set_rx_tx_desc_registers(tp);
6623 rtl_set_rx_tx_config_registers(tp);
6625 switch (tp->mac_version) {
6626 case RTL_GIGA_MAC_VER_07:
6627 rtl_hw_start_8102e_1(tp);
6630 case RTL_GIGA_MAC_VER_08:
6631 rtl_hw_start_8102e_3(tp);
6634 case RTL_GIGA_MAC_VER_09:
6635 rtl_hw_start_8102e_2(tp);
6638 case RTL_GIGA_MAC_VER_29:
6639 rtl_hw_start_8105e_1(tp);
6641 case RTL_GIGA_MAC_VER_30:
6642 rtl_hw_start_8105e_2(tp);
6645 case RTL_GIGA_MAC_VER_37:
6646 rtl_hw_start_8402(tp);
6649 case RTL_GIGA_MAC_VER_39:
6650 rtl_hw_start_8106(tp);
6652 case RTL_GIGA_MAC_VER_43:
6653 rtl_hw_start_8168g_2(tp);
6655 case RTL_GIGA_MAC_VER_47:
6656 case RTL_GIGA_MAC_VER_48:
6657 rtl_hw_start_8168h_1(tp);
6661 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
6663 RTL_W16(tp, IntrMitigate, 0x0000);
6665 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
6667 rtl_set_rx_mode(dev);
6669 RTL_R8(tp, IntrMask);
6671 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
6674 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6676 struct rtl8169_private *tp = netdev_priv(dev);
6678 if (new_mtu > ETH_DATA_LEN)
6679 rtl_hw_jumbo_enable(tp);
6681 rtl_hw_jumbo_disable(tp);
6684 netdev_update_features(dev);
6689 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6691 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
6692 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6695 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6696 void **data_buff, struct RxDesc *desc)
6698 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6699 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
6703 rtl8169_make_unusable_by_asic(desc);
6706 static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
6708 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6710 /* Force memory writes to complete before releasing descriptor */
6713 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
6716 static inline void *rtl8169_align(void *data)
6718 return (void *)ALIGN((long)data, 16);
6721 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6722 struct RxDesc *desc)
6726 struct device *d = tp_to_dev(tp);
6727 int node = dev_to_node(d);
6729 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
6733 if (rtl8169_align(data) != data) {
6735 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
6740 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
6742 if (unlikely(dma_mapping_error(d, mapping))) {
6743 if (net_ratelimit())
6744 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
6748 desc->addr = cpu_to_le64(mapping);
6749 rtl8169_mark_to_asic(desc);
6757 static void rtl8169_rx_clear(struct rtl8169_private *tp)
6761 for (i = 0; i < NUM_RX_DESC; i++) {
6762 if (tp->Rx_databuff[i]) {
6763 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
6764 tp->RxDescArray + i);
6769 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
6771 desc->opts1 |= cpu_to_le32(RingEnd);
6774 static int rtl8169_rx_fill(struct rtl8169_private *tp)
6778 for (i = 0; i < NUM_RX_DESC; 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 rtl8169_private *tp)
6799 rtl8169_init_ring_indexes(tp);
6801 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6802 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
6804 return rtl8169_rx_fill(tp);
6807 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
6808 struct TxDesc *desc)
6810 unsigned int len = tx_skb->len;
6812 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6820 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6825 for (i = 0; i < n; i++) {
6826 unsigned int entry = (start + i) % NUM_TX_DESC;
6827 struct ring_info *tx_skb = tp->tx_skb + entry;
6828 unsigned int len = tx_skb->len;
6831 struct sk_buff *skb = tx_skb->skb;
6833 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
6834 tp->TxDescArray + entry);
6836 dev_consume_skb_any(skb);
6843 static void rtl8169_tx_clear(struct rtl8169_private *tp)
6845 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
6846 tp->cur_tx = tp->dirty_tx = 0;
6849 static void rtl_reset_work(struct rtl8169_private *tp)
6851 struct net_device *dev = tp->dev;
6854 napi_disable(&tp->napi);
6855 netif_stop_queue(dev);
6856 synchronize_sched();
6858 rtl8169_hw_reset(tp);
6860 for (i = 0; i < NUM_RX_DESC; i++)
6861 rtl8169_mark_to_asic(tp->RxDescArray + i);
6863 rtl8169_tx_clear(tp);
6864 rtl8169_init_ring_indexes(tp);
6866 napi_enable(&tp->napi);
6868 netif_wake_queue(dev);
6869 rtl8169_check_link_status(dev, tp);
6872 static void rtl8169_tx_timeout(struct net_device *dev)
6874 struct rtl8169_private *tp = netdev_priv(dev);
6876 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6879 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
6882 struct skb_shared_info *info = skb_shinfo(skb);
6883 unsigned int cur_frag, entry;
6884 struct TxDesc *uninitialized_var(txd);
6885 struct device *d = tp_to_dev(tp);
6888 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
6889 const skb_frag_t *frag = info->frags + cur_frag;
6894 entry = (entry + 1) % NUM_TX_DESC;
6896 txd = tp->TxDescArray + entry;
6897 len = skb_frag_size(frag);
6898 addr = skb_frag_address(frag);
6899 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
6900 if (unlikely(dma_mapping_error(d, mapping))) {
6901 if (net_ratelimit())
6902 netif_err(tp, drv, tp->dev,
6903 "Failed to map TX fragments DMA!\n");
6907 /* Anti gcc 2.95.3 bugware (sic) */
6908 status = opts[0] | len |
6909 (RingEnd * !((entry + 1) % NUM_TX_DESC));
6911 txd->opts1 = cpu_to_le32(status);
6912 txd->opts2 = cpu_to_le32(opts[1]);
6913 txd->addr = cpu_to_le64(mapping);
6915 tp->tx_skb[entry].len = len;
6919 tp->tx_skb[entry].skb = skb;
6920 txd->opts1 |= cpu_to_le32(LastFrag);
6926 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6930 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6932 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6935 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6936 struct net_device *dev);
6937 /* r8169_csum_workaround()
6938 * The hw limites the value the transport offset. When the offset is out of the
6939 * range, calculate the checksum by sw.
6941 static void r8169_csum_workaround(struct rtl8169_private *tp,
6942 struct sk_buff *skb)
6944 if (skb_shinfo(skb)->gso_size) {
6945 netdev_features_t features = tp->dev->features;
6946 struct sk_buff *segs, *nskb;
6948 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6949 segs = skb_gso_segment(skb, features);
6950 if (IS_ERR(segs) || !segs)
6957 rtl8169_start_xmit(nskb, tp->dev);
6960 dev_consume_skb_any(skb);
6961 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6962 if (skb_checksum_help(skb) < 0)
6965 rtl8169_start_xmit(skb, tp->dev);
6967 struct net_device_stats *stats;
6970 stats = &tp->dev->stats;
6971 stats->tx_dropped++;
6972 dev_kfree_skb_any(skb);
6976 /* msdn_giant_send_check()
6977 * According to the document of microsoft, the TCP Pseudo Header excludes the
6978 * packet length for IPv6 TCP large packets.
6980 static int msdn_giant_send_check(struct sk_buff *skb)
6982 const struct ipv6hdr *ipv6h;
6986 ret = skb_cow_head(skb, 0);
6990 ipv6h = ipv6_hdr(skb);
6994 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6999 static inline __be16 get_protocol(struct sk_buff *skb)
7003 if (skb->protocol == htons(ETH_P_8021Q))
7004 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7006 protocol = skb->protocol;
7011 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7012 struct sk_buff *skb, u32 *opts)
7014 u32 mss = skb_shinfo(skb)->gso_size;
7018 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7019 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7020 const struct iphdr *ip = ip_hdr(skb);
7022 if (ip->protocol == IPPROTO_TCP)
7023 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7024 else if (ip->protocol == IPPROTO_UDP)
7025 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7033 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7034 struct sk_buff *skb, u32 *opts)
7036 u32 transport_offset = (u32)skb_transport_offset(skb);
7037 u32 mss = skb_shinfo(skb)->gso_size;
7040 if (transport_offset > GTTCPHO_MAX) {
7041 netif_warn(tp, tx_err, tp->dev,
7042 "Invalid transport offset 0x%x for TSO\n",
7047 switch (get_protocol(skb)) {
7048 case htons(ETH_P_IP):
7049 opts[0] |= TD1_GTSENV4;
7052 case htons(ETH_P_IPV6):
7053 if (msdn_giant_send_check(skb))
7056 opts[0] |= TD1_GTSENV6;
7064 opts[0] |= transport_offset << GTTCPHO_SHIFT;
7065 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
7066 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7069 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7070 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
7072 if (transport_offset > TCPHO_MAX) {
7073 netif_warn(tp, tx_err, tp->dev,
7074 "Invalid transport offset 0x%x\n",
7079 switch (get_protocol(skb)) {
7080 case htons(ETH_P_IP):
7081 opts[1] |= TD1_IPv4_CS;
7082 ip_protocol = ip_hdr(skb)->protocol;
7085 case htons(ETH_P_IPV6):
7086 opts[1] |= TD1_IPv6_CS;
7087 ip_protocol = ipv6_hdr(skb)->nexthdr;
7091 ip_protocol = IPPROTO_RAW;
7095 if (ip_protocol == IPPROTO_TCP)
7096 opts[1] |= TD1_TCP_CS;
7097 else if (ip_protocol == IPPROTO_UDP)
7098 opts[1] |= TD1_UDP_CS;
7102 opts[1] |= transport_offset << TCPHO_SHIFT;
7104 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7105 return !eth_skb_pad(skb);
7111 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7112 struct net_device *dev)
7114 struct rtl8169_private *tp = netdev_priv(dev);
7115 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
7116 struct TxDesc *txd = tp->TxDescArray + entry;
7117 struct device *d = tp_to_dev(tp);
7123 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
7124 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
7128 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
7131 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7134 if (!tp->tso_csum(tp, skb, opts)) {
7135 r8169_csum_workaround(tp, skb);
7136 return NETDEV_TX_OK;
7139 len = skb_headlen(skb);
7140 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
7141 if (unlikely(dma_mapping_error(d, mapping))) {
7142 if (net_ratelimit())
7143 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
7147 tp->tx_skb[entry].len = len;
7148 txd->addr = cpu_to_le64(mapping);
7150 frags = rtl8169_xmit_frags(tp, skb, opts);
7154 opts[0] |= FirstFrag;
7156 opts[0] |= FirstFrag | LastFrag;
7157 tp->tx_skb[entry].skb = skb;
7160 txd->opts2 = cpu_to_le32(opts[1]);
7162 skb_tx_timestamp(skb);
7164 /* Force memory writes to complete before releasing descriptor */
7167 /* Anti gcc 2.95.3 bugware (sic) */
7168 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
7169 txd->opts1 = cpu_to_le32(status);
7171 /* Force all memory writes to complete before notifying device */
7174 tp->cur_tx += frags + 1;
7176 RTL_W8(tp, TxPoll, NPQ);
7180 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
7181 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7182 * not miss a ring update when it notices a stopped queue.
7185 netif_stop_queue(dev);
7186 /* Sync with rtl_tx:
7187 * - publish queue status and cur_tx ring index (write barrier)
7188 * - refresh dirty_tx ring index (read barrier).
7189 * May the current thread have a pessimistic view of the ring
7190 * status and forget to wake up queue, a racing rtl_tx thread
7194 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
7195 netif_wake_queue(dev);
7198 return NETDEV_TX_OK;
7201 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
7203 dev_kfree_skb_any(skb);
7204 dev->stats.tx_dropped++;
7205 return NETDEV_TX_OK;
7208 netif_stop_queue(dev);
7209 dev->stats.tx_dropped++;
7210 return NETDEV_TX_BUSY;
7213 static void rtl8169_pcierr_interrupt(struct net_device *dev)
7215 struct rtl8169_private *tp = netdev_priv(dev);
7216 struct pci_dev *pdev = tp->pci_dev;
7217 u16 pci_status, pci_cmd;
7219 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7220 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7222 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7223 pci_cmd, pci_status);
7226 * The recovery sequence below admits a very elaborated explanation:
7227 * - it seems to work;
7228 * - I did not see what else could be done;
7229 * - it makes iop3xx happy.
7231 * Feel free to adjust to your needs.
7233 if (pdev->broken_parity_status)
7234 pci_cmd &= ~PCI_COMMAND_PARITY;
7236 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7238 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
7240 pci_write_config_word(pdev, PCI_STATUS,
7241 pci_status & (PCI_STATUS_DETECTED_PARITY |
7242 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7243 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7245 /* The infamous DAC f*ckup only happens at boot time */
7246 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
7247 netif_info(tp, intr, dev, "disabling PCI DAC\n");
7248 tp->cp_cmd &= ~PCIDAC;
7249 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
7250 dev->features &= ~NETIF_F_HIGHDMA;
7253 rtl8169_hw_reset(tp);
7255 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7258 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
7260 unsigned int dirty_tx, tx_left;
7262 dirty_tx = tp->dirty_tx;
7264 tx_left = tp->cur_tx - dirty_tx;
7266 while (tx_left > 0) {
7267 unsigned int entry = dirty_tx % NUM_TX_DESC;
7268 struct ring_info *tx_skb = tp->tx_skb + entry;
7271 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7272 if (status & DescOwn)
7275 /* This barrier is needed to keep us from reading
7276 * any other fields out of the Tx descriptor until
7277 * we know the status of DescOwn
7281 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
7282 tp->TxDescArray + entry);
7283 if (status & LastFrag) {
7284 u64_stats_update_begin(&tp->tx_stats.syncp);
7285 tp->tx_stats.packets++;
7286 tp->tx_stats.bytes += tx_skb->skb->len;
7287 u64_stats_update_end(&tp->tx_stats.syncp);
7288 dev_consume_skb_any(tx_skb->skb);
7295 if (tp->dirty_tx != dirty_tx) {
7296 tp->dirty_tx = dirty_tx;
7297 /* Sync with rtl8169_start_xmit:
7298 * - publish dirty_tx ring index (write barrier)
7299 * - refresh cur_tx ring index and queue status (read barrier)
7300 * May the current thread miss the stopped queue condition,
7301 * a racing xmit thread can only have a right view of the
7305 if (netif_queue_stopped(dev) &&
7306 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
7307 netif_wake_queue(dev);
7310 * 8168 hack: TxPoll requests are lost when the Tx packets are
7311 * too close. Let's kick an extra TxPoll request when a burst
7312 * of start_xmit activity is detected (if it is not detected,
7313 * it is slow enough). -- FR
7315 if (tp->cur_tx != dirty_tx)
7316 RTL_W8(tp, TxPoll, NPQ);
7320 static inline int rtl8169_fragmented_frame(u32 status)
7322 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7325 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
7327 u32 status = opts1 & RxProtoMask;
7329 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
7330 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
7331 skb->ip_summed = CHECKSUM_UNNECESSARY;
7333 skb_checksum_none_assert(skb);
7336 static struct sk_buff *rtl8169_try_rx_copy(void *data,
7337 struct rtl8169_private *tp,
7341 struct sk_buff *skb;
7342 struct device *d = tp_to_dev(tp);
7344 data = rtl8169_align(data);
7345 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
7347 skb = napi_alloc_skb(&tp->napi, pkt_size);
7349 skb_copy_to_linear_data(skb, data, pkt_size);
7350 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7355 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
7357 unsigned int cur_rx, rx_left;
7360 cur_rx = tp->cur_rx;
7362 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
7363 unsigned int entry = cur_rx % NUM_RX_DESC;
7364 struct RxDesc *desc = tp->RxDescArray + entry;
7367 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
7368 if (status & DescOwn)
7371 /* This barrier is needed to keep us from reading
7372 * any other fields out of the Rx descriptor until
7373 * we know the status of DescOwn
7377 if (unlikely(status & RxRES)) {
7378 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7380 dev->stats.rx_errors++;
7381 if (status & (RxRWT | RxRUNT))
7382 dev->stats.rx_length_errors++;
7384 dev->stats.rx_crc_errors++;
7385 if (status & RxFOVF) {
7386 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7387 dev->stats.rx_fifo_errors++;
7389 if ((status & (RxRUNT | RxCRC)) &&
7390 !(status & (RxRWT | RxFOVF)) &&
7391 (dev->features & NETIF_F_RXALL))
7394 struct sk_buff *skb;
7399 addr = le64_to_cpu(desc->addr);
7400 if (likely(!(dev->features & NETIF_F_RXFCS)))
7401 pkt_size = (status & 0x00003fff) - 4;
7403 pkt_size = status & 0x00003fff;
7406 * The driver does not support incoming fragmented
7407 * frames. They are seen as a symptom of over-mtu
7410 if (unlikely(rtl8169_fragmented_frame(status))) {
7411 dev->stats.rx_dropped++;
7412 dev->stats.rx_length_errors++;
7413 goto release_descriptor;
7416 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7417 tp, pkt_size, addr);
7419 dev->stats.rx_dropped++;
7420 goto release_descriptor;
7423 rtl8169_rx_csum(skb, status);
7424 skb_put(skb, pkt_size);
7425 skb->protocol = eth_type_trans(skb, dev);
7427 rtl8169_rx_vlan_tag(desc, skb);
7429 if (skb->pkt_type == PACKET_MULTICAST)
7430 dev->stats.multicast++;
7432 napi_gro_receive(&tp->napi, skb);
7434 u64_stats_update_begin(&tp->rx_stats.syncp);
7435 tp->rx_stats.packets++;
7436 tp->rx_stats.bytes += pkt_size;
7437 u64_stats_update_end(&tp->rx_stats.syncp);
7441 rtl8169_mark_to_asic(desc);
7444 count = cur_rx - tp->cur_rx;
7445 tp->cur_rx = cur_rx;
7450 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
7452 struct net_device *dev = dev_instance;
7453 struct rtl8169_private *tp = netdev_priv(dev);
7457 status = rtl_get_events(tp);
7458 if (status && status != 0xffff) {
7459 status &= RTL_EVENT_NAPI | tp->event_slow;
7463 rtl_irq_disable(tp);
7464 napi_schedule_irqoff(&tp->napi);
7467 return IRQ_RETVAL(handled);
7471 * Workqueue context.
7473 static void rtl_slow_event_work(struct rtl8169_private *tp)
7475 struct net_device *dev = tp->dev;
7478 status = rtl_get_events(tp) & tp->event_slow;
7479 rtl_ack_events(tp, status);
7481 if (unlikely(status & RxFIFOOver)) {
7482 switch (tp->mac_version) {
7483 /* Work around for rx fifo overflow */
7484 case RTL_GIGA_MAC_VER_11:
7485 netif_stop_queue(dev);
7486 /* XXX - Hack alert. See rtl_task(). */
7487 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
7493 if (unlikely(status & SYSErr))
7494 rtl8169_pcierr_interrupt(dev);
7496 if (status & LinkChg)
7497 rtl8169_check_link_status(dev, tp);
7499 rtl_irq_enable_all(tp);
7502 static void rtl_task(struct work_struct *work)
7504 static const struct {
7506 void (*action)(struct rtl8169_private *);
7508 /* XXX - keep rtl_slow_event_work() as first element. */
7509 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7510 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7511 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7513 struct rtl8169_private *tp =
7514 container_of(work, struct rtl8169_private, wk.work);
7515 struct net_device *dev = tp->dev;
7520 if (!netif_running(dev) ||
7521 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
7524 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7527 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
7529 rtl_work[i].action(tp);
7533 rtl_unlock_work(tp);
7536 static int rtl8169_poll(struct napi_struct *napi, int budget)
7538 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7539 struct net_device *dev = tp->dev;
7540 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7544 status = rtl_get_events(tp);
7545 rtl_ack_events(tp, status & ~tp->event_slow);
7547 if (status & RTL_EVENT_NAPI_RX)
7548 work_done = rtl_rx(dev, tp, (u32) budget);
7550 if (status & RTL_EVENT_NAPI_TX)
7553 if (status & tp->event_slow) {
7554 enable_mask &= ~tp->event_slow;
7556 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7559 if (work_done < budget) {
7560 napi_complete_done(napi, work_done);
7562 rtl_irq_enable(tp, enable_mask);
7569 static void rtl8169_rx_missed(struct net_device *dev)
7571 struct rtl8169_private *tp = netdev_priv(dev);
7573 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7576 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7577 RTL_W32(tp, RxMissed, 0);
7580 static void rtl8169_down(struct net_device *dev)
7582 struct rtl8169_private *tp = netdev_priv(dev);
7584 del_timer_sync(&tp->timer);
7586 napi_disable(&tp->napi);
7587 netif_stop_queue(dev);
7589 rtl8169_hw_reset(tp);
7591 * At this point device interrupts can not be enabled in any function,
7592 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7593 * and napi is disabled (rtl8169_poll).
7595 rtl8169_rx_missed(dev);
7597 /* Give a racing hard_start_xmit a few cycles to complete. */
7598 synchronize_sched();
7600 rtl8169_tx_clear(tp);
7602 rtl8169_rx_clear(tp);
7604 rtl_pll_power_down(tp);
7607 static int rtl8169_close(struct net_device *dev)
7609 struct rtl8169_private *tp = netdev_priv(dev);
7610 struct pci_dev *pdev = tp->pci_dev;
7612 pm_runtime_get_sync(&pdev->dev);
7614 /* Update counters before going down */
7615 rtl8169_update_counters(dev);
7618 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7621 rtl_unlock_work(tp);
7623 cancel_work_sync(&tp->wk.work);
7625 pci_free_irq(pdev, 0, dev);
7627 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7629 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7631 tp->TxDescArray = NULL;
7632 tp->RxDescArray = NULL;
7634 pm_runtime_put_sync(&pdev->dev);
7639 #ifdef CONFIG_NET_POLL_CONTROLLER
7640 static void rtl8169_netpoll(struct net_device *dev)
7642 struct rtl8169_private *tp = netdev_priv(dev);
7644 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
7648 static int rtl_open(struct net_device *dev)
7650 struct rtl8169_private *tp = netdev_priv(dev);
7651 struct pci_dev *pdev = tp->pci_dev;
7652 int retval = -ENOMEM;
7654 pm_runtime_get_sync(&pdev->dev);
7657 * Rx and Tx descriptors needs 256 bytes alignment.
7658 * dma_alloc_coherent provides more.
7660 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7661 &tp->TxPhyAddr, GFP_KERNEL);
7662 if (!tp->TxDescArray)
7663 goto err_pm_runtime_put;
7665 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7666 &tp->RxPhyAddr, GFP_KERNEL);
7667 if (!tp->RxDescArray)
7670 retval = rtl8169_init_ring(tp);
7674 INIT_WORK(&tp->wk.work, rtl_task);
7678 rtl_request_firmware(tp);
7680 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, dev,
7683 goto err_release_fw_2;
7687 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7689 napi_enable(&tp->napi);
7691 rtl8169_init_phy(dev, tp);
7693 __rtl8169_set_features(dev, dev->features);
7695 rtl_pll_power_up(tp);
7699 if (!rtl8169_init_counter_offsets(dev))
7700 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7702 netif_start_queue(dev);
7704 rtl_unlock_work(tp);
7706 tp->saved_wolopts = 0;
7707 pm_runtime_put_sync(&pdev->dev);
7709 rtl8169_check_link_status(dev, tp);
7714 rtl_release_firmware(tp);
7715 rtl8169_rx_clear(tp);
7717 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7719 tp->RxDescArray = NULL;
7721 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7723 tp->TxDescArray = NULL;
7725 pm_runtime_put_noidle(&pdev->dev);
7730 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7732 struct rtl8169_private *tp = netdev_priv(dev);
7733 struct pci_dev *pdev = tp->pci_dev;
7734 struct rtl8169_counters *counters = tp->counters;
7737 pm_runtime_get_noresume(&pdev->dev);
7739 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
7740 rtl8169_rx_missed(dev);
7743 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
7744 stats->rx_packets = tp->rx_stats.packets;
7745 stats->rx_bytes = tp->rx_stats.bytes;
7746 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
7749 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
7750 stats->tx_packets = tp->tx_stats.packets;
7751 stats->tx_bytes = tp->tx_stats.bytes;
7752 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
7754 stats->rx_dropped = dev->stats.rx_dropped;
7755 stats->tx_dropped = dev->stats.tx_dropped;
7756 stats->rx_length_errors = dev->stats.rx_length_errors;
7757 stats->rx_errors = dev->stats.rx_errors;
7758 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7759 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7760 stats->rx_missed_errors = dev->stats.rx_missed_errors;
7761 stats->multicast = dev->stats.multicast;
7764 * Fetch additonal counter values missing in stats collected by driver
7765 * from tally counters.
7767 if (pm_runtime_active(&pdev->dev))
7768 rtl8169_update_counters(dev);
7771 * Subtract values fetched during initalization.
7772 * See rtl8169_init_counter_offsets for a description why we do that.
7774 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
7775 le64_to_cpu(tp->tc_offset.tx_errors);
7776 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
7777 le32_to_cpu(tp->tc_offset.tx_multi_collision);
7778 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
7779 le16_to_cpu(tp->tc_offset.tx_aborted);
7781 pm_runtime_put_noidle(&pdev->dev);
7784 static void rtl8169_net_suspend(struct net_device *dev)
7786 struct rtl8169_private *tp = netdev_priv(dev);
7788 if (!netif_running(dev))
7791 netif_device_detach(dev);
7792 netif_stop_queue(dev);
7795 napi_disable(&tp->napi);
7796 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7797 rtl_unlock_work(tp);
7799 rtl_pll_power_down(tp);
7804 static int rtl8169_suspend(struct device *device)
7806 struct pci_dev *pdev = to_pci_dev(device);
7807 struct net_device *dev = pci_get_drvdata(pdev);
7809 rtl8169_net_suspend(dev);
7814 static void __rtl8169_resume(struct net_device *dev)
7816 struct rtl8169_private *tp = netdev_priv(dev);
7818 netif_device_attach(dev);
7820 rtl_pll_power_up(tp);
7823 napi_enable(&tp->napi);
7824 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7825 rtl_unlock_work(tp);
7827 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7830 static int rtl8169_resume(struct device *device)
7832 struct pci_dev *pdev = to_pci_dev(device);
7833 struct net_device *dev = pci_get_drvdata(pdev);
7834 struct rtl8169_private *tp = netdev_priv(dev);
7836 rtl8169_init_phy(dev, tp);
7838 if (netif_running(dev))
7839 __rtl8169_resume(dev);
7844 static int rtl8169_runtime_suspend(struct device *device)
7846 struct pci_dev *pdev = to_pci_dev(device);
7847 struct net_device *dev = pci_get_drvdata(pdev);
7848 struct rtl8169_private *tp = netdev_priv(dev);
7850 if (!tp->TxDescArray) {
7851 rtl_pll_power_down(tp);
7856 tp->saved_wolopts = __rtl8169_get_wol(tp);
7857 __rtl8169_set_wol(tp, WAKE_ANY);
7858 rtl_unlock_work(tp);
7860 rtl8169_net_suspend(dev);
7862 /* Update counters before going runtime suspend */
7863 rtl8169_rx_missed(dev);
7864 rtl8169_update_counters(dev);
7869 static int rtl8169_runtime_resume(struct device *device)
7871 struct pci_dev *pdev = to_pci_dev(device);
7872 struct net_device *dev = pci_get_drvdata(pdev);
7873 struct rtl8169_private *tp = netdev_priv(dev);
7874 rtl_rar_set(tp, dev->dev_addr);
7876 if (!tp->TxDescArray)
7880 __rtl8169_set_wol(tp, tp->saved_wolopts);
7881 tp->saved_wolopts = 0;
7882 rtl_unlock_work(tp);
7884 rtl8169_init_phy(dev, tp);
7886 __rtl8169_resume(dev);
7891 static int rtl8169_runtime_idle(struct device *device)
7893 struct pci_dev *pdev = to_pci_dev(device);
7894 struct net_device *dev = pci_get_drvdata(pdev);
7896 if (!netif_running(dev) || !netif_carrier_ok(dev))
7897 pm_schedule_suspend(device, 10000);
7902 static const struct dev_pm_ops rtl8169_pm_ops = {
7903 .suspend = rtl8169_suspend,
7904 .resume = rtl8169_resume,
7905 .freeze = rtl8169_suspend,
7906 .thaw = rtl8169_resume,
7907 .poweroff = rtl8169_suspend,
7908 .restore = rtl8169_resume,
7909 .runtime_suspend = rtl8169_runtime_suspend,
7910 .runtime_resume = rtl8169_runtime_resume,
7911 .runtime_idle = rtl8169_runtime_idle,
7914 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
7916 #else /* !CONFIG_PM */
7918 #define RTL8169_PM_OPS NULL
7920 #endif /* !CONFIG_PM */
7922 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7924 /* WoL fails with 8168b when the receiver is disabled. */
7925 switch (tp->mac_version) {
7926 case RTL_GIGA_MAC_VER_11:
7927 case RTL_GIGA_MAC_VER_12:
7928 case RTL_GIGA_MAC_VER_17:
7929 pci_clear_master(tp->pci_dev);
7931 RTL_W8(tp, ChipCmd, CmdRxEnb);
7933 RTL_R8(tp, ChipCmd);
7940 static void rtl_shutdown(struct pci_dev *pdev)
7942 struct net_device *dev = pci_get_drvdata(pdev);
7943 struct rtl8169_private *tp = netdev_priv(dev);
7945 rtl8169_net_suspend(dev);
7947 /* Restore original MAC address */
7948 rtl_rar_set(tp, dev->perm_addr);
7950 rtl8169_hw_reset(tp);
7952 if (system_state == SYSTEM_POWER_OFF) {
7953 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7954 rtl_wol_suspend_quirk(tp);
7955 rtl_wol_shutdown_quirk(tp);
7958 pci_wake_from_d3(pdev, true);
7959 pci_set_power_state(pdev, PCI_D3hot);
7963 static void rtl_remove_one(struct pci_dev *pdev)
7965 struct net_device *dev = pci_get_drvdata(pdev);
7966 struct rtl8169_private *tp = netdev_priv(dev);
7968 if (r8168_check_dash(tp))
7969 rtl8168_driver_stop(tp);
7971 netif_napi_del(&tp->napi);
7973 unregister_netdev(dev);
7975 rtl_release_firmware(tp);
7977 if (pci_dev_run_wake(pdev))
7978 pm_runtime_get_noresume(&pdev->dev);
7980 /* restore original MAC address */
7981 rtl_rar_set(tp, dev->perm_addr);
7984 static const struct net_device_ops rtl_netdev_ops = {
7985 .ndo_open = rtl_open,
7986 .ndo_stop = rtl8169_close,
7987 .ndo_get_stats64 = rtl8169_get_stats64,
7988 .ndo_start_xmit = rtl8169_start_xmit,
7989 .ndo_tx_timeout = rtl8169_tx_timeout,
7990 .ndo_validate_addr = eth_validate_addr,
7991 .ndo_change_mtu = rtl8169_change_mtu,
7992 .ndo_fix_features = rtl8169_fix_features,
7993 .ndo_set_features = rtl8169_set_features,
7994 .ndo_set_mac_address = rtl_set_mac_address,
7995 .ndo_do_ioctl = rtl8169_ioctl,
7996 .ndo_set_rx_mode = rtl_set_rx_mode,
7997 #ifdef CONFIG_NET_POLL_CONTROLLER
7998 .ndo_poll_controller = rtl8169_netpoll,
8003 static const struct rtl_cfg_info {
8004 void (*hw_start)(struct net_device *);
8005 unsigned int region;
8007 unsigned int has_gmii:1;
8008 const struct rtl_coalesce_info *coalesce_info;
8010 } rtl_cfg_infos [] = {
8012 .hw_start = rtl_hw_start_8169,
8014 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8016 .coalesce_info = rtl_coalesce_info_8169,
8017 .default_ver = RTL_GIGA_MAC_VER_01,
8020 .hw_start = rtl_hw_start_8168,
8022 .event_slow = SYSErr | LinkChg | RxOverflow,
8024 .coalesce_info = rtl_coalesce_info_8168_8136,
8025 .default_ver = RTL_GIGA_MAC_VER_11,
8028 .hw_start = rtl_hw_start_8101,
8030 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8032 .coalesce_info = rtl_coalesce_info_8168_8136,
8033 .default_ver = RTL_GIGA_MAC_VER_13,
8037 static int rtl_alloc_irq(struct rtl8169_private *tp)
8041 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
8042 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
8043 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
8044 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
8045 flags = PCI_IRQ_LEGACY;
8047 flags = PCI_IRQ_ALL_TYPES;
8050 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
8053 DECLARE_RTL_COND(rtl_link_list_ready_cond)
8055 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
8058 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8060 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8063 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
8067 tp->ocp_base = OCP_STD_PHY_BASE;
8069 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
8071 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8074 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8077 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8079 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
8081 data = r8168_mac_ocp_read(tp, 0xe8de);
8083 r8168_mac_ocp_write(tp, 0xe8de, data);
8085 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8088 data = r8168_mac_ocp_read(tp, 0xe8de);
8090 r8168_mac_ocp_write(tp, 0xe8de, data);
8092 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8096 static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8098 rtl8168ep_stop_cmac(tp);
8099 rtl_hw_init_8168g(tp);
8102 static void rtl_hw_initialize(struct rtl8169_private *tp)
8104 switch (tp->mac_version) {
8105 case RTL_GIGA_MAC_VER_40:
8106 case RTL_GIGA_MAC_VER_41:
8107 case RTL_GIGA_MAC_VER_42:
8108 case RTL_GIGA_MAC_VER_43:
8109 case RTL_GIGA_MAC_VER_44:
8110 case RTL_GIGA_MAC_VER_45:
8111 case RTL_GIGA_MAC_VER_46:
8112 case RTL_GIGA_MAC_VER_47:
8113 case RTL_GIGA_MAC_VER_48:
8114 rtl_hw_init_8168g(tp);
8116 case RTL_GIGA_MAC_VER_49:
8117 case RTL_GIGA_MAC_VER_50:
8118 case RTL_GIGA_MAC_VER_51:
8119 rtl_hw_init_8168ep(tp);
8126 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8128 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8129 const unsigned int region = cfg->region;
8130 struct rtl8169_private *tp;
8131 struct mii_if_info *mii;
8132 struct net_device *dev;
8136 if (netif_msg_drv(&debug)) {
8137 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8138 MODULENAME, RTL8169_VERSION);
8141 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8145 SET_NETDEV_DEV(dev, &pdev->dev);
8146 dev->netdev_ops = &rtl_netdev_ops;
8147 tp = netdev_priv(dev);
8150 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8154 mii->mdio_read = rtl_mdio_read;
8155 mii->mdio_write = rtl_mdio_write;
8156 mii->phy_id_mask = 0x1f;
8157 mii->reg_num_mask = 0x1f;
8158 mii->supports_gmii = cfg->has_gmii;
8160 /* disable ASPM completely as that cause random device stop working
8161 * problems as well as full system hangs for some PCIe devices users */
8162 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8163 PCIE_LINK_STATE_CLKPM);
8165 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8166 rc = pcim_enable_device(pdev);
8168 netif_err(tp, probe, dev, "enable failure\n");
8172 if (pcim_set_mwi(pdev) < 0)
8173 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8175 /* make sure PCI base addr 1 is MMIO */
8176 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8177 netif_err(tp, probe, dev,
8178 "region #%d not an MMIO resource, aborting\n",
8183 /* check for weird/broken PCI region reporting */
8184 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8185 netif_err(tp, probe, dev,
8186 "Invalid PCI region size(s), aborting\n");
8190 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
8192 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8196 tp->mmio_addr = pcim_iomap_table(pdev)[region];
8198 if (!pci_is_pcie(pdev))
8199 netif_info(tp, probe, dev, "not PCI Express\n");
8201 /* Identify chip attached to board */
8202 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8206 if ((sizeof(dma_addr_t) > 4) &&
8207 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8208 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
8209 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8210 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
8212 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8213 if (!pci_is_pcie(pdev))
8214 tp->cp_cmd |= PCIDAC;
8215 dev->features |= NETIF_F_HIGHDMA;
8217 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8219 netif_err(tp, probe, dev, "DMA configuration failed\n");
8226 rtl_irq_disable(tp);
8228 rtl_hw_initialize(tp);
8232 rtl_ack_events(tp, 0xffff);
8234 pci_set_master(pdev);
8236 rtl_init_mdio_ops(tp);
8237 rtl_init_pll_power_ops(tp);
8238 rtl_init_jumbo_ops(tp);
8239 rtl_init_csi_ops(tp);
8241 rtl8169_print_mac_version(tp);
8243 chipset = tp->mac_version;
8244 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8246 rc = rtl_alloc_irq(tp);
8248 netif_err(tp, probe, dev, "Can't allocate interrupt\n");
8252 /* override BIOS settings, use userspace tools to enable WOL */
8253 __rtl8169_set_wol(tp, 0);
8255 if (rtl_tbi_enabled(tp)) {
8256 tp->set_speed = rtl8169_set_speed_tbi;
8257 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
8258 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8259 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8260 tp->link_ok = rtl8169_tbi_link_ok;
8261 tp->do_ioctl = rtl_tbi_ioctl;
8263 tp->set_speed = rtl8169_set_speed_xmii;
8264 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
8265 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8266 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8267 tp->link_ok = rtl8169_xmii_link_ok;
8268 tp->do_ioctl = rtl_xmii_ioctl;
8271 mutex_init(&tp->wk.mutex);
8272 u64_stats_init(&tp->rx_stats.syncp);
8273 u64_stats_init(&tp->tx_stats.syncp);
8275 /* Get MAC address */
8276 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8277 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8278 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8279 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8280 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8281 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8282 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8283 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8284 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8285 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
8286 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8287 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
8288 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8289 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8290 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8291 tp->mac_version == RTL_GIGA_MAC_VER_51) {
8294 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8295 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
8297 if (is_valid_ether_addr((u8 *)mac_addr))
8298 rtl_rar_set(tp, (u8 *)mac_addr);
8300 for (i = 0; i < ETH_ALEN; i++)
8301 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
8303 dev->ethtool_ops = &rtl8169_ethtool_ops;
8304 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
8306 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
8308 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8309 * properly for all devices */
8310 dev->features |= NETIF_F_RXCSUM |
8311 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
8313 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8314 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8315 NETIF_F_HW_VLAN_CTAG_RX;
8316 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8319 tp->cp_cmd |= RxChkSum | RxVlan;
8322 * Pretend we are using VLANs; This bypasses a nasty bug where
8323 * Interrupts stop flowing on high load on 8110SCd controllers.
8325 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
8326 /* Disallow toggling */
8327 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
8329 if (tp->txd_version == RTL_TD_0)
8330 tp->tso_csum = rtl8169_tso_csum_v1;
8331 else if (tp->txd_version == RTL_TD_1) {
8332 tp->tso_csum = rtl8169_tso_csum_v2;
8333 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8337 dev->hw_features |= NETIF_F_RXALL;
8338 dev->hw_features |= NETIF_F_RXFCS;
8340 /* MTU range: 60 - hw-specific max */
8341 dev->min_mtu = ETH_ZLEN;
8342 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8344 tp->hw_start = cfg->hw_start;
8345 tp->event_slow = cfg->event_slow;
8346 tp->coalesce_info = cfg->coalesce_info;
8348 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8349 ~(RxBOVF | RxFOVF) : ~0;
8351 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
8353 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8355 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8356 &tp->counters_phys_addr,
8361 pci_set_drvdata(pdev, dev);
8363 rc = register_netdev(dev);
8367 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8368 rtl_chip_infos[chipset].name, tp->mmio_addr, dev->dev_addr,
8369 (u32)(RTL_R32(tp, TxConfig) & 0x9cf0f8ff),
8370 pci_irq_vector(pdev, 0));
8371 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8372 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8373 "tx checksumming: %s]\n",
8374 rtl_chip_infos[chipset].jumbo_max,
8375 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8378 if (r8168_check_dash(tp))
8379 rtl8168_driver_start(tp);
8381 netif_carrier_off(dev);
8383 if (pci_dev_run_wake(pdev))
8384 pm_runtime_put_sync(&pdev->dev);
8389 static struct pci_driver rtl8169_pci_driver = {
8391 .id_table = rtl8169_pci_tbl,
8392 .probe = rtl_init_one,
8393 .remove = rtl_remove_one,
8394 .shutdown = rtl_shutdown,
8395 .driver.pm = RTL8169_PM_OPS,
8398 module_pci_driver(rtl8169_pci_driver);