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/phy.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/prefetch.h>
29 #include <linux/ipv6.h>
30 #include <net/ip6_checksum.h>
35 #define RTL8169_VERSION "2.3LK-NAPI"
36 #define MODULENAME "r8169"
38 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
40 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
42 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
43 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
44 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
45 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
46 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
47 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
48 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
49 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
50 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
51 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
52 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
53 #define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
54 #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
55 #define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
56 #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
58 #define R8169_MSG_DEFAULT \
59 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
61 #define TX_SLOTS_AVAIL(tp) \
62 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
64 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
65 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
66 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
68 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
70 static const int multicast_filter_limit = 32;
72 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
73 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
75 #define R8169_REGS_SIZE 256
76 #define R8169_RX_BUF_SIZE (SZ_16K - 1)
77 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
78 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
79 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
80 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
82 #define RTL8169_TX_TIMEOUT (6*HZ)
84 /* write/read MMIO register */
85 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
86 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
87 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
88 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
89 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
90 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
93 RTL_GIGA_MAC_VER_01 = 0,
144 RTL_GIGA_MAC_NONE = 0xff,
147 enum rtl_tx_desc_version {
152 #define JUMBO_1K ETH_DATA_LEN
153 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
154 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
155 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
156 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
158 #define _R(NAME,TD,FW,SZ) { \
165 static const struct {
167 enum rtl_tx_desc_version txd_version;
170 } rtl_chip_infos[] = {
172 [RTL_GIGA_MAC_VER_01] =
173 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K),
174 [RTL_GIGA_MAC_VER_02] =
175 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K),
176 [RTL_GIGA_MAC_VER_03] =
177 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K),
178 [RTL_GIGA_MAC_VER_04] =
179 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K),
180 [RTL_GIGA_MAC_VER_05] =
181 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
182 [RTL_GIGA_MAC_VER_06] =
183 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
185 [RTL_GIGA_MAC_VER_07] =
186 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
187 [RTL_GIGA_MAC_VER_08] =
188 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
189 [RTL_GIGA_MAC_VER_09] =
190 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
191 [RTL_GIGA_MAC_VER_10] =
192 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
193 [RTL_GIGA_MAC_VER_11] =
194 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
195 [RTL_GIGA_MAC_VER_12] =
196 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
197 [RTL_GIGA_MAC_VER_13] =
198 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
199 [RTL_GIGA_MAC_VER_14] =
200 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
201 [RTL_GIGA_MAC_VER_15] =
202 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
203 [RTL_GIGA_MAC_VER_16] =
204 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
205 [RTL_GIGA_MAC_VER_17] =
206 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
207 [RTL_GIGA_MAC_VER_18] =
208 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
209 [RTL_GIGA_MAC_VER_19] =
210 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
211 [RTL_GIGA_MAC_VER_20] =
212 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
213 [RTL_GIGA_MAC_VER_21] =
214 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
215 [RTL_GIGA_MAC_VER_22] =
216 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
217 [RTL_GIGA_MAC_VER_23] =
218 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
219 [RTL_GIGA_MAC_VER_24] =
220 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
221 [RTL_GIGA_MAC_VER_25] =
222 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1, JUMBO_9K),
223 [RTL_GIGA_MAC_VER_26] =
224 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2, JUMBO_9K),
225 [RTL_GIGA_MAC_VER_27] =
226 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
227 [RTL_GIGA_MAC_VER_28] =
228 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
229 [RTL_GIGA_MAC_VER_29] =
230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
231 [RTL_GIGA_MAC_VER_30] =
232 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
233 [RTL_GIGA_MAC_VER_31] =
234 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
235 [RTL_GIGA_MAC_VER_32] =
236 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1, JUMBO_9K),
237 [RTL_GIGA_MAC_VER_33] =
238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2, JUMBO_9K),
239 [RTL_GIGA_MAC_VER_34] =
240 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3, JUMBO_9K),
241 [RTL_GIGA_MAC_VER_35] =
242 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1, JUMBO_9K),
243 [RTL_GIGA_MAC_VER_36] =
244 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2, JUMBO_9K),
245 [RTL_GIGA_MAC_VER_37] =
246 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1, JUMBO_1K),
247 [RTL_GIGA_MAC_VER_38] =
248 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1, JUMBO_9K),
249 [RTL_GIGA_MAC_VER_39] =
250 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1, JUMBO_1K),
251 [RTL_GIGA_MAC_VER_40] =
252 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2, JUMBO_9K),
253 [RTL_GIGA_MAC_VER_41] =
254 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K),
255 [RTL_GIGA_MAC_VER_42] =
256 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3, JUMBO_9K),
257 [RTL_GIGA_MAC_VER_43] =
258 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2, JUMBO_1K),
259 [RTL_GIGA_MAC_VER_44] =
260 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2, JUMBO_9K),
261 [RTL_GIGA_MAC_VER_45] =
262 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1, JUMBO_9K),
263 [RTL_GIGA_MAC_VER_46] =
264 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2, JUMBO_9K),
265 [RTL_GIGA_MAC_VER_47] =
266 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1, JUMBO_1K),
267 [RTL_GIGA_MAC_VER_48] =
268 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2, JUMBO_1K),
269 [RTL_GIGA_MAC_VER_49] =
270 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
271 [RTL_GIGA_MAC_VER_50] =
272 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
273 [RTL_GIGA_MAC_VER_51] =
274 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
284 static const struct pci_device_id rtl8169_pci_tbl[] = {
285 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
286 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
287 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
288 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
289 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
290 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
291 { PCI_VENDOR_ID_DLINK, 0x4300,
292 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
293 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
294 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
295 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
296 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
297 { PCI_VENDOR_ID_LINKSYS, 0x1032,
298 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
300 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
304 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
306 static int use_dac = -1;
312 MAC0 = 0, /* Ethernet hardware address. */
314 MAR0 = 8, /* Multicast filter. */
315 CounterAddrLow = 0x10,
316 CounterAddrHigh = 0x14,
317 TxDescStartAddrLow = 0x20,
318 TxDescStartAddrHigh = 0x24,
319 TxHDescStartAddrLow = 0x28,
320 TxHDescStartAddrHigh = 0x2c,
329 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
330 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
333 #define RX128_INT_EN (1 << 15) /* 8111c and later */
334 #define RX_MULTI_EN (1 << 14) /* 8111c only */
335 #define RXCFG_FIFO_SHIFT 13
336 /* No threshold before first PCI xfer */
337 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
338 #define RX_EARLY_OFF (1 << 11)
339 #define RXCFG_DMA_SHIFT 8
340 /* Unlimited maximum PCI burst. */
341 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
348 #define PME_SIGNAL (1 << 5) /* 8168c and later */
360 #define RTL_COALESCE_MASK 0x0f
361 #define RTL_COALESCE_SHIFT 4
362 #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
363 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
365 RxDescAddrLow = 0xe4,
366 RxDescAddrHigh = 0xe8,
367 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
369 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
371 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
373 #define TxPacketMax (8064 >> 7)
374 #define EarlySize 0x27
377 FuncEventMask = 0xf4,
378 FuncPresetState = 0xf8,
383 FuncForceEvent = 0xfc,
386 enum rtl8168_8101_registers {
389 #define CSIAR_FLAG 0x80000000
390 #define CSIAR_WRITE_CMD 0x80000000
391 #define CSIAR_BYTE_ENABLE 0x0000f000
392 #define CSIAR_ADDR_MASK 0x00000fff
395 #define EPHYAR_FLAG 0x80000000
396 #define EPHYAR_WRITE_CMD 0x80000000
397 #define EPHYAR_REG_MASK 0x1f
398 #define EPHYAR_REG_SHIFT 16
399 #define EPHYAR_DATA_MASK 0xffff
401 #define PFM_EN (1 << 6)
402 #define TX_10M_PS_EN (1 << 7)
404 #define FIX_NAK_1 (1 << 4)
405 #define FIX_NAK_2 (1 << 3)
408 #define NOW_IS_OOB (1 << 7)
409 #define TX_EMPTY (1 << 5)
410 #define RX_EMPTY (1 << 4)
411 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
412 #define EN_NDP (1 << 3)
413 #define EN_OOB_RESET (1 << 2)
414 #define LINK_LIST_RDY (1 << 1)
416 #define EFUSEAR_FLAG 0x80000000
417 #define EFUSEAR_WRITE_CMD 0x80000000
418 #define EFUSEAR_READ_CMD 0x00000000
419 #define EFUSEAR_REG_MASK 0x03ff
420 #define EFUSEAR_REG_SHIFT 8
421 #define EFUSEAR_DATA_MASK 0xff
423 #define PFM_D3COLD_EN (1 << 6)
426 enum rtl8168_registers {
431 #define ERIAR_FLAG 0x80000000
432 #define ERIAR_WRITE_CMD 0x80000000
433 #define ERIAR_READ_CMD 0x00000000
434 #define ERIAR_ADDR_BYTE_ALIGN 4
435 #define ERIAR_TYPE_SHIFT 16
436 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
437 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
438 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
439 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
440 #define ERIAR_MASK_SHIFT 12
441 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
442 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
443 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
444 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
445 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
446 EPHY_RXER_NUM = 0x7c,
447 OCPDR = 0xb0, /* OCP GPHY access */
448 #define OCPDR_WRITE_CMD 0x80000000
449 #define OCPDR_READ_CMD 0x00000000
450 #define OCPDR_REG_MASK 0x7f
451 #define OCPDR_GPHY_REG_SHIFT 16
452 #define OCPDR_DATA_MASK 0xffff
454 #define OCPAR_FLAG 0x80000000
455 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
456 #define OCPAR_GPHY_READ_CMD 0x0000f060
458 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
459 MISC = 0xf0, /* 8168e only. */
460 #define TXPLA_RST (1 << 29)
461 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
462 #define PWM_EN (1 << 22)
463 #define RXDV_GATED_EN (1 << 19)
464 #define EARLY_TALLY_EN (1 << 16)
467 enum rtl_register_content {
468 /* InterruptStatusBits */
472 TxDescUnavail = 0x0080,
496 /* TXPoll register p.5 */
497 HPQ = 0x80, /* Poll cmd on the high prio queue */
498 NPQ = 0x40, /* Poll cmd on the low prio queue */
499 FSWInt = 0x01, /* Forced software interrupt */
503 Cfg9346_Unlock = 0xc0,
508 AcceptBroadcast = 0x08,
509 AcceptMulticast = 0x04,
511 AcceptAllPhys = 0x01,
512 #define RX_CONFIG_ACCEPT_MASK 0x3f
515 TxInterFrameGapShift = 24,
516 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
518 /* Config1 register p.24 */
521 Speed_down = (1 << 4),
525 PMEnable = (1 << 0), /* Power Management Enable */
527 /* Config2 register p. 25 */
528 ClkReqEn = (1 << 7), /* Clock Request Enable */
529 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
530 PCI_Clock_66MHz = 0x01,
531 PCI_Clock_33MHz = 0x00,
533 /* Config3 register p.25 */
534 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
535 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
536 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
537 Rdy_to_L23 = (1 << 1), /* L23 Enable */
538 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
540 /* Config4 register */
541 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
543 /* Config5 register p.27 */
544 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
545 MWF = (1 << 5), /* Accept Multicast wakeup frame */
546 UWF = (1 << 4), /* Accept Unicast wakeup frame */
548 LanWake = (1 << 1), /* LanWake enable/disable */
549 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
550 ASPM_en = (1 << 0), /* ASPM enable */
553 EnableBist = (1 << 15), // 8168 8101
554 Mac_dbgo_oe = (1 << 14), // 8168 8101
555 Normal_mode = (1 << 13), // unused
556 Force_half_dup = (1 << 12), // 8168 8101
557 Force_rxflow_en = (1 << 11), // 8168 8101
558 Force_txflow_en = (1 << 10), // 8168 8101
559 Cxpl_dbg_sel = (1 << 9), // 8168 8101
560 ASF = (1 << 8), // 8168 8101
561 PktCntrDisable = (1 << 7), // 8168 8101
562 Mac_dbgo_sel = 0x001c, // 8168
567 #define INTT_MASK GENMASK(1, 0)
568 INTT_0 = 0x0000, // 8168
569 INTT_1 = 0x0001, // 8168
570 INTT_2 = 0x0002, // 8168
571 INTT_3 = 0x0003, // 8168
573 /* rtl8169_PHYstatus */
584 TBILinkOK = 0x02000000,
586 /* ResetCounterCommand */
589 /* DumpCounterCommand */
592 /* magic enable v2 */
593 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
597 /* First doubleword. */
598 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
599 RingEnd = (1 << 30), /* End of descriptor ring */
600 FirstFrag = (1 << 29), /* First segment of a packet */
601 LastFrag = (1 << 28), /* Final segment of a packet */
605 enum rtl_tx_desc_bit {
606 /* First doubleword. */
607 TD_LSO = (1 << 27), /* Large Send Offload */
608 #define TD_MSS_MAX 0x07ffu /* MSS value */
610 /* Second doubleword. */
611 TxVlanTag = (1 << 17), /* Add VLAN tag */
614 /* 8169, 8168b and 810x except 8102e. */
615 enum rtl_tx_desc_bit_0 {
616 /* First doubleword. */
617 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
618 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
619 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
620 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
623 /* 8102e, 8168c and beyond. */
624 enum rtl_tx_desc_bit_1 {
625 /* First doubleword. */
626 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
627 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
628 #define GTTCPHO_SHIFT 18
629 #define GTTCPHO_MAX 0x7fU
631 /* Second doubleword. */
632 #define TCPHO_SHIFT 18
633 #define TCPHO_MAX 0x3ffU
634 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
635 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
636 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
637 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
638 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
641 enum rtl_rx_desc_bit {
643 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
644 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
646 #define RxProtoUDP (PID1)
647 #define RxProtoTCP (PID0)
648 #define RxProtoIP (PID1 | PID0)
649 #define RxProtoMask RxProtoIP
651 IPFail = (1 << 16), /* IP checksum failed */
652 UDPFail = (1 << 15), /* UDP/IP checksum failed */
653 TCPFail = (1 << 14), /* TCP/IP checksum failed */
654 RxVlanTag = (1 << 16), /* VLAN tag available */
657 #define RsvdMask 0x3fffc000
658 #define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
675 u8 __pad[sizeof(void *) - sizeof(u32)];
678 struct rtl8169_counters {
685 __le32 tx_one_collision;
686 __le32 tx_multi_collision;
694 struct rtl8169_tc_offsets {
697 __le32 tx_multi_collision;
702 RTL_FLAG_TASK_ENABLED,
703 RTL_FLAG_TASK_SLOW_PENDING,
704 RTL_FLAG_TASK_RESET_PENDING,
708 struct rtl8169_stats {
711 struct u64_stats_sync syncp;
714 struct rtl8169_private {
715 void __iomem *mmio_addr; /* memory map physical address */
716 struct pci_dev *pci_dev;
717 struct net_device *dev;
718 struct napi_struct napi;
721 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
722 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
724 struct rtl8169_stats rx_stats;
725 struct rtl8169_stats tx_stats;
726 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
727 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
728 dma_addr_t TxPhyAddr;
729 dma_addr_t RxPhyAddr;
730 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
731 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
735 const struct rtl_coalesce_info *coalesce_info;
738 void (*write)(struct rtl8169_private *, int, int);
739 int (*read)(struct rtl8169_private *, int);
743 void (*enable)(struct rtl8169_private *);
744 void (*disable)(struct rtl8169_private *);
747 void (*hw_start)(struct rtl8169_private *tp);
748 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
751 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
753 struct work_struct work;
756 unsigned supports_gmii:1;
757 struct mii_bus *mii_bus;
758 dma_addr_t counters_phys_addr;
759 struct rtl8169_counters *counters;
760 struct rtl8169_tc_offsets tc_offset;
764 const struct firmware *fw;
766 #define RTL_VER_SIZE 32
768 char version[RTL_VER_SIZE];
770 struct rtl_fw_phy_action {
775 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
780 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
781 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
782 module_param(use_dac, int, 0);
783 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
784 module_param_named(debug, debug.msg_enable, int, 0);
785 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
786 MODULE_LICENSE("GPL");
787 MODULE_VERSION(RTL8169_VERSION);
788 MODULE_FIRMWARE(FIRMWARE_8168D_1);
789 MODULE_FIRMWARE(FIRMWARE_8168D_2);
790 MODULE_FIRMWARE(FIRMWARE_8168E_1);
791 MODULE_FIRMWARE(FIRMWARE_8168E_2);
792 MODULE_FIRMWARE(FIRMWARE_8168E_3);
793 MODULE_FIRMWARE(FIRMWARE_8105E_1);
794 MODULE_FIRMWARE(FIRMWARE_8168F_1);
795 MODULE_FIRMWARE(FIRMWARE_8168F_2);
796 MODULE_FIRMWARE(FIRMWARE_8402_1);
797 MODULE_FIRMWARE(FIRMWARE_8411_1);
798 MODULE_FIRMWARE(FIRMWARE_8411_2);
799 MODULE_FIRMWARE(FIRMWARE_8106E_1);
800 MODULE_FIRMWARE(FIRMWARE_8106E_2);
801 MODULE_FIRMWARE(FIRMWARE_8168G_2);
802 MODULE_FIRMWARE(FIRMWARE_8168G_3);
803 MODULE_FIRMWARE(FIRMWARE_8168H_1);
804 MODULE_FIRMWARE(FIRMWARE_8168H_2);
805 MODULE_FIRMWARE(FIRMWARE_8107E_1);
806 MODULE_FIRMWARE(FIRMWARE_8107E_2);
808 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
810 return &tp->pci_dev->dev;
813 static void rtl_lock_work(struct rtl8169_private *tp)
815 mutex_lock(&tp->wk.mutex);
818 static void rtl_unlock_work(struct rtl8169_private *tp)
820 mutex_unlock(&tp->wk.mutex);
823 static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
825 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
826 PCI_EXP_DEVCTL_READRQ, force);
830 bool (*check)(struct rtl8169_private *);
834 static void rtl_udelay(unsigned int d)
839 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
840 void (*delay)(unsigned int), unsigned int d, int n,
845 for (i = 0; i < n; i++) {
847 if (c->check(tp) == high)
850 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
851 c->msg, !high, n, d);
855 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
856 const struct rtl_cond *c,
857 unsigned int d, int n)
859 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
862 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
863 const struct rtl_cond *c,
864 unsigned int d, int n)
866 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
869 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
870 const struct rtl_cond *c,
871 unsigned int d, int n)
873 return rtl_loop_wait(tp, c, msleep, d, n, true);
876 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
877 const struct rtl_cond *c,
878 unsigned int d, int n)
880 return rtl_loop_wait(tp, c, msleep, d, n, false);
883 #define DECLARE_RTL_COND(name) \
884 static bool name ## _check(struct rtl8169_private *); \
886 static const struct rtl_cond name = { \
887 .check = name ## _check, \
891 static bool name ## _check(struct rtl8169_private *tp)
893 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
895 if (reg & 0xffff0001) {
896 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
902 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
904 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
907 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
909 if (rtl_ocp_reg_failure(tp, reg))
912 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
914 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
917 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
919 if (rtl_ocp_reg_failure(tp, reg))
922 RTL_W32(tp, GPHY_OCP, reg << 15);
924 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
925 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
928 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
930 if (rtl_ocp_reg_failure(tp, reg))
933 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
936 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
938 if (rtl_ocp_reg_failure(tp, reg))
941 RTL_W32(tp, OCPDR, reg << 15);
943 return RTL_R32(tp, OCPDR);
946 #define OCP_STD_PHY_BASE 0xa400
948 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
951 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
955 if (tp->ocp_base != OCP_STD_PHY_BASE)
958 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
961 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
963 if (tp->ocp_base != OCP_STD_PHY_BASE)
966 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
969 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
972 tp->ocp_base = value << 4;
976 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
979 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
981 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
984 DECLARE_RTL_COND(rtl_phyar_cond)
986 return RTL_R32(tp, PHYAR) & 0x80000000;
989 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
991 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
993 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
995 * According to hardware specs a 20us delay is required after write
996 * complete indication, but before sending next command.
1001 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1005 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
1007 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1008 RTL_R32(tp, PHYAR) & 0xffff : ~0;
1011 * According to hardware specs a 20us delay is required after read
1012 * complete indication, but before sending next command.
1019 DECLARE_RTL_COND(rtl_ocpar_cond)
1021 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
1024 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1026 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1027 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1028 RTL_W32(tp, EPHY_RXER_NUM, 0);
1030 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1033 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1035 r8168dp_1_mdio_access(tp, reg,
1036 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1039 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1041 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1044 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1045 RTL_W32(tp, EPHY_RXER_NUM, 0);
1047 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1048 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
1051 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1053 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
1055 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1058 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
1060 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1063 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1065 r8168dp_2_mdio_start(tp);
1067 r8169_mdio_write(tp, reg, value);
1069 r8168dp_2_mdio_stop(tp);
1072 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1076 r8168dp_2_mdio_start(tp);
1078 value = r8169_mdio_read(tp, reg);
1080 r8168dp_2_mdio_stop(tp);
1085 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1087 tp->mdio_ops.write(tp, location, val);
1090 static int rtl_readphy(struct rtl8169_private *tp, int location)
1092 return tp->mdio_ops.read(tp, location);
1095 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1097 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1100 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1104 val = rtl_readphy(tp, reg_addr);
1105 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1108 DECLARE_RTL_COND(rtl_ephyar_cond)
1110 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1113 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1115 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1116 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1118 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1123 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1125 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1127 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1128 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1131 DECLARE_RTL_COND(rtl_eriar_cond)
1133 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
1136 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1139 BUG_ON((addr & 3) || (mask == 0));
1140 RTL_W32(tp, ERIDR, val);
1141 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1143 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1146 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1148 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1150 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1151 RTL_R32(tp, ERIDR) : ~0;
1154 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1159 val = rtl_eri_read(tp, addr, type);
1160 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1163 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1165 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1166 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1167 RTL_R32(tp, OCPDR) : ~0;
1170 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1172 return rtl_eri_read(tp, reg, ERIAR_OOB);
1175 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1177 switch (tp->mac_version) {
1178 case RTL_GIGA_MAC_VER_27:
1179 case RTL_GIGA_MAC_VER_28:
1180 case RTL_GIGA_MAC_VER_31:
1181 return r8168dp_ocp_read(tp, mask, reg);
1182 case RTL_GIGA_MAC_VER_49:
1183 case RTL_GIGA_MAC_VER_50:
1184 case RTL_GIGA_MAC_VER_51:
1185 return r8168ep_ocp_read(tp, mask, reg);
1192 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1195 RTL_W32(tp, OCPDR, data);
1196 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1197 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1200 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1203 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1207 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1209 switch (tp->mac_version) {
1210 case RTL_GIGA_MAC_VER_27:
1211 case RTL_GIGA_MAC_VER_28:
1212 case RTL_GIGA_MAC_VER_31:
1213 r8168dp_ocp_write(tp, mask, reg, data);
1215 case RTL_GIGA_MAC_VER_49:
1216 case RTL_GIGA_MAC_VER_50:
1217 case RTL_GIGA_MAC_VER_51:
1218 r8168ep_ocp_write(tp, mask, reg, data);
1226 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1228 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1230 ocp_write(tp, 0x1, 0x30, 0x00000001);
1233 #define OOB_CMD_RESET 0x00
1234 #define OOB_CMD_DRIVER_START 0x05
1235 #define OOB_CMD_DRIVER_STOP 0x06
1237 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1239 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1242 DECLARE_RTL_COND(rtl_ocp_read_cond)
1246 reg = rtl8168_get_ocp_reg(tp);
1248 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1251 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1253 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1256 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1258 return RTL_R8(tp, IBISR0) & 0x20;
1261 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1263 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1264 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1265 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1266 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1269 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1271 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1272 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1275 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1277 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1278 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1279 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1282 static void rtl8168_driver_start(struct rtl8169_private *tp)
1284 switch (tp->mac_version) {
1285 case RTL_GIGA_MAC_VER_27:
1286 case RTL_GIGA_MAC_VER_28:
1287 case RTL_GIGA_MAC_VER_31:
1288 rtl8168dp_driver_start(tp);
1290 case RTL_GIGA_MAC_VER_49:
1291 case RTL_GIGA_MAC_VER_50:
1292 case RTL_GIGA_MAC_VER_51:
1293 rtl8168ep_driver_start(tp);
1301 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1303 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1304 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1307 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1309 rtl8168ep_stop_cmac(tp);
1310 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1311 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1312 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1315 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1317 switch (tp->mac_version) {
1318 case RTL_GIGA_MAC_VER_27:
1319 case RTL_GIGA_MAC_VER_28:
1320 case RTL_GIGA_MAC_VER_31:
1321 rtl8168dp_driver_stop(tp);
1323 case RTL_GIGA_MAC_VER_49:
1324 case RTL_GIGA_MAC_VER_50:
1325 case RTL_GIGA_MAC_VER_51:
1326 rtl8168ep_driver_stop(tp);
1334 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1336 u16 reg = rtl8168_get_ocp_reg(tp);
1338 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
1341 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1343 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
1346 static bool r8168_check_dash(struct rtl8169_private *tp)
1348 switch (tp->mac_version) {
1349 case RTL_GIGA_MAC_VER_27:
1350 case RTL_GIGA_MAC_VER_28:
1351 case RTL_GIGA_MAC_VER_31:
1352 return r8168dp_check_dash(tp);
1353 case RTL_GIGA_MAC_VER_49:
1354 case RTL_GIGA_MAC_VER_50:
1355 case RTL_GIGA_MAC_VER_51:
1356 return r8168ep_check_dash(tp);
1368 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1369 const struct exgmac_reg *r, int len)
1372 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1377 DECLARE_RTL_COND(rtl_efusear_cond)
1379 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1382 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1384 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1386 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1387 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1390 static u16 rtl_get_events(struct rtl8169_private *tp)
1392 return RTL_R16(tp, IntrStatus);
1395 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1397 RTL_W16(tp, IntrStatus, bits);
1401 static void rtl_irq_disable(struct rtl8169_private *tp)
1403 RTL_W16(tp, IntrMask, 0);
1407 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1409 RTL_W16(tp, IntrMask, bits);
1412 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1413 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1414 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1416 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1418 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1421 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1423 rtl_irq_disable(tp);
1424 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1425 RTL_R8(tp, ChipCmd);
1428 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1430 struct net_device *dev = tp->dev;
1432 if (!netif_running(dev))
1435 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1436 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1437 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
1438 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1440 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1442 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
1443 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1445 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1448 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1450 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1453 /* Reset packet filter */
1454 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1456 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1458 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1459 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1460 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
1461 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1463 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1466 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1468 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1471 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1472 if (RTL_R8(tp, PHYstatus) & _10bps) {
1473 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1475 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1478 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1484 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1486 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1491 options = RTL_R8(tp, Config1);
1492 if (!(options & PMEnable))
1495 options = RTL_R8(tp, Config3);
1496 if (options & LinkUp)
1497 wolopts |= WAKE_PHY;
1498 switch (tp->mac_version) {
1499 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1500 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1501 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1502 wolopts |= WAKE_MAGIC;
1505 if (options & MagicPacket)
1506 wolopts |= WAKE_MAGIC;
1510 options = RTL_R8(tp, Config5);
1512 wolopts |= WAKE_UCAST;
1514 wolopts |= WAKE_BCAST;
1516 wolopts |= WAKE_MCAST;
1521 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1523 struct rtl8169_private *tp = netdev_priv(dev);
1526 wol->supported = WAKE_ANY;
1527 wol->wolopts = tp->saved_wolopts;
1528 rtl_unlock_work(tp);
1531 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1533 unsigned int i, tmp;
1534 static const struct {
1539 { WAKE_PHY, Config3, LinkUp },
1540 { WAKE_UCAST, Config5, UWF },
1541 { WAKE_BCAST, Config5, BWF },
1542 { WAKE_MCAST, Config5, MWF },
1543 { WAKE_ANY, Config5, LanWake },
1544 { WAKE_MAGIC, Config3, MagicPacket }
1548 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
1550 switch (tp->mac_version) {
1551 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1552 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1553 tmp = ARRAY_SIZE(cfg) - 1;
1554 if (wolopts & WAKE_MAGIC)
1570 tmp = ARRAY_SIZE(cfg);
1574 for (i = 0; i < tmp; i++) {
1575 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1576 if (wolopts & cfg[i].opt)
1577 options |= cfg[i].mask;
1578 RTL_W8(tp, cfg[i].reg, options);
1581 switch (tp->mac_version) {
1582 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1583 options = RTL_R8(tp, Config1) & ~PMEnable;
1585 options |= PMEnable;
1586 RTL_W8(tp, Config1, options);
1589 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
1591 options |= PME_SIGNAL;
1592 RTL_W8(tp, Config2, options);
1596 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
1599 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1601 struct rtl8169_private *tp = netdev_priv(dev);
1602 struct device *d = tp_to_dev(tp);
1604 if (wol->wolopts & ~WAKE_ANY)
1607 pm_runtime_get_noresume(d);
1611 tp->saved_wolopts = wol->wolopts;
1613 if (pm_runtime_active(d))
1614 __rtl8169_set_wol(tp, tp->saved_wolopts);
1616 rtl_unlock_work(tp);
1618 device_set_wakeup_enable(d, tp->saved_wolopts);
1620 pm_runtime_put_noidle(d);
1625 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1627 return rtl_chip_infos[tp->mac_version].fw_name;
1630 static void rtl8169_get_drvinfo(struct net_device *dev,
1631 struct ethtool_drvinfo *info)
1633 struct rtl8169_private *tp = netdev_priv(dev);
1634 struct rtl_fw *rtl_fw = tp->rtl_fw;
1636 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1637 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1638 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1639 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1640 if (!IS_ERR_OR_NULL(rtl_fw))
1641 strlcpy(info->fw_version, rtl_fw->version,
1642 sizeof(info->fw_version));
1645 static int rtl8169_get_regs_len(struct net_device *dev)
1647 return R8169_REGS_SIZE;
1650 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1651 netdev_features_t features)
1653 struct rtl8169_private *tp = netdev_priv(dev);
1655 if (dev->mtu > TD_MSS_MAX)
1656 features &= ~NETIF_F_ALL_TSO;
1658 if (dev->mtu > JUMBO_1K &&
1659 tp->mac_version > RTL_GIGA_MAC_VER_06)
1660 features &= ~NETIF_F_IP_CSUM;
1665 static int rtl8169_set_features(struct net_device *dev,
1666 netdev_features_t features)
1668 struct rtl8169_private *tp = netdev_priv(dev);
1673 rx_config = RTL_R32(tp, RxConfig);
1674 if (features & NETIF_F_RXALL)
1675 rx_config |= (AcceptErr | AcceptRunt);
1677 rx_config &= ~(AcceptErr | AcceptRunt);
1679 RTL_W32(tp, RxConfig, rx_config);
1681 if (features & NETIF_F_RXCSUM)
1682 tp->cp_cmd |= RxChkSum;
1684 tp->cp_cmd &= ~RxChkSum;
1686 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1687 tp->cp_cmd |= RxVlan;
1689 tp->cp_cmd &= ~RxVlan;
1691 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1692 RTL_R16(tp, CPlusCmd);
1694 rtl_unlock_work(tp);
1699 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1701 return (skb_vlan_tag_present(skb)) ?
1702 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1705 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1707 u32 opts2 = le32_to_cpu(desc->opts2);
1709 if (opts2 & RxVlanTag)
1710 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1713 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1716 struct rtl8169_private *tp = netdev_priv(dev);
1717 u32 __iomem *data = tp->mmio_addr;
1722 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1723 memcpy_fromio(dw++, data++, 4);
1724 rtl_unlock_work(tp);
1727 static u32 rtl8169_get_msglevel(struct net_device *dev)
1729 struct rtl8169_private *tp = netdev_priv(dev);
1731 return tp->msg_enable;
1734 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1736 struct rtl8169_private *tp = netdev_priv(dev);
1738 tp->msg_enable = value;
1741 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1748 "tx_single_collisions",
1749 "tx_multi_collisions",
1757 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1761 return ARRAY_SIZE(rtl8169_gstrings);
1767 DECLARE_RTL_COND(rtl_counters_cond)
1769 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
1772 static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
1774 dma_addr_t paddr = tp->counters_phys_addr;
1777 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1778 RTL_R32(tp, CounterAddrHigh);
1779 cmd = (u64)paddr & DMA_BIT_MASK(32);
1780 RTL_W32(tp, CounterAddrLow, cmd);
1781 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
1783 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
1786 static bool rtl8169_reset_counters(struct rtl8169_private *tp)
1789 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1792 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1795 return rtl8169_do_counters(tp, CounterReset);
1798 static bool rtl8169_update_counters(struct rtl8169_private *tp)
1801 * Some chips are unable to dump tally counters when the receiver
1804 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
1807 return rtl8169_do_counters(tp, CounterDump);
1810 static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
1812 struct rtl8169_counters *counters = tp->counters;
1816 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1817 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1818 * reset by a power cycle, while the counter values collected by the
1819 * driver are reset at every driver unload/load cycle.
1821 * To make sure the HW values returned by @get_stats64 match the SW
1822 * values, we collect the initial values at first open(*) and use them
1823 * as offsets to normalize the values returned by @get_stats64.
1825 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1826 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1827 * set at open time by rtl_hw_start.
1830 if (tp->tc_offset.inited)
1833 /* If both, reset and update fail, propagate to caller. */
1834 if (rtl8169_reset_counters(tp))
1837 if (rtl8169_update_counters(tp))
1840 tp->tc_offset.tx_errors = counters->tx_errors;
1841 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1842 tp->tc_offset.tx_aborted = counters->tx_aborted;
1843 tp->tc_offset.inited = true;
1848 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1849 struct ethtool_stats *stats, u64 *data)
1851 struct rtl8169_private *tp = netdev_priv(dev);
1852 struct device *d = tp_to_dev(tp);
1853 struct rtl8169_counters *counters = tp->counters;
1857 pm_runtime_get_noresume(d);
1859 if (pm_runtime_active(d))
1860 rtl8169_update_counters(tp);
1862 pm_runtime_put_noidle(d);
1864 data[0] = le64_to_cpu(counters->tx_packets);
1865 data[1] = le64_to_cpu(counters->rx_packets);
1866 data[2] = le64_to_cpu(counters->tx_errors);
1867 data[3] = le32_to_cpu(counters->rx_errors);
1868 data[4] = le16_to_cpu(counters->rx_missed);
1869 data[5] = le16_to_cpu(counters->align_errors);
1870 data[6] = le32_to_cpu(counters->tx_one_collision);
1871 data[7] = le32_to_cpu(counters->tx_multi_collision);
1872 data[8] = le64_to_cpu(counters->rx_unicast);
1873 data[9] = le64_to_cpu(counters->rx_broadcast);
1874 data[10] = le32_to_cpu(counters->rx_multicast);
1875 data[11] = le16_to_cpu(counters->tx_aborted);
1876 data[12] = le16_to_cpu(counters->tx_underun);
1879 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1883 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1889 * Interrupt coalescing
1891 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1892 * > 8169, 8168 and 810x line of chipsets
1894 * 8169, 8168, and 8136(810x) serial chipsets support it.
1896 * > 2 - the Tx timer unit at gigabit speed
1898 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1899 * (0xe0) bit 1 and bit 0.
1902 * bit[1:0] \ speed 1000M 100M 10M
1903 * 0 0 320ns 2.56us 40.96us
1904 * 0 1 2.56us 20.48us 327.7us
1905 * 1 0 5.12us 40.96us 655.4us
1906 * 1 1 10.24us 81.92us 1.31ms
1909 * bit[1:0] \ speed 1000M 100M 10M
1910 * 0 0 5us 2.56us 40.96us
1911 * 0 1 40us 20.48us 327.7us
1912 * 1 0 80us 40.96us 655.4us
1913 * 1 1 160us 81.92us 1.31ms
1916 /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1917 struct rtl_coalesce_scale {
1922 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
1923 struct rtl_coalesce_info {
1925 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1928 /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1929 #define rxtx_x1822(r, t) { \
1932 {{(r)*8*2, (t)*8*2}}, \
1933 {{(r)*8*2*2, (t)*8*2*2}}, \
1935 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1936 /* speed delays: rx00 tx00 */
1937 { SPEED_10, rxtx_x1822(40960, 40960) },
1938 { SPEED_100, rxtx_x1822( 2560, 2560) },
1939 { SPEED_1000, rxtx_x1822( 320, 320) },
1943 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1944 /* speed delays: rx00 tx00 */
1945 { SPEED_10, rxtx_x1822(40960, 40960) },
1946 { SPEED_100, rxtx_x1822( 2560, 2560) },
1947 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1952 /* get rx/tx scale vector corresponding to current speed */
1953 static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1955 struct rtl8169_private *tp = netdev_priv(dev);
1956 struct ethtool_link_ksettings ecmd;
1957 const struct rtl_coalesce_info *ci;
1960 rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
1964 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
1965 if (ecmd.base.speed == ci->speed) {
1970 return ERR_PTR(-ELNRNG);
1973 static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1975 struct rtl8169_private *tp = netdev_priv(dev);
1976 const struct rtl_coalesce_info *ci;
1977 const struct rtl_coalesce_scale *scale;
1981 } coal_settings [] = {
1982 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1983 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1984 }, *p = coal_settings;
1988 memset(ec, 0, sizeof(*ec));
1990 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1991 ci = rtl_coalesce_info(dev);
1995 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
1997 /* read IntrMitigate and adjust according to scale */
1998 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
1999 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2000 w >>= RTL_COALESCE_SHIFT;
2001 *p->usecs = w & RTL_COALESCE_MASK;
2004 for (i = 0; i < 2; i++) {
2005 p = coal_settings + i;
2006 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2009 * ethtool_coalesce says it is illegal to set both usecs and
2012 if (!*p->usecs && !*p->max_frames)
2019 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2020 static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2021 struct net_device *dev, u32 nsec, u16 *cp01)
2023 const struct rtl_coalesce_info *ci;
2026 ci = rtl_coalesce_info(dev);
2028 return ERR_CAST(ci);
2030 for (i = 0; i < 4; i++) {
2031 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2032 ci->scalev[i].nsecs[1]);
2033 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2035 return &ci->scalev[i];
2039 return ERR_PTR(-EINVAL);
2042 static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2044 struct rtl8169_private *tp = netdev_priv(dev);
2045 const struct rtl_coalesce_scale *scale;
2049 } coal_settings [] = {
2050 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2051 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2052 }, *p = coal_settings;
2056 scale = rtl_coalesce_choose_scale(dev,
2057 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2059 return PTR_ERR(scale);
2061 for (i = 0; i < 2; i++, p++) {
2065 * accept max_frames=1 we returned in rtl_get_coalesce.
2066 * accept it not only when usecs=0 because of e.g. the following scenario:
2068 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2069 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2070 * - then user does `ethtool -C eth0 rx-usecs 100`
2072 * since ethtool sends to kernel whole ethtool_coalesce
2073 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2074 * we'll reject it below in `frames % 4 != 0`.
2076 if (p->frames == 1) {
2080 units = p->usecs * 1000 / scale->nsecs[i];
2081 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2084 w <<= RTL_COALESCE_SHIFT;
2086 w <<= RTL_COALESCE_SHIFT;
2087 w |= p->frames >> 2;
2092 RTL_W16(tp, IntrMitigate, swab16(w));
2094 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
2095 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2096 RTL_R16(tp, CPlusCmd);
2098 rtl_unlock_work(tp);
2103 static const struct ethtool_ops rtl8169_ethtool_ops = {
2104 .get_drvinfo = rtl8169_get_drvinfo,
2105 .get_regs_len = rtl8169_get_regs_len,
2106 .get_link = ethtool_op_get_link,
2107 .get_coalesce = rtl_get_coalesce,
2108 .set_coalesce = rtl_set_coalesce,
2109 .get_msglevel = rtl8169_get_msglevel,
2110 .set_msglevel = rtl8169_set_msglevel,
2111 .get_regs = rtl8169_get_regs,
2112 .get_wol = rtl8169_get_wol,
2113 .set_wol = rtl8169_set_wol,
2114 .get_strings = rtl8169_get_strings,
2115 .get_sset_count = rtl8169_get_sset_count,
2116 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2117 .get_ts_info = ethtool_op_get_ts_info,
2118 .nway_reset = phy_ethtool_nway_reset,
2119 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2120 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2123 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2127 * The driver currently handles the 8168Bf and the 8168Be identically
2128 * but they can be identified more specifically through the test below
2131 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2133 * Same thing for the 8101Eb and the 8101Ec:
2135 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2137 static const struct rtl_mac_info {
2142 /* 8168EP family. */
2143 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2144 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2145 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2148 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2149 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2152 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2153 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2154 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2155 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2158 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2159 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2160 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2163 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2164 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2165 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2168 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2169 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2171 /* 8168DP family. */
2172 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2173 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2174 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2177 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2178 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2179 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2180 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2181 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2182 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2183 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2186 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2187 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2188 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2191 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2192 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2193 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2194 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2195 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2196 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2197 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2198 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2199 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2200 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2201 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2202 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2203 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2204 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2205 /* FIXME: where did these entries come from ? -- FR */
2206 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2207 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2210 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2211 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2212 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2213 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2214 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2215 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2218 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2220 const struct rtl_mac_info *p = mac_info;
2223 reg = RTL_R32(tp, TxConfig);
2224 while ((reg & p->mask) != p->val)
2226 tp->mac_version = p->mac_version;
2228 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2229 dev_notice(tp_to_dev(tp),
2230 "unknown MAC, using family default\n");
2231 tp->mac_version = default_version;
2232 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2233 tp->mac_version = tp->supports_gmii ?
2234 RTL_GIGA_MAC_VER_42 :
2235 RTL_GIGA_MAC_VER_43;
2236 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2237 tp->mac_version = tp->supports_gmii ?
2238 RTL_GIGA_MAC_VER_45 :
2239 RTL_GIGA_MAC_VER_47;
2240 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2241 tp->mac_version = tp->supports_gmii ?
2242 RTL_GIGA_MAC_VER_46 :
2243 RTL_GIGA_MAC_VER_48;
2247 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2249 netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
2257 static void rtl_writephy_batch(struct rtl8169_private *tp,
2258 const struct phy_reg *regs, int len)
2261 rtl_writephy(tp, regs->reg, regs->val);
2266 #define PHY_READ 0x00000000
2267 #define PHY_DATA_OR 0x10000000
2268 #define PHY_DATA_AND 0x20000000
2269 #define PHY_BJMPN 0x30000000
2270 #define PHY_MDIO_CHG 0x40000000
2271 #define PHY_CLEAR_READCOUNT 0x70000000
2272 #define PHY_WRITE 0x80000000
2273 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2274 #define PHY_COMP_EQ_SKIPN 0xa0000000
2275 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2276 #define PHY_WRITE_PREVIOUS 0xc0000000
2277 #define PHY_SKIPN 0xd0000000
2278 #define PHY_DELAY_MS 0xe0000000
2282 char version[RTL_VER_SIZE];
2288 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2290 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2292 const struct firmware *fw = rtl_fw->fw;
2293 struct fw_info *fw_info = (struct fw_info *)fw->data;
2294 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2295 char *version = rtl_fw->version;
2298 if (fw->size < FW_OPCODE_SIZE)
2301 if (!fw_info->magic) {
2302 size_t i, size, start;
2305 if (fw->size < sizeof(*fw_info))
2308 for (i = 0; i < fw->size; i++)
2309 checksum += fw->data[i];
2313 start = le32_to_cpu(fw_info->fw_start);
2314 if (start > fw->size)
2317 size = le32_to_cpu(fw_info->fw_len);
2318 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2321 memcpy(version, fw_info->version, RTL_VER_SIZE);
2323 pa->code = (__le32 *)(fw->data + start);
2326 if (fw->size % FW_OPCODE_SIZE)
2329 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2331 pa->code = (__le32 *)fw->data;
2332 pa->size = fw->size / FW_OPCODE_SIZE;
2334 version[RTL_VER_SIZE - 1] = 0;
2341 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2342 struct rtl_fw_phy_action *pa)
2347 for (index = 0; index < pa->size; index++) {
2348 u32 action = le32_to_cpu(pa->code[index]);
2349 u32 regno = (action & 0x0fff0000) >> 16;
2351 switch(action & 0xf0000000) {
2356 case PHY_CLEAR_READCOUNT:
2358 case PHY_WRITE_PREVIOUS:
2363 if (regno > index) {
2364 netif_err(tp, ifup, tp->dev,
2365 "Out of range of firmware\n");
2369 case PHY_READCOUNT_EQ_SKIP:
2370 if (index + 2 >= pa->size) {
2371 netif_err(tp, ifup, tp->dev,
2372 "Out of range of firmware\n");
2376 case PHY_COMP_EQ_SKIPN:
2377 case PHY_COMP_NEQ_SKIPN:
2379 if (index + 1 + regno >= pa->size) {
2380 netif_err(tp, ifup, tp->dev,
2381 "Out of range of firmware\n");
2387 netif_err(tp, ifup, tp->dev,
2388 "Invalid action 0x%08x\n", action);
2397 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2399 struct net_device *dev = tp->dev;
2402 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2403 netif_err(tp, ifup, dev, "invalid firmware\n");
2407 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2413 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2415 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2416 struct mdio_ops org, *ops = &tp->mdio_ops;
2420 predata = count = 0;
2421 org.write = ops->write;
2422 org.read = ops->read;
2424 for (index = 0; index < pa->size; ) {
2425 u32 action = le32_to_cpu(pa->code[index]);
2426 u32 data = action & 0x0000ffff;
2427 u32 regno = (action & 0x0fff0000) >> 16;
2432 switch(action & 0xf0000000) {
2434 predata = rtl_readphy(tp, regno);
2451 ops->write = org.write;
2452 ops->read = org.read;
2453 } else if (data == 1) {
2454 ops->write = mac_mcu_write;
2455 ops->read = mac_mcu_read;
2460 case PHY_CLEAR_READCOUNT:
2465 rtl_writephy(tp, regno, data);
2468 case PHY_READCOUNT_EQ_SKIP:
2469 index += (count == data) ? 2 : 1;
2471 case PHY_COMP_EQ_SKIPN:
2472 if (predata == data)
2476 case PHY_COMP_NEQ_SKIPN:
2477 if (predata != data)
2481 case PHY_WRITE_PREVIOUS:
2482 rtl_writephy(tp, regno, predata);
2498 ops->write = org.write;
2499 ops->read = org.read;
2502 static void rtl_release_firmware(struct rtl8169_private *tp)
2504 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2505 release_firmware(tp->rtl_fw->fw);
2508 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2511 static void rtl_apply_firmware(struct rtl8169_private *tp)
2513 struct rtl_fw *rtl_fw = tp->rtl_fw;
2515 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2516 if (!IS_ERR_OR_NULL(rtl_fw))
2517 rtl_phy_write_fw(tp, rtl_fw);
2520 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2522 if (rtl_readphy(tp, reg) != val)
2523 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2525 rtl_apply_firmware(tp);
2528 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2530 static const struct phy_reg phy_reg_init[] = {
2592 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2595 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2597 static const struct phy_reg phy_reg_init[] = {
2603 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2606 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2608 struct pci_dev *pdev = tp->pci_dev;
2610 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2611 (pdev->subsystem_device != 0xe000))
2614 rtl_writephy(tp, 0x1f, 0x0001);
2615 rtl_writephy(tp, 0x10, 0xf01b);
2616 rtl_writephy(tp, 0x1f, 0x0000);
2619 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2621 static const struct phy_reg phy_reg_init[] = {
2661 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2663 rtl8169scd_hw_phy_config_quirk(tp);
2666 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2668 static const struct phy_reg phy_reg_init[] = {
2716 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2719 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2721 static const struct phy_reg phy_reg_init[] = {
2726 rtl_writephy(tp, 0x1f, 0x0001);
2727 rtl_patchphy(tp, 0x16, 1 << 0);
2729 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2732 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2734 static const struct phy_reg phy_reg_init[] = {
2740 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2743 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2745 static const struct phy_reg phy_reg_init[] = {
2753 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2756 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2758 static const struct phy_reg phy_reg_init[] = {
2764 rtl_writephy(tp, 0x1f, 0x0000);
2765 rtl_patchphy(tp, 0x14, 1 << 5);
2766 rtl_patchphy(tp, 0x0d, 1 << 5);
2768 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2771 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2773 static const struct phy_reg phy_reg_init[] = {
2793 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2795 rtl_patchphy(tp, 0x14, 1 << 5);
2796 rtl_patchphy(tp, 0x0d, 1 << 5);
2797 rtl_writephy(tp, 0x1f, 0x0000);
2800 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2802 static const struct phy_reg phy_reg_init[] = {
2820 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2822 rtl_patchphy(tp, 0x16, 1 << 0);
2823 rtl_patchphy(tp, 0x14, 1 << 5);
2824 rtl_patchphy(tp, 0x0d, 1 << 5);
2825 rtl_writephy(tp, 0x1f, 0x0000);
2828 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2830 static const struct phy_reg phy_reg_init[] = {
2842 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2844 rtl_patchphy(tp, 0x16, 1 << 0);
2845 rtl_patchphy(tp, 0x14, 1 << 5);
2846 rtl_patchphy(tp, 0x0d, 1 << 5);
2847 rtl_writephy(tp, 0x1f, 0x0000);
2850 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2852 rtl8168c_3_hw_phy_config(tp);
2855 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2857 static const struct phy_reg phy_reg_init_0[] = {
2858 /* Channel Estimation */
2879 * Enhance line driver power
2888 * Can not link to 1Gbps with bad cable
2889 * Decrease SNR threshold form 21.07dB to 19.04dB
2898 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2902 * Fine Tune Switching regulator parameter
2904 rtl_writephy(tp, 0x1f, 0x0002);
2905 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2906 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
2908 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2909 static const struct phy_reg phy_reg_init[] = {
2919 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2921 val = rtl_readphy(tp, 0x0d);
2923 if ((val & 0x00ff) != 0x006c) {
2924 static const u32 set[] = {
2925 0x0065, 0x0066, 0x0067, 0x0068,
2926 0x0069, 0x006a, 0x006b, 0x006c
2930 rtl_writephy(tp, 0x1f, 0x0002);
2933 for (i = 0; i < ARRAY_SIZE(set); i++)
2934 rtl_writephy(tp, 0x0d, val | set[i]);
2937 static const struct phy_reg phy_reg_init[] = {
2945 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2948 /* RSET couple improve */
2949 rtl_writephy(tp, 0x1f, 0x0002);
2950 rtl_patchphy(tp, 0x0d, 0x0300);
2951 rtl_patchphy(tp, 0x0f, 0x0010);
2953 /* Fine tune PLL performance */
2954 rtl_writephy(tp, 0x1f, 0x0002);
2955 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2956 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
2958 rtl_writephy(tp, 0x1f, 0x0005);
2959 rtl_writephy(tp, 0x05, 0x001b);
2961 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2963 rtl_writephy(tp, 0x1f, 0x0000);
2966 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2968 static const struct phy_reg phy_reg_init_0[] = {
2969 /* Channel Estimation */
2990 * Enhance line driver power
2999 * Can not link to 1Gbps with bad cable
3000 * Decrease SNR threshold form 21.07dB to 19.04dB
3009 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3011 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3012 static const struct phy_reg phy_reg_init[] = {
3023 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3025 val = rtl_readphy(tp, 0x0d);
3026 if ((val & 0x00ff) != 0x006c) {
3027 static const u32 set[] = {
3028 0x0065, 0x0066, 0x0067, 0x0068,
3029 0x0069, 0x006a, 0x006b, 0x006c
3033 rtl_writephy(tp, 0x1f, 0x0002);
3036 for (i = 0; i < ARRAY_SIZE(set); i++)
3037 rtl_writephy(tp, 0x0d, val | set[i]);
3040 static const struct phy_reg phy_reg_init[] = {
3048 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3051 /* Fine tune PLL performance */
3052 rtl_writephy(tp, 0x1f, 0x0002);
3053 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3054 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3056 /* Switching regulator Slew rate */
3057 rtl_writephy(tp, 0x1f, 0x0002);
3058 rtl_patchphy(tp, 0x0f, 0x0017);
3060 rtl_writephy(tp, 0x1f, 0x0005);
3061 rtl_writephy(tp, 0x05, 0x001b);
3063 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
3065 rtl_writephy(tp, 0x1f, 0x0000);
3068 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
3070 static const struct phy_reg phy_reg_init[] = {
3126 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3129 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3131 static const struct phy_reg phy_reg_init[] = {
3141 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3142 rtl_patchphy(tp, 0x0d, 1 << 5);
3145 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3147 static const struct phy_reg phy_reg_init[] = {
3148 /* Enable Delay cap */
3154 /* Channel estimation fine tune */
3163 /* Update PFM & 10M TX idle timer */
3175 rtl_apply_firmware(tp);
3177 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3179 /* DCO enable for 10M IDLE Power */
3180 rtl_writephy(tp, 0x1f, 0x0007);
3181 rtl_writephy(tp, 0x1e, 0x0023);
3182 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3183 rtl_writephy(tp, 0x1f, 0x0000);
3185 /* For impedance matching */
3186 rtl_writephy(tp, 0x1f, 0x0002);
3187 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3188 rtl_writephy(tp, 0x1f, 0x0000);
3190 /* PHY auto speed down */
3191 rtl_writephy(tp, 0x1f, 0x0007);
3192 rtl_writephy(tp, 0x1e, 0x002d);
3193 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3194 rtl_writephy(tp, 0x1f, 0x0000);
3195 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3197 rtl_writephy(tp, 0x1f, 0x0005);
3198 rtl_writephy(tp, 0x05, 0x8b86);
3199 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3200 rtl_writephy(tp, 0x1f, 0x0000);
3202 rtl_writephy(tp, 0x1f, 0x0005);
3203 rtl_writephy(tp, 0x05, 0x8b85);
3204 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3205 rtl_writephy(tp, 0x1f, 0x0007);
3206 rtl_writephy(tp, 0x1e, 0x0020);
3207 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3208 rtl_writephy(tp, 0x1f, 0x0006);
3209 rtl_writephy(tp, 0x00, 0x5a00);
3210 rtl_writephy(tp, 0x1f, 0x0000);
3211 rtl_writephy(tp, 0x0d, 0x0007);
3212 rtl_writephy(tp, 0x0e, 0x003c);
3213 rtl_writephy(tp, 0x0d, 0x4007);
3214 rtl_writephy(tp, 0x0e, 0x0000);
3215 rtl_writephy(tp, 0x0d, 0x0000);
3218 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3221 addr[0] | (addr[1] << 8),
3222 addr[2] | (addr[3] << 8),
3223 addr[4] | (addr[5] << 8)
3225 const struct exgmac_reg e[] = {
3226 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3227 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3228 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3229 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3232 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3235 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3237 static const struct phy_reg phy_reg_init[] = {
3238 /* Enable Delay cap */
3247 /* Channel estimation fine tune */
3264 rtl_apply_firmware(tp);
3266 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3268 /* For 4-corner performance improve */
3269 rtl_writephy(tp, 0x1f, 0x0005);
3270 rtl_writephy(tp, 0x05, 0x8b80);
3271 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3272 rtl_writephy(tp, 0x1f, 0x0000);
3274 /* PHY auto speed down */
3275 rtl_writephy(tp, 0x1f, 0x0004);
3276 rtl_writephy(tp, 0x1f, 0x0007);
3277 rtl_writephy(tp, 0x1e, 0x002d);
3278 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3279 rtl_writephy(tp, 0x1f, 0x0002);
3280 rtl_writephy(tp, 0x1f, 0x0000);
3281 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3283 /* improve 10M EEE waveform */
3284 rtl_writephy(tp, 0x1f, 0x0005);
3285 rtl_writephy(tp, 0x05, 0x8b86);
3286 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3287 rtl_writephy(tp, 0x1f, 0x0000);
3289 /* Improve 2-pair detection performance */
3290 rtl_writephy(tp, 0x1f, 0x0005);
3291 rtl_writephy(tp, 0x05, 0x8b85);
3292 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3293 rtl_writephy(tp, 0x1f, 0x0000);
3296 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
3297 rtl_writephy(tp, 0x1f, 0x0005);
3298 rtl_writephy(tp, 0x05, 0x8b85);
3299 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
3300 rtl_writephy(tp, 0x1f, 0x0004);
3301 rtl_writephy(tp, 0x1f, 0x0007);
3302 rtl_writephy(tp, 0x1e, 0x0020);
3303 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
3304 rtl_writephy(tp, 0x1f, 0x0002);
3305 rtl_writephy(tp, 0x1f, 0x0000);
3306 rtl_writephy(tp, 0x0d, 0x0007);
3307 rtl_writephy(tp, 0x0e, 0x003c);
3308 rtl_writephy(tp, 0x0d, 0x4007);
3309 rtl_writephy(tp, 0x0e, 0x0006);
3310 rtl_writephy(tp, 0x0d, 0x0000);
3313 rtl_writephy(tp, 0x1f, 0x0003);
3314 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3315 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
3316 rtl_writephy(tp, 0x1f, 0x0000);
3317 rtl_writephy(tp, 0x1f, 0x0005);
3318 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3319 rtl_writephy(tp, 0x1f, 0x0000);
3321 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3322 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3325 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3327 /* For 4-corner performance improve */
3328 rtl_writephy(tp, 0x1f, 0x0005);
3329 rtl_writephy(tp, 0x05, 0x8b80);
3330 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3331 rtl_writephy(tp, 0x1f, 0x0000);
3333 /* PHY auto speed down */
3334 rtl_writephy(tp, 0x1f, 0x0007);
3335 rtl_writephy(tp, 0x1e, 0x002d);
3336 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3337 rtl_writephy(tp, 0x1f, 0x0000);
3338 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3340 /* Improve 10M EEE waveform */
3341 rtl_writephy(tp, 0x1f, 0x0005);
3342 rtl_writephy(tp, 0x05, 0x8b86);
3343 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3344 rtl_writephy(tp, 0x1f, 0x0000);
3347 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3349 static const struct phy_reg phy_reg_init[] = {
3350 /* Channel estimation fine tune */
3355 /* Modify green table for giga & fnet */
3372 /* Modify green table for 10M */
3378 /* Disable hiimpedance detection (RTCT) */
3384 rtl_apply_firmware(tp);
3386 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3388 rtl8168f_hw_phy_config(tp);
3390 /* Improve 2-pair detection performance */
3391 rtl_writephy(tp, 0x1f, 0x0005);
3392 rtl_writephy(tp, 0x05, 0x8b85);
3393 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3394 rtl_writephy(tp, 0x1f, 0x0000);
3397 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3399 rtl_apply_firmware(tp);
3401 rtl8168f_hw_phy_config(tp);
3404 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3406 static const struct phy_reg phy_reg_init[] = {
3407 /* Channel estimation fine tune */
3412 /* Modify green table for giga & fnet */
3429 /* Modify green table for 10M */
3435 /* Disable hiimpedance detection (RTCT) */
3442 rtl_apply_firmware(tp);
3444 rtl8168f_hw_phy_config(tp);
3446 /* Improve 2-pair detection performance */
3447 rtl_writephy(tp, 0x1f, 0x0005);
3448 rtl_writephy(tp, 0x05, 0x8b85);
3449 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3450 rtl_writephy(tp, 0x1f, 0x0000);
3452 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3454 /* Modify green table for giga */
3455 rtl_writephy(tp, 0x1f, 0x0005);
3456 rtl_writephy(tp, 0x05, 0x8b54);
3457 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3458 rtl_writephy(tp, 0x05, 0x8b5d);
3459 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3460 rtl_writephy(tp, 0x05, 0x8a7c);
3461 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3462 rtl_writephy(tp, 0x05, 0x8a7f);
3463 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3464 rtl_writephy(tp, 0x05, 0x8a82);
3465 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3466 rtl_writephy(tp, 0x05, 0x8a85);
3467 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3468 rtl_writephy(tp, 0x05, 0x8a88);
3469 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3470 rtl_writephy(tp, 0x1f, 0x0000);
3472 /* uc same-seed solution */
3473 rtl_writephy(tp, 0x1f, 0x0005);
3474 rtl_writephy(tp, 0x05, 0x8b85);
3475 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3476 rtl_writephy(tp, 0x1f, 0x0000);
3479 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3480 rtl_writephy(tp, 0x1f, 0x0005);
3481 rtl_writephy(tp, 0x05, 0x8b85);
3482 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3483 rtl_writephy(tp, 0x1f, 0x0004);
3484 rtl_writephy(tp, 0x1f, 0x0007);
3485 rtl_writephy(tp, 0x1e, 0x0020);
3486 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3487 rtl_writephy(tp, 0x1f, 0x0000);
3488 rtl_writephy(tp, 0x0d, 0x0007);
3489 rtl_writephy(tp, 0x0e, 0x003c);
3490 rtl_writephy(tp, 0x0d, 0x4007);
3491 rtl_writephy(tp, 0x0e, 0x0000);
3492 rtl_writephy(tp, 0x0d, 0x0000);
3495 rtl_writephy(tp, 0x1f, 0x0003);
3496 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3497 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3498 rtl_writephy(tp, 0x1f, 0x0000);
3501 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3503 rtl_apply_firmware(tp);
3505 rtl_writephy(tp, 0x1f, 0x0a46);
3506 if (rtl_readphy(tp, 0x10) & 0x0100) {
3507 rtl_writephy(tp, 0x1f, 0x0bcc);
3508 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3510 rtl_writephy(tp, 0x1f, 0x0bcc);
3511 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3514 rtl_writephy(tp, 0x1f, 0x0a46);
3515 if (rtl_readphy(tp, 0x13) & 0x0100) {
3516 rtl_writephy(tp, 0x1f, 0x0c41);
3517 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3519 rtl_writephy(tp, 0x1f, 0x0c41);
3520 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3523 /* Enable PHY auto speed down */
3524 rtl_writephy(tp, 0x1f, 0x0a44);
3525 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3527 rtl_writephy(tp, 0x1f, 0x0bcc);
3528 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
3529 rtl_writephy(tp, 0x1f, 0x0a44);
3530 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3531 rtl_writephy(tp, 0x1f, 0x0a43);
3532 rtl_writephy(tp, 0x13, 0x8084);
3533 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3534 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3536 /* EEE auto-fallback function */
3537 rtl_writephy(tp, 0x1f, 0x0a4b);
3538 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3540 /* Enable UC LPF tune function */
3541 rtl_writephy(tp, 0x1f, 0x0a43);
3542 rtl_writephy(tp, 0x13, 0x8012);
3543 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3545 rtl_writephy(tp, 0x1f, 0x0c42);
3546 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3548 /* Improve SWR Efficiency */
3549 rtl_writephy(tp, 0x1f, 0x0bcd);
3550 rtl_writephy(tp, 0x14, 0x5065);
3551 rtl_writephy(tp, 0x14, 0xd065);
3552 rtl_writephy(tp, 0x1f, 0x0bc8);
3553 rtl_writephy(tp, 0x11, 0x5655);
3554 rtl_writephy(tp, 0x1f, 0x0bcd);
3555 rtl_writephy(tp, 0x14, 0x1065);
3556 rtl_writephy(tp, 0x14, 0x9065);
3557 rtl_writephy(tp, 0x14, 0x1065);
3559 /* Check ALDPS bit, disable it if enabled */
3560 rtl_writephy(tp, 0x1f, 0x0a43);
3561 if (rtl_readphy(tp, 0x10) & 0x0004)
3562 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3564 rtl_writephy(tp, 0x1f, 0x0000);
3567 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3569 rtl_apply_firmware(tp);
3572 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3577 rtl_apply_firmware(tp);
3579 /* CHN EST parameters adjust - giga master */
3580 rtl_writephy(tp, 0x1f, 0x0a43);
3581 rtl_writephy(tp, 0x13, 0x809b);
3582 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3583 rtl_writephy(tp, 0x13, 0x80a2);
3584 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3585 rtl_writephy(tp, 0x13, 0x80a4);
3586 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3587 rtl_writephy(tp, 0x13, 0x809c);
3588 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3589 rtl_writephy(tp, 0x1f, 0x0000);
3591 /* CHN EST parameters adjust - giga slave */
3592 rtl_writephy(tp, 0x1f, 0x0a43);
3593 rtl_writephy(tp, 0x13, 0x80ad);
3594 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
3595 rtl_writephy(tp, 0x13, 0x80b4);
3596 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
3597 rtl_writephy(tp, 0x13, 0x80ac);
3598 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
3599 rtl_writephy(tp, 0x1f, 0x0000);
3601 /* CHN EST parameters adjust - fnet */
3602 rtl_writephy(tp, 0x1f, 0x0a43);
3603 rtl_writephy(tp, 0x13, 0x808e);
3604 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
3605 rtl_writephy(tp, 0x13, 0x8090);
3606 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
3607 rtl_writephy(tp, 0x13, 0x8092);
3608 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
3609 rtl_writephy(tp, 0x1f, 0x0000);
3611 /* enable R-tune & PGA-retune function */
3613 rtl_writephy(tp, 0x1f, 0x0a46);
3614 data = rtl_readphy(tp, 0x13);
3617 dout_tapbin |= data;
3618 data = rtl_readphy(tp, 0x12);
3621 dout_tapbin |= data;
3622 dout_tapbin = ~(dout_tapbin^0x08);
3624 dout_tapbin &= 0xf000;
3625 rtl_writephy(tp, 0x1f, 0x0a43);
3626 rtl_writephy(tp, 0x13, 0x827a);
3627 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3628 rtl_writephy(tp, 0x13, 0x827b);
3629 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3630 rtl_writephy(tp, 0x13, 0x827c);
3631 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3632 rtl_writephy(tp, 0x13, 0x827d);
3633 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3635 rtl_writephy(tp, 0x1f, 0x0a43);
3636 rtl_writephy(tp, 0x13, 0x0811);
3637 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3638 rtl_writephy(tp, 0x1f, 0x0a42);
3639 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3640 rtl_writephy(tp, 0x1f, 0x0000);
3642 /* enable GPHY 10M */
3643 rtl_writephy(tp, 0x1f, 0x0a44);
3644 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3645 rtl_writephy(tp, 0x1f, 0x0000);
3647 /* SAR ADC performance */
3648 rtl_writephy(tp, 0x1f, 0x0bca);
3649 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
3650 rtl_writephy(tp, 0x1f, 0x0000);
3652 rtl_writephy(tp, 0x1f, 0x0a43);
3653 rtl_writephy(tp, 0x13, 0x803f);
3654 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3655 rtl_writephy(tp, 0x13, 0x8047);
3656 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3657 rtl_writephy(tp, 0x13, 0x804f);
3658 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3659 rtl_writephy(tp, 0x13, 0x8057);
3660 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3661 rtl_writephy(tp, 0x13, 0x805f);
3662 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3663 rtl_writephy(tp, 0x13, 0x8067);
3664 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3665 rtl_writephy(tp, 0x13, 0x806f);
3666 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3667 rtl_writephy(tp, 0x1f, 0x0000);
3669 /* disable phy pfm mode */
3670 rtl_writephy(tp, 0x1f, 0x0a44);
3671 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3672 rtl_writephy(tp, 0x1f, 0x0000);
3674 /* Check ALDPS bit, disable it if enabled */
3675 rtl_writephy(tp, 0x1f, 0x0a43);
3676 if (rtl_readphy(tp, 0x10) & 0x0004)
3677 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3679 rtl_writephy(tp, 0x1f, 0x0000);
3682 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3684 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3688 rtl_apply_firmware(tp);
3690 /* CHIN EST parameter update */
3691 rtl_writephy(tp, 0x1f, 0x0a43);
3692 rtl_writephy(tp, 0x13, 0x808a);
3693 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
3694 rtl_writephy(tp, 0x1f, 0x0000);
3696 /* enable R-tune & PGA-retune function */
3697 rtl_writephy(tp, 0x1f, 0x0a43);
3698 rtl_writephy(tp, 0x13, 0x0811);
3699 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3700 rtl_writephy(tp, 0x1f, 0x0a42);
3701 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3702 rtl_writephy(tp, 0x1f, 0x0000);
3704 /* enable GPHY 10M */
3705 rtl_writephy(tp, 0x1f, 0x0a44);
3706 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3707 rtl_writephy(tp, 0x1f, 0x0000);
3709 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3710 data = r8168_mac_ocp_read(tp, 0xdd02);
3711 ioffset_p3 = ((data & 0x80)>>7);
3714 data = r8168_mac_ocp_read(tp, 0xdd00);
3715 ioffset_p3 |= ((data & (0xe000))>>13);
3716 ioffset_p2 = ((data & (0x1e00))>>9);
3717 ioffset_p1 = ((data & (0x01e0))>>5);
3718 ioffset_p0 = ((data & 0x0010)>>4);
3720 ioffset_p0 |= (data & (0x07));
3721 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3723 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3724 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
3725 rtl_writephy(tp, 0x1f, 0x0bcf);
3726 rtl_writephy(tp, 0x16, data);
3727 rtl_writephy(tp, 0x1f, 0x0000);
3730 /* Modify rlen (TX LPF corner frequency) level */
3731 rtl_writephy(tp, 0x1f, 0x0bcd);
3732 data = rtl_readphy(tp, 0x16);
3737 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3738 rtl_writephy(tp, 0x17, data);
3739 rtl_writephy(tp, 0x1f, 0x0bcd);
3740 rtl_writephy(tp, 0x1f, 0x0000);
3742 /* disable phy pfm mode */
3743 rtl_writephy(tp, 0x1f, 0x0a44);
3744 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3745 rtl_writephy(tp, 0x1f, 0x0000);
3747 /* Check ALDPS bit, disable it if enabled */
3748 rtl_writephy(tp, 0x1f, 0x0a43);
3749 if (rtl_readphy(tp, 0x10) & 0x0004)
3750 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3752 rtl_writephy(tp, 0x1f, 0x0000);
3755 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3757 /* Enable PHY auto speed down */
3758 rtl_writephy(tp, 0x1f, 0x0a44);
3759 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3760 rtl_writephy(tp, 0x1f, 0x0000);
3762 /* patch 10M & ALDPS */
3763 rtl_writephy(tp, 0x1f, 0x0bcc);
3764 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3765 rtl_writephy(tp, 0x1f, 0x0a44);
3766 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3767 rtl_writephy(tp, 0x1f, 0x0a43);
3768 rtl_writephy(tp, 0x13, 0x8084);
3769 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3770 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3771 rtl_writephy(tp, 0x1f, 0x0000);
3773 /* Enable EEE auto-fallback function */
3774 rtl_writephy(tp, 0x1f, 0x0a4b);
3775 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3776 rtl_writephy(tp, 0x1f, 0x0000);
3778 /* Enable UC LPF tune function */
3779 rtl_writephy(tp, 0x1f, 0x0a43);
3780 rtl_writephy(tp, 0x13, 0x8012);
3781 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3782 rtl_writephy(tp, 0x1f, 0x0000);
3784 /* set rg_sel_sdm_rate */
3785 rtl_writephy(tp, 0x1f, 0x0c42);
3786 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3787 rtl_writephy(tp, 0x1f, 0x0000);
3789 /* Check ALDPS bit, disable it if enabled */
3790 rtl_writephy(tp, 0x1f, 0x0a43);
3791 if (rtl_readphy(tp, 0x10) & 0x0004)
3792 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3794 rtl_writephy(tp, 0x1f, 0x0000);
3797 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3799 /* patch 10M & ALDPS */
3800 rtl_writephy(tp, 0x1f, 0x0bcc);
3801 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3802 rtl_writephy(tp, 0x1f, 0x0a44);
3803 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3804 rtl_writephy(tp, 0x1f, 0x0a43);
3805 rtl_writephy(tp, 0x13, 0x8084);
3806 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3807 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3808 rtl_writephy(tp, 0x1f, 0x0000);
3810 /* Enable UC LPF tune function */
3811 rtl_writephy(tp, 0x1f, 0x0a43);
3812 rtl_writephy(tp, 0x13, 0x8012);
3813 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3814 rtl_writephy(tp, 0x1f, 0x0000);
3816 /* Set rg_sel_sdm_rate */
3817 rtl_writephy(tp, 0x1f, 0x0c42);
3818 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3819 rtl_writephy(tp, 0x1f, 0x0000);
3821 /* Channel estimation parameters */
3822 rtl_writephy(tp, 0x1f, 0x0a43);
3823 rtl_writephy(tp, 0x13, 0x80f3);
3824 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3825 rtl_writephy(tp, 0x13, 0x80f0);
3826 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3827 rtl_writephy(tp, 0x13, 0x80ef);
3828 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3829 rtl_writephy(tp, 0x13, 0x80f6);
3830 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3831 rtl_writephy(tp, 0x13, 0x80ec);
3832 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3833 rtl_writephy(tp, 0x13, 0x80ed);
3834 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3835 rtl_writephy(tp, 0x13, 0x80f2);
3836 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3837 rtl_writephy(tp, 0x13, 0x80f4);
3838 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3839 rtl_writephy(tp, 0x1f, 0x0a43);
3840 rtl_writephy(tp, 0x13, 0x8110);
3841 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3842 rtl_writephy(tp, 0x13, 0x810f);
3843 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3844 rtl_writephy(tp, 0x13, 0x8111);
3845 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3846 rtl_writephy(tp, 0x13, 0x8113);
3847 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3848 rtl_writephy(tp, 0x13, 0x8115);
3849 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3850 rtl_writephy(tp, 0x13, 0x810e);
3851 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3852 rtl_writephy(tp, 0x13, 0x810c);
3853 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3854 rtl_writephy(tp, 0x13, 0x810b);
3855 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3856 rtl_writephy(tp, 0x1f, 0x0a43);
3857 rtl_writephy(tp, 0x13, 0x80d1);
3858 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3859 rtl_writephy(tp, 0x13, 0x80cd);
3860 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3861 rtl_writephy(tp, 0x13, 0x80d3);
3862 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3863 rtl_writephy(tp, 0x13, 0x80d5);
3864 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3865 rtl_writephy(tp, 0x13, 0x80d7);
3866 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3868 /* Force PWM-mode */
3869 rtl_writephy(tp, 0x1f, 0x0bcd);
3870 rtl_writephy(tp, 0x14, 0x5065);
3871 rtl_writephy(tp, 0x14, 0xd065);
3872 rtl_writephy(tp, 0x1f, 0x0bc8);
3873 rtl_writephy(tp, 0x12, 0x00ed);
3874 rtl_writephy(tp, 0x1f, 0x0bcd);
3875 rtl_writephy(tp, 0x14, 0x1065);
3876 rtl_writephy(tp, 0x14, 0x9065);
3877 rtl_writephy(tp, 0x14, 0x1065);
3878 rtl_writephy(tp, 0x1f, 0x0000);
3880 /* Check ALDPS bit, disable it if enabled */
3881 rtl_writephy(tp, 0x1f, 0x0a43);
3882 if (rtl_readphy(tp, 0x10) & 0x0004)
3883 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3885 rtl_writephy(tp, 0x1f, 0x0000);
3888 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3890 static const struct phy_reg phy_reg_init[] = {
3897 rtl_writephy(tp, 0x1f, 0x0000);
3898 rtl_patchphy(tp, 0x11, 1 << 12);
3899 rtl_patchphy(tp, 0x19, 1 << 13);
3900 rtl_patchphy(tp, 0x10, 1 << 15);
3902 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3905 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3907 static const struct phy_reg phy_reg_init[] = {
3921 /* Disable ALDPS before ram code */
3922 rtl_writephy(tp, 0x1f, 0x0000);
3923 rtl_writephy(tp, 0x18, 0x0310);
3926 rtl_apply_firmware(tp);
3928 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3931 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3933 /* Disable ALDPS before setting firmware */
3934 rtl_writephy(tp, 0x1f, 0x0000);
3935 rtl_writephy(tp, 0x18, 0x0310);
3938 rtl_apply_firmware(tp);
3941 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3942 rtl_writephy(tp, 0x1f, 0x0004);
3943 rtl_writephy(tp, 0x10, 0x401f);
3944 rtl_writephy(tp, 0x19, 0x7030);
3945 rtl_writephy(tp, 0x1f, 0x0000);
3948 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3950 static const struct phy_reg phy_reg_init[] = {
3957 /* Disable ALDPS before ram code */
3958 rtl_writephy(tp, 0x1f, 0x0000);
3959 rtl_writephy(tp, 0x18, 0x0310);
3962 rtl_apply_firmware(tp);
3964 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3965 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3967 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3970 static void rtl_hw_phy_config(struct net_device *dev)
3972 struct rtl8169_private *tp = netdev_priv(dev);
3974 rtl8169_print_mac_version(tp);
3976 switch (tp->mac_version) {
3977 case RTL_GIGA_MAC_VER_01:
3979 case RTL_GIGA_MAC_VER_02:
3980 case RTL_GIGA_MAC_VER_03:
3981 rtl8169s_hw_phy_config(tp);
3983 case RTL_GIGA_MAC_VER_04:
3984 rtl8169sb_hw_phy_config(tp);
3986 case RTL_GIGA_MAC_VER_05:
3987 rtl8169scd_hw_phy_config(tp);
3989 case RTL_GIGA_MAC_VER_06:
3990 rtl8169sce_hw_phy_config(tp);
3992 case RTL_GIGA_MAC_VER_07:
3993 case RTL_GIGA_MAC_VER_08:
3994 case RTL_GIGA_MAC_VER_09:
3995 rtl8102e_hw_phy_config(tp);
3997 case RTL_GIGA_MAC_VER_11:
3998 rtl8168bb_hw_phy_config(tp);
4000 case RTL_GIGA_MAC_VER_12:
4001 rtl8168bef_hw_phy_config(tp);
4003 case RTL_GIGA_MAC_VER_17:
4004 rtl8168bef_hw_phy_config(tp);
4006 case RTL_GIGA_MAC_VER_18:
4007 rtl8168cp_1_hw_phy_config(tp);
4009 case RTL_GIGA_MAC_VER_19:
4010 rtl8168c_1_hw_phy_config(tp);
4012 case RTL_GIGA_MAC_VER_20:
4013 rtl8168c_2_hw_phy_config(tp);
4015 case RTL_GIGA_MAC_VER_21:
4016 rtl8168c_3_hw_phy_config(tp);
4018 case RTL_GIGA_MAC_VER_22:
4019 rtl8168c_4_hw_phy_config(tp);
4021 case RTL_GIGA_MAC_VER_23:
4022 case RTL_GIGA_MAC_VER_24:
4023 rtl8168cp_2_hw_phy_config(tp);
4025 case RTL_GIGA_MAC_VER_25:
4026 rtl8168d_1_hw_phy_config(tp);
4028 case RTL_GIGA_MAC_VER_26:
4029 rtl8168d_2_hw_phy_config(tp);
4031 case RTL_GIGA_MAC_VER_27:
4032 rtl8168d_3_hw_phy_config(tp);
4034 case RTL_GIGA_MAC_VER_28:
4035 rtl8168d_4_hw_phy_config(tp);
4037 case RTL_GIGA_MAC_VER_29:
4038 case RTL_GIGA_MAC_VER_30:
4039 rtl8105e_hw_phy_config(tp);
4041 case RTL_GIGA_MAC_VER_31:
4044 case RTL_GIGA_MAC_VER_32:
4045 case RTL_GIGA_MAC_VER_33:
4046 rtl8168e_1_hw_phy_config(tp);
4048 case RTL_GIGA_MAC_VER_34:
4049 rtl8168e_2_hw_phy_config(tp);
4051 case RTL_GIGA_MAC_VER_35:
4052 rtl8168f_1_hw_phy_config(tp);
4054 case RTL_GIGA_MAC_VER_36:
4055 rtl8168f_2_hw_phy_config(tp);
4058 case RTL_GIGA_MAC_VER_37:
4059 rtl8402_hw_phy_config(tp);
4062 case RTL_GIGA_MAC_VER_38:
4063 rtl8411_hw_phy_config(tp);
4066 case RTL_GIGA_MAC_VER_39:
4067 rtl8106e_hw_phy_config(tp);
4070 case RTL_GIGA_MAC_VER_40:
4071 rtl8168g_1_hw_phy_config(tp);
4073 case RTL_GIGA_MAC_VER_42:
4074 case RTL_GIGA_MAC_VER_43:
4075 case RTL_GIGA_MAC_VER_44:
4076 rtl8168g_2_hw_phy_config(tp);
4078 case RTL_GIGA_MAC_VER_45:
4079 case RTL_GIGA_MAC_VER_47:
4080 rtl8168h_1_hw_phy_config(tp);
4082 case RTL_GIGA_MAC_VER_46:
4083 case RTL_GIGA_MAC_VER_48:
4084 rtl8168h_2_hw_phy_config(tp);
4087 case RTL_GIGA_MAC_VER_49:
4088 rtl8168ep_1_hw_phy_config(tp);
4090 case RTL_GIGA_MAC_VER_50:
4091 case RTL_GIGA_MAC_VER_51:
4092 rtl8168ep_2_hw_phy_config(tp);
4095 case RTL_GIGA_MAC_VER_41:
4101 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4103 if (!test_and_set_bit(flag, tp->wk.flags))
4104 schedule_work(&tp->wk.work);
4107 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4109 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4110 (RTL_R8(tp, PHYstatus) & TBI_Enable);
4113 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4115 rtl_hw_phy_config(dev);
4117 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4118 netif_dbg(tp, drv, dev,
4119 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4120 RTL_W8(tp, 0x82, 0x01);
4123 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4125 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4126 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4128 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
4129 netif_dbg(tp, drv, dev,
4130 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4131 RTL_W8(tp, 0x82, 0x01);
4132 netif_dbg(tp, drv, dev,
4133 "Set PHY Reg 0x0bh = 0x00h\n");
4134 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
4137 /* We may have called phy_speed_down before */
4138 phy_speed_up(dev->phydev);
4140 genphy_soft_reset(dev->phydev);
4143 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4147 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4149 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4152 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4155 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4156 rtl_rar_exgmac_set(tp, addr);
4158 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4160 rtl_unlock_work(tp);
4163 static int rtl_set_mac_address(struct net_device *dev, void *p)
4165 struct rtl8169_private *tp = netdev_priv(dev);
4166 struct device *d = tp_to_dev(tp);
4169 ret = eth_mac_addr(dev, p);
4173 pm_runtime_get_noresume(d);
4175 if (pm_runtime_active(d))
4176 rtl_rar_set(tp, dev->dev_addr);
4178 pm_runtime_put_noidle(d);
4183 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4185 if (!netif_running(dev))
4188 return phy_mii_ioctl(dev->phydev, ifr, cmd);
4191 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4193 struct mdio_ops *ops = &tp->mdio_ops;
4195 switch (tp->mac_version) {
4196 case RTL_GIGA_MAC_VER_27:
4197 ops->write = r8168dp_1_mdio_write;
4198 ops->read = r8168dp_1_mdio_read;
4200 case RTL_GIGA_MAC_VER_28:
4201 case RTL_GIGA_MAC_VER_31:
4202 ops->write = r8168dp_2_mdio_write;
4203 ops->read = r8168dp_2_mdio_read;
4205 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4206 ops->write = r8168g_mdio_write;
4207 ops->read = r8168g_mdio_read;
4210 ops->write = r8169_mdio_write;
4211 ops->read = r8169_mdio_read;
4216 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4218 switch (tp->mac_version) {
4219 case RTL_GIGA_MAC_VER_25:
4220 case RTL_GIGA_MAC_VER_26:
4221 case RTL_GIGA_MAC_VER_29:
4222 case RTL_GIGA_MAC_VER_30:
4223 case RTL_GIGA_MAC_VER_32:
4224 case RTL_GIGA_MAC_VER_33:
4225 case RTL_GIGA_MAC_VER_34:
4226 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
4227 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
4228 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4235 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4237 if (!netif_running(tp->dev) || !__rtl8169_get_wol(tp))
4240 phy_speed_down(tp->dev->phydev, false);
4241 rtl_wol_suspend_quirk(tp);
4246 static void r8168_pll_power_down(struct rtl8169_private *tp)
4248 if (r8168_check_dash(tp))
4251 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4252 tp->mac_version == RTL_GIGA_MAC_VER_33)
4253 rtl_ephy_write(tp, 0x19, 0xff64);
4255 if (rtl_wol_pll_power_down(tp))
4258 switch (tp->mac_version) {
4259 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
4260 case RTL_GIGA_MAC_VER_37:
4261 case RTL_GIGA_MAC_VER_39:
4262 case RTL_GIGA_MAC_VER_43:
4263 case RTL_GIGA_MAC_VER_44:
4264 case RTL_GIGA_MAC_VER_45:
4265 case RTL_GIGA_MAC_VER_46:
4266 case RTL_GIGA_MAC_VER_47:
4267 case RTL_GIGA_MAC_VER_48:
4268 case RTL_GIGA_MAC_VER_50:
4269 case RTL_GIGA_MAC_VER_51:
4270 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4272 case RTL_GIGA_MAC_VER_40:
4273 case RTL_GIGA_MAC_VER_41:
4274 case RTL_GIGA_MAC_VER_49:
4275 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4276 0xfc000000, ERIAR_EXGMAC);
4277 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4282 static void r8168_pll_power_up(struct rtl8169_private *tp)
4284 switch (tp->mac_version) {
4285 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
4286 case RTL_GIGA_MAC_VER_37:
4287 case RTL_GIGA_MAC_VER_39:
4288 case RTL_GIGA_MAC_VER_43:
4289 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4291 case RTL_GIGA_MAC_VER_44:
4292 case RTL_GIGA_MAC_VER_45:
4293 case RTL_GIGA_MAC_VER_46:
4294 case RTL_GIGA_MAC_VER_47:
4295 case RTL_GIGA_MAC_VER_48:
4296 case RTL_GIGA_MAC_VER_50:
4297 case RTL_GIGA_MAC_VER_51:
4298 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4300 case RTL_GIGA_MAC_VER_40:
4301 case RTL_GIGA_MAC_VER_41:
4302 case RTL_GIGA_MAC_VER_49:
4303 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4304 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4305 0x00000000, ERIAR_EXGMAC);
4309 phy_resume(tp->dev->phydev);
4310 /* give MAC/PHY some time to resume */
4314 static void rtl_pll_power_down(struct rtl8169_private *tp)
4316 switch (tp->mac_version) {
4317 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4318 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4321 r8168_pll_power_down(tp);
4325 static void rtl_pll_power_up(struct rtl8169_private *tp)
4327 switch (tp->mac_version) {
4328 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4329 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4332 r8168_pll_power_up(tp);
4336 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4338 switch (tp->mac_version) {
4339 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4340 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
4341 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4343 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
4344 case RTL_GIGA_MAC_VER_34:
4345 case RTL_GIGA_MAC_VER_35:
4346 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4348 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4349 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4352 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
4357 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4359 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4362 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4364 if (tp->jumbo_ops.enable) {
4365 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4366 tp->jumbo_ops.enable(tp);
4367 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4371 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4373 if (tp->jumbo_ops.disable) {
4374 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4375 tp->jumbo_ops.disable(tp);
4376 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4380 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4382 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4383 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
4384 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4387 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4389 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4390 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
4391 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4394 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4396 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4399 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4401 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4404 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4406 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4407 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4408 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
4409 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4412 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4414 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4415 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4416 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
4417 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4420 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4422 rtl_tx_performance_tweak(tp,
4423 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4426 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4428 rtl_tx_performance_tweak(tp,
4429 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4432 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4434 r8168b_0_hw_jumbo_enable(tp);
4436 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
4439 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4441 r8168b_0_hw_jumbo_disable(tp);
4443 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4446 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4448 struct jumbo_ops *ops = &tp->jumbo_ops;
4450 switch (tp->mac_version) {
4451 case RTL_GIGA_MAC_VER_11:
4452 ops->disable = r8168b_0_hw_jumbo_disable;
4453 ops->enable = r8168b_0_hw_jumbo_enable;
4455 case RTL_GIGA_MAC_VER_12:
4456 case RTL_GIGA_MAC_VER_17:
4457 ops->disable = r8168b_1_hw_jumbo_disable;
4458 ops->enable = r8168b_1_hw_jumbo_enable;
4460 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4461 case RTL_GIGA_MAC_VER_19:
4462 case RTL_GIGA_MAC_VER_20:
4463 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4464 case RTL_GIGA_MAC_VER_22:
4465 case RTL_GIGA_MAC_VER_23:
4466 case RTL_GIGA_MAC_VER_24:
4467 case RTL_GIGA_MAC_VER_25:
4468 case RTL_GIGA_MAC_VER_26:
4469 ops->disable = r8168c_hw_jumbo_disable;
4470 ops->enable = r8168c_hw_jumbo_enable;
4472 case RTL_GIGA_MAC_VER_27:
4473 case RTL_GIGA_MAC_VER_28:
4474 ops->disable = r8168dp_hw_jumbo_disable;
4475 ops->enable = r8168dp_hw_jumbo_enable;
4477 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4478 case RTL_GIGA_MAC_VER_32:
4479 case RTL_GIGA_MAC_VER_33:
4480 case RTL_GIGA_MAC_VER_34:
4481 ops->disable = r8168e_hw_jumbo_disable;
4482 ops->enable = r8168e_hw_jumbo_enable;
4486 * No action needed for jumbo frames with 8169.
4487 * No jumbo for 810x at all.
4489 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4491 ops->disable = NULL;
4497 DECLARE_RTL_COND(rtl_chipcmd_cond)
4499 return RTL_R8(tp, ChipCmd) & CmdReset;
4502 static void rtl_hw_reset(struct rtl8169_private *tp)
4504 RTL_W8(tp, ChipCmd, CmdReset);
4506 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4509 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4511 struct rtl_fw *rtl_fw;
4515 name = rtl_lookup_firmware_name(tp);
4517 goto out_no_firmware;
4519 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4523 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
4527 rc = rtl_check_firmware(tp, rtl_fw);
4529 goto err_release_firmware;
4531 tp->rtl_fw = rtl_fw;
4535 err_release_firmware:
4536 release_firmware(rtl_fw->fw);
4540 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4547 static void rtl_request_firmware(struct rtl8169_private *tp)
4549 if (IS_ERR(tp->rtl_fw))
4550 rtl_request_uncached_firmware(tp);
4553 static void rtl_rx_close(struct rtl8169_private *tp)
4555 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4558 DECLARE_RTL_COND(rtl_npq_cond)
4560 return RTL_R8(tp, TxPoll) & NPQ;
4563 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4565 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
4568 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4570 /* Disable interrupts */
4571 rtl8169_irq_mask_and_ack(tp);
4575 switch (tp->mac_version) {
4576 case RTL_GIGA_MAC_VER_27:
4577 case RTL_GIGA_MAC_VER_28:
4578 case RTL_GIGA_MAC_VER_31:
4579 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4581 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4582 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4583 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4584 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4587 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4595 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4597 /* Set DMA burst size and Interframe Gap Time */
4598 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
4599 (InterFrameGap << TxInterFrameGapShift));
4602 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
4604 /* Low hurts. Let's disable the filtering. */
4605 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
4608 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
4611 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4612 * register to be written before TxDescAddrLow to work.
4613 * Switching from MMIO to I/O access fixes the issue as well.
4615 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4616 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4617 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4618 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4621 static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
4623 static const struct rtl_cfg2_info {
4628 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4629 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4630 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4631 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4633 const struct rtl_cfg2_info *p = cfg2_info;
4637 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
4638 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4639 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4640 RTL_W32(tp, 0x7c, p->val);
4646 static void rtl_set_rx_mode(struct net_device *dev)
4648 struct rtl8169_private *tp = netdev_priv(dev);
4649 u32 mc_filter[2]; /* Multicast hash filter */
4653 if (dev->flags & IFF_PROMISC) {
4654 /* Unconditionally log net taps. */
4655 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4657 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4659 mc_filter[1] = mc_filter[0] = 0xffffffff;
4660 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4661 (dev->flags & IFF_ALLMULTI)) {
4662 /* Too many to filter perfectly -- accept all multicasts. */
4663 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4664 mc_filter[1] = mc_filter[0] = 0xffffffff;
4666 struct netdev_hw_addr *ha;
4668 rx_mode = AcceptBroadcast | AcceptMyPhys;
4669 mc_filter[1] = mc_filter[0] = 0;
4670 netdev_for_each_mc_addr(ha, dev) {
4671 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4672 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4673 rx_mode |= AcceptMulticast;
4677 if (dev->features & NETIF_F_RXALL)
4678 rx_mode |= (AcceptErr | AcceptRunt);
4680 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4682 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4683 u32 data = mc_filter[0];
4685 mc_filter[0] = swab32(mc_filter[1]);
4686 mc_filter[1] = swab32(data);
4689 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4690 mc_filter[1] = mc_filter[0] = 0xffffffff;
4692 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4693 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
4695 RTL_W32(tp, RxConfig, tmp);
4698 static void rtl_hw_start(struct rtl8169_private *tp)
4700 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4704 rtl_set_rx_max_size(tp);
4705 rtl_set_rx_tx_desc_registers(tp);
4706 rtl_set_rx_tx_config_registers(tp);
4707 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4709 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4710 RTL_R8(tp, IntrMask);
4711 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
4712 rtl_set_rx_mode(tp->dev);
4713 /* no early-rx interrupts */
4714 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4715 rtl_irq_enable_all(tp);
4718 static void rtl_hw_start_8169(struct rtl8169_private *tp)
4720 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4721 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4723 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
4725 tp->cp_cmd |= PCIMulRW;
4727 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4728 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4729 netif_dbg(tp, drv, tp->dev,
4730 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
4731 tp->cp_cmd |= (1 << 14);
4734 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4736 rtl8169_set_magic_reg(tp, tp->mac_version);
4739 * Undocumented corner. Supposedly:
4740 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4742 RTL_W16(tp, IntrMitigate, 0x0000);
4744 RTL_W32(tp, RxMissed, 0);
4747 DECLARE_RTL_COND(rtl_csiar_cond)
4749 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
4752 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4754 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4756 RTL_W32(tp, CSIDR, value);
4757 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4758 CSIAR_BYTE_ENABLE | func << 16);
4760 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4763 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4765 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4767 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4770 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4771 RTL_R32(tp, CSIDR) : ~0;
4774 static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
4776 struct pci_dev *pdev = tp->pci_dev;
4779 /* According to Realtek the value at config space address 0x070f
4780 * controls the L0s/L1 entrance latency. We try standard ECAM access
4781 * first and if it fails fall back to CSI.
4783 if (pdev->cfg_size > 0x070f &&
4784 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4787 netdev_notice_once(tp->dev,
4788 "No native access to PCI extended config space, falling back to CSI\n");
4789 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4790 rtl_csi_write(tp, 0x070c, csi | val << 24);
4793 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
4795 rtl_csi_access_enable(tp, 0x27);
4799 unsigned int offset;
4804 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4810 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4811 rtl_ephy_write(tp, e->offset, w);
4816 static void rtl_disable_clock_request(struct rtl8169_private *tp)
4818 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
4819 PCI_EXP_LNKCTL_CLKREQ_EN);
4822 static void rtl_enable_clock_request(struct rtl8169_private *tp)
4824 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
4825 PCI_EXP_LNKCTL_CLKREQ_EN);
4828 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4832 data = RTL_R8(tp, Config3);
4837 data &= ~Rdy_to_L23;
4839 RTL_W8(tp, Config3, data);
4842 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4845 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
4846 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
4848 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4849 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4853 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4855 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4857 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4858 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4860 if (tp->dev->mtu <= ETH_DATA_LEN) {
4861 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
4862 PCI_EXP_DEVCTL_NOSNOOP_EN);
4866 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4868 rtl_hw_start_8168bb(tp);
4870 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4872 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4875 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4877 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
4879 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4881 if (tp->dev->mtu <= ETH_DATA_LEN)
4882 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4884 rtl_disable_clock_request(tp);
4886 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4887 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4890 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4892 static const struct ephy_info e_info_8168cp[] = {
4893 { 0x01, 0, 0x0001 },
4894 { 0x02, 0x0800, 0x1000 },
4895 { 0x03, 0, 0x0042 },
4896 { 0x06, 0x0080, 0x0000 },
4900 rtl_set_def_aspm_entry_latency(tp);
4902 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4904 __rtl_hw_start_8168cp(tp);
4907 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4909 rtl_set_def_aspm_entry_latency(tp);
4911 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4913 if (tp->dev->mtu <= ETH_DATA_LEN)
4914 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4916 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4917 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4920 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4922 rtl_set_def_aspm_entry_latency(tp);
4924 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4927 RTL_W8(tp, DBG_REG, 0x20);
4929 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4931 if (tp->dev->mtu <= ETH_DATA_LEN)
4932 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4934 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4935 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4938 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4940 static const struct ephy_info e_info_8168c_1[] = {
4941 { 0x02, 0x0800, 0x1000 },
4942 { 0x03, 0, 0x0002 },
4943 { 0x06, 0x0080, 0x0000 }
4946 rtl_set_def_aspm_entry_latency(tp);
4948 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4950 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4952 __rtl_hw_start_8168cp(tp);
4955 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4957 static const struct ephy_info e_info_8168c_2[] = {
4958 { 0x01, 0, 0x0001 },
4959 { 0x03, 0x0400, 0x0220 }
4962 rtl_set_def_aspm_entry_latency(tp);
4964 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4966 __rtl_hw_start_8168cp(tp);
4969 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4971 rtl_hw_start_8168c_2(tp);
4974 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4976 rtl_set_def_aspm_entry_latency(tp);
4978 __rtl_hw_start_8168cp(tp);
4981 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4983 rtl_set_def_aspm_entry_latency(tp);
4985 rtl_disable_clock_request(tp);
4987 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4989 if (tp->dev->mtu <= ETH_DATA_LEN)
4990 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4992 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4993 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4996 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
4998 rtl_set_def_aspm_entry_latency(tp);
5000 if (tp->dev->mtu <= ETH_DATA_LEN)
5001 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5003 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5005 rtl_disable_clock_request(tp);
5008 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
5010 static const struct ephy_info e_info_8168d_4[] = {
5011 { 0x0b, 0x0000, 0x0048 },
5012 { 0x19, 0x0020, 0x0050 },
5013 { 0x0c, 0x0100, 0x0020 }
5016 rtl_set_def_aspm_entry_latency(tp);
5018 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5020 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5022 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
5024 rtl_enable_clock_request(tp);
5027 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
5029 static const struct ephy_info e_info_8168e_1[] = {
5030 { 0x00, 0x0200, 0x0100 },
5031 { 0x00, 0x0000, 0x0004 },
5032 { 0x06, 0x0002, 0x0001 },
5033 { 0x06, 0x0000, 0x0030 },
5034 { 0x07, 0x0000, 0x2000 },
5035 { 0x00, 0x0000, 0x0020 },
5036 { 0x03, 0x5800, 0x2000 },
5037 { 0x03, 0x0000, 0x0001 },
5038 { 0x01, 0x0800, 0x1000 },
5039 { 0x07, 0x0000, 0x4000 },
5040 { 0x1e, 0x0000, 0x2000 },
5041 { 0x19, 0xffff, 0xfe6c },
5042 { 0x0a, 0x0000, 0x0040 }
5045 rtl_set_def_aspm_entry_latency(tp);
5047 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5049 if (tp->dev->mtu <= ETH_DATA_LEN)
5050 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5052 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5054 rtl_disable_clock_request(tp);
5056 /* Reset tx FIFO pointer */
5057 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5058 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
5060 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5063 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5065 static const struct ephy_info e_info_8168e_2[] = {
5066 { 0x09, 0x0000, 0x0080 },
5067 { 0x19, 0x0000, 0x0224 }
5070 rtl_set_def_aspm_entry_latency(tp);
5072 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5074 if (tp->dev->mtu <= ETH_DATA_LEN)
5075 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5077 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5078 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5079 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5080 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5081 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5082 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5083 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5084 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5086 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5088 rtl_disable_clock_request(tp);
5090 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5091 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5093 /* Adjust EEE LED frequency */
5094 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5096 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5097 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5098 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5100 rtl_hw_aspm_clkreq_enable(tp, true);
5103 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5105 rtl_set_def_aspm_entry_latency(tp);
5107 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5109 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5110 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5111 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5112 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5113 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5114 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5115 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5116 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5117 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5118 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5120 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5122 rtl_disable_clock_request(tp);
5124 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5125 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5126 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5127 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5128 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5131 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5133 static const struct ephy_info e_info_8168f_1[] = {
5134 { 0x06, 0x00c0, 0x0020 },
5135 { 0x08, 0x0001, 0x0002 },
5136 { 0x09, 0x0000, 0x0080 },
5137 { 0x19, 0x0000, 0x0224 }
5140 rtl_hw_start_8168f(tp);
5142 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5144 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5146 /* Adjust EEE LED frequency */
5147 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5150 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5152 static const struct ephy_info e_info_8168f_1[] = {
5153 { 0x06, 0x00c0, 0x0020 },
5154 { 0x0f, 0xffff, 0x5200 },
5155 { 0x1e, 0x0000, 0x4000 },
5156 { 0x19, 0x0000, 0x0224 }
5159 rtl_hw_start_8168f(tp);
5160 rtl_pcie_state_l2l3_enable(tp, false);
5162 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5164 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5167 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
5169 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5171 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5172 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5173 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5174 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5176 rtl_set_def_aspm_entry_latency(tp);
5178 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5180 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5181 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5182 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
5184 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5185 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5187 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5188 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5190 /* Adjust EEE LED frequency */
5191 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5193 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5194 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5196 rtl_pcie_state_l2l3_enable(tp, false);
5199 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5201 static const struct ephy_info e_info_8168g_1[] = {
5202 { 0x00, 0x0000, 0x0008 },
5203 { 0x0c, 0x37d0, 0x0820 },
5204 { 0x1e, 0x0000, 0x0001 },
5205 { 0x19, 0x8000, 0x0000 }
5208 rtl_hw_start_8168g(tp);
5210 /* disable aspm and clock request before access ephy */
5211 rtl_hw_aspm_clkreq_enable(tp, false);
5212 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
5213 rtl_hw_aspm_clkreq_enable(tp, true);
5216 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5218 static const struct ephy_info e_info_8168g_2[] = {
5219 { 0x00, 0x0000, 0x0008 },
5220 { 0x0c, 0x3df0, 0x0200 },
5221 { 0x19, 0xffff, 0xfc00 },
5222 { 0x1e, 0xffff, 0x20eb }
5225 rtl_hw_start_8168g(tp);
5227 /* disable aspm and clock request before access ephy */
5228 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5229 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5230 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5233 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5235 static const struct ephy_info e_info_8411_2[] = {
5236 { 0x00, 0x0000, 0x0008 },
5237 { 0x0c, 0x3df0, 0x0200 },
5238 { 0x0f, 0xffff, 0x5200 },
5239 { 0x19, 0x0020, 0x0000 },
5240 { 0x1e, 0x0000, 0x2000 }
5243 rtl_hw_start_8168g(tp);
5245 /* disable aspm and clock request before access ephy */
5246 rtl_hw_aspm_clkreq_enable(tp, false);
5247 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5248 rtl_hw_aspm_clkreq_enable(tp, true);
5251 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5255 static const struct ephy_info e_info_8168h_1[] = {
5256 { 0x1e, 0x0800, 0x0001 },
5257 { 0x1d, 0x0000, 0x0800 },
5258 { 0x05, 0xffff, 0x2089 },
5259 { 0x06, 0xffff, 0x5881 },
5260 { 0x04, 0xffff, 0x154a },
5261 { 0x01, 0xffff, 0x068b }
5264 /* disable aspm and clock request before access ephy */
5265 rtl_hw_aspm_clkreq_enable(tp, false);
5266 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5268 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5270 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5271 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5272 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5273 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5275 rtl_set_def_aspm_entry_latency(tp);
5277 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5279 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5280 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5282 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
5284 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
5286 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5288 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5289 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5291 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5292 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5294 /* Adjust EEE LED frequency */
5295 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5297 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5298 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5300 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5302 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5304 rtl_pcie_state_l2l3_enable(tp, false);
5306 rtl_writephy(tp, 0x1f, 0x0c42);
5307 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
5308 rtl_writephy(tp, 0x1f, 0x0000);
5309 if (rg_saw_cnt > 0) {
5312 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5313 sw_cnt_1ms_ini &= 0x0fff;
5314 data = r8168_mac_ocp_read(tp, 0xd412);
5316 data |= sw_cnt_1ms_ini;
5317 r8168_mac_ocp_write(tp, 0xd412, data);
5320 data = r8168_mac_ocp_read(tp, 0xe056);
5323 r8168_mac_ocp_write(tp, 0xe056, data);
5325 data = r8168_mac_ocp_read(tp, 0xe052);
5328 r8168_mac_ocp_write(tp, 0xe052, data);
5330 data = r8168_mac_ocp_read(tp, 0xe0d6);
5333 r8168_mac_ocp_write(tp, 0xe0d6, data);
5335 data = r8168_mac_ocp_read(tp, 0xd420);
5338 r8168_mac_ocp_write(tp, 0xd420, data);
5340 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5341 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5342 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5343 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
5345 rtl_hw_aspm_clkreq_enable(tp, true);
5348 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5350 rtl8168ep_stop_cmac(tp);
5352 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5354 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5355 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5356 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5357 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5359 rtl_set_def_aspm_entry_latency(tp);
5361 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5363 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5364 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5366 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5368 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5370 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5371 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5373 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5374 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5376 /* Adjust EEE LED frequency */
5377 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5379 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5381 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5383 rtl_pcie_state_l2l3_enable(tp, false);
5386 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5388 static const struct ephy_info e_info_8168ep_1[] = {
5389 { 0x00, 0xffff, 0x10ab },
5390 { 0x06, 0xffff, 0xf030 },
5391 { 0x08, 0xffff, 0x2006 },
5392 { 0x0d, 0xffff, 0x1666 },
5393 { 0x0c, 0x3ff0, 0x0000 }
5396 /* disable aspm and clock request before access ephy */
5397 rtl_hw_aspm_clkreq_enable(tp, false);
5398 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5400 rtl_hw_start_8168ep(tp);
5402 rtl_hw_aspm_clkreq_enable(tp, true);
5405 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5407 static const struct ephy_info e_info_8168ep_2[] = {
5408 { 0x00, 0xffff, 0x10a3 },
5409 { 0x19, 0xffff, 0xfc00 },
5410 { 0x1e, 0xffff, 0x20ea }
5413 /* disable aspm and clock request before access ephy */
5414 rtl_hw_aspm_clkreq_enable(tp, false);
5415 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5417 rtl_hw_start_8168ep(tp);
5419 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5420 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5422 rtl_hw_aspm_clkreq_enable(tp, true);
5425 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5428 static const struct ephy_info e_info_8168ep_3[] = {
5429 { 0x00, 0xffff, 0x10a3 },
5430 { 0x19, 0xffff, 0x7c00 },
5431 { 0x1e, 0xffff, 0x20eb },
5432 { 0x0d, 0xffff, 0x1666 }
5435 /* disable aspm and clock request before access ephy */
5436 rtl_hw_aspm_clkreq_enable(tp, false);
5437 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5439 rtl_hw_start_8168ep(tp);
5441 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5442 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5444 data = r8168_mac_ocp_read(tp, 0xd3e2);
5447 r8168_mac_ocp_write(tp, 0xd3e2, data);
5449 data = r8168_mac_ocp_read(tp, 0xd3e4);
5451 r8168_mac_ocp_write(tp, 0xd3e4, data);
5453 data = r8168_mac_ocp_read(tp, 0xe860);
5455 r8168_mac_ocp_write(tp, 0xe860, data);
5457 rtl_hw_aspm_clkreq_enable(tp, true);
5460 static void rtl_hw_start_8168(struct rtl8169_private *tp)
5462 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5464 tp->cp_cmd &= ~INTT_MASK;
5465 tp->cp_cmd |= PktCntrDisable | INTT_1;
5466 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5468 RTL_W16(tp, IntrMitigate, 0x5151);
5470 /* Work around for RxFIFO overflow. */
5471 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5472 tp->event_slow |= RxFIFOOver | PCSTimeout;
5473 tp->event_slow &= ~RxOverflow;
5476 switch (tp->mac_version) {
5477 case RTL_GIGA_MAC_VER_11:
5478 rtl_hw_start_8168bb(tp);
5481 case RTL_GIGA_MAC_VER_12:
5482 case RTL_GIGA_MAC_VER_17:
5483 rtl_hw_start_8168bef(tp);
5486 case RTL_GIGA_MAC_VER_18:
5487 rtl_hw_start_8168cp_1(tp);
5490 case RTL_GIGA_MAC_VER_19:
5491 rtl_hw_start_8168c_1(tp);
5494 case RTL_GIGA_MAC_VER_20:
5495 rtl_hw_start_8168c_2(tp);
5498 case RTL_GIGA_MAC_VER_21:
5499 rtl_hw_start_8168c_3(tp);
5502 case RTL_GIGA_MAC_VER_22:
5503 rtl_hw_start_8168c_4(tp);
5506 case RTL_GIGA_MAC_VER_23:
5507 rtl_hw_start_8168cp_2(tp);
5510 case RTL_GIGA_MAC_VER_24:
5511 rtl_hw_start_8168cp_3(tp);
5514 case RTL_GIGA_MAC_VER_25:
5515 case RTL_GIGA_MAC_VER_26:
5516 case RTL_GIGA_MAC_VER_27:
5517 rtl_hw_start_8168d(tp);
5520 case RTL_GIGA_MAC_VER_28:
5521 rtl_hw_start_8168d_4(tp);
5524 case RTL_GIGA_MAC_VER_31:
5525 rtl_hw_start_8168dp(tp);
5528 case RTL_GIGA_MAC_VER_32:
5529 case RTL_GIGA_MAC_VER_33:
5530 rtl_hw_start_8168e_1(tp);
5532 case RTL_GIGA_MAC_VER_34:
5533 rtl_hw_start_8168e_2(tp);
5536 case RTL_GIGA_MAC_VER_35:
5537 case RTL_GIGA_MAC_VER_36:
5538 rtl_hw_start_8168f_1(tp);
5541 case RTL_GIGA_MAC_VER_38:
5542 rtl_hw_start_8411(tp);
5545 case RTL_GIGA_MAC_VER_40:
5546 case RTL_GIGA_MAC_VER_41:
5547 rtl_hw_start_8168g_1(tp);
5549 case RTL_GIGA_MAC_VER_42:
5550 rtl_hw_start_8168g_2(tp);
5553 case RTL_GIGA_MAC_VER_44:
5554 rtl_hw_start_8411_2(tp);
5557 case RTL_GIGA_MAC_VER_45:
5558 case RTL_GIGA_MAC_VER_46:
5559 rtl_hw_start_8168h_1(tp);
5562 case RTL_GIGA_MAC_VER_49:
5563 rtl_hw_start_8168ep_1(tp);
5566 case RTL_GIGA_MAC_VER_50:
5567 rtl_hw_start_8168ep_2(tp);
5570 case RTL_GIGA_MAC_VER_51:
5571 rtl_hw_start_8168ep_3(tp);
5575 netif_err(tp, drv, tp->dev,
5576 "unknown chipset (mac_version = %d)\n",
5582 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5584 static const struct ephy_info e_info_8102e_1[] = {
5585 { 0x01, 0, 0x6e65 },
5586 { 0x02, 0, 0x091f },
5587 { 0x03, 0, 0xc2f9 },
5588 { 0x06, 0, 0xafb5 },
5589 { 0x07, 0, 0x0e00 },
5590 { 0x19, 0, 0xec80 },
5591 { 0x01, 0, 0x2e65 },
5596 rtl_set_def_aspm_entry_latency(tp);
5598 RTL_W8(tp, DBG_REG, FIX_NAK_1);
5600 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5603 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5604 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5606 cfg1 = RTL_R8(tp, Config1);
5607 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5608 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
5610 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5613 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5615 rtl_set_def_aspm_entry_latency(tp);
5617 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5619 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5620 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5623 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5625 rtl_hw_start_8102e_2(tp);
5627 rtl_ephy_write(tp, 0x03, 0xc2f9);
5630 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5632 static const struct ephy_info e_info_8105e_1[] = {
5633 { 0x07, 0, 0x4000 },
5634 { 0x19, 0, 0x0200 },
5635 { 0x19, 0, 0x0020 },
5636 { 0x1e, 0, 0x2000 },
5637 { 0x03, 0, 0x0001 },
5638 { 0x19, 0, 0x0100 },
5639 { 0x19, 0, 0x0004 },
5643 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5644 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5646 /* Disable Early Tally Counter */
5647 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
5649 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5650 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5652 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5654 rtl_pcie_state_l2l3_enable(tp, false);
5657 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5659 rtl_hw_start_8105e_1(tp);
5660 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5663 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5665 static const struct ephy_info e_info_8402[] = {
5666 { 0x19, 0xffff, 0xff64 },
5670 rtl_set_def_aspm_entry_latency(tp);
5672 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5673 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5675 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5676 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5678 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
5680 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5682 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5683 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5684 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5685 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5686 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5687 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5688 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
5690 rtl_pcie_state_l2l3_enable(tp, false);
5693 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5695 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5696 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5698 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5699 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5700 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5702 rtl_pcie_state_l2l3_enable(tp, false);
5705 static void rtl_hw_start_8101(struct rtl8169_private *tp)
5707 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5708 tp->event_slow &= ~RxFIFOOver;
5710 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5711 tp->mac_version == RTL_GIGA_MAC_VER_16)
5712 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
5713 PCI_EXP_DEVCTL_NOSNOOP_EN);
5715 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5717 tp->cp_cmd &= CPCMD_QUIRK_MASK;
5718 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5720 switch (tp->mac_version) {
5721 case RTL_GIGA_MAC_VER_07:
5722 rtl_hw_start_8102e_1(tp);
5725 case RTL_GIGA_MAC_VER_08:
5726 rtl_hw_start_8102e_3(tp);
5729 case RTL_GIGA_MAC_VER_09:
5730 rtl_hw_start_8102e_2(tp);
5733 case RTL_GIGA_MAC_VER_29:
5734 rtl_hw_start_8105e_1(tp);
5736 case RTL_GIGA_MAC_VER_30:
5737 rtl_hw_start_8105e_2(tp);
5740 case RTL_GIGA_MAC_VER_37:
5741 rtl_hw_start_8402(tp);
5744 case RTL_GIGA_MAC_VER_39:
5745 rtl_hw_start_8106(tp);
5747 case RTL_GIGA_MAC_VER_43:
5748 rtl_hw_start_8168g_2(tp);
5750 case RTL_GIGA_MAC_VER_47:
5751 case RTL_GIGA_MAC_VER_48:
5752 rtl_hw_start_8168h_1(tp);
5756 RTL_W16(tp, IntrMitigate, 0x0000);
5759 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5761 struct rtl8169_private *tp = netdev_priv(dev);
5763 if (new_mtu > ETH_DATA_LEN)
5764 rtl_hw_jumbo_enable(tp);
5766 rtl_hw_jumbo_disable(tp);
5769 netdev_update_features(dev);
5774 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5776 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5777 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5780 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5781 void **data_buff, struct RxDesc *desc)
5783 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5784 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5788 rtl8169_make_unusable_by_asic(desc);
5791 static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
5793 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5795 /* Force memory writes to complete before releasing descriptor */
5798 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
5801 static inline void *rtl8169_align(void *data)
5803 return (void *)ALIGN((long)data, 16);
5806 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5807 struct RxDesc *desc)
5811 struct device *d = tp_to_dev(tp);
5812 int node = dev_to_node(d);
5814 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
5818 if (rtl8169_align(data) != data) {
5820 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
5825 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
5827 if (unlikely(dma_mapping_error(d, mapping))) {
5828 if (net_ratelimit())
5829 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5833 desc->addr = cpu_to_le64(mapping);
5834 rtl8169_mark_to_asic(desc);
5842 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5846 for (i = 0; i < NUM_RX_DESC; i++) {
5847 if (tp->Rx_databuff[i]) {
5848 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5849 tp->RxDescArray + i);
5854 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5856 desc->opts1 |= cpu_to_le32(RingEnd);
5859 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5863 for (i = 0; i < NUM_RX_DESC; i++) {
5866 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5868 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5871 tp->Rx_databuff[i] = data;
5874 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5878 rtl8169_rx_clear(tp);
5882 static int rtl8169_init_ring(struct rtl8169_private *tp)
5884 rtl8169_init_ring_indexes(tp);
5886 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5887 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
5889 return rtl8169_rx_fill(tp);
5892 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5893 struct TxDesc *desc)
5895 unsigned int len = tx_skb->len;
5897 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5905 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5910 for (i = 0; i < n; i++) {
5911 unsigned int entry = (start + i) % NUM_TX_DESC;
5912 struct ring_info *tx_skb = tp->tx_skb + entry;
5913 unsigned int len = tx_skb->len;
5916 struct sk_buff *skb = tx_skb->skb;
5918 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
5919 tp->TxDescArray + entry);
5921 dev_consume_skb_any(skb);
5928 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5930 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5931 tp->cur_tx = tp->dirty_tx = 0;
5934 static void rtl_reset_work(struct rtl8169_private *tp)
5936 struct net_device *dev = tp->dev;
5939 napi_disable(&tp->napi);
5940 netif_stop_queue(dev);
5941 synchronize_sched();
5943 rtl8169_hw_reset(tp);
5945 for (i = 0; i < NUM_RX_DESC; i++)
5946 rtl8169_mark_to_asic(tp->RxDescArray + i);
5948 rtl8169_tx_clear(tp);
5949 rtl8169_init_ring_indexes(tp);
5951 napi_enable(&tp->napi);
5953 netif_wake_queue(dev);
5956 static void rtl8169_tx_timeout(struct net_device *dev)
5958 struct rtl8169_private *tp = netdev_priv(dev);
5960 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5963 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5966 struct skb_shared_info *info = skb_shinfo(skb);
5967 unsigned int cur_frag, entry;
5968 struct TxDesc *uninitialized_var(txd);
5969 struct device *d = tp_to_dev(tp);
5972 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5973 const skb_frag_t *frag = info->frags + cur_frag;
5978 entry = (entry + 1) % NUM_TX_DESC;
5980 txd = tp->TxDescArray + entry;
5981 len = skb_frag_size(frag);
5982 addr = skb_frag_address(frag);
5983 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5984 if (unlikely(dma_mapping_error(d, mapping))) {
5985 if (net_ratelimit())
5986 netif_err(tp, drv, tp->dev,
5987 "Failed to map TX fragments DMA!\n");
5991 /* Anti gcc 2.95.3 bugware (sic) */
5992 status = opts[0] | len |
5993 (RingEnd * !((entry + 1) % NUM_TX_DESC));
5995 txd->opts1 = cpu_to_le32(status);
5996 txd->opts2 = cpu_to_le32(opts[1]);
5997 txd->addr = cpu_to_le64(mapping);
5999 tp->tx_skb[entry].len = len;
6003 tp->tx_skb[entry].skb = skb;
6004 txd->opts1 |= cpu_to_le32(LastFrag);
6010 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6014 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6016 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6019 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6020 struct net_device *dev);
6021 /* r8169_csum_workaround()
6022 * The hw limites the value the transport offset. When the offset is out of the
6023 * range, calculate the checksum by sw.
6025 static void r8169_csum_workaround(struct rtl8169_private *tp,
6026 struct sk_buff *skb)
6028 if (skb_shinfo(skb)->gso_size) {
6029 netdev_features_t features = tp->dev->features;
6030 struct sk_buff *segs, *nskb;
6032 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6033 segs = skb_gso_segment(skb, features);
6034 if (IS_ERR(segs) || !segs)
6041 rtl8169_start_xmit(nskb, tp->dev);
6044 dev_consume_skb_any(skb);
6045 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6046 if (skb_checksum_help(skb) < 0)
6049 rtl8169_start_xmit(skb, tp->dev);
6051 struct net_device_stats *stats;
6054 stats = &tp->dev->stats;
6055 stats->tx_dropped++;
6056 dev_kfree_skb_any(skb);
6060 /* msdn_giant_send_check()
6061 * According to the document of microsoft, the TCP Pseudo Header excludes the
6062 * packet length for IPv6 TCP large packets.
6064 static int msdn_giant_send_check(struct sk_buff *skb)
6066 const struct ipv6hdr *ipv6h;
6070 ret = skb_cow_head(skb, 0);
6074 ipv6h = ipv6_hdr(skb);
6078 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6083 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6084 struct sk_buff *skb, u32 *opts)
6086 u32 mss = skb_shinfo(skb)->gso_size;
6090 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6091 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6092 const struct iphdr *ip = ip_hdr(skb);
6094 if (ip->protocol == IPPROTO_TCP)
6095 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6096 else if (ip->protocol == IPPROTO_UDP)
6097 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6105 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6106 struct sk_buff *skb, u32 *opts)
6108 u32 transport_offset = (u32)skb_transport_offset(skb);
6109 u32 mss = skb_shinfo(skb)->gso_size;
6112 if (transport_offset > GTTCPHO_MAX) {
6113 netif_warn(tp, tx_err, tp->dev,
6114 "Invalid transport offset 0x%x for TSO\n",
6119 switch (vlan_get_protocol(skb)) {
6120 case htons(ETH_P_IP):
6121 opts[0] |= TD1_GTSENV4;
6124 case htons(ETH_P_IPV6):
6125 if (msdn_giant_send_check(skb))
6128 opts[0] |= TD1_GTSENV6;
6136 opts[0] |= transport_offset << GTTCPHO_SHIFT;
6137 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
6138 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6141 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6142 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
6144 if (transport_offset > TCPHO_MAX) {
6145 netif_warn(tp, tx_err, tp->dev,
6146 "Invalid transport offset 0x%x\n",
6151 switch (vlan_get_protocol(skb)) {
6152 case htons(ETH_P_IP):
6153 opts[1] |= TD1_IPv4_CS;
6154 ip_protocol = ip_hdr(skb)->protocol;
6157 case htons(ETH_P_IPV6):
6158 opts[1] |= TD1_IPv6_CS;
6159 ip_protocol = ipv6_hdr(skb)->nexthdr;
6163 ip_protocol = IPPROTO_RAW;
6167 if (ip_protocol == IPPROTO_TCP)
6168 opts[1] |= TD1_TCP_CS;
6169 else if (ip_protocol == IPPROTO_UDP)
6170 opts[1] |= TD1_UDP_CS;
6174 opts[1] |= transport_offset << TCPHO_SHIFT;
6176 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6177 return !eth_skb_pad(skb);
6183 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6184 struct net_device *dev)
6186 struct rtl8169_private *tp = netdev_priv(dev);
6187 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
6188 struct TxDesc *txd = tp->TxDescArray + entry;
6189 struct device *d = tp_to_dev(tp);
6195 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
6196 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
6200 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
6203 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6206 if (!tp->tso_csum(tp, skb, opts)) {
6207 r8169_csum_workaround(tp, skb);
6208 return NETDEV_TX_OK;
6211 len = skb_headlen(skb);
6212 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
6213 if (unlikely(dma_mapping_error(d, mapping))) {
6214 if (net_ratelimit())
6215 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
6219 tp->tx_skb[entry].len = len;
6220 txd->addr = cpu_to_le64(mapping);
6222 frags = rtl8169_xmit_frags(tp, skb, opts);
6226 opts[0] |= FirstFrag;
6228 opts[0] |= FirstFrag | LastFrag;
6229 tp->tx_skb[entry].skb = skb;
6232 txd->opts2 = cpu_to_le32(opts[1]);
6234 skb_tx_timestamp(skb);
6236 /* Force memory writes to complete before releasing descriptor */
6239 /* Anti gcc 2.95.3 bugware (sic) */
6240 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
6241 txd->opts1 = cpu_to_le32(status);
6243 /* Force all memory writes to complete before notifying device */
6246 tp->cur_tx += frags + 1;
6248 RTL_W8(tp, TxPoll, NPQ);
6252 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6253 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6254 * not miss a ring update when it notices a stopped queue.
6257 netif_stop_queue(dev);
6258 /* Sync with rtl_tx:
6259 * - publish queue status and cur_tx ring index (write barrier)
6260 * - refresh dirty_tx ring index (read barrier).
6261 * May the current thread have a pessimistic view of the ring
6262 * status and forget to wake up queue, a racing rtl_tx thread
6266 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
6267 netif_wake_queue(dev);
6270 return NETDEV_TX_OK;
6273 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
6275 dev_kfree_skb_any(skb);
6276 dev->stats.tx_dropped++;
6277 return NETDEV_TX_OK;
6280 netif_stop_queue(dev);
6281 dev->stats.tx_dropped++;
6282 return NETDEV_TX_BUSY;
6285 static void rtl8169_pcierr_interrupt(struct net_device *dev)
6287 struct rtl8169_private *tp = netdev_priv(dev);
6288 struct pci_dev *pdev = tp->pci_dev;
6289 u16 pci_status, pci_cmd;
6291 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6292 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6294 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6295 pci_cmd, pci_status);
6298 * The recovery sequence below admits a very elaborated explanation:
6299 * - it seems to work;
6300 * - I did not see what else could be done;
6301 * - it makes iop3xx happy.
6303 * Feel free to adjust to your needs.
6305 if (pdev->broken_parity_status)
6306 pci_cmd &= ~PCI_COMMAND_PARITY;
6308 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6310 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
6312 pci_write_config_word(pdev, PCI_STATUS,
6313 pci_status & (PCI_STATUS_DETECTED_PARITY |
6314 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6315 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6317 /* The infamous DAC f*ckup only happens at boot time */
6318 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
6319 netif_info(tp, intr, dev, "disabling PCI DAC\n");
6320 tp->cp_cmd &= ~PCIDAC;
6321 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
6322 dev->features &= ~NETIF_F_HIGHDMA;
6325 rtl8169_hw_reset(tp);
6327 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6330 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
6332 unsigned int dirty_tx, tx_left;
6334 dirty_tx = tp->dirty_tx;
6336 tx_left = tp->cur_tx - dirty_tx;
6338 while (tx_left > 0) {
6339 unsigned int entry = dirty_tx % NUM_TX_DESC;
6340 struct ring_info *tx_skb = tp->tx_skb + entry;
6343 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6344 if (status & DescOwn)
6347 /* This barrier is needed to keep us from reading
6348 * any other fields out of the Tx descriptor until
6349 * we know the status of DescOwn
6353 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
6354 tp->TxDescArray + entry);
6355 if (status & LastFrag) {
6356 u64_stats_update_begin(&tp->tx_stats.syncp);
6357 tp->tx_stats.packets++;
6358 tp->tx_stats.bytes += tx_skb->skb->len;
6359 u64_stats_update_end(&tp->tx_stats.syncp);
6360 dev_consume_skb_any(tx_skb->skb);
6367 if (tp->dirty_tx != dirty_tx) {
6368 tp->dirty_tx = dirty_tx;
6369 /* Sync with rtl8169_start_xmit:
6370 * - publish dirty_tx ring index (write barrier)
6371 * - refresh cur_tx ring index and queue status (read barrier)
6372 * May the current thread miss the stopped queue condition,
6373 * a racing xmit thread can only have a right view of the
6377 if (netif_queue_stopped(dev) &&
6378 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6379 netif_wake_queue(dev);
6382 * 8168 hack: TxPoll requests are lost when the Tx packets are
6383 * too close. Let's kick an extra TxPoll request when a burst
6384 * of start_xmit activity is detected (if it is not detected,
6385 * it is slow enough). -- FR
6387 if (tp->cur_tx != dirty_tx)
6388 RTL_W8(tp, TxPoll, NPQ);
6392 static inline int rtl8169_fragmented_frame(u32 status)
6394 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6397 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
6399 u32 status = opts1 & RxProtoMask;
6401 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
6402 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
6403 skb->ip_summed = CHECKSUM_UNNECESSARY;
6405 skb_checksum_none_assert(skb);
6408 static struct sk_buff *rtl8169_try_rx_copy(void *data,
6409 struct rtl8169_private *tp,
6413 struct sk_buff *skb;
6414 struct device *d = tp_to_dev(tp);
6416 data = rtl8169_align(data);
6417 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6419 skb = napi_alloc_skb(&tp->napi, pkt_size);
6421 skb_copy_to_linear_data(skb, data, pkt_size);
6422 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6427 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
6429 unsigned int cur_rx, rx_left;
6432 cur_rx = tp->cur_rx;
6434 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
6435 unsigned int entry = cur_rx % NUM_RX_DESC;
6436 struct RxDesc *desc = tp->RxDescArray + entry;
6439 status = le32_to_cpu(desc->opts1);
6440 if (status & DescOwn)
6443 /* This barrier is needed to keep us from reading
6444 * any other fields out of the Rx descriptor until
6445 * we know the status of DescOwn
6449 if (unlikely(status & RxRES)) {
6450 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6452 dev->stats.rx_errors++;
6453 if (status & (RxRWT | RxRUNT))
6454 dev->stats.rx_length_errors++;
6456 dev->stats.rx_crc_errors++;
6457 /* RxFOVF is a reserved bit on later chip versions */
6458 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6460 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6461 dev->stats.rx_fifo_errors++;
6462 } else if (status & (RxRUNT | RxCRC) &&
6463 !(status & RxRWT) &&
6464 dev->features & NETIF_F_RXALL) {
6468 struct sk_buff *skb;
6473 addr = le64_to_cpu(desc->addr);
6474 if (likely(!(dev->features & NETIF_F_RXFCS)))
6475 pkt_size = (status & 0x00003fff) - 4;
6477 pkt_size = status & 0x00003fff;
6480 * The driver does not support incoming fragmented
6481 * frames. They are seen as a symptom of over-mtu
6484 if (unlikely(rtl8169_fragmented_frame(status))) {
6485 dev->stats.rx_dropped++;
6486 dev->stats.rx_length_errors++;
6487 goto release_descriptor;
6490 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6491 tp, pkt_size, addr);
6493 dev->stats.rx_dropped++;
6494 goto release_descriptor;
6497 rtl8169_rx_csum(skb, status);
6498 skb_put(skb, pkt_size);
6499 skb->protocol = eth_type_trans(skb, dev);
6501 rtl8169_rx_vlan_tag(desc, skb);
6503 if (skb->pkt_type == PACKET_MULTICAST)
6504 dev->stats.multicast++;
6506 napi_gro_receive(&tp->napi, skb);
6508 u64_stats_update_begin(&tp->rx_stats.syncp);
6509 tp->rx_stats.packets++;
6510 tp->rx_stats.bytes += pkt_size;
6511 u64_stats_update_end(&tp->rx_stats.syncp);
6515 rtl8169_mark_to_asic(desc);
6518 count = cur_rx - tp->cur_rx;
6519 tp->cur_rx = cur_rx;
6524 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6526 struct rtl8169_private *tp = dev_instance;
6530 status = rtl_get_events(tp);
6531 if (status && status != 0xffff) {
6532 status &= RTL_EVENT_NAPI | tp->event_slow;
6536 rtl_irq_disable(tp);
6537 napi_schedule_irqoff(&tp->napi);
6540 return IRQ_RETVAL(handled);
6544 * Workqueue context.
6546 static void rtl_slow_event_work(struct rtl8169_private *tp)
6548 struct net_device *dev = tp->dev;
6551 status = rtl_get_events(tp) & tp->event_slow;
6552 rtl_ack_events(tp, status);
6554 if (unlikely(status & RxFIFOOver)) {
6555 switch (tp->mac_version) {
6556 /* Work around for rx fifo overflow */
6557 case RTL_GIGA_MAC_VER_11:
6558 netif_stop_queue(dev);
6559 /* XXX - Hack alert. See rtl_task(). */
6560 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6566 if (unlikely(status & SYSErr))
6567 rtl8169_pcierr_interrupt(dev);
6569 if (status & LinkChg)
6570 phy_mac_interrupt(dev->phydev);
6572 rtl_irq_enable_all(tp);
6575 static void rtl_task(struct work_struct *work)
6577 static const struct {
6579 void (*action)(struct rtl8169_private *);
6581 /* XXX - keep rtl_slow_event_work() as first element. */
6582 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6583 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6585 struct rtl8169_private *tp =
6586 container_of(work, struct rtl8169_private, wk.work);
6587 struct net_device *dev = tp->dev;
6592 if (!netif_running(dev) ||
6593 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6596 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6599 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6601 rtl_work[i].action(tp);
6605 rtl_unlock_work(tp);
6608 static int rtl8169_poll(struct napi_struct *napi, int budget)
6610 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6611 struct net_device *dev = tp->dev;
6612 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6616 status = rtl_get_events(tp);
6617 rtl_ack_events(tp, status & ~tp->event_slow);
6619 if (status & RTL_EVENT_NAPI_RX)
6620 work_done = rtl_rx(dev, tp, (u32) budget);
6622 if (status & RTL_EVENT_NAPI_TX)
6625 if (status & tp->event_slow) {
6626 enable_mask &= ~tp->event_slow;
6628 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6631 if (work_done < budget) {
6632 napi_complete_done(napi, work_done);
6634 rtl_irq_enable(tp, enable_mask);
6641 static void rtl8169_rx_missed(struct net_device *dev)
6643 struct rtl8169_private *tp = netdev_priv(dev);
6645 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6648 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6649 RTL_W32(tp, RxMissed, 0);
6652 static void r8169_phylink_handler(struct net_device *ndev)
6654 struct rtl8169_private *tp = netdev_priv(ndev);
6656 if (netif_carrier_ok(ndev)) {
6657 rtl_link_chg_patch(tp);
6658 pm_request_resume(&tp->pci_dev->dev);
6660 pm_runtime_idle(&tp->pci_dev->dev);
6663 if (net_ratelimit())
6664 phy_print_status(ndev->phydev);
6667 static int r8169_phy_connect(struct rtl8169_private *tp)
6669 struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6670 phy_interface_t phy_mode;
6673 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
6674 PHY_INTERFACE_MODE_MII;
6676 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6681 if (!tp->supports_gmii)
6682 phy_set_max_speed(phydev, SPEED_100);
6684 /* Ensure to advertise everything, incl. pause */
6685 phydev->advertising = phydev->supported;
6687 phy_attached_info(phydev);
6692 static void rtl8169_down(struct net_device *dev)
6694 struct rtl8169_private *tp = netdev_priv(dev);
6696 phy_stop(dev->phydev);
6698 napi_disable(&tp->napi);
6699 netif_stop_queue(dev);
6701 rtl8169_hw_reset(tp);
6703 * At this point device interrupts can not be enabled in any function,
6704 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6705 * and napi is disabled (rtl8169_poll).
6707 rtl8169_rx_missed(dev);
6709 /* Give a racing hard_start_xmit a few cycles to complete. */
6710 synchronize_sched();
6712 rtl8169_tx_clear(tp);
6714 rtl8169_rx_clear(tp);
6716 rtl_pll_power_down(tp);
6719 static int rtl8169_close(struct net_device *dev)
6721 struct rtl8169_private *tp = netdev_priv(dev);
6722 struct pci_dev *pdev = tp->pci_dev;
6724 pm_runtime_get_sync(&pdev->dev);
6726 /* Update counters before going down */
6727 rtl8169_update_counters(tp);
6730 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6733 rtl_unlock_work(tp);
6735 cancel_work_sync(&tp->wk.work);
6737 phy_disconnect(dev->phydev);
6739 pci_free_irq(pdev, 0, tp);
6741 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6743 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6745 tp->TxDescArray = NULL;
6746 tp->RxDescArray = NULL;
6748 pm_runtime_put_sync(&pdev->dev);
6753 #ifdef CONFIG_NET_POLL_CONTROLLER
6754 static void rtl8169_netpoll(struct net_device *dev)
6756 struct rtl8169_private *tp = netdev_priv(dev);
6758 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
6762 static int rtl_open(struct net_device *dev)
6764 struct rtl8169_private *tp = netdev_priv(dev);
6765 struct pci_dev *pdev = tp->pci_dev;
6766 int retval = -ENOMEM;
6768 pm_runtime_get_sync(&pdev->dev);
6771 * Rx and Tx descriptors needs 256 bytes alignment.
6772 * dma_alloc_coherent provides more.
6774 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6775 &tp->TxPhyAddr, GFP_KERNEL);
6776 if (!tp->TxDescArray)
6777 goto err_pm_runtime_put;
6779 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6780 &tp->RxPhyAddr, GFP_KERNEL);
6781 if (!tp->RxDescArray)
6784 retval = rtl8169_init_ring(tp);
6788 INIT_WORK(&tp->wk.work, rtl_task);
6792 rtl_request_firmware(tp);
6794 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
6797 goto err_release_fw_2;
6799 retval = r8169_phy_connect(tp);
6805 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6807 napi_enable(&tp->napi);
6809 rtl8169_init_phy(dev, tp);
6811 rtl_pll_power_up(tp);
6815 if (!rtl8169_init_counter_offsets(tp))
6816 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6818 phy_start(dev->phydev);
6819 netif_start_queue(dev);
6821 rtl_unlock_work(tp);
6823 pm_runtime_put_sync(&pdev->dev);
6828 pci_free_irq(pdev, 0, tp);
6830 rtl_release_firmware(tp);
6831 rtl8169_rx_clear(tp);
6833 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6835 tp->RxDescArray = NULL;
6837 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6839 tp->TxDescArray = NULL;
6841 pm_runtime_put_noidle(&pdev->dev);
6846 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6848 struct rtl8169_private *tp = netdev_priv(dev);
6849 struct pci_dev *pdev = tp->pci_dev;
6850 struct rtl8169_counters *counters = tp->counters;
6853 pm_runtime_get_noresume(&pdev->dev);
6855 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
6856 rtl8169_rx_missed(dev);
6859 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
6860 stats->rx_packets = tp->rx_stats.packets;
6861 stats->rx_bytes = tp->rx_stats.bytes;
6862 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
6865 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
6866 stats->tx_packets = tp->tx_stats.packets;
6867 stats->tx_bytes = tp->tx_stats.bytes;
6868 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
6870 stats->rx_dropped = dev->stats.rx_dropped;
6871 stats->tx_dropped = dev->stats.tx_dropped;
6872 stats->rx_length_errors = dev->stats.rx_length_errors;
6873 stats->rx_errors = dev->stats.rx_errors;
6874 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6875 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6876 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6877 stats->multicast = dev->stats.multicast;
6880 * Fetch additonal counter values missing in stats collected by driver
6881 * from tally counters.
6883 if (pm_runtime_active(&pdev->dev))
6884 rtl8169_update_counters(tp);
6887 * Subtract values fetched during initalization.
6888 * See rtl8169_init_counter_offsets for a description why we do that.
6890 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
6891 le64_to_cpu(tp->tc_offset.tx_errors);
6892 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
6893 le32_to_cpu(tp->tc_offset.tx_multi_collision);
6894 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
6895 le16_to_cpu(tp->tc_offset.tx_aborted);
6897 pm_runtime_put_noidle(&pdev->dev);
6900 static void rtl8169_net_suspend(struct net_device *dev)
6902 struct rtl8169_private *tp = netdev_priv(dev);
6904 if (!netif_running(dev))
6907 phy_stop(dev->phydev);
6908 netif_device_detach(dev);
6909 netif_stop_queue(dev);
6912 napi_disable(&tp->napi);
6913 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6914 rtl_unlock_work(tp);
6916 rtl_pll_power_down(tp);
6921 static int rtl8169_suspend(struct device *device)
6923 struct pci_dev *pdev = to_pci_dev(device);
6924 struct net_device *dev = pci_get_drvdata(pdev);
6926 rtl8169_net_suspend(dev);
6931 static void __rtl8169_resume(struct net_device *dev)
6933 struct rtl8169_private *tp = netdev_priv(dev);
6935 netif_device_attach(dev);
6937 rtl_pll_power_up(tp);
6938 rtl8169_init_phy(dev, tp);
6940 phy_start(tp->dev->phydev);
6943 napi_enable(&tp->napi);
6944 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6945 rtl_unlock_work(tp);
6947 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6950 static int rtl8169_resume(struct device *device)
6952 struct pci_dev *pdev = to_pci_dev(device);
6953 struct net_device *dev = pci_get_drvdata(pdev);
6955 if (netif_running(dev))
6956 __rtl8169_resume(dev);
6961 static int rtl8169_runtime_suspend(struct device *device)
6963 struct pci_dev *pdev = to_pci_dev(device);
6964 struct net_device *dev = pci_get_drvdata(pdev);
6965 struct rtl8169_private *tp = netdev_priv(dev);
6967 if (!tp->TxDescArray) {
6968 rtl_pll_power_down(tp);
6973 __rtl8169_set_wol(tp, WAKE_ANY);
6974 rtl_unlock_work(tp);
6976 rtl8169_net_suspend(dev);
6978 /* Update counters before going runtime suspend */
6979 rtl8169_rx_missed(dev);
6980 rtl8169_update_counters(tp);
6985 static int rtl8169_runtime_resume(struct device *device)
6987 struct pci_dev *pdev = to_pci_dev(device);
6988 struct net_device *dev = pci_get_drvdata(pdev);
6989 struct rtl8169_private *tp = netdev_priv(dev);
6990 rtl_rar_set(tp, dev->dev_addr);
6992 if (!tp->TxDescArray)
6996 __rtl8169_set_wol(tp, tp->saved_wolopts);
6997 rtl_unlock_work(tp);
6999 __rtl8169_resume(dev);
7004 static int rtl8169_runtime_idle(struct device *device)
7006 struct pci_dev *pdev = to_pci_dev(device);
7007 struct net_device *dev = pci_get_drvdata(pdev);
7009 if (!netif_running(dev) || !netif_carrier_ok(dev))
7010 pm_schedule_suspend(device, 10000);
7015 static const struct dev_pm_ops rtl8169_pm_ops = {
7016 .suspend = rtl8169_suspend,
7017 .resume = rtl8169_resume,
7018 .freeze = rtl8169_suspend,
7019 .thaw = rtl8169_resume,
7020 .poweroff = rtl8169_suspend,
7021 .restore = rtl8169_resume,
7022 .runtime_suspend = rtl8169_runtime_suspend,
7023 .runtime_resume = rtl8169_runtime_resume,
7024 .runtime_idle = rtl8169_runtime_idle,
7027 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
7029 #else /* !CONFIG_PM */
7031 #define RTL8169_PM_OPS NULL
7033 #endif /* !CONFIG_PM */
7035 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7037 /* WoL fails with 8168b when the receiver is disabled. */
7038 switch (tp->mac_version) {
7039 case RTL_GIGA_MAC_VER_11:
7040 case RTL_GIGA_MAC_VER_12:
7041 case RTL_GIGA_MAC_VER_17:
7042 pci_clear_master(tp->pci_dev);
7044 RTL_W8(tp, ChipCmd, CmdRxEnb);
7046 RTL_R8(tp, ChipCmd);
7053 static void rtl_shutdown(struct pci_dev *pdev)
7055 struct net_device *dev = pci_get_drvdata(pdev);
7056 struct rtl8169_private *tp = netdev_priv(dev);
7058 rtl8169_net_suspend(dev);
7060 /* Restore original MAC address */
7061 rtl_rar_set(tp, dev->perm_addr);
7063 rtl8169_hw_reset(tp);
7065 if (system_state == SYSTEM_POWER_OFF) {
7066 if (tp->saved_wolopts) {
7067 rtl_wol_suspend_quirk(tp);
7068 rtl_wol_shutdown_quirk(tp);
7071 pci_wake_from_d3(pdev, true);
7072 pci_set_power_state(pdev, PCI_D3hot);
7076 static void rtl_remove_one(struct pci_dev *pdev)
7078 struct net_device *dev = pci_get_drvdata(pdev);
7079 struct rtl8169_private *tp = netdev_priv(dev);
7081 if (r8168_check_dash(tp))
7082 rtl8168_driver_stop(tp);
7084 netif_napi_del(&tp->napi);
7086 unregister_netdev(dev);
7087 mdiobus_unregister(tp->mii_bus);
7089 rtl_release_firmware(tp);
7091 if (pci_dev_run_wake(pdev))
7092 pm_runtime_get_noresume(&pdev->dev);
7094 /* restore original MAC address */
7095 rtl_rar_set(tp, dev->perm_addr);
7098 static const struct net_device_ops rtl_netdev_ops = {
7099 .ndo_open = rtl_open,
7100 .ndo_stop = rtl8169_close,
7101 .ndo_get_stats64 = rtl8169_get_stats64,
7102 .ndo_start_xmit = rtl8169_start_xmit,
7103 .ndo_tx_timeout = rtl8169_tx_timeout,
7104 .ndo_validate_addr = eth_validate_addr,
7105 .ndo_change_mtu = rtl8169_change_mtu,
7106 .ndo_fix_features = rtl8169_fix_features,
7107 .ndo_set_features = rtl8169_set_features,
7108 .ndo_set_mac_address = rtl_set_mac_address,
7109 .ndo_do_ioctl = rtl8169_ioctl,
7110 .ndo_set_rx_mode = rtl_set_rx_mode,
7111 #ifdef CONFIG_NET_POLL_CONTROLLER
7112 .ndo_poll_controller = rtl8169_netpoll,
7117 static const struct rtl_cfg_info {
7118 void (*hw_start)(struct rtl8169_private *tp);
7120 unsigned int has_gmii:1;
7121 const struct rtl_coalesce_info *coalesce_info;
7123 } rtl_cfg_infos [] = {
7125 .hw_start = rtl_hw_start_8169,
7126 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7128 .coalesce_info = rtl_coalesce_info_8169,
7129 .default_ver = RTL_GIGA_MAC_VER_01,
7132 .hw_start = rtl_hw_start_8168,
7133 .event_slow = SYSErr | LinkChg | RxOverflow,
7135 .coalesce_info = rtl_coalesce_info_8168_8136,
7136 .default_ver = RTL_GIGA_MAC_VER_11,
7139 .hw_start = rtl_hw_start_8101,
7140 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7142 .coalesce_info = rtl_coalesce_info_8168_8136,
7143 .default_ver = RTL_GIGA_MAC_VER_13,
7147 static int rtl_alloc_irq(struct rtl8169_private *tp)
7151 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
7152 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7153 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7154 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
7155 flags = PCI_IRQ_LEGACY;
7157 flags = PCI_IRQ_ALL_TYPES;
7160 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
7163 DECLARE_RTL_COND(rtl_link_list_ready_cond)
7165 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
7168 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7170 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7173 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7175 struct rtl8169_private *tp = mii_bus->priv;
7180 return rtl_readphy(tp, phyreg);
7183 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7184 int phyreg, u16 val)
7186 struct rtl8169_private *tp = mii_bus->priv;
7191 rtl_writephy(tp, phyreg, val);
7196 static int r8169_mdio_register(struct rtl8169_private *tp)
7198 struct pci_dev *pdev = tp->pci_dev;
7199 struct phy_device *phydev;
7200 struct mii_bus *new_bus;
7203 new_bus = devm_mdiobus_alloc(&pdev->dev);
7207 new_bus->name = "r8169";
7209 new_bus->parent = &pdev->dev;
7210 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7211 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7212 PCI_DEVID(pdev->bus->number, pdev->devfn));
7214 new_bus->read = r8169_mdio_read_reg;
7215 new_bus->write = r8169_mdio_write_reg;
7217 ret = mdiobus_register(new_bus);
7221 phydev = mdiobus_get_phy(new_bus, 0);
7223 mdiobus_unregister(new_bus);
7227 /* PHY will be woken up in rtl_open() */
7228 phy_suspend(phydev);
7230 tp->mii_bus = new_bus;
7235 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
7239 tp->ocp_base = OCP_STD_PHY_BASE;
7241 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
7243 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7246 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7249 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7251 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
7253 data = r8168_mac_ocp_read(tp, 0xe8de);
7255 r8168_mac_ocp_write(tp, 0xe8de, data);
7257 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7260 data = r8168_mac_ocp_read(tp, 0xe8de);
7262 r8168_mac_ocp_write(tp, 0xe8de, data);
7264 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7268 static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7270 rtl8168ep_stop_cmac(tp);
7271 rtl_hw_init_8168g(tp);
7274 static void rtl_hw_initialize(struct rtl8169_private *tp)
7276 switch (tp->mac_version) {
7277 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
7278 rtl_hw_init_8168g(tp);
7280 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
7281 rtl_hw_init_8168ep(tp);
7288 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7290 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7291 struct rtl8169_private *tp;
7292 struct net_device *dev;
7293 int chipset, region, i;
7296 if (netif_msg_drv(&debug)) {
7297 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7298 MODULENAME, RTL8169_VERSION);
7301 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7305 SET_NETDEV_DEV(dev, &pdev->dev);
7306 dev->netdev_ops = &rtl_netdev_ops;
7307 tp = netdev_priv(dev);
7310 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7311 tp->supports_gmii = cfg->has_gmii;
7313 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7314 rc = pcim_enable_device(pdev);
7316 dev_err(&pdev->dev, "enable failure\n");
7320 if (pcim_set_mwi(pdev) < 0)
7321 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
7323 /* use first MMIO region */
7324 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7326 dev_err(&pdev->dev, "no MMIO resource found\n");
7330 /* check for weird/broken PCI region reporting */
7331 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7332 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
7336 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
7338 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
7342 tp->mmio_addr = pcim_iomap_table(pdev)[region];
7344 if (!pci_is_pcie(pdev))
7345 dev_info(&pdev->dev, "not PCI Express\n");
7347 /* Identify chip attached to board */
7348 rtl8169_get_mac_version(tp, cfg->default_ver);
7350 if (rtl_tbi_enabled(tp)) {
7351 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7355 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
7357 if ((sizeof(dma_addr_t) > 4) &&
7358 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7359 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
7360 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7361 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
7363 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7364 if (!pci_is_pcie(pdev))
7365 tp->cp_cmd |= PCIDAC;
7366 dev->features |= NETIF_F_HIGHDMA;
7368 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7370 dev_err(&pdev->dev, "DMA configuration failed\n");
7377 rtl_irq_disable(tp);
7379 rtl_hw_initialize(tp);
7383 rtl_ack_events(tp, 0xffff);
7385 pci_set_master(pdev);
7387 rtl_init_mdio_ops(tp);
7388 rtl_init_jumbo_ops(tp);
7390 rtl8169_print_mac_version(tp);
7392 chipset = tp->mac_version;
7394 rc = rtl_alloc_irq(tp);
7396 dev_err(&pdev->dev, "Can't allocate interrupt\n");
7400 /* override BIOS settings, use userspace tools to enable WOL */
7401 __rtl8169_set_wol(tp, 0);
7403 mutex_init(&tp->wk.mutex);
7404 u64_stats_init(&tp->rx_stats.syncp);
7405 u64_stats_init(&tp->tx_stats.syncp);
7407 /* Get MAC address */
7408 switch (tp->mac_version) {
7409 u8 mac_addr[ETH_ALEN] __aligned(4);
7410 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7411 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
7412 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
7413 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
7415 if (is_valid_ether_addr(mac_addr))
7416 rtl_rar_set(tp, mac_addr);
7421 for (i = 0; i < ETH_ALEN; i++)
7422 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
7424 dev->ethtool_ops = &rtl8169_ethtool_ops;
7425 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
7427 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
7429 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7430 * properly for all devices */
7431 dev->features |= NETIF_F_RXCSUM |
7432 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
7434 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7435 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7436 NETIF_F_HW_VLAN_CTAG_RX;
7437 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7440 tp->cp_cmd |= RxChkSum | RxVlan;
7443 * Pretend we are using VLANs; This bypasses a nasty bug where
7444 * Interrupts stop flowing on high load on 8110SCd controllers.
7446 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7447 /* Disallow toggling */
7448 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
7450 switch (rtl_chip_infos[chipset].txd_version) {
7452 tp->tso_csum = rtl8169_tso_csum_v1;
7455 tp->tso_csum = rtl8169_tso_csum_v2;
7456 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7462 dev->hw_features |= NETIF_F_RXALL;
7463 dev->hw_features |= NETIF_F_RXFCS;
7465 /* MTU range: 60 - hw-specific max */
7466 dev->min_mtu = ETH_ZLEN;
7467 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
7469 tp->hw_start = cfg->hw_start;
7470 tp->event_slow = cfg->event_slow;
7471 tp->coalesce_info = cfg->coalesce_info;
7473 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7475 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7476 &tp->counters_phys_addr,
7481 pci_set_drvdata(pdev, dev);
7483 rc = r8169_mdio_register(tp);
7487 rc = register_netdev(dev);
7489 goto err_mdio_unregister;
7491 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7492 rtl_chip_infos[chipset].name, dev->dev_addr,
7493 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
7494 pci_irq_vector(pdev, 0));
7495 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7496 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7497 "tx checksumming: %s]\n",
7498 rtl_chip_infos[chipset].jumbo_max,
7499 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
7502 if (r8168_check_dash(tp))
7503 rtl8168_driver_start(tp);
7505 if (pci_dev_run_wake(pdev))
7506 pm_runtime_put_sync(&pdev->dev);
7510 err_mdio_unregister:
7511 mdiobus_unregister(tp->mii_bus);
7515 static struct pci_driver rtl8169_pci_driver = {
7517 .id_table = rtl8169_pci_tbl,
7518 .probe = rtl_init_one,
7519 .remove = rtl_remove_one,
7520 .shutdown = rtl_shutdown,
7521 .driver.pm = RTL8169_PM_OPS,
7524 module_pci_driver(rtl8169_pci_driver);