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 MAX_READ_REQUEST_SHIFT 12
88 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
89 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
91 #define R8169_REGS_SIZE 256
92 #define R8169_NAPI_WEIGHT 64
93 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
94 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
95 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
98 #define RTL8169_TX_TIMEOUT (6*HZ)
99 #define RTL8169_PHY_TIMEOUT (10*HZ)
101 /* write/read MMIO register */
102 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
103 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
104 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
105 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
106 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
107 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
110 RTL_GIGA_MAC_VER_01 = 0,
161 RTL_GIGA_MAC_NONE = 0xff,
164 enum rtl_tx_desc_version {
169 #define JUMBO_1K ETH_DATA_LEN
170 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
175 #define _R(NAME,TD,FW,SZ,B) { \
183 static const struct {
185 enum rtl_tx_desc_version txd_version;
189 } rtl_chip_infos[] = {
191 [RTL_GIGA_MAC_VER_01] =
192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
193 [RTL_GIGA_MAC_VER_02] =
194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
195 [RTL_GIGA_MAC_VER_03] =
196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
197 [RTL_GIGA_MAC_VER_04] =
198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
199 [RTL_GIGA_MAC_VER_05] =
200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
201 [RTL_GIGA_MAC_VER_06] =
202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
204 [RTL_GIGA_MAC_VER_07] =
205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
206 [RTL_GIGA_MAC_VER_08] =
207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
208 [RTL_GIGA_MAC_VER_09] =
209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
210 [RTL_GIGA_MAC_VER_10] =
211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
212 [RTL_GIGA_MAC_VER_11] =
213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
214 [RTL_GIGA_MAC_VER_12] =
215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
216 [RTL_GIGA_MAC_VER_13] =
217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
218 [RTL_GIGA_MAC_VER_14] =
219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
220 [RTL_GIGA_MAC_VER_15] =
221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
222 [RTL_GIGA_MAC_VER_16] =
223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
224 [RTL_GIGA_MAC_VER_17] =
225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
226 [RTL_GIGA_MAC_VER_18] =
227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
228 [RTL_GIGA_MAC_VER_19] =
229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
230 [RTL_GIGA_MAC_VER_20] =
231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
232 [RTL_GIGA_MAC_VER_21] =
233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
234 [RTL_GIGA_MAC_VER_22] =
235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
236 [RTL_GIGA_MAC_VER_23] =
237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
238 [RTL_GIGA_MAC_VER_24] =
239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
240 [RTL_GIGA_MAC_VER_25] =
241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
243 [RTL_GIGA_MAC_VER_26] =
244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
246 [RTL_GIGA_MAC_VER_27] =
247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
248 [RTL_GIGA_MAC_VER_28] =
249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
250 [RTL_GIGA_MAC_VER_29] =
251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
253 [RTL_GIGA_MAC_VER_30] =
254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
256 [RTL_GIGA_MAC_VER_31] =
257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
258 [RTL_GIGA_MAC_VER_32] =
259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
261 [RTL_GIGA_MAC_VER_33] =
262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
264 [RTL_GIGA_MAC_VER_34] =
265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
267 [RTL_GIGA_MAC_VER_35] =
268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
270 [RTL_GIGA_MAC_VER_36] =
271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
282 [RTL_GIGA_MAC_VER_40] =
283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
326 static const struct pci_device_id rtl8169_pci_tbl[] = {
327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
332 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
333 { PCI_VENDOR_ID_DLINK, 0x4300,
334 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
336 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
337 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
338 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
339 { PCI_VENDOR_ID_LINKSYS, 0x1032,
340 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
342 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
346 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
348 static int rx_buf_sz = 16383;
349 static int use_dac = -1;
355 MAC0 = 0, /* Ethernet hardware address. */
357 MAR0 = 8, /* Multicast filter. */
358 CounterAddrLow = 0x10,
359 CounterAddrHigh = 0x14,
360 TxDescStartAddrLow = 0x20,
361 TxDescStartAddrHigh = 0x24,
362 TxHDescStartAddrLow = 0x28,
363 TxHDescStartAddrHigh = 0x2c,
372 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
373 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
376 #define RX128_INT_EN (1 << 15) /* 8111c and later */
377 #define RX_MULTI_EN (1 << 14) /* 8111c only */
378 #define RXCFG_FIFO_SHIFT 13
379 /* No threshold before first PCI xfer */
380 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
381 #define RX_EARLY_OFF (1 << 11)
382 #define RXCFG_DMA_SHIFT 8
383 /* Unlimited maximum PCI burst. */
384 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
391 #define PME_SIGNAL (1 << 5) /* 8168c and later */
403 #define RTL_COALESCE_MASK 0x0f
404 #define RTL_COALESCE_SHIFT 4
405 #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
406 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
408 RxDescAddrLow = 0xe4,
409 RxDescAddrHigh = 0xe8,
410 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
412 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
414 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
416 #define TxPacketMax (8064 >> 7)
417 #define EarlySize 0x27
420 FuncEventMask = 0xf4,
421 FuncPresetState = 0xf8,
426 FuncForceEvent = 0xfc,
429 enum rtl8110_registers {
435 enum rtl8168_8101_registers {
438 #define CSIAR_FLAG 0x80000000
439 #define CSIAR_WRITE_CMD 0x80000000
440 #define CSIAR_BYTE_ENABLE 0x0f
441 #define CSIAR_BYTE_ENABLE_SHIFT 12
442 #define CSIAR_ADDR_MASK 0x0fff
443 #define CSIAR_FUNC_CARD 0x00000000
444 #define CSIAR_FUNC_SDIO 0x00010000
445 #define CSIAR_FUNC_NIC 0x00020000
446 #define CSIAR_FUNC_NIC2 0x00010000
449 #define EPHYAR_FLAG 0x80000000
450 #define EPHYAR_WRITE_CMD 0x80000000
451 #define EPHYAR_REG_MASK 0x1f
452 #define EPHYAR_REG_SHIFT 16
453 #define EPHYAR_DATA_MASK 0xffff
455 #define PFM_EN (1 << 6)
456 #define TX_10M_PS_EN (1 << 7)
458 #define FIX_NAK_1 (1 << 4)
459 #define FIX_NAK_2 (1 << 3)
462 #define NOW_IS_OOB (1 << 7)
463 #define TX_EMPTY (1 << 5)
464 #define RX_EMPTY (1 << 4)
465 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
466 #define EN_NDP (1 << 3)
467 #define EN_OOB_RESET (1 << 2)
468 #define LINK_LIST_RDY (1 << 1)
470 #define EFUSEAR_FLAG 0x80000000
471 #define EFUSEAR_WRITE_CMD 0x80000000
472 #define EFUSEAR_READ_CMD 0x00000000
473 #define EFUSEAR_REG_MASK 0x03ff
474 #define EFUSEAR_REG_SHIFT 8
475 #define EFUSEAR_DATA_MASK 0xff
477 #define PFM_D3COLD_EN (1 << 6)
480 enum rtl8168_registers {
485 #define ERIAR_FLAG 0x80000000
486 #define ERIAR_WRITE_CMD 0x80000000
487 #define ERIAR_READ_CMD 0x00000000
488 #define ERIAR_ADDR_BYTE_ALIGN 4
489 #define ERIAR_TYPE_SHIFT 16
490 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
491 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
492 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
493 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
494 #define ERIAR_MASK_SHIFT 12
495 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
496 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
497 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
498 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
499 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
500 EPHY_RXER_NUM = 0x7c,
501 OCPDR = 0xb0, /* OCP GPHY access */
502 #define OCPDR_WRITE_CMD 0x80000000
503 #define OCPDR_READ_CMD 0x00000000
504 #define OCPDR_REG_MASK 0x7f
505 #define OCPDR_GPHY_REG_SHIFT 16
506 #define OCPDR_DATA_MASK 0xffff
508 #define OCPAR_FLAG 0x80000000
509 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
510 #define OCPAR_GPHY_READ_CMD 0x0000f060
512 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
513 MISC = 0xf0, /* 8168e only. */
514 #define TXPLA_RST (1 << 29)
515 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
516 #define PWM_EN (1 << 22)
517 #define RXDV_GATED_EN (1 << 19)
518 #define EARLY_TALLY_EN (1 << 16)
521 enum rtl_register_content {
522 /* InterruptStatusBits */
526 TxDescUnavail = 0x0080,
550 /* TXPoll register p.5 */
551 HPQ = 0x80, /* Poll cmd on the high prio queue */
552 NPQ = 0x40, /* Poll cmd on the low prio queue */
553 FSWInt = 0x01, /* Forced software interrupt */
557 Cfg9346_Unlock = 0xc0,
562 AcceptBroadcast = 0x08,
563 AcceptMulticast = 0x04,
565 AcceptAllPhys = 0x01,
566 #define RX_CONFIG_ACCEPT_MASK 0x3f
569 TxInterFrameGapShift = 24,
570 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
572 /* Config1 register p.24 */
575 Speed_down = (1 << 4),
579 PMEnable = (1 << 0), /* Power Management Enable */
581 /* Config2 register p. 25 */
582 ClkReqEn = (1 << 7), /* Clock Request Enable */
583 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
584 PCI_Clock_66MHz = 0x01,
585 PCI_Clock_33MHz = 0x00,
587 /* Config3 register p.25 */
588 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
589 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
590 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
591 Rdy_to_L23 = (1 << 1), /* L23 Enable */
592 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
594 /* Config4 register */
595 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
597 /* Config5 register p.27 */
598 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
599 MWF = (1 << 5), /* Accept Multicast wakeup frame */
600 UWF = (1 << 4), /* Accept Unicast wakeup frame */
602 LanWake = (1 << 1), /* LanWake enable/disable */
603 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
604 ASPM_en = (1 << 0), /* ASPM enable */
607 TBIReset = 0x80000000,
608 TBILoopback = 0x40000000,
609 TBINwEnable = 0x20000000,
610 TBINwRestart = 0x10000000,
611 TBILinkOk = 0x02000000,
612 TBINwComplete = 0x01000000,
615 EnableBist = (1 << 15), // 8168 8101
616 Mac_dbgo_oe = (1 << 14), // 8168 8101
617 Normal_mode = (1 << 13), // unused
618 Force_half_dup = (1 << 12), // 8168 8101
619 Force_rxflow_en = (1 << 11), // 8168 8101
620 Force_txflow_en = (1 << 10), // 8168 8101
621 Cxpl_dbg_sel = (1 << 9), // 8168 8101
622 ASF = (1 << 8), // 8168 8101
623 PktCntrDisable = (1 << 7), // 8168 8101
624 Mac_dbgo_sel = 0x001c, // 8168
629 INTT_0 = 0x0000, // 8168
630 INTT_1 = 0x0001, // 8168
631 INTT_2 = 0x0002, // 8168
632 INTT_3 = 0x0003, // 8168
634 /* rtl8169_PHYstatus */
645 TBILinkOK = 0x02000000,
647 /* ResetCounterCommand */
650 /* DumpCounterCommand */
653 /* magic enable v2 */
654 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
658 /* First doubleword. */
659 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
660 RingEnd = (1 << 30), /* End of descriptor ring */
661 FirstFrag = (1 << 29), /* First segment of a packet */
662 LastFrag = (1 << 28), /* Final segment of a packet */
666 enum rtl_tx_desc_bit {
667 /* First doubleword. */
668 TD_LSO = (1 << 27), /* Large Send Offload */
669 #define TD_MSS_MAX 0x07ffu /* MSS value */
671 /* Second doubleword. */
672 TxVlanTag = (1 << 17), /* Add VLAN tag */
675 /* 8169, 8168b and 810x except 8102e. */
676 enum rtl_tx_desc_bit_0 {
677 /* First doubleword. */
678 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
679 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
680 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
681 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
684 /* 8102e, 8168c and beyond. */
685 enum rtl_tx_desc_bit_1 {
686 /* First doubleword. */
687 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
688 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
689 #define GTTCPHO_SHIFT 18
690 #define GTTCPHO_MAX 0x7fU
692 /* Second doubleword. */
693 #define TCPHO_SHIFT 18
694 #define TCPHO_MAX 0x3ffU
695 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
696 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
697 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
698 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
699 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
702 enum rtl_rx_desc_bit {
704 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
705 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
707 #define RxProtoUDP (PID1)
708 #define RxProtoTCP (PID0)
709 #define RxProtoIP (PID1 | PID0)
710 #define RxProtoMask RxProtoIP
712 IPFail = (1 << 16), /* IP checksum failed */
713 UDPFail = (1 << 15), /* UDP/IP checksum failed */
714 TCPFail = (1 << 14), /* TCP/IP checksum failed */
715 RxVlanTag = (1 << 16), /* VLAN tag available */
718 #define RsvdMask 0x3fffc000
735 u8 __pad[sizeof(void *) - sizeof(u32)];
738 struct rtl8169_counters {
745 __le32 tx_one_collision;
746 __le32 tx_multi_collision;
754 struct rtl8169_tc_offsets {
757 __le32 tx_multi_collision;
762 RTL_FLAG_TASK_ENABLED,
763 RTL_FLAG_TASK_SLOW_PENDING,
764 RTL_FLAG_TASK_RESET_PENDING,
765 RTL_FLAG_TASK_PHY_PENDING,
769 struct rtl8169_stats {
772 struct u64_stats_sync syncp;
775 struct rtl8169_private {
776 void __iomem *mmio_addr; /* memory map physical address */
777 struct pci_dev *pci_dev;
778 struct net_device *dev;
779 struct napi_struct napi;
783 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
784 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
786 struct rtl8169_stats rx_stats;
787 struct rtl8169_stats tx_stats;
788 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
789 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
790 dma_addr_t TxPhyAddr;
791 dma_addr_t RxPhyAddr;
792 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
793 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
794 struct timer_list timer;
798 const struct rtl_coalesce_info *coalesce_info;
801 void (*write)(struct rtl8169_private *, int, int);
802 int (*read)(struct rtl8169_private *, int);
805 struct pll_power_ops {
806 void (*down)(struct rtl8169_private *);
807 void (*up)(struct rtl8169_private *);
811 void (*enable)(struct rtl8169_private *);
812 void (*disable)(struct rtl8169_private *);
816 void (*write)(struct rtl8169_private *, int, int);
817 u32 (*read)(struct rtl8169_private *, int);
820 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
821 int (*get_link_ksettings)(struct net_device *,
822 struct ethtool_link_ksettings *);
823 void (*phy_reset_enable)(struct rtl8169_private *tp);
824 void (*hw_start)(struct net_device *);
825 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
826 unsigned int (*link_ok)(struct rtl8169_private *tp);
827 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
828 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
831 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
833 struct work_struct work;
838 struct mii_if_info mii;
839 dma_addr_t counters_phys_addr;
840 struct rtl8169_counters *counters;
841 struct rtl8169_tc_offsets tc_offset;
846 const struct firmware *fw;
848 #define RTL_VER_SIZE 32
850 char version[RTL_VER_SIZE];
852 struct rtl_fw_phy_action {
857 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
862 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
863 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
864 module_param(use_dac, int, 0);
865 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
866 module_param_named(debug, debug.msg_enable, int, 0);
867 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
868 MODULE_LICENSE("GPL");
869 MODULE_VERSION(RTL8169_VERSION);
870 MODULE_FIRMWARE(FIRMWARE_8168D_1);
871 MODULE_FIRMWARE(FIRMWARE_8168D_2);
872 MODULE_FIRMWARE(FIRMWARE_8168E_1);
873 MODULE_FIRMWARE(FIRMWARE_8168E_2);
874 MODULE_FIRMWARE(FIRMWARE_8168E_3);
875 MODULE_FIRMWARE(FIRMWARE_8105E_1);
876 MODULE_FIRMWARE(FIRMWARE_8168F_1);
877 MODULE_FIRMWARE(FIRMWARE_8168F_2);
878 MODULE_FIRMWARE(FIRMWARE_8402_1);
879 MODULE_FIRMWARE(FIRMWARE_8411_1);
880 MODULE_FIRMWARE(FIRMWARE_8411_2);
881 MODULE_FIRMWARE(FIRMWARE_8106E_1);
882 MODULE_FIRMWARE(FIRMWARE_8106E_2);
883 MODULE_FIRMWARE(FIRMWARE_8168G_2);
884 MODULE_FIRMWARE(FIRMWARE_8168G_3);
885 MODULE_FIRMWARE(FIRMWARE_8168H_1);
886 MODULE_FIRMWARE(FIRMWARE_8168H_2);
887 MODULE_FIRMWARE(FIRMWARE_8107E_1);
888 MODULE_FIRMWARE(FIRMWARE_8107E_2);
890 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
892 return &tp->pci_dev->dev;
895 static void rtl_lock_work(struct rtl8169_private *tp)
897 mutex_lock(&tp->wk.mutex);
900 static void rtl_unlock_work(struct rtl8169_private *tp)
902 mutex_unlock(&tp->wk.mutex);
905 static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
907 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
908 PCI_EXP_DEVCTL_READRQ, force);
912 bool (*check)(struct rtl8169_private *);
916 static void rtl_udelay(unsigned int d)
921 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
922 void (*delay)(unsigned int), unsigned int d, int n,
927 for (i = 0; i < n; i++) {
929 if (c->check(tp) == high)
932 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
933 c->msg, !high, n, d);
937 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
938 const struct rtl_cond *c,
939 unsigned int d, int n)
941 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
944 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
945 const struct rtl_cond *c,
946 unsigned int d, int n)
948 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
951 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
952 const struct rtl_cond *c,
953 unsigned int d, int n)
955 return rtl_loop_wait(tp, c, msleep, d, n, true);
958 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
959 const struct rtl_cond *c,
960 unsigned int d, int n)
962 return rtl_loop_wait(tp, c, msleep, d, n, false);
965 #define DECLARE_RTL_COND(name) \
966 static bool name ## _check(struct rtl8169_private *); \
968 static const struct rtl_cond name = { \
969 .check = name ## _check, \
973 static bool name ## _check(struct rtl8169_private *tp)
975 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
977 if (reg & 0xffff0001) {
978 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
984 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
986 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
989 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
991 if (rtl_ocp_reg_failure(tp, reg))
994 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
996 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
999 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1001 if (rtl_ocp_reg_failure(tp, reg))
1004 RTL_W32(tp, GPHY_OCP, reg << 15);
1006 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1007 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
1010 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1012 if (rtl_ocp_reg_failure(tp, reg))
1015 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
1018 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1020 if (rtl_ocp_reg_failure(tp, reg))
1023 RTL_W32(tp, OCPDR, reg << 15);
1025 return RTL_R32(tp, OCPDR);
1028 #define OCP_STD_PHY_BASE 0xa400
1030 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1033 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1037 if (tp->ocp_base != OCP_STD_PHY_BASE)
1040 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1043 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1045 if (tp->ocp_base != OCP_STD_PHY_BASE)
1048 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1051 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1054 tp->ocp_base = value << 4;
1058 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1061 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1063 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1066 DECLARE_RTL_COND(rtl_phyar_cond)
1068 return RTL_R32(tp, PHYAR) & 0x80000000;
1071 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1073 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1075 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
1077 * According to hardware specs a 20us delay is required after write
1078 * complete indication, but before sending next command.
1083 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1087 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
1089 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1090 RTL_R32(tp, PHYAR) & 0xffff : ~0;
1093 * According to hardware specs a 20us delay is required after read
1094 * complete indication, but before sending next command.
1101 DECLARE_RTL_COND(rtl_ocpar_cond)
1103 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
1106 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1108 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1109 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1110 RTL_W32(tp, EPHY_RXER_NUM, 0);
1112 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1115 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1117 r8168dp_1_mdio_access(tp, reg,
1118 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1121 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1123 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1126 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1127 RTL_W32(tp, EPHY_RXER_NUM, 0);
1129 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1130 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
1133 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1135 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
1137 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1140 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
1142 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1145 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1147 r8168dp_2_mdio_start(tp);
1149 r8169_mdio_write(tp, reg, value);
1151 r8168dp_2_mdio_stop(tp);
1154 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1158 r8168dp_2_mdio_start(tp);
1160 value = r8169_mdio_read(tp, reg);
1162 r8168dp_2_mdio_stop(tp);
1167 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1169 tp->mdio_ops.write(tp, location, val);
1172 static int rtl_readphy(struct rtl8169_private *tp, int location)
1174 return tp->mdio_ops.read(tp, location);
1177 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1179 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1182 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1186 val = rtl_readphy(tp, reg_addr);
1187 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1190 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1193 struct rtl8169_private *tp = netdev_priv(dev);
1195 rtl_writephy(tp, location, val);
1198 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1200 struct rtl8169_private *tp = netdev_priv(dev);
1202 return rtl_readphy(tp, location);
1205 DECLARE_RTL_COND(rtl_ephyar_cond)
1207 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1210 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1212 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1213 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1215 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1220 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1222 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1224 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1225 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1228 DECLARE_RTL_COND(rtl_eriar_cond)
1230 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
1233 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1236 BUG_ON((addr & 3) || (mask == 0));
1237 RTL_W32(tp, ERIDR, val);
1238 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1240 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1243 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1245 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1247 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1248 RTL_R32(tp, ERIDR) : ~0;
1251 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1256 val = rtl_eri_read(tp, addr, type);
1257 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1260 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1262 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1263 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1264 RTL_R32(tp, OCPDR) : ~0;
1267 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1269 return rtl_eri_read(tp, reg, ERIAR_OOB);
1272 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1274 switch (tp->mac_version) {
1275 case RTL_GIGA_MAC_VER_27:
1276 case RTL_GIGA_MAC_VER_28:
1277 case RTL_GIGA_MAC_VER_31:
1278 return r8168dp_ocp_read(tp, mask, reg);
1279 case RTL_GIGA_MAC_VER_49:
1280 case RTL_GIGA_MAC_VER_50:
1281 case RTL_GIGA_MAC_VER_51:
1282 return r8168ep_ocp_read(tp, mask, reg);
1289 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1292 RTL_W32(tp, OCPDR, data);
1293 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1294 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1297 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1300 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1304 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1306 switch (tp->mac_version) {
1307 case RTL_GIGA_MAC_VER_27:
1308 case RTL_GIGA_MAC_VER_28:
1309 case RTL_GIGA_MAC_VER_31:
1310 r8168dp_ocp_write(tp, mask, reg, data);
1312 case RTL_GIGA_MAC_VER_49:
1313 case RTL_GIGA_MAC_VER_50:
1314 case RTL_GIGA_MAC_VER_51:
1315 r8168ep_ocp_write(tp, mask, reg, data);
1323 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1325 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1327 ocp_write(tp, 0x1, 0x30, 0x00000001);
1330 #define OOB_CMD_RESET 0x00
1331 #define OOB_CMD_DRIVER_START 0x05
1332 #define OOB_CMD_DRIVER_STOP 0x06
1334 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1336 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1339 DECLARE_RTL_COND(rtl_ocp_read_cond)
1343 reg = rtl8168_get_ocp_reg(tp);
1345 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1348 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1350 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1353 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1355 return RTL_R8(tp, IBISR0) & 0x20;
1358 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1360 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1361 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1362 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1363 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1366 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1368 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1369 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1372 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1374 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1375 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1376 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1379 static void rtl8168_driver_start(struct rtl8169_private *tp)
1381 switch (tp->mac_version) {
1382 case RTL_GIGA_MAC_VER_27:
1383 case RTL_GIGA_MAC_VER_28:
1384 case RTL_GIGA_MAC_VER_31:
1385 rtl8168dp_driver_start(tp);
1387 case RTL_GIGA_MAC_VER_49:
1388 case RTL_GIGA_MAC_VER_50:
1389 case RTL_GIGA_MAC_VER_51:
1390 rtl8168ep_driver_start(tp);
1398 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1400 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1401 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1404 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1406 rtl8168ep_stop_cmac(tp);
1407 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1408 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1409 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1412 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1414 switch (tp->mac_version) {
1415 case RTL_GIGA_MAC_VER_27:
1416 case RTL_GIGA_MAC_VER_28:
1417 case RTL_GIGA_MAC_VER_31:
1418 rtl8168dp_driver_stop(tp);
1420 case RTL_GIGA_MAC_VER_49:
1421 case RTL_GIGA_MAC_VER_50:
1422 case RTL_GIGA_MAC_VER_51:
1423 rtl8168ep_driver_stop(tp);
1431 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1433 u16 reg = rtl8168_get_ocp_reg(tp);
1435 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
1438 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1440 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
1443 static bool r8168_check_dash(struct rtl8169_private *tp)
1445 switch (tp->mac_version) {
1446 case RTL_GIGA_MAC_VER_27:
1447 case RTL_GIGA_MAC_VER_28:
1448 case RTL_GIGA_MAC_VER_31:
1449 return r8168dp_check_dash(tp);
1450 case RTL_GIGA_MAC_VER_49:
1451 case RTL_GIGA_MAC_VER_50:
1452 case RTL_GIGA_MAC_VER_51:
1453 return r8168ep_check_dash(tp);
1465 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1466 const struct exgmac_reg *r, int len)
1469 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1474 DECLARE_RTL_COND(rtl_efusear_cond)
1476 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1479 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1481 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1483 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1484 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1487 static u16 rtl_get_events(struct rtl8169_private *tp)
1489 return RTL_R16(tp, IntrStatus);
1492 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1494 RTL_W16(tp, IntrStatus, bits);
1498 static void rtl_irq_disable(struct rtl8169_private *tp)
1500 RTL_W16(tp, IntrMask, 0);
1504 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1506 RTL_W16(tp, IntrMask, bits);
1509 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1510 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1511 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1513 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1515 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1518 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1520 rtl_irq_disable(tp);
1521 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1522 RTL_R8(tp, ChipCmd);
1525 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1527 return RTL_R32(tp, TBICSR) & TBIReset;
1530 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1532 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1535 static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
1537 return RTL_R32(tp, TBICSR) & TBILinkOk;
1540 static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
1542 return RTL_R8(tp, PHYstatus) & LinkStatus;
1545 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1547 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
1550 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1554 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1555 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1558 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1560 struct net_device *dev = tp->dev;
1562 if (!netif_running(dev))
1565 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1566 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1567 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
1568 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1570 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1572 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
1573 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1575 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1578 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1580 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1583 /* Reset packet filter */
1584 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1586 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1588 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1589 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1590 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
1591 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1593 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1596 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1598 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1601 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1602 if (RTL_R8(tp, PHYstatus) & _10bps) {
1603 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1605 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1608 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1614 static void rtl8169_check_link_status(struct net_device *dev,
1615 struct rtl8169_private *tp)
1617 struct device *d = tp_to_dev(tp);
1619 if (tp->link_ok(tp)) {
1620 rtl_link_chg_patch(tp);
1621 /* This is to cancel a scheduled suspend if there's one. */
1622 pm_request_resume(d);
1623 netif_carrier_on(dev);
1624 if (net_ratelimit())
1625 netif_info(tp, ifup, dev, "link up\n");
1627 netif_carrier_off(dev);
1628 netif_info(tp, ifdown, dev, "link down\n");
1633 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1635 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1640 options = RTL_R8(tp, Config1);
1641 if (!(options & PMEnable))
1644 options = RTL_R8(tp, Config3);
1645 if (options & LinkUp)
1646 wolopts |= WAKE_PHY;
1647 switch (tp->mac_version) {
1648 case RTL_GIGA_MAC_VER_34:
1649 case RTL_GIGA_MAC_VER_35:
1650 case RTL_GIGA_MAC_VER_36:
1651 case RTL_GIGA_MAC_VER_37:
1652 case RTL_GIGA_MAC_VER_38:
1653 case RTL_GIGA_MAC_VER_40:
1654 case RTL_GIGA_MAC_VER_41:
1655 case RTL_GIGA_MAC_VER_42:
1656 case RTL_GIGA_MAC_VER_43:
1657 case RTL_GIGA_MAC_VER_44:
1658 case RTL_GIGA_MAC_VER_45:
1659 case RTL_GIGA_MAC_VER_46:
1660 case RTL_GIGA_MAC_VER_47:
1661 case RTL_GIGA_MAC_VER_48:
1662 case RTL_GIGA_MAC_VER_49:
1663 case RTL_GIGA_MAC_VER_50:
1664 case RTL_GIGA_MAC_VER_51:
1665 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1666 wolopts |= WAKE_MAGIC;
1669 if (options & MagicPacket)
1670 wolopts |= WAKE_MAGIC;
1674 options = RTL_R8(tp, Config5);
1676 wolopts |= WAKE_UCAST;
1678 wolopts |= WAKE_BCAST;
1680 wolopts |= WAKE_MCAST;
1685 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1687 struct rtl8169_private *tp = netdev_priv(dev);
1688 struct device *d = tp_to_dev(tp);
1690 pm_runtime_get_noresume(d);
1694 wol->supported = WAKE_ANY;
1695 if (pm_runtime_active(d))
1696 wol->wolopts = __rtl8169_get_wol(tp);
1698 wol->wolopts = tp->saved_wolopts;
1700 rtl_unlock_work(tp);
1702 pm_runtime_put_noidle(d);
1705 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1707 unsigned int i, tmp;
1708 static const struct {
1713 { WAKE_PHY, Config3, LinkUp },
1714 { WAKE_UCAST, Config5, UWF },
1715 { WAKE_BCAST, Config5, BWF },
1716 { WAKE_MCAST, Config5, MWF },
1717 { WAKE_ANY, Config5, LanWake },
1718 { WAKE_MAGIC, Config3, MagicPacket }
1722 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
1724 switch (tp->mac_version) {
1725 case RTL_GIGA_MAC_VER_34:
1726 case RTL_GIGA_MAC_VER_35:
1727 case RTL_GIGA_MAC_VER_36:
1728 case RTL_GIGA_MAC_VER_37:
1729 case RTL_GIGA_MAC_VER_38:
1730 case RTL_GIGA_MAC_VER_40:
1731 case RTL_GIGA_MAC_VER_41:
1732 case RTL_GIGA_MAC_VER_42:
1733 case RTL_GIGA_MAC_VER_43:
1734 case RTL_GIGA_MAC_VER_44:
1735 case RTL_GIGA_MAC_VER_45:
1736 case RTL_GIGA_MAC_VER_46:
1737 case RTL_GIGA_MAC_VER_47:
1738 case RTL_GIGA_MAC_VER_48:
1739 case RTL_GIGA_MAC_VER_49:
1740 case RTL_GIGA_MAC_VER_50:
1741 case RTL_GIGA_MAC_VER_51:
1742 tmp = ARRAY_SIZE(cfg) - 1;
1743 if (wolopts & WAKE_MAGIC)
1759 tmp = ARRAY_SIZE(cfg);
1763 for (i = 0; i < tmp; i++) {
1764 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1765 if (wolopts & cfg[i].opt)
1766 options |= cfg[i].mask;
1767 RTL_W8(tp, cfg[i].reg, options);
1770 switch (tp->mac_version) {
1771 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1772 options = RTL_R8(tp, Config1) & ~PMEnable;
1774 options |= PMEnable;
1775 RTL_W8(tp, Config1, options);
1778 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
1780 options |= PME_SIGNAL;
1781 RTL_W8(tp, Config2, options);
1785 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
1788 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1790 struct rtl8169_private *tp = netdev_priv(dev);
1791 struct device *d = tp_to_dev(tp);
1793 pm_runtime_get_noresume(d);
1797 if (pm_runtime_active(d))
1798 __rtl8169_set_wol(tp, wol->wolopts);
1800 tp->saved_wolopts = wol->wolopts;
1802 rtl_unlock_work(tp);
1804 device_set_wakeup_enable(d, wol->wolopts);
1806 pm_runtime_put_noidle(d);
1811 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1813 return rtl_chip_infos[tp->mac_version].fw_name;
1816 static void rtl8169_get_drvinfo(struct net_device *dev,
1817 struct ethtool_drvinfo *info)
1819 struct rtl8169_private *tp = netdev_priv(dev);
1820 struct rtl_fw *rtl_fw = tp->rtl_fw;
1822 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1823 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1824 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1825 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1826 if (!IS_ERR_OR_NULL(rtl_fw))
1827 strlcpy(info->fw_version, rtl_fw->version,
1828 sizeof(info->fw_version));
1831 static int rtl8169_get_regs_len(struct net_device *dev)
1833 return R8169_REGS_SIZE;
1836 static int rtl8169_set_speed_tbi(struct net_device *dev,
1837 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1839 struct rtl8169_private *tp = netdev_priv(dev);
1843 reg = RTL_R32(tp, TBICSR);
1844 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1845 (duplex == DUPLEX_FULL)) {
1846 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1847 } else if (autoneg == AUTONEG_ENABLE)
1848 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
1850 netif_warn(tp, link, dev,
1851 "incorrect speed setting refused in TBI mode\n");
1858 static int rtl8169_set_speed_xmii(struct net_device *dev,
1859 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1861 struct rtl8169_private *tp = netdev_priv(dev);
1862 int giga_ctrl, bmcr;
1865 rtl_writephy(tp, 0x1f, 0x0000);
1867 if (autoneg == AUTONEG_ENABLE) {
1870 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1871 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1872 ADVERTISE_100HALF | ADVERTISE_100FULL);
1874 if (adv & ADVERTISED_10baseT_Half)
1875 auto_nego |= ADVERTISE_10HALF;
1876 if (adv & ADVERTISED_10baseT_Full)
1877 auto_nego |= ADVERTISE_10FULL;
1878 if (adv & ADVERTISED_100baseT_Half)
1879 auto_nego |= ADVERTISE_100HALF;
1880 if (adv & ADVERTISED_100baseT_Full)
1881 auto_nego |= ADVERTISE_100FULL;
1883 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1885 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1886 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1888 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1889 if (tp->mii.supports_gmii) {
1890 if (adv & ADVERTISED_1000baseT_Half)
1891 giga_ctrl |= ADVERTISE_1000HALF;
1892 if (adv & ADVERTISED_1000baseT_Full)
1893 giga_ctrl |= ADVERTISE_1000FULL;
1894 } else if (adv & (ADVERTISED_1000baseT_Half |
1895 ADVERTISED_1000baseT_Full)) {
1896 netif_info(tp, link, dev,
1897 "PHY does not support 1000Mbps\n");
1901 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1903 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1904 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1906 if (speed == SPEED_10)
1908 else if (speed == SPEED_100)
1909 bmcr = BMCR_SPEED100;
1913 if (duplex == DUPLEX_FULL)
1914 bmcr |= BMCR_FULLDPLX;
1917 rtl_writephy(tp, MII_BMCR, bmcr);
1919 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1920 tp->mac_version == RTL_GIGA_MAC_VER_03) {
1921 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1922 rtl_writephy(tp, 0x17, 0x2138);
1923 rtl_writephy(tp, 0x0e, 0x0260);
1925 rtl_writephy(tp, 0x17, 0x2108);
1926 rtl_writephy(tp, 0x0e, 0x0000);
1935 static int rtl8169_set_speed(struct net_device *dev,
1936 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1938 struct rtl8169_private *tp = netdev_priv(dev);
1941 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1945 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1946 (advertising & ADVERTISED_1000baseT_Full) &&
1947 !pci_is_pcie(tp->pci_dev)) {
1948 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1954 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1955 netdev_features_t features)
1957 struct rtl8169_private *tp = netdev_priv(dev);
1959 if (dev->mtu > TD_MSS_MAX)
1960 features &= ~NETIF_F_ALL_TSO;
1962 if (dev->mtu > JUMBO_1K &&
1963 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1964 features &= ~NETIF_F_IP_CSUM;
1969 static void __rtl8169_set_features(struct net_device *dev,
1970 netdev_features_t features)
1972 struct rtl8169_private *tp = netdev_priv(dev);
1975 rx_config = RTL_R32(tp, RxConfig);
1976 if (features & NETIF_F_RXALL)
1977 rx_config |= (AcceptErr | AcceptRunt);
1979 rx_config &= ~(AcceptErr | AcceptRunt);
1981 RTL_W32(tp, RxConfig, rx_config);
1983 if (features & NETIF_F_RXCSUM)
1984 tp->cp_cmd |= RxChkSum;
1986 tp->cp_cmd &= ~RxChkSum;
1988 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1989 tp->cp_cmd |= RxVlan;
1991 tp->cp_cmd &= ~RxVlan;
1993 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) & ~(RxVlan | RxChkSum);
1995 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1996 RTL_R16(tp, CPlusCmd);
1999 static int rtl8169_set_features(struct net_device *dev,
2000 netdev_features_t features)
2002 struct rtl8169_private *tp = netdev_priv(dev);
2004 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2007 if (features ^ dev->features)
2008 __rtl8169_set_features(dev, features);
2009 rtl_unlock_work(tp);
2015 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
2017 return (skb_vlan_tag_present(skb)) ?
2018 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
2021 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
2023 u32 opts2 = le32_to_cpu(desc->opts2);
2025 if (opts2 & RxVlanTag)
2026 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
2029 static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2030 struct ethtool_link_ksettings *cmd)
2032 struct rtl8169_private *tp = netdev_priv(dev);
2034 u32 supported, advertising;
2037 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
2038 cmd->base.port = PORT_FIBRE;
2040 status = RTL_R32(tp, TBICSR);
2041 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2042 cmd->base.autoneg = !!(status & TBINwEnable);
2044 cmd->base.speed = SPEED_1000;
2045 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2047 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2049 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2055 static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2056 struct ethtool_link_ksettings *cmd)
2058 struct rtl8169_private *tp = netdev_priv(dev);
2060 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2065 static int rtl8169_get_link_ksettings(struct net_device *dev,
2066 struct ethtool_link_ksettings *cmd)
2068 struct rtl8169_private *tp = netdev_priv(dev);
2072 rc = tp->get_link_ksettings(dev, cmd);
2073 rtl_unlock_work(tp);
2078 static int rtl8169_set_link_ksettings(struct net_device *dev,
2079 const struct ethtool_link_ksettings *cmd)
2081 struct rtl8169_private *tp = netdev_priv(dev);
2085 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2086 cmd->link_modes.advertising))
2089 del_timer_sync(&tp->timer);
2092 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2093 cmd->base.duplex, advertising);
2094 rtl_unlock_work(tp);
2099 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2102 struct rtl8169_private *tp = netdev_priv(dev);
2103 u32 __iomem *data = tp->mmio_addr;
2108 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2109 memcpy_fromio(dw++, data++, 4);
2110 rtl_unlock_work(tp);
2113 static u32 rtl8169_get_msglevel(struct net_device *dev)
2115 struct rtl8169_private *tp = netdev_priv(dev);
2117 return tp->msg_enable;
2120 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2122 struct rtl8169_private *tp = netdev_priv(dev);
2124 tp->msg_enable = value;
2127 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2134 "tx_single_collisions",
2135 "tx_multi_collisions",
2143 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
2147 return ARRAY_SIZE(rtl8169_gstrings);
2153 DECLARE_RTL_COND(rtl_counters_cond)
2155 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
2158 static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
2160 struct rtl8169_private *tp = netdev_priv(dev);
2161 dma_addr_t paddr = tp->counters_phys_addr;
2164 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2165 RTL_R32(tp, CounterAddrHigh);
2166 cmd = (u64)paddr & DMA_BIT_MASK(32);
2167 RTL_W32(tp, CounterAddrLow, cmd);
2168 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
2170 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
2173 static bool rtl8169_reset_counters(struct net_device *dev)
2175 struct rtl8169_private *tp = netdev_priv(dev);
2178 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2181 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2184 return rtl8169_do_counters(dev, CounterReset);
2187 static bool rtl8169_update_counters(struct net_device *dev)
2189 struct rtl8169_private *tp = netdev_priv(dev);
2192 * Some chips are unable to dump tally counters when the receiver
2195 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
2198 return rtl8169_do_counters(dev, CounterDump);
2201 static bool rtl8169_init_counter_offsets(struct net_device *dev)
2203 struct rtl8169_private *tp = netdev_priv(dev);
2204 struct rtl8169_counters *counters = tp->counters;
2208 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2209 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2210 * reset by a power cycle, while the counter values collected by the
2211 * driver are reset at every driver unload/load cycle.
2213 * To make sure the HW values returned by @get_stats64 match the SW
2214 * values, we collect the initial values at first open(*) and use them
2215 * as offsets to normalize the values returned by @get_stats64.
2217 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2218 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2219 * set at open time by rtl_hw_start.
2222 if (tp->tc_offset.inited)
2225 /* If both, reset and update fail, propagate to caller. */
2226 if (rtl8169_reset_counters(dev))
2229 if (rtl8169_update_counters(dev))
2232 tp->tc_offset.tx_errors = counters->tx_errors;
2233 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2234 tp->tc_offset.tx_aborted = counters->tx_aborted;
2235 tp->tc_offset.inited = true;
2240 static void rtl8169_get_ethtool_stats(struct net_device *dev,
2241 struct ethtool_stats *stats, u64 *data)
2243 struct rtl8169_private *tp = netdev_priv(dev);
2244 struct device *d = tp_to_dev(tp);
2245 struct rtl8169_counters *counters = tp->counters;
2249 pm_runtime_get_noresume(d);
2251 if (pm_runtime_active(d))
2252 rtl8169_update_counters(dev);
2254 pm_runtime_put_noidle(d);
2256 data[0] = le64_to_cpu(counters->tx_packets);
2257 data[1] = le64_to_cpu(counters->rx_packets);
2258 data[2] = le64_to_cpu(counters->tx_errors);
2259 data[3] = le32_to_cpu(counters->rx_errors);
2260 data[4] = le16_to_cpu(counters->rx_missed);
2261 data[5] = le16_to_cpu(counters->align_errors);
2262 data[6] = le32_to_cpu(counters->tx_one_collision);
2263 data[7] = le32_to_cpu(counters->tx_multi_collision);
2264 data[8] = le64_to_cpu(counters->rx_unicast);
2265 data[9] = le64_to_cpu(counters->rx_broadcast);
2266 data[10] = le32_to_cpu(counters->rx_multicast);
2267 data[11] = le16_to_cpu(counters->tx_aborted);
2268 data[12] = le16_to_cpu(counters->tx_underun);
2271 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2275 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2280 static int rtl8169_nway_reset(struct net_device *dev)
2282 struct rtl8169_private *tp = netdev_priv(dev);
2284 return mii_nway_restart(&tp->mii);
2288 * Interrupt coalescing
2290 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2291 * > 8169, 8168 and 810x line of chipsets
2293 * 8169, 8168, and 8136(810x) serial chipsets support it.
2295 * > 2 - the Tx timer unit at gigabit speed
2297 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2298 * (0xe0) bit 1 and bit 0.
2301 * bit[1:0] \ speed 1000M 100M 10M
2302 * 0 0 320ns 2.56us 40.96us
2303 * 0 1 2.56us 20.48us 327.7us
2304 * 1 0 5.12us 40.96us 655.4us
2305 * 1 1 10.24us 81.92us 1.31ms
2308 * bit[1:0] \ speed 1000M 100M 10M
2309 * 0 0 5us 2.56us 40.96us
2310 * 0 1 40us 20.48us 327.7us
2311 * 1 0 80us 40.96us 655.4us
2312 * 1 1 160us 81.92us 1.31ms
2315 /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2316 struct rtl_coalesce_scale {
2321 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
2322 struct rtl_coalesce_info {
2324 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2327 /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2328 #define rxtx_x1822(r, t) { \
2331 {{(r)*8*2, (t)*8*2}}, \
2332 {{(r)*8*2*2, (t)*8*2*2}}, \
2334 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2335 /* speed delays: rx00 tx00 */
2336 { SPEED_10, rxtx_x1822(40960, 40960) },
2337 { SPEED_100, rxtx_x1822( 2560, 2560) },
2338 { SPEED_1000, rxtx_x1822( 320, 320) },
2342 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2343 /* speed delays: rx00 tx00 */
2344 { SPEED_10, rxtx_x1822(40960, 40960) },
2345 { SPEED_100, rxtx_x1822( 2560, 2560) },
2346 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2351 /* get rx/tx scale vector corresponding to current speed */
2352 static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2354 struct rtl8169_private *tp = netdev_priv(dev);
2355 struct ethtool_link_ksettings ecmd;
2356 const struct rtl_coalesce_info *ci;
2359 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2363 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2364 if (ecmd.base.speed == ci->speed) {
2369 return ERR_PTR(-ELNRNG);
2372 static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2374 struct rtl8169_private *tp = netdev_priv(dev);
2375 const struct rtl_coalesce_info *ci;
2376 const struct rtl_coalesce_scale *scale;
2380 } coal_settings [] = {
2381 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2382 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2383 }, *p = coal_settings;
2387 memset(ec, 0, sizeof(*ec));
2389 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2390 ci = rtl_coalesce_info(dev);
2394 scale = &ci->scalev[RTL_R16(tp, CPlusCmd) & 3];
2396 /* read IntrMitigate and adjust according to scale */
2397 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2398 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2399 w >>= RTL_COALESCE_SHIFT;
2400 *p->usecs = w & RTL_COALESCE_MASK;
2403 for (i = 0; i < 2; i++) {
2404 p = coal_settings + i;
2405 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2408 * ethtool_coalesce says it is illegal to set both usecs and
2411 if (!*p->usecs && !*p->max_frames)
2418 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2419 static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2420 struct net_device *dev, u32 nsec, u16 *cp01)
2422 const struct rtl_coalesce_info *ci;
2425 ci = rtl_coalesce_info(dev);
2427 return ERR_CAST(ci);
2429 for (i = 0; i < 4; i++) {
2430 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2431 ci->scalev[i].nsecs[1]);
2432 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2434 return &ci->scalev[i];
2438 return ERR_PTR(-EINVAL);
2441 static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2443 struct rtl8169_private *tp = netdev_priv(dev);
2444 const struct rtl_coalesce_scale *scale;
2448 } coal_settings [] = {
2449 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2450 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2451 }, *p = coal_settings;
2455 scale = rtl_coalesce_choose_scale(dev,
2456 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2458 return PTR_ERR(scale);
2460 for (i = 0; i < 2; i++, p++) {
2464 * accept max_frames=1 we returned in rtl_get_coalesce.
2465 * accept it not only when usecs=0 because of e.g. the following scenario:
2467 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2468 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2469 * - then user does `ethtool -C eth0 rx-usecs 100`
2471 * since ethtool sends to kernel whole ethtool_coalesce
2472 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2473 * we'll reject it below in `frames % 4 != 0`.
2475 if (p->frames == 1) {
2479 units = p->usecs * 1000 / scale->nsecs[i];
2480 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2483 w <<= RTL_COALESCE_SHIFT;
2485 w <<= RTL_COALESCE_SHIFT;
2486 w |= p->frames >> 2;
2491 RTL_W16(tp, IntrMitigate, swab16(w));
2493 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
2494 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2495 RTL_R16(tp, CPlusCmd);
2497 rtl_unlock_work(tp);
2502 static const struct ethtool_ops rtl8169_ethtool_ops = {
2503 .get_drvinfo = rtl8169_get_drvinfo,
2504 .get_regs_len = rtl8169_get_regs_len,
2505 .get_link = ethtool_op_get_link,
2506 .get_coalesce = rtl_get_coalesce,
2507 .set_coalesce = rtl_set_coalesce,
2508 .get_msglevel = rtl8169_get_msglevel,
2509 .set_msglevel = rtl8169_set_msglevel,
2510 .get_regs = rtl8169_get_regs,
2511 .get_wol = rtl8169_get_wol,
2512 .set_wol = rtl8169_set_wol,
2513 .get_strings = rtl8169_get_strings,
2514 .get_sset_count = rtl8169_get_sset_count,
2515 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2516 .get_ts_info = ethtool_op_get_ts_info,
2517 .nway_reset = rtl8169_nway_reset,
2518 .get_link_ksettings = rtl8169_get_link_ksettings,
2519 .set_link_ksettings = rtl8169_set_link_ksettings,
2522 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2523 struct net_device *dev, u8 default_version)
2526 * The driver currently handles the 8168Bf and the 8168Be identically
2527 * but they can be identified more specifically through the test below
2530 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2532 * Same thing for the 8101Eb and the 8101Ec:
2534 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2536 static const struct rtl_mac_info {
2541 /* 8168EP family. */
2542 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2543 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2544 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2547 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2548 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2551 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2552 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2553 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2554 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2557 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2558 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2559 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2562 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2563 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2564 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2565 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2568 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2569 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2570 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2572 /* 8168DP family. */
2573 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2574 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2575 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2578 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
2579 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2580 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2581 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2582 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2583 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2584 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2585 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
2586 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2589 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2590 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2591 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2592 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2595 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2596 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2597 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2598 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
2599 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2600 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2601 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2602 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2603 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2604 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2605 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2606 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2607 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2608 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2609 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2610 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2611 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2612 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2613 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2614 /* FIXME: where did these entries come from ? -- FR */
2615 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2616 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2619 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2620 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2621 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2622 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2623 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2624 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2627 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2629 const struct rtl_mac_info *p = mac_info;
2632 reg = RTL_R32(tp, TxConfig);
2633 while ((reg & p->mask) != p->val)
2635 tp->mac_version = p->mac_version;
2637 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2638 netif_notice(tp, probe, dev,
2639 "unknown MAC, using family default\n");
2640 tp->mac_version = default_version;
2641 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2642 tp->mac_version = tp->mii.supports_gmii ?
2643 RTL_GIGA_MAC_VER_42 :
2644 RTL_GIGA_MAC_VER_43;
2645 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2646 tp->mac_version = tp->mii.supports_gmii ?
2647 RTL_GIGA_MAC_VER_45 :
2648 RTL_GIGA_MAC_VER_47;
2649 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2650 tp->mac_version = tp->mii.supports_gmii ?
2651 RTL_GIGA_MAC_VER_46 :
2652 RTL_GIGA_MAC_VER_48;
2656 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2658 dprintk("mac_version = 0x%02x\n", tp->mac_version);
2666 static void rtl_writephy_batch(struct rtl8169_private *tp,
2667 const struct phy_reg *regs, int len)
2670 rtl_writephy(tp, regs->reg, regs->val);
2675 #define PHY_READ 0x00000000
2676 #define PHY_DATA_OR 0x10000000
2677 #define PHY_DATA_AND 0x20000000
2678 #define PHY_BJMPN 0x30000000
2679 #define PHY_MDIO_CHG 0x40000000
2680 #define PHY_CLEAR_READCOUNT 0x70000000
2681 #define PHY_WRITE 0x80000000
2682 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2683 #define PHY_COMP_EQ_SKIPN 0xa0000000
2684 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2685 #define PHY_WRITE_PREVIOUS 0xc0000000
2686 #define PHY_SKIPN 0xd0000000
2687 #define PHY_DELAY_MS 0xe0000000
2691 char version[RTL_VER_SIZE];
2697 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2699 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2701 const struct firmware *fw = rtl_fw->fw;
2702 struct fw_info *fw_info = (struct fw_info *)fw->data;
2703 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2704 char *version = rtl_fw->version;
2707 if (fw->size < FW_OPCODE_SIZE)
2710 if (!fw_info->magic) {
2711 size_t i, size, start;
2714 if (fw->size < sizeof(*fw_info))
2717 for (i = 0; i < fw->size; i++)
2718 checksum += fw->data[i];
2722 start = le32_to_cpu(fw_info->fw_start);
2723 if (start > fw->size)
2726 size = le32_to_cpu(fw_info->fw_len);
2727 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2730 memcpy(version, fw_info->version, RTL_VER_SIZE);
2732 pa->code = (__le32 *)(fw->data + start);
2735 if (fw->size % FW_OPCODE_SIZE)
2738 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2740 pa->code = (__le32 *)fw->data;
2741 pa->size = fw->size / FW_OPCODE_SIZE;
2743 version[RTL_VER_SIZE - 1] = 0;
2750 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2751 struct rtl_fw_phy_action *pa)
2756 for (index = 0; index < pa->size; index++) {
2757 u32 action = le32_to_cpu(pa->code[index]);
2758 u32 regno = (action & 0x0fff0000) >> 16;
2760 switch(action & 0xf0000000) {
2765 case PHY_CLEAR_READCOUNT:
2767 case PHY_WRITE_PREVIOUS:
2772 if (regno > index) {
2773 netif_err(tp, ifup, tp->dev,
2774 "Out of range of firmware\n");
2778 case PHY_READCOUNT_EQ_SKIP:
2779 if (index + 2 >= pa->size) {
2780 netif_err(tp, ifup, tp->dev,
2781 "Out of range of firmware\n");
2785 case PHY_COMP_EQ_SKIPN:
2786 case PHY_COMP_NEQ_SKIPN:
2788 if (index + 1 + regno >= pa->size) {
2789 netif_err(tp, ifup, tp->dev,
2790 "Out of range of firmware\n");
2796 netif_err(tp, ifup, tp->dev,
2797 "Invalid action 0x%08x\n", action);
2806 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2808 struct net_device *dev = tp->dev;
2811 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2812 netif_err(tp, ifup, dev, "invalid firmware\n");
2816 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2822 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2824 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2825 struct mdio_ops org, *ops = &tp->mdio_ops;
2829 predata = count = 0;
2830 org.write = ops->write;
2831 org.read = ops->read;
2833 for (index = 0; index < pa->size; ) {
2834 u32 action = le32_to_cpu(pa->code[index]);
2835 u32 data = action & 0x0000ffff;
2836 u32 regno = (action & 0x0fff0000) >> 16;
2841 switch(action & 0xf0000000) {
2843 predata = rtl_readphy(tp, regno);
2860 ops->write = org.write;
2861 ops->read = org.read;
2862 } else if (data == 1) {
2863 ops->write = mac_mcu_write;
2864 ops->read = mac_mcu_read;
2869 case PHY_CLEAR_READCOUNT:
2874 rtl_writephy(tp, regno, data);
2877 case PHY_READCOUNT_EQ_SKIP:
2878 index += (count == data) ? 2 : 1;
2880 case PHY_COMP_EQ_SKIPN:
2881 if (predata == data)
2885 case PHY_COMP_NEQ_SKIPN:
2886 if (predata != data)
2890 case PHY_WRITE_PREVIOUS:
2891 rtl_writephy(tp, regno, predata);
2907 ops->write = org.write;
2908 ops->read = org.read;
2911 static void rtl_release_firmware(struct rtl8169_private *tp)
2913 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2914 release_firmware(tp->rtl_fw->fw);
2917 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2920 static void rtl_apply_firmware(struct rtl8169_private *tp)
2922 struct rtl_fw *rtl_fw = tp->rtl_fw;
2924 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2925 if (!IS_ERR_OR_NULL(rtl_fw))
2926 rtl_phy_write_fw(tp, rtl_fw);
2929 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2931 if (rtl_readphy(tp, reg) != val)
2932 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2934 rtl_apply_firmware(tp);
2937 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2939 static const struct phy_reg phy_reg_init[] = {
3001 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3004 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
3006 static const struct phy_reg phy_reg_init[] = {
3012 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3015 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
3017 struct pci_dev *pdev = tp->pci_dev;
3019 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3020 (pdev->subsystem_device != 0xe000))
3023 rtl_writephy(tp, 0x1f, 0x0001);
3024 rtl_writephy(tp, 0x10, 0xf01b);
3025 rtl_writephy(tp, 0x1f, 0x0000);
3028 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
3030 static const struct phy_reg phy_reg_init[] = {
3070 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3072 rtl8169scd_hw_phy_config_quirk(tp);
3075 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
3077 static const struct phy_reg phy_reg_init[] = {
3125 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3128 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
3130 static const struct phy_reg phy_reg_init[] = {
3135 rtl_writephy(tp, 0x1f, 0x0001);
3136 rtl_patchphy(tp, 0x16, 1 << 0);
3138 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3141 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
3143 static const struct phy_reg phy_reg_init[] = {
3149 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3152 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
3154 static const struct phy_reg phy_reg_init[] = {
3162 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3165 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
3167 static const struct phy_reg phy_reg_init[] = {
3173 rtl_writephy(tp, 0x1f, 0x0000);
3174 rtl_patchphy(tp, 0x14, 1 << 5);
3175 rtl_patchphy(tp, 0x0d, 1 << 5);
3177 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3180 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
3182 static const struct phy_reg phy_reg_init[] = {
3202 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3204 rtl_patchphy(tp, 0x14, 1 << 5);
3205 rtl_patchphy(tp, 0x0d, 1 << 5);
3206 rtl_writephy(tp, 0x1f, 0x0000);
3209 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
3211 static const struct phy_reg phy_reg_init[] = {
3229 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3231 rtl_patchphy(tp, 0x16, 1 << 0);
3232 rtl_patchphy(tp, 0x14, 1 << 5);
3233 rtl_patchphy(tp, 0x0d, 1 << 5);
3234 rtl_writephy(tp, 0x1f, 0x0000);
3237 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
3239 static const struct phy_reg phy_reg_init[] = {
3251 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3253 rtl_patchphy(tp, 0x16, 1 << 0);
3254 rtl_patchphy(tp, 0x14, 1 << 5);
3255 rtl_patchphy(tp, 0x0d, 1 << 5);
3256 rtl_writephy(tp, 0x1f, 0x0000);
3259 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
3261 rtl8168c_3_hw_phy_config(tp);
3264 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
3266 static const struct phy_reg phy_reg_init_0[] = {
3267 /* Channel Estimation */
3288 * Enhance line driver power
3297 * Can not link to 1Gbps with bad cable
3298 * Decrease SNR threshold form 21.07dB to 19.04dB
3307 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3311 * Fine Tune Switching regulator parameter
3313 rtl_writephy(tp, 0x1f, 0x0002);
3314 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3315 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
3317 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3318 static const struct phy_reg phy_reg_init[] = {
3328 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3330 val = rtl_readphy(tp, 0x0d);
3332 if ((val & 0x00ff) != 0x006c) {
3333 static const u32 set[] = {
3334 0x0065, 0x0066, 0x0067, 0x0068,
3335 0x0069, 0x006a, 0x006b, 0x006c
3339 rtl_writephy(tp, 0x1f, 0x0002);
3342 for (i = 0; i < ARRAY_SIZE(set); i++)
3343 rtl_writephy(tp, 0x0d, val | set[i]);
3346 static const struct phy_reg phy_reg_init[] = {
3354 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3357 /* RSET couple improve */
3358 rtl_writephy(tp, 0x1f, 0x0002);
3359 rtl_patchphy(tp, 0x0d, 0x0300);
3360 rtl_patchphy(tp, 0x0f, 0x0010);
3362 /* Fine tune PLL performance */
3363 rtl_writephy(tp, 0x1f, 0x0002);
3364 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3365 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3367 rtl_writephy(tp, 0x1f, 0x0005);
3368 rtl_writephy(tp, 0x05, 0x001b);
3370 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
3372 rtl_writephy(tp, 0x1f, 0x0000);
3375 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
3377 static const struct phy_reg phy_reg_init_0[] = {
3378 /* Channel Estimation */
3399 * Enhance line driver power
3408 * Can not link to 1Gbps with bad cable
3409 * Decrease SNR threshold form 21.07dB to 19.04dB
3418 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3420 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3421 static const struct phy_reg phy_reg_init[] = {
3432 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3434 val = rtl_readphy(tp, 0x0d);
3435 if ((val & 0x00ff) != 0x006c) {
3436 static const u32 set[] = {
3437 0x0065, 0x0066, 0x0067, 0x0068,
3438 0x0069, 0x006a, 0x006b, 0x006c
3442 rtl_writephy(tp, 0x1f, 0x0002);
3445 for (i = 0; i < ARRAY_SIZE(set); i++)
3446 rtl_writephy(tp, 0x0d, val | set[i]);
3449 static const struct phy_reg phy_reg_init[] = {
3457 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3460 /* Fine tune PLL performance */
3461 rtl_writephy(tp, 0x1f, 0x0002);
3462 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3463 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3465 /* Switching regulator Slew rate */
3466 rtl_writephy(tp, 0x1f, 0x0002);
3467 rtl_patchphy(tp, 0x0f, 0x0017);
3469 rtl_writephy(tp, 0x1f, 0x0005);
3470 rtl_writephy(tp, 0x05, 0x001b);
3472 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
3474 rtl_writephy(tp, 0x1f, 0x0000);
3477 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
3479 static const struct phy_reg phy_reg_init[] = {
3535 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3538 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3540 static const struct phy_reg phy_reg_init[] = {
3550 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3551 rtl_patchphy(tp, 0x0d, 1 << 5);
3554 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3556 static const struct phy_reg phy_reg_init[] = {
3557 /* Enable Delay cap */
3563 /* Channel estimation fine tune */
3572 /* Update PFM & 10M TX idle timer */
3584 rtl_apply_firmware(tp);
3586 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3588 /* DCO enable for 10M IDLE Power */
3589 rtl_writephy(tp, 0x1f, 0x0007);
3590 rtl_writephy(tp, 0x1e, 0x0023);
3591 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3592 rtl_writephy(tp, 0x1f, 0x0000);
3594 /* For impedance matching */
3595 rtl_writephy(tp, 0x1f, 0x0002);
3596 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3597 rtl_writephy(tp, 0x1f, 0x0000);
3599 /* PHY auto speed down */
3600 rtl_writephy(tp, 0x1f, 0x0007);
3601 rtl_writephy(tp, 0x1e, 0x002d);
3602 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3603 rtl_writephy(tp, 0x1f, 0x0000);
3604 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3606 rtl_writephy(tp, 0x1f, 0x0005);
3607 rtl_writephy(tp, 0x05, 0x8b86);
3608 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3609 rtl_writephy(tp, 0x1f, 0x0000);
3611 rtl_writephy(tp, 0x1f, 0x0005);
3612 rtl_writephy(tp, 0x05, 0x8b85);
3613 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3614 rtl_writephy(tp, 0x1f, 0x0007);
3615 rtl_writephy(tp, 0x1e, 0x0020);
3616 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3617 rtl_writephy(tp, 0x1f, 0x0006);
3618 rtl_writephy(tp, 0x00, 0x5a00);
3619 rtl_writephy(tp, 0x1f, 0x0000);
3620 rtl_writephy(tp, 0x0d, 0x0007);
3621 rtl_writephy(tp, 0x0e, 0x003c);
3622 rtl_writephy(tp, 0x0d, 0x4007);
3623 rtl_writephy(tp, 0x0e, 0x0000);
3624 rtl_writephy(tp, 0x0d, 0x0000);
3627 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3630 addr[0] | (addr[1] << 8),
3631 addr[2] | (addr[3] << 8),
3632 addr[4] | (addr[5] << 8)
3634 const struct exgmac_reg e[] = {
3635 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3636 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3637 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3638 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3641 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3644 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3646 static const struct phy_reg phy_reg_init[] = {
3647 /* Enable Delay cap */
3656 /* Channel estimation fine tune */
3673 rtl_apply_firmware(tp);
3675 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3677 /* For 4-corner performance improve */
3678 rtl_writephy(tp, 0x1f, 0x0005);
3679 rtl_writephy(tp, 0x05, 0x8b80);
3680 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3681 rtl_writephy(tp, 0x1f, 0x0000);
3683 /* PHY auto speed down */
3684 rtl_writephy(tp, 0x1f, 0x0004);
3685 rtl_writephy(tp, 0x1f, 0x0007);
3686 rtl_writephy(tp, 0x1e, 0x002d);
3687 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3688 rtl_writephy(tp, 0x1f, 0x0002);
3689 rtl_writephy(tp, 0x1f, 0x0000);
3690 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3692 /* improve 10M EEE waveform */
3693 rtl_writephy(tp, 0x1f, 0x0005);
3694 rtl_writephy(tp, 0x05, 0x8b86);
3695 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3696 rtl_writephy(tp, 0x1f, 0x0000);
3698 /* Improve 2-pair detection performance */
3699 rtl_writephy(tp, 0x1f, 0x0005);
3700 rtl_writephy(tp, 0x05, 0x8b85);
3701 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3702 rtl_writephy(tp, 0x1f, 0x0000);
3705 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
3706 rtl_writephy(tp, 0x1f, 0x0005);
3707 rtl_writephy(tp, 0x05, 0x8b85);
3708 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
3709 rtl_writephy(tp, 0x1f, 0x0004);
3710 rtl_writephy(tp, 0x1f, 0x0007);
3711 rtl_writephy(tp, 0x1e, 0x0020);
3712 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
3713 rtl_writephy(tp, 0x1f, 0x0002);
3714 rtl_writephy(tp, 0x1f, 0x0000);
3715 rtl_writephy(tp, 0x0d, 0x0007);
3716 rtl_writephy(tp, 0x0e, 0x003c);
3717 rtl_writephy(tp, 0x0d, 0x4007);
3718 rtl_writephy(tp, 0x0e, 0x0006);
3719 rtl_writephy(tp, 0x0d, 0x0000);
3722 rtl_writephy(tp, 0x1f, 0x0003);
3723 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3724 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
3725 rtl_writephy(tp, 0x1f, 0x0000);
3726 rtl_writephy(tp, 0x1f, 0x0005);
3727 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3728 rtl_writephy(tp, 0x1f, 0x0000);
3730 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3731 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3734 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3736 /* For 4-corner performance improve */
3737 rtl_writephy(tp, 0x1f, 0x0005);
3738 rtl_writephy(tp, 0x05, 0x8b80);
3739 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3740 rtl_writephy(tp, 0x1f, 0x0000);
3742 /* PHY auto speed down */
3743 rtl_writephy(tp, 0x1f, 0x0007);
3744 rtl_writephy(tp, 0x1e, 0x002d);
3745 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3746 rtl_writephy(tp, 0x1f, 0x0000);
3747 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3749 /* Improve 10M EEE waveform */
3750 rtl_writephy(tp, 0x1f, 0x0005);
3751 rtl_writephy(tp, 0x05, 0x8b86);
3752 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3753 rtl_writephy(tp, 0x1f, 0x0000);
3756 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3758 static const struct phy_reg phy_reg_init[] = {
3759 /* Channel estimation fine tune */
3764 /* Modify green table for giga & fnet */
3781 /* Modify green table for 10M */
3787 /* Disable hiimpedance detection (RTCT) */
3793 rtl_apply_firmware(tp);
3795 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3797 rtl8168f_hw_phy_config(tp);
3799 /* Improve 2-pair detection performance */
3800 rtl_writephy(tp, 0x1f, 0x0005);
3801 rtl_writephy(tp, 0x05, 0x8b85);
3802 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3803 rtl_writephy(tp, 0x1f, 0x0000);
3806 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3808 rtl_apply_firmware(tp);
3810 rtl8168f_hw_phy_config(tp);
3813 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3815 static const struct phy_reg phy_reg_init[] = {
3816 /* Channel estimation fine tune */
3821 /* Modify green table for giga & fnet */
3838 /* Modify green table for 10M */
3844 /* Disable hiimpedance detection (RTCT) */
3851 rtl_apply_firmware(tp);
3853 rtl8168f_hw_phy_config(tp);
3855 /* Improve 2-pair detection performance */
3856 rtl_writephy(tp, 0x1f, 0x0005);
3857 rtl_writephy(tp, 0x05, 0x8b85);
3858 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3859 rtl_writephy(tp, 0x1f, 0x0000);
3861 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3863 /* Modify green table for giga */
3864 rtl_writephy(tp, 0x1f, 0x0005);
3865 rtl_writephy(tp, 0x05, 0x8b54);
3866 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3867 rtl_writephy(tp, 0x05, 0x8b5d);
3868 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3869 rtl_writephy(tp, 0x05, 0x8a7c);
3870 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3871 rtl_writephy(tp, 0x05, 0x8a7f);
3872 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3873 rtl_writephy(tp, 0x05, 0x8a82);
3874 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3875 rtl_writephy(tp, 0x05, 0x8a85);
3876 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3877 rtl_writephy(tp, 0x05, 0x8a88);
3878 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3879 rtl_writephy(tp, 0x1f, 0x0000);
3881 /* uc same-seed solution */
3882 rtl_writephy(tp, 0x1f, 0x0005);
3883 rtl_writephy(tp, 0x05, 0x8b85);
3884 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3885 rtl_writephy(tp, 0x1f, 0x0000);
3888 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3889 rtl_writephy(tp, 0x1f, 0x0005);
3890 rtl_writephy(tp, 0x05, 0x8b85);
3891 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3892 rtl_writephy(tp, 0x1f, 0x0004);
3893 rtl_writephy(tp, 0x1f, 0x0007);
3894 rtl_writephy(tp, 0x1e, 0x0020);
3895 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3896 rtl_writephy(tp, 0x1f, 0x0000);
3897 rtl_writephy(tp, 0x0d, 0x0007);
3898 rtl_writephy(tp, 0x0e, 0x003c);
3899 rtl_writephy(tp, 0x0d, 0x4007);
3900 rtl_writephy(tp, 0x0e, 0x0000);
3901 rtl_writephy(tp, 0x0d, 0x0000);
3904 rtl_writephy(tp, 0x1f, 0x0003);
3905 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3906 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3907 rtl_writephy(tp, 0x1f, 0x0000);
3910 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3912 rtl_apply_firmware(tp);
3914 rtl_writephy(tp, 0x1f, 0x0a46);
3915 if (rtl_readphy(tp, 0x10) & 0x0100) {
3916 rtl_writephy(tp, 0x1f, 0x0bcc);
3917 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3919 rtl_writephy(tp, 0x1f, 0x0bcc);
3920 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3923 rtl_writephy(tp, 0x1f, 0x0a46);
3924 if (rtl_readphy(tp, 0x13) & 0x0100) {
3925 rtl_writephy(tp, 0x1f, 0x0c41);
3926 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3928 rtl_writephy(tp, 0x1f, 0x0c41);
3929 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3932 /* Enable PHY auto speed down */
3933 rtl_writephy(tp, 0x1f, 0x0a44);
3934 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3936 rtl_writephy(tp, 0x1f, 0x0bcc);
3937 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
3938 rtl_writephy(tp, 0x1f, 0x0a44);
3939 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3940 rtl_writephy(tp, 0x1f, 0x0a43);
3941 rtl_writephy(tp, 0x13, 0x8084);
3942 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3943 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3945 /* EEE auto-fallback function */
3946 rtl_writephy(tp, 0x1f, 0x0a4b);
3947 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3949 /* Enable UC LPF tune function */
3950 rtl_writephy(tp, 0x1f, 0x0a43);
3951 rtl_writephy(tp, 0x13, 0x8012);
3952 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3954 rtl_writephy(tp, 0x1f, 0x0c42);
3955 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3957 /* Improve SWR Efficiency */
3958 rtl_writephy(tp, 0x1f, 0x0bcd);
3959 rtl_writephy(tp, 0x14, 0x5065);
3960 rtl_writephy(tp, 0x14, 0xd065);
3961 rtl_writephy(tp, 0x1f, 0x0bc8);
3962 rtl_writephy(tp, 0x11, 0x5655);
3963 rtl_writephy(tp, 0x1f, 0x0bcd);
3964 rtl_writephy(tp, 0x14, 0x1065);
3965 rtl_writephy(tp, 0x14, 0x9065);
3966 rtl_writephy(tp, 0x14, 0x1065);
3968 /* Check ALDPS bit, disable it if enabled */
3969 rtl_writephy(tp, 0x1f, 0x0a43);
3970 if (rtl_readphy(tp, 0x10) & 0x0004)
3971 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3973 rtl_writephy(tp, 0x1f, 0x0000);
3976 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3978 rtl_apply_firmware(tp);
3981 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3986 rtl_apply_firmware(tp);
3988 /* CHN EST parameters adjust - giga master */
3989 rtl_writephy(tp, 0x1f, 0x0a43);
3990 rtl_writephy(tp, 0x13, 0x809b);
3991 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3992 rtl_writephy(tp, 0x13, 0x80a2);
3993 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3994 rtl_writephy(tp, 0x13, 0x80a4);
3995 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3996 rtl_writephy(tp, 0x13, 0x809c);
3997 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3998 rtl_writephy(tp, 0x1f, 0x0000);
4000 /* CHN EST parameters adjust - giga slave */
4001 rtl_writephy(tp, 0x1f, 0x0a43);
4002 rtl_writephy(tp, 0x13, 0x80ad);
4003 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
4004 rtl_writephy(tp, 0x13, 0x80b4);
4005 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
4006 rtl_writephy(tp, 0x13, 0x80ac);
4007 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
4008 rtl_writephy(tp, 0x1f, 0x0000);
4010 /* CHN EST parameters adjust - fnet */
4011 rtl_writephy(tp, 0x1f, 0x0a43);
4012 rtl_writephy(tp, 0x13, 0x808e);
4013 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
4014 rtl_writephy(tp, 0x13, 0x8090);
4015 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
4016 rtl_writephy(tp, 0x13, 0x8092);
4017 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
4018 rtl_writephy(tp, 0x1f, 0x0000);
4020 /* enable R-tune & PGA-retune function */
4022 rtl_writephy(tp, 0x1f, 0x0a46);
4023 data = rtl_readphy(tp, 0x13);
4026 dout_tapbin |= data;
4027 data = rtl_readphy(tp, 0x12);
4030 dout_tapbin |= data;
4031 dout_tapbin = ~(dout_tapbin^0x08);
4033 dout_tapbin &= 0xf000;
4034 rtl_writephy(tp, 0x1f, 0x0a43);
4035 rtl_writephy(tp, 0x13, 0x827a);
4036 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
4037 rtl_writephy(tp, 0x13, 0x827b);
4038 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
4039 rtl_writephy(tp, 0x13, 0x827c);
4040 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
4041 rtl_writephy(tp, 0x13, 0x827d);
4042 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
4044 rtl_writephy(tp, 0x1f, 0x0a43);
4045 rtl_writephy(tp, 0x13, 0x0811);
4046 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
4047 rtl_writephy(tp, 0x1f, 0x0a42);
4048 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
4049 rtl_writephy(tp, 0x1f, 0x0000);
4051 /* enable GPHY 10M */
4052 rtl_writephy(tp, 0x1f, 0x0a44);
4053 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
4054 rtl_writephy(tp, 0x1f, 0x0000);
4056 /* SAR ADC performance */
4057 rtl_writephy(tp, 0x1f, 0x0bca);
4058 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
4059 rtl_writephy(tp, 0x1f, 0x0000);
4061 rtl_writephy(tp, 0x1f, 0x0a43);
4062 rtl_writephy(tp, 0x13, 0x803f);
4063 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4064 rtl_writephy(tp, 0x13, 0x8047);
4065 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4066 rtl_writephy(tp, 0x13, 0x804f);
4067 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4068 rtl_writephy(tp, 0x13, 0x8057);
4069 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4070 rtl_writephy(tp, 0x13, 0x805f);
4071 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4072 rtl_writephy(tp, 0x13, 0x8067);
4073 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4074 rtl_writephy(tp, 0x13, 0x806f);
4075 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
4076 rtl_writephy(tp, 0x1f, 0x0000);
4078 /* disable phy pfm mode */
4079 rtl_writephy(tp, 0x1f, 0x0a44);
4080 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
4081 rtl_writephy(tp, 0x1f, 0x0000);
4083 /* Check ALDPS bit, disable it if enabled */
4084 rtl_writephy(tp, 0x1f, 0x0a43);
4085 if (rtl_readphy(tp, 0x10) & 0x0004)
4086 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4088 rtl_writephy(tp, 0x1f, 0x0000);
4091 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4093 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4097 rtl_apply_firmware(tp);
4099 /* CHIN EST parameter update */
4100 rtl_writephy(tp, 0x1f, 0x0a43);
4101 rtl_writephy(tp, 0x13, 0x808a);
4102 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
4103 rtl_writephy(tp, 0x1f, 0x0000);
4105 /* enable R-tune & PGA-retune function */
4106 rtl_writephy(tp, 0x1f, 0x0a43);
4107 rtl_writephy(tp, 0x13, 0x0811);
4108 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
4109 rtl_writephy(tp, 0x1f, 0x0a42);
4110 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
4111 rtl_writephy(tp, 0x1f, 0x0000);
4113 /* enable GPHY 10M */
4114 rtl_writephy(tp, 0x1f, 0x0a44);
4115 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
4116 rtl_writephy(tp, 0x1f, 0x0000);
4118 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4119 data = r8168_mac_ocp_read(tp, 0xdd02);
4120 ioffset_p3 = ((data & 0x80)>>7);
4123 data = r8168_mac_ocp_read(tp, 0xdd00);
4124 ioffset_p3 |= ((data & (0xe000))>>13);
4125 ioffset_p2 = ((data & (0x1e00))>>9);
4126 ioffset_p1 = ((data & (0x01e0))>>5);
4127 ioffset_p0 = ((data & 0x0010)>>4);
4129 ioffset_p0 |= (data & (0x07));
4130 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4132 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
4133 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
4134 rtl_writephy(tp, 0x1f, 0x0bcf);
4135 rtl_writephy(tp, 0x16, data);
4136 rtl_writephy(tp, 0x1f, 0x0000);
4139 /* Modify rlen (TX LPF corner frequency) level */
4140 rtl_writephy(tp, 0x1f, 0x0bcd);
4141 data = rtl_readphy(tp, 0x16);
4146 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4147 rtl_writephy(tp, 0x17, data);
4148 rtl_writephy(tp, 0x1f, 0x0bcd);
4149 rtl_writephy(tp, 0x1f, 0x0000);
4151 /* disable phy pfm mode */
4152 rtl_writephy(tp, 0x1f, 0x0a44);
4153 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
4154 rtl_writephy(tp, 0x1f, 0x0000);
4156 /* Check ALDPS bit, disable it if enabled */
4157 rtl_writephy(tp, 0x1f, 0x0a43);
4158 if (rtl_readphy(tp, 0x10) & 0x0004)
4159 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4161 rtl_writephy(tp, 0x1f, 0x0000);
4164 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4166 /* Enable PHY auto speed down */
4167 rtl_writephy(tp, 0x1f, 0x0a44);
4168 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4169 rtl_writephy(tp, 0x1f, 0x0000);
4171 /* patch 10M & ALDPS */
4172 rtl_writephy(tp, 0x1f, 0x0bcc);
4173 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4174 rtl_writephy(tp, 0x1f, 0x0a44);
4175 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4176 rtl_writephy(tp, 0x1f, 0x0a43);
4177 rtl_writephy(tp, 0x13, 0x8084);
4178 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4179 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4180 rtl_writephy(tp, 0x1f, 0x0000);
4182 /* Enable EEE auto-fallback function */
4183 rtl_writephy(tp, 0x1f, 0x0a4b);
4184 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4185 rtl_writephy(tp, 0x1f, 0x0000);
4187 /* Enable UC LPF tune function */
4188 rtl_writephy(tp, 0x1f, 0x0a43);
4189 rtl_writephy(tp, 0x13, 0x8012);
4190 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4191 rtl_writephy(tp, 0x1f, 0x0000);
4193 /* set rg_sel_sdm_rate */
4194 rtl_writephy(tp, 0x1f, 0x0c42);
4195 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4196 rtl_writephy(tp, 0x1f, 0x0000);
4198 /* Check ALDPS bit, disable it if enabled */
4199 rtl_writephy(tp, 0x1f, 0x0a43);
4200 if (rtl_readphy(tp, 0x10) & 0x0004)
4201 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4203 rtl_writephy(tp, 0x1f, 0x0000);
4206 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4208 /* patch 10M & ALDPS */
4209 rtl_writephy(tp, 0x1f, 0x0bcc);
4210 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4211 rtl_writephy(tp, 0x1f, 0x0a44);
4212 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4213 rtl_writephy(tp, 0x1f, 0x0a43);
4214 rtl_writephy(tp, 0x13, 0x8084);
4215 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4216 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4217 rtl_writephy(tp, 0x1f, 0x0000);
4219 /* Enable UC LPF tune function */
4220 rtl_writephy(tp, 0x1f, 0x0a43);
4221 rtl_writephy(tp, 0x13, 0x8012);
4222 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4223 rtl_writephy(tp, 0x1f, 0x0000);
4225 /* Set rg_sel_sdm_rate */
4226 rtl_writephy(tp, 0x1f, 0x0c42);
4227 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4228 rtl_writephy(tp, 0x1f, 0x0000);
4230 /* Channel estimation parameters */
4231 rtl_writephy(tp, 0x1f, 0x0a43);
4232 rtl_writephy(tp, 0x13, 0x80f3);
4233 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4234 rtl_writephy(tp, 0x13, 0x80f0);
4235 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4236 rtl_writephy(tp, 0x13, 0x80ef);
4237 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4238 rtl_writephy(tp, 0x13, 0x80f6);
4239 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4240 rtl_writephy(tp, 0x13, 0x80ec);
4241 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4242 rtl_writephy(tp, 0x13, 0x80ed);
4243 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4244 rtl_writephy(tp, 0x13, 0x80f2);
4245 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4246 rtl_writephy(tp, 0x13, 0x80f4);
4247 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4248 rtl_writephy(tp, 0x1f, 0x0a43);
4249 rtl_writephy(tp, 0x13, 0x8110);
4250 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4251 rtl_writephy(tp, 0x13, 0x810f);
4252 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4253 rtl_writephy(tp, 0x13, 0x8111);
4254 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4255 rtl_writephy(tp, 0x13, 0x8113);
4256 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4257 rtl_writephy(tp, 0x13, 0x8115);
4258 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4259 rtl_writephy(tp, 0x13, 0x810e);
4260 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4261 rtl_writephy(tp, 0x13, 0x810c);
4262 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4263 rtl_writephy(tp, 0x13, 0x810b);
4264 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4265 rtl_writephy(tp, 0x1f, 0x0a43);
4266 rtl_writephy(tp, 0x13, 0x80d1);
4267 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4268 rtl_writephy(tp, 0x13, 0x80cd);
4269 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4270 rtl_writephy(tp, 0x13, 0x80d3);
4271 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4272 rtl_writephy(tp, 0x13, 0x80d5);
4273 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4274 rtl_writephy(tp, 0x13, 0x80d7);
4275 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4277 /* Force PWM-mode */
4278 rtl_writephy(tp, 0x1f, 0x0bcd);
4279 rtl_writephy(tp, 0x14, 0x5065);
4280 rtl_writephy(tp, 0x14, 0xd065);
4281 rtl_writephy(tp, 0x1f, 0x0bc8);
4282 rtl_writephy(tp, 0x12, 0x00ed);
4283 rtl_writephy(tp, 0x1f, 0x0bcd);
4284 rtl_writephy(tp, 0x14, 0x1065);
4285 rtl_writephy(tp, 0x14, 0x9065);
4286 rtl_writephy(tp, 0x14, 0x1065);
4287 rtl_writephy(tp, 0x1f, 0x0000);
4289 /* Check ALDPS bit, disable it if enabled */
4290 rtl_writephy(tp, 0x1f, 0x0a43);
4291 if (rtl_readphy(tp, 0x10) & 0x0004)
4292 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4294 rtl_writephy(tp, 0x1f, 0x0000);
4297 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
4299 static const struct phy_reg phy_reg_init[] = {
4306 rtl_writephy(tp, 0x1f, 0x0000);
4307 rtl_patchphy(tp, 0x11, 1 << 12);
4308 rtl_patchphy(tp, 0x19, 1 << 13);
4309 rtl_patchphy(tp, 0x10, 1 << 15);
4311 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4314 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4316 static const struct phy_reg phy_reg_init[] = {
4330 /* Disable ALDPS before ram code */
4331 rtl_writephy(tp, 0x1f, 0x0000);
4332 rtl_writephy(tp, 0x18, 0x0310);
4335 rtl_apply_firmware(tp);
4337 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4340 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4342 /* Disable ALDPS before setting firmware */
4343 rtl_writephy(tp, 0x1f, 0x0000);
4344 rtl_writephy(tp, 0x18, 0x0310);
4347 rtl_apply_firmware(tp);
4350 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4351 rtl_writephy(tp, 0x1f, 0x0004);
4352 rtl_writephy(tp, 0x10, 0x401f);
4353 rtl_writephy(tp, 0x19, 0x7030);
4354 rtl_writephy(tp, 0x1f, 0x0000);
4357 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4359 static const struct phy_reg phy_reg_init[] = {
4366 /* Disable ALDPS before ram code */
4367 rtl_writephy(tp, 0x1f, 0x0000);
4368 rtl_writephy(tp, 0x18, 0x0310);
4371 rtl_apply_firmware(tp);
4373 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4374 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4376 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4379 static void rtl_hw_phy_config(struct net_device *dev)
4381 struct rtl8169_private *tp = netdev_priv(dev);
4383 rtl8169_print_mac_version(tp);
4385 switch (tp->mac_version) {
4386 case RTL_GIGA_MAC_VER_01:
4388 case RTL_GIGA_MAC_VER_02:
4389 case RTL_GIGA_MAC_VER_03:
4390 rtl8169s_hw_phy_config(tp);
4392 case RTL_GIGA_MAC_VER_04:
4393 rtl8169sb_hw_phy_config(tp);
4395 case RTL_GIGA_MAC_VER_05:
4396 rtl8169scd_hw_phy_config(tp);
4398 case RTL_GIGA_MAC_VER_06:
4399 rtl8169sce_hw_phy_config(tp);
4401 case RTL_GIGA_MAC_VER_07:
4402 case RTL_GIGA_MAC_VER_08:
4403 case RTL_GIGA_MAC_VER_09:
4404 rtl8102e_hw_phy_config(tp);
4406 case RTL_GIGA_MAC_VER_11:
4407 rtl8168bb_hw_phy_config(tp);
4409 case RTL_GIGA_MAC_VER_12:
4410 rtl8168bef_hw_phy_config(tp);
4412 case RTL_GIGA_MAC_VER_17:
4413 rtl8168bef_hw_phy_config(tp);
4415 case RTL_GIGA_MAC_VER_18:
4416 rtl8168cp_1_hw_phy_config(tp);
4418 case RTL_GIGA_MAC_VER_19:
4419 rtl8168c_1_hw_phy_config(tp);
4421 case RTL_GIGA_MAC_VER_20:
4422 rtl8168c_2_hw_phy_config(tp);
4424 case RTL_GIGA_MAC_VER_21:
4425 rtl8168c_3_hw_phy_config(tp);
4427 case RTL_GIGA_MAC_VER_22:
4428 rtl8168c_4_hw_phy_config(tp);
4430 case RTL_GIGA_MAC_VER_23:
4431 case RTL_GIGA_MAC_VER_24:
4432 rtl8168cp_2_hw_phy_config(tp);
4434 case RTL_GIGA_MAC_VER_25:
4435 rtl8168d_1_hw_phy_config(tp);
4437 case RTL_GIGA_MAC_VER_26:
4438 rtl8168d_2_hw_phy_config(tp);
4440 case RTL_GIGA_MAC_VER_27:
4441 rtl8168d_3_hw_phy_config(tp);
4443 case RTL_GIGA_MAC_VER_28:
4444 rtl8168d_4_hw_phy_config(tp);
4446 case RTL_GIGA_MAC_VER_29:
4447 case RTL_GIGA_MAC_VER_30:
4448 rtl8105e_hw_phy_config(tp);
4450 case RTL_GIGA_MAC_VER_31:
4453 case RTL_GIGA_MAC_VER_32:
4454 case RTL_GIGA_MAC_VER_33:
4455 rtl8168e_1_hw_phy_config(tp);
4457 case RTL_GIGA_MAC_VER_34:
4458 rtl8168e_2_hw_phy_config(tp);
4460 case RTL_GIGA_MAC_VER_35:
4461 rtl8168f_1_hw_phy_config(tp);
4463 case RTL_GIGA_MAC_VER_36:
4464 rtl8168f_2_hw_phy_config(tp);
4467 case RTL_GIGA_MAC_VER_37:
4468 rtl8402_hw_phy_config(tp);
4471 case RTL_GIGA_MAC_VER_38:
4472 rtl8411_hw_phy_config(tp);
4475 case RTL_GIGA_MAC_VER_39:
4476 rtl8106e_hw_phy_config(tp);
4479 case RTL_GIGA_MAC_VER_40:
4480 rtl8168g_1_hw_phy_config(tp);
4482 case RTL_GIGA_MAC_VER_42:
4483 case RTL_GIGA_MAC_VER_43:
4484 case RTL_GIGA_MAC_VER_44:
4485 rtl8168g_2_hw_phy_config(tp);
4487 case RTL_GIGA_MAC_VER_45:
4488 case RTL_GIGA_MAC_VER_47:
4489 rtl8168h_1_hw_phy_config(tp);
4491 case RTL_GIGA_MAC_VER_46:
4492 case RTL_GIGA_MAC_VER_48:
4493 rtl8168h_2_hw_phy_config(tp);
4496 case RTL_GIGA_MAC_VER_49:
4497 rtl8168ep_1_hw_phy_config(tp);
4499 case RTL_GIGA_MAC_VER_50:
4500 case RTL_GIGA_MAC_VER_51:
4501 rtl8168ep_2_hw_phy_config(tp);
4504 case RTL_GIGA_MAC_VER_41:
4510 static void rtl_phy_work(struct rtl8169_private *tp)
4512 struct timer_list *timer = &tp->timer;
4513 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4515 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
4517 if (tp->phy_reset_pending(tp)) {
4519 * A busy loop could burn quite a few cycles on nowadays CPU.
4520 * Let's delay the execution of the timer for a few ticks.
4526 if (tp->link_ok(tp))
4529 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
4531 tp->phy_reset_enable(tp);
4534 mod_timer(timer, jiffies + timeout);
4537 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4539 if (!test_and_set_bit(flag, tp->wk.flags))
4540 schedule_work(&tp->wk.work);
4543 static void rtl8169_phy_timer(struct timer_list *t)
4545 struct rtl8169_private *tp = from_timer(tp, t, timer);
4547 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
4550 DECLARE_RTL_COND(rtl_phy_reset_cond)
4552 return tp->phy_reset_pending(tp);
4555 static void rtl8169_phy_reset(struct net_device *dev,
4556 struct rtl8169_private *tp)
4558 tp->phy_reset_enable(tp);
4559 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
4562 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4564 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4565 (RTL_R8(tp, PHYstatus) & TBI_Enable);
4568 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4570 rtl_hw_phy_config(dev);
4572 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4573 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4574 RTL_W8(tp, 0x82, 0x01);
4577 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4579 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4580 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4582 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
4583 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4584 RTL_W8(tp, 0x82, 0x01);
4585 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
4586 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
4589 rtl8169_phy_reset(dev, tp);
4591 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4592 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4593 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4594 (tp->mii.supports_gmii ?
4595 ADVERTISED_1000baseT_Half |
4596 ADVERTISED_1000baseT_Full : 0));
4598 if (rtl_tbi_enabled(tp))
4599 netif_info(tp, link, dev, "TBI auto-negotiating\n");
4602 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4606 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4608 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4611 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4614 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4615 rtl_rar_exgmac_set(tp, addr);
4617 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4619 rtl_unlock_work(tp);
4622 static int rtl_set_mac_address(struct net_device *dev, void *p)
4624 struct rtl8169_private *tp = netdev_priv(dev);
4625 struct device *d = tp_to_dev(tp);
4628 ret = eth_mac_addr(dev, p);
4632 pm_runtime_get_noresume(d);
4634 if (pm_runtime_active(d))
4635 rtl_rar_set(tp, dev->dev_addr);
4637 pm_runtime_put_noidle(d);
4642 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4644 struct rtl8169_private *tp = netdev_priv(dev);
4645 struct mii_ioctl_data *data = if_mii(ifr);
4647 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4650 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4651 struct mii_ioctl_data *data, int cmd)
4655 data->phy_id = 32; /* Internal PHY */
4659 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
4663 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
4669 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4674 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4676 struct mdio_ops *ops = &tp->mdio_ops;
4678 switch (tp->mac_version) {
4679 case RTL_GIGA_MAC_VER_27:
4680 ops->write = r8168dp_1_mdio_write;
4681 ops->read = r8168dp_1_mdio_read;
4683 case RTL_GIGA_MAC_VER_28:
4684 case RTL_GIGA_MAC_VER_31:
4685 ops->write = r8168dp_2_mdio_write;
4686 ops->read = r8168dp_2_mdio_read;
4688 case RTL_GIGA_MAC_VER_40:
4689 case RTL_GIGA_MAC_VER_41:
4690 case RTL_GIGA_MAC_VER_42:
4691 case RTL_GIGA_MAC_VER_43:
4692 case RTL_GIGA_MAC_VER_44:
4693 case RTL_GIGA_MAC_VER_45:
4694 case RTL_GIGA_MAC_VER_46:
4695 case RTL_GIGA_MAC_VER_47:
4696 case RTL_GIGA_MAC_VER_48:
4697 case RTL_GIGA_MAC_VER_49:
4698 case RTL_GIGA_MAC_VER_50:
4699 case RTL_GIGA_MAC_VER_51:
4700 ops->write = r8168g_mdio_write;
4701 ops->read = r8168g_mdio_read;
4704 ops->write = r8169_mdio_write;
4705 ops->read = r8169_mdio_read;
4710 static void rtl_speed_down(struct rtl8169_private *tp)
4715 rtl_writephy(tp, 0x1f, 0x0000);
4716 lpa = rtl_readphy(tp, MII_LPA);
4718 if (lpa & (LPA_10HALF | LPA_10FULL))
4719 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4720 else if (lpa & (LPA_100HALF | LPA_100FULL))
4721 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4722 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4724 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4725 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4726 (tp->mii.supports_gmii ?
4727 ADVERTISED_1000baseT_Half |
4728 ADVERTISED_1000baseT_Full : 0);
4730 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4734 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4736 switch (tp->mac_version) {
4737 case RTL_GIGA_MAC_VER_25:
4738 case RTL_GIGA_MAC_VER_26:
4739 case RTL_GIGA_MAC_VER_29:
4740 case RTL_GIGA_MAC_VER_30:
4741 case RTL_GIGA_MAC_VER_32:
4742 case RTL_GIGA_MAC_VER_33:
4743 case RTL_GIGA_MAC_VER_34:
4744 case RTL_GIGA_MAC_VER_37:
4745 case RTL_GIGA_MAC_VER_38:
4746 case RTL_GIGA_MAC_VER_39:
4747 case RTL_GIGA_MAC_VER_40:
4748 case RTL_GIGA_MAC_VER_41:
4749 case RTL_GIGA_MAC_VER_42:
4750 case RTL_GIGA_MAC_VER_43:
4751 case RTL_GIGA_MAC_VER_44:
4752 case RTL_GIGA_MAC_VER_45:
4753 case RTL_GIGA_MAC_VER_46:
4754 case RTL_GIGA_MAC_VER_47:
4755 case RTL_GIGA_MAC_VER_48:
4756 case RTL_GIGA_MAC_VER_49:
4757 case RTL_GIGA_MAC_VER_50:
4758 case RTL_GIGA_MAC_VER_51:
4759 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
4760 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4767 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4769 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4773 rtl_wol_suspend_quirk(tp);
4778 static void r810x_phy_power_down(struct rtl8169_private *tp)
4780 rtl_writephy(tp, 0x1f, 0x0000);
4781 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4784 static void r810x_phy_power_up(struct rtl8169_private *tp)
4786 rtl_writephy(tp, 0x1f, 0x0000);
4787 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4790 static void r810x_pll_power_down(struct rtl8169_private *tp)
4792 if (rtl_wol_pll_power_down(tp))
4795 r810x_phy_power_down(tp);
4797 switch (tp->mac_version) {
4798 case RTL_GIGA_MAC_VER_07:
4799 case RTL_GIGA_MAC_VER_08:
4800 case RTL_GIGA_MAC_VER_09:
4801 case RTL_GIGA_MAC_VER_10:
4802 case RTL_GIGA_MAC_VER_13:
4803 case RTL_GIGA_MAC_VER_16:
4806 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4811 static void r810x_pll_power_up(struct rtl8169_private *tp)
4813 r810x_phy_power_up(tp);
4815 switch (tp->mac_version) {
4816 case RTL_GIGA_MAC_VER_07:
4817 case RTL_GIGA_MAC_VER_08:
4818 case RTL_GIGA_MAC_VER_09:
4819 case RTL_GIGA_MAC_VER_10:
4820 case RTL_GIGA_MAC_VER_13:
4821 case RTL_GIGA_MAC_VER_16:
4823 case RTL_GIGA_MAC_VER_47:
4824 case RTL_GIGA_MAC_VER_48:
4825 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4828 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4833 static void r8168_phy_power_up(struct rtl8169_private *tp)
4835 rtl_writephy(tp, 0x1f, 0x0000);
4836 switch (tp->mac_version) {
4837 case RTL_GIGA_MAC_VER_11:
4838 case RTL_GIGA_MAC_VER_12:
4839 case RTL_GIGA_MAC_VER_17:
4840 case RTL_GIGA_MAC_VER_18:
4841 case RTL_GIGA_MAC_VER_19:
4842 case RTL_GIGA_MAC_VER_20:
4843 case RTL_GIGA_MAC_VER_21:
4844 case RTL_GIGA_MAC_VER_22:
4845 case RTL_GIGA_MAC_VER_23:
4846 case RTL_GIGA_MAC_VER_24:
4847 case RTL_GIGA_MAC_VER_25:
4848 case RTL_GIGA_MAC_VER_26:
4849 case RTL_GIGA_MAC_VER_27:
4850 case RTL_GIGA_MAC_VER_28:
4851 case RTL_GIGA_MAC_VER_31:
4852 rtl_writephy(tp, 0x0e, 0x0000);
4857 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4860 static void r8168_phy_power_down(struct rtl8169_private *tp)
4862 rtl_writephy(tp, 0x1f, 0x0000);
4863 switch (tp->mac_version) {
4864 case RTL_GIGA_MAC_VER_32:
4865 case RTL_GIGA_MAC_VER_33:
4866 case RTL_GIGA_MAC_VER_40:
4867 case RTL_GIGA_MAC_VER_41:
4868 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4871 case RTL_GIGA_MAC_VER_11:
4872 case RTL_GIGA_MAC_VER_12:
4873 case RTL_GIGA_MAC_VER_17:
4874 case RTL_GIGA_MAC_VER_18:
4875 case RTL_GIGA_MAC_VER_19:
4876 case RTL_GIGA_MAC_VER_20:
4877 case RTL_GIGA_MAC_VER_21:
4878 case RTL_GIGA_MAC_VER_22:
4879 case RTL_GIGA_MAC_VER_23:
4880 case RTL_GIGA_MAC_VER_24:
4881 case RTL_GIGA_MAC_VER_25:
4882 case RTL_GIGA_MAC_VER_26:
4883 case RTL_GIGA_MAC_VER_27:
4884 case RTL_GIGA_MAC_VER_28:
4885 case RTL_GIGA_MAC_VER_31:
4886 rtl_writephy(tp, 0x0e, 0x0200);
4888 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4893 static void r8168_pll_power_down(struct rtl8169_private *tp)
4895 if (r8168_check_dash(tp))
4898 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4899 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
4900 (RTL_R16(tp, CPlusCmd) & ASF)) {
4904 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4905 tp->mac_version == RTL_GIGA_MAC_VER_33)
4906 rtl_ephy_write(tp, 0x19, 0xff64);
4908 if (rtl_wol_pll_power_down(tp))
4911 r8168_phy_power_down(tp);
4913 switch (tp->mac_version) {
4914 case RTL_GIGA_MAC_VER_25:
4915 case RTL_GIGA_MAC_VER_26:
4916 case RTL_GIGA_MAC_VER_27:
4917 case RTL_GIGA_MAC_VER_28:
4918 case RTL_GIGA_MAC_VER_31:
4919 case RTL_GIGA_MAC_VER_32:
4920 case RTL_GIGA_MAC_VER_33:
4921 case RTL_GIGA_MAC_VER_44:
4922 case RTL_GIGA_MAC_VER_45:
4923 case RTL_GIGA_MAC_VER_46:
4924 case RTL_GIGA_MAC_VER_50:
4925 case RTL_GIGA_MAC_VER_51:
4926 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4928 case RTL_GIGA_MAC_VER_40:
4929 case RTL_GIGA_MAC_VER_41:
4930 case RTL_GIGA_MAC_VER_49:
4931 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4932 0xfc000000, ERIAR_EXGMAC);
4933 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4938 static void r8168_pll_power_up(struct rtl8169_private *tp)
4940 switch (tp->mac_version) {
4941 case RTL_GIGA_MAC_VER_25:
4942 case RTL_GIGA_MAC_VER_26:
4943 case RTL_GIGA_MAC_VER_27:
4944 case RTL_GIGA_MAC_VER_28:
4945 case RTL_GIGA_MAC_VER_31:
4946 case RTL_GIGA_MAC_VER_32:
4947 case RTL_GIGA_MAC_VER_33:
4948 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4950 case RTL_GIGA_MAC_VER_44:
4951 case RTL_GIGA_MAC_VER_45:
4952 case RTL_GIGA_MAC_VER_46:
4953 case RTL_GIGA_MAC_VER_50:
4954 case RTL_GIGA_MAC_VER_51:
4955 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4957 case RTL_GIGA_MAC_VER_40:
4958 case RTL_GIGA_MAC_VER_41:
4959 case RTL_GIGA_MAC_VER_49:
4960 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4961 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4962 0x00000000, ERIAR_EXGMAC);
4966 r8168_phy_power_up(tp);
4969 static void rtl_generic_op(struct rtl8169_private *tp,
4970 void (*op)(struct rtl8169_private *))
4976 static void rtl_pll_power_down(struct rtl8169_private *tp)
4978 rtl_generic_op(tp, tp->pll_power_ops.down);
4981 static void rtl_pll_power_up(struct rtl8169_private *tp)
4983 rtl_generic_op(tp, tp->pll_power_ops.up);
4986 static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
4988 struct pll_power_ops *ops = &tp->pll_power_ops;
4990 switch (tp->mac_version) {
4991 case RTL_GIGA_MAC_VER_07:
4992 case RTL_GIGA_MAC_VER_08:
4993 case RTL_GIGA_MAC_VER_09:
4994 case RTL_GIGA_MAC_VER_10:
4995 case RTL_GIGA_MAC_VER_16:
4996 case RTL_GIGA_MAC_VER_29:
4997 case RTL_GIGA_MAC_VER_30:
4998 case RTL_GIGA_MAC_VER_37:
4999 case RTL_GIGA_MAC_VER_39:
5000 case RTL_GIGA_MAC_VER_43:
5001 case RTL_GIGA_MAC_VER_47:
5002 case RTL_GIGA_MAC_VER_48:
5003 ops->down = r810x_pll_power_down;
5004 ops->up = r810x_pll_power_up;
5007 case RTL_GIGA_MAC_VER_11:
5008 case RTL_GIGA_MAC_VER_12:
5009 case RTL_GIGA_MAC_VER_17:
5010 case RTL_GIGA_MAC_VER_18:
5011 case RTL_GIGA_MAC_VER_19:
5012 case RTL_GIGA_MAC_VER_20:
5013 case RTL_GIGA_MAC_VER_21:
5014 case RTL_GIGA_MAC_VER_22:
5015 case RTL_GIGA_MAC_VER_23:
5016 case RTL_GIGA_MAC_VER_24:
5017 case RTL_GIGA_MAC_VER_25:
5018 case RTL_GIGA_MAC_VER_26:
5019 case RTL_GIGA_MAC_VER_27:
5020 case RTL_GIGA_MAC_VER_28:
5021 case RTL_GIGA_MAC_VER_31:
5022 case RTL_GIGA_MAC_VER_32:
5023 case RTL_GIGA_MAC_VER_33:
5024 case RTL_GIGA_MAC_VER_34:
5025 case RTL_GIGA_MAC_VER_35:
5026 case RTL_GIGA_MAC_VER_36:
5027 case RTL_GIGA_MAC_VER_38:
5028 case RTL_GIGA_MAC_VER_40:
5029 case RTL_GIGA_MAC_VER_41:
5030 case RTL_GIGA_MAC_VER_42:
5031 case RTL_GIGA_MAC_VER_44:
5032 case RTL_GIGA_MAC_VER_45:
5033 case RTL_GIGA_MAC_VER_46:
5034 case RTL_GIGA_MAC_VER_49:
5035 case RTL_GIGA_MAC_VER_50:
5036 case RTL_GIGA_MAC_VER_51:
5037 ops->down = r8168_pll_power_down;
5038 ops->up = r8168_pll_power_up;
5048 static void rtl_init_rxcfg(struct rtl8169_private *tp)
5050 switch (tp->mac_version) {
5051 case RTL_GIGA_MAC_VER_01:
5052 case RTL_GIGA_MAC_VER_02:
5053 case RTL_GIGA_MAC_VER_03:
5054 case RTL_GIGA_MAC_VER_04:
5055 case RTL_GIGA_MAC_VER_05:
5056 case RTL_GIGA_MAC_VER_06:
5057 case RTL_GIGA_MAC_VER_10:
5058 case RTL_GIGA_MAC_VER_11:
5059 case RTL_GIGA_MAC_VER_12:
5060 case RTL_GIGA_MAC_VER_13:
5061 case RTL_GIGA_MAC_VER_14:
5062 case RTL_GIGA_MAC_VER_15:
5063 case RTL_GIGA_MAC_VER_16:
5064 case RTL_GIGA_MAC_VER_17:
5065 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5067 case RTL_GIGA_MAC_VER_18:
5068 case RTL_GIGA_MAC_VER_19:
5069 case RTL_GIGA_MAC_VER_20:
5070 case RTL_GIGA_MAC_VER_21:
5071 case RTL_GIGA_MAC_VER_22:
5072 case RTL_GIGA_MAC_VER_23:
5073 case RTL_GIGA_MAC_VER_24:
5074 case RTL_GIGA_MAC_VER_34:
5075 case RTL_GIGA_MAC_VER_35:
5076 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5078 case RTL_GIGA_MAC_VER_40:
5079 case RTL_GIGA_MAC_VER_41:
5080 case RTL_GIGA_MAC_VER_42:
5081 case RTL_GIGA_MAC_VER_43:
5082 case RTL_GIGA_MAC_VER_44:
5083 case RTL_GIGA_MAC_VER_45:
5084 case RTL_GIGA_MAC_VER_46:
5085 case RTL_GIGA_MAC_VER_47:
5086 case RTL_GIGA_MAC_VER_48:
5087 case RTL_GIGA_MAC_VER_49:
5088 case RTL_GIGA_MAC_VER_50:
5089 case RTL_GIGA_MAC_VER_51:
5090 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
5093 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
5098 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5100 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
5103 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5105 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5106 rtl_generic_op(tp, tp->jumbo_ops.enable);
5107 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5110 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5112 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5113 rtl_generic_op(tp, tp->jumbo_ops.disable);
5114 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5117 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5119 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5120 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
5121 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
5124 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5126 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5127 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
5128 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5131 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5133 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5136 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5138 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5141 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5143 RTL_W8(tp, MaxTxPacketSize, 0x3f);
5144 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5145 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
5146 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
5149 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5151 RTL_W8(tp, MaxTxPacketSize, 0x0c);
5152 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5153 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
5154 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5157 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5159 rtl_tx_performance_tweak(tp,
5160 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
5163 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5165 rtl_tx_performance_tweak(tp,
5166 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5169 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5171 r8168b_0_hw_jumbo_enable(tp);
5173 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
5176 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5178 r8168b_0_hw_jumbo_disable(tp);
5180 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
5183 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
5185 struct jumbo_ops *ops = &tp->jumbo_ops;
5187 switch (tp->mac_version) {
5188 case RTL_GIGA_MAC_VER_11:
5189 ops->disable = r8168b_0_hw_jumbo_disable;
5190 ops->enable = r8168b_0_hw_jumbo_enable;
5192 case RTL_GIGA_MAC_VER_12:
5193 case RTL_GIGA_MAC_VER_17:
5194 ops->disable = r8168b_1_hw_jumbo_disable;
5195 ops->enable = r8168b_1_hw_jumbo_enable;
5197 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5198 case RTL_GIGA_MAC_VER_19:
5199 case RTL_GIGA_MAC_VER_20:
5200 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5201 case RTL_GIGA_MAC_VER_22:
5202 case RTL_GIGA_MAC_VER_23:
5203 case RTL_GIGA_MAC_VER_24:
5204 case RTL_GIGA_MAC_VER_25:
5205 case RTL_GIGA_MAC_VER_26:
5206 ops->disable = r8168c_hw_jumbo_disable;
5207 ops->enable = r8168c_hw_jumbo_enable;
5209 case RTL_GIGA_MAC_VER_27:
5210 case RTL_GIGA_MAC_VER_28:
5211 ops->disable = r8168dp_hw_jumbo_disable;
5212 ops->enable = r8168dp_hw_jumbo_enable;
5214 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5215 case RTL_GIGA_MAC_VER_32:
5216 case RTL_GIGA_MAC_VER_33:
5217 case RTL_GIGA_MAC_VER_34:
5218 ops->disable = r8168e_hw_jumbo_disable;
5219 ops->enable = r8168e_hw_jumbo_enable;
5223 * No action needed for jumbo frames with 8169.
5224 * No jumbo for 810x at all.
5226 case RTL_GIGA_MAC_VER_40:
5227 case RTL_GIGA_MAC_VER_41:
5228 case RTL_GIGA_MAC_VER_42:
5229 case RTL_GIGA_MAC_VER_43:
5230 case RTL_GIGA_MAC_VER_44:
5231 case RTL_GIGA_MAC_VER_45:
5232 case RTL_GIGA_MAC_VER_46:
5233 case RTL_GIGA_MAC_VER_47:
5234 case RTL_GIGA_MAC_VER_48:
5235 case RTL_GIGA_MAC_VER_49:
5236 case RTL_GIGA_MAC_VER_50:
5237 case RTL_GIGA_MAC_VER_51:
5239 ops->disable = NULL;
5245 DECLARE_RTL_COND(rtl_chipcmd_cond)
5247 return RTL_R8(tp, ChipCmd) & CmdReset;
5250 static void rtl_hw_reset(struct rtl8169_private *tp)
5252 RTL_W8(tp, ChipCmd, CmdReset);
5254 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
5257 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5259 struct rtl_fw *rtl_fw;
5263 name = rtl_lookup_firmware_name(tp);
5265 goto out_no_firmware;
5267 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5271 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
5275 rc = rtl_check_firmware(tp, rtl_fw);
5277 goto err_release_firmware;
5279 tp->rtl_fw = rtl_fw;
5283 err_release_firmware:
5284 release_firmware(rtl_fw->fw);
5288 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5295 static void rtl_request_firmware(struct rtl8169_private *tp)
5297 if (IS_ERR(tp->rtl_fw))
5298 rtl_request_uncached_firmware(tp);
5301 static void rtl_rx_close(struct rtl8169_private *tp)
5303 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
5306 DECLARE_RTL_COND(rtl_npq_cond)
5308 return RTL_R8(tp, TxPoll) & NPQ;
5311 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5313 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
5316 static void rtl8169_hw_reset(struct rtl8169_private *tp)
5318 /* Disable interrupts */
5319 rtl8169_irq_mask_and_ack(tp);
5323 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5324 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5325 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5326 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
5327 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
5328 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5329 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5330 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5331 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5332 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5333 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5334 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5335 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5336 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5337 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5338 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5339 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
5340 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5341 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5342 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5343 tp->mac_version == RTL_GIGA_MAC_VER_51) {
5344 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
5345 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
5347 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
5354 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
5356 /* Set DMA burst size and Interframe Gap Time */
5357 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
5358 (InterFrameGap << TxInterFrameGapShift));
5361 static void rtl_hw_start(struct net_device *dev)
5363 struct rtl8169_private *tp = netdev_priv(dev);
5367 rtl_irq_enable_all(tp);
5370 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
5373 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5374 * register to be written before TxDescAddrLow to work.
5375 * Switching from MMIO to I/O access fixes the issue as well.
5377 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5378 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5379 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5380 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
5383 static u16 rtl_rw_cpluscmd(struct rtl8169_private *tp)
5387 cmd = RTL_R16(tp, CPlusCmd);
5388 RTL_W16(tp, CPlusCmd, cmd);
5392 static void rtl_set_rx_max_size(struct rtl8169_private *tp, unsigned int rx_buf_sz)
5394 /* Low hurts. Let's disable the filtering. */
5395 RTL_W16(tp, RxMaxSize, rx_buf_sz + 1);
5398 static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
5400 static const struct rtl_cfg2_info {
5405 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5406 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5407 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5408 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
5410 const struct rtl_cfg2_info *p = cfg2_info;
5414 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
5415 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
5416 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5417 RTL_W32(tp, 0x7c, p->val);
5423 static void rtl_set_rx_mode(struct net_device *dev)
5425 struct rtl8169_private *tp = netdev_priv(dev);
5426 u32 mc_filter[2]; /* Multicast hash filter */
5430 if (dev->flags & IFF_PROMISC) {
5431 /* Unconditionally log net taps. */
5432 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5434 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5436 mc_filter[1] = mc_filter[0] = 0xffffffff;
5437 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5438 (dev->flags & IFF_ALLMULTI)) {
5439 /* Too many to filter perfectly -- accept all multicasts. */
5440 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5441 mc_filter[1] = mc_filter[0] = 0xffffffff;
5443 struct netdev_hw_addr *ha;
5445 rx_mode = AcceptBroadcast | AcceptMyPhys;
5446 mc_filter[1] = mc_filter[0] = 0;
5447 netdev_for_each_mc_addr(ha, dev) {
5448 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5449 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5450 rx_mode |= AcceptMulticast;
5454 if (dev->features & NETIF_F_RXALL)
5455 rx_mode |= (AcceptErr | AcceptRunt);
5457 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5459 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5460 u32 data = mc_filter[0];
5462 mc_filter[0] = swab32(mc_filter[1]);
5463 mc_filter[1] = swab32(data);
5466 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5467 mc_filter[1] = mc_filter[0] = 0xffffffff;
5469 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5470 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
5472 RTL_W32(tp, RxConfig, tmp);
5475 static void rtl_hw_start_8169(struct net_device *dev)
5477 struct rtl8169_private *tp = netdev_priv(dev);
5478 struct pci_dev *pdev = tp->pci_dev;
5480 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5481 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) | PCIMulRW);
5482 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5485 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5486 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5487 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5488 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5489 tp->mac_version == RTL_GIGA_MAC_VER_04)
5490 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5494 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
5496 rtl_set_rx_max_size(tp, rx_buf_sz);
5498 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5499 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5500 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5501 tp->mac_version == RTL_GIGA_MAC_VER_04)
5502 rtl_set_rx_tx_config_registers(tp);
5504 tp->cp_cmd |= rtl_rw_cpluscmd(tp) | PCIMulRW;
5506 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5507 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5508 dprintk("Set MAC Reg C+CR Offset 0xe0. "
5509 "Bit-3 and bit-14 MUST be 1\n");
5510 tp->cp_cmd |= (1 << 14);
5513 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5515 rtl8169_set_magic_reg(tp, tp->mac_version);
5518 * Undocumented corner. Supposedly:
5519 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5521 RTL_W16(tp, IntrMitigate, 0x0000);
5523 rtl_set_rx_tx_desc_registers(tp);
5525 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5526 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5527 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5528 tp->mac_version != RTL_GIGA_MAC_VER_04) {
5529 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5530 rtl_set_rx_tx_config_registers(tp);
5533 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5535 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5536 RTL_R8(tp, IntrMask);
5538 RTL_W32(tp, RxMissed, 0);
5540 rtl_set_rx_mode(dev);
5542 /* no early-rx interrupts */
5543 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5546 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5548 if (tp->csi_ops.write)
5549 tp->csi_ops.write(tp, addr, value);
5552 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5554 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
5557 static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
5561 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5562 rtl_csi_write(tp, 0x070c, csi | bits);
5565 static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
5567 rtl_csi_access_enable(tp, 0x17000000);
5570 static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
5572 rtl_csi_access_enable(tp, 0x27000000);
5575 DECLARE_RTL_COND(rtl_csiar_cond)
5577 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
5580 static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
5582 RTL_W32(tp, CSIDR, value);
5583 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5584 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5586 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5589 static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
5591 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) |
5592 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5594 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5595 RTL_R32(tp, CSIDR) : ~0;
5598 static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
5600 RTL_W32(tp, CSIDR, value);
5601 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5602 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5605 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5608 static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
5610 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5611 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5613 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5614 RTL_R32(tp, CSIDR) : ~0;
5617 static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5619 RTL_W32(tp, CSIDR, value);
5620 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5621 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5624 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5627 static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5629 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5630 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5632 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5633 RTL_R32(tp, CSIDR) : ~0;
5636 static void rtl_init_csi_ops(struct rtl8169_private *tp)
5638 struct csi_ops *ops = &tp->csi_ops;
5640 switch (tp->mac_version) {
5641 case RTL_GIGA_MAC_VER_01:
5642 case RTL_GIGA_MAC_VER_02:
5643 case RTL_GIGA_MAC_VER_03:
5644 case RTL_GIGA_MAC_VER_04:
5645 case RTL_GIGA_MAC_VER_05:
5646 case RTL_GIGA_MAC_VER_06:
5647 case RTL_GIGA_MAC_VER_10:
5648 case RTL_GIGA_MAC_VER_11:
5649 case RTL_GIGA_MAC_VER_12:
5650 case RTL_GIGA_MAC_VER_13:
5651 case RTL_GIGA_MAC_VER_14:
5652 case RTL_GIGA_MAC_VER_15:
5653 case RTL_GIGA_MAC_VER_16:
5654 case RTL_GIGA_MAC_VER_17:
5659 case RTL_GIGA_MAC_VER_37:
5660 case RTL_GIGA_MAC_VER_38:
5661 ops->write = r8402_csi_write;
5662 ops->read = r8402_csi_read;
5665 case RTL_GIGA_MAC_VER_44:
5666 ops->write = r8411_csi_write;
5667 ops->read = r8411_csi_read;
5671 ops->write = r8169_csi_write;
5672 ops->read = r8169_csi_read;
5678 unsigned int offset;
5683 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5689 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5690 rtl_ephy_write(tp, e->offset, w);
5695 static void rtl_disable_clock_request(struct rtl8169_private *tp)
5697 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
5698 PCI_EXP_LNKCTL_CLKREQ_EN);
5701 static void rtl_enable_clock_request(struct rtl8169_private *tp)
5703 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
5704 PCI_EXP_LNKCTL_CLKREQ_EN);
5707 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5711 data = RTL_R8(tp, Config3);
5716 data &= ~Rdy_to_L23;
5718 RTL_W8(tp, Config3, data);
5721 #define R8168_CPCMD_QUIRK_MASK (\
5732 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
5734 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5736 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5738 if (tp->dev->mtu <= ETH_DATA_LEN) {
5739 rtl_tx_performance_tweak(tp, (0x5 << MAX_READ_REQUEST_SHIFT) |
5740 PCI_EXP_DEVCTL_NOSNOOP_EN);
5744 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
5746 rtl_hw_start_8168bb(tp);
5748 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5750 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
5753 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
5755 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
5757 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5759 if (tp->dev->mtu <= ETH_DATA_LEN)
5760 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5762 rtl_disable_clock_request(tp);
5764 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5767 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
5769 static const struct ephy_info e_info_8168cp[] = {
5770 { 0x01, 0, 0x0001 },
5771 { 0x02, 0x0800, 0x1000 },
5772 { 0x03, 0, 0x0042 },
5773 { 0x06, 0x0080, 0x0000 },
5777 rtl_csi_access_enable_2(tp);
5779 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
5781 __rtl_hw_start_8168cp(tp);
5784 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
5786 rtl_csi_access_enable_2(tp);
5788 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5790 if (tp->dev->mtu <= ETH_DATA_LEN)
5791 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5793 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5796 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
5798 rtl_csi_access_enable_2(tp);
5800 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5803 RTL_W8(tp, DBG_REG, 0x20);
5805 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5807 if (tp->dev->mtu <= ETH_DATA_LEN)
5808 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5810 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5813 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
5815 static const struct ephy_info e_info_8168c_1[] = {
5816 { 0x02, 0x0800, 0x1000 },
5817 { 0x03, 0, 0x0002 },
5818 { 0x06, 0x0080, 0x0000 }
5821 rtl_csi_access_enable_2(tp);
5823 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5825 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
5827 __rtl_hw_start_8168cp(tp);
5830 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
5832 static const struct ephy_info e_info_8168c_2[] = {
5833 { 0x01, 0, 0x0001 },
5834 { 0x03, 0x0400, 0x0220 }
5837 rtl_csi_access_enable_2(tp);
5839 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
5841 __rtl_hw_start_8168cp(tp);
5844 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
5846 rtl_hw_start_8168c_2(tp);
5849 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
5851 rtl_csi_access_enable_2(tp);
5853 __rtl_hw_start_8168cp(tp);
5856 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
5858 rtl_csi_access_enable_2(tp);
5860 rtl_disable_clock_request(tp);
5862 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5864 if (tp->dev->mtu <= ETH_DATA_LEN)
5865 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5867 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5870 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
5872 rtl_csi_access_enable_1(tp);
5874 if (tp->dev->mtu <= ETH_DATA_LEN)
5875 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5877 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5879 rtl_disable_clock_request(tp);
5882 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
5884 static const struct ephy_info e_info_8168d_4[] = {
5885 { 0x0b, 0x0000, 0x0048 },
5886 { 0x19, 0x0020, 0x0050 },
5887 { 0x0c, 0x0100, 0x0020 }
5890 rtl_csi_access_enable_1(tp);
5892 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5894 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5896 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
5898 rtl_enable_clock_request(tp);
5901 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
5903 static const struct ephy_info e_info_8168e_1[] = {
5904 { 0x00, 0x0200, 0x0100 },
5905 { 0x00, 0x0000, 0x0004 },
5906 { 0x06, 0x0002, 0x0001 },
5907 { 0x06, 0x0000, 0x0030 },
5908 { 0x07, 0x0000, 0x2000 },
5909 { 0x00, 0x0000, 0x0020 },
5910 { 0x03, 0x5800, 0x2000 },
5911 { 0x03, 0x0000, 0x0001 },
5912 { 0x01, 0x0800, 0x1000 },
5913 { 0x07, 0x0000, 0x4000 },
5914 { 0x1e, 0x0000, 0x2000 },
5915 { 0x19, 0xffff, 0xfe6c },
5916 { 0x0a, 0x0000, 0x0040 }
5919 rtl_csi_access_enable_2(tp);
5921 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5923 if (tp->dev->mtu <= ETH_DATA_LEN)
5924 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5926 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5928 rtl_disable_clock_request(tp);
5930 /* Reset tx FIFO pointer */
5931 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5932 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
5934 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5937 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5939 static const struct ephy_info e_info_8168e_2[] = {
5940 { 0x09, 0x0000, 0x0080 },
5941 { 0x19, 0x0000, 0x0224 }
5944 rtl_csi_access_enable_1(tp);
5946 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5948 if (tp->dev->mtu <= ETH_DATA_LEN)
5949 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5951 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5952 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5953 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5954 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5955 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5956 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5957 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5958 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5960 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5962 rtl_disable_clock_request(tp);
5964 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5965 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5967 /* Adjust EEE LED frequency */
5968 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5970 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5971 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5972 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5975 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5977 rtl_csi_access_enable_2(tp);
5979 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
5981 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5982 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5983 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5984 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5985 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5986 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5987 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5988 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5989 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5990 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5992 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5994 rtl_disable_clock_request(tp);
5996 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5997 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5998 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5999 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
6000 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
6003 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6005 static const struct ephy_info e_info_8168f_1[] = {
6006 { 0x06, 0x00c0, 0x0020 },
6007 { 0x08, 0x0001, 0x0002 },
6008 { 0x09, 0x0000, 0x0080 },
6009 { 0x19, 0x0000, 0x0224 }
6012 rtl_hw_start_8168f(tp);
6014 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
6016 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
6018 /* Adjust EEE LED frequency */
6019 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
6022 static void rtl_hw_start_8411(struct rtl8169_private *tp)
6024 static const struct ephy_info e_info_8168f_1[] = {
6025 { 0x06, 0x00c0, 0x0020 },
6026 { 0x0f, 0xffff, 0x5200 },
6027 { 0x1e, 0x0000, 0x4000 },
6028 { 0x19, 0x0000, 0x0224 }
6031 rtl_hw_start_8168f(tp);
6032 rtl_pcie_state_l2l3_enable(tp, false);
6034 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
6036 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
6039 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
6041 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6043 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6044 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6045 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6046 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6048 rtl_csi_access_enable_1(tp);
6050 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
6052 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6053 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6054 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
6056 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6057 RTL_W8(tp, MaxTxPacketSize, EarlySize);
6059 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6060 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6062 /* Adjust EEE LED frequency */
6063 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
6065 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6066 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6068 rtl_pcie_state_l2l3_enable(tp, false);
6071 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6073 static const struct ephy_info e_info_8168g_1[] = {
6074 { 0x00, 0x0000, 0x0008 },
6075 { 0x0c, 0x37d0, 0x0820 },
6076 { 0x1e, 0x0000, 0x0001 },
6077 { 0x19, 0x8000, 0x0000 }
6080 rtl_hw_start_8168g(tp);
6082 /* disable aspm and clock request before access ephy */
6083 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6084 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6085 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6088 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6090 static const struct ephy_info e_info_8168g_2[] = {
6091 { 0x00, 0x0000, 0x0008 },
6092 { 0x0c, 0x3df0, 0x0200 },
6093 { 0x19, 0xffff, 0xfc00 },
6094 { 0x1e, 0xffff, 0x20eb }
6097 rtl_hw_start_8168g(tp);
6099 /* disable aspm and clock request before access ephy */
6100 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6101 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6102 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6105 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6107 static const struct ephy_info e_info_8411_2[] = {
6108 { 0x00, 0x0000, 0x0008 },
6109 { 0x0c, 0x3df0, 0x0200 },
6110 { 0x0f, 0xffff, 0x5200 },
6111 { 0x19, 0x0020, 0x0000 },
6112 { 0x1e, 0x0000, 0x2000 }
6115 rtl_hw_start_8168g(tp);
6117 /* disable aspm and clock request before access ephy */
6118 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6119 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6120 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6123 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6127 static const struct ephy_info e_info_8168h_1[] = {
6128 { 0x1e, 0x0800, 0x0001 },
6129 { 0x1d, 0x0000, 0x0800 },
6130 { 0x05, 0xffff, 0x2089 },
6131 { 0x06, 0xffff, 0x5881 },
6132 { 0x04, 0xffff, 0x154a },
6133 { 0x01, 0xffff, 0x068b }
6136 /* disable aspm and clock request before access ephy */
6137 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6138 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6139 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6141 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6143 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6144 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6145 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6146 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6148 rtl_csi_access_enable_1(tp);
6150 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
6152 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6153 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6155 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
6157 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
6159 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6161 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6162 RTL_W8(tp, MaxTxPacketSize, EarlySize);
6164 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6165 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6167 /* Adjust EEE LED frequency */
6168 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
6170 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6171 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
6173 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
6175 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6177 rtl_pcie_state_l2l3_enable(tp, false);
6179 rtl_writephy(tp, 0x1f, 0x0c42);
6180 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
6181 rtl_writephy(tp, 0x1f, 0x0000);
6182 if (rg_saw_cnt > 0) {
6185 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6186 sw_cnt_1ms_ini &= 0x0fff;
6187 data = r8168_mac_ocp_read(tp, 0xd412);
6189 data |= sw_cnt_1ms_ini;
6190 r8168_mac_ocp_write(tp, 0xd412, data);
6193 data = r8168_mac_ocp_read(tp, 0xe056);
6196 r8168_mac_ocp_write(tp, 0xe056, data);
6198 data = r8168_mac_ocp_read(tp, 0xe052);
6201 r8168_mac_ocp_write(tp, 0xe052, data);
6203 data = r8168_mac_ocp_read(tp, 0xe0d6);
6206 r8168_mac_ocp_write(tp, 0xe0d6, data);
6208 data = r8168_mac_ocp_read(tp, 0xd420);
6211 r8168_mac_ocp_write(tp, 0xd420, data);
6213 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6214 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6215 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6216 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6219 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6221 rtl8168ep_stop_cmac(tp);
6223 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6225 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6226 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6227 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6228 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6230 rtl_csi_access_enable_1(tp);
6232 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
6234 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6235 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6237 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6239 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6241 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6242 RTL_W8(tp, MaxTxPacketSize, EarlySize);
6244 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6245 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6247 /* Adjust EEE LED frequency */
6248 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
6250 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6252 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
6254 rtl_pcie_state_l2l3_enable(tp, false);
6257 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6259 static const struct ephy_info e_info_8168ep_1[] = {
6260 { 0x00, 0xffff, 0x10ab },
6261 { 0x06, 0xffff, 0xf030 },
6262 { 0x08, 0xffff, 0x2006 },
6263 { 0x0d, 0xffff, 0x1666 },
6264 { 0x0c, 0x3ff0, 0x0000 }
6267 /* disable aspm and clock request before access ephy */
6268 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6269 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6270 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6272 rtl_hw_start_8168ep(tp);
6275 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6277 static const struct ephy_info e_info_8168ep_2[] = {
6278 { 0x00, 0xffff, 0x10a3 },
6279 { 0x19, 0xffff, 0xfc00 },
6280 { 0x1e, 0xffff, 0x20ea }
6283 /* disable aspm and clock request before access ephy */
6284 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6285 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6286 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6288 rtl_hw_start_8168ep(tp);
6290 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6291 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
6294 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6297 static const struct ephy_info e_info_8168ep_3[] = {
6298 { 0x00, 0xffff, 0x10a3 },
6299 { 0x19, 0xffff, 0x7c00 },
6300 { 0x1e, 0xffff, 0x20eb },
6301 { 0x0d, 0xffff, 0x1666 }
6304 /* disable aspm and clock request before access ephy */
6305 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6306 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
6307 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6309 rtl_hw_start_8168ep(tp);
6311 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6312 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
6314 data = r8168_mac_ocp_read(tp, 0xd3e2);
6317 r8168_mac_ocp_write(tp, 0xd3e2, data);
6319 data = r8168_mac_ocp_read(tp, 0xd3e4);
6321 r8168_mac_ocp_write(tp, 0xd3e4, data);
6323 data = r8168_mac_ocp_read(tp, 0xe860);
6325 r8168_mac_ocp_write(tp, 0xe860, data);
6328 static void rtl_hw_start_8168(struct net_device *dev)
6330 struct rtl8169_private *tp = netdev_priv(dev);
6332 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
6334 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
6336 rtl_set_rx_max_size(tp, rx_buf_sz);
6338 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) | PktCntrDisable | INTT_1;
6340 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
6342 RTL_W16(tp, IntrMitigate, 0x5151);
6344 /* Work around for RxFIFO overflow. */
6345 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
6346 tp->event_slow |= RxFIFOOver | PCSTimeout;
6347 tp->event_slow &= ~RxOverflow;
6350 rtl_set_rx_tx_desc_registers(tp);
6352 rtl_set_rx_tx_config_registers(tp);
6354 RTL_R8(tp, IntrMask);
6356 switch (tp->mac_version) {
6357 case RTL_GIGA_MAC_VER_11:
6358 rtl_hw_start_8168bb(tp);
6361 case RTL_GIGA_MAC_VER_12:
6362 case RTL_GIGA_MAC_VER_17:
6363 rtl_hw_start_8168bef(tp);
6366 case RTL_GIGA_MAC_VER_18:
6367 rtl_hw_start_8168cp_1(tp);
6370 case RTL_GIGA_MAC_VER_19:
6371 rtl_hw_start_8168c_1(tp);
6374 case RTL_GIGA_MAC_VER_20:
6375 rtl_hw_start_8168c_2(tp);
6378 case RTL_GIGA_MAC_VER_21:
6379 rtl_hw_start_8168c_3(tp);
6382 case RTL_GIGA_MAC_VER_22:
6383 rtl_hw_start_8168c_4(tp);
6386 case RTL_GIGA_MAC_VER_23:
6387 rtl_hw_start_8168cp_2(tp);
6390 case RTL_GIGA_MAC_VER_24:
6391 rtl_hw_start_8168cp_3(tp);
6394 case RTL_GIGA_MAC_VER_25:
6395 case RTL_GIGA_MAC_VER_26:
6396 case RTL_GIGA_MAC_VER_27:
6397 rtl_hw_start_8168d(tp);
6400 case RTL_GIGA_MAC_VER_28:
6401 rtl_hw_start_8168d_4(tp);
6404 case RTL_GIGA_MAC_VER_31:
6405 rtl_hw_start_8168dp(tp);
6408 case RTL_GIGA_MAC_VER_32:
6409 case RTL_GIGA_MAC_VER_33:
6410 rtl_hw_start_8168e_1(tp);
6412 case RTL_GIGA_MAC_VER_34:
6413 rtl_hw_start_8168e_2(tp);
6416 case RTL_GIGA_MAC_VER_35:
6417 case RTL_GIGA_MAC_VER_36:
6418 rtl_hw_start_8168f_1(tp);
6421 case RTL_GIGA_MAC_VER_38:
6422 rtl_hw_start_8411(tp);
6425 case RTL_GIGA_MAC_VER_40:
6426 case RTL_GIGA_MAC_VER_41:
6427 rtl_hw_start_8168g_1(tp);
6429 case RTL_GIGA_MAC_VER_42:
6430 rtl_hw_start_8168g_2(tp);
6433 case RTL_GIGA_MAC_VER_44:
6434 rtl_hw_start_8411_2(tp);
6437 case RTL_GIGA_MAC_VER_45:
6438 case RTL_GIGA_MAC_VER_46:
6439 rtl_hw_start_8168h_1(tp);
6442 case RTL_GIGA_MAC_VER_49:
6443 rtl_hw_start_8168ep_1(tp);
6446 case RTL_GIGA_MAC_VER_50:
6447 rtl_hw_start_8168ep_2(tp);
6450 case RTL_GIGA_MAC_VER_51:
6451 rtl_hw_start_8168ep_3(tp);
6455 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6456 dev->name, tp->mac_version);
6460 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
6462 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
6464 rtl_set_rx_mode(dev);
6466 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
6469 #define R810X_CPCMD_QUIRK_MASK (\
6480 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
6482 static const struct ephy_info e_info_8102e_1[] = {
6483 { 0x01, 0, 0x6e65 },
6484 { 0x02, 0, 0x091f },
6485 { 0x03, 0, 0xc2f9 },
6486 { 0x06, 0, 0xafb5 },
6487 { 0x07, 0, 0x0e00 },
6488 { 0x19, 0, 0xec80 },
6489 { 0x01, 0, 0x2e65 },
6494 rtl_csi_access_enable_2(tp);
6496 RTL_W8(tp, DBG_REG, FIX_NAK_1);
6498 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
6501 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6502 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
6504 cfg1 = RTL_R8(tp, Config1);
6505 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6506 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
6508 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
6511 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
6513 rtl_csi_access_enable_2(tp);
6515 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
6517 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6518 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
6521 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
6523 rtl_hw_start_8102e_2(tp);
6525 rtl_ephy_write(tp, 0x03, 0xc2f9);
6528 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
6530 static const struct ephy_info e_info_8105e_1[] = {
6531 { 0x07, 0, 0x4000 },
6532 { 0x19, 0, 0x0200 },
6533 { 0x19, 0, 0x0020 },
6534 { 0x1e, 0, 0x2000 },
6535 { 0x03, 0, 0x0001 },
6536 { 0x19, 0, 0x0100 },
6537 { 0x19, 0, 0x0004 },
6541 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6542 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
6544 /* Disable Early Tally Counter */
6545 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
6547 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6548 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
6550 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
6552 rtl_pcie_state_l2l3_enable(tp, false);
6555 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
6557 rtl_hw_start_8105e_1(tp);
6558 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
6561 static void rtl_hw_start_8402(struct rtl8169_private *tp)
6563 static const struct ephy_info e_info_8402[] = {
6564 { 0x19, 0xffff, 0xff64 },
6568 rtl_csi_access_enable_2(tp);
6570 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6571 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
6573 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6574 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
6576 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
6578 rtl_tx_performance_tweak(tp, 0x5 << MAX_READ_REQUEST_SHIFT);
6580 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6581 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
6582 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6583 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6584 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6585 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6586 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
6588 rtl_pcie_state_l2l3_enable(tp, false);
6591 static void rtl_hw_start_8106(struct rtl8169_private *tp)
6593 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6594 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
6596 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6597 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6598 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6600 rtl_pcie_state_l2l3_enable(tp, false);
6603 static void rtl_hw_start_8101(struct net_device *dev)
6605 struct rtl8169_private *tp = netdev_priv(dev);
6606 struct pci_dev *pdev = tp->pci_dev;
6608 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6609 tp->event_slow &= ~RxFIFOOver;
6611 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
6612 tp->mac_version == RTL_GIGA_MAC_VER_16)
6613 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6614 PCI_EXP_DEVCTL_NOSNOOP_EN);
6616 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
6618 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
6620 rtl_set_rx_max_size(tp, rx_buf_sz);
6622 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6623 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
6625 rtl_set_rx_tx_desc_registers(tp);
6627 rtl_set_rx_tx_config_registers(tp);
6629 switch (tp->mac_version) {
6630 case RTL_GIGA_MAC_VER_07:
6631 rtl_hw_start_8102e_1(tp);
6634 case RTL_GIGA_MAC_VER_08:
6635 rtl_hw_start_8102e_3(tp);
6638 case RTL_GIGA_MAC_VER_09:
6639 rtl_hw_start_8102e_2(tp);
6642 case RTL_GIGA_MAC_VER_29:
6643 rtl_hw_start_8105e_1(tp);
6645 case RTL_GIGA_MAC_VER_30:
6646 rtl_hw_start_8105e_2(tp);
6649 case RTL_GIGA_MAC_VER_37:
6650 rtl_hw_start_8402(tp);
6653 case RTL_GIGA_MAC_VER_39:
6654 rtl_hw_start_8106(tp);
6656 case RTL_GIGA_MAC_VER_43:
6657 rtl_hw_start_8168g_2(tp);
6659 case RTL_GIGA_MAC_VER_47:
6660 case RTL_GIGA_MAC_VER_48:
6661 rtl_hw_start_8168h_1(tp);
6665 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
6667 RTL_W16(tp, IntrMitigate, 0x0000);
6669 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
6671 rtl_set_rx_mode(dev);
6673 RTL_R8(tp, IntrMask);
6675 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
6678 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6680 struct rtl8169_private *tp = netdev_priv(dev);
6682 if (new_mtu > ETH_DATA_LEN)
6683 rtl_hw_jumbo_enable(tp);
6685 rtl_hw_jumbo_disable(tp);
6688 netdev_update_features(dev);
6693 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6695 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
6696 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6699 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6700 void **data_buff, struct RxDesc *desc)
6702 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr), rx_buf_sz,
6707 rtl8169_make_unusable_by_asic(desc);
6710 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6712 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6714 /* Force memory writes to complete before releasing descriptor */
6717 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6720 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6723 desc->addr = cpu_to_le64(mapping);
6724 rtl8169_mark_to_asic(desc, rx_buf_sz);
6727 static inline void *rtl8169_align(void *data)
6729 return (void *)ALIGN((long)data, 16);
6732 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6733 struct RxDesc *desc)
6737 struct device *d = tp_to_dev(tp);
6738 struct net_device *dev = tp->dev;
6739 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
6741 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6745 if (rtl8169_align(data) != data) {
6747 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6752 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
6754 if (unlikely(dma_mapping_error(d, mapping))) {
6755 if (net_ratelimit())
6756 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
6760 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
6768 static void rtl8169_rx_clear(struct rtl8169_private *tp)
6772 for (i = 0; i < NUM_RX_DESC; i++) {
6773 if (tp->Rx_databuff[i]) {
6774 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
6775 tp->RxDescArray + i);
6780 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
6782 desc->opts1 |= cpu_to_le32(RingEnd);
6785 static int rtl8169_rx_fill(struct rtl8169_private *tp)
6789 for (i = 0; i < NUM_RX_DESC; i++) {
6792 if (tp->Rx_databuff[i])
6795 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
6797 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
6800 tp->Rx_databuff[i] = data;
6803 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6807 rtl8169_rx_clear(tp);
6811 static int rtl8169_init_ring(struct net_device *dev)
6813 struct rtl8169_private *tp = netdev_priv(dev);
6815 rtl8169_init_ring_indexes(tp);
6817 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
6818 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
6820 return rtl8169_rx_fill(tp);
6823 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
6824 struct TxDesc *desc)
6826 unsigned int len = tx_skb->len;
6828 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6836 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6841 for (i = 0; i < n; i++) {
6842 unsigned int entry = (start + i) % NUM_TX_DESC;
6843 struct ring_info *tx_skb = tp->tx_skb + entry;
6844 unsigned int len = tx_skb->len;
6847 struct sk_buff *skb = tx_skb->skb;
6849 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
6850 tp->TxDescArray + entry);
6852 dev_consume_skb_any(skb);
6859 static void rtl8169_tx_clear(struct rtl8169_private *tp)
6861 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
6862 tp->cur_tx = tp->dirty_tx = 0;
6865 static void rtl_reset_work(struct rtl8169_private *tp)
6867 struct net_device *dev = tp->dev;
6870 napi_disable(&tp->napi);
6871 netif_stop_queue(dev);
6872 synchronize_sched();
6874 rtl8169_hw_reset(tp);
6876 for (i = 0; i < NUM_RX_DESC; i++)
6877 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6879 rtl8169_tx_clear(tp);
6880 rtl8169_init_ring_indexes(tp);
6882 napi_enable(&tp->napi);
6884 netif_wake_queue(dev);
6885 rtl8169_check_link_status(dev, tp);
6888 static void rtl8169_tx_timeout(struct net_device *dev)
6890 struct rtl8169_private *tp = netdev_priv(dev);
6892 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6895 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
6898 struct skb_shared_info *info = skb_shinfo(skb);
6899 unsigned int cur_frag, entry;
6900 struct TxDesc *uninitialized_var(txd);
6901 struct device *d = tp_to_dev(tp);
6904 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
6905 const skb_frag_t *frag = info->frags + cur_frag;
6910 entry = (entry + 1) % NUM_TX_DESC;
6912 txd = tp->TxDescArray + entry;
6913 len = skb_frag_size(frag);
6914 addr = skb_frag_address(frag);
6915 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
6916 if (unlikely(dma_mapping_error(d, mapping))) {
6917 if (net_ratelimit())
6918 netif_err(tp, drv, tp->dev,
6919 "Failed to map TX fragments DMA!\n");
6923 /* Anti gcc 2.95.3 bugware (sic) */
6924 status = opts[0] | len |
6925 (RingEnd * !((entry + 1) % NUM_TX_DESC));
6927 txd->opts1 = cpu_to_le32(status);
6928 txd->opts2 = cpu_to_le32(opts[1]);
6929 txd->addr = cpu_to_le64(mapping);
6931 tp->tx_skb[entry].len = len;
6935 tp->tx_skb[entry].skb = skb;
6936 txd->opts1 |= cpu_to_le32(LastFrag);
6942 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6946 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6948 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6951 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6952 struct net_device *dev);
6953 /* r8169_csum_workaround()
6954 * The hw limites the value the transport offset. When the offset is out of the
6955 * range, calculate the checksum by sw.
6957 static void r8169_csum_workaround(struct rtl8169_private *tp,
6958 struct sk_buff *skb)
6960 if (skb_shinfo(skb)->gso_size) {
6961 netdev_features_t features = tp->dev->features;
6962 struct sk_buff *segs, *nskb;
6964 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6965 segs = skb_gso_segment(skb, features);
6966 if (IS_ERR(segs) || !segs)
6973 rtl8169_start_xmit(nskb, tp->dev);
6976 dev_consume_skb_any(skb);
6977 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6978 if (skb_checksum_help(skb) < 0)
6981 rtl8169_start_xmit(skb, tp->dev);
6983 struct net_device_stats *stats;
6986 stats = &tp->dev->stats;
6987 stats->tx_dropped++;
6988 dev_kfree_skb_any(skb);
6992 /* msdn_giant_send_check()
6993 * According to the document of microsoft, the TCP Pseudo Header excludes the
6994 * packet length for IPv6 TCP large packets.
6996 static int msdn_giant_send_check(struct sk_buff *skb)
6998 const struct ipv6hdr *ipv6h;
7002 ret = skb_cow_head(skb, 0);
7006 ipv6h = ipv6_hdr(skb);
7010 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7015 static inline __be16 get_protocol(struct sk_buff *skb)
7019 if (skb->protocol == htons(ETH_P_8021Q))
7020 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7022 protocol = skb->protocol;
7027 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7028 struct sk_buff *skb, u32 *opts)
7030 u32 mss = skb_shinfo(skb)->gso_size;
7034 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7035 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7036 const struct iphdr *ip = ip_hdr(skb);
7038 if (ip->protocol == IPPROTO_TCP)
7039 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7040 else if (ip->protocol == IPPROTO_UDP)
7041 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7049 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7050 struct sk_buff *skb, u32 *opts)
7052 u32 transport_offset = (u32)skb_transport_offset(skb);
7053 u32 mss = skb_shinfo(skb)->gso_size;
7056 if (transport_offset > GTTCPHO_MAX) {
7057 netif_warn(tp, tx_err, tp->dev,
7058 "Invalid transport offset 0x%x for TSO\n",
7063 switch (get_protocol(skb)) {
7064 case htons(ETH_P_IP):
7065 opts[0] |= TD1_GTSENV4;
7068 case htons(ETH_P_IPV6):
7069 if (msdn_giant_send_check(skb))
7072 opts[0] |= TD1_GTSENV6;
7080 opts[0] |= transport_offset << GTTCPHO_SHIFT;
7081 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
7082 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7085 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7086 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
7088 if (transport_offset > TCPHO_MAX) {
7089 netif_warn(tp, tx_err, tp->dev,
7090 "Invalid transport offset 0x%x\n",
7095 switch (get_protocol(skb)) {
7096 case htons(ETH_P_IP):
7097 opts[1] |= TD1_IPv4_CS;
7098 ip_protocol = ip_hdr(skb)->protocol;
7101 case htons(ETH_P_IPV6):
7102 opts[1] |= TD1_IPv6_CS;
7103 ip_protocol = ipv6_hdr(skb)->nexthdr;
7107 ip_protocol = IPPROTO_RAW;
7111 if (ip_protocol == IPPROTO_TCP)
7112 opts[1] |= TD1_TCP_CS;
7113 else if (ip_protocol == IPPROTO_UDP)
7114 opts[1] |= TD1_UDP_CS;
7118 opts[1] |= transport_offset << TCPHO_SHIFT;
7120 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7121 return !eth_skb_pad(skb);
7127 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7128 struct net_device *dev)
7130 struct rtl8169_private *tp = netdev_priv(dev);
7131 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
7132 struct TxDesc *txd = tp->TxDescArray + entry;
7133 struct device *d = tp_to_dev(tp);
7139 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
7140 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
7144 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
7147 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7150 if (!tp->tso_csum(tp, skb, opts)) {
7151 r8169_csum_workaround(tp, skb);
7152 return NETDEV_TX_OK;
7155 len = skb_headlen(skb);
7156 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
7157 if (unlikely(dma_mapping_error(d, mapping))) {
7158 if (net_ratelimit())
7159 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
7163 tp->tx_skb[entry].len = len;
7164 txd->addr = cpu_to_le64(mapping);
7166 frags = rtl8169_xmit_frags(tp, skb, opts);
7170 opts[0] |= FirstFrag;
7172 opts[0] |= FirstFrag | LastFrag;
7173 tp->tx_skb[entry].skb = skb;
7176 txd->opts2 = cpu_to_le32(opts[1]);
7178 skb_tx_timestamp(skb);
7180 /* Force memory writes to complete before releasing descriptor */
7183 /* Anti gcc 2.95.3 bugware (sic) */
7184 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
7185 txd->opts1 = cpu_to_le32(status);
7187 /* Force all memory writes to complete before notifying device */
7190 tp->cur_tx += frags + 1;
7192 RTL_W8(tp, TxPoll, NPQ);
7196 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
7197 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7198 * not miss a ring update when it notices a stopped queue.
7201 netif_stop_queue(dev);
7202 /* Sync with rtl_tx:
7203 * - publish queue status and cur_tx ring index (write barrier)
7204 * - refresh dirty_tx ring index (read barrier).
7205 * May the current thread have a pessimistic view of the ring
7206 * status and forget to wake up queue, a racing rtl_tx thread
7210 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
7211 netif_wake_queue(dev);
7214 return NETDEV_TX_OK;
7217 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
7219 dev_kfree_skb_any(skb);
7220 dev->stats.tx_dropped++;
7221 return NETDEV_TX_OK;
7224 netif_stop_queue(dev);
7225 dev->stats.tx_dropped++;
7226 return NETDEV_TX_BUSY;
7229 static void rtl8169_pcierr_interrupt(struct net_device *dev)
7231 struct rtl8169_private *tp = netdev_priv(dev);
7232 struct pci_dev *pdev = tp->pci_dev;
7233 u16 pci_status, pci_cmd;
7235 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7236 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7238 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7239 pci_cmd, pci_status);
7242 * The recovery sequence below admits a very elaborated explanation:
7243 * - it seems to work;
7244 * - I did not see what else could be done;
7245 * - it makes iop3xx happy.
7247 * Feel free to adjust to your needs.
7249 if (pdev->broken_parity_status)
7250 pci_cmd &= ~PCI_COMMAND_PARITY;
7252 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7254 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
7256 pci_write_config_word(pdev, PCI_STATUS,
7257 pci_status & (PCI_STATUS_DETECTED_PARITY |
7258 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7259 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7261 /* The infamous DAC f*ckup only happens at boot time */
7262 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
7263 netif_info(tp, intr, dev, "disabling PCI DAC\n");
7264 tp->cp_cmd &= ~PCIDAC;
7265 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
7266 dev->features &= ~NETIF_F_HIGHDMA;
7269 rtl8169_hw_reset(tp);
7271 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7274 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
7276 unsigned int dirty_tx, tx_left;
7278 dirty_tx = tp->dirty_tx;
7280 tx_left = tp->cur_tx - dirty_tx;
7282 while (tx_left > 0) {
7283 unsigned int entry = dirty_tx % NUM_TX_DESC;
7284 struct ring_info *tx_skb = tp->tx_skb + entry;
7287 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7288 if (status & DescOwn)
7291 /* This barrier is needed to keep us from reading
7292 * any other fields out of the Tx descriptor until
7293 * we know the status of DescOwn
7297 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
7298 tp->TxDescArray + entry);
7299 if (status & LastFrag) {
7300 u64_stats_update_begin(&tp->tx_stats.syncp);
7301 tp->tx_stats.packets++;
7302 tp->tx_stats.bytes += tx_skb->skb->len;
7303 u64_stats_update_end(&tp->tx_stats.syncp);
7304 dev_consume_skb_any(tx_skb->skb);
7311 if (tp->dirty_tx != dirty_tx) {
7312 tp->dirty_tx = dirty_tx;
7313 /* Sync with rtl8169_start_xmit:
7314 * - publish dirty_tx ring index (write barrier)
7315 * - refresh cur_tx ring index and queue status (read barrier)
7316 * May the current thread miss the stopped queue condition,
7317 * a racing xmit thread can only have a right view of the
7321 if (netif_queue_stopped(dev) &&
7322 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
7323 netif_wake_queue(dev);
7326 * 8168 hack: TxPoll requests are lost when the Tx packets are
7327 * too close. Let's kick an extra TxPoll request when a burst
7328 * of start_xmit activity is detected (if it is not detected,
7329 * it is slow enough). -- FR
7331 if (tp->cur_tx != dirty_tx)
7332 RTL_W8(tp, TxPoll, NPQ);
7336 static inline int rtl8169_fragmented_frame(u32 status)
7338 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7341 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
7343 u32 status = opts1 & RxProtoMask;
7345 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
7346 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
7347 skb->ip_summed = CHECKSUM_UNNECESSARY;
7349 skb_checksum_none_assert(skb);
7352 static struct sk_buff *rtl8169_try_rx_copy(void *data,
7353 struct rtl8169_private *tp,
7357 struct sk_buff *skb;
7358 struct device *d = tp_to_dev(tp);
7360 data = rtl8169_align(data);
7361 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
7363 skb = napi_alloc_skb(&tp->napi, pkt_size);
7365 memcpy(skb->data, data, pkt_size);
7366 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7371 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
7373 unsigned int cur_rx, rx_left;
7376 cur_rx = tp->cur_rx;
7378 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
7379 unsigned int entry = cur_rx % NUM_RX_DESC;
7380 struct RxDesc *desc = tp->RxDescArray + entry;
7383 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
7384 if (status & DescOwn)
7387 /* This barrier is needed to keep us from reading
7388 * any other fields out of the Rx descriptor until
7389 * we know the status of DescOwn
7393 if (unlikely(status & RxRES)) {
7394 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7396 dev->stats.rx_errors++;
7397 if (status & (RxRWT | RxRUNT))
7398 dev->stats.rx_length_errors++;
7400 dev->stats.rx_crc_errors++;
7401 if (status & RxFOVF) {
7402 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7403 dev->stats.rx_fifo_errors++;
7405 if ((status & (RxRUNT | RxCRC)) &&
7406 !(status & (RxRWT | RxFOVF)) &&
7407 (dev->features & NETIF_F_RXALL))
7410 struct sk_buff *skb;
7415 addr = le64_to_cpu(desc->addr);
7416 if (likely(!(dev->features & NETIF_F_RXFCS)))
7417 pkt_size = (status & 0x00003fff) - 4;
7419 pkt_size = status & 0x00003fff;
7422 * The driver does not support incoming fragmented
7423 * frames. They are seen as a symptom of over-mtu
7426 if (unlikely(rtl8169_fragmented_frame(status))) {
7427 dev->stats.rx_dropped++;
7428 dev->stats.rx_length_errors++;
7429 goto release_descriptor;
7432 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7433 tp, pkt_size, addr);
7435 dev->stats.rx_dropped++;
7436 goto release_descriptor;
7439 rtl8169_rx_csum(skb, status);
7440 skb_put(skb, pkt_size);
7441 skb->protocol = eth_type_trans(skb, dev);
7443 rtl8169_rx_vlan_tag(desc, skb);
7445 if (skb->pkt_type == PACKET_MULTICAST)
7446 dev->stats.multicast++;
7448 napi_gro_receive(&tp->napi, skb);
7450 u64_stats_update_begin(&tp->rx_stats.syncp);
7451 tp->rx_stats.packets++;
7452 tp->rx_stats.bytes += pkt_size;
7453 u64_stats_update_end(&tp->rx_stats.syncp);
7457 rtl8169_mark_to_asic(desc, rx_buf_sz);
7460 count = cur_rx - tp->cur_rx;
7461 tp->cur_rx = cur_rx;
7466 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
7468 struct net_device *dev = dev_instance;
7469 struct rtl8169_private *tp = netdev_priv(dev);
7473 status = rtl_get_events(tp);
7474 if (status && status != 0xffff) {
7475 status &= RTL_EVENT_NAPI | tp->event_slow;
7479 rtl_irq_disable(tp);
7480 napi_schedule(&tp->napi);
7483 return IRQ_RETVAL(handled);
7487 * Workqueue context.
7489 static void rtl_slow_event_work(struct rtl8169_private *tp)
7491 struct net_device *dev = tp->dev;
7494 status = rtl_get_events(tp) & tp->event_slow;
7495 rtl_ack_events(tp, status);
7497 if (unlikely(status & RxFIFOOver)) {
7498 switch (tp->mac_version) {
7499 /* Work around for rx fifo overflow */
7500 case RTL_GIGA_MAC_VER_11:
7501 netif_stop_queue(dev);
7502 /* XXX - Hack alert. See rtl_task(). */
7503 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
7509 if (unlikely(status & SYSErr))
7510 rtl8169_pcierr_interrupt(dev);
7512 if (status & LinkChg)
7513 rtl8169_check_link_status(dev, tp);
7515 rtl_irq_enable_all(tp);
7518 static void rtl_task(struct work_struct *work)
7520 static const struct {
7522 void (*action)(struct rtl8169_private *);
7524 /* XXX - keep rtl_slow_event_work() as first element. */
7525 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7526 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7527 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7529 struct rtl8169_private *tp =
7530 container_of(work, struct rtl8169_private, wk.work);
7531 struct net_device *dev = tp->dev;
7536 if (!netif_running(dev) ||
7537 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
7540 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7543 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
7545 rtl_work[i].action(tp);
7549 rtl_unlock_work(tp);
7552 static int rtl8169_poll(struct napi_struct *napi, int budget)
7554 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7555 struct net_device *dev = tp->dev;
7556 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7560 status = rtl_get_events(tp);
7561 rtl_ack_events(tp, status & ~tp->event_slow);
7563 if (status & RTL_EVENT_NAPI_RX)
7564 work_done = rtl_rx(dev, tp, (u32) budget);
7566 if (status & RTL_EVENT_NAPI_TX)
7569 if (status & tp->event_slow) {
7570 enable_mask &= ~tp->event_slow;
7572 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7575 if (work_done < budget) {
7576 napi_complete_done(napi, work_done);
7578 rtl_irq_enable(tp, enable_mask);
7585 static void rtl8169_rx_missed(struct net_device *dev)
7587 struct rtl8169_private *tp = netdev_priv(dev);
7589 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7592 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7593 RTL_W32(tp, RxMissed, 0);
7596 static void rtl8169_down(struct net_device *dev)
7598 struct rtl8169_private *tp = netdev_priv(dev);
7600 del_timer_sync(&tp->timer);
7602 napi_disable(&tp->napi);
7603 netif_stop_queue(dev);
7605 rtl8169_hw_reset(tp);
7607 * At this point device interrupts can not be enabled in any function,
7608 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7609 * and napi is disabled (rtl8169_poll).
7611 rtl8169_rx_missed(dev);
7613 /* Give a racing hard_start_xmit a few cycles to complete. */
7614 synchronize_sched();
7616 rtl8169_tx_clear(tp);
7618 rtl8169_rx_clear(tp);
7620 rtl_pll_power_down(tp);
7623 static int rtl8169_close(struct net_device *dev)
7625 struct rtl8169_private *tp = netdev_priv(dev);
7626 struct pci_dev *pdev = tp->pci_dev;
7628 pm_runtime_get_sync(&pdev->dev);
7630 /* Update counters before going down */
7631 rtl8169_update_counters(dev);
7634 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7637 rtl_unlock_work(tp);
7639 cancel_work_sync(&tp->wk.work);
7641 pci_free_irq(pdev, 0, dev);
7643 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7645 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7647 tp->TxDescArray = NULL;
7648 tp->RxDescArray = NULL;
7650 pm_runtime_put_sync(&pdev->dev);
7655 #ifdef CONFIG_NET_POLL_CONTROLLER
7656 static void rtl8169_netpoll(struct net_device *dev)
7658 struct rtl8169_private *tp = netdev_priv(dev);
7660 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
7664 static int rtl_open(struct net_device *dev)
7666 struct rtl8169_private *tp = netdev_priv(dev);
7667 struct pci_dev *pdev = tp->pci_dev;
7668 int retval = -ENOMEM;
7670 pm_runtime_get_sync(&pdev->dev);
7673 * Rx and Tx descriptors needs 256 bytes alignment.
7674 * dma_alloc_coherent provides more.
7676 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7677 &tp->TxPhyAddr, GFP_KERNEL);
7678 if (!tp->TxDescArray)
7679 goto err_pm_runtime_put;
7681 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7682 &tp->RxPhyAddr, GFP_KERNEL);
7683 if (!tp->RxDescArray)
7686 retval = rtl8169_init_ring(dev);
7690 INIT_WORK(&tp->wk.work, rtl_task);
7694 rtl_request_firmware(tp);
7696 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, dev,
7699 goto err_release_fw_2;
7703 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7705 napi_enable(&tp->napi);
7707 rtl8169_init_phy(dev, tp);
7709 __rtl8169_set_features(dev, dev->features);
7711 rtl_pll_power_up(tp);
7715 if (!rtl8169_init_counter_offsets(dev))
7716 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7718 netif_start_queue(dev);
7720 rtl_unlock_work(tp);
7722 tp->saved_wolopts = 0;
7723 pm_runtime_put_sync(&pdev->dev);
7725 rtl8169_check_link_status(dev, tp);
7730 rtl_release_firmware(tp);
7731 rtl8169_rx_clear(tp);
7733 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7735 tp->RxDescArray = NULL;
7737 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7739 tp->TxDescArray = NULL;
7741 pm_runtime_put_noidle(&pdev->dev);
7746 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7748 struct rtl8169_private *tp = netdev_priv(dev);
7749 struct pci_dev *pdev = tp->pci_dev;
7750 struct rtl8169_counters *counters = tp->counters;
7753 pm_runtime_get_noresume(&pdev->dev);
7755 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
7756 rtl8169_rx_missed(dev);
7759 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
7760 stats->rx_packets = tp->rx_stats.packets;
7761 stats->rx_bytes = tp->rx_stats.bytes;
7762 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
7765 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
7766 stats->tx_packets = tp->tx_stats.packets;
7767 stats->tx_bytes = tp->tx_stats.bytes;
7768 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
7770 stats->rx_dropped = dev->stats.rx_dropped;
7771 stats->tx_dropped = dev->stats.tx_dropped;
7772 stats->rx_length_errors = dev->stats.rx_length_errors;
7773 stats->rx_errors = dev->stats.rx_errors;
7774 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7775 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7776 stats->rx_missed_errors = dev->stats.rx_missed_errors;
7777 stats->multicast = dev->stats.multicast;
7780 * Fetch additonal counter values missing in stats collected by driver
7781 * from tally counters.
7783 if (pm_runtime_active(&pdev->dev))
7784 rtl8169_update_counters(dev);
7787 * Subtract values fetched during initalization.
7788 * See rtl8169_init_counter_offsets for a description why we do that.
7790 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
7791 le64_to_cpu(tp->tc_offset.tx_errors);
7792 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
7793 le32_to_cpu(tp->tc_offset.tx_multi_collision);
7794 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
7795 le16_to_cpu(tp->tc_offset.tx_aborted);
7797 pm_runtime_put_noidle(&pdev->dev);
7800 static void rtl8169_net_suspend(struct net_device *dev)
7802 struct rtl8169_private *tp = netdev_priv(dev);
7804 if (!netif_running(dev))
7807 netif_device_detach(dev);
7808 netif_stop_queue(dev);
7811 napi_disable(&tp->napi);
7812 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7813 rtl_unlock_work(tp);
7815 rtl_pll_power_down(tp);
7820 static int rtl8169_suspend(struct device *device)
7822 struct pci_dev *pdev = to_pci_dev(device);
7823 struct net_device *dev = pci_get_drvdata(pdev);
7825 rtl8169_net_suspend(dev);
7830 static void __rtl8169_resume(struct net_device *dev)
7832 struct rtl8169_private *tp = netdev_priv(dev);
7834 netif_device_attach(dev);
7836 rtl_pll_power_up(tp);
7839 napi_enable(&tp->napi);
7840 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7841 rtl_unlock_work(tp);
7843 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
7846 static int rtl8169_resume(struct device *device)
7848 struct pci_dev *pdev = to_pci_dev(device);
7849 struct net_device *dev = pci_get_drvdata(pdev);
7850 struct rtl8169_private *tp = netdev_priv(dev);
7852 rtl8169_init_phy(dev, tp);
7854 if (netif_running(dev))
7855 __rtl8169_resume(dev);
7860 static int rtl8169_runtime_suspend(struct device *device)
7862 struct pci_dev *pdev = to_pci_dev(device);
7863 struct net_device *dev = pci_get_drvdata(pdev);
7864 struct rtl8169_private *tp = netdev_priv(dev);
7866 if (!tp->TxDescArray) {
7867 rtl_pll_power_down(tp);
7872 tp->saved_wolopts = __rtl8169_get_wol(tp);
7873 __rtl8169_set_wol(tp, WAKE_ANY);
7874 rtl_unlock_work(tp);
7876 rtl8169_net_suspend(dev);
7878 /* Update counters before going runtime suspend */
7879 rtl8169_rx_missed(dev);
7880 rtl8169_update_counters(dev);
7885 static int rtl8169_runtime_resume(struct device *device)
7887 struct pci_dev *pdev = to_pci_dev(device);
7888 struct net_device *dev = pci_get_drvdata(pdev);
7889 struct rtl8169_private *tp = netdev_priv(dev);
7890 rtl_rar_set(tp, dev->dev_addr);
7892 if (!tp->TxDescArray)
7896 __rtl8169_set_wol(tp, tp->saved_wolopts);
7897 tp->saved_wolopts = 0;
7898 rtl_unlock_work(tp);
7900 rtl8169_init_phy(dev, tp);
7902 __rtl8169_resume(dev);
7907 static int rtl8169_runtime_idle(struct device *device)
7909 struct pci_dev *pdev = to_pci_dev(device);
7910 struct net_device *dev = pci_get_drvdata(pdev);
7912 if (!netif_running(dev) || !netif_carrier_ok(dev))
7913 pm_schedule_suspend(device, 10000);
7918 static const struct dev_pm_ops rtl8169_pm_ops = {
7919 .suspend = rtl8169_suspend,
7920 .resume = rtl8169_resume,
7921 .freeze = rtl8169_suspend,
7922 .thaw = rtl8169_resume,
7923 .poweroff = rtl8169_suspend,
7924 .restore = rtl8169_resume,
7925 .runtime_suspend = rtl8169_runtime_suspend,
7926 .runtime_resume = rtl8169_runtime_resume,
7927 .runtime_idle = rtl8169_runtime_idle,
7930 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
7932 #else /* !CONFIG_PM */
7934 #define RTL8169_PM_OPS NULL
7936 #endif /* !CONFIG_PM */
7938 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7940 /* WoL fails with 8168b when the receiver is disabled. */
7941 switch (tp->mac_version) {
7942 case RTL_GIGA_MAC_VER_11:
7943 case RTL_GIGA_MAC_VER_12:
7944 case RTL_GIGA_MAC_VER_17:
7945 pci_clear_master(tp->pci_dev);
7947 RTL_W8(tp, ChipCmd, CmdRxEnb);
7949 RTL_R8(tp, ChipCmd);
7956 static void rtl_shutdown(struct pci_dev *pdev)
7958 struct net_device *dev = pci_get_drvdata(pdev);
7959 struct rtl8169_private *tp = netdev_priv(dev);
7961 rtl8169_net_suspend(dev);
7963 /* Restore original MAC address */
7964 rtl_rar_set(tp, dev->perm_addr);
7966 rtl8169_hw_reset(tp);
7968 if (system_state == SYSTEM_POWER_OFF) {
7969 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7970 rtl_wol_suspend_quirk(tp);
7971 rtl_wol_shutdown_quirk(tp);
7974 pci_wake_from_d3(pdev, true);
7975 pci_set_power_state(pdev, PCI_D3hot);
7979 static void rtl_remove_one(struct pci_dev *pdev)
7981 struct net_device *dev = pci_get_drvdata(pdev);
7982 struct rtl8169_private *tp = netdev_priv(dev);
7984 if (r8168_check_dash(tp))
7985 rtl8168_driver_stop(tp);
7987 netif_napi_del(&tp->napi);
7989 unregister_netdev(dev);
7991 rtl_release_firmware(tp);
7993 if (pci_dev_run_wake(pdev))
7994 pm_runtime_get_noresume(&pdev->dev);
7996 /* restore original MAC address */
7997 rtl_rar_set(tp, dev->perm_addr);
8000 static const struct net_device_ops rtl_netdev_ops = {
8001 .ndo_open = rtl_open,
8002 .ndo_stop = rtl8169_close,
8003 .ndo_get_stats64 = rtl8169_get_stats64,
8004 .ndo_start_xmit = rtl8169_start_xmit,
8005 .ndo_tx_timeout = rtl8169_tx_timeout,
8006 .ndo_validate_addr = eth_validate_addr,
8007 .ndo_change_mtu = rtl8169_change_mtu,
8008 .ndo_fix_features = rtl8169_fix_features,
8009 .ndo_set_features = rtl8169_set_features,
8010 .ndo_set_mac_address = rtl_set_mac_address,
8011 .ndo_do_ioctl = rtl8169_ioctl,
8012 .ndo_set_rx_mode = rtl_set_rx_mode,
8013 #ifdef CONFIG_NET_POLL_CONTROLLER
8014 .ndo_poll_controller = rtl8169_netpoll,
8019 static const struct rtl_cfg_info {
8020 void (*hw_start)(struct net_device *);
8021 unsigned int region;
8024 unsigned int has_gmii:1;
8025 const struct rtl_coalesce_info *coalesce_info;
8027 } rtl_cfg_infos [] = {
8029 .hw_start = rtl_hw_start_8169,
8032 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8034 .coalesce_info = rtl_coalesce_info_8169,
8035 .default_ver = RTL_GIGA_MAC_VER_01,
8038 .hw_start = rtl_hw_start_8168,
8041 .event_slow = SYSErr | LinkChg | RxOverflow,
8043 .coalesce_info = rtl_coalesce_info_8168_8136,
8044 .default_ver = RTL_GIGA_MAC_VER_11,
8047 .hw_start = rtl_hw_start_8101,
8050 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8052 .coalesce_info = rtl_coalesce_info_8168_8136,
8053 .default_ver = RTL_GIGA_MAC_VER_13,
8057 static int rtl_alloc_irq(struct rtl8169_private *tp)
8061 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
8062 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
8063 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
8064 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
8065 flags = PCI_IRQ_LEGACY;
8067 flags = PCI_IRQ_ALL_TYPES;
8070 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
8073 DECLARE_RTL_COND(rtl_link_list_ready_cond)
8075 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
8078 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8080 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8083 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
8087 tp->ocp_base = OCP_STD_PHY_BASE;
8089 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
8091 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8094 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8097 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8099 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
8101 data = r8168_mac_ocp_read(tp, 0xe8de);
8103 r8168_mac_ocp_write(tp, 0xe8de, data);
8105 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8108 data = r8168_mac_ocp_read(tp, 0xe8de);
8110 r8168_mac_ocp_write(tp, 0xe8de, data);
8112 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8116 static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8118 rtl8168ep_stop_cmac(tp);
8119 rtl_hw_init_8168g(tp);
8122 static void rtl_hw_initialize(struct rtl8169_private *tp)
8124 switch (tp->mac_version) {
8125 case RTL_GIGA_MAC_VER_40:
8126 case RTL_GIGA_MAC_VER_41:
8127 case RTL_GIGA_MAC_VER_42:
8128 case RTL_GIGA_MAC_VER_43:
8129 case RTL_GIGA_MAC_VER_44:
8130 case RTL_GIGA_MAC_VER_45:
8131 case RTL_GIGA_MAC_VER_46:
8132 case RTL_GIGA_MAC_VER_47:
8133 case RTL_GIGA_MAC_VER_48:
8134 rtl_hw_init_8168g(tp);
8136 case RTL_GIGA_MAC_VER_49:
8137 case RTL_GIGA_MAC_VER_50:
8138 case RTL_GIGA_MAC_VER_51:
8139 rtl_hw_init_8168ep(tp);
8146 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8148 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8149 const unsigned int region = cfg->region;
8150 struct rtl8169_private *tp;
8151 struct mii_if_info *mii;
8152 struct net_device *dev;
8156 if (netif_msg_drv(&debug)) {
8157 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8158 MODULENAME, RTL8169_VERSION);
8161 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8165 SET_NETDEV_DEV(dev, &pdev->dev);
8166 dev->netdev_ops = &rtl_netdev_ops;
8167 tp = netdev_priv(dev);
8170 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8174 mii->mdio_read = rtl_mdio_read;
8175 mii->mdio_write = rtl_mdio_write;
8176 mii->phy_id_mask = 0x1f;
8177 mii->reg_num_mask = 0x1f;
8178 mii->supports_gmii = cfg->has_gmii;
8180 /* disable ASPM completely as that cause random device stop working
8181 * problems as well as full system hangs for some PCIe devices users */
8182 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8183 PCIE_LINK_STATE_CLKPM);
8185 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8186 rc = pcim_enable_device(pdev);
8188 netif_err(tp, probe, dev, "enable failure\n");
8192 if (pcim_set_mwi(pdev) < 0)
8193 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8195 /* make sure PCI base addr 1 is MMIO */
8196 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8197 netif_err(tp, probe, dev,
8198 "region #%d not an MMIO resource, aborting\n",
8203 /* check for weird/broken PCI region reporting */
8204 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8205 netif_err(tp, probe, dev,
8206 "Invalid PCI region size(s), aborting\n");
8210 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
8212 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8216 tp->mmio_addr = pcim_iomap_table(pdev)[region];
8218 if (!pci_is_pcie(pdev))
8219 netif_info(tp, probe, dev, "not PCI Express\n");
8221 /* Identify chip attached to board */
8222 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8226 if ((sizeof(dma_addr_t) > 4) &&
8227 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8228 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
8229 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8230 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
8232 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8233 if (!pci_is_pcie(pdev))
8234 tp->cp_cmd |= PCIDAC;
8235 dev->features |= NETIF_F_HIGHDMA;
8237 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8239 netif_err(tp, probe, dev, "DMA configuration failed\n");
8246 rtl_irq_disable(tp);
8248 rtl_hw_initialize(tp);
8252 rtl_ack_events(tp, 0xffff);
8254 pci_set_master(pdev);
8256 rtl_init_mdio_ops(tp);
8257 rtl_init_pll_power_ops(tp);
8258 rtl_init_jumbo_ops(tp);
8259 rtl_init_csi_ops(tp);
8261 rtl8169_print_mac_version(tp);
8263 chipset = tp->mac_version;
8264 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8266 rc = rtl_alloc_irq(tp);
8268 netif_err(tp, probe, dev, "Can't allocate interrupt\n");
8272 /* override BIOS settings, use userspace tools to enable WOL */
8273 __rtl8169_set_wol(tp, 0);
8275 if (rtl_tbi_enabled(tp)) {
8276 tp->set_speed = rtl8169_set_speed_tbi;
8277 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
8278 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8279 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8280 tp->link_ok = rtl8169_tbi_link_ok;
8281 tp->do_ioctl = rtl_tbi_ioctl;
8283 tp->set_speed = rtl8169_set_speed_xmii;
8284 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
8285 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8286 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8287 tp->link_ok = rtl8169_xmii_link_ok;
8288 tp->do_ioctl = rtl_xmii_ioctl;
8291 mutex_init(&tp->wk.mutex);
8292 u64_stats_init(&tp->rx_stats.syncp);
8293 u64_stats_init(&tp->tx_stats.syncp);
8295 /* Get MAC address */
8296 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8297 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8298 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8299 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8300 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8301 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8302 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8303 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8304 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8305 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
8306 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8307 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
8308 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8309 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8310 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8311 tp->mac_version == RTL_GIGA_MAC_VER_51) {
8314 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8315 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
8317 if (is_valid_ether_addr((u8 *)mac_addr))
8318 rtl_rar_set(tp, (u8 *)mac_addr);
8320 for (i = 0; i < ETH_ALEN; i++)
8321 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
8323 dev->ethtool_ops = &rtl8169_ethtool_ops;
8324 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
8326 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8328 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8329 * properly for all devices */
8330 dev->features |= NETIF_F_RXCSUM |
8331 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
8333 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8334 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8335 NETIF_F_HW_VLAN_CTAG_RX;
8336 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8339 tp->cp_cmd |= RxChkSum | RxVlan;
8342 * Pretend we are using VLANs; This bypasses a nasty bug where
8343 * Interrupts stop flowing on high load on 8110SCd controllers.
8345 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
8346 /* Disallow toggling */
8347 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
8349 if (tp->txd_version == RTL_TD_0)
8350 tp->tso_csum = rtl8169_tso_csum_v1;
8351 else if (tp->txd_version == RTL_TD_1) {
8352 tp->tso_csum = rtl8169_tso_csum_v2;
8353 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8357 dev->hw_features |= NETIF_F_RXALL;
8358 dev->hw_features |= NETIF_F_RXFCS;
8360 /* MTU range: 60 - hw-specific max */
8361 dev->min_mtu = ETH_ZLEN;
8362 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8364 tp->hw_start = cfg->hw_start;
8365 tp->event_slow = cfg->event_slow;
8366 tp->coalesce_info = cfg->coalesce_info;
8368 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8369 ~(RxBOVF | RxFOVF) : ~0;
8371 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
8373 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8375 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8376 &tp->counters_phys_addr,
8381 pci_set_drvdata(pdev, dev);
8383 rc = register_netdev(dev);
8387 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8388 rtl_chip_infos[chipset].name, tp->mmio_addr, dev->dev_addr,
8389 (u32)(RTL_R32(tp, TxConfig) & 0x9cf0f8ff),
8390 pci_irq_vector(pdev, 0));
8391 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8392 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8393 "tx checksumming: %s]\n",
8394 rtl_chip_infos[chipset].jumbo_max,
8395 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8398 if (r8168_check_dash(tp))
8399 rtl8168_driver_start(tp);
8401 netif_carrier_off(dev);
8403 if (pci_dev_run_wake(pdev))
8404 pm_runtime_put_sync(&pdev->dev);
8409 static struct pci_driver rtl8169_pci_driver = {
8411 .id_table = rtl8169_pci_tbl,
8412 .probe = rtl_init_one,
8413 .remove = rtl_remove_one,
8414 .shutdown = rtl_shutdown,
8415 .driver.pm = RTL8169_PM_OPS,
8418 module_pci_driver(rtl8169_pci_driver);